blob: 8b3fb404d33c4a3e324b96c2f2d0b123319af6bd [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
Sunil Ravi036cec52023-03-29 11:35:17 -07001438 if (conf->bridge[0]) {
1439 /* Set explicitly configured bridge parameters that might have
1440 * been lost if the interface has been removed out of the
1441 * bridge. */
1442
1443 /* multicast to unicast on bridge ports */
1444 if (conf->bridge_multicast_to_unicast)
1445 hostapd_drv_br_port_set_attr(
1446 hapd, DRV_BR_PORT_ATTR_MCAST2UCAST, 1);
1447
1448 /* hairpin mode */
1449 if (conf->bridge_hairpin)
1450 hostapd_drv_br_port_set_attr(
1451 hapd, DRV_BR_PORT_ATTR_HAIRPIN_MODE, 1);
1452 }
1453
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001454 if (conf->proxy_arp) {
1455 if (x_snoop_init(hapd)) {
1456 wpa_printf(MSG_ERROR,
1457 "Generic snooping infrastructure initialization failed");
1458 return -1;
1459 }
1460
1461 if (dhcp_snoop_init(hapd)) {
1462 wpa_printf(MSG_ERROR,
1463 "DHCP snooping initialization failed");
1464 return -1;
1465 }
1466
1467 if (ndisc_snoop_init(hapd)) {
1468 wpa_printf(MSG_ERROR,
1469 "Neighbor Discovery snooping initialization failed");
1470 return -1;
1471 }
1472 }
1473
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001474 if (!hostapd_drv_none(hapd) && vlan_init(hapd)) {
1475 wpa_printf(MSG_ERROR, "VLAN initialization failed.");
1476 return -1;
1477 }
1478
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001479 if (hapd->wpa_auth && wpa_init_keys(hapd->wpa_auth) < 0)
1480 return -1;
1481
Sunil Ravi77d572f2023-01-17 23:58:31 +00001482 if (start_beacon)
1483 return hostapd_start_beacon(hapd, flush_old_stations);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001484
1485 return 0;
1486}
1487
1488
1489static void hostapd_tx_queue_params(struct hostapd_iface *iface)
1490{
1491 struct hostapd_data *hapd = iface->bss[0];
1492 int i;
1493 struct hostapd_tx_queue_params *p;
1494
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001495#ifdef CONFIG_MESH
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001496 if ((hapd->conf->mesh & MESH_ENABLED) && iface->mconf == NULL)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001497 return;
1498#endif /* CONFIG_MESH */
1499
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001500 for (i = 0; i < NUM_TX_QUEUES; i++) {
1501 p = &iface->conf->tx_queue[i];
1502
1503 if (hostapd_set_tx_queue_params(hapd, i, p->aifs, p->cwmin,
1504 p->cwmax, p->burst)) {
1505 wpa_printf(MSG_DEBUG, "Failed to set TX queue "
1506 "parameters for queue %d.", i);
1507 /* Continue anyway */
1508 }
1509 }
1510}
1511
1512
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001513static int hostapd_set_acl_list(struct hostapd_data *hapd,
1514 struct mac_acl_entry *mac_acl,
1515 int n_entries, u8 accept_acl)
1516{
1517 struct hostapd_acl_params *acl_params;
1518 int i, err;
1519
1520 acl_params = os_zalloc(sizeof(*acl_params) +
1521 (n_entries * sizeof(acl_params->mac_acl[0])));
1522 if (!acl_params)
1523 return -ENOMEM;
1524
1525 for (i = 0; i < n_entries; i++)
1526 os_memcpy(acl_params->mac_acl[i].addr, mac_acl[i].addr,
1527 ETH_ALEN);
1528
1529 acl_params->acl_policy = accept_acl;
1530 acl_params->num_mac_acl = n_entries;
1531
1532 err = hostapd_drv_set_acl(hapd, acl_params);
1533
1534 os_free(acl_params);
1535
1536 return err;
1537}
1538
1539
Sunil Ravia04bd252022-05-02 22:54:18 -07001540int hostapd_set_acl(struct hostapd_data *hapd)
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001541{
1542 struct hostapd_config *conf = hapd->iconf;
Sunil Ravia04bd252022-05-02 22:54:18 -07001543 int err = 0;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001544 u8 accept_acl;
1545
1546 if (hapd->iface->drv_max_acl_mac_addrs == 0)
Sunil Ravia04bd252022-05-02 22:54:18 -07001547 return 0;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001548
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001549 if (conf->bss[0]->macaddr_acl == DENY_UNLESS_ACCEPTED) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001550 accept_acl = 1;
1551 err = hostapd_set_acl_list(hapd, conf->bss[0]->accept_mac,
1552 conf->bss[0]->num_accept_mac,
1553 accept_acl);
1554 if (err) {
1555 wpa_printf(MSG_DEBUG, "Failed to set accept acl");
Sunil Ravia04bd252022-05-02 22:54:18 -07001556 return -1;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001557 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001558 } else if (conf->bss[0]->macaddr_acl == ACCEPT_UNLESS_DENIED) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001559 accept_acl = 0;
1560 err = hostapd_set_acl_list(hapd, conf->bss[0]->deny_mac,
1561 conf->bss[0]->num_deny_mac,
1562 accept_acl);
1563 if (err) {
1564 wpa_printf(MSG_DEBUG, "Failed to set deny acl");
Sunil Ravia04bd252022-05-02 22:54:18 -07001565 return -1;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001566 }
1567 }
Sunil Ravia04bd252022-05-02 22:54:18 -07001568 return err;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001569}
1570
1571
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001572static int start_ctrl_iface_bss(struct hostapd_data *hapd)
1573{
1574 if (!hapd->iface->interfaces ||
1575 !hapd->iface->interfaces->ctrl_iface_init)
1576 return 0;
1577
1578 if (hapd->iface->interfaces->ctrl_iface_init(hapd)) {
1579 wpa_printf(MSG_ERROR,
1580 "Failed to setup control interface for %s",
1581 hapd->conf->iface);
1582 return -1;
1583 }
1584
1585 return 0;
1586}
1587
1588
1589static int start_ctrl_iface(struct hostapd_iface *iface)
1590{
1591 size_t i;
1592
1593 if (!iface->interfaces || !iface->interfaces->ctrl_iface_init)
1594 return 0;
1595
1596 for (i = 0; i < iface->num_bss; i++) {
1597 struct hostapd_data *hapd = iface->bss[i];
1598 if (iface->interfaces->ctrl_iface_init(hapd)) {
1599 wpa_printf(MSG_ERROR,
1600 "Failed to setup control interface for %s",
1601 hapd->conf->iface);
1602 return -1;
1603 }
1604 }
1605
1606 return 0;
1607}
1608
1609
1610static void channel_list_update_timeout(void *eloop_ctx, void *timeout_ctx)
1611{
1612 struct hostapd_iface *iface = eloop_ctx;
1613
1614 if (!iface->wait_channel_update) {
1615 wpa_printf(MSG_INFO, "Channel list update timeout, but interface was not waiting for it");
1616 return;
1617 }
1618
1619 /*
1620 * It is possible that the existing channel list is acceptable, so try
1621 * to proceed.
1622 */
1623 wpa_printf(MSG_DEBUG, "Channel list update timeout - try to continue anyway");
1624 setup_interface2(iface);
1625}
1626
1627
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001628void hostapd_channel_list_updated(struct hostapd_iface *iface, int initiator)
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001629{
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001630 if (!iface->wait_channel_update || initiator != REGDOM_SET_BY_USER)
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001631 return;
1632
1633 wpa_printf(MSG_DEBUG, "Channel list updated - continue setup");
1634 eloop_cancel_timeout(channel_list_update_timeout, iface, NULL);
1635 setup_interface2(iface);
1636}
1637
1638
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001639static int setup_interface(struct hostapd_iface *iface)
1640{
1641 struct hostapd_data *hapd = iface->bss[0];
1642 size_t i;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001643
Dmitry Shmidta38abf92014-03-06 13:38:44 -08001644 /*
1645 * It is possible that setup_interface() is called after the interface
1646 * was disabled etc., in which case driver_ap_teardown is possibly set
1647 * to 1. Clear it here so any other key/station deletion, which is not
1648 * part of a teardown flow, would also call the relevant driver
1649 * callbacks.
1650 */
1651 iface->driver_ap_teardown = 0;
1652
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001653 if (!iface->phy[0]) {
1654 const char *phy = hostapd_drv_get_radio_name(hapd);
1655 if (phy) {
1656 wpa_printf(MSG_DEBUG, "phy: %s", phy);
1657 os_strlcpy(iface->phy, phy, sizeof(iface->phy));
1658 }
1659 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001660
1661 /*
1662 * Make sure that all BSSes get configured with a pointer to the same
1663 * driver interface.
1664 */
1665 for (i = 1; i < iface->num_bss; i++) {
1666 iface->bss[i]->driver = hapd->driver;
1667 iface->bss[i]->drv_priv = hapd->drv_priv;
1668 }
1669
1670 if (hostapd_validate_bssid_configuration(iface))
1671 return -1;
1672
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001673 /*
1674 * Initialize control interfaces early to allow external monitoring of
1675 * channel setup operations that may take considerable amount of time
1676 * especially for DFS cases.
1677 */
1678 if (start_ctrl_iface(iface))
1679 return -1;
1680
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001681 if (hapd->iconf->country[0] && hapd->iconf->country[1]) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001682 char country[4], previous_country[4];
1683
1684 hostapd_set_state(iface, HAPD_IFACE_COUNTRY_UPDATE);
1685 if (hostapd_get_country(hapd, previous_country) < 0)
1686 previous_country[0] = '\0';
1687
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001688 os_memcpy(country, hapd->iconf->country, 3);
1689 country[3] = '\0';
1690 if (hostapd_set_country(hapd, country) < 0) {
1691 wpa_printf(MSG_ERROR, "Failed to set country code");
1692 return -1;
1693 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001694
1695 wpa_printf(MSG_DEBUG, "Previous country code %s, new country code %s",
1696 previous_country, country);
1697
1698 if (os_strncmp(previous_country, country, 2) != 0) {
1699 wpa_printf(MSG_DEBUG, "Continue interface setup after channel list update");
1700 iface->wait_channel_update = 1;
Dmitry Shmidt97672262014-02-03 13:02:54 -08001701 eloop_register_timeout(5, 0,
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001702 channel_list_update_timeout,
1703 iface, NULL);
1704 return 0;
1705 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001706 }
1707
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001708 return setup_interface2(iface);
1709}
1710
1711
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001712static int configured_fixed_chan_to_freq(struct hostapd_iface *iface)
1713{
1714 int freq, i, j;
1715
1716 if (!iface->conf->channel)
1717 return 0;
1718 if (iface->conf->op_class) {
1719 freq = ieee80211_chan_to_freq(NULL, iface->conf->op_class,
1720 iface->conf->channel);
1721 if (freq < 0) {
1722 wpa_printf(MSG_INFO,
1723 "Could not convert op_class %u channel %u to operating frequency",
1724 iface->conf->op_class, iface->conf->channel);
1725 return -1;
1726 }
1727 iface->freq = freq;
1728 return 0;
1729 }
1730
1731 /* Old configurations using only 2.4/5/60 GHz bands may not specify the
1732 * op_class parameter. Select a matching channel from the configured
1733 * mode using the channel parameter for these cases.
1734 */
1735 for (j = 0; j < iface->num_hw_features; j++) {
1736 struct hostapd_hw_modes *mode = &iface->hw_features[j];
1737
1738 if (iface->conf->hw_mode != HOSTAPD_MODE_IEEE80211ANY &&
1739 iface->conf->hw_mode != mode->mode)
1740 continue;
1741 for (i = 0; i < mode->num_channels; i++) {
1742 struct hostapd_channel_data *chan = &mode->channels[i];
1743
1744 if (chan->chan == iface->conf->channel &&
1745 !is_6ghz_freq(chan->freq)) {
1746 iface->freq = chan->freq;
1747 return 0;
1748 }
1749 }
1750 }
1751
1752 wpa_printf(MSG_INFO, "Could not determine operating frequency");
1753 return -1;
1754}
1755
1756
Hai Shaloma20dcd72022-02-04 13:43:00 -08001757static void hostapd_set_6ghz_sec_chan(struct hostapd_iface *iface)
1758{
1759 int bw, seg0;
1760
1761 if (!is_6ghz_op_class(iface->conf->op_class))
1762 return;
1763
1764 seg0 = hostapd_get_oper_centr_freq_seg0_idx(iface->conf);
1765 bw = center_idx_to_bw_6ghz(seg0);
1766 /* Assign the secondary channel if absent in config for
1767 * bandwidths > 20 MHz */
Sunil Ravi036cec52023-03-29 11:35:17 -07001768 if (bw > 0 && !iface->conf->secondary_channel) {
Hai Shaloma20dcd72022-02-04 13:43:00 -08001769 if (((iface->conf->channel - 1) / 4) % 2)
1770 iface->conf->secondary_channel = -1;
1771 else
1772 iface->conf->secondary_channel = 1;
1773 }
1774}
1775
1776
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001777static int setup_interface2(struct hostapd_iface *iface)
1778{
1779 iface->wait_channel_update = 0;
1780
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001781 if (hostapd_get_hw_features(iface)) {
1782 /* Not all drivers support this yet, so continue without hw
1783 * feature data. */
1784 } else {
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001785 int ret;
1786
Sunil Ravi036cec52023-03-29 11:35:17 -07001787 if (iface->conf->acs) {
1788 iface->freq = 0;
1789 iface->conf->channel = 0;
1790 }
1791
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001792 ret = configured_fixed_chan_to_freq(iface);
1793 if (ret < 0)
1794 goto fail;
1795
1796 if (iface->conf->op_class) {
Sunil8cd6f4d2022-06-28 18:40:46 +00001797 enum oper_chan_width ch_width;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001798
1799 ch_width = op_class_to_ch_width(iface->conf->op_class);
1800 hostapd_set_oper_chwidth(iface->conf, ch_width);
Hai Shaloma20dcd72022-02-04 13:43:00 -08001801 hostapd_set_6ghz_sec_chan(iface);
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001802 }
1803
1804 ret = hostapd_select_hw_mode(iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001805 if (ret < 0) {
1806 wpa_printf(MSG_ERROR, "Could not select hw_mode and "
1807 "channel. (%d)", ret);
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001808 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001809 }
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001810 if (ret == 1) {
1811 wpa_printf(MSG_DEBUG, "Interface initialization will be completed in a callback (ACS)");
1812 return 0;
1813 }
Hai Shalomc3565922019-10-28 11:58:20 -07001814 ret = hostapd_check_edmg_capab(iface);
1815 if (ret < 0)
1816 goto fail;
Hai Shalom60840252021-02-19 19:02:11 -08001817 ret = hostapd_check_he_6ghz_capab(iface);
1818 if (ret < 0)
1819 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001820 ret = hostapd_check_ht_capab(iface);
1821 if (ret < 0)
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001822 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001823 if (ret == 1) {
1824 wpa_printf(MSG_DEBUG, "Interface initialization will "
1825 "be completed in a callback");
1826 return 0;
1827 }
Dmitry Shmidt051af732013-10-22 13:52:46 -07001828
1829 if (iface->conf->ieee80211h)
1830 wpa_printf(MSG_DEBUG, "DFS support is enabled");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001831 }
1832 return hostapd_setup_interface_complete(iface, 0);
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001833
1834fail:
1835 hostapd_set_state(iface, HAPD_IFACE_DISABLED);
1836 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
1837 if (iface->interfaces && iface->interfaces->terminate_on_error)
1838 eloop_terminate();
1839 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001840}
1841
1842
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001843#ifdef CONFIG_FST
1844
1845static const u8 * fst_hostapd_get_bssid_cb(void *ctx)
1846{
1847 struct hostapd_data *hapd = ctx;
1848
1849 return hapd->own_addr;
1850}
1851
1852
1853static void fst_hostapd_get_channel_info_cb(void *ctx,
1854 enum hostapd_hw_mode *hw_mode,
1855 u8 *channel)
1856{
1857 struct hostapd_data *hapd = ctx;
1858
1859 *hw_mode = ieee80211_freq_to_chan(hapd->iface->freq, channel);
1860}
1861
1862
Sunil8cd6f4d2022-06-28 18:40:46 +00001863static int fst_hostapd_get_hw_modes_cb(void *ctx,
1864 struct hostapd_hw_modes **modes)
1865{
1866 struct hostapd_data *hapd = ctx;
1867
1868 *modes = hapd->iface->hw_features;
1869 return hapd->iface->num_hw_features;
1870}
1871
1872
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001873static void fst_hostapd_set_ies_cb(void *ctx, const struct wpabuf *fst_ies)
1874{
1875 struct hostapd_data *hapd = ctx;
1876
1877 if (hapd->iface->fst_ies != fst_ies) {
1878 hapd->iface->fst_ies = fst_ies;
1879 if (ieee802_11_set_beacon(hapd))
1880 wpa_printf(MSG_WARNING, "FST: Cannot set beacon");
1881 }
1882}
1883
1884
1885static int fst_hostapd_send_action_cb(void *ctx, const u8 *da,
1886 struct wpabuf *buf)
1887{
1888 struct hostapd_data *hapd = ctx;
1889
1890 return hostapd_drv_send_action(hapd, hapd->iface->freq, 0, da,
1891 wpabuf_head(buf), wpabuf_len(buf));
1892}
1893
1894
1895static const struct wpabuf * fst_hostapd_get_mb_ie_cb(void *ctx, const u8 *addr)
1896{
1897 struct hostapd_data *hapd = ctx;
1898 struct sta_info *sta = ap_get_sta(hapd, addr);
1899
1900 return sta ? sta->mb_ies : NULL;
1901}
1902
1903
1904static void fst_hostapd_update_mb_ie_cb(void *ctx, const u8 *addr,
1905 const u8 *buf, size_t size)
1906{
1907 struct hostapd_data *hapd = ctx;
1908 struct sta_info *sta = ap_get_sta(hapd, addr);
1909
1910 if (sta) {
1911 struct mb_ies_info info;
1912
1913 if (!mb_ies_info_by_ies(&info, buf, size)) {
1914 wpabuf_free(sta->mb_ies);
1915 sta->mb_ies = mb_ies_by_info(&info);
1916 }
1917 }
1918}
1919
1920
1921static const u8 * fst_hostapd_get_sta(struct fst_get_peer_ctx **get_ctx,
Hai Shalome21d4e82020-04-29 16:34:06 -07001922 bool mb_only)
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001923{
1924 struct sta_info *s = (struct sta_info *) *get_ctx;
1925
1926 if (mb_only) {
1927 for (; s && !s->mb_ies; s = s->next)
1928 ;
1929 }
1930
1931 if (s) {
1932 *get_ctx = (struct fst_get_peer_ctx *) s->next;
1933
1934 return s->addr;
1935 }
1936
1937 *get_ctx = NULL;
1938 return NULL;
1939}
1940
1941
1942static const u8 * fst_hostapd_get_peer_first(void *ctx,
1943 struct fst_get_peer_ctx **get_ctx,
Hai Shalome21d4e82020-04-29 16:34:06 -07001944 bool mb_only)
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001945{
1946 struct hostapd_data *hapd = ctx;
1947
1948 *get_ctx = (struct fst_get_peer_ctx *) hapd->sta_list;
1949
1950 return fst_hostapd_get_sta(get_ctx, mb_only);
1951}
1952
1953
1954static const u8 * fst_hostapd_get_peer_next(void *ctx,
1955 struct fst_get_peer_ctx **get_ctx,
Hai Shalome21d4e82020-04-29 16:34:06 -07001956 bool mb_only)
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001957{
1958 return fst_hostapd_get_sta(get_ctx, mb_only);
1959}
1960
1961
1962void fst_hostapd_fill_iface_obj(struct hostapd_data *hapd,
1963 struct fst_wpa_obj *iface_obj)
1964{
Sunil8cd6f4d2022-06-28 18:40:46 +00001965 os_memset(iface_obj, 0, sizeof(*iface_obj));
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001966 iface_obj->ctx = hapd;
1967 iface_obj->get_bssid = fst_hostapd_get_bssid_cb;
1968 iface_obj->get_channel_info = fst_hostapd_get_channel_info_cb;
Sunil8cd6f4d2022-06-28 18:40:46 +00001969 iface_obj->get_hw_modes = fst_hostapd_get_hw_modes_cb;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001970 iface_obj->set_ies = fst_hostapd_set_ies_cb;
1971 iface_obj->send_action = fst_hostapd_send_action_cb;
1972 iface_obj->get_mb_ie = fst_hostapd_get_mb_ie_cb;
1973 iface_obj->update_mb_ie = fst_hostapd_update_mb_ie_cb;
1974 iface_obj->get_peer_first = fst_hostapd_get_peer_first;
1975 iface_obj->get_peer_next = fst_hostapd_get_peer_next;
1976}
1977
1978#endif /* CONFIG_FST */
1979
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001980#ifdef CONFIG_OWE
1981
1982static int hostapd_owe_iface_iter(struct hostapd_iface *iface, void *ctx)
1983{
1984 struct hostapd_data *hapd = ctx;
1985 size_t i;
1986
1987 for (i = 0; i < iface->num_bss; i++) {
1988 struct hostapd_data *bss = iface->bss[i];
1989
1990 if (os_strcmp(hapd->conf->owe_transition_ifname,
1991 bss->conf->iface) != 0)
1992 continue;
1993
1994 wpa_printf(MSG_DEBUG,
1995 "OWE: ifname=%s found transition mode ifname=%s BSSID "
1996 MACSTR " SSID %s",
1997 hapd->conf->iface, bss->conf->iface,
1998 MAC2STR(bss->own_addr),
1999 wpa_ssid_txt(bss->conf->ssid.ssid,
2000 bss->conf->ssid.ssid_len));
2001 if (!bss->conf->ssid.ssid_set || !bss->conf->ssid.ssid_len ||
2002 is_zero_ether_addr(bss->own_addr))
2003 continue;
2004
2005 os_memcpy(hapd->conf->owe_transition_bssid, bss->own_addr,
2006 ETH_ALEN);
2007 os_memcpy(hapd->conf->owe_transition_ssid,
2008 bss->conf->ssid.ssid, bss->conf->ssid.ssid_len);
2009 hapd->conf->owe_transition_ssid_len = bss->conf->ssid.ssid_len;
2010 wpa_printf(MSG_DEBUG,
2011 "OWE: Copied transition mode information");
2012 return 1;
2013 }
2014
2015 return 0;
2016}
2017
2018
2019int hostapd_owe_trans_get_info(struct hostapd_data *hapd)
2020{
2021 if (hapd->conf->owe_transition_ssid_len > 0 &&
2022 !is_zero_ether_addr(hapd->conf->owe_transition_bssid))
2023 return 0;
2024
2025 /* Find transition mode SSID/BSSID information from a BSS operated by
2026 * this hostapd instance. */
2027 if (!hapd->iface->interfaces ||
2028 !hapd->iface->interfaces->for_each_interface)
2029 return hostapd_owe_iface_iter(hapd->iface, hapd);
2030 else
2031 return hapd->iface->interfaces->for_each_interface(
2032 hapd->iface->interfaces, hostapd_owe_iface_iter, hapd);
2033}
2034
2035
2036static int hostapd_owe_iface_iter2(struct hostapd_iface *iface, void *ctx)
2037{
2038 size_t i;
2039
2040 for (i = 0; i < iface->num_bss; i++) {
2041 struct hostapd_data *bss = iface->bss[i];
2042 int res;
2043
2044 if (!bss->conf->owe_transition_ifname[0])
2045 continue;
Hai Shalom899fcc72020-10-19 14:38:18 -07002046 if (bss->iface->state != HAPD_IFACE_ENABLED) {
2047 wpa_printf(MSG_DEBUG,
2048 "OWE: Interface %s state %s - defer beacon update",
2049 bss->conf->iface,
2050 hostapd_state_text(bss->iface->state));
2051 continue;
2052 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002053 res = hostapd_owe_trans_get_info(bss);
2054 if (res == 0)
2055 continue;
2056 wpa_printf(MSG_DEBUG,
2057 "OWE: Matching transition mode interface enabled - update beacon data for %s",
2058 bss->conf->iface);
2059 ieee802_11_set_beacon(bss);
2060 }
2061
2062 return 0;
2063}
2064
2065#endif /* CONFIG_OWE */
2066
2067
2068static void hostapd_owe_update_trans(struct hostapd_iface *iface)
2069{
2070#ifdef CONFIG_OWE
2071 /* Check whether the enabled BSS can complete OWE transition mode
2072 * configuration for any pending interface. */
2073 if (!iface->interfaces ||
2074 !iface->interfaces->for_each_interface)
2075 hostapd_owe_iface_iter2(iface, NULL);
2076 else
2077 iface->interfaces->for_each_interface(
2078 iface->interfaces, hostapd_owe_iface_iter2, NULL);
2079#endif /* CONFIG_OWE */
2080}
2081
2082
Roshan Pius3a1667e2018-07-03 15:17:14 -07002083static void hostapd_interface_setup_failure_handler(void *eloop_ctx,
2084 void *timeout_ctx)
2085{
2086 struct hostapd_iface *iface = eloop_ctx;
2087 struct hostapd_data *hapd;
2088
2089 if (iface->num_bss < 1 || !iface->bss || !iface->bss[0])
2090 return;
2091 hapd = iface->bss[0];
2092 if (hapd->setup_complete_cb)
2093 hapd->setup_complete_cb(hapd->setup_complete_cb_ctx);
2094}
2095
2096
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002097static int hostapd_setup_interface_complete_sync(struct hostapd_iface *iface,
2098 int err)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002099{
2100 struct hostapd_data *hapd = iface->bss[0];
2101 size_t j;
2102 u8 *prev_addr;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002103 int delay_apply_cfg = 0;
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08002104 int res_dfs_offload = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002105
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002106 if (err)
2107 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002108
2109 wpa_printf(MSG_DEBUG, "Completing interface initialization");
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08002110 if (iface->freq) {
Dmitry Shmidt051af732013-10-22 13:52:46 -07002111#ifdef NEED_AP_MLME
2112 int res;
2113#endif /* NEED_AP_MLME */
2114
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002115 wpa_printf(MSG_DEBUG, "Mode: %s Channel: %d "
2116 "Frequency: %d MHz",
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002117 hostapd_hw_mode_txt(iface->conf->hw_mode),
2118 iface->conf->channel, iface->freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002119
Dmitry Shmidt051af732013-10-22 13:52:46 -07002120#ifdef NEED_AP_MLME
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07002121 /* Handle DFS only if it is not offloaded to the driver */
2122 if (!(iface->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD)) {
2123 /* Check DFS */
2124 res = hostapd_handle_dfs(iface);
2125 if (res <= 0) {
2126 if (res < 0)
2127 goto fail;
2128 return res;
2129 }
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08002130 } else {
2131 /* If DFS is offloaded to the driver */
2132 res_dfs_offload = hostapd_handle_dfs_offload(iface);
2133 if (res_dfs_offload <= 0) {
2134 if (res_dfs_offload < 0)
2135 goto fail;
2136 } else {
2137 wpa_printf(MSG_DEBUG,
2138 "Proceed with AP/channel setup");
2139 /*
2140 * If this is a DFS channel, move to completing
2141 * AP setup.
2142 */
2143 if (res_dfs_offload == 1)
2144 goto dfs_offload;
2145 /* Otherwise fall through. */
2146 }
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002147 }
Dmitry Shmidt051af732013-10-22 13:52:46 -07002148#endif /* NEED_AP_MLME */
2149
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002150#ifdef CONFIG_MESH
2151 if (iface->mconf != NULL) {
2152 wpa_printf(MSG_DEBUG,
2153 "%s: Mesh configuration will be applied while joining the mesh network",
2154 iface->bss[0]->conf->iface);
2155 delay_apply_cfg = 1;
2156 }
2157#endif /* CONFIG_MESH */
2158
2159 if (!delay_apply_cfg &&
2160 hostapd_set_freq(hapd, hapd->iconf->hw_mode, iface->freq,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002161 hapd->iconf->channel,
Hai Shalomc3565922019-10-28 11:58:20 -07002162 hapd->iconf->enable_edmg,
2163 hapd->iconf->edmg_channel,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002164 hapd->iconf->ieee80211n,
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002165 hapd->iconf->ieee80211ac,
Hai Shalom81f62d82019-07-22 12:10:00 -07002166 hapd->iconf->ieee80211ax,
Sunil Ravia04bd252022-05-02 22:54:18 -07002167 hapd->iconf->ieee80211be,
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002168 hapd->iconf->secondary_channel,
Hai Shalom81f62d82019-07-22 12:10:00 -07002169 hostapd_get_oper_chwidth(hapd->iconf),
2170 hostapd_get_oper_centr_freq_seg0_idx(
2171 hapd->iconf),
2172 hostapd_get_oper_centr_freq_seg1_idx(
2173 hapd->iconf))) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002174 wpa_printf(MSG_ERROR, "Could not set channel for "
2175 "kernel driver");
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002176 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002177 }
2178 }
2179
2180 if (iface->current_mode) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002181 if (hostapd_prepare_rates(iface, iface->current_mode)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002182 wpa_printf(MSG_ERROR, "Failed to prepare rates "
2183 "table.");
2184 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
2185 HOSTAPD_LEVEL_WARNING,
2186 "Failed to prepare rates table.");
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002187 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002188 }
2189 }
2190
Hai Shalom021b0b52019-04-10 11:17:58 -07002191 if (hapd->iconf->rts_threshold >= -1 &&
2192 hostapd_set_rts(hapd, hapd->iconf->rts_threshold) &&
2193 hapd->iconf->rts_threshold >= -1) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002194 wpa_printf(MSG_ERROR, "Could not set RTS threshold for "
2195 "kernel driver");
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002196 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002197 }
2198
Hai Shalom021b0b52019-04-10 11:17:58 -07002199 if (hapd->iconf->fragm_threshold >= -1 &&
2200 hostapd_set_frag(hapd, hapd->iconf->fragm_threshold) &&
2201 hapd->iconf->fragm_threshold != -1) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002202 wpa_printf(MSG_ERROR, "Could not set fragmentation threshold "
2203 "for kernel driver");
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002204 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002205 }
2206
2207 prev_addr = hapd->own_addr;
2208
2209 for (j = 0; j < iface->num_bss; j++) {
2210 hapd = iface->bss[j];
2211 if (j)
2212 os_memcpy(hapd->own_addr, prev_addr, ETH_ALEN);
Sunil Ravi77d572f2023-01-17 23:58:31 +00002213 if (hostapd_setup_bss(hapd, j == 0, !iface->conf->mbssid)) {
Hai Shalom021b0b52019-04-10 11:17:58 -07002214 for (;;) {
Dmitry Shmidt71757432014-06-02 13:50:35 -07002215 hapd = iface->bss[j];
2216 hostapd_bss_deinit_no_free(hapd);
2217 hostapd_free_hapd_data(hapd);
Hai Shalom021b0b52019-04-10 11:17:58 -07002218 if (j == 0)
2219 break;
2220 j--;
2221 }
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002222 goto fail;
Dmitry Shmidt71757432014-06-02 13:50:35 -07002223 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002224 if (is_zero_ether_addr(hapd->conf->bssid))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002225 prev_addr = hapd->own_addr;
2226 }
Sunil Ravi77d572f2023-01-17 23:58:31 +00002227
2228 if (hapd->iconf->mbssid) {
2229 for (j = 0; hapd->iconf->mbssid && j < iface->num_bss; j++) {
2230 hapd = iface->bss[j];
2231 if (hostapd_start_beacon(hapd, true)) {
2232 for (;;) {
2233 hapd = iface->bss[j];
2234 hostapd_bss_deinit_no_free(hapd);
2235 hostapd_free_hapd_data(hapd);
2236 if (j == 0)
2237 break;
2238 j--;
2239 }
2240 goto fail;
2241 }
2242 }
2243 }
2244
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002245 hapd = iface->bss[0];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002246
2247 hostapd_tx_queue_params(iface);
2248
2249 ap_list_init(iface);
2250
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07002251 hostapd_set_acl(hapd);
2252
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002253 if (hostapd_driver_commit(hapd) < 0) {
2254 wpa_printf(MSG_ERROR, "%s: Failed to commit driver "
2255 "configuration", __func__);
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002256 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002257 }
2258
Jouni Malinen87fd2792011-05-16 18:35:42 +03002259 /*
2260 * WPS UPnP module can be initialized only when the "upnp_iface" is up.
2261 * If "interface" and "upnp_iface" are the same (e.g., non-bridge
2262 * mode), the interface is up only after driver_commit, so initialize
2263 * WPS after driver_commit.
2264 */
2265 for (j = 0; j < iface->num_bss; j++) {
2266 if (hostapd_init_wps_complete(iface->bss[j]))
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002267 goto fail;
Jouni Malinen87fd2792011-05-16 18:35:42 +03002268 }
2269
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08002270 if ((iface->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
2271 !res_dfs_offload) {
2272 /*
2273 * If freq is DFS, and DFS is offloaded to the driver, then wait
2274 * for CAC to complete.
2275 */
2276 wpa_printf(MSG_DEBUG, "%s: Wait for CAC to complete", __func__);
2277 return res_dfs_offload;
2278 }
2279
2280#ifdef NEED_AP_MLME
2281dfs_offload:
2282#endif /* NEED_AP_MLME */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002283
2284#ifdef CONFIG_FST
2285 if (hapd->iconf->fst_cfg.group_id[0]) {
2286 struct fst_wpa_obj iface_obj;
2287
2288 fst_hostapd_fill_iface_obj(hapd, &iface_obj);
2289 iface->fst = fst_attach(hapd->conf->iface, hapd->own_addr,
2290 &iface_obj, &hapd->iconf->fst_cfg);
2291 if (!iface->fst) {
2292 wpa_printf(MSG_ERROR, "Could not attach to FST %s",
2293 hapd->iconf->fst_cfg.group_id);
2294 goto fail;
2295 }
2296 }
2297#endif /* CONFIG_FST */
2298
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002299 hostapd_set_state(iface, HAPD_IFACE_ENABLED);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002300 hostapd_owe_update_trans(iface);
Hai Shalom81f62d82019-07-22 12:10:00 -07002301 airtime_policy_update_init(iface);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002302 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_ENABLED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002303 if (hapd->setup_complete_cb)
2304 hapd->setup_complete_cb(hapd->setup_complete_cb_ctx);
2305
Hai Shalom60840252021-02-19 19:02:11 -08002306#ifdef CONFIG_MESH
2307 if (delay_apply_cfg && !iface->mconf) {
2308 wpa_printf(MSG_ERROR, "Error while completing mesh init");
2309 goto fail;
2310 }
2311#endif /* CONFIG_MESH */
2312
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002313 wpa_printf(MSG_DEBUG, "%s: Setup of interface done.",
2314 iface->bss[0]->conf->iface);
Dmitry Shmidtb96dad42013-11-05 10:07:29 -08002315 if (iface->interfaces && iface->interfaces->terminate_on_error > 0)
2316 iface->interfaces->terminate_on_error--;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002317
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002318 for (j = 0; j < iface->num_bss; j++)
Hai Shalom74f70d42019-02-11 14:42:39 -08002319 hostapd_neighbor_set_own_report(iface->bss[j]);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002320
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002321 return 0;
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002322
2323fail:
2324 wpa_printf(MSG_ERROR, "Interface initialization failed");
2325 hostapd_set_state(iface, HAPD_IFACE_DISABLED);
2326 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002327#ifdef CONFIG_FST
2328 if (iface->fst) {
2329 fst_detach(iface->fst);
2330 iface->fst = NULL;
2331 }
2332#endif /* CONFIG_FST */
Roshan Pius3a1667e2018-07-03 15:17:14 -07002333
2334 if (iface->interfaces && iface->interfaces->terminate_on_error) {
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002335 eloop_terminate();
Roshan Pius3a1667e2018-07-03 15:17:14 -07002336 } else if (hapd->setup_complete_cb) {
2337 /*
2338 * Calling hapd->setup_complete_cb directly may cause iface
2339 * deinitialization which may be accessed later by the caller.
2340 */
2341 eloop_register_timeout(0, 0,
2342 hostapd_interface_setup_failure_handler,
2343 iface, NULL);
2344 }
2345
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002346 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002347}
2348
2349
2350/**
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002351 * hostapd_setup_interface_complete - Complete interface setup
2352 *
2353 * This function is called when previous steps in the interface setup has been
2354 * completed. This can also start operations, e.g., DFS, that will require
2355 * additional processing before interface is ready to be enabled. Such
2356 * operations will call this function from eloop callbacks when finished.
2357 */
2358int hostapd_setup_interface_complete(struct hostapd_iface *iface, int err)
2359{
2360 struct hapd_interfaces *interfaces = iface->interfaces;
2361 struct hostapd_data *hapd = iface->bss[0];
2362 unsigned int i;
2363 int not_ready_in_sync_ifaces = 0;
2364
2365 if (!iface->need_to_start_in_sync)
2366 return hostapd_setup_interface_complete_sync(iface, err);
2367
2368 if (err) {
2369 wpa_printf(MSG_ERROR, "Interface initialization failed");
2370 hostapd_set_state(iface, HAPD_IFACE_DISABLED);
2371 iface->need_to_start_in_sync = 0;
2372 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
2373 if (interfaces && interfaces->terminate_on_error)
2374 eloop_terminate();
2375 return -1;
2376 }
2377
2378 if (iface->ready_to_start_in_sync) {
2379 /* Already in ready and waiting. should never happpen */
2380 return 0;
2381 }
2382
2383 for (i = 0; i < interfaces->count; i++) {
2384 if (interfaces->iface[i]->need_to_start_in_sync &&
2385 !interfaces->iface[i]->ready_to_start_in_sync)
2386 not_ready_in_sync_ifaces++;
2387 }
2388
2389 /*
2390 * Check if this is the last interface, if yes then start all the other
2391 * waiting interfaces. If not, add this interface to the waiting list.
2392 */
2393 if (not_ready_in_sync_ifaces > 1 && iface->state == HAPD_IFACE_DFS) {
2394 /*
2395 * If this interface went through CAC, do not synchronize, just
2396 * start immediately.
2397 */
2398 iface->need_to_start_in_sync = 0;
2399 wpa_printf(MSG_INFO,
2400 "%s: Finished CAC - bypass sync and start interface",
2401 iface->bss[0]->conf->iface);
2402 return hostapd_setup_interface_complete_sync(iface, err);
2403 }
2404
2405 if (not_ready_in_sync_ifaces > 1) {
2406 /* need to wait as there are other interfaces still coming up */
2407 iface->ready_to_start_in_sync = 1;
2408 wpa_printf(MSG_INFO,
2409 "%s: Interface waiting to sync with other interfaces",
2410 iface->bss[0]->conf->iface);
2411 return 0;
2412 }
2413
2414 wpa_printf(MSG_INFO,
2415 "%s: Last interface to sync - starting all interfaces",
2416 iface->bss[0]->conf->iface);
2417 iface->need_to_start_in_sync = 0;
2418 hostapd_setup_interface_complete_sync(iface, err);
2419 for (i = 0; i < interfaces->count; i++) {
2420 if (interfaces->iface[i]->need_to_start_in_sync &&
2421 interfaces->iface[i]->ready_to_start_in_sync) {
2422 hostapd_setup_interface_complete_sync(
2423 interfaces->iface[i], 0);
2424 /* Only once the interfaces are sync started */
2425 interfaces->iface[i]->need_to_start_in_sync = 0;
2426 }
2427 }
2428
2429 return 0;
2430}
2431
2432
2433/**
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002434 * hostapd_setup_interface - Setup of an interface
2435 * @iface: Pointer to interface data.
2436 * Returns: 0 on success, -1 on failure
2437 *
2438 * Initializes the driver interface, validates the configuration,
2439 * and sets driver parameters based on the configuration.
2440 * Flushes old stations, sets the channel, encryption,
2441 * beacons, and WDS links based on the configuration.
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002442 *
2443 * If interface setup requires more time, e.g., to perform HT co-ex scans, ACS,
2444 * or DFS operations, this function returns 0 before such operations have been
2445 * completed. The pending operations are registered into eloop and will be
2446 * completed from eloop callbacks. Those callbacks end up calling
2447 * hostapd_setup_interface_complete() once setup has been completed.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002448 */
2449int hostapd_setup_interface(struct hostapd_iface *iface)
2450{
2451 int ret;
2452
Hai Shaloma20dcd72022-02-04 13:43:00 -08002453 if (!iface->conf)
2454 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002455 ret = setup_interface(iface);
2456 if (ret) {
2457 wpa_printf(MSG_ERROR, "%s: Unable to setup interface.",
Hai Shaloma20dcd72022-02-04 13:43:00 -08002458 iface->conf->bss[0]->iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002459 return -1;
2460 }
2461
2462 return 0;
2463}
2464
2465
2466/**
2467 * hostapd_alloc_bss_data - Allocate and initialize per-BSS data
2468 * @hapd_iface: Pointer to interface data
2469 * @conf: Pointer to per-interface configuration
2470 * @bss: Pointer to per-BSS configuration for this BSS
2471 * Returns: Pointer to allocated BSS data
2472 *
2473 * This function is used to allocate per-BSS data structure. This data will be
2474 * freed after hostapd_cleanup() is called for it during interface
2475 * deinitialization.
2476 */
2477struct hostapd_data *
2478hostapd_alloc_bss_data(struct hostapd_iface *hapd_iface,
2479 struct hostapd_config *conf,
2480 struct hostapd_bss_config *bss)
2481{
2482 struct hostapd_data *hapd;
2483
2484 hapd = os_zalloc(sizeof(*hapd));
2485 if (hapd == NULL)
2486 return NULL;
2487
2488 hapd->new_assoc_sta_cb = hostapd_new_assoc_sta;
2489 hapd->iconf = conf;
2490 hapd->conf = bss;
2491 hapd->iface = hapd_iface;
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08002492 if (conf)
2493 hapd->driver = conf->driver;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002494 hapd->ctrl_sock = -1;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08002495 dl_list_init(&hapd->ctrl_dst);
Dmitry Shmidt7d175302016-09-06 13:11:34 -07002496 dl_list_init(&hapd->nr_db);
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08002497 hapd->dhcp_sock = -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002498#ifdef CONFIG_IEEE80211R_AP
2499 dl_list_init(&hapd->l2_queue);
2500 dl_list_init(&hapd->l2_oui_queue);
2501#endif /* CONFIG_IEEE80211R_AP */
Hai Shalom021b0b52019-04-10 11:17:58 -07002502#ifdef CONFIG_SAE
2503 dl_list_init(&hapd->sae_commit_queue);
2504#endif /* CONFIG_SAE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002505
2506 return hapd;
2507}
2508
2509
Dmitry Shmidt54605472013-11-08 11:10:19 -08002510static void hostapd_bss_deinit(struct hostapd_data *hapd)
2511{
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07002512 if (!hapd)
2513 return;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002514 wpa_printf(MSG_DEBUG, "%s: deinit bss %s", __func__,
Hai Shalom021b0b52019-04-10 11:17:58 -07002515 hapd->conf ? hapd->conf->iface : "N/A");
Dmitry Shmidt71757432014-06-02 13:50:35 -07002516 hostapd_bss_deinit_no_free(hapd);
Dmitry Shmidtf73259c2015-03-17 11:00:54 -07002517 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
Hai Shalomc3565922019-10-28 11:58:20 -07002518#ifdef CONFIG_SQLITE
2519 if (hapd->rad_attr_db) {
2520 sqlite3_close(hapd->rad_attr_db);
2521 hapd->rad_attr_db = NULL;
2522 }
2523#endif /* CONFIG_SQLITE */
Dmitry Shmidt54605472013-11-08 11:10:19 -08002524 hostapd_cleanup(hapd);
2525}
2526
2527
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002528void hostapd_interface_deinit(struct hostapd_iface *iface)
2529{
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002530 int j;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002531
Dmitry Shmidt54605472013-11-08 11:10:19 -08002532 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002533 if (iface == NULL)
2534 return;
2535
Dmitry Shmidtf73259c2015-03-17 11:00:54 -07002536 hostapd_set_state(iface, HAPD_IFACE_DISABLED);
2537
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002538 eloop_cancel_timeout(channel_list_update_timeout, iface, NULL);
2539 iface->wait_channel_update = 0;
2540
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002541#ifdef CONFIG_FST
2542 if (iface->fst) {
2543 fst_detach(iface->fst);
2544 iface->fst = NULL;
2545 }
2546#endif /* CONFIG_FST */
2547
Hai Shalom021b0b52019-04-10 11:17:58 -07002548 for (j = (int) iface->num_bss - 1; j >= 0; j--) {
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07002549 if (!iface->bss)
2550 break;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002551 hostapd_bss_deinit(iface->bss[j]);
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07002552 }
Roshan Pius3a1667e2018-07-03 15:17:14 -07002553
Roshan Pius3a1667e2018-07-03 15:17:14 -07002554#ifdef NEED_AP_MLME
2555 hostapd_stop_setup_timers(iface);
2556 eloop_cancel_timeout(ap_ht2040_timeout, iface, NULL);
2557#endif /* NEED_AP_MLME */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002558}
2559
2560
2561void hostapd_interface_free(struct hostapd_iface *iface)
2562{
2563 size_t j;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002564 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
2565 for (j = 0; j < iface->num_bss; j++) {
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07002566 if (!iface->bss)
2567 break;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002568 wpa_printf(MSG_DEBUG, "%s: free hapd %p",
2569 __func__, iface->bss[j]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002570 os_free(iface->bss[j]);
Dmitry Shmidt54605472013-11-08 11:10:19 -08002571 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002572 hostapd_cleanup_iface(iface);
2573}
2574
2575
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07002576struct hostapd_iface * hostapd_alloc_iface(void)
2577{
2578 struct hostapd_iface *hapd_iface;
2579
2580 hapd_iface = os_zalloc(sizeof(*hapd_iface));
2581 if (!hapd_iface)
2582 return NULL;
2583
2584 dl_list_init(&hapd_iface->sta_seen);
2585
2586 return hapd_iface;
2587}
2588
2589
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002590/**
2591 * hostapd_init - Allocate and initialize per-interface data
2592 * @config_file: Path to the configuration file
2593 * Returns: Pointer to the allocated interface data or %NULL on failure
2594 *
2595 * This function is used to allocate main data structures for per-interface
2596 * data. The allocated data buffer will be freed by calling
2597 * hostapd_cleanup_iface().
2598 */
2599struct hostapd_iface * hostapd_init(struct hapd_interfaces *interfaces,
2600 const char *config_file)
2601{
2602 struct hostapd_iface *hapd_iface = NULL;
2603 struct hostapd_config *conf = NULL;
2604 struct hostapd_data *hapd;
2605 size_t i;
2606
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07002607 hapd_iface = hostapd_alloc_iface();
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002608 if (hapd_iface == NULL)
2609 goto fail;
2610
2611 hapd_iface->config_fname = os_strdup(config_file);
2612 if (hapd_iface->config_fname == NULL)
2613 goto fail;
2614
2615 conf = interfaces->config_read_cb(hapd_iface->config_fname);
2616 if (conf == NULL)
2617 goto fail;
2618 hapd_iface->conf = conf;
2619
2620 hapd_iface->num_bss = conf->num_bss;
2621 hapd_iface->bss = os_calloc(conf->num_bss,
2622 sizeof(struct hostapd_data *));
2623 if (hapd_iface->bss == NULL)
2624 goto fail;
2625
2626 for (i = 0; i < conf->num_bss; i++) {
2627 hapd = hapd_iface->bss[i] =
2628 hostapd_alloc_bss_data(hapd_iface, conf,
2629 conf->bss[i]);
2630 if (hapd == NULL)
2631 goto fail;
2632 hapd->msg_ctx = hapd;
2633 }
2634
2635 return hapd_iface;
2636
2637fail:
2638 wpa_printf(MSG_ERROR, "Failed to set up interface with %s",
2639 config_file);
2640 if (conf)
2641 hostapd_config_free(conf);
2642 if (hapd_iface) {
2643 os_free(hapd_iface->config_fname);
2644 os_free(hapd_iface->bss);
Dmitry Shmidt54605472013-11-08 11:10:19 -08002645 wpa_printf(MSG_DEBUG, "%s: free iface %p",
2646 __func__, hapd_iface);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002647 os_free(hapd_iface);
2648 }
2649 return NULL;
2650}
2651
2652
2653static int ifname_in_use(struct hapd_interfaces *interfaces, const char *ifname)
2654{
2655 size_t i, j;
2656
2657 for (i = 0; i < interfaces->count; i++) {
2658 struct hostapd_iface *iface = interfaces->iface[i];
2659 for (j = 0; j < iface->num_bss; j++) {
2660 struct hostapd_data *hapd = iface->bss[j];
2661 if (os_strcmp(ifname, hapd->conf->iface) == 0)
2662 return 1;
2663 }
2664 }
2665
2666 return 0;
2667}
2668
2669
2670/**
2671 * hostapd_interface_init_bss - Read configuration file and init BSS data
2672 *
2673 * This function is used to parse configuration file for a BSS. This BSS is
2674 * added to an existing interface sharing the same radio (if any) or a new
2675 * interface is created if this is the first interface on a radio. This
2676 * allocate memory for the BSS. No actual driver operations are started.
2677 *
2678 * This is similar to hostapd_interface_init(), but for a case where the
2679 * configuration is used to add a single BSS instead of all BSSes for a radio.
2680 */
2681struct hostapd_iface *
2682hostapd_interface_init_bss(struct hapd_interfaces *interfaces, const char *phy,
2683 const char *config_fname, int debug)
2684{
2685 struct hostapd_iface *new_iface = NULL, *iface = NULL;
2686 struct hostapd_data *hapd;
2687 int k;
2688 size_t i, bss_idx;
2689
2690 if (!phy || !*phy)
2691 return NULL;
2692
2693 for (i = 0; i < interfaces->count; i++) {
2694 if (os_strcmp(interfaces->iface[i]->phy, phy) == 0) {
2695 iface = interfaces->iface[i];
2696 break;
2697 }
2698 }
2699
2700 wpa_printf(MSG_INFO, "Configuration file: %s (phy %s)%s",
2701 config_fname, phy, iface ? "" : " --> new PHY");
2702 if (iface) {
2703 struct hostapd_config *conf;
2704 struct hostapd_bss_config **tmp_conf;
2705 struct hostapd_data **tmp_bss;
2706 struct hostapd_bss_config *bss;
2707 const char *ifname;
2708
2709 /* Add new BSS to existing iface */
2710 conf = interfaces->config_read_cb(config_fname);
2711 if (conf == NULL)
2712 return NULL;
2713 if (conf->num_bss > 1) {
2714 wpa_printf(MSG_ERROR, "Multiple BSSes specified in BSS-config");
2715 hostapd_config_free(conf);
2716 return NULL;
2717 }
2718
2719 ifname = conf->bss[0]->iface;
2720 if (ifname[0] != '\0' && ifname_in_use(interfaces, ifname)) {
2721 wpa_printf(MSG_ERROR,
2722 "Interface name %s already in use", ifname);
2723 hostapd_config_free(conf);
2724 return NULL;
2725 }
2726
2727 tmp_conf = os_realloc_array(
2728 iface->conf->bss, iface->conf->num_bss + 1,
2729 sizeof(struct hostapd_bss_config *));
2730 tmp_bss = os_realloc_array(iface->bss, iface->num_bss + 1,
2731 sizeof(struct hostapd_data *));
2732 if (tmp_bss)
2733 iface->bss = tmp_bss;
2734 if (tmp_conf) {
2735 iface->conf->bss = tmp_conf;
2736 iface->conf->last_bss = tmp_conf[0];
2737 }
2738 if (tmp_bss == NULL || tmp_conf == NULL) {
2739 hostapd_config_free(conf);
2740 return NULL;
2741 }
2742 bss = iface->conf->bss[iface->conf->num_bss] = conf->bss[0];
2743 iface->conf->num_bss++;
2744
2745 hapd = hostapd_alloc_bss_data(iface, iface->conf, bss);
2746 if (hapd == NULL) {
2747 iface->conf->num_bss--;
2748 hostapd_config_free(conf);
2749 return NULL;
2750 }
2751 iface->conf->last_bss = bss;
2752 iface->bss[iface->num_bss] = hapd;
2753 hapd->msg_ctx = hapd;
2754
2755 bss_idx = iface->num_bss++;
2756 conf->num_bss--;
2757 conf->bss[0] = NULL;
2758 hostapd_config_free(conf);
2759 } else {
2760 /* Add a new iface with the first BSS */
2761 new_iface = iface = hostapd_init(interfaces, config_fname);
2762 if (!iface)
2763 return NULL;
2764 os_strlcpy(iface->phy, phy, sizeof(iface->phy));
2765 iface->interfaces = interfaces;
2766 bss_idx = 0;
2767 }
2768
2769 for (k = 0; k < debug; k++) {
2770 if (iface->bss[bss_idx]->conf->logger_stdout_level > 0)
2771 iface->bss[bss_idx]->conf->logger_stdout_level--;
2772 }
2773
2774 if (iface->conf->bss[bss_idx]->iface[0] == '\0' &&
2775 !hostapd_drv_none(iface->bss[bss_idx])) {
2776 wpa_printf(MSG_ERROR, "Interface name not specified in %s",
2777 config_fname);
2778 if (new_iface)
2779 hostapd_interface_deinit_free(new_iface);
2780 return NULL;
2781 }
2782
2783 return iface;
2784}
2785
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002786
2787void hostapd_interface_deinit_free(struct hostapd_iface *iface)
2788{
2789 const struct wpa_driver_ops *driver;
2790 void *drv_priv;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002791
2792 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002793 if (iface == NULL)
2794 return;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002795 wpa_printf(MSG_DEBUG, "%s: num_bss=%u conf->num_bss=%u",
2796 __func__, (unsigned int) iface->num_bss,
2797 (unsigned int) iface->conf->num_bss);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002798 driver = iface->bss[0]->driver;
2799 drv_priv = iface->bss[0]->drv_priv;
2800 hostapd_interface_deinit(iface);
Dmitry Shmidt54605472013-11-08 11:10:19 -08002801 wpa_printf(MSG_DEBUG, "%s: driver=%p drv_priv=%p -> hapd_deinit",
2802 __func__, driver, drv_priv);
Dmitry Shmidt71757432014-06-02 13:50:35 -07002803 if (driver && driver->hapd_deinit && drv_priv) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002804 driver->hapd_deinit(drv_priv);
Dmitry Shmidt71757432014-06-02 13:50:35 -07002805 iface->bss[0]->drv_priv = NULL;
2806 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002807 hostapd_interface_free(iface);
2808}
2809
2810
Dmitry Shmidt15907092014-03-25 10:42:57 -07002811static void hostapd_deinit_driver(const struct wpa_driver_ops *driver,
2812 void *drv_priv,
2813 struct hostapd_iface *hapd_iface)
2814{
2815 size_t j;
2816
2817 wpa_printf(MSG_DEBUG, "%s: driver=%p drv_priv=%p -> hapd_deinit",
2818 __func__, driver, drv_priv);
2819 if (driver && driver->hapd_deinit && drv_priv) {
2820 driver->hapd_deinit(drv_priv);
2821 for (j = 0; j < hapd_iface->num_bss; j++) {
2822 wpa_printf(MSG_DEBUG, "%s:bss[%d]->drv_priv=%p",
2823 __func__, (int) j,
2824 hapd_iface->bss[j]->drv_priv);
Hai Shalom1dc4d202019-04-29 16:22:27 -07002825 if (hapd_iface->bss[j]->drv_priv == drv_priv) {
Dmitry Shmidt15907092014-03-25 10:42:57 -07002826 hapd_iface->bss[j]->drv_priv = NULL;
Hai Shalom1dc4d202019-04-29 16:22:27 -07002827 hapd_iface->extended_capa = NULL;
2828 hapd_iface->extended_capa_mask = NULL;
2829 hapd_iface->extended_capa_len = 0;
2830 }
Dmitry Shmidt15907092014-03-25 10:42:57 -07002831 }
2832 }
2833}
2834
2835
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002836int hostapd_enable_iface(struct hostapd_iface *hapd_iface)
2837{
Dmitry Shmidt71757432014-06-02 13:50:35 -07002838 size_t j;
2839
Hai Shalom60840252021-02-19 19:02:11 -08002840 if (!hapd_iface)
2841 return -1;
2842
2843 if (hapd_iface->enable_iface_cb)
2844 return hapd_iface->enable_iface_cb(hapd_iface);
2845
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002846 if (hapd_iface->bss[0]->drv_priv != NULL) {
2847 wpa_printf(MSG_ERROR, "Interface %s already enabled",
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002848 hapd_iface->conf->bss[0]->iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002849 return -1;
2850 }
2851
2852 wpa_printf(MSG_DEBUG, "Enable interface %s",
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002853 hapd_iface->conf->bss[0]->iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002854
Dmitry Shmidt71757432014-06-02 13:50:35 -07002855 for (j = 0; j < hapd_iface->num_bss; j++)
2856 hostapd_set_security_params(hapd_iface->conf->bss[j], 1);
Dmitry Shmidt344abd32014-01-14 13:17:00 -08002857 if (hostapd_config_check(hapd_iface->conf, 1) < 0) {
2858 wpa_printf(MSG_INFO, "Invalid configuration - cannot enable");
2859 return -1;
2860 }
2861
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002862 if (hapd_iface->interfaces == NULL ||
2863 hapd_iface->interfaces->driver_init == NULL ||
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002864 hapd_iface->interfaces->driver_init(hapd_iface))
2865 return -1;
2866
2867 if (hostapd_setup_interface(hapd_iface)) {
Dmitry Shmidt15907092014-03-25 10:42:57 -07002868 hostapd_deinit_driver(hapd_iface->bss[0]->driver,
2869 hapd_iface->bss[0]->drv_priv,
2870 hapd_iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002871 return -1;
2872 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002873
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002874 return 0;
2875}
2876
2877
2878int hostapd_reload_iface(struct hostapd_iface *hapd_iface)
2879{
2880 size_t j;
2881
2882 wpa_printf(MSG_DEBUG, "Reload interface %s",
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002883 hapd_iface->conf->bss[0]->iface);
2884 for (j = 0; j < hapd_iface->num_bss; j++)
Dmitry Shmidt71757432014-06-02 13:50:35 -07002885 hostapd_set_security_params(hapd_iface->conf->bss[j], 1);
Dmitry Shmidt344abd32014-01-14 13:17:00 -08002886 if (hostapd_config_check(hapd_iface->conf, 1) < 0) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002887 wpa_printf(MSG_ERROR, "Updated configuration is invalid");
2888 return -1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002889 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002890 hostapd_clear_old(hapd_iface);
2891 for (j = 0; j < hapd_iface->num_bss; j++)
2892 hostapd_reload_bss(hapd_iface->bss[j]);
2893
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002894 return 0;
2895}
2896
2897
Sunil Ravi77d572f2023-01-17 23:58:31 +00002898int hostapd_reload_bss_only(struct hostapd_data *bss)
2899{
2900
2901 wpa_printf(MSG_DEBUG, "Reload BSS %s", bss->conf->iface);
2902 hostapd_set_security_params(bss->conf, 1);
2903 if (hostapd_config_check(bss->iconf, 1) < 0) {
2904 wpa_printf(MSG_ERROR, "Updated BSS configuration is invalid");
2905 return -1;
2906 }
2907 hostapd_clear_old_bss(bss);
2908 hostapd_reload_bss(bss);
2909 return 0;
2910}
2911
2912
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002913int hostapd_disable_iface(struct hostapd_iface *hapd_iface)
2914{
2915 size_t j;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002916 const struct wpa_driver_ops *driver;
2917 void *drv_priv;
2918
2919 if (hapd_iface == NULL)
2920 return -1;
Dmitry Shmidt71757432014-06-02 13:50:35 -07002921
Hai Shalom60840252021-02-19 19:02:11 -08002922 if (hapd_iface->disable_iface_cb)
2923 return hapd_iface->disable_iface_cb(hapd_iface);
2924
Dmitry Shmidt71757432014-06-02 13:50:35 -07002925 if (hapd_iface->bss[0]->drv_priv == NULL) {
2926 wpa_printf(MSG_INFO, "Interface %s already disabled",
2927 hapd_iface->conf->bss[0]->iface);
2928 return -1;
2929 }
2930
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002931 wpa_msg(hapd_iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002932 driver = hapd_iface->bss[0]->driver;
2933 drv_priv = hapd_iface->bss[0]->drv_priv;
2934
Dmitry Shmidta38abf92014-03-06 13:38:44 -08002935 hapd_iface->driver_ap_teardown =
2936 !!(hapd_iface->drv_flags &
2937 WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
2938
Hai Shalom39ba6fc2019-01-22 12:40:38 -08002939#ifdef NEED_AP_MLME
2940 for (j = 0; j < hapd_iface->num_bss; j++)
2941 hostapd_cleanup_cs_params(hapd_iface->bss[j]);
2942#endif /* NEED_AP_MLME */
2943
Dmitry Shmidta38abf92014-03-06 13:38:44 -08002944 /* same as hostapd_interface_deinit without deinitializing ctrl-iface */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002945 for (j = 0; j < hapd_iface->num_bss; j++) {
2946 struct hostapd_data *hapd = hapd_iface->bss[j];
Dmitry Shmidt71757432014-06-02 13:50:35 -07002947 hostapd_bss_deinit_no_free(hapd);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002948 hostapd_free_hapd_data(hapd);
2949 }
2950
Dmitry Shmidt15907092014-03-25 10:42:57 -07002951 hostapd_deinit_driver(driver, drv_priv, hapd_iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002952
2953 /* From hostapd_cleanup_iface: These were initialized in
2954 * hostapd_setup_interface and hostapd_setup_interface_complete
2955 */
2956 hostapd_cleanup_iface_partial(hapd_iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002957
Dmitry Shmidt56052862013-10-04 10:23:25 -07002958 wpa_printf(MSG_DEBUG, "Interface %s disabled",
2959 hapd_iface->bss[0]->conf->iface);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002960 hostapd_set_state(hapd_iface, HAPD_IFACE_DISABLED);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002961 return 0;
2962}
2963
2964
2965static struct hostapd_iface *
2966hostapd_iface_alloc(struct hapd_interfaces *interfaces)
2967{
2968 struct hostapd_iface **iface, *hapd_iface;
2969
2970 iface = os_realloc_array(interfaces->iface, interfaces->count + 1,
2971 sizeof(struct hostapd_iface *));
2972 if (iface == NULL)
2973 return NULL;
2974 interfaces->iface = iface;
2975 hapd_iface = interfaces->iface[interfaces->count] =
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07002976 hostapd_alloc_iface();
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002977 if (hapd_iface == NULL) {
2978 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory for "
2979 "the interface", __func__);
2980 return NULL;
2981 }
2982 interfaces->count++;
2983 hapd_iface->interfaces = interfaces;
2984
2985 return hapd_iface;
2986}
2987
2988
2989static struct hostapd_config *
2990hostapd_config_alloc(struct hapd_interfaces *interfaces, const char *ifname,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002991 const char *ctrl_iface, const char *driver)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002992{
2993 struct hostapd_bss_config *bss;
2994 struct hostapd_config *conf;
2995
2996 /* Allocates memory for bss and conf */
2997 conf = hostapd_config_defaults();
2998 if (conf == NULL) {
2999 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory for "
3000 "configuration", __func__);
Hai Shalom74f70d42019-02-11 14:42:39 -08003001 return NULL;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003002 }
3003
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003004 if (driver) {
3005 int j;
3006
3007 for (j = 0; wpa_drivers[j]; j++) {
3008 if (os_strcmp(driver, wpa_drivers[j]->name) == 0) {
3009 conf->driver = wpa_drivers[j];
3010 goto skip;
3011 }
3012 }
3013
3014 wpa_printf(MSG_ERROR,
3015 "Invalid/unknown driver '%s' - registering the default driver",
3016 driver);
3017 }
3018
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003019 conf->driver = wpa_drivers[0];
3020 if (conf->driver == NULL) {
3021 wpa_printf(MSG_ERROR, "No driver wrappers registered!");
3022 hostapd_config_free(conf);
3023 return NULL;
3024 }
3025
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003026skip:
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003027 bss = conf->last_bss = conf->bss[0];
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003028
3029 os_strlcpy(bss->iface, ifname, sizeof(bss->iface));
3030 bss->ctrl_interface = os_strdup(ctrl_iface);
3031 if (bss->ctrl_interface == NULL) {
3032 hostapd_config_free(conf);
3033 return NULL;
3034 }
3035
3036 /* Reading configuration file skipped, will be done in SET!
3037 * From reading the configuration till the end has to be done in
3038 * SET
3039 */
3040 return conf;
3041}
3042
3043
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003044static int hostapd_data_alloc(struct hostapd_iface *hapd_iface,
3045 struct hostapd_config *conf)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003046{
3047 size_t i;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003048 struct hostapd_data *hapd;
3049
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003050 hapd_iface->bss = os_calloc(conf->num_bss,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003051 sizeof(struct hostapd_data *));
3052 if (hapd_iface->bss == NULL)
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003053 return -1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003054
3055 for (i = 0; i < conf->num_bss; i++) {
3056 hapd = hapd_iface->bss[i] =
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003057 hostapd_alloc_bss_data(hapd_iface, conf, conf->bss[i]);
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003058 if (hapd == NULL) {
3059 while (i > 0) {
3060 i--;
3061 os_free(hapd_iface->bss[i]);
3062 hapd_iface->bss[i] = NULL;
3063 }
3064 os_free(hapd_iface->bss);
3065 hapd_iface->bss = NULL;
3066 return -1;
3067 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003068 hapd->msg_ctx = hapd;
3069 }
3070
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003071 hapd_iface->conf = conf;
3072 hapd_iface->num_bss = conf->num_bss;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003073
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003074 return 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003075}
3076
3077
3078int hostapd_add_iface(struct hapd_interfaces *interfaces, char *buf)
3079{
3080 struct hostapd_config *conf = NULL;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003081 struct hostapd_iface *hapd_iface = NULL, *new_iface = NULL;
3082 struct hostapd_data *hapd;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003083 char *ptr;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003084 size_t i, j;
3085 const char *conf_file = NULL, *phy_name = NULL;
3086
3087 if (os_strncmp(buf, "bss_config=", 11) == 0) {
3088 char *pos;
3089 phy_name = buf + 11;
3090 pos = os_strchr(phy_name, ':');
3091 if (!pos)
3092 return -1;
3093 *pos++ = '\0';
3094 conf_file = pos;
3095 if (!os_strlen(conf_file))
3096 return -1;
3097
3098 hapd_iface = hostapd_interface_init_bss(interfaces, phy_name,
3099 conf_file, 0);
3100 if (!hapd_iface)
3101 return -1;
3102 for (j = 0; j < interfaces->count; j++) {
3103 if (interfaces->iface[j] == hapd_iface)
3104 break;
3105 }
3106 if (j == interfaces->count) {
3107 struct hostapd_iface **tmp;
3108 tmp = os_realloc_array(interfaces->iface,
3109 interfaces->count + 1,
3110 sizeof(struct hostapd_iface *));
3111 if (!tmp) {
3112 hostapd_interface_deinit_free(hapd_iface);
3113 return -1;
3114 }
3115 interfaces->iface = tmp;
3116 interfaces->iface[interfaces->count++] = hapd_iface;
3117 new_iface = hapd_iface;
3118 }
3119
3120 if (new_iface) {
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003121 if (interfaces->driver_init(hapd_iface))
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003122 goto fail;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003123
3124 if (hostapd_setup_interface(hapd_iface)) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003125 hostapd_deinit_driver(
3126 hapd_iface->bss[0]->driver,
3127 hapd_iface->bss[0]->drv_priv,
3128 hapd_iface);
3129 goto fail;
3130 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003131 } else {
3132 /* Assign new BSS with bss[0]'s driver info */
3133 hapd = hapd_iface->bss[hapd_iface->num_bss - 1];
3134 hapd->driver = hapd_iface->bss[0]->driver;
3135 hapd->drv_priv = hapd_iface->bss[0]->drv_priv;
3136 os_memcpy(hapd->own_addr, hapd_iface->bss[0]->own_addr,
3137 ETH_ALEN);
3138
3139 if (start_ctrl_iface_bss(hapd) < 0 ||
Dmitry Shmidt54605472013-11-08 11:10:19 -08003140 (hapd_iface->state == HAPD_IFACE_ENABLED &&
Sunil Ravi77d572f2023-01-17 23:58:31 +00003141 hostapd_setup_bss(hapd, -1, true))) {
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07003142 hostapd_cleanup(hapd);
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08003143 hapd_iface->bss[hapd_iface->num_bss - 1] = NULL;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003144 hapd_iface->conf->num_bss--;
3145 hapd_iface->num_bss--;
Dmitry Shmidt54605472013-11-08 11:10:19 -08003146 wpa_printf(MSG_DEBUG, "%s: free hapd %p %s",
3147 __func__, hapd, hapd->conf->iface);
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003148 hostapd_config_free_bss(hapd->conf);
3149 hapd->conf = NULL;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003150 os_free(hapd);
3151 return -1;
3152 }
3153 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003154 hostapd_owe_update_trans(hapd_iface);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003155 return 0;
3156 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003157
3158 ptr = os_strchr(buf, ' ');
3159 if (ptr == NULL)
3160 return -1;
3161 *ptr++ = '\0';
3162
Dmitry Shmidt56052862013-10-04 10:23:25 -07003163 if (os_strncmp(ptr, "config=", 7) == 0)
3164 conf_file = ptr + 7;
3165
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003166 for (i = 0; i < interfaces->count; i++) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003167 if (!os_strcmp(interfaces->iface[i]->conf->bss[0]->iface,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003168 buf)) {
3169 wpa_printf(MSG_INFO, "Cannot add interface - it "
3170 "already exists");
3171 return -1;
3172 }
3173 }
3174
3175 hapd_iface = hostapd_iface_alloc(interfaces);
3176 if (hapd_iface == NULL) {
3177 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
3178 "for interface", __func__);
3179 goto fail;
3180 }
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003181 new_iface = hapd_iface;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003182
Dmitry Shmidt56052862013-10-04 10:23:25 -07003183 if (conf_file && interfaces->config_read_cb) {
3184 conf = interfaces->config_read_cb(conf_file);
3185 if (conf && conf->bss)
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003186 os_strlcpy(conf->bss[0]->iface, buf,
3187 sizeof(conf->bss[0]->iface));
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003188 } else {
3189 char *driver = os_strchr(ptr, ' ');
3190
3191 if (driver)
3192 *driver++ = '\0';
3193 conf = hostapd_config_alloc(interfaces, buf, ptr, driver);
3194 }
3195
Dmitry Shmidt56052862013-10-04 10:23:25 -07003196 if (conf == NULL || conf->bss == NULL) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003197 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
3198 "for configuration", __func__);
3199 goto fail;
3200 }
3201
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003202 if (hostapd_data_alloc(hapd_iface, conf) < 0) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003203 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
3204 "for hostapd", __func__);
3205 goto fail;
3206 }
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003207 conf = NULL;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003208
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003209 if (start_ctrl_iface(hapd_iface) < 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003210 goto fail;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003211
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003212 wpa_printf(MSG_INFO, "Add interface '%s'",
3213 hapd_iface->conf->bss[0]->iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003214
3215 return 0;
3216
3217fail:
3218 if (conf)
3219 hostapd_config_free(conf);
3220 if (hapd_iface) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003221 if (hapd_iface->bss) {
Dmitry Shmidt54605472013-11-08 11:10:19 -08003222 for (i = 0; i < hapd_iface->num_bss; i++) {
3223 hapd = hapd_iface->bss[i];
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08003224 if (!hapd)
3225 continue;
3226 if (hapd_iface->interfaces &&
Dmitry Shmidt54605472013-11-08 11:10:19 -08003227 hapd_iface->interfaces->ctrl_iface_deinit)
3228 hapd_iface->interfaces->
3229 ctrl_iface_deinit(hapd);
3230 wpa_printf(MSG_DEBUG, "%s: free hapd %p (%s)",
3231 __func__, hapd_iface->bss[i],
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08003232 hapd->conf->iface);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003233 hostapd_cleanup(hapd);
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08003234 os_free(hapd);
3235 hapd_iface->bss[i] = NULL;
Dmitry Shmidt54605472013-11-08 11:10:19 -08003236 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003237 os_free(hapd_iface->bss);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003238 hapd_iface->bss = NULL;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003239 }
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003240 if (new_iface) {
3241 interfaces->count--;
3242 interfaces->iface[interfaces->count] = NULL;
3243 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003244 hostapd_cleanup_iface(hapd_iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003245 }
3246 return -1;
3247}
3248
3249
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003250static int hostapd_remove_bss(struct hostapd_iface *iface, unsigned int idx)
3251{
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003252 size_t i;
3253
Dmitry Shmidt54605472013-11-08 11:10:19 -08003254 wpa_printf(MSG_INFO, "Remove BSS '%s'", iface->conf->bss[idx]->iface);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003255
Dmitry Shmidt54605472013-11-08 11:10:19 -08003256 /* Remove hostapd_data only if it has already been initialized */
3257 if (idx < iface->num_bss) {
3258 struct hostapd_data *hapd = iface->bss[idx];
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003259
Dmitry Shmidt54605472013-11-08 11:10:19 -08003260 hostapd_bss_deinit(hapd);
3261 wpa_printf(MSG_DEBUG, "%s: free hapd %p (%s)",
3262 __func__, hapd, hapd->conf->iface);
3263 hostapd_config_free_bss(hapd->conf);
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003264 hapd->conf = NULL;
Dmitry Shmidt54605472013-11-08 11:10:19 -08003265 os_free(hapd);
3266
3267 iface->num_bss--;
3268
3269 for (i = idx; i < iface->num_bss; i++)
3270 iface->bss[i] = iface->bss[i + 1];
3271 } else {
3272 hostapd_config_free_bss(iface->conf->bss[idx]);
3273 iface->conf->bss[idx] = NULL;
3274 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003275
3276 iface->conf->num_bss--;
Dmitry Shmidt54605472013-11-08 11:10:19 -08003277 for (i = idx; i < iface->conf->num_bss; i++)
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003278 iface->conf->bss[i] = iface->conf->bss[i + 1];
3279
3280 return 0;
3281}
3282
3283
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003284int hostapd_remove_iface(struct hapd_interfaces *interfaces, char *buf)
3285{
3286 struct hostapd_iface *hapd_iface;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003287 size_t i, j, k = 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003288
3289 for (i = 0; i < interfaces->count; i++) {
3290 hapd_iface = interfaces->iface[i];
3291 if (hapd_iface == NULL)
3292 return -1;
Dmitry Shmidt54605472013-11-08 11:10:19 -08003293 if (!os_strcmp(hapd_iface->conf->bss[0]->iface, buf)) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003294 wpa_printf(MSG_INFO, "Remove interface '%s'", buf);
Dmitry Shmidta38abf92014-03-06 13:38:44 -08003295 hapd_iface->driver_ap_teardown =
3296 !!(hapd_iface->drv_flags &
3297 WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
3298
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003299 hostapd_interface_deinit_free(hapd_iface);
3300 k = i;
3301 while (k < (interfaces->count - 1)) {
3302 interfaces->iface[k] =
3303 interfaces->iface[k + 1];
3304 k++;
3305 }
3306 interfaces->count--;
3307 return 0;
3308 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003309
3310 for (j = 0; j < hapd_iface->conf->num_bss; j++) {
Dmitry Shmidta38abf92014-03-06 13:38:44 -08003311 if (!os_strcmp(hapd_iface->conf->bss[j]->iface, buf)) {
3312 hapd_iface->driver_ap_teardown =
3313 !(hapd_iface->drv_flags &
3314 WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003315 return hostapd_remove_bss(hapd_iface, j);
Dmitry Shmidta38abf92014-03-06 13:38:44 -08003316 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003317 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003318 }
3319 return -1;
3320}
3321
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003322
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003323/**
3324 * hostapd_new_assoc_sta - Notify that a new station associated with the AP
3325 * @hapd: Pointer to BSS data
3326 * @sta: Pointer to the associated STA data
3327 * @reassoc: 1 to indicate this was a re-association; 0 = first association
3328 *
3329 * This function will be called whenever a station associates with the AP. It
3330 * can be called from ieee802_11.c for drivers that export MLME to hostapd and
3331 * from drv_callbacks.c based on driver events for drivers that take care of
3332 * management frames (IEEE 802.11 authentication and association) internally.
3333 */
3334void hostapd_new_assoc_sta(struct hostapd_data *hapd, struct sta_info *sta,
3335 int reassoc)
3336{
3337 if (hapd->tkip_countermeasures) {
3338 hostapd_drv_sta_deauth(hapd, sta->addr,
3339 WLAN_REASON_MICHAEL_MIC_FAILURE);
3340 return;
3341 }
3342
3343 hostapd_prune_associations(hapd, sta->addr);
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -08003344 ap_sta_clear_disconnect_timeouts(hapd, sta);
Hai Shalom899fcc72020-10-19 14:38:18 -07003345 sta->post_csa_sa_query = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003346
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003347#ifdef CONFIG_P2P
3348 if (sta->p2p_ie == NULL && !sta->no_p2p_set) {
3349 sta->no_p2p_set = 1;
3350 hapd->num_sta_no_p2p++;
3351 if (hapd->num_sta_no_p2p == 1)
3352 hostapd_p2p_non_p2p_sta_connected(hapd);
3353 }
3354#endif /* CONFIG_P2P */
3355
Hai Shalom81f62d82019-07-22 12:10:00 -07003356 airtime_policy_new_sta(hapd, sta);
3357
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003358 /* Start accounting here, if IEEE 802.1X and WPA are not used.
3359 * IEEE 802.1X/WPA code will start accounting after the station has
3360 * been authorized. */
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -08003361 if (!hapd->conf->ieee802_1x && !hapd->conf->wpa && !hapd->conf->osen) {
3362 ap_sta_set_authorized(hapd, sta, 1);
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08003363 os_get_reltime(&sta->connected_time);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003364 accounting_sta_start(hapd, sta);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003365 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003366
3367 /* Start IEEE 802.1X authentication process for new stations */
3368 ieee802_1x_new_station(hapd, sta);
3369 if (reassoc) {
3370 if (sta->auth_alg != WLAN_AUTH_FT &&
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003371 sta->auth_alg != WLAN_AUTH_FILS_SK &&
3372 sta->auth_alg != WLAN_AUTH_FILS_SK_PFS &&
3373 sta->auth_alg != WLAN_AUTH_FILS_PK &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003374 !(sta->flags & (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS)))
3375 wpa_auth_sm_event(sta->wpa_sm, WPA_REAUTH);
3376 } else
3377 wpa_auth_sta_associated(hapd->wpa_auth, sta->wpa_sm);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003378
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08003379 if (hapd->iface->drv_flags & WPA_DRIVER_FLAGS_WIRED) {
3380 if (eloop_cancel_timeout(ap_handle_timer, hapd, sta) > 0) {
3381 wpa_printf(MSG_DEBUG,
3382 "%s: %s: canceled wired ap_handle_timer timeout for "
3383 MACSTR,
3384 hapd->conf->iface, __func__,
3385 MAC2STR(sta->addr));
3386 }
3387 } else if (!(hapd->iface->drv_flags &
3388 WPA_DRIVER_FLAGS_INACTIVITY_TIMER)) {
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -08003389 wpa_printf(MSG_DEBUG,
3390 "%s: %s: reschedule ap_handle_timer timeout for "
3391 MACSTR " (%d seconds - ap_max_inactivity)",
3392 hapd->conf->iface, __func__, MAC2STR(sta->addr),
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08003393 hapd->conf->ap_max_inactivity);
3394 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
3395 eloop_register_timeout(hapd->conf->ap_max_inactivity, 0,
3396 ap_handle_timer, hapd, sta);
3397 }
Hai Shalom81f62d82019-07-22 12:10:00 -07003398
3399#ifdef CONFIG_MACSEC
3400 if (hapd->conf->wpa_key_mgmt == WPA_KEY_MGMT_NONE &&
3401 hapd->conf->mka_psk_set)
3402 ieee802_1x_create_preshared_mka_hapd(hapd, sta);
3403 else
3404 ieee802_1x_alloc_kay_sm_hapd(hapd, sta);
3405#endif /* CONFIG_MACSEC */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003406}
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003407
3408
3409const char * hostapd_state_text(enum hostapd_iface_state s)
3410{
3411 switch (s) {
3412 case HAPD_IFACE_UNINITIALIZED:
3413 return "UNINITIALIZED";
3414 case HAPD_IFACE_DISABLED:
3415 return "DISABLED";
3416 case HAPD_IFACE_COUNTRY_UPDATE:
3417 return "COUNTRY_UPDATE";
3418 case HAPD_IFACE_ACS:
3419 return "ACS";
3420 case HAPD_IFACE_HT_SCAN:
3421 return "HT_SCAN";
3422 case HAPD_IFACE_DFS:
3423 return "DFS";
3424 case HAPD_IFACE_ENABLED:
3425 return "ENABLED";
3426 }
3427
3428 return "UNKNOWN";
3429}
3430
3431
3432void hostapd_set_state(struct hostapd_iface *iface, enum hostapd_iface_state s)
3433{
3434 wpa_printf(MSG_INFO, "%s: interface state %s->%s",
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07003435 iface->conf ? iface->conf->bss[0]->iface : "N/A",
3436 hostapd_state_text(iface->state), hostapd_state_text(s));
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003437 iface->state = s;
3438}
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003439
3440
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003441int hostapd_csa_in_progress(struct hostapd_iface *iface)
3442{
3443 unsigned int i;
3444
3445 for (i = 0; i < iface->num_bss; i++)
3446 if (iface->bss[i]->csa_in_progress)
3447 return 1;
3448 return 0;
3449}
3450
3451
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003452#ifdef NEED_AP_MLME
3453
3454static void free_beacon_data(struct beacon_data *beacon)
3455{
3456 os_free(beacon->head);
3457 beacon->head = NULL;
3458 os_free(beacon->tail);
3459 beacon->tail = NULL;
3460 os_free(beacon->probe_resp);
3461 beacon->probe_resp = NULL;
3462 os_free(beacon->beacon_ies);
3463 beacon->beacon_ies = NULL;
3464 os_free(beacon->proberesp_ies);
3465 beacon->proberesp_ies = NULL;
3466 os_free(beacon->assocresp_ies);
3467 beacon->assocresp_ies = NULL;
3468}
3469
3470
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003471static int hostapd_build_beacon_data(struct hostapd_data *hapd,
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003472 struct beacon_data *beacon)
3473{
3474 struct wpabuf *beacon_extra, *proberesp_extra, *assocresp_extra;
3475 struct wpa_driver_ap_params params;
3476 int ret;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003477
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08003478 os_memset(beacon, 0, sizeof(*beacon));
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003479 ret = ieee802_11_build_ap_params(hapd, &params);
3480 if (ret < 0)
3481 return ret;
3482
3483 ret = hostapd_build_ap_extra_ies(hapd, &beacon_extra,
3484 &proberesp_extra,
3485 &assocresp_extra);
3486 if (ret)
3487 goto free_ap_params;
3488
3489 ret = -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003490 beacon->head = os_memdup(params.head, params.head_len);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003491 if (!beacon->head)
3492 goto free_ap_extra_ies;
3493
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003494 beacon->head_len = params.head_len;
3495
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003496 beacon->tail = os_memdup(params.tail, params.tail_len);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003497 if (!beacon->tail)
3498 goto free_beacon;
3499
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003500 beacon->tail_len = params.tail_len;
3501
3502 if (params.proberesp != NULL) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003503 beacon->probe_resp = os_memdup(params.proberesp,
3504 params.proberesp_len);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003505 if (!beacon->probe_resp)
3506 goto free_beacon;
3507
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003508 beacon->probe_resp_len = params.proberesp_len;
3509 }
3510
3511 /* copy the extra ies */
3512 if (beacon_extra) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003513 beacon->beacon_ies = os_memdup(beacon_extra->buf,
3514 wpabuf_len(beacon_extra));
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003515 if (!beacon->beacon_ies)
3516 goto free_beacon;
3517
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003518 beacon->beacon_ies_len = wpabuf_len(beacon_extra);
3519 }
3520
3521 if (proberesp_extra) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003522 beacon->proberesp_ies = os_memdup(proberesp_extra->buf,
3523 wpabuf_len(proberesp_extra));
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003524 if (!beacon->proberesp_ies)
3525 goto free_beacon;
3526
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003527 beacon->proberesp_ies_len = wpabuf_len(proberesp_extra);
3528 }
3529
3530 if (assocresp_extra) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003531 beacon->assocresp_ies = os_memdup(assocresp_extra->buf,
3532 wpabuf_len(assocresp_extra));
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003533 if (!beacon->assocresp_ies)
3534 goto free_beacon;
3535
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003536 beacon->assocresp_ies_len = wpabuf_len(assocresp_extra);
3537 }
3538
3539 ret = 0;
3540free_beacon:
3541 /* if the function fails, the caller should not free beacon data */
3542 if (ret)
3543 free_beacon_data(beacon);
3544
3545free_ap_extra_ies:
3546 hostapd_free_ap_extra_ies(hapd, beacon_extra, proberesp_extra,
3547 assocresp_extra);
3548free_ap_params:
3549 ieee802_11_free_ap_params(&params);
3550 return ret;
3551}
3552
3553
3554/*
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003555 * TODO: This flow currently supports only changing channel and width within
3556 * the same hw_mode. Any other changes to MAC parameters or provided settings
3557 * are not supported.
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003558 */
3559static int hostapd_change_config_freq(struct hostapd_data *hapd,
3560 struct hostapd_config *conf,
3561 struct hostapd_freq_params *params,
3562 struct hostapd_freq_params *old_params)
3563{
3564 int channel;
Hai Shalom81f62d82019-07-22 12:10:00 -07003565 u8 seg0, seg1;
3566 struct hostapd_hw_modes *mode;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003567
3568 if (!params->channel) {
3569 /* check if the new channel is supported by hw */
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003570 params->channel = hostapd_hw_get_channel(hapd, params->freq);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003571 }
3572
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003573 channel = params->channel;
3574 if (!channel)
3575 return -1;
3576
Hai Shalom81f62d82019-07-22 12:10:00 -07003577 mode = hapd->iface->current_mode;
3578
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003579 /* if a pointer to old_params is provided we save previous state */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003580 if (old_params &&
3581 hostapd_set_freq_params(old_params, conf->hw_mode,
3582 hostapd_hw_get_freq(hapd, conf->channel),
Hai Shalomc3565922019-10-28 11:58:20 -07003583 conf->channel, conf->enable_edmg,
3584 conf->edmg_channel, conf->ieee80211n,
Hai Shalom81f62d82019-07-22 12:10:00 -07003585 conf->ieee80211ac, conf->ieee80211ax,
Sunil Ravia04bd252022-05-02 22:54:18 -07003586 conf->ieee80211be, conf->secondary_channel,
Hai Shalom81f62d82019-07-22 12:10:00 -07003587 hostapd_get_oper_chwidth(conf),
3588 hostapd_get_oper_centr_freq_seg0_idx(conf),
3589 hostapd_get_oper_centr_freq_seg1_idx(conf),
3590 conf->vht_capab,
3591 mode ? &mode->he_capab[IEEE80211_MODE_AP] :
Sunil Ravia04bd252022-05-02 22:54:18 -07003592 NULL,
3593 mode ? &mode->eht_capab[IEEE80211_MODE_AP] :
Hai Shalom81f62d82019-07-22 12:10:00 -07003594 NULL))
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003595 return -1;
3596
3597 switch (params->bandwidth) {
3598 case 0:
3599 case 20:
Hai Shaloma20dcd72022-02-04 13:43:00 -08003600 conf->ht_capab &= ~HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET;
3601 break;
3602 case 40:
3603 case 80:
3604 case 160:
3605 conf->ht_capab |= HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET;
3606 break;
3607 default:
3608 return -1;
3609 }
3610
3611 switch (params->bandwidth) {
3612 case 0:
3613 case 20:
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003614 case 40:
Sunil8cd6f4d2022-06-28 18:40:46 +00003615 hostapd_set_oper_chwidth(conf, CONF_OPER_CHWIDTH_USE_HT);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003616 break;
3617 case 80:
3618 if (params->center_freq2)
Sunil8cd6f4d2022-06-28 18:40:46 +00003619 hostapd_set_oper_chwidth(conf,
3620 CONF_OPER_CHWIDTH_80P80MHZ);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003621 else
Sunil8cd6f4d2022-06-28 18:40:46 +00003622 hostapd_set_oper_chwidth(conf,
3623 CONF_OPER_CHWIDTH_80MHZ);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003624 break;
3625 case 160:
Sunil8cd6f4d2022-06-28 18:40:46 +00003626 hostapd_set_oper_chwidth(conf, CONF_OPER_CHWIDTH_160MHZ);
3627 break;
3628 case 320:
3629 hostapd_set_oper_chwidth(conf, CONF_OPER_CHWIDTH_320MHZ);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003630 break;
3631 default:
3632 return -1;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003633 }
3634
3635 conf->channel = channel;
3636 conf->ieee80211n = params->ht_enabled;
Hai Shaloma20dcd72022-02-04 13:43:00 -08003637 conf->ieee80211ac = params->vht_enabled;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003638 conf->secondary_channel = params->sec_channel_offset;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003639 ieee80211_freq_to_chan(params->center_freq1,
Hai Shalom81f62d82019-07-22 12:10:00 -07003640 &seg0);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003641 ieee80211_freq_to_chan(params->center_freq2,
Hai Shalom81f62d82019-07-22 12:10:00 -07003642 &seg1);
3643 hostapd_set_oper_centr_freq_seg0_idx(conf, seg0);
3644 hostapd_set_oper_centr_freq_seg1_idx(conf, seg1);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003645
3646 /* TODO: maybe call here hostapd_config_check here? */
3647
3648 return 0;
3649}
3650
3651
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003652static int hostapd_fill_csa_settings(struct hostapd_data *hapd,
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003653 struct csa_settings *settings)
3654{
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003655 struct hostapd_iface *iface = hapd->iface;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003656 struct hostapd_freq_params old_freq;
3657 int ret;
Sunil Ravi036cec52023-03-29 11:35:17 -07003658#ifdef CONFIG_IEEE80211BE
3659 u16 old_punct_bitmap;
3660#endif /* CONFIG_IEEE80211BE */
Hai Shalom81f62d82019-07-22 12:10:00 -07003661 u8 chan, bandwidth;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003662
3663 os_memset(&old_freq, 0, sizeof(old_freq));
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003664 if (!iface || !iface->freq || hapd->csa_in_progress)
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003665 return -1;
3666
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003667 switch (settings->freq_params.bandwidth) {
3668 case 80:
3669 if (settings->freq_params.center_freq2)
Sunil8cd6f4d2022-06-28 18:40:46 +00003670 bandwidth = CONF_OPER_CHWIDTH_80P80MHZ;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003671 else
Sunil8cd6f4d2022-06-28 18:40:46 +00003672 bandwidth = CONF_OPER_CHWIDTH_80MHZ;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003673 break;
3674 case 160:
Sunil8cd6f4d2022-06-28 18:40:46 +00003675 bandwidth = CONF_OPER_CHWIDTH_160MHZ;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003676 break;
3677 default:
Sunil8cd6f4d2022-06-28 18:40:46 +00003678 bandwidth = CONF_OPER_CHWIDTH_USE_HT;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003679 break;
3680 }
3681
3682 if (ieee80211_freq_to_channel_ext(
3683 settings->freq_params.freq,
3684 settings->freq_params.sec_channel_offset,
Hai Shalom81f62d82019-07-22 12:10:00 -07003685 bandwidth,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003686 &hapd->iface->cs_oper_class,
3687 &chan) == NUM_HOSTAPD_MODES) {
3688 wpa_printf(MSG_DEBUG,
Sunil Ravia04bd252022-05-02 22:54:18 -07003689 "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 -08003690 settings->freq_params.freq,
3691 settings->freq_params.sec_channel_offset,
Hai Shalom81f62d82019-07-22 12:10:00 -07003692 settings->freq_params.vht_enabled,
Sunil Ravia04bd252022-05-02 22:54:18 -07003693 settings->freq_params.he_enabled,
3694 settings->freq_params.eht_enabled);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003695 return -1;
3696 }
3697
3698 settings->freq_params.channel = chan;
3699
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003700 ret = hostapd_change_config_freq(iface->bss[0], iface->conf,
3701 &settings->freq_params,
3702 &old_freq);
3703 if (ret)
3704 return ret;
3705
Sunil Ravi036cec52023-03-29 11:35:17 -07003706#ifdef CONFIG_IEEE80211BE
3707 old_punct_bitmap = iface->conf->punct_bitmap;
3708 iface->conf->punct_bitmap = settings->punct_bitmap;
3709#endif /* CONFIG_IEEE80211BE */
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003710 ret = hostapd_build_beacon_data(hapd, &settings->beacon_after);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003711
3712 /* change back the configuration */
Sunil Ravi036cec52023-03-29 11:35:17 -07003713#ifdef CONFIG_IEEE80211BE
3714 iface->conf->punct_bitmap = old_punct_bitmap;
3715#endif /* CONFIG_IEEE80211BE */
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003716 hostapd_change_config_freq(iface->bss[0], iface->conf,
3717 &old_freq, NULL);
3718
3719 if (ret)
3720 return ret;
3721
3722 /* set channel switch parameters for csa ie */
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003723 hapd->cs_freq_params = settings->freq_params;
3724 hapd->cs_count = settings->cs_count;
3725 hapd->cs_block_tx = settings->block_tx;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003726
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003727 ret = hostapd_build_beacon_data(hapd, &settings->beacon_csa);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003728 if (ret) {
3729 free_beacon_data(&settings->beacon_after);
3730 return ret;
3731 }
3732
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003733 settings->counter_offset_beacon[0] = hapd->cs_c_off_beacon;
3734 settings->counter_offset_presp[0] = hapd->cs_c_off_proberesp;
3735 settings->counter_offset_beacon[1] = hapd->cs_c_off_ecsa_beacon;
3736 settings->counter_offset_presp[1] = hapd->cs_c_off_ecsa_proberesp;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003737
3738 return 0;
3739}
3740
3741
3742void hostapd_cleanup_cs_params(struct hostapd_data *hapd)
3743{
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003744 os_memset(&hapd->cs_freq_params, 0, sizeof(hapd->cs_freq_params));
3745 hapd->cs_count = 0;
3746 hapd->cs_block_tx = 0;
3747 hapd->cs_c_off_beacon = 0;
3748 hapd->cs_c_off_proberesp = 0;
3749 hapd->csa_in_progress = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003750 hapd->cs_c_off_ecsa_beacon = 0;
3751 hapd->cs_c_off_ecsa_proberesp = 0;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003752}
3753
3754
Hai Shalom60840252021-02-19 19:02:11 -08003755void hostapd_chan_switch_config(struct hostapd_data *hapd,
3756 struct hostapd_freq_params *freq_params)
Roshan Pius3a1667e2018-07-03 15:17:14 -07003757{
Sunil Ravia04bd252022-05-02 22:54:18 -07003758 if (freq_params->eht_enabled)
3759 hapd->iconf->ch_switch_eht_config |= CH_SWITCH_EHT_ENABLED;
3760 else
3761 hapd->iconf->ch_switch_eht_config |= CH_SWITCH_EHT_DISABLED;
3762
Hai Shalom60840252021-02-19 19:02:11 -08003763 if (freq_params->he_enabled)
3764 hapd->iconf->ch_switch_he_config |= CH_SWITCH_HE_ENABLED;
3765 else
3766 hapd->iconf->ch_switch_he_config |= CH_SWITCH_HE_DISABLED;
3767
3768 if (freq_params->vht_enabled)
Roshan Pius3a1667e2018-07-03 15:17:14 -07003769 hapd->iconf->ch_switch_vht_config |= CH_SWITCH_VHT_ENABLED;
3770 else
3771 hapd->iconf->ch_switch_vht_config |= CH_SWITCH_VHT_DISABLED;
3772
3773 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
Hai Shalom60840252021-02-19 19:02:11 -08003774 HOSTAPD_LEVEL_INFO,
Sunil Ravia04bd252022-05-02 22:54:18 -07003775 "CHAN_SWITCH EHT config 0x%x HE config 0x%x VHT config 0x%x",
3776 hapd->iconf->ch_switch_eht_config,
Hai Shalom60840252021-02-19 19:02:11 -08003777 hapd->iconf->ch_switch_he_config,
Roshan Pius3a1667e2018-07-03 15:17:14 -07003778 hapd->iconf->ch_switch_vht_config);
3779}
3780
3781
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003782int hostapd_switch_channel(struct hostapd_data *hapd,
3783 struct csa_settings *settings)
3784{
3785 int ret;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003786
3787 if (!(hapd->iface->drv_flags & WPA_DRIVER_FLAGS_AP_CSA)) {
3788 wpa_printf(MSG_INFO, "CSA is not supported");
3789 return -1;
3790 }
3791
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003792 ret = hostapd_fill_csa_settings(hapd, settings);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003793 if (ret)
3794 return ret;
3795
3796 ret = hostapd_drv_switch_channel(hapd, settings);
3797 free_beacon_data(&settings->beacon_csa);
3798 free_beacon_data(&settings->beacon_after);
3799
3800 if (ret) {
3801 /* if we failed, clean cs parameters */
3802 hostapd_cleanup_cs_params(hapd);
3803 return ret;
3804 }
3805
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003806 hapd->csa_in_progress = 1;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003807 return 0;
3808}
3809
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003810
3811void
3812hostapd_switch_channel_fallback(struct hostapd_iface *iface,
3813 const struct hostapd_freq_params *freq_params)
3814{
Sunil8cd6f4d2022-06-28 18:40:46 +00003815 int seg0_idx = 0, seg1_idx = 0;
3816 enum oper_chan_width bw = CONF_OPER_CHWIDTH_USE_HT;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003817
3818 wpa_printf(MSG_DEBUG, "Restarting all CSA-related BSSes");
3819
3820 if (freq_params->center_freq1)
Hai Shalom81f62d82019-07-22 12:10:00 -07003821 seg0_idx = 36 + (freq_params->center_freq1 - 5180) / 5;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003822 if (freq_params->center_freq2)
Hai Shalom81f62d82019-07-22 12:10:00 -07003823 seg1_idx = 36 + (freq_params->center_freq2 - 5180) / 5;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003824
3825 switch (freq_params->bandwidth) {
3826 case 0:
3827 case 20:
3828 case 40:
Sunil8cd6f4d2022-06-28 18:40:46 +00003829 bw = CONF_OPER_CHWIDTH_USE_HT;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003830 break;
3831 case 80:
3832 if (freq_params->center_freq2)
Sunil8cd6f4d2022-06-28 18:40:46 +00003833 bw = CONF_OPER_CHWIDTH_80P80MHZ;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003834 else
Sunil8cd6f4d2022-06-28 18:40:46 +00003835 bw = CONF_OPER_CHWIDTH_80MHZ;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003836 break;
3837 case 160:
Sunil8cd6f4d2022-06-28 18:40:46 +00003838 bw = CONF_OPER_CHWIDTH_160MHZ;
3839 break;
3840 case 320:
3841 bw = CONF_OPER_CHWIDTH_320MHZ;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003842 break;
3843 default:
3844 wpa_printf(MSG_WARNING, "Unknown CSA bandwidth: %d",
3845 freq_params->bandwidth);
3846 break;
3847 }
3848
3849 iface->freq = freq_params->freq;
3850 iface->conf->channel = freq_params->channel;
3851 iface->conf->secondary_channel = freq_params->sec_channel_offset;
Hai Shalom81f62d82019-07-22 12:10:00 -07003852 hostapd_set_oper_centr_freq_seg0_idx(iface->conf, seg0_idx);
3853 hostapd_set_oper_centr_freq_seg1_idx(iface->conf, seg1_idx);
3854 hostapd_set_oper_chwidth(iface->conf, bw);
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003855 iface->conf->ieee80211n = freq_params->ht_enabled;
3856 iface->conf->ieee80211ac = freq_params->vht_enabled;
Hai Shalom81f62d82019-07-22 12:10:00 -07003857 iface->conf->ieee80211ax = freq_params->he_enabled;
Sunil Ravia04bd252022-05-02 22:54:18 -07003858 iface->conf->ieee80211be = freq_params->eht_enabled;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003859
3860 /*
3861 * cs_params must not be cleared earlier because the freq_params
3862 * argument may actually point to one of these.
Hai Shalom39ba6fc2019-01-22 12:40:38 -08003863 * These params will be cleared during interface disable below.
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003864 */
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003865 hostapd_disable_iface(iface);
3866 hostapd_enable_iface(iface);
3867}
3868
Sunil Ravia04bd252022-05-02 22:54:18 -07003869
3870#ifdef CONFIG_IEEE80211AX
3871
3872void hostapd_cleanup_cca_params(struct hostapd_data *hapd)
3873{
3874 hapd->cca_count = 0;
3875 hapd->cca_color = 0;
3876 hapd->cca_c_off_beacon = 0;
3877 hapd->cca_c_off_proberesp = 0;
3878 hapd->cca_in_progress = false;
3879}
3880
3881
3882static int hostapd_fill_cca_settings(struct hostapd_data *hapd,
3883 struct cca_settings *settings)
3884{
3885 struct hostapd_iface *iface = hapd->iface;
3886 u8 old_color;
3887 int ret;
3888
3889 if (!iface || iface->conf->he_op.he_bss_color_disabled)
3890 return -1;
3891
3892 old_color = iface->conf->he_op.he_bss_color;
3893 iface->conf->he_op.he_bss_color = hapd->cca_color;
3894 ret = hostapd_build_beacon_data(hapd, &settings->beacon_after);
3895 if (ret)
3896 return ret;
3897
3898 iface->conf->he_op.he_bss_color = old_color;
3899
3900 settings->cca_count = hapd->cca_count;
3901 settings->cca_color = hapd->cca_color,
3902 hapd->cca_in_progress = true;
3903
3904 ret = hostapd_build_beacon_data(hapd, &settings->beacon_cca);
3905 if (ret) {
3906 free_beacon_data(&settings->beacon_after);
3907 return ret;
3908 }
3909
3910 settings->counter_offset_beacon = hapd->cca_c_off_beacon;
3911 settings->counter_offset_presp = hapd->cca_c_off_proberesp;
3912
3913 return 0;
3914}
3915
3916
3917static void hostapd_switch_color_timeout_handler(void *eloop_data,
3918 void *user_ctx)
3919{
3920 struct hostapd_data *hapd = (struct hostapd_data *) eloop_data;
3921 os_time_t delta_t;
3922 unsigned int b;
3923 int i, r;
3924
3925 /* CCA can be triggered once the handler constantly receives
3926 * color collision events to for at least
3927 * DOT11BSS_COLOR_COLLISION_AP_PERIOD (50 s by default). */
3928 delta_t = hapd->last_color_collision.sec -
3929 hapd->first_color_collision.sec;
3930 if (delta_t < DOT11BSS_COLOR_COLLISION_AP_PERIOD)
3931 return;
3932
3933 r = os_random() % HE_OPERATION_BSS_COLOR_MAX;
3934 for (i = 0; i < HE_OPERATION_BSS_COLOR_MAX; i++) {
Sunil8cd6f4d2022-06-28 18:40:46 +00003935 if (r && !(hapd->color_collision_bitmap & (1ULL << r)))
Sunil Ravia04bd252022-05-02 22:54:18 -07003936 break;
3937
3938 r = (r + 1) % HE_OPERATION_BSS_COLOR_MAX;
3939 }
3940
3941 if (i == HE_OPERATION_BSS_COLOR_MAX) {
3942 /* There are no free colors so turn BSS coloring off */
3943 wpa_printf(MSG_INFO,
3944 "No free colors left, turning off BSS coloring");
3945 hapd->iface->conf->he_op.he_bss_color_disabled = 1;
3946 hapd->iface->conf->he_op.he_bss_color = os_random() % 63 + 1;
3947 for (b = 0; b < hapd->iface->num_bss; b++)
3948 ieee802_11_set_beacon(hapd->iface->bss[b]);
3949 return;
3950 }
3951
3952 for (b = 0; b < hapd->iface->num_bss; b++) {
3953 struct hostapd_data *bss = hapd->iface->bss[b];
3954 struct cca_settings settings;
3955 int ret;
3956
3957 hostapd_cleanup_cca_params(bss);
3958 bss->cca_color = r;
3959 bss->cca_count = 10;
3960
3961 if (hostapd_fill_cca_settings(bss, &settings)) {
3962 hostapd_cleanup_cca_params(bss);
3963 continue;
3964 }
3965
3966 ret = hostapd_drv_switch_color(bss, &settings);
3967 if (ret)
3968 hostapd_cleanup_cca_params(bss);
3969
3970 free_beacon_data(&settings.beacon_cca);
3971 free_beacon_data(&settings.beacon_after);
3972 }
3973}
3974
3975
3976void hostapd_switch_color(struct hostapd_data *hapd, u64 bitmap)
3977{
3978 struct os_reltime now;
3979
3980 if (hapd->cca_in_progress)
3981 return;
3982
3983 if (os_get_reltime(&now))
3984 return;
3985
3986 hapd->color_collision_bitmap = bitmap;
3987 hapd->last_color_collision = now;
3988
3989 if (eloop_is_timeout_registered(hostapd_switch_color_timeout_handler,
3990 hapd, NULL))
3991 return;
3992
3993 hapd->first_color_collision = now;
3994 /* 10 s window as margin for persistent color collision reporting */
3995 eloop_register_timeout(DOT11BSS_COLOR_COLLISION_AP_PERIOD + 10, 0,
3996 hostapd_switch_color_timeout_handler,
3997 hapd, NULL);
3998}
3999
4000#endif /* CONFIG_IEEE80211AX */
4001
Dmitry Shmidte4663042016-04-04 10:07:49 -07004002#endif /* NEED_AP_MLME */
4003
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004004
4005struct hostapd_data * hostapd_get_iface(struct hapd_interfaces *interfaces,
4006 const char *ifname)
4007{
4008 size_t i, j;
4009
4010 for (i = 0; i < interfaces->count; i++) {
4011 struct hostapd_iface *iface = interfaces->iface[i];
4012
4013 for (j = 0; j < iface->num_bss; j++) {
4014 struct hostapd_data *hapd = iface->bss[j];
4015
4016 if (os_strcmp(ifname, hapd->conf->iface) == 0)
4017 return hapd;
4018 }
4019 }
4020
4021 return NULL;
4022}
4023
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004024
4025void hostapd_periodic_iface(struct hostapd_iface *iface)
4026{
4027 size_t i;
4028
4029 ap_list_timer(iface);
4030
4031 for (i = 0; i < iface->num_bss; i++) {
4032 struct hostapd_data *hapd = iface->bss[i];
4033
4034 if (!hapd->started)
4035 continue;
4036
4037#ifndef CONFIG_NO_RADIUS
4038 hostapd_acl_expire(hapd);
4039#endif /* CONFIG_NO_RADIUS */
4040 }
4041}
Hai Shalom899fcc72020-10-19 14:38:18 -07004042
4043
4044#ifdef CONFIG_OCV
4045void hostapd_ocv_check_csa_sa_query(void *eloop_ctx, void *timeout_ctx)
4046{
4047 struct hostapd_data *hapd = eloop_ctx;
4048 struct sta_info *sta;
4049
4050 wpa_printf(MSG_DEBUG, "OCV: Post-CSA SA Query initiation check");
4051
4052 for (sta = hapd->sta_list; sta; sta = sta->next) {
4053 if (!sta->post_csa_sa_query)
4054 continue;
4055
4056 wpa_printf(MSG_DEBUG, "OCV: OCVC STA " MACSTR
4057 " did not start SA Query after CSA - disconnect",
4058 MAC2STR(sta->addr));
4059 ap_sta_disconnect(hapd, sta, sta->addr,
4060 WLAN_REASON_PREV_AUTH_NOT_VALID);
4061 }
4062}
4063#endif /* CONFIG_OCV */