blob: 4bc6b3abcce5385ebe88a607f3555b5382be6449 [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;
Sunil Ravi79e6c4f2025-01-04 00:47:06 +0000245#ifdef CONFIG_IEEE80211BE
246 if (newconf->bss[i]->mld_ap != oldconf->bss[i]->mld_ap)
247 return 1;
248#endif /* CONFIG_IEEE80211BE */
Hai Shalom74f70d42019-02-11 14:42:39 -0800249 }
250
251 return 0;
252}
253
254
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700255int hostapd_reload_config(struct hostapd_iface *iface)
256{
Hai Shalom74f70d42019-02-11 14:42:39 -0800257 struct hapd_interfaces *interfaces = iface->interfaces;
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700258 struct hostapd_data *hapd = iface->bss[0];
259 struct hostapd_config *newconf, *oldconf;
260 size_t j;
261
262 if (iface->config_fname == NULL) {
263 /* Only in-memory config in use - assume it has been updated */
264 hostapd_clear_old(iface);
265 for (j = 0; j < iface->num_bss; j++)
266 hostapd_reload_bss(iface->bss[j]);
267 return 0;
268 }
269
270 if (iface->interfaces == NULL ||
271 iface->interfaces->config_read_cb == NULL)
272 return -1;
273 newconf = iface->interfaces->config_read_cb(iface->config_fname);
274 if (newconf == NULL)
275 return -1;
276
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700277 oldconf = hapd->iconf;
Hai Shalom74f70d42019-02-11 14:42:39 -0800278 if (hostapd_iface_conf_changed(newconf, oldconf)) {
279 char *fname;
280 int res;
281
Sunil Ravi77d572f2023-01-17 23:58:31 +0000282 hostapd_clear_old(iface);
283
Hai Shalom74f70d42019-02-11 14:42:39 -0800284 wpa_printf(MSG_DEBUG,
285 "Configuration changes include interface/BSS modification - force full disable+enable sequence");
286 fname = os_strdup(iface->config_fname);
287 if (!fname) {
288 hostapd_config_free(newconf);
289 return -1;
290 }
291 hostapd_remove_iface(interfaces, hapd->conf->iface);
292 iface = hostapd_init(interfaces, fname);
293 os_free(fname);
294 hostapd_config_free(newconf);
295 if (!iface) {
296 wpa_printf(MSG_ERROR,
297 "Failed to initialize interface on config reload");
298 return -1;
299 }
300 iface->interfaces = interfaces;
301 interfaces->iface[interfaces->count] = iface;
302 interfaces->count++;
303 res = hostapd_enable_iface(iface);
304 if (res < 0)
305 wpa_printf(MSG_ERROR,
306 "Failed to enable interface on config reload");
307 return res;
308 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700309
310 for (j = 0; j < iface->num_bss; j++) {
311 hapd = iface->bss[j];
Sunil Ravi77d572f2023-01-17 23:58:31 +0000312 if (!hapd->conf->config_id || !newconf->bss[j]->config_id ||
313 os_strcmp(hapd->conf->config_id,
314 newconf->bss[j]->config_id) != 0)
315 hostapd_clear_old_bss(hapd);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700316 hapd->iconf = newconf;
Dmitry Shmidtd11f0192014-03-24 12:09:47 -0700317 hapd->iconf->channel = oldconf->channel;
Dmitry Shmidtdda10c22015-03-24 16:05:01 -0700318 hapd->iconf->acs = oldconf->acs;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -0700319 hapd->iconf->secondary_channel = oldconf->secondary_channel;
Dmitry Shmidtd11f0192014-03-24 12:09:47 -0700320 hapd->iconf->ieee80211n = oldconf->ieee80211n;
321 hapd->iconf->ieee80211ac = oldconf->ieee80211ac;
322 hapd->iconf->ht_capab = oldconf->ht_capab;
323 hapd->iconf->vht_capab = oldconf->vht_capab;
Hai Shalom81f62d82019-07-22 12:10:00 -0700324 hostapd_set_oper_chwidth(hapd->iconf,
325 hostapd_get_oper_chwidth(oldconf));
326 hostapd_set_oper_centr_freq_seg0_idx(
327 hapd->iconf,
328 hostapd_get_oper_centr_freq_seg0_idx(oldconf));
329 hostapd_set_oper_centr_freq_seg1_idx(
330 hapd->iconf,
331 hostapd_get_oper_centr_freq_seg1_idx(oldconf));
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800332 hapd->conf = newconf->bss[j];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700333 hostapd_reload_bss(hapd);
334 }
335
Sunil Ravi79e6c4f2025-01-04 00:47:06 +0000336 iface->conf = newconf;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700337 hostapd_config_free(oldconf);
338
339
340 return 0;
341}
342
343
Hai Shalomfdcde762020-04-02 11:19:20 -0700344#ifdef CONFIG_WEP
345
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700346static void hostapd_broadcast_key_clear_iface(struct hostapd_data *hapd,
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700347 const char *ifname)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700348{
349 int i;
350
Dmitry Shmidt29333592017-01-09 12:27:11 -0800351 if (!ifname || !hapd->drv_priv)
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700352 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700353 for (i = 0; i < NUM_WEP_KEYS; i++) {
Hai Shalomfdcde762020-04-02 11:19:20 -0700354 if (hostapd_drv_set_key(ifname, hapd, WPA_ALG_NONE, NULL, i, 0,
355 0, NULL, 0, NULL, 0, KEY_FLAG_GROUP)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700356 wpa_printf(MSG_DEBUG, "Failed to clear default "
357 "encryption keys (ifname=%s keyidx=%d)",
358 ifname, i);
359 }
360 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700361 if (hapd->conf->ieee80211w) {
362 for (i = NUM_WEP_KEYS; i < NUM_WEP_KEYS + 2; i++) {
363 if (hostapd_drv_set_key(ifname, hapd, WPA_ALG_NONE,
Hai Shalomfdcde762020-04-02 11:19:20 -0700364 NULL, i, 0, 0, NULL,
365 0, NULL, 0, KEY_FLAG_GROUP)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700366 wpa_printf(MSG_DEBUG, "Failed to clear "
367 "default mgmt encryption keys "
368 "(ifname=%s keyidx=%d)", ifname, i);
369 }
370 }
371 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700372}
373
374
375static int hostapd_broadcast_wep_clear(struct hostapd_data *hapd)
376{
377 hostapd_broadcast_key_clear_iface(hapd, hapd->conf->iface);
378 return 0;
379}
380
381
382static int hostapd_broadcast_wep_set(struct hostapd_data *hapd)
383{
384 int errors = 0, idx;
385 struct hostapd_ssid *ssid = &hapd->conf->ssid;
386
387 idx = ssid->wep.idx;
Hai Shalomfdcde762020-04-02 11:19:20 -0700388 if (ssid->wep.default_len && ssid->wep.key[idx] &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700389 hostapd_drv_set_key(hapd->conf->iface,
Hai Shalomfdcde762020-04-02 11:19:20 -0700390 hapd, WPA_ALG_WEP, broadcast_ether_addr, idx, 0,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700391 1, NULL, 0, ssid->wep.key[idx],
Hai Shalomfdcde762020-04-02 11:19:20 -0700392 ssid->wep.len[idx],
393 KEY_FLAG_GROUP_RX_TX_DEFAULT)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700394 wpa_printf(MSG_WARNING, "Could not set WEP encryption.");
395 errors++;
396 }
397
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700398 return errors;
399}
400
Hai Shalomfdcde762020-04-02 11:19:20 -0700401#endif /* CONFIG_WEP */
402
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700403
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000404#ifdef CONFIG_IEEE80211BE
405#ifdef CONFIG_TESTING_OPTIONS
406
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000407static void hostapd_link_remove_timeout_handler(void *eloop_data,
408 void *user_ctx)
409{
410 struct hostapd_data *hapd = (struct hostapd_data *) eloop_data;
411
412 if (hapd->eht_mld_link_removal_count == 0)
413 return;
414 hapd->eht_mld_link_removal_count--;
415
416 wpa_printf(MSG_DEBUG, "MLD: Remove link_id=%u in %u beacons",
417 hapd->mld_link_id,
418 hapd->eht_mld_link_removal_count);
419
420 ieee802_11_set_beacon(hapd);
421
422 if (!hapd->eht_mld_link_removal_count) {
Sunil Ravi99c035e2024-07-12 01:42:03 +0000423 hostapd_free_link_stas(hapd);
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000424 hostapd_disable_iface(hapd->iface);
425 return;
426 }
427
428 eloop_register_timeout(0, TU_TO_USEC(hapd->iconf->beacon_int),
429 hostapd_link_remove_timeout_handler,
430 hapd, NULL);
431}
432
433
434int hostapd_link_remove(struct hostapd_data *hapd, u32 count)
435{
436 if (!hapd->conf->mld_ap)
437 return -1;
438
439 wpa_printf(MSG_DEBUG,
440 "MLD: Remove link_id=%u in %u beacons",
441 hapd->mld_link_id, count);
442
443 hapd->eht_mld_link_removal_count = count;
444 hapd->eht_mld_bss_param_change++;
Sunil Ravic0f5d412024-09-11 22:12:49 +0000445 if (hapd->eht_mld_bss_param_change == 255)
446 hapd->eht_mld_bss_param_change = 0;
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000447
448 eloop_register_timeout(0, TU_TO_USEC(hapd->iconf->beacon_int),
449 hostapd_link_remove_timeout_handler,
450 hapd, NULL);
451
452 ieee802_11_set_beacon(hapd);
453 return 0;
454}
455
456#endif /* CONFIG_TESTING_OPTIONS */
457#endif /* CONFIG_IEEE80211BE */
458
459
Hai Shalom60840252021-02-19 19:02:11 -0800460void hostapd_free_hapd_data(struct hostapd_data *hapd)
Dmitry Shmidt04949592012-07-19 12:16:46 -0700461{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800462 os_free(hapd->probereq_cb);
463 hapd->probereq_cb = NULL;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800464 hapd->num_probereq_cb = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800465
466#ifdef CONFIG_P2P
467 wpabuf_free(hapd->p2p_beacon_ie);
468 hapd->p2p_beacon_ie = NULL;
469 wpabuf_free(hapd->p2p_probe_resp_ie);
470 hapd->p2p_probe_resp_ie = NULL;
471#endif /* CONFIG_P2P */
472
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -0800473 if (!hapd->started) {
474 wpa_printf(MSG_ERROR, "%s: Interface %s wasn't started",
Hai Shalom021b0b52019-04-10 11:17:58 -0700475 __func__, hapd->conf ? hapd->conf->iface : "N/A");
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -0800476 return;
477 }
478 hapd->started = 0;
Hai Shalom5f92bc92019-04-18 11:54:11 -0700479 hapd->beacon_set_done = 0;
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -0800480
Dmitry Shmidt54605472013-11-08 11:10:19 -0800481 wpa_printf(MSG_DEBUG, "%s(%s)", __func__, hapd->conf->iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700482 accounting_deinit(hapd);
483 hostapd_deinit_wpa(hapd);
484 vlan_deinit(hapd);
485 hostapd_acl_deinit(hapd);
486#ifndef CONFIG_NO_RADIUS
Sunil Ravi99c035e2024-07-12 01:42:03 +0000487 if (hostapd_mld_is_first_bss(hapd)) {
488#ifdef CONFIG_IEEE80211BE
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000489 struct hapd_interfaces *ifaces = hapd->iface->interfaces;
490 size_t i;
491
492 for (i = 0; i < ifaces->count; i++) {
493 struct hostapd_iface *iface = ifaces->iface[i];
494 size_t j;
495
496 for (j = 0; iface && j < iface->num_bss; j++) {
497 struct hostapd_data *h = iface->bss[j];
498
499 if (hapd == h)
500 continue;
501 if (h->radius == hapd->radius)
502 h->radius = NULL;
503 if (h->radius_das == hapd->radius_das)
504 h->radius_das = NULL;
505 }
506 }
Sunil Ravi99c035e2024-07-12 01:42:03 +0000507#endif /* CONFIG_IEEE80211BE */
Sunil Ravi2a14cf12023-11-21 00:54:38 +0000508 radius_client_deinit(hapd->radius);
509 radius_das_deinit(hapd->radius_das);
510 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700511 hapd->radius = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700512 hapd->radius_das = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700513#endif /* CONFIG_NO_RADIUS */
514
515 hostapd_deinit_wps(hapd);
Hai Shalom81f62d82019-07-22 12:10:00 -0700516 ieee802_1x_dealloc_kay_sm_hapd(hapd);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700517#ifdef CONFIG_DPP
518 hostapd_dpp_deinit(hapd);
519 gas_query_ap_deinit(hapd->gas);
Hai Shaloma20dcd72022-02-04 13:43:00 -0800520 hapd->gas = NULL;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700521#endif /* CONFIG_DPP */
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000522#ifdef CONFIG_NAN_USD
523 hostapd_nan_usd_deinit(hapd);
524#endif /* CONFIG_NAN_USD */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700525
526 authsrv_deinit(hapd);
527
Sunil Ravi79e6c4f2025-01-04 00:47:06 +0000528 /* For single drv, first bss would have interface_added flag set.
529 * Don't remove interface now. Driver deinit part will take care
530 */
531 if (hapd->interface_added && hapd->iface->bss[0] != hapd) {
Dmitry Shmidt71757432014-06-02 13:50:35 -0700532 hapd->interface_added = 0;
533 if (hostapd_if_remove(hapd, WPA_IF_AP_BSS, hapd->conf->iface)) {
534 wpa_printf(MSG_WARNING,
535 "Failed to remove BSS interface %s",
536 hapd->conf->iface);
537 hapd->interface_added = 1;
538 } else {
539 /*
540 * Since this was a dynamically added interface, the
541 * driver wrapper may have removed its internal instance
542 * and hapd->drv_priv is not valid anymore.
543 */
Sunil Ravi99c035e2024-07-12 01:42:03 +0000544 hapd->drv_priv = NULL;
Dmitry Shmidt71757432014-06-02 13:50:35 -0700545 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700546 }
547
Sunil Ravi99c035e2024-07-12 01:42:03 +0000548#ifdef CONFIG_IEEE80211BE
549 /* If the interface was not added as well as it is not the first BSS,
550 * at least the link should be removed here since deinit will take care
551 * of only the first BSS. */
552 if (hapd->conf->mld_ap && !hapd->interface_added &&
553 hapd->iface->bss[0] != hapd)
554 hostapd_if_link_remove(hapd, WPA_IF_AP_BSS, hapd->conf->iface,
555 hapd->mld_link_id);
556#endif /* CONFIG_IEEE80211BE */
557
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800558 wpabuf_free(hapd->time_adv);
Hai Shaloma20dcd72022-02-04 13:43:00 -0800559 hapd->time_adv = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700560
561#ifdef CONFIG_INTERWORKING
562 gas_serv_deinit(hapd);
563#endif /* CONFIG_INTERWORKING */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800564
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800565 bss_load_update_deinit(hapd);
566 ndisc_snoop_deinit(hapd);
567 dhcp_snoop_deinit(hapd);
568 x_snoop_deinit(hapd);
569
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800570#ifdef CONFIG_SQLITE
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700571 bin_clear_free(hapd->tmp_eap_user.identity,
572 hapd->tmp_eap_user.identity_len);
573 bin_clear_free(hapd->tmp_eap_user.password,
574 hapd->tmp_eap_user.password_len);
Hai Shaloma20dcd72022-02-04 13:43:00 -0800575 os_memset(&hapd->tmp_eap_user, 0, sizeof(hapd->tmp_eap_user));
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800576#endif /* CONFIG_SQLITE */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800577
578#ifdef CONFIG_MESH
579 wpabuf_free(hapd->mesh_pending_auth);
580 hapd->mesh_pending_auth = NULL;
Hai Shalom60840252021-02-19 19:02:11 -0800581 /* handling setup failure is already done */
582 hapd->setup_complete_cb = NULL;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800583#endif /* CONFIG_MESH */
Dmitry Shmidt849734c2016-05-27 09:59:01 -0700584
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000585#ifndef CONFIG_NO_RRM
Dmitry Shmidt849734c2016-05-27 09:59:01 -0700586 hostapd_clean_rrm(hapd);
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000587#endif /* CONFIG_NO_RRM */
Dmitry Shmidtebd93af2017-02-21 13:40:44 -0800588 fils_hlp_deinit(hapd);
Hai Shalom021b0b52019-04-10 11:17:58 -0700589
Hai Shalom899fcc72020-10-19 14:38:18 -0700590#ifdef CONFIG_OCV
591 eloop_cancel_timeout(hostapd_ocv_check_csa_sa_query, hapd, NULL);
592#endif /* CONFIG_OCV */
593
Hai Shalom021b0b52019-04-10 11:17:58 -0700594#ifdef CONFIG_SAE
595 {
596 struct hostapd_sae_commit_queue *q;
597
598 while ((q = dl_list_first(&hapd->sae_commit_queue,
599 struct hostapd_sae_commit_queue,
600 list))) {
601 dl_list_del(&q->list);
602 os_free(q);
603 }
604 }
605 eloop_cancel_timeout(auth_sae_process_commit, hapd, NULL);
606#endif /* CONFIG_SAE */
Sunil Ravia04bd252022-05-02 22:54:18 -0700607
608#ifdef CONFIG_IEEE80211AX
609 eloop_cancel_timeout(hostapd_switch_color_timeout_handler, hapd, NULL);
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000610#ifdef CONFIG_TESTING_OPTIONS
611#ifdef CONFIG_IEEE80211BE
612 eloop_cancel_timeout(hostapd_link_remove_timeout_handler, hapd, NULL);
613#endif /* CONFIG_IEEE80211BE */
614#endif /* CONFIG_TESTING_OPTIONS */
615
Sunil Ravia04bd252022-05-02 22:54:18 -0700616#endif /* CONFIG_IEEE80211AX */
Dmitry Shmidt04949592012-07-19 12:16:46 -0700617}
618
619
Sunil Ravi99c035e2024-07-12 01:42:03 +0000620/* hostapd_bss_link_deinit - Per-BSS ML cleanup (deinitialization)
621 * @hapd: Pointer to BSS data
622 *
623 * This function is used to unlink the BSS from the AP MLD.
624 * If the BSS being removed is the first link, the next link becomes the first
625 * link.
626 */
627static void hostapd_bss_link_deinit(struct hostapd_data *hapd)
628{
629#ifdef CONFIG_IEEE80211BE
Sunil Ravic0f5d412024-09-11 22:12:49 +0000630 int i;
631
Sunil Ravi99c035e2024-07-12 01:42:03 +0000632 if (!hapd->conf || !hapd->conf->mld_ap)
633 return;
634
Sunil Ravic0f5d412024-09-11 22:12:49 +0000635 /* Free per STA profiles */
636 for (i = 0; i < MAX_NUM_MLD_LINKS; i++) {
637 os_free(hapd->partner_links[i].resp_sta_profile);
638 os_memset(&hapd->partner_links[i], 0,
639 sizeof(hapd->partner_links[i]));
640 }
641
642 /* Put all freeing logic above this */
Sunil Ravi79e6c4f2025-01-04 00:47:06 +0000643 if (!hapd->mld || !hapd->mld->num_links)
Sunil Ravi99c035e2024-07-12 01:42:03 +0000644 return;
645
646 /* If not started, not yet linked to the MLD. However, the first
647 * BSS is always linked since it is linked during driver_init(), and
648 * hence, need to remove it from the AP MLD.
649 */
650 if (!hapd->started && hapd->iface->bss[0] != hapd)
651 return;
652
653 /* The first BSS can also be only linked when at least driver_init() is
654 * executed. But if previous interface fails, it is not, and hence,
655 * safe to skip.
656 */
657 if (hapd->iface->bss[0] == hapd && !hapd->drv_priv)
658 return;
659
660 hostapd_mld_remove_link(hapd);
661#endif /* CONFIG_IEEE80211BE */
662}
663
664
Dmitry Shmidt04949592012-07-19 12:16:46 -0700665/**
666 * hostapd_cleanup - Per-BSS cleanup (deinitialization)
667 * @hapd: Pointer to BSS data
668 *
669 * This function is used to free all per-BSS data structures and resources.
Dmitry Shmidt54605472013-11-08 11:10:19 -0800670 * Most of the modules that are initialized in hostapd_setup_bss() are
671 * deinitialized here.
Dmitry Shmidt04949592012-07-19 12:16:46 -0700672 */
673static void hostapd_cleanup(struct hostapd_data *hapd)
674{
Dmitry Shmidt54605472013-11-08 11:10:19 -0800675 wpa_printf(MSG_DEBUG, "%s(hapd=%p (%s))", __func__, hapd,
Hai Shalom021b0b52019-04-10 11:17:58 -0700676 hapd->conf ? hapd->conf->iface : "N/A");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700677 if (hapd->iface->interfaces &&
Dmitry Shmidt29333592017-01-09 12:27:11 -0800678 hapd->iface->interfaces->ctrl_iface_deinit) {
679 wpa_msg(hapd->msg_ctx, MSG_INFO, WPA_EVENT_TERMINATING);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700680 hapd->iface->interfaces->ctrl_iface_deinit(hapd);
Dmitry Shmidt29333592017-01-09 12:27:11 -0800681 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700682 hostapd_free_hapd_data(hapd);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700683}
684
685
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800686static void sta_track_deinit(struct hostapd_iface *iface)
687{
688 struct hostapd_sta_info *info;
689
690 if (!iface->num_sta_seen)
691 return;
692
693 while ((info = dl_list_first(&iface->sta_seen, struct hostapd_sta_info,
694 list))) {
695 dl_list_del(&info->list);
696 iface->num_sta_seen--;
Dmitry Shmidtaca489e2016-09-28 15:44:14 -0700697 sta_track_del(info);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800698 }
699}
700
701
Hai Shalom60840252021-02-19 19:02:11 -0800702void hostapd_cleanup_iface_partial(struct hostapd_iface *iface)
Dmitry Shmidt04949592012-07-19 12:16:46 -0700703{
Dmitry Shmidt54605472013-11-08 11:10:19 -0800704 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
Sunil Ravi99c035e2024-07-12 01:42:03 +0000705 eloop_cancel_timeout(channel_list_update_timeout, iface, NULL);
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800706#ifdef NEED_AP_MLME
707 hostapd_stop_setup_timers(iface);
708#endif /* NEED_AP_MLME */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700709 if (iface->current_mode)
710 acs_cleanup(iface);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700711 hostapd_free_hw_features(iface->hw_features, iface->num_hw_features);
712 iface->hw_features = NULL;
Sunil Ravi79e6c4f2025-01-04 00:47:06 +0000713 iface->num_hw_features = 0;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700714 iface->current_mode = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700715 os_free(iface->current_rates);
716 iface->current_rates = NULL;
717 os_free(iface->basic_rates);
718 iface->basic_rates = NULL;
Sunil Ravi77d572f2023-01-17 23:58:31 +0000719 iface->cac_started = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700720 ap_list_deinit(iface);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800721 sta_track_deinit(iface);
Hai Shalom81f62d82019-07-22 12:10:00 -0700722 airtime_policy_update_deinit(iface);
Sunil Ravic0f5d412024-09-11 22:12:49 +0000723 hostapd_free_multi_hw_info(iface->multi_hw_info);
724 iface->multi_hw_info = NULL;
725 iface->current_hw_info = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700726}
727
728
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700729/**
730 * hostapd_cleanup_iface - Complete per-interface cleanup
731 * @iface: Pointer to interface data
732 *
733 * This function is called after per-BSS data structures are deinitialized
734 * with hostapd_cleanup().
735 */
736static void hostapd_cleanup_iface(struct hostapd_iface *iface)
737{
Dmitry Shmidt54605472013-11-08 11:10:19 -0800738 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
Roshan Pius3a1667e2018-07-03 15:17:14 -0700739 eloop_cancel_timeout(hostapd_interface_setup_failure_handler, iface,
740 NULL);
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800741
Dmitry Shmidt04949592012-07-19 12:16:46 -0700742 hostapd_cleanup_iface_partial(iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700743 hostapd_config_free(iface->conf);
744 iface->conf = NULL;
745
746 os_free(iface->config_fname);
747 os_free(iface->bss);
Dmitry Shmidt54605472013-11-08 11:10:19 -0800748 wpa_printf(MSG_DEBUG, "%s: free iface=%p", __func__, iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700749 os_free(iface);
750}
751
752
Hai Shalomfdcde762020-04-02 11:19:20 -0700753#ifdef CONFIG_WEP
754
Dmitry Shmidt04949592012-07-19 12:16:46 -0700755static void hostapd_clear_wep(struct hostapd_data *hapd)
756{
Hai Shalom021b0b52019-04-10 11:17:58 -0700757 if (hapd->drv_priv && !hapd->iface->driver_ap_teardown && hapd->conf) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700758 hostapd_set_privacy(hapd, 0);
759 hostapd_broadcast_wep_clear(hapd);
760 }
761}
762
763
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700764static int hostapd_setup_encryption(char *iface, struct hostapd_data *hapd)
765{
766 int i;
767
768 hostapd_broadcast_wep_set(hapd);
769
770 if (hapd->conf->ssid.wep.default_len) {
771 hostapd_set_privacy(hapd, 1);
772 return 0;
773 }
774
Jouni Malinen75ecf522011-06-27 15:19:46 -0700775 /*
776 * When IEEE 802.1X is not enabled, the driver may need to know how to
777 * set authentication algorithms for static WEP.
778 */
779 hostapd_drv_set_authmode(hapd, hapd->conf->auth_algs);
780
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700781 for (i = 0; i < 4; i++) {
782 if (hapd->conf->ssid.wep.key[i] &&
Hai Shalomfdcde762020-04-02 11:19:20 -0700783 hostapd_drv_set_key(iface, hapd, WPA_ALG_WEP, NULL, i, 0,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700784 i == hapd->conf->ssid.wep.idx, NULL, 0,
785 hapd->conf->ssid.wep.key[i],
Hai Shalomfdcde762020-04-02 11:19:20 -0700786 hapd->conf->ssid.wep.len[i],
787 i == hapd->conf->ssid.wep.idx ?
788 KEY_FLAG_GROUP_RX_TX_DEFAULT :
789 KEY_FLAG_GROUP_RX_TX)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700790 wpa_printf(MSG_WARNING, "Could not set WEP "
791 "encryption.");
792 return -1;
793 }
794 if (hapd->conf->ssid.wep.key[i] &&
795 i == hapd->conf->ssid.wep.idx)
796 hostapd_set_privacy(hapd, 1);
797 }
798
799 return 0;
800}
801
Hai Shalomfdcde762020-04-02 11:19:20 -0700802#endif /* CONFIG_WEP */
803
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700804
Dmitry Shmidt04949592012-07-19 12:16:46 -0700805static int hostapd_flush_old_stations(struct hostapd_data *hapd, u16 reason)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700806{
807 int ret = 0;
808 u8 addr[ETH_ALEN];
809
810 if (hostapd_drv_none(hapd) || hapd->drv_priv == NULL)
811 return 0;
812
Dmitry Shmidta38abf92014-03-06 13:38:44 -0800813 if (!hapd->iface->driver_ap_teardown) {
814 wpa_dbg(hapd->msg_ctx, MSG_DEBUG,
815 "Flushing old station entries");
816
817 if (hostapd_flush(hapd)) {
818 wpa_msg(hapd->msg_ctx, MSG_WARNING,
819 "Could not connect to kernel driver");
820 ret = -1;
821 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700822 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700823 if (hapd->conf && hapd->conf->broadcast_deauth) {
824 wpa_dbg(hapd->msg_ctx, MSG_DEBUG,
825 "Deauthenticate all stations");
826 os_memset(addr, 0xff, ETH_ALEN);
827 hostapd_drv_sta_deauth(hapd, addr, reason);
828 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700829 hostapd_free_stas(hapd);
830
831 return ret;
832}
833
834
Hai Shalom60840252021-02-19 19:02:11 -0800835void hostapd_bss_deinit_no_free(struct hostapd_data *hapd)
Dmitry Shmidt71757432014-06-02 13:50:35 -0700836{
837 hostapd_free_stas(hapd);
838 hostapd_flush_old_stations(hapd, WLAN_REASON_DEAUTH_LEAVING);
Hai Shalomfdcde762020-04-02 11:19:20 -0700839#ifdef CONFIG_WEP
Dmitry Shmidt71757432014-06-02 13:50:35 -0700840 hostapd_clear_wep(hapd);
Hai Shalomfdcde762020-04-02 11:19:20 -0700841#endif /* CONFIG_WEP */
Dmitry Shmidt71757432014-06-02 13:50:35 -0700842}
843
844
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700845/**
846 * hostapd_validate_bssid_configuration - Validate BSSID configuration
847 * @iface: Pointer to interface data
848 * Returns: 0 on success, -1 on failure
849 *
850 * This function is used to validate that the configured BSSIDs are valid.
851 */
852static int hostapd_validate_bssid_configuration(struct hostapd_iface *iface)
853{
854 u8 mask[ETH_ALEN] = { 0 };
855 struct hostapd_data *hapd = iface->bss[0];
856 unsigned int i = iface->conf->num_bss, bits = 0, j;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700857 int auto_addr = 0;
858
859 if (hostapd_drv_none(hapd))
860 return 0;
861
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -0800862 if (iface->conf->use_driver_iface_addr)
863 return 0;
864
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700865 /* Generate BSSID mask that is large enough to cover the BSSIDs. */
866
867 /* Determine the bits necessary to cover the number of BSSIDs. */
868 for (i--; i; i >>= 1)
869 bits++;
870
871 /* Determine the bits necessary to any configured BSSIDs,
872 if they are higher than the number of BSSIDs. */
873 for (j = 0; j < iface->conf->num_bss; j++) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800874 if (is_zero_ether_addr(iface->conf->bss[j]->bssid)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700875 if (j)
876 auto_addr++;
877 continue;
878 }
879
880 for (i = 0; i < ETH_ALEN; i++) {
881 mask[i] |=
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800882 iface->conf->bss[j]->bssid[i] ^
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700883 hapd->own_addr[i];
884 }
885 }
886
887 if (!auto_addr)
888 goto skip_mask_ext;
889
890 for (i = 0; i < ETH_ALEN && mask[i] == 0; i++)
891 ;
892 j = 0;
893 if (i < ETH_ALEN) {
894 j = (5 - i) * 8;
895
896 while (mask[i] != 0) {
897 mask[i] >>= 1;
898 j++;
899 }
900 }
901
902 if (bits < j)
903 bits = j;
904
905 if (bits > 40) {
906 wpa_printf(MSG_ERROR, "Too many bits in the BSSID mask (%u)",
907 bits);
908 return -1;
909 }
910
911 os_memset(mask, 0xff, ETH_ALEN);
912 j = bits / 8;
913 for (i = 5; i > 5 - j; i--)
914 mask[i] = 0;
915 j = bits % 8;
Hai Shalom021b0b52019-04-10 11:17:58 -0700916 while (j) {
917 j--;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700918 mask[i] <<= 1;
Hai Shalom021b0b52019-04-10 11:17:58 -0700919 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700920
921skip_mask_ext:
922 wpa_printf(MSG_DEBUG, "BSS count %lu, BSSID mask " MACSTR " (%d bits)",
923 (unsigned long) iface->conf->num_bss, MAC2STR(mask), bits);
924
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700925 if (!auto_addr)
926 return 0;
927
928 for (i = 0; i < ETH_ALEN; i++) {
929 if ((hapd->own_addr[i] & mask[i]) != hapd->own_addr[i]) {
930 wpa_printf(MSG_ERROR, "Invalid BSSID mask " MACSTR
931 " for start address " MACSTR ".",
932 MAC2STR(mask), MAC2STR(hapd->own_addr));
933 wpa_printf(MSG_ERROR, "Start address must be the "
934 "first address in the block (i.e., addr "
935 "AND mask == addr).");
936 return -1;
937 }
938 }
939
940 return 0;
941}
942
943
944static int mac_in_conf(struct hostapd_config *conf, const void *a)
945{
946 size_t i;
947
948 for (i = 0; i < conf->num_bss; i++) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800949 if (hostapd_mac_comp(conf->bss[i]->bssid, a) == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700950 return 1;
951 }
952 }
953
954 return 0;
955}
956
957
Dmitry Shmidt04949592012-07-19 12:16:46 -0700958#ifndef CONFIG_NO_RADIUS
959
960static int hostapd_das_nas_mismatch(struct hostapd_data *hapd,
961 struct radius_das_attrs *attr)
962{
Dmitry Shmidt13ca8d82014-02-20 10:18:40 -0800963 if (attr->nas_identifier &&
964 (!hapd->conf->nas_identifier ||
965 os_strlen(hapd->conf->nas_identifier) !=
966 attr->nas_identifier_len ||
967 os_memcmp(hapd->conf->nas_identifier, attr->nas_identifier,
968 attr->nas_identifier_len) != 0)) {
969 wpa_printf(MSG_DEBUG, "RADIUS DAS: NAS-Identifier mismatch");
970 return 1;
971 }
972
973 if (attr->nas_ip_addr &&
974 (hapd->conf->own_ip_addr.af != AF_INET ||
975 os_memcmp(&hapd->conf->own_ip_addr.u.v4, attr->nas_ip_addr, 4) !=
976 0)) {
977 wpa_printf(MSG_DEBUG, "RADIUS DAS: NAS-IP-Address mismatch");
978 return 1;
979 }
980
981#ifdef CONFIG_IPV6
982 if (attr->nas_ipv6_addr &&
983 (hapd->conf->own_ip_addr.af != AF_INET6 ||
984 os_memcmp(&hapd->conf->own_ip_addr.u.v6, attr->nas_ipv6_addr, 16)
985 != 0)) {
986 wpa_printf(MSG_DEBUG, "RADIUS DAS: NAS-IPv6-Address mismatch");
987 return 1;
988 }
989#endif /* CONFIG_IPV6 */
990
Dmitry Shmidt04949592012-07-19 12:16:46 -0700991 return 0;
992}
993
994
995static struct sta_info * hostapd_das_find_sta(struct hostapd_data *hapd,
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800996 struct radius_das_attrs *attr,
997 int *multi)
Dmitry Shmidt04949592012-07-19 12:16:46 -0700998{
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800999 struct sta_info *selected, *sta;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001000 char buf[128];
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001001 int num_attr = 0;
1002 int count;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001003
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001004 *multi = 0;
1005
1006 for (sta = hapd->sta_list; sta; sta = sta->next)
1007 sta->radius_das_match = 1;
1008
1009 if (attr->sta_addr) {
1010 num_attr++;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001011 sta = ap_get_sta(hapd, attr->sta_addr);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001012 if (!sta) {
1013 wpa_printf(MSG_DEBUG,
1014 "RADIUS DAS: No Calling-Station-Id match");
1015 return NULL;
1016 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001017
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001018 selected = sta;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001019 for (sta = hapd->sta_list; sta; sta = sta->next) {
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001020 if (sta != selected)
1021 sta->radius_das_match = 0;
1022 }
1023 wpa_printf(MSG_DEBUG, "RADIUS DAS: Calling-Station-Id match");
1024 }
1025
1026 if (attr->acct_session_id) {
1027 num_attr++;
Dmitry Shmidtb97e4282016-02-08 10:16:07 -08001028 if (attr->acct_session_id_len != 16) {
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001029 wpa_printf(MSG_DEBUG,
1030 "RADIUS DAS: Acct-Session-Id cannot match");
1031 return NULL;
1032 }
1033 count = 0;
1034
1035 for (sta = hapd->sta_list; sta; sta = sta->next) {
1036 if (!sta->radius_das_match)
1037 continue;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001038 os_snprintf(buf, sizeof(buf), "%016llX",
1039 (unsigned long long) sta->acct_session_id);
Dmitry Shmidtb97e4282016-02-08 10:16:07 -08001040 if (os_memcmp(attr->acct_session_id, buf, 16) != 0)
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001041 sta->radius_das_match = 0;
1042 else
1043 count++;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001044 }
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001045
1046 if (count == 0) {
1047 wpa_printf(MSG_DEBUG,
1048 "RADIUS DAS: No matches remaining after Acct-Session-Id check");
1049 return NULL;
1050 }
1051 wpa_printf(MSG_DEBUG, "RADIUS DAS: Acct-Session-Id match");
Dmitry Shmidt04949592012-07-19 12:16:46 -07001052 }
1053
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001054 if (attr->acct_multi_session_id) {
1055 num_attr++;
Dmitry Shmidtb97e4282016-02-08 10:16:07 -08001056 if (attr->acct_multi_session_id_len != 16) {
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001057 wpa_printf(MSG_DEBUG,
1058 "RADIUS DAS: Acct-Multi-Session-Id cannot match");
1059 return NULL;
1060 }
1061 count = 0;
1062
1063 for (sta = hapd->sta_list; sta; sta = sta->next) {
1064 if (!sta->radius_das_match)
1065 continue;
1066 if (!sta->eapol_sm ||
Dmitry Shmidtb97e4282016-02-08 10:16:07 -08001067 !sta->eapol_sm->acct_multi_session_id) {
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001068 sta->radius_das_match = 0;
1069 continue;
1070 }
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001071 os_snprintf(buf, sizeof(buf), "%016llX",
1072 (unsigned long long)
Dmitry Shmidtb97e4282016-02-08 10:16:07 -08001073 sta->eapol_sm->acct_multi_session_id);
1074 if (os_memcmp(attr->acct_multi_session_id, buf, 16) !=
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001075 0)
1076 sta->radius_das_match = 0;
1077 else
1078 count++;
1079 }
1080
1081 if (count == 0) {
1082 wpa_printf(MSG_DEBUG,
1083 "RADIUS DAS: No matches remaining after Acct-Multi-Session-Id check");
1084 return NULL;
1085 }
1086 wpa_printf(MSG_DEBUG,
1087 "RADIUS DAS: Acct-Multi-Session-Id match");
1088 }
1089
1090 if (attr->cui) {
1091 num_attr++;
1092 count = 0;
1093
Dmitry Shmidt04949592012-07-19 12:16:46 -07001094 for (sta = hapd->sta_list; sta; sta = sta->next) {
1095 struct wpabuf *cui;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001096
1097 if (!sta->radius_das_match)
1098 continue;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001099 cui = ieee802_1x_get_radius_cui(sta->eapol_sm);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001100 if (!cui || wpabuf_len(cui) != attr->cui_len ||
Dmitry Shmidt04949592012-07-19 12:16:46 -07001101 os_memcmp(wpabuf_head(cui), attr->cui,
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001102 attr->cui_len) != 0)
1103 sta->radius_das_match = 0;
1104 else
1105 count++;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001106 }
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001107
1108 if (count == 0) {
1109 wpa_printf(MSG_DEBUG,
1110 "RADIUS DAS: No matches remaining after Chargeable-User-Identity check");
1111 return NULL;
1112 }
1113 wpa_printf(MSG_DEBUG,
1114 "RADIUS DAS: Chargeable-User-Identity match");
Dmitry Shmidt04949592012-07-19 12:16:46 -07001115 }
1116
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001117 if (attr->user_name) {
1118 num_attr++;
1119 count = 0;
1120
Dmitry Shmidt04949592012-07-19 12:16:46 -07001121 for (sta = hapd->sta_list; sta; sta = sta->next) {
1122 u8 *identity;
1123 size_t identity_len;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001124
1125 if (!sta->radius_das_match)
1126 continue;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001127 identity = ieee802_1x_get_identity(sta->eapol_sm,
1128 &identity_len);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001129 if (!identity ||
1130 identity_len != attr->user_name_len ||
Dmitry Shmidt04949592012-07-19 12:16:46 -07001131 os_memcmp(identity, attr->user_name, identity_len)
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001132 != 0)
1133 sta->radius_das_match = 0;
1134 else
1135 count++;
1136 }
1137
1138 if (count == 0) {
1139 wpa_printf(MSG_DEBUG,
1140 "RADIUS DAS: No matches remaining after User-Name check");
1141 return NULL;
1142 }
1143 wpa_printf(MSG_DEBUG,
1144 "RADIUS DAS: User-Name match");
1145 }
1146
1147 if (num_attr == 0) {
1148 /*
1149 * In theory, we could match all current associations, but it
1150 * seems safer to just reject requests that do not include any
1151 * session identification attributes.
1152 */
1153 wpa_printf(MSG_DEBUG,
1154 "RADIUS DAS: No session identification attributes included");
1155 return NULL;
1156 }
1157
1158 selected = NULL;
1159 for (sta = hapd->sta_list; sta; sta = sta->next) {
1160 if (sta->radius_das_match) {
1161 if (selected) {
1162 *multi = 1;
1163 return NULL;
1164 }
1165 selected = sta;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001166 }
1167 }
1168
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001169 return selected;
1170}
1171
1172
1173static int hostapd_das_disconnect_pmksa(struct hostapd_data *hapd,
1174 struct radius_das_attrs *attr)
1175{
1176 if (!hapd->wpa_auth)
1177 return -1;
1178 return wpa_auth_radius_das_disconnect_pmksa(hapd->wpa_auth, attr);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001179}
1180
1181
1182static enum radius_das_res
1183hostapd_das_disconnect(void *ctx, struct radius_das_attrs *attr)
1184{
1185 struct hostapd_data *hapd = ctx;
1186 struct sta_info *sta;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001187 int multi;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001188
1189 if (hostapd_das_nas_mismatch(hapd, attr))
1190 return RADIUS_DAS_NAS_MISMATCH;
1191
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001192 sta = hostapd_das_find_sta(hapd, attr, &multi);
1193 if (sta == NULL) {
1194 if (multi) {
1195 wpa_printf(MSG_DEBUG,
1196 "RADIUS DAS: Multiple sessions match - not supported");
1197 return RADIUS_DAS_MULTI_SESSION_MATCH;
1198 }
1199 if (hostapd_das_disconnect_pmksa(hapd, attr) == 0) {
1200 wpa_printf(MSG_DEBUG,
1201 "RADIUS DAS: PMKSA cache entry matched");
1202 return RADIUS_DAS_SUCCESS;
1203 }
1204 wpa_printf(MSG_DEBUG, "RADIUS DAS: No matching session found");
Dmitry Shmidt04949592012-07-19 12:16:46 -07001205 return RADIUS_DAS_SESSION_NOT_FOUND;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001206 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001207
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001208 wpa_printf(MSG_DEBUG, "RADIUS DAS: Found a matching session " MACSTR
1209 " - disconnecting", MAC2STR(sta->addr));
Dmitry Shmidt13ca8d82014-02-20 10:18:40 -08001210 wpa_auth_pmksa_remove(hapd->wpa_auth, sta->addr);
1211
Dmitry Shmidt04949592012-07-19 12:16:46 -07001212 hostapd_drv_sta_deauth(hapd, sta->addr,
1213 WLAN_REASON_PREV_AUTH_NOT_VALID);
1214 ap_sta_deauthenticate(hapd, sta, WLAN_REASON_PREV_AUTH_NOT_VALID);
1215
1216 return RADIUS_DAS_SUCCESS;
1217}
1218
Roshan Pius3a1667e2018-07-03 15:17:14 -07001219
1220#ifdef CONFIG_HS20
1221static enum radius_das_res
1222hostapd_das_coa(void *ctx, struct radius_das_attrs *attr)
1223{
1224 struct hostapd_data *hapd = ctx;
1225 struct sta_info *sta;
1226 int multi;
1227
1228 if (hostapd_das_nas_mismatch(hapd, attr))
1229 return RADIUS_DAS_NAS_MISMATCH;
1230
1231 sta = hostapd_das_find_sta(hapd, attr, &multi);
1232 if (!sta) {
1233 if (multi) {
1234 wpa_printf(MSG_DEBUG,
1235 "RADIUS DAS: Multiple sessions match - not supported");
1236 return RADIUS_DAS_MULTI_SESSION_MATCH;
1237 }
1238 wpa_printf(MSG_DEBUG, "RADIUS DAS: No matching session found");
1239 return RADIUS_DAS_SESSION_NOT_FOUND;
1240 }
1241
1242 wpa_printf(MSG_DEBUG, "RADIUS DAS: Found a matching session " MACSTR
1243 " - CoA", MAC2STR(sta->addr));
1244
1245 if (attr->hs20_t_c_filtering) {
1246 if (attr->hs20_t_c_filtering[0] & BIT(0)) {
1247 wpa_printf(MSG_DEBUG,
1248 "HS 2.0: Unexpected Terms and Conditions filtering required in CoA-Request");
1249 return RADIUS_DAS_COA_FAILED;
1250 }
1251
1252 hs20_t_c_filtering(hapd, sta, 0);
1253 }
1254
1255 return RADIUS_DAS_SUCCESS;
1256}
1257#else /* CONFIG_HS20 */
1258#define hostapd_das_coa NULL
1259#endif /* CONFIG_HS20 */
1260
Hai Shalomc3565922019-10-28 11:58:20 -07001261
1262#ifdef CONFIG_SQLITE
1263
1264static int db_table_exists(sqlite3 *db, const char *name)
1265{
1266 char cmd[128];
1267
1268 os_snprintf(cmd, sizeof(cmd), "SELECT 1 FROM %s;", name);
1269 return sqlite3_exec(db, cmd, NULL, NULL, NULL) == SQLITE_OK;
1270}
1271
1272
1273static int db_table_create_radius_attributes(sqlite3 *db)
1274{
1275 char *err = NULL;
1276 const char *sql =
1277 "CREATE TABLE radius_attributes("
1278 " id INTEGER PRIMARY KEY,"
1279 " sta TEXT,"
1280 " reqtype TEXT,"
1281 " attr TEXT"
1282 ");"
1283 "CREATE INDEX idx_sta_reqtype ON radius_attributes(sta,reqtype);";
1284
1285 wpa_printf(MSG_DEBUG,
1286 "Adding database table for RADIUS attribute information");
1287 if (sqlite3_exec(db, sql, NULL, NULL, &err) != SQLITE_OK) {
1288 wpa_printf(MSG_ERROR, "SQLite error: %s", err);
1289 sqlite3_free(err);
1290 return -1;
1291 }
1292
1293 return 0;
1294}
1295
1296#endif /* CONFIG_SQLITE */
1297
Dmitry Shmidt04949592012-07-19 12:16:46 -07001298#endif /* CONFIG_NO_RADIUS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001299
1300
Sunil Ravi77d572f2023-01-17 23:58:31 +00001301static int hostapd_start_beacon(struct hostapd_data *hapd,
1302 bool flush_old_stations)
1303{
1304 struct hostapd_bss_config *conf = hapd->conf;
1305
1306 if (!conf->start_disabled && ieee802_11_set_beacon(hapd) < 0)
1307 return -1;
1308
1309 if (flush_old_stations && !conf->start_disabled &&
1310 conf->broadcast_deauth) {
1311 u8 addr[ETH_ALEN];
1312
1313 /* Should any previously associated STA not have noticed that
1314 * the AP had stopped and restarted, send one more
1315 * deauthentication notification now that the AP is ready to
1316 * operate. */
1317 wpa_dbg(hapd->msg_ctx, MSG_DEBUG,
1318 "Deauthenticate all stations at BSS start");
1319 os_memset(addr, 0xff, ETH_ALEN);
1320 hostapd_drv_sta_deauth(hapd, addr,
1321 WLAN_REASON_PREV_AUTH_NOT_VALID);
1322 }
1323
1324 if (hapd->driver && hapd->driver->set_operstate)
1325 hapd->driver->set_operstate(hapd->drv_priv, 1);
1326
1327 return 0;
1328}
1329
1330
Sunil Ravi7f769292024-07-23 22:21:32 +00001331#ifndef CONFIG_NO_RADIUS
1332static int hostapd_bss_radius_init(struct hostapd_data *hapd)
1333{
1334 struct hostapd_bss_config *conf;
1335
1336 if (!hapd)
1337 return -1;
1338
1339 conf = hapd->conf;
1340
1341 if (hapd->radius) {
1342 wpa_printf(MSG_DEBUG,
1343 "Skipping RADIUS client init (already done)");
1344 return 0;
1345 }
1346
1347 hapd->radius = radius_client_init(hapd, conf->radius);
1348 if (!hapd->radius) {
1349 wpa_printf(MSG_ERROR,
1350 "RADIUS client initialization failed.");
1351 return -1;
1352 }
1353
1354 if (conf->radius_das_port) {
1355 struct radius_das_conf das_conf;
1356
1357 os_memset(&das_conf, 0, sizeof(das_conf));
1358 das_conf.port = conf->radius_das_port;
1359 das_conf.shared_secret = conf->radius_das_shared_secret;
1360 das_conf.shared_secret_len =
1361 conf->radius_das_shared_secret_len;
1362 das_conf.client_addr = &conf->radius_das_client_addr;
1363 das_conf.time_window = conf->radius_das_time_window;
1364 das_conf.require_event_timestamp =
1365 conf->radius_das_require_event_timestamp;
1366 das_conf.require_message_authenticator =
1367 conf->radius_das_require_message_authenticator;
1368 das_conf.ctx = hapd;
1369 das_conf.disconnect = hostapd_das_disconnect;
1370 das_conf.coa = hostapd_das_coa;
1371 hapd->radius_das = radius_das_init(&das_conf);
1372 if (!hapd->radius_das) {
1373 wpa_printf(MSG_ERROR,
1374 "RADIUS DAS initialization failed.");
1375 return -1;
1376 }
1377 }
1378
1379 return 0;
1380}
1381#endif /* CONFIG_NO_RADIUS */
1382
1383
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001384/**
1385 * hostapd_setup_bss - Per-BSS setup (initialization)
1386 * @hapd: Pointer to BSS data
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001387 * @first: Whether this BSS is the first BSS of an interface; -1 = not first,
1388 * but interface may exist
Sunil Ravi77d572f2023-01-17 23:58:31 +00001389 * @start_beacon: Whether Beacon frame template should be configured and
1390 * transmission of Beaconf rames started at this time. This is used when
1391 * MBSSID element is enabled where the information regarding all BSSes
1392 * should be retrieved before configuring the Beacon frame template. The
1393 * calling functions are responsible for configuring the Beacon frame
1394 * explicitly if this is set to false.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001395 *
1396 * This function is used to initialize all per-BSS data structures and
1397 * resources. This gets called in a loop for each BSS when an interface is
1398 * initialized. Most of the modules that are initialized here will be
1399 * deinitialized in hostapd_cleanup().
1400 */
Sunil Ravi77d572f2023-01-17 23:58:31 +00001401static int hostapd_setup_bss(struct hostapd_data *hapd, int first,
1402 bool start_beacon)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001403{
1404 struct hostapd_bss_config *conf = hapd->conf;
Dmitry Shmidt9d9e6022015-04-23 10:34:55 -07001405 u8 ssid[SSID_MAX_LEN + 1];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001406 int ssid_len, set_ssid;
1407 char force_ifname[IFNAMSIZ];
1408 u8 if_addr[ETH_ALEN];
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001409 int flush_old_stations = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001410
Sunil Ravi99c035e2024-07-12 01:42:03 +00001411 if (!hostapd_mld_is_first_bss(hapd))
Sunil Ravi2a14cf12023-11-21 00:54:38 +00001412 wpa_printf(MSG_DEBUG,
1413 "MLD: %s: Setting non-first BSS", __func__);
1414
Dmitry Shmidt54605472013-11-08 11:10:19 -08001415 wpa_printf(MSG_DEBUG, "%s(hapd=%p (%s), first=%d)",
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001416 __func__, hapd, conf->iface, first);
Dmitry Shmidt54605472013-11-08 11:10:19 -08001417
Dmitry Shmidt50b691d2014-05-21 14:01:45 -07001418#ifdef EAP_SERVER_TNC
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001419 if (conf->tnc && tncs_global_init() < 0) {
Dmitry Shmidt50b691d2014-05-21 14:01:45 -07001420 wpa_printf(MSG_ERROR, "Failed to initialize TNCS");
1421 return -1;
1422 }
1423#endif /* EAP_SERVER_TNC */
1424
Dmitry Shmidt54605472013-11-08 11:10:19 -08001425 if (hapd->started) {
1426 wpa_printf(MSG_ERROR, "%s: Interface %s was already started",
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001427 __func__, conf->iface);
Dmitry Shmidt54605472013-11-08 11:10:19 -08001428 return -1;
1429 }
1430 hapd->started = 1;
1431
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001432 if (!first || first == -1) {
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08001433 u8 *addr = hapd->own_addr;
1434
1435 if (!is_zero_ether_addr(conf->bssid)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001436 /* Allocate the configured BSSID. */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001437 os_memcpy(hapd->own_addr, conf->bssid, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001438
1439 if (hostapd_mac_comp(hapd->own_addr,
1440 hapd->iface->bss[0]->own_addr) ==
1441 0) {
1442 wpa_printf(MSG_ERROR, "BSS '%s' may not have "
1443 "BSSID set to the MAC address of "
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001444 "the radio", conf->iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001445 return -1;
1446 }
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08001447 } else if (hapd->iconf->use_driver_iface_addr) {
1448 addr = NULL;
1449 } else {
1450 /* Allocate the next available BSSID. */
1451 do {
1452 inc_byte_array(hapd->own_addr, ETH_ALEN);
1453 } while (mac_in_conf(hapd->iconf, hapd->own_addr));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001454 }
1455
Sunil Ravi7f769292024-07-23 22:21:32 +00001456#ifdef CONFIG_IEEE80211BE
1457 if (conf->mld_ap) {
1458 struct hostapd_data *h_hapd;
1459
1460 h_hapd = hostapd_mld_get_first_bss(hapd);
1461 if (h_hapd) {
1462 hapd->drv_priv = h_hapd->drv_priv;
1463 hapd->interface_added = h_hapd->interface_added;
Sunil Ravi7f769292024-07-23 22:21:32 +00001464 wpa_printf(MSG_DEBUG,
1465 "Setup of non first link (%d) BSS of MLD %s",
1466 hapd->mld_link_id, hapd->conf->iface);
1467 goto setup_mld;
1468 }
1469 }
1470#endif /* CONFIG_IEEE80211BE */
1471
Dmitry Shmidt54605472013-11-08 11:10:19 -08001472 hapd->interface_added = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001473 if (hostapd_if_add(hapd->iface->bss[0], WPA_IF_AP_BSS,
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08001474 conf->iface, addr, hapd,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001475 &hapd->drv_priv, force_ifname, if_addr,
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001476 conf->bridge[0] ? conf->bridge : NULL,
1477 first == -1)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001478 wpa_printf(MSG_ERROR, "Failed to add BSS (BSSID="
1479 MACSTR ")", MAC2STR(hapd->own_addr));
Dmitry Shmidt3cf6f792013-12-18 13:12:19 -08001480 hapd->interface_added = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001481 return -1;
1482 }
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08001483
1484 if (!addr)
1485 os_memcpy(hapd->own_addr, if_addr, ETH_ALEN);
Sunil Ravi7f769292024-07-23 22:21:32 +00001486
1487#ifdef CONFIG_IEEE80211BE
1488 if (hapd->conf->mld_ap) {
1489 wpa_printf(MSG_DEBUG,
1490 "Setup of first link (%d) BSS of MLD %s",
1491 hapd->mld_link_id, hapd->conf->iface);
1492 os_memcpy(hapd->mld->mld_addr, hapd->own_addr,
1493 ETH_ALEN);
Sunil Ravi7f769292024-07-23 22:21:32 +00001494 }
1495#endif /* CONFIG_IEEE80211BE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001496 }
1497
Sunil Ravi7f769292024-07-23 22:21:32 +00001498#ifdef CONFIG_IEEE80211BE
1499setup_mld:
1500 if (hapd->conf->mld_ap && !first) {
1501 wpa_printf(MSG_DEBUG,
1502 "MLD: Set link_id=%u, mld_addr=" MACSTR
1503 ", own_addr=" MACSTR,
1504 hapd->mld_link_id, MAC2STR(hapd->mld->mld_addr),
1505 MAC2STR(hapd->own_addr));
1506
1507 if (hostapd_drv_link_add(hapd, hapd->mld_link_id,
Sunil Ravic0f5d412024-09-11 22:12:49 +00001508 hapd->own_addr)) {
1509 wpa_printf(MSG_ERROR,
1510 "MLD: Failed to add link %d in MLD %s",
1511 hapd->mld_link_id, hapd->conf->iface);
Sunil Ravi7f769292024-07-23 22:21:32 +00001512 return -1;
Sunil Ravic0f5d412024-09-11 22:12:49 +00001513 }
1514 hostapd_mld_add_link(hapd);
Sunil Ravi7f769292024-07-23 22:21:32 +00001515 }
1516#endif /* CONFIG_IEEE80211BE */
1517
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001518 if (conf->wmm_enabled < 0)
Hai Shaloma20dcd72022-02-04 13:43:00 -08001519 conf->wmm_enabled = hapd->iconf->ieee80211n |
1520 hapd->iconf->ieee80211ax;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001521
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001522#ifdef CONFIG_IEEE80211R_AP
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001523 if (is_zero_ether_addr(conf->r1_key_holder))
1524 os_memcpy(conf->r1_key_holder, hapd->own_addr, ETH_ALEN);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001525#endif /* CONFIG_IEEE80211R_AP */
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001526
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001527#ifdef CONFIG_MESH
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001528 if ((hapd->conf->mesh & MESH_ENABLED) && hapd->iface->mconf == NULL)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001529 flush_old_stations = 0;
1530#endif /* CONFIG_MESH */
1531
1532 if (flush_old_stations)
Hai Shalom4fbc08f2020-05-18 12:37:00 -07001533 hostapd_flush(hapd);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001534 hostapd_set_privacy(hapd, 0);
1535
Hai Shalomfdcde762020-04-02 11:19:20 -07001536#ifdef CONFIG_WEP
Hai Shalom4fbc08f2020-05-18 12:37:00 -07001537 if (!hostapd_drv_nl80211(hapd))
1538 hostapd_broadcast_wep_clear(hapd);
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001539 if (hostapd_setup_encryption(conf->iface, hapd))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001540 return -1;
Hai Shalomfdcde762020-04-02 11:19:20 -07001541#endif /* CONFIG_WEP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001542
1543 /*
1544 * Fetch the SSID from the system and use it or,
1545 * if one was specified in the config file, verify they
1546 * match.
1547 */
1548 ssid_len = hostapd_get_ssid(hapd, ssid, sizeof(ssid));
1549 if (ssid_len < 0) {
1550 wpa_printf(MSG_ERROR, "Could not read SSID from system");
1551 return -1;
1552 }
1553 if (conf->ssid.ssid_set) {
1554 /*
1555 * If SSID is specified in the config file and it differs
1556 * from what is being used then force installation of the
1557 * new SSID.
1558 */
1559 set_ssid = (conf->ssid.ssid_len != (size_t) ssid_len ||
1560 os_memcmp(conf->ssid.ssid, ssid, ssid_len) != 0);
1561 } else {
1562 /*
1563 * No SSID in the config file; just use the one we got
1564 * from the system.
1565 */
1566 set_ssid = 0;
1567 conf->ssid.ssid_len = ssid_len;
1568 os_memcpy(conf->ssid.ssid, ssid, conf->ssid.ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001569 }
1570
Hai Shalomfdcde762020-04-02 11:19:20 -07001571 /*
1572 * Short SSID calculation is identical to FCS and it is defined in
1573 * IEEE P802.11-REVmd/D3.0, 9.4.2.170.3 (Calculating the Short-SSID).
1574 */
Sunil Ravi89eba102022-09-13 21:04:37 -07001575 conf->ssid.short_ssid = ieee80211_crc32(conf->ssid.ssid,
1576 conf->ssid.ssid_len);
Hai Shalomfdcde762020-04-02 11:19:20 -07001577
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001578 if (!hostapd_drv_none(hapd)) {
Hai Shalomfdcde762020-04-02 11:19:20 -07001579 wpa_printf(MSG_DEBUG, "Using interface %s with hwaddr " MACSTR
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001580 " and ssid \"%s\"",
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001581 conf->iface, MAC2STR(hapd->own_addr),
1582 wpa_ssid_txt(conf->ssid.ssid, conf->ssid.ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001583 }
1584
1585 if (hostapd_setup_wpa_psk(conf)) {
1586 wpa_printf(MSG_ERROR, "WPA-PSK setup failed.");
1587 return -1;
1588 }
1589
1590 /* Set SSID for the kernel driver (to be used in beacon and probe
1591 * response frames) */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001592 if (set_ssid && hostapd_set_ssid(hapd, conf->ssid.ssid,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001593 conf->ssid.ssid_len)) {
1594 wpa_printf(MSG_ERROR, "Could not set SSID for kernel driver");
1595 return -1;
1596 }
1597
Dmitry Shmidt818ea482014-03-10 13:15:21 -07001598 if (wpa_debug_level <= MSG_MSGDUMP)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001599 conf->radius->msg_dumps = 1;
1600#ifndef CONFIG_NO_RADIUS
Hai Shalomc3565922019-10-28 11:58:20 -07001601
1602#ifdef CONFIG_SQLITE
1603 if (conf->radius_req_attr_sqlite) {
1604 if (sqlite3_open(conf->radius_req_attr_sqlite,
1605 &hapd->rad_attr_db)) {
1606 wpa_printf(MSG_ERROR, "Could not open SQLite file '%s'",
1607 conf->radius_req_attr_sqlite);
1608 return -1;
1609 }
1610
1611 wpa_printf(MSG_DEBUG, "Opening RADIUS attribute database: %s",
1612 conf->radius_req_attr_sqlite);
1613 if (!db_table_exists(hapd->rad_attr_db, "radius_attributes") &&
1614 db_table_create_radius_attributes(hapd->rad_attr_db) < 0)
1615 return -1;
1616 }
1617#endif /* CONFIG_SQLITE */
1618
Sunil Ravi99c035e2024-07-12 01:42:03 +00001619 if (hostapd_mld_is_first_bss(hapd)) {
Sunil Ravi7f769292024-07-23 22:21:32 +00001620 if (hostapd_bss_radius_init(hapd))
Dmitry Shmidt04949592012-07-19 12:16:46 -07001621 return -1;
Sunil Ravi2a14cf12023-11-21 00:54:38 +00001622 } else {
Sunil Ravi99c035e2024-07-12 01:42:03 +00001623#ifdef CONFIG_IEEE80211BE
1624 struct hostapd_data *f_bss;
1625
Sunil Ravi99c035e2024-07-12 01:42:03 +00001626 f_bss = hostapd_mld_get_first_bss(hapd);
1627 if (!f_bss)
1628 return -1;
Sunil Ravi7f769292024-07-23 22:21:32 +00001629
1630 if (!f_bss->radius) {
1631 wpa_printf(MSG_DEBUG,
1632 "MLD: First BSS RADIUS client does not exist. Init on its behalf");
1633
1634 if (hostapd_bss_radius_init(f_bss))
1635 return -1;
1636 }
1637
1638 wpa_printf(MSG_DEBUG,
1639 "MLD: Using RADIUS client of the first BSS");
Sunil Ravi99c035e2024-07-12 01:42:03 +00001640 hapd->radius = f_bss->radius;
1641 hapd->radius_das = f_bss->radius_das;
1642#endif /* CONFIG_IEEE80211BE */
Dmitry Shmidt04949592012-07-19 12:16:46 -07001643 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001644#endif /* CONFIG_NO_RADIUS */
1645
1646 if (hostapd_acl_init(hapd)) {
1647 wpa_printf(MSG_ERROR, "ACL initialization failed.");
1648 return -1;
1649 }
1650 if (hostapd_init_wps(hapd, conf))
1651 return -1;
1652
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001653#ifdef CONFIG_DPP
1654 hapd->gas = gas_query_ap_init(hapd, hapd->msg_ctx);
1655 if (!hapd->gas)
1656 return -1;
1657 if (hostapd_dpp_init(hapd))
1658 return -1;
1659#endif /* CONFIG_DPP */
1660
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001661#ifdef CONFIG_NAN_USD
1662 if (hostapd_nan_usd_init(hapd) < 0)
1663 return -1;
1664#endif /* CONFIG_NAN_USD */
1665
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001666 if (authsrv_init(hapd) < 0)
1667 return -1;
1668
1669 if (ieee802_1x_init(hapd)) {
1670 wpa_printf(MSG_ERROR, "IEEE 802.1X initialization failed.");
1671 return -1;
1672 }
1673
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001674 if ((conf->wpa || conf->osen) && hostapd_setup_wpa(hapd))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001675 return -1;
1676
1677 if (accounting_init(hapd)) {
1678 wpa_printf(MSG_ERROR, "Accounting initialization failed.");
1679 return -1;
1680 }
1681
Dmitry Shmidt04949592012-07-19 12:16:46 -07001682#ifdef CONFIG_INTERWORKING
1683 if (gas_serv_init(hapd)) {
1684 wpa_printf(MSG_ERROR, "GAS server initialization failed");
1685 return -1;
1686 }
Sunil Ravi99c035e2024-07-12 01:42:03 +00001687#endif /* CONFIG_INTERWORKING */
Dmitry Shmidt04949592012-07-19 12:16:46 -07001688
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001689 if (conf->qos_map_set_len &&
1690 hostapd_drv_set_qos_map(hapd, conf->qos_map_set,
1691 conf->qos_map_set_len)) {
1692 wpa_printf(MSG_ERROR, "Failed to initialize QoS Map");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001693 return -1;
1694 }
1695
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001696 if (conf->bss_load_update_period && bss_load_update_init(hapd)) {
1697 wpa_printf(MSG_ERROR, "BSS Load initialization failed");
1698 return -1;
1699 }
1700
Sunil Ravi036cec52023-03-29 11:35:17 -07001701 if (conf->bridge[0]) {
1702 /* Set explicitly configured bridge parameters that might have
1703 * been lost if the interface has been removed out of the
1704 * bridge. */
1705
1706 /* multicast to unicast on bridge ports */
1707 if (conf->bridge_multicast_to_unicast)
1708 hostapd_drv_br_port_set_attr(
1709 hapd, DRV_BR_PORT_ATTR_MCAST2UCAST, 1);
1710
1711 /* hairpin mode */
1712 if (conf->bridge_hairpin)
1713 hostapd_drv_br_port_set_attr(
1714 hapd, DRV_BR_PORT_ATTR_HAIRPIN_MODE, 1);
1715 }
1716
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001717 if (conf->proxy_arp) {
1718 if (x_snoop_init(hapd)) {
1719 wpa_printf(MSG_ERROR,
1720 "Generic snooping infrastructure initialization failed");
1721 return -1;
1722 }
1723
1724 if (dhcp_snoop_init(hapd)) {
1725 wpa_printf(MSG_ERROR,
1726 "DHCP snooping initialization failed");
1727 return -1;
1728 }
1729
1730 if (ndisc_snoop_init(hapd)) {
1731 wpa_printf(MSG_ERROR,
1732 "Neighbor Discovery snooping initialization failed");
1733 return -1;
1734 }
1735 }
1736
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001737 if (!hostapd_drv_none(hapd) && vlan_init(hapd)) {
1738 wpa_printf(MSG_ERROR, "VLAN initialization failed.");
1739 return -1;
1740 }
1741
Sunil Ravi640215c2023-06-28 23:08:09 +00001742 if (start_beacon && hostapd_start_beacon(hapd, flush_old_stations) < 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001743 return -1;
1744
Sunil Ravi640215c2023-06-28 23:08:09 +00001745 if (hapd->wpa_auth && wpa_init_keys(hapd->wpa_auth) < 0)
1746 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001747
1748 return 0;
1749}
1750
1751
1752static void hostapd_tx_queue_params(struct hostapd_iface *iface)
1753{
1754 struct hostapd_data *hapd = iface->bss[0];
1755 int i;
1756 struct hostapd_tx_queue_params *p;
1757
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001758#ifdef CONFIG_MESH
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001759 if ((hapd->conf->mesh & MESH_ENABLED) && iface->mconf == NULL)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001760 return;
1761#endif /* CONFIG_MESH */
1762
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001763 for (i = 0; i < NUM_TX_QUEUES; i++) {
1764 p = &iface->conf->tx_queue[i];
1765
1766 if (hostapd_set_tx_queue_params(hapd, i, p->aifs, p->cwmin,
1767 p->cwmax, p->burst)) {
1768 wpa_printf(MSG_DEBUG, "Failed to set TX queue "
1769 "parameters for queue %d.", i);
1770 /* Continue anyway */
1771 }
1772 }
1773}
1774
1775
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001776static int hostapd_set_acl_list(struct hostapd_data *hapd,
1777 struct mac_acl_entry *mac_acl,
1778 int n_entries, u8 accept_acl)
1779{
1780 struct hostapd_acl_params *acl_params;
1781 int i, err;
1782
1783 acl_params = os_zalloc(sizeof(*acl_params) +
1784 (n_entries * sizeof(acl_params->mac_acl[0])));
1785 if (!acl_params)
1786 return -ENOMEM;
1787
1788 for (i = 0; i < n_entries; i++)
1789 os_memcpy(acl_params->mac_acl[i].addr, mac_acl[i].addr,
1790 ETH_ALEN);
1791
1792 acl_params->acl_policy = accept_acl;
1793 acl_params->num_mac_acl = n_entries;
1794
1795 err = hostapd_drv_set_acl(hapd, acl_params);
1796
1797 os_free(acl_params);
1798
1799 return err;
1800}
1801
1802
Sunil Ravia04bd252022-05-02 22:54:18 -07001803int hostapd_set_acl(struct hostapd_data *hapd)
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001804{
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00001805 struct hostapd_bss_config *conf = hapd->conf;
Sunil Ravia04bd252022-05-02 22:54:18 -07001806 int err = 0;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001807 u8 accept_acl;
1808
1809 if (hapd->iface->drv_max_acl_mac_addrs == 0)
Sunil Ravia04bd252022-05-02 22:54:18 -07001810 return 0;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001811
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00001812 if (conf->macaddr_acl == DENY_UNLESS_ACCEPTED) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001813 accept_acl = 1;
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00001814 err = hostapd_set_acl_list(hapd, conf->accept_mac,
1815 conf->num_accept_mac,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001816 accept_acl);
1817 if (err) {
1818 wpa_printf(MSG_DEBUG, "Failed to set accept acl");
Sunil Ravia04bd252022-05-02 22:54:18 -07001819 return -1;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001820 }
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00001821 } else if (conf->macaddr_acl == ACCEPT_UNLESS_DENIED) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001822 accept_acl = 0;
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00001823 err = hostapd_set_acl_list(hapd, conf->deny_mac,
1824 conf->num_deny_mac,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001825 accept_acl);
1826 if (err) {
1827 wpa_printf(MSG_DEBUG, "Failed to set deny acl");
Sunil Ravia04bd252022-05-02 22:54:18 -07001828 return -1;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001829 }
1830 }
Sunil Ravia04bd252022-05-02 22:54:18 -07001831 return err;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001832}
1833
1834
Sunil Ravic0f5d412024-09-11 22:12:49 +00001835static int hostapd_set_ctrl_sock_iface(struct hostapd_data *hapd)
1836{
1837#ifdef CONFIG_IEEE80211BE
1838 int ret;
1839
1840 if (hapd->conf->mld_ap) {
1841 ret = os_snprintf(hapd->ctrl_sock_iface,
1842 sizeof(hapd->ctrl_sock_iface), "%s_%s%d",
1843 hapd->conf->iface, WPA_CTRL_IFACE_LINK_NAME,
1844 hapd->mld_link_id);
1845 if (os_snprintf_error(sizeof(hapd->ctrl_sock_iface), ret))
1846 return -1;
1847 } else {
1848 os_strlcpy(hapd->ctrl_sock_iface, hapd->conf->iface,
1849 sizeof(hapd->ctrl_sock_iface));
1850 }
1851#endif /* CONFIG_IEEE80211BE */
1852 return 0;
1853}
1854
1855
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001856static int start_ctrl_iface_bss(struct hostapd_data *hapd)
1857{
1858 if (!hapd->iface->interfaces ||
1859 !hapd->iface->interfaces->ctrl_iface_init)
1860 return 0;
1861
Sunil Ravic0f5d412024-09-11 22:12:49 +00001862 if (hostapd_set_ctrl_sock_iface(hapd))
1863 return -1;
1864
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001865 if (hapd->iface->interfaces->ctrl_iface_init(hapd)) {
1866 wpa_printf(MSG_ERROR,
1867 "Failed to setup control interface for %s",
1868 hapd->conf->iface);
1869 return -1;
1870 }
1871
1872 return 0;
1873}
1874
1875
1876static int start_ctrl_iface(struct hostapd_iface *iface)
1877{
1878 size_t i;
1879
1880 if (!iface->interfaces || !iface->interfaces->ctrl_iface_init)
1881 return 0;
1882
1883 for (i = 0; i < iface->num_bss; i++) {
1884 struct hostapd_data *hapd = iface->bss[i];
Sunil Ravic0f5d412024-09-11 22:12:49 +00001885
1886 if (hostapd_set_ctrl_sock_iface(hapd))
1887 return -1;
1888
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001889 if (iface->interfaces->ctrl_iface_init(hapd)) {
1890 wpa_printf(MSG_ERROR,
1891 "Failed to setup control interface for %s",
1892 hapd->conf->iface);
1893 return -1;
1894 }
1895 }
1896
1897 return 0;
1898}
1899
1900
Sunil Ravi2a14cf12023-11-21 00:54:38 +00001901/* When NO_IR flag is set and AP is stopped, clean up BSS parameters without
1902 * deinitializing the driver and the control interfaces. A subsequent
1903 * REG_CHANGE event can bring the AP back up.
1904 */
1905static void hostapd_no_ir_cleanup(struct hostapd_data *bss)
1906{
1907 hostapd_bss_deinit_no_free(bss);
Sunil Ravi99c035e2024-07-12 01:42:03 +00001908 hostapd_bss_link_deinit(bss);
Sunil Ravi2a14cf12023-11-21 00:54:38 +00001909 hostapd_free_hapd_data(bss);
1910 hostapd_cleanup_iface_partial(bss->iface);
1911}
1912
1913
1914static int hostapd_no_ir_channel_list_updated(struct hostapd_iface *iface,
1915 void *ctx)
1916{
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00001917 struct hostapd_data *hapd = iface->bss[0];
Sunil Ravi2a14cf12023-11-21 00:54:38 +00001918 bool all_no_ir, is_6ghz;
1919 int i, j;
1920 struct hostapd_hw_modes *mode = NULL;
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00001921 struct hostapd_hw_modes *hw_features;
1922 u16 num_hw_features, flags;
1923 u8 dfs_domain;
Sunil Ravi2a14cf12023-11-21 00:54:38 +00001924
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00001925 if (hostapd_drv_none(hapd))
1926 return -1;
1927
1928 hw_features = hostapd_get_hw_feature_data(hapd, &num_hw_features,
1929 &flags, &dfs_domain);
1930 if (!hw_features) {
1931 wpa_printf(MSG_DEBUG,
1932 "Could not fetching hardware channel list");
1933 return -1;
1934 }
Sunil Ravi2a14cf12023-11-21 00:54:38 +00001935
1936 all_no_ir = true;
1937 is_6ghz = false;
1938
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00001939 for (i = 0; i < num_hw_features; i++) {
1940 mode = &hw_features[i];
Sunil Ravi2a14cf12023-11-21 00:54:38 +00001941
1942 if (mode->mode == iface->conf->hw_mode) {
1943 if (iface->freq > 0 &&
1944 !hw_mode_get_channel(mode, iface->freq, NULL)) {
1945 mode = NULL;
1946 continue;
1947 }
1948
1949 for (j = 0; j < mode->num_channels; j++) {
1950 if (!(mode->channels[j].flag &
1951 HOSTAPD_CHAN_NO_IR))
1952 all_no_ir = false;
1953
1954 if (is_6ghz_freq(mode->channels[j].freq))
1955 is_6ghz = true;
1956 }
1957 break;
1958 }
1959 }
1960
1961 if (!mode || !is_6ghz)
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00001962 goto free_hw_features;
Sunil Ravi2a14cf12023-11-21 00:54:38 +00001963
1964 if (iface->state == HAPD_IFACE_ENABLED) {
1965 if (!all_no_ir) {
1966 struct hostapd_channel_data *chan;
1967
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00001968 chan = hw_get_channel_freq(mode->mode,
Sunil Ravi2a14cf12023-11-21 00:54:38 +00001969 iface->freq, NULL,
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00001970 hw_features,
1971 num_hw_features);
Sunil Ravi2a14cf12023-11-21 00:54:38 +00001972
1973 if (!chan) {
1974 wpa_printf(MSG_ERROR,
1975 "NO_IR: Could not derive chan from freq");
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00001976 goto free_hw_features;
Sunil Ravi2a14cf12023-11-21 00:54:38 +00001977 }
1978
1979 if (!(chan->flag & HOSTAPD_CHAN_NO_IR))
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00001980 goto free_hw_features;
Sunil Ravi2a14cf12023-11-21 00:54:38 +00001981 wpa_printf(MSG_DEBUG,
1982 "NO_IR: The current channel has NO_IR flag now, stop AP.");
1983 } else {
1984 wpa_printf(MSG_DEBUG,
1985 "NO_IR: All chan in new chanlist are NO_IR, stop AP.");
1986 }
1987
1988 hostapd_set_state(iface, HAPD_IFACE_NO_IR);
1989 iface->is_no_ir = true;
1990 hostapd_drv_stop_ap(iface->bss[0]);
1991 hostapd_no_ir_cleanup(iface->bss[0]);
1992 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_NO_IR);
1993 } else if (iface->state == HAPD_IFACE_NO_IR) {
1994 if (all_no_ir) {
1995 wpa_printf(MSG_DEBUG,
1996 "NO_IR: AP in NO_IR and all chan in the new chanlist are NO_IR. Ignore");
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00001997 goto free_hw_features;
Sunil Ravi2a14cf12023-11-21 00:54:38 +00001998 }
1999
2000 if (!iface->conf->acs) {
2001 struct hostapd_channel_data *chan;
2002
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00002003 chan = hw_get_channel_freq(mode->mode,
Sunil Ravi2a14cf12023-11-21 00:54:38 +00002004 iface->freq, NULL,
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00002005 hw_features,
2006 num_hw_features);
Sunil Ravi2a14cf12023-11-21 00:54:38 +00002007 if (!chan) {
2008 wpa_printf(MSG_ERROR,
2009 "NO_IR: Could not derive chan from freq");
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00002010 goto free_hw_features;
Sunil Ravi2a14cf12023-11-21 00:54:38 +00002011 }
2012
2013 /* If the last operating channel is NO_IR, trigger ACS.
2014 */
2015 if (chan->flag & HOSTAPD_CHAN_NO_IR) {
2016 iface->freq = 0;
2017 iface->conf->channel = 0;
2018 if (acs_init(iface) != HOSTAPD_CHAN_ACS)
2019 wpa_printf(MSG_ERROR,
2020 "NO_IR: Could not start ACS");
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00002021 goto free_hw_features;
Sunil Ravi2a14cf12023-11-21 00:54:38 +00002022 }
2023 }
2024
2025 setup_interface2(iface);
2026 }
2027
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00002028free_hw_features:
2029 hostapd_free_hw_features(hw_features, num_hw_features);
Sunil Ravi2a14cf12023-11-21 00:54:38 +00002030 return 0;
2031}
2032
2033
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002034static void channel_list_update_timeout(void *eloop_ctx, void *timeout_ctx)
2035{
2036 struct hostapd_iface *iface = eloop_ctx;
2037
2038 if (!iface->wait_channel_update) {
2039 wpa_printf(MSG_INFO, "Channel list update timeout, but interface was not waiting for it");
2040 return;
2041 }
2042
2043 /*
2044 * It is possible that the existing channel list is acceptable, so try
2045 * to proceed.
2046 */
2047 wpa_printf(MSG_DEBUG, "Channel list update timeout - try to continue anyway");
2048 setup_interface2(iface);
2049}
2050
2051
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002052void hostapd_channel_list_updated(struct hostapd_iface *iface, int initiator)
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002053{
Sunil Ravi2a14cf12023-11-21 00:54:38 +00002054 if (initiator == REGDOM_SET_BY_DRIVER) {
2055 hostapd_for_each_interface(iface->interfaces,
2056 hostapd_no_ir_channel_list_updated,
2057 NULL);
2058 return;
2059 }
2060
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002061 if (!iface->wait_channel_update || initiator != REGDOM_SET_BY_USER)
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002062 return;
2063
2064 wpa_printf(MSG_DEBUG, "Channel list updated - continue setup");
2065 eloop_cancel_timeout(channel_list_update_timeout, iface, NULL);
2066 setup_interface2(iface);
2067}
2068
2069
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002070static int setup_interface(struct hostapd_iface *iface)
2071{
2072 struct hostapd_data *hapd = iface->bss[0];
2073 size_t i;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002074
Dmitry Shmidta38abf92014-03-06 13:38:44 -08002075 /*
2076 * It is possible that setup_interface() is called after the interface
2077 * was disabled etc., in which case driver_ap_teardown is possibly set
2078 * to 1. Clear it here so any other key/station deletion, which is not
2079 * part of a teardown flow, would also call the relevant driver
2080 * callbacks.
2081 */
2082 iface->driver_ap_teardown = 0;
2083
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002084 if (!iface->phy[0]) {
2085 const char *phy = hostapd_drv_get_radio_name(hapd);
2086 if (phy) {
2087 wpa_printf(MSG_DEBUG, "phy: %s", phy);
2088 os_strlcpy(iface->phy, phy, sizeof(iface->phy));
2089 }
2090 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002091
2092 /*
2093 * Make sure that all BSSes get configured with a pointer to the same
2094 * driver interface.
2095 */
2096 for (i = 1; i < iface->num_bss; i++) {
2097 iface->bss[i]->driver = hapd->driver;
2098 iface->bss[i]->drv_priv = hapd->drv_priv;
2099 }
2100
2101 if (hostapd_validate_bssid_configuration(iface))
2102 return -1;
2103
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002104 /*
2105 * Initialize control interfaces early to allow external monitoring of
2106 * channel setup operations that may take considerable amount of time
2107 * especially for DFS cases.
2108 */
2109 if (start_ctrl_iface(iface))
2110 return -1;
2111
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002112 if (hapd->iconf->country[0] && hapd->iconf->country[1]) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002113 char country[4], previous_country[4];
2114
2115 hostapd_set_state(iface, HAPD_IFACE_COUNTRY_UPDATE);
2116 if (hostapd_get_country(hapd, previous_country) < 0)
2117 previous_country[0] = '\0';
2118
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002119 os_memcpy(country, hapd->iconf->country, 3);
2120 country[3] = '\0';
2121 if (hostapd_set_country(hapd, country) < 0) {
2122 wpa_printf(MSG_ERROR, "Failed to set country code");
2123 return -1;
2124 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002125
2126 wpa_printf(MSG_DEBUG, "Previous country code %s, new country code %s",
2127 previous_country, country);
2128
2129 if (os_strncmp(previous_country, country, 2) != 0) {
2130 wpa_printf(MSG_DEBUG, "Continue interface setup after channel list update");
2131 iface->wait_channel_update = 1;
Dmitry Shmidt97672262014-02-03 13:02:54 -08002132 eloop_register_timeout(5, 0,
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002133 channel_list_update_timeout,
2134 iface, NULL);
2135 return 0;
2136 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002137 }
2138
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002139 return setup_interface2(iface);
2140}
2141
2142
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08002143static int configured_fixed_chan_to_freq(struct hostapd_iface *iface)
2144{
2145 int freq, i, j;
2146
2147 if (!iface->conf->channel)
2148 return 0;
2149 if (iface->conf->op_class) {
2150 freq = ieee80211_chan_to_freq(NULL, iface->conf->op_class,
2151 iface->conf->channel);
2152 if (freq < 0) {
2153 wpa_printf(MSG_INFO,
2154 "Could not convert op_class %u channel %u to operating frequency",
2155 iface->conf->op_class, iface->conf->channel);
2156 return -1;
2157 }
2158 iface->freq = freq;
2159 return 0;
2160 }
2161
2162 /* Old configurations using only 2.4/5/60 GHz bands may not specify the
2163 * op_class parameter. Select a matching channel from the configured
2164 * mode using the channel parameter for these cases.
2165 */
2166 for (j = 0; j < iface->num_hw_features; j++) {
2167 struct hostapd_hw_modes *mode = &iface->hw_features[j];
2168
2169 if (iface->conf->hw_mode != HOSTAPD_MODE_IEEE80211ANY &&
2170 iface->conf->hw_mode != mode->mode)
2171 continue;
2172 for (i = 0; i < mode->num_channels; i++) {
2173 struct hostapd_channel_data *chan = &mode->channels[i];
2174
2175 if (chan->chan == iface->conf->channel &&
2176 !is_6ghz_freq(chan->freq)) {
2177 iface->freq = chan->freq;
2178 return 0;
2179 }
2180 }
2181 }
2182
2183 wpa_printf(MSG_INFO, "Could not determine operating frequency");
2184 return -1;
2185}
2186
2187
Hai Shaloma20dcd72022-02-04 13:43:00 -08002188static void hostapd_set_6ghz_sec_chan(struct hostapd_iface *iface)
2189{
Sunil Ravi640215c2023-06-28 23:08:09 +00002190 int bw;
Hai Shaloma20dcd72022-02-04 13:43:00 -08002191
2192 if (!is_6ghz_op_class(iface->conf->op_class))
2193 return;
2194
Sunil Ravi640215c2023-06-28 23:08:09 +00002195 bw = op_class_to_bandwidth(iface->conf->op_class);
Hai Shaloma20dcd72022-02-04 13:43:00 -08002196 /* Assign the secondary channel if absent in config for
2197 * bandwidths > 20 MHz */
Sunil Ravi640215c2023-06-28 23:08:09 +00002198 if (bw >= 40 && !iface->conf->secondary_channel) {
Hai Shaloma20dcd72022-02-04 13:43:00 -08002199 if (((iface->conf->channel - 1) / 4) % 2)
2200 iface->conf->secondary_channel = -1;
2201 else
2202 iface->conf->secondary_channel = 1;
2203 }
2204}
2205
2206
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002207static int setup_interface2(struct hostapd_iface *iface)
2208{
2209 iface->wait_channel_update = 0;
Sunil Ravi2a14cf12023-11-21 00:54:38 +00002210 iface->is_no_ir = false;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002211
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002212 if (hostapd_get_hw_features(iface)) {
2213 /* Not all drivers support this yet, so continue without hw
2214 * feature data. */
2215 } else {
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08002216 int ret;
2217
Sunil Ravi99c035e2024-07-12 01:42:03 +00002218 if (iface->conf->acs && !iface->is_ch_switch_dfs) {
Sunil Ravi036cec52023-03-29 11:35:17 -07002219 iface->freq = 0;
2220 iface->conf->channel = 0;
2221 }
Sunil Ravi99c035e2024-07-12 01:42:03 +00002222 iface->is_ch_switch_dfs = false;
Sunil Ravi036cec52023-03-29 11:35:17 -07002223
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08002224 ret = configured_fixed_chan_to_freq(iface);
2225 if (ret < 0)
2226 goto fail;
2227
2228 if (iface->conf->op_class) {
Sunil8cd6f4d2022-06-28 18:40:46 +00002229 enum oper_chan_width ch_width;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08002230
2231 ch_width = op_class_to_ch_width(iface->conf->op_class);
2232 hostapd_set_oper_chwidth(iface->conf, ch_width);
Hai Shaloma20dcd72022-02-04 13:43:00 -08002233 hostapd_set_6ghz_sec_chan(iface);
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08002234 }
2235
2236 ret = hostapd_select_hw_mode(iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002237 if (ret < 0) {
2238 wpa_printf(MSG_ERROR, "Could not select hw_mode and "
2239 "channel. (%d)", ret);
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002240 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002241 }
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002242 if (ret == 1) {
2243 wpa_printf(MSG_DEBUG, "Interface initialization will be completed in a callback (ACS)");
2244 return 0;
2245 }
Hai Shalomc3565922019-10-28 11:58:20 -07002246 ret = hostapd_check_edmg_capab(iface);
2247 if (ret < 0)
2248 goto fail;
Hai Shalom60840252021-02-19 19:02:11 -08002249 ret = hostapd_check_he_6ghz_capab(iface);
2250 if (ret < 0)
2251 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002252 ret = hostapd_check_ht_capab(iface);
2253 if (ret < 0)
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002254 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002255 if (ret == 1) {
2256 wpa_printf(MSG_DEBUG, "Interface initialization will "
2257 "be completed in a callback");
2258 return 0;
2259 }
Dmitry Shmidt051af732013-10-22 13:52:46 -07002260
2261 if (iface->conf->ieee80211h)
2262 wpa_printf(MSG_DEBUG, "DFS support is enabled");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002263 }
2264 return hostapd_setup_interface_complete(iface, 0);
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002265
2266fail:
Sunil Ravi2a14cf12023-11-21 00:54:38 +00002267 if (iface->is_no_ir) {
2268 /* If AP is in NO_IR state, it can be reenabled by the driver
2269 * regulatory update and EVENT_CHANNEL_LIST_CHANGED. */
2270 hostapd_set_state(iface, HAPD_IFACE_NO_IR);
2271 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_NO_IR);
2272 return 0;
2273 }
2274
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002275 hostapd_set_state(iface, HAPD_IFACE_DISABLED);
2276 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
2277 if (iface->interfaces && iface->interfaces->terminate_on_error)
2278 eloop_terminate();
2279 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002280}
2281
2282
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002283#ifdef CONFIG_FST
2284
2285static const u8 * fst_hostapd_get_bssid_cb(void *ctx)
2286{
2287 struct hostapd_data *hapd = ctx;
2288
2289 return hapd->own_addr;
2290}
2291
2292
2293static void fst_hostapd_get_channel_info_cb(void *ctx,
2294 enum hostapd_hw_mode *hw_mode,
2295 u8 *channel)
2296{
2297 struct hostapd_data *hapd = ctx;
2298
2299 *hw_mode = ieee80211_freq_to_chan(hapd->iface->freq, channel);
2300}
2301
2302
Sunil8cd6f4d2022-06-28 18:40:46 +00002303static int fst_hostapd_get_hw_modes_cb(void *ctx,
2304 struct hostapd_hw_modes **modes)
2305{
2306 struct hostapd_data *hapd = ctx;
2307
2308 *modes = hapd->iface->hw_features;
2309 return hapd->iface->num_hw_features;
2310}
2311
2312
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002313static void fst_hostapd_set_ies_cb(void *ctx, const struct wpabuf *fst_ies)
2314{
2315 struct hostapd_data *hapd = ctx;
2316
2317 if (hapd->iface->fst_ies != fst_ies) {
2318 hapd->iface->fst_ies = fst_ies;
2319 if (ieee802_11_set_beacon(hapd))
2320 wpa_printf(MSG_WARNING, "FST: Cannot set beacon");
2321 }
2322}
2323
2324
2325static int fst_hostapd_send_action_cb(void *ctx, const u8 *da,
2326 struct wpabuf *buf)
2327{
2328 struct hostapd_data *hapd = ctx;
2329
2330 return hostapd_drv_send_action(hapd, hapd->iface->freq, 0, da,
2331 wpabuf_head(buf), wpabuf_len(buf));
2332}
2333
2334
2335static const struct wpabuf * fst_hostapd_get_mb_ie_cb(void *ctx, const u8 *addr)
2336{
2337 struct hostapd_data *hapd = ctx;
2338 struct sta_info *sta = ap_get_sta(hapd, addr);
2339
2340 return sta ? sta->mb_ies : NULL;
2341}
2342
2343
2344static void fst_hostapd_update_mb_ie_cb(void *ctx, const u8 *addr,
2345 const u8 *buf, size_t size)
2346{
2347 struct hostapd_data *hapd = ctx;
2348 struct sta_info *sta = ap_get_sta(hapd, addr);
2349
2350 if (sta) {
2351 struct mb_ies_info info;
2352
2353 if (!mb_ies_info_by_ies(&info, buf, size)) {
2354 wpabuf_free(sta->mb_ies);
2355 sta->mb_ies = mb_ies_by_info(&info);
2356 }
2357 }
2358}
2359
2360
2361static const u8 * fst_hostapd_get_sta(struct fst_get_peer_ctx **get_ctx,
Hai Shalome21d4e82020-04-29 16:34:06 -07002362 bool mb_only)
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002363{
2364 struct sta_info *s = (struct sta_info *) *get_ctx;
2365
2366 if (mb_only) {
2367 for (; s && !s->mb_ies; s = s->next)
2368 ;
2369 }
2370
2371 if (s) {
2372 *get_ctx = (struct fst_get_peer_ctx *) s->next;
2373
2374 return s->addr;
2375 }
2376
2377 *get_ctx = NULL;
2378 return NULL;
2379}
2380
2381
2382static const u8 * fst_hostapd_get_peer_first(void *ctx,
2383 struct fst_get_peer_ctx **get_ctx,
Hai Shalome21d4e82020-04-29 16:34:06 -07002384 bool mb_only)
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002385{
2386 struct hostapd_data *hapd = ctx;
2387
2388 *get_ctx = (struct fst_get_peer_ctx *) hapd->sta_list;
2389
2390 return fst_hostapd_get_sta(get_ctx, mb_only);
2391}
2392
2393
2394static const u8 * fst_hostapd_get_peer_next(void *ctx,
2395 struct fst_get_peer_ctx **get_ctx,
Hai Shalome21d4e82020-04-29 16:34:06 -07002396 bool mb_only)
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002397{
2398 return fst_hostapd_get_sta(get_ctx, mb_only);
2399}
2400
2401
2402void fst_hostapd_fill_iface_obj(struct hostapd_data *hapd,
2403 struct fst_wpa_obj *iface_obj)
2404{
Sunil8cd6f4d2022-06-28 18:40:46 +00002405 os_memset(iface_obj, 0, sizeof(*iface_obj));
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002406 iface_obj->ctx = hapd;
2407 iface_obj->get_bssid = fst_hostapd_get_bssid_cb;
2408 iface_obj->get_channel_info = fst_hostapd_get_channel_info_cb;
Sunil8cd6f4d2022-06-28 18:40:46 +00002409 iface_obj->get_hw_modes = fst_hostapd_get_hw_modes_cb;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002410 iface_obj->set_ies = fst_hostapd_set_ies_cb;
2411 iface_obj->send_action = fst_hostapd_send_action_cb;
2412 iface_obj->get_mb_ie = fst_hostapd_get_mb_ie_cb;
2413 iface_obj->update_mb_ie = fst_hostapd_update_mb_ie_cb;
2414 iface_obj->get_peer_first = fst_hostapd_get_peer_first;
2415 iface_obj->get_peer_next = fst_hostapd_get_peer_next;
2416}
2417
2418#endif /* CONFIG_FST */
2419
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002420#ifdef CONFIG_OWE
2421
2422static int hostapd_owe_iface_iter(struct hostapd_iface *iface, void *ctx)
2423{
2424 struct hostapd_data *hapd = ctx;
2425 size_t i;
2426
2427 for (i = 0; i < iface->num_bss; i++) {
2428 struct hostapd_data *bss = iface->bss[i];
2429
2430 if (os_strcmp(hapd->conf->owe_transition_ifname,
2431 bss->conf->iface) != 0)
2432 continue;
2433
2434 wpa_printf(MSG_DEBUG,
2435 "OWE: ifname=%s found transition mode ifname=%s BSSID "
2436 MACSTR " SSID %s",
2437 hapd->conf->iface, bss->conf->iface,
2438 MAC2STR(bss->own_addr),
2439 wpa_ssid_txt(bss->conf->ssid.ssid,
2440 bss->conf->ssid.ssid_len));
2441 if (!bss->conf->ssid.ssid_set || !bss->conf->ssid.ssid_len ||
2442 is_zero_ether_addr(bss->own_addr))
2443 continue;
2444
2445 os_memcpy(hapd->conf->owe_transition_bssid, bss->own_addr,
2446 ETH_ALEN);
2447 os_memcpy(hapd->conf->owe_transition_ssid,
2448 bss->conf->ssid.ssid, bss->conf->ssid.ssid_len);
2449 hapd->conf->owe_transition_ssid_len = bss->conf->ssid.ssid_len;
2450 wpa_printf(MSG_DEBUG,
2451 "OWE: Copied transition mode information");
2452 return 1;
2453 }
2454
2455 return 0;
2456}
2457
2458
2459int hostapd_owe_trans_get_info(struct hostapd_data *hapd)
2460{
2461 if (hapd->conf->owe_transition_ssid_len > 0 &&
2462 !is_zero_ether_addr(hapd->conf->owe_transition_bssid))
2463 return 0;
2464
2465 /* Find transition mode SSID/BSSID information from a BSS operated by
2466 * this hostapd instance. */
2467 if (!hapd->iface->interfaces ||
2468 !hapd->iface->interfaces->for_each_interface)
2469 return hostapd_owe_iface_iter(hapd->iface, hapd);
2470 else
2471 return hapd->iface->interfaces->for_each_interface(
2472 hapd->iface->interfaces, hostapd_owe_iface_iter, hapd);
2473}
2474
2475
2476static int hostapd_owe_iface_iter2(struct hostapd_iface *iface, void *ctx)
2477{
2478 size_t i;
2479
2480 for (i = 0; i < iface->num_bss; i++) {
2481 struct hostapd_data *bss = iface->bss[i];
2482 int res;
2483
2484 if (!bss->conf->owe_transition_ifname[0])
2485 continue;
Hai Shalom899fcc72020-10-19 14:38:18 -07002486 if (bss->iface->state != HAPD_IFACE_ENABLED) {
2487 wpa_printf(MSG_DEBUG,
2488 "OWE: Interface %s state %s - defer beacon update",
2489 bss->conf->iface,
2490 hostapd_state_text(bss->iface->state));
2491 continue;
2492 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002493 res = hostapd_owe_trans_get_info(bss);
2494 if (res == 0)
2495 continue;
2496 wpa_printf(MSG_DEBUG,
2497 "OWE: Matching transition mode interface enabled - update beacon data for %s",
2498 bss->conf->iface);
2499 ieee802_11_set_beacon(bss);
2500 }
2501
2502 return 0;
2503}
2504
2505#endif /* CONFIG_OWE */
2506
2507
2508static void hostapd_owe_update_trans(struct hostapd_iface *iface)
2509{
2510#ifdef CONFIG_OWE
2511 /* Check whether the enabled BSS can complete OWE transition mode
2512 * configuration for any pending interface. */
2513 if (!iface->interfaces ||
2514 !iface->interfaces->for_each_interface)
2515 hostapd_owe_iface_iter2(iface, NULL);
2516 else
2517 iface->interfaces->for_each_interface(
2518 iface->interfaces, hostapd_owe_iface_iter2, NULL);
2519#endif /* CONFIG_OWE */
2520}
2521
2522
Roshan Pius3a1667e2018-07-03 15:17:14 -07002523static void hostapd_interface_setup_failure_handler(void *eloop_ctx,
2524 void *timeout_ctx)
2525{
2526 struct hostapd_iface *iface = eloop_ctx;
2527 struct hostapd_data *hapd;
2528
2529 if (iface->num_bss < 1 || !iface->bss || !iface->bss[0])
2530 return;
2531 hapd = iface->bss[0];
2532 if (hapd->setup_complete_cb)
2533 hapd->setup_complete_cb(hapd->setup_complete_cb_ctx);
2534}
2535
2536
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002537static int hostapd_setup_interface_complete_sync(struct hostapd_iface *iface,
2538 int err)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002539{
2540 struct hostapd_data *hapd = iface->bss[0];
2541 size_t j;
2542 u8 *prev_addr;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002543 int delay_apply_cfg = 0;
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08002544 int res_dfs_offload = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002545
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002546 if (err)
2547 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002548
2549 wpa_printf(MSG_DEBUG, "Completing interface initialization");
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08002550 if (iface->freq) {
Dmitry Shmidt051af732013-10-22 13:52:46 -07002551#ifdef NEED_AP_MLME
2552 int res;
2553#endif /* NEED_AP_MLME */
2554
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002555 wpa_printf(MSG_DEBUG, "Mode: %s Channel: %d "
2556 "Frequency: %d MHz",
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002557 hostapd_hw_mode_txt(iface->conf->hw_mode),
2558 iface->conf->channel, iface->freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002559
Sunil Ravic0f5d412024-09-11 22:12:49 +00002560 if (hostapd_set_current_hw_info(iface, iface->freq)) {
2561 wpa_printf(MSG_ERROR,
2562 "Failed to set current hardware info");
2563 goto fail;
2564 }
2565
Dmitry Shmidt051af732013-10-22 13:52:46 -07002566#ifdef NEED_AP_MLME
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07002567 /* Handle DFS only if it is not offloaded to the driver */
2568 if (!(iface->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD)) {
2569 /* Check DFS */
2570 res = hostapd_handle_dfs(iface);
2571 if (res <= 0) {
2572 if (res < 0)
2573 goto fail;
2574 return res;
2575 }
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08002576 } else {
2577 /* If DFS is offloaded to the driver */
2578 res_dfs_offload = hostapd_handle_dfs_offload(iface);
2579 if (res_dfs_offload <= 0) {
2580 if (res_dfs_offload < 0)
2581 goto fail;
2582 } else {
2583 wpa_printf(MSG_DEBUG,
2584 "Proceed with AP/channel setup");
2585 /*
2586 * If this is a DFS channel, move to completing
2587 * AP setup.
2588 */
2589 if (res_dfs_offload == 1)
2590 goto dfs_offload;
2591 /* Otherwise fall through. */
2592 }
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002593 }
Dmitry Shmidt051af732013-10-22 13:52:46 -07002594#endif /* NEED_AP_MLME */
2595
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002596#ifdef CONFIG_MESH
2597 if (iface->mconf != NULL) {
2598 wpa_printf(MSG_DEBUG,
2599 "%s: Mesh configuration will be applied while joining the mesh network",
2600 iface->bss[0]->conf->iface);
2601 delay_apply_cfg = 1;
2602 }
2603#endif /* CONFIG_MESH */
2604
2605 if (!delay_apply_cfg &&
2606 hostapd_set_freq(hapd, hapd->iconf->hw_mode, iface->freq,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002607 hapd->iconf->channel,
Hai Shalomc3565922019-10-28 11:58:20 -07002608 hapd->iconf->enable_edmg,
2609 hapd->iconf->edmg_channel,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002610 hapd->iconf->ieee80211n,
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002611 hapd->iconf->ieee80211ac,
Hai Shalom81f62d82019-07-22 12:10:00 -07002612 hapd->iconf->ieee80211ax,
Sunil Ravia04bd252022-05-02 22:54:18 -07002613 hapd->iconf->ieee80211be,
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002614 hapd->iconf->secondary_channel,
Hai Shalom81f62d82019-07-22 12:10:00 -07002615 hostapd_get_oper_chwidth(hapd->iconf),
2616 hostapd_get_oper_centr_freq_seg0_idx(
2617 hapd->iconf),
2618 hostapd_get_oper_centr_freq_seg1_idx(
2619 hapd->iconf))) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002620 wpa_printf(MSG_ERROR, "Could not set channel for "
2621 "kernel driver");
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002622 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002623 }
2624 }
2625
2626 if (iface->current_mode) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002627 if (hostapd_prepare_rates(iface, iface->current_mode)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002628 wpa_printf(MSG_ERROR, "Failed to prepare rates "
2629 "table.");
2630 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
2631 HOSTAPD_LEVEL_WARNING,
2632 "Failed to prepare rates table.");
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002633 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002634 }
2635 }
2636
Hai Shalom021b0b52019-04-10 11:17:58 -07002637 if (hapd->iconf->rts_threshold >= -1 &&
2638 hostapd_set_rts(hapd, hapd->iconf->rts_threshold) &&
2639 hapd->iconf->rts_threshold >= -1) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002640 wpa_printf(MSG_ERROR, "Could not set RTS threshold for "
2641 "kernel driver");
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002642 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002643 }
2644
Hai Shalom021b0b52019-04-10 11:17:58 -07002645 if (hapd->iconf->fragm_threshold >= -1 &&
2646 hostapd_set_frag(hapd, hapd->iconf->fragm_threshold) &&
2647 hapd->iconf->fragm_threshold != -1) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002648 wpa_printf(MSG_ERROR, "Could not set fragmentation threshold "
2649 "for kernel driver");
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002650 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002651 }
2652
2653 prev_addr = hapd->own_addr;
2654
2655 for (j = 0; j < iface->num_bss; j++) {
2656 hapd = iface->bss[j];
2657 if (j)
2658 os_memcpy(hapd->own_addr, prev_addr, ETH_ALEN);
Sunil Ravi77d572f2023-01-17 23:58:31 +00002659 if (hostapd_setup_bss(hapd, j == 0, !iface->conf->mbssid)) {
Hai Shalom021b0b52019-04-10 11:17:58 -07002660 for (;;) {
Dmitry Shmidt71757432014-06-02 13:50:35 -07002661 hapd = iface->bss[j];
2662 hostapd_bss_deinit_no_free(hapd);
2663 hostapd_free_hapd_data(hapd);
Hai Shalom021b0b52019-04-10 11:17:58 -07002664 if (j == 0)
2665 break;
2666 j--;
2667 }
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002668 goto fail;
Dmitry Shmidt71757432014-06-02 13:50:35 -07002669 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002670 if (is_zero_ether_addr(hapd->conf->bssid))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002671 prev_addr = hapd->own_addr;
2672 }
Sunil Ravi77d572f2023-01-17 23:58:31 +00002673
2674 if (hapd->iconf->mbssid) {
2675 for (j = 0; hapd->iconf->mbssid && j < iface->num_bss; j++) {
2676 hapd = iface->bss[j];
2677 if (hostapd_start_beacon(hapd, true)) {
2678 for (;;) {
2679 hapd = iface->bss[j];
2680 hostapd_bss_deinit_no_free(hapd);
2681 hostapd_free_hapd_data(hapd);
2682 if (j == 0)
2683 break;
2684 j--;
2685 }
2686 goto fail;
2687 }
2688 }
2689 }
2690
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002691 hapd = iface->bss[0];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002692
2693 hostapd_tx_queue_params(iface);
2694
2695 ap_list_init(iface);
2696
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07002697 hostapd_set_acl(hapd);
2698
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002699 if (hostapd_driver_commit(hapd) < 0) {
2700 wpa_printf(MSG_ERROR, "%s: Failed to commit driver "
2701 "configuration", __func__);
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002702 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002703 }
2704
Jouni Malinen87fd2792011-05-16 18:35:42 +03002705 /*
2706 * WPS UPnP module can be initialized only when the "upnp_iface" is up.
2707 * If "interface" and "upnp_iface" are the same (e.g., non-bridge
2708 * mode), the interface is up only after driver_commit, so initialize
2709 * WPS after driver_commit.
2710 */
2711 for (j = 0; j < iface->num_bss; j++) {
2712 if (hostapd_init_wps_complete(iface->bss[j]))
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002713 goto fail;
Jouni Malinen87fd2792011-05-16 18:35:42 +03002714 }
2715
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08002716 if ((iface->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
2717 !res_dfs_offload) {
2718 /*
2719 * If freq is DFS, and DFS is offloaded to the driver, then wait
2720 * for CAC to complete.
2721 */
2722 wpa_printf(MSG_DEBUG, "%s: Wait for CAC to complete", __func__);
2723 return res_dfs_offload;
2724 }
2725
2726#ifdef NEED_AP_MLME
2727dfs_offload:
2728#endif /* NEED_AP_MLME */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002729
2730#ifdef CONFIG_FST
2731 if (hapd->iconf->fst_cfg.group_id[0]) {
2732 struct fst_wpa_obj iface_obj;
2733
2734 fst_hostapd_fill_iface_obj(hapd, &iface_obj);
2735 iface->fst = fst_attach(hapd->conf->iface, hapd->own_addr,
2736 &iface_obj, &hapd->iconf->fst_cfg);
2737 if (!iface->fst) {
2738 wpa_printf(MSG_ERROR, "Could not attach to FST %s",
2739 hapd->iconf->fst_cfg.group_id);
2740 goto fail;
2741 }
2742 }
2743#endif /* CONFIG_FST */
2744
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002745 hostapd_set_state(iface, HAPD_IFACE_ENABLED);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002746 hostapd_owe_update_trans(iface);
Hai Shalom81f62d82019-07-22 12:10:00 -07002747 airtime_policy_update_init(iface);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002748 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_ENABLED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002749 if (hapd->setup_complete_cb)
2750 hapd->setup_complete_cb(hapd->setup_complete_cb_ctx);
2751
Hai Shalom60840252021-02-19 19:02:11 -08002752#ifdef CONFIG_MESH
2753 if (delay_apply_cfg && !iface->mconf) {
2754 wpa_printf(MSG_ERROR, "Error while completing mesh init");
2755 goto fail;
2756 }
2757#endif /* CONFIG_MESH */
2758
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002759 wpa_printf(MSG_DEBUG, "%s: Setup of interface done.",
2760 iface->bss[0]->conf->iface);
Dmitry Shmidtb96dad42013-11-05 10:07:29 -08002761 if (iface->interfaces && iface->interfaces->terminate_on_error > 0)
2762 iface->interfaces->terminate_on_error--;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002763
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002764 for (j = 0; j < iface->num_bss; j++)
Hai Shalom74f70d42019-02-11 14:42:39 -08002765 hostapd_neighbor_set_own_report(iface->bss[j]);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002766
Sunil Ravi2a14cf12023-11-21 00:54:38 +00002767 if (iface->interfaces && iface->interfaces->count > 1)
2768 ieee802_11_set_beacons(iface);
2769
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002770 return 0;
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002771
2772fail:
2773 wpa_printf(MSG_ERROR, "Interface initialization failed");
Sunil Ravi2a14cf12023-11-21 00:54:38 +00002774
2775 if (iface->is_no_ir) {
2776 hostapd_set_state(iface, HAPD_IFACE_NO_IR);
2777 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_EVENT_NO_IR);
2778 return 0;
2779 }
2780
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002781 hostapd_set_state(iface, HAPD_IFACE_DISABLED);
2782 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002783#ifdef CONFIG_FST
2784 if (iface->fst) {
2785 fst_detach(iface->fst);
2786 iface->fst = NULL;
2787 }
2788#endif /* CONFIG_FST */
Roshan Pius3a1667e2018-07-03 15:17:14 -07002789
2790 if (iface->interfaces && iface->interfaces->terminate_on_error) {
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002791 eloop_terminate();
Roshan Pius3a1667e2018-07-03 15:17:14 -07002792 } else if (hapd->setup_complete_cb) {
2793 /*
2794 * Calling hapd->setup_complete_cb directly may cause iface
2795 * deinitialization which may be accessed later by the caller.
2796 */
2797 eloop_register_timeout(0, 0,
2798 hostapd_interface_setup_failure_handler,
2799 iface, NULL);
2800 }
2801
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002802 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002803}
2804
2805
2806/**
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002807 * hostapd_setup_interface_complete - Complete interface setup
2808 *
2809 * This function is called when previous steps in the interface setup has been
2810 * completed. This can also start operations, e.g., DFS, that will require
2811 * additional processing before interface is ready to be enabled. Such
2812 * operations will call this function from eloop callbacks when finished.
2813 */
2814int hostapd_setup_interface_complete(struct hostapd_iface *iface, int err)
2815{
2816 struct hapd_interfaces *interfaces = iface->interfaces;
2817 struct hostapd_data *hapd = iface->bss[0];
2818 unsigned int i;
2819 int not_ready_in_sync_ifaces = 0;
2820
2821 if (!iface->need_to_start_in_sync)
2822 return hostapd_setup_interface_complete_sync(iface, err);
2823
2824 if (err) {
2825 wpa_printf(MSG_ERROR, "Interface initialization failed");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002826 iface->need_to_start_in_sync = 0;
Sunil Ravi2a14cf12023-11-21 00:54:38 +00002827
2828 if (iface->is_no_ir) {
2829 hostapd_set_state(iface, HAPD_IFACE_NO_IR);
2830 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_EVENT_NO_IR);
2831 return 0;
2832 }
2833
2834 hostapd_set_state(iface, HAPD_IFACE_DISABLED);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002835 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
2836 if (interfaces && interfaces->terminate_on_error)
2837 eloop_terminate();
2838 return -1;
2839 }
2840
2841 if (iface->ready_to_start_in_sync) {
2842 /* Already in ready and waiting. should never happpen */
2843 return 0;
2844 }
2845
2846 for (i = 0; i < interfaces->count; i++) {
2847 if (interfaces->iface[i]->need_to_start_in_sync &&
2848 !interfaces->iface[i]->ready_to_start_in_sync)
2849 not_ready_in_sync_ifaces++;
2850 }
2851
2852 /*
2853 * Check if this is the last interface, if yes then start all the other
2854 * waiting interfaces. If not, add this interface to the waiting list.
2855 */
2856 if (not_ready_in_sync_ifaces > 1 && iface->state == HAPD_IFACE_DFS) {
2857 /*
2858 * If this interface went through CAC, do not synchronize, just
2859 * start immediately.
2860 */
2861 iface->need_to_start_in_sync = 0;
2862 wpa_printf(MSG_INFO,
2863 "%s: Finished CAC - bypass sync and start interface",
2864 iface->bss[0]->conf->iface);
2865 return hostapd_setup_interface_complete_sync(iface, err);
2866 }
2867
2868 if (not_ready_in_sync_ifaces > 1) {
2869 /* need to wait as there are other interfaces still coming up */
2870 iface->ready_to_start_in_sync = 1;
2871 wpa_printf(MSG_INFO,
2872 "%s: Interface waiting to sync with other interfaces",
2873 iface->bss[0]->conf->iface);
2874 return 0;
2875 }
2876
2877 wpa_printf(MSG_INFO,
2878 "%s: Last interface to sync - starting all interfaces",
2879 iface->bss[0]->conf->iface);
2880 iface->need_to_start_in_sync = 0;
2881 hostapd_setup_interface_complete_sync(iface, err);
2882 for (i = 0; i < interfaces->count; i++) {
2883 if (interfaces->iface[i]->need_to_start_in_sync &&
2884 interfaces->iface[i]->ready_to_start_in_sync) {
2885 hostapd_setup_interface_complete_sync(
2886 interfaces->iface[i], 0);
2887 /* Only once the interfaces are sync started */
2888 interfaces->iface[i]->need_to_start_in_sync = 0;
2889 }
2890 }
2891
2892 return 0;
2893}
2894
2895
2896/**
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002897 * hostapd_setup_interface - Setup of an interface
2898 * @iface: Pointer to interface data.
2899 * Returns: 0 on success, -1 on failure
2900 *
2901 * Initializes the driver interface, validates the configuration,
2902 * and sets driver parameters based on the configuration.
2903 * Flushes old stations, sets the channel, encryption,
2904 * beacons, and WDS links based on the configuration.
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002905 *
2906 * If interface setup requires more time, e.g., to perform HT co-ex scans, ACS,
2907 * or DFS operations, this function returns 0 before such operations have been
2908 * completed. The pending operations are registered into eloop and will be
2909 * completed from eloop callbacks. Those callbacks end up calling
2910 * hostapd_setup_interface_complete() once setup has been completed.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002911 */
2912int hostapd_setup_interface(struct hostapd_iface *iface)
2913{
2914 int ret;
2915
Hai Shaloma20dcd72022-02-04 13:43:00 -08002916 if (!iface->conf)
2917 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002918 ret = setup_interface(iface);
2919 if (ret) {
2920 wpa_printf(MSG_ERROR, "%s: Unable to setup interface.",
Hai Shaloma20dcd72022-02-04 13:43:00 -08002921 iface->conf->bss[0]->iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002922 return -1;
2923 }
2924
2925 return 0;
2926}
2927
2928
2929/**
2930 * hostapd_alloc_bss_data - Allocate and initialize per-BSS data
2931 * @hapd_iface: Pointer to interface data
2932 * @conf: Pointer to per-interface configuration
2933 * @bss: Pointer to per-BSS configuration for this BSS
2934 * Returns: Pointer to allocated BSS data
2935 *
2936 * This function is used to allocate per-BSS data structure. This data will be
2937 * freed after hostapd_cleanup() is called for it during interface
2938 * deinitialization.
2939 */
2940struct hostapd_data *
2941hostapd_alloc_bss_data(struct hostapd_iface *hapd_iface,
2942 struct hostapd_config *conf,
2943 struct hostapd_bss_config *bss)
2944{
2945 struct hostapd_data *hapd;
2946
2947 hapd = os_zalloc(sizeof(*hapd));
2948 if (hapd == NULL)
2949 return NULL;
2950
2951 hapd->new_assoc_sta_cb = hostapd_new_assoc_sta;
2952 hapd->iconf = conf;
2953 hapd->conf = bss;
2954 hapd->iface = hapd_iface;
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08002955 if (conf)
2956 hapd->driver = conf->driver;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002957 hapd->ctrl_sock = -1;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08002958 dl_list_init(&hapd->ctrl_dst);
Dmitry Shmidt7d175302016-09-06 13:11:34 -07002959 dl_list_init(&hapd->nr_db);
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08002960 hapd->dhcp_sock = -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002961#ifdef CONFIG_IEEE80211R_AP
2962 dl_list_init(&hapd->l2_queue);
2963 dl_list_init(&hapd->l2_oui_queue);
2964#endif /* CONFIG_IEEE80211R_AP */
Hai Shalom021b0b52019-04-10 11:17:58 -07002965#ifdef CONFIG_SAE
2966 dl_list_init(&hapd->sae_commit_queue);
2967#endif /* CONFIG_SAE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002968
2969 return hapd;
2970}
2971
2972
Dmitry Shmidt54605472013-11-08 11:10:19 -08002973static void hostapd_bss_deinit(struct hostapd_data *hapd)
2974{
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07002975 if (!hapd)
2976 return;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002977 wpa_printf(MSG_DEBUG, "%s: deinit bss %s", __func__,
Hai Shalom021b0b52019-04-10 11:17:58 -07002978 hapd->conf ? hapd->conf->iface : "N/A");
Dmitry Shmidt71757432014-06-02 13:50:35 -07002979 hostapd_bss_deinit_no_free(hapd);
Dmitry Shmidtf73259c2015-03-17 11:00:54 -07002980 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
Hai Shalomc3565922019-10-28 11:58:20 -07002981#ifdef CONFIG_SQLITE
2982 if (hapd->rad_attr_db) {
2983 sqlite3_close(hapd->rad_attr_db);
2984 hapd->rad_attr_db = NULL;
2985 }
2986#endif /* CONFIG_SQLITE */
Sunil Ravi99c035e2024-07-12 01:42:03 +00002987
2988 hostapd_bss_link_deinit(hapd);
Dmitry Shmidt54605472013-11-08 11:10:19 -08002989 hostapd_cleanup(hapd);
2990}
2991
2992
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002993void hostapd_interface_deinit(struct hostapd_iface *iface)
2994{
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002995 int j;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002996
Dmitry Shmidt54605472013-11-08 11:10:19 -08002997 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002998 if (iface == NULL)
2999 return;
3000
Dmitry Shmidtf73259c2015-03-17 11:00:54 -07003001 hostapd_set_state(iface, HAPD_IFACE_DISABLED);
3002
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003003 eloop_cancel_timeout(channel_list_update_timeout, iface, NULL);
3004 iface->wait_channel_update = 0;
Sunil Ravi2a14cf12023-11-21 00:54:38 +00003005 iface->is_no_ir = false;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003006
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003007#ifdef CONFIG_FST
3008 if (iface->fst) {
3009 fst_detach(iface->fst);
3010 iface->fst = NULL;
3011 }
3012#endif /* CONFIG_FST */
3013
Hai Shalom021b0b52019-04-10 11:17:58 -07003014 for (j = (int) iface->num_bss - 1; j >= 0; j--) {
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07003015 if (!iface->bss)
3016 break;
Dmitry Shmidt54605472013-11-08 11:10:19 -08003017 hostapd_bss_deinit(iface->bss[j]);
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07003018 }
Roshan Pius3a1667e2018-07-03 15:17:14 -07003019
Roshan Pius3a1667e2018-07-03 15:17:14 -07003020#ifdef NEED_AP_MLME
3021 hostapd_stop_setup_timers(iface);
3022 eloop_cancel_timeout(ap_ht2040_timeout, iface, NULL);
3023#endif /* NEED_AP_MLME */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003024}
3025
3026
Sunil Ravi99c035e2024-07-12 01:42:03 +00003027#ifdef CONFIG_IEEE80211BE
3028
3029static void hostapd_mld_ref_inc(struct hostapd_mld *mld)
3030{
3031 if (!mld)
3032 return;
3033
3034 if (mld->refcount == HOSTAPD_MLD_MAX_REF_COUNT) {
3035 wpa_printf(MSG_ERROR, "AP MLD %s: Ref count overflow",
3036 mld->name);
3037 return;
3038 }
3039
3040 mld->refcount++;
3041}
3042
3043
3044static void hostapd_mld_ref_dec(struct hostapd_mld *mld)
3045{
3046 if (!mld)
3047 return;
3048
3049 if (!mld->refcount) {
3050 wpa_printf(MSG_ERROR, "AP MLD %s: Ref count underflow",
3051 mld->name);
3052 return;
3053 }
3054
3055 mld->refcount--;
3056}
3057
3058#endif /* CONFIG_IEEE80211BE */
3059
3060
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003061void hostapd_interface_free(struct hostapd_iface *iface)
3062{
3063 size_t j;
Dmitry Shmidt54605472013-11-08 11:10:19 -08003064 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
3065 for (j = 0; j < iface->num_bss; j++) {
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07003066 if (!iface->bss)
3067 break;
Sunil Ravi99c035e2024-07-12 01:42:03 +00003068#ifdef CONFIG_IEEE80211BE
3069 if (iface->bss[j])
3070 hostapd_mld_ref_dec(iface->bss[j]->mld);
3071#endif /* CONFIG_IEEE80211BE */
Dmitry Shmidt54605472013-11-08 11:10:19 -08003072 wpa_printf(MSG_DEBUG, "%s: free hapd %p",
3073 __func__, iface->bss[j]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003074 os_free(iface->bss[j]);
Dmitry Shmidt54605472013-11-08 11:10:19 -08003075 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003076 hostapd_cleanup_iface(iface);
3077}
3078
3079
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07003080struct hostapd_iface * hostapd_alloc_iface(void)
3081{
3082 struct hostapd_iface *hapd_iface;
3083
3084 hapd_iface = os_zalloc(sizeof(*hapd_iface));
3085 if (!hapd_iface)
3086 return NULL;
3087
3088 dl_list_init(&hapd_iface->sta_seen);
3089
3090 return hapd_iface;
3091}
3092
3093
Sunil Ravi99c035e2024-07-12 01:42:03 +00003094#ifdef CONFIG_IEEE80211BE
3095static void hostapd_bss_alloc_link_id(struct hostapd_data *hapd)
3096{
3097 hapd->mld_link_id = hapd->mld->next_link_id++;
3098 wpa_printf(MSG_DEBUG, "AP MLD: %s: Link ID %d assigned.",
3099 hapd->mld->name, hapd->mld_link_id);
3100}
3101#endif /* CONFIG_IEEE80211BE */
3102
3103
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00003104void hostapd_bss_setup_multi_link(struct hostapd_data *hapd,
3105 struct hapd_interfaces *interfaces)
Sunil Ravi99c035e2024-07-12 01:42:03 +00003106{
3107#ifdef CONFIG_IEEE80211BE
3108 struct hostapd_mld *mld, **all_mld;
3109 struct hostapd_bss_config *conf;
3110 size_t i;
3111
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00003112 if (hapd->mld)
3113 return;
3114
Sunil Ravi99c035e2024-07-12 01:42:03 +00003115 conf = hapd->conf;
3116
3117 if (!hapd->iconf || !hapd->iconf->ieee80211be || !conf->mld_ap ||
3118 conf->disable_11be)
3119 return;
3120
3121 for (i = 0; i < interfaces->mld_count; i++) {
3122 mld = interfaces->mld[i];
3123
3124 if (!mld || os_strcmp(conf->iface, mld->name) != 0)
3125 continue;
3126
3127 hapd->mld = mld;
3128 hostapd_mld_ref_inc(mld);
3129 hostapd_bss_alloc_link_id(hapd);
3130 break;
3131 }
3132
3133 if (hapd->mld)
3134 return;
3135
3136 mld = os_zalloc(sizeof(struct hostapd_mld));
3137 if (!mld)
3138 goto fail;
3139
3140 os_strlcpy(mld->name, conf->iface, sizeof(conf->iface));
3141 dl_list_init(&mld->links);
Sunil Ravic0f5d412024-09-11 22:12:49 +00003142 mld->ctrl_sock = -1;
3143 if (hapd->conf->ctrl_interface)
3144 mld->ctrl_interface = os_strdup(hapd->conf->ctrl_interface);
Sunil Ravi99c035e2024-07-12 01:42:03 +00003145
3146 wpa_printf(MSG_DEBUG, "AP MLD %s created", mld->name);
3147
Sunil Ravic0f5d412024-09-11 22:12:49 +00003148 /* Initialize MLD control interfaces early to allow external monitoring
3149 * of link setup operations. */
3150 if (interfaces->mld_ctrl_iface_init(mld))
3151 goto fail;
3152
Sunil Ravi99c035e2024-07-12 01:42:03 +00003153 hapd->mld = mld;
3154 hostapd_mld_ref_inc(mld);
3155 hostapd_bss_alloc_link_id(hapd);
3156
3157 all_mld = os_realloc_array(interfaces->mld, interfaces->mld_count + 1,
3158 sizeof(struct hostapd_mld *));
3159 if (!all_mld)
3160 goto fail;
3161
3162 interfaces->mld = all_mld;
3163 interfaces->mld[interfaces->mld_count] = mld;
3164 interfaces->mld_count++;
3165
3166 return;
3167fail:
3168 if (!mld)
3169 return;
3170
3171 wpa_printf(MSG_DEBUG, "AP MLD %s: free mld %p", mld->name, mld);
3172 os_free(mld);
3173 hapd->mld = NULL;
3174#endif /* CONFIG_IEEE80211BE */
3175}
3176
3177
3178static void hostapd_cleanup_unused_mlds(struct hapd_interfaces *interfaces)
3179{
3180#ifdef CONFIG_IEEE80211BE
3181 struct hostapd_mld *mld, **all_mld;
3182 size_t i, j, num_mlds;
3183 bool forced_remove, remove;
3184
3185 if (!interfaces->mld)
3186 return;
3187
3188 num_mlds = interfaces->mld_count;
3189
3190 for (i = 0; i < interfaces->mld_count; i++) {
3191 mld = interfaces->mld[i];
3192 if (!mld)
3193 continue;
3194
3195 remove = false;
3196 forced_remove = false;
3197
3198 if (!mld->refcount)
3199 remove = true;
3200
3201 /* If MLD is still being referenced but the number of interfaces
3202 * is zero, it is safe to force its deletion. Normally, this
3203 * should not happen but even if it does, let us free the
3204 * memory.
3205 */
3206 if (!remove && !interfaces->count)
3207 forced_remove = true;
3208
3209 if (!remove && !forced_remove)
3210 continue;
3211
Sunil Ravic0f5d412024-09-11 22:12:49 +00003212 interfaces->mld_ctrl_iface_deinit(mld);
3213
Sunil Ravi99c035e2024-07-12 01:42:03 +00003214 wpa_printf(MSG_DEBUG, "AP MLD %s: Freed%s", mld->name,
3215 forced_remove ? " (forced)" : "");
3216 os_free(mld);
3217 interfaces->mld[i] = NULL;
3218 num_mlds--;
3219 }
3220
3221 if (!num_mlds) {
3222 interfaces->mld_count = 0;
3223 os_free(interfaces->mld);
3224 interfaces->mld = NULL;
3225 return;
3226 }
3227
3228 all_mld = os_zalloc(num_mlds * sizeof(struct hostapd_mld *));
3229 if (!all_mld) {
3230 wpa_printf(MSG_ERROR,
3231 "AP MLD: Failed to re-allocate the MLDs. Expect issues");
3232 return;
3233 }
3234
3235 for (i = 0, j = 0; i < interfaces->mld_count; i++) {
3236 mld = interfaces->mld[i];
3237 if (!mld)
3238 continue;
3239
3240 all_mld[j++] = mld;
3241 }
3242
3243 /* This should not happen */
3244 if (j != num_mlds) {
3245 wpa_printf(MSG_DEBUG,
3246 "AP MLD: Some error occurred while reallocating MLDs. Expect issues.");
3247 os_free(all_mld);
3248 return;
3249 }
3250
3251 os_free(interfaces->mld);
3252 interfaces->mld = all_mld;
3253 interfaces->mld_count = num_mlds;
3254#endif /* CONFIG_IEEE80211BE */
3255}
3256
3257
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003258/**
3259 * hostapd_init - Allocate and initialize per-interface data
3260 * @config_file: Path to the configuration file
3261 * Returns: Pointer to the allocated interface data or %NULL on failure
3262 *
3263 * This function is used to allocate main data structures for per-interface
3264 * data. The allocated data buffer will be freed by calling
3265 * hostapd_cleanup_iface().
3266 */
3267struct hostapd_iface * hostapd_init(struct hapd_interfaces *interfaces,
3268 const char *config_file)
3269{
3270 struct hostapd_iface *hapd_iface = NULL;
3271 struct hostapd_config *conf = NULL;
3272 struct hostapd_data *hapd;
3273 size_t i;
3274
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07003275 hapd_iface = hostapd_alloc_iface();
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003276 if (hapd_iface == NULL)
3277 goto fail;
3278
3279 hapd_iface->config_fname = os_strdup(config_file);
3280 if (hapd_iface->config_fname == NULL)
3281 goto fail;
3282
3283 conf = interfaces->config_read_cb(hapd_iface->config_fname);
3284 if (conf == NULL)
3285 goto fail;
3286 hapd_iface->conf = conf;
3287
3288 hapd_iface->num_bss = conf->num_bss;
3289 hapd_iface->bss = os_calloc(conf->num_bss,
3290 sizeof(struct hostapd_data *));
3291 if (hapd_iface->bss == NULL)
3292 goto fail;
3293
3294 for (i = 0; i < conf->num_bss; i++) {
3295 hapd = hapd_iface->bss[i] =
3296 hostapd_alloc_bss_data(hapd_iface, conf,
3297 conf->bss[i]);
3298 if (hapd == NULL)
3299 goto fail;
3300 hapd->msg_ctx = hapd;
Sunil Ravi99c035e2024-07-12 01:42:03 +00003301 hostapd_bss_setup_multi_link(hapd, interfaces);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003302 }
3303
Sunil Ravi99c035e2024-07-12 01:42:03 +00003304 hapd_iface->is_ch_switch_dfs = false;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003305 return hapd_iface;
3306
3307fail:
3308 wpa_printf(MSG_ERROR, "Failed to set up interface with %s",
3309 config_file);
3310 if (conf)
3311 hostapd_config_free(conf);
3312 if (hapd_iface) {
3313 os_free(hapd_iface->config_fname);
3314 os_free(hapd_iface->bss);
Dmitry Shmidt54605472013-11-08 11:10:19 -08003315 wpa_printf(MSG_DEBUG, "%s: free iface %p",
3316 __func__, hapd_iface);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003317 os_free(hapd_iface);
3318 }
3319 return NULL;
3320}
3321
3322
3323static int ifname_in_use(struct hapd_interfaces *interfaces, const char *ifname)
3324{
3325 size_t i, j;
3326
3327 for (i = 0; i < interfaces->count; i++) {
3328 struct hostapd_iface *iface = interfaces->iface[i];
3329 for (j = 0; j < iface->num_bss; j++) {
3330 struct hostapd_data *hapd = iface->bss[j];
3331 if (os_strcmp(ifname, hapd->conf->iface) == 0)
3332 return 1;
3333 }
3334 }
3335
3336 return 0;
3337}
3338
3339
3340/**
3341 * hostapd_interface_init_bss - Read configuration file and init BSS data
3342 *
3343 * This function is used to parse configuration file for a BSS. This BSS is
3344 * added to an existing interface sharing the same radio (if any) or a new
3345 * interface is created if this is the first interface on a radio. This
3346 * allocate memory for the BSS. No actual driver operations are started.
3347 *
3348 * This is similar to hostapd_interface_init(), but for a case where the
3349 * configuration is used to add a single BSS instead of all BSSes for a radio.
3350 */
3351struct hostapd_iface *
3352hostapd_interface_init_bss(struct hapd_interfaces *interfaces, const char *phy,
3353 const char *config_fname, int debug)
3354{
3355 struct hostapd_iface *new_iface = NULL, *iface = NULL;
3356 struct hostapd_data *hapd;
3357 int k;
3358 size_t i, bss_idx;
3359
3360 if (!phy || !*phy)
3361 return NULL;
3362
3363 for (i = 0; i < interfaces->count; i++) {
3364 if (os_strcmp(interfaces->iface[i]->phy, phy) == 0) {
3365 iface = interfaces->iface[i];
3366 break;
3367 }
3368 }
3369
3370 wpa_printf(MSG_INFO, "Configuration file: %s (phy %s)%s",
3371 config_fname, phy, iface ? "" : " --> new PHY");
3372 if (iface) {
3373 struct hostapd_config *conf;
3374 struct hostapd_bss_config **tmp_conf;
3375 struct hostapd_data **tmp_bss;
3376 struct hostapd_bss_config *bss;
3377 const char *ifname;
3378
3379 /* Add new BSS to existing iface */
3380 conf = interfaces->config_read_cb(config_fname);
3381 if (conf == NULL)
3382 return NULL;
3383 if (conf->num_bss > 1) {
3384 wpa_printf(MSG_ERROR, "Multiple BSSes specified in BSS-config");
3385 hostapd_config_free(conf);
3386 return NULL;
3387 }
3388
3389 ifname = conf->bss[0]->iface;
3390 if (ifname[0] != '\0' && ifname_in_use(interfaces, ifname)) {
3391 wpa_printf(MSG_ERROR,
3392 "Interface name %s already in use", ifname);
3393 hostapd_config_free(conf);
3394 return NULL;
3395 }
3396
3397 tmp_conf = os_realloc_array(
3398 iface->conf->bss, iface->conf->num_bss + 1,
3399 sizeof(struct hostapd_bss_config *));
3400 tmp_bss = os_realloc_array(iface->bss, iface->num_bss + 1,
3401 sizeof(struct hostapd_data *));
3402 if (tmp_bss)
3403 iface->bss = tmp_bss;
3404 if (tmp_conf) {
3405 iface->conf->bss = tmp_conf;
3406 iface->conf->last_bss = tmp_conf[0];
3407 }
3408 if (tmp_bss == NULL || tmp_conf == NULL) {
3409 hostapd_config_free(conf);
3410 return NULL;
3411 }
3412 bss = iface->conf->bss[iface->conf->num_bss] = conf->bss[0];
3413 iface->conf->num_bss++;
3414
3415 hapd = hostapd_alloc_bss_data(iface, iface->conf, bss);
3416 if (hapd == NULL) {
3417 iface->conf->num_bss--;
3418 hostapd_config_free(conf);
3419 return NULL;
3420 }
3421 iface->conf->last_bss = bss;
3422 iface->bss[iface->num_bss] = hapd;
3423 hapd->msg_ctx = hapd;
Sunil Ravi99c035e2024-07-12 01:42:03 +00003424 hostapd_bss_setup_multi_link(hapd, interfaces);
3425
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003426
3427 bss_idx = iface->num_bss++;
3428 conf->num_bss--;
3429 conf->bss[0] = NULL;
3430 hostapd_config_free(conf);
3431 } else {
3432 /* Add a new iface with the first BSS */
3433 new_iface = iface = hostapd_init(interfaces, config_fname);
3434 if (!iface)
3435 return NULL;
3436 os_strlcpy(iface->phy, phy, sizeof(iface->phy));
3437 iface->interfaces = interfaces;
3438 bss_idx = 0;
3439 }
3440
3441 for (k = 0; k < debug; k++) {
3442 if (iface->bss[bss_idx]->conf->logger_stdout_level > 0)
3443 iface->bss[bss_idx]->conf->logger_stdout_level--;
3444 }
3445
3446 if (iface->conf->bss[bss_idx]->iface[0] == '\0' &&
3447 !hostapd_drv_none(iface->bss[bss_idx])) {
3448 wpa_printf(MSG_ERROR, "Interface name not specified in %s",
3449 config_fname);
3450 if (new_iface)
3451 hostapd_interface_deinit_free(new_iface);
3452 return NULL;
3453 }
3454
3455 return iface;
3456}
3457
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003458
Sunil Ravi99c035e2024-07-12 01:42:03 +00003459static void hostapd_cleanup_driver(const struct wpa_driver_ops *driver,
3460 void *drv_priv, struct hostapd_iface *iface)
3461{
Sunil Ravi99c035e2024-07-12 01:42:03 +00003462 if (!driver || !driver->hapd_deinit || !drv_priv)
3463 return;
3464
Sunil Ravi7f769292024-07-23 22:21:32 +00003465#ifdef CONFIG_IEEE80211BE
Sunil Ravi99c035e2024-07-12 01:42:03 +00003466 /* In case of non-ML operation, de-init. But if ML operation exist,
3467 * even if that's the last BSS in the interface, the driver (drv) could
3468 * be in use for a different AP MLD. Hence, need to check if drv is
3469 * still being used by some other BSS before de-initiallizing. */
3470 if (!iface->bss[0]->conf->mld_ap) {
3471 driver->hapd_deinit(drv_priv);
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00003472 } else if (driver->is_drv_shared &&
Sunil Ravic0f5d412024-09-11 22:12:49 +00003473 !driver->is_drv_shared(drv_priv,
3474 iface->bss[0]->mld_link_id)) {
Sunil Ravi99c035e2024-07-12 01:42:03 +00003475 driver->hapd_deinit(drv_priv);
Sunil Ravic0f5d412024-09-11 22:12:49 +00003476 hostapd_mld_interface_freed(iface->bss[0]);
Sunil Ravi99c035e2024-07-12 01:42:03 +00003477 } else if (hostapd_if_link_remove(iface->bss[0],
3478 WPA_IF_AP_BSS,
3479 iface->bss[0]->conf->iface,
3480 iface->bss[0]->mld_link_id)) {
3481 wpa_printf(MSG_WARNING, "Failed to remove BSS interface %s",
3482 iface->bss[0]->conf->iface);
3483 }
3484#else /* CONFIG_IEEE80211BE */
3485 driver->hapd_deinit(drv_priv);
3486#endif /* CONFIG_IEEE80211BE */
3487 iface->bss[0]->drv_priv = NULL;
3488}
3489
3490
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003491void hostapd_interface_deinit_free(struct hostapd_iface *iface)
3492{
3493 const struct wpa_driver_ops *driver;
3494 void *drv_priv;
Dmitry Shmidt54605472013-11-08 11:10:19 -08003495
3496 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003497 if (iface == NULL)
3498 return;
Dmitry Shmidt54605472013-11-08 11:10:19 -08003499 wpa_printf(MSG_DEBUG, "%s: num_bss=%u conf->num_bss=%u",
3500 __func__, (unsigned int) iface->num_bss,
3501 (unsigned int) iface->conf->num_bss);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003502 driver = iface->bss[0]->driver;
3503 drv_priv = iface->bss[0]->drv_priv;
3504 hostapd_interface_deinit(iface);
Dmitry Shmidt54605472013-11-08 11:10:19 -08003505 wpa_printf(MSG_DEBUG, "%s: driver=%p drv_priv=%p -> hapd_deinit",
3506 __func__, driver, drv_priv);
Sunil Ravi99c035e2024-07-12 01:42:03 +00003507 hostapd_cleanup_driver(driver, drv_priv, iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003508 hostapd_interface_free(iface);
3509}
3510
3511
Dmitry Shmidt15907092014-03-25 10:42:57 -07003512static void hostapd_deinit_driver(const struct wpa_driver_ops *driver,
3513 void *drv_priv,
3514 struct hostapd_iface *hapd_iface)
3515{
3516 size_t j;
3517
3518 wpa_printf(MSG_DEBUG, "%s: driver=%p drv_priv=%p -> hapd_deinit",
3519 __func__, driver, drv_priv);
Sunil Ravi99c035e2024-07-12 01:42:03 +00003520
3521 hostapd_cleanup_driver(driver, drv_priv, hapd_iface);
3522
Dmitry Shmidt15907092014-03-25 10:42:57 -07003523 if (driver && driver->hapd_deinit && drv_priv) {
Dmitry Shmidt15907092014-03-25 10:42:57 -07003524 for (j = 0; j < hapd_iface->num_bss; j++) {
3525 wpa_printf(MSG_DEBUG, "%s:bss[%d]->drv_priv=%p",
3526 __func__, (int) j,
3527 hapd_iface->bss[j]->drv_priv);
Hai Shalom1dc4d202019-04-29 16:22:27 -07003528 if (hapd_iface->bss[j]->drv_priv == drv_priv) {
Sunil Ravi99c035e2024-07-12 01:42:03 +00003529 hapd_iface->bss[j]->drv_priv = NULL;
Hai Shalom1dc4d202019-04-29 16:22:27 -07003530 hapd_iface->extended_capa = NULL;
3531 hapd_iface->extended_capa_mask = NULL;
3532 hapd_iface->extended_capa_len = 0;
3533 }
Dmitry Shmidt15907092014-03-25 10:42:57 -07003534 }
3535 }
3536}
3537
3538
Sunil Ravi99c035e2024-07-12 01:42:03 +00003539static void hostapd_refresh_all_iface_beacons(struct hostapd_iface *hapd_iface)
3540{
3541 size_t j;
3542
3543 if (!hapd_iface->interfaces || hapd_iface->interfaces->count <= 1)
3544 return;
3545
3546 for (j = 0; j < hapd_iface->interfaces->count; j++) {
3547 if (hapd_iface->interfaces->iface[j] == hapd_iface)
3548 continue;
3549
3550 ieee802_11_update_beacons(hapd_iface->interfaces->iface[j]);
3551 }
3552}
3553
3554
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003555int hostapd_enable_iface(struct hostapd_iface *hapd_iface)
3556{
Dmitry Shmidt71757432014-06-02 13:50:35 -07003557 size_t j;
3558
Hai Shalom60840252021-02-19 19:02:11 -08003559 if (!hapd_iface)
3560 return -1;
3561
3562 if (hapd_iface->enable_iface_cb)
3563 return hapd_iface->enable_iface_cb(hapd_iface);
3564
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003565 if (hapd_iface->bss[0]->drv_priv != NULL) {
3566 wpa_printf(MSG_ERROR, "Interface %s already enabled",
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003567 hapd_iface->conf->bss[0]->iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003568 return -1;
3569 }
3570
3571 wpa_printf(MSG_DEBUG, "Enable interface %s",
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003572 hapd_iface->conf->bss[0]->iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003573
Dmitry Shmidt71757432014-06-02 13:50:35 -07003574 for (j = 0; j < hapd_iface->num_bss; j++)
3575 hostapd_set_security_params(hapd_iface->conf->bss[j], 1);
Dmitry Shmidt344abd32014-01-14 13:17:00 -08003576 if (hostapd_config_check(hapd_iface->conf, 1) < 0) {
3577 wpa_printf(MSG_INFO, "Invalid configuration - cannot enable");
3578 return -1;
3579 }
3580
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003581 if (hapd_iface->interfaces == NULL ||
3582 hapd_iface->interfaces->driver_init == NULL ||
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003583 hapd_iface->interfaces->driver_init(hapd_iface))
3584 return -1;
3585
3586 if (hostapd_setup_interface(hapd_iface)) {
Dmitry Shmidt15907092014-03-25 10:42:57 -07003587 hostapd_deinit_driver(hapd_iface->bss[0]->driver,
3588 hapd_iface->bss[0]->drv_priv,
3589 hapd_iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003590 return -1;
3591 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003592
Sunil Ravi99c035e2024-07-12 01:42:03 +00003593 hostapd_refresh_all_iface_beacons(hapd_iface);
3594
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003595 return 0;
3596}
3597
3598
3599int hostapd_reload_iface(struct hostapd_iface *hapd_iface)
3600{
3601 size_t j;
3602
3603 wpa_printf(MSG_DEBUG, "Reload interface %s",
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003604 hapd_iface->conf->bss[0]->iface);
3605 for (j = 0; j < hapd_iface->num_bss; j++)
Dmitry Shmidt71757432014-06-02 13:50:35 -07003606 hostapd_set_security_params(hapd_iface->conf->bss[j], 1);
Dmitry Shmidt344abd32014-01-14 13:17:00 -08003607 if (hostapd_config_check(hapd_iface->conf, 1) < 0) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003608 wpa_printf(MSG_ERROR, "Updated configuration is invalid");
3609 return -1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003610 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003611 hostapd_clear_old(hapd_iface);
3612 for (j = 0; j < hapd_iface->num_bss; j++)
3613 hostapd_reload_bss(hapd_iface->bss[j]);
3614
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003615 return 0;
3616}
3617
3618
Sunil Ravi77d572f2023-01-17 23:58:31 +00003619int hostapd_reload_bss_only(struct hostapd_data *bss)
3620{
3621
3622 wpa_printf(MSG_DEBUG, "Reload BSS %s", bss->conf->iface);
3623 hostapd_set_security_params(bss->conf, 1);
3624 if (hostapd_config_check(bss->iconf, 1) < 0) {
3625 wpa_printf(MSG_ERROR, "Updated BSS configuration is invalid");
3626 return -1;
3627 }
3628 hostapd_clear_old_bss(bss);
3629 hostapd_reload_bss(bss);
3630 return 0;
3631}
3632
3633
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003634int hostapd_disable_iface(struct hostapd_iface *hapd_iface)
3635{
3636 size_t j;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003637
3638 if (hapd_iface == NULL)
3639 return -1;
Dmitry Shmidt71757432014-06-02 13:50:35 -07003640
Hai Shalom60840252021-02-19 19:02:11 -08003641 if (hapd_iface->disable_iface_cb)
3642 return hapd_iface->disable_iface_cb(hapd_iface);
3643
Dmitry Shmidt71757432014-06-02 13:50:35 -07003644 if (hapd_iface->bss[0]->drv_priv == NULL) {
3645 wpa_printf(MSG_INFO, "Interface %s already disabled",
3646 hapd_iface->conf->bss[0]->iface);
3647 return -1;
3648 }
3649
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003650 wpa_msg(hapd_iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003651
Dmitry Shmidta38abf92014-03-06 13:38:44 -08003652 hapd_iface->driver_ap_teardown =
3653 !!(hapd_iface->drv_flags &
3654 WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
3655
Hai Shalom39ba6fc2019-01-22 12:40:38 -08003656#ifdef NEED_AP_MLME
3657 for (j = 0; j < hapd_iface->num_bss; j++)
3658 hostapd_cleanup_cs_params(hapd_iface->bss[j]);
3659#endif /* NEED_AP_MLME */
3660
Dmitry Shmidta38abf92014-03-06 13:38:44 -08003661 /* same as hostapd_interface_deinit without deinitializing ctrl-iface */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003662 for (j = 0; j < hapd_iface->num_bss; j++) {
3663 struct hostapd_data *hapd = hapd_iface->bss[j];
Dmitry Shmidt71757432014-06-02 13:50:35 -07003664 hostapd_bss_deinit_no_free(hapd);
Sunil Ravi99c035e2024-07-12 01:42:03 +00003665 hostapd_bss_link_deinit(hapd);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003666 hostapd_free_hapd_data(hapd);
3667 }
3668
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00003669 hostapd_deinit_driver(hapd_iface->bss[0]->driver,
3670 hapd_iface->bss[0]->drv_priv, hapd_iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003671
3672 /* From hostapd_cleanup_iface: These were initialized in
3673 * hostapd_setup_interface and hostapd_setup_interface_complete
3674 */
3675 hostapd_cleanup_iface_partial(hapd_iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003676
Dmitry Shmidt56052862013-10-04 10:23:25 -07003677 wpa_printf(MSG_DEBUG, "Interface %s disabled",
3678 hapd_iface->bss[0]->conf->iface);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003679 hostapd_set_state(hapd_iface, HAPD_IFACE_DISABLED);
Sunil Ravi99c035e2024-07-12 01:42:03 +00003680 hostapd_refresh_all_iface_beacons(hapd_iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003681 return 0;
3682}
3683
3684
3685static struct hostapd_iface *
3686hostapd_iface_alloc(struct hapd_interfaces *interfaces)
3687{
3688 struct hostapd_iface **iface, *hapd_iface;
3689
3690 iface = os_realloc_array(interfaces->iface, interfaces->count + 1,
3691 sizeof(struct hostapd_iface *));
3692 if (iface == NULL)
3693 return NULL;
3694 interfaces->iface = iface;
3695 hapd_iface = interfaces->iface[interfaces->count] =
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07003696 hostapd_alloc_iface();
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003697 if (hapd_iface == NULL) {
3698 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory for "
3699 "the interface", __func__);
3700 return NULL;
3701 }
3702 interfaces->count++;
3703 hapd_iface->interfaces = interfaces;
3704
3705 return hapd_iface;
3706}
3707
3708
3709static struct hostapd_config *
3710hostapd_config_alloc(struct hapd_interfaces *interfaces, const char *ifname,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003711 const char *ctrl_iface, const char *driver)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003712{
3713 struct hostapd_bss_config *bss;
3714 struct hostapd_config *conf;
3715
3716 /* Allocates memory for bss and conf */
3717 conf = hostapd_config_defaults();
3718 if (conf == NULL) {
3719 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory for "
3720 "configuration", __func__);
Hai Shalom74f70d42019-02-11 14:42:39 -08003721 return NULL;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003722 }
3723
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003724 if (driver) {
3725 int j;
3726
3727 for (j = 0; wpa_drivers[j]; j++) {
3728 if (os_strcmp(driver, wpa_drivers[j]->name) == 0) {
3729 conf->driver = wpa_drivers[j];
3730 goto skip;
3731 }
3732 }
3733
3734 wpa_printf(MSG_ERROR,
3735 "Invalid/unknown driver '%s' - registering the default driver",
3736 driver);
3737 }
3738
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003739 conf->driver = wpa_drivers[0];
3740 if (conf->driver == NULL) {
3741 wpa_printf(MSG_ERROR, "No driver wrappers registered!");
3742 hostapd_config_free(conf);
3743 return NULL;
3744 }
3745
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003746skip:
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003747 bss = conf->last_bss = conf->bss[0];
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003748
3749 os_strlcpy(bss->iface, ifname, sizeof(bss->iface));
3750 bss->ctrl_interface = os_strdup(ctrl_iface);
3751 if (bss->ctrl_interface == NULL) {
3752 hostapd_config_free(conf);
3753 return NULL;
3754 }
3755
3756 /* Reading configuration file skipped, will be done in SET!
3757 * From reading the configuration till the end has to be done in
3758 * SET
3759 */
3760 return conf;
3761}
3762
3763
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003764static int hostapd_data_alloc(struct hostapd_iface *hapd_iface,
3765 struct hostapd_config *conf)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003766{
3767 size_t i;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003768 struct hostapd_data *hapd;
3769
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003770 hapd_iface->bss = os_calloc(conf->num_bss,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003771 sizeof(struct hostapd_data *));
3772 if (hapd_iface->bss == NULL)
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003773 return -1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003774
3775 for (i = 0; i < conf->num_bss; i++) {
3776 hapd = hapd_iface->bss[i] =
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003777 hostapd_alloc_bss_data(hapd_iface, conf, conf->bss[i]);
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003778 if (hapd == NULL) {
3779 while (i > 0) {
3780 i--;
3781 os_free(hapd_iface->bss[i]);
3782 hapd_iface->bss[i] = NULL;
3783 }
3784 os_free(hapd_iface->bss);
3785 hapd_iface->bss = NULL;
3786 return -1;
3787 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003788 hapd->msg_ctx = hapd;
Sunil Ravi99c035e2024-07-12 01:42:03 +00003789 hostapd_bss_setup_multi_link(hapd, hapd_iface->interfaces);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003790 }
3791
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003792 hapd_iface->conf = conf;
3793 hapd_iface->num_bss = conf->num_bss;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003794
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003795 return 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003796}
3797
3798
3799int hostapd_add_iface(struct hapd_interfaces *interfaces, char *buf)
3800{
3801 struct hostapd_config *conf = NULL;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003802 struct hostapd_iface *hapd_iface = NULL, *new_iface = NULL;
3803 struct hostapd_data *hapd;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003804 char *ptr;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003805 size_t i, j;
3806 const char *conf_file = NULL, *phy_name = NULL;
3807
3808 if (os_strncmp(buf, "bss_config=", 11) == 0) {
3809 char *pos;
3810 phy_name = buf + 11;
3811 pos = os_strchr(phy_name, ':');
3812 if (!pos)
3813 return -1;
3814 *pos++ = '\0';
3815 conf_file = pos;
3816 if (!os_strlen(conf_file))
3817 return -1;
3818
3819 hapd_iface = hostapd_interface_init_bss(interfaces, phy_name,
3820 conf_file, 0);
3821 if (!hapd_iface)
3822 return -1;
3823 for (j = 0; j < interfaces->count; j++) {
3824 if (interfaces->iface[j] == hapd_iface)
3825 break;
3826 }
3827 if (j == interfaces->count) {
3828 struct hostapd_iface **tmp;
3829 tmp = os_realloc_array(interfaces->iface,
3830 interfaces->count + 1,
3831 sizeof(struct hostapd_iface *));
3832 if (!tmp) {
3833 hostapd_interface_deinit_free(hapd_iface);
3834 return -1;
3835 }
3836 interfaces->iface = tmp;
3837 interfaces->iface[interfaces->count++] = hapd_iface;
3838 new_iface = hapd_iface;
3839 }
3840
3841 if (new_iface) {
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003842 if (interfaces->driver_init(hapd_iface))
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003843 goto fail;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003844
3845 if (hostapd_setup_interface(hapd_iface)) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003846 hostapd_deinit_driver(
3847 hapd_iface->bss[0]->driver,
3848 hapd_iface->bss[0]->drv_priv,
3849 hapd_iface);
3850 goto fail;
3851 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003852 } else {
3853 /* Assign new BSS with bss[0]'s driver info */
3854 hapd = hapd_iface->bss[hapd_iface->num_bss - 1];
3855 hapd->driver = hapd_iface->bss[0]->driver;
3856 hapd->drv_priv = hapd_iface->bss[0]->drv_priv;
3857 os_memcpy(hapd->own_addr, hapd_iface->bss[0]->own_addr,
3858 ETH_ALEN);
3859
3860 if (start_ctrl_iface_bss(hapd) < 0 ||
Dmitry Shmidt54605472013-11-08 11:10:19 -08003861 (hapd_iface->state == HAPD_IFACE_ENABLED &&
Sunil Ravi77d572f2023-01-17 23:58:31 +00003862 hostapd_setup_bss(hapd, -1, true))) {
Sunil Ravi99c035e2024-07-12 01:42:03 +00003863 hostapd_bss_link_deinit(hapd);
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07003864 hostapd_cleanup(hapd);
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08003865 hapd_iface->bss[hapd_iface->num_bss - 1] = NULL;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003866 hapd_iface->conf->num_bss--;
3867 hapd_iface->num_bss--;
Dmitry Shmidt54605472013-11-08 11:10:19 -08003868 wpa_printf(MSG_DEBUG, "%s: free hapd %p %s",
3869 __func__, hapd, hapd->conf->iface);
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003870 hostapd_config_free_bss(hapd->conf);
3871 hapd->conf = NULL;
Sunil Ravi99c035e2024-07-12 01:42:03 +00003872#ifdef CONFIG_IEEE80211BE
3873 hostapd_mld_ref_dec(hapd->mld);
3874#endif /* CONFIG_IEEE80211BE */
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003875 os_free(hapd);
3876 return -1;
3877 }
3878 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003879 hostapd_owe_update_trans(hapd_iface);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003880 return 0;
3881 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003882
3883 ptr = os_strchr(buf, ' ');
3884 if (ptr == NULL)
3885 return -1;
3886 *ptr++ = '\0';
3887
Dmitry Shmidt56052862013-10-04 10:23:25 -07003888 if (os_strncmp(ptr, "config=", 7) == 0)
3889 conf_file = ptr + 7;
3890
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003891 for (i = 0; i < interfaces->count; i++) {
Sunil Ravi2a14cf12023-11-21 00:54:38 +00003892 bool mld_ap = false;
3893
3894#ifdef CONFIG_IEEE80211BE
3895 mld_ap = interfaces->iface[i]->conf->bss[0]->mld_ap;
3896#endif /* CONFIG_IEEE80211BE */
3897
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003898 if (!os_strcmp(interfaces->iface[i]->conf->bss[0]->iface,
Sunil Ravi2a14cf12023-11-21 00:54:38 +00003899 buf) && !mld_ap) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003900 wpa_printf(MSG_INFO, "Cannot add interface - it "
3901 "already exists");
3902 return -1;
3903 }
3904 }
3905
3906 hapd_iface = hostapd_iface_alloc(interfaces);
3907 if (hapd_iface == NULL) {
3908 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
3909 "for interface", __func__);
3910 goto fail;
3911 }
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003912 new_iface = hapd_iface;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003913
Dmitry Shmidt56052862013-10-04 10:23:25 -07003914 if (conf_file && interfaces->config_read_cb) {
3915 conf = interfaces->config_read_cb(conf_file);
3916 if (conf && conf->bss)
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003917 os_strlcpy(conf->bss[0]->iface, buf,
3918 sizeof(conf->bss[0]->iface));
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003919 } else {
3920 char *driver = os_strchr(ptr, ' ');
3921
3922 if (driver)
3923 *driver++ = '\0';
3924 conf = hostapd_config_alloc(interfaces, buf, ptr, driver);
3925 }
3926
Dmitry Shmidt56052862013-10-04 10:23:25 -07003927 if (conf == NULL || conf->bss == NULL) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003928 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
3929 "for configuration", __func__);
3930 goto fail;
3931 }
3932
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003933 if (hostapd_data_alloc(hapd_iface, conf) < 0) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003934 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
3935 "for hostapd", __func__);
3936 goto fail;
3937 }
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003938 conf = NULL;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003939
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003940 if (start_ctrl_iface(hapd_iface) < 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003941 goto fail;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003942
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003943 wpa_printf(MSG_INFO, "Add interface '%s'",
3944 hapd_iface->conf->bss[0]->iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003945
3946 return 0;
3947
3948fail:
3949 if (conf)
3950 hostapd_config_free(conf);
3951 if (hapd_iface) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003952 if (hapd_iface->bss) {
Dmitry Shmidt54605472013-11-08 11:10:19 -08003953 for (i = 0; i < hapd_iface->num_bss; i++) {
3954 hapd = hapd_iface->bss[i];
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08003955 if (!hapd)
3956 continue;
3957 if (hapd_iface->interfaces &&
Dmitry Shmidt54605472013-11-08 11:10:19 -08003958 hapd_iface->interfaces->ctrl_iface_deinit)
3959 hapd_iface->interfaces->
3960 ctrl_iface_deinit(hapd);
3961 wpa_printf(MSG_DEBUG, "%s: free hapd %p (%s)",
3962 __func__, hapd_iface->bss[i],
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08003963 hapd->conf->iface);
Sunil Ravi99c035e2024-07-12 01:42:03 +00003964 hostapd_bss_link_deinit(hapd);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003965 hostapd_cleanup(hapd);
Sunil Ravi99c035e2024-07-12 01:42:03 +00003966#ifdef CONFIG_IEEE80211BE
3967 hostapd_mld_ref_dec(hapd->mld);
3968#endif /* CONFIG_IEEE80211BE */
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08003969 os_free(hapd);
3970 hapd_iface->bss[i] = NULL;
Dmitry Shmidt54605472013-11-08 11:10:19 -08003971 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003972 os_free(hapd_iface->bss);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003973 hapd_iface->bss = NULL;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003974 }
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003975 if (new_iface) {
3976 interfaces->count--;
3977 interfaces->iface[interfaces->count] = NULL;
Sunil Ravi99c035e2024-07-12 01:42:03 +00003978 hostapd_cleanup_unused_mlds(interfaces);
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003979 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003980 hostapd_cleanup_iface(hapd_iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003981 }
3982 return -1;
3983}
3984
3985
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003986static int hostapd_remove_bss(struct hostapd_iface *iface, unsigned int idx)
3987{
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003988 size_t i;
3989
Dmitry Shmidt54605472013-11-08 11:10:19 -08003990 wpa_printf(MSG_INFO, "Remove BSS '%s'", iface->conf->bss[idx]->iface);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003991
Dmitry Shmidt54605472013-11-08 11:10:19 -08003992 /* Remove hostapd_data only if it has already been initialized */
3993 if (idx < iface->num_bss) {
3994 struct hostapd_data *hapd = iface->bss[idx];
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003995
Dmitry Shmidt54605472013-11-08 11:10:19 -08003996 hostapd_bss_deinit(hapd);
3997 wpa_printf(MSG_DEBUG, "%s: free hapd %p (%s)",
3998 __func__, hapd, hapd->conf->iface);
3999 hostapd_config_free_bss(hapd->conf);
Dmitry Shmidtff787d52015-01-12 13:01:47 -08004000 hapd->conf = NULL;
Sunil Ravi99c035e2024-07-12 01:42:03 +00004001#ifdef CONFIG_IEEE80211BE
4002 hostapd_mld_ref_dec(hapd->mld);
4003#endif /* CONFIG_IEEE80211BE */
Dmitry Shmidt54605472013-11-08 11:10:19 -08004004 os_free(hapd);
4005
4006 iface->num_bss--;
4007
4008 for (i = idx; i < iface->num_bss; i++)
4009 iface->bss[i] = iface->bss[i + 1];
4010 } else {
4011 hostapd_config_free_bss(iface->conf->bss[idx]);
4012 iface->conf->bss[idx] = NULL;
4013 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004014
4015 iface->conf->num_bss--;
Dmitry Shmidt54605472013-11-08 11:10:19 -08004016 for (i = idx; i < iface->conf->num_bss; i++)
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004017 iface->conf->bss[i] = iface->conf->bss[i + 1];
4018
4019 return 0;
4020}
4021
4022
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004023int hostapd_remove_iface(struct hapd_interfaces *interfaces, char *buf)
4024{
4025 struct hostapd_iface *hapd_iface;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004026 size_t i, j, k = 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004027
4028 for (i = 0; i < interfaces->count; i++) {
4029 hapd_iface = interfaces->iface[i];
4030 if (hapd_iface == NULL)
4031 return -1;
Dmitry Shmidt54605472013-11-08 11:10:19 -08004032 if (!os_strcmp(hapd_iface->conf->bss[0]->iface, buf)) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004033 wpa_printf(MSG_INFO, "Remove interface '%s'", buf);
Dmitry Shmidta38abf92014-03-06 13:38:44 -08004034 hapd_iface->driver_ap_teardown =
4035 !!(hapd_iface->drv_flags &
4036 WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
4037
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004038 hostapd_interface_deinit_free(hapd_iface);
4039 k = i;
4040 while (k < (interfaces->count - 1)) {
4041 interfaces->iface[k] =
4042 interfaces->iface[k + 1];
4043 k++;
4044 }
4045 interfaces->count--;
Sunil Ravi99c035e2024-07-12 01:42:03 +00004046 hostapd_cleanup_unused_mlds(interfaces);
4047
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004048 return 0;
4049 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004050
4051 for (j = 0; j < hapd_iface->conf->num_bss; j++) {
Dmitry Shmidta38abf92014-03-06 13:38:44 -08004052 if (!os_strcmp(hapd_iface->conf->bss[j]->iface, buf)) {
4053 hapd_iface->driver_ap_teardown =
4054 !(hapd_iface->drv_flags &
4055 WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004056 return hostapd_remove_bss(hapd_iface, j);
Dmitry Shmidta38abf92014-03-06 13:38:44 -08004057 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004058 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004059 }
4060 return -1;
4061}
4062
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004063
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004064/**
4065 * hostapd_new_assoc_sta - Notify that a new station associated with the AP
4066 * @hapd: Pointer to BSS data
4067 * @sta: Pointer to the associated STA data
4068 * @reassoc: 1 to indicate this was a re-association; 0 = first association
4069 *
4070 * This function will be called whenever a station associates with the AP. It
4071 * can be called from ieee802_11.c for drivers that export MLME to hostapd and
4072 * from drv_callbacks.c based on driver events for drivers that take care of
4073 * management frames (IEEE 802.11 authentication and association) internally.
4074 */
4075void hostapd_new_assoc_sta(struct hostapd_data *hapd, struct sta_info *sta,
4076 int reassoc)
4077{
4078 if (hapd->tkip_countermeasures) {
4079 hostapd_drv_sta_deauth(hapd, sta->addr,
4080 WLAN_REASON_MICHAEL_MIC_FAILURE);
4081 return;
4082 }
4083
Sunil Ravi2a14cf12023-11-21 00:54:38 +00004084#ifdef CONFIG_IEEE80211BE
Sunil Ravib0ac25f2024-07-12 01:42:03 +00004085 if (ap_sta_is_mld(hapd, sta) &&
Sunil Ravi2a14cf12023-11-21 00:54:38 +00004086 sta->mld_assoc_link_id != hapd->mld_link_id)
4087 return;
4088#endif /* CONFIG_IEEE80211BE */
4089
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -08004090 ap_sta_clear_disconnect_timeouts(hapd, sta);
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00004091 ap_sta_clear_assoc_timeout(hapd, sta);
Hai Shalom899fcc72020-10-19 14:38:18 -07004092 sta->post_csa_sa_query = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004093
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004094#ifdef CONFIG_P2P
4095 if (sta->p2p_ie == NULL && !sta->no_p2p_set) {
4096 sta->no_p2p_set = 1;
4097 hapd->num_sta_no_p2p++;
4098 if (hapd->num_sta_no_p2p == 1)
4099 hostapd_p2p_non_p2p_sta_connected(hapd);
4100 }
4101#endif /* CONFIG_P2P */
4102
Hai Shalom81f62d82019-07-22 12:10:00 -07004103 airtime_policy_new_sta(hapd, sta);
4104
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004105 /* Start accounting here, if IEEE 802.1X and WPA are not used.
4106 * IEEE 802.1X/WPA code will start accounting after the station has
4107 * been authorized. */
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -08004108 if (!hapd->conf->ieee802_1x && !hapd->conf->wpa && !hapd->conf->osen) {
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00004109 if (ap_sta_set_authorized(hapd, sta, 1)) {
4110 /* Update driver authorized flag for the STA to cover
4111 * the case where AP SME is in the driver and there is
4112 * no separate event for handling TX status event for
4113 * the (Re)Association Response frame. */
4114 hostapd_set_sta_flags(hapd, sta);
4115 }
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08004116 os_get_reltime(&sta->connected_time);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004117 accounting_sta_start(hapd, sta);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004118 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004119
4120 /* Start IEEE 802.1X authentication process for new stations */
4121 ieee802_1x_new_station(hapd, sta);
4122 if (reassoc) {
4123 if (sta->auth_alg != WLAN_AUTH_FT &&
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004124 sta->auth_alg != WLAN_AUTH_FILS_SK &&
4125 sta->auth_alg != WLAN_AUTH_FILS_SK_PFS &&
4126 sta->auth_alg != WLAN_AUTH_FILS_PK &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004127 !(sta->flags & (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS)))
4128 wpa_auth_sm_event(sta->wpa_sm, WPA_REAUTH);
Sunil Ravi2a14cf12023-11-21 00:54:38 +00004129 } else if (!(hapd->iface->drv_flags2 &
4130 WPA_DRIVER_FLAGS2_4WAY_HANDSHAKE_AP_PSK)) {
4131 /* The 4-way handshake offloaded case will have this handled
4132 * based on the port authorized event. */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004133 wpa_auth_sta_associated(hapd->wpa_auth, sta->wpa_sm);
Sunil Ravi2a14cf12023-11-21 00:54:38 +00004134 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07004135
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08004136 if (hapd->iface->drv_flags & WPA_DRIVER_FLAGS_WIRED) {
4137 if (eloop_cancel_timeout(ap_handle_timer, hapd, sta) > 0) {
4138 wpa_printf(MSG_DEBUG,
4139 "%s: %s: canceled wired ap_handle_timer timeout for "
4140 MACSTR,
4141 hapd->conf->iface, __func__,
4142 MAC2STR(sta->addr));
4143 }
4144 } else if (!(hapd->iface->drv_flags &
4145 WPA_DRIVER_FLAGS_INACTIVITY_TIMER)) {
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -08004146 wpa_printf(MSG_DEBUG,
4147 "%s: %s: reschedule ap_handle_timer timeout for "
4148 MACSTR " (%d seconds - ap_max_inactivity)",
4149 hapd->conf->iface, __func__, MAC2STR(sta->addr),
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08004150 hapd->conf->ap_max_inactivity);
4151 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
4152 eloop_register_timeout(hapd->conf->ap_max_inactivity, 0,
4153 ap_handle_timer, hapd, sta);
4154 }
Hai Shalom81f62d82019-07-22 12:10:00 -07004155
4156#ifdef CONFIG_MACSEC
4157 if (hapd->conf->wpa_key_mgmt == WPA_KEY_MGMT_NONE &&
4158 hapd->conf->mka_psk_set)
4159 ieee802_1x_create_preshared_mka_hapd(hapd, sta);
4160 else
4161 ieee802_1x_alloc_kay_sm_hapd(hapd, sta);
4162#endif /* CONFIG_MACSEC */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004163}
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004164
4165
4166const char * hostapd_state_text(enum hostapd_iface_state s)
4167{
4168 switch (s) {
4169 case HAPD_IFACE_UNINITIALIZED:
4170 return "UNINITIALIZED";
4171 case HAPD_IFACE_DISABLED:
4172 return "DISABLED";
4173 case HAPD_IFACE_COUNTRY_UPDATE:
4174 return "COUNTRY_UPDATE";
4175 case HAPD_IFACE_ACS:
4176 return "ACS";
4177 case HAPD_IFACE_HT_SCAN:
4178 return "HT_SCAN";
4179 case HAPD_IFACE_DFS:
4180 return "DFS";
4181 case HAPD_IFACE_ENABLED:
4182 return "ENABLED";
Sunil Ravi2a14cf12023-11-21 00:54:38 +00004183 case HAPD_IFACE_NO_IR:
4184 return "NO_IR";
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004185 }
4186
4187 return "UNKNOWN";
4188}
4189
4190
4191void hostapd_set_state(struct hostapd_iface *iface, enum hostapd_iface_state s)
4192{
4193 wpa_printf(MSG_INFO, "%s: interface state %s->%s",
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07004194 iface->conf ? iface->conf->bss[0]->iface : "N/A",
4195 hostapd_state_text(iface->state), hostapd_state_text(s));
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004196 iface->state = s;
4197}
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004198
4199
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004200int hostapd_csa_in_progress(struct hostapd_iface *iface)
4201{
4202 unsigned int i;
4203
4204 for (i = 0; i < iface->num_bss; i++)
4205 if (iface->bss[i]->csa_in_progress)
4206 return 1;
4207 return 0;
4208}
4209
4210
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004211#ifdef NEED_AP_MLME
4212
Sunil Ravi7f769292024-07-23 22:21:32 +00004213void free_beacon_data(struct beacon_data *beacon)
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004214{
4215 os_free(beacon->head);
4216 beacon->head = NULL;
4217 os_free(beacon->tail);
4218 beacon->tail = NULL;
4219 os_free(beacon->probe_resp);
4220 beacon->probe_resp = NULL;
4221 os_free(beacon->beacon_ies);
4222 beacon->beacon_ies = NULL;
4223 os_free(beacon->proberesp_ies);
4224 beacon->proberesp_ies = NULL;
4225 os_free(beacon->assocresp_ies);
4226 beacon->assocresp_ies = NULL;
4227}
4228
4229
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00004230int hostapd_build_beacon_data(struct hostapd_data *hapd,
4231 struct beacon_data *beacon)
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004232{
4233 struct wpabuf *beacon_extra, *proberesp_extra, *assocresp_extra;
4234 struct wpa_driver_ap_params params;
4235 int ret;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004236
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08004237 os_memset(beacon, 0, sizeof(*beacon));
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004238 ret = ieee802_11_build_ap_params(hapd, &params);
4239 if (ret < 0)
4240 return ret;
4241
4242 ret = hostapd_build_ap_extra_ies(hapd, &beacon_extra,
4243 &proberesp_extra,
4244 &assocresp_extra);
4245 if (ret)
4246 goto free_ap_params;
4247
4248 ret = -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004249 beacon->head = os_memdup(params.head, params.head_len);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004250 if (!beacon->head)
4251 goto free_ap_extra_ies;
4252
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004253 beacon->head_len = params.head_len;
4254
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004255 beacon->tail = os_memdup(params.tail, params.tail_len);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004256 if (!beacon->tail)
4257 goto free_beacon;
4258
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004259 beacon->tail_len = params.tail_len;
4260
4261 if (params.proberesp != NULL) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004262 beacon->probe_resp = os_memdup(params.proberesp,
4263 params.proberesp_len);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004264 if (!beacon->probe_resp)
4265 goto free_beacon;
4266
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004267 beacon->probe_resp_len = params.proberesp_len;
4268 }
4269
4270 /* copy the extra ies */
4271 if (beacon_extra) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004272 beacon->beacon_ies = os_memdup(beacon_extra->buf,
4273 wpabuf_len(beacon_extra));
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004274 if (!beacon->beacon_ies)
4275 goto free_beacon;
4276
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004277 beacon->beacon_ies_len = wpabuf_len(beacon_extra);
4278 }
4279
4280 if (proberesp_extra) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004281 beacon->proberesp_ies = os_memdup(proberesp_extra->buf,
4282 wpabuf_len(proberesp_extra));
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004283 if (!beacon->proberesp_ies)
4284 goto free_beacon;
4285
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004286 beacon->proberesp_ies_len = wpabuf_len(proberesp_extra);
4287 }
4288
4289 if (assocresp_extra) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004290 beacon->assocresp_ies = os_memdup(assocresp_extra->buf,
4291 wpabuf_len(assocresp_extra));
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004292 if (!beacon->assocresp_ies)
4293 goto free_beacon;
4294
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004295 beacon->assocresp_ies_len = wpabuf_len(assocresp_extra);
4296 }
4297
4298 ret = 0;
4299free_beacon:
4300 /* if the function fails, the caller should not free beacon data */
4301 if (ret)
4302 free_beacon_data(beacon);
4303
4304free_ap_extra_ies:
4305 hostapd_free_ap_extra_ies(hapd, beacon_extra, proberesp_extra,
4306 assocresp_extra);
4307free_ap_params:
4308 ieee802_11_free_ap_params(&params);
4309 return ret;
4310}
4311
4312
4313/*
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004314 * TODO: This flow currently supports only changing channel and width within
4315 * the same hw_mode. Any other changes to MAC parameters or provided settings
4316 * are not supported.
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004317 */
4318static int hostapd_change_config_freq(struct hostapd_data *hapd,
4319 struct hostapd_config *conf,
4320 struct hostapd_freq_params *params,
4321 struct hostapd_freq_params *old_params)
4322{
4323 int channel;
Sunil Ravib0ac25f2024-07-12 01:42:03 +00004324 u8 seg0 = 0, seg1 = 0;
Hai Shalom81f62d82019-07-22 12:10:00 -07004325 struct hostapd_hw_modes *mode;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004326
4327 if (!params->channel) {
4328 /* check if the new channel is supported by hw */
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004329 params->channel = hostapd_hw_get_channel(hapd, params->freq);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004330 }
4331
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004332 channel = params->channel;
4333 if (!channel)
4334 return -1;
4335
Sunil Ravi2a14cf12023-11-21 00:54:38 +00004336 hostapd_determine_mode(hapd->iface);
Hai Shalom81f62d82019-07-22 12:10:00 -07004337 mode = hapd->iface->current_mode;
4338
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004339 /* if a pointer to old_params is provided we save previous state */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004340 if (old_params &&
4341 hostapd_set_freq_params(old_params, conf->hw_mode,
4342 hostapd_hw_get_freq(hapd, conf->channel),
Hai Shalomc3565922019-10-28 11:58:20 -07004343 conf->channel, conf->enable_edmg,
4344 conf->edmg_channel, conf->ieee80211n,
Hai Shalom81f62d82019-07-22 12:10:00 -07004345 conf->ieee80211ac, conf->ieee80211ax,
Sunil Ravia04bd252022-05-02 22:54:18 -07004346 conf->ieee80211be, conf->secondary_channel,
Hai Shalom81f62d82019-07-22 12:10:00 -07004347 hostapd_get_oper_chwidth(conf),
4348 hostapd_get_oper_centr_freq_seg0_idx(conf),
4349 hostapd_get_oper_centr_freq_seg1_idx(conf),
4350 conf->vht_capab,
4351 mode ? &mode->he_capab[IEEE80211_MODE_AP] :
Sunil Ravia04bd252022-05-02 22:54:18 -07004352 NULL,
4353 mode ? &mode->eht_capab[IEEE80211_MODE_AP] :
Sunil Ravi99c035e2024-07-12 01:42:03 +00004354 NULL,
4355 hostapd_get_punct_bitmap(hapd)))
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004356 return -1;
4357
4358 switch (params->bandwidth) {
4359 case 0:
4360 case 20:
Hai Shaloma20dcd72022-02-04 13:43:00 -08004361 conf->ht_capab &= ~HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET;
4362 break;
4363 case 40:
4364 case 80:
4365 case 160:
Sunil Ravi640215c2023-06-28 23:08:09 +00004366 case 320:
Hai Shaloma20dcd72022-02-04 13:43:00 -08004367 conf->ht_capab |= HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET;
4368 break;
4369 default:
4370 return -1;
4371 }
4372
4373 switch (params->bandwidth) {
4374 case 0:
4375 case 20:
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004376 case 40:
Sunil8cd6f4d2022-06-28 18:40:46 +00004377 hostapd_set_oper_chwidth(conf, CONF_OPER_CHWIDTH_USE_HT);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004378 break;
4379 case 80:
4380 if (params->center_freq2)
Sunil8cd6f4d2022-06-28 18:40:46 +00004381 hostapd_set_oper_chwidth(conf,
4382 CONF_OPER_CHWIDTH_80P80MHZ);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004383 else
Sunil8cd6f4d2022-06-28 18:40:46 +00004384 hostapd_set_oper_chwidth(conf,
4385 CONF_OPER_CHWIDTH_80MHZ);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004386 break;
4387 case 160:
Sunil8cd6f4d2022-06-28 18:40:46 +00004388 hostapd_set_oper_chwidth(conf, CONF_OPER_CHWIDTH_160MHZ);
4389 break;
4390 case 320:
4391 hostapd_set_oper_chwidth(conf, CONF_OPER_CHWIDTH_320MHZ);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004392 break;
4393 default:
4394 return -1;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004395 }
4396
4397 conf->channel = channel;
4398 conf->ieee80211n = params->ht_enabled;
Hai Shaloma20dcd72022-02-04 13:43:00 -08004399 conf->ieee80211ac = params->vht_enabled;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004400 conf->secondary_channel = params->sec_channel_offset;
Sunil Ravib0ac25f2024-07-12 01:42:03 +00004401 if (params->center_freq1 &&
4402 ieee80211_freq_to_chan(params->center_freq1, &seg0) ==
4403 NUM_HOSTAPD_MODES)
4404 return -1;
4405 if (params->center_freq2 &&
4406 ieee80211_freq_to_chan(params->center_freq2,
4407 &seg1) == NUM_HOSTAPD_MODES)
4408 return -1;
Hai Shalom81f62d82019-07-22 12:10:00 -07004409 hostapd_set_oper_centr_freq_seg0_idx(conf, seg0);
4410 hostapd_set_oper_centr_freq_seg1_idx(conf, seg1);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004411
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00004412#ifdef CONFIG_IEEE80211BE
4413 conf->punct_bitmap = params->punct_bitmap;
4414#endif /* CONFIG_IEEE80211BE */
4415
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004416 /* TODO: maybe call here hostapd_config_check here? */
4417
4418 return 0;
4419}
4420
4421
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004422static int hostapd_fill_csa_settings(struct hostapd_data *hapd,
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004423 struct csa_settings *settings)
4424{
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004425 struct hostapd_iface *iface = hapd->iface;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004426 struct hostapd_freq_params old_freq;
4427 int ret;
Hai Shalom81f62d82019-07-22 12:10:00 -07004428 u8 chan, bandwidth;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004429
4430 os_memset(&old_freq, 0, sizeof(old_freq));
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004431 if (!iface || !iface->freq || hapd->csa_in_progress)
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004432 return -1;
4433
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004434 switch (settings->freq_params.bandwidth) {
4435 case 80:
4436 if (settings->freq_params.center_freq2)
Sunil8cd6f4d2022-06-28 18:40:46 +00004437 bandwidth = CONF_OPER_CHWIDTH_80P80MHZ;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004438 else
Sunil8cd6f4d2022-06-28 18:40:46 +00004439 bandwidth = CONF_OPER_CHWIDTH_80MHZ;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004440 break;
4441 case 160:
Sunil8cd6f4d2022-06-28 18:40:46 +00004442 bandwidth = CONF_OPER_CHWIDTH_160MHZ;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004443 break;
Sunil Ravi640215c2023-06-28 23:08:09 +00004444 case 320:
4445 bandwidth = CONF_OPER_CHWIDTH_320MHZ;
4446 break;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004447 default:
Sunil8cd6f4d2022-06-28 18:40:46 +00004448 bandwidth = CONF_OPER_CHWIDTH_USE_HT;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004449 break;
4450 }
4451
4452 if (ieee80211_freq_to_channel_ext(
4453 settings->freq_params.freq,
4454 settings->freq_params.sec_channel_offset,
Hai Shalom81f62d82019-07-22 12:10:00 -07004455 bandwidth,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004456 &hapd->iface->cs_oper_class,
4457 &chan) == NUM_HOSTAPD_MODES) {
4458 wpa_printf(MSG_DEBUG,
Sunil Ravia04bd252022-05-02 22:54:18 -07004459 "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 -08004460 settings->freq_params.freq,
4461 settings->freq_params.sec_channel_offset,
Hai Shalom81f62d82019-07-22 12:10:00 -07004462 settings->freq_params.vht_enabled,
Sunil Ravia04bd252022-05-02 22:54:18 -07004463 settings->freq_params.he_enabled,
4464 settings->freq_params.eht_enabled);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004465 return -1;
4466 }
4467
4468 settings->freq_params.channel = chan;
4469
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004470 ret = hostapd_change_config_freq(iface->bss[0], iface->conf,
4471 &settings->freq_params,
4472 &old_freq);
4473 if (ret)
4474 return ret;
4475
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004476 ret = hostapd_build_beacon_data(hapd, &settings->beacon_after);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004477
4478 /* change back the configuration */
4479 hostapd_change_config_freq(iface->bss[0], iface->conf,
4480 &old_freq, NULL);
4481
4482 if (ret)
4483 return ret;
4484
4485 /* set channel switch parameters for csa ie */
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004486 hapd->cs_freq_params = settings->freq_params;
4487 hapd->cs_count = settings->cs_count;
4488 hapd->cs_block_tx = settings->block_tx;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004489
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004490 ret = hostapd_build_beacon_data(hapd, &settings->beacon_csa);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004491 if (ret) {
4492 free_beacon_data(&settings->beacon_after);
4493 return ret;
4494 }
4495
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004496 settings->counter_offset_beacon[0] = hapd->cs_c_off_beacon;
4497 settings->counter_offset_presp[0] = hapd->cs_c_off_proberesp;
4498 settings->counter_offset_beacon[1] = hapd->cs_c_off_ecsa_beacon;
4499 settings->counter_offset_presp[1] = hapd->cs_c_off_ecsa_proberesp;
Sunil Ravib0ac25f2024-07-12 01:42:03 +00004500 settings->link_id = -1;
4501#ifdef CONFIG_IEEE80211BE
4502 if (hapd->conf->mld_ap)
4503 settings->link_id = hapd->mld_link_id;
4504#endif /* CONFIG_IEEE80211BE */
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004505
Sunil Ravi7f769292024-07-23 22:21:32 +00004506#ifdef CONFIG_IEEE80211AX
4507 settings->ubpr.unsol_bcast_probe_resp_tmpl =
4508 hostapd_unsol_bcast_probe_resp(hapd, &settings->ubpr);
4509#endif /* CONFIG_IEEE80211AX */
4510
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004511 return 0;
4512}
4513
4514
4515void hostapd_cleanup_cs_params(struct hostapd_data *hapd)
4516{
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004517 os_memset(&hapd->cs_freq_params, 0, sizeof(hapd->cs_freq_params));
4518 hapd->cs_count = 0;
4519 hapd->cs_block_tx = 0;
4520 hapd->cs_c_off_beacon = 0;
4521 hapd->cs_c_off_proberesp = 0;
4522 hapd->csa_in_progress = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004523 hapd->cs_c_off_ecsa_beacon = 0;
4524 hapd->cs_c_off_ecsa_proberesp = 0;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004525}
4526
4527
Hai Shalom60840252021-02-19 19:02:11 -08004528void hostapd_chan_switch_config(struct hostapd_data *hapd,
4529 struct hostapd_freq_params *freq_params)
Roshan Pius3a1667e2018-07-03 15:17:14 -07004530{
Sunil Ravia04bd252022-05-02 22:54:18 -07004531 if (freq_params->eht_enabled)
4532 hapd->iconf->ch_switch_eht_config |= CH_SWITCH_EHT_ENABLED;
4533 else
4534 hapd->iconf->ch_switch_eht_config |= CH_SWITCH_EHT_DISABLED;
4535
Hai Shalom60840252021-02-19 19:02:11 -08004536 if (freq_params->he_enabled)
4537 hapd->iconf->ch_switch_he_config |= CH_SWITCH_HE_ENABLED;
4538 else
4539 hapd->iconf->ch_switch_he_config |= CH_SWITCH_HE_DISABLED;
4540
4541 if (freq_params->vht_enabled)
Roshan Pius3a1667e2018-07-03 15:17:14 -07004542 hapd->iconf->ch_switch_vht_config |= CH_SWITCH_VHT_ENABLED;
4543 else
4544 hapd->iconf->ch_switch_vht_config |= CH_SWITCH_VHT_DISABLED;
4545
4546 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
Hai Shalom60840252021-02-19 19:02:11 -08004547 HOSTAPD_LEVEL_INFO,
Sunil Ravia04bd252022-05-02 22:54:18 -07004548 "CHAN_SWITCH EHT config 0x%x HE config 0x%x VHT config 0x%x",
4549 hapd->iconf->ch_switch_eht_config,
Hai Shalom60840252021-02-19 19:02:11 -08004550 hapd->iconf->ch_switch_he_config,
Roshan Pius3a1667e2018-07-03 15:17:14 -07004551 hapd->iconf->ch_switch_vht_config);
4552}
4553
4554
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004555int hostapd_switch_channel(struct hostapd_data *hapd,
4556 struct csa_settings *settings)
4557{
4558 int ret;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004559
4560 if (!(hapd->iface->drv_flags & WPA_DRIVER_FLAGS_AP_CSA)) {
4561 wpa_printf(MSG_INFO, "CSA is not supported");
4562 return -1;
4563 }
4564
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004565 ret = hostapd_fill_csa_settings(hapd, settings);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004566 if (ret)
4567 return ret;
4568
4569 ret = hostapd_drv_switch_channel(hapd, settings);
4570 free_beacon_data(&settings->beacon_csa);
4571 free_beacon_data(&settings->beacon_after);
Sunil Ravi7f769292024-07-23 22:21:32 +00004572#ifdef CONFIG_IEEE80211AX
4573 os_free(settings->ubpr.unsol_bcast_probe_resp_tmpl);
4574#endif /* CONFIG_IEEE80211AX */
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004575
4576 if (ret) {
4577 /* if we failed, clean cs parameters */
4578 hostapd_cleanup_cs_params(hapd);
4579 return ret;
4580 }
4581
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004582 hapd->csa_in_progress = 1;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004583 return 0;
4584}
4585
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004586
Sunil Ravic0f5d412024-09-11 22:12:49 +00004587int hostapd_force_channel_switch(struct hostapd_iface *iface,
4588 struct csa_settings *settings)
4589{
4590 int ret = 0;
4591
4592 if (!settings->freq_params.channel) {
4593 /* Check if the new channel is supported */
4594 settings->freq_params.channel = hostapd_hw_get_channel(
4595 iface->bss[0], settings->freq_params.freq);
4596 if (!settings->freq_params.channel)
4597 return -1;
4598 }
4599
4600 ret = hostapd_disable_iface(iface);
4601 if (ret) {
4602 wpa_printf(MSG_DEBUG, "Failed to disable the interface");
4603 return ret;
4604 }
4605
4606 hostapd_chan_switch_config(iface->bss[0], &settings->freq_params);
4607 ret = hostapd_change_config_freq(iface->bss[0], iface->conf,
4608 &settings->freq_params, NULL);
4609 if (ret) {
4610 wpa_printf(MSG_DEBUG,
4611 "Failed to set the new channel in config");
4612 return ret;
4613 }
4614
4615 ret = hostapd_enable_iface(iface);
4616 if (ret)
4617 wpa_printf(MSG_DEBUG, "Failed to enable the interface");
4618
4619 return ret;
4620}
4621
4622
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004623void
4624hostapd_switch_channel_fallback(struct hostapd_iface *iface,
4625 const struct hostapd_freq_params *freq_params)
4626{
Sunil Ravi7f769292024-07-23 22:21:32 +00004627 u8 seg0_idx = 0, seg1_idx = 0;
Sunil8cd6f4d2022-06-28 18:40:46 +00004628 enum oper_chan_width bw = CONF_OPER_CHWIDTH_USE_HT;
Sunil Ravi7f769292024-07-23 22:21:32 +00004629 u8 op_class, chan = 0;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004630
4631 wpa_printf(MSG_DEBUG, "Restarting all CSA-related BSSes");
4632
4633 if (freq_params->center_freq1)
Sunil Ravi7f769292024-07-23 22:21:32 +00004634 ieee80211_freq_to_chan(freq_params->center_freq1, &seg0_idx);
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004635 if (freq_params->center_freq2)
Sunil Ravi7f769292024-07-23 22:21:32 +00004636 ieee80211_freq_to_chan(freq_params->center_freq2, &seg1_idx);
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004637
4638 switch (freq_params->bandwidth) {
4639 case 0:
4640 case 20:
4641 case 40:
Sunil8cd6f4d2022-06-28 18:40:46 +00004642 bw = CONF_OPER_CHWIDTH_USE_HT;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004643 break;
4644 case 80:
Sunil Ravib0ac25f2024-07-12 01:42:03 +00004645 if (freq_params->center_freq2) {
Sunil8cd6f4d2022-06-28 18:40:46 +00004646 bw = CONF_OPER_CHWIDTH_80P80MHZ;
Sunil Ravib0ac25f2024-07-12 01:42:03 +00004647 iface->conf->vht_capab |=
4648 VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ;
4649 } else {
Sunil8cd6f4d2022-06-28 18:40:46 +00004650 bw = CONF_OPER_CHWIDTH_80MHZ;
Sunil Ravib0ac25f2024-07-12 01:42:03 +00004651 }
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004652 break;
4653 case 160:
Sunil8cd6f4d2022-06-28 18:40:46 +00004654 bw = CONF_OPER_CHWIDTH_160MHZ;
Sunil Ravib0ac25f2024-07-12 01:42:03 +00004655 iface->conf->vht_capab |= VHT_CAP_SUPP_CHAN_WIDTH_160MHZ;
Sunil8cd6f4d2022-06-28 18:40:46 +00004656 break;
4657 case 320:
4658 bw = CONF_OPER_CHWIDTH_320MHZ;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004659 break;
4660 default:
4661 wpa_printf(MSG_WARNING, "Unknown CSA bandwidth: %d",
4662 freq_params->bandwidth);
4663 break;
4664 }
4665
4666 iface->freq = freq_params->freq;
4667 iface->conf->channel = freq_params->channel;
4668 iface->conf->secondary_channel = freq_params->sec_channel_offset;
Sunil Ravi7f769292024-07-23 22:21:32 +00004669 if (ieee80211_freq_to_channel_ext(freq_params->freq,
4670 freq_params->sec_channel_offset, bw,
4671 &op_class, &chan) ==
4672 NUM_HOSTAPD_MODES ||
4673 chan != freq_params->channel)
4674 wpa_printf(MSG_INFO, "CSA: Channel mismatch: %d -> %d",
4675 freq_params->channel, chan);
4676
4677 iface->conf->op_class = op_class;
Hai Shalom81f62d82019-07-22 12:10:00 -07004678 hostapd_set_oper_centr_freq_seg0_idx(iface->conf, seg0_idx);
4679 hostapd_set_oper_centr_freq_seg1_idx(iface->conf, seg1_idx);
4680 hostapd_set_oper_chwidth(iface->conf, bw);
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004681 iface->conf->ieee80211n = freq_params->ht_enabled;
4682 iface->conf->ieee80211ac = freq_params->vht_enabled;
Hai Shalom81f62d82019-07-22 12:10:00 -07004683 iface->conf->ieee80211ax = freq_params->he_enabled;
Sunil Ravia04bd252022-05-02 22:54:18 -07004684 iface->conf->ieee80211be = freq_params->eht_enabled;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004685
4686 /*
4687 * cs_params must not be cleared earlier because the freq_params
4688 * argument may actually point to one of these.
Hai Shalom39ba6fc2019-01-22 12:40:38 -08004689 * These params will be cleared during interface disable below.
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004690 */
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004691 hostapd_disable_iface(iface);
4692 hostapd_enable_iface(iface);
4693}
4694
Sunil Ravia04bd252022-05-02 22:54:18 -07004695
4696#ifdef CONFIG_IEEE80211AX
4697
4698void hostapd_cleanup_cca_params(struct hostapd_data *hapd)
4699{
4700 hapd->cca_count = 0;
4701 hapd->cca_color = 0;
4702 hapd->cca_c_off_beacon = 0;
4703 hapd->cca_c_off_proberesp = 0;
4704 hapd->cca_in_progress = false;
4705}
4706
4707
Sunil Ravi7f769292024-07-23 22:21:32 +00004708int hostapd_fill_cca_settings(struct hostapd_data *hapd,
4709 struct cca_settings *settings)
Sunil Ravia04bd252022-05-02 22:54:18 -07004710{
4711 struct hostapd_iface *iface = hapd->iface;
4712 u8 old_color;
4713 int ret;
4714
4715 if (!iface || iface->conf->he_op.he_bss_color_disabled)
4716 return -1;
4717
Sunil Ravi7f769292024-07-23 22:21:32 +00004718 settings->link_id = -1;
4719#ifdef CONFIG_IEEE80211BE
4720 if (hapd->conf->mld_ap)
4721 settings->link_id = hapd->mld_link_id;
4722#endif /* CONFIG_IEEE80211BE */
4723
Sunil Ravia04bd252022-05-02 22:54:18 -07004724 old_color = iface->conf->he_op.he_bss_color;
4725 iface->conf->he_op.he_bss_color = hapd->cca_color;
4726 ret = hostapd_build_beacon_data(hapd, &settings->beacon_after);
4727 if (ret)
4728 return ret;
4729
4730 iface->conf->he_op.he_bss_color = old_color;
4731
4732 settings->cca_count = hapd->cca_count;
4733 settings->cca_color = hapd->cca_color,
4734 hapd->cca_in_progress = true;
4735
4736 ret = hostapd_build_beacon_data(hapd, &settings->beacon_cca);
4737 if (ret) {
4738 free_beacon_data(&settings->beacon_after);
4739 return ret;
4740 }
4741
Sunil Ravi7f769292024-07-23 22:21:32 +00004742 settings->ubpr.unsol_bcast_probe_resp_tmpl =
4743 hostapd_unsol_bcast_probe_resp(hapd, &settings->ubpr);
4744
Sunil Ravia04bd252022-05-02 22:54:18 -07004745 settings->counter_offset_beacon = hapd->cca_c_off_beacon;
4746 settings->counter_offset_presp = hapd->cca_c_off_proberesp;
4747
4748 return 0;
4749}
4750
4751
4752static void hostapd_switch_color_timeout_handler(void *eloop_data,
4753 void *user_ctx)
4754{
4755 struct hostapd_data *hapd = (struct hostapd_data *) eloop_data;
4756 os_time_t delta_t;
4757 unsigned int b;
4758 int i, r;
4759
4760 /* CCA can be triggered once the handler constantly receives
4761 * color collision events to for at least
4762 * DOT11BSS_COLOR_COLLISION_AP_PERIOD (50 s by default). */
4763 delta_t = hapd->last_color_collision.sec -
4764 hapd->first_color_collision.sec;
4765 if (delta_t < DOT11BSS_COLOR_COLLISION_AP_PERIOD)
4766 return;
4767
4768 r = os_random() % HE_OPERATION_BSS_COLOR_MAX;
4769 for (i = 0; i < HE_OPERATION_BSS_COLOR_MAX; i++) {
Sunil8cd6f4d2022-06-28 18:40:46 +00004770 if (r && !(hapd->color_collision_bitmap & (1ULL << r)))
Sunil Ravia04bd252022-05-02 22:54:18 -07004771 break;
4772
4773 r = (r + 1) % HE_OPERATION_BSS_COLOR_MAX;
4774 }
4775
4776 if (i == HE_OPERATION_BSS_COLOR_MAX) {
4777 /* There are no free colors so turn BSS coloring off */
4778 wpa_printf(MSG_INFO,
4779 "No free colors left, turning off BSS coloring");
4780 hapd->iface->conf->he_op.he_bss_color_disabled = 1;
4781 hapd->iface->conf->he_op.he_bss_color = os_random() % 63 + 1;
4782 for (b = 0; b < hapd->iface->num_bss; b++)
4783 ieee802_11_set_beacon(hapd->iface->bss[b]);
4784 return;
4785 }
4786
4787 for (b = 0; b < hapd->iface->num_bss; b++) {
4788 struct hostapd_data *bss = hapd->iface->bss[b];
4789 struct cca_settings settings;
4790 int ret;
4791
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00004792 os_memset(&settings, 0, sizeof(settings));
Sunil Ravia04bd252022-05-02 22:54:18 -07004793 hostapd_cleanup_cca_params(bss);
4794 bss->cca_color = r;
4795 bss->cca_count = 10;
4796
4797 if (hostapd_fill_cca_settings(bss, &settings)) {
4798 hostapd_cleanup_cca_params(bss);
4799 continue;
4800 }
4801
4802 ret = hostapd_drv_switch_color(bss, &settings);
4803 if (ret)
4804 hostapd_cleanup_cca_params(bss);
4805
4806 free_beacon_data(&settings.beacon_cca);
4807 free_beacon_data(&settings.beacon_after);
Sunil Ravi7f769292024-07-23 22:21:32 +00004808 os_free(settings.ubpr.unsol_bcast_probe_resp_tmpl);
Sunil Ravia04bd252022-05-02 22:54:18 -07004809 }
4810}
4811
4812
4813void hostapd_switch_color(struct hostapd_data *hapd, u64 bitmap)
4814{
4815 struct os_reltime now;
4816
4817 if (hapd->cca_in_progress)
4818 return;
4819
4820 if (os_get_reltime(&now))
4821 return;
4822
4823 hapd->color_collision_bitmap = bitmap;
4824 hapd->last_color_collision = now;
4825
4826 if (eloop_is_timeout_registered(hostapd_switch_color_timeout_handler,
4827 hapd, NULL))
4828 return;
4829
4830 hapd->first_color_collision = now;
4831 /* 10 s window as margin for persistent color collision reporting */
4832 eloop_register_timeout(DOT11BSS_COLOR_COLLISION_AP_PERIOD + 10, 0,
4833 hostapd_switch_color_timeout_handler,
4834 hapd, NULL);
4835}
4836
4837#endif /* CONFIG_IEEE80211AX */
4838
Dmitry Shmidte4663042016-04-04 10:07:49 -07004839#endif /* NEED_AP_MLME */
4840
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004841
4842struct hostapd_data * hostapd_get_iface(struct hapd_interfaces *interfaces,
4843 const char *ifname)
4844{
4845 size_t i, j;
4846
4847 for (i = 0; i < interfaces->count; i++) {
4848 struct hostapd_iface *iface = interfaces->iface[i];
4849
4850 for (j = 0; j < iface->num_bss; j++) {
4851 struct hostapd_data *hapd = iface->bss[j];
4852
4853 if (os_strcmp(ifname, hapd->conf->iface) == 0)
4854 return hapd;
4855 }
4856 }
4857
4858 return NULL;
4859}
4860
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004861
4862void hostapd_periodic_iface(struct hostapd_iface *iface)
4863{
4864 size_t i;
4865
4866 ap_list_timer(iface);
4867
4868 for (i = 0; i < iface->num_bss; i++) {
4869 struct hostapd_data *hapd = iface->bss[i];
4870
4871 if (!hapd->started)
4872 continue;
4873
4874#ifndef CONFIG_NO_RADIUS
4875 hostapd_acl_expire(hapd);
4876#endif /* CONFIG_NO_RADIUS */
4877 }
4878}
Hai Shalom899fcc72020-10-19 14:38:18 -07004879
4880
4881#ifdef CONFIG_OCV
4882void hostapd_ocv_check_csa_sa_query(void *eloop_ctx, void *timeout_ctx)
4883{
4884 struct hostapd_data *hapd = eloop_ctx;
4885 struct sta_info *sta;
4886
4887 wpa_printf(MSG_DEBUG, "OCV: Post-CSA SA Query initiation check");
4888
4889 for (sta = hapd->sta_list; sta; sta = sta->next) {
4890 if (!sta->post_csa_sa_query)
4891 continue;
4892
4893 wpa_printf(MSG_DEBUG, "OCV: OCVC STA " MACSTR
4894 " did not start SA Query after CSA - disconnect",
4895 MAC2STR(sta->addr));
4896 ap_sta_disconnect(hapd, sta, sta->addr,
4897 WLAN_REASON_PREV_AUTH_NOT_VALID);
4898 }
4899}
4900#endif /* CONFIG_OCV */
Sunil Ravi2a14cf12023-11-21 00:54:38 +00004901
4902
4903#ifdef CONFIG_IEEE80211BE
Sunil Ravi99c035e2024-07-12 01:42:03 +00004904
Sunil Ravi2a14cf12023-11-21 00:54:38 +00004905struct hostapd_data * hostapd_mld_get_link_bss(struct hostapd_data *hapd,
4906 u8 link_id)
4907{
Sunil Ravi7f769292024-07-23 22:21:32 +00004908 struct hostapd_iface *iface;
4909 struct hostapd_data *bss;
4910 unsigned int i, j;
Sunil Ravi2a14cf12023-11-21 00:54:38 +00004911
4912 for (i = 0; i < hapd->iface->interfaces->count; i++) {
Sunil Ravi7f769292024-07-23 22:21:32 +00004913 iface = hapd->iface->interfaces->iface[i];
4914 if (!iface)
Sunil Ravi2a14cf12023-11-21 00:54:38 +00004915 continue;
4916
Sunil Ravi7f769292024-07-23 22:21:32 +00004917 for (j = 0; j < iface->num_bss; j++) {
4918 bss = iface->bss[j];
4919
4920 if (!bss->conf->mld_ap ||
4921 !hostapd_is_ml_partner(hapd, bss))
4922 continue;
4923
4924 if (!bss->drv_priv)
4925 continue;
4926
4927 if (bss->mld_link_id == link_id)
4928 return bss;
4929 }
Sunil Ravi2a14cf12023-11-21 00:54:38 +00004930 }
4931
4932 return NULL;
4933}
Sunil Ravi99c035e2024-07-12 01:42:03 +00004934
4935
4936bool hostapd_is_ml_partner(struct hostapd_data *hapd1,
4937 struct hostapd_data *hapd2)
4938{
4939 if (!hapd1->conf->mld_ap || !hapd2->conf->mld_ap)
4940 return false;
4941
4942 return !os_strcmp(hapd1->conf->iface, hapd2->conf->iface);
4943}
4944
4945
4946u8 hostapd_get_mld_id(struct hostapd_data *hapd)
4947{
4948 if (!hapd->conf->mld_ap)
4949 return 255;
4950
4951 /* MLD ID 0 represents self */
4952 return 0;
4953
4954 /* TODO: MLD ID for Multiple BSS cases */
4955}
4956
4957
4958int hostapd_mld_add_link(struct hostapd_data *hapd)
4959{
4960 struct hostapd_mld *mld = hapd->mld;
4961
4962 if (!hapd->conf->mld_ap)
4963 return 0;
4964
4965 /* Should not happen */
4966 if (!mld)
4967 return -1;
4968
4969 dl_list_add_tail(&mld->links, &hapd->link);
4970 mld->num_links++;
4971
4972 wpa_printf(MSG_DEBUG, "AP MLD %s: Link ID %d added. num_links: %d",
4973 mld->name, hapd->mld_link_id, mld->num_links);
4974
4975 if (mld->fbss)
4976 return 0;
4977
4978 mld->fbss = hapd;
4979 wpa_printf(MSG_DEBUG, "AP MLD %s: First link BSS set to %p",
4980 mld->name, mld->fbss);
4981 return 0;
4982}
4983
4984
4985int hostapd_mld_remove_link(struct hostapd_data *hapd)
4986{
4987 struct hostapd_mld *mld = hapd->mld;
4988 struct hostapd_data *next_fbss;
4989
4990 if (!hapd->conf->mld_ap)
4991 return 0;
4992
4993 /* Should not happen */
4994 if (!mld)
4995 return -1;
4996
4997 dl_list_del(&hapd->link);
4998 mld->num_links--;
4999
5000 wpa_printf(MSG_DEBUG, "AP MLD %s: Link ID %d removed. num_links: %d",
5001 mld->name, hapd->mld_link_id, mld->num_links);
5002
5003 if (mld->fbss != hapd)
5004 return 0;
5005
5006 /* If the list is empty, all links are removed */
5007 if (dl_list_empty(&mld->links)) {
5008 mld->fbss = NULL;
5009 } else {
5010 next_fbss = dl_list_entry(mld->links.next, struct hostapd_data,
5011 link);
5012 mld->fbss = next_fbss;
5013 }
5014
5015 wpa_printf(MSG_DEBUG, "AP MLD %s: First link BSS set to %p",
5016 mld->name, mld->fbss);
5017 return 0;
5018}
5019
5020
5021bool hostapd_mld_is_first_bss(struct hostapd_data *hapd)
5022{
5023 struct hostapd_mld *mld = hapd->mld;
5024
5025 if (!hapd->conf->mld_ap)
5026 return true;
5027
5028 /* Should not happen */
5029 if (!mld)
5030 return false;
5031
5032 /* If fbss is not set, it is safe to assume the caller is the first BSS.
5033 */
5034 if (!mld->fbss)
5035 return true;
5036
5037 return hapd == mld->fbss;
5038}
5039
5040
5041struct hostapd_data * hostapd_mld_get_first_bss(struct hostapd_data *hapd)
5042{
5043 struct hostapd_mld *mld = hapd->mld;
5044
5045 if (!hapd->conf->mld_ap)
5046 return NULL;
5047
5048 /* Should not happen */
5049 if (!mld)
5050 return NULL;
5051
5052 return mld->fbss;
5053}
5054
Sunil Ravic0f5d412024-09-11 22:12:49 +00005055
5056void hostapd_mld_interface_freed(struct hostapd_data *hapd)
5057{
5058 struct hostapd_data *link_bss = NULL;
5059
5060 if (!hapd || !hapd->conf->mld_ap)
5061 return;
5062
5063 for_each_mld_link(link_bss, hapd)
5064 link_bss->drv_priv = NULL;
5065}
5066
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00005067
5068/* Return the number of currently active links, not counting the calling link
5069 * (i.e., a value that is suitable to be used as-is in fields that use encoding
5070 * of the value minus 1). */
5071u8 hostapd_get_active_links(struct hostapd_data *hapd)
5072{
5073 struct hostapd_data *link_bss;
5074 u8 active_links = 0;
5075
5076 if (!hapd || !hapd->conf->mld_ap)
5077 return 0;
5078
5079 for_each_mld_link(link_bss, hapd) {
5080 if (link_bss == hapd || !link_bss->started)
5081 continue;
5082
5083 active_links++;
5084 }
5085
5086 return active_links;
5087}
5088
Sunil Ravi2a14cf12023-11-21 00:54:38 +00005089#endif /* CONFIG_IEEE80211BE */
Sunil Ravi99c035e2024-07-12 01:42:03 +00005090
5091
5092u16 hostapd_get_punct_bitmap(struct hostapd_data *hapd)
5093{
5094 u16 punct_bitmap = 0;
5095
5096#ifdef CONFIG_IEEE80211BE
5097 punct_bitmap = hapd->iconf->punct_bitmap;
5098#ifdef CONFIG_TESTING_OPTIONS
5099 if (!punct_bitmap)
5100 punct_bitmap = hapd->conf->eht_oper_puncturing_override;
5101#endif /* CONFIG_TESTING_OPTIONS */
5102#endif /* CONFIG_IEEE80211BE */
5103
5104 return punct_bitmap;
5105}