blob: a05de030d91f5eee65a37166afe064d8946cb6b2 [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
Sunil Ravi99c035e2024-07-12 01:42:03 +0000187 hostapd_neighbor_sync_own_report(hapd);
188
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700189 ieee802_11_set_beacon(hapd);
190 hostapd_update_wps(hapd);
191
192 if (hapd->conf->ssid.ssid_set &&
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700193 hostapd_set_ssid(hapd, hapd->conf->ssid.ssid,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700194 hapd->conf->ssid.ssid_len)) {
195 wpa_printf(MSG_ERROR, "Could not set SSID for kernel driver");
196 /* try to continue */
197 }
198 wpa_printf(MSG_DEBUG, "Reconfigured interface %s", hapd->conf->iface);
199}
200
201
Sunil Ravi77d572f2023-01-17 23:58:31 +0000202static void hostapd_clear_old_bss(struct hostapd_data *bss)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700203{
Sunil Ravi77d572f2023-01-17 23:58:31 +0000204 wpa_printf(MSG_DEBUG, "BSS %s changed - clear old state",
205 bss->conf->iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700206
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700207 /*
208 * Deauthenticate all stations since the new configuration may not
209 * allow them to use the BSS anymore.
210 */
Sunil Ravi77d572f2023-01-17 23:58:31 +0000211 hostapd_flush_old_stations(bss, WLAN_REASON_PREV_AUTH_NOT_VALID);
Hai Shalomfdcde762020-04-02 11:19:20 -0700212#ifdef CONFIG_WEP
Sunil Ravi77d572f2023-01-17 23:58:31 +0000213 hostapd_broadcast_wep_clear(bss);
Hai Shalomfdcde762020-04-02 11:19:20 -0700214#endif /* CONFIG_WEP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700215
216#ifndef CONFIG_NO_RADIUS
Sunil Ravi77d572f2023-01-17 23:58:31 +0000217 /* TODO: update dynamic data based on changed configuration
218 * items (e.g., open/close sockets, etc.) */
219 radius_client_flush(bss->radius, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700220#endif /* CONFIG_NO_RADIUS */
Sunil Ravi77d572f2023-01-17 23:58:31 +0000221}
222
223
224static void hostapd_clear_old(struct hostapd_iface *iface)
225{
226 size_t j;
227
228 for (j = 0; j < iface->num_bss; j++)
229 hostapd_clear_old_bss(iface->bss[j]);
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700230}
231
232
Hai Shalom74f70d42019-02-11 14:42:39 -0800233static int hostapd_iface_conf_changed(struct hostapd_config *newconf,
234 struct hostapd_config *oldconf)
235{
236 size_t i;
237
238 if (newconf->num_bss != oldconf->num_bss)
239 return 1;
240
241 for (i = 0; i < newconf->num_bss; i++) {
242 if (os_strcmp(newconf->bss[i]->iface,
243 oldconf->bss[i]->iface) != 0)
244 return 1;
245 }
246
247 return 0;
248}
249
250
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700251int hostapd_reload_config(struct hostapd_iface *iface)
252{
Hai Shalom74f70d42019-02-11 14:42:39 -0800253 struct hapd_interfaces *interfaces = iface->interfaces;
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700254 struct hostapd_data *hapd = iface->bss[0];
255 struct hostapd_config *newconf, *oldconf;
256 size_t j;
257
258 if (iface->config_fname == NULL) {
259 /* Only in-memory config in use - assume it has been updated */
260 hostapd_clear_old(iface);
261 for (j = 0; j < iface->num_bss; j++)
262 hostapd_reload_bss(iface->bss[j]);
263 return 0;
264 }
265
266 if (iface->interfaces == NULL ||
267 iface->interfaces->config_read_cb == NULL)
268 return -1;
269 newconf = iface->interfaces->config_read_cb(iface->config_fname);
270 if (newconf == NULL)
271 return -1;
272
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700273 oldconf = hapd->iconf;
Hai Shalom74f70d42019-02-11 14:42:39 -0800274 if (hostapd_iface_conf_changed(newconf, oldconf)) {
275 char *fname;
276 int res;
277
Sunil Ravi77d572f2023-01-17 23:58:31 +0000278 hostapd_clear_old(iface);
279
Hai Shalom74f70d42019-02-11 14:42:39 -0800280 wpa_printf(MSG_DEBUG,
281 "Configuration changes include interface/BSS modification - force full disable+enable sequence");
282 fname = os_strdup(iface->config_fname);
283 if (!fname) {
284 hostapd_config_free(newconf);
285 return -1;
286 }
287 hostapd_remove_iface(interfaces, hapd->conf->iface);
288 iface = hostapd_init(interfaces, fname);
289 os_free(fname);
290 hostapd_config_free(newconf);
291 if (!iface) {
292 wpa_printf(MSG_ERROR,
293 "Failed to initialize interface on config reload");
294 return -1;
295 }
296 iface->interfaces = interfaces;
297 interfaces->iface[interfaces->count] = iface;
298 interfaces->count++;
299 res = hostapd_enable_iface(iface);
300 if (res < 0)
301 wpa_printf(MSG_ERROR,
302 "Failed to enable interface on config reload");
303 return res;
304 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700305 iface->conf = newconf;
306
307 for (j = 0; j < iface->num_bss; j++) {
308 hapd = iface->bss[j];
Sunil Ravi77d572f2023-01-17 23:58:31 +0000309 if (!hapd->conf->config_id || !newconf->bss[j]->config_id ||
310 os_strcmp(hapd->conf->config_id,
311 newconf->bss[j]->config_id) != 0)
312 hostapd_clear_old_bss(hapd);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700313 hapd->iconf = newconf;
Dmitry Shmidtd11f0192014-03-24 12:09:47 -0700314 hapd->iconf->channel = oldconf->channel;
Dmitry Shmidtdda10c22015-03-24 16:05:01 -0700315 hapd->iconf->acs = oldconf->acs;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -0700316 hapd->iconf->secondary_channel = oldconf->secondary_channel;
Dmitry Shmidtd11f0192014-03-24 12:09:47 -0700317 hapd->iconf->ieee80211n = oldconf->ieee80211n;
318 hapd->iconf->ieee80211ac = oldconf->ieee80211ac;
319 hapd->iconf->ht_capab = oldconf->ht_capab;
320 hapd->iconf->vht_capab = oldconf->vht_capab;
Hai Shalom81f62d82019-07-22 12:10:00 -0700321 hostapd_set_oper_chwidth(hapd->iconf,
322 hostapd_get_oper_chwidth(oldconf));
323 hostapd_set_oper_centr_freq_seg0_idx(
324 hapd->iconf,
325 hostapd_get_oper_centr_freq_seg0_idx(oldconf));
326 hostapd_set_oper_centr_freq_seg1_idx(
327 hapd->iconf,
328 hostapd_get_oper_centr_freq_seg1_idx(oldconf));
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800329 hapd->conf = newconf->bss[j];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700330 hostapd_reload_bss(hapd);
331 }
332
333 hostapd_config_free(oldconf);
334
335
336 return 0;
337}
338
339
Hai Shalomfdcde762020-04-02 11:19:20 -0700340#ifdef CONFIG_WEP
341
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700342static void hostapd_broadcast_key_clear_iface(struct hostapd_data *hapd,
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700343 const char *ifname)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700344{
345 int i;
346
Dmitry Shmidt29333592017-01-09 12:27:11 -0800347 if (!ifname || !hapd->drv_priv)
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700348 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700349 for (i = 0; i < NUM_WEP_KEYS; i++) {
Hai Shalomfdcde762020-04-02 11:19:20 -0700350 if (hostapd_drv_set_key(ifname, hapd, WPA_ALG_NONE, NULL, i, 0,
351 0, NULL, 0, NULL, 0, KEY_FLAG_GROUP)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700352 wpa_printf(MSG_DEBUG, "Failed to clear default "
353 "encryption keys (ifname=%s keyidx=%d)",
354 ifname, i);
355 }
356 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700357 if (hapd->conf->ieee80211w) {
358 for (i = NUM_WEP_KEYS; i < NUM_WEP_KEYS + 2; i++) {
359 if (hostapd_drv_set_key(ifname, hapd, WPA_ALG_NONE,
Hai Shalomfdcde762020-04-02 11:19:20 -0700360 NULL, i, 0, 0, NULL,
361 0, NULL, 0, KEY_FLAG_GROUP)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700362 wpa_printf(MSG_DEBUG, "Failed to clear "
363 "default mgmt encryption keys "
364 "(ifname=%s keyidx=%d)", ifname, i);
365 }
366 }
367 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700368}
369
370
371static int hostapd_broadcast_wep_clear(struct hostapd_data *hapd)
372{
373 hostapd_broadcast_key_clear_iface(hapd, hapd->conf->iface);
374 return 0;
375}
376
377
378static int hostapd_broadcast_wep_set(struct hostapd_data *hapd)
379{
380 int errors = 0, idx;
381 struct hostapd_ssid *ssid = &hapd->conf->ssid;
382
383 idx = ssid->wep.idx;
Hai Shalomfdcde762020-04-02 11:19:20 -0700384 if (ssid->wep.default_len && ssid->wep.key[idx] &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700385 hostapd_drv_set_key(hapd->conf->iface,
Hai Shalomfdcde762020-04-02 11:19:20 -0700386 hapd, WPA_ALG_WEP, broadcast_ether_addr, idx, 0,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700387 1, NULL, 0, ssid->wep.key[idx],
Hai Shalomfdcde762020-04-02 11:19:20 -0700388 ssid->wep.len[idx],
389 KEY_FLAG_GROUP_RX_TX_DEFAULT)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700390 wpa_printf(MSG_WARNING, "Could not set WEP encryption.");
391 errors++;
392 }
393
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700394 return errors;
395}
396
Hai Shalomfdcde762020-04-02 11:19:20 -0700397#endif /* CONFIG_WEP */
398
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700399
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000400#ifdef CONFIG_IEEE80211BE
401#ifdef CONFIG_TESTING_OPTIONS
402
403#define TU_TO_USEC(_val) ((_val) * 1024)
404
405static void hostapd_link_remove_timeout_handler(void *eloop_data,
406 void *user_ctx)
407{
408 struct hostapd_data *hapd = (struct hostapd_data *) eloop_data;
409
410 if (hapd->eht_mld_link_removal_count == 0)
411 return;
412 hapd->eht_mld_link_removal_count--;
413
414 wpa_printf(MSG_DEBUG, "MLD: Remove link_id=%u in %u beacons",
415 hapd->mld_link_id,
416 hapd->eht_mld_link_removal_count);
417
418 ieee802_11_set_beacon(hapd);
419
420 if (!hapd->eht_mld_link_removal_count) {
Sunil Ravi99c035e2024-07-12 01:42:03 +0000421 hostapd_free_link_stas(hapd);
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000422 hostapd_disable_iface(hapd->iface);
423 return;
424 }
425
426 eloop_register_timeout(0, TU_TO_USEC(hapd->iconf->beacon_int),
427 hostapd_link_remove_timeout_handler,
428 hapd, NULL);
429}
430
431
432int hostapd_link_remove(struct hostapd_data *hapd, u32 count)
433{
434 if (!hapd->conf->mld_ap)
435 return -1;
436
437 wpa_printf(MSG_DEBUG,
438 "MLD: Remove link_id=%u in %u beacons",
439 hapd->mld_link_id, count);
440
441 hapd->eht_mld_link_removal_count = count;
442 hapd->eht_mld_bss_param_change++;
443
444 eloop_register_timeout(0, TU_TO_USEC(hapd->iconf->beacon_int),
445 hostapd_link_remove_timeout_handler,
446 hapd, NULL);
447
448 ieee802_11_set_beacon(hapd);
449 return 0;
450}
451
452#endif /* CONFIG_TESTING_OPTIONS */
453#endif /* CONFIG_IEEE80211BE */
454
455
Hai Shalom60840252021-02-19 19:02:11 -0800456void hostapd_free_hapd_data(struct hostapd_data *hapd)
Dmitry Shmidt04949592012-07-19 12:16:46 -0700457{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800458 os_free(hapd->probereq_cb);
459 hapd->probereq_cb = NULL;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800460 hapd->num_probereq_cb = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800461
462#ifdef CONFIG_P2P
463 wpabuf_free(hapd->p2p_beacon_ie);
464 hapd->p2p_beacon_ie = NULL;
465 wpabuf_free(hapd->p2p_probe_resp_ie);
466 hapd->p2p_probe_resp_ie = NULL;
467#endif /* CONFIG_P2P */
468
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -0800469 if (!hapd->started) {
470 wpa_printf(MSG_ERROR, "%s: Interface %s wasn't started",
Hai Shalom021b0b52019-04-10 11:17:58 -0700471 __func__, hapd->conf ? hapd->conf->iface : "N/A");
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -0800472 return;
473 }
474 hapd->started = 0;
Hai Shalom5f92bc92019-04-18 11:54:11 -0700475 hapd->beacon_set_done = 0;
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -0800476
Dmitry Shmidt54605472013-11-08 11:10:19 -0800477 wpa_printf(MSG_DEBUG, "%s(%s)", __func__, hapd->conf->iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700478 accounting_deinit(hapd);
479 hostapd_deinit_wpa(hapd);
480 vlan_deinit(hapd);
481 hostapd_acl_deinit(hapd);
482#ifndef CONFIG_NO_RADIUS
Sunil Ravi99c035e2024-07-12 01:42:03 +0000483 if (hostapd_mld_is_first_bss(hapd)) {
484#ifdef CONFIG_IEEE80211BE
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000485 struct hapd_interfaces *ifaces = hapd->iface->interfaces;
486 size_t i;
487
488 for (i = 0; i < ifaces->count; i++) {
489 struct hostapd_iface *iface = ifaces->iface[i];
490 size_t j;
491
492 for (j = 0; iface && j < iface->num_bss; j++) {
493 struct hostapd_data *h = iface->bss[j];
494
495 if (hapd == h)
496 continue;
497 if (h->radius == hapd->radius)
498 h->radius = NULL;
499 if (h->radius_das == hapd->radius_das)
500 h->radius_das = NULL;
501 }
502 }
Sunil Ravi99c035e2024-07-12 01:42:03 +0000503#endif /* CONFIG_IEEE80211BE */
Sunil Ravi2a14cf12023-11-21 00:54:38 +0000504 radius_client_deinit(hapd->radius);
505 radius_das_deinit(hapd->radius_das);
506 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700507 hapd->radius = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700508 hapd->radius_das = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700509#endif /* CONFIG_NO_RADIUS */
510
511 hostapd_deinit_wps(hapd);
Hai Shalom81f62d82019-07-22 12:10:00 -0700512 ieee802_1x_dealloc_kay_sm_hapd(hapd);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700513#ifdef CONFIG_DPP
514 hostapd_dpp_deinit(hapd);
515 gas_query_ap_deinit(hapd->gas);
Hai Shaloma20dcd72022-02-04 13:43:00 -0800516 hapd->gas = NULL;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700517#endif /* CONFIG_DPP */
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000518#ifdef CONFIG_NAN_USD
519 hostapd_nan_usd_deinit(hapd);
520#endif /* CONFIG_NAN_USD */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700521
522 authsrv_deinit(hapd);
523
Dmitry Shmidt71757432014-06-02 13:50:35 -0700524 if (hapd->interface_added) {
525 hapd->interface_added = 0;
526 if (hostapd_if_remove(hapd, WPA_IF_AP_BSS, hapd->conf->iface)) {
527 wpa_printf(MSG_WARNING,
528 "Failed to remove BSS interface %s",
529 hapd->conf->iface);
530 hapd->interface_added = 1;
531 } else {
532 /*
533 * Since this was a dynamically added interface, the
534 * driver wrapper may have removed its internal instance
535 * and hapd->drv_priv is not valid anymore.
536 */
Sunil Ravi99c035e2024-07-12 01:42:03 +0000537 hapd->drv_priv = NULL;
Dmitry Shmidt71757432014-06-02 13:50:35 -0700538 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700539 }
540
Sunil Ravi99c035e2024-07-12 01:42:03 +0000541#ifdef CONFIG_IEEE80211BE
542 /* If the interface was not added as well as it is not the first BSS,
543 * at least the link should be removed here since deinit will take care
544 * of only the first BSS. */
545 if (hapd->conf->mld_ap && !hapd->interface_added &&
546 hapd->iface->bss[0] != hapd)
547 hostapd_if_link_remove(hapd, WPA_IF_AP_BSS, hapd->conf->iface,
548 hapd->mld_link_id);
549#endif /* CONFIG_IEEE80211BE */
550
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800551 wpabuf_free(hapd->time_adv);
Hai Shaloma20dcd72022-02-04 13:43:00 -0800552 hapd->time_adv = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700553
554#ifdef CONFIG_INTERWORKING
555 gas_serv_deinit(hapd);
556#endif /* CONFIG_INTERWORKING */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800557
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800558 bss_load_update_deinit(hapd);
559 ndisc_snoop_deinit(hapd);
560 dhcp_snoop_deinit(hapd);
561 x_snoop_deinit(hapd);
562
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800563#ifdef CONFIG_SQLITE
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700564 bin_clear_free(hapd->tmp_eap_user.identity,
565 hapd->tmp_eap_user.identity_len);
566 bin_clear_free(hapd->tmp_eap_user.password,
567 hapd->tmp_eap_user.password_len);
Hai Shaloma20dcd72022-02-04 13:43:00 -0800568 os_memset(&hapd->tmp_eap_user, 0, sizeof(hapd->tmp_eap_user));
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800569#endif /* CONFIG_SQLITE */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800570
571#ifdef CONFIG_MESH
572 wpabuf_free(hapd->mesh_pending_auth);
573 hapd->mesh_pending_auth = NULL;
Hai Shalom60840252021-02-19 19:02:11 -0800574 /* handling setup failure is already done */
575 hapd->setup_complete_cb = NULL;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800576#endif /* CONFIG_MESH */
Dmitry Shmidt849734c2016-05-27 09:59:01 -0700577
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000578#ifndef CONFIG_NO_RRM
Dmitry Shmidt849734c2016-05-27 09:59:01 -0700579 hostapd_clean_rrm(hapd);
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000580#endif /* CONFIG_NO_RRM */
Dmitry Shmidtebd93af2017-02-21 13:40:44 -0800581 fils_hlp_deinit(hapd);
Hai Shalom021b0b52019-04-10 11:17:58 -0700582
Hai Shalom899fcc72020-10-19 14:38:18 -0700583#ifdef CONFIG_OCV
584 eloop_cancel_timeout(hostapd_ocv_check_csa_sa_query, hapd, NULL);
585#endif /* CONFIG_OCV */
586
Hai Shalom021b0b52019-04-10 11:17:58 -0700587#ifdef CONFIG_SAE
588 {
589 struct hostapd_sae_commit_queue *q;
590
591 while ((q = dl_list_first(&hapd->sae_commit_queue,
592 struct hostapd_sae_commit_queue,
593 list))) {
594 dl_list_del(&q->list);
595 os_free(q);
596 }
597 }
598 eloop_cancel_timeout(auth_sae_process_commit, hapd, NULL);
599#endif /* CONFIG_SAE */
Sunil Ravia04bd252022-05-02 22:54:18 -0700600
601#ifdef CONFIG_IEEE80211AX
602 eloop_cancel_timeout(hostapd_switch_color_timeout_handler, hapd, NULL);
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000603#ifdef CONFIG_TESTING_OPTIONS
604#ifdef CONFIG_IEEE80211BE
605 eloop_cancel_timeout(hostapd_link_remove_timeout_handler, hapd, NULL);
606#endif /* CONFIG_IEEE80211BE */
607#endif /* CONFIG_TESTING_OPTIONS */
608
Sunil Ravia04bd252022-05-02 22:54:18 -0700609#endif /* CONFIG_IEEE80211AX */
Dmitry Shmidt04949592012-07-19 12:16:46 -0700610}
611
612
Sunil Ravi99c035e2024-07-12 01:42:03 +0000613/* hostapd_bss_link_deinit - Per-BSS ML cleanup (deinitialization)
614 * @hapd: Pointer to BSS data
615 *
616 * This function is used to unlink the BSS from the AP MLD.
617 * If the BSS being removed is the first link, the next link becomes the first
618 * link.
619 */
620static void hostapd_bss_link_deinit(struct hostapd_data *hapd)
621{
622#ifdef CONFIG_IEEE80211BE
623 if (!hapd->conf || !hapd->conf->mld_ap)
624 return;
625
626 if (!hapd->mld->num_links)
627 return;
628
629 /* If not started, not yet linked to the MLD. However, the first
630 * BSS is always linked since it is linked during driver_init(), and
631 * hence, need to remove it from the AP MLD.
632 */
633 if (!hapd->started && hapd->iface->bss[0] != hapd)
634 return;
635
636 /* The first BSS can also be only linked when at least driver_init() is
637 * executed. But if previous interface fails, it is not, and hence,
638 * safe to skip.
639 */
640 if (hapd->iface->bss[0] == hapd && !hapd->drv_priv)
641 return;
642
643 hostapd_mld_remove_link(hapd);
644#endif /* CONFIG_IEEE80211BE */
645}
646
647
Dmitry Shmidt04949592012-07-19 12:16:46 -0700648/**
649 * hostapd_cleanup - Per-BSS cleanup (deinitialization)
650 * @hapd: Pointer to BSS data
651 *
652 * This function is used to free all per-BSS data structures and resources.
Dmitry Shmidt54605472013-11-08 11:10:19 -0800653 * Most of the modules that are initialized in hostapd_setup_bss() are
654 * deinitialized here.
Dmitry Shmidt04949592012-07-19 12:16:46 -0700655 */
656static void hostapd_cleanup(struct hostapd_data *hapd)
657{
Dmitry Shmidt54605472013-11-08 11:10:19 -0800658 wpa_printf(MSG_DEBUG, "%s(hapd=%p (%s))", __func__, hapd,
Hai Shalom021b0b52019-04-10 11:17:58 -0700659 hapd->conf ? hapd->conf->iface : "N/A");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700660 if (hapd->iface->interfaces &&
Dmitry Shmidt29333592017-01-09 12:27:11 -0800661 hapd->iface->interfaces->ctrl_iface_deinit) {
662 wpa_msg(hapd->msg_ctx, MSG_INFO, WPA_EVENT_TERMINATING);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700663 hapd->iface->interfaces->ctrl_iface_deinit(hapd);
Dmitry Shmidt29333592017-01-09 12:27:11 -0800664 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700665 hostapd_free_hapd_data(hapd);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700666}
667
668
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800669static void sta_track_deinit(struct hostapd_iface *iface)
670{
671 struct hostapd_sta_info *info;
672
673 if (!iface->num_sta_seen)
674 return;
675
676 while ((info = dl_list_first(&iface->sta_seen, struct hostapd_sta_info,
677 list))) {
678 dl_list_del(&info->list);
679 iface->num_sta_seen--;
Dmitry Shmidtaca489e2016-09-28 15:44:14 -0700680 sta_track_del(info);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800681 }
682}
683
684
Hai Shalom60840252021-02-19 19:02:11 -0800685void hostapd_cleanup_iface_partial(struct hostapd_iface *iface)
Dmitry Shmidt04949592012-07-19 12:16:46 -0700686{
Dmitry Shmidt54605472013-11-08 11:10:19 -0800687 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
Sunil Ravi99c035e2024-07-12 01:42:03 +0000688 eloop_cancel_timeout(channel_list_update_timeout, iface, NULL);
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800689#ifdef NEED_AP_MLME
690 hostapd_stop_setup_timers(iface);
691#endif /* NEED_AP_MLME */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700692 if (iface->current_mode)
693 acs_cleanup(iface);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700694 hostapd_free_hw_features(iface->hw_features, iface->num_hw_features);
695 iface->hw_features = NULL;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700696 iface->current_mode = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700697 os_free(iface->current_rates);
698 iface->current_rates = NULL;
699 os_free(iface->basic_rates);
700 iface->basic_rates = NULL;
Sunil Ravi77d572f2023-01-17 23:58:31 +0000701 iface->cac_started = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700702 ap_list_deinit(iface);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800703 sta_track_deinit(iface);
Hai Shalom81f62d82019-07-22 12:10:00 -0700704 airtime_policy_update_deinit(iface);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700705}
706
707
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700708/**
709 * hostapd_cleanup_iface - Complete per-interface cleanup
710 * @iface: Pointer to interface data
711 *
712 * This function is called after per-BSS data structures are deinitialized
713 * with hostapd_cleanup().
714 */
715static void hostapd_cleanup_iface(struct hostapd_iface *iface)
716{
Dmitry Shmidt54605472013-11-08 11:10:19 -0800717 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
Roshan Pius3a1667e2018-07-03 15:17:14 -0700718 eloop_cancel_timeout(hostapd_interface_setup_failure_handler, iface,
719 NULL);
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800720
Dmitry Shmidt04949592012-07-19 12:16:46 -0700721 hostapd_cleanup_iface_partial(iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700722 hostapd_config_free(iface->conf);
723 iface->conf = NULL;
724
725 os_free(iface->config_fname);
726 os_free(iface->bss);
Dmitry Shmidt54605472013-11-08 11:10:19 -0800727 wpa_printf(MSG_DEBUG, "%s: free iface=%p", __func__, iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700728 os_free(iface);
729}
730
731
Hai Shalomfdcde762020-04-02 11:19:20 -0700732#ifdef CONFIG_WEP
733
Dmitry Shmidt04949592012-07-19 12:16:46 -0700734static void hostapd_clear_wep(struct hostapd_data *hapd)
735{
Hai Shalom021b0b52019-04-10 11:17:58 -0700736 if (hapd->drv_priv && !hapd->iface->driver_ap_teardown && hapd->conf) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700737 hostapd_set_privacy(hapd, 0);
738 hostapd_broadcast_wep_clear(hapd);
739 }
740}
741
742
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700743static int hostapd_setup_encryption(char *iface, struct hostapd_data *hapd)
744{
745 int i;
746
747 hostapd_broadcast_wep_set(hapd);
748
749 if (hapd->conf->ssid.wep.default_len) {
750 hostapd_set_privacy(hapd, 1);
751 return 0;
752 }
753
Jouni Malinen75ecf522011-06-27 15:19:46 -0700754 /*
755 * When IEEE 802.1X is not enabled, the driver may need to know how to
756 * set authentication algorithms for static WEP.
757 */
758 hostapd_drv_set_authmode(hapd, hapd->conf->auth_algs);
759
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700760 for (i = 0; i < 4; i++) {
761 if (hapd->conf->ssid.wep.key[i] &&
Hai Shalomfdcde762020-04-02 11:19:20 -0700762 hostapd_drv_set_key(iface, hapd, WPA_ALG_WEP, NULL, i, 0,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700763 i == hapd->conf->ssid.wep.idx, NULL, 0,
764 hapd->conf->ssid.wep.key[i],
Hai Shalomfdcde762020-04-02 11:19:20 -0700765 hapd->conf->ssid.wep.len[i],
766 i == hapd->conf->ssid.wep.idx ?
767 KEY_FLAG_GROUP_RX_TX_DEFAULT :
768 KEY_FLAG_GROUP_RX_TX)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700769 wpa_printf(MSG_WARNING, "Could not set WEP "
770 "encryption.");
771 return -1;
772 }
773 if (hapd->conf->ssid.wep.key[i] &&
774 i == hapd->conf->ssid.wep.idx)
775 hostapd_set_privacy(hapd, 1);
776 }
777
778 return 0;
779}
780
Hai Shalomfdcde762020-04-02 11:19:20 -0700781#endif /* CONFIG_WEP */
782
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700783
Dmitry Shmidt04949592012-07-19 12:16:46 -0700784static int hostapd_flush_old_stations(struct hostapd_data *hapd, u16 reason)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700785{
786 int ret = 0;
787 u8 addr[ETH_ALEN];
788
789 if (hostapd_drv_none(hapd) || hapd->drv_priv == NULL)
790 return 0;
791
Dmitry Shmidta38abf92014-03-06 13:38:44 -0800792 if (!hapd->iface->driver_ap_teardown) {
793 wpa_dbg(hapd->msg_ctx, MSG_DEBUG,
794 "Flushing old station entries");
795
796 if (hostapd_flush(hapd)) {
797 wpa_msg(hapd->msg_ctx, MSG_WARNING,
798 "Could not connect to kernel driver");
799 ret = -1;
800 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700801 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700802 if (hapd->conf && hapd->conf->broadcast_deauth) {
803 wpa_dbg(hapd->msg_ctx, MSG_DEBUG,
804 "Deauthenticate all stations");
805 os_memset(addr, 0xff, ETH_ALEN);
806 hostapd_drv_sta_deauth(hapd, addr, reason);
807 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700808 hostapd_free_stas(hapd);
809
810 return ret;
811}
812
813
Hai Shalom60840252021-02-19 19:02:11 -0800814void hostapd_bss_deinit_no_free(struct hostapd_data *hapd)
Dmitry Shmidt71757432014-06-02 13:50:35 -0700815{
816 hostapd_free_stas(hapd);
817 hostapd_flush_old_stations(hapd, WLAN_REASON_DEAUTH_LEAVING);
Hai Shalomfdcde762020-04-02 11:19:20 -0700818#ifdef CONFIG_WEP
Dmitry Shmidt71757432014-06-02 13:50:35 -0700819 hostapd_clear_wep(hapd);
Hai Shalomfdcde762020-04-02 11:19:20 -0700820#endif /* CONFIG_WEP */
Dmitry Shmidt71757432014-06-02 13:50:35 -0700821}
822
823
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700824/**
825 * hostapd_validate_bssid_configuration - Validate BSSID configuration
826 * @iface: Pointer to interface data
827 * Returns: 0 on success, -1 on failure
828 *
829 * This function is used to validate that the configured BSSIDs are valid.
830 */
831static int hostapd_validate_bssid_configuration(struct hostapd_iface *iface)
832{
833 u8 mask[ETH_ALEN] = { 0 };
834 struct hostapd_data *hapd = iface->bss[0];
835 unsigned int i = iface->conf->num_bss, bits = 0, j;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700836 int auto_addr = 0;
837
838 if (hostapd_drv_none(hapd))
839 return 0;
840
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -0800841 if (iface->conf->use_driver_iface_addr)
842 return 0;
843
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700844 /* Generate BSSID mask that is large enough to cover the BSSIDs. */
845
846 /* Determine the bits necessary to cover the number of BSSIDs. */
847 for (i--; i; i >>= 1)
848 bits++;
849
850 /* Determine the bits necessary to any configured BSSIDs,
851 if they are higher than the number of BSSIDs. */
852 for (j = 0; j < iface->conf->num_bss; j++) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800853 if (is_zero_ether_addr(iface->conf->bss[j]->bssid)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700854 if (j)
855 auto_addr++;
856 continue;
857 }
858
859 for (i = 0; i < ETH_ALEN; i++) {
860 mask[i] |=
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800861 iface->conf->bss[j]->bssid[i] ^
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700862 hapd->own_addr[i];
863 }
864 }
865
866 if (!auto_addr)
867 goto skip_mask_ext;
868
869 for (i = 0; i < ETH_ALEN && mask[i] == 0; i++)
870 ;
871 j = 0;
872 if (i < ETH_ALEN) {
873 j = (5 - i) * 8;
874
875 while (mask[i] != 0) {
876 mask[i] >>= 1;
877 j++;
878 }
879 }
880
881 if (bits < j)
882 bits = j;
883
884 if (bits > 40) {
885 wpa_printf(MSG_ERROR, "Too many bits in the BSSID mask (%u)",
886 bits);
887 return -1;
888 }
889
890 os_memset(mask, 0xff, ETH_ALEN);
891 j = bits / 8;
892 for (i = 5; i > 5 - j; i--)
893 mask[i] = 0;
894 j = bits % 8;
Hai Shalom021b0b52019-04-10 11:17:58 -0700895 while (j) {
896 j--;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700897 mask[i] <<= 1;
Hai Shalom021b0b52019-04-10 11:17:58 -0700898 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700899
900skip_mask_ext:
901 wpa_printf(MSG_DEBUG, "BSS count %lu, BSSID mask " MACSTR " (%d bits)",
902 (unsigned long) iface->conf->num_bss, MAC2STR(mask), bits);
903
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700904 if (!auto_addr)
905 return 0;
906
907 for (i = 0; i < ETH_ALEN; i++) {
908 if ((hapd->own_addr[i] & mask[i]) != hapd->own_addr[i]) {
909 wpa_printf(MSG_ERROR, "Invalid BSSID mask " MACSTR
910 " for start address " MACSTR ".",
911 MAC2STR(mask), MAC2STR(hapd->own_addr));
912 wpa_printf(MSG_ERROR, "Start address must be the "
913 "first address in the block (i.e., addr "
914 "AND mask == addr).");
915 return -1;
916 }
917 }
918
919 return 0;
920}
921
922
923static int mac_in_conf(struct hostapd_config *conf, const void *a)
924{
925 size_t i;
926
927 for (i = 0; i < conf->num_bss; i++) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800928 if (hostapd_mac_comp(conf->bss[i]->bssid, a) == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700929 return 1;
930 }
931 }
932
933 return 0;
934}
935
936
Dmitry Shmidt04949592012-07-19 12:16:46 -0700937#ifndef CONFIG_NO_RADIUS
938
939static int hostapd_das_nas_mismatch(struct hostapd_data *hapd,
940 struct radius_das_attrs *attr)
941{
Dmitry Shmidt13ca8d82014-02-20 10:18:40 -0800942 if (attr->nas_identifier &&
943 (!hapd->conf->nas_identifier ||
944 os_strlen(hapd->conf->nas_identifier) !=
945 attr->nas_identifier_len ||
946 os_memcmp(hapd->conf->nas_identifier, attr->nas_identifier,
947 attr->nas_identifier_len) != 0)) {
948 wpa_printf(MSG_DEBUG, "RADIUS DAS: NAS-Identifier mismatch");
949 return 1;
950 }
951
952 if (attr->nas_ip_addr &&
953 (hapd->conf->own_ip_addr.af != AF_INET ||
954 os_memcmp(&hapd->conf->own_ip_addr.u.v4, attr->nas_ip_addr, 4) !=
955 0)) {
956 wpa_printf(MSG_DEBUG, "RADIUS DAS: NAS-IP-Address mismatch");
957 return 1;
958 }
959
960#ifdef CONFIG_IPV6
961 if (attr->nas_ipv6_addr &&
962 (hapd->conf->own_ip_addr.af != AF_INET6 ||
963 os_memcmp(&hapd->conf->own_ip_addr.u.v6, attr->nas_ipv6_addr, 16)
964 != 0)) {
965 wpa_printf(MSG_DEBUG, "RADIUS DAS: NAS-IPv6-Address mismatch");
966 return 1;
967 }
968#endif /* CONFIG_IPV6 */
969
Dmitry Shmidt04949592012-07-19 12:16:46 -0700970 return 0;
971}
972
973
974static struct sta_info * hostapd_das_find_sta(struct hostapd_data *hapd,
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800975 struct radius_das_attrs *attr,
976 int *multi)
Dmitry Shmidt04949592012-07-19 12:16:46 -0700977{
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800978 struct sta_info *selected, *sta;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700979 char buf[128];
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800980 int num_attr = 0;
981 int count;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700982
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800983 *multi = 0;
984
985 for (sta = hapd->sta_list; sta; sta = sta->next)
986 sta->radius_das_match = 1;
987
988 if (attr->sta_addr) {
989 num_attr++;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700990 sta = ap_get_sta(hapd, attr->sta_addr);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800991 if (!sta) {
992 wpa_printf(MSG_DEBUG,
993 "RADIUS DAS: No Calling-Station-Id match");
994 return NULL;
995 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700996
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800997 selected = sta;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700998 for (sta = hapd->sta_list; sta; sta = sta->next) {
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800999 if (sta != selected)
1000 sta->radius_das_match = 0;
1001 }
1002 wpa_printf(MSG_DEBUG, "RADIUS DAS: Calling-Station-Id match");
1003 }
1004
1005 if (attr->acct_session_id) {
1006 num_attr++;
Dmitry Shmidtb97e4282016-02-08 10:16:07 -08001007 if (attr->acct_session_id_len != 16) {
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001008 wpa_printf(MSG_DEBUG,
1009 "RADIUS DAS: Acct-Session-Id cannot match");
1010 return NULL;
1011 }
1012 count = 0;
1013
1014 for (sta = hapd->sta_list; sta; sta = sta->next) {
1015 if (!sta->radius_das_match)
1016 continue;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001017 os_snprintf(buf, sizeof(buf), "%016llX",
1018 (unsigned long long) sta->acct_session_id);
Dmitry Shmidtb97e4282016-02-08 10:16:07 -08001019 if (os_memcmp(attr->acct_session_id, buf, 16) != 0)
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001020 sta->radius_das_match = 0;
1021 else
1022 count++;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001023 }
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001024
1025 if (count == 0) {
1026 wpa_printf(MSG_DEBUG,
1027 "RADIUS DAS: No matches remaining after Acct-Session-Id check");
1028 return NULL;
1029 }
1030 wpa_printf(MSG_DEBUG, "RADIUS DAS: Acct-Session-Id match");
Dmitry Shmidt04949592012-07-19 12:16:46 -07001031 }
1032
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001033 if (attr->acct_multi_session_id) {
1034 num_attr++;
Dmitry Shmidtb97e4282016-02-08 10:16:07 -08001035 if (attr->acct_multi_session_id_len != 16) {
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001036 wpa_printf(MSG_DEBUG,
1037 "RADIUS DAS: Acct-Multi-Session-Id cannot match");
1038 return NULL;
1039 }
1040 count = 0;
1041
1042 for (sta = hapd->sta_list; sta; sta = sta->next) {
1043 if (!sta->radius_das_match)
1044 continue;
1045 if (!sta->eapol_sm ||
Dmitry Shmidtb97e4282016-02-08 10:16:07 -08001046 !sta->eapol_sm->acct_multi_session_id) {
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001047 sta->radius_das_match = 0;
1048 continue;
1049 }
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001050 os_snprintf(buf, sizeof(buf), "%016llX",
1051 (unsigned long long)
Dmitry Shmidtb97e4282016-02-08 10:16:07 -08001052 sta->eapol_sm->acct_multi_session_id);
1053 if (os_memcmp(attr->acct_multi_session_id, buf, 16) !=
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001054 0)
1055 sta->radius_das_match = 0;
1056 else
1057 count++;
1058 }
1059
1060 if (count == 0) {
1061 wpa_printf(MSG_DEBUG,
1062 "RADIUS DAS: No matches remaining after Acct-Multi-Session-Id check");
1063 return NULL;
1064 }
1065 wpa_printf(MSG_DEBUG,
1066 "RADIUS DAS: Acct-Multi-Session-Id match");
1067 }
1068
1069 if (attr->cui) {
1070 num_attr++;
1071 count = 0;
1072
Dmitry Shmidt04949592012-07-19 12:16:46 -07001073 for (sta = hapd->sta_list; sta; sta = sta->next) {
1074 struct wpabuf *cui;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001075
1076 if (!sta->radius_das_match)
1077 continue;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001078 cui = ieee802_1x_get_radius_cui(sta->eapol_sm);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001079 if (!cui || wpabuf_len(cui) != attr->cui_len ||
Dmitry Shmidt04949592012-07-19 12:16:46 -07001080 os_memcmp(wpabuf_head(cui), attr->cui,
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001081 attr->cui_len) != 0)
1082 sta->radius_das_match = 0;
1083 else
1084 count++;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001085 }
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001086
1087 if (count == 0) {
1088 wpa_printf(MSG_DEBUG,
1089 "RADIUS DAS: No matches remaining after Chargeable-User-Identity check");
1090 return NULL;
1091 }
1092 wpa_printf(MSG_DEBUG,
1093 "RADIUS DAS: Chargeable-User-Identity match");
Dmitry Shmidt04949592012-07-19 12:16:46 -07001094 }
1095
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001096 if (attr->user_name) {
1097 num_attr++;
1098 count = 0;
1099
Dmitry Shmidt04949592012-07-19 12:16:46 -07001100 for (sta = hapd->sta_list; sta; sta = sta->next) {
1101 u8 *identity;
1102 size_t identity_len;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001103
1104 if (!sta->radius_das_match)
1105 continue;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001106 identity = ieee802_1x_get_identity(sta->eapol_sm,
1107 &identity_len);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001108 if (!identity ||
1109 identity_len != attr->user_name_len ||
Dmitry Shmidt04949592012-07-19 12:16:46 -07001110 os_memcmp(identity, attr->user_name, identity_len)
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001111 != 0)
1112 sta->radius_das_match = 0;
1113 else
1114 count++;
1115 }
1116
1117 if (count == 0) {
1118 wpa_printf(MSG_DEBUG,
1119 "RADIUS DAS: No matches remaining after User-Name check");
1120 return NULL;
1121 }
1122 wpa_printf(MSG_DEBUG,
1123 "RADIUS DAS: User-Name match");
1124 }
1125
1126 if (num_attr == 0) {
1127 /*
1128 * In theory, we could match all current associations, but it
1129 * seems safer to just reject requests that do not include any
1130 * session identification attributes.
1131 */
1132 wpa_printf(MSG_DEBUG,
1133 "RADIUS DAS: No session identification attributes included");
1134 return NULL;
1135 }
1136
1137 selected = NULL;
1138 for (sta = hapd->sta_list; sta; sta = sta->next) {
1139 if (sta->radius_das_match) {
1140 if (selected) {
1141 *multi = 1;
1142 return NULL;
1143 }
1144 selected = sta;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001145 }
1146 }
1147
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001148 return selected;
1149}
1150
1151
1152static int hostapd_das_disconnect_pmksa(struct hostapd_data *hapd,
1153 struct radius_das_attrs *attr)
1154{
1155 if (!hapd->wpa_auth)
1156 return -1;
1157 return wpa_auth_radius_das_disconnect_pmksa(hapd->wpa_auth, attr);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001158}
1159
1160
1161static enum radius_das_res
1162hostapd_das_disconnect(void *ctx, struct radius_das_attrs *attr)
1163{
1164 struct hostapd_data *hapd = ctx;
1165 struct sta_info *sta;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001166 int multi;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001167
1168 if (hostapd_das_nas_mismatch(hapd, attr))
1169 return RADIUS_DAS_NAS_MISMATCH;
1170
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001171 sta = hostapd_das_find_sta(hapd, attr, &multi);
1172 if (sta == NULL) {
1173 if (multi) {
1174 wpa_printf(MSG_DEBUG,
1175 "RADIUS DAS: Multiple sessions match - not supported");
1176 return RADIUS_DAS_MULTI_SESSION_MATCH;
1177 }
1178 if (hostapd_das_disconnect_pmksa(hapd, attr) == 0) {
1179 wpa_printf(MSG_DEBUG,
1180 "RADIUS DAS: PMKSA cache entry matched");
1181 return RADIUS_DAS_SUCCESS;
1182 }
1183 wpa_printf(MSG_DEBUG, "RADIUS DAS: No matching session found");
Dmitry Shmidt04949592012-07-19 12:16:46 -07001184 return RADIUS_DAS_SESSION_NOT_FOUND;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001185 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001186
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001187 wpa_printf(MSG_DEBUG, "RADIUS DAS: Found a matching session " MACSTR
1188 " - disconnecting", MAC2STR(sta->addr));
Dmitry Shmidt13ca8d82014-02-20 10:18:40 -08001189 wpa_auth_pmksa_remove(hapd->wpa_auth, sta->addr);
1190
Dmitry Shmidt04949592012-07-19 12:16:46 -07001191 hostapd_drv_sta_deauth(hapd, sta->addr,
1192 WLAN_REASON_PREV_AUTH_NOT_VALID);
1193 ap_sta_deauthenticate(hapd, sta, WLAN_REASON_PREV_AUTH_NOT_VALID);
1194
1195 return RADIUS_DAS_SUCCESS;
1196}
1197
Roshan Pius3a1667e2018-07-03 15:17:14 -07001198
1199#ifdef CONFIG_HS20
1200static enum radius_das_res
1201hostapd_das_coa(void *ctx, struct radius_das_attrs *attr)
1202{
1203 struct hostapd_data *hapd = ctx;
1204 struct sta_info *sta;
1205 int multi;
1206
1207 if (hostapd_das_nas_mismatch(hapd, attr))
1208 return RADIUS_DAS_NAS_MISMATCH;
1209
1210 sta = hostapd_das_find_sta(hapd, attr, &multi);
1211 if (!sta) {
1212 if (multi) {
1213 wpa_printf(MSG_DEBUG,
1214 "RADIUS DAS: Multiple sessions match - not supported");
1215 return RADIUS_DAS_MULTI_SESSION_MATCH;
1216 }
1217 wpa_printf(MSG_DEBUG, "RADIUS DAS: No matching session found");
1218 return RADIUS_DAS_SESSION_NOT_FOUND;
1219 }
1220
1221 wpa_printf(MSG_DEBUG, "RADIUS DAS: Found a matching session " MACSTR
1222 " - CoA", MAC2STR(sta->addr));
1223
1224 if (attr->hs20_t_c_filtering) {
1225 if (attr->hs20_t_c_filtering[0] & BIT(0)) {
1226 wpa_printf(MSG_DEBUG,
1227 "HS 2.0: Unexpected Terms and Conditions filtering required in CoA-Request");
1228 return RADIUS_DAS_COA_FAILED;
1229 }
1230
1231 hs20_t_c_filtering(hapd, sta, 0);
1232 }
1233
1234 return RADIUS_DAS_SUCCESS;
1235}
1236#else /* CONFIG_HS20 */
1237#define hostapd_das_coa NULL
1238#endif /* CONFIG_HS20 */
1239
Hai Shalomc3565922019-10-28 11:58:20 -07001240
1241#ifdef CONFIG_SQLITE
1242
1243static int db_table_exists(sqlite3 *db, const char *name)
1244{
1245 char cmd[128];
1246
1247 os_snprintf(cmd, sizeof(cmd), "SELECT 1 FROM %s;", name);
1248 return sqlite3_exec(db, cmd, NULL, NULL, NULL) == SQLITE_OK;
1249}
1250
1251
1252static int db_table_create_radius_attributes(sqlite3 *db)
1253{
1254 char *err = NULL;
1255 const char *sql =
1256 "CREATE TABLE radius_attributes("
1257 " id INTEGER PRIMARY KEY,"
1258 " sta TEXT,"
1259 " reqtype TEXT,"
1260 " attr TEXT"
1261 ");"
1262 "CREATE INDEX idx_sta_reqtype ON radius_attributes(sta,reqtype);";
1263
1264 wpa_printf(MSG_DEBUG,
1265 "Adding database table for RADIUS attribute information");
1266 if (sqlite3_exec(db, sql, NULL, NULL, &err) != SQLITE_OK) {
1267 wpa_printf(MSG_ERROR, "SQLite error: %s", err);
1268 sqlite3_free(err);
1269 return -1;
1270 }
1271
1272 return 0;
1273}
1274
1275#endif /* CONFIG_SQLITE */
1276
Dmitry Shmidt04949592012-07-19 12:16:46 -07001277#endif /* CONFIG_NO_RADIUS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001278
1279
Sunil Ravi77d572f2023-01-17 23:58:31 +00001280static int hostapd_start_beacon(struct hostapd_data *hapd,
1281 bool flush_old_stations)
1282{
1283 struct hostapd_bss_config *conf = hapd->conf;
1284
1285 if (!conf->start_disabled && ieee802_11_set_beacon(hapd) < 0)
1286 return -1;
1287
1288 if (flush_old_stations && !conf->start_disabled &&
1289 conf->broadcast_deauth) {
1290 u8 addr[ETH_ALEN];
1291
1292 /* Should any previously associated STA not have noticed that
1293 * the AP had stopped and restarted, send one more
1294 * deauthentication notification now that the AP is ready to
1295 * operate. */
1296 wpa_dbg(hapd->msg_ctx, MSG_DEBUG,
1297 "Deauthenticate all stations at BSS start");
1298 os_memset(addr, 0xff, ETH_ALEN);
1299 hostapd_drv_sta_deauth(hapd, addr,
1300 WLAN_REASON_PREV_AUTH_NOT_VALID);
1301 }
1302
1303 if (hapd->driver && hapd->driver->set_operstate)
1304 hapd->driver->set_operstate(hapd->drv_priv, 1);
1305
1306 return 0;
1307}
1308
1309
Sunil Ravi7f769292024-07-23 22:21:32 +00001310#ifndef CONFIG_NO_RADIUS
1311static int hostapd_bss_radius_init(struct hostapd_data *hapd)
1312{
1313 struct hostapd_bss_config *conf;
1314
1315 if (!hapd)
1316 return -1;
1317
1318 conf = hapd->conf;
1319
1320 if (hapd->radius) {
1321 wpa_printf(MSG_DEBUG,
1322 "Skipping RADIUS client init (already done)");
1323 return 0;
1324 }
1325
1326 hapd->radius = radius_client_init(hapd, conf->radius);
1327 if (!hapd->radius) {
1328 wpa_printf(MSG_ERROR,
1329 "RADIUS client initialization failed.");
1330 return -1;
1331 }
1332
1333 if (conf->radius_das_port) {
1334 struct radius_das_conf das_conf;
1335
1336 os_memset(&das_conf, 0, sizeof(das_conf));
1337 das_conf.port = conf->radius_das_port;
1338 das_conf.shared_secret = conf->radius_das_shared_secret;
1339 das_conf.shared_secret_len =
1340 conf->radius_das_shared_secret_len;
1341 das_conf.client_addr = &conf->radius_das_client_addr;
1342 das_conf.time_window = conf->radius_das_time_window;
1343 das_conf.require_event_timestamp =
1344 conf->radius_das_require_event_timestamp;
1345 das_conf.require_message_authenticator =
1346 conf->radius_das_require_message_authenticator;
1347 das_conf.ctx = hapd;
1348 das_conf.disconnect = hostapd_das_disconnect;
1349 das_conf.coa = hostapd_das_coa;
1350 hapd->radius_das = radius_das_init(&das_conf);
1351 if (!hapd->radius_das) {
1352 wpa_printf(MSG_ERROR,
1353 "RADIUS DAS initialization failed.");
1354 return -1;
1355 }
1356 }
1357
1358 return 0;
1359}
1360#endif /* CONFIG_NO_RADIUS */
1361
1362
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001363/**
1364 * hostapd_setup_bss - Per-BSS setup (initialization)
1365 * @hapd: Pointer to BSS data
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001366 * @first: Whether this BSS is the first BSS of an interface; -1 = not first,
1367 * but interface may exist
Sunil Ravi77d572f2023-01-17 23:58:31 +00001368 * @start_beacon: Whether Beacon frame template should be configured and
1369 * transmission of Beaconf rames started at this time. This is used when
1370 * MBSSID element is enabled where the information regarding all BSSes
1371 * should be retrieved before configuring the Beacon frame template. The
1372 * calling functions are responsible for configuring the Beacon frame
1373 * explicitly if this is set to false.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001374 *
1375 * This function is used to initialize all per-BSS data structures and
1376 * resources. This gets called in a loop for each BSS when an interface is
1377 * initialized. Most of the modules that are initialized here will be
1378 * deinitialized in hostapd_cleanup().
1379 */
Sunil Ravi77d572f2023-01-17 23:58:31 +00001380static int hostapd_setup_bss(struct hostapd_data *hapd, int first,
1381 bool start_beacon)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001382{
1383 struct hostapd_bss_config *conf = hapd->conf;
Dmitry Shmidt9d9e6022015-04-23 10:34:55 -07001384 u8 ssid[SSID_MAX_LEN + 1];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001385 int ssid_len, set_ssid;
1386 char force_ifname[IFNAMSIZ];
1387 u8 if_addr[ETH_ALEN];
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001388 int flush_old_stations = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001389
Sunil Ravi99c035e2024-07-12 01:42:03 +00001390 if (!hostapd_mld_is_first_bss(hapd))
Sunil Ravi2a14cf12023-11-21 00:54:38 +00001391 wpa_printf(MSG_DEBUG,
1392 "MLD: %s: Setting non-first BSS", __func__);
1393
Dmitry Shmidt54605472013-11-08 11:10:19 -08001394 wpa_printf(MSG_DEBUG, "%s(hapd=%p (%s), first=%d)",
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001395 __func__, hapd, conf->iface, first);
Dmitry Shmidt54605472013-11-08 11:10:19 -08001396
Dmitry Shmidt50b691d2014-05-21 14:01:45 -07001397#ifdef EAP_SERVER_TNC
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001398 if (conf->tnc && tncs_global_init() < 0) {
Dmitry Shmidt50b691d2014-05-21 14:01:45 -07001399 wpa_printf(MSG_ERROR, "Failed to initialize TNCS");
1400 return -1;
1401 }
1402#endif /* EAP_SERVER_TNC */
1403
Dmitry Shmidt54605472013-11-08 11:10:19 -08001404 if (hapd->started) {
1405 wpa_printf(MSG_ERROR, "%s: Interface %s was already started",
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001406 __func__, conf->iface);
Dmitry Shmidt54605472013-11-08 11:10:19 -08001407 return -1;
1408 }
1409 hapd->started = 1;
1410
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001411 if (!first || first == -1) {
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08001412 u8 *addr = hapd->own_addr;
1413
1414 if (!is_zero_ether_addr(conf->bssid)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001415 /* Allocate the configured BSSID. */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001416 os_memcpy(hapd->own_addr, conf->bssid, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001417
1418 if (hostapd_mac_comp(hapd->own_addr,
1419 hapd->iface->bss[0]->own_addr) ==
1420 0) {
1421 wpa_printf(MSG_ERROR, "BSS '%s' may not have "
1422 "BSSID set to the MAC address of "
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001423 "the radio", conf->iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001424 return -1;
1425 }
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08001426 } else if (hapd->iconf->use_driver_iface_addr) {
1427 addr = NULL;
1428 } else {
1429 /* Allocate the next available BSSID. */
1430 do {
1431 inc_byte_array(hapd->own_addr, ETH_ALEN);
1432 } while (mac_in_conf(hapd->iconf, hapd->own_addr));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001433 }
1434
Sunil Ravi7f769292024-07-23 22:21:32 +00001435#ifdef CONFIG_IEEE80211BE
1436 if (conf->mld_ap) {
1437 struct hostapd_data *h_hapd;
1438
1439 h_hapd = hostapd_mld_get_first_bss(hapd);
1440 if (h_hapd) {
1441 hapd->drv_priv = h_hapd->drv_priv;
1442 hapd->interface_added = h_hapd->interface_added;
1443 hostapd_mld_add_link(hapd);
1444 wpa_printf(MSG_DEBUG,
1445 "Setup of non first link (%d) BSS of MLD %s",
1446 hapd->mld_link_id, hapd->conf->iface);
1447 goto setup_mld;
1448 }
1449 }
1450#endif /* CONFIG_IEEE80211BE */
1451
Dmitry Shmidt54605472013-11-08 11:10:19 -08001452 hapd->interface_added = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001453 if (hostapd_if_add(hapd->iface->bss[0], WPA_IF_AP_BSS,
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08001454 conf->iface, addr, hapd,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001455 &hapd->drv_priv, force_ifname, if_addr,
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001456 conf->bridge[0] ? conf->bridge : NULL,
1457 first == -1)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001458 wpa_printf(MSG_ERROR, "Failed to add BSS (BSSID="
1459 MACSTR ")", MAC2STR(hapd->own_addr));
Dmitry Shmidt3cf6f792013-12-18 13:12:19 -08001460 hapd->interface_added = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001461 return -1;
1462 }
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08001463
1464 if (!addr)
1465 os_memcpy(hapd->own_addr, if_addr, ETH_ALEN);
Sunil Ravi7f769292024-07-23 22:21:32 +00001466
1467#ifdef CONFIG_IEEE80211BE
1468 if (hapd->conf->mld_ap) {
1469 wpa_printf(MSG_DEBUG,
1470 "Setup of first link (%d) BSS of MLD %s",
1471 hapd->mld_link_id, hapd->conf->iface);
1472 os_memcpy(hapd->mld->mld_addr, hapd->own_addr,
1473 ETH_ALEN);
1474 hostapd_mld_add_link(hapd);
1475 }
1476#endif /* CONFIG_IEEE80211BE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001477 }
1478
Sunil Ravi7f769292024-07-23 22:21:32 +00001479#ifdef CONFIG_IEEE80211BE
1480setup_mld:
1481 if (hapd->conf->mld_ap && !first) {
1482 wpa_printf(MSG_DEBUG,
1483 "MLD: Set link_id=%u, mld_addr=" MACSTR
1484 ", own_addr=" MACSTR,
1485 hapd->mld_link_id, MAC2STR(hapd->mld->mld_addr),
1486 MAC2STR(hapd->own_addr));
1487
1488 if (hostapd_drv_link_add(hapd, hapd->mld_link_id,
1489 hapd->own_addr))
1490 return -1;
1491 }
1492#endif /* CONFIG_IEEE80211BE */
1493
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001494 if (conf->wmm_enabled < 0)
Hai Shaloma20dcd72022-02-04 13:43:00 -08001495 conf->wmm_enabled = hapd->iconf->ieee80211n |
1496 hapd->iconf->ieee80211ax;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001497
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001498#ifdef CONFIG_IEEE80211R_AP
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001499 if (is_zero_ether_addr(conf->r1_key_holder))
1500 os_memcpy(conf->r1_key_holder, hapd->own_addr, ETH_ALEN);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001501#endif /* CONFIG_IEEE80211R_AP */
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001502
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001503#ifdef CONFIG_MESH
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001504 if ((hapd->conf->mesh & MESH_ENABLED) && hapd->iface->mconf == NULL)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001505 flush_old_stations = 0;
1506#endif /* CONFIG_MESH */
1507
1508 if (flush_old_stations)
Hai Shalom4fbc08f2020-05-18 12:37:00 -07001509 hostapd_flush(hapd);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001510 hostapd_set_privacy(hapd, 0);
1511
Hai Shalomfdcde762020-04-02 11:19:20 -07001512#ifdef CONFIG_WEP
Hai Shalom4fbc08f2020-05-18 12:37:00 -07001513 if (!hostapd_drv_nl80211(hapd))
1514 hostapd_broadcast_wep_clear(hapd);
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001515 if (hostapd_setup_encryption(conf->iface, hapd))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001516 return -1;
Hai Shalomfdcde762020-04-02 11:19:20 -07001517#endif /* CONFIG_WEP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001518
1519 /*
1520 * Fetch the SSID from the system and use it or,
1521 * if one was specified in the config file, verify they
1522 * match.
1523 */
1524 ssid_len = hostapd_get_ssid(hapd, ssid, sizeof(ssid));
1525 if (ssid_len < 0) {
1526 wpa_printf(MSG_ERROR, "Could not read SSID from system");
1527 return -1;
1528 }
1529 if (conf->ssid.ssid_set) {
1530 /*
1531 * If SSID is specified in the config file and it differs
1532 * from what is being used then force installation of the
1533 * new SSID.
1534 */
1535 set_ssid = (conf->ssid.ssid_len != (size_t) ssid_len ||
1536 os_memcmp(conf->ssid.ssid, ssid, ssid_len) != 0);
1537 } else {
1538 /*
1539 * No SSID in the config file; just use the one we got
1540 * from the system.
1541 */
1542 set_ssid = 0;
1543 conf->ssid.ssid_len = ssid_len;
1544 os_memcpy(conf->ssid.ssid, ssid, conf->ssid.ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001545 }
1546
Hai Shalomfdcde762020-04-02 11:19:20 -07001547 /*
1548 * Short SSID calculation is identical to FCS and it is defined in
1549 * IEEE P802.11-REVmd/D3.0, 9.4.2.170.3 (Calculating the Short-SSID).
1550 */
Sunil Ravi89eba102022-09-13 21:04:37 -07001551 conf->ssid.short_ssid = ieee80211_crc32(conf->ssid.ssid,
1552 conf->ssid.ssid_len);
Hai Shalomfdcde762020-04-02 11:19:20 -07001553
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001554 if (!hostapd_drv_none(hapd)) {
Hai Shalomfdcde762020-04-02 11:19:20 -07001555 wpa_printf(MSG_DEBUG, "Using interface %s with hwaddr " MACSTR
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001556 " and ssid \"%s\"",
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001557 conf->iface, MAC2STR(hapd->own_addr),
1558 wpa_ssid_txt(conf->ssid.ssid, conf->ssid.ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001559 }
1560
1561 if (hostapd_setup_wpa_psk(conf)) {
1562 wpa_printf(MSG_ERROR, "WPA-PSK setup failed.");
1563 return -1;
1564 }
1565
1566 /* Set SSID for the kernel driver (to be used in beacon and probe
1567 * response frames) */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001568 if (set_ssid && hostapd_set_ssid(hapd, conf->ssid.ssid,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001569 conf->ssid.ssid_len)) {
1570 wpa_printf(MSG_ERROR, "Could not set SSID for kernel driver");
1571 return -1;
1572 }
1573
Dmitry Shmidt818ea482014-03-10 13:15:21 -07001574 if (wpa_debug_level <= MSG_MSGDUMP)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001575 conf->radius->msg_dumps = 1;
1576#ifndef CONFIG_NO_RADIUS
Hai Shalomc3565922019-10-28 11:58:20 -07001577
1578#ifdef CONFIG_SQLITE
1579 if (conf->radius_req_attr_sqlite) {
1580 if (sqlite3_open(conf->radius_req_attr_sqlite,
1581 &hapd->rad_attr_db)) {
1582 wpa_printf(MSG_ERROR, "Could not open SQLite file '%s'",
1583 conf->radius_req_attr_sqlite);
1584 return -1;
1585 }
1586
1587 wpa_printf(MSG_DEBUG, "Opening RADIUS attribute database: %s",
1588 conf->radius_req_attr_sqlite);
1589 if (!db_table_exists(hapd->rad_attr_db, "radius_attributes") &&
1590 db_table_create_radius_attributes(hapd->rad_attr_db) < 0)
1591 return -1;
1592 }
1593#endif /* CONFIG_SQLITE */
1594
Sunil Ravi99c035e2024-07-12 01:42:03 +00001595 if (hostapd_mld_is_first_bss(hapd)) {
Sunil Ravi7f769292024-07-23 22:21:32 +00001596 if (hostapd_bss_radius_init(hapd))
Dmitry Shmidt04949592012-07-19 12:16:46 -07001597 return -1;
Sunil Ravi2a14cf12023-11-21 00:54:38 +00001598 } else {
Sunil Ravi99c035e2024-07-12 01:42:03 +00001599#ifdef CONFIG_IEEE80211BE
1600 struct hostapd_data *f_bss;
1601
Sunil Ravi99c035e2024-07-12 01:42:03 +00001602 f_bss = hostapd_mld_get_first_bss(hapd);
1603 if (!f_bss)
1604 return -1;
Sunil Ravi7f769292024-07-23 22:21:32 +00001605
1606 if (!f_bss->radius) {
1607 wpa_printf(MSG_DEBUG,
1608 "MLD: First BSS RADIUS client does not exist. Init on its behalf");
1609
1610 if (hostapd_bss_radius_init(f_bss))
1611 return -1;
1612 }
1613
1614 wpa_printf(MSG_DEBUG,
1615 "MLD: Using RADIUS client of the first BSS");
Sunil Ravi99c035e2024-07-12 01:42:03 +00001616 hapd->radius = f_bss->radius;
1617 hapd->radius_das = f_bss->radius_das;
1618#endif /* CONFIG_IEEE80211BE */
Dmitry Shmidt04949592012-07-19 12:16:46 -07001619 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001620#endif /* CONFIG_NO_RADIUS */
1621
1622 if (hostapd_acl_init(hapd)) {
1623 wpa_printf(MSG_ERROR, "ACL initialization failed.");
1624 return -1;
1625 }
1626 if (hostapd_init_wps(hapd, conf))
1627 return -1;
1628
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001629#ifdef CONFIG_DPP
1630 hapd->gas = gas_query_ap_init(hapd, hapd->msg_ctx);
1631 if (!hapd->gas)
1632 return -1;
1633 if (hostapd_dpp_init(hapd))
1634 return -1;
1635#endif /* CONFIG_DPP */
1636
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001637#ifdef CONFIG_NAN_USD
1638 if (hostapd_nan_usd_init(hapd) < 0)
1639 return -1;
1640#endif /* CONFIG_NAN_USD */
1641
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001642 if (authsrv_init(hapd) < 0)
1643 return -1;
1644
1645 if (ieee802_1x_init(hapd)) {
1646 wpa_printf(MSG_ERROR, "IEEE 802.1X initialization failed.");
1647 return -1;
1648 }
1649
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001650 if ((conf->wpa || conf->osen) && hostapd_setup_wpa(hapd))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001651 return -1;
1652
1653 if (accounting_init(hapd)) {
1654 wpa_printf(MSG_ERROR, "Accounting initialization failed.");
1655 return -1;
1656 }
1657
Dmitry Shmidt04949592012-07-19 12:16:46 -07001658#ifdef CONFIG_INTERWORKING
1659 if (gas_serv_init(hapd)) {
1660 wpa_printf(MSG_ERROR, "GAS server initialization failed");
1661 return -1;
1662 }
Sunil Ravi99c035e2024-07-12 01:42:03 +00001663#endif /* CONFIG_INTERWORKING */
Dmitry Shmidt04949592012-07-19 12:16:46 -07001664
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001665 if (conf->qos_map_set_len &&
1666 hostapd_drv_set_qos_map(hapd, conf->qos_map_set,
1667 conf->qos_map_set_len)) {
1668 wpa_printf(MSG_ERROR, "Failed to initialize QoS Map");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001669 return -1;
1670 }
1671
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001672 if (conf->bss_load_update_period && bss_load_update_init(hapd)) {
1673 wpa_printf(MSG_ERROR, "BSS Load initialization failed");
1674 return -1;
1675 }
1676
Sunil Ravi036cec52023-03-29 11:35:17 -07001677 if (conf->bridge[0]) {
1678 /* Set explicitly configured bridge parameters that might have
1679 * been lost if the interface has been removed out of the
1680 * bridge. */
1681
1682 /* multicast to unicast on bridge ports */
1683 if (conf->bridge_multicast_to_unicast)
1684 hostapd_drv_br_port_set_attr(
1685 hapd, DRV_BR_PORT_ATTR_MCAST2UCAST, 1);
1686
1687 /* hairpin mode */
1688 if (conf->bridge_hairpin)
1689 hostapd_drv_br_port_set_attr(
1690 hapd, DRV_BR_PORT_ATTR_HAIRPIN_MODE, 1);
1691 }
1692
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001693 if (conf->proxy_arp) {
1694 if (x_snoop_init(hapd)) {
1695 wpa_printf(MSG_ERROR,
1696 "Generic snooping infrastructure initialization failed");
1697 return -1;
1698 }
1699
1700 if (dhcp_snoop_init(hapd)) {
1701 wpa_printf(MSG_ERROR,
1702 "DHCP snooping initialization failed");
1703 return -1;
1704 }
1705
1706 if (ndisc_snoop_init(hapd)) {
1707 wpa_printf(MSG_ERROR,
1708 "Neighbor Discovery snooping initialization failed");
1709 return -1;
1710 }
1711 }
1712
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001713 if (!hostapd_drv_none(hapd) && vlan_init(hapd)) {
1714 wpa_printf(MSG_ERROR, "VLAN initialization failed.");
1715 return -1;
1716 }
1717
Sunil Ravi640215c2023-06-28 23:08:09 +00001718 if (start_beacon && hostapd_start_beacon(hapd, flush_old_stations) < 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001719 return -1;
1720
Sunil Ravi640215c2023-06-28 23:08:09 +00001721 if (hapd->wpa_auth && wpa_init_keys(hapd->wpa_auth) < 0)
1722 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001723
1724 return 0;
1725}
1726
1727
1728static void hostapd_tx_queue_params(struct hostapd_iface *iface)
1729{
1730 struct hostapd_data *hapd = iface->bss[0];
1731 int i;
1732 struct hostapd_tx_queue_params *p;
1733
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001734#ifdef CONFIG_MESH
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001735 if ((hapd->conf->mesh & MESH_ENABLED) && iface->mconf == NULL)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001736 return;
1737#endif /* CONFIG_MESH */
1738
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001739 for (i = 0; i < NUM_TX_QUEUES; i++) {
1740 p = &iface->conf->tx_queue[i];
1741
1742 if (hostapd_set_tx_queue_params(hapd, i, p->aifs, p->cwmin,
1743 p->cwmax, p->burst)) {
1744 wpa_printf(MSG_DEBUG, "Failed to set TX queue "
1745 "parameters for queue %d.", i);
1746 /* Continue anyway */
1747 }
1748 }
1749}
1750
1751
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001752static int hostapd_set_acl_list(struct hostapd_data *hapd,
1753 struct mac_acl_entry *mac_acl,
1754 int n_entries, u8 accept_acl)
1755{
1756 struct hostapd_acl_params *acl_params;
1757 int i, err;
1758
1759 acl_params = os_zalloc(sizeof(*acl_params) +
1760 (n_entries * sizeof(acl_params->mac_acl[0])));
1761 if (!acl_params)
1762 return -ENOMEM;
1763
1764 for (i = 0; i < n_entries; i++)
1765 os_memcpy(acl_params->mac_acl[i].addr, mac_acl[i].addr,
1766 ETH_ALEN);
1767
1768 acl_params->acl_policy = accept_acl;
1769 acl_params->num_mac_acl = n_entries;
1770
1771 err = hostapd_drv_set_acl(hapd, acl_params);
1772
1773 os_free(acl_params);
1774
1775 return err;
1776}
1777
1778
Sunil Ravia04bd252022-05-02 22:54:18 -07001779int hostapd_set_acl(struct hostapd_data *hapd)
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001780{
1781 struct hostapd_config *conf = hapd->iconf;
Sunil Ravia04bd252022-05-02 22:54:18 -07001782 int err = 0;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001783 u8 accept_acl;
1784
1785 if (hapd->iface->drv_max_acl_mac_addrs == 0)
Sunil Ravia04bd252022-05-02 22:54:18 -07001786 return 0;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001787
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001788 if (conf->bss[0]->macaddr_acl == DENY_UNLESS_ACCEPTED) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001789 accept_acl = 1;
1790 err = hostapd_set_acl_list(hapd, conf->bss[0]->accept_mac,
1791 conf->bss[0]->num_accept_mac,
1792 accept_acl);
1793 if (err) {
1794 wpa_printf(MSG_DEBUG, "Failed to set accept acl");
Sunil Ravia04bd252022-05-02 22:54:18 -07001795 return -1;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001796 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001797 } else if (conf->bss[0]->macaddr_acl == ACCEPT_UNLESS_DENIED) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001798 accept_acl = 0;
1799 err = hostapd_set_acl_list(hapd, conf->bss[0]->deny_mac,
1800 conf->bss[0]->num_deny_mac,
1801 accept_acl);
1802 if (err) {
1803 wpa_printf(MSG_DEBUG, "Failed to set deny acl");
Sunil Ravia04bd252022-05-02 22:54:18 -07001804 return -1;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001805 }
1806 }
Sunil Ravia04bd252022-05-02 22:54:18 -07001807 return err;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001808}
1809
1810
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001811static int start_ctrl_iface_bss(struct hostapd_data *hapd)
1812{
1813 if (!hapd->iface->interfaces ||
1814 !hapd->iface->interfaces->ctrl_iface_init)
1815 return 0;
1816
1817 if (hapd->iface->interfaces->ctrl_iface_init(hapd)) {
1818 wpa_printf(MSG_ERROR,
1819 "Failed to setup control interface for %s",
1820 hapd->conf->iface);
1821 return -1;
1822 }
1823
1824 return 0;
1825}
1826
1827
1828static int start_ctrl_iface(struct hostapd_iface *iface)
1829{
1830 size_t i;
1831
1832 if (!iface->interfaces || !iface->interfaces->ctrl_iface_init)
1833 return 0;
1834
1835 for (i = 0; i < iface->num_bss; i++) {
1836 struct hostapd_data *hapd = iface->bss[i];
1837 if (iface->interfaces->ctrl_iface_init(hapd)) {
1838 wpa_printf(MSG_ERROR,
1839 "Failed to setup control interface for %s",
1840 hapd->conf->iface);
1841 return -1;
1842 }
1843 }
1844
1845 return 0;
1846}
1847
1848
Sunil Ravi2a14cf12023-11-21 00:54:38 +00001849/* When NO_IR flag is set and AP is stopped, clean up BSS parameters without
1850 * deinitializing the driver and the control interfaces. A subsequent
1851 * REG_CHANGE event can bring the AP back up.
1852 */
1853static void hostapd_no_ir_cleanup(struct hostapd_data *bss)
1854{
1855 hostapd_bss_deinit_no_free(bss);
Sunil Ravi99c035e2024-07-12 01:42:03 +00001856 hostapd_bss_link_deinit(bss);
Sunil Ravi2a14cf12023-11-21 00:54:38 +00001857 hostapd_free_hapd_data(bss);
1858 hostapd_cleanup_iface_partial(bss->iface);
1859}
1860
1861
1862static int hostapd_no_ir_channel_list_updated(struct hostapd_iface *iface,
1863 void *ctx)
1864{
1865 bool all_no_ir, is_6ghz;
1866 int i, j;
1867 struct hostapd_hw_modes *mode = NULL;
1868
1869 if (hostapd_get_hw_features(iface))
1870 return 0;
1871
1872 all_no_ir = true;
1873 is_6ghz = false;
1874
1875 for (i = 0; i < iface->num_hw_features; i++) {
1876 mode = &iface->hw_features[i];
1877
1878 if (mode->mode == iface->conf->hw_mode) {
1879 if (iface->freq > 0 &&
1880 !hw_mode_get_channel(mode, iface->freq, NULL)) {
1881 mode = NULL;
1882 continue;
1883 }
1884
1885 for (j = 0; j < mode->num_channels; j++) {
1886 if (!(mode->channels[j].flag &
1887 HOSTAPD_CHAN_NO_IR))
1888 all_no_ir = false;
1889
1890 if (is_6ghz_freq(mode->channels[j].freq))
1891 is_6ghz = true;
1892 }
1893 break;
1894 }
1895 }
1896
1897 if (!mode || !is_6ghz)
1898 return 0;
1899 iface->current_mode = mode;
1900
1901 if (iface->state == HAPD_IFACE_ENABLED) {
1902 if (!all_no_ir) {
1903 struct hostapd_channel_data *chan;
1904
1905 chan = hw_get_channel_freq(iface->current_mode->mode,
1906 iface->freq, NULL,
1907 iface->hw_features,
1908 iface->num_hw_features);
1909
1910 if (!chan) {
1911 wpa_printf(MSG_ERROR,
1912 "NO_IR: Could not derive chan from freq");
1913 return 0;
1914 }
1915
1916 if (!(chan->flag & HOSTAPD_CHAN_NO_IR))
1917 return 0;
1918 wpa_printf(MSG_DEBUG,
1919 "NO_IR: The current channel has NO_IR flag now, stop AP.");
1920 } else {
1921 wpa_printf(MSG_DEBUG,
1922 "NO_IR: All chan in new chanlist are NO_IR, stop AP.");
1923 }
1924
1925 hostapd_set_state(iface, HAPD_IFACE_NO_IR);
1926 iface->is_no_ir = true;
1927 hostapd_drv_stop_ap(iface->bss[0]);
1928 hostapd_no_ir_cleanup(iface->bss[0]);
1929 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_NO_IR);
1930 } else if (iface->state == HAPD_IFACE_NO_IR) {
1931 if (all_no_ir) {
1932 wpa_printf(MSG_DEBUG,
1933 "NO_IR: AP in NO_IR and all chan in the new chanlist are NO_IR. Ignore");
1934 return 0;
1935 }
1936
1937 if (!iface->conf->acs) {
1938 struct hostapd_channel_data *chan;
1939
1940 chan = hw_get_channel_freq(iface->current_mode->mode,
1941 iface->freq, NULL,
1942 iface->hw_features,
1943 iface->num_hw_features);
1944 if (!chan) {
1945 wpa_printf(MSG_ERROR,
1946 "NO_IR: Could not derive chan from freq");
1947 return 0;
1948 }
1949
1950 /* If the last operating channel is NO_IR, trigger ACS.
1951 */
1952 if (chan->flag & HOSTAPD_CHAN_NO_IR) {
1953 iface->freq = 0;
1954 iface->conf->channel = 0;
1955 if (acs_init(iface) != HOSTAPD_CHAN_ACS)
1956 wpa_printf(MSG_ERROR,
1957 "NO_IR: Could not start ACS");
1958 return 0;
1959 }
1960 }
1961
1962 setup_interface2(iface);
1963 }
1964
1965 return 0;
1966}
1967
1968
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001969static void channel_list_update_timeout(void *eloop_ctx, void *timeout_ctx)
1970{
1971 struct hostapd_iface *iface = eloop_ctx;
1972
1973 if (!iface->wait_channel_update) {
1974 wpa_printf(MSG_INFO, "Channel list update timeout, but interface was not waiting for it");
1975 return;
1976 }
1977
1978 /*
1979 * It is possible that the existing channel list is acceptable, so try
1980 * to proceed.
1981 */
1982 wpa_printf(MSG_DEBUG, "Channel list update timeout - try to continue anyway");
1983 setup_interface2(iface);
1984}
1985
1986
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001987void hostapd_channel_list_updated(struct hostapd_iface *iface, int initiator)
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001988{
Sunil Ravi2a14cf12023-11-21 00:54:38 +00001989 if (initiator == REGDOM_SET_BY_DRIVER) {
1990 hostapd_for_each_interface(iface->interfaces,
1991 hostapd_no_ir_channel_list_updated,
1992 NULL);
1993 return;
1994 }
1995
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001996 if (!iface->wait_channel_update || initiator != REGDOM_SET_BY_USER)
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001997 return;
1998
1999 wpa_printf(MSG_DEBUG, "Channel list updated - continue setup");
2000 eloop_cancel_timeout(channel_list_update_timeout, iface, NULL);
2001 setup_interface2(iface);
2002}
2003
2004
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002005static int setup_interface(struct hostapd_iface *iface)
2006{
2007 struct hostapd_data *hapd = iface->bss[0];
2008 size_t i;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002009
Dmitry Shmidta38abf92014-03-06 13:38:44 -08002010 /*
2011 * It is possible that setup_interface() is called after the interface
2012 * was disabled etc., in which case driver_ap_teardown is possibly set
2013 * to 1. Clear it here so any other key/station deletion, which is not
2014 * part of a teardown flow, would also call the relevant driver
2015 * callbacks.
2016 */
2017 iface->driver_ap_teardown = 0;
2018
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002019 if (!iface->phy[0]) {
2020 const char *phy = hostapd_drv_get_radio_name(hapd);
2021 if (phy) {
2022 wpa_printf(MSG_DEBUG, "phy: %s", phy);
2023 os_strlcpy(iface->phy, phy, sizeof(iface->phy));
2024 }
2025 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002026
2027 /*
2028 * Make sure that all BSSes get configured with a pointer to the same
2029 * driver interface.
2030 */
2031 for (i = 1; i < iface->num_bss; i++) {
2032 iface->bss[i]->driver = hapd->driver;
2033 iface->bss[i]->drv_priv = hapd->drv_priv;
2034 }
2035
2036 if (hostapd_validate_bssid_configuration(iface))
2037 return -1;
2038
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002039 /*
2040 * Initialize control interfaces early to allow external monitoring of
2041 * channel setup operations that may take considerable amount of time
2042 * especially for DFS cases.
2043 */
2044 if (start_ctrl_iface(iface))
2045 return -1;
2046
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002047 if (hapd->iconf->country[0] && hapd->iconf->country[1]) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002048 char country[4], previous_country[4];
2049
2050 hostapd_set_state(iface, HAPD_IFACE_COUNTRY_UPDATE);
2051 if (hostapd_get_country(hapd, previous_country) < 0)
2052 previous_country[0] = '\0';
2053
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002054 os_memcpy(country, hapd->iconf->country, 3);
2055 country[3] = '\0';
2056 if (hostapd_set_country(hapd, country) < 0) {
2057 wpa_printf(MSG_ERROR, "Failed to set country code");
2058 return -1;
2059 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002060
2061 wpa_printf(MSG_DEBUG, "Previous country code %s, new country code %s",
2062 previous_country, country);
2063
2064 if (os_strncmp(previous_country, country, 2) != 0) {
2065 wpa_printf(MSG_DEBUG, "Continue interface setup after channel list update");
2066 iface->wait_channel_update = 1;
Dmitry Shmidt97672262014-02-03 13:02:54 -08002067 eloop_register_timeout(5, 0,
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002068 channel_list_update_timeout,
2069 iface, NULL);
2070 return 0;
2071 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002072 }
2073
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002074 return setup_interface2(iface);
2075}
2076
2077
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08002078static int configured_fixed_chan_to_freq(struct hostapd_iface *iface)
2079{
2080 int freq, i, j;
2081
2082 if (!iface->conf->channel)
2083 return 0;
2084 if (iface->conf->op_class) {
2085 freq = ieee80211_chan_to_freq(NULL, iface->conf->op_class,
2086 iface->conf->channel);
2087 if (freq < 0) {
2088 wpa_printf(MSG_INFO,
2089 "Could not convert op_class %u channel %u to operating frequency",
2090 iface->conf->op_class, iface->conf->channel);
2091 return -1;
2092 }
2093 iface->freq = freq;
2094 return 0;
2095 }
2096
2097 /* Old configurations using only 2.4/5/60 GHz bands may not specify the
2098 * op_class parameter. Select a matching channel from the configured
2099 * mode using the channel parameter for these cases.
2100 */
2101 for (j = 0; j < iface->num_hw_features; j++) {
2102 struct hostapd_hw_modes *mode = &iface->hw_features[j];
2103
2104 if (iface->conf->hw_mode != HOSTAPD_MODE_IEEE80211ANY &&
2105 iface->conf->hw_mode != mode->mode)
2106 continue;
2107 for (i = 0; i < mode->num_channels; i++) {
2108 struct hostapd_channel_data *chan = &mode->channels[i];
2109
2110 if (chan->chan == iface->conf->channel &&
2111 !is_6ghz_freq(chan->freq)) {
2112 iface->freq = chan->freq;
2113 return 0;
2114 }
2115 }
2116 }
2117
2118 wpa_printf(MSG_INFO, "Could not determine operating frequency");
2119 return -1;
2120}
2121
2122
Hai Shaloma20dcd72022-02-04 13:43:00 -08002123static void hostapd_set_6ghz_sec_chan(struct hostapd_iface *iface)
2124{
Sunil Ravi640215c2023-06-28 23:08:09 +00002125 int bw;
Hai Shaloma20dcd72022-02-04 13:43:00 -08002126
2127 if (!is_6ghz_op_class(iface->conf->op_class))
2128 return;
2129
Sunil Ravi640215c2023-06-28 23:08:09 +00002130 bw = op_class_to_bandwidth(iface->conf->op_class);
Hai Shaloma20dcd72022-02-04 13:43:00 -08002131 /* Assign the secondary channel if absent in config for
2132 * bandwidths > 20 MHz */
Sunil Ravi640215c2023-06-28 23:08:09 +00002133 if (bw >= 40 && !iface->conf->secondary_channel) {
Hai Shaloma20dcd72022-02-04 13:43:00 -08002134 if (((iface->conf->channel - 1) / 4) % 2)
2135 iface->conf->secondary_channel = -1;
2136 else
2137 iface->conf->secondary_channel = 1;
2138 }
2139}
2140
2141
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002142static int setup_interface2(struct hostapd_iface *iface)
2143{
2144 iface->wait_channel_update = 0;
Sunil Ravi2a14cf12023-11-21 00:54:38 +00002145 iface->is_no_ir = false;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002146
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002147 if (hostapd_get_hw_features(iface)) {
2148 /* Not all drivers support this yet, so continue without hw
2149 * feature data. */
2150 } else {
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08002151 int ret;
2152
Sunil Ravi99c035e2024-07-12 01:42:03 +00002153 if (iface->conf->acs && !iface->is_ch_switch_dfs) {
Sunil Ravi036cec52023-03-29 11:35:17 -07002154 iface->freq = 0;
2155 iface->conf->channel = 0;
2156 }
Sunil Ravi99c035e2024-07-12 01:42:03 +00002157 iface->is_ch_switch_dfs = false;
Sunil Ravi036cec52023-03-29 11:35:17 -07002158
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08002159 ret = configured_fixed_chan_to_freq(iface);
2160 if (ret < 0)
2161 goto fail;
2162
2163 if (iface->conf->op_class) {
Sunil8cd6f4d2022-06-28 18:40:46 +00002164 enum oper_chan_width ch_width;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08002165
2166 ch_width = op_class_to_ch_width(iface->conf->op_class);
2167 hostapd_set_oper_chwidth(iface->conf, ch_width);
Hai Shaloma20dcd72022-02-04 13:43:00 -08002168 hostapd_set_6ghz_sec_chan(iface);
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08002169 }
2170
2171 ret = hostapd_select_hw_mode(iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002172 if (ret < 0) {
2173 wpa_printf(MSG_ERROR, "Could not select hw_mode and "
2174 "channel. (%d)", ret);
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002175 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002176 }
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002177 if (ret == 1) {
2178 wpa_printf(MSG_DEBUG, "Interface initialization will be completed in a callback (ACS)");
2179 return 0;
2180 }
Hai Shalomc3565922019-10-28 11:58:20 -07002181 ret = hostapd_check_edmg_capab(iface);
2182 if (ret < 0)
2183 goto fail;
Hai Shalom60840252021-02-19 19:02:11 -08002184 ret = hostapd_check_he_6ghz_capab(iface);
2185 if (ret < 0)
2186 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002187 ret = hostapd_check_ht_capab(iface);
2188 if (ret < 0)
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002189 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002190 if (ret == 1) {
2191 wpa_printf(MSG_DEBUG, "Interface initialization will "
2192 "be completed in a callback");
2193 return 0;
2194 }
Dmitry Shmidt051af732013-10-22 13:52:46 -07002195
2196 if (iface->conf->ieee80211h)
2197 wpa_printf(MSG_DEBUG, "DFS support is enabled");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002198 }
2199 return hostapd_setup_interface_complete(iface, 0);
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002200
2201fail:
Sunil Ravi2a14cf12023-11-21 00:54:38 +00002202 if (iface->is_no_ir) {
2203 /* If AP is in NO_IR state, it can be reenabled by the driver
2204 * regulatory update and EVENT_CHANNEL_LIST_CHANGED. */
2205 hostapd_set_state(iface, HAPD_IFACE_NO_IR);
2206 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_NO_IR);
2207 return 0;
2208 }
2209
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002210 hostapd_set_state(iface, HAPD_IFACE_DISABLED);
2211 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
2212 if (iface->interfaces && iface->interfaces->terminate_on_error)
2213 eloop_terminate();
2214 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002215}
2216
2217
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002218#ifdef CONFIG_FST
2219
2220static const u8 * fst_hostapd_get_bssid_cb(void *ctx)
2221{
2222 struct hostapd_data *hapd = ctx;
2223
2224 return hapd->own_addr;
2225}
2226
2227
2228static void fst_hostapd_get_channel_info_cb(void *ctx,
2229 enum hostapd_hw_mode *hw_mode,
2230 u8 *channel)
2231{
2232 struct hostapd_data *hapd = ctx;
2233
2234 *hw_mode = ieee80211_freq_to_chan(hapd->iface->freq, channel);
2235}
2236
2237
Sunil8cd6f4d2022-06-28 18:40:46 +00002238static int fst_hostapd_get_hw_modes_cb(void *ctx,
2239 struct hostapd_hw_modes **modes)
2240{
2241 struct hostapd_data *hapd = ctx;
2242
2243 *modes = hapd->iface->hw_features;
2244 return hapd->iface->num_hw_features;
2245}
2246
2247
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002248static void fst_hostapd_set_ies_cb(void *ctx, const struct wpabuf *fst_ies)
2249{
2250 struct hostapd_data *hapd = ctx;
2251
2252 if (hapd->iface->fst_ies != fst_ies) {
2253 hapd->iface->fst_ies = fst_ies;
2254 if (ieee802_11_set_beacon(hapd))
2255 wpa_printf(MSG_WARNING, "FST: Cannot set beacon");
2256 }
2257}
2258
2259
2260static int fst_hostapd_send_action_cb(void *ctx, const u8 *da,
2261 struct wpabuf *buf)
2262{
2263 struct hostapd_data *hapd = ctx;
2264
2265 return hostapd_drv_send_action(hapd, hapd->iface->freq, 0, da,
2266 wpabuf_head(buf), wpabuf_len(buf));
2267}
2268
2269
2270static const struct wpabuf * fst_hostapd_get_mb_ie_cb(void *ctx, const u8 *addr)
2271{
2272 struct hostapd_data *hapd = ctx;
2273 struct sta_info *sta = ap_get_sta(hapd, addr);
2274
2275 return sta ? sta->mb_ies : NULL;
2276}
2277
2278
2279static void fst_hostapd_update_mb_ie_cb(void *ctx, const u8 *addr,
2280 const u8 *buf, size_t size)
2281{
2282 struct hostapd_data *hapd = ctx;
2283 struct sta_info *sta = ap_get_sta(hapd, addr);
2284
2285 if (sta) {
2286 struct mb_ies_info info;
2287
2288 if (!mb_ies_info_by_ies(&info, buf, size)) {
2289 wpabuf_free(sta->mb_ies);
2290 sta->mb_ies = mb_ies_by_info(&info);
2291 }
2292 }
2293}
2294
2295
2296static const u8 * fst_hostapd_get_sta(struct fst_get_peer_ctx **get_ctx,
Hai Shalome21d4e82020-04-29 16:34:06 -07002297 bool mb_only)
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002298{
2299 struct sta_info *s = (struct sta_info *) *get_ctx;
2300
2301 if (mb_only) {
2302 for (; s && !s->mb_ies; s = s->next)
2303 ;
2304 }
2305
2306 if (s) {
2307 *get_ctx = (struct fst_get_peer_ctx *) s->next;
2308
2309 return s->addr;
2310 }
2311
2312 *get_ctx = NULL;
2313 return NULL;
2314}
2315
2316
2317static const u8 * fst_hostapd_get_peer_first(void *ctx,
2318 struct fst_get_peer_ctx **get_ctx,
Hai Shalome21d4e82020-04-29 16:34:06 -07002319 bool mb_only)
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002320{
2321 struct hostapd_data *hapd = ctx;
2322
2323 *get_ctx = (struct fst_get_peer_ctx *) hapd->sta_list;
2324
2325 return fst_hostapd_get_sta(get_ctx, mb_only);
2326}
2327
2328
2329static const u8 * fst_hostapd_get_peer_next(void *ctx,
2330 struct fst_get_peer_ctx **get_ctx,
Hai Shalome21d4e82020-04-29 16:34:06 -07002331 bool mb_only)
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002332{
2333 return fst_hostapd_get_sta(get_ctx, mb_only);
2334}
2335
2336
2337void fst_hostapd_fill_iface_obj(struct hostapd_data *hapd,
2338 struct fst_wpa_obj *iface_obj)
2339{
Sunil8cd6f4d2022-06-28 18:40:46 +00002340 os_memset(iface_obj, 0, sizeof(*iface_obj));
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002341 iface_obj->ctx = hapd;
2342 iface_obj->get_bssid = fst_hostapd_get_bssid_cb;
2343 iface_obj->get_channel_info = fst_hostapd_get_channel_info_cb;
Sunil8cd6f4d2022-06-28 18:40:46 +00002344 iface_obj->get_hw_modes = fst_hostapd_get_hw_modes_cb;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002345 iface_obj->set_ies = fst_hostapd_set_ies_cb;
2346 iface_obj->send_action = fst_hostapd_send_action_cb;
2347 iface_obj->get_mb_ie = fst_hostapd_get_mb_ie_cb;
2348 iface_obj->update_mb_ie = fst_hostapd_update_mb_ie_cb;
2349 iface_obj->get_peer_first = fst_hostapd_get_peer_first;
2350 iface_obj->get_peer_next = fst_hostapd_get_peer_next;
2351}
2352
2353#endif /* CONFIG_FST */
2354
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002355#ifdef CONFIG_OWE
2356
2357static int hostapd_owe_iface_iter(struct hostapd_iface *iface, void *ctx)
2358{
2359 struct hostapd_data *hapd = ctx;
2360 size_t i;
2361
2362 for (i = 0; i < iface->num_bss; i++) {
2363 struct hostapd_data *bss = iface->bss[i];
2364
2365 if (os_strcmp(hapd->conf->owe_transition_ifname,
2366 bss->conf->iface) != 0)
2367 continue;
2368
2369 wpa_printf(MSG_DEBUG,
2370 "OWE: ifname=%s found transition mode ifname=%s BSSID "
2371 MACSTR " SSID %s",
2372 hapd->conf->iface, bss->conf->iface,
2373 MAC2STR(bss->own_addr),
2374 wpa_ssid_txt(bss->conf->ssid.ssid,
2375 bss->conf->ssid.ssid_len));
2376 if (!bss->conf->ssid.ssid_set || !bss->conf->ssid.ssid_len ||
2377 is_zero_ether_addr(bss->own_addr))
2378 continue;
2379
2380 os_memcpy(hapd->conf->owe_transition_bssid, bss->own_addr,
2381 ETH_ALEN);
2382 os_memcpy(hapd->conf->owe_transition_ssid,
2383 bss->conf->ssid.ssid, bss->conf->ssid.ssid_len);
2384 hapd->conf->owe_transition_ssid_len = bss->conf->ssid.ssid_len;
2385 wpa_printf(MSG_DEBUG,
2386 "OWE: Copied transition mode information");
2387 return 1;
2388 }
2389
2390 return 0;
2391}
2392
2393
2394int hostapd_owe_trans_get_info(struct hostapd_data *hapd)
2395{
2396 if (hapd->conf->owe_transition_ssid_len > 0 &&
2397 !is_zero_ether_addr(hapd->conf->owe_transition_bssid))
2398 return 0;
2399
2400 /* Find transition mode SSID/BSSID information from a BSS operated by
2401 * this hostapd instance. */
2402 if (!hapd->iface->interfaces ||
2403 !hapd->iface->interfaces->for_each_interface)
2404 return hostapd_owe_iface_iter(hapd->iface, hapd);
2405 else
2406 return hapd->iface->interfaces->for_each_interface(
2407 hapd->iface->interfaces, hostapd_owe_iface_iter, hapd);
2408}
2409
2410
2411static int hostapd_owe_iface_iter2(struct hostapd_iface *iface, void *ctx)
2412{
2413 size_t i;
2414
2415 for (i = 0; i < iface->num_bss; i++) {
2416 struct hostapd_data *bss = iface->bss[i];
2417 int res;
2418
2419 if (!bss->conf->owe_transition_ifname[0])
2420 continue;
Hai Shalom899fcc72020-10-19 14:38:18 -07002421 if (bss->iface->state != HAPD_IFACE_ENABLED) {
2422 wpa_printf(MSG_DEBUG,
2423 "OWE: Interface %s state %s - defer beacon update",
2424 bss->conf->iface,
2425 hostapd_state_text(bss->iface->state));
2426 continue;
2427 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002428 res = hostapd_owe_trans_get_info(bss);
2429 if (res == 0)
2430 continue;
2431 wpa_printf(MSG_DEBUG,
2432 "OWE: Matching transition mode interface enabled - update beacon data for %s",
2433 bss->conf->iface);
2434 ieee802_11_set_beacon(bss);
2435 }
2436
2437 return 0;
2438}
2439
2440#endif /* CONFIG_OWE */
2441
2442
2443static void hostapd_owe_update_trans(struct hostapd_iface *iface)
2444{
2445#ifdef CONFIG_OWE
2446 /* Check whether the enabled BSS can complete OWE transition mode
2447 * configuration for any pending interface. */
2448 if (!iface->interfaces ||
2449 !iface->interfaces->for_each_interface)
2450 hostapd_owe_iface_iter2(iface, NULL);
2451 else
2452 iface->interfaces->for_each_interface(
2453 iface->interfaces, hostapd_owe_iface_iter2, NULL);
2454#endif /* CONFIG_OWE */
2455}
2456
2457
Roshan Pius3a1667e2018-07-03 15:17:14 -07002458static void hostapd_interface_setup_failure_handler(void *eloop_ctx,
2459 void *timeout_ctx)
2460{
2461 struct hostapd_iface *iface = eloop_ctx;
2462 struct hostapd_data *hapd;
2463
2464 if (iface->num_bss < 1 || !iface->bss || !iface->bss[0])
2465 return;
2466 hapd = iface->bss[0];
2467 if (hapd->setup_complete_cb)
2468 hapd->setup_complete_cb(hapd->setup_complete_cb_ctx);
2469}
2470
2471
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002472static int hostapd_setup_interface_complete_sync(struct hostapd_iface *iface,
2473 int err)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002474{
2475 struct hostapd_data *hapd = iface->bss[0];
2476 size_t j;
2477 u8 *prev_addr;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002478 int delay_apply_cfg = 0;
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08002479 int res_dfs_offload = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002480
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002481 if (err)
2482 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002483
2484 wpa_printf(MSG_DEBUG, "Completing interface initialization");
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08002485 if (iface->freq) {
Dmitry Shmidt051af732013-10-22 13:52:46 -07002486#ifdef NEED_AP_MLME
2487 int res;
2488#endif /* NEED_AP_MLME */
2489
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002490 wpa_printf(MSG_DEBUG, "Mode: %s Channel: %d "
2491 "Frequency: %d MHz",
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002492 hostapd_hw_mode_txt(iface->conf->hw_mode),
2493 iface->conf->channel, iface->freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002494
Dmitry Shmidt051af732013-10-22 13:52:46 -07002495#ifdef NEED_AP_MLME
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07002496 /* Handle DFS only if it is not offloaded to the driver */
2497 if (!(iface->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD)) {
2498 /* Check DFS */
2499 res = hostapd_handle_dfs(iface);
2500 if (res <= 0) {
2501 if (res < 0)
2502 goto fail;
2503 return res;
2504 }
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08002505 } else {
2506 /* If DFS is offloaded to the driver */
2507 res_dfs_offload = hostapd_handle_dfs_offload(iface);
2508 if (res_dfs_offload <= 0) {
2509 if (res_dfs_offload < 0)
2510 goto fail;
2511 } else {
2512 wpa_printf(MSG_DEBUG,
2513 "Proceed with AP/channel setup");
2514 /*
2515 * If this is a DFS channel, move to completing
2516 * AP setup.
2517 */
2518 if (res_dfs_offload == 1)
2519 goto dfs_offload;
2520 /* Otherwise fall through. */
2521 }
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002522 }
Dmitry Shmidt051af732013-10-22 13:52:46 -07002523#endif /* NEED_AP_MLME */
2524
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002525#ifdef CONFIG_MESH
2526 if (iface->mconf != NULL) {
2527 wpa_printf(MSG_DEBUG,
2528 "%s: Mesh configuration will be applied while joining the mesh network",
2529 iface->bss[0]->conf->iface);
2530 delay_apply_cfg = 1;
2531 }
2532#endif /* CONFIG_MESH */
2533
2534 if (!delay_apply_cfg &&
2535 hostapd_set_freq(hapd, hapd->iconf->hw_mode, iface->freq,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002536 hapd->iconf->channel,
Hai Shalomc3565922019-10-28 11:58:20 -07002537 hapd->iconf->enable_edmg,
2538 hapd->iconf->edmg_channel,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002539 hapd->iconf->ieee80211n,
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002540 hapd->iconf->ieee80211ac,
Hai Shalom81f62d82019-07-22 12:10:00 -07002541 hapd->iconf->ieee80211ax,
Sunil Ravia04bd252022-05-02 22:54:18 -07002542 hapd->iconf->ieee80211be,
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002543 hapd->iconf->secondary_channel,
Hai Shalom81f62d82019-07-22 12:10:00 -07002544 hostapd_get_oper_chwidth(hapd->iconf),
2545 hostapd_get_oper_centr_freq_seg0_idx(
2546 hapd->iconf),
2547 hostapd_get_oper_centr_freq_seg1_idx(
2548 hapd->iconf))) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002549 wpa_printf(MSG_ERROR, "Could not set channel for "
2550 "kernel driver");
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002551 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002552 }
2553 }
2554
2555 if (iface->current_mode) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002556 if (hostapd_prepare_rates(iface, iface->current_mode)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002557 wpa_printf(MSG_ERROR, "Failed to prepare rates "
2558 "table.");
2559 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
2560 HOSTAPD_LEVEL_WARNING,
2561 "Failed to prepare rates table.");
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002562 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002563 }
2564 }
2565
Hai Shalom021b0b52019-04-10 11:17:58 -07002566 if (hapd->iconf->rts_threshold >= -1 &&
2567 hostapd_set_rts(hapd, hapd->iconf->rts_threshold) &&
2568 hapd->iconf->rts_threshold >= -1) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002569 wpa_printf(MSG_ERROR, "Could not set RTS threshold for "
2570 "kernel driver");
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002571 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002572 }
2573
Hai Shalom021b0b52019-04-10 11:17:58 -07002574 if (hapd->iconf->fragm_threshold >= -1 &&
2575 hostapd_set_frag(hapd, hapd->iconf->fragm_threshold) &&
2576 hapd->iconf->fragm_threshold != -1) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002577 wpa_printf(MSG_ERROR, "Could not set fragmentation threshold "
2578 "for kernel driver");
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002579 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002580 }
2581
2582 prev_addr = hapd->own_addr;
2583
2584 for (j = 0; j < iface->num_bss; j++) {
2585 hapd = iface->bss[j];
2586 if (j)
2587 os_memcpy(hapd->own_addr, prev_addr, ETH_ALEN);
Sunil Ravi77d572f2023-01-17 23:58:31 +00002588 if (hostapd_setup_bss(hapd, j == 0, !iface->conf->mbssid)) {
Hai Shalom021b0b52019-04-10 11:17:58 -07002589 for (;;) {
Dmitry Shmidt71757432014-06-02 13:50:35 -07002590 hapd = iface->bss[j];
2591 hostapd_bss_deinit_no_free(hapd);
2592 hostapd_free_hapd_data(hapd);
Hai Shalom021b0b52019-04-10 11:17:58 -07002593 if (j == 0)
2594 break;
2595 j--;
2596 }
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002597 goto fail;
Dmitry Shmidt71757432014-06-02 13:50:35 -07002598 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002599 if (is_zero_ether_addr(hapd->conf->bssid))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002600 prev_addr = hapd->own_addr;
2601 }
Sunil Ravi77d572f2023-01-17 23:58:31 +00002602
2603 if (hapd->iconf->mbssid) {
2604 for (j = 0; hapd->iconf->mbssid && j < iface->num_bss; j++) {
2605 hapd = iface->bss[j];
2606 if (hostapd_start_beacon(hapd, true)) {
2607 for (;;) {
2608 hapd = iface->bss[j];
2609 hostapd_bss_deinit_no_free(hapd);
2610 hostapd_free_hapd_data(hapd);
2611 if (j == 0)
2612 break;
2613 j--;
2614 }
2615 goto fail;
2616 }
2617 }
2618 }
2619
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002620 hapd = iface->bss[0];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002621
2622 hostapd_tx_queue_params(iface);
2623
2624 ap_list_init(iface);
2625
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07002626 hostapd_set_acl(hapd);
2627
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002628 if (hostapd_driver_commit(hapd) < 0) {
2629 wpa_printf(MSG_ERROR, "%s: Failed to commit driver "
2630 "configuration", __func__);
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002631 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002632 }
2633
Jouni Malinen87fd2792011-05-16 18:35:42 +03002634 /*
2635 * WPS UPnP module can be initialized only when the "upnp_iface" is up.
2636 * If "interface" and "upnp_iface" are the same (e.g., non-bridge
2637 * mode), the interface is up only after driver_commit, so initialize
2638 * WPS after driver_commit.
2639 */
2640 for (j = 0; j < iface->num_bss; j++) {
2641 if (hostapd_init_wps_complete(iface->bss[j]))
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002642 goto fail;
Jouni Malinen87fd2792011-05-16 18:35:42 +03002643 }
2644
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08002645 if ((iface->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
2646 !res_dfs_offload) {
2647 /*
2648 * If freq is DFS, and DFS is offloaded to the driver, then wait
2649 * for CAC to complete.
2650 */
2651 wpa_printf(MSG_DEBUG, "%s: Wait for CAC to complete", __func__);
2652 return res_dfs_offload;
2653 }
2654
2655#ifdef NEED_AP_MLME
2656dfs_offload:
2657#endif /* NEED_AP_MLME */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002658
2659#ifdef CONFIG_FST
2660 if (hapd->iconf->fst_cfg.group_id[0]) {
2661 struct fst_wpa_obj iface_obj;
2662
2663 fst_hostapd_fill_iface_obj(hapd, &iface_obj);
2664 iface->fst = fst_attach(hapd->conf->iface, hapd->own_addr,
2665 &iface_obj, &hapd->iconf->fst_cfg);
2666 if (!iface->fst) {
2667 wpa_printf(MSG_ERROR, "Could not attach to FST %s",
2668 hapd->iconf->fst_cfg.group_id);
2669 goto fail;
2670 }
2671 }
2672#endif /* CONFIG_FST */
2673
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002674 hostapd_set_state(iface, HAPD_IFACE_ENABLED);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002675 hostapd_owe_update_trans(iface);
Hai Shalom81f62d82019-07-22 12:10:00 -07002676 airtime_policy_update_init(iface);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002677 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_ENABLED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002678 if (hapd->setup_complete_cb)
2679 hapd->setup_complete_cb(hapd->setup_complete_cb_ctx);
2680
Hai Shalom60840252021-02-19 19:02:11 -08002681#ifdef CONFIG_MESH
2682 if (delay_apply_cfg && !iface->mconf) {
2683 wpa_printf(MSG_ERROR, "Error while completing mesh init");
2684 goto fail;
2685 }
2686#endif /* CONFIG_MESH */
2687
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002688 wpa_printf(MSG_DEBUG, "%s: Setup of interface done.",
2689 iface->bss[0]->conf->iface);
Dmitry Shmidtb96dad42013-11-05 10:07:29 -08002690 if (iface->interfaces && iface->interfaces->terminate_on_error > 0)
2691 iface->interfaces->terminate_on_error--;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002692
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002693 for (j = 0; j < iface->num_bss; j++)
Hai Shalom74f70d42019-02-11 14:42:39 -08002694 hostapd_neighbor_set_own_report(iface->bss[j]);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002695
Sunil Ravi2a14cf12023-11-21 00:54:38 +00002696 if (iface->interfaces && iface->interfaces->count > 1)
2697 ieee802_11_set_beacons(iface);
2698
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002699 return 0;
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002700
2701fail:
2702 wpa_printf(MSG_ERROR, "Interface initialization failed");
Sunil Ravi2a14cf12023-11-21 00:54:38 +00002703
2704 if (iface->is_no_ir) {
2705 hostapd_set_state(iface, HAPD_IFACE_NO_IR);
2706 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_EVENT_NO_IR);
2707 return 0;
2708 }
2709
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002710 hostapd_set_state(iface, HAPD_IFACE_DISABLED);
2711 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002712#ifdef CONFIG_FST
2713 if (iface->fst) {
2714 fst_detach(iface->fst);
2715 iface->fst = NULL;
2716 }
2717#endif /* CONFIG_FST */
Roshan Pius3a1667e2018-07-03 15:17:14 -07002718
2719 if (iface->interfaces && iface->interfaces->terminate_on_error) {
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002720 eloop_terminate();
Roshan Pius3a1667e2018-07-03 15:17:14 -07002721 } else if (hapd->setup_complete_cb) {
2722 /*
2723 * Calling hapd->setup_complete_cb directly may cause iface
2724 * deinitialization which may be accessed later by the caller.
2725 */
2726 eloop_register_timeout(0, 0,
2727 hostapd_interface_setup_failure_handler,
2728 iface, NULL);
2729 }
2730
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002731 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002732}
2733
2734
2735/**
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002736 * hostapd_setup_interface_complete - Complete interface setup
2737 *
2738 * This function is called when previous steps in the interface setup has been
2739 * completed. This can also start operations, e.g., DFS, that will require
2740 * additional processing before interface is ready to be enabled. Such
2741 * operations will call this function from eloop callbacks when finished.
2742 */
2743int hostapd_setup_interface_complete(struct hostapd_iface *iface, int err)
2744{
2745 struct hapd_interfaces *interfaces = iface->interfaces;
2746 struct hostapd_data *hapd = iface->bss[0];
2747 unsigned int i;
2748 int not_ready_in_sync_ifaces = 0;
2749
2750 if (!iface->need_to_start_in_sync)
2751 return hostapd_setup_interface_complete_sync(iface, err);
2752
2753 if (err) {
2754 wpa_printf(MSG_ERROR, "Interface initialization failed");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002755 iface->need_to_start_in_sync = 0;
Sunil Ravi2a14cf12023-11-21 00:54:38 +00002756
2757 if (iface->is_no_ir) {
2758 hostapd_set_state(iface, HAPD_IFACE_NO_IR);
2759 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_EVENT_NO_IR);
2760 return 0;
2761 }
2762
2763 hostapd_set_state(iface, HAPD_IFACE_DISABLED);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002764 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
2765 if (interfaces && interfaces->terminate_on_error)
2766 eloop_terminate();
2767 return -1;
2768 }
2769
2770 if (iface->ready_to_start_in_sync) {
2771 /* Already in ready and waiting. should never happpen */
2772 return 0;
2773 }
2774
2775 for (i = 0; i < interfaces->count; i++) {
2776 if (interfaces->iface[i]->need_to_start_in_sync &&
2777 !interfaces->iface[i]->ready_to_start_in_sync)
2778 not_ready_in_sync_ifaces++;
2779 }
2780
2781 /*
2782 * Check if this is the last interface, if yes then start all the other
2783 * waiting interfaces. If not, add this interface to the waiting list.
2784 */
2785 if (not_ready_in_sync_ifaces > 1 && iface->state == HAPD_IFACE_DFS) {
2786 /*
2787 * If this interface went through CAC, do not synchronize, just
2788 * start immediately.
2789 */
2790 iface->need_to_start_in_sync = 0;
2791 wpa_printf(MSG_INFO,
2792 "%s: Finished CAC - bypass sync and start interface",
2793 iface->bss[0]->conf->iface);
2794 return hostapd_setup_interface_complete_sync(iface, err);
2795 }
2796
2797 if (not_ready_in_sync_ifaces > 1) {
2798 /* need to wait as there are other interfaces still coming up */
2799 iface->ready_to_start_in_sync = 1;
2800 wpa_printf(MSG_INFO,
2801 "%s: Interface waiting to sync with other interfaces",
2802 iface->bss[0]->conf->iface);
2803 return 0;
2804 }
2805
2806 wpa_printf(MSG_INFO,
2807 "%s: Last interface to sync - starting all interfaces",
2808 iface->bss[0]->conf->iface);
2809 iface->need_to_start_in_sync = 0;
2810 hostapd_setup_interface_complete_sync(iface, err);
2811 for (i = 0; i < interfaces->count; i++) {
2812 if (interfaces->iface[i]->need_to_start_in_sync &&
2813 interfaces->iface[i]->ready_to_start_in_sync) {
2814 hostapd_setup_interface_complete_sync(
2815 interfaces->iface[i], 0);
2816 /* Only once the interfaces are sync started */
2817 interfaces->iface[i]->need_to_start_in_sync = 0;
2818 }
2819 }
2820
2821 return 0;
2822}
2823
2824
2825/**
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002826 * hostapd_setup_interface - Setup of an interface
2827 * @iface: Pointer to interface data.
2828 * Returns: 0 on success, -1 on failure
2829 *
2830 * Initializes the driver interface, validates the configuration,
2831 * and sets driver parameters based on the configuration.
2832 * Flushes old stations, sets the channel, encryption,
2833 * beacons, and WDS links based on the configuration.
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002834 *
2835 * If interface setup requires more time, e.g., to perform HT co-ex scans, ACS,
2836 * or DFS operations, this function returns 0 before such operations have been
2837 * completed. The pending operations are registered into eloop and will be
2838 * completed from eloop callbacks. Those callbacks end up calling
2839 * hostapd_setup_interface_complete() once setup has been completed.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002840 */
2841int hostapd_setup_interface(struct hostapd_iface *iface)
2842{
2843 int ret;
2844
Hai Shaloma20dcd72022-02-04 13:43:00 -08002845 if (!iface->conf)
2846 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002847 ret = setup_interface(iface);
2848 if (ret) {
2849 wpa_printf(MSG_ERROR, "%s: Unable to setup interface.",
Hai Shaloma20dcd72022-02-04 13:43:00 -08002850 iface->conf->bss[0]->iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002851 return -1;
2852 }
2853
2854 return 0;
2855}
2856
2857
2858/**
2859 * hostapd_alloc_bss_data - Allocate and initialize per-BSS data
2860 * @hapd_iface: Pointer to interface data
2861 * @conf: Pointer to per-interface configuration
2862 * @bss: Pointer to per-BSS configuration for this BSS
2863 * Returns: Pointer to allocated BSS data
2864 *
2865 * This function is used to allocate per-BSS data structure. This data will be
2866 * freed after hostapd_cleanup() is called for it during interface
2867 * deinitialization.
2868 */
2869struct hostapd_data *
2870hostapd_alloc_bss_data(struct hostapd_iface *hapd_iface,
2871 struct hostapd_config *conf,
2872 struct hostapd_bss_config *bss)
2873{
2874 struct hostapd_data *hapd;
2875
2876 hapd = os_zalloc(sizeof(*hapd));
2877 if (hapd == NULL)
2878 return NULL;
2879
2880 hapd->new_assoc_sta_cb = hostapd_new_assoc_sta;
2881 hapd->iconf = conf;
2882 hapd->conf = bss;
2883 hapd->iface = hapd_iface;
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08002884 if (conf)
2885 hapd->driver = conf->driver;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002886 hapd->ctrl_sock = -1;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08002887 dl_list_init(&hapd->ctrl_dst);
Dmitry Shmidt7d175302016-09-06 13:11:34 -07002888 dl_list_init(&hapd->nr_db);
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08002889 hapd->dhcp_sock = -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002890#ifdef CONFIG_IEEE80211R_AP
2891 dl_list_init(&hapd->l2_queue);
2892 dl_list_init(&hapd->l2_oui_queue);
2893#endif /* CONFIG_IEEE80211R_AP */
Hai Shalom021b0b52019-04-10 11:17:58 -07002894#ifdef CONFIG_SAE
2895 dl_list_init(&hapd->sae_commit_queue);
2896#endif /* CONFIG_SAE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002897
2898 return hapd;
2899}
2900
2901
Dmitry Shmidt54605472013-11-08 11:10:19 -08002902static void hostapd_bss_deinit(struct hostapd_data *hapd)
2903{
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07002904 if (!hapd)
2905 return;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002906 wpa_printf(MSG_DEBUG, "%s: deinit bss %s", __func__,
Hai Shalom021b0b52019-04-10 11:17:58 -07002907 hapd->conf ? hapd->conf->iface : "N/A");
Dmitry Shmidt71757432014-06-02 13:50:35 -07002908 hostapd_bss_deinit_no_free(hapd);
Dmitry Shmidtf73259c2015-03-17 11:00:54 -07002909 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
Hai Shalomc3565922019-10-28 11:58:20 -07002910#ifdef CONFIG_SQLITE
2911 if (hapd->rad_attr_db) {
2912 sqlite3_close(hapd->rad_attr_db);
2913 hapd->rad_attr_db = NULL;
2914 }
2915#endif /* CONFIG_SQLITE */
Sunil Ravi99c035e2024-07-12 01:42:03 +00002916
2917 hostapd_bss_link_deinit(hapd);
Dmitry Shmidt54605472013-11-08 11:10:19 -08002918 hostapd_cleanup(hapd);
2919}
2920
2921
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002922void hostapd_interface_deinit(struct hostapd_iface *iface)
2923{
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002924 int j;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002925
Dmitry Shmidt54605472013-11-08 11:10:19 -08002926 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002927 if (iface == NULL)
2928 return;
2929
Dmitry Shmidtf73259c2015-03-17 11:00:54 -07002930 hostapd_set_state(iface, HAPD_IFACE_DISABLED);
2931
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002932 eloop_cancel_timeout(channel_list_update_timeout, iface, NULL);
2933 iface->wait_channel_update = 0;
Sunil Ravi2a14cf12023-11-21 00:54:38 +00002934 iface->is_no_ir = false;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002935
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002936#ifdef CONFIG_FST
2937 if (iface->fst) {
2938 fst_detach(iface->fst);
2939 iface->fst = NULL;
2940 }
2941#endif /* CONFIG_FST */
2942
Hai Shalom021b0b52019-04-10 11:17:58 -07002943 for (j = (int) iface->num_bss - 1; j >= 0; j--) {
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07002944 if (!iface->bss)
2945 break;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002946 hostapd_bss_deinit(iface->bss[j]);
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07002947 }
Roshan Pius3a1667e2018-07-03 15:17:14 -07002948
Roshan Pius3a1667e2018-07-03 15:17:14 -07002949#ifdef NEED_AP_MLME
2950 hostapd_stop_setup_timers(iface);
2951 eloop_cancel_timeout(ap_ht2040_timeout, iface, NULL);
2952#endif /* NEED_AP_MLME */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002953}
2954
2955
Sunil Ravi99c035e2024-07-12 01:42:03 +00002956#ifdef CONFIG_IEEE80211BE
2957
2958static void hostapd_mld_ref_inc(struct hostapd_mld *mld)
2959{
2960 if (!mld)
2961 return;
2962
2963 if (mld->refcount == HOSTAPD_MLD_MAX_REF_COUNT) {
2964 wpa_printf(MSG_ERROR, "AP MLD %s: Ref count overflow",
2965 mld->name);
2966 return;
2967 }
2968
2969 mld->refcount++;
2970}
2971
2972
2973static void hostapd_mld_ref_dec(struct hostapd_mld *mld)
2974{
2975 if (!mld)
2976 return;
2977
2978 if (!mld->refcount) {
2979 wpa_printf(MSG_ERROR, "AP MLD %s: Ref count underflow",
2980 mld->name);
2981 return;
2982 }
2983
2984 mld->refcount--;
2985}
2986
2987#endif /* CONFIG_IEEE80211BE */
2988
2989
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002990void hostapd_interface_free(struct hostapd_iface *iface)
2991{
2992 size_t j;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002993 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
2994 for (j = 0; j < iface->num_bss; j++) {
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07002995 if (!iface->bss)
2996 break;
Sunil Ravi99c035e2024-07-12 01:42:03 +00002997#ifdef CONFIG_IEEE80211BE
2998 if (iface->bss[j])
2999 hostapd_mld_ref_dec(iface->bss[j]->mld);
3000#endif /* CONFIG_IEEE80211BE */
Dmitry Shmidt54605472013-11-08 11:10:19 -08003001 wpa_printf(MSG_DEBUG, "%s: free hapd %p",
3002 __func__, iface->bss[j]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003003 os_free(iface->bss[j]);
Dmitry Shmidt54605472013-11-08 11:10:19 -08003004 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003005 hostapd_cleanup_iface(iface);
3006}
3007
3008
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07003009struct hostapd_iface * hostapd_alloc_iface(void)
3010{
3011 struct hostapd_iface *hapd_iface;
3012
3013 hapd_iface = os_zalloc(sizeof(*hapd_iface));
3014 if (!hapd_iface)
3015 return NULL;
3016
3017 dl_list_init(&hapd_iface->sta_seen);
3018
3019 return hapd_iface;
3020}
3021
3022
Sunil Ravi99c035e2024-07-12 01:42:03 +00003023#ifdef CONFIG_IEEE80211BE
3024static void hostapd_bss_alloc_link_id(struct hostapd_data *hapd)
3025{
3026 hapd->mld_link_id = hapd->mld->next_link_id++;
3027 wpa_printf(MSG_DEBUG, "AP MLD: %s: Link ID %d assigned.",
3028 hapd->mld->name, hapd->mld_link_id);
3029}
3030#endif /* CONFIG_IEEE80211BE */
3031
3032
3033static void hostapd_bss_setup_multi_link(struct hostapd_data *hapd,
3034 struct hapd_interfaces *interfaces)
3035{
3036#ifdef CONFIG_IEEE80211BE
3037 struct hostapd_mld *mld, **all_mld;
3038 struct hostapd_bss_config *conf;
3039 size_t i;
3040
3041 conf = hapd->conf;
3042
3043 if (!hapd->iconf || !hapd->iconf->ieee80211be || !conf->mld_ap ||
3044 conf->disable_11be)
3045 return;
3046
3047 for (i = 0; i < interfaces->mld_count; i++) {
3048 mld = interfaces->mld[i];
3049
3050 if (!mld || os_strcmp(conf->iface, mld->name) != 0)
3051 continue;
3052
3053 hapd->mld = mld;
3054 hostapd_mld_ref_inc(mld);
3055 hostapd_bss_alloc_link_id(hapd);
3056 break;
3057 }
3058
3059 if (hapd->mld)
3060 return;
3061
3062 mld = os_zalloc(sizeof(struct hostapd_mld));
3063 if (!mld)
3064 goto fail;
3065
3066 os_strlcpy(mld->name, conf->iface, sizeof(conf->iface));
3067 dl_list_init(&mld->links);
3068
3069 wpa_printf(MSG_DEBUG, "AP MLD %s created", mld->name);
3070
3071 hapd->mld = mld;
3072 hostapd_mld_ref_inc(mld);
3073 hostapd_bss_alloc_link_id(hapd);
3074
3075 all_mld = os_realloc_array(interfaces->mld, interfaces->mld_count + 1,
3076 sizeof(struct hostapd_mld *));
3077 if (!all_mld)
3078 goto fail;
3079
3080 interfaces->mld = all_mld;
3081 interfaces->mld[interfaces->mld_count] = mld;
3082 interfaces->mld_count++;
3083
3084 return;
3085fail:
3086 if (!mld)
3087 return;
3088
3089 wpa_printf(MSG_DEBUG, "AP MLD %s: free mld %p", mld->name, mld);
3090 os_free(mld);
3091 hapd->mld = NULL;
3092#endif /* CONFIG_IEEE80211BE */
3093}
3094
3095
3096static void hostapd_cleanup_unused_mlds(struct hapd_interfaces *interfaces)
3097{
3098#ifdef CONFIG_IEEE80211BE
3099 struct hostapd_mld *mld, **all_mld;
3100 size_t i, j, num_mlds;
3101 bool forced_remove, remove;
3102
3103 if (!interfaces->mld)
3104 return;
3105
3106 num_mlds = interfaces->mld_count;
3107
3108 for (i = 0; i < interfaces->mld_count; i++) {
3109 mld = interfaces->mld[i];
3110 if (!mld)
3111 continue;
3112
3113 remove = false;
3114 forced_remove = false;
3115
3116 if (!mld->refcount)
3117 remove = true;
3118
3119 /* If MLD is still being referenced but the number of interfaces
3120 * is zero, it is safe to force its deletion. Normally, this
3121 * should not happen but even if it does, let us free the
3122 * memory.
3123 */
3124 if (!remove && !interfaces->count)
3125 forced_remove = true;
3126
3127 if (!remove && !forced_remove)
3128 continue;
3129
3130 wpa_printf(MSG_DEBUG, "AP MLD %s: Freed%s", mld->name,
3131 forced_remove ? " (forced)" : "");
3132 os_free(mld);
3133 interfaces->mld[i] = NULL;
3134 num_mlds--;
3135 }
3136
3137 if (!num_mlds) {
3138 interfaces->mld_count = 0;
3139 os_free(interfaces->mld);
3140 interfaces->mld = NULL;
3141 return;
3142 }
3143
3144 all_mld = os_zalloc(num_mlds * sizeof(struct hostapd_mld *));
3145 if (!all_mld) {
3146 wpa_printf(MSG_ERROR,
3147 "AP MLD: Failed to re-allocate the MLDs. Expect issues");
3148 return;
3149 }
3150
3151 for (i = 0, j = 0; i < interfaces->mld_count; i++) {
3152 mld = interfaces->mld[i];
3153 if (!mld)
3154 continue;
3155
3156 all_mld[j++] = mld;
3157 }
3158
3159 /* This should not happen */
3160 if (j != num_mlds) {
3161 wpa_printf(MSG_DEBUG,
3162 "AP MLD: Some error occurred while reallocating MLDs. Expect issues.");
3163 os_free(all_mld);
3164 return;
3165 }
3166
3167 os_free(interfaces->mld);
3168 interfaces->mld = all_mld;
3169 interfaces->mld_count = num_mlds;
3170#endif /* CONFIG_IEEE80211BE */
3171}
3172
3173
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003174/**
3175 * hostapd_init - Allocate and initialize per-interface data
3176 * @config_file: Path to the configuration file
3177 * Returns: Pointer to the allocated interface data or %NULL on failure
3178 *
3179 * This function is used to allocate main data structures for per-interface
3180 * data. The allocated data buffer will be freed by calling
3181 * hostapd_cleanup_iface().
3182 */
3183struct hostapd_iface * hostapd_init(struct hapd_interfaces *interfaces,
3184 const char *config_file)
3185{
3186 struct hostapd_iface *hapd_iface = NULL;
3187 struct hostapd_config *conf = NULL;
3188 struct hostapd_data *hapd;
3189 size_t i;
3190
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07003191 hapd_iface = hostapd_alloc_iface();
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003192 if (hapd_iface == NULL)
3193 goto fail;
3194
3195 hapd_iface->config_fname = os_strdup(config_file);
3196 if (hapd_iface->config_fname == NULL)
3197 goto fail;
3198
3199 conf = interfaces->config_read_cb(hapd_iface->config_fname);
3200 if (conf == NULL)
3201 goto fail;
3202 hapd_iface->conf = conf;
3203
3204 hapd_iface->num_bss = conf->num_bss;
3205 hapd_iface->bss = os_calloc(conf->num_bss,
3206 sizeof(struct hostapd_data *));
3207 if (hapd_iface->bss == NULL)
3208 goto fail;
3209
3210 for (i = 0; i < conf->num_bss; i++) {
3211 hapd = hapd_iface->bss[i] =
3212 hostapd_alloc_bss_data(hapd_iface, conf,
3213 conf->bss[i]);
3214 if (hapd == NULL)
3215 goto fail;
3216 hapd->msg_ctx = hapd;
Sunil Ravi99c035e2024-07-12 01:42:03 +00003217 hostapd_bss_setup_multi_link(hapd, interfaces);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003218 }
3219
Sunil Ravi99c035e2024-07-12 01:42:03 +00003220 hapd_iface->is_ch_switch_dfs = false;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003221 return hapd_iface;
3222
3223fail:
3224 wpa_printf(MSG_ERROR, "Failed to set up interface with %s",
3225 config_file);
3226 if (conf)
3227 hostapd_config_free(conf);
3228 if (hapd_iface) {
3229 os_free(hapd_iface->config_fname);
3230 os_free(hapd_iface->bss);
Dmitry Shmidt54605472013-11-08 11:10:19 -08003231 wpa_printf(MSG_DEBUG, "%s: free iface %p",
3232 __func__, hapd_iface);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003233 os_free(hapd_iface);
3234 }
3235 return NULL;
3236}
3237
3238
3239static int ifname_in_use(struct hapd_interfaces *interfaces, const char *ifname)
3240{
3241 size_t i, j;
3242
3243 for (i = 0; i < interfaces->count; i++) {
3244 struct hostapd_iface *iface = interfaces->iface[i];
3245 for (j = 0; j < iface->num_bss; j++) {
3246 struct hostapd_data *hapd = iface->bss[j];
3247 if (os_strcmp(ifname, hapd->conf->iface) == 0)
3248 return 1;
3249 }
3250 }
3251
3252 return 0;
3253}
3254
3255
3256/**
3257 * hostapd_interface_init_bss - Read configuration file and init BSS data
3258 *
3259 * This function is used to parse configuration file for a BSS. This BSS is
3260 * added to an existing interface sharing the same radio (if any) or a new
3261 * interface is created if this is the first interface on a radio. This
3262 * allocate memory for the BSS. No actual driver operations are started.
3263 *
3264 * This is similar to hostapd_interface_init(), but for a case where the
3265 * configuration is used to add a single BSS instead of all BSSes for a radio.
3266 */
3267struct hostapd_iface *
3268hostapd_interface_init_bss(struct hapd_interfaces *interfaces, const char *phy,
3269 const char *config_fname, int debug)
3270{
3271 struct hostapd_iface *new_iface = NULL, *iface = NULL;
3272 struct hostapd_data *hapd;
3273 int k;
3274 size_t i, bss_idx;
3275
3276 if (!phy || !*phy)
3277 return NULL;
3278
3279 for (i = 0; i < interfaces->count; i++) {
3280 if (os_strcmp(interfaces->iface[i]->phy, phy) == 0) {
3281 iface = interfaces->iface[i];
3282 break;
3283 }
3284 }
3285
3286 wpa_printf(MSG_INFO, "Configuration file: %s (phy %s)%s",
3287 config_fname, phy, iface ? "" : " --> new PHY");
3288 if (iface) {
3289 struct hostapd_config *conf;
3290 struct hostapd_bss_config **tmp_conf;
3291 struct hostapd_data **tmp_bss;
3292 struct hostapd_bss_config *bss;
3293 const char *ifname;
3294
3295 /* Add new BSS to existing iface */
3296 conf = interfaces->config_read_cb(config_fname);
3297 if (conf == NULL)
3298 return NULL;
3299 if (conf->num_bss > 1) {
3300 wpa_printf(MSG_ERROR, "Multiple BSSes specified in BSS-config");
3301 hostapd_config_free(conf);
3302 return NULL;
3303 }
3304
3305 ifname = conf->bss[0]->iface;
3306 if (ifname[0] != '\0' && ifname_in_use(interfaces, ifname)) {
3307 wpa_printf(MSG_ERROR,
3308 "Interface name %s already in use", ifname);
3309 hostapd_config_free(conf);
3310 return NULL;
3311 }
3312
3313 tmp_conf = os_realloc_array(
3314 iface->conf->bss, iface->conf->num_bss + 1,
3315 sizeof(struct hostapd_bss_config *));
3316 tmp_bss = os_realloc_array(iface->bss, iface->num_bss + 1,
3317 sizeof(struct hostapd_data *));
3318 if (tmp_bss)
3319 iface->bss = tmp_bss;
3320 if (tmp_conf) {
3321 iface->conf->bss = tmp_conf;
3322 iface->conf->last_bss = tmp_conf[0];
3323 }
3324 if (tmp_bss == NULL || tmp_conf == NULL) {
3325 hostapd_config_free(conf);
3326 return NULL;
3327 }
3328 bss = iface->conf->bss[iface->conf->num_bss] = conf->bss[0];
3329 iface->conf->num_bss++;
3330
3331 hapd = hostapd_alloc_bss_data(iface, iface->conf, bss);
3332 if (hapd == NULL) {
3333 iface->conf->num_bss--;
3334 hostapd_config_free(conf);
3335 return NULL;
3336 }
3337 iface->conf->last_bss = bss;
3338 iface->bss[iface->num_bss] = hapd;
3339 hapd->msg_ctx = hapd;
Sunil Ravi99c035e2024-07-12 01:42:03 +00003340 hostapd_bss_setup_multi_link(hapd, interfaces);
3341
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003342
3343 bss_idx = iface->num_bss++;
3344 conf->num_bss--;
3345 conf->bss[0] = NULL;
3346 hostapd_config_free(conf);
3347 } else {
3348 /* Add a new iface with the first BSS */
3349 new_iface = iface = hostapd_init(interfaces, config_fname);
3350 if (!iface)
3351 return NULL;
3352 os_strlcpy(iface->phy, phy, sizeof(iface->phy));
3353 iface->interfaces = interfaces;
3354 bss_idx = 0;
3355 }
3356
3357 for (k = 0; k < debug; k++) {
3358 if (iface->bss[bss_idx]->conf->logger_stdout_level > 0)
3359 iface->bss[bss_idx]->conf->logger_stdout_level--;
3360 }
3361
3362 if (iface->conf->bss[bss_idx]->iface[0] == '\0' &&
3363 !hostapd_drv_none(iface->bss[bss_idx])) {
3364 wpa_printf(MSG_ERROR, "Interface name not specified in %s",
3365 config_fname);
3366 if (new_iface)
3367 hostapd_interface_deinit_free(new_iface);
3368 return NULL;
3369 }
3370
3371 return iface;
3372}
3373
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003374
Sunil Ravi99c035e2024-07-12 01:42:03 +00003375static void hostapd_cleanup_driver(const struct wpa_driver_ops *driver,
3376 void *drv_priv, struct hostapd_iface *iface)
3377{
Sunil Ravi99c035e2024-07-12 01:42:03 +00003378 if (!driver || !driver->hapd_deinit || !drv_priv)
3379 return;
3380
Sunil Ravi7f769292024-07-23 22:21:32 +00003381#ifdef CONFIG_IEEE80211BE
Sunil Ravi99c035e2024-07-12 01:42:03 +00003382 /* In case of non-ML operation, de-init. But if ML operation exist,
3383 * even if that's the last BSS in the interface, the driver (drv) could
3384 * be in use for a different AP MLD. Hence, need to check if drv is
3385 * still being used by some other BSS before de-initiallizing. */
3386 if (!iface->bss[0]->conf->mld_ap) {
3387 driver->hapd_deinit(drv_priv);
3388 } else if (hostapd_mld_is_first_bss(iface->bss[0]) &&
3389 driver->is_drv_shared &&
3390 !driver->is_drv_shared(drv_priv, iface->bss[0])) {
3391 driver->hapd_deinit(drv_priv);
3392 } else if (hostapd_if_link_remove(iface->bss[0],
3393 WPA_IF_AP_BSS,
3394 iface->bss[0]->conf->iface,
3395 iface->bss[0]->mld_link_id)) {
3396 wpa_printf(MSG_WARNING, "Failed to remove BSS interface %s",
3397 iface->bss[0]->conf->iface);
3398 }
3399#else /* CONFIG_IEEE80211BE */
3400 driver->hapd_deinit(drv_priv);
3401#endif /* CONFIG_IEEE80211BE */
3402 iface->bss[0]->drv_priv = NULL;
3403}
3404
3405
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003406void hostapd_interface_deinit_free(struct hostapd_iface *iface)
3407{
3408 const struct wpa_driver_ops *driver;
3409 void *drv_priv;
Dmitry Shmidt54605472013-11-08 11:10:19 -08003410
3411 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003412 if (iface == NULL)
3413 return;
Dmitry Shmidt54605472013-11-08 11:10:19 -08003414 wpa_printf(MSG_DEBUG, "%s: num_bss=%u conf->num_bss=%u",
3415 __func__, (unsigned int) iface->num_bss,
3416 (unsigned int) iface->conf->num_bss);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003417 driver = iface->bss[0]->driver;
3418 drv_priv = iface->bss[0]->drv_priv;
3419 hostapd_interface_deinit(iface);
Dmitry Shmidt54605472013-11-08 11:10:19 -08003420 wpa_printf(MSG_DEBUG, "%s: driver=%p drv_priv=%p -> hapd_deinit",
3421 __func__, driver, drv_priv);
Sunil Ravi99c035e2024-07-12 01:42:03 +00003422 hostapd_cleanup_driver(driver, drv_priv, iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003423 hostapd_interface_free(iface);
3424}
3425
3426
Dmitry Shmidt15907092014-03-25 10:42:57 -07003427static void hostapd_deinit_driver(const struct wpa_driver_ops *driver,
3428 void *drv_priv,
3429 struct hostapd_iface *hapd_iface)
3430{
3431 size_t j;
3432
3433 wpa_printf(MSG_DEBUG, "%s: driver=%p drv_priv=%p -> hapd_deinit",
3434 __func__, driver, drv_priv);
Sunil Ravi99c035e2024-07-12 01:42:03 +00003435
3436 hostapd_cleanup_driver(driver, drv_priv, hapd_iface);
3437
Dmitry Shmidt15907092014-03-25 10:42:57 -07003438 if (driver && driver->hapd_deinit && drv_priv) {
Dmitry Shmidt15907092014-03-25 10:42:57 -07003439 for (j = 0; j < hapd_iface->num_bss; j++) {
3440 wpa_printf(MSG_DEBUG, "%s:bss[%d]->drv_priv=%p",
3441 __func__, (int) j,
3442 hapd_iface->bss[j]->drv_priv);
Hai Shalom1dc4d202019-04-29 16:22:27 -07003443 if (hapd_iface->bss[j]->drv_priv == drv_priv) {
Sunil Ravi99c035e2024-07-12 01:42:03 +00003444 hapd_iface->bss[j]->drv_priv = NULL;
Hai Shalom1dc4d202019-04-29 16:22:27 -07003445 hapd_iface->extended_capa = NULL;
3446 hapd_iface->extended_capa_mask = NULL;
3447 hapd_iface->extended_capa_len = 0;
3448 }
Dmitry Shmidt15907092014-03-25 10:42:57 -07003449 }
3450 }
3451}
3452
3453
Sunil Ravi99c035e2024-07-12 01:42:03 +00003454static void hostapd_refresh_all_iface_beacons(struct hostapd_iface *hapd_iface)
3455{
3456 size_t j;
3457
3458 if (!hapd_iface->interfaces || hapd_iface->interfaces->count <= 1)
3459 return;
3460
3461 for (j = 0; j < hapd_iface->interfaces->count; j++) {
3462 if (hapd_iface->interfaces->iface[j] == hapd_iface)
3463 continue;
3464
3465 ieee802_11_update_beacons(hapd_iface->interfaces->iface[j]);
3466 }
3467}
3468
3469
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003470int hostapd_enable_iface(struct hostapd_iface *hapd_iface)
3471{
Dmitry Shmidt71757432014-06-02 13:50:35 -07003472 size_t j;
3473
Hai Shalom60840252021-02-19 19:02:11 -08003474 if (!hapd_iface)
3475 return -1;
3476
3477 if (hapd_iface->enable_iface_cb)
3478 return hapd_iface->enable_iface_cb(hapd_iface);
3479
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003480 if (hapd_iface->bss[0]->drv_priv != NULL) {
3481 wpa_printf(MSG_ERROR, "Interface %s already enabled",
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003482 hapd_iface->conf->bss[0]->iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003483 return -1;
3484 }
3485
3486 wpa_printf(MSG_DEBUG, "Enable interface %s",
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003487 hapd_iface->conf->bss[0]->iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003488
Dmitry Shmidt71757432014-06-02 13:50:35 -07003489 for (j = 0; j < hapd_iface->num_bss; j++)
3490 hostapd_set_security_params(hapd_iface->conf->bss[j], 1);
Dmitry Shmidt344abd32014-01-14 13:17:00 -08003491 if (hostapd_config_check(hapd_iface->conf, 1) < 0) {
3492 wpa_printf(MSG_INFO, "Invalid configuration - cannot enable");
3493 return -1;
3494 }
3495
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003496 if (hapd_iface->interfaces == NULL ||
3497 hapd_iface->interfaces->driver_init == NULL ||
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003498 hapd_iface->interfaces->driver_init(hapd_iface))
3499 return -1;
3500
3501 if (hostapd_setup_interface(hapd_iface)) {
Dmitry Shmidt15907092014-03-25 10:42:57 -07003502 hostapd_deinit_driver(hapd_iface->bss[0]->driver,
3503 hapd_iface->bss[0]->drv_priv,
3504 hapd_iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003505 return -1;
3506 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003507
Sunil Ravi99c035e2024-07-12 01:42:03 +00003508 hostapd_refresh_all_iface_beacons(hapd_iface);
3509
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003510 return 0;
3511}
3512
3513
3514int hostapd_reload_iface(struct hostapd_iface *hapd_iface)
3515{
3516 size_t j;
3517
3518 wpa_printf(MSG_DEBUG, "Reload interface %s",
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003519 hapd_iface->conf->bss[0]->iface);
3520 for (j = 0; j < hapd_iface->num_bss; j++)
Dmitry Shmidt71757432014-06-02 13:50:35 -07003521 hostapd_set_security_params(hapd_iface->conf->bss[j], 1);
Dmitry Shmidt344abd32014-01-14 13:17:00 -08003522 if (hostapd_config_check(hapd_iface->conf, 1) < 0) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003523 wpa_printf(MSG_ERROR, "Updated configuration is invalid");
3524 return -1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003525 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003526 hostapd_clear_old(hapd_iface);
3527 for (j = 0; j < hapd_iface->num_bss; j++)
3528 hostapd_reload_bss(hapd_iface->bss[j]);
3529
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003530 return 0;
3531}
3532
3533
Sunil Ravi77d572f2023-01-17 23:58:31 +00003534int hostapd_reload_bss_only(struct hostapd_data *bss)
3535{
3536
3537 wpa_printf(MSG_DEBUG, "Reload BSS %s", bss->conf->iface);
3538 hostapd_set_security_params(bss->conf, 1);
3539 if (hostapd_config_check(bss->iconf, 1) < 0) {
3540 wpa_printf(MSG_ERROR, "Updated BSS configuration is invalid");
3541 return -1;
3542 }
3543 hostapd_clear_old_bss(bss);
3544 hostapd_reload_bss(bss);
3545 return 0;
3546}
3547
3548
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003549int hostapd_disable_iface(struct hostapd_iface *hapd_iface)
3550{
3551 size_t j;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003552 const struct wpa_driver_ops *driver;
3553 void *drv_priv;
3554
3555 if (hapd_iface == NULL)
3556 return -1;
Dmitry Shmidt71757432014-06-02 13:50:35 -07003557
Hai Shalom60840252021-02-19 19:02:11 -08003558 if (hapd_iface->disable_iface_cb)
3559 return hapd_iface->disable_iface_cb(hapd_iface);
3560
Dmitry Shmidt71757432014-06-02 13:50:35 -07003561 if (hapd_iface->bss[0]->drv_priv == NULL) {
3562 wpa_printf(MSG_INFO, "Interface %s already disabled",
3563 hapd_iface->conf->bss[0]->iface);
3564 return -1;
3565 }
3566
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003567 wpa_msg(hapd_iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003568 driver = hapd_iface->bss[0]->driver;
3569 drv_priv = hapd_iface->bss[0]->drv_priv;
3570
Dmitry Shmidta38abf92014-03-06 13:38:44 -08003571 hapd_iface->driver_ap_teardown =
3572 !!(hapd_iface->drv_flags &
3573 WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
3574
Hai Shalom39ba6fc2019-01-22 12:40:38 -08003575#ifdef NEED_AP_MLME
3576 for (j = 0; j < hapd_iface->num_bss; j++)
3577 hostapd_cleanup_cs_params(hapd_iface->bss[j]);
3578#endif /* NEED_AP_MLME */
3579
Dmitry Shmidta38abf92014-03-06 13:38:44 -08003580 /* same as hostapd_interface_deinit without deinitializing ctrl-iface */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003581 for (j = 0; j < hapd_iface->num_bss; j++) {
3582 struct hostapd_data *hapd = hapd_iface->bss[j];
Dmitry Shmidt71757432014-06-02 13:50:35 -07003583 hostapd_bss_deinit_no_free(hapd);
Sunil Ravi99c035e2024-07-12 01:42:03 +00003584 hostapd_bss_link_deinit(hapd);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003585 hostapd_free_hapd_data(hapd);
3586 }
3587
Dmitry Shmidt15907092014-03-25 10:42:57 -07003588 hostapd_deinit_driver(driver, drv_priv, hapd_iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003589
3590 /* From hostapd_cleanup_iface: These were initialized in
3591 * hostapd_setup_interface and hostapd_setup_interface_complete
3592 */
3593 hostapd_cleanup_iface_partial(hapd_iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003594
Dmitry Shmidt56052862013-10-04 10:23:25 -07003595 wpa_printf(MSG_DEBUG, "Interface %s disabled",
3596 hapd_iface->bss[0]->conf->iface);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003597 hostapd_set_state(hapd_iface, HAPD_IFACE_DISABLED);
Sunil Ravi99c035e2024-07-12 01:42:03 +00003598 hostapd_refresh_all_iface_beacons(hapd_iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003599 return 0;
3600}
3601
3602
3603static struct hostapd_iface *
3604hostapd_iface_alloc(struct hapd_interfaces *interfaces)
3605{
3606 struct hostapd_iface **iface, *hapd_iface;
3607
3608 iface = os_realloc_array(interfaces->iface, interfaces->count + 1,
3609 sizeof(struct hostapd_iface *));
3610 if (iface == NULL)
3611 return NULL;
3612 interfaces->iface = iface;
3613 hapd_iface = interfaces->iface[interfaces->count] =
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07003614 hostapd_alloc_iface();
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003615 if (hapd_iface == NULL) {
3616 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory for "
3617 "the interface", __func__);
3618 return NULL;
3619 }
3620 interfaces->count++;
3621 hapd_iface->interfaces = interfaces;
3622
3623 return hapd_iface;
3624}
3625
3626
3627static struct hostapd_config *
3628hostapd_config_alloc(struct hapd_interfaces *interfaces, const char *ifname,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003629 const char *ctrl_iface, const char *driver)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003630{
3631 struct hostapd_bss_config *bss;
3632 struct hostapd_config *conf;
3633
3634 /* Allocates memory for bss and conf */
3635 conf = hostapd_config_defaults();
3636 if (conf == NULL) {
3637 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory for "
3638 "configuration", __func__);
Hai Shalom74f70d42019-02-11 14:42:39 -08003639 return NULL;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003640 }
3641
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003642 if (driver) {
3643 int j;
3644
3645 for (j = 0; wpa_drivers[j]; j++) {
3646 if (os_strcmp(driver, wpa_drivers[j]->name) == 0) {
3647 conf->driver = wpa_drivers[j];
3648 goto skip;
3649 }
3650 }
3651
3652 wpa_printf(MSG_ERROR,
3653 "Invalid/unknown driver '%s' - registering the default driver",
3654 driver);
3655 }
3656
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003657 conf->driver = wpa_drivers[0];
3658 if (conf->driver == NULL) {
3659 wpa_printf(MSG_ERROR, "No driver wrappers registered!");
3660 hostapd_config_free(conf);
3661 return NULL;
3662 }
3663
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003664skip:
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003665 bss = conf->last_bss = conf->bss[0];
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003666
3667 os_strlcpy(bss->iface, ifname, sizeof(bss->iface));
3668 bss->ctrl_interface = os_strdup(ctrl_iface);
3669 if (bss->ctrl_interface == NULL) {
3670 hostapd_config_free(conf);
3671 return NULL;
3672 }
3673
3674 /* Reading configuration file skipped, will be done in SET!
3675 * From reading the configuration till the end has to be done in
3676 * SET
3677 */
3678 return conf;
3679}
3680
3681
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003682static int hostapd_data_alloc(struct hostapd_iface *hapd_iface,
3683 struct hostapd_config *conf)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003684{
3685 size_t i;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003686 struct hostapd_data *hapd;
3687
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003688 hapd_iface->bss = os_calloc(conf->num_bss,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003689 sizeof(struct hostapd_data *));
3690 if (hapd_iface->bss == NULL)
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003691 return -1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003692
3693 for (i = 0; i < conf->num_bss; i++) {
3694 hapd = hapd_iface->bss[i] =
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003695 hostapd_alloc_bss_data(hapd_iface, conf, conf->bss[i]);
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003696 if (hapd == NULL) {
3697 while (i > 0) {
3698 i--;
3699 os_free(hapd_iface->bss[i]);
3700 hapd_iface->bss[i] = NULL;
3701 }
3702 os_free(hapd_iface->bss);
3703 hapd_iface->bss = NULL;
3704 return -1;
3705 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003706 hapd->msg_ctx = hapd;
Sunil Ravi99c035e2024-07-12 01:42:03 +00003707 hostapd_bss_setup_multi_link(hapd, hapd_iface->interfaces);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003708 }
3709
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003710 hapd_iface->conf = conf;
3711 hapd_iface->num_bss = conf->num_bss;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003712
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003713 return 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003714}
3715
3716
3717int hostapd_add_iface(struct hapd_interfaces *interfaces, char *buf)
3718{
3719 struct hostapd_config *conf = NULL;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003720 struct hostapd_iface *hapd_iface = NULL, *new_iface = NULL;
3721 struct hostapd_data *hapd;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003722 char *ptr;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003723 size_t i, j;
3724 const char *conf_file = NULL, *phy_name = NULL;
3725
3726 if (os_strncmp(buf, "bss_config=", 11) == 0) {
3727 char *pos;
3728 phy_name = buf + 11;
3729 pos = os_strchr(phy_name, ':');
3730 if (!pos)
3731 return -1;
3732 *pos++ = '\0';
3733 conf_file = pos;
3734 if (!os_strlen(conf_file))
3735 return -1;
3736
3737 hapd_iface = hostapd_interface_init_bss(interfaces, phy_name,
3738 conf_file, 0);
3739 if (!hapd_iface)
3740 return -1;
3741 for (j = 0; j < interfaces->count; j++) {
3742 if (interfaces->iface[j] == hapd_iface)
3743 break;
3744 }
3745 if (j == interfaces->count) {
3746 struct hostapd_iface **tmp;
3747 tmp = os_realloc_array(interfaces->iface,
3748 interfaces->count + 1,
3749 sizeof(struct hostapd_iface *));
3750 if (!tmp) {
3751 hostapd_interface_deinit_free(hapd_iface);
3752 return -1;
3753 }
3754 interfaces->iface = tmp;
3755 interfaces->iface[interfaces->count++] = hapd_iface;
3756 new_iface = hapd_iface;
3757 }
3758
3759 if (new_iface) {
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003760 if (interfaces->driver_init(hapd_iface))
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003761 goto fail;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003762
3763 if (hostapd_setup_interface(hapd_iface)) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003764 hostapd_deinit_driver(
3765 hapd_iface->bss[0]->driver,
3766 hapd_iface->bss[0]->drv_priv,
3767 hapd_iface);
3768 goto fail;
3769 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003770 } else {
3771 /* Assign new BSS with bss[0]'s driver info */
3772 hapd = hapd_iface->bss[hapd_iface->num_bss - 1];
3773 hapd->driver = hapd_iface->bss[0]->driver;
3774 hapd->drv_priv = hapd_iface->bss[0]->drv_priv;
3775 os_memcpy(hapd->own_addr, hapd_iface->bss[0]->own_addr,
3776 ETH_ALEN);
3777
3778 if (start_ctrl_iface_bss(hapd) < 0 ||
Dmitry Shmidt54605472013-11-08 11:10:19 -08003779 (hapd_iface->state == HAPD_IFACE_ENABLED &&
Sunil Ravi77d572f2023-01-17 23:58:31 +00003780 hostapd_setup_bss(hapd, -1, true))) {
Sunil Ravi99c035e2024-07-12 01:42:03 +00003781 hostapd_bss_link_deinit(hapd);
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07003782 hostapd_cleanup(hapd);
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08003783 hapd_iface->bss[hapd_iface->num_bss - 1] = NULL;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003784 hapd_iface->conf->num_bss--;
3785 hapd_iface->num_bss--;
Dmitry Shmidt54605472013-11-08 11:10:19 -08003786 wpa_printf(MSG_DEBUG, "%s: free hapd %p %s",
3787 __func__, hapd, hapd->conf->iface);
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003788 hostapd_config_free_bss(hapd->conf);
3789 hapd->conf = NULL;
Sunil Ravi99c035e2024-07-12 01:42:03 +00003790#ifdef CONFIG_IEEE80211BE
3791 hostapd_mld_ref_dec(hapd->mld);
3792#endif /* CONFIG_IEEE80211BE */
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003793 os_free(hapd);
3794 return -1;
3795 }
3796 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003797 hostapd_owe_update_trans(hapd_iface);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003798 return 0;
3799 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003800
3801 ptr = os_strchr(buf, ' ');
3802 if (ptr == NULL)
3803 return -1;
3804 *ptr++ = '\0';
3805
Dmitry Shmidt56052862013-10-04 10:23:25 -07003806 if (os_strncmp(ptr, "config=", 7) == 0)
3807 conf_file = ptr + 7;
3808
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003809 for (i = 0; i < interfaces->count; i++) {
Sunil Ravi2a14cf12023-11-21 00:54:38 +00003810 bool mld_ap = false;
3811
3812#ifdef CONFIG_IEEE80211BE
3813 mld_ap = interfaces->iface[i]->conf->bss[0]->mld_ap;
3814#endif /* CONFIG_IEEE80211BE */
3815
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003816 if (!os_strcmp(interfaces->iface[i]->conf->bss[0]->iface,
Sunil Ravi2a14cf12023-11-21 00:54:38 +00003817 buf) && !mld_ap) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003818 wpa_printf(MSG_INFO, "Cannot add interface - it "
3819 "already exists");
3820 return -1;
3821 }
3822 }
3823
3824 hapd_iface = hostapd_iface_alloc(interfaces);
3825 if (hapd_iface == NULL) {
3826 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
3827 "for interface", __func__);
3828 goto fail;
3829 }
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003830 new_iface = hapd_iface;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003831
Dmitry Shmidt56052862013-10-04 10:23:25 -07003832 if (conf_file && interfaces->config_read_cb) {
3833 conf = interfaces->config_read_cb(conf_file);
3834 if (conf && conf->bss)
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003835 os_strlcpy(conf->bss[0]->iface, buf,
3836 sizeof(conf->bss[0]->iface));
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003837 } else {
3838 char *driver = os_strchr(ptr, ' ');
3839
3840 if (driver)
3841 *driver++ = '\0';
3842 conf = hostapd_config_alloc(interfaces, buf, ptr, driver);
3843 }
3844
Dmitry Shmidt56052862013-10-04 10:23:25 -07003845 if (conf == NULL || conf->bss == NULL) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003846 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
3847 "for configuration", __func__);
3848 goto fail;
3849 }
3850
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003851 if (hostapd_data_alloc(hapd_iface, conf) < 0) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003852 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
3853 "for hostapd", __func__);
3854 goto fail;
3855 }
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003856 conf = NULL;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003857
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003858 if (start_ctrl_iface(hapd_iface) < 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003859 goto fail;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003860
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003861 wpa_printf(MSG_INFO, "Add interface '%s'",
3862 hapd_iface->conf->bss[0]->iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003863
3864 return 0;
3865
3866fail:
3867 if (conf)
3868 hostapd_config_free(conf);
3869 if (hapd_iface) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003870 if (hapd_iface->bss) {
Dmitry Shmidt54605472013-11-08 11:10:19 -08003871 for (i = 0; i < hapd_iface->num_bss; i++) {
3872 hapd = hapd_iface->bss[i];
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08003873 if (!hapd)
3874 continue;
3875 if (hapd_iface->interfaces &&
Dmitry Shmidt54605472013-11-08 11:10:19 -08003876 hapd_iface->interfaces->ctrl_iface_deinit)
3877 hapd_iface->interfaces->
3878 ctrl_iface_deinit(hapd);
3879 wpa_printf(MSG_DEBUG, "%s: free hapd %p (%s)",
3880 __func__, hapd_iface->bss[i],
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08003881 hapd->conf->iface);
Sunil Ravi99c035e2024-07-12 01:42:03 +00003882 hostapd_bss_link_deinit(hapd);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003883 hostapd_cleanup(hapd);
Sunil Ravi99c035e2024-07-12 01:42:03 +00003884#ifdef CONFIG_IEEE80211BE
3885 hostapd_mld_ref_dec(hapd->mld);
3886#endif /* CONFIG_IEEE80211BE */
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08003887 os_free(hapd);
3888 hapd_iface->bss[i] = NULL;
Dmitry Shmidt54605472013-11-08 11:10:19 -08003889 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003890 os_free(hapd_iface->bss);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003891 hapd_iface->bss = NULL;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003892 }
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003893 if (new_iface) {
3894 interfaces->count--;
3895 interfaces->iface[interfaces->count] = NULL;
Sunil Ravi99c035e2024-07-12 01:42:03 +00003896 hostapd_cleanup_unused_mlds(interfaces);
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003897 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003898 hostapd_cleanup_iface(hapd_iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003899 }
3900 return -1;
3901}
3902
3903
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003904static int hostapd_remove_bss(struct hostapd_iface *iface, unsigned int idx)
3905{
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003906 size_t i;
3907
Dmitry Shmidt54605472013-11-08 11:10:19 -08003908 wpa_printf(MSG_INFO, "Remove BSS '%s'", iface->conf->bss[idx]->iface);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003909
Dmitry Shmidt54605472013-11-08 11:10:19 -08003910 /* Remove hostapd_data only if it has already been initialized */
3911 if (idx < iface->num_bss) {
3912 struct hostapd_data *hapd = iface->bss[idx];
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003913
Dmitry Shmidt54605472013-11-08 11:10:19 -08003914 hostapd_bss_deinit(hapd);
3915 wpa_printf(MSG_DEBUG, "%s: free hapd %p (%s)",
3916 __func__, hapd, hapd->conf->iface);
3917 hostapd_config_free_bss(hapd->conf);
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003918 hapd->conf = NULL;
Sunil Ravi99c035e2024-07-12 01:42:03 +00003919#ifdef CONFIG_IEEE80211BE
3920 hostapd_mld_ref_dec(hapd->mld);
3921#endif /* CONFIG_IEEE80211BE */
Dmitry Shmidt54605472013-11-08 11:10:19 -08003922 os_free(hapd);
3923
3924 iface->num_bss--;
3925
3926 for (i = idx; i < iface->num_bss; i++)
3927 iface->bss[i] = iface->bss[i + 1];
3928 } else {
3929 hostapd_config_free_bss(iface->conf->bss[idx]);
3930 iface->conf->bss[idx] = NULL;
3931 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003932
3933 iface->conf->num_bss--;
Dmitry Shmidt54605472013-11-08 11:10:19 -08003934 for (i = idx; i < iface->conf->num_bss; i++)
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003935 iface->conf->bss[i] = iface->conf->bss[i + 1];
3936
3937 return 0;
3938}
3939
3940
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003941int hostapd_remove_iface(struct hapd_interfaces *interfaces, char *buf)
3942{
3943 struct hostapd_iface *hapd_iface;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003944 size_t i, j, k = 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003945
3946 for (i = 0; i < interfaces->count; i++) {
3947 hapd_iface = interfaces->iface[i];
3948 if (hapd_iface == NULL)
3949 return -1;
Dmitry Shmidt54605472013-11-08 11:10:19 -08003950 if (!os_strcmp(hapd_iface->conf->bss[0]->iface, buf)) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003951 wpa_printf(MSG_INFO, "Remove interface '%s'", buf);
Dmitry Shmidta38abf92014-03-06 13:38:44 -08003952 hapd_iface->driver_ap_teardown =
3953 !!(hapd_iface->drv_flags &
3954 WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
3955
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003956 hostapd_interface_deinit_free(hapd_iface);
3957 k = i;
3958 while (k < (interfaces->count - 1)) {
3959 interfaces->iface[k] =
3960 interfaces->iface[k + 1];
3961 k++;
3962 }
3963 interfaces->count--;
Sunil Ravi99c035e2024-07-12 01:42:03 +00003964 hostapd_cleanup_unused_mlds(interfaces);
3965
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003966 return 0;
3967 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003968
3969 for (j = 0; j < hapd_iface->conf->num_bss; j++) {
Dmitry Shmidta38abf92014-03-06 13:38:44 -08003970 if (!os_strcmp(hapd_iface->conf->bss[j]->iface, buf)) {
3971 hapd_iface->driver_ap_teardown =
3972 !(hapd_iface->drv_flags &
3973 WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003974 return hostapd_remove_bss(hapd_iface, j);
Dmitry Shmidta38abf92014-03-06 13:38:44 -08003975 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003976 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003977 }
3978 return -1;
3979}
3980
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003981
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003982/**
3983 * hostapd_new_assoc_sta - Notify that a new station associated with the AP
3984 * @hapd: Pointer to BSS data
3985 * @sta: Pointer to the associated STA data
3986 * @reassoc: 1 to indicate this was a re-association; 0 = first association
3987 *
3988 * This function will be called whenever a station associates with the AP. It
3989 * can be called from ieee802_11.c for drivers that export MLME to hostapd and
3990 * from drv_callbacks.c based on driver events for drivers that take care of
3991 * management frames (IEEE 802.11 authentication and association) internally.
3992 */
3993void hostapd_new_assoc_sta(struct hostapd_data *hapd, struct sta_info *sta,
3994 int reassoc)
3995{
3996 if (hapd->tkip_countermeasures) {
3997 hostapd_drv_sta_deauth(hapd, sta->addr,
3998 WLAN_REASON_MICHAEL_MIC_FAILURE);
3999 return;
4000 }
4001
Sunil Ravi2a14cf12023-11-21 00:54:38 +00004002#ifdef CONFIG_IEEE80211BE
Sunil Ravib0ac25f2024-07-12 01:42:03 +00004003 if (ap_sta_is_mld(hapd, sta) &&
Sunil Ravi2a14cf12023-11-21 00:54:38 +00004004 sta->mld_assoc_link_id != hapd->mld_link_id)
4005 return;
4006#endif /* CONFIG_IEEE80211BE */
4007
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -08004008 ap_sta_clear_disconnect_timeouts(hapd, sta);
Hai Shalom899fcc72020-10-19 14:38:18 -07004009 sta->post_csa_sa_query = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004010
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004011#ifdef CONFIG_P2P
4012 if (sta->p2p_ie == NULL && !sta->no_p2p_set) {
4013 sta->no_p2p_set = 1;
4014 hapd->num_sta_no_p2p++;
4015 if (hapd->num_sta_no_p2p == 1)
4016 hostapd_p2p_non_p2p_sta_connected(hapd);
4017 }
4018#endif /* CONFIG_P2P */
4019
Hai Shalom81f62d82019-07-22 12:10:00 -07004020 airtime_policy_new_sta(hapd, sta);
4021
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004022 /* Start accounting here, if IEEE 802.1X and WPA are not used.
4023 * IEEE 802.1X/WPA code will start accounting after the station has
4024 * been authorized. */
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -08004025 if (!hapd->conf->ieee802_1x && !hapd->conf->wpa && !hapd->conf->osen) {
4026 ap_sta_set_authorized(hapd, sta, 1);
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08004027 os_get_reltime(&sta->connected_time);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004028 accounting_sta_start(hapd, sta);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004029 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004030
4031 /* Start IEEE 802.1X authentication process for new stations */
4032 ieee802_1x_new_station(hapd, sta);
4033 if (reassoc) {
4034 if (sta->auth_alg != WLAN_AUTH_FT &&
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004035 sta->auth_alg != WLAN_AUTH_FILS_SK &&
4036 sta->auth_alg != WLAN_AUTH_FILS_SK_PFS &&
4037 sta->auth_alg != WLAN_AUTH_FILS_PK &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004038 !(sta->flags & (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS)))
4039 wpa_auth_sm_event(sta->wpa_sm, WPA_REAUTH);
Sunil Ravi2a14cf12023-11-21 00:54:38 +00004040 } else if (!(hapd->iface->drv_flags2 &
4041 WPA_DRIVER_FLAGS2_4WAY_HANDSHAKE_AP_PSK)) {
4042 /* The 4-way handshake offloaded case will have this handled
4043 * based on the port authorized event. */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004044 wpa_auth_sta_associated(hapd->wpa_auth, sta->wpa_sm);
Sunil Ravi2a14cf12023-11-21 00:54:38 +00004045 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07004046
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08004047 if (hapd->iface->drv_flags & WPA_DRIVER_FLAGS_WIRED) {
4048 if (eloop_cancel_timeout(ap_handle_timer, hapd, sta) > 0) {
4049 wpa_printf(MSG_DEBUG,
4050 "%s: %s: canceled wired ap_handle_timer timeout for "
4051 MACSTR,
4052 hapd->conf->iface, __func__,
4053 MAC2STR(sta->addr));
4054 }
4055 } else if (!(hapd->iface->drv_flags &
4056 WPA_DRIVER_FLAGS_INACTIVITY_TIMER)) {
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -08004057 wpa_printf(MSG_DEBUG,
4058 "%s: %s: reschedule ap_handle_timer timeout for "
4059 MACSTR " (%d seconds - ap_max_inactivity)",
4060 hapd->conf->iface, __func__, MAC2STR(sta->addr),
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08004061 hapd->conf->ap_max_inactivity);
4062 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
4063 eloop_register_timeout(hapd->conf->ap_max_inactivity, 0,
4064 ap_handle_timer, hapd, sta);
4065 }
Hai Shalom81f62d82019-07-22 12:10:00 -07004066
4067#ifdef CONFIG_MACSEC
4068 if (hapd->conf->wpa_key_mgmt == WPA_KEY_MGMT_NONE &&
4069 hapd->conf->mka_psk_set)
4070 ieee802_1x_create_preshared_mka_hapd(hapd, sta);
4071 else
4072 ieee802_1x_alloc_kay_sm_hapd(hapd, sta);
4073#endif /* CONFIG_MACSEC */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004074}
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004075
4076
4077const char * hostapd_state_text(enum hostapd_iface_state s)
4078{
4079 switch (s) {
4080 case HAPD_IFACE_UNINITIALIZED:
4081 return "UNINITIALIZED";
4082 case HAPD_IFACE_DISABLED:
4083 return "DISABLED";
4084 case HAPD_IFACE_COUNTRY_UPDATE:
4085 return "COUNTRY_UPDATE";
4086 case HAPD_IFACE_ACS:
4087 return "ACS";
4088 case HAPD_IFACE_HT_SCAN:
4089 return "HT_SCAN";
4090 case HAPD_IFACE_DFS:
4091 return "DFS";
4092 case HAPD_IFACE_ENABLED:
4093 return "ENABLED";
Sunil Ravi2a14cf12023-11-21 00:54:38 +00004094 case HAPD_IFACE_NO_IR:
4095 return "NO_IR";
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004096 }
4097
4098 return "UNKNOWN";
4099}
4100
4101
4102void hostapd_set_state(struct hostapd_iface *iface, enum hostapd_iface_state s)
4103{
4104 wpa_printf(MSG_INFO, "%s: interface state %s->%s",
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07004105 iface->conf ? iface->conf->bss[0]->iface : "N/A",
4106 hostapd_state_text(iface->state), hostapd_state_text(s));
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004107 iface->state = s;
4108}
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004109
4110
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004111int hostapd_csa_in_progress(struct hostapd_iface *iface)
4112{
4113 unsigned int i;
4114
4115 for (i = 0; i < iface->num_bss; i++)
4116 if (iface->bss[i]->csa_in_progress)
4117 return 1;
4118 return 0;
4119}
4120
4121
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004122#ifdef NEED_AP_MLME
4123
Sunil Ravi7f769292024-07-23 22:21:32 +00004124void free_beacon_data(struct beacon_data *beacon)
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004125{
4126 os_free(beacon->head);
4127 beacon->head = NULL;
4128 os_free(beacon->tail);
4129 beacon->tail = NULL;
4130 os_free(beacon->probe_resp);
4131 beacon->probe_resp = NULL;
4132 os_free(beacon->beacon_ies);
4133 beacon->beacon_ies = NULL;
4134 os_free(beacon->proberesp_ies);
4135 beacon->proberesp_ies = NULL;
4136 os_free(beacon->assocresp_ies);
4137 beacon->assocresp_ies = NULL;
4138}
4139
4140
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004141static int hostapd_build_beacon_data(struct hostapd_data *hapd,
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004142 struct beacon_data *beacon)
4143{
4144 struct wpabuf *beacon_extra, *proberesp_extra, *assocresp_extra;
4145 struct wpa_driver_ap_params params;
4146 int ret;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004147
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08004148 os_memset(beacon, 0, sizeof(*beacon));
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004149 ret = ieee802_11_build_ap_params(hapd, &params);
4150 if (ret < 0)
4151 return ret;
4152
4153 ret = hostapd_build_ap_extra_ies(hapd, &beacon_extra,
4154 &proberesp_extra,
4155 &assocresp_extra);
4156 if (ret)
4157 goto free_ap_params;
4158
4159 ret = -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004160 beacon->head = os_memdup(params.head, params.head_len);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004161 if (!beacon->head)
4162 goto free_ap_extra_ies;
4163
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004164 beacon->head_len = params.head_len;
4165
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004166 beacon->tail = os_memdup(params.tail, params.tail_len);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004167 if (!beacon->tail)
4168 goto free_beacon;
4169
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004170 beacon->tail_len = params.tail_len;
4171
4172 if (params.proberesp != NULL) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004173 beacon->probe_resp = os_memdup(params.proberesp,
4174 params.proberesp_len);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004175 if (!beacon->probe_resp)
4176 goto free_beacon;
4177
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004178 beacon->probe_resp_len = params.proberesp_len;
4179 }
4180
4181 /* copy the extra ies */
4182 if (beacon_extra) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004183 beacon->beacon_ies = os_memdup(beacon_extra->buf,
4184 wpabuf_len(beacon_extra));
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004185 if (!beacon->beacon_ies)
4186 goto free_beacon;
4187
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004188 beacon->beacon_ies_len = wpabuf_len(beacon_extra);
4189 }
4190
4191 if (proberesp_extra) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004192 beacon->proberesp_ies = os_memdup(proberesp_extra->buf,
4193 wpabuf_len(proberesp_extra));
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004194 if (!beacon->proberesp_ies)
4195 goto free_beacon;
4196
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004197 beacon->proberesp_ies_len = wpabuf_len(proberesp_extra);
4198 }
4199
4200 if (assocresp_extra) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004201 beacon->assocresp_ies = os_memdup(assocresp_extra->buf,
4202 wpabuf_len(assocresp_extra));
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004203 if (!beacon->assocresp_ies)
4204 goto free_beacon;
4205
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004206 beacon->assocresp_ies_len = wpabuf_len(assocresp_extra);
4207 }
4208
4209 ret = 0;
4210free_beacon:
4211 /* if the function fails, the caller should not free beacon data */
4212 if (ret)
4213 free_beacon_data(beacon);
4214
4215free_ap_extra_ies:
4216 hostapd_free_ap_extra_ies(hapd, beacon_extra, proberesp_extra,
4217 assocresp_extra);
4218free_ap_params:
4219 ieee802_11_free_ap_params(&params);
4220 return ret;
4221}
4222
4223
4224/*
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004225 * TODO: This flow currently supports only changing channel and width within
4226 * the same hw_mode. Any other changes to MAC parameters or provided settings
4227 * are not supported.
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004228 */
4229static int hostapd_change_config_freq(struct hostapd_data *hapd,
4230 struct hostapd_config *conf,
4231 struct hostapd_freq_params *params,
4232 struct hostapd_freq_params *old_params)
4233{
4234 int channel;
Sunil Ravib0ac25f2024-07-12 01:42:03 +00004235 u8 seg0 = 0, seg1 = 0;
Hai Shalom81f62d82019-07-22 12:10:00 -07004236 struct hostapd_hw_modes *mode;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004237
4238 if (!params->channel) {
4239 /* check if the new channel is supported by hw */
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004240 params->channel = hostapd_hw_get_channel(hapd, params->freq);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004241 }
4242
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004243 channel = params->channel;
4244 if (!channel)
4245 return -1;
4246
Sunil Ravi2a14cf12023-11-21 00:54:38 +00004247 hostapd_determine_mode(hapd->iface);
Hai Shalom81f62d82019-07-22 12:10:00 -07004248 mode = hapd->iface->current_mode;
4249
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004250 /* if a pointer to old_params is provided we save previous state */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004251 if (old_params &&
4252 hostapd_set_freq_params(old_params, conf->hw_mode,
4253 hostapd_hw_get_freq(hapd, conf->channel),
Hai Shalomc3565922019-10-28 11:58:20 -07004254 conf->channel, conf->enable_edmg,
4255 conf->edmg_channel, conf->ieee80211n,
Hai Shalom81f62d82019-07-22 12:10:00 -07004256 conf->ieee80211ac, conf->ieee80211ax,
Sunil Ravia04bd252022-05-02 22:54:18 -07004257 conf->ieee80211be, conf->secondary_channel,
Hai Shalom81f62d82019-07-22 12:10:00 -07004258 hostapd_get_oper_chwidth(conf),
4259 hostapd_get_oper_centr_freq_seg0_idx(conf),
4260 hostapd_get_oper_centr_freq_seg1_idx(conf),
4261 conf->vht_capab,
4262 mode ? &mode->he_capab[IEEE80211_MODE_AP] :
Sunil Ravia04bd252022-05-02 22:54:18 -07004263 NULL,
4264 mode ? &mode->eht_capab[IEEE80211_MODE_AP] :
Sunil Ravi99c035e2024-07-12 01:42:03 +00004265 NULL,
4266 hostapd_get_punct_bitmap(hapd)))
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004267 return -1;
4268
4269 switch (params->bandwidth) {
4270 case 0:
4271 case 20:
Hai Shaloma20dcd72022-02-04 13:43:00 -08004272 conf->ht_capab &= ~HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET;
4273 break;
4274 case 40:
4275 case 80:
4276 case 160:
Sunil Ravi640215c2023-06-28 23:08:09 +00004277 case 320:
Hai Shaloma20dcd72022-02-04 13:43:00 -08004278 conf->ht_capab |= HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET;
4279 break;
4280 default:
4281 return -1;
4282 }
4283
4284 switch (params->bandwidth) {
4285 case 0:
4286 case 20:
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004287 case 40:
Sunil8cd6f4d2022-06-28 18:40:46 +00004288 hostapd_set_oper_chwidth(conf, CONF_OPER_CHWIDTH_USE_HT);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004289 break;
4290 case 80:
4291 if (params->center_freq2)
Sunil8cd6f4d2022-06-28 18:40:46 +00004292 hostapd_set_oper_chwidth(conf,
4293 CONF_OPER_CHWIDTH_80P80MHZ);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004294 else
Sunil8cd6f4d2022-06-28 18:40:46 +00004295 hostapd_set_oper_chwidth(conf,
4296 CONF_OPER_CHWIDTH_80MHZ);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004297 break;
4298 case 160:
Sunil8cd6f4d2022-06-28 18:40:46 +00004299 hostapd_set_oper_chwidth(conf, CONF_OPER_CHWIDTH_160MHZ);
4300 break;
4301 case 320:
4302 hostapd_set_oper_chwidth(conf, CONF_OPER_CHWIDTH_320MHZ);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004303 break;
4304 default:
4305 return -1;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004306 }
4307
4308 conf->channel = channel;
4309 conf->ieee80211n = params->ht_enabled;
Hai Shaloma20dcd72022-02-04 13:43:00 -08004310 conf->ieee80211ac = params->vht_enabled;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004311 conf->secondary_channel = params->sec_channel_offset;
Sunil Ravib0ac25f2024-07-12 01:42:03 +00004312 if (params->center_freq1 &&
4313 ieee80211_freq_to_chan(params->center_freq1, &seg0) ==
4314 NUM_HOSTAPD_MODES)
4315 return -1;
4316 if (params->center_freq2 &&
4317 ieee80211_freq_to_chan(params->center_freq2,
4318 &seg1) == NUM_HOSTAPD_MODES)
4319 return -1;
Hai Shalom81f62d82019-07-22 12:10:00 -07004320 hostapd_set_oper_centr_freq_seg0_idx(conf, seg0);
4321 hostapd_set_oper_centr_freq_seg1_idx(conf, seg1);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004322
4323 /* TODO: maybe call here hostapd_config_check here? */
4324
4325 return 0;
4326}
4327
4328
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004329static int hostapd_fill_csa_settings(struct hostapd_data *hapd,
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004330 struct csa_settings *settings)
4331{
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004332 struct hostapd_iface *iface = hapd->iface;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004333 struct hostapd_freq_params old_freq;
4334 int ret;
Sunil Ravi036cec52023-03-29 11:35:17 -07004335#ifdef CONFIG_IEEE80211BE
4336 u16 old_punct_bitmap;
4337#endif /* CONFIG_IEEE80211BE */
Hai Shalom81f62d82019-07-22 12:10:00 -07004338 u8 chan, bandwidth;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004339
4340 os_memset(&old_freq, 0, sizeof(old_freq));
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004341 if (!iface || !iface->freq || hapd->csa_in_progress)
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004342 return -1;
4343
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004344 switch (settings->freq_params.bandwidth) {
4345 case 80:
4346 if (settings->freq_params.center_freq2)
Sunil8cd6f4d2022-06-28 18:40:46 +00004347 bandwidth = CONF_OPER_CHWIDTH_80P80MHZ;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004348 else
Sunil8cd6f4d2022-06-28 18:40:46 +00004349 bandwidth = CONF_OPER_CHWIDTH_80MHZ;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004350 break;
4351 case 160:
Sunil8cd6f4d2022-06-28 18:40:46 +00004352 bandwidth = CONF_OPER_CHWIDTH_160MHZ;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004353 break;
Sunil Ravi640215c2023-06-28 23:08:09 +00004354 case 320:
4355 bandwidth = CONF_OPER_CHWIDTH_320MHZ;
4356 break;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004357 default:
Sunil8cd6f4d2022-06-28 18:40:46 +00004358 bandwidth = CONF_OPER_CHWIDTH_USE_HT;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004359 break;
4360 }
4361
4362 if (ieee80211_freq_to_channel_ext(
4363 settings->freq_params.freq,
4364 settings->freq_params.sec_channel_offset,
Hai Shalom81f62d82019-07-22 12:10:00 -07004365 bandwidth,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004366 &hapd->iface->cs_oper_class,
4367 &chan) == NUM_HOSTAPD_MODES) {
4368 wpa_printf(MSG_DEBUG,
Sunil Ravia04bd252022-05-02 22:54:18 -07004369 "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 -08004370 settings->freq_params.freq,
4371 settings->freq_params.sec_channel_offset,
Hai Shalom81f62d82019-07-22 12:10:00 -07004372 settings->freq_params.vht_enabled,
Sunil Ravia04bd252022-05-02 22:54:18 -07004373 settings->freq_params.he_enabled,
4374 settings->freq_params.eht_enabled);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004375 return -1;
4376 }
4377
4378 settings->freq_params.channel = chan;
4379
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004380 ret = hostapd_change_config_freq(iface->bss[0], iface->conf,
4381 &settings->freq_params,
4382 &old_freq);
4383 if (ret)
4384 return ret;
4385
Sunil Ravi036cec52023-03-29 11:35:17 -07004386#ifdef CONFIG_IEEE80211BE
4387 old_punct_bitmap = iface->conf->punct_bitmap;
4388 iface->conf->punct_bitmap = settings->punct_bitmap;
4389#endif /* CONFIG_IEEE80211BE */
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004390 ret = hostapd_build_beacon_data(hapd, &settings->beacon_after);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004391
4392 /* change back the configuration */
Sunil Ravi036cec52023-03-29 11:35:17 -07004393#ifdef CONFIG_IEEE80211BE
4394 iface->conf->punct_bitmap = old_punct_bitmap;
4395#endif /* CONFIG_IEEE80211BE */
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004396 hostapd_change_config_freq(iface->bss[0], iface->conf,
4397 &old_freq, NULL);
4398
4399 if (ret)
4400 return ret;
4401
4402 /* set channel switch parameters for csa ie */
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004403 hapd->cs_freq_params = settings->freq_params;
4404 hapd->cs_count = settings->cs_count;
4405 hapd->cs_block_tx = settings->block_tx;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004406
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004407 ret = hostapd_build_beacon_data(hapd, &settings->beacon_csa);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004408 if (ret) {
4409 free_beacon_data(&settings->beacon_after);
4410 return ret;
4411 }
4412
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004413 settings->counter_offset_beacon[0] = hapd->cs_c_off_beacon;
4414 settings->counter_offset_presp[0] = hapd->cs_c_off_proberesp;
4415 settings->counter_offset_beacon[1] = hapd->cs_c_off_ecsa_beacon;
4416 settings->counter_offset_presp[1] = hapd->cs_c_off_ecsa_proberesp;
Sunil Ravib0ac25f2024-07-12 01:42:03 +00004417 settings->link_id = -1;
4418#ifdef CONFIG_IEEE80211BE
4419 if (hapd->conf->mld_ap)
4420 settings->link_id = hapd->mld_link_id;
4421#endif /* CONFIG_IEEE80211BE */
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004422
Sunil Ravi7f769292024-07-23 22:21:32 +00004423#ifdef CONFIG_IEEE80211AX
4424 settings->ubpr.unsol_bcast_probe_resp_tmpl =
4425 hostapd_unsol_bcast_probe_resp(hapd, &settings->ubpr);
4426#endif /* CONFIG_IEEE80211AX */
4427
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004428 return 0;
4429}
4430
4431
4432void hostapd_cleanup_cs_params(struct hostapd_data *hapd)
4433{
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004434 os_memset(&hapd->cs_freq_params, 0, sizeof(hapd->cs_freq_params));
4435 hapd->cs_count = 0;
4436 hapd->cs_block_tx = 0;
4437 hapd->cs_c_off_beacon = 0;
4438 hapd->cs_c_off_proberesp = 0;
4439 hapd->csa_in_progress = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004440 hapd->cs_c_off_ecsa_beacon = 0;
4441 hapd->cs_c_off_ecsa_proberesp = 0;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004442}
4443
4444
Hai Shalom60840252021-02-19 19:02:11 -08004445void hostapd_chan_switch_config(struct hostapd_data *hapd,
4446 struct hostapd_freq_params *freq_params)
Roshan Pius3a1667e2018-07-03 15:17:14 -07004447{
Sunil Ravia04bd252022-05-02 22:54:18 -07004448 if (freq_params->eht_enabled)
4449 hapd->iconf->ch_switch_eht_config |= CH_SWITCH_EHT_ENABLED;
4450 else
4451 hapd->iconf->ch_switch_eht_config |= CH_SWITCH_EHT_DISABLED;
4452
Hai Shalom60840252021-02-19 19:02:11 -08004453 if (freq_params->he_enabled)
4454 hapd->iconf->ch_switch_he_config |= CH_SWITCH_HE_ENABLED;
4455 else
4456 hapd->iconf->ch_switch_he_config |= CH_SWITCH_HE_DISABLED;
4457
4458 if (freq_params->vht_enabled)
Roshan Pius3a1667e2018-07-03 15:17:14 -07004459 hapd->iconf->ch_switch_vht_config |= CH_SWITCH_VHT_ENABLED;
4460 else
4461 hapd->iconf->ch_switch_vht_config |= CH_SWITCH_VHT_DISABLED;
4462
4463 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
Hai Shalom60840252021-02-19 19:02:11 -08004464 HOSTAPD_LEVEL_INFO,
Sunil Ravia04bd252022-05-02 22:54:18 -07004465 "CHAN_SWITCH EHT config 0x%x HE config 0x%x VHT config 0x%x",
4466 hapd->iconf->ch_switch_eht_config,
Hai Shalom60840252021-02-19 19:02:11 -08004467 hapd->iconf->ch_switch_he_config,
Roshan Pius3a1667e2018-07-03 15:17:14 -07004468 hapd->iconf->ch_switch_vht_config);
4469}
4470
4471
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004472int hostapd_switch_channel(struct hostapd_data *hapd,
4473 struct csa_settings *settings)
4474{
4475 int ret;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004476
4477 if (!(hapd->iface->drv_flags & WPA_DRIVER_FLAGS_AP_CSA)) {
4478 wpa_printf(MSG_INFO, "CSA is not supported");
4479 return -1;
4480 }
4481
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004482 ret = hostapd_fill_csa_settings(hapd, settings);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004483 if (ret)
4484 return ret;
4485
4486 ret = hostapd_drv_switch_channel(hapd, settings);
4487 free_beacon_data(&settings->beacon_csa);
4488 free_beacon_data(&settings->beacon_after);
Sunil Ravi7f769292024-07-23 22:21:32 +00004489#ifdef CONFIG_IEEE80211AX
4490 os_free(settings->ubpr.unsol_bcast_probe_resp_tmpl);
4491#endif /* CONFIG_IEEE80211AX */
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004492
4493 if (ret) {
4494 /* if we failed, clean cs parameters */
4495 hostapd_cleanup_cs_params(hapd);
4496 return ret;
4497 }
4498
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004499 hapd->csa_in_progress = 1;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004500 return 0;
4501}
4502
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004503
4504void
4505hostapd_switch_channel_fallback(struct hostapd_iface *iface,
4506 const struct hostapd_freq_params *freq_params)
4507{
Sunil Ravi7f769292024-07-23 22:21:32 +00004508 u8 seg0_idx = 0, seg1_idx = 0;
Sunil8cd6f4d2022-06-28 18:40:46 +00004509 enum oper_chan_width bw = CONF_OPER_CHWIDTH_USE_HT;
Sunil Ravi7f769292024-07-23 22:21:32 +00004510 u8 op_class, chan = 0;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004511
4512 wpa_printf(MSG_DEBUG, "Restarting all CSA-related BSSes");
4513
4514 if (freq_params->center_freq1)
Sunil Ravi7f769292024-07-23 22:21:32 +00004515 ieee80211_freq_to_chan(freq_params->center_freq1, &seg0_idx);
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004516 if (freq_params->center_freq2)
Sunil Ravi7f769292024-07-23 22:21:32 +00004517 ieee80211_freq_to_chan(freq_params->center_freq2, &seg1_idx);
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004518
4519 switch (freq_params->bandwidth) {
4520 case 0:
4521 case 20:
4522 case 40:
Sunil8cd6f4d2022-06-28 18:40:46 +00004523 bw = CONF_OPER_CHWIDTH_USE_HT;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004524 break;
4525 case 80:
Sunil Ravib0ac25f2024-07-12 01:42:03 +00004526 if (freq_params->center_freq2) {
Sunil8cd6f4d2022-06-28 18:40:46 +00004527 bw = CONF_OPER_CHWIDTH_80P80MHZ;
Sunil Ravib0ac25f2024-07-12 01:42:03 +00004528 iface->conf->vht_capab |=
4529 VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ;
4530 } else {
Sunil8cd6f4d2022-06-28 18:40:46 +00004531 bw = CONF_OPER_CHWIDTH_80MHZ;
Sunil Ravib0ac25f2024-07-12 01:42:03 +00004532 }
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004533 break;
4534 case 160:
Sunil8cd6f4d2022-06-28 18:40:46 +00004535 bw = CONF_OPER_CHWIDTH_160MHZ;
Sunil Ravib0ac25f2024-07-12 01:42:03 +00004536 iface->conf->vht_capab |= VHT_CAP_SUPP_CHAN_WIDTH_160MHZ;
Sunil8cd6f4d2022-06-28 18:40:46 +00004537 break;
4538 case 320:
4539 bw = CONF_OPER_CHWIDTH_320MHZ;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004540 break;
4541 default:
4542 wpa_printf(MSG_WARNING, "Unknown CSA bandwidth: %d",
4543 freq_params->bandwidth);
4544 break;
4545 }
4546
4547 iface->freq = freq_params->freq;
4548 iface->conf->channel = freq_params->channel;
4549 iface->conf->secondary_channel = freq_params->sec_channel_offset;
Sunil Ravi7f769292024-07-23 22:21:32 +00004550 if (ieee80211_freq_to_channel_ext(freq_params->freq,
4551 freq_params->sec_channel_offset, bw,
4552 &op_class, &chan) ==
4553 NUM_HOSTAPD_MODES ||
4554 chan != freq_params->channel)
4555 wpa_printf(MSG_INFO, "CSA: Channel mismatch: %d -> %d",
4556 freq_params->channel, chan);
4557
4558 iface->conf->op_class = op_class;
Hai Shalom81f62d82019-07-22 12:10:00 -07004559 hostapd_set_oper_centr_freq_seg0_idx(iface->conf, seg0_idx);
4560 hostapd_set_oper_centr_freq_seg1_idx(iface->conf, seg1_idx);
4561 hostapd_set_oper_chwidth(iface->conf, bw);
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004562 iface->conf->ieee80211n = freq_params->ht_enabled;
4563 iface->conf->ieee80211ac = freq_params->vht_enabled;
Hai Shalom81f62d82019-07-22 12:10:00 -07004564 iface->conf->ieee80211ax = freq_params->he_enabled;
Sunil Ravia04bd252022-05-02 22:54:18 -07004565 iface->conf->ieee80211be = freq_params->eht_enabled;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004566
4567 /*
4568 * cs_params must not be cleared earlier because the freq_params
4569 * argument may actually point to one of these.
Hai Shalom39ba6fc2019-01-22 12:40:38 -08004570 * These params will be cleared during interface disable below.
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004571 */
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004572 hostapd_disable_iface(iface);
4573 hostapd_enable_iface(iface);
4574}
4575
Sunil Ravia04bd252022-05-02 22:54:18 -07004576
4577#ifdef CONFIG_IEEE80211AX
4578
4579void hostapd_cleanup_cca_params(struct hostapd_data *hapd)
4580{
4581 hapd->cca_count = 0;
4582 hapd->cca_color = 0;
4583 hapd->cca_c_off_beacon = 0;
4584 hapd->cca_c_off_proberesp = 0;
4585 hapd->cca_in_progress = false;
4586}
4587
4588
Sunil Ravi7f769292024-07-23 22:21:32 +00004589int hostapd_fill_cca_settings(struct hostapd_data *hapd,
4590 struct cca_settings *settings)
Sunil Ravia04bd252022-05-02 22:54:18 -07004591{
4592 struct hostapd_iface *iface = hapd->iface;
4593 u8 old_color;
4594 int ret;
4595
4596 if (!iface || iface->conf->he_op.he_bss_color_disabled)
4597 return -1;
4598
Sunil Ravi7f769292024-07-23 22:21:32 +00004599 settings->link_id = -1;
4600#ifdef CONFIG_IEEE80211BE
4601 if (hapd->conf->mld_ap)
4602 settings->link_id = hapd->mld_link_id;
4603#endif /* CONFIG_IEEE80211BE */
4604
Sunil Ravia04bd252022-05-02 22:54:18 -07004605 old_color = iface->conf->he_op.he_bss_color;
4606 iface->conf->he_op.he_bss_color = hapd->cca_color;
4607 ret = hostapd_build_beacon_data(hapd, &settings->beacon_after);
4608 if (ret)
4609 return ret;
4610
4611 iface->conf->he_op.he_bss_color = old_color;
4612
4613 settings->cca_count = hapd->cca_count;
4614 settings->cca_color = hapd->cca_color,
4615 hapd->cca_in_progress = true;
4616
4617 ret = hostapd_build_beacon_data(hapd, &settings->beacon_cca);
4618 if (ret) {
4619 free_beacon_data(&settings->beacon_after);
4620 return ret;
4621 }
4622
Sunil Ravi7f769292024-07-23 22:21:32 +00004623 settings->ubpr.unsol_bcast_probe_resp_tmpl =
4624 hostapd_unsol_bcast_probe_resp(hapd, &settings->ubpr);
4625
Sunil Ravia04bd252022-05-02 22:54:18 -07004626 settings->counter_offset_beacon = hapd->cca_c_off_beacon;
4627 settings->counter_offset_presp = hapd->cca_c_off_proberesp;
4628
4629 return 0;
4630}
4631
4632
4633static void hostapd_switch_color_timeout_handler(void *eloop_data,
4634 void *user_ctx)
4635{
4636 struct hostapd_data *hapd = (struct hostapd_data *) eloop_data;
4637 os_time_t delta_t;
4638 unsigned int b;
4639 int i, r;
4640
4641 /* CCA can be triggered once the handler constantly receives
4642 * color collision events to for at least
4643 * DOT11BSS_COLOR_COLLISION_AP_PERIOD (50 s by default). */
4644 delta_t = hapd->last_color_collision.sec -
4645 hapd->first_color_collision.sec;
4646 if (delta_t < DOT11BSS_COLOR_COLLISION_AP_PERIOD)
4647 return;
4648
4649 r = os_random() % HE_OPERATION_BSS_COLOR_MAX;
4650 for (i = 0; i < HE_OPERATION_BSS_COLOR_MAX; i++) {
Sunil8cd6f4d2022-06-28 18:40:46 +00004651 if (r && !(hapd->color_collision_bitmap & (1ULL << r)))
Sunil Ravia04bd252022-05-02 22:54:18 -07004652 break;
4653
4654 r = (r + 1) % HE_OPERATION_BSS_COLOR_MAX;
4655 }
4656
4657 if (i == HE_OPERATION_BSS_COLOR_MAX) {
4658 /* There are no free colors so turn BSS coloring off */
4659 wpa_printf(MSG_INFO,
4660 "No free colors left, turning off BSS coloring");
4661 hapd->iface->conf->he_op.he_bss_color_disabled = 1;
4662 hapd->iface->conf->he_op.he_bss_color = os_random() % 63 + 1;
4663 for (b = 0; b < hapd->iface->num_bss; b++)
4664 ieee802_11_set_beacon(hapd->iface->bss[b]);
4665 return;
4666 }
4667
4668 for (b = 0; b < hapd->iface->num_bss; b++) {
4669 struct hostapd_data *bss = hapd->iface->bss[b];
4670 struct cca_settings settings;
4671 int ret;
4672
4673 hostapd_cleanup_cca_params(bss);
4674 bss->cca_color = r;
4675 bss->cca_count = 10;
4676
4677 if (hostapd_fill_cca_settings(bss, &settings)) {
4678 hostapd_cleanup_cca_params(bss);
4679 continue;
4680 }
4681
4682 ret = hostapd_drv_switch_color(bss, &settings);
4683 if (ret)
4684 hostapd_cleanup_cca_params(bss);
4685
4686 free_beacon_data(&settings.beacon_cca);
4687 free_beacon_data(&settings.beacon_after);
Sunil Ravi7f769292024-07-23 22:21:32 +00004688 os_free(settings.ubpr.unsol_bcast_probe_resp_tmpl);
Sunil Ravia04bd252022-05-02 22:54:18 -07004689 }
4690}
4691
4692
4693void hostapd_switch_color(struct hostapd_data *hapd, u64 bitmap)
4694{
4695 struct os_reltime now;
4696
4697 if (hapd->cca_in_progress)
4698 return;
4699
4700 if (os_get_reltime(&now))
4701 return;
4702
4703 hapd->color_collision_bitmap = bitmap;
4704 hapd->last_color_collision = now;
4705
4706 if (eloop_is_timeout_registered(hostapd_switch_color_timeout_handler,
4707 hapd, NULL))
4708 return;
4709
4710 hapd->first_color_collision = now;
4711 /* 10 s window as margin for persistent color collision reporting */
4712 eloop_register_timeout(DOT11BSS_COLOR_COLLISION_AP_PERIOD + 10, 0,
4713 hostapd_switch_color_timeout_handler,
4714 hapd, NULL);
4715}
4716
4717#endif /* CONFIG_IEEE80211AX */
4718
Dmitry Shmidte4663042016-04-04 10:07:49 -07004719#endif /* NEED_AP_MLME */
4720
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004721
4722struct hostapd_data * hostapd_get_iface(struct hapd_interfaces *interfaces,
4723 const char *ifname)
4724{
4725 size_t i, j;
4726
4727 for (i = 0; i < interfaces->count; i++) {
4728 struct hostapd_iface *iface = interfaces->iface[i];
4729
4730 for (j = 0; j < iface->num_bss; j++) {
4731 struct hostapd_data *hapd = iface->bss[j];
4732
4733 if (os_strcmp(ifname, hapd->conf->iface) == 0)
4734 return hapd;
4735 }
4736 }
4737
4738 return NULL;
4739}
4740
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004741
4742void hostapd_periodic_iface(struct hostapd_iface *iface)
4743{
4744 size_t i;
4745
4746 ap_list_timer(iface);
4747
4748 for (i = 0; i < iface->num_bss; i++) {
4749 struct hostapd_data *hapd = iface->bss[i];
4750
4751 if (!hapd->started)
4752 continue;
4753
4754#ifndef CONFIG_NO_RADIUS
4755 hostapd_acl_expire(hapd);
4756#endif /* CONFIG_NO_RADIUS */
4757 }
4758}
Hai Shalom899fcc72020-10-19 14:38:18 -07004759
4760
4761#ifdef CONFIG_OCV
4762void hostapd_ocv_check_csa_sa_query(void *eloop_ctx, void *timeout_ctx)
4763{
4764 struct hostapd_data *hapd = eloop_ctx;
4765 struct sta_info *sta;
4766
4767 wpa_printf(MSG_DEBUG, "OCV: Post-CSA SA Query initiation check");
4768
4769 for (sta = hapd->sta_list; sta; sta = sta->next) {
4770 if (!sta->post_csa_sa_query)
4771 continue;
4772
4773 wpa_printf(MSG_DEBUG, "OCV: OCVC STA " MACSTR
4774 " did not start SA Query after CSA - disconnect",
4775 MAC2STR(sta->addr));
4776 ap_sta_disconnect(hapd, sta, sta->addr,
4777 WLAN_REASON_PREV_AUTH_NOT_VALID);
4778 }
4779}
4780#endif /* CONFIG_OCV */
Sunil Ravi2a14cf12023-11-21 00:54:38 +00004781
4782
4783#ifdef CONFIG_IEEE80211BE
Sunil Ravi99c035e2024-07-12 01:42:03 +00004784
Sunil Ravi2a14cf12023-11-21 00:54:38 +00004785struct hostapd_data * hostapd_mld_get_link_bss(struct hostapd_data *hapd,
4786 u8 link_id)
4787{
Sunil Ravi7f769292024-07-23 22:21:32 +00004788 struct hostapd_iface *iface;
4789 struct hostapd_data *bss;
4790 unsigned int i, j;
Sunil Ravi2a14cf12023-11-21 00:54:38 +00004791
4792 for (i = 0; i < hapd->iface->interfaces->count; i++) {
Sunil Ravi7f769292024-07-23 22:21:32 +00004793 iface = hapd->iface->interfaces->iface[i];
4794 if (!iface)
Sunil Ravi2a14cf12023-11-21 00:54:38 +00004795 continue;
4796
Sunil Ravi7f769292024-07-23 22:21:32 +00004797 for (j = 0; j < iface->num_bss; j++) {
4798 bss = iface->bss[j];
4799
4800 if (!bss->conf->mld_ap ||
4801 !hostapd_is_ml_partner(hapd, bss))
4802 continue;
4803
4804 if (!bss->drv_priv)
4805 continue;
4806
4807 if (bss->mld_link_id == link_id)
4808 return bss;
4809 }
Sunil Ravi2a14cf12023-11-21 00:54:38 +00004810 }
4811
4812 return NULL;
4813}
Sunil Ravi99c035e2024-07-12 01:42:03 +00004814
4815
4816bool hostapd_is_ml_partner(struct hostapd_data *hapd1,
4817 struct hostapd_data *hapd2)
4818{
4819 if (!hapd1->conf->mld_ap || !hapd2->conf->mld_ap)
4820 return false;
4821
4822 return !os_strcmp(hapd1->conf->iface, hapd2->conf->iface);
4823}
4824
4825
4826u8 hostapd_get_mld_id(struct hostapd_data *hapd)
4827{
4828 if (!hapd->conf->mld_ap)
4829 return 255;
4830
4831 /* MLD ID 0 represents self */
4832 return 0;
4833
4834 /* TODO: MLD ID for Multiple BSS cases */
4835}
4836
4837
4838int hostapd_mld_add_link(struct hostapd_data *hapd)
4839{
4840 struct hostapd_mld *mld = hapd->mld;
4841
4842 if (!hapd->conf->mld_ap)
4843 return 0;
4844
4845 /* Should not happen */
4846 if (!mld)
4847 return -1;
4848
4849 dl_list_add_tail(&mld->links, &hapd->link);
4850 mld->num_links++;
4851
4852 wpa_printf(MSG_DEBUG, "AP MLD %s: Link ID %d added. num_links: %d",
4853 mld->name, hapd->mld_link_id, mld->num_links);
4854
4855 if (mld->fbss)
4856 return 0;
4857
4858 mld->fbss = hapd;
4859 wpa_printf(MSG_DEBUG, "AP MLD %s: First link BSS set to %p",
4860 mld->name, mld->fbss);
4861 return 0;
4862}
4863
4864
4865int hostapd_mld_remove_link(struct hostapd_data *hapd)
4866{
4867 struct hostapd_mld *mld = hapd->mld;
4868 struct hostapd_data *next_fbss;
4869
4870 if (!hapd->conf->mld_ap)
4871 return 0;
4872
4873 /* Should not happen */
4874 if (!mld)
4875 return -1;
4876
4877 dl_list_del(&hapd->link);
4878 mld->num_links--;
4879
4880 wpa_printf(MSG_DEBUG, "AP MLD %s: Link ID %d removed. num_links: %d",
4881 mld->name, hapd->mld_link_id, mld->num_links);
4882
4883 if (mld->fbss != hapd)
4884 return 0;
4885
4886 /* If the list is empty, all links are removed */
4887 if (dl_list_empty(&mld->links)) {
4888 mld->fbss = NULL;
4889 } else {
4890 next_fbss = dl_list_entry(mld->links.next, struct hostapd_data,
4891 link);
4892 mld->fbss = next_fbss;
4893 }
4894
4895 wpa_printf(MSG_DEBUG, "AP MLD %s: First link BSS set to %p",
4896 mld->name, mld->fbss);
4897 return 0;
4898}
4899
4900
4901bool hostapd_mld_is_first_bss(struct hostapd_data *hapd)
4902{
4903 struct hostapd_mld *mld = hapd->mld;
4904
4905 if (!hapd->conf->mld_ap)
4906 return true;
4907
4908 /* Should not happen */
4909 if (!mld)
4910 return false;
4911
4912 /* If fbss is not set, it is safe to assume the caller is the first BSS.
4913 */
4914 if (!mld->fbss)
4915 return true;
4916
4917 return hapd == mld->fbss;
4918}
4919
4920
4921struct hostapd_data * hostapd_mld_get_first_bss(struct hostapd_data *hapd)
4922{
4923 struct hostapd_mld *mld = hapd->mld;
4924
4925 if (!hapd->conf->mld_ap)
4926 return NULL;
4927
4928 /* Should not happen */
4929 if (!mld)
4930 return NULL;
4931
4932 return mld->fbss;
4933}
4934
Sunil Ravi2a14cf12023-11-21 00:54:38 +00004935#endif /* CONFIG_IEEE80211BE */
Sunil Ravi99c035e2024-07-12 01:42:03 +00004936
4937
4938u16 hostapd_get_punct_bitmap(struct hostapd_data *hapd)
4939{
4940 u16 punct_bitmap = 0;
4941
4942#ifdef CONFIG_IEEE80211BE
4943 punct_bitmap = hapd->iconf->punct_bitmap;
4944#ifdef CONFIG_TESTING_OPTIONS
4945 if (!punct_bitmap)
4946 punct_bitmap = hapd->conf->eht_oper_puncturing_override;
4947#endif /* CONFIG_TESTING_OPTIONS */
4948#endif /* CONFIG_IEEE80211BE */
4949
4950 return punct_bitmap;
4951}