blob: f9af038bec9ac7f7e2e8ae89cbb29d4def383c7a [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
442#ifdef CONFIG_SAE
443 {
444 struct hostapd_sae_commit_queue *q;
445
446 while ((q = dl_list_first(&hapd->sae_commit_queue,
447 struct hostapd_sae_commit_queue,
448 list))) {
449 dl_list_del(&q->list);
450 os_free(q);
451 }
452 }
453 eloop_cancel_timeout(auth_sae_process_commit, hapd, NULL);
454#endif /* CONFIG_SAE */
Dmitry Shmidt04949592012-07-19 12:16:46 -0700455}
456
457
458/**
459 * hostapd_cleanup - Per-BSS cleanup (deinitialization)
460 * @hapd: Pointer to BSS data
461 *
462 * This function is used to free all per-BSS data structures and resources.
Dmitry Shmidt54605472013-11-08 11:10:19 -0800463 * Most of the modules that are initialized in hostapd_setup_bss() are
464 * deinitialized here.
Dmitry Shmidt04949592012-07-19 12:16:46 -0700465 */
466static void hostapd_cleanup(struct hostapd_data *hapd)
467{
Dmitry Shmidt54605472013-11-08 11:10:19 -0800468 wpa_printf(MSG_DEBUG, "%s(hapd=%p (%s))", __func__, hapd,
Hai Shalom021b0b52019-04-10 11:17:58 -0700469 hapd->conf ? hapd->conf->iface : "N/A");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700470 if (hapd->iface->interfaces &&
Dmitry Shmidt29333592017-01-09 12:27:11 -0800471 hapd->iface->interfaces->ctrl_iface_deinit) {
472 wpa_msg(hapd->msg_ctx, MSG_INFO, WPA_EVENT_TERMINATING);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700473 hapd->iface->interfaces->ctrl_iface_deinit(hapd);
Dmitry Shmidt29333592017-01-09 12:27:11 -0800474 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700475 hostapd_free_hapd_data(hapd);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700476}
477
478
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800479static void sta_track_deinit(struct hostapd_iface *iface)
480{
481 struct hostapd_sta_info *info;
482
483 if (!iface->num_sta_seen)
484 return;
485
486 while ((info = dl_list_first(&iface->sta_seen, struct hostapd_sta_info,
487 list))) {
488 dl_list_del(&info->list);
489 iface->num_sta_seen--;
Dmitry Shmidtaca489e2016-09-28 15:44:14 -0700490 sta_track_del(info);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800491 }
492}
493
494
Dmitry Shmidt04949592012-07-19 12:16:46 -0700495static void hostapd_cleanup_iface_partial(struct hostapd_iface *iface)
496{
Dmitry Shmidt54605472013-11-08 11:10:19 -0800497 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800498#ifdef NEED_AP_MLME
499 hostapd_stop_setup_timers(iface);
500#endif /* NEED_AP_MLME */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700501 if (iface->current_mode)
502 acs_cleanup(iface);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700503 hostapd_free_hw_features(iface->hw_features, iface->num_hw_features);
504 iface->hw_features = NULL;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700505 iface->current_mode = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700506 os_free(iface->current_rates);
507 iface->current_rates = NULL;
508 os_free(iface->basic_rates);
509 iface->basic_rates = NULL;
510 ap_list_deinit(iface);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800511 sta_track_deinit(iface);
Hai Shalom81f62d82019-07-22 12:10:00 -0700512 airtime_policy_update_deinit(iface);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700513}
514
515
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700516/**
517 * hostapd_cleanup_iface - Complete per-interface cleanup
518 * @iface: Pointer to interface data
519 *
520 * This function is called after per-BSS data structures are deinitialized
521 * with hostapd_cleanup().
522 */
523static void hostapd_cleanup_iface(struct hostapd_iface *iface)
524{
Dmitry Shmidt54605472013-11-08 11:10:19 -0800525 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800526 eloop_cancel_timeout(channel_list_update_timeout, iface, NULL);
Roshan Pius3a1667e2018-07-03 15:17:14 -0700527 eloop_cancel_timeout(hostapd_interface_setup_failure_handler, iface,
528 NULL);
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800529
Dmitry Shmidt04949592012-07-19 12:16:46 -0700530 hostapd_cleanup_iface_partial(iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700531 hostapd_config_free(iface->conf);
532 iface->conf = NULL;
533
534 os_free(iface->config_fname);
535 os_free(iface->bss);
Dmitry Shmidt54605472013-11-08 11:10:19 -0800536 wpa_printf(MSG_DEBUG, "%s: free iface=%p", __func__, iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700537 os_free(iface);
538}
539
540
Hai Shalomfdcde762020-04-02 11:19:20 -0700541#ifdef CONFIG_WEP
542
Dmitry Shmidt04949592012-07-19 12:16:46 -0700543static void hostapd_clear_wep(struct hostapd_data *hapd)
544{
Hai Shalom021b0b52019-04-10 11:17:58 -0700545 if (hapd->drv_priv && !hapd->iface->driver_ap_teardown && hapd->conf) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700546 hostapd_set_privacy(hapd, 0);
547 hostapd_broadcast_wep_clear(hapd);
548 }
549}
550
551
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700552static int hostapd_setup_encryption(char *iface, struct hostapd_data *hapd)
553{
554 int i;
555
556 hostapd_broadcast_wep_set(hapd);
557
558 if (hapd->conf->ssid.wep.default_len) {
559 hostapd_set_privacy(hapd, 1);
560 return 0;
561 }
562
Jouni Malinen75ecf522011-06-27 15:19:46 -0700563 /*
564 * When IEEE 802.1X is not enabled, the driver may need to know how to
565 * set authentication algorithms for static WEP.
566 */
567 hostapd_drv_set_authmode(hapd, hapd->conf->auth_algs);
568
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700569 for (i = 0; i < 4; i++) {
570 if (hapd->conf->ssid.wep.key[i] &&
Hai Shalomfdcde762020-04-02 11:19:20 -0700571 hostapd_drv_set_key(iface, hapd, WPA_ALG_WEP, NULL, i, 0,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700572 i == hapd->conf->ssid.wep.idx, NULL, 0,
573 hapd->conf->ssid.wep.key[i],
Hai Shalomfdcde762020-04-02 11:19:20 -0700574 hapd->conf->ssid.wep.len[i],
575 i == hapd->conf->ssid.wep.idx ?
576 KEY_FLAG_GROUP_RX_TX_DEFAULT :
577 KEY_FLAG_GROUP_RX_TX)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700578 wpa_printf(MSG_WARNING, "Could not set WEP "
579 "encryption.");
580 return -1;
581 }
582 if (hapd->conf->ssid.wep.key[i] &&
583 i == hapd->conf->ssid.wep.idx)
584 hostapd_set_privacy(hapd, 1);
585 }
586
587 return 0;
588}
589
Hai Shalomfdcde762020-04-02 11:19:20 -0700590#endif /* CONFIG_WEP */
591
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700592
Dmitry Shmidt04949592012-07-19 12:16:46 -0700593static int hostapd_flush_old_stations(struct hostapd_data *hapd, u16 reason)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700594{
595 int ret = 0;
596 u8 addr[ETH_ALEN];
597
598 if (hostapd_drv_none(hapd) || hapd->drv_priv == NULL)
599 return 0;
600
Dmitry Shmidta38abf92014-03-06 13:38:44 -0800601 if (!hapd->iface->driver_ap_teardown) {
602 wpa_dbg(hapd->msg_ctx, MSG_DEBUG,
603 "Flushing old station entries");
604
605 if (hostapd_flush(hapd)) {
606 wpa_msg(hapd->msg_ctx, MSG_WARNING,
607 "Could not connect to kernel driver");
608 ret = -1;
609 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700610 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700611 if (hapd->conf && hapd->conf->broadcast_deauth) {
612 wpa_dbg(hapd->msg_ctx, MSG_DEBUG,
613 "Deauthenticate all stations");
614 os_memset(addr, 0xff, ETH_ALEN);
615 hostapd_drv_sta_deauth(hapd, addr, reason);
616 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700617 hostapd_free_stas(hapd);
618
619 return ret;
620}
621
622
Dmitry Shmidt71757432014-06-02 13:50:35 -0700623static void hostapd_bss_deinit_no_free(struct hostapd_data *hapd)
624{
625 hostapd_free_stas(hapd);
626 hostapd_flush_old_stations(hapd, WLAN_REASON_DEAUTH_LEAVING);
Hai Shalomfdcde762020-04-02 11:19:20 -0700627#ifdef CONFIG_WEP
Dmitry Shmidt71757432014-06-02 13:50:35 -0700628 hostapd_clear_wep(hapd);
Hai Shalomfdcde762020-04-02 11:19:20 -0700629#endif /* CONFIG_WEP */
Dmitry Shmidt71757432014-06-02 13:50:35 -0700630}
631
632
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700633/**
634 * hostapd_validate_bssid_configuration - Validate BSSID configuration
635 * @iface: Pointer to interface data
636 * Returns: 0 on success, -1 on failure
637 *
638 * This function is used to validate that the configured BSSIDs are valid.
639 */
640static int hostapd_validate_bssid_configuration(struct hostapd_iface *iface)
641{
642 u8 mask[ETH_ALEN] = { 0 };
643 struct hostapd_data *hapd = iface->bss[0];
644 unsigned int i = iface->conf->num_bss, bits = 0, j;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700645 int auto_addr = 0;
646
647 if (hostapd_drv_none(hapd))
648 return 0;
649
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -0800650 if (iface->conf->use_driver_iface_addr)
651 return 0;
652
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700653 /* Generate BSSID mask that is large enough to cover the BSSIDs. */
654
655 /* Determine the bits necessary to cover the number of BSSIDs. */
656 for (i--; i; i >>= 1)
657 bits++;
658
659 /* Determine the bits necessary to any configured BSSIDs,
660 if they are higher than the number of BSSIDs. */
661 for (j = 0; j < iface->conf->num_bss; j++) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800662 if (is_zero_ether_addr(iface->conf->bss[j]->bssid)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700663 if (j)
664 auto_addr++;
665 continue;
666 }
667
668 for (i = 0; i < ETH_ALEN; i++) {
669 mask[i] |=
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800670 iface->conf->bss[j]->bssid[i] ^
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700671 hapd->own_addr[i];
672 }
673 }
674
675 if (!auto_addr)
676 goto skip_mask_ext;
677
678 for (i = 0; i < ETH_ALEN && mask[i] == 0; i++)
679 ;
680 j = 0;
681 if (i < ETH_ALEN) {
682 j = (5 - i) * 8;
683
684 while (mask[i] != 0) {
685 mask[i] >>= 1;
686 j++;
687 }
688 }
689
690 if (bits < j)
691 bits = j;
692
693 if (bits > 40) {
694 wpa_printf(MSG_ERROR, "Too many bits in the BSSID mask (%u)",
695 bits);
696 return -1;
697 }
698
699 os_memset(mask, 0xff, ETH_ALEN);
700 j = bits / 8;
701 for (i = 5; i > 5 - j; i--)
702 mask[i] = 0;
703 j = bits % 8;
Hai Shalom021b0b52019-04-10 11:17:58 -0700704 while (j) {
705 j--;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700706 mask[i] <<= 1;
Hai Shalom021b0b52019-04-10 11:17:58 -0700707 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700708
709skip_mask_ext:
710 wpa_printf(MSG_DEBUG, "BSS count %lu, BSSID mask " MACSTR " (%d bits)",
711 (unsigned long) iface->conf->num_bss, MAC2STR(mask), bits);
712
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700713 if (!auto_addr)
714 return 0;
715
716 for (i = 0; i < ETH_ALEN; i++) {
717 if ((hapd->own_addr[i] & mask[i]) != hapd->own_addr[i]) {
718 wpa_printf(MSG_ERROR, "Invalid BSSID mask " MACSTR
719 " for start address " MACSTR ".",
720 MAC2STR(mask), MAC2STR(hapd->own_addr));
721 wpa_printf(MSG_ERROR, "Start address must be the "
722 "first address in the block (i.e., addr "
723 "AND mask == addr).");
724 return -1;
725 }
726 }
727
728 return 0;
729}
730
731
732static int mac_in_conf(struct hostapd_config *conf, const void *a)
733{
734 size_t i;
735
736 for (i = 0; i < conf->num_bss; i++) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800737 if (hostapd_mac_comp(conf->bss[i]->bssid, a) == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700738 return 1;
739 }
740 }
741
742 return 0;
743}
744
745
Dmitry Shmidt04949592012-07-19 12:16:46 -0700746#ifndef CONFIG_NO_RADIUS
747
748static int hostapd_das_nas_mismatch(struct hostapd_data *hapd,
749 struct radius_das_attrs *attr)
750{
Dmitry Shmidt13ca8d82014-02-20 10:18:40 -0800751 if (attr->nas_identifier &&
752 (!hapd->conf->nas_identifier ||
753 os_strlen(hapd->conf->nas_identifier) !=
754 attr->nas_identifier_len ||
755 os_memcmp(hapd->conf->nas_identifier, attr->nas_identifier,
756 attr->nas_identifier_len) != 0)) {
757 wpa_printf(MSG_DEBUG, "RADIUS DAS: NAS-Identifier mismatch");
758 return 1;
759 }
760
761 if (attr->nas_ip_addr &&
762 (hapd->conf->own_ip_addr.af != AF_INET ||
763 os_memcmp(&hapd->conf->own_ip_addr.u.v4, attr->nas_ip_addr, 4) !=
764 0)) {
765 wpa_printf(MSG_DEBUG, "RADIUS DAS: NAS-IP-Address mismatch");
766 return 1;
767 }
768
769#ifdef CONFIG_IPV6
770 if (attr->nas_ipv6_addr &&
771 (hapd->conf->own_ip_addr.af != AF_INET6 ||
772 os_memcmp(&hapd->conf->own_ip_addr.u.v6, attr->nas_ipv6_addr, 16)
773 != 0)) {
774 wpa_printf(MSG_DEBUG, "RADIUS DAS: NAS-IPv6-Address mismatch");
775 return 1;
776 }
777#endif /* CONFIG_IPV6 */
778
Dmitry Shmidt04949592012-07-19 12:16:46 -0700779 return 0;
780}
781
782
783static struct sta_info * hostapd_das_find_sta(struct hostapd_data *hapd,
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800784 struct radius_das_attrs *attr,
785 int *multi)
Dmitry Shmidt04949592012-07-19 12:16:46 -0700786{
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800787 struct sta_info *selected, *sta;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700788 char buf[128];
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800789 int num_attr = 0;
790 int count;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700791
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800792 *multi = 0;
793
794 for (sta = hapd->sta_list; sta; sta = sta->next)
795 sta->radius_das_match = 1;
796
797 if (attr->sta_addr) {
798 num_attr++;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700799 sta = ap_get_sta(hapd, attr->sta_addr);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800800 if (!sta) {
801 wpa_printf(MSG_DEBUG,
802 "RADIUS DAS: No Calling-Station-Id match");
803 return NULL;
804 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700805
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800806 selected = sta;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700807 for (sta = hapd->sta_list; sta; sta = sta->next) {
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800808 if (sta != selected)
809 sta->radius_das_match = 0;
810 }
811 wpa_printf(MSG_DEBUG, "RADIUS DAS: Calling-Station-Id match");
812 }
813
814 if (attr->acct_session_id) {
815 num_attr++;
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800816 if (attr->acct_session_id_len != 16) {
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800817 wpa_printf(MSG_DEBUG,
818 "RADIUS DAS: Acct-Session-Id cannot match");
819 return NULL;
820 }
821 count = 0;
822
823 for (sta = hapd->sta_list; sta; sta = sta->next) {
824 if (!sta->radius_das_match)
825 continue;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800826 os_snprintf(buf, sizeof(buf), "%016llX",
827 (unsigned long long) sta->acct_session_id);
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800828 if (os_memcmp(attr->acct_session_id, buf, 16) != 0)
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800829 sta->radius_das_match = 0;
830 else
831 count++;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700832 }
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800833
834 if (count == 0) {
835 wpa_printf(MSG_DEBUG,
836 "RADIUS DAS: No matches remaining after Acct-Session-Id check");
837 return NULL;
838 }
839 wpa_printf(MSG_DEBUG, "RADIUS DAS: Acct-Session-Id match");
Dmitry Shmidt04949592012-07-19 12:16:46 -0700840 }
841
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800842 if (attr->acct_multi_session_id) {
843 num_attr++;
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800844 if (attr->acct_multi_session_id_len != 16) {
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800845 wpa_printf(MSG_DEBUG,
846 "RADIUS DAS: Acct-Multi-Session-Id cannot match");
847 return NULL;
848 }
849 count = 0;
850
851 for (sta = hapd->sta_list; sta; sta = sta->next) {
852 if (!sta->radius_das_match)
853 continue;
854 if (!sta->eapol_sm ||
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800855 !sta->eapol_sm->acct_multi_session_id) {
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800856 sta->radius_das_match = 0;
857 continue;
858 }
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800859 os_snprintf(buf, sizeof(buf), "%016llX",
860 (unsigned long long)
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800861 sta->eapol_sm->acct_multi_session_id);
862 if (os_memcmp(attr->acct_multi_session_id, buf, 16) !=
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800863 0)
864 sta->radius_das_match = 0;
865 else
866 count++;
867 }
868
869 if (count == 0) {
870 wpa_printf(MSG_DEBUG,
871 "RADIUS DAS: No matches remaining after Acct-Multi-Session-Id check");
872 return NULL;
873 }
874 wpa_printf(MSG_DEBUG,
875 "RADIUS DAS: Acct-Multi-Session-Id match");
876 }
877
878 if (attr->cui) {
879 num_attr++;
880 count = 0;
881
Dmitry Shmidt04949592012-07-19 12:16:46 -0700882 for (sta = hapd->sta_list; sta; sta = sta->next) {
883 struct wpabuf *cui;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800884
885 if (!sta->radius_das_match)
886 continue;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700887 cui = ieee802_1x_get_radius_cui(sta->eapol_sm);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800888 if (!cui || wpabuf_len(cui) != attr->cui_len ||
Dmitry Shmidt04949592012-07-19 12:16:46 -0700889 os_memcmp(wpabuf_head(cui), attr->cui,
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800890 attr->cui_len) != 0)
891 sta->radius_das_match = 0;
892 else
893 count++;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700894 }
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800895
896 if (count == 0) {
897 wpa_printf(MSG_DEBUG,
898 "RADIUS DAS: No matches remaining after Chargeable-User-Identity check");
899 return NULL;
900 }
901 wpa_printf(MSG_DEBUG,
902 "RADIUS DAS: Chargeable-User-Identity match");
Dmitry Shmidt04949592012-07-19 12:16:46 -0700903 }
904
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800905 if (attr->user_name) {
906 num_attr++;
907 count = 0;
908
Dmitry Shmidt04949592012-07-19 12:16:46 -0700909 for (sta = hapd->sta_list; sta; sta = sta->next) {
910 u8 *identity;
911 size_t identity_len;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800912
913 if (!sta->radius_das_match)
914 continue;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700915 identity = ieee802_1x_get_identity(sta->eapol_sm,
916 &identity_len);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800917 if (!identity ||
918 identity_len != attr->user_name_len ||
Dmitry Shmidt04949592012-07-19 12:16:46 -0700919 os_memcmp(identity, attr->user_name, identity_len)
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800920 != 0)
921 sta->radius_das_match = 0;
922 else
923 count++;
924 }
925
926 if (count == 0) {
927 wpa_printf(MSG_DEBUG,
928 "RADIUS DAS: No matches remaining after User-Name check");
929 return NULL;
930 }
931 wpa_printf(MSG_DEBUG,
932 "RADIUS DAS: User-Name match");
933 }
934
935 if (num_attr == 0) {
936 /*
937 * In theory, we could match all current associations, but it
938 * seems safer to just reject requests that do not include any
939 * session identification attributes.
940 */
941 wpa_printf(MSG_DEBUG,
942 "RADIUS DAS: No session identification attributes included");
943 return NULL;
944 }
945
946 selected = NULL;
947 for (sta = hapd->sta_list; sta; sta = sta->next) {
948 if (sta->radius_das_match) {
949 if (selected) {
950 *multi = 1;
951 return NULL;
952 }
953 selected = sta;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700954 }
955 }
956
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800957 return selected;
958}
959
960
961static int hostapd_das_disconnect_pmksa(struct hostapd_data *hapd,
962 struct radius_das_attrs *attr)
963{
964 if (!hapd->wpa_auth)
965 return -1;
966 return wpa_auth_radius_das_disconnect_pmksa(hapd->wpa_auth, attr);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700967}
968
969
970static enum radius_das_res
971hostapd_das_disconnect(void *ctx, struct radius_das_attrs *attr)
972{
973 struct hostapd_data *hapd = ctx;
974 struct sta_info *sta;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800975 int multi;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700976
977 if (hostapd_das_nas_mismatch(hapd, attr))
978 return RADIUS_DAS_NAS_MISMATCH;
979
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800980 sta = hostapd_das_find_sta(hapd, attr, &multi);
981 if (sta == NULL) {
982 if (multi) {
983 wpa_printf(MSG_DEBUG,
984 "RADIUS DAS: Multiple sessions match - not supported");
985 return RADIUS_DAS_MULTI_SESSION_MATCH;
986 }
987 if (hostapd_das_disconnect_pmksa(hapd, attr) == 0) {
988 wpa_printf(MSG_DEBUG,
989 "RADIUS DAS: PMKSA cache entry matched");
990 return RADIUS_DAS_SUCCESS;
991 }
992 wpa_printf(MSG_DEBUG, "RADIUS DAS: No matching session found");
Dmitry Shmidt04949592012-07-19 12:16:46 -0700993 return RADIUS_DAS_SESSION_NOT_FOUND;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800994 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700995
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800996 wpa_printf(MSG_DEBUG, "RADIUS DAS: Found a matching session " MACSTR
997 " - disconnecting", MAC2STR(sta->addr));
Dmitry Shmidt13ca8d82014-02-20 10:18:40 -0800998 wpa_auth_pmksa_remove(hapd->wpa_auth, sta->addr);
999
Dmitry Shmidt04949592012-07-19 12:16:46 -07001000 hostapd_drv_sta_deauth(hapd, sta->addr,
1001 WLAN_REASON_PREV_AUTH_NOT_VALID);
1002 ap_sta_deauthenticate(hapd, sta, WLAN_REASON_PREV_AUTH_NOT_VALID);
1003
1004 return RADIUS_DAS_SUCCESS;
1005}
1006
Roshan Pius3a1667e2018-07-03 15:17:14 -07001007
1008#ifdef CONFIG_HS20
1009static enum radius_das_res
1010hostapd_das_coa(void *ctx, struct radius_das_attrs *attr)
1011{
1012 struct hostapd_data *hapd = ctx;
1013 struct sta_info *sta;
1014 int multi;
1015
1016 if (hostapd_das_nas_mismatch(hapd, attr))
1017 return RADIUS_DAS_NAS_MISMATCH;
1018
1019 sta = hostapd_das_find_sta(hapd, attr, &multi);
1020 if (!sta) {
1021 if (multi) {
1022 wpa_printf(MSG_DEBUG,
1023 "RADIUS DAS: Multiple sessions match - not supported");
1024 return RADIUS_DAS_MULTI_SESSION_MATCH;
1025 }
1026 wpa_printf(MSG_DEBUG, "RADIUS DAS: No matching session found");
1027 return RADIUS_DAS_SESSION_NOT_FOUND;
1028 }
1029
1030 wpa_printf(MSG_DEBUG, "RADIUS DAS: Found a matching session " MACSTR
1031 " - CoA", MAC2STR(sta->addr));
1032
1033 if (attr->hs20_t_c_filtering) {
1034 if (attr->hs20_t_c_filtering[0] & BIT(0)) {
1035 wpa_printf(MSG_DEBUG,
1036 "HS 2.0: Unexpected Terms and Conditions filtering required in CoA-Request");
1037 return RADIUS_DAS_COA_FAILED;
1038 }
1039
1040 hs20_t_c_filtering(hapd, sta, 0);
1041 }
1042
1043 return RADIUS_DAS_SUCCESS;
1044}
1045#else /* CONFIG_HS20 */
1046#define hostapd_das_coa NULL
1047#endif /* CONFIG_HS20 */
1048
Hai Shalomc3565922019-10-28 11:58:20 -07001049
1050#ifdef CONFIG_SQLITE
1051
1052static int db_table_exists(sqlite3 *db, const char *name)
1053{
1054 char cmd[128];
1055
1056 os_snprintf(cmd, sizeof(cmd), "SELECT 1 FROM %s;", name);
1057 return sqlite3_exec(db, cmd, NULL, NULL, NULL) == SQLITE_OK;
1058}
1059
1060
1061static int db_table_create_radius_attributes(sqlite3 *db)
1062{
1063 char *err = NULL;
1064 const char *sql =
1065 "CREATE TABLE radius_attributes("
1066 " id INTEGER PRIMARY KEY,"
1067 " sta TEXT,"
1068 " reqtype TEXT,"
1069 " attr TEXT"
1070 ");"
1071 "CREATE INDEX idx_sta_reqtype ON radius_attributes(sta,reqtype);";
1072
1073 wpa_printf(MSG_DEBUG,
1074 "Adding database table for RADIUS attribute information");
1075 if (sqlite3_exec(db, sql, NULL, NULL, &err) != SQLITE_OK) {
1076 wpa_printf(MSG_ERROR, "SQLite error: %s", err);
1077 sqlite3_free(err);
1078 return -1;
1079 }
1080
1081 return 0;
1082}
1083
1084#endif /* CONFIG_SQLITE */
1085
Dmitry Shmidt04949592012-07-19 12:16:46 -07001086#endif /* CONFIG_NO_RADIUS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001087
1088
1089/**
1090 * hostapd_setup_bss - Per-BSS setup (initialization)
1091 * @hapd: Pointer to BSS data
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001092 * @first: Whether this BSS is the first BSS of an interface; -1 = not first,
1093 * but interface may exist
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001094 *
1095 * This function is used to initialize all per-BSS data structures and
1096 * resources. This gets called in a loop for each BSS when an interface is
1097 * initialized. Most of the modules that are initialized here will be
1098 * deinitialized in hostapd_cleanup().
1099 */
1100static int hostapd_setup_bss(struct hostapd_data *hapd, int first)
1101{
1102 struct hostapd_bss_config *conf = hapd->conf;
Dmitry Shmidt9d9e6022015-04-23 10:34:55 -07001103 u8 ssid[SSID_MAX_LEN + 1];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001104 int ssid_len, set_ssid;
1105 char force_ifname[IFNAMSIZ];
1106 u8 if_addr[ETH_ALEN];
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001107 int flush_old_stations = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001108
Dmitry Shmidt54605472013-11-08 11:10:19 -08001109 wpa_printf(MSG_DEBUG, "%s(hapd=%p (%s), first=%d)",
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001110 __func__, hapd, conf->iface, first);
Dmitry Shmidt54605472013-11-08 11:10:19 -08001111
Dmitry Shmidt50b691d2014-05-21 14:01:45 -07001112#ifdef EAP_SERVER_TNC
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001113 if (conf->tnc && tncs_global_init() < 0) {
Dmitry Shmidt50b691d2014-05-21 14:01:45 -07001114 wpa_printf(MSG_ERROR, "Failed to initialize TNCS");
1115 return -1;
1116 }
1117#endif /* EAP_SERVER_TNC */
1118
Dmitry Shmidt54605472013-11-08 11:10:19 -08001119 if (hapd->started) {
1120 wpa_printf(MSG_ERROR, "%s: Interface %s was already started",
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001121 __func__, conf->iface);
Dmitry Shmidt54605472013-11-08 11:10:19 -08001122 return -1;
1123 }
1124 hapd->started = 1;
1125
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001126 if (!first || first == -1) {
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08001127 u8 *addr = hapd->own_addr;
1128
1129 if (!is_zero_ether_addr(conf->bssid)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001130 /* Allocate the configured BSSID. */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001131 os_memcpy(hapd->own_addr, conf->bssid, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001132
1133 if (hostapd_mac_comp(hapd->own_addr,
1134 hapd->iface->bss[0]->own_addr) ==
1135 0) {
1136 wpa_printf(MSG_ERROR, "BSS '%s' may not have "
1137 "BSSID set to the MAC address of "
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001138 "the radio", conf->iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001139 return -1;
1140 }
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08001141 } else if (hapd->iconf->use_driver_iface_addr) {
1142 addr = NULL;
1143 } else {
1144 /* Allocate the next available BSSID. */
1145 do {
1146 inc_byte_array(hapd->own_addr, ETH_ALEN);
1147 } while (mac_in_conf(hapd->iconf, hapd->own_addr));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001148 }
1149
Dmitry Shmidt54605472013-11-08 11:10:19 -08001150 hapd->interface_added = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001151 if (hostapd_if_add(hapd->iface->bss[0], WPA_IF_AP_BSS,
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08001152 conf->iface, addr, hapd,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001153 &hapd->drv_priv, force_ifname, if_addr,
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001154 conf->bridge[0] ? conf->bridge : NULL,
1155 first == -1)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001156 wpa_printf(MSG_ERROR, "Failed to add BSS (BSSID="
1157 MACSTR ")", MAC2STR(hapd->own_addr));
Dmitry Shmidt3cf6f792013-12-18 13:12:19 -08001158 hapd->interface_added = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001159 return -1;
1160 }
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08001161
1162 if (!addr)
1163 os_memcpy(hapd->own_addr, if_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001164 }
1165
1166 if (conf->wmm_enabled < 0)
1167 conf->wmm_enabled = hapd->iconf->ieee80211n;
1168
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001169#ifdef CONFIG_IEEE80211R_AP
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001170 if (is_zero_ether_addr(conf->r1_key_holder))
1171 os_memcpy(conf->r1_key_holder, hapd->own_addr, ETH_ALEN);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001172#endif /* CONFIG_IEEE80211R_AP */
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001173
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001174#ifdef CONFIG_MESH
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001175 if ((hapd->conf->mesh & MESH_ENABLED) && hapd->iface->mconf == NULL)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001176 flush_old_stations = 0;
1177#endif /* CONFIG_MESH */
1178
1179 if (flush_old_stations)
Hai Shalom4fbc08f2020-05-18 12:37:00 -07001180 hostapd_flush(hapd);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001181 hostapd_set_privacy(hapd, 0);
1182
Hai Shalomfdcde762020-04-02 11:19:20 -07001183#ifdef CONFIG_WEP
Hai Shalom4fbc08f2020-05-18 12:37:00 -07001184 if (!hostapd_drv_nl80211(hapd))
1185 hostapd_broadcast_wep_clear(hapd);
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001186 if (hostapd_setup_encryption(conf->iface, hapd))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001187 return -1;
Hai Shalomfdcde762020-04-02 11:19:20 -07001188#endif /* CONFIG_WEP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001189
1190 /*
1191 * Fetch the SSID from the system and use it or,
1192 * if one was specified in the config file, verify they
1193 * match.
1194 */
1195 ssid_len = hostapd_get_ssid(hapd, ssid, sizeof(ssid));
1196 if (ssid_len < 0) {
1197 wpa_printf(MSG_ERROR, "Could not read SSID from system");
1198 return -1;
1199 }
1200 if (conf->ssid.ssid_set) {
1201 /*
1202 * If SSID is specified in the config file and it differs
1203 * from what is being used then force installation of the
1204 * new SSID.
1205 */
1206 set_ssid = (conf->ssid.ssid_len != (size_t) ssid_len ||
1207 os_memcmp(conf->ssid.ssid, ssid, ssid_len) != 0);
1208 } else {
1209 /*
1210 * No SSID in the config file; just use the one we got
1211 * from the system.
1212 */
1213 set_ssid = 0;
1214 conf->ssid.ssid_len = ssid_len;
1215 os_memcpy(conf->ssid.ssid, ssid, conf->ssid.ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001216 }
1217
Hai Shalomfdcde762020-04-02 11:19:20 -07001218 /*
1219 * Short SSID calculation is identical to FCS and it is defined in
1220 * IEEE P802.11-REVmd/D3.0, 9.4.2.170.3 (Calculating the Short-SSID).
1221 */
1222 conf->ssid.short_ssid = crc32(conf->ssid.ssid, conf->ssid.ssid_len);
1223
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001224 if (!hostapd_drv_none(hapd)) {
Hai Shalomfdcde762020-04-02 11:19:20 -07001225 wpa_printf(MSG_DEBUG, "Using interface %s with hwaddr " MACSTR
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001226 " and ssid \"%s\"",
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001227 conf->iface, MAC2STR(hapd->own_addr),
1228 wpa_ssid_txt(conf->ssid.ssid, conf->ssid.ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001229 }
1230
1231 if (hostapd_setup_wpa_psk(conf)) {
1232 wpa_printf(MSG_ERROR, "WPA-PSK setup failed.");
1233 return -1;
1234 }
1235
1236 /* Set SSID for the kernel driver (to be used in beacon and probe
1237 * response frames) */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001238 if (set_ssid && hostapd_set_ssid(hapd, conf->ssid.ssid,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001239 conf->ssid.ssid_len)) {
1240 wpa_printf(MSG_ERROR, "Could not set SSID for kernel driver");
1241 return -1;
1242 }
1243
Dmitry Shmidt818ea482014-03-10 13:15:21 -07001244 if (wpa_debug_level <= MSG_MSGDUMP)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001245 conf->radius->msg_dumps = 1;
1246#ifndef CONFIG_NO_RADIUS
Hai Shalomc3565922019-10-28 11:58:20 -07001247
1248#ifdef CONFIG_SQLITE
1249 if (conf->radius_req_attr_sqlite) {
1250 if (sqlite3_open(conf->radius_req_attr_sqlite,
1251 &hapd->rad_attr_db)) {
1252 wpa_printf(MSG_ERROR, "Could not open SQLite file '%s'",
1253 conf->radius_req_attr_sqlite);
1254 return -1;
1255 }
1256
1257 wpa_printf(MSG_DEBUG, "Opening RADIUS attribute database: %s",
1258 conf->radius_req_attr_sqlite);
1259 if (!db_table_exists(hapd->rad_attr_db, "radius_attributes") &&
1260 db_table_create_radius_attributes(hapd->rad_attr_db) < 0)
1261 return -1;
1262 }
1263#endif /* CONFIG_SQLITE */
1264
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001265 hapd->radius = radius_client_init(hapd, conf->radius);
1266 if (hapd->radius == NULL) {
1267 wpa_printf(MSG_ERROR, "RADIUS client initialization failed.");
1268 return -1;
1269 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001270
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001271 if (conf->radius_das_port) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001272 struct radius_das_conf das_conf;
1273 os_memset(&das_conf, 0, sizeof(das_conf));
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001274 das_conf.port = conf->radius_das_port;
1275 das_conf.shared_secret = conf->radius_das_shared_secret;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001276 das_conf.shared_secret_len =
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001277 conf->radius_das_shared_secret_len;
1278 das_conf.client_addr = &conf->radius_das_client_addr;
1279 das_conf.time_window = conf->radius_das_time_window;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001280 das_conf.require_event_timestamp =
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001281 conf->radius_das_require_event_timestamp;
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07001282 das_conf.require_message_authenticator =
1283 conf->radius_das_require_message_authenticator;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001284 das_conf.ctx = hapd;
1285 das_conf.disconnect = hostapd_das_disconnect;
Roshan Pius3a1667e2018-07-03 15:17:14 -07001286 das_conf.coa = hostapd_das_coa;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001287 hapd->radius_das = radius_das_init(&das_conf);
1288 if (hapd->radius_das == NULL) {
1289 wpa_printf(MSG_ERROR, "RADIUS DAS initialization "
1290 "failed.");
1291 return -1;
1292 }
1293 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001294#endif /* CONFIG_NO_RADIUS */
1295
1296 if (hostapd_acl_init(hapd)) {
1297 wpa_printf(MSG_ERROR, "ACL initialization failed.");
1298 return -1;
1299 }
1300 if (hostapd_init_wps(hapd, conf))
1301 return -1;
1302
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001303#ifdef CONFIG_DPP
1304 hapd->gas = gas_query_ap_init(hapd, hapd->msg_ctx);
1305 if (!hapd->gas)
1306 return -1;
1307 if (hostapd_dpp_init(hapd))
1308 return -1;
1309#endif /* CONFIG_DPP */
1310
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001311 if (authsrv_init(hapd) < 0)
1312 return -1;
1313
1314 if (ieee802_1x_init(hapd)) {
1315 wpa_printf(MSG_ERROR, "IEEE 802.1X initialization failed.");
1316 return -1;
1317 }
1318
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001319 if ((conf->wpa || conf->osen) && hostapd_setup_wpa(hapd))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001320 return -1;
1321
1322 if (accounting_init(hapd)) {
1323 wpa_printf(MSG_ERROR, "Accounting initialization failed.");
1324 return -1;
1325 }
1326
Dmitry Shmidt04949592012-07-19 12:16:46 -07001327#ifdef CONFIG_INTERWORKING
1328 if (gas_serv_init(hapd)) {
1329 wpa_printf(MSG_ERROR, "GAS server initialization failed");
1330 return -1;
1331 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001332
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001333 if (conf->qos_map_set_len &&
1334 hostapd_drv_set_qos_map(hapd, conf->qos_map_set,
1335 conf->qos_map_set_len)) {
1336 wpa_printf(MSG_ERROR, "Failed to initialize QoS Map");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001337 return -1;
1338 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001339#endif /* CONFIG_INTERWORKING */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001340
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001341 if (conf->bss_load_update_period && bss_load_update_init(hapd)) {
1342 wpa_printf(MSG_ERROR, "BSS Load initialization failed");
1343 return -1;
1344 }
1345
1346 if (conf->proxy_arp) {
1347 if (x_snoop_init(hapd)) {
1348 wpa_printf(MSG_ERROR,
1349 "Generic snooping infrastructure initialization failed");
1350 return -1;
1351 }
1352
1353 if (dhcp_snoop_init(hapd)) {
1354 wpa_printf(MSG_ERROR,
1355 "DHCP snooping initialization failed");
1356 return -1;
1357 }
1358
1359 if (ndisc_snoop_init(hapd)) {
1360 wpa_printf(MSG_ERROR,
1361 "Neighbor Discovery snooping initialization failed");
1362 return -1;
1363 }
1364 }
1365
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001366 if (!hostapd_drv_none(hapd) && vlan_init(hapd)) {
1367 wpa_printf(MSG_ERROR, "VLAN initialization failed.");
1368 return -1;
1369 }
1370
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001371 if (!conf->start_disabled && ieee802_11_set_beacon(hapd) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001372 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001373
Hai Shalom4fbc08f2020-05-18 12:37:00 -07001374 if (flush_old_stations && !conf->start_disabled &&
1375 conf->broadcast_deauth) {
1376 u8 addr[ETH_ALEN];
1377
1378 /* Should any previously associated STA not have noticed that
1379 * the AP had stopped and restarted, send one more
1380 * deauthentication notification now that the AP is ready to
1381 * operate. */
1382 wpa_dbg(hapd->msg_ctx, MSG_DEBUG,
1383 "Deauthenticate all stations at BSS start");
1384 os_memset(addr, 0xff, ETH_ALEN);
1385 hostapd_drv_sta_deauth(hapd, addr,
1386 WLAN_REASON_PREV_AUTH_NOT_VALID);
1387 }
1388
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001389 if (hapd->wpa_auth && wpa_init_keys(hapd->wpa_auth) < 0)
1390 return -1;
1391
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001392 if (hapd->driver && hapd->driver->set_operstate)
1393 hapd->driver->set_operstate(hapd->drv_priv, 1);
1394
1395 return 0;
1396}
1397
1398
1399static void hostapd_tx_queue_params(struct hostapd_iface *iface)
1400{
1401 struct hostapd_data *hapd = iface->bss[0];
1402 int i;
1403 struct hostapd_tx_queue_params *p;
1404
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001405#ifdef CONFIG_MESH
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001406 if ((hapd->conf->mesh & MESH_ENABLED) && iface->mconf == NULL)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001407 return;
1408#endif /* CONFIG_MESH */
1409
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001410 for (i = 0; i < NUM_TX_QUEUES; i++) {
1411 p = &iface->conf->tx_queue[i];
1412
1413 if (hostapd_set_tx_queue_params(hapd, i, p->aifs, p->cwmin,
1414 p->cwmax, p->burst)) {
1415 wpa_printf(MSG_DEBUG, "Failed to set TX queue "
1416 "parameters for queue %d.", i);
1417 /* Continue anyway */
1418 }
1419 }
1420}
1421
1422
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001423static int hostapd_set_acl_list(struct hostapd_data *hapd,
1424 struct mac_acl_entry *mac_acl,
1425 int n_entries, u8 accept_acl)
1426{
1427 struct hostapd_acl_params *acl_params;
1428 int i, err;
1429
1430 acl_params = os_zalloc(sizeof(*acl_params) +
1431 (n_entries * sizeof(acl_params->mac_acl[0])));
1432 if (!acl_params)
1433 return -ENOMEM;
1434
1435 for (i = 0; i < n_entries; i++)
1436 os_memcpy(acl_params->mac_acl[i].addr, mac_acl[i].addr,
1437 ETH_ALEN);
1438
1439 acl_params->acl_policy = accept_acl;
1440 acl_params->num_mac_acl = n_entries;
1441
1442 err = hostapd_drv_set_acl(hapd, acl_params);
1443
1444 os_free(acl_params);
1445
1446 return err;
1447}
1448
1449
1450static void hostapd_set_acl(struct hostapd_data *hapd)
1451{
1452 struct hostapd_config *conf = hapd->iconf;
1453 int err;
1454 u8 accept_acl;
1455
1456 if (hapd->iface->drv_max_acl_mac_addrs == 0)
1457 return;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001458
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001459 if (conf->bss[0]->macaddr_acl == DENY_UNLESS_ACCEPTED) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001460 accept_acl = 1;
1461 err = hostapd_set_acl_list(hapd, conf->bss[0]->accept_mac,
1462 conf->bss[0]->num_accept_mac,
1463 accept_acl);
1464 if (err) {
1465 wpa_printf(MSG_DEBUG, "Failed to set accept acl");
1466 return;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001467 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001468 } else if (conf->bss[0]->macaddr_acl == ACCEPT_UNLESS_DENIED) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001469 accept_acl = 0;
1470 err = hostapd_set_acl_list(hapd, conf->bss[0]->deny_mac,
1471 conf->bss[0]->num_deny_mac,
1472 accept_acl);
1473 if (err) {
1474 wpa_printf(MSG_DEBUG, "Failed to set deny acl");
1475 return;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001476 }
1477 }
1478}
1479
1480
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001481static int start_ctrl_iface_bss(struct hostapd_data *hapd)
1482{
1483 if (!hapd->iface->interfaces ||
1484 !hapd->iface->interfaces->ctrl_iface_init)
1485 return 0;
1486
1487 if (hapd->iface->interfaces->ctrl_iface_init(hapd)) {
1488 wpa_printf(MSG_ERROR,
1489 "Failed to setup control interface for %s",
1490 hapd->conf->iface);
1491 return -1;
1492 }
1493
1494 return 0;
1495}
1496
1497
1498static int start_ctrl_iface(struct hostapd_iface *iface)
1499{
1500 size_t i;
1501
1502 if (!iface->interfaces || !iface->interfaces->ctrl_iface_init)
1503 return 0;
1504
1505 for (i = 0; i < iface->num_bss; i++) {
1506 struct hostapd_data *hapd = iface->bss[i];
1507 if (iface->interfaces->ctrl_iface_init(hapd)) {
1508 wpa_printf(MSG_ERROR,
1509 "Failed to setup control interface for %s",
1510 hapd->conf->iface);
1511 return -1;
1512 }
1513 }
1514
1515 return 0;
1516}
1517
1518
1519static void channel_list_update_timeout(void *eloop_ctx, void *timeout_ctx)
1520{
1521 struct hostapd_iface *iface = eloop_ctx;
1522
1523 if (!iface->wait_channel_update) {
1524 wpa_printf(MSG_INFO, "Channel list update timeout, but interface was not waiting for it");
1525 return;
1526 }
1527
1528 /*
1529 * It is possible that the existing channel list is acceptable, so try
1530 * to proceed.
1531 */
1532 wpa_printf(MSG_DEBUG, "Channel list update timeout - try to continue anyway");
1533 setup_interface2(iface);
1534}
1535
1536
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001537void hostapd_channel_list_updated(struct hostapd_iface *iface, int initiator)
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001538{
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001539 if (!iface->wait_channel_update || initiator != REGDOM_SET_BY_USER)
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001540 return;
1541
1542 wpa_printf(MSG_DEBUG, "Channel list updated - continue setup");
1543 eloop_cancel_timeout(channel_list_update_timeout, iface, NULL);
1544 setup_interface2(iface);
1545}
1546
1547
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001548static int setup_interface(struct hostapd_iface *iface)
1549{
1550 struct hostapd_data *hapd = iface->bss[0];
1551 size_t i;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001552
Dmitry Shmidta38abf92014-03-06 13:38:44 -08001553 /*
1554 * It is possible that setup_interface() is called after the interface
1555 * was disabled etc., in which case driver_ap_teardown is possibly set
1556 * to 1. Clear it here so any other key/station deletion, which is not
1557 * part of a teardown flow, would also call the relevant driver
1558 * callbacks.
1559 */
1560 iface->driver_ap_teardown = 0;
1561
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001562 if (!iface->phy[0]) {
1563 const char *phy = hostapd_drv_get_radio_name(hapd);
1564 if (phy) {
1565 wpa_printf(MSG_DEBUG, "phy: %s", phy);
1566 os_strlcpy(iface->phy, phy, sizeof(iface->phy));
1567 }
1568 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001569
1570 /*
1571 * Make sure that all BSSes get configured with a pointer to the same
1572 * driver interface.
1573 */
1574 for (i = 1; i < iface->num_bss; i++) {
1575 iface->bss[i]->driver = hapd->driver;
1576 iface->bss[i]->drv_priv = hapd->drv_priv;
1577 }
1578
1579 if (hostapd_validate_bssid_configuration(iface))
1580 return -1;
1581
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001582 /*
1583 * Initialize control interfaces early to allow external monitoring of
1584 * channel setup operations that may take considerable amount of time
1585 * especially for DFS cases.
1586 */
1587 if (start_ctrl_iface(iface))
1588 return -1;
1589
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001590 if (hapd->iconf->country[0] && hapd->iconf->country[1]) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001591 char country[4], previous_country[4];
1592
1593 hostapd_set_state(iface, HAPD_IFACE_COUNTRY_UPDATE);
1594 if (hostapd_get_country(hapd, previous_country) < 0)
1595 previous_country[0] = '\0';
1596
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001597 os_memcpy(country, hapd->iconf->country, 3);
1598 country[3] = '\0';
1599 if (hostapd_set_country(hapd, country) < 0) {
1600 wpa_printf(MSG_ERROR, "Failed to set country code");
1601 return -1;
1602 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001603
1604 wpa_printf(MSG_DEBUG, "Previous country code %s, new country code %s",
1605 previous_country, country);
1606
1607 if (os_strncmp(previous_country, country, 2) != 0) {
1608 wpa_printf(MSG_DEBUG, "Continue interface setup after channel list update");
1609 iface->wait_channel_update = 1;
Dmitry Shmidt97672262014-02-03 13:02:54 -08001610 eloop_register_timeout(5, 0,
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001611 channel_list_update_timeout,
1612 iface, NULL);
1613 return 0;
1614 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001615 }
1616
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001617 return setup_interface2(iface);
1618}
1619
1620
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001621static int configured_fixed_chan_to_freq(struct hostapd_iface *iface)
1622{
1623 int freq, i, j;
1624
1625 if (!iface->conf->channel)
1626 return 0;
1627 if (iface->conf->op_class) {
1628 freq = ieee80211_chan_to_freq(NULL, iface->conf->op_class,
1629 iface->conf->channel);
1630 if (freq < 0) {
1631 wpa_printf(MSG_INFO,
1632 "Could not convert op_class %u channel %u to operating frequency",
1633 iface->conf->op_class, iface->conf->channel);
1634 return -1;
1635 }
1636 iface->freq = freq;
1637 return 0;
1638 }
1639
1640 /* Old configurations using only 2.4/5/60 GHz bands may not specify the
1641 * op_class parameter. Select a matching channel from the configured
1642 * mode using the channel parameter for these cases.
1643 */
1644 for (j = 0; j < iface->num_hw_features; j++) {
1645 struct hostapd_hw_modes *mode = &iface->hw_features[j];
1646
1647 if (iface->conf->hw_mode != HOSTAPD_MODE_IEEE80211ANY &&
1648 iface->conf->hw_mode != mode->mode)
1649 continue;
1650 for (i = 0; i < mode->num_channels; i++) {
1651 struct hostapd_channel_data *chan = &mode->channels[i];
1652
1653 if (chan->chan == iface->conf->channel &&
1654 !is_6ghz_freq(chan->freq)) {
1655 iface->freq = chan->freq;
1656 return 0;
1657 }
1658 }
1659 }
1660
1661 wpa_printf(MSG_INFO, "Could not determine operating frequency");
1662 return -1;
1663}
1664
1665
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001666static int setup_interface2(struct hostapd_iface *iface)
1667{
1668 iface->wait_channel_update = 0;
1669
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001670 if (hostapd_get_hw_features(iface)) {
1671 /* Not all drivers support this yet, so continue without hw
1672 * feature data. */
1673 } else {
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001674 int ret;
1675
1676 ret = configured_fixed_chan_to_freq(iface);
1677 if (ret < 0)
1678 goto fail;
1679
1680 if (iface->conf->op_class) {
1681 int ch_width;
1682
1683 ch_width = op_class_to_ch_width(iface->conf->op_class);
1684 hostapd_set_oper_chwidth(iface->conf, ch_width);
1685 }
1686
1687 ret = hostapd_select_hw_mode(iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001688 if (ret < 0) {
1689 wpa_printf(MSG_ERROR, "Could not select hw_mode and "
1690 "channel. (%d)", ret);
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001691 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001692 }
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001693 if (ret == 1) {
1694 wpa_printf(MSG_DEBUG, "Interface initialization will be completed in a callback (ACS)");
1695 return 0;
1696 }
Hai Shalomc3565922019-10-28 11:58:20 -07001697 ret = hostapd_check_edmg_capab(iface);
1698 if (ret < 0)
1699 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001700 ret = hostapd_check_ht_capab(iface);
1701 if (ret < 0)
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001702 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001703 if (ret == 1) {
1704 wpa_printf(MSG_DEBUG, "Interface initialization will "
1705 "be completed in a callback");
1706 return 0;
1707 }
Dmitry Shmidt051af732013-10-22 13:52:46 -07001708
1709 if (iface->conf->ieee80211h)
1710 wpa_printf(MSG_DEBUG, "DFS support is enabled");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001711 }
1712 return hostapd_setup_interface_complete(iface, 0);
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001713
1714fail:
1715 hostapd_set_state(iface, HAPD_IFACE_DISABLED);
1716 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
1717 if (iface->interfaces && iface->interfaces->terminate_on_error)
1718 eloop_terminate();
1719 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001720}
1721
1722
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001723#ifdef CONFIG_FST
1724
1725static const u8 * fst_hostapd_get_bssid_cb(void *ctx)
1726{
1727 struct hostapd_data *hapd = ctx;
1728
1729 return hapd->own_addr;
1730}
1731
1732
1733static void fst_hostapd_get_channel_info_cb(void *ctx,
1734 enum hostapd_hw_mode *hw_mode,
1735 u8 *channel)
1736{
1737 struct hostapd_data *hapd = ctx;
1738
1739 *hw_mode = ieee80211_freq_to_chan(hapd->iface->freq, channel);
1740}
1741
1742
1743static void fst_hostapd_set_ies_cb(void *ctx, const struct wpabuf *fst_ies)
1744{
1745 struct hostapd_data *hapd = ctx;
1746
1747 if (hapd->iface->fst_ies != fst_ies) {
1748 hapd->iface->fst_ies = fst_ies;
1749 if (ieee802_11_set_beacon(hapd))
1750 wpa_printf(MSG_WARNING, "FST: Cannot set beacon");
1751 }
1752}
1753
1754
1755static int fst_hostapd_send_action_cb(void *ctx, const u8 *da,
1756 struct wpabuf *buf)
1757{
1758 struct hostapd_data *hapd = ctx;
1759
1760 return hostapd_drv_send_action(hapd, hapd->iface->freq, 0, da,
1761 wpabuf_head(buf), wpabuf_len(buf));
1762}
1763
1764
1765static const struct wpabuf * fst_hostapd_get_mb_ie_cb(void *ctx, const u8 *addr)
1766{
1767 struct hostapd_data *hapd = ctx;
1768 struct sta_info *sta = ap_get_sta(hapd, addr);
1769
1770 return sta ? sta->mb_ies : NULL;
1771}
1772
1773
1774static void fst_hostapd_update_mb_ie_cb(void *ctx, const u8 *addr,
1775 const u8 *buf, size_t size)
1776{
1777 struct hostapd_data *hapd = ctx;
1778 struct sta_info *sta = ap_get_sta(hapd, addr);
1779
1780 if (sta) {
1781 struct mb_ies_info info;
1782
1783 if (!mb_ies_info_by_ies(&info, buf, size)) {
1784 wpabuf_free(sta->mb_ies);
1785 sta->mb_ies = mb_ies_by_info(&info);
1786 }
1787 }
1788}
1789
1790
1791static const u8 * fst_hostapd_get_sta(struct fst_get_peer_ctx **get_ctx,
Hai Shalome21d4e82020-04-29 16:34:06 -07001792 bool mb_only)
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001793{
1794 struct sta_info *s = (struct sta_info *) *get_ctx;
1795
1796 if (mb_only) {
1797 for (; s && !s->mb_ies; s = s->next)
1798 ;
1799 }
1800
1801 if (s) {
1802 *get_ctx = (struct fst_get_peer_ctx *) s->next;
1803
1804 return s->addr;
1805 }
1806
1807 *get_ctx = NULL;
1808 return NULL;
1809}
1810
1811
1812static const u8 * fst_hostapd_get_peer_first(void *ctx,
1813 struct fst_get_peer_ctx **get_ctx,
Hai Shalome21d4e82020-04-29 16:34:06 -07001814 bool mb_only)
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001815{
1816 struct hostapd_data *hapd = ctx;
1817
1818 *get_ctx = (struct fst_get_peer_ctx *) hapd->sta_list;
1819
1820 return fst_hostapd_get_sta(get_ctx, mb_only);
1821}
1822
1823
1824static const u8 * fst_hostapd_get_peer_next(void *ctx,
1825 struct fst_get_peer_ctx **get_ctx,
Hai Shalome21d4e82020-04-29 16:34:06 -07001826 bool mb_only)
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001827{
1828 return fst_hostapd_get_sta(get_ctx, mb_only);
1829}
1830
1831
1832void fst_hostapd_fill_iface_obj(struct hostapd_data *hapd,
1833 struct fst_wpa_obj *iface_obj)
1834{
1835 iface_obj->ctx = hapd;
1836 iface_obj->get_bssid = fst_hostapd_get_bssid_cb;
1837 iface_obj->get_channel_info = fst_hostapd_get_channel_info_cb;
1838 iface_obj->set_ies = fst_hostapd_set_ies_cb;
1839 iface_obj->send_action = fst_hostapd_send_action_cb;
1840 iface_obj->get_mb_ie = fst_hostapd_get_mb_ie_cb;
1841 iface_obj->update_mb_ie = fst_hostapd_update_mb_ie_cb;
1842 iface_obj->get_peer_first = fst_hostapd_get_peer_first;
1843 iface_obj->get_peer_next = fst_hostapd_get_peer_next;
1844}
1845
1846#endif /* CONFIG_FST */
1847
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001848#ifdef CONFIG_OWE
1849
1850static int hostapd_owe_iface_iter(struct hostapd_iface *iface, void *ctx)
1851{
1852 struct hostapd_data *hapd = ctx;
1853 size_t i;
1854
1855 for (i = 0; i < iface->num_bss; i++) {
1856 struct hostapd_data *bss = iface->bss[i];
1857
1858 if (os_strcmp(hapd->conf->owe_transition_ifname,
1859 bss->conf->iface) != 0)
1860 continue;
1861
1862 wpa_printf(MSG_DEBUG,
1863 "OWE: ifname=%s found transition mode ifname=%s BSSID "
1864 MACSTR " SSID %s",
1865 hapd->conf->iface, bss->conf->iface,
1866 MAC2STR(bss->own_addr),
1867 wpa_ssid_txt(bss->conf->ssid.ssid,
1868 bss->conf->ssid.ssid_len));
1869 if (!bss->conf->ssid.ssid_set || !bss->conf->ssid.ssid_len ||
1870 is_zero_ether_addr(bss->own_addr))
1871 continue;
1872
1873 os_memcpy(hapd->conf->owe_transition_bssid, bss->own_addr,
1874 ETH_ALEN);
1875 os_memcpy(hapd->conf->owe_transition_ssid,
1876 bss->conf->ssid.ssid, bss->conf->ssid.ssid_len);
1877 hapd->conf->owe_transition_ssid_len = bss->conf->ssid.ssid_len;
1878 wpa_printf(MSG_DEBUG,
1879 "OWE: Copied transition mode information");
1880 return 1;
1881 }
1882
1883 return 0;
1884}
1885
1886
1887int hostapd_owe_trans_get_info(struct hostapd_data *hapd)
1888{
1889 if (hapd->conf->owe_transition_ssid_len > 0 &&
1890 !is_zero_ether_addr(hapd->conf->owe_transition_bssid))
1891 return 0;
1892
1893 /* Find transition mode SSID/BSSID information from a BSS operated by
1894 * this hostapd instance. */
1895 if (!hapd->iface->interfaces ||
1896 !hapd->iface->interfaces->for_each_interface)
1897 return hostapd_owe_iface_iter(hapd->iface, hapd);
1898 else
1899 return hapd->iface->interfaces->for_each_interface(
1900 hapd->iface->interfaces, hostapd_owe_iface_iter, hapd);
1901}
1902
1903
1904static int hostapd_owe_iface_iter2(struct hostapd_iface *iface, void *ctx)
1905{
1906 size_t i;
1907
1908 for (i = 0; i < iface->num_bss; i++) {
1909 struct hostapd_data *bss = iface->bss[i];
1910 int res;
1911
1912 if (!bss->conf->owe_transition_ifname[0])
1913 continue;
1914 res = hostapd_owe_trans_get_info(bss);
1915 if (res == 0)
1916 continue;
1917 wpa_printf(MSG_DEBUG,
1918 "OWE: Matching transition mode interface enabled - update beacon data for %s",
1919 bss->conf->iface);
1920 ieee802_11_set_beacon(bss);
1921 }
1922
1923 return 0;
1924}
1925
1926#endif /* CONFIG_OWE */
1927
1928
1929static void hostapd_owe_update_trans(struct hostapd_iface *iface)
1930{
1931#ifdef CONFIG_OWE
1932 /* Check whether the enabled BSS can complete OWE transition mode
1933 * configuration for any pending interface. */
1934 if (!iface->interfaces ||
1935 !iface->interfaces->for_each_interface)
1936 hostapd_owe_iface_iter2(iface, NULL);
1937 else
1938 iface->interfaces->for_each_interface(
1939 iface->interfaces, hostapd_owe_iface_iter2, NULL);
1940#endif /* CONFIG_OWE */
1941}
1942
1943
Roshan Pius3a1667e2018-07-03 15:17:14 -07001944static void hostapd_interface_setup_failure_handler(void *eloop_ctx,
1945 void *timeout_ctx)
1946{
1947 struct hostapd_iface *iface = eloop_ctx;
1948 struct hostapd_data *hapd;
1949
1950 if (iface->num_bss < 1 || !iface->bss || !iface->bss[0])
1951 return;
1952 hapd = iface->bss[0];
1953 if (hapd->setup_complete_cb)
1954 hapd->setup_complete_cb(hapd->setup_complete_cb_ctx);
1955}
1956
1957
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001958static int hostapd_setup_interface_complete_sync(struct hostapd_iface *iface,
1959 int err)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001960{
1961 struct hostapd_data *hapd = iface->bss[0];
1962 size_t j;
1963 u8 *prev_addr;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001964 int delay_apply_cfg = 0;
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001965 int res_dfs_offload = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001966
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001967 if (err)
1968 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001969
1970 wpa_printf(MSG_DEBUG, "Completing interface initialization");
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001971 if (iface->freq) {
Dmitry Shmidt051af732013-10-22 13:52:46 -07001972#ifdef NEED_AP_MLME
1973 int res;
1974#endif /* NEED_AP_MLME */
1975
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001976 wpa_printf(MSG_DEBUG, "Mode: %s Channel: %d "
1977 "Frequency: %d MHz",
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001978 hostapd_hw_mode_txt(iface->conf->hw_mode),
1979 iface->conf->channel, iface->freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001980
Dmitry Shmidt051af732013-10-22 13:52:46 -07001981#ifdef NEED_AP_MLME
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001982 /* Handle DFS only if it is not offloaded to the driver */
1983 if (!(iface->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD)) {
1984 /* Check DFS */
1985 res = hostapd_handle_dfs(iface);
1986 if (res <= 0) {
1987 if (res < 0)
1988 goto fail;
1989 return res;
1990 }
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001991 } else {
1992 /* If DFS is offloaded to the driver */
1993 res_dfs_offload = hostapd_handle_dfs_offload(iface);
1994 if (res_dfs_offload <= 0) {
1995 if (res_dfs_offload < 0)
1996 goto fail;
1997 } else {
1998 wpa_printf(MSG_DEBUG,
1999 "Proceed with AP/channel setup");
2000 /*
2001 * If this is a DFS channel, move to completing
2002 * AP setup.
2003 */
2004 if (res_dfs_offload == 1)
2005 goto dfs_offload;
2006 /* Otherwise fall through. */
2007 }
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002008 }
Dmitry Shmidt051af732013-10-22 13:52:46 -07002009#endif /* NEED_AP_MLME */
2010
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002011#ifdef CONFIG_MESH
2012 if (iface->mconf != NULL) {
2013 wpa_printf(MSG_DEBUG,
2014 "%s: Mesh configuration will be applied while joining the mesh network",
2015 iface->bss[0]->conf->iface);
2016 delay_apply_cfg = 1;
2017 }
2018#endif /* CONFIG_MESH */
2019
2020 if (!delay_apply_cfg &&
2021 hostapd_set_freq(hapd, hapd->iconf->hw_mode, iface->freq,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002022 hapd->iconf->channel,
Hai Shalomc3565922019-10-28 11:58:20 -07002023 hapd->iconf->enable_edmg,
2024 hapd->iconf->edmg_channel,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002025 hapd->iconf->ieee80211n,
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002026 hapd->iconf->ieee80211ac,
Hai Shalom81f62d82019-07-22 12:10:00 -07002027 hapd->iconf->ieee80211ax,
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002028 hapd->iconf->secondary_channel,
Hai Shalom81f62d82019-07-22 12:10:00 -07002029 hostapd_get_oper_chwidth(hapd->iconf),
2030 hostapd_get_oper_centr_freq_seg0_idx(
2031 hapd->iconf),
2032 hostapd_get_oper_centr_freq_seg1_idx(
2033 hapd->iconf))) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002034 wpa_printf(MSG_ERROR, "Could not set channel for "
2035 "kernel driver");
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002036 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002037 }
2038 }
2039
2040 if (iface->current_mode) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002041 if (hostapd_prepare_rates(iface, iface->current_mode)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002042 wpa_printf(MSG_ERROR, "Failed to prepare rates "
2043 "table.");
2044 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
2045 HOSTAPD_LEVEL_WARNING,
2046 "Failed to prepare rates table.");
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002047 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002048 }
2049 }
2050
Hai Shalom021b0b52019-04-10 11:17:58 -07002051 if (hapd->iconf->rts_threshold >= -1 &&
2052 hostapd_set_rts(hapd, hapd->iconf->rts_threshold) &&
2053 hapd->iconf->rts_threshold >= -1) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002054 wpa_printf(MSG_ERROR, "Could not set RTS threshold for "
2055 "kernel driver");
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002056 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002057 }
2058
Hai Shalom021b0b52019-04-10 11:17:58 -07002059 if (hapd->iconf->fragm_threshold >= -1 &&
2060 hostapd_set_frag(hapd, hapd->iconf->fragm_threshold) &&
2061 hapd->iconf->fragm_threshold != -1) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002062 wpa_printf(MSG_ERROR, "Could not set fragmentation threshold "
2063 "for kernel driver");
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002064 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002065 }
2066
2067 prev_addr = hapd->own_addr;
2068
2069 for (j = 0; j < iface->num_bss; j++) {
2070 hapd = iface->bss[j];
2071 if (j)
2072 os_memcpy(hapd->own_addr, prev_addr, ETH_ALEN);
Dmitry Shmidt71757432014-06-02 13:50:35 -07002073 if (hostapd_setup_bss(hapd, j == 0)) {
Hai Shalom021b0b52019-04-10 11:17:58 -07002074 for (;;) {
Dmitry Shmidt71757432014-06-02 13:50:35 -07002075 hapd = iface->bss[j];
2076 hostapd_bss_deinit_no_free(hapd);
2077 hostapd_free_hapd_data(hapd);
Hai Shalom021b0b52019-04-10 11:17:58 -07002078 if (j == 0)
2079 break;
2080 j--;
2081 }
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002082 goto fail;
Dmitry Shmidt71757432014-06-02 13:50:35 -07002083 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002084 if (is_zero_ether_addr(hapd->conf->bssid))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002085 prev_addr = hapd->own_addr;
2086 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002087 hapd = iface->bss[0];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002088
2089 hostapd_tx_queue_params(iface);
2090
2091 ap_list_init(iface);
2092
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07002093 hostapd_set_acl(hapd);
2094
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002095 if (hostapd_driver_commit(hapd) < 0) {
2096 wpa_printf(MSG_ERROR, "%s: Failed to commit driver "
2097 "configuration", __func__);
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002098 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002099 }
2100
Jouni Malinen87fd2792011-05-16 18:35:42 +03002101 /*
2102 * WPS UPnP module can be initialized only when the "upnp_iface" is up.
2103 * If "interface" and "upnp_iface" are the same (e.g., non-bridge
2104 * mode), the interface is up only after driver_commit, so initialize
2105 * WPS after driver_commit.
2106 */
2107 for (j = 0; j < iface->num_bss; j++) {
2108 if (hostapd_init_wps_complete(iface->bss[j]))
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002109 goto fail;
Jouni Malinen87fd2792011-05-16 18:35:42 +03002110 }
2111
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08002112 if ((iface->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
2113 !res_dfs_offload) {
2114 /*
2115 * If freq is DFS, and DFS is offloaded to the driver, then wait
2116 * for CAC to complete.
2117 */
2118 wpa_printf(MSG_DEBUG, "%s: Wait for CAC to complete", __func__);
2119 return res_dfs_offload;
2120 }
2121
2122#ifdef NEED_AP_MLME
2123dfs_offload:
2124#endif /* NEED_AP_MLME */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002125
2126#ifdef CONFIG_FST
2127 if (hapd->iconf->fst_cfg.group_id[0]) {
2128 struct fst_wpa_obj iface_obj;
2129
2130 fst_hostapd_fill_iface_obj(hapd, &iface_obj);
2131 iface->fst = fst_attach(hapd->conf->iface, hapd->own_addr,
2132 &iface_obj, &hapd->iconf->fst_cfg);
2133 if (!iface->fst) {
2134 wpa_printf(MSG_ERROR, "Could not attach to FST %s",
2135 hapd->iconf->fst_cfg.group_id);
2136 goto fail;
2137 }
2138 }
2139#endif /* CONFIG_FST */
2140
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002141 hostapd_set_state(iface, HAPD_IFACE_ENABLED);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002142 hostapd_owe_update_trans(iface);
Hai Shalom81f62d82019-07-22 12:10:00 -07002143 airtime_policy_update_init(iface);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002144 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_ENABLED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002145 if (hapd->setup_complete_cb)
2146 hapd->setup_complete_cb(hapd->setup_complete_cb_ctx);
2147
2148 wpa_printf(MSG_DEBUG, "%s: Setup of interface done.",
2149 iface->bss[0]->conf->iface);
Dmitry Shmidtb96dad42013-11-05 10:07:29 -08002150 if (iface->interfaces && iface->interfaces->terminate_on_error > 0)
2151 iface->interfaces->terminate_on_error--;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002152
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002153 for (j = 0; j < iface->num_bss; j++)
Hai Shalom74f70d42019-02-11 14:42:39 -08002154 hostapd_neighbor_set_own_report(iface->bss[j]);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002155
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002156 return 0;
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002157
2158fail:
2159 wpa_printf(MSG_ERROR, "Interface initialization failed");
2160 hostapd_set_state(iface, HAPD_IFACE_DISABLED);
2161 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002162#ifdef CONFIG_FST
2163 if (iface->fst) {
2164 fst_detach(iface->fst);
2165 iface->fst = NULL;
2166 }
2167#endif /* CONFIG_FST */
Roshan Pius3a1667e2018-07-03 15:17:14 -07002168
2169 if (iface->interfaces && iface->interfaces->terminate_on_error) {
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002170 eloop_terminate();
Roshan Pius3a1667e2018-07-03 15:17:14 -07002171 } else if (hapd->setup_complete_cb) {
2172 /*
2173 * Calling hapd->setup_complete_cb directly may cause iface
2174 * deinitialization which may be accessed later by the caller.
2175 */
2176 eloop_register_timeout(0, 0,
2177 hostapd_interface_setup_failure_handler,
2178 iface, NULL);
2179 }
2180
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002181 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002182}
2183
2184
2185/**
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002186 * hostapd_setup_interface_complete - Complete interface setup
2187 *
2188 * This function is called when previous steps in the interface setup has been
2189 * completed. This can also start operations, e.g., DFS, that will require
2190 * additional processing before interface is ready to be enabled. Such
2191 * operations will call this function from eloop callbacks when finished.
2192 */
2193int hostapd_setup_interface_complete(struct hostapd_iface *iface, int err)
2194{
2195 struct hapd_interfaces *interfaces = iface->interfaces;
2196 struct hostapd_data *hapd = iface->bss[0];
2197 unsigned int i;
2198 int not_ready_in_sync_ifaces = 0;
2199
2200 if (!iface->need_to_start_in_sync)
2201 return hostapd_setup_interface_complete_sync(iface, err);
2202
2203 if (err) {
2204 wpa_printf(MSG_ERROR, "Interface initialization failed");
2205 hostapd_set_state(iface, HAPD_IFACE_DISABLED);
2206 iface->need_to_start_in_sync = 0;
2207 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
2208 if (interfaces && interfaces->terminate_on_error)
2209 eloop_terminate();
2210 return -1;
2211 }
2212
2213 if (iface->ready_to_start_in_sync) {
2214 /* Already in ready and waiting. should never happpen */
2215 return 0;
2216 }
2217
2218 for (i = 0; i < interfaces->count; i++) {
2219 if (interfaces->iface[i]->need_to_start_in_sync &&
2220 !interfaces->iface[i]->ready_to_start_in_sync)
2221 not_ready_in_sync_ifaces++;
2222 }
2223
2224 /*
2225 * Check if this is the last interface, if yes then start all the other
2226 * waiting interfaces. If not, add this interface to the waiting list.
2227 */
2228 if (not_ready_in_sync_ifaces > 1 && iface->state == HAPD_IFACE_DFS) {
2229 /*
2230 * If this interface went through CAC, do not synchronize, just
2231 * start immediately.
2232 */
2233 iface->need_to_start_in_sync = 0;
2234 wpa_printf(MSG_INFO,
2235 "%s: Finished CAC - bypass sync and start interface",
2236 iface->bss[0]->conf->iface);
2237 return hostapd_setup_interface_complete_sync(iface, err);
2238 }
2239
2240 if (not_ready_in_sync_ifaces > 1) {
2241 /* need to wait as there are other interfaces still coming up */
2242 iface->ready_to_start_in_sync = 1;
2243 wpa_printf(MSG_INFO,
2244 "%s: Interface waiting to sync with other interfaces",
2245 iface->bss[0]->conf->iface);
2246 return 0;
2247 }
2248
2249 wpa_printf(MSG_INFO,
2250 "%s: Last interface to sync - starting all interfaces",
2251 iface->bss[0]->conf->iface);
2252 iface->need_to_start_in_sync = 0;
2253 hostapd_setup_interface_complete_sync(iface, err);
2254 for (i = 0; i < interfaces->count; i++) {
2255 if (interfaces->iface[i]->need_to_start_in_sync &&
2256 interfaces->iface[i]->ready_to_start_in_sync) {
2257 hostapd_setup_interface_complete_sync(
2258 interfaces->iface[i], 0);
2259 /* Only once the interfaces are sync started */
2260 interfaces->iface[i]->need_to_start_in_sync = 0;
2261 }
2262 }
2263
2264 return 0;
2265}
2266
2267
2268/**
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002269 * hostapd_setup_interface - Setup of an interface
2270 * @iface: Pointer to interface data.
2271 * Returns: 0 on success, -1 on failure
2272 *
2273 * Initializes the driver interface, validates the configuration,
2274 * and sets driver parameters based on the configuration.
2275 * Flushes old stations, sets the channel, encryption,
2276 * beacons, and WDS links based on the configuration.
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002277 *
2278 * If interface setup requires more time, e.g., to perform HT co-ex scans, ACS,
2279 * or DFS operations, this function returns 0 before such operations have been
2280 * completed. The pending operations are registered into eloop and will be
2281 * completed from eloop callbacks. Those callbacks end up calling
2282 * hostapd_setup_interface_complete() once setup has been completed.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002283 */
2284int hostapd_setup_interface(struct hostapd_iface *iface)
2285{
2286 int ret;
2287
2288 ret = setup_interface(iface);
2289 if (ret) {
2290 wpa_printf(MSG_ERROR, "%s: Unable to setup interface.",
2291 iface->bss[0]->conf->iface);
2292 return -1;
2293 }
2294
2295 return 0;
2296}
2297
2298
2299/**
2300 * hostapd_alloc_bss_data - Allocate and initialize per-BSS data
2301 * @hapd_iface: Pointer to interface data
2302 * @conf: Pointer to per-interface configuration
2303 * @bss: Pointer to per-BSS configuration for this BSS
2304 * Returns: Pointer to allocated BSS data
2305 *
2306 * This function is used to allocate per-BSS data structure. This data will be
2307 * freed after hostapd_cleanup() is called for it during interface
2308 * deinitialization.
2309 */
2310struct hostapd_data *
2311hostapd_alloc_bss_data(struct hostapd_iface *hapd_iface,
2312 struct hostapd_config *conf,
2313 struct hostapd_bss_config *bss)
2314{
2315 struct hostapd_data *hapd;
2316
2317 hapd = os_zalloc(sizeof(*hapd));
2318 if (hapd == NULL)
2319 return NULL;
2320
2321 hapd->new_assoc_sta_cb = hostapd_new_assoc_sta;
2322 hapd->iconf = conf;
2323 hapd->conf = bss;
2324 hapd->iface = hapd_iface;
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08002325 if (conf)
2326 hapd->driver = conf->driver;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002327 hapd->ctrl_sock = -1;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08002328 dl_list_init(&hapd->ctrl_dst);
Dmitry Shmidt7d175302016-09-06 13:11:34 -07002329 dl_list_init(&hapd->nr_db);
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08002330 hapd->dhcp_sock = -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002331#ifdef CONFIG_IEEE80211R_AP
2332 dl_list_init(&hapd->l2_queue);
2333 dl_list_init(&hapd->l2_oui_queue);
2334#endif /* CONFIG_IEEE80211R_AP */
Hai Shalom021b0b52019-04-10 11:17:58 -07002335#ifdef CONFIG_SAE
2336 dl_list_init(&hapd->sae_commit_queue);
2337#endif /* CONFIG_SAE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002338
2339 return hapd;
2340}
2341
2342
Dmitry Shmidt54605472013-11-08 11:10:19 -08002343static void hostapd_bss_deinit(struct hostapd_data *hapd)
2344{
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07002345 if (!hapd)
2346 return;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002347 wpa_printf(MSG_DEBUG, "%s: deinit bss %s", __func__,
Hai Shalom021b0b52019-04-10 11:17:58 -07002348 hapd->conf ? hapd->conf->iface : "N/A");
Dmitry Shmidt71757432014-06-02 13:50:35 -07002349 hostapd_bss_deinit_no_free(hapd);
Dmitry Shmidtf73259c2015-03-17 11:00:54 -07002350 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
Hai Shalomc3565922019-10-28 11:58:20 -07002351#ifdef CONFIG_SQLITE
2352 if (hapd->rad_attr_db) {
2353 sqlite3_close(hapd->rad_attr_db);
2354 hapd->rad_attr_db = NULL;
2355 }
2356#endif /* CONFIG_SQLITE */
Dmitry Shmidt54605472013-11-08 11:10:19 -08002357 hostapd_cleanup(hapd);
2358}
2359
2360
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002361void hostapd_interface_deinit(struct hostapd_iface *iface)
2362{
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002363 int j;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002364
Dmitry Shmidt54605472013-11-08 11:10:19 -08002365 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002366 if (iface == NULL)
2367 return;
2368
Dmitry Shmidtf73259c2015-03-17 11:00:54 -07002369 hostapd_set_state(iface, HAPD_IFACE_DISABLED);
2370
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002371 eloop_cancel_timeout(channel_list_update_timeout, iface, NULL);
2372 iface->wait_channel_update = 0;
2373
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002374#ifdef CONFIG_FST
2375 if (iface->fst) {
2376 fst_detach(iface->fst);
2377 iface->fst = NULL;
2378 }
2379#endif /* CONFIG_FST */
2380
Hai Shalom021b0b52019-04-10 11:17:58 -07002381 for (j = (int) iface->num_bss - 1; j >= 0; j--) {
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07002382 if (!iface->bss)
2383 break;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002384 hostapd_bss_deinit(iface->bss[j]);
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07002385 }
Roshan Pius3a1667e2018-07-03 15:17:14 -07002386
Roshan Pius3a1667e2018-07-03 15:17:14 -07002387#ifdef NEED_AP_MLME
2388 hostapd_stop_setup_timers(iface);
2389 eloop_cancel_timeout(ap_ht2040_timeout, iface, NULL);
2390#endif /* NEED_AP_MLME */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002391}
2392
2393
2394void hostapd_interface_free(struct hostapd_iface *iface)
2395{
2396 size_t j;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002397 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
2398 for (j = 0; j < iface->num_bss; j++) {
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07002399 if (!iface->bss)
2400 break;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002401 wpa_printf(MSG_DEBUG, "%s: free hapd %p",
2402 __func__, iface->bss[j]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002403 os_free(iface->bss[j]);
Dmitry Shmidt54605472013-11-08 11:10:19 -08002404 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002405 hostapd_cleanup_iface(iface);
2406}
2407
2408
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07002409struct hostapd_iface * hostapd_alloc_iface(void)
2410{
2411 struct hostapd_iface *hapd_iface;
2412
2413 hapd_iface = os_zalloc(sizeof(*hapd_iface));
2414 if (!hapd_iface)
2415 return NULL;
2416
2417 dl_list_init(&hapd_iface->sta_seen);
2418
2419 return hapd_iface;
2420}
2421
2422
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002423/**
2424 * hostapd_init - Allocate and initialize per-interface data
2425 * @config_file: Path to the configuration file
2426 * Returns: Pointer to the allocated interface data or %NULL on failure
2427 *
2428 * This function is used to allocate main data structures for per-interface
2429 * data. The allocated data buffer will be freed by calling
2430 * hostapd_cleanup_iface().
2431 */
2432struct hostapd_iface * hostapd_init(struct hapd_interfaces *interfaces,
2433 const char *config_file)
2434{
2435 struct hostapd_iface *hapd_iface = NULL;
2436 struct hostapd_config *conf = NULL;
2437 struct hostapd_data *hapd;
2438 size_t i;
2439
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07002440 hapd_iface = hostapd_alloc_iface();
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002441 if (hapd_iface == NULL)
2442 goto fail;
2443
2444 hapd_iface->config_fname = os_strdup(config_file);
2445 if (hapd_iface->config_fname == NULL)
2446 goto fail;
2447
2448 conf = interfaces->config_read_cb(hapd_iface->config_fname);
2449 if (conf == NULL)
2450 goto fail;
2451 hapd_iface->conf = conf;
2452
2453 hapd_iface->num_bss = conf->num_bss;
2454 hapd_iface->bss = os_calloc(conf->num_bss,
2455 sizeof(struct hostapd_data *));
2456 if (hapd_iface->bss == NULL)
2457 goto fail;
2458
2459 for (i = 0; i < conf->num_bss; i++) {
2460 hapd = hapd_iface->bss[i] =
2461 hostapd_alloc_bss_data(hapd_iface, conf,
2462 conf->bss[i]);
2463 if (hapd == NULL)
2464 goto fail;
2465 hapd->msg_ctx = hapd;
2466 }
2467
2468 return hapd_iface;
2469
2470fail:
2471 wpa_printf(MSG_ERROR, "Failed to set up interface with %s",
2472 config_file);
2473 if (conf)
2474 hostapd_config_free(conf);
2475 if (hapd_iface) {
2476 os_free(hapd_iface->config_fname);
2477 os_free(hapd_iface->bss);
Dmitry Shmidt54605472013-11-08 11:10:19 -08002478 wpa_printf(MSG_DEBUG, "%s: free iface %p",
2479 __func__, hapd_iface);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002480 os_free(hapd_iface);
2481 }
2482 return NULL;
2483}
2484
2485
2486static int ifname_in_use(struct hapd_interfaces *interfaces, const char *ifname)
2487{
2488 size_t i, j;
2489
2490 for (i = 0; i < interfaces->count; i++) {
2491 struct hostapd_iface *iface = interfaces->iface[i];
2492 for (j = 0; j < iface->num_bss; j++) {
2493 struct hostapd_data *hapd = iface->bss[j];
2494 if (os_strcmp(ifname, hapd->conf->iface) == 0)
2495 return 1;
2496 }
2497 }
2498
2499 return 0;
2500}
2501
2502
2503/**
2504 * hostapd_interface_init_bss - Read configuration file and init BSS data
2505 *
2506 * This function is used to parse configuration file for a BSS. This BSS is
2507 * added to an existing interface sharing the same radio (if any) or a new
2508 * interface is created if this is the first interface on a radio. This
2509 * allocate memory for the BSS. No actual driver operations are started.
2510 *
2511 * This is similar to hostapd_interface_init(), but for a case where the
2512 * configuration is used to add a single BSS instead of all BSSes for a radio.
2513 */
2514struct hostapd_iface *
2515hostapd_interface_init_bss(struct hapd_interfaces *interfaces, const char *phy,
2516 const char *config_fname, int debug)
2517{
2518 struct hostapd_iface *new_iface = NULL, *iface = NULL;
2519 struct hostapd_data *hapd;
2520 int k;
2521 size_t i, bss_idx;
2522
2523 if (!phy || !*phy)
2524 return NULL;
2525
2526 for (i = 0; i < interfaces->count; i++) {
2527 if (os_strcmp(interfaces->iface[i]->phy, phy) == 0) {
2528 iface = interfaces->iface[i];
2529 break;
2530 }
2531 }
2532
2533 wpa_printf(MSG_INFO, "Configuration file: %s (phy %s)%s",
2534 config_fname, phy, iface ? "" : " --> new PHY");
2535 if (iface) {
2536 struct hostapd_config *conf;
2537 struct hostapd_bss_config **tmp_conf;
2538 struct hostapd_data **tmp_bss;
2539 struct hostapd_bss_config *bss;
2540 const char *ifname;
2541
2542 /* Add new BSS to existing iface */
2543 conf = interfaces->config_read_cb(config_fname);
2544 if (conf == NULL)
2545 return NULL;
2546 if (conf->num_bss > 1) {
2547 wpa_printf(MSG_ERROR, "Multiple BSSes specified in BSS-config");
2548 hostapd_config_free(conf);
2549 return NULL;
2550 }
2551
2552 ifname = conf->bss[0]->iface;
2553 if (ifname[0] != '\0' && ifname_in_use(interfaces, ifname)) {
2554 wpa_printf(MSG_ERROR,
2555 "Interface name %s already in use", ifname);
2556 hostapd_config_free(conf);
2557 return NULL;
2558 }
2559
2560 tmp_conf = os_realloc_array(
2561 iface->conf->bss, iface->conf->num_bss + 1,
2562 sizeof(struct hostapd_bss_config *));
2563 tmp_bss = os_realloc_array(iface->bss, iface->num_bss + 1,
2564 sizeof(struct hostapd_data *));
2565 if (tmp_bss)
2566 iface->bss = tmp_bss;
2567 if (tmp_conf) {
2568 iface->conf->bss = tmp_conf;
2569 iface->conf->last_bss = tmp_conf[0];
2570 }
2571 if (tmp_bss == NULL || tmp_conf == NULL) {
2572 hostapd_config_free(conf);
2573 return NULL;
2574 }
2575 bss = iface->conf->bss[iface->conf->num_bss] = conf->bss[0];
2576 iface->conf->num_bss++;
2577
2578 hapd = hostapd_alloc_bss_data(iface, iface->conf, bss);
2579 if (hapd == NULL) {
2580 iface->conf->num_bss--;
2581 hostapd_config_free(conf);
2582 return NULL;
2583 }
2584 iface->conf->last_bss = bss;
2585 iface->bss[iface->num_bss] = hapd;
2586 hapd->msg_ctx = hapd;
2587
2588 bss_idx = iface->num_bss++;
2589 conf->num_bss--;
2590 conf->bss[0] = NULL;
2591 hostapd_config_free(conf);
2592 } else {
2593 /* Add a new iface with the first BSS */
2594 new_iface = iface = hostapd_init(interfaces, config_fname);
2595 if (!iface)
2596 return NULL;
2597 os_strlcpy(iface->phy, phy, sizeof(iface->phy));
2598 iface->interfaces = interfaces;
2599 bss_idx = 0;
2600 }
2601
2602 for (k = 0; k < debug; k++) {
2603 if (iface->bss[bss_idx]->conf->logger_stdout_level > 0)
2604 iface->bss[bss_idx]->conf->logger_stdout_level--;
2605 }
2606
2607 if (iface->conf->bss[bss_idx]->iface[0] == '\0' &&
2608 !hostapd_drv_none(iface->bss[bss_idx])) {
2609 wpa_printf(MSG_ERROR, "Interface name not specified in %s",
2610 config_fname);
2611 if (new_iface)
2612 hostapd_interface_deinit_free(new_iface);
2613 return NULL;
2614 }
2615
2616 return iface;
2617}
2618
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002619
2620void hostapd_interface_deinit_free(struct hostapd_iface *iface)
2621{
2622 const struct wpa_driver_ops *driver;
2623 void *drv_priv;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002624
2625 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002626 if (iface == NULL)
2627 return;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002628 wpa_printf(MSG_DEBUG, "%s: num_bss=%u conf->num_bss=%u",
2629 __func__, (unsigned int) iface->num_bss,
2630 (unsigned int) iface->conf->num_bss);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002631 driver = iface->bss[0]->driver;
2632 drv_priv = iface->bss[0]->drv_priv;
2633 hostapd_interface_deinit(iface);
Dmitry Shmidt54605472013-11-08 11:10:19 -08002634 wpa_printf(MSG_DEBUG, "%s: driver=%p drv_priv=%p -> hapd_deinit",
2635 __func__, driver, drv_priv);
Dmitry Shmidt71757432014-06-02 13:50:35 -07002636 if (driver && driver->hapd_deinit && drv_priv) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002637 driver->hapd_deinit(drv_priv);
Dmitry Shmidt71757432014-06-02 13:50:35 -07002638 iface->bss[0]->drv_priv = NULL;
2639 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002640 hostapd_interface_free(iface);
2641}
2642
2643
Dmitry Shmidt15907092014-03-25 10:42:57 -07002644static void hostapd_deinit_driver(const struct wpa_driver_ops *driver,
2645 void *drv_priv,
2646 struct hostapd_iface *hapd_iface)
2647{
2648 size_t j;
2649
2650 wpa_printf(MSG_DEBUG, "%s: driver=%p drv_priv=%p -> hapd_deinit",
2651 __func__, driver, drv_priv);
2652 if (driver && driver->hapd_deinit && drv_priv) {
2653 driver->hapd_deinit(drv_priv);
2654 for (j = 0; j < hapd_iface->num_bss; j++) {
2655 wpa_printf(MSG_DEBUG, "%s:bss[%d]->drv_priv=%p",
2656 __func__, (int) j,
2657 hapd_iface->bss[j]->drv_priv);
Hai Shalom1dc4d202019-04-29 16:22:27 -07002658 if (hapd_iface->bss[j]->drv_priv == drv_priv) {
Dmitry Shmidt15907092014-03-25 10:42:57 -07002659 hapd_iface->bss[j]->drv_priv = NULL;
Hai Shalom1dc4d202019-04-29 16:22:27 -07002660 hapd_iface->extended_capa = NULL;
2661 hapd_iface->extended_capa_mask = NULL;
2662 hapd_iface->extended_capa_len = 0;
2663 }
Dmitry Shmidt15907092014-03-25 10:42:57 -07002664 }
2665 }
2666}
2667
2668
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002669int hostapd_enable_iface(struct hostapd_iface *hapd_iface)
2670{
Dmitry Shmidt71757432014-06-02 13:50:35 -07002671 size_t j;
2672
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002673 if (hapd_iface->bss[0]->drv_priv != NULL) {
2674 wpa_printf(MSG_ERROR, "Interface %s already enabled",
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002675 hapd_iface->conf->bss[0]->iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002676 return -1;
2677 }
2678
2679 wpa_printf(MSG_DEBUG, "Enable interface %s",
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002680 hapd_iface->conf->bss[0]->iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002681
Dmitry Shmidt71757432014-06-02 13:50:35 -07002682 for (j = 0; j < hapd_iface->num_bss; j++)
2683 hostapd_set_security_params(hapd_iface->conf->bss[j], 1);
Dmitry Shmidt344abd32014-01-14 13:17:00 -08002684 if (hostapd_config_check(hapd_iface->conf, 1) < 0) {
2685 wpa_printf(MSG_INFO, "Invalid configuration - cannot enable");
2686 return -1;
2687 }
2688
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002689 if (hapd_iface->interfaces == NULL ||
2690 hapd_iface->interfaces->driver_init == NULL ||
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002691 hapd_iface->interfaces->driver_init(hapd_iface))
2692 return -1;
2693
2694 if (hostapd_setup_interface(hapd_iface)) {
Dmitry Shmidt15907092014-03-25 10:42:57 -07002695 hostapd_deinit_driver(hapd_iface->bss[0]->driver,
2696 hapd_iface->bss[0]->drv_priv,
2697 hapd_iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002698 return -1;
2699 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002700
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002701 return 0;
2702}
2703
2704
2705int hostapd_reload_iface(struct hostapd_iface *hapd_iface)
2706{
2707 size_t j;
2708
2709 wpa_printf(MSG_DEBUG, "Reload interface %s",
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002710 hapd_iface->conf->bss[0]->iface);
2711 for (j = 0; j < hapd_iface->num_bss; j++)
Dmitry Shmidt71757432014-06-02 13:50:35 -07002712 hostapd_set_security_params(hapd_iface->conf->bss[j], 1);
Dmitry Shmidt344abd32014-01-14 13:17:00 -08002713 if (hostapd_config_check(hapd_iface->conf, 1) < 0) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002714 wpa_printf(MSG_ERROR, "Updated configuration is invalid");
2715 return -1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002716 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002717 hostapd_clear_old(hapd_iface);
2718 for (j = 0; j < hapd_iface->num_bss; j++)
2719 hostapd_reload_bss(hapd_iface->bss[j]);
2720
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002721 return 0;
2722}
2723
2724
2725int hostapd_disable_iface(struct hostapd_iface *hapd_iface)
2726{
2727 size_t j;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002728 const struct wpa_driver_ops *driver;
2729 void *drv_priv;
2730
2731 if (hapd_iface == NULL)
2732 return -1;
Dmitry Shmidt71757432014-06-02 13:50:35 -07002733
2734 if (hapd_iface->bss[0]->drv_priv == NULL) {
2735 wpa_printf(MSG_INFO, "Interface %s already disabled",
2736 hapd_iface->conf->bss[0]->iface);
2737 return -1;
2738 }
2739
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002740 wpa_msg(hapd_iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002741 driver = hapd_iface->bss[0]->driver;
2742 drv_priv = hapd_iface->bss[0]->drv_priv;
2743
Dmitry Shmidta38abf92014-03-06 13:38:44 -08002744 hapd_iface->driver_ap_teardown =
2745 !!(hapd_iface->drv_flags &
2746 WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
2747
Hai Shalom39ba6fc2019-01-22 12:40:38 -08002748#ifdef NEED_AP_MLME
2749 for (j = 0; j < hapd_iface->num_bss; j++)
2750 hostapd_cleanup_cs_params(hapd_iface->bss[j]);
2751#endif /* NEED_AP_MLME */
2752
Dmitry Shmidta38abf92014-03-06 13:38:44 -08002753 /* same as hostapd_interface_deinit without deinitializing ctrl-iface */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002754 for (j = 0; j < hapd_iface->num_bss; j++) {
2755 struct hostapd_data *hapd = hapd_iface->bss[j];
Dmitry Shmidt71757432014-06-02 13:50:35 -07002756 hostapd_bss_deinit_no_free(hapd);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002757 hostapd_free_hapd_data(hapd);
2758 }
2759
Dmitry Shmidt15907092014-03-25 10:42:57 -07002760 hostapd_deinit_driver(driver, drv_priv, hapd_iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002761
2762 /* From hostapd_cleanup_iface: These were initialized in
2763 * hostapd_setup_interface and hostapd_setup_interface_complete
2764 */
2765 hostapd_cleanup_iface_partial(hapd_iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002766
Dmitry Shmidt56052862013-10-04 10:23:25 -07002767 wpa_printf(MSG_DEBUG, "Interface %s disabled",
2768 hapd_iface->bss[0]->conf->iface);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002769 hostapd_set_state(hapd_iface, HAPD_IFACE_DISABLED);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002770 return 0;
2771}
2772
2773
2774static struct hostapd_iface *
2775hostapd_iface_alloc(struct hapd_interfaces *interfaces)
2776{
2777 struct hostapd_iface **iface, *hapd_iface;
2778
2779 iface = os_realloc_array(interfaces->iface, interfaces->count + 1,
2780 sizeof(struct hostapd_iface *));
2781 if (iface == NULL)
2782 return NULL;
2783 interfaces->iface = iface;
2784 hapd_iface = interfaces->iface[interfaces->count] =
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07002785 hostapd_alloc_iface();
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002786 if (hapd_iface == NULL) {
2787 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory for "
2788 "the interface", __func__);
2789 return NULL;
2790 }
2791 interfaces->count++;
2792 hapd_iface->interfaces = interfaces;
2793
2794 return hapd_iface;
2795}
2796
2797
2798static struct hostapd_config *
2799hostapd_config_alloc(struct hapd_interfaces *interfaces, const char *ifname,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002800 const char *ctrl_iface, const char *driver)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002801{
2802 struct hostapd_bss_config *bss;
2803 struct hostapd_config *conf;
2804
2805 /* Allocates memory for bss and conf */
2806 conf = hostapd_config_defaults();
2807 if (conf == NULL) {
2808 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory for "
2809 "configuration", __func__);
Hai Shalom74f70d42019-02-11 14:42:39 -08002810 return NULL;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002811 }
2812
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002813 if (driver) {
2814 int j;
2815
2816 for (j = 0; wpa_drivers[j]; j++) {
2817 if (os_strcmp(driver, wpa_drivers[j]->name) == 0) {
2818 conf->driver = wpa_drivers[j];
2819 goto skip;
2820 }
2821 }
2822
2823 wpa_printf(MSG_ERROR,
2824 "Invalid/unknown driver '%s' - registering the default driver",
2825 driver);
2826 }
2827
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002828 conf->driver = wpa_drivers[0];
2829 if (conf->driver == NULL) {
2830 wpa_printf(MSG_ERROR, "No driver wrappers registered!");
2831 hostapd_config_free(conf);
2832 return NULL;
2833 }
2834
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002835skip:
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002836 bss = conf->last_bss = conf->bss[0];
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002837
2838 os_strlcpy(bss->iface, ifname, sizeof(bss->iface));
2839 bss->ctrl_interface = os_strdup(ctrl_iface);
2840 if (bss->ctrl_interface == NULL) {
2841 hostapd_config_free(conf);
2842 return NULL;
2843 }
2844
2845 /* Reading configuration file skipped, will be done in SET!
2846 * From reading the configuration till the end has to be done in
2847 * SET
2848 */
2849 return conf;
2850}
2851
2852
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002853static int hostapd_data_alloc(struct hostapd_iface *hapd_iface,
2854 struct hostapd_config *conf)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002855{
2856 size_t i;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002857 struct hostapd_data *hapd;
2858
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002859 hapd_iface->bss = os_calloc(conf->num_bss,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002860 sizeof(struct hostapd_data *));
2861 if (hapd_iface->bss == NULL)
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002862 return -1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002863
2864 for (i = 0; i < conf->num_bss; i++) {
2865 hapd = hapd_iface->bss[i] =
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002866 hostapd_alloc_bss_data(hapd_iface, conf, conf->bss[i]);
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002867 if (hapd == NULL) {
2868 while (i > 0) {
2869 i--;
2870 os_free(hapd_iface->bss[i]);
2871 hapd_iface->bss[i] = NULL;
2872 }
2873 os_free(hapd_iface->bss);
2874 hapd_iface->bss = NULL;
2875 return -1;
2876 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002877 hapd->msg_ctx = hapd;
2878 }
2879
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002880 hapd_iface->conf = conf;
2881 hapd_iface->num_bss = conf->num_bss;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002882
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002883 return 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002884}
2885
2886
2887int hostapd_add_iface(struct hapd_interfaces *interfaces, char *buf)
2888{
2889 struct hostapd_config *conf = NULL;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002890 struct hostapd_iface *hapd_iface = NULL, *new_iface = NULL;
2891 struct hostapd_data *hapd;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002892 char *ptr;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002893 size_t i, j;
2894 const char *conf_file = NULL, *phy_name = NULL;
2895
2896 if (os_strncmp(buf, "bss_config=", 11) == 0) {
2897 char *pos;
2898 phy_name = buf + 11;
2899 pos = os_strchr(phy_name, ':');
2900 if (!pos)
2901 return -1;
2902 *pos++ = '\0';
2903 conf_file = pos;
2904 if (!os_strlen(conf_file))
2905 return -1;
2906
2907 hapd_iface = hostapd_interface_init_bss(interfaces, phy_name,
2908 conf_file, 0);
2909 if (!hapd_iface)
2910 return -1;
2911 for (j = 0; j < interfaces->count; j++) {
2912 if (interfaces->iface[j] == hapd_iface)
2913 break;
2914 }
2915 if (j == interfaces->count) {
2916 struct hostapd_iface **tmp;
2917 tmp = os_realloc_array(interfaces->iface,
2918 interfaces->count + 1,
2919 sizeof(struct hostapd_iface *));
2920 if (!tmp) {
2921 hostapd_interface_deinit_free(hapd_iface);
2922 return -1;
2923 }
2924 interfaces->iface = tmp;
2925 interfaces->iface[interfaces->count++] = hapd_iface;
2926 new_iface = hapd_iface;
2927 }
2928
2929 if (new_iface) {
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002930 if (interfaces->driver_init(hapd_iface))
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002931 goto fail;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002932
2933 if (hostapd_setup_interface(hapd_iface)) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002934 hostapd_deinit_driver(
2935 hapd_iface->bss[0]->driver,
2936 hapd_iface->bss[0]->drv_priv,
2937 hapd_iface);
2938 goto fail;
2939 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002940 } else {
2941 /* Assign new BSS with bss[0]'s driver info */
2942 hapd = hapd_iface->bss[hapd_iface->num_bss - 1];
2943 hapd->driver = hapd_iface->bss[0]->driver;
2944 hapd->drv_priv = hapd_iface->bss[0]->drv_priv;
2945 os_memcpy(hapd->own_addr, hapd_iface->bss[0]->own_addr,
2946 ETH_ALEN);
2947
2948 if (start_ctrl_iface_bss(hapd) < 0 ||
Dmitry Shmidt54605472013-11-08 11:10:19 -08002949 (hapd_iface->state == HAPD_IFACE_ENABLED &&
2950 hostapd_setup_bss(hapd, -1))) {
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002951 hostapd_cleanup(hapd);
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08002952 hapd_iface->bss[hapd_iface->num_bss - 1] = NULL;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002953 hapd_iface->conf->num_bss--;
2954 hapd_iface->num_bss--;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002955 wpa_printf(MSG_DEBUG, "%s: free hapd %p %s",
2956 __func__, hapd, hapd->conf->iface);
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002957 hostapd_config_free_bss(hapd->conf);
2958 hapd->conf = NULL;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002959 os_free(hapd);
2960 return -1;
2961 }
2962 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002963 hostapd_owe_update_trans(hapd_iface);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002964 return 0;
2965 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002966
2967 ptr = os_strchr(buf, ' ');
2968 if (ptr == NULL)
2969 return -1;
2970 *ptr++ = '\0';
2971
Dmitry Shmidt56052862013-10-04 10:23:25 -07002972 if (os_strncmp(ptr, "config=", 7) == 0)
2973 conf_file = ptr + 7;
2974
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002975 for (i = 0; i < interfaces->count; i++) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002976 if (!os_strcmp(interfaces->iface[i]->conf->bss[0]->iface,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002977 buf)) {
2978 wpa_printf(MSG_INFO, "Cannot add interface - it "
2979 "already exists");
2980 return -1;
2981 }
2982 }
2983
2984 hapd_iface = hostapd_iface_alloc(interfaces);
2985 if (hapd_iface == NULL) {
2986 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
2987 "for interface", __func__);
2988 goto fail;
2989 }
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002990 new_iface = hapd_iface;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002991
Dmitry Shmidt56052862013-10-04 10:23:25 -07002992 if (conf_file && interfaces->config_read_cb) {
2993 conf = interfaces->config_read_cb(conf_file);
2994 if (conf && conf->bss)
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002995 os_strlcpy(conf->bss[0]->iface, buf,
2996 sizeof(conf->bss[0]->iface));
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002997 } else {
2998 char *driver = os_strchr(ptr, ' ');
2999
3000 if (driver)
3001 *driver++ = '\0';
3002 conf = hostapd_config_alloc(interfaces, buf, ptr, driver);
3003 }
3004
Dmitry Shmidt56052862013-10-04 10:23:25 -07003005 if (conf == NULL || conf->bss == NULL) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003006 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
3007 "for configuration", __func__);
3008 goto fail;
3009 }
3010
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003011 if (hostapd_data_alloc(hapd_iface, conf) < 0) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003012 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
3013 "for hostapd", __func__);
3014 goto fail;
3015 }
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003016 conf = NULL;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003017
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003018 if (start_ctrl_iface(hapd_iface) < 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003019 goto fail;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003020
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003021 wpa_printf(MSG_INFO, "Add interface '%s'",
3022 hapd_iface->conf->bss[0]->iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003023
3024 return 0;
3025
3026fail:
3027 if (conf)
3028 hostapd_config_free(conf);
3029 if (hapd_iface) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003030 if (hapd_iface->bss) {
Dmitry Shmidt54605472013-11-08 11:10:19 -08003031 for (i = 0; i < hapd_iface->num_bss; i++) {
3032 hapd = hapd_iface->bss[i];
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08003033 if (!hapd)
3034 continue;
3035 if (hapd_iface->interfaces &&
Dmitry Shmidt54605472013-11-08 11:10:19 -08003036 hapd_iface->interfaces->ctrl_iface_deinit)
3037 hapd_iface->interfaces->
3038 ctrl_iface_deinit(hapd);
3039 wpa_printf(MSG_DEBUG, "%s: free hapd %p (%s)",
3040 __func__, hapd_iface->bss[i],
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08003041 hapd->conf->iface);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003042 hostapd_cleanup(hapd);
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08003043 os_free(hapd);
3044 hapd_iface->bss[i] = NULL;
Dmitry Shmidt54605472013-11-08 11:10:19 -08003045 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003046 os_free(hapd_iface->bss);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003047 hapd_iface->bss = NULL;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003048 }
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003049 if (new_iface) {
3050 interfaces->count--;
3051 interfaces->iface[interfaces->count] = NULL;
3052 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003053 hostapd_cleanup_iface(hapd_iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003054 }
3055 return -1;
3056}
3057
3058
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003059static int hostapd_remove_bss(struct hostapd_iface *iface, unsigned int idx)
3060{
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003061 size_t i;
3062
Dmitry Shmidt54605472013-11-08 11:10:19 -08003063 wpa_printf(MSG_INFO, "Remove BSS '%s'", iface->conf->bss[idx]->iface);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003064
Dmitry Shmidt54605472013-11-08 11:10:19 -08003065 /* Remove hostapd_data only if it has already been initialized */
3066 if (idx < iface->num_bss) {
3067 struct hostapd_data *hapd = iface->bss[idx];
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003068
Dmitry Shmidt54605472013-11-08 11:10:19 -08003069 hostapd_bss_deinit(hapd);
3070 wpa_printf(MSG_DEBUG, "%s: free hapd %p (%s)",
3071 __func__, hapd, hapd->conf->iface);
3072 hostapd_config_free_bss(hapd->conf);
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003073 hapd->conf = NULL;
Dmitry Shmidt54605472013-11-08 11:10:19 -08003074 os_free(hapd);
3075
3076 iface->num_bss--;
3077
3078 for (i = idx; i < iface->num_bss; i++)
3079 iface->bss[i] = iface->bss[i + 1];
3080 } else {
3081 hostapd_config_free_bss(iface->conf->bss[idx]);
3082 iface->conf->bss[idx] = NULL;
3083 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003084
3085 iface->conf->num_bss--;
Dmitry Shmidt54605472013-11-08 11:10:19 -08003086 for (i = idx; i < iface->conf->num_bss; i++)
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003087 iface->conf->bss[i] = iface->conf->bss[i + 1];
3088
3089 return 0;
3090}
3091
3092
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003093int hostapd_remove_iface(struct hapd_interfaces *interfaces, char *buf)
3094{
3095 struct hostapd_iface *hapd_iface;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003096 size_t i, j, k = 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003097
3098 for (i = 0; i < interfaces->count; i++) {
3099 hapd_iface = interfaces->iface[i];
3100 if (hapd_iface == NULL)
3101 return -1;
Dmitry Shmidt54605472013-11-08 11:10:19 -08003102 if (!os_strcmp(hapd_iface->conf->bss[0]->iface, buf)) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003103 wpa_printf(MSG_INFO, "Remove interface '%s'", buf);
Dmitry Shmidta38abf92014-03-06 13:38:44 -08003104 hapd_iface->driver_ap_teardown =
3105 !!(hapd_iface->drv_flags &
3106 WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
3107
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003108 hostapd_interface_deinit_free(hapd_iface);
3109 k = i;
3110 while (k < (interfaces->count - 1)) {
3111 interfaces->iface[k] =
3112 interfaces->iface[k + 1];
3113 k++;
3114 }
3115 interfaces->count--;
3116 return 0;
3117 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003118
3119 for (j = 0; j < hapd_iface->conf->num_bss; j++) {
Dmitry Shmidta38abf92014-03-06 13:38:44 -08003120 if (!os_strcmp(hapd_iface->conf->bss[j]->iface, buf)) {
3121 hapd_iface->driver_ap_teardown =
3122 !(hapd_iface->drv_flags &
3123 WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003124 return hostapd_remove_bss(hapd_iface, j);
Dmitry Shmidta38abf92014-03-06 13:38:44 -08003125 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003126 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003127 }
3128 return -1;
3129}
3130
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003131
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003132/**
3133 * hostapd_new_assoc_sta - Notify that a new station associated with the AP
3134 * @hapd: Pointer to BSS data
3135 * @sta: Pointer to the associated STA data
3136 * @reassoc: 1 to indicate this was a re-association; 0 = first association
3137 *
3138 * This function will be called whenever a station associates with the AP. It
3139 * can be called from ieee802_11.c for drivers that export MLME to hostapd and
3140 * from drv_callbacks.c based on driver events for drivers that take care of
3141 * management frames (IEEE 802.11 authentication and association) internally.
3142 */
3143void hostapd_new_assoc_sta(struct hostapd_data *hapd, struct sta_info *sta,
3144 int reassoc)
3145{
3146 if (hapd->tkip_countermeasures) {
3147 hostapd_drv_sta_deauth(hapd, sta->addr,
3148 WLAN_REASON_MICHAEL_MIC_FAILURE);
3149 return;
3150 }
3151
3152 hostapd_prune_associations(hapd, sta->addr);
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -08003153 ap_sta_clear_disconnect_timeouts(hapd, sta);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003154
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003155#ifdef CONFIG_P2P
3156 if (sta->p2p_ie == NULL && !sta->no_p2p_set) {
3157 sta->no_p2p_set = 1;
3158 hapd->num_sta_no_p2p++;
3159 if (hapd->num_sta_no_p2p == 1)
3160 hostapd_p2p_non_p2p_sta_connected(hapd);
3161 }
3162#endif /* CONFIG_P2P */
3163
Hai Shalom81f62d82019-07-22 12:10:00 -07003164 airtime_policy_new_sta(hapd, sta);
3165
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003166 /* Start accounting here, if IEEE 802.1X and WPA are not used.
3167 * IEEE 802.1X/WPA code will start accounting after the station has
3168 * been authorized. */
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -08003169 if (!hapd->conf->ieee802_1x && !hapd->conf->wpa && !hapd->conf->osen) {
3170 ap_sta_set_authorized(hapd, sta, 1);
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08003171 os_get_reltime(&sta->connected_time);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003172 accounting_sta_start(hapd, sta);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003173 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003174
3175 /* Start IEEE 802.1X authentication process for new stations */
3176 ieee802_1x_new_station(hapd, sta);
3177 if (reassoc) {
3178 if (sta->auth_alg != WLAN_AUTH_FT &&
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003179 sta->auth_alg != WLAN_AUTH_FILS_SK &&
3180 sta->auth_alg != WLAN_AUTH_FILS_SK_PFS &&
3181 sta->auth_alg != WLAN_AUTH_FILS_PK &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003182 !(sta->flags & (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS)))
3183 wpa_auth_sm_event(sta->wpa_sm, WPA_REAUTH);
3184 } else
3185 wpa_auth_sta_associated(hapd->wpa_auth, sta->wpa_sm);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003186
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08003187 if (hapd->iface->drv_flags & WPA_DRIVER_FLAGS_WIRED) {
3188 if (eloop_cancel_timeout(ap_handle_timer, hapd, sta) > 0) {
3189 wpa_printf(MSG_DEBUG,
3190 "%s: %s: canceled wired ap_handle_timer timeout for "
3191 MACSTR,
3192 hapd->conf->iface, __func__,
3193 MAC2STR(sta->addr));
3194 }
3195 } else if (!(hapd->iface->drv_flags &
3196 WPA_DRIVER_FLAGS_INACTIVITY_TIMER)) {
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -08003197 wpa_printf(MSG_DEBUG,
3198 "%s: %s: reschedule ap_handle_timer timeout for "
3199 MACSTR " (%d seconds - ap_max_inactivity)",
3200 hapd->conf->iface, __func__, MAC2STR(sta->addr),
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08003201 hapd->conf->ap_max_inactivity);
3202 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
3203 eloop_register_timeout(hapd->conf->ap_max_inactivity, 0,
3204 ap_handle_timer, hapd, sta);
3205 }
Hai Shalom81f62d82019-07-22 12:10:00 -07003206
3207#ifdef CONFIG_MACSEC
3208 if (hapd->conf->wpa_key_mgmt == WPA_KEY_MGMT_NONE &&
3209 hapd->conf->mka_psk_set)
3210 ieee802_1x_create_preshared_mka_hapd(hapd, sta);
3211 else
3212 ieee802_1x_alloc_kay_sm_hapd(hapd, sta);
3213#endif /* CONFIG_MACSEC */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003214}
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003215
3216
3217const char * hostapd_state_text(enum hostapd_iface_state s)
3218{
3219 switch (s) {
3220 case HAPD_IFACE_UNINITIALIZED:
3221 return "UNINITIALIZED";
3222 case HAPD_IFACE_DISABLED:
3223 return "DISABLED";
3224 case HAPD_IFACE_COUNTRY_UPDATE:
3225 return "COUNTRY_UPDATE";
3226 case HAPD_IFACE_ACS:
3227 return "ACS";
3228 case HAPD_IFACE_HT_SCAN:
3229 return "HT_SCAN";
3230 case HAPD_IFACE_DFS:
3231 return "DFS";
3232 case HAPD_IFACE_ENABLED:
3233 return "ENABLED";
3234 }
3235
3236 return "UNKNOWN";
3237}
3238
3239
3240void hostapd_set_state(struct hostapd_iface *iface, enum hostapd_iface_state s)
3241{
3242 wpa_printf(MSG_INFO, "%s: interface state %s->%s",
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07003243 iface->conf ? iface->conf->bss[0]->iface : "N/A",
3244 hostapd_state_text(iface->state), hostapd_state_text(s));
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003245 iface->state = s;
3246}
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003247
3248
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003249int hostapd_csa_in_progress(struct hostapd_iface *iface)
3250{
3251 unsigned int i;
3252
3253 for (i = 0; i < iface->num_bss; i++)
3254 if (iface->bss[i]->csa_in_progress)
3255 return 1;
3256 return 0;
3257}
3258
3259
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003260#ifdef NEED_AP_MLME
3261
3262static void free_beacon_data(struct beacon_data *beacon)
3263{
3264 os_free(beacon->head);
3265 beacon->head = NULL;
3266 os_free(beacon->tail);
3267 beacon->tail = NULL;
3268 os_free(beacon->probe_resp);
3269 beacon->probe_resp = NULL;
3270 os_free(beacon->beacon_ies);
3271 beacon->beacon_ies = NULL;
3272 os_free(beacon->proberesp_ies);
3273 beacon->proberesp_ies = NULL;
3274 os_free(beacon->assocresp_ies);
3275 beacon->assocresp_ies = NULL;
3276}
3277
3278
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003279static int hostapd_build_beacon_data(struct hostapd_data *hapd,
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003280 struct beacon_data *beacon)
3281{
3282 struct wpabuf *beacon_extra, *proberesp_extra, *assocresp_extra;
3283 struct wpa_driver_ap_params params;
3284 int ret;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003285
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08003286 os_memset(beacon, 0, sizeof(*beacon));
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003287 ret = ieee802_11_build_ap_params(hapd, &params);
3288 if (ret < 0)
3289 return ret;
3290
3291 ret = hostapd_build_ap_extra_ies(hapd, &beacon_extra,
3292 &proberesp_extra,
3293 &assocresp_extra);
3294 if (ret)
3295 goto free_ap_params;
3296
3297 ret = -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003298 beacon->head = os_memdup(params.head, params.head_len);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003299 if (!beacon->head)
3300 goto free_ap_extra_ies;
3301
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003302 beacon->head_len = params.head_len;
3303
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003304 beacon->tail = os_memdup(params.tail, params.tail_len);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003305 if (!beacon->tail)
3306 goto free_beacon;
3307
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003308 beacon->tail_len = params.tail_len;
3309
3310 if (params.proberesp != NULL) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003311 beacon->probe_resp = os_memdup(params.proberesp,
3312 params.proberesp_len);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003313 if (!beacon->probe_resp)
3314 goto free_beacon;
3315
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003316 beacon->probe_resp_len = params.proberesp_len;
3317 }
3318
3319 /* copy the extra ies */
3320 if (beacon_extra) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003321 beacon->beacon_ies = os_memdup(beacon_extra->buf,
3322 wpabuf_len(beacon_extra));
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003323 if (!beacon->beacon_ies)
3324 goto free_beacon;
3325
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003326 beacon->beacon_ies_len = wpabuf_len(beacon_extra);
3327 }
3328
3329 if (proberesp_extra) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003330 beacon->proberesp_ies = os_memdup(proberesp_extra->buf,
3331 wpabuf_len(proberesp_extra));
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003332 if (!beacon->proberesp_ies)
3333 goto free_beacon;
3334
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003335 beacon->proberesp_ies_len = wpabuf_len(proberesp_extra);
3336 }
3337
3338 if (assocresp_extra) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003339 beacon->assocresp_ies = os_memdup(assocresp_extra->buf,
3340 wpabuf_len(assocresp_extra));
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003341 if (!beacon->assocresp_ies)
3342 goto free_beacon;
3343
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003344 beacon->assocresp_ies_len = wpabuf_len(assocresp_extra);
3345 }
3346
3347 ret = 0;
3348free_beacon:
3349 /* if the function fails, the caller should not free beacon data */
3350 if (ret)
3351 free_beacon_data(beacon);
3352
3353free_ap_extra_ies:
3354 hostapd_free_ap_extra_ies(hapd, beacon_extra, proberesp_extra,
3355 assocresp_extra);
3356free_ap_params:
3357 ieee802_11_free_ap_params(&params);
3358 return ret;
3359}
3360
3361
3362/*
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003363 * TODO: This flow currently supports only changing channel and width within
3364 * the same hw_mode. Any other changes to MAC parameters or provided settings
3365 * are not supported.
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003366 */
3367static int hostapd_change_config_freq(struct hostapd_data *hapd,
3368 struct hostapd_config *conf,
3369 struct hostapd_freq_params *params,
3370 struct hostapd_freq_params *old_params)
3371{
3372 int channel;
Hai Shalom81f62d82019-07-22 12:10:00 -07003373 u8 seg0, seg1;
3374 struct hostapd_hw_modes *mode;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003375
3376 if (!params->channel) {
3377 /* check if the new channel is supported by hw */
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003378 params->channel = hostapd_hw_get_channel(hapd, params->freq);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003379 }
3380
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003381 channel = params->channel;
3382 if (!channel)
3383 return -1;
3384
Hai Shalom81f62d82019-07-22 12:10:00 -07003385 mode = hapd->iface->current_mode;
3386
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003387 /* if a pointer to old_params is provided we save previous state */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003388 if (old_params &&
3389 hostapd_set_freq_params(old_params, conf->hw_mode,
3390 hostapd_hw_get_freq(hapd, conf->channel),
Hai Shalomc3565922019-10-28 11:58:20 -07003391 conf->channel, conf->enable_edmg,
3392 conf->edmg_channel, conf->ieee80211n,
Hai Shalom81f62d82019-07-22 12:10:00 -07003393 conf->ieee80211ac, conf->ieee80211ax,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003394 conf->secondary_channel,
Hai Shalom81f62d82019-07-22 12:10:00 -07003395 hostapd_get_oper_chwidth(conf),
3396 hostapd_get_oper_centr_freq_seg0_idx(conf),
3397 hostapd_get_oper_centr_freq_seg1_idx(conf),
3398 conf->vht_capab,
3399 mode ? &mode->he_capab[IEEE80211_MODE_AP] :
3400 NULL))
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003401 return -1;
3402
3403 switch (params->bandwidth) {
3404 case 0:
3405 case 20:
3406 case 40:
Hai Shalom81f62d82019-07-22 12:10:00 -07003407 hostapd_set_oper_chwidth(conf, CHANWIDTH_USE_HT);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003408 break;
3409 case 80:
3410 if (params->center_freq2)
Hai Shalom81f62d82019-07-22 12:10:00 -07003411 hostapd_set_oper_chwidth(conf, CHANWIDTH_80P80MHZ);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003412 else
Hai Shalom81f62d82019-07-22 12:10:00 -07003413 hostapd_set_oper_chwidth(conf, CHANWIDTH_80MHZ);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003414 break;
3415 case 160:
Hai Shalom81f62d82019-07-22 12:10:00 -07003416 hostapd_set_oper_chwidth(conf, CHANWIDTH_160MHZ);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003417 break;
3418 default:
3419 return -1;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003420 }
3421
3422 conf->channel = channel;
3423 conf->ieee80211n = params->ht_enabled;
3424 conf->secondary_channel = params->sec_channel_offset;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003425 ieee80211_freq_to_chan(params->center_freq1,
Hai Shalom81f62d82019-07-22 12:10:00 -07003426 &seg0);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003427 ieee80211_freq_to_chan(params->center_freq2,
Hai Shalom81f62d82019-07-22 12:10:00 -07003428 &seg1);
3429 hostapd_set_oper_centr_freq_seg0_idx(conf, seg0);
3430 hostapd_set_oper_centr_freq_seg1_idx(conf, seg1);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003431
3432 /* TODO: maybe call here hostapd_config_check here? */
3433
3434 return 0;
3435}
3436
3437
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003438static int hostapd_fill_csa_settings(struct hostapd_data *hapd,
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003439 struct csa_settings *settings)
3440{
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003441 struct hostapd_iface *iface = hapd->iface;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003442 struct hostapd_freq_params old_freq;
3443 int ret;
Hai Shalom81f62d82019-07-22 12:10:00 -07003444 u8 chan, bandwidth;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003445
3446 os_memset(&old_freq, 0, sizeof(old_freq));
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003447 if (!iface || !iface->freq || hapd->csa_in_progress)
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003448 return -1;
3449
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003450 switch (settings->freq_params.bandwidth) {
3451 case 80:
3452 if (settings->freq_params.center_freq2)
Hai Shalom81f62d82019-07-22 12:10:00 -07003453 bandwidth = CHANWIDTH_80P80MHZ;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003454 else
Hai Shalom81f62d82019-07-22 12:10:00 -07003455 bandwidth = CHANWIDTH_80MHZ;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003456 break;
3457 case 160:
Hai Shalom81f62d82019-07-22 12:10:00 -07003458 bandwidth = CHANWIDTH_160MHZ;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003459 break;
3460 default:
Hai Shalom81f62d82019-07-22 12:10:00 -07003461 bandwidth = CHANWIDTH_USE_HT;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003462 break;
3463 }
3464
3465 if (ieee80211_freq_to_channel_ext(
3466 settings->freq_params.freq,
3467 settings->freq_params.sec_channel_offset,
Hai Shalom81f62d82019-07-22 12:10:00 -07003468 bandwidth,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003469 &hapd->iface->cs_oper_class,
3470 &chan) == NUM_HOSTAPD_MODES) {
3471 wpa_printf(MSG_DEBUG,
Hai Shalom81f62d82019-07-22 12:10:00 -07003472 "invalid frequency for channel switch (freq=%d, sec_channel_offset=%d, vht_enabled=%d, he_enabled=%d)",
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003473 settings->freq_params.freq,
3474 settings->freq_params.sec_channel_offset,
Hai Shalom81f62d82019-07-22 12:10:00 -07003475 settings->freq_params.vht_enabled,
3476 settings->freq_params.he_enabled);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003477 return -1;
3478 }
3479
3480 settings->freq_params.channel = chan;
3481
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003482 ret = hostapd_change_config_freq(iface->bss[0], iface->conf,
3483 &settings->freq_params,
3484 &old_freq);
3485 if (ret)
3486 return ret;
3487
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003488 ret = hostapd_build_beacon_data(hapd, &settings->beacon_after);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003489
3490 /* change back the configuration */
3491 hostapd_change_config_freq(iface->bss[0], iface->conf,
3492 &old_freq, NULL);
3493
3494 if (ret)
3495 return ret;
3496
3497 /* set channel switch parameters for csa ie */
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003498 hapd->cs_freq_params = settings->freq_params;
3499 hapd->cs_count = settings->cs_count;
3500 hapd->cs_block_tx = settings->block_tx;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003501
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003502 ret = hostapd_build_beacon_data(hapd, &settings->beacon_csa);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003503 if (ret) {
3504 free_beacon_data(&settings->beacon_after);
3505 return ret;
3506 }
3507
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003508 settings->counter_offset_beacon[0] = hapd->cs_c_off_beacon;
3509 settings->counter_offset_presp[0] = hapd->cs_c_off_proberesp;
3510 settings->counter_offset_beacon[1] = hapd->cs_c_off_ecsa_beacon;
3511 settings->counter_offset_presp[1] = hapd->cs_c_off_ecsa_proberesp;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003512
3513 return 0;
3514}
3515
3516
3517void hostapd_cleanup_cs_params(struct hostapd_data *hapd)
3518{
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003519 os_memset(&hapd->cs_freq_params, 0, sizeof(hapd->cs_freq_params));
3520 hapd->cs_count = 0;
3521 hapd->cs_block_tx = 0;
3522 hapd->cs_c_off_beacon = 0;
3523 hapd->cs_c_off_proberesp = 0;
3524 hapd->csa_in_progress = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003525 hapd->cs_c_off_ecsa_beacon = 0;
3526 hapd->cs_c_off_ecsa_proberesp = 0;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003527}
3528
3529
Roshan Pius3a1667e2018-07-03 15:17:14 -07003530void hostapd_chan_switch_vht_config(struct hostapd_data *hapd, int vht_enabled)
3531{
3532 if (vht_enabled)
3533 hapd->iconf->ch_switch_vht_config |= CH_SWITCH_VHT_ENABLED;
3534 else
3535 hapd->iconf->ch_switch_vht_config |= CH_SWITCH_VHT_DISABLED;
3536
3537 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
3538 HOSTAPD_LEVEL_INFO, "CHAN_SWITCH VHT CONFIG 0x%x",
3539 hapd->iconf->ch_switch_vht_config);
3540}
3541
3542
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003543int hostapd_switch_channel(struct hostapd_data *hapd,
3544 struct csa_settings *settings)
3545{
3546 int ret;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003547
3548 if (!(hapd->iface->drv_flags & WPA_DRIVER_FLAGS_AP_CSA)) {
3549 wpa_printf(MSG_INFO, "CSA is not supported");
3550 return -1;
3551 }
3552
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003553 ret = hostapd_fill_csa_settings(hapd, settings);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003554 if (ret)
3555 return ret;
3556
3557 ret = hostapd_drv_switch_channel(hapd, settings);
3558 free_beacon_data(&settings->beacon_csa);
3559 free_beacon_data(&settings->beacon_after);
3560
3561 if (ret) {
3562 /* if we failed, clean cs parameters */
3563 hostapd_cleanup_cs_params(hapd);
3564 return ret;
3565 }
3566
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003567 hapd->csa_in_progress = 1;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003568 return 0;
3569}
3570
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003571
3572void
3573hostapd_switch_channel_fallback(struct hostapd_iface *iface,
3574 const struct hostapd_freq_params *freq_params)
3575{
Hai Shalom81f62d82019-07-22 12:10:00 -07003576 int seg0_idx = 0, seg1_idx = 0, bw = CHANWIDTH_USE_HT;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003577
3578 wpa_printf(MSG_DEBUG, "Restarting all CSA-related BSSes");
3579
3580 if (freq_params->center_freq1)
Hai Shalom81f62d82019-07-22 12:10:00 -07003581 seg0_idx = 36 + (freq_params->center_freq1 - 5180) / 5;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003582 if (freq_params->center_freq2)
Hai Shalom81f62d82019-07-22 12:10:00 -07003583 seg1_idx = 36 + (freq_params->center_freq2 - 5180) / 5;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003584
3585 switch (freq_params->bandwidth) {
3586 case 0:
3587 case 20:
3588 case 40:
Hai Shalom81f62d82019-07-22 12:10:00 -07003589 bw = CHANWIDTH_USE_HT;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003590 break;
3591 case 80:
3592 if (freq_params->center_freq2)
Hai Shalom81f62d82019-07-22 12:10:00 -07003593 bw = CHANWIDTH_80P80MHZ;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003594 else
Hai Shalom81f62d82019-07-22 12:10:00 -07003595 bw = CHANWIDTH_80MHZ;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003596 break;
3597 case 160:
Hai Shalom81f62d82019-07-22 12:10:00 -07003598 bw = CHANWIDTH_160MHZ;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003599 break;
3600 default:
3601 wpa_printf(MSG_WARNING, "Unknown CSA bandwidth: %d",
3602 freq_params->bandwidth);
3603 break;
3604 }
3605
3606 iface->freq = freq_params->freq;
3607 iface->conf->channel = freq_params->channel;
3608 iface->conf->secondary_channel = freq_params->sec_channel_offset;
Hai Shalom81f62d82019-07-22 12:10:00 -07003609 hostapd_set_oper_centr_freq_seg0_idx(iface->conf, seg0_idx);
3610 hostapd_set_oper_centr_freq_seg1_idx(iface->conf, seg1_idx);
3611 hostapd_set_oper_chwidth(iface->conf, bw);
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003612 iface->conf->ieee80211n = freq_params->ht_enabled;
3613 iface->conf->ieee80211ac = freq_params->vht_enabled;
Hai Shalom81f62d82019-07-22 12:10:00 -07003614 iface->conf->ieee80211ax = freq_params->he_enabled;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003615
3616 /*
3617 * cs_params must not be cleared earlier because the freq_params
3618 * argument may actually point to one of these.
Hai Shalom39ba6fc2019-01-22 12:40:38 -08003619 * These params will be cleared during interface disable below.
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003620 */
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003621 hostapd_disable_iface(iface);
3622 hostapd_enable_iface(iface);
3623}
3624
Dmitry Shmidte4663042016-04-04 10:07:49 -07003625#endif /* NEED_AP_MLME */
3626
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003627
3628struct hostapd_data * hostapd_get_iface(struct hapd_interfaces *interfaces,
3629 const char *ifname)
3630{
3631 size_t i, j;
3632
3633 for (i = 0; i < interfaces->count; i++) {
3634 struct hostapd_iface *iface = interfaces->iface[i];
3635
3636 for (j = 0; j < iface->num_bss; j++) {
3637 struct hostapd_data *hapd = iface->bss[j];
3638
3639 if (os_strcmp(ifname, hapd->conf->iface) == 0)
3640 return hapd;
3641 }
3642 }
3643
3644 return NULL;
3645}
3646
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003647
3648void hostapd_periodic_iface(struct hostapd_iface *iface)
3649{
3650 size_t i;
3651
3652 ap_list_timer(iface);
3653
3654 for (i = 0; i < iface->num_bss; i++) {
3655 struct hostapd_data *hapd = iface->bss[i];
3656
3657 if (!hapd->started)
3658 continue;
3659
3660#ifndef CONFIG_NO_RADIUS
3661 hostapd_acl_expire(hapd);
3662#endif /* CONFIG_NO_RADIUS */
3663 }
3664}