blob: 368643867f38c0b5eb03a3082ee56bd9a75a8a13 [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
1575static int setup_interface2(struct hostapd_iface *iface)
1576{
1577 iface->wait_channel_update = 0;
1578
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001579 if (hostapd_get_hw_features(iface)) {
1580 /* Not all drivers support this yet, so continue without hw
1581 * feature data. */
1582 } else {
1583 int ret = hostapd_select_hw_mode(iface);
1584 if (ret < 0) {
1585 wpa_printf(MSG_ERROR, "Could not select hw_mode and "
1586 "channel. (%d)", ret);
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001587 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001588 }
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001589 if (ret == 1) {
1590 wpa_printf(MSG_DEBUG, "Interface initialization will be completed in a callback (ACS)");
1591 return 0;
1592 }
Hai Shalomc3565922019-10-28 11:58:20 -07001593 ret = hostapd_check_edmg_capab(iface);
1594 if (ret < 0)
1595 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001596 ret = hostapd_check_ht_capab(iface);
1597 if (ret < 0)
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001598 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001599 if (ret == 1) {
1600 wpa_printf(MSG_DEBUG, "Interface initialization will "
1601 "be completed in a callback");
1602 return 0;
1603 }
Dmitry Shmidt051af732013-10-22 13:52:46 -07001604
1605 if (iface->conf->ieee80211h)
1606 wpa_printf(MSG_DEBUG, "DFS support is enabled");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001607 }
1608 return hostapd_setup_interface_complete(iface, 0);
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001609
1610fail:
1611 hostapd_set_state(iface, HAPD_IFACE_DISABLED);
1612 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
1613 if (iface->interfaces && iface->interfaces->terminate_on_error)
1614 eloop_terminate();
1615 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001616}
1617
1618
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001619#ifdef CONFIG_FST
1620
1621static const u8 * fst_hostapd_get_bssid_cb(void *ctx)
1622{
1623 struct hostapd_data *hapd = ctx;
1624
1625 return hapd->own_addr;
1626}
1627
1628
1629static void fst_hostapd_get_channel_info_cb(void *ctx,
1630 enum hostapd_hw_mode *hw_mode,
1631 u8 *channel)
1632{
1633 struct hostapd_data *hapd = ctx;
1634
1635 *hw_mode = ieee80211_freq_to_chan(hapd->iface->freq, channel);
1636}
1637
1638
1639static void fst_hostapd_set_ies_cb(void *ctx, const struct wpabuf *fst_ies)
1640{
1641 struct hostapd_data *hapd = ctx;
1642
1643 if (hapd->iface->fst_ies != fst_ies) {
1644 hapd->iface->fst_ies = fst_ies;
1645 if (ieee802_11_set_beacon(hapd))
1646 wpa_printf(MSG_WARNING, "FST: Cannot set beacon");
1647 }
1648}
1649
1650
1651static int fst_hostapd_send_action_cb(void *ctx, const u8 *da,
1652 struct wpabuf *buf)
1653{
1654 struct hostapd_data *hapd = ctx;
1655
1656 return hostapd_drv_send_action(hapd, hapd->iface->freq, 0, da,
1657 wpabuf_head(buf), wpabuf_len(buf));
1658}
1659
1660
1661static const struct wpabuf * fst_hostapd_get_mb_ie_cb(void *ctx, const u8 *addr)
1662{
1663 struct hostapd_data *hapd = ctx;
1664 struct sta_info *sta = ap_get_sta(hapd, addr);
1665
1666 return sta ? sta->mb_ies : NULL;
1667}
1668
1669
1670static void fst_hostapd_update_mb_ie_cb(void *ctx, const u8 *addr,
1671 const u8 *buf, size_t size)
1672{
1673 struct hostapd_data *hapd = ctx;
1674 struct sta_info *sta = ap_get_sta(hapd, addr);
1675
1676 if (sta) {
1677 struct mb_ies_info info;
1678
1679 if (!mb_ies_info_by_ies(&info, buf, size)) {
1680 wpabuf_free(sta->mb_ies);
1681 sta->mb_ies = mb_ies_by_info(&info);
1682 }
1683 }
1684}
1685
1686
1687static const u8 * fst_hostapd_get_sta(struct fst_get_peer_ctx **get_ctx,
1688 Boolean mb_only)
1689{
1690 struct sta_info *s = (struct sta_info *) *get_ctx;
1691
1692 if (mb_only) {
1693 for (; s && !s->mb_ies; s = s->next)
1694 ;
1695 }
1696
1697 if (s) {
1698 *get_ctx = (struct fst_get_peer_ctx *) s->next;
1699
1700 return s->addr;
1701 }
1702
1703 *get_ctx = NULL;
1704 return NULL;
1705}
1706
1707
1708static const u8 * fst_hostapd_get_peer_first(void *ctx,
1709 struct fst_get_peer_ctx **get_ctx,
1710 Boolean mb_only)
1711{
1712 struct hostapd_data *hapd = ctx;
1713
1714 *get_ctx = (struct fst_get_peer_ctx *) hapd->sta_list;
1715
1716 return fst_hostapd_get_sta(get_ctx, mb_only);
1717}
1718
1719
1720static const u8 * fst_hostapd_get_peer_next(void *ctx,
1721 struct fst_get_peer_ctx **get_ctx,
1722 Boolean mb_only)
1723{
1724 return fst_hostapd_get_sta(get_ctx, mb_only);
1725}
1726
1727
1728void fst_hostapd_fill_iface_obj(struct hostapd_data *hapd,
1729 struct fst_wpa_obj *iface_obj)
1730{
1731 iface_obj->ctx = hapd;
1732 iface_obj->get_bssid = fst_hostapd_get_bssid_cb;
1733 iface_obj->get_channel_info = fst_hostapd_get_channel_info_cb;
1734 iface_obj->set_ies = fst_hostapd_set_ies_cb;
1735 iface_obj->send_action = fst_hostapd_send_action_cb;
1736 iface_obj->get_mb_ie = fst_hostapd_get_mb_ie_cb;
1737 iface_obj->update_mb_ie = fst_hostapd_update_mb_ie_cb;
1738 iface_obj->get_peer_first = fst_hostapd_get_peer_first;
1739 iface_obj->get_peer_next = fst_hostapd_get_peer_next;
1740}
1741
1742#endif /* CONFIG_FST */
1743
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001744#ifdef CONFIG_OWE
1745
1746static int hostapd_owe_iface_iter(struct hostapd_iface *iface, void *ctx)
1747{
1748 struct hostapd_data *hapd = ctx;
1749 size_t i;
1750
1751 for (i = 0; i < iface->num_bss; i++) {
1752 struct hostapd_data *bss = iface->bss[i];
1753
1754 if (os_strcmp(hapd->conf->owe_transition_ifname,
1755 bss->conf->iface) != 0)
1756 continue;
1757
1758 wpa_printf(MSG_DEBUG,
1759 "OWE: ifname=%s found transition mode ifname=%s BSSID "
1760 MACSTR " SSID %s",
1761 hapd->conf->iface, bss->conf->iface,
1762 MAC2STR(bss->own_addr),
1763 wpa_ssid_txt(bss->conf->ssid.ssid,
1764 bss->conf->ssid.ssid_len));
1765 if (!bss->conf->ssid.ssid_set || !bss->conf->ssid.ssid_len ||
1766 is_zero_ether_addr(bss->own_addr))
1767 continue;
1768
1769 os_memcpy(hapd->conf->owe_transition_bssid, bss->own_addr,
1770 ETH_ALEN);
1771 os_memcpy(hapd->conf->owe_transition_ssid,
1772 bss->conf->ssid.ssid, bss->conf->ssid.ssid_len);
1773 hapd->conf->owe_transition_ssid_len = bss->conf->ssid.ssid_len;
1774 wpa_printf(MSG_DEBUG,
1775 "OWE: Copied transition mode information");
1776 return 1;
1777 }
1778
1779 return 0;
1780}
1781
1782
1783int hostapd_owe_trans_get_info(struct hostapd_data *hapd)
1784{
1785 if (hapd->conf->owe_transition_ssid_len > 0 &&
1786 !is_zero_ether_addr(hapd->conf->owe_transition_bssid))
1787 return 0;
1788
1789 /* Find transition mode SSID/BSSID information from a BSS operated by
1790 * this hostapd instance. */
1791 if (!hapd->iface->interfaces ||
1792 !hapd->iface->interfaces->for_each_interface)
1793 return hostapd_owe_iface_iter(hapd->iface, hapd);
1794 else
1795 return hapd->iface->interfaces->for_each_interface(
1796 hapd->iface->interfaces, hostapd_owe_iface_iter, hapd);
1797}
1798
1799
1800static int hostapd_owe_iface_iter2(struct hostapd_iface *iface, void *ctx)
1801{
1802 size_t i;
1803
1804 for (i = 0; i < iface->num_bss; i++) {
1805 struct hostapd_data *bss = iface->bss[i];
1806 int res;
1807
1808 if (!bss->conf->owe_transition_ifname[0])
1809 continue;
1810 res = hostapd_owe_trans_get_info(bss);
1811 if (res == 0)
1812 continue;
1813 wpa_printf(MSG_DEBUG,
1814 "OWE: Matching transition mode interface enabled - update beacon data for %s",
1815 bss->conf->iface);
1816 ieee802_11_set_beacon(bss);
1817 }
1818
1819 return 0;
1820}
1821
1822#endif /* CONFIG_OWE */
1823
1824
1825static void hostapd_owe_update_trans(struct hostapd_iface *iface)
1826{
1827#ifdef CONFIG_OWE
1828 /* Check whether the enabled BSS can complete OWE transition mode
1829 * configuration for any pending interface. */
1830 if (!iface->interfaces ||
1831 !iface->interfaces->for_each_interface)
1832 hostapd_owe_iface_iter2(iface, NULL);
1833 else
1834 iface->interfaces->for_each_interface(
1835 iface->interfaces, hostapd_owe_iface_iter2, NULL);
1836#endif /* CONFIG_OWE */
1837}
1838
1839
Roshan Pius3a1667e2018-07-03 15:17:14 -07001840static void hostapd_interface_setup_failure_handler(void *eloop_ctx,
1841 void *timeout_ctx)
1842{
1843 struct hostapd_iface *iface = eloop_ctx;
1844 struct hostapd_data *hapd;
1845
1846 if (iface->num_bss < 1 || !iface->bss || !iface->bss[0])
1847 return;
1848 hapd = iface->bss[0];
1849 if (hapd->setup_complete_cb)
1850 hapd->setup_complete_cb(hapd->setup_complete_cb_ctx);
1851}
1852
1853
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001854static int hostapd_setup_interface_complete_sync(struct hostapd_iface *iface,
1855 int err)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001856{
1857 struct hostapd_data *hapd = iface->bss[0];
1858 size_t j;
1859 u8 *prev_addr;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001860 int delay_apply_cfg = 0;
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001861 int res_dfs_offload = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001862
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001863 if (err)
1864 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001865
1866 wpa_printf(MSG_DEBUG, "Completing interface initialization");
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001867 if (iface->conf->channel) {
Dmitry Shmidt051af732013-10-22 13:52:46 -07001868#ifdef NEED_AP_MLME
1869 int res;
1870#endif /* NEED_AP_MLME */
1871
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001872 iface->freq = hostapd_hw_get_freq(hapd, iface->conf->channel);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001873 wpa_printf(MSG_DEBUG, "Mode: %s Channel: %d "
1874 "Frequency: %d MHz",
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001875 hostapd_hw_mode_txt(iface->conf->hw_mode),
1876 iface->conf->channel, iface->freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001877
Dmitry Shmidt051af732013-10-22 13:52:46 -07001878#ifdef NEED_AP_MLME
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001879 /* Handle DFS only if it is not offloaded to the driver */
1880 if (!(iface->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD)) {
1881 /* Check DFS */
1882 res = hostapd_handle_dfs(iface);
1883 if (res <= 0) {
1884 if (res < 0)
1885 goto fail;
1886 return res;
1887 }
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001888 } else {
1889 /* If DFS is offloaded to the driver */
1890 res_dfs_offload = hostapd_handle_dfs_offload(iface);
1891 if (res_dfs_offload <= 0) {
1892 if (res_dfs_offload < 0)
1893 goto fail;
1894 } else {
1895 wpa_printf(MSG_DEBUG,
1896 "Proceed with AP/channel setup");
1897 /*
1898 * If this is a DFS channel, move to completing
1899 * AP setup.
1900 */
1901 if (res_dfs_offload == 1)
1902 goto dfs_offload;
1903 /* Otherwise fall through. */
1904 }
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001905 }
Dmitry Shmidt051af732013-10-22 13:52:46 -07001906#endif /* NEED_AP_MLME */
1907
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001908#ifdef CONFIG_MESH
1909 if (iface->mconf != NULL) {
1910 wpa_printf(MSG_DEBUG,
1911 "%s: Mesh configuration will be applied while joining the mesh network",
1912 iface->bss[0]->conf->iface);
1913 delay_apply_cfg = 1;
1914 }
1915#endif /* CONFIG_MESH */
1916
1917 if (!delay_apply_cfg &&
1918 hostapd_set_freq(hapd, hapd->iconf->hw_mode, iface->freq,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001919 hapd->iconf->channel,
Hai Shalomc3565922019-10-28 11:58:20 -07001920 hapd->iconf->enable_edmg,
1921 hapd->iconf->edmg_channel,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001922 hapd->iconf->ieee80211n,
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001923 hapd->iconf->ieee80211ac,
Hai Shalom81f62d82019-07-22 12:10:00 -07001924 hapd->iconf->ieee80211ax,
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001925 hapd->iconf->secondary_channel,
Hai Shalom81f62d82019-07-22 12:10:00 -07001926 hostapd_get_oper_chwidth(hapd->iconf),
1927 hostapd_get_oper_centr_freq_seg0_idx(
1928 hapd->iconf),
1929 hostapd_get_oper_centr_freq_seg1_idx(
1930 hapd->iconf))) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001931 wpa_printf(MSG_ERROR, "Could not set channel for "
1932 "kernel driver");
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001933 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001934 }
1935 }
1936
1937 if (iface->current_mode) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001938 if (hostapd_prepare_rates(iface, iface->current_mode)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001939 wpa_printf(MSG_ERROR, "Failed to prepare rates "
1940 "table.");
1941 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
1942 HOSTAPD_LEVEL_WARNING,
1943 "Failed to prepare rates table.");
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001944 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001945 }
1946 }
1947
Hai Shalom021b0b52019-04-10 11:17:58 -07001948 if (hapd->iconf->rts_threshold >= -1 &&
1949 hostapd_set_rts(hapd, hapd->iconf->rts_threshold) &&
1950 hapd->iconf->rts_threshold >= -1) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001951 wpa_printf(MSG_ERROR, "Could not set RTS threshold for "
1952 "kernel driver");
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001953 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001954 }
1955
Hai Shalom021b0b52019-04-10 11:17:58 -07001956 if (hapd->iconf->fragm_threshold >= -1 &&
1957 hostapd_set_frag(hapd, hapd->iconf->fragm_threshold) &&
1958 hapd->iconf->fragm_threshold != -1) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001959 wpa_printf(MSG_ERROR, "Could not set fragmentation threshold "
1960 "for kernel driver");
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001961 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001962 }
1963
1964 prev_addr = hapd->own_addr;
1965
1966 for (j = 0; j < iface->num_bss; j++) {
1967 hapd = iface->bss[j];
1968 if (j)
1969 os_memcpy(hapd->own_addr, prev_addr, ETH_ALEN);
Dmitry Shmidt71757432014-06-02 13:50:35 -07001970 if (hostapd_setup_bss(hapd, j == 0)) {
Hai Shalom021b0b52019-04-10 11:17:58 -07001971 for (;;) {
Dmitry Shmidt71757432014-06-02 13:50:35 -07001972 hapd = iface->bss[j];
1973 hostapd_bss_deinit_no_free(hapd);
1974 hostapd_free_hapd_data(hapd);
Hai Shalom021b0b52019-04-10 11:17:58 -07001975 if (j == 0)
1976 break;
1977 j--;
1978 }
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001979 goto fail;
Dmitry Shmidt71757432014-06-02 13:50:35 -07001980 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001981 if (is_zero_ether_addr(hapd->conf->bssid))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001982 prev_addr = hapd->own_addr;
1983 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001984 hapd = iface->bss[0];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001985
1986 hostapd_tx_queue_params(iface);
1987
1988 ap_list_init(iface);
1989
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001990 hostapd_set_acl(hapd);
1991
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001992 if (hostapd_driver_commit(hapd) < 0) {
1993 wpa_printf(MSG_ERROR, "%s: Failed to commit driver "
1994 "configuration", __func__);
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001995 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001996 }
1997
Jouni Malinen87fd2792011-05-16 18:35:42 +03001998 /*
1999 * WPS UPnP module can be initialized only when the "upnp_iface" is up.
2000 * If "interface" and "upnp_iface" are the same (e.g., non-bridge
2001 * mode), the interface is up only after driver_commit, so initialize
2002 * WPS after driver_commit.
2003 */
2004 for (j = 0; j < iface->num_bss; j++) {
2005 if (hostapd_init_wps_complete(iface->bss[j]))
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002006 goto fail;
Jouni Malinen87fd2792011-05-16 18:35:42 +03002007 }
2008
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08002009 if ((iface->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
2010 !res_dfs_offload) {
2011 /*
2012 * If freq is DFS, and DFS is offloaded to the driver, then wait
2013 * for CAC to complete.
2014 */
2015 wpa_printf(MSG_DEBUG, "%s: Wait for CAC to complete", __func__);
2016 return res_dfs_offload;
2017 }
2018
2019#ifdef NEED_AP_MLME
2020dfs_offload:
2021#endif /* NEED_AP_MLME */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002022
2023#ifdef CONFIG_FST
2024 if (hapd->iconf->fst_cfg.group_id[0]) {
2025 struct fst_wpa_obj iface_obj;
2026
2027 fst_hostapd_fill_iface_obj(hapd, &iface_obj);
2028 iface->fst = fst_attach(hapd->conf->iface, hapd->own_addr,
2029 &iface_obj, &hapd->iconf->fst_cfg);
2030 if (!iface->fst) {
2031 wpa_printf(MSG_ERROR, "Could not attach to FST %s",
2032 hapd->iconf->fst_cfg.group_id);
2033 goto fail;
2034 }
2035 }
2036#endif /* CONFIG_FST */
2037
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002038 hostapd_set_state(iface, HAPD_IFACE_ENABLED);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002039 hostapd_owe_update_trans(iface);
Hai Shalom81f62d82019-07-22 12:10:00 -07002040 airtime_policy_update_init(iface);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002041 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_ENABLED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002042 if (hapd->setup_complete_cb)
2043 hapd->setup_complete_cb(hapd->setup_complete_cb_ctx);
2044
2045 wpa_printf(MSG_DEBUG, "%s: Setup of interface done.",
2046 iface->bss[0]->conf->iface);
Dmitry Shmidtb96dad42013-11-05 10:07:29 -08002047 if (iface->interfaces && iface->interfaces->terminate_on_error > 0)
2048 iface->interfaces->terminate_on_error--;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002049
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002050 for (j = 0; j < iface->num_bss; j++)
Hai Shalom74f70d42019-02-11 14:42:39 -08002051 hostapd_neighbor_set_own_report(iface->bss[j]);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002052
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002053 return 0;
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002054
2055fail:
2056 wpa_printf(MSG_ERROR, "Interface initialization failed");
2057 hostapd_set_state(iface, HAPD_IFACE_DISABLED);
2058 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002059#ifdef CONFIG_FST
2060 if (iface->fst) {
2061 fst_detach(iface->fst);
2062 iface->fst = NULL;
2063 }
2064#endif /* CONFIG_FST */
Roshan Pius3a1667e2018-07-03 15:17:14 -07002065
2066 if (iface->interfaces && iface->interfaces->terminate_on_error) {
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002067 eloop_terminate();
Roshan Pius3a1667e2018-07-03 15:17:14 -07002068 } else if (hapd->setup_complete_cb) {
2069 /*
2070 * Calling hapd->setup_complete_cb directly may cause iface
2071 * deinitialization which may be accessed later by the caller.
2072 */
2073 eloop_register_timeout(0, 0,
2074 hostapd_interface_setup_failure_handler,
2075 iface, NULL);
2076 }
2077
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002078 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002079}
2080
2081
2082/**
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002083 * hostapd_setup_interface_complete - Complete interface setup
2084 *
2085 * This function is called when previous steps in the interface setup has been
2086 * completed. This can also start operations, e.g., DFS, that will require
2087 * additional processing before interface is ready to be enabled. Such
2088 * operations will call this function from eloop callbacks when finished.
2089 */
2090int hostapd_setup_interface_complete(struct hostapd_iface *iface, int err)
2091{
2092 struct hapd_interfaces *interfaces = iface->interfaces;
2093 struct hostapd_data *hapd = iface->bss[0];
2094 unsigned int i;
2095 int not_ready_in_sync_ifaces = 0;
2096
2097 if (!iface->need_to_start_in_sync)
2098 return hostapd_setup_interface_complete_sync(iface, err);
2099
2100 if (err) {
2101 wpa_printf(MSG_ERROR, "Interface initialization failed");
2102 hostapd_set_state(iface, HAPD_IFACE_DISABLED);
2103 iface->need_to_start_in_sync = 0;
2104 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
2105 if (interfaces && interfaces->terminate_on_error)
2106 eloop_terminate();
2107 return -1;
2108 }
2109
2110 if (iface->ready_to_start_in_sync) {
2111 /* Already in ready and waiting. should never happpen */
2112 return 0;
2113 }
2114
2115 for (i = 0; i < interfaces->count; i++) {
2116 if (interfaces->iface[i]->need_to_start_in_sync &&
2117 !interfaces->iface[i]->ready_to_start_in_sync)
2118 not_ready_in_sync_ifaces++;
2119 }
2120
2121 /*
2122 * Check if this is the last interface, if yes then start all the other
2123 * waiting interfaces. If not, add this interface to the waiting list.
2124 */
2125 if (not_ready_in_sync_ifaces > 1 && iface->state == HAPD_IFACE_DFS) {
2126 /*
2127 * If this interface went through CAC, do not synchronize, just
2128 * start immediately.
2129 */
2130 iface->need_to_start_in_sync = 0;
2131 wpa_printf(MSG_INFO,
2132 "%s: Finished CAC - bypass sync and start interface",
2133 iface->bss[0]->conf->iface);
2134 return hostapd_setup_interface_complete_sync(iface, err);
2135 }
2136
2137 if (not_ready_in_sync_ifaces > 1) {
2138 /* need to wait as there are other interfaces still coming up */
2139 iface->ready_to_start_in_sync = 1;
2140 wpa_printf(MSG_INFO,
2141 "%s: Interface waiting to sync with other interfaces",
2142 iface->bss[0]->conf->iface);
2143 return 0;
2144 }
2145
2146 wpa_printf(MSG_INFO,
2147 "%s: Last interface to sync - starting all interfaces",
2148 iface->bss[0]->conf->iface);
2149 iface->need_to_start_in_sync = 0;
2150 hostapd_setup_interface_complete_sync(iface, err);
2151 for (i = 0; i < interfaces->count; i++) {
2152 if (interfaces->iface[i]->need_to_start_in_sync &&
2153 interfaces->iface[i]->ready_to_start_in_sync) {
2154 hostapd_setup_interface_complete_sync(
2155 interfaces->iface[i], 0);
2156 /* Only once the interfaces are sync started */
2157 interfaces->iface[i]->need_to_start_in_sync = 0;
2158 }
2159 }
2160
2161 return 0;
2162}
2163
2164
2165/**
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002166 * hostapd_setup_interface - Setup of an interface
2167 * @iface: Pointer to interface data.
2168 * Returns: 0 on success, -1 on failure
2169 *
2170 * Initializes the driver interface, validates the configuration,
2171 * and sets driver parameters based on the configuration.
2172 * Flushes old stations, sets the channel, encryption,
2173 * beacons, and WDS links based on the configuration.
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002174 *
2175 * If interface setup requires more time, e.g., to perform HT co-ex scans, ACS,
2176 * or DFS operations, this function returns 0 before such operations have been
2177 * completed. The pending operations are registered into eloop and will be
2178 * completed from eloop callbacks. Those callbacks end up calling
2179 * hostapd_setup_interface_complete() once setup has been completed.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002180 */
2181int hostapd_setup_interface(struct hostapd_iface *iface)
2182{
2183 int ret;
2184
2185 ret = setup_interface(iface);
2186 if (ret) {
2187 wpa_printf(MSG_ERROR, "%s: Unable to setup interface.",
2188 iface->bss[0]->conf->iface);
2189 return -1;
2190 }
2191
2192 return 0;
2193}
2194
2195
2196/**
2197 * hostapd_alloc_bss_data - Allocate and initialize per-BSS data
2198 * @hapd_iface: Pointer to interface data
2199 * @conf: Pointer to per-interface configuration
2200 * @bss: Pointer to per-BSS configuration for this BSS
2201 * Returns: Pointer to allocated BSS data
2202 *
2203 * This function is used to allocate per-BSS data structure. This data will be
2204 * freed after hostapd_cleanup() is called for it during interface
2205 * deinitialization.
2206 */
2207struct hostapd_data *
2208hostapd_alloc_bss_data(struct hostapd_iface *hapd_iface,
2209 struct hostapd_config *conf,
2210 struct hostapd_bss_config *bss)
2211{
2212 struct hostapd_data *hapd;
2213
2214 hapd = os_zalloc(sizeof(*hapd));
2215 if (hapd == NULL)
2216 return NULL;
2217
2218 hapd->new_assoc_sta_cb = hostapd_new_assoc_sta;
2219 hapd->iconf = conf;
2220 hapd->conf = bss;
2221 hapd->iface = hapd_iface;
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08002222 if (conf)
2223 hapd->driver = conf->driver;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002224 hapd->ctrl_sock = -1;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08002225 dl_list_init(&hapd->ctrl_dst);
Dmitry Shmidt7d175302016-09-06 13:11:34 -07002226 dl_list_init(&hapd->nr_db);
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08002227 hapd->dhcp_sock = -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002228#ifdef CONFIG_IEEE80211R_AP
2229 dl_list_init(&hapd->l2_queue);
2230 dl_list_init(&hapd->l2_oui_queue);
2231#endif /* CONFIG_IEEE80211R_AP */
Hai Shalom021b0b52019-04-10 11:17:58 -07002232#ifdef CONFIG_SAE
2233 dl_list_init(&hapd->sae_commit_queue);
2234#endif /* CONFIG_SAE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002235
2236 return hapd;
2237}
2238
2239
Dmitry Shmidt54605472013-11-08 11:10:19 -08002240static void hostapd_bss_deinit(struct hostapd_data *hapd)
2241{
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07002242 if (!hapd)
2243 return;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002244 wpa_printf(MSG_DEBUG, "%s: deinit bss %s", __func__,
Hai Shalom021b0b52019-04-10 11:17:58 -07002245 hapd->conf ? hapd->conf->iface : "N/A");
Dmitry Shmidt71757432014-06-02 13:50:35 -07002246 hostapd_bss_deinit_no_free(hapd);
Dmitry Shmidtf73259c2015-03-17 11:00:54 -07002247 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
Hai Shalomc3565922019-10-28 11:58:20 -07002248#ifdef CONFIG_SQLITE
2249 if (hapd->rad_attr_db) {
2250 sqlite3_close(hapd->rad_attr_db);
2251 hapd->rad_attr_db = NULL;
2252 }
2253#endif /* CONFIG_SQLITE */
Dmitry Shmidt54605472013-11-08 11:10:19 -08002254 hostapd_cleanup(hapd);
2255}
2256
2257
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002258void hostapd_interface_deinit(struct hostapd_iface *iface)
2259{
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002260 int j;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002261
Dmitry Shmidt54605472013-11-08 11:10:19 -08002262 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002263 if (iface == NULL)
2264 return;
2265
Dmitry Shmidtf73259c2015-03-17 11:00:54 -07002266 hostapd_set_state(iface, HAPD_IFACE_DISABLED);
2267
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002268 eloop_cancel_timeout(channel_list_update_timeout, iface, NULL);
2269 iface->wait_channel_update = 0;
2270
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002271#ifdef CONFIG_FST
2272 if (iface->fst) {
2273 fst_detach(iface->fst);
2274 iface->fst = NULL;
2275 }
2276#endif /* CONFIG_FST */
2277
Hai Shalom021b0b52019-04-10 11:17:58 -07002278 for (j = (int) iface->num_bss - 1; j >= 0; j--) {
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07002279 if (!iface->bss)
2280 break;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002281 hostapd_bss_deinit(iface->bss[j]);
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07002282 }
Roshan Pius3a1667e2018-07-03 15:17:14 -07002283
2284#ifdef CONFIG_IEEE80211N
2285#ifdef NEED_AP_MLME
2286 hostapd_stop_setup_timers(iface);
2287 eloop_cancel_timeout(ap_ht2040_timeout, iface, NULL);
2288#endif /* NEED_AP_MLME */
2289#endif /* CONFIG_IEEE80211N */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002290}
2291
2292
2293void hostapd_interface_free(struct hostapd_iface *iface)
2294{
2295 size_t j;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002296 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
2297 for (j = 0; j < iface->num_bss; j++) {
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07002298 if (!iface->bss)
2299 break;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002300 wpa_printf(MSG_DEBUG, "%s: free hapd %p",
2301 __func__, iface->bss[j]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002302 os_free(iface->bss[j]);
Dmitry Shmidt54605472013-11-08 11:10:19 -08002303 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002304 hostapd_cleanup_iface(iface);
2305}
2306
2307
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07002308struct hostapd_iface * hostapd_alloc_iface(void)
2309{
2310 struct hostapd_iface *hapd_iface;
2311
2312 hapd_iface = os_zalloc(sizeof(*hapd_iface));
2313 if (!hapd_iface)
2314 return NULL;
2315
2316 dl_list_init(&hapd_iface->sta_seen);
2317
2318 return hapd_iface;
2319}
2320
2321
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002322/**
2323 * hostapd_init - Allocate and initialize per-interface data
2324 * @config_file: Path to the configuration file
2325 * Returns: Pointer to the allocated interface data or %NULL on failure
2326 *
2327 * This function is used to allocate main data structures for per-interface
2328 * data. The allocated data buffer will be freed by calling
2329 * hostapd_cleanup_iface().
2330 */
2331struct hostapd_iface * hostapd_init(struct hapd_interfaces *interfaces,
2332 const char *config_file)
2333{
2334 struct hostapd_iface *hapd_iface = NULL;
2335 struct hostapd_config *conf = NULL;
2336 struct hostapd_data *hapd;
2337 size_t i;
2338
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07002339 hapd_iface = hostapd_alloc_iface();
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002340 if (hapd_iface == NULL)
2341 goto fail;
2342
2343 hapd_iface->config_fname = os_strdup(config_file);
2344 if (hapd_iface->config_fname == NULL)
2345 goto fail;
2346
2347 conf = interfaces->config_read_cb(hapd_iface->config_fname);
2348 if (conf == NULL)
2349 goto fail;
2350 hapd_iface->conf = conf;
2351
2352 hapd_iface->num_bss = conf->num_bss;
2353 hapd_iface->bss = os_calloc(conf->num_bss,
2354 sizeof(struct hostapd_data *));
2355 if (hapd_iface->bss == NULL)
2356 goto fail;
2357
2358 for (i = 0; i < conf->num_bss; i++) {
2359 hapd = hapd_iface->bss[i] =
2360 hostapd_alloc_bss_data(hapd_iface, conf,
2361 conf->bss[i]);
2362 if (hapd == NULL)
2363 goto fail;
2364 hapd->msg_ctx = hapd;
2365 }
2366
2367 return hapd_iface;
2368
2369fail:
2370 wpa_printf(MSG_ERROR, "Failed to set up interface with %s",
2371 config_file);
2372 if (conf)
2373 hostapd_config_free(conf);
2374 if (hapd_iface) {
2375 os_free(hapd_iface->config_fname);
2376 os_free(hapd_iface->bss);
Dmitry Shmidt54605472013-11-08 11:10:19 -08002377 wpa_printf(MSG_DEBUG, "%s: free iface %p",
2378 __func__, hapd_iface);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002379 os_free(hapd_iface);
2380 }
2381 return NULL;
2382}
2383
2384
2385static int ifname_in_use(struct hapd_interfaces *interfaces, const char *ifname)
2386{
2387 size_t i, j;
2388
2389 for (i = 0; i < interfaces->count; i++) {
2390 struct hostapd_iface *iface = interfaces->iface[i];
2391 for (j = 0; j < iface->num_bss; j++) {
2392 struct hostapd_data *hapd = iface->bss[j];
2393 if (os_strcmp(ifname, hapd->conf->iface) == 0)
2394 return 1;
2395 }
2396 }
2397
2398 return 0;
2399}
2400
2401
2402/**
2403 * hostapd_interface_init_bss - Read configuration file and init BSS data
2404 *
2405 * This function is used to parse configuration file for a BSS. This BSS is
2406 * added to an existing interface sharing the same radio (if any) or a new
2407 * interface is created if this is the first interface on a radio. This
2408 * allocate memory for the BSS. No actual driver operations are started.
2409 *
2410 * This is similar to hostapd_interface_init(), but for a case where the
2411 * configuration is used to add a single BSS instead of all BSSes for a radio.
2412 */
2413struct hostapd_iface *
2414hostapd_interface_init_bss(struct hapd_interfaces *interfaces, const char *phy,
2415 const char *config_fname, int debug)
2416{
2417 struct hostapd_iface *new_iface = NULL, *iface = NULL;
2418 struct hostapd_data *hapd;
2419 int k;
2420 size_t i, bss_idx;
2421
2422 if (!phy || !*phy)
2423 return NULL;
2424
2425 for (i = 0; i < interfaces->count; i++) {
2426 if (os_strcmp(interfaces->iface[i]->phy, phy) == 0) {
2427 iface = interfaces->iface[i];
2428 break;
2429 }
2430 }
2431
2432 wpa_printf(MSG_INFO, "Configuration file: %s (phy %s)%s",
2433 config_fname, phy, iface ? "" : " --> new PHY");
2434 if (iface) {
2435 struct hostapd_config *conf;
2436 struct hostapd_bss_config **tmp_conf;
2437 struct hostapd_data **tmp_bss;
2438 struct hostapd_bss_config *bss;
2439 const char *ifname;
2440
2441 /* Add new BSS to existing iface */
2442 conf = interfaces->config_read_cb(config_fname);
2443 if (conf == NULL)
2444 return NULL;
2445 if (conf->num_bss > 1) {
2446 wpa_printf(MSG_ERROR, "Multiple BSSes specified in BSS-config");
2447 hostapd_config_free(conf);
2448 return NULL;
2449 }
2450
2451 ifname = conf->bss[0]->iface;
2452 if (ifname[0] != '\0' && ifname_in_use(interfaces, ifname)) {
2453 wpa_printf(MSG_ERROR,
2454 "Interface name %s already in use", ifname);
2455 hostapd_config_free(conf);
2456 return NULL;
2457 }
2458
2459 tmp_conf = os_realloc_array(
2460 iface->conf->bss, iface->conf->num_bss + 1,
2461 sizeof(struct hostapd_bss_config *));
2462 tmp_bss = os_realloc_array(iface->bss, iface->num_bss + 1,
2463 sizeof(struct hostapd_data *));
2464 if (tmp_bss)
2465 iface->bss = tmp_bss;
2466 if (tmp_conf) {
2467 iface->conf->bss = tmp_conf;
2468 iface->conf->last_bss = tmp_conf[0];
2469 }
2470 if (tmp_bss == NULL || tmp_conf == NULL) {
2471 hostapd_config_free(conf);
2472 return NULL;
2473 }
2474 bss = iface->conf->bss[iface->conf->num_bss] = conf->bss[0];
2475 iface->conf->num_bss++;
2476
2477 hapd = hostapd_alloc_bss_data(iface, iface->conf, bss);
2478 if (hapd == NULL) {
2479 iface->conf->num_bss--;
2480 hostapd_config_free(conf);
2481 return NULL;
2482 }
2483 iface->conf->last_bss = bss;
2484 iface->bss[iface->num_bss] = hapd;
2485 hapd->msg_ctx = hapd;
2486
2487 bss_idx = iface->num_bss++;
2488 conf->num_bss--;
2489 conf->bss[0] = NULL;
2490 hostapd_config_free(conf);
2491 } else {
2492 /* Add a new iface with the first BSS */
2493 new_iface = iface = hostapd_init(interfaces, config_fname);
2494 if (!iface)
2495 return NULL;
2496 os_strlcpy(iface->phy, phy, sizeof(iface->phy));
2497 iface->interfaces = interfaces;
2498 bss_idx = 0;
2499 }
2500
2501 for (k = 0; k < debug; k++) {
2502 if (iface->bss[bss_idx]->conf->logger_stdout_level > 0)
2503 iface->bss[bss_idx]->conf->logger_stdout_level--;
2504 }
2505
2506 if (iface->conf->bss[bss_idx]->iface[0] == '\0' &&
2507 !hostapd_drv_none(iface->bss[bss_idx])) {
2508 wpa_printf(MSG_ERROR, "Interface name not specified in %s",
2509 config_fname);
2510 if (new_iface)
2511 hostapd_interface_deinit_free(new_iface);
2512 return NULL;
2513 }
2514
2515 return iface;
2516}
2517
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002518
2519void hostapd_interface_deinit_free(struct hostapd_iface *iface)
2520{
2521 const struct wpa_driver_ops *driver;
2522 void *drv_priv;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002523
2524 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002525 if (iface == NULL)
2526 return;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002527 wpa_printf(MSG_DEBUG, "%s: num_bss=%u conf->num_bss=%u",
2528 __func__, (unsigned int) iface->num_bss,
2529 (unsigned int) iface->conf->num_bss);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002530 driver = iface->bss[0]->driver;
2531 drv_priv = iface->bss[0]->drv_priv;
2532 hostapd_interface_deinit(iface);
Dmitry Shmidt54605472013-11-08 11:10:19 -08002533 wpa_printf(MSG_DEBUG, "%s: driver=%p drv_priv=%p -> hapd_deinit",
2534 __func__, driver, drv_priv);
Dmitry Shmidt71757432014-06-02 13:50:35 -07002535 if (driver && driver->hapd_deinit && drv_priv) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002536 driver->hapd_deinit(drv_priv);
Dmitry Shmidt71757432014-06-02 13:50:35 -07002537 iface->bss[0]->drv_priv = NULL;
2538 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002539 hostapd_interface_free(iface);
2540}
2541
2542
Dmitry Shmidt15907092014-03-25 10:42:57 -07002543static void hostapd_deinit_driver(const struct wpa_driver_ops *driver,
2544 void *drv_priv,
2545 struct hostapd_iface *hapd_iface)
2546{
2547 size_t j;
2548
2549 wpa_printf(MSG_DEBUG, "%s: driver=%p drv_priv=%p -> hapd_deinit",
2550 __func__, driver, drv_priv);
2551 if (driver && driver->hapd_deinit && drv_priv) {
2552 driver->hapd_deinit(drv_priv);
2553 for (j = 0; j < hapd_iface->num_bss; j++) {
2554 wpa_printf(MSG_DEBUG, "%s:bss[%d]->drv_priv=%p",
2555 __func__, (int) j,
2556 hapd_iface->bss[j]->drv_priv);
Hai Shalom1dc4d202019-04-29 16:22:27 -07002557 if (hapd_iface->bss[j]->drv_priv == drv_priv) {
Dmitry Shmidt15907092014-03-25 10:42:57 -07002558 hapd_iface->bss[j]->drv_priv = NULL;
Hai Shalom1dc4d202019-04-29 16:22:27 -07002559 hapd_iface->extended_capa = NULL;
2560 hapd_iface->extended_capa_mask = NULL;
2561 hapd_iface->extended_capa_len = 0;
2562 }
Dmitry Shmidt15907092014-03-25 10:42:57 -07002563 }
2564 }
2565}
2566
2567
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002568int hostapd_enable_iface(struct hostapd_iface *hapd_iface)
2569{
Dmitry Shmidt71757432014-06-02 13:50:35 -07002570 size_t j;
2571
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002572 if (hapd_iface->bss[0]->drv_priv != NULL) {
2573 wpa_printf(MSG_ERROR, "Interface %s already enabled",
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002574 hapd_iface->conf->bss[0]->iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002575 return -1;
2576 }
2577
2578 wpa_printf(MSG_DEBUG, "Enable interface %s",
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002579 hapd_iface->conf->bss[0]->iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002580
Dmitry Shmidt71757432014-06-02 13:50:35 -07002581 for (j = 0; j < hapd_iface->num_bss; j++)
2582 hostapd_set_security_params(hapd_iface->conf->bss[j], 1);
Dmitry Shmidt344abd32014-01-14 13:17:00 -08002583 if (hostapd_config_check(hapd_iface->conf, 1) < 0) {
2584 wpa_printf(MSG_INFO, "Invalid configuration - cannot enable");
2585 return -1;
2586 }
2587
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002588 if (hapd_iface->interfaces == NULL ||
2589 hapd_iface->interfaces->driver_init == NULL ||
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002590 hapd_iface->interfaces->driver_init(hapd_iface))
2591 return -1;
2592
2593 if (hostapd_setup_interface(hapd_iface)) {
Dmitry Shmidt15907092014-03-25 10:42:57 -07002594 hostapd_deinit_driver(hapd_iface->bss[0]->driver,
2595 hapd_iface->bss[0]->drv_priv,
2596 hapd_iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002597 return -1;
2598 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002599
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002600 return 0;
2601}
2602
2603
2604int hostapd_reload_iface(struct hostapd_iface *hapd_iface)
2605{
2606 size_t j;
2607
2608 wpa_printf(MSG_DEBUG, "Reload interface %s",
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002609 hapd_iface->conf->bss[0]->iface);
2610 for (j = 0; j < hapd_iface->num_bss; j++)
Dmitry Shmidt71757432014-06-02 13:50:35 -07002611 hostapd_set_security_params(hapd_iface->conf->bss[j], 1);
Dmitry Shmidt344abd32014-01-14 13:17:00 -08002612 if (hostapd_config_check(hapd_iface->conf, 1) < 0) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002613 wpa_printf(MSG_ERROR, "Updated configuration is invalid");
2614 return -1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002615 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002616 hostapd_clear_old(hapd_iface);
2617 for (j = 0; j < hapd_iface->num_bss; j++)
2618 hostapd_reload_bss(hapd_iface->bss[j]);
2619
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002620 return 0;
2621}
2622
2623
2624int hostapd_disable_iface(struct hostapd_iface *hapd_iface)
2625{
2626 size_t j;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002627 const struct wpa_driver_ops *driver;
2628 void *drv_priv;
2629
2630 if (hapd_iface == NULL)
2631 return -1;
Dmitry Shmidt71757432014-06-02 13:50:35 -07002632
2633 if (hapd_iface->bss[0]->drv_priv == NULL) {
2634 wpa_printf(MSG_INFO, "Interface %s already disabled",
2635 hapd_iface->conf->bss[0]->iface);
2636 return -1;
2637 }
2638
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002639 wpa_msg(hapd_iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002640 driver = hapd_iface->bss[0]->driver;
2641 drv_priv = hapd_iface->bss[0]->drv_priv;
2642
Dmitry Shmidta38abf92014-03-06 13:38:44 -08002643 hapd_iface->driver_ap_teardown =
2644 !!(hapd_iface->drv_flags &
2645 WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
2646
Hai Shalom39ba6fc2019-01-22 12:40:38 -08002647#ifdef NEED_AP_MLME
2648 for (j = 0; j < hapd_iface->num_bss; j++)
2649 hostapd_cleanup_cs_params(hapd_iface->bss[j]);
2650#endif /* NEED_AP_MLME */
2651
Dmitry Shmidta38abf92014-03-06 13:38:44 -08002652 /* same as hostapd_interface_deinit without deinitializing ctrl-iface */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002653 for (j = 0; j < hapd_iface->num_bss; j++) {
2654 struct hostapd_data *hapd = hapd_iface->bss[j];
Dmitry Shmidt71757432014-06-02 13:50:35 -07002655 hostapd_bss_deinit_no_free(hapd);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002656 hostapd_free_hapd_data(hapd);
2657 }
2658
Dmitry Shmidt15907092014-03-25 10:42:57 -07002659 hostapd_deinit_driver(driver, drv_priv, hapd_iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002660
2661 /* From hostapd_cleanup_iface: These were initialized in
2662 * hostapd_setup_interface and hostapd_setup_interface_complete
2663 */
2664 hostapd_cleanup_iface_partial(hapd_iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002665
Dmitry Shmidt56052862013-10-04 10:23:25 -07002666 wpa_printf(MSG_DEBUG, "Interface %s disabled",
2667 hapd_iface->bss[0]->conf->iface);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002668 hostapd_set_state(hapd_iface, HAPD_IFACE_DISABLED);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002669 return 0;
2670}
2671
2672
2673static struct hostapd_iface *
2674hostapd_iface_alloc(struct hapd_interfaces *interfaces)
2675{
2676 struct hostapd_iface **iface, *hapd_iface;
2677
2678 iface = os_realloc_array(interfaces->iface, interfaces->count + 1,
2679 sizeof(struct hostapd_iface *));
2680 if (iface == NULL)
2681 return NULL;
2682 interfaces->iface = iface;
2683 hapd_iface = interfaces->iface[interfaces->count] =
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07002684 hostapd_alloc_iface();
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002685 if (hapd_iface == NULL) {
2686 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory for "
2687 "the interface", __func__);
2688 return NULL;
2689 }
2690 interfaces->count++;
2691 hapd_iface->interfaces = interfaces;
2692
2693 return hapd_iface;
2694}
2695
2696
2697static struct hostapd_config *
2698hostapd_config_alloc(struct hapd_interfaces *interfaces, const char *ifname,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002699 const char *ctrl_iface, const char *driver)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002700{
2701 struct hostapd_bss_config *bss;
2702 struct hostapd_config *conf;
2703
2704 /* Allocates memory for bss and conf */
2705 conf = hostapd_config_defaults();
2706 if (conf == NULL) {
2707 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory for "
2708 "configuration", __func__);
Hai Shalom74f70d42019-02-11 14:42:39 -08002709 return NULL;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002710 }
2711
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002712 if (driver) {
2713 int j;
2714
2715 for (j = 0; wpa_drivers[j]; j++) {
2716 if (os_strcmp(driver, wpa_drivers[j]->name) == 0) {
2717 conf->driver = wpa_drivers[j];
2718 goto skip;
2719 }
2720 }
2721
2722 wpa_printf(MSG_ERROR,
2723 "Invalid/unknown driver '%s' - registering the default driver",
2724 driver);
2725 }
2726
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002727 conf->driver = wpa_drivers[0];
2728 if (conf->driver == NULL) {
2729 wpa_printf(MSG_ERROR, "No driver wrappers registered!");
2730 hostapd_config_free(conf);
2731 return NULL;
2732 }
2733
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002734skip:
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002735 bss = conf->last_bss = conf->bss[0];
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002736
2737 os_strlcpy(bss->iface, ifname, sizeof(bss->iface));
2738 bss->ctrl_interface = os_strdup(ctrl_iface);
2739 if (bss->ctrl_interface == NULL) {
2740 hostapd_config_free(conf);
2741 return NULL;
2742 }
2743
2744 /* Reading configuration file skipped, will be done in SET!
2745 * From reading the configuration till the end has to be done in
2746 * SET
2747 */
2748 return conf;
2749}
2750
2751
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002752static int hostapd_data_alloc(struct hostapd_iface *hapd_iface,
2753 struct hostapd_config *conf)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002754{
2755 size_t i;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002756 struct hostapd_data *hapd;
2757
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002758 hapd_iface->bss = os_calloc(conf->num_bss,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002759 sizeof(struct hostapd_data *));
2760 if (hapd_iface->bss == NULL)
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002761 return -1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002762
2763 for (i = 0; i < conf->num_bss; i++) {
2764 hapd = hapd_iface->bss[i] =
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002765 hostapd_alloc_bss_data(hapd_iface, conf, conf->bss[i]);
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002766 if (hapd == NULL) {
2767 while (i > 0) {
2768 i--;
2769 os_free(hapd_iface->bss[i]);
2770 hapd_iface->bss[i] = NULL;
2771 }
2772 os_free(hapd_iface->bss);
2773 hapd_iface->bss = NULL;
2774 return -1;
2775 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002776 hapd->msg_ctx = hapd;
2777 }
2778
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002779 hapd_iface->conf = conf;
2780 hapd_iface->num_bss = conf->num_bss;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002781
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002782 return 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002783}
2784
2785
2786int hostapd_add_iface(struct hapd_interfaces *interfaces, char *buf)
2787{
2788 struct hostapd_config *conf = NULL;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002789 struct hostapd_iface *hapd_iface = NULL, *new_iface = NULL;
2790 struct hostapd_data *hapd;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002791 char *ptr;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002792 size_t i, j;
2793 const char *conf_file = NULL, *phy_name = NULL;
2794
2795 if (os_strncmp(buf, "bss_config=", 11) == 0) {
2796 char *pos;
2797 phy_name = buf + 11;
2798 pos = os_strchr(phy_name, ':');
2799 if (!pos)
2800 return -1;
2801 *pos++ = '\0';
2802 conf_file = pos;
2803 if (!os_strlen(conf_file))
2804 return -1;
2805
2806 hapd_iface = hostapd_interface_init_bss(interfaces, phy_name,
2807 conf_file, 0);
2808 if (!hapd_iface)
2809 return -1;
2810 for (j = 0; j < interfaces->count; j++) {
2811 if (interfaces->iface[j] == hapd_iface)
2812 break;
2813 }
2814 if (j == interfaces->count) {
2815 struct hostapd_iface **tmp;
2816 tmp = os_realloc_array(interfaces->iface,
2817 interfaces->count + 1,
2818 sizeof(struct hostapd_iface *));
2819 if (!tmp) {
2820 hostapd_interface_deinit_free(hapd_iface);
2821 return -1;
2822 }
2823 interfaces->iface = tmp;
2824 interfaces->iface[interfaces->count++] = hapd_iface;
2825 new_iface = hapd_iface;
2826 }
2827
2828 if (new_iface) {
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002829 if (interfaces->driver_init(hapd_iface))
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002830 goto fail;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002831
2832 if (hostapd_setup_interface(hapd_iface)) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002833 hostapd_deinit_driver(
2834 hapd_iface->bss[0]->driver,
2835 hapd_iface->bss[0]->drv_priv,
2836 hapd_iface);
2837 goto fail;
2838 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002839 } else {
2840 /* Assign new BSS with bss[0]'s driver info */
2841 hapd = hapd_iface->bss[hapd_iface->num_bss - 1];
2842 hapd->driver = hapd_iface->bss[0]->driver;
2843 hapd->drv_priv = hapd_iface->bss[0]->drv_priv;
2844 os_memcpy(hapd->own_addr, hapd_iface->bss[0]->own_addr,
2845 ETH_ALEN);
2846
2847 if (start_ctrl_iface_bss(hapd) < 0 ||
Dmitry Shmidt54605472013-11-08 11:10:19 -08002848 (hapd_iface->state == HAPD_IFACE_ENABLED &&
2849 hostapd_setup_bss(hapd, -1))) {
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002850 hostapd_cleanup(hapd);
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08002851 hapd_iface->bss[hapd_iface->num_bss - 1] = NULL;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002852 hapd_iface->conf->num_bss--;
2853 hapd_iface->num_bss--;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002854 wpa_printf(MSG_DEBUG, "%s: free hapd %p %s",
2855 __func__, hapd, hapd->conf->iface);
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002856 hostapd_config_free_bss(hapd->conf);
2857 hapd->conf = NULL;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002858 os_free(hapd);
2859 return -1;
2860 }
2861 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002862 hostapd_owe_update_trans(hapd_iface);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002863 return 0;
2864 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002865
2866 ptr = os_strchr(buf, ' ');
2867 if (ptr == NULL)
2868 return -1;
2869 *ptr++ = '\0';
2870
Dmitry Shmidt56052862013-10-04 10:23:25 -07002871 if (os_strncmp(ptr, "config=", 7) == 0)
2872 conf_file = ptr + 7;
2873
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002874 for (i = 0; i < interfaces->count; i++) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002875 if (!os_strcmp(interfaces->iface[i]->conf->bss[0]->iface,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002876 buf)) {
2877 wpa_printf(MSG_INFO, "Cannot add interface - it "
2878 "already exists");
2879 return -1;
2880 }
2881 }
2882
2883 hapd_iface = hostapd_iface_alloc(interfaces);
2884 if (hapd_iface == NULL) {
2885 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
2886 "for interface", __func__);
2887 goto fail;
2888 }
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002889 new_iface = hapd_iface;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002890
Dmitry Shmidt56052862013-10-04 10:23:25 -07002891 if (conf_file && interfaces->config_read_cb) {
2892 conf = interfaces->config_read_cb(conf_file);
2893 if (conf && conf->bss)
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002894 os_strlcpy(conf->bss[0]->iface, buf,
2895 sizeof(conf->bss[0]->iface));
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002896 } else {
2897 char *driver = os_strchr(ptr, ' ');
2898
2899 if (driver)
2900 *driver++ = '\0';
2901 conf = hostapd_config_alloc(interfaces, buf, ptr, driver);
2902 }
2903
Dmitry Shmidt56052862013-10-04 10:23:25 -07002904 if (conf == NULL || conf->bss == NULL) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002905 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
2906 "for configuration", __func__);
2907 goto fail;
2908 }
2909
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002910 if (hostapd_data_alloc(hapd_iface, conf) < 0) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002911 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
2912 "for hostapd", __func__);
2913 goto fail;
2914 }
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002915 conf = NULL;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002916
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002917 if (start_ctrl_iface(hapd_iface) < 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002918 goto fail;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002919
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002920 wpa_printf(MSG_INFO, "Add interface '%s'",
2921 hapd_iface->conf->bss[0]->iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002922
2923 return 0;
2924
2925fail:
2926 if (conf)
2927 hostapd_config_free(conf);
2928 if (hapd_iface) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002929 if (hapd_iface->bss) {
Dmitry Shmidt54605472013-11-08 11:10:19 -08002930 for (i = 0; i < hapd_iface->num_bss; i++) {
2931 hapd = hapd_iface->bss[i];
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08002932 if (!hapd)
2933 continue;
2934 if (hapd_iface->interfaces &&
Dmitry Shmidt54605472013-11-08 11:10:19 -08002935 hapd_iface->interfaces->ctrl_iface_deinit)
2936 hapd_iface->interfaces->
2937 ctrl_iface_deinit(hapd);
2938 wpa_printf(MSG_DEBUG, "%s: free hapd %p (%s)",
2939 __func__, hapd_iface->bss[i],
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08002940 hapd->conf->iface);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002941 hostapd_cleanup(hapd);
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08002942 os_free(hapd);
2943 hapd_iface->bss[i] = NULL;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002944 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002945 os_free(hapd_iface->bss);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002946 hapd_iface->bss = NULL;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002947 }
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002948 if (new_iface) {
2949 interfaces->count--;
2950 interfaces->iface[interfaces->count] = NULL;
2951 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002952 hostapd_cleanup_iface(hapd_iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002953 }
2954 return -1;
2955}
2956
2957
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002958static int hostapd_remove_bss(struct hostapd_iface *iface, unsigned int idx)
2959{
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002960 size_t i;
2961
Dmitry Shmidt54605472013-11-08 11:10:19 -08002962 wpa_printf(MSG_INFO, "Remove BSS '%s'", iface->conf->bss[idx]->iface);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002963
Dmitry Shmidt54605472013-11-08 11:10:19 -08002964 /* Remove hostapd_data only if it has already been initialized */
2965 if (idx < iface->num_bss) {
2966 struct hostapd_data *hapd = iface->bss[idx];
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002967
Dmitry Shmidt54605472013-11-08 11:10:19 -08002968 hostapd_bss_deinit(hapd);
2969 wpa_printf(MSG_DEBUG, "%s: free hapd %p (%s)",
2970 __func__, hapd, hapd->conf->iface);
2971 hostapd_config_free_bss(hapd->conf);
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002972 hapd->conf = NULL;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002973 os_free(hapd);
2974
2975 iface->num_bss--;
2976
2977 for (i = idx; i < iface->num_bss; i++)
2978 iface->bss[i] = iface->bss[i + 1];
2979 } else {
2980 hostapd_config_free_bss(iface->conf->bss[idx]);
2981 iface->conf->bss[idx] = NULL;
2982 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002983
2984 iface->conf->num_bss--;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002985 for (i = idx; i < iface->conf->num_bss; i++)
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002986 iface->conf->bss[i] = iface->conf->bss[i + 1];
2987
2988 return 0;
2989}
2990
2991
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002992int hostapd_remove_iface(struct hapd_interfaces *interfaces, char *buf)
2993{
2994 struct hostapd_iface *hapd_iface;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002995 size_t i, j, k = 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002996
2997 for (i = 0; i < interfaces->count; i++) {
2998 hapd_iface = interfaces->iface[i];
2999 if (hapd_iface == NULL)
3000 return -1;
Dmitry Shmidt54605472013-11-08 11:10:19 -08003001 if (!os_strcmp(hapd_iface->conf->bss[0]->iface, buf)) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003002 wpa_printf(MSG_INFO, "Remove interface '%s'", buf);
Dmitry Shmidta38abf92014-03-06 13:38:44 -08003003 hapd_iface->driver_ap_teardown =
3004 !!(hapd_iface->drv_flags &
3005 WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
3006
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003007 hostapd_interface_deinit_free(hapd_iface);
3008 k = i;
3009 while (k < (interfaces->count - 1)) {
3010 interfaces->iface[k] =
3011 interfaces->iface[k + 1];
3012 k++;
3013 }
3014 interfaces->count--;
3015 return 0;
3016 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003017
3018 for (j = 0; j < hapd_iface->conf->num_bss; j++) {
Dmitry Shmidta38abf92014-03-06 13:38:44 -08003019 if (!os_strcmp(hapd_iface->conf->bss[j]->iface, buf)) {
3020 hapd_iface->driver_ap_teardown =
3021 !(hapd_iface->drv_flags &
3022 WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003023 return hostapd_remove_bss(hapd_iface, j);
Dmitry Shmidta38abf92014-03-06 13:38:44 -08003024 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003025 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003026 }
3027 return -1;
3028}
3029
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003030
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003031/**
3032 * hostapd_new_assoc_sta - Notify that a new station associated with the AP
3033 * @hapd: Pointer to BSS data
3034 * @sta: Pointer to the associated STA data
3035 * @reassoc: 1 to indicate this was a re-association; 0 = first association
3036 *
3037 * This function will be called whenever a station associates with the AP. It
3038 * can be called from ieee802_11.c for drivers that export MLME to hostapd and
3039 * from drv_callbacks.c based on driver events for drivers that take care of
3040 * management frames (IEEE 802.11 authentication and association) internally.
3041 */
3042void hostapd_new_assoc_sta(struct hostapd_data *hapd, struct sta_info *sta,
3043 int reassoc)
3044{
3045 if (hapd->tkip_countermeasures) {
3046 hostapd_drv_sta_deauth(hapd, sta->addr,
3047 WLAN_REASON_MICHAEL_MIC_FAILURE);
3048 return;
3049 }
3050
3051 hostapd_prune_associations(hapd, sta->addr);
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -08003052 ap_sta_clear_disconnect_timeouts(hapd, sta);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003053
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003054#ifdef CONFIG_P2P
3055 if (sta->p2p_ie == NULL && !sta->no_p2p_set) {
3056 sta->no_p2p_set = 1;
3057 hapd->num_sta_no_p2p++;
3058 if (hapd->num_sta_no_p2p == 1)
3059 hostapd_p2p_non_p2p_sta_connected(hapd);
3060 }
3061#endif /* CONFIG_P2P */
3062
Hai Shalom81f62d82019-07-22 12:10:00 -07003063 airtime_policy_new_sta(hapd, sta);
3064
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003065 /* Start accounting here, if IEEE 802.1X and WPA are not used.
3066 * IEEE 802.1X/WPA code will start accounting after the station has
3067 * been authorized. */
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -08003068 if (!hapd->conf->ieee802_1x && !hapd->conf->wpa && !hapd->conf->osen) {
3069 ap_sta_set_authorized(hapd, sta, 1);
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08003070 os_get_reltime(&sta->connected_time);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003071 accounting_sta_start(hapd, sta);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003072 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003073
3074 /* Start IEEE 802.1X authentication process for new stations */
3075 ieee802_1x_new_station(hapd, sta);
3076 if (reassoc) {
3077 if (sta->auth_alg != WLAN_AUTH_FT &&
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003078 sta->auth_alg != WLAN_AUTH_FILS_SK &&
3079 sta->auth_alg != WLAN_AUTH_FILS_SK_PFS &&
3080 sta->auth_alg != WLAN_AUTH_FILS_PK &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003081 !(sta->flags & (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS)))
3082 wpa_auth_sm_event(sta->wpa_sm, WPA_REAUTH);
3083 } else
3084 wpa_auth_sta_associated(hapd->wpa_auth, sta->wpa_sm);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003085
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08003086 if (hapd->iface->drv_flags & WPA_DRIVER_FLAGS_WIRED) {
3087 if (eloop_cancel_timeout(ap_handle_timer, hapd, sta) > 0) {
3088 wpa_printf(MSG_DEBUG,
3089 "%s: %s: canceled wired ap_handle_timer timeout for "
3090 MACSTR,
3091 hapd->conf->iface, __func__,
3092 MAC2STR(sta->addr));
3093 }
3094 } else if (!(hapd->iface->drv_flags &
3095 WPA_DRIVER_FLAGS_INACTIVITY_TIMER)) {
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -08003096 wpa_printf(MSG_DEBUG,
3097 "%s: %s: reschedule ap_handle_timer timeout for "
3098 MACSTR " (%d seconds - ap_max_inactivity)",
3099 hapd->conf->iface, __func__, MAC2STR(sta->addr),
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08003100 hapd->conf->ap_max_inactivity);
3101 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
3102 eloop_register_timeout(hapd->conf->ap_max_inactivity, 0,
3103 ap_handle_timer, hapd, sta);
3104 }
Hai Shalom81f62d82019-07-22 12:10:00 -07003105
3106#ifdef CONFIG_MACSEC
3107 if (hapd->conf->wpa_key_mgmt == WPA_KEY_MGMT_NONE &&
3108 hapd->conf->mka_psk_set)
3109 ieee802_1x_create_preshared_mka_hapd(hapd, sta);
3110 else
3111 ieee802_1x_alloc_kay_sm_hapd(hapd, sta);
3112#endif /* CONFIG_MACSEC */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003113}
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003114
3115
3116const char * hostapd_state_text(enum hostapd_iface_state s)
3117{
3118 switch (s) {
3119 case HAPD_IFACE_UNINITIALIZED:
3120 return "UNINITIALIZED";
3121 case HAPD_IFACE_DISABLED:
3122 return "DISABLED";
3123 case HAPD_IFACE_COUNTRY_UPDATE:
3124 return "COUNTRY_UPDATE";
3125 case HAPD_IFACE_ACS:
3126 return "ACS";
3127 case HAPD_IFACE_HT_SCAN:
3128 return "HT_SCAN";
3129 case HAPD_IFACE_DFS:
3130 return "DFS";
3131 case HAPD_IFACE_ENABLED:
3132 return "ENABLED";
3133 }
3134
3135 return "UNKNOWN";
3136}
3137
3138
3139void hostapd_set_state(struct hostapd_iface *iface, enum hostapd_iface_state s)
3140{
3141 wpa_printf(MSG_INFO, "%s: interface state %s->%s",
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07003142 iface->conf ? iface->conf->bss[0]->iface : "N/A",
3143 hostapd_state_text(iface->state), hostapd_state_text(s));
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003144 iface->state = s;
3145}
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003146
3147
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003148int hostapd_csa_in_progress(struct hostapd_iface *iface)
3149{
3150 unsigned int i;
3151
3152 for (i = 0; i < iface->num_bss; i++)
3153 if (iface->bss[i]->csa_in_progress)
3154 return 1;
3155 return 0;
3156}
3157
3158
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003159#ifdef NEED_AP_MLME
3160
3161static void free_beacon_data(struct beacon_data *beacon)
3162{
3163 os_free(beacon->head);
3164 beacon->head = NULL;
3165 os_free(beacon->tail);
3166 beacon->tail = NULL;
3167 os_free(beacon->probe_resp);
3168 beacon->probe_resp = NULL;
3169 os_free(beacon->beacon_ies);
3170 beacon->beacon_ies = NULL;
3171 os_free(beacon->proberesp_ies);
3172 beacon->proberesp_ies = NULL;
3173 os_free(beacon->assocresp_ies);
3174 beacon->assocresp_ies = NULL;
3175}
3176
3177
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003178static int hostapd_build_beacon_data(struct hostapd_data *hapd,
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003179 struct beacon_data *beacon)
3180{
3181 struct wpabuf *beacon_extra, *proberesp_extra, *assocresp_extra;
3182 struct wpa_driver_ap_params params;
3183 int ret;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003184
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08003185 os_memset(beacon, 0, sizeof(*beacon));
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003186 ret = ieee802_11_build_ap_params(hapd, &params);
3187 if (ret < 0)
3188 return ret;
3189
3190 ret = hostapd_build_ap_extra_ies(hapd, &beacon_extra,
3191 &proberesp_extra,
3192 &assocresp_extra);
3193 if (ret)
3194 goto free_ap_params;
3195
3196 ret = -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003197 beacon->head = os_memdup(params.head, params.head_len);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003198 if (!beacon->head)
3199 goto free_ap_extra_ies;
3200
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003201 beacon->head_len = params.head_len;
3202
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003203 beacon->tail = os_memdup(params.tail, params.tail_len);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003204 if (!beacon->tail)
3205 goto free_beacon;
3206
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003207 beacon->tail_len = params.tail_len;
3208
3209 if (params.proberesp != NULL) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003210 beacon->probe_resp = os_memdup(params.proberesp,
3211 params.proberesp_len);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003212 if (!beacon->probe_resp)
3213 goto free_beacon;
3214
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003215 beacon->probe_resp_len = params.proberesp_len;
3216 }
3217
3218 /* copy the extra ies */
3219 if (beacon_extra) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003220 beacon->beacon_ies = os_memdup(beacon_extra->buf,
3221 wpabuf_len(beacon_extra));
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003222 if (!beacon->beacon_ies)
3223 goto free_beacon;
3224
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003225 beacon->beacon_ies_len = wpabuf_len(beacon_extra);
3226 }
3227
3228 if (proberesp_extra) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003229 beacon->proberesp_ies = os_memdup(proberesp_extra->buf,
3230 wpabuf_len(proberesp_extra));
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003231 if (!beacon->proberesp_ies)
3232 goto free_beacon;
3233
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003234 beacon->proberesp_ies_len = wpabuf_len(proberesp_extra);
3235 }
3236
3237 if (assocresp_extra) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003238 beacon->assocresp_ies = os_memdup(assocresp_extra->buf,
3239 wpabuf_len(assocresp_extra));
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003240 if (!beacon->assocresp_ies)
3241 goto free_beacon;
3242
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003243 beacon->assocresp_ies_len = wpabuf_len(assocresp_extra);
3244 }
3245
3246 ret = 0;
3247free_beacon:
3248 /* if the function fails, the caller should not free beacon data */
3249 if (ret)
3250 free_beacon_data(beacon);
3251
3252free_ap_extra_ies:
3253 hostapd_free_ap_extra_ies(hapd, beacon_extra, proberesp_extra,
3254 assocresp_extra);
3255free_ap_params:
3256 ieee802_11_free_ap_params(&params);
3257 return ret;
3258}
3259
3260
3261/*
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003262 * TODO: This flow currently supports only changing channel and width within
3263 * the same hw_mode. Any other changes to MAC parameters or provided settings
3264 * are not supported.
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003265 */
3266static int hostapd_change_config_freq(struct hostapd_data *hapd,
3267 struct hostapd_config *conf,
3268 struct hostapd_freq_params *params,
3269 struct hostapd_freq_params *old_params)
3270{
3271 int channel;
Hai Shalom81f62d82019-07-22 12:10:00 -07003272 u8 seg0, seg1;
3273 struct hostapd_hw_modes *mode;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003274
3275 if (!params->channel) {
3276 /* check if the new channel is supported by hw */
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003277 params->channel = hostapd_hw_get_channel(hapd, params->freq);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003278 }
3279
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003280 channel = params->channel;
3281 if (!channel)
3282 return -1;
3283
Hai Shalom81f62d82019-07-22 12:10:00 -07003284 mode = hapd->iface->current_mode;
3285
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003286 /* if a pointer to old_params is provided we save previous state */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003287 if (old_params &&
3288 hostapd_set_freq_params(old_params, conf->hw_mode,
3289 hostapd_hw_get_freq(hapd, conf->channel),
Hai Shalomc3565922019-10-28 11:58:20 -07003290 conf->channel, conf->enable_edmg,
3291 conf->edmg_channel, conf->ieee80211n,
Hai Shalom81f62d82019-07-22 12:10:00 -07003292 conf->ieee80211ac, conf->ieee80211ax,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003293 conf->secondary_channel,
Hai Shalom81f62d82019-07-22 12:10:00 -07003294 hostapd_get_oper_chwidth(conf),
3295 hostapd_get_oper_centr_freq_seg0_idx(conf),
3296 hostapd_get_oper_centr_freq_seg1_idx(conf),
3297 conf->vht_capab,
3298 mode ? &mode->he_capab[IEEE80211_MODE_AP] :
3299 NULL))
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003300 return -1;
3301
3302 switch (params->bandwidth) {
3303 case 0:
3304 case 20:
3305 case 40:
Hai Shalom81f62d82019-07-22 12:10:00 -07003306 hostapd_set_oper_chwidth(conf, CHANWIDTH_USE_HT);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003307 break;
3308 case 80:
3309 if (params->center_freq2)
Hai Shalom81f62d82019-07-22 12:10:00 -07003310 hostapd_set_oper_chwidth(conf, CHANWIDTH_80P80MHZ);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003311 else
Hai Shalom81f62d82019-07-22 12:10:00 -07003312 hostapd_set_oper_chwidth(conf, CHANWIDTH_80MHZ);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003313 break;
3314 case 160:
Hai Shalom81f62d82019-07-22 12:10:00 -07003315 hostapd_set_oper_chwidth(conf, CHANWIDTH_160MHZ);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003316 break;
3317 default:
3318 return -1;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003319 }
3320
3321 conf->channel = channel;
3322 conf->ieee80211n = params->ht_enabled;
3323 conf->secondary_channel = params->sec_channel_offset;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003324 ieee80211_freq_to_chan(params->center_freq1,
Hai Shalom81f62d82019-07-22 12:10:00 -07003325 &seg0);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003326 ieee80211_freq_to_chan(params->center_freq2,
Hai Shalom81f62d82019-07-22 12:10:00 -07003327 &seg1);
3328 hostapd_set_oper_centr_freq_seg0_idx(conf, seg0);
3329 hostapd_set_oper_centr_freq_seg1_idx(conf, seg1);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003330
3331 /* TODO: maybe call here hostapd_config_check here? */
3332
3333 return 0;
3334}
3335
3336
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003337static int hostapd_fill_csa_settings(struct hostapd_data *hapd,
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003338 struct csa_settings *settings)
3339{
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003340 struct hostapd_iface *iface = hapd->iface;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003341 struct hostapd_freq_params old_freq;
3342 int ret;
Hai Shalom81f62d82019-07-22 12:10:00 -07003343 u8 chan, bandwidth;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003344
3345 os_memset(&old_freq, 0, sizeof(old_freq));
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003346 if (!iface || !iface->freq || hapd->csa_in_progress)
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003347 return -1;
3348
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003349 switch (settings->freq_params.bandwidth) {
3350 case 80:
3351 if (settings->freq_params.center_freq2)
Hai Shalom81f62d82019-07-22 12:10:00 -07003352 bandwidth = CHANWIDTH_80P80MHZ;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003353 else
Hai Shalom81f62d82019-07-22 12:10:00 -07003354 bandwidth = CHANWIDTH_80MHZ;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003355 break;
3356 case 160:
Hai Shalom81f62d82019-07-22 12:10:00 -07003357 bandwidth = CHANWIDTH_160MHZ;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003358 break;
3359 default:
Hai Shalom81f62d82019-07-22 12:10:00 -07003360 bandwidth = CHANWIDTH_USE_HT;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003361 break;
3362 }
3363
3364 if (ieee80211_freq_to_channel_ext(
3365 settings->freq_params.freq,
3366 settings->freq_params.sec_channel_offset,
Hai Shalom81f62d82019-07-22 12:10:00 -07003367 bandwidth,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003368 &hapd->iface->cs_oper_class,
3369 &chan) == NUM_HOSTAPD_MODES) {
3370 wpa_printf(MSG_DEBUG,
Hai Shalom81f62d82019-07-22 12:10:00 -07003371 "invalid frequency for channel switch (freq=%d, sec_channel_offset=%d, vht_enabled=%d, he_enabled=%d)",
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003372 settings->freq_params.freq,
3373 settings->freq_params.sec_channel_offset,
Hai Shalom81f62d82019-07-22 12:10:00 -07003374 settings->freq_params.vht_enabled,
3375 settings->freq_params.he_enabled);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003376 return -1;
3377 }
3378
3379 settings->freq_params.channel = chan;
3380
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003381 ret = hostapd_change_config_freq(iface->bss[0], iface->conf,
3382 &settings->freq_params,
3383 &old_freq);
3384 if (ret)
3385 return ret;
3386
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003387 ret = hostapd_build_beacon_data(hapd, &settings->beacon_after);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003388
3389 /* change back the configuration */
3390 hostapd_change_config_freq(iface->bss[0], iface->conf,
3391 &old_freq, NULL);
3392
3393 if (ret)
3394 return ret;
3395
3396 /* set channel switch parameters for csa ie */
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003397 hapd->cs_freq_params = settings->freq_params;
3398 hapd->cs_count = settings->cs_count;
3399 hapd->cs_block_tx = settings->block_tx;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003400
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003401 ret = hostapd_build_beacon_data(hapd, &settings->beacon_csa);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003402 if (ret) {
3403 free_beacon_data(&settings->beacon_after);
3404 return ret;
3405 }
3406
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003407 settings->counter_offset_beacon[0] = hapd->cs_c_off_beacon;
3408 settings->counter_offset_presp[0] = hapd->cs_c_off_proberesp;
3409 settings->counter_offset_beacon[1] = hapd->cs_c_off_ecsa_beacon;
3410 settings->counter_offset_presp[1] = hapd->cs_c_off_ecsa_proberesp;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003411
3412 return 0;
3413}
3414
3415
3416void hostapd_cleanup_cs_params(struct hostapd_data *hapd)
3417{
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003418 os_memset(&hapd->cs_freq_params, 0, sizeof(hapd->cs_freq_params));
3419 hapd->cs_count = 0;
3420 hapd->cs_block_tx = 0;
3421 hapd->cs_c_off_beacon = 0;
3422 hapd->cs_c_off_proberesp = 0;
3423 hapd->csa_in_progress = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003424 hapd->cs_c_off_ecsa_beacon = 0;
3425 hapd->cs_c_off_ecsa_proberesp = 0;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003426}
3427
3428
Roshan Pius3a1667e2018-07-03 15:17:14 -07003429void hostapd_chan_switch_vht_config(struct hostapd_data *hapd, int vht_enabled)
3430{
3431 if (vht_enabled)
3432 hapd->iconf->ch_switch_vht_config |= CH_SWITCH_VHT_ENABLED;
3433 else
3434 hapd->iconf->ch_switch_vht_config |= CH_SWITCH_VHT_DISABLED;
3435
3436 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
3437 HOSTAPD_LEVEL_INFO, "CHAN_SWITCH VHT CONFIG 0x%x",
3438 hapd->iconf->ch_switch_vht_config);
3439}
3440
3441
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003442int hostapd_switch_channel(struct hostapd_data *hapd,
3443 struct csa_settings *settings)
3444{
3445 int ret;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003446
3447 if (!(hapd->iface->drv_flags & WPA_DRIVER_FLAGS_AP_CSA)) {
3448 wpa_printf(MSG_INFO, "CSA is not supported");
3449 return -1;
3450 }
3451
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003452 ret = hostapd_fill_csa_settings(hapd, settings);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003453 if (ret)
3454 return ret;
3455
3456 ret = hostapd_drv_switch_channel(hapd, settings);
3457 free_beacon_data(&settings->beacon_csa);
3458 free_beacon_data(&settings->beacon_after);
3459
3460 if (ret) {
3461 /* if we failed, clean cs parameters */
3462 hostapd_cleanup_cs_params(hapd);
3463 return ret;
3464 }
3465
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003466 hapd->csa_in_progress = 1;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003467 return 0;
3468}
3469
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003470
3471void
3472hostapd_switch_channel_fallback(struct hostapd_iface *iface,
3473 const struct hostapd_freq_params *freq_params)
3474{
Hai Shalom81f62d82019-07-22 12:10:00 -07003475 int seg0_idx = 0, seg1_idx = 0, bw = CHANWIDTH_USE_HT;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003476
3477 wpa_printf(MSG_DEBUG, "Restarting all CSA-related BSSes");
3478
3479 if (freq_params->center_freq1)
Hai Shalom81f62d82019-07-22 12:10:00 -07003480 seg0_idx = 36 + (freq_params->center_freq1 - 5180) / 5;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003481 if (freq_params->center_freq2)
Hai Shalom81f62d82019-07-22 12:10:00 -07003482 seg1_idx = 36 + (freq_params->center_freq2 - 5180) / 5;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003483
3484 switch (freq_params->bandwidth) {
3485 case 0:
3486 case 20:
3487 case 40:
Hai Shalom81f62d82019-07-22 12:10:00 -07003488 bw = CHANWIDTH_USE_HT;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003489 break;
3490 case 80:
3491 if (freq_params->center_freq2)
Hai Shalom81f62d82019-07-22 12:10:00 -07003492 bw = CHANWIDTH_80P80MHZ;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003493 else
Hai Shalom81f62d82019-07-22 12:10:00 -07003494 bw = CHANWIDTH_80MHZ;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003495 break;
3496 case 160:
Hai Shalom81f62d82019-07-22 12:10:00 -07003497 bw = CHANWIDTH_160MHZ;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003498 break;
3499 default:
3500 wpa_printf(MSG_WARNING, "Unknown CSA bandwidth: %d",
3501 freq_params->bandwidth);
3502 break;
3503 }
3504
3505 iface->freq = freq_params->freq;
3506 iface->conf->channel = freq_params->channel;
3507 iface->conf->secondary_channel = freq_params->sec_channel_offset;
Hai Shalom81f62d82019-07-22 12:10:00 -07003508 hostapd_set_oper_centr_freq_seg0_idx(iface->conf, seg0_idx);
3509 hostapd_set_oper_centr_freq_seg1_idx(iface->conf, seg1_idx);
3510 hostapd_set_oper_chwidth(iface->conf, bw);
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003511 iface->conf->ieee80211n = freq_params->ht_enabled;
3512 iface->conf->ieee80211ac = freq_params->vht_enabled;
Hai Shalom81f62d82019-07-22 12:10:00 -07003513 iface->conf->ieee80211ax = freq_params->he_enabled;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003514
3515 /*
3516 * cs_params must not be cleared earlier because the freq_params
3517 * argument may actually point to one of these.
Hai Shalom39ba6fc2019-01-22 12:40:38 -08003518 * These params will be cleared during interface disable below.
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003519 */
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003520 hostapd_disable_iface(iface);
3521 hostapd_enable_iface(iface);
3522}
3523
Dmitry Shmidte4663042016-04-04 10:07:49 -07003524#endif /* NEED_AP_MLME */
3525
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003526
3527struct hostapd_data * hostapd_get_iface(struct hapd_interfaces *interfaces,
3528 const char *ifname)
3529{
3530 size_t i, j;
3531
3532 for (i = 0; i < interfaces->count; i++) {
3533 struct hostapd_iface *iface = interfaces->iface[i];
3534
3535 for (j = 0; j < iface->num_bss; j++) {
3536 struct hostapd_data *hapd = iface->bss[j];
3537
3538 if (os_strcmp(ifname, hapd->conf->iface) == 0)
3539 return hapd;
3540 }
3541 }
3542
3543 return NULL;
3544}
3545
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003546
3547void hostapd_periodic_iface(struct hostapd_iface *iface)
3548{
3549 size_t i;
3550
3551 ap_list_timer(iface);
3552
3553 for (i = 0; i < iface->num_bss; i++) {
3554 struct hostapd_data *hapd = iface->bss[i];
3555
3556 if (!hapd->started)
3557 continue;
3558
3559#ifndef CONFIG_NO_RADIUS
3560 hostapd_acl_expire(hapd);
3561#endif /* CONFIG_NO_RADIUS */
3562 }
3563}