blob: 5abe5edd894d5b236d59cd6df108f066f0e54728 [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 Shmidt2f74e362015-01-21 13:19:05 -080018#include "eapol_auth/eapol_auth_sm.h"
19#include "eapol_auth/eapol_auth_sm_i.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070020#include "hostapd.h"
21#include "authsrv.h"
22#include "sta_info.h"
23#include "accounting.h"
24#include "ap_list.h"
25#include "beacon.h"
26#include "iapp.h"
27#include "ieee802_1x.h"
28#include "ieee802_11_auth.h"
29#include "vlan_init.h"
30#include "wpa_auth.h"
31#include "wps_hostapd.h"
32#include "hw_features.h"
33#include "wpa_auth_glue.h"
34#include "ap_drv_ops.h"
35#include "ap_config.h"
36#include "p2p_hostapd.h"
Dmitry Shmidt04949592012-07-19 12:16:46 -070037#include "gas_serv.h"
Dmitry Shmidt051af732013-10-22 13:52:46 -070038#include "dfs.h"
Dmitry Shmidt7832adb2014-04-29 10:53:02 -070039#include "ieee802_11.h"
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080040#include "bss_load.h"
41#include "x_snoop.h"
42#include "dhcp_snoop.h"
43#include "ndisc_snoop.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070044
45
Dmitry Shmidt04949592012-07-19 12:16:46 -070046static int hostapd_flush_old_stations(struct hostapd_data *hapd, u16 reason);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070047static int hostapd_setup_encryption(char *iface, struct hostapd_data *hapd);
Dmitry Shmidtc55524a2011-07-07 11:18:38 -070048static int hostapd_broadcast_wep_clear(struct hostapd_data *hapd);
Dmitry Shmidtcce06662013-11-04 18:44:24 -080049static int setup_interface2(struct hostapd_iface *iface);
50static void channel_list_update_timeout(void *eloop_ctx, void *timeout_ctx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070051
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070052
Dmitry Shmidt04949592012-07-19 12:16:46 -070053int hostapd_for_each_interface(struct hapd_interfaces *interfaces,
54 int (*cb)(struct hostapd_iface *iface,
55 void *ctx), void *ctx)
56{
57 size_t i;
58 int ret;
59
60 for (i = 0; i < interfaces->count; i++) {
61 ret = cb(interfaces->iface[i], ctx);
62 if (ret)
63 return ret;
64 }
65
66 return 0;
67}
68
69
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070070static void hostapd_reload_bss(struct hostapd_data *hapd)
71{
Dmitry Shmidtcce06662013-11-04 18:44:24 -080072 struct hostapd_ssid *ssid;
73
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070074#ifndef CONFIG_NO_RADIUS
75 radius_client_reconfig(hapd->radius, hapd->conf->radius);
76#endif /* CONFIG_NO_RADIUS */
77
Dmitry Shmidtcce06662013-11-04 18:44:24 -080078 ssid = &hapd->conf->ssid;
79 if (!ssid->wpa_psk_set && ssid->wpa_psk && !ssid->wpa_psk->next &&
80 ssid->wpa_passphrase_set && ssid->wpa_passphrase) {
81 /*
82 * Force PSK to be derived again since SSID or passphrase may
83 * have changed.
84 */
Dmitry Shmidt7f656022015-02-25 14:36:37 -080085 hostapd_config_clear_wpa_psk(&hapd->conf->ssid.wpa_psk);
Dmitry Shmidtcce06662013-11-04 18:44:24 -080086 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070087 if (hostapd_setup_wpa_psk(hapd->conf)) {
88 wpa_printf(MSG_ERROR, "Failed to re-configure WPA PSK "
89 "after reloading configuration");
90 }
91
92 if (hapd->conf->ieee802_1x || hapd->conf->wpa)
93 hostapd_set_drv_ieee8021x(hapd, hapd->conf->iface, 1);
94 else
95 hostapd_set_drv_ieee8021x(hapd, hapd->conf->iface, 0);
96
Dmitry Shmidtf21452a2014-02-26 10:55:25 -080097 if ((hapd->conf->wpa || hapd->conf->osen) && hapd->wpa_auth == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070098 hostapd_setup_wpa(hapd);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080099 if (hapd->wpa_auth)
100 wpa_init_keys(hapd->wpa_auth);
101 } else if (hapd->conf->wpa) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700102 const u8 *wpa_ie;
103 size_t wpa_ie_len;
104 hostapd_reconfig_wpa(hapd);
105 wpa_ie = wpa_auth_get_wpa_ie(hapd->wpa_auth, &wpa_ie_len);
106 if (hostapd_set_generic_elem(hapd, wpa_ie, wpa_ie_len))
107 wpa_printf(MSG_ERROR, "Failed to configure WPA IE for "
108 "the kernel driver.");
109 } else if (hapd->wpa_auth) {
110 wpa_deinit(hapd->wpa_auth);
111 hapd->wpa_auth = NULL;
112 hostapd_set_privacy(hapd, 0);
113 hostapd_setup_encryption(hapd->conf->iface, hapd);
114 hostapd_set_generic_elem(hapd, (u8 *) "", 0);
115 }
116
117 ieee802_11_set_beacon(hapd);
118 hostapd_update_wps(hapd);
119
120 if (hapd->conf->ssid.ssid_set &&
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700121 hostapd_set_ssid(hapd, hapd->conf->ssid.ssid,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700122 hapd->conf->ssid.ssid_len)) {
123 wpa_printf(MSG_ERROR, "Could not set SSID for kernel driver");
124 /* try to continue */
125 }
126 wpa_printf(MSG_DEBUG, "Reconfigured interface %s", hapd->conf->iface);
127}
128
129
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700130static void hostapd_clear_old(struct hostapd_iface *iface)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700131{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700132 size_t j;
133
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700134 /*
135 * Deauthenticate all stations since the new configuration may not
136 * allow them to use the BSS anymore.
137 */
138 for (j = 0; j < iface->num_bss; j++) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700139 hostapd_flush_old_stations(iface->bss[j],
140 WLAN_REASON_PREV_AUTH_NOT_VALID);
Dmitry Shmidtc55524a2011-07-07 11:18:38 -0700141 hostapd_broadcast_wep_clear(iface->bss[j]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700142
143#ifndef CONFIG_NO_RADIUS
144 /* TODO: update dynamic data based on changed configuration
145 * items (e.g., open/close sockets, etc.) */
146 radius_client_flush(iface->bss[j]->radius, 0);
147#endif /* CONFIG_NO_RADIUS */
148 }
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700149}
150
151
152int hostapd_reload_config(struct hostapd_iface *iface)
153{
154 struct hostapd_data *hapd = iface->bss[0];
155 struct hostapd_config *newconf, *oldconf;
156 size_t j;
157
158 if (iface->config_fname == NULL) {
159 /* Only in-memory config in use - assume it has been updated */
160 hostapd_clear_old(iface);
161 for (j = 0; j < iface->num_bss; j++)
162 hostapd_reload_bss(iface->bss[j]);
163 return 0;
164 }
165
166 if (iface->interfaces == NULL ||
167 iface->interfaces->config_read_cb == NULL)
168 return -1;
169 newconf = iface->interfaces->config_read_cb(iface->config_fname);
170 if (newconf == NULL)
171 return -1;
172
173 hostapd_clear_old(iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700174
175 oldconf = hapd->iconf;
176 iface->conf = newconf;
177
178 for (j = 0; j < iface->num_bss; j++) {
179 hapd = iface->bss[j];
180 hapd->iconf = newconf;
Dmitry Shmidtd11f0192014-03-24 12:09:47 -0700181 hapd->iconf->channel = oldconf->channel;
Dmitry Shmidtdda10c22015-03-24 16:05:01 -0700182 hapd->iconf->acs = oldconf->acs;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -0700183 hapd->iconf->secondary_channel = oldconf->secondary_channel;
Dmitry Shmidtd11f0192014-03-24 12:09:47 -0700184 hapd->iconf->ieee80211n = oldconf->ieee80211n;
185 hapd->iconf->ieee80211ac = oldconf->ieee80211ac;
186 hapd->iconf->ht_capab = oldconf->ht_capab;
187 hapd->iconf->vht_capab = oldconf->vht_capab;
188 hapd->iconf->vht_oper_chwidth = oldconf->vht_oper_chwidth;
189 hapd->iconf->vht_oper_centr_freq_seg0_idx =
190 oldconf->vht_oper_centr_freq_seg0_idx;
191 hapd->iconf->vht_oper_centr_freq_seg1_idx =
192 oldconf->vht_oper_centr_freq_seg1_idx;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800193 hapd->conf = newconf->bss[j];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700194 hostapd_reload_bss(hapd);
195 }
196
197 hostapd_config_free(oldconf);
198
199
200 return 0;
201}
202
203
204static void hostapd_broadcast_key_clear_iface(struct hostapd_data *hapd,
205 char *ifname)
206{
207 int i;
208
209 for (i = 0; i < NUM_WEP_KEYS; i++) {
210 if (hostapd_drv_set_key(ifname, hapd, WPA_ALG_NONE, NULL, i,
211 0, NULL, 0, NULL, 0)) {
212 wpa_printf(MSG_DEBUG, "Failed to clear default "
213 "encryption keys (ifname=%s keyidx=%d)",
214 ifname, i);
215 }
216 }
217#ifdef CONFIG_IEEE80211W
218 if (hapd->conf->ieee80211w) {
219 for (i = NUM_WEP_KEYS; i < NUM_WEP_KEYS + 2; i++) {
220 if (hostapd_drv_set_key(ifname, hapd, WPA_ALG_NONE,
221 NULL, i, 0, NULL,
222 0, NULL, 0)) {
223 wpa_printf(MSG_DEBUG, "Failed to clear "
224 "default mgmt encryption keys "
225 "(ifname=%s keyidx=%d)", ifname, i);
226 }
227 }
228 }
229#endif /* CONFIG_IEEE80211W */
230}
231
232
233static int hostapd_broadcast_wep_clear(struct hostapd_data *hapd)
234{
235 hostapd_broadcast_key_clear_iface(hapd, hapd->conf->iface);
236 return 0;
237}
238
239
240static int hostapd_broadcast_wep_set(struct hostapd_data *hapd)
241{
242 int errors = 0, idx;
243 struct hostapd_ssid *ssid = &hapd->conf->ssid;
244
245 idx = ssid->wep.idx;
246 if (ssid->wep.default_len &&
247 hostapd_drv_set_key(hapd->conf->iface,
248 hapd, WPA_ALG_WEP, broadcast_ether_addr, idx,
249 1, NULL, 0, ssid->wep.key[idx],
250 ssid->wep.len[idx])) {
251 wpa_printf(MSG_WARNING, "Could not set WEP encryption.");
252 errors++;
253 }
254
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700255 return errors;
256}
257
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700258
Dmitry Shmidt04949592012-07-19 12:16:46 -0700259static void hostapd_free_hapd_data(struct hostapd_data *hapd)
260{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800261 os_free(hapd->probereq_cb);
262 hapd->probereq_cb = NULL;
263
264#ifdef CONFIG_P2P
265 wpabuf_free(hapd->p2p_beacon_ie);
266 hapd->p2p_beacon_ie = NULL;
267 wpabuf_free(hapd->p2p_probe_resp_ie);
268 hapd->p2p_probe_resp_ie = NULL;
269#endif /* CONFIG_P2P */
270
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -0800271 if (!hapd->started) {
272 wpa_printf(MSG_ERROR, "%s: Interface %s wasn't started",
273 __func__, hapd->conf->iface);
274 return;
275 }
276 hapd->started = 0;
277
Dmitry Shmidt54605472013-11-08 11:10:19 -0800278 wpa_printf(MSG_DEBUG, "%s(%s)", __func__, hapd->conf->iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700279 iapp_deinit(hapd->iapp);
280 hapd->iapp = NULL;
281 accounting_deinit(hapd);
282 hostapd_deinit_wpa(hapd);
283 vlan_deinit(hapd);
284 hostapd_acl_deinit(hapd);
285#ifndef CONFIG_NO_RADIUS
286 radius_client_deinit(hapd->radius);
287 hapd->radius = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700288 radius_das_deinit(hapd->radius_das);
289 hapd->radius_das = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700290#endif /* CONFIG_NO_RADIUS */
291
292 hostapd_deinit_wps(hapd);
293
294 authsrv_deinit(hapd);
295
Dmitry Shmidt71757432014-06-02 13:50:35 -0700296 if (hapd->interface_added) {
297 hapd->interface_added = 0;
298 if (hostapd_if_remove(hapd, WPA_IF_AP_BSS, hapd->conf->iface)) {
299 wpa_printf(MSG_WARNING,
300 "Failed to remove BSS interface %s",
301 hapd->conf->iface);
302 hapd->interface_added = 1;
303 } else {
304 /*
305 * Since this was a dynamically added interface, the
306 * driver wrapper may have removed its internal instance
307 * and hapd->drv_priv is not valid anymore.
308 */
309 hapd->drv_priv = NULL;
310 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700311 }
312
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800313 wpabuf_free(hapd->time_adv);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700314
315#ifdef CONFIG_INTERWORKING
316 gas_serv_deinit(hapd);
317#endif /* CONFIG_INTERWORKING */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800318
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800319 bss_load_update_deinit(hapd);
320 ndisc_snoop_deinit(hapd);
321 dhcp_snoop_deinit(hapd);
322 x_snoop_deinit(hapd);
323
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800324#ifdef CONFIG_SQLITE
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700325 bin_clear_free(hapd->tmp_eap_user.identity,
326 hapd->tmp_eap_user.identity_len);
327 bin_clear_free(hapd->tmp_eap_user.password,
328 hapd->tmp_eap_user.password_len);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800329#endif /* CONFIG_SQLITE */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800330
331#ifdef CONFIG_MESH
332 wpabuf_free(hapd->mesh_pending_auth);
333 hapd->mesh_pending_auth = NULL;
334#endif /* CONFIG_MESH */
Dmitry Shmidt04949592012-07-19 12:16:46 -0700335}
336
337
338/**
339 * hostapd_cleanup - Per-BSS cleanup (deinitialization)
340 * @hapd: Pointer to BSS data
341 *
342 * This function is used to free all per-BSS data structures and resources.
Dmitry Shmidt54605472013-11-08 11:10:19 -0800343 * Most of the modules that are initialized in hostapd_setup_bss() are
344 * deinitialized here.
Dmitry Shmidt04949592012-07-19 12:16:46 -0700345 */
346static void hostapd_cleanup(struct hostapd_data *hapd)
347{
Dmitry Shmidt54605472013-11-08 11:10:19 -0800348 wpa_printf(MSG_DEBUG, "%s(hapd=%p (%s))", __func__, hapd,
349 hapd->conf->iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700350 if (hapd->iface->interfaces &&
351 hapd->iface->interfaces->ctrl_iface_deinit)
352 hapd->iface->interfaces->ctrl_iface_deinit(hapd);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700353 hostapd_free_hapd_data(hapd);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700354}
355
356
Dmitry Shmidt04949592012-07-19 12:16:46 -0700357static void hostapd_cleanup_iface_partial(struct hostapd_iface *iface)
358{
Dmitry Shmidt54605472013-11-08 11:10:19 -0800359 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800360#ifdef CONFIG_IEEE80211N
361#ifdef NEED_AP_MLME
362 hostapd_stop_setup_timers(iface);
363#endif /* NEED_AP_MLME */
364#endif /* CONFIG_IEEE80211N */
Dmitry Shmidt04949592012-07-19 12:16:46 -0700365 hostapd_free_hw_features(iface->hw_features, iface->num_hw_features);
366 iface->hw_features = NULL;
367 os_free(iface->current_rates);
368 iface->current_rates = NULL;
369 os_free(iface->basic_rates);
370 iface->basic_rates = NULL;
371 ap_list_deinit(iface);
372}
373
374
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700375/**
376 * hostapd_cleanup_iface - Complete per-interface cleanup
377 * @iface: Pointer to interface data
378 *
379 * This function is called after per-BSS data structures are deinitialized
380 * with hostapd_cleanup().
381 */
382static void hostapd_cleanup_iface(struct hostapd_iface *iface)
383{
Dmitry Shmidt54605472013-11-08 11:10:19 -0800384 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800385 eloop_cancel_timeout(channel_list_update_timeout, iface, NULL);
386
Dmitry Shmidt04949592012-07-19 12:16:46 -0700387 hostapd_cleanup_iface_partial(iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700388 hostapd_config_free(iface->conf);
389 iface->conf = NULL;
390
391 os_free(iface->config_fname);
392 os_free(iface->bss);
Dmitry Shmidt54605472013-11-08 11:10:19 -0800393 wpa_printf(MSG_DEBUG, "%s: free iface=%p", __func__, iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700394 os_free(iface);
395}
396
397
Dmitry Shmidt04949592012-07-19 12:16:46 -0700398static void hostapd_clear_wep(struct hostapd_data *hapd)
399{
Dmitry Shmidta38abf92014-03-06 13:38:44 -0800400 if (hapd->drv_priv && !hapd->iface->driver_ap_teardown) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700401 hostapd_set_privacy(hapd, 0);
402 hostapd_broadcast_wep_clear(hapd);
403 }
404}
405
406
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700407static int hostapd_setup_encryption(char *iface, struct hostapd_data *hapd)
408{
409 int i;
410
411 hostapd_broadcast_wep_set(hapd);
412
413 if (hapd->conf->ssid.wep.default_len) {
414 hostapd_set_privacy(hapd, 1);
415 return 0;
416 }
417
Jouni Malinen75ecf522011-06-27 15:19:46 -0700418 /*
419 * When IEEE 802.1X is not enabled, the driver may need to know how to
420 * set authentication algorithms for static WEP.
421 */
422 hostapd_drv_set_authmode(hapd, hapd->conf->auth_algs);
423
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700424 for (i = 0; i < 4; i++) {
425 if (hapd->conf->ssid.wep.key[i] &&
426 hostapd_drv_set_key(iface, hapd, WPA_ALG_WEP, NULL, i,
427 i == hapd->conf->ssid.wep.idx, NULL, 0,
428 hapd->conf->ssid.wep.key[i],
429 hapd->conf->ssid.wep.len[i])) {
430 wpa_printf(MSG_WARNING, "Could not set WEP "
431 "encryption.");
432 return -1;
433 }
434 if (hapd->conf->ssid.wep.key[i] &&
435 i == hapd->conf->ssid.wep.idx)
436 hostapd_set_privacy(hapd, 1);
437 }
438
439 return 0;
440}
441
442
Dmitry Shmidt04949592012-07-19 12:16:46 -0700443static int hostapd_flush_old_stations(struct hostapd_data *hapd, u16 reason)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700444{
445 int ret = 0;
446 u8 addr[ETH_ALEN];
447
448 if (hostapd_drv_none(hapd) || hapd->drv_priv == NULL)
449 return 0;
450
Dmitry Shmidta38abf92014-03-06 13:38:44 -0800451 if (!hapd->iface->driver_ap_teardown) {
452 wpa_dbg(hapd->msg_ctx, MSG_DEBUG,
453 "Flushing old station entries");
454
455 if (hostapd_flush(hapd)) {
456 wpa_msg(hapd->msg_ctx, MSG_WARNING,
457 "Could not connect to kernel driver");
458 ret = -1;
459 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700460 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800461 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "Deauthenticate all stations");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700462 os_memset(addr, 0xff, ETH_ALEN);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700463 hostapd_drv_sta_deauth(hapd, addr, reason);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700464 hostapd_free_stas(hapd);
465
466 return ret;
467}
468
469
Dmitry Shmidt71757432014-06-02 13:50:35 -0700470static void hostapd_bss_deinit_no_free(struct hostapd_data *hapd)
471{
472 hostapd_free_stas(hapd);
473 hostapd_flush_old_stations(hapd, WLAN_REASON_DEAUTH_LEAVING);
474 hostapd_clear_wep(hapd);
475}
476
477
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700478/**
479 * hostapd_validate_bssid_configuration - Validate BSSID configuration
480 * @iface: Pointer to interface data
481 * Returns: 0 on success, -1 on failure
482 *
483 * This function is used to validate that the configured BSSIDs are valid.
484 */
485static int hostapd_validate_bssid_configuration(struct hostapd_iface *iface)
486{
487 u8 mask[ETH_ALEN] = { 0 };
488 struct hostapd_data *hapd = iface->bss[0];
489 unsigned int i = iface->conf->num_bss, bits = 0, j;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700490 int auto_addr = 0;
491
492 if (hostapd_drv_none(hapd))
493 return 0;
494
495 /* Generate BSSID mask that is large enough to cover the BSSIDs. */
496
497 /* Determine the bits necessary to cover the number of BSSIDs. */
498 for (i--; i; i >>= 1)
499 bits++;
500
501 /* Determine the bits necessary to any configured BSSIDs,
502 if they are higher than the number of BSSIDs. */
503 for (j = 0; j < iface->conf->num_bss; j++) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800504 if (hostapd_mac_comp_empty(iface->conf->bss[j]->bssid) == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700505 if (j)
506 auto_addr++;
507 continue;
508 }
509
510 for (i = 0; i < ETH_ALEN; i++) {
511 mask[i] |=
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800512 iface->conf->bss[j]->bssid[i] ^
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700513 hapd->own_addr[i];
514 }
515 }
516
517 if (!auto_addr)
518 goto skip_mask_ext;
519
520 for (i = 0; i < ETH_ALEN && mask[i] == 0; i++)
521 ;
522 j = 0;
523 if (i < ETH_ALEN) {
524 j = (5 - i) * 8;
525
526 while (mask[i] != 0) {
527 mask[i] >>= 1;
528 j++;
529 }
530 }
531
532 if (bits < j)
533 bits = j;
534
535 if (bits > 40) {
536 wpa_printf(MSG_ERROR, "Too many bits in the BSSID mask (%u)",
537 bits);
538 return -1;
539 }
540
541 os_memset(mask, 0xff, ETH_ALEN);
542 j = bits / 8;
543 for (i = 5; i > 5 - j; i--)
544 mask[i] = 0;
545 j = bits % 8;
546 while (j--)
547 mask[i] <<= 1;
548
549skip_mask_ext:
550 wpa_printf(MSG_DEBUG, "BSS count %lu, BSSID mask " MACSTR " (%d bits)",
551 (unsigned long) iface->conf->num_bss, MAC2STR(mask), bits);
552
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700553 if (!auto_addr)
554 return 0;
555
556 for (i = 0; i < ETH_ALEN; i++) {
557 if ((hapd->own_addr[i] & mask[i]) != hapd->own_addr[i]) {
558 wpa_printf(MSG_ERROR, "Invalid BSSID mask " MACSTR
559 " for start address " MACSTR ".",
560 MAC2STR(mask), MAC2STR(hapd->own_addr));
561 wpa_printf(MSG_ERROR, "Start address must be the "
562 "first address in the block (i.e., addr "
563 "AND mask == addr).");
564 return -1;
565 }
566 }
567
568 return 0;
569}
570
571
572static int mac_in_conf(struct hostapd_config *conf, const void *a)
573{
574 size_t i;
575
576 for (i = 0; i < conf->num_bss; i++) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800577 if (hostapd_mac_comp(conf->bss[i]->bssid, a) == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700578 return 1;
579 }
580 }
581
582 return 0;
583}
584
585
Dmitry Shmidt04949592012-07-19 12:16:46 -0700586#ifndef CONFIG_NO_RADIUS
587
588static int hostapd_das_nas_mismatch(struct hostapd_data *hapd,
589 struct radius_das_attrs *attr)
590{
Dmitry Shmidt13ca8d82014-02-20 10:18:40 -0800591 if (attr->nas_identifier &&
592 (!hapd->conf->nas_identifier ||
593 os_strlen(hapd->conf->nas_identifier) !=
594 attr->nas_identifier_len ||
595 os_memcmp(hapd->conf->nas_identifier, attr->nas_identifier,
596 attr->nas_identifier_len) != 0)) {
597 wpa_printf(MSG_DEBUG, "RADIUS DAS: NAS-Identifier mismatch");
598 return 1;
599 }
600
601 if (attr->nas_ip_addr &&
602 (hapd->conf->own_ip_addr.af != AF_INET ||
603 os_memcmp(&hapd->conf->own_ip_addr.u.v4, attr->nas_ip_addr, 4) !=
604 0)) {
605 wpa_printf(MSG_DEBUG, "RADIUS DAS: NAS-IP-Address mismatch");
606 return 1;
607 }
608
609#ifdef CONFIG_IPV6
610 if (attr->nas_ipv6_addr &&
611 (hapd->conf->own_ip_addr.af != AF_INET6 ||
612 os_memcmp(&hapd->conf->own_ip_addr.u.v6, attr->nas_ipv6_addr, 16)
613 != 0)) {
614 wpa_printf(MSG_DEBUG, "RADIUS DAS: NAS-IPv6-Address mismatch");
615 return 1;
616 }
617#endif /* CONFIG_IPV6 */
618
Dmitry Shmidt04949592012-07-19 12:16:46 -0700619 return 0;
620}
621
622
623static struct sta_info * hostapd_das_find_sta(struct hostapd_data *hapd,
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800624 struct radius_das_attrs *attr,
625 int *multi)
Dmitry Shmidt04949592012-07-19 12:16:46 -0700626{
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800627 struct sta_info *selected, *sta;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700628 char buf[128];
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800629 int num_attr = 0;
630 int count;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700631
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800632 *multi = 0;
633
634 for (sta = hapd->sta_list; sta; sta = sta->next)
635 sta->radius_das_match = 1;
636
637 if (attr->sta_addr) {
638 num_attr++;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700639 sta = ap_get_sta(hapd, attr->sta_addr);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800640 if (!sta) {
641 wpa_printf(MSG_DEBUG,
642 "RADIUS DAS: No Calling-Station-Id match");
643 return NULL;
644 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700645
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800646 selected = sta;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700647 for (sta = hapd->sta_list; sta; sta = sta->next) {
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800648 if (sta != selected)
649 sta->radius_das_match = 0;
650 }
651 wpa_printf(MSG_DEBUG, "RADIUS DAS: Calling-Station-Id match");
652 }
653
654 if (attr->acct_session_id) {
655 num_attr++;
656 if (attr->acct_session_id_len != 17) {
657 wpa_printf(MSG_DEBUG,
658 "RADIUS DAS: Acct-Session-Id cannot match");
659 return NULL;
660 }
661 count = 0;
662
663 for (sta = hapd->sta_list; sta; sta = sta->next) {
664 if (!sta->radius_das_match)
665 continue;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700666 os_snprintf(buf, sizeof(buf), "%08X-%08X",
667 sta->acct_session_id_hi,
668 sta->acct_session_id_lo);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800669 if (os_memcmp(attr->acct_session_id, buf, 17) != 0)
670 sta->radius_das_match = 0;
671 else
672 count++;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700673 }
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800674
675 if (count == 0) {
676 wpa_printf(MSG_DEBUG,
677 "RADIUS DAS: No matches remaining after Acct-Session-Id check");
678 return NULL;
679 }
680 wpa_printf(MSG_DEBUG, "RADIUS DAS: Acct-Session-Id match");
Dmitry Shmidt04949592012-07-19 12:16:46 -0700681 }
682
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800683 if (attr->acct_multi_session_id) {
684 num_attr++;
685 if (attr->acct_multi_session_id_len != 17) {
686 wpa_printf(MSG_DEBUG,
687 "RADIUS DAS: Acct-Multi-Session-Id cannot match");
688 return NULL;
689 }
690 count = 0;
691
692 for (sta = hapd->sta_list; sta; sta = sta->next) {
693 if (!sta->radius_das_match)
694 continue;
695 if (!sta->eapol_sm ||
696 !sta->eapol_sm->acct_multi_session_id_hi) {
697 sta->radius_das_match = 0;
698 continue;
699 }
700 os_snprintf(buf, sizeof(buf), "%08X+%08X",
701 sta->eapol_sm->acct_multi_session_id_hi,
702 sta->eapol_sm->acct_multi_session_id_lo);
703 if (os_memcmp(attr->acct_multi_session_id, buf, 17) !=
704 0)
705 sta->radius_das_match = 0;
706 else
707 count++;
708 }
709
710 if (count == 0) {
711 wpa_printf(MSG_DEBUG,
712 "RADIUS DAS: No matches remaining after Acct-Multi-Session-Id check");
713 return NULL;
714 }
715 wpa_printf(MSG_DEBUG,
716 "RADIUS DAS: Acct-Multi-Session-Id match");
717 }
718
719 if (attr->cui) {
720 num_attr++;
721 count = 0;
722
Dmitry Shmidt04949592012-07-19 12:16:46 -0700723 for (sta = hapd->sta_list; sta; sta = sta->next) {
724 struct wpabuf *cui;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800725
726 if (!sta->radius_das_match)
727 continue;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700728 cui = ieee802_1x_get_radius_cui(sta->eapol_sm);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800729 if (!cui || wpabuf_len(cui) != attr->cui_len ||
Dmitry Shmidt04949592012-07-19 12:16:46 -0700730 os_memcmp(wpabuf_head(cui), attr->cui,
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800731 attr->cui_len) != 0)
732 sta->radius_das_match = 0;
733 else
734 count++;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700735 }
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800736
737 if (count == 0) {
738 wpa_printf(MSG_DEBUG,
739 "RADIUS DAS: No matches remaining after Chargeable-User-Identity check");
740 return NULL;
741 }
742 wpa_printf(MSG_DEBUG,
743 "RADIUS DAS: Chargeable-User-Identity match");
Dmitry Shmidt04949592012-07-19 12:16:46 -0700744 }
745
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800746 if (attr->user_name) {
747 num_attr++;
748 count = 0;
749
Dmitry Shmidt04949592012-07-19 12:16:46 -0700750 for (sta = hapd->sta_list; sta; sta = sta->next) {
751 u8 *identity;
752 size_t identity_len;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800753
754 if (!sta->radius_das_match)
755 continue;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700756 identity = ieee802_1x_get_identity(sta->eapol_sm,
757 &identity_len);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800758 if (!identity ||
759 identity_len != attr->user_name_len ||
Dmitry Shmidt04949592012-07-19 12:16:46 -0700760 os_memcmp(identity, attr->user_name, identity_len)
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800761 != 0)
762 sta->radius_das_match = 0;
763 else
764 count++;
765 }
766
767 if (count == 0) {
768 wpa_printf(MSG_DEBUG,
769 "RADIUS DAS: No matches remaining after User-Name check");
770 return NULL;
771 }
772 wpa_printf(MSG_DEBUG,
773 "RADIUS DAS: User-Name match");
774 }
775
776 if (num_attr == 0) {
777 /*
778 * In theory, we could match all current associations, but it
779 * seems safer to just reject requests that do not include any
780 * session identification attributes.
781 */
782 wpa_printf(MSG_DEBUG,
783 "RADIUS DAS: No session identification attributes included");
784 return NULL;
785 }
786
787 selected = NULL;
788 for (sta = hapd->sta_list; sta; sta = sta->next) {
789 if (sta->radius_das_match) {
790 if (selected) {
791 *multi = 1;
792 return NULL;
793 }
794 selected = sta;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700795 }
796 }
797
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800798 return selected;
799}
800
801
802static int hostapd_das_disconnect_pmksa(struct hostapd_data *hapd,
803 struct radius_das_attrs *attr)
804{
805 if (!hapd->wpa_auth)
806 return -1;
807 return wpa_auth_radius_das_disconnect_pmksa(hapd->wpa_auth, attr);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700808}
809
810
811static enum radius_das_res
812hostapd_das_disconnect(void *ctx, struct radius_das_attrs *attr)
813{
814 struct hostapd_data *hapd = ctx;
815 struct sta_info *sta;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800816 int multi;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700817
818 if (hostapd_das_nas_mismatch(hapd, attr))
819 return RADIUS_DAS_NAS_MISMATCH;
820
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800821 sta = hostapd_das_find_sta(hapd, attr, &multi);
822 if (sta == NULL) {
823 if (multi) {
824 wpa_printf(MSG_DEBUG,
825 "RADIUS DAS: Multiple sessions match - not supported");
826 return RADIUS_DAS_MULTI_SESSION_MATCH;
827 }
828 if (hostapd_das_disconnect_pmksa(hapd, attr) == 0) {
829 wpa_printf(MSG_DEBUG,
830 "RADIUS DAS: PMKSA cache entry matched");
831 return RADIUS_DAS_SUCCESS;
832 }
833 wpa_printf(MSG_DEBUG, "RADIUS DAS: No matching session found");
Dmitry Shmidt04949592012-07-19 12:16:46 -0700834 return RADIUS_DAS_SESSION_NOT_FOUND;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800835 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700836
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800837 wpa_printf(MSG_DEBUG, "RADIUS DAS: Found a matching session " MACSTR
838 " - disconnecting", MAC2STR(sta->addr));
Dmitry Shmidt13ca8d82014-02-20 10:18:40 -0800839 wpa_auth_pmksa_remove(hapd->wpa_auth, sta->addr);
840
Dmitry Shmidt04949592012-07-19 12:16:46 -0700841 hostapd_drv_sta_deauth(hapd, sta->addr,
842 WLAN_REASON_PREV_AUTH_NOT_VALID);
843 ap_sta_deauthenticate(hapd, sta, WLAN_REASON_PREV_AUTH_NOT_VALID);
844
845 return RADIUS_DAS_SUCCESS;
846}
847
848#endif /* CONFIG_NO_RADIUS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700849
850
851/**
852 * hostapd_setup_bss - Per-BSS setup (initialization)
853 * @hapd: Pointer to BSS data
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800854 * @first: Whether this BSS is the first BSS of an interface; -1 = not first,
855 * but interface may exist
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700856 *
857 * This function is used to initialize all per-BSS data structures and
858 * resources. This gets called in a loop for each BSS when an interface is
859 * initialized. Most of the modules that are initialized here will be
860 * deinitialized in hostapd_cleanup().
861 */
862static int hostapd_setup_bss(struct hostapd_data *hapd, int first)
863{
864 struct hostapd_bss_config *conf = hapd->conf;
Dmitry Shmidt9d9e6022015-04-23 10:34:55 -0700865 u8 ssid[SSID_MAX_LEN + 1];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700866 int ssid_len, set_ssid;
867 char force_ifname[IFNAMSIZ];
868 u8 if_addr[ETH_ALEN];
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800869 int flush_old_stations = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700870
Dmitry Shmidt54605472013-11-08 11:10:19 -0800871 wpa_printf(MSG_DEBUG, "%s(hapd=%p (%s), first=%d)",
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700872 __func__, hapd, conf->iface, first);
Dmitry Shmidt54605472013-11-08 11:10:19 -0800873
Dmitry Shmidt50b691d2014-05-21 14:01:45 -0700874#ifdef EAP_SERVER_TNC
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700875 if (conf->tnc && tncs_global_init() < 0) {
Dmitry Shmidt50b691d2014-05-21 14:01:45 -0700876 wpa_printf(MSG_ERROR, "Failed to initialize TNCS");
877 return -1;
878 }
879#endif /* EAP_SERVER_TNC */
880
Dmitry Shmidt54605472013-11-08 11:10:19 -0800881 if (hapd->started) {
882 wpa_printf(MSG_ERROR, "%s: Interface %s was already started",
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700883 __func__, conf->iface);
Dmitry Shmidt54605472013-11-08 11:10:19 -0800884 return -1;
885 }
886 hapd->started = 1;
887
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800888 if (!first || first == -1) {
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700889 if (hostapd_mac_comp_empty(conf->bssid) == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700890 /* Allocate the next available BSSID. */
891 do {
892 inc_byte_array(hapd->own_addr, ETH_ALEN);
893 } while (mac_in_conf(hapd->iconf, hapd->own_addr));
894 } else {
895 /* Allocate the configured BSSID. */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700896 os_memcpy(hapd->own_addr, conf->bssid, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700897
898 if (hostapd_mac_comp(hapd->own_addr,
899 hapd->iface->bss[0]->own_addr) ==
900 0) {
901 wpa_printf(MSG_ERROR, "BSS '%s' may not have "
902 "BSSID set to the MAC address of "
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700903 "the radio", conf->iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700904 return -1;
905 }
906 }
907
Dmitry Shmidt54605472013-11-08 11:10:19 -0800908 hapd->interface_added = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700909 if (hostapd_if_add(hapd->iface->bss[0], WPA_IF_AP_BSS,
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700910 conf->iface, hapd->own_addr, hapd,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700911 &hapd->drv_priv, force_ifname, if_addr,
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700912 conf->bridge[0] ? conf->bridge : NULL,
913 first == -1)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700914 wpa_printf(MSG_ERROR, "Failed to add BSS (BSSID="
915 MACSTR ")", MAC2STR(hapd->own_addr));
Dmitry Shmidt3cf6f792013-12-18 13:12:19 -0800916 hapd->interface_added = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700917 return -1;
918 }
919 }
920
921 if (conf->wmm_enabled < 0)
922 conf->wmm_enabled = hapd->iconf->ieee80211n;
923
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800924#ifdef CONFIG_MESH
925 if (hapd->iface->mconf == NULL)
926 flush_old_stations = 0;
927#endif /* CONFIG_MESH */
928
929 if (flush_old_stations)
930 hostapd_flush_old_stations(hapd,
931 WLAN_REASON_PREV_AUTH_NOT_VALID);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700932 hostapd_set_privacy(hapd, 0);
933
934 hostapd_broadcast_wep_clear(hapd);
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700935 if (hostapd_setup_encryption(conf->iface, hapd))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700936 return -1;
937
938 /*
939 * Fetch the SSID from the system and use it or,
940 * if one was specified in the config file, verify they
941 * match.
942 */
943 ssid_len = hostapd_get_ssid(hapd, ssid, sizeof(ssid));
944 if (ssid_len < 0) {
945 wpa_printf(MSG_ERROR, "Could not read SSID from system");
946 return -1;
947 }
948 if (conf->ssid.ssid_set) {
949 /*
950 * If SSID is specified in the config file and it differs
951 * from what is being used then force installation of the
952 * new SSID.
953 */
954 set_ssid = (conf->ssid.ssid_len != (size_t) ssid_len ||
955 os_memcmp(conf->ssid.ssid, ssid, ssid_len) != 0);
956 } else {
957 /*
958 * No SSID in the config file; just use the one we got
959 * from the system.
960 */
961 set_ssid = 0;
962 conf->ssid.ssid_len = ssid_len;
963 os_memcpy(conf->ssid.ssid, ssid, conf->ssid.ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700964 }
965
966 if (!hostapd_drv_none(hapd)) {
967 wpa_printf(MSG_ERROR, "Using interface %s with hwaddr " MACSTR
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700968 " and ssid \"%s\"",
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700969 conf->iface, MAC2STR(hapd->own_addr),
970 wpa_ssid_txt(conf->ssid.ssid, conf->ssid.ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700971 }
972
973 if (hostapd_setup_wpa_psk(conf)) {
974 wpa_printf(MSG_ERROR, "WPA-PSK setup failed.");
975 return -1;
976 }
977
978 /* Set SSID for the kernel driver (to be used in beacon and probe
979 * response frames) */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700980 if (set_ssid && hostapd_set_ssid(hapd, conf->ssid.ssid,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700981 conf->ssid.ssid_len)) {
982 wpa_printf(MSG_ERROR, "Could not set SSID for kernel driver");
983 return -1;
984 }
985
Dmitry Shmidt818ea482014-03-10 13:15:21 -0700986 if (wpa_debug_level <= MSG_MSGDUMP)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700987 conf->radius->msg_dumps = 1;
988#ifndef CONFIG_NO_RADIUS
989 hapd->radius = radius_client_init(hapd, conf->radius);
990 if (hapd->radius == NULL) {
991 wpa_printf(MSG_ERROR, "RADIUS client initialization failed.");
992 return -1;
993 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700994
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700995 if (conf->radius_das_port) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700996 struct radius_das_conf das_conf;
997 os_memset(&das_conf, 0, sizeof(das_conf));
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700998 das_conf.port = conf->radius_das_port;
999 das_conf.shared_secret = conf->radius_das_shared_secret;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001000 das_conf.shared_secret_len =
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001001 conf->radius_das_shared_secret_len;
1002 das_conf.client_addr = &conf->radius_das_client_addr;
1003 das_conf.time_window = conf->radius_das_time_window;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001004 das_conf.require_event_timestamp =
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001005 conf->radius_das_require_event_timestamp;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001006 das_conf.ctx = hapd;
1007 das_conf.disconnect = hostapd_das_disconnect;
1008 hapd->radius_das = radius_das_init(&das_conf);
1009 if (hapd->radius_das == NULL) {
1010 wpa_printf(MSG_ERROR, "RADIUS DAS initialization "
1011 "failed.");
1012 return -1;
1013 }
1014 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001015#endif /* CONFIG_NO_RADIUS */
1016
1017 if (hostapd_acl_init(hapd)) {
1018 wpa_printf(MSG_ERROR, "ACL initialization failed.");
1019 return -1;
1020 }
1021 if (hostapd_init_wps(hapd, conf))
1022 return -1;
1023
1024 if (authsrv_init(hapd) < 0)
1025 return -1;
1026
1027 if (ieee802_1x_init(hapd)) {
1028 wpa_printf(MSG_ERROR, "IEEE 802.1X initialization failed.");
1029 return -1;
1030 }
1031
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001032 if ((conf->wpa || conf->osen) && hostapd_setup_wpa(hapd))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001033 return -1;
1034
1035 if (accounting_init(hapd)) {
1036 wpa_printf(MSG_ERROR, "Accounting initialization failed.");
1037 return -1;
1038 }
1039
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001040 if (conf->ieee802_11f &&
1041 (hapd->iapp = iapp_init(hapd, conf->iapp_iface)) == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001042 wpa_printf(MSG_ERROR, "IEEE 802.11F (IAPP) initialization "
1043 "failed.");
1044 return -1;
1045 }
1046
Dmitry Shmidt04949592012-07-19 12:16:46 -07001047#ifdef CONFIG_INTERWORKING
1048 if (gas_serv_init(hapd)) {
1049 wpa_printf(MSG_ERROR, "GAS server initialization failed");
1050 return -1;
1051 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001052
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001053 if (conf->qos_map_set_len &&
1054 hostapd_drv_set_qos_map(hapd, conf->qos_map_set,
1055 conf->qos_map_set_len)) {
1056 wpa_printf(MSG_ERROR, "Failed to initialize QoS Map");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001057 return -1;
1058 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001059#endif /* CONFIG_INTERWORKING */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001060
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001061 if (conf->bss_load_update_period && bss_load_update_init(hapd)) {
1062 wpa_printf(MSG_ERROR, "BSS Load initialization failed");
1063 return -1;
1064 }
1065
1066 if (conf->proxy_arp) {
1067 if (x_snoop_init(hapd)) {
1068 wpa_printf(MSG_ERROR,
1069 "Generic snooping infrastructure initialization failed");
1070 return -1;
1071 }
1072
1073 if (dhcp_snoop_init(hapd)) {
1074 wpa_printf(MSG_ERROR,
1075 "DHCP snooping initialization failed");
1076 return -1;
1077 }
1078
1079 if (ndisc_snoop_init(hapd)) {
1080 wpa_printf(MSG_ERROR,
1081 "Neighbor Discovery snooping initialization failed");
1082 return -1;
1083 }
1084 }
1085
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001086 if (!hostapd_drv_none(hapd) && vlan_init(hapd)) {
1087 wpa_printf(MSG_ERROR, "VLAN initialization failed.");
1088 return -1;
1089 }
1090
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001091 if (!conf->start_disabled && ieee802_11_set_beacon(hapd) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001092 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001093
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001094 if (hapd->wpa_auth && wpa_init_keys(hapd->wpa_auth) < 0)
1095 return -1;
1096
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001097 if (hapd->driver && hapd->driver->set_operstate)
1098 hapd->driver->set_operstate(hapd->drv_priv, 1);
1099
1100 return 0;
1101}
1102
1103
1104static void hostapd_tx_queue_params(struct hostapd_iface *iface)
1105{
1106 struct hostapd_data *hapd = iface->bss[0];
1107 int i;
1108 struct hostapd_tx_queue_params *p;
1109
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001110#ifdef CONFIG_MESH
1111 if (iface->mconf == NULL)
1112 return;
1113#endif /* CONFIG_MESH */
1114
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001115 for (i = 0; i < NUM_TX_QUEUES; i++) {
1116 p = &iface->conf->tx_queue[i];
1117
1118 if (hostapd_set_tx_queue_params(hapd, i, p->aifs, p->cwmin,
1119 p->cwmax, p->burst)) {
1120 wpa_printf(MSG_DEBUG, "Failed to set TX queue "
1121 "parameters for queue %d.", i);
1122 /* Continue anyway */
1123 }
1124 }
1125}
1126
1127
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001128static int hostapd_set_acl_list(struct hostapd_data *hapd,
1129 struct mac_acl_entry *mac_acl,
1130 int n_entries, u8 accept_acl)
1131{
1132 struct hostapd_acl_params *acl_params;
1133 int i, err;
1134
1135 acl_params = os_zalloc(sizeof(*acl_params) +
1136 (n_entries * sizeof(acl_params->mac_acl[0])));
1137 if (!acl_params)
1138 return -ENOMEM;
1139
1140 for (i = 0; i < n_entries; i++)
1141 os_memcpy(acl_params->mac_acl[i].addr, mac_acl[i].addr,
1142 ETH_ALEN);
1143
1144 acl_params->acl_policy = accept_acl;
1145 acl_params->num_mac_acl = n_entries;
1146
1147 err = hostapd_drv_set_acl(hapd, acl_params);
1148
1149 os_free(acl_params);
1150
1151 return err;
1152}
1153
1154
1155static void hostapd_set_acl(struct hostapd_data *hapd)
1156{
1157 struct hostapd_config *conf = hapd->iconf;
1158 int err;
1159 u8 accept_acl;
1160
1161 if (hapd->iface->drv_max_acl_mac_addrs == 0)
1162 return;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001163
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001164 if (conf->bss[0]->macaddr_acl == DENY_UNLESS_ACCEPTED) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001165 accept_acl = 1;
1166 err = hostapd_set_acl_list(hapd, conf->bss[0]->accept_mac,
1167 conf->bss[0]->num_accept_mac,
1168 accept_acl);
1169 if (err) {
1170 wpa_printf(MSG_DEBUG, "Failed to set accept acl");
1171 return;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001172 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001173 } else if (conf->bss[0]->macaddr_acl == ACCEPT_UNLESS_DENIED) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001174 accept_acl = 0;
1175 err = hostapd_set_acl_list(hapd, conf->bss[0]->deny_mac,
1176 conf->bss[0]->num_deny_mac,
1177 accept_acl);
1178 if (err) {
1179 wpa_printf(MSG_DEBUG, "Failed to set deny acl");
1180 return;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001181 }
1182 }
1183}
1184
1185
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001186static int start_ctrl_iface_bss(struct hostapd_data *hapd)
1187{
1188 if (!hapd->iface->interfaces ||
1189 !hapd->iface->interfaces->ctrl_iface_init)
1190 return 0;
1191
1192 if (hapd->iface->interfaces->ctrl_iface_init(hapd)) {
1193 wpa_printf(MSG_ERROR,
1194 "Failed to setup control interface for %s",
1195 hapd->conf->iface);
1196 return -1;
1197 }
1198
1199 return 0;
1200}
1201
1202
1203static int start_ctrl_iface(struct hostapd_iface *iface)
1204{
1205 size_t i;
1206
1207 if (!iface->interfaces || !iface->interfaces->ctrl_iface_init)
1208 return 0;
1209
1210 for (i = 0; i < iface->num_bss; i++) {
1211 struct hostapd_data *hapd = iface->bss[i];
1212 if (iface->interfaces->ctrl_iface_init(hapd)) {
1213 wpa_printf(MSG_ERROR,
1214 "Failed to setup control interface for %s",
1215 hapd->conf->iface);
1216 return -1;
1217 }
1218 }
1219
1220 return 0;
1221}
1222
1223
1224static void channel_list_update_timeout(void *eloop_ctx, void *timeout_ctx)
1225{
1226 struct hostapd_iface *iface = eloop_ctx;
1227
1228 if (!iface->wait_channel_update) {
1229 wpa_printf(MSG_INFO, "Channel list update timeout, but interface was not waiting for it");
1230 return;
1231 }
1232
1233 /*
1234 * It is possible that the existing channel list is acceptable, so try
1235 * to proceed.
1236 */
1237 wpa_printf(MSG_DEBUG, "Channel list update timeout - try to continue anyway");
1238 setup_interface2(iface);
1239}
1240
1241
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001242void hostapd_channel_list_updated(struct hostapd_iface *iface, int initiator)
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001243{
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001244 if (!iface->wait_channel_update || initiator != REGDOM_SET_BY_USER)
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001245 return;
1246
1247 wpa_printf(MSG_DEBUG, "Channel list updated - continue setup");
1248 eloop_cancel_timeout(channel_list_update_timeout, iface, NULL);
1249 setup_interface2(iface);
1250}
1251
1252
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001253static int setup_interface(struct hostapd_iface *iface)
1254{
1255 struct hostapd_data *hapd = iface->bss[0];
1256 size_t i;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001257
Dmitry Shmidta38abf92014-03-06 13:38:44 -08001258 /*
1259 * It is possible that setup_interface() is called after the interface
1260 * was disabled etc., in which case driver_ap_teardown is possibly set
1261 * to 1. Clear it here so any other key/station deletion, which is not
1262 * part of a teardown flow, would also call the relevant driver
1263 * callbacks.
1264 */
1265 iface->driver_ap_teardown = 0;
1266
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001267 if (!iface->phy[0]) {
1268 const char *phy = hostapd_drv_get_radio_name(hapd);
1269 if (phy) {
1270 wpa_printf(MSG_DEBUG, "phy: %s", phy);
1271 os_strlcpy(iface->phy, phy, sizeof(iface->phy));
1272 }
1273 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001274
1275 /*
1276 * Make sure that all BSSes get configured with a pointer to the same
1277 * driver interface.
1278 */
1279 for (i = 1; i < iface->num_bss; i++) {
1280 iface->bss[i]->driver = hapd->driver;
1281 iface->bss[i]->drv_priv = hapd->drv_priv;
1282 }
1283
1284 if (hostapd_validate_bssid_configuration(iface))
1285 return -1;
1286
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001287 /*
1288 * Initialize control interfaces early to allow external monitoring of
1289 * channel setup operations that may take considerable amount of time
1290 * especially for DFS cases.
1291 */
1292 if (start_ctrl_iface(iface))
1293 return -1;
1294
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001295 if (hapd->iconf->country[0] && hapd->iconf->country[1]) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001296 char country[4], previous_country[4];
1297
1298 hostapd_set_state(iface, HAPD_IFACE_COUNTRY_UPDATE);
1299 if (hostapd_get_country(hapd, previous_country) < 0)
1300 previous_country[0] = '\0';
1301
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001302 os_memcpy(country, hapd->iconf->country, 3);
1303 country[3] = '\0';
1304 if (hostapd_set_country(hapd, country) < 0) {
1305 wpa_printf(MSG_ERROR, "Failed to set country code");
1306 return -1;
1307 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001308
1309 wpa_printf(MSG_DEBUG, "Previous country code %s, new country code %s",
1310 previous_country, country);
1311
1312 if (os_strncmp(previous_country, country, 2) != 0) {
1313 wpa_printf(MSG_DEBUG, "Continue interface setup after channel list update");
1314 iface->wait_channel_update = 1;
Dmitry Shmidt97672262014-02-03 13:02:54 -08001315 eloop_register_timeout(5, 0,
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001316 channel_list_update_timeout,
1317 iface, NULL);
1318 return 0;
1319 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001320 }
1321
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001322 return setup_interface2(iface);
1323}
1324
1325
1326static int setup_interface2(struct hostapd_iface *iface)
1327{
1328 iface->wait_channel_update = 0;
1329
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001330 if (hostapd_get_hw_features(iface)) {
1331 /* Not all drivers support this yet, so continue without hw
1332 * feature data. */
1333 } else {
1334 int ret = hostapd_select_hw_mode(iface);
1335 if (ret < 0) {
1336 wpa_printf(MSG_ERROR, "Could not select hw_mode and "
1337 "channel. (%d)", ret);
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001338 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001339 }
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001340 if (ret == 1) {
1341 wpa_printf(MSG_DEBUG, "Interface initialization will be completed in a callback (ACS)");
1342 return 0;
1343 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001344 ret = hostapd_check_ht_capab(iface);
1345 if (ret < 0)
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001346 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001347 if (ret == 1) {
1348 wpa_printf(MSG_DEBUG, "Interface initialization will "
1349 "be completed in a callback");
1350 return 0;
1351 }
Dmitry Shmidt051af732013-10-22 13:52:46 -07001352
1353 if (iface->conf->ieee80211h)
1354 wpa_printf(MSG_DEBUG, "DFS support is enabled");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001355 }
1356 return hostapd_setup_interface_complete(iface, 0);
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001357
1358fail:
1359 hostapd_set_state(iface, HAPD_IFACE_DISABLED);
1360 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
1361 if (iface->interfaces && iface->interfaces->terminate_on_error)
1362 eloop_terminate();
1363 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001364}
1365
1366
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001367/**
1368 * hostapd_setup_interface_complete - Complete interface setup
1369 *
1370 * This function is called when previous steps in the interface setup has been
1371 * completed. This can also start operations, e.g., DFS, that will require
1372 * additional processing before interface is ready to be enabled. Such
1373 * operations will call this function from eloop callbacks when finished.
1374 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001375int hostapd_setup_interface_complete(struct hostapd_iface *iface, int err)
1376{
1377 struct hostapd_data *hapd = iface->bss[0];
1378 size_t j;
1379 u8 *prev_addr;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001380 int delay_apply_cfg = 0;
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001381 int res_dfs_offload = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001382
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001383 if (err)
1384 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001385
1386 wpa_printf(MSG_DEBUG, "Completing interface initialization");
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001387 if (iface->conf->channel) {
Dmitry Shmidt051af732013-10-22 13:52:46 -07001388#ifdef NEED_AP_MLME
1389 int res;
1390#endif /* NEED_AP_MLME */
1391
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001392 iface->freq = hostapd_hw_get_freq(hapd, iface->conf->channel);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001393 wpa_printf(MSG_DEBUG, "Mode: %s Channel: %d "
1394 "Frequency: %d MHz",
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001395 hostapd_hw_mode_txt(iface->conf->hw_mode),
1396 iface->conf->channel, iface->freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001397
Dmitry Shmidt051af732013-10-22 13:52:46 -07001398#ifdef NEED_AP_MLME
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001399 /* Handle DFS only if it is not offloaded to the driver */
1400 if (!(iface->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD)) {
1401 /* Check DFS */
1402 res = hostapd_handle_dfs(iface);
1403 if (res <= 0) {
1404 if (res < 0)
1405 goto fail;
1406 return res;
1407 }
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001408 } else {
1409 /* If DFS is offloaded to the driver */
1410 res_dfs_offload = hostapd_handle_dfs_offload(iface);
1411 if (res_dfs_offload <= 0) {
1412 if (res_dfs_offload < 0)
1413 goto fail;
1414 } else {
1415 wpa_printf(MSG_DEBUG,
1416 "Proceed with AP/channel setup");
1417 /*
1418 * If this is a DFS channel, move to completing
1419 * AP setup.
1420 */
1421 if (res_dfs_offload == 1)
1422 goto dfs_offload;
1423 /* Otherwise fall through. */
1424 }
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001425 }
Dmitry Shmidt051af732013-10-22 13:52:46 -07001426#endif /* NEED_AP_MLME */
1427
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001428#ifdef CONFIG_MESH
1429 if (iface->mconf != NULL) {
1430 wpa_printf(MSG_DEBUG,
1431 "%s: Mesh configuration will be applied while joining the mesh network",
1432 iface->bss[0]->conf->iface);
1433 delay_apply_cfg = 1;
1434 }
1435#endif /* CONFIG_MESH */
1436
1437 if (!delay_apply_cfg &&
1438 hostapd_set_freq(hapd, hapd->iconf->hw_mode, iface->freq,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001439 hapd->iconf->channel,
1440 hapd->iconf->ieee80211n,
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001441 hapd->iconf->ieee80211ac,
1442 hapd->iconf->secondary_channel,
1443 hapd->iconf->vht_oper_chwidth,
1444 hapd->iconf->vht_oper_centr_freq_seg0_idx,
1445 hapd->iconf->vht_oper_centr_freq_seg1_idx)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001446 wpa_printf(MSG_ERROR, "Could not set channel for "
1447 "kernel driver");
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001448 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001449 }
1450 }
1451
1452 if (iface->current_mode) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001453 if (hostapd_prepare_rates(iface, iface->current_mode)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001454 wpa_printf(MSG_ERROR, "Failed to prepare rates "
1455 "table.");
1456 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
1457 HOSTAPD_LEVEL_WARNING,
1458 "Failed to prepare rates table.");
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001459 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001460 }
1461 }
1462
1463 if (hapd->iconf->rts_threshold > -1 &&
1464 hostapd_set_rts(hapd, hapd->iconf->rts_threshold)) {
1465 wpa_printf(MSG_ERROR, "Could not set RTS threshold for "
1466 "kernel driver");
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001467 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001468 }
1469
1470 if (hapd->iconf->fragm_threshold > -1 &&
1471 hostapd_set_frag(hapd, hapd->iconf->fragm_threshold)) {
1472 wpa_printf(MSG_ERROR, "Could not set fragmentation threshold "
1473 "for kernel driver");
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001474 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001475 }
1476
1477 prev_addr = hapd->own_addr;
1478
1479 for (j = 0; j < iface->num_bss; j++) {
1480 hapd = iface->bss[j];
1481 if (j)
1482 os_memcpy(hapd->own_addr, prev_addr, ETH_ALEN);
Dmitry Shmidt71757432014-06-02 13:50:35 -07001483 if (hostapd_setup_bss(hapd, j == 0)) {
1484 do {
1485 hapd = iface->bss[j];
1486 hostapd_bss_deinit_no_free(hapd);
1487 hostapd_free_hapd_data(hapd);
1488 } while (j-- > 0);
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001489 goto fail;
Dmitry Shmidt71757432014-06-02 13:50:35 -07001490 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001491 if (hostapd_mac_comp_empty(hapd->conf->bssid) == 0)
1492 prev_addr = hapd->own_addr;
1493 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001494 hapd = iface->bss[0];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001495
1496 hostapd_tx_queue_params(iface);
1497
1498 ap_list_init(iface);
1499
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001500 hostapd_set_acl(hapd);
1501
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001502 if (hostapd_driver_commit(hapd) < 0) {
1503 wpa_printf(MSG_ERROR, "%s: Failed to commit driver "
1504 "configuration", __func__);
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001505 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001506 }
1507
Jouni Malinen87fd2792011-05-16 18:35:42 +03001508 /*
1509 * WPS UPnP module can be initialized only when the "upnp_iface" is up.
1510 * If "interface" and "upnp_iface" are the same (e.g., non-bridge
1511 * mode), the interface is up only after driver_commit, so initialize
1512 * WPS after driver_commit.
1513 */
1514 for (j = 0; j < iface->num_bss; j++) {
1515 if (hostapd_init_wps_complete(iface->bss[j]))
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001516 goto fail;
Jouni Malinen87fd2792011-05-16 18:35:42 +03001517 }
1518
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001519 if ((iface->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
1520 !res_dfs_offload) {
1521 /*
1522 * If freq is DFS, and DFS is offloaded to the driver, then wait
1523 * for CAC to complete.
1524 */
1525 wpa_printf(MSG_DEBUG, "%s: Wait for CAC to complete", __func__);
1526 return res_dfs_offload;
1527 }
1528
1529#ifdef NEED_AP_MLME
1530dfs_offload:
1531#endif /* NEED_AP_MLME */
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001532 hostapd_set_state(iface, HAPD_IFACE_ENABLED);
1533 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_ENABLED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001534 if (hapd->setup_complete_cb)
1535 hapd->setup_complete_cb(hapd->setup_complete_cb_ctx);
1536
1537 wpa_printf(MSG_DEBUG, "%s: Setup of interface done.",
1538 iface->bss[0]->conf->iface);
Dmitry Shmidtb96dad42013-11-05 10:07:29 -08001539 if (iface->interfaces && iface->interfaces->terminate_on_error > 0)
1540 iface->interfaces->terminate_on_error--;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001541
1542 return 0;
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001543
1544fail:
1545 wpa_printf(MSG_ERROR, "Interface initialization failed");
1546 hostapd_set_state(iface, HAPD_IFACE_DISABLED);
1547 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
1548 if (iface->interfaces && iface->interfaces->terminate_on_error)
1549 eloop_terminate();
1550 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001551}
1552
1553
1554/**
1555 * hostapd_setup_interface - Setup of an interface
1556 * @iface: Pointer to interface data.
1557 * Returns: 0 on success, -1 on failure
1558 *
1559 * Initializes the driver interface, validates the configuration,
1560 * and sets driver parameters based on the configuration.
1561 * Flushes old stations, sets the channel, encryption,
1562 * beacons, and WDS links based on the configuration.
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001563 *
1564 * If interface setup requires more time, e.g., to perform HT co-ex scans, ACS,
1565 * or DFS operations, this function returns 0 before such operations have been
1566 * completed. The pending operations are registered into eloop and will be
1567 * completed from eloop callbacks. Those callbacks end up calling
1568 * hostapd_setup_interface_complete() once setup has been completed.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001569 */
1570int hostapd_setup_interface(struct hostapd_iface *iface)
1571{
1572 int ret;
1573
1574 ret = setup_interface(iface);
1575 if (ret) {
1576 wpa_printf(MSG_ERROR, "%s: Unable to setup interface.",
1577 iface->bss[0]->conf->iface);
1578 return -1;
1579 }
1580
1581 return 0;
1582}
1583
1584
1585/**
1586 * hostapd_alloc_bss_data - Allocate and initialize per-BSS data
1587 * @hapd_iface: Pointer to interface data
1588 * @conf: Pointer to per-interface configuration
1589 * @bss: Pointer to per-BSS configuration for this BSS
1590 * Returns: Pointer to allocated BSS data
1591 *
1592 * This function is used to allocate per-BSS data structure. This data will be
1593 * freed after hostapd_cleanup() is called for it during interface
1594 * deinitialization.
1595 */
1596struct hostapd_data *
1597hostapd_alloc_bss_data(struct hostapd_iface *hapd_iface,
1598 struct hostapd_config *conf,
1599 struct hostapd_bss_config *bss)
1600{
1601 struct hostapd_data *hapd;
1602
1603 hapd = os_zalloc(sizeof(*hapd));
1604 if (hapd == NULL)
1605 return NULL;
1606
1607 hapd->new_assoc_sta_cb = hostapd_new_assoc_sta;
1608 hapd->iconf = conf;
1609 hapd->conf = bss;
1610 hapd->iface = hapd_iface;
1611 hapd->driver = hapd->iconf->driver;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001612 hapd->ctrl_sock = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001613
1614 return hapd;
1615}
1616
1617
Dmitry Shmidt54605472013-11-08 11:10:19 -08001618static void hostapd_bss_deinit(struct hostapd_data *hapd)
1619{
1620 wpa_printf(MSG_DEBUG, "%s: deinit bss %s", __func__,
1621 hapd->conf->iface);
Dmitry Shmidt71757432014-06-02 13:50:35 -07001622 hostapd_bss_deinit_no_free(hapd);
Dmitry Shmidtf73259c2015-03-17 11:00:54 -07001623 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
Dmitry Shmidt54605472013-11-08 11:10:19 -08001624 hostapd_cleanup(hapd);
1625}
1626
1627
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001628void hostapd_interface_deinit(struct hostapd_iface *iface)
1629{
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001630 int j;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001631
Dmitry Shmidt54605472013-11-08 11:10:19 -08001632 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001633 if (iface == NULL)
1634 return;
1635
Dmitry Shmidtf73259c2015-03-17 11:00:54 -07001636 hostapd_set_state(iface, HAPD_IFACE_DISABLED);
1637
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07001638#ifdef CONFIG_IEEE80211N
1639#ifdef NEED_AP_MLME
1640 hostapd_stop_setup_timers(iface);
1641 eloop_cancel_timeout(ap_ht2040_timeout, iface, NULL);
1642#endif /* NEED_AP_MLME */
1643#endif /* CONFIG_IEEE80211N */
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001644 eloop_cancel_timeout(channel_list_update_timeout, iface, NULL);
1645 iface->wait_channel_update = 0;
1646
Dmitry Shmidt54605472013-11-08 11:10:19 -08001647 for (j = iface->num_bss - 1; j >= 0; j--)
1648 hostapd_bss_deinit(iface->bss[j]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001649}
1650
1651
1652void hostapd_interface_free(struct hostapd_iface *iface)
1653{
1654 size_t j;
Dmitry Shmidt54605472013-11-08 11:10:19 -08001655 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
1656 for (j = 0; j < iface->num_bss; j++) {
1657 wpa_printf(MSG_DEBUG, "%s: free hapd %p",
1658 __func__, iface->bss[j]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001659 os_free(iface->bss[j]);
Dmitry Shmidt54605472013-11-08 11:10:19 -08001660 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001661 hostapd_cleanup_iface(iface);
1662}
1663
1664
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001665/**
1666 * hostapd_init - Allocate and initialize per-interface data
1667 * @config_file: Path to the configuration file
1668 * Returns: Pointer to the allocated interface data or %NULL on failure
1669 *
1670 * This function is used to allocate main data structures for per-interface
1671 * data. The allocated data buffer will be freed by calling
1672 * hostapd_cleanup_iface().
1673 */
1674struct hostapd_iface * hostapd_init(struct hapd_interfaces *interfaces,
1675 const char *config_file)
1676{
1677 struct hostapd_iface *hapd_iface = NULL;
1678 struct hostapd_config *conf = NULL;
1679 struct hostapd_data *hapd;
1680 size_t i;
1681
1682 hapd_iface = os_zalloc(sizeof(*hapd_iface));
1683 if (hapd_iface == NULL)
1684 goto fail;
1685
1686 hapd_iface->config_fname = os_strdup(config_file);
1687 if (hapd_iface->config_fname == NULL)
1688 goto fail;
1689
1690 conf = interfaces->config_read_cb(hapd_iface->config_fname);
1691 if (conf == NULL)
1692 goto fail;
1693 hapd_iface->conf = conf;
1694
1695 hapd_iface->num_bss = conf->num_bss;
1696 hapd_iface->bss = os_calloc(conf->num_bss,
1697 sizeof(struct hostapd_data *));
1698 if (hapd_iface->bss == NULL)
1699 goto fail;
1700
1701 for (i = 0; i < conf->num_bss; i++) {
1702 hapd = hapd_iface->bss[i] =
1703 hostapd_alloc_bss_data(hapd_iface, conf,
1704 conf->bss[i]);
1705 if (hapd == NULL)
1706 goto fail;
1707 hapd->msg_ctx = hapd;
1708 }
1709
1710 return hapd_iface;
1711
1712fail:
1713 wpa_printf(MSG_ERROR, "Failed to set up interface with %s",
1714 config_file);
1715 if (conf)
1716 hostapd_config_free(conf);
1717 if (hapd_iface) {
1718 os_free(hapd_iface->config_fname);
1719 os_free(hapd_iface->bss);
Dmitry Shmidt54605472013-11-08 11:10:19 -08001720 wpa_printf(MSG_DEBUG, "%s: free iface %p",
1721 __func__, hapd_iface);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001722 os_free(hapd_iface);
1723 }
1724 return NULL;
1725}
1726
1727
1728static int ifname_in_use(struct hapd_interfaces *interfaces, const char *ifname)
1729{
1730 size_t i, j;
1731
1732 for (i = 0; i < interfaces->count; i++) {
1733 struct hostapd_iface *iface = interfaces->iface[i];
1734 for (j = 0; j < iface->num_bss; j++) {
1735 struct hostapd_data *hapd = iface->bss[j];
1736 if (os_strcmp(ifname, hapd->conf->iface) == 0)
1737 return 1;
1738 }
1739 }
1740
1741 return 0;
1742}
1743
1744
1745/**
1746 * hostapd_interface_init_bss - Read configuration file and init BSS data
1747 *
1748 * This function is used to parse configuration file for a BSS. This BSS is
1749 * added to an existing interface sharing the same radio (if any) or a new
1750 * interface is created if this is the first interface on a radio. This
1751 * allocate memory for the BSS. No actual driver operations are started.
1752 *
1753 * This is similar to hostapd_interface_init(), but for a case where the
1754 * configuration is used to add a single BSS instead of all BSSes for a radio.
1755 */
1756struct hostapd_iface *
1757hostapd_interface_init_bss(struct hapd_interfaces *interfaces, const char *phy,
1758 const char *config_fname, int debug)
1759{
1760 struct hostapd_iface *new_iface = NULL, *iface = NULL;
1761 struct hostapd_data *hapd;
1762 int k;
1763 size_t i, bss_idx;
1764
1765 if (!phy || !*phy)
1766 return NULL;
1767
1768 for (i = 0; i < interfaces->count; i++) {
1769 if (os_strcmp(interfaces->iface[i]->phy, phy) == 0) {
1770 iface = interfaces->iface[i];
1771 break;
1772 }
1773 }
1774
1775 wpa_printf(MSG_INFO, "Configuration file: %s (phy %s)%s",
1776 config_fname, phy, iface ? "" : " --> new PHY");
1777 if (iface) {
1778 struct hostapd_config *conf;
1779 struct hostapd_bss_config **tmp_conf;
1780 struct hostapd_data **tmp_bss;
1781 struct hostapd_bss_config *bss;
1782 const char *ifname;
1783
1784 /* Add new BSS to existing iface */
1785 conf = interfaces->config_read_cb(config_fname);
1786 if (conf == NULL)
1787 return NULL;
1788 if (conf->num_bss > 1) {
1789 wpa_printf(MSG_ERROR, "Multiple BSSes specified in BSS-config");
1790 hostapd_config_free(conf);
1791 return NULL;
1792 }
1793
1794 ifname = conf->bss[0]->iface;
1795 if (ifname[0] != '\0' && ifname_in_use(interfaces, ifname)) {
1796 wpa_printf(MSG_ERROR,
1797 "Interface name %s already in use", ifname);
1798 hostapd_config_free(conf);
1799 return NULL;
1800 }
1801
1802 tmp_conf = os_realloc_array(
1803 iface->conf->bss, iface->conf->num_bss + 1,
1804 sizeof(struct hostapd_bss_config *));
1805 tmp_bss = os_realloc_array(iface->bss, iface->num_bss + 1,
1806 sizeof(struct hostapd_data *));
1807 if (tmp_bss)
1808 iface->bss = tmp_bss;
1809 if (tmp_conf) {
1810 iface->conf->bss = tmp_conf;
1811 iface->conf->last_bss = tmp_conf[0];
1812 }
1813 if (tmp_bss == NULL || tmp_conf == NULL) {
1814 hostapd_config_free(conf);
1815 return NULL;
1816 }
1817 bss = iface->conf->bss[iface->conf->num_bss] = conf->bss[0];
1818 iface->conf->num_bss++;
1819
1820 hapd = hostapd_alloc_bss_data(iface, iface->conf, bss);
1821 if (hapd == NULL) {
1822 iface->conf->num_bss--;
1823 hostapd_config_free(conf);
1824 return NULL;
1825 }
1826 iface->conf->last_bss = bss;
1827 iface->bss[iface->num_bss] = hapd;
1828 hapd->msg_ctx = hapd;
1829
1830 bss_idx = iface->num_bss++;
1831 conf->num_bss--;
1832 conf->bss[0] = NULL;
1833 hostapd_config_free(conf);
1834 } else {
1835 /* Add a new iface with the first BSS */
1836 new_iface = iface = hostapd_init(interfaces, config_fname);
1837 if (!iface)
1838 return NULL;
1839 os_strlcpy(iface->phy, phy, sizeof(iface->phy));
1840 iface->interfaces = interfaces;
1841 bss_idx = 0;
1842 }
1843
1844 for (k = 0; k < debug; k++) {
1845 if (iface->bss[bss_idx]->conf->logger_stdout_level > 0)
1846 iface->bss[bss_idx]->conf->logger_stdout_level--;
1847 }
1848
1849 if (iface->conf->bss[bss_idx]->iface[0] == '\0' &&
1850 !hostapd_drv_none(iface->bss[bss_idx])) {
1851 wpa_printf(MSG_ERROR, "Interface name not specified in %s",
1852 config_fname);
1853 if (new_iface)
1854 hostapd_interface_deinit_free(new_iface);
1855 return NULL;
1856 }
1857
1858 return iface;
1859}
1860
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001861
1862void hostapd_interface_deinit_free(struct hostapd_iface *iface)
1863{
1864 const struct wpa_driver_ops *driver;
1865 void *drv_priv;
Dmitry Shmidt54605472013-11-08 11:10:19 -08001866
1867 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001868 if (iface == NULL)
1869 return;
Dmitry Shmidt54605472013-11-08 11:10:19 -08001870 wpa_printf(MSG_DEBUG, "%s: num_bss=%u conf->num_bss=%u",
1871 __func__, (unsigned int) iface->num_bss,
1872 (unsigned int) iface->conf->num_bss);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001873 driver = iface->bss[0]->driver;
1874 drv_priv = iface->bss[0]->drv_priv;
1875 hostapd_interface_deinit(iface);
Dmitry Shmidt54605472013-11-08 11:10:19 -08001876 wpa_printf(MSG_DEBUG, "%s: driver=%p drv_priv=%p -> hapd_deinit",
1877 __func__, driver, drv_priv);
Dmitry Shmidt71757432014-06-02 13:50:35 -07001878 if (driver && driver->hapd_deinit && drv_priv) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001879 driver->hapd_deinit(drv_priv);
Dmitry Shmidt71757432014-06-02 13:50:35 -07001880 iface->bss[0]->drv_priv = NULL;
1881 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001882 hostapd_interface_free(iface);
1883}
1884
1885
Dmitry Shmidt15907092014-03-25 10:42:57 -07001886static void hostapd_deinit_driver(const struct wpa_driver_ops *driver,
1887 void *drv_priv,
1888 struct hostapd_iface *hapd_iface)
1889{
1890 size_t j;
1891
1892 wpa_printf(MSG_DEBUG, "%s: driver=%p drv_priv=%p -> hapd_deinit",
1893 __func__, driver, drv_priv);
1894 if (driver && driver->hapd_deinit && drv_priv) {
1895 driver->hapd_deinit(drv_priv);
1896 for (j = 0; j < hapd_iface->num_bss; j++) {
1897 wpa_printf(MSG_DEBUG, "%s:bss[%d]->drv_priv=%p",
1898 __func__, (int) j,
1899 hapd_iface->bss[j]->drv_priv);
1900 if (hapd_iface->bss[j]->drv_priv == drv_priv)
1901 hapd_iface->bss[j]->drv_priv = NULL;
1902 }
1903 }
1904}
1905
1906
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001907int hostapd_enable_iface(struct hostapd_iface *hapd_iface)
1908{
Dmitry Shmidt71757432014-06-02 13:50:35 -07001909 size_t j;
1910
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001911 if (hapd_iface->bss[0]->drv_priv != NULL) {
1912 wpa_printf(MSG_ERROR, "Interface %s already enabled",
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001913 hapd_iface->conf->bss[0]->iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001914 return -1;
1915 }
1916
1917 wpa_printf(MSG_DEBUG, "Enable interface %s",
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001918 hapd_iface->conf->bss[0]->iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001919
Dmitry Shmidt71757432014-06-02 13:50:35 -07001920 for (j = 0; j < hapd_iface->num_bss; j++)
1921 hostapd_set_security_params(hapd_iface->conf->bss[j], 1);
Dmitry Shmidt344abd32014-01-14 13:17:00 -08001922 if (hostapd_config_check(hapd_iface->conf, 1) < 0) {
1923 wpa_printf(MSG_INFO, "Invalid configuration - cannot enable");
1924 return -1;
1925 }
1926
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001927 if (hapd_iface->interfaces == NULL ||
1928 hapd_iface->interfaces->driver_init == NULL ||
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001929 hapd_iface->interfaces->driver_init(hapd_iface))
1930 return -1;
1931
1932 if (hostapd_setup_interface(hapd_iface)) {
Dmitry Shmidt15907092014-03-25 10:42:57 -07001933 hostapd_deinit_driver(hapd_iface->bss[0]->driver,
1934 hapd_iface->bss[0]->drv_priv,
1935 hapd_iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001936 return -1;
1937 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001938
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001939 return 0;
1940}
1941
1942
1943int hostapd_reload_iface(struct hostapd_iface *hapd_iface)
1944{
1945 size_t j;
1946
1947 wpa_printf(MSG_DEBUG, "Reload interface %s",
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001948 hapd_iface->conf->bss[0]->iface);
1949 for (j = 0; j < hapd_iface->num_bss; j++)
Dmitry Shmidt71757432014-06-02 13:50:35 -07001950 hostapd_set_security_params(hapd_iface->conf->bss[j], 1);
Dmitry Shmidt344abd32014-01-14 13:17:00 -08001951 if (hostapd_config_check(hapd_iface->conf, 1) < 0) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001952 wpa_printf(MSG_ERROR, "Updated configuration is invalid");
1953 return -1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001954 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001955 hostapd_clear_old(hapd_iface);
1956 for (j = 0; j < hapd_iface->num_bss; j++)
1957 hostapd_reload_bss(hapd_iface->bss[j]);
1958
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001959 return 0;
1960}
1961
1962
1963int hostapd_disable_iface(struct hostapd_iface *hapd_iface)
1964{
1965 size_t j;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001966 const struct wpa_driver_ops *driver;
1967 void *drv_priv;
1968
1969 if (hapd_iface == NULL)
1970 return -1;
Dmitry Shmidt71757432014-06-02 13:50:35 -07001971
1972 if (hapd_iface->bss[0]->drv_priv == NULL) {
1973 wpa_printf(MSG_INFO, "Interface %s already disabled",
1974 hapd_iface->conf->bss[0]->iface);
1975 return -1;
1976 }
1977
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001978 wpa_msg(hapd_iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001979 driver = hapd_iface->bss[0]->driver;
1980 drv_priv = hapd_iface->bss[0]->drv_priv;
1981
Dmitry Shmidta38abf92014-03-06 13:38:44 -08001982 hapd_iface->driver_ap_teardown =
1983 !!(hapd_iface->drv_flags &
1984 WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
1985
1986 /* same as hostapd_interface_deinit without deinitializing ctrl-iface */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001987 for (j = 0; j < hapd_iface->num_bss; j++) {
1988 struct hostapd_data *hapd = hapd_iface->bss[j];
Dmitry Shmidt71757432014-06-02 13:50:35 -07001989 hostapd_bss_deinit_no_free(hapd);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001990 hostapd_free_hapd_data(hapd);
1991 }
1992
Dmitry Shmidt15907092014-03-25 10:42:57 -07001993 hostapd_deinit_driver(driver, drv_priv, hapd_iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001994
1995 /* From hostapd_cleanup_iface: These were initialized in
1996 * hostapd_setup_interface and hostapd_setup_interface_complete
1997 */
1998 hostapd_cleanup_iface_partial(hapd_iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001999
Dmitry Shmidt56052862013-10-04 10:23:25 -07002000 wpa_printf(MSG_DEBUG, "Interface %s disabled",
2001 hapd_iface->bss[0]->conf->iface);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002002 hostapd_set_state(hapd_iface, HAPD_IFACE_DISABLED);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002003 return 0;
2004}
2005
2006
2007static struct hostapd_iface *
2008hostapd_iface_alloc(struct hapd_interfaces *interfaces)
2009{
2010 struct hostapd_iface **iface, *hapd_iface;
2011
2012 iface = os_realloc_array(interfaces->iface, interfaces->count + 1,
2013 sizeof(struct hostapd_iface *));
2014 if (iface == NULL)
2015 return NULL;
2016 interfaces->iface = iface;
2017 hapd_iface = interfaces->iface[interfaces->count] =
2018 os_zalloc(sizeof(*hapd_iface));
2019 if (hapd_iface == NULL) {
2020 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory for "
2021 "the interface", __func__);
2022 return NULL;
2023 }
2024 interfaces->count++;
2025 hapd_iface->interfaces = interfaces;
2026
2027 return hapd_iface;
2028}
2029
2030
2031static struct hostapd_config *
2032hostapd_config_alloc(struct hapd_interfaces *interfaces, const char *ifname,
2033 const char *ctrl_iface)
2034{
2035 struct hostapd_bss_config *bss;
2036 struct hostapd_config *conf;
2037
2038 /* Allocates memory for bss and conf */
2039 conf = hostapd_config_defaults();
2040 if (conf == NULL) {
2041 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory for "
2042 "configuration", __func__);
2043 return NULL;
2044 }
2045
2046 conf->driver = wpa_drivers[0];
2047 if (conf->driver == NULL) {
2048 wpa_printf(MSG_ERROR, "No driver wrappers registered!");
2049 hostapd_config_free(conf);
2050 return NULL;
2051 }
2052
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002053 bss = conf->last_bss = conf->bss[0];
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002054
2055 os_strlcpy(bss->iface, ifname, sizeof(bss->iface));
2056 bss->ctrl_interface = os_strdup(ctrl_iface);
2057 if (bss->ctrl_interface == NULL) {
2058 hostapd_config_free(conf);
2059 return NULL;
2060 }
2061
2062 /* Reading configuration file skipped, will be done in SET!
2063 * From reading the configuration till the end has to be done in
2064 * SET
2065 */
2066 return conf;
2067}
2068
2069
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002070static int hostapd_data_alloc(struct hostapd_iface *hapd_iface,
2071 struct hostapd_config *conf)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002072{
2073 size_t i;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002074 struct hostapd_data *hapd;
2075
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002076 hapd_iface->bss = os_calloc(conf->num_bss,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002077 sizeof(struct hostapd_data *));
2078 if (hapd_iface->bss == NULL)
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002079 return -1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002080
2081 for (i = 0; i < conf->num_bss; i++) {
2082 hapd = hapd_iface->bss[i] =
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002083 hostapd_alloc_bss_data(hapd_iface, conf, conf->bss[i]);
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002084 if (hapd == NULL) {
2085 while (i > 0) {
2086 i--;
2087 os_free(hapd_iface->bss[i]);
2088 hapd_iface->bss[i] = NULL;
2089 }
2090 os_free(hapd_iface->bss);
2091 hapd_iface->bss = NULL;
2092 return -1;
2093 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002094 hapd->msg_ctx = hapd;
2095 }
2096
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002097 hapd_iface->conf = conf;
2098 hapd_iface->num_bss = conf->num_bss;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002099
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002100 return 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002101}
2102
2103
2104int hostapd_add_iface(struct hapd_interfaces *interfaces, char *buf)
2105{
2106 struct hostapd_config *conf = NULL;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002107 struct hostapd_iface *hapd_iface = NULL, *new_iface = NULL;
2108 struct hostapd_data *hapd;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002109 char *ptr;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002110 size_t i, j;
2111 const char *conf_file = NULL, *phy_name = NULL;
2112
2113 if (os_strncmp(buf, "bss_config=", 11) == 0) {
2114 char *pos;
2115 phy_name = buf + 11;
2116 pos = os_strchr(phy_name, ':');
2117 if (!pos)
2118 return -1;
2119 *pos++ = '\0';
2120 conf_file = pos;
2121 if (!os_strlen(conf_file))
2122 return -1;
2123
2124 hapd_iface = hostapd_interface_init_bss(interfaces, phy_name,
2125 conf_file, 0);
2126 if (!hapd_iface)
2127 return -1;
2128 for (j = 0; j < interfaces->count; j++) {
2129 if (interfaces->iface[j] == hapd_iface)
2130 break;
2131 }
2132 if (j == interfaces->count) {
2133 struct hostapd_iface **tmp;
2134 tmp = os_realloc_array(interfaces->iface,
2135 interfaces->count + 1,
2136 sizeof(struct hostapd_iface *));
2137 if (!tmp) {
2138 hostapd_interface_deinit_free(hapd_iface);
2139 return -1;
2140 }
2141 interfaces->iface = tmp;
2142 interfaces->iface[interfaces->count++] = hapd_iface;
2143 new_iface = hapd_iface;
2144 }
2145
2146 if (new_iface) {
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002147 if (interfaces->driver_init(hapd_iface))
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002148 goto fail;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002149
2150 if (hostapd_setup_interface(hapd_iface)) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002151 hostapd_deinit_driver(
2152 hapd_iface->bss[0]->driver,
2153 hapd_iface->bss[0]->drv_priv,
2154 hapd_iface);
2155 goto fail;
2156 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002157 } else {
2158 /* Assign new BSS with bss[0]'s driver info */
2159 hapd = hapd_iface->bss[hapd_iface->num_bss - 1];
2160 hapd->driver = hapd_iface->bss[0]->driver;
2161 hapd->drv_priv = hapd_iface->bss[0]->drv_priv;
2162 os_memcpy(hapd->own_addr, hapd_iface->bss[0]->own_addr,
2163 ETH_ALEN);
2164
2165 if (start_ctrl_iface_bss(hapd) < 0 ||
Dmitry Shmidt54605472013-11-08 11:10:19 -08002166 (hapd_iface->state == HAPD_IFACE_ENABLED &&
2167 hostapd_setup_bss(hapd, -1))) {
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002168 hostapd_cleanup(hapd);
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08002169 hapd_iface->bss[hapd_iface->num_bss - 1] = NULL;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002170 hapd_iface->conf->num_bss--;
2171 hapd_iface->num_bss--;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002172 wpa_printf(MSG_DEBUG, "%s: free hapd %p %s",
2173 __func__, hapd, hapd->conf->iface);
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002174 hostapd_config_free_bss(hapd->conf);
2175 hapd->conf = NULL;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002176 os_free(hapd);
2177 return -1;
2178 }
2179 }
2180 return 0;
2181 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002182
2183 ptr = os_strchr(buf, ' ');
2184 if (ptr == NULL)
2185 return -1;
2186 *ptr++ = '\0';
2187
Dmitry Shmidt56052862013-10-04 10:23:25 -07002188 if (os_strncmp(ptr, "config=", 7) == 0)
2189 conf_file = ptr + 7;
2190
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002191 for (i = 0; i < interfaces->count; i++) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002192 if (!os_strcmp(interfaces->iface[i]->conf->bss[0]->iface,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002193 buf)) {
2194 wpa_printf(MSG_INFO, "Cannot add interface - it "
2195 "already exists");
2196 return -1;
2197 }
2198 }
2199
2200 hapd_iface = hostapd_iface_alloc(interfaces);
2201 if (hapd_iface == NULL) {
2202 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
2203 "for interface", __func__);
2204 goto fail;
2205 }
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002206 new_iface = hapd_iface;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002207
Dmitry Shmidt56052862013-10-04 10:23:25 -07002208 if (conf_file && interfaces->config_read_cb) {
2209 conf = interfaces->config_read_cb(conf_file);
2210 if (conf && conf->bss)
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002211 os_strlcpy(conf->bss[0]->iface, buf,
2212 sizeof(conf->bss[0]->iface));
Dmitry Shmidt56052862013-10-04 10:23:25 -07002213 } else
2214 conf = hostapd_config_alloc(interfaces, buf, ptr);
2215 if (conf == NULL || conf->bss == NULL) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002216 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
2217 "for configuration", __func__);
2218 goto fail;
2219 }
2220
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002221 if (hostapd_data_alloc(hapd_iface, conf) < 0) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002222 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
2223 "for hostapd", __func__);
2224 goto fail;
2225 }
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002226 conf = NULL;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002227
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002228 if (start_ctrl_iface(hapd_iface) < 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002229 goto fail;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002230
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002231 wpa_printf(MSG_INFO, "Add interface '%s'",
2232 hapd_iface->conf->bss[0]->iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002233
2234 return 0;
2235
2236fail:
2237 if (conf)
2238 hostapd_config_free(conf);
2239 if (hapd_iface) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002240 if (hapd_iface->bss) {
Dmitry Shmidt54605472013-11-08 11:10:19 -08002241 for (i = 0; i < hapd_iface->num_bss; i++) {
2242 hapd = hapd_iface->bss[i];
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08002243 if (!hapd)
2244 continue;
2245 if (hapd_iface->interfaces &&
Dmitry Shmidt54605472013-11-08 11:10:19 -08002246 hapd_iface->interfaces->ctrl_iface_deinit)
2247 hapd_iface->interfaces->
2248 ctrl_iface_deinit(hapd);
2249 wpa_printf(MSG_DEBUG, "%s: free hapd %p (%s)",
2250 __func__, hapd_iface->bss[i],
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08002251 hapd->conf->iface);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002252 hostapd_cleanup(hapd);
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08002253 os_free(hapd);
2254 hapd_iface->bss[i] = NULL;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002255 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002256 os_free(hapd_iface->bss);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002257 hapd_iface->bss = NULL;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002258 }
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002259 if (new_iface) {
2260 interfaces->count--;
2261 interfaces->iface[interfaces->count] = NULL;
2262 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002263 hostapd_cleanup_iface(hapd_iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002264 }
2265 return -1;
2266}
2267
2268
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002269static int hostapd_remove_bss(struct hostapd_iface *iface, unsigned int idx)
2270{
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002271 size_t i;
2272
Dmitry Shmidt54605472013-11-08 11:10:19 -08002273 wpa_printf(MSG_INFO, "Remove BSS '%s'", iface->conf->bss[idx]->iface);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002274
Dmitry Shmidt54605472013-11-08 11:10:19 -08002275 /* Remove hostapd_data only if it has already been initialized */
2276 if (idx < iface->num_bss) {
2277 struct hostapd_data *hapd = iface->bss[idx];
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002278
Dmitry Shmidt54605472013-11-08 11:10:19 -08002279 hostapd_bss_deinit(hapd);
2280 wpa_printf(MSG_DEBUG, "%s: free hapd %p (%s)",
2281 __func__, hapd, hapd->conf->iface);
2282 hostapd_config_free_bss(hapd->conf);
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002283 hapd->conf = NULL;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002284 os_free(hapd);
2285
2286 iface->num_bss--;
2287
2288 for (i = idx; i < iface->num_bss; i++)
2289 iface->bss[i] = iface->bss[i + 1];
2290 } else {
2291 hostapd_config_free_bss(iface->conf->bss[idx]);
2292 iface->conf->bss[idx] = NULL;
2293 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002294
2295 iface->conf->num_bss--;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002296 for (i = idx; i < iface->conf->num_bss; i++)
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002297 iface->conf->bss[i] = iface->conf->bss[i + 1];
2298
2299 return 0;
2300}
2301
2302
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002303int hostapd_remove_iface(struct hapd_interfaces *interfaces, char *buf)
2304{
2305 struct hostapd_iface *hapd_iface;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002306 size_t i, j, k = 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002307
2308 for (i = 0; i < interfaces->count; i++) {
2309 hapd_iface = interfaces->iface[i];
2310 if (hapd_iface == NULL)
2311 return -1;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002312 if (!os_strcmp(hapd_iface->conf->bss[0]->iface, buf)) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002313 wpa_printf(MSG_INFO, "Remove interface '%s'", buf);
Dmitry Shmidta38abf92014-03-06 13:38:44 -08002314 hapd_iface->driver_ap_teardown =
2315 !!(hapd_iface->drv_flags &
2316 WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
2317
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002318 hostapd_interface_deinit_free(hapd_iface);
2319 k = i;
2320 while (k < (interfaces->count - 1)) {
2321 interfaces->iface[k] =
2322 interfaces->iface[k + 1];
2323 k++;
2324 }
2325 interfaces->count--;
2326 return 0;
2327 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002328
2329 for (j = 0; j < hapd_iface->conf->num_bss; j++) {
Dmitry Shmidta38abf92014-03-06 13:38:44 -08002330 if (!os_strcmp(hapd_iface->conf->bss[j]->iface, buf)) {
2331 hapd_iface->driver_ap_teardown =
2332 !(hapd_iface->drv_flags &
2333 WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002334 return hostapd_remove_bss(hapd_iface, j);
Dmitry Shmidta38abf92014-03-06 13:38:44 -08002335 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002336 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002337 }
2338 return -1;
2339}
2340
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002341
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002342/**
2343 * hostapd_new_assoc_sta - Notify that a new station associated with the AP
2344 * @hapd: Pointer to BSS data
2345 * @sta: Pointer to the associated STA data
2346 * @reassoc: 1 to indicate this was a re-association; 0 = first association
2347 *
2348 * This function will be called whenever a station associates with the AP. It
2349 * can be called from ieee802_11.c for drivers that export MLME to hostapd and
2350 * from drv_callbacks.c based on driver events for drivers that take care of
2351 * management frames (IEEE 802.11 authentication and association) internally.
2352 */
2353void hostapd_new_assoc_sta(struct hostapd_data *hapd, struct sta_info *sta,
2354 int reassoc)
2355{
2356 if (hapd->tkip_countermeasures) {
2357 hostapd_drv_sta_deauth(hapd, sta->addr,
2358 WLAN_REASON_MICHAEL_MIC_FAILURE);
2359 return;
2360 }
2361
2362 hostapd_prune_associations(hapd, sta->addr);
2363
2364 /* IEEE 802.11F (IAPP) */
2365 if (hapd->conf->ieee802_11f)
2366 iapp_new_station(hapd->iapp, sta);
2367
2368#ifdef CONFIG_P2P
2369 if (sta->p2p_ie == NULL && !sta->no_p2p_set) {
2370 sta->no_p2p_set = 1;
2371 hapd->num_sta_no_p2p++;
2372 if (hapd->num_sta_no_p2p == 1)
2373 hostapd_p2p_non_p2p_sta_connected(hapd);
2374 }
2375#endif /* CONFIG_P2P */
2376
2377 /* Start accounting here, if IEEE 802.1X and WPA are not used.
2378 * IEEE 802.1X/WPA code will start accounting after the station has
2379 * been authorized. */
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -08002380 if (!hapd->conf->ieee802_1x && !hapd->conf->wpa && !hapd->conf->osen) {
2381 ap_sta_set_authorized(hapd, sta, 1);
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002382 os_get_reltime(&sta->connected_time);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002383 accounting_sta_start(hapd, sta);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002384 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002385
2386 /* Start IEEE 802.1X authentication process for new stations */
2387 ieee802_1x_new_station(hapd, sta);
2388 if (reassoc) {
2389 if (sta->auth_alg != WLAN_AUTH_FT &&
2390 !(sta->flags & (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS)))
2391 wpa_auth_sm_event(sta->wpa_sm, WPA_REAUTH);
2392 } else
2393 wpa_auth_sta_associated(hapd->wpa_auth, sta->wpa_sm);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002394
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08002395 if (!(hapd->iface->drv_flags & WPA_DRIVER_FLAGS_INACTIVITY_TIMER)) {
2396 wpa_printf(MSG_DEBUG, "%s: reschedule ap_handle_timer timeout "
2397 "for " MACSTR " (%d seconds - ap_max_inactivity)",
2398 __func__, MAC2STR(sta->addr),
2399 hapd->conf->ap_max_inactivity);
2400 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
2401 eloop_register_timeout(hapd->conf->ap_max_inactivity, 0,
2402 ap_handle_timer, hapd, sta);
2403 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002404}
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002405
2406
2407const char * hostapd_state_text(enum hostapd_iface_state s)
2408{
2409 switch (s) {
2410 case HAPD_IFACE_UNINITIALIZED:
2411 return "UNINITIALIZED";
2412 case HAPD_IFACE_DISABLED:
2413 return "DISABLED";
2414 case HAPD_IFACE_COUNTRY_UPDATE:
2415 return "COUNTRY_UPDATE";
2416 case HAPD_IFACE_ACS:
2417 return "ACS";
2418 case HAPD_IFACE_HT_SCAN:
2419 return "HT_SCAN";
2420 case HAPD_IFACE_DFS:
2421 return "DFS";
2422 case HAPD_IFACE_ENABLED:
2423 return "ENABLED";
2424 }
2425
2426 return "UNKNOWN";
2427}
2428
2429
2430void hostapd_set_state(struct hostapd_iface *iface, enum hostapd_iface_state s)
2431{
2432 wpa_printf(MSG_INFO, "%s: interface state %s->%s",
2433 iface->conf->bss[0]->iface, hostapd_state_text(iface->state),
2434 hostapd_state_text(s));
2435 iface->state = s;
2436}
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002437
2438
2439#ifdef NEED_AP_MLME
2440
2441static void free_beacon_data(struct beacon_data *beacon)
2442{
2443 os_free(beacon->head);
2444 beacon->head = NULL;
2445 os_free(beacon->tail);
2446 beacon->tail = NULL;
2447 os_free(beacon->probe_resp);
2448 beacon->probe_resp = NULL;
2449 os_free(beacon->beacon_ies);
2450 beacon->beacon_ies = NULL;
2451 os_free(beacon->proberesp_ies);
2452 beacon->proberesp_ies = NULL;
2453 os_free(beacon->assocresp_ies);
2454 beacon->assocresp_ies = NULL;
2455}
2456
2457
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07002458static int hostapd_build_beacon_data(struct hostapd_data *hapd,
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002459 struct beacon_data *beacon)
2460{
2461 struct wpabuf *beacon_extra, *proberesp_extra, *assocresp_extra;
2462 struct wpa_driver_ap_params params;
2463 int ret;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002464
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08002465 os_memset(beacon, 0, sizeof(*beacon));
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002466 ret = ieee802_11_build_ap_params(hapd, &params);
2467 if (ret < 0)
2468 return ret;
2469
2470 ret = hostapd_build_ap_extra_ies(hapd, &beacon_extra,
2471 &proberesp_extra,
2472 &assocresp_extra);
2473 if (ret)
2474 goto free_ap_params;
2475
2476 ret = -1;
2477 beacon->head = os_malloc(params.head_len);
2478 if (!beacon->head)
2479 goto free_ap_extra_ies;
2480
2481 os_memcpy(beacon->head, params.head, params.head_len);
2482 beacon->head_len = params.head_len;
2483
2484 beacon->tail = os_malloc(params.tail_len);
2485 if (!beacon->tail)
2486 goto free_beacon;
2487
2488 os_memcpy(beacon->tail, params.tail, params.tail_len);
2489 beacon->tail_len = params.tail_len;
2490
2491 if (params.proberesp != NULL) {
2492 beacon->probe_resp = os_malloc(params.proberesp_len);
2493 if (!beacon->probe_resp)
2494 goto free_beacon;
2495
2496 os_memcpy(beacon->probe_resp, params.proberesp,
2497 params.proberesp_len);
2498 beacon->probe_resp_len = params.proberesp_len;
2499 }
2500
2501 /* copy the extra ies */
2502 if (beacon_extra) {
2503 beacon->beacon_ies = os_malloc(wpabuf_len(beacon_extra));
2504 if (!beacon->beacon_ies)
2505 goto free_beacon;
2506
2507 os_memcpy(beacon->beacon_ies,
2508 beacon_extra->buf, wpabuf_len(beacon_extra));
2509 beacon->beacon_ies_len = wpabuf_len(beacon_extra);
2510 }
2511
2512 if (proberesp_extra) {
2513 beacon->proberesp_ies =
2514 os_malloc(wpabuf_len(proberesp_extra));
2515 if (!beacon->proberesp_ies)
2516 goto free_beacon;
2517
2518 os_memcpy(beacon->proberesp_ies, proberesp_extra->buf,
2519 wpabuf_len(proberesp_extra));
2520 beacon->proberesp_ies_len = wpabuf_len(proberesp_extra);
2521 }
2522
2523 if (assocresp_extra) {
2524 beacon->assocresp_ies =
2525 os_malloc(wpabuf_len(assocresp_extra));
2526 if (!beacon->assocresp_ies)
2527 goto free_beacon;
2528
2529 os_memcpy(beacon->assocresp_ies, assocresp_extra->buf,
2530 wpabuf_len(assocresp_extra));
2531 beacon->assocresp_ies_len = wpabuf_len(assocresp_extra);
2532 }
2533
2534 ret = 0;
2535free_beacon:
2536 /* if the function fails, the caller should not free beacon data */
2537 if (ret)
2538 free_beacon_data(beacon);
2539
2540free_ap_extra_ies:
2541 hostapd_free_ap_extra_ies(hapd, beacon_extra, proberesp_extra,
2542 assocresp_extra);
2543free_ap_params:
2544 ieee802_11_free_ap_params(&params);
2545 return ret;
2546}
2547
2548
2549/*
2550 * TODO: This flow currently supports only changing frequency within the
2551 * same hw_mode. Any other changes to MAC parameters or provided settings (even
2552 * width) are not supported.
2553 */
2554static int hostapd_change_config_freq(struct hostapd_data *hapd,
2555 struct hostapd_config *conf,
2556 struct hostapd_freq_params *params,
2557 struct hostapd_freq_params *old_params)
2558{
2559 int channel;
2560
2561 if (!params->channel) {
2562 /* check if the new channel is supported by hw */
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07002563 params->channel = hostapd_hw_get_channel(hapd, params->freq);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002564 }
2565
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07002566 channel = params->channel;
2567 if (!channel)
2568 return -1;
2569
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002570 /* if a pointer to old_params is provided we save previous state */
2571 if (old_params) {
2572 old_params->channel = conf->channel;
2573 old_params->ht_enabled = conf->ieee80211n;
2574 old_params->sec_channel_offset = conf->secondary_channel;
2575 }
2576
2577 conf->channel = channel;
2578 conf->ieee80211n = params->ht_enabled;
2579 conf->secondary_channel = params->sec_channel_offset;
2580
2581 /* TODO: maybe call here hostapd_config_check here? */
2582
2583 return 0;
2584}
2585
2586
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07002587static int hostapd_fill_csa_settings(struct hostapd_data *hapd,
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002588 struct csa_settings *settings)
2589{
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07002590 struct hostapd_iface *iface = hapd->iface;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002591 struct hostapd_freq_params old_freq;
2592 int ret;
2593
2594 os_memset(&old_freq, 0, sizeof(old_freq));
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07002595 if (!iface || !iface->freq || hapd->csa_in_progress)
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002596 return -1;
2597
2598 ret = hostapd_change_config_freq(iface->bss[0], iface->conf,
2599 &settings->freq_params,
2600 &old_freq);
2601 if (ret)
2602 return ret;
2603
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07002604 ret = hostapd_build_beacon_data(hapd, &settings->beacon_after);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002605
2606 /* change back the configuration */
2607 hostapd_change_config_freq(iface->bss[0], iface->conf,
2608 &old_freq, NULL);
2609
2610 if (ret)
2611 return ret;
2612
2613 /* set channel switch parameters for csa ie */
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07002614 hapd->cs_freq_params = settings->freq_params;
2615 hapd->cs_count = settings->cs_count;
2616 hapd->cs_block_tx = settings->block_tx;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002617
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07002618 ret = hostapd_build_beacon_data(hapd, &settings->beacon_csa);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002619 if (ret) {
2620 free_beacon_data(&settings->beacon_after);
2621 return ret;
2622 }
2623
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07002624 settings->counter_offset_beacon = hapd->cs_c_off_beacon;
2625 settings->counter_offset_presp = hapd->cs_c_off_proberesp;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002626
2627 return 0;
2628}
2629
2630
2631void hostapd_cleanup_cs_params(struct hostapd_data *hapd)
2632{
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07002633 os_memset(&hapd->cs_freq_params, 0, sizeof(hapd->cs_freq_params));
2634 hapd->cs_count = 0;
2635 hapd->cs_block_tx = 0;
2636 hapd->cs_c_off_beacon = 0;
2637 hapd->cs_c_off_proberesp = 0;
2638 hapd->csa_in_progress = 0;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002639}
2640
2641
2642int hostapd_switch_channel(struct hostapd_data *hapd,
2643 struct csa_settings *settings)
2644{
2645 int ret;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002646
2647 if (!(hapd->iface->drv_flags & WPA_DRIVER_FLAGS_AP_CSA)) {
2648 wpa_printf(MSG_INFO, "CSA is not supported");
2649 return -1;
2650 }
2651
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07002652 ret = hostapd_fill_csa_settings(hapd, settings);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002653 if (ret)
2654 return ret;
2655
2656 ret = hostapd_drv_switch_channel(hapd, settings);
2657 free_beacon_data(&settings->beacon_csa);
2658 free_beacon_data(&settings->beacon_after);
2659
2660 if (ret) {
2661 /* if we failed, clean cs parameters */
2662 hostapd_cleanup_cs_params(hapd);
2663 return ret;
2664 }
2665
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07002666 hapd->csa_in_progress = 1;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002667 return 0;
2668}
2669
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07002670
2671void
2672hostapd_switch_channel_fallback(struct hostapd_iface *iface,
2673 const struct hostapd_freq_params *freq_params)
2674{
2675 int vht_seg0_idx = 0, vht_seg1_idx = 0, vht_bw = VHT_CHANWIDTH_USE_HT;
2676 unsigned int i;
2677
2678 wpa_printf(MSG_DEBUG, "Restarting all CSA-related BSSes");
2679
2680 if (freq_params->center_freq1)
2681 vht_seg0_idx = 36 + (freq_params->center_freq1 - 5180) / 5;
2682 if (freq_params->center_freq2)
2683 vht_seg1_idx = 36 + (freq_params->center_freq2 - 5180) / 5;
2684
2685 switch (freq_params->bandwidth) {
2686 case 0:
2687 case 20:
2688 case 40:
2689 vht_bw = VHT_CHANWIDTH_USE_HT;
2690 break;
2691 case 80:
2692 if (freq_params->center_freq2)
2693 vht_bw = VHT_CHANWIDTH_80P80MHZ;
2694 else
2695 vht_bw = VHT_CHANWIDTH_80MHZ;
2696 break;
2697 case 160:
2698 vht_bw = VHT_CHANWIDTH_160MHZ;
2699 break;
2700 default:
2701 wpa_printf(MSG_WARNING, "Unknown CSA bandwidth: %d",
2702 freq_params->bandwidth);
2703 break;
2704 }
2705
2706 iface->freq = freq_params->freq;
2707 iface->conf->channel = freq_params->channel;
2708 iface->conf->secondary_channel = freq_params->sec_channel_offset;
2709 iface->conf->vht_oper_centr_freq_seg0_idx = vht_seg0_idx;
2710 iface->conf->vht_oper_centr_freq_seg1_idx = vht_seg1_idx;
2711 iface->conf->vht_oper_chwidth = vht_bw;
2712 iface->conf->ieee80211n = freq_params->ht_enabled;
2713 iface->conf->ieee80211ac = freq_params->vht_enabled;
2714
2715 /*
2716 * cs_params must not be cleared earlier because the freq_params
2717 * argument may actually point to one of these.
2718 */
2719 for (i = 0; i < iface->num_bss; i++)
2720 hostapd_cleanup_cs_params(iface->bss[i]);
2721
2722 hostapd_disable_iface(iface);
2723 hostapd_enable_iface(iface);
2724}
2725
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002726#endif /* NEED_AP_MLME */