blob: ef53c41dfb0d2c7599dbc7fd21e7aacf5f1996dd [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
Hai Shalomce48b4a2018-09-05 11:41:35 -070094void hostapd_reconfig_encryption(struct hostapd_data *hapd)
95{
96 if (hapd->wpa_auth)
97 return;
98
99 hostapd_set_privacy(hapd, 0);
Hai Shalomfdcde762020-04-02 11:19:20 -0700100#ifdef CONFIG_WEP
Hai Shalomce48b4a2018-09-05 11:41:35 -0700101 hostapd_setup_encryption(hapd->conf->iface, hapd);
Hai Shalomfdcde762020-04-02 11:19:20 -0700102#endif /* CONFIG_WEP */
Hai Shalomce48b4a2018-09-05 11:41:35 -0700103}
104
105
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700106static void hostapd_reload_bss(struct hostapd_data *hapd)
107{
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800108 struct hostapd_ssid *ssid;
109
Dmitry Shmidt29333592017-01-09 12:27:11 -0800110 if (!hapd->started)
111 return;
112
Roshan Pius3a1667e2018-07-03 15:17:14 -0700113 if (hapd->conf->wmm_enabled < 0)
Hai Shaloma20dcd72022-02-04 13:43:00 -0800114 hapd->conf->wmm_enabled = hapd->iconf->ieee80211n |
115 hapd->iconf->ieee80211ax;
Roshan Pius3a1667e2018-07-03 15:17:14 -0700116
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700117#ifndef CONFIG_NO_RADIUS
118 radius_client_reconfig(hapd->radius, hapd->conf->radius);
119#endif /* CONFIG_NO_RADIUS */
120
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800121 ssid = &hapd->conf->ssid;
122 if (!ssid->wpa_psk_set && ssid->wpa_psk && !ssid->wpa_psk->next &&
123 ssid->wpa_passphrase_set && ssid->wpa_passphrase) {
124 /*
125 * Force PSK to be derived again since SSID or passphrase may
126 * have changed.
127 */
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800128 hostapd_config_clear_wpa_psk(&hapd->conf->ssid.wpa_psk);
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800129 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700130 if (hostapd_setup_wpa_psk(hapd->conf)) {
131 wpa_printf(MSG_ERROR, "Failed to re-configure WPA PSK "
132 "after reloading configuration");
133 }
134
135 if (hapd->conf->ieee802_1x || hapd->conf->wpa)
136 hostapd_set_drv_ieee8021x(hapd, hapd->conf->iface, 1);
137 else
138 hostapd_set_drv_ieee8021x(hapd, hapd->conf->iface, 0);
139
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800140 if ((hapd->conf->wpa || hapd->conf->osen) && hapd->wpa_auth == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700141 hostapd_setup_wpa(hapd);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800142 if (hapd->wpa_auth)
143 wpa_init_keys(hapd->wpa_auth);
144 } else if (hapd->conf->wpa) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700145 const u8 *wpa_ie;
146 size_t wpa_ie_len;
147 hostapd_reconfig_wpa(hapd);
148 wpa_ie = wpa_auth_get_wpa_ie(hapd->wpa_auth, &wpa_ie_len);
149 if (hostapd_set_generic_elem(hapd, wpa_ie, wpa_ie_len))
150 wpa_printf(MSG_ERROR, "Failed to configure WPA IE for "
151 "the kernel driver.");
152 } else if (hapd->wpa_auth) {
153 wpa_deinit(hapd->wpa_auth);
154 hapd->wpa_auth = NULL;
155 hostapd_set_privacy(hapd, 0);
Hai Shalomfdcde762020-04-02 11:19:20 -0700156#ifdef CONFIG_WEP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700157 hostapd_setup_encryption(hapd->conf->iface, hapd);
Hai Shalomfdcde762020-04-02 11:19:20 -0700158#endif /* CONFIG_WEP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700159 hostapd_set_generic_elem(hapd, (u8 *) "", 0);
160 }
161
162 ieee802_11_set_beacon(hapd);
163 hostapd_update_wps(hapd);
164
165 if (hapd->conf->ssid.ssid_set &&
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700166 hostapd_set_ssid(hapd, hapd->conf->ssid.ssid,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700167 hapd->conf->ssid.ssid_len)) {
168 wpa_printf(MSG_ERROR, "Could not set SSID for kernel driver");
169 /* try to continue */
170 }
171 wpa_printf(MSG_DEBUG, "Reconfigured interface %s", hapd->conf->iface);
172}
173
174
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700175static void hostapd_clear_old(struct hostapd_iface *iface)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700176{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700177 size_t j;
178
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700179 /*
180 * Deauthenticate all stations since the new configuration may not
181 * allow them to use the BSS anymore.
182 */
183 for (j = 0; j < iface->num_bss; j++) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700184 hostapd_flush_old_stations(iface->bss[j],
185 WLAN_REASON_PREV_AUTH_NOT_VALID);
Hai Shalomfdcde762020-04-02 11:19:20 -0700186#ifdef CONFIG_WEP
Dmitry Shmidtc55524a2011-07-07 11:18:38 -0700187 hostapd_broadcast_wep_clear(iface->bss[j]);
Hai Shalomfdcde762020-04-02 11:19:20 -0700188#endif /* CONFIG_WEP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700189
190#ifndef CONFIG_NO_RADIUS
191 /* TODO: update dynamic data based on changed configuration
192 * items (e.g., open/close sockets, etc.) */
193 radius_client_flush(iface->bss[j]->radius, 0);
194#endif /* CONFIG_NO_RADIUS */
195 }
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700196}
197
198
Hai Shalom74f70d42019-02-11 14:42:39 -0800199static int hostapd_iface_conf_changed(struct hostapd_config *newconf,
200 struct hostapd_config *oldconf)
201{
202 size_t i;
203
204 if (newconf->num_bss != oldconf->num_bss)
205 return 1;
206
207 for (i = 0; i < newconf->num_bss; i++) {
208 if (os_strcmp(newconf->bss[i]->iface,
209 oldconf->bss[i]->iface) != 0)
210 return 1;
211 }
212
213 return 0;
214}
215
216
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700217int hostapd_reload_config(struct hostapd_iface *iface)
218{
Hai Shalom74f70d42019-02-11 14:42:39 -0800219 struct hapd_interfaces *interfaces = iface->interfaces;
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700220 struct hostapd_data *hapd = iface->bss[0];
221 struct hostapd_config *newconf, *oldconf;
222 size_t j;
223
224 if (iface->config_fname == NULL) {
225 /* Only in-memory config in use - assume it has been updated */
226 hostapd_clear_old(iface);
227 for (j = 0; j < iface->num_bss; j++)
228 hostapd_reload_bss(iface->bss[j]);
229 return 0;
230 }
231
232 if (iface->interfaces == NULL ||
233 iface->interfaces->config_read_cb == NULL)
234 return -1;
235 newconf = iface->interfaces->config_read_cb(iface->config_fname);
236 if (newconf == NULL)
237 return -1;
238
239 hostapd_clear_old(iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700240
241 oldconf = hapd->iconf;
Hai Shalom74f70d42019-02-11 14:42:39 -0800242 if (hostapd_iface_conf_changed(newconf, oldconf)) {
243 char *fname;
244 int res;
245
246 wpa_printf(MSG_DEBUG,
247 "Configuration changes include interface/BSS modification - force full disable+enable sequence");
248 fname = os_strdup(iface->config_fname);
249 if (!fname) {
250 hostapd_config_free(newconf);
251 return -1;
252 }
253 hostapd_remove_iface(interfaces, hapd->conf->iface);
254 iface = hostapd_init(interfaces, fname);
255 os_free(fname);
256 hostapd_config_free(newconf);
257 if (!iface) {
258 wpa_printf(MSG_ERROR,
259 "Failed to initialize interface on config reload");
260 return -1;
261 }
262 iface->interfaces = interfaces;
263 interfaces->iface[interfaces->count] = iface;
264 interfaces->count++;
265 res = hostapd_enable_iface(iface);
266 if (res < 0)
267 wpa_printf(MSG_ERROR,
268 "Failed to enable interface on config reload");
269 return res;
270 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700271 iface->conf = newconf;
272
273 for (j = 0; j < iface->num_bss; j++) {
274 hapd = iface->bss[j];
275 hapd->iconf = newconf;
Dmitry Shmidtd11f0192014-03-24 12:09:47 -0700276 hapd->iconf->channel = oldconf->channel;
Dmitry Shmidtdda10c22015-03-24 16:05:01 -0700277 hapd->iconf->acs = oldconf->acs;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -0700278 hapd->iconf->secondary_channel = oldconf->secondary_channel;
Dmitry Shmidtd11f0192014-03-24 12:09:47 -0700279 hapd->iconf->ieee80211n = oldconf->ieee80211n;
280 hapd->iconf->ieee80211ac = oldconf->ieee80211ac;
281 hapd->iconf->ht_capab = oldconf->ht_capab;
282 hapd->iconf->vht_capab = oldconf->vht_capab;
Hai Shalom81f62d82019-07-22 12:10:00 -0700283 hostapd_set_oper_chwidth(hapd->iconf,
284 hostapd_get_oper_chwidth(oldconf));
285 hostapd_set_oper_centr_freq_seg0_idx(
286 hapd->iconf,
287 hostapd_get_oper_centr_freq_seg0_idx(oldconf));
288 hostapd_set_oper_centr_freq_seg1_idx(
289 hapd->iconf,
290 hostapd_get_oper_centr_freq_seg1_idx(oldconf));
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800291 hapd->conf = newconf->bss[j];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700292 hostapd_reload_bss(hapd);
293 }
294
295 hostapd_config_free(oldconf);
296
297
298 return 0;
299}
300
301
Hai Shalomfdcde762020-04-02 11:19:20 -0700302#ifdef CONFIG_WEP
303
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700304static void hostapd_broadcast_key_clear_iface(struct hostapd_data *hapd,
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700305 const char *ifname)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700306{
307 int i;
308
Dmitry Shmidt29333592017-01-09 12:27:11 -0800309 if (!ifname || !hapd->drv_priv)
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700310 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700311 for (i = 0; i < NUM_WEP_KEYS; i++) {
Hai Shalomfdcde762020-04-02 11:19:20 -0700312 if (hostapd_drv_set_key(ifname, hapd, WPA_ALG_NONE, NULL, i, 0,
313 0, NULL, 0, NULL, 0, KEY_FLAG_GROUP)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700314 wpa_printf(MSG_DEBUG, "Failed to clear default "
315 "encryption keys (ifname=%s keyidx=%d)",
316 ifname, i);
317 }
318 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700319 if (hapd->conf->ieee80211w) {
320 for (i = NUM_WEP_KEYS; i < NUM_WEP_KEYS + 2; i++) {
321 if (hostapd_drv_set_key(ifname, hapd, WPA_ALG_NONE,
Hai Shalomfdcde762020-04-02 11:19:20 -0700322 NULL, i, 0, 0, NULL,
323 0, NULL, 0, KEY_FLAG_GROUP)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700324 wpa_printf(MSG_DEBUG, "Failed to clear "
325 "default mgmt encryption keys "
326 "(ifname=%s keyidx=%d)", ifname, i);
327 }
328 }
329 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700330}
331
332
333static int hostapd_broadcast_wep_clear(struct hostapd_data *hapd)
334{
335 hostapd_broadcast_key_clear_iface(hapd, hapd->conf->iface);
336 return 0;
337}
338
339
340static int hostapd_broadcast_wep_set(struct hostapd_data *hapd)
341{
342 int errors = 0, idx;
343 struct hostapd_ssid *ssid = &hapd->conf->ssid;
344
345 idx = ssid->wep.idx;
Hai Shalomfdcde762020-04-02 11:19:20 -0700346 if (ssid->wep.default_len && ssid->wep.key[idx] &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700347 hostapd_drv_set_key(hapd->conf->iface,
Hai Shalomfdcde762020-04-02 11:19:20 -0700348 hapd, WPA_ALG_WEP, broadcast_ether_addr, idx, 0,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700349 1, NULL, 0, ssid->wep.key[idx],
Hai Shalomfdcde762020-04-02 11:19:20 -0700350 ssid->wep.len[idx],
351 KEY_FLAG_GROUP_RX_TX_DEFAULT)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700352 wpa_printf(MSG_WARNING, "Could not set WEP encryption.");
353 errors++;
354 }
355
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700356 return errors;
357}
358
Hai Shalomfdcde762020-04-02 11:19:20 -0700359#endif /* CONFIG_WEP */
360
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700361
Hai Shalom60840252021-02-19 19:02:11 -0800362void hostapd_free_hapd_data(struct hostapd_data *hapd)
Dmitry Shmidt04949592012-07-19 12:16:46 -0700363{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800364 os_free(hapd->probereq_cb);
365 hapd->probereq_cb = NULL;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800366 hapd->num_probereq_cb = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800367
368#ifdef CONFIG_P2P
369 wpabuf_free(hapd->p2p_beacon_ie);
370 hapd->p2p_beacon_ie = NULL;
371 wpabuf_free(hapd->p2p_probe_resp_ie);
372 hapd->p2p_probe_resp_ie = NULL;
373#endif /* CONFIG_P2P */
374
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -0800375 if (!hapd->started) {
376 wpa_printf(MSG_ERROR, "%s: Interface %s wasn't started",
Hai Shalom021b0b52019-04-10 11:17:58 -0700377 __func__, hapd->conf ? hapd->conf->iface : "N/A");
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -0800378 return;
379 }
380 hapd->started = 0;
Hai Shalom5f92bc92019-04-18 11:54:11 -0700381 hapd->beacon_set_done = 0;
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -0800382
Dmitry Shmidt54605472013-11-08 11:10:19 -0800383 wpa_printf(MSG_DEBUG, "%s(%s)", __func__, hapd->conf->iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700384 accounting_deinit(hapd);
385 hostapd_deinit_wpa(hapd);
386 vlan_deinit(hapd);
387 hostapd_acl_deinit(hapd);
388#ifndef CONFIG_NO_RADIUS
389 radius_client_deinit(hapd->radius);
390 hapd->radius = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700391 radius_das_deinit(hapd->radius_das);
392 hapd->radius_das = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700393#endif /* CONFIG_NO_RADIUS */
394
395 hostapd_deinit_wps(hapd);
Hai Shalom81f62d82019-07-22 12:10:00 -0700396 ieee802_1x_dealloc_kay_sm_hapd(hapd);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700397#ifdef CONFIG_DPP
398 hostapd_dpp_deinit(hapd);
399 gas_query_ap_deinit(hapd->gas);
Hai Shaloma20dcd72022-02-04 13:43:00 -0800400 hapd->gas = NULL;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700401#endif /* CONFIG_DPP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700402
403 authsrv_deinit(hapd);
404
Dmitry Shmidt71757432014-06-02 13:50:35 -0700405 if (hapd->interface_added) {
406 hapd->interface_added = 0;
407 if (hostapd_if_remove(hapd, WPA_IF_AP_BSS, hapd->conf->iface)) {
408 wpa_printf(MSG_WARNING,
409 "Failed to remove BSS interface %s",
410 hapd->conf->iface);
411 hapd->interface_added = 1;
412 } else {
413 /*
414 * Since this was a dynamically added interface, the
415 * driver wrapper may have removed its internal instance
416 * and hapd->drv_priv is not valid anymore.
417 */
418 hapd->drv_priv = NULL;
419 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700420 }
421
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800422 wpabuf_free(hapd->time_adv);
Hai Shaloma20dcd72022-02-04 13:43:00 -0800423 hapd->time_adv = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700424
425#ifdef CONFIG_INTERWORKING
426 gas_serv_deinit(hapd);
427#endif /* CONFIG_INTERWORKING */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800428
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800429 bss_load_update_deinit(hapd);
430 ndisc_snoop_deinit(hapd);
431 dhcp_snoop_deinit(hapd);
432 x_snoop_deinit(hapd);
433
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800434#ifdef CONFIG_SQLITE
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700435 bin_clear_free(hapd->tmp_eap_user.identity,
436 hapd->tmp_eap_user.identity_len);
437 bin_clear_free(hapd->tmp_eap_user.password,
438 hapd->tmp_eap_user.password_len);
Hai Shaloma20dcd72022-02-04 13:43:00 -0800439 os_memset(&hapd->tmp_eap_user, 0, sizeof(hapd->tmp_eap_user));
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800440#endif /* CONFIG_SQLITE */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800441
442#ifdef CONFIG_MESH
443 wpabuf_free(hapd->mesh_pending_auth);
444 hapd->mesh_pending_auth = NULL;
Hai Shalom60840252021-02-19 19:02:11 -0800445 /* handling setup failure is already done */
446 hapd->setup_complete_cb = NULL;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800447#endif /* CONFIG_MESH */
Dmitry Shmidt849734c2016-05-27 09:59:01 -0700448
449 hostapd_clean_rrm(hapd);
Dmitry Shmidtebd93af2017-02-21 13:40:44 -0800450 fils_hlp_deinit(hapd);
Hai Shalom021b0b52019-04-10 11:17:58 -0700451
Hai Shalom899fcc72020-10-19 14:38:18 -0700452#ifdef CONFIG_OCV
453 eloop_cancel_timeout(hostapd_ocv_check_csa_sa_query, hapd, NULL);
454#endif /* CONFIG_OCV */
455
Hai Shalom021b0b52019-04-10 11:17:58 -0700456#ifdef CONFIG_SAE
457 {
458 struct hostapd_sae_commit_queue *q;
459
460 while ((q = dl_list_first(&hapd->sae_commit_queue,
461 struct hostapd_sae_commit_queue,
462 list))) {
463 dl_list_del(&q->list);
464 os_free(q);
465 }
466 }
467 eloop_cancel_timeout(auth_sae_process_commit, hapd, NULL);
468#endif /* CONFIG_SAE */
Sunil Ravia04bd252022-05-02 22:54:18 -0700469
470#ifdef CONFIG_IEEE80211AX
471 eloop_cancel_timeout(hostapd_switch_color_timeout_handler, hapd, NULL);
472#endif /* CONFIG_IEEE80211AX */
Dmitry Shmidt04949592012-07-19 12:16:46 -0700473}
474
475
476/**
477 * hostapd_cleanup - Per-BSS cleanup (deinitialization)
478 * @hapd: Pointer to BSS data
479 *
480 * This function is used to free all per-BSS data structures and resources.
Dmitry Shmidt54605472013-11-08 11:10:19 -0800481 * Most of the modules that are initialized in hostapd_setup_bss() are
482 * deinitialized here.
Dmitry Shmidt04949592012-07-19 12:16:46 -0700483 */
484static void hostapd_cleanup(struct hostapd_data *hapd)
485{
Dmitry Shmidt54605472013-11-08 11:10:19 -0800486 wpa_printf(MSG_DEBUG, "%s(hapd=%p (%s))", __func__, hapd,
Hai Shalom021b0b52019-04-10 11:17:58 -0700487 hapd->conf ? hapd->conf->iface : "N/A");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700488 if (hapd->iface->interfaces &&
Dmitry Shmidt29333592017-01-09 12:27:11 -0800489 hapd->iface->interfaces->ctrl_iface_deinit) {
490 wpa_msg(hapd->msg_ctx, MSG_INFO, WPA_EVENT_TERMINATING);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700491 hapd->iface->interfaces->ctrl_iface_deinit(hapd);
Dmitry Shmidt29333592017-01-09 12:27:11 -0800492 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700493 hostapd_free_hapd_data(hapd);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700494}
495
496
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800497static void sta_track_deinit(struct hostapd_iface *iface)
498{
499 struct hostapd_sta_info *info;
500
501 if (!iface->num_sta_seen)
502 return;
503
504 while ((info = dl_list_first(&iface->sta_seen, struct hostapd_sta_info,
505 list))) {
506 dl_list_del(&info->list);
507 iface->num_sta_seen--;
Dmitry Shmidtaca489e2016-09-28 15:44:14 -0700508 sta_track_del(info);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800509 }
510}
511
512
Hai Shalom60840252021-02-19 19:02:11 -0800513void hostapd_cleanup_iface_partial(struct hostapd_iface *iface)
Dmitry Shmidt04949592012-07-19 12:16:46 -0700514{
Dmitry Shmidt54605472013-11-08 11:10:19 -0800515 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800516#ifdef NEED_AP_MLME
517 hostapd_stop_setup_timers(iface);
518#endif /* NEED_AP_MLME */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700519 if (iface->current_mode)
520 acs_cleanup(iface);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700521 hostapd_free_hw_features(iface->hw_features, iface->num_hw_features);
522 iface->hw_features = NULL;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700523 iface->current_mode = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700524 os_free(iface->current_rates);
525 iface->current_rates = NULL;
526 os_free(iface->basic_rates);
527 iface->basic_rates = NULL;
528 ap_list_deinit(iface);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800529 sta_track_deinit(iface);
Hai Shalom81f62d82019-07-22 12:10:00 -0700530 airtime_policy_update_deinit(iface);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700531}
532
533
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700534/**
535 * hostapd_cleanup_iface - Complete per-interface cleanup
536 * @iface: Pointer to interface data
537 *
538 * This function is called after per-BSS data structures are deinitialized
539 * with hostapd_cleanup().
540 */
541static void hostapd_cleanup_iface(struct hostapd_iface *iface)
542{
Dmitry Shmidt54605472013-11-08 11:10:19 -0800543 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800544 eloop_cancel_timeout(channel_list_update_timeout, iface, NULL);
Roshan Pius3a1667e2018-07-03 15:17:14 -0700545 eloop_cancel_timeout(hostapd_interface_setup_failure_handler, iface,
546 NULL);
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800547
Dmitry Shmidt04949592012-07-19 12:16:46 -0700548 hostapd_cleanup_iface_partial(iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700549 hostapd_config_free(iface->conf);
550 iface->conf = NULL;
551
552 os_free(iface->config_fname);
553 os_free(iface->bss);
Dmitry Shmidt54605472013-11-08 11:10:19 -0800554 wpa_printf(MSG_DEBUG, "%s: free iface=%p", __func__, iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700555 os_free(iface);
556}
557
558
Hai Shalomfdcde762020-04-02 11:19:20 -0700559#ifdef CONFIG_WEP
560
Dmitry Shmidt04949592012-07-19 12:16:46 -0700561static void hostapd_clear_wep(struct hostapd_data *hapd)
562{
Hai Shalom021b0b52019-04-10 11:17:58 -0700563 if (hapd->drv_priv && !hapd->iface->driver_ap_teardown && hapd->conf) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700564 hostapd_set_privacy(hapd, 0);
565 hostapd_broadcast_wep_clear(hapd);
566 }
567}
568
569
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700570static int hostapd_setup_encryption(char *iface, struct hostapd_data *hapd)
571{
572 int i;
573
574 hostapd_broadcast_wep_set(hapd);
575
576 if (hapd->conf->ssid.wep.default_len) {
577 hostapd_set_privacy(hapd, 1);
578 return 0;
579 }
580
Jouni Malinen75ecf522011-06-27 15:19:46 -0700581 /*
582 * When IEEE 802.1X is not enabled, the driver may need to know how to
583 * set authentication algorithms for static WEP.
584 */
585 hostapd_drv_set_authmode(hapd, hapd->conf->auth_algs);
586
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700587 for (i = 0; i < 4; i++) {
588 if (hapd->conf->ssid.wep.key[i] &&
Hai Shalomfdcde762020-04-02 11:19:20 -0700589 hostapd_drv_set_key(iface, hapd, WPA_ALG_WEP, NULL, i, 0,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700590 i == hapd->conf->ssid.wep.idx, NULL, 0,
591 hapd->conf->ssid.wep.key[i],
Hai Shalomfdcde762020-04-02 11:19:20 -0700592 hapd->conf->ssid.wep.len[i],
593 i == hapd->conf->ssid.wep.idx ?
594 KEY_FLAG_GROUP_RX_TX_DEFAULT :
595 KEY_FLAG_GROUP_RX_TX)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700596 wpa_printf(MSG_WARNING, "Could not set WEP "
597 "encryption.");
598 return -1;
599 }
600 if (hapd->conf->ssid.wep.key[i] &&
601 i == hapd->conf->ssid.wep.idx)
602 hostapd_set_privacy(hapd, 1);
603 }
604
605 return 0;
606}
607
Hai Shalomfdcde762020-04-02 11:19:20 -0700608#endif /* CONFIG_WEP */
609
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700610
Dmitry Shmidt04949592012-07-19 12:16:46 -0700611static int hostapd_flush_old_stations(struct hostapd_data *hapd, u16 reason)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700612{
613 int ret = 0;
614 u8 addr[ETH_ALEN];
615
616 if (hostapd_drv_none(hapd) || hapd->drv_priv == NULL)
617 return 0;
618
Dmitry Shmidta38abf92014-03-06 13:38:44 -0800619 if (!hapd->iface->driver_ap_teardown) {
620 wpa_dbg(hapd->msg_ctx, MSG_DEBUG,
621 "Flushing old station entries");
622
623 if (hostapd_flush(hapd)) {
624 wpa_msg(hapd->msg_ctx, MSG_WARNING,
625 "Could not connect to kernel driver");
626 ret = -1;
627 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700628 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700629 if (hapd->conf && hapd->conf->broadcast_deauth) {
630 wpa_dbg(hapd->msg_ctx, MSG_DEBUG,
631 "Deauthenticate all stations");
632 os_memset(addr, 0xff, ETH_ALEN);
633 hostapd_drv_sta_deauth(hapd, addr, reason);
634 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700635 hostapd_free_stas(hapd);
636
637 return ret;
638}
639
640
Hai Shalom60840252021-02-19 19:02:11 -0800641void hostapd_bss_deinit_no_free(struct hostapd_data *hapd)
Dmitry Shmidt71757432014-06-02 13:50:35 -0700642{
643 hostapd_free_stas(hapd);
644 hostapd_flush_old_stations(hapd, WLAN_REASON_DEAUTH_LEAVING);
Hai Shalomfdcde762020-04-02 11:19:20 -0700645#ifdef CONFIG_WEP
Dmitry Shmidt71757432014-06-02 13:50:35 -0700646 hostapd_clear_wep(hapd);
Hai Shalomfdcde762020-04-02 11:19:20 -0700647#endif /* CONFIG_WEP */
Dmitry Shmidt71757432014-06-02 13:50:35 -0700648}
649
650
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700651/**
652 * hostapd_validate_bssid_configuration - Validate BSSID configuration
653 * @iface: Pointer to interface data
654 * Returns: 0 on success, -1 on failure
655 *
656 * This function is used to validate that the configured BSSIDs are valid.
657 */
658static int hostapd_validate_bssid_configuration(struct hostapd_iface *iface)
659{
660 u8 mask[ETH_ALEN] = { 0 };
661 struct hostapd_data *hapd = iface->bss[0];
662 unsigned int i = iface->conf->num_bss, bits = 0, j;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700663 int auto_addr = 0;
664
665 if (hostapd_drv_none(hapd))
666 return 0;
667
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -0800668 if (iface->conf->use_driver_iface_addr)
669 return 0;
670
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700671 /* Generate BSSID mask that is large enough to cover the BSSIDs. */
672
673 /* Determine the bits necessary to cover the number of BSSIDs. */
674 for (i--; i; i >>= 1)
675 bits++;
676
677 /* Determine the bits necessary to any configured BSSIDs,
678 if they are higher than the number of BSSIDs. */
679 for (j = 0; j < iface->conf->num_bss; j++) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800680 if (is_zero_ether_addr(iface->conf->bss[j]->bssid)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700681 if (j)
682 auto_addr++;
683 continue;
684 }
685
686 for (i = 0; i < ETH_ALEN; i++) {
687 mask[i] |=
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800688 iface->conf->bss[j]->bssid[i] ^
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700689 hapd->own_addr[i];
690 }
691 }
692
693 if (!auto_addr)
694 goto skip_mask_ext;
695
696 for (i = 0; i < ETH_ALEN && mask[i] == 0; i++)
697 ;
698 j = 0;
699 if (i < ETH_ALEN) {
700 j = (5 - i) * 8;
701
702 while (mask[i] != 0) {
703 mask[i] >>= 1;
704 j++;
705 }
706 }
707
708 if (bits < j)
709 bits = j;
710
711 if (bits > 40) {
712 wpa_printf(MSG_ERROR, "Too many bits in the BSSID mask (%u)",
713 bits);
714 return -1;
715 }
716
717 os_memset(mask, 0xff, ETH_ALEN);
718 j = bits / 8;
719 for (i = 5; i > 5 - j; i--)
720 mask[i] = 0;
721 j = bits % 8;
Hai Shalom021b0b52019-04-10 11:17:58 -0700722 while (j) {
723 j--;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700724 mask[i] <<= 1;
Hai Shalom021b0b52019-04-10 11:17:58 -0700725 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700726
727skip_mask_ext:
728 wpa_printf(MSG_DEBUG, "BSS count %lu, BSSID mask " MACSTR " (%d bits)",
729 (unsigned long) iface->conf->num_bss, MAC2STR(mask), bits);
730
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700731 if (!auto_addr)
732 return 0;
733
734 for (i = 0; i < ETH_ALEN; i++) {
735 if ((hapd->own_addr[i] & mask[i]) != hapd->own_addr[i]) {
736 wpa_printf(MSG_ERROR, "Invalid BSSID mask " MACSTR
737 " for start address " MACSTR ".",
738 MAC2STR(mask), MAC2STR(hapd->own_addr));
739 wpa_printf(MSG_ERROR, "Start address must be the "
740 "first address in the block (i.e., addr "
741 "AND mask == addr).");
742 return -1;
743 }
744 }
745
746 return 0;
747}
748
749
750static int mac_in_conf(struct hostapd_config *conf, const void *a)
751{
752 size_t i;
753
754 for (i = 0; i < conf->num_bss; i++) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800755 if (hostapd_mac_comp(conf->bss[i]->bssid, a) == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700756 return 1;
757 }
758 }
759
760 return 0;
761}
762
763
Dmitry Shmidt04949592012-07-19 12:16:46 -0700764#ifndef CONFIG_NO_RADIUS
765
766static int hostapd_das_nas_mismatch(struct hostapd_data *hapd,
767 struct radius_das_attrs *attr)
768{
Dmitry Shmidt13ca8d82014-02-20 10:18:40 -0800769 if (attr->nas_identifier &&
770 (!hapd->conf->nas_identifier ||
771 os_strlen(hapd->conf->nas_identifier) !=
772 attr->nas_identifier_len ||
773 os_memcmp(hapd->conf->nas_identifier, attr->nas_identifier,
774 attr->nas_identifier_len) != 0)) {
775 wpa_printf(MSG_DEBUG, "RADIUS DAS: NAS-Identifier mismatch");
776 return 1;
777 }
778
779 if (attr->nas_ip_addr &&
780 (hapd->conf->own_ip_addr.af != AF_INET ||
781 os_memcmp(&hapd->conf->own_ip_addr.u.v4, attr->nas_ip_addr, 4) !=
782 0)) {
783 wpa_printf(MSG_DEBUG, "RADIUS DAS: NAS-IP-Address mismatch");
784 return 1;
785 }
786
787#ifdef CONFIG_IPV6
788 if (attr->nas_ipv6_addr &&
789 (hapd->conf->own_ip_addr.af != AF_INET6 ||
790 os_memcmp(&hapd->conf->own_ip_addr.u.v6, attr->nas_ipv6_addr, 16)
791 != 0)) {
792 wpa_printf(MSG_DEBUG, "RADIUS DAS: NAS-IPv6-Address mismatch");
793 return 1;
794 }
795#endif /* CONFIG_IPV6 */
796
Dmitry Shmidt04949592012-07-19 12:16:46 -0700797 return 0;
798}
799
800
801static struct sta_info * hostapd_das_find_sta(struct hostapd_data *hapd,
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800802 struct radius_das_attrs *attr,
803 int *multi)
Dmitry Shmidt04949592012-07-19 12:16:46 -0700804{
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800805 struct sta_info *selected, *sta;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700806 char buf[128];
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800807 int num_attr = 0;
808 int count;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700809
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800810 *multi = 0;
811
812 for (sta = hapd->sta_list; sta; sta = sta->next)
813 sta->radius_das_match = 1;
814
815 if (attr->sta_addr) {
816 num_attr++;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700817 sta = ap_get_sta(hapd, attr->sta_addr);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800818 if (!sta) {
819 wpa_printf(MSG_DEBUG,
820 "RADIUS DAS: No Calling-Station-Id match");
821 return NULL;
822 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700823
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800824 selected = sta;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700825 for (sta = hapd->sta_list; sta; sta = sta->next) {
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800826 if (sta != selected)
827 sta->radius_das_match = 0;
828 }
829 wpa_printf(MSG_DEBUG, "RADIUS DAS: Calling-Station-Id match");
830 }
831
832 if (attr->acct_session_id) {
833 num_attr++;
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800834 if (attr->acct_session_id_len != 16) {
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800835 wpa_printf(MSG_DEBUG,
836 "RADIUS DAS: Acct-Session-Id cannot match");
837 return NULL;
838 }
839 count = 0;
840
841 for (sta = hapd->sta_list; sta; sta = sta->next) {
842 if (!sta->radius_das_match)
843 continue;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800844 os_snprintf(buf, sizeof(buf), "%016llX",
845 (unsigned long long) sta->acct_session_id);
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800846 if (os_memcmp(attr->acct_session_id, buf, 16) != 0)
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800847 sta->radius_das_match = 0;
848 else
849 count++;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700850 }
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800851
852 if (count == 0) {
853 wpa_printf(MSG_DEBUG,
854 "RADIUS DAS: No matches remaining after Acct-Session-Id check");
855 return NULL;
856 }
857 wpa_printf(MSG_DEBUG, "RADIUS DAS: Acct-Session-Id match");
Dmitry Shmidt04949592012-07-19 12:16:46 -0700858 }
859
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800860 if (attr->acct_multi_session_id) {
861 num_attr++;
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800862 if (attr->acct_multi_session_id_len != 16) {
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800863 wpa_printf(MSG_DEBUG,
864 "RADIUS DAS: Acct-Multi-Session-Id cannot match");
865 return NULL;
866 }
867 count = 0;
868
869 for (sta = hapd->sta_list; sta; sta = sta->next) {
870 if (!sta->radius_das_match)
871 continue;
872 if (!sta->eapol_sm ||
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800873 !sta->eapol_sm->acct_multi_session_id) {
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800874 sta->radius_das_match = 0;
875 continue;
876 }
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800877 os_snprintf(buf, sizeof(buf), "%016llX",
878 (unsigned long long)
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800879 sta->eapol_sm->acct_multi_session_id);
880 if (os_memcmp(attr->acct_multi_session_id, buf, 16) !=
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800881 0)
882 sta->radius_das_match = 0;
883 else
884 count++;
885 }
886
887 if (count == 0) {
888 wpa_printf(MSG_DEBUG,
889 "RADIUS DAS: No matches remaining after Acct-Multi-Session-Id check");
890 return NULL;
891 }
892 wpa_printf(MSG_DEBUG,
893 "RADIUS DAS: Acct-Multi-Session-Id match");
894 }
895
896 if (attr->cui) {
897 num_attr++;
898 count = 0;
899
Dmitry Shmidt04949592012-07-19 12:16:46 -0700900 for (sta = hapd->sta_list; sta; sta = sta->next) {
901 struct wpabuf *cui;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800902
903 if (!sta->radius_das_match)
904 continue;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700905 cui = ieee802_1x_get_radius_cui(sta->eapol_sm);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800906 if (!cui || wpabuf_len(cui) != attr->cui_len ||
Dmitry Shmidt04949592012-07-19 12:16:46 -0700907 os_memcmp(wpabuf_head(cui), attr->cui,
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800908 attr->cui_len) != 0)
909 sta->radius_das_match = 0;
910 else
911 count++;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700912 }
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800913
914 if (count == 0) {
915 wpa_printf(MSG_DEBUG,
916 "RADIUS DAS: No matches remaining after Chargeable-User-Identity check");
917 return NULL;
918 }
919 wpa_printf(MSG_DEBUG,
920 "RADIUS DAS: Chargeable-User-Identity match");
Dmitry Shmidt04949592012-07-19 12:16:46 -0700921 }
922
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800923 if (attr->user_name) {
924 num_attr++;
925 count = 0;
926
Dmitry Shmidt04949592012-07-19 12:16:46 -0700927 for (sta = hapd->sta_list; sta; sta = sta->next) {
928 u8 *identity;
929 size_t identity_len;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800930
931 if (!sta->radius_das_match)
932 continue;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700933 identity = ieee802_1x_get_identity(sta->eapol_sm,
934 &identity_len);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800935 if (!identity ||
936 identity_len != attr->user_name_len ||
Dmitry Shmidt04949592012-07-19 12:16:46 -0700937 os_memcmp(identity, attr->user_name, identity_len)
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800938 != 0)
939 sta->radius_das_match = 0;
940 else
941 count++;
942 }
943
944 if (count == 0) {
945 wpa_printf(MSG_DEBUG,
946 "RADIUS DAS: No matches remaining after User-Name check");
947 return NULL;
948 }
949 wpa_printf(MSG_DEBUG,
950 "RADIUS DAS: User-Name match");
951 }
952
953 if (num_attr == 0) {
954 /*
955 * In theory, we could match all current associations, but it
956 * seems safer to just reject requests that do not include any
957 * session identification attributes.
958 */
959 wpa_printf(MSG_DEBUG,
960 "RADIUS DAS: No session identification attributes included");
961 return NULL;
962 }
963
964 selected = NULL;
965 for (sta = hapd->sta_list; sta; sta = sta->next) {
966 if (sta->radius_das_match) {
967 if (selected) {
968 *multi = 1;
969 return NULL;
970 }
971 selected = sta;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700972 }
973 }
974
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800975 return selected;
976}
977
978
979static int hostapd_das_disconnect_pmksa(struct hostapd_data *hapd,
980 struct radius_das_attrs *attr)
981{
982 if (!hapd->wpa_auth)
983 return -1;
984 return wpa_auth_radius_das_disconnect_pmksa(hapd->wpa_auth, attr);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700985}
986
987
988static enum radius_das_res
989hostapd_das_disconnect(void *ctx, struct radius_das_attrs *attr)
990{
991 struct hostapd_data *hapd = ctx;
992 struct sta_info *sta;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800993 int multi;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700994
995 if (hostapd_das_nas_mismatch(hapd, attr))
996 return RADIUS_DAS_NAS_MISMATCH;
997
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800998 sta = hostapd_das_find_sta(hapd, attr, &multi);
999 if (sta == NULL) {
1000 if (multi) {
1001 wpa_printf(MSG_DEBUG,
1002 "RADIUS DAS: Multiple sessions match - not supported");
1003 return RADIUS_DAS_MULTI_SESSION_MATCH;
1004 }
1005 if (hostapd_das_disconnect_pmksa(hapd, attr) == 0) {
1006 wpa_printf(MSG_DEBUG,
1007 "RADIUS DAS: PMKSA cache entry matched");
1008 return RADIUS_DAS_SUCCESS;
1009 }
1010 wpa_printf(MSG_DEBUG, "RADIUS DAS: No matching session found");
Dmitry Shmidt04949592012-07-19 12:16:46 -07001011 return RADIUS_DAS_SESSION_NOT_FOUND;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001012 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001013
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001014 wpa_printf(MSG_DEBUG, "RADIUS DAS: Found a matching session " MACSTR
1015 " - disconnecting", MAC2STR(sta->addr));
Dmitry Shmidt13ca8d82014-02-20 10:18:40 -08001016 wpa_auth_pmksa_remove(hapd->wpa_auth, sta->addr);
1017
Dmitry Shmidt04949592012-07-19 12:16:46 -07001018 hostapd_drv_sta_deauth(hapd, sta->addr,
1019 WLAN_REASON_PREV_AUTH_NOT_VALID);
1020 ap_sta_deauthenticate(hapd, sta, WLAN_REASON_PREV_AUTH_NOT_VALID);
1021
1022 return RADIUS_DAS_SUCCESS;
1023}
1024
Roshan Pius3a1667e2018-07-03 15:17:14 -07001025
1026#ifdef CONFIG_HS20
1027static enum radius_das_res
1028hostapd_das_coa(void *ctx, struct radius_das_attrs *attr)
1029{
1030 struct hostapd_data *hapd = ctx;
1031 struct sta_info *sta;
1032 int multi;
1033
1034 if (hostapd_das_nas_mismatch(hapd, attr))
1035 return RADIUS_DAS_NAS_MISMATCH;
1036
1037 sta = hostapd_das_find_sta(hapd, attr, &multi);
1038 if (!sta) {
1039 if (multi) {
1040 wpa_printf(MSG_DEBUG,
1041 "RADIUS DAS: Multiple sessions match - not supported");
1042 return RADIUS_DAS_MULTI_SESSION_MATCH;
1043 }
1044 wpa_printf(MSG_DEBUG, "RADIUS DAS: No matching session found");
1045 return RADIUS_DAS_SESSION_NOT_FOUND;
1046 }
1047
1048 wpa_printf(MSG_DEBUG, "RADIUS DAS: Found a matching session " MACSTR
1049 " - CoA", MAC2STR(sta->addr));
1050
1051 if (attr->hs20_t_c_filtering) {
1052 if (attr->hs20_t_c_filtering[0] & BIT(0)) {
1053 wpa_printf(MSG_DEBUG,
1054 "HS 2.0: Unexpected Terms and Conditions filtering required in CoA-Request");
1055 return RADIUS_DAS_COA_FAILED;
1056 }
1057
1058 hs20_t_c_filtering(hapd, sta, 0);
1059 }
1060
1061 return RADIUS_DAS_SUCCESS;
1062}
1063#else /* CONFIG_HS20 */
1064#define hostapd_das_coa NULL
1065#endif /* CONFIG_HS20 */
1066
Hai Shalomc3565922019-10-28 11:58:20 -07001067
1068#ifdef CONFIG_SQLITE
1069
1070static int db_table_exists(sqlite3 *db, const char *name)
1071{
1072 char cmd[128];
1073
1074 os_snprintf(cmd, sizeof(cmd), "SELECT 1 FROM %s;", name);
1075 return sqlite3_exec(db, cmd, NULL, NULL, NULL) == SQLITE_OK;
1076}
1077
1078
1079static int db_table_create_radius_attributes(sqlite3 *db)
1080{
1081 char *err = NULL;
1082 const char *sql =
1083 "CREATE TABLE radius_attributes("
1084 " id INTEGER PRIMARY KEY,"
1085 " sta TEXT,"
1086 " reqtype TEXT,"
1087 " attr TEXT"
1088 ");"
1089 "CREATE INDEX idx_sta_reqtype ON radius_attributes(sta,reqtype);";
1090
1091 wpa_printf(MSG_DEBUG,
1092 "Adding database table for RADIUS attribute information");
1093 if (sqlite3_exec(db, sql, NULL, NULL, &err) != SQLITE_OK) {
1094 wpa_printf(MSG_ERROR, "SQLite error: %s", err);
1095 sqlite3_free(err);
1096 return -1;
1097 }
1098
1099 return 0;
1100}
1101
1102#endif /* CONFIG_SQLITE */
1103
Dmitry Shmidt04949592012-07-19 12:16:46 -07001104#endif /* CONFIG_NO_RADIUS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001105
1106
1107/**
1108 * hostapd_setup_bss - Per-BSS setup (initialization)
1109 * @hapd: Pointer to BSS data
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001110 * @first: Whether this BSS is the first BSS of an interface; -1 = not first,
1111 * but interface may exist
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001112 *
1113 * This function is used to initialize all per-BSS data structures and
1114 * resources. This gets called in a loop for each BSS when an interface is
1115 * initialized. Most of the modules that are initialized here will be
1116 * deinitialized in hostapd_cleanup().
1117 */
1118static int hostapd_setup_bss(struct hostapd_data *hapd, int first)
1119{
1120 struct hostapd_bss_config *conf = hapd->conf;
Dmitry Shmidt9d9e6022015-04-23 10:34:55 -07001121 u8 ssid[SSID_MAX_LEN + 1];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001122 int ssid_len, set_ssid;
1123 char force_ifname[IFNAMSIZ];
1124 u8 if_addr[ETH_ALEN];
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001125 int flush_old_stations = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001126
Dmitry Shmidt54605472013-11-08 11:10:19 -08001127 wpa_printf(MSG_DEBUG, "%s(hapd=%p (%s), first=%d)",
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001128 __func__, hapd, conf->iface, first);
Dmitry Shmidt54605472013-11-08 11:10:19 -08001129
Dmitry Shmidt50b691d2014-05-21 14:01:45 -07001130#ifdef EAP_SERVER_TNC
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001131 if (conf->tnc && tncs_global_init() < 0) {
Dmitry Shmidt50b691d2014-05-21 14:01:45 -07001132 wpa_printf(MSG_ERROR, "Failed to initialize TNCS");
1133 return -1;
1134 }
1135#endif /* EAP_SERVER_TNC */
1136
Dmitry Shmidt54605472013-11-08 11:10:19 -08001137 if (hapd->started) {
1138 wpa_printf(MSG_ERROR, "%s: Interface %s was already started",
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001139 __func__, conf->iface);
Dmitry Shmidt54605472013-11-08 11:10:19 -08001140 return -1;
1141 }
1142 hapd->started = 1;
1143
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001144 if (!first || first == -1) {
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08001145 u8 *addr = hapd->own_addr;
1146
1147 if (!is_zero_ether_addr(conf->bssid)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001148 /* Allocate the configured BSSID. */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001149 os_memcpy(hapd->own_addr, conf->bssid, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001150
1151 if (hostapd_mac_comp(hapd->own_addr,
1152 hapd->iface->bss[0]->own_addr) ==
1153 0) {
1154 wpa_printf(MSG_ERROR, "BSS '%s' may not have "
1155 "BSSID set to the MAC address of "
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001156 "the radio", conf->iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001157 return -1;
1158 }
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08001159 } else if (hapd->iconf->use_driver_iface_addr) {
1160 addr = NULL;
1161 } else {
1162 /* Allocate the next available BSSID. */
1163 do {
1164 inc_byte_array(hapd->own_addr, ETH_ALEN);
1165 } while (mac_in_conf(hapd->iconf, hapd->own_addr));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001166 }
1167
Dmitry Shmidt54605472013-11-08 11:10:19 -08001168 hapd->interface_added = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001169 if (hostapd_if_add(hapd->iface->bss[0], WPA_IF_AP_BSS,
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08001170 conf->iface, addr, hapd,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001171 &hapd->drv_priv, force_ifname, if_addr,
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001172 conf->bridge[0] ? conf->bridge : NULL,
1173 first == -1)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001174 wpa_printf(MSG_ERROR, "Failed to add BSS (BSSID="
1175 MACSTR ")", MAC2STR(hapd->own_addr));
Dmitry Shmidt3cf6f792013-12-18 13:12:19 -08001176 hapd->interface_added = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001177 return -1;
1178 }
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08001179
1180 if (!addr)
1181 os_memcpy(hapd->own_addr, if_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001182 }
1183
1184 if (conf->wmm_enabled < 0)
Hai Shaloma20dcd72022-02-04 13:43:00 -08001185 conf->wmm_enabled = hapd->iconf->ieee80211n |
1186 hapd->iconf->ieee80211ax;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001187
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001188#ifdef CONFIG_IEEE80211R_AP
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001189 if (is_zero_ether_addr(conf->r1_key_holder))
1190 os_memcpy(conf->r1_key_holder, hapd->own_addr, ETH_ALEN);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001191#endif /* CONFIG_IEEE80211R_AP */
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001192
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001193#ifdef CONFIG_MESH
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001194 if ((hapd->conf->mesh & MESH_ENABLED) && hapd->iface->mconf == NULL)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001195 flush_old_stations = 0;
1196#endif /* CONFIG_MESH */
1197
1198 if (flush_old_stations)
Hai Shalom4fbc08f2020-05-18 12:37:00 -07001199 hostapd_flush(hapd);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001200 hostapd_set_privacy(hapd, 0);
1201
Hai Shalomfdcde762020-04-02 11:19:20 -07001202#ifdef CONFIG_WEP
Hai Shalom4fbc08f2020-05-18 12:37:00 -07001203 if (!hostapd_drv_nl80211(hapd))
1204 hostapd_broadcast_wep_clear(hapd);
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001205 if (hostapd_setup_encryption(conf->iface, hapd))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001206 return -1;
Hai Shalomfdcde762020-04-02 11:19:20 -07001207#endif /* CONFIG_WEP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001208
1209 /*
1210 * Fetch the SSID from the system and use it or,
1211 * if one was specified in the config file, verify they
1212 * match.
1213 */
1214 ssid_len = hostapd_get_ssid(hapd, ssid, sizeof(ssid));
1215 if (ssid_len < 0) {
1216 wpa_printf(MSG_ERROR, "Could not read SSID from system");
1217 return -1;
1218 }
1219 if (conf->ssid.ssid_set) {
1220 /*
1221 * If SSID is specified in the config file and it differs
1222 * from what is being used then force installation of the
1223 * new SSID.
1224 */
1225 set_ssid = (conf->ssid.ssid_len != (size_t) ssid_len ||
1226 os_memcmp(conf->ssid.ssid, ssid, ssid_len) != 0);
1227 } else {
1228 /*
1229 * No SSID in the config file; just use the one we got
1230 * from the system.
1231 */
1232 set_ssid = 0;
1233 conf->ssid.ssid_len = ssid_len;
1234 os_memcpy(conf->ssid.ssid, ssid, conf->ssid.ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001235 }
1236
Hai Shalomfdcde762020-04-02 11:19:20 -07001237 /*
1238 * Short SSID calculation is identical to FCS and it is defined in
1239 * IEEE P802.11-REVmd/D3.0, 9.4.2.170.3 (Calculating the Short-SSID).
1240 */
1241 conf->ssid.short_ssid = crc32(conf->ssid.ssid, conf->ssid.ssid_len);
1242
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001243 if (!hostapd_drv_none(hapd)) {
Hai Shalomfdcde762020-04-02 11:19:20 -07001244 wpa_printf(MSG_DEBUG, "Using interface %s with hwaddr " MACSTR
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001245 " and ssid \"%s\"",
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001246 conf->iface, MAC2STR(hapd->own_addr),
1247 wpa_ssid_txt(conf->ssid.ssid, conf->ssid.ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001248 }
1249
1250 if (hostapd_setup_wpa_psk(conf)) {
1251 wpa_printf(MSG_ERROR, "WPA-PSK setup failed.");
1252 return -1;
1253 }
1254
1255 /* Set SSID for the kernel driver (to be used in beacon and probe
1256 * response frames) */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001257 if (set_ssid && hostapd_set_ssid(hapd, conf->ssid.ssid,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001258 conf->ssid.ssid_len)) {
1259 wpa_printf(MSG_ERROR, "Could not set SSID for kernel driver");
1260 return -1;
1261 }
1262
Dmitry Shmidt818ea482014-03-10 13:15:21 -07001263 if (wpa_debug_level <= MSG_MSGDUMP)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001264 conf->radius->msg_dumps = 1;
1265#ifndef CONFIG_NO_RADIUS
Hai Shalomc3565922019-10-28 11:58:20 -07001266
1267#ifdef CONFIG_SQLITE
1268 if (conf->radius_req_attr_sqlite) {
1269 if (sqlite3_open(conf->radius_req_attr_sqlite,
1270 &hapd->rad_attr_db)) {
1271 wpa_printf(MSG_ERROR, "Could not open SQLite file '%s'",
1272 conf->radius_req_attr_sqlite);
1273 return -1;
1274 }
1275
1276 wpa_printf(MSG_DEBUG, "Opening RADIUS attribute database: %s",
1277 conf->radius_req_attr_sqlite);
1278 if (!db_table_exists(hapd->rad_attr_db, "radius_attributes") &&
1279 db_table_create_radius_attributes(hapd->rad_attr_db) < 0)
1280 return -1;
1281 }
1282#endif /* CONFIG_SQLITE */
1283
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001284 hapd->radius = radius_client_init(hapd, conf->radius);
1285 if (hapd->radius == NULL) {
1286 wpa_printf(MSG_ERROR, "RADIUS client initialization failed.");
1287 return -1;
1288 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001289
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001290 if (conf->radius_das_port) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001291 struct radius_das_conf das_conf;
1292 os_memset(&das_conf, 0, sizeof(das_conf));
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001293 das_conf.port = conf->radius_das_port;
1294 das_conf.shared_secret = conf->radius_das_shared_secret;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001295 das_conf.shared_secret_len =
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001296 conf->radius_das_shared_secret_len;
1297 das_conf.client_addr = &conf->radius_das_client_addr;
1298 das_conf.time_window = conf->radius_das_time_window;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001299 das_conf.require_event_timestamp =
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001300 conf->radius_das_require_event_timestamp;
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07001301 das_conf.require_message_authenticator =
1302 conf->radius_das_require_message_authenticator;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001303 das_conf.ctx = hapd;
1304 das_conf.disconnect = hostapd_das_disconnect;
Roshan Pius3a1667e2018-07-03 15:17:14 -07001305 das_conf.coa = hostapd_das_coa;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001306 hapd->radius_das = radius_das_init(&das_conf);
1307 if (hapd->radius_das == NULL) {
1308 wpa_printf(MSG_ERROR, "RADIUS DAS initialization "
1309 "failed.");
1310 return -1;
1311 }
1312 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001313#endif /* CONFIG_NO_RADIUS */
1314
1315 if (hostapd_acl_init(hapd)) {
1316 wpa_printf(MSG_ERROR, "ACL initialization failed.");
1317 return -1;
1318 }
1319 if (hostapd_init_wps(hapd, conf))
1320 return -1;
1321
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001322#ifdef CONFIG_DPP
1323 hapd->gas = gas_query_ap_init(hapd, hapd->msg_ctx);
1324 if (!hapd->gas)
1325 return -1;
1326 if (hostapd_dpp_init(hapd))
1327 return -1;
1328#endif /* CONFIG_DPP */
1329
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001330 if (authsrv_init(hapd) < 0)
1331 return -1;
1332
1333 if (ieee802_1x_init(hapd)) {
1334 wpa_printf(MSG_ERROR, "IEEE 802.1X initialization failed.");
1335 return -1;
1336 }
1337
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001338 if ((conf->wpa || conf->osen) && hostapd_setup_wpa(hapd))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001339 return -1;
1340
1341 if (accounting_init(hapd)) {
1342 wpa_printf(MSG_ERROR, "Accounting initialization failed.");
1343 return -1;
1344 }
1345
Dmitry Shmidt04949592012-07-19 12:16:46 -07001346#ifdef CONFIG_INTERWORKING
1347 if (gas_serv_init(hapd)) {
1348 wpa_printf(MSG_ERROR, "GAS server initialization failed");
1349 return -1;
1350 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001351
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001352 if (conf->qos_map_set_len &&
1353 hostapd_drv_set_qos_map(hapd, conf->qos_map_set,
1354 conf->qos_map_set_len)) {
1355 wpa_printf(MSG_ERROR, "Failed to initialize QoS Map");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001356 return -1;
1357 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001358#endif /* CONFIG_INTERWORKING */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001359
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001360 if (conf->bss_load_update_period && bss_load_update_init(hapd)) {
1361 wpa_printf(MSG_ERROR, "BSS Load initialization failed");
1362 return -1;
1363 }
1364
1365 if (conf->proxy_arp) {
1366 if (x_snoop_init(hapd)) {
1367 wpa_printf(MSG_ERROR,
1368 "Generic snooping infrastructure initialization failed");
1369 return -1;
1370 }
1371
1372 if (dhcp_snoop_init(hapd)) {
1373 wpa_printf(MSG_ERROR,
1374 "DHCP snooping initialization failed");
1375 return -1;
1376 }
1377
1378 if (ndisc_snoop_init(hapd)) {
1379 wpa_printf(MSG_ERROR,
1380 "Neighbor Discovery snooping initialization failed");
1381 return -1;
1382 }
1383 }
1384
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001385 if (!hostapd_drv_none(hapd) && vlan_init(hapd)) {
1386 wpa_printf(MSG_ERROR, "VLAN initialization failed.");
1387 return -1;
1388 }
1389
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001390 if (!conf->start_disabled && ieee802_11_set_beacon(hapd) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001391 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001392
Hai Shalom4fbc08f2020-05-18 12:37:00 -07001393 if (flush_old_stations && !conf->start_disabled &&
1394 conf->broadcast_deauth) {
1395 u8 addr[ETH_ALEN];
1396
1397 /* Should any previously associated STA not have noticed that
1398 * the AP had stopped and restarted, send one more
1399 * deauthentication notification now that the AP is ready to
1400 * operate. */
1401 wpa_dbg(hapd->msg_ctx, MSG_DEBUG,
1402 "Deauthenticate all stations at BSS start");
1403 os_memset(addr, 0xff, ETH_ALEN);
1404 hostapd_drv_sta_deauth(hapd, addr,
1405 WLAN_REASON_PREV_AUTH_NOT_VALID);
1406 }
1407
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001408 if (hapd->wpa_auth && wpa_init_keys(hapd->wpa_auth) < 0)
1409 return -1;
1410
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001411 if (hapd->driver && hapd->driver->set_operstate)
1412 hapd->driver->set_operstate(hapd->drv_priv, 1);
1413
1414 return 0;
1415}
1416
1417
1418static void hostapd_tx_queue_params(struct hostapd_iface *iface)
1419{
1420 struct hostapd_data *hapd = iface->bss[0];
1421 int i;
1422 struct hostapd_tx_queue_params *p;
1423
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001424#ifdef CONFIG_MESH
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001425 if ((hapd->conf->mesh & MESH_ENABLED) && iface->mconf == NULL)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001426 return;
1427#endif /* CONFIG_MESH */
1428
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001429 for (i = 0; i < NUM_TX_QUEUES; i++) {
1430 p = &iface->conf->tx_queue[i];
1431
1432 if (hostapd_set_tx_queue_params(hapd, i, p->aifs, p->cwmin,
1433 p->cwmax, p->burst)) {
1434 wpa_printf(MSG_DEBUG, "Failed to set TX queue "
1435 "parameters for queue %d.", i);
1436 /* Continue anyway */
1437 }
1438 }
1439}
1440
1441
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001442static int hostapd_set_acl_list(struct hostapd_data *hapd,
1443 struct mac_acl_entry *mac_acl,
1444 int n_entries, u8 accept_acl)
1445{
1446 struct hostapd_acl_params *acl_params;
1447 int i, err;
1448
1449 acl_params = os_zalloc(sizeof(*acl_params) +
1450 (n_entries * sizeof(acl_params->mac_acl[0])));
1451 if (!acl_params)
1452 return -ENOMEM;
1453
1454 for (i = 0; i < n_entries; i++)
1455 os_memcpy(acl_params->mac_acl[i].addr, mac_acl[i].addr,
1456 ETH_ALEN);
1457
1458 acl_params->acl_policy = accept_acl;
1459 acl_params->num_mac_acl = n_entries;
1460
1461 err = hostapd_drv_set_acl(hapd, acl_params);
1462
1463 os_free(acl_params);
1464
1465 return err;
1466}
1467
1468
Sunil Ravia04bd252022-05-02 22:54:18 -07001469int hostapd_set_acl(struct hostapd_data *hapd)
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001470{
1471 struct hostapd_config *conf = hapd->iconf;
Sunil Ravia04bd252022-05-02 22:54:18 -07001472 int err = 0;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001473 u8 accept_acl;
1474
1475 if (hapd->iface->drv_max_acl_mac_addrs == 0)
Sunil Ravia04bd252022-05-02 22:54:18 -07001476 return 0;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001477
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001478 if (conf->bss[0]->macaddr_acl == DENY_UNLESS_ACCEPTED) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001479 accept_acl = 1;
1480 err = hostapd_set_acl_list(hapd, conf->bss[0]->accept_mac,
1481 conf->bss[0]->num_accept_mac,
1482 accept_acl);
1483 if (err) {
1484 wpa_printf(MSG_DEBUG, "Failed to set accept acl");
Sunil Ravia04bd252022-05-02 22:54:18 -07001485 return -1;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001486 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001487 } else if (conf->bss[0]->macaddr_acl == ACCEPT_UNLESS_DENIED) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001488 accept_acl = 0;
1489 err = hostapd_set_acl_list(hapd, conf->bss[0]->deny_mac,
1490 conf->bss[0]->num_deny_mac,
1491 accept_acl);
1492 if (err) {
1493 wpa_printf(MSG_DEBUG, "Failed to set deny acl");
Sunil Ravia04bd252022-05-02 22:54:18 -07001494 return -1;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001495 }
1496 }
Sunil Ravia04bd252022-05-02 22:54:18 -07001497 return err;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001498}
1499
1500
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001501static int start_ctrl_iface_bss(struct hostapd_data *hapd)
1502{
1503 if (!hapd->iface->interfaces ||
1504 !hapd->iface->interfaces->ctrl_iface_init)
1505 return 0;
1506
1507 if (hapd->iface->interfaces->ctrl_iface_init(hapd)) {
1508 wpa_printf(MSG_ERROR,
1509 "Failed to setup control interface for %s",
1510 hapd->conf->iface);
1511 return -1;
1512 }
1513
1514 return 0;
1515}
1516
1517
1518static int start_ctrl_iface(struct hostapd_iface *iface)
1519{
1520 size_t i;
1521
1522 if (!iface->interfaces || !iface->interfaces->ctrl_iface_init)
1523 return 0;
1524
1525 for (i = 0; i < iface->num_bss; i++) {
1526 struct hostapd_data *hapd = iface->bss[i];
1527 if (iface->interfaces->ctrl_iface_init(hapd)) {
1528 wpa_printf(MSG_ERROR,
1529 "Failed to setup control interface for %s",
1530 hapd->conf->iface);
1531 return -1;
1532 }
1533 }
1534
1535 return 0;
1536}
1537
1538
1539static void channel_list_update_timeout(void *eloop_ctx, void *timeout_ctx)
1540{
1541 struct hostapd_iface *iface = eloop_ctx;
1542
1543 if (!iface->wait_channel_update) {
1544 wpa_printf(MSG_INFO, "Channel list update timeout, but interface was not waiting for it");
1545 return;
1546 }
1547
1548 /*
1549 * It is possible that the existing channel list is acceptable, so try
1550 * to proceed.
1551 */
1552 wpa_printf(MSG_DEBUG, "Channel list update timeout - try to continue anyway");
1553 setup_interface2(iface);
1554}
1555
1556
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001557void hostapd_channel_list_updated(struct hostapd_iface *iface, int initiator)
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001558{
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001559 if (!iface->wait_channel_update || initiator != REGDOM_SET_BY_USER)
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001560 return;
1561
1562 wpa_printf(MSG_DEBUG, "Channel list updated - continue setup");
1563 eloop_cancel_timeout(channel_list_update_timeout, iface, NULL);
1564 setup_interface2(iface);
1565}
1566
1567
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001568static int setup_interface(struct hostapd_iface *iface)
1569{
1570 struct hostapd_data *hapd = iface->bss[0];
1571 size_t i;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001572
Dmitry Shmidta38abf92014-03-06 13:38:44 -08001573 /*
1574 * It is possible that setup_interface() is called after the interface
1575 * was disabled etc., in which case driver_ap_teardown is possibly set
1576 * to 1. Clear it here so any other key/station deletion, which is not
1577 * part of a teardown flow, would also call the relevant driver
1578 * callbacks.
1579 */
1580 iface->driver_ap_teardown = 0;
1581
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001582 if (!iface->phy[0]) {
1583 const char *phy = hostapd_drv_get_radio_name(hapd);
1584 if (phy) {
1585 wpa_printf(MSG_DEBUG, "phy: %s", phy);
1586 os_strlcpy(iface->phy, phy, sizeof(iface->phy));
1587 }
1588 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001589
1590 /*
1591 * Make sure that all BSSes get configured with a pointer to the same
1592 * driver interface.
1593 */
1594 for (i = 1; i < iface->num_bss; i++) {
1595 iface->bss[i]->driver = hapd->driver;
1596 iface->bss[i]->drv_priv = hapd->drv_priv;
1597 }
1598
1599 if (hostapd_validate_bssid_configuration(iface))
1600 return -1;
1601
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001602 /*
1603 * Initialize control interfaces early to allow external monitoring of
1604 * channel setup operations that may take considerable amount of time
1605 * especially for DFS cases.
1606 */
1607 if (start_ctrl_iface(iface))
1608 return -1;
1609
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001610 if (hapd->iconf->country[0] && hapd->iconf->country[1]) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001611 char country[4], previous_country[4];
1612
1613 hostapd_set_state(iface, HAPD_IFACE_COUNTRY_UPDATE);
1614 if (hostapd_get_country(hapd, previous_country) < 0)
1615 previous_country[0] = '\0';
1616
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001617 os_memcpy(country, hapd->iconf->country, 3);
1618 country[3] = '\0';
1619 if (hostapd_set_country(hapd, country) < 0) {
1620 wpa_printf(MSG_ERROR, "Failed to set country code");
1621 return -1;
1622 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001623
1624 wpa_printf(MSG_DEBUG, "Previous country code %s, new country code %s",
1625 previous_country, country);
1626
1627 if (os_strncmp(previous_country, country, 2) != 0) {
1628 wpa_printf(MSG_DEBUG, "Continue interface setup after channel list update");
1629 iface->wait_channel_update = 1;
Dmitry Shmidt97672262014-02-03 13:02:54 -08001630 eloop_register_timeout(5, 0,
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001631 channel_list_update_timeout,
1632 iface, NULL);
1633 return 0;
1634 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001635 }
1636
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001637 return setup_interface2(iface);
1638}
1639
1640
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001641static int configured_fixed_chan_to_freq(struct hostapd_iface *iface)
1642{
1643 int freq, i, j;
1644
1645 if (!iface->conf->channel)
1646 return 0;
1647 if (iface->conf->op_class) {
1648 freq = ieee80211_chan_to_freq(NULL, iface->conf->op_class,
1649 iface->conf->channel);
1650 if (freq < 0) {
1651 wpa_printf(MSG_INFO,
1652 "Could not convert op_class %u channel %u to operating frequency",
1653 iface->conf->op_class, iface->conf->channel);
1654 return -1;
1655 }
1656 iface->freq = freq;
1657 return 0;
1658 }
1659
1660 /* Old configurations using only 2.4/5/60 GHz bands may not specify the
1661 * op_class parameter. Select a matching channel from the configured
1662 * mode using the channel parameter for these cases.
1663 */
1664 for (j = 0; j < iface->num_hw_features; j++) {
1665 struct hostapd_hw_modes *mode = &iface->hw_features[j];
1666
1667 if (iface->conf->hw_mode != HOSTAPD_MODE_IEEE80211ANY &&
1668 iface->conf->hw_mode != mode->mode)
1669 continue;
1670 for (i = 0; i < mode->num_channels; i++) {
1671 struct hostapd_channel_data *chan = &mode->channels[i];
1672
1673 if (chan->chan == iface->conf->channel &&
1674 !is_6ghz_freq(chan->freq)) {
1675 iface->freq = chan->freq;
1676 return 0;
1677 }
1678 }
1679 }
1680
1681 wpa_printf(MSG_INFO, "Could not determine operating frequency");
1682 return -1;
1683}
1684
1685
Hai Shaloma20dcd72022-02-04 13:43:00 -08001686static void hostapd_set_6ghz_sec_chan(struct hostapd_iface *iface)
1687{
1688 int bw, seg0;
1689
1690 if (!is_6ghz_op_class(iface->conf->op_class))
1691 return;
1692
1693 seg0 = hostapd_get_oper_centr_freq_seg0_idx(iface->conf);
1694 bw = center_idx_to_bw_6ghz(seg0);
1695 /* Assign the secondary channel if absent in config for
1696 * bandwidths > 20 MHz */
1697 if (bw > 20 && !iface->conf->secondary_channel) {
1698 if (((iface->conf->channel - 1) / 4) % 2)
1699 iface->conf->secondary_channel = -1;
1700 else
1701 iface->conf->secondary_channel = 1;
1702 }
1703}
1704
1705
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001706static int setup_interface2(struct hostapd_iface *iface)
1707{
1708 iface->wait_channel_update = 0;
1709
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001710 if (hostapd_get_hw_features(iface)) {
1711 /* Not all drivers support this yet, so continue without hw
1712 * feature data. */
1713 } else {
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001714 int ret;
1715
1716 ret = configured_fixed_chan_to_freq(iface);
1717 if (ret < 0)
1718 goto fail;
1719
1720 if (iface->conf->op_class) {
1721 int ch_width;
1722
1723 ch_width = op_class_to_ch_width(iface->conf->op_class);
1724 hostapd_set_oper_chwidth(iface->conf, ch_width);
Hai Shaloma20dcd72022-02-04 13:43:00 -08001725 hostapd_set_6ghz_sec_chan(iface);
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001726 }
1727
1728 ret = hostapd_select_hw_mode(iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001729 if (ret < 0) {
1730 wpa_printf(MSG_ERROR, "Could not select hw_mode and "
1731 "channel. (%d)", ret);
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001732 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001733 }
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001734 if (ret == 1) {
1735 wpa_printf(MSG_DEBUG, "Interface initialization will be completed in a callback (ACS)");
1736 return 0;
1737 }
Hai Shalomc3565922019-10-28 11:58:20 -07001738 ret = hostapd_check_edmg_capab(iface);
1739 if (ret < 0)
1740 goto fail;
Hai Shalom60840252021-02-19 19:02:11 -08001741 ret = hostapd_check_he_6ghz_capab(iface);
1742 if (ret < 0)
1743 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001744 ret = hostapd_check_ht_capab(iface);
1745 if (ret < 0)
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001746 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001747 if (ret == 1) {
1748 wpa_printf(MSG_DEBUG, "Interface initialization will "
1749 "be completed in a callback");
1750 return 0;
1751 }
Dmitry Shmidt051af732013-10-22 13:52:46 -07001752
1753 if (iface->conf->ieee80211h)
1754 wpa_printf(MSG_DEBUG, "DFS support is enabled");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001755 }
1756 return hostapd_setup_interface_complete(iface, 0);
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001757
1758fail:
1759 hostapd_set_state(iface, HAPD_IFACE_DISABLED);
1760 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
1761 if (iface->interfaces && iface->interfaces->terminate_on_error)
1762 eloop_terminate();
1763 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001764}
1765
1766
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001767#ifdef CONFIG_FST
1768
1769static const u8 * fst_hostapd_get_bssid_cb(void *ctx)
1770{
1771 struct hostapd_data *hapd = ctx;
1772
1773 return hapd->own_addr;
1774}
1775
1776
1777static void fst_hostapd_get_channel_info_cb(void *ctx,
1778 enum hostapd_hw_mode *hw_mode,
1779 u8 *channel)
1780{
1781 struct hostapd_data *hapd = ctx;
1782
1783 *hw_mode = ieee80211_freq_to_chan(hapd->iface->freq, channel);
1784}
1785
1786
1787static void fst_hostapd_set_ies_cb(void *ctx, const struct wpabuf *fst_ies)
1788{
1789 struct hostapd_data *hapd = ctx;
1790
1791 if (hapd->iface->fst_ies != fst_ies) {
1792 hapd->iface->fst_ies = fst_ies;
1793 if (ieee802_11_set_beacon(hapd))
1794 wpa_printf(MSG_WARNING, "FST: Cannot set beacon");
1795 }
1796}
1797
1798
1799static int fst_hostapd_send_action_cb(void *ctx, const u8 *da,
1800 struct wpabuf *buf)
1801{
1802 struct hostapd_data *hapd = ctx;
1803
1804 return hostapd_drv_send_action(hapd, hapd->iface->freq, 0, da,
1805 wpabuf_head(buf), wpabuf_len(buf));
1806}
1807
1808
1809static const struct wpabuf * fst_hostapd_get_mb_ie_cb(void *ctx, const u8 *addr)
1810{
1811 struct hostapd_data *hapd = ctx;
1812 struct sta_info *sta = ap_get_sta(hapd, addr);
1813
1814 return sta ? sta->mb_ies : NULL;
1815}
1816
1817
1818static void fst_hostapd_update_mb_ie_cb(void *ctx, const u8 *addr,
1819 const u8 *buf, size_t size)
1820{
1821 struct hostapd_data *hapd = ctx;
1822 struct sta_info *sta = ap_get_sta(hapd, addr);
1823
1824 if (sta) {
1825 struct mb_ies_info info;
1826
1827 if (!mb_ies_info_by_ies(&info, buf, size)) {
1828 wpabuf_free(sta->mb_ies);
1829 sta->mb_ies = mb_ies_by_info(&info);
1830 }
1831 }
1832}
1833
1834
1835static const u8 * fst_hostapd_get_sta(struct fst_get_peer_ctx **get_ctx,
Hai Shalome21d4e82020-04-29 16:34:06 -07001836 bool mb_only)
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001837{
1838 struct sta_info *s = (struct sta_info *) *get_ctx;
1839
1840 if (mb_only) {
1841 for (; s && !s->mb_ies; s = s->next)
1842 ;
1843 }
1844
1845 if (s) {
1846 *get_ctx = (struct fst_get_peer_ctx *) s->next;
1847
1848 return s->addr;
1849 }
1850
1851 *get_ctx = NULL;
1852 return NULL;
1853}
1854
1855
1856static const u8 * fst_hostapd_get_peer_first(void *ctx,
1857 struct fst_get_peer_ctx **get_ctx,
Hai Shalome21d4e82020-04-29 16:34:06 -07001858 bool mb_only)
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001859{
1860 struct hostapd_data *hapd = ctx;
1861
1862 *get_ctx = (struct fst_get_peer_ctx *) hapd->sta_list;
1863
1864 return fst_hostapd_get_sta(get_ctx, mb_only);
1865}
1866
1867
1868static const u8 * fst_hostapd_get_peer_next(void *ctx,
1869 struct fst_get_peer_ctx **get_ctx,
Hai Shalome21d4e82020-04-29 16:34:06 -07001870 bool mb_only)
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001871{
1872 return fst_hostapd_get_sta(get_ctx, mb_only);
1873}
1874
1875
1876void fst_hostapd_fill_iface_obj(struct hostapd_data *hapd,
1877 struct fst_wpa_obj *iface_obj)
1878{
1879 iface_obj->ctx = hapd;
1880 iface_obj->get_bssid = fst_hostapd_get_bssid_cb;
1881 iface_obj->get_channel_info = fst_hostapd_get_channel_info_cb;
1882 iface_obj->set_ies = fst_hostapd_set_ies_cb;
1883 iface_obj->send_action = fst_hostapd_send_action_cb;
1884 iface_obj->get_mb_ie = fst_hostapd_get_mb_ie_cb;
1885 iface_obj->update_mb_ie = fst_hostapd_update_mb_ie_cb;
1886 iface_obj->get_peer_first = fst_hostapd_get_peer_first;
1887 iface_obj->get_peer_next = fst_hostapd_get_peer_next;
1888}
1889
1890#endif /* CONFIG_FST */
1891
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001892#ifdef CONFIG_OWE
1893
1894static int hostapd_owe_iface_iter(struct hostapd_iface *iface, void *ctx)
1895{
1896 struct hostapd_data *hapd = ctx;
1897 size_t i;
1898
1899 for (i = 0; i < iface->num_bss; i++) {
1900 struct hostapd_data *bss = iface->bss[i];
1901
1902 if (os_strcmp(hapd->conf->owe_transition_ifname,
1903 bss->conf->iface) != 0)
1904 continue;
1905
1906 wpa_printf(MSG_DEBUG,
1907 "OWE: ifname=%s found transition mode ifname=%s BSSID "
1908 MACSTR " SSID %s",
1909 hapd->conf->iface, bss->conf->iface,
1910 MAC2STR(bss->own_addr),
1911 wpa_ssid_txt(bss->conf->ssid.ssid,
1912 bss->conf->ssid.ssid_len));
1913 if (!bss->conf->ssid.ssid_set || !bss->conf->ssid.ssid_len ||
1914 is_zero_ether_addr(bss->own_addr))
1915 continue;
1916
1917 os_memcpy(hapd->conf->owe_transition_bssid, bss->own_addr,
1918 ETH_ALEN);
1919 os_memcpy(hapd->conf->owe_transition_ssid,
1920 bss->conf->ssid.ssid, bss->conf->ssid.ssid_len);
1921 hapd->conf->owe_transition_ssid_len = bss->conf->ssid.ssid_len;
1922 wpa_printf(MSG_DEBUG,
1923 "OWE: Copied transition mode information");
1924 return 1;
1925 }
1926
1927 return 0;
1928}
1929
1930
1931int hostapd_owe_trans_get_info(struct hostapd_data *hapd)
1932{
1933 if (hapd->conf->owe_transition_ssid_len > 0 &&
1934 !is_zero_ether_addr(hapd->conf->owe_transition_bssid))
1935 return 0;
1936
1937 /* Find transition mode SSID/BSSID information from a BSS operated by
1938 * this hostapd instance. */
1939 if (!hapd->iface->interfaces ||
1940 !hapd->iface->interfaces->for_each_interface)
1941 return hostapd_owe_iface_iter(hapd->iface, hapd);
1942 else
1943 return hapd->iface->interfaces->for_each_interface(
1944 hapd->iface->interfaces, hostapd_owe_iface_iter, hapd);
1945}
1946
1947
1948static int hostapd_owe_iface_iter2(struct hostapd_iface *iface, void *ctx)
1949{
1950 size_t i;
1951
1952 for (i = 0; i < iface->num_bss; i++) {
1953 struct hostapd_data *bss = iface->bss[i];
1954 int res;
1955
1956 if (!bss->conf->owe_transition_ifname[0])
1957 continue;
Hai Shalom899fcc72020-10-19 14:38:18 -07001958 if (bss->iface->state != HAPD_IFACE_ENABLED) {
1959 wpa_printf(MSG_DEBUG,
1960 "OWE: Interface %s state %s - defer beacon update",
1961 bss->conf->iface,
1962 hostapd_state_text(bss->iface->state));
1963 continue;
1964 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001965 res = hostapd_owe_trans_get_info(bss);
1966 if (res == 0)
1967 continue;
1968 wpa_printf(MSG_DEBUG,
1969 "OWE: Matching transition mode interface enabled - update beacon data for %s",
1970 bss->conf->iface);
1971 ieee802_11_set_beacon(bss);
1972 }
1973
1974 return 0;
1975}
1976
1977#endif /* CONFIG_OWE */
1978
1979
1980static void hostapd_owe_update_trans(struct hostapd_iface *iface)
1981{
1982#ifdef CONFIG_OWE
1983 /* Check whether the enabled BSS can complete OWE transition mode
1984 * configuration for any pending interface. */
1985 if (!iface->interfaces ||
1986 !iface->interfaces->for_each_interface)
1987 hostapd_owe_iface_iter2(iface, NULL);
1988 else
1989 iface->interfaces->for_each_interface(
1990 iface->interfaces, hostapd_owe_iface_iter2, NULL);
1991#endif /* CONFIG_OWE */
1992}
1993
1994
Roshan Pius3a1667e2018-07-03 15:17:14 -07001995static void hostapd_interface_setup_failure_handler(void *eloop_ctx,
1996 void *timeout_ctx)
1997{
1998 struct hostapd_iface *iface = eloop_ctx;
1999 struct hostapd_data *hapd;
2000
2001 if (iface->num_bss < 1 || !iface->bss || !iface->bss[0])
2002 return;
2003 hapd = iface->bss[0];
2004 if (hapd->setup_complete_cb)
2005 hapd->setup_complete_cb(hapd->setup_complete_cb_ctx);
2006}
2007
2008
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002009static int hostapd_setup_interface_complete_sync(struct hostapd_iface *iface,
2010 int err)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002011{
2012 struct hostapd_data *hapd = iface->bss[0];
2013 size_t j;
2014 u8 *prev_addr;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002015 int delay_apply_cfg = 0;
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08002016 int res_dfs_offload = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002017
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002018 if (err)
2019 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002020
2021 wpa_printf(MSG_DEBUG, "Completing interface initialization");
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08002022 if (iface->freq) {
Dmitry Shmidt051af732013-10-22 13:52:46 -07002023#ifdef NEED_AP_MLME
2024 int res;
2025#endif /* NEED_AP_MLME */
2026
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002027 wpa_printf(MSG_DEBUG, "Mode: %s Channel: %d "
2028 "Frequency: %d MHz",
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002029 hostapd_hw_mode_txt(iface->conf->hw_mode),
2030 iface->conf->channel, iface->freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002031
Dmitry Shmidt051af732013-10-22 13:52:46 -07002032#ifdef NEED_AP_MLME
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07002033 /* Handle DFS only if it is not offloaded to the driver */
2034 if (!(iface->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD)) {
2035 /* Check DFS */
2036 res = hostapd_handle_dfs(iface);
2037 if (res <= 0) {
2038 if (res < 0)
2039 goto fail;
2040 return res;
2041 }
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08002042 } else {
2043 /* If DFS is offloaded to the driver */
2044 res_dfs_offload = hostapd_handle_dfs_offload(iface);
2045 if (res_dfs_offload <= 0) {
2046 if (res_dfs_offload < 0)
2047 goto fail;
2048 } else {
2049 wpa_printf(MSG_DEBUG,
2050 "Proceed with AP/channel setup");
2051 /*
2052 * If this is a DFS channel, move to completing
2053 * AP setup.
2054 */
2055 if (res_dfs_offload == 1)
2056 goto dfs_offload;
2057 /* Otherwise fall through. */
2058 }
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002059 }
Dmitry Shmidt051af732013-10-22 13:52:46 -07002060#endif /* NEED_AP_MLME */
2061
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002062#ifdef CONFIG_MESH
2063 if (iface->mconf != NULL) {
2064 wpa_printf(MSG_DEBUG,
2065 "%s: Mesh configuration will be applied while joining the mesh network",
2066 iface->bss[0]->conf->iface);
2067 delay_apply_cfg = 1;
2068 }
2069#endif /* CONFIG_MESH */
2070
2071 if (!delay_apply_cfg &&
2072 hostapd_set_freq(hapd, hapd->iconf->hw_mode, iface->freq,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002073 hapd->iconf->channel,
Hai Shalomc3565922019-10-28 11:58:20 -07002074 hapd->iconf->enable_edmg,
2075 hapd->iconf->edmg_channel,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002076 hapd->iconf->ieee80211n,
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002077 hapd->iconf->ieee80211ac,
Hai Shalom81f62d82019-07-22 12:10:00 -07002078 hapd->iconf->ieee80211ax,
Sunil Ravia04bd252022-05-02 22:54:18 -07002079 hapd->iconf->ieee80211be,
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002080 hapd->iconf->secondary_channel,
Hai Shalom81f62d82019-07-22 12:10:00 -07002081 hostapd_get_oper_chwidth(hapd->iconf),
2082 hostapd_get_oper_centr_freq_seg0_idx(
2083 hapd->iconf),
2084 hostapd_get_oper_centr_freq_seg1_idx(
2085 hapd->iconf))) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002086 wpa_printf(MSG_ERROR, "Could not set channel for "
2087 "kernel driver");
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002088 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002089 }
2090 }
2091
2092 if (iface->current_mode) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002093 if (hostapd_prepare_rates(iface, iface->current_mode)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002094 wpa_printf(MSG_ERROR, "Failed to prepare rates "
2095 "table.");
2096 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
2097 HOSTAPD_LEVEL_WARNING,
2098 "Failed to prepare rates table.");
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002099 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002100 }
2101 }
2102
Hai Shalom021b0b52019-04-10 11:17:58 -07002103 if (hapd->iconf->rts_threshold >= -1 &&
2104 hostapd_set_rts(hapd, hapd->iconf->rts_threshold) &&
2105 hapd->iconf->rts_threshold >= -1) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002106 wpa_printf(MSG_ERROR, "Could not set RTS threshold for "
2107 "kernel driver");
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002108 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002109 }
2110
Hai Shalom021b0b52019-04-10 11:17:58 -07002111 if (hapd->iconf->fragm_threshold >= -1 &&
2112 hostapd_set_frag(hapd, hapd->iconf->fragm_threshold) &&
2113 hapd->iconf->fragm_threshold != -1) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002114 wpa_printf(MSG_ERROR, "Could not set fragmentation threshold "
2115 "for kernel driver");
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002116 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002117 }
2118
2119 prev_addr = hapd->own_addr;
2120
2121 for (j = 0; j < iface->num_bss; j++) {
2122 hapd = iface->bss[j];
2123 if (j)
2124 os_memcpy(hapd->own_addr, prev_addr, ETH_ALEN);
Dmitry Shmidt71757432014-06-02 13:50:35 -07002125 if (hostapd_setup_bss(hapd, j == 0)) {
Hai Shalom021b0b52019-04-10 11:17:58 -07002126 for (;;) {
Dmitry Shmidt71757432014-06-02 13:50:35 -07002127 hapd = iface->bss[j];
2128 hostapd_bss_deinit_no_free(hapd);
2129 hostapd_free_hapd_data(hapd);
Hai Shalom021b0b52019-04-10 11:17:58 -07002130 if (j == 0)
2131 break;
2132 j--;
2133 }
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002134 goto fail;
Dmitry Shmidt71757432014-06-02 13:50:35 -07002135 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002136 if (is_zero_ether_addr(hapd->conf->bssid))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002137 prev_addr = hapd->own_addr;
2138 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002139 hapd = iface->bss[0];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002140
2141 hostapd_tx_queue_params(iface);
2142
2143 ap_list_init(iface);
2144
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07002145 hostapd_set_acl(hapd);
2146
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002147 if (hostapd_driver_commit(hapd) < 0) {
2148 wpa_printf(MSG_ERROR, "%s: Failed to commit driver "
2149 "configuration", __func__);
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002150 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002151 }
2152
Jouni Malinen87fd2792011-05-16 18:35:42 +03002153 /*
2154 * WPS UPnP module can be initialized only when the "upnp_iface" is up.
2155 * If "interface" and "upnp_iface" are the same (e.g., non-bridge
2156 * mode), the interface is up only after driver_commit, so initialize
2157 * WPS after driver_commit.
2158 */
2159 for (j = 0; j < iface->num_bss; j++) {
2160 if (hostapd_init_wps_complete(iface->bss[j]))
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002161 goto fail;
Jouni Malinen87fd2792011-05-16 18:35:42 +03002162 }
2163
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08002164 if ((iface->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
2165 !res_dfs_offload) {
2166 /*
2167 * If freq is DFS, and DFS is offloaded to the driver, then wait
2168 * for CAC to complete.
2169 */
2170 wpa_printf(MSG_DEBUG, "%s: Wait for CAC to complete", __func__);
2171 return res_dfs_offload;
2172 }
2173
2174#ifdef NEED_AP_MLME
2175dfs_offload:
2176#endif /* NEED_AP_MLME */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002177
2178#ifdef CONFIG_FST
2179 if (hapd->iconf->fst_cfg.group_id[0]) {
2180 struct fst_wpa_obj iface_obj;
2181
2182 fst_hostapd_fill_iface_obj(hapd, &iface_obj);
2183 iface->fst = fst_attach(hapd->conf->iface, hapd->own_addr,
2184 &iface_obj, &hapd->iconf->fst_cfg);
2185 if (!iface->fst) {
2186 wpa_printf(MSG_ERROR, "Could not attach to FST %s",
2187 hapd->iconf->fst_cfg.group_id);
2188 goto fail;
2189 }
2190 }
2191#endif /* CONFIG_FST */
2192
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002193 hostapd_set_state(iface, HAPD_IFACE_ENABLED);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002194 hostapd_owe_update_trans(iface);
Hai Shalom81f62d82019-07-22 12:10:00 -07002195 airtime_policy_update_init(iface);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002196 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_ENABLED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002197 if (hapd->setup_complete_cb)
2198 hapd->setup_complete_cb(hapd->setup_complete_cb_ctx);
2199
Hai Shalom60840252021-02-19 19:02:11 -08002200#ifdef CONFIG_MESH
2201 if (delay_apply_cfg && !iface->mconf) {
2202 wpa_printf(MSG_ERROR, "Error while completing mesh init");
2203 goto fail;
2204 }
2205#endif /* CONFIG_MESH */
2206
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002207 wpa_printf(MSG_DEBUG, "%s: Setup of interface done.",
2208 iface->bss[0]->conf->iface);
Dmitry Shmidtb96dad42013-11-05 10:07:29 -08002209 if (iface->interfaces && iface->interfaces->terminate_on_error > 0)
2210 iface->interfaces->terminate_on_error--;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002211
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002212 for (j = 0; j < iface->num_bss; j++)
Hai Shalom74f70d42019-02-11 14:42:39 -08002213 hostapd_neighbor_set_own_report(iface->bss[j]);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002214
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002215 return 0;
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002216
2217fail:
2218 wpa_printf(MSG_ERROR, "Interface initialization failed");
2219 hostapd_set_state(iface, HAPD_IFACE_DISABLED);
2220 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002221#ifdef CONFIG_FST
2222 if (iface->fst) {
2223 fst_detach(iface->fst);
2224 iface->fst = NULL;
2225 }
2226#endif /* CONFIG_FST */
Roshan Pius3a1667e2018-07-03 15:17:14 -07002227
2228 if (iface->interfaces && iface->interfaces->terminate_on_error) {
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002229 eloop_terminate();
Roshan Pius3a1667e2018-07-03 15:17:14 -07002230 } else if (hapd->setup_complete_cb) {
2231 /*
2232 * Calling hapd->setup_complete_cb directly may cause iface
2233 * deinitialization which may be accessed later by the caller.
2234 */
2235 eloop_register_timeout(0, 0,
2236 hostapd_interface_setup_failure_handler,
2237 iface, NULL);
2238 }
2239
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002240 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002241}
2242
2243
2244/**
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002245 * hostapd_setup_interface_complete - Complete interface setup
2246 *
2247 * This function is called when previous steps in the interface setup has been
2248 * completed. This can also start operations, e.g., DFS, that will require
2249 * additional processing before interface is ready to be enabled. Such
2250 * operations will call this function from eloop callbacks when finished.
2251 */
2252int hostapd_setup_interface_complete(struct hostapd_iface *iface, int err)
2253{
2254 struct hapd_interfaces *interfaces = iface->interfaces;
2255 struct hostapd_data *hapd = iface->bss[0];
2256 unsigned int i;
2257 int not_ready_in_sync_ifaces = 0;
2258
2259 if (!iface->need_to_start_in_sync)
2260 return hostapd_setup_interface_complete_sync(iface, err);
2261
2262 if (err) {
2263 wpa_printf(MSG_ERROR, "Interface initialization failed");
2264 hostapd_set_state(iface, HAPD_IFACE_DISABLED);
2265 iface->need_to_start_in_sync = 0;
2266 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
2267 if (interfaces && interfaces->terminate_on_error)
2268 eloop_terminate();
2269 return -1;
2270 }
2271
2272 if (iface->ready_to_start_in_sync) {
2273 /* Already in ready and waiting. should never happpen */
2274 return 0;
2275 }
2276
2277 for (i = 0; i < interfaces->count; i++) {
2278 if (interfaces->iface[i]->need_to_start_in_sync &&
2279 !interfaces->iface[i]->ready_to_start_in_sync)
2280 not_ready_in_sync_ifaces++;
2281 }
2282
2283 /*
2284 * Check if this is the last interface, if yes then start all the other
2285 * waiting interfaces. If not, add this interface to the waiting list.
2286 */
2287 if (not_ready_in_sync_ifaces > 1 && iface->state == HAPD_IFACE_DFS) {
2288 /*
2289 * If this interface went through CAC, do not synchronize, just
2290 * start immediately.
2291 */
2292 iface->need_to_start_in_sync = 0;
2293 wpa_printf(MSG_INFO,
2294 "%s: Finished CAC - bypass sync and start interface",
2295 iface->bss[0]->conf->iface);
2296 return hostapd_setup_interface_complete_sync(iface, err);
2297 }
2298
2299 if (not_ready_in_sync_ifaces > 1) {
2300 /* need to wait as there are other interfaces still coming up */
2301 iface->ready_to_start_in_sync = 1;
2302 wpa_printf(MSG_INFO,
2303 "%s: Interface waiting to sync with other interfaces",
2304 iface->bss[0]->conf->iface);
2305 return 0;
2306 }
2307
2308 wpa_printf(MSG_INFO,
2309 "%s: Last interface to sync - starting all interfaces",
2310 iface->bss[0]->conf->iface);
2311 iface->need_to_start_in_sync = 0;
2312 hostapd_setup_interface_complete_sync(iface, err);
2313 for (i = 0; i < interfaces->count; i++) {
2314 if (interfaces->iface[i]->need_to_start_in_sync &&
2315 interfaces->iface[i]->ready_to_start_in_sync) {
2316 hostapd_setup_interface_complete_sync(
2317 interfaces->iface[i], 0);
2318 /* Only once the interfaces are sync started */
2319 interfaces->iface[i]->need_to_start_in_sync = 0;
2320 }
2321 }
2322
2323 return 0;
2324}
2325
2326
2327/**
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002328 * hostapd_setup_interface - Setup of an interface
2329 * @iface: Pointer to interface data.
2330 * Returns: 0 on success, -1 on failure
2331 *
2332 * Initializes the driver interface, validates the configuration,
2333 * and sets driver parameters based on the configuration.
2334 * Flushes old stations, sets the channel, encryption,
2335 * beacons, and WDS links based on the configuration.
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002336 *
2337 * If interface setup requires more time, e.g., to perform HT co-ex scans, ACS,
2338 * or DFS operations, this function returns 0 before such operations have been
2339 * completed. The pending operations are registered into eloop and will be
2340 * completed from eloop callbacks. Those callbacks end up calling
2341 * hostapd_setup_interface_complete() once setup has been completed.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002342 */
2343int hostapd_setup_interface(struct hostapd_iface *iface)
2344{
2345 int ret;
2346
Hai Shaloma20dcd72022-02-04 13:43:00 -08002347 if (!iface->conf)
2348 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002349 ret = setup_interface(iface);
2350 if (ret) {
2351 wpa_printf(MSG_ERROR, "%s: Unable to setup interface.",
Hai Shaloma20dcd72022-02-04 13:43:00 -08002352 iface->conf->bss[0]->iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002353 return -1;
2354 }
2355
2356 return 0;
2357}
2358
2359
2360/**
2361 * hostapd_alloc_bss_data - Allocate and initialize per-BSS data
2362 * @hapd_iface: Pointer to interface data
2363 * @conf: Pointer to per-interface configuration
2364 * @bss: Pointer to per-BSS configuration for this BSS
2365 * Returns: Pointer to allocated BSS data
2366 *
2367 * This function is used to allocate per-BSS data structure. This data will be
2368 * freed after hostapd_cleanup() is called for it during interface
2369 * deinitialization.
2370 */
2371struct hostapd_data *
2372hostapd_alloc_bss_data(struct hostapd_iface *hapd_iface,
2373 struct hostapd_config *conf,
2374 struct hostapd_bss_config *bss)
2375{
2376 struct hostapd_data *hapd;
2377
2378 hapd = os_zalloc(sizeof(*hapd));
2379 if (hapd == NULL)
2380 return NULL;
2381
2382 hapd->new_assoc_sta_cb = hostapd_new_assoc_sta;
2383 hapd->iconf = conf;
2384 hapd->conf = bss;
2385 hapd->iface = hapd_iface;
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08002386 if (conf)
2387 hapd->driver = conf->driver;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002388 hapd->ctrl_sock = -1;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08002389 dl_list_init(&hapd->ctrl_dst);
Dmitry Shmidt7d175302016-09-06 13:11:34 -07002390 dl_list_init(&hapd->nr_db);
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08002391 hapd->dhcp_sock = -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002392#ifdef CONFIG_IEEE80211R_AP
2393 dl_list_init(&hapd->l2_queue);
2394 dl_list_init(&hapd->l2_oui_queue);
2395#endif /* CONFIG_IEEE80211R_AP */
Hai Shalom021b0b52019-04-10 11:17:58 -07002396#ifdef CONFIG_SAE
2397 dl_list_init(&hapd->sae_commit_queue);
2398#endif /* CONFIG_SAE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002399
2400 return hapd;
2401}
2402
2403
Dmitry Shmidt54605472013-11-08 11:10:19 -08002404static void hostapd_bss_deinit(struct hostapd_data *hapd)
2405{
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07002406 if (!hapd)
2407 return;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002408 wpa_printf(MSG_DEBUG, "%s: deinit bss %s", __func__,
Hai Shalom021b0b52019-04-10 11:17:58 -07002409 hapd->conf ? hapd->conf->iface : "N/A");
Dmitry Shmidt71757432014-06-02 13:50:35 -07002410 hostapd_bss_deinit_no_free(hapd);
Dmitry Shmidtf73259c2015-03-17 11:00:54 -07002411 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
Hai Shalomc3565922019-10-28 11:58:20 -07002412#ifdef CONFIG_SQLITE
2413 if (hapd->rad_attr_db) {
2414 sqlite3_close(hapd->rad_attr_db);
2415 hapd->rad_attr_db = NULL;
2416 }
2417#endif /* CONFIG_SQLITE */
Dmitry Shmidt54605472013-11-08 11:10:19 -08002418 hostapd_cleanup(hapd);
2419}
2420
2421
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002422void hostapd_interface_deinit(struct hostapd_iface *iface)
2423{
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002424 int j;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002425
Dmitry Shmidt54605472013-11-08 11:10:19 -08002426 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002427 if (iface == NULL)
2428 return;
2429
Dmitry Shmidtf73259c2015-03-17 11:00:54 -07002430 hostapd_set_state(iface, HAPD_IFACE_DISABLED);
2431
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002432 eloop_cancel_timeout(channel_list_update_timeout, iface, NULL);
2433 iface->wait_channel_update = 0;
2434
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002435#ifdef CONFIG_FST
2436 if (iface->fst) {
2437 fst_detach(iface->fst);
2438 iface->fst = NULL;
2439 }
2440#endif /* CONFIG_FST */
2441
Hai Shalom021b0b52019-04-10 11:17:58 -07002442 for (j = (int) iface->num_bss - 1; j >= 0; j--) {
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07002443 if (!iface->bss)
2444 break;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002445 hostapd_bss_deinit(iface->bss[j]);
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07002446 }
Roshan Pius3a1667e2018-07-03 15:17:14 -07002447
Roshan Pius3a1667e2018-07-03 15:17:14 -07002448#ifdef NEED_AP_MLME
2449 hostapd_stop_setup_timers(iface);
2450 eloop_cancel_timeout(ap_ht2040_timeout, iface, NULL);
2451#endif /* NEED_AP_MLME */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002452}
2453
2454
2455void hostapd_interface_free(struct hostapd_iface *iface)
2456{
2457 size_t j;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002458 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
2459 for (j = 0; j < iface->num_bss; j++) {
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07002460 if (!iface->bss)
2461 break;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002462 wpa_printf(MSG_DEBUG, "%s: free hapd %p",
2463 __func__, iface->bss[j]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002464 os_free(iface->bss[j]);
Dmitry Shmidt54605472013-11-08 11:10:19 -08002465 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002466 hostapd_cleanup_iface(iface);
2467}
2468
2469
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07002470struct hostapd_iface * hostapd_alloc_iface(void)
2471{
2472 struct hostapd_iface *hapd_iface;
2473
2474 hapd_iface = os_zalloc(sizeof(*hapd_iface));
2475 if (!hapd_iface)
2476 return NULL;
2477
2478 dl_list_init(&hapd_iface->sta_seen);
2479
2480 return hapd_iface;
2481}
2482
2483
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002484/**
2485 * hostapd_init - Allocate and initialize per-interface data
2486 * @config_file: Path to the configuration file
2487 * Returns: Pointer to the allocated interface data or %NULL on failure
2488 *
2489 * This function is used to allocate main data structures for per-interface
2490 * data. The allocated data buffer will be freed by calling
2491 * hostapd_cleanup_iface().
2492 */
2493struct hostapd_iface * hostapd_init(struct hapd_interfaces *interfaces,
2494 const char *config_file)
2495{
2496 struct hostapd_iface *hapd_iface = NULL;
2497 struct hostapd_config *conf = NULL;
2498 struct hostapd_data *hapd;
2499 size_t i;
2500
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07002501 hapd_iface = hostapd_alloc_iface();
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002502 if (hapd_iface == NULL)
2503 goto fail;
2504
2505 hapd_iface->config_fname = os_strdup(config_file);
2506 if (hapd_iface->config_fname == NULL)
2507 goto fail;
2508
2509 conf = interfaces->config_read_cb(hapd_iface->config_fname);
2510 if (conf == NULL)
2511 goto fail;
2512 hapd_iface->conf = conf;
2513
2514 hapd_iface->num_bss = conf->num_bss;
2515 hapd_iface->bss = os_calloc(conf->num_bss,
2516 sizeof(struct hostapd_data *));
2517 if (hapd_iface->bss == NULL)
2518 goto fail;
2519
2520 for (i = 0; i < conf->num_bss; i++) {
2521 hapd = hapd_iface->bss[i] =
2522 hostapd_alloc_bss_data(hapd_iface, conf,
2523 conf->bss[i]);
2524 if (hapd == NULL)
2525 goto fail;
2526 hapd->msg_ctx = hapd;
2527 }
2528
2529 return hapd_iface;
2530
2531fail:
2532 wpa_printf(MSG_ERROR, "Failed to set up interface with %s",
2533 config_file);
2534 if (conf)
2535 hostapd_config_free(conf);
2536 if (hapd_iface) {
2537 os_free(hapd_iface->config_fname);
2538 os_free(hapd_iface->bss);
Dmitry Shmidt54605472013-11-08 11:10:19 -08002539 wpa_printf(MSG_DEBUG, "%s: free iface %p",
2540 __func__, hapd_iface);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002541 os_free(hapd_iface);
2542 }
2543 return NULL;
2544}
2545
2546
2547static int ifname_in_use(struct hapd_interfaces *interfaces, const char *ifname)
2548{
2549 size_t i, j;
2550
2551 for (i = 0; i < interfaces->count; i++) {
2552 struct hostapd_iface *iface = interfaces->iface[i];
2553 for (j = 0; j < iface->num_bss; j++) {
2554 struct hostapd_data *hapd = iface->bss[j];
2555 if (os_strcmp(ifname, hapd->conf->iface) == 0)
2556 return 1;
2557 }
2558 }
2559
2560 return 0;
2561}
2562
2563
2564/**
2565 * hostapd_interface_init_bss - Read configuration file and init BSS data
2566 *
2567 * This function is used to parse configuration file for a BSS. This BSS is
2568 * added to an existing interface sharing the same radio (if any) or a new
2569 * interface is created if this is the first interface on a radio. This
2570 * allocate memory for the BSS. No actual driver operations are started.
2571 *
2572 * This is similar to hostapd_interface_init(), but for a case where the
2573 * configuration is used to add a single BSS instead of all BSSes for a radio.
2574 */
2575struct hostapd_iface *
2576hostapd_interface_init_bss(struct hapd_interfaces *interfaces, const char *phy,
2577 const char *config_fname, int debug)
2578{
2579 struct hostapd_iface *new_iface = NULL, *iface = NULL;
2580 struct hostapd_data *hapd;
2581 int k;
2582 size_t i, bss_idx;
2583
2584 if (!phy || !*phy)
2585 return NULL;
2586
2587 for (i = 0; i < interfaces->count; i++) {
2588 if (os_strcmp(interfaces->iface[i]->phy, phy) == 0) {
2589 iface = interfaces->iface[i];
2590 break;
2591 }
2592 }
2593
2594 wpa_printf(MSG_INFO, "Configuration file: %s (phy %s)%s",
2595 config_fname, phy, iface ? "" : " --> new PHY");
2596 if (iface) {
2597 struct hostapd_config *conf;
2598 struct hostapd_bss_config **tmp_conf;
2599 struct hostapd_data **tmp_bss;
2600 struct hostapd_bss_config *bss;
2601 const char *ifname;
2602
2603 /* Add new BSS to existing iface */
2604 conf = interfaces->config_read_cb(config_fname);
2605 if (conf == NULL)
2606 return NULL;
2607 if (conf->num_bss > 1) {
2608 wpa_printf(MSG_ERROR, "Multiple BSSes specified in BSS-config");
2609 hostapd_config_free(conf);
2610 return NULL;
2611 }
2612
2613 ifname = conf->bss[0]->iface;
2614 if (ifname[0] != '\0' && ifname_in_use(interfaces, ifname)) {
2615 wpa_printf(MSG_ERROR,
2616 "Interface name %s already in use", ifname);
2617 hostapd_config_free(conf);
2618 return NULL;
2619 }
2620
2621 tmp_conf = os_realloc_array(
2622 iface->conf->bss, iface->conf->num_bss + 1,
2623 sizeof(struct hostapd_bss_config *));
2624 tmp_bss = os_realloc_array(iface->bss, iface->num_bss + 1,
2625 sizeof(struct hostapd_data *));
2626 if (tmp_bss)
2627 iface->bss = tmp_bss;
2628 if (tmp_conf) {
2629 iface->conf->bss = tmp_conf;
2630 iface->conf->last_bss = tmp_conf[0];
2631 }
2632 if (tmp_bss == NULL || tmp_conf == NULL) {
2633 hostapd_config_free(conf);
2634 return NULL;
2635 }
2636 bss = iface->conf->bss[iface->conf->num_bss] = conf->bss[0];
2637 iface->conf->num_bss++;
2638
2639 hapd = hostapd_alloc_bss_data(iface, iface->conf, bss);
2640 if (hapd == NULL) {
2641 iface->conf->num_bss--;
2642 hostapd_config_free(conf);
2643 return NULL;
2644 }
2645 iface->conf->last_bss = bss;
2646 iface->bss[iface->num_bss] = hapd;
2647 hapd->msg_ctx = hapd;
2648
2649 bss_idx = iface->num_bss++;
2650 conf->num_bss--;
2651 conf->bss[0] = NULL;
2652 hostapd_config_free(conf);
2653 } else {
2654 /* Add a new iface with the first BSS */
2655 new_iface = iface = hostapd_init(interfaces, config_fname);
2656 if (!iface)
2657 return NULL;
2658 os_strlcpy(iface->phy, phy, sizeof(iface->phy));
2659 iface->interfaces = interfaces;
2660 bss_idx = 0;
2661 }
2662
2663 for (k = 0; k < debug; k++) {
2664 if (iface->bss[bss_idx]->conf->logger_stdout_level > 0)
2665 iface->bss[bss_idx]->conf->logger_stdout_level--;
2666 }
2667
2668 if (iface->conf->bss[bss_idx]->iface[0] == '\0' &&
2669 !hostapd_drv_none(iface->bss[bss_idx])) {
2670 wpa_printf(MSG_ERROR, "Interface name not specified in %s",
2671 config_fname);
2672 if (new_iface)
2673 hostapd_interface_deinit_free(new_iface);
2674 return NULL;
2675 }
2676
2677 return iface;
2678}
2679
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002680
2681void hostapd_interface_deinit_free(struct hostapd_iface *iface)
2682{
2683 const struct wpa_driver_ops *driver;
2684 void *drv_priv;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002685
2686 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002687 if (iface == NULL)
2688 return;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002689 wpa_printf(MSG_DEBUG, "%s: num_bss=%u conf->num_bss=%u",
2690 __func__, (unsigned int) iface->num_bss,
2691 (unsigned int) iface->conf->num_bss);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002692 driver = iface->bss[0]->driver;
2693 drv_priv = iface->bss[0]->drv_priv;
2694 hostapd_interface_deinit(iface);
Dmitry Shmidt54605472013-11-08 11:10:19 -08002695 wpa_printf(MSG_DEBUG, "%s: driver=%p drv_priv=%p -> hapd_deinit",
2696 __func__, driver, drv_priv);
Dmitry Shmidt71757432014-06-02 13:50:35 -07002697 if (driver && driver->hapd_deinit && drv_priv) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002698 driver->hapd_deinit(drv_priv);
Dmitry Shmidt71757432014-06-02 13:50:35 -07002699 iface->bss[0]->drv_priv = NULL;
2700 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002701 hostapd_interface_free(iface);
2702}
2703
2704
Dmitry Shmidt15907092014-03-25 10:42:57 -07002705static void hostapd_deinit_driver(const struct wpa_driver_ops *driver,
2706 void *drv_priv,
2707 struct hostapd_iface *hapd_iface)
2708{
2709 size_t j;
2710
2711 wpa_printf(MSG_DEBUG, "%s: driver=%p drv_priv=%p -> hapd_deinit",
2712 __func__, driver, drv_priv);
2713 if (driver && driver->hapd_deinit && drv_priv) {
2714 driver->hapd_deinit(drv_priv);
2715 for (j = 0; j < hapd_iface->num_bss; j++) {
2716 wpa_printf(MSG_DEBUG, "%s:bss[%d]->drv_priv=%p",
2717 __func__, (int) j,
2718 hapd_iface->bss[j]->drv_priv);
Hai Shalom1dc4d202019-04-29 16:22:27 -07002719 if (hapd_iface->bss[j]->drv_priv == drv_priv) {
Dmitry Shmidt15907092014-03-25 10:42:57 -07002720 hapd_iface->bss[j]->drv_priv = NULL;
Hai Shalom1dc4d202019-04-29 16:22:27 -07002721 hapd_iface->extended_capa = NULL;
2722 hapd_iface->extended_capa_mask = NULL;
2723 hapd_iface->extended_capa_len = 0;
2724 }
Dmitry Shmidt15907092014-03-25 10:42:57 -07002725 }
2726 }
2727}
2728
2729
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002730int hostapd_enable_iface(struct hostapd_iface *hapd_iface)
2731{
Dmitry Shmidt71757432014-06-02 13:50:35 -07002732 size_t j;
2733
Hai Shalom60840252021-02-19 19:02:11 -08002734 if (!hapd_iface)
2735 return -1;
2736
2737 if (hapd_iface->enable_iface_cb)
2738 return hapd_iface->enable_iface_cb(hapd_iface);
2739
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002740 if (hapd_iface->bss[0]->drv_priv != NULL) {
2741 wpa_printf(MSG_ERROR, "Interface %s already enabled",
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002742 hapd_iface->conf->bss[0]->iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002743 return -1;
2744 }
2745
2746 wpa_printf(MSG_DEBUG, "Enable interface %s",
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002747 hapd_iface->conf->bss[0]->iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002748
Dmitry Shmidt71757432014-06-02 13:50:35 -07002749 for (j = 0; j < hapd_iface->num_bss; j++)
2750 hostapd_set_security_params(hapd_iface->conf->bss[j], 1);
Dmitry Shmidt344abd32014-01-14 13:17:00 -08002751 if (hostapd_config_check(hapd_iface->conf, 1) < 0) {
2752 wpa_printf(MSG_INFO, "Invalid configuration - cannot enable");
2753 return -1;
2754 }
2755
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002756 if (hapd_iface->interfaces == NULL ||
2757 hapd_iface->interfaces->driver_init == NULL ||
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002758 hapd_iface->interfaces->driver_init(hapd_iface))
2759 return -1;
2760
2761 if (hostapd_setup_interface(hapd_iface)) {
Dmitry Shmidt15907092014-03-25 10:42:57 -07002762 hostapd_deinit_driver(hapd_iface->bss[0]->driver,
2763 hapd_iface->bss[0]->drv_priv,
2764 hapd_iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002765 return -1;
2766 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002767
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002768 return 0;
2769}
2770
2771
2772int hostapd_reload_iface(struct hostapd_iface *hapd_iface)
2773{
2774 size_t j;
2775
2776 wpa_printf(MSG_DEBUG, "Reload interface %s",
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002777 hapd_iface->conf->bss[0]->iface);
2778 for (j = 0; j < hapd_iface->num_bss; j++)
Dmitry Shmidt71757432014-06-02 13:50:35 -07002779 hostapd_set_security_params(hapd_iface->conf->bss[j], 1);
Dmitry Shmidt344abd32014-01-14 13:17:00 -08002780 if (hostapd_config_check(hapd_iface->conf, 1) < 0) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002781 wpa_printf(MSG_ERROR, "Updated configuration is invalid");
2782 return -1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002783 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002784 hostapd_clear_old(hapd_iface);
2785 for (j = 0; j < hapd_iface->num_bss; j++)
2786 hostapd_reload_bss(hapd_iface->bss[j]);
2787
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002788 return 0;
2789}
2790
2791
2792int hostapd_disable_iface(struct hostapd_iface *hapd_iface)
2793{
2794 size_t j;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002795 const struct wpa_driver_ops *driver;
2796 void *drv_priv;
2797
2798 if (hapd_iface == NULL)
2799 return -1;
Dmitry Shmidt71757432014-06-02 13:50:35 -07002800
Hai Shalom60840252021-02-19 19:02:11 -08002801 if (hapd_iface->disable_iface_cb)
2802 return hapd_iface->disable_iface_cb(hapd_iface);
2803
Dmitry Shmidt71757432014-06-02 13:50:35 -07002804 if (hapd_iface->bss[0]->drv_priv == NULL) {
2805 wpa_printf(MSG_INFO, "Interface %s already disabled",
2806 hapd_iface->conf->bss[0]->iface);
2807 return -1;
2808 }
2809
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002810 wpa_msg(hapd_iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002811 driver = hapd_iface->bss[0]->driver;
2812 drv_priv = hapd_iface->bss[0]->drv_priv;
2813
Dmitry Shmidta38abf92014-03-06 13:38:44 -08002814 hapd_iface->driver_ap_teardown =
2815 !!(hapd_iface->drv_flags &
2816 WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
2817
Hai Shalom39ba6fc2019-01-22 12:40:38 -08002818#ifdef NEED_AP_MLME
2819 for (j = 0; j < hapd_iface->num_bss; j++)
2820 hostapd_cleanup_cs_params(hapd_iface->bss[j]);
2821#endif /* NEED_AP_MLME */
2822
Dmitry Shmidta38abf92014-03-06 13:38:44 -08002823 /* same as hostapd_interface_deinit without deinitializing ctrl-iface */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002824 for (j = 0; j < hapd_iface->num_bss; j++) {
2825 struct hostapd_data *hapd = hapd_iface->bss[j];
Dmitry Shmidt71757432014-06-02 13:50:35 -07002826 hostapd_bss_deinit_no_free(hapd);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002827 hostapd_free_hapd_data(hapd);
2828 }
2829
Dmitry Shmidt15907092014-03-25 10:42:57 -07002830 hostapd_deinit_driver(driver, drv_priv, hapd_iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002831
2832 /* From hostapd_cleanup_iface: These were initialized in
2833 * hostapd_setup_interface and hostapd_setup_interface_complete
2834 */
2835 hostapd_cleanup_iface_partial(hapd_iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002836
Dmitry Shmidt56052862013-10-04 10:23:25 -07002837 wpa_printf(MSG_DEBUG, "Interface %s disabled",
2838 hapd_iface->bss[0]->conf->iface);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002839 hostapd_set_state(hapd_iface, HAPD_IFACE_DISABLED);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002840 return 0;
2841}
2842
2843
2844static struct hostapd_iface *
2845hostapd_iface_alloc(struct hapd_interfaces *interfaces)
2846{
2847 struct hostapd_iface **iface, *hapd_iface;
2848
2849 iface = os_realloc_array(interfaces->iface, interfaces->count + 1,
2850 sizeof(struct hostapd_iface *));
2851 if (iface == NULL)
2852 return NULL;
2853 interfaces->iface = iface;
2854 hapd_iface = interfaces->iface[interfaces->count] =
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07002855 hostapd_alloc_iface();
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002856 if (hapd_iface == NULL) {
2857 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory for "
2858 "the interface", __func__);
2859 return NULL;
2860 }
2861 interfaces->count++;
2862 hapd_iface->interfaces = interfaces;
2863
2864 return hapd_iface;
2865}
2866
2867
2868static struct hostapd_config *
2869hostapd_config_alloc(struct hapd_interfaces *interfaces, const char *ifname,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002870 const char *ctrl_iface, const char *driver)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002871{
2872 struct hostapd_bss_config *bss;
2873 struct hostapd_config *conf;
2874
2875 /* Allocates memory for bss and conf */
2876 conf = hostapd_config_defaults();
2877 if (conf == NULL) {
2878 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory for "
2879 "configuration", __func__);
Hai Shalom74f70d42019-02-11 14:42:39 -08002880 return NULL;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002881 }
2882
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002883 if (driver) {
2884 int j;
2885
2886 for (j = 0; wpa_drivers[j]; j++) {
2887 if (os_strcmp(driver, wpa_drivers[j]->name) == 0) {
2888 conf->driver = wpa_drivers[j];
2889 goto skip;
2890 }
2891 }
2892
2893 wpa_printf(MSG_ERROR,
2894 "Invalid/unknown driver '%s' - registering the default driver",
2895 driver);
2896 }
2897
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002898 conf->driver = wpa_drivers[0];
2899 if (conf->driver == NULL) {
2900 wpa_printf(MSG_ERROR, "No driver wrappers registered!");
2901 hostapd_config_free(conf);
2902 return NULL;
2903 }
2904
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002905skip:
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002906 bss = conf->last_bss = conf->bss[0];
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002907
2908 os_strlcpy(bss->iface, ifname, sizeof(bss->iface));
2909 bss->ctrl_interface = os_strdup(ctrl_iface);
2910 if (bss->ctrl_interface == NULL) {
2911 hostapd_config_free(conf);
2912 return NULL;
2913 }
2914
2915 /* Reading configuration file skipped, will be done in SET!
2916 * From reading the configuration till the end has to be done in
2917 * SET
2918 */
2919 return conf;
2920}
2921
2922
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002923static int hostapd_data_alloc(struct hostapd_iface *hapd_iface,
2924 struct hostapd_config *conf)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002925{
2926 size_t i;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002927 struct hostapd_data *hapd;
2928
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002929 hapd_iface->bss = os_calloc(conf->num_bss,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002930 sizeof(struct hostapd_data *));
2931 if (hapd_iface->bss == NULL)
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002932 return -1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002933
2934 for (i = 0; i < conf->num_bss; i++) {
2935 hapd = hapd_iface->bss[i] =
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002936 hostapd_alloc_bss_data(hapd_iface, conf, conf->bss[i]);
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002937 if (hapd == NULL) {
2938 while (i > 0) {
2939 i--;
2940 os_free(hapd_iface->bss[i]);
2941 hapd_iface->bss[i] = NULL;
2942 }
2943 os_free(hapd_iface->bss);
2944 hapd_iface->bss = NULL;
2945 return -1;
2946 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002947 hapd->msg_ctx = hapd;
2948 }
2949
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002950 hapd_iface->conf = conf;
2951 hapd_iface->num_bss = conf->num_bss;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002952
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002953 return 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002954}
2955
2956
2957int hostapd_add_iface(struct hapd_interfaces *interfaces, char *buf)
2958{
2959 struct hostapd_config *conf = NULL;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002960 struct hostapd_iface *hapd_iface = NULL, *new_iface = NULL;
2961 struct hostapd_data *hapd;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002962 char *ptr;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002963 size_t i, j;
2964 const char *conf_file = NULL, *phy_name = NULL;
2965
2966 if (os_strncmp(buf, "bss_config=", 11) == 0) {
2967 char *pos;
2968 phy_name = buf + 11;
2969 pos = os_strchr(phy_name, ':');
2970 if (!pos)
2971 return -1;
2972 *pos++ = '\0';
2973 conf_file = pos;
2974 if (!os_strlen(conf_file))
2975 return -1;
2976
2977 hapd_iface = hostapd_interface_init_bss(interfaces, phy_name,
2978 conf_file, 0);
2979 if (!hapd_iface)
2980 return -1;
2981 for (j = 0; j < interfaces->count; j++) {
2982 if (interfaces->iface[j] == hapd_iface)
2983 break;
2984 }
2985 if (j == interfaces->count) {
2986 struct hostapd_iface **tmp;
2987 tmp = os_realloc_array(interfaces->iface,
2988 interfaces->count + 1,
2989 sizeof(struct hostapd_iface *));
2990 if (!tmp) {
2991 hostapd_interface_deinit_free(hapd_iface);
2992 return -1;
2993 }
2994 interfaces->iface = tmp;
2995 interfaces->iface[interfaces->count++] = hapd_iface;
2996 new_iface = hapd_iface;
2997 }
2998
2999 if (new_iface) {
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003000 if (interfaces->driver_init(hapd_iface))
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003001 goto fail;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003002
3003 if (hostapd_setup_interface(hapd_iface)) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003004 hostapd_deinit_driver(
3005 hapd_iface->bss[0]->driver,
3006 hapd_iface->bss[0]->drv_priv,
3007 hapd_iface);
3008 goto fail;
3009 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003010 } else {
3011 /* Assign new BSS with bss[0]'s driver info */
3012 hapd = hapd_iface->bss[hapd_iface->num_bss - 1];
3013 hapd->driver = hapd_iface->bss[0]->driver;
3014 hapd->drv_priv = hapd_iface->bss[0]->drv_priv;
3015 os_memcpy(hapd->own_addr, hapd_iface->bss[0]->own_addr,
3016 ETH_ALEN);
3017
3018 if (start_ctrl_iface_bss(hapd) < 0 ||
Dmitry Shmidt54605472013-11-08 11:10:19 -08003019 (hapd_iface->state == HAPD_IFACE_ENABLED &&
3020 hostapd_setup_bss(hapd, -1))) {
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07003021 hostapd_cleanup(hapd);
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08003022 hapd_iface->bss[hapd_iface->num_bss - 1] = NULL;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003023 hapd_iface->conf->num_bss--;
3024 hapd_iface->num_bss--;
Dmitry Shmidt54605472013-11-08 11:10:19 -08003025 wpa_printf(MSG_DEBUG, "%s: free hapd %p %s",
3026 __func__, hapd, hapd->conf->iface);
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003027 hostapd_config_free_bss(hapd->conf);
3028 hapd->conf = NULL;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003029 os_free(hapd);
3030 return -1;
3031 }
3032 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003033 hostapd_owe_update_trans(hapd_iface);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003034 return 0;
3035 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003036
3037 ptr = os_strchr(buf, ' ');
3038 if (ptr == NULL)
3039 return -1;
3040 *ptr++ = '\0';
3041
Dmitry Shmidt56052862013-10-04 10:23:25 -07003042 if (os_strncmp(ptr, "config=", 7) == 0)
3043 conf_file = ptr + 7;
3044
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003045 for (i = 0; i < interfaces->count; i++) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003046 if (!os_strcmp(interfaces->iface[i]->conf->bss[0]->iface,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003047 buf)) {
3048 wpa_printf(MSG_INFO, "Cannot add interface - it "
3049 "already exists");
3050 return -1;
3051 }
3052 }
3053
3054 hapd_iface = hostapd_iface_alloc(interfaces);
3055 if (hapd_iface == NULL) {
3056 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
3057 "for interface", __func__);
3058 goto fail;
3059 }
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003060 new_iface = hapd_iface;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003061
Dmitry Shmidt56052862013-10-04 10:23:25 -07003062 if (conf_file && interfaces->config_read_cb) {
3063 conf = interfaces->config_read_cb(conf_file);
3064 if (conf && conf->bss)
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003065 os_strlcpy(conf->bss[0]->iface, buf,
3066 sizeof(conf->bss[0]->iface));
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003067 } else {
3068 char *driver = os_strchr(ptr, ' ');
3069
3070 if (driver)
3071 *driver++ = '\0';
3072 conf = hostapd_config_alloc(interfaces, buf, ptr, driver);
3073 }
3074
Dmitry Shmidt56052862013-10-04 10:23:25 -07003075 if (conf == NULL || conf->bss == NULL) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003076 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
3077 "for configuration", __func__);
3078 goto fail;
3079 }
3080
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003081 if (hostapd_data_alloc(hapd_iface, conf) < 0) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003082 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
3083 "for hostapd", __func__);
3084 goto fail;
3085 }
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003086 conf = NULL;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003087
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003088 if (start_ctrl_iface(hapd_iface) < 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003089 goto fail;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003090
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003091 wpa_printf(MSG_INFO, "Add interface '%s'",
3092 hapd_iface->conf->bss[0]->iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003093
3094 return 0;
3095
3096fail:
3097 if (conf)
3098 hostapd_config_free(conf);
3099 if (hapd_iface) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003100 if (hapd_iface->bss) {
Dmitry Shmidt54605472013-11-08 11:10:19 -08003101 for (i = 0; i < hapd_iface->num_bss; i++) {
3102 hapd = hapd_iface->bss[i];
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08003103 if (!hapd)
3104 continue;
3105 if (hapd_iface->interfaces &&
Dmitry Shmidt54605472013-11-08 11:10:19 -08003106 hapd_iface->interfaces->ctrl_iface_deinit)
3107 hapd_iface->interfaces->
3108 ctrl_iface_deinit(hapd);
3109 wpa_printf(MSG_DEBUG, "%s: free hapd %p (%s)",
3110 __func__, hapd_iface->bss[i],
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08003111 hapd->conf->iface);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003112 hostapd_cleanup(hapd);
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08003113 os_free(hapd);
3114 hapd_iface->bss[i] = NULL;
Dmitry Shmidt54605472013-11-08 11:10:19 -08003115 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003116 os_free(hapd_iface->bss);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003117 hapd_iface->bss = NULL;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003118 }
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003119 if (new_iface) {
3120 interfaces->count--;
3121 interfaces->iface[interfaces->count] = NULL;
3122 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003123 hostapd_cleanup_iface(hapd_iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003124 }
3125 return -1;
3126}
3127
3128
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003129static int hostapd_remove_bss(struct hostapd_iface *iface, unsigned int idx)
3130{
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003131 size_t i;
3132
Dmitry Shmidt54605472013-11-08 11:10:19 -08003133 wpa_printf(MSG_INFO, "Remove BSS '%s'", iface->conf->bss[idx]->iface);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003134
Dmitry Shmidt54605472013-11-08 11:10:19 -08003135 /* Remove hostapd_data only if it has already been initialized */
3136 if (idx < iface->num_bss) {
3137 struct hostapd_data *hapd = iface->bss[idx];
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003138
Dmitry Shmidt54605472013-11-08 11:10:19 -08003139 hostapd_bss_deinit(hapd);
3140 wpa_printf(MSG_DEBUG, "%s: free hapd %p (%s)",
3141 __func__, hapd, hapd->conf->iface);
3142 hostapd_config_free_bss(hapd->conf);
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003143 hapd->conf = NULL;
Dmitry Shmidt54605472013-11-08 11:10:19 -08003144 os_free(hapd);
3145
3146 iface->num_bss--;
3147
3148 for (i = idx; i < iface->num_bss; i++)
3149 iface->bss[i] = iface->bss[i + 1];
3150 } else {
3151 hostapd_config_free_bss(iface->conf->bss[idx]);
3152 iface->conf->bss[idx] = NULL;
3153 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003154
3155 iface->conf->num_bss--;
Dmitry Shmidt54605472013-11-08 11:10:19 -08003156 for (i = idx; i < iface->conf->num_bss; i++)
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003157 iface->conf->bss[i] = iface->conf->bss[i + 1];
3158
3159 return 0;
3160}
3161
3162
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003163int hostapd_remove_iface(struct hapd_interfaces *interfaces, char *buf)
3164{
3165 struct hostapd_iface *hapd_iface;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003166 size_t i, j, k = 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003167
3168 for (i = 0; i < interfaces->count; i++) {
3169 hapd_iface = interfaces->iface[i];
3170 if (hapd_iface == NULL)
3171 return -1;
Dmitry Shmidt54605472013-11-08 11:10:19 -08003172 if (!os_strcmp(hapd_iface->conf->bss[0]->iface, buf)) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003173 wpa_printf(MSG_INFO, "Remove interface '%s'", buf);
Dmitry Shmidta38abf92014-03-06 13:38:44 -08003174 hapd_iface->driver_ap_teardown =
3175 !!(hapd_iface->drv_flags &
3176 WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
3177
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003178 hostapd_interface_deinit_free(hapd_iface);
3179 k = i;
3180 while (k < (interfaces->count - 1)) {
3181 interfaces->iface[k] =
3182 interfaces->iface[k + 1];
3183 k++;
3184 }
3185 interfaces->count--;
3186 return 0;
3187 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003188
3189 for (j = 0; j < hapd_iface->conf->num_bss; j++) {
Dmitry Shmidta38abf92014-03-06 13:38:44 -08003190 if (!os_strcmp(hapd_iface->conf->bss[j]->iface, buf)) {
3191 hapd_iface->driver_ap_teardown =
3192 !(hapd_iface->drv_flags &
3193 WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003194 return hostapd_remove_bss(hapd_iface, j);
Dmitry Shmidta38abf92014-03-06 13:38:44 -08003195 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003196 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003197 }
3198 return -1;
3199}
3200
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003201
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003202/**
3203 * hostapd_new_assoc_sta - Notify that a new station associated with the AP
3204 * @hapd: Pointer to BSS data
3205 * @sta: Pointer to the associated STA data
3206 * @reassoc: 1 to indicate this was a re-association; 0 = first association
3207 *
3208 * This function will be called whenever a station associates with the AP. It
3209 * can be called from ieee802_11.c for drivers that export MLME to hostapd and
3210 * from drv_callbacks.c based on driver events for drivers that take care of
3211 * management frames (IEEE 802.11 authentication and association) internally.
3212 */
3213void hostapd_new_assoc_sta(struct hostapd_data *hapd, struct sta_info *sta,
3214 int reassoc)
3215{
3216 if (hapd->tkip_countermeasures) {
3217 hostapd_drv_sta_deauth(hapd, sta->addr,
3218 WLAN_REASON_MICHAEL_MIC_FAILURE);
3219 return;
3220 }
3221
3222 hostapd_prune_associations(hapd, sta->addr);
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -08003223 ap_sta_clear_disconnect_timeouts(hapd, sta);
Hai Shalom899fcc72020-10-19 14:38:18 -07003224 sta->post_csa_sa_query = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003225
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003226#ifdef CONFIG_P2P
3227 if (sta->p2p_ie == NULL && !sta->no_p2p_set) {
3228 sta->no_p2p_set = 1;
3229 hapd->num_sta_no_p2p++;
3230 if (hapd->num_sta_no_p2p == 1)
3231 hostapd_p2p_non_p2p_sta_connected(hapd);
3232 }
3233#endif /* CONFIG_P2P */
3234
Hai Shalom81f62d82019-07-22 12:10:00 -07003235 airtime_policy_new_sta(hapd, sta);
3236
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003237 /* Start accounting here, if IEEE 802.1X and WPA are not used.
3238 * IEEE 802.1X/WPA code will start accounting after the station has
3239 * been authorized. */
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -08003240 if (!hapd->conf->ieee802_1x && !hapd->conf->wpa && !hapd->conf->osen) {
3241 ap_sta_set_authorized(hapd, sta, 1);
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08003242 os_get_reltime(&sta->connected_time);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003243 accounting_sta_start(hapd, sta);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003244 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003245
3246 /* Start IEEE 802.1X authentication process for new stations */
3247 ieee802_1x_new_station(hapd, sta);
3248 if (reassoc) {
3249 if (sta->auth_alg != WLAN_AUTH_FT &&
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003250 sta->auth_alg != WLAN_AUTH_FILS_SK &&
3251 sta->auth_alg != WLAN_AUTH_FILS_SK_PFS &&
3252 sta->auth_alg != WLAN_AUTH_FILS_PK &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003253 !(sta->flags & (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS)))
3254 wpa_auth_sm_event(sta->wpa_sm, WPA_REAUTH);
3255 } else
3256 wpa_auth_sta_associated(hapd->wpa_auth, sta->wpa_sm);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003257
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08003258 if (hapd->iface->drv_flags & WPA_DRIVER_FLAGS_WIRED) {
3259 if (eloop_cancel_timeout(ap_handle_timer, hapd, sta) > 0) {
3260 wpa_printf(MSG_DEBUG,
3261 "%s: %s: canceled wired ap_handle_timer timeout for "
3262 MACSTR,
3263 hapd->conf->iface, __func__,
3264 MAC2STR(sta->addr));
3265 }
3266 } else if (!(hapd->iface->drv_flags &
3267 WPA_DRIVER_FLAGS_INACTIVITY_TIMER)) {
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -08003268 wpa_printf(MSG_DEBUG,
3269 "%s: %s: reschedule ap_handle_timer timeout for "
3270 MACSTR " (%d seconds - ap_max_inactivity)",
3271 hapd->conf->iface, __func__, MAC2STR(sta->addr),
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08003272 hapd->conf->ap_max_inactivity);
3273 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
3274 eloop_register_timeout(hapd->conf->ap_max_inactivity, 0,
3275 ap_handle_timer, hapd, sta);
3276 }
Hai Shalom81f62d82019-07-22 12:10:00 -07003277
3278#ifdef CONFIG_MACSEC
3279 if (hapd->conf->wpa_key_mgmt == WPA_KEY_MGMT_NONE &&
3280 hapd->conf->mka_psk_set)
3281 ieee802_1x_create_preshared_mka_hapd(hapd, sta);
3282 else
3283 ieee802_1x_alloc_kay_sm_hapd(hapd, sta);
3284#endif /* CONFIG_MACSEC */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003285}
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003286
3287
3288const char * hostapd_state_text(enum hostapd_iface_state s)
3289{
3290 switch (s) {
3291 case HAPD_IFACE_UNINITIALIZED:
3292 return "UNINITIALIZED";
3293 case HAPD_IFACE_DISABLED:
3294 return "DISABLED";
3295 case HAPD_IFACE_COUNTRY_UPDATE:
3296 return "COUNTRY_UPDATE";
3297 case HAPD_IFACE_ACS:
3298 return "ACS";
3299 case HAPD_IFACE_HT_SCAN:
3300 return "HT_SCAN";
3301 case HAPD_IFACE_DFS:
3302 return "DFS";
3303 case HAPD_IFACE_ENABLED:
3304 return "ENABLED";
3305 }
3306
3307 return "UNKNOWN";
3308}
3309
3310
3311void hostapd_set_state(struct hostapd_iface *iface, enum hostapd_iface_state s)
3312{
3313 wpa_printf(MSG_INFO, "%s: interface state %s->%s",
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07003314 iface->conf ? iface->conf->bss[0]->iface : "N/A",
3315 hostapd_state_text(iface->state), hostapd_state_text(s));
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003316 iface->state = s;
3317}
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003318
3319
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003320int hostapd_csa_in_progress(struct hostapd_iface *iface)
3321{
3322 unsigned int i;
3323
3324 for (i = 0; i < iface->num_bss; i++)
3325 if (iface->bss[i]->csa_in_progress)
3326 return 1;
3327 return 0;
3328}
3329
3330
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003331#ifdef NEED_AP_MLME
3332
3333static void free_beacon_data(struct beacon_data *beacon)
3334{
3335 os_free(beacon->head);
3336 beacon->head = NULL;
3337 os_free(beacon->tail);
3338 beacon->tail = NULL;
3339 os_free(beacon->probe_resp);
3340 beacon->probe_resp = NULL;
3341 os_free(beacon->beacon_ies);
3342 beacon->beacon_ies = NULL;
3343 os_free(beacon->proberesp_ies);
3344 beacon->proberesp_ies = NULL;
3345 os_free(beacon->assocresp_ies);
3346 beacon->assocresp_ies = NULL;
3347}
3348
3349
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003350static int hostapd_build_beacon_data(struct hostapd_data *hapd,
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003351 struct beacon_data *beacon)
3352{
3353 struct wpabuf *beacon_extra, *proberesp_extra, *assocresp_extra;
3354 struct wpa_driver_ap_params params;
3355 int ret;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003356
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08003357 os_memset(beacon, 0, sizeof(*beacon));
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003358 ret = ieee802_11_build_ap_params(hapd, &params);
3359 if (ret < 0)
3360 return ret;
3361
3362 ret = hostapd_build_ap_extra_ies(hapd, &beacon_extra,
3363 &proberesp_extra,
3364 &assocresp_extra);
3365 if (ret)
3366 goto free_ap_params;
3367
3368 ret = -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003369 beacon->head = os_memdup(params.head, params.head_len);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003370 if (!beacon->head)
3371 goto free_ap_extra_ies;
3372
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003373 beacon->head_len = params.head_len;
3374
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003375 beacon->tail = os_memdup(params.tail, params.tail_len);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003376 if (!beacon->tail)
3377 goto free_beacon;
3378
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003379 beacon->tail_len = params.tail_len;
3380
3381 if (params.proberesp != NULL) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003382 beacon->probe_resp = os_memdup(params.proberesp,
3383 params.proberesp_len);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003384 if (!beacon->probe_resp)
3385 goto free_beacon;
3386
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003387 beacon->probe_resp_len = params.proberesp_len;
3388 }
3389
3390 /* copy the extra ies */
3391 if (beacon_extra) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003392 beacon->beacon_ies = os_memdup(beacon_extra->buf,
3393 wpabuf_len(beacon_extra));
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003394 if (!beacon->beacon_ies)
3395 goto free_beacon;
3396
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003397 beacon->beacon_ies_len = wpabuf_len(beacon_extra);
3398 }
3399
3400 if (proberesp_extra) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003401 beacon->proberesp_ies = os_memdup(proberesp_extra->buf,
3402 wpabuf_len(proberesp_extra));
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003403 if (!beacon->proberesp_ies)
3404 goto free_beacon;
3405
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003406 beacon->proberesp_ies_len = wpabuf_len(proberesp_extra);
3407 }
3408
3409 if (assocresp_extra) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003410 beacon->assocresp_ies = os_memdup(assocresp_extra->buf,
3411 wpabuf_len(assocresp_extra));
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003412 if (!beacon->assocresp_ies)
3413 goto free_beacon;
3414
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003415 beacon->assocresp_ies_len = wpabuf_len(assocresp_extra);
3416 }
3417
3418 ret = 0;
3419free_beacon:
3420 /* if the function fails, the caller should not free beacon data */
3421 if (ret)
3422 free_beacon_data(beacon);
3423
3424free_ap_extra_ies:
3425 hostapd_free_ap_extra_ies(hapd, beacon_extra, proberesp_extra,
3426 assocresp_extra);
3427free_ap_params:
3428 ieee802_11_free_ap_params(&params);
3429 return ret;
3430}
3431
3432
3433/*
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003434 * TODO: This flow currently supports only changing channel and width within
3435 * the same hw_mode. Any other changes to MAC parameters or provided settings
3436 * are not supported.
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003437 */
3438static int hostapd_change_config_freq(struct hostapd_data *hapd,
3439 struct hostapd_config *conf,
3440 struct hostapd_freq_params *params,
3441 struct hostapd_freq_params *old_params)
3442{
3443 int channel;
Hai Shalom81f62d82019-07-22 12:10:00 -07003444 u8 seg0, seg1;
3445 struct hostapd_hw_modes *mode;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003446
3447 if (!params->channel) {
3448 /* check if the new channel is supported by hw */
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003449 params->channel = hostapd_hw_get_channel(hapd, params->freq);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003450 }
3451
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003452 channel = params->channel;
3453 if (!channel)
3454 return -1;
3455
Hai Shalom81f62d82019-07-22 12:10:00 -07003456 mode = hapd->iface->current_mode;
3457
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003458 /* if a pointer to old_params is provided we save previous state */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003459 if (old_params &&
3460 hostapd_set_freq_params(old_params, conf->hw_mode,
3461 hostapd_hw_get_freq(hapd, conf->channel),
Hai Shalomc3565922019-10-28 11:58:20 -07003462 conf->channel, conf->enable_edmg,
3463 conf->edmg_channel, conf->ieee80211n,
Hai Shalom81f62d82019-07-22 12:10:00 -07003464 conf->ieee80211ac, conf->ieee80211ax,
Sunil Ravia04bd252022-05-02 22:54:18 -07003465 conf->ieee80211be, conf->secondary_channel,
Hai Shalom81f62d82019-07-22 12:10:00 -07003466 hostapd_get_oper_chwidth(conf),
3467 hostapd_get_oper_centr_freq_seg0_idx(conf),
3468 hostapd_get_oper_centr_freq_seg1_idx(conf),
3469 conf->vht_capab,
3470 mode ? &mode->he_capab[IEEE80211_MODE_AP] :
Sunil Ravia04bd252022-05-02 22:54:18 -07003471 NULL,
3472 mode ? &mode->eht_capab[IEEE80211_MODE_AP] :
Hai Shalom81f62d82019-07-22 12:10:00 -07003473 NULL))
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003474 return -1;
3475
3476 switch (params->bandwidth) {
3477 case 0:
3478 case 20:
Hai Shaloma20dcd72022-02-04 13:43:00 -08003479 conf->ht_capab &= ~HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET;
3480 break;
3481 case 40:
3482 case 80:
3483 case 160:
3484 conf->ht_capab |= HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET;
3485 break;
3486 default:
3487 return -1;
3488 }
3489
3490 switch (params->bandwidth) {
3491 case 0:
3492 case 20:
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003493 case 40:
Hai Shalom81f62d82019-07-22 12:10:00 -07003494 hostapd_set_oper_chwidth(conf, CHANWIDTH_USE_HT);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003495 break;
3496 case 80:
3497 if (params->center_freq2)
Hai Shalom81f62d82019-07-22 12:10:00 -07003498 hostapd_set_oper_chwidth(conf, CHANWIDTH_80P80MHZ);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003499 else
Hai Shalom81f62d82019-07-22 12:10:00 -07003500 hostapd_set_oper_chwidth(conf, CHANWIDTH_80MHZ);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003501 break;
3502 case 160:
Hai Shalom81f62d82019-07-22 12:10:00 -07003503 hostapd_set_oper_chwidth(conf, CHANWIDTH_160MHZ);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003504 break;
3505 default:
3506 return -1;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003507 }
3508
3509 conf->channel = channel;
3510 conf->ieee80211n = params->ht_enabled;
Hai Shaloma20dcd72022-02-04 13:43:00 -08003511 conf->ieee80211ac = params->vht_enabled;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003512 conf->secondary_channel = params->sec_channel_offset;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003513 ieee80211_freq_to_chan(params->center_freq1,
Hai Shalom81f62d82019-07-22 12:10:00 -07003514 &seg0);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003515 ieee80211_freq_to_chan(params->center_freq2,
Hai Shalom81f62d82019-07-22 12:10:00 -07003516 &seg1);
3517 hostapd_set_oper_centr_freq_seg0_idx(conf, seg0);
3518 hostapd_set_oper_centr_freq_seg1_idx(conf, seg1);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003519
3520 /* TODO: maybe call here hostapd_config_check here? */
3521
3522 return 0;
3523}
3524
3525
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003526static int hostapd_fill_csa_settings(struct hostapd_data *hapd,
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003527 struct csa_settings *settings)
3528{
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003529 struct hostapd_iface *iface = hapd->iface;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003530 struct hostapd_freq_params old_freq;
3531 int ret;
Hai Shalom81f62d82019-07-22 12:10:00 -07003532 u8 chan, bandwidth;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003533
3534 os_memset(&old_freq, 0, sizeof(old_freq));
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003535 if (!iface || !iface->freq || hapd->csa_in_progress)
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003536 return -1;
3537
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003538 switch (settings->freq_params.bandwidth) {
3539 case 80:
3540 if (settings->freq_params.center_freq2)
Hai Shalom81f62d82019-07-22 12:10:00 -07003541 bandwidth = CHANWIDTH_80P80MHZ;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003542 else
Hai Shalom81f62d82019-07-22 12:10:00 -07003543 bandwidth = CHANWIDTH_80MHZ;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003544 break;
3545 case 160:
Hai Shalom81f62d82019-07-22 12:10:00 -07003546 bandwidth = CHANWIDTH_160MHZ;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003547 break;
3548 default:
Hai Shalom81f62d82019-07-22 12:10:00 -07003549 bandwidth = CHANWIDTH_USE_HT;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003550 break;
3551 }
3552
3553 if (ieee80211_freq_to_channel_ext(
3554 settings->freq_params.freq,
3555 settings->freq_params.sec_channel_offset,
Hai Shalom81f62d82019-07-22 12:10:00 -07003556 bandwidth,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003557 &hapd->iface->cs_oper_class,
3558 &chan) == NUM_HOSTAPD_MODES) {
3559 wpa_printf(MSG_DEBUG,
Sunil Ravia04bd252022-05-02 22:54:18 -07003560 "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 -08003561 settings->freq_params.freq,
3562 settings->freq_params.sec_channel_offset,
Hai Shalom81f62d82019-07-22 12:10:00 -07003563 settings->freq_params.vht_enabled,
Sunil Ravia04bd252022-05-02 22:54:18 -07003564 settings->freq_params.he_enabled,
3565 settings->freq_params.eht_enabled);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003566 return -1;
3567 }
3568
3569 settings->freq_params.channel = chan;
3570
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003571 ret = hostapd_change_config_freq(iface->bss[0], iface->conf,
3572 &settings->freq_params,
3573 &old_freq);
3574 if (ret)
3575 return ret;
3576
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003577 ret = hostapd_build_beacon_data(hapd, &settings->beacon_after);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003578
3579 /* change back the configuration */
3580 hostapd_change_config_freq(iface->bss[0], iface->conf,
3581 &old_freq, NULL);
3582
3583 if (ret)
3584 return ret;
3585
3586 /* set channel switch parameters for csa ie */
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003587 hapd->cs_freq_params = settings->freq_params;
3588 hapd->cs_count = settings->cs_count;
3589 hapd->cs_block_tx = settings->block_tx;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003590
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003591 ret = hostapd_build_beacon_data(hapd, &settings->beacon_csa);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003592 if (ret) {
3593 free_beacon_data(&settings->beacon_after);
3594 return ret;
3595 }
3596
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003597 settings->counter_offset_beacon[0] = hapd->cs_c_off_beacon;
3598 settings->counter_offset_presp[0] = hapd->cs_c_off_proberesp;
3599 settings->counter_offset_beacon[1] = hapd->cs_c_off_ecsa_beacon;
3600 settings->counter_offset_presp[1] = hapd->cs_c_off_ecsa_proberesp;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003601
3602 return 0;
3603}
3604
3605
3606void hostapd_cleanup_cs_params(struct hostapd_data *hapd)
3607{
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003608 os_memset(&hapd->cs_freq_params, 0, sizeof(hapd->cs_freq_params));
3609 hapd->cs_count = 0;
3610 hapd->cs_block_tx = 0;
3611 hapd->cs_c_off_beacon = 0;
3612 hapd->cs_c_off_proberesp = 0;
3613 hapd->csa_in_progress = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003614 hapd->cs_c_off_ecsa_beacon = 0;
3615 hapd->cs_c_off_ecsa_proberesp = 0;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003616}
3617
3618
Hai Shalom60840252021-02-19 19:02:11 -08003619void hostapd_chan_switch_config(struct hostapd_data *hapd,
3620 struct hostapd_freq_params *freq_params)
Roshan Pius3a1667e2018-07-03 15:17:14 -07003621{
Sunil Ravia04bd252022-05-02 22:54:18 -07003622 if (freq_params->eht_enabled)
3623 hapd->iconf->ch_switch_eht_config |= CH_SWITCH_EHT_ENABLED;
3624 else
3625 hapd->iconf->ch_switch_eht_config |= CH_SWITCH_EHT_DISABLED;
3626
Hai Shalom60840252021-02-19 19:02:11 -08003627 if (freq_params->he_enabled)
3628 hapd->iconf->ch_switch_he_config |= CH_SWITCH_HE_ENABLED;
3629 else
3630 hapd->iconf->ch_switch_he_config |= CH_SWITCH_HE_DISABLED;
3631
3632 if (freq_params->vht_enabled)
Roshan Pius3a1667e2018-07-03 15:17:14 -07003633 hapd->iconf->ch_switch_vht_config |= CH_SWITCH_VHT_ENABLED;
3634 else
3635 hapd->iconf->ch_switch_vht_config |= CH_SWITCH_VHT_DISABLED;
3636
3637 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
Hai Shalom60840252021-02-19 19:02:11 -08003638 HOSTAPD_LEVEL_INFO,
Sunil Ravia04bd252022-05-02 22:54:18 -07003639 "CHAN_SWITCH EHT config 0x%x HE config 0x%x VHT config 0x%x",
3640 hapd->iconf->ch_switch_eht_config,
Hai Shalom60840252021-02-19 19:02:11 -08003641 hapd->iconf->ch_switch_he_config,
Roshan Pius3a1667e2018-07-03 15:17:14 -07003642 hapd->iconf->ch_switch_vht_config);
3643}
3644
3645
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003646int hostapd_switch_channel(struct hostapd_data *hapd,
3647 struct csa_settings *settings)
3648{
3649 int ret;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003650
3651 if (!(hapd->iface->drv_flags & WPA_DRIVER_FLAGS_AP_CSA)) {
3652 wpa_printf(MSG_INFO, "CSA is not supported");
3653 return -1;
3654 }
3655
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003656 ret = hostapd_fill_csa_settings(hapd, settings);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003657 if (ret)
3658 return ret;
3659
3660 ret = hostapd_drv_switch_channel(hapd, settings);
3661 free_beacon_data(&settings->beacon_csa);
3662 free_beacon_data(&settings->beacon_after);
3663
3664 if (ret) {
3665 /* if we failed, clean cs parameters */
3666 hostapd_cleanup_cs_params(hapd);
3667 return ret;
3668 }
3669
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003670 hapd->csa_in_progress = 1;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003671 return 0;
3672}
3673
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003674
3675void
3676hostapd_switch_channel_fallback(struct hostapd_iface *iface,
3677 const struct hostapd_freq_params *freq_params)
3678{
Hai Shalom81f62d82019-07-22 12:10:00 -07003679 int seg0_idx = 0, seg1_idx = 0, bw = CHANWIDTH_USE_HT;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003680
3681 wpa_printf(MSG_DEBUG, "Restarting all CSA-related BSSes");
3682
3683 if (freq_params->center_freq1)
Hai Shalom81f62d82019-07-22 12:10:00 -07003684 seg0_idx = 36 + (freq_params->center_freq1 - 5180) / 5;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003685 if (freq_params->center_freq2)
Hai Shalom81f62d82019-07-22 12:10:00 -07003686 seg1_idx = 36 + (freq_params->center_freq2 - 5180) / 5;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003687
3688 switch (freq_params->bandwidth) {
3689 case 0:
3690 case 20:
3691 case 40:
Hai Shalom81f62d82019-07-22 12:10:00 -07003692 bw = CHANWIDTH_USE_HT;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003693 break;
3694 case 80:
3695 if (freq_params->center_freq2)
Hai Shalom81f62d82019-07-22 12:10:00 -07003696 bw = CHANWIDTH_80P80MHZ;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003697 else
Hai Shalom81f62d82019-07-22 12:10:00 -07003698 bw = CHANWIDTH_80MHZ;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003699 break;
3700 case 160:
Hai Shalom81f62d82019-07-22 12:10:00 -07003701 bw = CHANWIDTH_160MHZ;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003702 break;
3703 default:
3704 wpa_printf(MSG_WARNING, "Unknown CSA bandwidth: %d",
3705 freq_params->bandwidth);
3706 break;
3707 }
3708
3709 iface->freq = freq_params->freq;
3710 iface->conf->channel = freq_params->channel;
3711 iface->conf->secondary_channel = freq_params->sec_channel_offset;
Hai Shalom81f62d82019-07-22 12:10:00 -07003712 hostapd_set_oper_centr_freq_seg0_idx(iface->conf, seg0_idx);
3713 hostapd_set_oper_centr_freq_seg1_idx(iface->conf, seg1_idx);
3714 hostapd_set_oper_chwidth(iface->conf, bw);
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003715 iface->conf->ieee80211n = freq_params->ht_enabled;
3716 iface->conf->ieee80211ac = freq_params->vht_enabled;
Hai Shalom81f62d82019-07-22 12:10:00 -07003717 iface->conf->ieee80211ax = freq_params->he_enabled;
Sunil Ravia04bd252022-05-02 22:54:18 -07003718 iface->conf->ieee80211be = freq_params->eht_enabled;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003719
3720 /*
3721 * cs_params must not be cleared earlier because the freq_params
3722 * argument may actually point to one of these.
Hai Shalom39ba6fc2019-01-22 12:40:38 -08003723 * These params will be cleared during interface disable below.
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003724 */
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003725 hostapd_disable_iface(iface);
3726 hostapd_enable_iface(iface);
3727}
3728
Sunil Ravia04bd252022-05-02 22:54:18 -07003729
3730#ifdef CONFIG_IEEE80211AX
3731
3732void hostapd_cleanup_cca_params(struct hostapd_data *hapd)
3733{
3734 hapd->cca_count = 0;
3735 hapd->cca_color = 0;
3736 hapd->cca_c_off_beacon = 0;
3737 hapd->cca_c_off_proberesp = 0;
3738 hapd->cca_in_progress = false;
3739}
3740
3741
3742static int hostapd_fill_cca_settings(struct hostapd_data *hapd,
3743 struct cca_settings *settings)
3744{
3745 struct hostapd_iface *iface = hapd->iface;
3746 u8 old_color;
3747 int ret;
3748
3749 if (!iface || iface->conf->he_op.he_bss_color_disabled)
3750 return -1;
3751
3752 old_color = iface->conf->he_op.he_bss_color;
3753 iface->conf->he_op.he_bss_color = hapd->cca_color;
3754 ret = hostapd_build_beacon_data(hapd, &settings->beacon_after);
3755 if (ret)
3756 return ret;
3757
3758 iface->conf->he_op.he_bss_color = old_color;
3759
3760 settings->cca_count = hapd->cca_count;
3761 settings->cca_color = hapd->cca_color,
3762 hapd->cca_in_progress = true;
3763
3764 ret = hostapd_build_beacon_data(hapd, &settings->beacon_cca);
3765 if (ret) {
3766 free_beacon_data(&settings->beacon_after);
3767 return ret;
3768 }
3769
3770 settings->counter_offset_beacon = hapd->cca_c_off_beacon;
3771 settings->counter_offset_presp = hapd->cca_c_off_proberesp;
3772
3773 return 0;
3774}
3775
3776
3777static void hostapd_switch_color_timeout_handler(void *eloop_data,
3778 void *user_ctx)
3779{
3780 struct hostapd_data *hapd = (struct hostapd_data *) eloop_data;
3781 os_time_t delta_t;
3782 unsigned int b;
3783 int i, r;
3784
3785 /* CCA can be triggered once the handler constantly receives
3786 * color collision events to for at least
3787 * DOT11BSS_COLOR_COLLISION_AP_PERIOD (50 s by default). */
3788 delta_t = hapd->last_color_collision.sec -
3789 hapd->first_color_collision.sec;
3790 if (delta_t < DOT11BSS_COLOR_COLLISION_AP_PERIOD)
3791 return;
3792
3793 r = os_random() % HE_OPERATION_BSS_COLOR_MAX;
3794 for (i = 0; i < HE_OPERATION_BSS_COLOR_MAX; i++) {
3795 if (r && !(hapd->color_collision_bitmap & BIT(r)))
3796 break;
3797
3798 r = (r + 1) % HE_OPERATION_BSS_COLOR_MAX;
3799 }
3800
3801 if (i == HE_OPERATION_BSS_COLOR_MAX) {
3802 /* There are no free colors so turn BSS coloring off */
3803 wpa_printf(MSG_INFO,
3804 "No free colors left, turning off BSS coloring");
3805 hapd->iface->conf->he_op.he_bss_color_disabled = 1;
3806 hapd->iface->conf->he_op.he_bss_color = os_random() % 63 + 1;
3807 for (b = 0; b < hapd->iface->num_bss; b++)
3808 ieee802_11_set_beacon(hapd->iface->bss[b]);
3809 return;
3810 }
3811
3812 for (b = 0; b < hapd->iface->num_bss; b++) {
3813 struct hostapd_data *bss = hapd->iface->bss[b];
3814 struct cca_settings settings;
3815 int ret;
3816
3817 hostapd_cleanup_cca_params(bss);
3818 bss->cca_color = r;
3819 bss->cca_count = 10;
3820
3821 if (hostapd_fill_cca_settings(bss, &settings)) {
3822 hostapd_cleanup_cca_params(bss);
3823 continue;
3824 }
3825
3826 ret = hostapd_drv_switch_color(bss, &settings);
3827 if (ret)
3828 hostapd_cleanup_cca_params(bss);
3829
3830 free_beacon_data(&settings.beacon_cca);
3831 free_beacon_data(&settings.beacon_after);
3832 }
3833}
3834
3835
3836void hostapd_switch_color(struct hostapd_data *hapd, u64 bitmap)
3837{
3838 struct os_reltime now;
3839
3840 if (hapd->cca_in_progress)
3841 return;
3842
3843 if (os_get_reltime(&now))
3844 return;
3845
3846 hapd->color_collision_bitmap = bitmap;
3847 hapd->last_color_collision = now;
3848
3849 if (eloop_is_timeout_registered(hostapd_switch_color_timeout_handler,
3850 hapd, NULL))
3851 return;
3852
3853 hapd->first_color_collision = now;
3854 /* 10 s window as margin for persistent color collision reporting */
3855 eloop_register_timeout(DOT11BSS_COLOR_COLLISION_AP_PERIOD + 10, 0,
3856 hostapd_switch_color_timeout_handler,
3857 hapd, NULL);
3858}
3859
3860#endif /* CONFIG_IEEE80211AX */
3861
Dmitry Shmidte4663042016-04-04 10:07:49 -07003862#endif /* NEED_AP_MLME */
3863
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003864
3865struct hostapd_data * hostapd_get_iface(struct hapd_interfaces *interfaces,
3866 const char *ifname)
3867{
3868 size_t i, j;
3869
3870 for (i = 0; i < interfaces->count; i++) {
3871 struct hostapd_iface *iface = interfaces->iface[i];
3872
3873 for (j = 0; j < iface->num_bss; j++) {
3874 struct hostapd_data *hapd = iface->bss[j];
3875
3876 if (os_strcmp(ifname, hapd->conf->iface) == 0)
3877 return hapd;
3878 }
3879 }
3880
3881 return NULL;
3882}
3883
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003884
3885void hostapd_periodic_iface(struct hostapd_iface *iface)
3886{
3887 size_t i;
3888
3889 ap_list_timer(iface);
3890
3891 for (i = 0; i < iface->num_bss; i++) {
3892 struct hostapd_data *hapd = iface->bss[i];
3893
3894 if (!hapd->started)
3895 continue;
3896
3897#ifndef CONFIG_NO_RADIUS
3898 hostapd_acl_expire(hapd);
3899#endif /* CONFIG_NO_RADIUS */
3900 }
3901}
Hai Shalom899fcc72020-10-19 14:38:18 -07003902
3903
3904#ifdef CONFIG_OCV
3905void hostapd_ocv_check_csa_sa_query(void *eloop_ctx, void *timeout_ctx)
3906{
3907 struct hostapd_data *hapd = eloop_ctx;
3908 struct sta_info *sta;
3909
3910 wpa_printf(MSG_DEBUG, "OCV: Post-CSA SA Query initiation check");
3911
3912 for (sta = hapd->sta_list; sta; sta = sta->next) {
3913 if (!sta->post_csa_sa_query)
3914 continue;
3915
3916 wpa_printf(MSG_DEBUG, "OCV: OCVC STA " MACSTR
3917 " did not start SA Query after CSA - disconnect",
3918 MAC2STR(sta->addr));
3919 ap_sta_disconnect(hapd, sta, sta->addr,
3920 WLAN_REASON_PREV_AUTH_NOT_VALID);
3921 }
3922}
3923#endif /* CONFIG_OCV */