blob: 5515ab34d4c4df5ef56e02574ccdc8512db5c9c4 [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * hostapd / Initialization and configuration
Hai Shalom021b0b52019-04-10 11:17:58 -07003 * Copyright (c) 2002-2019, 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);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070069
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070070
Dmitry Shmidt04949592012-07-19 12:16:46 -070071int hostapd_for_each_interface(struct hapd_interfaces *interfaces,
72 int (*cb)(struct hostapd_iface *iface,
73 void *ctx), void *ctx)
74{
75 size_t i;
76 int ret;
77
78 for (i = 0; i < interfaces->count; i++) {
79 ret = cb(interfaces->iface[i], ctx);
80 if (ret)
81 return ret;
82 }
83
84 return 0;
85}
86
87
Hai Shalomce48b4a2018-09-05 11:41:35 -070088void hostapd_reconfig_encryption(struct hostapd_data *hapd)
89{
90 if (hapd->wpa_auth)
91 return;
92
93 hostapd_set_privacy(hapd, 0);
Hai Shalomfdcde762020-04-02 11:19:20 -070094#ifdef CONFIG_WEP
Hai Shalomce48b4a2018-09-05 11:41:35 -070095 hostapd_setup_encryption(hapd->conf->iface, hapd);
Hai Shalomfdcde762020-04-02 11:19:20 -070096#endif /* CONFIG_WEP */
Hai Shalomce48b4a2018-09-05 11:41:35 -070097}
98
99
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700100static void hostapd_reload_bss(struct hostapd_data *hapd)
101{
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800102 struct hostapd_ssid *ssid;
103
Dmitry Shmidt29333592017-01-09 12:27:11 -0800104 if (!hapd->started)
105 return;
106
Roshan Pius3a1667e2018-07-03 15:17:14 -0700107 if (hapd->conf->wmm_enabled < 0)
108 hapd->conf->wmm_enabled = hapd->iconf->ieee80211n;
109
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700110#ifndef CONFIG_NO_RADIUS
111 radius_client_reconfig(hapd->radius, hapd->conf->radius);
112#endif /* CONFIG_NO_RADIUS */
113
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800114 ssid = &hapd->conf->ssid;
115 if (!ssid->wpa_psk_set && ssid->wpa_psk && !ssid->wpa_psk->next &&
116 ssid->wpa_passphrase_set && ssid->wpa_passphrase) {
117 /*
118 * Force PSK to be derived again since SSID or passphrase may
119 * have changed.
120 */
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800121 hostapd_config_clear_wpa_psk(&hapd->conf->ssid.wpa_psk);
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800122 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700123 if (hostapd_setup_wpa_psk(hapd->conf)) {
124 wpa_printf(MSG_ERROR, "Failed to re-configure WPA PSK "
125 "after reloading configuration");
126 }
127
128 if (hapd->conf->ieee802_1x || hapd->conf->wpa)
129 hostapd_set_drv_ieee8021x(hapd, hapd->conf->iface, 1);
130 else
131 hostapd_set_drv_ieee8021x(hapd, hapd->conf->iface, 0);
132
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800133 if ((hapd->conf->wpa || hapd->conf->osen) && hapd->wpa_auth == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700134 hostapd_setup_wpa(hapd);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800135 if (hapd->wpa_auth)
136 wpa_init_keys(hapd->wpa_auth);
137 } else if (hapd->conf->wpa) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700138 const u8 *wpa_ie;
139 size_t wpa_ie_len;
140 hostapd_reconfig_wpa(hapd);
141 wpa_ie = wpa_auth_get_wpa_ie(hapd->wpa_auth, &wpa_ie_len);
142 if (hostapd_set_generic_elem(hapd, wpa_ie, wpa_ie_len))
143 wpa_printf(MSG_ERROR, "Failed to configure WPA IE for "
144 "the kernel driver.");
145 } else if (hapd->wpa_auth) {
146 wpa_deinit(hapd->wpa_auth);
147 hapd->wpa_auth = NULL;
148 hostapd_set_privacy(hapd, 0);
Hai Shalomfdcde762020-04-02 11:19:20 -0700149#ifdef CONFIG_WEP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700150 hostapd_setup_encryption(hapd->conf->iface, hapd);
Hai Shalomfdcde762020-04-02 11:19:20 -0700151#endif /* CONFIG_WEP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700152 hostapd_set_generic_elem(hapd, (u8 *) "", 0);
153 }
154
155 ieee802_11_set_beacon(hapd);
156 hostapd_update_wps(hapd);
157
158 if (hapd->conf->ssid.ssid_set &&
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700159 hostapd_set_ssid(hapd, hapd->conf->ssid.ssid,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700160 hapd->conf->ssid.ssid_len)) {
161 wpa_printf(MSG_ERROR, "Could not set SSID for kernel driver");
162 /* try to continue */
163 }
164 wpa_printf(MSG_DEBUG, "Reconfigured interface %s", hapd->conf->iface);
165}
166
167
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700168static void hostapd_clear_old(struct hostapd_iface *iface)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700169{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700170 size_t j;
171
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700172 /*
173 * Deauthenticate all stations since the new configuration may not
174 * allow them to use the BSS anymore.
175 */
176 for (j = 0; j < iface->num_bss; j++) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700177 hostapd_flush_old_stations(iface->bss[j],
178 WLAN_REASON_PREV_AUTH_NOT_VALID);
Hai Shalomfdcde762020-04-02 11:19:20 -0700179#ifdef CONFIG_WEP
Dmitry Shmidtc55524a2011-07-07 11:18:38 -0700180 hostapd_broadcast_wep_clear(iface->bss[j]);
Hai Shalomfdcde762020-04-02 11:19:20 -0700181#endif /* CONFIG_WEP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700182
183#ifndef CONFIG_NO_RADIUS
184 /* TODO: update dynamic data based on changed configuration
185 * items (e.g., open/close sockets, etc.) */
186 radius_client_flush(iface->bss[j]->radius, 0);
187#endif /* CONFIG_NO_RADIUS */
188 }
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700189}
190
191
Hai Shalom74f70d42019-02-11 14:42:39 -0800192static int hostapd_iface_conf_changed(struct hostapd_config *newconf,
193 struct hostapd_config *oldconf)
194{
195 size_t i;
196
197 if (newconf->num_bss != oldconf->num_bss)
198 return 1;
199
200 for (i = 0; i < newconf->num_bss; i++) {
201 if (os_strcmp(newconf->bss[i]->iface,
202 oldconf->bss[i]->iface) != 0)
203 return 1;
204 }
205
206 return 0;
207}
208
209
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700210int hostapd_reload_config(struct hostapd_iface *iface)
211{
Hai Shalom74f70d42019-02-11 14:42:39 -0800212 struct hapd_interfaces *interfaces = iface->interfaces;
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700213 struct hostapd_data *hapd = iface->bss[0];
214 struct hostapd_config *newconf, *oldconf;
215 size_t j;
216
217 if (iface->config_fname == NULL) {
218 /* Only in-memory config in use - assume it has been updated */
219 hostapd_clear_old(iface);
220 for (j = 0; j < iface->num_bss; j++)
221 hostapd_reload_bss(iface->bss[j]);
222 return 0;
223 }
224
225 if (iface->interfaces == NULL ||
226 iface->interfaces->config_read_cb == NULL)
227 return -1;
228 newconf = iface->interfaces->config_read_cb(iface->config_fname);
229 if (newconf == NULL)
230 return -1;
231
232 hostapd_clear_old(iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700233
234 oldconf = hapd->iconf;
Hai Shalom74f70d42019-02-11 14:42:39 -0800235 if (hostapd_iface_conf_changed(newconf, oldconf)) {
236 char *fname;
237 int res;
238
239 wpa_printf(MSG_DEBUG,
240 "Configuration changes include interface/BSS modification - force full disable+enable sequence");
241 fname = os_strdup(iface->config_fname);
242 if (!fname) {
243 hostapd_config_free(newconf);
244 return -1;
245 }
246 hostapd_remove_iface(interfaces, hapd->conf->iface);
247 iface = hostapd_init(interfaces, fname);
248 os_free(fname);
249 hostapd_config_free(newconf);
250 if (!iface) {
251 wpa_printf(MSG_ERROR,
252 "Failed to initialize interface on config reload");
253 return -1;
254 }
255 iface->interfaces = interfaces;
256 interfaces->iface[interfaces->count] = iface;
257 interfaces->count++;
258 res = hostapd_enable_iface(iface);
259 if (res < 0)
260 wpa_printf(MSG_ERROR,
261 "Failed to enable interface on config reload");
262 return res;
263 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700264 iface->conf = newconf;
265
266 for (j = 0; j < iface->num_bss; j++) {
267 hapd = iface->bss[j];
268 hapd->iconf = newconf;
Dmitry Shmidtd11f0192014-03-24 12:09:47 -0700269 hapd->iconf->channel = oldconf->channel;
Dmitry Shmidtdda10c22015-03-24 16:05:01 -0700270 hapd->iconf->acs = oldconf->acs;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -0700271 hapd->iconf->secondary_channel = oldconf->secondary_channel;
Dmitry Shmidtd11f0192014-03-24 12:09:47 -0700272 hapd->iconf->ieee80211n = oldconf->ieee80211n;
273 hapd->iconf->ieee80211ac = oldconf->ieee80211ac;
274 hapd->iconf->ht_capab = oldconf->ht_capab;
275 hapd->iconf->vht_capab = oldconf->vht_capab;
Hai Shalom81f62d82019-07-22 12:10:00 -0700276 hostapd_set_oper_chwidth(hapd->iconf,
277 hostapd_get_oper_chwidth(oldconf));
278 hostapd_set_oper_centr_freq_seg0_idx(
279 hapd->iconf,
280 hostapd_get_oper_centr_freq_seg0_idx(oldconf));
281 hostapd_set_oper_centr_freq_seg1_idx(
282 hapd->iconf,
283 hostapd_get_oper_centr_freq_seg1_idx(oldconf));
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800284 hapd->conf = newconf->bss[j];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700285 hostapd_reload_bss(hapd);
286 }
287
288 hostapd_config_free(oldconf);
289
290
291 return 0;
292}
293
294
Hai Shalomfdcde762020-04-02 11:19:20 -0700295#ifdef CONFIG_WEP
296
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700297static void hostapd_broadcast_key_clear_iface(struct hostapd_data *hapd,
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700298 const char *ifname)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700299{
300 int i;
301
Dmitry Shmidt29333592017-01-09 12:27:11 -0800302 if (!ifname || !hapd->drv_priv)
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700303 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700304 for (i = 0; i < NUM_WEP_KEYS; i++) {
Hai Shalomfdcde762020-04-02 11:19:20 -0700305 if (hostapd_drv_set_key(ifname, hapd, WPA_ALG_NONE, NULL, i, 0,
306 0, NULL, 0, NULL, 0, KEY_FLAG_GROUP)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700307 wpa_printf(MSG_DEBUG, "Failed to clear default "
308 "encryption keys (ifname=%s keyidx=%d)",
309 ifname, i);
310 }
311 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700312 if (hapd->conf->ieee80211w) {
313 for (i = NUM_WEP_KEYS; i < NUM_WEP_KEYS + 2; i++) {
314 if (hostapd_drv_set_key(ifname, hapd, WPA_ALG_NONE,
Hai Shalomfdcde762020-04-02 11:19:20 -0700315 NULL, i, 0, 0, NULL,
316 0, NULL, 0, KEY_FLAG_GROUP)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700317 wpa_printf(MSG_DEBUG, "Failed to clear "
318 "default mgmt encryption keys "
319 "(ifname=%s keyidx=%d)", ifname, i);
320 }
321 }
322 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700323}
324
325
326static int hostapd_broadcast_wep_clear(struct hostapd_data *hapd)
327{
328 hostapd_broadcast_key_clear_iface(hapd, hapd->conf->iface);
329 return 0;
330}
331
332
333static int hostapd_broadcast_wep_set(struct hostapd_data *hapd)
334{
335 int errors = 0, idx;
336 struct hostapd_ssid *ssid = &hapd->conf->ssid;
337
338 idx = ssid->wep.idx;
Hai Shalomfdcde762020-04-02 11:19:20 -0700339 if (ssid->wep.default_len && ssid->wep.key[idx] &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700340 hostapd_drv_set_key(hapd->conf->iface,
Hai Shalomfdcde762020-04-02 11:19:20 -0700341 hapd, WPA_ALG_WEP, broadcast_ether_addr, idx, 0,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700342 1, NULL, 0, ssid->wep.key[idx],
Hai Shalomfdcde762020-04-02 11:19:20 -0700343 ssid->wep.len[idx],
344 KEY_FLAG_GROUP_RX_TX_DEFAULT)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700345 wpa_printf(MSG_WARNING, "Could not set WEP encryption.");
346 errors++;
347 }
348
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700349 return errors;
350}
351
Hai Shalomfdcde762020-04-02 11:19:20 -0700352#endif /* CONFIG_WEP */
353
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700354
Dmitry Shmidt04949592012-07-19 12:16:46 -0700355static void hostapd_free_hapd_data(struct hostapd_data *hapd)
356{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800357 os_free(hapd->probereq_cb);
358 hapd->probereq_cb = NULL;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800359 hapd->num_probereq_cb = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800360
361#ifdef CONFIG_P2P
362 wpabuf_free(hapd->p2p_beacon_ie);
363 hapd->p2p_beacon_ie = NULL;
364 wpabuf_free(hapd->p2p_probe_resp_ie);
365 hapd->p2p_probe_resp_ie = NULL;
366#endif /* CONFIG_P2P */
367
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -0800368 if (!hapd->started) {
369 wpa_printf(MSG_ERROR, "%s: Interface %s wasn't started",
Hai Shalom021b0b52019-04-10 11:17:58 -0700370 __func__, hapd->conf ? hapd->conf->iface : "N/A");
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -0800371 return;
372 }
373 hapd->started = 0;
Hai Shalom5f92bc92019-04-18 11:54:11 -0700374 hapd->beacon_set_done = 0;
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -0800375
Dmitry Shmidt54605472013-11-08 11:10:19 -0800376 wpa_printf(MSG_DEBUG, "%s(%s)", __func__, hapd->conf->iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700377 accounting_deinit(hapd);
378 hostapd_deinit_wpa(hapd);
379 vlan_deinit(hapd);
380 hostapd_acl_deinit(hapd);
381#ifndef CONFIG_NO_RADIUS
382 radius_client_deinit(hapd->radius);
383 hapd->radius = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700384 radius_das_deinit(hapd->radius_das);
385 hapd->radius_das = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700386#endif /* CONFIG_NO_RADIUS */
387
388 hostapd_deinit_wps(hapd);
Hai Shalom81f62d82019-07-22 12:10:00 -0700389 ieee802_1x_dealloc_kay_sm_hapd(hapd);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700390#ifdef CONFIG_DPP
391 hostapd_dpp_deinit(hapd);
392 gas_query_ap_deinit(hapd->gas);
393#endif /* CONFIG_DPP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700394
395 authsrv_deinit(hapd);
396
Dmitry Shmidt71757432014-06-02 13:50:35 -0700397 if (hapd->interface_added) {
398 hapd->interface_added = 0;
399 if (hostapd_if_remove(hapd, WPA_IF_AP_BSS, hapd->conf->iface)) {
400 wpa_printf(MSG_WARNING,
401 "Failed to remove BSS interface %s",
402 hapd->conf->iface);
403 hapd->interface_added = 1;
404 } else {
405 /*
406 * Since this was a dynamically added interface, the
407 * driver wrapper may have removed its internal instance
408 * and hapd->drv_priv is not valid anymore.
409 */
410 hapd->drv_priv = NULL;
411 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700412 }
413
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800414 wpabuf_free(hapd->time_adv);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700415
416#ifdef CONFIG_INTERWORKING
417 gas_serv_deinit(hapd);
418#endif /* CONFIG_INTERWORKING */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800419
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800420 bss_load_update_deinit(hapd);
421 ndisc_snoop_deinit(hapd);
422 dhcp_snoop_deinit(hapd);
423 x_snoop_deinit(hapd);
424
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800425#ifdef CONFIG_SQLITE
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700426 bin_clear_free(hapd->tmp_eap_user.identity,
427 hapd->tmp_eap_user.identity_len);
428 bin_clear_free(hapd->tmp_eap_user.password,
429 hapd->tmp_eap_user.password_len);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800430#endif /* CONFIG_SQLITE */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800431
432#ifdef CONFIG_MESH
433 wpabuf_free(hapd->mesh_pending_auth);
434 hapd->mesh_pending_auth = NULL;
435#endif /* CONFIG_MESH */
Dmitry Shmidt849734c2016-05-27 09:59:01 -0700436
437 hostapd_clean_rrm(hapd);
Dmitry Shmidtebd93af2017-02-21 13:40:44 -0800438 fils_hlp_deinit(hapd);
Hai Shalom021b0b52019-04-10 11:17:58 -0700439
440#ifdef CONFIG_SAE
441 {
442 struct hostapd_sae_commit_queue *q;
443
444 while ((q = dl_list_first(&hapd->sae_commit_queue,
445 struct hostapd_sae_commit_queue,
446 list))) {
447 dl_list_del(&q->list);
448 os_free(q);
449 }
450 }
451 eloop_cancel_timeout(auth_sae_process_commit, hapd, NULL);
452#endif /* CONFIG_SAE */
Dmitry Shmidt04949592012-07-19 12:16:46 -0700453}
454
455
456/**
457 * hostapd_cleanup - Per-BSS cleanup (deinitialization)
458 * @hapd: Pointer to BSS data
459 *
460 * This function is used to free all per-BSS data structures and resources.
Dmitry Shmidt54605472013-11-08 11:10:19 -0800461 * Most of the modules that are initialized in hostapd_setup_bss() are
462 * deinitialized here.
Dmitry Shmidt04949592012-07-19 12:16:46 -0700463 */
464static void hostapd_cleanup(struct hostapd_data *hapd)
465{
Dmitry Shmidt54605472013-11-08 11:10:19 -0800466 wpa_printf(MSG_DEBUG, "%s(hapd=%p (%s))", __func__, hapd,
Hai Shalom021b0b52019-04-10 11:17:58 -0700467 hapd->conf ? hapd->conf->iface : "N/A");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700468 if (hapd->iface->interfaces &&
Dmitry Shmidt29333592017-01-09 12:27:11 -0800469 hapd->iface->interfaces->ctrl_iface_deinit) {
470 wpa_msg(hapd->msg_ctx, MSG_INFO, WPA_EVENT_TERMINATING);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700471 hapd->iface->interfaces->ctrl_iface_deinit(hapd);
Dmitry Shmidt29333592017-01-09 12:27:11 -0800472 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700473 hostapd_free_hapd_data(hapd);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700474}
475
476
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800477static void sta_track_deinit(struct hostapd_iface *iface)
478{
479 struct hostapd_sta_info *info;
480
481 if (!iface->num_sta_seen)
482 return;
483
484 while ((info = dl_list_first(&iface->sta_seen, struct hostapd_sta_info,
485 list))) {
486 dl_list_del(&info->list);
487 iface->num_sta_seen--;
Dmitry Shmidtaca489e2016-09-28 15:44:14 -0700488 sta_track_del(info);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800489 }
490}
491
492
Dmitry Shmidt04949592012-07-19 12:16:46 -0700493static void hostapd_cleanup_iface_partial(struct hostapd_iface *iface)
494{
Dmitry Shmidt54605472013-11-08 11:10:19 -0800495 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800496#ifdef NEED_AP_MLME
497 hostapd_stop_setup_timers(iface);
498#endif /* NEED_AP_MLME */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700499 if (iface->current_mode)
500 acs_cleanup(iface);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700501 hostapd_free_hw_features(iface->hw_features, iface->num_hw_features);
502 iface->hw_features = NULL;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700503 iface->current_mode = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700504 os_free(iface->current_rates);
505 iface->current_rates = NULL;
506 os_free(iface->basic_rates);
507 iface->basic_rates = NULL;
508 ap_list_deinit(iface);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800509 sta_track_deinit(iface);
Hai Shalom81f62d82019-07-22 12:10:00 -0700510 airtime_policy_update_deinit(iface);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700511}
512
513
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700514/**
515 * hostapd_cleanup_iface - Complete per-interface cleanup
516 * @iface: Pointer to interface data
517 *
518 * This function is called after per-BSS data structures are deinitialized
519 * with hostapd_cleanup().
520 */
521static void hostapd_cleanup_iface(struct hostapd_iface *iface)
522{
Dmitry Shmidt54605472013-11-08 11:10:19 -0800523 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800524 eloop_cancel_timeout(channel_list_update_timeout, iface, NULL);
Roshan Pius3a1667e2018-07-03 15:17:14 -0700525 eloop_cancel_timeout(hostapd_interface_setup_failure_handler, iface,
526 NULL);
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800527
Dmitry Shmidt04949592012-07-19 12:16:46 -0700528 hostapd_cleanup_iface_partial(iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700529 hostapd_config_free(iface->conf);
530 iface->conf = NULL;
531
532 os_free(iface->config_fname);
533 os_free(iface->bss);
Dmitry Shmidt54605472013-11-08 11:10:19 -0800534 wpa_printf(MSG_DEBUG, "%s: free iface=%p", __func__, iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700535 os_free(iface);
536}
537
538
Hai Shalomfdcde762020-04-02 11:19:20 -0700539#ifdef CONFIG_WEP
540
Dmitry Shmidt04949592012-07-19 12:16:46 -0700541static void hostapd_clear_wep(struct hostapd_data *hapd)
542{
Hai Shalom021b0b52019-04-10 11:17:58 -0700543 if (hapd->drv_priv && !hapd->iface->driver_ap_teardown && hapd->conf) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700544 hostapd_set_privacy(hapd, 0);
545 hostapd_broadcast_wep_clear(hapd);
546 }
547}
548
549
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700550static int hostapd_setup_encryption(char *iface, struct hostapd_data *hapd)
551{
552 int i;
553
554 hostapd_broadcast_wep_set(hapd);
555
556 if (hapd->conf->ssid.wep.default_len) {
557 hostapd_set_privacy(hapd, 1);
558 return 0;
559 }
560
Jouni Malinen75ecf522011-06-27 15:19:46 -0700561 /*
562 * When IEEE 802.1X is not enabled, the driver may need to know how to
563 * set authentication algorithms for static WEP.
564 */
565 hostapd_drv_set_authmode(hapd, hapd->conf->auth_algs);
566
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700567 for (i = 0; i < 4; i++) {
568 if (hapd->conf->ssid.wep.key[i] &&
Hai Shalomfdcde762020-04-02 11:19:20 -0700569 hostapd_drv_set_key(iface, hapd, WPA_ALG_WEP, NULL, i, 0,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700570 i == hapd->conf->ssid.wep.idx, NULL, 0,
571 hapd->conf->ssid.wep.key[i],
Hai Shalomfdcde762020-04-02 11:19:20 -0700572 hapd->conf->ssid.wep.len[i],
573 i == hapd->conf->ssid.wep.idx ?
574 KEY_FLAG_GROUP_RX_TX_DEFAULT :
575 KEY_FLAG_GROUP_RX_TX)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700576 wpa_printf(MSG_WARNING, "Could not set WEP "
577 "encryption.");
578 return -1;
579 }
580 if (hapd->conf->ssid.wep.key[i] &&
581 i == hapd->conf->ssid.wep.idx)
582 hostapd_set_privacy(hapd, 1);
583 }
584
585 return 0;
586}
587
Hai Shalomfdcde762020-04-02 11:19:20 -0700588#endif /* CONFIG_WEP */
589
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700590
Dmitry Shmidt04949592012-07-19 12:16:46 -0700591static int hostapd_flush_old_stations(struct hostapd_data *hapd, u16 reason)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700592{
593 int ret = 0;
594 u8 addr[ETH_ALEN];
595
596 if (hostapd_drv_none(hapd) || hapd->drv_priv == NULL)
597 return 0;
598
Dmitry Shmidta38abf92014-03-06 13:38:44 -0800599 if (!hapd->iface->driver_ap_teardown) {
600 wpa_dbg(hapd->msg_ctx, MSG_DEBUG,
601 "Flushing old station entries");
602
603 if (hostapd_flush(hapd)) {
604 wpa_msg(hapd->msg_ctx, MSG_WARNING,
605 "Could not connect to kernel driver");
606 ret = -1;
607 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700608 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700609 if (hapd->conf && hapd->conf->broadcast_deauth) {
610 wpa_dbg(hapd->msg_ctx, MSG_DEBUG,
611 "Deauthenticate all stations");
612 os_memset(addr, 0xff, ETH_ALEN);
613 hostapd_drv_sta_deauth(hapd, addr, reason);
614 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700615 hostapd_free_stas(hapd);
616
617 return ret;
618}
619
620
Dmitry Shmidt71757432014-06-02 13:50:35 -0700621static void hostapd_bss_deinit_no_free(struct hostapd_data *hapd)
622{
623 hostapd_free_stas(hapd);
624 hostapd_flush_old_stations(hapd, WLAN_REASON_DEAUTH_LEAVING);
Hai Shalomfdcde762020-04-02 11:19:20 -0700625#ifdef CONFIG_WEP
Dmitry Shmidt71757432014-06-02 13:50:35 -0700626 hostapd_clear_wep(hapd);
Hai Shalomfdcde762020-04-02 11:19:20 -0700627#endif /* CONFIG_WEP */
Dmitry Shmidt71757432014-06-02 13:50:35 -0700628}
629
630
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700631/**
632 * hostapd_validate_bssid_configuration - Validate BSSID configuration
633 * @iface: Pointer to interface data
634 * Returns: 0 on success, -1 on failure
635 *
636 * This function is used to validate that the configured BSSIDs are valid.
637 */
638static int hostapd_validate_bssid_configuration(struct hostapd_iface *iface)
639{
640 u8 mask[ETH_ALEN] = { 0 };
641 struct hostapd_data *hapd = iface->bss[0];
642 unsigned int i = iface->conf->num_bss, bits = 0, j;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700643 int auto_addr = 0;
644
645 if (hostapd_drv_none(hapd))
646 return 0;
647
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -0800648 if (iface->conf->use_driver_iface_addr)
649 return 0;
650
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700651 /* Generate BSSID mask that is large enough to cover the BSSIDs. */
652
653 /* Determine the bits necessary to cover the number of BSSIDs. */
654 for (i--; i; i >>= 1)
655 bits++;
656
657 /* Determine the bits necessary to any configured BSSIDs,
658 if they are higher than the number of BSSIDs. */
659 for (j = 0; j < iface->conf->num_bss; j++) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800660 if (is_zero_ether_addr(iface->conf->bss[j]->bssid)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700661 if (j)
662 auto_addr++;
663 continue;
664 }
665
666 for (i = 0; i < ETH_ALEN; i++) {
667 mask[i] |=
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800668 iface->conf->bss[j]->bssid[i] ^
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700669 hapd->own_addr[i];
670 }
671 }
672
673 if (!auto_addr)
674 goto skip_mask_ext;
675
676 for (i = 0; i < ETH_ALEN && mask[i] == 0; i++)
677 ;
678 j = 0;
679 if (i < ETH_ALEN) {
680 j = (5 - i) * 8;
681
682 while (mask[i] != 0) {
683 mask[i] >>= 1;
684 j++;
685 }
686 }
687
688 if (bits < j)
689 bits = j;
690
691 if (bits > 40) {
692 wpa_printf(MSG_ERROR, "Too many bits in the BSSID mask (%u)",
693 bits);
694 return -1;
695 }
696
697 os_memset(mask, 0xff, ETH_ALEN);
698 j = bits / 8;
699 for (i = 5; i > 5 - j; i--)
700 mask[i] = 0;
701 j = bits % 8;
Hai Shalom021b0b52019-04-10 11:17:58 -0700702 while (j) {
703 j--;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700704 mask[i] <<= 1;
Hai Shalom021b0b52019-04-10 11:17:58 -0700705 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700706
707skip_mask_ext:
708 wpa_printf(MSG_DEBUG, "BSS count %lu, BSSID mask " MACSTR " (%d bits)",
709 (unsigned long) iface->conf->num_bss, MAC2STR(mask), bits);
710
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700711 if (!auto_addr)
712 return 0;
713
714 for (i = 0; i < ETH_ALEN; i++) {
715 if ((hapd->own_addr[i] & mask[i]) != hapd->own_addr[i]) {
716 wpa_printf(MSG_ERROR, "Invalid BSSID mask " MACSTR
717 " for start address " MACSTR ".",
718 MAC2STR(mask), MAC2STR(hapd->own_addr));
719 wpa_printf(MSG_ERROR, "Start address must be the "
720 "first address in the block (i.e., addr "
721 "AND mask == addr).");
722 return -1;
723 }
724 }
725
726 return 0;
727}
728
729
730static int mac_in_conf(struct hostapd_config *conf, const void *a)
731{
732 size_t i;
733
734 for (i = 0; i < conf->num_bss; i++) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800735 if (hostapd_mac_comp(conf->bss[i]->bssid, a) == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700736 return 1;
737 }
738 }
739
740 return 0;
741}
742
743
Dmitry Shmidt04949592012-07-19 12:16:46 -0700744#ifndef CONFIG_NO_RADIUS
745
746static int hostapd_das_nas_mismatch(struct hostapd_data *hapd,
747 struct radius_das_attrs *attr)
748{
Dmitry Shmidt13ca8d82014-02-20 10:18:40 -0800749 if (attr->nas_identifier &&
750 (!hapd->conf->nas_identifier ||
751 os_strlen(hapd->conf->nas_identifier) !=
752 attr->nas_identifier_len ||
753 os_memcmp(hapd->conf->nas_identifier, attr->nas_identifier,
754 attr->nas_identifier_len) != 0)) {
755 wpa_printf(MSG_DEBUG, "RADIUS DAS: NAS-Identifier mismatch");
756 return 1;
757 }
758
759 if (attr->nas_ip_addr &&
760 (hapd->conf->own_ip_addr.af != AF_INET ||
761 os_memcmp(&hapd->conf->own_ip_addr.u.v4, attr->nas_ip_addr, 4) !=
762 0)) {
763 wpa_printf(MSG_DEBUG, "RADIUS DAS: NAS-IP-Address mismatch");
764 return 1;
765 }
766
767#ifdef CONFIG_IPV6
768 if (attr->nas_ipv6_addr &&
769 (hapd->conf->own_ip_addr.af != AF_INET6 ||
770 os_memcmp(&hapd->conf->own_ip_addr.u.v6, attr->nas_ipv6_addr, 16)
771 != 0)) {
772 wpa_printf(MSG_DEBUG, "RADIUS DAS: NAS-IPv6-Address mismatch");
773 return 1;
774 }
775#endif /* CONFIG_IPV6 */
776
Dmitry Shmidt04949592012-07-19 12:16:46 -0700777 return 0;
778}
779
780
781static struct sta_info * hostapd_das_find_sta(struct hostapd_data *hapd,
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800782 struct radius_das_attrs *attr,
783 int *multi)
Dmitry Shmidt04949592012-07-19 12:16:46 -0700784{
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800785 struct sta_info *selected, *sta;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700786 char buf[128];
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800787 int num_attr = 0;
788 int count;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700789
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800790 *multi = 0;
791
792 for (sta = hapd->sta_list; sta; sta = sta->next)
793 sta->radius_das_match = 1;
794
795 if (attr->sta_addr) {
796 num_attr++;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700797 sta = ap_get_sta(hapd, attr->sta_addr);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800798 if (!sta) {
799 wpa_printf(MSG_DEBUG,
800 "RADIUS DAS: No Calling-Station-Id match");
801 return NULL;
802 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700803
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800804 selected = sta;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700805 for (sta = hapd->sta_list; sta; sta = sta->next) {
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800806 if (sta != selected)
807 sta->radius_das_match = 0;
808 }
809 wpa_printf(MSG_DEBUG, "RADIUS DAS: Calling-Station-Id match");
810 }
811
812 if (attr->acct_session_id) {
813 num_attr++;
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800814 if (attr->acct_session_id_len != 16) {
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800815 wpa_printf(MSG_DEBUG,
816 "RADIUS DAS: Acct-Session-Id cannot match");
817 return NULL;
818 }
819 count = 0;
820
821 for (sta = hapd->sta_list; sta; sta = sta->next) {
822 if (!sta->radius_das_match)
823 continue;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800824 os_snprintf(buf, sizeof(buf), "%016llX",
825 (unsigned long long) sta->acct_session_id);
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800826 if (os_memcmp(attr->acct_session_id, buf, 16) != 0)
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800827 sta->radius_das_match = 0;
828 else
829 count++;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700830 }
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800831
832 if (count == 0) {
833 wpa_printf(MSG_DEBUG,
834 "RADIUS DAS: No matches remaining after Acct-Session-Id check");
835 return NULL;
836 }
837 wpa_printf(MSG_DEBUG, "RADIUS DAS: Acct-Session-Id match");
Dmitry Shmidt04949592012-07-19 12:16:46 -0700838 }
839
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800840 if (attr->acct_multi_session_id) {
841 num_attr++;
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800842 if (attr->acct_multi_session_id_len != 16) {
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800843 wpa_printf(MSG_DEBUG,
844 "RADIUS DAS: Acct-Multi-Session-Id cannot match");
845 return NULL;
846 }
847 count = 0;
848
849 for (sta = hapd->sta_list; sta; sta = sta->next) {
850 if (!sta->radius_das_match)
851 continue;
852 if (!sta->eapol_sm ||
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800853 !sta->eapol_sm->acct_multi_session_id) {
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800854 sta->radius_das_match = 0;
855 continue;
856 }
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800857 os_snprintf(buf, sizeof(buf), "%016llX",
858 (unsigned long long)
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800859 sta->eapol_sm->acct_multi_session_id);
860 if (os_memcmp(attr->acct_multi_session_id, buf, 16) !=
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800861 0)
862 sta->radius_das_match = 0;
863 else
864 count++;
865 }
866
867 if (count == 0) {
868 wpa_printf(MSG_DEBUG,
869 "RADIUS DAS: No matches remaining after Acct-Multi-Session-Id check");
870 return NULL;
871 }
872 wpa_printf(MSG_DEBUG,
873 "RADIUS DAS: Acct-Multi-Session-Id match");
874 }
875
876 if (attr->cui) {
877 num_attr++;
878 count = 0;
879
Dmitry Shmidt04949592012-07-19 12:16:46 -0700880 for (sta = hapd->sta_list; sta; sta = sta->next) {
881 struct wpabuf *cui;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800882
883 if (!sta->radius_das_match)
884 continue;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700885 cui = ieee802_1x_get_radius_cui(sta->eapol_sm);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800886 if (!cui || wpabuf_len(cui) != attr->cui_len ||
Dmitry Shmidt04949592012-07-19 12:16:46 -0700887 os_memcmp(wpabuf_head(cui), attr->cui,
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800888 attr->cui_len) != 0)
889 sta->radius_das_match = 0;
890 else
891 count++;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700892 }
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800893
894 if (count == 0) {
895 wpa_printf(MSG_DEBUG,
896 "RADIUS DAS: No matches remaining after Chargeable-User-Identity check");
897 return NULL;
898 }
899 wpa_printf(MSG_DEBUG,
900 "RADIUS DAS: Chargeable-User-Identity match");
Dmitry Shmidt04949592012-07-19 12:16:46 -0700901 }
902
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800903 if (attr->user_name) {
904 num_attr++;
905 count = 0;
906
Dmitry Shmidt04949592012-07-19 12:16:46 -0700907 for (sta = hapd->sta_list; sta; sta = sta->next) {
908 u8 *identity;
909 size_t identity_len;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800910
911 if (!sta->radius_das_match)
912 continue;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700913 identity = ieee802_1x_get_identity(sta->eapol_sm,
914 &identity_len);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800915 if (!identity ||
916 identity_len != attr->user_name_len ||
Dmitry Shmidt04949592012-07-19 12:16:46 -0700917 os_memcmp(identity, attr->user_name, identity_len)
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800918 != 0)
919 sta->radius_das_match = 0;
920 else
921 count++;
922 }
923
924 if (count == 0) {
925 wpa_printf(MSG_DEBUG,
926 "RADIUS DAS: No matches remaining after User-Name check");
927 return NULL;
928 }
929 wpa_printf(MSG_DEBUG,
930 "RADIUS DAS: User-Name match");
931 }
932
933 if (num_attr == 0) {
934 /*
935 * In theory, we could match all current associations, but it
936 * seems safer to just reject requests that do not include any
937 * session identification attributes.
938 */
939 wpa_printf(MSG_DEBUG,
940 "RADIUS DAS: No session identification attributes included");
941 return NULL;
942 }
943
944 selected = NULL;
945 for (sta = hapd->sta_list; sta; sta = sta->next) {
946 if (sta->radius_das_match) {
947 if (selected) {
948 *multi = 1;
949 return NULL;
950 }
951 selected = sta;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700952 }
953 }
954
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800955 return selected;
956}
957
958
959static int hostapd_das_disconnect_pmksa(struct hostapd_data *hapd,
960 struct radius_das_attrs *attr)
961{
962 if (!hapd->wpa_auth)
963 return -1;
964 return wpa_auth_radius_das_disconnect_pmksa(hapd->wpa_auth, attr);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700965}
966
967
968static enum radius_das_res
969hostapd_das_disconnect(void *ctx, struct radius_das_attrs *attr)
970{
971 struct hostapd_data *hapd = ctx;
972 struct sta_info *sta;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800973 int multi;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700974
975 if (hostapd_das_nas_mismatch(hapd, attr))
976 return RADIUS_DAS_NAS_MISMATCH;
977
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800978 sta = hostapd_das_find_sta(hapd, attr, &multi);
979 if (sta == NULL) {
980 if (multi) {
981 wpa_printf(MSG_DEBUG,
982 "RADIUS DAS: Multiple sessions match - not supported");
983 return RADIUS_DAS_MULTI_SESSION_MATCH;
984 }
985 if (hostapd_das_disconnect_pmksa(hapd, attr) == 0) {
986 wpa_printf(MSG_DEBUG,
987 "RADIUS DAS: PMKSA cache entry matched");
988 return RADIUS_DAS_SUCCESS;
989 }
990 wpa_printf(MSG_DEBUG, "RADIUS DAS: No matching session found");
Dmitry Shmidt04949592012-07-19 12:16:46 -0700991 return RADIUS_DAS_SESSION_NOT_FOUND;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800992 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700993
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800994 wpa_printf(MSG_DEBUG, "RADIUS DAS: Found a matching session " MACSTR
995 " - disconnecting", MAC2STR(sta->addr));
Dmitry Shmidt13ca8d82014-02-20 10:18:40 -0800996 wpa_auth_pmksa_remove(hapd->wpa_auth, sta->addr);
997
Dmitry Shmidt04949592012-07-19 12:16:46 -0700998 hostapd_drv_sta_deauth(hapd, sta->addr,
999 WLAN_REASON_PREV_AUTH_NOT_VALID);
1000 ap_sta_deauthenticate(hapd, sta, WLAN_REASON_PREV_AUTH_NOT_VALID);
1001
1002 return RADIUS_DAS_SUCCESS;
1003}
1004
Roshan Pius3a1667e2018-07-03 15:17:14 -07001005
1006#ifdef CONFIG_HS20
1007static enum radius_das_res
1008hostapd_das_coa(void *ctx, struct radius_das_attrs *attr)
1009{
1010 struct hostapd_data *hapd = ctx;
1011 struct sta_info *sta;
1012 int multi;
1013
1014 if (hostapd_das_nas_mismatch(hapd, attr))
1015 return RADIUS_DAS_NAS_MISMATCH;
1016
1017 sta = hostapd_das_find_sta(hapd, attr, &multi);
1018 if (!sta) {
1019 if (multi) {
1020 wpa_printf(MSG_DEBUG,
1021 "RADIUS DAS: Multiple sessions match - not supported");
1022 return RADIUS_DAS_MULTI_SESSION_MATCH;
1023 }
1024 wpa_printf(MSG_DEBUG, "RADIUS DAS: No matching session found");
1025 return RADIUS_DAS_SESSION_NOT_FOUND;
1026 }
1027
1028 wpa_printf(MSG_DEBUG, "RADIUS DAS: Found a matching session " MACSTR
1029 " - CoA", MAC2STR(sta->addr));
1030
1031 if (attr->hs20_t_c_filtering) {
1032 if (attr->hs20_t_c_filtering[0] & BIT(0)) {
1033 wpa_printf(MSG_DEBUG,
1034 "HS 2.0: Unexpected Terms and Conditions filtering required in CoA-Request");
1035 return RADIUS_DAS_COA_FAILED;
1036 }
1037
1038 hs20_t_c_filtering(hapd, sta, 0);
1039 }
1040
1041 return RADIUS_DAS_SUCCESS;
1042}
1043#else /* CONFIG_HS20 */
1044#define hostapd_das_coa NULL
1045#endif /* CONFIG_HS20 */
1046
Hai Shalomc3565922019-10-28 11:58:20 -07001047
1048#ifdef CONFIG_SQLITE
1049
1050static int db_table_exists(sqlite3 *db, const char *name)
1051{
1052 char cmd[128];
1053
1054 os_snprintf(cmd, sizeof(cmd), "SELECT 1 FROM %s;", name);
1055 return sqlite3_exec(db, cmd, NULL, NULL, NULL) == SQLITE_OK;
1056}
1057
1058
1059static int db_table_create_radius_attributes(sqlite3 *db)
1060{
1061 char *err = NULL;
1062 const char *sql =
1063 "CREATE TABLE radius_attributes("
1064 " id INTEGER PRIMARY KEY,"
1065 " sta TEXT,"
1066 " reqtype TEXT,"
1067 " attr TEXT"
1068 ");"
1069 "CREATE INDEX idx_sta_reqtype ON radius_attributes(sta,reqtype);";
1070
1071 wpa_printf(MSG_DEBUG,
1072 "Adding database table for RADIUS attribute information");
1073 if (sqlite3_exec(db, sql, NULL, NULL, &err) != SQLITE_OK) {
1074 wpa_printf(MSG_ERROR, "SQLite error: %s", err);
1075 sqlite3_free(err);
1076 return -1;
1077 }
1078
1079 return 0;
1080}
1081
1082#endif /* CONFIG_SQLITE */
1083
Dmitry Shmidt04949592012-07-19 12:16:46 -07001084#endif /* CONFIG_NO_RADIUS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001085
1086
1087/**
1088 * hostapd_setup_bss - Per-BSS setup (initialization)
1089 * @hapd: Pointer to BSS data
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001090 * @first: Whether this BSS is the first BSS of an interface; -1 = not first,
1091 * but interface may exist
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001092 *
1093 * This function is used to initialize all per-BSS data structures and
1094 * resources. This gets called in a loop for each BSS when an interface is
1095 * initialized. Most of the modules that are initialized here will be
1096 * deinitialized in hostapd_cleanup().
1097 */
1098static int hostapd_setup_bss(struct hostapd_data *hapd, int first)
1099{
1100 struct hostapd_bss_config *conf = hapd->conf;
Dmitry Shmidt9d9e6022015-04-23 10:34:55 -07001101 u8 ssid[SSID_MAX_LEN + 1];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001102 int ssid_len, set_ssid;
1103 char force_ifname[IFNAMSIZ];
1104 u8 if_addr[ETH_ALEN];
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001105 int flush_old_stations = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001106
Dmitry Shmidt54605472013-11-08 11:10:19 -08001107 wpa_printf(MSG_DEBUG, "%s(hapd=%p (%s), first=%d)",
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001108 __func__, hapd, conf->iface, first);
Dmitry Shmidt54605472013-11-08 11:10:19 -08001109
Dmitry Shmidt50b691d2014-05-21 14:01:45 -07001110#ifdef EAP_SERVER_TNC
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001111 if (conf->tnc && tncs_global_init() < 0) {
Dmitry Shmidt50b691d2014-05-21 14:01:45 -07001112 wpa_printf(MSG_ERROR, "Failed to initialize TNCS");
1113 return -1;
1114 }
1115#endif /* EAP_SERVER_TNC */
1116
Dmitry Shmidt54605472013-11-08 11:10:19 -08001117 if (hapd->started) {
1118 wpa_printf(MSG_ERROR, "%s: Interface %s was already started",
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001119 __func__, conf->iface);
Dmitry Shmidt54605472013-11-08 11:10:19 -08001120 return -1;
1121 }
1122 hapd->started = 1;
1123
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001124 if (!first || first == -1) {
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08001125 u8 *addr = hapd->own_addr;
1126
1127 if (!is_zero_ether_addr(conf->bssid)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001128 /* Allocate the configured BSSID. */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001129 os_memcpy(hapd->own_addr, conf->bssid, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001130
1131 if (hostapd_mac_comp(hapd->own_addr,
1132 hapd->iface->bss[0]->own_addr) ==
1133 0) {
1134 wpa_printf(MSG_ERROR, "BSS '%s' may not have "
1135 "BSSID set to the MAC address of "
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001136 "the radio", conf->iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001137 return -1;
1138 }
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08001139 } else if (hapd->iconf->use_driver_iface_addr) {
1140 addr = NULL;
1141 } else {
1142 /* Allocate the next available BSSID. */
1143 do {
1144 inc_byte_array(hapd->own_addr, ETH_ALEN);
1145 } while (mac_in_conf(hapd->iconf, hapd->own_addr));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001146 }
1147
Dmitry Shmidt54605472013-11-08 11:10:19 -08001148 hapd->interface_added = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001149 if (hostapd_if_add(hapd->iface->bss[0], WPA_IF_AP_BSS,
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08001150 conf->iface, addr, hapd,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001151 &hapd->drv_priv, force_ifname, if_addr,
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001152 conf->bridge[0] ? conf->bridge : NULL,
1153 first == -1)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001154 wpa_printf(MSG_ERROR, "Failed to add BSS (BSSID="
1155 MACSTR ")", MAC2STR(hapd->own_addr));
Dmitry Shmidt3cf6f792013-12-18 13:12:19 -08001156 hapd->interface_added = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001157 return -1;
1158 }
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08001159
1160 if (!addr)
1161 os_memcpy(hapd->own_addr, if_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001162 }
1163
1164 if (conf->wmm_enabled < 0)
1165 conf->wmm_enabled = hapd->iconf->ieee80211n;
1166
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001167#ifdef CONFIG_IEEE80211R_AP
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001168 if (is_zero_ether_addr(conf->r1_key_holder))
1169 os_memcpy(conf->r1_key_holder, hapd->own_addr, ETH_ALEN);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001170#endif /* CONFIG_IEEE80211R_AP */
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001171
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001172#ifdef CONFIG_MESH
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001173 if ((hapd->conf->mesh & MESH_ENABLED) && hapd->iface->mconf == NULL)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001174 flush_old_stations = 0;
1175#endif /* CONFIG_MESH */
1176
1177 if (flush_old_stations)
1178 hostapd_flush_old_stations(hapd,
1179 WLAN_REASON_PREV_AUTH_NOT_VALID);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001180 hostapd_set_privacy(hapd, 0);
1181
Hai Shalomfdcde762020-04-02 11:19:20 -07001182#ifdef CONFIG_WEP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001183 hostapd_broadcast_wep_clear(hapd);
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001184 if (hostapd_setup_encryption(conf->iface, hapd))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001185 return -1;
Hai Shalomfdcde762020-04-02 11:19:20 -07001186#endif /* CONFIG_WEP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001187
1188 /*
1189 * Fetch the SSID from the system and use it or,
1190 * if one was specified in the config file, verify they
1191 * match.
1192 */
1193 ssid_len = hostapd_get_ssid(hapd, ssid, sizeof(ssid));
1194 if (ssid_len < 0) {
1195 wpa_printf(MSG_ERROR, "Could not read SSID from system");
1196 return -1;
1197 }
1198 if (conf->ssid.ssid_set) {
1199 /*
1200 * If SSID is specified in the config file and it differs
1201 * from what is being used then force installation of the
1202 * new SSID.
1203 */
1204 set_ssid = (conf->ssid.ssid_len != (size_t) ssid_len ||
1205 os_memcmp(conf->ssid.ssid, ssid, ssid_len) != 0);
1206 } else {
1207 /*
1208 * No SSID in the config file; just use the one we got
1209 * from the system.
1210 */
1211 set_ssid = 0;
1212 conf->ssid.ssid_len = ssid_len;
1213 os_memcpy(conf->ssid.ssid, ssid, conf->ssid.ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001214 }
1215
Hai Shalomfdcde762020-04-02 11:19:20 -07001216 /*
1217 * Short SSID calculation is identical to FCS and it is defined in
1218 * IEEE P802.11-REVmd/D3.0, 9.4.2.170.3 (Calculating the Short-SSID).
1219 */
1220 conf->ssid.short_ssid = crc32(conf->ssid.ssid, conf->ssid.ssid_len);
1221
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001222 if (!hostapd_drv_none(hapd)) {
Hai Shalomfdcde762020-04-02 11:19:20 -07001223 wpa_printf(MSG_DEBUG, "Using interface %s with hwaddr " MACSTR
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001224 " and ssid \"%s\"",
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001225 conf->iface, MAC2STR(hapd->own_addr),
1226 wpa_ssid_txt(conf->ssid.ssid, conf->ssid.ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001227 }
1228
1229 if (hostapd_setup_wpa_psk(conf)) {
1230 wpa_printf(MSG_ERROR, "WPA-PSK setup failed.");
1231 return -1;
1232 }
1233
1234 /* Set SSID for the kernel driver (to be used in beacon and probe
1235 * response frames) */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001236 if (set_ssid && hostapd_set_ssid(hapd, conf->ssid.ssid,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001237 conf->ssid.ssid_len)) {
1238 wpa_printf(MSG_ERROR, "Could not set SSID for kernel driver");
1239 return -1;
1240 }
1241
Dmitry Shmidt818ea482014-03-10 13:15:21 -07001242 if (wpa_debug_level <= MSG_MSGDUMP)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001243 conf->radius->msg_dumps = 1;
1244#ifndef CONFIG_NO_RADIUS
Hai Shalomc3565922019-10-28 11:58:20 -07001245
1246#ifdef CONFIG_SQLITE
1247 if (conf->radius_req_attr_sqlite) {
1248 if (sqlite3_open(conf->radius_req_attr_sqlite,
1249 &hapd->rad_attr_db)) {
1250 wpa_printf(MSG_ERROR, "Could not open SQLite file '%s'",
1251 conf->radius_req_attr_sqlite);
1252 return -1;
1253 }
1254
1255 wpa_printf(MSG_DEBUG, "Opening RADIUS attribute database: %s",
1256 conf->radius_req_attr_sqlite);
1257 if (!db_table_exists(hapd->rad_attr_db, "radius_attributes") &&
1258 db_table_create_radius_attributes(hapd->rad_attr_db) < 0)
1259 return -1;
1260 }
1261#endif /* CONFIG_SQLITE */
1262
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001263 hapd->radius = radius_client_init(hapd, conf->radius);
1264 if (hapd->radius == NULL) {
1265 wpa_printf(MSG_ERROR, "RADIUS client initialization failed.");
1266 return -1;
1267 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001268
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001269 if (conf->radius_das_port) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001270 struct radius_das_conf das_conf;
1271 os_memset(&das_conf, 0, sizeof(das_conf));
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001272 das_conf.port = conf->radius_das_port;
1273 das_conf.shared_secret = conf->radius_das_shared_secret;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001274 das_conf.shared_secret_len =
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001275 conf->radius_das_shared_secret_len;
1276 das_conf.client_addr = &conf->radius_das_client_addr;
1277 das_conf.time_window = conf->radius_das_time_window;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001278 das_conf.require_event_timestamp =
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001279 conf->radius_das_require_event_timestamp;
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07001280 das_conf.require_message_authenticator =
1281 conf->radius_das_require_message_authenticator;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001282 das_conf.ctx = hapd;
1283 das_conf.disconnect = hostapd_das_disconnect;
Roshan Pius3a1667e2018-07-03 15:17:14 -07001284 das_conf.coa = hostapd_das_coa;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001285 hapd->radius_das = radius_das_init(&das_conf);
1286 if (hapd->radius_das == NULL) {
1287 wpa_printf(MSG_ERROR, "RADIUS DAS initialization "
1288 "failed.");
1289 return -1;
1290 }
1291 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001292#endif /* CONFIG_NO_RADIUS */
1293
1294 if (hostapd_acl_init(hapd)) {
1295 wpa_printf(MSG_ERROR, "ACL initialization failed.");
1296 return -1;
1297 }
1298 if (hostapd_init_wps(hapd, conf))
1299 return -1;
1300
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001301#ifdef CONFIG_DPP
1302 hapd->gas = gas_query_ap_init(hapd, hapd->msg_ctx);
1303 if (!hapd->gas)
1304 return -1;
1305 if (hostapd_dpp_init(hapd))
1306 return -1;
1307#endif /* CONFIG_DPP */
1308
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001309 if (authsrv_init(hapd) < 0)
1310 return -1;
1311
1312 if (ieee802_1x_init(hapd)) {
1313 wpa_printf(MSG_ERROR, "IEEE 802.1X initialization failed.");
1314 return -1;
1315 }
1316
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001317 if ((conf->wpa || conf->osen) && hostapd_setup_wpa(hapd))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001318 return -1;
1319
1320 if (accounting_init(hapd)) {
1321 wpa_printf(MSG_ERROR, "Accounting initialization failed.");
1322 return -1;
1323 }
1324
Dmitry Shmidt04949592012-07-19 12:16:46 -07001325#ifdef CONFIG_INTERWORKING
1326 if (gas_serv_init(hapd)) {
1327 wpa_printf(MSG_ERROR, "GAS server initialization failed");
1328 return -1;
1329 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001330
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001331 if (conf->qos_map_set_len &&
1332 hostapd_drv_set_qos_map(hapd, conf->qos_map_set,
1333 conf->qos_map_set_len)) {
1334 wpa_printf(MSG_ERROR, "Failed to initialize QoS Map");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001335 return -1;
1336 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001337#endif /* CONFIG_INTERWORKING */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001338
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001339 if (conf->bss_load_update_period && bss_load_update_init(hapd)) {
1340 wpa_printf(MSG_ERROR, "BSS Load initialization failed");
1341 return -1;
1342 }
1343
1344 if (conf->proxy_arp) {
1345 if (x_snoop_init(hapd)) {
1346 wpa_printf(MSG_ERROR,
1347 "Generic snooping infrastructure initialization failed");
1348 return -1;
1349 }
1350
1351 if (dhcp_snoop_init(hapd)) {
1352 wpa_printf(MSG_ERROR,
1353 "DHCP snooping initialization failed");
1354 return -1;
1355 }
1356
1357 if (ndisc_snoop_init(hapd)) {
1358 wpa_printf(MSG_ERROR,
1359 "Neighbor Discovery snooping initialization failed");
1360 return -1;
1361 }
1362 }
1363
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001364 if (!hostapd_drv_none(hapd) && vlan_init(hapd)) {
1365 wpa_printf(MSG_ERROR, "VLAN initialization failed.");
1366 return -1;
1367 }
1368
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001369 if (!conf->start_disabled && ieee802_11_set_beacon(hapd) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001370 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001371
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001372 if (hapd->wpa_auth && wpa_init_keys(hapd->wpa_auth) < 0)
1373 return -1;
1374
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001375 if (hapd->driver && hapd->driver->set_operstate)
1376 hapd->driver->set_operstate(hapd->drv_priv, 1);
1377
1378 return 0;
1379}
1380
1381
1382static void hostapd_tx_queue_params(struct hostapd_iface *iface)
1383{
1384 struct hostapd_data *hapd = iface->bss[0];
1385 int i;
1386 struct hostapd_tx_queue_params *p;
1387
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001388#ifdef CONFIG_MESH
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001389 if ((hapd->conf->mesh & MESH_ENABLED) && iface->mconf == NULL)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001390 return;
1391#endif /* CONFIG_MESH */
1392
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001393 for (i = 0; i < NUM_TX_QUEUES; i++) {
1394 p = &iface->conf->tx_queue[i];
1395
1396 if (hostapd_set_tx_queue_params(hapd, i, p->aifs, p->cwmin,
1397 p->cwmax, p->burst)) {
1398 wpa_printf(MSG_DEBUG, "Failed to set TX queue "
1399 "parameters for queue %d.", i);
1400 /* Continue anyway */
1401 }
1402 }
1403}
1404
1405
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001406static int hostapd_set_acl_list(struct hostapd_data *hapd,
1407 struct mac_acl_entry *mac_acl,
1408 int n_entries, u8 accept_acl)
1409{
1410 struct hostapd_acl_params *acl_params;
1411 int i, err;
1412
1413 acl_params = os_zalloc(sizeof(*acl_params) +
1414 (n_entries * sizeof(acl_params->mac_acl[0])));
1415 if (!acl_params)
1416 return -ENOMEM;
1417
1418 for (i = 0; i < n_entries; i++)
1419 os_memcpy(acl_params->mac_acl[i].addr, mac_acl[i].addr,
1420 ETH_ALEN);
1421
1422 acl_params->acl_policy = accept_acl;
1423 acl_params->num_mac_acl = n_entries;
1424
1425 err = hostapd_drv_set_acl(hapd, acl_params);
1426
1427 os_free(acl_params);
1428
1429 return err;
1430}
1431
1432
1433static void hostapd_set_acl(struct hostapd_data *hapd)
1434{
1435 struct hostapd_config *conf = hapd->iconf;
1436 int err;
1437 u8 accept_acl;
1438
1439 if (hapd->iface->drv_max_acl_mac_addrs == 0)
1440 return;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001441
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001442 if (conf->bss[0]->macaddr_acl == DENY_UNLESS_ACCEPTED) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001443 accept_acl = 1;
1444 err = hostapd_set_acl_list(hapd, conf->bss[0]->accept_mac,
1445 conf->bss[0]->num_accept_mac,
1446 accept_acl);
1447 if (err) {
1448 wpa_printf(MSG_DEBUG, "Failed to set accept acl");
1449 return;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001450 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001451 } else if (conf->bss[0]->macaddr_acl == ACCEPT_UNLESS_DENIED) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001452 accept_acl = 0;
1453 err = hostapd_set_acl_list(hapd, conf->bss[0]->deny_mac,
1454 conf->bss[0]->num_deny_mac,
1455 accept_acl);
1456 if (err) {
1457 wpa_printf(MSG_DEBUG, "Failed to set deny acl");
1458 return;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001459 }
1460 }
1461}
1462
1463
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001464static int start_ctrl_iface_bss(struct hostapd_data *hapd)
1465{
1466 if (!hapd->iface->interfaces ||
1467 !hapd->iface->interfaces->ctrl_iface_init)
1468 return 0;
1469
1470 if (hapd->iface->interfaces->ctrl_iface_init(hapd)) {
1471 wpa_printf(MSG_ERROR,
1472 "Failed to setup control interface for %s",
1473 hapd->conf->iface);
1474 return -1;
1475 }
1476
1477 return 0;
1478}
1479
1480
1481static int start_ctrl_iface(struct hostapd_iface *iface)
1482{
1483 size_t i;
1484
1485 if (!iface->interfaces || !iface->interfaces->ctrl_iface_init)
1486 return 0;
1487
1488 for (i = 0; i < iface->num_bss; i++) {
1489 struct hostapd_data *hapd = iface->bss[i];
1490 if (iface->interfaces->ctrl_iface_init(hapd)) {
1491 wpa_printf(MSG_ERROR,
1492 "Failed to setup control interface for %s",
1493 hapd->conf->iface);
1494 return -1;
1495 }
1496 }
1497
1498 return 0;
1499}
1500
1501
1502static void channel_list_update_timeout(void *eloop_ctx, void *timeout_ctx)
1503{
1504 struct hostapd_iface *iface = eloop_ctx;
1505
1506 if (!iface->wait_channel_update) {
1507 wpa_printf(MSG_INFO, "Channel list update timeout, but interface was not waiting for it");
1508 return;
1509 }
1510
1511 /*
1512 * It is possible that the existing channel list is acceptable, so try
1513 * to proceed.
1514 */
1515 wpa_printf(MSG_DEBUG, "Channel list update timeout - try to continue anyway");
1516 setup_interface2(iface);
1517}
1518
1519
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001520void hostapd_channel_list_updated(struct hostapd_iface *iface, int initiator)
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001521{
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001522 if (!iface->wait_channel_update || initiator != REGDOM_SET_BY_USER)
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001523 return;
1524
1525 wpa_printf(MSG_DEBUG, "Channel list updated - continue setup");
1526 eloop_cancel_timeout(channel_list_update_timeout, iface, NULL);
1527 setup_interface2(iface);
1528}
1529
1530
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001531static int setup_interface(struct hostapd_iface *iface)
1532{
1533 struct hostapd_data *hapd = iface->bss[0];
1534 size_t i;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001535
Dmitry Shmidta38abf92014-03-06 13:38:44 -08001536 /*
1537 * It is possible that setup_interface() is called after the interface
1538 * was disabled etc., in which case driver_ap_teardown is possibly set
1539 * to 1. Clear it here so any other key/station deletion, which is not
1540 * part of a teardown flow, would also call the relevant driver
1541 * callbacks.
1542 */
1543 iface->driver_ap_teardown = 0;
1544
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001545 if (!iface->phy[0]) {
1546 const char *phy = hostapd_drv_get_radio_name(hapd);
1547 if (phy) {
1548 wpa_printf(MSG_DEBUG, "phy: %s", phy);
1549 os_strlcpy(iface->phy, phy, sizeof(iface->phy));
1550 }
1551 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001552
1553 /*
1554 * Make sure that all BSSes get configured with a pointer to the same
1555 * driver interface.
1556 */
1557 for (i = 1; i < iface->num_bss; i++) {
1558 iface->bss[i]->driver = hapd->driver;
1559 iface->bss[i]->drv_priv = hapd->drv_priv;
1560 }
1561
1562 if (hostapd_validate_bssid_configuration(iface))
1563 return -1;
1564
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001565 /*
1566 * Initialize control interfaces early to allow external monitoring of
1567 * channel setup operations that may take considerable amount of time
1568 * especially for DFS cases.
1569 */
1570 if (start_ctrl_iface(iface))
1571 return -1;
1572
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001573 if (hapd->iconf->country[0] && hapd->iconf->country[1]) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001574 char country[4], previous_country[4];
1575
1576 hostapd_set_state(iface, HAPD_IFACE_COUNTRY_UPDATE);
1577 if (hostapd_get_country(hapd, previous_country) < 0)
1578 previous_country[0] = '\0';
1579
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001580 os_memcpy(country, hapd->iconf->country, 3);
1581 country[3] = '\0';
1582 if (hostapd_set_country(hapd, country) < 0) {
1583 wpa_printf(MSG_ERROR, "Failed to set country code");
1584 return -1;
1585 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001586
1587 wpa_printf(MSG_DEBUG, "Previous country code %s, new country code %s",
1588 previous_country, country);
1589
1590 if (os_strncmp(previous_country, country, 2) != 0) {
1591 wpa_printf(MSG_DEBUG, "Continue interface setup after channel list update");
1592 iface->wait_channel_update = 1;
Dmitry Shmidt97672262014-02-03 13:02:54 -08001593 eloop_register_timeout(5, 0,
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001594 channel_list_update_timeout,
1595 iface, NULL);
1596 return 0;
1597 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001598 }
1599
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001600 return setup_interface2(iface);
1601}
1602
1603
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001604static int configured_fixed_chan_to_freq(struct hostapd_iface *iface)
1605{
1606 int freq, i, j;
1607
1608 if (!iface->conf->channel)
1609 return 0;
1610 if (iface->conf->op_class) {
1611 freq = ieee80211_chan_to_freq(NULL, iface->conf->op_class,
1612 iface->conf->channel);
1613 if (freq < 0) {
1614 wpa_printf(MSG_INFO,
1615 "Could not convert op_class %u channel %u to operating frequency",
1616 iface->conf->op_class, iface->conf->channel);
1617 return -1;
1618 }
1619 iface->freq = freq;
1620 return 0;
1621 }
1622
1623 /* Old configurations using only 2.4/5/60 GHz bands may not specify the
1624 * op_class parameter. Select a matching channel from the configured
1625 * mode using the channel parameter for these cases.
1626 */
1627 for (j = 0; j < iface->num_hw_features; j++) {
1628 struct hostapd_hw_modes *mode = &iface->hw_features[j];
1629
1630 if (iface->conf->hw_mode != HOSTAPD_MODE_IEEE80211ANY &&
1631 iface->conf->hw_mode != mode->mode)
1632 continue;
1633 for (i = 0; i < mode->num_channels; i++) {
1634 struct hostapd_channel_data *chan = &mode->channels[i];
1635
1636 if (chan->chan == iface->conf->channel &&
1637 !is_6ghz_freq(chan->freq)) {
1638 iface->freq = chan->freq;
1639 return 0;
1640 }
1641 }
1642 }
1643
1644 wpa_printf(MSG_INFO, "Could not determine operating frequency");
1645 return -1;
1646}
1647
1648
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001649static int setup_interface2(struct hostapd_iface *iface)
1650{
1651 iface->wait_channel_update = 0;
1652
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001653 if (hostapd_get_hw_features(iface)) {
1654 /* Not all drivers support this yet, so continue without hw
1655 * feature data. */
1656 } else {
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001657 int ret;
1658
1659 ret = configured_fixed_chan_to_freq(iface);
1660 if (ret < 0)
1661 goto fail;
1662
1663 if (iface->conf->op_class) {
1664 int ch_width;
1665
1666 ch_width = op_class_to_ch_width(iface->conf->op_class);
1667 hostapd_set_oper_chwidth(iface->conf, ch_width);
1668 }
1669
1670 ret = hostapd_select_hw_mode(iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001671 if (ret < 0) {
1672 wpa_printf(MSG_ERROR, "Could not select hw_mode and "
1673 "channel. (%d)", ret);
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001674 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001675 }
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001676 if (ret == 1) {
1677 wpa_printf(MSG_DEBUG, "Interface initialization will be completed in a callback (ACS)");
1678 return 0;
1679 }
Hai Shalomc3565922019-10-28 11:58:20 -07001680 ret = hostapd_check_edmg_capab(iface);
1681 if (ret < 0)
1682 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001683 ret = hostapd_check_ht_capab(iface);
1684 if (ret < 0)
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001685 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001686 if (ret == 1) {
1687 wpa_printf(MSG_DEBUG, "Interface initialization will "
1688 "be completed in a callback");
1689 return 0;
1690 }
Dmitry Shmidt051af732013-10-22 13:52:46 -07001691
1692 if (iface->conf->ieee80211h)
1693 wpa_printf(MSG_DEBUG, "DFS support is enabled");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001694 }
1695 return hostapd_setup_interface_complete(iface, 0);
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001696
1697fail:
1698 hostapd_set_state(iface, HAPD_IFACE_DISABLED);
1699 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
1700 if (iface->interfaces && iface->interfaces->terminate_on_error)
1701 eloop_terminate();
1702 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001703}
1704
1705
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001706#ifdef CONFIG_FST
1707
1708static const u8 * fst_hostapd_get_bssid_cb(void *ctx)
1709{
1710 struct hostapd_data *hapd = ctx;
1711
1712 return hapd->own_addr;
1713}
1714
1715
1716static void fst_hostapd_get_channel_info_cb(void *ctx,
1717 enum hostapd_hw_mode *hw_mode,
1718 u8 *channel)
1719{
1720 struct hostapd_data *hapd = ctx;
1721
1722 *hw_mode = ieee80211_freq_to_chan(hapd->iface->freq, channel);
1723}
1724
1725
1726static void fst_hostapd_set_ies_cb(void *ctx, const struct wpabuf *fst_ies)
1727{
1728 struct hostapd_data *hapd = ctx;
1729
1730 if (hapd->iface->fst_ies != fst_ies) {
1731 hapd->iface->fst_ies = fst_ies;
1732 if (ieee802_11_set_beacon(hapd))
1733 wpa_printf(MSG_WARNING, "FST: Cannot set beacon");
1734 }
1735}
1736
1737
1738static int fst_hostapd_send_action_cb(void *ctx, const u8 *da,
1739 struct wpabuf *buf)
1740{
1741 struct hostapd_data *hapd = ctx;
1742
1743 return hostapd_drv_send_action(hapd, hapd->iface->freq, 0, da,
1744 wpabuf_head(buf), wpabuf_len(buf));
1745}
1746
1747
1748static const struct wpabuf * fst_hostapd_get_mb_ie_cb(void *ctx, const u8 *addr)
1749{
1750 struct hostapd_data *hapd = ctx;
1751 struct sta_info *sta = ap_get_sta(hapd, addr);
1752
1753 return sta ? sta->mb_ies : NULL;
1754}
1755
1756
1757static void fst_hostapd_update_mb_ie_cb(void *ctx, const u8 *addr,
1758 const u8 *buf, size_t size)
1759{
1760 struct hostapd_data *hapd = ctx;
1761 struct sta_info *sta = ap_get_sta(hapd, addr);
1762
1763 if (sta) {
1764 struct mb_ies_info info;
1765
1766 if (!mb_ies_info_by_ies(&info, buf, size)) {
1767 wpabuf_free(sta->mb_ies);
1768 sta->mb_ies = mb_ies_by_info(&info);
1769 }
1770 }
1771}
1772
1773
1774static const u8 * fst_hostapd_get_sta(struct fst_get_peer_ctx **get_ctx,
Hai Shalome21d4e82020-04-29 16:34:06 -07001775 bool mb_only)
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001776{
1777 struct sta_info *s = (struct sta_info *) *get_ctx;
1778
1779 if (mb_only) {
1780 for (; s && !s->mb_ies; s = s->next)
1781 ;
1782 }
1783
1784 if (s) {
1785 *get_ctx = (struct fst_get_peer_ctx *) s->next;
1786
1787 return s->addr;
1788 }
1789
1790 *get_ctx = NULL;
1791 return NULL;
1792}
1793
1794
1795static const u8 * fst_hostapd_get_peer_first(void *ctx,
1796 struct fst_get_peer_ctx **get_ctx,
Hai Shalome21d4e82020-04-29 16:34:06 -07001797 bool mb_only)
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001798{
1799 struct hostapd_data *hapd = ctx;
1800
1801 *get_ctx = (struct fst_get_peer_ctx *) hapd->sta_list;
1802
1803 return fst_hostapd_get_sta(get_ctx, mb_only);
1804}
1805
1806
1807static const u8 * fst_hostapd_get_peer_next(void *ctx,
1808 struct fst_get_peer_ctx **get_ctx,
Hai Shalome21d4e82020-04-29 16:34:06 -07001809 bool mb_only)
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001810{
1811 return fst_hostapd_get_sta(get_ctx, mb_only);
1812}
1813
1814
1815void fst_hostapd_fill_iface_obj(struct hostapd_data *hapd,
1816 struct fst_wpa_obj *iface_obj)
1817{
1818 iface_obj->ctx = hapd;
1819 iface_obj->get_bssid = fst_hostapd_get_bssid_cb;
1820 iface_obj->get_channel_info = fst_hostapd_get_channel_info_cb;
1821 iface_obj->set_ies = fst_hostapd_set_ies_cb;
1822 iface_obj->send_action = fst_hostapd_send_action_cb;
1823 iface_obj->get_mb_ie = fst_hostapd_get_mb_ie_cb;
1824 iface_obj->update_mb_ie = fst_hostapd_update_mb_ie_cb;
1825 iface_obj->get_peer_first = fst_hostapd_get_peer_first;
1826 iface_obj->get_peer_next = fst_hostapd_get_peer_next;
1827}
1828
1829#endif /* CONFIG_FST */
1830
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001831#ifdef CONFIG_OWE
1832
1833static int hostapd_owe_iface_iter(struct hostapd_iface *iface, void *ctx)
1834{
1835 struct hostapd_data *hapd = ctx;
1836 size_t i;
1837
1838 for (i = 0; i < iface->num_bss; i++) {
1839 struct hostapd_data *bss = iface->bss[i];
1840
1841 if (os_strcmp(hapd->conf->owe_transition_ifname,
1842 bss->conf->iface) != 0)
1843 continue;
1844
1845 wpa_printf(MSG_DEBUG,
1846 "OWE: ifname=%s found transition mode ifname=%s BSSID "
1847 MACSTR " SSID %s",
1848 hapd->conf->iface, bss->conf->iface,
1849 MAC2STR(bss->own_addr),
1850 wpa_ssid_txt(bss->conf->ssid.ssid,
1851 bss->conf->ssid.ssid_len));
1852 if (!bss->conf->ssid.ssid_set || !bss->conf->ssid.ssid_len ||
1853 is_zero_ether_addr(bss->own_addr))
1854 continue;
1855
1856 os_memcpy(hapd->conf->owe_transition_bssid, bss->own_addr,
1857 ETH_ALEN);
1858 os_memcpy(hapd->conf->owe_transition_ssid,
1859 bss->conf->ssid.ssid, bss->conf->ssid.ssid_len);
1860 hapd->conf->owe_transition_ssid_len = bss->conf->ssid.ssid_len;
1861 wpa_printf(MSG_DEBUG,
1862 "OWE: Copied transition mode information");
1863 return 1;
1864 }
1865
1866 return 0;
1867}
1868
1869
1870int hostapd_owe_trans_get_info(struct hostapd_data *hapd)
1871{
1872 if (hapd->conf->owe_transition_ssid_len > 0 &&
1873 !is_zero_ether_addr(hapd->conf->owe_transition_bssid))
1874 return 0;
1875
1876 /* Find transition mode SSID/BSSID information from a BSS operated by
1877 * this hostapd instance. */
1878 if (!hapd->iface->interfaces ||
1879 !hapd->iface->interfaces->for_each_interface)
1880 return hostapd_owe_iface_iter(hapd->iface, hapd);
1881 else
1882 return hapd->iface->interfaces->for_each_interface(
1883 hapd->iface->interfaces, hostapd_owe_iface_iter, hapd);
1884}
1885
1886
1887static int hostapd_owe_iface_iter2(struct hostapd_iface *iface, void *ctx)
1888{
1889 size_t i;
1890
1891 for (i = 0; i < iface->num_bss; i++) {
1892 struct hostapd_data *bss = iface->bss[i];
1893 int res;
1894
1895 if (!bss->conf->owe_transition_ifname[0])
1896 continue;
1897 res = hostapd_owe_trans_get_info(bss);
1898 if (res == 0)
1899 continue;
1900 wpa_printf(MSG_DEBUG,
1901 "OWE: Matching transition mode interface enabled - update beacon data for %s",
1902 bss->conf->iface);
1903 ieee802_11_set_beacon(bss);
1904 }
1905
1906 return 0;
1907}
1908
1909#endif /* CONFIG_OWE */
1910
1911
1912static void hostapd_owe_update_trans(struct hostapd_iface *iface)
1913{
1914#ifdef CONFIG_OWE
1915 /* Check whether the enabled BSS can complete OWE transition mode
1916 * configuration for any pending interface. */
1917 if (!iface->interfaces ||
1918 !iface->interfaces->for_each_interface)
1919 hostapd_owe_iface_iter2(iface, NULL);
1920 else
1921 iface->interfaces->for_each_interface(
1922 iface->interfaces, hostapd_owe_iface_iter2, NULL);
1923#endif /* CONFIG_OWE */
1924}
1925
1926
Roshan Pius3a1667e2018-07-03 15:17:14 -07001927static void hostapd_interface_setup_failure_handler(void *eloop_ctx,
1928 void *timeout_ctx)
1929{
1930 struct hostapd_iface *iface = eloop_ctx;
1931 struct hostapd_data *hapd;
1932
1933 if (iface->num_bss < 1 || !iface->bss || !iface->bss[0])
1934 return;
1935 hapd = iface->bss[0];
1936 if (hapd->setup_complete_cb)
1937 hapd->setup_complete_cb(hapd->setup_complete_cb_ctx);
1938}
1939
1940
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001941static int hostapd_setup_interface_complete_sync(struct hostapd_iface *iface,
1942 int err)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001943{
1944 struct hostapd_data *hapd = iface->bss[0];
1945 size_t j;
1946 u8 *prev_addr;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001947 int delay_apply_cfg = 0;
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001948 int res_dfs_offload = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001949
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001950 if (err)
1951 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001952
1953 wpa_printf(MSG_DEBUG, "Completing interface initialization");
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001954 if (iface->freq) {
Dmitry Shmidt051af732013-10-22 13:52:46 -07001955#ifdef NEED_AP_MLME
1956 int res;
1957#endif /* NEED_AP_MLME */
1958
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001959 wpa_printf(MSG_DEBUG, "Mode: %s Channel: %d "
1960 "Frequency: %d MHz",
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001961 hostapd_hw_mode_txt(iface->conf->hw_mode),
1962 iface->conf->channel, iface->freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001963
Dmitry Shmidt051af732013-10-22 13:52:46 -07001964#ifdef NEED_AP_MLME
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001965 /* Handle DFS only if it is not offloaded to the driver */
1966 if (!(iface->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD)) {
1967 /* Check DFS */
1968 res = hostapd_handle_dfs(iface);
1969 if (res <= 0) {
1970 if (res < 0)
1971 goto fail;
1972 return res;
1973 }
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001974 } else {
1975 /* If DFS is offloaded to the driver */
1976 res_dfs_offload = hostapd_handle_dfs_offload(iface);
1977 if (res_dfs_offload <= 0) {
1978 if (res_dfs_offload < 0)
1979 goto fail;
1980 } else {
1981 wpa_printf(MSG_DEBUG,
1982 "Proceed with AP/channel setup");
1983 /*
1984 * If this is a DFS channel, move to completing
1985 * AP setup.
1986 */
1987 if (res_dfs_offload == 1)
1988 goto dfs_offload;
1989 /* Otherwise fall through. */
1990 }
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001991 }
Dmitry Shmidt051af732013-10-22 13:52:46 -07001992#endif /* NEED_AP_MLME */
1993
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001994#ifdef CONFIG_MESH
1995 if (iface->mconf != NULL) {
1996 wpa_printf(MSG_DEBUG,
1997 "%s: Mesh configuration will be applied while joining the mesh network",
1998 iface->bss[0]->conf->iface);
1999 delay_apply_cfg = 1;
2000 }
2001#endif /* CONFIG_MESH */
2002
2003 if (!delay_apply_cfg &&
2004 hostapd_set_freq(hapd, hapd->iconf->hw_mode, iface->freq,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002005 hapd->iconf->channel,
Hai Shalomc3565922019-10-28 11:58:20 -07002006 hapd->iconf->enable_edmg,
2007 hapd->iconf->edmg_channel,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002008 hapd->iconf->ieee80211n,
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002009 hapd->iconf->ieee80211ac,
Hai Shalom81f62d82019-07-22 12:10:00 -07002010 hapd->iconf->ieee80211ax,
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002011 hapd->iconf->secondary_channel,
Hai Shalom81f62d82019-07-22 12:10:00 -07002012 hostapd_get_oper_chwidth(hapd->iconf),
2013 hostapd_get_oper_centr_freq_seg0_idx(
2014 hapd->iconf),
2015 hostapd_get_oper_centr_freq_seg1_idx(
2016 hapd->iconf))) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002017 wpa_printf(MSG_ERROR, "Could not set channel for "
2018 "kernel driver");
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002019 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002020 }
2021 }
2022
2023 if (iface->current_mode) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002024 if (hostapd_prepare_rates(iface, iface->current_mode)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002025 wpa_printf(MSG_ERROR, "Failed to prepare rates "
2026 "table.");
2027 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
2028 HOSTAPD_LEVEL_WARNING,
2029 "Failed to prepare rates table.");
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002030 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002031 }
2032 }
2033
Hai Shalom021b0b52019-04-10 11:17:58 -07002034 if (hapd->iconf->rts_threshold >= -1 &&
2035 hostapd_set_rts(hapd, hapd->iconf->rts_threshold) &&
2036 hapd->iconf->rts_threshold >= -1) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002037 wpa_printf(MSG_ERROR, "Could not set RTS threshold for "
2038 "kernel driver");
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002039 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002040 }
2041
Hai Shalom021b0b52019-04-10 11:17:58 -07002042 if (hapd->iconf->fragm_threshold >= -1 &&
2043 hostapd_set_frag(hapd, hapd->iconf->fragm_threshold) &&
2044 hapd->iconf->fragm_threshold != -1) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002045 wpa_printf(MSG_ERROR, "Could not set fragmentation threshold "
2046 "for kernel driver");
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002047 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002048 }
2049
2050 prev_addr = hapd->own_addr;
2051
2052 for (j = 0; j < iface->num_bss; j++) {
2053 hapd = iface->bss[j];
2054 if (j)
2055 os_memcpy(hapd->own_addr, prev_addr, ETH_ALEN);
Dmitry Shmidt71757432014-06-02 13:50:35 -07002056 if (hostapd_setup_bss(hapd, j == 0)) {
Hai Shalom021b0b52019-04-10 11:17:58 -07002057 for (;;) {
Dmitry Shmidt71757432014-06-02 13:50:35 -07002058 hapd = iface->bss[j];
2059 hostapd_bss_deinit_no_free(hapd);
2060 hostapd_free_hapd_data(hapd);
Hai Shalom021b0b52019-04-10 11:17:58 -07002061 if (j == 0)
2062 break;
2063 j--;
2064 }
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002065 goto fail;
Dmitry Shmidt71757432014-06-02 13:50:35 -07002066 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002067 if (is_zero_ether_addr(hapd->conf->bssid))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002068 prev_addr = hapd->own_addr;
2069 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002070 hapd = iface->bss[0];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002071
2072 hostapd_tx_queue_params(iface);
2073
2074 ap_list_init(iface);
2075
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07002076 hostapd_set_acl(hapd);
2077
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002078 if (hostapd_driver_commit(hapd) < 0) {
2079 wpa_printf(MSG_ERROR, "%s: Failed to commit driver "
2080 "configuration", __func__);
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002081 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002082 }
2083
Jouni Malinen87fd2792011-05-16 18:35:42 +03002084 /*
2085 * WPS UPnP module can be initialized only when the "upnp_iface" is up.
2086 * If "interface" and "upnp_iface" are the same (e.g., non-bridge
2087 * mode), the interface is up only after driver_commit, so initialize
2088 * WPS after driver_commit.
2089 */
2090 for (j = 0; j < iface->num_bss; j++) {
2091 if (hostapd_init_wps_complete(iface->bss[j]))
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002092 goto fail;
Jouni Malinen87fd2792011-05-16 18:35:42 +03002093 }
2094
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08002095 if ((iface->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
2096 !res_dfs_offload) {
2097 /*
2098 * If freq is DFS, and DFS is offloaded to the driver, then wait
2099 * for CAC to complete.
2100 */
2101 wpa_printf(MSG_DEBUG, "%s: Wait for CAC to complete", __func__);
2102 return res_dfs_offload;
2103 }
2104
2105#ifdef NEED_AP_MLME
2106dfs_offload:
2107#endif /* NEED_AP_MLME */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002108
2109#ifdef CONFIG_FST
2110 if (hapd->iconf->fst_cfg.group_id[0]) {
2111 struct fst_wpa_obj iface_obj;
2112
2113 fst_hostapd_fill_iface_obj(hapd, &iface_obj);
2114 iface->fst = fst_attach(hapd->conf->iface, hapd->own_addr,
2115 &iface_obj, &hapd->iconf->fst_cfg);
2116 if (!iface->fst) {
2117 wpa_printf(MSG_ERROR, "Could not attach to FST %s",
2118 hapd->iconf->fst_cfg.group_id);
2119 goto fail;
2120 }
2121 }
2122#endif /* CONFIG_FST */
2123
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002124 hostapd_set_state(iface, HAPD_IFACE_ENABLED);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002125 hostapd_owe_update_trans(iface);
Hai Shalom81f62d82019-07-22 12:10:00 -07002126 airtime_policy_update_init(iface);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002127 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_ENABLED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002128 if (hapd->setup_complete_cb)
2129 hapd->setup_complete_cb(hapd->setup_complete_cb_ctx);
2130
2131 wpa_printf(MSG_DEBUG, "%s: Setup of interface done.",
2132 iface->bss[0]->conf->iface);
Dmitry Shmidtb96dad42013-11-05 10:07:29 -08002133 if (iface->interfaces && iface->interfaces->terminate_on_error > 0)
2134 iface->interfaces->terminate_on_error--;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002135
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002136 for (j = 0; j < iface->num_bss; j++)
Hai Shalom74f70d42019-02-11 14:42:39 -08002137 hostapd_neighbor_set_own_report(iface->bss[j]);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002138
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002139 return 0;
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002140
2141fail:
2142 wpa_printf(MSG_ERROR, "Interface initialization failed");
2143 hostapd_set_state(iface, HAPD_IFACE_DISABLED);
2144 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002145#ifdef CONFIG_FST
2146 if (iface->fst) {
2147 fst_detach(iface->fst);
2148 iface->fst = NULL;
2149 }
2150#endif /* CONFIG_FST */
Roshan Pius3a1667e2018-07-03 15:17:14 -07002151
2152 if (iface->interfaces && iface->interfaces->terminate_on_error) {
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002153 eloop_terminate();
Roshan Pius3a1667e2018-07-03 15:17:14 -07002154 } else if (hapd->setup_complete_cb) {
2155 /*
2156 * Calling hapd->setup_complete_cb directly may cause iface
2157 * deinitialization which may be accessed later by the caller.
2158 */
2159 eloop_register_timeout(0, 0,
2160 hostapd_interface_setup_failure_handler,
2161 iface, NULL);
2162 }
2163
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002164 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002165}
2166
2167
2168/**
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002169 * hostapd_setup_interface_complete - Complete interface setup
2170 *
2171 * This function is called when previous steps in the interface setup has been
2172 * completed. This can also start operations, e.g., DFS, that will require
2173 * additional processing before interface is ready to be enabled. Such
2174 * operations will call this function from eloop callbacks when finished.
2175 */
2176int hostapd_setup_interface_complete(struct hostapd_iface *iface, int err)
2177{
2178 struct hapd_interfaces *interfaces = iface->interfaces;
2179 struct hostapd_data *hapd = iface->bss[0];
2180 unsigned int i;
2181 int not_ready_in_sync_ifaces = 0;
2182
2183 if (!iface->need_to_start_in_sync)
2184 return hostapd_setup_interface_complete_sync(iface, err);
2185
2186 if (err) {
2187 wpa_printf(MSG_ERROR, "Interface initialization failed");
2188 hostapd_set_state(iface, HAPD_IFACE_DISABLED);
2189 iface->need_to_start_in_sync = 0;
2190 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
2191 if (interfaces && interfaces->terminate_on_error)
2192 eloop_terminate();
2193 return -1;
2194 }
2195
2196 if (iface->ready_to_start_in_sync) {
2197 /* Already in ready and waiting. should never happpen */
2198 return 0;
2199 }
2200
2201 for (i = 0; i < interfaces->count; i++) {
2202 if (interfaces->iface[i]->need_to_start_in_sync &&
2203 !interfaces->iface[i]->ready_to_start_in_sync)
2204 not_ready_in_sync_ifaces++;
2205 }
2206
2207 /*
2208 * Check if this is the last interface, if yes then start all the other
2209 * waiting interfaces. If not, add this interface to the waiting list.
2210 */
2211 if (not_ready_in_sync_ifaces > 1 && iface->state == HAPD_IFACE_DFS) {
2212 /*
2213 * If this interface went through CAC, do not synchronize, just
2214 * start immediately.
2215 */
2216 iface->need_to_start_in_sync = 0;
2217 wpa_printf(MSG_INFO,
2218 "%s: Finished CAC - bypass sync and start interface",
2219 iface->bss[0]->conf->iface);
2220 return hostapd_setup_interface_complete_sync(iface, err);
2221 }
2222
2223 if (not_ready_in_sync_ifaces > 1) {
2224 /* need to wait as there are other interfaces still coming up */
2225 iface->ready_to_start_in_sync = 1;
2226 wpa_printf(MSG_INFO,
2227 "%s: Interface waiting to sync with other interfaces",
2228 iface->bss[0]->conf->iface);
2229 return 0;
2230 }
2231
2232 wpa_printf(MSG_INFO,
2233 "%s: Last interface to sync - starting all interfaces",
2234 iface->bss[0]->conf->iface);
2235 iface->need_to_start_in_sync = 0;
2236 hostapd_setup_interface_complete_sync(iface, err);
2237 for (i = 0; i < interfaces->count; i++) {
2238 if (interfaces->iface[i]->need_to_start_in_sync &&
2239 interfaces->iface[i]->ready_to_start_in_sync) {
2240 hostapd_setup_interface_complete_sync(
2241 interfaces->iface[i], 0);
2242 /* Only once the interfaces are sync started */
2243 interfaces->iface[i]->need_to_start_in_sync = 0;
2244 }
2245 }
2246
2247 return 0;
2248}
2249
2250
2251/**
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002252 * hostapd_setup_interface - Setup of an interface
2253 * @iface: Pointer to interface data.
2254 * Returns: 0 on success, -1 on failure
2255 *
2256 * Initializes the driver interface, validates the configuration,
2257 * and sets driver parameters based on the configuration.
2258 * Flushes old stations, sets the channel, encryption,
2259 * beacons, and WDS links based on the configuration.
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002260 *
2261 * If interface setup requires more time, e.g., to perform HT co-ex scans, ACS,
2262 * or DFS operations, this function returns 0 before such operations have been
2263 * completed. The pending operations are registered into eloop and will be
2264 * completed from eloop callbacks. Those callbacks end up calling
2265 * hostapd_setup_interface_complete() once setup has been completed.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002266 */
2267int hostapd_setup_interface(struct hostapd_iface *iface)
2268{
2269 int ret;
2270
2271 ret = setup_interface(iface);
2272 if (ret) {
2273 wpa_printf(MSG_ERROR, "%s: Unable to setup interface.",
2274 iface->bss[0]->conf->iface);
2275 return -1;
2276 }
2277
2278 return 0;
2279}
2280
2281
2282/**
2283 * hostapd_alloc_bss_data - Allocate and initialize per-BSS data
2284 * @hapd_iface: Pointer to interface data
2285 * @conf: Pointer to per-interface configuration
2286 * @bss: Pointer to per-BSS configuration for this BSS
2287 * Returns: Pointer to allocated BSS data
2288 *
2289 * This function is used to allocate per-BSS data structure. This data will be
2290 * freed after hostapd_cleanup() is called for it during interface
2291 * deinitialization.
2292 */
2293struct hostapd_data *
2294hostapd_alloc_bss_data(struct hostapd_iface *hapd_iface,
2295 struct hostapd_config *conf,
2296 struct hostapd_bss_config *bss)
2297{
2298 struct hostapd_data *hapd;
2299
2300 hapd = os_zalloc(sizeof(*hapd));
2301 if (hapd == NULL)
2302 return NULL;
2303
2304 hapd->new_assoc_sta_cb = hostapd_new_assoc_sta;
2305 hapd->iconf = conf;
2306 hapd->conf = bss;
2307 hapd->iface = hapd_iface;
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08002308 if (conf)
2309 hapd->driver = conf->driver;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002310 hapd->ctrl_sock = -1;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08002311 dl_list_init(&hapd->ctrl_dst);
Dmitry Shmidt7d175302016-09-06 13:11:34 -07002312 dl_list_init(&hapd->nr_db);
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08002313 hapd->dhcp_sock = -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002314#ifdef CONFIG_IEEE80211R_AP
2315 dl_list_init(&hapd->l2_queue);
2316 dl_list_init(&hapd->l2_oui_queue);
2317#endif /* CONFIG_IEEE80211R_AP */
Hai Shalom021b0b52019-04-10 11:17:58 -07002318#ifdef CONFIG_SAE
2319 dl_list_init(&hapd->sae_commit_queue);
2320#endif /* CONFIG_SAE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002321
2322 return hapd;
2323}
2324
2325
Dmitry Shmidt54605472013-11-08 11:10:19 -08002326static void hostapd_bss_deinit(struct hostapd_data *hapd)
2327{
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07002328 if (!hapd)
2329 return;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002330 wpa_printf(MSG_DEBUG, "%s: deinit bss %s", __func__,
Hai Shalom021b0b52019-04-10 11:17:58 -07002331 hapd->conf ? hapd->conf->iface : "N/A");
Dmitry Shmidt71757432014-06-02 13:50:35 -07002332 hostapd_bss_deinit_no_free(hapd);
Dmitry Shmidtf73259c2015-03-17 11:00:54 -07002333 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
Hai Shalomc3565922019-10-28 11:58:20 -07002334#ifdef CONFIG_SQLITE
2335 if (hapd->rad_attr_db) {
2336 sqlite3_close(hapd->rad_attr_db);
2337 hapd->rad_attr_db = NULL;
2338 }
2339#endif /* CONFIG_SQLITE */
Dmitry Shmidt54605472013-11-08 11:10:19 -08002340 hostapd_cleanup(hapd);
2341}
2342
2343
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002344void hostapd_interface_deinit(struct hostapd_iface *iface)
2345{
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002346 int j;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002347
Dmitry Shmidt54605472013-11-08 11:10:19 -08002348 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002349 if (iface == NULL)
2350 return;
2351
Dmitry Shmidtf73259c2015-03-17 11:00:54 -07002352 hostapd_set_state(iface, HAPD_IFACE_DISABLED);
2353
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002354 eloop_cancel_timeout(channel_list_update_timeout, iface, NULL);
2355 iface->wait_channel_update = 0;
2356
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002357#ifdef CONFIG_FST
2358 if (iface->fst) {
2359 fst_detach(iface->fst);
2360 iface->fst = NULL;
2361 }
2362#endif /* CONFIG_FST */
2363
Hai Shalom021b0b52019-04-10 11:17:58 -07002364 for (j = (int) iface->num_bss - 1; j >= 0; j--) {
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07002365 if (!iface->bss)
2366 break;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002367 hostapd_bss_deinit(iface->bss[j]);
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07002368 }
Roshan Pius3a1667e2018-07-03 15:17:14 -07002369
Roshan Pius3a1667e2018-07-03 15:17:14 -07002370#ifdef NEED_AP_MLME
2371 hostapd_stop_setup_timers(iface);
2372 eloop_cancel_timeout(ap_ht2040_timeout, iface, NULL);
2373#endif /* NEED_AP_MLME */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002374}
2375
2376
2377void hostapd_interface_free(struct hostapd_iface *iface)
2378{
2379 size_t j;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002380 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
2381 for (j = 0; j < iface->num_bss; j++) {
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07002382 if (!iface->bss)
2383 break;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002384 wpa_printf(MSG_DEBUG, "%s: free hapd %p",
2385 __func__, iface->bss[j]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002386 os_free(iface->bss[j]);
Dmitry Shmidt54605472013-11-08 11:10:19 -08002387 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002388 hostapd_cleanup_iface(iface);
2389}
2390
2391
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07002392struct hostapd_iface * hostapd_alloc_iface(void)
2393{
2394 struct hostapd_iface *hapd_iface;
2395
2396 hapd_iface = os_zalloc(sizeof(*hapd_iface));
2397 if (!hapd_iface)
2398 return NULL;
2399
2400 dl_list_init(&hapd_iface->sta_seen);
2401
2402 return hapd_iface;
2403}
2404
2405
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002406/**
2407 * hostapd_init - Allocate and initialize per-interface data
2408 * @config_file: Path to the configuration file
2409 * Returns: Pointer to the allocated interface data or %NULL on failure
2410 *
2411 * This function is used to allocate main data structures for per-interface
2412 * data. The allocated data buffer will be freed by calling
2413 * hostapd_cleanup_iface().
2414 */
2415struct hostapd_iface * hostapd_init(struct hapd_interfaces *interfaces,
2416 const char *config_file)
2417{
2418 struct hostapd_iface *hapd_iface = NULL;
2419 struct hostapd_config *conf = NULL;
2420 struct hostapd_data *hapd;
2421 size_t i;
2422
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07002423 hapd_iface = hostapd_alloc_iface();
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002424 if (hapd_iface == NULL)
2425 goto fail;
2426
2427 hapd_iface->config_fname = os_strdup(config_file);
2428 if (hapd_iface->config_fname == NULL)
2429 goto fail;
2430
2431 conf = interfaces->config_read_cb(hapd_iface->config_fname);
2432 if (conf == NULL)
2433 goto fail;
2434 hapd_iface->conf = conf;
2435
2436 hapd_iface->num_bss = conf->num_bss;
2437 hapd_iface->bss = os_calloc(conf->num_bss,
2438 sizeof(struct hostapd_data *));
2439 if (hapd_iface->bss == NULL)
2440 goto fail;
2441
2442 for (i = 0; i < conf->num_bss; i++) {
2443 hapd = hapd_iface->bss[i] =
2444 hostapd_alloc_bss_data(hapd_iface, conf,
2445 conf->bss[i]);
2446 if (hapd == NULL)
2447 goto fail;
2448 hapd->msg_ctx = hapd;
2449 }
2450
2451 return hapd_iface;
2452
2453fail:
2454 wpa_printf(MSG_ERROR, "Failed to set up interface with %s",
2455 config_file);
2456 if (conf)
2457 hostapd_config_free(conf);
2458 if (hapd_iface) {
2459 os_free(hapd_iface->config_fname);
2460 os_free(hapd_iface->bss);
Dmitry Shmidt54605472013-11-08 11:10:19 -08002461 wpa_printf(MSG_DEBUG, "%s: free iface %p",
2462 __func__, hapd_iface);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002463 os_free(hapd_iface);
2464 }
2465 return NULL;
2466}
2467
2468
2469static int ifname_in_use(struct hapd_interfaces *interfaces, const char *ifname)
2470{
2471 size_t i, j;
2472
2473 for (i = 0; i < interfaces->count; i++) {
2474 struct hostapd_iface *iface = interfaces->iface[i];
2475 for (j = 0; j < iface->num_bss; j++) {
2476 struct hostapd_data *hapd = iface->bss[j];
2477 if (os_strcmp(ifname, hapd->conf->iface) == 0)
2478 return 1;
2479 }
2480 }
2481
2482 return 0;
2483}
2484
2485
2486/**
2487 * hostapd_interface_init_bss - Read configuration file and init BSS data
2488 *
2489 * This function is used to parse configuration file for a BSS. This BSS is
2490 * added to an existing interface sharing the same radio (if any) or a new
2491 * interface is created if this is the first interface on a radio. This
2492 * allocate memory for the BSS. No actual driver operations are started.
2493 *
2494 * This is similar to hostapd_interface_init(), but for a case where the
2495 * configuration is used to add a single BSS instead of all BSSes for a radio.
2496 */
2497struct hostapd_iface *
2498hostapd_interface_init_bss(struct hapd_interfaces *interfaces, const char *phy,
2499 const char *config_fname, int debug)
2500{
2501 struct hostapd_iface *new_iface = NULL, *iface = NULL;
2502 struct hostapd_data *hapd;
2503 int k;
2504 size_t i, bss_idx;
2505
2506 if (!phy || !*phy)
2507 return NULL;
2508
2509 for (i = 0; i < interfaces->count; i++) {
2510 if (os_strcmp(interfaces->iface[i]->phy, phy) == 0) {
2511 iface = interfaces->iface[i];
2512 break;
2513 }
2514 }
2515
2516 wpa_printf(MSG_INFO, "Configuration file: %s (phy %s)%s",
2517 config_fname, phy, iface ? "" : " --> new PHY");
2518 if (iface) {
2519 struct hostapd_config *conf;
2520 struct hostapd_bss_config **tmp_conf;
2521 struct hostapd_data **tmp_bss;
2522 struct hostapd_bss_config *bss;
2523 const char *ifname;
2524
2525 /* Add new BSS to existing iface */
2526 conf = interfaces->config_read_cb(config_fname);
2527 if (conf == NULL)
2528 return NULL;
2529 if (conf->num_bss > 1) {
2530 wpa_printf(MSG_ERROR, "Multiple BSSes specified in BSS-config");
2531 hostapd_config_free(conf);
2532 return NULL;
2533 }
2534
2535 ifname = conf->bss[0]->iface;
2536 if (ifname[0] != '\0' && ifname_in_use(interfaces, ifname)) {
2537 wpa_printf(MSG_ERROR,
2538 "Interface name %s already in use", ifname);
2539 hostapd_config_free(conf);
2540 return NULL;
2541 }
2542
2543 tmp_conf = os_realloc_array(
2544 iface->conf->bss, iface->conf->num_bss + 1,
2545 sizeof(struct hostapd_bss_config *));
2546 tmp_bss = os_realloc_array(iface->bss, iface->num_bss + 1,
2547 sizeof(struct hostapd_data *));
2548 if (tmp_bss)
2549 iface->bss = tmp_bss;
2550 if (tmp_conf) {
2551 iface->conf->bss = tmp_conf;
2552 iface->conf->last_bss = tmp_conf[0];
2553 }
2554 if (tmp_bss == NULL || tmp_conf == NULL) {
2555 hostapd_config_free(conf);
2556 return NULL;
2557 }
2558 bss = iface->conf->bss[iface->conf->num_bss] = conf->bss[0];
2559 iface->conf->num_bss++;
2560
2561 hapd = hostapd_alloc_bss_data(iface, iface->conf, bss);
2562 if (hapd == NULL) {
2563 iface->conf->num_bss--;
2564 hostapd_config_free(conf);
2565 return NULL;
2566 }
2567 iface->conf->last_bss = bss;
2568 iface->bss[iface->num_bss] = hapd;
2569 hapd->msg_ctx = hapd;
2570
2571 bss_idx = iface->num_bss++;
2572 conf->num_bss--;
2573 conf->bss[0] = NULL;
2574 hostapd_config_free(conf);
2575 } else {
2576 /* Add a new iface with the first BSS */
2577 new_iface = iface = hostapd_init(interfaces, config_fname);
2578 if (!iface)
2579 return NULL;
2580 os_strlcpy(iface->phy, phy, sizeof(iface->phy));
2581 iface->interfaces = interfaces;
2582 bss_idx = 0;
2583 }
2584
2585 for (k = 0; k < debug; k++) {
2586 if (iface->bss[bss_idx]->conf->logger_stdout_level > 0)
2587 iface->bss[bss_idx]->conf->logger_stdout_level--;
2588 }
2589
2590 if (iface->conf->bss[bss_idx]->iface[0] == '\0' &&
2591 !hostapd_drv_none(iface->bss[bss_idx])) {
2592 wpa_printf(MSG_ERROR, "Interface name not specified in %s",
2593 config_fname);
2594 if (new_iface)
2595 hostapd_interface_deinit_free(new_iface);
2596 return NULL;
2597 }
2598
2599 return iface;
2600}
2601
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002602
2603void hostapd_interface_deinit_free(struct hostapd_iface *iface)
2604{
2605 const struct wpa_driver_ops *driver;
2606 void *drv_priv;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002607
2608 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002609 if (iface == NULL)
2610 return;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002611 wpa_printf(MSG_DEBUG, "%s: num_bss=%u conf->num_bss=%u",
2612 __func__, (unsigned int) iface->num_bss,
2613 (unsigned int) iface->conf->num_bss);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002614 driver = iface->bss[0]->driver;
2615 drv_priv = iface->bss[0]->drv_priv;
2616 hostapd_interface_deinit(iface);
Dmitry Shmidt54605472013-11-08 11:10:19 -08002617 wpa_printf(MSG_DEBUG, "%s: driver=%p drv_priv=%p -> hapd_deinit",
2618 __func__, driver, drv_priv);
Dmitry Shmidt71757432014-06-02 13:50:35 -07002619 if (driver && driver->hapd_deinit && drv_priv) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002620 driver->hapd_deinit(drv_priv);
Dmitry Shmidt71757432014-06-02 13:50:35 -07002621 iface->bss[0]->drv_priv = NULL;
2622 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002623 hostapd_interface_free(iface);
2624}
2625
2626
Dmitry Shmidt15907092014-03-25 10:42:57 -07002627static void hostapd_deinit_driver(const struct wpa_driver_ops *driver,
2628 void *drv_priv,
2629 struct hostapd_iface *hapd_iface)
2630{
2631 size_t j;
2632
2633 wpa_printf(MSG_DEBUG, "%s: driver=%p drv_priv=%p -> hapd_deinit",
2634 __func__, driver, drv_priv);
2635 if (driver && driver->hapd_deinit && drv_priv) {
2636 driver->hapd_deinit(drv_priv);
2637 for (j = 0; j < hapd_iface->num_bss; j++) {
2638 wpa_printf(MSG_DEBUG, "%s:bss[%d]->drv_priv=%p",
2639 __func__, (int) j,
2640 hapd_iface->bss[j]->drv_priv);
Hai Shalom1dc4d202019-04-29 16:22:27 -07002641 if (hapd_iface->bss[j]->drv_priv == drv_priv) {
Dmitry Shmidt15907092014-03-25 10:42:57 -07002642 hapd_iface->bss[j]->drv_priv = NULL;
Hai Shalom1dc4d202019-04-29 16:22:27 -07002643 hapd_iface->extended_capa = NULL;
2644 hapd_iface->extended_capa_mask = NULL;
2645 hapd_iface->extended_capa_len = 0;
2646 }
Dmitry Shmidt15907092014-03-25 10:42:57 -07002647 }
2648 }
2649}
2650
2651
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002652int hostapd_enable_iface(struct hostapd_iface *hapd_iface)
2653{
Dmitry Shmidt71757432014-06-02 13:50:35 -07002654 size_t j;
2655
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002656 if (hapd_iface->bss[0]->drv_priv != NULL) {
2657 wpa_printf(MSG_ERROR, "Interface %s already enabled",
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002658 hapd_iface->conf->bss[0]->iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002659 return -1;
2660 }
2661
2662 wpa_printf(MSG_DEBUG, "Enable interface %s",
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002663 hapd_iface->conf->bss[0]->iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002664
Dmitry Shmidt71757432014-06-02 13:50:35 -07002665 for (j = 0; j < hapd_iface->num_bss; j++)
2666 hostapd_set_security_params(hapd_iface->conf->bss[j], 1);
Dmitry Shmidt344abd32014-01-14 13:17:00 -08002667 if (hostapd_config_check(hapd_iface->conf, 1) < 0) {
2668 wpa_printf(MSG_INFO, "Invalid configuration - cannot enable");
2669 return -1;
2670 }
2671
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002672 if (hapd_iface->interfaces == NULL ||
2673 hapd_iface->interfaces->driver_init == NULL ||
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002674 hapd_iface->interfaces->driver_init(hapd_iface))
2675 return -1;
2676
2677 if (hostapd_setup_interface(hapd_iface)) {
Dmitry Shmidt15907092014-03-25 10:42:57 -07002678 hostapd_deinit_driver(hapd_iface->bss[0]->driver,
2679 hapd_iface->bss[0]->drv_priv,
2680 hapd_iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002681 return -1;
2682 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002683
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002684 return 0;
2685}
2686
2687
2688int hostapd_reload_iface(struct hostapd_iface *hapd_iface)
2689{
2690 size_t j;
2691
2692 wpa_printf(MSG_DEBUG, "Reload interface %s",
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002693 hapd_iface->conf->bss[0]->iface);
2694 for (j = 0; j < hapd_iface->num_bss; j++)
Dmitry Shmidt71757432014-06-02 13:50:35 -07002695 hostapd_set_security_params(hapd_iface->conf->bss[j], 1);
Dmitry Shmidt344abd32014-01-14 13:17:00 -08002696 if (hostapd_config_check(hapd_iface->conf, 1) < 0) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002697 wpa_printf(MSG_ERROR, "Updated configuration is invalid");
2698 return -1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002699 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002700 hostapd_clear_old(hapd_iface);
2701 for (j = 0; j < hapd_iface->num_bss; j++)
2702 hostapd_reload_bss(hapd_iface->bss[j]);
2703
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002704 return 0;
2705}
2706
2707
2708int hostapd_disable_iface(struct hostapd_iface *hapd_iface)
2709{
2710 size_t j;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002711 const struct wpa_driver_ops *driver;
2712 void *drv_priv;
2713
2714 if (hapd_iface == NULL)
2715 return -1;
Dmitry Shmidt71757432014-06-02 13:50:35 -07002716
2717 if (hapd_iface->bss[0]->drv_priv == NULL) {
2718 wpa_printf(MSG_INFO, "Interface %s already disabled",
2719 hapd_iface->conf->bss[0]->iface);
2720 return -1;
2721 }
2722
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002723 wpa_msg(hapd_iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002724 driver = hapd_iface->bss[0]->driver;
2725 drv_priv = hapd_iface->bss[0]->drv_priv;
2726
Dmitry Shmidta38abf92014-03-06 13:38:44 -08002727 hapd_iface->driver_ap_teardown =
2728 !!(hapd_iface->drv_flags &
2729 WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
2730
Hai Shalom39ba6fc2019-01-22 12:40:38 -08002731#ifdef NEED_AP_MLME
2732 for (j = 0; j < hapd_iface->num_bss; j++)
2733 hostapd_cleanup_cs_params(hapd_iface->bss[j]);
2734#endif /* NEED_AP_MLME */
2735
Dmitry Shmidta38abf92014-03-06 13:38:44 -08002736 /* same as hostapd_interface_deinit without deinitializing ctrl-iface */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002737 for (j = 0; j < hapd_iface->num_bss; j++) {
2738 struct hostapd_data *hapd = hapd_iface->bss[j];
Dmitry Shmidt71757432014-06-02 13:50:35 -07002739 hostapd_bss_deinit_no_free(hapd);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002740 hostapd_free_hapd_data(hapd);
2741 }
2742
Dmitry Shmidt15907092014-03-25 10:42:57 -07002743 hostapd_deinit_driver(driver, drv_priv, hapd_iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002744
2745 /* From hostapd_cleanup_iface: These were initialized in
2746 * hostapd_setup_interface and hostapd_setup_interface_complete
2747 */
2748 hostapd_cleanup_iface_partial(hapd_iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002749
Dmitry Shmidt56052862013-10-04 10:23:25 -07002750 wpa_printf(MSG_DEBUG, "Interface %s disabled",
2751 hapd_iface->bss[0]->conf->iface);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002752 hostapd_set_state(hapd_iface, HAPD_IFACE_DISABLED);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002753 return 0;
2754}
2755
2756
2757static struct hostapd_iface *
2758hostapd_iface_alloc(struct hapd_interfaces *interfaces)
2759{
2760 struct hostapd_iface **iface, *hapd_iface;
2761
2762 iface = os_realloc_array(interfaces->iface, interfaces->count + 1,
2763 sizeof(struct hostapd_iface *));
2764 if (iface == NULL)
2765 return NULL;
2766 interfaces->iface = iface;
2767 hapd_iface = interfaces->iface[interfaces->count] =
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07002768 hostapd_alloc_iface();
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002769 if (hapd_iface == NULL) {
2770 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory for "
2771 "the interface", __func__);
2772 return NULL;
2773 }
2774 interfaces->count++;
2775 hapd_iface->interfaces = interfaces;
2776
2777 return hapd_iface;
2778}
2779
2780
2781static struct hostapd_config *
2782hostapd_config_alloc(struct hapd_interfaces *interfaces, const char *ifname,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002783 const char *ctrl_iface, const char *driver)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002784{
2785 struct hostapd_bss_config *bss;
2786 struct hostapd_config *conf;
2787
2788 /* Allocates memory for bss and conf */
2789 conf = hostapd_config_defaults();
2790 if (conf == NULL) {
2791 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory for "
2792 "configuration", __func__);
Hai Shalom74f70d42019-02-11 14:42:39 -08002793 return NULL;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002794 }
2795
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002796 if (driver) {
2797 int j;
2798
2799 for (j = 0; wpa_drivers[j]; j++) {
2800 if (os_strcmp(driver, wpa_drivers[j]->name) == 0) {
2801 conf->driver = wpa_drivers[j];
2802 goto skip;
2803 }
2804 }
2805
2806 wpa_printf(MSG_ERROR,
2807 "Invalid/unknown driver '%s' - registering the default driver",
2808 driver);
2809 }
2810
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002811 conf->driver = wpa_drivers[0];
2812 if (conf->driver == NULL) {
2813 wpa_printf(MSG_ERROR, "No driver wrappers registered!");
2814 hostapd_config_free(conf);
2815 return NULL;
2816 }
2817
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002818skip:
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002819 bss = conf->last_bss = conf->bss[0];
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002820
2821 os_strlcpy(bss->iface, ifname, sizeof(bss->iface));
2822 bss->ctrl_interface = os_strdup(ctrl_iface);
2823 if (bss->ctrl_interface == NULL) {
2824 hostapd_config_free(conf);
2825 return NULL;
2826 }
2827
2828 /* Reading configuration file skipped, will be done in SET!
2829 * From reading the configuration till the end has to be done in
2830 * SET
2831 */
2832 return conf;
2833}
2834
2835
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002836static int hostapd_data_alloc(struct hostapd_iface *hapd_iface,
2837 struct hostapd_config *conf)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002838{
2839 size_t i;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002840 struct hostapd_data *hapd;
2841
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002842 hapd_iface->bss = os_calloc(conf->num_bss,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002843 sizeof(struct hostapd_data *));
2844 if (hapd_iface->bss == NULL)
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002845 return -1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002846
2847 for (i = 0; i < conf->num_bss; i++) {
2848 hapd = hapd_iface->bss[i] =
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002849 hostapd_alloc_bss_data(hapd_iface, conf, conf->bss[i]);
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002850 if (hapd == NULL) {
2851 while (i > 0) {
2852 i--;
2853 os_free(hapd_iface->bss[i]);
2854 hapd_iface->bss[i] = NULL;
2855 }
2856 os_free(hapd_iface->bss);
2857 hapd_iface->bss = NULL;
2858 return -1;
2859 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002860 hapd->msg_ctx = hapd;
2861 }
2862
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002863 hapd_iface->conf = conf;
2864 hapd_iface->num_bss = conf->num_bss;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002865
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002866 return 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002867}
2868
2869
2870int hostapd_add_iface(struct hapd_interfaces *interfaces, char *buf)
2871{
2872 struct hostapd_config *conf = NULL;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002873 struct hostapd_iface *hapd_iface = NULL, *new_iface = NULL;
2874 struct hostapd_data *hapd;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002875 char *ptr;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002876 size_t i, j;
2877 const char *conf_file = NULL, *phy_name = NULL;
2878
2879 if (os_strncmp(buf, "bss_config=", 11) == 0) {
2880 char *pos;
2881 phy_name = buf + 11;
2882 pos = os_strchr(phy_name, ':');
2883 if (!pos)
2884 return -1;
2885 *pos++ = '\0';
2886 conf_file = pos;
2887 if (!os_strlen(conf_file))
2888 return -1;
2889
2890 hapd_iface = hostapd_interface_init_bss(interfaces, phy_name,
2891 conf_file, 0);
2892 if (!hapd_iface)
2893 return -1;
2894 for (j = 0; j < interfaces->count; j++) {
2895 if (interfaces->iface[j] == hapd_iface)
2896 break;
2897 }
2898 if (j == interfaces->count) {
2899 struct hostapd_iface **tmp;
2900 tmp = os_realloc_array(interfaces->iface,
2901 interfaces->count + 1,
2902 sizeof(struct hostapd_iface *));
2903 if (!tmp) {
2904 hostapd_interface_deinit_free(hapd_iface);
2905 return -1;
2906 }
2907 interfaces->iface = tmp;
2908 interfaces->iface[interfaces->count++] = hapd_iface;
2909 new_iface = hapd_iface;
2910 }
2911
2912 if (new_iface) {
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002913 if (interfaces->driver_init(hapd_iface))
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002914 goto fail;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002915
2916 if (hostapd_setup_interface(hapd_iface)) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002917 hostapd_deinit_driver(
2918 hapd_iface->bss[0]->driver,
2919 hapd_iface->bss[0]->drv_priv,
2920 hapd_iface);
2921 goto fail;
2922 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002923 } else {
2924 /* Assign new BSS with bss[0]'s driver info */
2925 hapd = hapd_iface->bss[hapd_iface->num_bss - 1];
2926 hapd->driver = hapd_iface->bss[0]->driver;
2927 hapd->drv_priv = hapd_iface->bss[0]->drv_priv;
2928 os_memcpy(hapd->own_addr, hapd_iface->bss[0]->own_addr,
2929 ETH_ALEN);
2930
2931 if (start_ctrl_iface_bss(hapd) < 0 ||
Dmitry Shmidt54605472013-11-08 11:10:19 -08002932 (hapd_iface->state == HAPD_IFACE_ENABLED &&
2933 hostapd_setup_bss(hapd, -1))) {
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002934 hostapd_cleanup(hapd);
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08002935 hapd_iface->bss[hapd_iface->num_bss - 1] = NULL;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002936 hapd_iface->conf->num_bss--;
2937 hapd_iface->num_bss--;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002938 wpa_printf(MSG_DEBUG, "%s: free hapd %p %s",
2939 __func__, hapd, hapd->conf->iface);
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002940 hostapd_config_free_bss(hapd->conf);
2941 hapd->conf = NULL;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002942 os_free(hapd);
2943 return -1;
2944 }
2945 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002946 hostapd_owe_update_trans(hapd_iface);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002947 return 0;
2948 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002949
2950 ptr = os_strchr(buf, ' ');
2951 if (ptr == NULL)
2952 return -1;
2953 *ptr++ = '\0';
2954
Dmitry Shmidt56052862013-10-04 10:23:25 -07002955 if (os_strncmp(ptr, "config=", 7) == 0)
2956 conf_file = ptr + 7;
2957
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002958 for (i = 0; i < interfaces->count; i++) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002959 if (!os_strcmp(interfaces->iface[i]->conf->bss[0]->iface,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002960 buf)) {
2961 wpa_printf(MSG_INFO, "Cannot add interface - it "
2962 "already exists");
2963 return -1;
2964 }
2965 }
2966
2967 hapd_iface = hostapd_iface_alloc(interfaces);
2968 if (hapd_iface == NULL) {
2969 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
2970 "for interface", __func__);
2971 goto fail;
2972 }
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002973 new_iface = hapd_iface;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002974
Dmitry Shmidt56052862013-10-04 10:23:25 -07002975 if (conf_file && interfaces->config_read_cb) {
2976 conf = interfaces->config_read_cb(conf_file);
2977 if (conf && conf->bss)
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002978 os_strlcpy(conf->bss[0]->iface, buf,
2979 sizeof(conf->bss[0]->iface));
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002980 } else {
2981 char *driver = os_strchr(ptr, ' ');
2982
2983 if (driver)
2984 *driver++ = '\0';
2985 conf = hostapd_config_alloc(interfaces, buf, ptr, driver);
2986 }
2987
Dmitry Shmidt56052862013-10-04 10:23:25 -07002988 if (conf == NULL || conf->bss == NULL) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002989 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
2990 "for configuration", __func__);
2991 goto fail;
2992 }
2993
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002994 if (hostapd_data_alloc(hapd_iface, conf) < 0) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002995 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
2996 "for hostapd", __func__);
2997 goto fail;
2998 }
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002999 conf = NULL;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003000
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003001 if (start_ctrl_iface(hapd_iface) < 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003002 goto fail;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003003
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003004 wpa_printf(MSG_INFO, "Add interface '%s'",
3005 hapd_iface->conf->bss[0]->iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003006
3007 return 0;
3008
3009fail:
3010 if (conf)
3011 hostapd_config_free(conf);
3012 if (hapd_iface) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003013 if (hapd_iface->bss) {
Dmitry Shmidt54605472013-11-08 11:10:19 -08003014 for (i = 0; i < hapd_iface->num_bss; i++) {
3015 hapd = hapd_iface->bss[i];
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08003016 if (!hapd)
3017 continue;
3018 if (hapd_iface->interfaces &&
Dmitry Shmidt54605472013-11-08 11:10:19 -08003019 hapd_iface->interfaces->ctrl_iface_deinit)
3020 hapd_iface->interfaces->
3021 ctrl_iface_deinit(hapd);
3022 wpa_printf(MSG_DEBUG, "%s: free hapd %p (%s)",
3023 __func__, hapd_iface->bss[i],
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08003024 hapd->conf->iface);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003025 hostapd_cleanup(hapd);
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08003026 os_free(hapd);
3027 hapd_iface->bss[i] = NULL;
Dmitry Shmidt54605472013-11-08 11:10:19 -08003028 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003029 os_free(hapd_iface->bss);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003030 hapd_iface->bss = NULL;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003031 }
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003032 if (new_iface) {
3033 interfaces->count--;
3034 interfaces->iface[interfaces->count] = NULL;
3035 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003036 hostapd_cleanup_iface(hapd_iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003037 }
3038 return -1;
3039}
3040
3041
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003042static int hostapd_remove_bss(struct hostapd_iface *iface, unsigned int idx)
3043{
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003044 size_t i;
3045
Dmitry Shmidt54605472013-11-08 11:10:19 -08003046 wpa_printf(MSG_INFO, "Remove BSS '%s'", iface->conf->bss[idx]->iface);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003047
Dmitry Shmidt54605472013-11-08 11:10:19 -08003048 /* Remove hostapd_data only if it has already been initialized */
3049 if (idx < iface->num_bss) {
3050 struct hostapd_data *hapd = iface->bss[idx];
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003051
Dmitry Shmidt54605472013-11-08 11:10:19 -08003052 hostapd_bss_deinit(hapd);
3053 wpa_printf(MSG_DEBUG, "%s: free hapd %p (%s)",
3054 __func__, hapd, hapd->conf->iface);
3055 hostapd_config_free_bss(hapd->conf);
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003056 hapd->conf = NULL;
Dmitry Shmidt54605472013-11-08 11:10:19 -08003057 os_free(hapd);
3058
3059 iface->num_bss--;
3060
3061 for (i = idx; i < iface->num_bss; i++)
3062 iface->bss[i] = iface->bss[i + 1];
3063 } else {
3064 hostapd_config_free_bss(iface->conf->bss[idx]);
3065 iface->conf->bss[idx] = NULL;
3066 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003067
3068 iface->conf->num_bss--;
Dmitry Shmidt54605472013-11-08 11:10:19 -08003069 for (i = idx; i < iface->conf->num_bss; i++)
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003070 iface->conf->bss[i] = iface->conf->bss[i + 1];
3071
3072 return 0;
3073}
3074
3075
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003076int hostapd_remove_iface(struct hapd_interfaces *interfaces, char *buf)
3077{
3078 struct hostapd_iface *hapd_iface;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003079 size_t i, j, k = 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003080
3081 for (i = 0; i < interfaces->count; i++) {
3082 hapd_iface = interfaces->iface[i];
3083 if (hapd_iface == NULL)
3084 return -1;
Dmitry Shmidt54605472013-11-08 11:10:19 -08003085 if (!os_strcmp(hapd_iface->conf->bss[0]->iface, buf)) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003086 wpa_printf(MSG_INFO, "Remove interface '%s'", buf);
Dmitry Shmidta38abf92014-03-06 13:38:44 -08003087 hapd_iface->driver_ap_teardown =
3088 !!(hapd_iface->drv_flags &
3089 WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
3090
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003091 hostapd_interface_deinit_free(hapd_iface);
3092 k = i;
3093 while (k < (interfaces->count - 1)) {
3094 interfaces->iface[k] =
3095 interfaces->iface[k + 1];
3096 k++;
3097 }
3098 interfaces->count--;
3099 return 0;
3100 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003101
3102 for (j = 0; j < hapd_iface->conf->num_bss; j++) {
Dmitry Shmidta38abf92014-03-06 13:38:44 -08003103 if (!os_strcmp(hapd_iface->conf->bss[j]->iface, buf)) {
3104 hapd_iface->driver_ap_teardown =
3105 !(hapd_iface->drv_flags &
3106 WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003107 return hostapd_remove_bss(hapd_iface, j);
Dmitry Shmidta38abf92014-03-06 13:38:44 -08003108 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003109 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003110 }
3111 return -1;
3112}
3113
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003114
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003115/**
3116 * hostapd_new_assoc_sta - Notify that a new station associated with the AP
3117 * @hapd: Pointer to BSS data
3118 * @sta: Pointer to the associated STA data
3119 * @reassoc: 1 to indicate this was a re-association; 0 = first association
3120 *
3121 * This function will be called whenever a station associates with the AP. It
3122 * can be called from ieee802_11.c for drivers that export MLME to hostapd and
3123 * from drv_callbacks.c based on driver events for drivers that take care of
3124 * management frames (IEEE 802.11 authentication and association) internally.
3125 */
3126void hostapd_new_assoc_sta(struct hostapd_data *hapd, struct sta_info *sta,
3127 int reassoc)
3128{
3129 if (hapd->tkip_countermeasures) {
3130 hostapd_drv_sta_deauth(hapd, sta->addr,
3131 WLAN_REASON_MICHAEL_MIC_FAILURE);
3132 return;
3133 }
3134
3135 hostapd_prune_associations(hapd, sta->addr);
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -08003136 ap_sta_clear_disconnect_timeouts(hapd, sta);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003137
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003138#ifdef CONFIG_P2P
3139 if (sta->p2p_ie == NULL && !sta->no_p2p_set) {
3140 sta->no_p2p_set = 1;
3141 hapd->num_sta_no_p2p++;
3142 if (hapd->num_sta_no_p2p == 1)
3143 hostapd_p2p_non_p2p_sta_connected(hapd);
3144 }
3145#endif /* CONFIG_P2P */
3146
Hai Shalom81f62d82019-07-22 12:10:00 -07003147 airtime_policy_new_sta(hapd, sta);
3148
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003149 /* Start accounting here, if IEEE 802.1X and WPA are not used.
3150 * IEEE 802.1X/WPA code will start accounting after the station has
3151 * been authorized. */
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -08003152 if (!hapd->conf->ieee802_1x && !hapd->conf->wpa && !hapd->conf->osen) {
3153 ap_sta_set_authorized(hapd, sta, 1);
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08003154 os_get_reltime(&sta->connected_time);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003155 accounting_sta_start(hapd, sta);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003156 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003157
3158 /* Start IEEE 802.1X authentication process for new stations */
3159 ieee802_1x_new_station(hapd, sta);
3160 if (reassoc) {
3161 if (sta->auth_alg != WLAN_AUTH_FT &&
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003162 sta->auth_alg != WLAN_AUTH_FILS_SK &&
3163 sta->auth_alg != WLAN_AUTH_FILS_SK_PFS &&
3164 sta->auth_alg != WLAN_AUTH_FILS_PK &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003165 !(sta->flags & (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS)))
3166 wpa_auth_sm_event(sta->wpa_sm, WPA_REAUTH);
3167 } else
3168 wpa_auth_sta_associated(hapd->wpa_auth, sta->wpa_sm);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003169
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08003170 if (hapd->iface->drv_flags & WPA_DRIVER_FLAGS_WIRED) {
3171 if (eloop_cancel_timeout(ap_handle_timer, hapd, sta) > 0) {
3172 wpa_printf(MSG_DEBUG,
3173 "%s: %s: canceled wired ap_handle_timer timeout for "
3174 MACSTR,
3175 hapd->conf->iface, __func__,
3176 MAC2STR(sta->addr));
3177 }
3178 } else if (!(hapd->iface->drv_flags &
3179 WPA_DRIVER_FLAGS_INACTIVITY_TIMER)) {
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -08003180 wpa_printf(MSG_DEBUG,
3181 "%s: %s: reschedule ap_handle_timer timeout for "
3182 MACSTR " (%d seconds - ap_max_inactivity)",
3183 hapd->conf->iface, __func__, MAC2STR(sta->addr),
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08003184 hapd->conf->ap_max_inactivity);
3185 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
3186 eloop_register_timeout(hapd->conf->ap_max_inactivity, 0,
3187 ap_handle_timer, hapd, sta);
3188 }
Hai Shalom81f62d82019-07-22 12:10:00 -07003189
3190#ifdef CONFIG_MACSEC
3191 if (hapd->conf->wpa_key_mgmt == WPA_KEY_MGMT_NONE &&
3192 hapd->conf->mka_psk_set)
3193 ieee802_1x_create_preshared_mka_hapd(hapd, sta);
3194 else
3195 ieee802_1x_alloc_kay_sm_hapd(hapd, sta);
3196#endif /* CONFIG_MACSEC */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003197}
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003198
3199
3200const char * hostapd_state_text(enum hostapd_iface_state s)
3201{
3202 switch (s) {
3203 case HAPD_IFACE_UNINITIALIZED:
3204 return "UNINITIALIZED";
3205 case HAPD_IFACE_DISABLED:
3206 return "DISABLED";
3207 case HAPD_IFACE_COUNTRY_UPDATE:
3208 return "COUNTRY_UPDATE";
3209 case HAPD_IFACE_ACS:
3210 return "ACS";
3211 case HAPD_IFACE_HT_SCAN:
3212 return "HT_SCAN";
3213 case HAPD_IFACE_DFS:
3214 return "DFS";
3215 case HAPD_IFACE_ENABLED:
3216 return "ENABLED";
3217 }
3218
3219 return "UNKNOWN";
3220}
3221
3222
3223void hostapd_set_state(struct hostapd_iface *iface, enum hostapd_iface_state s)
3224{
3225 wpa_printf(MSG_INFO, "%s: interface state %s->%s",
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07003226 iface->conf ? iface->conf->bss[0]->iface : "N/A",
3227 hostapd_state_text(iface->state), hostapd_state_text(s));
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003228 iface->state = s;
3229}
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003230
3231
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003232int hostapd_csa_in_progress(struct hostapd_iface *iface)
3233{
3234 unsigned int i;
3235
3236 for (i = 0; i < iface->num_bss; i++)
3237 if (iface->bss[i]->csa_in_progress)
3238 return 1;
3239 return 0;
3240}
3241
3242
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003243#ifdef NEED_AP_MLME
3244
3245static void free_beacon_data(struct beacon_data *beacon)
3246{
3247 os_free(beacon->head);
3248 beacon->head = NULL;
3249 os_free(beacon->tail);
3250 beacon->tail = NULL;
3251 os_free(beacon->probe_resp);
3252 beacon->probe_resp = NULL;
3253 os_free(beacon->beacon_ies);
3254 beacon->beacon_ies = NULL;
3255 os_free(beacon->proberesp_ies);
3256 beacon->proberesp_ies = NULL;
3257 os_free(beacon->assocresp_ies);
3258 beacon->assocresp_ies = NULL;
3259}
3260
3261
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003262static int hostapd_build_beacon_data(struct hostapd_data *hapd,
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003263 struct beacon_data *beacon)
3264{
3265 struct wpabuf *beacon_extra, *proberesp_extra, *assocresp_extra;
3266 struct wpa_driver_ap_params params;
3267 int ret;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003268
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08003269 os_memset(beacon, 0, sizeof(*beacon));
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003270 ret = ieee802_11_build_ap_params(hapd, &params);
3271 if (ret < 0)
3272 return ret;
3273
3274 ret = hostapd_build_ap_extra_ies(hapd, &beacon_extra,
3275 &proberesp_extra,
3276 &assocresp_extra);
3277 if (ret)
3278 goto free_ap_params;
3279
3280 ret = -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003281 beacon->head = os_memdup(params.head, params.head_len);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003282 if (!beacon->head)
3283 goto free_ap_extra_ies;
3284
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003285 beacon->head_len = params.head_len;
3286
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003287 beacon->tail = os_memdup(params.tail, params.tail_len);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003288 if (!beacon->tail)
3289 goto free_beacon;
3290
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003291 beacon->tail_len = params.tail_len;
3292
3293 if (params.proberesp != NULL) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003294 beacon->probe_resp = os_memdup(params.proberesp,
3295 params.proberesp_len);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003296 if (!beacon->probe_resp)
3297 goto free_beacon;
3298
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003299 beacon->probe_resp_len = params.proberesp_len;
3300 }
3301
3302 /* copy the extra ies */
3303 if (beacon_extra) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003304 beacon->beacon_ies = os_memdup(beacon_extra->buf,
3305 wpabuf_len(beacon_extra));
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003306 if (!beacon->beacon_ies)
3307 goto free_beacon;
3308
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003309 beacon->beacon_ies_len = wpabuf_len(beacon_extra);
3310 }
3311
3312 if (proberesp_extra) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003313 beacon->proberesp_ies = os_memdup(proberesp_extra->buf,
3314 wpabuf_len(proberesp_extra));
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003315 if (!beacon->proberesp_ies)
3316 goto free_beacon;
3317
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003318 beacon->proberesp_ies_len = wpabuf_len(proberesp_extra);
3319 }
3320
3321 if (assocresp_extra) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003322 beacon->assocresp_ies = os_memdup(assocresp_extra->buf,
3323 wpabuf_len(assocresp_extra));
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003324 if (!beacon->assocresp_ies)
3325 goto free_beacon;
3326
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003327 beacon->assocresp_ies_len = wpabuf_len(assocresp_extra);
3328 }
3329
3330 ret = 0;
3331free_beacon:
3332 /* if the function fails, the caller should not free beacon data */
3333 if (ret)
3334 free_beacon_data(beacon);
3335
3336free_ap_extra_ies:
3337 hostapd_free_ap_extra_ies(hapd, beacon_extra, proberesp_extra,
3338 assocresp_extra);
3339free_ap_params:
3340 ieee802_11_free_ap_params(&params);
3341 return ret;
3342}
3343
3344
3345/*
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003346 * TODO: This flow currently supports only changing channel and width within
3347 * the same hw_mode. Any other changes to MAC parameters or provided settings
3348 * are not supported.
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003349 */
3350static int hostapd_change_config_freq(struct hostapd_data *hapd,
3351 struct hostapd_config *conf,
3352 struct hostapd_freq_params *params,
3353 struct hostapd_freq_params *old_params)
3354{
3355 int channel;
Hai Shalom81f62d82019-07-22 12:10:00 -07003356 u8 seg0, seg1;
3357 struct hostapd_hw_modes *mode;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003358
3359 if (!params->channel) {
3360 /* check if the new channel is supported by hw */
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003361 params->channel = hostapd_hw_get_channel(hapd, params->freq);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003362 }
3363
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003364 channel = params->channel;
3365 if (!channel)
3366 return -1;
3367
Hai Shalom81f62d82019-07-22 12:10:00 -07003368 mode = hapd->iface->current_mode;
3369
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003370 /* if a pointer to old_params is provided we save previous state */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003371 if (old_params &&
3372 hostapd_set_freq_params(old_params, conf->hw_mode,
3373 hostapd_hw_get_freq(hapd, conf->channel),
Hai Shalomc3565922019-10-28 11:58:20 -07003374 conf->channel, conf->enable_edmg,
3375 conf->edmg_channel, conf->ieee80211n,
Hai Shalom81f62d82019-07-22 12:10:00 -07003376 conf->ieee80211ac, conf->ieee80211ax,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003377 conf->secondary_channel,
Hai Shalom81f62d82019-07-22 12:10:00 -07003378 hostapd_get_oper_chwidth(conf),
3379 hostapd_get_oper_centr_freq_seg0_idx(conf),
3380 hostapd_get_oper_centr_freq_seg1_idx(conf),
3381 conf->vht_capab,
3382 mode ? &mode->he_capab[IEEE80211_MODE_AP] :
3383 NULL))
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003384 return -1;
3385
3386 switch (params->bandwidth) {
3387 case 0:
3388 case 20:
3389 case 40:
Hai Shalom81f62d82019-07-22 12:10:00 -07003390 hostapd_set_oper_chwidth(conf, CHANWIDTH_USE_HT);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003391 break;
3392 case 80:
3393 if (params->center_freq2)
Hai Shalom81f62d82019-07-22 12:10:00 -07003394 hostapd_set_oper_chwidth(conf, CHANWIDTH_80P80MHZ);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003395 else
Hai Shalom81f62d82019-07-22 12:10:00 -07003396 hostapd_set_oper_chwidth(conf, CHANWIDTH_80MHZ);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003397 break;
3398 case 160:
Hai Shalom81f62d82019-07-22 12:10:00 -07003399 hostapd_set_oper_chwidth(conf, CHANWIDTH_160MHZ);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003400 break;
3401 default:
3402 return -1;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003403 }
3404
3405 conf->channel = channel;
3406 conf->ieee80211n = params->ht_enabled;
3407 conf->secondary_channel = params->sec_channel_offset;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003408 ieee80211_freq_to_chan(params->center_freq1,
Hai Shalom81f62d82019-07-22 12:10:00 -07003409 &seg0);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003410 ieee80211_freq_to_chan(params->center_freq2,
Hai Shalom81f62d82019-07-22 12:10:00 -07003411 &seg1);
3412 hostapd_set_oper_centr_freq_seg0_idx(conf, seg0);
3413 hostapd_set_oper_centr_freq_seg1_idx(conf, seg1);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003414
3415 /* TODO: maybe call here hostapd_config_check here? */
3416
3417 return 0;
3418}
3419
3420
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003421static int hostapd_fill_csa_settings(struct hostapd_data *hapd,
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003422 struct csa_settings *settings)
3423{
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003424 struct hostapd_iface *iface = hapd->iface;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003425 struct hostapd_freq_params old_freq;
3426 int ret;
Hai Shalom81f62d82019-07-22 12:10:00 -07003427 u8 chan, bandwidth;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003428
3429 os_memset(&old_freq, 0, sizeof(old_freq));
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003430 if (!iface || !iface->freq || hapd->csa_in_progress)
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003431 return -1;
3432
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003433 switch (settings->freq_params.bandwidth) {
3434 case 80:
3435 if (settings->freq_params.center_freq2)
Hai Shalom81f62d82019-07-22 12:10:00 -07003436 bandwidth = CHANWIDTH_80P80MHZ;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003437 else
Hai Shalom81f62d82019-07-22 12:10:00 -07003438 bandwidth = CHANWIDTH_80MHZ;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003439 break;
3440 case 160:
Hai Shalom81f62d82019-07-22 12:10:00 -07003441 bandwidth = CHANWIDTH_160MHZ;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003442 break;
3443 default:
Hai Shalom81f62d82019-07-22 12:10:00 -07003444 bandwidth = CHANWIDTH_USE_HT;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003445 break;
3446 }
3447
3448 if (ieee80211_freq_to_channel_ext(
3449 settings->freq_params.freq,
3450 settings->freq_params.sec_channel_offset,
Hai Shalom81f62d82019-07-22 12:10:00 -07003451 bandwidth,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003452 &hapd->iface->cs_oper_class,
3453 &chan) == NUM_HOSTAPD_MODES) {
3454 wpa_printf(MSG_DEBUG,
Hai Shalom81f62d82019-07-22 12:10:00 -07003455 "invalid frequency for channel switch (freq=%d, sec_channel_offset=%d, vht_enabled=%d, he_enabled=%d)",
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003456 settings->freq_params.freq,
3457 settings->freq_params.sec_channel_offset,
Hai Shalom81f62d82019-07-22 12:10:00 -07003458 settings->freq_params.vht_enabled,
3459 settings->freq_params.he_enabled);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003460 return -1;
3461 }
3462
3463 settings->freq_params.channel = chan;
3464
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003465 ret = hostapd_change_config_freq(iface->bss[0], iface->conf,
3466 &settings->freq_params,
3467 &old_freq);
3468 if (ret)
3469 return ret;
3470
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003471 ret = hostapd_build_beacon_data(hapd, &settings->beacon_after);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003472
3473 /* change back the configuration */
3474 hostapd_change_config_freq(iface->bss[0], iface->conf,
3475 &old_freq, NULL);
3476
3477 if (ret)
3478 return ret;
3479
3480 /* set channel switch parameters for csa ie */
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003481 hapd->cs_freq_params = settings->freq_params;
3482 hapd->cs_count = settings->cs_count;
3483 hapd->cs_block_tx = settings->block_tx;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003484
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003485 ret = hostapd_build_beacon_data(hapd, &settings->beacon_csa);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003486 if (ret) {
3487 free_beacon_data(&settings->beacon_after);
3488 return ret;
3489 }
3490
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003491 settings->counter_offset_beacon[0] = hapd->cs_c_off_beacon;
3492 settings->counter_offset_presp[0] = hapd->cs_c_off_proberesp;
3493 settings->counter_offset_beacon[1] = hapd->cs_c_off_ecsa_beacon;
3494 settings->counter_offset_presp[1] = hapd->cs_c_off_ecsa_proberesp;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003495
3496 return 0;
3497}
3498
3499
3500void hostapd_cleanup_cs_params(struct hostapd_data *hapd)
3501{
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003502 os_memset(&hapd->cs_freq_params, 0, sizeof(hapd->cs_freq_params));
3503 hapd->cs_count = 0;
3504 hapd->cs_block_tx = 0;
3505 hapd->cs_c_off_beacon = 0;
3506 hapd->cs_c_off_proberesp = 0;
3507 hapd->csa_in_progress = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003508 hapd->cs_c_off_ecsa_beacon = 0;
3509 hapd->cs_c_off_ecsa_proberesp = 0;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003510}
3511
3512
Roshan Pius3a1667e2018-07-03 15:17:14 -07003513void hostapd_chan_switch_vht_config(struct hostapd_data *hapd, int vht_enabled)
3514{
3515 if (vht_enabled)
3516 hapd->iconf->ch_switch_vht_config |= CH_SWITCH_VHT_ENABLED;
3517 else
3518 hapd->iconf->ch_switch_vht_config |= CH_SWITCH_VHT_DISABLED;
3519
3520 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
3521 HOSTAPD_LEVEL_INFO, "CHAN_SWITCH VHT CONFIG 0x%x",
3522 hapd->iconf->ch_switch_vht_config);
3523}
3524
3525
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003526int hostapd_switch_channel(struct hostapd_data *hapd,
3527 struct csa_settings *settings)
3528{
3529 int ret;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003530
3531 if (!(hapd->iface->drv_flags & WPA_DRIVER_FLAGS_AP_CSA)) {
3532 wpa_printf(MSG_INFO, "CSA is not supported");
3533 return -1;
3534 }
3535
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003536 ret = hostapd_fill_csa_settings(hapd, settings);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003537 if (ret)
3538 return ret;
3539
3540 ret = hostapd_drv_switch_channel(hapd, settings);
3541 free_beacon_data(&settings->beacon_csa);
3542 free_beacon_data(&settings->beacon_after);
3543
3544 if (ret) {
3545 /* if we failed, clean cs parameters */
3546 hostapd_cleanup_cs_params(hapd);
3547 return ret;
3548 }
3549
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003550 hapd->csa_in_progress = 1;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003551 return 0;
3552}
3553
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003554
3555void
3556hostapd_switch_channel_fallback(struct hostapd_iface *iface,
3557 const struct hostapd_freq_params *freq_params)
3558{
Hai Shalom81f62d82019-07-22 12:10:00 -07003559 int seg0_idx = 0, seg1_idx = 0, bw = CHANWIDTH_USE_HT;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003560
3561 wpa_printf(MSG_DEBUG, "Restarting all CSA-related BSSes");
3562
3563 if (freq_params->center_freq1)
Hai Shalom81f62d82019-07-22 12:10:00 -07003564 seg0_idx = 36 + (freq_params->center_freq1 - 5180) / 5;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003565 if (freq_params->center_freq2)
Hai Shalom81f62d82019-07-22 12:10:00 -07003566 seg1_idx = 36 + (freq_params->center_freq2 - 5180) / 5;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003567
3568 switch (freq_params->bandwidth) {
3569 case 0:
3570 case 20:
3571 case 40:
Hai Shalom81f62d82019-07-22 12:10:00 -07003572 bw = CHANWIDTH_USE_HT;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003573 break;
3574 case 80:
3575 if (freq_params->center_freq2)
Hai Shalom81f62d82019-07-22 12:10:00 -07003576 bw = CHANWIDTH_80P80MHZ;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003577 else
Hai Shalom81f62d82019-07-22 12:10:00 -07003578 bw = CHANWIDTH_80MHZ;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003579 break;
3580 case 160:
Hai Shalom81f62d82019-07-22 12:10:00 -07003581 bw = CHANWIDTH_160MHZ;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003582 break;
3583 default:
3584 wpa_printf(MSG_WARNING, "Unknown CSA bandwidth: %d",
3585 freq_params->bandwidth);
3586 break;
3587 }
3588
3589 iface->freq = freq_params->freq;
3590 iface->conf->channel = freq_params->channel;
3591 iface->conf->secondary_channel = freq_params->sec_channel_offset;
Hai Shalom81f62d82019-07-22 12:10:00 -07003592 hostapd_set_oper_centr_freq_seg0_idx(iface->conf, seg0_idx);
3593 hostapd_set_oper_centr_freq_seg1_idx(iface->conf, seg1_idx);
3594 hostapd_set_oper_chwidth(iface->conf, bw);
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003595 iface->conf->ieee80211n = freq_params->ht_enabled;
3596 iface->conf->ieee80211ac = freq_params->vht_enabled;
Hai Shalom81f62d82019-07-22 12:10:00 -07003597 iface->conf->ieee80211ax = freq_params->he_enabled;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003598
3599 /*
3600 * cs_params must not be cleared earlier because the freq_params
3601 * argument may actually point to one of these.
Hai Shalom39ba6fc2019-01-22 12:40:38 -08003602 * These params will be cleared during interface disable below.
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003603 */
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003604 hostapd_disable_iface(iface);
3605 hostapd_enable_iface(iface);
3606}
3607
Dmitry Shmidte4663042016-04-04 10:07:49 -07003608#endif /* NEED_AP_MLME */
3609
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003610
3611struct hostapd_data * hostapd_get_iface(struct hapd_interfaces *interfaces,
3612 const char *ifname)
3613{
3614 size_t i, j;
3615
3616 for (i = 0; i < interfaces->count; i++) {
3617 struct hostapd_iface *iface = interfaces->iface[i];
3618
3619 for (j = 0; j < iface->num_bss; j++) {
3620 struct hostapd_data *hapd = iface->bss[j];
3621
3622 if (os_strcmp(ifname, hapd->conf->iface) == 0)
3623 return hapd;
3624 }
3625 }
3626
3627 return NULL;
3628}
3629
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003630
3631void hostapd_periodic_iface(struct hostapd_iface *iface)
3632{
3633 size_t i;
3634
3635 ap_list_timer(iface);
3636
3637 for (i = 0; i < iface->num_bss; i++) {
3638 struct hostapd_data *hapd = iface->bss[i];
3639
3640 if (!hapd->started)
3641 continue;
3642
3643#ifndef CONFIG_NO_RADIUS
3644 hostapd_acl_expire(hapd);
3645#endif /* CONFIG_NO_RADIUS */
3646 }
3647}