blob: cf8a8cb37316fdee05556aa55244c67755af23e7 [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 Shmidtebd93af2017-02-21 13:40:44 -080048#include "fils_hlp.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070049
50
Dmitry Shmidt04949592012-07-19 12:16:46 -070051static int hostapd_flush_old_stations(struct hostapd_data *hapd, u16 reason);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070052static int hostapd_setup_encryption(char *iface, struct hostapd_data *hapd);
Dmitry Shmidtc55524a2011-07-07 11:18:38 -070053static int hostapd_broadcast_wep_clear(struct hostapd_data *hapd);
Dmitry Shmidtcce06662013-11-04 18:44:24 -080054static int setup_interface2(struct hostapd_iface *iface);
55static void channel_list_update_timeout(void *eloop_ctx, void *timeout_ctx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070056
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070057
Dmitry Shmidt04949592012-07-19 12:16:46 -070058int hostapd_for_each_interface(struct hapd_interfaces *interfaces,
59 int (*cb)(struct hostapd_iface *iface,
60 void *ctx), void *ctx)
61{
62 size_t i;
63 int ret;
64
65 for (i = 0; i < interfaces->count; i++) {
66 ret = cb(interfaces->iface[i], ctx);
67 if (ret)
68 return ret;
69 }
70
71 return 0;
72}
73
74
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070075static void hostapd_reload_bss(struct hostapd_data *hapd)
76{
Dmitry Shmidtcce06662013-11-04 18:44:24 -080077 struct hostapd_ssid *ssid;
78
Dmitry Shmidt29333592017-01-09 12:27:11 -080079 if (!hapd->started)
80 return;
81
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070082#ifndef CONFIG_NO_RADIUS
83 radius_client_reconfig(hapd->radius, hapd->conf->radius);
84#endif /* CONFIG_NO_RADIUS */
85
Dmitry Shmidtcce06662013-11-04 18:44:24 -080086 ssid = &hapd->conf->ssid;
87 if (!ssid->wpa_psk_set && ssid->wpa_psk && !ssid->wpa_psk->next &&
88 ssid->wpa_passphrase_set && ssid->wpa_passphrase) {
89 /*
90 * Force PSK to be derived again since SSID or passphrase may
91 * have changed.
92 */
Dmitry Shmidt7f656022015-02-25 14:36:37 -080093 hostapd_config_clear_wpa_psk(&hapd->conf->ssid.wpa_psk);
Dmitry Shmidtcce06662013-11-04 18:44:24 -080094 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070095 if (hostapd_setup_wpa_psk(hapd->conf)) {
96 wpa_printf(MSG_ERROR, "Failed to re-configure WPA PSK "
97 "after reloading configuration");
98 }
99
100 if (hapd->conf->ieee802_1x || hapd->conf->wpa)
101 hostapd_set_drv_ieee8021x(hapd, hapd->conf->iface, 1);
102 else
103 hostapd_set_drv_ieee8021x(hapd, hapd->conf->iface, 0);
104
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800105 if ((hapd->conf->wpa || hapd->conf->osen) && hapd->wpa_auth == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700106 hostapd_setup_wpa(hapd);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800107 if (hapd->wpa_auth)
108 wpa_init_keys(hapd->wpa_auth);
109 } else if (hapd->conf->wpa) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700110 const u8 *wpa_ie;
111 size_t wpa_ie_len;
112 hostapd_reconfig_wpa(hapd);
113 wpa_ie = wpa_auth_get_wpa_ie(hapd->wpa_auth, &wpa_ie_len);
114 if (hostapd_set_generic_elem(hapd, wpa_ie, wpa_ie_len))
115 wpa_printf(MSG_ERROR, "Failed to configure WPA IE for "
116 "the kernel driver.");
117 } else if (hapd->wpa_auth) {
118 wpa_deinit(hapd->wpa_auth);
119 hapd->wpa_auth = NULL;
120 hostapd_set_privacy(hapd, 0);
121 hostapd_setup_encryption(hapd->conf->iface, hapd);
122 hostapd_set_generic_elem(hapd, (u8 *) "", 0);
123 }
124
125 ieee802_11_set_beacon(hapd);
126 hostapd_update_wps(hapd);
127
128 if (hapd->conf->ssid.ssid_set &&
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700129 hostapd_set_ssid(hapd, hapd->conf->ssid.ssid,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700130 hapd->conf->ssid.ssid_len)) {
131 wpa_printf(MSG_ERROR, "Could not set SSID for kernel driver");
132 /* try to continue */
133 }
134 wpa_printf(MSG_DEBUG, "Reconfigured interface %s", hapd->conf->iface);
135}
136
137
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700138static void hostapd_clear_old(struct hostapd_iface *iface)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700139{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700140 size_t j;
141
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700142 /*
143 * Deauthenticate all stations since the new configuration may not
144 * allow them to use the BSS anymore.
145 */
146 for (j = 0; j < iface->num_bss; j++) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700147 hostapd_flush_old_stations(iface->bss[j],
148 WLAN_REASON_PREV_AUTH_NOT_VALID);
Dmitry Shmidtc55524a2011-07-07 11:18:38 -0700149 hostapd_broadcast_wep_clear(iface->bss[j]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700150
151#ifndef CONFIG_NO_RADIUS
152 /* TODO: update dynamic data based on changed configuration
153 * items (e.g., open/close sockets, etc.) */
154 radius_client_flush(iface->bss[j]->radius, 0);
155#endif /* CONFIG_NO_RADIUS */
156 }
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700157}
158
159
160int hostapd_reload_config(struct hostapd_iface *iface)
161{
162 struct hostapd_data *hapd = iface->bss[0];
163 struct hostapd_config *newconf, *oldconf;
164 size_t j;
165
166 if (iface->config_fname == NULL) {
167 /* Only in-memory config in use - assume it has been updated */
168 hostapd_clear_old(iface);
169 for (j = 0; j < iface->num_bss; j++)
170 hostapd_reload_bss(iface->bss[j]);
171 return 0;
172 }
173
174 if (iface->interfaces == NULL ||
175 iface->interfaces->config_read_cb == NULL)
176 return -1;
177 newconf = iface->interfaces->config_read_cb(iface->config_fname);
178 if (newconf == NULL)
179 return -1;
180
181 hostapd_clear_old(iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700182
183 oldconf = hapd->iconf;
184 iface->conf = newconf;
185
186 for (j = 0; j < iface->num_bss; j++) {
187 hapd = iface->bss[j];
188 hapd->iconf = newconf;
Dmitry Shmidtd11f0192014-03-24 12:09:47 -0700189 hapd->iconf->channel = oldconf->channel;
Dmitry Shmidtdda10c22015-03-24 16:05:01 -0700190 hapd->iconf->acs = oldconf->acs;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -0700191 hapd->iconf->secondary_channel = oldconf->secondary_channel;
Dmitry Shmidtd11f0192014-03-24 12:09:47 -0700192 hapd->iconf->ieee80211n = oldconf->ieee80211n;
193 hapd->iconf->ieee80211ac = oldconf->ieee80211ac;
194 hapd->iconf->ht_capab = oldconf->ht_capab;
195 hapd->iconf->vht_capab = oldconf->vht_capab;
196 hapd->iconf->vht_oper_chwidth = oldconf->vht_oper_chwidth;
197 hapd->iconf->vht_oper_centr_freq_seg0_idx =
198 oldconf->vht_oper_centr_freq_seg0_idx;
199 hapd->iconf->vht_oper_centr_freq_seg1_idx =
200 oldconf->vht_oper_centr_freq_seg1_idx;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800201 hapd->conf = newconf->bss[j];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700202 hostapd_reload_bss(hapd);
203 }
204
205 hostapd_config_free(oldconf);
206
207
208 return 0;
209}
210
211
212static void hostapd_broadcast_key_clear_iface(struct hostapd_data *hapd,
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700213 const char *ifname)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700214{
215 int i;
216
Dmitry Shmidt29333592017-01-09 12:27:11 -0800217 if (!ifname || !hapd->drv_priv)
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700218 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700219 for (i = 0; i < NUM_WEP_KEYS; i++) {
220 if (hostapd_drv_set_key(ifname, hapd, WPA_ALG_NONE, NULL, i,
221 0, NULL, 0, NULL, 0)) {
222 wpa_printf(MSG_DEBUG, "Failed to clear default "
223 "encryption keys (ifname=%s keyidx=%d)",
224 ifname, i);
225 }
226 }
227#ifdef CONFIG_IEEE80211W
228 if (hapd->conf->ieee80211w) {
229 for (i = NUM_WEP_KEYS; i < NUM_WEP_KEYS + 2; i++) {
230 if (hostapd_drv_set_key(ifname, hapd, WPA_ALG_NONE,
231 NULL, i, 0, NULL,
232 0, NULL, 0)) {
233 wpa_printf(MSG_DEBUG, "Failed to clear "
234 "default mgmt encryption keys "
235 "(ifname=%s keyidx=%d)", ifname, i);
236 }
237 }
238 }
239#endif /* CONFIG_IEEE80211W */
240}
241
242
243static int hostapd_broadcast_wep_clear(struct hostapd_data *hapd)
244{
245 hostapd_broadcast_key_clear_iface(hapd, hapd->conf->iface);
246 return 0;
247}
248
249
250static int hostapd_broadcast_wep_set(struct hostapd_data *hapd)
251{
252 int errors = 0, idx;
253 struct hostapd_ssid *ssid = &hapd->conf->ssid;
254
255 idx = ssid->wep.idx;
256 if (ssid->wep.default_len &&
257 hostapd_drv_set_key(hapd->conf->iface,
258 hapd, WPA_ALG_WEP, broadcast_ether_addr, idx,
259 1, NULL, 0, ssid->wep.key[idx],
260 ssid->wep.len[idx])) {
261 wpa_printf(MSG_WARNING, "Could not set WEP encryption.");
262 errors++;
263 }
264
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700265 return errors;
266}
267
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700268
Dmitry Shmidt04949592012-07-19 12:16:46 -0700269static void hostapd_free_hapd_data(struct hostapd_data *hapd)
270{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800271 os_free(hapd->probereq_cb);
272 hapd->probereq_cb = NULL;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800273 hapd->num_probereq_cb = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800274
275#ifdef CONFIG_P2P
276 wpabuf_free(hapd->p2p_beacon_ie);
277 hapd->p2p_beacon_ie = NULL;
278 wpabuf_free(hapd->p2p_probe_resp_ie);
279 hapd->p2p_probe_resp_ie = NULL;
280#endif /* CONFIG_P2P */
281
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -0800282 if (!hapd->started) {
283 wpa_printf(MSG_ERROR, "%s: Interface %s wasn't started",
284 __func__, hapd->conf->iface);
285 return;
286 }
287 hapd->started = 0;
288
Dmitry Shmidt54605472013-11-08 11:10:19 -0800289 wpa_printf(MSG_DEBUG, "%s(%s)", __func__, hapd->conf->iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700290 iapp_deinit(hapd->iapp);
291 hapd->iapp = NULL;
292 accounting_deinit(hapd);
293 hostapd_deinit_wpa(hapd);
294 vlan_deinit(hapd);
295 hostapd_acl_deinit(hapd);
296#ifndef CONFIG_NO_RADIUS
297 radius_client_deinit(hapd->radius);
298 hapd->radius = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700299 radius_das_deinit(hapd->radius_das);
300 hapd->radius_das = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700301#endif /* CONFIG_NO_RADIUS */
302
303 hostapd_deinit_wps(hapd);
304
305 authsrv_deinit(hapd);
306
Dmitry Shmidt71757432014-06-02 13:50:35 -0700307 if (hapd->interface_added) {
308 hapd->interface_added = 0;
309 if (hostapd_if_remove(hapd, WPA_IF_AP_BSS, hapd->conf->iface)) {
310 wpa_printf(MSG_WARNING,
311 "Failed to remove BSS interface %s",
312 hapd->conf->iface);
313 hapd->interface_added = 1;
314 } else {
315 /*
316 * Since this was a dynamically added interface, the
317 * driver wrapper may have removed its internal instance
318 * and hapd->drv_priv is not valid anymore.
319 */
320 hapd->drv_priv = NULL;
321 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700322 }
323
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800324 wpabuf_free(hapd->time_adv);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700325
326#ifdef CONFIG_INTERWORKING
327 gas_serv_deinit(hapd);
328#endif /* CONFIG_INTERWORKING */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800329
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800330 bss_load_update_deinit(hapd);
331 ndisc_snoop_deinit(hapd);
332 dhcp_snoop_deinit(hapd);
333 x_snoop_deinit(hapd);
334
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800335#ifdef CONFIG_SQLITE
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700336 bin_clear_free(hapd->tmp_eap_user.identity,
337 hapd->tmp_eap_user.identity_len);
338 bin_clear_free(hapd->tmp_eap_user.password,
339 hapd->tmp_eap_user.password_len);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800340#endif /* CONFIG_SQLITE */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800341
342#ifdef CONFIG_MESH
343 wpabuf_free(hapd->mesh_pending_auth);
344 hapd->mesh_pending_auth = NULL;
345#endif /* CONFIG_MESH */
Dmitry Shmidt849734c2016-05-27 09:59:01 -0700346
347 hostapd_clean_rrm(hapd);
Dmitry Shmidtebd93af2017-02-21 13:40:44 -0800348 fils_hlp_deinit(hapd);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700349}
350
351
352/**
353 * hostapd_cleanup - Per-BSS cleanup (deinitialization)
354 * @hapd: Pointer to BSS data
355 *
356 * This function is used to free all per-BSS data structures and resources.
Dmitry Shmidt54605472013-11-08 11:10:19 -0800357 * Most of the modules that are initialized in hostapd_setup_bss() are
358 * deinitialized here.
Dmitry Shmidt04949592012-07-19 12:16:46 -0700359 */
360static void hostapd_cleanup(struct hostapd_data *hapd)
361{
Dmitry Shmidt54605472013-11-08 11:10:19 -0800362 wpa_printf(MSG_DEBUG, "%s(hapd=%p (%s))", __func__, hapd,
363 hapd->conf->iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700364 if (hapd->iface->interfaces &&
Dmitry Shmidt29333592017-01-09 12:27:11 -0800365 hapd->iface->interfaces->ctrl_iface_deinit) {
366 wpa_msg(hapd->msg_ctx, MSG_INFO, WPA_EVENT_TERMINATING);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700367 hapd->iface->interfaces->ctrl_iface_deinit(hapd);
Dmitry Shmidt29333592017-01-09 12:27:11 -0800368 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700369 hostapd_free_hapd_data(hapd);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700370}
371
372
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800373static void sta_track_deinit(struct hostapd_iface *iface)
374{
375 struct hostapd_sta_info *info;
376
377 if (!iface->num_sta_seen)
378 return;
379
380 while ((info = dl_list_first(&iface->sta_seen, struct hostapd_sta_info,
381 list))) {
382 dl_list_del(&info->list);
383 iface->num_sta_seen--;
Dmitry Shmidtaca489e2016-09-28 15:44:14 -0700384 sta_track_del(info);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800385 }
386}
387
388
Dmitry Shmidt04949592012-07-19 12:16:46 -0700389static void hostapd_cleanup_iface_partial(struct hostapd_iface *iface)
390{
Dmitry Shmidt54605472013-11-08 11:10:19 -0800391 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800392#ifdef CONFIG_IEEE80211N
393#ifdef NEED_AP_MLME
394 hostapd_stop_setup_timers(iface);
395#endif /* NEED_AP_MLME */
396#endif /* CONFIG_IEEE80211N */
Dmitry Shmidt04949592012-07-19 12:16:46 -0700397 hostapd_free_hw_features(iface->hw_features, iface->num_hw_features);
398 iface->hw_features = NULL;
399 os_free(iface->current_rates);
400 iface->current_rates = NULL;
401 os_free(iface->basic_rates);
402 iface->basic_rates = NULL;
403 ap_list_deinit(iface);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800404 sta_track_deinit(iface);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700405}
406
407
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700408/**
409 * hostapd_cleanup_iface - Complete per-interface cleanup
410 * @iface: Pointer to interface data
411 *
412 * This function is called after per-BSS data structures are deinitialized
413 * with hostapd_cleanup().
414 */
415static void hostapd_cleanup_iface(struct hostapd_iface *iface)
416{
Dmitry Shmidt54605472013-11-08 11:10:19 -0800417 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800418 eloop_cancel_timeout(channel_list_update_timeout, iface, NULL);
419
Dmitry Shmidt04949592012-07-19 12:16:46 -0700420 hostapd_cleanup_iface_partial(iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700421 hostapd_config_free(iface->conf);
422 iface->conf = NULL;
423
424 os_free(iface->config_fname);
425 os_free(iface->bss);
Dmitry Shmidt54605472013-11-08 11:10:19 -0800426 wpa_printf(MSG_DEBUG, "%s: free iface=%p", __func__, iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700427 os_free(iface);
428}
429
430
Dmitry Shmidt04949592012-07-19 12:16:46 -0700431static void hostapd_clear_wep(struct hostapd_data *hapd)
432{
Dmitry Shmidta38abf92014-03-06 13:38:44 -0800433 if (hapd->drv_priv && !hapd->iface->driver_ap_teardown) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700434 hostapd_set_privacy(hapd, 0);
435 hostapd_broadcast_wep_clear(hapd);
436 }
437}
438
439
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700440static int hostapd_setup_encryption(char *iface, struct hostapd_data *hapd)
441{
442 int i;
443
444 hostapd_broadcast_wep_set(hapd);
445
446 if (hapd->conf->ssid.wep.default_len) {
447 hostapd_set_privacy(hapd, 1);
448 return 0;
449 }
450
Jouni Malinen75ecf522011-06-27 15:19:46 -0700451 /*
452 * When IEEE 802.1X is not enabled, the driver may need to know how to
453 * set authentication algorithms for static WEP.
454 */
455 hostapd_drv_set_authmode(hapd, hapd->conf->auth_algs);
456
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700457 for (i = 0; i < 4; i++) {
458 if (hapd->conf->ssid.wep.key[i] &&
459 hostapd_drv_set_key(iface, hapd, WPA_ALG_WEP, NULL, i,
460 i == hapd->conf->ssid.wep.idx, NULL, 0,
461 hapd->conf->ssid.wep.key[i],
462 hapd->conf->ssid.wep.len[i])) {
463 wpa_printf(MSG_WARNING, "Could not set WEP "
464 "encryption.");
465 return -1;
466 }
467 if (hapd->conf->ssid.wep.key[i] &&
468 i == hapd->conf->ssid.wep.idx)
469 hostapd_set_privacy(hapd, 1);
470 }
471
472 return 0;
473}
474
475
Dmitry Shmidt04949592012-07-19 12:16:46 -0700476static int hostapd_flush_old_stations(struct hostapd_data *hapd, u16 reason)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700477{
478 int ret = 0;
479 u8 addr[ETH_ALEN];
480
481 if (hostapd_drv_none(hapd) || hapd->drv_priv == NULL)
482 return 0;
483
Dmitry Shmidta38abf92014-03-06 13:38:44 -0800484 if (!hapd->iface->driver_ap_teardown) {
485 wpa_dbg(hapd->msg_ctx, MSG_DEBUG,
486 "Flushing old station entries");
487
488 if (hostapd_flush(hapd)) {
489 wpa_msg(hapd->msg_ctx, MSG_WARNING,
490 "Could not connect to kernel driver");
491 ret = -1;
492 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700493 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800494 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "Deauthenticate all stations");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700495 os_memset(addr, 0xff, ETH_ALEN);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700496 hostapd_drv_sta_deauth(hapd, addr, reason);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700497 hostapd_free_stas(hapd);
498
499 return ret;
500}
501
502
Dmitry Shmidt71757432014-06-02 13:50:35 -0700503static void hostapd_bss_deinit_no_free(struct hostapd_data *hapd)
504{
505 hostapd_free_stas(hapd);
506 hostapd_flush_old_stations(hapd, WLAN_REASON_DEAUTH_LEAVING);
507 hostapd_clear_wep(hapd);
508}
509
510
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700511/**
512 * hostapd_validate_bssid_configuration - Validate BSSID configuration
513 * @iface: Pointer to interface data
514 * Returns: 0 on success, -1 on failure
515 *
516 * This function is used to validate that the configured BSSIDs are valid.
517 */
518static int hostapd_validate_bssid_configuration(struct hostapd_iface *iface)
519{
520 u8 mask[ETH_ALEN] = { 0 };
521 struct hostapd_data *hapd = iface->bss[0];
522 unsigned int i = iface->conf->num_bss, bits = 0, j;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700523 int auto_addr = 0;
524
525 if (hostapd_drv_none(hapd))
526 return 0;
527
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -0800528 if (iface->conf->use_driver_iface_addr)
529 return 0;
530
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700531 /* Generate BSSID mask that is large enough to cover the BSSIDs. */
532
533 /* Determine the bits necessary to cover the number of BSSIDs. */
534 for (i--; i; i >>= 1)
535 bits++;
536
537 /* Determine the bits necessary to any configured BSSIDs,
538 if they are higher than the number of BSSIDs. */
539 for (j = 0; j < iface->conf->num_bss; j++) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800540 if (is_zero_ether_addr(iface->conf->bss[j]->bssid)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700541 if (j)
542 auto_addr++;
543 continue;
544 }
545
546 for (i = 0; i < ETH_ALEN; i++) {
547 mask[i] |=
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800548 iface->conf->bss[j]->bssid[i] ^
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700549 hapd->own_addr[i];
550 }
551 }
552
553 if (!auto_addr)
554 goto skip_mask_ext;
555
556 for (i = 0; i < ETH_ALEN && mask[i] == 0; i++)
557 ;
558 j = 0;
559 if (i < ETH_ALEN) {
560 j = (5 - i) * 8;
561
562 while (mask[i] != 0) {
563 mask[i] >>= 1;
564 j++;
565 }
566 }
567
568 if (bits < j)
569 bits = j;
570
571 if (bits > 40) {
572 wpa_printf(MSG_ERROR, "Too many bits in the BSSID mask (%u)",
573 bits);
574 return -1;
575 }
576
577 os_memset(mask, 0xff, ETH_ALEN);
578 j = bits / 8;
579 for (i = 5; i > 5 - j; i--)
580 mask[i] = 0;
581 j = bits % 8;
582 while (j--)
583 mask[i] <<= 1;
584
585skip_mask_ext:
586 wpa_printf(MSG_DEBUG, "BSS count %lu, BSSID mask " MACSTR " (%d bits)",
587 (unsigned long) iface->conf->num_bss, MAC2STR(mask), bits);
588
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700589 if (!auto_addr)
590 return 0;
591
592 for (i = 0; i < ETH_ALEN; i++) {
593 if ((hapd->own_addr[i] & mask[i]) != hapd->own_addr[i]) {
594 wpa_printf(MSG_ERROR, "Invalid BSSID mask " MACSTR
595 " for start address " MACSTR ".",
596 MAC2STR(mask), MAC2STR(hapd->own_addr));
597 wpa_printf(MSG_ERROR, "Start address must be the "
598 "first address in the block (i.e., addr "
599 "AND mask == addr).");
600 return -1;
601 }
602 }
603
604 return 0;
605}
606
607
608static int mac_in_conf(struct hostapd_config *conf, const void *a)
609{
610 size_t i;
611
612 for (i = 0; i < conf->num_bss; i++) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800613 if (hostapd_mac_comp(conf->bss[i]->bssid, a) == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700614 return 1;
615 }
616 }
617
618 return 0;
619}
620
621
Dmitry Shmidt04949592012-07-19 12:16:46 -0700622#ifndef CONFIG_NO_RADIUS
623
624static int hostapd_das_nas_mismatch(struct hostapd_data *hapd,
625 struct radius_das_attrs *attr)
626{
Dmitry Shmidt13ca8d82014-02-20 10:18:40 -0800627 if (attr->nas_identifier &&
628 (!hapd->conf->nas_identifier ||
629 os_strlen(hapd->conf->nas_identifier) !=
630 attr->nas_identifier_len ||
631 os_memcmp(hapd->conf->nas_identifier, attr->nas_identifier,
632 attr->nas_identifier_len) != 0)) {
633 wpa_printf(MSG_DEBUG, "RADIUS DAS: NAS-Identifier mismatch");
634 return 1;
635 }
636
637 if (attr->nas_ip_addr &&
638 (hapd->conf->own_ip_addr.af != AF_INET ||
639 os_memcmp(&hapd->conf->own_ip_addr.u.v4, attr->nas_ip_addr, 4) !=
640 0)) {
641 wpa_printf(MSG_DEBUG, "RADIUS DAS: NAS-IP-Address mismatch");
642 return 1;
643 }
644
645#ifdef CONFIG_IPV6
646 if (attr->nas_ipv6_addr &&
647 (hapd->conf->own_ip_addr.af != AF_INET6 ||
648 os_memcmp(&hapd->conf->own_ip_addr.u.v6, attr->nas_ipv6_addr, 16)
649 != 0)) {
650 wpa_printf(MSG_DEBUG, "RADIUS DAS: NAS-IPv6-Address mismatch");
651 return 1;
652 }
653#endif /* CONFIG_IPV6 */
654
Dmitry Shmidt04949592012-07-19 12:16:46 -0700655 return 0;
656}
657
658
659static struct sta_info * hostapd_das_find_sta(struct hostapd_data *hapd,
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800660 struct radius_das_attrs *attr,
661 int *multi)
Dmitry Shmidt04949592012-07-19 12:16:46 -0700662{
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800663 struct sta_info *selected, *sta;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700664 char buf[128];
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800665 int num_attr = 0;
666 int count;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700667
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800668 *multi = 0;
669
670 for (sta = hapd->sta_list; sta; sta = sta->next)
671 sta->radius_das_match = 1;
672
673 if (attr->sta_addr) {
674 num_attr++;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700675 sta = ap_get_sta(hapd, attr->sta_addr);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800676 if (!sta) {
677 wpa_printf(MSG_DEBUG,
678 "RADIUS DAS: No Calling-Station-Id match");
679 return NULL;
680 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700681
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800682 selected = sta;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700683 for (sta = hapd->sta_list; sta; sta = sta->next) {
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800684 if (sta != selected)
685 sta->radius_das_match = 0;
686 }
687 wpa_printf(MSG_DEBUG, "RADIUS DAS: Calling-Station-Id match");
688 }
689
690 if (attr->acct_session_id) {
691 num_attr++;
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800692 if (attr->acct_session_id_len != 16) {
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800693 wpa_printf(MSG_DEBUG,
694 "RADIUS DAS: Acct-Session-Id cannot match");
695 return NULL;
696 }
697 count = 0;
698
699 for (sta = hapd->sta_list; sta; sta = sta->next) {
700 if (!sta->radius_das_match)
701 continue;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800702 os_snprintf(buf, sizeof(buf), "%016llX",
703 (unsigned long long) sta->acct_session_id);
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800704 if (os_memcmp(attr->acct_session_id, buf, 16) != 0)
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800705 sta->radius_das_match = 0;
706 else
707 count++;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700708 }
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800709
710 if (count == 0) {
711 wpa_printf(MSG_DEBUG,
712 "RADIUS DAS: No matches remaining after Acct-Session-Id check");
713 return NULL;
714 }
715 wpa_printf(MSG_DEBUG, "RADIUS DAS: Acct-Session-Id match");
Dmitry Shmidt04949592012-07-19 12:16:46 -0700716 }
717
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800718 if (attr->acct_multi_session_id) {
719 num_attr++;
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800720 if (attr->acct_multi_session_id_len != 16) {
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800721 wpa_printf(MSG_DEBUG,
722 "RADIUS DAS: Acct-Multi-Session-Id cannot match");
723 return NULL;
724 }
725 count = 0;
726
727 for (sta = hapd->sta_list; sta; sta = sta->next) {
728 if (!sta->radius_das_match)
729 continue;
730 if (!sta->eapol_sm ||
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800731 !sta->eapol_sm->acct_multi_session_id) {
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800732 sta->radius_das_match = 0;
733 continue;
734 }
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800735 os_snprintf(buf, sizeof(buf), "%016llX",
736 (unsigned long long)
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800737 sta->eapol_sm->acct_multi_session_id);
738 if (os_memcmp(attr->acct_multi_session_id, buf, 16) !=
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800739 0)
740 sta->radius_das_match = 0;
741 else
742 count++;
743 }
744
745 if (count == 0) {
746 wpa_printf(MSG_DEBUG,
747 "RADIUS DAS: No matches remaining after Acct-Multi-Session-Id check");
748 return NULL;
749 }
750 wpa_printf(MSG_DEBUG,
751 "RADIUS DAS: Acct-Multi-Session-Id match");
752 }
753
754 if (attr->cui) {
755 num_attr++;
756 count = 0;
757
Dmitry Shmidt04949592012-07-19 12:16:46 -0700758 for (sta = hapd->sta_list; sta; sta = sta->next) {
759 struct wpabuf *cui;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800760
761 if (!sta->radius_das_match)
762 continue;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700763 cui = ieee802_1x_get_radius_cui(sta->eapol_sm);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800764 if (!cui || wpabuf_len(cui) != attr->cui_len ||
Dmitry Shmidt04949592012-07-19 12:16:46 -0700765 os_memcmp(wpabuf_head(cui), attr->cui,
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800766 attr->cui_len) != 0)
767 sta->radius_das_match = 0;
768 else
769 count++;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700770 }
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800771
772 if (count == 0) {
773 wpa_printf(MSG_DEBUG,
774 "RADIUS DAS: No matches remaining after Chargeable-User-Identity check");
775 return NULL;
776 }
777 wpa_printf(MSG_DEBUG,
778 "RADIUS DAS: Chargeable-User-Identity match");
Dmitry Shmidt04949592012-07-19 12:16:46 -0700779 }
780
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800781 if (attr->user_name) {
782 num_attr++;
783 count = 0;
784
Dmitry Shmidt04949592012-07-19 12:16:46 -0700785 for (sta = hapd->sta_list; sta; sta = sta->next) {
786 u8 *identity;
787 size_t identity_len;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800788
789 if (!sta->radius_das_match)
790 continue;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700791 identity = ieee802_1x_get_identity(sta->eapol_sm,
792 &identity_len);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800793 if (!identity ||
794 identity_len != attr->user_name_len ||
Dmitry Shmidt04949592012-07-19 12:16:46 -0700795 os_memcmp(identity, attr->user_name, identity_len)
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800796 != 0)
797 sta->radius_das_match = 0;
798 else
799 count++;
800 }
801
802 if (count == 0) {
803 wpa_printf(MSG_DEBUG,
804 "RADIUS DAS: No matches remaining after User-Name check");
805 return NULL;
806 }
807 wpa_printf(MSG_DEBUG,
808 "RADIUS DAS: User-Name match");
809 }
810
811 if (num_attr == 0) {
812 /*
813 * In theory, we could match all current associations, but it
814 * seems safer to just reject requests that do not include any
815 * session identification attributes.
816 */
817 wpa_printf(MSG_DEBUG,
818 "RADIUS DAS: No session identification attributes included");
819 return NULL;
820 }
821
822 selected = NULL;
823 for (sta = hapd->sta_list; sta; sta = sta->next) {
824 if (sta->radius_das_match) {
825 if (selected) {
826 *multi = 1;
827 return NULL;
828 }
829 selected = sta;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700830 }
831 }
832
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800833 return selected;
834}
835
836
837static int hostapd_das_disconnect_pmksa(struct hostapd_data *hapd,
838 struct radius_das_attrs *attr)
839{
840 if (!hapd->wpa_auth)
841 return -1;
842 return wpa_auth_radius_das_disconnect_pmksa(hapd->wpa_auth, attr);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700843}
844
845
846static enum radius_das_res
847hostapd_das_disconnect(void *ctx, struct radius_das_attrs *attr)
848{
849 struct hostapd_data *hapd = ctx;
850 struct sta_info *sta;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800851 int multi;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700852
853 if (hostapd_das_nas_mismatch(hapd, attr))
854 return RADIUS_DAS_NAS_MISMATCH;
855
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800856 sta = hostapd_das_find_sta(hapd, attr, &multi);
857 if (sta == NULL) {
858 if (multi) {
859 wpa_printf(MSG_DEBUG,
860 "RADIUS DAS: Multiple sessions match - not supported");
861 return RADIUS_DAS_MULTI_SESSION_MATCH;
862 }
863 if (hostapd_das_disconnect_pmksa(hapd, attr) == 0) {
864 wpa_printf(MSG_DEBUG,
865 "RADIUS DAS: PMKSA cache entry matched");
866 return RADIUS_DAS_SUCCESS;
867 }
868 wpa_printf(MSG_DEBUG, "RADIUS DAS: No matching session found");
Dmitry Shmidt04949592012-07-19 12:16:46 -0700869 return RADIUS_DAS_SESSION_NOT_FOUND;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800870 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700871
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800872 wpa_printf(MSG_DEBUG, "RADIUS DAS: Found a matching session " MACSTR
873 " - disconnecting", MAC2STR(sta->addr));
Dmitry Shmidt13ca8d82014-02-20 10:18:40 -0800874 wpa_auth_pmksa_remove(hapd->wpa_auth, sta->addr);
875
Dmitry Shmidt04949592012-07-19 12:16:46 -0700876 hostapd_drv_sta_deauth(hapd, sta->addr,
877 WLAN_REASON_PREV_AUTH_NOT_VALID);
878 ap_sta_deauthenticate(hapd, sta, WLAN_REASON_PREV_AUTH_NOT_VALID);
879
880 return RADIUS_DAS_SUCCESS;
881}
882
883#endif /* CONFIG_NO_RADIUS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700884
885
886/**
887 * hostapd_setup_bss - Per-BSS setup (initialization)
888 * @hapd: Pointer to BSS data
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800889 * @first: Whether this BSS is the first BSS of an interface; -1 = not first,
890 * but interface may exist
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700891 *
892 * This function is used to initialize all per-BSS data structures and
893 * resources. This gets called in a loop for each BSS when an interface is
894 * initialized. Most of the modules that are initialized here will be
895 * deinitialized in hostapd_cleanup().
896 */
897static int hostapd_setup_bss(struct hostapd_data *hapd, int first)
898{
899 struct hostapd_bss_config *conf = hapd->conf;
Dmitry Shmidt9d9e6022015-04-23 10:34:55 -0700900 u8 ssid[SSID_MAX_LEN + 1];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700901 int ssid_len, set_ssid;
902 char force_ifname[IFNAMSIZ];
903 u8 if_addr[ETH_ALEN];
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800904 int flush_old_stations = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700905
Dmitry Shmidt54605472013-11-08 11:10:19 -0800906 wpa_printf(MSG_DEBUG, "%s(hapd=%p (%s), first=%d)",
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700907 __func__, hapd, conf->iface, first);
Dmitry Shmidt54605472013-11-08 11:10:19 -0800908
Dmitry Shmidt50b691d2014-05-21 14:01:45 -0700909#ifdef EAP_SERVER_TNC
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700910 if (conf->tnc && tncs_global_init() < 0) {
Dmitry Shmidt50b691d2014-05-21 14:01:45 -0700911 wpa_printf(MSG_ERROR, "Failed to initialize TNCS");
912 return -1;
913 }
914#endif /* EAP_SERVER_TNC */
915
Dmitry Shmidt54605472013-11-08 11:10:19 -0800916 if (hapd->started) {
917 wpa_printf(MSG_ERROR, "%s: Interface %s was already started",
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700918 __func__, conf->iface);
Dmitry Shmidt54605472013-11-08 11:10:19 -0800919 return -1;
920 }
921 hapd->started = 1;
922
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800923 if (!first || first == -1) {
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -0800924 u8 *addr = hapd->own_addr;
925
926 if (!is_zero_ether_addr(conf->bssid)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700927 /* Allocate the configured BSSID. */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700928 os_memcpy(hapd->own_addr, conf->bssid, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700929
930 if (hostapd_mac_comp(hapd->own_addr,
931 hapd->iface->bss[0]->own_addr) ==
932 0) {
933 wpa_printf(MSG_ERROR, "BSS '%s' may not have "
934 "BSSID set to the MAC address of "
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700935 "the radio", conf->iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700936 return -1;
937 }
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -0800938 } else if (hapd->iconf->use_driver_iface_addr) {
939 addr = NULL;
940 } else {
941 /* Allocate the next available BSSID. */
942 do {
943 inc_byte_array(hapd->own_addr, ETH_ALEN);
944 } while (mac_in_conf(hapd->iconf, hapd->own_addr));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700945 }
946
Dmitry Shmidt54605472013-11-08 11:10:19 -0800947 hapd->interface_added = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700948 if (hostapd_if_add(hapd->iface->bss[0], WPA_IF_AP_BSS,
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -0800949 conf->iface, addr, hapd,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700950 &hapd->drv_priv, force_ifname, if_addr,
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700951 conf->bridge[0] ? conf->bridge : NULL,
952 first == -1)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700953 wpa_printf(MSG_ERROR, "Failed to add BSS (BSSID="
954 MACSTR ")", MAC2STR(hapd->own_addr));
Dmitry Shmidt3cf6f792013-12-18 13:12:19 -0800955 hapd->interface_added = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700956 return -1;
957 }
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -0800958
959 if (!addr)
960 os_memcpy(hapd->own_addr, if_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700961 }
962
963 if (conf->wmm_enabled < 0)
964 conf->wmm_enabled = hapd->iconf->ieee80211n;
965
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800966#ifdef CONFIG_IEEE80211R_AP
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800967 if (is_zero_ether_addr(conf->r1_key_holder))
968 os_memcpy(conf->r1_key_holder, hapd->own_addr, ETH_ALEN);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800969#endif /* CONFIG_IEEE80211R_AP */
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800970
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800971#ifdef CONFIG_MESH
972 if (hapd->iface->mconf == NULL)
973 flush_old_stations = 0;
974#endif /* CONFIG_MESH */
975
976 if (flush_old_stations)
977 hostapd_flush_old_stations(hapd,
978 WLAN_REASON_PREV_AUTH_NOT_VALID);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700979 hostapd_set_privacy(hapd, 0);
980
981 hostapd_broadcast_wep_clear(hapd);
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700982 if (hostapd_setup_encryption(conf->iface, hapd))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700983 return -1;
984
985 /*
986 * Fetch the SSID from the system and use it or,
987 * if one was specified in the config file, verify they
988 * match.
989 */
990 ssid_len = hostapd_get_ssid(hapd, ssid, sizeof(ssid));
991 if (ssid_len < 0) {
992 wpa_printf(MSG_ERROR, "Could not read SSID from system");
993 return -1;
994 }
995 if (conf->ssid.ssid_set) {
996 /*
997 * If SSID is specified in the config file and it differs
998 * from what is being used then force installation of the
999 * new SSID.
1000 */
1001 set_ssid = (conf->ssid.ssid_len != (size_t) ssid_len ||
1002 os_memcmp(conf->ssid.ssid, ssid, ssid_len) != 0);
1003 } else {
1004 /*
1005 * No SSID in the config file; just use the one we got
1006 * from the system.
1007 */
1008 set_ssid = 0;
1009 conf->ssid.ssid_len = ssid_len;
1010 os_memcpy(conf->ssid.ssid, ssid, conf->ssid.ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001011 }
1012
1013 if (!hostapd_drv_none(hapd)) {
1014 wpa_printf(MSG_ERROR, "Using interface %s with hwaddr " MACSTR
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001015 " and ssid \"%s\"",
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001016 conf->iface, MAC2STR(hapd->own_addr),
1017 wpa_ssid_txt(conf->ssid.ssid, conf->ssid.ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001018 }
1019
1020 if (hostapd_setup_wpa_psk(conf)) {
1021 wpa_printf(MSG_ERROR, "WPA-PSK setup failed.");
1022 return -1;
1023 }
1024
1025 /* Set SSID for the kernel driver (to be used in beacon and probe
1026 * response frames) */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001027 if (set_ssid && hostapd_set_ssid(hapd, conf->ssid.ssid,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001028 conf->ssid.ssid_len)) {
1029 wpa_printf(MSG_ERROR, "Could not set SSID for kernel driver");
1030 return -1;
1031 }
1032
Dmitry Shmidt818ea482014-03-10 13:15:21 -07001033 if (wpa_debug_level <= MSG_MSGDUMP)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001034 conf->radius->msg_dumps = 1;
1035#ifndef CONFIG_NO_RADIUS
1036 hapd->radius = radius_client_init(hapd, conf->radius);
1037 if (hapd->radius == NULL) {
1038 wpa_printf(MSG_ERROR, "RADIUS client initialization failed.");
1039 return -1;
1040 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001041
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001042 if (conf->radius_das_port) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001043 struct radius_das_conf das_conf;
1044 os_memset(&das_conf, 0, sizeof(das_conf));
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001045 das_conf.port = conf->radius_das_port;
1046 das_conf.shared_secret = conf->radius_das_shared_secret;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001047 das_conf.shared_secret_len =
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001048 conf->radius_das_shared_secret_len;
1049 das_conf.client_addr = &conf->radius_das_client_addr;
1050 das_conf.time_window = conf->radius_das_time_window;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001051 das_conf.require_event_timestamp =
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001052 conf->radius_das_require_event_timestamp;
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07001053 das_conf.require_message_authenticator =
1054 conf->radius_das_require_message_authenticator;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001055 das_conf.ctx = hapd;
1056 das_conf.disconnect = hostapd_das_disconnect;
1057 hapd->radius_das = radius_das_init(&das_conf);
1058 if (hapd->radius_das == NULL) {
1059 wpa_printf(MSG_ERROR, "RADIUS DAS initialization "
1060 "failed.");
1061 return -1;
1062 }
1063 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001064#endif /* CONFIG_NO_RADIUS */
1065
1066 if (hostapd_acl_init(hapd)) {
1067 wpa_printf(MSG_ERROR, "ACL initialization failed.");
1068 return -1;
1069 }
1070 if (hostapd_init_wps(hapd, conf))
1071 return -1;
1072
1073 if (authsrv_init(hapd) < 0)
1074 return -1;
1075
1076 if (ieee802_1x_init(hapd)) {
1077 wpa_printf(MSG_ERROR, "IEEE 802.1X initialization failed.");
1078 return -1;
1079 }
1080
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001081 if ((conf->wpa || conf->osen) && hostapd_setup_wpa(hapd))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001082 return -1;
1083
1084 if (accounting_init(hapd)) {
1085 wpa_printf(MSG_ERROR, "Accounting initialization failed.");
1086 return -1;
1087 }
1088
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001089 if (conf->ieee802_11f &&
1090 (hapd->iapp = iapp_init(hapd, conf->iapp_iface)) == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001091 wpa_printf(MSG_ERROR, "IEEE 802.11F (IAPP) initialization "
1092 "failed.");
1093 return -1;
1094 }
1095
Dmitry Shmidt04949592012-07-19 12:16:46 -07001096#ifdef CONFIG_INTERWORKING
1097 if (gas_serv_init(hapd)) {
1098 wpa_printf(MSG_ERROR, "GAS server initialization failed");
1099 return -1;
1100 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001101
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001102 if (conf->qos_map_set_len &&
1103 hostapd_drv_set_qos_map(hapd, conf->qos_map_set,
1104 conf->qos_map_set_len)) {
1105 wpa_printf(MSG_ERROR, "Failed to initialize QoS Map");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001106 return -1;
1107 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001108#endif /* CONFIG_INTERWORKING */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001109
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001110 if (conf->bss_load_update_period && bss_load_update_init(hapd)) {
1111 wpa_printf(MSG_ERROR, "BSS Load initialization failed");
1112 return -1;
1113 }
1114
1115 if (conf->proxy_arp) {
1116 if (x_snoop_init(hapd)) {
1117 wpa_printf(MSG_ERROR,
1118 "Generic snooping infrastructure initialization failed");
1119 return -1;
1120 }
1121
1122 if (dhcp_snoop_init(hapd)) {
1123 wpa_printf(MSG_ERROR,
1124 "DHCP snooping initialization failed");
1125 return -1;
1126 }
1127
1128 if (ndisc_snoop_init(hapd)) {
1129 wpa_printf(MSG_ERROR,
1130 "Neighbor Discovery snooping initialization failed");
1131 return -1;
1132 }
1133 }
1134
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001135 if (!hostapd_drv_none(hapd) && vlan_init(hapd)) {
1136 wpa_printf(MSG_ERROR, "VLAN initialization failed.");
1137 return -1;
1138 }
1139
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001140 if (!conf->start_disabled && ieee802_11_set_beacon(hapd) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001141 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001142
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001143 if (hapd->wpa_auth && wpa_init_keys(hapd->wpa_auth) < 0)
1144 return -1;
1145
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001146 if (hapd->driver && hapd->driver->set_operstate)
1147 hapd->driver->set_operstate(hapd->drv_priv, 1);
1148
1149 return 0;
1150}
1151
1152
1153static void hostapd_tx_queue_params(struct hostapd_iface *iface)
1154{
1155 struct hostapd_data *hapd = iface->bss[0];
1156 int i;
1157 struct hostapd_tx_queue_params *p;
1158
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001159#ifdef CONFIG_MESH
1160 if (iface->mconf == NULL)
1161 return;
1162#endif /* CONFIG_MESH */
1163
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001164 for (i = 0; i < NUM_TX_QUEUES; i++) {
1165 p = &iface->conf->tx_queue[i];
1166
1167 if (hostapd_set_tx_queue_params(hapd, i, p->aifs, p->cwmin,
1168 p->cwmax, p->burst)) {
1169 wpa_printf(MSG_DEBUG, "Failed to set TX queue "
1170 "parameters for queue %d.", i);
1171 /* Continue anyway */
1172 }
1173 }
1174}
1175
1176
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001177static int hostapd_set_acl_list(struct hostapd_data *hapd,
1178 struct mac_acl_entry *mac_acl,
1179 int n_entries, u8 accept_acl)
1180{
1181 struct hostapd_acl_params *acl_params;
1182 int i, err;
1183
1184 acl_params = os_zalloc(sizeof(*acl_params) +
1185 (n_entries * sizeof(acl_params->mac_acl[0])));
1186 if (!acl_params)
1187 return -ENOMEM;
1188
1189 for (i = 0; i < n_entries; i++)
1190 os_memcpy(acl_params->mac_acl[i].addr, mac_acl[i].addr,
1191 ETH_ALEN);
1192
1193 acl_params->acl_policy = accept_acl;
1194 acl_params->num_mac_acl = n_entries;
1195
1196 err = hostapd_drv_set_acl(hapd, acl_params);
1197
1198 os_free(acl_params);
1199
1200 return err;
1201}
1202
1203
1204static void hostapd_set_acl(struct hostapd_data *hapd)
1205{
1206 struct hostapd_config *conf = hapd->iconf;
1207 int err;
1208 u8 accept_acl;
1209
1210 if (hapd->iface->drv_max_acl_mac_addrs == 0)
1211 return;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001212
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001213 if (conf->bss[0]->macaddr_acl == DENY_UNLESS_ACCEPTED) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001214 accept_acl = 1;
1215 err = hostapd_set_acl_list(hapd, conf->bss[0]->accept_mac,
1216 conf->bss[0]->num_accept_mac,
1217 accept_acl);
1218 if (err) {
1219 wpa_printf(MSG_DEBUG, "Failed to set accept acl");
1220 return;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001221 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001222 } else if (conf->bss[0]->macaddr_acl == ACCEPT_UNLESS_DENIED) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001223 accept_acl = 0;
1224 err = hostapd_set_acl_list(hapd, conf->bss[0]->deny_mac,
1225 conf->bss[0]->num_deny_mac,
1226 accept_acl);
1227 if (err) {
1228 wpa_printf(MSG_DEBUG, "Failed to set deny acl");
1229 return;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001230 }
1231 }
1232}
1233
1234
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001235static int start_ctrl_iface_bss(struct hostapd_data *hapd)
1236{
1237 if (!hapd->iface->interfaces ||
1238 !hapd->iface->interfaces->ctrl_iface_init)
1239 return 0;
1240
1241 if (hapd->iface->interfaces->ctrl_iface_init(hapd)) {
1242 wpa_printf(MSG_ERROR,
1243 "Failed to setup control interface for %s",
1244 hapd->conf->iface);
1245 return -1;
1246 }
1247
1248 return 0;
1249}
1250
1251
1252static int start_ctrl_iface(struct hostapd_iface *iface)
1253{
1254 size_t i;
1255
1256 if (!iface->interfaces || !iface->interfaces->ctrl_iface_init)
1257 return 0;
1258
1259 for (i = 0; i < iface->num_bss; i++) {
1260 struct hostapd_data *hapd = iface->bss[i];
1261 if (iface->interfaces->ctrl_iface_init(hapd)) {
1262 wpa_printf(MSG_ERROR,
1263 "Failed to setup control interface for %s",
1264 hapd->conf->iface);
1265 return -1;
1266 }
1267 }
1268
1269 return 0;
1270}
1271
1272
1273static void channel_list_update_timeout(void *eloop_ctx, void *timeout_ctx)
1274{
1275 struct hostapd_iface *iface = eloop_ctx;
1276
1277 if (!iface->wait_channel_update) {
1278 wpa_printf(MSG_INFO, "Channel list update timeout, but interface was not waiting for it");
1279 return;
1280 }
1281
1282 /*
1283 * It is possible that the existing channel list is acceptable, so try
1284 * to proceed.
1285 */
1286 wpa_printf(MSG_DEBUG, "Channel list update timeout - try to continue anyway");
1287 setup_interface2(iface);
1288}
1289
1290
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001291void hostapd_channel_list_updated(struct hostapd_iface *iface, int initiator)
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001292{
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001293 if (!iface->wait_channel_update || initiator != REGDOM_SET_BY_USER)
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001294 return;
1295
1296 wpa_printf(MSG_DEBUG, "Channel list updated - continue setup");
1297 eloop_cancel_timeout(channel_list_update_timeout, iface, NULL);
1298 setup_interface2(iface);
1299}
1300
1301
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001302static int setup_interface(struct hostapd_iface *iface)
1303{
1304 struct hostapd_data *hapd = iface->bss[0];
1305 size_t i;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001306
Dmitry Shmidta38abf92014-03-06 13:38:44 -08001307 /*
1308 * It is possible that setup_interface() is called after the interface
1309 * was disabled etc., in which case driver_ap_teardown is possibly set
1310 * to 1. Clear it here so any other key/station deletion, which is not
1311 * part of a teardown flow, would also call the relevant driver
1312 * callbacks.
1313 */
1314 iface->driver_ap_teardown = 0;
1315
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001316 if (!iface->phy[0]) {
1317 const char *phy = hostapd_drv_get_radio_name(hapd);
1318 if (phy) {
1319 wpa_printf(MSG_DEBUG, "phy: %s", phy);
1320 os_strlcpy(iface->phy, phy, sizeof(iface->phy));
1321 }
1322 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001323
1324 /*
1325 * Make sure that all BSSes get configured with a pointer to the same
1326 * driver interface.
1327 */
1328 for (i = 1; i < iface->num_bss; i++) {
1329 iface->bss[i]->driver = hapd->driver;
1330 iface->bss[i]->drv_priv = hapd->drv_priv;
1331 }
1332
1333 if (hostapd_validate_bssid_configuration(iface))
1334 return -1;
1335
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001336 /*
1337 * Initialize control interfaces early to allow external monitoring of
1338 * channel setup operations that may take considerable amount of time
1339 * especially for DFS cases.
1340 */
1341 if (start_ctrl_iface(iface))
1342 return -1;
1343
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001344 if (hapd->iconf->country[0] && hapd->iconf->country[1]) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001345 char country[4], previous_country[4];
1346
1347 hostapd_set_state(iface, HAPD_IFACE_COUNTRY_UPDATE);
1348 if (hostapd_get_country(hapd, previous_country) < 0)
1349 previous_country[0] = '\0';
1350
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001351 os_memcpy(country, hapd->iconf->country, 3);
1352 country[3] = '\0';
1353 if (hostapd_set_country(hapd, country) < 0) {
1354 wpa_printf(MSG_ERROR, "Failed to set country code");
1355 return -1;
1356 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001357
1358 wpa_printf(MSG_DEBUG, "Previous country code %s, new country code %s",
1359 previous_country, country);
1360
1361 if (os_strncmp(previous_country, country, 2) != 0) {
1362 wpa_printf(MSG_DEBUG, "Continue interface setup after channel list update");
1363 iface->wait_channel_update = 1;
Dmitry Shmidt97672262014-02-03 13:02:54 -08001364 eloop_register_timeout(5, 0,
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001365 channel_list_update_timeout,
1366 iface, NULL);
1367 return 0;
1368 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001369 }
1370
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001371 return setup_interface2(iface);
1372}
1373
1374
1375static int setup_interface2(struct hostapd_iface *iface)
1376{
1377 iface->wait_channel_update = 0;
1378
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001379 if (hostapd_get_hw_features(iface)) {
1380 /* Not all drivers support this yet, so continue without hw
1381 * feature data. */
1382 } else {
1383 int ret = hostapd_select_hw_mode(iface);
1384 if (ret < 0) {
1385 wpa_printf(MSG_ERROR, "Could not select hw_mode and "
1386 "channel. (%d)", ret);
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001387 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001388 }
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001389 if (ret == 1) {
1390 wpa_printf(MSG_DEBUG, "Interface initialization will be completed in a callback (ACS)");
1391 return 0;
1392 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001393 ret = hostapd_check_ht_capab(iface);
1394 if (ret < 0)
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001395 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001396 if (ret == 1) {
1397 wpa_printf(MSG_DEBUG, "Interface initialization will "
1398 "be completed in a callback");
1399 return 0;
1400 }
Dmitry Shmidt051af732013-10-22 13:52:46 -07001401
1402 if (iface->conf->ieee80211h)
1403 wpa_printf(MSG_DEBUG, "DFS support is enabled");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001404 }
1405 return hostapd_setup_interface_complete(iface, 0);
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001406
1407fail:
1408 hostapd_set_state(iface, HAPD_IFACE_DISABLED);
1409 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
1410 if (iface->interfaces && iface->interfaces->terminate_on_error)
1411 eloop_terminate();
1412 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001413}
1414
1415
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001416#ifdef CONFIG_FST
1417
1418static const u8 * fst_hostapd_get_bssid_cb(void *ctx)
1419{
1420 struct hostapd_data *hapd = ctx;
1421
1422 return hapd->own_addr;
1423}
1424
1425
1426static void fst_hostapd_get_channel_info_cb(void *ctx,
1427 enum hostapd_hw_mode *hw_mode,
1428 u8 *channel)
1429{
1430 struct hostapd_data *hapd = ctx;
1431
1432 *hw_mode = ieee80211_freq_to_chan(hapd->iface->freq, channel);
1433}
1434
1435
1436static void fst_hostapd_set_ies_cb(void *ctx, const struct wpabuf *fst_ies)
1437{
1438 struct hostapd_data *hapd = ctx;
1439
1440 if (hapd->iface->fst_ies != fst_ies) {
1441 hapd->iface->fst_ies = fst_ies;
1442 if (ieee802_11_set_beacon(hapd))
1443 wpa_printf(MSG_WARNING, "FST: Cannot set beacon");
1444 }
1445}
1446
1447
1448static int fst_hostapd_send_action_cb(void *ctx, const u8 *da,
1449 struct wpabuf *buf)
1450{
1451 struct hostapd_data *hapd = ctx;
1452
1453 return hostapd_drv_send_action(hapd, hapd->iface->freq, 0, da,
1454 wpabuf_head(buf), wpabuf_len(buf));
1455}
1456
1457
1458static const struct wpabuf * fst_hostapd_get_mb_ie_cb(void *ctx, const u8 *addr)
1459{
1460 struct hostapd_data *hapd = ctx;
1461 struct sta_info *sta = ap_get_sta(hapd, addr);
1462
1463 return sta ? sta->mb_ies : NULL;
1464}
1465
1466
1467static void fst_hostapd_update_mb_ie_cb(void *ctx, const u8 *addr,
1468 const u8 *buf, size_t size)
1469{
1470 struct hostapd_data *hapd = ctx;
1471 struct sta_info *sta = ap_get_sta(hapd, addr);
1472
1473 if (sta) {
1474 struct mb_ies_info info;
1475
1476 if (!mb_ies_info_by_ies(&info, buf, size)) {
1477 wpabuf_free(sta->mb_ies);
1478 sta->mb_ies = mb_ies_by_info(&info);
1479 }
1480 }
1481}
1482
1483
1484static const u8 * fst_hostapd_get_sta(struct fst_get_peer_ctx **get_ctx,
1485 Boolean mb_only)
1486{
1487 struct sta_info *s = (struct sta_info *) *get_ctx;
1488
1489 if (mb_only) {
1490 for (; s && !s->mb_ies; s = s->next)
1491 ;
1492 }
1493
1494 if (s) {
1495 *get_ctx = (struct fst_get_peer_ctx *) s->next;
1496
1497 return s->addr;
1498 }
1499
1500 *get_ctx = NULL;
1501 return NULL;
1502}
1503
1504
1505static const u8 * fst_hostapd_get_peer_first(void *ctx,
1506 struct fst_get_peer_ctx **get_ctx,
1507 Boolean mb_only)
1508{
1509 struct hostapd_data *hapd = ctx;
1510
1511 *get_ctx = (struct fst_get_peer_ctx *) hapd->sta_list;
1512
1513 return fst_hostapd_get_sta(get_ctx, mb_only);
1514}
1515
1516
1517static const u8 * fst_hostapd_get_peer_next(void *ctx,
1518 struct fst_get_peer_ctx **get_ctx,
1519 Boolean mb_only)
1520{
1521 return fst_hostapd_get_sta(get_ctx, mb_only);
1522}
1523
1524
1525void fst_hostapd_fill_iface_obj(struct hostapd_data *hapd,
1526 struct fst_wpa_obj *iface_obj)
1527{
1528 iface_obj->ctx = hapd;
1529 iface_obj->get_bssid = fst_hostapd_get_bssid_cb;
1530 iface_obj->get_channel_info = fst_hostapd_get_channel_info_cb;
1531 iface_obj->set_ies = fst_hostapd_set_ies_cb;
1532 iface_obj->send_action = fst_hostapd_send_action_cb;
1533 iface_obj->get_mb_ie = fst_hostapd_get_mb_ie_cb;
1534 iface_obj->update_mb_ie = fst_hostapd_update_mb_ie_cb;
1535 iface_obj->get_peer_first = fst_hostapd_get_peer_first;
1536 iface_obj->get_peer_next = fst_hostapd_get_peer_next;
1537}
1538
1539#endif /* CONFIG_FST */
1540
1541
Dmitry Shmidt849734c2016-05-27 09:59:01 -07001542#ifdef NEED_AP_MLME
1543static enum nr_chan_width hostapd_get_nr_chan_width(struct hostapd_data *hapd,
1544 int ht, int vht)
1545{
1546 if (!ht && !vht)
1547 return NR_CHAN_WIDTH_20;
1548 if (!hapd->iconf->secondary_channel)
1549 return NR_CHAN_WIDTH_20;
1550 if (!vht || hapd->iconf->vht_oper_chwidth == VHT_CHANWIDTH_USE_HT)
1551 return NR_CHAN_WIDTH_40;
1552 if (hapd->iconf->vht_oper_chwidth == VHT_CHANWIDTH_80MHZ)
1553 return NR_CHAN_WIDTH_80;
1554 if (hapd->iconf->vht_oper_chwidth == VHT_CHANWIDTH_160MHZ)
1555 return NR_CHAN_WIDTH_160;
1556 if (hapd->iconf->vht_oper_chwidth == VHT_CHANWIDTH_80P80MHZ)
1557 return NR_CHAN_WIDTH_80P80;
1558 return NR_CHAN_WIDTH_20;
1559}
1560#endif /* NEED_AP_MLME */
1561
1562
1563static void hostapd_set_own_neighbor_report(struct hostapd_data *hapd)
1564{
1565#ifdef NEED_AP_MLME
1566 u16 capab = hostapd_own_capab_info(hapd);
1567 int ht = hapd->iconf->ieee80211n && !hapd->conf->disable_11n;
1568 int vht = hapd->iconf->ieee80211ac && !hapd->conf->disable_11ac;
1569 struct wpa_ssid_value ssid;
1570 u8 channel, op_class;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001571 u8 center_freq1_idx = 0, center_freq2_idx = 0;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07001572 enum nr_chan_width width;
1573 u32 bssid_info;
1574 struct wpabuf *nr;
1575
1576 if (!(hapd->conf->radio_measurements[0] &
1577 WLAN_RRM_CAPS_NEIGHBOR_REPORT))
1578 return;
1579
1580 bssid_info = 3; /* AP is reachable */
1581 bssid_info |= NEI_REP_BSSID_INFO_SECURITY; /* "same as the AP" */
1582 bssid_info |= NEI_REP_BSSID_INFO_KEY_SCOPE; /* "same as the AP" */
1583
1584 if (capab & WLAN_CAPABILITY_SPECTRUM_MGMT)
1585 bssid_info |= NEI_REP_BSSID_INFO_SPECTRUM_MGMT;
1586
1587 bssid_info |= NEI_REP_BSSID_INFO_RM; /* RRM is supported */
1588
1589 if (hapd->conf->wmm_enabled) {
1590 bssid_info |= NEI_REP_BSSID_INFO_QOS;
1591
1592 if (hapd->conf->wmm_uapsd &&
1593 (hapd->iface->drv_flags & WPA_DRIVER_FLAGS_AP_UAPSD))
1594 bssid_info |= NEI_REP_BSSID_INFO_APSD;
1595 }
1596
1597 if (ht) {
1598 bssid_info |= NEI_REP_BSSID_INFO_HT |
1599 NEI_REP_BSSID_INFO_DELAYED_BA;
1600
1601 /* VHT bit added in IEEE P802.11-REVmc/D4.3 */
1602 if (vht)
1603 bssid_info |= NEI_REP_BSSID_INFO_VHT;
1604 }
1605
1606 /* TODO: Set NEI_REP_BSSID_INFO_MOBILITY_DOMAIN if MDE is set */
1607
Dmitry Shmidt29333592017-01-09 12:27:11 -08001608 if (ieee80211_freq_to_channel_ext(hapd->iface->freq,
1609 hapd->iconf->secondary_channel,
1610 hapd->iconf->vht_oper_chwidth,
1611 &op_class, &channel) ==
1612 NUM_HOSTAPD_MODES)
1613 return;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07001614 width = hostapd_get_nr_chan_width(hapd, ht, vht);
1615 if (vht) {
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001616 center_freq1_idx = hapd->iconf->vht_oper_centr_freq_seg0_idx;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07001617 if (width == NR_CHAN_WIDTH_80P80)
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001618 center_freq2_idx =
1619 hapd->iconf->vht_oper_centr_freq_seg1_idx;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07001620 } else if (ht) {
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001621 ieee80211_freq_to_chan(hapd->iface->freq +
1622 10 * hapd->iconf->secondary_channel,
1623 &center_freq1_idx);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07001624 }
1625
1626 ssid.ssid_len = hapd->conf->ssid.ssid_len;
1627 os_memcpy(ssid.ssid, hapd->conf->ssid.ssid, ssid.ssid_len);
1628
1629 /*
1630 * Neighbor Report element size = BSSID + BSSID info + op_class + chan +
1631 * phy type + wide bandwidth channel subelement.
1632 */
1633 nr = wpabuf_alloc(ETH_ALEN + 4 + 1 + 1 + 1 + 5);
1634 if (!nr)
1635 return;
1636
1637 wpabuf_put_data(nr, hapd->own_addr, ETH_ALEN);
1638 wpabuf_put_le32(nr, bssid_info);
1639 wpabuf_put_u8(nr, op_class);
1640 wpabuf_put_u8(nr, channel);
1641 wpabuf_put_u8(nr, ieee80211_get_phy_type(hapd->iface->freq, ht, vht));
1642
1643 /*
1644 * Wide Bandwidth Channel subelement may be needed to allow the
1645 * receiving STA to send packets to the AP. See IEEE P802.11-REVmc/D5.0
1646 * Figure 9-301.
1647 */
1648 wpabuf_put_u8(nr, WNM_NEIGHBOR_WIDE_BW_CHAN);
1649 wpabuf_put_u8(nr, 3);
1650 wpabuf_put_u8(nr, width);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001651 wpabuf_put_u8(nr, center_freq1_idx);
1652 wpabuf_put_u8(nr, center_freq2_idx);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07001653
1654 hostapd_neighbor_set(hapd, hapd->own_addr, &ssid, nr, hapd->iconf->lci,
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001655 hapd->iconf->civic, hapd->iconf->stationary_ap);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07001656
1657 wpabuf_free(nr);
1658#endif /* NEED_AP_MLME */
1659}
1660
1661
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001662static int hostapd_setup_interface_complete_sync(struct hostapd_iface *iface,
1663 int err)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001664{
1665 struct hostapd_data *hapd = iface->bss[0];
1666 size_t j;
1667 u8 *prev_addr;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001668 int delay_apply_cfg = 0;
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001669 int res_dfs_offload = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001670
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001671 if (err)
1672 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001673
1674 wpa_printf(MSG_DEBUG, "Completing interface initialization");
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001675 if (iface->conf->channel) {
Dmitry Shmidt051af732013-10-22 13:52:46 -07001676#ifdef NEED_AP_MLME
1677 int res;
1678#endif /* NEED_AP_MLME */
1679
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001680 iface->freq = hostapd_hw_get_freq(hapd, iface->conf->channel);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001681 wpa_printf(MSG_DEBUG, "Mode: %s Channel: %d "
1682 "Frequency: %d MHz",
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001683 hostapd_hw_mode_txt(iface->conf->hw_mode),
1684 iface->conf->channel, iface->freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001685
Dmitry Shmidt051af732013-10-22 13:52:46 -07001686#ifdef NEED_AP_MLME
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001687 /* Handle DFS only if it is not offloaded to the driver */
1688 if (!(iface->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD)) {
1689 /* Check DFS */
1690 res = hostapd_handle_dfs(iface);
1691 if (res <= 0) {
1692 if (res < 0)
1693 goto fail;
1694 return res;
1695 }
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001696 } else {
1697 /* If DFS is offloaded to the driver */
1698 res_dfs_offload = hostapd_handle_dfs_offload(iface);
1699 if (res_dfs_offload <= 0) {
1700 if (res_dfs_offload < 0)
1701 goto fail;
1702 } else {
1703 wpa_printf(MSG_DEBUG,
1704 "Proceed with AP/channel setup");
1705 /*
1706 * If this is a DFS channel, move to completing
1707 * AP setup.
1708 */
1709 if (res_dfs_offload == 1)
1710 goto dfs_offload;
1711 /* Otherwise fall through. */
1712 }
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001713 }
Dmitry Shmidt051af732013-10-22 13:52:46 -07001714#endif /* NEED_AP_MLME */
1715
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001716#ifdef CONFIG_MESH
1717 if (iface->mconf != NULL) {
1718 wpa_printf(MSG_DEBUG,
1719 "%s: Mesh configuration will be applied while joining the mesh network",
1720 iface->bss[0]->conf->iface);
1721 delay_apply_cfg = 1;
1722 }
1723#endif /* CONFIG_MESH */
1724
1725 if (!delay_apply_cfg &&
1726 hostapd_set_freq(hapd, hapd->iconf->hw_mode, iface->freq,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001727 hapd->iconf->channel,
1728 hapd->iconf->ieee80211n,
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001729 hapd->iconf->ieee80211ac,
1730 hapd->iconf->secondary_channel,
1731 hapd->iconf->vht_oper_chwidth,
1732 hapd->iconf->vht_oper_centr_freq_seg0_idx,
1733 hapd->iconf->vht_oper_centr_freq_seg1_idx)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001734 wpa_printf(MSG_ERROR, "Could not set channel for "
1735 "kernel driver");
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001736 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001737 }
1738 }
1739
1740 if (iface->current_mode) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001741 if (hostapd_prepare_rates(iface, iface->current_mode)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001742 wpa_printf(MSG_ERROR, "Failed to prepare rates "
1743 "table.");
1744 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
1745 HOSTAPD_LEVEL_WARNING,
1746 "Failed to prepare rates table.");
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001747 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001748 }
1749 }
1750
1751 if (hapd->iconf->rts_threshold > -1 &&
1752 hostapd_set_rts(hapd, hapd->iconf->rts_threshold)) {
1753 wpa_printf(MSG_ERROR, "Could not set RTS threshold for "
1754 "kernel driver");
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001755 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001756 }
1757
1758 if (hapd->iconf->fragm_threshold > -1 &&
1759 hostapd_set_frag(hapd, hapd->iconf->fragm_threshold)) {
1760 wpa_printf(MSG_ERROR, "Could not set fragmentation threshold "
1761 "for kernel driver");
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001762 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001763 }
1764
1765 prev_addr = hapd->own_addr;
1766
1767 for (j = 0; j < iface->num_bss; j++) {
1768 hapd = iface->bss[j];
1769 if (j)
1770 os_memcpy(hapd->own_addr, prev_addr, ETH_ALEN);
Dmitry Shmidt71757432014-06-02 13:50:35 -07001771 if (hostapd_setup_bss(hapd, j == 0)) {
1772 do {
1773 hapd = iface->bss[j];
1774 hostapd_bss_deinit_no_free(hapd);
1775 hostapd_free_hapd_data(hapd);
1776 } while (j-- > 0);
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001777 goto fail;
Dmitry Shmidt71757432014-06-02 13:50:35 -07001778 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001779 if (is_zero_ether_addr(hapd->conf->bssid))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001780 prev_addr = hapd->own_addr;
1781 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001782 hapd = iface->bss[0];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001783
1784 hostapd_tx_queue_params(iface);
1785
1786 ap_list_init(iface);
1787
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001788 hostapd_set_acl(hapd);
1789
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001790 if (hostapd_driver_commit(hapd) < 0) {
1791 wpa_printf(MSG_ERROR, "%s: Failed to commit driver "
1792 "configuration", __func__);
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001793 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001794 }
1795
Jouni Malinen87fd2792011-05-16 18:35:42 +03001796 /*
1797 * WPS UPnP module can be initialized only when the "upnp_iface" is up.
1798 * If "interface" and "upnp_iface" are the same (e.g., non-bridge
1799 * mode), the interface is up only after driver_commit, so initialize
1800 * WPS after driver_commit.
1801 */
1802 for (j = 0; j < iface->num_bss; j++) {
1803 if (hostapd_init_wps_complete(iface->bss[j]))
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001804 goto fail;
Jouni Malinen87fd2792011-05-16 18:35:42 +03001805 }
1806
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001807 if ((iface->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
1808 !res_dfs_offload) {
1809 /*
1810 * If freq is DFS, and DFS is offloaded to the driver, then wait
1811 * for CAC to complete.
1812 */
1813 wpa_printf(MSG_DEBUG, "%s: Wait for CAC to complete", __func__);
1814 return res_dfs_offload;
1815 }
1816
1817#ifdef NEED_AP_MLME
1818dfs_offload:
1819#endif /* NEED_AP_MLME */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001820
1821#ifdef CONFIG_FST
1822 if (hapd->iconf->fst_cfg.group_id[0]) {
1823 struct fst_wpa_obj iface_obj;
1824
1825 fst_hostapd_fill_iface_obj(hapd, &iface_obj);
1826 iface->fst = fst_attach(hapd->conf->iface, hapd->own_addr,
1827 &iface_obj, &hapd->iconf->fst_cfg);
1828 if (!iface->fst) {
1829 wpa_printf(MSG_ERROR, "Could not attach to FST %s",
1830 hapd->iconf->fst_cfg.group_id);
1831 goto fail;
1832 }
1833 }
1834#endif /* CONFIG_FST */
1835
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001836 hostapd_set_state(iface, HAPD_IFACE_ENABLED);
1837 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_ENABLED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001838 if (hapd->setup_complete_cb)
1839 hapd->setup_complete_cb(hapd->setup_complete_cb_ctx);
1840
1841 wpa_printf(MSG_DEBUG, "%s: Setup of interface done.",
1842 iface->bss[0]->conf->iface);
Dmitry Shmidtb96dad42013-11-05 10:07:29 -08001843 if (iface->interfaces && iface->interfaces->terminate_on_error > 0)
1844 iface->interfaces->terminate_on_error--;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001845
Dmitry Shmidt849734c2016-05-27 09:59:01 -07001846 for (j = 0; j < iface->num_bss; j++)
1847 hostapd_set_own_neighbor_report(iface->bss[j]);
1848
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001849 return 0;
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001850
1851fail:
1852 wpa_printf(MSG_ERROR, "Interface initialization failed");
1853 hostapd_set_state(iface, HAPD_IFACE_DISABLED);
1854 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001855#ifdef CONFIG_FST
1856 if (iface->fst) {
1857 fst_detach(iface->fst);
1858 iface->fst = NULL;
1859 }
1860#endif /* CONFIG_FST */
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001861 if (iface->interfaces && iface->interfaces->terminate_on_error)
1862 eloop_terminate();
1863 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001864}
1865
1866
1867/**
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001868 * hostapd_setup_interface_complete - Complete interface setup
1869 *
1870 * This function is called when previous steps in the interface setup has been
1871 * completed. This can also start operations, e.g., DFS, that will require
1872 * additional processing before interface is ready to be enabled. Such
1873 * operations will call this function from eloop callbacks when finished.
1874 */
1875int hostapd_setup_interface_complete(struct hostapd_iface *iface, int err)
1876{
1877 struct hapd_interfaces *interfaces = iface->interfaces;
1878 struct hostapd_data *hapd = iface->bss[0];
1879 unsigned int i;
1880 int not_ready_in_sync_ifaces = 0;
1881
1882 if (!iface->need_to_start_in_sync)
1883 return hostapd_setup_interface_complete_sync(iface, err);
1884
1885 if (err) {
1886 wpa_printf(MSG_ERROR, "Interface initialization failed");
1887 hostapd_set_state(iface, HAPD_IFACE_DISABLED);
1888 iface->need_to_start_in_sync = 0;
1889 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
1890 if (interfaces && interfaces->terminate_on_error)
1891 eloop_terminate();
1892 return -1;
1893 }
1894
1895 if (iface->ready_to_start_in_sync) {
1896 /* Already in ready and waiting. should never happpen */
1897 return 0;
1898 }
1899
1900 for (i = 0; i < interfaces->count; i++) {
1901 if (interfaces->iface[i]->need_to_start_in_sync &&
1902 !interfaces->iface[i]->ready_to_start_in_sync)
1903 not_ready_in_sync_ifaces++;
1904 }
1905
1906 /*
1907 * Check if this is the last interface, if yes then start all the other
1908 * waiting interfaces. If not, add this interface to the waiting list.
1909 */
1910 if (not_ready_in_sync_ifaces > 1 && iface->state == HAPD_IFACE_DFS) {
1911 /*
1912 * If this interface went through CAC, do not synchronize, just
1913 * start immediately.
1914 */
1915 iface->need_to_start_in_sync = 0;
1916 wpa_printf(MSG_INFO,
1917 "%s: Finished CAC - bypass sync and start interface",
1918 iface->bss[0]->conf->iface);
1919 return hostapd_setup_interface_complete_sync(iface, err);
1920 }
1921
1922 if (not_ready_in_sync_ifaces > 1) {
1923 /* need to wait as there are other interfaces still coming up */
1924 iface->ready_to_start_in_sync = 1;
1925 wpa_printf(MSG_INFO,
1926 "%s: Interface waiting to sync with other interfaces",
1927 iface->bss[0]->conf->iface);
1928 return 0;
1929 }
1930
1931 wpa_printf(MSG_INFO,
1932 "%s: Last interface to sync - starting all interfaces",
1933 iface->bss[0]->conf->iface);
1934 iface->need_to_start_in_sync = 0;
1935 hostapd_setup_interface_complete_sync(iface, err);
1936 for (i = 0; i < interfaces->count; i++) {
1937 if (interfaces->iface[i]->need_to_start_in_sync &&
1938 interfaces->iface[i]->ready_to_start_in_sync) {
1939 hostapd_setup_interface_complete_sync(
1940 interfaces->iface[i], 0);
1941 /* Only once the interfaces are sync started */
1942 interfaces->iface[i]->need_to_start_in_sync = 0;
1943 }
1944 }
1945
1946 return 0;
1947}
1948
1949
1950/**
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001951 * hostapd_setup_interface - Setup of an interface
1952 * @iface: Pointer to interface data.
1953 * Returns: 0 on success, -1 on failure
1954 *
1955 * Initializes the driver interface, validates the configuration,
1956 * and sets driver parameters based on the configuration.
1957 * Flushes old stations, sets the channel, encryption,
1958 * beacons, and WDS links based on the configuration.
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001959 *
1960 * If interface setup requires more time, e.g., to perform HT co-ex scans, ACS,
1961 * or DFS operations, this function returns 0 before such operations have been
1962 * completed. The pending operations are registered into eloop and will be
1963 * completed from eloop callbacks. Those callbacks end up calling
1964 * hostapd_setup_interface_complete() once setup has been completed.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001965 */
1966int hostapd_setup_interface(struct hostapd_iface *iface)
1967{
1968 int ret;
1969
1970 ret = setup_interface(iface);
1971 if (ret) {
1972 wpa_printf(MSG_ERROR, "%s: Unable to setup interface.",
1973 iface->bss[0]->conf->iface);
1974 return -1;
1975 }
1976
1977 return 0;
1978}
1979
1980
1981/**
1982 * hostapd_alloc_bss_data - Allocate and initialize per-BSS data
1983 * @hapd_iface: Pointer to interface data
1984 * @conf: Pointer to per-interface configuration
1985 * @bss: Pointer to per-BSS configuration for this BSS
1986 * Returns: Pointer to allocated BSS data
1987 *
1988 * This function is used to allocate per-BSS data structure. This data will be
1989 * freed after hostapd_cleanup() is called for it during interface
1990 * deinitialization.
1991 */
1992struct hostapd_data *
1993hostapd_alloc_bss_data(struct hostapd_iface *hapd_iface,
1994 struct hostapd_config *conf,
1995 struct hostapd_bss_config *bss)
1996{
1997 struct hostapd_data *hapd;
1998
1999 hapd = os_zalloc(sizeof(*hapd));
2000 if (hapd == NULL)
2001 return NULL;
2002
2003 hapd->new_assoc_sta_cb = hostapd_new_assoc_sta;
2004 hapd->iconf = conf;
2005 hapd->conf = bss;
2006 hapd->iface = hapd_iface;
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08002007 if (conf)
2008 hapd->driver = conf->driver;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002009 hapd->ctrl_sock = -1;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08002010 dl_list_init(&hapd->ctrl_dst);
Dmitry Shmidt7d175302016-09-06 13:11:34 -07002011 dl_list_init(&hapd->nr_db);
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08002012 hapd->dhcp_sock = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002013
2014 return hapd;
2015}
2016
2017
Dmitry Shmidt54605472013-11-08 11:10:19 -08002018static void hostapd_bss_deinit(struct hostapd_data *hapd)
2019{
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07002020 if (!hapd)
2021 return;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002022 wpa_printf(MSG_DEBUG, "%s: deinit bss %s", __func__,
2023 hapd->conf->iface);
Dmitry Shmidt71757432014-06-02 13:50:35 -07002024 hostapd_bss_deinit_no_free(hapd);
Dmitry Shmidtf73259c2015-03-17 11:00:54 -07002025 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
Dmitry Shmidt54605472013-11-08 11:10:19 -08002026 hostapd_cleanup(hapd);
2027}
2028
2029
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002030void hostapd_interface_deinit(struct hostapd_iface *iface)
2031{
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002032 int j;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002033
Dmitry Shmidt54605472013-11-08 11:10:19 -08002034 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002035 if (iface == NULL)
2036 return;
2037
Dmitry Shmidtf73259c2015-03-17 11:00:54 -07002038 hostapd_set_state(iface, HAPD_IFACE_DISABLED);
2039
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07002040#ifdef CONFIG_IEEE80211N
2041#ifdef NEED_AP_MLME
2042 hostapd_stop_setup_timers(iface);
2043 eloop_cancel_timeout(ap_ht2040_timeout, iface, NULL);
2044#endif /* NEED_AP_MLME */
2045#endif /* CONFIG_IEEE80211N */
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002046 eloop_cancel_timeout(channel_list_update_timeout, iface, NULL);
2047 iface->wait_channel_update = 0;
2048
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002049#ifdef CONFIG_FST
2050 if (iface->fst) {
2051 fst_detach(iface->fst);
2052 iface->fst = NULL;
2053 }
2054#endif /* CONFIG_FST */
2055
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07002056 for (j = iface->num_bss - 1; j >= 0; j--) {
2057 if (!iface->bss)
2058 break;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002059 hostapd_bss_deinit(iface->bss[j]);
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07002060 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002061}
2062
2063
2064void hostapd_interface_free(struct hostapd_iface *iface)
2065{
2066 size_t j;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002067 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
2068 for (j = 0; j < iface->num_bss; j++) {
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07002069 if (!iface->bss)
2070 break;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002071 wpa_printf(MSG_DEBUG, "%s: free hapd %p",
2072 __func__, iface->bss[j]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002073 os_free(iface->bss[j]);
Dmitry Shmidt54605472013-11-08 11:10:19 -08002074 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002075 hostapd_cleanup_iface(iface);
2076}
2077
2078
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07002079struct hostapd_iface * hostapd_alloc_iface(void)
2080{
2081 struct hostapd_iface *hapd_iface;
2082
2083 hapd_iface = os_zalloc(sizeof(*hapd_iface));
2084 if (!hapd_iface)
2085 return NULL;
2086
2087 dl_list_init(&hapd_iface->sta_seen);
2088
2089 return hapd_iface;
2090}
2091
2092
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002093/**
2094 * hostapd_init - Allocate and initialize per-interface data
2095 * @config_file: Path to the configuration file
2096 * Returns: Pointer to the allocated interface data or %NULL on failure
2097 *
2098 * This function is used to allocate main data structures for per-interface
2099 * data. The allocated data buffer will be freed by calling
2100 * hostapd_cleanup_iface().
2101 */
2102struct hostapd_iface * hostapd_init(struct hapd_interfaces *interfaces,
2103 const char *config_file)
2104{
2105 struct hostapd_iface *hapd_iface = NULL;
2106 struct hostapd_config *conf = NULL;
2107 struct hostapd_data *hapd;
2108 size_t i;
2109
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07002110 hapd_iface = hostapd_alloc_iface();
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002111 if (hapd_iface == NULL)
2112 goto fail;
2113
2114 hapd_iface->config_fname = os_strdup(config_file);
2115 if (hapd_iface->config_fname == NULL)
2116 goto fail;
2117
2118 conf = interfaces->config_read_cb(hapd_iface->config_fname);
2119 if (conf == NULL)
2120 goto fail;
2121 hapd_iface->conf = conf;
2122
2123 hapd_iface->num_bss = conf->num_bss;
2124 hapd_iface->bss = os_calloc(conf->num_bss,
2125 sizeof(struct hostapd_data *));
2126 if (hapd_iface->bss == NULL)
2127 goto fail;
2128
2129 for (i = 0; i < conf->num_bss; i++) {
2130 hapd = hapd_iface->bss[i] =
2131 hostapd_alloc_bss_data(hapd_iface, conf,
2132 conf->bss[i]);
2133 if (hapd == NULL)
2134 goto fail;
2135 hapd->msg_ctx = hapd;
2136 }
2137
2138 return hapd_iface;
2139
2140fail:
2141 wpa_printf(MSG_ERROR, "Failed to set up interface with %s",
2142 config_file);
2143 if (conf)
2144 hostapd_config_free(conf);
2145 if (hapd_iface) {
2146 os_free(hapd_iface->config_fname);
2147 os_free(hapd_iface->bss);
Dmitry Shmidt54605472013-11-08 11:10:19 -08002148 wpa_printf(MSG_DEBUG, "%s: free iface %p",
2149 __func__, hapd_iface);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002150 os_free(hapd_iface);
2151 }
2152 return NULL;
2153}
2154
2155
2156static int ifname_in_use(struct hapd_interfaces *interfaces, const char *ifname)
2157{
2158 size_t i, j;
2159
2160 for (i = 0; i < interfaces->count; i++) {
2161 struct hostapd_iface *iface = interfaces->iface[i];
2162 for (j = 0; j < iface->num_bss; j++) {
2163 struct hostapd_data *hapd = iface->bss[j];
2164 if (os_strcmp(ifname, hapd->conf->iface) == 0)
2165 return 1;
2166 }
2167 }
2168
2169 return 0;
2170}
2171
2172
2173/**
2174 * hostapd_interface_init_bss - Read configuration file and init BSS data
2175 *
2176 * This function is used to parse configuration file for a BSS. This BSS is
2177 * added to an existing interface sharing the same radio (if any) or a new
2178 * interface is created if this is the first interface on a radio. This
2179 * allocate memory for the BSS. No actual driver operations are started.
2180 *
2181 * This is similar to hostapd_interface_init(), but for a case where the
2182 * configuration is used to add a single BSS instead of all BSSes for a radio.
2183 */
2184struct hostapd_iface *
2185hostapd_interface_init_bss(struct hapd_interfaces *interfaces, const char *phy,
2186 const char *config_fname, int debug)
2187{
2188 struct hostapd_iface *new_iface = NULL, *iface = NULL;
2189 struct hostapd_data *hapd;
2190 int k;
2191 size_t i, bss_idx;
2192
2193 if (!phy || !*phy)
2194 return NULL;
2195
2196 for (i = 0; i < interfaces->count; i++) {
2197 if (os_strcmp(interfaces->iface[i]->phy, phy) == 0) {
2198 iface = interfaces->iface[i];
2199 break;
2200 }
2201 }
2202
2203 wpa_printf(MSG_INFO, "Configuration file: %s (phy %s)%s",
2204 config_fname, phy, iface ? "" : " --> new PHY");
2205 if (iface) {
2206 struct hostapd_config *conf;
2207 struct hostapd_bss_config **tmp_conf;
2208 struct hostapd_data **tmp_bss;
2209 struct hostapd_bss_config *bss;
2210 const char *ifname;
2211
2212 /* Add new BSS to existing iface */
2213 conf = interfaces->config_read_cb(config_fname);
2214 if (conf == NULL)
2215 return NULL;
2216 if (conf->num_bss > 1) {
2217 wpa_printf(MSG_ERROR, "Multiple BSSes specified in BSS-config");
2218 hostapd_config_free(conf);
2219 return NULL;
2220 }
2221
2222 ifname = conf->bss[0]->iface;
2223 if (ifname[0] != '\0' && ifname_in_use(interfaces, ifname)) {
2224 wpa_printf(MSG_ERROR,
2225 "Interface name %s already in use", ifname);
2226 hostapd_config_free(conf);
2227 return NULL;
2228 }
2229
2230 tmp_conf = os_realloc_array(
2231 iface->conf->bss, iface->conf->num_bss + 1,
2232 sizeof(struct hostapd_bss_config *));
2233 tmp_bss = os_realloc_array(iface->bss, iface->num_bss + 1,
2234 sizeof(struct hostapd_data *));
2235 if (tmp_bss)
2236 iface->bss = tmp_bss;
2237 if (tmp_conf) {
2238 iface->conf->bss = tmp_conf;
2239 iface->conf->last_bss = tmp_conf[0];
2240 }
2241 if (tmp_bss == NULL || tmp_conf == NULL) {
2242 hostapd_config_free(conf);
2243 return NULL;
2244 }
2245 bss = iface->conf->bss[iface->conf->num_bss] = conf->bss[0];
2246 iface->conf->num_bss++;
2247
2248 hapd = hostapd_alloc_bss_data(iface, iface->conf, bss);
2249 if (hapd == NULL) {
2250 iface->conf->num_bss--;
2251 hostapd_config_free(conf);
2252 return NULL;
2253 }
2254 iface->conf->last_bss = bss;
2255 iface->bss[iface->num_bss] = hapd;
2256 hapd->msg_ctx = hapd;
2257
2258 bss_idx = iface->num_bss++;
2259 conf->num_bss--;
2260 conf->bss[0] = NULL;
2261 hostapd_config_free(conf);
2262 } else {
2263 /* Add a new iface with the first BSS */
2264 new_iface = iface = hostapd_init(interfaces, config_fname);
2265 if (!iface)
2266 return NULL;
2267 os_strlcpy(iface->phy, phy, sizeof(iface->phy));
2268 iface->interfaces = interfaces;
2269 bss_idx = 0;
2270 }
2271
2272 for (k = 0; k < debug; k++) {
2273 if (iface->bss[bss_idx]->conf->logger_stdout_level > 0)
2274 iface->bss[bss_idx]->conf->logger_stdout_level--;
2275 }
2276
2277 if (iface->conf->bss[bss_idx]->iface[0] == '\0' &&
2278 !hostapd_drv_none(iface->bss[bss_idx])) {
2279 wpa_printf(MSG_ERROR, "Interface name not specified in %s",
2280 config_fname);
2281 if (new_iface)
2282 hostapd_interface_deinit_free(new_iface);
2283 return NULL;
2284 }
2285
2286 return iface;
2287}
2288
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002289
2290void hostapd_interface_deinit_free(struct hostapd_iface *iface)
2291{
2292 const struct wpa_driver_ops *driver;
2293 void *drv_priv;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002294
2295 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002296 if (iface == NULL)
2297 return;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002298 wpa_printf(MSG_DEBUG, "%s: num_bss=%u conf->num_bss=%u",
2299 __func__, (unsigned int) iface->num_bss,
2300 (unsigned int) iface->conf->num_bss);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002301 driver = iface->bss[0]->driver;
2302 drv_priv = iface->bss[0]->drv_priv;
2303 hostapd_interface_deinit(iface);
Dmitry Shmidt54605472013-11-08 11:10:19 -08002304 wpa_printf(MSG_DEBUG, "%s: driver=%p drv_priv=%p -> hapd_deinit",
2305 __func__, driver, drv_priv);
Dmitry Shmidt71757432014-06-02 13:50:35 -07002306 if (driver && driver->hapd_deinit && drv_priv) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002307 driver->hapd_deinit(drv_priv);
Dmitry Shmidt71757432014-06-02 13:50:35 -07002308 iface->bss[0]->drv_priv = NULL;
2309 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002310 hostapd_interface_free(iface);
2311}
2312
2313
Dmitry Shmidt15907092014-03-25 10:42:57 -07002314static void hostapd_deinit_driver(const struct wpa_driver_ops *driver,
2315 void *drv_priv,
2316 struct hostapd_iface *hapd_iface)
2317{
2318 size_t j;
2319
2320 wpa_printf(MSG_DEBUG, "%s: driver=%p drv_priv=%p -> hapd_deinit",
2321 __func__, driver, drv_priv);
2322 if (driver && driver->hapd_deinit && drv_priv) {
2323 driver->hapd_deinit(drv_priv);
2324 for (j = 0; j < hapd_iface->num_bss; j++) {
2325 wpa_printf(MSG_DEBUG, "%s:bss[%d]->drv_priv=%p",
2326 __func__, (int) j,
2327 hapd_iface->bss[j]->drv_priv);
2328 if (hapd_iface->bss[j]->drv_priv == drv_priv)
2329 hapd_iface->bss[j]->drv_priv = NULL;
2330 }
2331 }
2332}
2333
2334
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002335int hostapd_enable_iface(struct hostapd_iface *hapd_iface)
2336{
Dmitry Shmidt71757432014-06-02 13:50:35 -07002337 size_t j;
2338
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002339 if (hapd_iface->bss[0]->drv_priv != NULL) {
2340 wpa_printf(MSG_ERROR, "Interface %s already enabled",
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002341 hapd_iface->conf->bss[0]->iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002342 return -1;
2343 }
2344
2345 wpa_printf(MSG_DEBUG, "Enable interface %s",
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002346 hapd_iface->conf->bss[0]->iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002347
Dmitry Shmidt71757432014-06-02 13:50:35 -07002348 for (j = 0; j < hapd_iface->num_bss; j++)
2349 hostapd_set_security_params(hapd_iface->conf->bss[j], 1);
Dmitry Shmidt344abd32014-01-14 13:17:00 -08002350 if (hostapd_config_check(hapd_iface->conf, 1) < 0) {
2351 wpa_printf(MSG_INFO, "Invalid configuration - cannot enable");
2352 return -1;
2353 }
2354
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002355 if (hapd_iface->interfaces == NULL ||
2356 hapd_iface->interfaces->driver_init == NULL ||
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002357 hapd_iface->interfaces->driver_init(hapd_iface))
2358 return -1;
2359
2360 if (hostapd_setup_interface(hapd_iface)) {
Dmitry Shmidt15907092014-03-25 10:42:57 -07002361 hostapd_deinit_driver(hapd_iface->bss[0]->driver,
2362 hapd_iface->bss[0]->drv_priv,
2363 hapd_iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002364 return -1;
2365 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002366
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002367 return 0;
2368}
2369
2370
2371int hostapd_reload_iface(struct hostapd_iface *hapd_iface)
2372{
2373 size_t j;
2374
2375 wpa_printf(MSG_DEBUG, "Reload interface %s",
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002376 hapd_iface->conf->bss[0]->iface);
2377 for (j = 0; j < hapd_iface->num_bss; j++)
Dmitry Shmidt71757432014-06-02 13:50:35 -07002378 hostapd_set_security_params(hapd_iface->conf->bss[j], 1);
Dmitry Shmidt344abd32014-01-14 13:17:00 -08002379 if (hostapd_config_check(hapd_iface->conf, 1) < 0) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002380 wpa_printf(MSG_ERROR, "Updated configuration is invalid");
2381 return -1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002382 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002383 hostapd_clear_old(hapd_iface);
2384 for (j = 0; j < hapd_iface->num_bss; j++)
2385 hostapd_reload_bss(hapd_iface->bss[j]);
2386
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002387 return 0;
2388}
2389
2390
2391int hostapd_disable_iface(struct hostapd_iface *hapd_iface)
2392{
2393 size_t j;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002394 const struct wpa_driver_ops *driver;
2395 void *drv_priv;
2396
2397 if (hapd_iface == NULL)
2398 return -1;
Dmitry Shmidt71757432014-06-02 13:50:35 -07002399
2400 if (hapd_iface->bss[0]->drv_priv == NULL) {
2401 wpa_printf(MSG_INFO, "Interface %s already disabled",
2402 hapd_iface->conf->bss[0]->iface);
2403 return -1;
2404 }
2405
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002406 wpa_msg(hapd_iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002407 driver = hapd_iface->bss[0]->driver;
2408 drv_priv = hapd_iface->bss[0]->drv_priv;
2409
Dmitry Shmidta38abf92014-03-06 13:38:44 -08002410 hapd_iface->driver_ap_teardown =
2411 !!(hapd_iface->drv_flags &
2412 WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
2413
2414 /* same as hostapd_interface_deinit without deinitializing ctrl-iface */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002415 for (j = 0; j < hapd_iface->num_bss; j++) {
2416 struct hostapd_data *hapd = hapd_iface->bss[j];
Dmitry Shmidt71757432014-06-02 13:50:35 -07002417 hostapd_bss_deinit_no_free(hapd);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002418 hostapd_free_hapd_data(hapd);
2419 }
2420
Dmitry Shmidt15907092014-03-25 10:42:57 -07002421 hostapd_deinit_driver(driver, drv_priv, hapd_iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002422
2423 /* From hostapd_cleanup_iface: These were initialized in
2424 * hostapd_setup_interface and hostapd_setup_interface_complete
2425 */
2426 hostapd_cleanup_iface_partial(hapd_iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002427
Dmitry Shmidt56052862013-10-04 10:23:25 -07002428 wpa_printf(MSG_DEBUG, "Interface %s disabled",
2429 hapd_iface->bss[0]->conf->iface);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002430 hostapd_set_state(hapd_iface, HAPD_IFACE_DISABLED);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002431 return 0;
2432}
2433
2434
2435static struct hostapd_iface *
2436hostapd_iface_alloc(struct hapd_interfaces *interfaces)
2437{
2438 struct hostapd_iface **iface, *hapd_iface;
2439
2440 iface = os_realloc_array(interfaces->iface, interfaces->count + 1,
2441 sizeof(struct hostapd_iface *));
2442 if (iface == NULL)
2443 return NULL;
2444 interfaces->iface = iface;
2445 hapd_iface = interfaces->iface[interfaces->count] =
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07002446 hostapd_alloc_iface();
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002447 if (hapd_iface == NULL) {
2448 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory for "
2449 "the interface", __func__);
2450 return NULL;
2451 }
2452 interfaces->count++;
2453 hapd_iface->interfaces = interfaces;
2454
2455 return hapd_iface;
2456}
2457
2458
2459static struct hostapd_config *
2460hostapd_config_alloc(struct hapd_interfaces *interfaces, const char *ifname,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002461 const char *ctrl_iface, const char *driver)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002462{
2463 struct hostapd_bss_config *bss;
2464 struct hostapd_config *conf;
2465
2466 /* Allocates memory for bss and conf */
2467 conf = hostapd_config_defaults();
2468 if (conf == NULL) {
2469 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory for "
2470 "configuration", __func__);
2471 return NULL;
2472 }
2473
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002474 if (driver) {
2475 int j;
2476
2477 for (j = 0; wpa_drivers[j]; j++) {
2478 if (os_strcmp(driver, wpa_drivers[j]->name) == 0) {
2479 conf->driver = wpa_drivers[j];
2480 goto skip;
2481 }
2482 }
2483
2484 wpa_printf(MSG_ERROR,
2485 "Invalid/unknown driver '%s' - registering the default driver",
2486 driver);
2487 }
2488
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002489 conf->driver = wpa_drivers[0];
2490 if (conf->driver == NULL) {
2491 wpa_printf(MSG_ERROR, "No driver wrappers registered!");
2492 hostapd_config_free(conf);
2493 return NULL;
2494 }
2495
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002496skip:
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002497 bss = conf->last_bss = conf->bss[0];
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002498
2499 os_strlcpy(bss->iface, ifname, sizeof(bss->iface));
2500 bss->ctrl_interface = os_strdup(ctrl_iface);
2501 if (bss->ctrl_interface == NULL) {
2502 hostapd_config_free(conf);
2503 return NULL;
2504 }
2505
2506 /* Reading configuration file skipped, will be done in SET!
2507 * From reading the configuration till the end has to be done in
2508 * SET
2509 */
2510 return conf;
2511}
2512
2513
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002514static int hostapd_data_alloc(struct hostapd_iface *hapd_iface,
2515 struct hostapd_config *conf)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002516{
2517 size_t i;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002518 struct hostapd_data *hapd;
2519
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002520 hapd_iface->bss = os_calloc(conf->num_bss,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002521 sizeof(struct hostapd_data *));
2522 if (hapd_iface->bss == NULL)
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002523 return -1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002524
2525 for (i = 0; i < conf->num_bss; i++) {
2526 hapd = hapd_iface->bss[i] =
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002527 hostapd_alloc_bss_data(hapd_iface, conf, conf->bss[i]);
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002528 if (hapd == NULL) {
2529 while (i > 0) {
2530 i--;
2531 os_free(hapd_iface->bss[i]);
2532 hapd_iface->bss[i] = NULL;
2533 }
2534 os_free(hapd_iface->bss);
2535 hapd_iface->bss = NULL;
2536 return -1;
2537 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002538 hapd->msg_ctx = hapd;
2539 }
2540
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002541 hapd_iface->conf = conf;
2542 hapd_iface->num_bss = conf->num_bss;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002543
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002544 return 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002545}
2546
2547
2548int hostapd_add_iface(struct hapd_interfaces *interfaces, char *buf)
2549{
2550 struct hostapd_config *conf = NULL;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002551 struct hostapd_iface *hapd_iface = NULL, *new_iface = NULL;
2552 struct hostapd_data *hapd;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002553 char *ptr;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002554 size_t i, j;
2555 const char *conf_file = NULL, *phy_name = NULL;
2556
2557 if (os_strncmp(buf, "bss_config=", 11) == 0) {
2558 char *pos;
2559 phy_name = buf + 11;
2560 pos = os_strchr(phy_name, ':');
2561 if (!pos)
2562 return -1;
2563 *pos++ = '\0';
2564 conf_file = pos;
2565 if (!os_strlen(conf_file))
2566 return -1;
2567
2568 hapd_iface = hostapd_interface_init_bss(interfaces, phy_name,
2569 conf_file, 0);
2570 if (!hapd_iface)
2571 return -1;
2572 for (j = 0; j < interfaces->count; j++) {
2573 if (interfaces->iface[j] == hapd_iface)
2574 break;
2575 }
2576 if (j == interfaces->count) {
2577 struct hostapd_iface **tmp;
2578 tmp = os_realloc_array(interfaces->iface,
2579 interfaces->count + 1,
2580 sizeof(struct hostapd_iface *));
2581 if (!tmp) {
2582 hostapd_interface_deinit_free(hapd_iface);
2583 return -1;
2584 }
2585 interfaces->iface = tmp;
2586 interfaces->iface[interfaces->count++] = hapd_iface;
2587 new_iface = hapd_iface;
2588 }
2589
2590 if (new_iface) {
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002591 if (interfaces->driver_init(hapd_iface))
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002592 goto fail;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002593
2594 if (hostapd_setup_interface(hapd_iface)) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002595 hostapd_deinit_driver(
2596 hapd_iface->bss[0]->driver,
2597 hapd_iface->bss[0]->drv_priv,
2598 hapd_iface);
2599 goto fail;
2600 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002601 } else {
2602 /* Assign new BSS with bss[0]'s driver info */
2603 hapd = hapd_iface->bss[hapd_iface->num_bss - 1];
2604 hapd->driver = hapd_iface->bss[0]->driver;
2605 hapd->drv_priv = hapd_iface->bss[0]->drv_priv;
2606 os_memcpy(hapd->own_addr, hapd_iface->bss[0]->own_addr,
2607 ETH_ALEN);
2608
2609 if (start_ctrl_iface_bss(hapd) < 0 ||
Dmitry Shmidt54605472013-11-08 11:10:19 -08002610 (hapd_iface->state == HAPD_IFACE_ENABLED &&
2611 hostapd_setup_bss(hapd, -1))) {
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002612 hostapd_cleanup(hapd);
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08002613 hapd_iface->bss[hapd_iface->num_bss - 1] = NULL;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002614 hapd_iface->conf->num_bss--;
2615 hapd_iface->num_bss--;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002616 wpa_printf(MSG_DEBUG, "%s: free hapd %p %s",
2617 __func__, hapd, hapd->conf->iface);
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002618 hostapd_config_free_bss(hapd->conf);
2619 hapd->conf = NULL;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002620 os_free(hapd);
2621 return -1;
2622 }
2623 }
2624 return 0;
2625 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002626
2627 ptr = os_strchr(buf, ' ');
2628 if (ptr == NULL)
2629 return -1;
2630 *ptr++ = '\0';
2631
Dmitry Shmidt56052862013-10-04 10:23:25 -07002632 if (os_strncmp(ptr, "config=", 7) == 0)
2633 conf_file = ptr + 7;
2634
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002635 for (i = 0; i < interfaces->count; i++) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002636 if (!os_strcmp(interfaces->iface[i]->conf->bss[0]->iface,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002637 buf)) {
2638 wpa_printf(MSG_INFO, "Cannot add interface - it "
2639 "already exists");
2640 return -1;
2641 }
2642 }
2643
2644 hapd_iface = hostapd_iface_alloc(interfaces);
2645 if (hapd_iface == NULL) {
2646 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
2647 "for interface", __func__);
2648 goto fail;
2649 }
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002650 new_iface = hapd_iface;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002651
Dmitry Shmidt56052862013-10-04 10:23:25 -07002652 if (conf_file && interfaces->config_read_cb) {
2653 conf = interfaces->config_read_cb(conf_file);
2654 if (conf && conf->bss)
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002655 os_strlcpy(conf->bss[0]->iface, buf,
2656 sizeof(conf->bss[0]->iface));
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002657 } else {
2658 char *driver = os_strchr(ptr, ' ');
2659
2660 if (driver)
2661 *driver++ = '\0';
2662 conf = hostapd_config_alloc(interfaces, buf, ptr, driver);
2663 }
2664
Dmitry Shmidt56052862013-10-04 10:23:25 -07002665 if (conf == NULL || conf->bss == NULL) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002666 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
2667 "for configuration", __func__);
2668 goto fail;
2669 }
2670
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002671 if (hostapd_data_alloc(hapd_iface, conf) < 0) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002672 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
2673 "for hostapd", __func__);
2674 goto fail;
2675 }
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002676 conf = NULL;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002677
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002678 if (start_ctrl_iface(hapd_iface) < 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002679 goto fail;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002680
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002681 wpa_printf(MSG_INFO, "Add interface '%s'",
2682 hapd_iface->conf->bss[0]->iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002683
2684 return 0;
2685
2686fail:
2687 if (conf)
2688 hostapd_config_free(conf);
2689 if (hapd_iface) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002690 if (hapd_iface->bss) {
Dmitry Shmidt54605472013-11-08 11:10:19 -08002691 for (i = 0; i < hapd_iface->num_bss; i++) {
2692 hapd = hapd_iface->bss[i];
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08002693 if (!hapd)
2694 continue;
2695 if (hapd_iface->interfaces &&
Dmitry Shmidt54605472013-11-08 11:10:19 -08002696 hapd_iface->interfaces->ctrl_iface_deinit)
2697 hapd_iface->interfaces->
2698 ctrl_iface_deinit(hapd);
2699 wpa_printf(MSG_DEBUG, "%s: free hapd %p (%s)",
2700 __func__, hapd_iface->bss[i],
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08002701 hapd->conf->iface);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002702 hostapd_cleanup(hapd);
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08002703 os_free(hapd);
2704 hapd_iface->bss[i] = NULL;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002705 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002706 os_free(hapd_iface->bss);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002707 hapd_iface->bss = NULL;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002708 }
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002709 if (new_iface) {
2710 interfaces->count--;
2711 interfaces->iface[interfaces->count] = NULL;
2712 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002713 hostapd_cleanup_iface(hapd_iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002714 }
2715 return -1;
2716}
2717
2718
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002719static int hostapd_remove_bss(struct hostapd_iface *iface, unsigned int idx)
2720{
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002721 size_t i;
2722
Dmitry Shmidt54605472013-11-08 11:10:19 -08002723 wpa_printf(MSG_INFO, "Remove BSS '%s'", iface->conf->bss[idx]->iface);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002724
Dmitry Shmidt54605472013-11-08 11:10:19 -08002725 /* Remove hostapd_data only if it has already been initialized */
2726 if (idx < iface->num_bss) {
2727 struct hostapd_data *hapd = iface->bss[idx];
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002728
Dmitry Shmidt54605472013-11-08 11:10:19 -08002729 hostapd_bss_deinit(hapd);
2730 wpa_printf(MSG_DEBUG, "%s: free hapd %p (%s)",
2731 __func__, hapd, hapd->conf->iface);
2732 hostapd_config_free_bss(hapd->conf);
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002733 hapd->conf = NULL;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002734 os_free(hapd);
2735
2736 iface->num_bss--;
2737
2738 for (i = idx; i < iface->num_bss; i++)
2739 iface->bss[i] = iface->bss[i + 1];
2740 } else {
2741 hostapd_config_free_bss(iface->conf->bss[idx]);
2742 iface->conf->bss[idx] = NULL;
2743 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002744
2745 iface->conf->num_bss--;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002746 for (i = idx; i < iface->conf->num_bss; i++)
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002747 iface->conf->bss[i] = iface->conf->bss[i + 1];
2748
2749 return 0;
2750}
2751
2752
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002753int hostapd_remove_iface(struct hapd_interfaces *interfaces, char *buf)
2754{
2755 struct hostapd_iface *hapd_iface;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002756 size_t i, j, k = 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002757
2758 for (i = 0; i < interfaces->count; i++) {
2759 hapd_iface = interfaces->iface[i];
2760 if (hapd_iface == NULL)
2761 return -1;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002762 if (!os_strcmp(hapd_iface->conf->bss[0]->iface, buf)) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002763 wpa_printf(MSG_INFO, "Remove interface '%s'", buf);
Dmitry Shmidta38abf92014-03-06 13:38:44 -08002764 hapd_iface->driver_ap_teardown =
2765 !!(hapd_iface->drv_flags &
2766 WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
2767
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002768 hostapd_interface_deinit_free(hapd_iface);
2769 k = i;
2770 while (k < (interfaces->count - 1)) {
2771 interfaces->iface[k] =
2772 interfaces->iface[k + 1];
2773 k++;
2774 }
2775 interfaces->count--;
2776 return 0;
2777 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002778
2779 for (j = 0; j < hapd_iface->conf->num_bss; j++) {
Dmitry Shmidta38abf92014-03-06 13:38:44 -08002780 if (!os_strcmp(hapd_iface->conf->bss[j]->iface, buf)) {
2781 hapd_iface->driver_ap_teardown =
2782 !(hapd_iface->drv_flags &
2783 WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002784 return hostapd_remove_bss(hapd_iface, j);
Dmitry Shmidta38abf92014-03-06 13:38:44 -08002785 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002786 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002787 }
2788 return -1;
2789}
2790
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002791
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002792/**
2793 * hostapd_new_assoc_sta - Notify that a new station associated with the AP
2794 * @hapd: Pointer to BSS data
2795 * @sta: Pointer to the associated STA data
2796 * @reassoc: 1 to indicate this was a re-association; 0 = first association
2797 *
2798 * This function will be called whenever a station associates with the AP. It
2799 * can be called from ieee802_11.c for drivers that export MLME to hostapd and
2800 * from drv_callbacks.c based on driver events for drivers that take care of
2801 * management frames (IEEE 802.11 authentication and association) internally.
2802 */
2803void hostapd_new_assoc_sta(struct hostapd_data *hapd, struct sta_info *sta,
2804 int reassoc)
2805{
2806 if (hapd->tkip_countermeasures) {
2807 hostapd_drv_sta_deauth(hapd, sta->addr,
2808 WLAN_REASON_MICHAEL_MIC_FAILURE);
2809 return;
2810 }
2811
2812 hostapd_prune_associations(hapd, sta->addr);
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -08002813 ap_sta_clear_disconnect_timeouts(hapd, sta);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002814
2815 /* IEEE 802.11F (IAPP) */
2816 if (hapd->conf->ieee802_11f)
2817 iapp_new_station(hapd->iapp, sta);
2818
2819#ifdef CONFIG_P2P
2820 if (sta->p2p_ie == NULL && !sta->no_p2p_set) {
2821 sta->no_p2p_set = 1;
2822 hapd->num_sta_no_p2p++;
2823 if (hapd->num_sta_no_p2p == 1)
2824 hostapd_p2p_non_p2p_sta_connected(hapd);
2825 }
2826#endif /* CONFIG_P2P */
2827
2828 /* Start accounting here, if IEEE 802.1X and WPA are not used.
2829 * IEEE 802.1X/WPA code will start accounting after the station has
2830 * been authorized. */
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -08002831 if (!hapd->conf->ieee802_1x && !hapd->conf->wpa && !hapd->conf->osen) {
2832 ap_sta_set_authorized(hapd, sta, 1);
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002833 os_get_reltime(&sta->connected_time);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002834 accounting_sta_start(hapd, sta);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002835 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002836
2837 /* Start IEEE 802.1X authentication process for new stations */
2838 ieee802_1x_new_station(hapd, sta);
2839 if (reassoc) {
2840 if (sta->auth_alg != WLAN_AUTH_FT &&
2841 !(sta->flags & (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS)))
2842 wpa_auth_sm_event(sta->wpa_sm, WPA_REAUTH);
2843 } else
2844 wpa_auth_sta_associated(hapd->wpa_auth, sta->wpa_sm);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002845
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08002846 if (hapd->iface->drv_flags & WPA_DRIVER_FLAGS_WIRED) {
2847 if (eloop_cancel_timeout(ap_handle_timer, hapd, sta) > 0) {
2848 wpa_printf(MSG_DEBUG,
2849 "%s: %s: canceled wired ap_handle_timer timeout for "
2850 MACSTR,
2851 hapd->conf->iface, __func__,
2852 MAC2STR(sta->addr));
2853 }
2854 } else if (!(hapd->iface->drv_flags &
2855 WPA_DRIVER_FLAGS_INACTIVITY_TIMER)) {
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -08002856 wpa_printf(MSG_DEBUG,
2857 "%s: %s: reschedule ap_handle_timer timeout for "
2858 MACSTR " (%d seconds - ap_max_inactivity)",
2859 hapd->conf->iface, __func__, MAC2STR(sta->addr),
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08002860 hapd->conf->ap_max_inactivity);
2861 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
2862 eloop_register_timeout(hapd->conf->ap_max_inactivity, 0,
2863 ap_handle_timer, hapd, sta);
2864 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002865}
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002866
2867
2868const char * hostapd_state_text(enum hostapd_iface_state s)
2869{
2870 switch (s) {
2871 case HAPD_IFACE_UNINITIALIZED:
2872 return "UNINITIALIZED";
2873 case HAPD_IFACE_DISABLED:
2874 return "DISABLED";
2875 case HAPD_IFACE_COUNTRY_UPDATE:
2876 return "COUNTRY_UPDATE";
2877 case HAPD_IFACE_ACS:
2878 return "ACS";
2879 case HAPD_IFACE_HT_SCAN:
2880 return "HT_SCAN";
2881 case HAPD_IFACE_DFS:
2882 return "DFS";
2883 case HAPD_IFACE_ENABLED:
2884 return "ENABLED";
2885 }
2886
2887 return "UNKNOWN";
2888}
2889
2890
2891void hostapd_set_state(struct hostapd_iface *iface, enum hostapd_iface_state s)
2892{
2893 wpa_printf(MSG_INFO, "%s: interface state %s->%s",
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07002894 iface->conf ? iface->conf->bss[0]->iface : "N/A",
2895 hostapd_state_text(iface->state), hostapd_state_text(s));
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002896 iface->state = s;
2897}
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002898
2899
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002900int hostapd_csa_in_progress(struct hostapd_iface *iface)
2901{
2902 unsigned int i;
2903
2904 for (i = 0; i < iface->num_bss; i++)
2905 if (iface->bss[i]->csa_in_progress)
2906 return 1;
2907 return 0;
2908}
2909
2910
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002911#ifdef NEED_AP_MLME
2912
2913static void free_beacon_data(struct beacon_data *beacon)
2914{
2915 os_free(beacon->head);
2916 beacon->head = NULL;
2917 os_free(beacon->tail);
2918 beacon->tail = NULL;
2919 os_free(beacon->probe_resp);
2920 beacon->probe_resp = NULL;
2921 os_free(beacon->beacon_ies);
2922 beacon->beacon_ies = NULL;
2923 os_free(beacon->proberesp_ies);
2924 beacon->proberesp_ies = NULL;
2925 os_free(beacon->assocresp_ies);
2926 beacon->assocresp_ies = NULL;
2927}
2928
2929
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07002930static int hostapd_build_beacon_data(struct hostapd_data *hapd,
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002931 struct beacon_data *beacon)
2932{
2933 struct wpabuf *beacon_extra, *proberesp_extra, *assocresp_extra;
2934 struct wpa_driver_ap_params params;
2935 int ret;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002936
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08002937 os_memset(beacon, 0, sizeof(*beacon));
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002938 ret = ieee802_11_build_ap_params(hapd, &params);
2939 if (ret < 0)
2940 return ret;
2941
2942 ret = hostapd_build_ap_extra_ies(hapd, &beacon_extra,
2943 &proberesp_extra,
2944 &assocresp_extra);
2945 if (ret)
2946 goto free_ap_params;
2947
2948 ret = -1;
2949 beacon->head = os_malloc(params.head_len);
2950 if (!beacon->head)
2951 goto free_ap_extra_ies;
2952
2953 os_memcpy(beacon->head, params.head, params.head_len);
2954 beacon->head_len = params.head_len;
2955
2956 beacon->tail = os_malloc(params.tail_len);
2957 if (!beacon->tail)
2958 goto free_beacon;
2959
2960 os_memcpy(beacon->tail, params.tail, params.tail_len);
2961 beacon->tail_len = params.tail_len;
2962
2963 if (params.proberesp != NULL) {
2964 beacon->probe_resp = os_malloc(params.proberesp_len);
2965 if (!beacon->probe_resp)
2966 goto free_beacon;
2967
2968 os_memcpy(beacon->probe_resp, params.proberesp,
2969 params.proberesp_len);
2970 beacon->probe_resp_len = params.proberesp_len;
2971 }
2972
2973 /* copy the extra ies */
2974 if (beacon_extra) {
2975 beacon->beacon_ies = os_malloc(wpabuf_len(beacon_extra));
2976 if (!beacon->beacon_ies)
2977 goto free_beacon;
2978
2979 os_memcpy(beacon->beacon_ies,
2980 beacon_extra->buf, wpabuf_len(beacon_extra));
2981 beacon->beacon_ies_len = wpabuf_len(beacon_extra);
2982 }
2983
2984 if (proberesp_extra) {
2985 beacon->proberesp_ies =
2986 os_malloc(wpabuf_len(proberesp_extra));
2987 if (!beacon->proberesp_ies)
2988 goto free_beacon;
2989
2990 os_memcpy(beacon->proberesp_ies, proberesp_extra->buf,
2991 wpabuf_len(proberesp_extra));
2992 beacon->proberesp_ies_len = wpabuf_len(proberesp_extra);
2993 }
2994
2995 if (assocresp_extra) {
2996 beacon->assocresp_ies =
2997 os_malloc(wpabuf_len(assocresp_extra));
2998 if (!beacon->assocresp_ies)
2999 goto free_beacon;
3000
3001 os_memcpy(beacon->assocresp_ies, assocresp_extra->buf,
3002 wpabuf_len(assocresp_extra));
3003 beacon->assocresp_ies_len = wpabuf_len(assocresp_extra);
3004 }
3005
3006 ret = 0;
3007free_beacon:
3008 /* if the function fails, the caller should not free beacon data */
3009 if (ret)
3010 free_beacon_data(beacon);
3011
3012free_ap_extra_ies:
3013 hostapd_free_ap_extra_ies(hapd, beacon_extra, proberesp_extra,
3014 assocresp_extra);
3015free_ap_params:
3016 ieee802_11_free_ap_params(&params);
3017 return ret;
3018}
3019
3020
3021/*
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003022 * TODO: This flow currently supports only changing channel and width within
3023 * the same hw_mode. Any other changes to MAC parameters or provided settings
3024 * are not supported.
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003025 */
3026static int hostapd_change_config_freq(struct hostapd_data *hapd,
3027 struct hostapd_config *conf,
3028 struct hostapd_freq_params *params,
3029 struct hostapd_freq_params *old_params)
3030{
3031 int channel;
3032
3033 if (!params->channel) {
3034 /* check if the new channel is supported by hw */
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003035 params->channel = hostapd_hw_get_channel(hapd, params->freq);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003036 }
3037
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003038 channel = params->channel;
3039 if (!channel)
3040 return -1;
3041
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003042 /* if a pointer to old_params is provided we save previous state */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003043 if (old_params &&
3044 hostapd_set_freq_params(old_params, conf->hw_mode,
3045 hostapd_hw_get_freq(hapd, conf->channel),
3046 conf->channel, conf->ieee80211n,
3047 conf->ieee80211ac,
3048 conf->secondary_channel,
3049 conf->vht_oper_chwidth,
3050 conf->vht_oper_centr_freq_seg0_idx,
3051 conf->vht_oper_centr_freq_seg1_idx,
3052 conf->vht_capab))
3053 return -1;
3054
3055 switch (params->bandwidth) {
3056 case 0:
3057 case 20:
3058 case 40:
3059 conf->vht_oper_chwidth = VHT_CHANWIDTH_USE_HT;
3060 break;
3061 case 80:
3062 if (params->center_freq2)
3063 conf->vht_oper_chwidth = VHT_CHANWIDTH_80P80MHZ;
3064 else
3065 conf->vht_oper_chwidth = VHT_CHANWIDTH_80MHZ;
3066 break;
3067 case 160:
3068 conf->vht_oper_chwidth = VHT_CHANWIDTH_160MHZ;
3069 break;
3070 default:
3071 return -1;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003072 }
3073
3074 conf->channel = channel;
3075 conf->ieee80211n = params->ht_enabled;
3076 conf->secondary_channel = params->sec_channel_offset;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003077 ieee80211_freq_to_chan(params->center_freq1,
3078 &conf->vht_oper_centr_freq_seg0_idx);
3079 ieee80211_freq_to_chan(params->center_freq2,
3080 &conf->vht_oper_centr_freq_seg1_idx);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003081
3082 /* TODO: maybe call here hostapd_config_check here? */
3083
3084 return 0;
3085}
3086
3087
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003088static int hostapd_fill_csa_settings(struct hostapd_data *hapd,
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003089 struct csa_settings *settings)
3090{
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003091 struct hostapd_iface *iface = hapd->iface;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003092 struct hostapd_freq_params old_freq;
3093 int ret;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003094 u8 chan, vht_bandwidth;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003095
3096 os_memset(&old_freq, 0, sizeof(old_freq));
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003097 if (!iface || !iface->freq || hapd->csa_in_progress)
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003098 return -1;
3099
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003100 switch (settings->freq_params.bandwidth) {
3101 case 80:
3102 if (settings->freq_params.center_freq2)
3103 vht_bandwidth = VHT_CHANWIDTH_80P80MHZ;
3104 else
3105 vht_bandwidth = VHT_CHANWIDTH_80MHZ;
3106 break;
3107 case 160:
3108 vht_bandwidth = VHT_CHANWIDTH_160MHZ;
3109 break;
3110 default:
3111 vht_bandwidth = VHT_CHANWIDTH_USE_HT;
3112 break;
3113 }
3114
3115 if (ieee80211_freq_to_channel_ext(
3116 settings->freq_params.freq,
3117 settings->freq_params.sec_channel_offset,
3118 vht_bandwidth,
3119 &hapd->iface->cs_oper_class,
3120 &chan) == NUM_HOSTAPD_MODES) {
3121 wpa_printf(MSG_DEBUG,
3122 "invalid frequency for channel switch (freq=%d, sec_channel_offset=%d, vht_enabled=%d)",
3123 settings->freq_params.freq,
3124 settings->freq_params.sec_channel_offset,
3125 settings->freq_params.vht_enabled);
3126 return -1;
3127 }
3128
3129 settings->freq_params.channel = chan;
3130
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003131 ret = hostapd_change_config_freq(iface->bss[0], iface->conf,
3132 &settings->freq_params,
3133 &old_freq);
3134 if (ret)
3135 return ret;
3136
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003137 ret = hostapd_build_beacon_data(hapd, &settings->beacon_after);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003138
3139 /* change back the configuration */
3140 hostapd_change_config_freq(iface->bss[0], iface->conf,
3141 &old_freq, NULL);
3142
3143 if (ret)
3144 return ret;
3145
3146 /* set channel switch parameters for csa ie */
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003147 hapd->cs_freq_params = settings->freq_params;
3148 hapd->cs_count = settings->cs_count;
3149 hapd->cs_block_tx = settings->block_tx;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003150
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003151 ret = hostapd_build_beacon_data(hapd, &settings->beacon_csa);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003152 if (ret) {
3153 free_beacon_data(&settings->beacon_after);
3154 return ret;
3155 }
3156
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003157 settings->counter_offset_beacon[0] = hapd->cs_c_off_beacon;
3158 settings->counter_offset_presp[0] = hapd->cs_c_off_proberesp;
3159 settings->counter_offset_beacon[1] = hapd->cs_c_off_ecsa_beacon;
3160 settings->counter_offset_presp[1] = hapd->cs_c_off_ecsa_proberesp;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003161
3162 return 0;
3163}
3164
3165
3166void hostapd_cleanup_cs_params(struct hostapd_data *hapd)
3167{
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003168 os_memset(&hapd->cs_freq_params, 0, sizeof(hapd->cs_freq_params));
3169 hapd->cs_count = 0;
3170 hapd->cs_block_tx = 0;
3171 hapd->cs_c_off_beacon = 0;
3172 hapd->cs_c_off_proberesp = 0;
3173 hapd->csa_in_progress = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003174 hapd->cs_c_off_ecsa_beacon = 0;
3175 hapd->cs_c_off_ecsa_proberesp = 0;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003176}
3177
3178
3179int hostapd_switch_channel(struct hostapd_data *hapd,
3180 struct csa_settings *settings)
3181{
3182 int ret;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003183
3184 if (!(hapd->iface->drv_flags & WPA_DRIVER_FLAGS_AP_CSA)) {
3185 wpa_printf(MSG_INFO, "CSA is not supported");
3186 return -1;
3187 }
3188
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003189 ret = hostapd_fill_csa_settings(hapd, settings);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003190 if (ret)
3191 return ret;
3192
3193 ret = hostapd_drv_switch_channel(hapd, settings);
3194 free_beacon_data(&settings->beacon_csa);
3195 free_beacon_data(&settings->beacon_after);
3196
3197 if (ret) {
3198 /* if we failed, clean cs parameters */
3199 hostapd_cleanup_cs_params(hapd);
3200 return ret;
3201 }
3202
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003203 hapd->csa_in_progress = 1;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003204 return 0;
3205}
3206
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003207
3208void
3209hostapd_switch_channel_fallback(struct hostapd_iface *iface,
3210 const struct hostapd_freq_params *freq_params)
3211{
3212 int vht_seg0_idx = 0, vht_seg1_idx = 0, vht_bw = VHT_CHANWIDTH_USE_HT;
3213 unsigned int i;
3214
3215 wpa_printf(MSG_DEBUG, "Restarting all CSA-related BSSes");
3216
3217 if (freq_params->center_freq1)
3218 vht_seg0_idx = 36 + (freq_params->center_freq1 - 5180) / 5;
3219 if (freq_params->center_freq2)
3220 vht_seg1_idx = 36 + (freq_params->center_freq2 - 5180) / 5;
3221
3222 switch (freq_params->bandwidth) {
3223 case 0:
3224 case 20:
3225 case 40:
3226 vht_bw = VHT_CHANWIDTH_USE_HT;
3227 break;
3228 case 80:
3229 if (freq_params->center_freq2)
3230 vht_bw = VHT_CHANWIDTH_80P80MHZ;
3231 else
3232 vht_bw = VHT_CHANWIDTH_80MHZ;
3233 break;
3234 case 160:
3235 vht_bw = VHT_CHANWIDTH_160MHZ;
3236 break;
3237 default:
3238 wpa_printf(MSG_WARNING, "Unknown CSA bandwidth: %d",
3239 freq_params->bandwidth);
3240 break;
3241 }
3242
3243 iface->freq = freq_params->freq;
3244 iface->conf->channel = freq_params->channel;
3245 iface->conf->secondary_channel = freq_params->sec_channel_offset;
3246 iface->conf->vht_oper_centr_freq_seg0_idx = vht_seg0_idx;
3247 iface->conf->vht_oper_centr_freq_seg1_idx = vht_seg1_idx;
3248 iface->conf->vht_oper_chwidth = vht_bw;
3249 iface->conf->ieee80211n = freq_params->ht_enabled;
3250 iface->conf->ieee80211ac = freq_params->vht_enabled;
3251
3252 /*
3253 * cs_params must not be cleared earlier because the freq_params
3254 * argument may actually point to one of these.
3255 */
3256 for (i = 0; i < iface->num_bss; i++)
3257 hostapd_cleanup_cs_params(iface->bss[i]);
3258
3259 hostapd_disable_iface(iface);
3260 hostapd_enable_iface(iface);
3261}
3262
Dmitry Shmidte4663042016-04-04 10:07:49 -07003263#endif /* NEED_AP_MLME */
3264
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003265
3266struct hostapd_data * hostapd_get_iface(struct hapd_interfaces *interfaces,
3267 const char *ifname)
3268{
3269 size_t i, j;
3270
3271 for (i = 0; i < interfaces->count; i++) {
3272 struct hostapd_iface *iface = interfaces->iface[i];
3273
3274 for (j = 0; j < iface->num_bss; j++) {
3275 struct hostapd_data *hapd = iface->bss[j];
3276
3277 if (os_strcmp(ifname, hapd->conf->iface) == 0)
3278 return hapd;
3279 }
3280 }
3281
3282 return NULL;
3283}
3284
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003285
3286void hostapd_periodic_iface(struct hostapd_iface *iface)
3287{
3288 size_t i;
3289
3290 ap_list_timer(iface);
3291
3292 for (i = 0; i < iface->num_bss; i++) {
3293 struct hostapd_data *hapd = iface->bss[i];
3294
3295 if (!hapd->started)
3296 continue;
3297
3298#ifndef CONFIG_NO_RADIUS
3299 hostapd_acl_expire(hapd);
3300#endif /* CONFIG_NO_RADIUS */
3301 }
3302}