blob: b37f49f9a8ecac0cb0f2cda2d0fe46a8b1a116cd [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++) {
Hai Shalom4fbc08f2020-05-18 12:37:00 -070079 if (!interfaces->iface[i])
80 continue;
Dmitry Shmidt04949592012-07-19 12:16:46 -070081 ret = cb(interfaces->iface[i], ctx);
82 if (ret)
83 return ret;
84 }
85
86 return 0;
87}
88
89
Hai Shalomce48b4a2018-09-05 11:41:35 -070090void hostapd_reconfig_encryption(struct hostapd_data *hapd)
91{
92 if (hapd->wpa_auth)
93 return;
94
95 hostapd_set_privacy(hapd, 0);
Hai Shalomfdcde762020-04-02 11:19:20 -070096#ifdef CONFIG_WEP
Hai Shalomce48b4a2018-09-05 11:41:35 -070097 hostapd_setup_encryption(hapd->conf->iface, hapd);
Hai Shalomfdcde762020-04-02 11:19:20 -070098#endif /* CONFIG_WEP */
Hai Shalomce48b4a2018-09-05 11:41:35 -070099}
100
101
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700102static void hostapd_reload_bss(struct hostapd_data *hapd)
103{
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800104 struct hostapd_ssid *ssid;
105
Dmitry Shmidt29333592017-01-09 12:27:11 -0800106 if (!hapd->started)
107 return;
108
Roshan Pius3a1667e2018-07-03 15:17:14 -0700109 if (hapd->conf->wmm_enabled < 0)
110 hapd->conf->wmm_enabled = hapd->iconf->ieee80211n;
111
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700112#ifndef CONFIG_NO_RADIUS
113 radius_client_reconfig(hapd->radius, hapd->conf->radius);
114#endif /* CONFIG_NO_RADIUS */
115
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800116 ssid = &hapd->conf->ssid;
117 if (!ssid->wpa_psk_set && ssid->wpa_psk && !ssid->wpa_psk->next &&
118 ssid->wpa_passphrase_set && ssid->wpa_passphrase) {
119 /*
120 * Force PSK to be derived again since SSID or passphrase may
121 * have changed.
122 */
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800123 hostapd_config_clear_wpa_psk(&hapd->conf->ssid.wpa_psk);
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800124 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700125 if (hostapd_setup_wpa_psk(hapd->conf)) {
126 wpa_printf(MSG_ERROR, "Failed to re-configure WPA PSK "
127 "after reloading configuration");
128 }
129
130 if (hapd->conf->ieee802_1x || hapd->conf->wpa)
131 hostapd_set_drv_ieee8021x(hapd, hapd->conf->iface, 1);
132 else
133 hostapd_set_drv_ieee8021x(hapd, hapd->conf->iface, 0);
134
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800135 if ((hapd->conf->wpa || hapd->conf->osen) && hapd->wpa_auth == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700136 hostapd_setup_wpa(hapd);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800137 if (hapd->wpa_auth)
138 wpa_init_keys(hapd->wpa_auth);
139 } else if (hapd->conf->wpa) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700140 const u8 *wpa_ie;
141 size_t wpa_ie_len;
142 hostapd_reconfig_wpa(hapd);
143 wpa_ie = wpa_auth_get_wpa_ie(hapd->wpa_auth, &wpa_ie_len);
144 if (hostapd_set_generic_elem(hapd, wpa_ie, wpa_ie_len))
145 wpa_printf(MSG_ERROR, "Failed to configure WPA IE for "
146 "the kernel driver.");
147 } else if (hapd->wpa_auth) {
148 wpa_deinit(hapd->wpa_auth);
149 hapd->wpa_auth = NULL;
150 hostapd_set_privacy(hapd, 0);
Hai Shalomfdcde762020-04-02 11:19:20 -0700151#ifdef CONFIG_WEP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700152 hostapd_setup_encryption(hapd->conf->iface, hapd);
Hai Shalomfdcde762020-04-02 11:19:20 -0700153#endif /* CONFIG_WEP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700154 hostapd_set_generic_elem(hapd, (u8 *) "", 0);
155 }
156
157 ieee802_11_set_beacon(hapd);
158 hostapd_update_wps(hapd);
159
160 if (hapd->conf->ssid.ssid_set &&
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700161 hostapd_set_ssid(hapd, hapd->conf->ssid.ssid,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700162 hapd->conf->ssid.ssid_len)) {
163 wpa_printf(MSG_ERROR, "Could not set SSID for kernel driver");
164 /* try to continue */
165 }
166 wpa_printf(MSG_DEBUG, "Reconfigured interface %s", hapd->conf->iface);
167}
168
169
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700170static void hostapd_clear_old(struct hostapd_iface *iface)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700171{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700172 size_t j;
173
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700174 /*
175 * Deauthenticate all stations since the new configuration may not
176 * allow them to use the BSS anymore.
177 */
178 for (j = 0; j < iface->num_bss; j++) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700179 hostapd_flush_old_stations(iface->bss[j],
180 WLAN_REASON_PREV_AUTH_NOT_VALID);
Hai Shalomfdcde762020-04-02 11:19:20 -0700181#ifdef CONFIG_WEP
Dmitry Shmidtc55524a2011-07-07 11:18:38 -0700182 hostapd_broadcast_wep_clear(iface->bss[j]);
Hai Shalomfdcde762020-04-02 11:19:20 -0700183#endif /* CONFIG_WEP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700184
185#ifndef CONFIG_NO_RADIUS
186 /* TODO: update dynamic data based on changed configuration
187 * items (e.g., open/close sockets, etc.) */
188 radius_client_flush(iface->bss[j]->radius, 0);
189#endif /* CONFIG_NO_RADIUS */
190 }
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700191}
192
193
Hai Shalom74f70d42019-02-11 14:42:39 -0800194static int hostapd_iface_conf_changed(struct hostapd_config *newconf,
195 struct hostapd_config *oldconf)
196{
197 size_t i;
198
199 if (newconf->num_bss != oldconf->num_bss)
200 return 1;
201
202 for (i = 0; i < newconf->num_bss; i++) {
203 if (os_strcmp(newconf->bss[i]->iface,
204 oldconf->bss[i]->iface) != 0)
205 return 1;
206 }
207
208 return 0;
209}
210
211
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700212int hostapd_reload_config(struct hostapd_iface *iface)
213{
Hai Shalom74f70d42019-02-11 14:42:39 -0800214 struct hapd_interfaces *interfaces = iface->interfaces;
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700215 struct hostapd_data *hapd = iface->bss[0];
216 struct hostapd_config *newconf, *oldconf;
217 size_t j;
218
219 if (iface->config_fname == NULL) {
220 /* Only in-memory config in use - assume it has been updated */
221 hostapd_clear_old(iface);
222 for (j = 0; j < iface->num_bss; j++)
223 hostapd_reload_bss(iface->bss[j]);
224 return 0;
225 }
226
227 if (iface->interfaces == NULL ||
228 iface->interfaces->config_read_cb == NULL)
229 return -1;
230 newconf = iface->interfaces->config_read_cb(iface->config_fname);
231 if (newconf == NULL)
232 return -1;
233
234 hostapd_clear_old(iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700235
236 oldconf = hapd->iconf;
Hai Shalom74f70d42019-02-11 14:42:39 -0800237 if (hostapd_iface_conf_changed(newconf, oldconf)) {
238 char *fname;
239 int res;
240
241 wpa_printf(MSG_DEBUG,
242 "Configuration changes include interface/BSS modification - force full disable+enable sequence");
243 fname = os_strdup(iface->config_fname);
244 if (!fname) {
245 hostapd_config_free(newconf);
246 return -1;
247 }
248 hostapd_remove_iface(interfaces, hapd->conf->iface);
249 iface = hostapd_init(interfaces, fname);
250 os_free(fname);
251 hostapd_config_free(newconf);
252 if (!iface) {
253 wpa_printf(MSG_ERROR,
254 "Failed to initialize interface on config reload");
255 return -1;
256 }
257 iface->interfaces = interfaces;
258 interfaces->iface[interfaces->count] = iface;
259 interfaces->count++;
260 res = hostapd_enable_iface(iface);
261 if (res < 0)
262 wpa_printf(MSG_ERROR,
263 "Failed to enable interface on config reload");
264 return res;
265 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700266 iface->conf = newconf;
267
268 for (j = 0; j < iface->num_bss; j++) {
269 hapd = iface->bss[j];
270 hapd->iconf = newconf;
Dmitry Shmidtd11f0192014-03-24 12:09:47 -0700271 hapd->iconf->channel = oldconf->channel;
Dmitry Shmidtdda10c22015-03-24 16:05:01 -0700272 hapd->iconf->acs = oldconf->acs;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -0700273 hapd->iconf->secondary_channel = oldconf->secondary_channel;
Dmitry Shmidtd11f0192014-03-24 12:09:47 -0700274 hapd->iconf->ieee80211n = oldconf->ieee80211n;
275 hapd->iconf->ieee80211ac = oldconf->ieee80211ac;
276 hapd->iconf->ht_capab = oldconf->ht_capab;
277 hapd->iconf->vht_capab = oldconf->vht_capab;
Hai Shalom81f62d82019-07-22 12:10:00 -0700278 hostapd_set_oper_chwidth(hapd->iconf,
279 hostapd_get_oper_chwidth(oldconf));
280 hostapd_set_oper_centr_freq_seg0_idx(
281 hapd->iconf,
282 hostapd_get_oper_centr_freq_seg0_idx(oldconf));
283 hostapd_set_oper_centr_freq_seg1_idx(
284 hapd->iconf,
285 hostapd_get_oper_centr_freq_seg1_idx(oldconf));
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800286 hapd->conf = newconf->bss[j];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700287 hostapd_reload_bss(hapd);
288 }
289
290 hostapd_config_free(oldconf);
291
292
293 return 0;
294}
295
296
Hai Shalomfdcde762020-04-02 11:19:20 -0700297#ifdef CONFIG_WEP
298
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700299static void hostapd_broadcast_key_clear_iface(struct hostapd_data *hapd,
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700300 const char *ifname)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700301{
302 int i;
303
Dmitry Shmidt29333592017-01-09 12:27:11 -0800304 if (!ifname || !hapd->drv_priv)
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700305 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700306 for (i = 0; i < NUM_WEP_KEYS; i++) {
Hai Shalomfdcde762020-04-02 11:19:20 -0700307 if (hostapd_drv_set_key(ifname, hapd, WPA_ALG_NONE, NULL, i, 0,
308 0, NULL, 0, NULL, 0, KEY_FLAG_GROUP)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700309 wpa_printf(MSG_DEBUG, "Failed to clear default "
310 "encryption keys (ifname=%s keyidx=%d)",
311 ifname, i);
312 }
313 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700314 if (hapd->conf->ieee80211w) {
315 for (i = NUM_WEP_KEYS; i < NUM_WEP_KEYS + 2; i++) {
316 if (hostapd_drv_set_key(ifname, hapd, WPA_ALG_NONE,
Hai Shalomfdcde762020-04-02 11:19:20 -0700317 NULL, i, 0, 0, NULL,
318 0, NULL, 0, KEY_FLAG_GROUP)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700319 wpa_printf(MSG_DEBUG, "Failed to clear "
320 "default mgmt encryption keys "
321 "(ifname=%s keyidx=%d)", ifname, i);
322 }
323 }
324 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700325}
326
327
328static int hostapd_broadcast_wep_clear(struct hostapd_data *hapd)
329{
330 hostapd_broadcast_key_clear_iface(hapd, hapd->conf->iface);
331 return 0;
332}
333
334
335static int hostapd_broadcast_wep_set(struct hostapd_data *hapd)
336{
337 int errors = 0, idx;
338 struct hostapd_ssid *ssid = &hapd->conf->ssid;
339
340 idx = ssid->wep.idx;
Hai Shalomfdcde762020-04-02 11:19:20 -0700341 if (ssid->wep.default_len && ssid->wep.key[idx] &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700342 hostapd_drv_set_key(hapd->conf->iface,
Hai Shalomfdcde762020-04-02 11:19:20 -0700343 hapd, WPA_ALG_WEP, broadcast_ether_addr, idx, 0,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700344 1, NULL, 0, ssid->wep.key[idx],
Hai Shalomfdcde762020-04-02 11:19:20 -0700345 ssid->wep.len[idx],
346 KEY_FLAG_GROUP_RX_TX_DEFAULT)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700347 wpa_printf(MSG_WARNING, "Could not set WEP encryption.");
348 errors++;
349 }
350
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700351 return errors;
352}
353
Hai Shalomfdcde762020-04-02 11:19:20 -0700354#endif /* CONFIG_WEP */
355
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700356
Dmitry Shmidt04949592012-07-19 12:16:46 -0700357static void hostapd_free_hapd_data(struct hostapd_data *hapd)
358{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800359 os_free(hapd->probereq_cb);
360 hapd->probereq_cb = NULL;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800361 hapd->num_probereq_cb = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800362
363#ifdef CONFIG_P2P
364 wpabuf_free(hapd->p2p_beacon_ie);
365 hapd->p2p_beacon_ie = NULL;
366 wpabuf_free(hapd->p2p_probe_resp_ie);
367 hapd->p2p_probe_resp_ie = NULL;
368#endif /* CONFIG_P2P */
369
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -0800370 if (!hapd->started) {
371 wpa_printf(MSG_ERROR, "%s: Interface %s wasn't started",
Hai Shalom021b0b52019-04-10 11:17:58 -0700372 __func__, hapd->conf ? hapd->conf->iface : "N/A");
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -0800373 return;
374 }
375 hapd->started = 0;
Hai Shalom5f92bc92019-04-18 11:54:11 -0700376 hapd->beacon_set_done = 0;
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -0800377
Dmitry Shmidt54605472013-11-08 11:10:19 -0800378 wpa_printf(MSG_DEBUG, "%s(%s)", __func__, hapd->conf->iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700379 accounting_deinit(hapd);
380 hostapd_deinit_wpa(hapd);
381 vlan_deinit(hapd);
382 hostapd_acl_deinit(hapd);
383#ifndef CONFIG_NO_RADIUS
384 radius_client_deinit(hapd->radius);
385 hapd->radius = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700386 radius_das_deinit(hapd->radius_das);
387 hapd->radius_das = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700388#endif /* CONFIG_NO_RADIUS */
389
390 hostapd_deinit_wps(hapd);
Hai Shalom81f62d82019-07-22 12:10:00 -0700391 ieee802_1x_dealloc_kay_sm_hapd(hapd);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700392#ifdef CONFIG_DPP
393 hostapd_dpp_deinit(hapd);
394 gas_query_ap_deinit(hapd->gas);
395#endif /* CONFIG_DPP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700396
397 authsrv_deinit(hapd);
398
Dmitry Shmidt71757432014-06-02 13:50:35 -0700399 if (hapd->interface_added) {
400 hapd->interface_added = 0;
401 if (hostapd_if_remove(hapd, WPA_IF_AP_BSS, hapd->conf->iface)) {
402 wpa_printf(MSG_WARNING,
403 "Failed to remove BSS interface %s",
404 hapd->conf->iface);
405 hapd->interface_added = 1;
406 } else {
407 /*
408 * Since this was a dynamically added interface, the
409 * driver wrapper may have removed its internal instance
410 * and hapd->drv_priv is not valid anymore.
411 */
412 hapd->drv_priv = NULL;
413 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700414 }
415
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800416 wpabuf_free(hapd->time_adv);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700417
418#ifdef CONFIG_INTERWORKING
419 gas_serv_deinit(hapd);
420#endif /* CONFIG_INTERWORKING */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800421
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800422 bss_load_update_deinit(hapd);
423 ndisc_snoop_deinit(hapd);
424 dhcp_snoop_deinit(hapd);
425 x_snoop_deinit(hapd);
426
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800427#ifdef CONFIG_SQLITE
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700428 bin_clear_free(hapd->tmp_eap_user.identity,
429 hapd->tmp_eap_user.identity_len);
430 bin_clear_free(hapd->tmp_eap_user.password,
431 hapd->tmp_eap_user.password_len);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800432#endif /* CONFIG_SQLITE */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800433
434#ifdef CONFIG_MESH
435 wpabuf_free(hapd->mesh_pending_auth);
436 hapd->mesh_pending_auth = NULL;
437#endif /* CONFIG_MESH */
Dmitry Shmidt849734c2016-05-27 09:59:01 -0700438
439 hostapd_clean_rrm(hapd);
Dmitry Shmidtebd93af2017-02-21 13:40:44 -0800440 fils_hlp_deinit(hapd);
Hai Shalom021b0b52019-04-10 11:17:58 -0700441
Hai Shalom899fcc72020-10-19 14:38:18 -0700442#ifdef CONFIG_OCV
443 eloop_cancel_timeout(hostapd_ocv_check_csa_sa_query, hapd, NULL);
444#endif /* CONFIG_OCV */
445
Hai Shalom021b0b52019-04-10 11:17:58 -0700446#ifdef CONFIG_SAE
447 {
448 struct hostapd_sae_commit_queue *q;
449
450 while ((q = dl_list_first(&hapd->sae_commit_queue,
451 struct hostapd_sae_commit_queue,
452 list))) {
453 dl_list_del(&q->list);
454 os_free(q);
455 }
456 }
457 eloop_cancel_timeout(auth_sae_process_commit, hapd, NULL);
458#endif /* CONFIG_SAE */
Dmitry Shmidt04949592012-07-19 12:16:46 -0700459}
460
461
462/**
463 * hostapd_cleanup - Per-BSS cleanup (deinitialization)
464 * @hapd: Pointer to BSS data
465 *
466 * This function is used to free all per-BSS data structures and resources.
Dmitry Shmidt54605472013-11-08 11:10:19 -0800467 * Most of the modules that are initialized in hostapd_setup_bss() are
468 * deinitialized here.
Dmitry Shmidt04949592012-07-19 12:16:46 -0700469 */
470static void hostapd_cleanup(struct hostapd_data *hapd)
471{
Dmitry Shmidt54605472013-11-08 11:10:19 -0800472 wpa_printf(MSG_DEBUG, "%s(hapd=%p (%s))", __func__, hapd,
Hai Shalom021b0b52019-04-10 11:17:58 -0700473 hapd->conf ? hapd->conf->iface : "N/A");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700474 if (hapd->iface->interfaces &&
Dmitry Shmidt29333592017-01-09 12:27:11 -0800475 hapd->iface->interfaces->ctrl_iface_deinit) {
476 wpa_msg(hapd->msg_ctx, MSG_INFO, WPA_EVENT_TERMINATING);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700477 hapd->iface->interfaces->ctrl_iface_deinit(hapd);
Dmitry Shmidt29333592017-01-09 12:27:11 -0800478 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700479 hostapd_free_hapd_data(hapd);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700480}
481
482
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800483static void sta_track_deinit(struct hostapd_iface *iface)
484{
485 struct hostapd_sta_info *info;
486
487 if (!iface->num_sta_seen)
488 return;
489
490 while ((info = dl_list_first(&iface->sta_seen, struct hostapd_sta_info,
491 list))) {
492 dl_list_del(&info->list);
493 iface->num_sta_seen--;
Dmitry Shmidtaca489e2016-09-28 15:44:14 -0700494 sta_track_del(info);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800495 }
496}
497
498
Dmitry Shmidt04949592012-07-19 12:16:46 -0700499static void hostapd_cleanup_iface_partial(struct hostapd_iface *iface)
500{
Dmitry Shmidt54605472013-11-08 11:10:19 -0800501 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800502#ifdef NEED_AP_MLME
503 hostapd_stop_setup_timers(iface);
504#endif /* NEED_AP_MLME */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700505 if (iface->current_mode)
506 acs_cleanup(iface);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700507 hostapd_free_hw_features(iface->hw_features, iface->num_hw_features);
508 iface->hw_features = NULL;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700509 iface->current_mode = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700510 os_free(iface->current_rates);
511 iface->current_rates = NULL;
512 os_free(iface->basic_rates);
513 iface->basic_rates = NULL;
514 ap_list_deinit(iface);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800515 sta_track_deinit(iface);
Hai Shalom81f62d82019-07-22 12:10:00 -0700516 airtime_policy_update_deinit(iface);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700517}
518
519
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700520/**
521 * hostapd_cleanup_iface - Complete per-interface cleanup
522 * @iface: Pointer to interface data
523 *
524 * This function is called after per-BSS data structures are deinitialized
525 * with hostapd_cleanup().
526 */
527static void hostapd_cleanup_iface(struct hostapd_iface *iface)
528{
Dmitry Shmidt54605472013-11-08 11:10:19 -0800529 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800530 eloop_cancel_timeout(channel_list_update_timeout, iface, NULL);
Roshan Pius3a1667e2018-07-03 15:17:14 -0700531 eloop_cancel_timeout(hostapd_interface_setup_failure_handler, iface,
532 NULL);
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800533
Dmitry Shmidt04949592012-07-19 12:16:46 -0700534 hostapd_cleanup_iface_partial(iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700535 hostapd_config_free(iface->conf);
536 iface->conf = NULL;
537
538 os_free(iface->config_fname);
539 os_free(iface->bss);
Dmitry Shmidt54605472013-11-08 11:10:19 -0800540 wpa_printf(MSG_DEBUG, "%s: free iface=%p", __func__, iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700541 os_free(iface);
542}
543
544
Hai Shalomfdcde762020-04-02 11:19:20 -0700545#ifdef CONFIG_WEP
546
Dmitry Shmidt04949592012-07-19 12:16:46 -0700547static void hostapd_clear_wep(struct hostapd_data *hapd)
548{
Hai Shalom021b0b52019-04-10 11:17:58 -0700549 if (hapd->drv_priv && !hapd->iface->driver_ap_teardown && hapd->conf) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700550 hostapd_set_privacy(hapd, 0);
551 hostapd_broadcast_wep_clear(hapd);
552 }
553}
554
555
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700556static int hostapd_setup_encryption(char *iface, struct hostapd_data *hapd)
557{
558 int i;
559
560 hostapd_broadcast_wep_set(hapd);
561
562 if (hapd->conf->ssid.wep.default_len) {
563 hostapd_set_privacy(hapd, 1);
564 return 0;
565 }
566
Jouni Malinen75ecf522011-06-27 15:19:46 -0700567 /*
568 * When IEEE 802.1X is not enabled, the driver may need to know how to
569 * set authentication algorithms for static WEP.
570 */
571 hostapd_drv_set_authmode(hapd, hapd->conf->auth_algs);
572
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700573 for (i = 0; i < 4; i++) {
574 if (hapd->conf->ssid.wep.key[i] &&
Hai Shalomfdcde762020-04-02 11:19:20 -0700575 hostapd_drv_set_key(iface, hapd, WPA_ALG_WEP, NULL, i, 0,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700576 i == hapd->conf->ssid.wep.idx, NULL, 0,
577 hapd->conf->ssid.wep.key[i],
Hai Shalomfdcde762020-04-02 11:19:20 -0700578 hapd->conf->ssid.wep.len[i],
579 i == hapd->conf->ssid.wep.idx ?
580 KEY_FLAG_GROUP_RX_TX_DEFAULT :
581 KEY_FLAG_GROUP_RX_TX)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700582 wpa_printf(MSG_WARNING, "Could not set WEP "
583 "encryption.");
584 return -1;
585 }
586 if (hapd->conf->ssid.wep.key[i] &&
587 i == hapd->conf->ssid.wep.idx)
588 hostapd_set_privacy(hapd, 1);
589 }
590
591 return 0;
592}
593
Hai Shalomfdcde762020-04-02 11:19:20 -0700594#endif /* CONFIG_WEP */
595
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700596
Dmitry Shmidt04949592012-07-19 12:16:46 -0700597static int hostapd_flush_old_stations(struct hostapd_data *hapd, u16 reason)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700598{
599 int ret = 0;
600 u8 addr[ETH_ALEN];
601
602 if (hostapd_drv_none(hapd) || hapd->drv_priv == NULL)
603 return 0;
604
Dmitry Shmidta38abf92014-03-06 13:38:44 -0800605 if (!hapd->iface->driver_ap_teardown) {
606 wpa_dbg(hapd->msg_ctx, MSG_DEBUG,
607 "Flushing old station entries");
608
609 if (hostapd_flush(hapd)) {
610 wpa_msg(hapd->msg_ctx, MSG_WARNING,
611 "Could not connect to kernel driver");
612 ret = -1;
613 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700614 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700615 if (hapd->conf && hapd->conf->broadcast_deauth) {
616 wpa_dbg(hapd->msg_ctx, MSG_DEBUG,
617 "Deauthenticate all stations");
618 os_memset(addr, 0xff, ETH_ALEN);
619 hostapd_drv_sta_deauth(hapd, addr, reason);
620 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700621 hostapd_free_stas(hapd);
622
623 return ret;
624}
625
626
Dmitry Shmidt71757432014-06-02 13:50:35 -0700627static void hostapd_bss_deinit_no_free(struct hostapd_data *hapd)
628{
629 hostapd_free_stas(hapd);
630 hostapd_flush_old_stations(hapd, WLAN_REASON_DEAUTH_LEAVING);
Hai Shalomfdcde762020-04-02 11:19:20 -0700631#ifdef CONFIG_WEP
Dmitry Shmidt71757432014-06-02 13:50:35 -0700632 hostapd_clear_wep(hapd);
Hai Shalomfdcde762020-04-02 11:19:20 -0700633#endif /* CONFIG_WEP */
Dmitry Shmidt71757432014-06-02 13:50:35 -0700634}
635
636
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700637/**
638 * hostapd_validate_bssid_configuration - Validate BSSID configuration
639 * @iface: Pointer to interface data
640 * Returns: 0 on success, -1 on failure
641 *
642 * This function is used to validate that the configured BSSIDs are valid.
643 */
644static int hostapd_validate_bssid_configuration(struct hostapd_iface *iface)
645{
646 u8 mask[ETH_ALEN] = { 0 };
647 struct hostapd_data *hapd = iface->bss[0];
648 unsigned int i = iface->conf->num_bss, bits = 0, j;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700649 int auto_addr = 0;
650
651 if (hostapd_drv_none(hapd))
652 return 0;
653
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -0800654 if (iface->conf->use_driver_iface_addr)
655 return 0;
656
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700657 /* Generate BSSID mask that is large enough to cover the BSSIDs. */
658
659 /* Determine the bits necessary to cover the number of BSSIDs. */
660 for (i--; i; i >>= 1)
661 bits++;
662
663 /* Determine the bits necessary to any configured BSSIDs,
664 if they are higher than the number of BSSIDs. */
665 for (j = 0; j < iface->conf->num_bss; j++) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800666 if (is_zero_ether_addr(iface->conf->bss[j]->bssid)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700667 if (j)
668 auto_addr++;
669 continue;
670 }
671
672 for (i = 0; i < ETH_ALEN; i++) {
673 mask[i] |=
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800674 iface->conf->bss[j]->bssid[i] ^
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700675 hapd->own_addr[i];
676 }
677 }
678
679 if (!auto_addr)
680 goto skip_mask_ext;
681
682 for (i = 0; i < ETH_ALEN && mask[i] == 0; i++)
683 ;
684 j = 0;
685 if (i < ETH_ALEN) {
686 j = (5 - i) * 8;
687
688 while (mask[i] != 0) {
689 mask[i] >>= 1;
690 j++;
691 }
692 }
693
694 if (bits < j)
695 bits = j;
696
697 if (bits > 40) {
698 wpa_printf(MSG_ERROR, "Too many bits in the BSSID mask (%u)",
699 bits);
700 return -1;
701 }
702
703 os_memset(mask, 0xff, ETH_ALEN);
704 j = bits / 8;
705 for (i = 5; i > 5 - j; i--)
706 mask[i] = 0;
707 j = bits % 8;
Hai Shalom021b0b52019-04-10 11:17:58 -0700708 while (j) {
709 j--;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700710 mask[i] <<= 1;
Hai Shalom021b0b52019-04-10 11:17:58 -0700711 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700712
713skip_mask_ext:
714 wpa_printf(MSG_DEBUG, "BSS count %lu, BSSID mask " MACSTR " (%d bits)",
715 (unsigned long) iface->conf->num_bss, MAC2STR(mask), bits);
716
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700717 if (!auto_addr)
718 return 0;
719
720 for (i = 0; i < ETH_ALEN; i++) {
721 if ((hapd->own_addr[i] & mask[i]) != hapd->own_addr[i]) {
722 wpa_printf(MSG_ERROR, "Invalid BSSID mask " MACSTR
723 " for start address " MACSTR ".",
724 MAC2STR(mask), MAC2STR(hapd->own_addr));
725 wpa_printf(MSG_ERROR, "Start address must be the "
726 "first address in the block (i.e., addr "
727 "AND mask == addr).");
728 return -1;
729 }
730 }
731
732 return 0;
733}
734
735
736static int mac_in_conf(struct hostapd_config *conf, const void *a)
737{
738 size_t i;
739
740 for (i = 0; i < conf->num_bss; i++) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800741 if (hostapd_mac_comp(conf->bss[i]->bssid, a) == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700742 return 1;
743 }
744 }
745
746 return 0;
747}
748
749
Dmitry Shmidt04949592012-07-19 12:16:46 -0700750#ifndef CONFIG_NO_RADIUS
751
752static int hostapd_das_nas_mismatch(struct hostapd_data *hapd,
753 struct radius_das_attrs *attr)
754{
Dmitry Shmidt13ca8d82014-02-20 10:18:40 -0800755 if (attr->nas_identifier &&
756 (!hapd->conf->nas_identifier ||
757 os_strlen(hapd->conf->nas_identifier) !=
758 attr->nas_identifier_len ||
759 os_memcmp(hapd->conf->nas_identifier, attr->nas_identifier,
760 attr->nas_identifier_len) != 0)) {
761 wpa_printf(MSG_DEBUG, "RADIUS DAS: NAS-Identifier mismatch");
762 return 1;
763 }
764
765 if (attr->nas_ip_addr &&
766 (hapd->conf->own_ip_addr.af != AF_INET ||
767 os_memcmp(&hapd->conf->own_ip_addr.u.v4, attr->nas_ip_addr, 4) !=
768 0)) {
769 wpa_printf(MSG_DEBUG, "RADIUS DAS: NAS-IP-Address mismatch");
770 return 1;
771 }
772
773#ifdef CONFIG_IPV6
774 if (attr->nas_ipv6_addr &&
775 (hapd->conf->own_ip_addr.af != AF_INET6 ||
776 os_memcmp(&hapd->conf->own_ip_addr.u.v6, attr->nas_ipv6_addr, 16)
777 != 0)) {
778 wpa_printf(MSG_DEBUG, "RADIUS DAS: NAS-IPv6-Address mismatch");
779 return 1;
780 }
781#endif /* CONFIG_IPV6 */
782
Dmitry Shmidt04949592012-07-19 12:16:46 -0700783 return 0;
784}
785
786
787static struct sta_info * hostapd_das_find_sta(struct hostapd_data *hapd,
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800788 struct radius_das_attrs *attr,
789 int *multi)
Dmitry Shmidt04949592012-07-19 12:16:46 -0700790{
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800791 struct sta_info *selected, *sta;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700792 char buf[128];
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800793 int num_attr = 0;
794 int count;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700795
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800796 *multi = 0;
797
798 for (sta = hapd->sta_list; sta; sta = sta->next)
799 sta->radius_das_match = 1;
800
801 if (attr->sta_addr) {
802 num_attr++;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700803 sta = ap_get_sta(hapd, attr->sta_addr);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800804 if (!sta) {
805 wpa_printf(MSG_DEBUG,
806 "RADIUS DAS: No Calling-Station-Id match");
807 return NULL;
808 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700809
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800810 selected = sta;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700811 for (sta = hapd->sta_list; sta; sta = sta->next) {
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800812 if (sta != selected)
813 sta->radius_das_match = 0;
814 }
815 wpa_printf(MSG_DEBUG, "RADIUS DAS: Calling-Station-Id match");
816 }
817
818 if (attr->acct_session_id) {
819 num_attr++;
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800820 if (attr->acct_session_id_len != 16) {
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800821 wpa_printf(MSG_DEBUG,
822 "RADIUS DAS: Acct-Session-Id cannot match");
823 return NULL;
824 }
825 count = 0;
826
827 for (sta = hapd->sta_list; sta; sta = sta->next) {
828 if (!sta->radius_das_match)
829 continue;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800830 os_snprintf(buf, sizeof(buf), "%016llX",
831 (unsigned long long) sta->acct_session_id);
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800832 if (os_memcmp(attr->acct_session_id, buf, 16) != 0)
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800833 sta->radius_das_match = 0;
834 else
835 count++;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700836 }
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800837
838 if (count == 0) {
839 wpa_printf(MSG_DEBUG,
840 "RADIUS DAS: No matches remaining after Acct-Session-Id check");
841 return NULL;
842 }
843 wpa_printf(MSG_DEBUG, "RADIUS DAS: Acct-Session-Id match");
Dmitry Shmidt04949592012-07-19 12:16:46 -0700844 }
845
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800846 if (attr->acct_multi_session_id) {
847 num_attr++;
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800848 if (attr->acct_multi_session_id_len != 16) {
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800849 wpa_printf(MSG_DEBUG,
850 "RADIUS DAS: Acct-Multi-Session-Id cannot match");
851 return NULL;
852 }
853 count = 0;
854
855 for (sta = hapd->sta_list; sta; sta = sta->next) {
856 if (!sta->radius_das_match)
857 continue;
858 if (!sta->eapol_sm ||
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800859 !sta->eapol_sm->acct_multi_session_id) {
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800860 sta->radius_das_match = 0;
861 continue;
862 }
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800863 os_snprintf(buf, sizeof(buf), "%016llX",
864 (unsigned long long)
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800865 sta->eapol_sm->acct_multi_session_id);
866 if (os_memcmp(attr->acct_multi_session_id, buf, 16) !=
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800867 0)
868 sta->radius_das_match = 0;
869 else
870 count++;
871 }
872
873 if (count == 0) {
874 wpa_printf(MSG_DEBUG,
875 "RADIUS DAS: No matches remaining after Acct-Multi-Session-Id check");
876 return NULL;
877 }
878 wpa_printf(MSG_DEBUG,
879 "RADIUS DAS: Acct-Multi-Session-Id match");
880 }
881
882 if (attr->cui) {
883 num_attr++;
884 count = 0;
885
Dmitry Shmidt04949592012-07-19 12:16:46 -0700886 for (sta = hapd->sta_list; sta; sta = sta->next) {
887 struct wpabuf *cui;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800888
889 if (!sta->radius_das_match)
890 continue;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700891 cui = ieee802_1x_get_radius_cui(sta->eapol_sm);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800892 if (!cui || wpabuf_len(cui) != attr->cui_len ||
Dmitry Shmidt04949592012-07-19 12:16:46 -0700893 os_memcmp(wpabuf_head(cui), attr->cui,
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800894 attr->cui_len) != 0)
895 sta->radius_das_match = 0;
896 else
897 count++;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700898 }
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800899
900 if (count == 0) {
901 wpa_printf(MSG_DEBUG,
902 "RADIUS DAS: No matches remaining after Chargeable-User-Identity check");
903 return NULL;
904 }
905 wpa_printf(MSG_DEBUG,
906 "RADIUS DAS: Chargeable-User-Identity match");
Dmitry Shmidt04949592012-07-19 12:16:46 -0700907 }
908
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800909 if (attr->user_name) {
910 num_attr++;
911 count = 0;
912
Dmitry Shmidt04949592012-07-19 12:16:46 -0700913 for (sta = hapd->sta_list; sta; sta = sta->next) {
914 u8 *identity;
915 size_t identity_len;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800916
917 if (!sta->radius_das_match)
918 continue;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700919 identity = ieee802_1x_get_identity(sta->eapol_sm,
920 &identity_len);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800921 if (!identity ||
922 identity_len != attr->user_name_len ||
Dmitry Shmidt04949592012-07-19 12:16:46 -0700923 os_memcmp(identity, attr->user_name, identity_len)
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800924 != 0)
925 sta->radius_das_match = 0;
926 else
927 count++;
928 }
929
930 if (count == 0) {
931 wpa_printf(MSG_DEBUG,
932 "RADIUS DAS: No matches remaining after User-Name check");
933 return NULL;
934 }
935 wpa_printf(MSG_DEBUG,
936 "RADIUS DAS: User-Name match");
937 }
938
939 if (num_attr == 0) {
940 /*
941 * In theory, we could match all current associations, but it
942 * seems safer to just reject requests that do not include any
943 * session identification attributes.
944 */
945 wpa_printf(MSG_DEBUG,
946 "RADIUS DAS: No session identification attributes included");
947 return NULL;
948 }
949
950 selected = NULL;
951 for (sta = hapd->sta_list; sta; sta = sta->next) {
952 if (sta->radius_das_match) {
953 if (selected) {
954 *multi = 1;
955 return NULL;
956 }
957 selected = sta;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700958 }
959 }
960
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800961 return selected;
962}
963
964
965static int hostapd_das_disconnect_pmksa(struct hostapd_data *hapd,
966 struct radius_das_attrs *attr)
967{
968 if (!hapd->wpa_auth)
969 return -1;
970 return wpa_auth_radius_das_disconnect_pmksa(hapd->wpa_auth, attr);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700971}
972
973
974static enum radius_das_res
975hostapd_das_disconnect(void *ctx, struct radius_das_attrs *attr)
976{
977 struct hostapd_data *hapd = ctx;
978 struct sta_info *sta;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800979 int multi;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700980
981 if (hostapd_das_nas_mismatch(hapd, attr))
982 return RADIUS_DAS_NAS_MISMATCH;
983
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800984 sta = hostapd_das_find_sta(hapd, attr, &multi);
985 if (sta == NULL) {
986 if (multi) {
987 wpa_printf(MSG_DEBUG,
988 "RADIUS DAS: Multiple sessions match - not supported");
989 return RADIUS_DAS_MULTI_SESSION_MATCH;
990 }
991 if (hostapd_das_disconnect_pmksa(hapd, attr) == 0) {
992 wpa_printf(MSG_DEBUG,
993 "RADIUS DAS: PMKSA cache entry matched");
994 return RADIUS_DAS_SUCCESS;
995 }
996 wpa_printf(MSG_DEBUG, "RADIUS DAS: No matching session found");
Dmitry Shmidt04949592012-07-19 12:16:46 -0700997 return RADIUS_DAS_SESSION_NOT_FOUND;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800998 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700999
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001000 wpa_printf(MSG_DEBUG, "RADIUS DAS: Found a matching session " MACSTR
1001 " - disconnecting", MAC2STR(sta->addr));
Dmitry Shmidt13ca8d82014-02-20 10:18:40 -08001002 wpa_auth_pmksa_remove(hapd->wpa_auth, sta->addr);
1003
Dmitry Shmidt04949592012-07-19 12:16:46 -07001004 hostapd_drv_sta_deauth(hapd, sta->addr,
1005 WLAN_REASON_PREV_AUTH_NOT_VALID);
1006 ap_sta_deauthenticate(hapd, sta, WLAN_REASON_PREV_AUTH_NOT_VALID);
1007
1008 return RADIUS_DAS_SUCCESS;
1009}
1010
Roshan Pius3a1667e2018-07-03 15:17:14 -07001011
1012#ifdef CONFIG_HS20
1013static enum radius_das_res
1014hostapd_das_coa(void *ctx, struct radius_das_attrs *attr)
1015{
1016 struct hostapd_data *hapd = ctx;
1017 struct sta_info *sta;
1018 int multi;
1019
1020 if (hostapd_das_nas_mismatch(hapd, attr))
1021 return RADIUS_DAS_NAS_MISMATCH;
1022
1023 sta = hostapd_das_find_sta(hapd, attr, &multi);
1024 if (!sta) {
1025 if (multi) {
1026 wpa_printf(MSG_DEBUG,
1027 "RADIUS DAS: Multiple sessions match - not supported");
1028 return RADIUS_DAS_MULTI_SESSION_MATCH;
1029 }
1030 wpa_printf(MSG_DEBUG, "RADIUS DAS: No matching session found");
1031 return RADIUS_DAS_SESSION_NOT_FOUND;
1032 }
1033
1034 wpa_printf(MSG_DEBUG, "RADIUS DAS: Found a matching session " MACSTR
1035 " - CoA", MAC2STR(sta->addr));
1036
1037 if (attr->hs20_t_c_filtering) {
1038 if (attr->hs20_t_c_filtering[0] & BIT(0)) {
1039 wpa_printf(MSG_DEBUG,
1040 "HS 2.0: Unexpected Terms and Conditions filtering required in CoA-Request");
1041 return RADIUS_DAS_COA_FAILED;
1042 }
1043
1044 hs20_t_c_filtering(hapd, sta, 0);
1045 }
1046
1047 return RADIUS_DAS_SUCCESS;
1048}
1049#else /* CONFIG_HS20 */
1050#define hostapd_das_coa NULL
1051#endif /* CONFIG_HS20 */
1052
Hai Shalomc3565922019-10-28 11:58:20 -07001053
1054#ifdef CONFIG_SQLITE
1055
1056static int db_table_exists(sqlite3 *db, const char *name)
1057{
1058 char cmd[128];
1059
1060 os_snprintf(cmd, sizeof(cmd), "SELECT 1 FROM %s;", name);
1061 return sqlite3_exec(db, cmd, NULL, NULL, NULL) == SQLITE_OK;
1062}
1063
1064
1065static int db_table_create_radius_attributes(sqlite3 *db)
1066{
1067 char *err = NULL;
1068 const char *sql =
1069 "CREATE TABLE radius_attributes("
1070 " id INTEGER PRIMARY KEY,"
1071 " sta TEXT,"
1072 " reqtype TEXT,"
1073 " attr TEXT"
1074 ");"
1075 "CREATE INDEX idx_sta_reqtype ON radius_attributes(sta,reqtype);";
1076
1077 wpa_printf(MSG_DEBUG,
1078 "Adding database table for RADIUS attribute information");
1079 if (sqlite3_exec(db, sql, NULL, NULL, &err) != SQLITE_OK) {
1080 wpa_printf(MSG_ERROR, "SQLite error: %s", err);
1081 sqlite3_free(err);
1082 return -1;
1083 }
1084
1085 return 0;
1086}
1087
1088#endif /* CONFIG_SQLITE */
1089
Dmitry Shmidt04949592012-07-19 12:16:46 -07001090#endif /* CONFIG_NO_RADIUS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001091
1092
1093/**
1094 * hostapd_setup_bss - Per-BSS setup (initialization)
1095 * @hapd: Pointer to BSS data
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001096 * @first: Whether this BSS is the first BSS of an interface; -1 = not first,
1097 * but interface may exist
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001098 *
1099 * This function is used to initialize all per-BSS data structures and
1100 * resources. This gets called in a loop for each BSS when an interface is
1101 * initialized. Most of the modules that are initialized here will be
1102 * deinitialized in hostapd_cleanup().
1103 */
1104static int hostapd_setup_bss(struct hostapd_data *hapd, int first)
1105{
1106 struct hostapd_bss_config *conf = hapd->conf;
Dmitry Shmidt9d9e6022015-04-23 10:34:55 -07001107 u8 ssid[SSID_MAX_LEN + 1];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001108 int ssid_len, set_ssid;
1109 char force_ifname[IFNAMSIZ];
1110 u8 if_addr[ETH_ALEN];
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001111 int flush_old_stations = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001112
Dmitry Shmidt54605472013-11-08 11:10:19 -08001113 wpa_printf(MSG_DEBUG, "%s(hapd=%p (%s), first=%d)",
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001114 __func__, hapd, conf->iface, first);
Dmitry Shmidt54605472013-11-08 11:10:19 -08001115
Dmitry Shmidt50b691d2014-05-21 14:01:45 -07001116#ifdef EAP_SERVER_TNC
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001117 if (conf->tnc && tncs_global_init() < 0) {
Dmitry Shmidt50b691d2014-05-21 14:01:45 -07001118 wpa_printf(MSG_ERROR, "Failed to initialize TNCS");
1119 return -1;
1120 }
1121#endif /* EAP_SERVER_TNC */
1122
Dmitry Shmidt54605472013-11-08 11:10:19 -08001123 if (hapd->started) {
1124 wpa_printf(MSG_ERROR, "%s: Interface %s was already started",
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001125 __func__, conf->iface);
Dmitry Shmidt54605472013-11-08 11:10:19 -08001126 return -1;
1127 }
1128 hapd->started = 1;
1129
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001130 if (!first || first == -1) {
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08001131 u8 *addr = hapd->own_addr;
1132
1133 if (!is_zero_ether_addr(conf->bssid)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001134 /* Allocate the configured BSSID. */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001135 os_memcpy(hapd->own_addr, conf->bssid, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001136
1137 if (hostapd_mac_comp(hapd->own_addr,
1138 hapd->iface->bss[0]->own_addr) ==
1139 0) {
1140 wpa_printf(MSG_ERROR, "BSS '%s' may not have "
1141 "BSSID set to the MAC address of "
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001142 "the radio", conf->iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001143 return -1;
1144 }
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08001145 } else if (hapd->iconf->use_driver_iface_addr) {
1146 addr = NULL;
1147 } else {
1148 /* Allocate the next available BSSID. */
1149 do {
1150 inc_byte_array(hapd->own_addr, ETH_ALEN);
1151 } while (mac_in_conf(hapd->iconf, hapd->own_addr));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001152 }
1153
Dmitry Shmidt54605472013-11-08 11:10:19 -08001154 hapd->interface_added = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001155 if (hostapd_if_add(hapd->iface->bss[0], WPA_IF_AP_BSS,
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08001156 conf->iface, addr, hapd,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001157 &hapd->drv_priv, force_ifname, if_addr,
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001158 conf->bridge[0] ? conf->bridge : NULL,
1159 first == -1)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001160 wpa_printf(MSG_ERROR, "Failed to add BSS (BSSID="
1161 MACSTR ")", MAC2STR(hapd->own_addr));
Dmitry Shmidt3cf6f792013-12-18 13:12:19 -08001162 hapd->interface_added = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001163 return -1;
1164 }
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08001165
1166 if (!addr)
1167 os_memcpy(hapd->own_addr, if_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001168 }
1169
1170 if (conf->wmm_enabled < 0)
1171 conf->wmm_enabled = hapd->iconf->ieee80211n;
1172
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001173#ifdef CONFIG_IEEE80211R_AP
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001174 if (is_zero_ether_addr(conf->r1_key_holder))
1175 os_memcpy(conf->r1_key_holder, hapd->own_addr, ETH_ALEN);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001176#endif /* CONFIG_IEEE80211R_AP */
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001177
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001178#ifdef CONFIG_MESH
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001179 if ((hapd->conf->mesh & MESH_ENABLED) && hapd->iface->mconf == NULL)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001180 flush_old_stations = 0;
1181#endif /* CONFIG_MESH */
1182
1183 if (flush_old_stations)
Hai Shalom4fbc08f2020-05-18 12:37:00 -07001184 hostapd_flush(hapd);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001185 hostapd_set_privacy(hapd, 0);
1186
Hai Shalomfdcde762020-04-02 11:19:20 -07001187#ifdef CONFIG_WEP
Hai Shalom4fbc08f2020-05-18 12:37:00 -07001188 if (!hostapd_drv_nl80211(hapd))
1189 hostapd_broadcast_wep_clear(hapd);
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001190 if (hostapd_setup_encryption(conf->iface, hapd))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001191 return -1;
Hai Shalomfdcde762020-04-02 11:19:20 -07001192#endif /* CONFIG_WEP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001193
1194 /*
1195 * Fetch the SSID from the system and use it or,
1196 * if one was specified in the config file, verify they
1197 * match.
1198 */
1199 ssid_len = hostapd_get_ssid(hapd, ssid, sizeof(ssid));
1200 if (ssid_len < 0) {
1201 wpa_printf(MSG_ERROR, "Could not read SSID from system");
1202 return -1;
1203 }
1204 if (conf->ssid.ssid_set) {
1205 /*
1206 * If SSID is specified in the config file and it differs
1207 * from what is being used then force installation of the
1208 * new SSID.
1209 */
1210 set_ssid = (conf->ssid.ssid_len != (size_t) ssid_len ||
1211 os_memcmp(conf->ssid.ssid, ssid, ssid_len) != 0);
1212 } else {
1213 /*
1214 * No SSID in the config file; just use the one we got
1215 * from the system.
1216 */
1217 set_ssid = 0;
1218 conf->ssid.ssid_len = ssid_len;
1219 os_memcpy(conf->ssid.ssid, ssid, conf->ssid.ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001220 }
1221
Hai Shalomfdcde762020-04-02 11:19:20 -07001222 /*
1223 * Short SSID calculation is identical to FCS and it is defined in
1224 * IEEE P802.11-REVmd/D3.0, 9.4.2.170.3 (Calculating the Short-SSID).
1225 */
1226 conf->ssid.short_ssid = crc32(conf->ssid.ssid, conf->ssid.ssid_len);
1227
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001228 if (!hostapd_drv_none(hapd)) {
Hai Shalomfdcde762020-04-02 11:19:20 -07001229 wpa_printf(MSG_DEBUG, "Using interface %s with hwaddr " MACSTR
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001230 " and ssid \"%s\"",
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001231 conf->iface, MAC2STR(hapd->own_addr),
1232 wpa_ssid_txt(conf->ssid.ssid, conf->ssid.ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001233 }
1234
1235 if (hostapd_setup_wpa_psk(conf)) {
1236 wpa_printf(MSG_ERROR, "WPA-PSK setup failed.");
1237 return -1;
1238 }
1239
1240 /* Set SSID for the kernel driver (to be used in beacon and probe
1241 * response frames) */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001242 if (set_ssid && hostapd_set_ssid(hapd, conf->ssid.ssid,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001243 conf->ssid.ssid_len)) {
1244 wpa_printf(MSG_ERROR, "Could not set SSID for kernel driver");
1245 return -1;
1246 }
1247
Dmitry Shmidt818ea482014-03-10 13:15:21 -07001248 if (wpa_debug_level <= MSG_MSGDUMP)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001249 conf->radius->msg_dumps = 1;
1250#ifndef CONFIG_NO_RADIUS
Hai Shalomc3565922019-10-28 11:58:20 -07001251
1252#ifdef CONFIG_SQLITE
1253 if (conf->radius_req_attr_sqlite) {
1254 if (sqlite3_open(conf->radius_req_attr_sqlite,
1255 &hapd->rad_attr_db)) {
1256 wpa_printf(MSG_ERROR, "Could not open SQLite file '%s'",
1257 conf->radius_req_attr_sqlite);
1258 return -1;
1259 }
1260
1261 wpa_printf(MSG_DEBUG, "Opening RADIUS attribute database: %s",
1262 conf->radius_req_attr_sqlite);
1263 if (!db_table_exists(hapd->rad_attr_db, "radius_attributes") &&
1264 db_table_create_radius_attributes(hapd->rad_attr_db) < 0)
1265 return -1;
1266 }
1267#endif /* CONFIG_SQLITE */
1268
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001269 hapd->radius = radius_client_init(hapd, conf->radius);
1270 if (hapd->radius == NULL) {
1271 wpa_printf(MSG_ERROR, "RADIUS client initialization failed.");
1272 return -1;
1273 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001274
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001275 if (conf->radius_das_port) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001276 struct radius_das_conf das_conf;
1277 os_memset(&das_conf, 0, sizeof(das_conf));
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001278 das_conf.port = conf->radius_das_port;
1279 das_conf.shared_secret = conf->radius_das_shared_secret;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001280 das_conf.shared_secret_len =
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001281 conf->radius_das_shared_secret_len;
1282 das_conf.client_addr = &conf->radius_das_client_addr;
1283 das_conf.time_window = conf->radius_das_time_window;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001284 das_conf.require_event_timestamp =
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001285 conf->radius_das_require_event_timestamp;
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07001286 das_conf.require_message_authenticator =
1287 conf->radius_das_require_message_authenticator;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001288 das_conf.ctx = hapd;
1289 das_conf.disconnect = hostapd_das_disconnect;
Roshan Pius3a1667e2018-07-03 15:17:14 -07001290 das_conf.coa = hostapd_das_coa;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001291 hapd->radius_das = radius_das_init(&das_conf);
1292 if (hapd->radius_das == NULL) {
1293 wpa_printf(MSG_ERROR, "RADIUS DAS initialization "
1294 "failed.");
1295 return -1;
1296 }
1297 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001298#endif /* CONFIG_NO_RADIUS */
1299
1300 if (hostapd_acl_init(hapd)) {
1301 wpa_printf(MSG_ERROR, "ACL initialization failed.");
1302 return -1;
1303 }
1304 if (hostapd_init_wps(hapd, conf))
1305 return -1;
1306
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001307#ifdef CONFIG_DPP
1308 hapd->gas = gas_query_ap_init(hapd, hapd->msg_ctx);
1309 if (!hapd->gas)
1310 return -1;
1311 if (hostapd_dpp_init(hapd))
1312 return -1;
1313#endif /* CONFIG_DPP */
1314
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001315 if (authsrv_init(hapd) < 0)
1316 return -1;
1317
1318 if (ieee802_1x_init(hapd)) {
1319 wpa_printf(MSG_ERROR, "IEEE 802.1X initialization failed.");
1320 return -1;
1321 }
1322
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001323 if ((conf->wpa || conf->osen) && hostapd_setup_wpa(hapd))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001324 return -1;
1325
1326 if (accounting_init(hapd)) {
1327 wpa_printf(MSG_ERROR, "Accounting initialization failed.");
1328 return -1;
1329 }
1330
Dmitry Shmidt04949592012-07-19 12:16:46 -07001331#ifdef CONFIG_INTERWORKING
1332 if (gas_serv_init(hapd)) {
1333 wpa_printf(MSG_ERROR, "GAS server initialization failed");
1334 return -1;
1335 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001336
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001337 if (conf->qos_map_set_len &&
1338 hostapd_drv_set_qos_map(hapd, conf->qos_map_set,
1339 conf->qos_map_set_len)) {
1340 wpa_printf(MSG_ERROR, "Failed to initialize QoS Map");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001341 return -1;
1342 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001343#endif /* CONFIG_INTERWORKING */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001344
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001345 if (conf->bss_load_update_period && bss_load_update_init(hapd)) {
1346 wpa_printf(MSG_ERROR, "BSS Load initialization failed");
1347 return -1;
1348 }
1349
1350 if (conf->proxy_arp) {
1351 if (x_snoop_init(hapd)) {
1352 wpa_printf(MSG_ERROR,
1353 "Generic snooping infrastructure initialization failed");
1354 return -1;
1355 }
1356
1357 if (dhcp_snoop_init(hapd)) {
1358 wpa_printf(MSG_ERROR,
1359 "DHCP snooping initialization failed");
1360 return -1;
1361 }
1362
1363 if (ndisc_snoop_init(hapd)) {
1364 wpa_printf(MSG_ERROR,
1365 "Neighbor Discovery snooping initialization failed");
1366 return -1;
1367 }
1368 }
1369
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001370 if (!hostapd_drv_none(hapd) && vlan_init(hapd)) {
1371 wpa_printf(MSG_ERROR, "VLAN initialization failed.");
1372 return -1;
1373 }
1374
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001375 if (!conf->start_disabled && ieee802_11_set_beacon(hapd) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001376 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001377
Hai Shalom4fbc08f2020-05-18 12:37:00 -07001378 if (flush_old_stations && !conf->start_disabled &&
1379 conf->broadcast_deauth) {
1380 u8 addr[ETH_ALEN];
1381
1382 /* Should any previously associated STA not have noticed that
1383 * the AP had stopped and restarted, send one more
1384 * deauthentication notification now that the AP is ready to
1385 * operate. */
1386 wpa_dbg(hapd->msg_ctx, MSG_DEBUG,
1387 "Deauthenticate all stations at BSS start");
1388 os_memset(addr, 0xff, ETH_ALEN);
1389 hostapd_drv_sta_deauth(hapd, addr,
1390 WLAN_REASON_PREV_AUTH_NOT_VALID);
1391 }
1392
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001393 if (hapd->wpa_auth && wpa_init_keys(hapd->wpa_auth) < 0)
1394 return -1;
1395
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001396 if (hapd->driver && hapd->driver->set_operstate)
1397 hapd->driver->set_operstate(hapd->drv_priv, 1);
1398
1399 return 0;
1400}
1401
1402
1403static void hostapd_tx_queue_params(struct hostapd_iface *iface)
1404{
1405 struct hostapd_data *hapd = iface->bss[0];
1406 int i;
1407 struct hostapd_tx_queue_params *p;
1408
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001409#ifdef CONFIG_MESH
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001410 if ((hapd->conf->mesh & MESH_ENABLED) && iface->mconf == NULL)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001411 return;
1412#endif /* CONFIG_MESH */
1413
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001414 for (i = 0; i < NUM_TX_QUEUES; i++) {
1415 p = &iface->conf->tx_queue[i];
1416
1417 if (hostapd_set_tx_queue_params(hapd, i, p->aifs, p->cwmin,
1418 p->cwmax, p->burst)) {
1419 wpa_printf(MSG_DEBUG, "Failed to set TX queue "
1420 "parameters for queue %d.", i);
1421 /* Continue anyway */
1422 }
1423 }
1424}
1425
1426
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001427static int hostapd_set_acl_list(struct hostapd_data *hapd,
1428 struct mac_acl_entry *mac_acl,
1429 int n_entries, u8 accept_acl)
1430{
1431 struct hostapd_acl_params *acl_params;
1432 int i, err;
1433
1434 acl_params = os_zalloc(sizeof(*acl_params) +
1435 (n_entries * sizeof(acl_params->mac_acl[0])));
1436 if (!acl_params)
1437 return -ENOMEM;
1438
1439 for (i = 0; i < n_entries; i++)
1440 os_memcpy(acl_params->mac_acl[i].addr, mac_acl[i].addr,
1441 ETH_ALEN);
1442
1443 acl_params->acl_policy = accept_acl;
1444 acl_params->num_mac_acl = n_entries;
1445
1446 err = hostapd_drv_set_acl(hapd, acl_params);
1447
1448 os_free(acl_params);
1449
1450 return err;
1451}
1452
1453
1454static void hostapd_set_acl(struct hostapd_data *hapd)
1455{
1456 struct hostapd_config *conf = hapd->iconf;
1457 int err;
1458 u8 accept_acl;
1459
1460 if (hapd->iface->drv_max_acl_mac_addrs == 0)
1461 return;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001462
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001463 if (conf->bss[0]->macaddr_acl == DENY_UNLESS_ACCEPTED) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001464 accept_acl = 1;
1465 err = hostapd_set_acl_list(hapd, conf->bss[0]->accept_mac,
1466 conf->bss[0]->num_accept_mac,
1467 accept_acl);
1468 if (err) {
1469 wpa_printf(MSG_DEBUG, "Failed to set accept acl");
1470 return;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001471 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001472 } else if (conf->bss[0]->macaddr_acl == ACCEPT_UNLESS_DENIED) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001473 accept_acl = 0;
1474 err = hostapd_set_acl_list(hapd, conf->bss[0]->deny_mac,
1475 conf->bss[0]->num_deny_mac,
1476 accept_acl);
1477 if (err) {
1478 wpa_printf(MSG_DEBUG, "Failed to set deny acl");
1479 return;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001480 }
1481 }
1482}
1483
1484
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001485static int start_ctrl_iface_bss(struct hostapd_data *hapd)
1486{
1487 if (!hapd->iface->interfaces ||
1488 !hapd->iface->interfaces->ctrl_iface_init)
1489 return 0;
1490
1491 if (hapd->iface->interfaces->ctrl_iface_init(hapd)) {
1492 wpa_printf(MSG_ERROR,
1493 "Failed to setup control interface for %s",
1494 hapd->conf->iface);
1495 return -1;
1496 }
1497
1498 return 0;
1499}
1500
1501
1502static int start_ctrl_iface(struct hostapd_iface *iface)
1503{
1504 size_t i;
1505
1506 if (!iface->interfaces || !iface->interfaces->ctrl_iface_init)
1507 return 0;
1508
1509 for (i = 0; i < iface->num_bss; i++) {
1510 struct hostapd_data *hapd = iface->bss[i];
1511 if (iface->interfaces->ctrl_iface_init(hapd)) {
1512 wpa_printf(MSG_ERROR,
1513 "Failed to setup control interface for %s",
1514 hapd->conf->iface);
1515 return -1;
1516 }
1517 }
1518
1519 return 0;
1520}
1521
1522
1523static void channel_list_update_timeout(void *eloop_ctx, void *timeout_ctx)
1524{
1525 struct hostapd_iface *iface = eloop_ctx;
1526
1527 if (!iface->wait_channel_update) {
1528 wpa_printf(MSG_INFO, "Channel list update timeout, but interface was not waiting for it");
1529 return;
1530 }
1531
1532 /*
1533 * It is possible that the existing channel list is acceptable, so try
1534 * to proceed.
1535 */
1536 wpa_printf(MSG_DEBUG, "Channel list update timeout - try to continue anyway");
1537 setup_interface2(iface);
1538}
1539
1540
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001541void hostapd_channel_list_updated(struct hostapd_iface *iface, int initiator)
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001542{
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001543 if (!iface->wait_channel_update || initiator != REGDOM_SET_BY_USER)
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001544 return;
1545
1546 wpa_printf(MSG_DEBUG, "Channel list updated - continue setup");
1547 eloop_cancel_timeout(channel_list_update_timeout, iface, NULL);
1548 setup_interface2(iface);
1549}
1550
1551
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001552static int setup_interface(struct hostapd_iface *iface)
1553{
1554 struct hostapd_data *hapd = iface->bss[0];
1555 size_t i;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001556
Dmitry Shmidta38abf92014-03-06 13:38:44 -08001557 /*
1558 * It is possible that setup_interface() is called after the interface
1559 * was disabled etc., in which case driver_ap_teardown is possibly set
1560 * to 1. Clear it here so any other key/station deletion, which is not
1561 * part of a teardown flow, would also call the relevant driver
1562 * callbacks.
1563 */
1564 iface->driver_ap_teardown = 0;
1565
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001566 if (!iface->phy[0]) {
1567 const char *phy = hostapd_drv_get_radio_name(hapd);
1568 if (phy) {
1569 wpa_printf(MSG_DEBUG, "phy: %s", phy);
1570 os_strlcpy(iface->phy, phy, sizeof(iface->phy));
1571 }
1572 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001573
1574 /*
1575 * Make sure that all BSSes get configured with a pointer to the same
1576 * driver interface.
1577 */
1578 for (i = 1; i < iface->num_bss; i++) {
1579 iface->bss[i]->driver = hapd->driver;
1580 iface->bss[i]->drv_priv = hapd->drv_priv;
1581 }
1582
1583 if (hostapd_validate_bssid_configuration(iface))
1584 return -1;
1585
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001586 /*
1587 * Initialize control interfaces early to allow external monitoring of
1588 * channel setup operations that may take considerable amount of time
1589 * especially for DFS cases.
1590 */
1591 if (start_ctrl_iface(iface))
1592 return -1;
1593
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001594 if (hapd->iconf->country[0] && hapd->iconf->country[1]) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001595 char country[4], previous_country[4];
1596
1597 hostapd_set_state(iface, HAPD_IFACE_COUNTRY_UPDATE);
1598 if (hostapd_get_country(hapd, previous_country) < 0)
1599 previous_country[0] = '\0';
1600
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001601 os_memcpy(country, hapd->iconf->country, 3);
1602 country[3] = '\0';
1603 if (hostapd_set_country(hapd, country) < 0) {
1604 wpa_printf(MSG_ERROR, "Failed to set country code");
1605 return -1;
1606 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001607
1608 wpa_printf(MSG_DEBUG, "Previous country code %s, new country code %s",
1609 previous_country, country);
1610
1611 if (os_strncmp(previous_country, country, 2) != 0) {
1612 wpa_printf(MSG_DEBUG, "Continue interface setup after channel list update");
1613 iface->wait_channel_update = 1;
Dmitry Shmidt97672262014-02-03 13:02:54 -08001614 eloop_register_timeout(5, 0,
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001615 channel_list_update_timeout,
1616 iface, NULL);
1617 return 0;
1618 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001619 }
1620
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001621 return setup_interface2(iface);
1622}
1623
1624
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001625static int configured_fixed_chan_to_freq(struct hostapd_iface *iface)
1626{
1627 int freq, i, j;
1628
1629 if (!iface->conf->channel)
1630 return 0;
1631 if (iface->conf->op_class) {
1632 freq = ieee80211_chan_to_freq(NULL, iface->conf->op_class,
1633 iface->conf->channel);
1634 if (freq < 0) {
1635 wpa_printf(MSG_INFO,
1636 "Could not convert op_class %u channel %u to operating frequency",
1637 iface->conf->op_class, iface->conf->channel);
1638 return -1;
1639 }
1640 iface->freq = freq;
1641 return 0;
1642 }
1643
1644 /* Old configurations using only 2.4/5/60 GHz bands may not specify the
1645 * op_class parameter. Select a matching channel from the configured
1646 * mode using the channel parameter for these cases.
1647 */
1648 for (j = 0; j < iface->num_hw_features; j++) {
1649 struct hostapd_hw_modes *mode = &iface->hw_features[j];
1650
1651 if (iface->conf->hw_mode != HOSTAPD_MODE_IEEE80211ANY &&
1652 iface->conf->hw_mode != mode->mode)
1653 continue;
1654 for (i = 0; i < mode->num_channels; i++) {
1655 struct hostapd_channel_data *chan = &mode->channels[i];
1656
1657 if (chan->chan == iface->conf->channel &&
1658 !is_6ghz_freq(chan->freq)) {
1659 iface->freq = chan->freq;
1660 return 0;
1661 }
1662 }
1663 }
1664
1665 wpa_printf(MSG_INFO, "Could not determine operating frequency");
1666 return -1;
1667}
1668
1669
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001670static int setup_interface2(struct hostapd_iface *iface)
1671{
1672 iface->wait_channel_update = 0;
1673
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001674 if (hostapd_get_hw_features(iface)) {
1675 /* Not all drivers support this yet, so continue without hw
1676 * feature data. */
1677 } else {
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001678 int ret;
1679
1680 ret = configured_fixed_chan_to_freq(iface);
1681 if (ret < 0)
1682 goto fail;
1683
1684 if (iface->conf->op_class) {
1685 int ch_width;
1686
1687 ch_width = op_class_to_ch_width(iface->conf->op_class);
1688 hostapd_set_oper_chwidth(iface->conf, ch_width);
1689 }
1690
1691 ret = hostapd_select_hw_mode(iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001692 if (ret < 0) {
1693 wpa_printf(MSG_ERROR, "Could not select hw_mode and "
1694 "channel. (%d)", ret);
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001695 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001696 }
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001697 if (ret == 1) {
1698 wpa_printf(MSG_DEBUG, "Interface initialization will be completed in a callback (ACS)");
1699 return 0;
1700 }
Hai Shalomc3565922019-10-28 11:58:20 -07001701 ret = hostapd_check_edmg_capab(iface);
1702 if (ret < 0)
1703 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001704 ret = hostapd_check_ht_capab(iface);
1705 if (ret < 0)
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001706 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001707 if (ret == 1) {
1708 wpa_printf(MSG_DEBUG, "Interface initialization will "
1709 "be completed in a callback");
1710 return 0;
1711 }
Dmitry Shmidt051af732013-10-22 13:52:46 -07001712
1713 if (iface->conf->ieee80211h)
1714 wpa_printf(MSG_DEBUG, "DFS support is enabled");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001715 }
1716 return hostapd_setup_interface_complete(iface, 0);
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001717
1718fail:
1719 hostapd_set_state(iface, HAPD_IFACE_DISABLED);
1720 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
1721 if (iface->interfaces && iface->interfaces->terminate_on_error)
1722 eloop_terminate();
1723 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001724}
1725
1726
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001727#ifdef CONFIG_FST
1728
1729static const u8 * fst_hostapd_get_bssid_cb(void *ctx)
1730{
1731 struct hostapd_data *hapd = ctx;
1732
1733 return hapd->own_addr;
1734}
1735
1736
1737static void fst_hostapd_get_channel_info_cb(void *ctx,
1738 enum hostapd_hw_mode *hw_mode,
1739 u8 *channel)
1740{
1741 struct hostapd_data *hapd = ctx;
1742
1743 *hw_mode = ieee80211_freq_to_chan(hapd->iface->freq, channel);
1744}
1745
1746
1747static void fst_hostapd_set_ies_cb(void *ctx, const struct wpabuf *fst_ies)
1748{
1749 struct hostapd_data *hapd = ctx;
1750
1751 if (hapd->iface->fst_ies != fst_ies) {
1752 hapd->iface->fst_ies = fst_ies;
1753 if (ieee802_11_set_beacon(hapd))
1754 wpa_printf(MSG_WARNING, "FST: Cannot set beacon");
1755 }
1756}
1757
1758
1759static int fst_hostapd_send_action_cb(void *ctx, const u8 *da,
1760 struct wpabuf *buf)
1761{
1762 struct hostapd_data *hapd = ctx;
1763
1764 return hostapd_drv_send_action(hapd, hapd->iface->freq, 0, da,
1765 wpabuf_head(buf), wpabuf_len(buf));
1766}
1767
1768
1769static const struct wpabuf * fst_hostapd_get_mb_ie_cb(void *ctx, const u8 *addr)
1770{
1771 struct hostapd_data *hapd = ctx;
1772 struct sta_info *sta = ap_get_sta(hapd, addr);
1773
1774 return sta ? sta->mb_ies : NULL;
1775}
1776
1777
1778static void fst_hostapd_update_mb_ie_cb(void *ctx, const u8 *addr,
1779 const u8 *buf, size_t size)
1780{
1781 struct hostapd_data *hapd = ctx;
1782 struct sta_info *sta = ap_get_sta(hapd, addr);
1783
1784 if (sta) {
1785 struct mb_ies_info info;
1786
1787 if (!mb_ies_info_by_ies(&info, buf, size)) {
1788 wpabuf_free(sta->mb_ies);
1789 sta->mb_ies = mb_ies_by_info(&info);
1790 }
1791 }
1792}
1793
1794
1795static const u8 * fst_hostapd_get_sta(struct fst_get_peer_ctx **get_ctx,
Hai Shalome21d4e82020-04-29 16:34:06 -07001796 bool mb_only)
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001797{
1798 struct sta_info *s = (struct sta_info *) *get_ctx;
1799
1800 if (mb_only) {
1801 for (; s && !s->mb_ies; s = s->next)
1802 ;
1803 }
1804
1805 if (s) {
1806 *get_ctx = (struct fst_get_peer_ctx *) s->next;
1807
1808 return s->addr;
1809 }
1810
1811 *get_ctx = NULL;
1812 return NULL;
1813}
1814
1815
1816static const u8 * fst_hostapd_get_peer_first(void *ctx,
1817 struct fst_get_peer_ctx **get_ctx,
Hai Shalome21d4e82020-04-29 16:34:06 -07001818 bool mb_only)
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001819{
1820 struct hostapd_data *hapd = ctx;
1821
1822 *get_ctx = (struct fst_get_peer_ctx *) hapd->sta_list;
1823
1824 return fst_hostapd_get_sta(get_ctx, mb_only);
1825}
1826
1827
1828static const u8 * fst_hostapd_get_peer_next(void *ctx,
1829 struct fst_get_peer_ctx **get_ctx,
Hai Shalome21d4e82020-04-29 16:34:06 -07001830 bool mb_only)
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001831{
1832 return fst_hostapd_get_sta(get_ctx, mb_only);
1833}
1834
1835
1836void fst_hostapd_fill_iface_obj(struct hostapd_data *hapd,
1837 struct fst_wpa_obj *iface_obj)
1838{
1839 iface_obj->ctx = hapd;
1840 iface_obj->get_bssid = fst_hostapd_get_bssid_cb;
1841 iface_obj->get_channel_info = fst_hostapd_get_channel_info_cb;
1842 iface_obj->set_ies = fst_hostapd_set_ies_cb;
1843 iface_obj->send_action = fst_hostapd_send_action_cb;
1844 iface_obj->get_mb_ie = fst_hostapd_get_mb_ie_cb;
1845 iface_obj->update_mb_ie = fst_hostapd_update_mb_ie_cb;
1846 iface_obj->get_peer_first = fst_hostapd_get_peer_first;
1847 iface_obj->get_peer_next = fst_hostapd_get_peer_next;
1848}
1849
1850#endif /* CONFIG_FST */
1851
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001852#ifdef CONFIG_OWE
1853
1854static int hostapd_owe_iface_iter(struct hostapd_iface *iface, void *ctx)
1855{
1856 struct hostapd_data *hapd = ctx;
1857 size_t i;
1858
1859 for (i = 0; i < iface->num_bss; i++) {
1860 struct hostapd_data *bss = iface->bss[i];
1861
1862 if (os_strcmp(hapd->conf->owe_transition_ifname,
1863 bss->conf->iface) != 0)
1864 continue;
1865
1866 wpa_printf(MSG_DEBUG,
1867 "OWE: ifname=%s found transition mode ifname=%s BSSID "
1868 MACSTR " SSID %s",
1869 hapd->conf->iface, bss->conf->iface,
1870 MAC2STR(bss->own_addr),
1871 wpa_ssid_txt(bss->conf->ssid.ssid,
1872 bss->conf->ssid.ssid_len));
1873 if (!bss->conf->ssid.ssid_set || !bss->conf->ssid.ssid_len ||
1874 is_zero_ether_addr(bss->own_addr))
1875 continue;
1876
1877 os_memcpy(hapd->conf->owe_transition_bssid, bss->own_addr,
1878 ETH_ALEN);
1879 os_memcpy(hapd->conf->owe_transition_ssid,
1880 bss->conf->ssid.ssid, bss->conf->ssid.ssid_len);
1881 hapd->conf->owe_transition_ssid_len = bss->conf->ssid.ssid_len;
1882 wpa_printf(MSG_DEBUG,
1883 "OWE: Copied transition mode information");
1884 return 1;
1885 }
1886
1887 return 0;
1888}
1889
1890
1891int hostapd_owe_trans_get_info(struct hostapd_data *hapd)
1892{
1893 if (hapd->conf->owe_transition_ssid_len > 0 &&
1894 !is_zero_ether_addr(hapd->conf->owe_transition_bssid))
1895 return 0;
1896
1897 /* Find transition mode SSID/BSSID information from a BSS operated by
1898 * this hostapd instance. */
1899 if (!hapd->iface->interfaces ||
1900 !hapd->iface->interfaces->for_each_interface)
1901 return hostapd_owe_iface_iter(hapd->iface, hapd);
1902 else
1903 return hapd->iface->interfaces->for_each_interface(
1904 hapd->iface->interfaces, hostapd_owe_iface_iter, hapd);
1905}
1906
1907
1908static int hostapd_owe_iface_iter2(struct hostapd_iface *iface, void *ctx)
1909{
1910 size_t i;
1911
1912 for (i = 0; i < iface->num_bss; i++) {
1913 struct hostapd_data *bss = iface->bss[i];
1914 int res;
1915
1916 if (!bss->conf->owe_transition_ifname[0])
1917 continue;
Hai Shalom899fcc72020-10-19 14:38:18 -07001918 if (bss->iface->state != HAPD_IFACE_ENABLED) {
1919 wpa_printf(MSG_DEBUG,
1920 "OWE: Interface %s state %s - defer beacon update",
1921 bss->conf->iface,
1922 hostapd_state_text(bss->iface->state));
1923 continue;
1924 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001925 res = hostapd_owe_trans_get_info(bss);
1926 if (res == 0)
1927 continue;
1928 wpa_printf(MSG_DEBUG,
1929 "OWE: Matching transition mode interface enabled - update beacon data for %s",
1930 bss->conf->iface);
1931 ieee802_11_set_beacon(bss);
1932 }
1933
1934 return 0;
1935}
1936
1937#endif /* CONFIG_OWE */
1938
1939
1940static void hostapd_owe_update_trans(struct hostapd_iface *iface)
1941{
1942#ifdef CONFIG_OWE
1943 /* Check whether the enabled BSS can complete OWE transition mode
1944 * configuration for any pending interface. */
1945 if (!iface->interfaces ||
1946 !iface->interfaces->for_each_interface)
1947 hostapd_owe_iface_iter2(iface, NULL);
1948 else
1949 iface->interfaces->for_each_interface(
1950 iface->interfaces, hostapd_owe_iface_iter2, NULL);
1951#endif /* CONFIG_OWE */
1952}
1953
1954
Roshan Pius3a1667e2018-07-03 15:17:14 -07001955static void hostapd_interface_setup_failure_handler(void *eloop_ctx,
1956 void *timeout_ctx)
1957{
1958 struct hostapd_iface *iface = eloop_ctx;
1959 struct hostapd_data *hapd;
1960
1961 if (iface->num_bss < 1 || !iface->bss || !iface->bss[0])
1962 return;
1963 hapd = iface->bss[0];
1964 if (hapd->setup_complete_cb)
1965 hapd->setup_complete_cb(hapd->setup_complete_cb_ctx);
1966}
1967
1968
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001969static int hostapd_setup_interface_complete_sync(struct hostapd_iface *iface,
1970 int err)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001971{
1972 struct hostapd_data *hapd = iface->bss[0];
1973 size_t j;
1974 u8 *prev_addr;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001975 int delay_apply_cfg = 0;
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001976 int res_dfs_offload = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001977
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001978 if (err)
1979 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001980
1981 wpa_printf(MSG_DEBUG, "Completing interface initialization");
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001982 if (iface->freq) {
Dmitry Shmidt051af732013-10-22 13:52:46 -07001983#ifdef NEED_AP_MLME
1984 int res;
1985#endif /* NEED_AP_MLME */
1986
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001987 wpa_printf(MSG_DEBUG, "Mode: %s Channel: %d "
1988 "Frequency: %d MHz",
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001989 hostapd_hw_mode_txt(iface->conf->hw_mode),
1990 iface->conf->channel, iface->freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001991
Dmitry Shmidt051af732013-10-22 13:52:46 -07001992#ifdef NEED_AP_MLME
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001993 /* Handle DFS only if it is not offloaded to the driver */
1994 if (!(iface->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD)) {
1995 /* Check DFS */
1996 res = hostapd_handle_dfs(iface);
1997 if (res <= 0) {
1998 if (res < 0)
1999 goto fail;
2000 return res;
2001 }
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08002002 } else {
2003 /* If DFS is offloaded to the driver */
2004 res_dfs_offload = hostapd_handle_dfs_offload(iface);
2005 if (res_dfs_offload <= 0) {
2006 if (res_dfs_offload < 0)
2007 goto fail;
2008 } else {
2009 wpa_printf(MSG_DEBUG,
2010 "Proceed with AP/channel setup");
2011 /*
2012 * If this is a DFS channel, move to completing
2013 * AP setup.
2014 */
2015 if (res_dfs_offload == 1)
2016 goto dfs_offload;
2017 /* Otherwise fall through. */
2018 }
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002019 }
Dmitry Shmidt051af732013-10-22 13:52:46 -07002020#endif /* NEED_AP_MLME */
2021
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002022#ifdef CONFIG_MESH
2023 if (iface->mconf != NULL) {
2024 wpa_printf(MSG_DEBUG,
2025 "%s: Mesh configuration will be applied while joining the mesh network",
2026 iface->bss[0]->conf->iface);
2027 delay_apply_cfg = 1;
2028 }
2029#endif /* CONFIG_MESH */
2030
2031 if (!delay_apply_cfg &&
2032 hostapd_set_freq(hapd, hapd->iconf->hw_mode, iface->freq,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002033 hapd->iconf->channel,
Hai Shalomc3565922019-10-28 11:58:20 -07002034 hapd->iconf->enable_edmg,
2035 hapd->iconf->edmg_channel,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002036 hapd->iconf->ieee80211n,
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002037 hapd->iconf->ieee80211ac,
Hai Shalom81f62d82019-07-22 12:10:00 -07002038 hapd->iconf->ieee80211ax,
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002039 hapd->iconf->secondary_channel,
Hai Shalom81f62d82019-07-22 12:10:00 -07002040 hostapd_get_oper_chwidth(hapd->iconf),
2041 hostapd_get_oper_centr_freq_seg0_idx(
2042 hapd->iconf),
2043 hostapd_get_oper_centr_freq_seg1_idx(
2044 hapd->iconf))) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002045 wpa_printf(MSG_ERROR, "Could not set channel for "
2046 "kernel driver");
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002047 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002048 }
2049 }
2050
2051 if (iface->current_mode) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002052 if (hostapd_prepare_rates(iface, iface->current_mode)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002053 wpa_printf(MSG_ERROR, "Failed to prepare rates "
2054 "table.");
2055 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
2056 HOSTAPD_LEVEL_WARNING,
2057 "Failed to prepare rates table.");
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002058 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002059 }
2060 }
2061
Hai Shalom021b0b52019-04-10 11:17:58 -07002062 if (hapd->iconf->rts_threshold >= -1 &&
2063 hostapd_set_rts(hapd, hapd->iconf->rts_threshold) &&
2064 hapd->iconf->rts_threshold >= -1) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002065 wpa_printf(MSG_ERROR, "Could not set RTS threshold for "
2066 "kernel driver");
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002067 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002068 }
2069
Hai Shalom021b0b52019-04-10 11:17:58 -07002070 if (hapd->iconf->fragm_threshold >= -1 &&
2071 hostapd_set_frag(hapd, hapd->iconf->fragm_threshold) &&
2072 hapd->iconf->fragm_threshold != -1) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002073 wpa_printf(MSG_ERROR, "Could not set fragmentation threshold "
2074 "for kernel driver");
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002075 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002076 }
2077
2078 prev_addr = hapd->own_addr;
2079
2080 for (j = 0; j < iface->num_bss; j++) {
2081 hapd = iface->bss[j];
2082 if (j)
2083 os_memcpy(hapd->own_addr, prev_addr, ETH_ALEN);
Dmitry Shmidt71757432014-06-02 13:50:35 -07002084 if (hostapd_setup_bss(hapd, j == 0)) {
Hai Shalom021b0b52019-04-10 11:17:58 -07002085 for (;;) {
Dmitry Shmidt71757432014-06-02 13:50:35 -07002086 hapd = iface->bss[j];
2087 hostapd_bss_deinit_no_free(hapd);
2088 hostapd_free_hapd_data(hapd);
Hai Shalom021b0b52019-04-10 11:17:58 -07002089 if (j == 0)
2090 break;
2091 j--;
2092 }
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002093 goto fail;
Dmitry Shmidt71757432014-06-02 13:50:35 -07002094 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002095 if (is_zero_ether_addr(hapd->conf->bssid))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002096 prev_addr = hapd->own_addr;
2097 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002098 hapd = iface->bss[0];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002099
2100 hostapd_tx_queue_params(iface);
2101
2102 ap_list_init(iface);
2103
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07002104 hostapd_set_acl(hapd);
2105
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002106 if (hostapd_driver_commit(hapd) < 0) {
2107 wpa_printf(MSG_ERROR, "%s: Failed to commit driver "
2108 "configuration", __func__);
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002109 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002110 }
2111
Jouni Malinen87fd2792011-05-16 18:35:42 +03002112 /*
2113 * WPS UPnP module can be initialized only when the "upnp_iface" is up.
2114 * If "interface" and "upnp_iface" are the same (e.g., non-bridge
2115 * mode), the interface is up only after driver_commit, so initialize
2116 * WPS after driver_commit.
2117 */
2118 for (j = 0; j < iface->num_bss; j++) {
2119 if (hostapd_init_wps_complete(iface->bss[j]))
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002120 goto fail;
Jouni Malinen87fd2792011-05-16 18:35:42 +03002121 }
2122
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08002123 if ((iface->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
2124 !res_dfs_offload) {
2125 /*
2126 * If freq is DFS, and DFS is offloaded to the driver, then wait
2127 * for CAC to complete.
2128 */
2129 wpa_printf(MSG_DEBUG, "%s: Wait for CAC to complete", __func__);
2130 return res_dfs_offload;
2131 }
2132
2133#ifdef NEED_AP_MLME
2134dfs_offload:
2135#endif /* NEED_AP_MLME */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002136
2137#ifdef CONFIG_FST
2138 if (hapd->iconf->fst_cfg.group_id[0]) {
2139 struct fst_wpa_obj iface_obj;
2140
2141 fst_hostapd_fill_iface_obj(hapd, &iface_obj);
2142 iface->fst = fst_attach(hapd->conf->iface, hapd->own_addr,
2143 &iface_obj, &hapd->iconf->fst_cfg);
2144 if (!iface->fst) {
2145 wpa_printf(MSG_ERROR, "Could not attach to FST %s",
2146 hapd->iconf->fst_cfg.group_id);
2147 goto fail;
2148 }
2149 }
2150#endif /* CONFIG_FST */
2151
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002152 hostapd_set_state(iface, HAPD_IFACE_ENABLED);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002153 hostapd_owe_update_trans(iface);
Hai Shalom81f62d82019-07-22 12:10:00 -07002154 airtime_policy_update_init(iface);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002155 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_ENABLED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002156 if (hapd->setup_complete_cb)
2157 hapd->setup_complete_cb(hapd->setup_complete_cb_ctx);
2158
2159 wpa_printf(MSG_DEBUG, "%s: Setup of interface done.",
2160 iface->bss[0]->conf->iface);
Dmitry Shmidtb96dad42013-11-05 10:07:29 -08002161 if (iface->interfaces && iface->interfaces->terminate_on_error > 0)
2162 iface->interfaces->terminate_on_error--;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002163
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002164 for (j = 0; j < iface->num_bss; j++)
Hai Shalom74f70d42019-02-11 14:42:39 -08002165 hostapd_neighbor_set_own_report(iface->bss[j]);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002166
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002167 return 0;
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002168
2169fail:
2170 wpa_printf(MSG_ERROR, "Interface initialization failed");
2171 hostapd_set_state(iface, HAPD_IFACE_DISABLED);
2172 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002173#ifdef CONFIG_FST
2174 if (iface->fst) {
2175 fst_detach(iface->fst);
2176 iface->fst = NULL;
2177 }
2178#endif /* CONFIG_FST */
Roshan Pius3a1667e2018-07-03 15:17:14 -07002179
2180 if (iface->interfaces && iface->interfaces->terminate_on_error) {
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002181 eloop_terminate();
Roshan Pius3a1667e2018-07-03 15:17:14 -07002182 } else if (hapd->setup_complete_cb) {
2183 /*
2184 * Calling hapd->setup_complete_cb directly may cause iface
2185 * deinitialization which may be accessed later by the caller.
2186 */
2187 eloop_register_timeout(0, 0,
2188 hostapd_interface_setup_failure_handler,
2189 iface, NULL);
2190 }
2191
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002192 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002193}
2194
2195
2196/**
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002197 * hostapd_setup_interface_complete - Complete interface setup
2198 *
2199 * This function is called when previous steps in the interface setup has been
2200 * completed. This can also start operations, e.g., DFS, that will require
2201 * additional processing before interface is ready to be enabled. Such
2202 * operations will call this function from eloop callbacks when finished.
2203 */
2204int hostapd_setup_interface_complete(struct hostapd_iface *iface, int err)
2205{
2206 struct hapd_interfaces *interfaces = iface->interfaces;
2207 struct hostapd_data *hapd = iface->bss[0];
2208 unsigned int i;
2209 int not_ready_in_sync_ifaces = 0;
2210
2211 if (!iface->need_to_start_in_sync)
2212 return hostapd_setup_interface_complete_sync(iface, err);
2213
2214 if (err) {
2215 wpa_printf(MSG_ERROR, "Interface initialization failed");
2216 hostapd_set_state(iface, HAPD_IFACE_DISABLED);
2217 iface->need_to_start_in_sync = 0;
2218 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
2219 if (interfaces && interfaces->terminate_on_error)
2220 eloop_terminate();
2221 return -1;
2222 }
2223
2224 if (iface->ready_to_start_in_sync) {
2225 /* Already in ready and waiting. should never happpen */
2226 return 0;
2227 }
2228
2229 for (i = 0; i < interfaces->count; i++) {
2230 if (interfaces->iface[i]->need_to_start_in_sync &&
2231 !interfaces->iface[i]->ready_to_start_in_sync)
2232 not_ready_in_sync_ifaces++;
2233 }
2234
2235 /*
2236 * Check if this is the last interface, if yes then start all the other
2237 * waiting interfaces. If not, add this interface to the waiting list.
2238 */
2239 if (not_ready_in_sync_ifaces > 1 && iface->state == HAPD_IFACE_DFS) {
2240 /*
2241 * If this interface went through CAC, do not synchronize, just
2242 * start immediately.
2243 */
2244 iface->need_to_start_in_sync = 0;
2245 wpa_printf(MSG_INFO,
2246 "%s: Finished CAC - bypass sync and start interface",
2247 iface->bss[0]->conf->iface);
2248 return hostapd_setup_interface_complete_sync(iface, err);
2249 }
2250
2251 if (not_ready_in_sync_ifaces > 1) {
2252 /* need to wait as there are other interfaces still coming up */
2253 iface->ready_to_start_in_sync = 1;
2254 wpa_printf(MSG_INFO,
2255 "%s: Interface waiting to sync with other interfaces",
2256 iface->bss[0]->conf->iface);
2257 return 0;
2258 }
2259
2260 wpa_printf(MSG_INFO,
2261 "%s: Last interface to sync - starting all interfaces",
2262 iface->bss[0]->conf->iface);
2263 iface->need_to_start_in_sync = 0;
2264 hostapd_setup_interface_complete_sync(iface, err);
2265 for (i = 0; i < interfaces->count; i++) {
2266 if (interfaces->iface[i]->need_to_start_in_sync &&
2267 interfaces->iface[i]->ready_to_start_in_sync) {
2268 hostapd_setup_interface_complete_sync(
2269 interfaces->iface[i], 0);
2270 /* Only once the interfaces are sync started */
2271 interfaces->iface[i]->need_to_start_in_sync = 0;
2272 }
2273 }
2274
2275 return 0;
2276}
2277
2278
2279/**
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002280 * hostapd_setup_interface - Setup of an interface
2281 * @iface: Pointer to interface data.
2282 * Returns: 0 on success, -1 on failure
2283 *
2284 * Initializes the driver interface, validates the configuration,
2285 * and sets driver parameters based on the configuration.
2286 * Flushes old stations, sets the channel, encryption,
2287 * beacons, and WDS links based on the configuration.
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002288 *
2289 * If interface setup requires more time, e.g., to perform HT co-ex scans, ACS,
2290 * or DFS operations, this function returns 0 before such operations have been
2291 * completed. The pending operations are registered into eloop and will be
2292 * completed from eloop callbacks. Those callbacks end up calling
2293 * hostapd_setup_interface_complete() once setup has been completed.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002294 */
2295int hostapd_setup_interface(struct hostapd_iface *iface)
2296{
2297 int ret;
2298
2299 ret = setup_interface(iface);
2300 if (ret) {
2301 wpa_printf(MSG_ERROR, "%s: Unable to setup interface.",
2302 iface->bss[0]->conf->iface);
2303 return -1;
2304 }
2305
2306 return 0;
2307}
2308
2309
2310/**
2311 * hostapd_alloc_bss_data - Allocate and initialize per-BSS data
2312 * @hapd_iface: Pointer to interface data
2313 * @conf: Pointer to per-interface configuration
2314 * @bss: Pointer to per-BSS configuration for this BSS
2315 * Returns: Pointer to allocated BSS data
2316 *
2317 * This function is used to allocate per-BSS data structure. This data will be
2318 * freed after hostapd_cleanup() is called for it during interface
2319 * deinitialization.
2320 */
2321struct hostapd_data *
2322hostapd_alloc_bss_data(struct hostapd_iface *hapd_iface,
2323 struct hostapd_config *conf,
2324 struct hostapd_bss_config *bss)
2325{
2326 struct hostapd_data *hapd;
2327
2328 hapd = os_zalloc(sizeof(*hapd));
2329 if (hapd == NULL)
2330 return NULL;
2331
2332 hapd->new_assoc_sta_cb = hostapd_new_assoc_sta;
2333 hapd->iconf = conf;
2334 hapd->conf = bss;
2335 hapd->iface = hapd_iface;
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08002336 if (conf)
2337 hapd->driver = conf->driver;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002338 hapd->ctrl_sock = -1;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08002339 dl_list_init(&hapd->ctrl_dst);
Dmitry Shmidt7d175302016-09-06 13:11:34 -07002340 dl_list_init(&hapd->nr_db);
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08002341 hapd->dhcp_sock = -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002342#ifdef CONFIG_IEEE80211R_AP
2343 dl_list_init(&hapd->l2_queue);
2344 dl_list_init(&hapd->l2_oui_queue);
2345#endif /* CONFIG_IEEE80211R_AP */
Hai Shalom021b0b52019-04-10 11:17:58 -07002346#ifdef CONFIG_SAE
2347 dl_list_init(&hapd->sae_commit_queue);
2348#endif /* CONFIG_SAE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002349
2350 return hapd;
2351}
2352
2353
Dmitry Shmidt54605472013-11-08 11:10:19 -08002354static void hostapd_bss_deinit(struct hostapd_data *hapd)
2355{
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07002356 if (!hapd)
2357 return;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002358 wpa_printf(MSG_DEBUG, "%s: deinit bss %s", __func__,
Hai Shalom021b0b52019-04-10 11:17:58 -07002359 hapd->conf ? hapd->conf->iface : "N/A");
Dmitry Shmidt71757432014-06-02 13:50:35 -07002360 hostapd_bss_deinit_no_free(hapd);
Dmitry Shmidtf73259c2015-03-17 11:00:54 -07002361 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
Hai Shalomc3565922019-10-28 11:58:20 -07002362#ifdef CONFIG_SQLITE
2363 if (hapd->rad_attr_db) {
2364 sqlite3_close(hapd->rad_attr_db);
2365 hapd->rad_attr_db = NULL;
2366 }
2367#endif /* CONFIG_SQLITE */
Dmitry Shmidt54605472013-11-08 11:10:19 -08002368 hostapd_cleanup(hapd);
2369}
2370
2371
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002372void hostapd_interface_deinit(struct hostapd_iface *iface)
2373{
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002374 int j;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002375
Dmitry Shmidt54605472013-11-08 11:10:19 -08002376 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002377 if (iface == NULL)
2378 return;
2379
Dmitry Shmidtf73259c2015-03-17 11:00:54 -07002380 hostapd_set_state(iface, HAPD_IFACE_DISABLED);
2381
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002382 eloop_cancel_timeout(channel_list_update_timeout, iface, NULL);
2383 iface->wait_channel_update = 0;
2384
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002385#ifdef CONFIG_FST
2386 if (iface->fst) {
2387 fst_detach(iface->fst);
2388 iface->fst = NULL;
2389 }
2390#endif /* CONFIG_FST */
2391
Hai Shalom021b0b52019-04-10 11:17:58 -07002392 for (j = (int) iface->num_bss - 1; j >= 0; j--) {
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07002393 if (!iface->bss)
2394 break;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002395 hostapd_bss_deinit(iface->bss[j]);
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07002396 }
Roshan Pius3a1667e2018-07-03 15:17:14 -07002397
Roshan Pius3a1667e2018-07-03 15:17:14 -07002398#ifdef NEED_AP_MLME
2399 hostapd_stop_setup_timers(iface);
2400 eloop_cancel_timeout(ap_ht2040_timeout, iface, NULL);
2401#endif /* NEED_AP_MLME */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002402}
2403
2404
2405void hostapd_interface_free(struct hostapd_iface *iface)
2406{
2407 size_t j;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002408 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
2409 for (j = 0; j < iface->num_bss; j++) {
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07002410 if (!iface->bss)
2411 break;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002412 wpa_printf(MSG_DEBUG, "%s: free hapd %p",
2413 __func__, iface->bss[j]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002414 os_free(iface->bss[j]);
Dmitry Shmidt54605472013-11-08 11:10:19 -08002415 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002416 hostapd_cleanup_iface(iface);
2417}
2418
2419
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07002420struct hostapd_iface * hostapd_alloc_iface(void)
2421{
2422 struct hostapd_iface *hapd_iface;
2423
2424 hapd_iface = os_zalloc(sizeof(*hapd_iface));
2425 if (!hapd_iface)
2426 return NULL;
2427
2428 dl_list_init(&hapd_iface->sta_seen);
2429
2430 return hapd_iface;
2431}
2432
2433
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002434/**
2435 * hostapd_init - Allocate and initialize per-interface data
2436 * @config_file: Path to the configuration file
2437 * Returns: Pointer to the allocated interface data or %NULL on failure
2438 *
2439 * This function is used to allocate main data structures for per-interface
2440 * data. The allocated data buffer will be freed by calling
2441 * hostapd_cleanup_iface().
2442 */
2443struct hostapd_iface * hostapd_init(struct hapd_interfaces *interfaces,
2444 const char *config_file)
2445{
2446 struct hostapd_iface *hapd_iface = NULL;
2447 struct hostapd_config *conf = NULL;
2448 struct hostapd_data *hapd;
2449 size_t i;
2450
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07002451 hapd_iface = hostapd_alloc_iface();
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002452 if (hapd_iface == NULL)
2453 goto fail;
2454
2455 hapd_iface->config_fname = os_strdup(config_file);
2456 if (hapd_iface->config_fname == NULL)
2457 goto fail;
2458
2459 conf = interfaces->config_read_cb(hapd_iface->config_fname);
2460 if (conf == NULL)
2461 goto fail;
2462 hapd_iface->conf = conf;
2463
2464 hapd_iface->num_bss = conf->num_bss;
2465 hapd_iface->bss = os_calloc(conf->num_bss,
2466 sizeof(struct hostapd_data *));
2467 if (hapd_iface->bss == NULL)
2468 goto fail;
2469
2470 for (i = 0; i < conf->num_bss; i++) {
2471 hapd = hapd_iface->bss[i] =
2472 hostapd_alloc_bss_data(hapd_iface, conf,
2473 conf->bss[i]);
2474 if (hapd == NULL)
2475 goto fail;
2476 hapd->msg_ctx = hapd;
2477 }
2478
2479 return hapd_iface;
2480
2481fail:
2482 wpa_printf(MSG_ERROR, "Failed to set up interface with %s",
2483 config_file);
2484 if (conf)
2485 hostapd_config_free(conf);
2486 if (hapd_iface) {
2487 os_free(hapd_iface->config_fname);
2488 os_free(hapd_iface->bss);
Dmitry Shmidt54605472013-11-08 11:10:19 -08002489 wpa_printf(MSG_DEBUG, "%s: free iface %p",
2490 __func__, hapd_iface);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002491 os_free(hapd_iface);
2492 }
2493 return NULL;
2494}
2495
2496
2497static int ifname_in_use(struct hapd_interfaces *interfaces, const char *ifname)
2498{
2499 size_t i, j;
2500
2501 for (i = 0; i < interfaces->count; i++) {
2502 struct hostapd_iface *iface = interfaces->iface[i];
2503 for (j = 0; j < iface->num_bss; j++) {
2504 struct hostapd_data *hapd = iface->bss[j];
2505 if (os_strcmp(ifname, hapd->conf->iface) == 0)
2506 return 1;
2507 }
2508 }
2509
2510 return 0;
2511}
2512
2513
2514/**
2515 * hostapd_interface_init_bss - Read configuration file and init BSS data
2516 *
2517 * This function is used to parse configuration file for a BSS. This BSS is
2518 * added to an existing interface sharing the same radio (if any) or a new
2519 * interface is created if this is the first interface on a radio. This
2520 * allocate memory for the BSS. No actual driver operations are started.
2521 *
2522 * This is similar to hostapd_interface_init(), but for a case where the
2523 * configuration is used to add a single BSS instead of all BSSes for a radio.
2524 */
2525struct hostapd_iface *
2526hostapd_interface_init_bss(struct hapd_interfaces *interfaces, const char *phy,
2527 const char *config_fname, int debug)
2528{
2529 struct hostapd_iface *new_iface = NULL, *iface = NULL;
2530 struct hostapd_data *hapd;
2531 int k;
2532 size_t i, bss_idx;
2533
2534 if (!phy || !*phy)
2535 return NULL;
2536
2537 for (i = 0; i < interfaces->count; i++) {
2538 if (os_strcmp(interfaces->iface[i]->phy, phy) == 0) {
2539 iface = interfaces->iface[i];
2540 break;
2541 }
2542 }
2543
2544 wpa_printf(MSG_INFO, "Configuration file: %s (phy %s)%s",
2545 config_fname, phy, iface ? "" : " --> new PHY");
2546 if (iface) {
2547 struct hostapd_config *conf;
2548 struct hostapd_bss_config **tmp_conf;
2549 struct hostapd_data **tmp_bss;
2550 struct hostapd_bss_config *bss;
2551 const char *ifname;
2552
2553 /* Add new BSS to existing iface */
2554 conf = interfaces->config_read_cb(config_fname);
2555 if (conf == NULL)
2556 return NULL;
2557 if (conf->num_bss > 1) {
2558 wpa_printf(MSG_ERROR, "Multiple BSSes specified in BSS-config");
2559 hostapd_config_free(conf);
2560 return NULL;
2561 }
2562
2563 ifname = conf->bss[0]->iface;
2564 if (ifname[0] != '\0' && ifname_in_use(interfaces, ifname)) {
2565 wpa_printf(MSG_ERROR,
2566 "Interface name %s already in use", ifname);
2567 hostapd_config_free(conf);
2568 return NULL;
2569 }
2570
2571 tmp_conf = os_realloc_array(
2572 iface->conf->bss, iface->conf->num_bss + 1,
2573 sizeof(struct hostapd_bss_config *));
2574 tmp_bss = os_realloc_array(iface->bss, iface->num_bss + 1,
2575 sizeof(struct hostapd_data *));
2576 if (tmp_bss)
2577 iface->bss = tmp_bss;
2578 if (tmp_conf) {
2579 iface->conf->bss = tmp_conf;
2580 iface->conf->last_bss = tmp_conf[0];
2581 }
2582 if (tmp_bss == NULL || tmp_conf == NULL) {
2583 hostapd_config_free(conf);
2584 return NULL;
2585 }
2586 bss = iface->conf->bss[iface->conf->num_bss] = conf->bss[0];
2587 iface->conf->num_bss++;
2588
2589 hapd = hostapd_alloc_bss_data(iface, iface->conf, bss);
2590 if (hapd == NULL) {
2591 iface->conf->num_bss--;
2592 hostapd_config_free(conf);
2593 return NULL;
2594 }
2595 iface->conf->last_bss = bss;
2596 iface->bss[iface->num_bss] = hapd;
2597 hapd->msg_ctx = hapd;
2598
2599 bss_idx = iface->num_bss++;
2600 conf->num_bss--;
2601 conf->bss[0] = NULL;
2602 hostapd_config_free(conf);
2603 } else {
2604 /* Add a new iface with the first BSS */
2605 new_iface = iface = hostapd_init(interfaces, config_fname);
2606 if (!iface)
2607 return NULL;
2608 os_strlcpy(iface->phy, phy, sizeof(iface->phy));
2609 iface->interfaces = interfaces;
2610 bss_idx = 0;
2611 }
2612
2613 for (k = 0; k < debug; k++) {
2614 if (iface->bss[bss_idx]->conf->logger_stdout_level > 0)
2615 iface->bss[bss_idx]->conf->logger_stdout_level--;
2616 }
2617
2618 if (iface->conf->bss[bss_idx]->iface[0] == '\0' &&
2619 !hostapd_drv_none(iface->bss[bss_idx])) {
2620 wpa_printf(MSG_ERROR, "Interface name not specified in %s",
2621 config_fname);
2622 if (new_iface)
2623 hostapd_interface_deinit_free(new_iface);
2624 return NULL;
2625 }
2626
2627 return iface;
2628}
2629
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002630
2631void hostapd_interface_deinit_free(struct hostapd_iface *iface)
2632{
2633 const struct wpa_driver_ops *driver;
2634 void *drv_priv;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002635
2636 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002637 if (iface == NULL)
2638 return;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002639 wpa_printf(MSG_DEBUG, "%s: num_bss=%u conf->num_bss=%u",
2640 __func__, (unsigned int) iface->num_bss,
2641 (unsigned int) iface->conf->num_bss);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002642 driver = iface->bss[0]->driver;
2643 drv_priv = iface->bss[0]->drv_priv;
2644 hostapd_interface_deinit(iface);
Dmitry Shmidt54605472013-11-08 11:10:19 -08002645 wpa_printf(MSG_DEBUG, "%s: driver=%p drv_priv=%p -> hapd_deinit",
2646 __func__, driver, drv_priv);
Dmitry Shmidt71757432014-06-02 13:50:35 -07002647 if (driver && driver->hapd_deinit && drv_priv) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002648 driver->hapd_deinit(drv_priv);
Dmitry Shmidt71757432014-06-02 13:50:35 -07002649 iface->bss[0]->drv_priv = NULL;
2650 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002651 hostapd_interface_free(iface);
2652}
2653
2654
Dmitry Shmidt15907092014-03-25 10:42:57 -07002655static void hostapd_deinit_driver(const struct wpa_driver_ops *driver,
2656 void *drv_priv,
2657 struct hostapd_iface *hapd_iface)
2658{
2659 size_t j;
2660
2661 wpa_printf(MSG_DEBUG, "%s: driver=%p drv_priv=%p -> hapd_deinit",
2662 __func__, driver, drv_priv);
2663 if (driver && driver->hapd_deinit && drv_priv) {
2664 driver->hapd_deinit(drv_priv);
2665 for (j = 0; j < hapd_iface->num_bss; j++) {
2666 wpa_printf(MSG_DEBUG, "%s:bss[%d]->drv_priv=%p",
2667 __func__, (int) j,
2668 hapd_iface->bss[j]->drv_priv);
Hai Shalom1dc4d202019-04-29 16:22:27 -07002669 if (hapd_iface->bss[j]->drv_priv == drv_priv) {
Dmitry Shmidt15907092014-03-25 10:42:57 -07002670 hapd_iface->bss[j]->drv_priv = NULL;
Hai Shalom1dc4d202019-04-29 16:22:27 -07002671 hapd_iface->extended_capa = NULL;
2672 hapd_iface->extended_capa_mask = NULL;
2673 hapd_iface->extended_capa_len = 0;
2674 }
Dmitry Shmidt15907092014-03-25 10:42:57 -07002675 }
2676 }
2677}
2678
2679
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002680int hostapd_enable_iface(struct hostapd_iface *hapd_iface)
2681{
Dmitry Shmidt71757432014-06-02 13:50:35 -07002682 size_t j;
2683
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002684 if (hapd_iface->bss[0]->drv_priv != NULL) {
2685 wpa_printf(MSG_ERROR, "Interface %s already enabled",
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002686 hapd_iface->conf->bss[0]->iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002687 return -1;
2688 }
2689
2690 wpa_printf(MSG_DEBUG, "Enable interface %s",
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002691 hapd_iface->conf->bss[0]->iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002692
Dmitry Shmidt71757432014-06-02 13:50:35 -07002693 for (j = 0; j < hapd_iface->num_bss; j++)
2694 hostapd_set_security_params(hapd_iface->conf->bss[j], 1);
Dmitry Shmidt344abd32014-01-14 13:17:00 -08002695 if (hostapd_config_check(hapd_iface->conf, 1) < 0) {
2696 wpa_printf(MSG_INFO, "Invalid configuration - cannot enable");
2697 return -1;
2698 }
2699
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002700 if (hapd_iface->interfaces == NULL ||
2701 hapd_iface->interfaces->driver_init == NULL ||
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002702 hapd_iface->interfaces->driver_init(hapd_iface))
2703 return -1;
2704
2705 if (hostapd_setup_interface(hapd_iface)) {
Dmitry Shmidt15907092014-03-25 10:42:57 -07002706 hostapd_deinit_driver(hapd_iface->bss[0]->driver,
2707 hapd_iface->bss[0]->drv_priv,
2708 hapd_iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002709 return -1;
2710 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002711
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002712 return 0;
2713}
2714
2715
2716int hostapd_reload_iface(struct hostapd_iface *hapd_iface)
2717{
2718 size_t j;
2719
2720 wpa_printf(MSG_DEBUG, "Reload interface %s",
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002721 hapd_iface->conf->bss[0]->iface);
2722 for (j = 0; j < hapd_iface->num_bss; j++)
Dmitry Shmidt71757432014-06-02 13:50:35 -07002723 hostapd_set_security_params(hapd_iface->conf->bss[j], 1);
Dmitry Shmidt344abd32014-01-14 13:17:00 -08002724 if (hostapd_config_check(hapd_iface->conf, 1) < 0) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002725 wpa_printf(MSG_ERROR, "Updated configuration is invalid");
2726 return -1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002727 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002728 hostapd_clear_old(hapd_iface);
2729 for (j = 0; j < hapd_iface->num_bss; j++)
2730 hostapd_reload_bss(hapd_iface->bss[j]);
2731
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002732 return 0;
2733}
2734
2735
2736int hostapd_disable_iface(struct hostapd_iface *hapd_iface)
2737{
2738 size_t j;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002739 const struct wpa_driver_ops *driver;
2740 void *drv_priv;
2741
2742 if (hapd_iface == NULL)
2743 return -1;
Dmitry Shmidt71757432014-06-02 13:50:35 -07002744
2745 if (hapd_iface->bss[0]->drv_priv == NULL) {
2746 wpa_printf(MSG_INFO, "Interface %s already disabled",
2747 hapd_iface->conf->bss[0]->iface);
2748 return -1;
2749 }
2750
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002751 wpa_msg(hapd_iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002752 driver = hapd_iface->bss[0]->driver;
2753 drv_priv = hapd_iface->bss[0]->drv_priv;
2754
Dmitry Shmidta38abf92014-03-06 13:38:44 -08002755 hapd_iface->driver_ap_teardown =
2756 !!(hapd_iface->drv_flags &
2757 WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
2758
Hai Shalom39ba6fc2019-01-22 12:40:38 -08002759#ifdef NEED_AP_MLME
2760 for (j = 0; j < hapd_iface->num_bss; j++)
2761 hostapd_cleanup_cs_params(hapd_iface->bss[j]);
2762#endif /* NEED_AP_MLME */
2763
Dmitry Shmidta38abf92014-03-06 13:38:44 -08002764 /* same as hostapd_interface_deinit without deinitializing ctrl-iface */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002765 for (j = 0; j < hapd_iface->num_bss; j++) {
2766 struct hostapd_data *hapd = hapd_iface->bss[j];
Dmitry Shmidt71757432014-06-02 13:50:35 -07002767 hostapd_bss_deinit_no_free(hapd);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002768 hostapd_free_hapd_data(hapd);
2769 }
2770
Dmitry Shmidt15907092014-03-25 10:42:57 -07002771 hostapd_deinit_driver(driver, drv_priv, hapd_iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002772
2773 /* From hostapd_cleanup_iface: These were initialized in
2774 * hostapd_setup_interface and hostapd_setup_interface_complete
2775 */
2776 hostapd_cleanup_iface_partial(hapd_iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002777
Dmitry Shmidt56052862013-10-04 10:23:25 -07002778 wpa_printf(MSG_DEBUG, "Interface %s disabled",
2779 hapd_iface->bss[0]->conf->iface);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002780 hostapd_set_state(hapd_iface, HAPD_IFACE_DISABLED);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002781 return 0;
2782}
2783
2784
2785static struct hostapd_iface *
2786hostapd_iface_alloc(struct hapd_interfaces *interfaces)
2787{
2788 struct hostapd_iface **iface, *hapd_iface;
2789
2790 iface = os_realloc_array(interfaces->iface, interfaces->count + 1,
2791 sizeof(struct hostapd_iface *));
2792 if (iface == NULL)
2793 return NULL;
2794 interfaces->iface = iface;
2795 hapd_iface = interfaces->iface[interfaces->count] =
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07002796 hostapd_alloc_iface();
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002797 if (hapd_iface == NULL) {
2798 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory for "
2799 "the interface", __func__);
2800 return NULL;
2801 }
2802 interfaces->count++;
2803 hapd_iface->interfaces = interfaces;
2804
2805 return hapd_iface;
2806}
2807
2808
2809static struct hostapd_config *
2810hostapd_config_alloc(struct hapd_interfaces *interfaces, const char *ifname,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002811 const char *ctrl_iface, const char *driver)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002812{
2813 struct hostapd_bss_config *bss;
2814 struct hostapd_config *conf;
2815
2816 /* Allocates memory for bss and conf */
2817 conf = hostapd_config_defaults();
2818 if (conf == NULL) {
2819 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory for "
2820 "configuration", __func__);
Hai Shalom74f70d42019-02-11 14:42:39 -08002821 return NULL;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002822 }
2823
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002824 if (driver) {
2825 int j;
2826
2827 for (j = 0; wpa_drivers[j]; j++) {
2828 if (os_strcmp(driver, wpa_drivers[j]->name) == 0) {
2829 conf->driver = wpa_drivers[j];
2830 goto skip;
2831 }
2832 }
2833
2834 wpa_printf(MSG_ERROR,
2835 "Invalid/unknown driver '%s' - registering the default driver",
2836 driver);
2837 }
2838
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002839 conf->driver = wpa_drivers[0];
2840 if (conf->driver == NULL) {
2841 wpa_printf(MSG_ERROR, "No driver wrappers registered!");
2842 hostapd_config_free(conf);
2843 return NULL;
2844 }
2845
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002846skip:
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002847 bss = conf->last_bss = conf->bss[0];
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002848
2849 os_strlcpy(bss->iface, ifname, sizeof(bss->iface));
2850 bss->ctrl_interface = os_strdup(ctrl_iface);
2851 if (bss->ctrl_interface == NULL) {
2852 hostapd_config_free(conf);
2853 return NULL;
2854 }
2855
2856 /* Reading configuration file skipped, will be done in SET!
2857 * From reading the configuration till the end has to be done in
2858 * SET
2859 */
2860 return conf;
2861}
2862
2863
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002864static int hostapd_data_alloc(struct hostapd_iface *hapd_iface,
2865 struct hostapd_config *conf)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002866{
2867 size_t i;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002868 struct hostapd_data *hapd;
2869
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002870 hapd_iface->bss = os_calloc(conf->num_bss,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002871 sizeof(struct hostapd_data *));
2872 if (hapd_iface->bss == NULL)
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002873 return -1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002874
2875 for (i = 0; i < conf->num_bss; i++) {
2876 hapd = hapd_iface->bss[i] =
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002877 hostapd_alloc_bss_data(hapd_iface, conf, conf->bss[i]);
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002878 if (hapd == NULL) {
2879 while (i > 0) {
2880 i--;
2881 os_free(hapd_iface->bss[i]);
2882 hapd_iface->bss[i] = NULL;
2883 }
2884 os_free(hapd_iface->bss);
2885 hapd_iface->bss = NULL;
2886 return -1;
2887 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002888 hapd->msg_ctx = hapd;
2889 }
2890
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002891 hapd_iface->conf = conf;
2892 hapd_iface->num_bss = conf->num_bss;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002893
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002894 return 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002895}
2896
2897
2898int hostapd_add_iface(struct hapd_interfaces *interfaces, char *buf)
2899{
2900 struct hostapd_config *conf = NULL;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002901 struct hostapd_iface *hapd_iface = NULL, *new_iface = NULL;
2902 struct hostapd_data *hapd;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002903 char *ptr;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002904 size_t i, j;
2905 const char *conf_file = NULL, *phy_name = NULL;
2906
2907 if (os_strncmp(buf, "bss_config=", 11) == 0) {
2908 char *pos;
2909 phy_name = buf + 11;
2910 pos = os_strchr(phy_name, ':');
2911 if (!pos)
2912 return -1;
2913 *pos++ = '\0';
2914 conf_file = pos;
2915 if (!os_strlen(conf_file))
2916 return -1;
2917
2918 hapd_iface = hostapd_interface_init_bss(interfaces, phy_name,
2919 conf_file, 0);
2920 if (!hapd_iface)
2921 return -1;
2922 for (j = 0; j < interfaces->count; j++) {
2923 if (interfaces->iface[j] == hapd_iface)
2924 break;
2925 }
2926 if (j == interfaces->count) {
2927 struct hostapd_iface **tmp;
2928 tmp = os_realloc_array(interfaces->iface,
2929 interfaces->count + 1,
2930 sizeof(struct hostapd_iface *));
2931 if (!tmp) {
2932 hostapd_interface_deinit_free(hapd_iface);
2933 return -1;
2934 }
2935 interfaces->iface = tmp;
2936 interfaces->iface[interfaces->count++] = hapd_iface;
2937 new_iface = hapd_iface;
2938 }
2939
2940 if (new_iface) {
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002941 if (interfaces->driver_init(hapd_iface))
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002942 goto fail;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002943
2944 if (hostapd_setup_interface(hapd_iface)) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002945 hostapd_deinit_driver(
2946 hapd_iface->bss[0]->driver,
2947 hapd_iface->bss[0]->drv_priv,
2948 hapd_iface);
2949 goto fail;
2950 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002951 } else {
2952 /* Assign new BSS with bss[0]'s driver info */
2953 hapd = hapd_iface->bss[hapd_iface->num_bss - 1];
2954 hapd->driver = hapd_iface->bss[0]->driver;
2955 hapd->drv_priv = hapd_iface->bss[0]->drv_priv;
2956 os_memcpy(hapd->own_addr, hapd_iface->bss[0]->own_addr,
2957 ETH_ALEN);
2958
2959 if (start_ctrl_iface_bss(hapd) < 0 ||
Dmitry Shmidt54605472013-11-08 11:10:19 -08002960 (hapd_iface->state == HAPD_IFACE_ENABLED &&
2961 hostapd_setup_bss(hapd, -1))) {
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002962 hostapd_cleanup(hapd);
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08002963 hapd_iface->bss[hapd_iface->num_bss - 1] = NULL;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002964 hapd_iface->conf->num_bss--;
2965 hapd_iface->num_bss--;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002966 wpa_printf(MSG_DEBUG, "%s: free hapd %p %s",
2967 __func__, hapd, hapd->conf->iface);
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002968 hostapd_config_free_bss(hapd->conf);
2969 hapd->conf = NULL;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002970 os_free(hapd);
2971 return -1;
2972 }
2973 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002974 hostapd_owe_update_trans(hapd_iface);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002975 return 0;
2976 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002977
2978 ptr = os_strchr(buf, ' ');
2979 if (ptr == NULL)
2980 return -1;
2981 *ptr++ = '\0';
2982
Dmitry Shmidt56052862013-10-04 10:23:25 -07002983 if (os_strncmp(ptr, "config=", 7) == 0)
2984 conf_file = ptr + 7;
2985
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002986 for (i = 0; i < interfaces->count; i++) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002987 if (!os_strcmp(interfaces->iface[i]->conf->bss[0]->iface,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002988 buf)) {
2989 wpa_printf(MSG_INFO, "Cannot add interface - it "
2990 "already exists");
2991 return -1;
2992 }
2993 }
2994
2995 hapd_iface = hostapd_iface_alloc(interfaces);
2996 if (hapd_iface == NULL) {
2997 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
2998 "for interface", __func__);
2999 goto fail;
3000 }
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003001 new_iface = hapd_iface;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003002
Dmitry Shmidt56052862013-10-04 10:23:25 -07003003 if (conf_file && interfaces->config_read_cb) {
3004 conf = interfaces->config_read_cb(conf_file);
3005 if (conf && conf->bss)
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003006 os_strlcpy(conf->bss[0]->iface, buf,
3007 sizeof(conf->bss[0]->iface));
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003008 } else {
3009 char *driver = os_strchr(ptr, ' ');
3010
3011 if (driver)
3012 *driver++ = '\0';
3013 conf = hostapd_config_alloc(interfaces, buf, ptr, driver);
3014 }
3015
Dmitry Shmidt56052862013-10-04 10:23:25 -07003016 if (conf == NULL || conf->bss == NULL) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003017 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
3018 "for configuration", __func__);
3019 goto fail;
3020 }
3021
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003022 if (hostapd_data_alloc(hapd_iface, conf) < 0) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003023 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
3024 "for hostapd", __func__);
3025 goto fail;
3026 }
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003027 conf = NULL;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003028
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003029 if (start_ctrl_iface(hapd_iface) < 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003030 goto fail;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003031
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003032 wpa_printf(MSG_INFO, "Add interface '%s'",
3033 hapd_iface->conf->bss[0]->iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003034
3035 return 0;
3036
3037fail:
3038 if (conf)
3039 hostapd_config_free(conf);
3040 if (hapd_iface) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003041 if (hapd_iface->bss) {
Dmitry Shmidt54605472013-11-08 11:10:19 -08003042 for (i = 0; i < hapd_iface->num_bss; i++) {
3043 hapd = hapd_iface->bss[i];
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08003044 if (!hapd)
3045 continue;
3046 if (hapd_iface->interfaces &&
Dmitry Shmidt54605472013-11-08 11:10:19 -08003047 hapd_iface->interfaces->ctrl_iface_deinit)
3048 hapd_iface->interfaces->
3049 ctrl_iface_deinit(hapd);
3050 wpa_printf(MSG_DEBUG, "%s: free hapd %p (%s)",
3051 __func__, hapd_iface->bss[i],
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08003052 hapd->conf->iface);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003053 hostapd_cleanup(hapd);
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08003054 os_free(hapd);
3055 hapd_iface->bss[i] = NULL;
Dmitry Shmidt54605472013-11-08 11:10:19 -08003056 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003057 os_free(hapd_iface->bss);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003058 hapd_iface->bss = NULL;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003059 }
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003060 if (new_iface) {
3061 interfaces->count--;
3062 interfaces->iface[interfaces->count] = NULL;
3063 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003064 hostapd_cleanup_iface(hapd_iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003065 }
3066 return -1;
3067}
3068
3069
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003070static int hostapd_remove_bss(struct hostapd_iface *iface, unsigned int idx)
3071{
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003072 size_t i;
3073
Dmitry Shmidt54605472013-11-08 11:10:19 -08003074 wpa_printf(MSG_INFO, "Remove BSS '%s'", iface->conf->bss[idx]->iface);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003075
Dmitry Shmidt54605472013-11-08 11:10:19 -08003076 /* Remove hostapd_data only if it has already been initialized */
3077 if (idx < iface->num_bss) {
3078 struct hostapd_data *hapd = iface->bss[idx];
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003079
Dmitry Shmidt54605472013-11-08 11:10:19 -08003080 hostapd_bss_deinit(hapd);
3081 wpa_printf(MSG_DEBUG, "%s: free hapd %p (%s)",
3082 __func__, hapd, hapd->conf->iface);
3083 hostapd_config_free_bss(hapd->conf);
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003084 hapd->conf = NULL;
Dmitry Shmidt54605472013-11-08 11:10:19 -08003085 os_free(hapd);
3086
3087 iface->num_bss--;
3088
3089 for (i = idx; i < iface->num_bss; i++)
3090 iface->bss[i] = iface->bss[i + 1];
3091 } else {
3092 hostapd_config_free_bss(iface->conf->bss[idx]);
3093 iface->conf->bss[idx] = NULL;
3094 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003095
3096 iface->conf->num_bss--;
Dmitry Shmidt54605472013-11-08 11:10:19 -08003097 for (i = idx; i < iface->conf->num_bss; i++)
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003098 iface->conf->bss[i] = iface->conf->bss[i + 1];
3099
3100 return 0;
3101}
3102
3103
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003104int hostapd_remove_iface(struct hapd_interfaces *interfaces, char *buf)
3105{
3106 struct hostapd_iface *hapd_iface;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003107 size_t i, j, k = 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003108
3109 for (i = 0; i < interfaces->count; i++) {
3110 hapd_iface = interfaces->iface[i];
3111 if (hapd_iface == NULL)
3112 return -1;
Dmitry Shmidt54605472013-11-08 11:10:19 -08003113 if (!os_strcmp(hapd_iface->conf->bss[0]->iface, buf)) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003114 wpa_printf(MSG_INFO, "Remove interface '%s'", buf);
Dmitry Shmidta38abf92014-03-06 13:38:44 -08003115 hapd_iface->driver_ap_teardown =
3116 !!(hapd_iface->drv_flags &
3117 WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
3118
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003119 hostapd_interface_deinit_free(hapd_iface);
3120 k = i;
3121 while (k < (interfaces->count - 1)) {
3122 interfaces->iface[k] =
3123 interfaces->iface[k + 1];
3124 k++;
3125 }
3126 interfaces->count--;
3127 return 0;
3128 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003129
3130 for (j = 0; j < hapd_iface->conf->num_bss; j++) {
Dmitry Shmidta38abf92014-03-06 13:38:44 -08003131 if (!os_strcmp(hapd_iface->conf->bss[j]->iface, buf)) {
3132 hapd_iface->driver_ap_teardown =
3133 !(hapd_iface->drv_flags &
3134 WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003135 return hostapd_remove_bss(hapd_iface, j);
Dmitry Shmidta38abf92014-03-06 13:38:44 -08003136 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003137 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003138 }
3139 return -1;
3140}
3141
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003142
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003143/**
3144 * hostapd_new_assoc_sta - Notify that a new station associated with the AP
3145 * @hapd: Pointer to BSS data
3146 * @sta: Pointer to the associated STA data
3147 * @reassoc: 1 to indicate this was a re-association; 0 = first association
3148 *
3149 * This function will be called whenever a station associates with the AP. It
3150 * can be called from ieee802_11.c for drivers that export MLME to hostapd and
3151 * from drv_callbacks.c based on driver events for drivers that take care of
3152 * management frames (IEEE 802.11 authentication and association) internally.
3153 */
3154void hostapd_new_assoc_sta(struct hostapd_data *hapd, struct sta_info *sta,
3155 int reassoc)
3156{
3157 if (hapd->tkip_countermeasures) {
3158 hostapd_drv_sta_deauth(hapd, sta->addr,
3159 WLAN_REASON_MICHAEL_MIC_FAILURE);
3160 return;
3161 }
3162
3163 hostapd_prune_associations(hapd, sta->addr);
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -08003164 ap_sta_clear_disconnect_timeouts(hapd, sta);
Hai Shalom899fcc72020-10-19 14:38:18 -07003165 sta->post_csa_sa_query = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003166
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003167#ifdef CONFIG_P2P
3168 if (sta->p2p_ie == NULL && !sta->no_p2p_set) {
3169 sta->no_p2p_set = 1;
3170 hapd->num_sta_no_p2p++;
3171 if (hapd->num_sta_no_p2p == 1)
3172 hostapd_p2p_non_p2p_sta_connected(hapd);
3173 }
3174#endif /* CONFIG_P2P */
3175
Hai Shalom81f62d82019-07-22 12:10:00 -07003176 airtime_policy_new_sta(hapd, sta);
3177
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003178 /* Start accounting here, if IEEE 802.1X and WPA are not used.
3179 * IEEE 802.1X/WPA code will start accounting after the station has
3180 * been authorized. */
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -08003181 if (!hapd->conf->ieee802_1x && !hapd->conf->wpa && !hapd->conf->osen) {
3182 ap_sta_set_authorized(hapd, sta, 1);
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08003183 os_get_reltime(&sta->connected_time);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003184 accounting_sta_start(hapd, sta);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003185 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003186
3187 /* Start IEEE 802.1X authentication process for new stations */
3188 ieee802_1x_new_station(hapd, sta);
3189 if (reassoc) {
3190 if (sta->auth_alg != WLAN_AUTH_FT &&
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003191 sta->auth_alg != WLAN_AUTH_FILS_SK &&
3192 sta->auth_alg != WLAN_AUTH_FILS_SK_PFS &&
3193 sta->auth_alg != WLAN_AUTH_FILS_PK &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003194 !(sta->flags & (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS)))
3195 wpa_auth_sm_event(sta->wpa_sm, WPA_REAUTH);
3196 } else
3197 wpa_auth_sta_associated(hapd->wpa_auth, sta->wpa_sm);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003198
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08003199 if (hapd->iface->drv_flags & WPA_DRIVER_FLAGS_WIRED) {
3200 if (eloop_cancel_timeout(ap_handle_timer, hapd, sta) > 0) {
3201 wpa_printf(MSG_DEBUG,
3202 "%s: %s: canceled wired ap_handle_timer timeout for "
3203 MACSTR,
3204 hapd->conf->iface, __func__,
3205 MAC2STR(sta->addr));
3206 }
3207 } else if (!(hapd->iface->drv_flags &
3208 WPA_DRIVER_FLAGS_INACTIVITY_TIMER)) {
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -08003209 wpa_printf(MSG_DEBUG,
3210 "%s: %s: reschedule ap_handle_timer timeout for "
3211 MACSTR " (%d seconds - ap_max_inactivity)",
3212 hapd->conf->iface, __func__, MAC2STR(sta->addr),
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08003213 hapd->conf->ap_max_inactivity);
3214 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
3215 eloop_register_timeout(hapd->conf->ap_max_inactivity, 0,
3216 ap_handle_timer, hapd, sta);
3217 }
Hai Shalom81f62d82019-07-22 12:10:00 -07003218
3219#ifdef CONFIG_MACSEC
3220 if (hapd->conf->wpa_key_mgmt == WPA_KEY_MGMT_NONE &&
3221 hapd->conf->mka_psk_set)
3222 ieee802_1x_create_preshared_mka_hapd(hapd, sta);
3223 else
3224 ieee802_1x_alloc_kay_sm_hapd(hapd, sta);
3225#endif /* CONFIG_MACSEC */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003226}
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003227
3228
3229const char * hostapd_state_text(enum hostapd_iface_state s)
3230{
3231 switch (s) {
3232 case HAPD_IFACE_UNINITIALIZED:
3233 return "UNINITIALIZED";
3234 case HAPD_IFACE_DISABLED:
3235 return "DISABLED";
3236 case HAPD_IFACE_COUNTRY_UPDATE:
3237 return "COUNTRY_UPDATE";
3238 case HAPD_IFACE_ACS:
3239 return "ACS";
3240 case HAPD_IFACE_HT_SCAN:
3241 return "HT_SCAN";
3242 case HAPD_IFACE_DFS:
3243 return "DFS";
3244 case HAPD_IFACE_ENABLED:
3245 return "ENABLED";
3246 }
3247
3248 return "UNKNOWN";
3249}
3250
3251
3252void hostapd_set_state(struct hostapd_iface *iface, enum hostapd_iface_state s)
3253{
3254 wpa_printf(MSG_INFO, "%s: interface state %s->%s",
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07003255 iface->conf ? iface->conf->bss[0]->iface : "N/A",
3256 hostapd_state_text(iface->state), hostapd_state_text(s));
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003257 iface->state = s;
3258}
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003259
3260
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003261int hostapd_csa_in_progress(struct hostapd_iface *iface)
3262{
3263 unsigned int i;
3264
3265 for (i = 0; i < iface->num_bss; i++)
3266 if (iface->bss[i]->csa_in_progress)
3267 return 1;
3268 return 0;
3269}
3270
3271
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003272#ifdef NEED_AP_MLME
3273
3274static void free_beacon_data(struct beacon_data *beacon)
3275{
3276 os_free(beacon->head);
3277 beacon->head = NULL;
3278 os_free(beacon->tail);
3279 beacon->tail = NULL;
3280 os_free(beacon->probe_resp);
3281 beacon->probe_resp = NULL;
3282 os_free(beacon->beacon_ies);
3283 beacon->beacon_ies = NULL;
3284 os_free(beacon->proberesp_ies);
3285 beacon->proberesp_ies = NULL;
3286 os_free(beacon->assocresp_ies);
3287 beacon->assocresp_ies = NULL;
3288}
3289
3290
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003291static int hostapd_build_beacon_data(struct hostapd_data *hapd,
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003292 struct beacon_data *beacon)
3293{
3294 struct wpabuf *beacon_extra, *proberesp_extra, *assocresp_extra;
3295 struct wpa_driver_ap_params params;
3296 int ret;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003297
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08003298 os_memset(beacon, 0, sizeof(*beacon));
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003299 ret = ieee802_11_build_ap_params(hapd, &params);
3300 if (ret < 0)
3301 return ret;
3302
3303 ret = hostapd_build_ap_extra_ies(hapd, &beacon_extra,
3304 &proberesp_extra,
3305 &assocresp_extra);
3306 if (ret)
3307 goto free_ap_params;
3308
3309 ret = -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003310 beacon->head = os_memdup(params.head, params.head_len);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003311 if (!beacon->head)
3312 goto free_ap_extra_ies;
3313
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003314 beacon->head_len = params.head_len;
3315
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003316 beacon->tail = os_memdup(params.tail, params.tail_len);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003317 if (!beacon->tail)
3318 goto free_beacon;
3319
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003320 beacon->tail_len = params.tail_len;
3321
3322 if (params.proberesp != NULL) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003323 beacon->probe_resp = os_memdup(params.proberesp,
3324 params.proberesp_len);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003325 if (!beacon->probe_resp)
3326 goto free_beacon;
3327
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003328 beacon->probe_resp_len = params.proberesp_len;
3329 }
3330
3331 /* copy the extra ies */
3332 if (beacon_extra) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003333 beacon->beacon_ies = os_memdup(beacon_extra->buf,
3334 wpabuf_len(beacon_extra));
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003335 if (!beacon->beacon_ies)
3336 goto free_beacon;
3337
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003338 beacon->beacon_ies_len = wpabuf_len(beacon_extra);
3339 }
3340
3341 if (proberesp_extra) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003342 beacon->proberesp_ies = os_memdup(proberesp_extra->buf,
3343 wpabuf_len(proberesp_extra));
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003344 if (!beacon->proberesp_ies)
3345 goto free_beacon;
3346
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003347 beacon->proberesp_ies_len = wpabuf_len(proberesp_extra);
3348 }
3349
3350 if (assocresp_extra) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003351 beacon->assocresp_ies = os_memdup(assocresp_extra->buf,
3352 wpabuf_len(assocresp_extra));
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003353 if (!beacon->assocresp_ies)
3354 goto free_beacon;
3355
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003356 beacon->assocresp_ies_len = wpabuf_len(assocresp_extra);
3357 }
3358
3359 ret = 0;
3360free_beacon:
3361 /* if the function fails, the caller should not free beacon data */
3362 if (ret)
3363 free_beacon_data(beacon);
3364
3365free_ap_extra_ies:
3366 hostapd_free_ap_extra_ies(hapd, beacon_extra, proberesp_extra,
3367 assocresp_extra);
3368free_ap_params:
3369 ieee802_11_free_ap_params(&params);
3370 return ret;
3371}
3372
3373
3374/*
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003375 * TODO: This flow currently supports only changing channel and width within
3376 * the same hw_mode. Any other changes to MAC parameters or provided settings
3377 * are not supported.
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003378 */
3379static int hostapd_change_config_freq(struct hostapd_data *hapd,
3380 struct hostapd_config *conf,
3381 struct hostapd_freq_params *params,
3382 struct hostapd_freq_params *old_params)
3383{
3384 int channel;
Hai Shalom81f62d82019-07-22 12:10:00 -07003385 u8 seg0, seg1;
3386 struct hostapd_hw_modes *mode;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003387
3388 if (!params->channel) {
3389 /* check if the new channel is supported by hw */
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003390 params->channel = hostapd_hw_get_channel(hapd, params->freq);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003391 }
3392
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003393 channel = params->channel;
3394 if (!channel)
3395 return -1;
3396
Hai Shalom81f62d82019-07-22 12:10:00 -07003397 mode = hapd->iface->current_mode;
3398
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003399 /* if a pointer to old_params is provided we save previous state */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003400 if (old_params &&
3401 hostapd_set_freq_params(old_params, conf->hw_mode,
3402 hostapd_hw_get_freq(hapd, conf->channel),
Hai Shalomc3565922019-10-28 11:58:20 -07003403 conf->channel, conf->enable_edmg,
3404 conf->edmg_channel, conf->ieee80211n,
Hai Shalom81f62d82019-07-22 12:10:00 -07003405 conf->ieee80211ac, conf->ieee80211ax,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003406 conf->secondary_channel,
Hai Shalom81f62d82019-07-22 12:10:00 -07003407 hostapd_get_oper_chwidth(conf),
3408 hostapd_get_oper_centr_freq_seg0_idx(conf),
3409 hostapd_get_oper_centr_freq_seg1_idx(conf),
3410 conf->vht_capab,
3411 mode ? &mode->he_capab[IEEE80211_MODE_AP] :
3412 NULL))
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003413 return -1;
3414
3415 switch (params->bandwidth) {
3416 case 0:
3417 case 20:
3418 case 40:
Hai Shalom81f62d82019-07-22 12:10:00 -07003419 hostapd_set_oper_chwidth(conf, CHANWIDTH_USE_HT);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003420 break;
3421 case 80:
3422 if (params->center_freq2)
Hai Shalom81f62d82019-07-22 12:10:00 -07003423 hostapd_set_oper_chwidth(conf, CHANWIDTH_80P80MHZ);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003424 else
Hai Shalom81f62d82019-07-22 12:10:00 -07003425 hostapd_set_oper_chwidth(conf, CHANWIDTH_80MHZ);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003426 break;
3427 case 160:
Hai Shalom81f62d82019-07-22 12:10:00 -07003428 hostapd_set_oper_chwidth(conf, CHANWIDTH_160MHZ);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003429 break;
3430 default:
3431 return -1;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003432 }
3433
3434 conf->channel = channel;
3435 conf->ieee80211n = params->ht_enabled;
3436 conf->secondary_channel = params->sec_channel_offset;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003437 ieee80211_freq_to_chan(params->center_freq1,
Hai Shalom81f62d82019-07-22 12:10:00 -07003438 &seg0);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003439 ieee80211_freq_to_chan(params->center_freq2,
Hai Shalom81f62d82019-07-22 12:10:00 -07003440 &seg1);
3441 hostapd_set_oper_centr_freq_seg0_idx(conf, seg0);
3442 hostapd_set_oper_centr_freq_seg1_idx(conf, seg1);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003443
3444 /* TODO: maybe call here hostapd_config_check here? */
3445
3446 return 0;
3447}
3448
3449
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003450static int hostapd_fill_csa_settings(struct hostapd_data *hapd,
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003451 struct csa_settings *settings)
3452{
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003453 struct hostapd_iface *iface = hapd->iface;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003454 struct hostapd_freq_params old_freq;
3455 int ret;
Hai Shalom81f62d82019-07-22 12:10:00 -07003456 u8 chan, bandwidth;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003457
3458 os_memset(&old_freq, 0, sizeof(old_freq));
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003459 if (!iface || !iface->freq || hapd->csa_in_progress)
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003460 return -1;
3461
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003462 switch (settings->freq_params.bandwidth) {
3463 case 80:
3464 if (settings->freq_params.center_freq2)
Hai Shalom81f62d82019-07-22 12:10:00 -07003465 bandwidth = CHANWIDTH_80P80MHZ;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003466 else
Hai Shalom81f62d82019-07-22 12:10:00 -07003467 bandwidth = CHANWIDTH_80MHZ;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003468 break;
3469 case 160:
Hai Shalom81f62d82019-07-22 12:10:00 -07003470 bandwidth = CHANWIDTH_160MHZ;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003471 break;
3472 default:
Hai Shalom81f62d82019-07-22 12:10:00 -07003473 bandwidth = CHANWIDTH_USE_HT;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003474 break;
3475 }
3476
3477 if (ieee80211_freq_to_channel_ext(
3478 settings->freq_params.freq,
3479 settings->freq_params.sec_channel_offset,
Hai Shalom81f62d82019-07-22 12:10:00 -07003480 bandwidth,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003481 &hapd->iface->cs_oper_class,
3482 &chan) == NUM_HOSTAPD_MODES) {
3483 wpa_printf(MSG_DEBUG,
Hai Shalom81f62d82019-07-22 12:10:00 -07003484 "invalid frequency for channel switch (freq=%d, sec_channel_offset=%d, vht_enabled=%d, he_enabled=%d)",
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003485 settings->freq_params.freq,
3486 settings->freq_params.sec_channel_offset,
Hai Shalom81f62d82019-07-22 12:10:00 -07003487 settings->freq_params.vht_enabled,
3488 settings->freq_params.he_enabled);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003489 return -1;
3490 }
3491
3492 settings->freq_params.channel = chan;
3493
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003494 ret = hostapd_change_config_freq(iface->bss[0], iface->conf,
3495 &settings->freq_params,
3496 &old_freq);
3497 if (ret)
3498 return ret;
3499
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003500 ret = hostapd_build_beacon_data(hapd, &settings->beacon_after);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003501
3502 /* change back the configuration */
3503 hostapd_change_config_freq(iface->bss[0], iface->conf,
3504 &old_freq, NULL);
3505
3506 if (ret)
3507 return ret;
3508
3509 /* set channel switch parameters for csa ie */
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003510 hapd->cs_freq_params = settings->freq_params;
3511 hapd->cs_count = settings->cs_count;
3512 hapd->cs_block_tx = settings->block_tx;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003513
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003514 ret = hostapd_build_beacon_data(hapd, &settings->beacon_csa);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003515 if (ret) {
3516 free_beacon_data(&settings->beacon_after);
3517 return ret;
3518 }
3519
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003520 settings->counter_offset_beacon[0] = hapd->cs_c_off_beacon;
3521 settings->counter_offset_presp[0] = hapd->cs_c_off_proberesp;
3522 settings->counter_offset_beacon[1] = hapd->cs_c_off_ecsa_beacon;
3523 settings->counter_offset_presp[1] = hapd->cs_c_off_ecsa_proberesp;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003524
3525 return 0;
3526}
3527
3528
3529void hostapd_cleanup_cs_params(struct hostapd_data *hapd)
3530{
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003531 os_memset(&hapd->cs_freq_params, 0, sizeof(hapd->cs_freq_params));
3532 hapd->cs_count = 0;
3533 hapd->cs_block_tx = 0;
3534 hapd->cs_c_off_beacon = 0;
3535 hapd->cs_c_off_proberesp = 0;
3536 hapd->csa_in_progress = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003537 hapd->cs_c_off_ecsa_beacon = 0;
3538 hapd->cs_c_off_ecsa_proberesp = 0;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003539}
3540
3541
Roshan Pius3a1667e2018-07-03 15:17:14 -07003542void hostapd_chan_switch_vht_config(struct hostapd_data *hapd, int vht_enabled)
3543{
3544 if (vht_enabled)
3545 hapd->iconf->ch_switch_vht_config |= CH_SWITCH_VHT_ENABLED;
3546 else
3547 hapd->iconf->ch_switch_vht_config |= CH_SWITCH_VHT_DISABLED;
3548
3549 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
3550 HOSTAPD_LEVEL_INFO, "CHAN_SWITCH VHT CONFIG 0x%x",
3551 hapd->iconf->ch_switch_vht_config);
3552}
3553
3554
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003555int hostapd_switch_channel(struct hostapd_data *hapd,
3556 struct csa_settings *settings)
3557{
3558 int ret;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003559
3560 if (!(hapd->iface->drv_flags & WPA_DRIVER_FLAGS_AP_CSA)) {
3561 wpa_printf(MSG_INFO, "CSA is not supported");
3562 return -1;
3563 }
3564
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003565 ret = hostapd_fill_csa_settings(hapd, settings);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003566 if (ret)
3567 return ret;
3568
3569 ret = hostapd_drv_switch_channel(hapd, settings);
3570 free_beacon_data(&settings->beacon_csa);
3571 free_beacon_data(&settings->beacon_after);
3572
3573 if (ret) {
3574 /* if we failed, clean cs parameters */
3575 hostapd_cleanup_cs_params(hapd);
3576 return ret;
3577 }
3578
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003579 hapd->csa_in_progress = 1;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003580 return 0;
3581}
3582
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003583
3584void
3585hostapd_switch_channel_fallback(struct hostapd_iface *iface,
3586 const struct hostapd_freq_params *freq_params)
3587{
Hai Shalom81f62d82019-07-22 12:10:00 -07003588 int seg0_idx = 0, seg1_idx = 0, bw = CHANWIDTH_USE_HT;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003589
3590 wpa_printf(MSG_DEBUG, "Restarting all CSA-related BSSes");
3591
3592 if (freq_params->center_freq1)
Hai Shalom81f62d82019-07-22 12:10:00 -07003593 seg0_idx = 36 + (freq_params->center_freq1 - 5180) / 5;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003594 if (freq_params->center_freq2)
Hai Shalom81f62d82019-07-22 12:10:00 -07003595 seg1_idx = 36 + (freq_params->center_freq2 - 5180) / 5;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003596
3597 switch (freq_params->bandwidth) {
3598 case 0:
3599 case 20:
3600 case 40:
Hai Shalom81f62d82019-07-22 12:10:00 -07003601 bw = CHANWIDTH_USE_HT;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003602 break;
3603 case 80:
3604 if (freq_params->center_freq2)
Hai Shalom81f62d82019-07-22 12:10:00 -07003605 bw = CHANWIDTH_80P80MHZ;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003606 else
Hai Shalom81f62d82019-07-22 12:10:00 -07003607 bw = CHANWIDTH_80MHZ;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003608 break;
3609 case 160:
Hai Shalom81f62d82019-07-22 12:10:00 -07003610 bw = CHANWIDTH_160MHZ;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003611 break;
3612 default:
3613 wpa_printf(MSG_WARNING, "Unknown CSA bandwidth: %d",
3614 freq_params->bandwidth);
3615 break;
3616 }
3617
3618 iface->freq = freq_params->freq;
3619 iface->conf->channel = freq_params->channel;
3620 iface->conf->secondary_channel = freq_params->sec_channel_offset;
Hai Shalom81f62d82019-07-22 12:10:00 -07003621 hostapd_set_oper_centr_freq_seg0_idx(iface->conf, seg0_idx);
3622 hostapd_set_oper_centr_freq_seg1_idx(iface->conf, seg1_idx);
3623 hostapd_set_oper_chwidth(iface->conf, bw);
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003624 iface->conf->ieee80211n = freq_params->ht_enabled;
3625 iface->conf->ieee80211ac = freq_params->vht_enabled;
Hai Shalom81f62d82019-07-22 12:10:00 -07003626 iface->conf->ieee80211ax = freq_params->he_enabled;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003627
3628 /*
3629 * cs_params must not be cleared earlier because the freq_params
3630 * argument may actually point to one of these.
Hai Shalom39ba6fc2019-01-22 12:40:38 -08003631 * These params will be cleared during interface disable below.
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003632 */
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003633 hostapd_disable_iface(iface);
3634 hostapd_enable_iface(iface);
3635}
3636
Dmitry Shmidte4663042016-04-04 10:07:49 -07003637#endif /* NEED_AP_MLME */
3638
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003639
3640struct hostapd_data * hostapd_get_iface(struct hapd_interfaces *interfaces,
3641 const char *ifname)
3642{
3643 size_t i, j;
3644
3645 for (i = 0; i < interfaces->count; i++) {
3646 struct hostapd_iface *iface = interfaces->iface[i];
3647
3648 for (j = 0; j < iface->num_bss; j++) {
3649 struct hostapd_data *hapd = iface->bss[j];
3650
3651 if (os_strcmp(ifname, hapd->conf->iface) == 0)
3652 return hapd;
3653 }
3654 }
3655
3656 return NULL;
3657}
3658
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003659
3660void hostapd_periodic_iface(struct hostapd_iface *iface)
3661{
3662 size_t i;
3663
3664 ap_list_timer(iface);
3665
3666 for (i = 0; i < iface->num_bss; i++) {
3667 struct hostapd_data *hapd = iface->bss[i];
3668
3669 if (!hapd->started)
3670 continue;
3671
3672#ifndef CONFIG_NO_RADIUS
3673 hostapd_acl_expire(hapd);
3674#endif /* CONFIG_NO_RADIUS */
3675 }
3676}
Hai Shalom899fcc72020-10-19 14:38:18 -07003677
3678
3679#ifdef CONFIG_OCV
3680void hostapd_ocv_check_csa_sa_query(void *eloop_ctx, void *timeout_ctx)
3681{
3682 struct hostapd_data *hapd = eloop_ctx;
3683 struct sta_info *sta;
3684
3685 wpa_printf(MSG_DEBUG, "OCV: Post-CSA SA Query initiation check");
3686
3687 for (sta = hapd->sta_list; sta; sta = sta->next) {
3688 if (!sta->post_csa_sa_query)
3689 continue;
3690
3691 wpa_printf(MSG_DEBUG, "OCV: OCVC STA " MACSTR
3692 " did not start SA Query after CSA - disconnect",
3693 MAC2STR(sta->addr));
3694 ap_sta_disconnect(hapd, sta, sta->addr,
3695 WLAN_REASON_PREV_AUTH_NOT_VALID);
3696 }
3697}
3698#endif /* CONFIG_OCV */