blob: cc75a7765a80f01e35dad7dd00ea34ad47d7bdad [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"
10
11#include "utils/common.h"
12#include "utils/eloop.h"
13#include "common/ieee802_11_defs.h"
Dmitry Shmidtcce06662013-11-04 18:44:24 -080014#include "common/wpa_ctrl.h"
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080015#include "common/hw_features_common.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070016#include "radius/radius_client.h"
Dmitry Shmidt04949592012-07-19 12:16:46 -070017#include "radius/radius_das.h"
Dmitry Shmidt50b691d2014-05-21 14:01:45 -070018#include "eap_server/tncs.h"
Dmitry Shmidt2f74e362015-01-21 13:19:05 -080019#include "eapol_auth/eapol_auth_sm.h"
20#include "eapol_auth/eapol_auth_sm_i.h"
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080021#include "fst/fst.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070022#include "hostapd.h"
23#include "authsrv.h"
24#include "sta_info.h"
25#include "accounting.h"
26#include "ap_list.h"
27#include "beacon.h"
28#include "iapp.h"
29#include "ieee802_1x.h"
30#include "ieee802_11_auth.h"
31#include "vlan_init.h"
32#include "wpa_auth.h"
33#include "wps_hostapd.h"
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070034#include "dpp_hostapd.h"
35#include "gas_query_ap.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070036#include "hw_features.h"
37#include "wpa_auth_glue.h"
38#include "ap_drv_ops.h"
39#include "ap_config.h"
40#include "p2p_hostapd.h"
Dmitry Shmidt04949592012-07-19 12:16:46 -070041#include "gas_serv.h"
Dmitry Shmidt051af732013-10-22 13:52:46 -070042#include "dfs.h"
Dmitry Shmidt7832adb2014-04-29 10:53:02 -070043#include "ieee802_11.h"
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080044#include "bss_load.h"
45#include "x_snoop.h"
46#include "dhcp_snoop.h"
47#include "ndisc_snoop.h"
Dmitry Shmidt849734c2016-05-27 09:59:01 -070048#include "neighbor_db.h"
49#include "rrm.h"
Dmitry Shmidtebd93af2017-02-21 13:40:44 -080050#include "fils_hlp.h"
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070051#include "acs.h"
Roshan Pius3a1667e2018-07-03 15:17:14 -070052#include "hs20.h"
Hai Shalom81f62d82019-07-22 12:10:00 -070053#include "airtime_policy.h"
54#include "wpa_auth_kay.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070055
56
Dmitry Shmidt04949592012-07-19 12:16:46 -070057static int hostapd_flush_old_stations(struct hostapd_data *hapd, u16 reason);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070058static int hostapd_setup_encryption(char *iface, struct hostapd_data *hapd);
Dmitry Shmidtc55524a2011-07-07 11:18:38 -070059static int hostapd_broadcast_wep_clear(struct hostapd_data *hapd);
Dmitry Shmidtcce06662013-11-04 18:44:24 -080060static int setup_interface2(struct hostapd_iface *iface);
61static void channel_list_update_timeout(void *eloop_ctx, void *timeout_ctx);
Roshan Pius3a1667e2018-07-03 15:17:14 -070062static void hostapd_interface_setup_failure_handler(void *eloop_ctx,
63 void *timeout_ctx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070064
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070065
Dmitry Shmidt04949592012-07-19 12:16:46 -070066int hostapd_for_each_interface(struct hapd_interfaces *interfaces,
67 int (*cb)(struct hostapd_iface *iface,
68 void *ctx), void *ctx)
69{
70 size_t i;
71 int ret;
72
73 for (i = 0; i < interfaces->count; i++) {
74 ret = cb(interfaces->iface[i], ctx);
75 if (ret)
76 return ret;
77 }
78
79 return 0;
80}
81
82
Hai Shalomce48b4a2018-09-05 11:41:35 -070083void hostapd_reconfig_encryption(struct hostapd_data *hapd)
84{
85 if (hapd->wpa_auth)
86 return;
87
88 hostapd_set_privacy(hapd, 0);
89 hostapd_setup_encryption(hapd->conf->iface, hapd);
90}
91
92
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070093static void hostapd_reload_bss(struct hostapd_data *hapd)
94{
Dmitry Shmidtcce06662013-11-04 18:44:24 -080095 struct hostapd_ssid *ssid;
96
Dmitry Shmidt29333592017-01-09 12:27:11 -080097 if (!hapd->started)
98 return;
99
Roshan Pius3a1667e2018-07-03 15:17:14 -0700100 if (hapd->conf->wmm_enabled < 0)
101 hapd->conf->wmm_enabled = hapd->iconf->ieee80211n;
102
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700103#ifndef CONFIG_NO_RADIUS
104 radius_client_reconfig(hapd->radius, hapd->conf->radius);
105#endif /* CONFIG_NO_RADIUS */
106
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800107 ssid = &hapd->conf->ssid;
108 if (!ssid->wpa_psk_set && ssid->wpa_psk && !ssid->wpa_psk->next &&
109 ssid->wpa_passphrase_set && ssid->wpa_passphrase) {
110 /*
111 * Force PSK to be derived again since SSID or passphrase may
112 * have changed.
113 */
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800114 hostapd_config_clear_wpa_psk(&hapd->conf->ssid.wpa_psk);
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800115 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700116 if (hostapd_setup_wpa_psk(hapd->conf)) {
117 wpa_printf(MSG_ERROR, "Failed to re-configure WPA PSK "
118 "after reloading configuration");
119 }
120
121 if (hapd->conf->ieee802_1x || hapd->conf->wpa)
122 hostapd_set_drv_ieee8021x(hapd, hapd->conf->iface, 1);
123 else
124 hostapd_set_drv_ieee8021x(hapd, hapd->conf->iface, 0);
125
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800126 if ((hapd->conf->wpa || hapd->conf->osen) && hapd->wpa_auth == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700127 hostapd_setup_wpa(hapd);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800128 if (hapd->wpa_auth)
129 wpa_init_keys(hapd->wpa_auth);
130 } else if (hapd->conf->wpa) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700131 const u8 *wpa_ie;
132 size_t wpa_ie_len;
133 hostapd_reconfig_wpa(hapd);
134 wpa_ie = wpa_auth_get_wpa_ie(hapd->wpa_auth, &wpa_ie_len);
135 if (hostapd_set_generic_elem(hapd, wpa_ie, wpa_ie_len))
136 wpa_printf(MSG_ERROR, "Failed to configure WPA IE for "
137 "the kernel driver.");
138 } else if (hapd->wpa_auth) {
139 wpa_deinit(hapd->wpa_auth);
140 hapd->wpa_auth = NULL;
141 hostapd_set_privacy(hapd, 0);
142 hostapd_setup_encryption(hapd->conf->iface, hapd);
143 hostapd_set_generic_elem(hapd, (u8 *) "", 0);
144 }
145
146 ieee802_11_set_beacon(hapd);
147 hostapd_update_wps(hapd);
148
149 if (hapd->conf->ssid.ssid_set &&
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700150 hostapd_set_ssid(hapd, hapd->conf->ssid.ssid,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700151 hapd->conf->ssid.ssid_len)) {
152 wpa_printf(MSG_ERROR, "Could not set SSID for kernel driver");
153 /* try to continue */
154 }
155 wpa_printf(MSG_DEBUG, "Reconfigured interface %s", hapd->conf->iface);
156}
157
158
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700159static void hostapd_clear_old(struct hostapd_iface *iface)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700160{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700161 size_t j;
162
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700163 /*
164 * Deauthenticate all stations since the new configuration may not
165 * allow them to use the BSS anymore.
166 */
167 for (j = 0; j < iface->num_bss; j++) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700168 hostapd_flush_old_stations(iface->bss[j],
169 WLAN_REASON_PREV_AUTH_NOT_VALID);
Dmitry Shmidtc55524a2011-07-07 11:18:38 -0700170 hostapd_broadcast_wep_clear(iface->bss[j]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700171
172#ifndef CONFIG_NO_RADIUS
173 /* TODO: update dynamic data based on changed configuration
174 * items (e.g., open/close sockets, etc.) */
175 radius_client_flush(iface->bss[j]->radius, 0);
176#endif /* CONFIG_NO_RADIUS */
177 }
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700178}
179
180
Hai Shalom74f70d42019-02-11 14:42:39 -0800181static int hostapd_iface_conf_changed(struct hostapd_config *newconf,
182 struct hostapd_config *oldconf)
183{
184 size_t i;
185
186 if (newconf->num_bss != oldconf->num_bss)
187 return 1;
188
189 for (i = 0; i < newconf->num_bss; i++) {
190 if (os_strcmp(newconf->bss[i]->iface,
191 oldconf->bss[i]->iface) != 0)
192 return 1;
193 }
194
195 return 0;
196}
197
198
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700199int hostapd_reload_config(struct hostapd_iface *iface)
200{
Hai Shalom74f70d42019-02-11 14:42:39 -0800201 struct hapd_interfaces *interfaces = iface->interfaces;
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700202 struct hostapd_data *hapd = iface->bss[0];
203 struct hostapd_config *newconf, *oldconf;
204 size_t j;
205
206 if (iface->config_fname == NULL) {
207 /* Only in-memory config in use - assume it has been updated */
208 hostapd_clear_old(iface);
209 for (j = 0; j < iface->num_bss; j++)
210 hostapd_reload_bss(iface->bss[j]);
211 return 0;
212 }
213
214 if (iface->interfaces == NULL ||
215 iface->interfaces->config_read_cb == NULL)
216 return -1;
217 newconf = iface->interfaces->config_read_cb(iface->config_fname);
218 if (newconf == NULL)
219 return -1;
220
221 hostapd_clear_old(iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700222
223 oldconf = hapd->iconf;
Hai Shalom74f70d42019-02-11 14:42:39 -0800224 if (hostapd_iface_conf_changed(newconf, oldconf)) {
225 char *fname;
226 int res;
227
228 wpa_printf(MSG_DEBUG,
229 "Configuration changes include interface/BSS modification - force full disable+enable sequence");
230 fname = os_strdup(iface->config_fname);
231 if (!fname) {
232 hostapd_config_free(newconf);
233 return -1;
234 }
235 hostapd_remove_iface(interfaces, hapd->conf->iface);
236 iface = hostapd_init(interfaces, fname);
237 os_free(fname);
238 hostapd_config_free(newconf);
239 if (!iface) {
240 wpa_printf(MSG_ERROR,
241 "Failed to initialize interface on config reload");
242 return -1;
243 }
244 iface->interfaces = interfaces;
245 interfaces->iface[interfaces->count] = iface;
246 interfaces->count++;
247 res = hostapd_enable_iface(iface);
248 if (res < 0)
249 wpa_printf(MSG_ERROR,
250 "Failed to enable interface on config reload");
251 return res;
252 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700253 iface->conf = newconf;
254
255 for (j = 0; j < iface->num_bss; j++) {
256 hapd = iface->bss[j];
257 hapd->iconf = newconf;
Dmitry Shmidtd11f0192014-03-24 12:09:47 -0700258 hapd->iconf->channel = oldconf->channel;
Dmitry Shmidtdda10c22015-03-24 16:05:01 -0700259 hapd->iconf->acs = oldconf->acs;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -0700260 hapd->iconf->secondary_channel = oldconf->secondary_channel;
Dmitry Shmidtd11f0192014-03-24 12:09:47 -0700261 hapd->iconf->ieee80211n = oldconf->ieee80211n;
262 hapd->iconf->ieee80211ac = oldconf->ieee80211ac;
263 hapd->iconf->ht_capab = oldconf->ht_capab;
264 hapd->iconf->vht_capab = oldconf->vht_capab;
Hai Shalom81f62d82019-07-22 12:10:00 -0700265 hostapd_set_oper_chwidth(hapd->iconf,
266 hostapd_get_oper_chwidth(oldconf));
267 hostapd_set_oper_centr_freq_seg0_idx(
268 hapd->iconf,
269 hostapd_get_oper_centr_freq_seg0_idx(oldconf));
270 hostapd_set_oper_centr_freq_seg1_idx(
271 hapd->iconf,
272 hostapd_get_oper_centr_freq_seg1_idx(oldconf));
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800273 hapd->conf = newconf->bss[j];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700274 hostapd_reload_bss(hapd);
275 }
276
277 hostapd_config_free(oldconf);
278
279
280 return 0;
281}
282
283
284static void hostapd_broadcast_key_clear_iface(struct hostapd_data *hapd,
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700285 const char *ifname)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700286{
287 int i;
288
Dmitry Shmidt29333592017-01-09 12:27:11 -0800289 if (!ifname || !hapd->drv_priv)
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700290 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700291 for (i = 0; i < NUM_WEP_KEYS; i++) {
292 if (hostapd_drv_set_key(ifname, hapd, WPA_ALG_NONE, NULL, i,
293 0, NULL, 0, NULL, 0)) {
294 wpa_printf(MSG_DEBUG, "Failed to clear default "
295 "encryption keys (ifname=%s keyidx=%d)",
296 ifname, i);
297 }
298 }
299#ifdef CONFIG_IEEE80211W
300 if (hapd->conf->ieee80211w) {
301 for (i = NUM_WEP_KEYS; i < NUM_WEP_KEYS + 2; i++) {
302 if (hostapd_drv_set_key(ifname, hapd, WPA_ALG_NONE,
303 NULL, i, 0, NULL,
304 0, NULL, 0)) {
305 wpa_printf(MSG_DEBUG, "Failed to clear "
306 "default mgmt encryption keys "
307 "(ifname=%s keyidx=%d)", ifname, i);
308 }
309 }
310 }
311#endif /* CONFIG_IEEE80211W */
312}
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 iapp_deinit(hapd->iapp);
364 hapd->iapp = NULL;
365 accounting_deinit(hapd);
366 hostapd_deinit_wpa(hapd);
367 vlan_deinit(hapd);
368 hostapd_acl_deinit(hapd);
369#ifndef CONFIG_NO_RADIUS
370 radius_client_deinit(hapd->radius);
371 hapd->radius = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700372 radius_das_deinit(hapd->radius_das);
373 hapd->radius_das = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700374#endif /* CONFIG_NO_RADIUS */
375
376 hostapd_deinit_wps(hapd);
Hai Shalom81f62d82019-07-22 12:10:00 -0700377 ieee802_1x_dealloc_kay_sm_hapd(hapd);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700378#ifdef CONFIG_DPP
379 hostapd_dpp_deinit(hapd);
380 gas_query_ap_deinit(hapd->gas);
381#endif /* CONFIG_DPP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700382
383 authsrv_deinit(hapd);
384
Dmitry Shmidt71757432014-06-02 13:50:35 -0700385 if (hapd->interface_added) {
386 hapd->interface_added = 0;
387 if (hostapd_if_remove(hapd, WPA_IF_AP_BSS, hapd->conf->iface)) {
388 wpa_printf(MSG_WARNING,
389 "Failed to remove BSS interface %s",
390 hapd->conf->iface);
391 hapd->interface_added = 1;
392 } else {
393 /*
394 * Since this was a dynamically added interface, the
395 * driver wrapper may have removed its internal instance
396 * and hapd->drv_priv is not valid anymore.
397 */
398 hapd->drv_priv = NULL;
399 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700400 }
401
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800402 wpabuf_free(hapd->time_adv);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700403
404#ifdef CONFIG_INTERWORKING
405 gas_serv_deinit(hapd);
406#endif /* CONFIG_INTERWORKING */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800407
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800408 bss_load_update_deinit(hapd);
409 ndisc_snoop_deinit(hapd);
410 dhcp_snoop_deinit(hapd);
411 x_snoop_deinit(hapd);
412
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800413#ifdef CONFIG_SQLITE
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700414 bin_clear_free(hapd->tmp_eap_user.identity,
415 hapd->tmp_eap_user.identity_len);
416 bin_clear_free(hapd->tmp_eap_user.password,
417 hapd->tmp_eap_user.password_len);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800418#endif /* CONFIG_SQLITE */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800419
420#ifdef CONFIG_MESH
421 wpabuf_free(hapd->mesh_pending_auth);
422 hapd->mesh_pending_auth = NULL;
423#endif /* CONFIG_MESH */
Dmitry Shmidt849734c2016-05-27 09:59:01 -0700424
425 hostapd_clean_rrm(hapd);
Dmitry Shmidtebd93af2017-02-21 13:40:44 -0800426 fils_hlp_deinit(hapd);
Hai Shalom021b0b52019-04-10 11:17:58 -0700427
428#ifdef CONFIG_SAE
429 {
430 struct hostapd_sae_commit_queue *q;
431
432 while ((q = dl_list_first(&hapd->sae_commit_queue,
433 struct hostapd_sae_commit_queue,
434 list))) {
435 dl_list_del(&q->list);
436 os_free(q);
437 }
438 }
439 eloop_cancel_timeout(auth_sae_process_commit, hapd, NULL);
440#endif /* CONFIG_SAE */
Dmitry Shmidt04949592012-07-19 12:16:46 -0700441}
442
443
444/**
445 * hostapd_cleanup - Per-BSS cleanup (deinitialization)
446 * @hapd: Pointer to BSS data
447 *
448 * This function is used to free all per-BSS data structures and resources.
Dmitry Shmidt54605472013-11-08 11:10:19 -0800449 * Most of the modules that are initialized in hostapd_setup_bss() are
450 * deinitialized here.
Dmitry Shmidt04949592012-07-19 12:16:46 -0700451 */
452static void hostapd_cleanup(struct hostapd_data *hapd)
453{
Dmitry Shmidt54605472013-11-08 11:10:19 -0800454 wpa_printf(MSG_DEBUG, "%s(hapd=%p (%s))", __func__, hapd,
Hai Shalom021b0b52019-04-10 11:17:58 -0700455 hapd->conf ? hapd->conf->iface : "N/A");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700456 if (hapd->iface->interfaces &&
Dmitry Shmidt29333592017-01-09 12:27:11 -0800457 hapd->iface->interfaces->ctrl_iface_deinit) {
458 wpa_msg(hapd->msg_ctx, MSG_INFO, WPA_EVENT_TERMINATING);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700459 hapd->iface->interfaces->ctrl_iface_deinit(hapd);
Dmitry Shmidt29333592017-01-09 12:27:11 -0800460 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700461 hostapd_free_hapd_data(hapd);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700462}
463
464
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800465static void sta_track_deinit(struct hostapd_iface *iface)
466{
467 struct hostapd_sta_info *info;
468
469 if (!iface->num_sta_seen)
470 return;
471
472 while ((info = dl_list_first(&iface->sta_seen, struct hostapd_sta_info,
473 list))) {
474 dl_list_del(&info->list);
475 iface->num_sta_seen--;
Dmitry Shmidtaca489e2016-09-28 15:44:14 -0700476 sta_track_del(info);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800477 }
478}
479
480
Dmitry Shmidt04949592012-07-19 12:16:46 -0700481static void hostapd_cleanup_iface_partial(struct hostapd_iface *iface)
482{
Dmitry Shmidt54605472013-11-08 11:10:19 -0800483 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800484#ifdef CONFIG_IEEE80211N
485#ifdef NEED_AP_MLME
486 hostapd_stop_setup_timers(iface);
487#endif /* NEED_AP_MLME */
488#endif /* CONFIG_IEEE80211N */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700489 if (iface->current_mode)
490 acs_cleanup(iface);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700491 hostapd_free_hw_features(iface->hw_features, iface->num_hw_features);
492 iface->hw_features = NULL;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700493 iface->current_mode = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700494 os_free(iface->current_rates);
495 iface->current_rates = NULL;
496 os_free(iface->basic_rates);
497 iface->basic_rates = NULL;
498 ap_list_deinit(iface);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800499 sta_track_deinit(iface);
Hai Shalom81f62d82019-07-22 12:10:00 -0700500 airtime_policy_update_deinit(iface);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700501}
502
503
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700504/**
505 * hostapd_cleanup_iface - Complete per-interface cleanup
506 * @iface: Pointer to interface data
507 *
508 * This function is called after per-BSS data structures are deinitialized
509 * with hostapd_cleanup().
510 */
511static void hostapd_cleanup_iface(struct hostapd_iface *iface)
512{
Dmitry Shmidt54605472013-11-08 11:10:19 -0800513 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800514 eloop_cancel_timeout(channel_list_update_timeout, iface, NULL);
Roshan Pius3a1667e2018-07-03 15:17:14 -0700515 eloop_cancel_timeout(hostapd_interface_setup_failure_handler, iface,
516 NULL);
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800517
Dmitry Shmidt04949592012-07-19 12:16:46 -0700518 hostapd_cleanup_iface_partial(iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700519 hostapd_config_free(iface->conf);
520 iface->conf = NULL;
521
522 os_free(iface->config_fname);
523 os_free(iface->bss);
Dmitry Shmidt54605472013-11-08 11:10:19 -0800524 wpa_printf(MSG_DEBUG, "%s: free iface=%p", __func__, iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700525 os_free(iface);
526}
527
528
Dmitry Shmidt04949592012-07-19 12:16:46 -0700529static void hostapd_clear_wep(struct hostapd_data *hapd)
530{
Hai Shalom021b0b52019-04-10 11:17:58 -0700531 if (hapd->drv_priv && !hapd->iface->driver_ap_teardown && hapd->conf) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700532 hostapd_set_privacy(hapd, 0);
533 hostapd_broadcast_wep_clear(hapd);
534 }
535}
536
537
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700538static int hostapd_setup_encryption(char *iface, struct hostapd_data *hapd)
539{
540 int i;
541
542 hostapd_broadcast_wep_set(hapd);
543
544 if (hapd->conf->ssid.wep.default_len) {
545 hostapd_set_privacy(hapd, 1);
546 return 0;
547 }
548
Jouni Malinen75ecf522011-06-27 15:19:46 -0700549 /*
550 * When IEEE 802.1X is not enabled, the driver may need to know how to
551 * set authentication algorithms for static WEP.
552 */
553 hostapd_drv_set_authmode(hapd, hapd->conf->auth_algs);
554
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700555 for (i = 0; i < 4; i++) {
556 if (hapd->conf->ssid.wep.key[i] &&
557 hostapd_drv_set_key(iface, hapd, WPA_ALG_WEP, NULL, i,
558 i == hapd->conf->ssid.wep.idx, NULL, 0,
559 hapd->conf->ssid.wep.key[i],
560 hapd->conf->ssid.wep.len[i])) {
561 wpa_printf(MSG_WARNING, "Could not set WEP "
562 "encryption.");
563 return -1;
564 }
565 if (hapd->conf->ssid.wep.key[i] &&
566 i == hapd->conf->ssid.wep.idx)
567 hostapd_set_privacy(hapd, 1);
568 }
569
570 return 0;
571}
572
573
Dmitry Shmidt04949592012-07-19 12:16:46 -0700574static int hostapd_flush_old_stations(struct hostapd_data *hapd, u16 reason)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700575{
576 int ret = 0;
577 u8 addr[ETH_ALEN];
578
579 if (hostapd_drv_none(hapd) || hapd->drv_priv == NULL)
580 return 0;
581
Dmitry Shmidta38abf92014-03-06 13:38:44 -0800582 if (!hapd->iface->driver_ap_teardown) {
583 wpa_dbg(hapd->msg_ctx, MSG_DEBUG,
584 "Flushing old station entries");
585
586 if (hostapd_flush(hapd)) {
587 wpa_msg(hapd->msg_ctx, MSG_WARNING,
588 "Could not connect to kernel driver");
589 ret = -1;
590 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700591 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700592 if (hapd->conf && hapd->conf->broadcast_deauth) {
593 wpa_dbg(hapd->msg_ctx, MSG_DEBUG,
594 "Deauthenticate all stations");
595 os_memset(addr, 0xff, ETH_ALEN);
596 hostapd_drv_sta_deauth(hapd, addr, reason);
597 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700598 hostapd_free_stas(hapd);
599
600 return ret;
601}
602
603
Dmitry Shmidt71757432014-06-02 13:50:35 -0700604static void hostapd_bss_deinit_no_free(struct hostapd_data *hapd)
605{
606 hostapd_free_stas(hapd);
607 hostapd_flush_old_stations(hapd, WLAN_REASON_DEAUTH_LEAVING);
608 hostapd_clear_wep(hapd);
609}
610
611
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700612/**
613 * hostapd_validate_bssid_configuration - Validate BSSID configuration
614 * @iface: Pointer to interface data
615 * Returns: 0 on success, -1 on failure
616 *
617 * This function is used to validate that the configured BSSIDs are valid.
618 */
619static int hostapd_validate_bssid_configuration(struct hostapd_iface *iface)
620{
621 u8 mask[ETH_ALEN] = { 0 };
622 struct hostapd_data *hapd = iface->bss[0];
623 unsigned int i = iface->conf->num_bss, bits = 0, j;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700624 int auto_addr = 0;
625
626 if (hostapd_drv_none(hapd))
627 return 0;
628
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -0800629 if (iface->conf->use_driver_iface_addr)
630 return 0;
631
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700632 /* Generate BSSID mask that is large enough to cover the BSSIDs. */
633
634 /* Determine the bits necessary to cover the number of BSSIDs. */
635 for (i--; i; i >>= 1)
636 bits++;
637
638 /* Determine the bits necessary to any configured BSSIDs,
639 if they are higher than the number of BSSIDs. */
640 for (j = 0; j < iface->conf->num_bss; j++) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800641 if (is_zero_ether_addr(iface->conf->bss[j]->bssid)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700642 if (j)
643 auto_addr++;
644 continue;
645 }
646
647 for (i = 0; i < ETH_ALEN; i++) {
648 mask[i] |=
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800649 iface->conf->bss[j]->bssid[i] ^
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700650 hapd->own_addr[i];
651 }
652 }
653
654 if (!auto_addr)
655 goto skip_mask_ext;
656
657 for (i = 0; i < ETH_ALEN && mask[i] == 0; i++)
658 ;
659 j = 0;
660 if (i < ETH_ALEN) {
661 j = (5 - i) * 8;
662
663 while (mask[i] != 0) {
664 mask[i] >>= 1;
665 j++;
666 }
667 }
668
669 if (bits < j)
670 bits = j;
671
672 if (bits > 40) {
673 wpa_printf(MSG_ERROR, "Too many bits in the BSSID mask (%u)",
674 bits);
675 return -1;
676 }
677
678 os_memset(mask, 0xff, ETH_ALEN);
679 j = bits / 8;
680 for (i = 5; i > 5 - j; i--)
681 mask[i] = 0;
682 j = bits % 8;
Hai Shalom021b0b52019-04-10 11:17:58 -0700683 while (j) {
684 j--;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700685 mask[i] <<= 1;
Hai Shalom021b0b52019-04-10 11:17:58 -0700686 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700687
688skip_mask_ext:
689 wpa_printf(MSG_DEBUG, "BSS count %lu, BSSID mask " MACSTR " (%d bits)",
690 (unsigned long) iface->conf->num_bss, MAC2STR(mask), bits);
691
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700692 if (!auto_addr)
693 return 0;
694
695 for (i = 0; i < ETH_ALEN; i++) {
696 if ((hapd->own_addr[i] & mask[i]) != hapd->own_addr[i]) {
697 wpa_printf(MSG_ERROR, "Invalid BSSID mask " MACSTR
698 " for start address " MACSTR ".",
699 MAC2STR(mask), MAC2STR(hapd->own_addr));
700 wpa_printf(MSG_ERROR, "Start address must be the "
701 "first address in the block (i.e., addr "
702 "AND mask == addr).");
703 return -1;
704 }
705 }
706
707 return 0;
708}
709
710
711static int mac_in_conf(struct hostapd_config *conf, const void *a)
712{
713 size_t i;
714
715 for (i = 0; i < conf->num_bss; i++) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800716 if (hostapd_mac_comp(conf->bss[i]->bssid, a) == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700717 return 1;
718 }
719 }
720
721 return 0;
722}
723
724
Dmitry Shmidt04949592012-07-19 12:16:46 -0700725#ifndef CONFIG_NO_RADIUS
726
727static int hostapd_das_nas_mismatch(struct hostapd_data *hapd,
728 struct radius_das_attrs *attr)
729{
Dmitry Shmidt13ca8d82014-02-20 10:18:40 -0800730 if (attr->nas_identifier &&
731 (!hapd->conf->nas_identifier ||
732 os_strlen(hapd->conf->nas_identifier) !=
733 attr->nas_identifier_len ||
734 os_memcmp(hapd->conf->nas_identifier, attr->nas_identifier,
735 attr->nas_identifier_len) != 0)) {
736 wpa_printf(MSG_DEBUG, "RADIUS DAS: NAS-Identifier mismatch");
737 return 1;
738 }
739
740 if (attr->nas_ip_addr &&
741 (hapd->conf->own_ip_addr.af != AF_INET ||
742 os_memcmp(&hapd->conf->own_ip_addr.u.v4, attr->nas_ip_addr, 4) !=
743 0)) {
744 wpa_printf(MSG_DEBUG, "RADIUS DAS: NAS-IP-Address mismatch");
745 return 1;
746 }
747
748#ifdef CONFIG_IPV6
749 if (attr->nas_ipv6_addr &&
750 (hapd->conf->own_ip_addr.af != AF_INET6 ||
751 os_memcmp(&hapd->conf->own_ip_addr.u.v6, attr->nas_ipv6_addr, 16)
752 != 0)) {
753 wpa_printf(MSG_DEBUG, "RADIUS DAS: NAS-IPv6-Address mismatch");
754 return 1;
755 }
756#endif /* CONFIG_IPV6 */
757
Dmitry Shmidt04949592012-07-19 12:16:46 -0700758 return 0;
759}
760
761
762static struct sta_info * hostapd_das_find_sta(struct hostapd_data *hapd,
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800763 struct radius_das_attrs *attr,
764 int *multi)
Dmitry Shmidt04949592012-07-19 12:16:46 -0700765{
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800766 struct sta_info *selected, *sta;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700767 char buf[128];
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800768 int num_attr = 0;
769 int count;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700770
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800771 *multi = 0;
772
773 for (sta = hapd->sta_list; sta; sta = sta->next)
774 sta->radius_das_match = 1;
775
776 if (attr->sta_addr) {
777 num_attr++;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700778 sta = ap_get_sta(hapd, attr->sta_addr);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800779 if (!sta) {
780 wpa_printf(MSG_DEBUG,
781 "RADIUS DAS: No Calling-Station-Id match");
782 return NULL;
783 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700784
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800785 selected = sta;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700786 for (sta = hapd->sta_list; sta; sta = sta->next) {
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800787 if (sta != selected)
788 sta->radius_das_match = 0;
789 }
790 wpa_printf(MSG_DEBUG, "RADIUS DAS: Calling-Station-Id match");
791 }
792
793 if (attr->acct_session_id) {
794 num_attr++;
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800795 if (attr->acct_session_id_len != 16) {
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800796 wpa_printf(MSG_DEBUG,
797 "RADIUS DAS: Acct-Session-Id cannot match");
798 return NULL;
799 }
800 count = 0;
801
802 for (sta = hapd->sta_list; sta; sta = sta->next) {
803 if (!sta->radius_das_match)
804 continue;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800805 os_snprintf(buf, sizeof(buf), "%016llX",
806 (unsigned long long) sta->acct_session_id);
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800807 if (os_memcmp(attr->acct_session_id, buf, 16) != 0)
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800808 sta->radius_das_match = 0;
809 else
810 count++;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700811 }
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800812
813 if (count == 0) {
814 wpa_printf(MSG_DEBUG,
815 "RADIUS DAS: No matches remaining after Acct-Session-Id check");
816 return NULL;
817 }
818 wpa_printf(MSG_DEBUG, "RADIUS DAS: Acct-Session-Id match");
Dmitry Shmidt04949592012-07-19 12:16:46 -0700819 }
820
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800821 if (attr->acct_multi_session_id) {
822 num_attr++;
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800823 if (attr->acct_multi_session_id_len != 16) {
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800824 wpa_printf(MSG_DEBUG,
825 "RADIUS DAS: Acct-Multi-Session-Id cannot match");
826 return NULL;
827 }
828 count = 0;
829
830 for (sta = hapd->sta_list; sta; sta = sta->next) {
831 if (!sta->radius_das_match)
832 continue;
833 if (!sta->eapol_sm ||
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800834 !sta->eapol_sm->acct_multi_session_id) {
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800835 sta->radius_das_match = 0;
836 continue;
837 }
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800838 os_snprintf(buf, sizeof(buf), "%016llX",
839 (unsigned long long)
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800840 sta->eapol_sm->acct_multi_session_id);
841 if (os_memcmp(attr->acct_multi_session_id, buf, 16) !=
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800842 0)
843 sta->radius_das_match = 0;
844 else
845 count++;
846 }
847
848 if (count == 0) {
849 wpa_printf(MSG_DEBUG,
850 "RADIUS DAS: No matches remaining after Acct-Multi-Session-Id check");
851 return NULL;
852 }
853 wpa_printf(MSG_DEBUG,
854 "RADIUS DAS: Acct-Multi-Session-Id match");
855 }
856
857 if (attr->cui) {
858 num_attr++;
859 count = 0;
860
Dmitry Shmidt04949592012-07-19 12:16:46 -0700861 for (sta = hapd->sta_list; sta; sta = sta->next) {
862 struct wpabuf *cui;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800863
864 if (!sta->radius_das_match)
865 continue;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700866 cui = ieee802_1x_get_radius_cui(sta->eapol_sm);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800867 if (!cui || wpabuf_len(cui) != attr->cui_len ||
Dmitry Shmidt04949592012-07-19 12:16:46 -0700868 os_memcmp(wpabuf_head(cui), attr->cui,
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800869 attr->cui_len) != 0)
870 sta->radius_das_match = 0;
871 else
872 count++;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700873 }
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800874
875 if (count == 0) {
876 wpa_printf(MSG_DEBUG,
877 "RADIUS DAS: No matches remaining after Chargeable-User-Identity check");
878 return NULL;
879 }
880 wpa_printf(MSG_DEBUG,
881 "RADIUS DAS: Chargeable-User-Identity match");
Dmitry Shmidt04949592012-07-19 12:16:46 -0700882 }
883
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800884 if (attr->user_name) {
885 num_attr++;
886 count = 0;
887
Dmitry Shmidt04949592012-07-19 12:16:46 -0700888 for (sta = hapd->sta_list; sta; sta = sta->next) {
889 u8 *identity;
890 size_t identity_len;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800891
892 if (!sta->radius_das_match)
893 continue;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700894 identity = ieee802_1x_get_identity(sta->eapol_sm,
895 &identity_len);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800896 if (!identity ||
897 identity_len != attr->user_name_len ||
Dmitry Shmidt04949592012-07-19 12:16:46 -0700898 os_memcmp(identity, attr->user_name, identity_len)
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800899 != 0)
900 sta->radius_das_match = 0;
901 else
902 count++;
903 }
904
905 if (count == 0) {
906 wpa_printf(MSG_DEBUG,
907 "RADIUS DAS: No matches remaining after User-Name check");
908 return NULL;
909 }
910 wpa_printf(MSG_DEBUG,
911 "RADIUS DAS: User-Name match");
912 }
913
914 if (num_attr == 0) {
915 /*
916 * In theory, we could match all current associations, but it
917 * seems safer to just reject requests that do not include any
918 * session identification attributes.
919 */
920 wpa_printf(MSG_DEBUG,
921 "RADIUS DAS: No session identification attributes included");
922 return NULL;
923 }
924
925 selected = NULL;
926 for (sta = hapd->sta_list; sta; sta = sta->next) {
927 if (sta->radius_das_match) {
928 if (selected) {
929 *multi = 1;
930 return NULL;
931 }
932 selected = sta;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700933 }
934 }
935
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800936 return selected;
937}
938
939
940static int hostapd_das_disconnect_pmksa(struct hostapd_data *hapd,
941 struct radius_das_attrs *attr)
942{
943 if (!hapd->wpa_auth)
944 return -1;
945 return wpa_auth_radius_das_disconnect_pmksa(hapd->wpa_auth, attr);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700946}
947
948
949static enum radius_das_res
950hostapd_das_disconnect(void *ctx, struct radius_das_attrs *attr)
951{
952 struct hostapd_data *hapd = ctx;
953 struct sta_info *sta;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800954 int multi;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700955
956 if (hostapd_das_nas_mismatch(hapd, attr))
957 return RADIUS_DAS_NAS_MISMATCH;
958
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800959 sta = hostapd_das_find_sta(hapd, attr, &multi);
960 if (sta == NULL) {
961 if (multi) {
962 wpa_printf(MSG_DEBUG,
963 "RADIUS DAS: Multiple sessions match - not supported");
964 return RADIUS_DAS_MULTI_SESSION_MATCH;
965 }
966 if (hostapd_das_disconnect_pmksa(hapd, attr) == 0) {
967 wpa_printf(MSG_DEBUG,
968 "RADIUS DAS: PMKSA cache entry matched");
969 return RADIUS_DAS_SUCCESS;
970 }
971 wpa_printf(MSG_DEBUG, "RADIUS DAS: No matching session found");
Dmitry Shmidt04949592012-07-19 12:16:46 -0700972 return RADIUS_DAS_SESSION_NOT_FOUND;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800973 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700974
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800975 wpa_printf(MSG_DEBUG, "RADIUS DAS: Found a matching session " MACSTR
976 " - disconnecting", MAC2STR(sta->addr));
Dmitry Shmidt13ca8d82014-02-20 10:18:40 -0800977 wpa_auth_pmksa_remove(hapd->wpa_auth, sta->addr);
978
Dmitry Shmidt04949592012-07-19 12:16:46 -0700979 hostapd_drv_sta_deauth(hapd, sta->addr,
980 WLAN_REASON_PREV_AUTH_NOT_VALID);
981 ap_sta_deauthenticate(hapd, sta, WLAN_REASON_PREV_AUTH_NOT_VALID);
982
983 return RADIUS_DAS_SUCCESS;
984}
985
Roshan Pius3a1667e2018-07-03 15:17:14 -0700986
987#ifdef CONFIG_HS20
988static enum radius_das_res
989hostapd_das_coa(void *ctx, struct radius_das_attrs *attr)
990{
991 struct hostapd_data *hapd = ctx;
992 struct sta_info *sta;
993 int multi;
994
995 if (hostapd_das_nas_mismatch(hapd, attr))
996 return RADIUS_DAS_NAS_MISMATCH;
997
998 sta = hostapd_das_find_sta(hapd, attr, &multi);
999 if (!sta) {
1000 if (multi) {
1001 wpa_printf(MSG_DEBUG,
1002 "RADIUS DAS: Multiple sessions match - not supported");
1003 return RADIUS_DAS_MULTI_SESSION_MATCH;
1004 }
1005 wpa_printf(MSG_DEBUG, "RADIUS DAS: No matching session found");
1006 return RADIUS_DAS_SESSION_NOT_FOUND;
1007 }
1008
1009 wpa_printf(MSG_DEBUG, "RADIUS DAS: Found a matching session " MACSTR
1010 " - CoA", MAC2STR(sta->addr));
1011
1012 if (attr->hs20_t_c_filtering) {
1013 if (attr->hs20_t_c_filtering[0] & BIT(0)) {
1014 wpa_printf(MSG_DEBUG,
1015 "HS 2.0: Unexpected Terms and Conditions filtering required in CoA-Request");
1016 return RADIUS_DAS_COA_FAILED;
1017 }
1018
1019 hs20_t_c_filtering(hapd, sta, 0);
1020 }
1021
1022 return RADIUS_DAS_SUCCESS;
1023}
1024#else /* CONFIG_HS20 */
1025#define hostapd_das_coa NULL
1026#endif /* CONFIG_HS20 */
1027
Dmitry Shmidt04949592012-07-19 12:16:46 -07001028#endif /* CONFIG_NO_RADIUS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001029
1030
1031/**
1032 * hostapd_setup_bss - Per-BSS setup (initialization)
1033 * @hapd: Pointer to BSS data
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001034 * @first: Whether this BSS is the first BSS of an interface; -1 = not first,
1035 * but interface may exist
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001036 *
1037 * This function is used to initialize all per-BSS data structures and
1038 * resources. This gets called in a loop for each BSS when an interface is
1039 * initialized. Most of the modules that are initialized here will be
1040 * deinitialized in hostapd_cleanup().
1041 */
1042static int hostapd_setup_bss(struct hostapd_data *hapd, int first)
1043{
1044 struct hostapd_bss_config *conf = hapd->conf;
Dmitry Shmidt9d9e6022015-04-23 10:34:55 -07001045 u8 ssid[SSID_MAX_LEN + 1];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001046 int ssid_len, set_ssid;
1047 char force_ifname[IFNAMSIZ];
1048 u8 if_addr[ETH_ALEN];
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001049 int flush_old_stations = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001050
Dmitry Shmidt54605472013-11-08 11:10:19 -08001051 wpa_printf(MSG_DEBUG, "%s(hapd=%p (%s), first=%d)",
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001052 __func__, hapd, conf->iface, first);
Dmitry Shmidt54605472013-11-08 11:10:19 -08001053
Dmitry Shmidt50b691d2014-05-21 14:01:45 -07001054#ifdef EAP_SERVER_TNC
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001055 if (conf->tnc && tncs_global_init() < 0) {
Dmitry Shmidt50b691d2014-05-21 14:01:45 -07001056 wpa_printf(MSG_ERROR, "Failed to initialize TNCS");
1057 return -1;
1058 }
1059#endif /* EAP_SERVER_TNC */
1060
Dmitry Shmidt54605472013-11-08 11:10:19 -08001061 if (hapd->started) {
1062 wpa_printf(MSG_ERROR, "%s: Interface %s was already started",
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001063 __func__, conf->iface);
Dmitry Shmidt54605472013-11-08 11:10:19 -08001064 return -1;
1065 }
1066 hapd->started = 1;
1067
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001068 if (!first || first == -1) {
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08001069 u8 *addr = hapd->own_addr;
1070
1071 if (!is_zero_ether_addr(conf->bssid)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001072 /* Allocate the configured BSSID. */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001073 os_memcpy(hapd->own_addr, conf->bssid, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001074
1075 if (hostapd_mac_comp(hapd->own_addr,
1076 hapd->iface->bss[0]->own_addr) ==
1077 0) {
1078 wpa_printf(MSG_ERROR, "BSS '%s' may not have "
1079 "BSSID set to the MAC address of "
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001080 "the radio", conf->iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001081 return -1;
1082 }
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08001083 } else if (hapd->iconf->use_driver_iface_addr) {
1084 addr = NULL;
1085 } else {
1086 /* Allocate the next available BSSID. */
1087 do {
1088 inc_byte_array(hapd->own_addr, ETH_ALEN);
1089 } while (mac_in_conf(hapd->iconf, hapd->own_addr));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001090 }
1091
Dmitry Shmidt54605472013-11-08 11:10:19 -08001092 hapd->interface_added = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001093 if (hostapd_if_add(hapd->iface->bss[0], WPA_IF_AP_BSS,
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08001094 conf->iface, addr, hapd,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001095 &hapd->drv_priv, force_ifname, if_addr,
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001096 conf->bridge[0] ? conf->bridge : NULL,
1097 first == -1)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001098 wpa_printf(MSG_ERROR, "Failed to add BSS (BSSID="
1099 MACSTR ")", MAC2STR(hapd->own_addr));
Dmitry Shmidt3cf6f792013-12-18 13:12:19 -08001100 hapd->interface_added = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001101 return -1;
1102 }
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08001103
1104 if (!addr)
1105 os_memcpy(hapd->own_addr, if_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001106 }
1107
1108 if (conf->wmm_enabled < 0)
1109 conf->wmm_enabled = hapd->iconf->ieee80211n;
1110
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001111#ifdef CONFIG_IEEE80211R_AP
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001112 if (is_zero_ether_addr(conf->r1_key_holder))
1113 os_memcpy(conf->r1_key_holder, hapd->own_addr, ETH_ALEN);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001114#endif /* CONFIG_IEEE80211R_AP */
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001115
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001116#ifdef CONFIG_MESH
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001117 if ((hapd->conf->mesh & MESH_ENABLED) && hapd->iface->mconf == NULL)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001118 flush_old_stations = 0;
1119#endif /* CONFIG_MESH */
1120
1121 if (flush_old_stations)
1122 hostapd_flush_old_stations(hapd,
1123 WLAN_REASON_PREV_AUTH_NOT_VALID);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001124 hostapd_set_privacy(hapd, 0);
1125
1126 hostapd_broadcast_wep_clear(hapd);
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001127 if (hostapd_setup_encryption(conf->iface, hapd))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001128 return -1;
1129
1130 /*
1131 * Fetch the SSID from the system and use it or,
1132 * if one was specified in the config file, verify they
1133 * match.
1134 */
1135 ssid_len = hostapd_get_ssid(hapd, ssid, sizeof(ssid));
1136 if (ssid_len < 0) {
1137 wpa_printf(MSG_ERROR, "Could not read SSID from system");
1138 return -1;
1139 }
1140 if (conf->ssid.ssid_set) {
1141 /*
1142 * If SSID is specified in the config file and it differs
1143 * from what is being used then force installation of the
1144 * new SSID.
1145 */
1146 set_ssid = (conf->ssid.ssid_len != (size_t) ssid_len ||
1147 os_memcmp(conf->ssid.ssid, ssid, ssid_len) != 0);
1148 } else {
1149 /*
1150 * No SSID in the config file; just use the one we got
1151 * from the system.
1152 */
1153 set_ssid = 0;
1154 conf->ssid.ssid_len = ssid_len;
1155 os_memcpy(conf->ssid.ssid, ssid, conf->ssid.ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001156 }
1157
1158 if (!hostapd_drv_none(hapd)) {
1159 wpa_printf(MSG_ERROR, "Using interface %s with hwaddr " MACSTR
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001160 " and ssid \"%s\"",
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001161 conf->iface, MAC2STR(hapd->own_addr),
1162 wpa_ssid_txt(conf->ssid.ssid, conf->ssid.ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001163 }
1164
1165 if (hostapd_setup_wpa_psk(conf)) {
1166 wpa_printf(MSG_ERROR, "WPA-PSK setup failed.");
1167 return -1;
1168 }
1169
1170 /* Set SSID for the kernel driver (to be used in beacon and probe
1171 * response frames) */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001172 if (set_ssid && hostapd_set_ssid(hapd, conf->ssid.ssid,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001173 conf->ssid.ssid_len)) {
1174 wpa_printf(MSG_ERROR, "Could not set SSID for kernel driver");
1175 return -1;
1176 }
1177
Dmitry Shmidt818ea482014-03-10 13:15:21 -07001178 if (wpa_debug_level <= MSG_MSGDUMP)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001179 conf->radius->msg_dumps = 1;
1180#ifndef CONFIG_NO_RADIUS
1181 hapd->radius = radius_client_init(hapd, conf->radius);
1182 if (hapd->radius == NULL) {
1183 wpa_printf(MSG_ERROR, "RADIUS client initialization failed.");
1184 return -1;
1185 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001186
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001187 if (conf->radius_das_port) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001188 struct radius_das_conf das_conf;
1189 os_memset(&das_conf, 0, sizeof(das_conf));
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001190 das_conf.port = conf->radius_das_port;
1191 das_conf.shared_secret = conf->radius_das_shared_secret;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001192 das_conf.shared_secret_len =
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001193 conf->radius_das_shared_secret_len;
1194 das_conf.client_addr = &conf->radius_das_client_addr;
1195 das_conf.time_window = conf->radius_das_time_window;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001196 das_conf.require_event_timestamp =
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001197 conf->radius_das_require_event_timestamp;
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07001198 das_conf.require_message_authenticator =
1199 conf->radius_das_require_message_authenticator;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001200 das_conf.ctx = hapd;
1201 das_conf.disconnect = hostapd_das_disconnect;
Roshan Pius3a1667e2018-07-03 15:17:14 -07001202 das_conf.coa = hostapd_das_coa;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001203 hapd->radius_das = radius_das_init(&das_conf);
1204 if (hapd->radius_das == NULL) {
1205 wpa_printf(MSG_ERROR, "RADIUS DAS initialization "
1206 "failed.");
1207 return -1;
1208 }
1209 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001210#endif /* CONFIG_NO_RADIUS */
1211
1212 if (hostapd_acl_init(hapd)) {
1213 wpa_printf(MSG_ERROR, "ACL initialization failed.");
1214 return -1;
1215 }
1216 if (hostapd_init_wps(hapd, conf))
1217 return -1;
1218
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001219#ifdef CONFIG_DPP
1220 hapd->gas = gas_query_ap_init(hapd, hapd->msg_ctx);
1221 if (!hapd->gas)
1222 return -1;
1223 if (hostapd_dpp_init(hapd))
1224 return -1;
1225#endif /* CONFIG_DPP */
1226
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001227 if (authsrv_init(hapd) < 0)
1228 return -1;
1229
1230 if (ieee802_1x_init(hapd)) {
1231 wpa_printf(MSG_ERROR, "IEEE 802.1X initialization failed.");
1232 return -1;
1233 }
1234
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001235 if ((conf->wpa || conf->osen) && hostapd_setup_wpa(hapd))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001236 return -1;
1237
1238 if (accounting_init(hapd)) {
1239 wpa_printf(MSG_ERROR, "Accounting initialization failed.");
1240 return -1;
1241 }
1242
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001243 if (conf->ieee802_11f &&
1244 (hapd->iapp = iapp_init(hapd, conf->iapp_iface)) == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001245 wpa_printf(MSG_ERROR, "IEEE 802.11F (IAPP) initialization "
1246 "failed.");
1247 return -1;
1248 }
1249
Dmitry Shmidt04949592012-07-19 12:16:46 -07001250#ifdef CONFIG_INTERWORKING
1251 if (gas_serv_init(hapd)) {
1252 wpa_printf(MSG_ERROR, "GAS server initialization failed");
1253 return -1;
1254 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001255
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001256 if (conf->qos_map_set_len &&
1257 hostapd_drv_set_qos_map(hapd, conf->qos_map_set,
1258 conf->qos_map_set_len)) {
1259 wpa_printf(MSG_ERROR, "Failed to initialize QoS Map");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001260 return -1;
1261 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001262#endif /* CONFIG_INTERWORKING */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001263
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001264 if (conf->bss_load_update_period && bss_load_update_init(hapd)) {
1265 wpa_printf(MSG_ERROR, "BSS Load initialization failed");
1266 return -1;
1267 }
1268
1269 if (conf->proxy_arp) {
1270 if (x_snoop_init(hapd)) {
1271 wpa_printf(MSG_ERROR,
1272 "Generic snooping infrastructure initialization failed");
1273 return -1;
1274 }
1275
1276 if (dhcp_snoop_init(hapd)) {
1277 wpa_printf(MSG_ERROR,
1278 "DHCP snooping initialization failed");
1279 return -1;
1280 }
1281
1282 if (ndisc_snoop_init(hapd)) {
1283 wpa_printf(MSG_ERROR,
1284 "Neighbor Discovery snooping initialization failed");
1285 return -1;
1286 }
1287 }
1288
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001289 if (!hostapd_drv_none(hapd) && vlan_init(hapd)) {
1290 wpa_printf(MSG_ERROR, "VLAN initialization failed.");
1291 return -1;
1292 }
1293
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001294 if (!conf->start_disabled && ieee802_11_set_beacon(hapd) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001295 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001296
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001297 if (hapd->wpa_auth && wpa_init_keys(hapd->wpa_auth) < 0)
1298 return -1;
1299
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001300 if (hapd->driver && hapd->driver->set_operstate)
1301 hapd->driver->set_operstate(hapd->drv_priv, 1);
1302
1303 return 0;
1304}
1305
1306
1307static void hostapd_tx_queue_params(struct hostapd_iface *iface)
1308{
1309 struct hostapd_data *hapd = iface->bss[0];
1310 int i;
1311 struct hostapd_tx_queue_params *p;
1312
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001313#ifdef CONFIG_MESH
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001314 if ((hapd->conf->mesh & MESH_ENABLED) && iface->mconf == NULL)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001315 return;
1316#endif /* CONFIG_MESH */
1317
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001318 for (i = 0; i < NUM_TX_QUEUES; i++) {
1319 p = &iface->conf->tx_queue[i];
1320
1321 if (hostapd_set_tx_queue_params(hapd, i, p->aifs, p->cwmin,
1322 p->cwmax, p->burst)) {
1323 wpa_printf(MSG_DEBUG, "Failed to set TX queue "
1324 "parameters for queue %d.", i);
1325 /* Continue anyway */
1326 }
1327 }
1328}
1329
1330
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001331static int hostapd_set_acl_list(struct hostapd_data *hapd,
1332 struct mac_acl_entry *mac_acl,
1333 int n_entries, u8 accept_acl)
1334{
1335 struct hostapd_acl_params *acl_params;
1336 int i, err;
1337
1338 acl_params = os_zalloc(sizeof(*acl_params) +
1339 (n_entries * sizeof(acl_params->mac_acl[0])));
1340 if (!acl_params)
1341 return -ENOMEM;
1342
1343 for (i = 0; i < n_entries; i++)
1344 os_memcpy(acl_params->mac_acl[i].addr, mac_acl[i].addr,
1345 ETH_ALEN);
1346
1347 acl_params->acl_policy = accept_acl;
1348 acl_params->num_mac_acl = n_entries;
1349
1350 err = hostapd_drv_set_acl(hapd, acl_params);
1351
1352 os_free(acl_params);
1353
1354 return err;
1355}
1356
1357
1358static void hostapd_set_acl(struct hostapd_data *hapd)
1359{
1360 struct hostapd_config *conf = hapd->iconf;
1361 int err;
1362 u8 accept_acl;
1363
1364 if (hapd->iface->drv_max_acl_mac_addrs == 0)
1365 return;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001366
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001367 if (conf->bss[0]->macaddr_acl == DENY_UNLESS_ACCEPTED) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001368 accept_acl = 1;
1369 err = hostapd_set_acl_list(hapd, conf->bss[0]->accept_mac,
1370 conf->bss[0]->num_accept_mac,
1371 accept_acl);
1372 if (err) {
1373 wpa_printf(MSG_DEBUG, "Failed to set accept acl");
1374 return;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001375 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001376 } else if (conf->bss[0]->macaddr_acl == ACCEPT_UNLESS_DENIED) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001377 accept_acl = 0;
1378 err = hostapd_set_acl_list(hapd, conf->bss[0]->deny_mac,
1379 conf->bss[0]->num_deny_mac,
1380 accept_acl);
1381 if (err) {
1382 wpa_printf(MSG_DEBUG, "Failed to set deny acl");
1383 return;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001384 }
1385 }
1386}
1387
1388
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001389static int start_ctrl_iface_bss(struct hostapd_data *hapd)
1390{
1391 if (!hapd->iface->interfaces ||
1392 !hapd->iface->interfaces->ctrl_iface_init)
1393 return 0;
1394
1395 if (hapd->iface->interfaces->ctrl_iface_init(hapd)) {
1396 wpa_printf(MSG_ERROR,
1397 "Failed to setup control interface for %s",
1398 hapd->conf->iface);
1399 return -1;
1400 }
1401
1402 return 0;
1403}
1404
1405
1406static int start_ctrl_iface(struct hostapd_iface *iface)
1407{
1408 size_t i;
1409
1410 if (!iface->interfaces || !iface->interfaces->ctrl_iface_init)
1411 return 0;
1412
1413 for (i = 0; i < iface->num_bss; i++) {
1414 struct hostapd_data *hapd = iface->bss[i];
1415 if (iface->interfaces->ctrl_iface_init(hapd)) {
1416 wpa_printf(MSG_ERROR,
1417 "Failed to setup control interface for %s",
1418 hapd->conf->iface);
1419 return -1;
1420 }
1421 }
1422
1423 return 0;
1424}
1425
1426
1427static void channel_list_update_timeout(void *eloop_ctx, void *timeout_ctx)
1428{
1429 struct hostapd_iface *iface = eloop_ctx;
1430
1431 if (!iface->wait_channel_update) {
1432 wpa_printf(MSG_INFO, "Channel list update timeout, but interface was not waiting for it");
1433 return;
1434 }
1435
1436 /*
1437 * It is possible that the existing channel list is acceptable, so try
1438 * to proceed.
1439 */
1440 wpa_printf(MSG_DEBUG, "Channel list update timeout - try to continue anyway");
1441 setup_interface2(iface);
1442}
1443
1444
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001445void hostapd_channel_list_updated(struct hostapd_iface *iface, int initiator)
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001446{
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001447 if (!iface->wait_channel_update || initiator != REGDOM_SET_BY_USER)
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001448 return;
1449
1450 wpa_printf(MSG_DEBUG, "Channel list updated - continue setup");
1451 eloop_cancel_timeout(channel_list_update_timeout, iface, NULL);
1452 setup_interface2(iface);
1453}
1454
1455
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001456static int setup_interface(struct hostapd_iface *iface)
1457{
1458 struct hostapd_data *hapd = iface->bss[0];
1459 size_t i;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001460
Dmitry Shmidta38abf92014-03-06 13:38:44 -08001461 /*
1462 * It is possible that setup_interface() is called after the interface
1463 * was disabled etc., in which case driver_ap_teardown is possibly set
1464 * to 1. Clear it here so any other key/station deletion, which is not
1465 * part of a teardown flow, would also call the relevant driver
1466 * callbacks.
1467 */
1468 iface->driver_ap_teardown = 0;
1469
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001470 if (!iface->phy[0]) {
1471 const char *phy = hostapd_drv_get_radio_name(hapd);
1472 if (phy) {
1473 wpa_printf(MSG_DEBUG, "phy: %s", phy);
1474 os_strlcpy(iface->phy, phy, sizeof(iface->phy));
1475 }
1476 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001477
1478 /*
1479 * Make sure that all BSSes get configured with a pointer to the same
1480 * driver interface.
1481 */
1482 for (i = 1; i < iface->num_bss; i++) {
1483 iface->bss[i]->driver = hapd->driver;
1484 iface->bss[i]->drv_priv = hapd->drv_priv;
1485 }
1486
1487 if (hostapd_validate_bssid_configuration(iface))
1488 return -1;
1489
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001490 /*
1491 * Initialize control interfaces early to allow external monitoring of
1492 * channel setup operations that may take considerable amount of time
1493 * especially for DFS cases.
1494 */
1495 if (start_ctrl_iface(iface))
1496 return -1;
1497
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001498 if (hapd->iconf->country[0] && hapd->iconf->country[1]) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001499 char country[4], previous_country[4];
1500
1501 hostapd_set_state(iface, HAPD_IFACE_COUNTRY_UPDATE);
1502 if (hostapd_get_country(hapd, previous_country) < 0)
1503 previous_country[0] = '\0';
1504
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001505 os_memcpy(country, hapd->iconf->country, 3);
1506 country[3] = '\0';
1507 if (hostapd_set_country(hapd, country) < 0) {
1508 wpa_printf(MSG_ERROR, "Failed to set country code");
1509 return -1;
1510 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001511
1512 wpa_printf(MSG_DEBUG, "Previous country code %s, new country code %s",
1513 previous_country, country);
1514
1515 if (os_strncmp(previous_country, country, 2) != 0) {
1516 wpa_printf(MSG_DEBUG, "Continue interface setup after channel list update");
1517 iface->wait_channel_update = 1;
Dmitry Shmidt97672262014-02-03 13:02:54 -08001518 eloop_register_timeout(5, 0,
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001519 channel_list_update_timeout,
1520 iface, NULL);
1521 return 0;
1522 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001523 }
1524
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001525 return setup_interface2(iface);
1526}
1527
1528
1529static int setup_interface2(struct hostapd_iface *iface)
1530{
1531 iface->wait_channel_update = 0;
1532
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001533 if (hostapd_get_hw_features(iface)) {
1534 /* Not all drivers support this yet, so continue without hw
1535 * feature data. */
1536 } else {
1537 int ret = hostapd_select_hw_mode(iface);
1538 if (ret < 0) {
1539 wpa_printf(MSG_ERROR, "Could not select hw_mode and "
1540 "channel. (%d)", ret);
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001541 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001542 }
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001543 if (ret == 1) {
1544 wpa_printf(MSG_DEBUG, "Interface initialization will be completed in a callback (ACS)");
1545 return 0;
1546 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001547 ret = hostapd_check_ht_capab(iface);
1548 if (ret < 0)
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001549 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001550 if (ret == 1) {
1551 wpa_printf(MSG_DEBUG, "Interface initialization will "
1552 "be completed in a callback");
1553 return 0;
1554 }
Dmitry Shmidt051af732013-10-22 13:52:46 -07001555
1556 if (iface->conf->ieee80211h)
1557 wpa_printf(MSG_DEBUG, "DFS support is enabled");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001558 }
1559 return hostapd_setup_interface_complete(iface, 0);
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001560
1561fail:
1562 hostapd_set_state(iface, HAPD_IFACE_DISABLED);
1563 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
1564 if (iface->interfaces && iface->interfaces->terminate_on_error)
1565 eloop_terminate();
1566 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001567}
1568
1569
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001570#ifdef CONFIG_FST
1571
1572static const u8 * fst_hostapd_get_bssid_cb(void *ctx)
1573{
1574 struct hostapd_data *hapd = ctx;
1575
1576 return hapd->own_addr;
1577}
1578
1579
1580static void fst_hostapd_get_channel_info_cb(void *ctx,
1581 enum hostapd_hw_mode *hw_mode,
1582 u8 *channel)
1583{
1584 struct hostapd_data *hapd = ctx;
1585
1586 *hw_mode = ieee80211_freq_to_chan(hapd->iface->freq, channel);
1587}
1588
1589
1590static void fst_hostapd_set_ies_cb(void *ctx, const struct wpabuf *fst_ies)
1591{
1592 struct hostapd_data *hapd = ctx;
1593
1594 if (hapd->iface->fst_ies != fst_ies) {
1595 hapd->iface->fst_ies = fst_ies;
1596 if (ieee802_11_set_beacon(hapd))
1597 wpa_printf(MSG_WARNING, "FST: Cannot set beacon");
1598 }
1599}
1600
1601
1602static int fst_hostapd_send_action_cb(void *ctx, const u8 *da,
1603 struct wpabuf *buf)
1604{
1605 struct hostapd_data *hapd = ctx;
1606
1607 return hostapd_drv_send_action(hapd, hapd->iface->freq, 0, da,
1608 wpabuf_head(buf), wpabuf_len(buf));
1609}
1610
1611
1612static const struct wpabuf * fst_hostapd_get_mb_ie_cb(void *ctx, const u8 *addr)
1613{
1614 struct hostapd_data *hapd = ctx;
1615 struct sta_info *sta = ap_get_sta(hapd, addr);
1616
1617 return sta ? sta->mb_ies : NULL;
1618}
1619
1620
1621static void fst_hostapd_update_mb_ie_cb(void *ctx, const u8 *addr,
1622 const u8 *buf, size_t size)
1623{
1624 struct hostapd_data *hapd = ctx;
1625 struct sta_info *sta = ap_get_sta(hapd, addr);
1626
1627 if (sta) {
1628 struct mb_ies_info info;
1629
1630 if (!mb_ies_info_by_ies(&info, buf, size)) {
1631 wpabuf_free(sta->mb_ies);
1632 sta->mb_ies = mb_ies_by_info(&info);
1633 }
1634 }
1635}
1636
1637
1638static const u8 * fst_hostapd_get_sta(struct fst_get_peer_ctx **get_ctx,
1639 Boolean mb_only)
1640{
1641 struct sta_info *s = (struct sta_info *) *get_ctx;
1642
1643 if (mb_only) {
1644 for (; s && !s->mb_ies; s = s->next)
1645 ;
1646 }
1647
1648 if (s) {
1649 *get_ctx = (struct fst_get_peer_ctx *) s->next;
1650
1651 return s->addr;
1652 }
1653
1654 *get_ctx = NULL;
1655 return NULL;
1656}
1657
1658
1659static const u8 * fst_hostapd_get_peer_first(void *ctx,
1660 struct fst_get_peer_ctx **get_ctx,
1661 Boolean mb_only)
1662{
1663 struct hostapd_data *hapd = ctx;
1664
1665 *get_ctx = (struct fst_get_peer_ctx *) hapd->sta_list;
1666
1667 return fst_hostapd_get_sta(get_ctx, mb_only);
1668}
1669
1670
1671static const u8 * fst_hostapd_get_peer_next(void *ctx,
1672 struct fst_get_peer_ctx **get_ctx,
1673 Boolean mb_only)
1674{
1675 return fst_hostapd_get_sta(get_ctx, mb_only);
1676}
1677
1678
1679void fst_hostapd_fill_iface_obj(struct hostapd_data *hapd,
1680 struct fst_wpa_obj *iface_obj)
1681{
1682 iface_obj->ctx = hapd;
1683 iface_obj->get_bssid = fst_hostapd_get_bssid_cb;
1684 iface_obj->get_channel_info = fst_hostapd_get_channel_info_cb;
1685 iface_obj->set_ies = fst_hostapd_set_ies_cb;
1686 iface_obj->send_action = fst_hostapd_send_action_cb;
1687 iface_obj->get_mb_ie = fst_hostapd_get_mb_ie_cb;
1688 iface_obj->update_mb_ie = fst_hostapd_update_mb_ie_cb;
1689 iface_obj->get_peer_first = fst_hostapd_get_peer_first;
1690 iface_obj->get_peer_next = fst_hostapd_get_peer_next;
1691}
1692
1693#endif /* CONFIG_FST */
1694
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001695#ifdef CONFIG_OWE
1696
1697static int hostapd_owe_iface_iter(struct hostapd_iface *iface, void *ctx)
1698{
1699 struct hostapd_data *hapd = ctx;
1700 size_t i;
1701
1702 for (i = 0; i < iface->num_bss; i++) {
1703 struct hostapd_data *bss = iface->bss[i];
1704
1705 if (os_strcmp(hapd->conf->owe_transition_ifname,
1706 bss->conf->iface) != 0)
1707 continue;
1708
1709 wpa_printf(MSG_DEBUG,
1710 "OWE: ifname=%s found transition mode ifname=%s BSSID "
1711 MACSTR " SSID %s",
1712 hapd->conf->iface, bss->conf->iface,
1713 MAC2STR(bss->own_addr),
1714 wpa_ssid_txt(bss->conf->ssid.ssid,
1715 bss->conf->ssid.ssid_len));
1716 if (!bss->conf->ssid.ssid_set || !bss->conf->ssid.ssid_len ||
1717 is_zero_ether_addr(bss->own_addr))
1718 continue;
1719
1720 os_memcpy(hapd->conf->owe_transition_bssid, bss->own_addr,
1721 ETH_ALEN);
1722 os_memcpy(hapd->conf->owe_transition_ssid,
1723 bss->conf->ssid.ssid, bss->conf->ssid.ssid_len);
1724 hapd->conf->owe_transition_ssid_len = bss->conf->ssid.ssid_len;
1725 wpa_printf(MSG_DEBUG,
1726 "OWE: Copied transition mode information");
1727 return 1;
1728 }
1729
1730 return 0;
1731}
1732
1733
1734int hostapd_owe_trans_get_info(struct hostapd_data *hapd)
1735{
1736 if (hapd->conf->owe_transition_ssid_len > 0 &&
1737 !is_zero_ether_addr(hapd->conf->owe_transition_bssid))
1738 return 0;
1739
1740 /* Find transition mode SSID/BSSID information from a BSS operated by
1741 * this hostapd instance. */
1742 if (!hapd->iface->interfaces ||
1743 !hapd->iface->interfaces->for_each_interface)
1744 return hostapd_owe_iface_iter(hapd->iface, hapd);
1745 else
1746 return hapd->iface->interfaces->for_each_interface(
1747 hapd->iface->interfaces, hostapd_owe_iface_iter, hapd);
1748}
1749
1750
1751static int hostapd_owe_iface_iter2(struct hostapd_iface *iface, void *ctx)
1752{
1753 size_t i;
1754
1755 for (i = 0; i < iface->num_bss; i++) {
1756 struct hostapd_data *bss = iface->bss[i];
1757 int res;
1758
1759 if (!bss->conf->owe_transition_ifname[0])
1760 continue;
1761 res = hostapd_owe_trans_get_info(bss);
1762 if (res == 0)
1763 continue;
1764 wpa_printf(MSG_DEBUG,
1765 "OWE: Matching transition mode interface enabled - update beacon data for %s",
1766 bss->conf->iface);
1767 ieee802_11_set_beacon(bss);
1768 }
1769
1770 return 0;
1771}
1772
1773#endif /* CONFIG_OWE */
1774
1775
1776static void hostapd_owe_update_trans(struct hostapd_iface *iface)
1777{
1778#ifdef CONFIG_OWE
1779 /* Check whether the enabled BSS can complete OWE transition mode
1780 * configuration for any pending interface. */
1781 if (!iface->interfaces ||
1782 !iface->interfaces->for_each_interface)
1783 hostapd_owe_iface_iter2(iface, NULL);
1784 else
1785 iface->interfaces->for_each_interface(
1786 iface->interfaces, hostapd_owe_iface_iter2, NULL);
1787#endif /* CONFIG_OWE */
1788}
1789
1790
Roshan Pius3a1667e2018-07-03 15:17:14 -07001791static void hostapd_interface_setup_failure_handler(void *eloop_ctx,
1792 void *timeout_ctx)
1793{
1794 struct hostapd_iface *iface = eloop_ctx;
1795 struct hostapd_data *hapd;
1796
1797 if (iface->num_bss < 1 || !iface->bss || !iface->bss[0])
1798 return;
1799 hapd = iface->bss[0];
1800 if (hapd->setup_complete_cb)
1801 hapd->setup_complete_cb(hapd->setup_complete_cb_ctx);
1802}
1803
1804
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001805static int hostapd_setup_interface_complete_sync(struct hostapd_iface *iface,
1806 int err)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001807{
1808 struct hostapd_data *hapd = iface->bss[0];
1809 size_t j;
1810 u8 *prev_addr;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001811 int delay_apply_cfg = 0;
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001812 int res_dfs_offload = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001813
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001814 if (err)
1815 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001816
1817 wpa_printf(MSG_DEBUG, "Completing interface initialization");
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001818 if (iface->conf->channel) {
Dmitry Shmidt051af732013-10-22 13:52:46 -07001819#ifdef NEED_AP_MLME
1820 int res;
1821#endif /* NEED_AP_MLME */
1822
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001823 iface->freq = hostapd_hw_get_freq(hapd, iface->conf->channel);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001824 wpa_printf(MSG_DEBUG, "Mode: %s Channel: %d "
1825 "Frequency: %d MHz",
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001826 hostapd_hw_mode_txt(iface->conf->hw_mode),
1827 iface->conf->channel, iface->freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001828
Dmitry Shmidt051af732013-10-22 13:52:46 -07001829#ifdef NEED_AP_MLME
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001830 /* Handle DFS only if it is not offloaded to the driver */
1831 if (!(iface->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD)) {
1832 /* Check DFS */
1833 res = hostapd_handle_dfs(iface);
1834 if (res <= 0) {
1835 if (res < 0)
1836 goto fail;
1837 return res;
1838 }
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001839 } else {
1840 /* If DFS is offloaded to the driver */
1841 res_dfs_offload = hostapd_handle_dfs_offload(iface);
1842 if (res_dfs_offload <= 0) {
1843 if (res_dfs_offload < 0)
1844 goto fail;
1845 } else {
1846 wpa_printf(MSG_DEBUG,
1847 "Proceed with AP/channel setup");
1848 /*
1849 * If this is a DFS channel, move to completing
1850 * AP setup.
1851 */
1852 if (res_dfs_offload == 1)
1853 goto dfs_offload;
1854 /* Otherwise fall through. */
1855 }
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001856 }
Dmitry Shmidt051af732013-10-22 13:52:46 -07001857#endif /* NEED_AP_MLME */
1858
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001859#ifdef CONFIG_MESH
1860 if (iface->mconf != NULL) {
1861 wpa_printf(MSG_DEBUG,
1862 "%s: Mesh configuration will be applied while joining the mesh network",
1863 iface->bss[0]->conf->iface);
1864 delay_apply_cfg = 1;
1865 }
1866#endif /* CONFIG_MESH */
1867
1868 if (!delay_apply_cfg &&
1869 hostapd_set_freq(hapd, hapd->iconf->hw_mode, iface->freq,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001870 hapd->iconf->channel,
1871 hapd->iconf->ieee80211n,
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001872 hapd->iconf->ieee80211ac,
Hai Shalom81f62d82019-07-22 12:10:00 -07001873 hapd->iconf->ieee80211ax,
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001874 hapd->iconf->secondary_channel,
Hai Shalom81f62d82019-07-22 12:10:00 -07001875 hostapd_get_oper_chwidth(hapd->iconf),
1876 hostapd_get_oper_centr_freq_seg0_idx(
1877 hapd->iconf),
1878 hostapd_get_oper_centr_freq_seg1_idx(
1879 hapd->iconf))) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001880 wpa_printf(MSG_ERROR, "Could not set channel for "
1881 "kernel driver");
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001882 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001883 }
1884 }
1885
1886 if (iface->current_mode) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001887 if (hostapd_prepare_rates(iface, iface->current_mode)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001888 wpa_printf(MSG_ERROR, "Failed to prepare rates "
1889 "table.");
1890 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
1891 HOSTAPD_LEVEL_WARNING,
1892 "Failed to prepare rates table.");
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001893 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001894 }
1895 }
1896
Hai Shalom021b0b52019-04-10 11:17:58 -07001897 if (hapd->iconf->rts_threshold >= -1 &&
1898 hostapd_set_rts(hapd, hapd->iconf->rts_threshold) &&
1899 hapd->iconf->rts_threshold >= -1) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001900 wpa_printf(MSG_ERROR, "Could not set RTS threshold for "
1901 "kernel driver");
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001902 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001903 }
1904
Hai Shalom021b0b52019-04-10 11:17:58 -07001905 if (hapd->iconf->fragm_threshold >= -1 &&
1906 hostapd_set_frag(hapd, hapd->iconf->fragm_threshold) &&
1907 hapd->iconf->fragm_threshold != -1) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001908 wpa_printf(MSG_ERROR, "Could not set fragmentation threshold "
1909 "for kernel driver");
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001910 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001911 }
1912
1913 prev_addr = hapd->own_addr;
1914
1915 for (j = 0; j < iface->num_bss; j++) {
1916 hapd = iface->bss[j];
1917 if (j)
1918 os_memcpy(hapd->own_addr, prev_addr, ETH_ALEN);
Dmitry Shmidt71757432014-06-02 13:50:35 -07001919 if (hostapd_setup_bss(hapd, j == 0)) {
Hai Shalom021b0b52019-04-10 11:17:58 -07001920 for (;;) {
Dmitry Shmidt71757432014-06-02 13:50:35 -07001921 hapd = iface->bss[j];
1922 hostapd_bss_deinit_no_free(hapd);
1923 hostapd_free_hapd_data(hapd);
Hai Shalom021b0b52019-04-10 11:17:58 -07001924 if (j == 0)
1925 break;
1926 j--;
1927 }
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001928 goto fail;
Dmitry Shmidt71757432014-06-02 13:50:35 -07001929 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001930 if (is_zero_ether_addr(hapd->conf->bssid))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001931 prev_addr = hapd->own_addr;
1932 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001933 hapd = iface->bss[0];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001934
1935 hostapd_tx_queue_params(iface);
1936
1937 ap_list_init(iface);
1938
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001939 hostapd_set_acl(hapd);
1940
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001941 if (hostapd_driver_commit(hapd) < 0) {
1942 wpa_printf(MSG_ERROR, "%s: Failed to commit driver "
1943 "configuration", __func__);
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001944 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001945 }
1946
Jouni Malinen87fd2792011-05-16 18:35:42 +03001947 /*
1948 * WPS UPnP module can be initialized only when the "upnp_iface" is up.
1949 * If "interface" and "upnp_iface" are the same (e.g., non-bridge
1950 * mode), the interface is up only after driver_commit, so initialize
1951 * WPS after driver_commit.
1952 */
1953 for (j = 0; j < iface->num_bss; j++) {
1954 if (hostapd_init_wps_complete(iface->bss[j]))
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001955 goto fail;
Jouni Malinen87fd2792011-05-16 18:35:42 +03001956 }
1957
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001958 if ((iface->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
1959 !res_dfs_offload) {
1960 /*
1961 * If freq is DFS, and DFS is offloaded to the driver, then wait
1962 * for CAC to complete.
1963 */
1964 wpa_printf(MSG_DEBUG, "%s: Wait for CAC to complete", __func__);
1965 return res_dfs_offload;
1966 }
1967
1968#ifdef NEED_AP_MLME
1969dfs_offload:
1970#endif /* NEED_AP_MLME */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001971
1972#ifdef CONFIG_FST
1973 if (hapd->iconf->fst_cfg.group_id[0]) {
1974 struct fst_wpa_obj iface_obj;
1975
1976 fst_hostapd_fill_iface_obj(hapd, &iface_obj);
1977 iface->fst = fst_attach(hapd->conf->iface, hapd->own_addr,
1978 &iface_obj, &hapd->iconf->fst_cfg);
1979 if (!iface->fst) {
1980 wpa_printf(MSG_ERROR, "Could not attach to FST %s",
1981 hapd->iconf->fst_cfg.group_id);
1982 goto fail;
1983 }
1984 }
1985#endif /* CONFIG_FST */
1986
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001987 hostapd_set_state(iface, HAPD_IFACE_ENABLED);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001988 hostapd_owe_update_trans(iface);
Hai Shalom81f62d82019-07-22 12:10:00 -07001989 airtime_policy_update_init(iface);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001990 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_ENABLED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001991 if (hapd->setup_complete_cb)
1992 hapd->setup_complete_cb(hapd->setup_complete_cb_ctx);
1993
1994 wpa_printf(MSG_DEBUG, "%s: Setup of interface done.",
1995 iface->bss[0]->conf->iface);
Dmitry Shmidtb96dad42013-11-05 10:07:29 -08001996 if (iface->interfaces && iface->interfaces->terminate_on_error > 0)
1997 iface->interfaces->terminate_on_error--;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001998
Dmitry Shmidt849734c2016-05-27 09:59:01 -07001999 for (j = 0; j < iface->num_bss; j++)
Hai Shalom74f70d42019-02-11 14:42:39 -08002000 hostapd_neighbor_set_own_report(iface->bss[j]);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002001
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002002 return 0;
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002003
2004fail:
2005 wpa_printf(MSG_ERROR, "Interface initialization failed");
2006 hostapd_set_state(iface, HAPD_IFACE_DISABLED);
2007 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002008#ifdef CONFIG_FST
2009 if (iface->fst) {
2010 fst_detach(iface->fst);
2011 iface->fst = NULL;
2012 }
2013#endif /* CONFIG_FST */
Roshan Pius3a1667e2018-07-03 15:17:14 -07002014
2015 if (iface->interfaces && iface->interfaces->terminate_on_error) {
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002016 eloop_terminate();
Roshan Pius3a1667e2018-07-03 15:17:14 -07002017 } else if (hapd->setup_complete_cb) {
2018 /*
2019 * Calling hapd->setup_complete_cb directly may cause iface
2020 * deinitialization which may be accessed later by the caller.
2021 */
2022 eloop_register_timeout(0, 0,
2023 hostapd_interface_setup_failure_handler,
2024 iface, NULL);
2025 }
2026
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002027 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002028}
2029
2030
2031/**
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002032 * hostapd_setup_interface_complete - Complete interface setup
2033 *
2034 * This function is called when previous steps in the interface setup has been
2035 * completed. This can also start operations, e.g., DFS, that will require
2036 * additional processing before interface is ready to be enabled. Such
2037 * operations will call this function from eloop callbacks when finished.
2038 */
2039int hostapd_setup_interface_complete(struct hostapd_iface *iface, int err)
2040{
2041 struct hapd_interfaces *interfaces = iface->interfaces;
2042 struct hostapd_data *hapd = iface->bss[0];
2043 unsigned int i;
2044 int not_ready_in_sync_ifaces = 0;
2045
2046 if (!iface->need_to_start_in_sync)
2047 return hostapd_setup_interface_complete_sync(iface, err);
2048
2049 if (err) {
2050 wpa_printf(MSG_ERROR, "Interface initialization failed");
2051 hostapd_set_state(iface, HAPD_IFACE_DISABLED);
2052 iface->need_to_start_in_sync = 0;
2053 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
2054 if (interfaces && interfaces->terminate_on_error)
2055 eloop_terminate();
2056 return -1;
2057 }
2058
2059 if (iface->ready_to_start_in_sync) {
2060 /* Already in ready and waiting. should never happpen */
2061 return 0;
2062 }
2063
2064 for (i = 0; i < interfaces->count; i++) {
2065 if (interfaces->iface[i]->need_to_start_in_sync &&
2066 !interfaces->iface[i]->ready_to_start_in_sync)
2067 not_ready_in_sync_ifaces++;
2068 }
2069
2070 /*
2071 * Check if this is the last interface, if yes then start all the other
2072 * waiting interfaces. If not, add this interface to the waiting list.
2073 */
2074 if (not_ready_in_sync_ifaces > 1 && iface->state == HAPD_IFACE_DFS) {
2075 /*
2076 * If this interface went through CAC, do not synchronize, just
2077 * start immediately.
2078 */
2079 iface->need_to_start_in_sync = 0;
2080 wpa_printf(MSG_INFO,
2081 "%s: Finished CAC - bypass sync and start interface",
2082 iface->bss[0]->conf->iface);
2083 return hostapd_setup_interface_complete_sync(iface, err);
2084 }
2085
2086 if (not_ready_in_sync_ifaces > 1) {
2087 /* need to wait as there are other interfaces still coming up */
2088 iface->ready_to_start_in_sync = 1;
2089 wpa_printf(MSG_INFO,
2090 "%s: Interface waiting to sync with other interfaces",
2091 iface->bss[0]->conf->iface);
2092 return 0;
2093 }
2094
2095 wpa_printf(MSG_INFO,
2096 "%s: Last interface to sync - starting all interfaces",
2097 iface->bss[0]->conf->iface);
2098 iface->need_to_start_in_sync = 0;
2099 hostapd_setup_interface_complete_sync(iface, err);
2100 for (i = 0; i < interfaces->count; i++) {
2101 if (interfaces->iface[i]->need_to_start_in_sync &&
2102 interfaces->iface[i]->ready_to_start_in_sync) {
2103 hostapd_setup_interface_complete_sync(
2104 interfaces->iface[i], 0);
2105 /* Only once the interfaces are sync started */
2106 interfaces->iface[i]->need_to_start_in_sync = 0;
2107 }
2108 }
2109
2110 return 0;
2111}
2112
2113
2114/**
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002115 * hostapd_setup_interface - Setup of an interface
2116 * @iface: Pointer to interface data.
2117 * Returns: 0 on success, -1 on failure
2118 *
2119 * Initializes the driver interface, validates the configuration,
2120 * and sets driver parameters based on the configuration.
2121 * Flushes old stations, sets the channel, encryption,
2122 * beacons, and WDS links based on the configuration.
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002123 *
2124 * If interface setup requires more time, e.g., to perform HT co-ex scans, ACS,
2125 * or DFS operations, this function returns 0 before such operations have been
2126 * completed. The pending operations are registered into eloop and will be
2127 * completed from eloop callbacks. Those callbacks end up calling
2128 * hostapd_setup_interface_complete() once setup has been completed.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002129 */
2130int hostapd_setup_interface(struct hostapd_iface *iface)
2131{
2132 int ret;
2133
2134 ret = setup_interface(iface);
2135 if (ret) {
2136 wpa_printf(MSG_ERROR, "%s: Unable to setup interface.",
2137 iface->bss[0]->conf->iface);
2138 return -1;
2139 }
2140
2141 return 0;
2142}
2143
2144
2145/**
2146 * hostapd_alloc_bss_data - Allocate and initialize per-BSS data
2147 * @hapd_iface: Pointer to interface data
2148 * @conf: Pointer to per-interface configuration
2149 * @bss: Pointer to per-BSS configuration for this BSS
2150 * Returns: Pointer to allocated BSS data
2151 *
2152 * This function is used to allocate per-BSS data structure. This data will be
2153 * freed after hostapd_cleanup() is called for it during interface
2154 * deinitialization.
2155 */
2156struct hostapd_data *
2157hostapd_alloc_bss_data(struct hostapd_iface *hapd_iface,
2158 struct hostapd_config *conf,
2159 struct hostapd_bss_config *bss)
2160{
2161 struct hostapd_data *hapd;
2162
2163 hapd = os_zalloc(sizeof(*hapd));
2164 if (hapd == NULL)
2165 return NULL;
2166
2167 hapd->new_assoc_sta_cb = hostapd_new_assoc_sta;
2168 hapd->iconf = conf;
2169 hapd->conf = bss;
2170 hapd->iface = hapd_iface;
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08002171 if (conf)
2172 hapd->driver = conf->driver;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002173 hapd->ctrl_sock = -1;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08002174 dl_list_init(&hapd->ctrl_dst);
Dmitry Shmidt7d175302016-09-06 13:11:34 -07002175 dl_list_init(&hapd->nr_db);
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08002176 hapd->dhcp_sock = -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002177#ifdef CONFIG_IEEE80211R_AP
2178 dl_list_init(&hapd->l2_queue);
2179 dl_list_init(&hapd->l2_oui_queue);
2180#endif /* CONFIG_IEEE80211R_AP */
Hai Shalom021b0b52019-04-10 11:17:58 -07002181#ifdef CONFIG_SAE
2182 dl_list_init(&hapd->sae_commit_queue);
2183#endif /* CONFIG_SAE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002184
2185 return hapd;
2186}
2187
2188
Dmitry Shmidt54605472013-11-08 11:10:19 -08002189static void hostapd_bss_deinit(struct hostapd_data *hapd)
2190{
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07002191 if (!hapd)
2192 return;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002193 wpa_printf(MSG_DEBUG, "%s: deinit bss %s", __func__,
Hai Shalom021b0b52019-04-10 11:17:58 -07002194 hapd->conf ? hapd->conf->iface : "N/A");
Dmitry Shmidt71757432014-06-02 13:50:35 -07002195 hostapd_bss_deinit_no_free(hapd);
Dmitry Shmidtf73259c2015-03-17 11:00:54 -07002196 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
Dmitry Shmidt54605472013-11-08 11:10:19 -08002197 hostapd_cleanup(hapd);
2198}
2199
2200
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002201void hostapd_interface_deinit(struct hostapd_iface *iface)
2202{
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002203 int j;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002204
Dmitry Shmidt54605472013-11-08 11:10:19 -08002205 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002206 if (iface == NULL)
2207 return;
2208
Dmitry Shmidtf73259c2015-03-17 11:00:54 -07002209 hostapd_set_state(iface, HAPD_IFACE_DISABLED);
2210
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002211 eloop_cancel_timeout(channel_list_update_timeout, iface, NULL);
2212 iface->wait_channel_update = 0;
2213
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002214#ifdef CONFIG_FST
2215 if (iface->fst) {
2216 fst_detach(iface->fst);
2217 iface->fst = NULL;
2218 }
2219#endif /* CONFIG_FST */
2220
Hai Shalom021b0b52019-04-10 11:17:58 -07002221 for (j = (int) iface->num_bss - 1; j >= 0; j--) {
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07002222 if (!iface->bss)
2223 break;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002224 hostapd_bss_deinit(iface->bss[j]);
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07002225 }
Roshan Pius3a1667e2018-07-03 15:17:14 -07002226
2227#ifdef CONFIG_IEEE80211N
2228#ifdef NEED_AP_MLME
2229 hostapd_stop_setup_timers(iface);
2230 eloop_cancel_timeout(ap_ht2040_timeout, iface, NULL);
2231#endif /* NEED_AP_MLME */
2232#endif /* CONFIG_IEEE80211N */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002233}
2234
2235
2236void hostapd_interface_free(struct hostapd_iface *iface)
2237{
2238 size_t j;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002239 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
2240 for (j = 0; j < iface->num_bss; j++) {
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07002241 if (!iface->bss)
2242 break;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002243 wpa_printf(MSG_DEBUG, "%s: free hapd %p",
2244 __func__, iface->bss[j]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002245 os_free(iface->bss[j]);
Dmitry Shmidt54605472013-11-08 11:10:19 -08002246 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002247 hostapd_cleanup_iface(iface);
2248}
2249
2250
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07002251struct hostapd_iface * hostapd_alloc_iface(void)
2252{
2253 struct hostapd_iface *hapd_iface;
2254
2255 hapd_iface = os_zalloc(sizeof(*hapd_iface));
2256 if (!hapd_iface)
2257 return NULL;
2258
2259 dl_list_init(&hapd_iface->sta_seen);
2260
2261 return hapd_iface;
2262}
2263
2264
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002265/**
2266 * hostapd_init - Allocate and initialize per-interface data
2267 * @config_file: Path to the configuration file
2268 * Returns: Pointer to the allocated interface data or %NULL on failure
2269 *
2270 * This function is used to allocate main data structures for per-interface
2271 * data. The allocated data buffer will be freed by calling
2272 * hostapd_cleanup_iface().
2273 */
2274struct hostapd_iface * hostapd_init(struct hapd_interfaces *interfaces,
2275 const char *config_file)
2276{
2277 struct hostapd_iface *hapd_iface = NULL;
2278 struct hostapd_config *conf = NULL;
2279 struct hostapd_data *hapd;
2280 size_t i;
2281
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07002282 hapd_iface = hostapd_alloc_iface();
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002283 if (hapd_iface == NULL)
2284 goto fail;
2285
2286 hapd_iface->config_fname = os_strdup(config_file);
2287 if (hapd_iface->config_fname == NULL)
2288 goto fail;
2289
2290 conf = interfaces->config_read_cb(hapd_iface->config_fname);
2291 if (conf == NULL)
2292 goto fail;
2293 hapd_iface->conf = conf;
2294
2295 hapd_iface->num_bss = conf->num_bss;
2296 hapd_iface->bss = os_calloc(conf->num_bss,
2297 sizeof(struct hostapd_data *));
2298 if (hapd_iface->bss == NULL)
2299 goto fail;
2300
2301 for (i = 0; i < conf->num_bss; i++) {
2302 hapd = hapd_iface->bss[i] =
2303 hostapd_alloc_bss_data(hapd_iface, conf,
2304 conf->bss[i]);
2305 if (hapd == NULL)
2306 goto fail;
2307 hapd->msg_ctx = hapd;
2308 }
2309
2310 return hapd_iface;
2311
2312fail:
2313 wpa_printf(MSG_ERROR, "Failed to set up interface with %s",
2314 config_file);
2315 if (conf)
2316 hostapd_config_free(conf);
2317 if (hapd_iface) {
2318 os_free(hapd_iface->config_fname);
2319 os_free(hapd_iface->bss);
Dmitry Shmidt54605472013-11-08 11:10:19 -08002320 wpa_printf(MSG_DEBUG, "%s: free iface %p",
2321 __func__, hapd_iface);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002322 os_free(hapd_iface);
2323 }
2324 return NULL;
2325}
2326
2327
2328static int ifname_in_use(struct hapd_interfaces *interfaces, const char *ifname)
2329{
2330 size_t i, j;
2331
2332 for (i = 0; i < interfaces->count; i++) {
2333 struct hostapd_iface *iface = interfaces->iface[i];
2334 for (j = 0; j < iface->num_bss; j++) {
2335 struct hostapd_data *hapd = iface->bss[j];
2336 if (os_strcmp(ifname, hapd->conf->iface) == 0)
2337 return 1;
2338 }
2339 }
2340
2341 return 0;
2342}
2343
2344
2345/**
2346 * hostapd_interface_init_bss - Read configuration file and init BSS data
2347 *
2348 * This function is used to parse configuration file for a BSS. This BSS is
2349 * added to an existing interface sharing the same radio (if any) or a new
2350 * interface is created if this is the first interface on a radio. This
2351 * allocate memory for the BSS. No actual driver operations are started.
2352 *
2353 * This is similar to hostapd_interface_init(), but for a case where the
2354 * configuration is used to add a single BSS instead of all BSSes for a radio.
2355 */
2356struct hostapd_iface *
2357hostapd_interface_init_bss(struct hapd_interfaces *interfaces, const char *phy,
2358 const char *config_fname, int debug)
2359{
2360 struct hostapd_iface *new_iface = NULL, *iface = NULL;
2361 struct hostapd_data *hapd;
2362 int k;
2363 size_t i, bss_idx;
2364
2365 if (!phy || !*phy)
2366 return NULL;
2367
2368 for (i = 0; i < interfaces->count; i++) {
2369 if (os_strcmp(interfaces->iface[i]->phy, phy) == 0) {
2370 iface = interfaces->iface[i];
2371 break;
2372 }
2373 }
2374
2375 wpa_printf(MSG_INFO, "Configuration file: %s (phy %s)%s",
2376 config_fname, phy, iface ? "" : " --> new PHY");
2377 if (iface) {
2378 struct hostapd_config *conf;
2379 struct hostapd_bss_config **tmp_conf;
2380 struct hostapd_data **tmp_bss;
2381 struct hostapd_bss_config *bss;
2382 const char *ifname;
2383
2384 /* Add new BSS to existing iface */
2385 conf = interfaces->config_read_cb(config_fname);
2386 if (conf == NULL)
2387 return NULL;
2388 if (conf->num_bss > 1) {
2389 wpa_printf(MSG_ERROR, "Multiple BSSes specified in BSS-config");
2390 hostapd_config_free(conf);
2391 return NULL;
2392 }
2393
2394 ifname = conf->bss[0]->iface;
2395 if (ifname[0] != '\0' && ifname_in_use(interfaces, ifname)) {
2396 wpa_printf(MSG_ERROR,
2397 "Interface name %s already in use", ifname);
2398 hostapd_config_free(conf);
2399 return NULL;
2400 }
2401
2402 tmp_conf = os_realloc_array(
2403 iface->conf->bss, iface->conf->num_bss + 1,
2404 sizeof(struct hostapd_bss_config *));
2405 tmp_bss = os_realloc_array(iface->bss, iface->num_bss + 1,
2406 sizeof(struct hostapd_data *));
2407 if (tmp_bss)
2408 iface->bss = tmp_bss;
2409 if (tmp_conf) {
2410 iface->conf->bss = tmp_conf;
2411 iface->conf->last_bss = tmp_conf[0];
2412 }
2413 if (tmp_bss == NULL || tmp_conf == NULL) {
2414 hostapd_config_free(conf);
2415 return NULL;
2416 }
2417 bss = iface->conf->bss[iface->conf->num_bss] = conf->bss[0];
2418 iface->conf->num_bss++;
2419
2420 hapd = hostapd_alloc_bss_data(iface, iface->conf, bss);
2421 if (hapd == NULL) {
2422 iface->conf->num_bss--;
2423 hostapd_config_free(conf);
2424 return NULL;
2425 }
2426 iface->conf->last_bss = bss;
2427 iface->bss[iface->num_bss] = hapd;
2428 hapd->msg_ctx = hapd;
2429
2430 bss_idx = iface->num_bss++;
2431 conf->num_bss--;
2432 conf->bss[0] = NULL;
2433 hostapd_config_free(conf);
2434 } else {
2435 /* Add a new iface with the first BSS */
2436 new_iface = iface = hostapd_init(interfaces, config_fname);
2437 if (!iface)
2438 return NULL;
2439 os_strlcpy(iface->phy, phy, sizeof(iface->phy));
2440 iface->interfaces = interfaces;
2441 bss_idx = 0;
2442 }
2443
2444 for (k = 0; k < debug; k++) {
2445 if (iface->bss[bss_idx]->conf->logger_stdout_level > 0)
2446 iface->bss[bss_idx]->conf->logger_stdout_level--;
2447 }
2448
2449 if (iface->conf->bss[bss_idx]->iface[0] == '\0' &&
2450 !hostapd_drv_none(iface->bss[bss_idx])) {
2451 wpa_printf(MSG_ERROR, "Interface name not specified in %s",
2452 config_fname);
2453 if (new_iface)
2454 hostapd_interface_deinit_free(new_iface);
2455 return NULL;
2456 }
2457
2458 return iface;
2459}
2460
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002461
2462void hostapd_interface_deinit_free(struct hostapd_iface *iface)
2463{
2464 const struct wpa_driver_ops *driver;
2465 void *drv_priv;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002466
2467 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002468 if (iface == NULL)
2469 return;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002470 wpa_printf(MSG_DEBUG, "%s: num_bss=%u conf->num_bss=%u",
2471 __func__, (unsigned int) iface->num_bss,
2472 (unsigned int) iface->conf->num_bss);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002473 driver = iface->bss[0]->driver;
2474 drv_priv = iface->bss[0]->drv_priv;
2475 hostapd_interface_deinit(iface);
Dmitry Shmidt54605472013-11-08 11:10:19 -08002476 wpa_printf(MSG_DEBUG, "%s: driver=%p drv_priv=%p -> hapd_deinit",
2477 __func__, driver, drv_priv);
Dmitry Shmidt71757432014-06-02 13:50:35 -07002478 if (driver && driver->hapd_deinit && drv_priv) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002479 driver->hapd_deinit(drv_priv);
Dmitry Shmidt71757432014-06-02 13:50:35 -07002480 iface->bss[0]->drv_priv = NULL;
2481 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002482 hostapd_interface_free(iface);
2483}
2484
2485
Dmitry Shmidt15907092014-03-25 10:42:57 -07002486static void hostapd_deinit_driver(const struct wpa_driver_ops *driver,
2487 void *drv_priv,
2488 struct hostapd_iface *hapd_iface)
2489{
2490 size_t j;
2491
2492 wpa_printf(MSG_DEBUG, "%s: driver=%p drv_priv=%p -> hapd_deinit",
2493 __func__, driver, drv_priv);
2494 if (driver && driver->hapd_deinit && drv_priv) {
2495 driver->hapd_deinit(drv_priv);
2496 for (j = 0; j < hapd_iface->num_bss; j++) {
2497 wpa_printf(MSG_DEBUG, "%s:bss[%d]->drv_priv=%p",
2498 __func__, (int) j,
2499 hapd_iface->bss[j]->drv_priv);
Hai Shalom1dc4d202019-04-29 16:22:27 -07002500 if (hapd_iface->bss[j]->drv_priv == drv_priv) {
Dmitry Shmidt15907092014-03-25 10:42:57 -07002501 hapd_iface->bss[j]->drv_priv = NULL;
Hai Shalom1dc4d202019-04-29 16:22:27 -07002502 hapd_iface->extended_capa = NULL;
2503 hapd_iface->extended_capa_mask = NULL;
2504 hapd_iface->extended_capa_len = 0;
2505 }
Dmitry Shmidt15907092014-03-25 10:42:57 -07002506 }
2507 }
2508}
2509
2510
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002511int hostapd_enable_iface(struct hostapd_iface *hapd_iface)
2512{
Dmitry Shmidt71757432014-06-02 13:50:35 -07002513 size_t j;
2514
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002515 if (hapd_iface->bss[0]->drv_priv != NULL) {
2516 wpa_printf(MSG_ERROR, "Interface %s already enabled",
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002517 hapd_iface->conf->bss[0]->iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002518 return -1;
2519 }
2520
2521 wpa_printf(MSG_DEBUG, "Enable interface %s",
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002522 hapd_iface->conf->bss[0]->iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002523
Dmitry Shmidt71757432014-06-02 13:50:35 -07002524 for (j = 0; j < hapd_iface->num_bss; j++)
2525 hostapd_set_security_params(hapd_iface->conf->bss[j], 1);
Dmitry Shmidt344abd32014-01-14 13:17:00 -08002526 if (hostapd_config_check(hapd_iface->conf, 1) < 0) {
2527 wpa_printf(MSG_INFO, "Invalid configuration - cannot enable");
2528 return -1;
2529 }
2530
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002531 if (hapd_iface->interfaces == NULL ||
2532 hapd_iface->interfaces->driver_init == NULL ||
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002533 hapd_iface->interfaces->driver_init(hapd_iface))
2534 return -1;
2535
2536 if (hostapd_setup_interface(hapd_iface)) {
Dmitry Shmidt15907092014-03-25 10:42:57 -07002537 hostapd_deinit_driver(hapd_iface->bss[0]->driver,
2538 hapd_iface->bss[0]->drv_priv,
2539 hapd_iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002540 return -1;
2541 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002542
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002543 return 0;
2544}
2545
2546
2547int hostapd_reload_iface(struct hostapd_iface *hapd_iface)
2548{
2549 size_t j;
2550
2551 wpa_printf(MSG_DEBUG, "Reload interface %s",
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002552 hapd_iface->conf->bss[0]->iface);
2553 for (j = 0; j < hapd_iface->num_bss; j++)
Dmitry Shmidt71757432014-06-02 13:50:35 -07002554 hostapd_set_security_params(hapd_iface->conf->bss[j], 1);
Dmitry Shmidt344abd32014-01-14 13:17:00 -08002555 if (hostapd_config_check(hapd_iface->conf, 1) < 0) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002556 wpa_printf(MSG_ERROR, "Updated configuration is invalid");
2557 return -1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002558 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002559 hostapd_clear_old(hapd_iface);
2560 for (j = 0; j < hapd_iface->num_bss; j++)
2561 hostapd_reload_bss(hapd_iface->bss[j]);
2562
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002563 return 0;
2564}
2565
2566
2567int hostapd_disable_iface(struct hostapd_iface *hapd_iface)
2568{
2569 size_t j;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002570 const struct wpa_driver_ops *driver;
2571 void *drv_priv;
2572
2573 if (hapd_iface == NULL)
2574 return -1;
Dmitry Shmidt71757432014-06-02 13:50:35 -07002575
2576 if (hapd_iface->bss[0]->drv_priv == NULL) {
2577 wpa_printf(MSG_INFO, "Interface %s already disabled",
2578 hapd_iface->conf->bss[0]->iface);
2579 return -1;
2580 }
2581
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002582 wpa_msg(hapd_iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002583 driver = hapd_iface->bss[0]->driver;
2584 drv_priv = hapd_iface->bss[0]->drv_priv;
2585
Dmitry Shmidta38abf92014-03-06 13:38:44 -08002586 hapd_iface->driver_ap_teardown =
2587 !!(hapd_iface->drv_flags &
2588 WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
2589
Hai Shalom39ba6fc2019-01-22 12:40:38 -08002590#ifdef NEED_AP_MLME
2591 for (j = 0; j < hapd_iface->num_bss; j++)
2592 hostapd_cleanup_cs_params(hapd_iface->bss[j]);
2593#endif /* NEED_AP_MLME */
2594
Dmitry Shmidta38abf92014-03-06 13:38:44 -08002595 /* same as hostapd_interface_deinit without deinitializing ctrl-iface */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002596 for (j = 0; j < hapd_iface->num_bss; j++) {
2597 struct hostapd_data *hapd = hapd_iface->bss[j];
Dmitry Shmidt71757432014-06-02 13:50:35 -07002598 hostapd_bss_deinit_no_free(hapd);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002599 hostapd_free_hapd_data(hapd);
2600 }
2601
Dmitry Shmidt15907092014-03-25 10:42:57 -07002602 hostapd_deinit_driver(driver, drv_priv, hapd_iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002603
2604 /* From hostapd_cleanup_iface: These were initialized in
2605 * hostapd_setup_interface and hostapd_setup_interface_complete
2606 */
2607 hostapd_cleanup_iface_partial(hapd_iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002608
Dmitry Shmidt56052862013-10-04 10:23:25 -07002609 wpa_printf(MSG_DEBUG, "Interface %s disabled",
2610 hapd_iface->bss[0]->conf->iface);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002611 hostapd_set_state(hapd_iface, HAPD_IFACE_DISABLED);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002612 return 0;
2613}
2614
2615
2616static struct hostapd_iface *
2617hostapd_iface_alloc(struct hapd_interfaces *interfaces)
2618{
2619 struct hostapd_iface **iface, *hapd_iface;
2620
2621 iface = os_realloc_array(interfaces->iface, interfaces->count + 1,
2622 sizeof(struct hostapd_iface *));
2623 if (iface == NULL)
2624 return NULL;
2625 interfaces->iface = iface;
2626 hapd_iface = interfaces->iface[interfaces->count] =
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07002627 hostapd_alloc_iface();
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002628 if (hapd_iface == NULL) {
2629 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory for "
2630 "the interface", __func__);
2631 return NULL;
2632 }
2633 interfaces->count++;
2634 hapd_iface->interfaces = interfaces;
2635
2636 return hapd_iface;
2637}
2638
2639
2640static struct hostapd_config *
2641hostapd_config_alloc(struct hapd_interfaces *interfaces, const char *ifname,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002642 const char *ctrl_iface, const char *driver)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002643{
2644 struct hostapd_bss_config *bss;
2645 struct hostapd_config *conf;
2646
2647 /* Allocates memory for bss and conf */
2648 conf = hostapd_config_defaults();
2649 if (conf == NULL) {
2650 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory for "
2651 "configuration", __func__);
Hai Shalom74f70d42019-02-11 14:42:39 -08002652 return NULL;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002653 }
2654
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002655 if (driver) {
2656 int j;
2657
2658 for (j = 0; wpa_drivers[j]; j++) {
2659 if (os_strcmp(driver, wpa_drivers[j]->name) == 0) {
2660 conf->driver = wpa_drivers[j];
2661 goto skip;
2662 }
2663 }
2664
2665 wpa_printf(MSG_ERROR,
2666 "Invalid/unknown driver '%s' - registering the default driver",
2667 driver);
2668 }
2669
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002670 conf->driver = wpa_drivers[0];
2671 if (conf->driver == NULL) {
2672 wpa_printf(MSG_ERROR, "No driver wrappers registered!");
2673 hostapd_config_free(conf);
2674 return NULL;
2675 }
2676
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002677skip:
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002678 bss = conf->last_bss = conf->bss[0];
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002679
2680 os_strlcpy(bss->iface, ifname, sizeof(bss->iface));
2681 bss->ctrl_interface = os_strdup(ctrl_iface);
2682 if (bss->ctrl_interface == NULL) {
2683 hostapd_config_free(conf);
2684 return NULL;
2685 }
2686
2687 /* Reading configuration file skipped, will be done in SET!
2688 * From reading the configuration till the end has to be done in
2689 * SET
2690 */
2691 return conf;
2692}
2693
2694
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002695static int hostapd_data_alloc(struct hostapd_iface *hapd_iface,
2696 struct hostapd_config *conf)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002697{
2698 size_t i;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002699 struct hostapd_data *hapd;
2700
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002701 hapd_iface->bss = os_calloc(conf->num_bss,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002702 sizeof(struct hostapd_data *));
2703 if (hapd_iface->bss == NULL)
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002704 return -1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002705
2706 for (i = 0; i < conf->num_bss; i++) {
2707 hapd = hapd_iface->bss[i] =
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002708 hostapd_alloc_bss_data(hapd_iface, conf, conf->bss[i]);
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002709 if (hapd == NULL) {
2710 while (i > 0) {
2711 i--;
2712 os_free(hapd_iface->bss[i]);
2713 hapd_iface->bss[i] = NULL;
2714 }
2715 os_free(hapd_iface->bss);
2716 hapd_iface->bss = NULL;
2717 return -1;
2718 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002719 hapd->msg_ctx = hapd;
2720 }
2721
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002722 hapd_iface->conf = conf;
2723 hapd_iface->num_bss = conf->num_bss;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002724
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002725 return 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002726}
2727
2728
2729int hostapd_add_iface(struct hapd_interfaces *interfaces, char *buf)
2730{
2731 struct hostapd_config *conf = NULL;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002732 struct hostapd_iface *hapd_iface = NULL, *new_iface = NULL;
2733 struct hostapd_data *hapd;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002734 char *ptr;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002735 size_t i, j;
2736 const char *conf_file = NULL, *phy_name = NULL;
2737
2738 if (os_strncmp(buf, "bss_config=", 11) == 0) {
2739 char *pos;
2740 phy_name = buf + 11;
2741 pos = os_strchr(phy_name, ':');
2742 if (!pos)
2743 return -1;
2744 *pos++ = '\0';
2745 conf_file = pos;
2746 if (!os_strlen(conf_file))
2747 return -1;
2748
2749 hapd_iface = hostapd_interface_init_bss(interfaces, phy_name,
2750 conf_file, 0);
2751 if (!hapd_iface)
2752 return -1;
2753 for (j = 0; j < interfaces->count; j++) {
2754 if (interfaces->iface[j] == hapd_iface)
2755 break;
2756 }
2757 if (j == interfaces->count) {
2758 struct hostapd_iface **tmp;
2759 tmp = os_realloc_array(interfaces->iface,
2760 interfaces->count + 1,
2761 sizeof(struct hostapd_iface *));
2762 if (!tmp) {
2763 hostapd_interface_deinit_free(hapd_iface);
2764 return -1;
2765 }
2766 interfaces->iface = tmp;
2767 interfaces->iface[interfaces->count++] = hapd_iface;
2768 new_iface = hapd_iface;
2769 }
2770
2771 if (new_iface) {
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002772 if (interfaces->driver_init(hapd_iface))
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002773 goto fail;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002774
2775 if (hostapd_setup_interface(hapd_iface)) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002776 hostapd_deinit_driver(
2777 hapd_iface->bss[0]->driver,
2778 hapd_iface->bss[0]->drv_priv,
2779 hapd_iface);
2780 goto fail;
2781 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002782 } else {
2783 /* Assign new BSS with bss[0]'s driver info */
2784 hapd = hapd_iface->bss[hapd_iface->num_bss - 1];
2785 hapd->driver = hapd_iface->bss[0]->driver;
2786 hapd->drv_priv = hapd_iface->bss[0]->drv_priv;
2787 os_memcpy(hapd->own_addr, hapd_iface->bss[0]->own_addr,
2788 ETH_ALEN);
2789
2790 if (start_ctrl_iface_bss(hapd) < 0 ||
Dmitry Shmidt54605472013-11-08 11:10:19 -08002791 (hapd_iface->state == HAPD_IFACE_ENABLED &&
2792 hostapd_setup_bss(hapd, -1))) {
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002793 hostapd_cleanup(hapd);
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08002794 hapd_iface->bss[hapd_iface->num_bss - 1] = NULL;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002795 hapd_iface->conf->num_bss--;
2796 hapd_iface->num_bss--;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002797 wpa_printf(MSG_DEBUG, "%s: free hapd %p %s",
2798 __func__, hapd, hapd->conf->iface);
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002799 hostapd_config_free_bss(hapd->conf);
2800 hapd->conf = NULL;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002801 os_free(hapd);
2802 return -1;
2803 }
2804 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002805 hostapd_owe_update_trans(hapd_iface);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002806 return 0;
2807 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002808
2809 ptr = os_strchr(buf, ' ');
2810 if (ptr == NULL)
2811 return -1;
2812 *ptr++ = '\0';
2813
Dmitry Shmidt56052862013-10-04 10:23:25 -07002814 if (os_strncmp(ptr, "config=", 7) == 0)
2815 conf_file = ptr + 7;
2816
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002817 for (i = 0; i < interfaces->count; i++) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002818 if (!os_strcmp(interfaces->iface[i]->conf->bss[0]->iface,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002819 buf)) {
2820 wpa_printf(MSG_INFO, "Cannot add interface - it "
2821 "already exists");
2822 return -1;
2823 }
2824 }
2825
2826 hapd_iface = hostapd_iface_alloc(interfaces);
2827 if (hapd_iface == NULL) {
2828 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
2829 "for interface", __func__);
2830 goto fail;
2831 }
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002832 new_iface = hapd_iface;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002833
Dmitry Shmidt56052862013-10-04 10:23:25 -07002834 if (conf_file && interfaces->config_read_cb) {
2835 conf = interfaces->config_read_cb(conf_file);
2836 if (conf && conf->bss)
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002837 os_strlcpy(conf->bss[0]->iface, buf,
2838 sizeof(conf->bss[0]->iface));
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002839 } else {
2840 char *driver = os_strchr(ptr, ' ');
2841
2842 if (driver)
2843 *driver++ = '\0';
2844 conf = hostapd_config_alloc(interfaces, buf, ptr, driver);
2845 }
2846
Dmitry Shmidt56052862013-10-04 10:23:25 -07002847 if (conf == NULL || conf->bss == NULL) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002848 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
2849 "for configuration", __func__);
2850 goto fail;
2851 }
2852
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002853 if (hostapd_data_alloc(hapd_iface, conf) < 0) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002854 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
2855 "for hostapd", __func__);
2856 goto fail;
2857 }
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002858 conf = NULL;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002859
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002860 if (start_ctrl_iface(hapd_iface) < 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002861 goto fail;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002862
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002863 wpa_printf(MSG_INFO, "Add interface '%s'",
2864 hapd_iface->conf->bss[0]->iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002865
2866 return 0;
2867
2868fail:
2869 if (conf)
2870 hostapd_config_free(conf);
2871 if (hapd_iface) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002872 if (hapd_iface->bss) {
Dmitry Shmidt54605472013-11-08 11:10:19 -08002873 for (i = 0; i < hapd_iface->num_bss; i++) {
2874 hapd = hapd_iface->bss[i];
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08002875 if (!hapd)
2876 continue;
2877 if (hapd_iface->interfaces &&
Dmitry Shmidt54605472013-11-08 11:10:19 -08002878 hapd_iface->interfaces->ctrl_iface_deinit)
2879 hapd_iface->interfaces->
2880 ctrl_iface_deinit(hapd);
2881 wpa_printf(MSG_DEBUG, "%s: free hapd %p (%s)",
2882 __func__, hapd_iface->bss[i],
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08002883 hapd->conf->iface);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002884 hostapd_cleanup(hapd);
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08002885 os_free(hapd);
2886 hapd_iface->bss[i] = NULL;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002887 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002888 os_free(hapd_iface->bss);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002889 hapd_iface->bss = NULL;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002890 }
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002891 if (new_iface) {
2892 interfaces->count--;
2893 interfaces->iface[interfaces->count] = NULL;
2894 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002895 hostapd_cleanup_iface(hapd_iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002896 }
2897 return -1;
2898}
2899
2900
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002901static int hostapd_remove_bss(struct hostapd_iface *iface, unsigned int idx)
2902{
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002903 size_t i;
2904
Dmitry Shmidt54605472013-11-08 11:10:19 -08002905 wpa_printf(MSG_INFO, "Remove BSS '%s'", iface->conf->bss[idx]->iface);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002906
Dmitry Shmidt54605472013-11-08 11:10:19 -08002907 /* Remove hostapd_data only if it has already been initialized */
2908 if (idx < iface->num_bss) {
2909 struct hostapd_data *hapd = iface->bss[idx];
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002910
Dmitry Shmidt54605472013-11-08 11:10:19 -08002911 hostapd_bss_deinit(hapd);
2912 wpa_printf(MSG_DEBUG, "%s: free hapd %p (%s)",
2913 __func__, hapd, hapd->conf->iface);
2914 hostapd_config_free_bss(hapd->conf);
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002915 hapd->conf = NULL;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002916 os_free(hapd);
2917
2918 iface->num_bss--;
2919
2920 for (i = idx; i < iface->num_bss; i++)
2921 iface->bss[i] = iface->bss[i + 1];
2922 } else {
2923 hostapd_config_free_bss(iface->conf->bss[idx]);
2924 iface->conf->bss[idx] = NULL;
2925 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002926
2927 iface->conf->num_bss--;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002928 for (i = idx; i < iface->conf->num_bss; i++)
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002929 iface->conf->bss[i] = iface->conf->bss[i + 1];
2930
2931 return 0;
2932}
2933
2934
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002935int hostapd_remove_iface(struct hapd_interfaces *interfaces, char *buf)
2936{
2937 struct hostapd_iface *hapd_iface;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002938 size_t i, j, k = 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002939
2940 for (i = 0; i < interfaces->count; i++) {
2941 hapd_iface = interfaces->iface[i];
2942 if (hapd_iface == NULL)
2943 return -1;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002944 if (!os_strcmp(hapd_iface->conf->bss[0]->iface, buf)) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002945 wpa_printf(MSG_INFO, "Remove interface '%s'", buf);
Dmitry Shmidta38abf92014-03-06 13:38:44 -08002946 hapd_iface->driver_ap_teardown =
2947 !!(hapd_iface->drv_flags &
2948 WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
2949
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002950 hostapd_interface_deinit_free(hapd_iface);
2951 k = i;
2952 while (k < (interfaces->count - 1)) {
2953 interfaces->iface[k] =
2954 interfaces->iface[k + 1];
2955 k++;
2956 }
2957 interfaces->count--;
2958 return 0;
2959 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002960
2961 for (j = 0; j < hapd_iface->conf->num_bss; j++) {
Dmitry Shmidta38abf92014-03-06 13:38:44 -08002962 if (!os_strcmp(hapd_iface->conf->bss[j]->iface, buf)) {
2963 hapd_iface->driver_ap_teardown =
2964 !(hapd_iface->drv_flags &
2965 WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002966 return hostapd_remove_bss(hapd_iface, j);
Dmitry Shmidta38abf92014-03-06 13:38:44 -08002967 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002968 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002969 }
2970 return -1;
2971}
2972
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002973
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002974/**
2975 * hostapd_new_assoc_sta - Notify that a new station associated with the AP
2976 * @hapd: Pointer to BSS data
2977 * @sta: Pointer to the associated STA data
2978 * @reassoc: 1 to indicate this was a re-association; 0 = first association
2979 *
2980 * This function will be called whenever a station associates with the AP. It
2981 * can be called from ieee802_11.c for drivers that export MLME to hostapd and
2982 * from drv_callbacks.c based on driver events for drivers that take care of
2983 * management frames (IEEE 802.11 authentication and association) internally.
2984 */
2985void hostapd_new_assoc_sta(struct hostapd_data *hapd, struct sta_info *sta,
2986 int reassoc)
2987{
2988 if (hapd->tkip_countermeasures) {
2989 hostapd_drv_sta_deauth(hapd, sta->addr,
2990 WLAN_REASON_MICHAEL_MIC_FAILURE);
2991 return;
2992 }
2993
2994 hostapd_prune_associations(hapd, sta->addr);
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -08002995 ap_sta_clear_disconnect_timeouts(hapd, sta);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002996
2997 /* IEEE 802.11F (IAPP) */
2998 if (hapd->conf->ieee802_11f)
2999 iapp_new_station(hapd->iapp, sta);
3000
3001#ifdef CONFIG_P2P
3002 if (sta->p2p_ie == NULL && !sta->no_p2p_set) {
3003 sta->no_p2p_set = 1;
3004 hapd->num_sta_no_p2p++;
3005 if (hapd->num_sta_no_p2p == 1)
3006 hostapd_p2p_non_p2p_sta_connected(hapd);
3007 }
3008#endif /* CONFIG_P2P */
3009
Hai Shalom81f62d82019-07-22 12:10:00 -07003010 airtime_policy_new_sta(hapd, sta);
3011
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003012 /* Start accounting here, if IEEE 802.1X and WPA are not used.
3013 * IEEE 802.1X/WPA code will start accounting after the station has
3014 * been authorized. */
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -08003015 if (!hapd->conf->ieee802_1x && !hapd->conf->wpa && !hapd->conf->osen) {
3016 ap_sta_set_authorized(hapd, sta, 1);
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08003017 os_get_reltime(&sta->connected_time);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003018 accounting_sta_start(hapd, sta);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003019 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003020
3021 /* Start IEEE 802.1X authentication process for new stations */
3022 ieee802_1x_new_station(hapd, sta);
3023 if (reassoc) {
3024 if (sta->auth_alg != WLAN_AUTH_FT &&
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003025 sta->auth_alg != WLAN_AUTH_FILS_SK &&
3026 sta->auth_alg != WLAN_AUTH_FILS_SK_PFS &&
3027 sta->auth_alg != WLAN_AUTH_FILS_PK &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003028 !(sta->flags & (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS)))
3029 wpa_auth_sm_event(sta->wpa_sm, WPA_REAUTH);
3030 } else
3031 wpa_auth_sta_associated(hapd->wpa_auth, sta->wpa_sm);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003032
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08003033 if (hapd->iface->drv_flags & WPA_DRIVER_FLAGS_WIRED) {
3034 if (eloop_cancel_timeout(ap_handle_timer, hapd, sta) > 0) {
3035 wpa_printf(MSG_DEBUG,
3036 "%s: %s: canceled wired ap_handle_timer timeout for "
3037 MACSTR,
3038 hapd->conf->iface, __func__,
3039 MAC2STR(sta->addr));
3040 }
3041 } else if (!(hapd->iface->drv_flags &
3042 WPA_DRIVER_FLAGS_INACTIVITY_TIMER)) {
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -08003043 wpa_printf(MSG_DEBUG,
3044 "%s: %s: reschedule ap_handle_timer timeout for "
3045 MACSTR " (%d seconds - ap_max_inactivity)",
3046 hapd->conf->iface, __func__, MAC2STR(sta->addr),
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08003047 hapd->conf->ap_max_inactivity);
3048 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
3049 eloop_register_timeout(hapd->conf->ap_max_inactivity, 0,
3050 ap_handle_timer, hapd, sta);
3051 }
Hai Shalom81f62d82019-07-22 12:10:00 -07003052
3053#ifdef CONFIG_MACSEC
3054 if (hapd->conf->wpa_key_mgmt == WPA_KEY_MGMT_NONE &&
3055 hapd->conf->mka_psk_set)
3056 ieee802_1x_create_preshared_mka_hapd(hapd, sta);
3057 else
3058 ieee802_1x_alloc_kay_sm_hapd(hapd, sta);
3059#endif /* CONFIG_MACSEC */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003060}
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003061
3062
3063const char * hostapd_state_text(enum hostapd_iface_state s)
3064{
3065 switch (s) {
3066 case HAPD_IFACE_UNINITIALIZED:
3067 return "UNINITIALIZED";
3068 case HAPD_IFACE_DISABLED:
3069 return "DISABLED";
3070 case HAPD_IFACE_COUNTRY_UPDATE:
3071 return "COUNTRY_UPDATE";
3072 case HAPD_IFACE_ACS:
3073 return "ACS";
3074 case HAPD_IFACE_HT_SCAN:
3075 return "HT_SCAN";
3076 case HAPD_IFACE_DFS:
3077 return "DFS";
3078 case HAPD_IFACE_ENABLED:
3079 return "ENABLED";
3080 }
3081
3082 return "UNKNOWN";
3083}
3084
3085
3086void hostapd_set_state(struct hostapd_iface *iface, enum hostapd_iface_state s)
3087{
3088 wpa_printf(MSG_INFO, "%s: interface state %s->%s",
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07003089 iface->conf ? iface->conf->bss[0]->iface : "N/A",
3090 hostapd_state_text(iface->state), hostapd_state_text(s));
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003091 iface->state = s;
3092}
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003093
3094
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003095int hostapd_csa_in_progress(struct hostapd_iface *iface)
3096{
3097 unsigned int i;
3098
3099 for (i = 0; i < iface->num_bss; i++)
3100 if (iface->bss[i]->csa_in_progress)
3101 return 1;
3102 return 0;
3103}
3104
3105
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003106#ifdef NEED_AP_MLME
3107
3108static void free_beacon_data(struct beacon_data *beacon)
3109{
3110 os_free(beacon->head);
3111 beacon->head = NULL;
3112 os_free(beacon->tail);
3113 beacon->tail = NULL;
3114 os_free(beacon->probe_resp);
3115 beacon->probe_resp = NULL;
3116 os_free(beacon->beacon_ies);
3117 beacon->beacon_ies = NULL;
3118 os_free(beacon->proberesp_ies);
3119 beacon->proberesp_ies = NULL;
3120 os_free(beacon->assocresp_ies);
3121 beacon->assocresp_ies = NULL;
3122}
3123
3124
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003125static int hostapd_build_beacon_data(struct hostapd_data *hapd,
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003126 struct beacon_data *beacon)
3127{
3128 struct wpabuf *beacon_extra, *proberesp_extra, *assocresp_extra;
3129 struct wpa_driver_ap_params params;
3130 int ret;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003131
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08003132 os_memset(beacon, 0, sizeof(*beacon));
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003133 ret = ieee802_11_build_ap_params(hapd, &params);
3134 if (ret < 0)
3135 return ret;
3136
3137 ret = hostapd_build_ap_extra_ies(hapd, &beacon_extra,
3138 &proberesp_extra,
3139 &assocresp_extra);
3140 if (ret)
3141 goto free_ap_params;
3142
3143 ret = -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003144 beacon->head = os_memdup(params.head, params.head_len);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003145 if (!beacon->head)
3146 goto free_ap_extra_ies;
3147
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003148 beacon->head_len = params.head_len;
3149
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003150 beacon->tail = os_memdup(params.tail, params.tail_len);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003151 if (!beacon->tail)
3152 goto free_beacon;
3153
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003154 beacon->tail_len = params.tail_len;
3155
3156 if (params.proberesp != NULL) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003157 beacon->probe_resp = os_memdup(params.proberesp,
3158 params.proberesp_len);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003159 if (!beacon->probe_resp)
3160 goto free_beacon;
3161
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003162 beacon->probe_resp_len = params.proberesp_len;
3163 }
3164
3165 /* copy the extra ies */
3166 if (beacon_extra) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003167 beacon->beacon_ies = os_memdup(beacon_extra->buf,
3168 wpabuf_len(beacon_extra));
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003169 if (!beacon->beacon_ies)
3170 goto free_beacon;
3171
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003172 beacon->beacon_ies_len = wpabuf_len(beacon_extra);
3173 }
3174
3175 if (proberesp_extra) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003176 beacon->proberesp_ies = os_memdup(proberesp_extra->buf,
3177 wpabuf_len(proberesp_extra));
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003178 if (!beacon->proberesp_ies)
3179 goto free_beacon;
3180
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003181 beacon->proberesp_ies_len = wpabuf_len(proberesp_extra);
3182 }
3183
3184 if (assocresp_extra) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003185 beacon->assocresp_ies = os_memdup(assocresp_extra->buf,
3186 wpabuf_len(assocresp_extra));
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003187 if (!beacon->assocresp_ies)
3188 goto free_beacon;
3189
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003190 beacon->assocresp_ies_len = wpabuf_len(assocresp_extra);
3191 }
3192
3193 ret = 0;
3194free_beacon:
3195 /* if the function fails, the caller should not free beacon data */
3196 if (ret)
3197 free_beacon_data(beacon);
3198
3199free_ap_extra_ies:
3200 hostapd_free_ap_extra_ies(hapd, beacon_extra, proberesp_extra,
3201 assocresp_extra);
3202free_ap_params:
3203 ieee802_11_free_ap_params(&params);
3204 return ret;
3205}
3206
3207
3208/*
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003209 * TODO: This flow currently supports only changing channel and width within
3210 * the same hw_mode. Any other changes to MAC parameters or provided settings
3211 * are not supported.
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003212 */
3213static int hostapd_change_config_freq(struct hostapd_data *hapd,
3214 struct hostapd_config *conf,
3215 struct hostapd_freq_params *params,
3216 struct hostapd_freq_params *old_params)
3217{
3218 int channel;
Hai Shalom81f62d82019-07-22 12:10:00 -07003219 u8 seg0, seg1;
3220 struct hostapd_hw_modes *mode;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003221
3222 if (!params->channel) {
3223 /* check if the new channel is supported by hw */
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003224 params->channel = hostapd_hw_get_channel(hapd, params->freq);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003225 }
3226
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003227 channel = params->channel;
3228 if (!channel)
3229 return -1;
3230
Hai Shalom81f62d82019-07-22 12:10:00 -07003231 mode = hapd->iface->current_mode;
3232
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003233 /* if a pointer to old_params is provided we save previous state */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003234 if (old_params &&
3235 hostapd_set_freq_params(old_params, conf->hw_mode,
3236 hostapd_hw_get_freq(hapd, conf->channel),
3237 conf->channel, conf->ieee80211n,
Hai Shalom81f62d82019-07-22 12:10:00 -07003238 conf->ieee80211ac, conf->ieee80211ax,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003239 conf->secondary_channel,
Hai Shalom81f62d82019-07-22 12:10:00 -07003240 hostapd_get_oper_chwidth(conf),
3241 hostapd_get_oper_centr_freq_seg0_idx(conf),
3242 hostapd_get_oper_centr_freq_seg1_idx(conf),
3243 conf->vht_capab,
3244 mode ? &mode->he_capab[IEEE80211_MODE_AP] :
3245 NULL))
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003246 return -1;
3247
3248 switch (params->bandwidth) {
3249 case 0:
3250 case 20:
3251 case 40:
Hai Shalom81f62d82019-07-22 12:10:00 -07003252 hostapd_set_oper_chwidth(conf, CHANWIDTH_USE_HT);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003253 break;
3254 case 80:
3255 if (params->center_freq2)
Hai Shalom81f62d82019-07-22 12:10:00 -07003256 hostapd_set_oper_chwidth(conf, CHANWIDTH_80P80MHZ);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003257 else
Hai Shalom81f62d82019-07-22 12:10:00 -07003258 hostapd_set_oper_chwidth(conf, CHANWIDTH_80MHZ);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003259 break;
3260 case 160:
Hai Shalom81f62d82019-07-22 12:10:00 -07003261 hostapd_set_oper_chwidth(conf, CHANWIDTH_160MHZ);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003262 break;
3263 default:
3264 return -1;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003265 }
3266
3267 conf->channel = channel;
3268 conf->ieee80211n = params->ht_enabled;
3269 conf->secondary_channel = params->sec_channel_offset;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003270 ieee80211_freq_to_chan(params->center_freq1,
Hai Shalom81f62d82019-07-22 12:10:00 -07003271 &seg0);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003272 ieee80211_freq_to_chan(params->center_freq2,
Hai Shalom81f62d82019-07-22 12:10:00 -07003273 &seg1);
3274 hostapd_set_oper_centr_freq_seg0_idx(conf, seg0);
3275 hostapd_set_oper_centr_freq_seg1_idx(conf, seg1);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003276
3277 /* TODO: maybe call here hostapd_config_check here? */
3278
3279 return 0;
3280}
3281
3282
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003283static int hostapd_fill_csa_settings(struct hostapd_data *hapd,
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003284 struct csa_settings *settings)
3285{
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003286 struct hostapd_iface *iface = hapd->iface;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003287 struct hostapd_freq_params old_freq;
3288 int ret;
Hai Shalom81f62d82019-07-22 12:10:00 -07003289 u8 chan, bandwidth;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003290
3291 os_memset(&old_freq, 0, sizeof(old_freq));
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003292 if (!iface || !iface->freq || hapd->csa_in_progress)
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003293 return -1;
3294
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003295 switch (settings->freq_params.bandwidth) {
3296 case 80:
3297 if (settings->freq_params.center_freq2)
Hai Shalom81f62d82019-07-22 12:10:00 -07003298 bandwidth = CHANWIDTH_80P80MHZ;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003299 else
Hai Shalom81f62d82019-07-22 12:10:00 -07003300 bandwidth = CHANWIDTH_80MHZ;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003301 break;
3302 case 160:
Hai Shalom81f62d82019-07-22 12:10:00 -07003303 bandwidth = CHANWIDTH_160MHZ;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003304 break;
3305 default:
Hai Shalom81f62d82019-07-22 12:10:00 -07003306 bandwidth = CHANWIDTH_USE_HT;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003307 break;
3308 }
3309
3310 if (ieee80211_freq_to_channel_ext(
3311 settings->freq_params.freq,
3312 settings->freq_params.sec_channel_offset,
Hai Shalom81f62d82019-07-22 12:10:00 -07003313 bandwidth,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003314 &hapd->iface->cs_oper_class,
3315 &chan) == NUM_HOSTAPD_MODES) {
3316 wpa_printf(MSG_DEBUG,
Hai Shalom81f62d82019-07-22 12:10:00 -07003317 "invalid frequency for channel switch (freq=%d, sec_channel_offset=%d, vht_enabled=%d, he_enabled=%d)",
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003318 settings->freq_params.freq,
3319 settings->freq_params.sec_channel_offset,
Hai Shalom81f62d82019-07-22 12:10:00 -07003320 settings->freq_params.vht_enabled,
3321 settings->freq_params.he_enabled);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003322 return -1;
3323 }
3324
3325 settings->freq_params.channel = chan;
3326
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003327 ret = hostapd_change_config_freq(iface->bss[0], iface->conf,
3328 &settings->freq_params,
3329 &old_freq);
3330 if (ret)
3331 return ret;
3332
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003333 ret = hostapd_build_beacon_data(hapd, &settings->beacon_after);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003334
3335 /* change back the configuration */
3336 hostapd_change_config_freq(iface->bss[0], iface->conf,
3337 &old_freq, NULL);
3338
3339 if (ret)
3340 return ret;
3341
3342 /* set channel switch parameters for csa ie */
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003343 hapd->cs_freq_params = settings->freq_params;
3344 hapd->cs_count = settings->cs_count;
3345 hapd->cs_block_tx = settings->block_tx;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003346
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003347 ret = hostapd_build_beacon_data(hapd, &settings->beacon_csa);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003348 if (ret) {
3349 free_beacon_data(&settings->beacon_after);
3350 return ret;
3351 }
3352
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003353 settings->counter_offset_beacon[0] = hapd->cs_c_off_beacon;
3354 settings->counter_offset_presp[0] = hapd->cs_c_off_proberesp;
3355 settings->counter_offset_beacon[1] = hapd->cs_c_off_ecsa_beacon;
3356 settings->counter_offset_presp[1] = hapd->cs_c_off_ecsa_proberesp;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003357
3358 return 0;
3359}
3360
3361
3362void hostapd_cleanup_cs_params(struct hostapd_data *hapd)
3363{
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003364 os_memset(&hapd->cs_freq_params, 0, sizeof(hapd->cs_freq_params));
3365 hapd->cs_count = 0;
3366 hapd->cs_block_tx = 0;
3367 hapd->cs_c_off_beacon = 0;
3368 hapd->cs_c_off_proberesp = 0;
3369 hapd->csa_in_progress = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003370 hapd->cs_c_off_ecsa_beacon = 0;
3371 hapd->cs_c_off_ecsa_proberesp = 0;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003372}
3373
3374
Roshan Pius3a1667e2018-07-03 15:17:14 -07003375void hostapd_chan_switch_vht_config(struct hostapd_data *hapd, int vht_enabled)
3376{
3377 if (vht_enabled)
3378 hapd->iconf->ch_switch_vht_config |= CH_SWITCH_VHT_ENABLED;
3379 else
3380 hapd->iconf->ch_switch_vht_config |= CH_SWITCH_VHT_DISABLED;
3381
3382 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
3383 HOSTAPD_LEVEL_INFO, "CHAN_SWITCH VHT CONFIG 0x%x",
3384 hapd->iconf->ch_switch_vht_config);
3385}
3386
3387
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003388int hostapd_switch_channel(struct hostapd_data *hapd,
3389 struct csa_settings *settings)
3390{
3391 int ret;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003392
3393 if (!(hapd->iface->drv_flags & WPA_DRIVER_FLAGS_AP_CSA)) {
3394 wpa_printf(MSG_INFO, "CSA is not supported");
3395 return -1;
3396 }
3397
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003398 ret = hostapd_fill_csa_settings(hapd, settings);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003399 if (ret)
3400 return ret;
3401
3402 ret = hostapd_drv_switch_channel(hapd, settings);
3403 free_beacon_data(&settings->beacon_csa);
3404 free_beacon_data(&settings->beacon_after);
3405
3406 if (ret) {
3407 /* if we failed, clean cs parameters */
3408 hostapd_cleanup_cs_params(hapd);
3409 return ret;
3410 }
3411
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003412 hapd->csa_in_progress = 1;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003413 return 0;
3414}
3415
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003416
3417void
3418hostapd_switch_channel_fallback(struct hostapd_iface *iface,
3419 const struct hostapd_freq_params *freq_params)
3420{
Hai Shalom81f62d82019-07-22 12:10:00 -07003421 int seg0_idx = 0, seg1_idx = 0, bw = CHANWIDTH_USE_HT;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003422
3423 wpa_printf(MSG_DEBUG, "Restarting all CSA-related BSSes");
3424
3425 if (freq_params->center_freq1)
Hai Shalom81f62d82019-07-22 12:10:00 -07003426 seg0_idx = 36 + (freq_params->center_freq1 - 5180) / 5;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003427 if (freq_params->center_freq2)
Hai Shalom81f62d82019-07-22 12:10:00 -07003428 seg1_idx = 36 + (freq_params->center_freq2 - 5180) / 5;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003429
3430 switch (freq_params->bandwidth) {
3431 case 0:
3432 case 20:
3433 case 40:
Hai Shalom81f62d82019-07-22 12:10:00 -07003434 bw = CHANWIDTH_USE_HT;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003435 break;
3436 case 80:
3437 if (freq_params->center_freq2)
Hai Shalom81f62d82019-07-22 12:10:00 -07003438 bw = CHANWIDTH_80P80MHZ;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003439 else
Hai Shalom81f62d82019-07-22 12:10:00 -07003440 bw = CHANWIDTH_80MHZ;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003441 break;
3442 case 160:
Hai Shalom81f62d82019-07-22 12:10:00 -07003443 bw = CHANWIDTH_160MHZ;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003444 break;
3445 default:
3446 wpa_printf(MSG_WARNING, "Unknown CSA bandwidth: %d",
3447 freq_params->bandwidth);
3448 break;
3449 }
3450
3451 iface->freq = freq_params->freq;
3452 iface->conf->channel = freq_params->channel;
3453 iface->conf->secondary_channel = freq_params->sec_channel_offset;
Hai Shalom81f62d82019-07-22 12:10:00 -07003454 hostapd_set_oper_centr_freq_seg0_idx(iface->conf, seg0_idx);
3455 hostapd_set_oper_centr_freq_seg1_idx(iface->conf, seg1_idx);
3456 hostapd_set_oper_chwidth(iface->conf, bw);
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003457 iface->conf->ieee80211n = freq_params->ht_enabled;
3458 iface->conf->ieee80211ac = freq_params->vht_enabled;
Hai Shalom81f62d82019-07-22 12:10:00 -07003459 iface->conf->ieee80211ax = freq_params->he_enabled;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003460
3461 /*
3462 * cs_params must not be cleared earlier because the freq_params
3463 * argument may actually point to one of these.
Hai Shalom39ba6fc2019-01-22 12:40:38 -08003464 * These params will be cleared during interface disable below.
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003465 */
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003466 hostapd_disable_iface(iface);
3467 hostapd_enable_iface(iface);
3468}
3469
Dmitry Shmidte4663042016-04-04 10:07:49 -07003470#endif /* NEED_AP_MLME */
3471
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003472
3473struct hostapd_data * hostapd_get_iface(struct hapd_interfaces *interfaces,
3474 const char *ifname)
3475{
3476 size_t i, j;
3477
3478 for (i = 0; i < interfaces->count; i++) {
3479 struct hostapd_iface *iface = interfaces->iface[i];
3480
3481 for (j = 0; j < iface->num_bss; j++) {
3482 struct hostapd_data *hapd = iface->bss[j];
3483
3484 if (os_strcmp(ifname, hapd->conf->iface) == 0)
3485 return hapd;
3486 }
3487 }
3488
3489 return NULL;
3490}
3491
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003492
3493void hostapd_periodic_iface(struct hostapd_iface *iface)
3494{
3495 size_t i;
3496
3497 ap_list_timer(iface);
3498
3499 for (i = 0; i < iface->num_bss; i++) {
3500 struct hostapd_data *hapd = iface->bss[i];
3501
3502 if (!hapd->started)
3503 continue;
3504
3505#ifndef CONFIG_NO_RADIUS
3506 hostapd_acl_expire(hapd);
3507#endif /* CONFIG_NO_RADIUS */
3508 }
3509}