blob: 55ca9e8f9ba857a060c86bf18658b5e68451abfd [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
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -0800515 if (iface->conf->use_driver_iface_addr)
516 return 0;
517
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700518 /* Generate BSSID mask that is large enough to cover the BSSIDs. */
519
520 /* Determine the bits necessary to cover the number of BSSIDs. */
521 for (i--; i; i >>= 1)
522 bits++;
523
524 /* Determine the bits necessary to any configured BSSIDs,
525 if they are higher than the number of BSSIDs. */
526 for (j = 0; j < iface->conf->num_bss; j++) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800527 if (is_zero_ether_addr(iface->conf->bss[j]->bssid)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700528 if (j)
529 auto_addr++;
530 continue;
531 }
532
533 for (i = 0; i < ETH_ALEN; i++) {
534 mask[i] |=
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800535 iface->conf->bss[j]->bssid[i] ^
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700536 hapd->own_addr[i];
537 }
538 }
539
540 if (!auto_addr)
541 goto skip_mask_ext;
542
543 for (i = 0; i < ETH_ALEN && mask[i] == 0; i++)
544 ;
545 j = 0;
546 if (i < ETH_ALEN) {
547 j = (5 - i) * 8;
548
549 while (mask[i] != 0) {
550 mask[i] >>= 1;
551 j++;
552 }
553 }
554
555 if (bits < j)
556 bits = j;
557
558 if (bits > 40) {
559 wpa_printf(MSG_ERROR, "Too many bits in the BSSID mask (%u)",
560 bits);
561 return -1;
562 }
563
564 os_memset(mask, 0xff, ETH_ALEN);
565 j = bits / 8;
566 for (i = 5; i > 5 - j; i--)
567 mask[i] = 0;
568 j = bits % 8;
569 while (j--)
570 mask[i] <<= 1;
571
572skip_mask_ext:
573 wpa_printf(MSG_DEBUG, "BSS count %lu, BSSID mask " MACSTR " (%d bits)",
574 (unsigned long) iface->conf->num_bss, MAC2STR(mask), bits);
575
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700576 if (!auto_addr)
577 return 0;
578
579 for (i = 0; i < ETH_ALEN; i++) {
580 if ((hapd->own_addr[i] & mask[i]) != hapd->own_addr[i]) {
581 wpa_printf(MSG_ERROR, "Invalid BSSID mask " MACSTR
582 " for start address " MACSTR ".",
583 MAC2STR(mask), MAC2STR(hapd->own_addr));
584 wpa_printf(MSG_ERROR, "Start address must be the "
585 "first address in the block (i.e., addr "
586 "AND mask == addr).");
587 return -1;
588 }
589 }
590
591 return 0;
592}
593
594
595static int mac_in_conf(struct hostapd_config *conf, const void *a)
596{
597 size_t i;
598
599 for (i = 0; i < conf->num_bss; i++) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800600 if (hostapd_mac_comp(conf->bss[i]->bssid, a) == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700601 return 1;
602 }
603 }
604
605 return 0;
606}
607
608
Dmitry Shmidt04949592012-07-19 12:16:46 -0700609#ifndef CONFIG_NO_RADIUS
610
611static int hostapd_das_nas_mismatch(struct hostapd_data *hapd,
612 struct radius_das_attrs *attr)
613{
Dmitry Shmidt13ca8d82014-02-20 10:18:40 -0800614 if (attr->nas_identifier &&
615 (!hapd->conf->nas_identifier ||
616 os_strlen(hapd->conf->nas_identifier) !=
617 attr->nas_identifier_len ||
618 os_memcmp(hapd->conf->nas_identifier, attr->nas_identifier,
619 attr->nas_identifier_len) != 0)) {
620 wpa_printf(MSG_DEBUG, "RADIUS DAS: NAS-Identifier mismatch");
621 return 1;
622 }
623
624 if (attr->nas_ip_addr &&
625 (hapd->conf->own_ip_addr.af != AF_INET ||
626 os_memcmp(&hapd->conf->own_ip_addr.u.v4, attr->nas_ip_addr, 4) !=
627 0)) {
628 wpa_printf(MSG_DEBUG, "RADIUS DAS: NAS-IP-Address mismatch");
629 return 1;
630 }
631
632#ifdef CONFIG_IPV6
633 if (attr->nas_ipv6_addr &&
634 (hapd->conf->own_ip_addr.af != AF_INET6 ||
635 os_memcmp(&hapd->conf->own_ip_addr.u.v6, attr->nas_ipv6_addr, 16)
636 != 0)) {
637 wpa_printf(MSG_DEBUG, "RADIUS DAS: NAS-IPv6-Address mismatch");
638 return 1;
639 }
640#endif /* CONFIG_IPV6 */
641
Dmitry Shmidt04949592012-07-19 12:16:46 -0700642 return 0;
643}
644
645
646static struct sta_info * hostapd_das_find_sta(struct hostapd_data *hapd,
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800647 struct radius_das_attrs *attr,
648 int *multi)
Dmitry Shmidt04949592012-07-19 12:16:46 -0700649{
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800650 struct sta_info *selected, *sta;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700651 char buf[128];
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800652 int num_attr = 0;
653 int count;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700654
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800655 *multi = 0;
656
657 for (sta = hapd->sta_list; sta; sta = sta->next)
658 sta->radius_das_match = 1;
659
660 if (attr->sta_addr) {
661 num_attr++;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700662 sta = ap_get_sta(hapd, attr->sta_addr);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800663 if (!sta) {
664 wpa_printf(MSG_DEBUG,
665 "RADIUS DAS: No Calling-Station-Id match");
666 return NULL;
667 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700668
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800669 selected = sta;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700670 for (sta = hapd->sta_list; sta; sta = sta->next) {
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800671 if (sta != selected)
672 sta->radius_das_match = 0;
673 }
674 wpa_printf(MSG_DEBUG, "RADIUS DAS: Calling-Station-Id match");
675 }
676
677 if (attr->acct_session_id) {
678 num_attr++;
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800679 if (attr->acct_session_id_len != 16) {
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800680 wpa_printf(MSG_DEBUG,
681 "RADIUS DAS: Acct-Session-Id cannot match");
682 return NULL;
683 }
684 count = 0;
685
686 for (sta = hapd->sta_list; sta; sta = sta->next) {
687 if (!sta->radius_das_match)
688 continue;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800689 os_snprintf(buf, sizeof(buf), "%016llX",
690 (unsigned long long) sta->acct_session_id);
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800691 if (os_memcmp(attr->acct_session_id, buf, 16) != 0)
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800692 sta->radius_das_match = 0;
693 else
694 count++;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700695 }
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800696
697 if (count == 0) {
698 wpa_printf(MSG_DEBUG,
699 "RADIUS DAS: No matches remaining after Acct-Session-Id check");
700 return NULL;
701 }
702 wpa_printf(MSG_DEBUG, "RADIUS DAS: Acct-Session-Id match");
Dmitry Shmidt04949592012-07-19 12:16:46 -0700703 }
704
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800705 if (attr->acct_multi_session_id) {
706 num_attr++;
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800707 if (attr->acct_multi_session_id_len != 16) {
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800708 wpa_printf(MSG_DEBUG,
709 "RADIUS DAS: Acct-Multi-Session-Id cannot match");
710 return NULL;
711 }
712 count = 0;
713
714 for (sta = hapd->sta_list; sta; sta = sta->next) {
715 if (!sta->radius_das_match)
716 continue;
717 if (!sta->eapol_sm ||
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800718 !sta->eapol_sm->acct_multi_session_id) {
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800719 sta->radius_das_match = 0;
720 continue;
721 }
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800722 os_snprintf(buf, sizeof(buf), "%016llX",
723 (unsigned long long)
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800724 sta->eapol_sm->acct_multi_session_id);
725 if (os_memcmp(attr->acct_multi_session_id, buf, 16) !=
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800726 0)
727 sta->radius_das_match = 0;
728 else
729 count++;
730 }
731
732 if (count == 0) {
733 wpa_printf(MSG_DEBUG,
734 "RADIUS DAS: No matches remaining after Acct-Multi-Session-Id check");
735 return NULL;
736 }
737 wpa_printf(MSG_DEBUG,
738 "RADIUS DAS: Acct-Multi-Session-Id match");
739 }
740
741 if (attr->cui) {
742 num_attr++;
743 count = 0;
744
Dmitry Shmidt04949592012-07-19 12:16:46 -0700745 for (sta = hapd->sta_list; sta; sta = sta->next) {
746 struct wpabuf *cui;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800747
748 if (!sta->radius_das_match)
749 continue;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700750 cui = ieee802_1x_get_radius_cui(sta->eapol_sm);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800751 if (!cui || wpabuf_len(cui) != attr->cui_len ||
Dmitry Shmidt04949592012-07-19 12:16:46 -0700752 os_memcmp(wpabuf_head(cui), attr->cui,
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800753 attr->cui_len) != 0)
754 sta->radius_das_match = 0;
755 else
756 count++;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700757 }
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800758
759 if (count == 0) {
760 wpa_printf(MSG_DEBUG,
761 "RADIUS DAS: No matches remaining after Chargeable-User-Identity check");
762 return NULL;
763 }
764 wpa_printf(MSG_DEBUG,
765 "RADIUS DAS: Chargeable-User-Identity match");
Dmitry Shmidt04949592012-07-19 12:16:46 -0700766 }
767
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800768 if (attr->user_name) {
769 num_attr++;
770 count = 0;
771
Dmitry Shmidt04949592012-07-19 12:16:46 -0700772 for (sta = hapd->sta_list; sta; sta = sta->next) {
773 u8 *identity;
774 size_t identity_len;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800775
776 if (!sta->radius_das_match)
777 continue;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700778 identity = ieee802_1x_get_identity(sta->eapol_sm,
779 &identity_len);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800780 if (!identity ||
781 identity_len != attr->user_name_len ||
Dmitry Shmidt04949592012-07-19 12:16:46 -0700782 os_memcmp(identity, attr->user_name, identity_len)
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800783 != 0)
784 sta->radius_das_match = 0;
785 else
786 count++;
787 }
788
789 if (count == 0) {
790 wpa_printf(MSG_DEBUG,
791 "RADIUS DAS: No matches remaining after User-Name check");
792 return NULL;
793 }
794 wpa_printf(MSG_DEBUG,
795 "RADIUS DAS: User-Name match");
796 }
797
798 if (num_attr == 0) {
799 /*
800 * In theory, we could match all current associations, but it
801 * seems safer to just reject requests that do not include any
802 * session identification attributes.
803 */
804 wpa_printf(MSG_DEBUG,
805 "RADIUS DAS: No session identification attributes included");
806 return NULL;
807 }
808
809 selected = NULL;
810 for (sta = hapd->sta_list; sta; sta = sta->next) {
811 if (sta->radius_das_match) {
812 if (selected) {
813 *multi = 1;
814 return NULL;
815 }
816 selected = sta;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700817 }
818 }
819
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800820 return selected;
821}
822
823
824static int hostapd_das_disconnect_pmksa(struct hostapd_data *hapd,
825 struct radius_das_attrs *attr)
826{
827 if (!hapd->wpa_auth)
828 return -1;
829 return wpa_auth_radius_das_disconnect_pmksa(hapd->wpa_auth, attr);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700830}
831
832
833static enum radius_das_res
834hostapd_das_disconnect(void *ctx, struct radius_das_attrs *attr)
835{
836 struct hostapd_data *hapd = ctx;
837 struct sta_info *sta;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800838 int multi;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700839
840 if (hostapd_das_nas_mismatch(hapd, attr))
841 return RADIUS_DAS_NAS_MISMATCH;
842
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800843 sta = hostapd_das_find_sta(hapd, attr, &multi);
844 if (sta == NULL) {
845 if (multi) {
846 wpa_printf(MSG_DEBUG,
847 "RADIUS DAS: Multiple sessions match - not supported");
848 return RADIUS_DAS_MULTI_SESSION_MATCH;
849 }
850 if (hostapd_das_disconnect_pmksa(hapd, attr) == 0) {
851 wpa_printf(MSG_DEBUG,
852 "RADIUS DAS: PMKSA cache entry matched");
853 return RADIUS_DAS_SUCCESS;
854 }
855 wpa_printf(MSG_DEBUG, "RADIUS DAS: No matching session found");
Dmitry Shmidt04949592012-07-19 12:16:46 -0700856 return RADIUS_DAS_SESSION_NOT_FOUND;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800857 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700858
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800859 wpa_printf(MSG_DEBUG, "RADIUS DAS: Found a matching session " MACSTR
860 " - disconnecting", MAC2STR(sta->addr));
Dmitry Shmidt13ca8d82014-02-20 10:18:40 -0800861 wpa_auth_pmksa_remove(hapd->wpa_auth, sta->addr);
862
Dmitry Shmidt04949592012-07-19 12:16:46 -0700863 hostapd_drv_sta_deauth(hapd, sta->addr,
864 WLAN_REASON_PREV_AUTH_NOT_VALID);
865 ap_sta_deauthenticate(hapd, sta, WLAN_REASON_PREV_AUTH_NOT_VALID);
866
867 return RADIUS_DAS_SUCCESS;
868}
869
870#endif /* CONFIG_NO_RADIUS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700871
872
873/**
874 * hostapd_setup_bss - Per-BSS setup (initialization)
875 * @hapd: Pointer to BSS data
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800876 * @first: Whether this BSS is the first BSS of an interface; -1 = not first,
877 * but interface may exist
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700878 *
879 * This function is used to initialize all per-BSS data structures and
880 * resources. This gets called in a loop for each BSS when an interface is
881 * initialized. Most of the modules that are initialized here will be
882 * deinitialized in hostapd_cleanup().
883 */
884static int hostapd_setup_bss(struct hostapd_data *hapd, int first)
885{
886 struct hostapd_bss_config *conf = hapd->conf;
Dmitry Shmidt9d9e6022015-04-23 10:34:55 -0700887 u8 ssid[SSID_MAX_LEN + 1];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700888 int ssid_len, set_ssid;
889 char force_ifname[IFNAMSIZ];
890 u8 if_addr[ETH_ALEN];
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800891 int flush_old_stations = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700892
Dmitry Shmidt54605472013-11-08 11:10:19 -0800893 wpa_printf(MSG_DEBUG, "%s(hapd=%p (%s), first=%d)",
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700894 __func__, hapd, conf->iface, first);
Dmitry Shmidt54605472013-11-08 11:10:19 -0800895
Dmitry Shmidt50b691d2014-05-21 14:01:45 -0700896#ifdef EAP_SERVER_TNC
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700897 if (conf->tnc && tncs_global_init() < 0) {
Dmitry Shmidt50b691d2014-05-21 14:01:45 -0700898 wpa_printf(MSG_ERROR, "Failed to initialize TNCS");
899 return -1;
900 }
901#endif /* EAP_SERVER_TNC */
902
Dmitry Shmidt54605472013-11-08 11:10:19 -0800903 if (hapd->started) {
904 wpa_printf(MSG_ERROR, "%s: Interface %s was already started",
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700905 __func__, conf->iface);
Dmitry Shmidt54605472013-11-08 11:10:19 -0800906 return -1;
907 }
908 hapd->started = 1;
909
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800910 if (!first || first == -1) {
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -0800911 u8 *addr = hapd->own_addr;
912
913 if (!is_zero_ether_addr(conf->bssid)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700914 /* 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 }
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -0800925 } else if (hapd->iconf->use_driver_iface_addr) {
926 addr = NULL;
927 } else {
928 /* Allocate the next available BSSID. */
929 do {
930 inc_byte_array(hapd->own_addr, ETH_ALEN);
931 } while (mac_in_conf(hapd->iconf, hapd->own_addr));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700932 }
933
Dmitry Shmidt54605472013-11-08 11:10:19 -0800934 hapd->interface_added = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700935 if (hostapd_if_add(hapd->iface->bss[0], WPA_IF_AP_BSS,
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -0800936 conf->iface, addr, hapd,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700937 &hapd->drv_priv, force_ifname, if_addr,
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700938 conf->bridge[0] ? conf->bridge : NULL,
939 first == -1)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700940 wpa_printf(MSG_ERROR, "Failed to add BSS (BSSID="
941 MACSTR ")", MAC2STR(hapd->own_addr));
Dmitry Shmidt3cf6f792013-12-18 13:12:19 -0800942 hapd->interface_added = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700943 return -1;
944 }
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -0800945
946 if (!addr)
947 os_memcpy(hapd->own_addr, if_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700948 }
949
950 if (conf->wmm_enabled < 0)
951 conf->wmm_enabled = hapd->iconf->ieee80211n;
952
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800953#ifdef CONFIG_IEEE80211R
954 if (is_zero_ether_addr(conf->r1_key_holder))
955 os_memcpy(conf->r1_key_holder, hapd->own_addr, ETH_ALEN);
956#endif /* CONFIG_IEEE80211R */
957
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800958#ifdef CONFIG_MESH
959 if (hapd->iface->mconf == NULL)
960 flush_old_stations = 0;
961#endif /* CONFIG_MESH */
962
963 if (flush_old_stations)
964 hostapd_flush_old_stations(hapd,
965 WLAN_REASON_PREV_AUTH_NOT_VALID);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700966 hostapd_set_privacy(hapd, 0);
967
968 hostapd_broadcast_wep_clear(hapd);
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700969 if (hostapd_setup_encryption(conf->iface, hapd))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700970 return -1;
971
972 /*
973 * Fetch the SSID from the system and use it or,
974 * if one was specified in the config file, verify they
975 * match.
976 */
977 ssid_len = hostapd_get_ssid(hapd, ssid, sizeof(ssid));
978 if (ssid_len < 0) {
979 wpa_printf(MSG_ERROR, "Could not read SSID from system");
980 return -1;
981 }
982 if (conf->ssid.ssid_set) {
983 /*
984 * If SSID is specified in the config file and it differs
985 * from what is being used then force installation of the
986 * new SSID.
987 */
988 set_ssid = (conf->ssid.ssid_len != (size_t) ssid_len ||
989 os_memcmp(conf->ssid.ssid, ssid, ssid_len) != 0);
990 } else {
991 /*
992 * No SSID in the config file; just use the one we got
993 * from the system.
994 */
995 set_ssid = 0;
996 conf->ssid.ssid_len = ssid_len;
997 os_memcpy(conf->ssid.ssid, ssid, conf->ssid.ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700998 }
999
1000 if (!hostapd_drv_none(hapd)) {
1001 wpa_printf(MSG_ERROR, "Using interface %s with hwaddr " MACSTR
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001002 " and ssid \"%s\"",
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001003 conf->iface, MAC2STR(hapd->own_addr),
1004 wpa_ssid_txt(conf->ssid.ssid, conf->ssid.ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001005 }
1006
1007 if (hostapd_setup_wpa_psk(conf)) {
1008 wpa_printf(MSG_ERROR, "WPA-PSK setup failed.");
1009 return -1;
1010 }
1011
1012 /* Set SSID for the kernel driver (to be used in beacon and probe
1013 * response frames) */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001014 if (set_ssid && hostapd_set_ssid(hapd, conf->ssid.ssid,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001015 conf->ssid.ssid_len)) {
1016 wpa_printf(MSG_ERROR, "Could not set SSID for kernel driver");
1017 return -1;
1018 }
1019
Dmitry Shmidt818ea482014-03-10 13:15:21 -07001020 if (wpa_debug_level <= MSG_MSGDUMP)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001021 conf->radius->msg_dumps = 1;
1022#ifndef CONFIG_NO_RADIUS
1023 hapd->radius = radius_client_init(hapd, conf->radius);
1024 if (hapd->radius == NULL) {
1025 wpa_printf(MSG_ERROR, "RADIUS client initialization failed.");
1026 return -1;
1027 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001028
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001029 if (conf->radius_das_port) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001030 struct radius_das_conf das_conf;
1031 os_memset(&das_conf, 0, sizeof(das_conf));
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001032 das_conf.port = conf->radius_das_port;
1033 das_conf.shared_secret = conf->radius_das_shared_secret;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001034 das_conf.shared_secret_len =
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001035 conf->radius_das_shared_secret_len;
1036 das_conf.client_addr = &conf->radius_das_client_addr;
1037 das_conf.time_window = conf->radius_das_time_window;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001038 das_conf.require_event_timestamp =
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001039 conf->radius_das_require_event_timestamp;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001040 das_conf.ctx = hapd;
1041 das_conf.disconnect = hostapd_das_disconnect;
1042 hapd->radius_das = radius_das_init(&das_conf);
1043 if (hapd->radius_das == NULL) {
1044 wpa_printf(MSG_ERROR, "RADIUS DAS initialization "
1045 "failed.");
1046 return -1;
1047 }
1048 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001049#endif /* CONFIG_NO_RADIUS */
1050
1051 if (hostapd_acl_init(hapd)) {
1052 wpa_printf(MSG_ERROR, "ACL initialization failed.");
1053 return -1;
1054 }
1055 if (hostapd_init_wps(hapd, conf))
1056 return -1;
1057
1058 if (authsrv_init(hapd) < 0)
1059 return -1;
1060
1061 if (ieee802_1x_init(hapd)) {
1062 wpa_printf(MSG_ERROR, "IEEE 802.1X initialization failed.");
1063 return -1;
1064 }
1065
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001066 if ((conf->wpa || conf->osen) && hostapd_setup_wpa(hapd))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001067 return -1;
1068
1069 if (accounting_init(hapd)) {
1070 wpa_printf(MSG_ERROR, "Accounting initialization failed.");
1071 return -1;
1072 }
1073
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001074 if (conf->ieee802_11f &&
1075 (hapd->iapp = iapp_init(hapd, conf->iapp_iface)) == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001076 wpa_printf(MSG_ERROR, "IEEE 802.11F (IAPP) initialization "
1077 "failed.");
1078 return -1;
1079 }
1080
Dmitry Shmidt04949592012-07-19 12:16:46 -07001081#ifdef CONFIG_INTERWORKING
1082 if (gas_serv_init(hapd)) {
1083 wpa_printf(MSG_ERROR, "GAS server initialization failed");
1084 return -1;
1085 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001086
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001087 if (conf->qos_map_set_len &&
1088 hostapd_drv_set_qos_map(hapd, conf->qos_map_set,
1089 conf->qos_map_set_len)) {
1090 wpa_printf(MSG_ERROR, "Failed to initialize QoS Map");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001091 return -1;
1092 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001093#endif /* CONFIG_INTERWORKING */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001094
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001095 if (conf->bss_load_update_period && bss_load_update_init(hapd)) {
1096 wpa_printf(MSG_ERROR, "BSS Load initialization failed");
1097 return -1;
1098 }
1099
1100 if (conf->proxy_arp) {
1101 if (x_snoop_init(hapd)) {
1102 wpa_printf(MSG_ERROR,
1103 "Generic snooping infrastructure initialization failed");
1104 return -1;
1105 }
1106
1107 if (dhcp_snoop_init(hapd)) {
1108 wpa_printf(MSG_ERROR,
1109 "DHCP snooping initialization failed");
1110 return -1;
1111 }
1112
1113 if (ndisc_snoop_init(hapd)) {
1114 wpa_printf(MSG_ERROR,
1115 "Neighbor Discovery snooping initialization failed");
1116 return -1;
1117 }
1118 }
1119
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001120 if (!hostapd_drv_none(hapd) && vlan_init(hapd)) {
1121 wpa_printf(MSG_ERROR, "VLAN initialization failed.");
1122 return -1;
1123 }
1124
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001125 if (!conf->start_disabled && ieee802_11_set_beacon(hapd) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001126 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001127
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001128 if (hapd->wpa_auth && wpa_init_keys(hapd->wpa_auth) < 0)
1129 return -1;
1130
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001131 if (hapd->driver && hapd->driver->set_operstate)
1132 hapd->driver->set_operstate(hapd->drv_priv, 1);
1133
1134 return 0;
1135}
1136
1137
1138static void hostapd_tx_queue_params(struct hostapd_iface *iface)
1139{
1140 struct hostapd_data *hapd = iface->bss[0];
1141 int i;
1142 struct hostapd_tx_queue_params *p;
1143
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001144#ifdef CONFIG_MESH
1145 if (iface->mconf == NULL)
1146 return;
1147#endif /* CONFIG_MESH */
1148
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001149 for (i = 0; i < NUM_TX_QUEUES; i++) {
1150 p = &iface->conf->tx_queue[i];
1151
1152 if (hostapd_set_tx_queue_params(hapd, i, p->aifs, p->cwmin,
1153 p->cwmax, p->burst)) {
1154 wpa_printf(MSG_DEBUG, "Failed to set TX queue "
1155 "parameters for queue %d.", i);
1156 /* Continue anyway */
1157 }
1158 }
1159}
1160
1161
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001162static int hostapd_set_acl_list(struct hostapd_data *hapd,
1163 struct mac_acl_entry *mac_acl,
1164 int n_entries, u8 accept_acl)
1165{
1166 struct hostapd_acl_params *acl_params;
1167 int i, err;
1168
1169 acl_params = os_zalloc(sizeof(*acl_params) +
1170 (n_entries * sizeof(acl_params->mac_acl[0])));
1171 if (!acl_params)
1172 return -ENOMEM;
1173
1174 for (i = 0; i < n_entries; i++)
1175 os_memcpy(acl_params->mac_acl[i].addr, mac_acl[i].addr,
1176 ETH_ALEN);
1177
1178 acl_params->acl_policy = accept_acl;
1179 acl_params->num_mac_acl = n_entries;
1180
1181 err = hostapd_drv_set_acl(hapd, acl_params);
1182
1183 os_free(acl_params);
1184
1185 return err;
1186}
1187
1188
1189static void hostapd_set_acl(struct hostapd_data *hapd)
1190{
1191 struct hostapd_config *conf = hapd->iconf;
1192 int err;
1193 u8 accept_acl;
1194
1195 if (hapd->iface->drv_max_acl_mac_addrs == 0)
1196 return;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001197
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001198 if (conf->bss[0]->macaddr_acl == DENY_UNLESS_ACCEPTED) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001199 accept_acl = 1;
1200 err = hostapd_set_acl_list(hapd, conf->bss[0]->accept_mac,
1201 conf->bss[0]->num_accept_mac,
1202 accept_acl);
1203 if (err) {
1204 wpa_printf(MSG_DEBUG, "Failed to set accept acl");
1205 return;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001206 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001207 } else if (conf->bss[0]->macaddr_acl == ACCEPT_UNLESS_DENIED) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001208 accept_acl = 0;
1209 err = hostapd_set_acl_list(hapd, conf->bss[0]->deny_mac,
1210 conf->bss[0]->num_deny_mac,
1211 accept_acl);
1212 if (err) {
1213 wpa_printf(MSG_DEBUG, "Failed to set deny acl");
1214 return;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001215 }
1216 }
1217}
1218
1219
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001220static int start_ctrl_iface_bss(struct hostapd_data *hapd)
1221{
1222 if (!hapd->iface->interfaces ||
1223 !hapd->iface->interfaces->ctrl_iface_init)
1224 return 0;
1225
1226 if (hapd->iface->interfaces->ctrl_iface_init(hapd)) {
1227 wpa_printf(MSG_ERROR,
1228 "Failed to setup control interface for %s",
1229 hapd->conf->iface);
1230 return -1;
1231 }
1232
1233 return 0;
1234}
1235
1236
1237static int start_ctrl_iface(struct hostapd_iface *iface)
1238{
1239 size_t i;
1240
1241 if (!iface->interfaces || !iface->interfaces->ctrl_iface_init)
1242 return 0;
1243
1244 for (i = 0; i < iface->num_bss; i++) {
1245 struct hostapd_data *hapd = iface->bss[i];
1246 if (iface->interfaces->ctrl_iface_init(hapd)) {
1247 wpa_printf(MSG_ERROR,
1248 "Failed to setup control interface for %s",
1249 hapd->conf->iface);
1250 return -1;
1251 }
1252 }
1253
1254 return 0;
1255}
1256
1257
1258static void channel_list_update_timeout(void *eloop_ctx, void *timeout_ctx)
1259{
1260 struct hostapd_iface *iface = eloop_ctx;
1261
1262 if (!iface->wait_channel_update) {
1263 wpa_printf(MSG_INFO, "Channel list update timeout, but interface was not waiting for it");
1264 return;
1265 }
1266
1267 /*
1268 * It is possible that the existing channel list is acceptable, so try
1269 * to proceed.
1270 */
1271 wpa_printf(MSG_DEBUG, "Channel list update timeout - try to continue anyway");
1272 setup_interface2(iface);
1273}
1274
1275
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001276void hostapd_channel_list_updated(struct hostapd_iface *iface, int initiator)
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001277{
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001278 if (!iface->wait_channel_update || initiator != REGDOM_SET_BY_USER)
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001279 return;
1280
1281 wpa_printf(MSG_DEBUG, "Channel list updated - continue setup");
1282 eloop_cancel_timeout(channel_list_update_timeout, iface, NULL);
1283 setup_interface2(iface);
1284}
1285
1286
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001287static int setup_interface(struct hostapd_iface *iface)
1288{
1289 struct hostapd_data *hapd = iface->bss[0];
1290 size_t i;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001291
Dmitry Shmidta38abf92014-03-06 13:38:44 -08001292 /*
1293 * It is possible that setup_interface() is called after the interface
1294 * was disabled etc., in which case driver_ap_teardown is possibly set
1295 * to 1. Clear it here so any other key/station deletion, which is not
1296 * part of a teardown flow, would also call the relevant driver
1297 * callbacks.
1298 */
1299 iface->driver_ap_teardown = 0;
1300
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001301 if (!iface->phy[0]) {
1302 const char *phy = hostapd_drv_get_radio_name(hapd);
1303 if (phy) {
1304 wpa_printf(MSG_DEBUG, "phy: %s", phy);
1305 os_strlcpy(iface->phy, phy, sizeof(iface->phy));
1306 }
1307 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001308
1309 /*
1310 * Make sure that all BSSes get configured with a pointer to the same
1311 * driver interface.
1312 */
1313 for (i = 1; i < iface->num_bss; i++) {
1314 iface->bss[i]->driver = hapd->driver;
1315 iface->bss[i]->drv_priv = hapd->drv_priv;
1316 }
1317
1318 if (hostapd_validate_bssid_configuration(iface))
1319 return -1;
1320
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001321 /*
1322 * Initialize control interfaces early to allow external monitoring of
1323 * channel setup operations that may take considerable amount of time
1324 * especially for DFS cases.
1325 */
1326 if (start_ctrl_iface(iface))
1327 return -1;
1328
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001329 if (hapd->iconf->country[0] && hapd->iconf->country[1]) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001330 char country[4], previous_country[4];
1331
1332 hostapd_set_state(iface, HAPD_IFACE_COUNTRY_UPDATE);
1333 if (hostapd_get_country(hapd, previous_country) < 0)
1334 previous_country[0] = '\0';
1335
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001336 os_memcpy(country, hapd->iconf->country, 3);
1337 country[3] = '\0';
1338 if (hostapd_set_country(hapd, country) < 0) {
1339 wpa_printf(MSG_ERROR, "Failed to set country code");
1340 return -1;
1341 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001342
1343 wpa_printf(MSG_DEBUG, "Previous country code %s, new country code %s",
1344 previous_country, country);
1345
1346 if (os_strncmp(previous_country, country, 2) != 0) {
1347 wpa_printf(MSG_DEBUG, "Continue interface setup after channel list update");
1348 iface->wait_channel_update = 1;
Dmitry Shmidt97672262014-02-03 13:02:54 -08001349 eloop_register_timeout(5, 0,
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001350 channel_list_update_timeout,
1351 iface, NULL);
1352 return 0;
1353 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001354 }
1355
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001356 return setup_interface2(iface);
1357}
1358
1359
1360static int setup_interface2(struct hostapd_iface *iface)
1361{
1362 iface->wait_channel_update = 0;
1363
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001364 if (hostapd_get_hw_features(iface)) {
1365 /* Not all drivers support this yet, so continue without hw
1366 * feature data. */
1367 } else {
1368 int ret = hostapd_select_hw_mode(iface);
1369 if (ret < 0) {
1370 wpa_printf(MSG_ERROR, "Could not select hw_mode and "
1371 "channel. (%d)", ret);
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001372 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001373 }
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001374 if (ret == 1) {
1375 wpa_printf(MSG_DEBUG, "Interface initialization will be completed in a callback (ACS)");
1376 return 0;
1377 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001378 ret = hostapd_check_ht_capab(iface);
1379 if (ret < 0)
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001380 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001381 if (ret == 1) {
1382 wpa_printf(MSG_DEBUG, "Interface initialization will "
1383 "be completed in a callback");
1384 return 0;
1385 }
Dmitry Shmidt051af732013-10-22 13:52:46 -07001386
1387 if (iface->conf->ieee80211h)
1388 wpa_printf(MSG_DEBUG, "DFS support is enabled");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001389 }
1390 return hostapd_setup_interface_complete(iface, 0);
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001391
1392fail:
1393 hostapd_set_state(iface, HAPD_IFACE_DISABLED);
1394 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
1395 if (iface->interfaces && iface->interfaces->terminate_on_error)
1396 eloop_terminate();
1397 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001398}
1399
1400
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001401#ifdef CONFIG_FST
1402
1403static const u8 * fst_hostapd_get_bssid_cb(void *ctx)
1404{
1405 struct hostapd_data *hapd = ctx;
1406
1407 return hapd->own_addr;
1408}
1409
1410
1411static void fst_hostapd_get_channel_info_cb(void *ctx,
1412 enum hostapd_hw_mode *hw_mode,
1413 u8 *channel)
1414{
1415 struct hostapd_data *hapd = ctx;
1416
1417 *hw_mode = ieee80211_freq_to_chan(hapd->iface->freq, channel);
1418}
1419
1420
1421static void fst_hostapd_set_ies_cb(void *ctx, const struct wpabuf *fst_ies)
1422{
1423 struct hostapd_data *hapd = ctx;
1424
1425 if (hapd->iface->fst_ies != fst_ies) {
1426 hapd->iface->fst_ies = fst_ies;
1427 if (ieee802_11_set_beacon(hapd))
1428 wpa_printf(MSG_WARNING, "FST: Cannot set beacon");
1429 }
1430}
1431
1432
1433static int fst_hostapd_send_action_cb(void *ctx, const u8 *da,
1434 struct wpabuf *buf)
1435{
1436 struct hostapd_data *hapd = ctx;
1437
1438 return hostapd_drv_send_action(hapd, hapd->iface->freq, 0, da,
1439 wpabuf_head(buf), wpabuf_len(buf));
1440}
1441
1442
1443static const struct wpabuf * fst_hostapd_get_mb_ie_cb(void *ctx, const u8 *addr)
1444{
1445 struct hostapd_data *hapd = ctx;
1446 struct sta_info *sta = ap_get_sta(hapd, addr);
1447
1448 return sta ? sta->mb_ies : NULL;
1449}
1450
1451
1452static void fst_hostapd_update_mb_ie_cb(void *ctx, const u8 *addr,
1453 const u8 *buf, size_t size)
1454{
1455 struct hostapd_data *hapd = ctx;
1456 struct sta_info *sta = ap_get_sta(hapd, addr);
1457
1458 if (sta) {
1459 struct mb_ies_info info;
1460
1461 if (!mb_ies_info_by_ies(&info, buf, size)) {
1462 wpabuf_free(sta->mb_ies);
1463 sta->mb_ies = mb_ies_by_info(&info);
1464 }
1465 }
1466}
1467
1468
1469static const u8 * fst_hostapd_get_sta(struct fst_get_peer_ctx **get_ctx,
1470 Boolean mb_only)
1471{
1472 struct sta_info *s = (struct sta_info *) *get_ctx;
1473
1474 if (mb_only) {
1475 for (; s && !s->mb_ies; s = s->next)
1476 ;
1477 }
1478
1479 if (s) {
1480 *get_ctx = (struct fst_get_peer_ctx *) s->next;
1481
1482 return s->addr;
1483 }
1484
1485 *get_ctx = NULL;
1486 return NULL;
1487}
1488
1489
1490static const u8 * fst_hostapd_get_peer_first(void *ctx,
1491 struct fst_get_peer_ctx **get_ctx,
1492 Boolean mb_only)
1493{
1494 struct hostapd_data *hapd = ctx;
1495
1496 *get_ctx = (struct fst_get_peer_ctx *) hapd->sta_list;
1497
1498 return fst_hostapd_get_sta(get_ctx, mb_only);
1499}
1500
1501
1502static const u8 * fst_hostapd_get_peer_next(void *ctx,
1503 struct fst_get_peer_ctx **get_ctx,
1504 Boolean mb_only)
1505{
1506 return fst_hostapd_get_sta(get_ctx, mb_only);
1507}
1508
1509
1510void fst_hostapd_fill_iface_obj(struct hostapd_data *hapd,
1511 struct fst_wpa_obj *iface_obj)
1512{
1513 iface_obj->ctx = hapd;
1514 iface_obj->get_bssid = fst_hostapd_get_bssid_cb;
1515 iface_obj->get_channel_info = fst_hostapd_get_channel_info_cb;
1516 iface_obj->set_ies = fst_hostapd_set_ies_cb;
1517 iface_obj->send_action = fst_hostapd_send_action_cb;
1518 iface_obj->get_mb_ie = fst_hostapd_get_mb_ie_cb;
1519 iface_obj->update_mb_ie = fst_hostapd_update_mb_ie_cb;
1520 iface_obj->get_peer_first = fst_hostapd_get_peer_first;
1521 iface_obj->get_peer_next = fst_hostapd_get_peer_next;
1522}
1523
1524#endif /* CONFIG_FST */
1525
1526
1527static int hostapd_setup_interface_complete_sync(struct hostapd_iface *iface,
1528 int err)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001529{
1530 struct hostapd_data *hapd = iface->bss[0];
1531 size_t j;
1532 u8 *prev_addr;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001533 int delay_apply_cfg = 0;
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001534 int res_dfs_offload = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001535
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001536 if (err)
1537 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001538
1539 wpa_printf(MSG_DEBUG, "Completing interface initialization");
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001540 if (iface->conf->channel) {
Dmitry Shmidt051af732013-10-22 13:52:46 -07001541#ifdef NEED_AP_MLME
1542 int res;
1543#endif /* NEED_AP_MLME */
1544
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001545 iface->freq = hostapd_hw_get_freq(hapd, iface->conf->channel);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001546 wpa_printf(MSG_DEBUG, "Mode: %s Channel: %d "
1547 "Frequency: %d MHz",
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001548 hostapd_hw_mode_txt(iface->conf->hw_mode),
1549 iface->conf->channel, iface->freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001550
Dmitry Shmidt051af732013-10-22 13:52:46 -07001551#ifdef NEED_AP_MLME
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001552 /* Handle DFS only if it is not offloaded to the driver */
1553 if (!(iface->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD)) {
1554 /* Check DFS */
1555 res = hostapd_handle_dfs(iface);
1556 if (res <= 0) {
1557 if (res < 0)
1558 goto fail;
1559 return res;
1560 }
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001561 } else {
1562 /* If DFS is offloaded to the driver */
1563 res_dfs_offload = hostapd_handle_dfs_offload(iface);
1564 if (res_dfs_offload <= 0) {
1565 if (res_dfs_offload < 0)
1566 goto fail;
1567 } else {
1568 wpa_printf(MSG_DEBUG,
1569 "Proceed with AP/channel setup");
1570 /*
1571 * If this is a DFS channel, move to completing
1572 * AP setup.
1573 */
1574 if (res_dfs_offload == 1)
1575 goto dfs_offload;
1576 /* Otherwise fall through. */
1577 }
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001578 }
Dmitry Shmidt051af732013-10-22 13:52:46 -07001579#endif /* NEED_AP_MLME */
1580
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001581#ifdef CONFIG_MESH
1582 if (iface->mconf != NULL) {
1583 wpa_printf(MSG_DEBUG,
1584 "%s: Mesh configuration will be applied while joining the mesh network",
1585 iface->bss[0]->conf->iface);
1586 delay_apply_cfg = 1;
1587 }
1588#endif /* CONFIG_MESH */
1589
1590 if (!delay_apply_cfg &&
1591 hostapd_set_freq(hapd, hapd->iconf->hw_mode, iface->freq,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001592 hapd->iconf->channel,
1593 hapd->iconf->ieee80211n,
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001594 hapd->iconf->ieee80211ac,
1595 hapd->iconf->secondary_channel,
1596 hapd->iconf->vht_oper_chwidth,
1597 hapd->iconf->vht_oper_centr_freq_seg0_idx,
1598 hapd->iconf->vht_oper_centr_freq_seg1_idx)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001599 wpa_printf(MSG_ERROR, "Could not set channel for "
1600 "kernel driver");
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001601 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001602 }
1603 }
1604
1605 if (iface->current_mode) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001606 if (hostapd_prepare_rates(iface, iface->current_mode)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001607 wpa_printf(MSG_ERROR, "Failed to prepare rates "
1608 "table.");
1609 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
1610 HOSTAPD_LEVEL_WARNING,
1611 "Failed to prepare rates table.");
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001612 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001613 }
1614 }
1615
1616 if (hapd->iconf->rts_threshold > -1 &&
1617 hostapd_set_rts(hapd, hapd->iconf->rts_threshold)) {
1618 wpa_printf(MSG_ERROR, "Could not set RTS threshold for "
1619 "kernel driver");
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001620 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001621 }
1622
1623 if (hapd->iconf->fragm_threshold > -1 &&
1624 hostapd_set_frag(hapd, hapd->iconf->fragm_threshold)) {
1625 wpa_printf(MSG_ERROR, "Could not set fragmentation threshold "
1626 "for kernel driver");
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001627 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001628 }
1629
1630 prev_addr = hapd->own_addr;
1631
1632 for (j = 0; j < iface->num_bss; j++) {
1633 hapd = iface->bss[j];
1634 if (j)
1635 os_memcpy(hapd->own_addr, prev_addr, ETH_ALEN);
Dmitry Shmidt71757432014-06-02 13:50:35 -07001636 if (hostapd_setup_bss(hapd, j == 0)) {
1637 do {
1638 hapd = iface->bss[j];
1639 hostapd_bss_deinit_no_free(hapd);
1640 hostapd_free_hapd_data(hapd);
1641 } while (j-- > 0);
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001642 goto fail;
Dmitry Shmidt71757432014-06-02 13:50:35 -07001643 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001644 if (is_zero_ether_addr(hapd->conf->bssid))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001645 prev_addr = hapd->own_addr;
1646 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001647 hapd = iface->bss[0];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001648
1649 hostapd_tx_queue_params(iface);
1650
1651 ap_list_init(iface);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001652 dl_list_init(&iface->sta_seen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001653
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001654 hostapd_set_acl(hapd);
1655
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001656 if (hostapd_driver_commit(hapd) < 0) {
1657 wpa_printf(MSG_ERROR, "%s: Failed to commit driver "
1658 "configuration", __func__);
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001659 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001660 }
1661
Jouni Malinen87fd2792011-05-16 18:35:42 +03001662 /*
1663 * WPS UPnP module can be initialized only when the "upnp_iface" is up.
1664 * If "interface" and "upnp_iface" are the same (e.g., non-bridge
1665 * mode), the interface is up only after driver_commit, so initialize
1666 * WPS after driver_commit.
1667 */
1668 for (j = 0; j < iface->num_bss; j++) {
1669 if (hostapd_init_wps_complete(iface->bss[j]))
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001670 goto fail;
Jouni Malinen87fd2792011-05-16 18:35:42 +03001671 }
1672
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001673 if ((iface->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
1674 !res_dfs_offload) {
1675 /*
1676 * If freq is DFS, and DFS is offloaded to the driver, then wait
1677 * for CAC to complete.
1678 */
1679 wpa_printf(MSG_DEBUG, "%s: Wait for CAC to complete", __func__);
1680 return res_dfs_offload;
1681 }
1682
1683#ifdef NEED_AP_MLME
1684dfs_offload:
1685#endif /* NEED_AP_MLME */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001686
1687#ifdef CONFIG_FST
1688 if (hapd->iconf->fst_cfg.group_id[0]) {
1689 struct fst_wpa_obj iface_obj;
1690
1691 fst_hostapd_fill_iface_obj(hapd, &iface_obj);
1692 iface->fst = fst_attach(hapd->conf->iface, hapd->own_addr,
1693 &iface_obj, &hapd->iconf->fst_cfg);
1694 if (!iface->fst) {
1695 wpa_printf(MSG_ERROR, "Could not attach to FST %s",
1696 hapd->iconf->fst_cfg.group_id);
1697 goto fail;
1698 }
1699 }
1700#endif /* CONFIG_FST */
1701
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001702 hostapd_set_state(iface, HAPD_IFACE_ENABLED);
1703 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_ENABLED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001704 if (hapd->setup_complete_cb)
1705 hapd->setup_complete_cb(hapd->setup_complete_cb_ctx);
1706
1707 wpa_printf(MSG_DEBUG, "%s: Setup of interface done.",
1708 iface->bss[0]->conf->iface);
Dmitry Shmidtb96dad42013-11-05 10:07:29 -08001709 if (iface->interfaces && iface->interfaces->terminate_on_error > 0)
1710 iface->interfaces->terminate_on_error--;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001711
1712 return 0;
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001713
1714fail:
1715 wpa_printf(MSG_ERROR, "Interface initialization failed");
1716 hostapd_set_state(iface, HAPD_IFACE_DISABLED);
1717 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001718#ifdef CONFIG_FST
1719 if (iface->fst) {
1720 fst_detach(iface->fst);
1721 iface->fst = NULL;
1722 }
1723#endif /* CONFIG_FST */
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001724 if (iface->interfaces && iface->interfaces->terminate_on_error)
1725 eloop_terminate();
1726 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001727}
1728
1729
1730/**
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001731 * hostapd_setup_interface_complete - Complete interface setup
1732 *
1733 * This function is called when previous steps in the interface setup has been
1734 * completed. This can also start operations, e.g., DFS, that will require
1735 * additional processing before interface is ready to be enabled. Such
1736 * operations will call this function from eloop callbacks when finished.
1737 */
1738int hostapd_setup_interface_complete(struct hostapd_iface *iface, int err)
1739{
1740 struct hapd_interfaces *interfaces = iface->interfaces;
1741 struct hostapd_data *hapd = iface->bss[0];
1742 unsigned int i;
1743 int not_ready_in_sync_ifaces = 0;
1744
1745 if (!iface->need_to_start_in_sync)
1746 return hostapd_setup_interface_complete_sync(iface, err);
1747
1748 if (err) {
1749 wpa_printf(MSG_ERROR, "Interface initialization failed");
1750 hostapd_set_state(iface, HAPD_IFACE_DISABLED);
1751 iface->need_to_start_in_sync = 0;
1752 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
1753 if (interfaces && interfaces->terminate_on_error)
1754 eloop_terminate();
1755 return -1;
1756 }
1757
1758 if (iface->ready_to_start_in_sync) {
1759 /* Already in ready and waiting. should never happpen */
1760 return 0;
1761 }
1762
1763 for (i = 0; i < interfaces->count; i++) {
1764 if (interfaces->iface[i]->need_to_start_in_sync &&
1765 !interfaces->iface[i]->ready_to_start_in_sync)
1766 not_ready_in_sync_ifaces++;
1767 }
1768
1769 /*
1770 * Check if this is the last interface, if yes then start all the other
1771 * waiting interfaces. If not, add this interface to the waiting list.
1772 */
1773 if (not_ready_in_sync_ifaces > 1 && iface->state == HAPD_IFACE_DFS) {
1774 /*
1775 * If this interface went through CAC, do not synchronize, just
1776 * start immediately.
1777 */
1778 iface->need_to_start_in_sync = 0;
1779 wpa_printf(MSG_INFO,
1780 "%s: Finished CAC - bypass sync and start interface",
1781 iface->bss[0]->conf->iface);
1782 return hostapd_setup_interface_complete_sync(iface, err);
1783 }
1784
1785 if (not_ready_in_sync_ifaces > 1) {
1786 /* need to wait as there are other interfaces still coming up */
1787 iface->ready_to_start_in_sync = 1;
1788 wpa_printf(MSG_INFO,
1789 "%s: Interface waiting to sync with other interfaces",
1790 iface->bss[0]->conf->iface);
1791 return 0;
1792 }
1793
1794 wpa_printf(MSG_INFO,
1795 "%s: Last interface to sync - starting all interfaces",
1796 iface->bss[0]->conf->iface);
1797 iface->need_to_start_in_sync = 0;
1798 hostapd_setup_interface_complete_sync(iface, err);
1799 for (i = 0; i < interfaces->count; i++) {
1800 if (interfaces->iface[i]->need_to_start_in_sync &&
1801 interfaces->iface[i]->ready_to_start_in_sync) {
1802 hostapd_setup_interface_complete_sync(
1803 interfaces->iface[i], 0);
1804 /* Only once the interfaces are sync started */
1805 interfaces->iface[i]->need_to_start_in_sync = 0;
1806 }
1807 }
1808
1809 return 0;
1810}
1811
1812
1813/**
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001814 * hostapd_setup_interface - Setup of an interface
1815 * @iface: Pointer to interface data.
1816 * Returns: 0 on success, -1 on failure
1817 *
1818 * Initializes the driver interface, validates the configuration,
1819 * and sets driver parameters based on the configuration.
1820 * Flushes old stations, sets the channel, encryption,
1821 * beacons, and WDS links based on the configuration.
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001822 *
1823 * If interface setup requires more time, e.g., to perform HT co-ex scans, ACS,
1824 * or DFS operations, this function returns 0 before such operations have been
1825 * completed. The pending operations are registered into eloop and will be
1826 * completed from eloop callbacks. Those callbacks end up calling
1827 * hostapd_setup_interface_complete() once setup has been completed.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001828 */
1829int hostapd_setup_interface(struct hostapd_iface *iface)
1830{
1831 int ret;
1832
1833 ret = setup_interface(iface);
1834 if (ret) {
1835 wpa_printf(MSG_ERROR, "%s: Unable to setup interface.",
1836 iface->bss[0]->conf->iface);
1837 return -1;
1838 }
1839
1840 return 0;
1841}
1842
1843
1844/**
1845 * hostapd_alloc_bss_data - Allocate and initialize per-BSS data
1846 * @hapd_iface: Pointer to interface data
1847 * @conf: Pointer to per-interface configuration
1848 * @bss: Pointer to per-BSS configuration for this BSS
1849 * Returns: Pointer to allocated BSS data
1850 *
1851 * This function is used to allocate per-BSS data structure. This data will be
1852 * freed after hostapd_cleanup() is called for it during interface
1853 * deinitialization.
1854 */
1855struct hostapd_data *
1856hostapd_alloc_bss_data(struct hostapd_iface *hapd_iface,
1857 struct hostapd_config *conf,
1858 struct hostapd_bss_config *bss)
1859{
1860 struct hostapd_data *hapd;
1861
1862 hapd = os_zalloc(sizeof(*hapd));
1863 if (hapd == NULL)
1864 return NULL;
1865
1866 hapd->new_assoc_sta_cb = hostapd_new_assoc_sta;
1867 hapd->iconf = conf;
1868 hapd->conf = bss;
1869 hapd->iface = hapd_iface;
1870 hapd->driver = hapd->iconf->driver;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001871 hapd->ctrl_sock = -1;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08001872 dl_list_init(&hapd->ctrl_dst);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001873
1874 return hapd;
1875}
1876
1877
Dmitry Shmidt54605472013-11-08 11:10:19 -08001878static void hostapd_bss_deinit(struct hostapd_data *hapd)
1879{
1880 wpa_printf(MSG_DEBUG, "%s: deinit bss %s", __func__,
1881 hapd->conf->iface);
Dmitry Shmidt71757432014-06-02 13:50:35 -07001882 hostapd_bss_deinit_no_free(hapd);
Dmitry Shmidtf73259c2015-03-17 11:00:54 -07001883 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
Dmitry Shmidt54605472013-11-08 11:10:19 -08001884 hostapd_cleanup(hapd);
1885}
1886
1887
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001888void hostapd_interface_deinit(struct hostapd_iface *iface)
1889{
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001890 int j;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001891
Dmitry Shmidt54605472013-11-08 11:10:19 -08001892 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001893 if (iface == NULL)
1894 return;
1895
Dmitry Shmidtf73259c2015-03-17 11:00:54 -07001896 hostapd_set_state(iface, HAPD_IFACE_DISABLED);
1897
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07001898#ifdef CONFIG_IEEE80211N
1899#ifdef NEED_AP_MLME
1900 hostapd_stop_setup_timers(iface);
1901 eloop_cancel_timeout(ap_ht2040_timeout, iface, NULL);
1902#endif /* NEED_AP_MLME */
1903#endif /* CONFIG_IEEE80211N */
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001904 eloop_cancel_timeout(channel_list_update_timeout, iface, NULL);
1905 iface->wait_channel_update = 0;
1906
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001907#ifdef CONFIG_FST
1908 if (iface->fst) {
1909 fst_detach(iface->fst);
1910 iface->fst = NULL;
1911 }
1912#endif /* CONFIG_FST */
1913
Dmitry Shmidt54605472013-11-08 11:10:19 -08001914 for (j = iface->num_bss - 1; j >= 0; j--)
1915 hostapd_bss_deinit(iface->bss[j]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001916}
1917
1918
1919void hostapd_interface_free(struct hostapd_iface *iface)
1920{
1921 size_t j;
Dmitry Shmidt54605472013-11-08 11:10:19 -08001922 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
1923 for (j = 0; j < iface->num_bss; j++) {
1924 wpa_printf(MSG_DEBUG, "%s: free hapd %p",
1925 __func__, iface->bss[j]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001926 os_free(iface->bss[j]);
Dmitry Shmidt54605472013-11-08 11:10:19 -08001927 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001928 hostapd_cleanup_iface(iface);
1929}
1930
1931
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001932/**
1933 * hostapd_init - Allocate and initialize per-interface data
1934 * @config_file: Path to the configuration file
1935 * Returns: Pointer to the allocated interface data or %NULL on failure
1936 *
1937 * This function is used to allocate main data structures for per-interface
1938 * data. The allocated data buffer will be freed by calling
1939 * hostapd_cleanup_iface().
1940 */
1941struct hostapd_iface * hostapd_init(struct hapd_interfaces *interfaces,
1942 const char *config_file)
1943{
1944 struct hostapd_iface *hapd_iface = NULL;
1945 struct hostapd_config *conf = NULL;
1946 struct hostapd_data *hapd;
1947 size_t i;
1948
1949 hapd_iface = os_zalloc(sizeof(*hapd_iface));
1950 if (hapd_iface == NULL)
1951 goto fail;
1952
1953 hapd_iface->config_fname = os_strdup(config_file);
1954 if (hapd_iface->config_fname == NULL)
1955 goto fail;
1956
1957 conf = interfaces->config_read_cb(hapd_iface->config_fname);
1958 if (conf == NULL)
1959 goto fail;
1960 hapd_iface->conf = conf;
1961
1962 hapd_iface->num_bss = conf->num_bss;
1963 hapd_iface->bss = os_calloc(conf->num_bss,
1964 sizeof(struct hostapd_data *));
1965 if (hapd_iface->bss == NULL)
1966 goto fail;
1967
1968 for (i = 0; i < conf->num_bss; i++) {
1969 hapd = hapd_iface->bss[i] =
1970 hostapd_alloc_bss_data(hapd_iface, conf,
1971 conf->bss[i]);
1972 if (hapd == NULL)
1973 goto fail;
1974 hapd->msg_ctx = hapd;
1975 }
1976
1977 return hapd_iface;
1978
1979fail:
1980 wpa_printf(MSG_ERROR, "Failed to set up interface with %s",
1981 config_file);
1982 if (conf)
1983 hostapd_config_free(conf);
1984 if (hapd_iface) {
1985 os_free(hapd_iface->config_fname);
1986 os_free(hapd_iface->bss);
Dmitry Shmidt54605472013-11-08 11:10:19 -08001987 wpa_printf(MSG_DEBUG, "%s: free iface %p",
1988 __func__, hapd_iface);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001989 os_free(hapd_iface);
1990 }
1991 return NULL;
1992}
1993
1994
1995static int ifname_in_use(struct hapd_interfaces *interfaces, const char *ifname)
1996{
1997 size_t i, j;
1998
1999 for (i = 0; i < interfaces->count; i++) {
2000 struct hostapd_iface *iface = interfaces->iface[i];
2001 for (j = 0; j < iface->num_bss; j++) {
2002 struct hostapd_data *hapd = iface->bss[j];
2003 if (os_strcmp(ifname, hapd->conf->iface) == 0)
2004 return 1;
2005 }
2006 }
2007
2008 return 0;
2009}
2010
2011
2012/**
2013 * hostapd_interface_init_bss - Read configuration file and init BSS data
2014 *
2015 * This function is used to parse configuration file for a BSS. This BSS is
2016 * added to an existing interface sharing the same radio (if any) or a new
2017 * interface is created if this is the first interface on a radio. This
2018 * allocate memory for the BSS. No actual driver operations are started.
2019 *
2020 * This is similar to hostapd_interface_init(), but for a case where the
2021 * configuration is used to add a single BSS instead of all BSSes for a radio.
2022 */
2023struct hostapd_iface *
2024hostapd_interface_init_bss(struct hapd_interfaces *interfaces, const char *phy,
2025 const char *config_fname, int debug)
2026{
2027 struct hostapd_iface *new_iface = NULL, *iface = NULL;
2028 struct hostapd_data *hapd;
2029 int k;
2030 size_t i, bss_idx;
2031
2032 if (!phy || !*phy)
2033 return NULL;
2034
2035 for (i = 0; i < interfaces->count; i++) {
2036 if (os_strcmp(interfaces->iface[i]->phy, phy) == 0) {
2037 iface = interfaces->iface[i];
2038 break;
2039 }
2040 }
2041
2042 wpa_printf(MSG_INFO, "Configuration file: %s (phy %s)%s",
2043 config_fname, phy, iface ? "" : " --> new PHY");
2044 if (iface) {
2045 struct hostapd_config *conf;
2046 struct hostapd_bss_config **tmp_conf;
2047 struct hostapd_data **tmp_bss;
2048 struct hostapd_bss_config *bss;
2049 const char *ifname;
2050
2051 /* Add new BSS to existing iface */
2052 conf = interfaces->config_read_cb(config_fname);
2053 if (conf == NULL)
2054 return NULL;
2055 if (conf->num_bss > 1) {
2056 wpa_printf(MSG_ERROR, "Multiple BSSes specified in BSS-config");
2057 hostapd_config_free(conf);
2058 return NULL;
2059 }
2060
2061 ifname = conf->bss[0]->iface;
2062 if (ifname[0] != '\0' && ifname_in_use(interfaces, ifname)) {
2063 wpa_printf(MSG_ERROR,
2064 "Interface name %s already in use", ifname);
2065 hostapd_config_free(conf);
2066 return NULL;
2067 }
2068
2069 tmp_conf = os_realloc_array(
2070 iface->conf->bss, iface->conf->num_bss + 1,
2071 sizeof(struct hostapd_bss_config *));
2072 tmp_bss = os_realloc_array(iface->bss, iface->num_bss + 1,
2073 sizeof(struct hostapd_data *));
2074 if (tmp_bss)
2075 iface->bss = tmp_bss;
2076 if (tmp_conf) {
2077 iface->conf->bss = tmp_conf;
2078 iface->conf->last_bss = tmp_conf[0];
2079 }
2080 if (tmp_bss == NULL || tmp_conf == NULL) {
2081 hostapd_config_free(conf);
2082 return NULL;
2083 }
2084 bss = iface->conf->bss[iface->conf->num_bss] = conf->bss[0];
2085 iface->conf->num_bss++;
2086
2087 hapd = hostapd_alloc_bss_data(iface, iface->conf, bss);
2088 if (hapd == NULL) {
2089 iface->conf->num_bss--;
2090 hostapd_config_free(conf);
2091 return NULL;
2092 }
2093 iface->conf->last_bss = bss;
2094 iface->bss[iface->num_bss] = hapd;
2095 hapd->msg_ctx = hapd;
2096
2097 bss_idx = iface->num_bss++;
2098 conf->num_bss--;
2099 conf->bss[0] = NULL;
2100 hostapd_config_free(conf);
2101 } else {
2102 /* Add a new iface with the first BSS */
2103 new_iface = iface = hostapd_init(interfaces, config_fname);
2104 if (!iface)
2105 return NULL;
2106 os_strlcpy(iface->phy, phy, sizeof(iface->phy));
2107 iface->interfaces = interfaces;
2108 bss_idx = 0;
2109 }
2110
2111 for (k = 0; k < debug; k++) {
2112 if (iface->bss[bss_idx]->conf->logger_stdout_level > 0)
2113 iface->bss[bss_idx]->conf->logger_stdout_level--;
2114 }
2115
2116 if (iface->conf->bss[bss_idx]->iface[0] == '\0' &&
2117 !hostapd_drv_none(iface->bss[bss_idx])) {
2118 wpa_printf(MSG_ERROR, "Interface name not specified in %s",
2119 config_fname);
2120 if (new_iface)
2121 hostapd_interface_deinit_free(new_iface);
2122 return NULL;
2123 }
2124
2125 return iface;
2126}
2127
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002128
2129void hostapd_interface_deinit_free(struct hostapd_iface *iface)
2130{
2131 const struct wpa_driver_ops *driver;
2132 void *drv_priv;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002133
2134 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002135 if (iface == NULL)
2136 return;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002137 wpa_printf(MSG_DEBUG, "%s: num_bss=%u conf->num_bss=%u",
2138 __func__, (unsigned int) iface->num_bss,
2139 (unsigned int) iface->conf->num_bss);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002140 driver = iface->bss[0]->driver;
2141 drv_priv = iface->bss[0]->drv_priv;
2142 hostapd_interface_deinit(iface);
Dmitry Shmidt54605472013-11-08 11:10:19 -08002143 wpa_printf(MSG_DEBUG, "%s: driver=%p drv_priv=%p -> hapd_deinit",
2144 __func__, driver, drv_priv);
Dmitry Shmidt71757432014-06-02 13:50:35 -07002145 if (driver && driver->hapd_deinit && drv_priv) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002146 driver->hapd_deinit(drv_priv);
Dmitry Shmidt71757432014-06-02 13:50:35 -07002147 iface->bss[0]->drv_priv = NULL;
2148 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002149 hostapd_interface_free(iface);
2150}
2151
2152
Dmitry Shmidt15907092014-03-25 10:42:57 -07002153static void hostapd_deinit_driver(const struct wpa_driver_ops *driver,
2154 void *drv_priv,
2155 struct hostapd_iface *hapd_iface)
2156{
2157 size_t j;
2158
2159 wpa_printf(MSG_DEBUG, "%s: driver=%p drv_priv=%p -> hapd_deinit",
2160 __func__, driver, drv_priv);
2161 if (driver && driver->hapd_deinit && drv_priv) {
2162 driver->hapd_deinit(drv_priv);
2163 for (j = 0; j < hapd_iface->num_bss; j++) {
2164 wpa_printf(MSG_DEBUG, "%s:bss[%d]->drv_priv=%p",
2165 __func__, (int) j,
2166 hapd_iface->bss[j]->drv_priv);
2167 if (hapd_iface->bss[j]->drv_priv == drv_priv)
2168 hapd_iface->bss[j]->drv_priv = NULL;
2169 }
2170 }
2171}
2172
2173
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002174int hostapd_enable_iface(struct hostapd_iface *hapd_iface)
2175{
Dmitry Shmidt71757432014-06-02 13:50:35 -07002176 size_t j;
2177
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002178 if (hapd_iface->bss[0]->drv_priv != NULL) {
2179 wpa_printf(MSG_ERROR, "Interface %s already enabled",
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002180 hapd_iface->conf->bss[0]->iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002181 return -1;
2182 }
2183
2184 wpa_printf(MSG_DEBUG, "Enable interface %s",
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002185 hapd_iface->conf->bss[0]->iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002186
Dmitry Shmidt71757432014-06-02 13:50:35 -07002187 for (j = 0; j < hapd_iface->num_bss; j++)
2188 hostapd_set_security_params(hapd_iface->conf->bss[j], 1);
Dmitry Shmidt344abd32014-01-14 13:17:00 -08002189 if (hostapd_config_check(hapd_iface->conf, 1) < 0) {
2190 wpa_printf(MSG_INFO, "Invalid configuration - cannot enable");
2191 return -1;
2192 }
2193
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002194 if (hapd_iface->interfaces == NULL ||
2195 hapd_iface->interfaces->driver_init == NULL ||
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002196 hapd_iface->interfaces->driver_init(hapd_iface))
2197 return -1;
2198
2199 if (hostapd_setup_interface(hapd_iface)) {
Dmitry Shmidt15907092014-03-25 10:42:57 -07002200 hostapd_deinit_driver(hapd_iface->bss[0]->driver,
2201 hapd_iface->bss[0]->drv_priv,
2202 hapd_iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002203 return -1;
2204 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002205
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002206 return 0;
2207}
2208
2209
2210int hostapd_reload_iface(struct hostapd_iface *hapd_iface)
2211{
2212 size_t j;
2213
2214 wpa_printf(MSG_DEBUG, "Reload interface %s",
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002215 hapd_iface->conf->bss[0]->iface);
2216 for (j = 0; j < hapd_iface->num_bss; j++)
Dmitry Shmidt71757432014-06-02 13:50:35 -07002217 hostapd_set_security_params(hapd_iface->conf->bss[j], 1);
Dmitry Shmidt344abd32014-01-14 13:17:00 -08002218 if (hostapd_config_check(hapd_iface->conf, 1) < 0) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002219 wpa_printf(MSG_ERROR, "Updated configuration is invalid");
2220 return -1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002221 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002222 hostapd_clear_old(hapd_iface);
2223 for (j = 0; j < hapd_iface->num_bss; j++)
2224 hostapd_reload_bss(hapd_iface->bss[j]);
2225
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002226 return 0;
2227}
2228
2229
2230int hostapd_disable_iface(struct hostapd_iface *hapd_iface)
2231{
2232 size_t j;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002233 const struct wpa_driver_ops *driver;
2234 void *drv_priv;
2235
2236 if (hapd_iface == NULL)
2237 return -1;
Dmitry Shmidt71757432014-06-02 13:50:35 -07002238
2239 if (hapd_iface->bss[0]->drv_priv == NULL) {
2240 wpa_printf(MSG_INFO, "Interface %s already disabled",
2241 hapd_iface->conf->bss[0]->iface);
2242 return -1;
2243 }
2244
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002245 wpa_msg(hapd_iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002246 driver = hapd_iface->bss[0]->driver;
2247 drv_priv = hapd_iface->bss[0]->drv_priv;
2248
Dmitry Shmidta38abf92014-03-06 13:38:44 -08002249 hapd_iface->driver_ap_teardown =
2250 !!(hapd_iface->drv_flags &
2251 WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
2252
2253 /* same as hostapd_interface_deinit without deinitializing ctrl-iface */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002254 for (j = 0; j < hapd_iface->num_bss; j++) {
2255 struct hostapd_data *hapd = hapd_iface->bss[j];
Dmitry Shmidt71757432014-06-02 13:50:35 -07002256 hostapd_bss_deinit_no_free(hapd);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002257 hostapd_free_hapd_data(hapd);
2258 }
2259
Dmitry Shmidt15907092014-03-25 10:42:57 -07002260 hostapd_deinit_driver(driver, drv_priv, hapd_iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002261
2262 /* From hostapd_cleanup_iface: These were initialized in
2263 * hostapd_setup_interface and hostapd_setup_interface_complete
2264 */
2265 hostapd_cleanup_iface_partial(hapd_iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002266
Dmitry Shmidt56052862013-10-04 10:23:25 -07002267 wpa_printf(MSG_DEBUG, "Interface %s disabled",
2268 hapd_iface->bss[0]->conf->iface);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002269 hostapd_set_state(hapd_iface, HAPD_IFACE_DISABLED);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002270 return 0;
2271}
2272
2273
2274static struct hostapd_iface *
2275hostapd_iface_alloc(struct hapd_interfaces *interfaces)
2276{
2277 struct hostapd_iface **iface, *hapd_iface;
2278
2279 iface = os_realloc_array(interfaces->iface, interfaces->count + 1,
2280 sizeof(struct hostapd_iface *));
2281 if (iface == NULL)
2282 return NULL;
2283 interfaces->iface = iface;
2284 hapd_iface = interfaces->iface[interfaces->count] =
2285 os_zalloc(sizeof(*hapd_iface));
2286 if (hapd_iface == NULL) {
2287 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory for "
2288 "the interface", __func__);
2289 return NULL;
2290 }
2291 interfaces->count++;
2292 hapd_iface->interfaces = interfaces;
2293
2294 return hapd_iface;
2295}
2296
2297
2298static struct hostapd_config *
2299hostapd_config_alloc(struct hapd_interfaces *interfaces, const char *ifname,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002300 const char *ctrl_iface, const char *driver)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002301{
2302 struct hostapd_bss_config *bss;
2303 struct hostapd_config *conf;
2304
2305 /* Allocates memory for bss and conf */
2306 conf = hostapd_config_defaults();
2307 if (conf == NULL) {
2308 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory for "
2309 "configuration", __func__);
2310 return NULL;
2311 }
2312
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002313 if (driver) {
2314 int j;
2315
2316 for (j = 0; wpa_drivers[j]; j++) {
2317 if (os_strcmp(driver, wpa_drivers[j]->name) == 0) {
2318 conf->driver = wpa_drivers[j];
2319 goto skip;
2320 }
2321 }
2322
2323 wpa_printf(MSG_ERROR,
2324 "Invalid/unknown driver '%s' - registering the default driver",
2325 driver);
2326 }
2327
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002328 conf->driver = wpa_drivers[0];
2329 if (conf->driver == NULL) {
2330 wpa_printf(MSG_ERROR, "No driver wrappers registered!");
2331 hostapd_config_free(conf);
2332 return NULL;
2333 }
2334
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002335skip:
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002336 bss = conf->last_bss = conf->bss[0];
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002337
2338 os_strlcpy(bss->iface, ifname, sizeof(bss->iface));
2339 bss->ctrl_interface = os_strdup(ctrl_iface);
2340 if (bss->ctrl_interface == NULL) {
2341 hostapd_config_free(conf);
2342 return NULL;
2343 }
2344
2345 /* Reading configuration file skipped, will be done in SET!
2346 * From reading the configuration till the end has to be done in
2347 * SET
2348 */
2349 return conf;
2350}
2351
2352
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002353static int hostapd_data_alloc(struct hostapd_iface *hapd_iface,
2354 struct hostapd_config *conf)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002355{
2356 size_t i;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002357 struct hostapd_data *hapd;
2358
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002359 hapd_iface->bss = os_calloc(conf->num_bss,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002360 sizeof(struct hostapd_data *));
2361 if (hapd_iface->bss == NULL)
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002362 return -1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002363
2364 for (i = 0; i < conf->num_bss; i++) {
2365 hapd = hapd_iface->bss[i] =
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002366 hostapd_alloc_bss_data(hapd_iface, conf, conf->bss[i]);
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002367 if (hapd == NULL) {
2368 while (i > 0) {
2369 i--;
2370 os_free(hapd_iface->bss[i]);
2371 hapd_iface->bss[i] = NULL;
2372 }
2373 os_free(hapd_iface->bss);
2374 hapd_iface->bss = NULL;
2375 return -1;
2376 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002377 hapd->msg_ctx = hapd;
2378 }
2379
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002380 hapd_iface->conf = conf;
2381 hapd_iface->num_bss = conf->num_bss;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002382
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002383 return 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002384}
2385
2386
2387int hostapd_add_iface(struct hapd_interfaces *interfaces, char *buf)
2388{
2389 struct hostapd_config *conf = NULL;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002390 struct hostapd_iface *hapd_iface = NULL, *new_iface = NULL;
2391 struct hostapd_data *hapd;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002392 char *ptr;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002393 size_t i, j;
2394 const char *conf_file = NULL, *phy_name = NULL;
2395
2396 if (os_strncmp(buf, "bss_config=", 11) == 0) {
2397 char *pos;
2398 phy_name = buf + 11;
2399 pos = os_strchr(phy_name, ':');
2400 if (!pos)
2401 return -1;
2402 *pos++ = '\0';
2403 conf_file = pos;
2404 if (!os_strlen(conf_file))
2405 return -1;
2406
2407 hapd_iface = hostapd_interface_init_bss(interfaces, phy_name,
2408 conf_file, 0);
2409 if (!hapd_iface)
2410 return -1;
2411 for (j = 0; j < interfaces->count; j++) {
2412 if (interfaces->iface[j] == hapd_iface)
2413 break;
2414 }
2415 if (j == interfaces->count) {
2416 struct hostapd_iface **tmp;
2417 tmp = os_realloc_array(interfaces->iface,
2418 interfaces->count + 1,
2419 sizeof(struct hostapd_iface *));
2420 if (!tmp) {
2421 hostapd_interface_deinit_free(hapd_iface);
2422 return -1;
2423 }
2424 interfaces->iface = tmp;
2425 interfaces->iface[interfaces->count++] = hapd_iface;
2426 new_iface = hapd_iface;
2427 }
2428
2429 if (new_iface) {
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002430 if (interfaces->driver_init(hapd_iface))
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002431 goto fail;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002432
2433 if (hostapd_setup_interface(hapd_iface)) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002434 hostapd_deinit_driver(
2435 hapd_iface->bss[0]->driver,
2436 hapd_iface->bss[0]->drv_priv,
2437 hapd_iface);
2438 goto fail;
2439 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002440 } else {
2441 /* Assign new BSS with bss[0]'s driver info */
2442 hapd = hapd_iface->bss[hapd_iface->num_bss - 1];
2443 hapd->driver = hapd_iface->bss[0]->driver;
2444 hapd->drv_priv = hapd_iface->bss[0]->drv_priv;
2445 os_memcpy(hapd->own_addr, hapd_iface->bss[0]->own_addr,
2446 ETH_ALEN);
2447
2448 if (start_ctrl_iface_bss(hapd) < 0 ||
Dmitry Shmidt54605472013-11-08 11:10:19 -08002449 (hapd_iface->state == HAPD_IFACE_ENABLED &&
2450 hostapd_setup_bss(hapd, -1))) {
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002451 hostapd_cleanup(hapd);
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08002452 hapd_iface->bss[hapd_iface->num_bss - 1] = NULL;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002453 hapd_iface->conf->num_bss--;
2454 hapd_iface->num_bss--;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002455 wpa_printf(MSG_DEBUG, "%s: free hapd %p %s",
2456 __func__, hapd, hapd->conf->iface);
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002457 hostapd_config_free_bss(hapd->conf);
2458 hapd->conf = NULL;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002459 os_free(hapd);
2460 return -1;
2461 }
2462 }
2463 return 0;
2464 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002465
2466 ptr = os_strchr(buf, ' ');
2467 if (ptr == NULL)
2468 return -1;
2469 *ptr++ = '\0';
2470
Dmitry Shmidt56052862013-10-04 10:23:25 -07002471 if (os_strncmp(ptr, "config=", 7) == 0)
2472 conf_file = ptr + 7;
2473
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002474 for (i = 0; i < interfaces->count; i++) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002475 if (!os_strcmp(interfaces->iface[i]->conf->bss[0]->iface,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002476 buf)) {
2477 wpa_printf(MSG_INFO, "Cannot add interface - it "
2478 "already exists");
2479 return -1;
2480 }
2481 }
2482
2483 hapd_iface = hostapd_iface_alloc(interfaces);
2484 if (hapd_iface == NULL) {
2485 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
2486 "for interface", __func__);
2487 goto fail;
2488 }
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002489 new_iface = hapd_iface;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002490
Dmitry Shmidt56052862013-10-04 10:23:25 -07002491 if (conf_file && interfaces->config_read_cb) {
2492 conf = interfaces->config_read_cb(conf_file);
2493 if (conf && conf->bss)
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002494 os_strlcpy(conf->bss[0]->iface, buf,
2495 sizeof(conf->bss[0]->iface));
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002496 } else {
2497 char *driver = os_strchr(ptr, ' ');
2498
2499 if (driver)
2500 *driver++ = '\0';
2501 conf = hostapd_config_alloc(interfaces, buf, ptr, driver);
2502 }
2503
Dmitry Shmidt56052862013-10-04 10:23:25 -07002504 if (conf == NULL || conf->bss == NULL) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002505 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
2506 "for configuration", __func__);
2507 goto fail;
2508 }
2509
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002510 if (hostapd_data_alloc(hapd_iface, conf) < 0) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002511 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
2512 "for hostapd", __func__);
2513 goto fail;
2514 }
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002515 conf = NULL;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002516
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002517 if (start_ctrl_iface(hapd_iface) < 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002518 goto fail;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002519
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002520 wpa_printf(MSG_INFO, "Add interface '%s'",
2521 hapd_iface->conf->bss[0]->iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002522
2523 return 0;
2524
2525fail:
2526 if (conf)
2527 hostapd_config_free(conf);
2528 if (hapd_iface) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002529 if (hapd_iface->bss) {
Dmitry Shmidt54605472013-11-08 11:10:19 -08002530 for (i = 0; i < hapd_iface->num_bss; i++) {
2531 hapd = hapd_iface->bss[i];
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08002532 if (!hapd)
2533 continue;
2534 if (hapd_iface->interfaces &&
Dmitry Shmidt54605472013-11-08 11:10:19 -08002535 hapd_iface->interfaces->ctrl_iface_deinit)
2536 hapd_iface->interfaces->
2537 ctrl_iface_deinit(hapd);
2538 wpa_printf(MSG_DEBUG, "%s: free hapd %p (%s)",
2539 __func__, hapd_iface->bss[i],
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08002540 hapd->conf->iface);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002541 hostapd_cleanup(hapd);
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08002542 os_free(hapd);
2543 hapd_iface->bss[i] = NULL;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002544 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002545 os_free(hapd_iface->bss);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002546 hapd_iface->bss = NULL;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002547 }
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002548 if (new_iface) {
2549 interfaces->count--;
2550 interfaces->iface[interfaces->count] = NULL;
2551 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002552 hostapd_cleanup_iface(hapd_iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002553 }
2554 return -1;
2555}
2556
2557
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002558static int hostapd_remove_bss(struct hostapd_iface *iface, unsigned int idx)
2559{
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002560 size_t i;
2561
Dmitry Shmidt54605472013-11-08 11:10:19 -08002562 wpa_printf(MSG_INFO, "Remove BSS '%s'", iface->conf->bss[idx]->iface);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002563
Dmitry Shmidt54605472013-11-08 11:10:19 -08002564 /* Remove hostapd_data only if it has already been initialized */
2565 if (idx < iface->num_bss) {
2566 struct hostapd_data *hapd = iface->bss[idx];
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002567
Dmitry Shmidt54605472013-11-08 11:10:19 -08002568 hostapd_bss_deinit(hapd);
2569 wpa_printf(MSG_DEBUG, "%s: free hapd %p (%s)",
2570 __func__, hapd, hapd->conf->iface);
2571 hostapd_config_free_bss(hapd->conf);
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002572 hapd->conf = NULL;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002573 os_free(hapd);
2574
2575 iface->num_bss--;
2576
2577 for (i = idx; i < iface->num_bss; i++)
2578 iface->bss[i] = iface->bss[i + 1];
2579 } else {
2580 hostapd_config_free_bss(iface->conf->bss[idx]);
2581 iface->conf->bss[idx] = NULL;
2582 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002583
2584 iface->conf->num_bss--;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002585 for (i = idx; i < iface->conf->num_bss; i++)
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002586 iface->conf->bss[i] = iface->conf->bss[i + 1];
2587
2588 return 0;
2589}
2590
2591
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002592int hostapd_remove_iface(struct hapd_interfaces *interfaces, char *buf)
2593{
2594 struct hostapd_iface *hapd_iface;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002595 size_t i, j, k = 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002596
2597 for (i = 0; i < interfaces->count; i++) {
2598 hapd_iface = interfaces->iface[i];
2599 if (hapd_iface == NULL)
2600 return -1;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002601 if (!os_strcmp(hapd_iface->conf->bss[0]->iface, buf)) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002602 wpa_printf(MSG_INFO, "Remove interface '%s'", buf);
Dmitry Shmidta38abf92014-03-06 13:38:44 -08002603 hapd_iface->driver_ap_teardown =
2604 !!(hapd_iface->drv_flags &
2605 WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
2606
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002607 hostapd_interface_deinit_free(hapd_iface);
2608 k = i;
2609 while (k < (interfaces->count - 1)) {
2610 interfaces->iface[k] =
2611 interfaces->iface[k + 1];
2612 k++;
2613 }
2614 interfaces->count--;
2615 return 0;
2616 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002617
2618 for (j = 0; j < hapd_iface->conf->num_bss; j++) {
Dmitry Shmidta38abf92014-03-06 13:38:44 -08002619 if (!os_strcmp(hapd_iface->conf->bss[j]->iface, buf)) {
2620 hapd_iface->driver_ap_teardown =
2621 !(hapd_iface->drv_flags &
2622 WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002623 return hostapd_remove_bss(hapd_iface, j);
Dmitry Shmidta38abf92014-03-06 13:38:44 -08002624 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002625 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002626 }
2627 return -1;
2628}
2629
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002630
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002631/**
2632 * hostapd_new_assoc_sta - Notify that a new station associated with the AP
2633 * @hapd: Pointer to BSS data
2634 * @sta: Pointer to the associated STA data
2635 * @reassoc: 1 to indicate this was a re-association; 0 = first association
2636 *
2637 * This function will be called whenever a station associates with the AP. It
2638 * can be called from ieee802_11.c for drivers that export MLME to hostapd and
2639 * from drv_callbacks.c based on driver events for drivers that take care of
2640 * management frames (IEEE 802.11 authentication and association) internally.
2641 */
2642void hostapd_new_assoc_sta(struct hostapd_data *hapd, struct sta_info *sta,
2643 int reassoc)
2644{
2645 if (hapd->tkip_countermeasures) {
2646 hostapd_drv_sta_deauth(hapd, sta->addr,
2647 WLAN_REASON_MICHAEL_MIC_FAILURE);
2648 return;
2649 }
2650
2651 hostapd_prune_associations(hapd, sta->addr);
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -08002652 ap_sta_clear_disconnect_timeouts(hapd, sta);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002653
2654 /* IEEE 802.11F (IAPP) */
2655 if (hapd->conf->ieee802_11f)
2656 iapp_new_station(hapd->iapp, sta);
2657
2658#ifdef CONFIG_P2P
2659 if (sta->p2p_ie == NULL && !sta->no_p2p_set) {
2660 sta->no_p2p_set = 1;
2661 hapd->num_sta_no_p2p++;
2662 if (hapd->num_sta_no_p2p == 1)
2663 hostapd_p2p_non_p2p_sta_connected(hapd);
2664 }
2665#endif /* CONFIG_P2P */
2666
2667 /* Start accounting here, if IEEE 802.1X and WPA are not used.
2668 * IEEE 802.1X/WPA code will start accounting after the station has
2669 * been authorized. */
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -08002670 if (!hapd->conf->ieee802_1x && !hapd->conf->wpa && !hapd->conf->osen) {
2671 ap_sta_set_authorized(hapd, sta, 1);
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002672 os_get_reltime(&sta->connected_time);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002673 accounting_sta_start(hapd, sta);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002674 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002675
2676 /* Start IEEE 802.1X authentication process for new stations */
2677 ieee802_1x_new_station(hapd, sta);
2678 if (reassoc) {
2679 if (sta->auth_alg != WLAN_AUTH_FT &&
2680 !(sta->flags & (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS)))
2681 wpa_auth_sm_event(sta->wpa_sm, WPA_REAUTH);
2682 } else
2683 wpa_auth_sta_associated(hapd->wpa_auth, sta->wpa_sm);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002684
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08002685 if (!(hapd->iface->drv_flags & WPA_DRIVER_FLAGS_INACTIVITY_TIMER)) {
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -08002686 wpa_printf(MSG_DEBUG,
2687 "%s: %s: reschedule ap_handle_timer timeout for "
2688 MACSTR " (%d seconds - ap_max_inactivity)",
2689 hapd->conf->iface, __func__, MAC2STR(sta->addr),
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08002690 hapd->conf->ap_max_inactivity);
2691 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
2692 eloop_register_timeout(hapd->conf->ap_max_inactivity, 0,
2693 ap_handle_timer, hapd, sta);
2694 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002695}
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002696
2697
2698const char * hostapd_state_text(enum hostapd_iface_state s)
2699{
2700 switch (s) {
2701 case HAPD_IFACE_UNINITIALIZED:
2702 return "UNINITIALIZED";
2703 case HAPD_IFACE_DISABLED:
2704 return "DISABLED";
2705 case HAPD_IFACE_COUNTRY_UPDATE:
2706 return "COUNTRY_UPDATE";
2707 case HAPD_IFACE_ACS:
2708 return "ACS";
2709 case HAPD_IFACE_HT_SCAN:
2710 return "HT_SCAN";
2711 case HAPD_IFACE_DFS:
2712 return "DFS";
2713 case HAPD_IFACE_ENABLED:
2714 return "ENABLED";
2715 }
2716
2717 return "UNKNOWN";
2718}
2719
2720
2721void hostapd_set_state(struct hostapd_iface *iface, enum hostapd_iface_state s)
2722{
2723 wpa_printf(MSG_INFO, "%s: interface state %s->%s",
2724 iface->conf->bss[0]->iface, hostapd_state_text(iface->state),
2725 hostapd_state_text(s));
2726 iface->state = s;
2727}
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002728
2729
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002730int hostapd_csa_in_progress(struct hostapd_iface *iface)
2731{
2732 unsigned int i;
2733
2734 for (i = 0; i < iface->num_bss; i++)
2735 if (iface->bss[i]->csa_in_progress)
2736 return 1;
2737 return 0;
2738}
2739
2740
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002741#ifdef NEED_AP_MLME
2742
2743static void free_beacon_data(struct beacon_data *beacon)
2744{
2745 os_free(beacon->head);
2746 beacon->head = NULL;
2747 os_free(beacon->tail);
2748 beacon->tail = NULL;
2749 os_free(beacon->probe_resp);
2750 beacon->probe_resp = NULL;
2751 os_free(beacon->beacon_ies);
2752 beacon->beacon_ies = NULL;
2753 os_free(beacon->proberesp_ies);
2754 beacon->proberesp_ies = NULL;
2755 os_free(beacon->assocresp_ies);
2756 beacon->assocresp_ies = NULL;
2757}
2758
2759
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07002760static int hostapd_build_beacon_data(struct hostapd_data *hapd,
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002761 struct beacon_data *beacon)
2762{
2763 struct wpabuf *beacon_extra, *proberesp_extra, *assocresp_extra;
2764 struct wpa_driver_ap_params params;
2765 int ret;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002766
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08002767 os_memset(beacon, 0, sizeof(*beacon));
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002768 ret = ieee802_11_build_ap_params(hapd, &params);
2769 if (ret < 0)
2770 return ret;
2771
2772 ret = hostapd_build_ap_extra_ies(hapd, &beacon_extra,
2773 &proberesp_extra,
2774 &assocresp_extra);
2775 if (ret)
2776 goto free_ap_params;
2777
2778 ret = -1;
2779 beacon->head = os_malloc(params.head_len);
2780 if (!beacon->head)
2781 goto free_ap_extra_ies;
2782
2783 os_memcpy(beacon->head, params.head, params.head_len);
2784 beacon->head_len = params.head_len;
2785
2786 beacon->tail = os_malloc(params.tail_len);
2787 if (!beacon->tail)
2788 goto free_beacon;
2789
2790 os_memcpy(beacon->tail, params.tail, params.tail_len);
2791 beacon->tail_len = params.tail_len;
2792
2793 if (params.proberesp != NULL) {
2794 beacon->probe_resp = os_malloc(params.proberesp_len);
2795 if (!beacon->probe_resp)
2796 goto free_beacon;
2797
2798 os_memcpy(beacon->probe_resp, params.proberesp,
2799 params.proberesp_len);
2800 beacon->probe_resp_len = params.proberesp_len;
2801 }
2802
2803 /* copy the extra ies */
2804 if (beacon_extra) {
2805 beacon->beacon_ies = os_malloc(wpabuf_len(beacon_extra));
2806 if (!beacon->beacon_ies)
2807 goto free_beacon;
2808
2809 os_memcpy(beacon->beacon_ies,
2810 beacon_extra->buf, wpabuf_len(beacon_extra));
2811 beacon->beacon_ies_len = wpabuf_len(beacon_extra);
2812 }
2813
2814 if (proberesp_extra) {
2815 beacon->proberesp_ies =
2816 os_malloc(wpabuf_len(proberesp_extra));
2817 if (!beacon->proberesp_ies)
2818 goto free_beacon;
2819
2820 os_memcpy(beacon->proberesp_ies, proberesp_extra->buf,
2821 wpabuf_len(proberesp_extra));
2822 beacon->proberesp_ies_len = wpabuf_len(proberesp_extra);
2823 }
2824
2825 if (assocresp_extra) {
2826 beacon->assocresp_ies =
2827 os_malloc(wpabuf_len(assocresp_extra));
2828 if (!beacon->assocresp_ies)
2829 goto free_beacon;
2830
2831 os_memcpy(beacon->assocresp_ies, assocresp_extra->buf,
2832 wpabuf_len(assocresp_extra));
2833 beacon->assocresp_ies_len = wpabuf_len(assocresp_extra);
2834 }
2835
2836 ret = 0;
2837free_beacon:
2838 /* if the function fails, the caller should not free beacon data */
2839 if (ret)
2840 free_beacon_data(beacon);
2841
2842free_ap_extra_ies:
2843 hostapd_free_ap_extra_ies(hapd, beacon_extra, proberesp_extra,
2844 assocresp_extra);
2845free_ap_params:
2846 ieee802_11_free_ap_params(&params);
2847 return ret;
2848}
2849
2850
2851/*
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002852 * TODO: This flow currently supports only changing channel and width within
2853 * the same hw_mode. Any other changes to MAC parameters or provided settings
2854 * are not supported.
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002855 */
2856static int hostapd_change_config_freq(struct hostapd_data *hapd,
2857 struct hostapd_config *conf,
2858 struct hostapd_freq_params *params,
2859 struct hostapd_freq_params *old_params)
2860{
2861 int channel;
2862
2863 if (!params->channel) {
2864 /* check if the new channel is supported by hw */
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07002865 params->channel = hostapd_hw_get_channel(hapd, params->freq);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002866 }
2867
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07002868 channel = params->channel;
2869 if (!channel)
2870 return -1;
2871
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002872 /* if a pointer to old_params is provided we save previous state */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002873 if (old_params &&
2874 hostapd_set_freq_params(old_params, conf->hw_mode,
2875 hostapd_hw_get_freq(hapd, conf->channel),
2876 conf->channel, conf->ieee80211n,
2877 conf->ieee80211ac,
2878 conf->secondary_channel,
2879 conf->vht_oper_chwidth,
2880 conf->vht_oper_centr_freq_seg0_idx,
2881 conf->vht_oper_centr_freq_seg1_idx,
2882 conf->vht_capab))
2883 return -1;
2884
2885 switch (params->bandwidth) {
2886 case 0:
2887 case 20:
2888 case 40:
2889 conf->vht_oper_chwidth = VHT_CHANWIDTH_USE_HT;
2890 break;
2891 case 80:
2892 if (params->center_freq2)
2893 conf->vht_oper_chwidth = VHT_CHANWIDTH_80P80MHZ;
2894 else
2895 conf->vht_oper_chwidth = VHT_CHANWIDTH_80MHZ;
2896 break;
2897 case 160:
2898 conf->vht_oper_chwidth = VHT_CHANWIDTH_160MHZ;
2899 break;
2900 default:
2901 return -1;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002902 }
2903
2904 conf->channel = channel;
2905 conf->ieee80211n = params->ht_enabled;
2906 conf->secondary_channel = params->sec_channel_offset;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002907 ieee80211_freq_to_chan(params->center_freq1,
2908 &conf->vht_oper_centr_freq_seg0_idx);
2909 ieee80211_freq_to_chan(params->center_freq2,
2910 &conf->vht_oper_centr_freq_seg1_idx);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002911
2912 /* TODO: maybe call here hostapd_config_check here? */
2913
2914 return 0;
2915}
2916
2917
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07002918static int hostapd_fill_csa_settings(struct hostapd_data *hapd,
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002919 struct csa_settings *settings)
2920{
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07002921 struct hostapd_iface *iface = hapd->iface;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002922 struct hostapd_freq_params old_freq;
2923 int ret;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002924 u8 chan, vht_bandwidth;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002925
2926 os_memset(&old_freq, 0, sizeof(old_freq));
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07002927 if (!iface || !iface->freq || hapd->csa_in_progress)
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002928 return -1;
2929
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002930 switch (settings->freq_params.bandwidth) {
2931 case 80:
2932 if (settings->freq_params.center_freq2)
2933 vht_bandwidth = VHT_CHANWIDTH_80P80MHZ;
2934 else
2935 vht_bandwidth = VHT_CHANWIDTH_80MHZ;
2936 break;
2937 case 160:
2938 vht_bandwidth = VHT_CHANWIDTH_160MHZ;
2939 break;
2940 default:
2941 vht_bandwidth = VHT_CHANWIDTH_USE_HT;
2942 break;
2943 }
2944
2945 if (ieee80211_freq_to_channel_ext(
2946 settings->freq_params.freq,
2947 settings->freq_params.sec_channel_offset,
2948 vht_bandwidth,
2949 &hapd->iface->cs_oper_class,
2950 &chan) == NUM_HOSTAPD_MODES) {
2951 wpa_printf(MSG_DEBUG,
2952 "invalid frequency for channel switch (freq=%d, sec_channel_offset=%d, vht_enabled=%d)",
2953 settings->freq_params.freq,
2954 settings->freq_params.sec_channel_offset,
2955 settings->freq_params.vht_enabled);
2956 return -1;
2957 }
2958
2959 settings->freq_params.channel = chan;
2960
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002961 ret = hostapd_change_config_freq(iface->bss[0], iface->conf,
2962 &settings->freq_params,
2963 &old_freq);
2964 if (ret)
2965 return ret;
2966
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07002967 ret = hostapd_build_beacon_data(hapd, &settings->beacon_after);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002968
2969 /* change back the configuration */
2970 hostapd_change_config_freq(iface->bss[0], iface->conf,
2971 &old_freq, NULL);
2972
2973 if (ret)
2974 return ret;
2975
2976 /* set channel switch parameters for csa ie */
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07002977 hapd->cs_freq_params = settings->freq_params;
2978 hapd->cs_count = settings->cs_count;
2979 hapd->cs_block_tx = settings->block_tx;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002980
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07002981 ret = hostapd_build_beacon_data(hapd, &settings->beacon_csa);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002982 if (ret) {
2983 free_beacon_data(&settings->beacon_after);
2984 return ret;
2985 }
2986
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002987 settings->counter_offset_beacon[0] = hapd->cs_c_off_beacon;
2988 settings->counter_offset_presp[0] = hapd->cs_c_off_proberesp;
2989 settings->counter_offset_beacon[1] = hapd->cs_c_off_ecsa_beacon;
2990 settings->counter_offset_presp[1] = hapd->cs_c_off_ecsa_proberesp;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002991
2992 return 0;
2993}
2994
2995
2996void hostapd_cleanup_cs_params(struct hostapd_data *hapd)
2997{
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07002998 os_memset(&hapd->cs_freq_params, 0, sizeof(hapd->cs_freq_params));
2999 hapd->cs_count = 0;
3000 hapd->cs_block_tx = 0;
3001 hapd->cs_c_off_beacon = 0;
3002 hapd->cs_c_off_proberesp = 0;
3003 hapd->csa_in_progress = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003004 hapd->cs_c_off_ecsa_beacon = 0;
3005 hapd->cs_c_off_ecsa_proberesp = 0;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003006}
3007
3008
3009int hostapd_switch_channel(struct hostapd_data *hapd,
3010 struct csa_settings *settings)
3011{
3012 int ret;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003013
3014 if (!(hapd->iface->drv_flags & WPA_DRIVER_FLAGS_AP_CSA)) {
3015 wpa_printf(MSG_INFO, "CSA is not supported");
3016 return -1;
3017 }
3018
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003019 ret = hostapd_fill_csa_settings(hapd, settings);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003020 if (ret)
3021 return ret;
3022
3023 ret = hostapd_drv_switch_channel(hapd, settings);
3024 free_beacon_data(&settings->beacon_csa);
3025 free_beacon_data(&settings->beacon_after);
3026
3027 if (ret) {
3028 /* if we failed, clean cs parameters */
3029 hostapd_cleanup_cs_params(hapd);
3030 return ret;
3031 }
3032
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003033 hapd->csa_in_progress = 1;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003034 return 0;
3035}
3036
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003037
3038void
3039hostapd_switch_channel_fallback(struct hostapd_iface *iface,
3040 const struct hostapd_freq_params *freq_params)
3041{
3042 int vht_seg0_idx = 0, vht_seg1_idx = 0, vht_bw = VHT_CHANWIDTH_USE_HT;
3043 unsigned int i;
3044
3045 wpa_printf(MSG_DEBUG, "Restarting all CSA-related BSSes");
3046
3047 if (freq_params->center_freq1)
3048 vht_seg0_idx = 36 + (freq_params->center_freq1 - 5180) / 5;
3049 if (freq_params->center_freq2)
3050 vht_seg1_idx = 36 + (freq_params->center_freq2 - 5180) / 5;
3051
3052 switch (freq_params->bandwidth) {
3053 case 0:
3054 case 20:
3055 case 40:
3056 vht_bw = VHT_CHANWIDTH_USE_HT;
3057 break;
3058 case 80:
3059 if (freq_params->center_freq2)
3060 vht_bw = VHT_CHANWIDTH_80P80MHZ;
3061 else
3062 vht_bw = VHT_CHANWIDTH_80MHZ;
3063 break;
3064 case 160:
3065 vht_bw = VHT_CHANWIDTH_160MHZ;
3066 break;
3067 default:
3068 wpa_printf(MSG_WARNING, "Unknown CSA bandwidth: %d",
3069 freq_params->bandwidth);
3070 break;
3071 }
3072
3073 iface->freq = freq_params->freq;
3074 iface->conf->channel = freq_params->channel;
3075 iface->conf->secondary_channel = freq_params->sec_channel_offset;
3076 iface->conf->vht_oper_centr_freq_seg0_idx = vht_seg0_idx;
3077 iface->conf->vht_oper_centr_freq_seg1_idx = vht_seg1_idx;
3078 iface->conf->vht_oper_chwidth = vht_bw;
3079 iface->conf->ieee80211n = freq_params->ht_enabled;
3080 iface->conf->ieee80211ac = freq_params->vht_enabled;
3081
3082 /*
3083 * cs_params must not be cleared earlier because the freq_params
3084 * argument may actually point to one of these.
3085 */
3086 for (i = 0; i < iface->num_bss; i++)
3087 hostapd_cleanup_cs_params(iface->bss[i]);
3088
3089 hostapd_disable_iface(iface);
3090 hostapd_enable_iface(iface);
3091}
3092
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003093
3094struct hostapd_data * hostapd_get_iface(struct hapd_interfaces *interfaces,
3095 const char *ifname)
3096{
3097 size_t i, j;
3098
3099 for (i = 0; i < interfaces->count; i++) {
3100 struct hostapd_iface *iface = interfaces->iface[i];
3101
3102 for (j = 0; j < iface->num_bss; j++) {
3103 struct hostapd_data *hapd = iface->bss[j];
3104
3105 if (os_strcmp(ifname, hapd->conf->iface) == 0)
3106 return hapd;
3107 }
3108 }
3109
3110 return NULL;
3111}
3112
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003113#endif /* NEED_AP_MLME */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003114
3115
3116void hostapd_periodic_iface(struct hostapd_iface *iface)
3117{
3118 size_t i;
3119
3120 ap_list_timer(iface);
3121
3122 for (i = 0; i < iface->num_bss; i++) {
3123 struct hostapd_data *hapd = iface->bss[i];
3124
3125 if (!hapd->started)
3126 continue;
3127
3128#ifndef CONFIG_NO_RADIUS
3129 hostapd_acl_expire(hapd);
3130#endif /* CONFIG_NO_RADIUS */
3131 }
3132}