blob: 58492e51edae51e5398446f2db7575992685f030 [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"
38#include "gas_query_ap.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070039#include "hw_features.h"
40#include "wpa_auth_glue.h"
41#include "ap_drv_ops.h"
42#include "ap_config.h"
43#include "p2p_hostapd.h"
Dmitry Shmidt04949592012-07-19 12:16:46 -070044#include "gas_serv.h"
Dmitry Shmidt051af732013-10-22 13:52:46 -070045#include "dfs.h"
Dmitry Shmidt7832adb2014-04-29 10:53:02 -070046#include "ieee802_11.h"
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080047#include "bss_load.h"
48#include "x_snoop.h"
49#include "dhcp_snoop.h"
50#include "ndisc_snoop.h"
Dmitry Shmidt849734c2016-05-27 09:59:01 -070051#include "neighbor_db.h"
52#include "rrm.h"
Dmitry Shmidtebd93af2017-02-21 13:40:44 -080053#include "fils_hlp.h"
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070054#include "acs.h"
Roshan Pius3a1667e2018-07-03 15:17:14 -070055#include "hs20.h"
Hai Shalom81f62d82019-07-22 12:10:00 -070056#include "airtime_policy.h"
57#include "wpa_auth_kay.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070058
59
Dmitry Shmidt04949592012-07-19 12:16:46 -070060static int hostapd_flush_old_stations(struct hostapd_data *hapd, u16 reason);
Hai Shalomfdcde762020-04-02 11:19:20 -070061#ifdef CONFIG_WEP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070062static int hostapd_setup_encryption(char *iface, struct hostapd_data *hapd);
Dmitry Shmidtc55524a2011-07-07 11:18:38 -070063static int hostapd_broadcast_wep_clear(struct hostapd_data *hapd);
Hai Shalomfdcde762020-04-02 11:19:20 -070064#endif /* CONFIG_WEP */
Dmitry Shmidtcce06662013-11-04 18:44:24 -080065static int setup_interface2(struct hostapd_iface *iface);
66static void channel_list_update_timeout(void *eloop_ctx, void *timeout_ctx);
Roshan Pius3a1667e2018-07-03 15:17:14 -070067static void hostapd_interface_setup_failure_handler(void *eloop_ctx,
68 void *timeout_ctx);
Sunil Ravia04bd252022-05-02 22:54:18 -070069#ifdef CONFIG_IEEE80211AX
70static void hostapd_switch_color_timeout_handler(void *eloop_data,
71 void *user_ctx);
72#endif /* CONFIG_IEEE80211AX */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070073
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070074
Dmitry Shmidt04949592012-07-19 12:16:46 -070075int hostapd_for_each_interface(struct hapd_interfaces *interfaces,
76 int (*cb)(struct hostapd_iface *iface,
77 void *ctx), void *ctx)
78{
79 size_t i;
80 int ret;
81
82 for (i = 0; i < interfaces->count; i++) {
Hai Shalom4fbc08f2020-05-18 12:37:00 -070083 if (!interfaces->iface[i])
84 continue;
Dmitry Shmidt04949592012-07-19 12:16:46 -070085 ret = cb(interfaces->iface[i], ctx);
86 if (ret)
87 return ret;
88 }
89
90 return 0;
91}
92
93
Sunil Ravi77d572f2023-01-17 23:58:31 +000094struct hostapd_data * hostapd_mbssid_get_tx_bss(struct hostapd_data *hapd)
95{
96 if (hapd->iconf->mbssid)
97 return hapd->iface->bss[0];
98
99 return hapd;
100}
101
102
103int hostapd_mbssid_get_bss_index(struct hostapd_data *hapd)
104{
105 if (hapd->iconf->mbssid) {
106 size_t i;
107
108 for (i = 1; i < hapd->iface->num_bss; i++)
109 if (hapd->iface->bss[i] == hapd)
110 return i;
111 }
112
113 return 0;
114}
115
116
Hai Shalomce48b4a2018-09-05 11:41:35 -0700117void hostapd_reconfig_encryption(struct hostapd_data *hapd)
118{
119 if (hapd->wpa_auth)
120 return;
121
122 hostapd_set_privacy(hapd, 0);
Hai Shalomfdcde762020-04-02 11:19:20 -0700123#ifdef CONFIG_WEP
Hai Shalomce48b4a2018-09-05 11:41:35 -0700124 hostapd_setup_encryption(hapd->conf->iface, hapd);
Hai Shalomfdcde762020-04-02 11:19:20 -0700125#endif /* CONFIG_WEP */
Hai Shalomce48b4a2018-09-05 11:41:35 -0700126}
127
128
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700129static void hostapd_reload_bss(struct hostapd_data *hapd)
130{
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800131 struct hostapd_ssid *ssid;
132
Dmitry Shmidt29333592017-01-09 12:27:11 -0800133 if (!hapd->started)
134 return;
135
Roshan Pius3a1667e2018-07-03 15:17:14 -0700136 if (hapd->conf->wmm_enabled < 0)
Hai Shaloma20dcd72022-02-04 13:43:00 -0800137 hapd->conf->wmm_enabled = hapd->iconf->ieee80211n |
138 hapd->iconf->ieee80211ax;
Roshan Pius3a1667e2018-07-03 15:17:14 -0700139
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700140#ifndef CONFIG_NO_RADIUS
141 radius_client_reconfig(hapd->radius, hapd->conf->radius);
142#endif /* CONFIG_NO_RADIUS */
143
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800144 ssid = &hapd->conf->ssid;
145 if (!ssid->wpa_psk_set && ssid->wpa_psk && !ssid->wpa_psk->next &&
146 ssid->wpa_passphrase_set && ssid->wpa_passphrase) {
147 /*
148 * Force PSK to be derived again since SSID or passphrase may
149 * have changed.
150 */
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800151 hostapd_config_clear_wpa_psk(&hapd->conf->ssid.wpa_psk);
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800152 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700153 if (hostapd_setup_wpa_psk(hapd->conf)) {
154 wpa_printf(MSG_ERROR, "Failed to re-configure WPA PSK "
155 "after reloading configuration");
156 }
157
158 if (hapd->conf->ieee802_1x || hapd->conf->wpa)
159 hostapd_set_drv_ieee8021x(hapd, hapd->conf->iface, 1);
160 else
161 hostapd_set_drv_ieee8021x(hapd, hapd->conf->iface, 0);
162
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800163 if ((hapd->conf->wpa || hapd->conf->osen) && hapd->wpa_auth == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700164 hostapd_setup_wpa(hapd);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800165 if (hapd->wpa_auth)
166 wpa_init_keys(hapd->wpa_auth);
167 } else if (hapd->conf->wpa) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700168 const u8 *wpa_ie;
169 size_t wpa_ie_len;
170 hostapd_reconfig_wpa(hapd);
171 wpa_ie = wpa_auth_get_wpa_ie(hapd->wpa_auth, &wpa_ie_len);
172 if (hostapd_set_generic_elem(hapd, wpa_ie, wpa_ie_len))
173 wpa_printf(MSG_ERROR, "Failed to configure WPA IE for "
174 "the kernel driver.");
175 } else if (hapd->wpa_auth) {
176 wpa_deinit(hapd->wpa_auth);
177 hapd->wpa_auth = NULL;
178 hostapd_set_privacy(hapd, 0);
Hai Shalomfdcde762020-04-02 11:19:20 -0700179#ifdef CONFIG_WEP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700180 hostapd_setup_encryption(hapd->conf->iface, hapd);
Hai Shalomfdcde762020-04-02 11:19:20 -0700181#endif /* CONFIG_WEP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700182 hostapd_set_generic_elem(hapd, (u8 *) "", 0);
183 }
184
185 ieee802_11_set_beacon(hapd);
186 hostapd_update_wps(hapd);
187
188 if (hapd->conf->ssid.ssid_set &&
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700189 hostapd_set_ssid(hapd, hapd->conf->ssid.ssid,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700190 hapd->conf->ssid.ssid_len)) {
191 wpa_printf(MSG_ERROR, "Could not set SSID for kernel driver");
192 /* try to continue */
193 }
194 wpa_printf(MSG_DEBUG, "Reconfigured interface %s", hapd->conf->iface);
195}
196
197
Sunil Ravi77d572f2023-01-17 23:58:31 +0000198static void hostapd_clear_old_bss(struct hostapd_data *bss)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700199{
Sunil Ravi77d572f2023-01-17 23:58:31 +0000200 wpa_printf(MSG_DEBUG, "BSS %s changed - clear old state",
201 bss->conf->iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700202
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700203 /*
204 * Deauthenticate all stations since the new configuration may not
205 * allow them to use the BSS anymore.
206 */
Sunil Ravi77d572f2023-01-17 23:58:31 +0000207 hostapd_flush_old_stations(bss, WLAN_REASON_PREV_AUTH_NOT_VALID);
Hai Shalomfdcde762020-04-02 11:19:20 -0700208#ifdef CONFIG_WEP
Sunil Ravi77d572f2023-01-17 23:58:31 +0000209 hostapd_broadcast_wep_clear(bss);
Hai Shalomfdcde762020-04-02 11:19:20 -0700210#endif /* CONFIG_WEP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700211
212#ifndef CONFIG_NO_RADIUS
Sunil Ravi77d572f2023-01-17 23:58:31 +0000213 /* TODO: update dynamic data based on changed configuration
214 * items (e.g., open/close sockets, etc.) */
215 radius_client_flush(bss->radius, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700216#endif /* CONFIG_NO_RADIUS */
Sunil Ravi77d572f2023-01-17 23:58:31 +0000217}
218
219
220static void hostapd_clear_old(struct hostapd_iface *iface)
221{
222 size_t j;
223
224 for (j = 0; j < iface->num_bss; j++)
225 hostapd_clear_old_bss(iface->bss[j]);
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700226}
227
228
Hai Shalom74f70d42019-02-11 14:42:39 -0800229static int hostapd_iface_conf_changed(struct hostapd_config *newconf,
230 struct hostapd_config *oldconf)
231{
232 size_t i;
233
234 if (newconf->num_bss != oldconf->num_bss)
235 return 1;
236
237 for (i = 0; i < newconf->num_bss; i++) {
238 if (os_strcmp(newconf->bss[i]->iface,
239 oldconf->bss[i]->iface) != 0)
240 return 1;
241 }
242
243 return 0;
244}
245
246
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700247int hostapd_reload_config(struct hostapd_iface *iface)
248{
Hai Shalom74f70d42019-02-11 14:42:39 -0800249 struct hapd_interfaces *interfaces = iface->interfaces;
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700250 struct hostapd_data *hapd = iface->bss[0];
251 struct hostapd_config *newconf, *oldconf;
252 size_t j;
253
254 if (iface->config_fname == NULL) {
255 /* Only in-memory config in use - assume it has been updated */
256 hostapd_clear_old(iface);
257 for (j = 0; j < iface->num_bss; j++)
258 hostapd_reload_bss(iface->bss[j]);
259 return 0;
260 }
261
262 if (iface->interfaces == NULL ||
263 iface->interfaces->config_read_cb == NULL)
264 return -1;
265 newconf = iface->interfaces->config_read_cb(iface->config_fname);
266 if (newconf == NULL)
267 return -1;
268
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700269 oldconf = hapd->iconf;
Hai Shalom74f70d42019-02-11 14:42:39 -0800270 if (hostapd_iface_conf_changed(newconf, oldconf)) {
271 char *fname;
272 int res;
273
Sunil Ravi77d572f2023-01-17 23:58:31 +0000274 hostapd_clear_old(iface);
275
Hai Shalom74f70d42019-02-11 14:42:39 -0800276 wpa_printf(MSG_DEBUG,
277 "Configuration changes include interface/BSS modification - force full disable+enable sequence");
278 fname = os_strdup(iface->config_fname);
279 if (!fname) {
280 hostapd_config_free(newconf);
281 return -1;
282 }
283 hostapd_remove_iface(interfaces, hapd->conf->iface);
284 iface = hostapd_init(interfaces, fname);
285 os_free(fname);
286 hostapd_config_free(newconf);
287 if (!iface) {
288 wpa_printf(MSG_ERROR,
289 "Failed to initialize interface on config reload");
290 return -1;
291 }
292 iface->interfaces = interfaces;
293 interfaces->iface[interfaces->count] = iface;
294 interfaces->count++;
295 res = hostapd_enable_iface(iface);
296 if (res < 0)
297 wpa_printf(MSG_ERROR,
298 "Failed to enable interface on config reload");
299 return res;
300 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700301 iface->conf = newconf;
302
303 for (j = 0; j < iface->num_bss; j++) {
304 hapd = iface->bss[j];
Sunil Ravi77d572f2023-01-17 23:58:31 +0000305 if (!hapd->conf->config_id || !newconf->bss[j]->config_id ||
306 os_strcmp(hapd->conf->config_id,
307 newconf->bss[j]->config_id) != 0)
308 hostapd_clear_old_bss(hapd);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700309 hapd->iconf = newconf;
Dmitry Shmidtd11f0192014-03-24 12:09:47 -0700310 hapd->iconf->channel = oldconf->channel;
Dmitry Shmidtdda10c22015-03-24 16:05:01 -0700311 hapd->iconf->acs = oldconf->acs;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -0700312 hapd->iconf->secondary_channel = oldconf->secondary_channel;
Dmitry Shmidtd11f0192014-03-24 12:09:47 -0700313 hapd->iconf->ieee80211n = oldconf->ieee80211n;
314 hapd->iconf->ieee80211ac = oldconf->ieee80211ac;
315 hapd->iconf->ht_capab = oldconf->ht_capab;
316 hapd->iconf->vht_capab = oldconf->vht_capab;
Hai Shalom81f62d82019-07-22 12:10:00 -0700317 hostapd_set_oper_chwidth(hapd->iconf,
318 hostapd_get_oper_chwidth(oldconf));
319 hostapd_set_oper_centr_freq_seg0_idx(
320 hapd->iconf,
321 hostapd_get_oper_centr_freq_seg0_idx(oldconf));
322 hostapd_set_oper_centr_freq_seg1_idx(
323 hapd->iconf,
324 hostapd_get_oper_centr_freq_seg1_idx(oldconf));
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800325 hapd->conf = newconf->bss[j];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700326 hostapd_reload_bss(hapd);
327 }
328
329 hostapd_config_free(oldconf);
330
331
332 return 0;
333}
334
335
Hai Shalomfdcde762020-04-02 11:19:20 -0700336#ifdef CONFIG_WEP
337
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700338static void hostapd_broadcast_key_clear_iface(struct hostapd_data *hapd,
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700339 const char *ifname)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700340{
341 int i;
342
Dmitry Shmidt29333592017-01-09 12:27:11 -0800343 if (!ifname || !hapd->drv_priv)
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700344 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700345 for (i = 0; i < NUM_WEP_KEYS; i++) {
Hai Shalomfdcde762020-04-02 11:19:20 -0700346 if (hostapd_drv_set_key(ifname, hapd, WPA_ALG_NONE, NULL, i, 0,
347 0, NULL, 0, NULL, 0, KEY_FLAG_GROUP)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700348 wpa_printf(MSG_DEBUG, "Failed to clear default "
349 "encryption keys (ifname=%s keyidx=%d)",
350 ifname, i);
351 }
352 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700353 if (hapd->conf->ieee80211w) {
354 for (i = NUM_WEP_KEYS; i < NUM_WEP_KEYS + 2; i++) {
355 if (hostapd_drv_set_key(ifname, hapd, WPA_ALG_NONE,
Hai Shalomfdcde762020-04-02 11:19:20 -0700356 NULL, i, 0, 0, NULL,
357 0, NULL, 0, KEY_FLAG_GROUP)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700358 wpa_printf(MSG_DEBUG, "Failed to clear "
359 "default mgmt encryption keys "
360 "(ifname=%s keyidx=%d)", ifname, i);
361 }
362 }
363 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700364}
365
366
367static int hostapd_broadcast_wep_clear(struct hostapd_data *hapd)
368{
369 hostapd_broadcast_key_clear_iface(hapd, hapd->conf->iface);
370 return 0;
371}
372
373
374static int hostapd_broadcast_wep_set(struct hostapd_data *hapd)
375{
376 int errors = 0, idx;
377 struct hostapd_ssid *ssid = &hapd->conf->ssid;
378
379 idx = ssid->wep.idx;
Hai Shalomfdcde762020-04-02 11:19:20 -0700380 if (ssid->wep.default_len && ssid->wep.key[idx] &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700381 hostapd_drv_set_key(hapd->conf->iface,
Hai Shalomfdcde762020-04-02 11:19:20 -0700382 hapd, WPA_ALG_WEP, broadcast_ether_addr, idx, 0,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700383 1, NULL, 0, ssid->wep.key[idx],
Hai Shalomfdcde762020-04-02 11:19:20 -0700384 ssid->wep.len[idx],
385 KEY_FLAG_GROUP_RX_TX_DEFAULT)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700386 wpa_printf(MSG_WARNING, "Could not set WEP encryption.");
387 errors++;
388 }
389
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700390 return errors;
391}
392
Hai Shalomfdcde762020-04-02 11:19:20 -0700393#endif /* CONFIG_WEP */
394
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700395
Hai Shalom60840252021-02-19 19:02:11 -0800396void hostapd_free_hapd_data(struct hostapd_data *hapd)
Dmitry Shmidt04949592012-07-19 12:16:46 -0700397{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800398 os_free(hapd->probereq_cb);
399 hapd->probereq_cb = NULL;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800400 hapd->num_probereq_cb = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800401
402#ifdef CONFIG_P2P
403 wpabuf_free(hapd->p2p_beacon_ie);
404 hapd->p2p_beacon_ie = NULL;
405 wpabuf_free(hapd->p2p_probe_resp_ie);
406 hapd->p2p_probe_resp_ie = NULL;
407#endif /* CONFIG_P2P */
408
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -0800409 if (!hapd->started) {
410 wpa_printf(MSG_ERROR, "%s: Interface %s wasn't started",
Hai Shalom021b0b52019-04-10 11:17:58 -0700411 __func__, hapd->conf ? hapd->conf->iface : "N/A");
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -0800412 return;
413 }
414 hapd->started = 0;
Hai Shalom5f92bc92019-04-18 11:54:11 -0700415 hapd->beacon_set_done = 0;
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -0800416
Dmitry Shmidt54605472013-11-08 11:10:19 -0800417 wpa_printf(MSG_DEBUG, "%s(%s)", __func__, hapd->conf->iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700418 accounting_deinit(hapd);
419 hostapd_deinit_wpa(hapd);
420 vlan_deinit(hapd);
421 hostapd_acl_deinit(hapd);
422#ifndef CONFIG_NO_RADIUS
423 radius_client_deinit(hapd->radius);
424 hapd->radius = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700425 radius_das_deinit(hapd->radius_das);
426 hapd->radius_das = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700427#endif /* CONFIG_NO_RADIUS */
428
429 hostapd_deinit_wps(hapd);
Hai Shalom81f62d82019-07-22 12:10:00 -0700430 ieee802_1x_dealloc_kay_sm_hapd(hapd);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700431#ifdef CONFIG_DPP
432 hostapd_dpp_deinit(hapd);
433 gas_query_ap_deinit(hapd->gas);
Hai Shaloma20dcd72022-02-04 13:43:00 -0800434 hapd->gas = NULL;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700435#endif /* CONFIG_DPP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700436
437 authsrv_deinit(hapd);
438
Dmitry Shmidt71757432014-06-02 13:50:35 -0700439 if (hapd->interface_added) {
440 hapd->interface_added = 0;
441 if (hostapd_if_remove(hapd, WPA_IF_AP_BSS, hapd->conf->iface)) {
442 wpa_printf(MSG_WARNING,
443 "Failed to remove BSS interface %s",
444 hapd->conf->iface);
445 hapd->interface_added = 1;
446 } else {
447 /*
448 * Since this was a dynamically added interface, the
449 * driver wrapper may have removed its internal instance
450 * and hapd->drv_priv is not valid anymore.
451 */
452 hapd->drv_priv = NULL;
453 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700454 }
455
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800456 wpabuf_free(hapd->time_adv);
Hai Shaloma20dcd72022-02-04 13:43:00 -0800457 hapd->time_adv = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700458
459#ifdef CONFIG_INTERWORKING
460 gas_serv_deinit(hapd);
461#endif /* CONFIG_INTERWORKING */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800462
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800463 bss_load_update_deinit(hapd);
464 ndisc_snoop_deinit(hapd);
465 dhcp_snoop_deinit(hapd);
466 x_snoop_deinit(hapd);
467
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800468#ifdef CONFIG_SQLITE
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700469 bin_clear_free(hapd->tmp_eap_user.identity,
470 hapd->tmp_eap_user.identity_len);
471 bin_clear_free(hapd->tmp_eap_user.password,
472 hapd->tmp_eap_user.password_len);
Hai Shaloma20dcd72022-02-04 13:43:00 -0800473 os_memset(&hapd->tmp_eap_user, 0, sizeof(hapd->tmp_eap_user));
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800474#endif /* CONFIG_SQLITE */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800475
476#ifdef CONFIG_MESH
477 wpabuf_free(hapd->mesh_pending_auth);
478 hapd->mesh_pending_auth = NULL;
Hai Shalom60840252021-02-19 19:02:11 -0800479 /* handling setup failure is already done */
480 hapd->setup_complete_cb = NULL;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800481#endif /* CONFIG_MESH */
Dmitry Shmidt849734c2016-05-27 09:59:01 -0700482
483 hostapd_clean_rrm(hapd);
Dmitry Shmidtebd93af2017-02-21 13:40:44 -0800484 fils_hlp_deinit(hapd);
Hai Shalom021b0b52019-04-10 11:17:58 -0700485
Hai Shalom899fcc72020-10-19 14:38:18 -0700486#ifdef CONFIG_OCV
487 eloop_cancel_timeout(hostapd_ocv_check_csa_sa_query, hapd, NULL);
488#endif /* CONFIG_OCV */
489
Hai Shalom021b0b52019-04-10 11:17:58 -0700490#ifdef CONFIG_SAE
491 {
492 struct hostapd_sae_commit_queue *q;
493
494 while ((q = dl_list_first(&hapd->sae_commit_queue,
495 struct hostapd_sae_commit_queue,
496 list))) {
497 dl_list_del(&q->list);
498 os_free(q);
499 }
500 }
501 eloop_cancel_timeout(auth_sae_process_commit, hapd, NULL);
502#endif /* CONFIG_SAE */
Sunil Ravia04bd252022-05-02 22:54:18 -0700503
504#ifdef CONFIG_IEEE80211AX
505 eloop_cancel_timeout(hostapd_switch_color_timeout_handler, hapd, NULL);
506#endif /* CONFIG_IEEE80211AX */
Dmitry Shmidt04949592012-07-19 12:16:46 -0700507}
508
509
510/**
511 * hostapd_cleanup - Per-BSS cleanup (deinitialization)
512 * @hapd: Pointer to BSS data
513 *
514 * This function is used to free all per-BSS data structures and resources.
Dmitry Shmidt54605472013-11-08 11:10:19 -0800515 * Most of the modules that are initialized in hostapd_setup_bss() are
516 * deinitialized here.
Dmitry Shmidt04949592012-07-19 12:16:46 -0700517 */
518static void hostapd_cleanup(struct hostapd_data *hapd)
519{
Dmitry Shmidt54605472013-11-08 11:10:19 -0800520 wpa_printf(MSG_DEBUG, "%s(hapd=%p (%s))", __func__, hapd,
Hai Shalom021b0b52019-04-10 11:17:58 -0700521 hapd->conf ? hapd->conf->iface : "N/A");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700522 if (hapd->iface->interfaces &&
Dmitry Shmidt29333592017-01-09 12:27:11 -0800523 hapd->iface->interfaces->ctrl_iface_deinit) {
524 wpa_msg(hapd->msg_ctx, MSG_INFO, WPA_EVENT_TERMINATING);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700525 hapd->iface->interfaces->ctrl_iface_deinit(hapd);
Dmitry Shmidt29333592017-01-09 12:27:11 -0800526 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700527 hostapd_free_hapd_data(hapd);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700528}
529
530
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800531static void sta_track_deinit(struct hostapd_iface *iface)
532{
533 struct hostapd_sta_info *info;
534
535 if (!iface->num_sta_seen)
536 return;
537
538 while ((info = dl_list_first(&iface->sta_seen, struct hostapd_sta_info,
539 list))) {
540 dl_list_del(&info->list);
541 iface->num_sta_seen--;
Dmitry Shmidtaca489e2016-09-28 15:44:14 -0700542 sta_track_del(info);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800543 }
544}
545
546
Hai Shalom60840252021-02-19 19:02:11 -0800547void hostapd_cleanup_iface_partial(struct hostapd_iface *iface)
Dmitry Shmidt04949592012-07-19 12:16:46 -0700548{
Dmitry Shmidt54605472013-11-08 11:10:19 -0800549 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800550#ifdef NEED_AP_MLME
551 hostapd_stop_setup_timers(iface);
552#endif /* NEED_AP_MLME */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700553 if (iface->current_mode)
554 acs_cleanup(iface);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700555 hostapd_free_hw_features(iface->hw_features, iface->num_hw_features);
556 iface->hw_features = NULL;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700557 iface->current_mode = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700558 os_free(iface->current_rates);
559 iface->current_rates = NULL;
560 os_free(iface->basic_rates);
561 iface->basic_rates = NULL;
Sunil Ravi77d572f2023-01-17 23:58:31 +0000562 iface->cac_started = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700563 ap_list_deinit(iface);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800564 sta_track_deinit(iface);
Hai Shalom81f62d82019-07-22 12:10:00 -0700565 airtime_policy_update_deinit(iface);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700566}
567
568
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700569/**
570 * hostapd_cleanup_iface - Complete per-interface cleanup
571 * @iface: Pointer to interface data
572 *
573 * This function is called after per-BSS data structures are deinitialized
574 * with hostapd_cleanup().
575 */
576static void hostapd_cleanup_iface(struct hostapd_iface *iface)
577{
Dmitry Shmidt54605472013-11-08 11:10:19 -0800578 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800579 eloop_cancel_timeout(channel_list_update_timeout, iface, NULL);
Roshan Pius3a1667e2018-07-03 15:17:14 -0700580 eloop_cancel_timeout(hostapd_interface_setup_failure_handler, iface,
581 NULL);
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800582
Dmitry Shmidt04949592012-07-19 12:16:46 -0700583 hostapd_cleanup_iface_partial(iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700584 hostapd_config_free(iface->conf);
585 iface->conf = NULL;
586
587 os_free(iface->config_fname);
588 os_free(iface->bss);
Dmitry Shmidt54605472013-11-08 11:10:19 -0800589 wpa_printf(MSG_DEBUG, "%s: free iface=%p", __func__, iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700590 os_free(iface);
591}
592
593
Hai Shalomfdcde762020-04-02 11:19:20 -0700594#ifdef CONFIG_WEP
595
Dmitry Shmidt04949592012-07-19 12:16:46 -0700596static void hostapd_clear_wep(struct hostapd_data *hapd)
597{
Hai Shalom021b0b52019-04-10 11:17:58 -0700598 if (hapd->drv_priv && !hapd->iface->driver_ap_teardown && hapd->conf) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700599 hostapd_set_privacy(hapd, 0);
600 hostapd_broadcast_wep_clear(hapd);
601 }
602}
603
604
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700605static int hostapd_setup_encryption(char *iface, struct hostapd_data *hapd)
606{
607 int i;
608
609 hostapd_broadcast_wep_set(hapd);
610
611 if (hapd->conf->ssid.wep.default_len) {
612 hostapd_set_privacy(hapd, 1);
613 return 0;
614 }
615
Jouni Malinen75ecf522011-06-27 15:19:46 -0700616 /*
617 * When IEEE 802.1X is not enabled, the driver may need to know how to
618 * set authentication algorithms for static WEP.
619 */
620 hostapd_drv_set_authmode(hapd, hapd->conf->auth_algs);
621
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700622 for (i = 0; i < 4; i++) {
623 if (hapd->conf->ssid.wep.key[i] &&
Hai Shalomfdcde762020-04-02 11:19:20 -0700624 hostapd_drv_set_key(iface, hapd, WPA_ALG_WEP, NULL, i, 0,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700625 i == hapd->conf->ssid.wep.idx, NULL, 0,
626 hapd->conf->ssid.wep.key[i],
Hai Shalomfdcde762020-04-02 11:19:20 -0700627 hapd->conf->ssid.wep.len[i],
628 i == hapd->conf->ssid.wep.idx ?
629 KEY_FLAG_GROUP_RX_TX_DEFAULT :
630 KEY_FLAG_GROUP_RX_TX)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700631 wpa_printf(MSG_WARNING, "Could not set WEP "
632 "encryption.");
633 return -1;
634 }
635 if (hapd->conf->ssid.wep.key[i] &&
636 i == hapd->conf->ssid.wep.idx)
637 hostapd_set_privacy(hapd, 1);
638 }
639
640 return 0;
641}
642
Hai Shalomfdcde762020-04-02 11:19:20 -0700643#endif /* CONFIG_WEP */
644
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700645
Dmitry Shmidt04949592012-07-19 12:16:46 -0700646static int hostapd_flush_old_stations(struct hostapd_data *hapd, u16 reason)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700647{
648 int ret = 0;
649 u8 addr[ETH_ALEN];
650
651 if (hostapd_drv_none(hapd) || hapd->drv_priv == NULL)
652 return 0;
653
Dmitry Shmidta38abf92014-03-06 13:38:44 -0800654 if (!hapd->iface->driver_ap_teardown) {
655 wpa_dbg(hapd->msg_ctx, MSG_DEBUG,
656 "Flushing old station entries");
657
658 if (hostapd_flush(hapd)) {
659 wpa_msg(hapd->msg_ctx, MSG_WARNING,
660 "Could not connect to kernel driver");
661 ret = -1;
662 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700663 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700664 if (hapd->conf && hapd->conf->broadcast_deauth) {
665 wpa_dbg(hapd->msg_ctx, MSG_DEBUG,
666 "Deauthenticate all stations");
667 os_memset(addr, 0xff, ETH_ALEN);
668 hostapd_drv_sta_deauth(hapd, addr, reason);
669 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700670 hostapd_free_stas(hapd);
671
672 return ret;
673}
674
675
Hai Shalom60840252021-02-19 19:02:11 -0800676void hostapd_bss_deinit_no_free(struct hostapd_data *hapd)
Dmitry Shmidt71757432014-06-02 13:50:35 -0700677{
678 hostapd_free_stas(hapd);
679 hostapd_flush_old_stations(hapd, WLAN_REASON_DEAUTH_LEAVING);
Hai Shalomfdcde762020-04-02 11:19:20 -0700680#ifdef CONFIG_WEP
Dmitry Shmidt71757432014-06-02 13:50:35 -0700681 hostapd_clear_wep(hapd);
Hai Shalomfdcde762020-04-02 11:19:20 -0700682#endif /* CONFIG_WEP */
Dmitry Shmidt71757432014-06-02 13:50:35 -0700683}
684
685
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700686/**
687 * hostapd_validate_bssid_configuration - Validate BSSID configuration
688 * @iface: Pointer to interface data
689 * Returns: 0 on success, -1 on failure
690 *
691 * This function is used to validate that the configured BSSIDs are valid.
692 */
693static int hostapd_validate_bssid_configuration(struct hostapd_iface *iface)
694{
695 u8 mask[ETH_ALEN] = { 0 };
696 struct hostapd_data *hapd = iface->bss[0];
697 unsigned int i = iface->conf->num_bss, bits = 0, j;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700698 int auto_addr = 0;
699
700 if (hostapd_drv_none(hapd))
701 return 0;
702
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -0800703 if (iface->conf->use_driver_iface_addr)
704 return 0;
705
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700706 /* Generate BSSID mask that is large enough to cover the BSSIDs. */
707
708 /* Determine the bits necessary to cover the number of BSSIDs. */
709 for (i--; i; i >>= 1)
710 bits++;
711
712 /* Determine the bits necessary to any configured BSSIDs,
713 if they are higher than the number of BSSIDs. */
714 for (j = 0; j < iface->conf->num_bss; j++) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800715 if (is_zero_ether_addr(iface->conf->bss[j]->bssid)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700716 if (j)
717 auto_addr++;
718 continue;
719 }
720
721 for (i = 0; i < ETH_ALEN; i++) {
722 mask[i] |=
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800723 iface->conf->bss[j]->bssid[i] ^
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700724 hapd->own_addr[i];
725 }
726 }
727
728 if (!auto_addr)
729 goto skip_mask_ext;
730
731 for (i = 0; i < ETH_ALEN && mask[i] == 0; i++)
732 ;
733 j = 0;
734 if (i < ETH_ALEN) {
735 j = (5 - i) * 8;
736
737 while (mask[i] != 0) {
738 mask[i] >>= 1;
739 j++;
740 }
741 }
742
743 if (bits < j)
744 bits = j;
745
746 if (bits > 40) {
747 wpa_printf(MSG_ERROR, "Too many bits in the BSSID mask (%u)",
748 bits);
749 return -1;
750 }
751
752 os_memset(mask, 0xff, ETH_ALEN);
753 j = bits / 8;
754 for (i = 5; i > 5 - j; i--)
755 mask[i] = 0;
756 j = bits % 8;
Hai Shalom021b0b52019-04-10 11:17:58 -0700757 while (j) {
758 j--;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700759 mask[i] <<= 1;
Hai Shalom021b0b52019-04-10 11:17:58 -0700760 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700761
762skip_mask_ext:
763 wpa_printf(MSG_DEBUG, "BSS count %lu, BSSID mask " MACSTR " (%d bits)",
764 (unsigned long) iface->conf->num_bss, MAC2STR(mask), bits);
765
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700766 if (!auto_addr)
767 return 0;
768
769 for (i = 0; i < ETH_ALEN; i++) {
770 if ((hapd->own_addr[i] & mask[i]) != hapd->own_addr[i]) {
771 wpa_printf(MSG_ERROR, "Invalid BSSID mask " MACSTR
772 " for start address " MACSTR ".",
773 MAC2STR(mask), MAC2STR(hapd->own_addr));
774 wpa_printf(MSG_ERROR, "Start address must be the "
775 "first address in the block (i.e., addr "
776 "AND mask == addr).");
777 return -1;
778 }
779 }
780
781 return 0;
782}
783
784
785static int mac_in_conf(struct hostapd_config *conf, const void *a)
786{
787 size_t i;
788
789 for (i = 0; i < conf->num_bss; i++) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800790 if (hostapd_mac_comp(conf->bss[i]->bssid, a) == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700791 return 1;
792 }
793 }
794
795 return 0;
796}
797
798
Dmitry Shmidt04949592012-07-19 12:16:46 -0700799#ifndef CONFIG_NO_RADIUS
800
801static int hostapd_das_nas_mismatch(struct hostapd_data *hapd,
802 struct radius_das_attrs *attr)
803{
Dmitry Shmidt13ca8d82014-02-20 10:18:40 -0800804 if (attr->nas_identifier &&
805 (!hapd->conf->nas_identifier ||
806 os_strlen(hapd->conf->nas_identifier) !=
807 attr->nas_identifier_len ||
808 os_memcmp(hapd->conf->nas_identifier, attr->nas_identifier,
809 attr->nas_identifier_len) != 0)) {
810 wpa_printf(MSG_DEBUG, "RADIUS DAS: NAS-Identifier mismatch");
811 return 1;
812 }
813
814 if (attr->nas_ip_addr &&
815 (hapd->conf->own_ip_addr.af != AF_INET ||
816 os_memcmp(&hapd->conf->own_ip_addr.u.v4, attr->nas_ip_addr, 4) !=
817 0)) {
818 wpa_printf(MSG_DEBUG, "RADIUS DAS: NAS-IP-Address mismatch");
819 return 1;
820 }
821
822#ifdef CONFIG_IPV6
823 if (attr->nas_ipv6_addr &&
824 (hapd->conf->own_ip_addr.af != AF_INET6 ||
825 os_memcmp(&hapd->conf->own_ip_addr.u.v6, attr->nas_ipv6_addr, 16)
826 != 0)) {
827 wpa_printf(MSG_DEBUG, "RADIUS DAS: NAS-IPv6-Address mismatch");
828 return 1;
829 }
830#endif /* CONFIG_IPV6 */
831
Dmitry Shmidt04949592012-07-19 12:16:46 -0700832 return 0;
833}
834
835
836static struct sta_info * hostapd_das_find_sta(struct hostapd_data *hapd,
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800837 struct radius_das_attrs *attr,
838 int *multi)
Dmitry Shmidt04949592012-07-19 12:16:46 -0700839{
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800840 struct sta_info *selected, *sta;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700841 char buf[128];
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800842 int num_attr = 0;
843 int count;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700844
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800845 *multi = 0;
846
847 for (sta = hapd->sta_list; sta; sta = sta->next)
848 sta->radius_das_match = 1;
849
850 if (attr->sta_addr) {
851 num_attr++;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700852 sta = ap_get_sta(hapd, attr->sta_addr);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800853 if (!sta) {
854 wpa_printf(MSG_DEBUG,
855 "RADIUS DAS: No Calling-Station-Id match");
856 return NULL;
857 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700858
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800859 selected = sta;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700860 for (sta = hapd->sta_list; sta; sta = sta->next) {
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800861 if (sta != selected)
862 sta->radius_das_match = 0;
863 }
864 wpa_printf(MSG_DEBUG, "RADIUS DAS: Calling-Station-Id match");
865 }
866
867 if (attr->acct_session_id) {
868 num_attr++;
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800869 if (attr->acct_session_id_len != 16) {
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800870 wpa_printf(MSG_DEBUG,
871 "RADIUS DAS: Acct-Session-Id cannot match");
872 return NULL;
873 }
874 count = 0;
875
876 for (sta = hapd->sta_list; sta; sta = sta->next) {
877 if (!sta->radius_das_match)
878 continue;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800879 os_snprintf(buf, sizeof(buf), "%016llX",
880 (unsigned long long) sta->acct_session_id);
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800881 if (os_memcmp(attr->acct_session_id, buf, 16) != 0)
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800882 sta->radius_das_match = 0;
883 else
884 count++;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700885 }
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800886
887 if (count == 0) {
888 wpa_printf(MSG_DEBUG,
889 "RADIUS DAS: No matches remaining after Acct-Session-Id check");
890 return NULL;
891 }
892 wpa_printf(MSG_DEBUG, "RADIUS DAS: Acct-Session-Id match");
Dmitry Shmidt04949592012-07-19 12:16:46 -0700893 }
894
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800895 if (attr->acct_multi_session_id) {
896 num_attr++;
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800897 if (attr->acct_multi_session_id_len != 16) {
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800898 wpa_printf(MSG_DEBUG,
899 "RADIUS DAS: Acct-Multi-Session-Id cannot match");
900 return NULL;
901 }
902 count = 0;
903
904 for (sta = hapd->sta_list; sta; sta = sta->next) {
905 if (!sta->radius_das_match)
906 continue;
907 if (!sta->eapol_sm ||
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800908 !sta->eapol_sm->acct_multi_session_id) {
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800909 sta->radius_das_match = 0;
910 continue;
911 }
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800912 os_snprintf(buf, sizeof(buf), "%016llX",
913 (unsigned long long)
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800914 sta->eapol_sm->acct_multi_session_id);
915 if (os_memcmp(attr->acct_multi_session_id, buf, 16) !=
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800916 0)
917 sta->radius_das_match = 0;
918 else
919 count++;
920 }
921
922 if (count == 0) {
923 wpa_printf(MSG_DEBUG,
924 "RADIUS DAS: No matches remaining after Acct-Multi-Session-Id check");
925 return NULL;
926 }
927 wpa_printf(MSG_DEBUG,
928 "RADIUS DAS: Acct-Multi-Session-Id match");
929 }
930
931 if (attr->cui) {
932 num_attr++;
933 count = 0;
934
Dmitry Shmidt04949592012-07-19 12:16:46 -0700935 for (sta = hapd->sta_list; sta; sta = sta->next) {
936 struct wpabuf *cui;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800937
938 if (!sta->radius_das_match)
939 continue;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700940 cui = ieee802_1x_get_radius_cui(sta->eapol_sm);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800941 if (!cui || wpabuf_len(cui) != attr->cui_len ||
Dmitry Shmidt04949592012-07-19 12:16:46 -0700942 os_memcmp(wpabuf_head(cui), attr->cui,
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800943 attr->cui_len) != 0)
944 sta->radius_das_match = 0;
945 else
946 count++;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700947 }
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800948
949 if (count == 0) {
950 wpa_printf(MSG_DEBUG,
951 "RADIUS DAS: No matches remaining after Chargeable-User-Identity check");
952 return NULL;
953 }
954 wpa_printf(MSG_DEBUG,
955 "RADIUS DAS: Chargeable-User-Identity match");
Dmitry Shmidt04949592012-07-19 12:16:46 -0700956 }
957
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800958 if (attr->user_name) {
959 num_attr++;
960 count = 0;
961
Dmitry Shmidt04949592012-07-19 12:16:46 -0700962 for (sta = hapd->sta_list; sta; sta = sta->next) {
963 u8 *identity;
964 size_t identity_len;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800965
966 if (!sta->radius_das_match)
967 continue;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700968 identity = ieee802_1x_get_identity(sta->eapol_sm,
969 &identity_len);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800970 if (!identity ||
971 identity_len != attr->user_name_len ||
Dmitry Shmidt04949592012-07-19 12:16:46 -0700972 os_memcmp(identity, attr->user_name, identity_len)
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800973 != 0)
974 sta->radius_das_match = 0;
975 else
976 count++;
977 }
978
979 if (count == 0) {
980 wpa_printf(MSG_DEBUG,
981 "RADIUS DAS: No matches remaining after User-Name check");
982 return NULL;
983 }
984 wpa_printf(MSG_DEBUG,
985 "RADIUS DAS: User-Name match");
986 }
987
988 if (num_attr == 0) {
989 /*
990 * In theory, we could match all current associations, but it
991 * seems safer to just reject requests that do not include any
992 * session identification attributes.
993 */
994 wpa_printf(MSG_DEBUG,
995 "RADIUS DAS: No session identification attributes included");
996 return NULL;
997 }
998
999 selected = NULL;
1000 for (sta = hapd->sta_list; sta; sta = sta->next) {
1001 if (sta->radius_das_match) {
1002 if (selected) {
1003 *multi = 1;
1004 return NULL;
1005 }
1006 selected = sta;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001007 }
1008 }
1009
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001010 return selected;
1011}
1012
1013
1014static int hostapd_das_disconnect_pmksa(struct hostapd_data *hapd,
1015 struct radius_das_attrs *attr)
1016{
1017 if (!hapd->wpa_auth)
1018 return -1;
1019 return wpa_auth_radius_das_disconnect_pmksa(hapd->wpa_auth, attr);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001020}
1021
1022
1023static enum radius_das_res
1024hostapd_das_disconnect(void *ctx, struct radius_das_attrs *attr)
1025{
1026 struct hostapd_data *hapd = ctx;
1027 struct sta_info *sta;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001028 int multi;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001029
1030 if (hostapd_das_nas_mismatch(hapd, attr))
1031 return RADIUS_DAS_NAS_MISMATCH;
1032
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001033 sta = hostapd_das_find_sta(hapd, attr, &multi);
1034 if (sta == NULL) {
1035 if (multi) {
1036 wpa_printf(MSG_DEBUG,
1037 "RADIUS DAS: Multiple sessions match - not supported");
1038 return RADIUS_DAS_MULTI_SESSION_MATCH;
1039 }
1040 if (hostapd_das_disconnect_pmksa(hapd, attr) == 0) {
1041 wpa_printf(MSG_DEBUG,
1042 "RADIUS DAS: PMKSA cache entry matched");
1043 return RADIUS_DAS_SUCCESS;
1044 }
1045 wpa_printf(MSG_DEBUG, "RADIUS DAS: No matching session found");
Dmitry Shmidt04949592012-07-19 12:16:46 -07001046 return RADIUS_DAS_SESSION_NOT_FOUND;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001047 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001048
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001049 wpa_printf(MSG_DEBUG, "RADIUS DAS: Found a matching session " MACSTR
1050 " - disconnecting", MAC2STR(sta->addr));
Dmitry Shmidt13ca8d82014-02-20 10:18:40 -08001051 wpa_auth_pmksa_remove(hapd->wpa_auth, sta->addr);
1052
Dmitry Shmidt04949592012-07-19 12:16:46 -07001053 hostapd_drv_sta_deauth(hapd, sta->addr,
1054 WLAN_REASON_PREV_AUTH_NOT_VALID);
1055 ap_sta_deauthenticate(hapd, sta, WLAN_REASON_PREV_AUTH_NOT_VALID);
1056
1057 return RADIUS_DAS_SUCCESS;
1058}
1059
Roshan Pius3a1667e2018-07-03 15:17:14 -07001060
1061#ifdef CONFIG_HS20
1062static enum radius_das_res
1063hostapd_das_coa(void *ctx, struct radius_das_attrs *attr)
1064{
1065 struct hostapd_data *hapd = ctx;
1066 struct sta_info *sta;
1067 int multi;
1068
1069 if (hostapd_das_nas_mismatch(hapd, attr))
1070 return RADIUS_DAS_NAS_MISMATCH;
1071
1072 sta = hostapd_das_find_sta(hapd, attr, &multi);
1073 if (!sta) {
1074 if (multi) {
1075 wpa_printf(MSG_DEBUG,
1076 "RADIUS DAS: Multiple sessions match - not supported");
1077 return RADIUS_DAS_MULTI_SESSION_MATCH;
1078 }
1079 wpa_printf(MSG_DEBUG, "RADIUS DAS: No matching session found");
1080 return RADIUS_DAS_SESSION_NOT_FOUND;
1081 }
1082
1083 wpa_printf(MSG_DEBUG, "RADIUS DAS: Found a matching session " MACSTR
1084 " - CoA", MAC2STR(sta->addr));
1085
1086 if (attr->hs20_t_c_filtering) {
1087 if (attr->hs20_t_c_filtering[0] & BIT(0)) {
1088 wpa_printf(MSG_DEBUG,
1089 "HS 2.0: Unexpected Terms and Conditions filtering required in CoA-Request");
1090 return RADIUS_DAS_COA_FAILED;
1091 }
1092
1093 hs20_t_c_filtering(hapd, sta, 0);
1094 }
1095
1096 return RADIUS_DAS_SUCCESS;
1097}
1098#else /* CONFIG_HS20 */
1099#define hostapd_das_coa NULL
1100#endif /* CONFIG_HS20 */
1101
Hai Shalomc3565922019-10-28 11:58:20 -07001102
1103#ifdef CONFIG_SQLITE
1104
1105static int db_table_exists(sqlite3 *db, const char *name)
1106{
1107 char cmd[128];
1108
1109 os_snprintf(cmd, sizeof(cmd), "SELECT 1 FROM %s;", name);
1110 return sqlite3_exec(db, cmd, NULL, NULL, NULL) == SQLITE_OK;
1111}
1112
1113
1114static int db_table_create_radius_attributes(sqlite3 *db)
1115{
1116 char *err = NULL;
1117 const char *sql =
1118 "CREATE TABLE radius_attributes("
1119 " id INTEGER PRIMARY KEY,"
1120 " sta TEXT,"
1121 " reqtype TEXT,"
1122 " attr TEXT"
1123 ");"
1124 "CREATE INDEX idx_sta_reqtype ON radius_attributes(sta,reqtype);";
1125
1126 wpa_printf(MSG_DEBUG,
1127 "Adding database table for RADIUS attribute information");
1128 if (sqlite3_exec(db, sql, NULL, NULL, &err) != SQLITE_OK) {
1129 wpa_printf(MSG_ERROR, "SQLite error: %s", err);
1130 sqlite3_free(err);
1131 return -1;
1132 }
1133
1134 return 0;
1135}
1136
1137#endif /* CONFIG_SQLITE */
1138
Dmitry Shmidt04949592012-07-19 12:16:46 -07001139#endif /* CONFIG_NO_RADIUS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001140
1141
Sunil Ravi77d572f2023-01-17 23:58:31 +00001142static int hostapd_start_beacon(struct hostapd_data *hapd,
1143 bool flush_old_stations)
1144{
1145 struct hostapd_bss_config *conf = hapd->conf;
1146
1147 if (!conf->start_disabled && ieee802_11_set_beacon(hapd) < 0)
1148 return -1;
1149
1150 if (flush_old_stations && !conf->start_disabled &&
1151 conf->broadcast_deauth) {
1152 u8 addr[ETH_ALEN];
1153
1154 /* Should any previously associated STA not have noticed that
1155 * the AP had stopped and restarted, send one more
1156 * deauthentication notification now that the AP is ready to
1157 * operate. */
1158 wpa_dbg(hapd->msg_ctx, MSG_DEBUG,
1159 "Deauthenticate all stations at BSS start");
1160 os_memset(addr, 0xff, ETH_ALEN);
1161 hostapd_drv_sta_deauth(hapd, addr,
1162 WLAN_REASON_PREV_AUTH_NOT_VALID);
1163 }
1164
1165 if (hapd->driver && hapd->driver->set_operstate)
1166 hapd->driver->set_operstate(hapd->drv_priv, 1);
1167
1168 return 0;
1169}
1170
1171
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001172/**
1173 * hostapd_setup_bss - Per-BSS setup (initialization)
1174 * @hapd: Pointer to BSS data
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001175 * @first: Whether this BSS is the first BSS of an interface; -1 = not first,
1176 * but interface may exist
Sunil Ravi77d572f2023-01-17 23:58:31 +00001177 * @start_beacon: Whether Beacon frame template should be configured and
1178 * transmission of Beaconf rames started at this time. This is used when
1179 * MBSSID element is enabled where the information regarding all BSSes
1180 * should be retrieved before configuring the Beacon frame template. The
1181 * calling functions are responsible for configuring the Beacon frame
1182 * explicitly if this is set to false.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001183 *
1184 * This function is used to initialize all per-BSS data structures and
1185 * resources. This gets called in a loop for each BSS when an interface is
1186 * initialized. Most of the modules that are initialized here will be
1187 * deinitialized in hostapd_cleanup().
1188 */
Sunil Ravi77d572f2023-01-17 23:58:31 +00001189static int hostapd_setup_bss(struct hostapd_data *hapd, int first,
1190 bool start_beacon)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001191{
1192 struct hostapd_bss_config *conf = hapd->conf;
Dmitry Shmidt9d9e6022015-04-23 10:34:55 -07001193 u8 ssid[SSID_MAX_LEN + 1];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001194 int ssid_len, set_ssid;
1195 char force_ifname[IFNAMSIZ];
1196 u8 if_addr[ETH_ALEN];
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001197 int flush_old_stations = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001198
Dmitry Shmidt54605472013-11-08 11:10:19 -08001199 wpa_printf(MSG_DEBUG, "%s(hapd=%p (%s), first=%d)",
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001200 __func__, hapd, conf->iface, first);
Dmitry Shmidt54605472013-11-08 11:10:19 -08001201
Dmitry Shmidt50b691d2014-05-21 14:01:45 -07001202#ifdef EAP_SERVER_TNC
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001203 if (conf->tnc && tncs_global_init() < 0) {
Dmitry Shmidt50b691d2014-05-21 14:01:45 -07001204 wpa_printf(MSG_ERROR, "Failed to initialize TNCS");
1205 return -1;
1206 }
1207#endif /* EAP_SERVER_TNC */
1208
Dmitry Shmidt54605472013-11-08 11:10:19 -08001209 if (hapd->started) {
1210 wpa_printf(MSG_ERROR, "%s: Interface %s was already started",
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001211 __func__, conf->iface);
Dmitry Shmidt54605472013-11-08 11:10:19 -08001212 return -1;
1213 }
1214 hapd->started = 1;
1215
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001216 if (!first || first == -1) {
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08001217 u8 *addr = hapd->own_addr;
1218
1219 if (!is_zero_ether_addr(conf->bssid)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001220 /* Allocate the configured BSSID. */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001221 os_memcpy(hapd->own_addr, conf->bssid, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001222
1223 if (hostapd_mac_comp(hapd->own_addr,
1224 hapd->iface->bss[0]->own_addr) ==
1225 0) {
1226 wpa_printf(MSG_ERROR, "BSS '%s' may not have "
1227 "BSSID set to the MAC address of "
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001228 "the radio", conf->iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001229 return -1;
1230 }
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08001231 } else if (hapd->iconf->use_driver_iface_addr) {
1232 addr = NULL;
1233 } else {
1234 /* Allocate the next available BSSID. */
1235 do {
1236 inc_byte_array(hapd->own_addr, ETH_ALEN);
1237 } while (mac_in_conf(hapd->iconf, hapd->own_addr));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001238 }
1239
Dmitry Shmidt54605472013-11-08 11:10:19 -08001240 hapd->interface_added = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001241 if (hostapd_if_add(hapd->iface->bss[0], WPA_IF_AP_BSS,
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08001242 conf->iface, addr, hapd,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001243 &hapd->drv_priv, force_ifname, if_addr,
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001244 conf->bridge[0] ? conf->bridge : NULL,
1245 first == -1)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001246 wpa_printf(MSG_ERROR, "Failed to add BSS (BSSID="
1247 MACSTR ")", MAC2STR(hapd->own_addr));
Dmitry Shmidt3cf6f792013-12-18 13:12:19 -08001248 hapd->interface_added = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001249 return -1;
1250 }
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08001251
1252 if (!addr)
1253 os_memcpy(hapd->own_addr, if_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001254 }
1255
1256 if (conf->wmm_enabled < 0)
Hai Shaloma20dcd72022-02-04 13:43:00 -08001257 conf->wmm_enabled = hapd->iconf->ieee80211n |
1258 hapd->iconf->ieee80211ax;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001259
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001260#ifdef CONFIG_IEEE80211R_AP
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001261 if (is_zero_ether_addr(conf->r1_key_holder))
1262 os_memcpy(conf->r1_key_holder, hapd->own_addr, ETH_ALEN);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001263#endif /* CONFIG_IEEE80211R_AP */
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001264
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001265#ifdef CONFIG_MESH
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001266 if ((hapd->conf->mesh & MESH_ENABLED) && hapd->iface->mconf == NULL)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001267 flush_old_stations = 0;
1268#endif /* CONFIG_MESH */
1269
1270 if (flush_old_stations)
Hai Shalom4fbc08f2020-05-18 12:37:00 -07001271 hostapd_flush(hapd);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001272 hostapd_set_privacy(hapd, 0);
1273
Hai Shalomfdcde762020-04-02 11:19:20 -07001274#ifdef CONFIG_WEP
Hai Shalom4fbc08f2020-05-18 12:37:00 -07001275 if (!hostapd_drv_nl80211(hapd))
1276 hostapd_broadcast_wep_clear(hapd);
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001277 if (hostapd_setup_encryption(conf->iface, hapd))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001278 return -1;
Hai Shalomfdcde762020-04-02 11:19:20 -07001279#endif /* CONFIG_WEP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001280
1281 /*
1282 * Fetch the SSID from the system and use it or,
1283 * if one was specified in the config file, verify they
1284 * match.
1285 */
1286 ssid_len = hostapd_get_ssid(hapd, ssid, sizeof(ssid));
1287 if (ssid_len < 0) {
1288 wpa_printf(MSG_ERROR, "Could not read SSID from system");
1289 return -1;
1290 }
1291 if (conf->ssid.ssid_set) {
1292 /*
1293 * If SSID is specified in the config file and it differs
1294 * from what is being used then force installation of the
1295 * new SSID.
1296 */
1297 set_ssid = (conf->ssid.ssid_len != (size_t) ssid_len ||
1298 os_memcmp(conf->ssid.ssid, ssid, ssid_len) != 0);
1299 } else {
1300 /*
1301 * No SSID in the config file; just use the one we got
1302 * from the system.
1303 */
1304 set_ssid = 0;
1305 conf->ssid.ssid_len = ssid_len;
1306 os_memcpy(conf->ssid.ssid, ssid, conf->ssid.ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001307 }
1308
Hai Shalomfdcde762020-04-02 11:19:20 -07001309 /*
1310 * Short SSID calculation is identical to FCS and it is defined in
1311 * IEEE P802.11-REVmd/D3.0, 9.4.2.170.3 (Calculating the Short-SSID).
1312 */
Sunil Ravi89eba102022-09-13 21:04:37 -07001313 conf->ssid.short_ssid = ieee80211_crc32(conf->ssid.ssid,
1314 conf->ssid.ssid_len);
Hai Shalomfdcde762020-04-02 11:19:20 -07001315
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001316 if (!hostapd_drv_none(hapd)) {
Hai Shalomfdcde762020-04-02 11:19:20 -07001317 wpa_printf(MSG_DEBUG, "Using interface %s with hwaddr " MACSTR
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001318 " and ssid \"%s\"",
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001319 conf->iface, MAC2STR(hapd->own_addr),
1320 wpa_ssid_txt(conf->ssid.ssid, conf->ssid.ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001321 }
1322
1323 if (hostapd_setup_wpa_psk(conf)) {
1324 wpa_printf(MSG_ERROR, "WPA-PSK setup failed.");
1325 return -1;
1326 }
1327
1328 /* Set SSID for the kernel driver (to be used in beacon and probe
1329 * response frames) */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001330 if (set_ssid && hostapd_set_ssid(hapd, conf->ssid.ssid,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001331 conf->ssid.ssid_len)) {
1332 wpa_printf(MSG_ERROR, "Could not set SSID for kernel driver");
1333 return -1;
1334 }
1335
Dmitry Shmidt818ea482014-03-10 13:15:21 -07001336 if (wpa_debug_level <= MSG_MSGDUMP)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001337 conf->radius->msg_dumps = 1;
1338#ifndef CONFIG_NO_RADIUS
Hai Shalomc3565922019-10-28 11:58:20 -07001339
1340#ifdef CONFIG_SQLITE
1341 if (conf->radius_req_attr_sqlite) {
1342 if (sqlite3_open(conf->radius_req_attr_sqlite,
1343 &hapd->rad_attr_db)) {
1344 wpa_printf(MSG_ERROR, "Could not open SQLite file '%s'",
1345 conf->radius_req_attr_sqlite);
1346 return -1;
1347 }
1348
1349 wpa_printf(MSG_DEBUG, "Opening RADIUS attribute database: %s",
1350 conf->radius_req_attr_sqlite);
1351 if (!db_table_exists(hapd->rad_attr_db, "radius_attributes") &&
1352 db_table_create_radius_attributes(hapd->rad_attr_db) < 0)
1353 return -1;
1354 }
1355#endif /* CONFIG_SQLITE */
1356
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001357 hapd->radius = radius_client_init(hapd, conf->radius);
1358 if (hapd->radius == NULL) {
1359 wpa_printf(MSG_ERROR, "RADIUS client initialization failed.");
1360 return -1;
1361 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001362
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001363 if (conf->radius_das_port) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001364 struct radius_das_conf das_conf;
1365 os_memset(&das_conf, 0, sizeof(das_conf));
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001366 das_conf.port = conf->radius_das_port;
1367 das_conf.shared_secret = conf->radius_das_shared_secret;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001368 das_conf.shared_secret_len =
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001369 conf->radius_das_shared_secret_len;
1370 das_conf.client_addr = &conf->radius_das_client_addr;
1371 das_conf.time_window = conf->radius_das_time_window;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001372 das_conf.require_event_timestamp =
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001373 conf->radius_das_require_event_timestamp;
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07001374 das_conf.require_message_authenticator =
1375 conf->radius_das_require_message_authenticator;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001376 das_conf.ctx = hapd;
1377 das_conf.disconnect = hostapd_das_disconnect;
Roshan Pius3a1667e2018-07-03 15:17:14 -07001378 das_conf.coa = hostapd_das_coa;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001379 hapd->radius_das = radius_das_init(&das_conf);
1380 if (hapd->radius_das == NULL) {
1381 wpa_printf(MSG_ERROR, "RADIUS DAS initialization "
1382 "failed.");
1383 return -1;
1384 }
1385 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001386#endif /* CONFIG_NO_RADIUS */
1387
1388 if (hostapd_acl_init(hapd)) {
1389 wpa_printf(MSG_ERROR, "ACL initialization failed.");
1390 return -1;
1391 }
1392 if (hostapd_init_wps(hapd, conf))
1393 return -1;
1394
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001395#ifdef CONFIG_DPP
1396 hapd->gas = gas_query_ap_init(hapd, hapd->msg_ctx);
1397 if (!hapd->gas)
1398 return -1;
1399 if (hostapd_dpp_init(hapd))
1400 return -1;
1401#endif /* CONFIG_DPP */
1402
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001403 if (authsrv_init(hapd) < 0)
1404 return -1;
1405
1406 if (ieee802_1x_init(hapd)) {
1407 wpa_printf(MSG_ERROR, "IEEE 802.1X initialization failed.");
1408 return -1;
1409 }
1410
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001411 if ((conf->wpa || conf->osen) && hostapd_setup_wpa(hapd))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001412 return -1;
1413
1414 if (accounting_init(hapd)) {
1415 wpa_printf(MSG_ERROR, "Accounting initialization failed.");
1416 return -1;
1417 }
1418
Dmitry Shmidt04949592012-07-19 12:16:46 -07001419#ifdef CONFIG_INTERWORKING
1420 if (gas_serv_init(hapd)) {
1421 wpa_printf(MSG_ERROR, "GAS server initialization failed");
1422 return -1;
1423 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001424
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001425 if (conf->qos_map_set_len &&
1426 hostapd_drv_set_qos_map(hapd, conf->qos_map_set,
1427 conf->qos_map_set_len)) {
1428 wpa_printf(MSG_ERROR, "Failed to initialize QoS Map");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001429 return -1;
1430 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001431#endif /* CONFIG_INTERWORKING */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001432
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001433 if (conf->bss_load_update_period && bss_load_update_init(hapd)) {
1434 wpa_printf(MSG_ERROR, "BSS Load initialization failed");
1435 return -1;
1436 }
1437
1438 if (conf->proxy_arp) {
1439 if (x_snoop_init(hapd)) {
1440 wpa_printf(MSG_ERROR,
1441 "Generic snooping infrastructure initialization failed");
1442 return -1;
1443 }
1444
1445 if (dhcp_snoop_init(hapd)) {
1446 wpa_printf(MSG_ERROR,
1447 "DHCP snooping initialization failed");
1448 return -1;
1449 }
1450
1451 if (ndisc_snoop_init(hapd)) {
1452 wpa_printf(MSG_ERROR,
1453 "Neighbor Discovery snooping initialization failed");
1454 return -1;
1455 }
1456 }
1457
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001458 if (!hostapd_drv_none(hapd) && vlan_init(hapd)) {
1459 wpa_printf(MSG_ERROR, "VLAN initialization failed.");
1460 return -1;
1461 }
1462
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001463 if (hapd->wpa_auth && wpa_init_keys(hapd->wpa_auth) < 0)
1464 return -1;
1465
Sunil Ravi77d572f2023-01-17 23:58:31 +00001466 if (start_beacon)
1467 return hostapd_start_beacon(hapd, flush_old_stations);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001468
1469 return 0;
1470}
1471
1472
1473static void hostapd_tx_queue_params(struct hostapd_iface *iface)
1474{
1475 struct hostapd_data *hapd = iface->bss[0];
1476 int i;
1477 struct hostapd_tx_queue_params *p;
1478
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001479#ifdef CONFIG_MESH
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001480 if ((hapd->conf->mesh & MESH_ENABLED) && iface->mconf == NULL)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001481 return;
1482#endif /* CONFIG_MESH */
1483
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001484 for (i = 0; i < NUM_TX_QUEUES; i++) {
1485 p = &iface->conf->tx_queue[i];
1486
1487 if (hostapd_set_tx_queue_params(hapd, i, p->aifs, p->cwmin,
1488 p->cwmax, p->burst)) {
1489 wpa_printf(MSG_DEBUG, "Failed to set TX queue "
1490 "parameters for queue %d.", i);
1491 /* Continue anyway */
1492 }
1493 }
1494}
1495
1496
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001497static int hostapd_set_acl_list(struct hostapd_data *hapd,
1498 struct mac_acl_entry *mac_acl,
1499 int n_entries, u8 accept_acl)
1500{
1501 struct hostapd_acl_params *acl_params;
1502 int i, err;
1503
1504 acl_params = os_zalloc(sizeof(*acl_params) +
1505 (n_entries * sizeof(acl_params->mac_acl[0])));
1506 if (!acl_params)
1507 return -ENOMEM;
1508
1509 for (i = 0; i < n_entries; i++)
1510 os_memcpy(acl_params->mac_acl[i].addr, mac_acl[i].addr,
1511 ETH_ALEN);
1512
1513 acl_params->acl_policy = accept_acl;
1514 acl_params->num_mac_acl = n_entries;
1515
1516 err = hostapd_drv_set_acl(hapd, acl_params);
1517
1518 os_free(acl_params);
1519
1520 return err;
1521}
1522
1523
Sunil Ravia04bd252022-05-02 22:54:18 -07001524int hostapd_set_acl(struct hostapd_data *hapd)
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001525{
1526 struct hostapd_config *conf = hapd->iconf;
Sunil Ravia04bd252022-05-02 22:54:18 -07001527 int err = 0;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001528 u8 accept_acl;
1529
1530 if (hapd->iface->drv_max_acl_mac_addrs == 0)
Sunil Ravia04bd252022-05-02 22:54:18 -07001531 return 0;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001532
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001533 if (conf->bss[0]->macaddr_acl == DENY_UNLESS_ACCEPTED) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001534 accept_acl = 1;
1535 err = hostapd_set_acl_list(hapd, conf->bss[0]->accept_mac,
1536 conf->bss[0]->num_accept_mac,
1537 accept_acl);
1538 if (err) {
1539 wpa_printf(MSG_DEBUG, "Failed to set accept acl");
Sunil Ravia04bd252022-05-02 22:54:18 -07001540 return -1;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001541 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001542 } else if (conf->bss[0]->macaddr_acl == ACCEPT_UNLESS_DENIED) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001543 accept_acl = 0;
1544 err = hostapd_set_acl_list(hapd, conf->bss[0]->deny_mac,
1545 conf->bss[0]->num_deny_mac,
1546 accept_acl);
1547 if (err) {
1548 wpa_printf(MSG_DEBUG, "Failed to set deny acl");
Sunil Ravia04bd252022-05-02 22:54:18 -07001549 return -1;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001550 }
1551 }
Sunil Ravia04bd252022-05-02 22:54:18 -07001552 return err;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001553}
1554
1555
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001556static int start_ctrl_iface_bss(struct hostapd_data *hapd)
1557{
1558 if (!hapd->iface->interfaces ||
1559 !hapd->iface->interfaces->ctrl_iface_init)
1560 return 0;
1561
1562 if (hapd->iface->interfaces->ctrl_iface_init(hapd)) {
1563 wpa_printf(MSG_ERROR,
1564 "Failed to setup control interface for %s",
1565 hapd->conf->iface);
1566 return -1;
1567 }
1568
1569 return 0;
1570}
1571
1572
1573static int start_ctrl_iface(struct hostapd_iface *iface)
1574{
1575 size_t i;
1576
1577 if (!iface->interfaces || !iface->interfaces->ctrl_iface_init)
1578 return 0;
1579
1580 for (i = 0; i < iface->num_bss; i++) {
1581 struct hostapd_data *hapd = iface->bss[i];
1582 if (iface->interfaces->ctrl_iface_init(hapd)) {
1583 wpa_printf(MSG_ERROR,
1584 "Failed to setup control interface for %s",
1585 hapd->conf->iface);
1586 return -1;
1587 }
1588 }
1589
1590 return 0;
1591}
1592
1593
1594static void channel_list_update_timeout(void *eloop_ctx, void *timeout_ctx)
1595{
1596 struct hostapd_iface *iface = eloop_ctx;
1597
1598 if (!iface->wait_channel_update) {
1599 wpa_printf(MSG_INFO, "Channel list update timeout, but interface was not waiting for it");
1600 return;
1601 }
1602
1603 /*
1604 * It is possible that the existing channel list is acceptable, so try
1605 * to proceed.
1606 */
1607 wpa_printf(MSG_DEBUG, "Channel list update timeout - try to continue anyway");
1608 setup_interface2(iface);
1609}
1610
1611
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001612void hostapd_channel_list_updated(struct hostapd_iface *iface, int initiator)
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001613{
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001614 if (!iface->wait_channel_update || initiator != REGDOM_SET_BY_USER)
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001615 return;
1616
1617 wpa_printf(MSG_DEBUG, "Channel list updated - continue setup");
1618 eloop_cancel_timeout(channel_list_update_timeout, iface, NULL);
1619 setup_interface2(iface);
1620}
1621
1622
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001623static int setup_interface(struct hostapd_iface *iface)
1624{
1625 struct hostapd_data *hapd = iface->bss[0];
1626 size_t i;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001627
Dmitry Shmidta38abf92014-03-06 13:38:44 -08001628 /*
1629 * It is possible that setup_interface() is called after the interface
1630 * was disabled etc., in which case driver_ap_teardown is possibly set
1631 * to 1. Clear it here so any other key/station deletion, which is not
1632 * part of a teardown flow, would also call the relevant driver
1633 * callbacks.
1634 */
1635 iface->driver_ap_teardown = 0;
1636
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001637 if (!iface->phy[0]) {
1638 const char *phy = hostapd_drv_get_radio_name(hapd);
1639 if (phy) {
1640 wpa_printf(MSG_DEBUG, "phy: %s", phy);
1641 os_strlcpy(iface->phy, phy, sizeof(iface->phy));
1642 }
1643 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001644
1645 /*
1646 * Make sure that all BSSes get configured with a pointer to the same
1647 * driver interface.
1648 */
1649 for (i = 1; i < iface->num_bss; i++) {
1650 iface->bss[i]->driver = hapd->driver;
1651 iface->bss[i]->drv_priv = hapd->drv_priv;
1652 }
1653
1654 if (hostapd_validate_bssid_configuration(iface))
1655 return -1;
1656
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001657 /*
1658 * Initialize control interfaces early to allow external monitoring of
1659 * channel setup operations that may take considerable amount of time
1660 * especially for DFS cases.
1661 */
1662 if (start_ctrl_iface(iface))
1663 return -1;
1664
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001665 if (hapd->iconf->country[0] && hapd->iconf->country[1]) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001666 char country[4], previous_country[4];
1667
1668 hostapd_set_state(iface, HAPD_IFACE_COUNTRY_UPDATE);
1669 if (hostapd_get_country(hapd, previous_country) < 0)
1670 previous_country[0] = '\0';
1671
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001672 os_memcpy(country, hapd->iconf->country, 3);
1673 country[3] = '\0';
1674 if (hostapd_set_country(hapd, country) < 0) {
1675 wpa_printf(MSG_ERROR, "Failed to set country code");
1676 return -1;
1677 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001678
1679 wpa_printf(MSG_DEBUG, "Previous country code %s, new country code %s",
1680 previous_country, country);
1681
1682 if (os_strncmp(previous_country, country, 2) != 0) {
1683 wpa_printf(MSG_DEBUG, "Continue interface setup after channel list update");
1684 iface->wait_channel_update = 1;
Dmitry Shmidt97672262014-02-03 13:02:54 -08001685 eloop_register_timeout(5, 0,
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001686 channel_list_update_timeout,
1687 iface, NULL);
1688 return 0;
1689 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001690 }
1691
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001692 return setup_interface2(iface);
1693}
1694
1695
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001696static int configured_fixed_chan_to_freq(struct hostapd_iface *iface)
1697{
1698 int freq, i, j;
1699
1700 if (!iface->conf->channel)
1701 return 0;
1702 if (iface->conf->op_class) {
1703 freq = ieee80211_chan_to_freq(NULL, iface->conf->op_class,
1704 iface->conf->channel);
1705 if (freq < 0) {
1706 wpa_printf(MSG_INFO,
1707 "Could not convert op_class %u channel %u to operating frequency",
1708 iface->conf->op_class, iface->conf->channel);
1709 return -1;
1710 }
1711 iface->freq = freq;
1712 return 0;
1713 }
1714
1715 /* Old configurations using only 2.4/5/60 GHz bands may not specify the
1716 * op_class parameter. Select a matching channel from the configured
1717 * mode using the channel parameter for these cases.
1718 */
1719 for (j = 0; j < iface->num_hw_features; j++) {
1720 struct hostapd_hw_modes *mode = &iface->hw_features[j];
1721
1722 if (iface->conf->hw_mode != HOSTAPD_MODE_IEEE80211ANY &&
1723 iface->conf->hw_mode != mode->mode)
1724 continue;
1725 for (i = 0; i < mode->num_channels; i++) {
1726 struct hostapd_channel_data *chan = &mode->channels[i];
1727
1728 if (chan->chan == iface->conf->channel &&
1729 !is_6ghz_freq(chan->freq)) {
1730 iface->freq = chan->freq;
1731 return 0;
1732 }
1733 }
1734 }
1735
1736 wpa_printf(MSG_INFO, "Could not determine operating frequency");
1737 return -1;
1738}
1739
1740
Hai Shaloma20dcd72022-02-04 13:43:00 -08001741static void hostapd_set_6ghz_sec_chan(struct hostapd_iface *iface)
1742{
1743 int bw, seg0;
1744
1745 if (!is_6ghz_op_class(iface->conf->op_class))
1746 return;
1747
1748 seg0 = hostapd_get_oper_centr_freq_seg0_idx(iface->conf);
1749 bw = center_idx_to_bw_6ghz(seg0);
1750 /* Assign the secondary channel if absent in config for
1751 * bandwidths > 20 MHz */
1752 if (bw > 20 && !iface->conf->secondary_channel) {
1753 if (((iface->conf->channel - 1) / 4) % 2)
1754 iface->conf->secondary_channel = -1;
1755 else
1756 iface->conf->secondary_channel = 1;
1757 }
1758}
1759
1760
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001761static int setup_interface2(struct hostapd_iface *iface)
1762{
1763 iface->wait_channel_update = 0;
1764
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001765 if (hostapd_get_hw_features(iface)) {
1766 /* Not all drivers support this yet, so continue without hw
1767 * feature data. */
1768 } else {
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001769 int ret;
1770
1771 ret = configured_fixed_chan_to_freq(iface);
1772 if (ret < 0)
1773 goto fail;
1774
1775 if (iface->conf->op_class) {
Sunil8cd6f4d2022-06-28 18:40:46 +00001776 enum oper_chan_width ch_width;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001777
1778 ch_width = op_class_to_ch_width(iface->conf->op_class);
1779 hostapd_set_oper_chwidth(iface->conf, ch_width);
Hai Shaloma20dcd72022-02-04 13:43:00 -08001780 hostapd_set_6ghz_sec_chan(iface);
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001781 }
1782
1783 ret = hostapd_select_hw_mode(iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001784 if (ret < 0) {
1785 wpa_printf(MSG_ERROR, "Could not select hw_mode and "
1786 "channel. (%d)", ret);
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001787 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001788 }
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001789 if (ret == 1) {
1790 wpa_printf(MSG_DEBUG, "Interface initialization will be completed in a callback (ACS)");
1791 return 0;
1792 }
Hai Shalomc3565922019-10-28 11:58:20 -07001793 ret = hostapd_check_edmg_capab(iface);
1794 if (ret < 0)
1795 goto fail;
Hai Shalom60840252021-02-19 19:02:11 -08001796 ret = hostapd_check_he_6ghz_capab(iface);
1797 if (ret < 0)
1798 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001799 ret = hostapd_check_ht_capab(iface);
1800 if (ret < 0)
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001801 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001802 if (ret == 1) {
1803 wpa_printf(MSG_DEBUG, "Interface initialization will "
1804 "be completed in a callback");
1805 return 0;
1806 }
Dmitry Shmidt051af732013-10-22 13:52:46 -07001807
1808 if (iface->conf->ieee80211h)
1809 wpa_printf(MSG_DEBUG, "DFS support is enabled");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001810 }
1811 return hostapd_setup_interface_complete(iface, 0);
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001812
1813fail:
1814 hostapd_set_state(iface, HAPD_IFACE_DISABLED);
1815 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
1816 if (iface->interfaces && iface->interfaces->terminate_on_error)
1817 eloop_terminate();
1818 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001819}
1820
1821
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001822#ifdef CONFIG_FST
1823
1824static const u8 * fst_hostapd_get_bssid_cb(void *ctx)
1825{
1826 struct hostapd_data *hapd = ctx;
1827
1828 return hapd->own_addr;
1829}
1830
1831
1832static void fst_hostapd_get_channel_info_cb(void *ctx,
1833 enum hostapd_hw_mode *hw_mode,
1834 u8 *channel)
1835{
1836 struct hostapd_data *hapd = ctx;
1837
1838 *hw_mode = ieee80211_freq_to_chan(hapd->iface->freq, channel);
1839}
1840
1841
Sunil8cd6f4d2022-06-28 18:40:46 +00001842static int fst_hostapd_get_hw_modes_cb(void *ctx,
1843 struct hostapd_hw_modes **modes)
1844{
1845 struct hostapd_data *hapd = ctx;
1846
1847 *modes = hapd->iface->hw_features;
1848 return hapd->iface->num_hw_features;
1849}
1850
1851
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001852static void fst_hostapd_set_ies_cb(void *ctx, const struct wpabuf *fst_ies)
1853{
1854 struct hostapd_data *hapd = ctx;
1855
1856 if (hapd->iface->fst_ies != fst_ies) {
1857 hapd->iface->fst_ies = fst_ies;
1858 if (ieee802_11_set_beacon(hapd))
1859 wpa_printf(MSG_WARNING, "FST: Cannot set beacon");
1860 }
1861}
1862
1863
1864static int fst_hostapd_send_action_cb(void *ctx, const u8 *da,
1865 struct wpabuf *buf)
1866{
1867 struct hostapd_data *hapd = ctx;
1868
1869 return hostapd_drv_send_action(hapd, hapd->iface->freq, 0, da,
1870 wpabuf_head(buf), wpabuf_len(buf));
1871}
1872
1873
1874static const struct wpabuf * fst_hostapd_get_mb_ie_cb(void *ctx, const u8 *addr)
1875{
1876 struct hostapd_data *hapd = ctx;
1877 struct sta_info *sta = ap_get_sta(hapd, addr);
1878
1879 return sta ? sta->mb_ies : NULL;
1880}
1881
1882
1883static void fst_hostapd_update_mb_ie_cb(void *ctx, const u8 *addr,
1884 const u8 *buf, size_t size)
1885{
1886 struct hostapd_data *hapd = ctx;
1887 struct sta_info *sta = ap_get_sta(hapd, addr);
1888
1889 if (sta) {
1890 struct mb_ies_info info;
1891
1892 if (!mb_ies_info_by_ies(&info, buf, size)) {
1893 wpabuf_free(sta->mb_ies);
1894 sta->mb_ies = mb_ies_by_info(&info);
1895 }
1896 }
1897}
1898
1899
1900static const u8 * fst_hostapd_get_sta(struct fst_get_peer_ctx **get_ctx,
Hai Shalome21d4e82020-04-29 16:34:06 -07001901 bool mb_only)
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001902{
1903 struct sta_info *s = (struct sta_info *) *get_ctx;
1904
1905 if (mb_only) {
1906 for (; s && !s->mb_ies; s = s->next)
1907 ;
1908 }
1909
1910 if (s) {
1911 *get_ctx = (struct fst_get_peer_ctx *) s->next;
1912
1913 return s->addr;
1914 }
1915
1916 *get_ctx = NULL;
1917 return NULL;
1918}
1919
1920
1921static const u8 * fst_hostapd_get_peer_first(void *ctx,
1922 struct fst_get_peer_ctx **get_ctx,
Hai Shalome21d4e82020-04-29 16:34:06 -07001923 bool mb_only)
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001924{
1925 struct hostapd_data *hapd = ctx;
1926
1927 *get_ctx = (struct fst_get_peer_ctx *) hapd->sta_list;
1928
1929 return fst_hostapd_get_sta(get_ctx, mb_only);
1930}
1931
1932
1933static const u8 * fst_hostapd_get_peer_next(void *ctx,
1934 struct fst_get_peer_ctx **get_ctx,
Hai Shalome21d4e82020-04-29 16:34:06 -07001935 bool mb_only)
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001936{
1937 return fst_hostapd_get_sta(get_ctx, mb_only);
1938}
1939
1940
1941void fst_hostapd_fill_iface_obj(struct hostapd_data *hapd,
1942 struct fst_wpa_obj *iface_obj)
1943{
Sunil8cd6f4d2022-06-28 18:40:46 +00001944 os_memset(iface_obj, 0, sizeof(*iface_obj));
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001945 iface_obj->ctx = hapd;
1946 iface_obj->get_bssid = fst_hostapd_get_bssid_cb;
1947 iface_obj->get_channel_info = fst_hostapd_get_channel_info_cb;
Sunil8cd6f4d2022-06-28 18:40:46 +00001948 iface_obj->get_hw_modes = fst_hostapd_get_hw_modes_cb;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001949 iface_obj->set_ies = fst_hostapd_set_ies_cb;
1950 iface_obj->send_action = fst_hostapd_send_action_cb;
1951 iface_obj->get_mb_ie = fst_hostapd_get_mb_ie_cb;
1952 iface_obj->update_mb_ie = fst_hostapd_update_mb_ie_cb;
1953 iface_obj->get_peer_first = fst_hostapd_get_peer_first;
1954 iface_obj->get_peer_next = fst_hostapd_get_peer_next;
1955}
1956
1957#endif /* CONFIG_FST */
1958
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001959#ifdef CONFIG_OWE
1960
1961static int hostapd_owe_iface_iter(struct hostapd_iface *iface, void *ctx)
1962{
1963 struct hostapd_data *hapd = ctx;
1964 size_t i;
1965
1966 for (i = 0; i < iface->num_bss; i++) {
1967 struct hostapd_data *bss = iface->bss[i];
1968
1969 if (os_strcmp(hapd->conf->owe_transition_ifname,
1970 bss->conf->iface) != 0)
1971 continue;
1972
1973 wpa_printf(MSG_DEBUG,
1974 "OWE: ifname=%s found transition mode ifname=%s BSSID "
1975 MACSTR " SSID %s",
1976 hapd->conf->iface, bss->conf->iface,
1977 MAC2STR(bss->own_addr),
1978 wpa_ssid_txt(bss->conf->ssid.ssid,
1979 bss->conf->ssid.ssid_len));
1980 if (!bss->conf->ssid.ssid_set || !bss->conf->ssid.ssid_len ||
1981 is_zero_ether_addr(bss->own_addr))
1982 continue;
1983
1984 os_memcpy(hapd->conf->owe_transition_bssid, bss->own_addr,
1985 ETH_ALEN);
1986 os_memcpy(hapd->conf->owe_transition_ssid,
1987 bss->conf->ssid.ssid, bss->conf->ssid.ssid_len);
1988 hapd->conf->owe_transition_ssid_len = bss->conf->ssid.ssid_len;
1989 wpa_printf(MSG_DEBUG,
1990 "OWE: Copied transition mode information");
1991 return 1;
1992 }
1993
1994 return 0;
1995}
1996
1997
1998int hostapd_owe_trans_get_info(struct hostapd_data *hapd)
1999{
2000 if (hapd->conf->owe_transition_ssid_len > 0 &&
2001 !is_zero_ether_addr(hapd->conf->owe_transition_bssid))
2002 return 0;
2003
2004 /* Find transition mode SSID/BSSID information from a BSS operated by
2005 * this hostapd instance. */
2006 if (!hapd->iface->interfaces ||
2007 !hapd->iface->interfaces->for_each_interface)
2008 return hostapd_owe_iface_iter(hapd->iface, hapd);
2009 else
2010 return hapd->iface->interfaces->for_each_interface(
2011 hapd->iface->interfaces, hostapd_owe_iface_iter, hapd);
2012}
2013
2014
2015static int hostapd_owe_iface_iter2(struct hostapd_iface *iface, void *ctx)
2016{
2017 size_t i;
2018
2019 for (i = 0; i < iface->num_bss; i++) {
2020 struct hostapd_data *bss = iface->bss[i];
2021 int res;
2022
2023 if (!bss->conf->owe_transition_ifname[0])
2024 continue;
Hai Shalom899fcc72020-10-19 14:38:18 -07002025 if (bss->iface->state != HAPD_IFACE_ENABLED) {
2026 wpa_printf(MSG_DEBUG,
2027 "OWE: Interface %s state %s - defer beacon update",
2028 bss->conf->iface,
2029 hostapd_state_text(bss->iface->state));
2030 continue;
2031 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002032 res = hostapd_owe_trans_get_info(bss);
2033 if (res == 0)
2034 continue;
2035 wpa_printf(MSG_DEBUG,
2036 "OWE: Matching transition mode interface enabled - update beacon data for %s",
2037 bss->conf->iface);
2038 ieee802_11_set_beacon(bss);
2039 }
2040
2041 return 0;
2042}
2043
2044#endif /* CONFIG_OWE */
2045
2046
2047static void hostapd_owe_update_trans(struct hostapd_iface *iface)
2048{
2049#ifdef CONFIG_OWE
2050 /* Check whether the enabled BSS can complete OWE transition mode
2051 * configuration for any pending interface. */
2052 if (!iface->interfaces ||
2053 !iface->interfaces->for_each_interface)
2054 hostapd_owe_iface_iter2(iface, NULL);
2055 else
2056 iface->interfaces->for_each_interface(
2057 iface->interfaces, hostapd_owe_iface_iter2, NULL);
2058#endif /* CONFIG_OWE */
2059}
2060
2061
Roshan Pius3a1667e2018-07-03 15:17:14 -07002062static void hostapd_interface_setup_failure_handler(void *eloop_ctx,
2063 void *timeout_ctx)
2064{
2065 struct hostapd_iface *iface = eloop_ctx;
2066 struct hostapd_data *hapd;
2067
2068 if (iface->num_bss < 1 || !iface->bss || !iface->bss[0])
2069 return;
2070 hapd = iface->bss[0];
2071 if (hapd->setup_complete_cb)
2072 hapd->setup_complete_cb(hapd->setup_complete_cb_ctx);
2073}
2074
2075
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002076static int hostapd_setup_interface_complete_sync(struct hostapd_iface *iface,
2077 int err)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002078{
2079 struct hostapd_data *hapd = iface->bss[0];
2080 size_t j;
2081 u8 *prev_addr;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002082 int delay_apply_cfg = 0;
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08002083 int res_dfs_offload = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002084
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002085 if (err)
2086 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002087
2088 wpa_printf(MSG_DEBUG, "Completing interface initialization");
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08002089 if (iface->freq) {
Dmitry Shmidt051af732013-10-22 13:52:46 -07002090#ifdef NEED_AP_MLME
2091 int res;
2092#endif /* NEED_AP_MLME */
2093
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002094 wpa_printf(MSG_DEBUG, "Mode: %s Channel: %d "
2095 "Frequency: %d MHz",
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002096 hostapd_hw_mode_txt(iface->conf->hw_mode),
2097 iface->conf->channel, iface->freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002098
Dmitry Shmidt051af732013-10-22 13:52:46 -07002099#ifdef NEED_AP_MLME
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07002100 /* Handle DFS only if it is not offloaded to the driver */
2101 if (!(iface->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD)) {
2102 /* Check DFS */
2103 res = hostapd_handle_dfs(iface);
2104 if (res <= 0) {
2105 if (res < 0)
2106 goto fail;
2107 return res;
2108 }
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08002109 } else {
2110 /* If DFS is offloaded to the driver */
2111 res_dfs_offload = hostapd_handle_dfs_offload(iface);
2112 if (res_dfs_offload <= 0) {
2113 if (res_dfs_offload < 0)
2114 goto fail;
2115 } else {
2116 wpa_printf(MSG_DEBUG,
2117 "Proceed with AP/channel setup");
2118 /*
2119 * If this is a DFS channel, move to completing
2120 * AP setup.
2121 */
2122 if (res_dfs_offload == 1)
2123 goto dfs_offload;
2124 /* Otherwise fall through. */
2125 }
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002126 }
Dmitry Shmidt051af732013-10-22 13:52:46 -07002127#endif /* NEED_AP_MLME */
2128
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002129#ifdef CONFIG_MESH
2130 if (iface->mconf != NULL) {
2131 wpa_printf(MSG_DEBUG,
2132 "%s: Mesh configuration will be applied while joining the mesh network",
2133 iface->bss[0]->conf->iface);
2134 delay_apply_cfg = 1;
2135 }
2136#endif /* CONFIG_MESH */
2137
2138 if (!delay_apply_cfg &&
2139 hostapd_set_freq(hapd, hapd->iconf->hw_mode, iface->freq,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002140 hapd->iconf->channel,
Hai Shalomc3565922019-10-28 11:58:20 -07002141 hapd->iconf->enable_edmg,
2142 hapd->iconf->edmg_channel,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002143 hapd->iconf->ieee80211n,
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002144 hapd->iconf->ieee80211ac,
Hai Shalom81f62d82019-07-22 12:10:00 -07002145 hapd->iconf->ieee80211ax,
Sunil Ravia04bd252022-05-02 22:54:18 -07002146 hapd->iconf->ieee80211be,
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002147 hapd->iconf->secondary_channel,
Hai Shalom81f62d82019-07-22 12:10:00 -07002148 hostapd_get_oper_chwidth(hapd->iconf),
2149 hostapd_get_oper_centr_freq_seg0_idx(
2150 hapd->iconf),
2151 hostapd_get_oper_centr_freq_seg1_idx(
2152 hapd->iconf))) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002153 wpa_printf(MSG_ERROR, "Could not set channel for "
2154 "kernel driver");
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002155 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002156 }
2157 }
2158
2159 if (iface->current_mode) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002160 if (hostapd_prepare_rates(iface, iface->current_mode)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002161 wpa_printf(MSG_ERROR, "Failed to prepare rates "
2162 "table.");
2163 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
2164 HOSTAPD_LEVEL_WARNING,
2165 "Failed to prepare rates table.");
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002166 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002167 }
2168 }
2169
Hai Shalom021b0b52019-04-10 11:17:58 -07002170 if (hapd->iconf->rts_threshold >= -1 &&
2171 hostapd_set_rts(hapd, hapd->iconf->rts_threshold) &&
2172 hapd->iconf->rts_threshold >= -1) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002173 wpa_printf(MSG_ERROR, "Could not set RTS threshold for "
2174 "kernel driver");
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002175 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002176 }
2177
Hai Shalom021b0b52019-04-10 11:17:58 -07002178 if (hapd->iconf->fragm_threshold >= -1 &&
2179 hostapd_set_frag(hapd, hapd->iconf->fragm_threshold) &&
2180 hapd->iconf->fragm_threshold != -1) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002181 wpa_printf(MSG_ERROR, "Could not set fragmentation threshold "
2182 "for kernel driver");
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002183 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002184 }
2185
2186 prev_addr = hapd->own_addr;
2187
2188 for (j = 0; j < iface->num_bss; j++) {
2189 hapd = iface->bss[j];
2190 if (j)
2191 os_memcpy(hapd->own_addr, prev_addr, ETH_ALEN);
Sunil Ravi77d572f2023-01-17 23:58:31 +00002192 if (hostapd_setup_bss(hapd, j == 0, !iface->conf->mbssid)) {
Hai Shalom021b0b52019-04-10 11:17:58 -07002193 for (;;) {
Dmitry Shmidt71757432014-06-02 13:50:35 -07002194 hapd = iface->bss[j];
2195 hostapd_bss_deinit_no_free(hapd);
2196 hostapd_free_hapd_data(hapd);
Hai Shalom021b0b52019-04-10 11:17:58 -07002197 if (j == 0)
2198 break;
2199 j--;
2200 }
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002201 goto fail;
Dmitry Shmidt71757432014-06-02 13:50:35 -07002202 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002203 if (is_zero_ether_addr(hapd->conf->bssid))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002204 prev_addr = hapd->own_addr;
2205 }
Sunil Ravi77d572f2023-01-17 23:58:31 +00002206
2207 if (hapd->iconf->mbssid) {
2208 for (j = 0; hapd->iconf->mbssid && j < iface->num_bss; j++) {
2209 hapd = iface->bss[j];
2210 if (hostapd_start_beacon(hapd, true)) {
2211 for (;;) {
2212 hapd = iface->bss[j];
2213 hostapd_bss_deinit_no_free(hapd);
2214 hostapd_free_hapd_data(hapd);
2215 if (j == 0)
2216 break;
2217 j--;
2218 }
2219 goto fail;
2220 }
2221 }
2222 }
2223
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002224 hapd = iface->bss[0];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002225
2226 hostapd_tx_queue_params(iface);
2227
2228 ap_list_init(iface);
2229
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07002230 hostapd_set_acl(hapd);
2231
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002232 if (hostapd_driver_commit(hapd) < 0) {
2233 wpa_printf(MSG_ERROR, "%s: Failed to commit driver "
2234 "configuration", __func__);
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002235 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002236 }
2237
Jouni Malinen87fd2792011-05-16 18:35:42 +03002238 /*
2239 * WPS UPnP module can be initialized only when the "upnp_iface" is up.
2240 * If "interface" and "upnp_iface" are the same (e.g., non-bridge
2241 * mode), the interface is up only after driver_commit, so initialize
2242 * WPS after driver_commit.
2243 */
2244 for (j = 0; j < iface->num_bss; j++) {
2245 if (hostapd_init_wps_complete(iface->bss[j]))
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002246 goto fail;
Jouni Malinen87fd2792011-05-16 18:35:42 +03002247 }
2248
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08002249 if ((iface->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
2250 !res_dfs_offload) {
2251 /*
2252 * If freq is DFS, and DFS is offloaded to the driver, then wait
2253 * for CAC to complete.
2254 */
2255 wpa_printf(MSG_DEBUG, "%s: Wait for CAC to complete", __func__);
2256 return res_dfs_offload;
2257 }
2258
2259#ifdef NEED_AP_MLME
2260dfs_offload:
2261#endif /* NEED_AP_MLME */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002262
2263#ifdef CONFIG_FST
2264 if (hapd->iconf->fst_cfg.group_id[0]) {
2265 struct fst_wpa_obj iface_obj;
2266
2267 fst_hostapd_fill_iface_obj(hapd, &iface_obj);
2268 iface->fst = fst_attach(hapd->conf->iface, hapd->own_addr,
2269 &iface_obj, &hapd->iconf->fst_cfg);
2270 if (!iface->fst) {
2271 wpa_printf(MSG_ERROR, "Could not attach to FST %s",
2272 hapd->iconf->fst_cfg.group_id);
2273 goto fail;
2274 }
2275 }
2276#endif /* CONFIG_FST */
2277
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002278 hostapd_set_state(iface, HAPD_IFACE_ENABLED);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002279 hostapd_owe_update_trans(iface);
Hai Shalom81f62d82019-07-22 12:10:00 -07002280 airtime_policy_update_init(iface);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002281 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_ENABLED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002282 if (hapd->setup_complete_cb)
2283 hapd->setup_complete_cb(hapd->setup_complete_cb_ctx);
2284
Hai Shalom60840252021-02-19 19:02:11 -08002285#ifdef CONFIG_MESH
2286 if (delay_apply_cfg && !iface->mconf) {
2287 wpa_printf(MSG_ERROR, "Error while completing mesh init");
2288 goto fail;
2289 }
2290#endif /* CONFIG_MESH */
2291
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002292 wpa_printf(MSG_DEBUG, "%s: Setup of interface done.",
2293 iface->bss[0]->conf->iface);
Dmitry Shmidtb96dad42013-11-05 10:07:29 -08002294 if (iface->interfaces && iface->interfaces->terminate_on_error > 0)
2295 iface->interfaces->terminate_on_error--;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002296
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002297 for (j = 0; j < iface->num_bss; j++)
Hai Shalom74f70d42019-02-11 14:42:39 -08002298 hostapd_neighbor_set_own_report(iface->bss[j]);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002299
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002300 return 0;
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002301
2302fail:
2303 wpa_printf(MSG_ERROR, "Interface initialization failed");
2304 hostapd_set_state(iface, HAPD_IFACE_DISABLED);
2305 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002306#ifdef CONFIG_FST
2307 if (iface->fst) {
2308 fst_detach(iface->fst);
2309 iface->fst = NULL;
2310 }
2311#endif /* CONFIG_FST */
Roshan Pius3a1667e2018-07-03 15:17:14 -07002312
2313 if (iface->interfaces && iface->interfaces->terminate_on_error) {
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002314 eloop_terminate();
Roshan Pius3a1667e2018-07-03 15:17:14 -07002315 } else if (hapd->setup_complete_cb) {
2316 /*
2317 * Calling hapd->setup_complete_cb directly may cause iface
2318 * deinitialization which may be accessed later by the caller.
2319 */
2320 eloop_register_timeout(0, 0,
2321 hostapd_interface_setup_failure_handler,
2322 iface, NULL);
2323 }
2324
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002325 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002326}
2327
2328
2329/**
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002330 * hostapd_setup_interface_complete - Complete interface setup
2331 *
2332 * This function is called when previous steps in the interface setup has been
2333 * completed. This can also start operations, e.g., DFS, that will require
2334 * additional processing before interface is ready to be enabled. Such
2335 * operations will call this function from eloop callbacks when finished.
2336 */
2337int hostapd_setup_interface_complete(struct hostapd_iface *iface, int err)
2338{
2339 struct hapd_interfaces *interfaces = iface->interfaces;
2340 struct hostapd_data *hapd = iface->bss[0];
2341 unsigned int i;
2342 int not_ready_in_sync_ifaces = 0;
2343
2344 if (!iface->need_to_start_in_sync)
2345 return hostapd_setup_interface_complete_sync(iface, err);
2346
2347 if (err) {
2348 wpa_printf(MSG_ERROR, "Interface initialization failed");
2349 hostapd_set_state(iface, HAPD_IFACE_DISABLED);
2350 iface->need_to_start_in_sync = 0;
2351 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
2352 if (interfaces && interfaces->terminate_on_error)
2353 eloop_terminate();
2354 return -1;
2355 }
2356
2357 if (iface->ready_to_start_in_sync) {
2358 /* Already in ready and waiting. should never happpen */
2359 return 0;
2360 }
2361
2362 for (i = 0; i < interfaces->count; i++) {
2363 if (interfaces->iface[i]->need_to_start_in_sync &&
2364 !interfaces->iface[i]->ready_to_start_in_sync)
2365 not_ready_in_sync_ifaces++;
2366 }
2367
2368 /*
2369 * Check if this is the last interface, if yes then start all the other
2370 * waiting interfaces. If not, add this interface to the waiting list.
2371 */
2372 if (not_ready_in_sync_ifaces > 1 && iface->state == HAPD_IFACE_DFS) {
2373 /*
2374 * If this interface went through CAC, do not synchronize, just
2375 * start immediately.
2376 */
2377 iface->need_to_start_in_sync = 0;
2378 wpa_printf(MSG_INFO,
2379 "%s: Finished CAC - bypass sync and start interface",
2380 iface->bss[0]->conf->iface);
2381 return hostapd_setup_interface_complete_sync(iface, err);
2382 }
2383
2384 if (not_ready_in_sync_ifaces > 1) {
2385 /* need to wait as there are other interfaces still coming up */
2386 iface->ready_to_start_in_sync = 1;
2387 wpa_printf(MSG_INFO,
2388 "%s: Interface waiting to sync with other interfaces",
2389 iface->bss[0]->conf->iface);
2390 return 0;
2391 }
2392
2393 wpa_printf(MSG_INFO,
2394 "%s: Last interface to sync - starting all interfaces",
2395 iface->bss[0]->conf->iface);
2396 iface->need_to_start_in_sync = 0;
2397 hostapd_setup_interface_complete_sync(iface, err);
2398 for (i = 0; i < interfaces->count; i++) {
2399 if (interfaces->iface[i]->need_to_start_in_sync &&
2400 interfaces->iface[i]->ready_to_start_in_sync) {
2401 hostapd_setup_interface_complete_sync(
2402 interfaces->iface[i], 0);
2403 /* Only once the interfaces are sync started */
2404 interfaces->iface[i]->need_to_start_in_sync = 0;
2405 }
2406 }
2407
2408 return 0;
2409}
2410
2411
2412/**
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002413 * hostapd_setup_interface - Setup of an interface
2414 * @iface: Pointer to interface data.
2415 * Returns: 0 on success, -1 on failure
2416 *
2417 * Initializes the driver interface, validates the configuration,
2418 * and sets driver parameters based on the configuration.
2419 * Flushes old stations, sets the channel, encryption,
2420 * beacons, and WDS links based on the configuration.
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002421 *
2422 * If interface setup requires more time, e.g., to perform HT co-ex scans, ACS,
2423 * or DFS operations, this function returns 0 before such operations have been
2424 * completed. The pending operations are registered into eloop and will be
2425 * completed from eloop callbacks. Those callbacks end up calling
2426 * hostapd_setup_interface_complete() once setup has been completed.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002427 */
2428int hostapd_setup_interface(struct hostapd_iface *iface)
2429{
2430 int ret;
2431
Hai Shaloma20dcd72022-02-04 13:43:00 -08002432 if (!iface->conf)
2433 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002434 ret = setup_interface(iface);
2435 if (ret) {
2436 wpa_printf(MSG_ERROR, "%s: Unable to setup interface.",
Hai Shaloma20dcd72022-02-04 13:43:00 -08002437 iface->conf->bss[0]->iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002438 return -1;
2439 }
2440
2441 return 0;
2442}
2443
2444
2445/**
2446 * hostapd_alloc_bss_data - Allocate and initialize per-BSS data
2447 * @hapd_iface: Pointer to interface data
2448 * @conf: Pointer to per-interface configuration
2449 * @bss: Pointer to per-BSS configuration for this BSS
2450 * Returns: Pointer to allocated BSS data
2451 *
2452 * This function is used to allocate per-BSS data structure. This data will be
2453 * freed after hostapd_cleanup() is called for it during interface
2454 * deinitialization.
2455 */
2456struct hostapd_data *
2457hostapd_alloc_bss_data(struct hostapd_iface *hapd_iface,
2458 struct hostapd_config *conf,
2459 struct hostapd_bss_config *bss)
2460{
2461 struct hostapd_data *hapd;
2462
2463 hapd = os_zalloc(sizeof(*hapd));
2464 if (hapd == NULL)
2465 return NULL;
2466
2467 hapd->new_assoc_sta_cb = hostapd_new_assoc_sta;
2468 hapd->iconf = conf;
2469 hapd->conf = bss;
2470 hapd->iface = hapd_iface;
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08002471 if (conf)
2472 hapd->driver = conf->driver;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002473 hapd->ctrl_sock = -1;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08002474 dl_list_init(&hapd->ctrl_dst);
Dmitry Shmidt7d175302016-09-06 13:11:34 -07002475 dl_list_init(&hapd->nr_db);
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08002476 hapd->dhcp_sock = -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002477#ifdef CONFIG_IEEE80211R_AP
2478 dl_list_init(&hapd->l2_queue);
2479 dl_list_init(&hapd->l2_oui_queue);
2480#endif /* CONFIG_IEEE80211R_AP */
Hai Shalom021b0b52019-04-10 11:17:58 -07002481#ifdef CONFIG_SAE
2482 dl_list_init(&hapd->sae_commit_queue);
2483#endif /* CONFIG_SAE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002484
2485 return hapd;
2486}
2487
2488
Dmitry Shmidt54605472013-11-08 11:10:19 -08002489static void hostapd_bss_deinit(struct hostapd_data *hapd)
2490{
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07002491 if (!hapd)
2492 return;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002493 wpa_printf(MSG_DEBUG, "%s: deinit bss %s", __func__,
Hai Shalom021b0b52019-04-10 11:17:58 -07002494 hapd->conf ? hapd->conf->iface : "N/A");
Dmitry Shmidt71757432014-06-02 13:50:35 -07002495 hostapd_bss_deinit_no_free(hapd);
Dmitry Shmidtf73259c2015-03-17 11:00:54 -07002496 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
Hai Shalomc3565922019-10-28 11:58:20 -07002497#ifdef CONFIG_SQLITE
2498 if (hapd->rad_attr_db) {
2499 sqlite3_close(hapd->rad_attr_db);
2500 hapd->rad_attr_db = NULL;
2501 }
2502#endif /* CONFIG_SQLITE */
Dmitry Shmidt54605472013-11-08 11:10:19 -08002503 hostapd_cleanup(hapd);
2504}
2505
2506
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002507void hostapd_interface_deinit(struct hostapd_iface *iface)
2508{
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002509 int j;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002510
Dmitry Shmidt54605472013-11-08 11:10:19 -08002511 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002512 if (iface == NULL)
2513 return;
2514
Dmitry Shmidtf73259c2015-03-17 11:00:54 -07002515 hostapd_set_state(iface, HAPD_IFACE_DISABLED);
2516
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002517 eloop_cancel_timeout(channel_list_update_timeout, iface, NULL);
2518 iface->wait_channel_update = 0;
2519
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002520#ifdef CONFIG_FST
2521 if (iface->fst) {
2522 fst_detach(iface->fst);
2523 iface->fst = NULL;
2524 }
2525#endif /* CONFIG_FST */
2526
Hai Shalom021b0b52019-04-10 11:17:58 -07002527 for (j = (int) iface->num_bss - 1; j >= 0; j--) {
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07002528 if (!iface->bss)
2529 break;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002530 hostapd_bss_deinit(iface->bss[j]);
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07002531 }
Roshan Pius3a1667e2018-07-03 15:17:14 -07002532
Roshan Pius3a1667e2018-07-03 15:17:14 -07002533#ifdef NEED_AP_MLME
2534 hostapd_stop_setup_timers(iface);
2535 eloop_cancel_timeout(ap_ht2040_timeout, iface, NULL);
2536#endif /* NEED_AP_MLME */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002537}
2538
2539
2540void hostapd_interface_free(struct hostapd_iface *iface)
2541{
2542 size_t j;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002543 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
2544 for (j = 0; j < iface->num_bss; j++) {
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07002545 if (!iface->bss)
2546 break;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002547 wpa_printf(MSG_DEBUG, "%s: free hapd %p",
2548 __func__, iface->bss[j]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002549 os_free(iface->bss[j]);
Dmitry Shmidt54605472013-11-08 11:10:19 -08002550 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002551 hostapd_cleanup_iface(iface);
2552}
2553
2554
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07002555struct hostapd_iface * hostapd_alloc_iface(void)
2556{
2557 struct hostapd_iface *hapd_iface;
2558
2559 hapd_iface = os_zalloc(sizeof(*hapd_iface));
2560 if (!hapd_iface)
2561 return NULL;
2562
2563 dl_list_init(&hapd_iface->sta_seen);
2564
2565 return hapd_iface;
2566}
2567
2568
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002569/**
2570 * hostapd_init - Allocate and initialize per-interface data
2571 * @config_file: Path to the configuration file
2572 * Returns: Pointer to the allocated interface data or %NULL on failure
2573 *
2574 * This function is used to allocate main data structures for per-interface
2575 * data. The allocated data buffer will be freed by calling
2576 * hostapd_cleanup_iface().
2577 */
2578struct hostapd_iface * hostapd_init(struct hapd_interfaces *interfaces,
2579 const char *config_file)
2580{
2581 struct hostapd_iface *hapd_iface = NULL;
2582 struct hostapd_config *conf = NULL;
2583 struct hostapd_data *hapd;
2584 size_t i;
2585
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07002586 hapd_iface = hostapd_alloc_iface();
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002587 if (hapd_iface == NULL)
2588 goto fail;
2589
2590 hapd_iface->config_fname = os_strdup(config_file);
2591 if (hapd_iface->config_fname == NULL)
2592 goto fail;
2593
2594 conf = interfaces->config_read_cb(hapd_iface->config_fname);
2595 if (conf == NULL)
2596 goto fail;
2597 hapd_iface->conf = conf;
2598
2599 hapd_iface->num_bss = conf->num_bss;
2600 hapd_iface->bss = os_calloc(conf->num_bss,
2601 sizeof(struct hostapd_data *));
2602 if (hapd_iface->bss == NULL)
2603 goto fail;
2604
2605 for (i = 0; i < conf->num_bss; i++) {
2606 hapd = hapd_iface->bss[i] =
2607 hostapd_alloc_bss_data(hapd_iface, conf,
2608 conf->bss[i]);
2609 if (hapd == NULL)
2610 goto fail;
2611 hapd->msg_ctx = hapd;
2612 }
2613
2614 return hapd_iface;
2615
2616fail:
2617 wpa_printf(MSG_ERROR, "Failed to set up interface with %s",
2618 config_file);
2619 if (conf)
2620 hostapd_config_free(conf);
2621 if (hapd_iface) {
2622 os_free(hapd_iface->config_fname);
2623 os_free(hapd_iface->bss);
Dmitry Shmidt54605472013-11-08 11:10:19 -08002624 wpa_printf(MSG_DEBUG, "%s: free iface %p",
2625 __func__, hapd_iface);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002626 os_free(hapd_iface);
2627 }
2628 return NULL;
2629}
2630
2631
2632static int ifname_in_use(struct hapd_interfaces *interfaces, const char *ifname)
2633{
2634 size_t i, j;
2635
2636 for (i = 0; i < interfaces->count; i++) {
2637 struct hostapd_iface *iface = interfaces->iface[i];
2638 for (j = 0; j < iface->num_bss; j++) {
2639 struct hostapd_data *hapd = iface->bss[j];
2640 if (os_strcmp(ifname, hapd->conf->iface) == 0)
2641 return 1;
2642 }
2643 }
2644
2645 return 0;
2646}
2647
2648
2649/**
2650 * hostapd_interface_init_bss - Read configuration file and init BSS data
2651 *
2652 * This function is used to parse configuration file for a BSS. This BSS is
2653 * added to an existing interface sharing the same radio (if any) or a new
2654 * interface is created if this is the first interface on a radio. This
2655 * allocate memory for the BSS. No actual driver operations are started.
2656 *
2657 * This is similar to hostapd_interface_init(), but for a case where the
2658 * configuration is used to add a single BSS instead of all BSSes for a radio.
2659 */
2660struct hostapd_iface *
2661hostapd_interface_init_bss(struct hapd_interfaces *interfaces, const char *phy,
2662 const char *config_fname, int debug)
2663{
2664 struct hostapd_iface *new_iface = NULL, *iface = NULL;
2665 struct hostapd_data *hapd;
2666 int k;
2667 size_t i, bss_idx;
2668
2669 if (!phy || !*phy)
2670 return NULL;
2671
2672 for (i = 0; i < interfaces->count; i++) {
2673 if (os_strcmp(interfaces->iface[i]->phy, phy) == 0) {
2674 iface = interfaces->iface[i];
2675 break;
2676 }
2677 }
2678
2679 wpa_printf(MSG_INFO, "Configuration file: %s (phy %s)%s",
2680 config_fname, phy, iface ? "" : " --> new PHY");
2681 if (iface) {
2682 struct hostapd_config *conf;
2683 struct hostapd_bss_config **tmp_conf;
2684 struct hostapd_data **tmp_bss;
2685 struct hostapd_bss_config *bss;
2686 const char *ifname;
2687
2688 /* Add new BSS to existing iface */
2689 conf = interfaces->config_read_cb(config_fname);
2690 if (conf == NULL)
2691 return NULL;
2692 if (conf->num_bss > 1) {
2693 wpa_printf(MSG_ERROR, "Multiple BSSes specified in BSS-config");
2694 hostapd_config_free(conf);
2695 return NULL;
2696 }
2697
2698 ifname = conf->bss[0]->iface;
2699 if (ifname[0] != '\0' && ifname_in_use(interfaces, ifname)) {
2700 wpa_printf(MSG_ERROR,
2701 "Interface name %s already in use", ifname);
2702 hostapd_config_free(conf);
2703 return NULL;
2704 }
2705
2706 tmp_conf = os_realloc_array(
2707 iface->conf->bss, iface->conf->num_bss + 1,
2708 sizeof(struct hostapd_bss_config *));
2709 tmp_bss = os_realloc_array(iface->bss, iface->num_bss + 1,
2710 sizeof(struct hostapd_data *));
2711 if (tmp_bss)
2712 iface->bss = tmp_bss;
2713 if (tmp_conf) {
2714 iface->conf->bss = tmp_conf;
2715 iface->conf->last_bss = tmp_conf[0];
2716 }
2717 if (tmp_bss == NULL || tmp_conf == NULL) {
2718 hostapd_config_free(conf);
2719 return NULL;
2720 }
2721 bss = iface->conf->bss[iface->conf->num_bss] = conf->bss[0];
2722 iface->conf->num_bss++;
2723
2724 hapd = hostapd_alloc_bss_data(iface, iface->conf, bss);
2725 if (hapd == NULL) {
2726 iface->conf->num_bss--;
2727 hostapd_config_free(conf);
2728 return NULL;
2729 }
2730 iface->conf->last_bss = bss;
2731 iface->bss[iface->num_bss] = hapd;
2732 hapd->msg_ctx = hapd;
2733
2734 bss_idx = iface->num_bss++;
2735 conf->num_bss--;
2736 conf->bss[0] = NULL;
2737 hostapd_config_free(conf);
2738 } else {
2739 /* Add a new iface with the first BSS */
2740 new_iface = iface = hostapd_init(interfaces, config_fname);
2741 if (!iface)
2742 return NULL;
2743 os_strlcpy(iface->phy, phy, sizeof(iface->phy));
2744 iface->interfaces = interfaces;
2745 bss_idx = 0;
2746 }
2747
2748 for (k = 0; k < debug; k++) {
2749 if (iface->bss[bss_idx]->conf->logger_stdout_level > 0)
2750 iface->bss[bss_idx]->conf->logger_stdout_level--;
2751 }
2752
2753 if (iface->conf->bss[bss_idx]->iface[0] == '\0' &&
2754 !hostapd_drv_none(iface->bss[bss_idx])) {
2755 wpa_printf(MSG_ERROR, "Interface name not specified in %s",
2756 config_fname);
2757 if (new_iface)
2758 hostapd_interface_deinit_free(new_iface);
2759 return NULL;
2760 }
2761
2762 return iface;
2763}
2764
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002765
2766void hostapd_interface_deinit_free(struct hostapd_iface *iface)
2767{
2768 const struct wpa_driver_ops *driver;
2769 void *drv_priv;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002770
2771 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002772 if (iface == NULL)
2773 return;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002774 wpa_printf(MSG_DEBUG, "%s: num_bss=%u conf->num_bss=%u",
2775 __func__, (unsigned int) iface->num_bss,
2776 (unsigned int) iface->conf->num_bss);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002777 driver = iface->bss[0]->driver;
2778 drv_priv = iface->bss[0]->drv_priv;
2779 hostapd_interface_deinit(iface);
Dmitry Shmidt54605472013-11-08 11:10:19 -08002780 wpa_printf(MSG_DEBUG, "%s: driver=%p drv_priv=%p -> hapd_deinit",
2781 __func__, driver, drv_priv);
Dmitry Shmidt71757432014-06-02 13:50:35 -07002782 if (driver && driver->hapd_deinit && drv_priv) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002783 driver->hapd_deinit(drv_priv);
Dmitry Shmidt71757432014-06-02 13:50:35 -07002784 iface->bss[0]->drv_priv = NULL;
2785 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002786 hostapd_interface_free(iface);
2787}
2788
2789
Dmitry Shmidt15907092014-03-25 10:42:57 -07002790static void hostapd_deinit_driver(const struct wpa_driver_ops *driver,
2791 void *drv_priv,
2792 struct hostapd_iface *hapd_iface)
2793{
2794 size_t j;
2795
2796 wpa_printf(MSG_DEBUG, "%s: driver=%p drv_priv=%p -> hapd_deinit",
2797 __func__, driver, drv_priv);
2798 if (driver && driver->hapd_deinit && drv_priv) {
2799 driver->hapd_deinit(drv_priv);
2800 for (j = 0; j < hapd_iface->num_bss; j++) {
2801 wpa_printf(MSG_DEBUG, "%s:bss[%d]->drv_priv=%p",
2802 __func__, (int) j,
2803 hapd_iface->bss[j]->drv_priv);
Hai Shalom1dc4d202019-04-29 16:22:27 -07002804 if (hapd_iface->bss[j]->drv_priv == drv_priv) {
Dmitry Shmidt15907092014-03-25 10:42:57 -07002805 hapd_iface->bss[j]->drv_priv = NULL;
Hai Shalom1dc4d202019-04-29 16:22:27 -07002806 hapd_iface->extended_capa = NULL;
2807 hapd_iface->extended_capa_mask = NULL;
2808 hapd_iface->extended_capa_len = 0;
2809 }
Dmitry Shmidt15907092014-03-25 10:42:57 -07002810 }
2811 }
2812}
2813
2814
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002815int hostapd_enable_iface(struct hostapd_iface *hapd_iface)
2816{
Dmitry Shmidt71757432014-06-02 13:50:35 -07002817 size_t j;
2818
Hai Shalom60840252021-02-19 19:02:11 -08002819 if (!hapd_iface)
2820 return -1;
2821
2822 if (hapd_iface->enable_iface_cb)
2823 return hapd_iface->enable_iface_cb(hapd_iface);
2824
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002825 if (hapd_iface->bss[0]->drv_priv != NULL) {
2826 wpa_printf(MSG_ERROR, "Interface %s already enabled",
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002827 hapd_iface->conf->bss[0]->iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002828 return -1;
2829 }
2830
2831 wpa_printf(MSG_DEBUG, "Enable interface %s",
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002832 hapd_iface->conf->bss[0]->iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002833
Dmitry Shmidt71757432014-06-02 13:50:35 -07002834 for (j = 0; j < hapd_iface->num_bss; j++)
2835 hostapd_set_security_params(hapd_iface->conf->bss[j], 1);
Dmitry Shmidt344abd32014-01-14 13:17:00 -08002836 if (hostapd_config_check(hapd_iface->conf, 1) < 0) {
2837 wpa_printf(MSG_INFO, "Invalid configuration - cannot enable");
2838 return -1;
2839 }
2840
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002841 if (hapd_iface->interfaces == NULL ||
2842 hapd_iface->interfaces->driver_init == NULL ||
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002843 hapd_iface->interfaces->driver_init(hapd_iface))
2844 return -1;
2845
2846 if (hostapd_setup_interface(hapd_iface)) {
Dmitry Shmidt15907092014-03-25 10:42:57 -07002847 hostapd_deinit_driver(hapd_iface->bss[0]->driver,
2848 hapd_iface->bss[0]->drv_priv,
2849 hapd_iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002850 return -1;
2851 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002852
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002853 return 0;
2854}
2855
2856
2857int hostapd_reload_iface(struct hostapd_iface *hapd_iface)
2858{
2859 size_t j;
2860
2861 wpa_printf(MSG_DEBUG, "Reload interface %s",
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002862 hapd_iface->conf->bss[0]->iface);
2863 for (j = 0; j < hapd_iface->num_bss; j++)
Dmitry Shmidt71757432014-06-02 13:50:35 -07002864 hostapd_set_security_params(hapd_iface->conf->bss[j], 1);
Dmitry Shmidt344abd32014-01-14 13:17:00 -08002865 if (hostapd_config_check(hapd_iface->conf, 1) < 0) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002866 wpa_printf(MSG_ERROR, "Updated configuration is invalid");
2867 return -1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002868 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002869 hostapd_clear_old(hapd_iface);
2870 for (j = 0; j < hapd_iface->num_bss; j++)
2871 hostapd_reload_bss(hapd_iface->bss[j]);
2872
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002873 return 0;
2874}
2875
2876
Sunil Ravi77d572f2023-01-17 23:58:31 +00002877int hostapd_reload_bss_only(struct hostapd_data *bss)
2878{
2879
2880 wpa_printf(MSG_DEBUG, "Reload BSS %s", bss->conf->iface);
2881 hostapd_set_security_params(bss->conf, 1);
2882 if (hostapd_config_check(bss->iconf, 1) < 0) {
2883 wpa_printf(MSG_ERROR, "Updated BSS configuration is invalid");
2884 return -1;
2885 }
2886 hostapd_clear_old_bss(bss);
2887 hostapd_reload_bss(bss);
2888 return 0;
2889}
2890
2891
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002892int hostapd_disable_iface(struct hostapd_iface *hapd_iface)
2893{
2894 size_t j;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002895 const struct wpa_driver_ops *driver;
2896 void *drv_priv;
2897
2898 if (hapd_iface == NULL)
2899 return -1;
Dmitry Shmidt71757432014-06-02 13:50:35 -07002900
Hai Shalom60840252021-02-19 19:02:11 -08002901 if (hapd_iface->disable_iface_cb)
2902 return hapd_iface->disable_iface_cb(hapd_iface);
2903
Dmitry Shmidt71757432014-06-02 13:50:35 -07002904 if (hapd_iface->bss[0]->drv_priv == NULL) {
2905 wpa_printf(MSG_INFO, "Interface %s already disabled",
2906 hapd_iface->conf->bss[0]->iface);
2907 return -1;
2908 }
2909
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002910 wpa_msg(hapd_iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002911 driver = hapd_iface->bss[0]->driver;
2912 drv_priv = hapd_iface->bss[0]->drv_priv;
2913
Dmitry Shmidta38abf92014-03-06 13:38:44 -08002914 hapd_iface->driver_ap_teardown =
2915 !!(hapd_iface->drv_flags &
2916 WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
2917
Hai Shalom39ba6fc2019-01-22 12:40:38 -08002918#ifdef NEED_AP_MLME
2919 for (j = 0; j < hapd_iface->num_bss; j++)
2920 hostapd_cleanup_cs_params(hapd_iface->bss[j]);
2921#endif /* NEED_AP_MLME */
2922
Dmitry Shmidta38abf92014-03-06 13:38:44 -08002923 /* same as hostapd_interface_deinit without deinitializing ctrl-iface */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002924 for (j = 0; j < hapd_iface->num_bss; j++) {
2925 struct hostapd_data *hapd = hapd_iface->bss[j];
Dmitry Shmidt71757432014-06-02 13:50:35 -07002926 hostapd_bss_deinit_no_free(hapd);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002927 hostapd_free_hapd_data(hapd);
2928 }
2929
Dmitry Shmidt15907092014-03-25 10:42:57 -07002930 hostapd_deinit_driver(driver, drv_priv, hapd_iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002931
2932 /* From hostapd_cleanup_iface: These were initialized in
2933 * hostapd_setup_interface and hostapd_setup_interface_complete
2934 */
2935 hostapd_cleanup_iface_partial(hapd_iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002936
Dmitry Shmidt56052862013-10-04 10:23:25 -07002937 wpa_printf(MSG_DEBUG, "Interface %s disabled",
2938 hapd_iface->bss[0]->conf->iface);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002939 hostapd_set_state(hapd_iface, HAPD_IFACE_DISABLED);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002940 return 0;
2941}
2942
2943
2944static struct hostapd_iface *
2945hostapd_iface_alloc(struct hapd_interfaces *interfaces)
2946{
2947 struct hostapd_iface **iface, *hapd_iface;
2948
2949 iface = os_realloc_array(interfaces->iface, interfaces->count + 1,
2950 sizeof(struct hostapd_iface *));
2951 if (iface == NULL)
2952 return NULL;
2953 interfaces->iface = iface;
2954 hapd_iface = interfaces->iface[interfaces->count] =
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07002955 hostapd_alloc_iface();
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002956 if (hapd_iface == NULL) {
2957 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory for "
2958 "the interface", __func__);
2959 return NULL;
2960 }
2961 interfaces->count++;
2962 hapd_iface->interfaces = interfaces;
2963
2964 return hapd_iface;
2965}
2966
2967
2968static struct hostapd_config *
2969hostapd_config_alloc(struct hapd_interfaces *interfaces, const char *ifname,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002970 const char *ctrl_iface, const char *driver)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002971{
2972 struct hostapd_bss_config *bss;
2973 struct hostapd_config *conf;
2974
2975 /* Allocates memory for bss and conf */
2976 conf = hostapd_config_defaults();
2977 if (conf == NULL) {
2978 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory for "
2979 "configuration", __func__);
Hai Shalom74f70d42019-02-11 14:42:39 -08002980 return NULL;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002981 }
2982
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002983 if (driver) {
2984 int j;
2985
2986 for (j = 0; wpa_drivers[j]; j++) {
2987 if (os_strcmp(driver, wpa_drivers[j]->name) == 0) {
2988 conf->driver = wpa_drivers[j];
2989 goto skip;
2990 }
2991 }
2992
2993 wpa_printf(MSG_ERROR,
2994 "Invalid/unknown driver '%s' - registering the default driver",
2995 driver);
2996 }
2997
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002998 conf->driver = wpa_drivers[0];
2999 if (conf->driver == NULL) {
3000 wpa_printf(MSG_ERROR, "No driver wrappers registered!");
3001 hostapd_config_free(conf);
3002 return NULL;
3003 }
3004
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003005skip:
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003006 bss = conf->last_bss = conf->bss[0];
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003007
3008 os_strlcpy(bss->iface, ifname, sizeof(bss->iface));
3009 bss->ctrl_interface = os_strdup(ctrl_iface);
3010 if (bss->ctrl_interface == NULL) {
3011 hostapd_config_free(conf);
3012 return NULL;
3013 }
3014
3015 /* Reading configuration file skipped, will be done in SET!
3016 * From reading the configuration till the end has to be done in
3017 * SET
3018 */
3019 return conf;
3020}
3021
3022
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003023static int hostapd_data_alloc(struct hostapd_iface *hapd_iface,
3024 struct hostapd_config *conf)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003025{
3026 size_t i;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003027 struct hostapd_data *hapd;
3028
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003029 hapd_iface->bss = os_calloc(conf->num_bss,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003030 sizeof(struct hostapd_data *));
3031 if (hapd_iface->bss == NULL)
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003032 return -1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003033
3034 for (i = 0; i < conf->num_bss; i++) {
3035 hapd = hapd_iface->bss[i] =
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003036 hostapd_alloc_bss_data(hapd_iface, conf, conf->bss[i]);
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003037 if (hapd == NULL) {
3038 while (i > 0) {
3039 i--;
3040 os_free(hapd_iface->bss[i]);
3041 hapd_iface->bss[i] = NULL;
3042 }
3043 os_free(hapd_iface->bss);
3044 hapd_iface->bss = NULL;
3045 return -1;
3046 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003047 hapd->msg_ctx = hapd;
3048 }
3049
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003050 hapd_iface->conf = conf;
3051 hapd_iface->num_bss = conf->num_bss;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003052
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003053 return 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003054}
3055
3056
3057int hostapd_add_iface(struct hapd_interfaces *interfaces, char *buf)
3058{
3059 struct hostapd_config *conf = NULL;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003060 struct hostapd_iface *hapd_iface = NULL, *new_iface = NULL;
3061 struct hostapd_data *hapd;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003062 char *ptr;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003063 size_t i, j;
3064 const char *conf_file = NULL, *phy_name = NULL;
3065
3066 if (os_strncmp(buf, "bss_config=", 11) == 0) {
3067 char *pos;
3068 phy_name = buf + 11;
3069 pos = os_strchr(phy_name, ':');
3070 if (!pos)
3071 return -1;
3072 *pos++ = '\0';
3073 conf_file = pos;
3074 if (!os_strlen(conf_file))
3075 return -1;
3076
3077 hapd_iface = hostapd_interface_init_bss(interfaces, phy_name,
3078 conf_file, 0);
3079 if (!hapd_iface)
3080 return -1;
3081 for (j = 0; j < interfaces->count; j++) {
3082 if (interfaces->iface[j] == hapd_iface)
3083 break;
3084 }
3085 if (j == interfaces->count) {
3086 struct hostapd_iface **tmp;
3087 tmp = os_realloc_array(interfaces->iface,
3088 interfaces->count + 1,
3089 sizeof(struct hostapd_iface *));
3090 if (!tmp) {
3091 hostapd_interface_deinit_free(hapd_iface);
3092 return -1;
3093 }
3094 interfaces->iface = tmp;
3095 interfaces->iface[interfaces->count++] = hapd_iface;
3096 new_iface = hapd_iface;
3097 }
3098
3099 if (new_iface) {
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003100 if (interfaces->driver_init(hapd_iface))
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003101 goto fail;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003102
3103 if (hostapd_setup_interface(hapd_iface)) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003104 hostapd_deinit_driver(
3105 hapd_iface->bss[0]->driver,
3106 hapd_iface->bss[0]->drv_priv,
3107 hapd_iface);
3108 goto fail;
3109 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003110 } else {
3111 /* Assign new BSS with bss[0]'s driver info */
3112 hapd = hapd_iface->bss[hapd_iface->num_bss - 1];
3113 hapd->driver = hapd_iface->bss[0]->driver;
3114 hapd->drv_priv = hapd_iface->bss[0]->drv_priv;
3115 os_memcpy(hapd->own_addr, hapd_iface->bss[0]->own_addr,
3116 ETH_ALEN);
3117
3118 if (start_ctrl_iface_bss(hapd) < 0 ||
Dmitry Shmidt54605472013-11-08 11:10:19 -08003119 (hapd_iface->state == HAPD_IFACE_ENABLED &&
Sunil Ravi77d572f2023-01-17 23:58:31 +00003120 hostapd_setup_bss(hapd, -1, true))) {
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07003121 hostapd_cleanup(hapd);
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08003122 hapd_iface->bss[hapd_iface->num_bss - 1] = NULL;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003123 hapd_iface->conf->num_bss--;
3124 hapd_iface->num_bss--;
Dmitry Shmidt54605472013-11-08 11:10:19 -08003125 wpa_printf(MSG_DEBUG, "%s: free hapd %p %s",
3126 __func__, hapd, hapd->conf->iface);
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003127 hostapd_config_free_bss(hapd->conf);
3128 hapd->conf = NULL;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003129 os_free(hapd);
3130 return -1;
3131 }
3132 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003133 hostapd_owe_update_trans(hapd_iface);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003134 return 0;
3135 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003136
3137 ptr = os_strchr(buf, ' ');
3138 if (ptr == NULL)
3139 return -1;
3140 *ptr++ = '\0';
3141
Dmitry Shmidt56052862013-10-04 10:23:25 -07003142 if (os_strncmp(ptr, "config=", 7) == 0)
3143 conf_file = ptr + 7;
3144
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003145 for (i = 0; i < interfaces->count; i++) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003146 if (!os_strcmp(interfaces->iface[i]->conf->bss[0]->iface,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003147 buf)) {
3148 wpa_printf(MSG_INFO, "Cannot add interface - it "
3149 "already exists");
3150 return -1;
3151 }
3152 }
3153
3154 hapd_iface = hostapd_iface_alloc(interfaces);
3155 if (hapd_iface == NULL) {
3156 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
3157 "for interface", __func__);
3158 goto fail;
3159 }
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003160 new_iface = hapd_iface;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003161
Dmitry Shmidt56052862013-10-04 10:23:25 -07003162 if (conf_file && interfaces->config_read_cb) {
3163 conf = interfaces->config_read_cb(conf_file);
3164 if (conf && conf->bss)
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003165 os_strlcpy(conf->bss[0]->iface, buf,
3166 sizeof(conf->bss[0]->iface));
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003167 } else {
3168 char *driver = os_strchr(ptr, ' ');
3169
3170 if (driver)
3171 *driver++ = '\0';
3172 conf = hostapd_config_alloc(interfaces, buf, ptr, driver);
3173 }
3174
Dmitry Shmidt56052862013-10-04 10:23:25 -07003175 if (conf == NULL || conf->bss == NULL) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003176 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
3177 "for configuration", __func__);
3178 goto fail;
3179 }
3180
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003181 if (hostapd_data_alloc(hapd_iface, conf) < 0) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003182 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
3183 "for hostapd", __func__);
3184 goto fail;
3185 }
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003186 conf = NULL;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003187
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003188 if (start_ctrl_iface(hapd_iface) < 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003189 goto fail;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003190
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003191 wpa_printf(MSG_INFO, "Add interface '%s'",
3192 hapd_iface->conf->bss[0]->iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003193
3194 return 0;
3195
3196fail:
3197 if (conf)
3198 hostapd_config_free(conf);
3199 if (hapd_iface) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003200 if (hapd_iface->bss) {
Dmitry Shmidt54605472013-11-08 11:10:19 -08003201 for (i = 0; i < hapd_iface->num_bss; i++) {
3202 hapd = hapd_iface->bss[i];
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08003203 if (!hapd)
3204 continue;
3205 if (hapd_iface->interfaces &&
Dmitry Shmidt54605472013-11-08 11:10:19 -08003206 hapd_iface->interfaces->ctrl_iface_deinit)
3207 hapd_iface->interfaces->
3208 ctrl_iface_deinit(hapd);
3209 wpa_printf(MSG_DEBUG, "%s: free hapd %p (%s)",
3210 __func__, hapd_iface->bss[i],
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08003211 hapd->conf->iface);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003212 hostapd_cleanup(hapd);
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08003213 os_free(hapd);
3214 hapd_iface->bss[i] = NULL;
Dmitry Shmidt54605472013-11-08 11:10:19 -08003215 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003216 os_free(hapd_iface->bss);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003217 hapd_iface->bss = NULL;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003218 }
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003219 if (new_iface) {
3220 interfaces->count--;
3221 interfaces->iface[interfaces->count] = NULL;
3222 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003223 hostapd_cleanup_iface(hapd_iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003224 }
3225 return -1;
3226}
3227
3228
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003229static int hostapd_remove_bss(struct hostapd_iface *iface, unsigned int idx)
3230{
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003231 size_t i;
3232
Dmitry Shmidt54605472013-11-08 11:10:19 -08003233 wpa_printf(MSG_INFO, "Remove BSS '%s'", iface->conf->bss[idx]->iface);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003234
Dmitry Shmidt54605472013-11-08 11:10:19 -08003235 /* Remove hostapd_data only if it has already been initialized */
3236 if (idx < iface->num_bss) {
3237 struct hostapd_data *hapd = iface->bss[idx];
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003238
Dmitry Shmidt54605472013-11-08 11:10:19 -08003239 hostapd_bss_deinit(hapd);
3240 wpa_printf(MSG_DEBUG, "%s: free hapd %p (%s)",
3241 __func__, hapd, hapd->conf->iface);
3242 hostapd_config_free_bss(hapd->conf);
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003243 hapd->conf = NULL;
Dmitry Shmidt54605472013-11-08 11:10:19 -08003244 os_free(hapd);
3245
3246 iface->num_bss--;
3247
3248 for (i = idx; i < iface->num_bss; i++)
3249 iface->bss[i] = iface->bss[i + 1];
3250 } else {
3251 hostapd_config_free_bss(iface->conf->bss[idx]);
3252 iface->conf->bss[idx] = NULL;
3253 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003254
3255 iface->conf->num_bss--;
Dmitry Shmidt54605472013-11-08 11:10:19 -08003256 for (i = idx; i < iface->conf->num_bss; i++)
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003257 iface->conf->bss[i] = iface->conf->bss[i + 1];
3258
3259 return 0;
3260}
3261
3262
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003263int hostapd_remove_iface(struct hapd_interfaces *interfaces, char *buf)
3264{
3265 struct hostapd_iface *hapd_iface;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003266 size_t i, j, k = 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003267
3268 for (i = 0; i < interfaces->count; i++) {
3269 hapd_iface = interfaces->iface[i];
3270 if (hapd_iface == NULL)
3271 return -1;
Dmitry Shmidt54605472013-11-08 11:10:19 -08003272 if (!os_strcmp(hapd_iface->conf->bss[0]->iface, buf)) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003273 wpa_printf(MSG_INFO, "Remove interface '%s'", buf);
Dmitry Shmidta38abf92014-03-06 13:38:44 -08003274 hapd_iface->driver_ap_teardown =
3275 !!(hapd_iface->drv_flags &
3276 WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
3277
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003278 hostapd_interface_deinit_free(hapd_iface);
3279 k = i;
3280 while (k < (interfaces->count - 1)) {
3281 interfaces->iface[k] =
3282 interfaces->iface[k + 1];
3283 k++;
3284 }
3285 interfaces->count--;
3286 return 0;
3287 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003288
3289 for (j = 0; j < hapd_iface->conf->num_bss; j++) {
Dmitry Shmidta38abf92014-03-06 13:38:44 -08003290 if (!os_strcmp(hapd_iface->conf->bss[j]->iface, buf)) {
3291 hapd_iface->driver_ap_teardown =
3292 !(hapd_iface->drv_flags &
3293 WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003294 return hostapd_remove_bss(hapd_iface, j);
Dmitry Shmidta38abf92014-03-06 13:38:44 -08003295 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003296 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003297 }
3298 return -1;
3299}
3300
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003301
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003302/**
3303 * hostapd_new_assoc_sta - Notify that a new station associated with the AP
3304 * @hapd: Pointer to BSS data
3305 * @sta: Pointer to the associated STA data
3306 * @reassoc: 1 to indicate this was a re-association; 0 = first association
3307 *
3308 * This function will be called whenever a station associates with the AP. It
3309 * can be called from ieee802_11.c for drivers that export MLME to hostapd and
3310 * from drv_callbacks.c based on driver events for drivers that take care of
3311 * management frames (IEEE 802.11 authentication and association) internally.
3312 */
3313void hostapd_new_assoc_sta(struct hostapd_data *hapd, struct sta_info *sta,
3314 int reassoc)
3315{
3316 if (hapd->tkip_countermeasures) {
3317 hostapd_drv_sta_deauth(hapd, sta->addr,
3318 WLAN_REASON_MICHAEL_MIC_FAILURE);
3319 return;
3320 }
3321
3322 hostapd_prune_associations(hapd, sta->addr);
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -08003323 ap_sta_clear_disconnect_timeouts(hapd, sta);
Hai Shalom899fcc72020-10-19 14:38:18 -07003324 sta->post_csa_sa_query = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003325
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003326#ifdef CONFIG_P2P
3327 if (sta->p2p_ie == NULL && !sta->no_p2p_set) {
3328 sta->no_p2p_set = 1;
3329 hapd->num_sta_no_p2p++;
3330 if (hapd->num_sta_no_p2p == 1)
3331 hostapd_p2p_non_p2p_sta_connected(hapd);
3332 }
3333#endif /* CONFIG_P2P */
3334
Hai Shalom81f62d82019-07-22 12:10:00 -07003335 airtime_policy_new_sta(hapd, sta);
3336
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003337 /* Start accounting here, if IEEE 802.1X and WPA are not used.
3338 * IEEE 802.1X/WPA code will start accounting after the station has
3339 * been authorized. */
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -08003340 if (!hapd->conf->ieee802_1x && !hapd->conf->wpa && !hapd->conf->osen) {
3341 ap_sta_set_authorized(hapd, sta, 1);
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08003342 os_get_reltime(&sta->connected_time);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003343 accounting_sta_start(hapd, sta);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003344 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003345
3346 /* Start IEEE 802.1X authentication process for new stations */
3347 ieee802_1x_new_station(hapd, sta);
3348 if (reassoc) {
3349 if (sta->auth_alg != WLAN_AUTH_FT &&
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003350 sta->auth_alg != WLAN_AUTH_FILS_SK &&
3351 sta->auth_alg != WLAN_AUTH_FILS_SK_PFS &&
3352 sta->auth_alg != WLAN_AUTH_FILS_PK &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003353 !(sta->flags & (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS)))
3354 wpa_auth_sm_event(sta->wpa_sm, WPA_REAUTH);
3355 } else
3356 wpa_auth_sta_associated(hapd->wpa_auth, sta->wpa_sm);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003357
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08003358 if (hapd->iface->drv_flags & WPA_DRIVER_FLAGS_WIRED) {
3359 if (eloop_cancel_timeout(ap_handle_timer, hapd, sta) > 0) {
3360 wpa_printf(MSG_DEBUG,
3361 "%s: %s: canceled wired ap_handle_timer timeout for "
3362 MACSTR,
3363 hapd->conf->iface, __func__,
3364 MAC2STR(sta->addr));
3365 }
3366 } else if (!(hapd->iface->drv_flags &
3367 WPA_DRIVER_FLAGS_INACTIVITY_TIMER)) {
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -08003368 wpa_printf(MSG_DEBUG,
3369 "%s: %s: reschedule ap_handle_timer timeout for "
3370 MACSTR " (%d seconds - ap_max_inactivity)",
3371 hapd->conf->iface, __func__, MAC2STR(sta->addr),
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08003372 hapd->conf->ap_max_inactivity);
3373 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
3374 eloop_register_timeout(hapd->conf->ap_max_inactivity, 0,
3375 ap_handle_timer, hapd, sta);
3376 }
Hai Shalom81f62d82019-07-22 12:10:00 -07003377
3378#ifdef CONFIG_MACSEC
3379 if (hapd->conf->wpa_key_mgmt == WPA_KEY_MGMT_NONE &&
3380 hapd->conf->mka_psk_set)
3381 ieee802_1x_create_preshared_mka_hapd(hapd, sta);
3382 else
3383 ieee802_1x_alloc_kay_sm_hapd(hapd, sta);
3384#endif /* CONFIG_MACSEC */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003385}
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003386
3387
3388const char * hostapd_state_text(enum hostapd_iface_state s)
3389{
3390 switch (s) {
3391 case HAPD_IFACE_UNINITIALIZED:
3392 return "UNINITIALIZED";
3393 case HAPD_IFACE_DISABLED:
3394 return "DISABLED";
3395 case HAPD_IFACE_COUNTRY_UPDATE:
3396 return "COUNTRY_UPDATE";
3397 case HAPD_IFACE_ACS:
3398 return "ACS";
3399 case HAPD_IFACE_HT_SCAN:
3400 return "HT_SCAN";
3401 case HAPD_IFACE_DFS:
3402 return "DFS";
3403 case HAPD_IFACE_ENABLED:
3404 return "ENABLED";
3405 }
3406
3407 return "UNKNOWN";
3408}
3409
3410
3411void hostapd_set_state(struct hostapd_iface *iface, enum hostapd_iface_state s)
3412{
3413 wpa_printf(MSG_INFO, "%s: interface state %s->%s",
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07003414 iface->conf ? iface->conf->bss[0]->iface : "N/A",
3415 hostapd_state_text(iface->state), hostapd_state_text(s));
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003416 iface->state = s;
3417}
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003418
3419
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003420int hostapd_csa_in_progress(struct hostapd_iface *iface)
3421{
3422 unsigned int i;
3423
3424 for (i = 0; i < iface->num_bss; i++)
3425 if (iface->bss[i]->csa_in_progress)
3426 return 1;
3427 return 0;
3428}
3429
3430
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003431#ifdef NEED_AP_MLME
3432
3433static void free_beacon_data(struct beacon_data *beacon)
3434{
3435 os_free(beacon->head);
3436 beacon->head = NULL;
3437 os_free(beacon->tail);
3438 beacon->tail = NULL;
3439 os_free(beacon->probe_resp);
3440 beacon->probe_resp = NULL;
3441 os_free(beacon->beacon_ies);
3442 beacon->beacon_ies = NULL;
3443 os_free(beacon->proberesp_ies);
3444 beacon->proberesp_ies = NULL;
3445 os_free(beacon->assocresp_ies);
3446 beacon->assocresp_ies = NULL;
3447}
3448
3449
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003450static int hostapd_build_beacon_data(struct hostapd_data *hapd,
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003451 struct beacon_data *beacon)
3452{
3453 struct wpabuf *beacon_extra, *proberesp_extra, *assocresp_extra;
3454 struct wpa_driver_ap_params params;
3455 int ret;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003456
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08003457 os_memset(beacon, 0, sizeof(*beacon));
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003458 ret = ieee802_11_build_ap_params(hapd, &params);
3459 if (ret < 0)
3460 return ret;
3461
3462 ret = hostapd_build_ap_extra_ies(hapd, &beacon_extra,
3463 &proberesp_extra,
3464 &assocresp_extra);
3465 if (ret)
3466 goto free_ap_params;
3467
3468 ret = -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003469 beacon->head = os_memdup(params.head, params.head_len);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003470 if (!beacon->head)
3471 goto free_ap_extra_ies;
3472
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003473 beacon->head_len = params.head_len;
3474
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003475 beacon->tail = os_memdup(params.tail, params.tail_len);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003476 if (!beacon->tail)
3477 goto free_beacon;
3478
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003479 beacon->tail_len = params.tail_len;
3480
3481 if (params.proberesp != NULL) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003482 beacon->probe_resp = os_memdup(params.proberesp,
3483 params.proberesp_len);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003484 if (!beacon->probe_resp)
3485 goto free_beacon;
3486
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003487 beacon->probe_resp_len = params.proberesp_len;
3488 }
3489
3490 /* copy the extra ies */
3491 if (beacon_extra) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003492 beacon->beacon_ies = os_memdup(beacon_extra->buf,
3493 wpabuf_len(beacon_extra));
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003494 if (!beacon->beacon_ies)
3495 goto free_beacon;
3496
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003497 beacon->beacon_ies_len = wpabuf_len(beacon_extra);
3498 }
3499
3500 if (proberesp_extra) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003501 beacon->proberesp_ies = os_memdup(proberesp_extra->buf,
3502 wpabuf_len(proberesp_extra));
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003503 if (!beacon->proberesp_ies)
3504 goto free_beacon;
3505
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003506 beacon->proberesp_ies_len = wpabuf_len(proberesp_extra);
3507 }
3508
3509 if (assocresp_extra) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003510 beacon->assocresp_ies = os_memdup(assocresp_extra->buf,
3511 wpabuf_len(assocresp_extra));
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003512 if (!beacon->assocresp_ies)
3513 goto free_beacon;
3514
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003515 beacon->assocresp_ies_len = wpabuf_len(assocresp_extra);
3516 }
3517
3518 ret = 0;
3519free_beacon:
3520 /* if the function fails, the caller should not free beacon data */
3521 if (ret)
3522 free_beacon_data(beacon);
3523
3524free_ap_extra_ies:
3525 hostapd_free_ap_extra_ies(hapd, beacon_extra, proberesp_extra,
3526 assocresp_extra);
3527free_ap_params:
3528 ieee802_11_free_ap_params(&params);
3529 return ret;
3530}
3531
3532
3533/*
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003534 * TODO: This flow currently supports only changing channel and width within
3535 * the same hw_mode. Any other changes to MAC parameters or provided settings
3536 * are not supported.
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003537 */
3538static int hostapd_change_config_freq(struct hostapd_data *hapd,
3539 struct hostapd_config *conf,
3540 struct hostapd_freq_params *params,
3541 struct hostapd_freq_params *old_params)
3542{
3543 int channel;
Hai Shalom81f62d82019-07-22 12:10:00 -07003544 u8 seg0, seg1;
3545 struct hostapd_hw_modes *mode;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003546
3547 if (!params->channel) {
3548 /* check if the new channel is supported by hw */
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003549 params->channel = hostapd_hw_get_channel(hapd, params->freq);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003550 }
3551
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003552 channel = params->channel;
3553 if (!channel)
3554 return -1;
3555
Hai Shalom81f62d82019-07-22 12:10:00 -07003556 mode = hapd->iface->current_mode;
3557
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003558 /* if a pointer to old_params is provided we save previous state */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003559 if (old_params &&
3560 hostapd_set_freq_params(old_params, conf->hw_mode,
3561 hostapd_hw_get_freq(hapd, conf->channel),
Hai Shalomc3565922019-10-28 11:58:20 -07003562 conf->channel, conf->enable_edmg,
3563 conf->edmg_channel, conf->ieee80211n,
Hai Shalom81f62d82019-07-22 12:10:00 -07003564 conf->ieee80211ac, conf->ieee80211ax,
Sunil Ravia04bd252022-05-02 22:54:18 -07003565 conf->ieee80211be, conf->secondary_channel,
Hai Shalom81f62d82019-07-22 12:10:00 -07003566 hostapd_get_oper_chwidth(conf),
3567 hostapd_get_oper_centr_freq_seg0_idx(conf),
3568 hostapd_get_oper_centr_freq_seg1_idx(conf),
3569 conf->vht_capab,
3570 mode ? &mode->he_capab[IEEE80211_MODE_AP] :
Sunil Ravia04bd252022-05-02 22:54:18 -07003571 NULL,
3572 mode ? &mode->eht_capab[IEEE80211_MODE_AP] :
Hai Shalom81f62d82019-07-22 12:10:00 -07003573 NULL))
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003574 return -1;
3575
3576 switch (params->bandwidth) {
3577 case 0:
3578 case 20:
Hai Shaloma20dcd72022-02-04 13:43:00 -08003579 conf->ht_capab &= ~HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET;
3580 break;
3581 case 40:
3582 case 80:
3583 case 160:
3584 conf->ht_capab |= HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET;
3585 break;
3586 default:
3587 return -1;
3588 }
3589
3590 switch (params->bandwidth) {
3591 case 0:
3592 case 20:
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003593 case 40:
Sunil8cd6f4d2022-06-28 18:40:46 +00003594 hostapd_set_oper_chwidth(conf, CONF_OPER_CHWIDTH_USE_HT);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003595 break;
3596 case 80:
3597 if (params->center_freq2)
Sunil8cd6f4d2022-06-28 18:40:46 +00003598 hostapd_set_oper_chwidth(conf,
3599 CONF_OPER_CHWIDTH_80P80MHZ);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003600 else
Sunil8cd6f4d2022-06-28 18:40:46 +00003601 hostapd_set_oper_chwidth(conf,
3602 CONF_OPER_CHWIDTH_80MHZ);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003603 break;
3604 case 160:
Sunil8cd6f4d2022-06-28 18:40:46 +00003605 hostapd_set_oper_chwidth(conf, CONF_OPER_CHWIDTH_160MHZ);
3606 break;
3607 case 320:
3608 hostapd_set_oper_chwidth(conf, CONF_OPER_CHWIDTH_320MHZ);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003609 break;
3610 default:
3611 return -1;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003612 }
3613
3614 conf->channel = channel;
3615 conf->ieee80211n = params->ht_enabled;
Hai Shaloma20dcd72022-02-04 13:43:00 -08003616 conf->ieee80211ac = params->vht_enabled;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003617 conf->secondary_channel = params->sec_channel_offset;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003618 ieee80211_freq_to_chan(params->center_freq1,
Hai Shalom81f62d82019-07-22 12:10:00 -07003619 &seg0);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003620 ieee80211_freq_to_chan(params->center_freq2,
Hai Shalom81f62d82019-07-22 12:10:00 -07003621 &seg1);
3622 hostapd_set_oper_centr_freq_seg0_idx(conf, seg0);
3623 hostapd_set_oper_centr_freq_seg1_idx(conf, seg1);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003624
3625 /* TODO: maybe call here hostapd_config_check here? */
3626
3627 return 0;
3628}
3629
3630
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003631static int hostapd_fill_csa_settings(struct hostapd_data *hapd,
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003632 struct csa_settings *settings)
3633{
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003634 struct hostapd_iface *iface = hapd->iface;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003635 struct hostapd_freq_params old_freq;
3636 int ret;
Hai Shalom81f62d82019-07-22 12:10:00 -07003637 u8 chan, bandwidth;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003638
3639 os_memset(&old_freq, 0, sizeof(old_freq));
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003640 if (!iface || !iface->freq || hapd->csa_in_progress)
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003641 return -1;
3642
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003643 switch (settings->freq_params.bandwidth) {
3644 case 80:
3645 if (settings->freq_params.center_freq2)
Sunil8cd6f4d2022-06-28 18:40:46 +00003646 bandwidth = CONF_OPER_CHWIDTH_80P80MHZ;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003647 else
Sunil8cd6f4d2022-06-28 18:40:46 +00003648 bandwidth = CONF_OPER_CHWIDTH_80MHZ;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003649 break;
3650 case 160:
Sunil8cd6f4d2022-06-28 18:40:46 +00003651 bandwidth = CONF_OPER_CHWIDTH_160MHZ;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003652 break;
3653 default:
Sunil8cd6f4d2022-06-28 18:40:46 +00003654 bandwidth = CONF_OPER_CHWIDTH_USE_HT;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003655 break;
3656 }
3657
3658 if (ieee80211_freq_to_channel_ext(
3659 settings->freq_params.freq,
3660 settings->freq_params.sec_channel_offset,
Hai Shalom81f62d82019-07-22 12:10:00 -07003661 bandwidth,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003662 &hapd->iface->cs_oper_class,
3663 &chan) == NUM_HOSTAPD_MODES) {
3664 wpa_printf(MSG_DEBUG,
Sunil Ravia04bd252022-05-02 22:54:18 -07003665 "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 -08003666 settings->freq_params.freq,
3667 settings->freq_params.sec_channel_offset,
Hai Shalom81f62d82019-07-22 12:10:00 -07003668 settings->freq_params.vht_enabled,
Sunil Ravia04bd252022-05-02 22:54:18 -07003669 settings->freq_params.he_enabled,
3670 settings->freq_params.eht_enabled);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003671 return -1;
3672 }
3673
3674 settings->freq_params.channel = chan;
3675
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003676 ret = hostapd_change_config_freq(iface->bss[0], iface->conf,
3677 &settings->freq_params,
3678 &old_freq);
3679 if (ret)
3680 return ret;
3681
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003682 ret = hostapd_build_beacon_data(hapd, &settings->beacon_after);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003683
3684 /* change back the configuration */
3685 hostapd_change_config_freq(iface->bss[0], iface->conf,
3686 &old_freq, NULL);
3687
3688 if (ret)
3689 return ret;
3690
3691 /* set channel switch parameters for csa ie */
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003692 hapd->cs_freq_params = settings->freq_params;
3693 hapd->cs_count = settings->cs_count;
3694 hapd->cs_block_tx = settings->block_tx;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003695
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003696 ret = hostapd_build_beacon_data(hapd, &settings->beacon_csa);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003697 if (ret) {
3698 free_beacon_data(&settings->beacon_after);
3699 return ret;
3700 }
3701
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003702 settings->counter_offset_beacon[0] = hapd->cs_c_off_beacon;
3703 settings->counter_offset_presp[0] = hapd->cs_c_off_proberesp;
3704 settings->counter_offset_beacon[1] = hapd->cs_c_off_ecsa_beacon;
3705 settings->counter_offset_presp[1] = hapd->cs_c_off_ecsa_proberesp;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003706
3707 return 0;
3708}
3709
3710
3711void hostapd_cleanup_cs_params(struct hostapd_data *hapd)
3712{
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003713 os_memset(&hapd->cs_freq_params, 0, sizeof(hapd->cs_freq_params));
3714 hapd->cs_count = 0;
3715 hapd->cs_block_tx = 0;
3716 hapd->cs_c_off_beacon = 0;
3717 hapd->cs_c_off_proberesp = 0;
3718 hapd->csa_in_progress = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003719 hapd->cs_c_off_ecsa_beacon = 0;
3720 hapd->cs_c_off_ecsa_proberesp = 0;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003721}
3722
3723
Hai Shalom60840252021-02-19 19:02:11 -08003724void hostapd_chan_switch_config(struct hostapd_data *hapd,
3725 struct hostapd_freq_params *freq_params)
Roshan Pius3a1667e2018-07-03 15:17:14 -07003726{
Sunil Ravia04bd252022-05-02 22:54:18 -07003727 if (freq_params->eht_enabled)
3728 hapd->iconf->ch_switch_eht_config |= CH_SWITCH_EHT_ENABLED;
3729 else
3730 hapd->iconf->ch_switch_eht_config |= CH_SWITCH_EHT_DISABLED;
3731
Hai Shalom60840252021-02-19 19:02:11 -08003732 if (freq_params->he_enabled)
3733 hapd->iconf->ch_switch_he_config |= CH_SWITCH_HE_ENABLED;
3734 else
3735 hapd->iconf->ch_switch_he_config |= CH_SWITCH_HE_DISABLED;
3736
3737 if (freq_params->vht_enabled)
Roshan Pius3a1667e2018-07-03 15:17:14 -07003738 hapd->iconf->ch_switch_vht_config |= CH_SWITCH_VHT_ENABLED;
3739 else
3740 hapd->iconf->ch_switch_vht_config |= CH_SWITCH_VHT_DISABLED;
3741
3742 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
Hai Shalom60840252021-02-19 19:02:11 -08003743 HOSTAPD_LEVEL_INFO,
Sunil Ravia04bd252022-05-02 22:54:18 -07003744 "CHAN_SWITCH EHT config 0x%x HE config 0x%x VHT config 0x%x",
3745 hapd->iconf->ch_switch_eht_config,
Hai Shalom60840252021-02-19 19:02:11 -08003746 hapd->iconf->ch_switch_he_config,
Roshan Pius3a1667e2018-07-03 15:17:14 -07003747 hapd->iconf->ch_switch_vht_config);
3748}
3749
3750
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003751int hostapd_switch_channel(struct hostapd_data *hapd,
3752 struct csa_settings *settings)
3753{
3754 int ret;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003755
3756 if (!(hapd->iface->drv_flags & WPA_DRIVER_FLAGS_AP_CSA)) {
3757 wpa_printf(MSG_INFO, "CSA is not supported");
3758 return -1;
3759 }
3760
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003761 ret = hostapd_fill_csa_settings(hapd, settings);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003762 if (ret)
3763 return ret;
3764
3765 ret = hostapd_drv_switch_channel(hapd, settings);
3766 free_beacon_data(&settings->beacon_csa);
3767 free_beacon_data(&settings->beacon_after);
3768
3769 if (ret) {
3770 /* if we failed, clean cs parameters */
3771 hostapd_cleanup_cs_params(hapd);
3772 return ret;
3773 }
3774
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003775 hapd->csa_in_progress = 1;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003776 return 0;
3777}
3778
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003779
3780void
3781hostapd_switch_channel_fallback(struct hostapd_iface *iface,
3782 const struct hostapd_freq_params *freq_params)
3783{
Sunil8cd6f4d2022-06-28 18:40:46 +00003784 int seg0_idx = 0, seg1_idx = 0;
3785 enum oper_chan_width bw = CONF_OPER_CHWIDTH_USE_HT;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003786
3787 wpa_printf(MSG_DEBUG, "Restarting all CSA-related BSSes");
3788
3789 if (freq_params->center_freq1)
Hai Shalom81f62d82019-07-22 12:10:00 -07003790 seg0_idx = 36 + (freq_params->center_freq1 - 5180) / 5;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003791 if (freq_params->center_freq2)
Hai Shalom81f62d82019-07-22 12:10:00 -07003792 seg1_idx = 36 + (freq_params->center_freq2 - 5180) / 5;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003793
3794 switch (freq_params->bandwidth) {
3795 case 0:
3796 case 20:
3797 case 40:
Sunil8cd6f4d2022-06-28 18:40:46 +00003798 bw = CONF_OPER_CHWIDTH_USE_HT;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003799 break;
3800 case 80:
3801 if (freq_params->center_freq2)
Sunil8cd6f4d2022-06-28 18:40:46 +00003802 bw = CONF_OPER_CHWIDTH_80P80MHZ;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003803 else
Sunil8cd6f4d2022-06-28 18:40:46 +00003804 bw = CONF_OPER_CHWIDTH_80MHZ;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003805 break;
3806 case 160:
Sunil8cd6f4d2022-06-28 18:40:46 +00003807 bw = CONF_OPER_CHWIDTH_160MHZ;
3808 break;
3809 case 320:
3810 bw = CONF_OPER_CHWIDTH_320MHZ;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003811 break;
3812 default:
3813 wpa_printf(MSG_WARNING, "Unknown CSA bandwidth: %d",
3814 freq_params->bandwidth);
3815 break;
3816 }
3817
3818 iface->freq = freq_params->freq;
3819 iface->conf->channel = freq_params->channel;
3820 iface->conf->secondary_channel = freq_params->sec_channel_offset;
Hai Shalom81f62d82019-07-22 12:10:00 -07003821 hostapd_set_oper_centr_freq_seg0_idx(iface->conf, seg0_idx);
3822 hostapd_set_oper_centr_freq_seg1_idx(iface->conf, seg1_idx);
3823 hostapd_set_oper_chwidth(iface->conf, bw);
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003824 iface->conf->ieee80211n = freq_params->ht_enabled;
3825 iface->conf->ieee80211ac = freq_params->vht_enabled;
Hai Shalom81f62d82019-07-22 12:10:00 -07003826 iface->conf->ieee80211ax = freq_params->he_enabled;
Sunil Ravia04bd252022-05-02 22:54:18 -07003827 iface->conf->ieee80211be = freq_params->eht_enabled;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003828
3829 /*
3830 * cs_params must not be cleared earlier because the freq_params
3831 * argument may actually point to one of these.
Hai Shalom39ba6fc2019-01-22 12:40:38 -08003832 * These params will be cleared during interface disable below.
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003833 */
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003834 hostapd_disable_iface(iface);
3835 hostapd_enable_iface(iface);
3836}
3837
Sunil Ravia04bd252022-05-02 22:54:18 -07003838
3839#ifdef CONFIG_IEEE80211AX
3840
3841void hostapd_cleanup_cca_params(struct hostapd_data *hapd)
3842{
3843 hapd->cca_count = 0;
3844 hapd->cca_color = 0;
3845 hapd->cca_c_off_beacon = 0;
3846 hapd->cca_c_off_proberesp = 0;
3847 hapd->cca_in_progress = false;
3848}
3849
3850
3851static int hostapd_fill_cca_settings(struct hostapd_data *hapd,
3852 struct cca_settings *settings)
3853{
3854 struct hostapd_iface *iface = hapd->iface;
3855 u8 old_color;
3856 int ret;
3857
3858 if (!iface || iface->conf->he_op.he_bss_color_disabled)
3859 return -1;
3860
3861 old_color = iface->conf->he_op.he_bss_color;
3862 iface->conf->he_op.he_bss_color = hapd->cca_color;
3863 ret = hostapd_build_beacon_data(hapd, &settings->beacon_after);
3864 if (ret)
3865 return ret;
3866
3867 iface->conf->he_op.he_bss_color = old_color;
3868
3869 settings->cca_count = hapd->cca_count;
3870 settings->cca_color = hapd->cca_color,
3871 hapd->cca_in_progress = true;
3872
3873 ret = hostapd_build_beacon_data(hapd, &settings->beacon_cca);
3874 if (ret) {
3875 free_beacon_data(&settings->beacon_after);
3876 return ret;
3877 }
3878
3879 settings->counter_offset_beacon = hapd->cca_c_off_beacon;
3880 settings->counter_offset_presp = hapd->cca_c_off_proberesp;
3881
3882 return 0;
3883}
3884
3885
3886static void hostapd_switch_color_timeout_handler(void *eloop_data,
3887 void *user_ctx)
3888{
3889 struct hostapd_data *hapd = (struct hostapd_data *) eloop_data;
3890 os_time_t delta_t;
3891 unsigned int b;
3892 int i, r;
3893
3894 /* CCA can be triggered once the handler constantly receives
3895 * color collision events to for at least
3896 * DOT11BSS_COLOR_COLLISION_AP_PERIOD (50 s by default). */
3897 delta_t = hapd->last_color_collision.sec -
3898 hapd->first_color_collision.sec;
3899 if (delta_t < DOT11BSS_COLOR_COLLISION_AP_PERIOD)
3900 return;
3901
3902 r = os_random() % HE_OPERATION_BSS_COLOR_MAX;
3903 for (i = 0; i < HE_OPERATION_BSS_COLOR_MAX; i++) {
Sunil8cd6f4d2022-06-28 18:40:46 +00003904 if (r && !(hapd->color_collision_bitmap & (1ULL << r)))
Sunil Ravia04bd252022-05-02 22:54:18 -07003905 break;
3906
3907 r = (r + 1) % HE_OPERATION_BSS_COLOR_MAX;
3908 }
3909
3910 if (i == HE_OPERATION_BSS_COLOR_MAX) {
3911 /* There are no free colors so turn BSS coloring off */
3912 wpa_printf(MSG_INFO,
3913 "No free colors left, turning off BSS coloring");
3914 hapd->iface->conf->he_op.he_bss_color_disabled = 1;
3915 hapd->iface->conf->he_op.he_bss_color = os_random() % 63 + 1;
3916 for (b = 0; b < hapd->iface->num_bss; b++)
3917 ieee802_11_set_beacon(hapd->iface->bss[b]);
3918 return;
3919 }
3920
3921 for (b = 0; b < hapd->iface->num_bss; b++) {
3922 struct hostapd_data *bss = hapd->iface->bss[b];
3923 struct cca_settings settings;
3924 int ret;
3925
3926 hostapd_cleanup_cca_params(bss);
3927 bss->cca_color = r;
3928 bss->cca_count = 10;
3929
3930 if (hostapd_fill_cca_settings(bss, &settings)) {
3931 hostapd_cleanup_cca_params(bss);
3932 continue;
3933 }
3934
3935 ret = hostapd_drv_switch_color(bss, &settings);
3936 if (ret)
3937 hostapd_cleanup_cca_params(bss);
3938
3939 free_beacon_data(&settings.beacon_cca);
3940 free_beacon_data(&settings.beacon_after);
3941 }
3942}
3943
3944
3945void hostapd_switch_color(struct hostapd_data *hapd, u64 bitmap)
3946{
3947 struct os_reltime now;
3948
3949 if (hapd->cca_in_progress)
3950 return;
3951
3952 if (os_get_reltime(&now))
3953 return;
3954
3955 hapd->color_collision_bitmap = bitmap;
3956 hapd->last_color_collision = now;
3957
3958 if (eloop_is_timeout_registered(hostapd_switch_color_timeout_handler,
3959 hapd, NULL))
3960 return;
3961
3962 hapd->first_color_collision = now;
3963 /* 10 s window as margin for persistent color collision reporting */
3964 eloop_register_timeout(DOT11BSS_COLOR_COLLISION_AP_PERIOD + 10, 0,
3965 hostapd_switch_color_timeout_handler,
3966 hapd, NULL);
3967}
3968
3969#endif /* CONFIG_IEEE80211AX */
3970
Dmitry Shmidte4663042016-04-04 10:07:49 -07003971#endif /* NEED_AP_MLME */
3972
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003973
3974struct hostapd_data * hostapd_get_iface(struct hapd_interfaces *interfaces,
3975 const char *ifname)
3976{
3977 size_t i, j;
3978
3979 for (i = 0; i < interfaces->count; i++) {
3980 struct hostapd_iface *iface = interfaces->iface[i];
3981
3982 for (j = 0; j < iface->num_bss; j++) {
3983 struct hostapd_data *hapd = iface->bss[j];
3984
3985 if (os_strcmp(ifname, hapd->conf->iface) == 0)
3986 return hapd;
3987 }
3988 }
3989
3990 return NULL;
3991}
3992
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003993
3994void hostapd_periodic_iface(struct hostapd_iface *iface)
3995{
3996 size_t i;
3997
3998 ap_list_timer(iface);
3999
4000 for (i = 0; i < iface->num_bss; i++) {
4001 struct hostapd_data *hapd = iface->bss[i];
4002
4003 if (!hapd->started)
4004 continue;
4005
4006#ifndef CONFIG_NO_RADIUS
4007 hostapd_acl_expire(hapd);
4008#endif /* CONFIG_NO_RADIUS */
4009 }
4010}
Hai Shalom899fcc72020-10-19 14:38:18 -07004011
4012
4013#ifdef CONFIG_OCV
4014void hostapd_ocv_check_csa_sa_query(void *eloop_ctx, void *timeout_ctx)
4015{
4016 struct hostapd_data *hapd = eloop_ctx;
4017 struct sta_info *sta;
4018
4019 wpa_printf(MSG_DEBUG, "OCV: Post-CSA SA Query initiation check");
4020
4021 for (sta = hapd->sta_list; sta; sta = sta->next) {
4022 if (!sta->post_csa_sa_query)
4023 continue;
4024
4025 wpa_printf(MSG_DEBUG, "OCV: OCVC STA " MACSTR
4026 " did not start SA Query after CSA - disconnect",
4027 MAC2STR(sta->addr));
4028 ap_sta_disconnect(hapd, sta, sta->addr,
4029 WLAN_REASON_PREV_AUTH_NOT_VALID);
4030 }
4031}
4032#endif /* CONFIG_OCV */