blob: 6e4169ba9ffd8917172ea5eb6462b9e791338293 [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 Shmidt8d520ff2011-05-09 14:06:53 -070015#include "radius/radius_client.h"
Dmitry Shmidt04949592012-07-19 12:16:46 -070016#include "radius/radius_das.h"
Dmitry Shmidt50b691d2014-05-21 14:01:45 -070017#include "eap_server/tncs.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070018#include "hostapd.h"
19#include "authsrv.h"
20#include "sta_info.h"
21#include "accounting.h"
22#include "ap_list.h"
23#include "beacon.h"
24#include "iapp.h"
25#include "ieee802_1x.h"
26#include "ieee802_11_auth.h"
27#include "vlan_init.h"
28#include "wpa_auth.h"
29#include "wps_hostapd.h"
30#include "hw_features.h"
31#include "wpa_auth_glue.h"
32#include "ap_drv_ops.h"
33#include "ap_config.h"
34#include "p2p_hostapd.h"
Dmitry Shmidt04949592012-07-19 12:16:46 -070035#include "gas_serv.h"
Dmitry Shmidt051af732013-10-22 13:52:46 -070036#include "dfs.h"
Dmitry Shmidt7832adb2014-04-29 10:53:02 -070037#include "ieee802_11.h"
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080038#include "bss_load.h"
39#include "x_snoop.h"
40#include "dhcp_snoop.h"
41#include "ndisc_snoop.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070042
43
Dmitry Shmidt04949592012-07-19 12:16:46 -070044static int hostapd_flush_old_stations(struct hostapd_data *hapd, u16 reason);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070045static int hostapd_setup_encryption(char *iface, struct hostapd_data *hapd);
Dmitry Shmidtc55524a2011-07-07 11:18:38 -070046static int hostapd_broadcast_wep_clear(struct hostapd_data *hapd);
Dmitry Shmidtcce06662013-11-04 18:44:24 -080047static int setup_interface2(struct hostapd_iface *iface);
48static void channel_list_update_timeout(void *eloop_ctx, void *timeout_ctx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070049
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070050
Dmitry Shmidt04949592012-07-19 12:16:46 -070051int hostapd_for_each_interface(struct hapd_interfaces *interfaces,
52 int (*cb)(struct hostapd_iface *iface,
53 void *ctx), void *ctx)
54{
55 size_t i;
56 int ret;
57
58 for (i = 0; i < interfaces->count; i++) {
59 ret = cb(interfaces->iface[i], ctx);
60 if (ret)
61 return ret;
62 }
63
64 return 0;
65}
66
67
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070068static void hostapd_reload_bss(struct hostapd_data *hapd)
69{
Dmitry Shmidtcce06662013-11-04 18:44:24 -080070 struct hostapd_ssid *ssid;
71
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070072#ifndef CONFIG_NO_RADIUS
73 radius_client_reconfig(hapd->radius, hapd->conf->radius);
74#endif /* CONFIG_NO_RADIUS */
75
Dmitry Shmidtcce06662013-11-04 18:44:24 -080076 ssid = &hapd->conf->ssid;
77 if (!ssid->wpa_psk_set && ssid->wpa_psk && !ssid->wpa_psk->next &&
78 ssid->wpa_passphrase_set && ssid->wpa_passphrase) {
79 /*
80 * Force PSK to be derived again since SSID or passphrase may
81 * have changed.
82 */
83 os_free(ssid->wpa_psk);
84 ssid->wpa_psk = NULL;
85 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070086 if (hostapd_setup_wpa_psk(hapd->conf)) {
87 wpa_printf(MSG_ERROR, "Failed to re-configure WPA PSK "
88 "after reloading configuration");
89 }
90
91 if (hapd->conf->ieee802_1x || hapd->conf->wpa)
92 hostapd_set_drv_ieee8021x(hapd, hapd->conf->iface, 1);
93 else
94 hostapd_set_drv_ieee8021x(hapd, hapd->conf->iface, 0);
95
Dmitry Shmidtf21452a2014-02-26 10:55:25 -080096 if ((hapd->conf->wpa || hapd->conf->osen) && hapd->wpa_auth == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070097 hostapd_setup_wpa(hapd);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080098 if (hapd->wpa_auth)
99 wpa_init_keys(hapd->wpa_auth);
100 } else if (hapd->conf->wpa) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700101 const u8 *wpa_ie;
102 size_t wpa_ie_len;
103 hostapd_reconfig_wpa(hapd);
104 wpa_ie = wpa_auth_get_wpa_ie(hapd->wpa_auth, &wpa_ie_len);
105 if (hostapd_set_generic_elem(hapd, wpa_ie, wpa_ie_len))
106 wpa_printf(MSG_ERROR, "Failed to configure WPA IE for "
107 "the kernel driver.");
108 } else if (hapd->wpa_auth) {
109 wpa_deinit(hapd->wpa_auth);
110 hapd->wpa_auth = NULL;
111 hostapd_set_privacy(hapd, 0);
112 hostapd_setup_encryption(hapd->conf->iface, hapd);
113 hostapd_set_generic_elem(hapd, (u8 *) "", 0);
114 }
115
116 ieee802_11_set_beacon(hapd);
117 hostapd_update_wps(hapd);
118
119 if (hapd->conf->ssid.ssid_set &&
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700120 hostapd_set_ssid(hapd, hapd->conf->ssid.ssid,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700121 hapd->conf->ssid.ssid_len)) {
122 wpa_printf(MSG_ERROR, "Could not set SSID for kernel driver");
123 /* try to continue */
124 }
125 wpa_printf(MSG_DEBUG, "Reconfigured interface %s", hapd->conf->iface);
126}
127
128
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700129static void hostapd_clear_old(struct hostapd_iface *iface)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700130{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700131 size_t j;
132
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700133 /*
134 * Deauthenticate all stations since the new configuration may not
135 * allow them to use the BSS anymore.
136 */
137 for (j = 0; j < iface->num_bss; j++) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700138 hostapd_flush_old_stations(iface->bss[j],
139 WLAN_REASON_PREV_AUTH_NOT_VALID);
Dmitry Shmidtc55524a2011-07-07 11:18:38 -0700140 hostapd_broadcast_wep_clear(iface->bss[j]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700141
142#ifndef CONFIG_NO_RADIUS
143 /* TODO: update dynamic data based on changed configuration
144 * items (e.g., open/close sockets, etc.) */
145 radius_client_flush(iface->bss[j]->radius, 0);
146#endif /* CONFIG_NO_RADIUS */
147 }
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700148}
149
150
151int hostapd_reload_config(struct hostapd_iface *iface)
152{
153 struct hostapd_data *hapd = iface->bss[0];
154 struct hostapd_config *newconf, *oldconf;
155 size_t j;
156
157 if (iface->config_fname == NULL) {
158 /* Only in-memory config in use - assume it has been updated */
159 hostapd_clear_old(iface);
160 for (j = 0; j < iface->num_bss; j++)
161 hostapd_reload_bss(iface->bss[j]);
162 return 0;
163 }
164
165 if (iface->interfaces == NULL ||
166 iface->interfaces->config_read_cb == NULL)
167 return -1;
168 newconf = iface->interfaces->config_read_cb(iface->config_fname);
169 if (newconf == NULL)
170 return -1;
171
172 hostapd_clear_old(iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700173
174 oldconf = hapd->iconf;
175 iface->conf = newconf;
176
177 for (j = 0; j < iface->num_bss; j++) {
178 hapd = iface->bss[j];
179 hapd->iconf = newconf;
Dmitry Shmidtd11f0192014-03-24 12:09:47 -0700180 hapd->iconf->channel = oldconf->channel;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -0700181 hapd->iconf->secondary_channel = oldconf->secondary_channel;
Dmitry Shmidtd11f0192014-03-24 12:09:47 -0700182 hapd->iconf->ieee80211n = oldconf->ieee80211n;
183 hapd->iconf->ieee80211ac = oldconf->ieee80211ac;
184 hapd->iconf->ht_capab = oldconf->ht_capab;
185 hapd->iconf->vht_capab = oldconf->vht_capab;
186 hapd->iconf->vht_oper_chwidth = oldconf->vht_oper_chwidth;
187 hapd->iconf->vht_oper_centr_freq_seg0_idx =
188 oldconf->vht_oper_centr_freq_seg0_idx;
189 hapd->iconf->vht_oper_centr_freq_seg1_idx =
190 oldconf->vht_oper_centr_freq_seg1_idx;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800191 hapd->conf = newconf->bss[j];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700192 hostapd_reload_bss(hapd);
193 }
194
195 hostapd_config_free(oldconf);
196
197
198 return 0;
199}
200
201
202static void hostapd_broadcast_key_clear_iface(struct hostapd_data *hapd,
203 char *ifname)
204{
205 int i;
206
207 for (i = 0; i < NUM_WEP_KEYS; i++) {
208 if (hostapd_drv_set_key(ifname, hapd, WPA_ALG_NONE, NULL, i,
209 0, NULL, 0, NULL, 0)) {
210 wpa_printf(MSG_DEBUG, "Failed to clear default "
211 "encryption keys (ifname=%s keyidx=%d)",
212 ifname, i);
213 }
214 }
215#ifdef CONFIG_IEEE80211W
216 if (hapd->conf->ieee80211w) {
217 for (i = NUM_WEP_KEYS; i < NUM_WEP_KEYS + 2; i++) {
218 if (hostapd_drv_set_key(ifname, hapd, WPA_ALG_NONE,
219 NULL, i, 0, NULL,
220 0, NULL, 0)) {
221 wpa_printf(MSG_DEBUG, "Failed to clear "
222 "default mgmt encryption keys "
223 "(ifname=%s keyidx=%d)", ifname, i);
224 }
225 }
226 }
227#endif /* CONFIG_IEEE80211W */
228}
229
230
231static int hostapd_broadcast_wep_clear(struct hostapd_data *hapd)
232{
233 hostapd_broadcast_key_clear_iface(hapd, hapd->conf->iface);
234 return 0;
235}
236
237
238static int hostapd_broadcast_wep_set(struct hostapd_data *hapd)
239{
240 int errors = 0, idx;
241 struct hostapd_ssid *ssid = &hapd->conf->ssid;
242
243 idx = ssid->wep.idx;
244 if (ssid->wep.default_len &&
245 hostapd_drv_set_key(hapd->conf->iface,
246 hapd, WPA_ALG_WEP, broadcast_ether_addr, idx,
247 1, NULL, 0, ssid->wep.key[idx],
248 ssid->wep.len[idx])) {
249 wpa_printf(MSG_WARNING, "Could not set WEP encryption.");
250 errors++;
251 }
252
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700253 return errors;
254}
255
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700256
Dmitry Shmidt04949592012-07-19 12:16:46 -0700257static void hostapd_free_hapd_data(struct hostapd_data *hapd)
258{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800259 os_free(hapd->probereq_cb);
260 hapd->probereq_cb = NULL;
261
262#ifdef CONFIG_P2P
263 wpabuf_free(hapd->p2p_beacon_ie);
264 hapd->p2p_beacon_ie = NULL;
265 wpabuf_free(hapd->p2p_probe_resp_ie);
266 hapd->p2p_probe_resp_ie = NULL;
267#endif /* CONFIG_P2P */
268
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -0800269 if (!hapd->started) {
270 wpa_printf(MSG_ERROR, "%s: Interface %s wasn't started",
271 __func__, hapd->conf->iface);
272 return;
273 }
274 hapd->started = 0;
275
Dmitry Shmidt54605472013-11-08 11:10:19 -0800276 wpa_printf(MSG_DEBUG, "%s(%s)", __func__, hapd->conf->iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700277 iapp_deinit(hapd->iapp);
278 hapd->iapp = NULL;
279 accounting_deinit(hapd);
280 hostapd_deinit_wpa(hapd);
281 vlan_deinit(hapd);
282 hostapd_acl_deinit(hapd);
283#ifndef CONFIG_NO_RADIUS
284 radius_client_deinit(hapd->radius);
285 hapd->radius = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700286 radius_das_deinit(hapd->radius_das);
287 hapd->radius_das = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700288#endif /* CONFIG_NO_RADIUS */
289
290 hostapd_deinit_wps(hapd);
291
292 authsrv_deinit(hapd);
293
Dmitry Shmidt71757432014-06-02 13:50:35 -0700294 if (hapd->interface_added) {
295 hapd->interface_added = 0;
296 if (hostapd_if_remove(hapd, WPA_IF_AP_BSS, hapd->conf->iface)) {
297 wpa_printf(MSG_WARNING,
298 "Failed to remove BSS interface %s",
299 hapd->conf->iface);
300 hapd->interface_added = 1;
301 } else {
302 /*
303 * Since this was a dynamically added interface, the
304 * driver wrapper may have removed its internal instance
305 * and hapd->drv_priv is not valid anymore.
306 */
307 hapd->drv_priv = NULL;
308 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700309 }
310
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800311 wpabuf_free(hapd->time_adv);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700312
313#ifdef CONFIG_INTERWORKING
314 gas_serv_deinit(hapd);
315#endif /* CONFIG_INTERWORKING */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800316
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800317 bss_load_update_deinit(hapd);
318 ndisc_snoop_deinit(hapd);
319 dhcp_snoop_deinit(hapd);
320 x_snoop_deinit(hapd);
321
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800322#ifdef CONFIG_SQLITE
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700323 bin_clear_free(hapd->tmp_eap_user.identity,
324 hapd->tmp_eap_user.identity_len);
325 bin_clear_free(hapd->tmp_eap_user.password,
326 hapd->tmp_eap_user.password_len);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800327#endif /* CONFIG_SQLITE */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800328
329#ifdef CONFIG_MESH
330 wpabuf_free(hapd->mesh_pending_auth);
331 hapd->mesh_pending_auth = NULL;
332#endif /* CONFIG_MESH */
Dmitry Shmidt04949592012-07-19 12:16:46 -0700333}
334
335
336/**
337 * hostapd_cleanup - Per-BSS cleanup (deinitialization)
338 * @hapd: Pointer to BSS data
339 *
340 * This function is used to free all per-BSS data structures and resources.
Dmitry Shmidt54605472013-11-08 11:10:19 -0800341 * Most of the modules that are initialized in hostapd_setup_bss() are
342 * deinitialized here.
Dmitry Shmidt04949592012-07-19 12:16:46 -0700343 */
344static void hostapd_cleanup(struct hostapd_data *hapd)
345{
Dmitry Shmidt54605472013-11-08 11:10:19 -0800346 wpa_printf(MSG_DEBUG, "%s(hapd=%p (%s))", __func__, hapd,
347 hapd->conf->iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700348 if (hapd->iface->interfaces &&
349 hapd->iface->interfaces->ctrl_iface_deinit)
350 hapd->iface->interfaces->ctrl_iface_deinit(hapd);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700351 hostapd_free_hapd_data(hapd);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700352}
353
354
Dmitry Shmidt04949592012-07-19 12:16:46 -0700355static void hostapd_cleanup_iface_partial(struct hostapd_iface *iface)
356{
Dmitry Shmidt54605472013-11-08 11:10:19 -0800357 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700358 hostapd_free_hw_features(iface->hw_features, iface->num_hw_features);
359 iface->hw_features = NULL;
360 os_free(iface->current_rates);
361 iface->current_rates = NULL;
362 os_free(iface->basic_rates);
363 iface->basic_rates = NULL;
364 ap_list_deinit(iface);
365}
366
367
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700368/**
369 * hostapd_cleanup_iface - Complete per-interface cleanup
370 * @iface: Pointer to interface data
371 *
372 * This function is called after per-BSS data structures are deinitialized
373 * with hostapd_cleanup().
374 */
375static void hostapd_cleanup_iface(struct hostapd_iface *iface)
376{
Dmitry Shmidt54605472013-11-08 11:10:19 -0800377 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800378 eloop_cancel_timeout(channel_list_update_timeout, iface, NULL);
379
Dmitry Shmidt04949592012-07-19 12:16:46 -0700380 hostapd_cleanup_iface_partial(iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700381 hostapd_config_free(iface->conf);
382 iface->conf = NULL;
383
384 os_free(iface->config_fname);
385 os_free(iface->bss);
Dmitry Shmidt54605472013-11-08 11:10:19 -0800386 wpa_printf(MSG_DEBUG, "%s: free iface=%p", __func__, iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700387 os_free(iface);
388}
389
390
Dmitry Shmidt04949592012-07-19 12:16:46 -0700391static void hostapd_clear_wep(struct hostapd_data *hapd)
392{
Dmitry Shmidta38abf92014-03-06 13:38:44 -0800393 if (hapd->drv_priv && !hapd->iface->driver_ap_teardown) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700394 hostapd_set_privacy(hapd, 0);
395 hostapd_broadcast_wep_clear(hapd);
396 }
397}
398
399
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700400static int hostapd_setup_encryption(char *iface, struct hostapd_data *hapd)
401{
402 int i;
403
404 hostapd_broadcast_wep_set(hapd);
405
406 if (hapd->conf->ssid.wep.default_len) {
407 hostapd_set_privacy(hapd, 1);
408 return 0;
409 }
410
Jouni Malinen75ecf522011-06-27 15:19:46 -0700411 /*
412 * When IEEE 802.1X is not enabled, the driver may need to know how to
413 * set authentication algorithms for static WEP.
414 */
415 hostapd_drv_set_authmode(hapd, hapd->conf->auth_algs);
416
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700417 for (i = 0; i < 4; i++) {
418 if (hapd->conf->ssid.wep.key[i] &&
419 hostapd_drv_set_key(iface, hapd, WPA_ALG_WEP, NULL, i,
420 i == hapd->conf->ssid.wep.idx, NULL, 0,
421 hapd->conf->ssid.wep.key[i],
422 hapd->conf->ssid.wep.len[i])) {
423 wpa_printf(MSG_WARNING, "Could not set WEP "
424 "encryption.");
425 return -1;
426 }
427 if (hapd->conf->ssid.wep.key[i] &&
428 i == hapd->conf->ssid.wep.idx)
429 hostapd_set_privacy(hapd, 1);
430 }
431
432 return 0;
433}
434
435
Dmitry Shmidt04949592012-07-19 12:16:46 -0700436static int hostapd_flush_old_stations(struct hostapd_data *hapd, u16 reason)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700437{
438 int ret = 0;
439 u8 addr[ETH_ALEN];
440
441 if (hostapd_drv_none(hapd) || hapd->drv_priv == NULL)
442 return 0;
443
Dmitry Shmidta38abf92014-03-06 13:38:44 -0800444 if (!hapd->iface->driver_ap_teardown) {
445 wpa_dbg(hapd->msg_ctx, MSG_DEBUG,
446 "Flushing old station entries");
447
448 if (hostapd_flush(hapd)) {
449 wpa_msg(hapd->msg_ctx, MSG_WARNING,
450 "Could not connect to kernel driver");
451 ret = -1;
452 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700453 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800454 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "Deauthenticate all stations");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700455 os_memset(addr, 0xff, ETH_ALEN);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700456 hostapd_drv_sta_deauth(hapd, addr, reason);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700457 hostapd_free_stas(hapd);
458
459 return ret;
460}
461
462
Dmitry Shmidt71757432014-06-02 13:50:35 -0700463static void hostapd_bss_deinit_no_free(struct hostapd_data *hapd)
464{
465 hostapd_free_stas(hapd);
466 hostapd_flush_old_stations(hapd, WLAN_REASON_DEAUTH_LEAVING);
467 hostapd_clear_wep(hapd);
468}
469
470
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700471/**
472 * hostapd_validate_bssid_configuration - Validate BSSID configuration
473 * @iface: Pointer to interface data
474 * Returns: 0 on success, -1 on failure
475 *
476 * This function is used to validate that the configured BSSIDs are valid.
477 */
478static int hostapd_validate_bssid_configuration(struct hostapd_iface *iface)
479{
480 u8 mask[ETH_ALEN] = { 0 };
481 struct hostapd_data *hapd = iface->bss[0];
482 unsigned int i = iface->conf->num_bss, bits = 0, j;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700483 int auto_addr = 0;
484
485 if (hostapd_drv_none(hapd))
486 return 0;
487
488 /* Generate BSSID mask that is large enough to cover the BSSIDs. */
489
490 /* Determine the bits necessary to cover the number of BSSIDs. */
491 for (i--; i; i >>= 1)
492 bits++;
493
494 /* Determine the bits necessary to any configured BSSIDs,
495 if they are higher than the number of BSSIDs. */
496 for (j = 0; j < iface->conf->num_bss; j++) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800497 if (hostapd_mac_comp_empty(iface->conf->bss[j]->bssid) == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700498 if (j)
499 auto_addr++;
500 continue;
501 }
502
503 for (i = 0; i < ETH_ALEN; i++) {
504 mask[i] |=
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800505 iface->conf->bss[j]->bssid[i] ^
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700506 hapd->own_addr[i];
507 }
508 }
509
510 if (!auto_addr)
511 goto skip_mask_ext;
512
513 for (i = 0; i < ETH_ALEN && mask[i] == 0; i++)
514 ;
515 j = 0;
516 if (i < ETH_ALEN) {
517 j = (5 - i) * 8;
518
519 while (mask[i] != 0) {
520 mask[i] >>= 1;
521 j++;
522 }
523 }
524
525 if (bits < j)
526 bits = j;
527
528 if (bits > 40) {
529 wpa_printf(MSG_ERROR, "Too many bits in the BSSID mask (%u)",
530 bits);
531 return -1;
532 }
533
534 os_memset(mask, 0xff, ETH_ALEN);
535 j = bits / 8;
536 for (i = 5; i > 5 - j; i--)
537 mask[i] = 0;
538 j = bits % 8;
539 while (j--)
540 mask[i] <<= 1;
541
542skip_mask_ext:
543 wpa_printf(MSG_DEBUG, "BSS count %lu, BSSID mask " MACSTR " (%d bits)",
544 (unsigned long) iface->conf->num_bss, MAC2STR(mask), bits);
545
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700546 if (!auto_addr)
547 return 0;
548
549 for (i = 0; i < ETH_ALEN; i++) {
550 if ((hapd->own_addr[i] & mask[i]) != hapd->own_addr[i]) {
551 wpa_printf(MSG_ERROR, "Invalid BSSID mask " MACSTR
552 " for start address " MACSTR ".",
553 MAC2STR(mask), MAC2STR(hapd->own_addr));
554 wpa_printf(MSG_ERROR, "Start address must be the "
555 "first address in the block (i.e., addr "
556 "AND mask == addr).");
557 return -1;
558 }
559 }
560
561 return 0;
562}
563
564
565static int mac_in_conf(struct hostapd_config *conf, const void *a)
566{
567 size_t i;
568
569 for (i = 0; i < conf->num_bss; i++) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800570 if (hostapd_mac_comp(conf->bss[i]->bssid, a) == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700571 return 1;
572 }
573 }
574
575 return 0;
576}
577
578
Dmitry Shmidt04949592012-07-19 12:16:46 -0700579#ifndef CONFIG_NO_RADIUS
580
581static int hostapd_das_nas_mismatch(struct hostapd_data *hapd,
582 struct radius_das_attrs *attr)
583{
Dmitry Shmidt13ca8d82014-02-20 10:18:40 -0800584 if (attr->nas_identifier &&
585 (!hapd->conf->nas_identifier ||
586 os_strlen(hapd->conf->nas_identifier) !=
587 attr->nas_identifier_len ||
588 os_memcmp(hapd->conf->nas_identifier, attr->nas_identifier,
589 attr->nas_identifier_len) != 0)) {
590 wpa_printf(MSG_DEBUG, "RADIUS DAS: NAS-Identifier mismatch");
591 return 1;
592 }
593
594 if (attr->nas_ip_addr &&
595 (hapd->conf->own_ip_addr.af != AF_INET ||
596 os_memcmp(&hapd->conf->own_ip_addr.u.v4, attr->nas_ip_addr, 4) !=
597 0)) {
598 wpa_printf(MSG_DEBUG, "RADIUS DAS: NAS-IP-Address mismatch");
599 return 1;
600 }
601
602#ifdef CONFIG_IPV6
603 if (attr->nas_ipv6_addr &&
604 (hapd->conf->own_ip_addr.af != AF_INET6 ||
605 os_memcmp(&hapd->conf->own_ip_addr.u.v6, attr->nas_ipv6_addr, 16)
606 != 0)) {
607 wpa_printf(MSG_DEBUG, "RADIUS DAS: NAS-IPv6-Address mismatch");
608 return 1;
609 }
610#endif /* CONFIG_IPV6 */
611
Dmitry Shmidt04949592012-07-19 12:16:46 -0700612 return 0;
613}
614
615
616static struct sta_info * hostapd_das_find_sta(struct hostapd_data *hapd,
617 struct radius_das_attrs *attr)
618{
619 struct sta_info *sta = NULL;
620 char buf[128];
621
622 if (attr->sta_addr)
623 sta = ap_get_sta(hapd, attr->sta_addr);
624
625 if (sta == NULL && attr->acct_session_id &&
626 attr->acct_session_id_len == 17) {
627 for (sta = hapd->sta_list; sta; sta = sta->next) {
628 os_snprintf(buf, sizeof(buf), "%08X-%08X",
629 sta->acct_session_id_hi,
630 sta->acct_session_id_lo);
631 if (os_memcmp(attr->acct_session_id, buf, 17) == 0)
632 break;
633 }
634 }
635
636 if (sta == NULL && attr->cui) {
637 for (sta = hapd->sta_list; sta; sta = sta->next) {
638 struct wpabuf *cui;
639 cui = ieee802_1x_get_radius_cui(sta->eapol_sm);
640 if (cui && wpabuf_len(cui) == attr->cui_len &&
641 os_memcmp(wpabuf_head(cui), attr->cui,
642 attr->cui_len) == 0)
643 break;
644 }
645 }
646
647 if (sta == NULL && attr->user_name) {
648 for (sta = hapd->sta_list; sta; sta = sta->next) {
649 u8 *identity;
650 size_t identity_len;
651 identity = ieee802_1x_get_identity(sta->eapol_sm,
652 &identity_len);
653 if (identity &&
654 identity_len == attr->user_name_len &&
655 os_memcmp(identity, attr->user_name, identity_len)
656 == 0)
657 break;
658 }
659 }
660
661 return sta;
662}
663
664
665static enum radius_das_res
666hostapd_das_disconnect(void *ctx, struct radius_das_attrs *attr)
667{
668 struct hostapd_data *hapd = ctx;
669 struct sta_info *sta;
670
671 if (hostapd_das_nas_mismatch(hapd, attr))
672 return RADIUS_DAS_NAS_MISMATCH;
673
674 sta = hostapd_das_find_sta(hapd, attr);
675 if (sta == NULL)
676 return RADIUS_DAS_SESSION_NOT_FOUND;
677
Dmitry Shmidt13ca8d82014-02-20 10:18:40 -0800678 wpa_auth_pmksa_remove(hapd->wpa_auth, sta->addr);
679
Dmitry Shmidt04949592012-07-19 12:16:46 -0700680 hostapd_drv_sta_deauth(hapd, sta->addr,
681 WLAN_REASON_PREV_AUTH_NOT_VALID);
682 ap_sta_deauthenticate(hapd, sta, WLAN_REASON_PREV_AUTH_NOT_VALID);
683
684 return RADIUS_DAS_SUCCESS;
685}
686
687#endif /* CONFIG_NO_RADIUS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700688
689
690/**
691 * hostapd_setup_bss - Per-BSS setup (initialization)
692 * @hapd: Pointer to BSS data
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800693 * @first: Whether this BSS is the first BSS of an interface; -1 = not first,
694 * but interface may exist
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700695 *
696 * This function is used to initialize all per-BSS data structures and
697 * resources. This gets called in a loop for each BSS when an interface is
698 * initialized. Most of the modules that are initialized here will be
699 * deinitialized in hostapd_cleanup().
700 */
701static int hostapd_setup_bss(struct hostapd_data *hapd, int first)
702{
703 struct hostapd_bss_config *conf = hapd->conf;
704 u8 ssid[HOSTAPD_MAX_SSID_LEN + 1];
705 int ssid_len, set_ssid;
706 char force_ifname[IFNAMSIZ];
707 u8 if_addr[ETH_ALEN];
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800708 int flush_old_stations = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700709
Dmitry Shmidt54605472013-11-08 11:10:19 -0800710 wpa_printf(MSG_DEBUG, "%s(hapd=%p (%s), first=%d)",
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700711 __func__, hapd, conf->iface, first);
Dmitry Shmidt54605472013-11-08 11:10:19 -0800712
Dmitry Shmidt50b691d2014-05-21 14:01:45 -0700713#ifdef EAP_SERVER_TNC
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700714 if (conf->tnc && tncs_global_init() < 0) {
Dmitry Shmidt50b691d2014-05-21 14:01:45 -0700715 wpa_printf(MSG_ERROR, "Failed to initialize TNCS");
716 return -1;
717 }
718#endif /* EAP_SERVER_TNC */
719
Dmitry Shmidt54605472013-11-08 11:10:19 -0800720 if (hapd->started) {
721 wpa_printf(MSG_ERROR, "%s: Interface %s was already started",
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700722 __func__, conf->iface);
Dmitry Shmidt54605472013-11-08 11:10:19 -0800723 return -1;
724 }
725 hapd->started = 1;
726
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800727 if (!first || first == -1) {
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700728 if (hostapd_mac_comp_empty(conf->bssid) == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700729 /* Allocate the next available BSSID. */
730 do {
731 inc_byte_array(hapd->own_addr, ETH_ALEN);
732 } while (mac_in_conf(hapd->iconf, hapd->own_addr));
733 } else {
734 /* Allocate the configured BSSID. */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700735 os_memcpy(hapd->own_addr, conf->bssid, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700736
737 if (hostapd_mac_comp(hapd->own_addr,
738 hapd->iface->bss[0]->own_addr) ==
739 0) {
740 wpa_printf(MSG_ERROR, "BSS '%s' may not have "
741 "BSSID set to the MAC address of "
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700742 "the radio", conf->iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700743 return -1;
744 }
745 }
746
Dmitry Shmidt54605472013-11-08 11:10:19 -0800747 hapd->interface_added = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700748 if (hostapd_if_add(hapd->iface->bss[0], WPA_IF_AP_BSS,
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700749 conf->iface, hapd->own_addr, hapd,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700750 &hapd->drv_priv, force_ifname, if_addr,
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700751 conf->bridge[0] ? conf->bridge : NULL,
752 first == -1)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700753 wpa_printf(MSG_ERROR, "Failed to add BSS (BSSID="
754 MACSTR ")", MAC2STR(hapd->own_addr));
Dmitry Shmidt3cf6f792013-12-18 13:12:19 -0800755 hapd->interface_added = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700756 return -1;
757 }
758 }
759
760 if (conf->wmm_enabled < 0)
761 conf->wmm_enabled = hapd->iconf->ieee80211n;
762
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800763#ifdef CONFIG_MESH
764 if (hapd->iface->mconf == NULL)
765 flush_old_stations = 0;
766#endif /* CONFIG_MESH */
767
768 if (flush_old_stations)
769 hostapd_flush_old_stations(hapd,
770 WLAN_REASON_PREV_AUTH_NOT_VALID);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700771 hostapd_set_privacy(hapd, 0);
772
773 hostapd_broadcast_wep_clear(hapd);
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700774 if (hostapd_setup_encryption(conf->iface, hapd))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700775 return -1;
776
777 /*
778 * Fetch the SSID from the system and use it or,
779 * if one was specified in the config file, verify they
780 * match.
781 */
782 ssid_len = hostapd_get_ssid(hapd, ssid, sizeof(ssid));
783 if (ssid_len < 0) {
784 wpa_printf(MSG_ERROR, "Could not read SSID from system");
785 return -1;
786 }
787 if (conf->ssid.ssid_set) {
788 /*
789 * If SSID is specified in the config file and it differs
790 * from what is being used then force installation of the
791 * new SSID.
792 */
793 set_ssid = (conf->ssid.ssid_len != (size_t) ssid_len ||
794 os_memcmp(conf->ssid.ssid, ssid, ssid_len) != 0);
795 } else {
796 /*
797 * No SSID in the config file; just use the one we got
798 * from the system.
799 */
800 set_ssid = 0;
801 conf->ssid.ssid_len = ssid_len;
802 os_memcpy(conf->ssid.ssid, ssid, conf->ssid.ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700803 }
804
805 if (!hostapd_drv_none(hapd)) {
806 wpa_printf(MSG_ERROR, "Using interface %s with hwaddr " MACSTR
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700807 " and ssid \"%s\"",
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700808 conf->iface, MAC2STR(hapd->own_addr),
809 wpa_ssid_txt(conf->ssid.ssid, conf->ssid.ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700810 }
811
812 if (hostapd_setup_wpa_psk(conf)) {
813 wpa_printf(MSG_ERROR, "WPA-PSK setup failed.");
814 return -1;
815 }
816
817 /* Set SSID for the kernel driver (to be used in beacon and probe
818 * response frames) */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700819 if (set_ssid && hostapd_set_ssid(hapd, conf->ssid.ssid,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700820 conf->ssid.ssid_len)) {
821 wpa_printf(MSG_ERROR, "Could not set SSID for kernel driver");
822 return -1;
823 }
824
Dmitry Shmidt818ea482014-03-10 13:15:21 -0700825 if (wpa_debug_level <= MSG_MSGDUMP)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700826 conf->radius->msg_dumps = 1;
827#ifndef CONFIG_NO_RADIUS
828 hapd->radius = radius_client_init(hapd, conf->radius);
829 if (hapd->radius == NULL) {
830 wpa_printf(MSG_ERROR, "RADIUS client initialization failed.");
831 return -1;
832 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700833
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700834 if (conf->radius_das_port) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700835 struct radius_das_conf das_conf;
836 os_memset(&das_conf, 0, sizeof(das_conf));
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700837 das_conf.port = conf->radius_das_port;
838 das_conf.shared_secret = conf->radius_das_shared_secret;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700839 das_conf.shared_secret_len =
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700840 conf->radius_das_shared_secret_len;
841 das_conf.client_addr = &conf->radius_das_client_addr;
842 das_conf.time_window = conf->radius_das_time_window;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700843 das_conf.require_event_timestamp =
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700844 conf->radius_das_require_event_timestamp;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700845 das_conf.ctx = hapd;
846 das_conf.disconnect = hostapd_das_disconnect;
847 hapd->radius_das = radius_das_init(&das_conf);
848 if (hapd->radius_das == NULL) {
849 wpa_printf(MSG_ERROR, "RADIUS DAS initialization "
850 "failed.");
851 return -1;
852 }
853 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700854#endif /* CONFIG_NO_RADIUS */
855
856 if (hostapd_acl_init(hapd)) {
857 wpa_printf(MSG_ERROR, "ACL initialization failed.");
858 return -1;
859 }
860 if (hostapd_init_wps(hapd, conf))
861 return -1;
862
863 if (authsrv_init(hapd) < 0)
864 return -1;
865
866 if (ieee802_1x_init(hapd)) {
867 wpa_printf(MSG_ERROR, "IEEE 802.1X initialization failed.");
868 return -1;
869 }
870
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700871 if ((conf->wpa || conf->osen) && hostapd_setup_wpa(hapd))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700872 return -1;
873
874 if (accounting_init(hapd)) {
875 wpa_printf(MSG_ERROR, "Accounting initialization failed.");
876 return -1;
877 }
878
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700879 if (conf->ieee802_11f &&
880 (hapd->iapp = iapp_init(hapd, conf->iapp_iface)) == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700881 wpa_printf(MSG_ERROR, "IEEE 802.11F (IAPP) initialization "
882 "failed.");
883 return -1;
884 }
885
Dmitry Shmidt04949592012-07-19 12:16:46 -0700886#ifdef CONFIG_INTERWORKING
887 if (gas_serv_init(hapd)) {
888 wpa_printf(MSG_ERROR, "GAS server initialization failed");
889 return -1;
890 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700891
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800892 if (conf->qos_map_set_len &&
893 hostapd_drv_set_qos_map(hapd, conf->qos_map_set,
894 conf->qos_map_set_len)) {
895 wpa_printf(MSG_ERROR, "Failed to initialize QoS Map");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700896 return -1;
897 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800898#endif /* CONFIG_INTERWORKING */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700899
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800900 if (conf->bss_load_update_period && bss_load_update_init(hapd)) {
901 wpa_printf(MSG_ERROR, "BSS Load initialization failed");
902 return -1;
903 }
904
905 if (conf->proxy_arp) {
906 if (x_snoop_init(hapd)) {
907 wpa_printf(MSG_ERROR,
908 "Generic snooping infrastructure initialization failed");
909 return -1;
910 }
911
912 if (dhcp_snoop_init(hapd)) {
913 wpa_printf(MSG_ERROR,
914 "DHCP snooping initialization failed");
915 return -1;
916 }
917
918 if (ndisc_snoop_init(hapd)) {
919 wpa_printf(MSG_ERROR,
920 "Neighbor Discovery snooping initialization failed");
921 return -1;
922 }
923 }
924
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700925 if (!hostapd_drv_none(hapd) && vlan_init(hapd)) {
926 wpa_printf(MSG_ERROR, "VLAN initialization failed.");
927 return -1;
928 }
929
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700930 if (!conf->start_disabled && ieee802_11_set_beacon(hapd) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800931 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700932
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800933 if (hapd->wpa_auth && wpa_init_keys(hapd->wpa_auth) < 0)
934 return -1;
935
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700936 if (hapd->driver && hapd->driver->set_operstate)
937 hapd->driver->set_operstate(hapd->drv_priv, 1);
938
939 return 0;
940}
941
942
943static void hostapd_tx_queue_params(struct hostapd_iface *iface)
944{
945 struct hostapd_data *hapd = iface->bss[0];
946 int i;
947 struct hostapd_tx_queue_params *p;
948
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800949#ifdef CONFIG_MESH
950 if (iface->mconf == NULL)
951 return;
952#endif /* CONFIG_MESH */
953
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700954 for (i = 0; i < NUM_TX_QUEUES; i++) {
955 p = &iface->conf->tx_queue[i];
956
957 if (hostapd_set_tx_queue_params(hapd, i, p->aifs, p->cwmin,
958 p->cwmax, p->burst)) {
959 wpa_printf(MSG_DEBUG, "Failed to set TX queue "
960 "parameters for queue %d.", i);
961 /* Continue anyway */
962 }
963 }
964}
965
966
Dmitry Shmidt8bae4132013-06-06 11:25:10 -0700967static int hostapd_set_acl_list(struct hostapd_data *hapd,
968 struct mac_acl_entry *mac_acl,
969 int n_entries, u8 accept_acl)
970{
971 struct hostapd_acl_params *acl_params;
972 int i, err;
973
974 acl_params = os_zalloc(sizeof(*acl_params) +
975 (n_entries * sizeof(acl_params->mac_acl[0])));
976 if (!acl_params)
977 return -ENOMEM;
978
979 for (i = 0; i < n_entries; i++)
980 os_memcpy(acl_params->mac_acl[i].addr, mac_acl[i].addr,
981 ETH_ALEN);
982
983 acl_params->acl_policy = accept_acl;
984 acl_params->num_mac_acl = n_entries;
985
986 err = hostapd_drv_set_acl(hapd, acl_params);
987
988 os_free(acl_params);
989
990 return err;
991}
992
993
994static void hostapd_set_acl(struct hostapd_data *hapd)
995{
996 struct hostapd_config *conf = hapd->iconf;
997 int err;
998 u8 accept_acl;
999
1000 if (hapd->iface->drv_max_acl_mac_addrs == 0)
1001 return;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001002
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001003 if (conf->bss[0]->macaddr_acl == DENY_UNLESS_ACCEPTED) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001004 accept_acl = 1;
1005 err = hostapd_set_acl_list(hapd, conf->bss[0]->accept_mac,
1006 conf->bss[0]->num_accept_mac,
1007 accept_acl);
1008 if (err) {
1009 wpa_printf(MSG_DEBUG, "Failed to set accept acl");
1010 return;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001011 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001012 } else if (conf->bss[0]->macaddr_acl == ACCEPT_UNLESS_DENIED) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001013 accept_acl = 0;
1014 err = hostapd_set_acl_list(hapd, conf->bss[0]->deny_mac,
1015 conf->bss[0]->num_deny_mac,
1016 accept_acl);
1017 if (err) {
1018 wpa_printf(MSG_DEBUG, "Failed to set deny acl");
1019 return;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001020 }
1021 }
1022}
1023
1024
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001025static int start_ctrl_iface_bss(struct hostapd_data *hapd)
1026{
1027 if (!hapd->iface->interfaces ||
1028 !hapd->iface->interfaces->ctrl_iface_init)
1029 return 0;
1030
1031 if (hapd->iface->interfaces->ctrl_iface_init(hapd)) {
1032 wpa_printf(MSG_ERROR,
1033 "Failed to setup control interface for %s",
1034 hapd->conf->iface);
1035 return -1;
1036 }
1037
1038 return 0;
1039}
1040
1041
1042static int start_ctrl_iface(struct hostapd_iface *iface)
1043{
1044 size_t i;
1045
1046 if (!iface->interfaces || !iface->interfaces->ctrl_iface_init)
1047 return 0;
1048
1049 for (i = 0; i < iface->num_bss; i++) {
1050 struct hostapd_data *hapd = iface->bss[i];
1051 if (iface->interfaces->ctrl_iface_init(hapd)) {
1052 wpa_printf(MSG_ERROR,
1053 "Failed to setup control interface for %s",
1054 hapd->conf->iface);
1055 return -1;
1056 }
1057 }
1058
1059 return 0;
1060}
1061
1062
1063static void channel_list_update_timeout(void *eloop_ctx, void *timeout_ctx)
1064{
1065 struct hostapd_iface *iface = eloop_ctx;
1066
1067 if (!iface->wait_channel_update) {
1068 wpa_printf(MSG_INFO, "Channel list update timeout, but interface was not waiting for it");
1069 return;
1070 }
1071
1072 /*
1073 * It is possible that the existing channel list is acceptable, so try
1074 * to proceed.
1075 */
1076 wpa_printf(MSG_DEBUG, "Channel list update timeout - try to continue anyway");
1077 setup_interface2(iface);
1078}
1079
1080
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001081void hostapd_channel_list_updated(struct hostapd_iface *iface, int initiator)
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001082{
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001083 if (!iface->wait_channel_update || initiator != REGDOM_SET_BY_USER)
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001084 return;
1085
1086 wpa_printf(MSG_DEBUG, "Channel list updated - continue setup");
1087 eloop_cancel_timeout(channel_list_update_timeout, iface, NULL);
1088 setup_interface2(iface);
1089}
1090
1091
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001092static int setup_interface(struct hostapd_iface *iface)
1093{
1094 struct hostapd_data *hapd = iface->bss[0];
1095 size_t i;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001096
Dmitry Shmidta38abf92014-03-06 13:38:44 -08001097 /*
1098 * It is possible that setup_interface() is called after the interface
1099 * was disabled etc., in which case driver_ap_teardown is possibly set
1100 * to 1. Clear it here so any other key/station deletion, which is not
1101 * part of a teardown flow, would also call the relevant driver
1102 * callbacks.
1103 */
1104 iface->driver_ap_teardown = 0;
1105
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001106 if (!iface->phy[0]) {
1107 const char *phy = hostapd_drv_get_radio_name(hapd);
1108 if (phy) {
1109 wpa_printf(MSG_DEBUG, "phy: %s", phy);
1110 os_strlcpy(iface->phy, phy, sizeof(iface->phy));
1111 }
1112 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001113
1114 /*
1115 * Make sure that all BSSes get configured with a pointer to the same
1116 * driver interface.
1117 */
1118 for (i = 1; i < iface->num_bss; i++) {
1119 iface->bss[i]->driver = hapd->driver;
1120 iface->bss[i]->drv_priv = hapd->drv_priv;
1121 }
1122
1123 if (hostapd_validate_bssid_configuration(iface))
1124 return -1;
1125
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001126 /*
1127 * Initialize control interfaces early to allow external monitoring of
1128 * channel setup operations that may take considerable amount of time
1129 * especially for DFS cases.
1130 */
1131 if (start_ctrl_iface(iface))
1132 return -1;
1133
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001134 if (hapd->iconf->country[0] && hapd->iconf->country[1]) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001135 char country[4], previous_country[4];
1136
1137 hostapd_set_state(iface, HAPD_IFACE_COUNTRY_UPDATE);
1138 if (hostapd_get_country(hapd, previous_country) < 0)
1139 previous_country[0] = '\0';
1140
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001141 os_memcpy(country, hapd->iconf->country, 3);
1142 country[3] = '\0';
1143 if (hostapd_set_country(hapd, country) < 0) {
1144 wpa_printf(MSG_ERROR, "Failed to set country code");
1145 return -1;
1146 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001147
1148 wpa_printf(MSG_DEBUG, "Previous country code %s, new country code %s",
1149 previous_country, country);
1150
1151 if (os_strncmp(previous_country, country, 2) != 0) {
1152 wpa_printf(MSG_DEBUG, "Continue interface setup after channel list update");
1153 iface->wait_channel_update = 1;
Dmitry Shmidt97672262014-02-03 13:02:54 -08001154 eloop_register_timeout(5, 0,
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001155 channel_list_update_timeout,
1156 iface, NULL);
1157 return 0;
1158 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001159 }
1160
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001161 return setup_interface2(iface);
1162}
1163
1164
1165static int setup_interface2(struct hostapd_iface *iface)
1166{
1167 iface->wait_channel_update = 0;
1168
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001169 if (hostapd_get_hw_features(iface)) {
1170 /* Not all drivers support this yet, so continue without hw
1171 * feature data. */
1172 } else {
1173 int ret = hostapd_select_hw_mode(iface);
1174 if (ret < 0) {
1175 wpa_printf(MSG_ERROR, "Could not select hw_mode and "
1176 "channel. (%d)", ret);
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001177 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001178 }
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001179 if (ret == 1) {
1180 wpa_printf(MSG_DEBUG, "Interface initialization will be completed in a callback (ACS)");
1181 return 0;
1182 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001183 ret = hostapd_check_ht_capab(iface);
1184 if (ret < 0)
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001185 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001186 if (ret == 1) {
1187 wpa_printf(MSG_DEBUG, "Interface initialization will "
1188 "be completed in a callback");
1189 return 0;
1190 }
Dmitry Shmidt051af732013-10-22 13:52:46 -07001191
1192 if (iface->conf->ieee80211h)
1193 wpa_printf(MSG_DEBUG, "DFS support is enabled");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001194 }
1195 return hostapd_setup_interface_complete(iface, 0);
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001196
1197fail:
1198 hostapd_set_state(iface, HAPD_IFACE_DISABLED);
1199 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
1200 if (iface->interfaces && iface->interfaces->terminate_on_error)
1201 eloop_terminate();
1202 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001203}
1204
1205
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001206/**
1207 * hostapd_setup_interface_complete - Complete interface setup
1208 *
1209 * This function is called when previous steps in the interface setup has been
1210 * completed. This can also start operations, e.g., DFS, that will require
1211 * additional processing before interface is ready to be enabled. Such
1212 * operations will call this function from eloop callbacks when finished.
1213 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001214int hostapd_setup_interface_complete(struct hostapd_iface *iface, int err)
1215{
1216 struct hostapd_data *hapd = iface->bss[0];
1217 size_t j;
1218 u8 *prev_addr;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001219 int delay_apply_cfg = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001220
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001221 if (err)
1222 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001223
1224 wpa_printf(MSG_DEBUG, "Completing interface initialization");
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001225 if (iface->conf->channel) {
Dmitry Shmidt051af732013-10-22 13:52:46 -07001226#ifdef NEED_AP_MLME
1227 int res;
1228#endif /* NEED_AP_MLME */
1229
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001230 iface->freq = hostapd_hw_get_freq(hapd, iface->conf->channel);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001231 wpa_printf(MSG_DEBUG, "Mode: %s Channel: %d "
1232 "Frequency: %d MHz",
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001233 hostapd_hw_mode_txt(iface->conf->hw_mode),
1234 iface->conf->channel, iface->freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001235
Dmitry Shmidt051af732013-10-22 13:52:46 -07001236#ifdef NEED_AP_MLME
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001237 /* Handle DFS only if it is not offloaded to the driver */
1238 if (!(iface->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD)) {
1239 /* Check DFS */
1240 res = hostapd_handle_dfs(iface);
1241 if (res <= 0) {
1242 if (res < 0)
1243 goto fail;
1244 return res;
1245 }
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001246 }
Dmitry Shmidt051af732013-10-22 13:52:46 -07001247#endif /* NEED_AP_MLME */
1248
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001249#ifdef CONFIG_MESH
1250 if (iface->mconf != NULL) {
1251 wpa_printf(MSG_DEBUG,
1252 "%s: Mesh configuration will be applied while joining the mesh network",
1253 iface->bss[0]->conf->iface);
1254 delay_apply_cfg = 1;
1255 }
1256#endif /* CONFIG_MESH */
1257
1258 if (!delay_apply_cfg &&
1259 hostapd_set_freq(hapd, hapd->iconf->hw_mode, iface->freq,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001260 hapd->iconf->channel,
1261 hapd->iconf->ieee80211n,
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001262 hapd->iconf->ieee80211ac,
1263 hapd->iconf->secondary_channel,
1264 hapd->iconf->vht_oper_chwidth,
1265 hapd->iconf->vht_oper_centr_freq_seg0_idx,
1266 hapd->iconf->vht_oper_centr_freq_seg1_idx)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001267 wpa_printf(MSG_ERROR, "Could not set channel for "
1268 "kernel driver");
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001269 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001270 }
1271 }
1272
1273 if (iface->current_mode) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001274 if (hostapd_prepare_rates(iface, iface->current_mode)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001275 wpa_printf(MSG_ERROR, "Failed to prepare rates "
1276 "table.");
1277 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
1278 HOSTAPD_LEVEL_WARNING,
1279 "Failed to prepare rates table.");
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001280 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001281 }
1282 }
1283
1284 if (hapd->iconf->rts_threshold > -1 &&
1285 hostapd_set_rts(hapd, hapd->iconf->rts_threshold)) {
1286 wpa_printf(MSG_ERROR, "Could not set RTS threshold for "
1287 "kernel driver");
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001288 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001289 }
1290
1291 if (hapd->iconf->fragm_threshold > -1 &&
1292 hostapd_set_frag(hapd, hapd->iconf->fragm_threshold)) {
1293 wpa_printf(MSG_ERROR, "Could not set fragmentation threshold "
1294 "for kernel driver");
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001295 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001296 }
1297
1298 prev_addr = hapd->own_addr;
1299
1300 for (j = 0; j < iface->num_bss; j++) {
1301 hapd = iface->bss[j];
1302 if (j)
1303 os_memcpy(hapd->own_addr, prev_addr, ETH_ALEN);
Dmitry Shmidt71757432014-06-02 13:50:35 -07001304 if (hostapd_setup_bss(hapd, j == 0)) {
1305 do {
1306 hapd = iface->bss[j];
1307 hostapd_bss_deinit_no_free(hapd);
1308 hostapd_free_hapd_data(hapd);
1309 } while (j-- > 0);
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001310 goto fail;
Dmitry Shmidt71757432014-06-02 13:50:35 -07001311 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001312 if (hostapd_mac_comp_empty(hapd->conf->bssid) == 0)
1313 prev_addr = hapd->own_addr;
1314 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001315 hapd = iface->bss[0];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001316
1317 hostapd_tx_queue_params(iface);
1318
1319 ap_list_init(iface);
1320
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001321 hostapd_set_acl(hapd);
1322
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001323 if (hostapd_driver_commit(hapd) < 0) {
1324 wpa_printf(MSG_ERROR, "%s: Failed to commit driver "
1325 "configuration", __func__);
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001326 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001327 }
1328
Jouni Malinen87fd2792011-05-16 18:35:42 +03001329 /*
1330 * WPS UPnP module can be initialized only when the "upnp_iface" is up.
1331 * If "interface" and "upnp_iface" are the same (e.g., non-bridge
1332 * mode), the interface is up only after driver_commit, so initialize
1333 * WPS after driver_commit.
1334 */
1335 for (j = 0; j < iface->num_bss; j++) {
1336 if (hostapd_init_wps_complete(iface->bss[j]))
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001337 goto fail;
Jouni Malinen87fd2792011-05-16 18:35:42 +03001338 }
1339
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001340 hostapd_set_state(iface, HAPD_IFACE_ENABLED);
1341 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_ENABLED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001342 if (hapd->setup_complete_cb)
1343 hapd->setup_complete_cb(hapd->setup_complete_cb_ctx);
1344
1345 wpa_printf(MSG_DEBUG, "%s: Setup of interface done.",
1346 iface->bss[0]->conf->iface);
Dmitry Shmidtb96dad42013-11-05 10:07:29 -08001347 if (iface->interfaces && iface->interfaces->terminate_on_error > 0)
1348 iface->interfaces->terminate_on_error--;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001349
1350 return 0;
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001351
1352fail:
1353 wpa_printf(MSG_ERROR, "Interface initialization failed");
1354 hostapd_set_state(iface, HAPD_IFACE_DISABLED);
1355 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
1356 if (iface->interfaces && iface->interfaces->terminate_on_error)
1357 eloop_terminate();
1358 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001359}
1360
1361
1362/**
1363 * hostapd_setup_interface - Setup of an interface
1364 * @iface: Pointer to interface data.
1365 * Returns: 0 on success, -1 on failure
1366 *
1367 * Initializes the driver interface, validates the configuration,
1368 * and sets driver parameters based on the configuration.
1369 * Flushes old stations, sets the channel, encryption,
1370 * beacons, and WDS links based on the configuration.
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001371 *
1372 * If interface setup requires more time, e.g., to perform HT co-ex scans, ACS,
1373 * or DFS operations, this function returns 0 before such operations have been
1374 * completed. The pending operations are registered into eloop and will be
1375 * completed from eloop callbacks. Those callbacks end up calling
1376 * hostapd_setup_interface_complete() once setup has been completed.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001377 */
1378int hostapd_setup_interface(struct hostapd_iface *iface)
1379{
1380 int ret;
1381
1382 ret = setup_interface(iface);
1383 if (ret) {
1384 wpa_printf(MSG_ERROR, "%s: Unable to setup interface.",
1385 iface->bss[0]->conf->iface);
1386 return -1;
1387 }
1388
1389 return 0;
1390}
1391
1392
1393/**
1394 * hostapd_alloc_bss_data - Allocate and initialize per-BSS data
1395 * @hapd_iface: Pointer to interface data
1396 * @conf: Pointer to per-interface configuration
1397 * @bss: Pointer to per-BSS configuration for this BSS
1398 * Returns: Pointer to allocated BSS data
1399 *
1400 * This function is used to allocate per-BSS data structure. This data will be
1401 * freed after hostapd_cleanup() is called for it during interface
1402 * deinitialization.
1403 */
1404struct hostapd_data *
1405hostapd_alloc_bss_data(struct hostapd_iface *hapd_iface,
1406 struct hostapd_config *conf,
1407 struct hostapd_bss_config *bss)
1408{
1409 struct hostapd_data *hapd;
1410
1411 hapd = os_zalloc(sizeof(*hapd));
1412 if (hapd == NULL)
1413 return NULL;
1414
1415 hapd->new_assoc_sta_cb = hostapd_new_assoc_sta;
1416 hapd->iconf = conf;
1417 hapd->conf = bss;
1418 hapd->iface = hapd_iface;
1419 hapd->driver = hapd->iconf->driver;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001420 hapd->ctrl_sock = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001421
1422 return hapd;
1423}
1424
1425
Dmitry Shmidt54605472013-11-08 11:10:19 -08001426static void hostapd_bss_deinit(struct hostapd_data *hapd)
1427{
1428 wpa_printf(MSG_DEBUG, "%s: deinit bss %s", __func__,
1429 hapd->conf->iface);
Dmitry Shmidt71757432014-06-02 13:50:35 -07001430 hostapd_bss_deinit_no_free(hapd);
Dmitry Shmidt54605472013-11-08 11:10:19 -08001431 hostapd_cleanup(hapd);
1432}
1433
1434
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001435void hostapd_interface_deinit(struct hostapd_iface *iface)
1436{
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001437 int j;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001438
Dmitry Shmidt54605472013-11-08 11:10:19 -08001439 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001440 if (iface == NULL)
1441 return;
1442
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07001443#ifdef CONFIG_IEEE80211N
1444#ifdef NEED_AP_MLME
1445 hostapd_stop_setup_timers(iface);
1446 eloop_cancel_timeout(ap_ht2040_timeout, iface, NULL);
1447#endif /* NEED_AP_MLME */
1448#endif /* CONFIG_IEEE80211N */
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001449 eloop_cancel_timeout(channel_list_update_timeout, iface, NULL);
1450 iface->wait_channel_update = 0;
1451
Dmitry Shmidt54605472013-11-08 11:10:19 -08001452 for (j = iface->num_bss - 1; j >= 0; j--)
1453 hostapd_bss_deinit(iface->bss[j]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001454}
1455
1456
1457void hostapd_interface_free(struct hostapd_iface *iface)
1458{
1459 size_t j;
Dmitry Shmidt54605472013-11-08 11:10:19 -08001460 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
1461 for (j = 0; j < iface->num_bss; j++) {
1462 wpa_printf(MSG_DEBUG, "%s: free hapd %p",
1463 __func__, iface->bss[j]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001464 os_free(iface->bss[j]);
Dmitry Shmidt54605472013-11-08 11:10:19 -08001465 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001466 hostapd_cleanup_iface(iface);
1467}
1468
1469
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001470/**
1471 * hostapd_init - Allocate and initialize per-interface data
1472 * @config_file: Path to the configuration file
1473 * Returns: Pointer to the allocated interface data or %NULL on failure
1474 *
1475 * This function is used to allocate main data structures for per-interface
1476 * data. The allocated data buffer will be freed by calling
1477 * hostapd_cleanup_iface().
1478 */
1479struct hostapd_iface * hostapd_init(struct hapd_interfaces *interfaces,
1480 const char *config_file)
1481{
1482 struct hostapd_iface *hapd_iface = NULL;
1483 struct hostapd_config *conf = NULL;
1484 struct hostapd_data *hapd;
1485 size_t i;
1486
1487 hapd_iface = os_zalloc(sizeof(*hapd_iface));
1488 if (hapd_iface == NULL)
1489 goto fail;
1490
1491 hapd_iface->config_fname = os_strdup(config_file);
1492 if (hapd_iface->config_fname == NULL)
1493 goto fail;
1494
1495 conf = interfaces->config_read_cb(hapd_iface->config_fname);
1496 if (conf == NULL)
1497 goto fail;
1498 hapd_iface->conf = conf;
1499
1500 hapd_iface->num_bss = conf->num_bss;
1501 hapd_iface->bss = os_calloc(conf->num_bss,
1502 sizeof(struct hostapd_data *));
1503 if (hapd_iface->bss == NULL)
1504 goto fail;
1505
1506 for (i = 0; i < conf->num_bss; i++) {
1507 hapd = hapd_iface->bss[i] =
1508 hostapd_alloc_bss_data(hapd_iface, conf,
1509 conf->bss[i]);
1510 if (hapd == NULL)
1511 goto fail;
1512 hapd->msg_ctx = hapd;
1513 }
1514
1515 return hapd_iface;
1516
1517fail:
1518 wpa_printf(MSG_ERROR, "Failed to set up interface with %s",
1519 config_file);
1520 if (conf)
1521 hostapd_config_free(conf);
1522 if (hapd_iface) {
1523 os_free(hapd_iface->config_fname);
1524 os_free(hapd_iface->bss);
Dmitry Shmidt54605472013-11-08 11:10:19 -08001525 wpa_printf(MSG_DEBUG, "%s: free iface %p",
1526 __func__, hapd_iface);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001527 os_free(hapd_iface);
1528 }
1529 return NULL;
1530}
1531
1532
1533static int ifname_in_use(struct hapd_interfaces *interfaces, const char *ifname)
1534{
1535 size_t i, j;
1536
1537 for (i = 0; i < interfaces->count; i++) {
1538 struct hostapd_iface *iface = interfaces->iface[i];
1539 for (j = 0; j < iface->num_bss; j++) {
1540 struct hostapd_data *hapd = iface->bss[j];
1541 if (os_strcmp(ifname, hapd->conf->iface) == 0)
1542 return 1;
1543 }
1544 }
1545
1546 return 0;
1547}
1548
1549
1550/**
1551 * hostapd_interface_init_bss - Read configuration file and init BSS data
1552 *
1553 * This function is used to parse configuration file for a BSS. This BSS is
1554 * added to an existing interface sharing the same radio (if any) or a new
1555 * interface is created if this is the first interface on a radio. This
1556 * allocate memory for the BSS. No actual driver operations are started.
1557 *
1558 * This is similar to hostapd_interface_init(), but for a case where the
1559 * configuration is used to add a single BSS instead of all BSSes for a radio.
1560 */
1561struct hostapd_iface *
1562hostapd_interface_init_bss(struct hapd_interfaces *interfaces, const char *phy,
1563 const char *config_fname, int debug)
1564{
1565 struct hostapd_iface *new_iface = NULL, *iface = NULL;
1566 struct hostapd_data *hapd;
1567 int k;
1568 size_t i, bss_idx;
1569
1570 if (!phy || !*phy)
1571 return NULL;
1572
1573 for (i = 0; i < interfaces->count; i++) {
1574 if (os_strcmp(interfaces->iface[i]->phy, phy) == 0) {
1575 iface = interfaces->iface[i];
1576 break;
1577 }
1578 }
1579
1580 wpa_printf(MSG_INFO, "Configuration file: %s (phy %s)%s",
1581 config_fname, phy, iface ? "" : " --> new PHY");
1582 if (iface) {
1583 struct hostapd_config *conf;
1584 struct hostapd_bss_config **tmp_conf;
1585 struct hostapd_data **tmp_bss;
1586 struct hostapd_bss_config *bss;
1587 const char *ifname;
1588
1589 /* Add new BSS to existing iface */
1590 conf = interfaces->config_read_cb(config_fname);
1591 if (conf == NULL)
1592 return NULL;
1593 if (conf->num_bss > 1) {
1594 wpa_printf(MSG_ERROR, "Multiple BSSes specified in BSS-config");
1595 hostapd_config_free(conf);
1596 return NULL;
1597 }
1598
1599 ifname = conf->bss[0]->iface;
1600 if (ifname[0] != '\0' && ifname_in_use(interfaces, ifname)) {
1601 wpa_printf(MSG_ERROR,
1602 "Interface name %s already in use", ifname);
1603 hostapd_config_free(conf);
1604 return NULL;
1605 }
1606
1607 tmp_conf = os_realloc_array(
1608 iface->conf->bss, iface->conf->num_bss + 1,
1609 sizeof(struct hostapd_bss_config *));
1610 tmp_bss = os_realloc_array(iface->bss, iface->num_bss + 1,
1611 sizeof(struct hostapd_data *));
1612 if (tmp_bss)
1613 iface->bss = tmp_bss;
1614 if (tmp_conf) {
1615 iface->conf->bss = tmp_conf;
1616 iface->conf->last_bss = tmp_conf[0];
1617 }
1618 if (tmp_bss == NULL || tmp_conf == NULL) {
1619 hostapd_config_free(conf);
1620 return NULL;
1621 }
1622 bss = iface->conf->bss[iface->conf->num_bss] = conf->bss[0];
1623 iface->conf->num_bss++;
1624
1625 hapd = hostapd_alloc_bss_data(iface, iface->conf, bss);
1626 if (hapd == NULL) {
1627 iface->conf->num_bss--;
1628 hostapd_config_free(conf);
1629 return NULL;
1630 }
1631 iface->conf->last_bss = bss;
1632 iface->bss[iface->num_bss] = hapd;
1633 hapd->msg_ctx = hapd;
1634
1635 bss_idx = iface->num_bss++;
1636 conf->num_bss--;
1637 conf->bss[0] = NULL;
1638 hostapd_config_free(conf);
1639 } else {
1640 /* Add a new iface with the first BSS */
1641 new_iface = iface = hostapd_init(interfaces, config_fname);
1642 if (!iface)
1643 return NULL;
1644 os_strlcpy(iface->phy, phy, sizeof(iface->phy));
1645 iface->interfaces = interfaces;
1646 bss_idx = 0;
1647 }
1648
1649 for (k = 0; k < debug; k++) {
1650 if (iface->bss[bss_idx]->conf->logger_stdout_level > 0)
1651 iface->bss[bss_idx]->conf->logger_stdout_level--;
1652 }
1653
1654 if (iface->conf->bss[bss_idx]->iface[0] == '\0' &&
1655 !hostapd_drv_none(iface->bss[bss_idx])) {
1656 wpa_printf(MSG_ERROR, "Interface name not specified in %s",
1657 config_fname);
1658 if (new_iface)
1659 hostapd_interface_deinit_free(new_iface);
1660 return NULL;
1661 }
1662
1663 return iface;
1664}
1665
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001666
1667void hostapd_interface_deinit_free(struct hostapd_iface *iface)
1668{
1669 const struct wpa_driver_ops *driver;
1670 void *drv_priv;
Dmitry Shmidt54605472013-11-08 11:10:19 -08001671
1672 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001673 if (iface == NULL)
1674 return;
Dmitry Shmidt54605472013-11-08 11:10:19 -08001675 wpa_printf(MSG_DEBUG, "%s: num_bss=%u conf->num_bss=%u",
1676 __func__, (unsigned int) iface->num_bss,
1677 (unsigned int) iface->conf->num_bss);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001678 driver = iface->bss[0]->driver;
1679 drv_priv = iface->bss[0]->drv_priv;
1680 hostapd_interface_deinit(iface);
Dmitry Shmidt54605472013-11-08 11:10:19 -08001681 wpa_printf(MSG_DEBUG, "%s: driver=%p drv_priv=%p -> hapd_deinit",
1682 __func__, driver, drv_priv);
Dmitry Shmidt71757432014-06-02 13:50:35 -07001683 if (driver && driver->hapd_deinit && drv_priv) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001684 driver->hapd_deinit(drv_priv);
Dmitry Shmidt71757432014-06-02 13:50:35 -07001685 iface->bss[0]->drv_priv = NULL;
1686 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001687 hostapd_interface_free(iface);
1688}
1689
1690
Dmitry Shmidt15907092014-03-25 10:42:57 -07001691static void hostapd_deinit_driver(const struct wpa_driver_ops *driver,
1692 void *drv_priv,
1693 struct hostapd_iface *hapd_iface)
1694{
1695 size_t j;
1696
1697 wpa_printf(MSG_DEBUG, "%s: driver=%p drv_priv=%p -> hapd_deinit",
1698 __func__, driver, drv_priv);
1699 if (driver && driver->hapd_deinit && drv_priv) {
1700 driver->hapd_deinit(drv_priv);
1701 for (j = 0; j < hapd_iface->num_bss; j++) {
1702 wpa_printf(MSG_DEBUG, "%s:bss[%d]->drv_priv=%p",
1703 __func__, (int) j,
1704 hapd_iface->bss[j]->drv_priv);
1705 if (hapd_iface->bss[j]->drv_priv == drv_priv)
1706 hapd_iface->bss[j]->drv_priv = NULL;
1707 }
1708 }
1709}
1710
1711
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001712int hostapd_enable_iface(struct hostapd_iface *hapd_iface)
1713{
Dmitry Shmidt71757432014-06-02 13:50:35 -07001714 size_t j;
1715
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001716 if (hapd_iface->bss[0]->drv_priv != NULL) {
1717 wpa_printf(MSG_ERROR, "Interface %s already enabled",
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001718 hapd_iface->conf->bss[0]->iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001719 return -1;
1720 }
1721
1722 wpa_printf(MSG_DEBUG, "Enable interface %s",
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001723 hapd_iface->conf->bss[0]->iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001724
Dmitry Shmidt71757432014-06-02 13:50:35 -07001725 for (j = 0; j < hapd_iface->num_bss; j++)
1726 hostapd_set_security_params(hapd_iface->conf->bss[j], 1);
Dmitry Shmidt344abd32014-01-14 13:17:00 -08001727 if (hostapd_config_check(hapd_iface->conf, 1) < 0) {
1728 wpa_printf(MSG_INFO, "Invalid configuration - cannot enable");
1729 return -1;
1730 }
1731
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001732 if (hapd_iface->interfaces == NULL ||
1733 hapd_iface->interfaces->driver_init == NULL ||
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001734 hapd_iface->interfaces->driver_init(hapd_iface))
1735 return -1;
1736
1737 if (hostapd_setup_interface(hapd_iface)) {
Dmitry Shmidt15907092014-03-25 10:42:57 -07001738 hostapd_deinit_driver(hapd_iface->bss[0]->driver,
1739 hapd_iface->bss[0]->drv_priv,
1740 hapd_iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001741 return -1;
1742 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001743
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001744 return 0;
1745}
1746
1747
1748int hostapd_reload_iface(struct hostapd_iface *hapd_iface)
1749{
1750 size_t j;
1751
1752 wpa_printf(MSG_DEBUG, "Reload interface %s",
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001753 hapd_iface->conf->bss[0]->iface);
1754 for (j = 0; j < hapd_iface->num_bss; j++)
Dmitry Shmidt71757432014-06-02 13:50:35 -07001755 hostapd_set_security_params(hapd_iface->conf->bss[j], 1);
Dmitry Shmidt344abd32014-01-14 13:17:00 -08001756 if (hostapd_config_check(hapd_iface->conf, 1) < 0) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001757 wpa_printf(MSG_ERROR, "Updated configuration is invalid");
1758 return -1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001759 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001760 hostapd_clear_old(hapd_iface);
1761 for (j = 0; j < hapd_iface->num_bss; j++)
1762 hostapd_reload_bss(hapd_iface->bss[j]);
1763
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001764 return 0;
1765}
1766
1767
1768int hostapd_disable_iface(struct hostapd_iface *hapd_iface)
1769{
1770 size_t j;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001771 const struct wpa_driver_ops *driver;
1772 void *drv_priv;
1773
1774 if (hapd_iface == NULL)
1775 return -1;
Dmitry Shmidt71757432014-06-02 13:50:35 -07001776
1777 if (hapd_iface->bss[0]->drv_priv == NULL) {
1778 wpa_printf(MSG_INFO, "Interface %s already disabled",
1779 hapd_iface->conf->bss[0]->iface);
1780 return -1;
1781 }
1782
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001783 wpa_msg(hapd_iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001784 driver = hapd_iface->bss[0]->driver;
1785 drv_priv = hapd_iface->bss[0]->drv_priv;
1786
Dmitry Shmidta38abf92014-03-06 13:38:44 -08001787 hapd_iface->driver_ap_teardown =
1788 !!(hapd_iface->drv_flags &
1789 WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
1790
1791 /* same as hostapd_interface_deinit without deinitializing ctrl-iface */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001792 for (j = 0; j < hapd_iface->num_bss; j++) {
1793 struct hostapd_data *hapd = hapd_iface->bss[j];
Dmitry Shmidt71757432014-06-02 13:50:35 -07001794 hostapd_bss_deinit_no_free(hapd);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001795 hostapd_free_hapd_data(hapd);
1796 }
1797
Dmitry Shmidt15907092014-03-25 10:42:57 -07001798 hostapd_deinit_driver(driver, drv_priv, hapd_iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001799
1800 /* From hostapd_cleanup_iface: These were initialized in
1801 * hostapd_setup_interface and hostapd_setup_interface_complete
1802 */
1803 hostapd_cleanup_iface_partial(hapd_iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001804
Dmitry Shmidt56052862013-10-04 10:23:25 -07001805 wpa_printf(MSG_DEBUG, "Interface %s disabled",
1806 hapd_iface->bss[0]->conf->iface);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001807 hostapd_set_state(hapd_iface, HAPD_IFACE_DISABLED);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001808 return 0;
1809}
1810
1811
1812static struct hostapd_iface *
1813hostapd_iface_alloc(struct hapd_interfaces *interfaces)
1814{
1815 struct hostapd_iface **iface, *hapd_iface;
1816
1817 iface = os_realloc_array(interfaces->iface, interfaces->count + 1,
1818 sizeof(struct hostapd_iface *));
1819 if (iface == NULL)
1820 return NULL;
1821 interfaces->iface = iface;
1822 hapd_iface = interfaces->iface[interfaces->count] =
1823 os_zalloc(sizeof(*hapd_iface));
1824 if (hapd_iface == NULL) {
1825 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory for "
1826 "the interface", __func__);
1827 return NULL;
1828 }
1829 interfaces->count++;
1830 hapd_iface->interfaces = interfaces;
1831
1832 return hapd_iface;
1833}
1834
1835
1836static struct hostapd_config *
1837hostapd_config_alloc(struct hapd_interfaces *interfaces, const char *ifname,
1838 const char *ctrl_iface)
1839{
1840 struct hostapd_bss_config *bss;
1841 struct hostapd_config *conf;
1842
1843 /* Allocates memory for bss and conf */
1844 conf = hostapd_config_defaults();
1845 if (conf == NULL) {
1846 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory for "
1847 "configuration", __func__);
1848 return NULL;
1849 }
1850
1851 conf->driver = wpa_drivers[0];
1852 if (conf->driver == NULL) {
1853 wpa_printf(MSG_ERROR, "No driver wrappers registered!");
1854 hostapd_config_free(conf);
1855 return NULL;
1856 }
1857
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001858 bss = conf->last_bss = conf->bss[0];
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001859
1860 os_strlcpy(bss->iface, ifname, sizeof(bss->iface));
1861 bss->ctrl_interface = os_strdup(ctrl_iface);
1862 if (bss->ctrl_interface == NULL) {
1863 hostapd_config_free(conf);
1864 return NULL;
1865 }
1866
1867 /* Reading configuration file skipped, will be done in SET!
1868 * From reading the configuration till the end has to be done in
1869 * SET
1870 */
1871 return conf;
1872}
1873
1874
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001875static int hostapd_data_alloc(struct hostapd_iface *hapd_iface,
1876 struct hostapd_config *conf)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001877{
1878 size_t i;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001879 struct hostapd_data *hapd;
1880
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001881 hapd_iface->bss = os_calloc(conf->num_bss,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001882 sizeof(struct hostapd_data *));
1883 if (hapd_iface->bss == NULL)
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001884 return -1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001885
1886 for (i = 0; i < conf->num_bss; i++) {
1887 hapd = hapd_iface->bss[i] =
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001888 hostapd_alloc_bss_data(hapd_iface, conf, conf->bss[i]);
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001889 if (hapd == NULL) {
1890 while (i > 0) {
1891 i--;
1892 os_free(hapd_iface->bss[i]);
1893 hapd_iface->bss[i] = NULL;
1894 }
1895 os_free(hapd_iface->bss);
1896 hapd_iface->bss = NULL;
1897 return -1;
1898 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001899 hapd->msg_ctx = hapd;
1900 }
1901
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001902 hapd_iface->conf = conf;
1903 hapd_iface->num_bss = conf->num_bss;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001904
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001905 return 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001906}
1907
1908
1909int hostapd_add_iface(struct hapd_interfaces *interfaces, char *buf)
1910{
1911 struct hostapd_config *conf = NULL;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001912 struct hostapd_iface *hapd_iface = NULL, *new_iface = NULL;
1913 struct hostapd_data *hapd;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001914 char *ptr;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001915 size_t i, j;
1916 const char *conf_file = NULL, *phy_name = NULL;
1917
1918 if (os_strncmp(buf, "bss_config=", 11) == 0) {
1919 char *pos;
1920 phy_name = buf + 11;
1921 pos = os_strchr(phy_name, ':');
1922 if (!pos)
1923 return -1;
1924 *pos++ = '\0';
1925 conf_file = pos;
1926 if (!os_strlen(conf_file))
1927 return -1;
1928
1929 hapd_iface = hostapd_interface_init_bss(interfaces, phy_name,
1930 conf_file, 0);
1931 if (!hapd_iface)
1932 return -1;
1933 for (j = 0; j < interfaces->count; j++) {
1934 if (interfaces->iface[j] == hapd_iface)
1935 break;
1936 }
1937 if (j == interfaces->count) {
1938 struct hostapd_iface **tmp;
1939 tmp = os_realloc_array(interfaces->iface,
1940 interfaces->count + 1,
1941 sizeof(struct hostapd_iface *));
1942 if (!tmp) {
1943 hostapd_interface_deinit_free(hapd_iface);
1944 return -1;
1945 }
1946 interfaces->iface = tmp;
1947 interfaces->iface[interfaces->count++] = hapd_iface;
1948 new_iface = hapd_iface;
1949 }
1950
1951 if (new_iface) {
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001952 if (interfaces->driver_init(hapd_iface))
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001953 goto fail;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001954
1955 if (hostapd_setup_interface(hapd_iface)) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001956 hostapd_deinit_driver(
1957 hapd_iface->bss[0]->driver,
1958 hapd_iface->bss[0]->drv_priv,
1959 hapd_iface);
1960 goto fail;
1961 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001962 } else {
1963 /* Assign new BSS with bss[0]'s driver info */
1964 hapd = hapd_iface->bss[hapd_iface->num_bss - 1];
1965 hapd->driver = hapd_iface->bss[0]->driver;
1966 hapd->drv_priv = hapd_iface->bss[0]->drv_priv;
1967 os_memcpy(hapd->own_addr, hapd_iface->bss[0]->own_addr,
1968 ETH_ALEN);
1969
1970 if (start_ctrl_iface_bss(hapd) < 0 ||
Dmitry Shmidt54605472013-11-08 11:10:19 -08001971 (hapd_iface->state == HAPD_IFACE_ENABLED &&
1972 hostapd_setup_bss(hapd, -1))) {
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001973 hostapd_cleanup(hapd);
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08001974 hapd_iface->bss[hapd_iface->num_bss - 1] = NULL;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001975 hapd_iface->conf->num_bss--;
1976 hapd_iface->num_bss--;
Dmitry Shmidt54605472013-11-08 11:10:19 -08001977 wpa_printf(MSG_DEBUG, "%s: free hapd %p %s",
1978 __func__, hapd, hapd->conf->iface);
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001979 hostapd_config_free_bss(hapd->conf);
1980 hapd->conf = NULL;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001981 os_free(hapd);
1982 return -1;
1983 }
1984 }
1985 return 0;
1986 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001987
1988 ptr = os_strchr(buf, ' ');
1989 if (ptr == NULL)
1990 return -1;
1991 *ptr++ = '\0';
1992
Dmitry Shmidt56052862013-10-04 10:23:25 -07001993 if (os_strncmp(ptr, "config=", 7) == 0)
1994 conf_file = ptr + 7;
1995
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001996 for (i = 0; i < interfaces->count; i++) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001997 if (!os_strcmp(interfaces->iface[i]->conf->bss[0]->iface,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001998 buf)) {
1999 wpa_printf(MSG_INFO, "Cannot add interface - it "
2000 "already exists");
2001 return -1;
2002 }
2003 }
2004
2005 hapd_iface = hostapd_iface_alloc(interfaces);
2006 if (hapd_iface == NULL) {
2007 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
2008 "for interface", __func__);
2009 goto fail;
2010 }
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002011 new_iface = hapd_iface;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002012
Dmitry Shmidt56052862013-10-04 10:23:25 -07002013 if (conf_file && interfaces->config_read_cb) {
2014 conf = interfaces->config_read_cb(conf_file);
2015 if (conf && conf->bss)
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002016 os_strlcpy(conf->bss[0]->iface, buf,
2017 sizeof(conf->bss[0]->iface));
Dmitry Shmidt56052862013-10-04 10:23:25 -07002018 } else
2019 conf = hostapd_config_alloc(interfaces, buf, ptr);
2020 if (conf == NULL || conf->bss == NULL) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002021 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
2022 "for configuration", __func__);
2023 goto fail;
2024 }
2025
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002026 if (hostapd_data_alloc(hapd_iface, conf) < 0) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002027 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
2028 "for hostapd", __func__);
2029 goto fail;
2030 }
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002031 conf = NULL;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002032
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002033 if (start_ctrl_iface(hapd_iface) < 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002034 goto fail;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002035
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002036 wpa_printf(MSG_INFO, "Add interface '%s'",
2037 hapd_iface->conf->bss[0]->iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002038
2039 return 0;
2040
2041fail:
2042 if (conf)
2043 hostapd_config_free(conf);
2044 if (hapd_iface) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002045 if (hapd_iface->bss) {
Dmitry Shmidt54605472013-11-08 11:10:19 -08002046 for (i = 0; i < hapd_iface->num_bss; i++) {
2047 hapd = hapd_iface->bss[i];
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08002048 if (!hapd)
2049 continue;
2050 if (hapd_iface->interfaces &&
Dmitry Shmidt54605472013-11-08 11:10:19 -08002051 hapd_iface->interfaces->ctrl_iface_deinit)
2052 hapd_iface->interfaces->
2053 ctrl_iface_deinit(hapd);
2054 wpa_printf(MSG_DEBUG, "%s: free hapd %p (%s)",
2055 __func__, hapd_iface->bss[i],
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08002056 hapd->conf->iface);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002057 hostapd_cleanup(hapd);
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08002058 os_free(hapd);
2059 hapd_iface->bss[i] = NULL;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002060 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002061 os_free(hapd_iface->bss);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002062 hapd_iface->bss = NULL;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002063 }
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002064 if (new_iface) {
2065 interfaces->count--;
2066 interfaces->iface[interfaces->count] = NULL;
2067 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002068 hostapd_cleanup_iface(hapd_iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002069 }
2070 return -1;
2071}
2072
2073
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002074static int hostapd_remove_bss(struct hostapd_iface *iface, unsigned int idx)
2075{
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002076 size_t i;
2077
Dmitry Shmidt54605472013-11-08 11:10:19 -08002078 wpa_printf(MSG_INFO, "Remove BSS '%s'", iface->conf->bss[idx]->iface);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002079
Dmitry Shmidt54605472013-11-08 11:10:19 -08002080 /* Remove hostapd_data only if it has already been initialized */
2081 if (idx < iface->num_bss) {
2082 struct hostapd_data *hapd = iface->bss[idx];
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002083
Dmitry Shmidt54605472013-11-08 11:10:19 -08002084 hostapd_bss_deinit(hapd);
2085 wpa_printf(MSG_DEBUG, "%s: free hapd %p (%s)",
2086 __func__, hapd, hapd->conf->iface);
2087 hostapd_config_free_bss(hapd->conf);
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002088 hapd->conf = NULL;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002089 os_free(hapd);
2090
2091 iface->num_bss--;
2092
2093 for (i = idx; i < iface->num_bss; i++)
2094 iface->bss[i] = iface->bss[i + 1];
2095 } else {
2096 hostapd_config_free_bss(iface->conf->bss[idx]);
2097 iface->conf->bss[idx] = NULL;
2098 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002099
2100 iface->conf->num_bss--;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002101 for (i = idx; i < iface->conf->num_bss; i++)
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002102 iface->conf->bss[i] = iface->conf->bss[i + 1];
2103
2104 return 0;
2105}
2106
2107
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002108int hostapd_remove_iface(struct hapd_interfaces *interfaces, char *buf)
2109{
2110 struct hostapd_iface *hapd_iface;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002111 size_t i, j, k = 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002112
2113 for (i = 0; i < interfaces->count; i++) {
2114 hapd_iface = interfaces->iface[i];
2115 if (hapd_iface == NULL)
2116 return -1;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002117 if (!os_strcmp(hapd_iface->conf->bss[0]->iface, buf)) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002118 wpa_printf(MSG_INFO, "Remove interface '%s'", buf);
Dmitry Shmidta38abf92014-03-06 13:38:44 -08002119 hapd_iface->driver_ap_teardown =
2120 !!(hapd_iface->drv_flags &
2121 WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
2122
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002123 hostapd_interface_deinit_free(hapd_iface);
2124 k = i;
2125 while (k < (interfaces->count - 1)) {
2126 interfaces->iface[k] =
2127 interfaces->iface[k + 1];
2128 k++;
2129 }
2130 interfaces->count--;
2131 return 0;
2132 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002133
2134 for (j = 0; j < hapd_iface->conf->num_bss; j++) {
Dmitry Shmidta38abf92014-03-06 13:38:44 -08002135 if (!os_strcmp(hapd_iface->conf->bss[j]->iface, buf)) {
2136 hapd_iface->driver_ap_teardown =
2137 !(hapd_iface->drv_flags &
2138 WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002139 return hostapd_remove_bss(hapd_iface, j);
Dmitry Shmidta38abf92014-03-06 13:38:44 -08002140 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002141 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002142 }
2143 return -1;
2144}
2145
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002146
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002147/**
2148 * hostapd_new_assoc_sta - Notify that a new station associated with the AP
2149 * @hapd: Pointer to BSS data
2150 * @sta: Pointer to the associated STA data
2151 * @reassoc: 1 to indicate this was a re-association; 0 = first association
2152 *
2153 * This function will be called whenever a station associates with the AP. It
2154 * can be called from ieee802_11.c for drivers that export MLME to hostapd and
2155 * from drv_callbacks.c based on driver events for drivers that take care of
2156 * management frames (IEEE 802.11 authentication and association) internally.
2157 */
2158void hostapd_new_assoc_sta(struct hostapd_data *hapd, struct sta_info *sta,
2159 int reassoc)
2160{
2161 if (hapd->tkip_countermeasures) {
2162 hostapd_drv_sta_deauth(hapd, sta->addr,
2163 WLAN_REASON_MICHAEL_MIC_FAILURE);
2164 return;
2165 }
2166
2167 hostapd_prune_associations(hapd, sta->addr);
2168
2169 /* IEEE 802.11F (IAPP) */
2170 if (hapd->conf->ieee802_11f)
2171 iapp_new_station(hapd->iapp, sta);
2172
2173#ifdef CONFIG_P2P
2174 if (sta->p2p_ie == NULL && !sta->no_p2p_set) {
2175 sta->no_p2p_set = 1;
2176 hapd->num_sta_no_p2p++;
2177 if (hapd->num_sta_no_p2p == 1)
2178 hostapd_p2p_non_p2p_sta_connected(hapd);
2179 }
2180#endif /* CONFIG_P2P */
2181
2182 /* Start accounting here, if IEEE 802.1X and WPA are not used.
2183 * IEEE 802.1X/WPA code will start accounting after the station has
2184 * been authorized. */
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -08002185 if (!hapd->conf->ieee802_1x && !hapd->conf->wpa && !hapd->conf->osen) {
2186 ap_sta_set_authorized(hapd, sta, 1);
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002187 os_get_reltime(&sta->connected_time);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002188 accounting_sta_start(hapd, sta);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002189 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002190
2191 /* Start IEEE 802.1X authentication process for new stations */
2192 ieee802_1x_new_station(hapd, sta);
2193 if (reassoc) {
2194 if (sta->auth_alg != WLAN_AUTH_FT &&
2195 !(sta->flags & (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS)))
2196 wpa_auth_sm_event(sta->wpa_sm, WPA_REAUTH);
2197 } else
2198 wpa_auth_sta_associated(hapd->wpa_auth, sta->wpa_sm);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002199
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08002200 if (!(hapd->iface->drv_flags & WPA_DRIVER_FLAGS_INACTIVITY_TIMER)) {
2201 wpa_printf(MSG_DEBUG, "%s: reschedule ap_handle_timer timeout "
2202 "for " MACSTR " (%d seconds - ap_max_inactivity)",
2203 __func__, MAC2STR(sta->addr),
2204 hapd->conf->ap_max_inactivity);
2205 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
2206 eloop_register_timeout(hapd->conf->ap_max_inactivity, 0,
2207 ap_handle_timer, hapd, sta);
2208 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002209}
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002210
2211
2212const char * hostapd_state_text(enum hostapd_iface_state s)
2213{
2214 switch (s) {
2215 case HAPD_IFACE_UNINITIALIZED:
2216 return "UNINITIALIZED";
2217 case HAPD_IFACE_DISABLED:
2218 return "DISABLED";
2219 case HAPD_IFACE_COUNTRY_UPDATE:
2220 return "COUNTRY_UPDATE";
2221 case HAPD_IFACE_ACS:
2222 return "ACS";
2223 case HAPD_IFACE_HT_SCAN:
2224 return "HT_SCAN";
2225 case HAPD_IFACE_DFS:
2226 return "DFS";
2227 case HAPD_IFACE_ENABLED:
2228 return "ENABLED";
2229 }
2230
2231 return "UNKNOWN";
2232}
2233
2234
2235void hostapd_set_state(struct hostapd_iface *iface, enum hostapd_iface_state s)
2236{
2237 wpa_printf(MSG_INFO, "%s: interface state %s->%s",
2238 iface->conf->bss[0]->iface, hostapd_state_text(iface->state),
2239 hostapd_state_text(s));
2240 iface->state = s;
2241}
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002242
2243
2244#ifdef NEED_AP_MLME
2245
2246static void free_beacon_data(struct beacon_data *beacon)
2247{
2248 os_free(beacon->head);
2249 beacon->head = NULL;
2250 os_free(beacon->tail);
2251 beacon->tail = NULL;
2252 os_free(beacon->probe_resp);
2253 beacon->probe_resp = NULL;
2254 os_free(beacon->beacon_ies);
2255 beacon->beacon_ies = NULL;
2256 os_free(beacon->proberesp_ies);
2257 beacon->proberesp_ies = NULL;
2258 os_free(beacon->assocresp_ies);
2259 beacon->assocresp_ies = NULL;
2260}
2261
2262
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07002263static int hostapd_build_beacon_data(struct hostapd_data *hapd,
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002264 struct beacon_data *beacon)
2265{
2266 struct wpabuf *beacon_extra, *proberesp_extra, *assocresp_extra;
2267 struct wpa_driver_ap_params params;
2268 int ret;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002269
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08002270 os_memset(beacon, 0, sizeof(*beacon));
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002271 ret = ieee802_11_build_ap_params(hapd, &params);
2272 if (ret < 0)
2273 return ret;
2274
2275 ret = hostapd_build_ap_extra_ies(hapd, &beacon_extra,
2276 &proberesp_extra,
2277 &assocresp_extra);
2278 if (ret)
2279 goto free_ap_params;
2280
2281 ret = -1;
2282 beacon->head = os_malloc(params.head_len);
2283 if (!beacon->head)
2284 goto free_ap_extra_ies;
2285
2286 os_memcpy(beacon->head, params.head, params.head_len);
2287 beacon->head_len = params.head_len;
2288
2289 beacon->tail = os_malloc(params.tail_len);
2290 if (!beacon->tail)
2291 goto free_beacon;
2292
2293 os_memcpy(beacon->tail, params.tail, params.tail_len);
2294 beacon->tail_len = params.tail_len;
2295
2296 if (params.proberesp != NULL) {
2297 beacon->probe_resp = os_malloc(params.proberesp_len);
2298 if (!beacon->probe_resp)
2299 goto free_beacon;
2300
2301 os_memcpy(beacon->probe_resp, params.proberesp,
2302 params.proberesp_len);
2303 beacon->probe_resp_len = params.proberesp_len;
2304 }
2305
2306 /* copy the extra ies */
2307 if (beacon_extra) {
2308 beacon->beacon_ies = os_malloc(wpabuf_len(beacon_extra));
2309 if (!beacon->beacon_ies)
2310 goto free_beacon;
2311
2312 os_memcpy(beacon->beacon_ies,
2313 beacon_extra->buf, wpabuf_len(beacon_extra));
2314 beacon->beacon_ies_len = wpabuf_len(beacon_extra);
2315 }
2316
2317 if (proberesp_extra) {
2318 beacon->proberesp_ies =
2319 os_malloc(wpabuf_len(proberesp_extra));
2320 if (!beacon->proberesp_ies)
2321 goto free_beacon;
2322
2323 os_memcpy(beacon->proberesp_ies, proberesp_extra->buf,
2324 wpabuf_len(proberesp_extra));
2325 beacon->proberesp_ies_len = wpabuf_len(proberesp_extra);
2326 }
2327
2328 if (assocresp_extra) {
2329 beacon->assocresp_ies =
2330 os_malloc(wpabuf_len(assocresp_extra));
2331 if (!beacon->assocresp_ies)
2332 goto free_beacon;
2333
2334 os_memcpy(beacon->assocresp_ies, assocresp_extra->buf,
2335 wpabuf_len(assocresp_extra));
2336 beacon->assocresp_ies_len = wpabuf_len(assocresp_extra);
2337 }
2338
2339 ret = 0;
2340free_beacon:
2341 /* if the function fails, the caller should not free beacon data */
2342 if (ret)
2343 free_beacon_data(beacon);
2344
2345free_ap_extra_ies:
2346 hostapd_free_ap_extra_ies(hapd, beacon_extra, proberesp_extra,
2347 assocresp_extra);
2348free_ap_params:
2349 ieee802_11_free_ap_params(&params);
2350 return ret;
2351}
2352
2353
2354/*
2355 * TODO: This flow currently supports only changing frequency within the
2356 * same hw_mode. Any other changes to MAC parameters or provided settings (even
2357 * width) are not supported.
2358 */
2359static int hostapd_change_config_freq(struct hostapd_data *hapd,
2360 struct hostapd_config *conf,
2361 struct hostapd_freq_params *params,
2362 struct hostapd_freq_params *old_params)
2363{
2364 int channel;
2365
2366 if (!params->channel) {
2367 /* check if the new channel is supported by hw */
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07002368 params->channel = hostapd_hw_get_channel(hapd, params->freq);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002369 }
2370
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07002371 channel = params->channel;
2372 if (!channel)
2373 return -1;
2374
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002375 /* if a pointer to old_params is provided we save previous state */
2376 if (old_params) {
2377 old_params->channel = conf->channel;
2378 old_params->ht_enabled = conf->ieee80211n;
2379 old_params->sec_channel_offset = conf->secondary_channel;
2380 }
2381
2382 conf->channel = channel;
2383 conf->ieee80211n = params->ht_enabled;
2384 conf->secondary_channel = params->sec_channel_offset;
2385
2386 /* TODO: maybe call here hostapd_config_check here? */
2387
2388 return 0;
2389}
2390
2391
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07002392static int hostapd_fill_csa_settings(struct hostapd_data *hapd,
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002393 struct csa_settings *settings)
2394{
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07002395 struct hostapd_iface *iface = hapd->iface;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002396 struct hostapd_freq_params old_freq;
2397 int ret;
2398
2399 os_memset(&old_freq, 0, sizeof(old_freq));
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07002400 if (!iface || !iface->freq || hapd->csa_in_progress)
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002401 return -1;
2402
2403 ret = hostapd_change_config_freq(iface->bss[0], iface->conf,
2404 &settings->freq_params,
2405 &old_freq);
2406 if (ret)
2407 return ret;
2408
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07002409 ret = hostapd_build_beacon_data(hapd, &settings->beacon_after);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002410
2411 /* change back the configuration */
2412 hostapd_change_config_freq(iface->bss[0], iface->conf,
2413 &old_freq, NULL);
2414
2415 if (ret)
2416 return ret;
2417
2418 /* set channel switch parameters for csa ie */
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07002419 hapd->cs_freq_params = settings->freq_params;
2420 hapd->cs_count = settings->cs_count;
2421 hapd->cs_block_tx = settings->block_tx;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002422
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07002423 ret = hostapd_build_beacon_data(hapd, &settings->beacon_csa);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002424 if (ret) {
2425 free_beacon_data(&settings->beacon_after);
2426 return ret;
2427 }
2428
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07002429 settings->counter_offset_beacon = hapd->cs_c_off_beacon;
2430 settings->counter_offset_presp = hapd->cs_c_off_proberesp;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002431
2432 return 0;
2433}
2434
2435
2436void hostapd_cleanup_cs_params(struct hostapd_data *hapd)
2437{
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07002438 os_memset(&hapd->cs_freq_params, 0, sizeof(hapd->cs_freq_params));
2439 hapd->cs_count = 0;
2440 hapd->cs_block_tx = 0;
2441 hapd->cs_c_off_beacon = 0;
2442 hapd->cs_c_off_proberesp = 0;
2443 hapd->csa_in_progress = 0;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002444}
2445
2446
2447int hostapd_switch_channel(struct hostapd_data *hapd,
2448 struct csa_settings *settings)
2449{
2450 int ret;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002451
2452 if (!(hapd->iface->drv_flags & WPA_DRIVER_FLAGS_AP_CSA)) {
2453 wpa_printf(MSG_INFO, "CSA is not supported");
2454 return -1;
2455 }
2456
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07002457 ret = hostapd_fill_csa_settings(hapd, settings);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002458 if (ret)
2459 return ret;
2460
2461 ret = hostapd_drv_switch_channel(hapd, settings);
2462 free_beacon_data(&settings->beacon_csa);
2463 free_beacon_data(&settings->beacon_after);
2464
2465 if (ret) {
2466 /* if we failed, clean cs parameters */
2467 hostapd_cleanup_cs_params(hapd);
2468 return ret;
2469 }
2470
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07002471 hapd->csa_in_progress = 1;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002472 return 0;
2473}
2474
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07002475
2476void
2477hostapd_switch_channel_fallback(struct hostapd_iface *iface,
2478 const struct hostapd_freq_params *freq_params)
2479{
2480 int vht_seg0_idx = 0, vht_seg1_idx = 0, vht_bw = VHT_CHANWIDTH_USE_HT;
2481 unsigned int i;
2482
2483 wpa_printf(MSG_DEBUG, "Restarting all CSA-related BSSes");
2484
2485 if (freq_params->center_freq1)
2486 vht_seg0_idx = 36 + (freq_params->center_freq1 - 5180) / 5;
2487 if (freq_params->center_freq2)
2488 vht_seg1_idx = 36 + (freq_params->center_freq2 - 5180) / 5;
2489
2490 switch (freq_params->bandwidth) {
2491 case 0:
2492 case 20:
2493 case 40:
2494 vht_bw = VHT_CHANWIDTH_USE_HT;
2495 break;
2496 case 80:
2497 if (freq_params->center_freq2)
2498 vht_bw = VHT_CHANWIDTH_80P80MHZ;
2499 else
2500 vht_bw = VHT_CHANWIDTH_80MHZ;
2501 break;
2502 case 160:
2503 vht_bw = VHT_CHANWIDTH_160MHZ;
2504 break;
2505 default:
2506 wpa_printf(MSG_WARNING, "Unknown CSA bandwidth: %d",
2507 freq_params->bandwidth);
2508 break;
2509 }
2510
2511 iface->freq = freq_params->freq;
2512 iface->conf->channel = freq_params->channel;
2513 iface->conf->secondary_channel = freq_params->sec_channel_offset;
2514 iface->conf->vht_oper_centr_freq_seg0_idx = vht_seg0_idx;
2515 iface->conf->vht_oper_centr_freq_seg1_idx = vht_seg1_idx;
2516 iface->conf->vht_oper_chwidth = vht_bw;
2517 iface->conf->ieee80211n = freq_params->ht_enabled;
2518 iface->conf->ieee80211ac = freq_params->vht_enabled;
2519
2520 /*
2521 * cs_params must not be cleared earlier because the freq_params
2522 * argument may actually point to one of these.
2523 */
2524 for (i = 0; i < iface->num_bss; i++)
2525 hostapd_cleanup_cs_params(iface->bss[i]);
2526
2527 hostapd_disable_iface(iface);
2528 hostapd_enable_iface(iface);
2529}
2530
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002531#endif /* NEED_AP_MLME */