blob: 26ebbb666cd12d4ae240c36a1f90116b7364f941 [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"
34#include "hw_features.h"
35#include "wpa_auth_glue.h"
36#include "ap_drv_ops.h"
37#include "ap_config.h"
38#include "p2p_hostapd.h"
Dmitry Shmidt04949592012-07-19 12:16:46 -070039#include "gas_serv.h"
Dmitry Shmidt051af732013-10-22 13:52:46 -070040#include "dfs.h"
Dmitry Shmidt7832adb2014-04-29 10:53:02 -070041#include "ieee802_11.h"
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080042#include "bss_load.h"
43#include "x_snoop.h"
44#include "dhcp_snoop.h"
45#include "ndisc_snoop.h"
Dmitry Shmidt849734c2016-05-27 09:59:01 -070046#include "neighbor_db.h"
47#include "rrm.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070048
49
Dmitry Shmidt04949592012-07-19 12:16:46 -070050static int hostapd_flush_old_stations(struct hostapd_data *hapd, u16 reason);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070051static int hostapd_setup_encryption(char *iface, struct hostapd_data *hapd);
Dmitry Shmidtc55524a2011-07-07 11:18:38 -070052static int hostapd_broadcast_wep_clear(struct hostapd_data *hapd);
Dmitry Shmidtcce06662013-11-04 18:44:24 -080053static int setup_interface2(struct hostapd_iface *iface);
54static void channel_list_update_timeout(void *eloop_ctx, void *timeout_ctx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070055
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070056
Dmitry Shmidt04949592012-07-19 12:16:46 -070057int hostapd_for_each_interface(struct hapd_interfaces *interfaces,
58 int (*cb)(struct hostapd_iface *iface,
59 void *ctx), void *ctx)
60{
61 size_t i;
62 int ret;
63
64 for (i = 0; i < interfaces->count; i++) {
65 ret = cb(interfaces->iface[i], ctx);
66 if (ret)
67 return ret;
68 }
69
70 return 0;
71}
72
73
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070074static void hostapd_reload_bss(struct hostapd_data *hapd)
75{
Dmitry Shmidtcce06662013-11-04 18:44:24 -080076 struct hostapd_ssid *ssid;
77
Dmitry Shmidt29333592017-01-09 12:27:11 -080078 if (!hapd->started)
79 return;
80
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070081#ifndef CONFIG_NO_RADIUS
82 radius_client_reconfig(hapd->radius, hapd->conf->radius);
83#endif /* CONFIG_NO_RADIUS */
84
Dmitry Shmidtcce06662013-11-04 18:44:24 -080085 ssid = &hapd->conf->ssid;
86 if (!ssid->wpa_psk_set && ssid->wpa_psk && !ssid->wpa_psk->next &&
87 ssid->wpa_passphrase_set && ssid->wpa_passphrase) {
88 /*
89 * Force PSK to be derived again since SSID or passphrase may
90 * have changed.
91 */
Dmitry Shmidt7f656022015-02-25 14:36:37 -080092 hostapd_config_clear_wpa_psk(&hapd->conf->ssid.wpa_psk);
Dmitry Shmidtcce06662013-11-04 18:44:24 -080093 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070094 if (hostapd_setup_wpa_psk(hapd->conf)) {
95 wpa_printf(MSG_ERROR, "Failed to re-configure WPA PSK "
96 "after reloading configuration");
97 }
98
99 if (hapd->conf->ieee802_1x || hapd->conf->wpa)
100 hostapd_set_drv_ieee8021x(hapd, hapd->conf->iface, 1);
101 else
102 hostapd_set_drv_ieee8021x(hapd, hapd->conf->iface, 0);
103
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800104 if ((hapd->conf->wpa || hapd->conf->osen) && hapd->wpa_auth == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700105 hostapd_setup_wpa(hapd);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800106 if (hapd->wpa_auth)
107 wpa_init_keys(hapd->wpa_auth);
108 } else if (hapd->conf->wpa) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700109 const u8 *wpa_ie;
110 size_t wpa_ie_len;
111 hostapd_reconfig_wpa(hapd);
112 wpa_ie = wpa_auth_get_wpa_ie(hapd->wpa_auth, &wpa_ie_len);
113 if (hostapd_set_generic_elem(hapd, wpa_ie, wpa_ie_len))
114 wpa_printf(MSG_ERROR, "Failed to configure WPA IE for "
115 "the kernel driver.");
116 } else if (hapd->wpa_auth) {
117 wpa_deinit(hapd->wpa_auth);
118 hapd->wpa_auth = NULL;
119 hostapd_set_privacy(hapd, 0);
120 hostapd_setup_encryption(hapd->conf->iface, hapd);
121 hostapd_set_generic_elem(hapd, (u8 *) "", 0);
122 }
123
124 ieee802_11_set_beacon(hapd);
125 hostapd_update_wps(hapd);
126
127 if (hapd->conf->ssid.ssid_set &&
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700128 hostapd_set_ssid(hapd, hapd->conf->ssid.ssid,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700129 hapd->conf->ssid.ssid_len)) {
130 wpa_printf(MSG_ERROR, "Could not set SSID for kernel driver");
131 /* try to continue */
132 }
133 wpa_printf(MSG_DEBUG, "Reconfigured interface %s", hapd->conf->iface);
134}
135
136
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700137static void hostapd_clear_old(struct hostapd_iface *iface)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700138{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700139 size_t j;
140
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700141 /*
142 * Deauthenticate all stations since the new configuration may not
143 * allow them to use the BSS anymore.
144 */
145 for (j = 0; j < iface->num_bss; j++) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700146 hostapd_flush_old_stations(iface->bss[j],
147 WLAN_REASON_PREV_AUTH_NOT_VALID);
Dmitry Shmidtc55524a2011-07-07 11:18:38 -0700148 hostapd_broadcast_wep_clear(iface->bss[j]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700149
150#ifndef CONFIG_NO_RADIUS
151 /* TODO: update dynamic data based on changed configuration
152 * items (e.g., open/close sockets, etc.) */
153 radius_client_flush(iface->bss[j]->radius, 0);
154#endif /* CONFIG_NO_RADIUS */
155 }
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700156}
157
158
159int hostapd_reload_config(struct hostapd_iface *iface)
160{
161 struct hostapd_data *hapd = iface->bss[0];
162 struct hostapd_config *newconf, *oldconf;
163 size_t j;
164
165 if (iface->config_fname == NULL) {
166 /* Only in-memory config in use - assume it has been updated */
167 hostapd_clear_old(iface);
168 for (j = 0; j < iface->num_bss; j++)
169 hostapd_reload_bss(iface->bss[j]);
170 return 0;
171 }
172
173 if (iface->interfaces == NULL ||
174 iface->interfaces->config_read_cb == NULL)
175 return -1;
176 newconf = iface->interfaces->config_read_cb(iface->config_fname);
177 if (newconf == NULL)
178 return -1;
179
180 hostapd_clear_old(iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700181
182 oldconf = hapd->iconf;
183 iface->conf = newconf;
184
185 for (j = 0; j < iface->num_bss; j++) {
186 hapd = iface->bss[j];
187 hapd->iconf = newconf;
Dmitry Shmidtd11f0192014-03-24 12:09:47 -0700188 hapd->iconf->channel = oldconf->channel;
Dmitry Shmidtdda10c22015-03-24 16:05:01 -0700189 hapd->iconf->acs = oldconf->acs;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -0700190 hapd->iconf->secondary_channel = oldconf->secondary_channel;
Dmitry Shmidtd11f0192014-03-24 12:09:47 -0700191 hapd->iconf->ieee80211n = oldconf->ieee80211n;
192 hapd->iconf->ieee80211ac = oldconf->ieee80211ac;
193 hapd->iconf->ht_capab = oldconf->ht_capab;
194 hapd->iconf->vht_capab = oldconf->vht_capab;
195 hapd->iconf->vht_oper_chwidth = oldconf->vht_oper_chwidth;
196 hapd->iconf->vht_oper_centr_freq_seg0_idx =
197 oldconf->vht_oper_centr_freq_seg0_idx;
198 hapd->iconf->vht_oper_centr_freq_seg1_idx =
199 oldconf->vht_oper_centr_freq_seg1_idx;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800200 hapd->conf = newconf->bss[j];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700201 hostapd_reload_bss(hapd);
202 }
203
204 hostapd_config_free(oldconf);
205
206
207 return 0;
208}
209
210
211static void hostapd_broadcast_key_clear_iface(struct hostapd_data *hapd,
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700212 const char *ifname)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700213{
214 int i;
215
Dmitry Shmidt29333592017-01-09 12:27:11 -0800216 if (!ifname || !hapd->drv_priv)
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700217 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700218 for (i = 0; i < NUM_WEP_KEYS; i++) {
219 if (hostapd_drv_set_key(ifname, hapd, WPA_ALG_NONE, NULL, i,
220 0, NULL, 0, NULL, 0)) {
221 wpa_printf(MSG_DEBUG, "Failed to clear default "
222 "encryption keys (ifname=%s keyidx=%d)",
223 ifname, i);
224 }
225 }
226#ifdef CONFIG_IEEE80211W
227 if (hapd->conf->ieee80211w) {
228 for (i = NUM_WEP_KEYS; i < NUM_WEP_KEYS + 2; i++) {
229 if (hostapd_drv_set_key(ifname, hapd, WPA_ALG_NONE,
230 NULL, i, 0, NULL,
231 0, NULL, 0)) {
232 wpa_printf(MSG_DEBUG, "Failed to clear "
233 "default mgmt encryption keys "
234 "(ifname=%s keyidx=%d)", ifname, i);
235 }
236 }
237 }
238#endif /* CONFIG_IEEE80211W */
239}
240
241
242static int hostapd_broadcast_wep_clear(struct hostapd_data *hapd)
243{
244 hostapd_broadcast_key_clear_iface(hapd, hapd->conf->iface);
245 return 0;
246}
247
248
249static int hostapd_broadcast_wep_set(struct hostapd_data *hapd)
250{
251 int errors = 0, idx;
252 struct hostapd_ssid *ssid = &hapd->conf->ssid;
253
254 idx = ssid->wep.idx;
255 if (ssid->wep.default_len &&
256 hostapd_drv_set_key(hapd->conf->iface,
257 hapd, WPA_ALG_WEP, broadcast_ether_addr, idx,
258 1, NULL, 0, ssid->wep.key[idx],
259 ssid->wep.len[idx])) {
260 wpa_printf(MSG_WARNING, "Could not set WEP encryption.");
261 errors++;
262 }
263
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700264 return errors;
265}
266
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700267
Dmitry Shmidt04949592012-07-19 12:16:46 -0700268static void hostapd_free_hapd_data(struct hostapd_data *hapd)
269{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800270 os_free(hapd->probereq_cb);
271 hapd->probereq_cb = NULL;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800272 hapd->num_probereq_cb = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800273
274#ifdef CONFIG_P2P
275 wpabuf_free(hapd->p2p_beacon_ie);
276 hapd->p2p_beacon_ie = NULL;
277 wpabuf_free(hapd->p2p_probe_resp_ie);
278 hapd->p2p_probe_resp_ie = NULL;
279#endif /* CONFIG_P2P */
280
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -0800281 if (!hapd->started) {
282 wpa_printf(MSG_ERROR, "%s: Interface %s wasn't started",
283 __func__, hapd->conf->iface);
284 return;
285 }
286 hapd->started = 0;
287
Dmitry Shmidt54605472013-11-08 11:10:19 -0800288 wpa_printf(MSG_DEBUG, "%s(%s)", __func__, hapd->conf->iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700289 iapp_deinit(hapd->iapp);
290 hapd->iapp = NULL;
291 accounting_deinit(hapd);
292 hostapd_deinit_wpa(hapd);
293 vlan_deinit(hapd);
294 hostapd_acl_deinit(hapd);
295#ifndef CONFIG_NO_RADIUS
296 radius_client_deinit(hapd->radius);
297 hapd->radius = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700298 radius_das_deinit(hapd->radius_das);
299 hapd->radius_das = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700300#endif /* CONFIG_NO_RADIUS */
301
302 hostapd_deinit_wps(hapd);
303
304 authsrv_deinit(hapd);
305
Dmitry Shmidt71757432014-06-02 13:50:35 -0700306 if (hapd->interface_added) {
307 hapd->interface_added = 0;
308 if (hostapd_if_remove(hapd, WPA_IF_AP_BSS, hapd->conf->iface)) {
309 wpa_printf(MSG_WARNING,
310 "Failed to remove BSS interface %s",
311 hapd->conf->iface);
312 hapd->interface_added = 1;
313 } else {
314 /*
315 * Since this was a dynamically added interface, the
316 * driver wrapper may have removed its internal instance
317 * and hapd->drv_priv is not valid anymore.
318 */
319 hapd->drv_priv = NULL;
320 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700321 }
322
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800323 wpabuf_free(hapd->time_adv);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700324
325#ifdef CONFIG_INTERWORKING
326 gas_serv_deinit(hapd);
327#endif /* CONFIG_INTERWORKING */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800328
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800329 bss_load_update_deinit(hapd);
330 ndisc_snoop_deinit(hapd);
331 dhcp_snoop_deinit(hapd);
332 x_snoop_deinit(hapd);
333
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800334#ifdef CONFIG_SQLITE
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700335 bin_clear_free(hapd->tmp_eap_user.identity,
336 hapd->tmp_eap_user.identity_len);
337 bin_clear_free(hapd->tmp_eap_user.password,
338 hapd->tmp_eap_user.password_len);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800339#endif /* CONFIG_SQLITE */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800340
341#ifdef CONFIG_MESH
342 wpabuf_free(hapd->mesh_pending_auth);
343 hapd->mesh_pending_auth = NULL;
344#endif /* CONFIG_MESH */
Dmitry Shmidt849734c2016-05-27 09:59:01 -0700345
346 hostapd_clean_rrm(hapd);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700347}
348
349
350/**
351 * hostapd_cleanup - Per-BSS cleanup (deinitialization)
352 * @hapd: Pointer to BSS data
353 *
354 * This function is used to free all per-BSS data structures and resources.
Dmitry Shmidt54605472013-11-08 11:10:19 -0800355 * Most of the modules that are initialized in hostapd_setup_bss() are
356 * deinitialized here.
Dmitry Shmidt04949592012-07-19 12:16:46 -0700357 */
358static void hostapd_cleanup(struct hostapd_data *hapd)
359{
Dmitry Shmidt54605472013-11-08 11:10:19 -0800360 wpa_printf(MSG_DEBUG, "%s(hapd=%p (%s))", __func__, hapd,
361 hapd->conf->iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700362 if (hapd->iface->interfaces &&
Dmitry Shmidt29333592017-01-09 12:27:11 -0800363 hapd->iface->interfaces->ctrl_iface_deinit) {
364 wpa_msg(hapd->msg_ctx, MSG_INFO, WPA_EVENT_TERMINATING);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700365 hapd->iface->interfaces->ctrl_iface_deinit(hapd);
Dmitry Shmidt29333592017-01-09 12:27:11 -0800366 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700367 hostapd_free_hapd_data(hapd);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700368}
369
370
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800371static void sta_track_deinit(struct hostapd_iface *iface)
372{
373 struct hostapd_sta_info *info;
374
375 if (!iface->num_sta_seen)
376 return;
377
378 while ((info = dl_list_first(&iface->sta_seen, struct hostapd_sta_info,
379 list))) {
380 dl_list_del(&info->list);
381 iface->num_sta_seen--;
Dmitry Shmidtaca489e2016-09-28 15:44:14 -0700382 sta_track_del(info);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800383 }
384}
385
386
Dmitry Shmidt04949592012-07-19 12:16:46 -0700387static void hostapd_cleanup_iface_partial(struct hostapd_iface *iface)
388{
Dmitry Shmidt54605472013-11-08 11:10:19 -0800389 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800390#ifdef CONFIG_IEEE80211N
391#ifdef NEED_AP_MLME
392 hostapd_stop_setup_timers(iface);
393#endif /* NEED_AP_MLME */
394#endif /* CONFIG_IEEE80211N */
Dmitry Shmidt04949592012-07-19 12:16:46 -0700395 hostapd_free_hw_features(iface->hw_features, iface->num_hw_features);
396 iface->hw_features = NULL;
397 os_free(iface->current_rates);
398 iface->current_rates = NULL;
399 os_free(iface->basic_rates);
400 iface->basic_rates = NULL;
401 ap_list_deinit(iface);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800402 sta_track_deinit(iface);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700403}
404
405
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700406/**
407 * hostapd_cleanup_iface - Complete per-interface cleanup
408 * @iface: Pointer to interface data
409 *
410 * This function is called after per-BSS data structures are deinitialized
411 * with hostapd_cleanup().
412 */
413static void hostapd_cleanup_iface(struct hostapd_iface *iface)
414{
Dmitry Shmidt54605472013-11-08 11:10:19 -0800415 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800416 eloop_cancel_timeout(channel_list_update_timeout, iface, NULL);
417
Dmitry Shmidt04949592012-07-19 12:16:46 -0700418 hostapd_cleanup_iface_partial(iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700419 hostapd_config_free(iface->conf);
420 iface->conf = NULL;
421
422 os_free(iface->config_fname);
423 os_free(iface->bss);
Dmitry Shmidt54605472013-11-08 11:10:19 -0800424 wpa_printf(MSG_DEBUG, "%s: free iface=%p", __func__, iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700425 os_free(iface);
426}
427
428
Dmitry Shmidt04949592012-07-19 12:16:46 -0700429static void hostapd_clear_wep(struct hostapd_data *hapd)
430{
Dmitry Shmidta38abf92014-03-06 13:38:44 -0800431 if (hapd->drv_priv && !hapd->iface->driver_ap_teardown) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700432 hostapd_set_privacy(hapd, 0);
433 hostapd_broadcast_wep_clear(hapd);
434 }
435}
436
437
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700438static int hostapd_setup_encryption(char *iface, struct hostapd_data *hapd)
439{
440 int i;
441
442 hostapd_broadcast_wep_set(hapd);
443
444 if (hapd->conf->ssid.wep.default_len) {
445 hostapd_set_privacy(hapd, 1);
446 return 0;
447 }
448
Jouni Malinen75ecf522011-06-27 15:19:46 -0700449 /*
450 * When IEEE 802.1X is not enabled, the driver may need to know how to
451 * set authentication algorithms for static WEP.
452 */
453 hostapd_drv_set_authmode(hapd, hapd->conf->auth_algs);
454
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700455 for (i = 0; i < 4; i++) {
456 if (hapd->conf->ssid.wep.key[i] &&
457 hostapd_drv_set_key(iface, hapd, WPA_ALG_WEP, NULL, i,
458 i == hapd->conf->ssid.wep.idx, NULL, 0,
459 hapd->conf->ssid.wep.key[i],
460 hapd->conf->ssid.wep.len[i])) {
461 wpa_printf(MSG_WARNING, "Could not set WEP "
462 "encryption.");
463 return -1;
464 }
465 if (hapd->conf->ssid.wep.key[i] &&
466 i == hapd->conf->ssid.wep.idx)
467 hostapd_set_privacy(hapd, 1);
468 }
469
470 return 0;
471}
472
473
Dmitry Shmidt04949592012-07-19 12:16:46 -0700474static int hostapd_flush_old_stations(struct hostapd_data *hapd, u16 reason)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700475{
476 int ret = 0;
477 u8 addr[ETH_ALEN];
478
479 if (hostapd_drv_none(hapd) || hapd->drv_priv == NULL)
480 return 0;
481
Dmitry Shmidta38abf92014-03-06 13:38:44 -0800482 if (!hapd->iface->driver_ap_teardown) {
483 wpa_dbg(hapd->msg_ctx, MSG_DEBUG,
484 "Flushing old station entries");
485
486 if (hostapd_flush(hapd)) {
487 wpa_msg(hapd->msg_ctx, MSG_WARNING,
488 "Could not connect to kernel driver");
489 ret = -1;
490 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700491 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800492 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "Deauthenticate all stations");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700493 os_memset(addr, 0xff, ETH_ALEN);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700494 hostapd_drv_sta_deauth(hapd, addr, reason);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700495 hostapd_free_stas(hapd);
496
497 return ret;
498}
499
500
Dmitry Shmidt71757432014-06-02 13:50:35 -0700501static void hostapd_bss_deinit_no_free(struct hostapd_data *hapd)
502{
503 hostapd_free_stas(hapd);
504 hostapd_flush_old_stations(hapd, WLAN_REASON_DEAUTH_LEAVING);
505 hostapd_clear_wep(hapd);
506}
507
508
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700509/**
510 * hostapd_validate_bssid_configuration - Validate BSSID configuration
511 * @iface: Pointer to interface data
512 * Returns: 0 on success, -1 on failure
513 *
514 * This function is used to validate that the configured BSSIDs are valid.
515 */
516static int hostapd_validate_bssid_configuration(struct hostapd_iface *iface)
517{
518 u8 mask[ETH_ALEN] = { 0 };
519 struct hostapd_data *hapd = iface->bss[0];
520 unsigned int i = iface->conf->num_bss, bits = 0, j;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700521 int auto_addr = 0;
522
523 if (hostapd_drv_none(hapd))
524 return 0;
525
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -0800526 if (iface->conf->use_driver_iface_addr)
527 return 0;
528
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700529 /* Generate BSSID mask that is large enough to cover the BSSIDs. */
530
531 /* Determine the bits necessary to cover the number of BSSIDs. */
532 for (i--; i; i >>= 1)
533 bits++;
534
535 /* Determine the bits necessary to any configured BSSIDs,
536 if they are higher than the number of BSSIDs. */
537 for (j = 0; j < iface->conf->num_bss; j++) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800538 if (is_zero_ether_addr(iface->conf->bss[j]->bssid)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700539 if (j)
540 auto_addr++;
541 continue;
542 }
543
544 for (i = 0; i < ETH_ALEN; i++) {
545 mask[i] |=
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800546 iface->conf->bss[j]->bssid[i] ^
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700547 hapd->own_addr[i];
548 }
549 }
550
551 if (!auto_addr)
552 goto skip_mask_ext;
553
554 for (i = 0; i < ETH_ALEN && mask[i] == 0; i++)
555 ;
556 j = 0;
557 if (i < ETH_ALEN) {
558 j = (5 - i) * 8;
559
560 while (mask[i] != 0) {
561 mask[i] >>= 1;
562 j++;
563 }
564 }
565
566 if (bits < j)
567 bits = j;
568
569 if (bits > 40) {
570 wpa_printf(MSG_ERROR, "Too many bits in the BSSID mask (%u)",
571 bits);
572 return -1;
573 }
574
575 os_memset(mask, 0xff, ETH_ALEN);
576 j = bits / 8;
577 for (i = 5; i > 5 - j; i--)
578 mask[i] = 0;
579 j = bits % 8;
580 while (j--)
581 mask[i] <<= 1;
582
583skip_mask_ext:
584 wpa_printf(MSG_DEBUG, "BSS count %lu, BSSID mask " MACSTR " (%d bits)",
585 (unsigned long) iface->conf->num_bss, MAC2STR(mask), bits);
586
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700587 if (!auto_addr)
588 return 0;
589
590 for (i = 0; i < ETH_ALEN; i++) {
591 if ((hapd->own_addr[i] & mask[i]) != hapd->own_addr[i]) {
592 wpa_printf(MSG_ERROR, "Invalid BSSID mask " MACSTR
593 " for start address " MACSTR ".",
594 MAC2STR(mask), MAC2STR(hapd->own_addr));
595 wpa_printf(MSG_ERROR, "Start address must be the "
596 "first address in the block (i.e., addr "
597 "AND mask == addr).");
598 return -1;
599 }
600 }
601
602 return 0;
603}
604
605
606static int mac_in_conf(struct hostapd_config *conf, const void *a)
607{
608 size_t i;
609
610 for (i = 0; i < conf->num_bss; i++) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800611 if (hostapd_mac_comp(conf->bss[i]->bssid, a) == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700612 return 1;
613 }
614 }
615
616 return 0;
617}
618
619
Dmitry Shmidt04949592012-07-19 12:16:46 -0700620#ifndef CONFIG_NO_RADIUS
621
622static int hostapd_das_nas_mismatch(struct hostapd_data *hapd,
623 struct radius_das_attrs *attr)
624{
Dmitry Shmidt13ca8d82014-02-20 10:18:40 -0800625 if (attr->nas_identifier &&
626 (!hapd->conf->nas_identifier ||
627 os_strlen(hapd->conf->nas_identifier) !=
628 attr->nas_identifier_len ||
629 os_memcmp(hapd->conf->nas_identifier, attr->nas_identifier,
630 attr->nas_identifier_len) != 0)) {
631 wpa_printf(MSG_DEBUG, "RADIUS DAS: NAS-Identifier mismatch");
632 return 1;
633 }
634
635 if (attr->nas_ip_addr &&
636 (hapd->conf->own_ip_addr.af != AF_INET ||
637 os_memcmp(&hapd->conf->own_ip_addr.u.v4, attr->nas_ip_addr, 4) !=
638 0)) {
639 wpa_printf(MSG_DEBUG, "RADIUS DAS: NAS-IP-Address mismatch");
640 return 1;
641 }
642
643#ifdef CONFIG_IPV6
644 if (attr->nas_ipv6_addr &&
645 (hapd->conf->own_ip_addr.af != AF_INET6 ||
646 os_memcmp(&hapd->conf->own_ip_addr.u.v6, attr->nas_ipv6_addr, 16)
647 != 0)) {
648 wpa_printf(MSG_DEBUG, "RADIUS DAS: NAS-IPv6-Address mismatch");
649 return 1;
650 }
651#endif /* CONFIG_IPV6 */
652
Dmitry Shmidt04949592012-07-19 12:16:46 -0700653 return 0;
654}
655
656
657static struct sta_info * hostapd_das_find_sta(struct hostapd_data *hapd,
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800658 struct radius_das_attrs *attr,
659 int *multi)
Dmitry Shmidt04949592012-07-19 12:16:46 -0700660{
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800661 struct sta_info *selected, *sta;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700662 char buf[128];
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800663 int num_attr = 0;
664 int count;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700665
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800666 *multi = 0;
667
668 for (sta = hapd->sta_list; sta; sta = sta->next)
669 sta->radius_das_match = 1;
670
671 if (attr->sta_addr) {
672 num_attr++;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700673 sta = ap_get_sta(hapd, attr->sta_addr);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800674 if (!sta) {
675 wpa_printf(MSG_DEBUG,
676 "RADIUS DAS: No Calling-Station-Id match");
677 return NULL;
678 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700679
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800680 selected = sta;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700681 for (sta = hapd->sta_list; sta; sta = sta->next) {
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800682 if (sta != selected)
683 sta->radius_das_match = 0;
684 }
685 wpa_printf(MSG_DEBUG, "RADIUS DAS: Calling-Station-Id match");
686 }
687
688 if (attr->acct_session_id) {
689 num_attr++;
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800690 if (attr->acct_session_id_len != 16) {
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800691 wpa_printf(MSG_DEBUG,
692 "RADIUS DAS: Acct-Session-Id cannot match");
693 return NULL;
694 }
695 count = 0;
696
697 for (sta = hapd->sta_list; sta; sta = sta->next) {
698 if (!sta->radius_das_match)
699 continue;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800700 os_snprintf(buf, sizeof(buf), "%016llX",
701 (unsigned long long) sta->acct_session_id);
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800702 if (os_memcmp(attr->acct_session_id, buf, 16) != 0)
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800703 sta->radius_das_match = 0;
704 else
705 count++;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700706 }
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800707
708 if (count == 0) {
709 wpa_printf(MSG_DEBUG,
710 "RADIUS DAS: No matches remaining after Acct-Session-Id check");
711 return NULL;
712 }
713 wpa_printf(MSG_DEBUG, "RADIUS DAS: Acct-Session-Id match");
Dmitry Shmidt04949592012-07-19 12:16:46 -0700714 }
715
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800716 if (attr->acct_multi_session_id) {
717 num_attr++;
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800718 if (attr->acct_multi_session_id_len != 16) {
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800719 wpa_printf(MSG_DEBUG,
720 "RADIUS DAS: Acct-Multi-Session-Id cannot match");
721 return NULL;
722 }
723 count = 0;
724
725 for (sta = hapd->sta_list; sta; sta = sta->next) {
726 if (!sta->radius_das_match)
727 continue;
728 if (!sta->eapol_sm ||
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800729 !sta->eapol_sm->acct_multi_session_id) {
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800730 sta->radius_das_match = 0;
731 continue;
732 }
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800733 os_snprintf(buf, sizeof(buf), "%016llX",
734 (unsigned long long)
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800735 sta->eapol_sm->acct_multi_session_id);
736 if (os_memcmp(attr->acct_multi_session_id, buf, 16) !=
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800737 0)
738 sta->radius_das_match = 0;
739 else
740 count++;
741 }
742
743 if (count == 0) {
744 wpa_printf(MSG_DEBUG,
745 "RADIUS DAS: No matches remaining after Acct-Multi-Session-Id check");
746 return NULL;
747 }
748 wpa_printf(MSG_DEBUG,
749 "RADIUS DAS: Acct-Multi-Session-Id match");
750 }
751
752 if (attr->cui) {
753 num_attr++;
754 count = 0;
755
Dmitry Shmidt04949592012-07-19 12:16:46 -0700756 for (sta = hapd->sta_list; sta; sta = sta->next) {
757 struct wpabuf *cui;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800758
759 if (!sta->radius_das_match)
760 continue;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700761 cui = ieee802_1x_get_radius_cui(sta->eapol_sm);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800762 if (!cui || wpabuf_len(cui) != attr->cui_len ||
Dmitry Shmidt04949592012-07-19 12:16:46 -0700763 os_memcmp(wpabuf_head(cui), attr->cui,
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800764 attr->cui_len) != 0)
765 sta->radius_das_match = 0;
766 else
767 count++;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700768 }
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800769
770 if (count == 0) {
771 wpa_printf(MSG_DEBUG,
772 "RADIUS DAS: No matches remaining after Chargeable-User-Identity check");
773 return NULL;
774 }
775 wpa_printf(MSG_DEBUG,
776 "RADIUS DAS: Chargeable-User-Identity match");
Dmitry Shmidt04949592012-07-19 12:16:46 -0700777 }
778
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800779 if (attr->user_name) {
780 num_attr++;
781 count = 0;
782
Dmitry Shmidt04949592012-07-19 12:16:46 -0700783 for (sta = hapd->sta_list; sta; sta = sta->next) {
784 u8 *identity;
785 size_t identity_len;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800786
787 if (!sta->radius_das_match)
788 continue;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700789 identity = ieee802_1x_get_identity(sta->eapol_sm,
790 &identity_len);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800791 if (!identity ||
792 identity_len != attr->user_name_len ||
Dmitry Shmidt04949592012-07-19 12:16:46 -0700793 os_memcmp(identity, attr->user_name, identity_len)
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800794 != 0)
795 sta->radius_das_match = 0;
796 else
797 count++;
798 }
799
800 if (count == 0) {
801 wpa_printf(MSG_DEBUG,
802 "RADIUS DAS: No matches remaining after User-Name check");
803 return NULL;
804 }
805 wpa_printf(MSG_DEBUG,
806 "RADIUS DAS: User-Name match");
807 }
808
809 if (num_attr == 0) {
810 /*
811 * In theory, we could match all current associations, but it
812 * seems safer to just reject requests that do not include any
813 * session identification attributes.
814 */
815 wpa_printf(MSG_DEBUG,
816 "RADIUS DAS: No session identification attributes included");
817 return NULL;
818 }
819
820 selected = NULL;
821 for (sta = hapd->sta_list; sta; sta = sta->next) {
822 if (sta->radius_das_match) {
823 if (selected) {
824 *multi = 1;
825 return NULL;
826 }
827 selected = sta;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700828 }
829 }
830
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800831 return selected;
832}
833
834
835static int hostapd_das_disconnect_pmksa(struct hostapd_data *hapd,
836 struct radius_das_attrs *attr)
837{
838 if (!hapd->wpa_auth)
839 return -1;
840 return wpa_auth_radius_das_disconnect_pmksa(hapd->wpa_auth, attr);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700841}
842
843
844static enum radius_das_res
845hostapd_das_disconnect(void *ctx, struct radius_das_attrs *attr)
846{
847 struct hostapd_data *hapd = ctx;
848 struct sta_info *sta;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800849 int multi;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700850
851 if (hostapd_das_nas_mismatch(hapd, attr))
852 return RADIUS_DAS_NAS_MISMATCH;
853
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800854 sta = hostapd_das_find_sta(hapd, attr, &multi);
855 if (sta == NULL) {
856 if (multi) {
857 wpa_printf(MSG_DEBUG,
858 "RADIUS DAS: Multiple sessions match - not supported");
859 return RADIUS_DAS_MULTI_SESSION_MATCH;
860 }
861 if (hostapd_das_disconnect_pmksa(hapd, attr) == 0) {
862 wpa_printf(MSG_DEBUG,
863 "RADIUS DAS: PMKSA cache entry matched");
864 return RADIUS_DAS_SUCCESS;
865 }
866 wpa_printf(MSG_DEBUG, "RADIUS DAS: No matching session found");
Dmitry Shmidt04949592012-07-19 12:16:46 -0700867 return RADIUS_DAS_SESSION_NOT_FOUND;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800868 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700869
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800870 wpa_printf(MSG_DEBUG, "RADIUS DAS: Found a matching session " MACSTR
871 " - disconnecting", MAC2STR(sta->addr));
Dmitry Shmidt13ca8d82014-02-20 10:18:40 -0800872 wpa_auth_pmksa_remove(hapd->wpa_auth, sta->addr);
873
Dmitry Shmidt04949592012-07-19 12:16:46 -0700874 hostapd_drv_sta_deauth(hapd, sta->addr,
875 WLAN_REASON_PREV_AUTH_NOT_VALID);
876 ap_sta_deauthenticate(hapd, sta, WLAN_REASON_PREV_AUTH_NOT_VALID);
877
878 return RADIUS_DAS_SUCCESS;
879}
880
881#endif /* CONFIG_NO_RADIUS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700882
883
884/**
885 * hostapd_setup_bss - Per-BSS setup (initialization)
886 * @hapd: Pointer to BSS data
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800887 * @first: Whether this BSS is the first BSS of an interface; -1 = not first,
888 * but interface may exist
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700889 *
890 * This function is used to initialize all per-BSS data structures and
891 * resources. This gets called in a loop for each BSS when an interface is
892 * initialized. Most of the modules that are initialized here will be
893 * deinitialized in hostapd_cleanup().
894 */
895static int hostapd_setup_bss(struct hostapd_data *hapd, int first)
896{
897 struct hostapd_bss_config *conf = hapd->conf;
Dmitry Shmidt9d9e6022015-04-23 10:34:55 -0700898 u8 ssid[SSID_MAX_LEN + 1];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700899 int ssid_len, set_ssid;
900 char force_ifname[IFNAMSIZ];
901 u8 if_addr[ETH_ALEN];
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800902 int flush_old_stations = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700903
Dmitry Shmidt54605472013-11-08 11:10:19 -0800904 wpa_printf(MSG_DEBUG, "%s(hapd=%p (%s), first=%d)",
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700905 __func__, hapd, conf->iface, first);
Dmitry Shmidt54605472013-11-08 11:10:19 -0800906
Dmitry Shmidt50b691d2014-05-21 14:01:45 -0700907#ifdef EAP_SERVER_TNC
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700908 if (conf->tnc && tncs_global_init() < 0) {
Dmitry Shmidt50b691d2014-05-21 14:01:45 -0700909 wpa_printf(MSG_ERROR, "Failed to initialize TNCS");
910 return -1;
911 }
912#endif /* EAP_SERVER_TNC */
913
Dmitry Shmidt54605472013-11-08 11:10:19 -0800914 if (hapd->started) {
915 wpa_printf(MSG_ERROR, "%s: Interface %s was already started",
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700916 __func__, conf->iface);
Dmitry Shmidt54605472013-11-08 11:10:19 -0800917 return -1;
918 }
919 hapd->started = 1;
920
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800921 if (!first || first == -1) {
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -0800922 u8 *addr = hapd->own_addr;
923
924 if (!is_zero_ether_addr(conf->bssid)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700925 /* Allocate the configured BSSID. */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700926 os_memcpy(hapd->own_addr, conf->bssid, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700927
928 if (hostapd_mac_comp(hapd->own_addr,
929 hapd->iface->bss[0]->own_addr) ==
930 0) {
931 wpa_printf(MSG_ERROR, "BSS '%s' may not have "
932 "BSSID set to the MAC address of "
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700933 "the radio", conf->iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700934 return -1;
935 }
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -0800936 } else if (hapd->iconf->use_driver_iface_addr) {
937 addr = NULL;
938 } else {
939 /* Allocate the next available BSSID. */
940 do {
941 inc_byte_array(hapd->own_addr, ETH_ALEN);
942 } while (mac_in_conf(hapd->iconf, hapd->own_addr));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700943 }
944
Dmitry Shmidt54605472013-11-08 11:10:19 -0800945 hapd->interface_added = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700946 if (hostapd_if_add(hapd->iface->bss[0], WPA_IF_AP_BSS,
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -0800947 conf->iface, addr, hapd,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700948 &hapd->drv_priv, force_ifname, if_addr,
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700949 conf->bridge[0] ? conf->bridge : NULL,
950 first == -1)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700951 wpa_printf(MSG_ERROR, "Failed to add BSS (BSSID="
952 MACSTR ")", MAC2STR(hapd->own_addr));
Dmitry Shmidt3cf6f792013-12-18 13:12:19 -0800953 hapd->interface_added = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700954 return -1;
955 }
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -0800956
957 if (!addr)
958 os_memcpy(hapd->own_addr, if_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700959 }
960
961 if (conf->wmm_enabled < 0)
962 conf->wmm_enabled = hapd->iconf->ieee80211n;
963
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800964#ifdef CONFIG_IEEE80211R_AP
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800965 if (is_zero_ether_addr(conf->r1_key_holder))
966 os_memcpy(conf->r1_key_holder, hapd->own_addr, ETH_ALEN);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800967#endif /* CONFIG_IEEE80211R_AP */
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800968
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800969#ifdef CONFIG_MESH
970 if (hapd->iface->mconf == NULL)
971 flush_old_stations = 0;
972#endif /* CONFIG_MESH */
973
974 if (flush_old_stations)
975 hostapd_flush_old_stations(hapd,
976 WLAN_REASON_PREV_AUTH_NOT_VALID);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700977 hostapd_set_privacy(hapd, 0);
978
979 hostapd_broadcast_wep_clear(hapd);
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700980 if (hostapd_setup_encryption(conf->iface, hapd))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700981 return -1;
982
983 /*
984 * Fetch the SSID from the system and use it or,
985 * if one was specified in the config file, verify they
986 * match.
987 */
988 ssid_len = hostapd_get_ssid(hapd, ssid, sizeof(ssid));
989 if (ssid_len < 0) {
990 wpa_printf(MSG_ERROR, "Could not read SSID from system");
991 return -1;
992 }
993 if (conf->ssid.ssid_set) {
994 /*
995 * If SSID is specified in the config file and it differs
996 * from what is being used then force installation of the
997 * new SSID.
998 */
999 set_ssid = (conf->ssid.ssid_len != (size_t) ssid_len ||
1000 os_memcmp(conf->ssid.ssid, ssid, ssid_len) != 0);
1001 } else {
1002 /*
1003 * No SSID in the config file; just use the one we got
1004 * from the system.
1005 */
1006 set_ssid = 0;
1007 conf->ssid.ssid_len = ssid_len;
1008 os_memcpy(conf->ssid.ssid, ssid, conf->ssid.ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001009 }
1010
1011 if (!hostapd_drv_none(hapd)) {
1012 wpa_printf(MSG_ERROR, "Using interface %s with hwaddr " MACSTR
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001013 " and ssid \"%s\"",
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001014 conf->iface, MAC2STR(hapd->own_addr),
1015 wpa_ssid_txt(conf->ssid.ssid, conf->ssid.ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001016 }
1017
1018 if (hostapd_setup_wpa_psk(conf)) {
1019 wpa_printf(MSG_ERROR, "WPA-PSK setup failed.");
1020 return -1;
1021 }
1022
1023 /* Set SSID for the kernel driver (to be used in beacon and probe
1024 * response frames) */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001025 if (set_ssid && hostapd_set_ssid(hapd, conf->ssid.ssid,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001026 conf->ssid.ssid_len)) {
1027 wpa_printf(MSG_ERROR, "Could not set SSID for kernel driver");
1028 return -1;
1029 }
1030
Dmitry Shmidt818ea482014-03-10 13:15:21 -07001031 if (wpa_debug_level <= MSG_MSGDUMP)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001032 conf->radius->msg_dumps = 1;
1033#ifndef CONFIG_NO_RADIUS
1034 hapd->radius = radius_client_init(hapd, conf->radius);
1035 if (hapd->radius == NULL) {
1036 wpa_printf(MSG_ERROR, "RADIUS client initialization failed.");
1037 return -1;
1038 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001039
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001040 if (conf->radius_das_port) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001041 struct radius_das_conf das_conf;
1042 os_memset(&das_conf, 0, sizeof(das_conf));
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001043 das_conf.port = conf->radius_das_port;
1044 das_conf.shared_secret = conf->radius_das_shared_secret;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001045 das_conf.shared_secret_len =
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001046 conf->radius_das_shared_secret_len;
1047 das_conf.client_addr = &conf->radius_das_client_addr;
1048 das_conf.time_window = conf->radius_das_time_window;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001049 das_conf.require_event_timestamp =
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001050 conf->radius_das_require_event_timestamp;
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07001051 das_conf.require_message_authenticator =
1052 conf->radius_das_require_message_authenticator;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001053 das_conf.ctx = hapd;
1054 das_conf.disconnect = hostapd_das_disconnect;
1055 hapd->radius_das = radius_das_init(&das_conf);
1056 if (hapd->radius_das == NULL) {
1057 wpa_printf(MSG_ERROR, "RADIUS DAS initialization "
1058 "failed.");
1059 return -1;
1060 }
1061 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001062#endif /* CONFIG_NO_RADIUS */
1063
1064 if (hostapd_acl_init(hapd)) {
1065 wpa_printf(MSG_ERROR, "ACL initialization failed.");
1066 return -1;
1067 }
1068 if (hostapd_init_wps(hapd, conf))
1069 return -1;
1070
1071 if (authsrv_init(hapd) < 0)
1072 return -1;
1073
1074 if (ieee802_1x_init(hapd)) {
1075 wpa_printf(MSG_ERROR, "IEEE 802.1X initialization failed.");
1076 return -1;
1077 }
1078
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001079 if ((conf->wpa || conf->osen) && hostapd_setup_wpa(hapd))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001080 return -1;
1081
1082 if (accounting_init(hapd)) {
1083 wpa_printf(MSG_ERROR, "Accounting initialization failed.");
1084 return -1;
1085 }
1086
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001087 if (conf->ieee802_11f &&
1088 (hapd->iapp = iapp_init(hapd, conf->iapp_iface)) == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001089 wpa_printf(MSG_ERROR, "IEEE 802.11F (IAPP) initialization "
1090 "failed.");
1091 return -1;
1092 }
1093
Dmitry Shmidt04949592012-07-19 12:16:46 -07001094#ifdef CONFIG_INTERWORKING
1095 if (gas_serv_init(hapd)) {
1096 wpa_printf(MSG_ERROR, "GAS server initialization failed");
1097 return -1;
1098 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001099
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001100 if (conf->qos_map_set_len &&
1101 hostapd_drv_set_qos_map(hapd, conf->qos_map_set,
1102 conf->qos_map_set_len)) {
1103 wpa_printf(MSG_ERROR, "Failed to initialize QoS Map");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001104 return -1;
1105 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001106#endif /* CONFIG_INTERWORKING */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001107
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001108 if (conf->bss_load_update_period && bss_load_update_init(hapd)) {
1109 wpa_printf(MSG_ERROR, "BSS Load initialization failed");
1110 return -1;
1111 }
1112
1113 if (conf->proxy_arp) {
1114 if (x_snoop_init(hapd)) {
1115 wpa_printf(MSG_ERROR,
1116 "Generic snooping infrastructure initialization failed");
1117 return -1;
1118 }
1119
1120 if (dhcp_snoop_init(hapd)) {
1121 wpa_printf(MSG_ERROR,
1122 "DHCP snooping initialization failed");
1123 return -1;
1124 }
1125
1126 if (ndisc_snoop_init(hapd)) {
1127 wpa_printf(MSG_ERROR,
1128 "Neighbor Discovery snooping initialization failed");
1129 return -1;
1130 }
1131 }
1132
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001133 if (!hostapd_drv_none(hapd) && vlan_init(hapd)) {
1134 wpa_printf(MSG_ERROR, "VLAN initialization failed.");
1135 return -1;
1136 }
1137
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001138 if (!conf->start_disabled && ieee802_11_set_beacon(hapd) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001139 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001140
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001141 if (hapd->wpa_auth && wpa_init_keys(hapd->wpa_auth) < 0)
1142 return -1;
1143
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001144 if (hapd->driver && hapd->driver->set_operstate)
1145 hapd->driver->set_operstate(hapd->drv_priv, 1);
1146
1147 return 0;
1148}
1149
1150
1151static void hostapd_tx_queue_params(struct hostapd_iface *iface)
1152{
1153 struct hostapd_data *hapd = iface->bss[0];
1154 int i;
1155 struct hostapd_tx_queue_params *p;
1156
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001157#ifdef CONFIG_MESH
1158 if (iface->mconf == NULL)
1159 return;
1160#endif /* CONFIG_MESH */
1161
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001162 for (i = 0; i < NUM_TX_QUEUES; i++) {
1163 p = &iface->conf->tx_queue[i];
1164
1165 if (hostapd_set_tx_queue_params(hapd, i, p->aifs, p->cwmin,
1166 p->cwmax, p->burst)) {
1167 wpa_printf(MSG_DEBUG, "Failed to set TX queue "
1168 "parameters for queue %d.", i);
1169 /* Continue anyway */
1170 }
1171 }
1172}
1173
1174
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001175static int hostapd_set_acl_list(struct hostapd_data *hapd,
1176 struct mac_acl_entry *mac_acl,
1177 int n_entries, u8 accept_acl)
1178{
1179 struct hostapd_acl_params *acl_params;
1180 int i, err;
1181
1182 acl_params = os_zalloc(sizeof(*acl_params) +
1183 (n_entries * sizeof(acl_params->mac_acl[0])));
1184 if (!acl_params)
1185 return -ENOMEM;
1186
1187 for (i = 0; i < n_entries; i++)
1188 os_memcpy(acl_params->mac_acl[i].addr, mac_acl[i].addr,
1189 ETH_ALEN);
1190
1191 acl_params->acl_policy = accept_acl;
1192 acl_params->num_mac_acl = n_entries;
1193
1194 err = hostapd_drv_set_acl(hapd, acl_params);
1195
1196 os_free(acl_params);
1197
1198 return err;
1199}
1200
1201
1202static void hostapd_set_acl(struct hostapd_data *hapd)
1203{
1204 struct hostapd_config *conf = hapd->iconf;
1205 int err;
1206 u8 accept_acl;
1207
1208 if (hapd->iface->drv_max_acl_mac_addrs == 0)
1209 return;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001210
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001211 if (conf->bss[0]->macaddr_acl == DENY_UNLESS_ACCEPTED) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001212 accept_acl = 1;
1213 err = hostapd_set_acl_list(hapd, conf->bss[0]->accept_mac,
1214 conf->bss[0]->num_accept_mac,
1215 accept_acl);
1216 if (err) {
1217 wpa_printf(MSG_DEBUG, "Failed to set accept acl");
1218 return;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001219 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001220 } else if (conf->bss[0]->macaddr_acl == ACCEPT_UNLESS_DENIED) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001221 accept_acl = 0;
1222 err = hostapd_set_acl_list(hapd, conf->bss[0]->deny_mac,
1223 conf->bss[0]->num_deny_mac,
1224 accept_acl);
1225 if (err) {
1226 wpa_printf(MSG_DEBUG, "Failed to set deny acl");
1227 return;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001228 }
1229 }
1230}
1231
1232
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001233static int start_ctrl_iface_bss(struct hostapd_data *hapd)
1234{
1235 if (!hapd->iface->interfaces ||
1236 !hapd->iface->interfaces->ctrl_iface_init)
1237 return 0;
1238
1239 if (hapd->iface->interfaces->ctrl_iface_init(hapd)) {
1240 wpa_printf(MSG_ERROR,
1241 "Failed to setup control interface for %s",
1242 hapd->conf->iface);
1243 return -1;
1244 }
1245
1246 return 0;
1247}
1248
1249
1250static int start_ctrl_iface(struct hostapd_iface *iface)
1251{
1252 size_t i;
1253
1254 if (!iface->interfaces || !iface->interfaces->ctrl_iface_init)
1255 return 0;
1256
1257 for (i = 0; i < iface->num_bss; i++) {
1258 struct hostapd_data *hapd = iface->bss[i];
1259 if (iface->interfaces->ctrl_iface_init(hapd)) {
1260 wpa_printf(MSG_ERROR,
1261 "Failed to setup control interface for %s",
1262 hapd->conf->iface);
1263 return -1;
1264 }
1265 }
1266
1267 return 0;
1268}
1269
1270
1271static void channel_list_update_timeout(void *eloop_ctx, void *timeout_ctx)
1272{
1273 struct hostapd_iface *iface = eloop_ctx;
1274
1275 if (!iface->wait_channel_update) {
1276 wpa_printf(MSG_INFO, "Channel list update timeout, but interface was not waiting for it");
1277 return;
1278 }
1279
1280 /*
1281 * It is possible that the existing channel list is acceptable, so try
1282 * to proceed.
1283 */
1284 wpa_printf(MSG_DEBUG, "Channel list update timeout - try to continue anyway");
1285 setup_interface2(iface);
1286}
1287
1288
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001289void hostapd_channel_list_updated(struct hostapd_iface *iface, int initiator)
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001290{
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001291 if (!iface->wait_channel_update || initiator != REGDOM_SET_BY_USER)
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001292 return;
1293
1294 wpa_printf(MSG_DEBUG, "Channel list updated - continue setup");
1295 eloop_cancel_timeout(channel_list_update_timeout, iface, NULL);
1296 setup_interface2(iface);
1297}
1298
1299
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001300static int setup_interface(struct hostapd_iface *iface)
1301{
1302 struct hostapd_data *hapd = iface->bss[0];
1303 size_t i;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001304
Dmitry Shmidta38abf92014-03-06 13:38:44 -08001305 /*
1306 * It is possible that setup_interface() is called after the interface
1307 * was disabled etc., in which case driver_ap_teardown is possibly set
1308 * to 1. Clear it here so any other key/station deletion, which is not
1309 * part of a teardown flow, would also call the relevant driver
1310 * callbacks.
1311 */
1312 iface->driver_ap_teardown = 0;
1313
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001314 if (!iface->phy[0]) {
1315 const char *phy = hostapd_drv_get_radio_name(hapd);
1316 if (phy) {
1317 wpa_printf(MSG_DEBUG, "phy: %s", phy);
1318 os_strlcpy(iface->phy, phy, sizeof(iface->phy));
1319 }
1320 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001321
1322 /*
1323 * Make sure that all BSSes get configured with a pointer to the same
1324 * driver interface.
1325 */
1326 for (i = 1; i < iface->num_bss; i++) {
1327 iface->bss[i]->driver = hapd->driver;
1328 iface->bss[i]->drv_priv = hapd->drv_priv;
1329 }
1330
1331 if (hostapd_validate_bssid_configuration(iface))
1332 return -1;
1333
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001334 /*
1335 * Initialize control interfaces early to allow external monitoring of
1336 * channel setup operations that may take considerable amount of time
1337 * especially for DFS cases.
1338 */
1339 if (start_ctrl_iface(iface))
1340 return -1;
1341
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001342 if (hapd->iconf->country[0] && hapd->iconf->country[1]) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001343 char country[4], previous_country[4];
1344
1345 hostapd_set_state(iface, HAPD_IFACE_COUNTRY_UPDATE);
1346 if (hostapd_get_country(hapd, previous_country) < 0)
1347 previous_country[0] = '\0';
1348
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001349 os_memcpy(country, hapd->iconf->country, 3);
1350 country[3] = '\0';
1351 if (hostapd_set_country(hapd, country) < 0) {
1352 wpa_printf(MSG_ERROR, "Failed to set country code");
1353 return -1;
1354 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001355
1356 wpa_printf(MSG_DEBUG, "Previous country code %s, new country code %s",
1357 previous_country, country);
1358
1359 if (os_strncmp(previous_country, country, 2) != 0) {
1360 wpa_printf(MSG_DEBUG, "Continue interface setup after channel list update");
1361 iface->wait_channel_update = 1;
Dmitry Shmidt97672262014-02-03 13:02:54 -08001362 eloop_register_timeout(5, 0,
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001363 channel_list_update_timeout,
1364 iface, NULL);
1365 return 0;
1366 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001367 }
1368
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001369 return setup_interface2(iface);
1370}
1371
1372
1373static int setup_interface2(struct hostapd_iface *iface)
1374{
1375 iface->wait_channel_update = 0;
1376
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001377 if (hostapd_get_hw_features(iface)) {
1378 /* Not all drivers support this yet, so continue without hw
1379 * feature data. */
1380 } else {
1381 int ret = hostapd_select_hw_mode(iface);
1382 if (ret < 0) {
1383 wpa_printf(MSG_ERROR, "Could not select hw_mode and "
1384 "channel. (%d)", ret);
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001385 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001386 }
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001387 if (ret == 1) {
1388 wpa_printf(MSG_DEBUG, "Interface initialization will be completed in a callback (ACS)");
1389 return 0;
1390 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001391 ret = hostapd_check_ht_capab(iface);
1392 if (ret < 0)
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001393 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001394 if (ret == 1) {
1395 wpa_printf(MSG_DEBUG, "Interface initialization will "
1396 "be completed in a callback");
1397 return 0;
1398 }
Dmitry Shmidt051af732013-10-22 13:52:46 -07001399
1400 if (iface->conf->ieee80211h)
1401 wpa_printf(MSG_DEBUG, "DFS support is enabled");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001402 }
1403 return hostapd_setup_interface_complete(iface, 0);
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001404
1405fail:
1406 hostapd_set_state(iface, HAPD_IFACE_DISABLED);
1407 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
1408 if (iface->interfaces && iface->interfaces->terminate_on_error)
1409 eloop_terminate();
1410 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001411}
1412
1413
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001414#ifdef CONFIG_FST
1415
1416static const u8 * fst_hostapd_get_bssid_cb(void *ctx)
1417{
1418 struct hostapd_data *hapd = ctx;
1419
1420 return hapd->own_addr;
1421}
1422
1423
1424static void fst_hostapd_get_channel_info_cb(void *ctx,
1425 enum hostapd_hw_mode *hw_mode,
1426 u8 *channel)
1427{
1428 struct hostapd_data *hapd = ctx;
1429
1430 *hw_mode = ieee80211_freq_to_chan(hapd->iface->freq, channel);
1431}
1432
1433
1434static void fst_hostapd_set_ies_cb(void *ctx, const struct wpabuf *fst_ies)
1435{
1436 struct hostapd_data *hapd = ctx;
1437
1438 if (hapd->iface->fst_ies != fst_ies) {
1439 hapd->iface->fst_ies = fst_ies;
1440 if (ieee802_11_set_beacon(hapd))
1441 wpa_printf(MSG_WARNING, "FST: Cannot set beacon");
1442 }
1443}
1444
1445
1446static int fst_hostapd_send_action_cb(void *ctx, const u8 *da,
1447 struct wpabuf *buf)
1448{
1449 struct hostapd_data *hapd = ctx;
1450
1451 return hostapd_drv_send_action(hapd, hapd->iface->freq, 0, da,
1452 wpabuf_head(buf), wpabuf_len(buf));
1453}
1454
1455
1456static const struct wpabuf * fst_hostapd_get_mb_ie_cb(void *ctx, const u8 *addr)
1457{
1458 struct hostapd_data *hapd = ctx;
1459 struct sta_info *sta = ap_get_sta(hapd, addr);
1460
1461 return sta ? sta->mb_ies : NULL;
1462}
1463
1464
1465static void fst_hostapd_update_mb_ie_cb(void *ctx, const u8 *addr,
1466 const u8 *buf, size_t size)
1467{
1468 struct hostapd_data *hapd = ctx;
1469 struct sta_info *sta = ap_get_sta(hapd, addr);
1470
1471 if (sta) {
1472 struct mb_ies_info info;
1473
1474 if (!mb_ies_info_by_ies(&info, buf, size)) {
1475 wpabuf_free(sta->mb_ies);
1476 sta->mb_ies = mb_ies_by_info(&info);
1477 }
1478 }
1479}
1480
1481
1482static const u8 * fst_hostapd_get_sta(struct fst_get_peer_ctx **get_ctx,
1483 Boolean mb_only)
1484{
1485 struct sta_info *s = (struct sta_info *) *get_ctx;
1486
1487 if (mb_only) {
1488 for (; s && !s->mb_ies; s = s->next)
1489 ;
1490 }
1491
1492 if (s) {
1493 *get_ctx = (struct fst_get_peer_ctx *) s->next;
1494
1495 return s->addr;
1496 }
1497
1498 *get_ctx = NULL;
1499 return NULL;
1500}
1501
1502
1503static const u8 * fst_hostapd_get_peer_first(void *ctx,
1504 struct fst_get_peer_ctx **get_ctx,
1505 Boolean mb_only)
1506{
1507 struct hostapd_data *hapd = ctx;
1508
1509 *get_ctx = (struct fst_get_peer_ctx *) hapd->sta_list;
1510
1511 return fst_hostapd_get_sta(get_ctx, mb_only);
1512}
1513
1514
1515static const u8 * fst_hostapd_get_peer_next(void *ctx,
1516 struct fst_get_peer_ctx **get_ctx,
1517 Boolean mb_only)
1518{
1519 return fst_hostapd_get_sta(get_ctx, mb_only);
1520}
1521
1522
1523void fst_hostapd_fill_iface_obj(struct hostapd_data *hapd,
1524 struct fst_wpa_obj *iface_obj)
1525{
1526 iface_obj->ctx = hapd;
1527 iface_obj->get_bssid = fst_hostapd_get_bssid_cb;
1528 iface_obj->get_channel_info = fst_hostapd_get_channel_info_cb;
1529 iface_obj->set_ies = fst_hostapd_set_ies_cb;
1530 iface_obj->send_action = fst_hostapd_send_action_cb;
1531 iface_obj->get_mb_ie = fst_hostapd_get_mb_ie_cb;
1532 iface_obj->update_mb_ie = fst_hostapd_update_mb_ie_cb;
1533 iface_obj->get_peer_first = fst_hostapd_get_peer_first;
1534 iface_obj->get_peer_next = fst_hostapd_get_peer_next;
1535}
1536
1537#endif /* CONFIG_FST */
1538
1539
Dmitry Shmidt849734c2016-05-27 09:59:01 -07001540#ifdef NEED_AP_MLME
1541static enum nr_chan_width hostapd_get_nr_chan_width(struct hostapd_data *hapd,
1542 int ht, int vht)
1543{
1544 if (!ht && !vht)
1545 return NR_CHAN_WIDTH_20;
1546 if (!hapd->iconf->secondary_channel)
1547 return NR_CHAN_WIDTH_20;
1548 if (!vht || hapd->iconf->vht_oper_chwidth == VHT_CHANWIDTH_USE_HT)
1549 return NR_CHAN_WIDTH_40;
1550 if (hapd->iconf->vht_oper_chwidth == VHT_CHANWIDTH_80MHZ)
1551 return NR_CHAN_WIDTH_80;
1552 if (hapd->iconf->vht_oper_chwidth == VHT_CHANWIDTH_160MHZ)
1553 return NR_CHAN_WIDTH_160;
1554 if (hapd->iconf->vht_oper_chwidth == VHT_CHANWIDTH_80P80MHZ)
1555 return NR_CHAN_WIDTH_80P80;
1556 return NR_CHAN_WIDTH_20;
1557}
1558#endif /* NEED_AP_MLME */
1559
1560
1561static void hostapd_set_own_neighbor_report(struct hostapd_data *hapd)
1562{
1563#ifdef NEED_AP_MLME
1564 u16 capab = hostapd_own_capab_info(hapd);
1565 int ht = hapd->iconf->ieee80211n && !hapd->conf->disable_11n;
1566 int vht = hapd->iconf->ieee80211ac && !hapd->conf->disable_11ac;
1567 struct wpa_ssid_value ssid;
1568 u8 channel, op_class;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001569 u8 center_freq1_idx = 0, center_freq2_idx = 0;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07001570 enum nr_chan_width width;
1571 u32 bssid_info;
1572 struct wpabuf *nr;
1573
1574 if (!(hapd->conf->radio_measurements[0] &
1575 WLAN_RRM_CAPS_NEIGHBOR_REPORT))
1576 return;
1577
1578 bssid_info = 3; /* AP is reachable */
1579 bssid_info |= NEI_REP_BSSID_INFO_SECURITY; /* "same as the AP" */
1580 bssid_info |= NEI_REP_BSSID_INFO_KEY_SCOPE; /* "same as the AP" */
1581
1582 if (capab & WLAN_CAPABILITY_SPECTRUM_MGMT)
1583 bssid_info |= NEI_REP_BSSID_INFO_SPECTRUM_MGMT;
1584
1585 bssid_info |= NEI_REP_BSSID_INFO_RM; /* RRM is supported */
1586
1587 if (hapd->conf->wmm_enabled) {
1588 bssid_info |= NEI_REP_BSSID_INFO_QOS;
1589
1590 if (hapd->conf->wmm_uapsd &&
1591 (hapd->iface->drv_flags & WPA_DRIVER_FLAGS_AP_UAPSD))
1592 bssid_info |= NEI_REP_BSSID_INFO_APSD;
1593 }
1594
1595 if (ht) {
1596 bssid_info |= NEI_REP_BSSID_INFO_HT |
1597 NEI_REP_BSSID_INFO_DELAYED_BA;
1598
1599 /* VHT bit added in IEEE P802.11-REVmc/D4.3 */
1600 if (vht)
1601 bssid_info |= NEI_REP_BSSID_INFO_VHT;
1602 }
1603
1604 /* TODO: Set NEI_REP_BSSID_INFO_MOBILITY_DOMAIN if MDE is set */
1605
Dmitry Shmidt29333592017-01-09 12:27:11 -08001606 if (ieee80211_freq_to_channel_ext(hapd->iface->freq,
1607 hapd->iconf->secondary_channel,
1608 hapd->iconf->vht_oper_chwidth,
1609 &op_class, &channel) ==
1610 NUM_HOSTAPD_MODES)
1611 return;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07001612 width = hostapd_get_nr_chan_width(hapd, ht, vht);
1613 if (vht) {
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001614 center_freq1_idx = hapd->iconf->vht_oper_centr_freq_seg0_idx;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07001615 if (width == NR_CHAN_WIDTH_80P80)
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001616 center_freq2_idx =
1617 hapd->iconf->vht_oper_centr_freq_seg1_idx;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07001618 } else if (ht) {
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001619 ieee80211_freq_to_chan(hapd->iface->freq +
1620 10 * hapd->iconf->secondary_channel,
1621 &center_freq1_idx);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07001622 }
1623
1624 ssid.ssid_len = hapd->conf->ssid.ssid_len;
1625 os_memcpy(ssid.ssid, hapd->conf->ssid.ssid, ssid.ssid_len);
1626
1627 /*
1628 * Neighbor Report element size = BSSID + BSSID info + op_class + chan +
1629 * phy type + wide bandwidth channel subelement.
1630 */
1631 nr = wpabuf_alloc(ETH_ALEN + 4 + 1 + 1 + 1 + 5);
1632 if (!nr)
1633 return;
1634
1635 wpabuf_put_data(nr, hapd->own_addr, ETH_ALEN);
1636 wpabuf_put_le32(nr, bssid_info);
1637 wpabuf_put_u8(nr, op_class);
1638 wpabuf_put_u8(nr, channel);
1639 wpabuf_put_u8(nr, ieee80211_get_phy_type(hapd->iface->freq, ht, vht));
1640
1641 /*
1642 * Wide Bandwidth Channel subelement may be needed to allow the
1643 * receiving STA to send packets to the AP. See IEEE P802.11-REVmc/D5.0
1644 * Figure 9-301.
1645 */
1646 wpabuf_put_u8(nr, WNM_NEIGHBOR_WIDE_BW_CHAN);
1647 wpabuf_put_u8(nr, 3);
1648 wpabuf_put_u8(nr, width);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001649 wpabuf_put_u8(nr, center_freq1_idx);
1650 wpabuf_put_u8(nr, center_freq2_idx);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07001651
1652 hostapd_neighbor_set(hapd, hapd->own_addr, &ssid, nr, hapd->iconf->lci,
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001653 hapd->iconf->civic, hapd->iconf->stationary_ap);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07001654
1655 wpabuf_free(nr);
1656#endif /* NEED_AP_MLME */
1657}
1658
1659
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001660static int hostapd_setup_interface_complete_sync(struct hostapd_iface *iface,
1661 int err)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001662{
1663 struct hostapd_data *hapd = iface->bss[0];
1664 size_t j;
1665 u8 *prev_addr;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001666 int delay_apply_cfg = 0;
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001667 int res_dfs_offload = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001668
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001669 if (err)
1670 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001671
1672 wpa_printf(MSG_DEBUG, "Completing interface initialization");
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001673 if (iface->conf->channel) {
Dmitry Shmidt051af732013-10-22 13:52:46 -07001674#ifdef NEED_AP_MLME
1675 int res;
1676#endif /* NEED_AP_MLME */
1677
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001678 iface->freq = hostapd_hw_get_freq(hapd, iface->conf->channel);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001679 wpa_printf(MSG_DEBUG, "Mode: %s Channel: %d "
1680 "Frequency: %d MHz",
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001681 hostapd_hw_mode_txt(iface->conf->hw_mode),
1682 iface->conf->channel, iface->freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001683
Dmitry Shmidt051af732013-10-22 13:52:46 -07001684#ifdef NEED_AP_MLME
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001685 /* Handle DFS only if it is not offloaded to the driver */
1686 if (!(iface->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD)) {
1687 /* Check DFS */
1688 res = hostapd_handle_dfs(iface);
1689 if (res <= 0) {
1690 if (res < 0)
1691 goto fail;
1692 return res;
1693 }
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001694 } else {
1695 /* If DFS is offloaded to the driver */
1696 res_dfs_offload = hostapd_handle_dfs_offload(iface);
1697 if (res_dfs_offload <= 0) {
1698 if (res_dfs_offload < 0)
1699 goto fail;
1700 } else {
1701 wpa_printf(MSG_DEBUG,
1702 "Proceed with AP/channel setup");
1703 /*
1704 * If this is a DFS channel, move to completing
1705 * AP setup.
1706 */
1707 if (res_dfs_offload == 1)
1708 goto dfs_offload;
1709 /* Otherwise fall through. */
1710 }
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001711 }
Dmitry Shmidt051af732013-10-22 13:52:46 -07001712#endif /* NEED_AP_MLME */
1713
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001714#ifdef CONFIG_MESH
1715 if (iface->mconf != NULL) {
1716 wpa_printf(MSG_DEBUG,
1717 "%s: Mesh configuration will be applied while joining the mesh network",
1718 iface->bss[0]->conf->iface);
1719 delay_apply_cfg = 1;
1720 }
1721#endif /* CONFIG_MESH */
1722
1723 if (!delay_apply_cfg &&
1724 hostapd_set_freq(hapd, hapd->iconf->hw_mode, iface->freq,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001725 hapd->iconf->channel,
1726 hapd->iconf->ieee80211n,
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001727 hapd->iconf->ieee80211ac,
1728 hapd->iconf->secondary_channel,
1729 hapd->iconf->vht_oper_chwidth,
1730 hapd->iconf->vht_oper_centr_freq_seg0_idx,
1731 hapd->iconf->vht_oper_centr_freq_seg1_idx)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001732 wpa_printf(MSG_ERROR, "Could not set channel for "
1733 "kernel driver");
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001734 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001735 }
1736 }
1737
1738 if (iface->current_mode) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001739 if (hostapd_prepare_rates(iface, iface->current_mode)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001740 wpa_printf(MSG_ERROR, "Failed to prepare rates "
1741 "table.");
1742 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
1743 HOSTAPD_LEVEL_WARNING,
1744 "Failed to prepare rates table.");
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001745 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001746 }
1747 }
1748
1749 if (hapd->iconf->rts_threshold > -1 &&
1750 hostapd_set_rts(hapd, hapd->iconf->rts_threshold)) {
1751 wpa_printf(MSG_ERROR, "Could not set RTS threshold for "
1752 "kernel driver");
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001753 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001754 }
1755
1756 if (hapd->iconf->fragm_threshold > -1 &&
1757 hostapd_set_frag(hapd, hapd->iconf->fragm_threshold)) {
1758 wpa_printf(MSG_ERROR, "Could not set fragmentation threshold "
1759 "for kernel driver");
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001760 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001761 }
1762
1763 prev_addr = hapd->own_addr;
1764
1765 for (j = 0; j < iface->num_bss; j++) {
1766 hapd = iface->bss[j];
1767 if (j)
1768 os_memcpy(hapd->own_addr, prev_addr, ETH_ALEN);
Dmitry Shmidt71757432014-06-02 13:50:35 -07001769 if (hostapd_setup_bss(hapd, j == 0)) {
1770 do {
1771 hapd = iface->bss[j];
1772 hostapd_bss_deinit_no_free(hapd);
1773 hostapd_free_hapd_data(hapd);
1774 } while (j-- > 0);
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001775 goto fail;
Dmitry Shmidt71757432014-06-02 13:50:35 -07001776 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001777 if (is_zero_ether_addr(hapd->conf->bssid))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001778 prev_addr = hapd->own_addr;
1779 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001780 hapd = iface->bss[0];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001781
1782 hostapd_tx_queue_params(iface);
1783
1784 ap_list_init(iface);
1785
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001786 hostapd_set_acl(hapd);
1787
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001788 if (hostapd_driver_commit(hapd) < 0) {
1789 wpa_printf(MSG_ERROR, "%s: Failed to commit driver "
1790 "configuration", __func__);
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001791 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001792 }
1793
Jouni Malinen87fd2792011-05-16 18:35:42 +03001794 /*
1795 * WPS UPnP module can be initialized only when the "upnp_iface" is up.
1796 * If "interface" and "upnp_iface" are the same (e.g., non-bridge
1797 * mode), the interface is up only after driver_commit, so initialize
1798 * WPS after driver_commit.
1799 */
1800 for (j = 0; j < iface->num_bss; j++) {
1801 if (hostapd_init_wps_complete(iface->bss[j]))
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001802 goto fail;
Jouni Malinen87fd2792011-05-16 18:35:42 +03001803 }
1804
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001805 if ((iface->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
1806 !res_dfs_offload) {
1807 /*
1808 * If freq is DFS, and DFS is offloaded to the driver, then wait
1809 * for CAC to complete.
1810 */
1811 wpa_printf(MSG_DEBUG, "%s: Wait for CAC to complete", __func__);
1812 return res_dfs_offload;
1813 }
1814
1815#ifdef NEED_AP_MLME
1816dfs_offload:
1817#endif /* NEED_AP_MLME */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001818
1819#ifdef CONFIG_FST
1820 if (hapd->iconf->fst_cfg.group_id[0]) {
1821 struct fst_wpa_obj iface_obj;
1822
1823 fst_hostapd_fill_iface_obj(hapd, &iface_obj);
1824 iface->fst = fst_attach(hapd->conf->iface, hapd->own_addr,
1825 &iface_obj, &hapd->iconf->fst_cfg);
1826 if (!iface->fst) {
1827 wpa_printf(MSG_ERROR, "Could not attach to FST %s",
1828 hapd->iconf->fst_cfg.group_id);
1829 goto fail;
1830 }
1831 }
1832#endif /* CONFIG_FST */
1833
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001834 hostapd_set_state(iface, HAPD_IFACE_ENABLED);
1835 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_ENABLED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001836 if (hapd->setup_complete_cb)
1837 hapd->setup_complete_cb(hapd->setup_complete_cb_ctx);
1838
1839 wpa_printf(MSG_DEBUG, "%s: Setup of interface done.",
1840 iface->bss[0]->conf->iface);
Dmitry Shmidtb96dad42013-11-05 10:07:29 -08001841 if (iface->interfaces && iface->interfaces->terminate_on_error > 0)
1842 iface->interfaces->terminate_on_error--;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001843
Dmitry Shmidt849734c2016-05-27 09:59:01 -07001844 for (j = 0; j < iface->num_bss; j++)
1845 hostapd_set_own_neighbor_report(iface->bss[j]);
1846
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001847 return 0;
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001848
1849fail:
1850 wpa_printf(MSG_ERROR, "Interface initialization failed");
1851 hostapd_set_state(iface, HAPD_IFACE_DISABLED);
1852 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001853#ifdef CONFIG_FST
1854 if (iface->fst) {
1855 fst_detach(iface->fst);
1856 iface->fst = NULL;
1857 }
1858#endif /* CONFIG_FST */
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001859 if (iface->interfaces && iface->interfaces->terminate_on_error)
1860 eloop_terminate();
1861 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001862}
1863
1864
1865/**
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001866 * hostapd_setup_interface_complete - Complete interface setup
1867 *
1868 * This function is called when previous steps in the interface setup has been
1869 * completed. This can also start operations, e.g., DFS, that will require
1870 * additional processing before interface is ready to be enabled. Such
1871 * operations will call this function from eloop callbacks when finished.
1872 */
1873int hostapd_setup_interface_complete(struct hostapd_iface *iface, int err)
1874{
1875 struct hapd_interfaces *interfaces = iface->interfaces;
1876 struct hostapd_data *hapd = iface->bss[0];
1877 unsigned int i;
1878 int not_ready_in_sync_ifaces = 0;
1879
1880 if (!iface->need_to_start_in_sync)
1881 return hostapd_setup_interface_complete_sync(iface, err);
1882
1883 if (err) {
1884 wpa_printf(MSG_ERROR, "Interface initialization failed");
1885 hostapd_set_state(iface, HAPD_IFACE_DISABLED);
1886 iface->need_to_start_in_sync = 0;
1887 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
1888 if (interfaces && interfaces->terminate_on_error)
1889 eloop_terminate();
1890 return -1;
1891 }
1892
1893 if (iface->ready_to_start_in_sync) {
1894 /* Already in ready and waiting. should never happpen */
1895 return 0;
1896 }
1897
1898 for (i = 0; i < interfaces->count; i++) {
1899 if (interfaces->iface[i]->need_to_start_in_sync &&
1900 !interfaces->iface[i]->ready_to_start_in_sync)
1901 not_ready_in_sync_ifaces++;
1902 }
1903
1904 /*
1905 * Check if this is the last interface, if yes then start all the other
1906 * waiting interfaces. If not, add this interface to the waiting list.
1907 */
1908 if (not_ready_in_sync_ifaces > 1 && iface->state == HAPD_IFACE_DFS) {
1909 /*
1910 * If this interface went through CAC, do not synchronize, just
1911 * start immediately.
1912 */
1913 iface->need_to_start_in_sync = 0;
1914 wpa_printf(MSG_INFO,
1915 "%s: Finished CAC - bypass sync and start interface",
1916 iface->bss[0]->conf->iface);
1917 return hostapd_setup_interface_complete_sync(iface, err);
1918 }
1919
1920 if (not_ready_in_sync_ifaces > 1) {
1921 /* need to wait as there are other interfaces still coming up */
1922 iface->ready_to_start_in_sync = 1;
1923 wpa_printf(MSG_INFO,
1924 "%s: Interface waiting to sync with other interfaces",
1925 iface->bss[0]->conf->iface);
1926 return 0;
1927 }
1928
1929 wpa_printf(MSG_INFO,
1930 "%s: Last interface to sync - starting all interfaces",
1931 iface->bss[0]->conf->iface);
1932 iface->need_to_start_in_sync = 0;
1933 hostapd_setup_interface_complete_sync(iface, err);
1934 for (i = 0; i < interfaces->count; i++) {
1935 if (interfaces->iface[i]->need_to_start_in_sync &&
1936 interfaces->iface[i]->ready_to_start_in_sync) {
1937 hostapd_setup_interface_complete_sync(
1938 interfaces->iface[i], 0);
1939 /* Only once the interfaces are sync started */
1940 interfaces->iface[i]->need_to_start_in_sync = 0;
1941 }
1942 }
1943
1944 return 0;
1945}
1946
1947
1948/**
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001949 * hostapd_setup_interface - Setup of an interface
1950 * @iface: Pointer to interface data.
1951 * Returns: 0 on success, -1 on failure
1952 *
1953 * Initializes the driver interface, validates the configuration,
1954 * and sets driver parameters based on the configuration.
1955 * Flushes old stations, sets the channel, encryption,
1956 * beacons, and WDS links based on the configuration.
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001957 *
1958 * If interface setup requires more time, e.g., to perform HT co-ex scans, ACS,
1959 * or DFS operations, this function returns 0 before such operations have been
1960 * completed. The pending operations are registered into eloop and will be
1961 * completed from eloop callbacks. Those callbacks end up calling
1962 * hostapd_setup_interface_complete() once setup has been completed.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001963 */
1964int hostapd_setup_interface(struct hostapd_iface *iface)
1965{
1966 int ret;
1967
1968 ret = setup_interface(iface);
1969 if (ret) {
1970 wpa_printf(MSG_ERROR, "%s: Unable to setup interface.",
1971 iface->bss[0]->conf->iface);
1972 return -1;
1973 }
1974
1975 return 0;
1976}
1977
1978
1979/**
1980 * hostapd_alloc_bss_data - Allocate and initialize per-BSS data
1981 * @hapd_iface: Pointer to interface data
1982 * @conf: Pointer to per-interface configuration
1983 * @bss: Pointer to per-BSS configuration for this BSS
1984 * Returns: Pointer to allocated BSS data
1985 *
1986 * This function is used to allocate per-BSS data structure. This data will be
1987 * freed after hostapd_cleanup() is called for it during interface
1988 * deinitialization.
1989 */
1990struct hostapd_data *
1991hostapd_alloc_bss_data(struct hostapd_iface *hapd_iface,
1992 struct hostapd_config *conf,
1993 struct hostapd_bss_config *bss)
1994{
1995 struct hostapd_data *hapd;
1996
1997 hapd = os_zalloc(sizeof(*hapd));
1998 if (hapd == NULL)
1999 return NULL;
2000
2001 hapd->new_assoc_sta_cb = hostapd_new_assoc_sta;
2002 hapd->iconf = conf;
2003 hapd->conf = bss;
2004 hapd->iface = hapd_iface;
2005 hapd->driver = hapd->iconf->driver;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002006 hapd->ctrl_sock = -1;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08002007 dl_list_init(&hapd->ctrl_dst);
Dmitry Shmidt7d175302016-09-06 13:11:34 -07002008 dl_list_init(&hapd->nr_db);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002009
2010 return hapd;
2011}
2012
2013
Dmitry Shmidt54605472013-11-08 11:10:19 -08002014static void hostapd_bss_deinit(struct hostapd_data *hapd)
2015{
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07002016 if (!hapd)
2017 return;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002018 wpa_printf(MSG_DEBUG, "%s: deinit bss %s", __func__,
2019 hapd->conf->iface);
Dmitry Shmidt71757432014-06-02 13:50:35 -07002020 hostapd_bss_deinit_no_free(hapd);
Dmitry Shmidtf73259c2015-03-17 11:00:54 -07002021 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
Dmitry Shmidt54605472013-11-08 11:10:19 -08002022 hostapd_cleanup(hapd);
2023}
2024
2025
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002026void hostapd_interface_deinit(struct hostapd_iface *iface)
2027{
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002028 int j;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002029
Dmitry Shmidt54605472013-11-08 11:10:19 -08002030 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002031 if (iface == NULL)
2032 return;
2033
Dmitry Shmidtf73259c2015-03-17 11:00:54 -07002034 hostapd_set_state(iface, HAPD_IFACE_DISABLED);
2035
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07002036#ifdef CONFIG_IEEE80211N
2037#ifdef NEED_AP_MLME
2038 hostapd_stop_setup_timers(iface);
2039 eloop_cancel_timeout(ap_ht2040_timeout, iface, NULL);
2040#endif /* NEED_AP_MLME */
2041#endif /* CONFIG_IEEE80211N */
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002042 eloop_cancel_timeout(channel_list_update_timeout, iface, NULL);
2043 iface->wait_channel_update = 0;
2044
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002045#ifdef CONFIG_FST
2046 if (iface->fst) {
2047 fst_detach(iface->fst);
2048 iface->fst = NULL;
2049 }
2050#endif /* CONFIG_FST */
2051
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07002052 for (j = iface->num_bss - 1; j >= 0; j--) {
2053 if (!iface->bss)
2054 break;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002055 hostapd_bss_deinit(iface->bss[j]);
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07002056 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002057}
2058
2059
2060void hostapd_interface_free(struct hostapd_iface *iface)
2061{
2062 size_t j;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002063 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
2064 for (j = 0; j < iface->num_bss; j++) {
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07002065 if (!iface->bss)
2066 break;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002067 wpa_printf(MSG_DEBUG, "%s: free hapd %p",
2068 __func__, iface->bss[j]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002069 os_free(iface->bss[j]);
Dmitry Shmidt54605472013-11-08 11:10:19 -08002070 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002071 hostapd_cleanup_iface(iface);
2072}
2073
2074
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07002075struct hostapd_iface * hostapd_alloc_iface(void)
2076{
2077 struct hostapd_iface *hapd_iface;
2078
2079 hapd_iface = os_zalloc(sizeof(*hapd_iface));
2080 if (!hapd_iface)
2081 return NULL;
2082
2083 dl_list_init(&hapd_iface->sta_seen);
2084
2085 return hapd_iface;
2086}
2087
2088
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002089/**
2090 * hostapd_init - Allocate and initialize per-interface data
2091 * @config_file: Path to the configuration file
2092 * Returns: Pointer to the allocated interface data or %NULL on failure
2093 *
2094 * This function is used to allocate main data structures for per-interface
2095 * data. The allocated data buffer will be freed by calling
2096 * hostapd_cleanup_iface().
2097 */
2098struct hostapd_iface * hostapd_init(struct hapd_interfaces *interfaces,
2099 const char *config_file)
2100{
2101 struct hostapd_iface *hapd_iface = NULL;
2102 struct hostapd_config *conf = NULL;
2103 struct hostapd_data *hapd;
2104 size_t i;
2105
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07002106 hapd_iface = hostapd_alloc_iface();
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002107 if (hapd_iface == NULL)
2108 goto fail;
2109
2110 hapd_iface->config_fname = os_strdup(config_file);
2111 if (hapd_iface->config_fname == NULL)
2112 goto fail;
2113
2114 conf = interfaces->config_read_cb(hapd_iface->config_fname);
2115 if (conf == NULL)
2116 goto fail;
2117 hapd_iface->conf = conf;
2118
2119 hapd_iface->num_bss = conf->num_bss;
2120 hapd_iface->bss = os_calloc(conf->num_bss,
2121 sizeof(struct hostapd_data *));
2122 if (hapd_iface->bss == NULL)
2123 goto fail;
2124
2125 for (i = 0; i < conf->num_bss; i++) {
2126 hapd = hapd_iface->bss[i] =
2127 hostapd_alloc_bss_data(hapd_iface, conf,
2128 conf->bss[i]);
2129 if (hapd == NULL)
2130 goto fail;
2131 hapd->msg_ctx = hapd;
2132 }
2133
2134 return hapd_iface;
2135
2136fail:
2137 wpa_printf(MSG_ERROR, "Failed to set up interface with %s",
2138 config_file);
2139 if (conf)
2140 hostapd_config_free(conf);
2141 if (hapd_iface) {
2142 os_free(hapd_iface->config_fname);
2143 os_free(hapd_iface->bss);
Dmitry Shmidt54605472013-11-08 11:10:19 -08002144 wpa_printf(MSG_DEBUG, "%s: free iface %p",
2145 __func__, hapd_iface);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002146 os_free(hapd_iface);
2147 }
2148 return NULL;
2149}
2150
2151
2152static int ifname_in_use(struct hapd_interfaces *interfaces, const char *ifname)
2153{
2154 size_t i, j;
2155
2156 for (i = 0; i < interfaces->count; i++) {
2157 struct hostapd_iface *iface = interfaces->iface[i];
2158 for (j = 0; j < iface->num_bss; j++) {
2159 struct hostapd_data *hapd = iface->bss[j];
2160 if (os_strcmp(ifname, hapd->conf->iface) == 0)
2161 return 1;
2162 }
2163 }
2164
2165 return 0;
2166}
2167
2168
2169/**
2170 * hostapd_interface_init_bss - Read configuration file and init BSS data
2171 *
2172 * This function is used to parse configuration file for a BSS. This BSS is
2173 * added to an existing interface sharing the same radio (if any) or a new
2174 * interface is created if this is the first interface on a radio. This
2175 * allocate memory for the BSS. No actual driver operations are started.
2176 *
2177 * This is similar to hostapd_interface_init(), but for a case where the
2178 * configuration is used to add a single BSS instead of all BSSes for a radio.
2179 */
2180struct hostapd_iface *
2181hostapd_interface_init_bss(struct hapd_interfaces *interfaces, const char *phy,
2182 const char *config_fname, int debug)
2183{
2184 struct hostapd_iface *new_iface = NULL, *iface = NULL;
2185 struct hostapd_data *hapd;
2186 int k;
2187 size_t i, bss_idx;
2188
2189 if (!phy || !*phy)
2190 return NULL;
2191
2192 for (i = 0; i < interfaces->count; i++) {
2193 if (os_strcmp(interfaces->iface[i]->phy, phy) == 0) {
2194 iface = interfaces->iface[i];
2195 break;
2196 }
2197 }
2198
2199 wpa_printf(MSG_INFO, "Configuration file: %s (phy %s)%s",
2200 config_fname, phy, iface ? "" : " --> new PHY");
2201 if (iface) {
2202 struct hostapd_config *conf;
2203 struct hostapd_bss_config **tmp_conf;
2204 struct hostapd_data **tmp_bss;
2205 struct hostapd_bss_config *bss;
2206 const char *ifname;
2207
2208 /* Add new BSS to existing iface */
2209 conf = interfaces->config_read_cb(config_fname);
2210 if (conf == NULL)
2211 return NULL;
2212 if (conf->num_bss > 1) {
2213 wpa_printf(MSG_ERROR, "Multiple BSSes specified in BSS-config");
2214 hostapd_config_free(conf);
2215 return NULL;
2216 }
2217
2218 ifname = conf->bss[0]->iface;
2219 if (ifname[0] != '\0' && ifname_in_use(interfaces, ifname)) {
2220 wpa_printf(MSG_ERROR,
2221 "Interface name %s already in use", ifname);
2222 hostapd_config_free(conf);
2223 return NULL;
2224 }
2225
2226 tmp_conf = os_realloc_array(
2227 iface->conf->bss, iface->conf->num_bss + 1,
2228 sizeof(struct hostapd_bss_config *));
2229 tmp_bss = os_realloc_array(iface->bss, iface->num_bss + 1,
2230 sizeof(struct hostapd_data *));
2231 if (tmp_bss)
2232 iface->bss = tmp_bss;
2233 if (tmp_conf) {
2234 iface->conf->bss = tmp_conf;
2235 iface->conf->last_bss = tmp_conf[0];
2236 }
2237 if (tmp_bss == NULL || tmp_conf == NULL) {
2238 hostapd_config_free(conf);
2239 return NULL;
2240 }
2241 bss = iface->conf->bss[iface->conf->num_bss] = conf->bss[0];
2242 iface->conf->num_bss++;
2243
2244 hapd = hostapd_alloc_bss_data(iface, iface->conf, bss);
2245 if (hapd == NULL) {
2246 iface->conf->num_bss--;
2247 hostapd_config_free(conf);
2248 return NULL;
2249 }
2250 iface->conf->last_bss = bss;
2251 iface->bss[iface->num_bss] = hapd;
2252 hapd->msg_ctx = hapd;
2253
2254 bss_idx = iface->num_bss++;
2255 conf->num_bss--;
2256 conf->bss[0] = NULL;
2257 hostapd_config_free(conf);
2258 } else {
2259 /* Add a new iface with the first BSS */
2260 new_iface = iface = hostapd_init(interfaces, config_fname);
2261 if (!iface)
2262 return NULL;
2263 os_strlcpy(iface->phy, phy, sizeof(iface->phy));
2264 iface->interfaces = interfaces;
2265 bss_idx = 0;
2266 }
2267
2268 for (k = 0; k < debug; k++) {
2269 if (iface->bss[bss_idx]->conf->logger_stdout_level > 0)
2270 iface->bss[bss_idx]->conf->logger_stdout_level--;
2271 }
2272
2273 if (iface->conf->bss[bss_idx]->iface[0] == '\0' &&
2274 !hostapd_drv_none(iface->bss[bss_idx])) {
2275 wpa_printf(MSG_ERROR, "Interface name not specified in %s",
2276 config_fname);
2277 if (new_iface)
2278 hostapd_interface_deinit_free(new_iface);
2279 return NULL;
2280 }
2281
2282 return iface;
2283}
2284
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002285
2286void hostapd_interface_deinit_free(struct hostapd_iface *iface)
2287{
2288 const struct wpa_driver_ops *driver;
2289 void *drv_priv;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002290
2291 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002292 if (iface == NULL)
2293 return;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002294 wpa_printf(MSG_DEBUG, "%s: num_bss=%u conf->num_bss=%u",
2295 __func__, (unsigned int) iface->num_bss,
2296 (unsigned int) iface->conf->num_bss);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002297 driver = iface->bss[0]->driver;
2298 drv_priv = iface->bss[0]->drv_priv;
2299 hostapd_interface_deinit(iface);
Dmitry Shmidt54605472013-11-08 11:10:19 -08002300 wpa_printf(MSG_DEBUG, "%s: driver=%p drv_priv=%p -> hapd_deinit",
2301 __func__, driver, drv_priv);
Dmitry Shmidt71757432014-06-02 13:50:35 -07002302 if (driver && driver->hapd_deinit && drv_priv) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002303 driver->hapd_deinit(drv_priv);
Dmitry Shmidt71757432014-06-02 13:50:35 -07002304 iface->bss[0]->drv_priv = NULL;
2305 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002306 hostapd_interface_free(iface);
2307}
2308
2309
Dmitry Shmidt15907092014-03-25 10:42:57 -07002310static void hostapd_deinit_driver(const struct wpa_driver_ops *driver,
2311 void *drv_priv,
2312 struct hostapd_iface *hapd_iface)
2313{
2314 size_t j;
2315
2316 wpa_printf(MSG_DEBUG, "%s: driver=%p drv_priv=%p -> hapd_deinit",
2317 __func__, driver, drv_priv);
2318 if (driver && driver->hapd_deinit && drv_priv) {
2319 driver->hapd_deinit(drv_priv);
2320 for (j = 0; j < hapd_iface->num_bss; j++) {
2321 wpa_printf(MSG_DEBUG, "%s:bss[%d]->drv_priv=%p",
2322 __func__, (int) j,
2323 hapd_iface->bss[j]->drv_priv);
2324 if (hapd_iface->bss[j]->drv_priv == drv_priv)
2325 hapd_iface->bss[j]->drv_priv = NULL;
2326 }
2327 }
2328}
2329
2330
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002331int hostapd_enable_iface(struct hostapd_iface *hapd_iface)
2332{
Dmitry Shmidt71757432014-06-02 13:50:35 -07002333 size_t j;
2334
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002335 if (hapd_iface->bss[0]->drv_priv != NULL) {
2336 wpa_printf(MSG_ERROR, "Interface %s already enabled",
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002337 hapd_iface->conf->bss[0]->iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002338 return -1;
2339 }
2340
2341 wpa_printf(MSG_DEBUG, "Enable interface %s",
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002342 hapd_iface->conf->bss[0]->iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002343
Dmitry Shmidt71757432014-06-02 13:50:35 -07002344 for (j = 0; j < hapd_iface->num_bss; j++)
2345 hostapd_set_security_params(hapd_iface->conf->bss[j], 1);
Dmitry Shmidt344abd32014-01-14 13:17:00 -08002346 if (hostapd_config_check(hapd_iface->conf, 1) < 0) {
2347 wpa_printf(MSG_INFO, "Invalid configuration - cannot enable");
2348 return -1;
2349 }
2350
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002351 if (hapd_iface->interfaces == NULL ||
2352 hapd_iface->interfaces->driver_init == NULL ||
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002353 hapd_iface->interfaces->driver_init(hapd_iface))
2354 return -1;
2355
2356 if (hostapd_setup_interface(hapd_iface)) {
Dmitry Shmidt15907092014-03-25 10:42:57 -07002357 hostapd_deinit_driver(hapd_iface->bss[0]->driver,
2358 hapd_iface->bss[0]->drv_priv,
2359 hapd_iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002360 return -1;
2361 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002362
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002363 return 0;
2364}
2365
2366
2367int hostapd_reload_iface(struct hostapd_iface *hapd_iface)
2368{
2369 size_t j;
2370
2371 wpa_printf(MSG_DEBUG, "Reload interface %s",
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002372 hapd_iface->conf->bss[0]->iface);
2373 for (j = 0; j < hapd_iface->num_bss; j++)
Dmitry Shmidt71757432014-06-02 13:50:35 -07002374 hostapd_set_security_params(hapd_iface->conf->bss[j], 1);
Dmitry Shmidt344abd32014-01-14 13:17:00 -08002375 if (hostapd_config_check(hapd_iface->conf, 1) < 0) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002376 wpa_printf(MSG_ERROR, "Updated configuration is invalid");
2377 return -1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002378 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002379 hostapd_clear_old(hapd_iface);
2380 for (j = 0; j < hapd_iface->num_bss; j++)
2381 hostapd_reload_bss(hapd_iface->bss[j]);
2382
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002383 return 0;
2384}
2385
2386
2387int hostapd_disable_iface(struct hostapd_iface *hapd_iface)
2388{
2389 size_t j;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002390 const struct wpa_driver_ops *driver;
2391 void *drv_priv;
2392
2393 if (hapd_iface == NULL)
2394 return -1;
Dmitry Shmidt71757432014-06-02 13:50:35 -07002395
2396 if (hapd_iface->bss[0]->drv_priv == NULL) {
2397 wpa_printf(MSG_INFO, "Interface %s already disabled",
2398 hapd_iface->conf->bss[0]->iface);
2399 return -1;
2400 }
2401
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002402 wpa_msg(hapd_iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002403 driver = hapd_iface->bss[0]->driver;
2404 drv_priv = hapd_iface->bss[0]->drv_priv;
2405
Dmitry Shmidta38abf92014-03-06 13:38:44 -08002406 hapd_iface->driver_ap_teardown =
2407 !!(hapd_iface->drv_flags &
2408 WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
2409
2410 /* same as hostapd_interface_deinit without deinitializing ctrl-iface */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002411 for (j = 0; j < hapd_iface->num_bss; j++) {
2412 struct hostapd_data *hapd = hapd_iface->bss[j];
Dmitry Shmidt71757432014-06-02 13:50:35 -07002413 hostapd_bss_deinit_no_free(hapd);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002414 hostapd_free_hapd_data(hapd);
2415 }
2416
Dmitry Shmidt15907092014-03-25 10:42:57 -07002417 hostapd_deinit_driver(driver, drv_priv, hapd_iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002418
2419 /* From hostapd_cleanup_iface: These were initialized in
2420 * hostapd_setup_interface and hostapd_setup_interface_complete
2421 */
2422 hostapd_cleanup_iface_partial(hapd_iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002423
Dmitry Shmidt56052862013-10-04 10:23:25 -07002424 wpa_printf(MSG_DEBUG, "Interface %s disabled",
2425 hapd_iface->bss[0]->conf->iface);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002426 hostapd_set_state(hapd_iface, HAPD_IFACE_DISABLED);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002427 return 0;
2428}
2429
2430
2431static struct hostapd_iface *
2432hostapd_iface_alloc(struct hapd_interfaces *interfaces)
2433{
2434 struct hostapd_iface **iface, *hapd_iface;
2435
2436 iface = os_realloc_array(interfaces->iface, interfaces->count + 1,
2437 sizeof(struct hostapd_iface *));
2438 if (iface == NULL)
2439 return NULL;
2440 interfaces->iface = iface;
2441 hapd_iface = interfaces->iface[interfaces->count] =
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07002442 hostapd_alloc_iface();
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002443 if (hapd_iface == NULL) {
2444 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory for "
2445 "the interface", __func__);
2446 return NULL;
2447 }
2448 interfaces->count++;
2449 hapd_iface->interfaces = interfaces;
2450
2451 return hapd_iface;
2452}
2453
2454
2455static struct hostapd_config *
2456hostapd_config_alloc(struct hapd_interfaces *interfaces, const char *ifname,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002457 const char *ctrl_iface, const char *driver)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002458{
2459 struct hostapd_bss_config *bss;
2460 struct hostapd_config *conf;
2461
2462 /* Allocates memory for bss and conf */
2463 conf = hostapd_config_defaults();
2464 if (conf == NULL) {
2465 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory for "
2466 "configuration", __func__);
2467 return NULL;
2468 }
2469
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002470 if (driver) {
2471 int j;
2472
2473 for (j = 0; wpa_drivers[j]; j++) {
2474 if (os_strcmp(driver, wpa_drivers[j]->name) == 0) {
2475 conf->driver = wpa_drivers[j];
2476 goto skip;
2477 }
2478 }
2479
2480 wpa_printf(MSG_ERROR,
2481 "Invalid/unknown driver '%s' - registering the default driver",
2482 driver);
2483 }
2484
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002485 conf->driver = wpa_drivers[0];
2486 if (conf->driver == NULL) {
2487 wpa_printf(MSG_ERROR, "No driver wrappers registered!");
2488 hostapd_config_free(conf);
2489 return NULL;
2490 }
2491
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002492skip:
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002493 bss = conf->last_bss = conf->bss[0];
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002494
2495 os_strlcpy(bss->iface, ifname, sizeof(bss->iface));
2496 bss->ctrl_interface = os_strdup(ctrl_iface);
2497 if (bss->ctrl_interface == NULL) {
2498 hostapd_config_free(conf);
2499 return NULL;
2500 }
2501
2502 /* Reading configuration file skipped, will be done in SET!
2503 * From reading the configuration till the end has to be done in
2504 * SET
2505 */
2506 return conf;
2507}
2508
2509
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002510static int hostapd_data_alloc(struct hostapd_iface *hapd_iface,
2511 struct hostapd_config *conf)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002512{
2513 size_t i;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002514 struct hostapd_data *hapd;
2515
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002516 hapd_iface->bss = os_calloc(conf->num_bss,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002517 sizeof(struct hostapd_data *));
2518 if (hapd_iface->bss == NULL)
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002519 return -1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002520
2521 for (i = 0; i < conf->num_bss; i++) {
2522 hapd = hapd_iface->bss[i] =
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002523 hostapd_alloc_bss_data(hapd_iface, conf, conf->bss[i]);
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002524 if (hapd == NULL) {
2525 while (i > 0) {
2526 i--;
2527 os_free(hapd_iface->bss[i]);
2528 hapd_iface->bss[i] = NULL;
2529 }
2530 os_free(hapd_iface->bss);
2531 hapd_iface->bss = NULL;
2532 return -1;
2533 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002534 hapd->msg_ctx = hapd;
2535 }
2536
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002537 hapd_iface->conf = conf;
2538 hapd_iface->num_bss = conf->num_bss;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002539
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002540 return 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002541}
2542
2543
2544int hostapd_add_iface(struct hapd_interfaces *interfaces, char *buf)
2545{
2546 struct hostapd_config *conf = NULL;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002547 struct hostapd_iface *hapd_iface = NULL, *new_iface = NULL;
2548 struct hostapd_data *hapd;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002549 char *ptr;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002550 size_t i, j;
2551 const char *conf_file = NULL, *phy_name = NULL;
2552
2553 if (os_strncmp(buf, "bss_config=", 11) == 0) {
2554 char *pos;
2555 phy_name = buf + 11;
2556 pos = os_strchr(phy_name, ':');
2557 if (!pos)
2558 return -1;
2559 *pos++ = '\0';
2560 conf_file = pos;
2561 if (!os_strlen(conf_file))
2562 return -1;
2563
2564 hapd_iface = hostapd_interface_init_bss(interfaces, phy_name,
2565 conf_file, 0);
2566 if (!hapd_iface)
2567 return -1;
2568 for (j = 0; j < interfaces->count; j++) {
2569 if (interfaces->iface[j] == hapd_iface)
2570 break;
2571 }
2572 if (j == interfaces->count) {
2573 struct hostapd_iface **tmp;
2574 tmp = os_realloc_array(interfaces->iface,
2575 interfaces->count + 1,
2576 sizeof(struct hostapd_iface *));
2577 if (!tmp) {
2578 hostapd_interface_deinit_free(hapd_iface);
2579 return -1;
2580 }
2581 interfaces->iface = tmp;
2582 interfaces->iface[interfaces->count++] = hapd_iface;
2583 new_iface = hapd_iface;
2584 }
2585
2586 if (new_iface) {
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002587 if (interfaces->driver_init(hapd_iface))
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002588 goto fail;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002589
2590 if (hostapd_setup_interface(hapd_iface)) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002591 hostapd_deinit_driver(
2592 hapd_iface->bss[0]->driver,
2593 hapd_iface->bss[0]->drv_priv,
2594 hapd_iface);
2595 goto fail;
2596 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002597 } else {
2598 /* Assign new BSS with bss[0]'s driver info */
2599 hapd = hapd_iface->bss[hapd_iface->num_bss - 1];
2600 hapd->driver = hapd_iface->bss[0]->driver;
2601 hapd->drv_priv = hapd_iface->bss[0]->drv_priv;
2602 os_memcpy(hapd->own_addr, hapd_iface->bss[0]->own_addr,
2603 ETH_ALEN);
2604
2605 if (start_ctrl_iface_bss(hapd) < 0 ||
Dmitry Shmidt54605472013-11-08 11:10:19 -08002606 (hapd_iface->state == HAPD_IFACE_ENABLED &&
2607 hostapd_setup_bss(hapd, -1))) {
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002608 hostapd_cleanup(hapd);
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08002609 hapd_iface->bss[hapd_iface->num_bss - 1] = NULL;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002610 hapd_iface->conf->num_bss--;
2611 hapd_iface->num_bss--;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002612 wpa_printf(MSG_DEBUG, "%s: free hapd %p %s",
2613 __func__, hapd, hapd->conf->iface);
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002614 hostapd_config_free_bss(hapd->conf);
2615 hapd->conf = NULL;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002616 os_free(hapd);
2617 return -1;
2618 }
2619 }
2620 return 0;
2621 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002622
2623 ptr = os_strchr(buf, ' ');
2624 if (ptr == NULL)
2625 return -1;
2626 *ptr++ = '\0';
2627
Dmitry Shmidt56052862013-10-04 10:23:25 -07002628 if (os_strncmp(ptr, "config=", 7) == 0)
2629 conf_file = ptr + 7;
2630
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002631 for (i = 0; i < interfaces->count; i++) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002632 if (!os_strcmp(interfaces->iface[i]->conf->bss[0]->iface,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002633 buf)) {
2634 wpa_printf(MSG_INFO, "Cannot add interface - it "
2635 "already exists");
2636 return -1;
2637 }
2638 }
2639
2640 hapd_iface = hostapd_iface_alloc(interfaces);
2641 if (hapd_iface == NULL) {
2642 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
2643 "for interface", __func__);
2644 goto fail;
2645 }
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002646 new_iface = hapd_iface;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002647
Dmitry Shmidt56052862013-10-04 10:23:25 -07002648 if (conf_file && interfaces->config_read_cb) {
2649 conf = interfaces->config_read_cb(conf_file);
2650 if (conf && conf->bss)
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002651 os_strlcpy(conf->bss[0]->iface, buf,
2652 sizeof(conf->bss[0]->iface));
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002653 } else {
2654 char *driver = os_strchr(ptr, ' ');
2655
2656 if (driver)
2657 *driver++ = '\0';
2658 conf = hostapd_config_alloc(interfaces, buf, ptr, driver);
2659 }
2660
Dmitry Shmidt56052862013-10-04 10:23:25 -07002661 if (conf == NULL || conf->bss == NULL) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002662 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
2663 "for configuration", __func__);
2664 goto fail;
2665 }
2666
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002667 if (hostapd_data_alloc(hapd_iface, conf) < 0) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002668 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
2669 "for hostapd", __func__);
2670 goto fail;
2671 }
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002672 conf = NULL;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002673
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002674 if (start_ctrl_iface(hapd_iface) < 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002675 goto fail;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002676
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002677 wpa_printf(MSG_INFO, "Add interface '%s'",
2678 hapd_iface->conf->bss[0]->iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002679
2680 return 0;
2681
2682fail:
2683 if (conf)
2684 hostapd_config_free(conf);
2685 if (hapd_iface) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002686 if (hapd_iface->bss) {
Dmitry Shmidt54605472013-11-08 11:10:19 -08002687 for (i = 0; i < hapd_iface->num_bss; i++) {
2688 hapd = hapd_iface->bss[i];
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08002689 if (!hapd)
2690 continue;
2691 if (hapd_iface->interfaces &&
Dmitry Shmidt54605472013-11-08 11:10:19 -08002692 hapd_iface->interfaces->ctrl_iface_deinit)
2693 hapd_iface->interfaces->
2694 ctrl_iface_deinit(hapd);
2695 wpa_printf(MSG_DEBUG, "%s: free hapd %p (%s)",
2696 __func__, hapd_iface->bss[i],
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08002697 hapd->conf->iface);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002698 hostapd_cleanup(hapd);
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08002699 os_free(hapd);
2700 hapd_iface->bss[i] = NULL;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002701 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002702 os_free(hapd_iface->bss);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002703 hapd_iface->bss = NULL;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002704 }
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002705 if (new_iface) {
2706 interfaces->count--;
2707 interfaces->iface[interfaces->count] = NULL;
2708 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002709 hostapd_cleanup_iface(hapd_iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002710 }
2711 return -1;
2712}
2713
2714
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002715static int hostapd_remove_bss(struct hostapd_iface *iface, unsigned int idx)
2716{
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002717 size_t i;
2718
Dmitry Shmidt54605472013-11-08 11:10:19 -08002719 wpa_printf(MSG_INFO, "Remove BSS '%s'", iface->conf->bss[idx]->iface);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002720
Dmitry Shmidt54605472013-11-08 11:10:19 -08002721 /* Remove hostapd_data only if it has already been initialized */
2722 if (idx < iface->num_bss) {
2723 struct hostapd_data *hapd = iface->bss[idx];
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002724
Dmitry Shmidt54605472013-11-08 11:10:19 -08002725 hostapd_bss_deinit(hapd);
2726 wpa_printf(MSG_DEBUG, "%s: free hapd %p (%s)",
2727 __func__, hapd, hapd->conf->iface);
2728 hostapd_config_free_bss(hapd->conf);
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002729 hapd->conf = NULL;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002730 os_free(hapd);
2731
2732 iface->num_bss--;
2733
2734 for (i = idx; i < iface->num_bss; i++)
2735 iface->bss[i] = iface->bss[i + 1];
2736 } else {
2737 hostapd_config_free_bss(iface->conf->bss[idx]);
2738 iface->conf->bss[idx] = NULL;
2739 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002740
2741 iface->conf->num_bss--;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002742 for (i = idx; i < iface->conf->num_bss; i++)
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002743 iface->conf->bss[i] = iface->conf->bss[i + 1];
2744
2745 return 0;
2746}
2747
2748
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002749int hostapd_remove_iface(struct hapd_interfaces *interfaces, char *buf)
2750{
2751 struct hostapd_iface *hapd_iface;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002752 size_t i, j, k = 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002753
2754 for (i = 0; i < interfaces->count; i++) {
2755 hapd_iface = interfaces->iface[i];
2756 if (hapd_iface == NULL)
2757 return -1;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002758 if (!os_strcmp(hapd_iface->conf->bss[0]->iface, buf)) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002759 wpa_printf(MSG_INFO, "Remove interface '%s'", buf);
Dmitry Shmidta38abf92014-03-06 13:38:44 -08002760 hapd_iface->driver_ap_teardown =
2761 !!(hapd_iface->drv_flags &
2762 WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
2763
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002764 hostapd_interface_deinit_free(hapd_iface);
2765 k = i;
2766 while (k < (interfaces->count - 1)) {
2767 interfaces->iface[k] =
2768 interfaces->iface[k + 1];
2769 k++;
2770 }
2771 interfaces->count--;
2772 return 0;
2773 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002774
2775 for (j = 0; j < hapd_iface->conf->num_bss; j++) {
Dmitry Shmidta38abf92014-03-06 13:38:44 -08002776 if (!os_strcmp(hapd_iface->conf->bss[j]->iface, buf)) {
2777 hapd_iface->driver_ap_teardown =
2778 !(hapd_iface->drv_flags &
2779 WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002780 return hostapd_remove_bss(hapd_iface, j);
Dmitry Shmidta38abf92014-03-06 13:38:44 -08002781 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002782 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002783 }
2784 return -1;
2785}
2786
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002787
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002788/**
2789 * hostapd_new_assoc_sta - Notify that a new station associated with the AP
2790 * @hapd: Pointer to BSS data
2791 * @sta: Pointer to the associated STA data
2792 * @reassoc: 1 to indicate this was a re-association; 0 = first association
2793 *
2794 * This function will be called whenever a station associates with the AP. It
2795 * can be called from ieee802_11.c for drivers that export MLME to hostapd and
2796 * from drv_callbacks.c based on driver events for drivers that take care of
2797 * management frames (IEEE 802.11 authentication and association) internally.
2798 */
2799void hostapd_new_assoc_sta(struct hostapd_data *hapd, struct sta_info *sta,
2800 int reassoc)
2801{
2802 if (hapd->tkip_countermeasures) {
2803 hostapd_drv_sta_deauth(hapd, sta->addr,
2804 WLAN_REASON_MICHAEL_MIC_FAILURE);
2805 return;
2806 }
2807
2808 hostapd_prune_associations(hapd, sta->addr);
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -08002809 ap_sta_clear_disconnect_timeouts(hapd, sta);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002810
2811 /* IEEE 802.11F (IAPP) */
2812 if (hapd->conf->ieee802_11f)
2813 iapp_new_station(hapd->iapp, sta);
2814
2815#ifdef CONFIG_P2P
2816 if (sta->p2p_ie == NULL && !sta->no_p2p_set) {
2817 sta->no_p2p_set = 1;
2818 hapd->num_sta_no_p2p++;
2819 if (hapd->num_sta_no_p2p == 1)
2820 hostapd_p2p_non_p2p_sta_connected(hapd);
2821 }
2822#endif /* CONFIG_P2P */
2823
2824 /* Start accounting here, if IEEE 802.1X and WPA are not used.
2825 * IEEE 802.1X/WPA code will start accounting after the station has
2826 * been authorized. */
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -08002827 if (!hapd->conf->ieee802_1x && !hapd->conf->wpa && !hapd->conf->osen) {
2828 ap_sta_set_authorized(hapd, sta, 1);
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002829 os_get_reltime(&sta->connected_time);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002830 accounting_sta_start(hapd, sta);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002831 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002832
2833 /* Start IEEE 802.1X authentication process for new stations */
2834 ieee802_1x_new_station(hapd, sta);
2835 if (reassoc) {
2836 if (sta->auth_alg != WLAN_AUTH_FT &&
2837 !(sta->flags & (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS)))
2838 wpa_auth_sm_event(sta->wpa_sm, WPA_REAUTH);
2839 } else
2840 wpa_auth_sta_associated(hapd->wpa_auth, sta->wpa_sm);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002841
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08002842 if (hapd->iface->drv_flags & WPA_DRIVER_FLAGS_WIRED) {
2843 if (eloop_cancel_timeout(ap_handle_timer, hapd, sta) > 0) {
2844 wpa_printf(MSG_DEBUG,
2845 "%s: %s: canceled wired ap_handle_timer timeout for "
2846 MACSTR,
2847 hapd->conf->iface, __func__,
2848 MAC2STR(sta->addr));
2849 }
2850 } else if (!(hapd->iface->drv_flags &
2851 WPA_DRIVER_FLAGS_INACTIVITY_TIMER)) {
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -08002852 wpa_printf(MSG_DEBUG,
2853 "%s: %s: reschedule ap_handle_timer timeout for "
2854 MACSTR " (%d seconds - ap_max_inactivity)",
2855 hapd->conf->iface, __func__, MAC2STR(sta->addr),
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08002856 hapd->conf->ap_max_inactivity);
2857 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
2858 eloop_register_timeout(hapd->conf->ap_max_inactivity, 0,
2859 ap_handle_timer, hapd, sta);
2860 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002861}
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002862
2863
2864const char * hostapd_state_text(enum hostapd_iface_state s)
2865{
2866 switch (s) {
2867 case HAPD_IFACE_UNINITIALIZED:
2868 return "UNINITIALIZED";
2869 case HAPD_IFACE_DISABLED:
2870 return "DISABLED";
2871 case HAPD_IFACE_COUNTRY_UPDATE:
2872 return "COUNTRY_UPDATE";
2873 case HAPD_IFACE_ACS:
2874 return "ACS";
2875 case HAPD_IFACE_HT_SCAN:
2876 return "HT_SCAN";
2877 case HAPD_IFACE_DFS:
2878 return "DFS";
2879 case HAPD_IFACE_ENABLED:
2880 return "ENABLED";
2881 }
2882
2883 return "UNKNOWN";
2884}
2885
2886
2887void hostapd_set_state(struct hostapd_iface *iface, enum hostapd_iface_state s)
2888{
2889 wpa_printf(MSG_INFO, "%s: interface state %s->%s",
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07002890 iface->conf ? iface->conf->bss[0]->iface : "N/A",
2891 hostapd_state_text(iface->state), hostapd_state_text(s));
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002892 iface->state = s;
2893}
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002894
2895
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002896int hostapd_csa_in_progress(struct hostapd_iface *iface)
2897{
2898 unsigned int i;
2899
2900 for (i = 0; i < iface->num_bss; i++)
2901 if (iface->bss[i]->csa_in_progress)
2902 return 1;
2903 return 0;
2904}
2905
2906
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002907#ifdef NEED_AP_MLME
2908
2909static void free_beacon_data(struct beacon_data *beacon)
2910{
2911 os_free(beacon->head);
2912 beacon->head = NULL;
2913 os_free(beacon->tail);
2914 beacon->tail = NULL;
2915 os_free(beacon->probe_resp);
2916 beacon->probe_resp = NULL;
2917 os_free(beacon->beacon_ies);
2918 beacon->beacon_ies = NULL;
2919 os_free(beacon->proberesp_ies);
2920 beacon->proberesp_ies = NULL;
2921 os_free(beacon->assocresp_ies);
2922 beacon->assocresp_ies = NULL;
2923}
2924
2925
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07002926static int hostapd_build_beacon_data(struct hostapd_data *hapd,
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002927 struct beacon_data *beacon)
2928{
2929 struct wpabuf *beacon_extra, *proberesp_extra, *assocresp_extra;
2930 struct wpa_driver_ap_params params;
2931 int ret;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002932
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08002933 os_memset(beacon, 0, sizeof(*beacon));
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002934 ret = ieee802_11_build_ap_params(hapd, &params);
2935 if (ret < 0)
2936 return ret;
2937
2938 ret = hostapd_build_ap_extra_ies(hapd, &beacon_extra,
2939 &proberesp_extra,
2940 &assocresp_extra);
2941 if (ret)
2942 goto free_ap_params;
2943
2944 ret = -1;
2945 beacon->head = os_malloc(params.head_len);
2946 if (!beacon->head)
2947 goto free_ap_extra_ies;
2948
2949 os_memcpy(beacon->head, params.head, params.head_len);
2950 beacon->head_len = params.head_len;
2951
2952 beacon->tail = os_malloc(params.tail_len);
2953 if (!beacon->tail)
2954 goto free_beacon;
2955
2956 os_memcpy(beacon->tail, params.tail, params.tail_len);
2957 beacon->tail_len = params.tail_len;
2958
2959 if (params.proberesp != NULL) {
2960 beacon->probe_resp = os_malloc(params.proberesp_len);
2961 if (!beacon->probe_resp)
2962 goto free_beacon;
2963
2964 os_memcpy(beacon->probe_resp, params.proberesp,
2965 params.proberesp_len);
2966 beacon->probe_resp_len = params.proberesp_len;
2967 }
2968
2969 /* copy the extra ies */
2970 if (beacon_extra) {
2971 beacon->beacon_ies = os_malloc(wpabuf_len(beacon_extra));
2972 if (!beacon->beacon_ies)
2973 goto free_beacon;
2974
2975 os_memcpy(beacon->beacon_ies,
2976 beacon_extra->buf, wpabuf_len(beacon_extra));
2977 beacon->beacon_ies_len = wpabuf_len(beacon_extra);
2978 }
2979
2980 if (proberesp_extra) {
2981 beacon->proberesp_ies =
2982 os_malloc(wpabuf_len(proberesp_extra));
2983 if (!beacon->proberesp_ies)
2984 goto free_beacon;
2985
2986 os_memcpy(beacon->proberesp_ies, proberesp_extra->buf,
2987 wpabuf_len(proberesp_extra));
2988 beacon->proberesp_ies_len = wpabuf_len(proberesp_extra);
2989 }
2990
2991 if (assocresp_extra) {
2992 beacon->assocresp_ies =
2993 os_malloc(wpabuf_len(assocresp_extra));
2994 if (!beacon->assocresp_ies)
2995 goto free_beacon;
2996
2997 os_memcpy(beacon->assocresp_ies, assocresp_extra->buf,
2998 wpabuf_len(assocresp_extra));
2999 beacon->assocresp_ies_len = wpabuf_len(assocresp_extra);
3000 }
3001
3002 ret = 0;
3003free_beacon:
3004 /* if the function fails, the caller should not free beacon data */
3005 if (ret)
3006 free_beacon_data(beacon);
3007
3008free_ap_extra_ies:
3009 hostapd_free_ap_extra_ies(hapd, beacon_extra, proberesp_extra,
3010 assocresp_extra);
3011free_ap_params:
3012 ieee802_11_free_ap_params(&params);
3013 return ret;
3014}
3015
3016
3017/*
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003018 * TODO: This flow currently supports only changing channel and width within
3019 * the same hw_mode. Any other changes to MAC parameters or provided settings
3020 * are not supported.
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003021 */
3022static int hostapd_change_config_freq(struct hostapd_data *hapd,
3023 struct hostapd_config *conf,
3024 struct hostapd_freq_params *params,
3025 struct hostapd_freq_params *old_params)
3026{
3027 int channel;
3028
3029 if (!params->channel) {
3030 /* check if the new channel is supported by hw */
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003031 params->channel = hostapd_hw_get_channel(hapd, params->freq);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003032 }
3033
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003034 channel = params->channel;
3035 if (!channel)
3036 return -1;
3037
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003038 /* if a pointer to old_params is provided we save previous state */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003039 if (old_params &&
3040 hostapd_set_freq_params(old_params, conf->hw_mode,
3041 hostapd_hw_get_freq(hapd, conf->channel),
3042 conf->channel, conf->ieee80211n,
3043 conf->ieee80211ac,
3044 conf->secondary_channel,
3045 conf->vht_oper_chwidth,
3046 conf->vht_oper_centr_freq_seg0_idx,
3047 conf->vht_oper_centr_freq_seg1_idx,
3048 conf->vht_capab))
3049 return -1;
3050
3051 switch (params->bandwidth) {
3052 case 0:
3053 case 20:
3054 case 40:
3055 conf->vht_oper_chwidth = VHT_CHANWIDTH_USE_HT;
3056 break;
3057 case 80:
3058 if (params->center_freq2)
3059 conf->vht_oper_chwidth = VHT_CHANWIDTH_80P80MHZ;
3060 else
3061 conf->vht_oper_chwidth = VHT_CHANWIDTH_80MHZ;
3062 break;
3063 case 160:
3064 conf->vht_oper_chwidth = VHT_CHANWIDTH_160MHZ;
3065 break;
3066 default:
3067 return -1;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003068 }
3069
3070 conf->channel = channel;
3071 conf->ieee80211n = params->ht_enabled;
3072 conf->secondary_channel = params->sec_channel_offset;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003073 ieee80211_freq_to_chan(params->center_freq1,
3074 &conf->vht_oper_centr_freq_seg0_idx);
3075 ieee80211_freq_to_chan(params->center_freq2,
3076 &conf->vht_oper_centr_freq_seg1_idx);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003077
3078 /* TODO: maybe call here hostapd_config_check here? */
3079
3080 return 0;
3081}
3082
3083
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003084static int hostapd_fill_csa_settings(struct hostapd_data *hapd,
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003085 struct csa_settings *settings)
3086{
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003087 struct hostapd_iface *iface = hapd->iface;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003088 struct hostapd_freq_params old_freq;
3089 int ret;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003090 u8 chan, vht_bandwidth;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003091
3092 os_memset(&old_freq, 0, sizeof(old_freq));
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003093 if (!iface || !iface->freq || hapd->csa_in_progress)
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003094 return -1;
3095
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003096 switch (settings->freq_params.bandwidth) {
3097 case 80:
3098 if (settings->freq_params.center_freq2)
3099 vht_bandwidth = VHT_CHANWIDTH_80P80MHZ;
3100 else
3101 vht_bandwidth = VHT_CHANWIDTH_80MHZ;
3102 break;
3103 case 160:
3104 vht_bandwidth = VHT_CHANWIDTH_160MHZ;
3105 break;
3106 default:
3107 vht_bandwidth = VHT_CHANWIDTH_USE_HT;
3108 break;
3109 }
3110
3111 if (ieee80211_freq_to_channel_ext(
3112 settings->freq_params.freq,
3113 settings->freq_params.sec_channel_offset,
3114 vht_bandwidth,
3115 &hapd->iface->cs_oper_class,
3116 &chan) == NUM_HOSTAPD_MODES) {
3117 wpa_printf(MSG_DEBUG,
3118 "invalid frequency for channel switch (freq=%d, sec_channel_offset=%d, vht_enabled=%d)",
3119 settings->freq_params.freq,
3120 settings->freq_params.sec_channel_offset,
3121 settings->freq_params.vht_enabled);
3122 return -1;
3123 }
3124
3125 settings->freq_params.channel = chan;
3126
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003127 ret = hostapd_change_config_freq(iface->bss[0], iface->conf,
3128 &settings->freq_params,
3129 &old_freq);
3130 if (ret)
3131 return ret;
3132
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003133 ret = hostapd_build_beacon_data(hapd, &settings->beacon_after);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003134
3135 /* change back the configuration */
3136 hostapd_change_config_freq(iface->bss[0], iface->conf,
3137 &old_freq, NULL);
3138
3139 if (ret)
3140 return ret;
3141
3142 /* set channel switch parameters for csa ie */
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003143 hapd->cs_freq_params = settings->freq_params;
3144 hapd->cs_count = settings->cs_count;
3145 hapd->cs_block_tx = settings->block_tx;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003146
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003147 ret = hostapd_build_beacon_data(hapd, &settings->beacon_csa);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003148 if (ret) {
3149 free_beacon_data(&settings->beacon_after);
3150 return ret;
3151 }
3152
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003153 settings->counter_offset_beacon[0] = hapd->cs_c_off_beacon;
3154 settings->counter_offset_presp[0] = hapd->cs_c_off_proberesp;
3155 settings->counter_offset_beacon[1] = hapd->cs_c_off_ecsa_beacon;
3156 settings->counter_offset_presp[1] = hapd->cs_c_off_ecsa_proberesp;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003157
3158 return 0;
3159}
3160
3161
3162void hostapd_cleanup_cs_params(struct hostapd_data *hapd)
3163{
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003164 os_memset(&hapd->cs_freq_params, 0, sizeof(hapd->cs_freq_params));
3165 hapd->cs_count = 0;
3166 hapd->cs_block_tx = 0;
3167 hapd->cs_c_off_beacon = 0;
3168 hapd->cs_c_off_proberesp = 0;
3169 hapd->csa_in_progress = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003170 hapd->cs_c_off_ecsa_beacon = 0;
3171 hapd->cs_c_off_ecsa_proberesp = 0;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003172}
3173
3174
3175int hostapd_switch_channel(struct hostapd_data *hapd,
3176 struct csa_settings *settings)
3177{
3178 int ret;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003179
3180 if (!(hapd->iface->drv_flags & WPA_DRIVER_FLAGS_AP_CSA)) {
3181 wpa_printf(MSG_INFO, "CSA is not supported");
3182 return -1;
3183 }
3184
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003185 ret = hostapd_fill_csa_settings(hapd, settings);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003186 if (ret)
3187 return ret;
3188
3189 ret = hostapd_drv_switch_channel(hapd, settings);
3190 free_beacon_data(&settings->beacon_csa);
3191 free_beacon_data(&settings->beacon_after);
3192
3193 if (ret) {
3194 /* if we failed, clean cs parameters */
3195 hostapd_cleanup_cs_params(hapd);
3196 return ret;
3197 }
3198
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003199 hapd->csa_in_progress = 1;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003200 return 0;
3201}
3202
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003203
3204void
3205hostapd_switch_channel_fallback(struct hostapd_iface *iface,
3206 const struct hostapd_freq_params *freq_params)
3207{
3208 int vht_seg0_idx = 0, vht_seg1_idx = 0, vht_bw = VHT_CHANWIDTH_USE_HT;
3209 unsigned int i;
3210
3211 wpa_printf(MSG_DEBUG, "Restarting all CSA-related BSSes");
3212
3213 if (freq_params->center_freq1)
3214 vht_seg0_idx = 36 + (freq_params->center_freq1 - 5180) / 5;
3215 if (freq_params->center_freq2)
3216 vht_seg1_idx = 36 + (freq_params->center_freq2 - 5180) / 5;
3217
3218 switch (freq_params->bandwidth) {
3219 case 0:
3220 case 20:
3221 case 40:
3222 vht_bw = VHT_CHANWIDTH_USE_HT;
3223 break;
3224 case 80:
3225 if (freq_params->center_freq2)
3226 vht_bw = VHT_CHANWIDTH_80P80MHZ;
3227 else
3228 vht_bw = VHT_CHANWIDTH_80MHZ;
3229 break;
3230 case 160:
3231 vht_bw = VHT_CHANWIDTH_160MHZ;
3232 break;
3233 default:
3234 wpa_printf(MSG_WARNING, "Unknown CSA bandwidth: %d",
3235 freq_params->bandwidth);
3236 break;
3237 }
3238
3239 iface->freq = freq_params->freq;
3240 iface->conf->channel = freq_params->channel;
3241 iface->conf->secondary_channel = freq_params->sec_channel_offset;
3242 iface->conf->vht_oper_centr_freq_seg0_idx = vht_seg0_idx;
3243 iface->conf->vht_oper_centr_freq_seg1_idx = vht_seg1_idx;
3244 iface->conf->vht_oper_chwidth = vht_bw;
3245 iface->conf->ieee80211n = freq_params->ht_enabled;
3246 iface->conf->ieee80211ac = freq_params->vht_enabled;
3247
3248 /*
3249 * cs_params must not be cleared earlier because the freq_params
3250 * argument may actually point to one of these.
3251 */
3252 for (i = 0; i < iface->num_bss; i++)
3253 hostapd_cleanup_cs_params(iface->bss[i]);
3254
3255 hostapd_disable_iface(iface);
3256 hostapd_enable_iface(iface);
3257}
3258
Dmitry Shmidte4663042016-04-04 10:07:49 -07003259#endif /* NEED_AP_MLME */
3260
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003261
3262struct hostapd_data * hostapd_get_iface(struct hapd_interfaces *interfaces,
3263 const char *ifname)
3264{
3265 size_t i, j;
3266
3267 for (i = 0; i < interfaces->count; i++) {
3268 struct hostapd_iface *iface = interfaces->iface[i];
3269
3270 for (j = 0; j < iface->num_bss; j++) {
3271 struct hostapd_data *hapd = iface->bss[j];
3272
3273 if (os_strcmp(ifname, hapd->conf->iface) == 0)
3274 return hapd;
3275 }
3276 }
3277
3278 return NULL;
3279}
3280
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003281
3282void hostapd_periodic_iface(struct hostapd_iface *iface)
3283{
3284 size_t i;
3285
3286 ap_list_timer(iface);
3287
3288 for (i = 0; i < iface->num_bss; i++) {
3289 struct hostapd_data *hapd = iface->bss[i];
3290
3291 if (!hapd->started)
3292 continue;
3293
3294#ifndef CONFIG_NO_RADIUS
3295 hostapd_acl_expire(hapd);
3296#endif /* CONFIG_NO_RADIUS */
3297 }
3298}