blob: e4950e3edf02c71ff70d4b0205ff8602c5982f79 [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"
16#include "common/ieee802_11_defs.h"
Dmitry Shmidtcce06662013-11-04 18:44:24 -080017#include "common/wpa_ctrl.h"
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080018#include "common/hw_features_common.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070019#include "radius/radius_client.h"
Dmitry Shmidt04949592012-07-19 12:16:46 -070020#include "radius/radius_das.h"
Dmitry Shmidt50b691d2014-05-21 14:01:45 -070021#include "eap_server/tncs.h"
Dmitry Shmidt2f74e362015-01-21 13:19:05 -080022#include "eapol_auth/eapol_auth_sm.h"
23#include "eapol_auth/eapol_auth_sm_i.h"
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080024#include "fst/fst.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070025#include "hostapd.h"
26#include "authsrv.h"
27#include "sta_info.h"
28#include "accounting.h"
29#include "ap_list.h"
30#include "beacon.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070031#include "ieee802_1x.h"
32#include "ieee802_11_auth.h"
33#include "vlan_init.h"
34#include "wpa_auth.h"
35#include "wps_hostapd.h"
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070036#include "dpp_hostapd.h"
37#include "gas_query_ap.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070038#include "hw_features.h"
39#include "wpa_auth_glue.h"
40#include "ap_drv_ops.h"
41#include "ap_config.h"
42#include "p2p_hostapd.h"
Dmitry Shmidt04949592012-07-19 12:16:46 -070043#include "gas_serv.h"
Dmitry Shmidt051af732013-10-22 13:52:46 -070044#include "dfs.h"
Dmitry Shmidt7832adb2014-04-29 10:53:02 -070045#include "ieee802_11.h"
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080046#include "bss_load.h"
47#include "x_snoop.h"
48#include "dhcp_snoop.h"
49#include "ndisc_snoop.h"
Dmitry Shmidt849734c2016-05-27 09:59:01 -070050#include "neighbor_db.h"
51#include "rrm.h"
Dmitry Shmidtebd93af2017-02-21 13:40:44 -080052#include "fils_hlp.h"
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070053#include "acs.h"
Roshan Pius3a1667e2018-07-03 15:17:14 -070054#include "hs20.h"
Hai Shalom81f62d82019-07-22 12:10:00 -070055#include "airtime_policy.h"
56#include "wpa_auth_kay.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070057
58
Dmitry Shmidt04949592012-07-19 12:16:46 -070059static int hostapd_flush_old_stations(struct hostapd_data *hapd, u16 reason);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070060static int hostapd_setup_encryption(char *iface, struct hostapd_data *hapd);
Dmitry Shmidtc55524a2011-07-07 11:18:38 -070061static int hostapd_broadcast_wep_clear(struct hostapd_data *hapd);
Dmitry Shmidtcce06662013-11-04 18:44:24 -080062static int setup_interface2(struct hostapd_iface *iface);
63static void channel_list_update_timeout(void *eloop_ctx, void *timeout_ctx);
Roshan Pius3a1667e2018-07-03 15:17:14 -070064static void hostapd_interface_setup_failure_handler(void *eloop_ctx,
65 void *timeout_ctx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070066
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070067
Dmitry Shmidt04949592012-07-19 12:16:46 -070068int hostapd_for_each_interface(struct hapd_interfaces *interfaces,
69 int (*cb)(struct hostapd_iface *iface,
70 void *ctx), void *ctx)
71{
72 size_t i;
73 int ret;
74
75 for (i = 0; i < interfaces->count; i++) {
76 ret = cb(interfaces->iface[i], ctx);
77 if (ret)
78 return ret;
79 }
80
81 return 0;
82}
83
84
Hai Shalomce48b4a2018-09-05 11:41:35 -070085void hostapd_reconfig_encryption(struct hostapd_data *hapd)
86{
87 if (hapd->wpa_auth)
88 return;
89
90 hostapd_set_privacy(hapd, 0);
91 hostapd_setup_encryption(hapd->conf->iface, hapd);
92}
93
94
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070095static void hostapd_reload_bss(struct hostapd_data *hapd)
96{
Dmitry Shmidtcce06662013-11-04 18:44:24 -080097 struct hostapd_ssid *ssid;
98
Dmitry Shmidt29333592017-01-09 12:27:11 -080099 if (!hapd->started)
100 return;
101
Roshan Pius3a1667e2018-07-03 15:17:14 -0700102 if (hapd->conf->wmm_enabled < 0)
103 hapd->conf->wmm_enabled = hapd->iconf->ieee80211n;
104
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700105#ifndef CONFIG_NO_RADIUS
106 radius_client_reconfig(hapd->radius, hapd->conf->radius);
107#endif /* CONFIG_NO_RADIUS */
108
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800109 ssid = &hapd->conf->ssid;
110 if (!ssid->wpa_psk_set && ssid->wpa_psk && !ssid->wpa_psk->next &&
111 ssid->wpa_passphrase_set && ssid->wpa_passphrase) {
112 /*
113 * Force PSK to be derived again since SSID or passphrase may
114 * have changed.
115 */
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800116 hostapd_config_clear_wpa_psk(&hapd->conf->ssid.wpa_psk);
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800117 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700118 if (hostapd_setup_wpa_psk(hapd->conf)) {
119 wpa_printf(MSG_ERROR, "Failed to re-configure WPA PSK "
120 "after reloading configuration");
121 }
122
123 if (hapd->conf->ieee802_1x || hapd->conf->wpa)
124 hostapd_set_drv_ieee8021x(hapd, hapd->conf->iface, 1);
125 else
126 hostapd_set_drv_ieee8021x(hapd, hapd->conf->iface, 0);
127
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800128 if ((hapd->conf->wpa || hapd->conf->osen) && hapd->wpa_auth == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700129 hostapd_setup_wpa(hapd);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800130 if (hapd->wpa_auth)
131 wpa_init_keys(hapd->wpa_auth);
132 } else if (hapd->conf->wpa) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700133 const u8 *wpa_ie;
134 size_t wpa_ie_len;
135 hostapd_reconfig_wpa(hapd);
136 wpa_ie = wpa_auth_get_wpa_ie(hapd->wpa_auth, &wpa_ie_len);
137 if (hostapd_set_generic_elem(hapd, wpa_ie, wpa_ie_len))
138 wpa_printf(MSG_ERROR, "Failed to configure WPA IE for "
139 "the kernel driver.");
140 } else if (hapd->wpa_auth) {
141 wpa_deinit(hapd->wpa_auth);
142 hapd->wpa_auth = NULL;
143 hostapd_set_privacy(hapd, 0);
144 hostapd_setup_encryption(hapd->conf->iface, hapd);
145 hostapd_set_generic_elem(hapd, (u8 *) "", 0);
146 }
147
148 ieee802_11_set_beacon(hapd);
149 hostapd_update_wps(hapd);
150
151 if (hapd->conf->ssid.ssid_set &&
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700152 hostapd_set_ssid(hapd, hapd->conf->ssid.ssid,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700153 hapd->conf->ssid.ssid_len)) {
154 wpa_printf(MSG_ERROR, "Could not set SSID for kernel driver");
155 /* try to continue */
156 }
157 wpa_printf(MSG_DEBUG, "Reconfigured interface %s", hapd->conf->iface);
158}
159
160
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700161static void hostapd_clear_old(struct hostapd_iface *iface)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700162{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700163 size_t j;
164
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700165 /*
166 * Deauthenticate all stations since the new configuration may not
167 * allow them to use the BSS anymore.
168 */
169 for (j = 0; j < iface->num_bss; j++) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700170 hostapd_flush_old_stations(iface->bss[j],
171 WLAN_REASON_PREV_AUTH_NOT_VALID);
Dmitry Shmidtc55524a2011-07-07 11:18:38 -0700172 hostapd_broadcast_wep_clear(iface->bss[j]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700173
174#ifndef CONFIG_NO_RADIUS
175 /* TODO: update dynamic data based on changed configuration
176 * items (e.g., open/close sockets, etc.) */
177 radius_client_flush(iface->bss[j]->radius, 0);
178#endif /* CONFIG_NO_RADIUS */
179 }
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700180}
181
182
Hai Shalom74f70d42019-02-11 14:42:39 -0800183static int hostapd_iface_conf_changed(struct hostapd_config *newconf,
184 struct hostapd_config *oldconf)
185{
186 size_t i;
187
188 if (newconf->num_bss != oldconf->num_bss)
189 return 1;
190
191 for (i = 0; i < newconf->num_bss; i++) {
192 if (os_strcmp(newconf->bss[i]->iface,
193 oldconf->bss[i]->iface) != 0)
194 return 1;
195 }
196
197 return 0;
198}
199
200
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700201int hostapd_reload_config(struct hostapd_iface *iface)
202{
Hai Shalom74f70d42019-02-11 14:42:39 -0800203 struct hapd_interfaces *interfaces = iface->interfaces;
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700204 struct hostapd_data *hapd = iface->bss[0];
205 struct hostapd_config *newconf, *oldconf;
206 size_t j;
207
208 if (iface->config_fname == NULL) {
209 /* Only in-memory config in use - assume it has been updated */
210 hostapd_clear_old(iface);
211 for (j = 0; j < iface->num_bss; j++)
212 hostapd_reload_bss(iface->bss[j]);
213 return 0;
214 }
215
216 if (iface->interfaces == NULL ||
217 iface->interfaces->config_read_cb == NULL)
218 return -1;
219 newconf = iface->interfaces->config_read_cb(iface->config_fname);
220 if (newconf == NULL)
221 return -1;
222
223 hostapd_clear_old(iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700224
225 oldconf = hapd->iconf;
Hai Shalom74f70d42019-02-11 14:42:39 -0800226 if (hostapd_iface_conf_changed(newconf, oldconf)) {
227 char *fname;
228 int res;
229
230 wpa_printf(MSG_DEBUG,
231 "Configuration changes include interface/BSS modification - force full disable+enable sequence");
232 fname = os_strdup(iface->config_fname);
233 if (!fname) {
234 hostapd_config_free(newconf);
235 return -1;
236 }
237 hostapd_remove_iface(interfaces, hapd->conf->iface);
238 iface = hostapd_init(interfaces, fname);
239 os_free(fname);
240 hostapd_config_free(newconf);
241 if (!iface) {
242 wpa_printf(MSG_ERROR,
243 "Failed to initialize interface on config reload");
244 return -1;
245 }
246 iface->interfaces = interfaces;
247 interfaces->iface[interfaces->count] = iface;
248 interfaces->count++;
249 res = hostapd_enable_iface(iface);
250 if (res < 0)
251 wpa_printf(MSG_ERROR,
252 "Failed to enable interface on config reload");
253 return res;
254 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700255 iface->conf = newconf;
256
257 for (j = 0; j < iface->num_bss; j++) {
258 hapd = iface->bss[j];
259 hapd->iconf = newconf;
Dmitry Shmidtd11f0192014-03-24 12:09:47 -0700260 hapd->iconf->channel = oldconf->channel;
Dmitry Shmidtdda10c22015-03-24 16:05:01 -0700261 hapd->iconf->acs = oldconf->acs;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -0700262 hapd->iconf->secondary_channel = oldconf->secondary_channel;
Dmitry Shmidtd11f0192014-03-24 12:09:47 -0700263 hapd->iconf->ieee80211n = oldconf->ieee80211n;
264 hapd->iconf->ieee80211ac = oldconf->ieee80211ac;
265 hapd->iconf->ht_capab = oldconf->ht_capab;
266 hapd->iconf->vht_capab = oldconf->vht_capab;
Hai Shalom81f62d82019-07-22 12:10:00 -0700267 hostapd_set_oper_chwidth(hapd->iconf,
268 hostapd_get_oper_chwidth(oldconf));
269 hostapd_set_oper_centr_freq_seg0_idx(
270 hapd->iconf,
271 hostapd_get_oper_centr_freq_seg0_idx(oldconf));
272 hostapd_set_oper_centr_freq_seg1_idx(
273 hapd->iconf,
274 hostapd_get_oper_centr_freq_seg1_idx(oldconf));
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800275 hapd->conf = newconf->bss[j];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700276 hostapd_reload_bss(hapd);
277 }
278
279 hostapd_config_free(oldconf);
280
281
282 return 0;
283}
284
285
286static void hostapd_broadcast_key_clear_iface(struct hostapd_data *hapd,
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700287 const char *ifname)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700288{
289 int i;
290
Dmitry Shmidt29333592017-01-09 12:27:11 -0800291 if (!ifname || !hapd->drv_priv)
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700292 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700293 for (i = 0; i < NUM_WEP_KEYS; i++) {
294 if (hostapd_drv_set_key(ifname, hapd, WPA_ALG_NONE, NULL, i,
295 0, NULL, 0, NULL, 0)) {
296 wpa_printf(MSG_DEBUG, "Failed to clear default "
297 "encryption keys (ifname=%s keyidx=%d)",
298 ifname, i);
299 }
300 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700301 if (hapd->conf->ieee80211w) {
302 for (i = NUM_WEP_KEYS; i < NUM_WEP_KEYS + 2; i++) {
303 if (hostapd_drv_set_key(ifname, hapd, WPA_ALG_NONE,
304 NULL, i, 0, NULL,
305 0, NULL, 0)) {
306 wpa_printf(MSG_DEBUG, "Failed to clear "
307 "default mgmt encryption keys "
308 "(ifname=%s keyidx=%d)", ifname, i);
309 }
310 }
311 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700312}
313
314
315static int hostapd_broadcast_wep_clear(struct hostapd_data *hapd)
316{
317 hostapd_broadcast_key_clear_iface(hapd, hapd->conf->iface);
318 return 0;
319}
320
321
322static int hostapd_broadcast_wep_set(struct hostapd_data *hapd)
323{
324 int errors = 0, idx;
325 struct hostapd_ssid *ssid = &hapd->conf->ssid;
326
327 idx = ssid->wep.idx;
328 if (ssid->wep.default_len &&
329 hostapd_drv_set_key(hapd->conf->iface,
330 hapd, WPA_ALG_WEP, broadcast_ether_addr, idx,
331 1, NULL, 0, ssid->wep.key[idx],
332 ssid->wep.len[idx])) {
333 wpa_printf(MSG_WARNING, "Could not set WEP encryption.");
334 errors++;
335 }
336
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700337 return errors;
338}
339
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700340
Dmitry Shmidt04949592012-07-19 12:16:46 -0700341static void hostapd_free_hapd_data(struct hostapd_data *hapd)
342{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800343 os_free(hapd->probereq_cb);
344 hapd->probereq_cb = NULL;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800345 hapd->num_probereq_cb = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800346
347#ifdef CONFIG_P2P
348 wpabuf_free(hapd->p2p_beacon_ie);
349 hapd->p2p_beacon_ie = NULL;
350 wpabuf_free(hapd->p2p_probe_resp_ie);
351 hapd->p2p_probe_resp_ie = NULL;
352#endif /* CONFIG_P2P */
353
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -0800354 if (!hapd->started) {
355 wpa_printf(MSG_ERROR, "%s: Interface %s wasn't started",
Hai Shalom021b0b52019-04-10 11:17:58 -0700356 __func__, hapd->conf ? hapd->conf->iface : "N/A");
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -0800357 return;
358 }
359 hapd->started = 0;
Hai Shalom5f92bc92019-04-18 11:54:11 -0700360 hapd->beacon_set_done = 0;
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -0800361
Dmitry Shmidt54605472013-11-08 11:10:19 -0800362 wpa_printf(MSG_DEBUG, "%s(%s)", __func__, hapd->conf->iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700363 accounting_deinit(hapd);
364 hostapd_deinit_wpa(hapd);
365 vlan_deinit(hapd);
366 hostapd_acl_deinit(hapd);
367#ifndef CONFIG_NO_RADIUS
368 radius_client_deinit(hapd->radius);
369 hapd->radius = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700370 radius_das_deinit(hapd->radius_das);
371 hapd->radius_das = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700372#endif /* CONFIG_NO_RADIUS */
373
374 hostapd_deinit_wps(hapd);
Hai Shalom81f62d82019-07-22 12:10:00 -0700375 ieee802_1x_dealloc_kay_sm_hapd(hapd);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700376#ifdef CONFIG_DPP
377 hostapd_dpp_deinit(hapd);
378 gas_query_ap_deinit(hapd->gas);
379#endif /* CONFIG_DPP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700380
381 authsrv_deinit(hapd);
382
Dmitry Shmidt71757432014-06-02 13:50:35 -0700383 if (hapd->interface_added) {
384 hapd->interface_added = 0;
385 if (hostapd_if_remove(hapd, WPA_IF_AP_BSS, hapd->conf->iface)) {
386 wpa_printf(MSG_WARNING,
387 "Failed to remove BSS interface %s",
388 hapd->conf->iface);
389 hapd->interface_added = 1;
390 } else {
391 /*
392 * Since this was a dynamically added interface, the
393 * driver wrapper may have removed its internal instance
394 * and hapd->drv_priv is not valid anymore.
395 */
396 hapd->drv_priv = NULL;
397 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700398 }
399
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800400 wpabuf_free(hapd->time_adv);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700401
402#ifdef CONFIG_INTERWORKING
403 gas_serv_deinit(hapd);
404#endif /* CONFIG_INTERWORKING */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800405
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800406 bss_load_update_deinit(hapd);
407 ndisc_snoop_deinit(hapd);
408 dhcp_snoop_deinit(hapd);
409 x_snoop_deinit(hapd);
410
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800411#ifdef CONFIG_SQLITE
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700412 bin_clear_free(hapd->tmp_eap_user.identity,
413 hapd->tmp_eap_user.identity_len);
414 bin_clear_free(hapd->tmp_eap_user.password,
415 hapd->tmp_eap_user.password_len);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800416#endif /* CONFIG_SQLITE */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800417
418#ifdef CONFIG_MESH
419 wpabuf_free(hapd->mesh_pending_auth);
420 hapd->mesh_pending_auth = NULL;
421#endif /* CONFIG_MESH */
Dmitry Shmidt849734c2016-05-27 09:59:01 -0700422
423 hostapd_clean_rrm(hapd);
Dmitry Shmidtebd93af2017-02-21 13:40:44 -0800424 fils_hlp_deinit(hapd);
Hai Shalom021b0b52019-04-10 11:17:58 -0700425
426#ifdef CONFIG_SAE
427 {
428 struct hostapd_sae_commit_queue *q;
429
430 while ((q = dl_list_first(&hapd->sae_commit_queue,
431 struct hostapd_sae_commit_queue,
432 list))) {
433 dl_list_del(&q->list);
434 os_free(q);
435 }
436 }
437 eloop_cancel_timeout(auth_sae_process_commit, hapd, NULL);
438#endif /* CONFIG_SAE */
Dmitry Shmidt04949592012-07-19 12:16:46 -0700439}
440
441
442/**
443 * hostapd_cleanup - Per-BSS cleanup (deinitialization)
444 * @hapd: Pointer to BSS data
445 *
446 * This function is used to free all per-BSS data structures and resources.
Dmitry Shmidt54605472013-11-08 11:10:19 -0800447 * Most of the modules that are initialized in hostapd_setup_bss() are
448 * deinitialized here.
Dmitry Shmidt04949592012-07-19 12:16:46 -0700449 */
450static void hostapd_cleanup(struct hostapd_data *hapd)
451{
Dmitry Shmidt54605472013-11-08 11:10:19 -0800452 wpa_printf(MSG_DEBUG, "%s(hapd=%p (%s))", __func__, hapd,
Hai Shalom021b0b52019-04-10 11:17:58 -0700453 hapd->conf ? hapd->conf->iface : "N/A");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700454 if (hapd->iface->interfaces &&
Dmitry Shmidt29333592017-01-09 12:27:11 -0800455 hapd->iface->interfaces->ctrl_iface_deinit) {
456 wpa_msg(hapd->msg_ctx, MSG_INFO, WPA_EVENT_TERMINATING);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700457 hapd->iface->interfaces->ctrl_iface_deinit(hapd);
Dmitry Shmidt29333592017-01-09 12:27:11 -0800458 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700459 hostapd_free_hapd_data(hapd);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700460}
461
462
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800463static void sta_track_deinit(struct hostapd_iface *iface)
464{
465 struct hostapd_sta_info *info;
466
467 if (!iface->num_sta_seen)
468 return;
469
470 while ((info = dl_list_first(&iface->sta_seen, struct hostapd_sta_info,
471 list))) {
472 dl_list_del(&info->list);
473 iface->num_sta_seen--;
Dmitry Shmidtaca489e2016-09-28 15:44:14 -0700474 sta_track_del(info);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800475 }
476}
477
478
Dmitry Shmidt04949592012-07-19 12:16:46 -0700479static void hostapd_cleanup_iface_partial(struct hostapd_iface *iface)
480{
Dmitry Shmidt54605472013-11-08 11:10:19 -0800481 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800482#ifdef CONFIG_IEEE80211N
483#ifdef NEED_AP_MLME
484 hostapd_stop_setup_timers(iface);
485#endif /* NEED_AP_MLME */
486#endif /* CONFIG_IEEE80211N */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700487 if (iface->current_mode)
488 acs_cleanup(iface);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700489 hostapd_free_hw_features(iface->hw_features, iface->num_hw_features);
490 iface->hw_features = NULL;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700491 iface->current_mode = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700492 os_free(iface->current_rates);
493 iface->current_rates = NULL;
494 os_free(iface->basic_rates);
495 iface->basic_rates = NULL;
496 ap_list_deinit(iface);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800497 sta_track_deinit(iface);
Hai Shalom81f62d82019-07-22 12:10:00 -0700498 airtime_policy_update_deinit(iface);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700499}
500
501
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700502/**
503 * hostapd_cleanup_iface - Complete per-interface cleanup
504 * @iface: Pointer to interface data
505 *
506 * This function is called after per-BSS data structures are deinitialized
507 * with hostapd_cleanup().
508 */
509static void hostapd_cleanup_iface(struct hostapd_iface *iface)
510{
Dmitry Shmidt54605472013-11-08 11:10:19 -0800511 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800512 eloop_cancel_timeout(channel_list_update_timeout, iface, NULL);
Roshan Pius3a1667e2018-07-03 15:17:14 -0700513 eloop_cancel_timeout(hostapd_interface_setup_failure_handler, iface,
514 NULL);
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800515
Dmitry Shmidt04949592012-07-19 12:16:46 -0700516 hostapd_cleanup_iface_partial(iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700517 hostapd_config_free(iface->conf);
518 iface->conf = NULL;
519
520 os_free(iface->config_fname);
521 os_free(iface->bss);
Dmitry Shmidt54605472013-11-08 11:10:19 -0800522 wpa_printf(MSG_DEBUG, "%s: free iface=%p", __func__, iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700523 os_free(iface);
524}
525
526
Dmitry Shmidt04949592012-07-19 12:16:46 -0700527static void hostapd_clear_wep(struct hostapd_data *hapd)
528{
Hai Shalom021b0b52019-04-10 11:17:58 -0700529 if (hapd->drv_priv && !hapd->iface->driver_ap_teardown && hapd->conf) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700530 hostapd_set_privacy(hapd, 0);
531 hostapd_broadcast_wep_clear(hapd);
532 }
533}
534
535
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700536static int hostapd_setup_encryption(char *iface, struct hostapd_data *hapd)
537{
538 int i;
539
540 hostapd_broadcast_wep_set(hapd);
541
542 if (hapd->conf->ssid.wep.default_len) {
543 hostapd_set_privacy(hapd, 1);
544 return 0;
545 }
546
Jouni Malinen75ecf522011-06-27 15:19:46 -0700547 /*
548 * When IEEE 802.1X is not enabled, the driver may need to know how to
549 * set authentication algorithms for static WEP.
550 */
551 hostapd_drv_set_authmode(hapd, hapd->conf->auth_algs);
552
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700553 for (i = 0; i < 4; i++) {
554 if (hapd->conf->ssid.wep.key[i] &&
555 hostapd_drv_set_key(iface, hapd, WPA_ALG_WEP, NULL, i,
556 i == hapd->conf->ssid.wep.idx, NULL, 0,
557 hapd->conf->ssid.wep.key[i],
558 hapd->conf->ssid.wep.len[i])) {
559 wpa_printf(MSG_WARNING, "Could not set WEP "
560 "encryption.");
561 return -1;
562 }
563 if (hapd->conf->ssid.wep.key[i] &&
564 i == hapd->conf->ssid.wep.idx)
565 hostapd_set_privacy(hapd, 1);
566 }
567
568 return 0;
569}
570
571
Dmitry Shmidt04949592012-07-19 12:16:46 -0700572static int hostapd_flush_old_stations(struct hostapd_data *hapd, u16 reason)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700573{
574 int ret = 0;
575 u8 addr[ETH_ALEN];
576
577 if (hostapd_drv_none(hapd) || hapd->drv_priv == NULL)
578 return 0;
579
Dmitry Shmidta38abf92014-03-06 13:38:44 -0800580 if (!hapd->iface->driver_ap_teardown) {
581 wpa_dbg(hapd->msg_ctx, MSG_DEBUG,
582 "Flushing old station entries");
583
584 if (hostapd_flush(hapd)) {
585 wpa_msg(hapd->msg_ctx, MSG_WARNING,
586 "Could not connect to kernel driver");
587 ret = -1;
588 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700589 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700590 if (hapd->conf && hapd->conf->broadcast_deauth) {
591 wpa_dbg(hapd->msg_ctx, MSG_DEBUG,
592 "Deauthenticate all stations");
593 os_memset(addr, 0xff, ETH_ALEN);
594 hostapd_drv_sta_deauth(hapd, addr, reason);
595 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700596 hostapd_free_stas(hapd);
597
598 return ret;
599}
600
601
Dmitry Shmidt71757432014-06-02 13:50:35 -0700602static void hostapd_bss_deinit_no_free(struct hostapd_data *hapd)
603{
604 hostapd_free_stas(hapd);
605 hostapd_flush_old_stations(hapd, WLAN_REASON_DEAUTH_LEAVING);
606 hostapd_clear_wep(hapd);
607}
608
609
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700610/**
611 * hostapd_validate_bssid_configuration - Validate BSSID configuration
612 * @iface: Pointer to interface data
613 * Returns: 0 on success, -1 on failure
614 *
615 * This function is used to validate that the configured BSSIDs are valid.
616 */
617static int hostapd_validate_bssid_configuration(struct hostapd_iface *iface)
618{
619 u8 mask[ETH_ALEN] = { 0 };
620 struct hostapd_data *hapd = iface->bss[0];
621 unsigned int i = iface->conf->num_bss, bits = 0, j;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700622 int auto_addr = 0;
623
624 if (hostapd_drv_none(hapd))
625 return 0;
626
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -0800627 if (iface->conf->use_driver_iface_addr)
628 return 0;
629
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700630 /* Generate BSSID mask that is large enough to cover the BSSIDs. */
631
632 /* Determine the bits necessary to cover the number of BSSIDs. */
633 for (i--; i; i >>= 1)
634 bits++;
635
636 /* Determine the bits necessary to any configured BSSIDs,
637 if they are higher than the number of BSSIDs. */
638 for (j = 0; j < iface->conf->num_bss; j++) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800639 if (is_zero_ether_addr(iface->conf->bss[j]->bssid)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700640 if (j)
641 auto_addr++;
642 continue;
643 }
644
645 for (i = 0; i < ETH_ALEN; i++) {
646 mask[i] |=
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800647 iface->conf->bss[j]->bssid[i] ^
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700648 hapd->own_addr[i];
649 }
650 }
651
652 if (!auto_addr)
653 goto skip_mask_ext;
654
655 for (i = 0; i < ETH_ALEN && mask[i] == 0; i++)
656 ;
657 j = 0;
658 if (i < ETH_ALEN) {
659 j = (5 - i) * 8;
660
661 while (mask[i] != 0) {
662 mask[i] >>= 1;
663 j++;
664 }
665 }
666
667 if (bits < j)
668 bits = j;
669
670 if (bits > 40) {
671 wpa_printf(MSG_ERROR, "Too many bits in the BSSID mask (%u)",
672 bits);
673 return -1;
674 }
675
676 os_memset(mask, 0xff, ETH_ALEN);
677 j = bits / 8;
678 for (i = 5; i > 5 - j; i--)
679 mask[i] = 0;
680 j = bits % 8;
Hai Shalom021b0b52019-04-10 11:17:58 -0700681 while (j) {
682 j--;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700683 mask[i] <<= 1;
Hai Shalom021b0b52019-04-10 11:17:58 -0700684 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700685
686skip_mask_ext:
687 wpa_printf(MSG_DEBUG, "BSS count %lu, BSSID mask " MACSTR " (%d bits)",
688 (unsigned long) iface->conf->num_bss, MAC2STR(mask), bits);
689
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700690 if (!auto_addr)
691 return 0;
692
693 for (i = 0; i < ETH_ALEN; i++) {
694 if ((hapd->own_addr[i] & mask[i]) != hapd->own_addr[i]) {
695 wpa_printf(MSG_ERROR, "Invalid BSSID mask " MACSTR
696 " for start address " MACSTR ".",
697 MAC2STR(mask), MAC2STR(hapd->own_addr));
698 wpa_printf(MSG_ERROR, "Start address must be the "
699 "first address in the block (i.e., addr "
700 "AND mask == addr).");
701 return -1;
702 }
703 }
704
705 return 0;
706}
707
708
709static int mac_in_conf(struct hostapd_config *conf, const void *a)
710{
711 size_t i;
712
713 for (i = 0; i < conf->num_bss; i++) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800714 if (hostapd_mac_comp(conf->bss[i]->bssid, a) == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700715 return 1;
716 }
717 }
718
719 return 0;
720}
721
722
Dmitry Shmidt04949592012-07-19 12:16:46 -0700723#ifndef CONFIG_NO_RADIUS
724
725static int hostapd_das_nas_mismatch(struct hostapd_data *hapd,
726 struct radius_das_attrs *attr)
727{
Dmitry Shmidt13ca8d82014-02-20 10:18:40 -0800728 if (attr->nas_identifier &&
729 (!hapd->conf->nas_identifier ||
730 os_strlen(hapd->conf->nas_identifier) !=
731 attr->nas_identifier_len ||
732 os_memcmp(hapd->conf->nas_identifier, attr->nas_identifier,
733 attr->nas_identifier_len) != 0)) {
734 wpa_printf(MSG_DEBUG, "RADIUS DAS: NAS-Identifier mismatch");
735 return 1;
736 }
737
738 if (attr->nas_ip_addr &&
739 (hapd->conf->own_ip_addr.af != AF_INET ||
740 os_memcmp(&hapd->conf->own_ip_addr.u.v4, attr->nas_ip_addr, 4) !=
741 0)) {
742 wpa_printf(MSG_DEBUG, "RADIUS DAS: NAS-IP-Address mismatch");
743 return 1;
744 }
745
746#ifdef CONFIG_IPV6
747 if (attr->nas_ipv6_addr &&
748 (hapd->conf->own_ip_addr.af != AF_INET6 ||
749 os_memcmp(&hapd->conf->own_ip_addr.u.v6, attr->nas_ipv6_addr, 16)
750 != 0)) {
751 wpa_printf(MSG_DEBUG, "RADIUS DAS: NAS-IPv6-Address mismatch");
752 return 1;
753 }
754#endif /* CONFIG_IPV6 */
755
Dmitry Shmidt04949592012-07-19 12:16:46 -0700756 return 0;
757}
758
759
760static struct sta_info * hostapd_das_find_sta(struct hostapd_data *hapd,
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800761 struct radius_das_attrs *attr,
762 int *multi)
Dmitry Shmidt04949592012-07-19 12:16:46 -0700763{
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800764 struct sta_info *selected, *sta;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700765 char buf[128];
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800766 int num_attr = 0;
767 int count;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700768
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800769 *multi = 0;
770
771 for (sta = hapd->sta_list; sta; sta = sta->next)
772 sta->radius_das_match = 1;
773
774 if (attr->sta_addr) {
775 num_attr++;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700776 sta = ap_get_sta(hapd, attr->sta_addr);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800777 if (!sta) {
778 wpa_printf(MSG_DEBUG,
779 "RADIUS DAS: No Calling-Station-Id match");
780 return NULL;
781 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700782
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800783 selected = sta;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700784 for (sta = hapd->sta_list; sta; sta = sta->next) {
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800785 if (sta != selected)
786 sta->radius_das_match = 0;
787 }
788 wpa_printf(MSG_DEBUG, "RADIUS DAS: Calling-Station-Id match");
789 }
790
791 if (attr->acct_session_id) {
792 num_attr++;
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800793 if (attr->acct_session_id_len != 16) {
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800794 wpa_printf(MSG_DEBUG,
795 "RADIUS DAS: Acct-Session-Id cannot match");
796 return NULL;
797 }
798 count = 0;
799
800 for (sta = hapd->sta_list; sta; sta = sta->next) {
801 if (!sta->radius_das_match)
802 continue;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800803 os_snprintf(buf, sizeof(buf), "%016llX",
804 (unsigned long long) sta->acct_session_id);
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800805 if (os_memcmp(attr->acct_session_id, buf, 16) != 0)
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800806 sta->radius_das_match = 0;
807 else
808 count++;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700809 }
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800810
811 if (count == 0) {
812 wpa_printf(MSG_DEBUG,
813 "RADIUS DAS: No matches remaining after Acct-Session-Id check");
814 return NULL;
815 }
816 wpa_printf(MSG_DEBUG, "RADIUS DAS: Acct-Session-Id match");
Dmitry Shmidt04949592012-07-19 12:16:46 -0700817 }
818
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800819 if (attr->acct_multi_session_id) {
820 num_attr++;
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800821 if (attr->acct_multi_session_id_len != 16) {
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800822 wpa_printf(MSG_DEBUG,
823 "RADIUS DAS: Acct-Multi-Session-Id cannot match");
824 return NULL;
825 }
826 count = 0;
827
828 for (sta = hapd->sta_list; sta; sta = sta->next) {
829 if (!sta->radius_das_match)
830 continue;
831 if (!sta->eapol_sm ||
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800832 !sta->eapol_sm->acct_multi_session_id) {
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800833 sta->radius_das_match = 0;
834 continue;
835 }
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800836 os_snprintf(buf, sizeof(buf), "%016llX",
837 (unsigned long long)
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800838 sta->eapol_sm->acct_multi_session_id);
839 if (os_memcmp(attr->acct_multi_session_id, buf, 16) !=
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800840 0)
841 sta->radius_das_match = 0;
842 else
843 count++;
844 }
845
846 if (count == 0) {
847 wpa_printf(MSG_DEBUG,
848 "RADIUS DAS: No matches remaining after Acct-Multi-Session-Id check");
849 return NULL;
850 }
851 wpa_printf(MSG_DEBUG,
852 "RADIUS DAS: Acct-Multi-Session-Id match");
853 }
854
855 if (attr->cui) {
856 num_attr++;
857 count = 0;
858
Dmitry Shmidt04949592012-07-19 12:16:46 -0700859 for (sta = hapd->sta_list; sta; sta = sta->next) {
860 struct wpabuf *cui;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800861
862 if (!sta->radius_das_match)
863 continue;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700864 cui = ieee802_1x_get_radius_cui(sta->eapol_sm);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800865 if (!cui || wpabuf_len(cui) != attr->cui_len ||
Dmitry Shmidt04949592012-07-19 12:16:46 -0700866 os_memcmp(wpabuf_head(cui), attr->cui,
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800867 attr->cui_len) != 0)
868 sta->radius_das_match = 0;
869 else
870 count++;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700871 }
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800872
873 if (count == 0) {
874 wpa_printf(MSG_DEBUG,
875 "RADIUS DAS: No matches remaining after Chargeable-User-Identity check");
876 return NULL;
877 }
878 wpa_printf(MSG_DEBUG,
879 "RADIUS DAS: Chargeable-User-Identity match");
Dmitry Shmidt04949592012-07-19 12:16:46 -0700880 }
881
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800882 if (attr->user_name) {
883 num_attr++;
884 count = 0;
885
Dmitry Shmidt04949592012-07-19 12:16:46 -0700886 for (sta = hapd->sta_list; sta; sta = sta->next) {
887 u8 *identity;
888 size_t identity_len;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800889
890 if (!sta->radius_das_match)
891 continue;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700892 identity = ieee802_1x_get_identity(sta->eapol_sm,
893 &identity_len);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800894 if (!identity ||
895 identity_len != attr->user_name_len ||
Dmitry Shmidt04949592012-07-19 12:16:46 -0700896 os_memcmp(identity, attr->user_name, identity_len)
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800897 != 0)
898 sta->radius_das_match = 0;
899 else
900 count++;
901 }
902
903 if (count == 0) {
904 wpa_printf(MSG_DEBUG,
905 "RADIUS DAS: No matches remaining after User-Name check");
906 return NULL;
907 }
908 wpa_printf(MSG_DEBUG,
909 "RADIUS DAS: User-Name match");
910 }
911
912 if (num_attr == 0) {
913 /*
914 * In theory, we could match all current associations, but it
915 * seems safer to just reject requests that do not include any
916 * session identification attributes.
917 */
918 wpa_printf(MSG_DEBUG,
919 "RADIUS DAS: No session identification attributes included");
920 return NULL;
921 }
922
923 selected = NULL;
924 for (sta = hapd->sta_list; sta; sta = sta->next) {
925 if (sta->radius_das_match) {
926 if (selected) {
927 *multi = 1;
928 return NULL;
929 }
930 selected = sta;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700931 }
932 }
933
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800934 return selected;
935}
936
937
938static int hostapd_das_disconnect_pmksa(struct hostapd_data *hapd,
939 struct radius_das_attrs *attr)
940{
941 if (!hapd->wpa_auth)
942 return -1;
943 return wpa_auth_radius_das_disconnect_pmksa(hapd->wpa_auth, attr);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700944}
945
946
947static enum radius_das_res
948hostapd_das_disconnect(void *ctx, struct radius_das_attrs *attr)
949{
950 struct hostapd_data *hapd = ctx;
951 struct sta_info *sta;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800952 int multi;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700953
954 if (hostapd_das_nas_mismatch(hapd, attr))
955 return RADIUS_DAS_NAS_MISMATCH;
956
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800957 sta = hostapd_das_find_sta(hapd, attr, &multi);
958 if (sta == NULL) {
959 if (multi) {
960 wpa_printf(MSG_DEBUG,
961 "RADIUS DAS: Multiple sessions match - not supported");
962 return RADIUS_DAS_MULTI_SESSION_MATCH;
963 }
964 if (hostapd_das_disconnect_pmksa(hapd, attr) == 0) {
965 wpa_printf(MSG_DEBUG,
966 "RADIUS DAS: PMKSA cache entry matched");
967 return RADIUS_DAS_SUCCESS;
968 }
969 wpa_printf(MSG_DEBUG, "RADIUS DAS: No matching session found");
Dmitry Shmidt04949592012-07-19 12:16:46 -0700970 return RADIUS_DAS_SESSION_NOT_FOUND;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800971 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700972
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800973 wpa_printf(MSG_DEBUG, "RADIUS DAS: Found a matching session " MACSTR
974 " - disconnecting", MAC2STR(sta->addr));
Dmitry Shmidt13ca8d82014-02-20 10:18:40 -0800975 wpa_auth_pmksa_remove(hapd->wpa_auth, sta->addr);
976
Dmitry Shmidt04949592012-07-19 12:16:46 -0700977 hostapd_drv_sta_deauth(hapd, sta->addr,
978 WLAN_REASON_PREV_AUTH_NOT_VALID);
979 ap_sta_deauthenticate(hapd, sta, WLAN_REASON_PREV_AUTH_NOT_VALID);
980
981 return RADIUS_DAS_SUCCESS;
982}
983
Roshan Pius3a1667e2018-07-03 15:17:14 -0700984
985#ifdef CONFIG_HS20
986static enum radius_das_res
987hostapd_das_coa(void *ctx, struct radius_das_attrs *attr)
988{
989 struct hostapd_data *hapd = ctx;
990 struct sta_info *sta;
991 int multi;
992
993 if (hostapd_das_nas_mismatch(hapd, attr))
994 return RADIUS_DAS_NAS_MISMATCH;
995
996 sta = hostapd_das_find_sta(hapd, attr, &multi);
997 if (!sta) {
998 if (multi) {
999 wpa_printf(MSG_DEBUG,
1000 "RADIUS DAS: Multiple sessions match - not supported");
1001 return RADIUS_DAS_MULTI_SESSION_MATCH;
1002 }
1003 wpa_printf(MSG_DEBUG, "RADIUS DAS: No matching session found");
1004 return RADIUS_DAS_SESSION_NOT_FOUND;
1005 }
1006
1007 wpa_printf(MSG_DEBUG, "RADIUS DAS: Found a matching session " MACSTR
1008 " - CoA", MAC2STR(sta->addr));
1009
1010 if (attr->hs20_t_c_filtering) {
1011 if (attr->hs20_t_c_filtering[0] & BIT(0)) {
1012 wpa_printf(MSG_DEBUG,
1013 "HS 2.0: Unexpected Terms and Conditions filtering required in CoA-Request");
1014 return RADIUS_DAS_COA_FAILED;
1015 }
1016
1017 hs20_t_c_filtering(hapd, sta, 0);
1018 }
1019
1020 return RADIUS_DAS_SUCCESS;
1021}
1022#else /* CONFIG_HS20 */
1023#define hostapd_das_coa NULL
1024#endif /* CONFIG_HS20 */
1025
Hai Shalomc3565922019-10-28 11:58:20 -07001026
1027#ifdef CONFIG_SQLITE
1028
1029static int db_table_exists(sqlite3 *db, const char *name)
1030{
1031 char cmd[128];
1032
1033 os_snprintf(cmd, sizeof(cmd), "SELECT 1 FROM %s;", name);
1034 return sqlite3_exec(db, cmd, NULL, NULL, NULL) == SQLITE_OK;
1035}
1036
1037
1038static int db_table_create_radius_attributes(sqlite3 *db)
1039{
1040 char *err = NULL;
1041 const char *sql =
1042 "CREATE TABLE radius_attributes("
1043 " id INTEGER PRIMARY KEY,"
1044 " sta TEXT,"
1045 " reqtype TEXT,"
1046 " attr TEXT"
1047 ");"
1048 "CREATE INDEX idx_sta_reqtype ON radius_attributes(sta,reqtype);";
1049
1050 wpa_printf(MSG_DEBUG,
1051 "Adding database table for RADIUS attribute information");
1052 if (sqlite3_exec(db, sql, NULL, NULL, &err) != SQLITE_OK) {
1053 wpa_printf(MSG_ERROR, "SQLite error: %s", err);
1054 sqlite3_free(err);
1055 return -1;
1056 }
1057
1058 return 0;
1059}
1060
1061#endif /* CONFIG_SQLITE */
1062
Dmitry Shmidt04949592012-07-19 12:16:46 -07001063#endif /* CONFIG_NO_RADIUS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001064
1065
1066/**
1067 * hostapd_setup_bss - Per-BSS setup (initialization)
1068 * @hapd: Pointer to BSS data
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001069 * @first: Whether this BSS is the first BSS of an interface; -1 = not first,
1070 * but interface may exist
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001071 *
1072 * This function is used to initialize all per-BSS data structures and
1073 * resources. This gets called in a loop for each BSS when an interface is
1074 * initialized. Most of the modules that are initialized here will be
1075 * deinitialized in hostapd_cleanup().
1076 */
1077static int hostapd_setup_bss(struct hostapd_data *hapd, int first)
1078{
1079 struct hostapd_bss_config *conf = hapd->conf;
Dmitry Shmidt9d9e6022015-04-23 10:34:55 -07001080 u8 ssid[SSID_MAX_LEN + 1];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001081 int ssid_len, set_ssid;
1082 char force_ifname[IFNAMSIZ];
1083 u8 if_addr[ETH_ALEN];
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001084 int flush_old_stations = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001085
Dmitry Shmidt54605472013-11-08 11:10:19 -08001086 wpa_printf(MSG_DEBUG, "%s(hapd=%p (%s), first=%d)",
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001087 __func__, hapd, conf->iface, first);
Dmitry Shmidt54605472013-11-08 11:10:19 -08001088
Dmitry Shmidt50b691d2014-05-21 14:01:45 -07001089#ifdef EAP_SERVER_TNC
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001090 if (conf->tnc && tncs_global_init() < 0) {
Dmitry Shmidt50b691d2014-05-21 14:01:45 -07001091 wpa_printf(MSG_ERROR, "Failed to initialize TNCS");
1092 return -1;
1093 }
1094#endif /* EAP_SERVER_TNC */
1095
Dmitry Shmidt54605472013-11-08 11:10:19 -08001096 if (hapd->started) {
1097 wpa_printf(MSG_ERROR, "%s: Interface %s was already started",
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001098 __func__, conf->iface);
Dmitry Shmidt54605472013-11-08 11:10:19 -08001099 return -1;
1100 }
1101 hapd->started = 1;
1102
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001103 if (!first || first == -1) {
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08001104 u8 *addr = hapd->own_addr;
1105
1106 if (!is_zero_ether_addr(conf->bssid)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001107 /* Allocate the configured BSSID. */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001108 os_memcpy(hapd->own_addr, conf->bssid, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001109
1110 if (hostapd_mac_comp(hapd->own_addr,
1111 hapd->iface->bss[0]->own_addr) ==
1112 0) {
1113 wpa_printf(MSG_ERROR, "BSS '%s' may not have "
1114 "BSSID set to the MAC address of "
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001115 "the radio", conf->iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001116 return -1;
1117 }
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08001118 } else if (hapd->iconf->use_driver_iface_addr) {
1119 addr = NULL;
1120 } else {
1121 /* Allocate the next available BSSID. */
1122 do {
1123 inc_byte_array(hapd->own_addr, ETH_ALEN);
1124 } while (mac_in_conf(hapd->iconf, hapd->own_addr));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001125 }
1126
Dmitry Shmidt54605472013-11-08 11:10:19 -08001127 hapd->interface_added = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001128 if (hostapd_if_add(hapd->iface->bss[0], WPA_IF_AP_BSS,
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08001129 conf->iface, addr, hapd,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001130 &hapd->drv_priv, force_ifname, if_addr,
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001131 conf->bridge[0] ? conf->bridge : NULL,
1132 first == -1)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001133 wpa_printf(MSG_ERROR, "Failed to add BSS (BSSID="
1134 MACSTR ")", MAC2STR(hapd->own_addr));
Dmitry Shmidt3cf6f792013-12-18 13:12:19 -08001135 hapd->interface_added = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001136 return -1;
1137 }
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08001138
1139 if (!addr)
1140 os_memcpy(hapd->own_addr, if_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001141 }
1142
1143 if (conf->wmm_enabled < 0)
1144 conf->wmm_enabled = hapd->iconf->ieee80211n;
1145
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001146#ifdef CONFIG_IEEE80211R_AP
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001147 if (is_zero_ether_addr(conf->r1_key_holder))
1148 os_memcpy(conf->r1_key_holder, hapd->own_addr, ETH_ALEN);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001149#endif /* CONFIG_IEEE80211R_AP */
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001150
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001151#ifdef CONFIG_MESH
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001152 if ((hapd->conf->mesh & MESH_ENABLED) && hapd->iface->mconf == NULL)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001153 flush_old_stations = 0;
1154#endif /* CONFIG_MESH */
1155
1156 if (flush_old_stations)
1157 hostapd_flush_old_stations(hapd,
1158 WLAN_REASON_PREV_AUTH_NOT_VALID);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001159 hostapd_set_privacy(hapd, 0);
1160
1161 hostapd_broadcast_wep_clear(hapd);
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001162 if (hostapd_setup_encryption(conf->iface, hapd))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001163 return -1;
1164
1165 /*
1166 * Fetch the SSID from the system and use it or,
1167 * if one was specified in the config file, verify they
1168 * match.
1169 */
1170 ssid_len = hostapd_get_ssid(hapd, ssid, sizeof(ssid));
1171 if (ssid_len < 0) {
1172 wpa_printf(MSG_ERROR, "Could not read SSID from system");
1173 return -1;
1174 }
1175 if (conf->ssid.ssid_set) {
1176 /*
1177 * If SSID is specified in the config file and it differs
1178 * from what is being used then force installation of the
1179 * new SSID.
1180 */
1181 set_ssid = (conf->ssid.ssid_len != (size_t) ssid_len ||
1182 os_memcmp(conf->ssid.ssid, ssid, ssid_len) != 0);
1183 } else {
1184 /*
1185 * No SSID in the config file; just use the one we got
1186 * from the system.
1187 */
1188 set_ssid = 0;
1189 conf->ssid.ssid_len = ssid_len;
1190 os_memcpy(conf->ssid.ssid, ssid, conf->ssid.ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001191 }
1192
1193 if (!hostapd_drv_none(hapd)) {
1194 wpa_printf(MSG_ERROR, "Using interface %s with hwaddr " MACSTR
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001195 " and ssid \"%s\"",
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001196 conf->iface, MAC2STR(hapd->own_addr),
1197 wpa_ssid_txt(conf->ssid.ssid, conf->ssid.ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001198 }
1199
1200 if (hostapd_setup_wpa_psk(conf)) {
1201 wpa_printf(MSG_ERROR, "WPA-PSK setup failed.");
1202 return -1;
1203 }
1204
1205 /* Set SSID for the kernel driver (to be used in beacon and probe
1206 * response frames) */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001207 if (set_ssid && hostapd_set_ssid(hapd, conf->ssid.ssid,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001208 conf->ssid.ssid_len)) {
1209 wpa_printf(MSG_ERROR, "Could not set SSID for kernel driver");
1210 return -1;
1211 }
1212
Dmitry Shmidt818ea482014-03-10 13:15:21 -07001213 if (wpa_debug_level <= MSG_MSGDUMP)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001214 conf->radius->msg_dumps = 1;
1215#ifndef CONFIG_NO_RADIUS
Hai Shalomc3565922019-10-28 11:58:20 -07001216
1217#ifdef CONFIG_SQLITE
1218 if (conf->radius_req_attr_sqlite) {
1219 if (sqlite3_open(conf->radius_req_attr_sqlite,
1220 &hapd->rad_attr_db)) {
1221 wpa_printf(MSG_ERROR, "Could not open SQLite file '%s'",
1222 conf->radius_req_attr_sqlite);
1223 return -1;
1224 }
1225
1226 wpa_printf(MSG_DEBUG, "Opening RADIUS attribute database: %s",
1227 conf->radius_req_attr_sqlite);
1228 if (!db_table_exists(hapd->rad_attr_db, "radius_attributes") &&
1229 db_table_create_radius_attributes(hapd->rad_attr_db) < 0)
1230 return -1;
1231 }
1232#endif /* CONFIG_SQLITE */
1233
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001234 hapd->radius = radius_client_init(hapd, conf->radius);
1235 if (hapd->radius == NULL) {
1236 wpa_printf(MSG_ERROR, "RADIUS client initialization failed.");
1237 return -1;
1238 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001239
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001240 if (conf->radius_das_port) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001241 struct radius_das_conf das_conf;
1242 os_memset(&das_conf, 0, sizeof(das_conf));
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001243 das_conf.port = conf->radius_das_port;
1244 das_conf.shared_secret = conf->radius_das_shared_secret;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001245 das_conf.shared_secret_len =
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001246 conf->radius_das_shared_secret_len;
1247 das_conf.client_addr = &conf->radius_das_client_addr;
1248 das_conf.time_window = conf->radius_das_time_window;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001249 das_conf.require_event_timestamp =
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001250 conf->radius_das_require_event_timestamp;
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07001251 das_conf.require_message_authenticator =
1252 conf->radius_das_require_message_authenticator;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001253 das_conf.ctx = hapd;
1254 das_conf.disconnect = hostapd_das_disconnect;
Roshan Pius3a1667e2018-07-03 15:17:14 -07001255 das_conf.coa = hostapd_das_coa;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001256 hapd->radius_das = radius_das_init(&das_conf);
1257 if (hapd->radius_das == NULL) {
1258 wpa_printf(MSG_ERROR, "RADIUS DAS initialization "
1259 "failed.");
1260 return -1;
1261 }
1262 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001263#endif /* CONFIG_NO_RADIUS */
1264
1265 if (hostapd_acl_init(hapd)) {
1266 wpa_printf(MSG_ERROR, "ACL initialization failed.");
1267 return -1;
1268 }
1269 if (hostapd_init_wps(hapd, conf))
1270 return -1;
1271
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001272#ifdef CONFIG_DPP
1273 hapd->gas = gas_query_ap_init(hapd, hapd->msg_ctx);
1274 if (!hapd->gas)
1275 return -1;
1276 if (hostapd_dpp_init(hapd))
1277 return -1;
1278#endif /* CONFIG_DPP */
1279
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001280 if (authsrv_init(hapd) < 0)
1281 return -1;
1282
1283 if (ieee802_1x_init(hapd)) {
1284 wpa_printf(MSG_ERROR, "IEEE 802.1X initialization failed.");
1285 return -1;
1286 }
1287
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001288 if ((conf->wpa || conf->osen) && hostapd_setup_wpa(hapd))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001289 return -1;
1290
1291 if (accounting_init(hapd)) {
1292 wpa_printf(MSG_ERROR, "Accounting initialization failed.");
1293 return -1;
1294 }
1295
Dmitry Shmidt04949592012-07-19 12:16:46 -07001296#ifdef CONFIG_INTERWORKING
1297 if (gas_serv_init(hapd)) {
1298 wpa_printf(MSG_ERROR, "GAS server initialization failed");
1299 return -1;
1300 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001301
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001302 if (conf->qos_map_set_len &&
1303 hostapd_drv_set_qos_map(hapd, conf->qos_map_set,
1304 conf->qos_map_set_len)) {
1305 wpa_printf(MSG_ERROR, "Failed to initialize QoS Map");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001306 return -1;
1307 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001308#endif /* CONFIG_INTERWORKING */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001309
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001310 if (conf->bss_load_update_period && bss_load_update_init(hapd)) {
1311 wpa_printf(MSG_ERROR, "BSS Load initialization failed");
1312 return -1;
1313 }
1314
1315 if (conf->proxy_arp) {
1316 if (x_snoop_init(hapd)) {
1317 wpa_printf(MSG_ERROR,
1318 "Generic snooping infrastructure initialization failed");
1319 return -1;
1320 }
1321
1322 if (dhcp_snoop_init(hapd)) {
1323 wpa_printf(MSG_ERROR,
1324 "DHCP snooping initialization failed");
1325 return -1;
1326 }
1327
1328 if (ndisc_snoop_init(hapd)) {
1329 wpa_printf(MSG_ERROR,
1330 "Neighbor Discovery snooping initialization failed");
1331 return -1;
1332 }
1333 }
1334
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001335 if (!hostapd_drv_none(hapd) && vlan_init(hapd)) {
1336 wpa_printf(MSG_ERROR, "VLAN initialization failed.");
1337 return -1;
1338 }
1339
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001340 if (!conf->start_disabled && ieee802_11_set_beacon(hapd) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001341 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001342
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001343 if (hapd->wpa_auth && wpa_init_keys(hapd->wpa_auth) < 0)
1344 return -1;
1345
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001346 if (hapd->driver && hapd->driver->set_operstate)
1347 hapd->driver->set_operstate(hapd->drv_priv, 1);
1348
1349 return 0;
1350}
1351
1352
1353static void hostapd_tx_queue_params(struct hostapd_iface *iface)
1354{
1355 struct hostapd_data *hapd = iface->bss[0];
1356 int i;
1357 struct hostapd_tx_queue_params *p;
1358
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001359#ifdef CONFIG_MESH
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001360 if ((hapd->conf->mesh & MESH_ENABLED) && iface->mconf == NULL)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001361 return;
1362#endif /* CONFIG_MESH */
1363
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001364 for (i = 0; i < NUM_TX_QUEUES; i++) {
1365 p = &iface->conf->tx_queue[i];
1366
1367 if (hostapd_set_tx_queue_params(hapd, i, p->aifs, p->cwmin,
1368 p->cwmax, p->burst)) {
1369 wpa_printf(MSG_DEBUG, "Failed to set TX queue "
1370 "parameters for queue %d.", i);
1371 /* Continue anyway */
1372 }
1373 }
1374}
1375
1376
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001377static int hostapd_set_acl_list(struct hostapd_data *hapd,
1378 struct mac_acl_entry *mac_acl,
1379 int n_entries, u8 accept_acl)
1380{
1381 struct hostapd_acl_params *acl_params;
1382 int i, err;
1383
1384 acl_params = os_zalloc(sizeof(*acl_params) +
1385 (n_entries * sizeof(acl_params->mac_acl[0])));
1386 if (!acl_params)
1387 return -ENOMEM;
1388
1389 for (i = 0; i < n_entries; i++)
1390 os_memcpy(acl_params->mac_acl[i].addr, mac_acl[i].addr,
1391 ETH_ALEN);
1392
1393 acl_params->acl_policy = accept_acl;
1394 acl_params->num_mac_acl = n_entries;
1395
1396 err = hostapd_drv_set_acl(hapd, acl_params);
1397
1398 os_free(acl_params);
1399
1400 return err;
1401}
1402
1403
1404static void hostapd_set_acl(struct hostapd_data *hapd)
1405{
1406 struct hostapd_config *conf = hapd->iconf;
1407 int err;
1408 u8 accept_acl;
1409
1410 if (hapd->iface->drv_max_acl_mac_addrs == 0)
1411 return;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001412
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001413 if (conf->bss[0]->macaddr_acl == DENY_UNLESS_ACCEPTED) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001414 accept_acl = 1;
1415 err = hostapd_set_acl_list(hapd, conf->bss[0]->accept_mac,
1416 conf->bss[0]->num_accept_mac,
1417 accept_acl);
1418 if (err) {
1419 wpa_printf(MSG_DEBUG, "Failed to set accept acl");
1420 return;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001421 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001422 } else if (conf->bss[0]->macaddr_acl == ACCEPT_UNLESS_DENIED) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001423 accept_acl = 0;
1424 err = hostapd_set_acl_list(hapd, conf->bss[0]->deny_mac,
1425 conf->bss[0]->num_deny_mac,
1426 accept_acl);
1427 if (err) {
1428 wpa_printf(MSG_DEBUG, "Failed to set deny acl");
1429 return;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001430 }
1431 }
1432}
1433
1434
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001435static int start_ctrl_iface_bss(struct hostapd_data *hapd)
1436{
1437 if (!hapd->iface->interfaces ||
1438 !hapd->iface->interfaces->ctrl_iface_init)
1439 return 0;
1440
1441 if (hapd->iface->interfaces->ctrl_iface_init(hapd)) {
1442 wpa_printf(MSG_ERROR,
1443 "Failed to setup control interface for %s",
1444 hapd->conf->iface);
1445 return -1;
1446 }
1447
1448 return 0;
1449}
1450
1451
1452static int start_ctrl_iface(struct hostapd_iface *iface)
1453{
1454 size_t i;
1455
1456 if (!iface->interfaces || !iface->interfaces->ctrl_iface_init)
1457 return 0;
1458
1459 for (i = 0; i < iface->num_bss; i++) {
1460 struct hostapd_data *hapd = iface->bss[i];
1461 if (iface->interfaces->ctrl_iface_init(hapd)) {
1462 wpa_printf(MSG_ERROR,
1463 "Failed to setup control interface for %s",
1464 hapd->conf->iface);
1465 return -1;
1466 }
1467 }
1468
1469 return 0;
1470}
1471
1472
1473static void channel_list_update_timeout(void *eloop_ctx, void *timeout_ctx)
1474{
1475 struct hostapd_iface *iface = eloop_ctx;
1476
1477 if (!iface->wait_channel_update) {
1478 wpa_printf(MSG_INFO, "Channel list update timeout, but interface was not waiting for it");
1479 return;
1480 }
1481
1482 /*
1483 * It is possible that the existing channel list is acceptable, so try
1484 * to proceed.
1485 */
1486 wpa_printf(MSG_DEBUG, "Channel list update timeout - try to continue anyway");
1487 setup_interface2(iface);
1488}
1489
1490
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001491void hostapd_channel_list_updated(struct hostapd_iface *iface, int initiator)
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001492{
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001493 if (!iface->wait_channel_update || initiator != REGDOM_SET_BY_USER)
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001494 return;
1495
1496 wpa_printf(MSG_DEBUG, "Channel list updated - continue setup");
1497 eloop_cancel_timeout(channel_list_update_timeout, iface, NULL);
1498 setup_interface2(iface);
1499}
1500
1501
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001502static int setup_interface(struct hostapd_iface *iface)
1503{
1504 struct hostapd_data *hapd = iface->bss[0];
1505 size_t i;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001506
Dmitry Shmidta38abf92014-03-06 13:38:44 -08001507 /*
1508 * It is possible that setup_interface() is called after the interface
1509 * was disabled etc., in which case driver_ap_teardown is possibly set
1510 * to 1. Clear it here so any other key/station deletion, which is not
1511 * part of a teardown flow, would also call the relevant driver
1512 * callbacks.
1513 */
1514 iface->driver_ap_teardown = 0;
1515
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001516 if (!iface->phy[0]) {
1517 const char *phy = hostapd_drv_get_radio_name(hapd);
1518 if (phy) {
1519 wpa_printf(MSG_DEBUG, "phy: %s", phy);
1520 os_strlcpy(iface->phy, phy, sizeof(iface->phy));
1521 }
1522 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001523
1524 /*
1525 * Make sure that all BSSes get configured with a pointer to the same
1526 * driver interface.
1527 */
1528 for (i = 1; i < iface->num_bss; i++) {
1529 iface->bss[i]->driver = hapd->driver;
1530 iface->bss[i]->drv_priv = hapd->drv_priv;
1531 }
1532
1533 if (hostapd_validate_bssid_configuration(iface))
1534 return -1;
1535
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001536 /*
1537 * Initialize control interfaces early to allow external monitoring of
1538 * channel setup operations that may take considerable amount of time
1539 * especially for DFS cases.
1540 */
1541 if (start_ctrl_iface(iface))
1542 return -1;
1543
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001544 if (hapd->iconf->country[0] && hapd->iconf->country[1]) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001545 char country[4], previous_country[4];
1546
1547 hostapd_set_state(iface, HAPD_IFACE_COUNTRY_UPDATE);
1548 if (hostapd_get_country(hapd, previous_country) < 0)
1549 previous_country[0] = '\0';
1550
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001551 os_memcpy(country, hapd->iconf->country, 3);
1552 country[3] = '\0';
1553 if (hostapd_set_country(hapd, country) < 0) {
1554 wpa_printf(MSG_ERROR, "Failed to set country code");
1555 return -1;
1556 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001557
1558 wpa_printf(MSG_DEBUG, "Previous country code %s, new country code %s",
1559 previous_country, country);
1560
1561 if (os_strncmp(previous_country, country, 2) != 0) {
1562 wpa_printf(MSG_DEBUG, "Continue interface setup after channel list update");
1563 iface->wait_channel_update = 1;
Dmitry Shmidt97672262014-02-03 13:02:54 -08001564 eloop_register_timeout(5, 0,
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001565 channel_list_update_timeout,
1566 iface, NULL);
1567 return 0;
1568 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001569 }
1570
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001571 return setup_interface2(iface);
1572}
1573
1574
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001575static int configured_fixed_chan_to_freq(struct hostapd_iface *iface)
1576{
1577 int freq, i, j;
1578
1579 if (!iface->conf->channel)
1580 return 0;
1581 if (iface->conf->op_class) {
1582 freq = ieee80211_chan_to_freq(NULL, iface->conf->op_class,
1583 iface->conf->channel);
1584 if (freq < 0) {
1585 wpa_printf(MSG_INFO,
1586 "Could not convert op_class %u channel %u to operating frequency",
1587 iface->conf->op_class, iface->conf->channel);
1588 return -1;
1589 }
1590 iface->freq = freq;
1591 return 0;
1592 }
1593
1594 /* Old configurations using only 2.4/5/60 GHz bands may not specify the
1595 * op_class parameter. Select a matching channel from the configured
1596 * mode using the channel parameter for these cases.
1597 */
1598 for (j = 0; j < iface->num_hw_features; j++) {
1599 struct hostapd_hw_modes *mode = &iface->hw_features[j];
1600
1601 if (iface->conf->hw_mode != HOSTAPD_MODE_IEEE80211ANY &&
1602 iface->conf->hw_mode != mode->mode)
1603 continue;
1604 for (i = 0; i < mode->num_channels; i++) {
1605 struct hostapd_channel_data *chan = &mode->channels[i];
1606
1607 if (chan->chan == iface->conf->channel &&
1608 !is_6ghz_freq(chan->freq)) {
1609 iface->freq = chan->freq;
1610 return 0;
1611 }
1612 }
1613 }
1614
1615 wpa_printf(MSG_INFO, "Could not determine operating frequency");
1616 return -1;
1617}
1618
1619
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001620static int setup_interface2(struct hostapd_iface *iface)
1621{
1622 iface->wait_channel_update = 0;
1623
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001624 if (hostapd_get_hw_features(iface)) {
1625 /* Not all drivers support this yet, so continue without hw
1626 * feature data. */
1627 } else {
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001628 int ret;
1629
1630 ret = configured_fixed_chan_to_freq(iface);
1631 if (ret < 0)
1632 goto fail;
1633
1634 if (iface->conf->op_class) {
1635 int ch_width;
1636
1637 ch_width = op_class_to_ch_width(iface->conf->op_class);
1638 hostapd_set_oper_chwidth(iface->conf, ch_width);
1639 }
1640
1641 ret = hostapd_select_hw_mode(iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001642 if (ret < 0) {
1643 wpa_printf(MSG_ERROR, "Could not select hw_mode and "
1644 "channel. (%d)", ret);
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001645 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001646 }
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001647 if (ret == 1) {
1648 wpa_printf(MSG_DEBUG, "Interface initialization will be completed in a callback (ACS)");
1649 return 0;
1650 }
Hai Shalomc3565922019-10-28 11:58:20 -07001651 ret = hostapd_check_edmg_capab(iface);
1652 if (ret < 0)
1653 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001654 ret = hostapd_check_ht_capab(iface);
1655 if (ret < 0)
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001656 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001657 if (ret == 1) {
1658 wpa_printf(MSG_DEBUG, "Interface initialization will "
1659 "be completed in a callback");
1660 return 0;
1661 }
Dmitry Shmidt051af732013-10-22 13:52:46 -07001662
1663 if (iface->conf->ieee80211h)
1664 wpa_printf(MSG_DEBUG, "DFS support is enabled");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001665 }
1666 return hostapd_setup_interface_complete(iface, 0);
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001667
1668fail:
1669 hostapd_set_state(iface, HAPD_IFACE_DISABLED);
1670 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
1671 if (iface->interfaces && iface->interfaces->terminate_on_error)
1672 eloop_terminate();
1673 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001674}
1675
1676
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001677#ifdef CONFIG_FST
1678
1679static const u8 * fst_hostapd_get_bssid_cb(void *ctx)
1680{
1681 struct hostapd_data *hapd = ctx;
1682
1683 return hapd->own_addr;
1684}
1685
1686
1687static void fst_hostapd_get_channel_info_cb(void *ctx,
1688 enum hostapd_hw_mode *hw_mode,
1689 u8 *channel)
1690{
1691 struct hostapd_data *hapd = ctx;
1692
1693 *hw_mode = ieee80211_freq_to_chan(hapd->iface->freq, channel);
1694}
1695
1696
1697static void fst_hostapd_set_ies_cb(void *ctx, const struct wpabuf *fst_ies)
1698{
1699 struct hostapd_data *hapd = ctx;
1700
1701 if (hapd->iface->fst_ies != fst_ies) {
1702 hapd->iface->fst_ies = fst_ies;
1703 if (ieee802_11_set_beacon(hapd))
1704 wpa_printf(MSG_WARNING, "FST: Cannot set beacon");
1705 }
1706}
1707
1708
1709static int fst_hostapd_send_action_cb(void *ctx, const u8 *da,
1710 struct wpabuf *buf)
1711{
1712 struct hostapd_data *hapd = ctx;
1713
1714 return hostapd_drv_send_action(hapd, hapd->iface->freq, 0, da,
1715 wpabuf_head(buf), wpabuf_len(buf));
1716}
1717
1718
1719static const struct wpabuf * fst_hostapd_get_mb_ie_cb(void *ctx, const u8 *addr)
1720{
1721 struct hostapd_data *hapd = ctx;
1722 struct sta_info *sta = ap_get_sta(hapd, addr);
1723
1724 return sta ? sta->mb_ies : NULL;
1725}
1726
1727
1728static void fst_hostapd_update_mb_ie_cb(void *ctx, const u8 *addr,
1729 const u8 *buf, size_t size)
1730{
1731 struct hostapd_data *hapd = ctx;
1732 struct sta_info *sta = ap_get_sta(hapd, addr);
1733
1734 if (sta) {
1735 struct mb_ies_info info;
1736
1737 if (!mb_ies_info_by_ies(&info, buf, size)) {
1738 wpabuf_free(sta->mb_ies);
1739 sta->mb_ies = mb_ies_by_info(&info);
1740 }
1741 }
1742}
1743
1744
1745static const u8 * fst_hostapd_get_sta(struct fst_get_peer_ctx **get_ctx,
1746 Boolean mb_only)
1747{
1748 struct sta_info *s = (struct sta_info *) *get_ctx;
1749
1750 if (mb_only) {
1751 for (; s && !s->mb_ies; s = s->next)
1752 ;
1753 }
1754
1755 if (s) {
1756 *get_ctx = (struct fst_get_peer_ctx *) s->next;
1757
1758 return s->addr;
1759 }
1760
1761 *get_ctx = NULL;
1762 return NULL;
1763}
1764
1765
1766static const u8 * fst_hostapd_get_peer_first(void *ctx,
1767 struct fst_get_peer_ctx **get_ctx,
1768 Boolean mb_only)
1769{
1770 struct hostapd_data *hapd = ctx;
1771
1772 *get_ctx = (struct fst_get_peer_ctx *) hapd->sta_list;
1773
1774 return fst_hostapd_get_sta(get_ctx, mb_only);
1775}
1776
1777
1778static const u8 * fst_hostapd_get_peer_next(void *ctx,
1779 struct fst_get_peer_ctx **get_ctx,
1780 Boolean mb_only)
1781{
1782 return fst_hostapd_get_sta(get_ctx, mb_only);
1783}
1784
1785
1786void fst_hostapd_fill_iface_obj(struct hostapd_data *hapd,
1787 struct fst_wpa_obj *iface_obj)
1788{
1789 iface_obj->ctx = hapd;
1790 iface_obj->get_bssid = fst_hostapd_get_bssid_cb;
1791 iface_obj->get_channel_info = fst_hostapd_get_channel_info_cb;
1792 iface_obj->set_ies = fst_hostapd_set_ies_cb;
1793 iface_obj->send_action = fst_hostapd_send_action_cb;
1794 iface_obj->get_mb_ie = fst_hostapd_get_mb_ie_cb;
1795 iface_obj->update_mb_ie = fst_hostapd_update_mb_ie_cb;
1796 iface_obj->get_peer_first = fst_hostapd_get_peer_first;
1797 iface_obj->get_peer_next = fst_hostapd_get_peer_next;
1798}
1799
1800#endif /* CONFIG_FST */
1801
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001802#ifdef CONFIG_OWE
1803
1804static int hostapd_owe_iface_iter(struct hostapd_iface *iface, void *ctx)
1805{
1806 struct hostapd_data *hapd = ctx;
1807 size_t i;
1808
1809 for (i = 0; i < iface->num_bss; i++) {
1810 struct hostapd_data *bss = iface->bss[i];
1811
1812 if (os_strcmp(hapd->conf->owe_transition_ifname,
1813 bss->conf->iface) != 0)
1814 continue;
1815
1816 wpa_printf(MSG_DEBUG,
1817 "OWE: ifname=%s found transition mode ifname=%s BSSID "
1818 MACSTR " SSID %s",
1819 hapd->conf->iface, bss->conf->iface,
1820 MAC2STR(bss->own_addr),
1821 wpa_ssid_txt(bss->conf->ssid.ssid,
1822 bss->conf->ssid.ssid_len));
1823 if (!bss->conf->ssid.ssid_set || !bss->conf->ssid.ssid_len ||
1824 is_zero_ether_addr(bss->own_addr))
1825 continue;
1826
1827 os_memcpy(hapd->conf->owe_transition_bssid, bss->own_addr,
1828 ETH_ALEN);
1829 os_memcpy(hapd->conf->owe_transition_ssid,
1830 bss->conf->ssid.ssid, bss->conf->ssid.ssid_len);
1831 hapd->conf->owe_transition_ssid_len = bss->conf->ssid.ssid_len;
1832 wpa_printf(MSG_DEBUG,
1833 "OWE: Copied transition mode information");
1834 return 1;
1835 }
1836
1837 return 0;
1838}
1839
1840
1841int hostapd_owe_trans_get_info(struct hostapd_data *hapd)
1842{
1843 if (hapd->conf->owe_transition_ssid_len > 0 &&
1844 !is_zero_ether_addr(hapd->conf->owe_transition_bssid))
1845 return 0;
1846
1847 /* Find transition mode SSID/BSSID information from a BSS operated by
1848 * this hostapd instance. */
1849 if (!hapd->iface->interfaces ||
1850 !hapd->iface->interfaces->for_each_interface)
1851 return hostapd_owe_iface_iter(hapd->iface, hapd);
1852 else
1853 return hapd->iface->interfaces->for_each_interface(
1854 hapd->iface->interfaces, hostapd_owe_iface_iter, hapd);
1855}
1856
1857
1858static int hostapd_owe_iface_iter2(struct hostapd_iface *iface, void *ctx)
1859{
1860 size_t i;
1861
1862 for (i = 0; i < iface->num_bss; i++) {
1863 struct hostapd_data *bss = iface->bss[i];
1864 int res;
1865
1866 if (!bss->conf->owe_transition_ifname[0])
1867 continue;
1868 res = hostapd_owe_trans_get_info(bss);
1869 if (res == 0)
1870 continue;
1871 wpa_printf(MSG_DEBUG,
1872 "OWE: Matching transition mode interface enabled - update beacon data for %s",
1873 bss->conf->iface);
1874 ieee802_11_set_beacon(bss);
1875 }
1876
1877 return 0;
1878}
1879
1880#endif /* CONFIG_OWE */
1881
1882
1883static void hostapd_owe_update_trans(struct hostapd_iface *iface)
1884{
1885#ifdef CONFIG_OWE
1886 /* Check whether the enabled BSS can complete OWE transition mode
1887 * configuration for any pending interface. */
1888 if (!iface->interfaces ||
1889 !iface->interfaces->for_each_interface)
1890 hostapd_owe_iface_iter2(iface, NULL);
1891 else
1892 iface->interfaces->for_each_interface(
1893 iface->interfaces, hostapd_owe_iface_iter2, NULL);
1894#endif /* CONFIG_OWE */
1895}
1896
1897
Roshan Pius3a1667e2018-07-03 15:17:14 -07001898static void hostapd_interface_setup_failure_handler(void *eloop_ctx,
1899 void *timeout_ctx)
1900{
1901 struct hostapd_iface *iface = eloop_ctx;
1902 struct hostapd_data *hapd;
1903
1904 if (iface->num_bss < 1 || !iface->bss || !iface->bss[0])
1905 return;
1906 hapd = iface->bss[0];
1907 if (hapd->setup_complete_cb)
1908 hapd->setup_complete_cb(hapd->setup_complete_cb_ctx);
1909}
1910
1911
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001912static int hostapd_setup_interface_complete_sync(struct hostapd_iface *iface,
1913 int err)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001914{
1915 struct hostapd_data *hapd = iface->bss[0];
1916 size_t j;
1917 u8 *prev_addr;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001918 int delay_apply_cfg = 0;
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001919 int res_dfs_offload = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001920
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001921 if (err)
1922 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001923
1924 wpa_printf(MSG_DEBUG, "Completing interface initialization");
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001925 if (iface->freq) {
Dmitry Shmidt051af732013-10-22 13:52:46 -07001926#ifdef NEED_AP_MLME
1927 int res;
1928#endif /* NEED_AP_MLME */
1929
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001930 wpa_printf(MSG_DEBUG, "Mode: %s Channel: %d "
1931 "Frequency: %d MHz",
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001932 hostapd_hw_mode_txt(iface->conf->hw_mode),
1933 iface->conf->channel, iface->freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001934
Dmitry Shmidt051af732013-10-22 13:52:46 -07001935#ifdef NEED_AP_MLME
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001936 /* Handle DFS only if it is not offloaded to the driver */
1937 if (!(iface->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD)) {
1938 /* Check DFS */
1939 res = hostapd_handle_dfs(iface);
1940 if (res <= 0) {
1941 if (res < 0)
1942 goto fail;
1943 return res;
1944 }
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001945 } else {
1946 /* If DFS is offloaded to the driver */
1947 res_dfs_offload = hostapd_handle_dfs_offload(iface);
1948 if (res_dfs_offload <= 0) {
1949 if (res_dfs_offload < 0)
1950 goto fail;
1951 } else {
1952 wpa_printf(MSG_DEBUG,
1953 "Proceed with AP/channel setup");
1954 /*
1955 * If this is a DFS channel, move to completing
1956 * AP setup.
1957 */
1958 if (res_dfs_offload == 1)
1959 goto dfs_offload;
1960 /* Otherwise fall through. */
1961 }
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001962 }
Dmitry Shmidt051af732013-10-22 13:52:46 -07001963#endif /* NEED_AP_MLME */
1964
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001965#ifdef CONFIG_MESH
1966 if (iface->mconf != NULL) {
1967 wpa_printf(MSG_DEBUG,
1968 "%s: Mesh configuration will be applied while joining the mesh network",
1969 iface->bss[0]->conf->iface);
1970 delay_apply_cfg = 1;
1971 }
1972#endif /* CONFIG_MESH */
1973
1974 if (!delay_apply_cfg &&
1975 hostapd_set_freq(hapd, hapd->iconf->hw_mode, iface->freq,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001976 hapd->iconf->channel,
Hai Shalomc3565922019-10-28 11:58:20 -07001977 hapd->iconf->enable_edmg,
1978 hapd->iconf->edmg_channel,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001979 hapd->iconf->ieee80211n,
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001980 hapd->iconf->ieee80211ac,
Hai Shalom81f62d82019-07-22 12:10:00 -07001981 hapd->iconf->ieee80211ax,
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001982 hapd->iconf->secondary_channel,
Hai Shalom81f62d82019-07-22 12:10:00 -07001983 hostapd_get_oper_chwidth(hapd->iconf),
1984 hostapd_get_oper_centr_freq_seg0_idx(
1985 hapd->iconf),
1986 hostapd_get_oper_centr_freq_seg1_idx(
1987 hapd->iconf))) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001988 wpa_printf(MSG_ERROR, "Could not set channel for "
1989 "kernel driver");
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001990 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001991 }
1992 }
1993
1994 if (iface->current_mode) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001995 if (hostapd_prepare_rates(iface, iface->current_mode)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001996 wpa_printf(MSG_ERROR, "Failed to prepare rates "
1997 "table.");
1998 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
1999 HOSTAPD_LEVEL_WARNING,
2000 "Failed to prepare rates table.");
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002001 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002002 }
2003 }
2004
Hai Shalom021b0b52019-04-10 11:17:58 -07002005 if (hapd->iconf->rts_threshold >= -1 &&
2006 hostapd_set_rts(hapd, hapd->iconf->rts_threshold) &&
2007 hapd->iconf->rts_threshold >= -1) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002008 wpa_printf(MSG_ERROR, "Could not set RTS threshold for "
2009 "kernel driver");
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002010 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002011 }
2012
Hai Shalom021b0b52019-04-10 11:17:58 -07002013 if (hapd->iconf->fragm_threshold >= -1 &&
2014 hostapd_set_frag(hapd, hapd->iconf->fragm_threshold) &&
2015 hapd->iconf->fragm_threshold != -1) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002016 wpa_printf(MSG_ERROR, "Could not set fragmentation threshold "
2017 "for kernel driver");
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002018 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002019 }
2020
2021 prev_addr = hapd->own_addr;
2022
2023 for (j = 0; j < iface->num_bss; j++) {
2024 hapd = iface->bss[j];
2025 if (j)
2026 os_memcpy(hapd->own_addr, prev_addr, ETH_ALEN);
Dmitry Shmidt71757432014-06-02 13:50:35 -07002027 if (hostapd_setup_bss(hapd, j == 0)) {
Hai Shalom021b0b52019-04-10 11:17:58 -07002028 for (;;) {
Dmitry Shmidt71757432014-06-02 13:50:35 -07002029 hapd = iface->bss[j];
2030 hostapd_bss_deinit_no_free(hapd);
2031 hostapd_free_hapd_data(hapd);
Hai Shalom021b0b52019-04-10 11:17:58 -07002032 if (j == 0)
2033 break;
2034 j--;
2035 }
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002036 goto fail;
Dmitry Shmidt71757432014-06-02 13:50:35 -07002037 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002038 if (is_zero_ether_addr(hapd->conf->bssid))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002039 prev_addr = hapd->own_addr;
2040 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002041 hapd = iface->bss[0];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002042
2043 hostapd_tx_queue_params(iface);
2044
2045 ap_list_init(iface);
2046
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07002047 hostapd_set_acl(hapd);
2048
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002049 if (hostapd_driver_commit(hapd) < 0) {
2050 wpa_printf(MSG_ERROR, "%s: Failed to commit driver "
2051 "configuration", __func__);
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002052 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002053 }
2054
Jouni Malinen87fd2792011-05-16 18:35:42 +03002055 /*
2056 * WPS UPnP module can be initialized only when the "upnp_iface" is up.
2057 * If "interface" and "upnp_iface" are the same (e.g., non-bridge
2058 * mode), the interface is up only after driver_commit, so initialize
2059 * WPS after driver_commit.
2060 */
2061 for (j = 0; j < iface->num_bss; j++) {
2062 if (hostapd_init_wps_complete(iface->bss[j]))
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002063 goto fail;
Jouni Malinen87fd2792011-05-16 18:35:42 +03002064 }
2065
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08002066 if ((iface->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
2067 !res_dfs_offload) {
2068 /*
2069 * If freq is DFS, and DFS is offloaded to the driver, then wait
2070 * for CAC to complete.
2071 */
2072 wpa_printf(MSG_DEBUG, "%s: Wait for CAC to complete", __func__);
2073 return res_dfs_offload;
2074 }
2075
2076#ifdef NEED_AP_MLME
2077dfs_offload:
2078#endif /* NEED_AP_MLME */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002079
2080#ifdef CONFIG_FST
2081 if (hapd->iconf->fst_cfg.group_id[0]) {
2082 struct fst_wpa_obj iface_obj;
2083
2084 fst_hostapd_fill_iface_obj(hapd, &iface_obj);
2085 iface->fst = fst_attach(hapd->conf->iface, hapd->own_addr,
2086 &iface_obj, &hapd->iconf->fst_cfg);
2087 if (!iface->fst) {
2088 wpa_printf(MSG_ERROR, "Could not attach to FST %s",
2089 hapd->iconf->fst_cfg.group_id);
2090 goto fail;
2091 }
2092 }
2093#endif /* CONFIG_FST */
2094
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002095 hostapd_set_state(iface, HAPD_IFACE_ENABLED);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002096 hostapd_owe_update_trans(iface);
Hai Shalom81f62d82019-07-22 12:10:00 -07002097 airtime_policy_update_init(iface);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002098 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_ENABLED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002099 if (hapd->setup_complete_cb)
2100 hapd->setup_complete_cb(hapd->setup_complete_cb_ctx);
2101
2102 wpa_printf(MSG_DEBUG, "%s: Setup of interface done.",
2103 iface->bss[0]->conf->iface);
Dmitry Shmidtb96dad42013-11-05 10:07:29 -08002104 if (iface->interfaces && iface->interfaces->terminate_on_error > 0)
2105 iface->interfaces->terminate_on_error--;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002106
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002107 for (j = 0; j < iface->num_bss; j++)
Hai Shalom74f70d42019-02-11 14:42:39 -08002108 hostapd_neighbor_set_own_report(iface->bss[j]);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002109
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002110 return 0;
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002111
2112fail:
2113 wpa_printf(MSG_ERROR, "Interface initialization failed");
2114 hostapd_set_state(iface, HAPD_IFACE_DISABLED);
2115 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002116#ifdef CONFIG_FST
2117 if (iface->fst) {
2118 fst_detach(iface->fst);
2119 iface->fst = NULL;
2120 }
2121#endif /* CONFIG_FST */
Roshan Pius3a1667e2018-07-03 15:17:14 -07002122
2123 if (iface->interfaces && iface->interfaces->terminate_on_error) {
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002124 eloop_terminate();
Roshan Pius3a1667e2018-07-03 15:17:14 -07002125 } else if (hapd->setup_complete_cb) {
2126 /*
2127 * Calling hapd->setup_complete_cb directly may cause iface
2128 * deinitialization which may be accessed later by the caller.
2129 */
2130 eloop_register_timeout(0, 0,
2131 hostapd_interface_setup_failure_handler,
2132 iface, NULL);
2133 }
2134
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002135 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002136}
2137
2138
2139/**
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002140 * hostapd_setup_interface_complete - Complete interface setup
2141 *
2142 * This function is called when previous steps in the interface setup has been
2143 * completed. This can also start operations, e.g., DFS, that will require
2144 * additional processing before interface is ready to be enabled. Such
2145 * operations will call this function from eloop callbacks when finished.
2146 */
2147int hostapd_setup_interface_complete(struct hostapd_iface *iface, int err)
2148{
2149 struct hapd_interfaces *interfaces = iface->interfaces;
2150 struct hostapd_data *hapd = iface->bss[0];
2151 unsigned int i;
2152 int not_ready_in_sync_ifaces = 0;
2153
2154 if (!iface->need_to_start_in_sync)
2155 return hostapd_setup_interface_complete_sync(iface, err);
2156
2157 if (err) {
2158 wpa_printf(MSG_ERROR, "Interface initialization failed");
2159 hostapd_set_state(iface, HAPD_IFACE_DISABLED);
2160 iface->need_to_start_in_sync = 0;
2161 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
2162 if (interfaces && interfaces->terminate_on_error)
2163 eloop_terminate();
2164 return -1;
2165 }
2166
2167 if (iface->ready_to_start_in_sync) {
2168 /* Already in ready and waiting. should never happpen */
2169 return 0;
2170 }
2171
2172 for (i = 0; i < interfaces->count; i++) {
2173 if (interfaces->iface[i]->need_to_start_in_sync &&
2174 !interfaces->iface[i]->ready_to_start_in_sync)
2175 not_ready_in_sync_ifaces++;
2176 }
2177
2178 /*
2179 * Check if this is the last interface, if yes then start all the other
2180 * waiting interfaces. If not, add this interface to the waiting list.
2181 */
2182 if (not_ready_in_sync_ifaces > 1 && iface->state == HAPD_IFACE_DFS) {
2183 /*
2184 * If this interface went through CAC, do not synchronize, just
2185 * start immediately.
2186 */
2187 iface->need_to_start_in_sync = 0;
2188 wpa_printf(MSG_INFO,
2189 "%s: Finished CAC - bypass sync and start interface",
2190 iface->bss[0]->conf->iface);
2191 return hostapd_setup_interface_complete_sync(iface, err);
2192 }
2193
2194 if (not_ready_in_sync_ifaces > 1) {
2195 /* need to wait as there are other interfaces still coming up */
2196 iface->ready_to_start_in_sync = 1;
2197 wpa_printf(MSG_INFO,
2198 "%s: Interface waiting to sync with other interfaces",
2199 iface->bss[0]->conf->iface);
2200 return 0;
2201 }
2202
2203 wpa_printf(MSG_INFO,
2204 "%s: Last interface to sync - starting all interfaces",
2205 iface->bss[0]->conf->iface);
2206 iface->need_to_start_in_sync = 0;
2207 hostapd_setup_interface_complete_sync(iface, err);
2208 for (i = 0; i < interfaces->count; i++) {
2209 if (interfaces->iface[i]->need_to_start_in_sync &&
2210 interfaces->iface[i]->ready_to_start_in_sync) {
2211 hostapd_setup_interface_complete_sync(
2212 interfaces->iface[i], 0);
2213 /* Only once the interfaces are sync started */
2214 interfaces->iface[i]->need_to_start_in_sync = 0;
2215 }
2216 }
2217
2218 return 0;
2219}
2220
2221
2222/**
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002223 * hostapd_setup_interface - Setup of an interface
2224 * @iface: Pointer to interface data.
2225 * Returns: 0 on success, -1 on failure
2226 *
2227 * Initializes the driver interface, validates the configuration,
2228 * and sets driver parameters based on the configuration.
2229 * Flushes old stations, sets the channel, encryption,
2230 * beacons, and WDS links based on the configuration.
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002231 *
2232 * If interface setup requires more time, e.g., to perform HT co-ex scans, ACS,
2233 * or DFS operations, this function returns 0 before such operations have been
2234 * completed. The pending operations are registered into eloop and will be
2235 * completed from eloop callbacks. Those callbacks end up calling
2236 * hostapd_setup_interface_complete() once setup has been completed.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002237 */
2238int hostapd_setup_interface(struct hostapd_iface *iface)
2239{
2240 int ret;
2241
2242 ret = setup_interface(iface);
2243 if (ret) {
2244 wpa_printf(MSG_ERROR, "%s: Unable to setup interface.",
2245 iface->bss[0]->conf->iface);
2246 return -1;
2247 }
2248
2249 return 0;
2250}
2251
2252
2253/**
2254 * hostapd_alloc_bss_data - Allocate and initialize per-BSS data
2255 * @hapd_iface: Pointer to interface data
2256 * @conf: Pointer to per-interface configuration
2257 * @bss: Pointer to per-BSS configuration for this BSS
2258 * Returns: Pointer to allocated BSS data
2259 *
2260 * This function is used to allocate per-BSS data structure. This data will be
2261 * freed after hostapd_cleanup() is called for it during interface
2262 * deinitialization.
2263 */
2264struct hostapd_data *
2265hostapd_alloc_bss_data(struct hostapd_iface *hapd_iface,
2266 struct hostapd_config *conf,
2267 struct hostapd_bss_config *bss)
2268{
2269 struct hostapd_data *hapd;
2270
2271 hapd = os_zalloc(sizeof(*hapd));
2272 if (hapd == NULL)
2273 return NULL;
2274
2275 hapd->new_assoc_sta_cb = hostapd_new_assoc_sta;
2276 hapd->iconf = conf;
2277 hapd->conf = bss;
2278 hapd->iface = hapd_iface;
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08002279 if (conf)
2280 hapd->driver = conf->driver;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002281 hapd->ctrl_sock = -1;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08002282 dl_list_init(&hapd->ctrl_dst);
Dmitry Shmidt7d175302016-09-06 13:11:34 -07002283 dl_list_init(&hapd->nr_db);
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08002284 hapd->dhcp_sock = -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002285#ifdef CONFIG_IEEE80211R_AP
2286 dl_list_init(&hapd->l2_queue);
2287 dl_list_init(&hapd->l2_oui_queue);
2288#endif /* CONFIG_IEEE80211R_AP */
Hai Shalom021b0b52019-04-10 11:17:58 -07002289#ifdef CONFIG_SAE
2290 dl_list_init(&hapd->sae_commit_queue);
2291#endif /* CONFIG_SAE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002292
2293 return hapd;
2294}
2295
2296
Dmitry Shmidt54605472013-11-08 11:10:19 -08002297static void hostapd_bss_deinit(struct hostapd_data *hapd)
2298{
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07002299 if (!hapd)
2300 return;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002301 wpa_printf(MSG_DEBUG, "%s: deinit bss %s", __func__,
Hai Shalom021b0b52019-04-10 11:17:58 -07002302 hapd->conf ? hapd->conf->iface : "N/A");
Dmitry Shmidt71757432014-06-02 13:50:35 -07002303 hostapd_bss_deinit_no_free(hapd);
Dmitry Shmidtf73259c2015-03-17 11:00:54 -07002304 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
Hai Shalomc3565922019-10-28 11:58:20 -07002305#ifdef CONFIG_SQLITE
2306 if (hapd->rad_attr_db) {
2307 sqlite3_close(hapd->rad_attr_db);
2308 hapd->rad_attr_db = NULL;
2309 }
2310#endif /* CONFIG_SQLITE */
Dmitry Shmidt54605472013-11-08 11:10:19 -08002311 hostapd_cleanup(hapd);
2312}
2313
2314
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002315void hostapd_interface_deinit(struct hostapd_iface *iface)
2316{
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002317 int j;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002318
Dmitry Shmidt54605472013-11-08 11:10:19 -08002319 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002320 if (iface == NULL)
2321 return;
2322
Dmitry Shmidtf73259c2015-03-17 11:00:54 -07002323 hostapd_set_state(iface, HAPD_IFACE_DISABLED);
2324
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002325 eloop_cancel_timeout(channel_list_update_timeout, iface, NULL);
2326 iface->wait_channel_update = 0;
2327
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002328#ifdef CONFIG_FST
2329 if (iface->fst) {
2330 fst_detach(iface->fst);
2331 iface->fst = NULL;
2332 }
2333#endif /* CONFIG_FST */
2334
Hai Shalom021b0b52019-04-10 11:17:58 -07002335 for (j = (int) iface->num_bss - 1; j >= 0; j--) {
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07002336 if (!iface->bss)
2337 break;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002338 hostapd_bss_deinit(iface->bss[j]);
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07002339 }
Roshan Pius3a1667e2018-07-03 15:17:14 -07002340
2341#ifdef CONFIG_IEEE80211N
2342#ifdef NEED_AP_MLME
2343 hostapd_stop_setup_timers(iface);
2344 eloop_cancel_timeout(ap_ht2040_timeout, iface, NULL);
2345#endif /* NEED_AP_MLME */
2346#endif /* CONFIG_IEEE80211N */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002347}
2348
2349
2350void hostapd_interface_free(struct hostapd_iface *iface)
2351{
2352 size_t j;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002353 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
2354 for (j = 0; j < iface->num_bss; j++) {
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07002355 if (!iface->bss)
2356 break;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002357 wpa_printf(MSG_DEBUG, "%s: free hapd %p",
2358 __func__, iface->bss[j]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002359 os_free(iface->bss[j]);
Dmitry Shmidt54605472013-11-08 11:10:19 -08002360 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002361 hostapd_cleanup_iface(iface);
2362}
2363
2364
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07002365struct hostapd_iface * hostapd_alloc_iface(void)
2366{
2367 struct hostapd_iface *hapd_iface;
2368
2369 hapd_iface = os_zalloc(sizeof(*hapd_iface));
2370 if (!hapd_iface)
2371 return NULL;
2372
2373 dl_list_init(&hapd_iface->sta_seen);
2374
2375 return hapd_iface;
2376}
2377
2378
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002379/**
2380 * hostapd_init - Allocate and initialize per-interface data
2381 * @config_file: Path to the configuration file
2382 * Returns: Pointer to the allocated interface data or %NULL on failure
2383 *
2384 * This function is used to allocate main data structures for per-interface
2385 * data. The allocated data buffer will be freed by calling
2386 * hostapd_cleanup_iface().
2387 */
2388struct hostapd_iface * hostapd_init(struct hapd_interfaces *interfaces,
2389 const char *config_file)
2390{
2391 struct hostapd_iface *hapd_iface = NULL;
2392 struct hostapd_config *conf = NULL;
2393 struct hostapd_data *hapd;
2394 size_t i;
2395
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07002396 hapd_iface = hostapd_alloc_iface();
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002397 if (hapd_iface == NULL)
2398 goto fail;
2399
2400 hapd_iface->config_fname = os_strdup(config_file);
2401 if (hapd_iface->config_fname == NULL)
2402 goto fail;
2403
2404 conf = interfaces->config_read_cb(hapd_iface->config_fname);
2405 if (conf == NULL)
2406 goto fail;
2407 hapd_iface->conf = conf;
2408
2409 hapd_iface->num_bss = conf->num_bss;
2410 hapd_iface->bss = os_calloc(conf->num_bss,
2411 sizeof(struct hostapd_data *));
2412 if (hapd_iface->bss == NULL)
2413 goto fail;
2414
2415 for (i = 0; i < conf->num_bss; i++) {
2416 hapd = hapd_iface->bss[i] =
2417 hostapd_alloc_bss_data(hapd_iface, conf,
2418 conf->bss[i]);
2419 if (hapd == NULL)
2420 goto fail;
2421 hapd->msg_ctx = hapd;
2422 }
2423
2424 return hapd_iface;
2425
2426fail:
2427 wpa_printf(MSG_ERROR, "Failed to set up interface with %s",
2428 config_file);
2429 if (conf)
2430 hostapd_config_free(conf);
2431 if (hapd_iface) {
2432 os_free(hapd_iface->config_fname);
2433 os_free(hapd_iface->bss);
Dmitry Shmidt54605472013-11-08 11:10:19 -08002434 wpa_printf(MSG_DEBUG, "%s: free iface %p",
2435 __func__, hapd_iface);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002436 os_free(hapd_iface);
2437 }
2438 return NULL;
2439}
2440
2441
2442static int ifname_in_use(struct hapd_interfaces *interfaces, const char *ifname)
2443{
2444 size_t i, j;
2445
2446 for (i = 0; i < interfaces->count; i++) {
2447 struct hostapd_iface *iface = interfaces->iface[i];
2448 for (j = 0; j < iface->num_bss; j++) {
2449 struct hostapd_data *hapd = iface->bss[j];
2450 if (os_strcmp(ifname, hapd->conf->iface) == 0)
2451 return 1;
2452 }
2453 }
2454
2455 return 0;
2456}
2457
2458
2459/**
2460 * hostapd_interface_init_bss - Read configuration file and init BSS data
2461 *
2462 * This function is used to parse configuration file for a BSS. This BSS is
2463 * added to an existing interface sharing the same radio (if any) or a new
2464 * interface is created if this is the first interface on a radio. This
2465 * allocate memory for the BSS. No actual driver operations are started.
2466 *
2467 * This is similar to hostapd_interface_init(), but for a case where the
2468 * configuration is used to add a single BSS instead of all BSSes for a radio.
2469 */
2470struct hostapd_iface *
2471hostapd_interface_init_bss(struct hapd_interfaces *interfaces, const char *phy,
2472 const char *config_fname, int debug)
2473{
2474 struct hostapd_iface *new_iface = NULL, *iface = NULL;
2475 struct hostapd_data *hapd;
2476 int k;
2477 size_t i, bss_idx;
2478
2479 if (!phy || !*phy)
2480 return NULL;
2481
2482 for (i = 0; i < interfaces->count; i++) {
2483 if (os_strcmp(interfaces->iface[i]->phy, phy) == 0) {
2484 iface = interfaces->iface[i];
2485 break;
2486 }
2487 }
2488
2489 wpa_printf(MSG_INFO, "Configuration file: %s (phy %s)%s",
2490 config_fname, phy, iface ? "" : " --> new PHY");
2491 if (iface) {
2492 struct hostapd_config *conf;
2493 struct hostapd_bss_config **tmp_conf;
2494 struct hostapd_data **tmp_bss;
2495 struct hostapd_bss_config *bss;
2496 const char *ifname;
2497
2498 /* Add new BSS to existing iface */
2499 conf = interfaces->config_read_cb(config_fname);
2500 if (conf == NULL)
2501 return NULL;
2502 if (conf->num_bss > 1) {
2503 wpa_printf(MSG_ERROR, "Multiple BSSes specified in BSS-config");
2504 hostapd_config_free(conf);
2505 return NULL;
2506 }
2507
2508 ifname = conf->bss[0]->iface;
2509 if (ifname[0] != '\0' && ifname_in_use(interfaces, ifname)) {
2510 wpa_printf(MSG_ERROR,
2511 "Interface name %s already in use", ifname);
2512 hostapd_config_free(conf);
2513 return NULL;
2514 }
2515
2516 tmp_conf = os_realloc_array(
2517 iface->conf->bss, iface->conf->num_bss + 1,
2518 sizeof(struct hostapd_bss_config *));
2519 tmp_bss = os_realloc_array(iface->bss, iface->num_bss + 1,
2520 sizeof(struct hostapd_data *));
2521 if (tmp_bss)
2522 iface->bss = tmp_bss;
2523 if (tmp_conf) {
2524 iface->conf->bss = tmp_conf;
2525 iface->conf->last_bss = tmp_conf[0];
2526 }
2527 if (tmp_bss == NULL || tmp_conf == NULL) {
2528 hostapd_config_free(conf);
2529 return NULL;
2530 }
2531 bss = iface->conf->bss[iface->conf->num_bss] = conf->bss[0];
2532 iface->conf->num_bss++;
2533
2534 hapd = hostapd_alloc_bss_data(iface, iface->conf, bss);
2535 if (hapd == NULL) {
2536 iface->conf->num_bss--;
2537 hostapd_config_free(conf);
2538 return NULL;
2539 }
2540 iface->conf->last_bss = bss;
2541 iface->bss[iface->num_bss] = hapd;
2542 hapd->msg_ctx = hapd;
2543
2544 bss_idx = iface->num_bss++;
2545 conf->num_bss--;
2546 conf->bss[0] = NULL;
2547 hostapd_config_free(conf);
2548 } else {
2549 /* Add a new iface with the first BSS */
2550 new_iface = iface = hostapd_init(interfaces, config_fname);
2551 if (!iface)
2552 return NULL;
2553 os_strlcpy(iface->phy, phy, sizeof(iface->phy));
2554 iface->interfaces = interfaces;
2555 bss_idx = 0;
2556 }
2557
2558 for (k = 0; k < debug; k++) {
2559 if (iface->bss[bss_idx]->conf->logger_stdout_level > 0)
2560 iface->bss[bss_idx]->conf->logger_stdout_level--;
2561 }
2562
2563 if (iface->conf->bss[bss_idx]->iface[0] == '\0' &&
2564 !hostapd_drv_none(iface->bss[bss_idx])) {
2565 wpa_printf(MSG_ERROR, "Interface name not specified in %s",
2566 config_fname);
2567 if (new_iface)
2568 hostapd_interface_deinit_free(new_iface);
2569 return NULL;
2570 }
2571
2572 return iface;
2573}
2574
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002575
2576void hostapd_interface_deinit_free(struct hostapd_iface *iface)
2577{
2578 const struct wpa_driver_ops *driver;
2579 void *drv_priv;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002580
2581 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002582 if (iface == NULL)
2583 return;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002584 wpa_printf(MSG_DEBUG, "%s: num_bss=%u conf->num_bss=%u",
2585 __func__, (unsigned int) iface->num_bss,
2586 (unsigned int) iface->conf->num_bss);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002587 driver = iface->bss[0]->driver;
2588 drv_priv = iface->bss[0]->drv_priv;
2589 hostapd_interface_deinit(iface);
Dmitry Shmidt54605472013-11-08 11:10:19 -08002590 wpa_printf(MSG_DEBUG, "%s: driver=%p drv_priv=%p -> hapd_deinit",
2591 __func__, driver, drv_priv);
Dmitry Shmidt71757432014-06-02 13:50:35 -07002592 if (driver && driver->hapd_deinit && drv_priv) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002593 driver->hapd_deinit(drv_priv);
Dmitry Shmidt71757432014-06-02 13:50:35 -07002594 iface->bss[0]->drv_priv = NULL;
2595 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002596 hostapd_interface_free(iface);
2597}
2598
2599
Dmitry Shmidt15907092014-03-25 10:42:57 -07002600static void hostapd_deinit_driver(const struct wpa_driver_ops *driver,
2601 void *drv_priv,
2602 struct hostapd_iface *hapd_iface)
2603{
2604 size_t j;
2605
2606 wpa_printf(MSG_DEBUG, "%s: driver=%p drv_priv=%p -> hapd_deinit",
2607 __func__, driver, drv_priv);
2608 if (driver && driver->hapd_deinit && drv_priv) {
2609 driver->hapd_deinit(drv_priv);
2610 for (j = 0; j < hapd_iface->num_bss; j++) {
2611 wpa_printf(MSG_DEBUG, "%s:bss[%d]->drv_priv=%p",
2612 __func__, (int) j,
2613 hapd_iface->bss[j]->drv_priv);
Hai Shalom1dc4d202019-04-29 16:22:27 -07002614 if (hapd_iface->bss[j]->drv_priv == drv_priv) {
Dmitry Shmidt15907092014-03-25 10:42:57 -07002615 hapd_iface->bss[j]->drv_priv = NULL;
Hai Shalom1dc4d202019-04-29 16:22:27 -07002616 hapd_iface->extended_capa = NULL;
2617 hapd_iface->extended_capa_mask = NULL;
2618 hapd_iface->extended_capa_len = 0;
2619 }
Dmitry Shmidt15907092014-03-25 10:42:57 -07002620 }
2621 }
2622}
2623
2624
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002625int hostapd_enable_iface(struct hostapd_iface *hapd_iface)
2626{
Dmitry Shmidt71757432014-06-02 13:50:35 -07002627 size_t j;
2628
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002629 if (hapd_iface->bss[0]->drv_priv != NULL) {
2630 wpa_printf(MSG_ERROR, "Interface %s already enabled",
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002631 hapd_iface->conf->bss[0]->iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002632 return -1;
2633 }
2634
2635 wpa_printf(MSG_DEBUG, "Enable interface %s",
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002636 hapd_iface->conf->bss[0]->iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002637
Dmitry Shmidt71757432014-06-02 13:50:35 -07002638 for (j = 0; j < hapd_iface->num_bss; j++)
2639 hostapd_set_security_params(hapd_iface->conf->bss[j], 1);
Dmitry Shmidt344abd32014-01-14 13:17:00 -08002640 if (hostapd_config_check(hapd_iface->conf, 1) < 0) {
2641 wpa_printf(MSG_INFO, "Invalid configuration - cannot enable");
2642 return -1;
2643 }
2644
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002645 if (hapd_iface->interfaces == NULL ||
2646 hapd_iface->interfaces->driver_init == NULL ||
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002647 hapd_iface->interfaces->driver_init(hapd_iface))
2648 return -1;
2649
2650 if (hostapd_setup_interface(hapd_iface)) {
Dmitry Shmidt15907092014-03-25 10:42:57 -07002651 hostapd_deinit_driver(hapd_iface->bss[0]->driver,
2652 hapd_iface->bss[0]->drv_priv,
2653 hapd_iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002654 return -1;
2655 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002656
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002657 return 0;
2658}
2659
2660
2661int hostapd_reload_iface(struct hostapd_iface *hapd_iface)
2662{
2663 size_t j;
2664
2665 wpa_printf(MSG_DEBUG, "Reload interface %s",
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002666 hapd_iface->conf->bss[0]->iface);
2667 for (j = 0; j < hapd_iface->num_bss; j++)
Dmitry Shmidt71757432014-06-02 13:50:35 -07002668 hostapd_set_security_params(hapd_iface->conf->bss[j], 1);
Dmitry Shmidt344abd32014-01-14 13:17:00 -08002669 if (hostapd_config_check(hapd_iface->conf, 1) < 0) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002670 wpa_printf(MSG_ERROR, "Updated configuration is invalid");
2671 return -1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002672 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002673 hostapd_clear_old(hapd_iface);
2674 for (j = 0; j < hapd_iface->num_bss; j++)
2675 hostapd_reload_bss(hapd_iface->bss[j]);
2676
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002677 return 0;
2678}
2679
2680
2681int hostapd_disable_iface(struct hostapd_iface *hapd_iface)
2682{
2683 size_t j;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002684 const struct wpa_driver_ops *driver;
2685 void *drv_priv;
2686
2687 if (hapd_iface == NULL)
2688 return -1;
Dmitry Shmidt71757432014-06-02 13:50:35 -07002689
2690 if (hapd_iface->bss[0]->drv_priv == NULL) {
2691 wpa_printf(MSG_INFO, "Interface %s already disabled",
2692 hapd_iface->conf->bss[0]->iface);
2693 return -1;
2694 }
2695
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002696 wpa_msg(hapd_iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002697 driver = hapd_iface->bss[0]->driver;
2698 drv_priv = hapd_iface->bss[0]->drv_priv;
2699
Dmitry Shmidta38abf92014-03-06 13:38:44 -08002700 hapd_iface->driver_ap_teardown =
2701 !!(hapd_iface->drv_flags &
2702 WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
2703
Hai Shalom39ba6fc2019-01-22 12:40:38 -08002704#ifdef NEED_AP_MLME
2705 for (j = 0; j < hapd_iface->num_bss; j++)
2706 hostapd_cleanup_cs_params(hapd_iface->bss[j]);
2707#endif /* NEED_AP_MLME */
2708
Dmitry Shmidta38abf92014-03-06 13:38:44 -08002709 /* same as hostapd_interface_deinit without deinitializing ctrl-iface */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002710 for (j = 0; j < hapd_iface->num_bss; j++) {
2711 struct hostapd_data *hapd = hapd_iface->bss[j];
Dmitry Shmidt71757432014-06-02 13:50:35 -07002712 hostapd_bss_deinit_no_free(hapd);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002713 hostapd_free_hapd_data(hapd);
2714 }
2715
Dmitry Shmidt15907092014-03-25 10:42:57 -07002716 hostapd_deinit_driver(driver, drv_priv, hapd_iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002717
2718 /* From hostapd_cleanup_iface: These were initialized in
2719 * hostapd_setup_interface and hostapd_setup_interface_complete
2720 */
2721 hostapd_cleanup_iface_partial(hapd_iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002722
Dmitry Shmidt56052862013-10-04 10:23:25 -07002723 wpa_printf(MSG_DEBUG, "Interface %s disabled",
2724 hapd_iface->bss[0]->conf->iface);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002725 hostapd_set_state(hapd_iface, HAPD_IFACE_DISABLED);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002726 return 0;
2727}
2728
2729
2730static struct hostapd_iface *
2731hostapd_iface_alloc(struct hapd_interfaces *interfaces)
2732{
2733 struct hostapd_iface **iface, *hapd_iface;
2734
2735 iface = os_realloc_array(interfaces->iface, interfaces->count + 1,
2736 sizeof(struct hostapd_iface *));
2737 if (iface == NULL)
2738 return NULL;
2739 interfaces->iface = iface;
2740 hapd_iface = interfaces->iface[interfaces->count] =
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07002741 hostapd_alloc_iface();
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002742 if (hapd_iface == NULL) {
2743 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory for "
2744 "the interface", __func__);
2745 return NULL;
2746 }
2747 interfaces->count++;
2748 hapd_iface->interfaces = interfaces;
2749
2750 return hapd_iface;
2751}
2752
2753
2754static struct hostapd_config *
2755hostapd_config_alloc(struct hapd_interfaces *interfaces, const char *ifname,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002756 const char *ctrl_iface, const char *driver)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002757{
2758 struct hostapd_bss_config *bss;
2759 struct hostapd_config *conf;
2760
2761 /* Allocates memory for bss and conf */
2762 conf = hostapd_config_defaults();
2763 if (conf == NULL) {
2764 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory for "
2765 "configuration", __func__);
Hai Shalom74f70d42019-02-11 14:42:39 -08002766 return NULL;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002767 }
2768
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002769 if (driver) {
2770 int j;
2771
2772 for (j = 0; wpa_drivers[j]; j++) {
2773 if (os_strcmp(driver, wpa_drivers[j]->name) == 0) {
2774 conf->driver = wpa_drivers[j];
2775 goto skip;
2776 }
2777 }
2778
2779 wpa_printf(MSG_ERROR,
2780 "Invalid/unknown driver '%s' - registering the default driver",
2781 driver);
2782 }
2783
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002784 conf->driver = wpa_drivers[0];
2785 if (conf->driver == NULL) {
2786 wpa_printf(MSG_ERROR, "No driver wrappers registered!");
2787 hostapd_config_free(conf);
2788 return NULL;
2789 }
2790
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002791skip:
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002792 bss = conf->last_bss = conf->bss[0];
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002793
2794 os_strlcpy(bss->iface, ifname, sizeof(bss->iface));
2795 bss->ctrl_interface = os_strdup(ctrl_iface);
2796 if (bss->ctrl_interface == NULL) {
2797 hostapd_config_free(conf);
2798 return NULL;
2799 }
2800
2801 /* Reading configuration file skipped, will be done in SET!
2802 * From reading the configuration till the end has to be done in
2803 * SET
2804 */
2805 return conf;
2806}
2807
2808
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002809static int hostapd_data_alloc(struct hostapd_iface *hapd_iface,
2810 struct hostapd_config *conf)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002811{
2812 size_t i;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002813 struct hostapd_data *hapd;
2814
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002815 hapd_iface->bss = os_calloc(conf->num_bss,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002816 sizeof(struct hostapd_data *));
2817 if (hapd_iface->bss == NULL)
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002818 return -1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002819
2820 for (i = 0; i < conf->num_bss; i++) {
2821 hapd = hapd_iface->bss[i] =
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002822 hostapd_alloc_bss_data(hapd_iface, conf, conf->bss[i]);
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002823 if (hapd == NULL) {
2824 while (i > 0) {
2825 i--;
2826 os_free(hapd_iface->bss[i]);
2827 hapd_iface->bss[i] = NULL;
2828 }
2829 os_free(hapd_iface->bss);
2830 hapd_iface->bss = NULL;
2831 return -1;
2832 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002833 hapd->msg_ctx = hapd;
2834 }
2835
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002836 hapd_iface->conf = conf;
2837 hapd_iface->num_bss = conf->num_bss;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002838
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002839 return 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002840}
2841
2842
2843int hostapd_add_iface(struct hapd_interfaces *interfaces, char *buf)
2844{
2845 struct hostapd_config *conf = NULL;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002846 struct hostapd_iface *hapd_iface = NULL, *new_iface = NULL;
2847 struct hostapd_data *hapd;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002848 char *ptr;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002849 size_t i, j;
2850 const char *conf_file = NULL, *phy_name = NULL;
2851
2852 if (os_strncmp(buf, "bss_config=", 11) == 0) {
2853 char *pos;
2854 phy_name = buf + 11;
2855 pos = os_strchr(phy_name, ':');
2856 if (!pos)
2857 return -1;
2858 *pos++ = '\0';
2859 conf_file = pos;
2860 if (!os_strlen(conf_file))
2861 return -1;
2862
2863 hapd_iface = hostapd_interface_init_bss(interfaces, phy_name,
2864 conf_file, 0);
2865 if (!hapd_iface)
2866 return -1;
2867 for (j = 0; j < interfaces->count; j++) {
2868 if (interfaces->iface[j] == hapd_iface)
2869 break;
2870 }
2871 if (j == interfaces->count) {
2872 struct hostapd_iface **tmp;
2873 tmp = os_realloc_array(interfaces->iface,
2874 interfaces->count + 1,
2875 sizeof(struct hostapd_iface *));
2876 if (!tmp) {
2877 hostapd_interface_deinit_free(hapd_iface);
2878 return -1;
2879 }
2880 interfaces->iface = tmp;
2881 interfaces->iface[interfaces->count++] = hapd_iface;
2882 new_iface = hapd_iface;
2883 }
2884
2885 if (new_iface) {
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002886 if (interfaces->driver_init(hapd_iface))
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002887 goto fail;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002888
2889 if (hostapd_setup_interface(hapd_iface)) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002890 hostapd_deinit_driver(
2891 hapd_iface->bss[0]->driver,
2892 hapd_iface->bss[0]->drv_priv,
2893 hapd_iface);
2894 goto fail;
2895 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002896 } else {
2897 /* Assign new BSS with bss[0]'s driver info */
2898 hapd = hapd_iface->bss[hapd_iface->num_bss - 1];
2899 hapd->driver = hapd_iface->bss[0]->driver;
2900 hapd->drv_priv = hapd_iface->bss[0]->drv_priv;
2901 os_memcpy(hapd->own_addr, hapd_iface->bss[0]->own_addr,
2902 ETH_ALEN);
2903
2904 if (start_ctrl_iface_bss(hapd) < 0 ||
Dmitry Shmidt54605472013-11-08 11:10:19 -08002905 (hapd_iface->state == HAPD_IFACE_ENABLED &&
2906 hostapd_setup_bss(hapd, -1))) {
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002907 hostapd_cleanup(hapd);
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08002908 hapd_iface->bss[hapd_iface->num_bss - 1] = NULL;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002909 hapd_iface->conf->num_bss--;
2910 hapd_iface->num_bss--;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002911 wpa_printf(MSG_DEBUG, "%s: free hapd %p %s",
2912 __func__, hapd, hapd->conf->iface);
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002913 hostapd_config_free_bss(hapd->conf);
2914 hapd->conf = NULL;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002915 os_free(hapd);
2916 return -1;
2917 }
2918 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002919 hostapd_owe_update_trans(hapd_iface);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002920 return 0;
2921 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002922
2923 ptr = os_strchr(buf, ' ');
2924 if (ptr == NULL)
2925 return -1;
2926 *ptr++ = '\0';
2927
Dmitry Shmidt56052862013-10-04 10:23:25 -07002928 if (os_strncmp(ptr, "config=", 7) == 0)
2929 conf_file = ptr + 7;
2930
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002931 for (i = 0; i < interfaces->count; i++) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002932 if (!os_strcmp(interfaces->iface[i]->conf->bss[0]->iface,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002933 buf)) {
2934 wpa_printf(MSG_INFO, "Cannot add interface - it "
2935 "already exists");
2936 return -1;
2937 }
2938 }
2939
2940 hapd_iface = hostapd_iface_alloc(interfaces);
2941 if (hapd_iface == NULL) {
2942 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
2943 "for interface", __func__);
2944 goto fail;
2945 }
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002946 new_iface = hapd_iface;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002947
Dmitry Shmidt56052862013-10-04 10:23:25 -07002948 if (conf_file && interfaces->config_read_cb) {
2949 conf = interfaces->config_read_cb(conf_file);
2950 if (conf && conf->bss)
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002951 os_strlcpy(conf->bss[0]->iface, buf,
2952 sizeof(conf->bss[0]->iface));
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002953 } else {
2954 char *driver = os_strchr(ptr, ' ');
2955
2956 if (driver)
2957 *driver++ = '\0';
2958 conf = hostapd_config_alloc(interfaces, buf, ptr, driver);
2959 }
2960
Dmitry Shmidt56052862013-10-04 10:23:25 -07002961 if (conf == NULL || conf->bss == NULL) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002962 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
2963 "for configuration", __func__);
2964 goto fail;
2965 }
2966
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002967 if (hostapd_data_alloc(hapd_iface, conf) < 0) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002968 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
2969 "for hostapd", __func__);
2970 goto fail;
2971 }
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002972 conf = NULL;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002973
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002974 if (start_ctrl_iface(hapd_iface) < 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002975 goto fail;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002976
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002977 wpa_printf(MSG_INFO, "Add interface '%s'",
2978 hapd_iface->conf->bss[0]->iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002979
2980 return 0;
2981
2982fail:
2983 if (conf)
2984 hostapd_config_free(conf);
2985 if (hapd_iface) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002986 if (hapd_iface->bss) {
Dmitry Shmidt54605472013-11-08 11:10:19 -08002987 for (i = 0; i < hapd_iface->num_bss; i++) {
2988 hapd = hapd_iface->bss[i];
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08002989 if (!hapd)
2990 continue;
2991 if (hapd_iface->interfaces &&
Dmitry Shmidt54605472013-11-08 11:10:19 -08002992 hapd_iface->interfaces->ctrl_iface_deinit)
2993 hapd_iface->interfaces->
2994 ctrl_iface_deinit(hapd);
2995 wpa_printf(MSG_DEBUG, "%s: free hapd %p (%s)",
2996 __func__, hapd_iface->bss[i],
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08002997 hapd->conf->iface);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002998 hostapd_cleanup(hapd);
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08002999 os_free(hapd);
3000 hapd_iface->bss[i] = NULL;
Dmitry Shmidt54605472013-11-08 11:10:19 -08003001 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003002 os_free(hapd_iface->bss);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003003 hapd_iface->bss = NULL;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003004 }
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003005 if (new_iface) {
3006 interfaces->count--;
3007 interfaces->iface[interfaces->count] = NULL;
3008 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003009 hostapd_cleanup_iface(hapd_iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003010 }
3011 return -1;
3012}
3013
3014
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003015static int hostapd_remove_bss(struct hostapd_iface *iface, unsigned int idx)
3016{
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003017 size_t i;
3018
Dmitry Shmidt54605472013-11-08 11:10:19 -08003019 wpa_printf(MSG_INFO, "Remove BSS '%s'", iface->conf->bss[idx]->iface);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003020
Dmitry Shmidt54605472013-11-08 11:10:19 -08003021 /* Remove hostapd_data only if it has already been initialized */
3022 if (idx < iface->num_bss) {
3023 struct hostapd_data *hapd = iface->bss[idx];
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003024
Dmitry Shmidt54605472013-11-08 11:10:19 -08003025 hostapd_bss_deinit(hapd);
3026 wpa_printf(MSG_DEBUG, "%s: free hapd %p (%s)",
3027 __func__, hapd, hapd->conf->iface);
3028 hostapd_config_free_bss(hapd->conf);
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003029 hapd->conf = NULL;
Dmitry Shmidt54605472013-11-08 11:10:19 -08003030 os_free(hapd);
3031
3032 iface->num_bss--;
3033
3034 for (i = idx; i < iface->num_bss; i++)
3035 iface->bss[i] = iface->bss[i + 1];
3036 } else {
3037 hostapd_config_free_bss(iface->conf->bss[idx]);
3038 iface->conf->bss[idx] = NULL;
3039 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003040
3041 iface->conf->num_bss--;
Dmitry Shmidt54605472013-11-08 11:10:19 -08003042 for (i = idx; i < iface->conf->num_bss; i++)
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003043 iface->conf->bss[i] = iface->conf->bss[i + 1];
3044
3045 return 0;
3046}
3047
3048
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003049int hostapd_remove_iface(struct hapd_interfaces *interfaces, char *buf)
3050{
3051 struct hostapd_iface *hapd_iface;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003052 size_t i, j, k = 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003053
3054 for (i = 0; i < interfaces->count; i++) {
3055 hapd_iface = interfaces->iface[i];
3056 if (hapd_iface == NULL)
3057 return -1;
Dmitry Shmidt54605472013-11-08 11:10:19 -08003058 if (!os_strcmp(hapd_iface->conf->bss[0]->iface, buf)) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003059 wpa_printf(MSG_INFO, "Remove interface '%s'", buf);
Dmitry Shmidta38abf92014-03-06 13:38:44 -08003060 hapd_iface->driver_ap_teardown =
3061 !!(hapd_iface->drv_flags &
3062 WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
3063
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003064 hostapd_interface_deinit_free(hapd_iface);
3065 k = i;
3066 while (k < (interfaces->count - 1)) {
3067 interfaces->iface[k] =
3068 interfaces->iface[k + 1];
3069 k++;
3070 }
3071 interfaces->count--;
3072 return 0;
3073 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003074
3075 for (j = 0; j < hapd_iface->conf->num_bss; j++) {
Dmitry Shmidta38abf92014-03-06 13:38:44 -08003076 if (!os_strcmp(hapd_iface->conf->bss[j]->iface, buf)) {
3077 hapd_iface->driver_ap_teardown =
3078 !(hapd_iface->drv_flags &
3079 WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003080 return hostapd_remove_bss(hapd_iface, j);
Dmitry Shmidta38abf92014-03-06 13:38:44 -08003081 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003082 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003083 }
3084 return -1;
3085}
3086
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003087
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003088/**
3089 * hostapd_new_assoc_sta - Notify that a new station associated with the AP
3090 * @hapd: Pointer to BSS data
3091 * @sta: Pointer to the associated STA data
3092 * @reassoc: 1 to indicate this was a re-association; 0 = first association
3093 *
3094 * This function will be called whenever a station associates with the AP. It
3095 * can be called from ieee802_11.c for drivers that export MLME to hostapd and
3096 * from drv_callbacks.c based on driver events for drivers that take care of
3097 * management frames (IEEE 802.11 authentication and association) internally.
3098 */
3099void hostapd_new_assoc_sta(struct hostapd_data *hapd, struct sta_info *sta,
3100 int reassoc)
3101{
3102 if (hapd->tkip_countermeasures) {
3103 hostapd_drv_sta_deauth(hapd, sta->addr,
3104 WLAN_REASON_MICHAEL_MIC_FAILURE);
3105 return;
3106 }
3107
3108 hostapd_prune_associations(hapd, sta->addr);
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -08003109 ap_sta_clear_disconnect_timeouts(hapd, sta);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003110
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003111#ifdef CONFIG_P2P
3112 if (sta->p2p_ie == NULL && !sta->no_p2p_set) {
3113 sta->no_p2p_set = 1;
3114 hapd->num_sta_no_p2p++;
3115 if (hapd->num_sta_no_p2p == 1)
3116 hostapd_p2p_non_p2p_sta_connected(hapd);
3117 }
3118#endif /* CONFIG_P2P */
3119
Hai Shalom81f62d82019-07-22 12:10:00 -07003120 airtime_policy_new_sta(hapd, sta);
3121
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003122 /* Start accounting here, if IEEE 802.1X and WPA are not used.
3123 * IEEE 802.1X/WPA code will start accounting after the station has
3124 * been authorized. */
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -08003125 if (!hapd->conf->ieee802_1x && !hapd->conf->wpa && !hapd->conf->osen) {
3126 ap_sta_set_authorized(hapd, sta, 1);
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08003127 os_get_reltime(&sta->connected_time);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003128 accounting_sta_start(hapd, sta);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003129 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003130
3131 /* Start IEEE 802.1X authentication process for new stations */
3132 ieee802_1x_new_station(hapd, sta);
3133 if (reassoc) {
3134 if (sta->auth_alg != WLAN_AUTH_FT &&
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003135 sta->auth_alg != WLAN_AUTH_FILS_SK &&
3136 sta->auth_alg != WLAN_AUTH_FILS_SK_PFS &&
3137 sta->auth_alg != WLAN_AUTH_FILS_PK &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003138 !(sta->flags & (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS)))
3139 wpa_auth_sm_event(sta->wpa_sm, WPA_REAUTH);
3140 } else
3141 wpa_auth_sta_associated(hapd->wpa_auth, sta->wpa_sm);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003142
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08003143 if (hapd->iface->drv_flags & WPA_DRIVER_FLAGS_WIRED) {
3144 if (eloop_cancel_timeout(ap_handle_timer, hapd, sta) > 0) {
3145 wpa_printf(MSG_DEBUG,
3146 "%s: %s: canceled wired ap_handle_timer timeout for "
3147 MACSTR,
3148 hapd->conf->iface, __func__,
3149 MAC2STR(sta->addr));
3150 }
3151 } else if (!(hapd->iface->drv_flags &
3152 WPA_DRIVER_FLAGS_INACTIVITY_TIMER)) {
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -08003153 wpa_printf(MSG_DEBUG,
3154 "%s: %s: reschedule ap_handle_timer timeout for "
3155 MACSTR " (%d seconds - ap_max_inactivity)",
3156 hapd->conf->iface, __func__, MAC2STR(sta->addr),
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08003157 hapd->conf->ap_max_inactivity);
3158 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
3159 eloop_register_timeout(hapd->conf->ap_max_inactivity, 0,
3160 ap_handle_timer, hapd, sta);
3161 }
Hai Shalom81f62d82019-07-22 12:10:00 -07003162
3163#ifdef CONFIG_MACSEC
3164 if (hapd->conf->wpa_key_mgmt == WPA_KEY_MGMT_NONE &&
3165 hapd->conf->mka_psk_set)
3166 ieee802_1x_create_preshared_mka_hapd(hapd, sta);
3167 else
3168 ieee802_1x_alloc_kay_sm_hapd(hapd, sta);
3169#endif /* CONFIG_MACSEC */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003170}
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003171
3172
3173const char * hostapd_state_text(enum hostapd_iface_state s)
3174{
3175 switch (s) {
3176 case HAPD_IFACE_UNINITIALIZED:
3177 return "UNINITIALIZED";
3178 case HAPD_IFACE_DISABLED:
3179 return "DISABLED";
3180 case HAPD_IFACE_COUNTRY_UPDATE:
3181 return "COUNTRY_UPDATE";
3182 case HAPD_IFACE_ACS:
3183 return "ACS";
3184 case HAPD_IFACE_HT_SCAN:
3185 return "HT_SCAN";
3186 case HAPD_IFACE_DFS:
3187 return "DFS";
3188 case HAPD_IFACE_ENABLED:
3189 return "ENABLED";
3190 }
3191
3192 return "UNKNOWN";
3193}
3194
3195
3196void hostapd_set_state(struct hostapd_iface *iface, enum hostapd_iface_state s)
3197{
3198 wpa_printf(MSG_INFO, "%s: interface state %s->%s",
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07003199 iface->conf ? iface->conf->bss[0]->iface : "N/A",
3200 hostapd_state_text(iface->state), hostapd_state_text(s));
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003201 iface->state = s;
3202}
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003203
3204
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003205int hostapd_csa_in_progress(struct hostapd_iface *iface)
3206{
3207 unsigned int i;
3208
3209 for (i = 0; i < iface->num_bss; i++)
3210 if (iface->bss[i]->csa_in_progress)
3211 return 1;
3212 return 0;
3213}
3214
3215
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003216#ifdef NEED_AP_MLME
3217
3218static void free_beacon_data(struct beacon_data *beacon)
3219{
3220 os_free(beacon->head);
3221 beacon->head = NULL;
3222 os_free(beacon->tail);
3223 beacon->tail = NULL;
3224 os_free(beacon->probe_resp);
3225 beacon->probe_resp = NULL;
3226 os_free(beacon->beacon_ies);
3227 beacon->beacon_ies = NULL;
3228 os_free(beacon->proberesp_ies);
3229 beacon->proberesp_ies = NULL;
3230 os_free(beacon->assocresp_ies);
3231 beacon->assocresp_ies = NULL;
3232}
3233
3234
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003235static int hostapd_build_beacon_data(struct hostapd_data *hapd,
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003236 struct beacon_data *beacon)
3237{
3238 struct wpabuf *beacon_extra, *proberesp_extra, *assocresp_extra;
3239 struct wpa_driver_ap_params params;
3240 int ret;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003241
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08003242 os_memset(beacon, 0, sizeof(*beacon));
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003243 ret = ieee802_11_build_ap_params(hapd, &params);
3244 if (ret < 0)
3245 return ret;
3246
3247 ret = hostapd_build_ap_extra_ies(hapd, &beacon_extra,
3248 &proberesp_extra,
3249 &assocresp_extra);
3250 if (ret)
3251 goto free_ap_params;
3252
3253 ret = -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003254 beacon->head = os_memdup(params.head, params.head_len);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003255 if (!beacon->head)
3256 goto free_ap_extra_ies;
3257
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003258 beacon->head_len = params.head_len;
3259
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003260 beacon->tail = os_memdup(params.tail, params.tail_len);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003261 if (!beacon->tail)
3262 goto free_beacon;
3263
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003264 beacon->tail_len = params.tail_len;
3265
3266 if (params.proberesp != NULL) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003267 beacon->probe_resp = os_memdup(params.proberesp,
3268 params.proberesp_len);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003269 if (!beacon->probe_resp)
3270 goto free_beacon;
3271
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003272 beacon->probe_resp_len = params.proberesp_len;
3273 }
3274
3275 /* copy the extra ies */
3276 if (beacon_extra) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003277 beacon->beacon_ies = os_memdup(beacon_extra->buf,
3278 wpabuf_len(beacon_extra));
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003279 if (!beacon->beacon_ies)
3280 goto free_beacon;
3281
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003282 beacon->beacon_ies_len = wpabuf_len(beacon_extra);
3283 }
3284
3285 if (proberesp_extra) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003286 beacon->proberesp_ies = os_memdup(proberesp_extra->buf,
3287 wpabuf_len(proberesp_extra));
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003288 if (!beacon->proberesp_ies)
3289 goto free_beacon;
3290
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003291 beacon->proberesp_ies_len = wpabuf_len(proberesp_extra);
3292 }
3293
3294 if (assocresp_extra) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003295 beacon->assocresp_ies = os_memdup(assocresp_extra->buf,
3296 wpabuf_len(assocresp_extra));
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003297 if (!beacon->assocresp_ies)
3298 goto free_beacon;
3299
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003300 beacon->assocresp_ies_len = wpabuf_len(assocresp_extra);
3301 }
3302
3303 ret = 0;
3304free_beacon:
3305 /* if the function fails, the caller should not free beacon data */
3306 if (ret)
3307 free_beacon_data(beacon);
3308
3309free_ap_extra_ies:
3310 hostapd_free_ap_extra_ies(hapd, beacon_extra, proberesp_extra,
3311 assocresp_extra);
3312free_ap_params:
3313 ieee802_11_free_ap_params(&params);
3314 return ret;
3315}
3316
3317
3318/*
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003319 * TODO: This flow currently supports only changing channel and width within
3320 * the same hw_mode. Any other changes to MAC parameters or provided settings
3321 * are not supported.
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003322 */
3323static int hostapd_change_config_freq(struct hostapd_data *hapd,
3324 struct hostapd_config *conf,
3325 struct hostapd_freq_params *params,
3326 struct hostapd_freq_params *old_params)
3327{
3328 int channel;
Hai Shalom81f62d82019-07-22 12:10:00 -07003329 u8 seg0, seg1;
3330 struct hostapd_hw_modes *mode;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003331
3332 if (!params->channel) {
3333 /* check if the new channel is supported by hw */
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003334 params->channel = hostapd_hw_get_channel(hapd, params->freq);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003335 }
3336
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003337 channel = params->channel;
3338 if (!channel)
3339 return -1;
3340
Hai Shalom81f62d82019-07-22 12:10:00 -07003341 mode = hapd->iface->current_mode;
3342
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003343 /* if a pointer to old_params is provided we save previous state */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003344 if (old_params &&
3345 hostapd_set_freq_params(old_params, conf->hw_mode,
3346 hostapd_hw_get_freq(hapd, conf->channel),
Hai Shalomc3565922019-10-28 11:58:20 -07003347 conf->channel, conf->enable_edmg,
3348 conf->edmg_channel, conf->ieee80211n,
Hai Shalom81f62d82019-07-22 12:10:00 -07003349 conf->ieee80211ac, conf->ieee80211ax,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003350 conf->secondary_channel,
Hai Shalom81f62d82019-07-22 12:10:00 -07003351 hostapd_get_oper_chwidth(conf),
3352 hostapd_get_oper_centr_freq_seg0_idx(conf),
3353 hostapd_get_oper_centr_freq_seg1_idx(conf),
3354 conf->vht_capab,
3355 mode ? &mode->he_capab[IEEE80211_MODE_AP] :
3356 NULL))
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003357 return -1;
3358
3359 switch (params->bandwidth) {
3360 case 0:
3361 case 20:
3362 case 40:
Hai Shalom81f62d82019-07-22 12:10:00 -07003363 hostapd_set_oper_chwidth(conf, CHANWIDTH_USE_HT);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003364 break;
3365 case 80:
3366 if (params->center_freq2)
Hai Shalom81f62d82019-07-22 12:10:00 -07003367 hostapd_set_oper_chwidth(conf, CHANWIDTH_80P80MHZ);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003368 else
Hai Shalom81f62d82019-07-22 12:10:00 -07003369 hostapd_set_oper_chwidth(conf, CHANWIDTH_80MHZ);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003370 break;
3371 case 160:
Hai Shalom81f62d82019-07-22 12:10:00 -07003372 hostapd_set_oper_chwidth(conf, CHANWIDTH_160MHZ);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003373 break;
3374 default:
3375 return -1;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003376 }
3377
3378 conf->channel = channel;
3379 conf->ieee80211n = params->ht_enabled;
3380 conf->secondary_channel = params->sec_channel_offset;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003381 ieee80211_freq_to_chan(params->center_freq1,
Hai Shalom81f62d82019-07-22 12:10:00 -07003382 &seg0);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003383 ieee80211_freq_to_chan(params->center_freq2,
Hai Shalom81f62d82019-07-22 12:10:00 -07003384 &seg1);
3385 hostapd_set_oper_centr_freq_seg0_idx(conf, seg0);
3386 hostapd_set_oper_centr_freq_seg1_idx(conf, seg1);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003387
3388 /* TODO: maybe call here hostapd_config_check here? */
3389
3390 return 0;
3391}
3392
3393
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003394static int hostapd_fill_csa_settings(struct hostapd_data *hapd,
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003395 struct csa_settings *settings)
3396{
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003397 struct hostapd_iface *iface = hapd->iface;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003398 struct hostapd_freq_params old_freq;
3399 int ret;
Hai Shalom81f62d82019-07-22 12:10:00 -07003400 u8 chan, bandwidth;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003401
3402 os_memset(&old_freq, 0, sizeof(old_freq));
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003403 if (!iface || !iface->freq || hapd->csa_in_progress)
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003404 return -1;
3405
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003406 switch (settings->freq_params.bandwidth) {
3407 case 80:
3408 if (settings->freq_params.center_freq2)
Hai Shalom81f62d82019-07-22 12:10:00 -07003409 bandwidth = CHANWIDTH_80P80MHZ;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003410 else
Hai Shalom81f62d82019-07-22 12:10:00 -07003411 bandwidth = CHANWIDTH_80MHZ;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003412 break;
3413 case 160:
Hai Shalom81f62d82019-07-22 12:10:00 -07003414 bandwidth = CHANWIDTH_160MHZ;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003415 break;
3416 default:
Hai Shalom81f62d82019-07-22 12:10:00 -07003417 bandwidth = CHANWIDTH_USE_HT;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003418 break;
3419 }
3420
3421 if (ieee80211_freq_to_channel_ext(
3422 settings->freq_params.freq,
3423 settings->freq_params.sec_channel_offset,
Hai Shalom81f62d82019-07-22 12:10:00 -07003424 bandwidth,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003425 &hapd->iface->cs_oper_class,
3426 &chan) == NUM_HOSTAPD_MODES) {
3427 wpa_printf(MSG_DEBUG,
Hai Shalom81f62d82019-07-22 12:10:00 -07003428 "invalid frequency for channel switch (freq=%d, sec_channel_offset=%d, vht_enabled=%d, he_enabled=%d)",
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003429 settings->freq_params.freq,
3430 settings->freq_params.sec_channel_offset,
Hai Shalom81f62d82019-07-22 12:10:00 -07003431 settings->freq_params.vht_enabled,
3432 settings->freq_params.he_enabled);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003433 return -1;
3434 }
3435
3436 settings->freq_params.channel = chan;
3437
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003438 ret = hostapd_change_config_freq(iface->bss[0], iface->conf,
3439 &settings->freq_params,
3440 &old_freq);
3441 if (ret)
3442 return ret;
3443
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003444 ret = hostapd_build_beacon_data(hapd, &settings->beacon_after);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003445
3446 /* change back the configuration */
3447 hostapd_change_config_freq(iface->bss[0], iface->conf,
3448 &old_freq, NULL);
3449
3450 if (ret)
3451 return ret;
3452
3453 /* set channel switch parameters for csa ie */
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003454 hapd->cs_freq_params = settings->freq_params;
3455 hapd->cs_count = settings->cs_count;
3456 hapd->cs_block_tx = settings->block_tx;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003457
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003458 ret = hostapd_build_beacon_data(hapd, &settings->beacon_csa);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003459 if (ret) {
3460 free_beacon_data(&settings->beacon_after);
3461 return ret;
3462 }
3463
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003464 settings->counter_offset_beacon[0] = hapd->cs_c_off_beacon;
3465 settings->counter_offset_presp[0] = hapd->cs_c_off_proberesp;
3466 settings->counter_offset_beacon[1] = hapd->cs_c_off_ecsa_beacon;
3467 settings->counter_offset_presp[1] = hapd->cs_c_off_ecsa_proberesp;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003468
3469 return 0;
3470}
3471
3472
3473void hostapd_cleanup_cs_params(struct hostapd_data *hapd)
3474{
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003475 os_memset(&hapd->cs_freq_params, 0, sizeof(hapd->cs_freq_params));
3476 hapd->cs_count = 0;
3477 hapd->cs_block_tx = 0;
3478 hapd->cs_c_off_beacon = 0;
3479 hapd->cs_c_off_proberesp = 0;
3480 hapd->csa_in_progress = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003481 hapd->cs_c_off_ecsa_beacon = 0;
3482 hapd->cs_c_off_ecsa_proberesp = 0;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003483}
3484
3485
Roshan Pius3a1667e2018-07-03 15:17:14 -07003486void hostapd_chan_switch_vht_config(struct hostapd_data *hapd, int vht_enabled)
3487{
3488 if (vht_enabled)
3489 hapd->iconf->ch_switch_vht_config |= CH_SWITCH_VHT_ENABLED;
3490 else
3491 hapd->iconf->ch_switch_vht_config |= CH_SWITCH_VHT_DISABLED;
3492
3493 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
3494 HOSTAPD_LEVEL_INFO, "CHAN_SWITCH VHT CONFIG 0x%x",
3495 hapd->iconf->ch_switch_vht_config);
3496}
3497
3498
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003499int hostapd_switch_channel(struct hostapd_data *hapd,
3500 struct csa_settings *settings)
3501{
3502 int ret;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003503
3504 if (!(hapd->iface->drv_flags & WPA_DRIVER_FLAGS_AP_CSA)) {
3505 wpa_printf(MSG_INFO, "CSA is not supported");
3506 return -1;
3507 }
3508
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003509 ret = hostapd_fill_csa_settings(hapd, settings);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003510 if (ret)
3511 return ret;
3512
3513 ret = hostapd_drv_switch_channel(hapd, settings);
3514 free_beacon_data(&settings->beacon_csa);
3515 free_beacon_data(&settings->beacon_after);
3516
3517 if (ret) {
3518 /* if we failed, clean cs parameters */
3519 hostapd_cleanup_cs_params(hapd);
3520 return ret;
3521 }
3522
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003523 hapd->csa_in_progress = 1;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003524 return 0;
3525}
3526
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003527
3528void
3529hostapd_switch_channel_fallback(struct hostapd_iface *iface,
3530 const struct hostapd_freq_params *freq_params)
3531{
Hai Shalom81f62d82019-07-22 12:10:00 -07003532 int seg0_idx = 0, seg1_idx = 0, bw = CHANWIDTH_USE_HT;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003533
3534 wpa_printf(MSG_DEBUG, "Restarting all CSA-related BSSes");
3535
3536 if (freq_params->center_freq1)
Hai Shalom81f62d82019-07-22 12:10:00 -07003537 seg0_idx = 36 + (freq_params->center_freq1 - 5180) / 5;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003538 if (freq_params->center_freq2)
Hai Shalom81f62d82019-07-22 12:10:00 -07003539 seg1_idx = 36 + (freq_params->center_freq2 - 5180) / 5;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003540
3541 switch (freq_params->bandwidth) {
3542 case 0:
3543 case 20:
3544 case 40:
Hai Shalom81f62d82019-07-22 12:10:00 -07003545 bw = CHANWIDTH_USE_HT;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003546 break;
3547 case 80:
3548 if (freq_params->center_freq2)
Hai Shalom81f62d82019-07-22 12:10:00 -07003549 bw = CHANWIDTH_80P80MHZ;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003550 else
Hai Shalom81f62d82019-07-22 12:10:00 -07003551 bw = CHANWIDTH_80MHZ;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003552 break;
3553 case 160:
Hai Shalom81f62d82019-07-22 12:10:00 -07003554 bw = CHANWIDTH_160MHZ;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003555 break;
3556 default:
3557 wpa_printf(MSG_WARNING, "Unknown CSA bandwidth: %d",
3558 freq_params->bandwidth);
3559 break;
3560 }
3561
3562 iface->freq = freq_params->freq;
3563 iface->conf->channel = freq_params->channel;
3564 iface->conf->secondary_channel = freq_params->sec_channel_offset;
Hai Shalom81f62d82019-07-22 12:10:00 -07003565 hostapd_set_oper_centr_freq_seg0_idx(iface->conf, seg0_idx);
3566 hostapd_set_oper_centr_freq_seg1_idx(iface->conf, seg1_idx);
3567 hostapd_set_oper_chwidth(iface->conf, bw);
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003568 iface->conf->ieee80211n = freq_params->ht_enabled;
3569 iface->conf->ieee80211ac = freq_params->vht_enabled;
Hai Shalom81f62d82019-07-22 12:10:00 -07003570 iface->conf->ieee80211ax = freq_params->he_enabled;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003571
3572 /*
3573 * cs_params must not be cleared earlier because the freq_params
3574 * argument may actually point to one of these.
Hai Shalom39ba6fc2019-01-22 12:40:38 -08003575 * These params will be cleared during interface disable below.
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003576 */
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003577 hostapd_disable_iface(iface);
3578 hostapd_enable_iface(iface);
3579}
3580
Dmitry Shmidte4663042016-04-04 10:07:49 -07003581#endif /* NEED_AP_MLME */
3582
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003583
3584struct hostapd_data * hostapd_get_iface(struct hapd_interfaces *interfaces,
3585 const char *ifname)
3586{
3587 size_t i, j;
3588
3589 for (i = 0; i < interfaces->count; i++) {
3590 struct hostapd_iface *iface = interfaces->iface[i];
3591
3592 for (j = 0; j < iface->num_bss; j++) {
3593 struct hostapd_data *hapd = iface->bss[j];
3594
3595 if (os_strcmp(ifname, hapd->conf->iface) == 0)
3596 return hapd;
3597 }
3598 }
3599
3600 return NULL;
3601}
3602
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003603
3604void hostapd_periodic_iface(struct hostapd_iface *iface)
3605{
3606 size_t i;
3607
3608 ap_list_timer(iface);
3609
3610 for (i = 0; i < iface->num_bss; i++) {
3611 struct hostapd_data *hapd = iface->bss[i];
3612
3613 if (!hapd->started)
3614 continue;
3615
3616#ifndef CONFIG_NO_RADIUS
3617 hostapd_acl_expire(hapd);
3618#endif /* CONFIG_NO_RADIUS */
3619 }
3620}