blob: 7d924893f17b39211848fa91af9f9cff342dac6b [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * hostapd / Initialization and configuration
Hai Shaloma20dcd72022-02-04 13:43:00 -08003 * Copyright (c) 2002-2021, Jouni Malinen <j@w1.fi>
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004 *
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007 */
8
9#include "utils/includes.h"
Hai Shalomc3565922019-10-28 11:58:20 -070010#ifdef CONFIG_SQLITE
11#include <sqlite3.h>
12#endif /* CONFIG_SQLITE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070013
14#include "utils/common.h"
15#include "utils/eloop.h"
Hai Shalomfdcde762020-04-02 11:19:20 -070016#include "utils/crc32.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070017#include "common/ieee802_11_defs.h"
Dmitry Shmidtcce06662013-11-04 18:44:24 -080018#include "common/wpa_ctrl.h"
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080019#include "common/hw_features_common.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070020#include "radius/radius_client.h"
Dmitry Shmidt04949592012-07-19 12:16:46 -070021#include "radius/radius_das.h"
Dmitry Shmidt50b691d2014-05-21 14:01:45 -070022#include "eap_server/tncs.h"
Dmitry Shmidt2f74e362015-01-21 13:19:05 -080023#include "eapol_auth/eapol_auth_sm.h"
24#include "eapol_auth/eapol_auth_sm_i.h"
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080025#include "fst/fst.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070026#include "hostapd.h"
27#include "authsrv.h"
28#include "sta_info.h"
29#include "accounting.h"
30#include "ap_list.h"
31#include "beacon.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070032#include "ieee802_1x.h"
33#include "ieee802_11_auth.h"
34#include "vlan_init.h"
35#include "wpa_auth.h"
36#include "wps_hostapd.h"
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070037#include "dpp_hostapd.h"
Sunil Ravib0ac25f2024-07-12 01:42:03 +000038#include "nan_usd_ap.h"
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070039#include "gas_query_ap.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070040#include "hw_features.h"
41#include "wpa_auth_glue.h"
42#include "ap_drv_ops.h"
43#include "ap_config.h"
44#include "p2p_hostapd.h"
Dmitry Shmidt04949592012-07-19 12:16:46 -070045#include "gas_serv.h"
Dmitry Shmidt051af732013-10-22 13:52:46 -070046#include "dfs.h"
Dmitry Shmidt7832adb2014-04-29 10:53:02 -070047#include "ieee802_11.h"
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080048#include "bss_load.h"
49#include "x_snoop.h"
50#include "dhcp_snoop.h"
51#include "ndisc_snoop.h"
Dmitry Shmidt849734c2016-05-27 09:59:01 -070052#include "neighbor_db.h"
53#include "rrm.h"
Dmitry Shmidtebd93af2017-02-21 13:40:44 -080054#include "fils_hlp.h"
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070055#include "acs.h"
Roshan Pius3a1667e2018-07-03 15:17:14 -070056#include "hs20.h"
Hai Shalom81f62d82019-07-22 12:10:00 -070057#include "airtime_policy.h"
58#include "wpa_auth_kay.h"
Sunil Ravi2a14cf12023-11-21 00:54:38 +000059#include "hw_features.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070060
61
Dmitry Shmidt04949592012-07-19 12:16:46 -070062static int hostapd_flush_old_stations(struct hostapd_data *hapd, u16 reason);
Hai Shalomfdcde762020-04-02 11:19:20 -070063#ifdef CONFIG_WEP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070064static int hostapd_setup_encryption(char *iface, struct hostapd_data *hapd);
Dmitry Shmidtc55524a2011-07-07 11:18:38 -070065static int hostapd_broadcast_wep_clear(struct hostapd_data *hapd);
Hai Shalomfdcde762020-04-02 11:19:20 -070066#endif /* CONFIG_WEP */
Dmitry Shmidtcce06662013-11-04 18:44:24 -080067static int setup_interface2(struct hostapd_iface *iface);
68static void channel_list_update_timeout(void *eloop_ctx, void *timeout_ctx);
Roshan Pius3a1667e2018-07-03 15:17:14 -070069static void hostapd_interface_setup_failure_handler(void *eloop_ctx,
70 void *timeout_ctx);
Sunil Ravia04bd252022-05-02 22:54:18 -070071#ifdef CONFIG_IEEE80211AX
72static void hostapd_switch_color_timeout_handler(void *eloop_data,
73 void *user_ctx);
74#endif /* CONFIG_IEEE80211AX */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070075
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070076
Dmitry Shmidt04949592012-07-19 12:16:46 -070077int hostapd_for_each_interface(struct hapd_interfaces *interfaces,
78 int (*cb)(struct hostapd_iface *iface,
79 void *ctx), void *ctx)
80{
81 size_t i;
82 int ret;
83
84 for (i = 0; i < interfaces->count; i++) {
Hai Shalom4fbc08f2020-05-18 12:37:00 -070085 if (!interfaces->iface[i])
86 continue;
Dmitry Shmidt04949592012-07-19 12:16:46 -070087 ret = cb(interfaces->iface[i], ctx);
88 if (ret)
89 return ret;
90 }
91
92 return 0;
93}
94
95
Sunil Ravi77d572f2023-01-17 23:58:31 +000096struct hostapd_data * hostapd_mbssid_get_tx_bss(struct hostapd_data *hapd)
97{
98 if (hapd->iconf->mbssid)
99 return hapd->iface->bss[0];
100
101 return hapd;
102}
103
104
105int hostapd_mbssid_get_bss_index(struct hostapd_data *hapd)
106{
107 if (hapd->iconf->mbssid) {
108 size_t i;
109
110 for (i = 1; i < hapd->iface->num_bss; i++)
111 if (hapd->iface->bss[i] == hapd)
112 return i;
113 }
114
115 return 0;
116}
117
118
Hai Shalomce48b4a2018-09-05 11:41:35 -0700119void hostapd_reconfig_encryption(struct hostapd_data *hapd)
120{
121 if (hapd->wpa_auth)
122 return;
123
124 hostapd_set_privacy(hapd, 0);
Hai Shalomfdcde762020-04-02 11:19:20 -0700125#ifdef CONFIG_WEP
Hai Shalomce48b4a2018-09-05 11:41:35 -0700126 hostapd_setup_encryption(hapd->conf->iface, hapd);
Hai Shalomfdcde762020-04-02 11:19:20 -0700127#endif /* CONFIG_WEP */
Hai Shalomce48b4a2018-09-05 11:41:35 -0700128}
129
130
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700131static void hostapd_reload_bss(struct hostapd_data *hapd)
132{
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800133 struct hostapd_ssid *ssid;
134
Dmitry Shmidt29333592017-01-09 12:27:11 -0800135 if (!hapd->started)
136 return;
137
Roshan Pius3a1667e2018-07-03 15:17:14 -0700138 if (hapd->conf->wmm_enabled < 0)
Hai Shaloma20dcd72022-02-04 13:43:00 -0800139 hapd->conf->wmm_enabled = hapd->iconf->ieee80211n |
140 hapd->iconf->ieee80211ax;
Roshan Pius3a1667e2018-07-03 15:17:14 -0700141
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700142#ifndef CONFIG_NO_RADIUS
143 radius_client_reconfig(hapd->radius, hapd->conf->radius);
144#endif /* CONFIG_NO_RADIUS */
145
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800146 ssid = &hapd->conf->ssid;
147 if (!ssid->wpa_psk_set && ssid->wpa_psk && !ssid->wpa_psk->next &&
148 ssid->wpa_passphrase_set && ssid->wpa_passphrase) {
149 /*
150 * Force PSK to be derived again since SSID or passphrase may
151 * have changed.
152 */
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800153 hostapd_config_clear_wpa_psk(&hapd->conf->ssid.wpa_psk);
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800154 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700155 if (hostapd_setup_wpa_psk(hapd->conf)) {
156 wpa_printf(MSG_ERROR, "Failed to re-configure WPA PSK "
157 "after reloading configuration");
158 }
159
160 if (hapd->conf->ieee802_1x || hapd->conf->wpa)
161 hostapd_set_drv_ieee8021x(hapd, hapd->conf->iface, 1);
162 else
163 hostapd_set_drv_ieee8021x(hapd, hapd->conf->iface, 0);
164
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800165 if ((hapd->conf->wpa || hapd->conf->osen) && hapd->wpa_auth == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700166 hostapd_setup_wpa(hapd);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800167 if (hapd->wpa_auth)
168 wpa_init_keys(hapd->wpa_auth);
169 } else if (hapd->conf->wpa) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700170 const u8 *wpa_ie;
171 size_t wpa_ie_len;
172 hostapd_reconfig_wpa(hapd);
173 wpa_ie = wpa_auth_get_wpa_ie(hapd->wpa_auth, &wpa_ie_len);
174 if (hostapd_set_generic_elem(hapd, wpa_ie, wpa_ie_len))
175 wpa_printf(MSG_ERROR, "Failed to configure WPA IE for "
176 "the kernel driver.");
177 } else if (hapd->wpa_auth) {
178 wpa_deinit(hapd->wpa_auth);
179 hapd->wpa_auth = NULL;
180 hostapd_set_privacy(hapd, 0);
Hai Shalomfdcde762020-04-02 11:19:20 -0700181#ifdef CONFIG_WEP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700182 hostapd_setup_encryption(hapd->conf->iface, hapd);
Hai Shalomfdcde762020-04-02 11:19:20 -0700183#endif /* CONFIG_WEP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700184 hostapd_set_generic_elem(hapd, (u8 *) "", 0);
185 }
186
Sunil Ravi99c035e2024-07-12 01:42:03 +0000187 hostapd_neighbor_sync_own_report(hapd);
188
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700189 ieee802_11_set_beacon(hapd);
190 hostapd_update_wps(hapd);
191
192 if (hapd->conf->ssid.ssid_set &&
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700193 hostapd_set_ssid(hapd, hapd->conf->ssid.ssid,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700194 hapd->conf->ssid.ssid_len)) {
195 wpa_printf(MSG_ERROR, "Could not set SSID for kernel driver");
196 /* try to continue */
197 }
198 wpa_printf(MSG_DEBUG, "Reconfigured interface %s", hapd->conf->iface);
199}
200
201
Sunil Ravi77d572f2023-01-17 23:58:31 +0000202static void hostapd_clear_old_bss(struct hostapd_data *bss)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700203{
Sunil Ravi77d572f2023-01-17 23:58:31 +0000204 wpa_printf(MSG_DEBUG, "BSS %s changed - clear old state",
205 bss->conf->iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700206
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700207 /*
208 * Deauthenticate all stations since the new configuration may not
209 * allow them to use the BSS anymore.
210 */
Sunil Ravi77d572f2023-01-17 23:58:31 +0000211 hostapd_flush_old_stations(bss, WLAN_REASON_PREV_AUTH_NOT_VALID);
Hai Shalomfdcde762020-04-02 11:19:20 -0700212#ifdef CONFIG_WEP
Sunil Ravi77d572f2023-01-17 23:58:31 +0000213 hostapd_broadcast_wep_clear(bss);
Hai Shalomfdcde762020-04-02 11:19:20 -0700214#endif /* CONFIG_WEP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700215
216#ifndef CONFIG_NO_RADIUS
Sunil Ravi77d572f2023-01-17 23:58:31 +0000217 /* TODO: update dynamic data based on changed configuration
218 * items (e.g., open/close sockets, etc.) */
219 radius_client_flush(bss->radius, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700220#endif /* CONFIG_NO_RADIUS */
Sunil Ravi77d572f2023-01-17 23:58:31 +0000221}
222
223
224static void hostapd_clear_old(struct hostapd_iface *iface)
225{
226 size_t j;
227
228 for (j = 0; j < iface->num_bss; j++)
229 hostapd_clear_old_bss(iface->bss[j]);
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700230}
231
232
Hai Shalom74f70d42019-02-11 14:42:39 -0800233static int hostapd_iface_conf_changed(struct hostapd_config *newconf,
234 struct hostapd_config *oldconf)
235{
236 size_t i;
237
238 if (newconf->num_bss != oldconf->num_bss)
239 return 1;
240
241 for (i = 0; i < newconf->num_bss; i++) {
242 if (os_strcmp(newconf->bss[i]->iface,
243 oldconf->bss[i]->iface) != 0)
244 return 1;
245 }
246
247 return 0;
248}
249
250
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700251int hostapd_reload_config(struct hostapd_iface *iface)
252{
Hai Shalom74f70d42019-02-11 14:42:39 -0800253 struct hapd_interfaces *interfaces = iface->interfaces;
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700254 struct hostapd_data *hapd = iface->bss[0];
255 struct hostapd_config *newconf, *oldconf;
256 size_t j;
257
258 if (iface->config_fname == NULL) {
259 /* Only in-memory config in use - assume it has been updated */
260 hostapd_clear_old(iface);
261 for (j = 0; j < iface->num_bss; j++)
262 hostapd_reload_bss(iface->bss[j]);
263 return 0;
264 }
265
266 if (iface->interfaces == NULL ||
267 iface->interfaces->config_read_cb == NULL)
268 return -1;
269 newconf = iface->interfaces->config_read_cb(iface->config_fname);
270 if (newconf == NULL)
271 return -1;
272
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700273 oldconf = hapd->iconf;
Hai Shalom74f70d42019-02-11 14:42:39 -0800274 if (hostapd_iface_conf_changed(newconf, oldconf)) {
275 char *fname;
276 int res;
277
Sunil Ravi77d572f2023-01-17 23:58:31 +0000278 hostapd_clear_old(iface);
279
Hai Shalom74f70d42019-02-11 14:42:39 -0800280 wpa_printf(MSG_DEBUG,
281 "Configuration changes include interface/BSS modification - force full disable+enable sequence");
282 fname = os_strdup(iface->config_fname);
283 if (!fname) {
284 hostapd_config_free(newconf);
285 return -1;
286 }
287 hostapd_remove_iface(interfaces, hapd->conf->iface);
288 iface = hostapd_init(interfaces, fname);
289 os_free(fname);
290 hostapd_config_free(newconf);
291 if (!iface) {
292 wpa_printf(MSG_ERROR,
293 "Failed to initialize interface on config reload");
294 return -1;
295 }
296 iface->interfaces = interfaces;
297 interfaces->iface[interfaces->count] = iface;
298 interfaces->count++;
299 res = hostapd_enable_iface(iface);
300 if (res < 0)
301 wpa_printf(MSG_ERROR,
302 "Failed to enable interface on config reload");
303 return res;
304 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700305 iface->conf = newconf;
306
307 for (j = 0; j < iface->num_bss; j++) {
308 hapd = iface->bss[j];
Sunil Ravi77d572f2023-01-17 23:58:31 +0000309 if (!hapd->conf->config_id || !newconf->bss[j]->config_id ||
310 os_strcmp(hapd->conf->config_id,
311 newconf->bss[j]->config_id) != 0)
312 hostapd_clear_old_bss(hapd);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700313 hapd->iconf = newconf;
Dmitry Shmidtd11f0192014-03-24 12:09:47 -0700314 hapd->iconf->channel = oldconf->channel;
Dmitry Shmidtdda10c22015-03-24 16:05:01 -0700315 hapd->iconf->acs = oldconf->acs;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -0700316 hapd->iconf->secondary_channel = oldconf->secondary_channel;
Dmitry Shmidtd11f0192014-03-24 12:09:47 -0700317 hapd->iconf->ieee80211n = oldconf->ieee80211n;
318 hapd->iconf->ieee80211ac = oldconf->ieee80211ac;
319 hapd->iconf->ht_capab = oldconf->ht_capab;
320 hapd->iconf->vht_capab = oldconf->vht_capab;
Hai Shalom81f62d82019-07-22 12:10:00 -0700321 hostapd_set_oper_chwidth(hapd->iconf,
322 hostapd_get_oper_chwidth(oldconf));
323 hostapd_set_oper_centr_freq_seg0_idx(
324 hapd->iconf,
325 hostapd_get_oper_centr_freq_seg0_idx(oldconf));
326 hostapd_set_oper_centr_freq_seg1_idx(
327 hapd->iconf,
328 hostapd_get_oper_centr_freq_seg1_idx(oldconf));
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800329 hapd->conf = newconf->bss[j];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700330 hostapd_reload_bss(hapd);
331 }
332
333 hostapd_config_free(oldconf);
334
335
336 return 0;
337}
338
339
Hai Shalomfdcde762020-04-02 11:19:20 -0700340#ifdef CONFIG_WEP
341
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700342static void hostapd_broadcast_key_clear_iface(struct hostapd_data *hapd,
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700343 const char *ifname)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700344{
345 int i;
346
Dmitry Shmidt29333592017-01-09 12:27:11 -0800347 if (!ifname || !hapd->drv_priv)
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700348 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700349 for (i = 0; i < NUM_WEP_KEYS; i++) {
Hai Shalomfdcde762020-04-02 11:19:20 -0700350 if (hostapd_drv_set_key(ifname, hapd, WPA_ALG_NONE, NULL, i, 0,
351 0, NULL, 0, NULL, 0, KEY_FLAG_GROUP)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700352 wpa_printf(MSG_DEBUG, "Failed to clear default "
353 "encryption keys (ifname=%s keyidx=%d)",
354 ifname, i);
355 }
356 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700357 if (hapd->conf->ieee80211w) {
358 for (i = NUM_WEP_KEYS; i < NUM_WEP_KEYS + 2; i++) {
359 if (hostapd_drv_set_key(ifname, hapd, WPA_ALG_NONE,
Hai Shalomfdcde762020-04-02 11:19:20 -0700360 NULL, i, 0, 0, NULL,
361 0, NULL, 0, KEY_FLAG_GROUP)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700362 wpa_printf(MSG_DEBUG, "Failed to clear "
363 "default mgmt encryption keys "
364 "(ifname=%s keyidx=%d)", ifname, i);
365 }
366 }
367 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700368}
369
370
371static int hostapd_broadcast_wep_clear(struct hostapd_data *hapd)
372{
373 hostapd_broadcast_key_clear_iface(hapd, hapd->conf->iface);
374 return 0;
375}
376
377
378static int hostapd_broadcast_wep_set(struct hostapd_data *hapd)
379{
380 int errors = 0, idx;
381 struct hostapd_ssid *ssid = &hapd->conf->ssid;
382
383 idx = ssid->wep.idx;
Hai Shalomfdcde762020-04-02 11:19:20 -0700384 if (ssid->wep.default_len && ssid->wep.key[idx] &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700385 hostapd_drv_set_key(hapd->conf->iface,
Hai Shalomfdcde762020-04-02 11:19:20 -0700386 hapd, WPA_ALG_WEP, broadcast_ether_addr, idx, 0,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700387 1, NULL, 0, ssid->wep.key[idx],
Hai Shalomfdcde762020-04-02 11:19:20 -0700388 ssid->wep.len[idx],
389 KEY_FLAG_GROUP_RX_TX_DEFAULT)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700390 wpa_printf(MSG_WARNING, "Could not set WEP encryption.");
391 errors++;
392 }
393
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700394 return errors;
395}
396
Hai Shalomfdcde762020-04-02 11:19:20 -0700397#endif /* CONFIG_WEP */
398
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700399
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000400#ifdef CONFIG_IEEE80211BE
401#ifdef CONFIG_TESTING_OPTIONS
402
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000403static void hostapd_link_remove_timeout_handler(void *eloop_data,
404 void *user_ctx)
405{
406 struct hostapd_data *hapd = (struct hostapd_data *) eloop_data;
407
408 if (hapd->eht_mld_link_removal_count == 0)
409 return;
410 hapd->eht_mld_link_removal_count--;
411
412 wpa_printf(MSG_DEBUG, "MLD: Remove link_id=%u in %u beacons",
413 hapd->mld_link_id,
414 hapd->eht_mld_link_removal_count);
415
416 ieee802_11_set_beacon(hapd);
417
418 if (!hapd->eht_mld_link_removal_count) {
Sunil Ravi99c035e2024-07-12 01:42:03 +0000419 hostapd_free_link_stas(hapd);
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000420 hostapd_disable_iface(hapd->iface);
421 return;
422 }
423
424 eloop_register_timeout(0, TU_TO_USEC(hapd->iconf->beacon_int),
425 hostapd_link_remove_timeout_handler,
426 hapd, NULL);
427}
428
429
430int hostapd_link_remove(struct hostapd_data *hapd, u32 count)
431{
432 if (!hapd->conf->mld_ap)
433 return -1;
434
435 wpa_printf(MSG_DEBUG,
436 "MLD: Remove link_id=%u in %u beacons",
437 hapd->mld_link_id, count);
438
439 hapd->eht_mld_link_removal_count = count;
440 hapd->eht_mld_bss_param_change++;
Sunil Ravic0f5d412024-09-11 22:12:49 +0000441 if (hapd->eht_mld_bss_param_change == 255)
442 hapd->eht_mld_bss_param_change = 0;
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000443
444 eloop_register_timeout(0, TU_TO_USEC(hapd->iconf->beacon_int),
445 hostapd_link_remove_timeout_handler,
446 hapd, NULL);
447
448 ieee802_11_set_beacon(hapd);
449 return 0;
450}
451
452#endif /* CONFIG_TESTING_OPTIONS */
453#endif /* CONFIG_IEEE80211BE */
454
455
Hai Shalom60840252021-02-19 19:02:11 -0800456void hostapd_free_hapd_data(struct hostapd_data *hapd)
Dmitry Shmidt04949592012-07-19 12:16:46 -0700457{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800458 os_free(hapd->probereq_cb);
459 hapd->probereq_cb = NULL;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800460 hapd->num_probereq_cb = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800461
462#ifdef CONFIG_P2P
463 wpabuf_free(hapd->p2p_beacon_ie);
464 hapd->p2p_beacon_ie = NULL;
465 wpabuf_free(hapd->p2p_probe_resp_ie);
466 hapd->p2p_probe_resp_ie = NULL;
467#endif /* CONFIG_P2P */
468
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -0800469 if (!hapd->started) {
470 wpa_printf(MSG_ERROR, "%s: Interface %s wasn't started",
Hai Shalom021b0b52019-04-10 11:17:58 -0700471 __func__, hapd->conf ? hapd->conf->iface : "N/A");
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -0800472 return;
473 }
474 hapd->started = 0;
Hai Shalom5f92bc92019-04-18 11:54:11 -0700475 hapd->beacon_set_done = 0;
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -0800476
Dmitry Shmidt54605472013-11-08 11:10:19 -0800477 wpa_printf(MSG_DEBUG, "%s(%s)", __func__, hapd->conf->iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700478 accounting_deinit(hapd);
479 hostapd_deinit_wpa(hapd);
480 vlan_deinit(hapd);
481 hostapd_acl_deinit(hapd);
482#ifndef CONFIG_NO_RADIUS
Sunil Ravi99c035e2024-07-12 01:42:03 +0000483 if (hostapd_mld_is_first_bss(hapd)) {
484#ifdef CONFIG_IEEE80211BE
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000485 struct hapd_interfaces *ifaces = hapd->iface->interfaces;
486 size_t i;
487
488 for (i = 0; i < ifaces->count; i++) {
489 struct hostapd_iface *iface = ifaces->iface[i];
490 size_t j;
491
492 for (j = 0; iface && j < iface->num_bss; j++) {
493 struct hostapd_data *h = iface->bss[j];
494
495 if (hapd == h)
496 continue;
497 if (h->radius == hapd->radius)
498 h->radius = NULL;
499 if (h->radius_das == hapd->radius_das)
500 h->radius_das = NULL;
501 }
502 }
Sunil Ravi99c035e2024-07-12 01:42:03 +0000503#endif /* CONFIG_IEEE80211BE */
Sunil Ravi2a14cf12023-11-21 00:54:38 +0000504 radius_client_deinit(hapd->radius);
505 radius_das_deinit(hapd->radius_das);
506 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700507 hapd->radius = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700508 hapd->radius_das = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700509#endif /* CONFIG_NO_RADIUS */
510
511 hostapd_deinit_wps(hapd);
Hai Shalom81f62d82019-07-22 12:10:00 -0700512 ieee802_1x_dealloc_kay_sm_hapd(hapd);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700513#ifdef CONFIG_DPP
514 hostapd_dpp_deinit(hapd);
515 gas_query_ap_deinit(hapd->gas);
Hai Shaloma20dcd72022-02-04 13:43:00 -0800516 hapd->gas = NULL;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700517#endif /* CONFIG_DPP */
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000518#ifdef CONFIG_NAN_USD
519 hostapd_nan_usd_deinit(hapd);
520#endif /* CONFIG_NAN_USD */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700521
522 authsrv_deinit(hapd);
523
Dmitry Shmidt71757432014-06-02 13:50:35 -0700524 if (hapd->interface_added) {
525 hapd->interface_added = 0;
526 if (hostapd_if_remove(hapd, WPA_IF_AP_BSS, hapd->conf->iface)) {
527 wpa_printf(MSG_WARNING,
528 "Failed to remove BSS interface %s",
529 hapd->conf->iface);
530 hapd->interface_added = 1;
531 } else {
532 /*
533 * Since this was a dynamically added interface, the
534 * driver wrapper may have removed its internal instance
535 * and hapd->drv_priv is not valid anymore.
536 */
Sunil Ravi99c035e2024-07-12 01:42:03 +0000537 hapd->drv_priv = NULL;
Dmitry Shmidt71757432014-06-02 13:50:35 -0700538 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700539 }
540
Sunil Ravi99c035e2024-07-12 01:42:03 +0000541#ifdef CONFIG_IEEE80211BE
542 /* If the interface was not added as well as it is not the first BSS,
543 * at least the link should be removed here since deinit will take care
544 * of only the first BSS. */
545 if (hapd->conf->mld_ap && !hapd->interface_added &&
546 hapd->iface->bss[0] != hapd)
547 hostapd_if_link_remove(hapd, WPA_IF_AP_BSS, hapd->conf->iface,
548 hapd->mld_link_id);
549#endif /* CONFIG_IEEE80211BE */
550
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800551 wpabuf_free(hapd->time_adv);
Hai Shaloma20dcd72022-02-04 13:43:00 -0800552 hapd->time_adv = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700553
554#ifdef CONFIG_INTERWORKING
555 gas_serv_deinit(hapd);
556#endif /* CONFIG_INTERWORKING */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800557
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800558 bss_load_update_deinit(hapd);
559 ndisc_snoop_deinit(hapd);
560 dhcp_snoop_deinit(hapd);
561 x_snoop_deinit(hapd);
562
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800563#ifdef CONFIG_SQLITE
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700564 bin_clear_free(hapd->tmp_eap_user.identity,
565 hapd->tmp_eap_user.identity_len);
566 bin_clear_free(hapd->tmp_eap_user.password,
567 hapd->tmp_eap_user.password_len);
Hai Shaloma20dcd72022-02-04 13:43:00 -0800568 os_memset(&hapd->tmp_eap_user, 0, sizeof(hapd->tmp_eap_user));
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800569#endif /* CONFIG_SQLITE */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800570
571#ifdef CONFIG_MESH
572 wpabuf_free(hapd->mesh_pending_auth);
573 hapd->mesh_pending_auth = NULL;
Hai Shalom60840252021-02-19 19:02:11 -0800574 /* handling setup failure is already done */
575 hapd->setup_complete_cb = NULL;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800576#endif /* CONFIG_MESH */
Dmitry Shmidt849734c2016-05-27 09:59:01 -0700577
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000578#ifndef CONFIG_NO_RRM
Dmitry Shmidt849734c2016-05-27 09:59:01 -0700579 hostapd_clean_rrm(hapd);
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000580#endif /* CONFIG_NO_RRM */
Dmitry Shmidtebd93af2017-02-21 13:40:44 -0800581 fils_hlp_deinit(hapd);
Hai Shalom021b0b52019-04-10 11:17:58 -0700582
Hai Shalom899fcc72020-10-19 14:38:18 -0700583#ifdef CONFIG_OCV
584 eloop_cancel_timeout(hostapd_ocv_check_csa_sa_query, hapd, NULL);
585#endif /* CONFIG_OCV */
586
Hai Shalom021b0b52019-04-10 11:17:58 -0700587#ifdef CONFIG_SAE
588 {
589 struct hostapd_sae_commit_queue *q;
590
591 while ((q = dl_list_first(&hapd->sae_commit_queue,
592 struct hostapd_sae_commit_queue,
593 list))) {
594 dl_list_del(&q->list);
595 os_free(q);
596 }
597 }
598 eloop_cancel_timeout(auth_sae_process_commit, hapd, NULL);
599#endif /* CONFIG_SAE */
Sunil Ravia04bd252022-05-02 22:54:18 -0700600
601#ifdef CONFIG_IEEE80211AX
602 eloop_cancel_timeout(hostapd_switch_color_timeout_handler, hapd, NULL);
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000603#ifdef CONFIG_TESTING_OPTIONS
604#ifdef CONFIG_IEEE80211BE
605 eloop_cancel_timeout(hostapd_link_remove_timeout_handler, hapd, NULL);
606#endif /* CONFIG_IEEE80211BE */
607#endif /* CONFIG_TESTING_OPTIONS */
608
Sunil Ravia04bd252022-05-02 22:54:18 -0700609#endif /* CONFIG_IEEE80211AX */
Dmitry Shmidt04949592012-07-19 12:16:46 -0700610}
611
612
Sunil Ravi99c035e2024-07-12 01:42:03 +0000613/* hostapd_bss_link_deinit - Per-BSS ML cleanup (deinitialization)
614 * @hapd: Pointer to BSS data
615 *
616 * This function is used to unlink the BSS from the AP MLD.
617 * If the BSS being removed is the first link, the next link becomes the first
618 * link.
619 */
620static void hostapd_bss_link_deinit(struct hostapd_data *hapd)
621{
622#ifdef CONFIG_IEEE80211BE
Sunil Ravic0f5d412024-09-11 22:12:49 +0000623 int i;
624
Sunil Ravi99c035e2024-07-12 01:42:03 +0000625 if (!hapd->conf || !hapd->conf->mld_ap)
626 return;
627
Sunil Ravic0f5d412024-09-11 22:12:49 +0000628 /* Free per STA profiles */
629 for (i = 0; i < MAX_NUM_MLD_LINKS; i++) {
630 os_free(hapd->partner_links[i].resp_sta_profile);
631 os_memset(&hapd->partner_links[i], 0,
632 sizeof(hapd->partner_links[i]));
633 }
634
635 /* Put all freeing logic above this */
Sunil Ravi99c035e2024-07-12 01:42:03 +0000636 if (!hapd->mld->num_links)
637 return;
638
639 /* If not started, not yet linked to the MLD. However, the first
640 * BSS is always linked since it is linked during driver_init(), and
641 * hence, need to remove it from the AP MLD.
642 */
643 if (!hapd->started && hapd->iface->bss[0] != hapd)
644 return;
645
646 /* The first BSS can also be only linked when at least driver_init() is
647 * executed. But if previous interface fails, it is not, and hence,
648 * safe to skip.
649 */
650 if (hapd->iface->bss[0] == hapd && !hapd->drv_priv)
651 return;
652
653 hostapd_mld_remove_link(hapd);
654#endif /* CONFIG_IEEE80211BE */
655}
656
657
Dmitry Shmidt04949592012-07-19 12:16:46 -0700658/**
659 * hostapd_cleanup - Per-BSS cleanup (deinitialization)
660 * @hapd: Pointer to BSS data
661 *
662 * This function is used to free all per-BSS data structures and resources.
Dmitry Shmidt54605472013-11-08 11:10:19 -0800663 * Most of the modules that are initialized in hostapd_setup_bss() are
664 * deinitialized here.
Dmitry Shmidt04949592012-07-19 12:16:46 -0700665 */
666static void hostapd_cleanup(struct hostapd_data *hapd)
667{
Dmitry Shmidt54605472013-11-08 11:10:19 -0800668 wpa_printf(MSG_DEBUG, "%s(hapd=%p (%s))", __func__, hapd,
Hai Shalom021b0b52019-04-10 11:17:58 -0700669 hapd->conf ? hapd->conf->iface : "N/A");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700670 if (hapd->iface->interfaces &&
Dmitry Shmidt29333592017-01-09 12:27:11 -0800671 hapd->iface->interfaces->ctrl_iface_deinit) {
672 wpa_msg(hapd->msg_ctx, MSG_INFO, WPA_EVENT_TERMINATING);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700673 hapd->iface->interfaces->ctrl_iface_deinit(hapd);
Dmitry Shmidt29333592017-01-09 12:27:11 -0800674 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700675 hostapd_free_hapd_data(hapd);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700676}
677
678
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800679static void sta_track_deinit(struct hostapd_iface *iface)
680{
681 struct hostapd_sta_info *info;
682
683 if (!iface->num_sta_seen)
684 return;
685
686 while ((info = dl_list_first(&iface->sta_seen, struct hostapd_sta_info,
687 list))) {
688 dl_list_del(&info->list);
689 iface->num_sta_seen--;
Dmitry Shmidtaca489e2016-09-28 15:44:14 -0700690 sta_track_del(info);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800691 }
692}
693
694
Hai Shalom60840252021-02-19 19:02:11 -0800695void hostapd_cleanup_iface_partial(struct hostapd_iface *iface)
Dmitry Shmidt04949592012-07-19 12:16:46 -0700696{
Dmitry Shmidt54605472013-11-08 11:10:19 -0800697 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
Sunil Ravi99c035e2024-07-12 01:42:03 +0000698 eloop_cancel_timeout(channel_list_update_timeout, iface, NULL);
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800699#ifdef NEED_AP_MLME
700 hostapd_stop_setup_timers(iface);
701#endif /* NEED_AP_MLME */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700702 if (iface->current_mode)
703 acs_cleanup(iface);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700704 hostapd_free_hw_features(iface->hw_features, iface->num_hw_features);
705 iface->hw_features = NULL;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700706 iface->current_mode = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700707 os_free(iface->current_rates);
708 iface->current_rates = NULL;
709 os_free(iface->basic_rates);
710 iface->basic_rates = NULL;
Sunil Ravi77d572f2023-01-17 23:58:31 +0000711 iface->cac_started = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700712 ap_list_deinit(iface);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800713 sta_track_deinit(iface);
Hai Shalom81f62d82019-07-22 12:10:00 -0700714 airtime_policy_update_deinit(iface);
Sunil Ravic0f5d412024-09-11 22:12:49 +0000715 hostapd_free_multi_hw_info(iface->multi_hw_info);
716 iface->multi_hw_info = NULL;
717 iface->current_hw_info = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700718}
719
720
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700721/**
722 * hostapd_cleanup_iface - Complete per-interface cleanup
723 * @iface: Pointer to interface data
724 *
725 * This function is called after per-BSS data structures are deinitialized
726 * with hostapd_cleanup().
727 */
728static void hostapd_cleanup_iface(struct hostapd_iface *iface)
729{
Dmitry Shmidt54605472013-11-08 11:10:19 -0800730 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
Roshan Pius3a1667e2018-07-03 15:17:14 -0700731 eloop_cancel_timeout(hostapd_interface_setup_failure_handler, iface,
732 NULL);
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800733
Dmitry Shmidt04949592012-07-19 12:16:46 -0700734 hostapd_cleanup_iface_partial(iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700735 hostapd_config_free(iface->conf);
736 iface->conf = NULL;
737
738 os_free(iface->config_fname);
739 os_free(iface->bss);
Dmitry Shmidt54605472013-11-08 11:10:19 -0800740 wpa_printf(MSG_DEBUG, "%s: free iface=%p", __func__, iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700741 os_free(iface);
742}
743
744
Hai Shalomfdcde762020-04-02 11:19:20 -0700745#ifdef CONFIG_WEP
746
Dmitry Shmidt04949592012-07-19 12:16:46 -0700747static void hostapd_clear_wep(struct hostapd_data *hapd)
748{
Hai Shalom021b0b52019-04-10 11:17:58 -0700749 if (hapd->drv_priv && !hapd->iface->driver_ap_teardown && hapd->conf) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700750 hostapd_set_privacy(hapd, 0);
751 hostapd_broadcast_wep_clear(hapd);
752 }
753}
754
755
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700756static int hostapd_setup_encryption(char *iface, struct hostapd_data *hapd)
757{
758 int i;
759
760 hostapd_broadcast_wep_set(hapd);
761
762 if (hapd->conf->ssid.wep.default_len) {
763 hostapd_set_privacy(hapd, 1);
764 return 0;
765 }
766
Jouni Malinen75ecf522011-06-27 15:19:46 -0700767 /*
768 * When IEEE 802.1X is not enabled, the driver may need to know how to
769 * set authentication algorithms for static WEP.
770 */
771 hostapd_drv_set_authmode(hapd, hapd->conf->auth_algs);
772
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700773 for (i = 0; i < 4; i++) {
774 if (hapd->conf->ssid.wep.key[i] &&
Hai Shalomfdcde762020-04-02 11:19:20 -0700775 hostapd_drv_set_key(iface, hapd, WPA_ALG_WEP, NULL, i, 0,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700776 i == hapd->conf->ssid.wep.idx, NULL, 0,
777 hapd->conf->ssid.wep.key[i],
Hai Shalomfdcde762020-04-02 11:19:20 -0700778 hapd->conf->ssid.wep.len[i],
779 i == hapd->conf->ssid.wep.idx ?
780 KEY_FLAG_GROUP_RX_TX_DEFAULT :
781 KEY_FLAG_GROUP_RX_TX)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700782 wpa_printf(MSG_WARNING, "Could not set WEP "
783 "encryption.");
784 return -1;
785 }
786 if (hapd->conf->ssid.wep.key[i] &&
787 i == hapd->conf->ssid.wep.idx)
788 hostapd_set_privacy(hapd, 1);
789 }
790
791 return 0;
792}
793
Hai Shalomfdcde762020-04-02 11:19:20 -0700794#endif /* CONFIG_WEP */
795
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700796
Dmitry Shmidt04949592012-07-19 12:16:46 -0700797static int hostapd_flush_old_stations(struct hostapd_data *hapd, u16 reason)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700798{
799 int ret = 0;
800 u8 addr[ETH_ALEN];
801
802 if (hostapd_drv_none(hapd) || hapd->drv_priv == NULL)
803 return 0;
804
Dmitry Shmidta38abf92014-03-06 13:38:44 -0800805 if (!hapd->iface->driver_ap_teardown) {
806 wpa_dbg(hapd->msg_ctx, MSG_DEBUG,
807 "Flushing old station entries");
808
809 if (hostapd_flush(hapd)) {
810 wpa_msg(hapd->msg_ctx, MSG_WARNING,
811 "Could not connect to kernel driver");
812 ret = -1;
813 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700814 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700815 if (hapd->conf && hapd->conf->broadcast_deauth) {
816 wpa_dbg(hapd->msg_ctx, MSG_DEBUG,
817 "Deauthenticate all stations");
818 os_memset(addr, 0xff, ETH_ALEN);
819 hostapd_drv_sta_deauth(hapd, addr, reason);
820 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700821 hostapd_free_stas(hapd);
822
823 return ret;
824}
825
826
Hai Shalom60840252021-02-19 19:02:11 -0800827void hostapd_bss_deinit_no_free(struct hostapd_data *hapd)
Dmitry Shmidt71757432014-06-02 13:50:35 -0700828{
829 hostapd_free_stas(hapd);
830 hostapd_flush_old_stations(hapd, WLAN_REASON_DEAUTH_LEAVING);
Hai Shalomfdcde762020-04-02 11:19:20 -0700831#ifdef CONFIG_WEP
Dmitry Shmidt71757432014-06-02 13:50:35 -0700832 hostapd_clear_wep(hapd);
Hai Shalomfdcde762020-04-02 11:19:20 -0700833#endif /* CONFIG_WEP */
Dmitry Shmidt71757432014-06-02 13:50:35 -0700834}
835
836
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700837/**
838 * hostapd_validate_bssid_configuration - Validate BSSID configuration
839 * @iface: Pointer to interface data
840 * Returns: 0 on success, -1 on failure
841 *
842 * This function is used to validate that the configured BSSIDs are valid.
843 */
844static int hostapd_validate_bssid_configuration(struct hostapd_iface *iface)
845{
846 u8 mask[ETH_ALEN] = { 0 };
847 struct hostapd_data *hapd = iface->bss[0];
848 unsigned int i = iface->conf->num_bss, bits = 0, j;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700849 int auto_addr = 0;
850
851 if (hostapd_drv_none(hapd))
852 return 0;
853
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -0800854 if (iface->conf->use_driver_iface_addr)
855 return 0;
856
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700857 /* Generate BSSID mask that is large enough to cover the BSSIDs. */
858
859 /* Determine the bits necessary to cover the number of BSSIDs. */
860 for (i--; i; i >>= 1)
861 bits++;
862
863 /* Determine the bits necessary to any configured BSSIDs,
864 if they are higher than the number of BSSIDs. */
865 for (j = 0; j < iface->conf->num_bss; j++) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800866 if (is_zero_ether_addr(iface->conf->bss[j]->bssid)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700867 if (j)
868 auto_addr++;
869 continue;
870 }
871
872 for (i = 0; i < ETH_ALEN; i++) {
873 mask[i] |=
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800874 iface->conf->bss[j]->bssid[i] ^
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700875 hapd->own_addr[i];
876 }
877 }
878
879 if (!auto_addr)
880 goto skip_mask_ext;
881
882 for (i = 0; i < ETH_ALEN && mask[i] == 0; i++)
883 ;
884 j = 0;
885 if (i < ETH_ALEN) {
886 j = (5 - i) * 8;
887
888 while (mask[i] != 0) {
889 mask[i] >>= 1;
890 j++;
891 }
892 }
893
894 if (bits < j)
895 bits = j;
896
897 if (bits > 40) {
898 wpa_printf(MSG_ERROR, "Too many bits in the BSSID mask (%u)",
899 bits);
900 return -1;
901 }
902
903 os_memset(mask, 0xff, ETH_ALEN);
904 j = bits / 8;
905 for (i = 5; i > 5 - j; i--)
906 mask[i] = 0;
907 j = bits % 8;
Hai Shalom021b0b52019-04-10 11:17:58 -0700908 while (j) {
909 j--;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700910 mask[i] <<= 1;
Hai Shalom021b0b52019-04-10 11:17:58 -0700911 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700912
913skip_mask_ext:
914 wpa_printf(MSG_DEBUG, "BSS count %lu, BSSID mask " MACSTR " (%d bits)",
915 (unsigned long) iface->conf->num_bss, MAC2STR(mask), bits);
916
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700917 if (!auto_addr)
918 return 0;
919
920 for (i = 0; i < ETH_ALEN; i++) {
921 if ((hapd->own_addr[i] & mask[i]) != hapd->own_addr[i]) {
922 wpa_printf(MSG_ERROR, "Invalid BSSID mask " MACSTR
923 " for start address " MACSTR ".",
924 MAC2STR(mask), MAC2STR(hapd->own_addr));
925 wpa_printf(MSG_ERROR, "Start address must be the "
926 "first address in the block (i.e., addr "
927 "AND mask == addr).");
928 return -1;
929 }
930 }
931
932 return 0;
933}
934
935
936static int mac_in_conf(struct hostapd_config *conf, const void *a)
937{
938 size_t i;
939
940 for (i = 0; i < conf->num_bss; i++) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800941 if (hostapd_mac_comp(conf->bss[i]->bssid, a) == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700942 return 1;
943 }
944 }
945
946 return 0;
947}
948
949
Dmitry Shmidt04949592012-07-19 12:16:46 -0700950#ifndef CONFIG_NO_RADIUS
951
952static int hostapd_das_nas_mismatch(struct hostapd_data *hapd,
953 struct radius_das_attrs *attr)
954{
Dmitry Shmidt13ca8d82014-02-20 10:18:40 -0800955 if (attr->nas_identifier &&
956 (!hapd->conf->nas_identifier ||
957 os_strlen(hapd->conf->nas_identifier) !=
958 attr->nas_identifier_len ||
959 os_memcmp(hapd->conf->nas_identifier, attr->nas_identifier,
960 attr->nas_identifier_len) != 0)) {
961 wpa_printf(MSG_DEBUG, "RADIUS DAS: NAS-Identifier mismatch");
962 return 1;
963 }
964
965 if (attr->nas_ip_addr &&
966 (hapd->conf->own_ip_addr.af != AF_INET ||
967 os_memcmp(&hapd->conf->own_ip_addr.u.v4, attr->nas_ip_addr, 4) !=
968 0)) {
969 wpa_printf(MSG_DEBUG, "RADIUS DAS: NAS-IP-Address mismatch");
970 return 1;
971 }
972
973#ifdef CONFIG_IPV6
974 if (attr->nas_ipv6_addr &&
975 (hapd->conf->own_ip_addr.af != AF_INET6 ||
976 os_memcmp(&hapd->conf->own_ip_addr.u.v6, attr->nas_ipv6_addr, 16)
977 != 0)) {
978 wpa_printf(MSG_DEBUG, "RADIUS DAS: NAS-IPv6-Address mismatch");
979 return 1;
980 }
981#endif /* CONFIG_IPV6 */
982
Dmitry Shmidt04949592012-07-19 12:16:46 -0700983 return 0;
984}
985
986
987static struct sta_info * hostapd_das_find_sta(struct hostapd_data *hapd,
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800988 struct radius_das_attrs *attr,
989 int *multi)
Dmitry Shmidt04949592012-07-19 12:16:46 -0700990{
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800991 struct sta_info *selected, *sta;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700992 char buf[128];
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800993 int num_attr = 0;
994 int count;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700995
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800996 *multi = 0;
997
998 for (sta = hapd->sta_list; sta; sta = sta->next)
999 sta->radius_das_match = 1;
1000
1001 if (attr->sta_addr) {
1002 num_attr++;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001003 sta = ap_get_sta(hapd, attr->sta_addr);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001004 if (!sta) {
1005 wpa_printf(MSG_DEBUG,
1006 "RADIUS DAS: No Calling-Station-Id match");
1007 return NULL;
1008 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001009
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001010 selected = sta;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001011 for (sta = hapd->sta_list; sta; sta = sta->next) {
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001012 if (sta != selected)
1013 sta->radius_das_match = 0;
1014 }
1015 wpa_printf(MSG_DEBUG, "RADIUS DAS: Calling-Station-Id match");
1016 }
1017
1018 if (attr->acct_session_id) {
1019 num_attr++;
Dmitry Shmidtb97e4282016-02-08 10:16:07 -08001020 if (attr->acct_session_id_len != 16) {
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001021 wpa_printf(MSG_DEBUG,
1022 "RADIUS DAS: Acct-Session-Id cannot match");
1023 return NULL;
1024 }
1025 count = 0;
1026
1027 for (sta = hapd->sta_list; sta; sta = sta->next) {
1028 if (!sta->radius_das_match)
1029 continue;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001030 os_snprintf(buf, sizeof(buf), "%016llX",
1031 (unsigned long long) sta->acct_session_id);
Dmitry Shmidtb97e4282016-02-08 10:16:07 -08001032 if (os_memcmp(attr->acct_session_id, buf, 16) != 0)
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001033 sta->radius_das_match = 0;
1034 else
1035 count++;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001036 }
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001037
1038 if (count == 0) {
1039 wpa_printf(MSG_DEBUG,
1040 "RADIUS DAS: No matches remaining after Acct-Session-Id check");
1041 return NULL;
1042 }
1043 wpa_printf(MSG_DEBUG, "RADIUS DAS: Acct-Session-Id match");
Dmitry Shmidt04949592012-07-19 12:16:46 -07001044 }
1045
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001046 if (attr->acct_multi_session_id) {
1047 num_attr++;
Dmitry Shmidtb97e4282016-02-08 10:16:07 -08001048 if (attr->acct_multi_session_id_len != 16) {
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001049 wpa_printf(MSG_DEBUG,
1050 "RADIUS DAS: Acct-Multi-Session-Id cannot match");
1051 return NULL;
1052 }
1053 count = 0;
1054
1055 for (sta = hapd->sta_list; sta; sta = sta->next) {
1056 if (!sta->radius_das_match)
1057 continue;
1058 if (!sta->eapol_sm ||
Dmitry Shmidtb97e4282016-02-08 10:16:07 -08001059 !sta->eapol_sm->acct_multi_session_id) {
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001060 sta->radius_das_match = 0;
1061 continue;
1062 }
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001063 os_snprintf(buf, sizeof(buf), "%016llX",
1064 (unsigned long long)
Dmitry Shmidtb97e4282016-02-08 10:16:07 -08001065 sta->eapol_sm->acct_multi_session_id);
1066 if (os_memcmp(attr->acct_multi_session_id, buf, 16) !=
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001067 0)
1068 sta->radius_das_match = 0;
1069 else
1070 count++;
1071 }
1072
1073 if (count == 0) {
1074 wpa_printf(MSG_DEBUG,
1075 "RADIUS DAS: No matches remaining after Acct-Multi-Session-Id check");
1076 return NULL;
1077 }
1078 wpa_printf(MSG_DEBUG,
1079 "RADIUS DAS: Acct-Multi-Session-Id match");
1080 }
1081
1082 if (attr->cui) {
1083 num_attr++;
1084 count = 0;
1085
Dmitry Shmidt04949592012-07-19 12:16:46 -07001086 for (sta = hapd->sta_list; sta; sta = sta->next) {
1087 struct wpabuf *cui;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001088
1089 if (!sta->radius_das_match)
1090 continue;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001091 cui = ieee802_1x_get_radius_cui(sta->eapol_sm);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001092 if (!cui || wpabuf_len(cui) != attr->cui_len ||
Dmitry Shmidt04949592012-07-19 12:16:46 -07001093 os_memcmp(wpabuf_head(cui), attr->cui,
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001094 attr->cui_len) != 0)
1095 sta->radius_das_match = 0;
1096 else
1097 count++;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001098 }
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001099
1100 if (count == 0) {
1101 wpa_printf(MSG_DEBUG,
1102 "RADIUS DAS: No matches remaining after Chargeable-User-Identity check");
1103 return NULL;
1104 }
1105 wpa_printf(MSG_DEBUG,
1106 "RADIUS DAS: Chargeable-User-Identity match");
Dmitry Shmidt04949592012-07-19 12:16:46 -07001107 }
1108
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001109 if (attr->user_name) {
1110 num_attr++;
1111 count = 0;
1112
Dmitry Shmidt04949592012-07-19 12:16:46 -07001113 for (sta = hapd->sta_list; sta; sta = sta->next) {
1114 u8 *identity;
1115 size_t identity_len;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001116
1117 if (!sta->radius_das_match)
1118 continue;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001119 identity = ieee802_1x_get_identity(sta->eapol_sm,
1120 &identity_len);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001121 if (!identity ||
1122 identity_len != attr->user_name_len ||
Dmitry Shmidt04949592012-07-19 12:16:46 -07001123 os_memcmp(identity, attr->user_name, identity_len)
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001124 != 0)
1125 sta->radius_das_match = 0;
1126 else
1127 count++;
1128 }
1129
1130 if (count == 0) {
1131 wpa_printf(MSG_DEBUG,
1132 "RADIUS DAS: No matches remaining after User-Name check");
1133 return NULL;
1134 }
1135 wpa_printf(MSG_DEBUG,
1136 "RADIUS DAS: User-Name match");
1137 }
1138
1139 if (num_attr == 0) {
1140 /*
1141 * In theory, we could match all current associations, but it
1142 * seems safer to just reject requests that do not include any
1143 * session identification attributes.
1144 */
1145 wpa_printf(MSG_DEBUG,
1146 "RADIUS DAS: No session identification attributes included");
1147 return NULL;
1148 }
1149
1150 selected = NULL;
1151 for (sta = hapd->sta_list; sta; sta = sta->next) {
1152 if (sta->radius_das_match) {
1153 if (selected) {
1154 *multi = 1;
1155 return NULL;
1156 }
1157 selected = sta;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001158 }
1159 }
1160
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001161 return selected;
1162}
1163
1164
1165static int hostapd_das_disconnect_pmksa(struct hostapd_data *hapd,
1166 struct radius_das_attrs *attr)
1167{
1168 if (!hapd->wpa_auth)
1169 return -1;
1170 return wpa_auth_radius_das_disconnect_pmksa(hapd->wpa_auth, attr);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001171}
1172
1173
1174static enum radius_das_res
1175hostapd_das_disconnect(void *ctx, struct radius_das_attrs *attr)
1176{
1177 struct hostapd_data *hapd = ctx;
1178 struct sta_info *sta;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001179 int multi;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001180
1181 if (hostapd_das_nas_mismatch(hapd, attr))
1182 return RADIUS_DAS_NAS_MISMATCH;
1183
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001184 sta = hostapd_das_find_sta(hapd, attr, &multi);
1185 if (sta == NULL) {
1186 if (multi) {
1187 wpa_printf(MSG_DEBUG,
1188 "RADIUS DAS: Multiple sessions match - not supported");
1189 return RADIUS_DAS_MULTI_SESSION_MATCH;
1190 }
1191 if (hostapd_das_disconnect_pmksa(hapd, attr) == 0) {
1192 wpa_printf(MSG_DEBUG,
1193 "RADIUS DAS: PMKSA cache entry matched");
1194 return RADIUS_DAS_SUCCESS;
1195 }
1196 wpa_printf(MSG_DEBUG, "RADIUS DAS: No matching session found");
Dmitry Shmidt04949592012-07-19 12:16:46 -07001197 return RADIUS_DAS_SESSION_NOT_FOUND;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001198 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001199
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001200 wpa_printf(MSG_DEBUG, "RADIUS DAS: Found a matching session " MACSTR
1201 " - disconnecting", MAC2STR(sta->addr));
Dmitry Shmidt13ca8d82014-02-20 10:18:40 -08001202 wpa_auth_pmksa_remove(hapd->wpa_auth, sta->addr);
1203
Dmitry Shmidt04949592012-07-19 12:16:46 -07001204 hostapd_drv_sta_deauth(hapd, sta->addr,
1205 WLAN_REASON_PREV_AUTH_NOT_VALID);
1206 ap_sta_deauthenticate(hapd, sta, WLAN_REASON_PREV_AUTH_NOT_VALID);
1207
1208 return RADIUS_DAS_SUCCESS;
1209}
1210
Roshan Pius3a1667e2018-07-03 15:17:14 -07001211
1212#ifdef CONFIG_HS20
1213static enum radius_das_res
1214hostapd_das_coa(void *ctx, struct radius_das_attrs *attr)
1215{
1216 struct hostapd_data *hapd = ctx;
1217 struct sta_info *sta;
1218 int multi;
1219
1220 if (hostapd_das_nas_mismatch(hapd, attr))
1221 return RADIUS_DAS_NAS_MISMATCH;
1222
1223 sta = hostapd_das_find_sta(hapd, attr, &multi);
1224 if (!sta) {
1225 if (multi) {
1226 wpa_printf(MSG_DEBUG,
1227 "RADIUS DAS: Multiple sessions match - not supported");
1228 return RADIUS_DAS_MULTI_SESSION_MATCH;
1229 }
1230 wpa_printf(MSG_DEBUG, "RADIUS DAS: No matching session found");
1231 return RADIUS_DAS_SESSION_NOT_FOUND;
1232 }
1233
1234 wpa_printf(MSG_DEBUG, "RADIUS DAS: Found a matching session " MACSTR
1235 " - CoA", MAC2STR(sta->addr));
1236
1237 if (attr->hs20_t_c_filtering) {
1238 if (attr->hs20_t_c_filtering[0] & BIT(0)) {
1239 wpa_printf(MSG_DEBUG,
1240 "HS 2.0: Unexpected Terms and Conditions filtering required in CoA-Request");
1241 return RADIUS_DAS_COA_FAILED;
1242 }
1243
1244 hs20_t_c_filtering(hapd, sta, 0);
1245 }
1246
1247 return RADIUS_DAS_SUCCESS;
1248}
1249#else /* CONFIG_HS20 */
1250#define hostapd_das_coa NULL
1251#endif /* CONFIG_HS20 */
1252
Hai Shalomc3565922019-10-28 11:58:20 -07001253
1254#ifdef CONFIG_SQLITE
1255
1256static int db_table_exists(sqlite3 *db, const char *name)
1257{
1258 char cmd[128];
1259
1260 os_snprintf(cmd, sizeof(cmd), "SELECT 1 FROM %s;", name);
1261 return sqlite3_exec(db, cmd, NULL, NULL, NULL) == SQLITE_OK;
1262}
1263
1264
1265static int db_table_create_radius_attributes(sqlite3 *db)
1266{
1267 char *err = NULL;
1268 const char *sql =
1269 "CREATE TABLE radius_attributes("
1270 " id INTEGER PRIMARY KEY,"
1271 " sta TEXT,"
1272 " reqtype TEXT,"
1273 " attr TEXT"
1274 ");"
1275 "CREATE INDEX idx_sta_reqtype ON radius_attributes(sta,reqtype);";
1276
1277 wpa_printf(MSG_DEBUG,
1278 "Adding database table for RADIUS attribute information");
1279 if (sqlite3_exec(db, sql, NULL, NULL, &err) != SQLITE_OK) {
1280 wpa_printf(MSG_ERROR, "SQLite error: %s", err);
1281 sqlite3_free(err);
1282 return -1;
1283 }
1284
1285 return 0;
1286}
1287
1288#endif /* CONFIG_SQLITE */
1289
Dmitry Shmidt04949592012-07-19 12:16:46 -07001290#endif /* CONFIG_NO_RADIUS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001291
1292
Sunil Ravi77d572f2023-01-17 23:58:31 +00001293static int hostapd_start_beacon(struct hostapd_data *hapd,
1294 bool flush_old_stations)
1295{
1296 struct hostapd_bss_config *conf = hapd->conf;
1297
1298 if (!conf->start_disabled && ieee802_11_set_beacon(hapd) < 0)
1299 return -1;
1300
1301 if (flush_old_stations && !conf->start_disabled &&
1302 conf->broadcast_deauth) {
1303 u8 addr[ETH_ALEN];
1304
1305 /* Should any previously associated STA not have noticed that
1306 * the AP had stopped and restarted, send one more
1307 * deauthentication notification now that the AP is ready to
1308 * operate. */
1309 wpa_dbg(hapd->msg_ctx, MSG_DEBUG,
1310 "Deauthenticate all stations at BSS start");
1311 os_memset(addr, 0xff, ETH_ALEN);
1312 hostapd_drv_sta_deauth(hapd, addr,
1313 WLAN_REASON_PREV_AUTH_NOT_VALID);
1314 }
1315
1316 if (hapd->driver && hapd->driver->set_operstate)
1317 hapd->driver->set_operstate(hapd->drv_priv, 1);
1318
1319 return 0;
1320}
1321
1322
Sunil Ravi7f769292024-07-23 22:21:32 +00001323#ifndef CONFIG_NO_RADIUS
1324static int hostapd_bss_radius_init(struct hostapd_data *hapd)
1325{
1326 struct hostapd_bss_config *conf;
1327
1328 if (!hapd)
1329 return -1;
1330
1331 conf = hapd->conf;
1332
1333 if (hapd->radius) {
1334 wpa_printf(MSG_DEBUG,
1335 "Skipping RADIUS client init (already done)");
1336 return 0;
1337 }
1338
1339 hapd->radius = radius_client_init(hapd, conf->radius);
1340 if (!hapd->radius) {
1341 wpa_printf(MSG_ERROR,
1342 "RADIUS client initialization failed.");
1343 return -1;
1344 }
1345
1346 if (conf->radius_das_port) {
1347 struct radius_das_conf das_conf;
1348
1349 os_memset(&das_conf, 0, sizeof(das_conf));
1350 das_conf.port = conf->radius_das_port;
1351 das_conf.shared_secret = conf->radius_das_shared_secret;
1352 das_conf.shared_secret_len =
1353 conf->radius_das_shared_secret_len;
1354 das_conf.client_addr = &conf->radius_das_client_addr;
1355 das_conf.time_window = conf->radius_das_time_window;
1356 das_conf.require_event_timestamp =
1357 conf->radius_das_require_event_timestamp;
1358 das_conf.require_message_authenticator =
1359 conf->radius_das_require_message_authenticator;
1360 das_conf.ctx = hapd;
1361 das_conf.disconnect = hostapd_das_disconnect;
1362 das_conf.coa = hostapd_das_coa;
1363 hapd->radius_das = radius_das_init(&das_conf);
1364 if (!hapd->radius_das) {
1365 wpa_printf(MSG_ERROR,
1366 "RADIUS DAS initialization failed.");
1367 return -1;
1368 }
1369 }
1370
1371 return 0;
1372}
1373#endif /* CONFIG_NO_RADIUS */
1374
1375
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001376/**
1377 * hostapd_setup_bss - Per-BSS setup (initialization)
1378 * @hapd: Pointer to BSS data
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001379 * @first: Whether this BSS is the first BSS of an interface; -1 = not first,
1380 * but interface may exist
Sunil Ravi77d572f2023-01-17 23:58:31 +00001381 * @start_beacon: Whether Beacon frame template should be configured and
1382 * transmission of Beaconf rames started at this time. This is used when
1383 * MBSSID element is enabled where the information regarding all BSSes
1384 * should be retrieved before configuring the Beacon frame template. The
1385 * calling functions are responsible for configuring the Beacon frame
1386 * explicitly if this is set to false.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001387 *
1388 * This function is used to initialize all per-BSS data structures and
1389 * resources. This gets called in a loop for each BSS when an interface is
1390 * initialized. Most of the modules that are initialized here will be
1391 * deinitialized in hostapd_cleanup().
1392 */
Sunil Ravi77d572f2023-01-17 23:58:31 +00001393static int hostapd_setup_bss(struct hostapd_data *hapd, int first,
1394 bool start_beacon)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001395{
1396 struct hostapd_bss_config *conf = hapd->conf;
Dmitry Shmidt9d9e6022015-04-23 10:34:55 -07001397 u8 ssid[SSID_MAX_LEN + 1];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001398 int ssid_len, set_ssid;
1399 char force_ifname[IFNAMSIZ];
1400 u8 if_addr[ETH_ALEN];
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001401 int flush_old_stations = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001402
Sunil Ravi99c035e2024-07-12 01:42:03 +00001403 if (!hostapd_mld_is_first_bss(hapd))
Sunil Ravi2a14cf12023-11-21 00:54:38 +00001404 wpa_printf(MSG_DEBUG,
1405 "MLD: %s: Setting non-first BSS", __func__);
1406
Dmitry Shmidt54605472013-11-08 11:10:19 -08001407 wpa_printf(MSG_DEBUG, "%s(hapd=%p (%s), first=%d)",
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001408 __func__, hapd, conf->iface, first);
Dmitry Shmidt54605472013-11-08 11:10:19 -08001409
Dmitry Shmidt50b691d2014-05-21 14:01:45 -07001410#ifdef EAP_SERVER_TNC
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001411 if (conf->tnc && tncs_global_init() < 0) {
Dmitry Shmidt50b691d2014-05-21 14:01:45 -07001412 wpa_printf(MSG_ERROR, "Failed to initialize TNCS");
1413 return -1;
1414 }
1415#endif /* EAP_SERVER_TNC */
1416
Dmitry Shmidt54605472013-11-08 11:10:19 -08001417 if (hapd->started) {
1418 wpa_printf(MSG_ERROR, "%s: Interface %s was already started",
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001419 __func__, conf->iface);
Dmitry Shmidt54605472013-11-08 11:10:19 -08001420 return -1;
1421 }
1422 hapd->started = 1;
1423
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001424 if (!first || first == -1) {
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08001425 u8 *addr = hapd->own_addr;
1426
1427 if (!is_zero_ether_addr(conf->bssid)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001428 /* Allocate the configured BSSID. */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001429 os_memcpy(hapd->own_addr, conf->bssid, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001430
1431 if (hostapd_mac_comp(hapd->own_addr,
1432 hapd->iface->bss[0]->own_addr) ==
1433 0) {
1434 wpa_printf(MSG_ERROR, "BSS '%s' may not have "
1435 "BSSID set to the MAC address of "
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001436 "the radio", conf->iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001437 return -1;
1438 }
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08001439 } else if (hapd->iconf->use_driver_iface_addr) {
1440 addr = NULL;
1441 } else {
1442 /* Allocate the next available BSSID. */
1443 do {
1444 inc_byte_array(hapd->own_addr, ETH_ALEN);
1445 } while (mac_in_conf(hapd->iconf, hapd->own_addr));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001446 }
1447
Sunil Ravi7f769292024-07-23 22:21:32 +00001448#ifdef CONFIG_IEEE80211BE
1449 if (conf->mld_ap) {
1450 struct hostapd_data *h_hapd;
1451
1452 h_hapd = hostapd_mld_get_first_bss(hapd);
1453 if (h_hapd) {
1454 hapd->drv_priv = h_hapd->drv_priv;
1455 hapd->interface_added = h_hapd->interface_added;
Sunil Ravi7f769292024-07-23 22:21:32 +00001456 wpa_printf(MSG_DEBUG,
1457 "Setup of non first link (%d) BSS of MLD %s",
1458 hapd->mld_link_id, hapd->conf->iface);
1459 goto setup_mld;
1460 }
1461 }
1462#endif /* CONFIG_IEEE80211BE */
1463
Dmitry Shmidt54605472013-11-08 11:10:19 -08001464 hapd->interface_added = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001465 if (hostapd_if_add(hapd->iface->bss[0], WPA_IF_AP_BSS,
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08001466 conf->iface, addr, hapd,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001467 &hapd->drv_priv, force_ifname, if_addr,
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001468 conf->bridge[0] ? conf->bridge : NULL,
1469 first == -1)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001470 wpa_printf(MSG_ERROR, "Failed to add BSS (BSSID="
1471 MACSTR ")", MAC2STR(hapd->own_addr));
Dmitry Shmidt3cf6f792013-12-18 13:12:19 -08001472 hapd->interface_added = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001473 return -1;
1474 }
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08001475
1476 if (!addr)
1477 os_memcpy(hapd->own_addr, if_addr, ETH_ALEN);
Sunil Ravi7f769292024-07-23 22:21:32 +00001478
1479#ifdef CONFIG_IEEE80211BE
1480 if (hapd->conf->mld_ap) {
1481 wpa_printf(MSG_DEBUG,
1482 "Setup of first link (%d) BSS of MLD %s",
1483 hapd->mld_link_id, hapd->conf->iface);
1484 os_memcpy(hapd->mld->mld_addr, hapd->own_addr,
1485 ETH_ALEN);
Sunil Ravi7f769292024-07-23 22:21:32 +00001486 }
1487#endif /* CONFIG_IEEE80211BE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001488 }
1489
Sunil Ravi7f769292024-07-23 22:21:32 +00001490#ifdef CONFIG_IEEE80211BE
1491setup_mld:
1492 if (hapd->conf->mld_ap && !first) {
1493 wpa_printf(MSG_DEBUG,
1494 "MLD: Set link_id=%u, mld_addr=" MACSTR
1495 ", own_addr=" MACSTR,
1496 hapd->mld_link_id, MAC2STR(hapd->mld->mld_addr),
1497 MAC2STR(hapd->own_addr));
1498
1499 if (hostapd_drv_link_add(hapd, hapd->mld_link_id,
Sunil Ravic0f5d412024-09-11 22:12:49 +00001500 hapd->own_addr)) {
1501 wpa_printf(MSG_ERROR,
1502 "MLD: Failed to add link %d in MLD %s",
1503 hapd->mld_link_id, hapd->conf->iface);
Sunil Ravi7f769292024-07-23 22:21:32 +00001504 return -1;
Sunil Ravic0f5d412024-09-11 22:12:49 +00001505 }
1506 hostapd_mld_add_link(hapd);
Sunil Ravi7f769292024-07-23 22:21:32 +00001507 }
1508#endif /* CONFIG_IEEE80211BE */
1509
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001510 if (conf->wmm_enabled < 0)
Hai Shaloma20dcd72022-02-04 13:43:00 -08001511 conf->wmm_enabled = hapd->iconf->ieee80211n |
1512 hapd->iconf->ieee80211ax;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001513
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001514#ifdef CONFIG_IEEE80211R_AP
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001515 if (is_zero_ether_addr(conf->r1_key_holder))
1516 os_memcpy(conf->r1_key_holder, hapd->own_addr, ETH_ALEN);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001517#endif /* CONFIG_IEEE80211R_AP */
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001518
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001519#ifdef CONFIG_MESH
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001520 if ((hapd->conf->mesh & MESH_ENABLED) && hapd->iface->mconf == NULL)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001521 flush_old_stations = 0;
1522#endif /* CONFIG_MESH */
1523
1524 if (flush_old_stations)
Hai Shalom4fbc08f2020-05-18 12:37:00 -07001525 hostapd_flush(hapd);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001526 hostapd_set_privacy(hapd, 0);
1527
Hai Shalomfdcde762020-04-02 11:19:20 -07001528#ifdef CONFIG_WEP
Hai Shalom4fbc08f2020-05-18 12:37:00 -07001529 if (!hostapd_drv_nl80211(hapd))
1530 hostapd_broadcast_wep_clear(hapd);
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001531 if (hostapd_setup_encryption(conf->iface, hapd))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001532 return -1;
Hai Shalomfdcde762020-04-02 11:19:20 -07001533#endif /* CONFIG_WEP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001534
1535 /*
1536 * Fetch the SSID from the system and use it or,
1537 * if one was specified in the config file, verify they
1538 * match.
1539 */
1540 ssid_len = hostapd_get_ssid(hapd, ssid, sizeof(ssid));
1541 if (ssid_len < 0) {
1542 wpa_printf(MSG_ERROR, "Could not read SSID from system");
1543 return -1;
1544 }
1545 if (conf->ssid.ssid_set) {
1546 /*
1547 * If SSID is specified in the config file and it differs
1548 * from what is being used then force installation of the
1549 * new SSID.
1550 */
1551 set_ssid = (conf->ssid.ssid_len != (size_t) ssid_len ||
1552 os_memcmp(conf->ssid.ssid, ssid, ssid_len) != 0);
1553 } else {
1554 /*
1555 * No SSID in the config file; just use the one we got
1556 * from the system.
1557 */
1558 set_ssid = 0;
1559 conf->ssid.ssid_len = ssid_len;
1560 os_memcpy(conf->ssid.ssid, ssid, conf->ssid.ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001561 }
1562
Hai Shalomfdcde762020-04-02 11:19:20 -07001563 /*
1564 * Short SSID calculation is identical to FCS and it is defined in
1565 * IEEE P802.11-REVmd/D3.0, 9.4.2.170.3 (Calculating the Short-SSID).
1566 */
Sunil Ravi89eba102022-09-13 21:04:37 -07001567 conf->ssid.short_ssid = ieee80211_crc32(conf->ssid.ssid,
1568 conf->ssid.ssid_len);
Hai Shalomfdcde762020-04-02 11:19:20 -07001569
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001570 if (!hostapd_drv_none(hapd)) {
Hai Shalomfdcde762020-04-02 11:19:20 -07001571 wpa_printf(MSG_DEBUG, "Using interface %s with hwaddr " MACSTR
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001572 " and ssid \"%s\"",
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001573 conf->iface, MAC2STR(hapd->own_addr),
1574 wpa_ssid_txt(conf->ssid.ssid, conf->ssid.ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001575 }
1576
1577 if (hostapd_setup_wpa_psk(conf)) {
1578 wpa_printf(MSG_ERROR, "WPA-PSK setup failed.");
1579 return -1;
1580 }
1581
1582 /* Set SSID for the kernel driver (to be used in beacon and probe
1583 * response frames) */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001584 if (set_ssid && hostapd_set_ssid(hapd, conf->ssid.ssid,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001585 conf->ssid.ssid_len)) {
1586 wpa_printf(MSG_ERROR, "Could not set SSID for kernel driver");
1587 return -1;
1588 }
1589
Dmitry Shmidt818ea482014-03-10 13:15:21 -07001590 if (wpa_debug_level <= MSG_MSGDUMP)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001591 conf->radius->msg_dumps = 1;
1592#ifndef CONFIG_NO_RADIUS
Hai Shalomc3565922019-10-28 11:58:20 -07001593
1594#ifdef CONFIG_SQLITE
1595 if (conf->radius_req_attr_sqlite) {
1596 if (sqlite3_open(conf->radius_req_attr_sqlite,
1597 &hapd->rad_attr_db)) {
1598 wpa_printf(MSG_ERROR, "Could not open SQLite file '%s'",
1599 conf->radius_req_attr_sqlite);
1600 return -1;
1601 }
1602
1603 wpa_printf(MSG_DEBUG, "Opening RADIUS attribute database: %s",
1604 conf->radius_req_attr_sqlite);
1605 if (!db_table_exists(hapd->rad_attr_db, "radius_attributes") &&
1606 db_table_create_radius_attributes(hapd->rad_attr_db) < 0)
1607 return -1;
1608 }
1609#endif /* CONFIG_SQLITE */
1610
Sunil Ravi99c035e2024-07-12 01:42:03 +00001611 if (hostapd_mld_is_first_bss(hapd)) {
Sunil Ravi7f769292024-07-23 22:21:32 +00001612 if (hostapd_bss_radius_init(hapd))
Dmitry Shmidt04949592012-07-19 12:16:46 -07001613 return -1;
Sunil Ravi2a14cf12023-11-21 00:54:38 +00001614 } else {
Sunil Ravi99c035e2024-07-12 01:42:03 +00001615#ifdef CONFIG_IEEE80211BE
1616 struct hostapd_data *f_bss;
1617
Sunil Ravi99c035e2024-07-12 01:42:03 +00001618 f_bss = hostapd_mld_get_first_bss(hapd);
1619 if (!f_bss)
1620 return -1;
Sunil Ravi7f769292024-07-23 22:21:32 +00001621
1622 if (!f_bss->radius) {
1623 wpa_printf(MSG_DEBUG,
1624 "MLD: First BSS RADIUS client does not exist. Init on its behalf");
1625
1626 if (hostapd_bss_radius_init(f_bss))
1627 return -1;
1628 }
1629
1630 wpa_printf(MSG_DEBUG,
1631 "MLD: Using RADIUS client of the first BSS");
Sunil Ravi99c035e2024-07-12 01:42:03 +00001632 hapd->radius = f_bss->radius;
1633 hapd->radius_das = f_bss->radius_das;
1634#endif /* CONFIG_IEEE80211BE */
Dmitry Shmidt04949592012-07-19 12:16:46 -07001635 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001636#endif /* CONFIG_NO_RADIUS */
1637
1638 if (hostapd_acl_init(hapd)) {
1639 wpa_printf(MSG_ERROR, "ACL initialization failed.");
1640 return -1;
1641 }
1642 if (hostapd_init_wps(hapd, conf))
1643 return -1;
1644
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001645#ifdef CONFIG_DPP
1646 hapd->gas = gas_query_ap_init(hapd, hapd->msg_ctx);
1647 if (!hapd->gas)
1648 return -1;
1649 if (hostapd_dpp_init(hapd))
1650 return -1;
1651#endif /* CONFIG_DPP */
1652
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001653#ifdef CONFIG_NAN_USD
1654 if (hostapd_nan_usd_init(hapd) < 0)
1655 return -1;
1656#endif /* CONFIG_NAN_USD */
1657
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001658 if (authsrv_init(hapd) < 0)
1659 return -1;
1660
1661 if (ieee802_1x_init(hapd)) {
1662 wpa_printf(MSG_ERROR, "IEEE 802.1X initialization failed.");
1663 return -1;
1664 }
1665
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001666 if ((conf->wpa || conf->osen) && hostapd_setup_wpa(hapd))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001667 return -1;
1668
1669 if (accounting_init(hapd)) {
1670 wpa_printf(MSG_ERROR, "Accounting initialization failed.");
1671 return -1;
1672 }
1673
Dmitry Shmidt04949592012-07-19 12:16:46 -07001674#ifdef CONFIG_INTERWORKING
1675 if (gas_serv_init(hapd)) {
1676 wpa_printf(MSG_ERROR, "GAS server initialization failed");
1677 return -1;
1678 }
Sunil Ravi99c035e2024-07-12 01:42:03 +00001679#endif /* CONFIG_INTERWORKING */
Dmitry Shmidt04949592012-07-19 12:16:46 -07001680
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001681 if (conf->qos_map_set_len &&
1682 hostapd_drv_set_qos_map(hapd, conf->qos_map_set,
1683 conf->qos_map_set_len)) {
1684 wpa_printf(MSG_ERROR, "Failed to initialize QoS Map");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001685 return -1;
1686 }
1687
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001688 if (conf->bss_load_update_period && bss_load_update_init(hapd)) {
1689 wpa_printf(MSG_ERROR, "BSS Load initialization failed");
1690 return -1;
1691 }
1692
Sunil Ravi036cec52023-03-29 11:35:17 -07001693 if (conf->bridge[0]) {
1694 /* Set explicitly configured bridge parameters that might have
1695 * been lost if the interface has been removed out of the
1696 * bridge. */
1697
1698 /* multicast to unicast on bridge ports */
1699 if (conf->bridge_multicast_to_unicast)
1700 hostapd_drv_br_port_set_attr(
1701 hapd, DRV_BR_PORT_ATTR_MCAST2UCAST, 1);
1702
1703 /* hairpin mode */
1704 if (conf->bridge_hairpin)
1705 hostapd_drv_br_port_set_attr(
1706 hapd, DRV_BR_PORT_ATTR_HAIRPIN_MODE, 1);
1707 }
1708
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001709 if (conf->proxy_arp) {
1710 if (x_snoop_init(hapd)) {
1711 wpa_printf(MSG_ERROR,
1712 "Generic snooping infrastructure initialization failed");
1713 return -1;
1714 }
1715
1716 if (dhcp_snoop_init(hapd)) {
1717 wpa_printf(MSG_ERROR,
1718 "DHCP snooping initialization failed");
1719 return -1;
1720 }
1721
1722 if (ndisc_snoop_init(hapd)) {
1723 wpa_printf(MSG_ERROR,
1724 "Neighbor Discovery snooping initialization failed");
1725 return -1;
1726 }
1727 }
1728
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001729 if (!hostapd_drv_none(hapd) && vlan_init(hapd)) {
1730 wpa_printf(MSG_ERROR, "VLAN initialization failed.");
1731 return -1;
1732 }
1733
Sunil Ravi640215c2023-06-28 23:08:09 +00001734 if (start_beacon && hostapd_start_beacon(hapd, flush_old_stations) < 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001735 return -1;
1736
Sunil Ravi640215c2023-06-28 23:08:09 +00001737 if (hapd->wpa_auth && wpa_init_keys(hapd->wpa_auth) < 0)
1738 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001739
1740 return 0;
1741}
1742
1743
1744static void hostapd_tx_queue_params(struct hostapd_iface *iface)
1745{
1746 struct hostapd_data *hapd = iface->bss[0];
1747 int i;
1748 struct hostapd_tx_queue_params *p;
1749
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001750#ifdef CONFIG_MESH
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001751 if ((hapd->conf->mesh & MESH_ENABLED) && iface->mconf == NULL)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001752 return;
1753#endif /* CONFIG_MESH */
1754
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001755 for (i = 0; i < NUM_TX_QUEUES; i++) {
1756 p = &iface->conf->tx_queue[i];
1757
1758 if (hostapd_set_tx_queue_params(hapd, i, p->aifs, p->cwmin,
1759 p->cwmax, p->burst)) {
1760 wpa_printf(MSG_DEBUG, "Failed to set TX queue "
1761 "parameters for queue %d.", i);
1762 /* Continue anyway */
1763 }
1764 }
1765}
1766
1767
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001768static int hostapd_set_acl_list(struct hostapd_data *hapd,
1769 struct mac_acl_entry *mac_acl,
1770 int n_entries, u8 accept_acl)
1771{
1772 struct hostapd_acl_params *acl_params;
1773 int i, err;
1774
1775 acl_params = os_zalloc(sizeof(*acl_params) +
1776 (n_entries * sizeof(acl_params->mac_acl[0])));
1777 if (!acl_params)
1778 return -ENOMEM;
1779
1780 for (i = 0; i < n_entries; i++)
1781 os_memcpy(acl_params->mac_acl[i].addr, mac_acl[i].addr,
1782 ETH_ALEN);
1783
1784 acl_params->acl_policy = accept_acl;
1785 acl_params->num_mac_acl = n_entries;
1786
1787 err = hostapd_drv_set_acl(hapd, acl_params);
1788
1789 os_free(acl_params);
1790
1791 return err;
1792}
1793
1794
Sunil Ravia04bd252022-05-02 22:54:18 -07001795int hostapd_set_acl(struct hostapd_data *hapd)
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001796{
1797 struct hostapd_config *conf = hapd->iconf;
Sunil Ravia04bd252022-05-02 22:54:18 -07001798 int err = 0;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001799 u8 accept_acl;
1800
1801 if (hapd->iface->drv_max_acl_mac_addrs == 0)
Sunil Ravia04bd252022-05-02 22:54:18 -07001802 return 0;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001803
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001804 if (conf->bss[0]->macaddr_acl == DENY_UNLESS_ACCEPTED) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001805 accept_acl = 1;
1806 err = hostapd_set_acl_list(hapd, conf->bss[0]->accept_mac,
1807 conf->bss[0]->num_accept_mac,
1808 accept_acl);
1809 if (err) {
1810 wpa_printf(MSG_DEBUG, "Failed to set accept acl");
Sunil Ravia04bd252022-05-02 22:54:18 -07001811 return -1;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001812 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001813 } else if (conf->bss[0]->macaddr_acl == ACCEPT_UNLESS_DENIED) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001814 accept_acl = 0;
1815 err = hostapd_set_acl_list(hapd, conf->bss[0]->deny_mac,
1816 conf->bss[0]->num_deny_mac,
1817 accept_acl);
1818 if (err) {
1819 wpa_printf(MSG_DEBUG, "Failed to set deny acl");
Sunil Ravia04bd252022-05-02 22:54:18 -07001820 return -1;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001821 }
1822 }
Sunil Ravia04bd252022-05-02 22:54:18 -07001823 return err;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001824}
1825
1826
Sunil Ravic0f5d412024-09-11 22:12:49 +00001827static int hostapd_set_ctrl_sock_iface(struct hostapd_data *hapd)
1828{
1829#ifdef CONFIG_IEEE80211BE
1830 int ret;
1831
1832 if (hapd->conf->mld_ap) {
1833 ret = os_snprintf(hapd->ctrl_sock_iface,
1834 sizeof(hapd->ctrl_sock_iface), "%s_%s%d",
1835 hapd->conf->iface, WPA_CTRL_IFACE_LINK_NAME,
1836 hapd->mld_link_id);
1837 if (os_snprintf_error(sizeof(hapd->ctrl_sock_iface), ret))
1838 return -1;
1839 } else {
1840 os_strlcpy(hapd->ctrl_sock_iface, hapd->conf->iface,
1841 sizeof(hapd->ctrl_sock_iface));
1842 }
1843#endif /* CONFIG_IEEE80211BE */
1844 return 0;
1845}
1846
1847
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001848static int start_ctrl_iface_bss(struct hostapd_data *hapd)
1849{
1850 if (!hapd->iface->interfaces ||
1851 !hapd->iface->interfaces->ctrl_iface_init)
1852 return 0;
1853
Sunil Ravic0f5d412024-09-11 22:12:49 +00001854 if (hostapd_set_ctrl_sock_iface(hapd))
1855 return -1;
1856
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001857 if (hapd->iface->interfaces->ctrl_iface_init(hapd)) {
1858 wpa_printf(MSG_ERROR,
1859 "Failed to setup control interface for %s",
1860 hapd->conf->iface);
1861 return -1;
1862 }
1863
1864 return 0;
1865}
1866
1867
1868static int start_ctrl_iface(struct hostapd_iface *iface)
1869{
1870 size_t i;
1871
1872 if (!iface->interfaces || !iface->interfaces->ctrl_iface_init)
1873 return 0;
1874
1875 for (i = 0; i < iface->num_bss; i++) {
1876 struct hostapd_data *hapd = iface->bss[i];
Sunil Ravic0f5d412024-09-11 22:12:49 +00001877
1878 if (hostapd_set_ctrl_sock_iface(hapd))
1879 return -1;
1880
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001881 if (iface->interfaces->ctrl_iface_init(hapd)) {
1882 wpa_printf(MSG_ERROR,
1883 "Failed to setup control interface for %s",
1884 hapd->conf->iface);
1885 return -1;
1886 }
1887 }
1888
1889 return 0;
1890}
1891
1892
Sunil Ravi2a14cf12023-11-21 00:54:38 +00001893/* When NO_IR flag is set and AP is stopped, clean up BSS parameters without
1894 * deinitializing the driver and the control interfaces. A subsequent
1895 * REG_CHANGE event can bring the AP back up.
1896 */
1897static void hostapd_no_ir_cleanup(struct hostapd_data *bss)
1898{
1899 hostapd_bss_deinit_no_free(bss);
Sunil Ravi99c035e2024-07-12 01:42:03 +00001900 hostapd_bss_link_deinit(bss);
Sunil Ravi2a14cf12023-11-21 00:54:38 +00001901 hostapd_free_hapd_data(bss);
1902 hostapd_cleanup_iface_partial(bss->iface);
1903}
1904
1905
1906static int hostapd_no_ir_channel_list_updated(struct hostapd_iface *iface,
1907 void *ctx)
1908{
1909 bool all_no_ir, is_6ghz;
1910 int i, j;
1911 struct hostapd_hw_modes *mode = NULL;
1912
1913 if (hostapd_get_hw_features(iface))
1914 return 0;
1915
1916 all_no_ir = true;
1917 is_6ghz = false;
1918
1919 for (i = 0; i < iface->num_hw_features; i++) {
1920 mode = &iface->hw_features[i];
1921
1922 if (mode->mode == iface->conf->hw_mode) {
1923 if (iface->freq > 0 &&
1924 !hw_mode_get_channel(mode, iface->freq, NULL)) {
1925 mode = NULL;
1926 continue;
1927 }
1928
1929 for (j = 0; j < mode->num_channels; j++) {
1930 if (!(mode->channels[j].flag &
1931 HOSTAPD_CHAN_NO_IR))
1932 all_no_ir = false;
1933
1934 if (is_6ghz_freq(mode->channels[j].freq))
1935 is_6ghz = true;
1936 }
1937 break;
1938 }
1939 }
1940
1941 if (!mode || !is_6ghz)
1942 return 0;
1943 iface->current_mode = mode;
1944
1945 if (iface->state == HAPD_IFACE_ENABLED) {
1946 if (!all_no_ir) {
1947 struct hostapd_channel_data *chan;
1948
1949 chan = hw_get_channel_freq(iface->current_mode->mode,
1950 iface->freq, NULL,
1951 iface->hw_features,
1952 iface->num_hw_features);
1953
1954 if (!chan) {
1955 wpa_printf(MSG_ERROR,
1956 "NO_IR: Could not derive chan from freq");
1957 return 0;
1958 }
1959
1960 if (!(chan->flag & HOSTAPD_CHAN_NO_IR))
1961 return 0;
1962 wpa_printf(MSG_DEBUG,
1963 "NO_IR: The current channel has NO_IR flag now, stop AP.");
1964 } else {
1965 wpa_printf(MSG_DEBUG,
1966 "NO_IR: All chan in new chanlist are NO_IR, stop AP.");
1967 }
1968
1969 hostapd_set_state(iface, HAPD_IFACE_NO_IR);
1970 iface->is_no_ir = true;
1971 hostapd_drv_stop_ap(iface->bss[0]);
1972 hostapd_no_ir_cleanup(iface->bss[0]);
1973 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_NO_IR);
1974 } else if (iface->state == HAPD_IFACE_NO_IR) {
1975 if (all_no_ir) {
1976 wpa_printf(MSG_DEBUG,
1977 "NO_IR: AP in NO_IR and all chan in the new chanlist are NO_IR. Ignore");
1978 return 0;
1979 }
1980
1981 if (!iface->conf->acs) {
1982 struct hostapd_channel_data *chan;
1983
1984 chan = hw_get_channel_freq(iface->current_mode->mode,
1985 iface->freq, NULL,
1986 iface->hw_features,
1987 iface->num_hw_features);
1988 if (!chan) {
1989 wpa_printf(MSG_ERROR,
1990 "NO_IR: Could not derive chan from freq");
1991 return 0;
1992 }
1993
1994 /* If the last operating channel is NO_IR, trigger ACS.
1995 */
1996 if (chan->flag & HOSTAPD_CHAN_NO_IR) {
1997 iface->freq = 0;
1998 iface->conf->channel = 0;
1999 if (acs_init(iface) != HOSTAPD_CHAN_ACS)
2000 wpa_printf(MSG_ERROR,
2001 "NO_IR: Could not start ACS");
2002 return 0;
2003 }
2004 }
2005
2006 setup_interface2(iface);
2007 }
2008
2009 return 0;
2010}
2011
2012
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002013static void channel_list_update_timeout(void *eloop_ctx, void *timeout_ctx)
2014{
2015 struct hostapd_iface *iface = eloop_ctx;
2016
2017 if (!iface->wait_channel_update) {
2018 wpa_printf(MSG_INFO, "Channel list update timeout, but interface was not waiting for it");
2019 return;
2020 }
2021
2022 /*
2023 * It is possible that the existing channel list is acceptable, so try
2024 * to proceed.
2025 */
2026 wpa_printf(MSG_DEBUG, "Channel list update timeout - try to continue anyway");
2027 setup_interface2(iface);
2028}
2029
2030
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002031void hostapd_channel_list_updated(struct hostapd_iface *iface, int initiator)
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002032{
Sunil Ravi2a14cf12023-11-21 00:54:38 +00002033 if (initiator == REGDOM_SET_BY_DRIVER) {
2034 hostapd_for_each_interface(iface->interfaces,
2035 hostapd_no_ir_channel_list_updated,
2036 NULL);
2037 return;
2038 }
2039
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002040 if (!iface->wait_channel_update || initiator != REGDOM_SET_BY_USER)
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002041 return;
2042
2043 wpa_printf(MSG_DEBUG, "Channel list updated - continue setup");
2044 eloop_cancel_timeout(channel_list_update_timeout, iface, NULL);
2045 setup_interface2(iface);
2046}
2047
2048
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002049static int setup_interface(struct hostapd_iface *iface)
2050{
2051 struct hostapd_data *hapd = iface->bss[0];
2052 size_t i;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002053
Dmitry Shmidta38abf92014-03-06 13:38:44 -08002054 /*
2055 * It is possible that setup_interface() is called after the interface
2056 * was disabled etc., in which case driver_ap_teardown is possibly set
2057 * to 1. Clear it here so any other key/station deletion, which is not
2058 * part of a teardown flow, would also call the relevant driver
2059 * callbacks.
2060 */
2061 iface->driver_ap_teardown = 0;
2062
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002063 if (!iface->phy[0]) {
2064 const char *phy = hostapd_drv_get_radio_name(hapd);
2065 if (phy) {
2066 wpa_printf(MSG_DEBUG, "phy: %s", phy);
2067 os_strlcpy(iface->phy, phy, sizeof(iface->phy));
2068 }
2069 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002070
2071 /*
2072 * Make sure that all BSSes get configured with a pointer to the same
2073 * driver interface.
2074 */
2075 for (i = 1; i < iface->num_bss; i++) {
2076 iface->bss[i]->driver = hapd->driver;
2077 iface->bss[i]->drv_priv = hapd->drv_priv;
2078 }
2079
2080 if (hostapd_validate_bssid_configuration(iface))
2081 return -1;
2082
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002083 /*
2084 * Initialize control interfaces early to allow external monitoring of
2085 * channel setup operations that may take considerable amount of time
2086 * especially for DFS cases.
2087 */
2088 if (start_ctrl_iface(iface))
2089 return -1;
2090
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002091 if (hapd->iconf->country[0] && hapd->iconf->country[1]) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002092 char country[4], previous_country[4];
2093
2094 hostapd_set_state(iface, HAPD_IFACE_COUNTRY_UPDATE);
2095 if (hostapd_get_country(hapd, previous_country) < 0)
2096 previous_country[0] = '\0';
2097
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002098 os_memcpy(country, hapd->iconf->country, 3);
2099 country[3] = '\0';
2100 if (hostapd_set_country(hapd, country) < 0) {
2101 wpa_printf(MSG_ERROR, "Failed to set country code");
2102 return -1;
2103 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002104
2105 wpa_printf(MSG_DEBUG, "Previous country code %s, new country code %s",
2106 previous_country, country);
2107
2108 if (os_strncmp(previous_country, country, 2) != 0) {
2109 wpa_printf(MSG_DEBUG, "Continue interface setup after channel list update");
2110 iface->wait_channel_update = 1;
Dmitry Shmidt97672262014-02-03 13:02:54 -08002111 eloop_register_timeout(5, 0,
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002112 channel_list_update_timeout,
2113 iface, NULL);
2114 return 0;
2115 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002116 }
2117
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002118 return setup_interface2(iface);
2119}
2120
2121
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08002122static int configured_fixed_chan_to_freq(struct hostapd_iface *iface)
2123{
2124 int freq, i, j;
2125
2126 if (!iface->conf->channel)
2127 return 0;
2128 if (iface->conf->op_class) {
2129 freq = ieee80211_chan_to_freq(NULL, iface->conf->op_class,
2130 iface->conf->channel);
2131 if (freq < 0) {
2132 wpa_printf(MSG_INFO,
2133 "Could not convert op_class %u channel %u to operating frequency",
2134 iface->conf->op_class, iface->conf->channel);
2135 return -1;
2136 }
2137 iface->freq = freq;
2138 return 0;
2139 }
2140
2141 /* Old configurations using only 2.4/5/60 GHz bands may not specify the
2142 * op_class parameter. Select a matching channel from the configured
2143 * mode using the channel parameter for these cases.
2144 */
2145 for (j = 0; j < iface->num_hw_features; j++) {
2146 struct hostapd_hw_modes *mode = &iface->hw_features[j];
2147
2148 if (iface->conf->hw_mode != HOSTAPD_MODE_IEEE80211ANY &&
2149 iface->conf->hw_mode != mode->mode)
2150 continue;
2151 for (i = 0; i < mode->num_channels; i++) {
2152 struct hostapd_channel_data *chan = &mode->channels[i];
2153
2154 if (chan->chan == iface->conf->channel &&
2155 !is_6ghz_freq(chan->freq)) {
2156 iface->freq = chan->freq;
2157 return 0;
2158 }
2159 }
2160 }
2161
2162 wpa_printf(MSG_INFO, "Could not determine operating frequency");
2163 return -1;
2164}
2165
2166
Hai Shaloma20dcd72022-02-04 13:43:00 -08002167static void hostapd_set_6ghz_sec_chan(struct hostapd_iface *iface)
2168{
Sunil Ravi640215c2023-06-28 23:08:09 +00002169 int bw;
Hai Shaloma20dcd72022-02-04 13:43:00 -08002170
2171 if (!is_6ghz_op_class(iface->conf->op_class))
2172 return;
2173
Sunil Ravi640215c2023-06-28 23:08:09 +00002174 bw = op_class_to_bandwidth(iface->conf->op_class);
Hai Shaloma20dcd72022-02-04 13:43:00 -08002175 /* Assign the secondary channel if absent in config for
2176 * bandwidths > 20 MHz */
Sunil Ravi640215c2023-06-28 23:08:09 +00002177 if (bw >= 40 && !iface->conf->secondary_channel) {
Hai Shaloma20dcd72022-02-04 13:43:00 -08002178 if (((iface->conf->channel - 1) / 4) % 2)
2179 iface->conf->secondary_channel = -1;
2180 else
2181 iface->conf->secondary_channel = 1;
2182 }
2183}
2184
2185
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002186static int setup_interface2(struct hostapd_iface *iface)
2187{
2188 iface->wait_channel_update = 0;
Sunil Ravi2a14cf12023-11-21 00:54:38 +00002189 iface->is_no_ir = false;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002190
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002191 if (hostapd_get_hw_features(iface)) {
2192 /* Not all drivers support this yet, so continue without hw
2193 * feature data. */
2194 } else {
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08002195 int ret;
2196
Sunil Ravi99c035e2024-07-12 01:42:03 +00002197 if (iface->conf->acs && !iface->is_ch_switch_dfs) {
Sunil Ravi036cec52023-03-29 11:35:17 -07002198 iface->freq = 0;
2199 iface->conf->channel = 0;
2200 }
Sunil Ravi99c035e2024-07-12 01:42:03 +00002201 iface->is_ch_switch_dfs = false;
Sunil Ravi036cec52023-03-29 11:35:17 -07002202
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08002203 ret = configured_fixed_chan_to_freq(iface);
2204 if (ret < 0)
2205 goto fail;
2206
2207 if (iface->conf->op_class) {
Sunil8cd6f4d2022-06-28 18:40:46 +00002208 enum oper_chan_width ch_width;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08002209
2210 ch_width = op_class_to_ch_width(iface->conf->op_class);
2211 hostapd_set_oper_chwidth(iface->conf, ch_width);
Hai Shaloma20dcd72022-02-04 13:43:00 -08002212 hostapd_set_6ghz_sec_chan(iface);
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08002213 }
2214
2215 ret = hostapd_select_hw_mode(iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002216 if (ret < 0) {
2217 wpa_printf(MSG_ERROR, "Could not select hw_mode and "
2218 "channel. (%d)", ret);
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002219 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002220 }
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002221 if (ret == 1) {
2222 wpa_printf(MSG_DEBUG, "Interface initialization will be completed in a callback (ACS)");
2223 return 0;
2224 }
Hai Shalomc3565922019-10-28 11:58:20 -07002225 ret = hostapd_check_edmg_capab(iface);
2226 if (ret < 0)
2227 goto fail;
Hai Shalom60840252021-02-19 19:02:11 -08002228 ret = hostapd_check_he_6ghz_capab(iface);
2229 if (ret < 0)
2230 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002231 ret = hostapd_check_ht_capab(iface);
2232 if (ret < 0)
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002233 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002234 if (ret == 1) {
2235 wpa_printf(MSG_DEBUG, "Interface initialization will "
2236 "be completed in a callback");
2237 return 0;
2238 }
Dmitry Shmidt051af732013-10-22 13:52:46 -07002239
2240 if (iface->conf->ieee80211h)
2241 wpa_printf(MSG_DEBUG, "DFS support is enabled");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002242 }
2243 return hostapd_setup_interface_complete(iface, 0);
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002244
2245fail:
Sunil Ravi2a14cf12023-11-21 00:54:38 +00002246 if (iface->is_no_ir) {
2247 /* If AP is in NO_IR state, it can be reenabled by the driver
2248 * regulatory update and EVENT_CHANNEL_LIST_CHANGED. */
2249 hostapd_set_state(iface, HAPD_IFACE_NO_IR);
2250 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_NO_IR);
2251 return 0;
2252 }
2253
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002254 hostapd_set_state(iface, HAPD_IFACE_DISABLED);
2255 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
2256 if (iface->interfaces && iface->interfaces->terminate_on_error)
2257 eloop_terminate();
2258 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002259}
2260
2261
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002262#ifdef CONFIG_FST
2263
2264static const u8 * fst_hostapd_get_bssid_cb(void *ctx)
2265{
2266 struct hostapd_data *hapd = ctx;
2267
2268 return hapd->own_addr;
2269}
2270
2271
2272static void fst_hostapd_get_channel_info_cb(void *ctx,
2273 enum hostapd_hw_mode *hw_mode,
2274 u8 *channel)
2275{
2276 struct hostapd_data *hapd = ctx;
2277
2278 *hw_mode = ieee80211_freq_to_chan(hapd->iface->freq, channel);
2279}
2280
2281
Sunil8cd6f4d2022-06-28 18:40:46 +00002282static int fst_hostapd_get_hw_modes_cb(void *ctx,
2283 struct hostapd_hw_modes **modes)
2284{
2285 struct hostapd_data *hapd = ctx;
2286
2287 *modes = hapd->iface->hw_features;
2288 return hapd->iface->num_hw_features;
2289}
2290
2291
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002292static void fst_hostapd_set_ies_cb(void *ctx, const struct wpabuf *fst_ies)
2293{
2294 struct hostapd_data *hapd = ctx;
2295
2296 if (hapd->iface->fst_ies != fst_ies) {
2297 hapd->iface->fst_ies = fst_ies;
2298 if (ieee802_11_set_beacon(hapd))
2299 wpa_printf(MSG_WARNING, "FST: Cannot set beacon");
2300 }
2301}
2302
2303
2304static int fst_hostapd_send_action_cb(void *ctx, const u8 *da,
2305 struct wpabuf *buf)
2306{
2307 struct hostapd_data *hapd = ctx;
2308
2309 return hostapd_drv_send_action(hapd, hapd->iface->freq, 0, da,
2310 wpabuf_head(buf), wpabuf_len(buf));
2311}
2312
2313
2314static const struct wpabuf * fst_hostapd_get_mb_ie_cb(void *ctx, const u8 *addr)
2315{
2316 struct hostapd_data *hapd = ctx;
2317 struct sta_info *sta = ap_get_sta(hapd, addr);
2318
2319 return sta ? sta->mb_ies : NULL;
2320}
2321
2322
2323static void fst_hostapd_update_mb_ie_cb(void *ctx, const u8 *addr,
2324 const u8 *buf, size_t size)
2325{
2326 struct hostapd_data *hapd = ctx;
2327 struct sta_info *sta = ap_get_sta(hapd, addr);
2328
2329 if (sta) {
2330 struct mb_ies_info info;
2331
2332 if (!mb_ies_info_by_ies(&info, buf, size)) {
2333 wpabuf_free(sta->mb_ies);
2334 sta->mb_ies = mb_ies_by_info(&info);
2335 }
2336 }
2337}
2338
2339
2340static const u8 * fst_hostapd_get_sta(struct fst_get_peer_ctx **get_ctx,
Hai Shalome21d4e82020-04-29 16:34:06 -07002341 bool mb_only)
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002342{
2343 struct sta_info *s = (struct sta_info *) *get_ctx;
2344
2345 if (mb_only) {
2346 for (; s && !s->mb_ies; s = s->next)
2347 ;
2348 }
2349
2350 if (s) {
2351 *get_ctx = (struct fst_get_peer_ctx *) s->next;
2352
2353 return s->addr;
2354 }
2355
2356 *get_ctx = NULL;
2357 return NULL;
2358}
2359
2360
2361static const u8 * fst_hostapd_get_peer_first(void *ctx,
2362 struct fst_get_peer_ctx **get_ctx,
Hai Shalome21d4e82020-04-29 16:34:06 -07002363 bool mb_only)
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002364{
2365 struct hostapd_data *hapd = ctx;
2366
2367 *get_ctx = (struct fst_get_peer_ctx *) hapd->sta_list;
2368
2369 return fst_hostapd_get_sta(get_ctx, mb_only);
2370}
2371
2372
2373static const u8 * fst_hostapd_get_peer_next(void *ctx,
2374 struct fst_get_peer_ctx **get_ctx,
Hai Shalome21d4e82020-04-29 16:34:06 -07002375 bool mb_only)
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002376{
2377 return fst_hostapd_get_sta(get_ctx, mb_only);
2378}
2379
2380
2381void fst_hostapd_fill_iface_obj(struct hostapd_data *hapd,
2382 struct fst_wpa_obj *iface_obj)
2383{
Sunil8cd6f4d2022-06-28 18:40:46 +00002384 os_memset(iface_obj, 0, sizeof(*iface_obj));
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002385 iface_obj->ctx = hapd;
2386 iface_obj->get_bssid = fst_hostapd_get_bssid_cb;
2387 iface_obj->get_channel_info = fst_hostapd_get_channel_info_cb;
Sunil8cd6f4d2022-06-28 18:40:46 +00002388 iface_obj->get_hw_modes = fst_hostapd_get_hw_modes_cb;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002389 iface_obj->set_ies = fst_hostapd_set_ies_cb;
2390 iface_obj->send_action = fst_hostapd_send_action_cb;
2391 iface_obj->get_mb_ie = fst_hostapd_get_mb_ie_cb;
2392 iface_obj->update_mb_ie = fst_hostapd_update_mb_ie_cb;
2393 iface_obj->get_peer_first = fst_hostapd_get_peer_first;
2394 iface_obj->get_peer_next = fst_hostapd_get_peer_next;
2395}
2396
2397#endif /* CONFIG_FST */
2398
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002399#ifdef CONFIG_OWE
2400
2401static int hostapd_owe_iface_iter(struct hostapd_iface *iface, void *ctx)
2402{
2403 struct hostapd_data *hapd = ctx;
2404 size_t i;
2405
2406 for (i = 0; i < iface->num_bss; i++) {
2407 struct hostapd_data *bss = iface->bss[i];
2408
2409 if (os_strcmp(hapd->conf->owe_transition_ifname,
2410 bss->conf->iface) != 0)
2411 continue;
2412
2413 wpa_printf(MSG_DEBUG,
2414 "OWE: ifname=%s found transition mode ifname=%s BSSID "
2415 MACSTR " SSID %s",
2416 hapd->conf->iface, bss->conf->iface,
2417 MAC2STR(bss->own_addr),
2418 wpa_ssid_txt(bss->conf->ssid.ssid,
2419 bss->conf->ssid.ssid_len));
2420 if (!bss->conf->ssid.ssid_set || !bss->conf->ssid.ssid_len ||
2421 is_zero_ether_addr(bss->own_addr))
2422 continue;
2423
2424 os_memcpy(hapd->conf->owe_transition_bssid, bss->own_addr,
2425 ETH_ALEN);
2426 os_memcpy(hapd->conf->owe_transition_ssid,
2427 bss->conf->ssid.ssid, bss->conf->ssid.ssid_len);
2428 hapd->conf->owe_transition_ssid_len = bss->conf->ssid.ssid_len;
2429 wpa_printf(MSG_DEBUG,
2430 "OWE: Copied transition mode information");
2431 return 1;
2432 }
2433
2434 return 0;
2435}
2436
2437
2438int hostapd_owe_trans_get_info(struct hostapd_data *hapd)
2439{
2440 if (hapd->conf->owe_transition_ssid_len > 0 &&
2441 !is_zero_ether_addr(hapd->conf->owe_transition_bssid))
2442 return 0;
2443
2444 /* Find transition mode SSID/BSSID information from a BSS operated by
2445 * this hostapd instance. */
2446 if (!hapd->iface->interfaces ||
2447 !hapd->iface->interfaces->for_each_interface)
2448 return hostapd_owe_iface_iter(hapd->iface, hapd);
2449 else
2450 return hapd->iface->interfaces->for_each_interface(
2451 hapd->iface->interfaces, hostapd_owe_iface_iter, hapd);
2452}
2453
2454
2455static int hostapd_owe_iface_iter2(struct hostapd_iface *iface, void *ctx)
2456{
2457 size_t i;
2458
2459 for (i = 0; i < iface->num_bss; i++) {
2460 struct hostapd_data *bss = iface->bss[i];
2461 int res;
2462
2463 if (!bss->conf->owe_transition_ifname[0])
2464 continue;
Hai Shalom899fcc72020-10-19 14:38:18 -07002465 if (bss->iface->state != HAPD_IFACE_ENABLED) {
2466 wpa_printf(MSG_DEBUG,
2467 "OWE: Interface %s state %s - defer beacon update",
2468 bss->conf->iface,
2469 hostapd_state_text(bss->iface->state));
2470 continue;
2471 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002472 res = hostapd_owe_trans_get_info(bss);
2473 if (res == 0)
2474 continue;
2475 wpa_printf(MSG_DEBUG,
2476 "OWE: Matching transition mode interface enabled - update beacon data for %s",
2477 bss->conf->iface);
2478 ieee802_11_set_beacon(bss);
2479 }
2480
2481 return 0;
2482}
2483
2484#endif /* CONFIG_OWE */
2485
2486
2487static void hostapd_owe_update_trans(struct hostapd_iface *iface)
2488{
2489#ifdef CONFIG_OWE
2490 /* Check whether the enabled BSS can complete OWE transition mode
2491 * configuration for any pending interface. */
2492 if (!iface->interfaces ||
2493 !iface->interfaces->for_each_interface)
2494 hostapd_owe_iface_iter2(iface, NULL);
2495 else
2496 iface->interfaces->for_each_interface(
2497 iface->interfaces, hostapd_owe_iface_iter2, NULL);
2498#endif /* CONFIG_OWE */
2499}
2500
2501
Roshan Pius3a1667e2018-07-03 15:17:14 -07002502static void hostapd_interface_setup_failure_handler(void *eloop_ctx,
2503 void *timeout_ctx)
2504{
2505 struct hostapd_iface *iface = eloop_ctx;
2506 struct hostapd_data *hapd;
2507
2508 if (iface->num_bss < 1 || !iface->bss || !iface->bss[0])
2509 return;
2510 hapd = iface->bss[0];
2511 if (hapd->setup_complete_cb)
2512 hapd->setup_complete_cb(hapd->setup_complete_cb_ctx);
2513}
2514
2515
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002516static int hostapd_setup_interface_complete_sync(struct hostapd_iface *iface,
2517 int err)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002518{
2519 struct hostapd_data *hapd = iface->bss[0];
2520 size_t j;
2521 u8 *prev_addr;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002522 int delay_apply_cfg = 0;
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08002523 int res_dfs_offload = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002524
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002525 if (err)
2526 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002527
2528 wpa_printf(MSG_DEBUG, "Completing interface initialization");
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08002529 if (iface->freq) {
Dmitry Shmidt051af732013-10-22 13:52:46 -07002530#ifdef NEED_AP_MLME
2531 int res;
2532#endif /* NEED_AP_MLME */
2533
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002534 wpa_printf(MSG_DEBUG, "Mode: %s Channel: %d "
2535 "Frequency: %d MHz",
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002536 hostapd_hw_mode_txt(iface->conf->hw_mode),
2537 iface->conf->channel, iface->freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002538
Sunil Ravic0f5d412024-09-11 22:12:49 +00002539 if (hostapd_set_current_hw_info(iface, iface->freq)) {
2540 wpa_printf(MSG_ERROR,
2541 "Failed to set current hardware info");
2542 goto fail;
2543 }
2544
Dmitry Shmidt051af732013-10-22 13:52:46 -07002545#ifdef NEED_AP_MLME
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07002546 /* Handle DFS only if it is not offloaded to the driver */
2547 if (!(iface->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD)) {
2548 /* Check DFS */
2549 res = hostapd_handle_dfs(iface);
2550 if (res <= 0) {
2551 if (res < 0)
2552 goto fail;
2553 return res;
2554 }
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08002555 } else {
2556 /* If DFS is offloaded to the driver */
2557 res_dfs_offload = hostapd_handle_dfs_offload(iface);
2558 if (res_dfs_offload <= 0) {
2559 if (res_dfs_offload < 0)
2560 goto fail;
2561 } else {
2562 wpa_printf(MSG_DEBUG,
2563 "Proceed with AP/channel setup");
2564 /*
2565 * If this is a DFS channel, move to completing
2566 * AP setup.
2567 */
2568 if (res_dfs_offload == 1)
2569 goto dfs_offload;
2570 /* Otherwise fall through. */
2571 }
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002572 }
Dmitry Shmidt051af732013-10-22 13:52:46 -07002573#endif /* NEED_AP_MLME */
2574
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002575#ifdef CONFIG_MESH
2576 if (iface->mconf != NULL) {
2577 wpa_printf(MSG_DEBUG,
2578 "%s: Mesh configuration will be applied while joining the mesh network",
2579 iface->bss[0]->conf->iface);
2580 delay_apply_cfg = 1;
2581 }
2582#endif /* CONFIG_MESH */
2583
2584 if (!delay_apply_cfg &&
2585 hostapd_set_freq(hapd, hapd->iconf->hw_mode, iface->freq,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002586 hapd->iconf->channel,
Hai Shalomc3565922019-10-28 11:58:20 -07002587 hapd->iconf->enable_edmg,
2588 hapd->iconf->edmg_channel,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002589 hapd->iconf->ieee80211n,
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002590 hapd->iconf->ieee80211ac,
Hai Shalom81f62d82019-07-22 12:10:00 -07002591 hapd->iconf->ieee80211ax,
Sunil Ravia04bd252022-05-02 22:54:18 -07002592 hapd->iconf->ieee80211be,
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002593 hapd->iconf->secondary_channel,
Hai Shalom81f62d82019-07-22 12:10:00 -07002594 hostapd_get_oper_chwidth(hapd->iconf),
2595 hostapd_get_oper_centr_freq_seg0_idx(
2596 hapd->iconf),
2597 hostapd_get_oper_centr_freq_seg1_idx(
2598 hapd->iconf))) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002599 wpa_printf(MSG_ERROR, "Could not set channel for "
2600 "kernel driver");
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002601 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002602 }
2603 }
2604
2605 if (iface->current_mode) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002606 if (hostapd_prepare_rates(iface, iface->current_mode)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002607 wpa_printf(MSG_ERROR, "Failed to prepare rates "
2608 "table.");
2609 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
2610 HOSTAPD_LEVEL_WARNING,
2611 "Failed to prepare rates table.");
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002612 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002613 }
2614 }
2615
Hai Shalom021b0b52019-04-10 11:17:58 -07002616 if (hapd->iconf->rts_threshold >= -1 &&
2617 hostapd_set_rts(hapd, hapd->iconf->rts_threshold) &&
2618 hapd->iconf->rts_threshold >= -1) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002619 wpa_printf(MSG_ERROR, "Could not set RTS threshold for "
2620 "kernel driver");
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002621 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002622 }
2623
Hai Shalom021b0b52019-04-10 11:17:58 -07002624 if (hapd->iconf->fragm_threshold >= -1 &&
2625 hostapd_set_frag(hapd, hapd->iconf->fragm_threshold) &&
2626 hapd->iconf->fragm_threshold != -1) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002627 wpa_printf(MSG_ERROR, "Could not set fragmentation threshold "
2628 "for kernel driver");
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002629 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002630 }
2631
2632 prev_addr = hapd->own_addr;
2633
2634 for (j = 0; j < iface->num_bss; j++) {
2635 hapd = iface->bss[j];
2636 if (j)
2637 os_memcpy(hapd->own_addr, prev_addr, ETH_ALEN);
Sunil Ravi77d572f2023-01-17 23:58:31 +00002638 if (hostapd_setup_bss(hapd, j == 0, !iface->conf->mbssid)) {
Hai Shalom021b0b52019-04-10 11:17:58 -07002639 for (;;) {
Dmitry Shmidt71757432014-06-02 13:50:35 -07002640 hapd = iface->bss[j];
2641 hostapd_bss_deinit_no_free(hapd);
2642 hostapd_free_hapd_data(hapd);
Hai Shalom021b0b52019-04-10 11:17:58 -07002643 if (j == 0)
2644 break;
2645 j--;
2646 }
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002647 goto fail;
Dmitry Shmidt71757432014-06-02 13:50:35 -07002648 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002649 if (is_zero_ether_addr(hapd->conf->bssid))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002650 prev_addr = hapd->own_addr;
2651 }
Sunil Ravi77d572f2023-01-17 23:58:31 +00002652
2653 if (hapd->iconf->mbssid) {
2654 for (j = 0; hapd->iconf->mbssid && j < iface->num_bss; j++) {
2655 hapd = iface->bss[j];
2656 if (hostapd_start_beacon(hapd, true)) {
2657 for (;;) {
2658 hapd = iface->bss[j];
2659 hostapd_bss_deinit_no_free(hapd);
2660 hostapd_free_hapd_data(hapd);
2661 if (j == 0)
2662 break;
2663 j--;
2664 }
2665 goto fail;
2666 }
2667 }
2668 }
2669
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002670 hapd = iface->bss[0];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002671
2672 hostapd_tx_queue_params(iface);
2673
2674 ap_list_init(iface);
2675
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07002676 hostapd_set_acl(hapd);
2677
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002678 if (hostapd_driver_commit(hapd) < 0) {
2679 wpa_printf(MSG_ERROR, "%s: Failed to commit driver "
2680 "configuration", __func__);
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002681 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002682 }
2683
Jouni Malinen87fd2792011-05-16 18:35:42 +03002684 /*
2685 * WPS UPnP module can be initialized only when the "upnp_iface" is up.
2686 * If "interface" and "upnp_iface" are the same (e.g., non-bridge
2687 * mode), the interface is up only after driver_commit, so initialize
2688 * WPS after driver_commit.
2689 */
2690 for (j = 0; j < iface->num_bss; j++) {
2691 if (hostapd_init_wps_complete(iface->bss[j]))
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002692 goto fail;
Jouni Malinen87fd2792011-05-16 18:35:42 +03002693 }
2694
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08002695 if ((iface->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
2696 !res_dfs_offload) {
2697 /*
2698 * If freq is DFS, and DFS is offloaded to the driver, then wait
2699 * for CAC to complete.
2700 */
2701 wpa_printf(MSG_DEBUG, "%s: Wait for CAC to complete", __func__);
2702 return res_dfs_offload;
2703 }
2704
2705#ifdef NEED_AP_MLME
2706dfs_offload:
2707#endif /* NEED_AP_MLME */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002708
2709#ifdef CONFIG_FST
2710 if (hapd->iconf->fst_cfg.group_id[0]) {
2711 struct fst_wpa_obj iface_obj;
2712
2713 fst_hostapd_fill_iface_obj(hapd, &iface_obj);
2714 iface->fst = fst_attach(hapd->conf->iface, hapd->own_addr,
2715 &iface_obj, &hapd->iconf->fst_cfg);
2716 if (!iface->fst) {
2717 wpa_printf(MSG_ERROR, "Could not attach to FST %s",
2718 hapd->iconf->fst_cfg.group_id);
2719 goto fail;
2720 }
2721 }
2722#endif /* CONFIG_FST */
2723
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002724 hostapd_set_state(iface, HAPD_IFACE_ENABLED);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002725 hostapd_owe_update_trans(iface);
Hai Shalom81f62d82019-07-22 12:10:00 -07002726 airtime_policy_update_init(iface);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002727 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_ENABLED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002728 if (hapd->setup_complete_cb)
2729 hapd->setup_complete_cb(hapd->setup_complete_cb_ctx);
2730
Hai Shalom60840252021-02-19 19:02:11 -08002731#ifdef CONFIG_MESH
2732 if (delay_apply_cfg && !iface->mconf) {
2733 wpa_printf(MSG_ERROR, "Error while completing mesh init");
2734 goto fail;
2735 }
2736#endif /* CONFIG_MESH */
2737
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002738 wpa_printf(MSG_DEBUG, "%s: Setup of interface done.",
2739 iface->bss[0]->conf->iface);
Dmitry Shmidtb96dad42013-11-05 10:07:29 -08002740 if (iface->interfaces && iface->interfaces->terminate_on_error > 0)
2741 iface->interfaces->terminate_on_error--;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002742
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002743 for (j = 0; j < iface->num_bss; j++)
Hai Shalom74f70d42019-02-11 14:42:39 -08002744 hostapd_neighbor_set_own_report(iface->bss[j]);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002745
Sunil Ravi2a14cf12023-11-21 00:54:38 +00002746 if (iface->interfaces && iface->interfaces->count > 1)
2747 ieee802_11_set_beacons(iface);
2748
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002749 return 0;
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002750
2751fail:
2752 wpa_printf(MSG_ERROR, "Interface initialization failed");
Sunil Ravi2a14cf12023-11-21 00:54:38 +00002753
2754 if (iface->is_no_ir) {
2755 hostapd_set_state(iface, HAPD_IFACE_NO_IR);
2756 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_EVENT_NO_IR);
2757 return 0;
2758 }
2759
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002760 hostapd_set_state(iface, HAPD_IFACE_DISABLED);
2761 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002762#ifdef CONFIG_FST
2763 if (iface->fst) {
2764 fst_detach(iface->fst);
2765 iface->fst = NULL;
2766 }
2767#endif /* CONFIG_FST */
Roshan Pius3a1667e2018-07-03 15:17:14 -07002768
2769 if (iface->interfaces && iface->interfaces->terminate_on_error) {
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002770 eloop_terminate();
Roshan Pius3a1667e2018-07-03 15:17:14 -07002771 } else if (hapd->setup_complete_cb) {
2772 /*
2773 * Calling hapd->setup_complete_cb directly may cause iface
2774 * deinitialization which may be accessed later by the caller.
2775 */
2776 eloop_register_timeout(0, 0,
2777 hostapd_interface_setup_failure_handler,
2778 iface, NULL);
2779 }
2780
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002781 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002782}
2783
2784
2785/**
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002786 * hostapd_setup_interface_complete - Complete interface setup
2787 *
2788 * This function is called when previous steps in the interface setup has been
2789 * completed. This can also start operations, e.g., DFS, that will require
2790 * additional processing before interface is ready to be enabled. Such
2791 * operations will call this function from eloop callbacks when finished.
2792 */
2793int hostapd_setup_interface_complete(struct hostapd_iface *iface, int err)
2794{
2795 struct hapd_interfaces *interfaces = iface->interfaces;
2796 struct hostapd_data *hapd = iface->bss[0];
2797 unsigned int i;
2798 int not_ready_in_sync_ifaces = 0;
2799
2800 if (!iface->need_to_start_in_sync)
2801 return hostapd_setup_interface_complete_sync(iface, err);
2802
2803 if (err) {
2804 wpa_printf(MSG_ERROR, "Interface initialization failed");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002805 iface->need_to_start_in_sync = 0;
Sunil Ravi2a14cf12023-11-21 00:54:38 +00002806
2807 if (iface->is_no_ir) {
2808 hostapd_set_state(iface, HAPD_IFACE_NO_IR);
2809 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_EVENT_NO_IR);
2810 return 0;
2811 }
2812
2813 hostapd_set_state(iface, HAPD_IFACE_DISABLED);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002814 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
2815 if (interfaces && interfaces->terminate_on_error)
2816 eloop_terminate();
2817 return -1;
2818 }
2819
2820 if (iface->ready_to_start_in_sync) {
2821 /* Already in ready and waiting. should never happpen */
2822 return 0;
2823 }
2824
2825 for (i = 0; i < interfaces->count; i++) {
2826 if (interfaces->iface[i]->need_to_start_in_sync &&
2827 !interfaces->iface[i]->ready_to_start_in_sync)
2828 not_ready_in_sync_ifaces++;
2829 }
2830
2831 /*
2832 * Check if this is the last interface, if yes then start all the other
2833 * waiting interfaces. If not, add this interface to the waiting list.
2834 */
2835 if (not_ready_in_sync_ifaces > 1 && iface->state == HAPD_IFACE_DFS) {
2836 /*
2837 * If this interface went through CAC, do not synchronize, just
2838 * start immediately.
2839 */
2840 iface->need_to_start_in_sync = 0;
2841 wpa_printf(MSG_INFO,
2842 "%s: Finished CAC - bypass sync and start interface",
2843 iface->bss[0]->conf->iface);
2844 return hostapd_setup_interface_complete_sync(iface, err);
2845 }
2846
2847 if (not_ready_in_sync_ifaces > 1) {
2848 /* need to wait as there are other interfaces still coming up */
2849 iface->ready_to_start_in_sync = 1;
2850 wpa_printf(MSG_INFO,
2851 "%s: Interface waiting to sync with other interfaces",
2852 iface->bss[0]->conf->iface);
2853 return 0;
2854 }
2855
2856 wpa_printf(MSG_INFO,
2857 "%s: Last interface to sync - starting all interfaces",
2858 iface->bss[0]->conf->iface);
2859 iface->need_to_start_in_sync = 0;
2860 hostapd_setup_interface_complete_sync(iface, err);
2861 for (i = 0; i < interfaces->count; i++) {
2862 if (interfaces->iface[i]->need_to_start_in_sync &&
2863 interfaces->iface[i]->ready_to_start_in_sync) {
2864 hostapd_setup_interface_complete_sync(
2865 interfaces->iface[i], 0);
2866 /* Only once the interfaces are sync started */
2867 interfaces->iface[i]->need_to_start_in_sync = 0;
2868 }
2869 }
2870
2871 return 0;
2872}
2873
2874
2875/**
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002876 * hostapd_setup_interface - Setup of an interface
2877 * @iface: Pointer to interface data.
2878 * Returns: 0 on success, -1 on failure
2879 *
2880 * Initializes the driver interface, validates the configuration,
2881 * and sets driver parameters based on the configuration.
2882 * Flushes old stations, sets the channel, encryption,
2883 * beacons, and WDS links based on the configuration.
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002884 *
2885 * If interface setup requires more time, e.g., to perform HT co-ex scans, ACS,
2886 * or DFS operations, this function returns 0 before such operations have been
2887 * completed. The pending operations are registered into eloop and will be
2888 * completed from eloop callbacks. Those callbacks end up calling
2889 * hostapd_setup_interface_complete() once setup has been completed.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002890 */
2891int hostapd_setup_interface(struct hostapd_iface *iface)
2892{
2893 int ret;
2894
Hai Shaloma20dcd72022-02-04 13:43:00 -08002895 if (!iface->conf)
2896 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002897 ret = setup_interface(iface);
2898 if (ret) {
2899 wpa_printf(MSG_ERROR, "%s: Unable to setup interface.",
Hai Shaloma20dcd72022-02-04 13:43:00 -08002900 iface->conf->bss[0]->iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002901 return -1;
2902 }
2903
2904 return 0;
2905}
2906
2907
2908/**
2909 * hostapd_alloc_bss_data - Allocate and initialize per-BSS data
2910 * @hapd_iface: Pointer to interface data
2911 * @conf: Pointer to per-interface configuration
2912 * @bss: Pointer to per-BSS configuration for this BSS
2913 * Returns: Pointer to allocated BSS data
2914 *
2915 * This function is used to allocate per-BSS data structure. This data will be
2916 * freed after hostapd_cleanup() is called for it during interface
2917 * deinitialization.
2918 */
2919struct hostapd_data *
2920hostapd_alloc_bss_data(struct hostapd_iface *hapd_iface,
2921 struct hostapd_config *conf,
2922 struct hostapd_bss_config *bss)
2923{
2924 struct hostapd_data *hapd;
2925
2926 hapd = os_zalloc(sizeof(*hapd));
2927 if (hapd == NULL)
2928 return NULL;
2929
2930 hapd->new_assoc_sta_cb = hostapd_new_assoc_sta;
2931 hapd->iconf = conf;
2932 hapd->conf = bss;
2933 hapd->iface = hapd_iface;
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08002934 if (conf)
2935 hapd->driver = conf->driver;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002936 hapd->ctrl_sock = -1;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08002937 dl_list_init(&hapd->ctrl_dst);
Dmitry Shmidt7d175302016-09-06 13:11:34 -07002938 dl_list_init(&hapd->nr_db);
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08002939 hapd->dhcp_sock = -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002940#ifdef CONFIG_IEEE80211R_AP
2941 dl_list_init(&hapd->l2_queue);
2942 dl_list_init(&hapd->l2_oui_queue);
2943#endif /* CONFIG_IEEE80211R_AP */
Hai Shalom021b0b52019-04-10 11:17:58 -07002944#ifdef CONFIG_SAE
2945 dl_list_init(&hapd->sae_commit_queue);
2946#endif /* CONFIG_SAE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002947
2948 return hapd;
2949}
2950
2951
Dmitry Shmidt54605472013-11-08 11:10:19 -08002952static void hostapd_bss_deinit(struct hostapd_data *hapd)
2953{
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07002954 if (!hapd)
2955 return;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002956 wpa_printf(MSG_DEBUG, "%s: deinit bss %s", __func__,
Hai Shalom021b0b52019-04-10 11:17:58 -07002957 hapd->conf ? hapd->conf->iface : "N/A");
Dmitry Shmidt71757432014-06-02 13:50:35 -07002958 hostapd_bss_deinit_no_free(hapd);
Dmitry Shmidtf73259c2015-03-17 11:00:54 -07002959 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
Hai Shalomc3565922019-10-28 11:58:20 -07002960#ifdef CONFIG_SQLITE
2961 if (hapd->rad_attr_db) {
2962 sqlite3_close(hapd->rad_attr_db);
2963 hapd->rad_attr_db = NULL;
2964 }
2965#endif /* CONFIG_SQLITE */
Sunil Ravi99c035e2024-07-12 01:42:03 +00002966
2967 hostapd_bss_link_deinit(hapd);
Dmitry Shmidt54605472013-11-08 11:10:19 -08002968 hostapd_cleanup(hapd);
2969}
2970
2971
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002972void hostapd_interface_deinit(struct hostapd_iface *iface)
2973{
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002974 int j;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002975
Dmitry Shmidt54605472013-11-08 11:10:19 -08002976 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002977 if (iface == NULL)
2978 return;
2979
Dmitry Shmidtf73259c2015-03-17 11:00:54 -07002980 hostapd_set_state(iface, HAPD_IFACE_DISABLED);
2981
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002982 eloop_cancel_timeout(channel_list_update_timeout, iface, NULL);
2983 iface->wait_channel_update = 0;
Sunil Ravi2a14cf12023-11-21 00:54:38 +00002984 iface->is_no_ir = false;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002985
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002986#ifdef CONFIG_FST
2987 if (iface->fst) {
2988 fst_detach(iface->fst);
2989 iface->fst = NULL;
2990 }
2991#endif /* CONFIG_FST */
2992
Hai Shalom021b0b52019-04-10 11:17:58 -07002993 for (j = (int) iface->num_bss - 1; j >= 0; j--) {
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07002994 if (!iface->bss)
2995 break;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002996 hostapd_bss_deinit(iface->bss[j]);
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07002997 }
Roshan Pius3a1667e2018-07-03 15:17:14 -07002998
Roshan Pius3a1667e2018-07-03 15:17:14 -07002999#ifdef NEED_AP_MLME
3000 hostapd_stop_setup_timers(iface);
3001 eloop_cancel_timeout(ap_ht2040_timeout, iface, NULL);
3002#endif /* NEED_AP_MLME */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003003}
3004
3005
Sunil Ravi99c035e2024-07-12 01:42:03 +00003006#ifdef CONFIG_IEEE80211BE
3007
3008static void hostapd_mld_ref_inc(struct hostapd_mld *mld)
3009{
3010 if (!mld)
3011 return;
3012
3013 if (mld->refcount == HOSTAPD_MLD_MAX_REF_COUNT) {
3014 wpa_printf(MSG_ERROR, "AP MLD %s: Ref count overflow",
3015 mld->name);
3016 return;
3017 }
3018
3019 mld->refcount++;
3020}
3021
3022
3023static void hostapd_mld_ref_dec(struct hostapd_mld *mld)
3024{
3025 if (!mld)
3026 return;
3027
3028 if (!mld->refcount) {
3029 wpa_printf(MSG_ERROR, "AP MLD %s: Ref count underflow",
3030 mld->name);
3031 return;
3032 }
3033
3034 mld->refcount--;
3035}
3036
3037#endif /* CONFIG_IEEE80211BE */
3038
3039
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003040void hostapd_interface_free(struct hostapd_iface *iface)
3041{
3042 size_t j;
Dmitry Shmidt54605472013-11-08 11:10:19 -08003043 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
3044 for (j = 0; j < iface->num_bss; j++) {
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07003045 if (!iface->bss)
3046 break;
Sunil Ravi99c035e2024-07-12 01:42:03 +00003047#ifdef CONFIG_IEEE80211BE
3048 if (iface->bss[j])
3049 hostapd_mld_ref_dec(iface->bss[j]->mld);
3050#endif /* CONFIG_IEEE80211BE */
Dmitry Shmidt54605472013-11-08 11:10:19 -08003051 wpa_printf(MSG_DEBUG, "%s: free hapd %p",
3052 __func__, iface->bss[j]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003053 os_free(iface->bss[j]);
Dmitry Shmidt54605472013-11-08 11:10:19 -08003054 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003055 hostapd_cleanup_iface(iface);
3056}
3057
3058
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07003059struct hostapd_iface * hostapd_alloc_iface(void)
3060{
3061 struct hostapd_iface *hapd_iface;
3062
3063 hapd_iface = os_zalloc(sizeof(*hapd_iface));
3064 if (!hapd_iface)
3065 return NULL;
3066
3067 dl_list_init(&hapd_iface->sta_seen);
3068
3069 return hapd_iface;
3070}
3071
3072
Sunil Ravi99c035e2024-07-12 01:42:03 +00003073#ifdef CONFIG_IEEE80211BE
3074static void hostapd_bss_alloc_link_id(struct hostapd_data *hapd)
3075{
3076 hapd->mld_link_id = hapd->mld->next_link_id++;
3077 wpa_printf(MSG_DEBUG, "AP MLD: %s: Link ID %d assigned.",
3078 hapd->mld->name, hapd->mld_link_id);
3079}
3080#endif /* CONFIG_IEEE80211BE */
3081
3082
3083static void hostapd_bss_setup_multi_link(struct hostapd_data *hapd,
3084 struct hapd_interfaces *interfaces)
3085{
3086#ifdef CONFIG_IEEE80211BE
3087 struct hostapd_mld *mld, **all_mld;
3088 struct hostapd_bss_config *conf;
3089 size_t i;
3090
3091 conf = hapd->conf;
3092
3093 if (!hapd->iconf || !hapd->iconf->ieee80211be || !conf->mld_ap ||
3094 conf->disable_11be)
3095 return;
3096
3097 for (i = 0; i < interfaces->mld_count; i++) {
3098 mld = interfaces->mld[i];
3099
3100 if (!mld || os_strcmp(conf->iface, mld->name) != 0)
3101 continue;
3102
3103 hapd->mld = mld;
3104 hostapd_mld_ref_inc(mld);
3105 hostapd_bss_alloc_link_id(hapd);
3106 break;
3107 }
3108
3109 if (hapd->mld)
3110 return;
3111
3112 mld = os_zalloc(sizeof(struct hostapd_mld));
3113 if (!mld)
3114 goto fail;
3115
3116 os_strlcpy(mld->name, conf->iface, sizeof(conf->iface));
3117 dl_list_init(&mld->links);
Sunil Ravic0f5d412024-09-11 22:12:49 +00003118 mld->ctrl_sock = -1;
3119 if (hapd->conf->ctrl_interface)
3120 mld->ctrl_interface = os_strdup(hapd->conf->ctrl_interface);
Sunil Ravi99c035e2024-07-12 01:42:03 +00003121
3122 wpa_printf(MSG_DEBUG, "AP MLD %s created", mld->name);
3123
Sunil Ravic0f5d412024-09-11 22:12:49 +00003124 /* Initialize MLD control interfaces early to allow external monitoring
3125 * of link setup operations. */
3126 if (interfaces->mld_ctrl_iface_init(mld))
3127 goto fail;
3128
Sunil Ravi99c035e2024-07-12 01:42:03 +00003129 hapd->mld = mld;
3130 hostapd_mld_ref_inc(mld);
3131 hostapd_bss_alloc_link_id(hapd);
3132
3133 all_mld = os_realloc_array(interfaces->mld, interfaces->mld_count + 1,
3134 sizeof(struct hostapd_mld *));
3135 if (!all_mld)
3136 goto fail;
3137
3138 interfaces->mld = all_mld;
3139 interfaces->mld[interfaces->mld_count] = mld;
3140 interfaces->mld_count++;
3141
3142 return;
3143fail:
3144 if (!mld)
3145 return;
3146
3147 wpa_printf(MSG_DEBUG, "AP MLD %s: free mld %p", mld->name, mld);
3148 os_free(mld);
3149 hapd->mld = NULL;
3150#endif /* CONFIG_IEEE80211BE */
3151}
3152
3153
3154static void hostapd_cleanup_unused_mlds(struct hapd_interfaces *interfaces)
3155{
3156#ifdef CONFIG_IEEE80211BE
3157 struct hostapd_mld *mld, **all_mld;
3158 size_t i, j, num_mlds;
3159 bool forced_remove, remove;
3160
3161 if (!interfaces->mld)
3162 return;
3163
3164 num_mlds = interfaces->mld_count;
3165
3166 for (i = 0; i < interfaces->mld_count; i++) {
3167 mld = interfaces->mld[i];
3168 if (!mld)
3169 continue;
3170
3171 remove = false;
3172 forced_remove = false;
3173
3174 if (!mld->refcount)
3175 remove = true;
3176
3177 /* If MLD is still being referenced but the number of interfaces
3178 * is zero, it is safe to force its deletion. Normally, this
3179 * should not happen but even if it does, let us free the
3180 * memory.
3181 */
3182 if (!remove && !interfaces->count)
3183 forced_remove = true;
3184
3185 if (!remove && !forced_remove)
3186 continue;
3187
Sunil Ravic0f5d412024-09-11 22:12:49 +00003188 interfaces->mld_ctrl_iface_deinit(mld);
3189
Sunil Ravi99c035e2024-07-12 01:42:03 +00003190 wpa_printf(MSG_DEBUG, "AP MLD %s: Freed%s", mld->name,
3191 forced_remove ? " (forced)" : "");
3192 os_free(mld);
3193 interfaces->mld[i] = NULL;
3194 num_mlds--;
3195 }
3196
3197 if (!num_mlds) {
3198 interfaces->mld_count = 0;
3199 os_free(interfaces->mld);
3200 interfaces->mld = NULL;
3201 return;
3202 }
3203
3204 all_mld = os_zalloc(num_mlds * sizeof(struct hostapd_mld *));
3205 if (!all_mld) {
3206 wpa_printf(MSG_ERROR,
3207 "AP MLD: Failed to re-allocate the MLDs. Expect issues");
3208 return;
3209 }
3210
3211 for (i = 0, j = 0; i < interfaces->mld_count; i++) {
3212 mld = interfaces->mld[i];
3213 if (!mld)
3214 continue;
3215
3216 all_mld[j++] = mld;
3217 }
3218
3219 /* This should not happen */
3220 if (j != num_mlds) {
3221 wpa_printf(MSG_DEBUG,
3222 "AP MLD: Some error occurred while reallocating MLDs. Expect issues.");
3223 os_free(all_mld);
3224 return;
3225 }
3226
3227 os_free(interfaces->mld);
3228 interfaces->mld = all_mld;
3229 interfaces->mld_count = num_mlds;
3230#endif /* CONFIG_IEEE80211BE */
3231}
3232
3233
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003234/**
3235 * hostapd_init - Allocate and initialize per-interface data
3236 * @config_file: Path to the configuration file
3237 * Returns: Pointer to the allocated interface data or %NULL on failure
3238 *
3239 * This function is used to allocate main data structures for per-interface
3240 * data. The allocated data buffer will be freed by calling
3241 * hostapd_cleanup_iface().
3242 */
3243struct hostapd_iface * hostapd_init(struct hapd_interfaces *interfaces,
3244 const char *config_file)
3245{
3246 struct hostapd_iface *hapd_iface = NULL;
3247 struct hostapd_config *conf = NULL;
3248 struct hostapd_data *hapd;
3249 size_t i;
3250
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07003251 hapd_iface = hostapd_alloc_iface();
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003252 if (hapd_iface == NULL)
3253 goto fail;
3254
3255 hapd_iface->config_fname = os_strdup(config_file);
3256 if (hapd_iface->config_fname == NULL)
3257 goto fail;
3258
3259 conf = interfaces->config_read_cb(hapd_iface->config_fname);
3260 if (conf == NULL)
3261 goto fail;
3262 hapd_iface->conf = conf;
3263
3264 hapd_iface->num_bss = conf->num_bss;
3265 hapd_iface->bss = os_calloc(conf->num_bss,
3266 sizeof(struct hostapd_data *));
3267 if (hapd_iface->bss == NULL)
3268 goto fail;
3269
3270 for (i = 0; i < conf->num_bss; i++) {
3271 hapd = hapd_iface->bss[i] =
3272 hostapd_alloc_bss_data(hapd_iface, conf,
3273 conf->bss[i]);
3274 if (hapd == NULL)
3275 goto fail;
3276 hapd->msg_ctx = hapd;
Sunil Ravi99c035e2024-07-12 01:42:03 +00003277 hostapd_bss_setup_multi_link(hapd, interfaces);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003278 }
3279
Sunil Ravi99c035e2024-07-12 01:42:03 +00003280 hapd_iface->is_ch_switch_dfs = false;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003281 return hapd_iface;
3282
3283fail:
3284 wpa_printf(MSG_ERROR, "Failed to set up interface with %s",
3285 config_file);
3286 if (conf)
3287 hostapd_config_free(conf);
3288 if (hapd_iface) {
3289 os_free(hapd_iface->config_fname);
3290 os_free(hapd_iface->bss);
Dmitry Shmidt54605472013-11-08 11:10:19 -08003291 wpa_printf(MSG_DEBUG, "%s: free iface %p",
3292 __func__, hapd_iface);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003293 os_free(hapd_iface);
3294 }
3295 return NULL;
3296}
3297
3298
3299static int ifname_in_use(struct hapd_interfaces *interfaces, const char *ifname)
3300{
3301 size_t i, j;
3302
3303 for (i = 0; i < interfaces->count; i++) {
3304 struct hostapd_iface *iface = interfaces->iface[i];
3305 for (j = 0; j < iface->num_bss; j++) {
3306 struct hostapd_data *hapd = iface->bss[j];
3307 if (os_strcmp(ifname, hapd->conf->iface) == 0)
3308 return 1;
3309 }
3310 }
3311
3312 return 0;
3313}
3314
3315
3316/**
3317 * hostapd_interface_init_bss - Read configuration file and init BSS data
3318 *
3319 * This function is used to parse configuration file for a BSS. This BSS is
3320 * added to an existing interface sharing the same radio (if any) or a new
3321 * interface is created if this is the first interface on a radio. This
3322 * allocate memory for the BSS. No actual driver operations are started.
3323 *
3324 * This is similar to hostapd_interface_init(), but for a case where the
3325 * configuration is used to add a single BSS instead of all BSSes for a radio.
3326 */
3327struct hostapd_iface *
3328hostapd_interface_init_bss(struct hapd_interfaces *interfaces, const char *phy,
3329 const char *config_fname, int debug)
3330{
3331 struct hostapd_iface *new_iface = NULL, *iface = NULL;
3332 struct hostapd_data *hapd;
3333 int k;
3334 size_t i, bss_idx;
3335
3336 if (!phy || !*phy)
3337 return NULL;
3338
3339 for (i = 0; i < interfaces->count; i++) {
3340 if (os_strcmp(interfaces->iface[i]->phy, phy) == 0) {
3341 iface = interfaces->iface[i];
3342 break;
3343 }
3344 }
3345
3346 wpa_printf(MSG_INFO, "Configuration file: %s (phy %s)%s",
3347 config_fname, phy, iface ? "" : " --> new PHY");
3348 if (iface) {
3349 struct hostapd_config *conf;
3350 struct hostapd_bss_config **tmp_conf;
3351 struct hostapd_data **tmp_bss;
3352 struct hostapd_bss_config *bss;
3353 const char *ifname;
3354
3355 /* Add new BSS to existing iface */
3356 conf = interfaces->config_read_cb(config_fname);
3357 if (conf == NULL)
3358 return NULL;
3359 if (conf->num_bss > 1) {
3360 wpa_printf(MSG_ERROR, "Multiple BSSes specified in BSS-config");
3361 hostapd_config_free(conf);
3362 return NULL;
3363 }
3364
3365 ifname = conf->bss[0]->iface;
3366 if (ifname[0] != '\0' && ifname_in_use(interfaces, ifname)) {
3367 wpa_printf(MSG_ERROR,
3368 "Interface name %s already in use", ifname);
3369 hostapd_config_free(conf);
3370 return NULL;
3371 }
3372
3373 tmp_conf = os_realloc_array(
3374 iface->conf->bss, iface->conf->num_bss + 1,
3375 sizeof(struct hostapd_bss_config *));
3376 tmp_bss = os_realloc_array(iface->bss, iface->num_bss + 1,
3377 sizeof(struct hostapd_data *));
3378 if (tmp_bss)
3379 iface->bss = tmp_bss;
3380 if (tmp_conf) {
3381 iface->conf->bss = tmp_conf;
3382 iface->conf->last_bss = tmp_conf[0];
3383 }
3384 if (tmp_bss == NULL || tmp_conf == NULL) {
3385 hostapd_config_free(conf);
3386 return NULL;
3387 }
3388 bss = iface->conf->bss[iface->conf->num_bss] = conf->bss[0];
3389 iface->conf->num_bss++;
3390
3391 hapd = hostapd_alloc_bss_data(iface, iface->conf, bss);
3392 if (hapd == NULL) {
3393 iface->conf->num_bss--;
3394 hostapd_config_free(conf);
3395 return NULL;
3396 }
3397 iface->conf->last_bss = bss;
3398 iface->bss[iface->num_bss] = hapd;
3399 hapd->msg_ctx = hapd;
Sunil Ravi99c035e2024-07-12 01:42:03 +00003400 hostapd_bss_setup_multi_link(hapd, interfaces);
3401
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003402
3403 bss_idx = iface->num_bss++;
3404 conf->num_bss--;
3405 conf->bss[0] = NULL;
3406 hostapd_config_free(conf);
3407 } else {
3408 /* Add a new iface with the first BSS */
3409 new_iface = iface = hostapd_init(interfaces, config_fname);
3410 if (!iface)
3411 return NULL;
3412 os_strlcpy(iface->phy, phy, sizeof(iface->phy));
3413 iface->interfaces = interfaces;
3414 bss_idx = 0;
3415 }
3416
3417 for (k = 0; k < debug; k++) {
3418 if (iface->bss[bss_idx]->conf->logger_stdout_level > 0)
3419 iface->bss[bss_idx]->conf->logger_stdout_level--;
3420 }
3421
3422 if (iface->conf->bss[bss_idx]->iface[0] == '\0' &&
3423 !hostapd_drv_none(iface->bss[bss_idx])) {
3424 wpa_printf(MSG_ERROR, "Interface name not specified in %s",
3425 config_fname);
3426 if (new_iface)
3427 hostapd_interface_deinit_free(new_iface);
3428 return NULL;
3429 }
3430
3431 return iface;
3432}
3433
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003434
Sunil Ravi99c035e2024-07-12 01:42:03 +00003435static void hostapd_cleanup_driver(const struct wpa_driver_ops *driver,
3436 void *drv_priv, struct hostapd_iface *iface)
3437{
Sunil Ravi99c035e2024-07-12 01:42:03 +00003438 if (!driver || !driver->hapd_deinit || !drv_priv)
3439 return;
3440
Sunil Ravi7f769292024-07-23 22:21:32 +00003441#ifdef CONFIG_IEEE80211BE
Sunil Ravi99c035e2024-07-12 01:42:03 +00003442 /* In case of non-ML operation, de-init. But if ML operation exist,
3443 * even if that's the last BSS in the interface, the driver (drv) could
3444 * be in use for a different AP MLD. Hence, need to check if drv is
3445 * still being used by some other BSS before de-initiallizing. */
3446 if (!iface->bss[0]->conf->mld_ap) {
3447 driver->hapd_deinit(drv_priv);
3448 } else if (hostapd_mld_is_first_bss(iface->bss[0]) &&
3449 driver->is_drv_shared &&
Sunil Ravic0f5d412024-09-11 22:12:49 +00003450 !driver->is_drv_shared(drv_priv,
3451 iface->bss[0]->mld_link_id)) {
Sunil Ravi99c035e2024-07-12 01:42:03 +00003452 driver->hapd_deinit(drv_priv);
Sunil Ravic0f5d412024-09-11 22:12:49 +00003453 hostapd_mld_interface_freed(iface->bss[0]);
Sunil Ravi99c035e2024-07-12 01:42:03 +00003454 } else if (hostapd_if_link_remove(iface->bss[0],
3455 WPA_IF_AP_BSS,
3456 iface->bss[0]->conf->iface,
3457 iface->bss[0]->mld_link_id)) {
3458 wpa_printf(MSG_WARNING, "Failed to remove BSS interface %s",
3459 iface->bss[0]->conf->iface);
3460 }
3461#else /* CONFIG_IEEE80211BE */
3462 driver->hapd_deinit(drv_priv);
3463#endif /* CONFIG_IEEE80211BE */
3464 iface->bss[0]->drv_priv = NULL;
3465}
3466
3467
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003468void hostapd_interface_deinit_free(struct hostapd_iface *iface)
3469{
3470 const struct wpa_driver_ops *driver;
3471 void *drv_priv;
Dmitry Shmidt54605472013-11-08 11:10:19 -08003472
3473 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003474 if (iface == NULL)
3475 return;
Dmitry Shmidt54605472013-11-08 11:10:19 -08003476 wpa_printf(MSG_DEBUG, "%s: num_bss=%u conf->num_bss=%u",
3477 __func__, (unsigned int) iface->num_bss,
3478 (unsigned int) iface->conf->num_bss);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003479 driver = iface->bss[0]->driver;
3480 drv_priv = iface->bss[0]->drv_priv;
3481 hostapd_interface_deinit(iface);
Dmitry Shmidt54605472013-11-08 11:10:19 -08003482 wpa_printf(MSG_DEBUG, "%s: driver=%p drv_priv=%p -> hapd_deinit",
3483 __func__, driver, drv_priv);
Sunil Ravi99c035e2024-07-12 01:42:03 +00003484 hostapd_cleanup_driver(driver, drv_priv, iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003485 hostapd_interface_free(iface);
3486}
3487
3488
Dmitry Shmidt15907092014-03-25 10:42:57 -07003489static void hostapd_deinit_driver(const struct wpa_driver_ops *driver,
3490 void *drv_priv,
3491 struct hostapd_iface *hapd_iface)
3492{
3493 size_t j;
3494
3495 wpa_printf(MSG_DEBUG, "%s: driver=%p drv_priv=%p -> hapd_deinit",
3496 __func__, driver, drv_priv);
Sunil Ravi99c035e2024-07-12 01:42:03 +00003497
3498 hostapd_cleanup_driver(driver, drv_priv, hapd_iface);
3499
Dmitry Shmidt15907092014-03-25 10:42:57 -07003500 if (driver && driver->hapd_deinit && drv_priv) {
Dmitry Shmidt15907092014-03-25 10:42:57 -07003501 for (j = 0; j < hapd_iface->num_bss; j++) {
3502 wpa_printf(MSG_DEBUG, "%s:bss[%d]->drv_priv=%p",
3503 __func__, (int) j,
3504 hapd_iface->bss[j]->drv_priv);
Hai Shalom1dc4d202019-04-29 16:22:27 -07003505 if (hapd_iface->bss[j]->drv_priv == drv_priv) {
Sunil Ravi99c035e2024-07-12 01:42:03 +00003506 hapd_iface->bss[j]->drv_priv = NULL;
Hai Shalom1dc4d202019-04-29 16:22:27 -07003507 hapd_iface->extended_capa = NULL;
3508 hapd_iface->extended_capa_mask = NULL;
3509 hapd_iface->extended_capa_len = 0;
3510 }
Dmitry Shmidt15907092014-03-25 10:42:57 -07003511 }
3512 }
3513}
3514
3515
Sunil Ravi99c035e2024-07-12 01:42:03 +00003516static void hostapd_refresh_all_iface_beacons(struct hostapd_iface *hapd_iface)
3517{
3518 size_t j;
3519
3520 if (!hapd_iface->interfaces || hapd_iface->interfaces->count <= 1)
3521 return;
3522
3523 for (j = 0; j < hapd_iface->interfaces->count; j++) {
3524 if (hapd_iface->interfaces->iface[j] == hapd_iface)
3525 continue;
3526
3527 ieee802_11_update_beacons(hapd_iface->interfaces->iface[j]);
3528 }
3529}
3530
3531
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003532int hostapd_enable_iface(struct hostapd_iface *hapd_iface)
3533{
Dmitry Shmidt71757432014-06-02 13:50:35 -07003534 size_t j;
3535
Hai Shalom60840252021-02-19 19:02:11 -08003536 if (!hapd_iface)
3537 return -1;
3538
3539 if (hapd_iface->enable_iface_cb)
3540 return hapd_iface->enable_iface_cb(hapd_iface);
3541
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003542 if (hapd_iface->bss[0]->drv_priv != NULL) {
3543 wpa_printf(MSG_ERROR, "Interface %s already enabled",
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003544 hapd_iface->conf->bss[0]->iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003545 return -1;
3546 }
3547
3548 wpa_printf(MSG_DEBUG, "Enable interface %s",
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003549 hapd_iface->conf->bss[0]->iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003550
Dmitry Shmidt71757432014-06-02 13:50:35 -07003551 for (j = 0; j < hapd_iface->num_bss; j++)
3552 hostapd_set_security_params(hapd_iface->conf->bss[j], 1);
Dmitry Shmidt344abd32014-01-14 13:17:00 -08003553 if (hostapd_config_check(hapd_iface->conf, 1) < 0) {
3554 wpa_printf(MSG_INFO, "Invalid configuration - cannot enable");
3555 return -1;
3556 }
3557
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003558 if (hapd_iface->interfaces == NULL ||
3559 hapd_iface->interfaces->driver_init == NULL ||
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003560 hapd_iface->interfaces->driver_init(hapd_iface))
3561 return -1;
3562
3563 if (hostapd_setup_interface(hapd_iface)) {
Dmitry Shmidt15907092014-03-25 10:42:57 -07003564 hostapd_deinit_driver(hapd_iface->bss[0]->driver,
3565 hapd_iface->bss[0]->drv_priv,
3566 hapd_iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003567 return -1;
3568 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003569
Sunil Ravi99c035e2024-07-12 01:42:03 +00003570 hostapd_refresh_all_iface_beacons(hapd_iface);
3571
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003572 return 0;
3573}
3574
3575
3576int hostapd_reload_iface(struct hostapd_iface *hapd_iface)
3577{
3578 size_t j;
3579
3580 wpa_printf(MSG_DEBUG, "Reload interface %s",
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003581 hapd_iface->conf->bss[0]->iface);
3582 for (j = 0; j < hapd_iface->num_bss; j++)
Dmitry Shmidt71757432014-06-02 13:50:35 -07003583 hostapd_set_security_params(hapd_iface->conf->bss[j], 1);
Dmitry Shmidt344abd32014-01-14 13:17:00 -08003584 if (hostapd_config_check(hapd_iface->conf, 1) < 0) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003585 wpa_printf(MSG_ERROR, "Updated configuration is invalid");
3586 return -1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003587 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003588 hostapd_clear_old(hapd_iface);
3589 for (j = 0; j < hapd_iface->num_bss; j++)
3590 hostapd_reload_bss(hapd_iface->bss[j]);
3591
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003592 return 0;
3593}
3594
3595
Sunil Ravi77d572f2023-01-17 23:58:31 +00003596int hostapd_reload_bss_only(struct hostapd_data *bss)
3597{
3598
3599 wpa_printf(MSG_DEBUG, "Reload BSS %s", bss->conf->iface);
3600 hostapd_set_security_params(bss->conf, 1);
3601 if (hostapd_config_check(bss->iconf, 1) < 0) {
3602 wpa_printf(MSG_ERROR, "Updated BSS configuration is invalid");
3603 return -1;
3604 }
3605 hostapd_clear_old_bss(bss);
3606 hostapd_reload_bss(bss);
3607 return 0;
3608}
3609
3610
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003611int hostapd_disable_iface(struct hostapd_iface *hapd_iface)
3612{
3613 size_t j;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003614 const struct wpa_driver_ops *driver;
3615 void *drv_priv;
3616
3617 if (hapd_iface == NULL)
3618 return -1;
Dmitry Shmidt71757432014-06-02 13:50:35 -07003619
Hai Shalom60840252021-02-19 19:02:11 -08003620 if (hapd_iface->disable_iface_cb)
3621 return hapd_iface->disable_iface_cb(hapd_iface);
3622
Dmitry Shmidt71757432014-06-02 13:50:35 -07003623 if (hapd_iface->bss[0]->drv_priv == NULL) {
3624 wpa_printf(MSG_INFO, "Interface %s already disabled",
3625 hapd_iface->conf->bss[0]->iface);
3626 return -1;
3627 }
3628
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003629 wpa_msg(hapd_iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003630 driver = hapd_iface->bss[0]->driver;
3631 drv_priv = hapd_iface->bss[0]->drv_priv;
3632
Dmitry Shmidta38abf92014-03-06 13:38:44 -08003633 hapd_iface->driver_ap_teardown =
3634 !!(hapd_iface->drv_flags &
3635 WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
3636
Hai Shalom39ba6fc2019-01-22 12:40:38 -08003637#ifdef NEED_AP_MLME
3638 for (j = 0; j < hapd_iface->num_bss; j++)
3639 hostapd_cleanup_cs_params(hapd_iface->bss[j]);
3640#endif /* NEED_AP_MLME */
3641
Dmitry Shmidta38abf92014-03-06 13:38:44 -08003642 /* same as hostapd_interface_deinit without deinitializing ctrl-iface */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003643 for (j = 0; j < hapd_iface->num_bss; j++) {
3644 struct hostapd_data *hapd = hapd_iface->bss[j];
Dmitry Shmidt71757432014-06-02 13:50:35 -07003645 hostapd_bss_deinit_no_free(hapd);
Sunil Ravi99c035e2024-07-12 01:42:03 +00003646 hostapd_bss_link_deinit(hapd);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003647 hostapd_free_hapd_data(hapd);
3648 }
3649
Dmitry Shmidt15907092014-03-25 10:42:57 -07003650 hostapd_deinit_driver(driver, drv_priv, hapd_iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003651
3652 /* From hostapd_cleanup_iface: These were initialized in
3653 * hostapd_setup_interface and hostapd_setup_interface_complete
3654 */
3655 hostapd_cleanup_iface_partial(hapd_iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003656
Dmitry Shmidt56052862013-10-04 10:23:25 -07003657 wpa_printf(MSG_DEBUG, "Interface %s disabled",
3658 hapd_iface->bss[0]->conf->iface);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003659 hostapd_set_state(hapd_iface, HAPD_IFACE_DISABLED);
Sunil Ravi99c035e2024-07-12 01:42:03 +00003660 hostapd_refresh_all_iface_beacons(hapd_iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003661 return 0;
3662}
3663
3664
3665static struct hostapd_iface *
3666hostapd_iface_alloc(struct hapd_interfaces *interfaces)
3667{
3668 struct hostapd_iface **iface, *hapd_iface;
3669
3670 iface = os_realloc_array(interfaces->iface, interfaces->count + 1,
3671 sizeof(struct hostapd_iface *));
3672 if (iface == NULL)
3673 return NULL;
3674 interfaces->iface = iface;
3675 hapd_iface = interfaces->iface[interfaces->count] =
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07003676 hostapd_alloc_iface();
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003677 if (hapd_iface == NULL) {
3678 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory for "
3679 "the interface", __func__);
3680 return NULL;
3681 }
3682 interfaces->count++;
3683 hapd_iface->interfaces = interfaces;
3684
3685 return hapd_iface;
3686}
3687
3688
3689static struct hostapd_config *
3690hostapd_config_alloc(struct hapd_interfaces *interfaces, const char *ifname,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003691 const char *ctrl_iface, const char *driver)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003692{
3693 struct hostapd_bss_config *bss;
3694 struct hostapd_config *conf;
3695
3696 /* Allocates memory for bss and conf */
3697 conf = hostapd_config_defaults();
3698 if (conf == NULL) {
3699 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory for "
3700 "configuration", __func__);
Hai Shalom74f70d42019-02-11 14:42:39 -08003701 return NULL;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003702 }
3703
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003704 if (driver) {
3705 int j;
3706
3707 for (j = 0; wpa_drivers[j]; j++) {
3708 if (os_strcmp(driver, wpa_drivers[j]->name) == 0) {
3709 conf->driver = wpa_drivers[j];
3710 goto skip;
3711 }
3712 }
3713
3714 wpa_printf(MSG_ERROR,
3715 "Invalid/unknown driver '%s' - registering the default driver",
3716 driver);
3717 }
3718
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003719 conf->driver = wpa_drivers[0];
3720 if (conf->driver == NULL) {
3721 wpa_printf(MSG_ERROR, "No driver wrappers registered!");
3722 hostapd_config_free(conf);
3723 return NULL;
3724 }
3725
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003726skip:
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003727 bss = conf->last_bss = conf->bss[0];
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003728
3729 os_strlcpy(bss->iface, ifname, sizeof(bss->iface));
3730 bss->ctrl_interface = os_strdup(ctrl_iface);
3731 if (bss->ctrl_interface == NULL) {
3732 hostapd_config_free(conf);
3733 return NULL;
3734 }
3735
3736 /* Reading configuration file skipped, will be done in SET!
3737 * From reading the configuration till the end has to be done in
3738 * SET
3739 */
3740 return conf;
3741}
3742
3743
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003744static int hostapd_data_alloc(struct hostapd_iface *hapd_iface,
3745 struct hostapd_config *conf)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003746{
3747 size_t i;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003748 struct hostapd_data *hapd;
3749
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003750 hapd_iface->bss = os_calloc(conf->num_bss,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003751 sizeof(struct hostapd_data *));
3752 if (hapd_iface->bss == NULL)
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003753 return -1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003754
3755 for (i = 0; i < conf->num_bss; i++) {
3756 hapd = hapd_iface->bss[i] =
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003757 hostapd_alloc_bss_data(hapd_iface, conf, conf->bss[i]);
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003758 if (hapd == NULL) {
3759 while (i > 0) {
3760 i--;
3761 os_free(hapd_iface->bss[i]);
3762 hapd_iface->bss[i] = NULL;
3763 }
3764 os_free(hapd_iface->bss);
3765 hapd_iface->bss = NULL;
3766 return -1;
3767 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003768 hapd->msg_ctx = hapd;
Sunil Ravi99c035e2024-07-12 01:42:03 +00003769 hostapd_bss_setup_multi_link(hapd, hapd_iface->interfaces);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003770 }
3771
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003772 hapd_iface->conf = conf;
3773 hapd_iface->num_bss = conf->num_bss;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003774
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003775 return 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003776}
3777
3778
3779int hostapd_add_iface(struct hapd_interfaces *interfaces, char *buf)
3780{
3781 struct hostapd_config *conf = NULL;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003782 struct hostapd_iface *hapd_iface = NULL, *new_iface = NULL;
3783 struct hostapd_data *hapd;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003784 char *ptr;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003785 size_t i, j;
3786 const char *conf_file = NULL, *phy_name = NULL;
3787
3788 if (os_strncmp(buf, "bss_config=", 11) == 0) {
3789 char *pos;
3790 phy_name = buf + 11;
3791 pos = os_strchr(phy_name, ':');
3792 if (!pos)
3793 return -1;
3794 *pos++ = '\0';
3795 conf_file = pos;
3796 if (!os_strlen(conf_file))
3797 return -1;
3798
3799 hapd_iface = hostapd_interface_init_bss(interfaces, phy_name,
3800 conf_file, 0);
3801 if (!hapd_iface)
3802 return -1;
3803 for (j = 0; j < interfaces->count; j++) {
3804 if (interfaces->iface[j] == hapd_iface)
3805 break;
3806 }
3807 if (j == interfaces->count) {
3808 struct hostapd_iface **tmp;
3809 tmp = os_realloc_array(interfaces->iface,
3810 interfaces->count + 1,
3811 sizeof(struct hostapd_iface *));
3812 if (!tmp) {
3813 hostapd_interface_deinit_free(hapd_iface);
3814 return -1;
3815 }
3816 interfaces->iface = tmp;
3817 interfaces->iface[interfaces->count++] = hapd_iface;
3818 new_iface = hapd_iface;
3819 }
3820
3821 if (new_iface) {
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003822 if (interfaces->driver_init(hapd_iface))
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003823 goto fail;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003824
3825 if (hostapd_setup_interface(hapd_iface)) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003826 hostapd_deinit_driver(
3827 hapd_iface->bss[0]->driver,
3828 hapd_iface->bss[0]->drv_priv,
3829 hapd_iface);
3830 goto fail;
3831 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003832 } else {
3833 /* Assign new BSS with bss[0]'s driver info */
3834 hapd = hapd_iface->bss[hapd_iface->num_bss - 1];
3835 hapd->driver = hapd_iface->bss[0]->driver;
3836 hapd->drv_priv = hapd_iface->bss[0]->drv_priv;
3837 os_memcpy(hapd->own_addr, hapd_iface->bss[0]->own_addr,
3838 ETH_ALEN);
3839
3840 if (start_ctrl_iface_bss(hapd) < 0 ||
Dmitry Shmidt54605472013-11-08 11:10:19 -08003841 (hapd_iface->state == HAPD_IFACE_ENABLED &&
Sunil Ravi77d572f2023-01-17 23:58:31 +00003842 hostapd_setup_bss(hapd, -1, true))) {
Sunil Ravi99c035e2024-07-12 01:42:03 +00003843 hostapd_bss_link_deinit(hapd);
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07003844 hostapd_cleanup(hapd);
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08003845 hapd_iface->bss[hapd_iface->num_bss - 1] = NULL;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003846 hapd_iface->conf->num_bss--;
3847 hapd_iface->num_bss--;
Dmitry Shmidt54605472013-11-08 11:10:19 -08003848 wpa_printf(MSG_DEBUG, "%s: free hapd %p %s",
3849 __func__, hapd, hapd->conf->iface);
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003850 hostapd_config_free_bss(hapd->conf);
3851 hapd->conf = NULL;
Sunil Ravi99c035e2024-07-12 01:42:03 +00003852#ifdef CONFIG_IEEE80211BE
3853 hostapd_mld_ref_dec(hapd->mld);
3854#endif /* CONFIG_IEEE80211BE */
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003855 os_free(hapd);
3856 return -1;
3857 }
3858 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003859 hostapd_owe_update_trans(hapd_iface);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003860 return 0;
3861 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003862
3863 ptr = os_strchr(buf, ' ');
3864 if (ptr == NULL)
3865 return -1;
3866 *ptr++ = '\0';
3867
Dmitry Shmidt56052862013-10-04 10:23:25 -07003868 if (os_strncmp(ptr, "config=", 7) == 0)
3869 conf_file = ptr + 7;
3870
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003871 for (i = 0; i < interfaces->count; i++) {
Sunil Ravi2a14cf12023-11-21 00:54:38 +00003872 bool mld_ap = false;
3873
3874#ifdef CONFIG_IEEE80211BE
3875 mld_ap = interfaces->iface[i]->conf->bss[0]->mld_ap;
3876#endif /* CONFIG_IEEE80211BE */
3877
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003878 if (!os_strcmp(interfaces->iface[i]->conf->bss[0]->iface,
Sunil Ravi2a14cf12023-11-21 00:54:38 +00003879 buf) && !mld_ap) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003880 wpa_printf(MSG_INFO, "Cannot add interface - it "
3881 "already exists");
3882 return -1;
3883 }
3884 }
3885
3886 hapd_iface = hostapd_iface_alloc(interfaces);
3887 if (hapd_iface == NULL) {
3888 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
3889 "for interface", __func__);
3890 goto fail;
3891 }
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003892 new_iface = hapd_iface;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003893
Dmitry Shmidt56052862013-10-04 10:23:25 -07003894 if (conf_file && interfaces->config_read_cb) {
3895 conf = interfaces->config_read_cb(conf_file);
3896 if (conf && conf->bss)
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003897 os_strlcpy(conf->bss[0]->iface, buf,
3898 sizeof(conf->bss[0]->iface));
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003899 } else {
3900 char *driver = os_strchr(ptr, ' ');
3901
3902 if (driver)
3903 *driver++ = '\0';
3904 conf = hostapd_config_alloc(interfaces, buf, ptr, driver);
3905 }
3906
Dmitry Shmidt56052862013-10-04 10:23:25 -07003907 if (conf == NULL || conf->bss == NULL) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003908 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
3909 "for configuration", __func__);
3910 goto fail;
3911 }
3912
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003913 if (hostapd_data_alloc(hapd_iface, conf) < 0) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003914 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
3915 "for hostapd", __func__);
3916 goto fail;
3917 }
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003918 conf = NULL;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003919
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003920 if (start_ctrl_iface(hapd_iface) < 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003921 goto fail;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003922
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003923 wpa_printf(MSG_INFO, "Add interface '%s'",
3924 hapd_iface->conf->bss[0]->iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003925
3926 return 0;
3927
3928fail:
3929 if (conf)
3930 hostapd_config_free(conf);
3931 if (hapd_iface) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003932 if (hapd_iface->bss) {
Dmitry Shmidt54605472013-11-08 11:10:19 -08003933 for (i = 0; i < hapd_iface->num_bss; i++) {
3934 hapd = hapd_iface->bss[i];
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08003935 if (!hapd)
3936 continue;
3937 if (hapd_iface->interfaces &&
Dmitry Shmidt54605472013-11-08 11:10:19 -08003938 hapd_iface->interfaces->ctrl_iface_deinit)
3939 hapd_iface->interfaces->
3940 ctrl_iface_deinit(hapd);
3941 wpa_printf(MSG_DEBUG, "%s: free hapd %p (%s)",
3942 __func__, hapd_iface->bss[i],
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08003943 hapd->conf->iface);
Sunil Ravi99c035e2024-07-12 01:42:03 +00003944 hostapd_bss_link_deinit(hapd);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003945 hostapd_cleanup(hapd);
Sunil Ravi99c035e2024-07-12 01:42:03 +00003946#ifdef CONFIG_IEEE80211BE
3947 hostapd_mld_ref_dec(hapd->mld);
3948#endif /* CONFIG_IEEE80211BE */
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08003949 os_free(hapd);
3950 hapd_iface->bss[i] = NULL;
Dmitry Shmidt54605472013-11-08 11:10:19 -08003951 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003952 os_free(hapd_iface->bss);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003953 hapd_iface->bss = NULL;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003954 }
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003955 if (new_iface) {
3956 interfaces->count--;
3957 interfaces->iface[interfaces->count] = NULL;
Sunil Ravi99c035e2024-07-12 01:42:03 +00003958 hostapd_cleanup_unused_mlds(interfaces);
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003959 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003960 hostapd_cleanup_iface(hapd_iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003961 }
3962 return -1;
3963}
3964
3965
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003966static int hostapd_remove_bss(struct hostapd_iface *iface, unsigned int idx)
3967{
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003968 size_t i;
3969
Dmitry Shmidt54605472013-11-08 11:10:19 -08003970 wpa_printf(MSG_INFO, "Remove BSS '%s'", iface->conf->bss[idx]->iface);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003971
Dmitry Shmidt54605472013-11-08 11:10:19 -08003972 /* Remove hostapd_data only if it has already been initialized */
3973 if (idx < iface->num_bss) {
3974 struct hostapd_data *hapd = iface->bss[idx];
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003975
Dmitry Shmidt54605472013-11-08 11:10:19 -08003976 hostapd_bss_deinit(hapd);
3977 wpa_printf(MSG_DEBUG, "%s: free hapd %p (%s)",
3978 __func__, hapd, hapd->conf->iface);
3979 hostapd_config_free_bss(hapd->conf);
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003980 hapd->conf = NULL;
Sunil Ravi99c035e2024-07-12 01:42:03 +00003981#ifdef CONFIG_IEEE80211BE
3982 hostapd_mld_ref_dec(hapd->mld);
3983#endif /* CONFIG_IEEE80211BE */
Dmitry Shmidt54605472013-11-08 11:10:19 -08003984 os_free(hapd);
3985
3986 iface->num_bss--;
3987
3988 for (i = idx; i < iface->num_bss; i++)
3989 iface->bss[i] = iface->bss[i + 1];
3990 } else {
3991 hostapd_config_free_bss(iface->conf->bss[idx]);
3992 iface->conf->bss[idx] = NULL;
3993 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003994
3995 iface->conf->num_bss--;
Dmitry Shmidt54605472013-11-08 11:10:19 -08003996 for (i = idx; i < iface->conf->num_bss; i++)
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003997 iface->conf->bss[i] = iface->conf->bss[i + 1];
3998
3999 return 0;
4000}
4001
4002
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004003int hostapd_remove_iface(struct hapd_interfaces *interfaces, char *buf)
4004{
4005 struct hostapd_iface *hapd_iface;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004006 size_t i, j, k = 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004007
4008 for (i = 0; i < interfaces->count; i++) {
4009 hapd_iface = interfaces->iface[i];
4010 if (hapd_iface == NULL)
4011 return -1;
Dmitry Shmidt54605472013-11-08 11:10:19 -08004012 if (!os_strcmp(hapd_iface->conf->bss[0]->iface, buf)) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004013 wpa_printf(MSG_INFO, "Remove interface '%s'", buf);
Dmitry Shmidta38abf92014-03-06 13:38:44 -08004014 hapd_iface->driver_ap_teardown =
4015 !!(hapd_iface->drv_flags &
4016 WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
4017
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004018 hostapd_interface_deinit_free(hapd_iface);
4019 k = i;
4020 while (k < (interfaces->count - 1)) {
4021 interfaces->iface[k] =
4022 interfaces->iface[k + 1];
4023 k++;
4024 }
4025 interfaces->count--;
Sunil Ravi99c035e2024-07-12 01:42:03 +00004026 hostapd_cleanup_unused_mlds(interfaces);
4027
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004028 return 0;
4029 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004030
4031 for (j = 0; j < hapd_iface->conf->num_bss; j++) {
Dmitry Shmidta38abf92014-03-06 13:38:44 -08004032 if (!os_strcmp(hapd_iface->conf->bss[j]->iface, buf)) {
4033 hapd_iface->driver_ap_teardown =
4034 !(hapd_iface->drv_flags &
4035 WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004036 return hostapd_remove_bss(hapd_iface, j);
Dmitry Shmidta38abf92014-03-06 13:38:44 -08004037 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004038 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004039 }
4040 return -1;
4041}
4042
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004043
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004044/**
4045 * hostapd_new_assoc_sta - Notify that a new station associated with the AP
4046 * @hapd: Pointer to BSS data
4047 * @sta: Pointer to the associated STA data
4048 * @reassoc: 1 to indicate this was a re-association; 0 = first association
4049 *
4050 * This function will be called whenever a station associates with the AP. It
4051 * can be called from ieee802_11.c for drivers that export MLME to hostapd and
4052 * from drv_callbacks.c based on driver events for drivers that take care of
4053 * management frames (IEEE 802.11 authentication and association) internally.
4054 */
4055void hostapd_new_assoc_sta(struct hostapd_data *hapd, struct sta_info *sta,
4056 int reassoc)
4057{
4058 if (hapd->tkip_countermeasures) {
4059 hostapd_drv_sta_deauth(hapd, sta->addr,
4060 WLAN_REASON_MICHAEL_MIC_FAILURE);
4061 return;
4062 }
4063
Sunil Ravi2a14cf12023-11-21 00:54:38 +00004064#ifdef CONFIG_IEEE80211BE
Sunil Ravib0ac25f2024-07-12 01:42:03 +00004065 if (ap_sta_is_mld(hapd, sta) &&
Sunil Ravi2a14cf12023-11-21 00:54:38 +00004066 sta->mld_assoc_link_id != hapd->mld_link_id)
4067 return;
4068#endif /* CONFIG_IEEE80211BE */
4069
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -08004070 ap_sta_clear_disconnect_timeouts(hapd, sta);
Hai Shalom899fcc72020-10-19 14:38:18 -07004071 sta->post_csa_sa_query = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004072
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004073#ifdef CONFIG_P2P
4074 if (sta->p2p_ie == NULL && !sta->no_p2p_set) {
4075 sta->no_p2p_set = 1;
4076 hapd->num_sta_no_p2p++;
4077 if (hapd->num_sta_no_p2p == 1)
4078 hostapd_p2p_non_p2p_sta_connected(hapd);
4079 }
4080#endif /* CONFIG_P2P */
4081
Hai Shalom81f62d82019-07-22 12:10:00 -07004082 airtime_policy_new_sta(hapd, sta);
4083
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004084 /* Start accounting here, if IEEE 802.1X and WPA are not used.
4085 * IEEE 802.1X/WPA code will start accounting after the station has
4086 * been authorized. */
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -08004087 if (!hapd->conf->ieee802_1x && !hapd->conf->wpa && !hapd->conf->osen) {
4088 ap_sta_set_authorized(hapd, sta, 1);
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08004089 os_get_reltime(&sta->connected_time);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004090 accounting_sta_start(hapd, sta);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004091 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004092
4093 /* Start IEEE 802.1X authentication process for new stations */
4094 ieee802_1x_new_station(hapd, sta);
4095 if (reassoc) {
4096 if (sta->auth_alg != WLAN_AUTH_FT &&
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004097 sta->auth_alg != WLAN_AUTH_FILS_SK &&
4098 sta->auth_alg != WLAN_AUTH_FILS_SK_PFS &&
4099 sta->auth_alg != WLAN_AUTH_FILS_PK &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004100 !(sta->flags & (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS)))
4101 wpa_auth_sm_event(sta->wpa_sm, WPA_REAUTH);
Sunil Ravi2a14cf12023-11-21 00:54:38 +00004102 } else if (!(hapd->iface->drv_flags2 &
4103 WPA_DRIVER_FLAGS2_4WAY_HANDSHAKE_AP_PSK)) {
4104 /* The 4-way handshake offloaded case will have this handled
4105 * based on the port authorized event. */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004106 wpa_auth_sta_associated(hapd->wpa_auth, sta->wpa_sm);
Sunil Ravi2a14cf12023-11-21 00:54:38 +00004107 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07004108
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08004109 if (hapd->iface->drv_flags & WPA_DRIVER_FLAGS_WIRED) {
4110 if (eloop_cancel_timeout(ap_handle_timer, hapd, sta) > 0) {
4111 wpa_printf(MSG_DEBUG,
4112 "%s: %s: canceled wired ap_handle_timer timeout for "
4113 MACSTR,
4114 hapd->conf->iface, __func__,
4115 MAC2STR(sta->addr));
4116 }
4117 } else if (!(hapd->iface->drv_flags &
4118 WPA_DRIVER_FLAGS_INACTIVITY_TIMER)) {
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -08004119 wpa_printf(MSG_DEBUG,
4120 "%s: %s: reschedule ap_handle_timer timeout for "
4121 MACSTR " (%d seconds - ap_max_inactivity)",
4122 hapd->conf->iface, __func__, MAC2STR(sta->addr),
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08004123 hapd->conf->ap_max_inactivity);
4124 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
4125 eloop_register_timeout(hapd->conf->ap_max_inactivity, 0,
4126 ap_handle_timer, hapd, sta);
4127 }
Hai Shalom81f62d82019-07-22 12:10:00 -07004128
4129#ifdef CONFIG_MACSEC
4130 if (hapd->conf->wpa_key_mgmt == WPA_KEY_MGMT_NONE &&
4131 hapd->conf->mka_psk_set)
4132 ieee802_1x_create_preshared_mka_hapd(hapd, sta);
4133 else
4134 ieee802_1x_alloc_kay_sm_hapd(hapd, sta);
4135#endif /* CONFIG_MACSEC */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004136}
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004137
4138
4139const char * hostapd_state_text(enum hostapd_iface_state s)
4140{
4141 switch (s) {
4142 case HAPD_IFACE_UNINITIALIZED:
4143 return "UNINITIALIZED";
4144 case HAPD_IFACE_DISABLED:
4145 return "DISABLED";
4146 case HAPD_IFACE_COUNTRY_UPDATE:
4147 return "COUNTRY_UPDATE";
4148 case HAPD_IFACE_ACS:
4149 return "ACS";
4150 case HAPD_IFACE_HT_SCAN:
4151 return "HT_SCAN";
4152 case HAPD_IFACE_DFS:
4153 return "DFS";
4154 case HAPD_IFACE_ENABLED:
4155 return "ENABLED";
Sunil Ravi2a14cf12023-11-21 00:54:38 +00004156 case HAPD_IFACE_NO_IR:
4157 return "NO_IR";
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004158 }
4159
4160 return "UNKNOWN";
4161}
4162
4163
4164void hostapd_set_state(struct hostapd_iface *iface, enum hostapd_iface_state s)
4165{
4166 wpa_printf(MSG_INFO, "%s: interface state %s->%s",
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07004167 iface->conf ? iface->conf->bss[0]->iface : "N/A",
4168 hostapd_state_text(iface->state), hostapd_state_text(s));
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004169 iface->state = s;
4170}
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004171
4172
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004173int hostapd_csa_in_progress(struct hostapd_iface *iface)
4174{
4175 unsigned int i;
4176
4177 for (i = 0; i < iface->num_bss; i++)
4178 if (iface->bss[i]->csa_in_progress)
4179 return 1;
4180 return 0;
4181}
4182
4183
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004184#ifdef NEED_AP_MLME
4185
Sunil Ravi7f769292024-07-23 22:21:32 +00004186void free_beacon_data(struct beacon_data *beacon)
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004187{
4188 os_free(beacon->head);
4189 beacon->head = NULL;
4190 os_free(beacon->tail);
4191 beacon->tail = NULL;
4192 os_free(beacon->probe_resp);
4193 beacon->probe_resp = NULL;
4194 os_free(beacon->beacon_ies);
4195 beacon->beacon_ies = NULL;
4196 os_free(beacon->proberesp_ies);
4197 beacon->proberesp_ies = NULL;
4198 os_free(beacon->assocresp_ies);
4199 beacon->assocresp_ies = NULL;
4200}
4201
4202
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004203static int hostapd_build_beacon_data(struct hostapd_data *hapd,
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004204 struct beacon_data *beacon)
4205{
4206 struct wpabuf *beacon_extra, *proberesp_extra, *assocresp_extra;
4207 struct wpa_driver_ap_params params;
4208 int ret;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004209
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08004210 os_memset(beacon, 0, sizeof(*beacon));
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004211 ret = ieee802_11_build_ap_params(hapd, &params);
4212 if (ret < 0)
4213 return ret;
4214
4215 ret = hostapd_build_ap_extra_ies(hapd, &beacon_extra,
4216 &proberesp_extra,
4217 &assocresp_extra);
4218 if (ret)
4219 goto free_ap_params;
4220
4221 ret = -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004222 beacon->head = os_memdup(params.head, params.head_len);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004223 if (!beacon->head)
4224 goto free_ap_extra_ies;
4225
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004226 beacon->head_len = params.head_len;
4227
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004228 beacon->tail = os_memdup(params.tail, params.tail_len);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004229 if (!beacon->tail)
4230 goto free_beacon;
4231
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004232 beacon->tail_len = params.tail_len;
4233
4234 if (params.proberesp != NULL) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004235 beacon->probe_resp = os_memdup(params.proberesp,
4236 params.proberesp_len);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004237 if (!beacon->probe_resp)
4238 goto free_beacon;
4239
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004240 beacon->probe_resp_len = params.proberesp_len;
4241 }
4242
4243 /* copy the extra ies */
4244 if (beacon_extra) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004245 beacon->beacon_ies = os_memdup(beacon_extra->buf,
4246 wpabuf_len(beacon_extra));
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004247 if (!beacon->beacon_ies)
4248 goto free_beacon;
4249
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004250 beacon->beacon_ies_len = wpabuf_len(beacon_extra);
4251 }
4252
4253 if (proberesp_extra) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004254 beacon->proberesp_ies = os_memdup(proberesp_extra->buf,
4255 wpabuf_len(proberesp_extra));
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004256 if (!beacon->proberesp_ies)
4257 goto free_beacon;
4258
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004259 beacon->proberesp_ies_len = wpabuf_len(proberesp_extra);
4260 }
4261
4262 if (assocresp_extra) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004263 beacon->assocresp_ies = os_memdup(assocresp_extra->buf,
4264 wpabuf_len(assocresp_extra));
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004265 if (!beacon->assocresp_ies)
4266 goto free_beacon;
4267
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004268 beacon->assocresp_ies_len = wpabuf_len(assocresp_extra);
4269 }
4270
4271 ret = 0;
4272free_beacon:
4273 /* if the function fails, the caller should not free beacon data */
4274 if (ret)
4275 free_beacon_data(beacon);
4276
4277free_ap_extra_ies:
4278 hostapd_free_ap_extra_ies(hapd, beacon_extra, proberesp_extra,
4279 assocresp_extra);
4280free_ap_params:
4281 ieee802_11_free_ap_params(&params);
4282 return ret;
4283}
4284
4285
4286/*
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004287 * TODO: This flow currently supports only changing channel and width within
4288 * the same hw_mode. Any other changes to MAC parameters or provided settings
4289 * are not supported.
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004290 */
4291static int hostapd_change_config_freq(struct hostapd_data *hapd,
4292 struct hostapd_config *conf,
4293 struct hostapd_freq_params *params,
4294 struct hostapd_freq_params *old_params)
4295{
4296 int channel;
Sunil Ravib0ac25f2024-07-12 01:42:03 +00004297 u8 seg0 = 0, seg1 = 0;
Hai Shalom81f62d82019-07-22 12:10:00 -07004298 struct hostapd_hw_modes *mode;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004299
4300 if (!params->channel) {
4301 /* check if the new channel is supported by hw */
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004302 params->channel = hostapd_hw_get_channel(hapd, params->freq);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004303 }
4304
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004305 channel = params->channel;
4306 if (!channel)
4307 return -1;
4308
Sunil Ravi2a14cf12023-11-21 00:54:38 +00004309 hostapd_determine_mode(hapd->iface);
Hai Shalom81f62d82019-07-22 12:10:00 -07004310 mode = hapd->iface->current_mode;
4311
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004312 /* if a pointer to old_params is provided we save previous state */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004313 if (old_params &&
4314 hostapd_set_freq_params(old_params, conf->hw_mode,
4315 hostapd_hw_get_freq(hapd, conf->channel),
Hai Shalomc3565922019-10-28 11:58:20 -07004316 conf->channel, conf->enable_edmg,
4317 conf->edmg_channel, conf->ieee80211n,
Hai Shalom81f62d82019-07-22 12:10:00 -07004318 conf->ieee80211ac, conf->ieee80211ax,
Sunil Ravia04bd252022-05-02 22:54:18 -07004319 conf->ieee80211be, conf->secondary_channel,
Hai Shalom81f62d82019-07-22 12:10:00 -07004320 hostapd_get_oper_chwidth(conf),
4321 hostapd_get_oper_centr_freq_seg0_idx(conf),
4322 hostapd_get_oper_centr_freq_seg1_idx(conf),
4323 conf->vht_capab,
4324 mode ? &mode->he_capab[IEEE80211_MODE_AP] :
Sunil Ravia04bd252022-05-02 22:54:18 -07004325 NULL,
4326 mode ? &mode->eht_capab[IEEE80211_MODE_AP] :
Sunil Ravi99c035e2024-07-12 01:42:03 +00004327 NULL,
4328 hostapd_get_punct_bitmap(hapd)))
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004329 return -1;
4330
4331 switch (params->bandwidth) {
4332 case 0:
4333 case 20:
Hai Shaloma20dcd72022-02-04 13:43:00 -08004334 conf->ht_capab &= ~HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET;
4335 break;
4336 case 40:
4337 case 80:
4338 case 160:
Sunil Ravi640215c2023-06-28 23:08:09 +00004339 case 320:
Hai Shaloma20dcd72022-02-04 13:43:00 -08004340 conf->ht_capab |= HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET;
4341 break;
4342 default:
4343 return -1;
4344 }
4345
4346 switch (params->bandwidth) {
4347 case 0:
4348 case 20:
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004349 case 40:
Sunil8cd6f4d2022-06-28 18:40:46 +00004350 hostapd_set_oper_chwidth(conf, CONF_OPER_CHWIDTH_USE_HT);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004351 break;
4352 case 80:
4353 if (params->center_freq2)
Sunil8cd6f4d2022-06-28 18:40:46 +00004354 hostapd_set_oper_chwidth(conf,
4355 CONF_OPER_CHWIDTH_80P80MHZ);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004356 else
Sunil8cd6f4d2022-06-28 18:40:46 +00004357 hostapd_set_oper_chwidth(conf,
4358 CONF_OPER_CHWIDTH_80MHZ);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004359 break;
4360 case 160:
Sunil8cd6f4d2022-06-28 18:40:46 +00004361 hostapd_set_oper_chwidth(conf, CONF_OPER_CHWIDTH_160MHZ);
4362 break;
4363 case 320:
4364 hostapd_set_oper_chwidth(conf, CONF_OPER_CHWIDTH_320MHZ);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004365 break;
4366 default:
4367 return -1;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004368 }
4369
4370 conf->channel = channel;
4371 conf->ieee80211n = params->ht_enabled;
Hai Shaloma20dcd72022-02-04 13:43:00 -08004372 conf->ieee80211ac = params->vht_enabled;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004373 conf->secondary_channel = params->sec_channel_offset;
Sunil Ravib0ac25f2024-07-12 01:42:03 +00004374 if (params->center_freq1 &&
4375 ieee80211_freq_to_chan(params->center_freq1, &seg0) ==
4376 NUM_HOSTAPD_MODES)
4377 return -1;
4378 if (params->center_freq2 &&
4379 ieee80211_freq_to_chan(params->center_freq2,
4380 &seg1) == NUM_HOSTAPD_MODES)
4381 return -1;
Hai Shalom81f62d82019-07-22 12:10:00 -07004382 hostapd_set_oper_centr_freq_seg0_idx(conf, seg0);
4383 hostapd_set_oper_centr_freq_seg1_idx(conf, seg1);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004384
4385 /* TODO: maybe call here hostapd_config_check here? */
4386
4387 return 0;
4388}
4389
4390
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004391static int hostapd_fill_csa_settings(struct hostapd_data *hapd,
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004392 struct csa_settings *settings)
4393{
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004394 struct hostapd_iface *iface = hapd->iface;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004395 struct hostapd_freq_params old_freq;
4396 int ret;
Sunil Ravi036cec52023-03-29 11:35:17 -07004397#ifdef CONFIG_IEEE80211BE
4398 u16 old_punct_bitmap;
4399#endif /* CONFIG_IEEE80211BE */
Hai Shalom81f62d82019-07-22 12:10:00 -07004400 u8 chan, bandwidth;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004401
4402 os_memset(&old_freq, 0, sizeof(old_freq));
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004403 if (!iface || !iface->freq || hapd->csa_in_progress)
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004404 return -1;
4405
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004406 switch (settings->freq_params.bandwidth) {
4407 case 80:
4408 if (settings->freq_params.center_freq2)
Sunil8cd6f4d2022-06-28 18:40:46 +00004409 bandwidth = CONF_OPER_CHWIDTH_80P80MHZ;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004410 else
Sunil8cd6f4d2022-06-28 18:40:46 +00004411 bandwidth = CONF_OPER_CHWIDTH_80MHZ;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004412 break;
4413 case 160:
Sunil8cd6f4d2022-06-28 18:40:46 +00004414 bandwidth = CONF_OPER_CHWIDTH_160MHZ;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004415 break;
Sunil Ravi640215c2023-06-28 23:08:09 +00004416 case 320:
4417 bandwidth = CONF_OPER_CHWIDTH_320MHZ;
4418 break;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004419 default:
Sunil8cd6f4d2022-06-28 18:40:46 +00004420 bandwidth = CONF_OPER_CHWIDTH_USE_HT;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004421 break;
4422 }
4423
4424 if (ieee80211_freq_to_channel_ext(
4425 settings->freq_params.freq,
4426 settings->freq_params.sec_channel_offset,
Hai Shalom81f62d82019-07-22 12:10:00 -07004427 bandwidth,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004428 &hapd->iface->cs_oper_class,
4429 &chan) == NUM_HOSTAPD_MODES) {
4430 wpa_printf(MSG_DEBUG,
Sunil Ravia04bd252022-05-02 22:54:18 -07004431 "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 -08004432 settings->freq_params.freq,
4433 settings->freq_params.sec_channel_offset,
Hai Shalom81f62d82019-07-22 12:10:00 -07004434 settings->freq_params.vht_enabled,
Sunil Ravia04bd252022-05-02 22:54:18 -07004435 settings->freq_params.he_enabled,
4436 settings->freq_params.eht_enabled);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004437 return -1;
4438 }
4439
4440 settings->freq_params.channel = chan;
4441
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004442 ret = hostapd_change_config_freq(iface->bss[0], iface->conf,
4443 &settings->freq_params,
4444 &old_freq);
4445 if (ret)
4446 return ret;
4447
Sunil Ravi036cec52023-03-29 11:35:17 -07004448#ifdef CONFIG_IEEE80211BE
4449 old_punct_bitmap = iface->conf->punct_bitmap;
4450 iface->conf->punct_bitmap = settings->punct_bitmap;
4451#endif /* CONFIG_IEEE80211BE */
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004452 ret = hostapd_build_beacon_data(hapd, &settings->beacon_after);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004453
4454 /* change back the configuration */
Sunil Ravi036cec52023-03-29 11:35:17 -07004455#ifdef CONFIG_IEEE80211BE
4456 iface->conf->punct_bitmap = old_punct_bitmap;
4457#endif /* CONFIG_IEEE80211BE */
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004458 hostapd_change_config_freq(iface->bss[0], iface->conf,
4459 &old_freq, NULL);
4460
4461 if (ret)
4462 return ret;
4463
4464 /* set channel switch parameters for csa ie */
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004465 hapd->cs_freq_params = settings->freq_params;
4466 hapd->cs_count = settings->cs_count;
4467 hapd->cs_block_tx = settings->block_tx;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004468
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004469 ret = hostapd_build_beacon_data(hapd, &settings->beacon_csa);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004470 if (ret) {
4471 free_beacon_data(&settings->beacon_after);
4472 return ret;
4473 }
4474
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004475 settings->counter_offset_beacon[0] = hapd->cs_c_off_beacon;
4476 settings->counter_offset_presp[0] = hapd->cs_c_off_proberesp;
4477 settings->counter_offset_beacon[1] = hapd->cs_c_off_ecsa_beacon;
4478 settings->counter_offset_presp[1] = hapd->cs_c_off_ecsa_proberesp;
Sunil Ravib0ac25f2024-07-12 01:42:03 +00004479 settings->link_id = -1;
4480#ifdef CONFIG_IEEE80211BE
4481 if (hapd->conf->mld_ap)
4482 settings->link_id = hapd->mld_link_id;
4483#endif /* CONFIG_IEEE80211BE */
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004484
Sunil Ravi7f769292024-07-23 22:21:32 +00004485#ifdef CONFIG_IEEE80211AX
4486 settings->ubpr.unsol_bcast_probe_resp_tmpl =
4487 hostapd_unsol_bcast_probe_resp(hapd, &settings->ubpr);
4488#endif /* CONFIG_IEEE80211AX */
4489
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004490 return 0;
4491}
4492
4493
4494void hostapd_cleanup_cs_params(struct hostapd_data *hapd)
4495{
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004496 os_memset(&hapd->cs_freq_params, 0, sizeof(hapd->cs_freq_params));
4497 hapd->cs_count = 0;
4498 hapd->cs_block_tx = 0;
4499 hapd->cs_c_off_beacon = 0;
4500 hapd->cs_c_off_proberesp = 0;
4501 hapd->csa_in_progress = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004502 hapd->cs_c_off_ecsa_beacon = 0;
4503 hapd->cs_c_off_ecsa_proberesp = 0;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004504}
4505
4506
Hai Shalom60840252021-02-19 19:02:11 -08004507void hostapd_chan_switch_config(struct hostapd_data *hapd,
4508 struct hostapd_freq_params *freq_params)
Roshan Pius3a1667e2018-07-03 15:17:14 -07004509{
Sunil Ravia04bd252022-05-02 22:54:18 -07004510 if (freq_params->eht_enabled)
4511 hapd->iconf->ch_switch_eht_config |= CH_SWITCH_EHT_ENABLED;
4512 else
4513 hapd->iconf->ch_switch_eht_config |= CH_SWITCH_EHT_DISABLED;
4514
Hai Shalom60840252021-02-19 19:02:11 -08004515 if (freq_params->he_enabled)
4516 hapd->iconf->ch_switch_he_config |= CH_SWITCH_HE_ENABLED;
4517 else
4518 hapd->iconf->ch_switch_he_config |= CH_SWITCH_HE_DISABLED;
4519
4520 if (freq_params->vht_enabled)
Roshan Pius3a1667e2018-07-03 15:17:14 -07004521 hapd->iconf->ch_switch_vht_config |= CH_SWITCH_VHT_ENABLED;
4522 else
4523 hapd->iconf->ch_switch_vht_config |= CH_SWITCH_VHT_DISABLED;
4524
4525 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
Hai Shalom60840252021-02-19 19:02:11 -08004526 HOSTAPD_LEVEL_INFO,
Sunil Ravia04bd252022-05-02 22:54:18 -07004527 "CHAN_SWITCH EHT config 0x%x HE config 0x%x VHT config 0x%x",
4528 hapd->iconf->ch_switch_eht_config,
Hai Shalom60840252021-02-19 19:02:11 -08004529 hapd->iconf->ch_switch_he_config,
Roshan Pius3a1667e2018-07-03 15:17:14 -07004530 hapd->iconf->ch_switch_vht_config);
4531}
4532
4533
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004534int hostapd_switch_channel(struct hostapd_data *hapd,
4535 struct csa_settings *settings)
4536{
4537 int ret;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004538
4539 if (!(hapd->iface->drv_flags & WPA_DRIVER_FLAGS_AP_CSA)) {
4540 wpa_printf(MSG_INFO, "CSA is not supported");
4541 return -1;
4542 }
4543
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004544 ret = hostapd_fill_csa_settings(hapd, settings);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004545 if (ret)
4546 return ret;
4547
4548 ret = hostapd_drv_switch_channel(hapd, settings);
4549 free_beacon_data(&settings->beacon_csa);
4550 free_beacon_data(&settings->beacon_after);
Sunil Ravi7f769292024-07-23 22:21:32 +00004551#ifdef CONFIG_IEEE80211AX
4552 os_free(settings->ubpr.unsol_bcast_probe_resp_tmpl);
4553#endif /* CONFIG_IEEE80211AX */
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004554
4555 if (ret) {
4556 /* if we failed, clean cs parameters */
4557 hostapd_cleanup_cs_params(hapd);
4558 return ret;
4559 }
4560
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004561 hapd->csa_in_progress = 1;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004562 return 0;
4563}
4564
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004565
Sunil Ravic0f5d412024-09-11 22:12:49 +00004566int hostapd_force_channel_switch(struct hostapd_iface *iface,
4567 struct csa_settings *settings)
4568{
4569 int ret = 0;
4570
4571 if (!settings->freq_params.channel) {
4572 /* Check if the new channel is supported */
4573 settings->freq_params.channel = hostapd_hw_get_channel(
4574 iface->bss[0], settings->freq_params.freq);
4575 if (!settings->freq_params.channel)
4576 return -1;
4577 }
4578
4579 ret = hostapd_disable_iface(iface);
4580 if (ret) {
4581 wpa_printf(MSG_DEBUG, "Failed to disable the interface");
4582 return ret;
4583 }
4584
4585 hostapd_chan_switch_config(iface->bss[0], &settings->freq_params);
4586 ret = hostapd_change_config_freq(iface->bss[0], iface->conf,
4587 &settings->freq_params, NULL);
4588 if (ret) {
4589 wpa_printf(MSG_DEBUG,
4590 "Failed to set the new channel in config");
4591 return ret;
4592 }
4593
4594 ret = hostapd_enable_iface(iface);
4595 if (ret)
4596 wpa_printf(MSG_DEBUG, "Failed to enable the interface");
4597
4598 return ret;
4599}
4600
4601
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004602void
4603hostapd_switch_channel_fallback(struct hostapd_iface *iface,
4604 const struct hostapd_freq_params *freq_params)
4605{
Sunil Ravi7f769292024-07-23 22:21:32 +00004606 u8 seg0_idx = 0, seg1_idx = 0;
Sunil8cd6f4d2022-06-28 18:40:46 +00004607 enum oper_chan_width bw = CONF_OPER_CHWIDTH_USE_HT;
Sunil Ravi7f769292024-07-23 22:21:32 +00004608 u8 op_class, chan = 0;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004609
4610 wpa_printf(MSG_DEBUG, "Restarting all CSA-related BSSes");
4611
4612 if (freq_params->center_freq1)
Sunil Ravi7f769292024-07-23 22:21:32 +00004613 ieee80211_freq_to_chan(freq_params->center_freq1, &seg0_idx);
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004614 if (freq_params->center_freq2)
Sunil Ravi7f769292024-07-23 22:21:32 +00004615 ieee80211_freq_to_chan(freq_params->center_freq2, &seg1_idx);
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004616
4617 switch (freq_params->bandwidth) {
4618 case 0:
4619 case 20:
4620 case 40:
Sunil8cd6f4d2022-06-28 18:40:46 +00004621 bw = CONF_OPER_CHWIDTH_USE_HT;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004622 break;
4623 case 80:
Sunil Ravib0ac25f2024-07-12 01:42:03 +00004624 if (freq_params->center_freq2) {
Sunil8cd6f4d2022-06-28 18:40:46 +00004625 bw = CONF_OPER_CHWIDTH_80P80MHZ;
Sunil Ravib0ac25f2024-07-12 01:42:03 +00004626 iface->conf->vht_capab |=
4627 VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ;
4628 } else {
Sunil8cd6f4d2022-06-28 18:40:46 +00004629 bw = CONF_OPER_CHWIDTH_80MHZ;
Sunil Ravib0ac25f2024-07-12 01:42:03 +00004630 }
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004631 break;
4632 case 160:
Sunil8cd6f4d2022-06-28 18:40:46 +00004633 bw = CONF_OPER_CHWIDTH_160MHZ;
Sunil Ravib0ac25f2024-07-12 01:42:03 +00004634 iface->conf->vht_capab |= VHT_CAP_SUPP_CHAN_WIDTH_160MHZ;
Sunil8cd6f4d2022-06-28 18:40:46 +00004635 break;
4636 case 320:
4637 bw = CONF_OPER_CHWIDTH_320MHZ;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004638 break;
4639 default:
4640 wpa_printf(MSG_WARNING, "Unknown CSA bandwidth: %d",
4641 freq_params->bandwidth);
4642 break;
4643 }
4644
4645 iface->freq = freq_params->freq;
4646 iface->conf->channel = freq_params->channel;
4647 iface->conf->secondary_channel = freq_params->sec_channel_offset;
Sunil Ravi7f769292024-07-23 22:21:32 +00004648 if (ieee80211_freq_to_channel_ext(freq_params->freq,
4649 freq_params->sec_channel_offset, bw,
4650 &op_class, &chan) ==
4651 NUM_HOSTAPD_MODES ||
4652 chan != freq_params->channel)
4653 wpa_printf(MSG_INFO, "CSA: Channel mismatch: %d -> %d",
4654 freq_params->channel, chan);
4655
4656 iface->conf->op_class = op_class;
Hai Shalom81f62d82019-07-22 12:10:00 -07004657 hostapd_set_oper_centr_freq_seg0_idx(iface->conf, seg0_idx);
4658 hostapd_set_oper_centr_freq_seg1_idx(iface->conf, seg1_idx);
4659 hostapd_set_oper_chwidth(iface->conf, bw);
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004660 iface->conf->ieee80211n = freq_params->ht_enabled;
4661 iface->conf->ieee80211ac = freq_params->vht_enabled;
Hai Shalom81f62d82019-07-22 12:10:00 -07004662 iface->conf->ieee80211ax = freq_params->he_enabled;
Sunil Ravia04bd252022-05-02 22:54:18 -07004663 iface->conf->ieee80211be = freq_params->eht_enabled;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004664
4665 /*
4666 * cs_params must not be cleared earlier because the freq_params
4667 * argument may actually point to one of these.
Hai Shalom39ba6fc2019-01-22 12:40:38 -08004668 * These params will be cleared during interface disable below.
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004669 */
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004670 hostapd_disable_iface(iface);
4671 hostapd_enable_iface(iface);
4672}
4673
Sunil Ravia04bd252022-05-02 22:54:18 -07004674
4675#ifdef CONFIG_IEEE80211AX
4676
4677void hostapd_cleanup_cca_params(struct hostapd_data *hapd)
4678{
4679 hapd->cca_count = 0;
4680 hapd->cca_color = 0;
4681 hapd->cca_c_off_beacon = 0;
4682 hapd->cca_c_off_proberesp = 0;
4683 hapd->cca_in_progress = false;
4684}
4685
4686
Sunil Ravi7f769292024-07-23 22:21:32 +00004687int hostapd_fill_cca_settings(struct hostapd_data *hapd,
4688 struct cca_settings *settings)
Sunil Ravia04bd252022-05-02 22:54:18 -07004689{
4690 struct hostapd_iface *iface = hapd->iface;
4691 u8 old_color;
4692 int ret;
4693
4694 if (!iface || iface->conf->he_op.he_bss_color_disabled)
4695 return -1;
4696
Sunil Ravi7f769292024-07-23 22:21:32 +00004697 settings->link_id = -1;
4698#ifdef CONFIG_IEEE80211BE
4699 if (hapd->conf->mld_ap)
4700 settings->link_id = hapd->mld_link_id;
4701#endif /* CONFIG_IEEE80211BE */
4702
Sunil Ravia04bd252022-05-02 22:54:18 -07004703 old_color = iface->conf->he_op.he_bss_color;
4704 iface->conf->he_op.he_bss_color = hapd->cca_color;
4705 ret = hostapd_build_beacon_data(hapd, &settings->beacon_after);
4706 if (ret)
4707 return ret;
4708
4709 iface->conf->he_op.he_bss_color = old_color;
4710
4711 settings->cca_count = hapd->cca_count;
4712 settings->cca_color = hapd->cca_color,
4713 hapd->cca_in_progress = true;
4714
4715 ret = hostapd_build_beacon_data(hapd, &settings->beacon_cca);
4716 if (ret) {
4717 free_beacon_data(&settings->beacon_after);
4718 return ret;
4719 }
4720
Sunil Ravi7f769292024-07-23 22:21:32 +00004721 settings->ubpr.unsol_bcast_probe_resp_tmpl =
4722 hostapd_unsol_bcast_probe_resp(hapd, &settings->ubpr);
4723
Sunil Ravia04bd252022-05-02 22:54:18 -07004724 settings->counter_offset_beacon = hapd->cca_c_off_beacon;
4725 settings->counter_offset_presp = hapd->cca_c_off_proberesp;
4726
4727 return 0;
4728}
4729
4730
4731static void hostapd_switch_color_timeout_handler(void *eloop_data,
4732 void *user_ctx)
4733{
4734 struct hostapd_data *hapd = (struct hostapd_data *) eloop_data;
4735 os_time_t delta_t;
4736 unsigned int b;
4737 int i, r;
4738
4739 /* CCA can be triggered once the handler constantly receives
4740 * color collision events to for at least
4741 * DOT11BSS_COLOR_COLLISION_AP_PERIOD (50 s by default). */
4742 delta_t = hapd->last_color_collision.sec -
4743 hapd->first_color_collision.sec;
4744 if (delta_t < DOT11BSS_COLOR_COLLISION_AP_PERIOD)
4745 return;
4746
4747 r = os_random() % HE_OPERATION_BSS_COLOR_MAX;
4748 for (i = 0; i < HE_OPERATION_BSS_COLOR_MAX; i++) {
Sunil8cd6f4d2022-06-28 18:40:46 +00004749 if (r && !(hapd->color_collision_bitmap & (1ULL << r)))
Sunil Ravia04bd252022-05-02 22:54:18 -07004750 break;
4751
4752 r = (r + 1) % HE_OPERATION_BSS_COLOR_MAX;
4753 }
4754
4755 if (i == HE_OPERATION_BSS_COLOR_MAX) {
4756 /* There are no free colors so turn BSS coloring off */
4757 wpa_printf(MSG_INFO,
4758 "No free colors left, turning off BSS coloring");
4759 hapd->iface->conf->he_op.he_bss_color_disabled = 1;
4760 hapd->iface->conf->he_op.he_bss_color = os_random() % 63 + 1;
4761 for (b = 0; b < hapd->iface->num_bss; b++)
4762 ieee802_11_set_beacon(hapd->iface->bss[b]);
4763 return;
4764 }
4765
4766 for (b = 0; b < hapd->iface->num_bss; b++) {
4767 struct hostapd_data *bss = hapd->iface->bss[b];
4768 struct cca_settings settings;
4769 int ret;
4770
4771 hostapd_cleanup_cca_params(bss);
4772 bss->cca_color = r;
4773 bss->cca_count = 10;
4774
4775 if (hostapd_fill_cca_settings(bss, &settings)) {
4776 hostapd_cleanup_cca_params(bss);
4777 continue;
4778 }
4779
4780 ret = hostapd_drv_switch_color(bss, &settings);
4781 if (ret)
4782 hostapd_cleanup_cca_params(bss);
4783
4784 free_beacon_data(&settings.beacon_cca);
4785 free_beacon_data(&settings.beacon_after);
Sunil Ravi7f769292024-07-23 22:21:32 +00004786 os_free(settings.ubpr.unsol_bcast_probe_resp_tmpl);
Sunil Ravia04bd252022-05-02 22:54:18 -07004787 }
4788}
4789
4790
4791void hostapd_switch_color(struct hostapd_data *hapd, u64 bitmap)
4792{
4793 struct os_reltime now;
4794
4795 if (hapd->cca_in_progress)
4796 return;
4797
4798 if (os_get_reltime(&now))
4799 return;
4800
4801 hapd->color_collision_bitmap = bitmap;
4802 hapd->last_color_collision = now;
4803
4804 if (eloop_is_timeout_registered(hostapd_switch_color_timeout_handler,
4805 hapd, NULL))
4806 return;
4807
4808 hapd->first_color_collision = now;
4809 /* 10 s window as margin for persistent color collision reporting */
4810 eloop_register_timeout(DOT11BSS_COLOR_COLLISION_AP_PERIOD + 10, 0,
4811 hostapd_switch_color_timeout_handler,
4812 hapd, NULL);
4813}
4814
4815#endif /* CONFIG_IEEE80211AX */
4816
Dmitry Shmidte4663042016-04-04 10:07:49 -07004817#endif /* NEED_AP_MLME */
4818
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004819
4820struct hostapd_data * hostapd_get_iface(struct hapd_interfaces *interfaces,
4821 const char *ifname)
4822{
4823 size_t i, j;
4824
4825 for (i = 0; i < interfaces->count; i++) {
4826 struct hostapd_iface *iface = interfaces->iface[i];
4827
4828 for (j = 0; j < iface->num_bss; j++) {
4829 struct hostapd_data *hapd = iface->bss[j];
4830
4831 if (os_strcmp(ifname, hapd->conf->iface) == 0)
4832 return hapd;
4833 }
4834 }
4835
4836 return NULL;
4837}
4838
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004839
4840void hostapd_periodic_iface(struct hostapd_iface *iface)
4841{
4842 size_t i;
4843
4844 ap_list_timer(iface);
4845
4846 for (i = 0; i < iface->num_bss; i++) {
4847 struct hostapd_data *hapd = iface->bss[i];
4848
4849 if (!hapd->started)
4850 continue;
4851
4852#ifndef CONFIG_NO_RADIUS
4853 hostapd_acl_expire(hapd);
4854#endif /* CONFIG_NO_RADIUS */
4855 }
4856}
Hai Shalom899fcc72020-10-19 14:38:18 -07004857
4858
4859#ifdef CONFIG_OCV
4860void hostapd_ocv_check_csa_sa_query(void *eloop_ctx, void *timeout_ctx)
4861{
4862 struct hostapd_data *hapd = eloop_ctx;
4863 struct sta_info *sta;
4864
4865 wpa_printf(MSG_DEBUG, "OCV: Post-CSA SA Query initiation check");
4866
4867 for (sta = hapd->sta_list; sta; sta = sta->next) {
4868 if (!sta->post_csa_sa_query)
4869 continue;
4870
4871 wpa_printf(MSG_DEBUG, "OCV: OCVC STA " MACSTR
4872 " did not start SA Query after CSA - disconnect",
4873 MAC2STR(sta->addr));
4874 ap_sta_disconnect(hapd, sta, sta->addr,
4875 WLAN_REASON_PREV_AUTH_NOT_VALID);
4876 }
4877}
4878#endif /* CONFIG_OCV */
Sunil Ravi2a14cf12023-11-21 00:54:38 +00004879
4880
4881#ifdef CONFIG_IEEE80211BE
Sunil Ravi99c035e2024-07-12 01:42:03 +00004882
Sunil Ravi2a14cf12023-11-21 00:54:38 +00004883struct hostapd_data * hostapd_mld_get_link_bss(struct hostapd_data *hapd,
4884 u8 link_id)
4885{
Sunil Ravi7f769292024-07-23 22:21:32 +00004886 struct hostapd_iface *iface;
4887 struct hostapd_data *bss;
4888 unsigned int i, j;
Sunil Ravi2a14cf12023-11-21 00:54:38 +00004889
4890 for (i = 0; i < hapd->iface->interfaces->count; i++) {
Sunil Ravi7f769292024-07-23 22:21:32 +00004891 iface = hapd->iface->interfaces->iface[i];
4892 if (!iface)
Sunil Ravi2a14cf12023-11-21 00:54:38 +00004893 continue;
4894
Sunil Ravi7f769292024-07-23 22:21:32 +00004895 for (j = 0; j < iface->num_bss; j++) {
4896 bss = iface->bss[j];
4897
4898 if (!bss->conf->mld_ap ||
4899 !hostapd_is_ml_partner(hapd, bss))
4900 continue;
4901
4902 if (!bss->drv_priv)
4903 continue;
4904
4905 if (bss->mld_link_id == link_id)
4906 return bss;
4907 }
Sunil Ravi2a14cf12023-11-21 00:54:38 +00004908 }
4909
4910 return NULL;
4911}
Sunil Ravi99c035e2024-07-12 01:42:03 +00004912
4913
4914bool hostapd_is_ml_partner(struct hostapd_data *hapd1,
4915 struct hostapd_data *hapd2)
4916{
4917 if (!hapd1->conf->mld_ap || !hapd2->conf->mld_ap)
4918 return false;
4919
4920 return !os_strcmp(hapd1->conf->iface, hapd2->conf->iface);
4921}
4922
4923
4924u8 hostapd_get_mld_id(struct hostapd_data *hapd)
4925{
4926 if (!hapd->conf->mld_ap)
4927 return 255;
4928
4929 /* MLD ID 0 represents self */
4930 return 0;
4931
4932 /* TODO: MLD ID for Multiple BSS cases */
4933}
4934
4935
4936int hostapd_mld_add_link(struct hostapd_data *hapd)
4937{
4938 struct hostapd_mld *mld = hapd->mld;
4939
4940 if (!hapd->conf->mld_ap)
4941 return 0;
4942
4943 /* Should not happen */
4944 if (!mld)
4945 return -1;
4946
4947 dl_list_add_tail(&mld->links, &hapd->link);
4948 mld->num_links++;
4949
4950 wpa_printf(MSG_DEBUG, "AP MLD %s: Link ID %d added. num_links: %d",
4951 mld->name, hapd->mld_link_id, mld->num_links);
4952
4953 if (mld->fbss)
4954 return 0;
4955
4956 mld->fbss = hapd;
4957 wpa_printf(MSG_DEBUG, "AP MLD %s: First link BSS set to %p",
4958 mld->name, mld->fbss);
4959 return 0;
4960}
4961
4962
4963int hostapd_mld_remove_link(struct hostapd_data *hapd)
4964{
4965 struct hostapd_mld *mld = hapd->mld;
4966 struct hostapd_data *next_fbss;
4967
4968 if (!hapd->conf->mld_ap)
4969 return 0;
4970
4971 /* Should not happen */
4972 if (!mld)
4973 return -1;
4974
4975 dl_list_del(&hapd->link);
4976 mld->num_links--;
4977
4978 wpa_printf(MSG_DEBUG, "AP MLD %s: Link ID %d removed. num_links: %d",
4979 mld->name, hapd->mld_link_id, mld->num_links);
4980
4981 if (mld->fbss != hapd)
4982 return 0;
4983
4984 /* If the list is empty, all links are removed */
4985 if (dl_list_empty(&mld->links)) {
4986 mld->fbss = NULL;
4987 } else {
4988 next_fbss = dl_list_entry(mld->links.next, struct hostapd_data,
4989 link);
4990 mld->fbss = next_fbss;
4991 }
4992
4993 wpa_printf(MSG_DEBUG, "AP MLD %s: First link BSS set to %p",
4994 mld->name, mld->fbss);
4995 return 0;
4996}
4997
4998
4999bool hostapd_mld_is_first_bss(struct hostapd_data *hapd)
5000{
5001 struct hostapd_mld *mld = hapd->mld;
5002
5003 if (!hapd->conf->mld_ap)
5004 return true;
5005
5006 /* Should not happen */
5007 if (!mld)
5008 return false;
5009
5010 /* If fbss is not set, it is safe to assume the caller is the first BSS.
5011 */
5012 if (!mld->fbss)
5013 return true;
5014
5015 return hapd == mld->fbss;
5016}
5017
5018
5019struct hostapd_data * hostapd_mld_get_first_bss(struct hostapd_data *hapd)
5020{
5021 struct hostapd_mld *mld = hapd->mld;
5022
5023 if (!hapd->conf->mld_ap)
5024 return NULL;
5025
5026 /* Should not happen */
5027 if (!mld)
5028 return NULL;
5029
5030 return mld->fbss;
5031}
5032
Sunil Ravic0f5d412024-09-11 22:12:49 +00005033
5034void hostapd_mld_interface_freed(struct hostapd_data *hapd)
5035{
5036 struct hostapd_data *link_bss = NULL;
5037
5038 if (!hapd || !hapd->conf->mld_ap)
5039 return;
5040
5041 for_each_mld_link(link_bss, hapd)
5042 link_bss->drv_priv = NULL;
5043}
5044
Sunil Ravi2a14cf12023-11-21 00:54:38 +00005045#endif /* CONFIG_IEEE80211BE */
Sunil Ravi99c035e2024-07-12 01:42:03 +00005046
5047
5048u16 hostapd_get_punct_bitmap(struct hostapd_data *hapd)
5049{
5050 u16 punct_bitmap = 0;
5051
5052#ifdef CONFIG_IEEE80211BE
5053 punct_bitmap = hapd->iconf->punct_bitmap;
5054#ifdef CONFIG_TESTING_OPTIONS
5055 if (!punct_bitmap)
5056 punct_bitmap = hapd->conf->eht_oper_puncturing_override;
5057#endif /* CONFIG_TESTING_OPTIONS */
5058#endif /* CONFIG_IEEE80211BE */
5059
5060 return punct_bitmap;
5061}