blob: 117ee08363a876be8f891e18e8ea1871defbd1a4 [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
Hai Shalomce48b4a2018-09-05 11:41:35 -070081void hostapd_reconfig_encryption(struct hostapd_data *hapd)
82{
83 if (hapd->wpa_auth)
84 return;
85
86 hostapd_set_privacy(hapd, 0);
87 hostapd_setup_encryption(hapd->conf->iface, hapd);
88}
89
90
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070091static void hostapd_reload_bss(struct hostapd_data *hapd)
92{
Dmitry Shmidtcce06662013-11-04 18:44:24 -080093 struct hostapd_ssid *ssid;
94
Dmitry Shmidt29333592017-01-09 12:27:11 -080095 if (!hapd->started)
96 return;
97
Roshan Pius3a1667e2018-07-03 15:17:14 -070098 if (hapd->conf->wmm_enabled < 0)
99 hapd->conf->wmm_enabled = hapd->iconf->ieee80211n;
100
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700101#ifndef CONFIG_NO_RADIUS
102 radius_client_reconfig(hapd->radius, hapd->conf->radius);
103#endif /* CONFIG_NO_RADIUS */
104
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800105 ssid = &hapd->conf->ssid;
106 if (!ssid->wpa_psk_set && ssid->wpa_psk && !ssid->wpa_psk->next &&
107 ssid->wpa_passphrase_set && ssid->wpa_passphrase) {
108 /*
109 * Force PSK to be derived again since SSID or passphrase may
110 * have changed.
111 */
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800112 hostapd_config_clear_wpa_psk(&hapd->conf->ssid.wpa_psk);
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800113 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700114 if (hostapd_setup_wpa_psk(hapd->conf)) {
115 wpa_printf(MSG_ERROR, "Failed to re-configure WPA PSK "
116 "after reloading configuration");
117 }
118
119 if (hapd->conf->ieee802_1x || hapd->conf->wpa)
120 hostapd_set_drv_ieee8021x(hapd, hapd->conf->iface, 1);
121 else
122 hostapd_set_drv_ieee8021x(hapd, hapd->conf->iface, 0);
123
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800124 if ((hapd->conf->wpa || hapd->conf->osen) && hapd->wpa_auth == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700125 hostapd_setup_wpa(hapd);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800126 if (hapd->wpa_auth)
127 wpa_init_keys(hapd->wpa_auth);
128 } else if (hapd->conf->wpa) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700129 const u8 *wpa_ie;
130 size_t wpa_ie_len;
131 hostapd_reconfig_wpa(hapd);
132 wpa_ie = wpa_auth_get_wpa_ie(hapd->wpa_auth, &wpa_ie_len);
133 if (hostapd_set_generic_elem(hapd, wpa_ie, wpa_ie_len))
134 wpa_printf(MSG_ERROR, "Failed to configure WPA IE for "
135 "the kernel driver.");
136 } else if (hapd->wpa_auth) {
137 wpa_deinit(hapd->wpa_auth);
138 hapd->wpa_auth = NULL;
139 hostapd_set_privacy(hapd, 0);
140 hostapd_setup_encryption(hapd->conf->iface, hapd);
141 hostapd_set_generic_elem(hapd, (u8 *) "", 0);
142 }
143
144 ieee802_11_set_beacon(hapd);
145 hostapd_update_wps(hapd);
146
147 if (hapd->conf->ssid.ssid_set &&
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700148 hostapd_set_ssid(hapd, hapd->conf->ssid.ssid,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700149 hapd->conf->ssid.ssid_len)) {
150 wpa_printf(MSG_ERROR, "Could not set SSID for kernel driver");
151 /* try to continue */
152 }
153 wpa_printf(MSG_DEBUG, "Reconfigured interface %s", hapd->conf->iface);
154}
155
156
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700157static void hostapd_clear_old(struct hostapd_iface *iface)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700158{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700159 size_t j;
160
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700161 /*
162 * Deauthenticate all stations since the new configuration may not
163 * allow them to use the BSS anymore.
164 */
165 for (j = 0; j < iface->num_bss; j++) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700166 hostapd_flush_old_stations(iface->bss[j],
167 WLAN_REASON_PREV_AUTH_NOT_VALID);
Dmitry Shmidtc55524a2011-07-07 11:18:38 -0700168 hostapd_broadcast_wep_clear(iface->bss[j]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700169
170#ifndef CONFIG_NO_RADIUS
171 /* TODO: update dynamic data based on changed configuration
172 * items (e.g., open/close sockets, etc.) */
173 radius_client_flush(iface->bss[j]->radius, 0);
174#endif /* CONFIG_NO_RADIUS */
175 }
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700176}
177
178
179int hostapd_reload_config(struct hostapd_iface *iface)
180{
181 struct hostapd_data *hapd = iface->bss[0];
182 struct hostapd_config *newconf, *oldconf;
183 size_t j;
184
185 if (iface->config_fname == NULL) {
186 /* Only in-memory config in use - assume it has been updated */
187 hostapd_clear_old(iface);
188 for (j = 0; j < iface->num_bss; j++)
189 hostapd_reload_bss(iface->bss[j]);
190 return 0;
191 }
192
193 if (iface->interfaces == NULL ||
194 iface->interfaces->config_read_cb == NULL)
195 return -1;
196 newconf = iface->interfaces->config_read_cb(iface->config_fname);
197 if (newconf == NULL)
198 return -1;
199
200 hostapd_clear_old(iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700201
202 oldconf = hapd->iconf;
203 iface->conf = newconf;
204
205 for (j = 0; j < iface->num_bss; j++) {
206 hapd = iface->bss[j];
207 hapd->iconf = newconf;
Dmitry Shmidtd11f0192014-03-24 12:09:47 -0700208 hapd->iconf->channel = oldconf->channel;
Dmitry Shmidtdda10c22015-03-24 16:05:01 -0700209 hapd->iconf->acs = oldconf->acs;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -0700210 hapd->iconf->secondary_channel = oldconf->secondary_channel;
Dmitry Shmidtd11f0192014-03-24 12:09:47 -0700211 hapd->iconf->ieee80211n = oldconf->ieee80211n;
212 hapd->iconf->ieee80211ac = oldconf->ieee80211ac;
213 hapd->iconf->ht_capab = oldconf->ht_capab;
214 hapd->iconf->vht_capab = oldconf->vht_capab;
215 hapd->iconf->vht_oper_chwidth = oldconf->vht_oper_chwidth;
216 hapd->iconf->vht_oper_centr_freq_seg0_idx =
217 oldconf->vht_oper_centr_freq_seg0_idx;
218 hapd->iconf->vht_oper_centr_freq_seg1_idx =
219 oldconf->vht_oper_centr_freq_seg1_idx;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800220 hapd->conf = newconf->bss[j];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700221 hostapd_reload_bss(hapd);
222 }
223
224 hostapd_config_free(oldconf);
225
226
227 return 0;
228}
229
230
231static void hostapd_broadcast_key_clear_iface(struct hostapd_data *hapd,
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700232 const char *ifname)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700233{
234 int i;
235
Dmitry Shmidt29333592017-01-09 12:27:11 -0800236 if (!ifname || !hapd->drv_priv)
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700237 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700238 for (i = 0; i < NUM_WEP_KEYS; i++) {
239 if (hostapd_drv_set_key(ifname, hapd, WPA_ALG_NONE, NULL, i,
240 0, NULL, 0, NULL, 0)) {
241 wpa_printf(MSG_DEBUG, "Failed to clear default "
242 "encryption keys (ifname=%s keyidx=%d)",
243 ifname, i);
244 }
245 }
246#ifdef CONFIG_IEEE80211W
247 if (hapd->conf->ieee80211w) {
248 for (i = NUM_WEP_KEYS; i < NUM_WEP_KEYS + 2; i++) {
249 if (hostapd_drv_set_key(ifname, hapd, WPA_ALG_NONE,
250 NULL, i, 0, NULL,
251 0, NULL, 0)) {
252 wpa_printf(MSG_DEBUG, "Failed to clear "
253 "default mgmt encryption keys "
254 "(ifname=%s keyidx=%d)", ifname, i);
255 }
256 }
257 }
258#endif /* CONFIG_IEEE80211W */
259}
260
261
262static int hostapd_broadcast_wep_clear(struct hostapd_data *hapd)
263{
264 hostapd_broadcast_key_clear_iface(hapd, hapd->conf->iface);
265 return 0;
266}
267
268
269static int hostapd_broadcast_wep_set(struct hostapd_data *hapd)
270{
271 int errors = 0, idx;
272 struct hostapd_ssid *ssid = &hapd->conf->ssid;
273
274 idx = ssid->wep.idx;
275 if (ssid->wep.default_len &&
276 hostapd_drv_set_key(hapd->conf->iface,
277 hapd, WPA_ALG_WEP, broadcast_ether_addr, idx,
278 1, NULL, 0, ssid->wep.key[idx],
279 ssid->wep.len[idx])) {
280 wpa_printf(MSG_WARNING, "Could not set WEP encryption.");
281 errors++;
282 }
283
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700284 return errors;
285}
286
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700287
Dmitry Shmidt04949592012-07-19 12:16:46 -0700288static void hostapd_free_hapd_data(struct hostapd_data *hapd)
289{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800290 os_free(hapd->probereq_cb);
291 hapd->probereq_cb = NULL;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800292 hapd->num_probereq_cb = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800293
294#ifdef CONFIG_P2P
295 wpabuf_free(hapd->p2p_beacon_ie);
296 hapd->p2p_beacon_ie = NULL;
297 wpabuf_free(hapd->p2p_probe_resp_ie);
298 hapd->p2p_probe_resp_ie = NULL;
299#endif /* CONFIG_P2P */
300
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -0800301 if (!hapd->started) {
302 wpa_printf(MSG_ERROR, "%s: Interface %s wasn't started",
303 __func__, hapd->conf->iface);
304 return;
305 }
306 hapd->started = 0;
307
Dmitry Shmidt54605472013-11-08 11:10:19 -0800308 wpa_printf(MSG_DEBUG, "%s(%s)", __func__, hapd->conf->iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700309 iapp_deinit(hapd->iapp);
310 hapd->iapp = NULL;
311 accounting_deinit(hapd);
312 hostapd_deinit_wpa(hapd);
313 vlan_deinit(hapd);
314 hostapd_acl_deinit(hapd);
315#ifndef CONFIG_NO_RADIUS
316 radius_client_deinit(hapd->radius);
317 hapd->radius = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700318 radius_das_deinit(hapd->radius_das);
319 hapd->radius_das = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700320#endif /* CONFIG_NO_RADIUS */
321
322 hostapd_deinit_wps(hapd);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700323#ifdef CONFIG_DPP
324 hostapd_dpp_deinit(hapd);
325 gas_query_ap_deinit(hapd->gas);
326#endif /* CONFIG_DPP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700327
328 authsrv_deinit(hapd);
329
Dmitry Shmidt71757432014-06-02 13:50:35 -0700330 if (hapd->interface_added) {
331 hapd->interface_added = 0;
332 if (hostapd_if_remove(hapd, WPA_IF_AP_BSS, hapd->conf->iface)) {
333 wpa_printf(MSG_WARNING,
334 "Failed to remove BSS interface %s",
335 hapd->conf->iface);
336 hapd->interface_added = 1;
337 } else {
338 /*
339 * Since this was a dynamically added interface, the
340 * driver wrapper may have removed its internal instance
341 * and hapd->drv_priv is not valid anymore.
342 */
343 hapd->drv_priv = NULL;
344 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700345 }
346
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800347 wpabuf_free(hapd->time_adv);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700348
349#ifdef CONFIG_INTERWORKING
350 gas_serv_deinit(hapd);
351#endif /* CONFIG_INTERWORKING */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800352
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800353 bss_load_update_deinit(hapd);
354 ndisc_snoop_deinit(hapd);
355 dhcp_snoop_deinit(hapd);
356 x_snoop_deinit(hapd);
357
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800358#ifdef CONFIG_SQLITE
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700359 bin_clear_free(hapd->tmp_eap_user.identity,
360 hapd->tmp_eap_user.identity_len);
361 bin_clear_free(hapd->tmp_eap_user.password,
362 hapd->tmp_eap_user.password_len);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800363#endif /* CONFIG_SQLITE */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800364
365#ifdef CONFIG_MESH
366 wpabuf_free(hapd->mesh_pending_auth);
367 hapd->mesh_pending_auth = NULL;
368#endif /* CONFIG_MESH */
Dmitry Shmidt849734c2016-05-27 09:59:01 -0700369
370 hostapd_clean_rrm(hapd);
Dmitry Shmidtebd93af2017-02-21 13:40:44 -0800371 fils_hlp_deinit(hapd);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700372}
373
374
375/**
376 * hostapd_cleanup - Per-BSS cleanup (deinitialization)
377 * @hapd: Pointer to BSS data
378 *
379 * This function is used to free all per-BSS data structures and resources.
Dmitry Shmidt54605472013-11-08 11:10:19 -0800380 * Most of the modules that are initialized in hostapd_setup_bss() are
381 * deinitialized here.
Dmitry Shmidt04949592012-07-19 12:16:46 -0700382 */
383static void hostapd_cleanup(struct hostapd_data *hapd)
384{
Dmitry Shmidt54605472013-11-08 11:10:19 -0800385 wpa_printf(MSG_DEBUG, "%s(hapd=%p (%s))", __func__, hapd,
386 hapd->conf->iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700387 if (hapd->iface->interfaces &&
Dmitry Shmidt29333592017-01-09 12:27:11 -0800388 hapd->iface->interfaces->ctrl_iface_deinit) {
389 wpa_msg(hapd->msg_ctx, MSG_INFO, WPA_EVENT_TERMINATING);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700390 hapd->iface->interfaces->ctrl_iface_deinit(hapd);
Dmitry Shmidt29333592017-01-09 12:27:11 -0800391 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700392 hostapd_free_hapd_data(hapd);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700393}
394
395
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800396static void sta_track_deinit(struct hostapd_iface *iface)
397{
398 struct hostapd_sta_info *info;
399
400 if (!iface->num_sta_seen)
401 return;
402
403 while ((info = dl_list_first(&iface->sta_seen, struct hostapd_sta_info,
404 list))) {
405 dl_list_del(&info->list);
406 iface->num_sta_seen--;
Dmitry Shmidtaca489e2016-09-28 15:44:14 -0700407 sta_track_del(info);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800408 }
409}
410
411
Dmitry Shmidt04949592012-07-19 12:16:46 -0700412static void hostapd_cleanup_iface_partial(struct hostapd_iface *iface)
413{
Dmitry Shmidt54605472013-11-08 11:10:19 -0800414 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800415#ifdef CONFIG_IEEE80211N
416#ifdef NEED_AP_MLME
417 hostapd_stop_setup_timers(iface);
418#endif /* NEED_AP_MLME */
419#endif /* CONFIG_IEEE80211N */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700420 if (iface->current_mode)
421 acs_cleanup(iface);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700422 hostapd_free_hw_features(iface->hw_features, iface->num_hw_features);
423 iface->hw_features = NULL;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700424 iface->current_mode = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700425 os_free(iface->current_rates);
426 iface->current_rates = NULL;
427 os_free(iface->basic_rates);
428 iface->basic_rates = NULL;
429 ap_list_deinit(iface);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800430 sta_track_deinit(iface);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700431}
432
433
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700434/**
435 * hostapd_cleanup_iface - Complete per-interface cleanup
436 * @iface: Pointer to interface data
437 *
438 * This function is called after per-BSS data structures are deinitialized
439 * with hostapd_cleanup().
440 */
441static void hostapd_cleanup_iface(struct hostapd_iface *iface)
442{
Dmitry Shmidt54605472013-11-08 11:10:19 -0800443 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800444 eloop_cancel_timeout(channel_list_update_timeout, iface, NULL);
Roshan Pius3a1667e2018-07-03 15:17:14 -0700445 eloop_cancel_timeout(hostapd_interface_setup_failure_handler, iface,
446 NULL);
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800447
Dmitry Shmidt04949592012-07-19 12:16:46 -0700448 hostapd_cleanup_iface_partial(iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700449 hostapd_config_free(iface->conf);
450 iface->conf = NULL;
451
452 os_free(iface->config_fname);
453 os_free(iface->bss);
Dmitry Shmidt54605472013-11-08 11:10:19 -0800454 wpa_printf(MSG_DEBUG, "%s: free iface=%p", __func__, iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700455 os_free(iface);
456}
457
458
Dmitry Shmidt04949592012-07-19 12:16:46 -0700459static void hostapd_clear_wep(struct hostapd_data *hapd)
460{
Dmitry Shmidta38abf92014-03-06 13:38:44 -0800461 if (hapd->drv_priv && !hapd->iface->driver_ap_teardown) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700462 hostapd_set_privacy(hapd, 0);
463 hostapd_broadcast_wep_clear(hapd);
464 }
465}
466
467
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700468static int hostapd_setup_encryption(char *iface, struct hostapd_data *hapd)
469{
470 int i;
471
472 hostapd_broadcast_wep_set(hapd);
473
474 if (hapd->conf->ssid.wep.default_len) {
475 hostapd_set_privacy(hapd, 1);
476 return 0;
477 }
478
Jouni Malinen75ecf522011-06-27 15:19:46 -0700479 /*
480 * When IEEE 802.1X is not enabled, the driver may need to know how to
481 * set authentication algorithms for static WEP.
482 */
483 hostapd_drv_set_authmode(hapd, hapd->conf->auth_algs);
484
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700485 for (i = 0; i < 4; i++) {
486 if (hapd->conf->ssid.wep.key[i] &&
487 hostapd_drv_set_key(iface, hapd, WPA_ALG_WEP, NULL, i,
488 i == hapd->conf->ssid.wep.idx, NULL, 0,
489 hapd->conf->ssid.wep.key[i],
490 hapd->conf->ssid.wep.len[i])) {
491 wpa_printf(MSG_WARNING, "Could not set WEP "
492 "encryption.");
493 return -1;
494 }
495 if (hapd->conf->ssid.wep.key[i] &&
496 i == hapd->conf->ssid.wep.idx)
497 hostapd_set_privacy(hapd, 1);
498 }
499
500 return 0;
501}
502
503
Dmitry Shmidt04949592012-07-19 12:16:46 -0700504static int hostapd_flush_old_stations(struct hostapd_data *hapd, u16 reason)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700505{
506 int ret = 0;
507 u8 addr[ETH_ALEN];
508
509 if (hostapd_drv_none(hapd) || hapd->drv_priv == NULL)
510 return 0;
511
Dmitry Shmidta38abf92014-03-06 13:38:44 -0800512 if (!hapd->iface->driver_ap_teardown) {
513 wpa_dbg(hapd->msg_ctx, MSG_DEBUG,
514 "Flushing old station entries");
515
516 if (hostapd_flush(hapd)) {
517 wpa_msg(hapd->msg_ctx, MSG_WARNING,
518 "Could not connect to kernel driver");
519 ret = -1;
520 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700521 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700522 if (hapd->conf && hapd->conf->broadcast_deauth) {
523 wpa_dbg(hapd->msg_ctx, MSG_DEBUG,
524 "Deauthenticate all stations");
525 os_memset(addr, 0xff, ETH_ALEN);
526 hostapd_drv_sta_deauth(hapd, addr, reason);
527 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700528 hostapd_free_stas(hapd);
529
530 return ret;
531}
532
533
Dmitry Shmidt71757432014-06-02 13:50:35 -0700534static void hostapd_bss_deinit_no_free(struct hostapd_data *hapd)
535{
536 hostapd_free_stas(hapd);
537 hostapd_flush_old_stations(hapd, WLAN_REASON_DEAUTH_LEAVING);
538 hostapd_clear_wep(hapd);
539}
540
541
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700542/**
543 * hostapd_validate_bssid_configuration - Validate BSSID configuration
544 * @iface: Pointer to interface data
545 * Returns: 0 on success, -1 on failure
546 *
547 * This function is used to validate that the configured BSSIDs are valid.
548 */
549static int hostapd_validate_bssid_configuration(struct hostapd_iface *iface)
550{
551 u8 mask[ETH_ALEN] = { 0 };
552 struct hostapd_data *hapd = iface->bss[0];
553 unsigned int i = iface->conf->num_bss, bits = 0, j;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700554 int auto_addr = 0;
555
556 if (hostapd_drv_none(hapd))
557 return 0;
558
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -0800559 if (iface->conf->use_driver_iface_addr)
560 return 0;
561
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700562 /* Generate BSSID mask that is large enough to cover the BSSIDs. */
563
564 /* Determine the bits necessary to cover the number of BSSIDs. */
565 for (i--; i; i >>= 1)
566 bits++;
567
568 /* Determine the bits necessary to any configured BSSIDs,
569 if they are higher than the number of BSSIDs. */
570 for (j = 0; j < iface->conf->num_bss; j++) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800571 if (is_zero_ether_addr(iface->conf->bss[j]->bssid)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700572 if (j)
573 auto_addr++;
574 continue;
575 }
576
577 for (i = 0; i < ETH_ALEN; i++) {
578 mask[i] |=
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800579 iface->conf->bss[j]->bssid[i] ^
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700580 hapd->own_addr[i];
581 }
582 }
583
584 if (!auto_addr)
585 goto skip_mask_ext;
586
587 for (i = 0; i < ETH_ALEN && mask[i] == 0; i++)
588 ;
589 j = 0;
590 if (i < ETH_ALEN) {
591 j = (5 - i) * 8;
592
593 while (mask[i] != 0) {
594 mask[i] >>= 1;
595 j++;
596 }
597 }
598
599 if (bits < j)
600 bits = j;
601
602 if (bits > 40) {
603 wpa_printf(MSG_ERROR, "Too many bits in the BSSID mask (%u)",
604 bits);
605 return -1;
606 }
607
608 os_memset(mask, 0xff, ETH_ALEN);
609 j = bits / 8;
610 for (i = 5; i > 5 - j; i--)
611 mask[i] = 0;
612 j = bits % 8;
613 while (j--)
614 mask[i] <<= 1;
615
616skip_mask_ext:
617 wpa_printf(MSG_DEBUG, "BSS count %lu, BSSID mask " MACSTR " (%d bits)",
618 (unsigned long) iface->conf->num_bss, MAC2STR(mask), bits);
619
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700620 if (!auto_addr)
621 return 0;
622
623 for (i = 0; i < ETH_ALEN; i++) {
624 if ((hapd->own_addr[i] & mask[i]) != hapd->own_addr[i]) {
625 wpa_printf(MSG_ERROR, "Invalid BSSID mask " MACSTR
626 " for start address " MACSTR ".",
627 MAC2STR(mask), MAC2STR(hapd->own_addr));
628 wpa_printf(MSG_ERROR, "Start address must be the "
629 "first address in the block (i.e., addr "
630 "AND mask == addr).");
631 return -1;
632 }
633 }
634
635 return 0;
636}
637
638
639static int mac_in_conf(struct hostapd_config *conf, const void *a)
640{
641 size_t i;
642
643 for (i = 0; i < conf->num_bss; i++) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800644 if (hostapd_mac_comp(conf->bss[i]->bssid, a) == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700645 return 1;
646 }
647 }
648
649 return 0;
650}
651
652
Dmitry Shmidt04949592012-07-19 12:16:46 -0700653#ifndef CONFIG_NO_RADIUS
654
655static int hostapd_das_nas_mismatch(struct hostapd_data *hapd,
656 struct radius_das_attrs *attr)
657{
Dmitry Shmidt13ca8d82014-02-20 10:18:40 -0800658 if (attr->nas_identifier &&
659 (!hapd->conf->nas_identifier ||
660 os_strlen(hapd->conf->nas_identifier) !=
661 attr->nas_identifier_len ||
662 os_memcmp(hapd->conf->nas_identifier, attr->nas_identifier,
663 attr->nas_identifier_len) != 0)) {
664 wpa_printf(MSG_DEBUG, "RADIUS DAS: NAS-Identifier mismatch");
665 return 1;
666 }
667
668 if (attr->nas_ip_addr &&
669 (hapd->conf->own_ip_addr.af != AF_INET ||
670 os_memcmp(&hapd->conf->own_ip_addr.u.v4, attr->nas_ip_addr, 4) !=
671 0)) {
672 wpa_printf(MSG_DEBUG, "RADIUS DAS: NAS-IP-Address mismatch");
673 return 1;
674 }
675
676#ifdef CONFIG_IPV6
677 if (attr->nas_ipv6_addr &&
678 (hapd->conf->own_ip_addr.af != AF_INET6 ||
679 os_memcmp(&hapd->conf->own_ip_addr.u.v6, attr->nas_ipv6_addr, 16)
680 != 0)) {
681 wpa_printf(MSG_DEBUG, "RADIUS DAS: NAS-IPv6-Address mismatch");
682 return 1;
683 }
684#endif /* CONFIG_IPV6 */
685
Dmitry Shmidt04949592012-07-19 12:16:46 -0700686 return 0;
687}
688
689
690static struct sta_info * hostapd_das_find_sta(struct hostapd_data *hapd,
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800691 struct radius_das_attrs *attr,
692 int *multi)
Dmitry Shmidt04949592012-07-19 12:16:46 -0700693{
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800694 struct sta_info *selected, *sta;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700695 char buf[128];
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800696 int num_attr = 0;
697 int count;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700698
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800699 *multi = 0;
700
701 for (sta = hapd->sta_list; sta; sta = sta->next)
702 sta->radius_das_match = 1;
703
704 if (attr->sta_addr) {
705 num_attr++;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700706 sta = ap_get_sta(hapd, attr->sta_addr);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800707 if (!sta) {
708 wpa_printf(MSG_DEBUG,
709 "RADIUS DAS: No Calling-Station-Id match");
710 return NULL;
711 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700712
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800713 selected = sta;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700714 for (sta = hapd->sta_list; sta; sta = sta->next) {
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800715 if (sta != selected)
716 sta->radius_das_match = 0;
717 }
718 wpa_printf(MSG_DEBUG, "RADIUS DAS: Calling-Station-Id match");
719 }
720
721 if (attr->acct_session_id) {
722 num_attr++;
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800723 if (attr->acct_session_id_len != 16) {
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800724 wpa_printf(MSG_DEBUG,
725 "RADIUS DAS: Acct-Session-Id cannot match");
726 return NULL;
727 }
728 count = 0;
729
730 for (sta = hapd->sta_list; sta; sta = sta->next) {
731 if (!sta->radius_das_match)
732 continue;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800733 os_snprintf(buf, sizeof(buf), "%016llX",
734 (unsigned long long) sta->acct_session_id);
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800735 if (os_memcmp(attr->acct_session_id, buf, 16) != 0)
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800736 sta->radius_das_match = 0;
737 else
738 count++;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700739 }
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800740
741 if (count == 0) {
742 wpa_printf(MSG_DEBUG,
743 "RADIUS DAS: No matches remaining after Acct-Session-Id check");
744 return NULL;
745 }
746 wpa_printf(MSG_DEBUG, "RADIUS DAS: Acct-Session-Id match");
Dmitry Shmidt04949592012-07-19 12:16:46 -0700747 }
748
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800749 if (attr->acct_multi_session_id) {
750 num_attr++;
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800751 if (attr->acct_multi_session_id_len != 16) {
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800752 wpa_printf(MSG_DEBUG,
753 "RADIUS DAS: Acct-Multi-Session-Id cannot match");
754 return NULL;
755 }
756 count = 0;
757
758 for (sta = hapd->sta_list; sta; sta = sta->next) {
759 if (!sta->radius_das_match)
760 continue;
761 if (!sta->eapol_sm ||
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800762 !sta->eapol_sm->acct_multi_session_id) {
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800763 sta->radius_das_match = 0;
764 continue;
765 }
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800766 os_snprintf(buf, sizeof(buf), "%016llX",
767 (unsigned long long)
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800768 sta->eapol_sm->acct_multi_session_id);
769 if (os_memcmp(attr->acct_multi_session_id, buf, 16) !=
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800770 0)
771 sta->radius_das_match = 0;
772 else
773 count++;
774 }
775
776 if (count == 0) {
777 wpa_printf(MSG_DEBUG,
778 "RADIUS DAS: No matches remaining after Acct-Multi-Session-Id check");
779 return NULL;
780 }
781 wpa_printf(MSG_DEBUG,
782 "RADIUS DAS: Acct-Multi-Session-Id match");
783 }
784
785 if (attr->cui) {
786 num_attr++;
787 count = 0;
788
Dmitry Shmidt04949592012-07-19 12:16:46 -0700789 for (sta = hapd->sta_list; sta; sta = sta->next) {
790 struct wpabuf *cui;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800791
792 if (!sta->radius_das_match)
793 continue;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700794 cui = ieee802_1x_get_radius_cui(sta->eapol_sm);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800795 if (!cui || wpabuf_len(cui) != attr->cui_len ||
Dmitry Shmidt04949592012-07-19 12:16:46 -0700796 os_memcmp(wpabuf_head(cui), attr->cui,
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800797 attr->cui_len) != 0)
798 sta->radius_das_match = 0;
799 else
800 count++;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700801 }
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800802
803 if (count == 0) {
804 wpa_printf(MSG_DEBUG,
805 "RADIUS DAS: No matches remaining after Chargeable-User-Identity check");
806 return NULL;
807 }
808 wpa_printf(MSG_DEBUG,
809 "RADIUS DAS: Chargeable-User-Identity match");
Dmitry Shmidt04949592012-07-19 12:16:46 -0700810 }
811
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800812 if (attr->user_name) {
813 num_attr++;
814 count = 0;
815
Dmitry Shmidt04949592012-07-19 12:16:46 -0700816 for (sta = hapd->sta_list; sta; sta = sta->next) {
817 u8 *identity;
818 size_t identity_len;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800819
820 if (!sta->radius_das_match)
821 continue;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700822 identity = ieee802_1x_get_identity(sta->eapol_sm,
823 &identity_len);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800824 if (!identity ||
825 identity_len != attr->user_name_len ||
Dmitry Shmidt04949592012-07-19 12:16:46 -0700826 os_memcmp(identity, attr->user_name, identity_len)
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800827 != 0)
828 sta->radius_das_match = 0;
829 else
830 count++;
831 }
832
833 if (count == 0) {
834 wpa_printf(MSG_DEBUG,
835 "RADIUS DAS: No matches remaining after User-Name check");
836 return NULL;
837 }
838 wpa_printf(MSG_DEBUG,
839 "RADIUS DAS: User-Name match");
840 }
841
842 if (num_attr == 0) {
843 /*
844 * In theory, we could match all current associations, but it
845 * seems safer to just reject requests that do not include any
846 * session identification attributes.
847 */
848 wpa_printf(MSG_DEBUG,
849 "RADIUS DAS: No session identification attributes included");
850 return NULL;
851 }
852
853 selected = NULL;
854 for (sta = hapd->sta_list; sta; sta = sta->next) {
855 if (sta->radius_das_match) {
856 if (selected) {
857 *multi = 1;
858 return NULL;
859 }
860 selected = sta;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700861 }
862 }
863
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800864 return selected;
865}
866
867
868static int hostapd_das_disconnect_pmksa(struct hostapd_data *hapd,
869 struct radius_das_attrs *attr)
870{
871 if (!hapd->wpa_auth)
872 return -1;
873 return wpa_auth_radius_das_disconnect_pmksa(hapd->wpa_auth, attr);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700874}
875
876
877static enum radius_das_res
878hostapd_das_disconnect(void *ctx, struct radius_das_attrs *attr)
879{
880 struct hostapd_data *hapd = ctx;
881 struct sta_info *sta;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800882 int multi;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700883
884 if (hostapd_das_nas_mismatch(hapd, attr))
885 return RADIUS_DAS_NAS_MISMATCH;
886
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800887 sta = hostapd_das_find_sta(hapd, attr, &multi);
888 if (sta == NULL) {
889 if (multi) {
890 wpa_printf(MSG_DEBUG,
891 "RADIUS DAS: Multiple sessions match - not supported");
892 return RADIUS_DAS_MULTI_SESSION_MATCH;
893 }
894 if (hostapd_das_disconnect_pmksa(hapd, attr) == 0) {
895 wpa_printf(MSG_DEBUG,
896 "RADIUS DAS: PMKSA cache entry matched");
897 return RADIUS_DAS_SUCCESS;
898 }
899 wpa_printf(MSG_DEBUG, "RADIUS DAS: No matching session found");
Dmitry Shmidt04949592012-07-19 12:16:46 -0700900 return RADIUS_DAS_SESSION_NOT_FOUND;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800901 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700902
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800903 wpa_printf(MSG_DEBUG, "RADIUS DAS: Found a matching session " MACSTR
904 " - disconnecting", MAC2STR(sta->addr));
Dmitry Shmidt13ca8d82014-02-20 10:18:40 -0800905 wpa_auth_pmksa_remove(hapd->wpa_auth, sta->addr);
906
Dmitry Shmidt04949592012-07-19 12:16:46 -0700907 hostapd_drv_sta_deauth(hapd, sta->addr,
908 WLAN_REASON_PREV_AUTH_NOT_VALID);
909 ap_sta_deauthenticate(hapd, sta, WLAN_REASON_PREV_AUTH_NOT_VALID);
910
911 return RADIUS_DAS_SUCCESS;
912}
913
Roshan Pius3a1667e2018-07-03 15:17:14 -0700914
915#ifdef CONFIG_HS20
916static enum radius_das_res
917hostapd_das_coa(void *ctx, struct radius_das_attrs *attr)
918{
919 struct hostapd_data *hapd = ctx;
920 struct sta_info *sta;
921 int multi;
922
923 if (hostapd_das_nas_mismatch(hapd, attr))
924 return RADIUS_DAS_NAS_MISMATCH;
925
926 sta = hostapd_das_find_sta(hapd, attr, &multi);
927 if (!sta) {
928 if (multi) {
929 wpa_printf(MSG_DEBUG,
930 "RADIUS DAS: Multiple sessions match - not supported");
931 return RADIUS_DAS_MULTI_SESSION_MATCH;
932 }
933 wpa_printf(MSG_DEBUG, "RADIUS DAS: No matching session found");
934 return RADIUS_DAS_SESSION_NOT_FOUND;
935 }
936
937 wpa_printf(MSG_DEBUG, "RADIUS DAS: Found a matching session " MACSTR
938 " - CoA", MAC2STR(sta->addr));
939
940 if (attr->hs20_t_c_filtering) {
941 if (attr->hs20_t_c_filtering[0] & BIT(0)) {
942 wpa_printf(MSG_DEBUG,
943 "HS 2.0: Unexpected Terms and Conditions filtering required in CoA-Request");
944 return RADIUS_DAS_COA_FAILED;
945 }
946
947 hs20_t_c_filtering(hapd, sta, 0);
948 }
949
950 return RADIUS_DAS_SUCCESS;
951}
952#else /* CONFIG_HS20 */
953#define hostapd_das_coa NULL
954#endif /* CONFIG_HS20 */
955
Dmitry Shmidt04949592012-07-19 12:16:46 -0700956#endif /* CONFIG_NO_RADIUS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700957
958
959/**
960 * hostapd_setup_bss - Per-BSS setup (initialization)
961 * @hapd: Pointer to BSS data
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800962 * @first: Whether this BSS is the first BSS of an interface; -1 = not first,
963 * but interface may exist
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700964 *
965 * This function is used to initialize all per-BSS data structures and
966 * resources. This gets called in a loop for each BSS when an interface is
967 * initialized. Most of the modules that are initialized here will be
968 * deinitialized in hostapd_cleanup().
969 */
970static int hostapd_setup_bss(struct hostapd_data *hapd, int first)
971{
972 struct hostapd_bss_config *conf = hapd->conf;
Dmitry Shmidt9d9e6022015-04-23 10:34:55 -0700973 u8 ssid[SSID_MAX_LEN + 1];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700974 int ssid_len, set_ssid;
975 char force_ifname[IFNAMSIZ];
976 u8 if_addr[ETH_ALEN];
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800977 int flush_old_stations = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700978
Dmitry Shmidt54605472013-11-08 11:10:19 -0800979 wpa_printf(MSG_DEBUG, "%s(hapd=%p (%s), first=%d)",
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700980 __func__, hapd, conf->iface, first);
Dmitry Shmidt54605472013-11-08 11:10:19 -0800981
Dmitry Shmidt50b691d2014-05-21 14:01:45 -0700982#ifdef EAP_SERVER_TNC
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700983 if (conf->tnc && tncs_global_init() < 0) {
Dmitry Shmidt50b691d2014-05-21 14:01:45 -0700984 wpa_printf(MSG_ERROR, "Failed to initialize TNCS");
985 return -1;
986 }
987#endif /* EAP_SERVER_TNC */
988
Dmitry Shmidt54605472013-11-08 11:10:19 -0800989 if (hapd->started) {
990 wpa_printf(MSG_ERROR, "%s: Interface %s was already started",
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700991 __func__, conf->iface);
Dmitry Shmidt54605472013-11-08 11:10:19 -0800992 return -1;
993 }
994 hapd->started = 1;
995
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800996 if (!first || first == -1) {
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -0800997 u8 *addr = hapd->own_addr;
998
999 if (!is_zero_ether_addr(conf->bssid)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001000 /* Allocate the configured BSSID. */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001001 os_memcpy(hapd->own_addr, conf->bssid, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001002
1003 if (hostapd_mac_comp(hapd->own_addr,
1004 hapd->iface->bss[0]->own_addr) ==
1005 0) {
1006 wpa_printf(MSG_ERROR, "BSS '%s' may not have "
1007 "BSSID set to the MAC address of "
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001008 "the radio", conf->iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001009 return -1;
1010 }
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08001011 } else if (hapd->iconf->use_driver_iface_addr) {
1012 addr = NULL;
1013 } else {
1014 /* Allocate the next available BSSID. */
1015 do {
1016 inc_byte_array(hapd->own_addr, ETH_ALEN);
1017 } while (mac_in_conf(hapd->iconf, hapd->own_addr));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001018 }
1019
Dmitry Shmidt54605472013-11-08 11:10:19 -08001020 hapd->interface_added = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001021 if (hostapd_if_add(hapd->iface->bss[0], WPA_IF_AP_BSS,
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08001022 conf->iface, addr, hapd,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001023 &hapd->drv_priv, force_ifname, if_addr,
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001024 conf->bridge[0] ? conf->bridge : NULL,
1025 first == -1)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001026 wpa_printf(MSG_ERROR, "Failed to add BSS (BSSID="
1027 MACSTR ")", MAC2STR(hapd->own_addr));
Dmitry Shmidt3cf6f792013-12-18 13:12:19 -08001028 hapd->interface_added = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001029 return -1;
1030 }
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08001031
1032 if (!addr)
1033 os_memcpy(hapd->own_addr, if_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001034 }
1035
1036 if (conf->wmm_enabled < 0)
1037 conf->wmm_enabled = hapd->iconf->ieee80211n;
1038
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001039#ifdef CONFIG_IEEE80211R_AP
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001040 if (is_zero_ether_addr(conf->r1_key_holder))
1041 os_memcpy(conf->r1_key_holder, hapd->own_addr, ETH_ALEN);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001042#endif /* CONFIG_IEEE80211R_AP */
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001043
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001044#ifdef CONFIG_MESH
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001045 if ((hapd->conf->mesh & MESH_ENABLED) && hapd->iface->mconf == NULL)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001046 flush_old_stations = 0;
1047#endif /* CONFIG_MESH */
1048
1049 if (flush_old_stations)
1050 hostapd_flush_old_stations(hapd,
1051 WLAN_REASON_PREV_AUTH_NOT_VALID);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001052 hostapd_set_privacy(hapd, 0);
1053
1054 hostapd_broadcast_wep_clear(hapd);
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001055 if (hostapd_setup_encryption(conf->iface, hapd))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001056 return -1;
1057
1058 /*
1059 * Fetch the SSID from the system and use it or,
1060 * if one was specified in the config file, verify they
1061 * match.
1062 */
1063 ssid_len = hostapd_get_ssid(hapd, ssid, sizeof(ssid));
1064 if (ssid_len < 0) {
1065 wpa_printf(MSG_ERROR, "Could not read SSID from system");
1066 return -1;
1067 }
1068 if (conf->ssid.ssid_set) {
1069 /*
1070 * If SSID is specified in the config file and it differs
1071 * from what is being used then force installation of the
1072 * new SSID.
1073 */
1074 set_ssid = (conf->ssid.ssid_len != (size_t) ssid_len ||
1075 os_memcmp(conf->ssid.ssid, ssid, ssid_len) != 0);
1076 } else {
1077 /*
1078 * No SSID in the config file; just use the one we got
1079 * from the system.
1080 */
1081 set_ssid = 0;
1082 conf->ssid.ssid_len = ssid_len;
1083 os_memcpy(conf->ssid.ssid, ssid, conf->ssid.ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001084 }
1085
1086 if (!hostapd_drv_none(hapd)) {
1087 wpa_printf(MSG_ERROR, "Using interface %s with hwaddr " MACSTR
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001088 " and ssid \"%s\"",
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001089 conf->iface, MAC2STR(hapd->own_addr),
1090 wpa_ssid_txt(conf->ssid.ssid, conf->ssid.ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001091 }
1092
1093 if (hostapd_setup_wpa_psk(conf)) {
1094 wpa_printf(MSG_ERROR, "WPA-PSK setup failed.");
1095 return -1;
1096 }
1097
1098 /* Set SSID for the kernel driver (to be used in beacon and probe
1099 * response frames) */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001100 if (set_ssid && hostapd_set_ssid(hapd, conf->ssid.ssid,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001101 conf->ssid.ssid_len)) {
1102 wpa_printf(MSG_ERROR, "Could not set SSID for kernel driver");
1103 return -1;
1104 }
1105
Dmitry Shmidt818ea482014-03-10 13:15:21 -07001106 if (wpa_debug_level <= MSG_MSGDUMP)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001107 conf->radius->msg_dumps = 1;
1108#ifndef CONFIG_NO_RADIUS
1109 hapd->radius = radius_client_init(hapd, conf->radius);
1110 if (hapd->radius == NULL) {
1111 wpa_printf(MSG_ERROR, "RADIUS client initialization failed.");
1112 return -1;
1113 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001114
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001115 if (conf->radius_das_port) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001116 struct radius_das_conf das_conf;
1117 os_memset(&das_conf, 0, sizeof(das_conf));
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001118 das_conf.port = conf->radius_das_port;
1119 das_conf.shared_secret = conf->radius_das_shared_secret;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001120 das_conf.shared_secret_len =
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001121 conf->radius_das_shared_secret_len;
1122 das_conf.client_addr = &conf->radius_das_client_addr;
1123 das_conf.time_window = conf->radius_das_time_window;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001124 das_conf.require_event_timestamp =
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001125 conf->radius_das_require_event_timestamp;
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07001126 das_conf.require_message_authenticator =
1127 conf->radius_das_require_message_authenticator;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001128 das_conf.ctx = hapd;
1129 das_conf.disconnect = hostapd_das_disconnect;
Roshan Pius3a1667e2018-07-03 15:17:14 -07001130 das_conf.coa = hostapd_das_coa;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001131 hapd->radius_das = radius_das_init(&das_conf);
1132 if (hapd->radius_das == NULL) {
1133 wpa_printf(MSG_ERROR, "RADIUS DAS initialization "
1134 "failed.");
1135 return -1;
1136 }
1137 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001138#endif /* CONFIG_NO_RADIUS */
1139
1140 if (hostapd_acl_init(hapd)) {
1141 wpa_printf(MSG_ERROR, "ACL initialization failed.");
1142 return -1;
1143 }
1144 if (hostapd_init_wps(hapd, conf))
1145 return -1;
1146
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001147#ifdef CONFIG_DPP
1148 hapd->gas = gas_query_ap_init(hapd, hapd->msg_ctx);
1149 if (!hapd->gas)
1150 return -1;
1151 if (hostapd_dpp_init(hapd))
1152 return -1;
1153#endif /* CONFIG_DPP */
1154
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001155 if (authsrv_init(hapd) < 0)
1156 return -1;
1157
1158 if (ieee802_1x_init(hapd)) {
1159 wpa_printf(MSG_ERROR, "IEEE 802.1X initialization failed.");
1160 return -1;
1161 }
1162
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001163 if ((conf->wpa || conf->osen) && hostapd_setup_wpa(hapd))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001164 return -1;
1165
1166 if (accounting_init(hapd)) {
1167 wpa_printf(MSG_ERROR, "Accounting initialization failed.");
1168 return -1;
1169 }
1170
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001171 if (conf->ieee802_11f &&
1172 (hapd->iapp = iapp_init(hapd, conf->iapp_iface)) == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001173 wpa_printf(MSG_ERROR, "IEEE 802.11F (IAPP) initialization "
1174 "failed.");
1175 return -1;
1176 }
1177
Dmitry Shmidt04949592012-07-19 12:16:46 -07001178#ifdef CONFIG_INTERWORKING
1179 if (gas_serv_init(hapd)) {
1180 wpa_printf(MSG_ERROR, "GAS server initialization failed");
1181 return -1;
1182 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001183
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001184 if (conf->qos_map_set_len &&
1185 hostapd_drv_set_qos_map(hapd, conf->qos_map_set,
1186 conf->qos_map_set_len)) {
1187 wpa_printf(MSG_ERROR, "Failed to initialize QoS Map");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001188 return -1;
1189 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001190#endif /* CONFIG_INTERWORKING */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001191
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001192 if (conf->bss_load_update_period && bss_load_update_init(hapd)) {
1193 wpa_printf(MSG_ERROR, "BSS Load initialization failed");
1194 return -1;
1195 }
1196
1197 if (conf->proxy_arp) {
1198 if (x_snoop_init(hapd)) {
1199 wpa_printf(MSG_ERROR,
1200 "Generic snooping infrastructure initialization failed");
1201 return -1;
1202 }
1203
1204 if (dhcp_snoop_init(hapd)) {
1205 wpa_printf(MSG_ERROR,
1206 "DHCP snooping initialization failed");
1207 return -1;
1208 }
1209
1210 if (ndisc_snoop_init(hapd)) {
1211 wpa_printf(MSG_ERROR,
1212 "Neighbor Discovery snooping initialization failed");
1213 return -1;
1214 }
1215 }
1216
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001217 if (!hostapd_drv_none(hapd) && vlan_init(hapd)) {
1218 wpa_printf(MSG_ERROR, "VLAN initialization failed.");
1219 return -1;
1220 }
1221
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001222 if (!conf->start_disabled && ieee802_11_set_beacon(hapd) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001223 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001224
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001225 if (hapd->wpa_auth && wpa_init_keys(hapd->wpa_auth) < 0)
1226 return -1;
1227
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001228 if (hapd->driver && hapd->driver->set_operstate)
1229 hapd->driver->set_operstate(hapd->drv_priv, 1);
1230
1231 return 0;
1232}
1233
1234
1235static void hostapd_tx_queue_params(struct hostapd_iface *iface)
1236{
1237 struct hostapd_data *hapd = iface->bss[0];
1238 int i;
1239 struct hostapd_tx_queue_params *p;
1240
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001241#ifdef CONFIG_MESH
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001242 if ((hapd->conf->mesh & MESH_ENABLED) && iface->mconf == NULL)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001243 return;
1244#endif /* CONFIG_MESH */
1245
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001246 for (i = 0; i < NUM_TX_QUEUES; i++) {
1247 p = &iface->conf->tx_queue[i];
1248
1249 if (hostapd_set_tx_queue_params(hapd, i, p->aifs, p->cwmin,
1250 p->cwmax, p->burst)) {
1251 wpa_printf(MSG_DEBUG, "Failed to set TX queue "
1252 "parameters for queue %d.", i);
1253 /* Continue anyway */
1254 }
1255 }
1256}
1257
1258
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001259static int hostapd_set_acl_list(struct hostapd_data *hapd,
1260 struct mac_acl_entry *mac_acl,
1261 int n_entries, u8 accept_acl)
1262{
1263 struct hostapd_acl_params *acl_params;
1264 int i, err;
1265
1266 acl_params = os_zalloc(sizeof(*acl_params) +
1267 (n_entries * sizeof(acl_params->mac_acl[0])));
1268 if (!acl_params)
1269 return -ENOMEM;
1270
1271 for (i = 0; i < n_entries; i++)
1272 os_memcpy(acl_params->mac_acl[i].addr, mac_acl[i].addr,
1273 ETH_ALEN);
1274
1275 acl_params->acl_policy = accept_acl;
1276 acl_params->num_mac_acl = n_entries;
1277
1278 err = hostapd_drv_set_acl(hapd, acl_params);
1279
1280 os_free(acl_params);
1281
1282 return err;
1283}
1284
1285
1286static void hostapd_set_acl(struct hostapd_data *hapd)
1287{
1288 struct hostapd_config *conf = hapd->iconf;
1289 int err;
1290 u8 accept_acl;
1291
1292 if (hapd->iface->drv_max_acl_mac_addrs == 0)
1293 return;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001294
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001295 if (conf->bss[0]->macaddr_acl == DENY_UNLESS_ACCEPTED) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001296 accept_acl = 1;
1297 err = hostapd_set_acl_list(hapd, conf->bss[0]->accept_mac,
1298 conf->bss[0]->num_accept_mac,
1299 accept_acl);
1300 if (err) {
1301 wpa_printf(MSG_DEBUG, "Failed to set accept acl");
1302 return;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001303 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001304 } else if (conf->bss[0]->macaddr_acl == ACCEPT_UNLESS_DENIED) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001305 accept_acl = 0;
1306 err = hostapd_set_acl_list(hapd, conf->bss[0]->deny_mac,
1307 conf->bss[0]->num_deny_mac,
1308 accept_acl);
1309 if (err) {
1310 wpa_printf(MSG_DEBUG, "Failed to set deny acl");
1311 return;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001312 }
1313 }
1314}
1315
1316
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001317static int start_ctrl_iface_bss(struct hostapd_data *hapd)
1318{
1319 if (!hapd->iface->interfaces ||
1320 !hapd->iface->interfaces->ctrl_iface_init)
1321 return 0;
1322
1323 if (hapd->iface->interfaces->ctrl_iface_init(hapd)) {
1324 wpa_printf(MSG_ERROR,
1325 "Failed to setup control interface for %s",
1326 hapd->conf->iface);
1327 return -1;
1328 }
1329
1330 return 0;
1331}
1332
1333
1334static int start_ctrl_iface(struct hostapd_iface *iface)
1335{
1336 size_t i;
1337
1338 if (!iface->interfaces || !iface->interfaces->ctrl_iface_init)
1339 return 0;
1340
1341 for (i = 0; i < iface->num_bss; i++) {
1342 struct hostapd_data *hapd = iface->bss[i];
1343 if (iface->interfaces->ctrl_iface_init(hapd)) {
1344 wpa_printf(MSG_ERROR,
1345 "Failed to setup control interface for %s",
1346 hapd->conf->iface);
1347 return -1;
1348 }
1349 }
1350
1351 return 0;
1352}
1353
1354
1355static void channel_list_update_timeout(void *eloop_ctx, void *timeout_ctx)
1356{
1357 struct hostapd_iface *iface = eloop_ctx;
1358
1359 if (!iface->wait_channel_update) {
1360 wpa_printf(MSG_INFO, "Channel list update timeout, but interface was not waiting for it");
1361 return;
1362 }
1363
1364 /*
1365 * It is possible that the existing channel list is acceptable, so try
1366 * to proceed.
1367 */
1368 wpa_printf(MSG_DEBUG, "Channel list update timeout - try to continue anyway");
1369 setup_interface2(iface);
1370}
1371
1372
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001373void hostapd_channel_list_updated(struct hostapd_iface *iface, int initiator)
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001374{
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001375 if (!iface->wait_channel_update || initiator != REGDOM_SET_BY_USER)
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001376 return;
1377
1378 wpa_printf(MSG_DEBUG, "Channel list updated - continue setup");
1379 eloop_cancel_timeout(channel_list_update_timeout, iface, NULL);
1380 setup_interface2(iface);
1381}
1382
1383
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001384static int setup_interface(struct hostapd_iface *iface)
1385{
1386 struct hostapd_data *hapd = iface->bss[0];
1387 size_t i;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001388
Dmitry Shmidta38abf92014-03-06 13:38:44 -08001389 /*
1390 * It is possible that setup_interface() is called after the interface
1391 * was disabled etc., in which case driver_ap_teardown is possibly set
1392 * to 1. Clear it here so any other key/station deletion, which is not
1393 * part of a teardown flow, would also call the relevant driver
1394 * callbacks.
1395 */
1396 iface->driver_ap_teardown = 0;
1397
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001398 if (!iface->phy[0]) {
1399 const char *phy = hostapd_drv_get_radio_name(hapd);
1400 if (phy) {
1401 wpa_printf(MSG_DEBUG, "phy: %s", phy);
1402 os_strlcpy(iface->phy, phy, sizeof(iface->phy));
1403 }
1404 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001405
1406 /*
1407 * Make sure that all BSSes get configured with a pointer to the same
1408 * driver interface.
1409 */
1410 for (i = 1; i < iface->num_bss; i++) {
1411 iface->bss[i]->driver = hapd->driver;
1412 iface->bss[i]->drv_priv = hapd->drv_priv;
1413 }
1414
1415 if (hostapd_validate_bssid_configuration(iface))
1416 return -1;
1417
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001418 /*
1419 * Initialize control interfaces early to allow external monitoring of
1420 * channel setup operations that may take considerable amount of time
1421 * especially for DFS cases.
1422 */
1423 if (start_ctrl_iface(iface))
1424 return -1;
1425
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001426 if (hapd->iconf->country[0] && hapd->iconf->country[1]) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001427 char country[4], previous_country[4];
1428
1429 hostapd_set_state(iface, HAPD_IFACE_COUNTRY_UPDATE);
1430 if (hostapd_get_country(hapd, previous_country) < 0)
1431 previous_country[0] = '\0';
1432
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001433 os_memcpy(country, hapd->iconf->country, 3);
1434 country[3] = '\0';
1435 if (hostapd_set_country(hapd, country) < 0) {
1436 wpa_printf(MSG_ERROR, "Failed to set country code");
1437 return -1;
1438 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001439
1440 wpa_printf(MSG_DEBUG, "Previous country code %s, new country code %s",
1441 previous_country, country);
1442
1443 if (os_strncmp(previous_country, country, 2) != 0) {
1444 wpa_printf(MSG_DEBUG, "Continue interface setup after channel list update");
1445 iface->wait_channel_update = 1;
Dmitry Shmidt97672262014-02-03 13:02:54 -08001446 eloop_register_timeout(5, 0,
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001447 channel_list_update_timeout,
1448 iface, NULL);
1449 return 0;
1450 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001451 }
1452
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001453 return setup_interface2(iface);
1454}
1455
1456
1457static int setup_interface2(struct hostapd_iface *iface)
1458{
1459 iface->wait_channel_update = 0;
1460
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001461 if (hostapd_get_hw_features(iface)) {
1462 /* Not all drivers support this yet, so continue without hw
1463 * feature data. */
1464 } else {
1465 int ret = hostapd_select_hw_mode(iface);
1466 if (ret < 0) {
1467 wpa_printf(MSG_ERROR, "Could not select hw_mode and "
1468 "channel. (%d)", ret);
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001469 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001470 }
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001471 if (ret == 1) {
1472 wpa_printf(MSG_DEBUG, "Interface initialization will be completed in a callback (ACS)");
1473 return 0;
1474 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001475 ret = hostapd_check_ht_capab(iface);
1476 if (ret < 0)
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001477 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001478 if (ret == 1) {
1479 wpa_printf(MSG_DEBUG, "Interface initialization will "
1480 "be completed in a callback");
1481 return 0;
1482 }
Dmitry Shmidt051af732013-10-22 13:52:46 -07001483
1484 if (iface->conf->ieee80211h)
1485 wpa_printf(MSG_DEBUG, "DFS support is enabled");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001486 }
1487 return hostapd_setup_interface_complete(iface, 0);
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001488
1489fail:
1490 hostapd_set_state(iface, HAPD_IFACE_DISABLED);
1491 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
1492 if (iface->interfaces && iface->interfaces->terminate_on_error)
1493 eloop_terminate();
1494 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001495}
1496
1497
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001498#ifdef CONFIG_FST
1499
1500static const u8 * fst_hostapd_get_bssid_cb(void *ctx)
1501{
1502 struct hostapd_data *hapd = ctx;
1503
1504 return hapd->own_addr;
1505}
1506
1507
1508static void fst_hostapd_get_channel_info_cb(void *ctx,
1509 enum hostapd_hw_mode *hw_mode,
1510 u8 *channel)
1511{
1512 struct hostapd_data *hapd = ctx;
1513
1514 *hw_mode = ieee80211_freq_to_chan(hapd->iface->freq, channel);
1515}
1516
1517
1518static void fst_hostapd_set_ies_cb(void *ctx, const struct wpabuf *fst_ies)
1519{
1520 struct hostapd_data *hapd = ctx;
1521
1522 if (hapd->iface->fst_ies != fst_ies) {
1523 hapd->iface->fst_ies = fst_ies;
1524 if (ieee802_11_set_beacon(hapd))
1525 wpa_printf(MSG_WARNING, "FST: Cannot set beacon");
1526 }
1527}
1528
1529
1530static int fst_hostapd_send_action_cb(void *ctx, const u8 *da,
1531 struct wpabuf *buf)
1532{
1533 struct hostapd_data *hapd = ctx;
1534
1535 return hostapd_drv_send_action(hapd, hapd->iface->freq, 0, da,
1536 wpabuf_head(buf), wpabuf_len(buf));
1537}
1538
1539
1540static const struct wpabuf * fst_hostapd_get_mb_ie_cb(void *ctx, const u8 *addr)
1541{
1542 struct hostapd_data *hapd = ctx;
1543 struct sta_info *sta = ap_get_sta(hapd, addr);
1544
1545 return sta ? sta->mb_ies : NULL;
1546}
1547
1548
1549static void fst_hostapd_update_mb_ie_cb(void *ctx, const u8 *addr,
1550 const u8 *buf, size_t size)
1551{
1552 struct hostapd_data *hapd = ctx;
1553 struct sta_info *sta = ap_get_sta(hapd, addr);
1554
1555 if (sta) {
1556 struct mb_ies_info info;
1557
1558 if (!mb_ies_info_by_ies(&info, buf, size)) {
1559 wpabuf_free(sta->mb_ies);
1560 sta->mb_ies = mb_ies_by_info(&info);
1561 }
1562 }
1563}
1564
1565
1566static const u8 * fst_hostapd_get_sta(struct fst_get_peer_ctx **get_ctx,
1567 Boolean mb_only)
1568{
1569 struct sta_info *s = (struct sta_info *) *get_ctx;
1570
1571 if (mb_only) {
1572 for (; s && !s->mb_ies; s = s->next)
1573 ;
1574 }
1575
1576 if (s) {
1577 *get_ctx = (struct fst_get_peer_ctx *) s->next;
1578
1579 return s->addr;
1580 }
1581
1582 *get_ctx = NULL;
1583 return NULL;
1584}
1585
1586
1587static const u8 * fst_hostapd_get_peer_first(void *ctx,
1588 struct fst_get_peer_ctx **get_ctx,
1589 Boolean mb_only)
1590{
1591 struct hostapd_data *hapd = ctx;
1592
1593 *get_ctx = (struct fst_get_peer_ctx *) hapd->sta_list;
1594
1595 return fst_hostapd_get_sta(get_ctx, mb_only);
1596}
1597
1598
1599static const u8 * fst_hostapd_get_peer_next(void *ctx,
1600 struct fst_get_peer_ctx **get_ctx,
1601 Boolean mb_only)
1602{
1603 return fst_hostapd_get_sta(get_ctx, mb_only);
1604}
1605
1606
1607void fst_hostapd_fill_iface_obj(struct hostapd_data *hapd,
1608 struct fst_wpa_obj *iface_obj)
1609{
1610 iface_obj->ctx = hapd;
1611 iface_obj->get_bssid = fst_hostapd_get_bssid_cb;
1612 iface_obj->get_channel_info = fst_hostapd_get_channel_info_cb;
1613 iface_obj->set_ies = fst_hostapd_set_ies_cb;
1614 iface_obj->send_action = fst_hostapd_send_action_cb;
1615 iface_obj->get_mb_ie = fst_hostapd_get_mb_ie_cb;
1616 iface_obj->update_mb_ie = fst_hostapd_update_mb_ie_cb;
1617 iface_obj->get_peer_first = fst_hostapd_get_peer_first;
1618 iface_obj->get_peer_next = fst_hostapd_get_peer_next;
1619}
1620
1621#endif /* CONFIG_FST */
1622
1623
Dmitry Shmidt849734c2016-05-27 09:59:01 -07001624#ifdef NEED_AP_MLME
1625static enum nr_chan_width hostapd_get_nr_chan_width(struct hostapd_data *hapd,
1626 int ht, int vht)
1627{
1628 if (!ht && !vht)
1629 return NR_CHAN_WIDTH_20;
1630 if (!hapd->iconf->secondary_channel)
1631 return NR_CHAN_WIDTH_20;
1632 if (!vht || hapd->iconf->vht_oper_chwidth == VHT_CHANWIDTH_USE_HT)
1633 return NR_CHAN_WIDTH_40;
1634 if (hapd->iconf->vht_oper_chwidth == VHT_CHANWIDTH_80MHZ)
1635 return NR_CHAN_WIDTH_80;
1636 if (hapd->iconf->vht_oper_chwidth == VHT_CHANWIDTH_160MHZ)
1637 return NR_CHAN_WIDTH_160;
1638 if (hapd->iconf->vht_oper_chwidth == VHT_CHANWIDTH_80P80MHZ)
1639 return NR_CHAN_WIDTH_80P80;
1640 return NR_CHAN_WIDTH_20;
1641}
1642#endif /* NEED_AP_MLME */
1643
1644
1645static void hostapd_set_own_neighbor_report(struct hostapd_data *hapd)
1646{
1647#ifdef NEED_AP_MLME
1648 u16 capab = hostapd_own_capab_info(hapd);
1649 int ht = hapd->iconf->ieee80211n && !hapd->conf->disable_11n;
1650 int vht = hapd->iconf->ieee80211ac && !hapd->conf->disable_11ac;
1651 struct wpa_ssid_value ssid;
1652 u8 channel, op_class;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001653 u8 center_freq1_idx = 0, center_freq2_idx = 0;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07001654 enum nr_chan_width width;
1655 u32 bssid_info;
1656 struct wpabuf *nr;
1657
1658 if (!(hapd->conf->radio_measurements[0] &
1659 WLAN_RRM_CAPS_NEIGHBOR_REPORT))
1660 return;
1661
1662 bssid_info = 3; /* AP is reachable */
1663 bssid_info |= NEI_REP_BSSID_INFO_SECURITY; /* "same as the AP" */
1664 bssid_info |= NEI_REP_BSSID_INFO_KEY_SCOPE; /* "same as the AP" */
1665
1666 if (capab & WLAN_CAPABILITY_SPECTRUM_MGMT)
1667 bssid_info |= NEI_REP_BSSID_INFO_SPECTRUM_MGMT;
1668
1669 bssid_info |= NEI_REP_BSSID_INFO_RM; /* RRM is supported */
1670
1671 if (hapd->conf->wmm_enabled) {
1672 bssid_info |= NEI_REP_BSSID_INFO_QOS;
1673
1674 if (hapd->conf->wmm_uapsd &&
1675 (hapd->iface->drv_flags & WPA_DRIVER_FLAGS_AP_UAPSD))
1676 bssid_info |= NEI_REP_BSSID_INFO_APSD;
1677 }
1678
1679 if (ht) {
1680 bssid_info |= NEI_REP_BSSID_INFO_HT |
1681 NEI_REP_BSSID_INFO_DELAYED_BA;
1682
1683 /* VHT bit added in IEEE P802.11-REVmc/D4.3 */
1684 if (vht)
1685 bssid_info |= NEI_REP_BSSID_INFO_VHT;
1686 }
1687
1688 /* TODO: Set NEI_REP_BSSID_INFO_MOBILITY_DOMAIN if MDE is set */
1689
Dmitry Shmidt29333592017-01-09 12:27:11 -08001690 if (ieee80211_freq_to_channel_ext(hapd->iface->freq,
1691 hapd->iconf->secondary_channel,
1692 hapd->iconf->vht_oper_chwidth,
1693 &op_class, &channel) ==
1694 NUM_HOSTAPD_MODES)
1695 return;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07001696 width = hostapd_get_nr_chan_width(hapd, ht, vht);
1697 if (vht) {
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001698 center_freq1_idx = hapd->iconf->vht_oper_centr_freq_seg0_idx;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07001699 if (width == NR_CHAN_WIDTH_80P80)
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001700 center_freq2_idx =
1701 hapd->iconf->vht_oper_centr_freq_seg1_idx;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07001702 } else if (ht) {
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001703 ieee80211_freq_to_chan(hapd->iface->freq +
1704 10 * hapd->iconf->secondary_channel,
1705 &center_freq1_idx);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07001706 }
1707
1708 ssid.ssid_len = hapd->conf->ssid.ssid_len;
1709 os_memcpy(ssid.ssid, hapd->conf->ssid.ssid, ssid.ssid_len);
1710
1711 /*
1712 * Neighbor Report element size = BSSID + BSSID info + op_class + chan +
1713 * phy type + wide bandwidth channel subelement.
1714 */
1715 nr = wpabuf_alloc(ETH_ALEN + 4 + 1 + 1 + 1 + 5);
1716 if (!nr)
1717 return;
1718
1719 wpabuf_put_data(nr, hapd->own_addr, ETH_ALEN);
1720 wpabuf_put_le32(nr, bssid_info);
1721 wpabuf_put_u8(nr, op_class);
1722 wpabuf_put_u8(nr, channel);
1723 wpabuf_put_u8(nr, ieee80211_get_phy_type(hapd->iface->freq, ht, vht));
1724
1725 /*
1726 * Wide Bandwidth Channel subelement may be needed to allow the
1727 * receiving STA to send packets to the AP. See IEEE P802.11-REVmc/D5.0
1728 * Figure 9-301.
1729 */
1730 wpabuf_put_u8(nr, WNM_NEIGHBOR_WIDE_BW_CHAN);
1731 wpabuf_put_u8(nr, 3);
1732 wpabuf_put_u8(nr, width);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001733 wpabuf_put_u8(nr, center_freq1_idx);
1734 wpabuf_put_u8(nr, center_freq2_idx);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07001735
1736 hostapd_neighbor_set(hapd, hapd->own_addr, &ssid, nr, hapd->iconf->lci,
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001737 hapd->iconf->civic, hapd->iconf->stationary_ap);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07001738
1739 wpabuf_free(nr);
1740#endif /* NEED_AP_MLME */
1741}
1742
1743
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001744#ifdef CONFIG_OWE
1745
1746static int hostapd_owe_iface_iter(struct hostapd_iface *iface, void *ctx)
1747{
1748 struct hostapd_data *hapd = ctx;
1749 size_t i;
1750
1751 for (i = 0; i < iface->num_bss; i++) {
1752 struct hostapd_data *bss = iface->bss[i];
1753
1754 if (os_strcmp(hapd->conf->owe_transition_ifname,
1755 bss->conf->iface) != 0)
1756 continue;
1757
1758 wpa_printf(MSG_DEBUG,
1759 "OWE: ifname=%s found transition mode ifname=%s BSSID "
1760 MACSTR " SSID %s",
1761 hapd->conf->iface, bss->conf->iface,
1762 MAC2STR(bss->own_addr),
1763 wpa_ssid_txt(bss->conf->ssid.ssid,
1764 bss->conf->ssid.ssid_len));
1765 if (!bss->conf->ssid.ssid_set || !bss->conf->ssid.ssid_len ||
1766 is_zero_ether_addr(bss->own_addr))
1767 continue;
1768
1769 os_memcpy(hapd->conf->owe_transition_bssid, bss->own_addr,
1770 ETH_ALEN);
1771 os_memcpy(hapd->conf->owe_transition_ssid,
1772 bss->conf->ssid.ssid, bss->conf->ssid.ssid_len);
1773 hapd->conf->owe_transition_ssid_len = bss->conf->ssid.ssid_len;
1774 wpa_printf(MSG_DEBUG,
1775 "OWE: Copied transition mode information");
1776 return 1;
1777 }
1778
1779 return 0;
1780}
1781
1782
1783int hostapd_owe_trans_get_info(struct hostapd_data *hapd)
1784{
1785 if (hapd->conf->owe_transition_ssid_len > 0 &&
1786 !is_zero_ether_addr(hapd->conf->owe_transition_bssid))
1787 return 0;
1788
1789 /* Find transition mode SSID/BSSID information from a BSS operated by
1790 * this hostapd instance. */
1791 if (!hapd->iface->interfaces ||
1792 !hapd->iface->interfaces->for_each_interface)
1793 return hostapd_owe_iface_iter(hapd->iface, hapd);
1794 else
1795 return hapd->iface->interfaces->for_each_interface(
1796 hapd->iface->interfaces, hostapd_owe_iface_iter, hapd);
1797}
1798
1799
1800static int hostapd_owe_iface_iter2(struct hostapd_iface *iface, void *ctx)
1801{
1802 size_t i;
1803
1804 for (i = 0; i < iface->num_bss; i++) {
1805 struct hostapd_data *bss = iface->bss[i];
1806 int res;
1807
1808 if (!bss->conf->owe_transition_ifname[0])
1809 continue;
1810 res = hostapd_owe_trans_get_info(bss);
1811 if (res == 0)
1812 continue;
1813 wpa_printf(MSG_DEBUG,
1814 "OWE: Matching transition mode interface enabled - update beacon data for %s",
1815 bss->conf->iface);
1816 ieee802_11_set_beacon(bss);
1817 }
1818
1819 return 0;
1820}
1821
1822#endif /* CONFIG_OWE */
1823
1824
1825static void hostapd_owe_update_trans(struct hostapd_iface *iface)
1826{
1827#ifdef CONFIG_OWE
1828 /* Check whether the enabled BSS can complete OWE transition mode
1829 * configuration for any pending interface. */
1830 if (!iface->interfaces ||
1831 !iface->interfaces->for_each_interface)
1832 hostapd_owe_iface_iter2(iface, NULL);
1833 else
1834 iface->interfaces->for_each_interface(
1835 iface->interfaces, hostapd_owe_iface_iter2, NULL);
1836#endif /* CONFIG_OWE */
1837}
1838
1839
Roshan Pius3a1667e2018-07-03 15:17:14 -07001840static void hostapd_interface_setup_failure_handler(void *eloop_ctx,
1841 void *timeout_ctx)
1842{
1843 struct hostapd_iface *iface = eloop_ctx;
1844 struct hostapd_data *hapd;
1845
1846 if (iface->num_bss < 1 || !iface->bss || !iface->bss[0])
1847 return;
1848 hapd = iface->bss[0];
1849 if (hapd->setup_complete_cb)
1850 hapd->setup_complete_cb(hapd->setup_complete_cb_ctx);
1851}
1852
1853
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001854static int hostapd_setup_interface_complete_sync(struct hostapd_iface *iface,
1855 int err)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001856{
1857 struct hostapd_data *hapd = iface->bss[0];
1858 size_t j;
1859 u8 *prev_addr;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001860 int delay_apply_cfg = 0;
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001861 int res_dfs_offload = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001862
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001863 if (err)
1864 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001865
1866 wpa_printf(MSG_DEBUG, "Completing interface initialization");
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001867 if (iface->conf->channel) {
Dmitry Shmidt051af732013-10-22 13:52:46 -07001868#ifdef NEED_AP_MLME
1869 int res;
1870#endif /* NEED_AP_MLME */
1871
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001872 iface->freq = hostapd_hw_get_freq(hapd, iface->conf->channel);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001873 wpa_printf(MSG_DEBUG, "Mode: %s Channel: %d "
1874 "Frequency: %d MHz",
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001875 hostapd_hw_mode_txt(iface->conf->hw_mode),
1876 iface->conf->channel, iface->freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001877
Dmitry Shmidt051af732013-10-22 13:52:46 -07001878#ifdef NEED_AP_MLME
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001879 /* Handle DFS only if it is not offloaded to the driver */
1880 if (!(iface->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD)) {
1881 /* Check DFS */
1882 res = hostapd_handle_dfs(iface);
1883 if (res <= 0) {
1884 if (res < 0)
1885 goto fail;
1886 return res;
1887 }
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001888 } else {
1889 /* If DFS is offloaded to the driver */
1890 res_dfs_offload = hostapd_handle_dfs_offload(iface);
1891 if (res_dfs_offload <= 0) {
1892 if (res_dfs_offload < 0)
1893 goto fail;
1894 } else {
1895 wpa_printf(MSG_DEBUG,
1896 "Proceed with AP/channel setup");
1897 /*
1898 * If this is a DFS channel, move to completing
1899 * AP setup.
1900 */
1901 if (res_dfs_offload == 1)
1902 goto dfs_offload;
1903 /* Otherwise fall through. */
1904 }
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001905 }
Dmitry Shmidt051af732013-10-22 13:52:46 -07001906#endif /* NEED_AP_MLME */
1907
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001908#ifdef CONFIG_MESH
1909 if (iface->mconf != NULL) {
1910 wpa_printf(MSG_DEBUG,
1911 "%s: Mesh configuration will be applied while joining the mesh network",
1912 iface->bss[0]->conf->iface);
1913 delay_apply_cfg = 1;
1914 }
1915#endif /* CONFIG_MESH */
1916
1917 if (!delay_apply_cfg &&
1918 hostapd_set_freq(hapd, hapd->iconf->hw_mode, iface->freq,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001919 hapd->iconf->channel,
1920 hapd->iconf->ieee80211n,
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001921 hapd->iconf->ieee80211ac,
1922 hapd->iconf->secondary_channel,
1923 hapd->iconf->vht_oper_chwidth,
1924 hapd->iconf->vht_oper_centr_freq_seg0_idx,
1925 hapd->iconf->vht_oper_centr_freq_seg1_idx)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001926 wpa_printf(MSG_ERROR, "Could not set channel for "
1927 "kernel driver");
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001928 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001929 }
1930 }
1931
1932 if (iface->current_mode) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001933 if (hostapd_prepare_rates(iface, iface->current_mode)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001934 wpa_printf(MSG_ERROR, "Failed to prepare rates "
1935 "table.");
1936 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
1937 HOSTAPD_LEVEL_WARNING,
1938 "Failed to prepare rates table.");
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001939 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001940 }
1941 }
1942
1943 if (hapd->iconf->rts_threshold > -1 &&
1944 hostapd_set_rts(hapd, hapd->iconf->rts_threshold)) {
1945 wpa_printf(MSG_ERROR, "Could not set RTS threshold for "
1946 "kernel driver");
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001947 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001948 }
1949
1950 if (hapd->iconf->fragm_threshold > -1 &&
1951 hostapd_set_frag(hapd, hapd->iconf->fragm_threshold)) {
1952 wpa_printf(MSG_ERROR, "Could not set fragmentation threshold "
1953 "for kernel driver");
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001954 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001955 }
1956
1957 prev_addr = hapd->own_addr;
1958
1959 for (j = 0; j < iface->num_bss; j++) {
1960 hapd = iface->bss[j];
1961 if (j)
1962 os_memcpy(hapd->own_addr, prev_addr, ETH_ALEN);
Dmitry Shmidt71757432014-06-02 13:50:35 -07001963 if (hostapd_setup_bss(hapd, j == 0)) {
1964 do {
1965 hapd = iface->bss[j];
1966 hostapd_bss_deinit_no_free(hapd);
1967 hostapd_free_hapd_data(hapd);
1968 } while (j-- > 0);
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001969 goto fail;
Dmitry Shmidt71757432014-06-02 13:50:35 -07001970 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001971 if (is_zero_ether_addr(hapd->conf->bssid))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001972 prev_addr = hapd->own_addr;
1973 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001974 hapd = iface->bss[0];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001975
1976 hostapd_tx_queue_params(iface);
1977
1978 ap_list_init(iface);
1979
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001980 hostapd_set_acl(hapd);
1981
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001982 if (hostapd_driver_commit(hapd) < 0) {
1983 wpa_printf(MSG_ERROR, "%s: Failed to commit driver "
1984 "configuration", __func__);
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001985 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001986 }
1987
Jouni Malinen87fd2792011-05-16 18:35:42 +03001988 /*
1989 * WPS UPnP module can be initialized only when the "upnp_iface" is up.
1990 * If "interface" and "upnp_iface" are the same (e.g., non-bridge
1991 * mode), the interface is up only after driver_commit, so initialize
1992 * WPS after driver_commit.
1993 */
1994 for (j = 0; j < iface->num_bss; j++) {
1995 if (hostapd_init_wps_complete(iface->bss[j]))
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001996 goto fail;
Jouni Malinen87fd2792011-05-16 18:35:42 +03001997 }
1998
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001999 if ((iface->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
2000 !res_dfs_offload) {
2001 /*
2002 * If freq is DFS, and DFS is offloaded to the driver, then wait
2003 * for CAC to complete.
2004 */
2005 wpa_printf(MSG_DEBUG, "%s: Wait for CAC to complete", __func__);
2006 return res_dfs_offload;
2007 }
2008
2009#ifdef NEED_AP_MLME
2010dfs_offload:
2011#endif /* NEED_AP_MLME */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002012
2013#ifdef CONFIG_FST
2014 if (hapd->iconf->fst_cfg.group_id[0]) {
2015 struct fst_wpa_obj iface_obj;
2016
2017 fst_hostapd_fill_iface_obj(hapd, &iface_obj);
2018 iface->fst = fst_attach(hapd->conf->iface, hapd->own_addr,
2019 &iface_obj, &hapd->iconf->fst_cfg);
2020 if (!iface->fst) {
2021 wpa_printf(MSG_ERROR, "Could not attach to FST %s",
2022 hapd->iconf->fst_cfg.group_id);
2023 goto fail;
2024 }
2025 }
2026#endif /* CONFIG_FST */
2027
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002028 hostapd_set_state(iface, HAPD_IFACE_ENABLED);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002029 hostapd_owe_update_trans(iface);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002030 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_ENABLED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002031 if (hapd->setup_complete_cb)
2032 hapd->setup_complete_cb(hapd->setup_complete_cb_ctx);
2033
2034 wpa_printf(MSG_DEBUG, "%s: Setup of interface done.",
2035 iface->bss[0]->conf->iface);
Dmitry Shmidtb96dad42013-11-05 10:07:29 -08002036 if (iface->interfaces && iface->interfaces->terminate_on_error > 0)
2037 iface->interfaces->terminate_on_error--;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002038
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002039 for (j = 0; j < iface->num_bss; j++)
2040 hostapd_set_own_neighbor_report(iface->bss[j]);
2041
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002042 return 0;
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002043
2044fail:
2045 wpa_printf(MSG_ERROR, "Interface initialization failed");
2046 hostapd_set_state(iface, HAPD_IFACE_DISABLED);
2047 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002048#ifdef CONFIG_FST
2049 if (iface->fst) {
2050 fst_detach(iface->fst);
2051 iface->fst = NULL;
2052 }
2053#endif /* CONFIG_FST */
Roshan Pius3a1667e2018-07-03 15:17:14 -07002054
2055 if (iface->interfaces && iface->interfaces->terminate_on_error) {
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002056 eloop_terminate();
Roshan Pius3a1667e2018-07-03 15:17:14 -07002057 } else if (hapd->setup_complete_cb) {
2058 /*
2059 * Calling hapd->setup_complete_cb directly may cause iface
2060 * deinitialization which may be accessed later by the caller.
2061 */
2062 eloop_register_timeout(0, 0,
2063 hostapd_interface_setup_failure_handler,
2064 iface, NULL);
2065 }
2066
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002067 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002068}
2069
2070
2071/**
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002072 * hostapd_setup_interface_complete - Complete interface setup
2073 *
2074 * This function is called when previous steps in the interface setup has been
2075 * completed. This can also start operations, e.g., DFS, that will require
2076 * additional processing before interface is ready to be enabled. Such
2077 * operations will call this function from eloop callbacks when finished.
2078 */
2079int hostapd_setup_interface_complete(struct hostapd_iface *iface, int err)
2080{
2081 struct hapd_interfaces *interfaces = iface->interfaces;
2082 struct hostapd_data *hapd = iface->bss[0];
2083 unsigned int i;
2084 int not_ready_in_sync_ifaces = 0;
2085
2086 if (!iface->need_to_start_in_sync)
2087 return hostapd_setup_interface_complete_sync(iface, err);
2088
2089 if (err) {
2090 wpa_printf(MSG_ERROR, "Interface initialization failed");
2091 hostapd_set_state(iface, HAPD_IFACE_DISABLED);
2092 iface->need_to_start_in_sync = 0;
2093 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
2094 if (interfaces && interfaces->terminate_on_error)
2095 eloop_terminate();
2096 return -1;
2097 }
2098
2099 if (iface->ready_to_start_in_sync) {
2100 /* Already in ready and waiting. should never happpen */
2101 return 0;
2102 }
2103
2104 for (i = 0; i < interfaces->count; i++) {
2105 if (interfaces->iface[i]->need_to_start_in_sync &&
2106 !interfaces->iface[i]->ready_to_start_in_sync)
2107 not_ready_in_sync_ifaces++;
2108 }
2109
2110 /*
2111 * Check if this is the last interface, if yes then start all the other
2112 * waiting interfaces. If not, add this interface to the waiting list.
2113 */
2114 if (not_ready_in_sync_ifaces > 1 && iface->state == HAPD_IFACE_DFS) {
2115 /*
2116 * If this interface went through CAC, do not synchronize, just
2117 * start immediately.
2118 */
2119 iface->need_to_start_in_sync = 0;
2120 wpa_printf(MSG_INFO,
2121 "%s: Finished CAC - bypass sync and start interface",
2122 iface->bss[0]->conf->iface);
2123 return hostapd_setup_interface_complete_sync(iface, err);
2124 }
2125
2126 if (not_ready_in_sync_ifaces > 1) {
2127 /* need to wait as there are other interfaces still coming up */
2128 iface->ready_to_start_in_sync = 1;
2129 wpa_printf(MSG_INFO,
2130 "%s: Interface waiting to sync with other interfaces",
2131 iface->bss[0]->conf->iface);
2132 return 0;
2133 }
2134
2135 wpa_printf(MSG_INFO,
2136 "%s: Last interface to sync - starting all interfaces",
2137 iface->bss[0]->conf->iface);
2138 iface->need_to_start_in_sync = 0;
2139 hostapd_setup_interface_complete_sync(iface, err);
2140 for (i = 0; i < interfaces->count; i++) {
2141 if (interfaces->iface[i]->need_to_start_in_sync &&
2142 interfaces->iface[i]->ready_to_start_in_sync) {
2143 hostapd_setup_interface_complete_sync(
2144 interfaces->iface[i], 0);
2145 /* Only once the interfaces are sync started */
2146 interfaces->iface[i]->need_to_start_in_sync = 0;
2147 }
2148 }
2149
2150 return 0;
2151}
2152
2153
2154/**
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002155 * hostapd_setup_interface - Setup of an interface
2156 * @iface: Pointer to interface data.
2157 * Returns: 0 on success, -1 on failure
2158 *
2159 * Initializes the driver interface, validates the configuration,
2160 * and sets driver parameters based on the configuration.
2161 * Flushes old stations, sets the channel, encryption,
2162 * beacons, and WDS links based on the configuration.
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002163 *
2164 * If interface setup requires more time, e.g., to perform HT co-ex scans, ACS,
2165 * or DFS operations, this function returns 0 before such operations have been
2166 * completed. The pending operations are registered into eloop and will be
2167 * completed from eloop callbacks. Those callbacks end up calling
2168 * hostapd_setup_interface_complete() once setup has been completed.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002169 */
2170int hostapd_setup_interface(struct hostapd_iface *iface)
2171{
2172 int ret;
2173
2174 ret = setup_interface(iface);
2175 if (ret) {
2176 wpa_printf(MSG_ERROR, "%s: Unable to setup interface.",
2177 iface->bss[0]->conf->iface);
2178 return -1;
2179 }
2180
2181 return 0;
2182}
2183
2184
2185/**
2186 * hostapd_alloc_bss_data - Allocate and initialize per-BSS data
2187 * @hapd_iface: Pointer to interface data
2188 * @conf: Pointer to per-interface configuration
2189 * @bss: Pointer to per-BSS configuration for this BSS
2190 * Returns: Pointer to allocated BSS data
2191 *
2192 * This function is used to allocate per-BSS data structure. This data will be
2193 * freed after hostapd_cleanup() is called for it during interface
2194 * deinitialization.
2195 */
2196struct hostapd_data *
2197hostapd_alloc_bss_data(struct hostapd_iface *hapd_iface,
2198 struct hostapd_config *conf,
2199 struct hostapd_bss_config *bss)
2200{
2201 struct hostapd_data *hapd;
2202
2203 hapd = os_zalloc(sizeof(*hapd));
2204 if (hapd == NULL)
2205 return NULL;
2206
2207 hapd->new_assoc_sta_cb = hostapd_new_assoc_sta;
2208 hapd->iconf = conf;
2209 hapd->conf = bss;
2210 hapd->iface = hapd_iface;
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08002211 if (conf)
2212 hapd->driver = conf->driver;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002213 hapd->ctrl_sock = -1;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08002214 dl_list_init(&hapd->ctrl_dst);
Dmitry Shmidt7d175302016-09-06 13:11:34 -07002215 dl_list_init(&hapd->nr_db);
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08002216 hapd->dhcp_sock = -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002217#ifdef CONFIG_IEEE80211R_AP
2218 dl_list_init(&hapd->l2_queue);
2219 dl_list_init(&hapd->l2_oui_queue);
2220#endif /* CONFIG_IEEE80211R_AP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002221
2222 return hapd;
2223}
2224
2225
Dmitry Shmidt54605472013-11-08 11:10:19 -08002226static void hostapd_bss_deinit(struct hostapd_data *hapd)
2227{
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07002228 if (!hapd)
2229 return;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002230 wpa_printf(MSG_DEBUG, "%s: deinit bss %s", __func__,
2231 hapd->conf->iface);
Dmitry Shmidt71757432014-06-02 13:50:35 -07002232 hostapd_bss_deinit_no_free(hapd);
Dmitry Shmidtf73259c2015-03-17 11:00:54 -07002233 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
Dmitry Shmidt54605472013-11-08 11:10:19 -08002234 hostapd_cleanup(hapd);
2235}
2236
2237
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002238void hostapd_interface_deinit(struct hostapd_iface *iface)
2239{
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002240 int j;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002241
Dmitry Shmidt54605472013-11-08 11:10:19 -08002242 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002243 if (iface == NULL)
2244 return;
2245
Dmitry Shmidtf73259c2015-03-17 11:00:54 -07002246 hostapd_set_state(iface, HAPD_IFACE_DISABLED);
2247
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002248 eloop_cancel_timeout(channel_list_update_timeout, iface, NULL);
2249 iface->wait_channel_update = 0;
2250
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002251#ifdef CONFIG_FST
2252 if (iface->fst) {
2253 fst_detach(iface->fst);
2254 iface->fst = NULL;
2255 }
2256#endif /* CONFIG_FST */
2257
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07002258 for (j = iface->num_bss - 1; j >= 0; j--) {
2259 if (!iface->bss)
2260 break;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002261 hostapd_bss_deinit(iface->bss[j]);
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07002262 }
Roshan Pius3a1667e2018-07-03 15:17:14 -07002263
2264#ifdef CONFIG_IEEE80211N
2265#ifdef NEED_AP_MLME
2266 hostapd_stop_setup_timers(iface);
2267 eloop_cancel_timeout(ap_ht2040_timeout, iface, NULL);
2268#endif /* NEED_AP_MLME */
2269#endif /* CONFIG_IEEE80211N */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002270}
2271
2272
2273void hostapd_interface_free(struct hostapd_iface *iface)
2274{
2275 size_t j;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002276 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
2277 for (j = 0; j < iface->num_bss; j++) {
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07002278 if (!iface->bss)
2279 break;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002280 wpa_printf(MSG_DEBUG, "%s: free hapd %p",
2281 __func__, iface->bss[j]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002282 os_free(iface->bss[j]);
Dmitry Shmidt54605472013-11-08 11:10:19 -08002283 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002284 hostapd_cleanup_iface(iface);
2285}
2286
2287
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07002288struct hostapd_iface * hostapd_alloc_iface(void)
2289{
2290 struct hostapd_iface *hapd_iface;
2291
2292 hapd_iface = os_zalloc(sizeof(*hapd_iface));
2293 if (!hapd_iface)
2294 return NULL;
2295
2296 dl_list_init(&hapd_iface->sta_seen);
2297
2298 return hapd_iface;
2299}
2300
2301
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002302/**
2303 * hostapd_init - Allocate and initialize per-interface data
2304 * @config_file: Path to the configuration file
2305 * Returns: Pointer to the allocated interface data or %NULL on failure
2306 *
2307 * This function is used to allocate main data structures for per-interface
2308 * data. The allocated data buffer will be freed by calling
2309 * hostapd_cleanup_iface().
2310 */
2311struct hostapd_iface * hostapd_init(struct hapd_interfaces *interfaces,
2312 const char *config_file)
2313{
2314 struct hostapd_iface *hapd_iface = NULL;
2315 struct hostapd_config *conf = NULL;
2316 struct hostapd_data *hapd;
2317 size_t i;
2318
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07002319 hapd_iface = hostapd_alloc_iface();
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002320 if (hapd_iface == NULL)
2321 goto fail;
2322
2323 hapd_iface->config_fname = os_strdup(config_file);
2324 if (hapd_iface->config_fname == NULL)
2325 goto fail;
2326
2327 conf = interfaces->config_read_cb(hapd_iface->config_fname);
2328 if (conf == NULL)
2329 goto fail;
2330 hapd_iface->conf = conf;
2331
2332 hapd_iface->num_bss = conf->num_bss;
2333 hapd_iface->bss = os_calloc(conf->num_bss,
2334 sizeof(struct hostapd_data *));
2335 if (hapd_iface->bss == NULL)
2336 goto fail;
2337
2338 for (i = 0; i < conf->num_bss; i++) {
2339 hapd = hapd_iface->bss[i] =
2340 hostapd_alloc_bss_data(hapd_iface, conf,
2341 conf->bss[i]);
2342 if (hapd == NULL)
2343 goto fail;
2344 hapd->msg_ctx = hapd;
2345 }
2346
2347 return hapd_iface;
2348
2349fail:
2350 wpa_printf(MSG_ERROR, "Failed to set up interface with %s",
2351 config_file);
2352 if (conf)
2353 hostapd_config_free(conf);
2354 if (hapd_iface) {
2355 os_free(hapd_iface->config_fname);
2356 os_free(hapd_iface->bss);
Dmitry Shmidt54605472013-11-08 11:10:19 -08002357 wpa_printf(MSG_DEBUG, "%s: free iface %p",
2358 __func__, hapd_iface);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002359 os_free(hapd_iface);
2360 }
2361 return NULL;
2362}
2363
2364
2365static int ifname_in_use(struct hapd_interfaces *interfaces, const char *ifname)
2366{
2367 size_t i, j;
2368
2369 for (i = 0; i < interfaces->count; i++) {
2370 struct hostapd_iface *iface = interfaces->iface[i];
2371 for (j = 0; j < iface->num_bss; j++) {
2372 struct hostapd_data *hapd = iface->bss[j];
2373 if (os_strcmp(ifname, hapd->conf->iface) == 0)
2374 return 1;
2375 }
2376 }
2377
2378 return 0;
2379}
2380
2381
2382/**
2383 * hostapd_interface_init_bss - Read configuration file and init BSS data
2384 *
2385 * This function is used to parse configuration file for a BSS. This BSS is
2386 * added to an existing interface sharing the same radio (if any) or a new
2387 * interface is created if this is the first interface on a radio. This
2388 * allocate memory for the BSS. No actual driver operations are started.
2389 *
2390 * This is similar to hostapd_interface_init(), but for a case where the
2391 * configuration is used to add a single BSS instead of all BSSes for a radio.
2392 */
2393struct hostapd_iface *
2394hostapd_interface_init_bss(struct hapd_interfaces *interfaces, const char *phy,
2395 const char *config_fname, int debug)
2396{
2397 struct hostapd_iface *new_iface = NULL, *iface = NULL;
2398 struct hostapd_data *hapd;
2399 int k;
2400 size_t i, bss_idx;
2401
2402 if (!phy || !*phy)
2403 return NULL;
2404
2405 for (i = 0; i < interfaces->count; i++) {
2406 if (os_strcmp(interfaces->iface[i]->phy, phy) == 0) {
2407 iface = interfaces->iface[i];
2408 break;
2409 }
2410 }
2411
2412 wpa_printf(MSG_INFO, "Configuration file: %s (phy %s)%s",
2413 config_fname, phy, iface ? "" : " --> new PHY");
2414 if (iface) {
2415 struct hostapd_config *conf;
2416 struct hostapd_bss_config **tmp_conf;
2417 struct hostapd_data **tmp_bss;
2418 struct hostapd_bss_config *bss;
2419 const char *ifname;
2420
2421 /* Add new BSS to existing iface */
2422 conf = interfaces->config_read_cb(config_fname);
2423 if (conf == NULL)
2424 return NULL;
2425 if (conf->num_bss > 1) {
2426 wpa_printf(MSG_ERROR, "Multiple BSSes specified in BSS-config");
2427 hostapd_config_free(conf);
2428 return NULL;
2429 }
2430
2431 ifname = conf->bss[0]->iface;
2432 if (ifname[0] != '\0' && ifname_in_use(interfaces, ifname)) {
2433 wpa_printf(MSG_ERROR,
2434 "Interface name %s already in use", ifname);
2435 hostapd_config_free(conf);
2436 return NULL;
2437 }
2438
2439 tmp_conf = os_realloc_array(
2440 iface->conf->bss, iface->conf->num_bss + 1,
2441 sizeof(struct hostapd_bss_config *));
2442 tmp_bss = os_realloc_array(iface->bss, iface->num_bss + 1,
2443 sizeof(struct hostapd_data *));
2444 if (tmp_bss)
2445 iface->bss = tmp_bss;
2446 if (tmp_conf) {
2447 iface->conf->bss = tmp_conf;
2448 iface->conf->last_bss = tmp_conf[0];
2449 }
2450 if (tmp_bss == NULL || tmp_conf == NULL) {
2451 hostapd_config_free(conf);
2452 return NULL;
2453 }
2454 bss = iface->conf->bss[iface->conf->num_bss] = conf->bss[0];
2455 iface->conf->num_bss++;
2456
2457 hapd = hostapd_alloc_bss_data(iface, iface->conf, bss);
2458 if (hapd == NULL) {
2459 iface->conf->num_bss--;
2460 hostapd_config_free(conf);
2461 return NULL;
2462 }
2463 iface->conf->last_bss = bss;
2464 iface->bss[iface->num_bss] = hapd;
2465 hapd->msg_ctx = hapd;
2466
2467 bss_idx = iface->num_bss++;
2468 conf->num_bss--;
2469 conf->bss[0] = NULL;
2470 hostapd_config_free(conf);
2471 } else {
2472 /* Add a new iface with the first BSS */
2473 new_iface = iface = hostapd_init(interfaces, config_fname);
2474 if (!iface)
2475 return NULL;
2476 os_strlcpy(iface->phy, phy, sizeof(iface->phy));
2477 iface->interfaces = interfaces;
2478 bss_idx = 0;
2479 }
2480
2481 for (k = 0; k < debug; k++) {
2482 if (iface->bss[bss_idx]->conf->logger_stdout_level > 0)
2483 iface->bss[bss_idx]->conf->logger_stdout_level--;
2484 }
2485
2486 if (iface->conf->bss[bss_idx]->iface[0] == '\0' &&
2487 !hostapd_drv_none(iface->bss[bss_idx])) {
2488 wpa_printf(MSG_ERROR, "Interface name not specified in %s",
2489 config_fname);
2490 if (new_iface)
2491 hostapd_interface_deinit_free(new_iface);
2492 return NULL;
2493 }
2494
2495 return iface;
2496}
2497
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002498
2499void hostapd_interface_deinit_free(struct hostapd_iface *iface)
2500{
2501 const struct wpa_driver_ops *driver;
2502 void *drv_priv;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002503
2504 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002505 if (iface == NULL)
2506 return;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002507 wpa_printf(MSG_DEBUG, "%s: num_bss=%u conf->num_bss=%u",
2508 __func__, (unsigned int) iface->num_bss,
2509 (unsigned int) iface->conf->num_bss);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002510 driver = iface->bss[0]->driver;
2511 drv_priv = iface->bss[0]->drv_priv;
2512 hostapd_interface_deinit(iface);
Dmitry Shmidt54605472013-11-08 11:10:19 -08002513 wpa_printf(MSG_DEBUG, "%s: driver=%p drv_priv=%p -> hapd_deinit",
2514 __func__, driver, drv_priv);
Dmitry Shmidt71757432014-06-02 13:50:35 -07002515 if (driver && driver->hapd_deinit && drv_priv) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002516 driver->hapd_deinit(drv_priv);
Dmitry Shmidt71757432014-06-02 13:50:35 -07002517 iface->bss[0]->drv_priv = NULL;
2518 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002519 hostapd_interface_free(iface);
2520}
2521
2522
Dmitry Shmidt15907092014-03-25 10:42:57 -07002523static void hostapd_deinit_driver(const struct wpa_driver_ops *driver,
2524 void *drv_priv,
2525 struct hostapd_iface *hapd_iface)
2526{
2527 size_t j;
2528
2529 wpa_printf(MSG_DEBUG, "%s: driver=%p drv_priv=%p -> hapd_deinit",
2530 __func__, driver, drv_priv);
2531 if (driver && driver->hapd_deinit && drv_priv) {
2532 driver->hapd_deinit(drv_priv);
2533 for (j = 0; j < hapd_iface->num_bss; j++) {
2534 wpa_printf(MSG_DEBUG, "%s:bss[%d]->drv_priv=%p",
2535 __func__, (int) j,
2536 hapd_iface->bss[j]->drv_priv);
2537 if (hapd_iface->bss[j]->drv_priv == drv_priv)
2538 hapd_iface->bss[j]->drv_priv = NULL;
2539 }
2540 }
2541}
2542
2543
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002544int hostapd_enable_iface(struct hostapd_iface *hapd_iface)
2545{
Dmitry Shmidt71757432014-06-02 13:50:35 -07002546 size_t j;
2547
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002548 if (hapd_iface->bss[0]->drv_priv != NULL) {
2549 wpa_printf(MSG_ERROR, "Interface %s already enabled",
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002550 hapd_iface->conf->bss[0]->iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002551 return -1;
2552 }
2553
2554 wpa_printf(MSG_DEBUG, "Enable interface %s",
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002555 hapd_iface->conf->bss[0]->iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002556
Dmitry Shmidt71757432014-06-02 13:50:35 -07002557 for (j = 0; j < hapd_iface->num_bss; j++)
2558 hostapd_set_security_params(hapd_iface->conf->bss[j], 1);
Dmitry Shmidt344abd32014-01-14 13:17:00 -08002559 if (hostapd_config_check(hapd_iface->conf, 1) < 0) {
2560 wpa_printf(MSG_INFO, "Invalid configuration - cannot enable");
2561 return -1;
2562 }
2563
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002564 if (hapd_iface->interfaces == NULL ||
2565 hapd_iface->interfaces->driver_init == NULL ||
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002566 hapd_iface->interfaces->driver_init(hapd_iface))
2567 return -1;
2568
2569 if (hostapd_setup_interface(hapd_iface)) {
Dmitry Shmidt15907092014-03-25 10:42:57 -07002570 hostapd_deinit_driver(hapd_iface->bss[0]->driver,
2571 hapd_iface->bss[0]->drv_priv,
2572 hapd_iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002573 return -1;
2574 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002575
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002576 return 0;
2577}
2578
2579
2580int hostapd_reload_iface(struct hostapd_iface *hapd_iface)
2581{
2582 size_t j;
2583
2584 wpa_printf(MSG_DEBUG, "Reload interface %s",
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002585 hapd_iface->conf->bss[0]->iface);
2586 for (j = 0; j < hapd_iface->num_bss; j++)
Dmitry Shmidt71757432014-06-02 13:50:35 -07002587 hostapd_set_security_params(hapd_iface->conf->bss[j], 1);
Dmitry Shmidt344abd32014-01-14 13:17:00 -08002588 if (hostapd_config_check(hapd_iface->conf, 1) < 0) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002589 wpa_printf(MSG_ERROR, "Updated configuration is invalid");
2590 return -1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002591 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002592 hostapd_clear_old(hapd_iface);
2593 for (j = 0; j < hapd_iface->num_bss; j++)
2594 hostapd_reload_bss(hapd_iface->bss[j]);
2595
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002596 return 0;
2597}
2598
2599
2600int hostapd_disable_iface(struct hostapd_iface *hapd_iface)
2601{
2602 size_t j;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002603 const struct wpa_driver_ops *driver;
2604 void *drv_priv;
2605
2606 if (hapd_iface == NULL)
2607 return -1;
Dmitry Shmidt71757432014-06-02 13:50:35 -07002608
2609 if (hapd_iface->bss[0]->drv_priv == NULL) {
2610 wpa_printf(MSG_INFO, "Interface %s already disabled",
2611 hapd_iface->conf->bss[0]->iface);
2612 return -1;
2613 }
2614
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002615 wpa_msg(hapd_iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002616 driver = hapd_iface->bss[0]->driver;
2617 drv_priv = hapd_iface->bss[0]->drv_priv;
2618
Dmitry Shmidta38abf92014-03-06 13:38:44 -08002619 hapd_iface->driver_ap_teardown =
2620 !!(hapd_iface->drv_flags &
2621 WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
2622
Hai Shalom39ba6fc2019-01-22 12:40:38 -08002623#ifdef NEED_AP_MLME
2624 for (j = 0; j < hapd_iface->num_bss; j++)
2625 hostapd_cleanup_cs_params(hapd_iface->bss[j]);
2626#endif /* NEED_AP_MLME */
2627
Dmitry Shmidta38abf92014-03-06 13:38:44 -08002628 /* same as hostapd_interface_deinit without deinitializing ctrl-iface */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002629 for (j = 0; j < hapd_iface->num_bss; j++) {
2630 struct hostapd_data *hapd = hapd_iface->bss[j];
Dmitry Shmidt71757432014-06-02 13:50:35 -07002631 hostapd_bss_deinit_no_free(hapd);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002632 hostapd_free_hapd_data(hapd);
2633 }
2634
Dmitry Shmidt15907092014-03-25 10:42:57 -07002635 hostapd_deinit_driver(driver, drv_priv, hapd_iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002636
2637 /* From hostapd_cleanup_iface: These were initialized in
2638 * hostapd_setup_interface and hostapd_setup_interface_complete
2639 */
2640 hostapd_cleanup_iface_partial(hapd_iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002641
Dmitry Shmidt56052862013-10-04 10:23:25 -07002642 wpa_printf(MSG_DEBUG, "Interface %s disabled",
2643 hapd_iface->bss[0]->conf->iface);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002644 hostapd_set_state(hapd_iface, HAPD_IFACE_DISABLED);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002645 return 0;
2646}
2647
2648
2649static struct hostapd_iface *
2650hostapd_iface_alloc(struct hapd_interfaces *interfaces)
2651{
2652 struct hostapd_iface **iface, *hapd_iface;
2653
2654 iface = os_realloc_array(interfaces->iface, interfaces->count + 1,
2655 sizeof(struct hostapd_iface *));
2656 if (iface == NULL)
2657 return NULL;
2658 interfaces->iface = iface;
2659 hapd_iface = interfaces->iface[interfaces->count] =
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07002660 hostapd_alloc_iface();
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002661 if (hapd_iface == NULL) {
2662 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory for "
2663 "the interface", __func__);
2664 return NULL;
2665 }
2666 interfaces->count++;
2667 hapd_iface->interfaces = interfaces;
2668
2669 return hapd_iface;
2670}
2671
2672
2673static struct hostapd_config *
2674hostapd_config_alloc(struct hapd_interfaces *interfaces, const char *ifname,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002675 const char *ctrl_iface, const char *driver)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002676{
2677 struct hostapd_bss_config *bss;
2678 struct hostapd_config *conf;
2679
2680 /* Allocates memory for bss and conf */
2681 conf = hostapd_config_defaults();
2682 if (conf == NULL) {
2683 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory for "
2684 "configuration", __func__);
2685 return NULL;
2686 }
2687
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002688 if (driver) {
2689 int j;
2690
2691 for (j = 0; wpa_drivers[j]; j++) {
2692 if (os_strcmp(driver, wpa_drivers[j]->name) == 0) {
2693 conf->driver = wpa_drivers[j];
2694 goto skip;
2695 }
2696 }
2697
2698 wpa_printf(MSG_ERROR,
2699 "Invalid/unknown driver '%s' - registering the default driver",
2700 driver);
2701 }
2702
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002703 conf->driver = wpa_drivers[0];
2704 if (conf->driver == NULL) {
2705 wpa_printf(MSG_ERROR, "No driver wrappers registered!");
2706 hostapd_config_free(conf);
2707 return NULL;
2708 }
2709
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002710skip:
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002711 bss = conf->last_bss = conf->bss[0];
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002712
2713 os_strlcpy(bss->iface, ifname, sizeof(bss->iface));
2714 bss->ctrl_interface = os_strdup(ctrl_iface);
2715 if (bss->ctrl_interface == NULL) {
2716 hostapd_config_free(conf);
2717 return NULL;
2718 }
2719
2720 /* Reading configuration file skipped, will be done in SET!
2721 * From reading the configuration till the end has to be done in
2722 * SET
2723 */
2724 return conf;
2725}
2726
2727
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002728static int hostapd_data_alloc(struct hostapd_iface *hapd_iface,
2729 struct hostapd_config *conf)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002730{
2731 size_t i;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002732 struct hostapd_data *hapd;
2733
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002734 hapd_iface->bss = os_calloc(conf->num_bss,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002735 sizeof(struct hostapd_data *));
2736 if (hapd_iface->bss == NULL)
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002737 return -1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002738
2739 for (i = 0; i < conf->num_bss; i++) {
2740 hapd = hapd_iface->bss[i] =
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002741 hostapd_alloc_bss_data(hapd_iface, conf, conf->bss[i]);
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002742 if (hapd == NULL) {
2743 while (i > 0) {
2744 i--;
2745 os_free(hapd_iface->bss[i]);
2746 hapd_iface->bss[i] = NULL;
2747 }
2748 os_free(hapd_iface->bss);
2749 hapd_iface->bss = NULL;
2750 return -1;
2751 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002752 hapd->msg_ctx = hapd;
2753 }
2754
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002755 hapd_iface->conf = conf;
2756 hapd_iface->num_bss = conf->num_bss;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002757
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002758 return 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002759}
2760
2761
2762int hostapd_add_iface(struct hapd_interfaces *interfaces, char *buf)
2763{
2764 struct hostapd_config *conf = NULL;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002765 struct hostapd_iface *hapd_iface = NULL, *new_iface = NULL;
2766 struct hostapd_data *hapd;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002767 char *ptr;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002768 size_t i, j;
2769 const char *conf_file = NULL, *phy_name = NULL;
2770
2771 if (os_strncmp(buf, "bss_config=", 11) == 0) {
2772 char *pos;
2773 phy_name = buf + 11;
2774 pos = os_strchr(phy_name, ':');
2775 if (!pos)
2776 return -1;
2777 *pos++ = '\0';
2778 conf_file = pos;
2779 if (!os_strlen(conf_file))
2780 return -1;
2781
2782 hapd_iface = hostapd_interface_init_bss(interfaces, phy_name,
2783 conf_file, 0);
2784 if (!hapd_iface)
2785 return -1;
2786 for (j = 0; j < interfaces->count; j++) {
2787 if (interfaces->iface[j] == hapd_iface)
2788 break;
2789 }
2790 if (j == interfaces->count) {
2791 struct hostapd_iface **tmp;
2792 tmp = os_realloc_array(interfaces->iface,
2793 interfaces->count + 1,
2794 sizeof(struct hostapd_iface *));
2795 if (!tmp) {
2796 hostapd_interface_deinit_free(hapd_iface);
2797 return -1;
2798 }
2799 interfaces->iface = tmp;
2800 interfaces->iface[interfaces->count++] = hapd_iface;
2801 new_iface = hapd_iface;
2802 }
2803
2804 if (new_iface) {
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002805 if (interfaces->driver_init(hapd_iface))
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002806 goto fail;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002807
2808 if (hostapd_setup_interface(hapd_iface)) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002809 hostapd_deinit_driver(
2810 hapd_iface->bss[0]->driver,
2811 hapd_iface->bss[0]->drv_priv,
2812 hapd_iface);
2813 goto fail;
2814 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002815 } else {
2816 /* Assign new BSS with bss[0]'s driver info */
2817 hapd = hapd_iface->bss[hapd_iface->num_bss - 1];
2818 hapd->driver = hapd_iface->bss[0]->driver;
2819 hapd->drv_priv = hapd_iface->bss[0]->drv_priv;
2820 os_memcpy(hapd->own_addr, hapd_iface->bss[0]->own_addr,
2821 ETH_ALEN);
2822
2823 if (start_ctrl_iface_bss(hapd) < 0 ||
Dmitry Shmidt54605472013-11-08 11:10:19 -08002824 (hapd_iface->state == HAPD_IFACE_ENABLED &&
2825 hostapd_setup_bss(hapd, -1))) {
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002826 hostapd_cleanup(hapd);
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08002827 hapd_iface->bss[hapd_iface->num_bss - 1] = NULL;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002828 hapd_iface->conf->num_bss--;
2829 hapd_iface->num_bss--;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002830 wpa_printf(MSG_DEBUG, "%s: free hapd %p %s",
2831 __func__, hapd, hapd->conf->iface);
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002832 hostapd_config_free_bss(hapd->conf);
2833 hapd->conf = NULL;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002834 os_free(hapd);
2835 return -1;
2836 }
2837 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002838 hostapd_owe_update_trans(hapd_iface);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002839 return 0;
2840 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002841
2842 ptr = os_strchr(buf, ' ');
2843 if (ptr == NULL)
2844 return -1;
2845 *ptr++ = '\0';
2846
Dmitry Shmidt56052862013-10-04 10:23:25 -07002847 if (os_strncmp(ptr, "config=", 7) == 0)
2848 conf_file = ptr + 7;
2849
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002850 for (i = 0; i < interfaces->count; i++) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002851 if (!os_strcmp(interfaces->iface[i]->conf->bss[0]->iface,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002852 buf)) {
2853 wpa_printf(MSG_INFO, "Cannot add interface - it "
2854 "already exists");
2855 return -1;
2856 }
2857 }
2858
2859 hapd_iface = hostapd_iface_alloc(interfaces);
2860 if (hapd_iface == NULL) {
2861 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
2862 "for interface", __func__);
2863 goto fail;
2864 }
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002865 new_iface = hapd_iface;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002866
Dmitry Shmidt56052862013-10-04 10:23:25 -07002867 if (conf_file && interfaces->config_read_cb) {
2868 conf = interfaces->config_read_cb(conf_file);
2869 if (conf && conf->bss)
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002870 os_strlcpy(conf->bss[0]->iface, buf,
2871 sizeof(conf->bss[0]->iface));
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002872 } else {
2873 char *driver = os_strchr(ptr, ' ');
2874
2875 if (driver)
2876 *driver++ = '\0';
2877 conf = hostapd_config_alloc(interfaces, buf, ptr, driver);
2878 }
2879
Dmitry Shmidt56052862013-10-04 10:23:25 -07002880 if (conf == NULL || conf->bss == NULL) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002881 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
2882 "for configuration", __func__);
2883 goto fail;
2884 }
2885
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002886 if (hostapd_data_alloc(hapd_iface, conf) < 0) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002887 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
2888 "for hostapd", __func__);
2889 goto fail;
2890 }
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002891 conf = NULL;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002892
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002893 if (start_ctrl_iface(hapd_iface) < 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002894 goto fail;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002895
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002896 wpa_printf(MSG_INFO, "Add interface '%s'",
2897 hapd_iface->conf->bss[0]->iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002898
2899 return 0;
2900
2901fail:
2902 if (conf)
2903 hostapd_config_free(conf);
2904 if (hapd_iface) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002905 if (hapd_iface->bss) {
Dmitry Shmidt54605472013-11-08 11:10:19 -08002906 for (i = 0; i < hapd_iface->num_bss; i++) {
2907 hapd = hapd_iface->bss[i];
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08002908 if (!hapd)
2909 continue;
2910 if (hapd_iface->interfaces &&
Dmitry Shmidt54605472013-11-08 11:10:19 -08002911 hapd_iface->interfaces->ctrl_iface_deinit)
2912 hapd_iface->interfaces->
2913 ctrl_iface_deinit(hapd);
2914 wpa_printf(MSG_DEBUG, "%s: free hapd %p (%s)",
2915 __func__, hapd_iface->bss[i],
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08002916 hapd->conf->iface);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002917 hostapd_cleanup(hapd);
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08002918 os_free(hapd);
2919 hapd_iface->bss[i] = NULL;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002920 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002921 os_free(hapd_iface->bss);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002922 hapd_iface->bss = NULL;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002923 }
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002924 if (new_iface) {
2925 interfaces->count--;
2926 interfaces->iface[interfaces->count] = NULL;
2927 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002928 hostapd_cleanup_iface(hapd_iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002929 }
2930 return -1;
2931}
2932
2933
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002934static int hostapd_remove_bss(struct hostapd_iface *iface, unsigned int idx)
2935{
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002936 size_t i;
2937
Dmitry Shmidt54605472013-11-08 11:10:19 -08002938 wpa_printf(MSG_INFO, "Remove BSS '%s'", iface->conf->bss[idx]->iface);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002939
Dmitry Shmidt54605472013-11-08 11:10:19 -08002940 /* Remove hostapd_data only if it has already been initialized */
2941 if (idx < iface->num_bss) {
2942 struct hostapd_data *hapd = iface->bss[idx];
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002943
Dmitry Shmidt54605472013-11-08 11:10:19 -08002944 hostapd_bss_deinit(hapd);
2945 wpa_printf(MSG_DEBUG, "%s: free hapd %p (%s)",
2946 __func__, hapd, hapd->conf->iface);
2947 hostapd_config_free_bss(hapd->conf);
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002948 hapd->conf = NULL;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002949 os_free(hapd);
2950
2951 iface->num_bss--;
2952
2953 for (i = idx; i < iface->num_bss; i++)
2954 iface->bss[i] = iface->bss[i + 1];
2955 } else {
2956 hostapd_config_free_bss(iface->conf->bss[idx]);
2957 iface->conf->bss[idx] = NULL;
2958 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002959
2960 iface->conf->num_bss--;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002961 for (i = idx; i < iface->conf->num_bss; i++)
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002962 iface->conf->bss[i] = iface->conf->bss[i + 1];
2963
2964 return 0;
2965}
2966
2967
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002968int hostapd_remove_iface(struct hapd_interfaces *interfaces, char *buf)
2969{
2970 struct hostapd_iface *hapd_iface;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002971 size_t i, j, k = 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002972
2973 for (i = 0; i < interfaces->count; i++) {
2974 hapd_iface = interfaces->iface[i];
2975 if (hapd_iface == NULL)
2976 return -1;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002977 if (!os_strcmp(hapd_iface->conf->bss[0]->iface, buf)) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002978 wpa_printf(MSG_INFO, "Remove interface '%s'", buf);
Dmitry Shmidta38abf92014-03-06 13:38:44 -08002979 hapd_iface->driver_ap_teardown =
2980 !!(hapd_iface->drv_flags &
2981 WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
2982
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002983 hostapd_interface_deinit_free(hapd_iface);
2984 k = i;
2985 while (k < (interfaces->count - 1)) {
2986 interfaces->iface[k] =
2987 interfaces->iface[k + 1];
2988 k++;
2989 }
2990 interfaces->count--;
2991 return 0;
2992 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002993
2994 for (j = 0; j < hapd_iface->conf->num_bss; j++) {
Dmitry Shmidta38abf92014-03-06 13:38:44 -08002995 if (!os_strcmp(hapd_iface->conf->bss[j]->iface, buf)) {
2996 hapd_iface->driver_ap_teardown =
2997 !(hapd_iface->drv_flags &
2998 WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002999 return hostapd_remove_bss(hapd_iface, j);
Dmitry Shmidta38abf92014-03-06 13:38:44 -08003000 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003001 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003002 }
3003 return -1;
3004}
3005
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003006
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003007/**
3008 * hostapd_new_assoc_sta - Notify that a new station associated with the AP
3009 * @hapd: Pointer to BSS data
3010 * @sta: Pointer to the associated STA data
3011 * @reassoc: 1 to indicate this was a re-association; 0 = first association
3012 *
3013 * This function will be called whenever a station associates with the AP. It
3014 * can be called from ieee802_11.c for drivers that export MLME to hostapd and
3015 * from drv_callbacks.c based on driver events for drivers that take care of
3016 * management frames (IEEE 802.11 authentication and association) internally.
3017 */
3018void hostapd_new_assoc_sta(struct hostapd_data *hapd, struct sta_info *sta,
3019 int reassoc)
3020{
3021 if (hapd->tkip_countermeasures) {
3022 hostapd_drv_sta_deauth(hapd, sta->addr,
3023 WLAN_REASON_MICHAEL_MIC_FAILURE);
3024 return;
3025 }
3026
3027 hostapd_prune_associations(hapd, sta->addr);
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -08003028 ap_sta_clear_disconnect_timeouts(hapd, sta);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003029
3030 /* IEEE 802.11F (IAPP) */
3031 if (hapd->conf->ieee802_11f)
3032 iapp_new_station(hapd->iapp, sta);
3033
3034#ifdef CONFIG_P2P
3035 if (sta->p2p_ie == NULL && !sta->no_p2p_set) {
3036 sta->no_p2p_set = 1;
3037 hapd->num_sta_no_p2p++;
3038 if (hapd->num_sta_no_p2p == 1)
3039 hostapd_p2p_non_p2p_sta_connected(hapd);
3040 }
3041#endif /* CONFIG_P2P */
3042
3043 /* Start accounting here, if IEEE 802.1X and WPA are not used.
3044 * IEEE 802.1X/WPA code will start accounting after the station has
3045 * been authorized. */
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -08003046 if (!hapd->conf->ieee802_1x && !hapd->conf->wpa && !hapd->conf->osen) {
3047 ap_sta_set_authorized(hapd, sta, 1);
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08003048 os_get_reltime(&sta->connected_time);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003049 accounting_sta_start(hapd, sta);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003050 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003051
3052 /* Start IEEE 802.1X authentication process for new stations */
3053 ieee802_1x_new_station(hapd, sta);
3054 if (reassoc) {
3055 if (sta->auth_alg != WLAN_AUTH_FT &&
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003056 sta->auth_alg != WLAN_AUTH_FILS_SK &&
3057 sta->auth_alg != WLAN_AUTH_FILS_SK_PFS &&
3058 sta->auth_alg != WLAN_AUTH_FILS_PK &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003059 !(sta->flags & (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS)))
3060 wpa_auth_sm_event(sta->wpa_sm, WPA_REAUTH);
3061 } else
3062 wpa_auth_sta_associated(hapd->wpa_auth, sta->wpa_sm);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003063
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08003064 if (hapd->iface->drv_flags & WPA_DRIVER_FLAGS_WIRED) {
3065 if (eloop_cancel_timeout(ap_handle_timer, hapd, sta) > 0) {
3066 wpa_printf(MSG_DEBUG,
3067 "%s: %s: canceled wired ap_handle_timer timeout for "
3068 MACSTR,
3069 hapd->conf->iface, __func__,
3070 MAC2STR(sta->addr));
3071 }
3072 } else if (!(hapd->iface->drv_flags &
3073 WPA_DRIVER_FLAGS_INACTIVITY_TIMER)) {
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -08003074 wpa_printf(MSG_DEBUG,
3075 "%s: %s: reschedule ap_handle_timer timeout for "
3076 MACSTR " (%d seconds - ap_max_inactivity)",
3077 hapd->conf->iface, __func__, MAC2STR(sta->addr),
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08003078 hapd->conf->ap_max_inactivity);
3079 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
3080 eloop_register_timeout(hapd->conf->ap_max_inactivity, 0,
3081 ap_handle_timer, hapd, sta);
3082 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003083}
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003084
3085
3086const char * hostapd_state_text(enum hostapd_iface_state s)
3087{
3088 switch (s) {
3089 case HAPD_IFACE_UNINITIALIZED:
3090 return "UNINITIALIZED";
3091 case HAPD_IFACE_DISABLED:
3092 return "DISABLED";
3093 case HAPD_IFACE_COUNTRY_UPDATE:
3094 return "COUNTRY_UPDATE";
3095 case HAPD_IFACE_ACS:
3096 return "ACS";
3097 case HAPD_IFACE_HT_SCAN:
3098 return "HT_SCAN";
3099 case HAPD_IFACE_DFS:
3100 return "DFS";
3101 case HAPD_IFACE_ENABLED:
3102 return "ENABLED";
3103 }
3104
3105 return "UNKNOWN";
3106}
3107
3108
3109void hostapd_set_state(struct hostapd_iface *iface, enum hostapd_iface_state s)
3110{
3111 wpa_printf(MSG_INFO, "%s: interface state %s->%s",
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07003112 iface->conf ? iface->conf->bss[0]->iface : "N/A",
3113 hostapd_state_text(iface->state), hostapd_state_text(s));
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003114 iface->state = s;
3115}
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003116
3117
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003118int hostapd_csa_in_progress(struct hostapd_iface *iface)
3119{
3120 unsigned int i;
3121
3122 for (i = 0; i < iface->num_bss; i++)
3123 if (iface->bss[i]->csa_in_progress)
3124 return 1;
3125 return 0;
3126}
3127
3128
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003129#ifdef NEED_AP_MLME
3130
3131static void free_beacon_data(struct beacon_data *beacon)
3132{
3133 os_free(beacon->head);
3134 beacon->head = NULL;
3135 os_free(beacon->tail);
3136 beacon->tail = NULL;
3137 os_free(beacon->probe_resp);
3138 beacon->probe_resp = NULL;
3139 os_free(beacon->beacon_ies);
3140 beacon->beacon_ies = NULL;
3141 os_free(beacon->proberesp_ies);
3142 beacon->proberesp_ies = NULL;
3143 os_free(beacon->assocresp_ies);
3144 beacon->assocresp_ies = NULL;
3145}
3146
3147
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003148static int hostapd_build_beacon_data(struct hostapd_data *hapd,
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003149 struct beacon_data *beacon)
3150{
3151 struct wpabuf *beacon_extra, *proberesp_extra, *assocresp_extra;
3152 struct wpa_driver_ap_params params;
3153 int ret;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003154
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08003155 os_memset(beacon, 0, sizeof(*beacon));
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003156 ret = ieee802_11_build_ap_params(hapd, &params);
3157 if (ret < 0)
3158 return ret;
3159
3160 ret = hostapd_build_ap_extra_ies(hapd, &beacon_extra,
3161 &proberesp_extra,
3162 &assocresp_extra);
3163 if (ret)
3164 goto free_ap_params;
3165
3166 ret = -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003167 beacon->head = os_memdup(params.head, params.head_len);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003168 if (!beacon->head)
3169 goto free_ap_extra_ies;
3170
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003171 beacon->head_len = params.head_len;
3172
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003173 beacon->tail = os_memdup(params.tail, params.tail_len);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003174 if (!beacon->tail)
3175 goto free_beacon;
3176
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003177 beacon->tail_len = params.tail_len;
3178
3179 if (params.proberesp != NULL) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003180 beacon->probe_resp = os_memdup(params.proberesp,
3181 params.proberesp_len);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003182 if (!beacon->probe_resp)
3183 goto free_beacon;
3184
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003185 beacon->probe_resp_len = params.proberesp_len;
3186 }
3187
3188 /* copy the extra ies */
3189 if (beacon_extra) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003190 beacon->beacon_ies = os_memdup(beacon_extra->buf,
3191 wpabuf_len(beacon_extra));
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003192 if (!beacon->beacon_ies)
3193 goto free_beacon;
3194
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003195 beacon->beacon_ies_len = wpabuf_len(beacon_extra);
3196 }
3197
3198 if (proberesp_extra) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003199 beacon->proberesp_ies = os_memdup(proberesp_extra->buf,
3200 wpabuf_len(proberesp_extra));
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003201 if (!beacon->proberesp_ies)
3202 goto free_beacon;
3203
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003204 beacon->proberesp_ies_len = wpabuf_len(proberesp_extra);
3205 }
3206
3207 if (assocresp_extra) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003208 beacon->assocresp_ies = os_memdup(assocresp_extra->buf,
3209 wpabuf_len(assocresp_extra));
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003210 if (!beacon->assocresp_ies)
3211 goto free_beacon;
3212
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003213 beacon->assocresp_ies_len = wpabuf_len(assocresp_extra);
3214 }
3215
3216 ret = 0;
3217free_beacon:
3218 /* if the function fails, the caller should not free beacon data */
3219 if (ret)
3220 free_beacon_data(beacon);
3221
3222free_ap_extra_ies:
3223 hostapd_free_ap_extra_ies(hapd, beacon_extra, proberesp_extra,
3224 assocresp_extra);
3225free_ap_params:
3226 ieee802_11_free_ap_params(&params);
3227 return ret;
3228}
3229
3230
3231/*
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003232 * TODO: This flow currently supports only changing channel and width within
3233 * the same hw_mode. Any other changes to MAC parameters or provided settings
3234 * are not supported.
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003235 */
3236static int hostapd_change_config_freq(struct hostapd_data *hapd,
3237 struct hostapd_config *conf,
3238 struct hostapd_freq_params *params,
3239 struct hostapd_freq_params *old_params)
3240{
3241 int channel;
3242
3243 if (!params->channel) {
3244 /* check if the new channel is supported by hw */
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003245 params->channel = hostapd_hw_get_channel(hapd, params->freq);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003246 }
3247
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003248 channel = params->channel;
3249 if (!channel)
3250 return -1;
3251
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003252 /* if a pointer to old_params is provided we save previous state */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003253 if (old_params &&
3254 hostapd_set_freq_params(old_params, conf->hw_mode,
3255 hostapd_hw_get_freq(hapd, conf->channel),
3256 conf->channel, conf->ieee80211n,
3257 conf->ieee80211ac,
3258 conf->secondary_channel,
3259 conf->vht_oper_chwidth,
3260 conf->vht_oper_centr_freq_seg0_idx,
3261 conf->vht_oper_centr_freq_seg1_idx,
3262 conf->vht_capab))
3263 return -1;
3264
3265 switch (params->bandwidth) {
3266 case 0:
3267 case 20:
3268 case 40:
3269 conf->vht_oper_chwidth = VHT_CHANWIDTH_USE_HT;
3270 break;
3271 case 80:
3272 if (params->center_freq2)
3273 conf->vht_oper_chwidth = VHT_CHANWIDTH_80P80MHZ;
3274 else
3275 conf->vht_oper_chwidth = VHT_CHANWIDTH_80MHZ;
3276 break;
3277 case 160:
3278 conf->vht_oper_chwidth = VHT_CHANWIDTH_160MHZ;
3279 break;
3280 default:
3281 return -1;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003282 }
3283
3284 conf->channel = channel;
3285 conf->ieee80211n = params->ht_enabled;
3286 conf->secondary_channel = params->sec_channel_offset;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003287 ieee80211_freq_to_chan(params->center_freq1,
3288 &conf->vht_oper_centr_freq_seg0_idx);
3289 ieee80211_freq_to_chan(params->center_freq2,
3290 &conf->vht_oper_centr_freq_seg1_idx);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003291
3292 /* TODO: maybe call here hostapd_config_check here? */
3293
3294 return 0;
3295}
3296
3297
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003298static int hostapd_fill_csa_settings(struct hostapd_data *hapd,
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003299 struct csa_settings *settings)
3300{
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003301 struct hostapd_iface *iface = hapd->iface;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003302 struct hostapd_freq_params old_freq;
3303 int ret;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003304 u8 chan, vht_bandwidth;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003305
3306 os_memset(&old_freq, 0, sizeof(old_freq));
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003307 if (!iface || !iface->freq || hapd->csa_in_progress)
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003308 return -1;
3309
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003310 switch (settings->freq_params.bandwidth) {
3311 case 80:
3312 if (settings->freq_params.center_freq2)
3313 vht_bandwidth = VHT_CHANWIDTH_80P80MHZ;
3314 else
3315 vht_bandwidth = VHT_CHANWIDTH_80MHZ;
3316 break;
3317 case 160:
3318 vht_bandwidth = VHT_CHANWIDTH_160MHZ;
3319 break;
3320 default:
3321 vht_bandwidth = VHT_CHANWIDTH_USE_HT;
3322 break;
3323 }
3324
3325 if (ieee80211_freq_to_channel_ext(
3326 settings->freq_params.freq,
3327 settings->freq_params.sec_channel_offset,
3328 vht_bandwidth,
3329 &hapd->iface->cs_oper_class,
3330 &chan) == NUM_HOSTAPD_MODES) {
3331 wpa_printf(MSG_DEBUG,
3332 "invalid frequency for channel switch (freq=%d, sec_channel_offset=%d, vht_enabled=%d)",
3333 settings->freq_params.freq,
3334 settings->freq_params.sec_channel_offset,
3335 settings->freq_params.vht_enabled);
3336 return -1;
3337 }
3338
3339 settings->freq_params.channel = chan;
3340
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003341 ret = hostapd_change_config_freq(iface->bss[0], iface->conf,
3342 &settings->freq_params,
3343 &old_freq);
3344 if (ret)
3345 return ret;
3346
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003347 ret = hostapd_build_beacon_data(hapd, &settings->beacon_after);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003348
3349 /* change back the configuration */
3350 hostapd_change_config_freq(iface->bss[0], iface->conf,
3351 &old_freq, NULL);
3352
3353 if (ret)
3354 return ret;
3355
3356 /* set channel switch parameters for csa ie */
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003357 hapd->cs_freq_params = settings->freq_params;
3358 hapd->cs_count = settings->cs_count;
3359 hapd->cs_block_tx = settings->block_tx;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003360
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003361 ret = hostapd_build_beacon_data(hapd, &settings->beacon_csa);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003362 if (ret) {
3363 free_beacon_data(&settings->beacon_after);
3364 return ret;
3365 }
3366
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003367 settings->counter_offset_beacon[0] = hapd->cs_c_off_beacon;
3368 settings->counter_offset_presp[0] = hapd->cs_c_off_proberesp;
3369 settings->counter_offset_beacon[1] = hapd->cs_c_off_ecsa_beacon;
3370 settings->counter_offset_presp[1] = hapd->cs_c_off_ecsa_proberesp;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003371
3372 return 0;
3373}
3374
3375
3376void hostapd_cleanup_cs_params(struct hostapd_data *hapd)
3377{
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003378 os_memset(&hapd->cs_freq_params, 0, sizeof(hapd->cs_freq_params));
3379 hapd->cs_count = 0;
3380 hapd->cs_block_tx = 0;
3381 hapd->cs_c_off_beacon = 0;
3382 hapd->cs_c_off_proberesp = 0;
3383 hapd->csa_in_progress = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003384 hapd->cs_c_off_ecsa_beacon = 0;
3385 hapd->cs_c_off_ecsa_proberesp = 0;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003386}
3387
3388
Roshan Pius3a1667e2018-07-03 15:17:14 -07003389void hostapd_chan_switch_vht_config(struct hostapd_data *hapd, int vht_enabled)
3390{
3391 if (vht_enabled)
3392 hapd->iconf->ch_switch_vht_config |= CH_SWITCH_VHT_ENABLED;
3393 else
3394 hapd->iconf->ch_switch_vht_config |= CH_SWITCH_VHT_DISABLED;
3395
3396 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
3397 HOSTAPD_LEVEL_INFO, "CHAN_SWITCH VHT CONFIG 0x%x",
3398 hapd->iconf->ch_switch_vht_config);
3399}
3400
3401
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003402int hostapd_switch_channel(struct hostapd_data *hapd,
3403 struct csa_settings *settings)
3404{
3405 int ret;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003406
3407 if (!(hapd->iface->drv_flags & WPA_DRIVER_FLAGS_AP_CSA)) {
3408 wpa_printf(MSG_INFO, "CSA is not supported");
3409 return -1;
3410 }
3411
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003412 ret = hostapd_fill_csa_settings(hapd, settings);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003413 if (ret)
3414 return ret;
3415
3416 ret = hostapd_drv_switch_channel(hapd, settings);
3417 free_beacon_data(&settings->beacon_csa);
3418 free_beacon_data(&settings->beacon_after);
3419
3420 if (ret) {
3421 /* if we failed, clean cs parameters */
3422 hostapd_cleanup_cs_params(hapd);
3423 return ret;
3424 }
3425
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003426 hapd->csa_in_progress = 1;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003427 return 0;
3428}
3429
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003430
3431void
3432hostapd_switch_channel_fallback(struct hostapd_iface *iface,
3433 const struct hostapd_freq_params *freq_params)
3434{
3435 int vht_seg0_idx = 0, vht_seg1_idx = 0, vht_bw = VHT_CHANWIDTH_USE_HT;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003436
3437 wpa_printf(MSG_DEBUG, "Restarting all CSA-related BSSes");
3438
3439 if (freq_params->center_freq1)
3440 vht_seg0_idx = 36 + (freq_params->center_freq1 - 5180) / 5;
3441 if (freq_params->center_freq2)
3442 vht_seg1_idx = 36 + (freq_params->center_freq2 - 5180) / 5;
3443
3444 switch (freq_params->bandwidth) {
3445 case 0:
3446 case 20:
3447 case 40:
3448 vht_bw = VHT_CHANWIDTH_USE_HT;
3449 break;
3450 case 80:
3451 if (freq_params->center_freq2)
3452 vht_bw = VHT_CHANWIDTH_80P80MHZ;
3453 else
3454 vht_bw = VHT_CHANWIDTH_80MHZ;
3455 break;
3456 case 160:
3457 vht_bw = VHT_CHANWIDTH_160MHZ;
3458 break;
3459 default:
3460 wpa_printf(MSG_WARNING, "Unknown CSA bandwidth: %d",
3461 freq_params->bandwidth);
3462 break;
3463 }
3464
3465 iface->freq = freq_params->freq;
3466 iface->conf->channel = freq_params->channel;
3467 iface->conf->secondary_channel = freq_params->sec_channel_offset;
3468 iface->conf->vht_oper_centr_freq_seg0_idx = vht_seg0_idx;
3469 iface->conf->vht_oper_centr_freq_seg1_idx = vht_seg1_idx;
3470 iface->conf->vht_oper_chwidth = vht_bw;
3471 iface->conf->ieee80211n = freq_params->ht_enabled;
3472 iface->conf->ieee80211ac = freq_params->vht_enabled;
3473
3474 /*
3475 * cs_params must not be cleared earlier because the freq_params
3476 * argument may actually point to one of these.
Hai Shalom39ba6fc2019-01-22 12:40:38 -08003477 * These params will be cleared during interface disable below.
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003478 */
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003479 hostapd_disable_iface(iface);
3480 hostapd_enable_iface(iface);
3481}
3482
Dmitry Shmidte4663042016-04-04 10:07:49 -07003483#endif /* NEED_AP_MLME */
3484
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003485
3486struct hostapd_data * hostapd_get_iface(struct hapd_interfaces *interfaces,
3487 const char *ifname)
3488{
3489 size_t i, j;
3490
3491 for (i = 0; i < interfaces->count; i++) {
3492 struct hostapd_iface *iface = interfaces->iface[i];
3493
3494 for (j = 0; j < iface->num_bss; j++) {
3495 struct hostapd_data *hapd = iface->bss[j];
3496
3497 if (os_strcmp(ifname, hapd->conf->iface) == 0)
3498 return hapd;
3499 }
3500 }
3501
3502 return NULL;
3503}
3504
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003505
3506void hostapd_periodic_iface(struct hostapd_iface *iface)
3507{
3508 size_t i;
3509
3510 ap_list_timer(iface);
3511
3512 for (i = 0; i < iface->num_bss; i++) {
3513 struct hostapd_data *hapd = iface->bss[i];
3514
3515 if (!hapd->started)
3516 continue;
3517
3518#ifndef CONFIG_NO_RADIUS
3519 hostapd_acl_expire(hapd);
3520#endif /* CONFIG_NO_RADIUS */
3521 }
3522}