blob: 56bac4526cc0cdd90238a038ea8947d7e04d9343 [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 Ravi72e01222024-03-09 01:25:43 +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 Raviaf399a82024-05-05 20:56:55 +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 Ravi72e01222024-03-09 01:25:43 +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 Raviaf399a82024-05-05 20:56:55 +0000421 hostapd_free_link_stas(hapd);
Sunil Ravi72e01222024-03-09 01:25:43 +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 Raviaf399a82024-05-05 20:56:55 +0000483 if (hostapd_mld_is_first_bss(hapd)) {
484#ifdef CONFIG_IEEE80211BE
Sunil Ravi72e01222024-03-09 01:25:43 +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 Raviaf399a82024-05-05 20:56:55 +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 Ravi72e01222024-03-09 01:25:43 +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 Raviaf399a82024-05-05 20:56:55 +0000537 hapd->drv_priv = NULL;
Dmitry Shmidt71757432014-06-02 13:50:35 -0700538 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700539 }
540
Sunil Raviaf399a82024-05-05 20:56:55 +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 Ravi72e01222024-03-09 01:25:43 +0000578#ifndef CONFIG_NO_RRM
Dmitry Shmidt849734c2016-05-27 09:59:01 -0700579 hostapd_clean_rrm(hapd);
Sunil Ravi72e01222024-03-09 01:25:43 +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 Ravi72e01222024-03-09 01:25:43 +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 Raviaf399a82024-05-05 20:56:55 +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 Raviaf399a82024-05-05 20:56:55 +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
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001310/**
1311 * hostapd_setup_bss - Per-BSS setup (initialization)
1312 * @hapd: Pointer to BSS data
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001313 * @first: Whether this BSS is the first BSS of an interface; -1 = not first,
1314 * but interface may exist
Sunil Ravi77d572f2023-01-17 23:58:31 +00001315 * @start_beacon: Whether Beacon frame template should be configured and
1316 * transmission of Beaconf rames started at this time. This is used when
1317 * MBSSID element is enabled where the information regarding all BSSes
1318 * should be retrieved before configuring the Beacon frame template. The
1319 * calling functions are responsible for configuring the Beacon frame
1320 * explicitly if this is set to false.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001321 *
1322 * This function is used to initialize all per-BSS data structures and
1323 * resources. This gets called in a loop for each BSS when an interface is
1324 * initialized. Most of the modules that are initialized here will be
1325 * deinitialized in hostapd_cleanup().
1326 */
Sunil Ravi77d572f2023-01-17 23:58:31 +00001327static int hostapd_setup_bss(struct hostapd_data *hapd, int first,
1328 bool start_beacon)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001329{
1330 struct hostapd_bss_config *conf = hapd->conf;
Dmitry Shmidt9d9e6022015-04-23 10:34:55 -07001331 u8 ssid[SSID_MAX_LEN + 1];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001332 int ssid_len, set_ssid;
1333 char force_ifname[IFNAMSIZ];
1334 u8 if_addr[ETH_ALEN];
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001335 int flush_old_stations = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001336
Sunil Raviaf399a82024-05-05 20:56:55 +00001337 if (!hostapd_mld_is_first_bss(hapd))
Sunil Ravi2a14cf12023-11-21 00:54:38 +00001338 wpa_printf(MSG_DEBUG,
1339 "MLD: %s: Setting non-first BSS", __func__);
1340
Dmitry Shmidt54605472013-11-08 11:10:19 -08001341 wpa_printf(MSG_DEBUG, "%s(hapd=%p (%s), first=%d)",
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001342 __func__, hapd, conf->iface, first);
Dmitry Shmidt54605472013-11-08 11:10:19 -08001343
Dmitry Shmidt50b691d2014-05-21 14:01:45 -07001344#ifdef EAP_SERVER_TNC
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001345 if (conf->tnc && tncs_global_init() < 0) {
Dmitry Shmidt50b691d2014-05-21 14:01:45 -07001346 wpa_printf(MSG_ERROR, "Failed to initialize TNCS");
1347 return -1;
1348 }
1349#endif /* EAP_SERVER_TNC */
1350
Dmitry Shmidt54605472013-11-08 11:10:19 -08001351 if (hapd->started) {
1352 wpa_printf(MSG_ERROR, "%s: Interface %s was already started",
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001353 __func__, conf->iface);
Dmitry Shmidt54605472013-11-08 11:10:19 -08001354 return -1;
1355 }
1356 hapd->started = 1;
1357
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001358 if (!first || first == -1) {
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08001359 u8 *addr = hapd->own_addr;
1360
1361 if (!is_zero_ether_addr(conf->bssid)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001362 /* Allocate the configured BSSID. */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001363 os_memcpy(hapd->own_addr, conf->bssid, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001364
1365 if (hostapd_mac_comp(hapd->own_addr,
1366 hapd->iface->bss[0]->own_addr) ==
1367 0) {
1368 wpa_printf(MSG_ERROR, "BSS '%s' may not have "
1369 "BSSID set to the MAC address of "
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001370 "the radio", conf->iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001371 return -1;
1372 }
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08001373 } else if (hapd->iconf->use_driver_iface_addr) {
1374 addr = NULL;
1375 } else {
1376 /* Allocate the next available BSSID. */
1377 do {
1378 inc_byte_array(hapd->own_addr, ETH_ALEN);
1379 } while (mac_in_conf(hapd->iconf, hapd->own_addr));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001380 }
1381
Dmitry Shmidt54605472013-11-08 11:10:19 -08001382 hapd->interface_added = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001383 if (hostapd_if_add(hapd->iface->bss[0], WPA_IF_AP_BSS,
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08001384 conf->iface, addr, hapd,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001385 &hapd->drv_priv, force_ifname, if_addr,
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001386 conf->bridge[0] ? conf->bridge : NULL,
1387 first == -1)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001388 wpa_printf(MSG_ERROR, "Failed to add BSS (BSSID="
1389 MACSTR ")", MAC2STR(hapd->own_addr));
Dmitry Shmidt3cf6f792013-12-18 13:12:19 -08001390 hapd->interface_added = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001391 return -1;
1392 }
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08001393
1394 if (!addr)
1395 os_memcpy(hapd->own_addr, if_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001396 }
1397
1398 if (conf->wmm_enabled < 0)
Hai Shaloma20dcd72022-02-04 13:43:00 -08001399 conf->wmm_enabled = hapd->iconf->ieee80211n |
1400 hapd->iconf->ieee80211ax;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001401
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001402#ifdef CONFIG_IEEE80211R_AP
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001403 if (is_zero_ether_addr(conf->r1_key_holder))
1404 os_memcpy(conf->r1_key_holder, hapd->own_addr, ETH_ALEN);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001405#endif /* CONFIG_IEEE80211R_AP */
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001406
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001407#ifdef CONFIG_MESH
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001408 if ((hapd->conf->mesh & MESH_ENABLED) && hapd->iface->mconf == NULL)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001409 flush_old_stations = 0;
1410#endif /* CONFIG_MESH */
1411
1412 if (flush_old_stations)
Hai Shalom4fbc08f2020-05-18 12:37:00 -07001413 hostapd_flush(hapd);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001414 hostapd_set_privacy(hapd, 0);
1415
Hai Shalomfdcde762020-04-02 11:19:20 -07001416#ifdef CONFIG_WEP
Hai Shalom4fbc08f2020-05-18 12:37:00 -07001417 if (!hostapd_drv_nl80211(hapd))
1418 hostapd_broadcast_wep_clear(hapd);
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001419 if (hostapd_setup_encryption(conf->iface, hapd))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001420 return -1;
Hai Shalomfdcde762020-04-02 11:19:20 -07001421#endif /* CONFIG_WEP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001422
1423 /*
1424 * Fetch the SSID from the system and use it or,
1425 * if one was specified in the config file, verify they
1426 * match.
1427 */
1428 ssid_len = hostapd_get_ssid(hapd, ssid, sizeof(ssid));
1429 if (ssid_len < 0) {
1430 wpa_printf(MSG_ERROR, "Could not read SSID from system");
1431 return -1;
1432 }
1433 if (conf->ssid.ssid_set) {
1434 /*
1435 * If SSID is specified in the config file and it differs
1436 * from what is being used then force installation of the
1437 * new SSID.
1438 */
1439 set_ssid = (conf->ssid.ssid_len != (size_t) ssid_len ||
1440 os_memcmp(conf->ssid.ssid, ssid, ssid_len) != 0);
1441 } else {
1442 /*
1443 * No SSID in the config file; just use the one we got
1444 * from the system.
1445 */
1446 set_ssid = 0;
1447 conf->ssid.ssid_len = ssid_len;
1448 os_memcpy(conf->ssid.ssid, ssid, conf->ssid.ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001449 }
1450
Hai Shalomfdcde762020-04-02 11:19:20 -07001451 /*
1452 * Short SSID calculation is identical to FCS and it is defined in
1453 * IEEE P802.11-REVmd/D3.0, 9.4.2.170.3 (Calculating the Short-SSID).
1454 */
Sunil Ravi89eba102022-09-13 21:04:37 -07001455 conf->ssid.short_ssid = ieee80211_crc32(conf->ssid.ssid,
1456 conf->ssid.ssid_len);
Hai Shalomfdcde762020-04-02 11:19:20 -07001457
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001458 if (!hostapd_drv_none(hapd)) {
Hai Shalomfdcde762020-04-02 11:19:20 -07001459 wpa_printf(MSG_DEBUG, "Using interface %s with hwaddr " MACSTR
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001460 " and ssid \"%s\"",
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001461 conf->iface, MAC2STR(hapd->own_addr),
1462 wpa_ssid_txt(conf->ssid.ssid, conf->ssid.ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001463 }
1464
1465 if (hostapd_setup_wpa_psk(conf)) {
1466 wpa_printf(MSG_ERROR, "WPA-PSK setup failed.");
1467 return -1;
1468 }
1469
1470 /* Set SSID for the kernel driver (to be used in beacon and probe
1471 * response frames) */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001472 if (set_ssid && hostapd_set_ssid(hapd, conf->ssid.ssid,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001473 conf->ssid.ssid_len)) {
1474 wpa_printf(MSG_ERROR, "Could not set SSID for kernel driver");
1475 return -1;
1476 }
1477
Dmitry Shmidt818ea482014-03-10 13:15:21 -07001478 if (wpa_debug_level <= MSG_MSGDUMP)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001479 conf->radius->msg_dumps = 1;
1480#ifndef CONFIG_NO_RADIUS
Hai Shalomc3565922019-10-28 11:58:20 -07001481
1482#ifdef CONFIG_SQLITE
1483 if (conf->radius_req_attr_sqlite) {
1484 if (sqlite3_open(conf->radius_req_attr_sqlite,
1485 &hapd->rad_attr_db)) {
1486 wpa_printf(MSG_ERROR, "Could not open SQLite file '%s'",
1487 conf->radius_req_attr_sqlite);
1488 return -1;
1489 }
1490
1491 wpa_printf(MSG_DEBUG, "Opening RADIUS attribute database: %s",
1492 conf->radius_req_attr_sqlite);
1493 if (!db_table_exists(hapd->rad_attr_db, "radius_attributes") &&
1494 db_table_create_radius_attributes(hapd->rad_attr_db) < 0)
1495 return -1;
1496 }
1497#endif /* CONFIG_SQLITE */
1498
Sunil Raviaf399a82024-05-05 20:56:55 +00001499 if (hostapd_mld_is_first_bss(hapd)) {
Sunil Ravi2a14cf12023-11-21 00:54:38 +00001500 hapd->radius = radius_client_init(hapd, conf->radius);
1501 if (!hapd->radius) {
1502 wpa_printf(MSG_ERROR,
1503 "RADIUS client initialization failed.");
Dmitry Shmidt04949592012-07-19 12:16:46 -07001504 return -1;
1505 }
Sunil Ravi2a14cf12023-11-21 00:54:38 +00001506
1507 if (conf->radius_das_port) {
1508 struct radius_das_conf das_conf;
1509
1510 os_memset(&das_conf, 0, sizeof(das_conf));
1511 das_conf.port = conf->radius_das_port;
1512 das_conf.shared_secret = conf->radius_das_shared_secret;
1513 das_conf.shared_secret_len =
1514 conf->radius_das_shared_secret_len;
1515 das_conf.client_addr = &conf->radius_das_client_addr;
1516 das_conf.time_window = conf->radius_das_time_window;
1517 das_conf.require_event_timestamp =
1518 conf->radius_das_require_event_timestamp;
1519 das_conf.require_message_authenticator =
1520 conf->radius_das_require_message_authenticator;
1521 das_conf.ctx = hapd;
1522 das_conf.disconnect = hostapd_das_disconnect;
1523 das_conf.coa = hostapd_das_coa;
1524 hapd->radius_das = radius_das_init(&das_conf);
1525 if (!hapd->radius_das) {
1526 wpa_printf(MSG_ERROR,
1527 "RADIUS DAS initialization failed.");
1528 return -1;
1529 }
1530 }
1531 } else {
Sunil Raviaf399a82024-05-05 20:56:55 +00001532#ifdef CONFIG_IEEE80211BE
1533 struct hostapd_data *f_bss;
1534
Sunil Ravi2a14cf12023-11-21 00:54:38 +00001535 wpa_printf(MSG_DEBUG,
1536 "MLD: Using RADIUS client of the first BSS");
Sunil Raviaf399a82024-05-05 20:56:55 +00001537 f_bss = hostapd_mld_get_first_bss(hapd);
1538 if (!f_bss)
1539 return -1;
1540 hapd->radius = f_bss->radius;
1541 hapd->radius_das = f_bss->radius_das;
1542#endif /* CONFIG_IEEE80211BE */
Dmitry Shmidt04949592012-07-19 12:16:46 -07001543 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001544#endif /* CONFIG_NO_RADIUS */
1545
1546 if (hostapd_acl_init(hapd)) {
1547 wpa_printf(MSG_ERROR, "ACL initialization failed.");
1548 return -1;
1549 }
1550 if (hostapd_init_wps(hapd, conf))
1551 return -1;
1552
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001553#ifdef CONFIG_DPP
1554 hapd->gas = gas_query_ap_init(hapd, hapd->msg_ctx);
1555 if (!hapd->gas)
1556 return -1;
1557 if (hostapd_dpp_init(hapd))
1558 return -1;
1559#endif /* CONFIG_DPP */
1560
Sunil Ravi72e01222024-03-09 01:25:43 +00001561#ifdef CONFIG_NAN_USD
1562 if (hostapd_nan_usd_init(hapd) < 0)
1563 return -1;
1564#endif /* CONFIG_NAN_USD */
1565
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001566 if (authsrv_init(hapd) < 0)
1567 return -1;
1568
1569 if (ieee802_1x_init(hapd)) {
1570 wpa_printf(MSG_ERROR, "IEEE 802.1X initialization failed.");
1571 return -1;
1572 }
1573
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001574 if ((conf->wpa || conf->osen) && hostapd_setup_wpa(hapd))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001575 return -1;
1576
1577 if (accounting_init(hapd)) {
1578 wpa_printf(MSG_ERROR, "Accounting initialization failed.");
1579 return -1;
1580 }
1581
Dmitry Shmidt04949592012-07-19 12:16:46 -07001582#ifdef CONFIG_INTERWORKING
1583 if (gas_serv_init(hapd)) {
1584 wpa_printf(MSG_ERROR, "GAS server initialization failed");
1585 return -1;
1586 }
Sunil Raviaf399a82024-05-05 20:56:55 +00001587#endif /* CONFIG_INTERWORKING */
Dmitry Shmidt04949592012-07-19 12:16:46 -07001588
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001589 if (conf->qos_map_set_len &&
1590 hostapd_drv_set_qos_map(hapd, conf->qos_map_set,
1591 conf->qos_map_set_len)) {
1592 wpa_printf(MSG_ERROR, "Failed to initialize QoS Map");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001593 return -1;
1594 }
1595
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001596 if (conf->bss_load_update_period && bss_load_update_init(hapd)) {
1597 wpa_printf(MSG_ERROR, "BSS Load initialization failed");
1598 return -1;
1599 }
1600
Sunil Ravi036cec52023-03-29 11:35:17 -07001601 if (conf->bridge[0]) {
1602 /* Set explicitly configured bridge parameters that might have
1603 * been lost if the interface has been removed out of the
1604 * bridge. */
1605
1606 /* multicast to unicast on bridge ports */
1607 if (conf->bridge_multicast_to_unicast)
1608 hostapd_drv_br_port_set_attr(
1609 hapd, DRV_BR_PORT_ATTR_MCAST2UCAST, 1);
1610
1611 /* hairpin mode */
1612 if (conf->bridge_hairpin)
1613 hostapd_drv_br_port_set_attr(
1614 hapd, DRV_BR_PORT_ATTR_HAIRPIN_MODE, 1);
1615 }
1616
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001617 if (conf->proxy_arp) {
1618 if (x_snoop_init(hapd)) {
1619 wpa_printf(MSG_ERROR,
1620 "Generic snooping infrastructure initialization failed");
1621 return -1;
1622 }
1623
1624 if (dhcp_snoop_init(hapd)) {
1625 wpa_printf(MSG_ERROR,
1626 "DHCP snooping initialization failed");
1627 return -1;
1628 }
1629
1630 if (ndisc_snoop_init(hapd)) {
1631 wpa_printf(MSG_ERROR,
1632 "Neighbor Discovery snooping initialization failed");
1633 return -1;
1634 }
1635 }
1636
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001637 if (!hostapd_drv_none(hapd) && vlan_init(hapd)) {
1638 wpa_printf(MSG_ERROR, "VLAN initialization failed.");
1639 return -1;
1640 }
1641
Sunil Ravi640215c2023-06-28 23:08:09 +00001642 if (start_beacon && hostapd_start_beacon(hapd, flush_old_stations) < 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001643 return -1;
1644
Sunil Ravi640215c2023-06-28 23:08:09 +00001645 if (hapd->wpa_auth && wpa_init_keys(hapd->wpa_auth) < 0)
1646 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001647
1648 return 0;
1649}
1650
1651
1652static void hostapd_tx_queue_params(struct hostapd_iface *iface)
1653{
1654 struct hostapd_data *hapd = iface->bss[0];
1655 int i;
1656 struct hostapd_tx_queue_params *p;
1657
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001658#ifdef CONFIG_MESH
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001659 if ((hapd->conf->mesh & MESH_ENABLED) && iface->mconf == NULL)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001660 return;
1661#endif /* CONFIG_MESH */
1662
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001663 for (i = 0; i < NUM_TX_QUEUES; i++) {
1664 p = &iface->conf->tx_queue[i];
1665
1666 if (hostapd_set_tx_queue_params(hapd, i, p->aifs, p->cwmin,
1667 p->cwmax, p->burst)) {
1668 wpa_printf(MSG_DEBUG, "Failed to set TX queue "
1669 "parameters for queue %d.", i);
1670 /* Continue anyway */
1671 }
1672 }
1673}
1674
1675
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001676static int hostapd_set_acl_list(struct hostapd_data *hapd,
1677 struct mac_acl_entry *mac_acl,
1678 int n_entries, u8 accept_acl)
1679{
1680 struct hostapd_acl_params *acl_params;
1681 int i, err;
1682
1683 acl_params = os_zalloc(sizeof(*acl_params) +
1684 (n_entries * sizeof(acl_params->mac_acl[0])));
1685 if (!acl_params)
1686 return -ENOMEM;
1687
1688 for (i = 0; i < n_entries; i++)
1689 os_memcpy(acl_params->mac_acl[i].addr, mac_acl[i].addr,
1690 ETH_ALEN);
1691
1692 acl_params->acl_policy = accept_acl;
1693 acl_params->num_mac_acl = n_entries;
1694
1695 err = hostapd_drv_set_acl(hapd, acl_params);
1696
1697 os_free(acl_params);
1698
1699 return err;
1700}
1701
1702
Sunil Ravia04bd252022-05-02 22:54:18 -07001703int hostapd_set_acl(struct hostapd_data *hapd)
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001704{
1705 struct hostapd_config *conf = hapd->iconf;
Sunil Ravia04bd252022-05-02 22:54:18 -07001706 int err = 0;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001707 u8 accept_acl;
1708
1709 if (hapd->iface->drv_max_acl_mac_addrs == 0)
Sunil Ravia04bd252022-05-02 22:54:18 -07001710 return 0;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001711
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001712 if (conf->bss[0]->macaddr_acl == DENY_UNLESS_ACCEPTED) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001713 accept_acl = 1;
1714 err = hostapd_set_acl_list(hapd, conf->bss[0]->accept_mac,
1715 conf->bss[0]->num_accept_mac,
1716 accept_acl);
1717 if (err) {
1718 wpa_printf(MSG_DEBUG, "Failed to set accept acl");
Sunil Ravia04bd252022-05-02 22:54:18 -07001719 return -1;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001720 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001721 } else if (conf->bss[0]->macaddr_acl == ACCEPT_UNLESS_DENIED) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001722 accept_acl = 0;
1723 err = hostapd_set_acl_list(hapd, conf->bss[0]->deny_mac,
1724 conf->bss[0]->num_deny_mac,
1725 accept_acl);
1726 if (err) {
1727 wpa_printf(MSG_DEBUG, "Failed to set deny acl");
Sunil Ravia04bd252022-05-02 22:54:18 -07001728 return -1;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001729 }
1730 }
Sunil Ravia04bd252022-05-02 22:54:18 -07001731 return err;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001732}
1733
1734
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001735static int start_ctrl_iface_bss(struct hostapd_data *hapd)
1736{
1737 if (!hapd->iface->interfaces ||
1738 !hapd->iface->interfaces->ctrl_iface_init)
1739 return 0;
1740
1741 if (hapd->iface->interfaces->ctrl_iface_init(hapd)) {
1742 wpa_printf(MSG_ERROR,
1743 "Failed to setup control interface for %s",
1744 hapd->conf->iface);
1745 return -1;
1746 }
1747
1748 return 0;
1749}
1750
1751
1752static int start_ctrl_iface(struct hostapd_iface *iface)
1753{
1754 size_t i;
1755
1756 if (!iface->interfaces || !iface->interfaces->ctrl_iface_init)
1757 return 0;
1758
1759 for (i = 0; i < iface->num_bss; i++) {
1760 struct hostapd_data *hapd = iface->bss[i];
1761 if (iface->interfaces->ctrl_iface_init(hapd)) {
1762 wpa_printf(MSG_ERROR,
1763 "Failed to setup control interface for %s",
1764 hapd->conf->iface);
1765 return -1;
1766 }
1767 }
1768
1769 return 0;
1770}
1771
1772
Sunil Ravi2a14cf12023-11-21 00:54:38 +00001773/* When NO_IR flag is set and AP is stopped, clean up BSS parameters without
1774 * deinitializing the driver and the control interfaces. A subsequent
1775 * REG_CHANGE event can bring the AP back up.
1776 */
1777static void hostapd_no_ir_cleanup(struct hostapd_data *bss)
1778{
1779 hostapd_bss_deinit_no_free(bss);
Sunil Raviaf399a82024-05-05 20:56:55 +00001780 hostapd_bss_link_deinit(bss);
Sunil Ravi2a14cf12023-11-21 00:54:38 +00001781 hostapd_free_hapd_data(bss);
1782 hostapd_cleanup_iface_partial(bss->iface);
1783}
1784
1785
1786static int hostapd_no_ir_channel_list_updated(struct hostapd_iface *iface,
1787 void *ctx)
1788{
1789 bool all_no_ir, is_6ghz;
1790 int i, j;
1791 struct hostapd_hw_modes *mode = NULL;
1792
1793 if (hostapd_get_hw_features(iface))
1794 return 0;
1795
1796 all_no_ir = true;
1797 is_6ghz = false;
1798
1799 for (i = 0; i < iface->num_hw_features; i++) {
1800 mode = &iface->hw_features[i];
1801
1802 if (mode->mode == iface->conf->hw_mode) {
1803 if (iface->freq > 0 &&
1804 !hw_mode_get_channel(mode, iface->freq, NULL)) {
1805 mode = NULL;
1806 continue;
1807 }
1808
1809 for (j = 0; j < mode->num_channels; j++) {
1810 if (!(mode->channels[j].flag &
1811 HOSTAPD_CHAN_NO_IR))
1812 all_no_ir = false;
1813
1814 if (is_6ghz_freq(mode->channels[j].freq))
1815 is_6ghz = true;
1816 }
1817 break;
1818 }
1819 }
1820
1821 if (!mode || !is_6ghz)
1822 return 0;
1823 iface->current_mode = mode;
1824
1825 if (iface->state == HAPD_IFACE_ENABLED) {
1826 if (!all_no_ir) {
1827 struct hostapd_channel_data *chan;
1828
1829 chan = hw_get_channel_freq(iface->current_mode->mode,
1830 iface->freq, NULL,
1831 iface->hw_features,
1832 iface->num_hw_features);
1833
1834 if (!chan) {
1835 wpa_printf(MSG_ERROR,
1836 "NO_IR: Could not derive chan from freq");
1837 return 0;
1838 }
1839
1840 if (!(chan->flag & HOSTAPD_CHAN_NO_IR))
1841 return 0;
1842 wpa_printf(MSG_DEBUG,
1843 "NO_IR: The current channel has NO_IR flag now, stop AP.");
1844 } else {
1845 wpa_printf(MSG_DEBUG,
1846 "NO_IR: All chan in new chanlist are NO_IR, stop AP.");
1847 }
1848
1849 hostapd_set_state(iface, HAPD_IFACE_NO_IR);
1850 iface->is_no_ir = true;
1851 hostapd_drv_stop_ap(iface->bss[0]);
1852 hostapd_no_ir_cleanup(iface->bss[0]);
1853 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_NO_IR);
1854 } else if (iface->state == HAPD_IFACE_NO_IR) {
1855 if (all_no_ir) {
1856 wpa_printf(MSG_DEBUG,
1857 "NO_IR: AP in NO_IR and all chan in the new chanlist are NO_IR. Ignore");
1858 return 0;
1859 }
1860
1861 if (!iface->conf->acs) {
1862 struct hostapd_channel_data *chan;
1863
1864 chan = hw_get_channel_freq(iface->current_mode->mode,
1865 iface->freq, NULL,
1866 iface->hw_features,
1867 iface->num_hw_features);
1868 if (!chan) {
1869 wpa_printf(MSG_ERROR,
1870 "NO_IR: Could not derive chan from freq");
1871 return 0;
1872 }
1873
1874 /* If the last operating channel is NO_IR, trigger ACS.
1875 */
1876 if (chan->flag & HOSTAPD_CHAN_NO_IR) {
1877 iface->freq = 0;
1878 iface->conf->channel = 0;
1879 if (acs_init(iface) != HOSTAPD_CHAN_ACS)
1880 wpa_printf(MSG_ERROR,
1881 "NO_IR: Could not start ACS");
1882 return 0;
1883 }
1884 }
1885
1886 setup_interface2(iface);
1887 }
1888
1889 return 0;
1890}
1891
1892
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001893static void channel_list_update_timeout(void *eloop_ctx, void *timeout_ctx)
1894{
1895 struct hostapd_iface *iface = eloop_ctx;
1896
1897 if (!iface->wait_channel_update) {
1898 wpa_printf(MSG_INFO, "Channel list update timeout, but interface was not waiting for it");
1899 return;
1900 }
1901
1902 /*
1903 * It is possible that the existing channel list is acceptable, so try
1904 * to proceed.
1905 */
1906 wpa_printf(MSG_DEBUG, "Channel list update timeout - try to continue anyway");
1907 setup_interface2(iface);
1908}
1909
1910
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001911void hostapd_channel_list_updated(struct hostapd_iface *iface, int initiator)
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001912{
Sunil Ravi2a14cf12023-11-21 00:54:38 +00001913 if (initiator == REGDOM_SET_BY_DRIVER) {
1914 hostapd_for_each_interface(iface->interfaces,
1915 hostapd_no_ir_channel_list_updated,
1916 NULL);
1917 return;
1918 }
1919
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001920 if (!iface->wait_channel_update || initiator != REGDOM_SET_BY_USER)
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001921 return;
1922
1923 wpa_printf(MSG_DEBUG, "Channel list updated - continue setup");
1924 eloop_cancel_timeout(channel_list_update_timeout, iface, NULL);
1925 setup_interface2(iface);
1926}
1927
1928
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001929static int setup_interface(struct hostapd_iface *iface)
1930{
1931 struct hostapd_data *hapd = iface->bss[0];
1932 size_t i;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001933
Dmitry Shmidta38abf92014-03-06 13:38:44 -08001934 /*
1935 * It is possible that setup_interface() is called after the interface
1936 * was disabled etc., in which case driver_ap_teardown is possibly set
1937 * to 1. Clear it here so any other key/station deletion, which is not
1938 * part of a teardown flow, would also call the relevant driver
1939 * callbacks.
1940 */
1941 iface->driver_ap_teardown = 0;
1942
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001943 if (!iface->phy[0]) {
1944 const char *phy = hostapd_drv_get_radio_name(hapd);
1945 if (phy) {
1946 wpa_printf(MSG_DEBUG, "phy: %s", phy);
1947 os_strlcpy(iface->phy, phy, sizeof(iface->phy));
1948 }
1949 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001950
1951 /*
1952 * Make sure that all BSSes get configured with a pointer to the same
1953 * driver interface.
1954 */
1955 for (i = 1; i < iface->num_bss; i++) {
1956 iface->bss[i]->driver = hapd->driver;
1957 iface->bss[i]->drv_priv = hapd->drv_priv;
1958 }
1959
1960 if (hostapd_validate_bssid_configuration(iface))
1961 return -1;
1962
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001963 /*
1964 * Initialize control interfaces early to allow external monitoring of
1965 * channel setup operations that may take considerable amount of time
1966 * especially for DFS cases.
1967 */
1968 if (start_ctrl_iface(iface))
1969 return -1;
1970
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001971 if (hapd->iconf->country[0] && hapd->iconf->country[1]) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001972 char country[4], previous_country[4];
1973
1974 hostapd_set_state(iface, HAPD_IFACE_COUNTRY_UPDATE);
1975 if (hostapd_get_country(hapd, previous_country) < 0)
1976 previous_country[0] = '\0';
1977
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001978 os_memcpy(country, hapd->iconf->country, 3);
1979 country[3] = '\0';
1980 if (hostapd_set_country(hapd, country) < 0) {
1981 wpa_printf(MSG_ERROR, "Failed to set country code");
1982 return -1;
1983 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001984
1985 wpa_printf(MSG_DEBUG, "Previous country code %s, new country code %s",
1986 previous_country, country);
1987
1988 if (os_strncmp(previous_country, country, 2) != 0) {
1989 wpa_printf(MSG_DEBUG, "Continue interface setup after channel list update");
1990 iface->wait_channel_update = 1;
Dmitry Shmidt97672262014-02-03 13:02:54 -08001991 eloop_register_timeout(5, 0,
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001992 channel_list_update_timeout,
1993 iface, NULL);
1994 return 0;
1995 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001996 }
1997
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001998 return setup_interface2(iface);
1999}
2000
2001
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08002002static int configured_fixed_chan_to_freq(struct hostapd_iface *iface)
2003{
2004 int freq, i, j;
2005
2006 if (!iface->conf->channel)
2007 return 0;
2008 if (iface->conf->op_class) {
2009 freq = ieee80211_chan_to_freq(NULL, iface->conf->op_class,
2010 iface->conf->channel);
2011 if (freq < 0) {
2012 wpa_printf(MSG_INFO,
2013 "Could not convert op_class %u channel %u to operating frequency",
2014 iface->conf->op_class, iface->conf->channel);
2015 return -1;
2016 }
2017 iface->freq = freq;
2018 return 0;
2019 }
2020
2021 /* Old configurations using only 2.4/5/60 GHz bands may not specify the
2022 * op_class parameter. Select a matching channel from the configured
2023 * mode using the channel parameter for these cases.
2024 */
2025 for (j = 0; j < iface->num_hw_features; j++) {
2026 struct hostapd_hw_modes *mode = &iface->hw_features[j];
2027
2028 if (iface->conf->hw_mode != HOSTAPD_MODE_IEEE80211ANY &&
2029 iface->conf->hw_mode != mode->mode)
2030 continue;
2031 for (i = 0; i < mode->num_channels; i++) {
2032 struct hostapd_channel_data *chan = &mode->channels[i];
2033
2034 if (chan->chan == iface->conf->channel &&
2035 !is_6ghz_freq(chan->freq)) {
2036 iface->freq = chan->freq;
2037 return 0;
2038 }
2039 }
2040 }
2041
2042 wpa_printf(MSG_INFO, "Could not determine operating frequency");
2043 return -1;
2044}
2045
2046
Hai Shaloma20dcd72022-02-04 13:43:00 -08002047static void hostapd_set_6ghz_sec_chan(struct hostapd_iface *iface)
2048{
Sunil Ravi640215c2023-06-28 23:08:09 +00002049 int bw;
Hai Shaloma20dcd72022-02-04 13:43:00 -08002050
2051 if (!is_6ghz_op_class(iface->conf->op_class))
2052 return;
2053
Sunil Ravi640215c2023-06-28 23:08:09 +00002054 bw = op_class_to_bandwidth(iface->conf->op_class);
Hai Shaloma20dcd72022-02-04 13:43:00 -08002055 /* Assign the secondary channel if absent in config for
2056 * bandwidths > 20 MHz */
Sunil Ravi640215c2023-06-28 23:08:09 +00002057 if (bw >= 40 && !iface->conf->secondary_channel) {
Hai Shaloma20dcd72022-02-04 13:43:00 -08002058 if (((iface->conf->channel - 1) / 4) % 2)
2059 iface->conf->secondary_channel = -1;
2060 else
2061 iface->conf->secondary_channel = 1;
2062 }
2063}
2064
2065
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002066static int setup_interface2(struct hostapd_iface *iface)
2067{
2068 iface->wait_channel_update = 0;
Sunil Ravi2a14cf12023-11-21 00:54:38 +00002069 iface->is_no_ir = false;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002070
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002071 if (hostapd_get_hw_features(iface)) {
2072 /* Not all drivers support this yet, so continue without hw
2073 * feature data. */
2074 } else {
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08002075 int ret;
2076
Sunil Raviaf399a82024-05-05 20:56:55 +00002077 if (iface->conf->acs && !iface->is_ch_switch_dfs) {
Sunil Ravi036cec52023-03-29 11:35:17 -07002078 iface->freq = 0;
2079 iface->conf->channel = 0;
2080 }
Sunil Raviaf399a82024-05-05 20:56:55 +00002081 iface->is_ch_switch_dfs = false;
Sunil Ravi036cec52023-03-29 11:35:17 -07002082
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08002083 ret = configured_fixed_chan_to_freq(iface);
2084 if (ret < 0)
2085 goto fail;
2086
2087 if (iface->conf->op_class) {
Sunil8cd6f4d2022-06-28 18:40:46 +00002088 enum oper_chan_width ch_width;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08002089
2090 ch_width = op_class_to_ch_width(iface->conf->op_class);
2091 hostapd_set_oper_chwidth(iface->conf, ch_width);
Hai Shaloma20dcd72022-02-04 13:43:00 -08002092 hostapd_set_6ghz_sec_chan(iface);
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08002093 }
2094
2095 ret = hostapd_select_hw_mode(iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002096 if (ret < 0) {
2097 wpa_printf(MSG_ERROR, "Could not select hw_mode and "
2098 "channel. (%d)", ret);
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002099 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002100 }
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002101 if (ret == 1) {
2102 wpa_printf(MSG_DEBUG, "Interface initialization will be completed in a callback (ACS)");
2103 return 0;
2104 }
Hai Shalomc3565922019-10-28 11:58:20 -07002105 ret = hostapd_check_edmg_capab(iface);
2106 if (ret < 0)
2107 goto fail;
Hai Shalom60840252021-02-19 19:02:11 -08002108 ret = hostapd_check_he_6ghz_capab(iface);
2109 if (ret < 0)
2110 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002111 ret = hostapd_check_ht_capab(iface);
2112 if (ret < 0)
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002113 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002114 if (ret == 1) {
2115 wpa_printf(MSG_DEBUG, "Interface initialization will "
2116 "be completed in a callback");
2117 return 0;
2118 }
Dmitry Shmidt051af732013-10-22 13:52:46 -07002119
2120 if (iface->conf->ieee80211h)
2121 wpa_printf(MSG_DEBUG, "DFS support is enabled");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002122 }
2123 return hostapd_setup_interface_complete(iface, 0);
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002124
2125fail:
Sunil Ravi2a14cf12023-11-21 00:54:38 +00002126 if (iface->is_no_ir) {
2127 /* If AP is in NO_IR state, it can be reenabled by the driver
2128 * regulatory update and EVENT_CHANNEL_LIST_CHANGED. */
2129 hostapd_set_state(iface, HAPD_IFACE_NO_IR);
2130 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_NO_IR);
2131 return 0;
2132 }
2133
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002134 hostapd_set_state(iface, HAPD_IFACE_DISABLED);
2135 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
2136 if (iface->interfaces && iface->interfaces->terminate_on_error)
2137 eloop_terminate();
2138 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002139}
2140
2141
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002142#ifdef CONFIG_FST
2143
2144static const u8 * fst_hostapd_get_bssid_cb(void *ctx)
2145{
2146 struct hostapd_data *hapd = ctx;
2147
2148 return hapd->own_addr;
2149}
2150
2151
2152static void fst_hostapd_get_channel_info_cb(void *ctx,
2153 enum hostapd_hw_mode *hw_mode,
2154 u8 *channel)
2155{
2156 struct hostapd_data *hapd = ctx;
2157
2158 *hw_mode = ieee80211_freq_to_chan(hapd->iface->freq, channel);
2159}
2160
2161
Sunil8cd6f4d2022-06-28 18:40:46 +00002162static int fst_hostapd_get_hw_modes_cb(void *ctx,
2163 struct hostapd_hw_modes **modes)
2164{
2165 struct hostapd_data *hapd = ctx;
2166
2167 *modes = hapd->iface->hw_features;
2168 return hapd->iface->num_hw_features;
2169}
2170
2171
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002172static void fst_hostapd_set_ies_cb(void *ctx, const struct wpabuf *fst_ies)
2173{
2174 struct hostapd_data *hapd = ctx;
2175
2176 if (hapd->iface->fst_ies != fst_ies) {
2177 hapd->iface->fst_ies = fst_ies;
2178 if (ieee802_11_set_beacon(hapd))
2179 wpa_printf(MSG_WARNING, "FST: Cannot set beacon");
2180 }
2181}
2182
2183
2184static int fst_hostapd_send_action_cb(void *ctx, const u8 *da,
2185 struct wpabuf *buf)
2186{
2187 struct hostapd_data *hapd = ctx;
2188
2189 return hostapd_drv_send_action(hapd, hapd->iface->freq, 0, da,
2190 wpabuf_head(buf), wpabuf_len(buf));
2191}
2192
2193
2194static const struct wpabuf * fst_hostapd_get_mb_ie_cb(void *ctx, const u8 *addr)
2195{
2196 struct hostapd_data *hapd = ctx;
2197 struct sta_info *sta = ap_get_sta(hapd, addr);
2198
2199 return sta ? sta->mb_ies : NULL;
2200}
2201
2202
2203static void fst_hostapd_update_mb_ie_cb(void *ctx, const u8 *addr,
2204 const u8 *buf, size_t size)
2205{
2206 struct hostapd_data *hapd = ctx;
2207 struct sta_info *sta = ap_get_sta(hapd, addr);
2208
2209 if (sta) {
2210 struct mb_ies_info info;
2211
2212 if (!mb_ies_info_by_ies(&info, buf, size)) {
2213 wpabuf_free(sta->mb_ies);
2214 sta->mb_ies = mb_ies_by_info(&info);
2215 }
2216 }
2217}
2218
2219
2220static const u8 * fst_hostapd_get_sta(struct fst_get_peer_ctx **get_ctx,
Hai Shalome21d4e82020-04-29 16:34:06 -07002221 bool mb_only)
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002222{
2223 struct sta_info *s = (struct sta_info *) *get_ctx;
2224
2225 if (mb_only) {
2226 for (; s && !s->mb_ies; s = s->next)
2227 ;
2228 }
2229
2230 if (s) {
2231 *get_ctx = (struct fst_get_peer_ctx *) s->next;
2232
2233 return s->addr;
2234 }
2235
2236 *get_ctx = NULL;
2237 return NULL;
2238}
2239
2240
2241static const u8 * fst_hostapd_get_peer_first(void *ctx,
2242 struct fst_get_peer_ctx **get_ctx,
Hai Shalome21d4e82020-04-29 16:34:06 -07002243 bool mb_only)
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002244{
2245 struct hostapd_data *hapd = ctx;
2246
2247 *get_ctx = (struct fst_get_peer_ctx *) hapd->sta_list;
2248
2249 return fst_hostapd_get_sta(get_ctx, mb_only);
2250}
2251
2252
2253static const u8 * fst_hostapd_get_peer_next(void *ctx,
2254 struct fst_get_peer_ctx **get_ctx,
Hai Shalome21d4e82020-04-29 16:34:06 -07002255 bool mb_only)
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002256{
2257 return fst_hostapd_get_sta(get_ctx, mb_only);
2258}
2259
2260
2261void fst_hostapd_fill_iface_obj(struct hostapd_data *hapd,
2262 struct fst_wpa_obj *iface_obj)
2263{
Sunil8cd6f4d2022-06-28 18:40:46 +00002264 os_memset(iface_obj, 0, sizeof(*iface_obj));
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002265 iface_obj->ctx = hapd;
2266 iface_obj->get_bssid = fst_hostapd_get_bssid_cb;
2267 iface_obj->get_channel_info = fst_hostapd_get_channel_info_cb;
Sunil8cd6f4d2022-06-28 18:40:46 +00002268 iface_obj->get_hw_modes = fst_hostapd_get_hw_modes_cb;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002269 iface_obj->set_ies = fst_hostapd_set_ies_cb;
2270 iface_obj->send_action = fst_hostapd_send_action_cb;
2271 iface_obj->get_mb_ie = fst_hostapd_get_mb_ie_cb;
2272 iface_obj->update_mb_ie = fst_hostapd_update_mb_ie_cb;
2273 iface_obj->get_peer_first = fst_hostapd_get_peer_first;
2274 iface_obj->get_peer_next = fst_hostapd_get_peer_next;
2275}
2276
2277#endif /* CONFIG_FST */
2278
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002279#ifdef CONFIG_OWE
2280
2281static int hostapd_owe_iface_iter(struct hostapd_iface *iface, void *ctx)
2282{
2283 struct hostapd_data *hapd = ctx;
2284 size_t i;
2285
2286 for (i = 0; i < iface->num_bss; i++) {
2287 struct hostapd_data *bss = iface->bss[i];
2288
2289 if (os_strcmp(hapd->conf->owe_transition_ifname,
2290 bss->conf->iface) != 0)
2291 continue;
2292
2293 wpa_printf(MSG_DEBUG,
2294 "OWE: ifname=%s found transition mode ifname=%s BSSID "
2295 MACSTR " SSID %s",
2296 hapd->conf->iface, bss->conf->iface,
2297 MAC2STR(bss->own_addr),
2298 wpa_ssid_txt(bss->conf->ssid.ssid,
2299 bss->conf->ssid.ssid_len));
2300 if (!bss->conf->ssid.ssid_set || !bss->conf->ssid.ssid_len ||
2301 is_zero_ether_addr(bss->own_addr))
2302 continue;
2303
2304 os_memcpy(hapd->conf->owe_transition_bssid, bss->own_addr,
2305 ETH_ALEN);
2306 os_memcpy(hapd->conf->owe_transition_ssid,
2307 bss->conf->ssid.ssid, bss->conf->ssid.ssid_len);
2308 hapd->conf->owe_transition_ssid_len = bss->conf->ssid.ssid_len;
2309 wpa_printf(MSG_DEBUG,
2310 "OWE: Copied transition mode information");
2311 return 1;
2312 }
2313
2314 return 0;
2315}
2316
2317
2318int hostapd_owe_trans_get_info(struct hostapd_data *hapd)
2319{
2320 if (hapd->conf->owe_transition_ssid_len > 0 &&
2321 !is_zero_ether_addr(hapd->conf->owe_transition_bssid))
2322 return 0;
2323
2324 /* Find transition mode SSID/BSSID information from a BSS operated by
2325 * this hostapd instance. */
2326 if (!hapd->iface->interfaces ||
2327 !hapd->iface->interfaces->for_each_interface)
2328 return hostapd_owe_iface_iter(hapd->iface, hapd);
2329 else
2330 return hapd->iface->interfaces->for_each_interface(
2331 hapd->iface->interfaces, hostapd_owe_iface_iter, hapd);
2332}
2333
2334
2335static int hostapd_owe_iface_iter2(struct hostapd_iface *iface, void *ctx)
2336{
2337 size_t i;
2338
2339 for (i = 0; i < iface->num_bss; i++) {
2340 struct hostapd_data *bss = iface->bss[i];
2341 int res;
2342
2343 if (!bss->conf->owe_transition_ifname[0])
2344 continue;
Hai Shalom899fcc72020-10-19 14:38:18 -07002345 if (bss->iface->state != HAPD_IFACE_ENABLED) {
2346 wpa_printf(MSG_DEBUG,
2347 "OWE: Interface %s state %s - defer beacon update",
2348 bss->conf->iface,
2349 hostapd_state_text(bss->iface->state));
2350 continue;
2351 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002352 res = hostapd_owe_trans_get_info(bss);
2353 if (res == 0)
2354 continue;
2355 wpa_printf(MSG_DEBUG,
2356 "OWE: Matching transition mode interface enabled - update beacon data for %s",
2357 bss->conf->iface);
2358 ieee802_11_set_beacon(bss);
2359 }
2360
2361 return 0;
2362}
2363
2364#endif /* CONFIG_OWE */
2365
2366
2367static void hostapd_owe_update_trans(struct hostapd_iface *iface)
2368{
2369#ifdef CONFIG_OWE
2370 /* Check whether the enabled BSS can complete OWE transition mode
2371 * configuration for any pending interface. */
2372 if (!iface->interfaces ||
2373 !iface->interfaces->for_each_interface)
2374 hostapd_owe_iface_iter2(iface, NULL);
2375 else
2376 iface->interfaces->for_each_interface(
2377 iface->interfaces, hostapd_owe_iface_iter2, NULL);
2378#endif /* CONFIG_OWE */
2379}
2380
2381
Roshan Pius3a1667e2018-07-03 15:17:14 -07002382static void hostapd_interface_setup_failure_handler(void *eloop_ctx,
2383 void *timeout_ctx)
2384{
2385 struct hostapd_iface *iface = eloop_ctx;
2386 struct hostapd_data *hapd;
2387
2388 if (iface->num_bss < 1 || !iface->bss || !iface->bss[0])
2389 return;
2390 hapd = iface->bss[0];
2391 if (hapd->setup_complete_cb)
2392 hapd->setup_complete_cb(hapd->setup_complete_cb_ctx);
2393}
2394
2395
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002396static int hostapd_setup_interface_complete_sync(struct hostapd_iface *iface,
2397 int err)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002398{
2399 struct hostapd_data *hapd = iface->bss[0];
2400 size_t j;
2401 u8 *prev_addr;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002402 int delay_apply_cfg = 0;
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08002403 int res_dfs_offload = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002404
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002405 if (err)
2406 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002407
2408 wpa_printf(MSG_DEBUG, "Completing interface initialization");
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08002409 if (iface->freq) {
Dmitry Shmidt051af732013-10-22 13:52:46 -07002410#ifdef NEED_AP_MLME
2411 int res;
2412#endif /* NEED_AP_MLME */
2413
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002414 wpa_printf(MSG_DEBUG, "Mode: %s Channel: %d "
2415 "Frequency: %d MHz",
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002416 hostapd_hw_mode_txt(iface->conf->hw_mode),
2417 iface->conf->channel, iface->freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002418
Dmitry Shmidt051af732013-10-22 13:52:46 -07002419#ifdef NEED_AP_MLME
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07002420 /* Handle DFS only if it is not offloaded to the driver */
2421 if (!(iface->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD)) {
2422 /* Check DFS */
2423 res = hostapd_handle_dfs(iface);
2424 if (res <= 0) {
2425 if (res < 0)
2426 goto fail;
2427 return res;
2428 }
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08002429 } else {
2430 /* If DFS is offloaded to the driver */
2431 res_dfs_offload = hostapd_handle_dfs_offload(iface);
2432 if (res_dfs_offload <= 0) {
2433 if (res_dfs_offload < 0)
2434 goto fail;
2435 } else {
2436 wpa_printf(MSG_DEBUG,
2437 "Proceed with AP/channel setup");
2438 /*
2439 * If this is a DFS channel, move to completing
2440 * AP setup.
2441 */
2442 if (res_dfs_offload == 1)
2443 goto dfs_offload;
2444 /* Otherwise fall through. */
2445 }
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002446 }
Dmitry Shmidt051af732013-10-22 13:52:46 -07002447#endif /* NEED_AP_MLME */
2448
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002449#ifdef CONFIG_MESH
2450 if (iface->mconf != NULL) {
2451 wpa_printf(MSG_DEBUG,
2452 "%s: Mesh configuration will be applied while joining the mesh network",
2453 iface->bss[0]->conf->iface);
2454 delay_apply_cfg = 1;
2455 }
2456#endif /* CONFIG_MESH */
2457
2458 if (!delay_apply_cfg &&
2459 hostapd_set_freq(hapd, hapd->iconf->hw_mode, iface->freq,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002460 hapd->iconf->channel,
Hai Shalomc3565922019-10-28 11:58:20 -07002461 hapd->iconf->enable_edmg,
2462 hapd->iconf->edmg_channel,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002463 hapd->iconf->ieee80211n,
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002464 hapd->iconf->ieee80211ac,
Hai Shalom81f62d82019-07-22 12:10:00 -07002465 hapd->iconf->ieee80211ax,
Sunil Ravia04bd252022-05-02 22:54:18 -07002466 hapd->iconf->ieee80211be,
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002467 hapd->iconf->secondary_channel,
Hai Shalom81f62d82019-07-22 12:10:00 -07002468 hostapd_get_oper_chwidth(hapd->iconf),
2469 hostapd_get_oper_centr_freq_seg0_idx(
2470 hapd->iconf),
2471 hostapd_get_oper_centr_freq_seg1_idx(
2472 hapd->iconf))) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002473 wpa_printf(MSG_ERROR, "Could not set channel for "
2474 "kernel driver");
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002475 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002476 }
2477 }
2478
2479 if (iface->current_mode) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002480 if (hostapd_prepare_rates(iface, iface->current_mode)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002481 wpa_printf(MSG_ERROR, "Failed to prepare rates "
2482 "table.");
2483 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
2484 HOSTAPD_LEVEL_WARNING,
2485 "Failed to prepare rates table.");
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002486 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002487 }
2488 }
2489
Hai Shalom021b0b52019-04-10 11:17:58 -07002490 if (hapd->iconf->rts_threshold >= -1 &&
2491 hostapd_set_rts(hapd, hapd->iconf->rts_threshold) &&
2492 hapd->iconf->rts_threshold >= -1) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002493 wpa_printf(MSG_ERROR, "Could not set RTS threshold for "
2494 "kernel driver");
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002495 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002496 }
2497
Hai Shalom021b0b52019-04-10 11:17:58 -07002498 if (hapd->iconf->fragm_threshold >= -1 &&
2499 hostapd_set_frag(hapd, hapd->iconf->fragm_threshold) &&
2500 hapd->iconf->fragm_threshold != -1) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002501 wpa_printf(MSG_ERROR, "Could not set fragmentation threshold "
2502 "for kernel driver");
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002503 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002504 }
2505
2506 prev_addr = hapd->own_addr;
2507
2508 for (j = 0; j < iface->num_bss; j++) {
2509 hapd = iface->bss[j];
2510 if (j)
2511 os_memcpy(hapd->own_addr, prev_addr, ETH_ALEN);
Sunil Ravi77d572f2023-01-17 23:58:31 +00002512 if (hostapd_setup_bss(hapd, j == 0, !iface->conf->mbssid)) {
Hai Shalom021b0b52019-04-10 11:17:58 -07002513 for (;;) {
Dmitry Shmidt71757432014-06-02 13:50:35 -07002514 hapd = iface->bss[j];
2515 hostapd_bss_deinit_no_free(hapd);
2516 hostapd_free_hapd_data(hapd);
Hai Shalom021b0b52019-04-10 11:17:58 -07002517 if (j == 0)
2518 break;
2519 j--;
2520 }
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002521 goto fail;
Dmitry Shmidt71757432014-06-02 13:50:35 -07002522 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002523 if (is_zero_ether_addr(hapd->conf->bssid))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002524 prev_addr = hapd->own_addr;
2525 }
Sunil Ravi77d572f2023-01-17 23:58:31 +00002526
2527 if (hapd->iconf->mbssid) {
2528 for (j = 0; hapd->iconf->mbssid && j < iface->num_bss; j++) {
2529 hapd = iface->bss[j];
2530 if (hostapd_start_beacon(hapd, true)) {
2531 for (;;) {
2532 hapd = iface->bss[j];
2533 hostapd_bss_deinit_no_free(hapd);
2534 hostapd_free_hapd_data(hapd);
2535 if (j == 0)
2536 break;
2537 j--;
2538 }
2539 goto fail;
2540 }
2541 }
2542 }
2543
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002544 hapd = iface->bss[0];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002545
2546 hostapd_tx_queue_params(iface);
2547
2548 ap_list_init(iface);
2549
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07002550 hostapd_set_acl(hapd);
2551
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002552 if (hostapd_driver_commit(hapd) < 0) {
2553 wpa_printf(MSG_ERROR, "%s: Failed to commit driver "
2554 "configuration", __func__);
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002555 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002556 }
2557
Jouni Malinen87fd2792011-05-16 18:35:42 +03002558 /*
2559 * WPS UPnP module can be initialized only when the "upnp_iface" is up.
2560 * If "interface" and "upnp_iface" are the same (e.g., non-bridge
2561 * mode), the interface is up only after driver_commit, so initialize
2562 * WPS after driver_commit.
2563 */
2564 for (j = 0; j < iface->num_bss; j++) {
2565 if (hostapd_init_wps_complete(iface->bss[j]))
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002566 goto fail;
Jouni Malinen87fd2792011-05-16 18:35:42 +03002567 }
2568
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08002569 if ((iface->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
2570 !res_dfs_offload) {
2571 /*
2572 * If freq is DFS, and DFS is offloaded to the driver, then wait
2573 * for CAC to complete.
2574 */
2575 wpa_printf(MSG_DEBUG, "%s: Wait for CAC to complete", __func__);
2576 return res_dfs_offload;
2577 }
2578
2579#ifdef NEED_AP_MLME
2580dfs_offload:
2581#endif /* NEED_AP_MLME */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002582
2583#ifdef CONFIG_FST
2584 if (hapd->iconf->fst_cfg.group_id[0]) {
2585 struct fst_wpa_obj iface_obj;
2586
2587 fst_hostapd_fill_iface_obj(hapd, &iface_obj);
2588 iface->fst = fst_attach(hapd->conf->iface, hapd->own_addr,
2589 &iface_obj, &hapd->iconf->fst_cfg);
2590 if (!iface->fst) {
2591 wpa_printf(MSG_ERROR, "Could not attach to FST %s",
2592 hapd->iconf->fst_cfg.group_id);
2593 goto fail;
2594 }
2595 }
2596#endif /* CONFIG_FST */
2597
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002598 hostapd_set_state(iface, HAPD_IFACE_ENABLED);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002599 hostapd_owe_update_trans(iface);
Hai Shalom81f62d82019-07-22 12:10:00 -07002600 airtime_policy_update_init(iface);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002601 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_ENABLED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002602 if (hapd->setup_complete_cb)
2603 hapd->setup_complete_cb(hapd->setup_complete_cb_ctx);
2604
Hai Shalom60840252021-02-19 19:02:11 -08002605#ifdef CONFIG_MESH
2606 if (delay_apply_cfg && !iface->mconf) {
2607 wpa_printf(MSG_ERROR, "Error while completing mesh init");
2608 goto fail;
2609 }
2610#endif /* CONFIG_MESH */
2611
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002612 wpa_printf(MSG_DEBUG, "%s: Setup of interface done.",
2613 iface->bss[0]->conf->iface);
Dmitry Shmidtb96dad42013-11-05 10:07:29 -08002614 if (iface->interfaces && iface->interfaces->terminate_on_error > 0)
2615 iface->interfaces->terminate_on_error--;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002616
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002617 for (j = 0; j < iface->num_bss; j++)
Hai Shalom74f70d42019-02-11 14:42:39 -08002618 hostapd_neighbor_set_own_report(iface->bss[j]);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002619
Sunil Ravi2a14cf12023-11-21 00:54:38 +00002620 if (iface->interfaces && iface->interfaces->count > 1)
2621 ieee802_11_set_beacons(iface);
2622
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002623 return 0;
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002624
2625fail:
2626 wpa_printf(MSG_ERROR, "Interface initialization failed");
Sunil Ravi2a14cf12023-11-21 00:54:38 +00002627
2628 if (iface->is_no_ir) {
2629 hostapd_set_state(iface, HAPD_IFACE_NO_IR);
2630 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_EVENT_NO_IR);
2631 return 0;
2632 }
2633
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002634 hostapd_set_state(iface, HAPD_IFACE_DISABLED);
2635 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002636#ifdef CONFIG_FST
2637 if (iface->fst) {
2638 fst_detach(iface->fst);
2639 iface->fst = NULL;
2640 }
2641#endif /* CONFIG_FST */
Roshan Pius3a1667e2018-07-03 15:17:14 -07002642
2643 if (iface->interfaces && iface->interfaces->terminate_on_error) {
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002644 eloop_terminate();
Roshan Pius3a1667e2018-07-03 15:17:14 -07002645 } else if (hapd->setup_complete_cb) {
2646 /*
2647 * Calling hapd->setup_complete_cb directly may cause iface
2648 * deinitialization which may be accessed later by the caller.
2649 */
2650 eloop_register_timeout(0, 0,
2651 hostapd_interface_setup_failure_handler,
2652 iface, NULL);
2653 }
2654
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002655 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002656}
2657
2658
2659/**
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002660 * hostapd_setup_interface_complete - Complete interface setup
2661 *
2662 * This function is called when previous steps in the interface setup has been
2663 * completed. This can also start operations, e.g., DFS, that will require
2664 * additional processing before interface is ready to be enabled. Such
2665 * operations will call this function from eloop callbacks when finished.
2666 */
2667int hostapd_setup_interface_complete(struct hostapd_iface *iface, int err)
2668{
2669 struct hapd_interfaces *interfaces = iface->interfaces;
2670 struct hostapd_data *hapd = iface->bss[0];
2671 unsigned int i;
2672 int not_ready_in_sync_ifaces = 0;
2673
2674 if (!iface->need_to_start_in_sync)
2675 return hostapd_setup_interface_complete_sync(iface, err);
2676
2677 if (err) {
2678 wpa_printf(MSG_ERROR, "Interface initialization failed");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002679 iface->need_to_start_in_sync = 0;
Sunil Ravi2a14cf12023-11-21 00:54:38 +00002680
2681 if (iface->is_no_ir) {
2682 hostapd_set_state(iface, HAPD_IFACE_NO_IR);
2683 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_EVENT_NO_IR);
2684 return 0;
2685 }
2686
2687 hostapd_set_state(iface, HAPD_IFACE_DISABLED);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002688 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
2689 if (interfaces && interfaces->terminate_on_error)
2690 eloop_terminate();
2691 return -1;
2692 }
2693
2694 if (iface->ready_to_start_in_sync) {
2695 /* Already in ready and waiting. should never happpen */
2696 return 0;
2697 }
2698
2699 for (i = 0; i < interfaces->count; i++) {
2700 if (interfaces->iface[i]->need_to_start_in_sync &&
2701 !interfaces->iface[i]->ready_to_start_in_sync)
2702 not_ready_in_sync_ifaces++;
2703 }
2704
2705 /*
2706 * Check if this is the last interface, if yes then start all the other
2707 * waiting interfaces. If not, add this interface to the waiting list.
2708 */
2709 if (not_ready_in_sync_ifaces > 1 && iface->state == HAPD_IFACE_DFS) {
2710 /*
2711 * If this interface went through CAC, do not synchronize, just
2712 * start immediately.
2713 */
2714 iface->need_to_start_in_sync = 0;
2715 wpa_printf(MSG_INFO,
2716 "%s: Finished CAC - bypass sync and start interface",
2717 iface->bss[0]->conf->iface);
2718 return hostapd_setup_interface_complete_sync(iface, err);
2719 }
2720
2721 if (not_ready_in_sync_ifaces > 1) {
2722 /* need to wait as there are other interfaces still coming up */
2723 iface->ready_to_start_in_sync = 1;
2724 wpa_printf(MSG_INFO,
2725 "%s: Interface waiting to sync with other interfaces",
2726 iface->bss[0]->conf->iface);
2727 return 0;
2728 }
2729
2730 wpa_printf(MSG_INFO,
2731 "%s: Last interface to sync - starting all interfaces",
2732 iface->bss[0]->conf->iface);
2733 iface->need_to_start_in_sync = 0;
2734 hostapd_setup_interface_complete_sync(iface, err);
2735 for (i = 0; i < interfaces->count; i++) {
2736 if (interfaces->iface[i]->need_to_start_in_sync &&
2737 interfaces->iface[i]->ready_to_start_in_sync) {
2738 hostapd_setup_interface_complete_sync(
2739 interfaces->iface[i], 0);
2740 /* Only once the interfaces are sync started */
2741 interfaces->iface[i]->need_to_start_in_sync = 0;
2742 }
2743 }
2744
2745 return 0;
2746}
2747
2748
2749/**
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002750 * hostapd_setup_interface - Setup of an interface
2751 * @iface: Pointer to interface data.
2752 * Returns: 0 on success, -1 on failure
2753 *
2754 * Initializes the driver interface, validates the configuration,
2755 * and sets driver parameters based on the configuration.
2756 * Flushes old stations, sets the channel, encryption,
2757 * beacons, and WDS links based on the configuration.
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002758 *
2759 * If interface setup requires more time, e.g., to perform HT co-ex scans, ACS,
2760 * or DFS operations, this function returns 0 before such operations have been
2761 * completed. The pending operations are registered into eloop and will be
2762 * completed from eloop callbacks. Those callbacks end up calling
2763 * hostapd_setup_interface_complete() once setup has been completed.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002764 */
2765int hostapd_setup_interface(struct hostapd_iface *iface)
2766{
2767 int ret;
2768
Hai Shaloma20dcd72022-02-04 13:43:00 -08002769 if (!iface->conf)
2770 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002771 ret = setup_interface(iface);
2772 if (ret) {
2773 wpa_printf(MSG_ERROR, "%s: Unable to setup interface.",
Hai Shaloma20dcd72022-02-04 13:43:00 -08002774 iface->conf->bss[0]->iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002775 return -1;
2776 }
2777
2778 return 0;
2779}
2780
2781
2782/**
2783 * hostapd_alloc_bss_data - Allocate and initialize per-BSS data
2784 * @hapd_iface: Pointer to interface data
2785 * @conf: Pointer to per-interface configuration
2786 * @bss: Pointer to per-BSS configuration for this BSS
2787 * Returns: Pointer to allocated BSS data
2788 *
2789 * This function is used to allocate per-BSS data structure. This data will be
2790 * freed after hostapd_cleanup() is called for it during interface
2791 * deinitialization.
2792 */
2793struct hostapd_data *
2794hostapd_alloc_bss_data(struct hostapd_iface *hapd_iface,
2795 struct hostapd_config *conf,
2796 struct hostapd_bss_config *bss)
2797{
2798 struct hostapd_data *hapd;
2799
2800 hapd = os_zalloc(sizeof(*hapd));
2801 if (hapd == NULL)
2802 return NULL;
2803
2804 hapd->new_assoc_sta_cb = hostapd_new_assoc_sta;
2805 hapd->iconf = conf;
2806 hapd->conf = bss;
2807 hapd->iface = hapd_iface;
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08002808 if (conf)
2809 hapd->driver = conf->driver;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002810 hapd->ctrl_sock = -1;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08002811 dl_list_init(&hapd->ctrl_dst);
Dmitry Shmidt7d175302016-09-06 13:11:34 -07002812 dl_list_init(&hapd->nr_db);
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08002813 hapd->dhcp_sock = -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002814#ifdef CONFIG_IEEE80211R_AP
2815 dl_list_init(&hapd->l2_queue);
2816 dl_list_init(&hapd->l2_oui_queue);
2817#endif /* CONFIG_IEEE80211R_AP */
Hai Shalom021b0b52019-04-10 11:17:58 -07002818#ifdef CONFIG_SAE
2819 dl_list_init(&hapd->sae_commit_queue);
2820#endif /* CONFIG_SAE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002821
2822 return hapd;
2823}
2824
2825
Dmitry Shmidt54605472013-11-08 11:10:19 -08002826static void hostapd_bss_deinit(struct hostapd_data *hapd)
2827{
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07002828 if (!hapd)
2829 return;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002830 wpa_printf(MSG_DEBUG, "%s: deinit bss %s", __func__,
Hai Shalom021b0b52019-04-10 11:17:58 -07002831 hapd->conf ? hapd->conf->iface : "N/A");
Dmitry Shmidt71757432014-06-02 13:50:35 -07002832 hostapd_bss_deinit_no_free(hapd);
Dmitry Shmidtf73259c2015-03-17 11:00:54 -07002833 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
Hai Shalomc3565922019-10-28 11:58:20 -07002834#ifdef CONFIG_SQLITE
2835 if (hapd->rad_attr_db) {
2836 sqlite3_close(hapd->rad_attr_db);
2837 hapd->rad_attr_db = NULL;
2838 }
2839#endif /* CONFIG_SQLITE */
Sunil Raviaf399a82024-05-05 20:56:55 +00002840
2841 hostapd_bss_link_deinit(hapd);
Dmitry Shmidt54605472013-11-08 11:10:19 -08002842 hostapd_cleanup(hapd);
2843}
2844
2845
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002846void hostapd_interface_deinit(struct hostapd_iface *iface)
2847{
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002848 int j;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002849
Dmitry Shmidt54605472013-11-08 11:10:19 -08002850 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002851 if (iface == NULL)
2852 return;
2853
Dmitry Shmidtf73259c2015-03-17 11:00:54 -07002854 hostapd_set_state(iface, HAPD_IFACE_DISABLED);
2855
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002856 eloop_cancel_timeout(channel_list_update_timeout, iface, NULL);
2857 iface->wait_channel_update = 0;
Sunil Ravi2a14cf12023-11-21 00:54:38 +00002858 iface->is_no_ir = false;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002859
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002860#ifdef CONFIG_FST
2861 if (iface->fst) {
2862 fst_detach(iface->fst);
2863 iface->fst = NULL;
2864 }
2865#endif /* CONFIG_FST */
2866
Hai Shalom021b0b52019-04-10 11:17:58 -07002867 for (j = (int) iface->num_bss - 1; j >= 0; j--) {
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07002868 if (!iface->bss)
2869 break;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002870 hostapd_bss_deinit(iface->bss[j]);
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07002871 }
Roshan Pius3a1667e2018-07-03 15:17:14 -07002872
Roshan Pius3a1667e2018-07-03 15:17:14 -07002873#ifdef NEED_AP_MLME
2874 hostapd_stop_setup_timers(iface);
2875 eloop_cancel_timeout(ap_ht2040_timeout, iface, NULL);
2876#endif /* NEED_AP_MLME */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002877}
2878
2879
Sunil Raviaf399a82024-05-05 20:56:55 +00002880#ifdef CONFIG_IEEE80211BE
2881
2882static void hostapd_mld_ref_inc(struct hostapd_mld *mld)
2883{
2884 if (!mld)
2885 return;
2886
2887 if (mld->refcount == HOSTAPD_MLD_MAX_REF_COUNT) {
2888 wpa_printf(MSG_ERROR, "AP MLD %s: Ref count overflow",
2889 mld->name);
2890 return;
2891 }
2892
2893 mld->refcount++;
2894}
2895
2896
2897static void hostapd_mld_ref_dec(struct hostapd_mld *mld)
2898{
2899 if (!mld)
2900 return;
2901
2902 if (!mld->refcount) {
2903 wpa_printf(MSG_ERROR, "AP MLD %s: Ref count underflow",
2904 mld->name);
2905 return;
2906 }
2907
2908 mld->refcount--;
2909}
2910
2911#endif /* CONFIG_IEEE80211BE */
2912
2913
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002914void hostapd_interface_free(struct hostapd_iface *iface)
2915{
2916 size_t j;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002917 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
2918 for (j = 0; j < iface->num_bss; j++) {
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07002919 if (!iface->bss)
2920 break;
Sunil Raviaf399a82024-05-05 20:56:55 +00002921#ifdef CONFIG_IEEE80211BE
2922 if (iface->bss[j])
2923 hostapd_mld_ref_dec(iface->bss[j]->mld);
2924#endif /* CONFIG_IEEE80211BE */
Dmitry Shmidt54605472013-11-08 11:10:19 -08002925 wpa_printf(MSG_DEBUG, "%s: free hapd %p",
2926 __func__, iface->bss[j]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002927 os_free(iface->bss[j]);
Dmitry Shmidt54605472013-11-08 11:10:19 -08002928 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002929 hostapd_cleanup_iface(iface);
2930}
2931
2932
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07002933struct hostapd_iface * hostapd_alloc_iface(void)
2934{
2935 struct hostapd_iface *hapd_iface;
2936
2937 hapd_iface = os_zalloc(sizeof(*hapd_iface));
2938 if (!hapd_iface)
2939 return NULL;
2940
2941 dl_list_init(&hapd_iface->sta_seen);
2942
2943 return hapd_iface;
2944}
2945
2946
Sunil Raviaf399a82024-05-05 20:56:55 +00002947#ifdef CONFIG_IEEE80211BE
2948static void hostapd_bss_alloc_link_id(struct hostapd_data *hapd)
2949{
2950 hapd->mld_link_id = hapd->mld->next_link_id++;
2951 wpa_printf(MSG_DEBUG, "AP MLD: %s: Link ID %d assigned.",
2952 hapd->mld->name, hapd->mld_link_id);
2953}
2954#endif /* CONFIG_IEEE80211BE */
2955
2956
2957static void hostapd_bss_setup_multi_link(struct hostapd_data *hapd,
2958 struct hapd_interfaces *interfaces)
2959{
2960#ifdef CONFIG_IEEE80211BE
2961 struct hostapd_mld *mld, **all_mld;
2962 struct hostapd_bss_config *conf;
2963 size_t i;
2964
2965 conf = hapd->conf;
2966
2967 if (!hapd->iconf || !hapd->iconf->ieee80211be || !conf->mld_ap ||
2968 conf->disable_11be)
2969 return;
2970
2971 for (i = 0; i < interfaces->mld_count; i++) {
2972 mld = interfaces->mld[i];
2973
2974 if (!mld || os_strcmp(conf->iface, mld->name) != 0)
2975 continue;
2976
2977 hapd->mld = mld;
2978 hostapd_mld_ref_inc(mld);
2979 hostapd_bss_alloc_link_id(hapd);
2980 break;
2981 }
2982
2983 if (hapd->mld)
2984 return;
2985
2986 mld = os_zalloc(sizeof(struct hostapd_mld));
2987 if (!mld)
2988 goto fail;
2989
2990 os_strlcpy(mld->name, conf->iface, sizeof(conf->iface));
2991 dl_list_init(&mld->links);
2992
2993 wpa_printf(MSG_DEBUG, "AP MLD %s created", mld->name);
2994
2995 hapd->mld = mld;
2996 hostapd_mld_ref_inc(mld);
2997 hostapd_bss_alloc_link_id(hapd);
2998
2999 all_mld = os_realloc_array(interfaces->mld, interfaces->mld_count + 1,
3000 sizeof(struct hostapd_mld *));
3001 if (!all_mld)
3002 goto fail;
3003
3004 interfaces->mld = all_mld;
3005 interfaces->mld[interfaces->mld_count] = mld;
3006 interfaces->mld_count++;
3007
3008 return;
3009fail:
3010 if (!mld)
3011 return;
3012
3013 wpa_printf(MSG_DEBUG, "AP MLD %s: free mld %p", mld->name, mld);
3014 os_free(mld);
3015 hapd->mld = NULL;
3016#endif /* CONFIG_IEEE80211BE */
3017}
3018
3019
3020static void hostapd_cleanup_unused_mlds(struct hapd_interfaces *interfaces)
3021{
3022#ifdef CONFIG_IEEE80211BE
3023 struct hostapd_mld *mld, **all_mld;
3024 size_t i, j, num_mlds;
3025 bool forced_remove, remove;
3026
3027 if (!interfaces->mld)
3028 return;
3029
3030 num_mlds = interfaces->mld_count;
3031
3032 for (i = 0; i < interfaces->mld_count; i++) {
3033 mld = interfaces->mld[i];
3034 if (!mld)
3035 continue;
3036
3037 remove = false;
3038 forced_remove = false;
3039
3040 if (!mld->refcount)
3041 remove = true;
3042
3043 /* If MLD is still being referenced but the number of interfaces
3044 * is zero, it is safe to force its deletion. Normally, this
3045 * should not happen but even if it does, let us free the
3046 * memory.
3047 */
3048 if (!remove && !interfaces->count)
3049 forced_remove = true;
3050
3051 if (!remove && !forced_remove)
3052 continue;
3053
3054 wpa_printf(MSG_DEBUG, "AP MLD %s: Freed%s", mld->name,
3055 forced_remove ? " (forced)" : "");
3056 os_free(mld);
3057 interfaces->mld[i] = NULL;
3058 num_mlds--;
3059 }
3060
3061 if (!num_mlds) {
3062 interfaces->mld_count = 0;
3063 os_free(interfaces->mld);
3064 interfaces->mld = NULL;
3065 return;
3066 }
3067
3068 all_mld = os_zalloc(num_mlds * sizeof(struct hostapd_mld *));
3069 if (!all_mld) {
3070 wpa_printf(MSG_ERROR,
3071 "AP MLD: Failed to re-allocate the MLDs. Expect issues");
3072 return;
3073 }
3074
3075 for (i = 0, j = 0; i < interfaces->mld_count; i++) {
3076 mld = interfaces->mld[i];
3077 if (!mld)
3078 continue;
3079
3080 all_mld[j++] = mld;
3081 }
3082
3083 /* This should not happen */
3084 if (j != num_mlds) {
3085 wpa_printf(MSG_DEBUG,
3086 "AP MLD: Some error occurred while reallocating MLDs. Expect issues.");
3087 os_free(all_mld);
3088 return;
3089 }
3090
3091 os_free(interfaces->mld);
3092 interfaces->mld = all_mld;
3093 interfaces->mld_count = num_mlds;
3094#endif /* CONFIG_IEEE80211BE */
3095}
3096
3097
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003098/**
3099 * hostapd_init - Allocate and initialize per-interface data
3100 * @config_file: Path to the configuration file
3101 * Returns: Pointer to the allocated interface data or %NULL on failure
3102 *
3103 * This function is used to allocate main data structures for per-interface
3104 * data. The allocated data buffer will be freed by calling
3105 * hostapd_cleanup_iface().
3106 */
3107struct hostapd_iface * hostapd_init(struct hapd_interfaces *interfaces,
3108 const char *config_file)
3109{
3110 struct hostapd_iface *hapd_iface = NULL;
3111 struct hostapd_config *conf = NULL;
3112 struct hostapd_data *hapd;
3113 size_t i;
3114
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07003115 hapd_iface = hostapd_alloc_iface();
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003116 if (hapd_iface == NULL)
3117 goto fail;
3118
3119 hapd_iface->config_fname = os_strdup(config_file);
3120 if (hapd_iface->config_fname == NULL)
3121 goto fail;
3122
3123 conf = interfaces->config_read_cb(hapd_iface->config_fname);
3124 if (conf == NULL)
3125 goto fail;
3126 hapd_iface->conf = conf;
3127
3128 hapd_iface->num_bss = conf->num_bss;
3129 hapd_iface->bss = os_calloc(conf->num_bss,
3130 sizeof(struct hostapd_data *));
3131 if (hapd_iface->bss == NULL)
3132 goto fail;
3133
3134 for (i = 0; i < conf->num_bss; i++) {
3135 hapd = hapd_iface->bss[i] =
3136 hostapd_alloc_bss_data(hapd_iface, conf,
3137 conf->bss[i]);
3138 if (hapd == NULL)
3139 goto fail;
3140 hapd->msg_ctx = hapd;
Sunil Raviaf399a82024-05-05 20:56:55 +00003141 hostapd_bss_setup_multi_link(hapd, interfaces);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003142 }
3143
Sunil Raviaf399a82024-05-05 20:56:55 +00003144 hapd_iface->is_ch_switch_dfs = false;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003145 return hapd_iface;
3146
3147fail:
3148 wpa_printf(MSG_ERROR, "Failed to set up interface with %s",
3149 config_file);
3150 if (conf)
3151 hostapd_config_free(conf);
3152 if (hapd_iface) {
3153 os_free(hapd_iface->config_fname);
3154 os_free(hapd_iface->bss);
Dmitry Shmidt54605472013-11-08 11:10:19 -08003155 wpa_printf(MSG_DEBUG, "%s: free iface %p",
3156 __func__, hapd_iface);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003157 os_free(hapd_iface);
3158 }
3159 return NULL;
3160}
3161
3162
3163static int ifname_in_use(struct hapd_interfaces *interfaces, const char *ifname)
3164{
3165 size_t i, j;
3166
3167 for (i = 0; i < interfaces->count; i++) {
3168 struct hostapd_iface *iface = interfaces->iface[i];
3169 for (j = 0; j < iface->num_bss; j++) {
3170 struct hostapd_data *hapd = iface->bss[j];
3171 if (os_strcmp(ifname, hapd->conf->iface) == 0)
3172 return 1;
3173 }
3174 }
3175
3176 return 0;
3177}
3178
3179
3180/**
3181 * hostapd_interface_init_bss - Read configuration file and init BSS data
3182 *
3183 * This function is used to parse configuration file for a BSS. This BSS is
3184 * added to an existing interface sharing the same radio (if any) or a new
3185 * interface is created if this is the first interface on a radio. This
3186 * allocate memory for the BSS. No actual driver operations are started.
3187 *
3188 * This is similar to hostapd_interface_init(), but for a case where the
3189 * configuration is used to add a single BSS instead of all BSSes for a radio.
3190 */
3191struct hostapd_iface *
3192hostapd_interface_init_bss(struct hapd_interfaces *interfaces, const char *phy,
3193 const char *config_fname, int debug)
3194{
3195 struct hostapd_iface *new_iface = NULL, *iface = NULL;
3196 struct hostapd_data *hapd;
3197 int k;
3198 size_t i, bss_idx;
3199
3200 if (!phy || !*phy)
3201 return NULL;
3202
3203 for (i = 0; i < interfaces->count; i++) {
3204 if (os_strcmp(interfaces->iface[i]->phy, phy) == 0) {
3205 iface = interfaces->iface[i];
3206 break;
3207 }
3208 }
3209
3210 wpa_printf(MSG_INFO, "Configuration file: %s (phy %s)%s",
3211 config_fname, phy, iface ? "" : " --> new PHY");
3212 if (iface) {
3213 struct hostapd_config *conf;
3214 struct hostapd_bss_config **tmp_conf;
3215 struct hostapd_data **tmp_bss;
3216 struct hostapd_bss_config *bss;
3217 const char *ifname;
3218
3219 /* Add new BSS to existing iface */
3220 conf = interfaces->config_read_cb(config_fname);
3221 if (conf == NULL)
3222 return NULL;
3223 if (conf->num_bss > 1) {
3224 wpa_printf(MSG_ERROR, "Multiple BSSes specified in BSS-config");
3225 hostapd_config_free(conf);
3226 return NULL;
3227 }
3228
3229 ifname = conf->bss[0]->iface;
3230 if (ifname[0] != '\0' && ifname_in_use(interfaces, ifname)) {
3231 wpa_printf(MSG_ERROR,
3232 "Interface name %s already in use", ifname);
3233 hostapd_config_free(conf);
3234 return NULL;
3235 }
3236
3237 tmp_conf = os_realloc_array(
3238 iface->conf->bss, iface->conf->num_bss + 1,
3239 sizeof(struct hostapd_bss_config *));
3240 tmp_bss = os_realloc_array(iface->bss, iface->num_bss + 1,
3241 sizeof(struct hostapd_data *));
3242 if (tmp_bss)
3243 iface->bss = tmp_bss;
3244 if (tmp_conf) {
3245 iface->conf->bss = tmp_conf;
3246 iface->conf->last_bss = tmp_conf[0];
3247 }
3248 if (tmp_bss == NULL || tmp_conf == NULL) {
3249 hostapd_config_free(conf);
3250 return NULL;
3251 }
3252 bss = iface->conf->bss[iface->conf->num_bss] = conf->bss[0];
3253 iface->conf->num_bss++;
3254
3255 hapd = hostapd_alloc_bss_data(iface, iface->conf, bss);
3256 if (hapd == NULL) {
3257 iface->conf->num_bss--;
3258 hostapd_config_free(conf);
3259 return NULL;
3260 }
3261 iface->conf->last_bss = bss;
3262 iface->bss[iface->num_bss] = hapd;
3263 hapd->msg_ctx = hapd;
Sunil Raviaf399a82024-05-05 20:56:55 +00003264 hostapd_bss_setup_multi_link(hapd, interfaces);
3265
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003266
3267 bss_idx = iface->num_bss++;
3268 conf->num_bss--;
3269 conf->bss[0] = NULL;
3270 hostapd_config_free(conf);
3271 } else {
3272 /* Add a new iface with the first BSS */
3273 new_iface = iface = hostapd_init(interfaces, config_fname);
3274 if (!iface)
3275 return NULL;
3276 os_strlcpy(iface->phy, phy, sizeof(iface->phy));
3277 iface->interfaces = interfaces;
3278 bss_idx = 0;
3279 }
3280
3281 for (k = 0; k < debug; k++) {
3282 if (iface->bss[bss_idx]->conf->logger_stdout_level > 0)
3283 iface->bss[bss_idx]->conf->logger_stdout_level--;
3284 }
3285
3286 if (iface->conf->bss[bss_idx]->iface[0] == '\0' &&
3287 !hostapd_drv_none(iface->bss[bss_idx])) {
3288 wpa_printf(MSG_ERROR, "Interface name not specified in %s",
3289 config_fname);
3290 if (new_iface)
3291 hostapd_interface_deinit_free(new_iface);
3292 return NULL;
3293 }
3294
3295 return iface;
3296}
3297
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003298
Sunil Raviaf399a82024-05-05 20:56:55 +00003299static void hostapd_cleanup_driver(const struct wpa_driver_ops *driver,
3300 void *drv_priv, struct hostapd_iface *iface)
3301{
3302#ifdef CONFIG_IEEE80211BE
3303 if (!driver || !driver->hapd_deinit || !drv_priv)
3304 return;
3305
3306 /* In case of non-ML operation, de-init. But if ML operation exist,
3307 * even if that's the last BSS in the interface, the driver (drv) could
3308 * be in use for a different AP MLD. Hence, need to check if drv is
3309 * still being used by some other BSS before de-initiallizing. */
3310 if (!iface->bss[0]->conf->mld_ap) {
3311 driver->hapd_deinit(drv_priv);
3312 } else if (hostapd_mld_is_first_bss(iface->bss[0]) &&
3313 driver->is_drv_shared &&
3314 !driver->is_drv_shared(drv_priv, iface->bss[0])) {
3315 driver->hapd_deinit(drv_priv);
3316 } else if (hostapd_if_link_remove(iface->bss[0],
3317 WPA_IF_AP_BSS,
3318 iface->bss[0]->conf->iface,
3319 iface->bss[0]->mld_link_id)) {
3320 wpa_printf(MSG_WARNING, "Failed to remove BSS interface %s",
3321 iface->bss[0]->conf->iface);
3322 }
3323#else /* CONFIG_IEEE80211BE */
3324 driver->hapd_deinit(drv_priv);
3325#endif /* CONFIG_IEEE80211BE */
3326 iface->bss[0]->drv_priv = NULL;
3327}
3328
3329
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003330void hostapd_interface_deinit_free(struct hostapd_iface *iface)
3331{
3332 const struct wpa_driver_ops *driver;
3333 void *drv_priv;
Dmitry Shmidt54605472013-11-08 11:10:19 -08003334
3335 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003336 if (iface == NULL)
3337 return;
Dmitry Shmidt54605472013-11-08 11:10:19 -08003338 wpa_printf(MSG_DEBUG, "%s: num_bss=%u conf->num_bss=%u",
3339 __func__, (unsigned int) iface->num_bss,
3340 (unsigned int) iface->conf->num_bss);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003341 driver = iface->bss[0]->driver;
3342 drv_priv = iface->bss[0]->drv_priv;
3343 hostapd_interface_deinit(iface);
Dmitry Shmidt54605472013-11-08 11:10:19 -08003344 wpa_printf(MSG_DEBUG, "%s: driver=%p drv_priv=%p -> hapd_deinit",
3345 __func__, driver, drv_priv);
Sunil Raviaf399a82024-05-05 20:56:55 +00003346 hostapd_cleanup_driver(driver, drv_priv, iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003347 hostapd_interface_free(iface);
3348}
3349
3350
Dmitry Shmidt15907092014-03-25 10:42:57 -07003351static void hostapd_deinit_driver(const struct wpa_driver_ops *driver,
3352 void *drv_priv,
3353 struct hostapd_iface *hapd_iface)
3354{
3355 size_t j;
3356
3357 wpa_printf(MSG_DEBUG, "%s: driver=%p drv_priv=%p -> hapd_deinit",
3358 __func__, driver, drv_priv);
Sunil Raviaf399a82024-05-05 20:56:55 +00003359
3360 hostapd_cleanup_driver(driver, drv_priv, hapd_iface);
3361
Dmitry Shmidt15907092014-03-25 10:42:57 -07003362 if (driver && driver->hapd_deinit && drv_priv) {
Dmitry Shmidt15907092014-03-25 10:42:57 -07003363 for (j = 0; j < hapd_iface->num_bss; j++) {
3364 wpa_printf(MSG_DEBUG, "%s:bss[%d]->drv_priv=%p",
3365 __func__, (int) j,
3366 hapd_iface->bss[j]->drv_priv);
Hai Shalom1dc4d202019-04-29 16:22:27 -07003367 if (hapd_iface->bss[j]->drv_priv == drv_priv) {
Sunil Raviaf399a82024-05-05 20:56:55 +00003368 hapd_iface->bss[j]->drv_priv = NULL;
Hai Shalom1dc4d202019-04-29 16:22:27 -07003369 hapd_iface->extended_capa = NULL;
3370 hapd_iface->extended_capa_mask = NULL;
3371 hapd_iface->extended_capa_len = 0;
3372 }
Dmitry Shmidt15907092014-03-25 10:42:57 -07003373 }
3374 }
3375}
3376
3377
Sunil Raviaf399a82024-05-05 20:56:55 +00003378static void hostapd_refresh_all_iface_beacons(struct hostapd_iface *hapd_iface)
3379{
3380 size_t j;
3381
3382 if (!hapd_iface->interfaces || hapd_iface->interfaces->count <= 1)
3383 return;
3384
3385 for (j = 0; j < hapd_iface->interfaces->count; j++) {
3386 if (hapd_iface->interfaces->iface[j] == hapd_iface)
3387 continue;
3388
3389 ieee802_11_update_beacons(hapd_iface->interfaces->iface[j]);
3390 }
3391}
3392
3393
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003394int hostapd_enable_iface(struct hostapd_iface *hapd_iface)
3395{
Dmitry Shmidt71757432014-06-02 13:50:35 -07003396 size_t j;
3397
Hai Shalom60840252021-02-19 19:02:11 -08003398 if (!hapd_iface)
3399 return -1;
3400
3401 if (hapd_iface->enable_iface_cb)
3402 return hapd_iface->enable_iface_cb(hapd_iface);
3403
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003404 if (hapd_iface->bss[0]->drv_priv != NULL) {
3405 wpa_printf(MSG_ERROR, "Interface %s already enabled",
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003406 hapd_iface->conf->bss[0]->iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003407 return -1;
3408 }
3409
3410 wpa_printf(MSG_DEBUG, "Enable interface %s",
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003411 hapd_iface->conf->bss[0]->iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003412
Dmitry Shmidt71757432014-06-02 13:50:35 -07003413 for (j = 0; j < hapd_iface->num_bss; j++)
3414 hostapd_set_security_params(hapd_iface->conf->bss[j], 1);
Dmitry Shmidt344abd32014-01-14 13:17:00 -08003415 if (hostapd_config_check(hapd_iface->conf, 1) < 0) {
3416 wpa_printf(MSG_INFO, "Invalid configuration - cannot enable");
3417 return -1;
3418 }
3419
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003420 if (hapd_iface->interfaces == NULL ||
3421 hapd_iface->interfaces->driver_init == NULL ||
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003422 hapd_iface->interfaces->driver_init(hapd_iface))
3423 return -1;
3424
3425 if (hostapd_setup_interface(hapd_iface)) {
Dmitry Shmidt15907092014-03-25 10:42:57 -07003426 hostapd_deinit_driver(hapd_iface->bss[0]->driver,
3427 hapd_iface->bss[0]->drv_priv,
3428 hapd_iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003429 return -1;
3430 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003431
Sunil Raviaf399a82024-05-05 20:56:55 +00003432 hostapd_refresh_all_iface_beacons(hapd_iface);
3433
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003434 return 0;
3435}
3436
3437
3438int hostapd_reload_iface(struct hostapd_iface *hapd_iface)
3439{
3440 size_t j;
3441
3442 wpa_printf(MSG_DEBUG, "Reload interface %s",
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003443 hapd_iface->conf->bss[0]->iface);
3444 for (j = 0; j < hapd_iface->num_bss; j++)
Dmitry Shmidt71757432014-06-02 13:50:35 -07003445 hostapd_set_security_params(hapd_iface->conf->bss[j], 1);
Dmitry Shmidt344abd32014-01-14 13:17:00 -08003446 if (hostapd_config_check(hapd_iface->conf, 1) < 0) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003447 wpa_printf(MSG_ERROR, "Updated configuration is invalid");
3448 return -1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003449 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003450 hostapd_clear_old(hapd_iface);
3451 for (j = 0; j < hapd_iface->num_bss; j++)
3452 hostapd_reload_bss(hapd_iface->bss[j]);
3453
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003454 return 0;
3455}
3456
3457
Sunil Ravi77d572f2023-01-17 23:58:31 +00003458int hostapd_reload_bss_only(struct hostapd_data *bss)
3459{
3460
3461 wpa_printf(MSG_DEBUG, "Reload BSS %s", bss->conf->iface);
3462 hostapd_set_security_params(bss->conf, 1);
3463 if (hostapd_config_check(bss->iconf, 1) < 0) {
3464 wpa_printf(MSG_ERROR, "Updated BSS configuration is invalid");
3465 return -1;
3466 }
3467 hostapd_clear_old_bss(bss);
3468 hostapd_reload_bss(bss);
3469 return 0;
3470}
3471
3472
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003473int hostapd_disable_iface(struct hostapd_iface *hapd_iface)
3474{
3475 size_t j;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003476 const struct wpa_driver_ops *driver;
3477 void *drv_priv;
3478
3479 if (hapd_iface == NULL)
3480 return -1;
Dmitry Shmidt71757432014-06-02 13:50:35 -07003481
Hai Shalom60840252021-02-19 19:02:11 -08003482 if (hapd_iface->disable_iface_cb)
3483 return hapd_iface->disable_iface_cb(hapd_iface);
3484
Dmitry Shmidt71757432014-06-02 13:50:35 -07003485 if (hapd_iface->bss[0]->drv_priv == NULL) {
3486 wpa_printf(MSG_INFO, "Interface %s already disabled",
3487 hapd_iface->conf->bss[0]->iface);
3488 return -1;
3489 }
3490
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003491 wpa_msg(hapd_iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003492 driver = hapd_iface->bss[0]->driver;
3493 drv_priv = hapd_iface->bss[0]->drv_priv;
3494
Dmitry Shmidta38abf92014-03-06 13:38:44 -08003495 hapd_iface->driver_ap_teardown =
3496 !!(hapd_iface->drv_flags &
3497 WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
3498
Hai Shalom39ba6fc2019-01-22 12:40:38 -08003499#ifdef NEED_AP_MLME
3500 for (j = 0; j < hapd_iface->num_bss; j++)
3501 hostapd_cleanup_cs_params(hapd_iface->bss[j]);
3502#endif /* NEED_AP_MLME */
3503
Dmitry Shmidta38abf92014-03-06 13:38:44 -08003504 /* same as hostapd_interface_deinit without deinitializing ctrl-iface */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003505 for (j = 0; j < hapd_iface->num_bss; j++) {
3506 struct hostapd_data *hapd = hapd_iface->bss[j];
Dmitry Shmidt71757432014-06-02 13:50:35 -07003507 hostapd_bss_deinit_no_free(hapd);
Sunil Raviaf399a82024-05-05 20:56:55 +00003508 hostapd_bss_link_deinit(hapd);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003509 hostapd_free_hapd_data(hapd);
3510 }
3511
Dmitry Shmidt15907092014-03-25 10:42:57 -07003512 hostapd_deinit_driver(driver, drv_priv, hapd_iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003513
3514 /* From hostapd_cleanup_iface: These were initialized in
3515 * hostapd_setup_interface and hostapd_setup_interface_complete
3516 */
3517 hostapd_cleanup_iface_partial(hapd_iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003518
Dmitry Shmidt56052862013-10-04 10:23:25 -07003519 wpa_printf(MSG_DEBUG, "Interface %s disabled",
3520 hapd_iface->bss[0]->conf->iface);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003521 hostapd_set_state(hapd_iface, HAPD_IFACE_DISABLED);
Sunil Raviaf399a82024-05-05 20:56:55 +00003522 hostapd_refresh_all_iface_beacons(hapd_iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003523 return 0;
3524}
3525
3526
3527static struct hostapd_iface *
3528hostapd_iface_alloc(struct hapd_interfaces *interfaces)
3529{
3530 struct hostapd_iface **iface, *hapd_iface;
3531
3532 iface = os_realloc_array(interfaces->iface, interfaces->count + 1,
3533 sizeof(struct hostapd_iface *));
3534 if (iface == NULL)
3535 return NULL;
3536 interfaces->iface = iface;
3537 hapd_iface = interfaces->iface[interfaces->count] =
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07003538 hostapd_alloc_iface();
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003539 if (hapd_iface == NULL) {
3540 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory for "
3541 "the interface", __func__);
3542 return NULL;
3543 }
3544 interfaces->count++;
3545 hapd_iface->interfaces = interfaces;
3546
3547 return hapd_iface;
3548}
3549
3550
3551static struct hostapd_config *
3552hostapd_config_alloc(struct hapd_interfaces *interfaces, const char *ifname,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003553 const char *ctrl_iface, const char *driver)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003554{
3555 struct hostapd_bss_config *bss;
3556 struct hostapd_config *conf;
3557
3558 /* Allocates memory for bss and conf */
3559 conf = hostapd_config_defaults();
3560 if (conf == NULL) {
3561 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory for "
3562 "configuration", __func__);
Hai Shalom74f70d42019-02-11 14:42:39 -08003563 return NULL;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003564 }
3565
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003566 if (driver) {
3567 int j;
3568
3569 for (j = 0; wpa_drivers[j]; j++) {
3570 if (os_strcmp(driver, wpa_drivers[j]->name) == 0) {
3571 conf->driver = wpa_drivers[j];
3572 goto skip;
3573 }
3574 }
3575
3576 wpa_printf(MSG_ERROR,
3577 "Invalid/unknown driver '%s' - registering the default driver",
3578 driver);
3579 }
3580
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003581 conf->driver = wpa_drivers[0];
3582 if (conf->driver == NULL) {
3583 wpa_printf(MSG_ERROR, "No driver wrappers registered!");
3584 hostapd_config_free(conf);
3585 return NULL;
3586 }
3587
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003588skip:
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003589 bss = conf->last_bss = conf->bss[0];
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003590
3591 os_strlcpy(bss->iface, ifname, sizeof(bss->iface));
3592 bss->ctrl_interface = os_strdup(ctrl_iface);
3593 if (bss->ctrl_interface == NULL) {
3594 hostapd_config_free(conf);
3595 return NULL;
3596 }
3597
3598 /* Reading configuration file skipped, will be done in SET!
3599 * From reading the configuration till the end has to be done in
3600 * SET
3601 */
3602 return conf;
3603}
3604
3605
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003606static int hostapd_data_alloc(struct hostapd_iface *hapd_iface,
3607 struct hostapd_config *conf)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003608{
3609 size_t i;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003610 struct hostapd_data *hapd;
3611
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003612 hapd_iface->bss = os_calloc(conf->num_bss,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003613 sizeof(struct hostapd_data *));
3614 if (hapd_iface->bss == NULL)
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003615 return -1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003616
3617 for (i = 0; i < conf->num_bss; i++) {
3618 hapd = hapd_iface->bss[i] =
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003619 hostapd_alloc_bss_data(hapd_iface, conf, conf->bss[i]);
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003620 if (hapd == NULL) {
3621 while (i > 0) {
3622 i--;
3623 os_free(hapd_iface->bss[i]);
3624 hapd_iface->bss[i] = NULL;
3625 }
3626 os_free(hapd_iface->bss);
3627 hapd_iface->bss = NULL;
3628 return -1;
3629 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003630 hapd->msg_ctx = hapd;
Sunil Raviaf399a82024-05-05 20:56:55 +00003631 hostapd_bss_setup_multi_link(hapd, hapd_iface->interfaces);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003632 }
3633
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003634 hapd_iface->conf = conf;
3635 hapd_iface->num_bss = conf->num_bss;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003636
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003637 return 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003638}
3639
3640
3641int hostapd_add_iface(struct hapd_interfaces *interfaces, char *buf)
3642{
3643 struct hostapd_config *conf = NULL;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003644 struct hostapd_iface *hapd_iface = NULL, *new_iface = NULL;
3645 struct hostapd_data *hapd;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003646 char *ptr;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003647 size_t i, j;
3648 const char *conf_file = NULL, *phy_name = NULL;
3649
3650 if (os_strncmp(buf, "bss_config=", 11) == 0) {
3651 char *pos;
3652 phy_name = buf + 11;
3653 pos = os_strchr(phy_name, ':');
3654 if (!pos)
3655 return -1;
3656 *pos++ = '\0';
3657 conf_file = pos;
3658 if (!os_strlen(conf_file))
3659 return -1;
3660
3661 hapd_iface = hostapd_interface_init_bss(interfaces, phy_name,
3662 conf_file, 0);
3663 if (!hapd_iface)
3664 return -1;
3665 for (j = 0; j < interfaces->count; j++) {
3666 if (interfaces->iface[j] == hapd_iface)
3667 break;
3668 }
3669 if (j == interfaces->count) {
3670 struct hostapd_iface **tmp;
3671 tmp = os_realloc_array(interfaces->iface,
3672 interfaces->count + 1,
3673 sizeof(struct hostapd_iface *));
3674 if (!tmp) {
3675 hostapd_interface_deinit_free(hapd_iface);
3676 return -1;
3677 }
3678 interfaces->iface = tmp;
3679 interfaces->iface[interfaces->count++] = hapd_iface;
3680 new_iface = hapd_iface;
3681 }
3682
3683 if (new_iface) {
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003684 if (interfaces->driver_init(hapd_iface))
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003685 goto fail;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003686
3687 if (hostapd_setup_interface(hapd_iface)) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003688 hostapd_deinit_driver(
3689 hapd_iface->bss[0]->driver,
3690 hapd_iface->bss[0]->drv_priv,
3691 hapd_iface);
3692 goto fail;
3693 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003694 } else {
3695 /* Assign new BSS with bss[0]'s driver info */
3696 hapd = hapd_iface->bss[hapd_iface->num_bss - 1];
3697 hapd->driver = hapd_iface->bss[0]->driver;
3698 hapd->drv_priv = hapd_iface->bss[0]->drv_priv;
3699 os_memcpy(hapd->own_addr, hapd_iface->bss[0]->own_addr,
3700 ETH_ALEN);
3701
3702 if (start_ctrl_iface_bss(hapd) < 0 ||
Dmitry Shmidt54605472013-11-08 11:10:19 -08003703 (hapd_iface->state == HAPD_IFACE_ENABLED &&
Sunil Ravi77d572f2023-01-17 23:58:31 +00003704 hostapd_setup_bss(hapd, -1, true))) {
Sunil Raviaf399a82024-05-05 20:56:55 +00003705 hostapd_bss_link_deinit(hapd);
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07003706 hostapd_cleanup(hapd);
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08003707 hapd_iface->bss[hapd_iface->num_bss - 1] = NULL;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003708 hapd_iface->conf->num_bss--;
3709 hapd_iface->num_bss--;
Dmitry Shmidt54605472013-11-08 11:10:19 -08003710 wpa_printf(MSG_DEBUG, "%s: free hapd %p %s",
3711 __func__, hapd, hapd->conf->iface);
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003712 hostapd_config_free_bss(hapd->conf);
3713 hapd->conf = NULL;
Sunil Raviaf399a82024-05-05 20:56:55 +00003714#ifdef CONFIG_IEEE80211BE
3715 hostapd_mld_ref_dec(hapd->mld);
3716#endif /* CONFIG_IEEE80211BE */
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003717 os_free(hapd);
3718 return -1;
3719 }
3720 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003721 hostapd_owe_update_trans(hapd_iface);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003722 return 0;
3723 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003724
3725 ptr = os_strchr(buf, ' ');
3726 if (ptr == NULL)
3727 return -1;
3728 *ptr++ = '\0';
3729
Dmitry Shmidt56052862013-10-04 10:23:25 -07003730 if (os_strncmp(ptr, "config=", 7) == 0)
3731 conf_file = ptr + 7;
3732
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003733 for (i = 0; i < interfaces->count; i++) {
Sunil Ravi2a14cf12023-11-21 00:54:38 +00003734 bool mld_ap = false;
3735
3736#ifdef CONFIG_IEEE80211BE
3737 mld_ap = interfaces->iface[i]->conf->bss[0]->mld_ap;
3738#endif /* CONFIG_IEEE80211BE */
3739
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003740 if (!os_strcmp(interfaces->iface[i]->conf->bss[0]->iface,
Sunil Ravi2a14cf12023-11-21 00:54:38 +00003741 buf) && !mld_ap) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003742 wpa_printf(MSG_INFO, "Cannot add interface - it "
3743 "already exists");
3744 return -1;
3745 }
3746 }
3747
3748 hapd_iface = hostapd_iface_alloc(interfaces);
3749 if (hapd_iface == NULL) {
3750 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
3751 "for interface", __func__);
3752 goto fail;
3753 }
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003754 new_iface = hapd_iface;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003755
Dmitry Shmidt56052862013-10-04 10:23:25 -07003756 if (conf_file && interfaces->config_read_cb) {
3757 conf = interfaces->config_read_cb(conf_file);
3758 if (conf && conf->bss)
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003759 os_strlcpy(conf->bss[0]->iface, buf,
3760 sizeof(conf->bss[0]->iface));
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003761 } else {
3762 char *driver = os_strchr(ptr, ' ');
3763
3764 if (driver)
3765 *driver++ = '\0';
3766 conf = hostapd_config_alloc(interfaces, buf, ptr, driver);
3767 }
3768
Dmitry Shmidt56052862013-10-04 10:23:25 -07003769 if (conf == NULL || conf->bss == NULL) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003770 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
3771 "for configuration", __func__);
3772 goto fail;
3773 }
3774
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003775 if (hostapd_data_alloc(hapd_iface, conf) < 0) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003776 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
3777 "for hostapd", __func__);
3778 goto fail;
3779 }
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003780 conf = NULL;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003781
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003782 if (start_ctrl_iface(hapd_iface) < 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003783 goto fail;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003784
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003785 wpa_printf(MSG_INFO, "Add interface '%s'",
3786 hapd_iface->conf->bss[0]->iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003787
3788 return 0;
3789
3790fail:
3791 if (conf)
3792 hostapd_config_free(conf);
3793 if (hapd_iface) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003794 if (hapd_iface->bss) {
Dmitry Shmidt54605472013-11-08 11:10:19 -08003795 for (i = 0; i < hapd_iface->num_bss; i++) {
3796 hapd = hapd_iface->bss[i];
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08003797 if (!hapd)
3798 continue;
3799 if (hapd_iface->interfaces &&
Dmitry Shmidt54605472013-11-08 11:10:19 -08003800 hapd_iface->interfaces->ctrl_iface_deinit)
3801 hapd_iface->interfaces->
3802 ctrl_iface_deinit(hapd);
3803 wpa_printf(MSG_DEBUG, "%s: free hapd %p (%s)",
3804 __func__, hapd_iface->bss[i],
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08003805 hapd->conf->iface);
Sunil Raviaf399a82024-05-05 20:56:55 +00003806 hostapd_bss_link_deinit(hapd);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003807 hostapd_cleanup(hapd);
Sunil Raviaf399a82024-05-05 20:56:55 +00003808#ifdef CONFIG_IEEE80211BE
3809 hostapd_mld_ref_dec(hapd->mld);
3810#endif /* CONFIG_IEEE80211BE */
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08003811 os_free(hapd);
3812 hapd_iface->bss[i] = NULL;
Dmitry Shmidt54605472013-11-08 11:10:19 -08003813 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003814 os_free(hapd_iface->bss);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003815 hapd_iface->bss = NULL;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003816 }
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003817 if (new_iface) {
3818 interfaces->count--;
3819 interfaces->iface[interfaces->count] = NULL;
Sunil Raviaf399a82024-05-05 20:56:55 +00003820 hostapd_cleanup_unused_mlds(interfaces);
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003821 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003822 hostapd_cleanup_iface(hapd_iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003823 }
3824 return -1;
3825}
3826
3827
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003828static int hostapd_remove_bss(struct hostapd_iface *iface, unsigned int idx)
3829{
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003830 size_t i;
3831
Dmitry Shmidt54605472013-11-08 11:10:19 -08003832 wpa_printf(MSG_INFO, "Remove BSS '%s'", iface->conf->bss[idx]->iface);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003833
Dmitry Shmidt54605472013-11-08 11:10:19 -08003834 /* Remove hostapd_data only if it has already been initialized */
3835 if (idx < iface->num_bss) {
3836 struct hostapd_data *hapd = iface->bss[idx];
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003837
Dmitry Shmidt54605472013-11-08 11:10:19 -08003838 hostapd_bss_deinit(hapd);
3839 wpa_printf(MSG_DEBUG, "%s: free hapd %p (%s)",
3840 __func__, hapd, hapd->conf->iface);
3841 hostapd_config_free_bss(hapd->conf);
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003842 hapd->conf = NULL;
Sunil Raviaf399a82024-05-05 20:56:55 +00003843#ifdef CONFIG_IEEE80211BE
3844 hostapd_mld_ref_dec(hapd->mld);
3845#endif /* CONFIG_IEEE80211BE */
Dmitry Shmidt54605472013-11-08 11:10:19 -08003846 os_free(hapd);
3847
3848 iface->num_bss--;
3849
3850 for (i = idx; i < iface->num_bss; i++)
3851 iface->bss[i] = iface->bss[i + 1];
3852 } else {
3853 hostapd_config_free_bss(iface->conf->bss[idx]);
3854 iface->conf->bss[idx] = NULL;
3855 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003856
3857 iface->conf->num_bss--;
Dmitry Shmidt54605472013-11-08 11:10:19 -08003858 for (i = idx; i < iface->conf->num_bss; i++)
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003859 iface->conf->bss[i] = iface->conf->bss[i + 1];
3860
3861 return 0;
3862}
3863
3864
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003865int hostapd_remove_iface(struct hapd_interfaces *interfaces, char *buf)
3866{
3867 struct hostapd_iface *hapd_iface;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003868 size_t i, j, k = 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003869
3870 for (i = 0; i < interfaces->count; i++) {
3871 hapd_iface = interfaces->iface[i];
3872 if (hapd_iface == NULL)
3873 return -1;
Dmitry Shmidt54605472013-11-08 11:10:19 -08003874 if (!os_strcmp(hapd_iface->conf->bss[0]->iface, buf)) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003875 wpa_printf(MSG_INFO, "Remove interface '%s'", buf);
Dmitry Shmidta38abf92014-03-06 13:38:44 -08003876 hapd_iface->driver_ap_teardown =
3877 !!(hapd_iface->drv_flags &
3878 WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
3879
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003880 hostapd_interface_deinit_free(hapd_iface);
3881 k = i;
3882 while (k < (interfaces->count - 1)) {
3883 interfaces->iface[k] =
3884 interfaces->iface[k + 1];
3885 k++;
3886 }
3887 interfaces->count--;
Sunil Raviaf399a82024-05-05 20:56:55 +00003888 hostapd_cleanup_unused_mlds(interfaces);
3889
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003890 return 0;
3891 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003892
3893 for (j = 0; j < hapd_iface->conf->num_bss; j++) {
Dmitry Shmidta38abf92014-03-06 13:38:44 -08003894 if (!os_strcmp(hapd_iface->conf->bss[j]->iface, buf)) {
3895 hapd_iface->driver_ap_teardown =
3896 !(hapd_iface->drv_flags &
3897 WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003898 return hostapd_remove_bss(hapd_iface, j);
Dmitry Shmidta38abf92014-03-06 13:38:44 -08003899 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003900 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003901 }
3902 return -1;
3903}
3904
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003905
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003906/**
3907 * hostapd_new_assoc_sta - Notify that a new station associated with the AP
3908 * @hapd: Pointer to BSS data
3909 * @sta: Pointer to the associated STA data
3910 * @reassoc: 1 to indicate this was a re-association; 0 = first association
3911 *
3912 * This function will be called whenever a station associates with the AP. It
3913 * can be called from ieee802_11.c for drivers that export MLME to hostapd and
3914 * from drv_callbacks.c based on driver events for drivers that take care of
3915 * management frames (IEEE 802.11 authentication and association) internally.
3916 */
3917void hostapd_new_assoc_sta(struct hostapd_data *hapd, struct sta_info *sta,
3918 int reassoc)
3919{
3920 if (hapd->tkip_countermeasures) {
3921 hostapd_drv_sta_deauth(hapd, sta->addr,
3922 WLAN_REASON_MICHAEL_MIC_FAILURE);
3923 return;
3924 }
3925
Sunil Ravi2a14cf12023-11-21 00:54:38 +00003926#ifdef CONFIG_IEEE80211BE
Sunil Ravi72e01222024-03-09 01:25:43 +00003927 if (ap_sta_is_mld(hapd, sta) &&
Sunil Ravi2a14cf12023-11-21 00:54:38 +00003928 sta->mld_assoc_link_id != hapd->mld_link_id)
3929 return;
3930#endif /* CONFIG_IEEE80211BE */
3931
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -08003932 ap_sta_clear_disconnect_timeouts(hapd, sta);
Hai Shalom899fcc72020-10-19 14:38:18 -07003933 sta->post_csa_sa_query = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003934
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003935#ifdef CONFIG_P2P
3936 if (sta->p2p_ie == NULL && !sta->no_p2p_set) {
3937 sta->no_p2p_set = 1;
3938 hapd->num_sta_no_p2p++;
3939 if (hapd->num_sta_no_p2p == 1)
3940 hostapd_p2p_non_p2p_sta_connected(hapd);
3941 }
3942#endif /* CONFIG_P2P */
3943
Hai Shalom81f62d82019-07-22 12:10:00 -07003944 airtime_policy_new_sta(hapd, sta);
3945
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003946 /* Start accounting here, if IEEE 802.1X and WPA are not used.
3947 * IEEE 802.1X/WPA code will start accounting after the station has
3948 * been authorized. */
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -08003949 if (!hapd->conf->ieee802_1x && !hapd->conf->wpa && !hapd->conf->osen) {
3950 ap_sta_set_authorized(hapd, sta, 1);
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08003951 os_get_reltime(&sta->connected_time);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003952 accounting_sta_start(hapd, sta);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003953 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003954
3955 /* Start IEEE 802.1X authentication process for new stations */
3956 ieee802_1x_new_station(hapd, sta);
3957 if (reassoc) {
3958 if (sta->auth_alg != WLAN_AUTH_FT &&
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003959 sta->auth_alg != WLAN_AUTH_FILS_SK &&
3960 sta->auth_alg != WLAN_AUTH_FILS_SK_PFS &&
3961 sta->auth_alg != WLAN_AUTH_FILS_PK &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003962 !(sta->flags & (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS)))
3963 wpa_auth_sm_event(sta->wpa_sm, WPA_REAUTH);
Sunil Ravi2a14cf12023-11-21 00:54:38 +00003964 } else if (!(hapd->iface->drv_flags2 &
3965 WPA_DRIVER_FLAGS2_4WAY_HANDSHAKE_AP_PSK)) {
3966 /* The 4-way handshake offloaded case will have this handled
3967 * based on the port authorized event. */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003968 wpa_auth_sta_associated(hapd->wpa_auth, sta->wpa_sm);
Sunil Ravi2a14cf12023-11-21 00:54:38 +00003969 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07003970
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08003971 if (hapd->iface->drv_flags & WPA_DRIVER_FLAGS_WIRED) {
3972 if (eloop_cancel_timeout(ap_handle_timer, hapd, sta) > 0) {
3973 wpa_printf(MSG_DEBUG,
3974 "%s: %s: canceled wired ap_handle_timer timeout for "
3975 MACSTR,
3976 hapd->conf->iface, __func__,
3977 MAC2STR(sta->addr));
3978 }
3979 } else if (!(hapd->iface->drv_flags &
3980 WPA_DRIVER_FLAGS_INACTIVITY_TIMER)) {
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -08003981 wpa_printf(MSG_DEBUG,
3982 "%s: %s: reschedule ap_handle_timer timeout for "
3983 MACSTR " (%d seconds - ap_max_inactivity)",
3984 hapd->conf->iface, __func__, MAC2STR(sta->addr),
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08003985 hapd->conf->ap_max_inactivity);
3986 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
3987 eloop_register_timeout(hapd->conf->ap_max_inactivity, 0,
3988 ap_handle_timer, hapd, sta);
3989 }
Hai Shalom81f62d82019-07-22 12:10:00 -07003990
3991#ifdef CONFIG_MACSEC
3992 if (hapd->conf->wpa_key_mgmt == WPA_KEY_MGMT_NONE &&
3993 hapd->conf->mka_psk_set)
3994 ieee802_1x_create_preshared_mka_hapd(hapd, sta);
3995 else
3996 ieee802_1x_alloc_kay_sm_hapd(hapd, sta);
3997#endif /* CONFIG_MACSEC */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003998}
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003999
4000
4001const char * hostapd_state_text(enum hostapd_iface_state s)
4002{
4003 switch (s) {
4004 case HAPD_IFACE_UNINITIALIZED:
4005 return "UNINITIALIZED";
4006 case HAPD_IFACE_DISABLED:
4007 return "DISABLED";
4008 case HAPD_IFACE_COUNTRY_UPDATE:
4009 return "COUNTRY_UPDATE";
4010 case HAPD_IFACE_ACS:
4011 return "ACS";
4012 case HAPD_IFACE_HT_SCAN:
4013 return "HT_SCAN";
4014 case HAPD_IFACE_DFS:
4015 return "DFS";
4016 case HAPD_IFACE_ENABLED:
4017 return "ENABLED";
Sunil Ravi2a14cf12023-11-21 00:54:38 +00004018 case HAPD_IFACE_NO_IR:
4019 return "NO_IR";
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004020 }
4021
4022 return "UNKNOWN";
4023}
4024
4025
4026void hostapd_set_state(struct hostapd_iface *iface, enum hostapd_iface_state s)
4027{
4028 wpa_printf(MSG_INFO, "%s: interface state %s->%s",
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07004029 iface->conf ? iface->conf->bss[0]->iface : "N/A",
4030 hostapd_state_text(iface->state), hostapd_state_text(s));
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004031 iface->state = s;
4032}
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004033
4034
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004035int hostapd_csa_in_progress(struct hostapd_iface *iface)
4036{
4037 unsigned int i;
4038
4039 for (i = 0; i < iface->num_bss; i++)
4040 if (iface->bss[i]->csa_in_progress)
4041 return 1;
4042 return 0;
4043}
4044
4045
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004046#ifdef NEED_AP_MLME
4047
4048static void free_beacon_data(struct beacon_data *beacon)
4049{
4050 os_free(beacon->head);
4051 beacon->head = NULL;
4052 os_free(beacon->tail);
4053 beacon->tail = NULL;
4054 os_free(beacon->probe_resp);
4055 beacon->probe_resp = NULL;
4056 os_free(beacon->beacon_ies);
4057 beacon->beacon_ies = NULL;
4058 os_free(beacon->proberesp_ies);
4059 beacon->proberesp_ies = NULL;
4060 os_free(beacon->assocresp_ies);
4061 beacon->assocresp_ies = NULL;
4062}
4063
4064
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004065static int hostapd_build_beacon_data(struct hostapd_data *hapd,
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004066 struct beacon_data *beacon)
4067{
4068 struct wpabuf *beacon_extra, *proberesp_extra, *assocresp_extra;
4069 struct wpa_driver_ap_params params;
4070 int ret;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004071
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08004072 os_memset(beacon, 0, sizeof(*beacon));
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004073 ret = ieee802_11_build_ap_params(hapd, &params);
4074 if (ret < 0)
4075 return ret;
4076
4077 ret = hostapd_build_ap_extra_ies(hapd, &beacon_extra,
4078 &proberesp_extra,
4079 &assocresp_extra);
4080 if (ret)
4081 goto free_ap_params;
4082
4083 ret = -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004084 beacon->head = os_memdup(params.head, params.head_len);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004085 if (!beacon->head)
4086 goto free_ap_extra_ies;
4087
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004088 beacon->head_len = params.head_len;
4089
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004090 beacon->tail = os_memdup(params.tail, params.tail_len);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004091 if (!beacon->tail)
4092 goto free_beacon;
4093
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004094 beacon->tail_len = params.tail_len;
4095
4096 if (params.proberesp != NULL) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004097 beacon->probe_resp = os_memdup(params.proberesp,
4098 params.proberesp_len);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004099 if (!beacon->probe_resp)
4100 goto free_beacon;
4101
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004102 beacon->probe_resp_len = params.proberesp_len;
4103 }
4104
4105 /* copy the extra ies */
4106 if (beacon_extra) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004107 beacon->beacon_ies = os_memdup(beacon_extra->buf,
4108 wpabuf_len(beacon_extra));
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004109 if (!beacon->beacon_ies)
4110 goto free_beacon;
4111
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004112 beacon->beacon_ies_len = wpabuf_len(beacon_extra);
4113 }
4114
4115 if (proberesp_extra) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004116 beacon->proberesp_ies = os_memdup(proberesp_extra->buf,
4117 wpabuf_len(proberesp_extra));
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004118 if (!beacon->proberesp_ies)
4119 goto free_beacon;
4120
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004121 beacon->proberesp_ies_len = wpabuf_len(proberesp_extra);
4122 }
4123
4124 if (assocresp_extra) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004125 beacon->assocresp_ies = os_memdup(assocresp_extra->buf,
4126 wpabuf_len(assocresp_extra));
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004127 if (!beacon->assocresp_ies)
4128 goto free_beacon;
4129
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004130 beacon->assocresp_ies_len = wpabuf_len(assocresp_extra);
4131 }
4132
4133 ret = 0;
4134free_beacon:
4135 /* if the function fails, the caller should not free beacon data */
4136 if (ret)
4137 free_beacon_data(beacon);
4138
4139free_ap_extra_ies:
4140 hostapd_free_ap_extra_ies(hapd, beacon_extra, proberesp_extra,
4141 assocresp_extra);
4142free_ap_params:
4143 ieee802_11_free_ap_params(&params);
4144 return ret;
4145}
4146
4147
4148/*
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004149 * TODO: This flow currently supports only changing channel and width within
4150 * the same hw_mode. Any other changes to MAC parameters or provided settings
4151 * are not supported.
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004152 */
4153static int hostapd_change_config_freq(struct hostapd_data *hapd,
4154 struct hostapd_config *conf,
4155 struct hostapd_freq_params *params,
4156 struct hostapd_freq_params *old_params)
4157{
4158 int channel;
Sunil Ravi72e01222024-03-09 01:25:43 +00004159 u8 seg0 = 0, seg1 = 0;
Hai Shalom81f62d82019-07-22 12:10:00 -07004160 struct hostapd_hw_modes *mode;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004161
4162 if (!params->channel) {
4163 /* check if the new channel is supported by hw */
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004164 params->channel = hostapd_hw_get_channel(hapd, params->freq);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004165 }
4166
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004167 channel = params->channel;
4168 if (!channel)
4169 return -1;
4170
Sunil Ravi2a14cf12023-11-21 00:54:38 +00004171 hostapd_determine_mode(hapd->iface);
Hai Shalom81f62d82019-07-22 12:10:00 -07004172 mode = hapd->iface->current_mode;
4173
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004174 /* if a pointer to old_params is provided we save previous state */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004175 if (old_params &&
4176 hostapd_set_freq_params(old_params, conf->hw_mode,
4177 hostapd_hw_get_freq(hapd, conf->channel),
Hai Shalomc3565922019-10-28 11:58:20 -07004178 conf->channel, conf->enable_edmg,
4179 conf->edmg_channel, conf->ieee80211n,
Hai Shalom81f62d82019-07-22 12:10:00 -07004180 conf->ieee80211ac, conf->ieee80211ax,
Sunil Ravia04bd252022-05-02 22:54:18 -07004181 conf->ieee80211be, conf->secondary_channel,
Hai Shalom81f62d82019-07-22 12:10:00 -07004182 hostapd_get_oper_chwidth(conf),
4183 hostapd_get_oper_centr_freq_seg0_idx(conf),
4184 hostapd_get_oper_centr_freq_seg1_idx(conf),
4185 conf->vht_capab,
4186 mode ? &mode->he_capab[IEEE80211_MODE_AP] :
Sunil Ravia04bd252022-05-02 22:54:18 -07004187 NULL,
4188 mode ? &mode->eht_capab[IEEE80211_MODE_AP] :
Sunil Raviaf399a82024-05-05 20:56:55 +00004189 NULL,
4190 hostapd_get_punct_bitmap(hapd)))
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004191 return -1;
4192
4193 switch (params->bandwidth) {
4194 case 0:
4195 case 20:
Hai Shaloma20dcd72022-02-04 13:43:00 -08004196 conf->ht_capab &= ~HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET;
4197 break;
4198 case 40:
4199 case 80:
4200 case 160:
Sunil Ravi640215c2023-06-28 23:08:09 +00004201 case 320:
Hai Shaloma20dcd72022-02-04 13:43:00 -08004202 conf->ht_capab |= HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET;
4203 break;
4204 default:
4205 return -1;
4206 }
4207
4208 switch (params->bandwidth) {
4209 case 0:
4210 case 20:
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004211 case 40:
Sunil8cd6f4d2022-06-28 18:40:46 +00004212 hostapd_set_oper_chwidth(conf, CONF_OPER_CHWIDTH_USE_HT);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004213 break;
4214 case 80:
4215 if (params->center_freq2)
Sunil8cd6f4d2022-06-28 18:40:46 +00004216 hostapd_set_oper_chwidth(conf,
4217 CONF_OPER_CHWIDTH_80P80MHZ);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004218 else
Sunil8cd6f4d2022-06-28 18:40:46 +00004219 hostapd_set_oper_chwidth(conf,
4220 CONF_OPER_CHWIDTH_80MHZ);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004221 break;
4222 case 160:
Sunil8cd6f4d2022-06-28 18:40:46 +00004223 hostapd_set_oper_chwidth(conf, CONF_OPER_CHWIDTH_160MHZ);
4224 break;
4225 case 320:
4226 hostapd_set_oper_chwidth(conf, CONF_OPER_CHWIDTH_320MHZ);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004227 break;
4228 default:
4229 return -1;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004230 }
4231
4232 conf->channel = channel;
4233 conf->ieee80211n = params->ht_enabled;
Hai Shaloma20dcd72022-02-04 13:43:00 -08004234 conf->ieee80211ac = params->vht_enabled;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004235 conf->secondary_channel = params->sec_channel_offset;
Sunil Ravi72e01222024-03-09 01:25:43 +00004236 if (params->center_freq1 &&
4237 ieee80211_freq_to_chan(params->center_freq1, &seg0) ==
4238 NUM_HOSTAPD_MODES)
4239 return -1;
4240 if (params->center_freq2 &&
4241 ieee80211_freq_to_chan(params->center_freq2,
4242 &seg1) == NUM_HOSTAPD_MODES)
4243 return -1;
Hai Shalom81f62d82019-07-22 12:10:00 -07004244 hostapd_set_oper_centr_freq_seg0_idx(conf, seg0);
4245 hostapd_set_oper_centr_freq_seg1_idx(conf, seg1);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004246
4247 /* TODO: maybe call here hostapd_config_check here? */
4248
4249 return 0;
4250}
4251
4252
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004253static int hostapd_fill_csa_settings(struct hostapd_data *hapd,
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004254 struct csa_settings *settings)
4255{
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004256 struct hostapd_iface *iface = hapd->iface;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004257 struct hostapd_freq_params old_freq;
4258 int ret;
Sunil Ravi036cec52023-03-29 11:35:17 -07004259#ifdef CONFIG_IEEE80211BE
4260 u16 old_punct_bitmap;
4261#endif /* CONFIG_IEEE80211BE */
Hai Shalom81f62d82019-07-22 12:10:00 -07004262 u8 chan, bandwidth;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004263
4264 os_memset(&old_freq, 0, sizeof(old_freq));
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004265 if (!iface || !iface->freq || hapd->csa_in_progress)
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004266 return -1;
4267
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004268 switch (settings->freq_params.bandwidth) {
4269 case 80:
4270 if (settings->freq_params.center_freq2)
Sunil8cd6f4d2022-06-28 18:40:46 +00004271 bandwidth = CONF_OPER_CHWIDTH_80P80MHZ;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004272 else
Sunil8cd6f4d2022-06-28 18:40:46 +00004273 bandwidth = CONF_OPER_CHWIDTH_80MHZ;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004274 break;
4275 case 160:
Sunil8cd6f4d2022-06-28 18:40:46 +00004276 bandwidth = CONF_OPER_CHWIDTH_160MHZ;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004277 break;
Sunil Ravi640215c2023-06-28 23:08:09 +00004278 case 320:
4279 bandwidth = CONF_OPER_CHWIDTH_320MHZ;
4280 break;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004281 default:
Sunil8cd6f4d2022-06-28 18:40:46 +00004282 bandwidth = CONF_OPER_CHWIDTH_USE_HT;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004283 break;
4284 }
4285
4286 if (ieee80211_freq_to_channel_ext(
4287 settings->freq_params.freq,
4288 settings->freq_params.sec_channel_offset,
Hai Shalom81f62d82019-07-22 12:10:00 -07004289 bandwidth,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004290 &hapd->iface->cs_oper_class,
4291 &chan) == NUM_HOSTAPD_MODES) {
4292 wpa_printf(MSG_DEBUG,
Sunil Ravia04bd252022-05-02 22:54:18 -07004293 "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 -08004294 settings->freq_params.freq,
4295 settings->freq_params.sec_channel_offset,
Hai Shalom81f62d82019-07-22 12:10:00 -07004296 settings->freq_params.vht_enabled,
Sunil Ravia04bd252022-05-02 22:54:18 -07004297 settings->freq_params.he_enabled,
4298 settings->freq_params.eht_enabled);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004299 return -1;
4300 }
4301
4302 settings->freq_params.channel = chan;
4303
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004304 ret = hostapd_change_config_freq(iface->bss[0], iface->conf,
4305 &settings->freq_params,
4306 &old_freq);
4307 if (ret)
4308 return ret;
4309
Sunil Ravi036cec52023-03-29 11:35:17 -07004310#ifdef CONFIG_IEEE80211BE
4311 old_punct_bitmap = iface->conf->punct_bitmap;
4312 iface->conf->punct_bitmap = settings->punct_bitmap;
4313#endif /* CONFIG_IEEE80211BE */
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004314 ret = hostapd_build_beacon_data(hapd, &settings->beacon_after);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004315
4316 /* change back the configuration */
Sunil Ravi036cec52023-03-29 11:35:17 -07004317#ifdef CONFIG_IEEE80211BE
4318 iface->conf->punct_bitmap = old_punct_bitmap;
4319#endif /* CONFIG_IEEE80211BE */
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004320 hostapd_change_config_freq(iface->bss[0], iface->conf,
4321 &old_freq, NULL);
4322
4323 if (ret)
4324 return ret;
4325
4326 /* set channel switch parameters for csa ie */
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004327 hapd->cs_freq_params = settings->freq_params;
4328 hapd->cs_count = settings->cs_count;
4329 hapd->cs_block_tx = settings->block_tx;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004330
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004331 ret = hostapd_build_beacon_data(hapd, &settings->beacon_csa);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004332 if (ret) {
4333 free_beacon_data(&settings->beacon_after);
4334 return ret;
4335 }
4336
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004337 settings->counter_offset_beacon[0] = hapd->cs_c_off_beacon;
4338 settings->counter_offset_presp[0] = hapd->cs_c_off_proberesp;
4339 settings->counter_offset_beacon[1] = hapd->cs_c_off_ecsa_beacon;
4340 settings->counter_offset_presp[1] = hapd->cs_c_off_ecsa_proberesp;
Sunil Ravi72e01222024-03-09 01:25:43 +00004341 settings->link_id = -1;
4342#ifdef CONFIG_IEEE80211BE
4343 if (hapd->conf->mld_ap)
4344 settings->link_id = hapd->mld_link_id;
4345#endif /* CONFIG_IEEE80211BE */
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004346
4347 return 0;
4348}
4349
4350
4351void hostapd_cleanup_cs_params(struct hostapd_data *hapd)
4352{
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004353 os_memset(&hapd->cs_freq_params, 0, sizeof(hapd->cs_freq_params));
4354 hapd->cs_count = 0;
4355 hapd->cs_block_tx = 0;
4356 hapd->cs_c_off_beacon = 0;
4357 hapd->cs_c_off_proberesp = 0;
4358 hapd->csa_in_progress = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004359 hapd->cs_c_off_ecsa_beacon = 0;
4360 hapd->cs_c_off_ecsa_proberesp = 0;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004361}
4362
4363
Hai Shalom60840252021-02-19 19:02:11 -08004364void hostapd_chan_switch_config(struct hostapd_data *hapd,
4365 struct hostapd_freq_params *freq_params)
Roshan Pius3a1667e2018-07-03 15:17:14 -07004366{
Sunil Ravia04bd252022-05-02 22:54:18 -07004367 if (freq_params->eht_enabled)
4368 hapd->iconf->ch_switch_eht_config |= CH_SWITCH_EHT_ENABLED;
4369 else
4370 hapd->iconf->ch_switch_eht_config |= CH_SWITCH_EHT_DISABLED;
4371
Hai Shalom60840252021-02-19 19:02:11 -08004372 if (freq_params->he_enabled)
4373 hapd->iconf->ch_switch_he_config |= CH_SWITCH_HE_ENABLED;
4374 else
4375 hapd->iconf->ch_switch_he_config |= CH_SWITCH_HE_DISABLED;
4376
4377 if (freq_params->vht_enabled)
Roshan Pius3a1667e2018-07-03 15:17:14 -07004378 hapd->iconf->ch_switch_vht_config |= CH_SWITCH_VHT_ENABLED;
4379 else
4380 hapd->iconf->ch_switch_vht_config |= CH_SWITCH_VHT_DISABLED;
4381
4382 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
Hai Shalom60840252021-02-19 19:02:11 -08004383 HOSTAPD_LEVEL_INFO,
Sunil Ravia04bd252022-05-02 22:54:18 -07004384 "CHAN_SWITCH EHT config 0x%x HE config 0x%x VHT config 0x%x",
4385 hapd->iconf->ch_switch_eht_config,
Hai Shalom60840252021-02-19 19:02:11 -08004386 hapd->iconf->ch_switch_he_config,
Roshan Pius3a1667e2018-07-03 15:17:14 -07004387 hapd->iconf->ch_switch_vht_config);
4388}
4389
4390
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004391int hostapd_switch_channel(struct hostapd_data *hapd,
4392 struct csa_settings *settings)
4393{
4394 int ret;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004395
4396 if (!(hapd->iface->drv_flags & WPA_DRIVER_FLAGS_AP_CSA)) {
4397 wpa_printf(MSG_INFO, "CSA is not supported");
4398 return -1;
4399 }
4400
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004401 ret = hostapd_fill_csa_settings(hapd, settings);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004402 if (ret)
4403 return ret;
4404
4405 ret = hostapd_drv_switch_channel(hapd, settings);
4406 free_beacon_data(&settings->beacon_csa);
4407 free_beacon_data(&settings->beacon_after);
4408
4409 if (ret) {
4410 /* if we failed, clean cs parameters */
4411 hostapd_cleanup_cs_params(hapd);
4412 return ret;
4413 }
4414
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004415 hapd->csa_in_progress = 1;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004416 return 0;
4417}
4418
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004419
4420void
4421hostapd_switch_channel_fallback(struct hostapd_iface *iface,
4422 const struct hostapd_freq_params *freq_params)
4423{
Sunil8cd6f4d2022-06-28 18:40:46 +00004424 int seg0_idx = 0, seg1_idx = 0;
4425 enum oper_chan_width bw = CONF_OPER_CHWIDTH_USE_HT;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004426
4427 wpa_printf(MSG_DEBUG, "Restarting all CSA-related BSSes");
4428
4429 if (freq_params->center_freq1)
Hai Shalom81f62d82019-07-22 12:10:00 -07004430 seg0_idx = 36 + (freq_params->center_freq1 - 5180) / 5;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004431 if (freq_params->center_freq2)
Hai Shalom81f62d82019-07-22 12:10:00 -07004432 seg1_idx = 36 + (freq_params->center_freq2 - 5180) / 5;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004433
4434 switch (freq_params->bandwidth) {
4435 case 0:
4436 case 20:
4437 case 40:
Sunil8cd6f4d2022-06-28 18:40:46 +00004438 bw = CONF_OPER_CHWIDTH_USE_HT;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004439 break;
4440 case 80:
Sunil Ravi72e01222024-03-09 01:25:43 +00004441 if (freq_params->center_freq2) {
Sunil8cd6f4d2022-06-28 18:40:46 +00004442 bw = CONF_OPER_CHWIDTH_80P80MHZ;
Sunil Ravi72e01222024-03-09 01:25:43 +00004443 iface->conf->vht_capab |=
4444 VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ;
4445 } else {
Sunil8cd6f4d2022-06-28 18:40:46 +00004446 bw = CONF_OPER_CHWIDTH_80MHZ;
Sunil Ravi72e01222024-03-09 01:25:43 +00004447 }
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004448 break;
4449 case 160:
Sunil8cd6f4d2022-06-28 18:40:46 +00004450 bw = CONF_OPER_CHWIDTH_160MHZ;
Sunil Ravi72e01222024-03-09 01:25:43 +00004451 iface->conf->vht_capab |= VHT_CAP_SUPP_CHAN_WIDTH_160MHZ;
Sunil8cd6f4d2022-06-28 18:40:46 +00004452 break;
4453 case 320:
4454 bw = CONF_OPER_CHWIDTH_320MHZ;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004455 break;
4456 default:
4457 wpa_printf(MSG_WARNING, "Unknown CSA bandwidth: %d",
4458 freq_params->bandwidth);
4459 break;
4460 }
4461
4462 iface->freq = freq_params->freq;
4463 iface->conf->channel = freq_params->channel;
4464 iface->conf->secondary_channel = freq_params->sec_channel_offset;
Hai Shalom81f62d82019-07-22 12:10:00 -07004465 hostapd_set_oper_centr_freq_seg0_idx(iface->conf, seg0_idx);
4466 hostapd_set_oper_centr_freq_seg1_idx(iface->conf, seg1_idx);
4467 hostapd_set_oper_chwidth(iface->conf, bw);
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004468 iface->conf->ieee80211n = freq_params->ht_enabled;
4469 iface->conf->ieee80211ac = freq_params->vht_enabled;
Hai Shalom81f62d82019-07-22 12:10:00 -07004470 iface->conf->ieee80211ax = freq_params->he_enabled;
Sunil Ravia04bd252022-05-02 22:54:18 -07004471 iface->conf->ieee80211be = freq_params->eht_enabled;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004472
4473 /*
4474 * cs_params must not be cleared earlier because the freq_params
4475 * argument may actually point to one of these.
Hai Shalom39ba6fc2019-01-22 12:40:38 -08004476 * These params will be cleared during interface disable below.
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004477 */
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004478 hostapd_disable_iface(iface);
4479 hostapd_enable_iface(iface);
4480}
4481
Sunil Ravia04bd252022-05-02 22:54:18 -07004482
4483#ifdef CONFIG_IEEE80211AX
4484
4485void hostapd_cleanup_cca_params(struct hostapd_data *hapd)
4486{
4487 hapd->cca_count = 0;
4488 hapd->cca_color = 0;
4489 hapd->cca_c_off_beacon = 0;
4490 hapd->cca_c_off_proberesp = 0;
4491 hapd->cca_in_progress = false;
4492}
4493
4494
4495static int hostapd_fill_cca_settings(struct hostapd_data *hapd,
4496 struct cca_settings *settings)
4497{
4498 struct hostapd_iface *iface = hapd->iface;
4499 u8 old_color;
4500 int ret;
4501
4502 if (!iface || iface->conf->he_op.he_bss_color_disabled)
4503 return -1;
4504
4505 old_color = iface->conf->he_op.he_bss_color;
4506 iface->conf->he_op.he_bss_color = hapd->cca_color;
4507 ret = hostapd_build_beacon_data(hapd, &settings->beacon_after);
4508 if (ret)
4509 return ret;
4510
4511 iface->conf->he_op.he_bss_color = old_color;
4512
4513 settings->cca_count = hapd->cca_count;
4514 settings->cca_color = hapd->cca_color,
4515 hapd->cca_in_progress = true;
4516
4517 ret = hostapd_build_beacon_data(hapd, &settings->beacon_cca);
4518 if (ret) {
4519 free_beacon_data(&settings->beacon_after);
4520 return ret;
4521 }
4522
4523 settings->counter_offset_beacon = hapd->cca_c_off_beacon;
4524 settings->counter_offset_presp = hapd->cca_c_off_proberesp;
4525
4526 return 0;
4527}
4528
4529
4530static void hostapd_switch_color_timeout_handler(void *eloop_data,
4531 void *user_ctx)
4532{
4533 struct hostapd_data *hapd = (struct hostapd_data *) eloop_data;
4534 os_time_t delta_t;
4535 unsigned int b;
4536 int i, r;
4537
4538 /* CCA can be triggered once the handler constantly receives
4539 * color collision events to for at least
4540 * DOT11BSS_COLOR_COLLISION_AP_PERIOD (50 s by default). */
4541 delta_t = hapd->last_color_collision.sec -
4542 hapd->first_color_collision.sec;
4543 if (delta_t < DOT11BSS_COLOR_COLLISION_AP_PERIOD)
4544 return;
4545
4546 r = os_random() % HE_OPERATION_BSS_COLOR_MAX;
4547 for (i = 0; i < HE_OPERATION_BSS_COLOR_MAX; i++) {
Sunil8cd6f4d2022-06-28 18:40:46 +00004548 if (r && !(hapd->color_collision_bitmap & (1ULL << r)))
Sunil Ravia04bd252022-05-02 22:54:18 -07004549 break;
4550
4551 r = (r + 1) % HE_OPERATION_BSS_COLOR_MAX;
4552 }
4553
4554 if (i == HE_OPERATION_BSS_COLOR_MAX) {
4555 /* There are no free colors so turn BSS coloring off */
4556 wpa_printf(MSG_INFO,
4557 "No free colors left, turning off BSS coloring");
4558 hapd->iface->conf->he_op.he_bss_color_disabled = 1;
4559 hapd->iface->conf->he_op.he_bss_color = os_random() % 63 + 1;
4560 for (b = 0; b < hapd->iface->num_bss; b++)
4561 ieee802_11_set_beacon(hapd->iface->bss[b]);
4562 return;
4563 }
4564
4565 for (b = 0; b < hapd->iface->num_bss; b++) {
4566 struct hostapd_data *bss = hapd->iface->bss[b];
4567 struct cca_settings settings;
4568 int ret;
4569
4570 hostapd_cleanup_cca_params(bss);
4571 bss->cca_color = r;
4572 bss->cca_count = 10;
4573
4574 if (hostapd_fill_cca_settings(bss, &settings)) {
4575 hostapd_cleanup_cca_params(bss);
4576 continue;
4577 }
4578
4579 ret = hostapd_drv_switch_color(bss, &settings);
4580 if (ret)
4581 hostapd_cleanup_cca_params(bss);
4582
4583 free_beacon_data(&settings.beacon_cca);
4584 free_beacon_data(&settings.beacon_after);
4585 }
4586}
4587
4588
4589void hostapd_switch_color(struct hostapd_data *hapd, u64 bitmap)
4590{
4591 struct os_reltime now;
4592
4593 if (hapd->cca_in_progress)
4594 return;
4595
4596 if (os_get_reltime(&now))
4597 return;
4598
4599 hapd->color_collision_bitmap = bitmap;
4600 hapd->last_color_collision = now;
4601
4602 if (eloop_is_timeout_registered(hostapd_switch_color_timeout_handler,
4603 hapd, NULL))
4604 return;
4605
4606 hapd->first_color_collision = now;
4607 /* 10 s window as margin for persistent color collision reporting */
4608 eloop_register_timeout(DOT11BSS_COLOR_COLLISION_AP_PERIOD + 10, 0,
4609 hostapd_switch_color_timeout_handler,
4610 hapd, NULL);
4611}
4612
4613#endif /* CONFIG_IEEE80211AX */
4614
Dmitry Shmidte4663042016-04-04 10:07:49 -07004615#endif /* NEED_AP_MLME */
4616
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004617
4618struct hostapd_data * hostapd_get_iface(struct hapd_interfaces *interfaces,
4619 const char *ifname)
4620{
4621 size_t i, j;
4622
4623 for (i = 0; i < interfaces->count; i++) {
4624 struct hostapd_iface *iface = interfaces->iface[i];
4625
4626 for (j = 0; j < iface->num_bss; j++) {
4627 struct hostapd_data *hapd = iface->bss[j];
4628
4629 if (os_strcmp(ifname, hapd->conf->iface) == 0)
4630 return hapd;
4631 }
4632 }
4633
4634 return NULL;
4635}
4636
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004637
4638void hostapd_periodic_iface(struct hostapd_iface *iface)
4639{
4640 size_t i;
4641
4642 ap_list_timer(iface);
4643
4644 for (i = 0; i < iface->num_bss; i++) {
4645 struct hostapd_data *hapd = iface->bss[i];
4646
4647 if (!hapd->started)
4648 continue;
4649
4650#ifndef CONFIG_NO_RADIUS
4651 hostapd_acl_expire(hapd);
4652#endif /* CONFIG_NO_RADIUS */
4653 }
4654}
Hai Shalom899fcc72020-10-19 14:38:18 -07004655
4656
4657#ifdef CONFIG_OCV
4658void hostapd_ocv_check_csa_sa_query(void *eloop_ctx, void *timeout_ctx)
4659{
4660 struct hostapd_data *hapd = eloop_ctx;
4661 struct sta_info *sta;
4662
4663 wpa_printf(MSG_DEBUG, "OCV: Post-CSA SA Query initiation check");
4664
4665 for (sta = hapd->sta_list; sta; sta = sta->next) {
4666 if (!sta->post_csa_sa_query)
4667 continue;
4668
4669 wpa_printf(MSG_DEBUG, "OCV: OCVC STA " MACSTR
4670 " did not start SA Query after CSA - disconnect",
4671 MAC2STR(sta->addr));
4672 ap_sta_disconnect(hapd, sta, sta->addr,
4673 WLAN_REASON_PREV_AUTH_NOT_VALID);
4674 }
4675}
4676#endif /* CONFIG_OCV */
Sunil Ravi2a14cf12023-11-21 00:54:38 +00004677
4678
4679#ifdef CONFIG_IEEE80211BE
Sunil Raviaf399a82024-05-05 20:56:55 +00004680
Sunil Ravi2a14cf12023-11-21 00:54:38 +00004681struct hostapd_data * hostapd_mld_get_link_bss(struct hostapd_data *hapd,
4682 u8 link_id)
4683{
4684 unsigned int i;
4685
4686 for (i = 0; i < hapd->iface->interfaces->count; i++) {
4687 struct hostapd_iface *h = hapd->iface->interfaces->iface[i];
4688 struct hostapd_data *h_hapd = h->bss[0];
Sunil Ravi2a14cf12023-11-21 00:54:38 +00004689
Sunil Raviaf399a82024-05-05 20:56:55 +00004690 if (!hostapd_is_ml_partner(hapd, h_hapd))
Sunil Ravi2a14cf12023-11-21 00:54:38 +00004691 continue;
4692
4693 if (h_hapd->mld_link_id == link_id)
4694 return h_hapd;
4695 }
4696
4697 return NULL;
4698}
Sunil Raviaf399a82024-05-05 20:56:55 +00004699
4700
4701bool hostapd_is_ml_partner(struct hostapd_data *hapd1,
4702 struct hostapd_data *hapd2)
4703{
4704 if (!hapd1->conf->mld_ap || !hapd2->conf->mld_ap)
4705 return false;
4706
4707 return !os_strcmp(hapd1->conf->iface, hapd2->conf->iface);
4708}
4709
4710
4711u8 hostapd_get_mld_id(struct hostapd_data *hapd)
4712{
4713 if (!hapd->conf->mld_ap)
4714 return 255;
4715
4716 /* MLD ID 0 represents self */
4717 return 0;
4718
4719 /* TODO: MLD ID for Multiple BSS cases */
4720}
4721
4722
4723int hostapd_mld_add_link(struct hostapd_data *hapd)
4724{
4725 struct hostapd_mld *mld = hapd->mld;
4726
4727 if (!hapd->conf->mld_ap)
4728 return 0;
4729
4730 /* Should not happen */
4731 if (!mld)
4732 return -1;
4733
4734 dl_list_add_tail(&mld->links, &hapd->link);
4735 mld->num_links++;
4736
4737 wpa_printf(MSG_DEBUG, "AP MLD %s: Link ID %d added. num_links: %d",
4738 mld->name, hapd->mld_link_id, mld->num_links);
4739
4740 if (mld->fbss)
4741 return 0;
4742
4743 mld->fbss = hapd;
4744 wpa_printf(MSG_DEBUG, "AP MLD %s: First link BSS set to %p",
4745 mld->name, mld->fbss);
4746 return 0;
4747}
4748
4749
4750int hostapd_mld_remove_link(struct hostapd_data *hapd)
4751{
4752 struct hostapd_mld *mld = hapd->mld;
4753 struct hostapd_data *next_fbss;
4754
4755 if (!hapd->conf->mld_ap)
4756 return 0;
4757
4758 /* Should not happen */
4759 if (!mld)
4760 return -1;
4761
4762 dl_list_del(&hapd->link);
4763 mld->num_links--;
4764
4765 wpa_printf(MSG_DEBUG, "AP MLD %s: Link ID %d removed. num_links: %d",
4766 mld->name, hapd->mld_link_id, mld->num_links);
4767
4768 if (mld->fbss != hapd)
4769 return 0;
4770
4771 /* If the list is empty, all links are removed */
4772 if (dl_list_empty(&mld->links)) {
4773 mld->fbss = NULL;
4774 } else {
4775 next_fbss = dl_list_entry(mld->links.next, struct hostapd_data,
4776 link);
4777 mld->fbss = next_fbss;
4778 }
4779
4780 wpa_printf(MSG_DEBUG, "AP MLD %s: First link BSS set to %p",
4781 mld->name, mld->fbss);
4782 return 0;
4783}
4784
4785
4786bool hostapd_mld_is_first_bss(struct hostapd_data *hapd)
4787{
4788 struct hostapd_mld *mld = hapd->mld;
4789
4790 if (!hapd->conf->mld_ap)
4791 return true;
4792
4793 /* Should not happen */
4794 if (!mld)
4795 return false;
4796
4797 /* If fbss is not set, it is safe to assume the caller is the first BSS.
4798 */
4799 if (!mld->fbss)
4800 return true;
4801
4802 return hapd == mld->fbss;
4803}
4804
4805
4806struct hostapd_data * hostapd_mld_get_first_bss(struct hostapd_data *hapd)
4807{
4808 struct hostapd_mld *mld = hapd->mld;
4809
4810 if (!hapd->conf->mld_ap)
4811 return NULL;
4812
4813 /* Should not happen */
4814 if (!mld)
4815 return NULL;
4816
4817 return mld->fbss;
4818}
4819
Sunil Ravi2a14cf12023-11-21 00:54:38 +00004820#endif /* CONFIG_IEEE80211BE */
Sunil Raviaf399a82024-05-05 20:56:55 +00004821
4822
4823u16 hostapd_get_punct_bitmap(struct hostapd_data *hapd)
4824{
4825 u16 punct_bitmap = 0;
4826
4827#ifdef CONFIG_IEEE80211BE
4828 punct_bitmap = hapd->iconf->punct_bitmap;
4829#ifdef CONFIG_TESTING_OPTIONS
4830 if (!punct_bitmap)
4831 punct_bitmap = hapd->conf->eht_oper_puncturing_override;
4832#endif /* CONFIG_TESTING_OPTIONS */
4833#endif /* CONFIG_IEEE80211BE */
4834
4835 return punct_bitmap;
4836}