blob: f0955863e74c5f4dc908510b01728d61573c32d0 [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * hostapd / Initialization and configuration
Dmitry Shmidt344abd32014-01-14 13:17:00 -08003 * Copyright (c) 2002-2014, 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"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070053
54
Dmitry Shmidt04949592012-07-19 12:16:46 -070055static int hostapd_flush_old_stations(struct hostapd_data *hapd, u16 reason);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070056static int hostapd_setup_encryption(char *iface, struct hostapd_data *hapd);
Dmitry Shmidtc55524a2011-07-07 11:18:38 -070057static int hostapd_broadcast_wep_clear(struct hostapd_data *hapd);
Dmitry Shmidtcce06662013-11-04 18:44:24 -080058static int setup_interface2(struct hostapd_iface *iface);
59static void channel_list_update_timeout(void *eloop_ctx, void *timeout_ctx);
Roshan Pius3a1667e2018-07-03 15:17:14 -070060static void hostapd_interface_setup_failure_handler(void *eloop_ctx,
61 void *timeout_ctx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070062
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070063
Dmitry Shmidt04949592012-07-19 12:16:46 -070064int hostapd_for_each_interface(struct hapd_interfaces *interfaces,
65 int (*cb)(struct hostapd_iface *iface,
66 void *ctx), void *ctx)
67{
68 size_t i;
69 int ret;
70
71 for (i = 0; i < interfaces->count; i++) {
72 ret = cb(interfaces->iface[i], ctx);
73 if (ret)
74 return ret;
75 }
76
77 return 0;
78}
79
80
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070081static void hostapd_reload_bss(struct hostapd_data *hapd)
82{
Dmitry Shmidtcce06662013-11-04 18:44:24 -080083 struct hostapd_ssid *ssid;
84
Dmitry Shmidt29333592017-01-09 12:27:11 -080085 if (!hapd->started)
86 return;
87
Roshan Pius3a1667e2018-07-03 15:17:14 -070088 if (hapd->conf->wmm_enabled < 0)
89 hapd->conf->wmm_enabled = hapd->iconf->ieee80211n;
90
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070091#ifndef CONFIG_NO_RADIUS
92 radius_client_reconfig(hapd->radius, hapd->conf->radius);
93#endif /* CONFIG_NO_RADIUS */
94
Dmitry Shmidtcce06662013-11-04 18:44:24 -080095 ssid = &hapd->conf->ssid;
96 if (!ssid->wpa_psk_set && ssid->wpa_psk && !ssid->wpa_psk->next &&
97 ssid->wpa_passphrase_set && ssid->wpa_passphrase) {
98 /*
99 * Force PSK to be derived again since SSID or passphrase may
100 * have changed.
101 */
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800102 hostapd_config_clear_wpa_psk(&hapd->conf->ssid.wpa_psk);
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800103 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700104 if (hostapd_setup_wpa_psk(hapd->conf)) {
105 wpa_printf(MSG_ERROR, "Failed to re-configure WPA PSK "
106 "after reloading configuration");
107 }
108
109 if (hapd->conf->ieee802_1x || hapd->conf->wpa)
110 hostapd_set_drv_ieee8021x(hapd, hapd->conf->iface, 1);
111 else
112 hostapd_set_drv_ieee8021x(hapd, hapd->conf->iface, 0);
113
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800114 if ((hapd->conf->wpa || hapd->conf->osen) && hapd->wpa_auth == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700115 hostapd_setup_wpa(hapd);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800116 if (hapd->wpa_auth)
117 wpa_init_keys(hapd->wpa_auth);
118 } else if (hapd->conf->wpa) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700119 const u8 *wpa_ie;
120 size_t wpa_ie_len;
121 hostapd_reconfig_wpa(hapd);
122 wpa_ie = wpa_auth_get_wpa_ie(hapd->wpa_auth, &wpa_ie_len);
123 if (hostapd_set_generic_elem(hapd, wpa_ie, wpa_ie_len))
124 wpa_printf(MSG_ERROR, "Failed to configure WPA IE for "
125 "the kernel driver.");
126 } else if (hapd->wpa_auth) {
127 wpa_deinit(hapd->wpa_auth);
128 hapd->wpa_auth = NULL;
129 hostapd_set_privacy(hapd, 0);
130 hostapd_setup_encryption(hapd->conf->iface, hapd);
131 hostapd_set_generic_elem(hapd, (u8 *) "", 0);
132 }
133
134 ieee802_11_set_beacon(hapd);
135 hostapd_update_wps(hapd);
136
137 if (hapd->conf->ssid.ssid_set &&
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700138 hostapd_set_ssid(hapd, hapd->conf->ssid.ssid,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700139 hapd->conf->ssid.ssid_len)) {
140 wpa_printf(MSG_ERROR, "Could not set SSID for kernel driver");
141 /* try to continue */
142 }
143 wpa_printf(MSG_DEBUG, "Reconfigured interface %s", hapd->conf->iface);
144}
145
146
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700147static void hostapd_clear_old(struct hostapd_iface *iface)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700148{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700149 size_t j;
150
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700151 /*
152 * Deauthenticate all stations since the new configuration may not
153 * allow them to use the BSS anymore.
154 */
155 for (j = 0; j < iface->num_bss; j++) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700156 hostapd_flush_old_stations(iface->bss[j],
157 WLAN_REASON_PREV_AUTH_NOT_VALID);
Dmitry Shmidtc55524a2011-07-07 11:18:38 -0700158 hostapd_broadcast_wep_clear(iface->bss[j]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700159
160#ifndef CONFIG_NO_RADIUS
161 /* TODO: update dynamic data based on changed configuration
162 * items (e.g., open/close sockets, etc.) */
163 radius_client_flush(iface->bss[j]->radius, 0);
164#endif /* CONFIG_NO_RADIUS */
165 }
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700166}
167
168
169int hostapd_reload_config(struct hostapd_iface *iface)
170{
171 struct hostapd_data *hapd = iface->bss[0];
172 struct hostapd_config *newconf, *oldconf;
173 size_t j;
174
175 if (iface->config_fname == NULL) {
176 /* Only in-memory config in use - assume it has been updated */
177 hostapd_clear_old(iface);
178 for (j = 0; j < iface->num_bss; j++)
179 hostapd_reload_bss(iface->bss[j]);
180 return 0;
181 }
182
183 if (iface->interfaces == NULL ||
184 iface->interfaces->config_read_cb == NULL)
185 return -1;
186 newconf = iface->interfaces->config_read_cb(iface->config_fname);
187 if (newconf == NULL)
188 return -1;
189
190 hostapd_clear_old(iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700191
192 oldconf = hapd->iconf;
193 iface->conf = newconf;
194
195 for (j = 0; j < iface->num_bss; j++) {
196 hapd = iface->bss[j];
197 hapd->iconf = newconf;
Dmitry Shmidtd11f0192014-03-24 12:09:47 -0700198 hapd->iconf->channel = oldconf->channel;
Dmitry Shmidtdda10c22015-03-24 16:05:01 -0700199 hapd->iconf->acs = oldconf->acs;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -0700200 hapd->iconf->secondary_channel = oldconf->secondary_channel;
Dmitry Shmidtd11f0192014-03-24 12:09:47 -0700201 hapd->iconf->ieee80211n = oldconf->ieee80211n;
202 hapd->iconf->ieee80211ac = oldconf->ieee80211ac;
203 hapd->iconf->ht_capab = oldconf->ht_capab;
204 hapd->iconf->vht_capab = oldconf->vht_capab;
205 hapd->iconf->vht_oper_chwidth = oldconf->vht_oper_chwidth;
206 hapd->iconf->vht_oper_centr_freq_seg0_idx =
207 oldconf->vht_oper_centr_freq_seg0_idx;
208 hapd->iconf->vht_oper_centr_freq_seg1_idx =
209 oldconf->vht_oper_centr_freq_seg1_idx;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800210 hapd->conf = newconf->bss[j];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700211 hostapd_reload_bss(hapd);
212 }
213
214 hostapd_config_free(oldconf);
215
216
217 return 0;
218}
219
220
221static void hostapd_broadcast_key_clear_iface(struct hostapd_data *hapd,
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700222 const char *ifname)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700223{
224 int i;
225
Dmitry Shmidt29333592017-01-09 12:27:11 -0800226 if (!ifname || !hapd->drv_priv)
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700227 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700228 for (i = 0; i < NUM_WEP_KEYS; i++) {
229 if (hostapd_drv_set_key(ifname, hapd, WPA_ALG_NONE, NULL, i,
230 0, NULL, 0, NULL, 0)) {
231 wpa_printf(MSG_DEBUG, "Failed to clear default "
232 "encryption keys (ifname=%s keyidx=%d)",
233 ifname, i);
234 }
235 }
236#ifdef CONFIG_IEEE80211W
237 if (hapd->conf->ieee80211w) {
238 for (i = NUM_WEP_KEYS; i < NUM_WEP_KEYS + 2; i++) {
239 if (hostapd_drv_set_key(ifname, hapd, WPA_ALG_NONE,
240 NULL, i, 0, NULL,
241 0, NULL, 0)) {
242 wpa_printf(MSG_DEBUG, "Failed to clear "
243 "default mgmt encryption keys "
244 "(ifname=%s keyidx=%d)", ifname, i);
245 }
246 }
247 }
248#endif /* CONFIG_IEEE80211W */
249}
250
251
252static int hostapd_broadcast_wep_clear(struct hostapd_data *hapd)
253{
254 hostapd_broadcast_key_clear_iface(hapd, hapd->conf->iface);
255 return 0;
256}
257
258
259static int hostapd_broadcast_wep_set(struct hostapd_data *hapd)
260{
261 int errors = 0, idx;
262 struct hostapd_ssid *ssid = &hapd->conf->ssid;
263
264 idx = ssid->wep.idx;
265 if (ssid->wep.default_len &&
266 hostapd_drv_set_key(hapd->conf->iface,
267 hapd, WPA_ALG_WEP, broadcast_ether_addr, idx,
268 1, NULL, 0, ssid->wep.key[idx],
269 ssid->wep.len[idx])) {
270 wpa_printf(MSG_WARNING, "Could not set WEP encryption.");
271 errors++;
272 }
273
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700274 return errors;
275}
276
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700277
Dmitry Shmidt04949592012-07-19 12:16:46 -0700278static void hostapd_free_hapd_data(struct hostapd_data *hapd)
279{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800280 os_free(hapd->probereq_cb);
281 hapd->probereq_cb = NULL;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800282 hapd->num_probereq_cb = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800283
284#ifdef CONFIG_P2P
285 wpabuf_free(hapd->p2p_beacon_ie);
286 hapd->p2p_beacon_ie = NULL;
287 wpabuf_free(hapd->p2p_probe_resp_ie);
288 hapd->p2p_probe_resp_ie = NULL;
289#endif /* CONFIG_P2P */
290
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -0800291 if (!hapd->started) {
292 wpa_printf(MSG_ERROR, "%s: Interface %s wasn't started",
293 __func__, hapd->conf->iface);
294 return;
295 }
296 hapd->started = 0;
297
Dmitry Shmidt54605472013-11-08 11:10:19 -0800298 wpa_printf(MSG_DEBUG, "%s(%s)", __func__, hapd->conf->iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700299 iapp_deinit(hapd->iapp);
300 hapd->iapp = NULL;
301 accounting_deinit(hapd);
302 hostapd_deinit_wpa(hapd);
303 vlan_deinit(hapd);
304 hostapd_acl_deinit(hapd);
305#ifndef CONFIG_NO_RADIUS
306 radius_client_deinit(hapd->radius);
307 hapd->radius = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700308 radius_das_deinit(hapd->radius_das);
309 hapd->radius_das = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700310#endif /* CONFIG_NO_RADIUS */
311
312 hostapd_deinit_wps(hapd);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700313#ifdef CONFIG_DPP
314 hostapd_dpp_deinit(hapd);
315 gas_query_ap_deinit(hapd->gas);
316#endif /* CONFIG_DPP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700317
318 authsrv_deinit(hapd);
319
Dmitry Shmidt71757432014-06-02 13:50:35 -0700320 if (hapd->interface_added) {
321 hapd->interface_added = 0;
322 if (hostapd_if_remove(hapd, WPA_IF_AP_BSS, hapd->conf->iface)) {
323 wpa_printf(MSG_WARNING,
324 "Failed to remove BSS interface %s",
325 hapd->conf->iface);
326 hapd->interface_added = 1;
327 } else {
328 /*
329 * Since this was a dynamically added interface, the
330 * driver wrapper may have removed its internal instance
331 * and hapd->drv_priv is not valid anymore.
332 */
333 hapd->drv_priv = NULL;
334 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700335 }
336
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800337 wpabuf_free(hapd->time_adv);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700338
339#ifdef CONFIG_INTERWORKING
340 gas_serv_deinit(hapd);
341#endif /* CONFIG_INTERWORKING */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800342
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800343 bss_load_update_deinit(hapd);
344 ndisc_snoop_deinit(hapd);
345 dhcp_snoop_deinit(hapd);
346 x_snoop_deinit(hapd);
347
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800348#ifdef CONFIG_SQLITE
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700349 bin_clear_free(hapd->tmp_eap_user.identity,
350 hapd->tmp_eap_user.identity_len);
351 bin_clear_free(hapd->tmp_eap_user.password,
352 hapd->tmp_eap_user.password_len);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800353#endif /* CONFIG_SQLITE */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800354
355#ifdef CONFIG_MESH
356 wpabuf_free(hapd->mesh_pending_auth);
357 hapd->mesh_pending_auth = NULL;
358#endif /* CONFIG_MESH */
Dmitry Shmidt849734c2016-05-27 09:59:01 -0700359
360 hostapd_clean_rrm(hapd);
Dmitry Shmidtebd93af2017-02-21 13:40:44 -0800361 fils_hlp_deinit(hapd);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700362}
363
364
365/**
366 * hostapd_cleanup - Per-BSS cleanup (deinitialization)
367 * @hapd: Pointer to BSS data
368 *
369 * This function is used to free all per-BSS data structures and resources.
Dmitry Shmidt54605472013-11-08 11:10:19 -0800370 * Most of the modules that are initialized in hostapd_setup_bss() are
371 * deinitialized here.
Dmitry Shmidt04949592012-07-19 12:16:46 -0700372 */
373static void hostapd_cleanup(struct hostapd_data *hapd)
374{
Dmitry Shmidt54605472013-11-08 11:10:19 -0800375 wpa_printf(MSG_DEBUG, "%s(hapd=%p (%s))", __func__, hapd,
376 hapd->conf->iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700377 if (hapd->iface->interfaces &&
Dmitry Shmidt29333592017-01-09 12:27:11 -0800378 hapd->iface->interfaces->ctrl_iface_deinit) {
379 wpa_msg(hapd->msg_ctx, MSG_INFO, WPA_EVENT_TERMINATING);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700380 hapd->iface->interfaces->ctrl_iface_deinit(hapd);
Dmitry Shmidt29333592017-01-09 12:27:11 -0800381 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700382 hostapd_free_hapd_data(hapd);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700383}
384
385
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800386static void sta_track_deinit(struct hostapd_iface *iface)
387{
388 struct hostapd_sta_info *info;
389
390 if (!iface->num_sta_seen)
391 return;
392
393 while ((info = dl_list_first(&iface->sta_seen, struct hostapd_sta_info,
394 list))) {
395 dl_list_del(&info->list);
396 iface->num_sta_seen--;
Dmitry Shmidtaca489e2016-09-28 15:44:14 -0700397 sta_track_del(info);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800398 }
399}
400
401
Dmitry Shmidt04949592012-07-19 12:16:46 -0700402static void hostapd_cleanup_iface_partial(struct hostapd_iface *iface)
403{
Dmitry Shmidt54605472013-11-08 11:10:19 -0800404 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800405#ifdef CONFIG_IEEE80211N
406#ifdef NEED_AP_MLME
407 hostapd_stop_setup_timers(iface);
408#endif /* NEED_AP_MLME */
409#endif /* CONFIG_IEEE80211N */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700410 if (iface->current_mode)
411 acs_cleanup(iface);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700412 hostapd_free_hw_features(iface->hw_features, iface->num_hw_features);
413 iface->hw_features = NULL;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700414 iface->current_mode = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700415 os_free(iface->current_rates);
416 iface->current_rates = NULL;
417 os_free(iface->basic_rates);
418 iface->basic_rates = NULL;
419 ap_list_deinit(iface);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800420 sta_track_deinit(iface);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700421}
422
423
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700424/**
425 * hostapd_cleanup_iface - Complete per-interface cleanup
426 * @iface: Pointer to interface data
427 *
428 * This function is called after per-BSS data structures are deinitialized
429 * with hostapd_cleanup().
430 */
431static void hostapd_cleanup_iface(struct hostapd_iface *iface)
432{
Dmitry Shmidt54605472013-11-08 11:10:19 -0800433 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800434 eloop_cancel_timeout(channel_list_update_timeout, iface, NULL);
Roshan Pius3a1667e2018-07-03 15:17:14 -0700435 eloop_cancel_timeout(hostapd_interface_setup_failure_handler, iface,
436 NULL);
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800437
Dmitry Shmidt04949592012-07-19 12:16:46 -0700438 hostapd_cleanup_iface_partial(iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700439 hostapd_config_free(iface->conf);
440 iface->conf = NULL;
441
442 os_free(iface->config_fname);
443 os_free(iface->bss);
Dmitry Shmidt54605472013-11-08 11:10:19 -0800444 wpa_printf(MSG_DEBUG, "%s: free iface=%p", __func__, iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700445 os_free(iface);
446}
447
448
Dmitry Shmidt04949592012-07-19 12:16:46 -0700449static void hostapd_clear_wep(struct hostapd_data *hapd)
450{
Dmitry Shmidta38abf92014-03-06 13:38:44 -0800451 if (hapd->drv_priv && !hapd->iface->driver_ap_teardown) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700452 hostapd_set_privacy(hapd, 0);
453 hostapd_broadcast_wep_clear(hapd);
454 }
455}
456
457
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700458static int hostapd_setup_encryption(char *iface, struct hostapd_data *hapd)
459{
460 int i;
461
462 hostapd_broadcast_wep_set(hapd);
463
464 if (hapd->conf->ssid.wep.default_len) {
465 hostapd_set_privacy(hapd, 1);
466 return 0;
467 }
468
Jouni Malinen75ecf522011-06-27 15:19:46 -0700469 /*
470 * When IEEE 802.1X is not enabled, the driver may need to know how to
471 * set authentication algorithms for static WEP.
472 */
473 hostapd_drv_set_authmode(hapd, hapd->conf->auth_algs);
474
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700475 for (i = 0; i < 4; i++) {
476 if (hapd->conf->ssid.wep.key[i] &&
477 hostapd_drv_set_key(iface, hapd, WPA_ALG_WEP, NULL, i,
478 i == hapd->conf->ssid.wep.idx, NULL, 0,
479 hapd->conf->ssid.wep.key[i],
480 hapd->conf->ssid.wep.len[i])) {
481 wpa_printf(MSG_WARNING, "Could not set WEP "
482 "encryption.");
483 return -1;
484 }
485 if (hapd->conf->ssid.wep.key[i] &&
486 i == hapd->conf->ssid.wep.idx)
487 hostapd_set_privacy(hapd, 1);
488 }
489
490 return 0;
491}
492
493
Dmitry Shmidt04949592012-07-19 12:16:46 -0700494static int hostapd_flush_old_stations(struct hostapd_data *hapd, u16 reason)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700495{
496 int ret = 0;
497 u8 addr[ETH_ALEN];
498
499 if (hostapd_drv_none(hapd) || hapd->drv_priv == NULL)
500 return 0;
501
Dmitry Shmidta38abf92014-03-06 13:38:44 -0800502 if (!hapd->iface->driver_ap_teardown) {
503 wpa_dbg(hapd->msg_ctx, MSG_DEBUG,
504 "Flushing old station entries");
505
506 if (hostapd_flush(hapd)) {
507 wpa_msg(hapd->msg_ctx, MSG_WARNING,
508 "Could not connect to kernel driver");
509 ret = -1;
510 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700511 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700512 if (hapd->conf && hapd->conf->broadcast_deauth) {
513 wpa_dbg(hapd->msg_ctx, MSG_DEBUG,
514 "Deauthenticate all stations");
515 os_memset(addr, 0xff, ETH_ALEN);
516 hostapd_drv_sta_deauth(hapd, addr, reason);
517 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700518 hostapd_free_stas(hapd);
519
520 return ret;
521}
522
523
Dmitry Shmidt71757432014-06-02 13:50:35 -0700524static void hostapd_bss_deinit_no_free(struct hostapd_data *hapd)
525{
526 hostapd_free_stas(hapd);
527 hostapd_flush_old_stations(hapd, WLAN_REASON_DEAUTH_LEAVING);
528 hostapd_clear_wep(hapd);
529}
530
531
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700532/**
533 * hostapd_validate_bssid_configuration - Validate BSSID configuration
534 * @iface: Pointer to interface data
535 * Returns: 0 on success, -1 on failure
536 *
537 * This function is used to validate that the configured BSSIDs are valid.
538 */
539static int hostapd_validate_bssid_configuration(struct hostapd_iface *iface)
540{
541 u8 mask[ETH_ALEN] = { 0 };
542 struct hostapd_data *hapd = iface->bss[0];
543 unsigned int i = iface->conf->num_bss, bits = 0, j;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700544 int auto_addr = 0;
545
546 if (hostapd_drv_none(hapd))
547 return 0;
548
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -0800549 if (iface->conf->use_driver_iface_addr)
550 return 0;
551
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700552 /* Generate BSSID mask that is large enough to cover the BSSIDs. */
553
554 /* Determine the bits necessary to cover the number of BSSIDs. */
555 for (i--; i; i >>= 1)
556 bits++;
557
558 /* Determine the bits necessary to any configured BSSIDs,
559 if they are higher than the number of BSSIDs. */
560 for (j = 0; j < iface->conf->num_bss; j++) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800561 if (is_zero_ether_addr(iface->conf->bss[j]->bssid)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700562 if (j)
563 auto_addr++;
564 continue;
565 }
566
567 for (i = 0; i < ETH_ALEN; i++) {
568 mask[i] |=
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800569 iface->conf->bss[j]->bssid[i] ^
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700570 hapd->own_addr[i];
571 }
572 }
573
574 if (!auto_addr)
575 goto skip_mask_ext;
576
577 for (i = 0; i < ETH_ALEN && mask[i] == 0; i++)
578 ;
579 j = 0;
580 if (i < ETH_ALEN) {
581 j = (5 - i) * 8;
582
583 while (mask[i] != 0) {
584 mask[i] >>= 1;
585 j++;
586 }
587 }
588
589 if (bits < j)
590 bits = j;
591
592 if (bits > 40) {
593 wpa_printf(MSG_ERROR, "Too many bits in the BSSID mask (%u)",
594 bits);
595 return -1;
596 }
597
598 os_memset(mask, 0xff, ETH_ALEN);
599 j = bits / 8;
600 for (i = 5; i > 5 - j; i--)
601 mask[i] = 0;
602 j = bits % 8;
603 while (j--)
604 mask[i] <<= 1;
605
606skip_mask_ext:
607 wpa_printf(MSG_DEBUG, "BSS count %lu, BSSID mask " MACSTR " (%d bits)",
608 (unsigned long) iface->conf->num_bss, MAC2STR(mask), bits);
609
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700610 if (!auto_addr)
611 return 0;
612
613 for (i = 0; i < ETH_ALEN; i++) {
614 if ((hapd->own_addr[i] & mask[i]) != hapd->own_addr[i]) {
615 wpa_printf(MSG_ERROR, "Invalid BSSID mask " MACSTR
616 " for start address " MACSTR ".",
617 MAC2STR(mask), MAC2STR(hapd->own_addr));
618 wpa_printf(MSG_ERROR, "Start address must be the "
619 "first address in the block (i.e., addr "
620 "AND mask == addr).");
621 return -1;
622 }
623 }
624
625 return 0;
626}
627
628
629static int mac_in_conf(struct hostapd_config *conf, const void *a)
630{
631 size_t i;
632
633 for (i = 0; i < conf->num_bss; i++) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800634 if (hostapd_mac_comp(conf->bss[i]->bssid, a) == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700635 return 1;
636 }
637 }
638
639 return 0;
640}
641
642
Dmitry Shmidt04949592012-07-19 12:16:46 -0700643#ifndef CONFIG_NO_RADIUS
644
645static int hostapd_das_nas_mismatch(struct hostapd_data *hapd,
646 struct radius_das_attrs *attr)
647{
Dmitry Shmidt13ca8d82014-02-20 10:18:40 -0800648 if (attr->nas_identifier &&
649 (!hapd->conf->nas_identifier ||
650 os_strlen(hapd->conf->nas_identifier) !=
651 attr->nas_identifier_len ||
652 os_memcmp(hapd->conf->nas_identifier, attr->nas_identifier,
653 attr->nas_identifier_len) != 0)) {
654 wpa_printf(MSG_DEBUG, "RADIUS DAS: NAS-Identifier mismatch");
655 return 1;
656 }
657
658 if (attr->nas_ip_addr &&
659 (hapd->conf->own_ip_addr.af != AF_INET ||
660 os_memcmp(&hapd->conf->own_ip_addr.u.v4, attr->nas_ip_addr, 4) !=
661 0)) {
662 wpa_printf(MSG_DEBUG, "RADIUS DAS: NAS-IP-Address mismatch");
663 return 1;
664 }
665
666#ifdef CONFIG_IPV6
667 if (attr->nas_ipv6_addr &&
668 (hapd->conf->own_ip_addr.af != AF_INET6 ||
669 os_memcmp(&hapd->conf->own_ip_addr.u.v6, attr->nas_ipv6_addr, 16)
670 != 0)) {
671 wpa_printf(MSG_DEBUG, "RADIUS DAS: NAS-IPv6-Address mismatch");
672 return 1;
673 }
674#endif /* CONFIG_IPV6 */
675
Dmitry Shmidt04949592012-07-19 12:16:46 -0700676 return 0;
677}
678
679
680static struct sta_info * hostapd_das_find_sta(struct hostapd_data *hapd,
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800681 struct radius_das_attrs *attr,
682 int *multi)
Dmitry Shmidt04949592012-07-19 12:16:46 -0700683{
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800684 struct sta_info *selected, *sta;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700685 char buf[128];
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800686 int num_attr = 0;
687 int count;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700688
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800689 *multi = 0;
690
691 for (sta = hapd->sta_list; sta; sta = sta->next)
692 sta->radius_das_match = 1;
693
694 if (attr->sta_addr) {
695 num_attr++;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700696 sta = ap_get_sta(hapd, attr->sta_addr);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800697 if (!sta) {
698 wpa_printf(MSG_DEBUG,
699 "RADIUS DAS: No Calling-Station-Id match");
700 return NULL;
701 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700702
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800703 selected = sta;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700704 for (sta = hapd->sta_list; sta; sta = sta->next) {
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800705 if (sta != selected)
706 sta->radius_das_match = 0;
707 }
708 wpa_printf(MSG_DEBUG, "RADIUS DAS: Calling-Station-Id match");
709 }
710
711 if (attr->acct_session_id) {
712 num_attr++;
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800713 if (attr->acct_session_id_len != 16) {
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800714 wpa_printf(MSG_DEBUG,
715 "RADIUS DAS: Acct-Session-Id cannot match");
716 return NULL;
717 }
718 count = 0;
719
720 for (sta = hapd->sta_list; sta; sta = sta->next) {
721 if (!sta->radius_das_match)
722 continue;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800723 os_snprintf(buf, sizeof(buf), "%016llX",
724 (unsigned long long) sta->acct_session_id);
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800725 if (os_memcmp(attr->acct_session_id, buf, 16) != 0)
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800726 sta->radius_das_match = 0;
727 else
728 count++;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700729 }
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800730
731 if (count == 0) {
732 wpa_printf(MSG_DEBUG,
733 "RADIUS DAS: No matches remaining after Acct-Session-Id check");
734 return NULL;
735 }
736 wpa_printf(MSG_DEBUG, "RADIUS DAS: Acct-Session-Id match");
Dmitry Shmidt04949592012-07-19 12:16:46 -0700737 }
738
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800739 if (attr->acct_multi_session_id) {
740 num_attr++;
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800741 if (attr->acct_multi_session_id_len != 16) {
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800742 wpa_printf(MSG_DEBUG,
743 "RADIUS DAS: Acct-Multi-Session-Id cannot match");
744 return NULL;
745 }
746 count = 0;
747
748 for (sta = hapd->sta_list; sta; sta = sta->next) {
749 if (!sta->radius_das_match)
750 continue;
751 if (!sta->eapol_sm ||
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800752 !sta->eapol_sm->acct_multi_session_id) {
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800753 sta->radius_das_match = 0;
754 continue;
755 }
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800756 os_snprintf(buf, sizeof(buf), "%016llX",
757 (unsigned long long)
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800758 sta->eapol_sm->acct_multi_session_id);
759 if (os_memcmp(attr->acct_multi_session_id, buf, 16) !=
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800760 0)
761 sta->radius_das_match = 0;
762 else
763 count++;
764 }
765
766 if (count == 0) {
767 wpa_printf(MSG_DEBUG,
768 "RADIUS DAS: No matches remaining after Acct-Multi-Session-Id check");
769 return NULL;
770 }
771 wpa_printf(MSG_DEBUG,
772 "RADIUS DAS: Acct-Multi-Session-Id match");
773 }
774
775 if (attr->cui) {
776 num_attr++;
777 count = 0;
778
Dmitry Shmidt04949592012-07-19 12:16:46 -0700779 for (sta = hapd->sta_list; sta; sta = sta->next) {
780 struct wpabuf *cui;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800781
782 if (!sta->radius_das_match)
783 continue;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700784 cui = ieee802_1x_get_radius_cui(sta->eapol_sm);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800785 if (!cui || wpabuf_len(cui) != attr->cui_len ||
Dmitry Shmidt04949592012-07-19 12:16:46 -0700786 os_memcmp(wpabuf_head(cui), attr->cui,
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800787 attr->cui_len) != 0)
788 sta->radius_das_match = 0;
789 else
790 count++;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700791 }
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800792
793 if (count == 0) {
794 wpa_printf(MSG_DEBUG,
795 "RADIUS DAS: No matches remaining after Chargeable-User-Identity check");
796 return NULL;
797 }
798 wpa_printf(MSG_DEBUG,
799 "RADIUS DAS: Chargeable-User-Identity match");
Dmitry Shmidt04949592012-07-19 12:16:46 -0700800 }
801
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800802 if (attr->user_name) {
803 num_attr++;
804 count = 0;
805
Dmitry Shmidt04949592012-07-19 12:16:46 -0700806 for (sta = hapd->sta_list; sta; sta = sta->next) {
807 u8 *identity;
808 size_t identity_len;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800809
810 if (!sta->radius_das_match)
811 continue;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700812 identity = ieee802_1x_get_identity(sta->eapol_sm,
813 &identity_len);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800814 if (!identity ||
815 identity_len != attr->user_name_len ||
Dmitry Shmidt04949592012-07-19 12:16:46 -0700816 os_memcmp(identity, attr->user_name, identity_len)
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800817 != 0)
818 sta->radius_das_match = 0;
819 else
820 count++;
821 }
822
823 if (count == 0) {
824 wpa_printf(MSG_DEBUG,
825 "RADIUS DAS: No matches remaining after User-Name check");
826 return NULL;
827 }
828 wpa_printf(MSG_DEBUG,
829 "RADIUS DAS: User-Name match");
830 }
831
832 if (num_attr == 0) {
833 /*
834 * In theory, we could match all current associations, but it
835 * seems safer to just reject requests that do not include any
836 * session identification attributes.
837 */
838 wpa_printf(MSG_DEBUG,
839 "RADIUS DAS: No session identification attributes included");
840 return NULL;
841 }
842
843 selected = NULL;
844 for (sta = hapd->sta_list; sta; sta = sta->next) {
845 if (sta->radius_das_match) {
846 if (selected) {
847 *multi = 1;
848 return NULL;
849 }
850 selected = sta;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700851 }
852 }
853
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800854 return selected;
855}
856
857
858static int hostapd_das_disconnect_pmksa(struct hostapd_data *hapd,
859 struct radius_das_attrs *attr)
860{
861 if (!hapd->wpa_auth)
862 return -1;
863 return wpa_auth_radius_das_disconnect_pmksa(hapd->wpa_auth, attr);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700864}
865
866
867static enum radius_das_res
868hostapd_das_disconnect(void *ctx, struct radius_das_attrs *attr)
869{
870 struct hostapd_data *hapd = ctx;
871 struct sta_info *sta;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800872 int multi;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700873
874 if (hostapd_das_nas_mismatch(hapd, attr))
875 return RADIUS_DAS_NAS_MISMATCH;
876
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800877 sta = hostapd_das_find_sta(hapd, attr, &multi);
878 if (sta == NULL) {
879 if (multi) {
880 wpa_printf(MSG_DEBUG,
881 "RADIUS DAS: Multiple sessions match - not supported");
882 return RADIUS_DAS_MULTI_SESSION_MATCH;
883 }
884 if (hostapd_das_disconnect_pmksa(hapd, attr) == 0) {
885 wpa_printf(MSG_DEBUG,
886 "RADIUS DAS: PMKSA cache entry matched");
887 return RADIUS_DAS_SUCCESS;
888 }
889 wpa_printf(MSG_DEBUG, "RADIUS DAS: No matching session found");
Dmitry Shmidt04949592012-07-19 12:16:46 -0700890 return RADIUS_DAS_SESSION_NOT_FOUND;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800891 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700892
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800893 wpa_printf(MSG_DEBUG, "RADIUS DAS: Found a matching session " MACSTR
894 " - disconnecting", MAC2STR(sta->addr));
Dmitry Shmidt13ca8d82014-02-20 10:18:40 -0800895 wpa_auth_pmksa_remove(hapd->wpa_auth, sta->addr);
896
Dmitry Shmidt04949592012-07-19 12:16:46 -0700897 hostapd_drv_sta_deauth(hapd, sta->addr,
898 WLAN_REASON_PREV_AUTH_NOT_VALID);
899 ap_sta_deauthenticate(hapd, sta, WLAN_REASON_PREV_AUTH_NOT_VALID);
900
901 return RADIUS_DAS_SUCCESS;
902}
903
Roshan Pius3a1667e2018-07-03 15:17:14 -0700904
905#ifdef CONFIG_HS20
906static enum radius_das_res
907hostapd_das_coa(void *ctx, struct radius_das_attrs *attr)
908{
909 struct hostapd_data *hapd = ctx;
910 struct sta_info *sta;
911 int multi;
912
913 if (hostapd_das_nas_mismatch(hapd, attr))
914 return RADIUS_DAS_NAS_MISMATCH;
915
916 sta = hostapd_das_find_sta(hapd, attr, &multi);
917 if (!sta) {
918 if (multi) {
919 wpa_printf(MSG_DEBUG,
920 "RADIUS DAS: Multiple sessions match - not supported");
921 return RADIUS_DAS_MULTI_SESSION_MATCH;
922 }
923 wpa_printf(MSG_DEBUG, "RADIUS DAS: No matching session found");
924 return RADIUS_DAS_SESSION_NOT_FOUND;
925 }
926
927 wpa_printf(MSG_DEBUG, "RADIUS DAS: Found a matching session " MACSTR
928 " - CoA", MAC2STR(sta->addr));
929
930 if (attr->hs20_t_c_filtering) {
931 if (attr->hs20_t_c_filtering[0] & BIT(0)) {
932 wpa_printf(MSG_DEBUG,
933 "HS 2.0: Unexpected Terms and Conditions filtering required in CoA-Request");
934 return RADIUS_DAS_COA_FAILED;
935 }
936
937 hs20_t_c_filtering(hapd, sta, 0);
938 }
939
940 return RADIUS_DAS_SUCCESS;
941}
942#else /* CONFIG_HS20 */
943#define hostapd_das_coa NULL
944#endif /* CONFIG_HS20 */
945
Dmitry Shmidt04949592012-07-19 12:16:46 -0700946#endif /* CONFIG_NO_RADIUS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700947
948
949/**
950 * hostapd_setup_bss - Per-BSS setup (initialization)
951 * @hapd: Pointer to BSS data
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800952 * @first: Whether this BSS is the first BSS of an interface; -1 = not first,
953 * but interface may exist
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700954 *
955 * This function is used to initialize all per-BSS data structures and
956 * resources. This gets called in a loop for each BSS when an interface is
957 * initialized. Most of the modules that are initialized here will be
958 * deinitialized in hostapd_cleanup().
959 */
960static int hostapd_setup_bss(struct hostapd_data *hapd, int first)
961{
962 struct hostapd_bss_config *conf = hapd->conf;
Dmitry Shmidt9d9e6022015-04-23 10:34:55 -0700963 u8 ssid[SSID_MAX_LEN + 1];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700964 int ssid_len, set_ssid;
965 char force_ifname[IFNAMSIZ];
966 u8 if_addr[ETH_ALEN];
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800967 int flush_old_stations = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700968
Dmitry Shmidt54605472013-11-08 11:10:19 -0800969 wpa_printf(MSG_DEBUG, "%s(hapd=%p (%s), first=%d)",
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700970 __func__, hapd, conf->iface, first);
Dmitry Shmidt54605472013-11-08 11:10:19 -0800971
Dmitry Shmidt50b691d2014-05-21 14:01:45 -0700972#ifdef EAP_SERVER_TNC
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700973 if (conf->tnc && tncs_global_init() < 0) {
Dmitry Shmidt50b691d2014-05-21 14:01:45 -0700974 wpa_printf(MSG_ERROR, "Failed to initialize TNCS");
975 return -1;
976 }
977#endif /* EAP_SERVER_TNC */
978
Dmitry Shmidt54605472013-11-08 11:10:19 -0800979 if (hapd->started) {
980 wpa_printf(MSG_ERROR, "%s: Interface %s was already started",
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700981 __func__, conf->iface);
Dmitry Shmidt54605472013-11-08 11:10:19 -0800982 return -1;
983 }
984 hapd->started = 1;
985
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800986 if (!first || first == -1) {
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -0800987 u8 *addr = hapd->own_addr;
988
989 if (!is_zero_ether_addr(conf->bssid)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700990 /* Allocate the configured BSSID. */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700991 os_memcpy(hapd->own_addr, conf->bssid, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700992
993 if (hostapd_mac_comp(hapd->own_addr,
994 hapd->iface->bss[0]->own_addr) ==
995 0) {
996 wpa_printf(MSG_ERROR, "BSS '%s' may not have "
997 "BSSID set to the MAC address of "
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700998 "the radio", conf->iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700999 return -1;
1000 }
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08001001 } else if (hapd->iconf->use_driver_iface_addr) {
1002 addr = NULL;
1003 } else {
1004 /* Allocate the next available BSSID. */
1005 do {
1006 inc_byte_array(hapd->own_addr, ETH_ALEN);
1007 } while (mac_in_conf(hapd->iconf, hapd->own_addr));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001008 }
1009
Dmitry Shmidt54605472013-11-08 11:10:19 -08001010 hapd->interface_added = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001011 if (hostapd_if_add(hapd->iface->bss[0], WPA_IF_AP_BSS,
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08001012 conf->iface, addr, hapd,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001013 &hapd->drv_priv, force_ifname, if_addr,
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001014 conf->bridge[0] ? conf->bridge : NULL,
1015 first == -1)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001016 wpa_printf(MSG_ERROR, "Failed to add BSS (BSSID="
1017 MACSTR ")", MAC2STR(hapd->own_addr));
Dmitry Shmidt3cf6f792013-12-18 13:12:19 -08001018 hapd->interface_added = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001019 return -1;
1020 }
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08001021
1022 if (!addr)
1023 os_memcpy(hapd->own_addr, if_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001024 }
1025
1026 if (conf->wmm_enabled < 0)
1027 conf->wmm_enabled = hapd->iconf->ieee80211n;
1028
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001029#ifdef CONFIG_IEEE80211R_AP
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001030 if (is_zero_ether_addr(conf->r1_key_holder))
1031 os_memcpy(conf->r1_key_holder, hapd->own_addr, ETH_ALEN);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001032#endif /* CONFIG_IEEE80211R_AP */
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001033
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001034#ifdef CONFIG_MESH
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001035 if ((hapd->conf->mesh & MESH_ENABLED) && hapd->iface->mconf == NULL)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001036 flush_old_stations = 0;
1037#endif /* CONFIG_MESH */
1038
1039 if (flush_old_stations)
1040 hostapd_flush_old_stations(hapd,
1041 WLAN_REASON_PREV_AUTH_NOT_VALID);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001042 hostapd_set_privacy(hapd, 0);
1043
1044 hostapd_broadcast_wep_clear(hapd);
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001045 if (hostapd_setup_encryption(conf->iface, hapd))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001046 return -1;
1047
1048 /*
1049 * Fetch the SSID from the system and use it or,
1050 * if one was specified in the config file, verify they
1051 * match.
1052 */
1053 ssid_len = hostapd_get_ssid(hapd, ssid, sizeof(ssid));
1054 if (ssid_len < 0) {
1055 wpa_printf(MSG_ERROR, "Could not read SSID from system");
1056 return -1;
1057 }
1058 if (conf->ssid.ssid_set) {
1059 /*
1060 * If SSID is specified in the config file and it differs
1061 * from what is being used then force installation of the
1062 * new SSID.
1063 */
1064 set_ssid = (conf->ssid.ssid_len != (size_t) ssid_len ||
1065 os_memcmp(conf->ssid.ssid, ssid, ssid_len) != 0);
1066 } else {
1067 /*
1068 * No SSID in the config file; just use the one we got
1069 * from the system.
1070 */
1071 set_ssid = 0;
1072 conf->ssid.ssid_len = ssid_len;
1073 os_memcpy(conf->ssid.ssid, ssid, conf->ssid.ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001074 }
1075
1076 if (!hostapd_drv_none(hapd)) {
1077 wpa_printf(MSG_ERROR, "Using interface %s with hwaddr " MACSTR
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001078 " and ssid \"%s\"",
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001079 conf->iface, MAC2STR(hapd->own_addr),
1080 wpa_ssid_txt(conf->ssid.ssid, conf->ssid.ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001081 }
1082
1083 if (hostapd_setup_wpa_psk(conf)) {
1084 wpa_printf(MSG_ERROR, "WPA-PSK setup failed.");
1085 return -1;
1086 }
1087
1088 /* Set SSID for the kernel driver (to be used in beacon and probe
1089 * response frames) */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001090 if (set_ssid && hostapd_set_ssid(hapd, conf->ssid.ssid,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001091 conf->ssid.ssid_len)) {
1092 wpa_printf(MSG_ERROR, "Could not set SSID for kernel driver");
1093 return -1;
1094 }
1095
Dmitry Shmidt818ea482014-03-10 13:15:21 -07001096 if (wpa_debug_level <= MSG_MSGDUMP)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001097 conf->radius->msg_dumps = 1;
1098#ifndef CONFIG_NO_RADIUS
1099 hapd->radius = radius_client_init(hapd, conf->radius);
1100 if (hapd->radius == NULL) {
1101 wpa_printf(MSG_ERROR, "RADIUS client initialization failed.");
1102 return -1;
1103 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001104
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001105 if (conf->radius_das_port) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001106 struct radius_das_conf das_conf;
1107 os_memset(&das_conf, 0, sizeof(das_conf));
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001108 das_conf.port = conf->radius_das_port;
1109 das_conf.shared_secret = conf->radius_das_shared_secret;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001110 das_conf.shared_secret_len =
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001111 conf->radius_das_shared_secret_len;
1112 das_conf.client_addr = &conf->radius_das_client_addr;
1113 das_conf.time_window = conf->radius_das_time_window;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001114 das_conf.require_event_timestamp =
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001115 conf->radius_das_require_event_timestamp;
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07001116 das_conf.require_message_authenticator =
1117 conf->radius_das_require_message_authenticator;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001118 das_conf.ctx = hapd;
1119 das_conf.disconnect = hostapd_das_disconnect;
Roshan Pius3a1667e2018-07-03 15:17:14 -07001120 das_conf.coa = hostapd_das_coa;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001121 hapd->radius_das = radius_das_init(&das_conf);
1122 if (hapd->radius_das == NULL) {
1123 wpa_printf(MSG_ERROR, "RADIUS DAS initialization "
1124 "failed.");
1125 return -1;
1126 }
1127 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001128#endif /* CONFIG_NO_RADIUS */
1129
1130 if (hostapd_acl_init(hapd)) {
1131 wpa_printf(MSG_ERROR, "ACL initialization failed.");
1132 return -1;
1133 }
1134 if (hostapd_init_wps(hapd, conf))
1135 return -1;
1136
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001137#ifdef CONFIG_DPP
1138 hapd->gas = gas_query_ap_init(hapd, hapd->msg_ctx);
1139 if (!hapd->gas)
1140 return -1;
1141 if (hostapd_dpp_init(hapd))
1142 return -1;
1143#endif /* CONFIG_DPP */
1144
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001145 if (authsrv_init(hapd) < 0)
1146 return -1;
1147
1148 if (ieee802_1x_init(hapd)) {
1149 wpa_printf(MSG_ERROR, "IEEE 802.1X initialization failed.");
1150 return -1;
1151 }
1152
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001153 if ((conf->wpa || conf->osen) && hostapd_setup_wpa(hapd))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001154 return -1;
1155
1156 if (accounting_init(hapd)) {
1157 wpa_printf(MSG_ERROR, "Accounting initialization failed.");
1158 return -1;
1159 }
1160
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001161 if (conf->ieee802_11f &&
1162 (hapd->iapp = iapp_init(hapd, conf->iapp_iface)) == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001163 wpa_printf(MSG_ERROR, "IEEE 802.11F (IAPP) initialization "
1164 "failed.");
1165 return -1;
1166 }
1167
Dmitry Shmidt04949592012-07-19 12:16:46 -07001168#ifdef CONFIG_INTERWORKING
1169 if (gas_serv_init(hapd)) {
1170 wpa_printf(MSG_ERROR, "GAS server initialization failed");
1171 return -1;
1172 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001173
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001174 if (conf->qos_map_set_len &&
1175 hostapd_drv_set_qos_map(hapd, conf->qos_map_set,
1176 conf->qos_map_set_len)) {
1177 wpa_printf(MSG_ERROR, "Failed to initialize QoS Map");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001178 return -1;
1179 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001180#endif /* CONFIG_INTERWORKING */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001181
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001182 if (conf->bss_load_update_period && bss_load_update_init(hapd)) {
1183 wpa_printf(MSG_ERROR, "BSS Load initialization failed");
1184 return -1;
1185 }
1186
1187 if (conf->proxy_arp) {
1188 if (x_snoop_init(hapd)) {
1189 wpa_printf(MSG_ERROR,
1190 "Generic snooping infrastructure initialization failed");
1191 return -1;
1192 }
1193
1194 if (dhcp_snoop_init(hapd)) {
1195 wpa_printf(MSG_ERROR,
1196 "DHCP snooping initialization failed");
1197 return -1;
1198 }
1199
1200 if (ndisc_snoop_init(hapd)) {
1201 wpa_printf(MSG_ERROR,
1202 "Neighbor Discovery snooping initialization failed");
1203 return -1;
1204 }
1205 }
1206
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001207 if (!hostapd_drv_none(hapd) && vlan_init(hapd)) {
1208 wpa_printf(MSG_ERROR, "VLAN initialization failed.");
1209 return -1;
1210 }
1211
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001212 if (!conf->start_disabled && ieee802_11_set_beacon(hapd) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001213 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001214
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001215 if (hapd->wpa_auth && wpa_init_keys(hapd->wpa_auth) < 0)
1216 return -1;
1217
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001218 if (hapd->driver && hapd->driver->set_operstate)
1219 hapd->driver->set_operstate(hapd->drv_priv, 1);
1220
1221 return 0;
1222}
1223
1224
1225static void hostapd_tx_queue_params(struct hostapd_iface *iface)
1226{
1227 struct hostapd_data *hapd = iface->bss[0];
1228 int i;
1229 struct hostapd_tx_queue_params *p;
1230
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001231#ifdef CONFIG_MESH
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001232 if ((hapd->conf->mesh & MESH_ENABLED) && iface->mconf == NULL)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001233 return;
1234#endif /* CONFIG_MESH */
1235
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001236 for (i = 0; i < NUM_TX_QUEUES; i++) {
1237 p = &iface->conf->tx_queue[i];
1238
1239 if (hostapd_set_tx_queue_params(hapd, i, p->aifs, p->cwmin,
1240 p->cwmax, p->burst)) {
1241 wpa_printf(MSG_DEBUG, "Failed to set TX queue "
1242 "parameters for queue %d.", i);
1243 /* Continue anyway */
1244 }
1245 }
1246}
1247
1248
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001249static int hostapd_set_acl_list(struct hostapd_data *hapd,
1250 struct mac_acl_entry *mac_acl,
1251 int n_entries, u8 accept_acl)
1252{
1253 struct hostapd_acl_params *acl_params;
1254 int i, err;
1255
1256 acl_params = os_zalloc(sizeof(*acl_params) +
1257 (n_entries * sizeof(acl_params->mac_acl[0])));
1258 if (!acl_params)
1259 return -ENOMEM;
1260
1261 for (i = 0; i < n_entries; i++)
1262 os_memcpy(acl_params->mac_acl[i].addr, mac_acl[i].addr,
1263 ETH_ALEN);
1264
1265 acl_params->acl_policy = accept_acl;
1266 acl_params->num_mac_acl = n_entries;
1267
1268 err = hostapd_drv_set_acl(hapd, acl_params);
1269
1270 os_free(acl_params);
1271
1272 return err;
1273}
1274
1275
1276static void hostapd_set_acl(struct hostapd_data *hapd)
1277{
1278 struct hostapd_config *conf = hapd->iconf;
1279 int err;
1280 u8 accept_acl;
1281
1282 if (hapd->iface->drv_max_acl_mac_addrs == 0)
1283 return;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001284
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001285 if (conf->bss[0]->macaddr_acl == DENY_UNLESS_ACCEPTED) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001286 accept_acl = 1;
1287 err = hostapd_set_acl_list(hapd, conf->bss[0]->accept_mac,
1288 conf->bss[0]->num_accept_mac,
1289 accept_acl);
1290 if (err) {
1291 wpa_printf(MSG_DEBUG, "Failed to set accept acl");
1292 return;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001293 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001294 } else if (conf->bss[0]->macaddr_acl == ACCEPT_UNLESS_DENIED) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001295 accept_acl = 0;
1296 err = hostapd_set_acl_list(hapd, conf->bss[0]->deny_mac,
1297 conf->bss[0]->num_deny_mac,
1298 accept_acl);
1299 if (err) {
1300 wpa_printf(MSG_DEBUG, "Failed to set deny acl");
1301 return;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001302 }
1303 }
1304}
1305
1306
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001307static int start_ctrl_iface_bss(struct hostapd_data *hapd)
1308{
1309 if (!hapd->iface->interfaces ||
1310 !hapd->iface->interfaces->ctrl_iface_init)
1311 return 0;
1312
1313 if (hapd->iface->interfaces->ctrl_iface_init(hapd)) {
1314 wpa_printf(MSG_ERROR,
1315 "Failed to setup control interface for %s",
1316 hapd->conf->iface);
1317 return -1;
1318 }
1319
1320 return 0;
1321}
1322
1323
1324static int start_ctrl_iface(struct hostapd_iface *iface)
1325{
1326 size_t i;
1327
1328 if (!iface->interfaces || !iface->interfaces->ctrl_iface_init)
1329 return 0;
1330
1331 for (i = 0; i < iface->num_bss; i++) {
1332 struct hostapd_data *hapd = iface->bss[i];
1333 if (iface->interfaces->ctrl_iface_init(hapd)) {
1334 wpa_printf(MSG_ERROR,
1335 "Failed to setup control interface for %s",
1336 hapd->conf->iface);
1337 return -1;
1338 }
1339 }
1340
1341 return 0;
1342}
1343
1344
1345static void channel_list_update_timeout(void *eloop_ctx, void *timeout_ctx)
1346{
1347 struct hostapd_iface *iface = eloop_ctx;
1348
1349 if (!iface->wait_channel_update) {
1350 wpa_printf(MSG_INFO, "Channel list update timeout, but interface was not waiting for it");
1351 return;
1352 }
1353
1354 /*
1355 * It is possible that the existing channel list is acceptable, so try
1356 * to proceed.
1357 */
1358 wpa_printf(MSG_DEBUG, "Channel list update timeout - try to continue anyway");
1359 setup_interface2(iface);
1360}
1361
1362
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001363void hostapd_channel_list_updated(struct hostapd_iface *iface, int initiator)
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001364{
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001365 if (!iface->wait_channel_update || initiator != REGDOM_SET_BY_USER)
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001366 return;
1367
1368 wpa_printf(MSG_DEBUG, "Channel list updated - continue setup");
1369 eloop_cancel_timeout(channel_list_update_timeout, iface, NULL);
1370 setup_interface2(iface);
1371}
1372
1373
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001374static int setup_interface(struct hostapd_iface *iface)
1375{
1376 struct hostapd_data *hapd = iface->bss[0];
1377 size_t i;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001378
Dmitry Shmidta38abf92014-03-06 13:38:44 -08001379 /*
1380 * It is possible that setup_interface() is called after the interface
1381 * was disabled etc., in which case driver_ap_teardown is possibly set
1382 * to 1. Clear it here so any other key/station deletion, which is not
1383 * part of a teardown flow, would also call the relevant driver
1384 * callbacks.
1385 */
1386 iface->driver_ap_teardown = 0;
1387
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001388 if (!iface->phy[0]) {
1389 const char *phy = hostapd_drv_get_radio_name(hapd);
1390 if (phy) {
1391 wpa_printf(MSG_DEBUG, "phy: %s", phy);
1392 os_strlcpy(iface->phy, phy, sizeof(iface->phy));
1393 }
1394 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001395
1396 /*
1397 * Make sure that all BSSes get configured with a pointer to the same
1398 * driver interface.
1399 */
1400 for (i = 1; i < iface->num_bss; i++) {
1401 iface->bss[i]->driver = hapd->driver;
1402 iface->bss[i]->drv_priv = hapd->drv_priv;
1403 }
1404
1405 if (hostapd_validate_bssid_configuration(iface))
1406 return -1;
1407
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001408 /*
1409 * Initialize control interfaces early to allow external monitoring of
1410 * channel setup operations that may take considerable amount of time
1411 * especially for DFS cases.
1412 */
1413 if (start_ctrl_iface(iface))
1414 return -1;
1415
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001416 if (hapd->iconf->country[0] && hapd->iconf->country[1]) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001417 char country[4], previous_country[4];
1418
1419 hostapd_set_state(iface, HAPD_IFACE_COUNTRY_UPDATE);
1420 if (hostapd_get_country(hapd, previous_country) < 0)
1421 previous_country[0] = '\0';
1422
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001423 os_memcpy(country, hapd->iconf->country, 3);
1424 country[3] = '\0';
1425 if (hostapd_set_country(hapd, country) < 0) {
1426 wpa_printf(MSG_ERROR, "Failed to set country code");
1427 return -1;
1428 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001429
1430 wpa_printf(MSG_DEBUG, "Previous country code %s, new country code %s",
1431 previous_country, country);
1432
1433 if (os_strncmp(previous_country, country, 2) != 0) {
1434 wpa_printf(MSG_DEBUG, "Continue interface setup after channel list update");
1435 iface->wait_channel_update = 1;
Dmitry Shmidt97672262014-02-03 13:02:54 -08001436 eloop_register_timeout(5, 0,
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001437 channel_list_update_timeout,
1438 iface, NULL);
1439 return 0;
1440 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001441 }
1442
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001443 return setup_interface2(iface);
1444}
1445
1446
1447static int setup_interface2(struct hostapd_iface *iface)
1448{
1449 iface->wait_channel_update = 0;
1450
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001451 if (hostapd_get_hw_features(iface)) {
1452 /* Not all drivers support this yet, so continue without hw
1453 * feature data. */
1454 } else {
1455 int ret = hostapd_select_hw_mode(iface);
1456 if (ret < 0) {
1457 wpa_printf(MSG_ERROR, "Could not select hw_mode and "
1458 "channel. (%d)", ret);
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001459 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001460 }
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001461 if (ret == 1) {
1462 wpa_printf(MSG_DEBUG, "Interface initialization will be completed in a callback (ACS)");
1463 return 0;
1464 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001465 ret = hostapd_check_ht_capab(iface);
1466 if (ret < 0)
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001467 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001468 if (ret == 1) {
1469 wpa_printf(MSG_DEBUG, "Interface initialization will "
1470 "be completed in a callback");
1471 return 0;
1472 }
Dmitry Shmidt051af732013-10-22 13:52:46 -07001473
1474 if (iface->conf->ieee80211h)
1475 wpa_printf(MSG_DEBUG, "DFS support is enabled");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001476 }
1477 return hostapd_setup_interface_complete(iface, 0);
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001478
1479fail:
1480 hostapd_set_state(iface, HAPD_IFACE_DISABLED);
1481 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
1482 if (iface->interfaces && iface->interfaces->terminate_on_error)
1483 eloop_terminate();
1484 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001485}
1486
1487
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001488#ifdef CONFIG_FST
1489
1490static const u8 * fst_hostapd_get_bssid_cb(void *ctx)
1491{
1492 struct hostapd_data *hapd = ctx;
1493
1494 return hapd->own_addr;
1495}
1496
1497
1498static void fst_hostapd_get_channel_info_cb(void *ctx,
1499 enum hostapd_hw_mode *hw_mode,
1500 u8 *channel)
1501{
1502 struct hostapd_data *hapd = ctx;
1503
1504 *hw_mode = ieee80211_freq_to_chan(hapd->iface->freq, channel);
1505}
1506
1507
1508static void fst_hostapd_set_ies_cb(void *ctx, const struct wpabuf *fst_ies)
1509{
1510 struct hostapd_data *hapd = ctx;
1511
1512 if (hapd->iface->fst_ies != fst_ies) {
1513 hapd->iface->fst_ies = fst_ies;
1514 if (ieee802_11_set_beacon(hapd))
1515 wpa_printf(MSG_WARNING, "FST: Cannot set beacon");
1516 }
1517}
1518
1519
1520static int fst_hostapd_send_action_cb(void *ctx, const u8 *da,
1521 struct wpabuf *buf)
1522{
1523 struct hostapd_data *hapd = ctx;
1524
1525 return hostapd_drv_send_action(hapd, hapd->iface->freq, 0, da,
1526 wpabuf_head(buf), wpabuf_len(buf));
1527}
1528
1529
1530static const struct wpabuf * fst_hostapd_get_mb_ie_cb(void *ctx, const u8 *addr)
1531{
1532 struct hostapd_data *hapd = ctx;
1533 struct sta_info *sta = ap_get_sta(hapd, addr);
1534
1535 return sta ? sta->mb_ies : NULL;
1536}
1537
1538
1539static void fst_hostapd_update_mb_ie_cb(void *ctx, const u8 *addr,
1540 const u8 *buf, size_t size)
1541{
1542 struct hostapd_data *hapd = ctx;
1543 struct sta_info *sta = ap_get_sta(hapd, addr);
1544
1545 if (sta) {
1546 struct mb_ies_info info;
1547
1548 if (!mb_ies_info_by_ies(&info, buf, size)) {
1549 wpabuf_free(sta->mb_ies);
1550 sta->mb_ies = mb_ies_by_info(&info);
1551 }
1552 }
1553}
1554
1555
1556static const u8 * fst_hostapd_get_sta(struct fst_get_peer_ctx **get_ctx,
1557 Boolean mb_only)
1558{
1559 struct sta_info *s = (struct sta_info *) *get_ctx;
1560
1561 if (mb_only) {
1562 for (; s && !s->mb_ies; s = s->next)
1563 ;
1564 }
1565
1566 if (s) {
1567 *get_ctx = (struct fst_get_peer_ctx *) s->next;
1568
1569 return s->addr;
1570 }
1571
1572 *get_ctx = NULL;
1573 return NULL;
1574}
1575
1576
1577static const u8 * fst_hostapd_get_peer_first(void *ctx,
1578 struct fst_get_peer_ctx **get_ctx,
1579 Boolean mb_only)
1580{
1581 struct hostapd_data *hapd = ctx;
1582
1583 *get_ctx = (struct fst_get_peer_ctx *) hapd->sta_list;
1584
1585 return fst_hostapd_get_sta(get_ctx, mb_only);
1586}
1587
1588
1589static const u8 * fst_hostapd_get_peer_next(void *ctx,
1590 struct fst_get_peer_ctx **get_ctx,
1591 Boolean mb_only)
1592{
1593 return fst_hostapd_get_sta(get_ctx, mb_only);
1594}
1595
1596
1597void fst_hostapd_fill_iface_obj(struct hostapd_data *hapd,
1598 struct fst_wpa_obj *iface_obj)
1599{
1600 iface_obj->ctx = hapd;
1601 iface_obj->get_bssid = fst_hostapd_get_bssid_cb;
1602 iface_obj->get_channel_info = fst_hostapd_get_channel_info_cb;
1603 iface_obj->set_ies = fst_hostapd_set_ies_cb;
1604 iface_obj->send_action = fst_hostapd_send_action_cb;
1605 iface_obj->get_mb_ie = fst_hostapd_get_mb_ie_cb;
1606 iface_obj->update_mb_ie = fst_hostapd_update_mb_ie_cb;
1607 iface_obj->get_peer_first = fst_hostapd_get_peer_first;
1608 iface_obj->get_peer_next = fst_hostapd_get_peer_next;
1609}
1610
1611#endif /* CONFIG_FST */
1612
1613
Dmitry Shmidt849734c2016-05-27 09:59:01 -07001614#ifdef NEED_AP_MLME
1615static enum nr_chan_width hostapd_get_nr_chan_width(struct hostapd_data *hapd,
1616 int ht, int vht)
1617{
1618 if (!ht && !vht)
1619 return NR_CHAN_WIDTH_20;
1620 if (!hapd->iconf->secondary_channel)
1621 return NR_CHAN_WIDTH_20;
1622 if (!vht || hapd->iconf->vht_oper_chwidth == VHT_CHANWIDTH_USE_HT)
1623 return NR_CHAN_WIDTH_40;
1624 if (hapd->iconf->vht_oper_chwidth == VHT_CHANWIDTH_80MHZ)
1625 return NR_CHAN_WIDTH_80;
1626 if (hapd->iconf->vht_oper_chwidth == VHT_CHANWIDTH_160MHZ)
1627 return NR_CHAN_WIDTH_160;
1628 if (hapd->iconf->vht_oper_chwidth == VHT_CHANWIDTH_80P80MHZ)
1629 return NR_CHAN_WIDTH_80P80;
1630 return NR_CHAN_WIDTH_20;
1631}
1632#endif /* NEED_AP_MLME */
1633
1634
1635static void hostapd_set_own_neighbor_report(struct hostapd_data *hapd)
1636{
1637#ifdef NEED_AP_MLME
1638 u16 capab = hostapd_own_capab_info(hapd);
1639 int ht = hapd->iconf->ieee80211n && !hapd->conf->disable_11n;
1640 int vht = hapd->iconf->ieee80211ac && !hapd->conf->disable_11ac;
1641 struct wpa_ssid_value ssid;
1642 u8 channel, op_class;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001643 u8 center_freq1_idx = 0, center_freq2_idx = 0;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07001644 enum nr_chan_width width;
1645 u32 bssid_info;
1646 struct wpabuf *nr;
1647
1648 if (!(hapd->conf->radio_measurements[0] &
1649 WLAN_RRM_CAPS_NEIGHBOR_REPORT))
1650 return;
1651
1652 bssid_info = 3; /* AP is reachable */
1653 bssid_info |= NEI_REP_BSSID_INFO_SECURITY; /* "same as the AP" */
1654 bssid_info |= NEI_REP_BSSID_INFO_KEY_SCOPE; /* "same as the AP" */
1655
1656 if (capab & WLAN_CAPABILITY_SPECTRUM_MGMT)
1657 bssid_info |= NEI_REP_BSSID_INFO_SPECTRUM_MGMT;
1658
1659 bssid_info |= NEI_REP_BSSID_INFO_RM; /* RRM is supported */
1660
1661 if (hapd->conf->wmm_enabled) {
1662 bssid_info |= NEI_REP_BSSID_INFO_QOS;
1663
1664 if (hapd->conf->wmm_uapsd &&
1665 (hapd->iface->drv_flags & WPA_DRIVER_FLAGS_AP_UAPSD))
1666 bssid_info |= NEI_REP_BSSID_INFO_APSD;
1667 }
1668
1669 if (ht) {
1670 bssid_info |= NEI_REP_BSSID_INFO_HT |
1671 NEI_REP_BSSID_INFO_DELAYED_BA;
1672
1673 /* VHT bit added in IEEE P802.11-REVmc/D4.3 */
1674 if (vht)
1675 bssid_info |= NEI_REP_BSSID_INFO_VHT;
1676 }
1677
1678 /* TODO: Set NEI_REP_BSSID_INFO_MOBILITY_DOMAIN if MDE is set */
1679
Dmitry Shmidt29333592017-01-09 12:27:11 -08001680 if (ieee80211_freq_to_channel_ext(hapd->iface->freq,
1681 hapd->iconf->secondary_channel,
1682 hapd->iconf->vht_oper_chwidth,
1683 &op_class, &channel) ==
1684 NUM_HOSTAPD_MODES)
1685 return;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07001686 width = hostapd_get_nr_chan_width(hapd, ht, vht);
1687 if (vht) {
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001688 center_freq1_idx = hapd->iconf->vht_oper_centr_freq_seg0_idx;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07001689 if (width == NR_CHAN_WIDTH_80P80)
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001690 center_freq2_idx =
1691 hapd->iconf->vht_oper_centr_freq_seg1_idx;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07001692 } else if (ht) {
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001693 ieee80211_freq_to_chan(hapd->iface->freq +
1694 10 * hapd->iconf->secondary_channel,
1695 &center_freq1_idx);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07001696 }
1697
1698 ssid.ssid_len = hapd->conf->ssid.ssid_len;
1699 os_memcpy(ssid.ssid, hapd->conf->ssid.ssid, ssid.ssid_len);
1700
1701 /*
1702 * Neighbor Report element size = BSSID + BSSID info + op_class + chan +
1703 * phy type + wide bandwidth channel subelement.
1704 */
1705 nr = wpabuf_alloc(ETH_ALEN + 4 + 1 + 1 + 1 + 5);
1706 if (!nr)
1707 return;
1708
1709 wpabuf_put_data(nr, hapd->own_addr, ETH_ALEN);
1710 wpabuf_put_le32(nr, bssid_info);
1711 wpabuf_put_u8(nr, op_class);
1712 wpabuf_put_u8(nr, channel);
1713 wpabuf_put_u8(nr, ieee80211_get_phy_type(hapd->iface->freq, ht, vht));
1714
1715 /*
1716 * Wide Bandwidth Channel subelement may be needed to allow the
1717 * receiving STA to send packets to the AP. See IEEE P802.11-REVmc/D5.0
1718 * Figure 9-301.
1719 */
1720 wpabuf_put_u8(nr, WNM_NEIGHBOR_WIDE_BW_CHAN);
1721 wpabuf_put_u8(nr, 3);
1722 wpabuf_put_u8(nr, width);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001723 wpabuf_put_u8(nr, center_freq1_idx);
1724 wpabuf_put_u8(nr, center_freq2_idx);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07001725
1726 hostapd_neighbor_set(hapd, hapd->own_addr, &ssid, nr, hapd->iconf->lci,
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001727 hapd->iconf->civic, hapd->iconf->stationary_ap);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07001728
1729 wpabuf_free(nr);
1730#endif /* NEED_AP_MLME */
1731}
1732
1733
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001734#ifdef CONFIG_OWE
1735
1736static int hostapd_owe_iface_iter(struct hostapd_iface *iface, void *ctx)
1737{
1738 struct hostapd_data *hapd = ctx;
1739 size_t i;
1740
1741 for (i = 0; i < iface->num_bss; i++) {
1742 struct hostapd_data *bss = iface->bss[i];
1743
1744 if (os_strcmp(hapd->conf->owe_transition_ifname,
1745 bss->conf->iface) != 0)
1746 continue;
1747
1748 wpa_printf(MSG_DEBUG,
1749 "OWE: ifname=%s found transition mode ifname=%s BSSID "
1750 MACSTR " SSID %s",
1751 hapd->conf->iface, bss->conf->iface,
1752 MAC2STR(bss->own_addr),
1753 wpa_ssid_txt(bss->conf->ssid.ssid,
1754 bss->conf->ssid.ssid_len));
1755 if (!bss->conf->ssid.ssid_set || !bss->conf->ssid.ssid_len ||
1756 is_zero_ether_addr(bss->own_addr))
1757 continue;
1758
1759 os_memcpy(hapd->conf->owe_transition_bssid, bss->own_addr,
1760 ETH_ALEN);
1761 os_memcpy(hapd->conf->owe_transition_ssid,
1762 bss->conf->ssid.ssid, bss->conf->ssid.ssid_len);
1763 hapd->conf->owe_transition_ssid_len = bss->conf->ssid.ssid_len;
1764 wpa_printf(MSG_DEBUG,
1765 "OWE: Copied transition mode information");
1766 return 1;
1767 }
1768
1769 return 0;
1770}
1771
1772
1773int hostapd_owe_trans_get_info(struct hostapd_data *hapd)
1774{
1775 if (hapd->conf->owe_transition_ssid_len > 0 &&
1776 !is_zero_ether_addr(hapd->conf->owe_transition_bssid))
1777 return 0;
1778
1779 /* Find transition mode SSID/BSSID information from a BSS operated by
1780 * this hostapd instance. */
1781 if (!hapd->iface->interfaces ||
1782 !hapd->iface->interfaces->for_each_interface)
1783 return hostapd_owe_iface_iter(hapd->iface, hapd);
1784 else
1785 return hapd->iface->interfaces->for_each_interface(
1786 hapd->iface->interfaces, hostapd_owe_iface_iter, hapd);
1787}
1788
1789
1790static int hostapd_owe_iface_iter2(struct hostapd_iface *iface, void *ctx)
1791{
1792 size_t i;
1793
1794 for (i = 0; i < iface->num_bss; i++) {
1795 struct hostapd_data *bss = iface->bss[i];
1796 int res;
1797
1798 if (!bss->conf->owe_transition_ifname[0])
1799 continue;
1800 res = hostapd_owe_trans_get_info(bss);
1801 if (res == 0)
1802 continue;
1803 wpa_printf(MSG_DEBUG,
1804 "OWE: Matching transition mode interface enabled - update beacon data for %s",
1805 bss->conf->iface);
1806 ieee802_11_set_beacon(bss);
1807 }
1808
1809 return 0;
1810}
1811
1812#endif /* CONFIG_OWE */
1813
1814
1815static void hostapd_owe_update_trans(struct hostapd_iface *iface)
1816{
1817#ifdef CONFIG_OWE
1818 /* Check whether the enabled BSS can complete OWE transition mode
1819 * configuration for any pending interface. */
1820 if (!iface->interfaces ||
1821 !iface->interfaces->for_each_interface)
1822 hostapd_owe_iface_iter2(iface, NULL);
1823 else
1824 iface->interfaces->for_each_interface(
1825 iface->interfaces, hostapd_owe_iface_iter2, NULL);
1826#endif /* CONFIG_OWE */
1827}
1828
1829
Roshan Pius3a1667e2018-07-03 15:17:14 -07001830static void hostapd_interface_setup_failure_handler(void *eloop_ctx,
1831 void *timeout_ctx)
1832{
1833 struct hostapd_iface *iface = eloop_ctx;
1834 struct hostapd_data *hapd;
1835
1836 if (iface->num_bss < 1 || !iface->bss || !iface->bss[0])
1837 return;
1838 hapd = iface->bss[0];
1839 if (hapd->setup_complete_cb)
1840 hapd->setup_complete_cb(hapd->setup_complete_cb_ctx);
1841}
1842
1843
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001844static int hostapd_setup_interface_complete_sync(struct hostapd_iface *iface,
1845 int err)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001846{
1847 struct hostapd_data *hapd = iface->bss[0];
1848 size_t j;
1849 u8 *prev_addr;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001850 int delay_apply_cfg = 0;
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001851 int res_dfs_offload = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001852
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001853 if (err)
1854 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001855
1856 wpa_printf(MSG_DEBUG, "Completing interface initialization");
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001857 if (iface->conf->channel) {
Dmitry Shmidt051af732013-10-22 13:52:46 -07001858#ifdef NEED_AP_MLME
1859 int res;
1860#endif /* NEED_AP_MLME */
1861
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001862 iface->freq = hostapd_hw_get_freq(hapd, iface->conf->channel);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001863 wpa_printf(MSG_DEBUG, "Mode: %s Channel: %d "
1864 "Frequency: %d MHz",
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001865 hostapd_hw_mode_txt(iface->conf->hw_mode),
1866 iface->conf->channel, iface->freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001867
Dmitry Shmidt051af732013-10-22 13:52:46 -07001868#ifdef NEED_AP_MLME
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001869 /* Handle DFS only if it is not offloaded to the driver */
1870 if (!(iface->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD)) {
1871 /* Check DFS */
1872 res = hostapd_handle_dfs(iface);
1873 if (res <= 0) {
1874 if (res < 0)
1875 goto fail;
1876 return res;
1877 }
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001878 } else {
1879 /* If DFS is offloaded to the driver */
1880 res_dfs_offload = hostapd_handle_dfs_offload(iface);
1881 if (res_dfs_offload <= 0) {
1882 if (res_dfs_offload < 0)
1883 goto fail;
1884 } else {
1885 wpa_printf(MSG_DEBUG,
1886 "Proceed with AP/channel setup");
1887 /*
1888 * If this is a DFS channel, move to completing
1889 * AP setup.
1890 */
1891 if (res_dfs_offload == 1)
1892 goto dfs_offload;
1893 /* Otherwise fall through. */
1894 }
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001895 }
Dmitry Shmidt051af732013-10-22 13:52:46 -07001896#endif /* NEED_AP_MLME */
1897
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001898#ifdef CONFIG_MESH
1899 if (iface->mconf != NULL) {
1900 wpa_printf(MSG_DEBUG,
1901 "%s: Mesh configuration will be applied while joining the mesh network",
1902 iface->bss[0]->conf->iface);
1903 delay_apply_cfg = 1;
1904 }
1905#endif /* CONFIG_MESH */
1906
1907 if (!delay_apply_cfg &&
1908 hostapd_set_freq(hapd, hapd->iconf->hw_mode, iface->freq,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001909 hapd->iconf->channel,
1910 hapd->iconf->ieee80211n,
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001911 hapd->iconf->ieee80211ac,
1912 hapd->iconf->secondary_channel,
1913 hapd->iconf->vht_oper_chwidth,
1914 hapd->iconf->vht_oper_centr_freq_seg0_idx,
1915 hapd->iconf->vht_oper_centr_freq_seg1_idx)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001916 wpa_printf(MSG_ERROR, "Could not set channel for "
1917 "kernel driver");
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001918 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001919 }
1920 }
1921
1922 if (iface->current_mode) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001923 if (hostapd_prepare_rates(iface, iface->current_mode)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001924 wpa_printf(MSG_ERROR, "Failed to prepare rates "
1925 "table.");
1926 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
1927 HOSTAPD_LEVEL_WARNING,
1928 "Failed to prepare rates table.");
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001929 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001930 }
1931 }
1932
1933 if (hapd->iconf->rts_threshold > -1 &&
1934 hostapd_set_rts(hapd, hapd->iconf->rts_threshold)) {
1935 wpa_printf(MSG_ERROR, "Could not set RTS threshold for "
1936 "kernel driver");
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001937 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001938 }
1939
1940 if (hapd->iconf->fragm_threshold > -1 &&
1941 hostapd_set_frag(hapd, hapd->iconf->fragm_threshold)) {
1942 wpa_printf(MSG_ERROR, "Could not set fragmentation threshold "
1943 "for kernel driver");
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001944 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001945 }
1946
1947 prev_addr = hapd->own_addr;
1948
1949 for (j = 0; j < iface->num_bss; j++) {
1950 hapd = iface->bss[j];
1951 if (j)
1952 os_memcpy(hapd->own_addr, prev_addr, ETH_ALEN);
Dmitry Shmidt71757432014-06-02 13:50:35 -07001953 if (hostapd_setup_bss(hapd, j == 0)) {
1954 do {
1955 hapd = iface->bss[j];
1956 hostapd_bss_deinit_no_free(hapd);
1957 hostapd_free_hapd_data(hapd);
1958 } while (j-- > 0);
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001959 goto fail;
Dmitry Shmidt71757432014-06-02 13:50:35 -07001960 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001961 if (is_zero_ether_addr(hapd->conf->bssid))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001962 prev_addr = hapd->own_addr;
1963 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001964 hapd = iface->bss[0];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001965
1966 hostapd_tx_queue_params(iface);
1967
1968 ap_list_init(iface);
1969
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001970 hostapd_set_acl(hapd);
1971
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001972 if (hostapd_driver_commit(hapd) < 0) {
1973 wpa_printf(MSG_ERROR, "%s: Failed to commit driver "
1974 "configuration", __func__);
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001975 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001976 }
1977
Jouni Malinen87fd2792011-05-16 18:35:42 +03001978 /*
1979 * WPS UPnP module can be initialized only when the "upnp_iface" is up.
1980 * If "interface" and "upnp_iface" are the same (e.g., non-bridge
1981 * mode), the interface is up only after driver_commit, so initialize
1982 * WPS after driver_commit.
1983 */
1984 for (j = 0; j < iface->num_bss; j++) {
1985 if (hostapd_init_wps_complete(iface->bss[j]))
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001986 goto fail;
Jouni Malinen87fd2792011-05-16 18:35:42 +03001987 }
1988
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001989 if ((iface->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
1990 !res_dfs_offload) {
1991 /*
1992 * If freq is DFS, and DFS is offloaded to the driver, then wait
1993 * for CAC to complete.
1994 */
1995 wpa_printf(MSG_DEBUG, "%s: Wait for CAC to complete", __func__);
1996 return res_dfs_offload;
1997 }
1998
1999#ifdef NEED_AP_MLME
2000dfs_offload:
2001#endif /* NEED_AP_MLME */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002002
2003#ifdef CONFIG_FST
2004 if (hapd->iconf->fst_cfg.group_id[0]) {
2005 struct fst_wpa_obj iface_obj;
2006
2007 fst_hostapd_fill_iface_obj(hapd, &iface_obj);
2008 iface->fst = fst_attach(hapd->conf->iface, hapd->own_addr,
2009 &iface_obj, &hapd->iconf->fst_cfg);
2010 if (!iface->fst) {
2011 wpa_printf(MSG_ERROR, "Could not attach to FST %s",
2012 hapd->iconf->fst_cfg.group_id);
2013 goto fail;
2014 }
2015 }
2016#endif /* CONFIG_FST */
2017
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002018 hostapd_set_state(iface, HAPD_IFACE_ENABLED);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002019 hostapd_owe_update_trans(iface);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002020 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_ENABLED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002021 if (hapd->setup_complete_cb)
2022 hapd->setup_complete_cb(hapd->setup_complete_cb_ctx);
2023
2024 wpa_printf(MSG_DEBUG, "%s: Setup of interface done.",
2025 iface->bss[0]->conf->iface);
Dmitry Shmidtb96dad42013-11-05 10:07:29 -08002026 if (iface->interfaces && iface->interfaces->terminate_on_error > 0)
2027 iface->interfaces->terminate_on_error--;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002028
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002029 for (j = 0; j < iface->num_bss; j++)
2030 hostapd_set_own_neighbor_report(iface->bss[j]);
2031
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002032 return 0;
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002033
2034fail:
2035 wpa_printf(MSG_ERROR, "Interface initialization failed");
2036 hostapd_set_state(iface, HAPD_IFACE_DISABLED);
2037 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002038#ifdef CONFIG_FST
2039 if (iface->fst) {
2040 fst_detach(iface->fst);
2041 iface->fst = NULL;
2042 }
2043#endif /* CONFIG_FST */
Roshan Pius3a1667e2018-07-03 15:17:14 -07002044
2045 if (iface->interfaces && iface->interfaces->terminate_on_error) {
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002046 eloop_terminate();
Roshan Pius3a1667e2018-07-03 15:17:14 -07002047 } else if (hapd->setup_complete_cb) {
2048 /*
2049 * Calling hapd->setup_complete_cb directly may cause iface
2050 * deinitialization which may be accessed later by the caller.
2051 */
2052 eloop_register_timeout(0, 0,
2053 hostapd_interface_setup_failure_handler,
2054 iface, NULL);
2055 }
2056
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002057 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002058}
2059
2060
2061/**
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002062 * hostapd_setup_interface_complete - Complete interface setup
2063 *
2064 * This function is called when previous steps in the interface setup has been
2065 * completed. This can also start operations, e.g., DFS, that will require
2066 * additional processing before interface is ready to be enabled. Such
2067 * operations will call this function from eloop callbacks when finished.
2068 */
2069int hostapd_setup_interface_complete(struct hostapd_iface *iface, int err)
2070{
2071 struct hapd_interfaces *interfaces = iface->interfaces;
2072 struct hostapd_data *hapd = iface->bss[0];
2073 unsigned int i;
2074 int not_ready_in_sync_ifaces = 0;
2075
2076 if (!iface->need_to_start_in_sync)
2077 return hostapd_setup_interface_complete_sync(iface, err);
2078
2079 if (err) {
2080 wpa_printf(MSG_ERROR, "Interface initialization failed");
2081 hostapd_set_state(iface, HAPD_IFACE_DISABLED);
2082 iface->need_to_start_in_sync = 0;
2083 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
2084 if (interfaces && interfaces->terminate_on_error)
2085 eloop_terminate();
2086 return -1;
2087 }
2088
2089 if (iface->ready_to_start_in_sync) {
2090 /* Already in ready and waiting. should never happpen */
2091 return 0;
2092 }
2093
2094 for (i = 0; i < interfaces->count; i++) {
2095 if (interfaces->iface[i]->need_to_start_in_sync &&
2096 !interfaces->iface[i]->ready_to_start_in_sync)
2097 not_ready_in_sync_ifaces++;
2098 }
2099
2100 /*
2101 * Check if this is the last interface, if yes then start all the other
2102 * waiting interfaces. If not, add this interface to the waiting list.
2103 */
2104 if (not_ready_in_sync_ifaces > 1 && iface->state == HAPD_IFACE_DFS) {
2105 /*
2106 * If this interface went through CAC, do not synchronize, just
2107 * start immediately.
2108 */
2109 iface->need_to_start_in_sync = 0;
2110 wpa_printf(MSG_INFO,
2111 "%s: Finished CAC - bypass sync and start interface",
2112 iface->bss[0]->conf->iface);
2113 return hostapd_setup_interface_complete_sync(iface, err);
2114 }
2115
2116 if (not_ready_in_sync_ifaces > 1) {
2117 /* need to wait as there are other interfaces still coming up */
2118 iface->ready_to_start_in_sync = 1;
2119 wpa_printf(MSG_INFO,
2120 "%s: Interface waiting to sync with other interfaces",
2121 iface->bss[0]->conf->iface);
2122 return 0;
2123 }
2124
2125 wpa_printf(MSG_INFO,
2126 "%s: Last interface to sync - starting all interfaces",
2127 iface->bss[0]->conf->iface);
2128 iface->need_to_start_in_sync = 0;
2129 hostapd_setup_interface_complete_sync(iface, err);
2130 for (i = 0; i < interfaces->count; i++) {
2131 if (interfaces->iface[i]->need_to_start_in_sync &&
2132 interfaces->iface[i]->ready_to_start_in_sync) {
2133 hostapd_setup_interface_complete_sync(
2134 interfaces->iface[i], 0);
2135 /* Only once the interfaces are sync started */
2136 interfaces->iface[i]->need_to_start_in_sync = 0;
2137 }
2138 }
2139
2140 return 0;
2141}
2142
2143
2144/**
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002145 * hostapd_setup_interface - Setup of an interface
2146 * @iface: Pointer to interface data.
2147 * Returns: 0 on success, -1 on failure
2148 *
2149 * Initializes the driver interface, validates the configuration,
2150 * and sets driver parameters based on the configuration.
2151 * Flushes old stations, sets the channel, encryption,
2152 * beacons, and WDS links based on the configuration.
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002153 *
2154 * If interface setup requires more time, e.g., to perform HT co-ex scans, ACS,
2155 * or DFS operations, this function returns 0 before such operations have been
2156 * completed. The pending operations are registered into eloop and will be
2157 * completed from eloop callbacks. Those callbacks end up calling
2158 * hostapd_setup_interface_complete() once setup has been completed.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002159 */
2160int hostapd_setup_interface(struct hostapd_iface *iface)
2161{
2162 int ret;
2163
2164 ret = setup_interface(iface);
2165 if (ret) {
2166 wpa_printf(MSG_ERROR, "%s: Unable to setup interface.",
2167 iface->bss[0]->conf->iface);
2168 return -1;
2169 }
2170
2171 return 0;
2172}
2173
2174
2175/**
2176 * hostapd_alloc_bss_data - Allocate and initialize per-BSS data
2177 * @hapd_iface: Pointer to interface data
2178 * @conf: Pointer to per-interface configuration
2179 * @bss: Pointer to per-BSS configuration for this BSS
2180 * Returns: Pointer to allocated BSS data
2181 *
2182 * This function is used to allocate per-BSS data structure. This data will be
2183 * freed after hostapd_cleanup() is called for it during interface
2184 * deinitialization.
2185 */
2186struct hostapd_data *
2187hostapd_alloc_bss_data(struct hostapd_iface *hapd_iface,
2188 struct hostapd_config *conf,
2189 struct hostapd_bss_config *bss)
2190{
2191 struct hostapd_data *hapd;
2192
2193 hapd = os_zalloc(sizeof(*hapd));
2194 if (hapd == NULL)
2195 return NULL;
2196
2197 hapd->new_assoc_sta_cb = hostapd_new_assoc_sta;
2198 hapd->iconf = conf;
2199 hapd->conf = bss;
2200 hapd->iface = hapd_iface;
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08002201 if (conf)
2202 hapd->driver = conf->driver;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002203 hapd->ctrl_sock = -1;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08002204 dl_list_init(&hapd->ctrl_dst);
Dmitry Shmidt7d175302016-09-06 13:11:34 -07002205 dl_list_init(&hapd->nr_db);
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08002206 hapd->dhcp_sock = -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002207#ifdef CONFIG_IEEE80211R_AP
2208 dl_list_init(&hapd->l2_queue);
2209 dl_list_init(&hapd->l2_oui_queue);
2210#endif /* CONFIG_IEEE80211R_AP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002211
2212 return hapd;
2213}
2214
2215
Dmitry Shmidt54605472013-11-08 11:10:19 -08002216static void hostapd_bss_deinit(struct hostapd_data *hapd)
2217{
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07002218 if (!hapd)
2219 return;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002220 wpa_printf(MSG_DEBUG, "%s: deinit bss %s", __func__,
2221 hapd->conf->iface);
Dmitry Shmidt71757432014-06-02 13:50:35 -07002222 hostapd_bss_deinit_no_free(hapd);
Dmitry Shmidtf73259c2015-03-17 11:00:54 -07002223 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
Dmitry Shmidt54605472013-11-08 11:10:19 -08002224 hostapd_cleanup(hapd);
2225}
2226
2227
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002228void hostapd_interface_deinit(struct hostapd_iface *iface)
2229{
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002230 int j;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002231
Dmitry Shmidt54605472013-11-08 11:10:19 -08002232 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002233 if (iface == NULL)
2234 return;
2235
Dmitry Shmidtf73259c2015-03-17 11:00:54 -07002236 hostapd_set_state(iface, HAPD_IFACE_DISABLED);
2237
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002238 eloop_cancel_timeout(channel_list_update_timeout, iface, NULL);
2239 iface->wait_channel_update = 0;
2240
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002241#ifdef CONFIG_FST
2242 if (iface->fst) {
2243 fst_detach(iface->fst);
2244 iface->fst = NULL;
2245 }
2246#endif /* CONFIG_FST */
2247
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07002248 for (j = iface->num_bss - 1; j >= 0; j--) {
2249 if (!iface->bss)
2250 break;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002251 hostapd_bss_deinit(iface->bss[j]);
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07002252 }
Roshan Pius3a1667e2018-07-03 15:17:14 -07002253
2254#ifdef CONFIG_IEEE80211N
2255#ifdef NEED_AP_MLME
2256 hostapd_stop_setup_timers(iface);
2257 eloop_cancel_timeout(ap_ht2040_timeout, iface, NULL);
2258#endif /* NEED_AP_MLME */
2259#endif /* CONFIG_IEEE80211N */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002260}
2261
2262
2263void hostapd_interface_free(struct hostapd_iface *iface)
2264{
2265 size_t j;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002266 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
2267 for (j = 0; j < iface->num_bss; j++) {
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07002268 if (!iface->bss)
2269 break;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002270 wpa_printf(MSG_DEBUG, "%s: free hapd %p",
2271 __func__, iface->bss[j]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002272 os_free(iface->bss[j]);
Dmitry Shmidt54605472013-11-08 11:10:19 -08002273 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002274 hostapd_cleanup_iface(iface);
2275}
2276
2277
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07002278struct hostapd_iface * hostapd_alloc_iface(void)
2279{
2280 struct hostapd_iface *hapd_iface;
2281
2282 hapd_iface = os_zalloc(sizeof(*hapd_iface));
2283 if (!hapd_iface)
2284 return NULL;
2285
2286 dl_list_init(&hapd_iface->sta_seen);
2287
2288 return hapd_iface;
2289}
2290
2291
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002292/**
2293 * hostapd_init - Allocate and initialize per-interface data
2294 * @config_file: Path to the configuration file
2295 * Returns: Pointer to the allocated interface data or %NULL on failure
2296 *
2297 * This function is used to allocate main data structures for per-interface
2298 * data. The allocated data buffer will be freed by calling
2299 * hostapd_cleanup_iface().
2300 */
2301struct hostapd_iface * hostapd_init(struct hapd_interfaces *interfaces,
2302 const char *config_file)
2303{
2304 struct hostapd_iface *hapd_iface = NULL;
2305 struct hostapd_config *conf = NULL;
2306 struct hostapd_data *hapd;
2307 size_t i;
2308
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07002309 hapd_iface = hostapd_alloc_iface();
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002310 if (hapd_iface == NULL)
2311 goto fail;
2312
2313 hapd_iface->config_fname = os_strdup(config_file);
2314 if (hapd_iface->config_fname == NULL)
2315 goto fail;
2316
2317 conf = interfaces->config_read_cb(hapd_iface->config_fname);
2318 if (conf == NULL)
2319 goto fail;
2320 hapd_iface->conf = conf;
2321
2322 hapd_iface->num_bss = conf->num_bss;
2323 hapd_iface->bss = os_calloc(conf->num_bss,
2324 sizeof(struct hostapd_data *));
2325 if (hapd_iface->bss == NULL)
2326 goto fail;
2327
2328 for (i = 0; i < conf->num_bss; i++) {
2329 hapd = hapd_iface->bss[i] =
2330 hostapd_alloc_bss_data(hapd_iface, conf,
2331 conf->bss[i]);
2332 if (hapd == NULL)
2333 goto fail;
2334 hapd->msg_ctx = hapd;
2335 }
2336
2337 return hapd_iface;
2338
2339fail:
2340 wpa_printf(MSG_ERROR, "Failed to set up interface with %s",
2341 config_file);
2342 if (conf)
2343 hostapd_config_free(conf);
2344 if (hapd_iface) {
2345 os_free(hapd_iface->config_fname);
2346 os_free(hapd_iface->bss);
Dmitry Shmidt54605472013-11-08 11:10:19 -08002347 wpa_printf(MSG_DEBUG, "%s: free iface %p",
2348 __func__, hapd_iface);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002349 os_free(hapd_iface);
2350 }
2351 return NULL;
2352}
2353
2354
2355static int ifname_in_use(struct hapd_interfaces *interfaces, const char *ifname)
2356{
2357 size_t i, j;
2358
2359 for (i = 0; i < interfaces->count; i++) {
2360 struct hostapd_iface *iface = interfaces->iface[i];
2361 for (j = 0; j < iface->num_bss; j++) {
2362 struct hostapd_data *hapd = iface->bss[j];
2363 if (os_strcmp(ifname, hapd->conf->iface) == 0)
2364 return 1;
2365 }
2366 }
2367
2368 return 0;
2369}
2370
2371
2372/**
2373 * hostapd_interface_init_bss - Read configuration file and init BSS data
2374 *
2375 * This function is used to parse configuration file for a BSS. This BSS is
2376 * added to an existing interface sharing the same radio (if any) or a new
2377 * interface is created if this is the first interface on a radio. This
2378 * allocate memory for the BSS. No actual driver operations are started.
2379 *
2380 * This is similar to hostapd_interface_init(), but for a case where the
2381 * configuration is used to add a single BSS instead of all BSSes for a radio.
2382 */
2383struct hostapd_iface *
2384hostapd_interface_init_bss(struct hapd_interfaces *interfaces, const char *phy,
2385 const char *config_fname, int debug)
2386{
2387 struct hostapd_iface *new_iface = NULL, *iface = NULL;
2388 struct hostapd_data *hapd;
2389 int k;
2390 size_t i, bss_idx;
2391
2392 if (!phy || !*phy)
2393 return NULL;
2394
2395 for (i = 0; i < interfaces->count; i++) {
2396 if (os_strcmp(interfaces->iface[i]->phy, phy) == 0) {
2397 iface = interfaces->iface[i];
2398 break;
2399 }
2400 }
2401
2402 wpa_printf(MSG_INFO, "Configuration file: %s (phy %s)%s",
2403 config_fname, phy, iface ? "" : " --> new PHY");
2404 if (iface) {
2405 struct hostapd_config *conf;
2406 struct hostapd_bss_config **tmp_conf;
2407 struct hostapd_data **tmp_bss;
2408 struct hostapd_bss_config *bss;
2409 const char *ifname;
2410
2411 /* Add new BSS to existing iface */
2412 conf = interfaces->config_read_cb(config_fname);
2413 if (conf == NULL)
2414 return NULL;
2415 if (conf->num_bss > 1) {
2416 wpa_printf(MSG_ERROR, "Multiple BSSes specified in BSS-config");
2417 hostapd_config_free(conf);
2418 return NULL;
2419 }
2420
2421 ifname = conf->bss[0]->iface;
2422 if (ifname[0] != '\0' && ifname_in_use(interfaces, ifname)) {
2423 wpa_printf(MSG_ERROR,
2424 "Interface name %s already in use", ifname);
2425 hostapd_config_free(conf);
2426 return NULL;
2427 }
2428
2429 tmp_conf = os_realloc_array(
2430 iface->conf->bss, iface->conf->num_bss + 1,
2431 sizeof(struct hostapd_bss_config *));
2432 tmp_bss = os_realloc_array(iface->bss, iface->num_bss + 1,
2433 sizeof(struct hostapd_data *));
2434 if (tmp_bss)
2435 iface->bss = tmp_bss;
2436 if (tmp_conf) {
2437 iface->conf->bss = tmp_conf;
2438 iface->conf->last_bss = tmp_conf[0];
2439 }
2440 if (tmp_bss == NULL || tmp_conf == NULL) {
2441 hostapd_config_free(conf);
2442 return NULL;
2443 }
2444 bss = iface->conf->bss[iface->conf->num_bss] = conf->bss[0];
2445 iface->conf->num_bss++;
2446
2447 hapd = hostapd_alloc_bss_data(iface, iface->conf, bss);
2448 if (hapd == NULL) {
2449 iface->conf->num_bss--;
2450 hostapd_config_free(conf);
2451 return NULL;
2452 }
2453 iface->conf->last_bss = bss;
2454 iface->bss[iface->num_bss] = hapd;
2455 hapd->msg_ctx = hapd;
2456
2457 bss_idx = iface->num_bss++;
2458 conf->num_bss--;
2459 conf->bss[0] = NULL;
2460 hostapd_config_free(conf);
2461 } else {
2462 /* Add a new iface with the first BSS */
2463 new_iface = iface = hostapd_init(interfaces, config_fname);
2464 if (!iface)
2465 return NULL;
2466 os_strlcpy(iface->phy, phy, sizeof(iface->phy));
2467 iface->interfaces = interfaces;
2468 bss_idx = 0;
2469 }
2470
2471 for (k = 0; k < debug; k++) {
2472 if (iface->bss[bss_idx]->conf->logger_stdout_level > 0)
2473 iface->bss[bss_idx]->conf->logger_stdout_level--;
2474 }
2475
2476 if (iface->conf->bss[bss_idx]->iface[0] == '\0' &&
2477 !hostapd_drv_none(iface->bss[bss_idx])) {
2478 wpa_printf(MSG_ERROR, "Interface name not specified in %s",
2479 config_fname);
2480 if (new_iface)
2481 hostapd_interface_deinit_free(new_iface);
2482 return NULL;
2483 }
2484
2485 return iface;
2486}
2487
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002488
2489void hostapd_interface_deinit_free(struct hostapd_iface *iface)
2490{
2491 const struct wpa_driver_ops *driver;
2492 void *drv_priv;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002493
2494 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002495 if (iface == NULL)
2496 return;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002497 wpa_printf(MSG_DEBUG, "%s: num_bss=%u conf->num_bss=%u",
2498 __func__, (unsigned int) iface->num_bss,
2499 (unsigned int) iface->conf->num_bss);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002500 driver = iface->bss[0]->driver;
2501 drv_priv = iface->bss[0]->drv_priv;
2502 hostapd_interface_deinit(iface);
Dmitry Shmidt54605472013-11-08 11:10:19 -08002503 wpa_printf(MSG_DEBUG, "%s: driver=%p drv_priv=%p -> hapd_deinit",
2504 __func__, driver, drv_priv);
Dmitry Shmidt71757432014-06-02 13:50:35 -07002505 if (driver && driver->hapd_deinit && drv_priv) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002506 driver->hapd_deinit(drv_priv);
Dmitry Shmidt71757432014-06-02 13:50:35 -07002507 iface->bss[0]->drv_priv = NULL;
2508 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002509 hostapd_interface_free(iface);
2510}
2511
2512
Dmitry Shmidt15907092014-03-25 10:42:57 -07002513static void hostapd_deinit_driver(const struct wpa_driver_ops *driver,
2514 void *drv_priv,
2515 struct hostapd_iface *hapd_iface)
2516{
2517 size_t j;
2518
2519 wpa_printf(MSG_DEBUG, "%s: driver=%p drv_priv=%p -> hapd_deinit",
2520 __func__, driver, drv_priv);
2521 if (driver && driver->hapd_deinit && drv_priv) {
2522 driver->hapd_deinit(drv_priv);
2523 for (j = 0; j < hapd_iface->num_bss; j++) {
2524 wpa_printf(MSG_DEBUG, "%s:bss[%d]->drv_priv=%p",
2525 __func__, (int) j,
2526 hapd_iface->bss[j]->drv_priv);
2527 if (hapd_iface->bss[j]->drv_priv == drv_priv)
2528 hapd_iface->bss[j]->drv_priv = NULL;
2529 }
2530 }
2531}
2532
2533
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002534int hostapd_enable_iface(struct hostapd_iface *hapd_iface)
2535{
Dmitry Shmidt71757432014-06-02 13:50:35 -07002536 size_t j;
2537
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002538 if (hapd_iface->bss[0]->drv_priv != NULL) {
2539 wpa_printf(MSG_ERROR, "Interface %s already enabled",
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002540 hapd_iface->conf->bss[0]->iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002541 return -1;
2542 }
2543
2544 wpa_printf(MSG_DEBUG, "Enable interface %s",
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002545 hapd_iface->conf->bss[0]->iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002546
Dmitry Shmidt71757432014-06-02 13:50:35 -07002547 for (j = 0; j < hapd_iface->num_bss; j++)
2548 hostapd_set_security_params(hapd_iface->conf->bss[j], 1);
Dmitry Shmidt344abd32014-01-14 13:17:00 -08002549 if (hostapd_config_check(hapd_iface->conf, 1) < 0) {
2550 wpa_printf(MSG_INFO, "Invalid configuration - cannot enable");
2551 return -1;
2552 }
2553
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002554 if (hapd_iface->interfaces == NULL ||
2555 hapd_iface->interfaces->driver_init == NULL ||
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002556 hapd_iface->interfaces->driver_init(hapd_iface))
2557 return -1;
2558
2559 if (hostapd_setup_interface(hapd_iface)) {
Dmitry Shmidt15907092014-03-25 10:42:57 -07002560 hostapd_deinit_driver(hapd_iface->bss[0]->driver,
2561 hapd_iface->bss[0]->drv_priv,
2562 hapd_iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002563 return -1;
2564 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002565
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002566 return 0;
2567}
2568
2569
2570int hostapd_reload_iface(struct hostapd_iface *hapd_iface)
2571{
2572 size_t j;
2573
2574 wpa_printf(MSG_DEBUG, "Reload interface %s",
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002575 hapd_iface->conf->bss[0]->iface);
2576 for (j = 0; j < hapd_iface->num_bss; j++)
Dmitry Shmidt71757432014-06-02 13:50:35 -07002577 hostapd_set_security_params(hapd_iface->conf->bss[j], 1);
Dmitry Shmidt344abd32014-01-14 13:17:00 -08002578 if (hostapd_config_check(hapd_iface->conf, 1) < 0) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002579 wpa_printf(MSG_ERROR, "Updated configuration is invalid");
2580 return -1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002581 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002582 hostapd_clear_old(hapd_iface);
2583 for (j = 0; j < hapd_iface->num_bss; j++)
2584 hostapd_reload_bss(hapd_iface->bss[j]);
2585
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002586 return 0;
2587}
2588
2589
2590int hostapd_disable_iface(struct hostapd_iface *hapd_iface)
2591{
2592 size_t j;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002593 const struct wpa_driver_ops *driver;
2594 void *drv_priv;
2595
2596 if (hapd_iface == NULL)
2597 return -1;
Dmitry Shmidt71757432014-06-02 13:50:35 -07002598
2599 if (hapd_iface->bss[0]->drv_priv == NULL) {
2600 wpa_printf(MSG_INFO, "Interface %s already disabled",
2601 hapd_iface->conf->bss[0]->iface);
2602 return -1;
2603 }
2604
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002605 wpa_msg(hapd_iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002606 driver = hapd_iface->bss[0]->driver;
2607 drv_priv = hapd_iface->bss[0]->drv_priv;
2608
Dmitry Shmidta38abf92014-03-06 13:38:44 -08002609 hapd_iface->driver_ap_teardown =
2610 !!(hapd_iface->drv_flags &
2611 WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
2612
2613 /* same as hostapd_interface_deinit without deinitializing ctrl-iface */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002614 for (j = 0; j < hapd_iface->num_bss; j++) {
2615 struct hostapd_data *hapd = hapd_iface->bss[j];
Dmitry Shmidt71757432014-06-02 13:50:35 -07002616 hostapd_bss_deinit_no_free(hapd);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002617 hostapd_free_hapd_data(hapd);
2618 }
2619
Dmitry Shmidt15907092014-03-25 10:42:57 -07002620 hostapd_deinit_driver(driver, drv_priv, hapd_iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002621
2622 /* From hostapd_cleanup_iface: These were initialized in
2623 * hostapd_setup_interface and hostapd_setup_interface_complete
2624 */
2625 hostapd_cleanup_iface_partial(hapd_iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002626
Dmitry Shmidt56052862013-10-04 10:23:25 -07002627 wpa_printf(MSG_DEBUG, "Interface %s disabled",
2628 hapd_iface->bss[0]->conf->iface);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002629 hostapd_set_state(hapd_iface, HAPD_IFACE_DISABLED);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002630 return 0;
2631}
2632
2633
2634static struct hostapd_iface *
2635hostapd_iface_alloc(struct hapd_interfaces *interfaces)
2636{
2637 struct hostapd_iface **iface, *hapd_iface;
2638
2639 iface = os_realloc_array(interfaces->iface, interfaces->count + 1,
2640 sizeof(struct hostapd_iface *));
2641 if (iface == NULL)
2642 return NULL;
2643 interfaces->iface = iface;
2644 hapd_iface = interfaces->iface[interfaces->count] =
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07002645 hostapd_alloc_iface();
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002646 if (hapd_iface == NULL) {
2647 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory for "
2648 "the interface", __func__);
2649 return NULL;
2650 }
2651 interfaces->count++;
2652 hapd_iface->interfaces = interfaces;
2653
2654 return hapd_iface;
2655}
2656
2657
2658static struct hostapd_config *
2659hostapd_config_alloc(struct hapd_interfaces *interfaces, const char *ifname,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002660 const char *ctrl_iface, const char *driver)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002661{
2662 struct hostapd_bss_config *bss;
2663 struct hostapd_config *conf;
2664
2665 /* Allocates memory for bss and conf */
2666 conf = hostapd_config_defaults();
2667 if (conf == NULL) {
2668 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory for "
2669 "configuration", __func__);
2670 return NULL;
2671 }
2672
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002673 if (driver) {
2674 int j;
2675
2676 for (j = 0; wpa_drivers[j]; j++) {
2677 if (os_strcmp(driver, wpa_drivers[j]->name) == 0) {
2678 conf->driver = wpa_drivers[j];
2679 goto skip;
2680 }
2681 }
2682
2683 wpa_printf(MSG_ERROR,
2684 "Invalid/unknown driver '%s' - registering the default driver",
2685 driver);
2686 }
2687
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002688 conf->driver = wpa_drivers[0];
2689 if (conf->driver == NULL) {
2690 wpa_printf(MSG_ERROR, "No driver wrappers registered!");
2691 hostapd_config_free(conf);
2692 return NULL;
2693 }
2694
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002695skip:
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002696 bss = conf->last_bss = conf->bss[0];
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002697
2698 os_strlcpy(bss->iface, ifname, sizeof(bss->iface));
2699 bss->ctrl_interface = os_strdup(ctrl_iface);
2700 if (bss->ctrl_interface == NULL) {
2701 hostapd_config_free(conf);
2702 return NULL;
2703 }
2704
2705 /* Reading configuration file skipped, will be done in SET!
2706 * From reading the configuration till the end has to be done in
2707 * SET
2708 */
2709 return conf;
2710}
2711
2712
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002713static int hostapd_data_alloc(struct hostapd_iface *hapd_iface,
2714 struct hostapd_config *conf)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002715{
2716 size_t i;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002717 struct hostapd_data *hapd;
2718
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002719 hapd_iface->bss = os_calloc(conf->num_bss,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002720 sizeof(struct hostapd_data *));
2721 if (hapd_iface->bss == NULL)
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002722 return -1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002723
2724 for (i = 0; i < conf->num_bss; i++) {
2725 hapd = hapd_iface->bss[i] =
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002726 hostapd_alloc_bss_data(hapd_iface, conf, conf->bss[i]);
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002727 if (hapd == NULL) {
2728 while (i > 0) {
2729 i--;
2730 os_free(hapd_iface->bss[i]);
2731 hapd_iface->bss[i] = NULL;
2732 }
2733 os_free(hapd_iface->bss);
2734 hapd_iface->bss = NULL;
2735 return -1;
2736 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002737 hapd->msg_ctx = hapd;
2738 }
2739
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002740 hapd_iface->conf = conf;
2741 hapd_iface->num_bss = conf->num_bss;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002742
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002743 return 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002744}
2745
2746
2747int hostapd_add_iface(struct hapd_interfaces *interfaces, char *buf)
2748{
2749 struct hostapd_config *conf = NULL;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002750 struct hostapd_iface *hapd_iface = NULL, *new_iface = NULL;
2751 struct hostapd_data *hapd;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002752 char *ptr;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002753 size_t i, j;
2754 const char *conf_file = NULL, *phy_name = NULL;
2755
2756 if (os_strncmp(buf, "bss_config=", 11) == 0) {
2757 char *pos;
2758 phy_name = buf + 11;
2759 pos = os_strchr(phy_name, ':');
2760 if (!pos)
2761 return -1;
2762 *pos++ = '\0';
2763 conf_file = pos;
2764 if (!os_strlen(conf_file))
2765 return -1;
2766
2767 hapd_iface = hostapd_interface_init_bss(interfaces, phy_name,
2768 conf_file, 0);
2769 if (!hapd_iface)
2770 return -1;
2771 for (j = 0; j < interfaces->count; j++) {
2772 if (interfaces->iface[j] == hapd_iface)
2773 break;
2774 }
2775 if (j == interfaces->count) {
2776 struct hostapd_iface **tmp;
2777 tmp = os_realloc_array(interfaces->iface,
2778 interfaces->count + 1,
2779 sizeof(struct hostapd_iface *));
2780 if (!tmp) {
2781 hostapd_interface_deinit_free(hapd_iface);
2782 return -1;
2783 }
2784 interfaces->iface = tmp;
2785 interfaces->iface[interfaces->count++] = hapd_iface;
2786 new_iface = hapd_iface;
2787 }
2788
2789 if (new_iface) {
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002790 if (interfaces->driver_init(hapd_iface))
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002791 goto fail;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002792
2793 if (hostapd_setup_interface(hapd_iface)) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002794 hostapd_deinit_driver(
2795 hapd_iface->bss[0]->driver,
2796 hapd_iface->bss[0]->drv_priv,
2797 hapd_iface);
2798 goto fail;
2799 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002800 } else {
2801 /* Assign new BSS with bss[0]'s driver info */
2802 hapd = hapd_iface->bss[hapd_iface->num_bss - 1];
2803 hapd->driver = hapd_iface->bss[0]->driver;
2804 hapd->drv_priv = hapd_iface->bss[0]->drv_priv;
2805 os_memcpy(hapd->own_addr, hapd_iface->bss[0]->own_addr,
2806 ETH_ALEN);
2807
2808 if (start_ctrl_iface_bss(hapd) < 0 ||
Dmitry Shmidt54605472013-11-08 11:10:19 -08002809 (hapd_iface->state == HAPD_IFACE_ENABLED &&
2810 hostapd_setup_bss(hapd, -1))) {
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002811 hostapd_cleanup(hapd);
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08002812 hapd_iface->bss[hapd_iface->num_bss - 1] = NULL;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002813 hapd_iface->conf->num_bss--;
2814 hapd_iface->num_bss--;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002815 wpa_printf(MSG_DEBUG, "%s: free hapd %p %s",
2816 __func__, hapd, hapd->conf->iface);
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002817 hostapd_config_free_bss(hapd->conf);
2818 hapd->conf = NULL;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002819 os_free(hapd);
2820 return -1;
2821 }
2822 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002823 hostapd_owe_update_trans(hapd_iface);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002824 return 0;
2825 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002826
2827 ptr = os_strchr(buf, ' ');
2828 if (ptr == NULL)
2829 return -1;
2830 *ptr++ = '\0';
2831
Dmitry Shmidt56052862013-10-04 10:23:25 -07002832 if (os_strncmp(ptr, "config=", 7) == 0)
2833 conf_file = ptr + 7;
2834
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002835 for (i = 0; i < interfaces->count; i++) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002836 if (!os_strcmp(interfaces->iface[i]->conf->bss[0]->iface,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002837 buf)) {
2838 wpa_printf(MSG_INFO, "Cannot add interface - it "
2839 "already exists");
2840 return -1;
2841 }
2842 }
2843
2844 hapd_iface = hostapd_iface_alloc(interfaces);
2845 if (hapd_iface == NULL) {
2846 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
2847 "for interface", __func__);
2848 goto fail;
2849 }
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002850 new_iface = hapd_iface;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002851
Dmitry Shmidt56052862013-10-04 10:23:25 -07002852 if (conf_file && interfaces->config_read_cb) {
2853 conf = interfaces->config_read_cb(conf_file);
2854 if (conf && conf->bss)
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002855 os_strlcpy(conf->bss[0]->iface, buf,
2856 sizeof(conf->bss[0]->iface));
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002857 } else {
2858 char *driver = os_strchr(ptr, ' ');
2859
2860 if (driver)
2861 *driver++ = '\0';
2862 conf = hostapd_config_alloc(interfaces, buf, ptr, driver);
2863 }
2864
Dmitry Shmidt56052862013-10-04 10:23:25 -07002865 if (conf == NULL || conf->bss == NULL) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002866 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
2867 "for configuration", __func__);
2868 goto fail;
2869 }
2870
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002871 if (hostapd_data_alloc(hapd_iface, conf) < 0) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002872 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
2873 "for hostapd", __func__);
2874 goto fail;
2875 }
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002876 conf = NULL;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002877
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002878 if (start_ctrl_iface(hapd_iface) < 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002879 goto fail;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002880
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002881 wpa_printf(MSG_INFO, "Add interface '%s'",
2882 hapd_iface->conf->bss[0]->iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002883
2884 return 0;
2885
2886fail:
2887 if (conf)
2888 hostapd_config_free(conf);
2889 if (hapd_iface) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002890 if (hapd_iface->bss) {
Dmitry Shmidt54605472013-11-08 11:10:19 -08002891 for (i = 0; i < hapd_iface->num_bss; i++) {
2892 hapd = hapd_iface->bss[i];
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08002893 if (!hapd)
2894 continue;
2895 if (hapd_iface->interfaces &&
Dmitry Shmidt54605472013-11-08 11:10:19 -08002896 hapd_iface->interfaces->ctrl_iface_deinit)
2897 hapd_iface->interfaces->
2898 ctrl_iface_deinit(hapd);
2899 wpa_printf(MSG_DEBUG, "%s: free hapd %p (%s)",
2900 __func__, hapd_iface->bss[i],
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08002901 hapd->conf->iface);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002902 hostapd_cleanup(hapd);
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08002903 os_free(hapd);
2904 hapd_iface->bss[i] = NULL;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002905 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002906 os_free(hapd_iface->bss);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002907 hapd_iface->bss = NULL;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002908 }
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002909 if (new_iface) {
2910 interfaces->count--;
2911 interfaces->iface[interfaces->count] = NULL;
2912 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002913 hostapd_cleanup_iface(hapd_iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002914 }
2915 return -1;
2916}
2917
2918
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002919static int hostapd_remove_bss(struct hostapd_iface *iface, unsigned int idx)
2920{
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002921 size_t i;
2922
Dmitry Shmidt54605472013-11-08 11:10:19 -08002923 wpa_printf(MSG_INFO, "Remove BSS '%s'", iface->conf->bss[idx]->iface);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002924
Dmitry Shmidt54605472013-11-08 11:10:19 -08002925 /* Remove hostapd_data only if it has already been initialized */
2926 if (idx < iface->num_bss) {
2927 struct hostapd_data *hapd = iface->bss[idx];
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002928
Dmitry Shmidt54605472013-11-08 11:10:19 -08002929 hostapd_bss_deinit(hapd);
2930 wpa_printf(MSG_DEBUG, "%s: free hapd %p (%s)",
2931 __func__, hapd, hapd->conf->iface);
2932 hostapd_config_free_bss(hapd->conf);
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002933 hapd->conf = NULL;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002934 os_free(hapd);
2935
2936 iface->num_bss--;
2937
2938 for (i = idx; i < iface->num_bss; i++)
2939 iface->bss[i] = iface->bss[i + 1];
2940 } else {
2941 hostapd_config_free_bss(iface->conf->bss[idx]);
2942 iface->conf->bss[idx] = NULL;
2943 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002944
2945 iface->conf->num_bss--;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002946 for (i = idx; i < iface->conf->num_bss; i++)
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002947 iface->conf->bss[i] = iface->conf->bss[i + 1];
2948
2949 return 0;
2950}
2951
2952
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002953int hostapd_remove_iface(struct hapd_interfaces *interfaces, char *buf)
2954{
2955 struct hostapd_iface *hapd_iface;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002956 size_t i, j, k = 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002957
2958 for (i = 0; i < interfaces->count; i++) {
2959 hapd_iface = interfaces->iface[i];
2960 if (hapd_iface == NULL)
2961 return -1;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002962 if (!os_strcmp(hapd_iface->conf->bss[0]->iface, buf)) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002963 wpa_printf(MSG_INFO, "Remove interface '%s'", buf);
Dmitry Shmidta38abf92014-03-06 13:38:44 -08002964 hapd_iface->driver_ap_teardown =
2965 !!(hapd_iface->drv_flags &
2966 WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
2967
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002968 hostapd_interface_deinit_free(hapd_iface);
2969 k = i;
2970 while (k < (interfaces->count - 1)) {
2971 interfaces->iface[k] =
2972 interfaces->iface[k + 1];
2973 k++;
2974 }
2975 interfaces->count--;
2976 return 0;
2977 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002978
2979 for (j = 0; j < hapd_iface->conf->num_bss; j++) {
Dmitry Shmidta38abf92014-03-06 13:38:44 -08002980 if (!os_strcmp(hapd_iface->conf->bss[j]->iface, buf)) {
2981 hapd_iface->driver_ap_teardown =
2982 !(hapd_iface->drv_flags &
2983 WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002984 return hostapd_remove_bss(hapd_iface, j);
Dmitry Shmidta38abf92014-03-06 13:38:44 -08002985 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002986 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002987 }
2988 return -1;
2989}
2990
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002991
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002992/**
2993 * hostapd_new_assoc_sta - Notify that a new station associated with the AP
2994 * @hapd: Pointer to BSS data
2995 * @sta: Pointer to the associated STA data
2996 * @reassoc: 1 to indicate this was a re-association; 0 = first association
2997 *
2998 * This function will be called whenever a station associates with the AP. It
2999 * can be called from ieee802_11.c for drivers that export MLME to hostapd and
3000 * from drv_callbacks.c based on driver events for drivers that take care of
3001 * management frames (IEEE 802.11 authentication and association) internally.
3002 */
3003void hostapd_new_assoc_sta(struct hostapd_data *hapd, struct sta_info *sta,
3004 int reassoc)
3005{
3006 if (hapd->tkip_countermeasures) {
3007 hostapd_drv_sta_deauth(hapd, sta->addr,
3008 WLAN_REASON_MICHAEL_MIC_FAILURE);
3009 return;
3010 }
3011
3012 hostapd_prune_associations(hapd, sta->addr);
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -08003013 ap_sta_clear_disconnect_timeouts(hapd, sta);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003014
3015 /* IEEE 802.11F (IAPP) */
3016 if (hapd->conf->ieee802_11f)
3017 iapp_new_station(hapd->iapp, sta);
3018
3019#ifdef CONFIG_P2P
3020 if (sta->p2p_ie == NULL && !sta->no_p2p_set) {
3021 sta->no_p2p_set = 1;
3022 hapd->num_sta_no_p2p++;
3023 if (hapd->num_sta_no_p2p == 1)
3024 hostapd_p2p_non_p2p_sta_connected(hapd);
3025 }
3026#endif /* CONFIG_P2P */
3027
3028 /* Start accounting here, if IEEE 802.1X and WPA are not used.
3029 * IEEE 802.1X/WPA code will start accounting after the station has
3030 * been authorized. */
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -08003031 if (!hapd->conf->ieee802_1x && !hapd->conf->wpa && !hapd->conf->osen) {
3032 ap_sta_set_authorized(hapd, sta, 1);
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08003033 os_get_reltime(&sta->connected_time);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003034 accounting_sta_start(hapd, sta);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003035 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003036
3037 /* Start IEEE 802.1X authentication process for new stations */
3038 ieee802_1x_new_station(hapd, sta);
3039 if (reassoc) {
3040 if (sta->auth_alg != WLAN_AUTH_FT &&
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003041 sta->auth_alg != WLAN_AUTH_FILS_SK &&
3042 sta->auth_alg != WLAN_AUTH_FILS_SK_PFS &&
3043 sta->auth_alg != WLAN_AUTH_FILS_PK &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003044 !(sta->flags & (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS)))
3045 wpa_auth_sm_event(sta->wpa_sm, WPA_REAUTH);
3046 } else
3047 wpa_auth_sta_associated(hapd->wpa_auth, sta->wpa_sm);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003048
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08003049 if (hapd->iface->drv_flags & WPA_DRIVER_FLAGS_WIRED) {
3050 if (eloop_cancel_timeout(ap_handle_timer, hapd, sta) > 0) {
3051 wpa_printf(MSG_DEBUG,
3052 "%s: %s: canceled wired ap_handle_timer timeout for "
3053 MACSTR,
3054 hapd->conf->iface, __func__,
3055 MAC2STR(sta->addr));
3056 }
3057 } else if (!(hapd->iface->drv_flags &
3058 WPA_DRIVER_FLAGS_INACTIVITY_TIMER)) {
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -08003059 wpa_printf(MSG_DEBUG,
3060 "%s: %s: reschedule ap_handle_timer timeout for "
3061 MACSTR " (%d seconds - ap_max_inactivity)",
3062 hapd->conf->iface, __func__, MAC2STR(sta->addr),
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08003063 hapd->conf->ap_max_inactivity);
3064 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
3065 eloop_register_timeout(hapd->conf->ap_max_inactivity, 0,
3066 ap_handle_timer, hapd, sta);
3067 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003068}
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003069
3070
3071const char * hostapd_state_text(enum hostapd_iface_state s)
3072{
3073 switch (s) {
3074 case HAPD_IFACE_UNINITIALIZED:
3075 return "UNINITIALIZED";
3076 case HAPD_IFACE_DISABLED:
3077 return "DISABLED";
3078 case HAPD_IFACE_COUNTRY_UPDATE:
3079 return "COUNTRY_UPDATE";
3080 case HAPD_IFACE_ACS:
3081 return "ACS";
3082 case HAPD_IFACE_HT_SCAN:
3083 return "HT_SCAN";
3084 case HAPD_IFACE_DFS:
3085 return "DFS";
3086 case HAPD_IFACE_ENABLED:
3087 return "ENABLED";
3088 }
3089
3090 return "UNKNOWN";
3091}
3092
3093
3094void hostapd_set_state(struct hostapd_iface *iface, enum hostapd_iface_state s)
3095{
3096 wpa_printf(MSG_INFO, "%s: interface state %s->%s",
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07003097 iface->conf ? iface->conf->bss[0]->iface : "N/A",
3098 hostapd_state_text(iface->state), hostapd_state_text(s));
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003099 iface->state = s;
3100}
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003101
3102
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003103int hostapd_csa_in_progress(struct hostapd_iface *iface)
3104{
3105 unsigned int i;
3106
3107 for (i = 0; i < iface->num_bss; i++)
3108 if (iface->bss[i]->csa_in_progress)
3109 return 1;
3110 return 0;
3111}
3112
3113
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003114#ifdef NEED_AP_MLME
3115
3116static void free_beacon_data(struct beacon_data *beacon)
3117{
3118 os_free(beacon->head);
3119 beacon->head = NULL;
3120 os_free(beacon->tail);
3121 beacon->tail = NULL;
3122 os_free(beacon->probe_resp);
3123 beacon->probe_resp = NULL;
3124 os_free(beacon->beacon_ies);
3125 beacon->beacon_ies = NULL;
3126 os_free(beacon->proberesp_ies);
3127 beacon->proberesp_ies = NULL;
3128 os_free(beacon->assocresp_ies);
3129 beacon->assocresp_ies = NULL;
3130}
3131
3132
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003133static int hostapd_build_beacon_data(struct hostapd_data *hapd,
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003134 struct beacon_data *beacon)
3135{
3136 struct wpabuf *beacon_extra, *proberesp_extra, *assocresp_extra;
3137 struct wpa_driver_ap_params params;
3138 int ret;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003139
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08003140 os_memset(beacon, 0, sizeof(*beacon));
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003141 ret = ieee802_11_build_ap_params(hapd, &params);
3142 if (ret < 0)
3143 return ret;
3144
3145 ret = hostapd_build_ap_extra_ies(hapd, &beacon_extra,
3146 &proberesp_extra,
3147 &assocresp_extra);
3148 if (ret)
3149 goto free_ap_params;
3150
3151 ret = -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003152 beacon->head = os_memdup(params.head, params.head_len);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003153 if (!beacon->head)
3154 goto free_ap_extra_ies;
3155
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003156 beacon->head_len = params.head_len;
3157
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003158 beacon->tail = os_memdup(params.tail, params.tail_len);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003159 if (!beacon->tail)
3160 goto free_beacon;
3161
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003162 beacon->tail_len = params.tail_len;
3163
3164 if (params.proberesp != NULL) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003165 beacon->probe_resp = os_memdup(params.proberesp,
3166 params.proberesp_len);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003167 if (!beacon->probe_resp)
3168 goto free_beacon;
3169
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003170 beacon->probe_resp_len = params.proberesp_len;
3171 }
3172
3173 /* copy the extra ies */
3174 if (beacon_extra) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003175 beacon->beacon_ies = os_memdup(beacon_extra->buf,
3176 wpabuf_len(beacon_extra));
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003177 if (!beacon->beacon_ies)
3178 goto free_beacon;
3179
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003180 beacon->beacon_ies_len = wpabuf_len(beacon_extra);
3181 }
3182
3183 if (proberesp_extra) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003184 beacon->proberesp_ies = os_memdup(proberesp_extra->buf,
3185 wpabuf_len(proberesp_extra));
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003186 if (!beacon->proberesp_ies)
3187 goto free_beacon;
3188
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003189 beacon->proberesp_ies_len = wpabuf_len(proberesp_extra);
3190 }
3191
3192 if (assocresp_extra) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003193 beacon->assocresp_ies = os_memdup(assocresp_extra->buf,
3194 wpabuf_len(assocresp_extra));
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003195 if (!beacon->assocresp_ies)
3196 goto free_beacon;
3197
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003198 beacon->assocresp_ies_len = wpabuf_len(assocresp_extra);
3199 }
3200
3201 ret = 0;
3202free_beacon:
3203 /* if the function fails, the caller should not free beacon data */
3204 if (ret)
3205 free_beacon_data(beacon);
3206
3207free_ap_extra_ies:
3208 hostapd_free_ap_extra_ies(hapd, beacon_extra, proberesp_extra,
3209 assocresp_extra);
3210free_ap_params:
3211 ieee802_11_free_ap_params(&params);
3212 return ret;
3213}
3214
3215
3216/*
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003217 * TODO: This flow currently supports only changing channel and width within
3218 * the same hw_mode. Any other changes to MAC parameters or provided settings
3219 * are not supported.
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003220 */
3221static int hostapd_change_config_freq(struct hostapd_data *hapd,
3222 struct hostapd_config *conf,
3223 struct hostapd_freq_params *params,
3224 struct hostapd_freq_params *old_params)
3225{
3226 int channel;
3227
3228 if (!params->channel) {
3229 /* check if the new channel is supported by hw */
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003230 params->channel = hostapd_hw_get_channel(hapd, params->freq);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003231 }
3232
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003233 channel = params->channel;
3234 if (!channel)
3235 return -1;
3236
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003237 /* if a pointer to old_params is provided we save previous state */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003238 if (old_params &&
3239 hostapd_set_freq_params(old_params, conf->hw_mode,
3240 hostapd_hw_get_freq(hapd, conf->channel),
3241 conf->channel, conf->ieee80211n,
3242 conf->ieee80211ac,
3243 conf->secondary_channel,
3244 conf->vht_oper_chwidth,
3245 conf->vht_oper_centr_freq_seg0_idx,
3246 conf->vht_oper_centr_freq_seg1_idx,
3247 conf->vht_capab))
3248 return -1;
3249
3250 switch (params->bandwidth) {
3251 case 0:
3252 case 20:
3253 case 40:
3254 conf->vht_oper_chwidth = VHT_CHANWIDTH_USE_HT;
3255 break;
3256 case 80:
3257 if (params->center_freq2)
3258 conf->vht_oper_chwidth = VHT_CHANWIDTH_80P80MHZ;
3259 else
3260 conf->vht_oper_chwidth = VHT_CHANWIDTH_80MHZ;
3261 break;
3262 case 160:
3263 conf->vht_oper_chwidth = VHT_CHANWIDTH_160MHZ;
3264 break;
3265 default:
3266 return -1;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003267 }
3268
3269 conf->channel = channel;
3270 conf->ieee80211n = params->ht_enabled;
3271 conf->secondary_channel = params->sec_channel_offset;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003272 ieee80211_freq_to_chan(params->center_freq1,
3273 &conf->vht_oper_centr_freq_seg0_idx);
3274 ieee80211_freq_to_chan(params->center_freq2,
3275 &conf->vht_oper_centr_freq_seg1_idx);
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;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003289 u8 chan, vht_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)
3298 vht_bandwidth = VHT_CHANWIDTH_80P80MHZ;
3299 else
3300 vht_bandwidth = VHT_CHANWIDTH_80MHZ;
3301 break;
3302 case 160:
3303 vht_bandwidth = VHT_CHANWIDTH_160MHZ;
3304 break;
3305 default:
3306 vht_bandwidth = VHT_CHANWIDTH_USE_HT;
3307 break;
3308 }
3309
3310 if (ieee80211_freq_to_channel_ext(
3311 settings->freq_params.freq,
3312 settings->freq_params.sec_channel_offset,
3313 vht_bandwidth,
3314 &hapd->iface->cs_oper_class,
3315 &chan) == NUM_HOSTAPD_MODES) {
3316 wpa_printf(MSG_DEBUG,
3317 "invalid frequency for channel switch (freq=%d, sec_channel_offset=%d, vht_enabled=%d)",
3318 settings->freq_params.freq,
3319 settings->freq_params.sec_channel_offset,
3320 settings->freq_params.vht_enabled);
3321 return -1;
3322 }
3323
3324 settings->freq_params.channel = chan;
3325
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003326 ret = hostapd_change_config_freq(iface->bss[0], iface->conf,
3327 &settings->freq_params,
3328 &old_freq);
3329 if (ret)
3330 return ret;
3331
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003332 ret = hostapd_build_beacon_data(hapd, &settings->beacon_after);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003333
3334 /* change back the configuration */
3335 hostapd_change_config_freq(iface->bss[0], iface->conf,
3336 &old_freq, NULL);
3337
3338 if (ret)
3339 return ret;
3340
3341 /* set channel switch parameters for csa ie */
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003342 hapd->cs_freq_params = settings->freq_params;
3343 hapd->cs_count = settings->cs_count;
3344 hapd->cs_block_tx = settings->block_tx;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003345
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003346 ret = hostapd_build_beacon_data(hapd, &settings->beacon_csa);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003347 if (ret) {
3348 free_beacon_data(&settings->beacon_after);
3349 return ret;
3350 }
3351
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003352 settings->counter_offset_beacon[0] = hapd->cs_c_off_beacon;
3353 settings->counter_offset_presp[0] = hapd->cs_c_off_proberesp;
3354 settings->counter_offset_beacon[1] = hapd->cs_c_off_ecsa_beacon;
3355 settings->counter_offset_presp[1] = hapd->cs_c_off_ecsa_proberesp;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003356
3357 return 0;
3358}
3359
3360
3361void hostapd_cleanup_cs_params(struct hostapd_data *hapd)
3362{
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003363 os_memset(&hapd->cs_freq_params, 0, sizeof(hapd->cs_freq_params));
3364 hapd->cs_count = 0;
3365 hapd->cs_block_tx = 0;
3366 hapd->cs_c_off_beacon = 0;
3367 hapd->cs_c_off_proberesp = 0;
3368 hapd->csa_in_progress = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003369 hapd->cs_c_off_ecsa_beacon = 0;
3370 hapd->cs_c_off_ecsa_proberesp = 0;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003371}
3372
3373
Roshan Pius3a1667e2018-07-03 15:17:14 -07003374void hostapd_chan_switch_vht_config(struct hostapd_data *hapd, int vht_enabled)
3375{
3376 if (vht_enabled)
3377 hapd->iconf->ch_switch_vht_config |= CH_SWITCH_VHT_ENABLED;
3378 else
3379 hapd->iconf->ch_switch_vht_config |= CH_SWITCH_VHT_DISABLED;
3380
3381 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
3382 HOSTAPD_LEVEL_INFO, "CHAN_SWITCH VHT CONFIG 0x%x",
3383 hapd->iconf->ch_switch_vht_config);
3384}
3385
3386
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003387int hostapd_switch_channel(struct hostapd_data *hapd,
3388 struct csa_settings *settings)
3389{
3390 int ret;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003391
3392 if (!(hapd->iface->drv_flags & WPA_DRIVER_FLAGS_AP_CSA)) {
3393 wpa_printf(MSG_INFO, "CSA is not supported");
3394 return -1;
3395 }
3396
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003397 ret = hostapd_fill_csa_settings(hapd, settings);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003398 if (ret)
3399 return ret;
3400
3401 ret = hostapd_drv_switch_channel(hapd, settings);
3402 free_beacon_data(&settings->beacon_csa);
3403 free_beacon_data(&settings->beacon_after);
3404
3405 if (ret) {
3406 /* if we failed, clean cs parameters */
3407 hostapd_cleanup_cs_params(hapd);
3408 return ret;
3409 }
3410
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003411 hapd->csa_in_progress = 1;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003412 return 0;
3413}
3414
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003415
3416void
3417hostapd_switch_channel_fallback(struct hostapd_iface *iface,
3418 const struct hostapd_freq_params *freq_params)
3419{
3420 int vht_seg0_idx = 0, vht_seg1_idx = 0, vht_bw = VHT_CHANWIDTH_USE_HT;
3421 unsigned int i;
3422
3423 wpa_printf(MSG_DEBUG, "Restarting all CSA-related BSSes");
3424
3425 if (freq_params->center_freq1)
3426 vht_seg0_idx = 36 + (freq_params->center_freq1 - 5180) / 5;
3427 if (freq_params->center_freq2)
3428 vht_seg1_idx = 36 + (freq_params->center_freq2 - 5180) / 5;
3429
3430 switch (freq_params->bandwidth) {
3431 case 0:
3432 case 20:
3433 case 40:
3434 vht_bw = VHT_CHANWIDTH_USE_HT;
3435 break;
3436 case 80:
3437 if (freq_params->center_freq2)
3438 vht_bw = VHT_CHANWIDTH_80P80MHZ;
3439 else
3440 vht_bw = VHT_CHANWIDTH_80MHZ;
3441 break;
3442 case 160:
3443 vht_bw = VHT_CHANWIDTH_160MHZ;
3444 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;
3454 iface->conf->vht_oper_centr_freq_seg0_idx = vht_seg0_idx;
3455 iface->conf->vht_oper_centr_freq_seg1_idx = vht_seg1_idx;
3456 iface->conf->vht_oper_chwidth = vht_bw;
3457 iface->conf->ieee80211n = freq_params->ht_enabled;
3458 iface->conf->ieee80211ac = freq_params->vht_enabled;
3459
3460 /*
3461 * cs_params must not be cleared earlier because the freq_params
3462 * argument may actually point to one of these.
3463 */
3464 for (i = 0; i < iface->num_bss; i++)
3465 hostapd_cleanup_cs_params(iface->bss[i]);
3466
3467 hostapd_disable_iface(iface);
3468 hostapd_enable_iface(iface);
3469}
3470
Dmitry Shmidte4663042016-04-04 10:07:49 -07003471#endif /* NEED_AP_MLME */
3472
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003473
3474struct hostapd_data * hostapd_get_iface(struct hapd_interfaces *interfaces,
3475 const char *ifname)
3476{
3477 size_t i, j;
3478
3479 for (i = 0; i < interfaces->count; i++) {
3480 struct hostapd_iface *iface = interfaces->iface[i];
3481
3482 for (j = 0; j < iface->num_bss; j++) {
3483 struct hostapd_data *hapd = iface->bss[j];
3484
3485 if (os_strcmp(ifname, hapd->conf->iface) == 0)
3486 return hapd;
3487 }
3488 }
3489
3490 return NULL;
3491}
3492
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003493
3494void hostapd_periodic_iface(struct hostapd_iface *iface)
3495{
3496 size_t i;
3497
3498 ap_list_timer(iface);
3499
3500 for (i = 0; i < iface->num_bss; i++) {
3501 struct hostapd_data *hapd = iface->bss[i];
3502
3503 if (!hapd->started)
3504 continue;
3505
3506#ifndef CONFIG_NO_RADIUS
3507 hostapd_acl_expire(hapd);
3508#endif /* CONFIG_NO_RADIUS */
3509 }
3510}