blob: 112e6fad312312a7e298eb94f5b7b9c06b5dc251 [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
Sunil Ravi640215c2023-06-28 23:08:09 +00001479 if (start_beacon && hostapd_start_beacon(hapd, flush_old_stations) < 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001480 return -1;
1481
Sunil Ravi640215c2023-06-28 23:08:09 +00001482 if (hapd->wpa_auth && wpa_init_keys(hapd->wpa_auth) < 0)
1483 return -1;
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{
Sunil Ravi640215c2023-06-28 23:08:09 +00001759 int bw;
Hai Shaloma20dcd72022-02-04 13:43:00 -08001760
1761 if (!is_6ghz_op_class(iface->conf->op_class))
1762 return;
1763
Sunil Ravi640215c2023-06-28 23:08:09 +00001764 bw = op_class_to_bandwidth(iface->conf->op_class);
Hai Shaloma20dcd72022-02-04 13:43:00 -08001765 /* Assign the secondary channel if absent in config for
1766 * bandwidths > 20 MHz */
Sunil Ravi640215c2023-06-28 23:08:09 +00001767 if (bw >= 40 && !iface->conf->secondary_channel) {
Hai Shaloma20dcd72022-02-04 13:43:00 -08001768 if (((iface->conf->channel - 1) / 4) % 2)
1769 iface->conf->secondary_channel = -1;
1770 else
1771 iface->conf->secondary_channel = 1;
1772 }
1773}
1774
1775
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001776static int setup_interface2(struct hostapd_iface *iface)
1777{
1778 iface->wait_channel_update = 0;
1779
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001780 if (hostapd_get_hw_features(iface)) {
1781 /* Not all drivers support this yet, so continue without hw
1782 * feature data. */
1783 } else {
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001784 int ret;
1785
Sunil Ravi036cec52023-03-29 11:35:17 -07001786 if (iface->conf->acs) {
1787 iface->freq = 0;
1788 iface->conf->channel = 0;
1789 }
1790
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001791 ret = configured_fixed_chan_to_freq(iface);
1792 if (ret < 0)
1793 goto fail;
1794
1795 if (iface->conf->op_class) {
Sunil8cd6f4d2022-06-28 18:40:46 +00001796 enum oper_chan_width ch_width;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001797
1798 ch_width = op_class_to_ch_width(iface->conf->op_class);
1799 hostapd_set_oper_chwidth(iface->conf, ch_width);
Hai Shaloma20dcd72022-02-04 13:43:00 -08001800 hostapd_set_6ghz_sec_chan(iface);
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001801 }
1802
1803 ret = hostapd_select_hw_mode(iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001804 if (ret < 0) {
1805 wpa_printf(MSG_ERROR, "Could not select hw_mode and "
1806 "channel. (%d)", ret);
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001807 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001808 }
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001809 if (ret == 1) {
1810 wpa_printf(MSG_DEBUG, "Interface initialization will be completed in a callback (ACS)");
1811 return 0;
1812 }
Hai Shalomc3565922019-10-28 11:58:20 -07001813 ret = hostapd_check_edmg_capab(iface);
1814 if (ret < 0)
1815 goto fail;
Hai Shalom60840252021-02-19 19:02:11 -08001816 ret = hostapd_check_he_6ghz_capab(iface);
1817 if (ret < 0)
1818 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001819 ret = hostapd_check_ht_capab(iface);
1820 if (ret < 0)
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001821 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001822 if (ret == 1) {
1823 wpa_printf(MSG_DEBUG, "Interface initialization will "
1824 "be completed in a callback");
1825 return 0;
1826 }
Dmitry Shmidt051af732013-10-22 13:52:46 -07001827
1828 if (iface->conf->ieee80211h)
1829 wpa_printf(MSG_DEBUG, "DFS support is enabled");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001830 }
1831 return hostapd_setup_interface_complete(iface, 0);
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001832
1833fail:
1834 hostapd_set_state(iface, HAPD_IFACE_DISABLED);
1835 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
1836 if (iface->interfaces && iface->interfaces->terminate_on_error)
1837 eloop_terminate();
1838 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001839}
1840
1841
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001842#ifdef CONFIG_FST
1843
1844static const u8 * fst_hostapd_get_bssid_cb(void *ctx)
1845{
1846 struct hostapd_data *hapd = ctx;
1847
1848 return hapd->own_addr;
1849}
1850
1851
1852static void fst_hostapd_get_channel_info_cb(void *ctx,
1853 enum hostapd_hw_mode *hw_mode,
1854 u8 *channel)
1855{
1856 struct hostapd_data *hapd = ctx;
1857
1858 *hw_mode = ieee80211_freq_to_chan(hapd->iface->freq, channel);
1859}
1860
1861
Sunil8cd6f4d2022-06-28 18:40:46 +00001862static int fst_hostapd_get_hw_modes_cb(void *ctx,
1863 struct hostapd_hw_modes **modes)
1864{
1865 struct hostapd_data *hapd = ctx;
1866
1867 *modes = hapd->iface->hw_features;
1868 return hapd->iface->num_hw_features;
1869}
1870
1871
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001872static void fst_hostapd_set_ies_cb(void *ctx, const struct wpabuf *fst_ies)
1873{
1874 struct hostapd_data *hapd = ctx;
1875
1876 if (hapd->iface->fst_ies != fst_ies) {
1877 hapd->iface->fst_ies = fst_ies;
1878 if (ieee802_11_set_beacon(hapd))
1879 wpa_printf(MSG_WARNING, "FST: Cannot set beacon");
1880 }
1881}
1882
1883
1884static int fst_hostapd_send_action_cb(void *ctx, const u8 *da,
1885 struct wpabuf *buf)
1886{
1887 struct hostapd_data *hapd = ctx;
1888
1889 return hostapd_drv_send_action(hapd, hapd->iface->freq, 0, da,
1890 wpabuf_head(buf), wpabuf_len(buf));
1891}
1892
1893
1894static const struct wpabuf * fst_hostapd_get_mb_ie_cb(void *ctx, const u8 *addr)
1895{
1896 struct hostapd_data *hapd = ctx;
1897 struct sta_info *sta = ap_get_sta(hapd, addr);
1898
1899 return sta ? sta->mb_ies : NULL;
1900}
1901
1902
1903static void fst_hostapd_update_mb_ie_cb(void *ctx, const u8 *addr,
1904 const u8 *buf, size_t size)
1905{
1906 struct hostapd_data *hapd = ctx;
1907 struct sta_info *sta = ap_get_sta(hapd, addr);
1908
1909 if (sta) {
1910 struct mb_ies_info info;
1911
1912 if (!mb_ies_info_by_ies(&info, buf, size)) {
1913 wpabuf_free(sta->mb_ies);
1914 sta->mb_ies = mb_ies_by_info(&info);
1915 }
1916 }
1917}
1918
1919
1920static const u8 * fst_hostapd_get_sta(struct fst_get_peer_ctx **get_ctx,
Hai Shalome21d4e82020-04-29 16:34:06 -07001921 bool mb_only)
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001922{
1923 struct sta_info *s = (struct sta_info *) *get_ctx;
1924
1925 if (mb_only) {
1926 for (; s && !s->mb_ies; s = s->next)
1927 ;
1928 }
1929
1930 if (s) {
1931 *get_ctx = (struct fst_get_peer_ctx *) s->next;
1932
1933 return s->addr;
1934 }
1935
1936 *get_ctx = NULL;
1937 return NULL;
1938}
1939
1940
1941static const u8 * fst_hostapd_get_peer_first(void *ctx,
1942 struct fst_get_peer_ctx **get_ctx,
Hai Shalome21d4e82020-04-29 16:34:06 -07001943 bool mb_only)
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001944{
1945 struct hostapd_data *hapd = ctx;
1946
1947 *get_ctx = (struct fst_get_peer_ctx *) hapd->sta_list;
1948
1949 return fst_hostapd_get_sta(get_ctx, mb_only);
1950}
1951
1952
1953static const u8 * fst_hostapd_get_peer_next(void *ctx,
1954 struct fst_get_peer_ctx **get_ctx,
Hai Shalome21d4e82020-04-29 16:34:06 -07001955 bool mb_only)
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001956{
1957 return fst_hostapd_get_sta(get_ctx, mb_only);
1958}
1959
1960
1961void fst_hostapd_fill_iface_obj(struct hostapd_data *hapd,
1962 struct fst_wpa_obj *iface_obj)
1963{
Sunil8cd6f4d2022-06-28 18:40:46 +00001964 os_memset(iface_obj, 0, sizeof(*iface_obj));
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001965 iface_obj->ctx = hapd;
1966 iface_obj->get_bssid = fst_hostapd_get_bssid_cb;
1967 iface_obj->get_channel_info = fst_hostapd_get_channel_info_cb;
Sunil8cd6f4d2022-06-28 18:40:46 +00001968 iface_obj->get_hw_modes = fst_hostapd_get_hw_modes_cb;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001969 iface_obj->set_ies = fst_hostapd_set_ies_cb;
1970 iface_obj->send_action = fst_hostapd_send_action_cb;
1971 iface_obj->get_mb_ie = fst_hostapd_get_mb_ie_cb;
1972 iface_obj->update_mb_ie = fst_hostapd_update_mb_ie_cb;
1973 iface_obj->get_peer_first = fst_hostapd_get_peer_first;
1974 iface_obj->get_peer_next = fst_hostapd_get_peer_next;
1975}
1976
1977#endif /* CONFIG_FST */
1978
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001979#ifdef CONFIG_OWE
1980
1981static int hostapd_owe_iface_iter(struct hostapd_iface *iface, void *ctx)
1982{
1983 struct hostapd_data *hapd = ctx;
1984 size_t i;
1985
1986 for (i = 0; i < iface->num_bss; i++) {
1987 struct hostapd_data *bss = iface->bss[i];
1988
1989 if (os_strcmp(hapd->conf->owe_transition_ifname,
1990 bss->conf->iface) != 0)
1991 continue;
1992
1993 wpa_printf(MSG_DEBUG,
1994 "OWE: ifname=%s found transition mode ifname=%s BSSID "
1995 MACSTR " SSID %s",
1996 hapd->conf->iface, bss->conf->iface,
1997 MAC2STR(bss->own_addr),
1998 wpa_ssid_txt(bss->conf->ssid.ssid,
1999 bss->conf->ssid.ssid_len));
2000 if (!bss->conf->ssid.ssid_set || !bss->conf->ssid.ssid_len ||
2001 is_zero_ether_addr(bss->own_addr))
2002 continue;
2003
2004 os_memcpy(hapd->conf->owe_transition_bssid, bss->own_addr,
2005 ETH_ALEN);
2006 os_memcpy(hapd->conf->owe_transition_ssid,
2007 bss->conf->ssid.ssid, bss->conf->ssid.ssid_len);
2008 hapd->conf->owe_transition_ssid_len = bss->conf->ssid.ssid_len;
2009 wpa_printf(MSG_DEBUG,
2010 "OWE: Copied transition mode information");
2011 return 1;
2012 }
2013
2014 return 0;
2015}
2016
2017
2018int hostapd_owe_trans_get_info(struct hostapd_data *hapd)
2019{
2020 if (hapd->conf->owe_transition_ssid_len > 0 &&
2021 !is_zero_ether_addr(hapd->conf->owe_transition_bssid))
2022 return 0;
2023
2024 /* Find transition mode SSID/BSSID information from a BSS operated by
2025 * this hostapd instance. */
2026 if (!hapd->iface->interfaces ||
2027 !hapd->iface->interfaces->for_each_interface)
2028 return hostapd_owe_iface_iter(hapd->iface, hapd);
2029 else
2030 return hapd->iface->interfaces->for_each_interface(
2031 hapd->iface->interfaces, hostapd_owe_iface_iter, hapd);
2032}
2033
2034
2035static int hostapd_owe_iface_iter2(struct hostapd_iface *iface, void *ctx)
2036{
2037 size_t i;
2038
2039 for (i = 0; i < iface->num_bss; i++) {
2040 struct hostapd_data *bss = iface->bss[i];
2041 int res;
2042
2043 if (!bss->conf->owe_transition_ifname[0])
2044 continue;
Hai Shalom899fcc72020-10-19 14:38:18 -07002045 if (bss->iface->state != HAPD_IFACE_ENABLED) {
2046 wpa_printf(MSG_DEBUG,
2047 "OWE: Interface %s state %s - defer beacon update",
2048 bss->conf->iface,
2049 hostapd_state_text(bss->iface->state));
2050 continue;
2051 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002052 res = hostapd_owe_trans_get_info(bss);
2053 if (res == 0)
2054 continue;
2055 wpa_printf(MSG_DEBUG,
2056 "OWE: Matching transition mode interface enabled - update beacon data for %s",
2057 bss->conf->iface);
2058 ieee802_11_set_beacon(bss);
2059 }
2060
2061 return 0;
2062}
2063
2064#endif /* CONFIG_OWE */
2065
2066
2067static void hostapd_owe_update_trans(struct hostapd_iface *iface)
2068{
2069#ifdef CONFIG_OWE
2070 /* Check whether the enabled BSS can complete OWE transition mode
2071 * configuration for any pending interface. */
2072 if (!iface->interfaces ||
2073 !iface->interfaces->for_each_interface)
2074 hostapd_owe_iface_iter2(iface, NULL);
2075 else
2076 iface->interfaces->for_each_interface(
2077 iface->interfaces, hostapd_owe_iface_iter2, NULL);
2078#endif /* CONFIG_OWE */
2079}
2080
2081
Roshan Pius3a1667e2018-07-03 15:17:14 -07002082static void hostapd_interface_setup_failure_handler(void *eloop_ctx,
2083 void *timeout_ctx)
2084{
2085 struct hostapd_iface *iface = eloop_ctx;
2086 struct hostapd_data *hapd;
2087
2088 if (iface->num_bss < 1 || !iface->bss || !iface->bss[0])
2089 return;
2090 hapd = iface->bss[0];
2091 if (hapd->setup_complete_cb)
2092 hapd->setup_complete_cb(hapd->setup_complete_cb_ctx);
2093}
2094
2095
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002096static int hostapd_setup_interface_complete_sync(struct hostapd_iface *iface,
2097 int err)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002098{
2099 struct hostapd_data *hapd = iface->bss[0];
2100 size_t j;
2101 u8 *prev_addr;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002102 int delay_apply_cfg = 0;
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08002103 int res_dfs_offload = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002104
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002105 if (err)
2106 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002107
2108 wpa_printf(MSG_DEBUG, "Completing interface initialization");
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08002109 if (iface->freq) {
Dmitry Shmidt051af732013-10-22 13:52:46 -07002110#ifdef NEED_AP_MLME
2111 int res;
2112#endif /* NEED_AP_MLME */
2113
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002114 wpa_printf(MSG_DEBUG, "Mode: %s Channel: %d "
2115 "Frequency: %d MHz",
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002116 hostapd_hw_mode_txt(iface->conf->hw_mode),
2117 iface->conf->channel, iface->freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002118
Dmitry Shmidt051af732013-10-22 13:52:46 -07002119#ifdef NEED_AP_MLME
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07002120 /* Handle DFS only if it is not offloaded to the driver */
2121 if (!(iface->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD)) {
2122 /* Check DFS */
2123 res = hostapd_handle_dfs(iface);
2124 if (res <= 0) {
2125 if (res < 0)
2126 goto fail;
2127 return res;
2128 }
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08002129 } else {
2130 /* If DFS is offloaded to the driver */
2131 res_dfs_offload = hostapd_handle_dfs_offload(iface);
2132 if (res_dfs_offload <= 0) {
2133 if (res_dfs_offload < 0)
2134 goto fail;
2135 } else {
2136 wpa_printf(MSG_DEBUG,
2137 "Proceed with AP/channel setup");
2138 /*
2139 * If this is a DFS channel, move to completing
2140 * AP setup.
2141 */
2142 if (res_dfs_offload == 1)
2143 goto dfs_offload;
2144 /* Otherwise fall through. */
2145 }
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002146 }
Dmitry Shmidt051af732013-10-22 13:52:46 -07002147#endif /* NEED_AP_MLME */
2148
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002149#ifdef CONFIG_MESH
2150 if (iface->mconf != NULL) {
2151 wpa_printf(MSG_DEBUG,
2152 "%s: Mesh configuration will be applied while joining the mesh network",
2153 iface->bss[0]->conf->iface);
2154 delay_apply_cfg = 1;
2155 }
2156#endif /* CONFIG_MESH */
2157
2158 if (!delay_apply_cfg &&
2159 hostapd_set_freq(hapd, hapd->iconf->hw_mode, iface->freq,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002160 hapd->iconf->channel,
Hai Shalomc3565922019-10-28 11:58:20 -07002161 hapd->iconf->enable_edmg,
2162 hapd->iconf->edmg_channel,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002163 hapd->iconf->ieee80211n,
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002164 hapd->iconf->ieee80211ac,
Hai Shalom81f62d82019-07-22 12:10:00 -07002165 hapd->iconf->ieee80211ax,
Sunil Ravia04bd252022-05-02 22:54:18 -07002166 hapd->iconf->ieee80211be,
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002167 hapd->iconf->secondary_channel,
Hai Shalom81f62d82019-07-22 12:10:00 -07002168 hostapd_get_oper_chwidth(hapd->iconf),
2169 hostapd_get_oper_centr_freq_seg0_idx(
2170 hapd->iconf),
2171 hostapd_get_oper_centr_freq_seg1_idx(
2172 hapd->iconf))) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002173 wpa_printf(MSG_ERROR, "Could not set channel for "
2174 "kernel driver");
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002175 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002176 }
2177 }
2178
2179 if (iface->current_mode) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002180 if (hostapd_prepare_rates(iface, iface->current_mode)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002181 wpa_printf(MSG_ERROR, "Failed to prepare rates "
2182 "table.");
2183 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
2184 HOSTAPD_LEVEL_WARNING,
2185 "Failed to prepare rates table.");
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002186 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002187 }
2188 }
2189
Hai Shalom021b0b52019-04-10 11:17:58 -07002190 if (hapd->iconf->rts_threshold >= -1 &&
2191 hostapd_set_rts(hapd, hapd->iconf->rts_threshold) &&
2192 hapd->iconf->rts_threshold >= -1) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002193 wpa_printf(MSG_ERROR, "Could not set RTS threshold for "
2194 "kernel driver");
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002195 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002196 }
2197
Hai Shalom021b0b52019-04-10 11:17:58 -07002198 if (hapd->iconf->fragm_threshold >= -1 &&
2199 hostapd_set_frag(hapd, hapd->iconf->fragm_threshold) &&
2200 hapd->iconf->fragm_threshold != -1) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002201 wpa_printf(MSG_ERROR, "Could not set fragmentation threshold "
2202 "for kernel driver");
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002203 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002204 }
2205
2206 prev_addr = hapd->own_addr;
2207
2208 for (j = 0; j < iface->num_bss; j++) {
2209 hapd = iface->bss[j];
2210 if (j)
2211 os_memcpy(hapd->own_addr, prev_addr, ETH_ALEN);
Sunil Ravi77d572f2023-01-17 23:58:31 +00002212 if (hostapd_setup_bss(hapd, j == 0, !iface->conf->mbssid)) {
Hai Shalom021b0b52019-04-10 11:17:58 -07002213 for (;;) {
Dmitry Shmidt71757432014-06-02 13:50:35 -07002214 hapd = iface->bss[j];
2215 hostapd_bss_deinit_no_free(hapd);
2216 hostapd_free_hapd_data(hapd);
Hai Shalom021b0b52019-04-10 11:17:58 -07002217 if (j == 0)
2218 break;
2219 j--;
2220 }
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002221 goto fail;
Dmitry Shmidt71757432014-06-02 13:50:35 -07002222 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002223 if (is_zero_ether_addr(hapd->conf->bssid))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002224 prev_addr = hapd->own_addr;
2225 }
Sunil Ravi77d572f2023-01-17 23:58:31 +00002226
2227 if (hapd->iconf->mbssid) {
2228 for (j = 0; hapd->iconf->mbssid && j < iface->num_bss; j++) {
2229 hapd = iface->bss[j];
2230 if (hostapd_start_beacon(hapd, true)) {
2231 for (;;) {
2232 hapd = iface->bss[j];
2233 hostapd_bss_deinit_no_free(hapd);
2234 hostapd_free_hapd_data(hapd);
2235 if (j == 0)
2236 break;
2237 j--;
2238 }
2239 goto fail;
2240 }
2241 }
2242 }
2243
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002244 hapd = iface->bss[0];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002245
2246 hostapd_tx_queue_params(iface);
2247
2248 ap_list_init(iface);
2249
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07002250 hostapd_set_acl(hapd);
2251
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002252 if (hostapd_driver_commit(hapd) < 0) {
2253 wpa_printf(MSG_ERROR, "%s: Failed to commit driver "
2254 "configuration", __func__);
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002255 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002256 }
2257
Jouni Malinen87fd2792011-05-16 18:35:42 +03002258 /*
2259 * WPS UPnP module can be initialized only when the "upnp_iface" is up.
2260 * If "interface" and "upnp_iface" are the same (e.g., non-bridge
2261 * mode), the interface is up only after driver_commit, so initialize
2262 * WPS after driver_commit.
2263 */
2264 for (j = 0; j < iface->num_bss; j++) {
2265 if (hostapd_init_wps_complete(iface->bss[j]))
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002266 goto fail;
Jouni Malinen87fd2792011-05-16 18:35:42 +03002267 }
2268
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08002269 if ((iface->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
2270 !res_dfs_offload) {
2271 /*
2272 * If freq is DFS, and DFS is offloaded to the driver, then wait
2273 * for CAC to complete.
2274 */
2275 wpa_printf(MSG_DEBUG, "%s: Wait for CAC to complete", __func__);
2276 return res_dfs_offload;
2277 }
2278
2279#ifdef NEED_AP_MLME
2280dfs_offload:
2281#endif /* NEED_AP_MLME */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002282
2283#ifdef CONFIG_FST
2284 if (hapd->iconf->fst_cfg.group_id[0]) {
2285 struct fst_wpa_obj iface_obj;
2286
2287 fst_hostapd_fill_iface_obj(hapd, &iface_obj);
2288 iface->fst = fst_attach(hapd->conf->iface, hapd->own_addr,
2289 &iface_obj, &hapd->iconf->fst_cfg);
2290 if (!iface->fst) {
2291 wpa_printf(MSG_ERROR, "Could not attach to FST %s",
2292 hapd->iconf->fst_cfg.group_id);
2293 goto fail;
2294 }
2295 }
2296#endif /* CONFIG_FST */
2297
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002298 hostapd_set_state(iface, HAPD_IFACE_ENABLED);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002299 hostapd_owe_update_trans(iface);
Hai Shalom81f62d82019-07-22 12:10:00 -07002300 airtime_policy_update_init(iface);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002301 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_ENABLED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002302 if (hapd->setup_complete_cb)
2303 hapd->setup_complete_cb(hapd->setup_complete_cb_ctx);
2304
Hai Shalom60840252021-02-19 19:02:11 -08002305#ifdef CONFIG_MESH
2306 if (delay_apply_cfg && !iface->mconf) {
2307 wpa_printf(MSG_ERROR, "Error while completing mesh init");
2308 goto fail;
2309 }
2310#endif /* CONFIG_MESH */
2311
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002312 wpa_printf(MSG_DEBUG, "%s: Setup of interface done.",
2313 iface->bss[0]->conf->iface);
Dmitry Shmidtb96dad42013-11-05 10:07:29 -08002314 if (iface->interfaces && iface->interfaces->terminate_on_error > 0)
2315 iface->interfaces->terminate_on_error--;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002316
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002317 for (j = 0; j < iface->num_bss; j++)
Hai Shalom74f70d42019-02-11 14:42:39 -08002318 hostapd_neighbor_set_own_report(iface->bss[j]);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002319
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002320 return 0;
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002321
2322fail:
2323 wpa_printf(MSG_ERROR, "Interface initialization failed");
2324 hostapd_set_state(iface, HAPD_IFACE_DISABLED);
2325 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002326#ifdef CONFIG_FST
2327 if (iface->fst) {
2328 fst_detach(iface->fst);
2329 iface->fst = NULL;
2330 }
2331#endif /* CONFIG_FST */
Roshan Pius3a1667e2018-07-03 15:17:14 -07002332
2333 if (iface->interfaces && iface->interfaces->terminate_on_error) {
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002334 eloop_terminate();
Roshan Pius3a1667e2018-07-03 15:17:14 -07002335 } else if (hapd->setup_complete_cb) {
2336 /*
2337 * Calling hapd->setup_complete_cb directly may cause iface
2338 * deinitialization which may be accessed later by the caller.
2339 */
2340 eloop_register_timeout(0, 0,
2341 hostapd_interface_setup_failure_handler,
2342 iface, NULL);
2343 }
2344
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002345 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002346}
2347
2348
2349/**
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002350 * hostapd_setup_interface_complete - Complete interface setup
2351 *
2352 * This function is called when previous steps in the interface setup has been
2353 * completed. This can also start operations, e.g., DFS, that will require
2354 * additional processing before interface is ready to be enabled. Such
2355 * operations will call this function from eloop callbacks when finished.
2356 */
2357int hostapd_setup_interface_complete(struct hostapd_iface *iface, int err)
2358{
2359 struct hapd_interfaces *interfaces = iface->interfaces;
2360 struct hostapd_data *hapd = iface->bss[0];
2361 unsigned int i;
2362 int not_ready_in_sync_ifaces = 0;
2363
2364 if (!iface->need_to_start_in_sync)
2365 return hostapd_setup_interface_complete_sync(iface, err);
2366
2367 if (err) {
2368 wpa_printf(MSG_ERROR, "Interface initialization failed");
2369 hostapd_set_state(iface, HAPD_IFACE_DISABLED);
2370 iface->need_to_start_in_sync = 0;
2371 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
2372 if (interfaces && interfaces->terminate_on_error)
2373 eloop_terminate();
2374 return -1;
2375 }
2376
2377 if (iface->ready_to_start_in_sync) {
2378 /* Already in ready and waiting. should never happpen */
2379 return 0;
2380 }
2381
2382 for (i = 0; i < interfaces->count; i++) {
2383 if (interfaces->iface[i]->need_to_start_in_sync &&
2384 !interfaces->iface[i]->ready_to_start_in_sync)
2385 not_ready_in_sync_ifaces++;
2386 }
2387
2388 /*
2389 * Check if this is the last interface, if yes then start all the other
2390 * waiting interfaces. If not, add this interface to the waiting list.
2391 */
2392 if (not_ready_in_sync_ifaces > 1 && iface->state == HAPD_IFACE_DFS) {
2393 /*
2394 * If this interface went through CAC, do not synchronize, just
2395 * start immediately.
2396 */
2397 iface->need_to_start_in_sync = 0;
2398 wpa_printf(MSG_INFO,
2399 "%s: Finished CAC - bypass sync and start interface",
2400 iface->bss[0]->conf->iface);
2401 return hostapd_setup_interface_complete_sync(iface, err);
2402 }
2403
2404 if (not_ready_in_sync_ifaces > 1) {
2405 /* need to wait as there are other interfaces still coming up */
2406 iface->ready_to_start_in_sync = 1;
2407 wpa_printf(MSG_INFO,
2408 "%s: Interface waiting to sync with other interfaces",
2409 iface->bss[0]->conf->iface);
2410 return 0;
2411 }
2412
2413 wpa_printf(MSG_INFO,
2414 "%s: Last interface to sync - starting all interfaces",
2415 iface->bss[0]->conf->iface);
2416 iface->need_to_start_in_sync = 0;
2417 hostapd_setup_interface_complete_sync(iface, err);
2418 for (i = 0; i < interfaces->count; i++) {
2419 if (interfaces->iface[i]->need_to_start_in_sync &&
2420 interfaces->iface[i]->ready_to_start_in_sync) {
2421 hostapd_setup_interface_complete_sync(
2422 interfaces->iface[i], 0);
2423 /* Only once the interfaces are sync started */
2424 interfaces->iface[i]->need_to_start_in_sync = 0;
2425 }
2426 }
2427
2428 return 0;
2429}
2430
2431
2432/**
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002433 * hostapd_setup_interface - Setup of an interface
2434 * @iface: Pointer to interface data.
2435 * Returns: 0 on success, -1 on failure
2436 *
2437 * Initializes the driver interface, validates the configuration,
2438 * and sets driver parameters based on the configuration.
2439 * Flushes old stations, sets the channel, encryption,
2440 * beacons, and WDS links based on the configuration.
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002441 *
2442 * If interface setup requires more time, e.g., to perform HT co-ex scans, ACS,
2443 * or DFS operations, this function returns 0 before such operations have been
2444 * completed. The pending operations are registered into eloop and will be
2445 * completed from eloop callbacks. Those callbacks end up calling
2446 * hostapd_setup_interface_complete() once setup has been completed.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002447 */
2448int hostapd_setup_interface(struct hostapd_iface *iface)
2449{
2450 int ret;
2451
Hai Shaloma20dcd72022-02-04 13:43:00 -08002452 if (!iface->conf)
2453 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002454 ret = setup_interface(iface);
2455 if (ret) {
2456 wpa_printf(MSG_ERROR, "%s: Unable to setup interface.",
Hai Shaloma20dcd72022-02-04 13:43:00 -08002457 iface->conf->bss[0]->iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002458 return -1;
2459 }
2460
2461 return 0;
2462}
2463
2464
2465/**
2466 * hostapd_alloc_bss_data - Allocate and initialize per-BSS data
2467 * @hapd_iface: Pointer to interface data
2468 * @conf: Pointer to per-interface configuration
2469 * @bss: Pointer to per-BSS configuration for this BSS
2470 * Returns: Pointer to allocated BSS data
2471 *
2472 * This function is used to allocate per-BSS data structure. This data will be
2473 * freed after hostapd_cleanup() is called for it during interface
2474 * deinitialization.
2475 */
2476struct hostapd_data *
2477hostapd_alloc_bss_data(struct hostapd_iface *hapd_iface,
2478 struct hostapd_config *conf,
2479 struct hostapd_bss_config *bss)
2480{
2481 struct hostapd_data *hapd;
2482
2483 hapd = os_zalloc(sizeof(*hapd));
2484 if (hapd == NULL)
2485 return NULL;
2486
2487 hapd->new_assoc_sta_cb = hostapd_new_assoc_sta;
2488 hapd->iconf = conf;
2489 hapd->conf = bss;
2490 hapd->iface = hapd_iface;
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08002491 if (conf)
2492 hapd->driver = conf->driver;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002493 hapd->ctrl_sock = -1;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08002494 dl_list_init(&hapd->ctrl_dst);
Dmitry Shmidt7d175302016-09-06 13:11:34 -07002495 dl_list_init(&hapd->nr_db);
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08002496 hapd->dhcp_sock = -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002497#ifdef CONFIG_IEEE80211R_AP
2498 dl_list_init(&hapd->l2_queue);
2499 dl_list_init(&hapd->l2_oui_queue);
2500#endif /* CONFIG_IEEE80211R_AP */
Hai Shalom021b0b52019-04-10 11:17:58 -07002501#ifdef CONFIG_SAE
2502 dl_list_init(&hapd->sae_commit_queue);
2503#endif /* CONFIG_SAE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002504
2505 return hapd;
2506}
2507
2508
Dmitry Shmidt54605472013-11-08 11:10:19 -08002509static void hostapd_bss_deinit(struct hostapd_data *hapd)
2510{
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07002511 if (!hapd)
2512 return;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002513 wpa_printf(MSG_DEBUG, "%s: deinit bss %s", __func__,
Hai Shalom021b0b52019-04-10 11:17:58 -07002514 hapd->conf ? hapd->conf->iface : "N/A");
Dmitry Shmidt71757432014-06-02 13:50:35 -07002515 hostapd_bss_deinit_no_free(hapd);
Dmitry Shmidtf73259c2015-03-17 11:00:54 -07002516 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
Hai Shalomc3565922019-10-28 11:58:20 -07002517#ifdef CONFIG_SQLITE
2518 if (hapd->rad_attr_db) {
2519 sqlite3_close(hapd->rad_attr_db);
2520 hapd->rad_attr_db = NULL;
2521 }
2522#endif /* CONFIG_SQLITE */
Dmitry Shmidt54605472013-11-08 11:10:19 -08002523 hostapd_cleanup(hapd);
2524}
2525
2526
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002527void hostapd_interface_deinit(struct hostapd_iface *iface)
2528{
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002529 int j;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002530
Dmitry Shmidt54605472013-11-08 11:10:19 -08002531 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002532 if (iface == NULL)
2533 return;
2534
Dmitry Shmidtf73259c2015-03-17 11:00:54 -07002535 hostapd_set_state(iface, HAPD_IFACE_DISABLED);
2536
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002537 eloop_cancel_timeout(channel_list_update_timeout, iface, NULL);
2538 iface->wait_channel_update = 0;
2539
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002540#ifdef CONFIG_FST
2541 if (iface->fst) {
2542 fst_detach(iface->fst);
2543 iface->fst = NULL;
2544 }
2545#endif /* CONFIG_FST */
2546
Hai Shalom021b0b52019-04-10 11:17:58 -07002547 for (j = (int) iface->num_bss - 1; j >= 0; j--) {
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07002548 if (!iface->bss)
2549 break;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002550 hostapd_bss_deinit(iface->bss[j]);
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07002551 }
Roshan Pius3a1667e2018-07-03 15:17:14 -07002552
Roshan Pius3a1667e2018-07-03 15:17:14 -07002553#ifdef NEED_AP_MLME
2554 hostapd_stop_setup_timers(iface);
2555 eloop_cancel_timeout(ap_ht2040_timeout, iface, NULL);
2556#endif /* NEED_AP_MLME */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002557}
2558
2559
2560void hostapd_interface_free(struct hostapd_iface *iface)
2561{
2562 size_t j;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002563 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
2564 for (j = 0; j < iface->num_bss; j++) {
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07002565 if (!iface->bss)
2566 break;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002567 wpa_printf(MSG_DEBUG, "%s: free hapd %p",
2568 __func__, iface->bss[j]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002569 os_free(iface->bss[j]);
Dmitry Shmidt54605472013-11-08 11:10:19 -08002570 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002571 hostapd_cleanup_iface(iface);
2572}
2573
2574
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07002575struct hostapd_iface * hostapd_alloc_iface(void)
2576{
2577 struct hostapd_iface *hapd_iface;
2578
2579 hapd_iface = os_zalloc(sizeof(*hapd_iface));
2580 if (!hapd_iface)
2581 return NULL;
2582
2583 dl_list_init(&hapd_iface->sta_seen);
2584
2585 return hapd_iface;
2586}
2587
2588
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002589/**
2590 * hostapd_init - Allocate and initialize per-interface data
2591 * @config_file: Path to the configuration file
2592 * Returns: Pointer to the allocated interface data or %NULL on failure
2593 *
2594 * This function is used to allocate main data structures for per-interface
2595 * data. The allocated data buffer will be freed by calling
2596 * hostapd_cleanup_iface().
2597 */
2598struct hostapd_iface * hostapd_init(struct hapd_interfaces *interfaces,
2599 const char *config_file)
2600{
2601 struct hostapd_iface *hapd_iface = NULL;
2602 struct hostapd_config *conf = NULL;
2603 struct hostapd_data *hapd;
2604 size_t i;
2605
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07002606 hapd_iface = hostapd_alloc_iface();
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002607 if (hapd_iface == NULL)
2608 goto fail;
2609
2610 hapd_iface->config_fname = os_strdup(config_file);
2611 if (hapd_iface->config_fname == NULL)
2612 goto fail;
2613
2614 conf = interfaces->config_read_cb(hapd_iface->config_fname);
2615 if (conf == NULL)
2616 goto fail;
2617 hapd_iface->conf = conf;
2618
2619 hapd_iface->num_bss = conf->num_bss;
2620 hapd_iface->bss = os_calloc(conf->num_bss,
2621 sizeof(struct hostapd_data *));
2622 if (hapd_iface->bss == NULL)
2623 goto fail;
2624
2625 for (i = 0; i < conf->num_bss; i++) {
2626 hapd = hapd_iface->bss[i] =
2627 hostapd_alloc_bss_data(hapd_iface, conf,
2628 conf->bss[i]);
2629 if (hapd == NULL)
2630 goto fail;
2631 hapd->msg_ctx = hapd;
2632 }
2633
2634 return hapd_iface;
2635
2636fail:
2637 wpa_printf(MSG_ERROR, "Failed to set up interface with %s",
2638 config_file);
2639 if (conf)
2640 hostapd_config_free(conf);
2641 if (hapd_iface) {
2642 os_free(hapd_iface->config_fname);
2643 os_free(hapd_iface->bss);
Dmitry Shmidt54605472013-11-08 11:10:19 -08002644 wpa_printf(MSG_DEBUG, "%s: free iface %p",
2645 __func__, hapd_iface);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002646 os_free(hapd_iface);
2647 }
2648 return NULL;
2649}
2650
2651
2652static int ifname_in_use(struct hapd_interfaces *interfaces, const char *ifname)
2653{
2654 size_t i, j;
2655
2656 for (i = 0; i < interfaces->count; i++) {
2657 struct hostapd_iface *iface = interfaces->iface[i];
2658 for (j = 0; j < iface->num_bss; j++) {
2659 struct hostapd_data *hapd = iface->bss[j];
2660 if (os_strcmp(ifname, hapd->conf->iface) == 0)
2661 return 1;
2662 }
2663 }
2664
2665 return 0;
2666}
2667
2668
2669/**
2670 * hostapd_interface_init_bss - Read configuration file and init BSS data
2671 *
2672 * This function is used to parse configuration file for a BSS. This BSS is
2673 * added to an existing interface sharing the same radio (if any) or a new
2674 * interface is created if this is the first interface on a radio. This
2675 * allocate memory for the BSS. No actual driver operations are started.
2676 *
2677 * This is similar to hostapd_interface_init(), but for a case where the
2678 * configuration is used to add a single BSS instead of all BSSes for a radio.
2679 */
2680struct hostapd_iface *
2681hostapd_interface_init_bss(struct hapd_interfaces *interfaces, const char *phy,
2682 const char *config_fname, int debug)
2683{
2684 struct hostapd_iface *new_iface = NULL, *iface = NULL;
2685 struct hostapd_data *hapd;
2686 int k;
2687 size_t i, bss_idx;
2688
2689 if (!phy || !*phy)
2690 return NULL;
2691
2692 for (i = 0; i < interfaces->count; i++) {
2693 if (os_strcmp(interfaces->iface[i]->phy, phy) == 0) {
2694 iface = interfaces->iface[i];
2695 break;
2696 }
2697 }
2698
2699 wpa_printf(MSG_INFO, "Configuration file: %s (phy %s)%s",
2700 config_fname, phy, iface ? "" : " --> new PHY");
2701 if (iface) {
2702 struct hostapd_config *conf;
2703 struct hostapd_bss_config **tmp_conf;
2704 struct hostapd_data **tmp_bss;
2705 struct hostapd_bss_config *bss;
2706 const char *ifname;
2707
2708 /* Add new BSS to existing iface */
2709 conf = interfaces->config_read_cb(config_fname);
2710 if (conf == NULL)
2711 return NULL;
2712 if (conf->num_bss > 1) {
2713 wpa_printf(MSG_ERROR, "Multiple BSSes specified in BSS-config");
2714 hostapd_config_free(conf);
2715 return NULL;
2716 }
2717
2718 ifname = conf->bss[0]->iface;
2719 if (ifname[0] != '\0' && ifname_in_use(interfaces, ifname)) {
2720 wpa_printf(MSG_ERROR,
2721 "Interface name %s already in use", ifname);
2722 hostapd_config_free(conf);
2723 return NULL;
2724 }
2725
2726 tmp_conf = os_realloc_array(
2727 iface->conf->bss, iface->conf->num_bss + 1,
2728 sizeof(struct hostapd_bss_config *));
2729 tmp_bss = os_realloc_array(iface->bss, iface->num_bss + 1,
2730 sizeof(struct hostapd_data *));
2731 if (tmp_bss)
2732 iface->bss = tmp_bss;
2733 if (tmp_conf) {
2734 iface->conf->bss = tmp_conf;
2735 iface->conf->last_bss = tmp_conf[0];
2736 }
2737 if (tmp_bss == NULL || tmp_conf == NULL) {
2738 hostapd_config_free(conf);
2739 return NULL;
2740 }
2741 bss = iface->conf->bss[iface->conf->num_bss] = conf->bss[0];
2742 iface->conf->num_bss++;
2743
2744 hapd = hostapd_alloc_bss_data(iface, iface->conf, bss);
2745 if (hapd == NULL) {
2746 iface->conf->num_bss--;
2747 hostapd_config_free(conf);
2748 return NULL;
2749 }
2750 iface->conf->last_bss = bss;
2751 iface->bss[iface->num_bss] = hapd;
2752 hapd->msg_ctx = hapd;
2753
2754 bss_idx = iface->num_bss++;
2755 conf->num_bss--;
2756 conf->bss[0] = NULL;
2757 hostapd_config_free(conf);
2758 } else {
2759 /* Add a new iface with the first BSS */
2760 new_iface = iface = hostapd_init(interfaces, config_fname);
2761 if (!iface)
2762 return NULL;
2763 os_strlcpy(iface->phy, phy, sizeof(iface->phy));
2764 iface->interfaces = interfaces;
2765 bss_idx = 0;
2766 }
2767
2768 for (k = 0; k < debug; k++) {
2769 if (iface->bss[bss_idx]->conf->logger_stdout_level > 0)
2770 iface->bss[bss_idx]->conf->logger_stdout_level--;
2771 }
2772
2773 if (iface->conf->bss[bss_idx]->iface[0] == '\0' &&
2774 !hostapd_drv_none(iface->bss[bss_idx])) {
2775 wpa_printf(MSG_ERROR, "Interface name not specified in %s",
2776 config_fname);
2777 if (new_iface)
2778 hostapd_interface_deinit_free(new_iface);
2779 return NULL;
2780 }
2781
2782 return iface;
2783}
2784
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002785
2786void hostapd_interface_deinit_free(struct hostapd_iface *iface)
2787{
2788 const struct wpa_driver_ops *driver;
2789 void *drv_priv;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002790
2791 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002792 if (iface == NULL)
2793 return;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002794 wpa_printf(MSG_DEBUG, "%s: num_bss=%u conf->num_bss=%u",
2795 __func__, (unsigned int) iface->num_bss,
2796 (unsigned int) iface->conf->num_bss);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002797 driver = iface->bss[0]->driver;
2798 drv_priv = iface->bss[0]->drv_priv;
2799 hostapd_interface_deinit(iface);
Dmitry Shmidt54605472013-11-08 11:10:19 -08002800 wpa_printf(MSG_DEBUG, "%s: driver=%p drv_priv=%p -> hapd_deinit",
2801 __func__, driver, drv_priv);
Dmitry Shmidt71757432014-06-02 13:50:35 -07002802 if (driver && driver->hapd_deinit && drv_priv) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002803 driver->hapd_deinit(drv_priv);
Dmitry Shmidt71757432014-06-02 13:50:35 -07002804 iface->bss[0]->drv_priv = NULL;
2805 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002806 hostapd_interface_free(iface);
2807}
2808
2809
Dmitry Shmidt15907092014-03-25 10:42:57 -07002810static void hostapd_deinit_driver(const struct wpa_driver_ops *driver,
2811 void *drv_priv,
2812 struct hostapd_iface *hapd_iface)
2813{
2814 size_t j;
2815
2816 wpa_printf(MSG_DEBUG, "%s: driver=%p drv_priv=%p -> hapd_deinit",
2817 __func__, driver, drv_priv);
2818 if (driver && driver->hapd_deinit && drv_priv) {
2819 driver->hapd_deinit(drv_priv);
2820 for (j = 0; j < hapd_iface->num_bss; j++) {
2821 wpa_printf(MSG_DEBUG, "%s:bss[%d]->drv_priv=%p",
2822 __func__, (int) j,
2823 hapd_iface->bss[j]->drv_priv);
Hai Shalom1dc4d202019-04-29 16:22:27 -07002824 if (hapd_iface->bss[j]->drv_priv == drv_priv) {
Dmitry Shmidt15907092014-03-25 10:42:57 -07002825 hapd_iface->bss[j]->drv_priv = NULL;
Hai Shalom1dc4d202019-04-29 16:22:27 -07002826 hapd_iface->extended_capa = NULL;
2827 hapd_iface->extended_capa_mask = NULL;
2828 hapd_iface->extended_capa_len = 0;
2829 }
Dmitry Shmidt15907092014-03-25 10:42:57 -07002830 }
2831 }
2832}
2833
2834
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002835int hostapd_enable_iface(struct hostapd_iface *hapd_iface)
2836{
Dmitry Shmidt71757432014-06-02 13:50:35 -07002837 size_t j;
2838
Hai Shalom60840252021-02-19 19:02:11 -08002839 if (!hapd_iface)
2840 return -1;
2841
2842 if (hapd_iface->enable_iface_cb)
2843 return hapd_iface->enable_iface_cb(hapd_iface);
2844
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002845 if (hapd_iface->bss[0]->drv_priv != NULL) {
2846 wpa_printf(MSG_ERROR, "Interface %s already enabled",
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002847 hapd_iface->conf->bss[0]->iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002848 return -1;
2849 }
2850
2851 wpa_printf(MSG_DEBUG, "Enable interface %s",
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002852 hapd_iface->conf->bss[0]->iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002853
Dmitry Shmidt71757432014-06-02 13:50:35 -07002854 for (j = 0; j < hapd_iface->num_bss; j++)
2855 hostapd_set_security_params(hapd_iface->conf->bss[j], 1);
Dmitry Shmidt344abd32014-01-14 13:17:00 -08002856 if (hostapd_config_check(hapd_iface->conf, 1) < 0) {
2857 wpa_printf(MSG_INFO, "Invalid configuration - cannot enable");
2858 return -1;
2859 }
2860
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002861 if (hapd_iface->interfaces == NULL ||
2862 hapd_iface->interfaces->driver_init == NULL ||
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002863 hapd_iface->interfaces->driver_init(hapd_iface))
2864 return -1;
2865
2866 if (hostapd_setup_interface(hapd_iface)) {
Dmitry Shmidt15907092014-03-25 10:42:57 -07002867 hostapd_deinit_driver(hapd_iface->bss[0]->driver,
2868 hapd_iface->bss[0]->drv_priv,
2869 hapd_iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002870 return -1;
2871 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002872
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002873 return 0;
2874}
2875
2876
2877int hostapd_reload_iface(struct hostapd_iface *hapd_iface)
2878{
2879 size_t j;
2880
2881 wpa_printf(MSG_DEBUG, "Reload interface %s",
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002882 hapd_iface->conf->bss[0]->iface);
2883 for (j = 0; j < hapd_iface->num_bss; j++)
Dmitry Shmidt71757432014-06-02 13:50:35 -07002884 hostapd_set_security_params(hapd_iface->conf->bss[j], 1);
Dmitry Shmidt344abd32014-01-14 13:17:00 -08002885 if (hostapd_config_check(hapd_iface->conf, 1) < 0) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002886 wpa_printf(MSG_ERROR, "Updated configuration is invalid");
2887 return -1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002888 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002889 hostapd_clear_old(hapd_iface);
2890 for (j = 0; j < hapd_iface->num_bss; j++)
2891 hostapd_reload_bss(hapd_iface->bss[j]);
2892
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002893 return 0;
2894}
2895
2896
Sunil Ravi77d572f2023-01-17 23:58:31 +00002897int hostapd_reload_bss_only(struct hostapd_data *bss)
2898{
2899
2900 wpa_printf(MSG_DEBUG, "Reload BSS %s", bss->conf->iface);
2901 hostapd_set_security_params(bss->conf, 1);
2902 if (hostapd_config_check(bss->iconf, 1) < 0) {
2903 wpa_printf(MSG_ERROR, "Updated BSS configuration is invalid");
2904 return -1;
2905 }
2906 hostapd_clear_old_bss(bss);
2907 hostapd_reload_bss(bss);
2908 return 0;
2909}
2910
2911
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002912int hostapd_disable_iface(struct hostapd_iface *hapd_iface)
2913{
2914 size_t j;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002915 const struct wpa_driver_ops *driver;
2916 void *drv_priv;
2917
2918 if (hapd_iface == NULL)
2919 return -1;
Dmitry Shmidt71757432014-06-02 13:50:35 -07002920
Hai Shalom60840252021-02-19 19:02:11 -08002921 if (hapd_iface->disable_iface_cb)
2922 return hapd_iface->disable_iface_cb(hapd_iface);
2923
Dmitry Shmidt71757432014-06-02 13:50:35 -07002924 if (hapd_iface->bss[0]->drv_priv == NULL) {
2925 wpa_printf(MSG_INFO, "Interface %s already disabled",
2926 hapd_iface->conf->bss[0]->iface);
2927 return -1;
2928 }
2929
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002930 wpa_msg(hapd_iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002931 driver = hapd_iface->bss[0]->driver;
2932 drv_priv = hapd_iface->bss[0]->drv_priv;
2933
Dmitry Shmidta38abf92014-03-06 13:38:44 -08002934 hapd_iface->driver_ap_teardown =
2935 !!(hapd_iface->drv_flags &
2936 WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
2937
Hai Shalom39ba6fc2019-01-22 12:40:38 -08002938#ifdef NEED_AP_MLME
2939 for (j = 0; j < hapd_iface->num_bss; j++)
2940 hostapd_cleanup_cs_params(hapd_iface->bss[j]);
2941#endif /* NEED_AP_MLME */
2942
Dmitry Shmidta38abf92014-03-06 13:38:44 -08002943 /* same as hostapd_interface_deinit without deinitializing ctrl-iface */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002944 for (j = 0; j < hapd_iface->num_bss; j++) {
2945 struct hostapd_data *hapd = hapd_iface->bss[j];
Dmitry Shmidt71757432014-06-02 13:50:35 -07002946 hostapd_bss_deinit_no_free(hapd);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002947 hostapd_free_hapd_data(hapd);
2948 }
2949
Dmitry Shmidt15907092014-03-25 10:42:57 -07002950 hostapd_deinit_driver(driver, drv_priv, hapd_iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002951
2952 /* From hostapd_cleanup_iface: These were initialized in
2953 * hostapd_setup_interface and hostapd_setup_interface_complete
2954 */
2955 hostapd_cleanup_iface_partial(hapd_iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002956
Dmitry Shmidt56052862013-10-04 10:23:25 -07002957 wpa_printf(MSG_DEBUG, "Interface %s disabled",
2958 hapd_iface->bss[0]->conf->iface);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002959 hostapd_set_state(hapd_iface, HAPD_IFACE_DISABLED);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002960 return 0;
2961}
2962
2963
2964static struct hostapd_iface *
2965hostapd_iface_alloc(struct hapd_interfaces *interfaces)
2966{
2967 struct hostapd_iface **iface, *hapd_iface;
2968
2969 iface = os_realloc_array(interfaces->iface, interfaces->count + 1,
2970 sizeof(struct hostapd_iface *));
2971 if (iface == NULL)
2972 return NULL;
2973 interfaces->iface = iface;
2974 hapd_iface = interfaces->iface[interfaces->count] =
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07002975 hostapd_alloc_iface();
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002976 if (hapd_iface == NULL) {
2977 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory for "
2978 "the interface", __func__);
2979 return NULL;
2980 }
2981 interfaces->count++;
2982 hapd_iface->interfaces = interfaces;
2983
2984 return hapd_iface;
2985}
2986
2987
2988static struct hostapd_config *
2989hostapd_config_alloc(struct hapd_interfaces *interfaces, const char *ifname,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002990 const char *ctrl_iface, const char *driver)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002991{
2992 struct hostapd_bss_config *bss;
2993 struct hostapd_config *conf;
2994
2995 /* Allocates memory for bss and conf */
2996 conf = hostapd_config_defaults();
2997 if (conf == NULL) {
2998 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory for "
2999 "configuration", __func__);
Hai Shalom74f70d42019-02-11 14:42:39 -08003000 return NULL;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003001 }
3002
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003003 if (driver) {
3004 int j;
3005
3006 for (j = 0; wpa_drivers[j]; j++) {
3007 if (os_strcmp(driver, wpa_drivers[j]->name) == 0) {
3008 conf->driver = wpa_drivers[j];
3009 goto skip;
3010 }
3011 }
3012
3013 wpa_printf(MSG_ERROR,
3014 "Invalid/unknown driver '%s' - registering the default driver",
3015 driver);
3016 }
3017
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003018 conf->driver = wpa_drivers[0];
3019 if (conf->driver == NULL) {
3020 wpa_printf(MSG_ERROR, "No driver wrappers registered!");
3021 hostapd_config_free(conf);
3022 return NULL;
3023 }
3024
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003025skip:
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003026 bss = conf->last_bss = conf->bss[0];
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003027
3028 os_strlcpy(bss->iface, ifname, sizeof(bss->iface));
3029 bss->ctrl_interface = os_strdup(ctrl_iface);
3030 if (bss->ctrl_interface == NULL) {
3031 hostapd_config_free(conf);
3032 return NULL;
3033 }
3034
3035 /* Reading configuration file skipped, will be done in SET!
3036 * From reading the configuration till the end has to be done in
3037 * SET
3038 */
3039 return conf;
3040}
3041
3042
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003043static int hostapd_data_alloc(struct hostapd_iface *hapd_iface,
3044 struct hostapd_config *conf)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003045{
3046 size_t i;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003047 struct hostapd_data *hapd;
3048
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003049 hapd_iface->bss = os_calloc(conf->num_bss,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003050 sizeof(struct hostapd_data *));
3051 if (hapd_iface->bss == NULL)
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003052 return -1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003053
3054 for (i = 0; i < conf->num_bss; i++) {
3055 hapd = hapd_iface->bss[i] =
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003056 hostapd_alloc_bss_data(hapd_iface, conf, conf->bss[i]);
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003057 if (hapd == NULL) {
3058 while (i > 0) {
3059 i--;
3060 os_free(hapd_iface->bss[i]);
3061 hapd_iface->bss[i] = NULL;
3062 }
3063 os_free(hapd_iface->bss);
3064 hapd_iface->bss = NULL;
3065 return -1;
3066 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003067 hapd->msg_ctx = hapd;
3068 }
3069
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003070 hapd_iface->conf = conf;
3071 hapd_iface->num_bss = conf->num_bss;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003072
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003073 return 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003074}
3075
3076
3077int hostapd_add_iface(struct hapd_interfaces *interfaces, char *buf)
3078{
3079 struct hostapd_config *conf = NULL;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003080 struct hostapd_iface *hapd_iface = NULL, *new_iface = NULL;
3081 struct hostapd_data *hapd;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003082 char *ptr;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003083 size_t i, j;
3084 const char *conf_file = NULL, *phy_name = NULL;
3085
3086 if (os_strncmp(buf, "bss_config=", 11) == 0) {
3087 char *pos;
3088 phy_name = buf + 11;
3089 pos = os_strchr(phy_name, ':');
3090 if (!pos)
3091 return -1;
3092 *pos++ = '\0';
3093 conf_file = pos;
3094 if (!os_strlen(conf_file))
3095 return -1;
3096
3097 hapd_iface = hostapd_interface_init_bss(interfaces, phy_name,
3098 conf_file, 0);
3099 if (!hapd_iface)
3100 return -1;
3101 for (j = 0; j < interfaces->count; j++) {
3102 if (interfaces->iface[j] == hapd_iface)
3103 break;
3104 }
3105 if (j == interfaces->count) {
3106 struct hostapd_iface **tmp;
3107 tmp = os_realloc_array(interfaces->iface,
3108 interfaces->count + 1,
3109 sizeof(struct hostapd_iface *));
3110 if (!tmp) {
3111 hostapd_interface_deinit_free(hapd_iface);
3112 return -1;
3113 }
3114 interfaces->iface = tmp;
3115 interfaces->iface[interfaces->count++] = hapd_iface;
3116 new_iface = hapd_iface;
3117 }
3118
3119 if (new_iface) {
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003120 if (interfaces->driver_init(hapd_iface))
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003121 goto fail;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003122
3123 if (hostapd_setup_interface(hapd_iface)) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003124 hostapd_deinit_driver(
3125 hapd_iface->bss[0]->driver,
3126 hapd_iface->bss[0]->drv_priv,
3127 hapd_iface);
3128 goto fail;
3129 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003130 } else {
3131 /* Assign new BSS with bss[0]'s driver info */
3132 hapd = hapd_iface->bss[hapd_iface->num_bss - 1];
3133 hapd->driver = hapd_iface->bss[0]->driver;
3134 hapd->drv_priv = hapd_iface->bss[0]->drv_priv;
3135 os_memcpy(hapd->own_addr, hapd_iface->bss[0]->own_addr,
3136 ETH_ALEN);
3137
3138 if (start_ctrl_iface_bss(hapd) < 0 ||
Dmitry Shmidt54605472013-11-08 11:10:19 -08003139 (hapd_iface->state == HAPD_IFACE_ENABLED &&
Sunil Ravi77d572f2023-01-17 23:58:31 +00003140 hostapd_setup_bss(hapd, -1, true))) {
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07003141 hostapd_cleanup(hapd);
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08003142 hapd_iface->bss[hapd_iface->num_bss - 1] = NULL;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003143 hapd_iface->conf->num_bss--;
3144 hapd_iface->num_bss--;
Dmitry Shmidt54605472013-11-08 11:10:19 -08003145 wpa_printf(MSG_DEBUG, "%s: free hapd %p %s",
3146 __func__, hapd, hapd->conf->iface);
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003147 hostapd_config_free_bss(hapd->conf);
3148 hapd->conf = NULL;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003149 os_free(hapd);
3150 return -1;
3151 }
3152 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003153 hostapd_owe_update_trans(hapd_iface);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003154 return 0;
3155 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003156
3157 ptr = os_strchr(buf, ' ');
3158 if (ptr == NULL)
3159 return -1;
3160 *ptr++ = '\0';
3161
Dmitry Shmidt56052862013-10-04 10:23:25 -07003162 if (os_strncmp(ptr, "config=", 7) == 0)
3163 conf_file = ptr + 7;
3164
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003165 for (i = 0; i < interfaces->count; i++) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003166 if (!os_strcmp(interfaces->iface[i]->conf->bss[0]->iface,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003167 buf)) {
3168 wpa_printf(MSG_INFO, "Cannot add interface - it "
3169 "already exists");
3170 return -1;
3171 }
3172 }
3173
3174 hapd_iface = hostapd_iface_alloc(interfaces);
3175 if (hapd_iface == NULL) {
3176 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
3177 "for interface", __func__);
3178 goto fail;
3179 }
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003180 new_iface = hapd_iface;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003181
Dmitry Shmidt56052862013-10-04 10:23:25 -07003182 if (conf_file && interfaces->config_read_cb) {
3183 conf = interfaces->config_read_cb(conf_file);
3184 if (conf && conf->bss)
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003185 os_strlcpy(conf->bss[0]->iface, buf,
3186 sizeof(conf->bss[0]->iface));
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003187 } else {
3188 char *driver = os_strchr(ptr, ' ');
3189
3190 if (driver)
3191 *driver++ = '\0';
3192 conf = hostapd_config_alloc(interfaces, buf, ptr, driver);
3193 }
3194
Dmitry Shmidt56052862013-10-04 10:23:25 -07003195 if (conf == NULL || conf->bss == NULL) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003196 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
3197 "for configuration", __func__);
3198 goto fail;
3199 }
3200
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003201 if (hostapd_data_alloc(hapd_iface, conf) < 0) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003202 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
3203 "for hostapd", __func__);
3204 goto fail;
3205 }
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003206 conf = NULL;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003207
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003208 if (start_ctrl_iface(hapd_iface) < 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003209 goto fail;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003210
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003211 wpa_printf(MSG_INFO, "Add interface '%s'",
3212 hapd_iface->conf->bss[0]->iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003213
3214 return 0;
3215
3216fail:
3217 if (conf)
3218 hostapd_config_free(conf);
3219 if (hapd_iface) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003220 if (hapd_iface->bss) {
Dmitry Shmidt54605472013-11-08 11:10:19 -08003221 for (i = 0; i < hapd_iface->num_bss; i++) {
3222 hapd = hapd_iface->bss[i];
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08003223 if (!hapd)
3224 continue;
3225 if (hapd_iface->interfaces &&
Dmitry Shmidt54605472013-11-08 11:10:19 -08003226 hapd_iface->interfaces->ctrl_iface_deinit)
3227 hapd_iface->interfaces->
3228 ctrl_iface_deinit(hapd);
3229 wpa_printf(MSG_DEBUG, "%s: free hapd %p (%s)",
3230 __func__, hapd_iface->bss[i],
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08003231 hapd->conf->iface);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003232 hostapd_cleanup(hapd);
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08003233 os_free(hapd);
3234 hapd_iface->bss[i] = NULL;
Dmitry Shmidt54605472013-11-08 11:10:19 -08003235 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003236 os_free(hapd_iface->bss);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003237 hapd_iface->bss = NULL;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003238 }
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003239 if (new_iface) {
3240 interfaces->count--;
3241 interfaces->iface[interfaces->count] = NULL;
3242 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003243 hostapd_cleanup_iface(hapd_iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003244 }
3245 return -1;
3246}
3247
3248
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003249static int hostapd_remove_bss(struct hostapd_iface *iface, unsigned int idx)
3250{
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003251 size_t i;
3252
Dmitry Shmidt54605472013-11-08 11:10:19 -08003253 wpa_printf(MSG_INFO, "Remove BSS '%s'", iface->conf->bss[idx]->iface);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003254
Dmitry Shmidt54605472013-11-08 11:10:19 -08003255 /* Remove hostapd_data only if it has already been initialized */
3256 if (idx < iface->num_bss) {
3257 struct hostapd_data *hapd = iface->bss[idx];
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003258
Dmitry Shmidt54605472013-11-08 11:10:19 -08003259 hostapd_bss_deinit(hapd);
3260 wpa_printf(MSG_DEBUG, "%s: free hapd %p (%s)",
3261 __func__, hapd, hapd->conf->iface);
3262 hostapd_config_free_bss(hapd->conf);
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003263 hapd->conf = NULL;
Dmitry Shmidt54605472013-11-08 11:10:19 -08003264 os_free(hapd);
3265
3266 iface->num_bss--;
3267
3268 for (i = idx; i < iface->num_bss; i++)
3269 iface->bss[i] = iface->bss[i + 1];
3270 } else {
3271 hostapd_config_free_bss(iface->conf->bss[idx]);
3272 iface->conf->bss[idx] = NULL;
3273 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003274
3275 iface->conf->num_bss--;
Dmitry Shmidt54605472013-11-08 11:10:19 -08003276 for (i = idx; i < iface->conf->num_bss; i++)
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003277 iface->conf->bss[i] = iface->conf->bss[i + 1];
3278
3279 return 0;
3280}
3281
3282
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003283int hostapd_remove_iface(struct hapd_interfaces *interfaces, char *buf)
3284{
3285 struct hostapd_iface *hapd_iface;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003286 size_t i, j, k = 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003287
3288 for (i = 0; i < interfaces->count; i++) {
3289 hapd_iface = interfaces->iface[i];
3290 if (hapd_iface == NULL)
3291 return -1;
Dmitry Shmidt54605472013-11-08 11:10:19 -08003292 if (!os_strcmp(hapd_iface->conf->bss[0]->iface, buf)) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003293 wpa_printf(MSG_INFO, "Remove interface '%s'", buf);
Dmitry Shmidta38abf92014-03-06 13:38:44 -08003294 hapd_iface->driver_ap_teardown =
3295 !!(hapd_iface->drv_flags &
3296 WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
3297
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003298 hostapd_interface_deinit_free(hapd_iface);
3299 k = i;
3300 while (k < (interfaces->count - 1)) {
3301 interfaces->iface[k] =
3302 interfaces->iface[k + 1];
3303 k++;
3304 }
3305 interfaces->count--;
3306 return 0;
3307 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003308
3309 for (j = 0; j < hapd_iface->conf->num_bss; j++) {
Dmitry Shmidta38abf92014-03-06 13:38:44 -08003310 if (!os_strcmp(hapd_iface->conf->bss[j]->iface, buf)) {
3311 hapd_iface->driver_ap_teardown =
3312 !(hapd_iface->drv_flags &
3313 WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003314 return hostapd_remove_bss(hapd_iface, j);
Dmitry Shmidta38abf92014-03-06 13:38:44 -08003315 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003316 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003317 }
3318 return -1;
3319}
3320
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003321
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003322/**
3323 * hostapd_new_assoc_sta - Notify that a new station associated with the AP
3324 * @hapd: Pointer to BSS data
3325 * @sta: Pointer to the associated STA data
3326 * @reassoc: 1 to indicate this was a re-association; 0 = first association
3327 *
3328 * This function will be called whenever a station associates with the AP. It
3329 * can be called from ieee802_11.c for drivers that export MLME to hostapd and
3330 * from drv_callbacks.c based on driver events for drivers that take care of
3331 * management frames (IEEE 802.11 authentication and association) internally.
3332 */
3333void hostapd_new_assoc_sta(struct hostapd_data *hapd, struct sta_info *sta,
3334 int reassoc)
3335{
3336 if (hapd->tkip_countermeasures) {
3337 hostapd_drv_sta_deauth(hapd, sta->addr,
3338 WLAN_REASON_MICHAEL_MIC_FAILURE);
3339 return;
3340 }
3341
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -08003342 ap_sta_clear_disconnect_timeouts(hapd, sta);
Hai Shalom899fcc72020-10-19 14:38:18 -07003343 sta->post_csa_sa_query = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003344
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003345#ifdef CONFIG_P2P
3346 if (sta->p2p_ie == NULL && !sta->no_p2p_set) {
3347 sta->no_p2p_set = 1;
3348 hapd->num_sta_no_p2p++;
3349 if (hapd->num_sta_no_p2p == 1)
3350 hostapd_p2p_non_p2p_sta_connected(hapd);
3351 }
3352#endif /* CONFIG_P2P */
3353
Hai Shalom81f62d82019-07-22 12:10:00 -07003354 airtime_policy_new_sta(hapd, sta);
3355
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003356 /* Start accounting here, if IEEE 802.1X and WPA are not used.
3357 * IEEE 802.1X/WPA code will start accounting after the station has
3358 * been authorized. */
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -08003359 if (!hapd->conf->ieee802_1x && !hapd->conf->wpa && !hapd->conf->osen) {
3360 ap_sta_set_authorized(hapd, sta, 1);
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08003361 os_get_reltime(&sta->connected_time);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003362 accounting_sta_start(hapd, sta);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003363 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003364
3365 /* Start IEEE 802.1X authentication process for new stations */
3366 ieee802_1x_new_station(hapd, sta);
3367 if (reassoc) {
3368 if (sta->auth_alg != WLAN_AUTH_FT &&
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003369 sta->auth_alg != WLAN_AUTH_FILS_SK &&
3370 sta->auth_alg != WLAN_AUTH_FILS_SK_PFS &&
3371 sta->auth_alg != WLAN_AUTH_FILS_PK &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003372 !(sta->flags & (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS)))
3373 wpa_auth_sm_event(sta->wpa_sm, WPA_REAUTH);
3374 } else
3375 wpa_auth_sta_associated(hapd->wpa_auth, sta->wpa_sm);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003376
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08003377 if (hapd->iface->drv_flags & WPA_DRIVER_FLAGS_WIRED) {
3378 if (eloop_cancel_timeout(ap_handle_timer, hapd, sta) > 0) {
3379 wpa_printf(MSG_DEBUG,
3380 "%s: %s: canceled wired ap_handle_timer timeout for "
3381 MACSTR,
3382 hapd->conf->iface, __func__,
3383 MAC2STR(sta->addr));
3384 }
3385 } else if (!(hapd->iface->drv_flags &
3386 WPA_DRIVER_FLAGS_INACTIVITY_TIMER)) {
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -08003387 wpa_printf(MSG_DEBUG,
3388 "%s: %s: reschedule ap_handle_timer timeout for "
3389 MACSTR " (%d seconds - ap_max_inactivity)",
3390 hapd->conf->iface, __func__, MAC2STR(sta->addr),
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08003391 hapd->conf->ap_max_inactivity);
3392 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
3393 eloop_register_timeout(hapd->conf->ap_max_inactivity, 0,
3394 ap_handle_timer, hapd, sta);
3395 }
Hai Shalom81f62d82019-07-22 12:10:00 -07003396
3397#ifdef CONFIG_MACSEC
3398 if (hapd->conf->wpa_key_mgmt == WPA_KEY_MGMT_NONE &&
3399 hapd->conf->mka_psk_set)
3400 ieee802_1x_create_preshared_mka_hapd(hapd, sta);
3401 else
3402 ieee802_1x_alloc_kay_sm_hapd(hapd, sta);
3403#endif /* CONFIG_MACSEC */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003404}
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003405
3406
3407const char * hostapd_state_text(enum hostapd_iface_state s)
3408{
3409 switch (s) {
3410 case HAPD_IFACE_UNINITIALIZED:
3411 return "UNINITIALIZED";
3412 case HAPD_IFACE_DISABLED:
3413 return "DISABLED";
3414 case HAPD_IFACE_COUNTRY_UPDATE:
3415 return "COUNTRY_UPDATE";
3416 case HAPD_IFACE_ACS:
3417 return "ACS";
3418 case HAPD_IFACE_HT_SCAN:
3419 return "HT_SCAN";
3420 case HAPD_IFACE_DFS:
3421 return "DFS";
3422 case HAPD_IFACE_ENABLED:
3423 return "ENABLED";
3424 }
3425
3426 return "UNKNOWN";
3427}
3428
3429
3430void hostapd_set_state(struct hostapd_iface *iface, enum hostapd_iface_state s)
3431{
3432 wpa_printf(MSG_INFO, "%s: interface state %s->%s",
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07003433 iface->conf ? iface->conf->bss[0]->iface : "N/A",
3434 hostapd_state_text(iface->state), hostapd_state_text(s));
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003435 iface->state = s;
3436}
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003437
3438
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003439int hostapd_csa_in_progress(struct hostapd_iface *iface)
3440{
3441 unsigned int i;
3442
3443 for (i = 0; i < iface->num_bss; i++)
3444 if (iface->bss[i]->csa_in_progress)
3445 return 1;
3446 return 0;
3447}
3448
3449
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003450#ifdef NEED_AP_MLME
3451
3452static void free_beacon_data(struct beacon_data *beacon)
3453{
3454 os_free(beacon->head);
3455 beacon->head = NULL;
3456 os_free(beacon->tail);
3457 beacon->tail = NULL;
3458 os_free(beacon->probe_resp);
3459 beacon->probe_resp = NULL;
3460 os_free(beacon->beacon_ies);
3461 beacon->beacon_ies = NULL;
3462 os_free(beacon->proberesp_ies);
3463 beacon->proberesp_ies = NULL;
3464 os_free(beacon->assocresp_ies);
3465 beacon->assocresp_ies = NULL;
3466}
3467
3468
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003469static int hostapd_build_beacon_data(struct hostapd_data *hapd,
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003470 struct beacon_data *beacon)
3471{
3472 struct wpabuf *beacon_extra, *proberesp_extra, *assocresp_extra;
3473 struct wpa_driver_ap_params params;
3474 int ret;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003475
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08003476 os_memset(beacon, 0, sizeof(*beacon));
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003477 ret = ieee802_11_build_ap_params(hapd, &params);
3478 if (ret < 0)
3479 return ret;
3480
3481 ret = hostapd_build_ap_extra_ies(hapd, &beacon_extra,
3482 &proberesp_extra,
3483 &assocresp_extra);
3484 if (ret)
3485 goto free_ap_params;
3486
3487 ret = -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003488 beacon->head = os_memdup(params.head, params.head_len);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003489 if (!beacon->head)
3490 goto free_ap_extra_ies;
3491
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003492 beacon->head_len = params.head_len;
3493
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003494 beacon->tail = os_memdup(params.tail, params.tail_len);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003495 if (!beacon->tail)
3496 goto free_beacon;
3497
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003498 beacon->tail_len = params.tail_len;
3499
3500 if (params.proberesp != NULL) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003501 beacon->probe_resp = os_memdup(params.proberesp,
3502 params.proberesp_len);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003503 if (!beacon->probe_resp)
3504 goto free_beacon;
3505
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003506 beacon->probe_resp_len = params.proberesp_len;
3507 }
3508
3509 /* copy the extra ies */
3510 if (beacon_extra) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003511 beacon->beacon_ies = os_memdup(beacon_extra->buf,
3512 wpabuf_len(beacon_extra));
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003513 if (!beacon->beacon_ies)
3514 goto free_beacon;
3515
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003516 beacon->beacon_ies_len = wpabuf_len(beacon_extra);
3517 }
3518
3519 if (proberesp_extra) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003520 beacon->proberesp_ies = os_memdup(proberesp_extra->buf,
3521 wpabuf_len(proberesp_extra));
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003522 if (!beacon->proberesp_ies)
3523 goto free_beacon;
3524
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003525 beacon->proberesp_ies_len = wpabuf_len(proberesp_extra);
3526 }
3527
3528 if (assocresp_extra) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003529 beacon->assocresp_ies = os_memdup(assocresp_extra->buf,
3530 wpabuf_len(assocresp_extra));
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003531 if (!beacon->assocresp_ies)
3532 goto free_beacon;
3533
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003534 beacon->assocresp_ies_len = wpabuf_len(assocresp_extra);
3535 }
3536
3537 ret = 0;
3538free_beacon:
3539 /* if the function fails, the caller should not free beacon data */
3540 if (ret)
3541 free_beacon_data(beacon);
3542
3543free_ap_extra_ies:
3544 hostapd_free_ap_extra_ies(hapd, beacon_extra, proberesp_extra,
3545 assocresp_extra);
3546free_ap_params:
3547 ieee802_11_free_ap_params(&params);
3548 return ret;
3549}
3550
3551
3552/*
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003553 * TODO: This flow currently supports only changing channel and width within
3554 * the same hw_mode. Any other changes to MAC parameters or provided settings
3555 * are not supported.
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003556 */
3557static int hostapd_change_config_freq(struct hostapd_data *hapd,
3558 struct hostapd_config *conf,
3559 struct hostapd_freq_params *params,
3560 struct hostapd_freq_params *old_params)
3561{
3562 int channel;
Hai Shalom81f62d82019-07-22 12:10:00 -07003563 u8 seg0, seg1;
3564 struct hostapd_hw_modes *mode;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003565
3566 if (!params->channel) {
3567 /* check if the new channel is supported by hw */
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003568 params->channel = hostapd_hw_get_channel(hapd, params->freq);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003569 }
3570
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003571 channel = params->channel;
3572 if (!channel)
3573 return -1;
3574
Hai Shalom81f62d82019-07-22 12:10:00 -07003575 mode = hapd->iface->current_mode;
3576
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003577 /* if a pointer to old_params is provided we save previous state */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003578 if (old_params &&
3579 hostapd_set_freq_params(old_params, conf->hw_mode,
3580 hostapd_hw_get_freq(hapd, conf->channel),
Hai Shalomc3565922019-10-28 11:58:20 -07003581 conf->channel, conf->enable_edmg,
3582 conf->edmg_channel, conf->ieee80211n,
Hai Shalom81f62d82019-07-22 12:10:00 -07003583 conf->ieee80211ac, conf->ieee80211ax,
Sunil Ravia04bd252022-05-02 22:54:18 -07003584 conf->ieee80211be, conf->secondary_channel,
Hai Shalom81f62d82019-07-22 12:10:00 -07003585 hostapd_get_oper_chwidth(conf),
3586 hostapd_get_oper_centr_freq_seg0_idx(conf),
3587 hostapd_get_oper_centr_freq_seg1_idx(conf),
3588 conf->vht_capab,
3589 mode ? &mode->he_capab[IEEE80211_MODE_AP] :
Sunil Ravia04bd252022-05-02 22:54:18 -07003590 NULL,
3591 mode ? &mode->eht_capab[IEEE80211_MODE_AP] :
Hai Shalom81f62d82019-07-22 12:10:00 -07003592 NULL))
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003593 return -1;
3594
3595 switch (params->bandwidth) {
3596 case 0:
3597 case 20:
Hai Shaloma20dcd72022-02-04 13:43:00 -08003598 conf->ht_capab &= ~HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET;
3599 break;
3600 case 40:
3601 case 80:
3602 case 160:
Sunil Ravi640215c2023-06-28 23:08:09 +00003603 case 320:
Hai Shaloma20dcd72022-02-04 13:43:00 -08003604 conf->ht_capab |= HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET;
3605 break;
3606 default:
3607 return -1;
3608 }
3609
3610 switch (params->bandwidth) {
3611 case 0:
3612 case 20:
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003613 case 40:
Sunil8cd6f4d2022-06-28 18:40:46 +00003614 hostapd_set_oper_chwidth(conf, CONF_OPER_CHWIDTH_USE_HT);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003615 break;
3616 case 80:
3617 if (params->center_freq2)
Sunil8cd6f4d2022-06-28 18:40:46 +00003618 hostapd_set_oper_chwidth(conf,
3619 CONF_OPER_CHWIDTH_80P80MHZ);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003620 else
Sunil8cd6f4d2022-06-28 18:40:46 +00003621 hostapd_set_oper_chwidth(conf,
3622 CONF_OPER_CHWIDTH_80MHZ);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003623 break;
3624 case 160:
Sunil8cd6f4d2022-06-28 18:40:46 +00003625 hostapd_set_oper_chwidth(conf, CONF_OPER_CHWIDTH_160MHZ);
3626 break;
3627 case 320:
3628 hostapd_set_oper_chwidth(conf, CONF_OPER_CHWIDTH_320MHZ);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003629 break;
3630 default:
3631 return -1;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003632 }
3633
3634 conf->channel = channel;
3635 conf->ieee80211n = params->ht_enabled;
Hai Shaloma20dcd72022-02-04 13:43:00 -08003636 conf->ieee80211ac = params->vht_enabled;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003637 conf->secondary_channel = params->sec_channel_offset;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003638 ieee80211_freq_to_chan(params->center_freq1,
Hai Shalom81f62d82019-07-22 12:10:00 -07003639 &seg0);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003640 ieee80211_freq_to_chan(params->center_freq2,
Hai Shalom81f62d82019-07-22 12:10:00 -07003641 &seg1);
3642 hostapd_set_oper_centr_freq_seg0_idx(conf, seg0);
3643 hostapd_set_oper_centr_freq_seg1_idx(conf, seg1);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003644
3645 /* TODO: maybe call here hostapd_config_check here? */
3646
3647 return 0;
3648}
3649
3650
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003651static int hostapd_fill_csa_settings(struct hostapd_data *hapd,
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003652 struct csa_settings *settings)
3653{
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003654 struct hostapd_iface *iface = hapd->iface;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003655 struct hostapd_freq_params old_freq;
3656 int ret;
Sunil Ravi036cec52023-03-29 11:35:17 -07003657#ifdef CONFIG_IEEE80211BE
3658 u16 old_punct_bitmap;
3659#endif /* CONFIG_IEEE80211BE */
Hai Shalom81f62d82019-07-22 12:10:00 -07003660 u8 chan, bandwidth;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003661
3662 os_memset(&old_freq, 0, sizeof(old_freq));
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003663 if (!iface || !iface->freq || hapd->csa_in_progress)
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003664 return -1;
3665
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003666 switch (settings->freq_params.bandwidth) {
3667 case 80:
3668 if (settings->freq_params.center_freq2)
Sunil8cd6f4d2022-06-28 18:40:46 +00003669 bandwidth = CONF_OPER_CHWIDTH_80P80MHZ;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003670 else
Sunil8cd6f4d2022-06-28 18:40:46 +00003671 bandwidth = CONF_OPER_CHWIDTH_80MHZ;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003672 break;
3673 case 160:
Sunil8cd6f4d2022-06-28 18:40:46 +00003674 bandwidth = CONF_OPER_CHWIDTH_160MHZ;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003675 break;
Sunil Ravi640215c2023-06-28 23:08:09 +00003676 case 320:
3677 bandwidth = CONF_OPER_CHWIDTH_320MHZ;
3678 break;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003679 default:
Sunil8cd6f4d2022-06-28 18:40:46 +00003680 bandwidth = CONF_OPER_CHWIDTH_USE_HT;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003681 break;
3682 }
3683
3684 if (ieee80211_freq_to_channel_ext(
3685 settings->freq_params.freq,
3686 settings->freq_params.sec_channel_offset,
Hai Shalom81f62d82019-07-22 12:10:00 -07003687 bandwidth,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003688 &hapd->iface->cs_oper_class,
3689 &chan) == NUM_HOSTAPD_MODES) {
3690 wpa_printf(MSG_DEBUG,
Sunil Ravia04bd252022-05-02 22:54:18 -07003691 "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 -08003692 settings->freq_params.freq,
3693 settings->freq_params.sec_channel_offset,
Hai Shalom81f62d82019-07-22 12:10:00 -07003694 settings->freq_params.vht_enabled,
Sunil Ravia04bd252022-05-02 22:54:18 -07003695 settings->freq_params.he_enabled,
3696 settings->freq_params.eht_enabled);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003697 return -1;
3698 }
3699
3700 settings->freq_params.channel = chan;
3701
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003702 ret = hostapd_change_config_freq(iface->bss[0], iface->conf,
3703 &settings->freq_params,
3704 &old_freq);
3705 if (ret)
3706 return ret;
3707
Sunil Ravi036cec52023-03-29 11:35:17 -07003708#ifdef CONFIG_IEEE80211BE
3709 old_punct_bitmap = iface->conf->punct_bitmap;
3710 iface->conf->punct_bitmap = settings->punct_bitmap;
3711#endif /* CONFIG_IEEE80211BE */
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003712 ret = hostapd_build_beacon_data(hapd, &settings->beacon_after);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003713
3714 /* change back the configuration */
Sunil Ravi036cec52023-03-29 11:35:17 -07003715#ifdef CONFIG_IEEE80211BE
3716 iface->conf->punct_bitmap = old_punct_bitmap;
3717#endif /* CONFIG_IEEE80211BE */
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003718 hostapd_change_config_freq(iface->bss[0], iface->conf,
3719 &old_freq, NULL);
3720
3721 if (ret)
3722 return ret;
3723
3724 /* set channel switch parameters for csa ie */
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003725 hapd->cs_freq_params = settings->freq_params;
3726 hapd->cs_count = settings->cs_count;
3727 hapd->cs_block_tx = settings->block_tx;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003728
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003729 ret = hostapd_build_beacon_data(hapd, &settings->beacon_csa);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003730 if (ret) {
3731 free_beacon_data(&settings->beacon_after);
3732 return ret;
3733 }
3734
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003735 settings->counter_offset_beacon[0] = hapd->cs_c_off_beacon;
3736 settings->counter_offset_presp[0] = hapd->cs_c_off_proberesp;
3737 settings->counter_offset_beacon[1] = hapd->cs_c_off_ecsa_beacon;
3738 settings->counter_offset_presp[1] = hapd->cs_c_off_ecsa_proberesp;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003739
3740 return 0;
3741}
3742
3743
3744void hostapd_cleanup_cs_params(struct hostapd_data *hapd)
3745{
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003746 os_memset(&hapd->cs_freq_params, 0, sizeof(hapd->cs_freq_params));
3747 hapd->cs_count = 0;
3748 hapd->cs_block_tx = 0;
3749 hapd->cs_c_off_beacon = 0;
3750 hapd->cs_c_off_proberesp = 0;
3751 hapd->csa_in_progress = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003752 hapd->cs_c_off_ecsa_beacon = 0;
3753 hapd->cs_c_off_ecsa_proberesp = 0;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003754}
3755
3756
Hai Shalom60840252021-02-19 19:02:11 -08003757void hostapd_chan_switch_config(struct hostapd_data *hapd,
3758 struct hostapd_freq_params *freq_params)
Roshan Pius3a1667e2018-07-03 15:17:14 -07003759{
Sunil Ravia04bd252022-05-02 22:54:18 -07003760 if (freq_params->eht_enabled)
3761 hapd->iconf->ch_switch_eht_config |= CH_SWITCH_EHT_ENABLED;
3762 else
3763 hapd->iconf->ch_switch_eht_config |= CH_SWITCH_EHT_DISABLED;
3764
Hai Shalom60840252021-02-19 19:02:11 -08003765 if (freq_params->he_enabled)
3766 hapd->iconf->ch_switch_he_config |= CH_SWITCH_HE_ENABLED;
3767 else
3768 hapd->iconf->ch_switch_he_config |= CH_SWITCH_HE_DISABLED;
3769
3770 if (freq_params->vht_enabled)
Roshan Pius3a1667e2018-07-03 15:17:14 -07003771 hapd->iconf->ch_switch_vht_config |= CH_SWITCH_VHT_ENABLED;
3772 else
3773 hapd->iconf->ch_switch_vht_config |= CH_SWITCH_VHT_DISABLED;
3774
3775 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
Hai Shalom60840252021-02-19 19:02:11 -08003776 HOSTAPD_LEVEL_INFO,
Sunil Ravia04bd252022-05-02 22:54:18 -07003777 "CHAN_SWITCH EHT config 0x%x HE config 0x%x VHT config 0x%x",
3778 hapd->iconf->ch_switch_eht_config,
Hai Shalom60840252021-02-19 19:02:11 -08003779 hapd->iconf->ch_switch_he_config,
Roshan Pius3a1667e2018-07-03 15:17:14 -07003780 hapd->iconf->ch_switch_vht_config);
3781}
3782
3783
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003784int hostapd_switch_channel(struct hostapd_data *hapd,
3785 struct csa_settings *settings)
3786{
3787 int ret;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003788
3789 if (!(hapd->iface->drv_flags & WPA_DRIVER_FLAGS_AP_CSA)) {
3790 wpa_printf(MSG_INFO, "CSA is not supported");
3791 return -1;
3792 }
3793
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003794 ret = hostapd_fill_csa_settings(hapd, settings);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003795 if (ret)
3796 return ret;
3797
3798 ret = hostapd_drv_switch_channel(hapd, settings);
3799 free_beacon_data(&settings->beacon_csa);
3800 free_beacon_data(&settings->beacon_after);
3801
3802 if (ret) {
3803 /* if we failed, clean cs parameters */
3804 hostapd_cleanup_cs_params(hapd);
3805 return ret;
3806 }
3807
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003808 hapd->csa_in_progress = 1;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003809 return 0;
3810}
3811
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003812
3813void
3814hostapd_switch_channel_fallback(struct hostapd_iface *iface,
3815 const struct hostapd_freq_params *freq_params)
3816{
Sunil8cd6f4d2022-06-28 18:40:46 +00003817 int seg0_idx = 0, seg1_idx = 0;
3818 enum oper_chan_width bw = CONF_OPER_CHWIDTH_USE_HT;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003819
3820 wpa_printf(MSG_DEBUG, "Restarting all CSA-related BSSes");
3821
3822 if (freq_params->center_freq1)
Hai Shalom81f62d82019-07-22 12:10:00 -07003823 seg0_idx = 36 + (freq_params->center_freq1 - 5180) / 5;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003824 if (freq_params->center_freq2)
Hai Shalom81f62d82019-07-22 12:10:00 -07003825 seg1_idx = 36 + (freq_params->center_freq2 - 5180) / 5;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003826
3827 switch (freq_params->bandwidth) {
3828 case 0:
3829 case 20:
3830 case 40:
Sunil8cd6f4d2022-06-28 18:40:46 +00003831 bw = CONF_OPER_CHWIDTH_USE_HT;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003832 break;
3833 case 80:
3834 if (freq_params->center_freq2)
Sunil8cd6f4d2022-06-28 18:40:46 +00003835 bw = CONF_OPER_CHWIDTH_80P80MHZ;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003836 else
Sunil8cd6f4d2022-06-28 18:40:46 +00003837 bw = CONF_OPER_CHWIDTH_80MHZ;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003838 break;
3839 case 160:
Sunil8cd6f4d2022-06-28 18:40:46 +00003840 bw = CONF_OPER_CHWIDTH_160MHZ;
3841 break;
3842 case 320:
3843 bw = CONF_OPER_CHWIDTH_320MHZ;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003844 break;
3845 default:
3846 wpa_printf(MSG_WARNING, "Unknown CSA bandwidth: %d",
3847 freq_params->bandwidth);
3848 break;
3849 }
3850
3851 iface->freq = freq_params->freq;
3852 iface->conf->channel = freq_params->channel;
3853 iface->conf->secondary_channel = freq_params->sec_channel_offset;
Hai Shalom81f62d82019-07-22 12:10:00 -07003854 hostapd_set_oper_centr_freq_seg0_idx(iface->conf, seg0_idx);
3855 hostapd_set_oper_centr_freq_seg1_idx(iface->conf, seg1_idx);
3856 hostapd_set_oper_chwidth(iface->conf, bw);
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003857 iface->conf->ieee80211n = freq_params->ht_enabled;
3858 iface->conf->ieee80211ac = freq_params->vht_enabled;
Hai Shalom81f62d82019-07-22 12:10:00 -07003859 iface->conf->ieee80211ax = freq_params->he_enabled;
Sunil Ravia04bd252022-05-02 22:54:18 -07003860 iface->conf->ieee80211be = freq_params->eht_enabled;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003861
3862 /*
3863 * cs_params must not be cleared earlier because the freq_params
3864 * argument may actually point to one of these.
Hai Shalom39ba6fc2019-01-22 12:40:38 -08003865 * These params will be cleared during interface disable below.
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003866 */
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003867 hostapd_disable_iface(iface);
3868 hostapd_enable_iface(iface);
3869}
3870
Sunil Ravia04bd252022-05-02 22:54:18 -07003871
3872#ifdef CONFIG_IEEE80211AX
3873
3874void hostapd_cleanup_cca_params(struct hostapd_data *hapd)
3875{
3876 hapd->cca_count = 0;
3877 hapd->cca_color = 0;
3878 hapd->cca_c_off_beacon = 0;
3879 hapd->cca_c_off_proberesp = 0;
3880 hapd->cca_in_progress = false;
3881}
3882
3883
3884static int hostapd_fill_cca_settings(struct hostapd_data *hapd,
3885 struct cca_settings *settings)
3886{
3887 struct hostapd_iface *iface = hapd->iface;
3888 u8 old_color;
3889 int ret;
3890
3891 if (!iface || iface->conf->he_op.he_bss_color_disabled)
3892 return -1;
3893
3894 old_color = iface->conf->he_op.he_bss_color;
3895 iface->conf->he_op.he_bss_color = hapd->cca_color;
3896 ret = hostapd_build_beacon_data(hapd, &settings->beacon_after);
3897 if (ret)
3898 return ret;
3899
3900 iface->conf->he_op.he_bss_color = old_color;
3901
3902 settings->cca_count = hapd->cca_count;
3903 settings->cca_color = hapd->cca_color,
3904 hapd->cca_in_progress = true;
3905
3906 ret = hostapd_build_beacon_data(hapd, &settings->beacon_cca);
3907 if (ret) {
3908 free_beacon_data(&settings->beacon_after);
3909 return ret;
3910 }
3911
3912 settings->counter_offset_beacon = hapd->cca_c_off_beacon;
3913 settings->counter_offset_presp = hapd->cca_c_off_proberesp;
3914
3915 return 0;
3916}
3917
3918
3919static void hostapd_switch_color_timeout_handler(void *eloop_data,
3920 void *user_ctx)
3921{
3922 struct hostapd_data *hapd = (struct hostapd_data *) eloop_data;
3923 os_time_t delta_t;
3924 unsigned int b;
3925 int i, r;
3926
3927 /* CCA can be triggered once the handler constantly receives
3928 * color collision events to for at least
3929 * DOT11BSS_COLOR_COLLISION_AP_PERIOD (50 s by default). */
3930 delta_t = hapd->last_color_collision.sec -
3931 hapd->first_color_collision.sec;
3932 if (delta_t < DOT11BSS_COLOR_COLLISION_AP_PERIOD)
3933 return;
3934
3935 r = os_random() % HE_OPERATION_BSS_COLOR_MAX;
3936 for (i = 0; i < HE_OPERATION_BSS_COLOR_MAX; i++) {
Sunil8cd6f4d2022-06-28 18:40:46 +00003937 if (r && !(hapd->color_collision_bitmap & (1ULL << r)))
Sunil Ravia04bd252022-05-02 22:54:18 -07003938 break;
3939
3940 r = (r + 1) % HE_OPERATION_BSS_COLOR_MAX;
3941 }
3942
3943 if (i == HE_OPERATION_BSS_COLOR_MAX) {
3944 /* There are no free colors so turn BSS coloring off */
3945 wpa_printf(MSG_INFO,
3946 "No free colors left, turning off BSS coloring");
3947 hapd->iface->conf->he_op.he_bss_color_disabled = 1;
3948 hapd->iface->conf->he_op.he_bss_color = os_random() % 63 + 1;
3949 for (b = 0; b < hapd->iface->num_bss; b++)
3950 ieee802_11_set_beacon(hapd->iface->bss[b]);
3951 return;
3952 }
3953
3954 for (b = 0; b < hapd->iface->num_bss; b++) {
3955 struct hostapd_data *bss = hapd->iface->bss[b];
3956 struct cca_settings settings;
3957 int ret;
3958
3959 hostapd_cleanup_cca_params(bss);
3960 bss->cca_color = r;
3961 bss->cca_count = 10;
3962
3963 if (hostapd_fill_cca_settings(bss, &settings)) {
3964 hostapd_cleanup_cca_params(bss);
3965 continue;
3966 }
3967
3968 ret = hostapd_drv_switch_color(bss, &settings);
3969 if (ret)
3970 hostapd_cleanup_cca_params(bss);
3971
3972 free_beacon_data(&settings.beacon_cca);
3973 free_beacon_data(&settings.beacon_after);
3974 }
3975}
3976
3977
3978void hostapd_switch_color(struct hostapd_data *hapd, u64 bitmap)
3979{
3980 struct os_reltime now;
3981
3982 if (hapd->cca_in_progress)
3983 return;
3984
3985 if (os_get_reltime(&now))
3986 return;
3987
3988 hapd->color_collision_bitmap = bitmap;
3989 hapd->last_color_collision = now;
3990
3991 if (eloop_is_timeout_registered(hostapd_switch_color_timeout_handler,
3992 hapd, NULL))
3993 return;
3994
3995 hapd->first_color_collision = now;
3996 /* 10 s window as margin for persistent color collision reporting */
3997 eloop_register_timeout(DOT11BSS_COLOR_COLLISION_AP_PERIOD + 10, 0,
3998 hostapd_switch_color_timeout_handler,
3999 hapd, NULL);
4000}
4001
4002#endif /* CONFIG_IEEE80211AX */
4003
Dmitry Shmidte4663042016-04-04 10:07:49 -07004004#endif /* NEED_AP_MLME */
4005
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004006
4007struct hostapd_data * hostapd_get_iface(struct hapd_interfaces *interfaces,
4008 const char *ifname)
4009{
4010 size_t i, j;
4011
4012 for (i = 0; i < interfaces->count; i++) {
4013 struct hostapd_iface *iface = interfaces->iface[i];
4014
4015 for (j = 0; j < iface->num_bss; j++) {
4016 struct hostapd_data *hapd = iface->bss[j];
4017
4018 if (os_strcmp(ifname, hapd->conf->iface) == 0)
4019 return hapd;
4020 }
4021 }
4022
4023 return NULL;
4024}
4025
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004026
4027void hostapd_periodic_iface(struct hostapd_iface *iface)
4028{
4029 size_t i;
4030
4031 ap_list_timer(iface);
4032
4033 for (i = 0; i < iface->num_bss; i++) {
4034 struct hostapd_data *hapd = iface->bss[i];
4035
4036 if (!hapd->started)
4037 continue;
4038
4039#ifndef CONFIG_NO_RADIUS
4040 hostapd_acl_expire(hapd);
4041#endif /* CONFIG_NO_RADIUS */
4042 }
4043}
Hai Shalom899fcc72020-10-19 14:38:18 -07004044
4045
4046#ifdef CONFIG_OCV
4047void hostapd_ocv_check_csa_sa_query(void *eloop_ctx, void *timeout_ctx)
4048{
4049 struct hostapd_data *hapd = eloop_ctx;
4050 struct sta_info *sta;
4051
4052 wpa_printf(MSG_DEBUG, "OCV: Post-CSA SA Query initiation check");
4053
4054 for (sta = hapd->sta_list; sta; sta = sta->next) {
4055 if (!sta->post_csa_sa_query)
4056 continue;
4057
4058 wpa_printf(MSG_DEBUG, "OCV: OCVC STA " MACSTR
4059 " did not start SA Query after CSA - disconnect",
4060 MAC2STR(sta->addr));
4061 ap_sta_disconnect(hapd, sta, sta->addr,
4062 WLAN_REASON_PREV_AUTH_NOT_VALID);
4063 }
4064}
4065#endif /* CONFIG_OCV */