blob: a848f35e9070157f7a53e8aec3bb8d2df526e001 [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 Shmidt8d520ff2011-05-09 14:06:53 -070046
47
Dmitry Shmidt04949592012-07-19 12:16:46 -070048static int hostapd_flush_old_stations(struct hostapd_data *hapd, u16 reason);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070049static int hostapd_setup_encryption(char *iface, struct hostapd_data *hapd);
Dmitry Shmidtc55524a2011-07-07 11:18:38 -070050static int hostapd_broadcast_wep_clear(struct hostapd_data *hapd);
Dmitry Shmidtcce06662013-11-04 18:44:24 -080051static int setup_interface2(struct hostapd_iface *iface);
52static void channel_list_update_timeout(void *eloop_ctx, void *timeout_ctx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070053
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070054
Dmitry Shmidt04949592012-07-19 12:16:46 -070055int hostapd_for_each_interface(struct hapd_interfaces *interfaces,
56 int (*cb)(struct hostapd_iface *iface,
57 void *ctx), void *ctx)
58{
59 size_t i;
60 int ret;
61
62 for (i = 0; i < interfaces->count; i++) {
63 ret = cb(interfaces->iface[i], ctx);
64 if (ret)
65 return ret;
66 }
67
68 return 0;
69}
70
71
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070072static void hostapd_reload_bss(struct hostapd_data *hapd)
73{
Dmitry Shmidtcce06662013-11-04 18:44:24 -080074 struct hostapd_ssid *ssid;
75
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070076#ifndef CONFIG_NO_RADIUS
77 radius_client_reconfig(hapd->radius, hapd->conf->radius);
78#endif /* CONFIG_NO_RADIUS */
79
Dmitry Shmidtcce06662013-11-04 18:44:24 -080080 ssid = &hapd->conf->ssid;
81 if (!ssid->wpa_psk_set && ssid->wpa_psk && !ssid->wpa_psk->next &&
82 ssid->wpa_passphrase_set && ssid->wpa_passphrase) {
83 /*
84 * Force PSK to be derived again since SSID or passphrase may
85 * have changed.
86 */
Dmitry Shmidt7f656022015-02-25 14:36:37 -080087 hostapd_config_clear_wpa_psk(&hapd->conf->ssid.wpa_psk);
Dmitry Shmidtcce06662013-11-04 18:44:24 -080088 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070089 if (hostapd_setup_wpa_psk(hapd->conf)) {
90 wpa_printf(MSG_ERROR, "Failed to re-configure WPA PSK "
91 "after reloading configuration");
92 }
93
94 if (hapd->conf->ieee802_1x || hapd->conf->wpa)
95 hostapd_set_drv_ieee8021x(hapd, hapd->conf->iface, 1);
96 else
97 hostapd_set_drv_ieee8021x(hapd, hapd->conf->iface, 0);
98
Dmitry Shmidtf21452a2014-02-26 10:55:25 -080099 if ((hapd->conf->wpa || hapd->conf->osen) && hapd->wpa_auth == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700100 hostapd_setup_wpa(hapd);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800101 if (hapd->wpa_auth)
102 wpa_init_keys(hapd->wpa_auth);
103 } else if (hapd->conf->wpa) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700104 const u8 *wpa_ie;
105 size_t wpa_ie_len;
106 hostapd_reconfig_wpa(hapd);
107 wpa_ie = wpa_auth_get_wpa_ie(hapd->wpa_auth, &wpa_ie_len);
108 if (hostapd_set_generic_elem(hapd, wpa_ie, wpa_ie_len))
109 wpa_printf(MSG_ERROR, "Failed to configure WPA IE for "
110 "the kernel driver.");
111 } else if (hapd->wpa_auth) {
112 wpa_deinit(hapd->wpa_auth);
113 hapd->wpa_auth = NULL;
114 hostapd_set_privacy(hapd, 0);
115 hostapd_setup_encryption(hapd->conf->iface, hapd);
116 hostapd_set_generic_elem(hapd, (u8 *) "", 0);
117 }
118
119 ieee802_11_set_beacon(hapd);
120 hostapd_update_wps(hapd);
121
122 if (hapd->conf->ssid.ssid_set &&
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700123 hostapd_set_ssid(hapd, hapd->conf->ssid.ssid,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700124 hapd->conf->ssid.ssid_len)) {
125 wpa_printf(MSG_ERROR, "Could not set SSID for kernel driver");
126 /* try to continue */
127 }
128 wpa_printf(MSG_DEBUG, "Reconfigured interface %s", hapd->conf->iface);
129}
130
131
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700132static void hostapd_clear_old(struct hostapd_iface *iface)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700133{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700134 size_t j;
135
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700136 /*
137 * Deauthenticate all stations since the new configuration may not
138 * allow them to use the BSS anymore.
139 */
140 for (j = 0; j < iface->num_bss; j++) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700141 hostapd_flush_old_stations(iface->bss[j],
142 WLAN_REASON_PREV_AUTH_NOT_VALID);
Dmitry Shmidtc55524a2011-07-07 11:18:38 -0700143 hostapd_broadcast_wep_clear(iface->bss[j]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700144
145#ifndef CONFIG_NO_RADIUS
146 /* TODO: update dynamic data based on changed configuration
147 * items (e.g., open/close sockets, etc.) */
148 radius_client_flush(iface->bss[j]->radius, 0);
149#endif /* CONFIG_NO_RADIUS */
150 }
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700151}
152
153
154int hostapd_reload_config(struct hostapd_iface *iface)
155{
156 struct hostapd_data *hapd = iface->bss[0];
157 struct hostapd_config *newconf, *oldconf;
158 size_t j;
159
160 if (iface->config_fname == NULL) {
161 /* Only in-memory config in use - assume it has been updated */
162 hostapd_clear_old(iface);
163 for (j = 0; j < iface->num_bss; j++)
164 hostapd_reload_bss(iface->bss[j]);
165 return 0;
166 }
167
168 if (iface->interfaces == NULL ||
169 iface->interfaces->config_read_cb == NULL)
170 return -1;
171 newconf = iface->interfaces->config_read_cb(iface->config_fname);
172 if (newconf == NULL)
173 return -1;
174
175 hostapd_clear_old(iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700176
177 oldconf = hapd->iconf;
178 iface->conf = newconf;
179
180 for (j = 0; j < iface->num_bss; j++) {
181 hapd = iface->bss[j];
182 hapd->iconf = newconf;
Dmitry Shmidtd11f0192014-03-24 12:09:47 -0700183 hapd->iconf->channel = oldconf->channel;
Dmitry Shmidtdda10c22015-03-24 16:05:01 -0700184 hapd->iconf->acs = oldconf->acs;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -0700185 hapd->iconf->secondary_channel = oldconf->secondary_channel;
Dmitry Shmidtd11f0192014-03-24 12:09:47 -0700186 hapd->iconf->ieee80211n = oldconf->ieee80211n;
187 hapd->iconf->ieee80211ac = oldconf->ieee80211ac;
188 hapd->iconf->ht_capab = oldconf->ht_capab;
189 hapd->iconf->vht_capab = oldconf->vht_capab;
190 hapd->iconf->vht_oper_chwidth = oldconf->vht_oper_chwidth;
191 hapd->iconf->vht_oper_centr_freq_seg0_idx =
192 oldconf->vht_oper_centr_freq_seg0_idx;
193 hapd->iconf->vht_oper_centr_freq_seg1_idx =
194 oldconf->vht_oper_centr_freq_seg1_idx;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800195 hapd->conf = newconf->bss[j];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700196 hostapd_reload_bss(hapd);
197 }
198
199 hostapd_config_free(oldconf);
200
201
202 return 0;
203}
204
205
206static void hostapd_broadcast_key_clear_iface(struct hostapd_data *hapd,
207 char *ifname)
208{
209 int i;
210
211 for (i = 0; i < NUM_WEP_KEYS; i++) {
212 if (hostapd_drv_set_key(ifname, hapd, WPA_ALG_NONE, NULL, i,
213 0, NULL, 0, NULL, 0)) {
214 wpa_printf(MSG_DEBUG, "Failed to clear default "
215 "encryption keys (ifname=%s keyidx=%d)",
216 ifname, i);
217 }
218 }
219#ifdef CONFIG_IEEE80211W
220 if (hapd->conf->ieee80211w) {
221 for (i = NUM_WEP_KEYS; i < NUM_WEP_KEYS + 2; i++) {
222 if (hostapd_drv_set_key(ifname, hapd, WPA_ALG_NONE,
223 NULL, i, 0, NULL,
224 0, NULL, 0)) {
225 wpa_printf(MSG_DEBUG, "Failed to clear "
226 "default mgmt encryption keys "
227 "(ifname=%s keyidx=%d)", ifname, i);
228 }
229 }
230 }
231#endif /* CONFIG_IEEE80211W */
232}
233
234
235static int hostapd_broadcast_wep_clear(struct hostapd_data *hapd)
236{
237 hostapd_broadcast_key_clear_iface(hapd, hapd->conf->iface);
238 return 0;
239}
240
241
242static int hostapd_broadcast_wep_set(struct hostapd_data *hapd)
243{
244 int errors = 0, idx;
245 struct hostapd_ssid *ssid = &hapd->conf->ssid;
246
247 idx = ssid->wep.idx;
248 if (ssid->wep.default_len &&
249 hostapd_drv_set_key(hapd->conf->iface,
250 hapd, WPA_ALG_WEP, broadcast_ether_addr, idx,
251 1, NULL, 0, ssid->wep.key[idx],
252 ssid->wep.len[idx])) {
253 wpa_printf(MSG_WARNING, "Could not set WEP encryption.");
254 errors++;
255 }
256
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700257 return errors;
258}
259
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700260
Dmitry Shmidt04949592012-07-19 12:16:46 -0700261static void hostapd_free_hapd_data(struct hostapd_data *hapd)
262{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800263 os_free(hapd->probereq_cb);
264 hapd->probereq_cb = NULL;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800265 hapd->num_probereq_cb = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800266
267#ifdef CONFIG_P2P
268 wpabuf_free(hapd->p2p_beacon_ie);
269 hapd->p2p_beacon_ie = NULL;
270 wpabuf_free(hapd->p2p_probe_resp_ie);
271 hapd->p2p_probe_resp_ie = NULL;
272#endif /* CONFIG_P2P */
273
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -0800274 if (!hapd->started) {
275 wpa_printf(MSG_ERROR, "%s: Interface %s wasn't started",
276 __func__, hapd->conf->iface);
277 return;
278 }
279 hapd->started = 0;
280
Dmitry Shmidt54605472013-11-08 11:10:19 -0800281 wpa_printf(MSG_DEBUG, "%s(%s)", __func__, hapd->conf->iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700282 iapp_deinit(hapd->iapp);
283 hapd->iapp = NULL;
284 accounting_deinit(hapd);
285 hostapd_deinit_wpa(hapd);
286 vlan_deinit(hapd);
287 hostapd_acl_deinit(hapd);
288#ifndef CONFIG_NO_RADIUS
289 radius_client_deinit(hapd->radius);
290 hapd->radius = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700291 radius_das_deinit(hapd->radius_das);
292 hapd->radius_das = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700293#endif /* CONFIG_NO_RADIUS */
294
295 hostapd_deinit_wps(hapd);
296
297 authsrv_deinit(hapd);
298
Dmitry Shmidt71757432014-06-02 13:50:35 -0700299 if (hapd->interface_added) {
300 hapd->interface_added = 0;
301 if (hostapd_if_remove(hapd, WPA_IF_AP_BSS, hapd->conf->iface)) {
302 wpa_printf(MSG_WARNING,
303 "Failed to remove BSS interface %s",
304 hapd->conf->iface);
305 hapd->interface_added = 1;
306 } else {
307 /*
308 * Since this was a dynamically added interface, the
309 * driver wrapper may have removed its internal instance
310 * and hapd->drv_priv is not valid anymore.
311 */
312 hapd->drv_priv = NULL;
313 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700314 }
315
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800316 wpabuf_free(hapd->time_adv);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700317
318#ifdef CONFIG_INTERWORKING
319 gas_serv_deinit(hapd);
320#endif /* CONFIG_INTERWORKING */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800321
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800322 bss_load_update_deinit(hapd);
323 ndisc_snoop_deinit(hapd);
324 dhcp_snoop_deinit(hapd);
325 x_snoop_deinit(hapd);
326
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800327#ifdef CONFIG_SQLITE
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700328 bin_clear_free(hapd->tmp_eap_user.identity,
329 hapd->tmp_eap_user.identity_len);
330 bin_clear_free(hapd->tmp_eap_user.password,
331 hapd->tmp_eap_user.password_len);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800332#endif /* CONFIG_SQLITE */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800333
334#ifdef CONFIG_MESH
335 wpabuf_free(hapd->mesh_pending_auth);
336 hapd->mesh_pending_auth = NULL;
337#endif /* CONFIG_MESH */
Dmitry Shmidt04949592012-07-19 12:16:46 -0700338}
339
340
341/**
342 * hostapd_cleanup - Per-BSS cleanup (deinitialization)
343 * @hapd: Pointer to BSS data
344 *
345 * This function is used to free all per-BSS data structures and resources.
Dmitry Shmidt54605472013-11-08 11:10:19 -0800346 * Most of the modules that are initialized in hostapd_setup_bss() are
347 * deinitialized here.
Dmitry Shmidt04949592012-07-19 12:16:46 -0700348 */
349static void hostapd_cleanup(struct hostapd_data *hapd)
350{
Dmitry Shmidt54605472013-11-08 11:10:19 -0800351 wpa_printf(MSG_DEBUG, "%s(hapd=%p (%s))", __func__, hapd,
352 hapd->conf->iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700353 if (hapd->iface->interfaces &&
354 hapd->iface->interfaces->ctrl_iface_deinit)
355 hapd->iface->interfaces->ctrl_iface_deinit(hapd);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700356 hostapd_free_hapd_data(hapd);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700357}
358
359
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800360static void sta_track_deinit(struct hostapd_iface *iface)
361{
362 struct hostapd_sta_info *info;
363
364 if (!iface->num_sta_seen)
365 return;
366
367 while ((info = dl_list_first(&iface->sta_seen, struct hostapd_sta_info,
368 list))) {
369 dl_list_del(&info->list);
370 iface->num_sta_seen--;
371 os_free(info);
372 }
373}
374
375
Dmitry Shmidt04949592012-07-19 12:16:46 -0700376static void hostapd_cleanup_iface_partial(struct hostapd_iface *iface)
377{
Dmitry Shmidt54605472013-11-08 11:10:19 -0800378 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800379#ifdef CONFIG_IEEE80211N
380#ifdef NEED_AP_MLME
381 hostapd_stop_setup_timers(iface);
382#endif /* NEED_AP_MLME */
383#endif /* CONFIG_IEEE80211N */
Dmitry Shmidt04949592012-07-19 12:16:46 -0700384 hostapd_free_hw_features(iface->hw_features, iface->num_hw_features);
385 iface->hw_features = NULL;
386 os_free(iface->current_rates);
387 iface->current_rates = NULL;
388 os_free(iface->basic_rates);
389 iface->basic_rates = NULL;
390 ap_list_deinit(iface);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800391 sta_track_deinit(iface);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700392}
393
394
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700395/**
396 * hostapd_cleanup_iface - Complete per-interface cleanup
397 * @iface: Pointer to interface data
398 *
399 * This function is called after per-BSS data structures are deinitialized
400 * with hostapd_cleanup().
401 */
402static void hostapd_cleanup_iface(struct hostapd_iface *iface)
403{
Dmitry Shmidt54605472013-11-08 11:10:19 -0800404 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800405 eloop_cancel_timeout(channel_list_update_timeout, iface, NULL);
406
Dmitry Shmidt04949592012-07-19 12:16:46 -0700407 hostapd_cleanup_iface_partial(iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700408 hostapd_config_free(iface->conf);
409 iface->conf = NULL;
410
411 os_free(iface->config_fname);
412 os_free(iface->bss);
Dmitry Shmidt54605472013-11-08 11:10:19 -0800413 wpa_printf(MSG_DEBUG, "%s: free iface=%p", __func__, iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700414 os_free(iface);
415}
416
417
Dmitry Shmidt04949592012-07-19 12:16:46 -0700418static void hostapd_clear_wep(struct hostapd_data *hapd)
419{
Dmitry Shmidta38abf92014-03-06 13:38:44 -0800420 if (hapd->drv_priv && !hapd->iface->driver_ap_teardown) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700421 hostapd_set_privacy(hapd, 0);
422 hostapd_broadcast_wep_clear(hapd);
423 }
424}
425
426
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700427static int hostapd_setup_encryption(char *iface, struct hostapd_data *hapd)
428{
429 int i;
430
431 hostapd_broadcast_wep_set(hapd);
432
433 if (hapd->conf->ssid.wep.default_len) {
434 hostapd_set_privacy(hapd, 1);
435 return 0;
436 }
437
Jouni Malinen75ecf522011-06-27 15:19:46 -0700438 /*
439 * When IEEE 802.1X is not enabled, the driver may need to know how to
440 * set authentication algorithms for static WEP.
441 */
442 hostapd_drv_set_authmode(hapd, hapd->conf->auth_algs);
443
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700444 for (i = 0; i < 4; i++) {
445 if (hapd->conf->ssid.wep.key[i] &&
446 hostapd_drv_set_key(iface, hapd, WPA_ALG_WEP, NULL, i,
447 i == hapd->conf->ssid.wep.idx, NULL, 0,
448 hapd->conf->ssid.wep.key[i],
449 hapd->conf->ssid.wep.len[i])) {
450 wpa_printf(MSG_WARNING, "Could not set WEP "
451 "encryption.");
452 return -1;
453 }
454 if (hapd->conf->ssid.wep.key[i] &&
455 i == hapd->conf->ssid.wep.idx)
456 hostapd_set_privacy(hapd, 1);
457 }
458
459 return 0;
460}
461
462
Dmitry Shmidt04949592012-07-19 12:16:46 -0700463static int hostapd_flush_old_stations(struct hostapd_data *hapd, u16 reason)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700464{
465 int ret = 0;
466 u8 addr[ETH_ALEN];
467
468 if (hostapd_drv_none(hapd) || hapd->drv_priv == NULL)
469 return 0;
470
Dmitry Shmidta38abf92014-03-06 13:38:44 -0800471 if (!hapd->iface->driver_ap_teardown) {
472 wpa_dbg(hapd->msg_ctx, MSG_DEBUG,
473 "Flushing old station entries");
474
475 if (hostapd_flush(hapd)) {
476 wpa_msg(hapd->msg_ctx, MSG_WARNING,
477 "Could not connect to kernel driver");
478 ret = -1;
479 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700480 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800481 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "Deauthenticate all stations");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700482 os_memset(addr, 0xff, ETH_ALEN);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700483 hostapd_drv_sta_deauth(hapd, addr, reason);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700484 hostapd_free_stas(hapd);
485
486 return ret;
487}
488
489
Dmitry Shmidt71757432014-06-02 13:50:35 -0700490static void hostapd_bss_deinit_no_free(struct hostapd_data *hapd)
491{
492 hostapd_free_stas(hapd);
493 hostapd_flush_old_stations(hapd, WLAN_REASON_DEAUTH_LEAVING);
494 hostapd_clear_wep(hapd);
495}
496
497
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700498/**
499 * hostapd_validate_bssid_configuration - Validate BSSID configuration
500 * @iface: Pointer to interface data
501 * Returns: 0 on success, -1 on failure
502 *
503 * This function is used to validate that the configured BSSIDs are valid.
504 */
505static int hostapd_validate_bssid_configuration(struct hostapd_iface *iface)
506{
507 u8 mask[ETH_ALEN] = { 0 };
508 struct hostapd_data *hapd = iface->bss[0];
509 unsigned int i = iface->conf->num_bss, bits = 0, j;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700510 int auto_addr = 0;
511
512 if (hostapd_drv_none(hapd))
513 return 0;
514
515 /* Generate BSSID mask that is large enough to cover the BSSIDs. */
516
517 /* Determine the bits necessary to cover the number of BSSIDs. */
518 for (i--; i; i >>= 1)
519 bits++;
520
521 /* Determine the bits necessary to any configured BSSIDs,
522 if they are higher than the number of BSSIDs. */
523 for (j = 0; j < iface->conf->num_bss; j++) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800524 if (hostapd_mac_comp_empty(iface->conf->bss[j]->bssid) == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700525 if (j)
526 auto_addr++;
527 continue;
528 }
529
530 for (i = 0; i < ETH_ALEN; i++) {
531 mask[i] |=
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800532 iface->conf->bss[j]->bssid[i] ^
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700533 hapd->own_addr[i];
534 }
535 }
536
537 if (!auto_addr)
538 goto skip_mask_ext;
539
540 for (i = 0; i < ETH_ALEN && mask[i] == 0; i++)
541 ;
542 j = 0;
543 if (i < ETH_ALEN) {
544 j = (5 - i) * 8;
545
546 while (mask[i] != 0) {
547 mask[i] >>= 1;
548 j++;
549 }
550 }
551
552 if (bits < j)
553 bits = j;
554
555 if (bits > 40) {
556 wpa_printf(MSG_ERROR, "Too many bits in the BSSID mask (%u)",
557 bits);
558 return -1;
559 }
560
561 os_memset(mask, 0xff, ETH_ALEN);
562 j = bits / 8;
563 for (i = 5; i > 5 - j; i--)
564 mask[i] = 0;
565 j = bits % 8;
566 while (j--)
567 mask[i] <<= 1;
568
569skip_mask_ext:
570 wpa_printf(MSG_DEBUG, "BSS count %lu, BSSID mask " MACSTR " (%d bits)",
571 (unsigned long) iface->conf->num_bss, MAC2STR(mask), bits);
572
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700573 if (!auto_addr)
574 return 0;
575
576 for (i = 0; i < ETH_ALEN; i++) {
577 if ((hapd->own_addr[i] & mask[i]) != hapd->own_addr[i]) {
578 wpa_printf(MSG_ERROR, "Invalid BSSID mask " MACSTR
579 " for start address " MACSTR ".",
580 MAC2STR(mask), MAC2STR(hapd->own_addr));
581 wpa_printf(MSG_ERROR, "Start address must be the "
582 "first address in the block (i.e., addr "
583 "AND mask == addr).");
584 return -1;
585 }
586 }
587
588 return 0;
589}
590
591
592static int mac_in_conf(struct hostapd_config *conf, const void *a)
593{
594 size_t i;
595
596 for (i = 0; i < conf->num_bss; i++) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800597 if (hostapd_mac_comp(conf->bss[i]->bssid, a) == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700598 return 1;
599 }
600 }
601
602 return 0;
603}
604
605
Dmitry Shmidt04949592012-07-19 12:16:46 -0700606#ifndef CONFIG_NO_RADIUS
607
608static int hostapd_das_nas_mismatch(struct hostapd_data *hapd,
609 struct radius_das_attrs *attr)
610{
Dmitry Shmidt13ca8d82014-02-20 10:18:40 -0800611 if (attr->nas_identifier &&
612 (!hapd->conf->nas_identifier ||
613 os_strlen(hapd->conf->nas_identifier) !=
614 attr->nas_identifier_len ||
615 os_memcmp(hapd->conf->nas_identifier, attr->nas_identifier,
616 attr->nas_identifier_len) != 0)) {
617 wpa_printf(MSG_DEBUG, "RADIUS DAS: NAS-Identifier mismatch");
618 return 1;
619 }
620
621 if (attr->nas_ip_addr &&
622 (hapd->conf->own_ip_addr.af != AF_INET ||
623 os_memcmp(&hapd->conf->own_ip_addr.u.v4, attr->nas_ip_addr, 4) !=
624 0)) {
625 wpa_printf(MSG_DEBUG, "RADIUS DAS: NAS-IP-Address mismatch");
626 return 1;
627 }
628
629#ifdef CONFIG_IPV6
630 if (attr->nas_ipv6_addr &&
631 (hapd->conf->own_ip_addr.af != AF_INET6 ||
632 os_memcmp(&hapd->conf->own_ip_addr.u.v6, attr->nas_ipv6_addr, 16)
633 != 0)) {
634 wpa_printf(MSG_DEBUG, "RADIUS DAS: NAS-IPv6-Address mismatch");
635 return 1;
636 }
637#endif /* CONFIG_IPV6 */
638
Dmitry Shmidt04949592012-07-19 12:16:46 -0700639 return 0;
640}
641
642
643static struct sta_info * hostapd_das_find_sta(struct hostapd_data *hapd,
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800644 struct radius_das_attrs *attr,
645 int *multi)
Dmitry Shmidt04949592012-07-19 12:16:46 -0700646{
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800647 struct sta_info *selected, *sta;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700648 char buf[128];
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800649 int num_attr = 0;
650 int count;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700651
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800652 *multi = 0;
653
654 for (sta = hapd->sta_list; sta; sta = sta->next)
655 sta->radius_das_match = 1;
656
657 if (attr->sta_addr) {
658 num_attr++;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700659 sta = ap_get_sta(hapd, attr->sta_addr);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800660 if (!sta) {
661 wpa_printf(MSG_DEBUG,
662 "RADIUS DAS: No Calling-Station-Id match");
663 return NULL;
664 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700665
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800666 selected = sta;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700667 for (sta = hapd->sta_list; sta; sta = sta->next) {
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800668 if (sta != selected)
669 sta->radius_das_match = 0;
670 }
671 wpa_printf(MSG_DEBUG, "RADIUS DAS: Calling-Station-Id match");
672 }
673
674 if (attr->acct_session_id) {
675 num_attr++;
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800676 if (attr->acct_session_id_len != 16) {
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800677 wpa_printf(MSG_DEBUG,
678 "RADIUS DAS: Acct-Session-Id cannot match");
679 return NULL;
680 }
681 count = 0;
682
683 for (sta = hapd->sta_list; sta; sta = sta->next) {
684 if (!sta->radius_das_match)
685 continue;
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800686 os_snprintf(buf, sizeof(buf), "%016lX",
687 (long unsigned int) sta->acct_session_id);
688 if (os_memcmp(attr->acct_session_id, buf, 16) != 0)
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800689 sta->radius_das_match = 0;
690 else
691 count++;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700692 }
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800693
694 if (count == 0) {
695 wpa_printf(MSG_DEBUG,
696 "RADIUS DAS: No matches remaining after Acct-Session-Id check");
697 return NULL;
698 }
699 wpa_printf(MSG_DEBUG, "RADIUS DAS: Acct-Session-Id match");
Dmitry Shmidt04949592012-07-19 12:16:46 -0700700 }
701
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800702 if (attr->acct_multi_session_id) {
703 num_attr++;
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800704 if (attr->acct_multi_session_id_len != 16) {
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800705 wpa_printf(MSG_DEBUG,
706 "RADIUS DAS: Acct-Multi-Session-Id cannot match");
707 return NULL;
708 }
709 count = 0;
710
711 for (sta = hapd->sta_list; sta; sta = sta->next) {
712 if (!sta->radius_das_match)
713 continue;
714 if (!sta->eapol_sm ||
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800715 !sta->eapol_sm->acct_multi_session_id) {
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800716 sta->radius_das_match = 0;
717 continue;
718 }
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800719 os_snprintf(buf, sizeof(buf), "%016lX",
720 (long unsigned int)
721 sta->eapol_sm->acct_multi_session_id);
722 if (os_memcmp(attr->acct_multi_session_id, buf, 16) !=
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800723 0)
724 sta->radius_das_match = 0;
725 else
726 count++;
727 }
728
729 if (count == 0) {
730 wpa_printf(MSG_DEBUG,
731 "RADIUS DAS: No matches remaining after Acct-Multi-Session-Id check");
732 return NULL;
733 }
734 wpa_printf(MSG_DEBUG,
735 "RADIUS DAS: Acct-Multi-Session-Id match");
736 }
737
738 if (attr->cui) {
739 num_attr++;
740 count = 0;
741
Dmitry Shmidt04949592012-07-19 12:16:46 -0700742 for (sta = hapd->sta_list; sta; sta = sta->next) {
743 struct wpabuf *cui;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800744
745 if (!sta->radius_das_match)
746 continue;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700747 cui = ieee802_1x_get_radius_cui(sta->eapol_sm);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800748 if (!cui || wpabuf_len(cui) != attr->cui_len ||
Dmitry Shmidt04949592012-07-19 12:16:46 -0700749 os_memcmp(wpabuf_head(cui), attr->cui,
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800750 attr->cui_len) != 0)
751 sta->radius_das_match = 0;
752 else
753 count++;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700754 }
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800755
756 if (count == 0) {
757 wpa_printf(MSG_DEBUG,
758 "RADIUS DAS: No matches remaining after Chargeable-User-Identity check");
759 return NULL;
760 }
761 wpa_printf(MSG_DEBUG,
762 "RADIUS DAS: Chargeable-User-Identity match");
Dmitry Shmidt04949592012-07-19 12:16:46 -0700763 }
764
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800765 if (attr->user_name) {
766 num_attr++;
767 count = 0;
768
Dmitry Shmidt04949592012-07-19 12:16:46 -0700769 for (sta = hapd->sta_list; sta; sta = sta->next) {
770 u8 *identity;
771 size_t identity_len;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800772
773 if (!sta->radius_das_match)
774 continue;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700775 identity = ieee802_1x_get_identity(sta->eapol_sm,
776 &identity_len);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800777 if (!identity ||
778 identity_len != attr->user_name_len ||
Dmitry Shmidt04949592012-07-19 12:16:46 -0700779 os_memcmp(identity, attr->user_name, identity_len)
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800780 != 0)
781 sta->radius_das_match = 0;
782 else
783 count++;
784 }
785
786 if (count == 0) {
787 wpa_printf(MSG_DEBUG,
788 "RADIUS DAS: No matches remaining after User-Name check");
789 return NULL;
790 }
791 wpa_printf(MSG_DEBUG,
792 "RADIUS DAS: User-Name match");
793 }
794
795 if (num_attr == 0) {
796 /*
797 * In theory, we could match all current associations, but it
798 * seems safer to just reject requests that do not include any
799 * session identification attributes.
800 */
801 wpa_printf(MSG_DEBUG,
802 "RADIUS DAS: No session identification attributes included");
803 return NULL;
804 }
805
806 selected = NULL;
807 for (sta = hapd->sta_list; sta; sta = sta->next) {
808 if (sta->radius_das_match) {
809 if (selected) {
810 *multi = 1;
811 return NULL;
812 }
813 selected = sta;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700814 }
815 }
816
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800817 return selected;
818}
819
820
821static int hostapd_das_disconnect_pmksa(struct hostapd_data *hapd,
822 struct radius_das_attrs *attr)
823{
824 if (!hapd->wpa_auth)
825 return -1;
826 return wpa_auth_radius_das_disconnect_pmksa(hapd->wpa_auth, attr);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700827}
828
829
830static enum radius_das_res
831hostapd_das_disconnect(void *ctx, struct radius_das_attrs *attr)
832{
833 struct hostapd_data *hapd = ctx;
834 struct sta_info *sta;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800835 int multi;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700836
837 if (hostapd_das_nas_mismatch(hapd, attr))
838 return RADIUS_DAS_NAS_MISMATCH;
839
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800840 sta = hostapd_das_find_sta(hapd, attr, &multi);
841 if (sta == NULL) {
842 if (multi) {
843 wpa_printf(MSG_DEBUG,
844 "RADIUS DAS: Multiple sessions match - not supported");
845 return RADIUS_DAS_MULTI_SESSION_MATCH;
846 }
847 if (hostapd_das_disconnect_pmksa(hapd, attr) == 0) {
848 wpa_printf(MSG_DEBUG,
849 "RADIUS DAS: PMKSA cache entry matched");
850 return RADIUS_DAS_SUCCESS;
851 }
852 wpa_printf(MSG_DEBUG, "RADIUS DAS: No matching session found");
Dmitry Shmidt04949592012-07-19 12:16:46 -0700853 return RADIUS_DAS_SESSION_NOT_FOUND;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800854 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700855
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800856 wpa_printf(MSG_DEBUG, "RADIUS DAS: Found a matching session " MACSTR
857 " - disconnecting", MAC2STR(sta->addr));
Dmitry Shmidt13ca8d82014-02-20 10:18:40 -0800858 wpa_auth_pmksa_remove(hapd->wpa_auth, sta->addr);
859
Dmitry Shmidt04949592012-07-19 12:16:46 -0700860 hostapd_drv_sta_deauth(hapd, sta->addr,
861 WLAN_REASON_PREV_AUTH_NOT_VALID);
862 ap_sta_deauthenticate(hapd, sta, WLAN_REASON_PREV_AUTH_NOT_VALID);
863
864 return RADIUS_DAS_SUCCESS;
865}
866
867#endif /* CONFIG_NO_RADIUS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700868
869
870/**
871 * hostapd_setup_bss - Per-BSS setup (initialization)
872 * @hapd: Pointer to BSS data
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800873 * @first: Whether this BSS is the first BSS of an interface; -1 = not first,
874 * but interface may exist
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700875 *
876 * This function is used to initialize all per-BSS data structures and
877 * resources. This gets called in a loop for each BSS when an interface is
878 * initialized. Most of the modules that are initialized here will be
879 * deinitialized in hostapd_cleanup().
880 */
881static int hostapd_setup_bss(struct hostapd_data *hapd, int first)
882{
883 struct hostapd_bss_config *conf = hapd->conf;
Dmitry Shmidt9d9e6022015-04-23 10:34:55 -0700884 u8 ssid[SSID_MAX_LEN + 1];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700885 int ssid_len, set_ssid;
886 char force_ifname[IFNAMSIZ];
887 u8 if_addr[ETH_ALEN];
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800888 int flush_old_stations = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700889
Dmitry Shmidt54605472013-11-08 11:10:19 -0800890 wpa_printf(MSG_DEBUG, "%s(hapd=%p (%s), first=%d)",
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700891 __func__, hapd, conf->iface, first);
Dmitry Shmidt54605472013-11-08 11:10:19 -0800892
Dmitry Shmidt50b691d2014-05-21 14:01:45 -0700893#ifdef EAP_SERVER_TNC
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700894 if (conf->tnc && tncs_global_init() < 0) {
Dmitry Shmidt50b691d2014-05-21 14:01:45 -0700895 wpa_printf(MSG_ERROR, "Failed to initialize TNCS");
896 return -1;
897 }
898#endif /* EAP_SERVER_TNC */
899
Dmitry Shmidt54605472013-11-08 11:10:19 -0800900 if (hapd->started) {
901 wpa_printf(MSG_ERROR, "%s: Interface %s was already started",
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700902 __func__, conf->iface);
Dmitry Shmidt54605472013-11-08 11:10:19 -0800903 return -1;
904 }
905 hapd->started = 1;
906
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800907 if (!first || first == -1) {
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700908 if (hostapd_mac_comp_empty(conf->bssid) == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700909 /* Allocate the next available BSSID. */
910 do {
911 inc_byte_array(hapd->own_addr, ETH_ALEN);
912 } while (mac_in_conf(hapd->iconf, hapd->own_addr));
913 } else {
914 /* Allocate the configured BSSID. */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700915 os_memcpy(hapd->own_addr, conf->bssid, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700916
917 if (hostapd_mac_comp(hapd->own_addr,
918 hapd->iface->bss[0]->own_addr) ==
919 0) {
920 wpa_printf(MSG_ERROR, "BSS '%s' may not have "
921 "BSSID set to the MAC address of "
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700922 "the radio", conf->iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700923 return -1;
924 }
925 }
926
Dmitry Shmidt54605472013-11-08 11:10:19 -0800927 hapd->interface_added = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700928 if (hostapd_if_add(hapd->iface->bss[0], WPA_IF_AP_BSS,
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700929 conf->iface, hapd->own_addr, hapd,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700930 &hapd->drv_priv, force_ifname, if_addr,
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700931 conf->bridge[0] ? conf->bridge : NULL,
932 first == -1)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700933 wpa_printf(MSG_ERROR, "Failed to add BSS (BSSID="
934 MACSTR ")", MAC2STR(hapd->own_addr));
Dmitry Shmidt3cf6f792013-12-18 13:12:19 -0800935 hapd->interface_added = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700936 return -1;
937 }
938 }
939
940 if (conf->wmm_enabled < 0)
941 conf->wmm_enabled = hapd->iconf->ieee80211n;
942
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800943#ifdef CONFIG_MESH
944 if (hapd->iface->mconf == NULL)
945 flush_old_stations = 0;
946#endif /* CONFIG_MESH */
947
948 if (flush_old_stations)
949 hostapd_flush_old_stations(hapd,
950 WLAN_REASON_PREV_AUTH_NOT_VALID);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700951 hostapd_set_privacy(hapd, 0);
952
953 hostapd_broadcast_wep_clear(hapd);
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700954 if (hostapd_setup_encryption(conf->iface, hapd))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700955 return -1;
956
957 /*
958 * Fetch the SSID from the system and use it or,
959 * if one was specified in the config file, verify they
960 * match.
961 */
962 ssid_len = hostapd_get_ssid(hapd, ssid, sizeof(ssid));
963 if (ssid_len < 0) {
964 wpa_printf(MSG_ERROR, "Could not read SSID from system");
965 return -1;
966 }
967 if (conf->ssid.ssid_set) {
968 /*
969 * If SSID is specified in the config file and it differs
970 * from what is being used then force installation of the
971 * new SSID.
972 */
973 set_ssid = (conf->ssid.ssid_len != (size_t) ssid_len ||
974 os_memcmp(conf->ssid.ssid, ssid, ssid_len) != 0);
975 } else {
976 /*
977 * No SSID in the config file; just use the one we got
978 * from the system.
979 */
980 set_ssid = 0;
981 conf->ssid.ssid_len = ssid_len;
982 os_memcpy(conf->ssid.ssid, ssid, conf->ssid.ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700983 }
984
985 if (!hostapd_drv_none(hapd)) {
986 wpa_printf(MSG_ERROR, "Using interface %s with hwaddr " MACSTR
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700987 " and ssid \"%s\"",
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700988 conf->iface, MAC2STR(hapd->own_addr),
989 wpa_ssid_txt(conf->ssid.ssid, conf->ssid.ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700990 }
991
992 if (hostapd_setup_wpa_psk(conf)) {
993 wpa_printf(MSG_ERROR, "WPA-PSK setup failed.");
994 return -1;
995 }
996
997 /* Set SSID for the kernel driver (to be used in beacon and probe
998 * response frames) */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700999 if (set_ssid && hostapd_set_ssid(hapd, conf->ssid.ssid,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001000 conf->ssid.ssid_len)) {
1001 wpa_printf(MSG_ERROR, "Could not set SSID for kernel driver");
1002 return -1;
1003 }
1004
Dmitry Shmidt818ea482014-03-10 13:15:21 -07001005 if (wpa_debug_level <= MSG_MSGDUMP)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001006 conf->radius->msg_dumps = 1;
1007#ifndef CONFIG_NO_RADIUS
1008 hapd->radius = radius_client_init(hapd, conf->radius);
1009 if (hapd->radius == NULL) {
1010 wpa_printf(MSG_ERROR, "RADIUS client initialization failed.");
1011 return -1;
1012 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001013
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001014 if (conf->radius_das_port) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001015 struct radius_das_conf das_conf;
1016 os_memset(&das_conf, 0, sizeof(das_conf));
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001017 das_conf.port = conf->radius_das_port;
1018 das_conf.shared_secret = conf->radius_das_shared_secret;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001019 das_conf.shared_secret_len =
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001020 conf->radius_das_shared_secret_len;
1021 das_conf.client_addr = &conf->radius_das_client_addr;
1022 das_conf.time_window = conf->radius_das_time_window;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001023 das_conf.require_event_timestamp =
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001024 conf->radius_das_require_event_timestamp;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001025 das_conf.ctx = hapd;
1026 das_conf.disconnect = hostapd_das_disconnect;
1027 hapd->radius_das = radius_das_init(&das_conf);
1028 if (hapd->radius_das == NULL) {
1029 wpa_printf(MSG_ERROR, "RADIUS DAS initialization "
1030 "failed.");
1031 return -1;
1032 }
1033 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001034#endif /* CONFIG_NO_RADIUS */
1035
1036 if (hostapd_acl_init(hapd)) {
1037 wpa_printf(MSG_ERROR, "ACL initialization failed.");
1038 return -1;
1039 }
1040 if (hostapd_init_wps(hapd, conf))
1041 return -1;
1042
1043 if (authsrv_init(hapd) < 0)
1044 return -1;
1045
1046 if (ieee802_1x_init(hapd)) {
1047 wpa_printf(MSG_ERROR, "IEEE 802.1X initialization failed.");
1048 return -1;
1049 }
1050
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001051 if ((conf->wpa || conf->osen) && hostapd_setup_wpa(hapd))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001052 return -1;
1053
1054 if (accounting_init(hapd)) {
1055 wpa_printf(MSG_ERROR, "Accounting initialization failed.");
1056 return -1;
1057 }
1058
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001059 if (conf->ieee802_11f &&
1060 (hapd->iapp = iapp_init(hapd, conf->iapp_iface)) == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001061 wpa_printf(MSG_ERROR, "IEEE 802.11F (IAPP) initialization "
1062 "failed.");
1063 return -1;
1064 }
1065
Dmitry Shmidt04949592012-07-19 12:16:46 -07001066#ifdef CONFIG_INTERWORKING
1067 if (gas_serv_init(hapd)) {
1068 wpa_printf(MSG_ERROR, "GAS server initialization failed");
1069 return -1;
1070 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001071
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001072 if (conf->qos_map_set_len &&
1073 hostapd_drv_set_qos_map(hapd, conf->qos_map_set,
1074 conf->qos_map_set_len)) {
1075 wpa_printf(MSG_ERROR, "Failed to initialize QoS Map");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001076 return -1;
1077 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001078#endif /* CONFIG_INTERWORKING */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001079
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001080 if (conf->bss_load_update_period && bss_load_update_init(hapd)) {
1081 wpa_printf(MSG_ERROR, "BSS Load initialization failed");
1082 return -1;
1083 }
1084
1085 if (conf->proxy_arp) {
1086 if (x_snoop_init(hapd)) {
1087 wpa_printf(MSG_ERROR,
1088 "Generic snooping infrastructure initialization failed");
1089 return -1;
1090 }
1091
1092 if (dhcp_snoop_init(hapd)) {
1093 wpa_printf(MSG_ERROR,
1094 "DHCP snooping initialization failed");
1095 return -1;
1096 }
1097
1098 if (ndisc_snoop_init(hapd)) {
1099 wpa_printf(MSG_ERROR,
1100 "Neighbor Discovery snooping initialization failed");
1101 return -1;
1102 }
1103 }
1104
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001105 if (!hostapd_drv_none(hapd) && vlan_init(hapd)) {
1106 wpa_printf(MSG_ERROR, "VLAN initialization failed.");
1107 return -1;
1108 }
1109
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001110 if (!conf->start_disabled && ieee802_11_set_beacon(hapd) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001111 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001112
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001113 if (hapd->wpa_auth && wpa_init_keys(hapd->wpa_auth) < 0)
1114 return -1;
1115
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001116 if (hapd->driver && hapd->driver->set_operstate)
1117 hapd->driver->set_operstate(hapd->drv_priv, 1);
1118
1119 return 0;
1120}
1121
1122
1123static void hostapd_tx_queue_params(struct hostapd_iface *iface)
1124{
1125 struct hostapd_data *hapd = iface->bss[0];
1126 int i;
1127 struct hostapd_tx_queue_params *p;
1128
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001129#ifdef CONFIG_MESH
1130 if (iface->mconf == NULL)
1131 return;
1132#endif /* CONFIG_MESH */
1133
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001134 for (i = 0; i < NUM_TX_QUEUES; i++) {
1135 p = &iface->conf->tx_queue[i];
1136
1137 if (hostapd_set_tx_queue_params(hapd, i, p->aifs, p->cwmin,
1138 p->cwmax, p->burst)) {
1139 wpa_printf(MSG_DEBUG, "Failed to set TX queue "
1140 "parameters for queue %d.", i);
1141 /* Continue anyway */
1142 }
1143 }
1144}
1145
1146
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001147static int hostapd_set_acl_list(struct hostapd_data *hapd,
1148 struct mac_acl_entry *mac_acl,
1149 int n_entries, u8 accept_acl)
1150{
1151 struct hostapd_acl_params *acl_params;
1152 int i, err;
1153
1154 acl_params = os_zalloc(sizeof(*acl_params) +
1155 (n_entries * sizeof(acl_params->mac_acl[0])));
1156 if (!acl_params)
1157 return -ENOMEM;
1158
1159 for (i = 0; i < n_entries; i++)
1160 os_memcpy(acl_params->mac_acl[i].addr, mac_acl[i].addr,
1161 ETH_ALEN);
1162
1163 acl_params->acl_policy = accept_acl;
1164 acl_params->num_mac_acl = n_entries;
1165
1166 err = hostapd_drv_set_acl(hapd, acl_params);
1167
1168 os_free(acl_params);
1169
1170 return err;
1171}
1172
1173
1174static void hostapd_set_acl(struct hostapd_data *hapd)
1175{
1176 struct hostapd_config *conf = hapd->iconf;
1177 int err;
1178 u8 accept_acl;
1179
1180 if (hapd->iface->drv_max_acl_mac_addrs == 0)
1181 return;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001182
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001183 if (conf->bss[0]->macaddr_acl == DENY_UNLESS_ACCEPTED) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001184 accept_acl = 1;
1185 err = hostapd_set_acl_list(hapd, conf->bss[0]->accept_mac,
1186 conf->bss[0]->num_accept_mac,
1187 accept_acl);
1188 if (err) {
1189 wpa_printf(MSG_DEBUG, "Failed to set accept acl");
1190 return;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001191 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001192 } else if (conf->bss[0]->macaddr_acl == ACCEPT_UNLESS_DENIED) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001193 accept_acl = 0;
1194 err = hostapd_set_acl_list(hapd, conf->bss[0]->deny_mac,
1195 conf->bss[0]->num_deny_mac,
1196 accept_acl);
1197 if (err) {
1198 wpa_printf(MSG_DEBUG, "Failed to set deny acl");
1199 return;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001200 }
1201 }
1202}
1203
1204
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001205static int start_ctrl_iface_bss(struct hostapd_data *hapd)
1206{
1207 if (!hapd->iface->interfaces ||
1208 !hapd->iface->interfaces->ctrl_iface_init)
1209 return 0;
1210
1211 if (hapd->iface->interfaces->ctrl_iface_init(hapd)) {
1212 wpa_printf(MSG_ERROR,
1213 "Failed to setup control interface for %s",
1214 hapd->conf->iface);
1215 return -1;
1216 }
1217
1218 return 0;
1219}
1220
1221
1222static int start_ctrl_iface(struct hostapd_iface *iface)
1223{
1224 size_t i;
1225
1226 if (!iface->interfaces || !iface->interfaces->ctrl_iface_init)
1227 return 0;
1228
1229 for (i = 0; i < iface->num_bss; i++) {
1230 struct hostapd_data *hapd = iface->bss[i];
1231 if (iface->interfaces->ctrl_iface_init(hapd)) {
1232 wpa_printf(MSG_ERROR,
1233 "Failed to setup control interface for %s",
1234 hapd->conf->iface);
1235 return -1;
1236 }
1237 }
1238
1239 return 0;
1240}
1241
1242
1243static void channel_list_update_timeout(void *eloop_ctx, void *timeout_ctx)
1244{
1245 struct hostapd_iface *iface = eloop_ctx;
1246
1247 if (!iface->wait_channel_update) {
1248 wpa_printf(MSG_INFO, "Channel list update timeout, but interface was not waiting for it");
1249 return;
1250 }
1251
1252 /*
1253 * It is possible that the existing channel list is acceptable, so try
1254 * to proceed.
1255 */
1256 wpa_printf(MSG_DEBUG, "Channel list update timeout - try to continue anyway");
1257 setup_interface2(iface);
1258}
1259
1260
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001261void hostapd_channel_list_updated(struct hostapd_iface *iface, int initiator)
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001262{
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001263 if (!iface->wait_channel_update || initiator != REGDOM_SET_BY_USER)
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001264 return;
1265
1266 wpa_printf(MSG_DEBUG, "Channel list updated - continue setup");
1267 eloop_cancel_timeout(channel_list_update_timeout, iface, NULL);
1268 setup_interface2(iface);
1269}
1270
1271
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001272static int setup_interface(struct hostapd_iface *iface)
1273{
1274 struct hostapd_data *hapd = iface->bss[0];
1275 size_t i;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001276
Dmitry Shmidta38abf92014-03-06 13:38:44 -08001277 /*
1278 * It is possible that setup_interface() is called after the interface
1279 * was disabled etc., in which case driver_ap_teardown is possibly set
1280 * to 1. Clear it here so any other key/station deletion, which is not
1281 * part of a teardown flow, would also call the relevant driver
1282 * callbacks.
1283 */
1284 iface->driver_ap_teardown = 0;
1285
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001286 if (!iface->phy[0]) {
1287 const char *phy = hostapd_drv_get_radio_name(hapd);
1288 if (phy) {
1289 wpa_printf(MSG_DEBUG, "phy: %s", phy);
1290 os_strlcpy(iface->phy, phy, sizeof(iface->phy));
1291 }
1292 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001293
1294 /*
1295 * Make sure that all BSSes get configured with a pointer to the same
1296 * driver interface.
1297 */
1298 for (i = 1; i < iface->num_bss; i++) {
1299 iface->bss[i]->driver = hapd->driver;
1300 iface->bss[i]->drv_priv = hapd->drv_priv;
1301 }
1302
1303 if (hostapd_validate_bssid_configuration(iface))
1304 return -1;
1305
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001306 /*
1307 * Initialize control interfaces early to allow external monitoring of
1308 * channel setup operations that may take considerable amount of time
1309 * especially for DFS cases.
1310 */
1311 if (start_ctrl_iface(iface))
1312 return -1;
1313
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001314 if (hapd->iconf->country[0] && hapd->iconf->country[1]) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001315 char country[4], previous_country[4];
1316
1317 hostapd_set_state(iface, HAPD_IFACE_COUNTRY_UPDATE);
1318 if (hostapd_get_country(hapd, previous_country) < 0)
1319 previous_country[0] = '\0';
1320
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001321 os_memcpy(country, hapd->iconf->country, 3);
1322 country[3] = '\0';
1323 if (hostapd_set_country(hapd, country) < 0) {
1324 wpa_printf(MSG_ERROR, "Failed to set country code");
1325 return -1;
1326 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001327
1328 wpa_printf(MSG_DEBUG, "Previous country code %s, new country code %s",
1329 previous_country, country);
1330
1331 if (os_strncmp(previous_country, country, 2) != 0) {
1332 wpa_printf(MSG_DEBUG, "Continue interface setup after channel list update");
1333 iface->wait_channel_update = 1;
Dmitry Shmidt97672262014-02-03 13:02:54 -08001334 eloop_register_timeout(5, 0,
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001335 channel_list_update_timeout,
1336 iface, NULL);
1337 return 0;
1338 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001339 }
1340
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001341 return setup_interface2(iface);
1342}
1343
1344
1345static int setup_interface2(struct hostapd_iface *iface)
1346{
1347 iface->wait_channel_update = 0;
1348
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001349 if (hostapd_get_hw_features(iface)) {
1350 /* Not all drivers support this yet, so continue without hw
1351 * feature data. */
1352 } else {
1353 int ret = hostapd_select_hw_mode(iface);
1354 if (ret < 0) {
1355 wpa_printf(MSG_ERROR, "Could not select hw_mode and "
1356 "channel. (%d)", ret);
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001357 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001358 }
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001359 if (ret == 1) {
1360 wpa_printf(MSG_DEBUG, "Interface initialization will be completed in a callback (ACS)");
1361 return 0;
1362 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001363 ret = hostapd_check_ht_capab(iface);
1364 if (ret < 0)
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001365 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001366 if (ret == 1) {
1367 wpa_printf(MSG_DEBUG, "Interface initialization will "
1368 "be completed in a callback");
1369 return 0;
1370 }
Dmitry Shmidt051af732013-10-22 13:52:46 -07001371
1372 if (iface->conf->ieee80211h)
1373 wpa_printf(MSG_DEBUG, "DFS support is enabled");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001374 }
1375 return hostapd_setup_interface_complete(iface, 0);
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001376
1377fail:
1378 hostapd_set_state(iface, HAPD_IFACE_DISABLED);
1379 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
1380 if (iface->interfaces && iface->interfaces->terminate_on_error)
1381 eloop_terminate();
1382 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001383}
1384
1385
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001386#ifdef CONFIG_FST
1387
1388static const u8 * fst_hostapd_get_bssid_cb(void *ctx)
1389{
1390 struct hostapd_data *hapd = ctx;
1391
1392 return hapd->own_addr;
1393}
1394
1395
1396static void fst_hostapd_get_channel_info_cb(void *ctx,
1397 enum hostapd_hw_mode *hw_mode,
1398 u8 *channel)
1399{
1400 struct hostapd_data *hapd = ctx;
1401
1402 *hw_mode = ieee80211_freq_to_chan(hapd->iface->freq, channel);
1403}
1404
1405
1406static void fst_hostapd_set_ies_cb(void *ctx, const struct wpabuf *fst_ies)
1407{
1408 struct hostapd_data *hapd = ctx;
1409
1410 if (hapd->iface->fst_ies != fst_ies) {
1411 hapd->iface->fst_ies = fst_ies;
1412 if (ieee802_11_set_beacon(hapd))
1413 wpa_printf(MSG_WARNING, "FST: Cannot set beacon");
1414 }
1415}
1416
1417
1418static int fst_hostapd_send_action_cb(void *ctx, const u8 *da,
1419 struct wpabuf *buf)
1420{
1421 struct hostapd_data *hapd = ctx;
1422
1423 return hostapd_drv_send_action(hapd, hapd->iface->freq, 0, da,
1424 wpabuf_head(buf), wpabuf_len(buf));
1425}
1426
1427
1428static const struct wpabuf * fst_hostapd_get_mb_ie_cb(void *ctx, const u8 *addr)
1429{
1430 struct hostapd_data *hapd = ctx;
1431 struct sta_info *sta = ap_get_sta(hapd, addr);
1432
1433 return sta ? sta->mb_ies : NULL;
1434}
1435
1436
1437static void fst_hostapd_update_mb_ie_cb(void *ctx, const u8 *addr,
1438 const u8 *buf, size_t size)
1439{
1440 struct hostapd_data *hapd = ctx;
1441 struct sta_info *sta = ap_get_sta(hapd, addr);
1442
1443 if (sta) {
1444 struct mb_ies_info info;
1445
1446 if (!mb_ies_info_by_ies(&info, buf, size)) {
1447 wpabuf_free(sta->mb_ies);
1448 sta->mb_ies = mb_ies_by_info(&info);
1449 }
1450 }
1451}
1452
1453
1454static const u8 * fst_hostapd_get_sta(struct fst_get_peer_ctx **get_ctx,
1455 Boolean mb_only)
1456{
1457 struct sta_info *s = (struct sta_info *) *get_ctx;
1458
1459 if (mb_only) {
1460 for (; s && !s->mb_ies; s = s->next)
1461 ;
1462 }
1463
1464 if (s) {
1465 *get_ctx = (struct fst_get_peer_ctx *) s->next;
1466
1467 return s->addr;
1468 }
1469
1470 *get_ctx = NULL;
1471 return NULL;
1472}
1473
1474
1475static const u8 * fst_hostapd_get_peer_first(void *ctx,
1476 struct fst_get_peer_ctx **get_ctx,
1477 Boolean mb_only)
1478{
1479 struct hostapd_data *hapd = ctx;
1480
1481 *get_ctx = (struct fst_get_peer_ctx *) hapd->sta_list;
1482
1483 return fst_hostapd_get_sta(get_ctx, mb_only);
1484}
1485
1486
1487static const u8 * fst_hostapd_get_peer_next(void *ctx,
1488 struct fst_get_peer_ctx **get_ctx,
1489 Boolean mb_only)
1490{
1491 return fst_hostapd_get_sta(get_ctx, mb_only);
1492}
1493
1494
1495void fst_hostapd_fill_iface_obj(struct hostapd_data *hapd,
1496 struct fst_wpa_obj *iface_obj)
1497{
1498 iface_obj->ctx = hapd;
1499 iface_obj->get_bssid = fst_hostapd_get_bssid_cb;
1500 iface_obj->get_channel_info = fst_hostapd_get_channel_info_cb;
1501 iface_obj->set_ies = fst_hostapd_set_ies_cb;
1502 iface_obj->send_action = fst_hostapd_send_action_cb;
1503 iface_obj->get_mb_ie = fst_hostapd_get_mb_ie_cb;
1504 iface_obj->update_mb_ie = fst_hostapd_update_mb_ie_cb;
1505 iface_obj->get_peer_first = fst_hostapd_get_peer_first;
1506 iface_obj->get_peer_next = fst_hostapd_get_peer_next;
1507}
1508
1509#endif /* CONFIG_FST */
1510
1511
1512static int hostapd_setup_interface_complete_sync(struct hostapd_iface *iface,
1513 int err)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001514{
1515 struct hostapd_data *hapd = iface->bss[0];
1516 size_t j;
1517 u8 *prev_addr;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001518 int delay_apply_cfg = 0;
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001519 int res_dfs_offload = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001520
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001521 if (err)
1522 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001523
1524 wpa_printf(MSG_DEBUG, "Completing interface initialization");
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001525 if (iface->conf->channel) {
Dmitry Shmidt051af732013-10-22 13:52:46 -07001526#ifdef NEED_AP_MLME
1527 int res;
1528#endif /* NEED_AP_MLME */
1529
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001530 iface->freq = hostapd_hw_get_freq(hapd, iface->conf->channel);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001531 wpa_printf(MSG_DEBUG, "Mode: %s Channel: %d "
1532 "Frequency: %d MHz",
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001533 hostapd_hw_mode_txt(iface->conf->hw_mode),
1534 iface->conf->channel, iface->freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001535
Dmitry Shmidt051af732013-10-22 13:52:46 -07001536#ifdef NEED_AP_MLME
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001537 /* Handle DFS only if it is not offloaded to the driver */
1538 if (!(iface->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD)) {
1539 /* Check DFS */
1540 res = hostapd_handle_dfs(iface);
1541 if (res <= 0) {
1542 if (res < 0)
1543 goto fail;
1544 return res;
1545 }
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001546 } else {
1547 /* If DFS is offloaded to the driver */
1548 res_dfs_offload = hostapd_handle_dfs_offload(iface);
1549 if (res_dfs_offload <= 0) {
1550 if (res_dfs_offload < 0)
1551 goto fail;
1552 } else {
1553 wpa_printf(MSG_DEBUG,
1554 "Proceed with AP/channel setup");
1555 /*
1556 * If this is a DFS channel, move to completing
1557 * AP setup.
1558 */
1559 if (res_dfs_offload == 1)
1560 goto dfs_offload;
1561 /* Otherwise fall through. */
1562 }
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001563 }
Dmitry Shmidt051af732013-10-22 13:52:46 -07001564#endif /* NEED_AP_MLME */
1565
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001566#ifdef CONFIG_MESH
1567 if (iface->mconf != NULL) {
1568 wpa_printf(MSG_DEBUG,
1569 "%s: Mesh configuration will be applied while joining the mesh network",
1570 iface->bss[0]->conf->iface);
1571 delay_apply_cfg = 1;
1572 }
1573#endif /* CONFIG_MESH */
1574
1575 if (!delay_apply_cfg &&
1576 hostapd_set_freq(hapd, hapd->iconf->hw_mode, iface->freq,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001577 hapd->iconf->channel,
1578 hapd->iconf->ieee80211n,
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001579 hapd->iconf->ieee80211ac,
1580 hapd->iconf->secondary_channel,
1581 hapd->iconf->vht_oper_chwidth,
1582 hapd->iconf->vht_oper_centr_freq_seg0_idx,
1583 hapd->iconf->vht_oper_centr_freq_seg1_idx)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001584 wpa_printf(MSG_ERROR, "Could not set channel for "
1585 "kernel driver");
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001586 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001587 }
1588 }
1589
1590 if (iface->current_mode) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001591 if (hostapd_prepare_rates(iface, iface->current_mode)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001592 wpa_printf(MSG_ERROR, "Failed to prepare rates "
1593 "table.");
1594 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
1595 HOSTAPD_LEVEL_WARNING,
1596 "Failed to prepare rates table.");
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001597 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001598 }
1599 }
1600
1601 if (hapd->iconf->rts_threshold > -1 &&
1602 hostapd_set_rts(hapd, hapd->iconf->rts_threshold)) {
1603 wpa_printf(MSG_ERROR, "Could not set RTS threshold for "
1604 "kernel driver");
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001605 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001606 }
1607
1608 if (hapd->iconf->fragm_threshold > -1 &&
1609 hostapd_set_frag(hapd, hapd->iconf->fragm_threshold)) {
1610 wpa_printf(MSG_ERROR, "Could not set fragmentation threshold "
1611 "for kernel driver");
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001612 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001613 }
1614
1615 prev_addr = hapd->own_addr;
1616
1617 for (j = 0; j < iface->num_bss; j++) {
1618 hapd = iface->bss[j];
1619 if (j)
1620 os_memcpy(hapd->own_addr, prev_addr, ETH_ALEN);
Dmitry Shmidt71757432014-06-02 13:50:35 -07001621 if (hostapd_setup_bss(hapd, j == 0)) {
1622 do {
1623 hapd = iface->bss[j];
1624 hostapd_bss_deinit_no_free(hapd);
1625 hostapd_free_hapd_data(hapd);
1626 } while (j-- > 0);
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001627 goto fail;
Dmitry Shmidt71757432014-06-02 13:50:35 -07001628 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001629 if (hostapd_mac_comp_empty(hapd->conf->bssid) == 0)
1630 prev_addr = hapd->own_addr;
1631 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001632 hapd = iface->bss[0];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001633
1634 hostapd_tx_queue_params(iface);
1635
1636 ap_list_init(iface);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001637 dl_list_init(&iface->sta_seen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001638
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001639 hostapd_set_acl(hapd);
1640
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001641 if (hostapd_driver_commit(hapd) < 0) {
1642 wpa_printf(MSG_ERROR, "%s: Failed to commit driver "
1643 "configuration", __func__);
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001644 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001645 }
1646
Jouni Malinen87fd2792011-05-16 18:35:42 +03001647 /*
1648 * WPS UPnP module can be initialized only when the "upnp_iface" is up.
1649 * If "interface" and "upnp_iface" are the same (e.g., non-bridge
1650 * mode), the interface is up only after driver_commit, so initialize
1651 * WPS after driver_commit.
1652 */
1653 for (j = 0; j < iface->num_bss; j++) {
1654 if (hostapd_init_wps_complete(iface->bss[j]))
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001655 goto fail;
Jouni Malinen87fd2792011-05-16 18:35:42 +03001656 }
1657
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001658 if ((iface->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
1659 !res_dfs_offload) {
1660 /*
1661 * If freq is DFS, and DFS is offloaded to the driver, then wait
1662 * for CAC to complete.
1663 */
1664 wpa_printf(MSG_DEBUG, "%s: Wait for CAC to complete", __func__);
1665 return res_dfs_offload;
1666 }
1667
1668#ifdef NEED_AP_MLME
1669dfs_offload:
1670#endif /* NEED_AP_MLME */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001671
1672#ifdef CONFIG_FST
1673 if (hapd->iconf->fst_cfg.group_id[0]) {
1674 struct fst_wpa_obj iface_obj;
1675
1676 fst_hostapd_fill_iface_obj(hapd, &iface_obj);
1677 iface->fst = fst_attach(hapd->conf->iface, hapd->own_addr,
1678 &iface_obj, &hapd->iconf->fst_cfg);
1679 if (!iface->fst) {
1680 wpa_printf(MSG_ERROR, "Could not attach to FST %s",
1681 hapd->iconf->fst_cfg.group_id);
1682 goto fail;
1683 }
1684 }
1685#endif /* CONFIG_FST */
1686
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001687 hostapd_set_state(iface, HAPD_IFACE_ENABLED);
1688 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_ENABLED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001689 if (hapd->setup_complete_cb)
1690 hapd->setup_complete_cb(hapd->setup_complete_cb_ctx);
1691
1692 wpa_printf(MSG_DEBUG, "%s: Setup of interface done.",
1693 iface->bss[0]->conf->iface);
Dmitry Shmidtb96dad42013-11-05 10:07:29 -08001694 if (iface->interfaces && iface->interfaces->terminate_on_error > 0)
1695 iface->interfaces->terminate_on_error--;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001696
1697 return 0;
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001698
1699fail:
1700 wpa_printf(MSG_ERROR, "Interface initialization failed");
1701 hostapd_set_state(iface, HAPD_IFACE_DISABLED);
1702 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001703#ifdef CONFIG_FST
1704 if (iface->fst) {
1705 fst_detach(iface->fst);
1706 iface->fst = NULL;
1707 }
1708#endif /* CONFIG_FST */
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001709 if (iface->interfaces && iface->interfaces->terminate_on_error)
1710 eloop_terminate();
1711 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001712}
1713
1714
1715/**
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001716 * hostapd_setup_interface_complete - Complete interface setup
1717 *
1718 * This function is called when previous steps in the interface setup has been
1719 * completed. This can also start operations, e.g., DFS, that will require
1720 * additional processing before interface is ready to be enabled. Such
1721 * operations will call this function from eloop callbacks when finished.
1722 */
1723int hostapd_setup_interface_complete(struct hostapd_iface *iface, int err)
1724{
1725 struct hapd_interfaces *interfaces = iface->interfaces;
1726 struct hostapd_data *hapd = iface->bss[0];
1727 unsigned int i;
1728 int not_ready_in_sync_ifaces = 0;
1729
1730 if (!iface->need_to_start_in_sync)
1731 return hostapd_setup_interface_complete_sync(iface, err);
1732
1733 if (err) {
1734 wpa_printf(MSG_ERROR, "Interface initialization failed");
1735 hostapd_set_state(iface, HAPD_IFACE_DISABLED);
1736 iface->need_to_start_in_sync = 0;
1737 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
1738 if (interfaces && interfaces->terminate_on_error)
1739 eloop_terminate();
1740 return -1;
1741 }
1742
1743 if (iface->ready_to_start_in_sync) {
1744 /* Already in ready and waiting. should never happpen */
1745 return 0;
1746 }
1747
1748 for (i = 0; i < interfaces->count; i++) {
1749 if (interfaces->iface[i]->need_to_start_in_sync &&
1750 !interfaces->iface[i]->ready_to_start_in_sync)
1751 not_ready_in_sync_ifaces++;
1752 }
1753
1754 /*
1755 * Check if this is the last interface, if yes then start all the other
1756 * waiting interfaces. If not, add this interface to the waiting list.
1757 */
1758 if (not_ready_in_sync_ifaces > 1 && iface->state == HAPD_IFACE_DFS) {
1759 /*
1760 * If this interface went through CAC, do not synchronize, just
1761 * start immediately.
1762 */
1763 iface->need_to_start_in_sync = 0;
1764 wpa_printf(MSG_INFO,
1765 "%s: Finished CAC - bypass sync and start interface",
1766 iface->bss[0]->conf->iface);
1767 return hostapd_setup_interface_complete_sync(iface, err);
1768 }
1769
1770 if (not_ready_in_sync_ifaces > 1) {
1771 /* need to wait as there are other interfaces still coming up */
1772 iface->ready_to_start_in_sync = 1;
1773 wpa_printf(MSG_INFO,
1774 "%s: Interface waiting to sync with other interfaces",
1775 iface->bss[0]->conf->iface);
1776 return 0;
1777 }
1778
1779 wpa_printf(MSG_INFO,
1780 "%s: Last interface to sync - starting all interfaces",
1781 iface->bss[0]->conf->iface);
1782 iface->need_to_start_in_sync = 0;
1783 hostapd_setup_interface_complete_sync(iface, err);
1784 for (i = 0; i < interfaces->count; i++) {
1785 if (interfaces->iface[i]->need_to_start_in_sync &&
1786 interfaces->iface[i]->ready_to_start_in_sync) {
1787 hostapd_setup_interface_complete_sync(
1788 interfaces->iface[i], 0);
1789 /* Only once the interfaces are sync started */
1790 interfaces->iface[i]->need_to_start_in_sync = 0;
1791 }
1792 }
1793
1794 return 0;
1795}
1796
1797
1798/**
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001799 * hostapd_setup_interface - Setup of an interface
1800 * @iface: Pointer to interface data.
1801 * Returns: 0 on success, -1 on failure
1802 *
1803 * Initializes the driver interface, validates the configuration,
1804 * and sets driver parameters based on the configuration.
1805 * Flushes old stations, sets the channel, encryption,
1806 * beacons, and WDS links based on the configuration.
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001807 *
1808 * If interface setup requires more time, e.g., to perform HT co-ex scans, ACS,
1809 * or DFS operations, this function returns 0 before such operations have been
1810 * completed. The pending operations are registered into eloop and will be
1811 * completed from eloop callbacks. Those callbacks end up calling
1812 * hostapd_setup_interface_complete() once setup has been completed.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001813 */
1814int hostapd_setup_interface(struct hostapd_iface *iface)
1815{
1816 int ret;
1817
1818 ret = setup_interface(iface);
1819 if (ret) {
1820 wpa_printf(MSG_ERROR, "%s: Unable to setup interface.",
1821 iface->bss[0]->conf->iface);
1822 return -1;
1823 }
1824
1825 return 0;
1826}
1827
1828
1829/**
1830 * hostapd_alloc_bss_data - Allocate and initialize per-BSS data
1831 * @hapd_iface: Pointer to interface data
1832 * @conf: Pointer to per-interface configuration
1833 * @bss: Pointer to per-BSS configuration for this BSS
1834 * Returns: Pointer to allocated BSS data
1835 *
1836 * This function is used to allocate per-BSS data structure. This data will be
1837 * freed after hostapd_cleanup() is called for it during interface
1838 * deinitialization.
1839 */
1840struct hostapd_data *
1841hostapd_alloc_bss_data(struct hostapd_iface *hapd_iface,
1842 struct hostapd_config *conf,
1843 struct hostapd_bss_config *bss)
1844{
1845 struct hostapd_data *hapd;
1846
1847 hapd = os_zalloc(sizeof(*hapd));
1848 if (hapd == NULL)
1849 return NULL;
1850
1851 hapd->new_assoc_sta_cb = hostapd_new_assoc_sta;
1852 hapd->iconf = conf;
1853 hapd->conf = bss;
1854 hapd->iface = hapd_iface;
1855 hapd->driver = hapd->iconf->driver;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001856 hapd->ctrl_sock = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001857
1858 return hapd;
1859}
1860
1861
Dmitry Shmidt54605472013-11-08 11:10:19 -08001862static void hostapd_bss_deinit(struct hostapd_data *hapd)
1863{
1864 wpa_printf(MSG_DEBUG, "%s: deinit bss %s", __func__,
1865 hapd->conf->iface);
Dmitry Shmidt71757432014-06-02 13:50:35 -07001866 hostapd_bss_deinit_no_free(hapd);
Dmitry Shmidtf73259c2015-03-17 11:00:54 -07001867 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
Dmitry Shmidt54605472013-11-08 11:10:19 -08001868 hostapd_cleanup(hapd);
1869}
1870
1871
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001872void hostapd_interface_deinit(struct hostapd_iface *iface)
1873{
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001874 int j;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001875
Dmitry Shmidt54605472013-11-08 11:10:19 -08001876 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001877 if (iface == NULL)
1878 return;
1879
Dmitry Shmidtf73259c2015-03-17 11:00:54 -07001880 hostapd_set_state(iface, HAPD_IFACE_DISABLED);
1881
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07001882#ifdef CONFIG_IEEE80211N
1883#ifdef NEED_AP_MLME
1884 hostapd_stop_setup_timers(iface);
1885 eloop_cancel_timeout(ap_ht2040_timeout, iface, NULL);
1886#endif /* NEED_AP_MLME */
1887#endif /* CONFIG_IEEE80211N */
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001888 eloop_cancel_timeout(channel_list_update_timeout, iface, NULL);
1889 iface->wait_channel_update = 0;
1890
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001891#ifdef CONFIG_FST
1892 if (iface->fst) {
1893 fst_detach(iface->fst);
1894 iface->fst = NULL;
1895 }
1896#endif /* CONFIG_FST */
1897
Dmitry Shmidt54605472013-11-08 11:10:19 -08001898 for (j = iface->num_bss - 1; j >= 0; j--)
1899 hostapd_bss_deinit(iface->bss[j]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001900}
1901
1902
1903void hostapd_interface_free(struct hostapd_iface *iface)
1904{
1905 size_t j;
Dmitry Shmidt54605472013-11-08 11:10:19 -08001906 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
1907 for (j = 0; j < iface->num_bss; j++) {
1908 wpa_printf(MSG_DEBUG, "%s: free hapd %p",
1909 __func__, iface->bss[j]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001910 os_free(iface->bss[j]);
Dmitry Shmidt54605472013-11-08 11:10:19 -08001911 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001912 hostapd_cleanup_iface(iface);
1913}
1914
1915
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001916/**
1917 * hostapd_init - Allocate and initialize per-interface data
1918 * @config_file: Path to the configuration file
1919 * Returns: Pointer to the allocated interface data or %NULL on failure
1920 *
1921 * This function is used to allocate main data structures for per-interface
1922 * data. The allocated data buffer will be freed by calling
1923 * hostapd_cleanup_iface().
1924 */
1925struct hostapd_iface * hostapd_init(struct hapd_interfaces *interfaces,
1926 const char *config_file)
1927{
1928 struct hostapd_iface *hapd_iface = NULL;
1929 struct hostapd_config *conf = NULL;
1930 struct hostapd_data *hapd;
1931 size_t i;
1932
1933 hapd_iface = os_zalloc(sizeof(*hapd_iface));
1934 if (hapd_iface == NULL)
1935 goto fail;
1936
1937 hapd_iface->config_fname = os_strdup(config_file);
1938 if (hapd_iface->config_fname == NULL)
1939 goto fail;
1940
1941 conf = interfaces->config_read_cb(hapd_iface->config_fname);
1942 if (conf == NULL)
1943 goto fail;
1944 hapd_iface->conf = conf;
1945
1946 hapd_iface->num_bss = conf->num_bss;
1947 hapd_iface->bss = os_calloc(conf->num_bss,
1948 sizeof(struct hostapd_data *));
1949 if (hapd_iface->bss == NULL)
1950 goto fail;
1951
1952 for (i = 0; i < conf->num_bss; i++) {
1953 hapd = hapd_iface->bss[i] =
1954 hostapd_alloc_bss_data(hapd_iface, conf,
1955 conf->bss[i]);
1956 if (hapd == NULL)
1957 goto fail;
1958 hapd->msg_ctx = hapd;
1959 }
1960
1961 return hapd_iface;
1962
1963fail:
1964 wpa_printf(MSG_ERROR, "Failed to set up interface with %s",
1965 config_file);
1966 if (conf)
1967 hostapd_config_free(conf);
1968 if (hapd_iface) {
1969 os_free(hapd_iface->config_fname);
1970 os_free(hapd_iface->bss);
Dmitry Shmidt54605472013-11-08 11:10:19 -08001971 wpa_printf(MSG_DEBUG, "%s: free iface %p",
1972 __func__, hapd_iface);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001973 os_free(hapd_iface);
1974 }
1975 return NULL;
1976}
1977
1978
1979static int ifname_in_use(struct hapd_interfaces *interfaces, const char *ifname)
1980{
1981 size_t i, j;
1982
1983 for (i = 0; i < interfaces->count; i++) {
1984 struct hostapd_iface *iface = interfaces->iface[i];
1985 for (j = 0; j < iface->num_bss; j++) {
1986 struct hostapd_data *hapd = iface->bss[j];
1987 if (os_strcmp(ifname, hapd->conf->iface) == 0)
1988 return 1;
1989 }
1990 }
1991
1992 return 0;
1993}
1994
1995
1996/**
1997 * hostapd_interface_init_bss - Read configuration file and init BSS data
1998 *
1999 * This function is used to parse configuration file for a BSS. This BSS is
2000 * added to an existing interface sharing the same radio (if any) or a new
2001 * interface is created if this is the first interface on a radio. This
2002 * allocate memory for the BSS. No actual driver operations are started.
2003 *
2004 * This is similar to hostapd_interface_init(), but for a case where the
2005 * configuration is used to add a single BSS instead of all BSSes for a radio.
2006 */
2007struct hostapd_iface *
2008hostapd_interface_init_bss(struct hapd_interfaces *interfaces, const char *phy,
2009 const char *config_fname, int debug)
2010{
2011 struct hostapd_iface *new_iface = NULL, *iface = NULL;
2012 struct hostapd_data *hapd;
2013 int k;
2014 size_t i, bss_idx;
2015
2016 if (!phy || !*phy)
2017 return NULL;
2018
2019 for (i = 0; i < interfaces->count; i++) {
2020 if (os_strcmp(interfaces->iface[i]->phy, phy) == 0) {
2021 iface = interfaces->iface[i];
2022 break;
2023 }
2024 }
2025
2026 wpa_printf(MSG_INFO, "Configuration file: %s (phy %s)%s",
2027 config_fname, phy, iface ? "" : " --> new PHY");
2028 if (iface) {
2029 struct hostapd_config *conf;
2030 struct hostapd_bss_config **tmp_conf;
2031 struct hostapd_data **tmp_bss;
2032 struct hostapd_bss_config *bss;
2033 const char *ifname;
2034
2035 /* Add new BSS to existing iface */
2036 conf = interfaces->config_read_cb(config_fname);
2037 if (conf == NULL)
2038 return NULL;
2039 if (conf->num_bss > 1) {
2040 wpa_printf(MSG_ERROR, "Multiple BSSes specified in BSS-config");
2041 hostapd_config_free(conf);
2042 return NULL;
2043 }
2044
2045 ifname = conf->bss[0]->iface;
2046 if (ifname[0] != '\0' && ifname_in_use(interfaces, ifname)) {
2047 wpa_printf(MSG_ERROR,
2048 "Interface name %s already in use", ifname);
2049 hostapd_config_free(conf);
2050 return NULL;
2051 }
2052
2053 tmp_conf = os_realloc_array(
2054 iface->conf->bss, iface->conf->num_bss + 1,
2055 sizeof(struct hostapd_bss_config *));
2056 tmp_bss = os_realloc_array(iface->bss, iface->num_bss + 1,
2057 sizeof(struct hostapd_data *));
2058 if (tmp_bss)
2059 iface->bss = tmp_bss;
2060 if (tmp_conf) {
2061 iface->conf->bss = tmp_conf;
2062 iface->conf->last_bss = tmp_conf[0];
2063 }
2064 if (tmp_bss == NULL || tmp_conf == NULL) {
2065 hostapd_config_free(conf);
2066 return NULL;
2067 }
2068 bss = iface->conf->bss[iface->conf->num_bss] = conf->bss[0];
2069 iface->conf->num_bss++;
2070
2071 hapd = hostapd_alloc_bss_data(iface, iface->conf, bss);
2072 if (hapd == NULL) {
2073 iface->conf->num_bss--;
2074 hostapd_config_free(conf);
2075 return NULL;
2076 }
2077 iface->conf->last_bss = bss;
2078 iface->bss[iface->num_bss] = hapd;
2079 hapd->msg_ctx = hapd;
2080
2081 bss_idx = iface->num_bss++;
2082 conf->num_bss--;
2083 conf->bss[0] = NULL;
2084 hostapd_config_free(conf);
2085 } else {
2086 /* Add a new iface with the first BSS */
2087 new_iface = iface = hostapd_init(interfaces, config_fname);
2088 if (!iface)
2089 return NULL;
2090 os_strlcpy(iface->phy, phy, sizeof(iface->phy));
2091 iface->interfaces = interfaces;
2092 bss_idx = 0;
2093 }
2094
2095 for (k = 0; k < debug; k++) {
2096 if (iface->bss[bss_idx]->conf->logger_stdout_level > 0)
2097 iface->bss[bss_idx]->conf->logger_stdout_level--;
2098 }
2099
2100 if (iface->conf->bss[bss_idx]->iface[0] == '\0' &&
2101 !hostapd_drv_none(iface->bss[bss_idx])) {
2102 wpa_printf(MSG_ERROR, "Interface name not specified in %s",
2103 config_fname);
2104 if (new_iface)
2105 hostapd_interface_deinit_free(new_iface);
2106 return NULL;
2107 }
2108
2109 return iface;
2110}
2111
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002112
2113void hostapd_interface_deinit_free(struct hostapd_iface *iface)
2114{
2115 const struct wpa_driver_ops *driver;
2116 void *drv_priv;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002117
2118 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002119 if (iface == NULL)
2120 return;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002121 wpa_printf(MSG_DEBUG, "%s: num_bss=%u conf->num_bss=%u",
2122 __func__, (unsigned int) iface->num_bss,
2123 (unsigned int) iface->conf->num_bss);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002124 driver = iface->bss[0]->driver;
2125 drv_priv = iface->bss[0]->drv_priv;
2126 hostapd_interface_deinit(iface);
Dmitry Shmidt54605472013-11-08 11:10:19 -08002127 wpa_printf(MSG_DEBUG, "%s: driver=%p drv_priv=%p -> hapd_deinit",
2128 __func__, driver, drv_priv);
Dmitry Shmidt71757432014-06-02 13:50:35 -07002129 if (driver && driver->hapd_deinit && drv_priv) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002130 driver->hapd_deinit(drv_priv);
Dmitry Shmidt71757432014-06-02 13:50:35 -07002131 iface->bss[0]->drv_priv = NULL;
2132 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002133 hostapd_interface_free(iface);
2134}
2135
2136
Dmitry Shmidt15907092014-03-25 10:42:57 -07002137static void hostapd_deinit_driver(const struct wpa_driver_ops *driver,
2138 void *drv_priv,
2139 struct hostapd_iface *hapd_iface)
2140{
2141 size_t j;
2142
2143 wpa_printf(MSG_DEBUG, "%s: driver=%p drv_priv=%p -> hapd_deinit",
2144 __func__, driver, drv_priv);
2145 if (driver && driver->hapd_deinit && drv_priv) {
2146 driver->hapd_deinit(drv_priv);
2147 for (j = 0; j < hapd_iface->num_bss; j++) {
2148 wpa_printf(MSG_DEBUG, "%s:bss[%d]->drv_priv=%p",
2149 __func__, (int) j,
2150 hapd_iface->bss[j]->drv_priv);
2151 if (hapd_iface->bss[j]->drv_priv == drv_priv)
2152 hapd_iface->bss[j]->drv_priv = NULL;
2153 }
2154 }
2155}
2156
2157
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002158int hostapd_enable_iface(struct hostapd_iface *hapd_iface)
2159{
Dmitry Shmidt71757432014-06-02 13:50:35 -07002160 size_t j;
2161
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002162 if (hapd_iface->bss[0]->drv_priv != NULL) {
2163 wpa_printf(MSG_ERROR, "Interface %s already enabled",
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002164 hapd_iface->conf->bss[0]->iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002165 return -1;
2166 }
2167
2168 wpa_printf(MSG_DEBUG, "Enable interface %s",
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002169 hapd_iface->conf->bss[0]->iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002170
Dmitry Shmidt71757432014-06-02 13:50:35 -07002171 for (j = 0; j < hapd_iface->num_bss; j++)
2172 hostapd_set_security_params(hapd_iface->conf->bss[j], 1);
Dmitry Shmidt344abd32014-01-14 13:17:00 -08002173 if (hostapd_config_check(hapd_iface->conf, 1) < 0) {
2174 wpa_printf(MSG_INFO, "Invalid configuration - cannot enable");
2175 return -1;
2176 }
2177
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002178 if (hapd_iface->interfaces == NULL ||
2179 hapd_iface->interfaces->driver_init == NULL ||
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002180 hapd_iface->interfaces->driver_init(hapd_iface))
2181 return -1;
2182
2183 if (hostapd_setup_interface(hapd_iface)) {
Dmitry Shmidt15907092014-03-25 10:42:57 -07002184 hostapd_deinit_driver(hapd_iface->bss[0]->driver,
2185 hapd_iface->bss[0]->drv_priv,
2186 hapd_iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002187 return -1;
2188 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002189
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002190 return 0;
2191}
2192
2193
2194int hostapd_reload_iface(struct hostapd_iface *hapd_iface)
2195{
2196 size_t j;
2197
2198 wpa_printf(MSG_DEBUG, "Reload interface %s",
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002199 hapd_iface->conf->bss[0]->iface);
2200 for (j = 0; j < hapd_iface->num_bss; j++)
Dmitry Shmidt71757432014-06-02 13:50:35 -07002201 hostapd_set_security_params(hapd_iface->conf->bss[j], 1);
Dmitry Shmidt344abd32014-01-14 13:17:00 -08002202 if (hostapd_config_check(hapd_iface->conf, 1) < 0) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002203 wpa_printf(MSG_ERROR, "Updated configuration is invalid");
2204 return -1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002205 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002206 hostapd_clear_old(hapd_iface);
2207 for (j = 0; j < hapd_iface->num_bss; j++)
2208 hostapd_reload_bss(hapd_iface->bss[j]);
2209
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002210 return 0;
2211}
2212
2213
2214int hostapd_disable_iface(struct hostapd_iface *hapd_iface)
2215{
2216 size_t j;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002217 const struct wpa_driver_ops *driver;
2218 void *drv_priv;
2219
2220 if (hapd_iface == NULL)
2221 return -1;
Dmitry Shmidt71757432014-06-02 13:50:35 -07002222
2223 if (hapd_iface->bss[0]->drv_priv == NULL) {
2224 wpa_printf(MSG_INFO, "Interface %s already disabled",
2225 hapd_iface->conf->bss[0]->iface);
2226 return -1;
2227 }
2228
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002229 wpa_msg(hapd_iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002230 driver = hapd_iface->bss[0]->driver;
2231 drv_priv = hapd_iface->bss[0]->drv_priv;
2232
Dmitry Shmidta38abf92014-03-06 13:38:44 -08002233 hapd_iface->driver_ap_teardown =
2234 !!(hapd_iface->drv_flags &
2235 WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
2236
2237 /* same as hostapd_interface_deinit without deinitializing ctrl-iface */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002238 for (j = 0; j < hapd_iface->num_bss; j++) {
2239 struct hostapd_data *hapd = hapd_iface->bss[j];
Dmitry Shmidt71757432014-06-02 13:50:35 -07002240 hostapd_bss_deinit_no_free(hapd);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002241 hostapd_free_hapd_data(hapd);
2242 }
2243
Dmitry Shmidt15907092014-03-25 10:42:57 -07002244 hostapd_deinit_driver(driver, drv_priv, hapd_iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002245
2246 /* From hostapd_cleanup_iface: These were initialized in
2247 * hostapd_setup_interface and hostapd_setup_interface_complete
2248 */
2249 hostapd_cleanup_iface_partial(hapd_iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002250
Dmitry Shmidt56052862013-10-04 10:23:25 -07002251 wpa_printf(MSG_DEBUG, "Interface %s disabled",
2252 hapd_iface->bss[0]->conf->iface);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002253 hostapd_set_state(hapd_iface, HAPD_IFACE_DISABLED);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002254 return 0;
2255}
2256
2257
2258static struct hostapd_iface *
2259hostapd_iface_alloc(struct hapd_interfaces *interfaces)
2260{
2261 struct hostapd_iface **iface, *hapd_iface;
2262
2263 iface = os_realloc_array(interfaces->iface, interfaces->count + 1,
2264 sizeof(struct hostapd_iface *));
2265 if (iface == NULL)
2266 return NULL;
2267 interfaces->iface = iface;
2268 hapd_iface = interfaces->iface[interfaces->count] =
2269 os_zalloc(sizeof(*hapd_iface));
2270 if (hapd_iface == NULL) {
2271 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory for "
2272 "the interface", __func__);
2273 return NULL;
2274 }
2275 interfaces->count++;
2276 hapd_iface->interfaces = interfaces;
2277
2278 return hapd_iface;
2279}
2280
2281
2282static struct hostapd_config *
2283hostapd_config_alloc(struct hapd_interfaces *interfaces, const char *ifname,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002284 const char *ctrl_iface, const char *driver)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002285{
2286 struct hostapd_bss_config *bss;
2287 struct hostapd_config *conf;
2288
2289 /* Allocates memory for bss and conf */
2290 conf = hostapd_config_defaults();
2291 if (conf == NULL) {
2292 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory for "
2293 "configuration", __func__);
2294 return NULL;
2295 }
2296
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002297 if (driver) {
2298 int j;
2299
2300 for (j = 0; wpa_drivers[j]; j++) {
2301 if (os_strcmp(driver, wpa_drivers[j]->name) == 0) {
2302 conf->driver = wpa_drivers[j];
2303 goto skip;
2304 }
2305 }
2306
2307 wpa_printf(MSG_ERROR,
2308 "Invalid/unknown driver '%s' - registering the default driver",
2309 driver);
2310 }
2311
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002312 conf->driver = wpa_drivers[0];
2313 if (conf->driver == NULL) {
2314 wpa_printf(MSG_ERROR, "No driver wrappers registered!");
2315 hostapd_config_free(conf);
2316 return NULL;
2317 }
2318
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002319skip:
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002320 bss = conf->last_bss = conf->bss[0];
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002321
2322 os_strlcpy(bss->iface, ifname, sizeof(bss->iface));
2323 bss->ctrl_interface = os_strdup(ctrl_iface);
2324 if (bss->ctrl_interface == NULL) {
2325 hostapd_config_free(conf);
2326 return NULL;
2327 }
2328
2329 /* Reading configuration file skipped, will be done in SET!
2330 * From reading the configuration till the end has to be done in
2331 * SET
2332 */
2333 return conf;
2334}
2335
2336
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002337static int hostapd_data_alloc(struct hostapd_iface *hapd_iface,
2338 struct hostapd_config *conf)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002339{
2340 size_t i;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002341 struct hostapd_data *hapd;
2342
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002343 hapd_iface->bss = os_calloc(conf->num_bss,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002344 sizeof(struct hostapd_data *));
2345 if (hapd_iface->bss == NULL)
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002346 return -1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002347
2348 for (i = 0; i < conf->num_bss; i++) {
2349 hapd = hapd_iface->bss[i] =
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002350 hostapd_alloc_bss_data(hapd_iface, conf, conf->bss[i]);
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002351 if (hapd == NULL) {
2352 while (i > 0) {
2353 i--;
2354 os_free(hapd_iface->bss[i]);
2355 hapd_iface->bss[i] = NULL;
2356 }
2357 os_free(hapd_iface->bss);
2358 hapd_iface->bss = NULL;
2359 return -1;
2360 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002361 hapd->msg_ctx = hapd;
2362 }
2363
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002364 hapd_iface->conf = conf;
2365 hapd_iface->num_bss = conf->num_bss;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002366
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002367 return 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002368}
2369
2370
2371int hostapd_add_iface(struct hapd_interfaces *interfaces, char *buf)
2372{
2373 struct hostapd_config *conf = NULL;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002374 struct hostapd_iface *hapd_iface = NULL, *new_iface = NULL;
2375 struct hostapd_data *hapd;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002376 char *ptr;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002377 size_t i, j;
2378 const char *conf_file = NULL, *phy_name = NULL;
2379
2380 if (os_strncmp(buf, "bss_config=", 11) == 0) {
2381 char *pos;
2382 phy_name = buf + 11;
2383 pos = os_strchr(phy_name, ':');
2384 if (!pos)
2385 return -1;
2386 *pos++ = '\0';
2387 conf_file = pos;
2388 if (!os_strlen(conf_file))
2389 return -1;
2390
2391 hapd_iface = hostapd_interface_init_bss(interfaces, phy_name,
2392 conf_file, 0);
2393 if (!hapd_iface)
2394 return -1;
2395 for (j = 0; j < interfaces->count; j++) {
2396 if (interfaces->iface[j] == hapd_iface)
2397 break;
2398 }
2399 if (j == interfaces->count) {
2400 struct hostapd_iface **tmp;
2401 tmp = os_realloc_array(interfaces->iface,
2402 interfaces->count + 1,
2403 sizeof(struct hostapd_iface *));
2404 if (!tmp) {
2405 hostapd_interface_deinit_free(hapd_iface);
2406 return -1;
2407 }
2408 interfaces->iface = tmp;
2409 interfaces->iface[interfaces->count++] = hapd_iface;
2410 new_iface = hapd_iface;
2411 }
2412
2413 if (new_iface) {
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002414 if (interfaces->driver_init(hapd_iface))
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002415 goto fail;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002416
2417 if (hostapd_setup_interface(hapd_iface)) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002418 hostapd_deinit_driver(
2419 hapd_iface->bss[0]->driver,
2420 hapd_iface->bss[0]->drv_priv,
2421 hapd_iface);
2422 goto fail;
2423 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002424 } else {
2425 /* Assign new BSS with bss[0]'s driver info */
2426 hapd = hapd_iface->bss[hapd_iface->num_bss - 1];
2427 hapd->driver = hapd_iface->bss[0]->driver;
2428 hapd->drv_priv = hapd_iface->bss[0]->drv_priv;
2429 os_memcpy(hapd->own_addr, hapd_iface->bss[0]->own_addr,
2430 ETH_ALEN);
2431
2432 if (start_ctrl_iface_bss(hapd) < 0 ||
Dmitry Shmidt54605472013-11-08 11:10:19 -08002433 (hapd_iface->state == HAPD_IFACE_ENABLED &&
2434 hostapd_setup_bss(hapd, -1))) {
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002435 hostapd_cleanup(hapd);
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08002436 hapd_iface->bss[hapd_iface->num_bss - 1] = NULL;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002437 hapd_iface->conf->num_bss--;
2438 hapd_iface->num_bss--;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002439 wpa_printf(MSG_DEBUG, "%s: free hapd %p %s",
2440 __func__, hapd, hapd->conf->iface);
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002441 hostapd_config_free_bss(hapd->conf);
2442 hapd->conf = NULL;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002443 os_free(hapd);
2444 return -1;
2445 }
2446 }
2447 return 0;
2448 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002449
2450 ptr = os_strchr(buf, ' ');
2451 if (ptr == NULL)
2452 return -1;
2453 *ptr++ = '\0';
2454
Dmitry Shmidt56052862013-10-04 10:23:25 -07002455 if (os_strncmp(ptr, "config=", 7) == 0)
2456 conf_file = ptr + 7;
2457
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002458 for (i = 0; i < interfaces->count; i++) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002459 if (!os_strcmp(interfaces->iface[i]->conf->bss[0]->iface,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002460 buf)) {
2461 wpa_printf(MSG_INFO, "Cannot add interface - it "
2462 "already exists");
2463 return -1;
2464 }
2465 }
2466
2467 hapd_iface = hostapd_iface_alloc(interfaces);
2468 if (hapd_iface == NULL) {
2469 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
2470 "for interface", __func__);
2471 goto fail;
2472 }
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002473 new_iface = hapd_iface;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002474
Dmitry Shmidt56052862013-10-04 10:23:25 -07002475 if (conf_file && interfaces->config_read_cb) {
2476 conf = interfaces->config_read_cb(conf_file);
2477 if (conf && conf->bss)
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002478 os_strlcpy(conf->bss[0]->iface, buf,
2479 sizeof(conf->bss[0]->iface));
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002480 } else {
2481 char *driver = os_strchr(ptr, ' ');
2482
2483 if (driver)
2484 *driver++ = '\0';
2485 conf = hostapd_config_alloc(interfaces, buf, ptr, driver);
2486 }
2487
Dmitry Shmidt56052862013-10-04 10:23:25 -07002488 if (conf == NULL || conf->bss == NULL) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002489 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
2490 "for configuration", __func__);
2491 goto fail;
2492 }
2493
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002494 if (hostapd_data_alloc(hapd_iface, conf) < 0) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002495 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
2496 "for hostapd", __func__);
2497 goto fail;
2498 }
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002499 conf = NULL;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002500
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002501 if (start_ctrl_iface(hapd_iface) < 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002502 goto fail;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002503
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002504 wpa_printf(MSG_INFO, "Add interface '%s'",
2505 hapd_iface->conf->bss[0]->iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002506
2507 return 0;
2508
2509fail:
2510 if (conf)
2511 hostapd_config_free(conf);
2512 if (hapd_iface) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002513 if (hapd_iface->bss) {
Dmitry Shmidt54605472013-11-08 11:10:19 -08002514 for (i = 0; i < hapd_iface->num_bss; i++) {
2515 hapd = hapd_iface->bss[i];
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08002516 if (!hapd)
2517 continue;
2518 if (hapd_iface->interfaces &&
Dmitry Shmidt54605472013-11-08 11:10:19 -08002519 hapd_iface->interfaces->ctrl_iface_deinit)
2520 hapd_iface->interfaces->
2521 ctrl_iface_deinit(hapd);
2522 wpa_printf(MSG_DEBUG, "%s: free hapd %p (%s)",
2523 __func__, hapd_iface->bss[i],
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08002524 hapd->conf->iface);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002525 hostapd_cleanup(hapd);
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08002526 os_free(hapd);
2527 hapd_iface->bss[i] = NULL;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002528 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002529 os_free(hapd_iface->bss);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002530 hapd_iface->bss = NULL;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002531 }
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002532 if (new_iface) {
2533 interfaces->count--;
2534 interfaces->iface[interfaces->count] = NULL;
2535 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002536 hostapd_cleanup_iface(hapd_iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002537 }
2538 return -1;
2539}
2540
2541
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002542static int hostapd_remove_bss(struct hostapd_iface *iface, unsigned int idx)
2543{
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002544 size_t i;
2545
Dmitry Shmidt54605472013-11-08 11:10:19 -08002546 wpa_printf(MSG_INFO, "Remove BSS '%s'", iface->conf->bss[idx]->iface);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002547
Dmitry Shmidt54605472013-11-08 11:10:19 -08002548 /* Remove hostapd_data only if it has already been initialized */
2549 if (idx < iface->num_bss) {
2550 struct hostapd_data *hapd = iface->bss[idx];
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002551
Dmitry Shmidt54605472013-11-08 11:10:19 -08002552 hostapd_bss_deinit(hapd);
2553 wpa_printf(MSG_DEBUG, "%s: free hapd %p (%s)",
2554 __func__, hapd, hapd->conf->iface);
2555 hostapd_config_free_bss(hapd->conf);
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002556 hapd->conf = NULL;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002557 os_free(hapd);
2558
2559 iface->num_bss--;
2560
2561 for (i = idx; i < iface->num_bss; i++)
2562 iface->bss[i] = iface->bss[i + 1];
2563 } else {
2564 hostapd_config_free_bss(iface->conf->bss[idx]);
2565 iface->conf->bss[idx] = NULL;
2566 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002567
2568 iface->conf->num_bss--;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002569 for (i = idx; i < iface->conf->num_bss; i++)
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002570 iface->conf->bss[i] = iface->conf->bss[i + 1];
2571
2572 return 0;
2573}
2574
2575
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002576int hostapd_remove_iface(struct hapd_interfaces *interfaces, char *buf)
2577{
2578 struct hostapd_iface *hapd_iface;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002579 size_t i, j, k = 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002580
2581 for (i = 0; i < interfaces->count; i++) {
2582 hapd_iface = interfaces->iface[i];
2583 if (hapd_iface == NULL)
2584 return -1;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002585 if (!os_strcmp(hapd_iface->conf->bss[0]->iface, buf)) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002586 wpa_printf(MSG_INFO, "Remove interface '%s'", buf);
Dmitry Shmidta38abf92014-03-06 13:38:44 -08002587 hapd_iface->driver_ap_teardown =
2588 !!(hapd_iface->drv_flags &
2589 WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
2590
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002591 hostapd_interface_deinit_free(hapd_iface);
2592 k = i;
2593 while (k < (interfaces->count - 1)) {
2594 interfaces->iface[k] =
2595 interfaces->iface[k + 1];
2596 k++;
2597 }
2598 interfaces->count--;
2599 return 0;
2600 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002601
2602 for (j = 0; j < hapd_iface->conf->num_bss; j++) {
Dmitry Shmidta38abf92014-03-06 13:38:44 -08002603 if (!os_strcmp(hapd_iface->conf->bss[j]->iface, buf)) {
2604 hapd_iface->driver_ap_teardown =
2605 !(hapd_iface->drv_flags &
2606 WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002607 return hostapd_remove_bss(hapd_iface, j);
Dmitry Shmidta38abf92014-03-06 13:38:44 -08002608 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002609 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002610 }
2611 return -1;
2612}
2613
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002614
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002615/**
2616 * hostapd_new_assoc_sta - Notify that a new station associated with the AP
2617 * @hapd: Pointer to BSS data
2618 * @sta: Pointer to the associated STA data
2619 * @reassoc: 1 to indicate this was a re-association; 0 = first association
2620 *
2621 * This function will be called whenever a station associates with the AP. It
2622 * can be called from ieee802_11.c for drivers that export MLME to hostapd and
2623 * from drv_callbacks.c based on driver events for drivers that take care of
2624 * management frames (IEEE 802.11 authentication and association) internally.
2625 */
2626void hostapd_new_assoc_sta(struct hostapd_data *hapd, struct sta_info *sta,
2627 int reassoc)
2628{
2629 if (hapd->tkip_countermeasures) {
2630 hostapd_drv_sta_deauth(hapd, sta->addr,
2631 WLAN_REASON_MICHAEL_MIC_FAILURE);
2632 return;
2633 }
2634
2635 hostapd_prune_associations(hapd, sta->addr);
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -08002636 ap_sta_clear_disconnect_timeouts(hapd, sta);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002637
2638 /* IEEE 802.11F (IAPP) */
2639 if (hapd->conf->ieee802_11f)
2640 iapp_new_station(hapd->iapp, sta);
2641
2642#ifdef CONFIG_P2P
2643 if (sta->p2p_ie == NULL && !sta->no_p2p_set) {
2644 sta->no_p2p_set = 1;
2645 hapd->num_sta_no_p2p++;
2646 if (hapd->num_sta_no_p2p == 1)
2647 hostapd_p2p_non_p2p_sta_connected(hapd);
2648 }
2649#endif /* CONFIG_P2P */
2650
2651 /* Start accounting here, if IEEE 802.1X and WPA are not used.
2652 * IEEE 802.1X/WPA code will start accounting after the station has
2653 * been authorized. */
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -08002654 if (!hapd->conf->ieee802_1x && !hapd->conf->wpa && !hapd->conf->osen) {
2655 ap_sta_set_authorized(hapd, sta, 1);
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002656 os_get_reltime(&sta->connected_time);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002657 accounting_sta_start(hapd, sta);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002658 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002659
2660 /* Start IEEE 802.1X authentication process for new stations */
2661 ieee802_1x_new_station(hapd, sta);
2662 if (reassoc) {
2663 if (sta->auth_alg != WLAN_AUTH_FT &&
2664 !(sta->flags & (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS)))
2665 wpa_auth_sm_event(sta->wpa_sm, WPA_REAUTH);
2666 } else
2667 wpa_auth_sta_associated(hapd->wpa_auth, sta->wpa_sm);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002668
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08002669 if (!(hapd->iface->drv_flags & WPA_DRIVER_FLAGS_INACTIVITY_TIMER)) {
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -08002670 wpa_printf(MSG_DEBUG,
2671 "%s: %s: reschedule ap_handle_timer timeout for "
2672 MACSTR " (%d seconds - ap_max_inactivity)",
2673 hapd->conf->iface, __func__, MAC2STR(sta->addr),
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08002674 hapd->conf->ap_max_inactivity);
2675 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
2676 eloop_register_timeout(hapd->conf->ap_max_inactivity, 0,
2677 ap_handle_timer, hapd, sta);
2678 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002679}
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002680
2681
2682const char * hostapd_state_text(enum hostapd_iface_state s)
2683{
2684 switch (s) {
2685 case HAPD_IFACE_UNINITIALIZED:
2686 return "UNINITIALIZED";
2687 case HAPD_IFACE_DISABLED:
2688 return "DISABLED";
2689 case HAPD_IFACE_COUNTRY_UPDATE:
2690 return "COUNTRY_UPDATE";
2691 case HAPD_IFACE_ACS:
2692 return "ACS";
2693 case HAPD_IFACE_HT_SCAN:
2694 return "HT_SCAN";
2695 case HAPD_IFACE_DFS:
2696 return "DFS";
2697 case HAPD_IFACE_ENABLED:
2698 return "ENABLED";
2699 }
2700
2701 return "UNKNOWN";
2702}
2703
2704
2705void hostapd_set_state(struct hostapd_iface *iface, enum hostapd_iface_state s)
2706{
2707 wpa_printf(MSG_INFO, "%s: interface state %s->%s",
2708 iface->conf->bss[0]->iface, hostapd_state_text(iface->state),
2709 hostapd_state_text(s));
2710 iface->state = s;
2711}
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002712
2713
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002714int hostapd_csa_in_progress(struct hostapd_iface *iface)
2715{
2716 unsigned int i;
2717
2718 for (i = 0; i < iface->num_bss; i++)
2719 if (iface->bss[i]->csa_in_progress)
2720 return 1;
2721 return 0;
2722}
2723
2724
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002725#ifdef NEED_AP_MLME
2726
2727static void free_beacon_data(struct beacon_data *beacon)
2728{
2729 os_free(beacon->head);
2730 beacon->head = NULL;
2731 os_free(beacon->tail);
2732 beacon->tail = NULL;
2733 os_free(beacon->probe_resp);
2734 beacon->probe_resp = NULL;
2735 os_free(beacon->beacon_ies);
2736 beacon->beacon_ies = NULL;
2737 os_free(beacon->proberesp_ies);
2738 beacon->proberesp_ies = NULL;
2739 os_free(beacon->assocresp_ies);
2740 beacon->assocresp_ies = NULL;
2741}
2742
2743
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07002744static int hostapd_build_beacon_data(struct hostapd_data *hapd,
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002745 struct beacon_data *beacon)
2746{
2747 struct wpabuf *beacon_extra, *proberesp_extra, *assocresp_extra;
2748 struct wpa_driver_ap_params params;
2749 int ret;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002750
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08002751 os_memset(beacon, 0, sizeof(*beacon));
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002752 ret = ieee802_11_build_ap_params(hapd, &params);
2753 if (ret < 0)
2754 return ret;
2755
2756 ret = hostapd_build_ap_extra_ies(hapd, &beacon_extra,
2757 &proberesp_extra,
2758 &assocresp_extra);
2759 if (ret)
2760 goto free_ap_params;
2761
2762 ret = -1;
2763 beacon->head = os_malloc(params.head_len);
2764 if (!beacon->head)
2765 goto free_ap_extra_ies;
2766
2767 os_memcpy(beacon->head, params.head, params.head_len);
2768 beacon->head_len = params.head_len;
2769
2770 beacon->tail = os_malloc(params.tail_len);
2771 if (!beacon->tail)
2772 goto free_beacon;
2773
2774 os_memcpy(beacon->tail, params.tail, params.tail_len);
2775 beacon->tail_len = params.tail_len;
2776
2777 if (params.proberesp != NULL) {
2778 beacon->probe_resp = os_malloc(params.proberesp_len);
2779 if (!beacon->probe_resp)
2780 goto free_beacon;
2781
2782 os_memcpy(beacon->probe_resp, params.proberesp,
2783 params.proberesp_len);
2784 beacon->probe_resp_len = params.proberesp_len;
2785 }
2786
2787 /* copy the extra ies */
2788 if (beacon_extra) {
2789 beacon->beacon_ies = os_malloc(wpabuf_len(beacon_extra));
2790 if (!beacon->beacon_ies)
2791 goto free_beacon;
2792
2793 os_memcpy(beacon->beacon_ies,
2794 beacon_extra->buf, wpabuf_len(beacon_extra));
2795 beacon->beacon_ies_len = wpabuf_len(beacon_extra);
2796 }
2797
2798 if (proberesp_extra) {
2799 beacon->proberesp_ies =
2800 os_malloc(wpabuf_len(proberesp_extra));
2801 if (!beacon->proberesp_ies)
2802 goto free_beacon;
2803
2804 os_memcpy(beacon->proberesp_ies, proberesp_extra->buf,
2805 wpabuf_len(proberesp_extra));
2806 beacon->proberesp_ies_len = wpabuf_len(proberesp_extra);
2807 }
2808
2809 if (assocresp_extra) {
2810 beacon->assocresp_ies =
2811 os_malloc(wpabuf_len(assocresp_extra));
2812 if (!beacon->assocresp_ies)
2813 goto free_beacon;
2814
2815 os_memcpy(beacon->assocresp_ies, assocresp_extra->buf,
2816 wpabuf_len(assocresp_extra));
2817 beacon->assocresp_ies_len = wpabuf_len(assocresp_extra);
2818 }
2819
2820 ret = 0;
2821free_beacon:
2822 /* if the function fails, the caller should not free beacon data */
2823 if (ret)
2824 free_beacon_data(beacon);
2825
2826free_ap_extra_ies:
2827 hostapd_free_ap_extra_ies(hapd, beacon_extra, proberesp_extra,
2828 assocresp_extra);
2829free_ap_params:
2830 ieee802_11_free_ap_params(&params);
2831 return ret;
2832}
2833
2834
2835/*
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002836 * TODO: This flow currently supports only changing channel and width within
2837 * the same hw_mode. Any other changes to MAC parameters or provided settings
2838 * are not supported.
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002839 */
2840static int hostapd_change_config_freq(struct hostapd_data *hapd,
2841 struct hostapd_config *conf,
2842 struct hostapd_freq_params *params,
2843 struct hostapd_freq_params *old_params)
2844{
2845 int channel;
2846
2847 if (!params->channel) {
2848 /* check if the new channel is supported by hw */
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07002849 params->channel = hostapd_hw_get_channel(hapd, params->freq);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002850 }
2851
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07002852 channel = params->channel;
2853 if (!channel)
2854 return -1;
2855
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002856 /* if a pointer to old_params is provided we save previous state */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002857 if (old_params &&
2858 hostapd_set_freq_params(old_params, conf->hw_mode,
2859 hostapd_hw_get_freq(hapd, conf->channel),
2860 conf->channel, conf->ieee80211n,
2861 conf->ieee80211ac,
2862 conf->secondary_channel,
2863 conf->vht_oper_chwidth,
2864 conf->vht_oper_centr_freq_seg0_idx,
2865 conf->vht_oper_centr_freq_seg1_idx,
2866 conf->vht_capab))
2867 return -1;
2868
2869 switch (params->bandwidth) {
2870 case 0:
2871 case 20:
2872 case 40:
2873 conf->vht_oper_chwidth = VHT_CHANWIDTH_USE_HT;
2874 break;
2875 case 80:
2876 if (params->center_freq2)
2877 conf->vht_oper_chwidth = VHT_CHANWIDTH_80P80MHZ;
2878 else
2879 conf->vht_oper_chwidth = VHT_CHANWIDTH_80MHZ;
2880 break;
2881 case 160:
2882 conf->vht_oper_chwidth = VHT_CHANWIDTH_160MHZ;
2883 break;
2884 default:
2885 return -1;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002886 }
2887
2888 conf->channel = channel;
2889 conf->ieee80211n = params->ht_enabled;
2890 conf->secondary_channel = params->sec_channel_offset;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002891 ieee80211_freq_to_chan(params->center_freq1,
2892 &conf->vht_oper_centr_freq_seg0_idx);
2893 ieee80211_freq_to_chan(params->center_freq2,
2894 &conf->vht_oper_centr_freq_seg1_idx);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002895
2896 /* TODO: maybe call here hostapd_config_check here? */
2897
2898 return 0;
2899}
2900
2901
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07002902static int hostapd_fill_csa_settings(struct hostapd_data *hapd,
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002903 struct csa_settings *settings)
2904{
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07002905 struct hostapd_iface *iface = hapd->iface;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002906 struct hostapd_freq_params old_freq;
2907 int ret;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002908 u8 chan, vht_bandwidth;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002909
2910 os_memset(&old_freq, 0, sizeof(old_freq));
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07002911 if (!iface || !iface->freq || hapd->csa_in_progress)
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002912 return -1;
2913
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002914 switch (settings->freq_params.bandwidth) {
2915 case 80:
2916 if (settings->freq_params.center_freq2)
2917 vht_bandwidth = VHT_CHANWIDTH_80P80MHZ;
2918 else
2919 vht_bandwidth = VHT_CHANWIDTH_80MHZ;
2920 break;
2921 case 160:
2922 vht_bandwidth = VHT_CHANWIDTH_160MHZ;
2923 break;
2924 default:
2925 vht_bandwidth = VHT_CHANWIDTH_USE_HT;
2926 break;
2927 }
2928
2929 if (ieee80211_freq_to_channel_ext(
2930 settings->freq_params.freq,
2931 settings->freq_params.sec_channel_offset,
2932 vht_bandwidth,
2933 &hapd->iface->cs_oper_class,
2934 &chan) == NUM_HOSTAPD_MODES) {
2935 wpa_printf(MSG_DEBUG,
2936 "invalid frequency for channel switch (freq=%d, sec_channel_offset=%d, vht_enabled=%d)",
2937 settings->freq_params.freq,
2938 settings->freq_params.sec_channel_offset,
2939 settings->freq_params.vht_enabled);
2940 return -1;
2941 }
2942
2943 settings->freq_params.channel = chan;
2944
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002945 ret = hostapd_change_config_freq(iface->bss[0], iface->conf,
2946 &settings->freq_params,
2947 &old_freq);
2948 if (ret)
2949 return ret;
2950
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07002951 ret = hostapd_build_beacon_data(hapd, &settings->beacon_after);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002952
2953 /* change back the configuration */
2954 hostapd_change_config_freq(iface->bss[0], iface->conf,
2955 &old_freq, NULL);
2956
2957 if (ret)
2958 return ret;
2959
2960 /* set channel switch parameters for csa ie */
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07002961 hapd->cs_freq_params = settings->freq_params;
2962 hapd->cs_count = settings->cs_count;
2963 hapd->cs_block_tx = settings->block_tx;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002964
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07002965 ret = hostapd_build_beacon_data(hapd, &settings->beacon_csa);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002966 if (ret) {
2967 free_beacon_data(&settings->beacon_after);
2968 return ret;
2969 }
2970
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002971 settings->counter_offset_beacon[0] = hapd->cs_c_off_beacon;
2972 settings->counter_offset_presp[0] = hapd->cs_c_off_proberesp;
2973 settings->counter_offset_beacon[1] = hapd->cs_c_off_ecsa_beacon;
2974 settings->counter_offset_presp[1] = hapd->cs_c_off_ecsa_proberesp;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002975
2976 return 0;
2977}
2978
2979
2980void hostapd_cleanup_cs_params(struct hostapd_data *hapd)
2981{
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07002982 os_memset(&hapd->cs_freq_params, 0, sizeof(hapd->cs_freq_params));
2983 hapd->cs_count = 0;
2984 hapd->cs_block_tx = 0;
2985 hapd->cs_c_off_beacon = 0;
2986 hapd->cs_c_off_proberesp = 0;
2987 hapd->csa_in_progress = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002988 hapd->cs_c_off_ecsa_beacon = 0;
2989 hapd->cs_c_off_ecsa_proberesp = 0;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002990}
2991
2992
2993int hostapd_switch_channel(struct hostapd_data *hapd,
2994 struct csa_settings *settings)
2995{
2996 int ret;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002997
2998 if (!(hapd->iface->drv_flags & WPA_DRIVER_FLAGS_AP_CSA)) {
2999 wpa_printf(MSG_INFO, "CSA is not supported");
3000 return -1;
3001 }
3002
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003003 ret = hostapd_fill_csa_settings(hapd, settings);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003004 if (ret)
3005 return ret;
3006
3007 ret = hostapd_drv_switch_channel(hapd, settings);
3008 free_beacon_data(&settings->beacon_csa);
3009 free_beacon_data(&settings->beacon_after);
3010
3011 if (ret) {
3012 /* if we failed, clean cs parameters */
3013 hostapd_cleanup_cs_params(hapd);
3014 return ret;
3015 }
3016
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003017 hapd->csa_in_progress = 1;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003018 return 0;
3019}
3020
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003021
3022void
3023hostapd_switch_channel_fallback(struct hostapd_iface *iface,
3024 const struct hostapd_freq_params *freq_params)
3025{
3026 int vht_seg0_idx = 0, vht_seg1_idx = 0, vht_bw = VHT_CHANWIDTH_USE_HT;
3027 unsigned int i;
3028
3029 wpa_printf(MSG_DEBUG, "Restarting all CSA-related BSSes");
3030
3031 if (freq_params->center_freq1)
3032 vht_seg0_idx = 36 + (freq_params->center_freq1 - 5180) / 5;
3033 if (freq_params->center_freq2)
3034 vht_seg1_idx = 36 + (freq_params->center_freq2 - 5180) / 5;
3035
3036 switch (freq_params->bandwidth) {
3037 case 0:
3038 case 20:
3039 case 40:
3040 vht_bw = VHT_CHANWIDTH_USE_HT;
3041 break;
3042 case 80:
3043 if (freq_params->center_freq2)
3044 vht_bw = VHT_CHANWIDTH_80P80MHZ;
3045 else
3046 vht_bw = VHT_CHANWIDTH_80MHZ;
3047 break;
3048 case 160:
3049 vht_bw = VHT_CHANWIDTH_160MHZ;
3050 break;
3051 default:
3052 wpa_printf(MSG_WARNING, "Unknown CSA bandwidth: %d",
3053 freq_params->bandwidth);
3054 break;
3055 }
3056
3057 iface->freq = freq_params->freq;
3058 iface->conf->channel = freq_params->channel;
3059 iface->conf->secondary_channel = freq_params->sec_channel_offset;
3060 iface->conf->vht_oper_centr_freq_seg0_idx = vht_seg0_idx;
3061 iface->conf->vht_oper_centr_freq_seg1_idx = vht_seg1_idx;
3062 iface->conf->vht_oper_chwidth = vht_bw;
3063 iface->conf->ieee80211n = freq_params->ht_enabled;
3064 iface->conf->ieee80211ac = freq_params->vht_enabled;
3065
3066 /*
3067 * cs_params must not be cleared earlier because the freq_params
3068 * argument may actually point to one of these.
3069 */
3070 for (i = 0; i < iface->num_bss; i++)
3071 hostapd_cleanup_cs_params(iface->bss[i]);
3072
3073 hostapd_disable_iface(iface);
3074 hostapd_enable_iface(iface);
3075}
3076
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003077
3078struct hostapd_data * hostapd_get_iface(struct hapd_interfaces *interfaces,
3079 const char *ifname)
3080{
3081 size_t i, j;
3082
3083 for (i = 0; i < interfaces->count; i++) {
3084 struct hostapd_iface *iface = interfaces->iface[i];
3085
3086 for (j = 0; j < iface->num_bss; j++) {
3087 struct hostapd_data *hapd = iface->bss[j];
3088
3089 if (os_strcmp(ifname, hapd->conf->iface) == 0)
3090 return hapd;
3091 }
3092 }
3093
3094 return NULL;
3095}
3096
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003097#endif /* NEED_AP_MLME */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003098
3099
3100void hostapd_periodic_iface(struct hostapd_iface *iface)
3101{
3102 size_t i;
3103
3104 ap_list_timer(iface);
3105
3106 for (i = 0; i < iface->num_bss; i++) {
3107 struct hostapd_data *hapd = iface->bss[i];
3108
3109 if (!hapd->started)
3110 continue;
3111
3112#ifndef CONFIG_NO_RADIUS
3113 hostapd_acl_expire(hapd);
3114#endif /* CONFIG_NO_RADIUS */
3115 }
3116}