blob: ed7330193e792cb20312774a67fd4def997d7ade [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * hostapd / Initialization and configuration
Dmitry Shmidt344abd32014-01-14 13:17:00 -08003 * Copyright (c) 2002-2014, Jouni Malinen <j@w1.fi>
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004 *
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007 */
8
9#include "utils/includes.h"
10
11#include "utils/common.h"
12#include "utils/eloop.h"
13#include "common/ieee802_11_defs.h"
Dmitry Shmidtcce06662013-11-04 18:44:24 -080014#include "common/wpa_ctrl.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070015#include "radius/radius_client.h"
Dmitry Shmidt04949592012-07-19 12:16:46 -070016#include "radius/radius_das.h"
Dmitry Shmidt50b691d2014-05-21 14:01:45 -070017#include "eap_server/tncs.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070018#include "hostapd.h"
19#include "authsrv.h"
20#include "sta_info.h"
21#include "accounting.h"
22#include "ap_list.h"
23#include "beacon.h"
24#include "iapp.h"
25#include "ieee802_1x.h"
26#include "ieee802_11_auth.h"
27#include "vlan_init.h"
28#include "wpa_auth.h"
29#include "wps_hostapd.h"
30#include "hw_features.h"
31#include "wpa_auth_glue.h"
32#include "ap_drv_ops.h"
33#include "ap_config.h"
34#include "p2p_hostapd.h"
Dmitry Shmidt04949592012-07-19 12:16:46 -070035#include "gas_serv.h"
Dmitry Shmidt051af732013-10-22 13:52:46 -070036#include "dfs.h"
Dmitry Shmidt7832adb2014-04-29 10:53:02 -070037#include "ieee802_11.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070038
39
Dmitry Shmidt04949592012-07-19 12:16:46 -070040static int hostapd_flush_old_stations(struct hostapd_data *hapd, u16 reason);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070041static int hostapd_setup_encryption(char *iface, struct hostapd_data *hapd);
Dmitry Shmidtc55524a2011-07-07 11:18:38 -070042static int hostapd_broadcast_wep_clear(struct hostapd_data *hapd);
Dmitry Shmidtcce06662013-11-04 18:44:24 -080043static int setup_interface2(struct hostapd_iface *iface);
44static void channel_list_update_timeout(void *eloop_ctx, void *timeout_ctx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070045
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070046
Dmitry Shmidt04949592012-07-19 12:16:46 -070047int hostapd_for_each_interface(struct hapd_interfaces *interfaces,
48 int (*cb)(struct hostapd_iface *iface,
49 void *ctx), void *ctx)
50{
51 size_t i;
52 int ret;
53
54 for (i = 0; i < interfaces->count; i++) {
55 ret = cb(interfaces->iface[i], ctx);
56 if (ret)
57 return ret;
58 }
59
60 return 0;
61}
62
63
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070064static void hostapd_reload_bss(struct hostapd_data *hapd)
65{
Dmitry Shmidtcce06662013-11-04 18:44:24 -080066 struct hostapd_ssid *ssid;
67
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070068#ifndef CONFIG_NO_RADIUS
69 radius_client_reconfig(hapd->radius, hapd->conf->radius);
70#endif /* CONFIG_NO_RADIUS */
71
Dmitry Shmidtcce06662013-11-04 18:44:24 -080072 ssid = &hapd->conf->ssid;
73 if (!ssid->wpa_psk_set && ssid->wpa_psk && !ssid->wpa_psk->next &&
74 ssid->wpa_passphrase_set && ssid->wpa_passphrase) {
75 /*
76 * Force PSK to be derived again since SSID or passphrase may
77 * have changed.
78 */
79 os_free(ssid->wpa_psk);
80 ssid->wpa_psk = NULL;
81 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070082 if (hostapd_setup_wpa_psk(hapd->conf)) {
83 wpa_printf(MSG_ERROR, "Failed to re-configure WPA PSK "
84 "after reloading configuration");
85 }
86
87 if (hapd->conf->ieee802_1x || hapd->conf->wpa)
88 hostapd_set_drv_ieee8021x(hapd, hapd->conf->iface, 1);
89 else
90 hostapd_set_drv_ieee8021x(hapd, hapd->conf->iface, 0);
91
Dmitry Shmidtf21452a2014-02-26 10:55:25 -080092 if ((hapd->conf->wpa || hapd->conf->osen) && hapd->wpa_auth == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070093 hostapd_setup_wpa(hapd);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080094 if (hapd->wpa_auth)
95 wpa_init_keys(hapd->wpa_auth);
96 } else if (hapd->conf->wpa) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070097 const u8 *wpa_ie;
98 size_t wpa_ie_len;
99 hostapd_reconfig_wpa(hapd);
100 wpa_ie = wpa_auth_get_wpa_ie(hapd->wpa_auth, &wpa_ie_len);
101 if (hostapd_set_generic_elem(hapd, wpa_ie, wpa_ie_len))
102 wpa_printf(MSG_ERROR, "Failed to configure WPA IE for "
103 "the kernel driver.");
104 } else if (hapd->wpa_auth) {
105 wpa_deinit(hapd->wpa_auth);
106 hapd->wpa_auth = NULL;
107 hostapd_set_privacy(hapd, 0);
108 hostapd_setup_encryption(hapd->conf->iface, hapd);
109 hostapd_set_generic_elem(hapd, (u8 *) "", 0);
110 }
111
112 ieee802_11_set_beacon(hapd);
113 hostapd_update_wps(hapd);
114
115 if (hapd->conf->ssid.ssid_set &&
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700116 hostapd_set_ssid(hapd, hapd->conf->ssid.ssid,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700117 hapd->conf->ssid.ssid_len)) {
118 wpa_printf(MSG_ERROR, "Could not set SSID for kernel driver");
119 /* try to continue */
120 }
121 wpa_printf(MSG_DEBUG, "Reconfigured interface %s", hapd->conf->iface);
122}
123
124
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700125static void hostapd_clear_old(struct hostapd_iface *iface)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700126{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700127 size_t j;
128
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700129 /*
130 * Deauthenticate all stations since the new configuration may not
131 * allow them to use the BSS anymore.
132 */
133 for (j = 0; j < iface->num_bss; j++) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700134 hostapd_flush_old_stations(iface->bss[j],
135 WLAN_REASON_PREV_AUTH_NOT_VALID);
Dmitry Shmidtc55524a2011-07-07 11:18:38 -0700136 hostapd_broadcast_wep_clear(iface->bss[j]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700137
138#ifndef CONFIG_NO_RADIUS
139 /* TODO: update dynamic data based on changed configuration
140 * items (e.g., open/close sockets, etc.) */
141 radius_client_flush(iface->bss[j]->radius, 0);
142#endif /* CONFIG_NO_RADIUS */
143 }
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700144}
145
146
147int hostapd_reload_config(struct hostapd_iface *iface)
148{
149 struct hostapd_data *hapd = iface->bss[0];
150 struct hostapd_config *newconf, *oldconf;
151 size_t j;
152
153 if (iface->config_fname == NULL) {
154 /* Only in-memory config in use - assume it has been updated */
155 hostapd_clear_old(iface);
156 for (j = 0; j < iface->num_bss; j++)
157 hostapd_reload_bss(iface->bss[j]);
158 return 0;
159 }
160
161 if (iface->interfaces == NULL ||
162 iface->interfaces->config_read_cb == NULL)
163 return -1;
164 newconf = iface->interfaces->config_read_cb(iface->config_fname);
165 if (newconf == NULL)
166 return -1;
167
168 hostapd_clear_old(iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700169
170 oldconf = hapd->iconf;
171 iface->conf = newconf;
172
173 for (j = 0; j < iface->num_bss; j++) {
174 hapd = iface->bss[j];
175 hapd->iconf = newconf;
Dmitry Shmidtd11f0192014-03-24 12:09:47 -0700176 hapd->iconf->channel = oldconf->channel;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -0700177 hapd->iconf->secondary_channel = oldconf->secondary_channel;
Dmitry Shmidtd11f0192014-03-24 12:09:47 -0700178 hapd->iconf->ieee80211n = oldconf->ieee80211n;
179 hapd->iconf->ieee80211ac = oldconf->ieee80211ac;
180 hapd->iconf->ht_capab = oldconf->ht_capab;
181 hapd->iconf->vht_capab = oldconf->vht_capab;
182 hapd->iconf->vht_oper_chwidth = oldconf->vht_oper_chwidth;
183 hapd->iconf->vht_oper_centr_freq_seg0_idx =
184 oldconf->vht_oper_centr_freq_seg0_idx;
185 hapd->iconf->vht_oper_centr_freq_seg1_idx =
186 oldconf->vht_oper_centr_freq_seg1_idx;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800187 hapd->conf = newconf->bss[j];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700188 hostapd_reload_bss(hapd);
189 }
190
191 hostapd_config_free(oldconf);
192
193
194 return 0;
195}
196
197
198static void hostapd_broadcast_key_clear_iface(struct hostapd_data *hapd,
199 char *ifname)
200{
201 int i;
202
203 for (i = 0; i < NUM_WEP_KEYS; i++) {
204 if (hostapd_drv_set_key(ifname, hapd, WPA_ALG_NONE, NULL, i,
205 0, NULL, 0, NULL, 0)) {
206 wpa_printf(MSG_DEBUG, "Failed to clear default "
207 "encryption keys (ifname=%s keyidx=%d)",
208 ifname, i);
209 }
210 }
211#ifdef CONFIG_IEEE80211W
212 if (hapd->conf->ieee80211w) {
213 for (i = NUM_WEP_KEYS; i < NUM_WEP_KEYS + 2; i++) {
214 if (hostapd_drv_set_key(ifname, hapd, WPA_ALG_NONE,
215 NULL, i, 0, NULL,
216 0, NULL, 0)) {
217 wpa_printf(MSG_DEBUG, "Failed to clear "
218 "default mgmt encryption keys "
219 "(ifname=%s keyidx=%d)", ifname, i);
220 }
221 }
222 }
223#endif /* CONFIG_IEEE80211W */
224}
225
226
227static int hostapd_broadcast_wep_clear(struct hostapd_data *hapd)
228{
229 hostapd_broadcast_key_clear_iface(hapd, hapd->conf->iface);
230 return 0;
231}
232
233
234static int hostapd_broadcast_wep_set(struct hostapd_data *hapd)
235{
236 int errors = 0, idx;
237 struct hostapd_ssid *ssid = &hapd->conf->ssid;
238
239 idx = ssid->wep.idx;
240 if (ssid->wep.default_len &&
241 hostapd_drv_set_key(hapd->conf->iface,
242 hapd, WPA_ALG_WEP, broadcast_ether_addr, idx,
243 1, NULL, 0, ssid->wep.key[idx],
244 ssid->wep.len[idx])) {
245 wpa_printf(MSG_WARNING, "Could not set WEP encryption.");
246 errors++;
247 }
248
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700249 return errors;
250}
251
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700252
Dmitry Shmidt04949592012-07-19 12:16:46 -0700253static void hostapd_free_hapd_data(struct hostapd_data *hapd)
254{
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -0800255 if (!hapd->started) {
256 wpa_printf(MSG_ERROR, "%s: Interface %s wasn't started",
257 __func__, hapd->conf->iface);
258 return;
259 }
260 hapd->started = 0;
261
Dmitry Shmidt54605472013-11-08 11:10:19 -0800262 wpa_printf(MSG_DEBUG, "%s(%s)", __func__, hapd->conf->iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700263 iapp_deinit(hapd->iapp);
264 hapd->iapp = NULL;
265 accounting_deinit(hapd);
266 hostapd_deinit_wpa(hapd);
267 vlan_deinit(hapd);
268 hostapd_acl_deinit(hapd);
269#ifndef CONFIG_NO_RADIUS
270 radius_client_deinit(hapd->radius);
271 hapd->radius = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700272 radius_das_deinit(hapd->radius_das);
273 hapd->radius_das = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700274#endif /* CONFIG_NO_RADIUS */
275
276 hostapd_deinit_wps(hapd);
277
278 authsrv_deinit(hapd);
279
Dmitry Shmidt71757432014-06-02 13:50:35 -0700280 if (hapd->interface_added) {
281 hapd->interface_added = 0;
282 if (hostapd_if_remove(hapd, WPA_IF_AP_BSS, hapd->conf->iface)) {
283 wpa_printf(MSG_WARNING,
284 "Failed to remove BSS interface %s",
285 hapd->conf->iface);
286 hapd->interface_added = 1;
287 } else {
288 /*
289 * Since this was a dynamically added interface, the
290 * driver wrapper may have removed its internal instance
291 * and hapd->drv_priv is not valid anymore.
292 */
293 hapd->drv_priv = NULL;
294 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700295 }
296
297 os_free(hapd->probereq_cb);
298 hapd->probereq_cb = NULL;
299
300#ifdef CONFIG_P2P
301 wpabuf_free(hapd->p2p_beacon_ie);
302 hapd->p2p_beacon_ie = NULL;
303 wpabuf_free(hapd->p2p_probe_resp_ie);
304 hapd->p2p_probe_resp_ie = NULL;
305#endif /* CONFIG_P2P */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800306
307 wpabuf_free(hapd->time_adv);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700308
309#ifdef CONFIG_INTERWORKING
310 gas_serv_deinit(hapd);
311#endif /* CONFIG_INTERWORKING */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800312
313#ifdef CONFIG_SQLITE
314 os_free(hapd->tmp_eap_user.identity);
315 os_free(hapd->tmp_eap_user.password);
316#endif /* CONFIG_SQLITE */
Dmitry Shmidt04949592012-07-19 12:16:46 -0700317}
318
319
320/**
321 * hostapd_cleanup - Per-BSS cleanup (deinitialization)
322 * @hapd: Pointer to BSS data
323 *
324 * This function is used to free all per-BSS data structures and resources.
Dmitry Shmidt54605472013-11-08 11:10:19 -0800325 * Most of the modules that are initialized in hostapd_setup_bss() are
326 * deinitialized here.
Dmitry Shmidt04949592012-07-19 12:16:46 -0700327 */
328static void hostapd_cleanup(struct hostapd_data *hapd)
329{
Dmitry Shmidt54605472013-11-08 11:10:19 -0800330 wpa_printf(MSG_DEBUG, "%s(hapd=%p (%s))", __func__, hapd,
331 hapd->conf->iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700332 if (hapd->iface->interfaces &&
333 hapd->iface->interfaces->ctrl_iface_deinit)
334 hapd->iface->interfaces->ctrl_iface_deinit(hapd);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700335 hostapd_free_hapd_data(hapd);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700336}
337
338
Dmitry Shmidt04949592012-07-19 12:16:46 -0700339static void hostapd_cleanup_iface_partial(struct hostapd_iface *iface)
340{
Dmitry Shmidt54605472013-11-08 11:10:19 -0800341 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700342 hostapd_free_hw_features(iface->hw_features, iface->num_hw_features);
343 iface->hw_features = NULL;
344 os_free(iface->current_rates);
345 iface->current_rates = NULL;
346 os_free(iface->basic_rates);
347 iface->basic_rates = NULL;
348 ap_list_deinit(iface);
349}
350
351
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700352/**
353 * hostapd_cleanup_iface - Complete per-interface cleanup
354 * @iface: Pointer to interface data
355 *
356 * This function is called after per-BSS data structures are deinitialized
357 * with hostapd_cleanup().
358 */
359static void hostapd_cleanup_iface(struct hostapd_iface *iface)
360{
Dmitry Shmidt54605472013-11-08 11:10:19 -0800361 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800362 eloop_cancel_timeout(channel_list_update_timeout, iface, NULL);
363
Dmitry Shmidt04949592012-07-19 12:16:46 -0700364 hostapd_cleanup_iface_partial(iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700365 hostapd_config_free(iface->conf);
366 iface->conf = NULL;
367
368 os_free(iface->config_fname);
369 os_free(iface->bss);
Dmitry Shmidt54605472013-11-08 11:10:19 -0800370 wpa_printf(MSG_DEBUG, "%s: free iface=%p", __func__, iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700371 os_free(iface);
372}
373
374
Dmitry Shmidt04949592012-07-19 12:16:46 -0700375static void hostapd_clear_wep(struct hostapd_data *hapd)
376{
Dmitry Shmidta38abf92014-03-06 13:38:44 -0800377 if (hapd->drv_priv && !hapd->iface->driver_ap_teardown) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700378 hostapd_set_privacy(hapd, 0);
379 hostapd_broadcast_wep_clear(hapd);
380 }
381}
382
383
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700384static int hostapd_setup_encryption(char *iface, struct hostapd_data *hapd)
385{
386 int i;
387
388 hostapd_broadcast_wep_set(hapd);
389
390 if (hapd->conf->ssid.wep.default_len) {
391 hostapd_set_privacy(hapd, 1);
392 return 0;
393 }
394
Jouni Malinen75ecf522011-06-27 15:19:46 -0700395 /*
396 * When IEEE 802.1X is not enabled, the driver may need to know how to
397 * set authentication algorithms for static WEP.
398 */
399 hostapd_drv_set_authmode(hapd, hapd->conf->auth_algs);
400
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700401 for (i = 0; i < 4; i++) {
402 if (hapd->conf->ssid.wep.key[i] &&
403 hostapd_drv_set_key(iface, hapd, WPA_ALG_WEP, NULL, i,
404 i == hapd->conf->ssid.wep.idx, NULL, 0,
405 hapd->conf->ssid.wep.key[i],
406 hapd->conf->ssid.wep.len[i])) {
407 wpa_printf(MSG_WARNING, "Could not set WEP "
408 "encryption.");
409 return -1;
410 }
411 if (hapd->conf->ssid.wep.key[i] &&
412 i == hapd->conf->ssid.wep.idx)
413 hostapd_set_privacy(hapd, 1);
414 }
415
416 return 0;
417}
418
419
Dmitry Shmidt04949592012-07-19 12:16:46 -0700420static int hostapd_flush_old_stations(struct hostapd_data *hapd, u16 reason)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700421{
422 int ret = 0;
423 u8 addr[ETH_ALEN];
424
425 if (hostapd_drv_none(hapd) || hapd->drv_priv == NULL)
426 return 0;
427
Dmitry Shmidta38abf92014-03-06 13:38:44 -0800428 if (!hapd->iface->driver_ap_teardown) {
429 wpa_dbg(hapd->msg_ctx, MSG_DEBUG,
430 "Flushing old station entries");
431
432 if (hostapd_flush(hapd)) {
433 wpa_msg(hapd->msg_ctx, MSG_WARNING,
434 "Could not connect to kernel driver");
435 ret = -1;
436 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700437 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800438 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "Deauthenticate all stations");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700439 os_memset(addr, 0xff, ETH_ALEN);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700440 hostapd_drv_sta_deauth(hapd, addr, reason);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700441 hostapd_free_stas(hapd);
442
443 return ret;
444}
445
446
Dmitry Shmidt71757432014-06-02 13:50:35 -0700447static void hostapd_bss_deinit_no_free(struct hostapd_data *hapd)
448{
449 hostapd_free_stas(hapd);
450 hostapd_flush_old_stations(hapd, WLAN_REASON_DEAUTH_LEAVING);
451 hostapd_clear_wep(hapd);
452}
453
454
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700455/**
456 * hostapd_validate_bssid_configuration - Validate BSSID configuration
457 * @iface: Pointer to interface data
458 * Returns: 0 on success, -1 on failure
459 *
460 * This function is used to validate that the configured BSSIDs are valid.
461 */
462static int hostapd_validate_bssid_configuration(struct hostapd_iface *iface)
463{
464 u8 mask[ETH_ALEN] = { 0 };
465 struct hostapd_data *hapd = iface->bss[0];
466 unsigned int i = iface->conf->num_bss, bits = 0, j;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700467 int auto_addr = 0;
468
469 if (hostapd_drv_none(hapd))
470 return 0;
471
472 /* Generate BSSID mask that is large enough to cover the BSSIDs. */
473
474 /* Determine the bits necessary to cover the number of BSSIDs. */
475 for (i--; i; i >>= 1)
476 bits++;
477
478 /* Determine the bits necessary to any configured BSSIDs,
479 if they are higher than the number of BSSIDs. */
480 for (j = 0; j < iface->conf->num_bss; j++) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800481 if (hostapd_mac_comp_empty(iface->conf->bss[j]->bssid) == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700482 if (j)
483 auto_addr++;
484 continue;
485 }
486
487 for (i = 0; i < ETH_ALEN; i++) {
488 mask[i] |=
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800489 iface->conf->bss[j]->bssid[i] ^
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700490 hapd->own_addr[i];
491 }
492 }
493
494 if (!auto_addr)
495 goto skip_mask_ext;
496
497 for (i = 0; i < ETH_ALEN && mask[i] == 0; i++)
498 ;
499 j = 0;
500 if (i < ETH_ALEN) {
501 j = (5 - i) * 8;
502
503 while (mask[i] != 0) {
504 mask[i] >>= 1;
505 j++;
506 }
507 }
508
509 if (bits < j)
510 bits = j;
511
512 if (bits > 40) {
513 wpa_printf(MSG_ERROR, "Too many bits in the BSSID mask (%u)",
514 bits);
515 return -1;
516 }
517
518 os_memset(mask, 0xff, ETH_ALEN);
519 j = bits / 8;
520 for (i = 5; i > 5 - j; i--)
521 mask[i] = 0;
522 j = bits % 8;
523 while (j--)
524 mask[i] <<= 1;
525
526skip_mask_ext:
527 wpa_printf(MSG_DEBUG, "BSS count %lu, BSSID mask " MACSTR " (%d bits)",
528 (unsigned long) iface->conf->num_bss, MAC2STR(mask), bits);
529
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700530 if (!auto_addr)
531 return 0;
532
533 for (i = 0; i < ETH_ALEN; i++) {
534 if ((hapd->own_addr[i] & mask[i]) != hapd->own_addr[i]) {
535 wpa_printf(MSG_ERROR, "Invalid BSSID mask " MACSTR
536 " for start address " MACSTR ".",
537 MAC2STR(mask), MAC2STR(hapd->own_addr));
538 wpa_printf(MSG_ERROR, "Start address must be the "
539 "first address in the block (i.e., addr "
540 "AND mask == addr).");
541 return -1;
542 }
543 }
544
545 return 0;
546}
547
548
549static int mac_in_conf(struct hostapd_config *conf, const void *a)
550{
551 size_t i;
552
553 for (i = 0; i < conf->num_bss; i++) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800554 if (hostapd_mac_comp(conf->bss[i]->bssid, a) == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700555 return 1;
556 }
557 }
558
559 return 0;
560}
561
562
Dmitry Shmidt04949592012-07-19 12:16:46 -0700563#ifndef CONFIG_NO_RADIUS
564
565static int hostapd_das_nas_mismatch(struct hostapd_data *hapd,
566 struct radius_das_attrs *attr)
567{
Dmitry Shmidt13ca8d82014-02-20 10:18:40 -0800568 if (attr->nas_identifier &&
569 (!hapd->conf->nas_identifier ||
570 os_strlen(hapd->conf->nas_identifier) !=
571 attr->nas_identifier_len ||
572 os_memcmp(hapd->conf->nas_identifier, attr->nas_identifier,
573 attr->nas_identifier_len) != 0)) {
574 wpa_printf(MSG_DEBUG, "RADIUS DAS: NAS-Identifier mismatch");
575 return 1;
576 }
577
578 if (attr->nas_ip_addr &&
579 (hapd->conf->own_ip_addr.af != AF_INET ||
580 os_memcmp(&hapd->conf->own_ip_addr.u.v4, attr->nas_ip_addr, 4) !=
581 0)) {
582 wpa_printf(MSG_DEBUG, "RADIUS DAS: NAS-IP-Address mismatch");
583 return 1;
584 }
585
586#ifdef CONFIG_IPV6
587 if (attr->nas_ipv6_addr &&
588 (hapd->conf->own_ip_addr.af != AF_INET6 ||
589 os_memcmp(&hapd->conf->own_ip_addr.u.v6, attr->nas_ipv6_addr, 16)
590 != 0)) {
591 wpa_printf(MSG_DEBUG, "RADIUS DAS: NAS-IPv6-Address mismatch");
592 return 1;
593 }
594#endif /* CONFIG_IPV6 */
595
Dmitry Shmidt04949592012-07-19 12:16:46 -0700596 return 0;
597}
598
599
600static struct sta_info * hostapd_das_find_sta(struct hostapd_data *hapd,
601 struct radius_das_attrs *attr)
602{
603 struct sta_info *sta = NULL;
604 char buf[128];
605
606 if (attr->sta_addr)
607 sta = ap_get_sta(hapd, attr->sta_addr);
608
609 if (sta == NULL && attr->acct_session_id &&
610 attr->acct_session_id_len == 17) {
611 for (sta = hapd->sta_list; sta; sta = sta->next) {
612 os_snprintf(buf, sizeof(buf), "%08X-%08X",
613 sta->acct_session_id_hi,
614 sta->acct_session_id_lo);
615 if (os_memcmp(attr->acct_session_id, buf, 17) == 0)
616 break;
617 }
618 }
619
620 if (sta == NULL && attr->cui) {
621 for (sta = hapd->sta_list; sta; sta = sta->next) {
622 struct wpabuf *cui;
623 cui = ieee802_1x_get_radius_cui(sta->eapol_sm);
624 if (cui && wpabuf_len(cui) == attr->cui_len &&
625 os_memcmp(wpabuf_head(cui), attr->cui,
626 attr->cui_len) == 0)
627 break;
628 }
629 }
630
631 if (sta == NULL && attr->user_name) {
632 for (sta = hapd->sta_list; sta; sta = sta->next) {
633 u8 *identity;
634 size_t identity_len;
635 identity = ieee802_1x_get_identity(sta->eapol_sm,
636 &identity_len);
637 if (identity &&
638 identity_len == attr->user_name_len &&
639 os_memcmp(identity, attr->user_name, identity_len)
640 == 0)
641 break;
642 }
643 }
644
645 return sta;
646}
647
648
649static enum radius_das_res
650hostapd_das_disconnect(void *ctx, struct radius_das_attrs *attr)
651{
652 struct hostapd_data *hapd = ctx;
653 struct sta_info *sta;
654
655 if (hostapd_das_nas_mismatch(hapd, attr))
656 return RADIUS_DAS_NAS_MISMATCH;
657
658 sta = hostapd_das_find_sta(hapd, attr);
659 if (sta == NULL)
660 return RADIUS_DAS_SESSION_NOT_FOUND;
661
Dmitry Shmidt13ca8d82014-02-20 10:18:40 -0800662 wpa_auth_pmksa_remove(hapd->wpa_auth, sta->addr);
663
Dmitry Shmidt04949592012-07-19 12:16:46 -0700664 hostapd_drv_sta_deauth(hapd, sta->addr,
665 WLAN_REASON_PREV_AUTH_NOT_VALID);
666 ap_sta_deauthenticate(hapd, sta, WLAN_REASON_PREV_AUTH_NOT_VALID);
667
668 return RADIUS_DAS_SUCCESS;
669}
670
671#endif /* CONFIG_NO_RADIUS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700672
673
674/**
675 * hostapd_setup_bss - Per-BSS setup (initialization)
676 * @hapd: Pointer to BSS data
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800677 * @first: Whether this BSS is the first BSS of an interface; -1 = not first,
678 * but interface may exist
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700679 *
680 * This function is used to initialize all per-BSS data structures and
681 * resources. This gets called in a loop for each BSS when an interface is
682 * initialized. Most of the modules that are initialized here will be
683 * deinitialized in hostapd_cleanup().
684 */
685static int hostapd_setup_bss(struct hostapd_data *hapd, int first)
686{
687 struct hostapd_bss_config *conf = hapd->conf;
688 u8 ssid[HOSTAPD_MAX_SSID_LEN + 1];
689 int ssid_len, set_ssid;
690 char force_ifname[IFNAMSIZ];
691 u8 if_addr[ETH_ALEN];
692
Dmitry Shmidt54605472013-11-08 11:10:19 -0800693 wpa_printf(MSG_DEBUG, "%s(hapd=%p (%s), first=%d)",
694 __func__, hapd, hapd->conf->iface, first);
695
Dmitry Shmidt50b691d2014-05-21 14:01:45 -0700696#ifdef EAP_SERVER_TNC
697 if (hapd->conf->tnc && tncs_global_init() < 0) {
698 wpa_printf(MSG_ERROR, "Failed to initialize TNCS");
699 return -1;
700 }
701#endif /* EAP_SERVER_TNC */
702
Dmitry Shmidt54605472013-11-08 11:10:19 -0800703 if (hapd->started) {
704 wpa_printf(MSG_ERROR, "%s: Interface %s was already started",
705 __func__, hapd->conf->iface);
706 return -1;
707 }
708 hapd->started = 1;
709
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800710 if (!first || first == -1) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700711 if (hostapd_mac_comp_empty(hapd->conf->bssid) == 0) {
712 /* Allocate the next available BSSID. */
713 do {
714 inc_byte_array(hapd->own_addr, ETH_ALEN);
715 } while (mac_in_conf(hapd->iconf, hapd->own_addr));
716 } else {
717 /* Allocate the configured BSSID. */
718 os_memcpy(hapd->own_addr, hapd->conf->bssid, ETH_ALEN);
719
720 if (hostapd_mac_comp(hapd->own_addr,
721 hapd->iface->bss[0]->own_addr) ==
722 0) {
723 wpa_printf(MSG_ERROR, "BSS '%s' may not have "
724 "BSSID set to the MAC address of "
725 "the radio", hapd->conf->iface);
726 return -1;
727 }
728 }
729
Dmitry Shmidt54605472013-11-08 11:10:19 -0800730 hapd->interface_added = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700731 if (hostapd_if_add(hapd->iface->bss[0], WPA_IF_AP_BSS,
732 hapd->conf->iface, hapd->own_addr, hapd,
733 &hapd->drv_priv, force_ifname, if_addr,
734 hapd->conf->bridge[0] ? hapd->conf->bridge :
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800735 NULL, first == -1)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700736 wpa_printf(MSG_ERROR, "Failed to add BSS (BSSID="
737 MACSTR ")", MAC2STR(hapd->own_addr));
Dmitry Shmidt3cf6f792013-12-18 13:12:19 -0800738 hapd->interface_added = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700739 return -1;
740 }
741 }
742
743 if (conf->wmm_enabled < 0)
744 conf->wmm_enabled = hapd->iconf->ieee80211n;
745
Dmitry Shmidt04949592012-07-19 12:16:46 -0700746 hostapd_flush_old_stations(hapd, WLAN_REASON_PREV_AUTH_NOT_VALID);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700747 hostapd_set_privacy(hapd, 0);
748
749 hostapd_broadcast_wep_clear(hapd);
750 if (hostapd_setup_encryption(hapd->conf->iface, hapd))
751 return -1;
752
753 /*
754 * Fetch the SSID from the system and use it or,
755 * if one was specified in the config file, verify they
756 * match.
757 */
758 ssid_len = hostapd_get_ssid(hapd, ssid, sizeof(ssid));
759 if (ssid_len < 0) {
760 wpa_printf(MSG_ERROR, "Could not read SSID from system");
761 return -1;
762 }
763 if (conf->ssid.ssid_set) {
764 /*
765 * If SSID is specified in the config file and it differs
766 * from what is being used then force installation of the
767 * new SSID.
768 */
769 set_ssid = (conf->ssid.ssid_len != (size_t) ssid_len ||
770 os_memcmp(conf->ssid.ssid, ssid, ssid_len) != 0);
771 } else {
772 /*
773 * No SSID in the config file; just use the one we got
774 * from the system.
775 */
776 set_ssid = 0;
777 conf->ssid.ssid_len = ssid_len;
778 os_memcpy(conf->ssid.ssid, ssid, conf->ssid.ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700779 }
780
781 if (!hostapd_drv_none(hapd)) {
782 wpa_printf(MSG_ERROR, "Using interface %s with hwaddr " MACSTR
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700783 " and ssid \"%s\"",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700784 hapd->conf->iface, MAC2STR(hapd->own_addr),
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700785 wpa_ssid_txt(hapd->conf->ssid.ssid,
786 hapd->conf->ssid.ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700787 }
788
789 if (hostapd_setup_wpa_psk(conf)) {
790 wpa_printf(MSG_ERROR, "WPA-PSK setup failed.");
791 return -1;
792 }
793
794 /* Set SSID for the kernel driver (to be used in beacon and probe
795 * response frames) */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700796 if (set_ssid && hostapd_set_ssid(hapd, conf->ssid.ssid,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700797 conf->ssid.ssid_len)) {
798 wpa_printf(MSG_ERROR, "Could not set SSID for kernel driver");
799 return -1;
800 }
801
Dmitry Shmidt818ea482014-03-10 13:15:21 -0700802 if (wpa_debug_level <= MSG_MSGDUMP)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700803 conf->radius->msg_dumps = 1;
804#ifndef CONFIG_NO_RADIUS
805 hapd->radius = radius_client_init(hapd, conf->radius);
806 if (hapd->radius == NULL) {
807 wpa_printf(MSG_ERROR, "RADIUS client initialization failed.");
808 return -1;
809 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700810
811 if (hapd->conf->radius_das_port) {
812 struct radius_das_conf das_conf;
813 os_memset(&das_conf, 0, sizeof(das_conf));
814 das_conf.port = hapd->conf->radius_das_port;
815 das_conf.shared_secret = hapd->conf->radius_das_shared_secret;
816 das_conf.shared_secret_len =
817 hapd->conf->radius_das_shared_secret_len;
818 das_conf.client_addr = &hapd->conf->radius_das_client_addr;
819 das_conf.time_window = hapd->conf->radius_das_time_window;
820 das_conf.require_event_timestamp =
821 hapd->conf->radius_das_require_event_timestamp;
822 das_conf.ctx = hapd;
823 das_conf.disconnect = hostapd_das_disconnect;
824 hapd->radius_das = radius_das_init(&das_conf);
825 if (hapd->radius_das == NULL) {
826 wpa_printf(MSG_ERROR, "RADIUS DAS initialization "
827 "failed.");
828 return -1;
829 }
830 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700831#endif /* CONFIG_NO_RADIUS */
832
833 if (hostapd_acl_init(hapd)) {
834 wpa_printf(MSG_ERROR, "ACL initialization failed.");
835 return -1;
836 }
837 if (hostapd_init_wps(hapd, conf))
838 return -1;
839
840 if (authsrv_init(hapd) < 0)
841 return -1;
842
843 if (ieee802_1x_init(hapd)) {
844 wpa_printf(MSG_ERROR, "IEEE 802.1X initialization failed.");
845 return -1;
846 }
847
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800848 if ((hapd->conf->wpa || hapd->conf->osen) && hostapd_setup_wpa(hapd))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700849 return -1;
850
851 if (accounting_init(hapd)) {
852 wpa_printf(MSG_ERROR, "Accounting initialization failed.");
853 return -1;
854 }
855
856 if (hapd->conf->ieee802_11f &&
857 (hapd->iapp = iapp_init(hapd, hapd->conf->iapp_iface)) == NULL) {
858 wpa_printf(MSG_ERROR, "IEEE 802.11F (IAPP) initialization "
859 "failed.");
860 return -1;
861 }
862
Dmitry Shmidt04949592012-07-19 12:16:46 -0700863#ifdef CONFIG_INTERWORKING
864 if (gas_serv_init(hapd)) {
865 wpa_printf(MSG_ERROR, "GAS server initialization failed");
866 return -1;
867 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700868
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800869 if (conf->qos_map_set_len &&
870 hostapd_drv_set_qos_map(hapd, conf->qos_map_set,
871 conf->qos_map_set_len)) {
872 wpa_printf(MSG_ERROR, "Failed to initialize QoS Map");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700873 return -1;
874 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800875#endif /* CONFIG_INTERWORKING */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700876
877 if (!hostapd_drv_none(hapd) && vlan_init(hapd)) {
878 wpa_printf(MSG_ERROR, "VLAN initialization failed.");
879 return -1;
880 }
881
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800882 if (!hapd->conf->start_disabled && ieee802_11_set_beacon(hapd) < 0)
883 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700884
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800885 if (hapd->wpa_auth && wpa_init_keys(hapd->wpa_auth) < 0)
886 return -1;
887
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700888 if (hapd->driver && hapd->driver->set_operstate)
889 hapd->driver->set_operstate(hapd->drv_priv, 1);
890
891 return 0;
892}
893
894
895static void hostapd_tx_queue_params(struct hostapd_iface *iface)
896{
897 struct hostapd_data *hapd = iface->bss[0];
898 int i;
899 struct hostapd_tx_queue_params *p;
900
901 for (i = 0; i < NUM_TX_QUEUES; i++) {
902 p = &iface->conf->tx_queue[i];
903
904 if (hostapd_set_tx_queue_params(hapd, i, p->aifs, p->cwmin,
905 p->cwmax, p->burst)) {
906 wpa_printf(MSG_DEBUG, "Failed to set TX queue "
907 "parameters for queue %d.", i);
908 /* Continue anyway */
909 }
910 }
911}
912
913
Dmitry Shmidt8bae4132013-06-06 11:25:10 -0700914static int hostapd_set_acl_list(struct hostapd_data *hapd,
915 struct mac_acl_entry *mac_acl,
916 int n_entries, u8 accept_acl)
917{
918 struct hostapd_acl_params *acl_params;
919 int i, err;
920
921 acl_params = os_zalloc(sizeof(*acl_params) +
922 (n_entries * sizeof(acl_params->mac_acl[0])));
923 if (!acl_params)
924 return -ENOMEM;
925
926 for (i = 0; i < n_entries; i++)
927 os_memcpy(acl_params->mac_acl[i].addr, mac_acl[i].addr,
928 ETH_ALEN);
929
930 acl_params->acl_policy = accept_acl;
931 acl_params->num_mac_acl = n_entries;
932
933 err = hostapd_drv_set_acl(hapd, acl_params);
934
935 os_free(acl_params);
936
937 return err;
938}
939
940
941static void hostapd_set_acl(struct hostapd_data *hapd)
942{
943 struct hostapd_config *conf = hapd->iconf;
944 int err;
945 u8 accept_acl;
946
947 if (hapd->iface->drv_max_acl_mac_addrs == 0)
948 return;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800949 if (!(conf->bss[0]->num_accept_mac || conf->bss[0]->num_deny_mac))
Dmitry Shmidt8bae4132013-06-06 11:25:10 -0700950 return;
951
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800952 if (conf->bss[0]->macaddr_acl == DENY_UNLESS_ACCEPTED) {
953 if (conf->bss[0]->num_accept_mac) {
Dmitry Shmidt8bae4132013-06-06 11:25:10 -0700954 accept_acl = 1;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800955 err = hostapd_set_acl_list(hapd,
956 conf->bss[0]->accept_mac,
957 conf->bss[0]->num_accept_mac,
Dmitry Shmidt8bae4132013-06-06 11:25:10 -0700958 accept_acl);
959 if (err) {
960 wpa_printf(MSG_DEBUG, "Failed to set accept acl");
961 return;
962 }
963 } else {
964 wpa_printf(MSG_DEBUG, "Mismatch between ACL Policy & Accept/deny lists file");
965 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800966 } else if (conf->bss[0]->macaddr_acl == ACCEPT_UNLESS_DENIED) {
967 if (conf->bss[0]->num_deny_mac) {
Dmitry Shmidt8bae4132013-06-06 11:25:10 -0700968 accept_acl = 0;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800969 err = hostapd_set_acl_list(hapd, conf->bss[0]->deny_mac,
970 conf->bss[0]->num_deny_mac,
Dmitry Shmidt8bae4132013-06-06 11:25:10 -0700971 accept_acl);
972 if (err) {
973 wpa_printf(MSG_DEBUG, "Failed to set deny acl");
974 return;
975 }
976 } else {
977 wpa_printf(MSG_DEBUG, "Mismatch between ACL Policy & Accept/deny lists file");
978 }
979 }
980}
981
982
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800983static int start_ctrl_iface_bss(struct hostapd_data *hapd)
984{
985 if (!hapd->iface->interfaces ||
986 !hapd->iface->interfaces->ctrl_iface_init)
987 return 0;
988
989 if (hapd->iface->interfaces->ctrl_iface_init(hapd)) {
990 wpa_printf(MSG_ERROR,
991 "Failed to setup control interface for %s",
992 hapd->conf->iface);
993 return -1;
994 }
995
996 return 0;
997}
998
999
1000static int start_ctrl_iface(struct hostapd_iface *iface)
1001{
1002 size_t i;
1003
1004 if (!iface->interfaces || !iface->interfaces->ctrl_iface_init)
1005 return 0;
1006
1007 for (i = 0; i < iface->num_bss; i++) {
1008 struct hostapd_data *hapd = iface->bss[i];
1009 if (iface->interfaces->ctrl_iface_init(hapd)) {
1010 wpa_printf(MSG_ERROR,
1011 "Failed to setup control interface for %s",
1012 hapd->conf->iface);
1013 return -1;
1014 }
1015 }
1016
1017 return 0;
1018}
1019
1020
1021static void channel_list_update_timeout(void *eloop_ctx, void *timeout_ctx)
1022{
1023 struct hostapd_iface *iface = eloop_ctx;
1024
1025 if (!iface->wait_channel_update) {
1026 wpa_printf(MSG_INFO, "Channel list update timeout, but interface was not waiting for it");
1027 return;
1028 }
1029
1030 /*
1031 * It is possible that the existing channel list is acceptable, so try
1032 * to proceed.
1033 */
1034 wpa_printf(MSG_DEBUG, "Channel list update timeout - try to continue anyway");
1035 setup_interface2(iface);
1036}
1037
1038
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001039void hostapd_channel_list_updated(struct hostapd_iface *iface, int initiator)
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001040{
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001041 if (!iface->wait_channel_update || initiator != REGDOM_SET_BY_USER)
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001042 return;
1043
1044 wpa_printf(MSG_DEBUG, "Channel list updated - continue setup");
1045 eloop_cancel_timeout(channel_list_update_timeout, iface, NULL);
1046 setup_interface2(iface);
1047}
1048
1049
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001050static int setup_interface(struct hostapd_iface *iface)
1051{
1052 struct hostapd_data *hapd = iface->bss[0];
1053 size_t i;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001054
Dmitry Shmidta38abf92014-03-06 13:38:44 -08001055 /*
1056 * It is possible that setup_interface() is called after the interface
1057 * was disabled etc., in which case driver_ap_teardown is possibly set
1058 * to 1. Clear it here so any other key/station deletion, which is not
1059 * part of a teardown flow, would also call the relevant driver
1060 * callbacks.
1061 */
1062 iface->driver_ap_teardown = 0;
1063
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001064 if (!iface->phy[0]) {
1065 const char *phy = hostapd_drv_get_radio_name(hapd);
1066 if (phy) {
1067 wpa_printf(MSG_DEBUG, "phy: %s", phy);
1068 os_strlcpy(iface->phy, phy, sizeof(iface->phy));
1069 }
1070 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001071
1072 /*
1073 * Make sure that all BSSes get configured with a pointer to the same
1074 * driver interface.
1075 */
1076 for (i = 1; i < iface->num_bss; i++) {
1077 iface->bss[i]->driver = hapd->driver;
1078 iface->bss[i]->drv_priv = hapd->drv_priv;
1079 }
1080
1081 if (hostapd_validate_bssid_configuration(iface))
1082 return -1;
1083
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001084 /*
1085 * Initialize control interfaces early to allow external monitoring of
1086 * channel setup operations that may take considerable amount of time
1087 * especially for DFS cases.
1088 */
1089 if (start_ctrl_iface(iface))
1090 return -1;
1091
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001092 if (hapd->iconf->country[0] && hapd->iconf->country[1]) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001093 char country[4], previous_country[4];
1094
1095 hostapd_set_state(iface, HAPD_IFACE_COUNTRY_UPDATE);
1096 if (hostapd_get_country(hapd, previous_country) < 0)
1097 previous_country[0] = '\0';
1098
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001099 os_memcpy(country, hapd->iconf->country, 3);
1100 country[3] = '\0';
1101 if (hostapd_set_country(hapd, country) < 0) {
1102 wpa_printf(MSG_ERROR, "Failed to set country code");
1103 return -1;
1104 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001105
1106 wpa_printf(MSG_DEBUG, "Previous country code %s, new country code %s",
1107 previous_country, country);
1108
1109 if (os_strncmp(previous_country, country, 2) != 0) {
1110 wpa_printf(MSG_DEBUG, "Continue interface setup after channel list update");
1111 iface->wait_channel_update = 1;
Dmitry Shmidt97672262014-02-03 13:02:54 -08001112 eloop_register_timeout(5, 0,
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001113 channel_list_update_timeout,
1114 iface, NULL);
1115 return 0;
1116 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001117 }
1118
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001119 return setup_interface2(iface);
1120}
1121
1122
1123static int setup_interface2(struct hostapd_iface *iface)
1124{
1125 iface->wait_channel_update = 0;
1126
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001127 if (hostapd_get_hw_features(iface)) {
1128 /* Not all drivers support this yet, so continue without hw
1129 * feature data. */
1130 } else {
1131 int ret = hostapd_select_hw_mode(iface);
1132 if (ret < 0) {
1133 wpa_printf(MSG_ERROR, "Could not select hw_mode and "
1134 "channel. (%d)", ret);
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001135 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001136 }
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001137 if (ret == 1) {
1138 wpa_printf(MSG_DEBUG, "Interface initialization will be completed in a callback (ACS)");
1139 return 0;
1140 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001141 ret = hostapd_check_ht_capab(iface);
1142 if (ret < 0)
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001143 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001144 if (ret == 1) {
1145 wpa_printf(MSG_DEBUG, "Interface initialization will "
1146 "be completed in a callback");
1147 return 0;
1148 }
Dmitry Shmidt051af732013-10-22 13:52:46 -07001149
1150 if (iface->conf->ieee80211h)
1151 wpa_printf(MSG_DEBUG, "DFS support is enabled");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001152 }
1153 return hostapd_setup_interface_complete(iface, 0);
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001154
1155fail:
1156 hostapd_set_state(iface, HAPD_IFACE_DISABLED);
1157 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
1158 if (iface->interfaces && iface->interfaces->terminate_on_error)
1159 eloop_terminate();
1160 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001161}
1162
1163
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001164/**
1165 * hostapd_setup_interface_complete - Complete interface setup
1166 *
1167 * This function is called when previous steps in the interface setup has been
1168 * completed. This can also start operations, e.g., DFS, that will require
1169 * additional processing before interface is ready to be enabled. Such
1170 * operations will call this function from eloop callbacks when finished.
1171 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001172int hostapd_setup_interface_complete(struct hostapd_iface *iface, int err)
1173{
1174 struct hostapd_data *hapd = iface->bss[0];
1175 size_t j;
1176 u8 *prev_addr;
1177
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001178 if (err)
1179 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001180
1181 wpa_printf(MSG_DEBUG, "Completing interface initialization");
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001182 if (iface->conf->channel) {
Dmitry Shmidt051af732013-10-22 13:52:46 -07001183#ifdef NEED_AP_MLME
1184 int res;
1185#endif /* NEED_AP_MLME */
1186
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001187 iface->freq = hostapd_hw_get_freq(hapd, iface->conf->channel);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001188 wpa_printf(MSG_DEBUG, "Mode: %s Channel: %d "
1189 "Frequency: %d MHz",
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001190 hostapd_hw_mode_txt(iface->conf->hw_mode),
1191 iface->conf->channel, iface->freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001192
Dmitry Shmidt051af732013-10-22 13:52:46 -07001193#ifdef NEED_AP_MLME
1194 /* Check DFS */
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001195 res = hostapd_handle_dfs(iface);
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001196 if (res <= 0) {
1197 if (res < 0)
1198 goto fail;
Dmitry Shmidt051af732013-10-22 13:52:46 -07001199 return res;
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001200 }
Dmitry Shmidt051af732013-10-22 13:52:46 -07001201#endif /* NEED_AP_MLME */
1202
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001203 if (hostapd_set_freq(hapd, hapd->iconf->hw_mode, iface->freq,
1204 hapd->iconf->channel,
1205 hapd->iconf->ieee80211n,
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001206 hapd->iconf->ieee80211ac,
1207 hapd->iconf->secondary_channel,
1208 hapd->iconf->vht_oper_chwidth,
1209 hapd->iconf->vht_oper_centr_freq_seg0_idx,
1210 hapd->iconf->vht_oper_centr_freq_seg1_idx)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001211 wpa_printf(MSG_ERROR, "Could not set channel for "
1212 "kernel driver");
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001213 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001214 }
1215 }
1216
1217 if (iface->current_mode) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001218 if (hostapd_prepare_rates(iface, iface->current_mode)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001219 wpa_printf(MSG_ERROR, "Failed to prepare rates "
1220 "table.");
1221 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
1222 HOSTAPD_LEVEL_WARNING,
1223 "Failed to prepare rates table.");
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001224 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001225 }
1226 }
1227
1228 if (hapd->iconf->rts_threshold > -1 &&
1229 hostapd_set_rts(hapd, hapd->iconf->rts_threshold)) {
1230 wpa_printf(MSG_ERROR, "Could not set RTS threshold for "
1231 "kernel driver");
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001232 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001233 }
1234
1235 if (hapd->iconf->fragm_threshold > -1 &&
1236 hostapd_set_frag(hapd, hapd->iconf->fragm_threshold)) {
1237 wpa_printf(MSG_ERROR, "Could not set fragmentation threshold "
1238 "for kernel driver");
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001239 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001240 }
1241
1242 prev_addr = hapd->own_addr;
1243
1244 for (j = 0; j < iface->num_bss; j++) {
1245 hapd = iface->bss[j];
1246 if (j)
1247 os_memcpy(hapd->own_addr, prev_addr, ETH_ALEN);
Dmitry Shmidt71757432014-06-02 13:50:35 -07001248 if (hostapd_setup_bss(hapd, j == 0)) {
1249 do {
1250 hapd = iface->bss[j];
1251 hostapd_bss_deinit_no_free(hapd);
1252 hostapd_free_hapd_data(hapd);
1253 } while (j-- > 0);
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001254 goto fail;
Dmitry Shmidt71757432014-06-02 13:50:35 -07001255 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001256 if (hostapd_mac_comp_empty(hapd->conf->bssid) == 0)
1257 prev_addr = hapd->own_addr;
1258 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001259 hapd = iface->bss[0];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001260
1261 hostapd_tx_queue_params(iface);
1262
1263 ap_list_init(iface);
1264
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001265 hostapd_set_acl(hapd);
1266
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001267 if (hostapd_driver_commit(hapd) < 0) {
1268 wpa_printf(MSG_ERROR, "%s: Failed to commit driver "
1269 "configuration", __func__);
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001270 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001271 }
1272
Jouni Malinen87fd2792011-05-16 18:35:42 +03001273 /*
1274 * WPS UPnP module can be initialized only when the "upnp_iface" is up.
1275 * If "interface" and "upnp_iface" are the same (e.g., non-bridge
1276 * mode), the interface is up only after driver_commit, so initialize
1277 * WPS after driver_commit.
1278 */
1279 for (j = 0; j < iface->num_bss; j++) {
1280 if (hostapd_init_wps_complete(iface->bss[j]))
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001281 goto fail;
Jouni Malinen87fd2792011-05-16 18:35:42 +03001282 }
1283
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001284 hostapd_set_state(iface, HAPD_IFACE_ENABLED);
1285 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_ENABLED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001286 if (hapd->setup_complete_cb)
1287 hapd->setup_complete_cb(hapd->setup_complete_cb_ctx);
1288
1289 wpa_printf(MSG_DEBUG, "%s: Setup of interface done.",
1290 iface->bss[0]->conf->iface);
Dmitry Shmidtb96dad42013-11-05 10:07:29 -08001291 if (iface->interfaces && iface->interfaces->terminate_on_error > 0)
1292 iface->interfaces->terminate_on_error--;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001293
1294 return 0;
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001295
1296fail:
1297 wpa_printf(MSG_ERROR, "Interface initialization failed");
1298 hostapd_set_state(iface, HAPD_IFACE_DISABLED);
1299 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
1300 if (iface->interfaces && iface->interfaces->terminate_on_error)
1301 eloop_terminate();
1302 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001303}
1304
1305
1306/**
1307 * hostapd_setup_interface - Setup of an interface
1308 * @iface: Pointer to interface data.
1309 * Returns: 0 on success, -1 on failure
1310 *
1311 * Initializes the driver interface, validates the configuration,
1312 * and sets driver parameters based on the configuration.
1313 * Flushes old stations, sets the channel, encryption,
1314 * beacons, and WDS links based on the configuration.
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001315 *
1316 * If interface setup requires more time, e.g., to perform HT co-ex scans, ACS,
1317 * or DFS operations, this function returns 0 before such operations have been
1318 * completed. The pending operations are registered into eloop and will be
1319 * completed from eloop callbacks. Those callbacks end up calling
1320 * hostapd_setup_interface_complete() once setup has been completed.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001321 */
1322int hostapd_setup_interface(struct hostapd_iface *iface)
1323{
1324 int ret;
1325
1326 ret = setup_interface(iface);
1327 if (ret) {
1328 wpa_printf(MSG_ERROR, "%s: Unable to setup interface.",
1329 iface->bss[0]->conf->iface);
1330 return -1;
1331 }
1332
1333 return 0;
1334}
1335
1336
1337/**
1338 * hostapd_alloc_bss_data - Allocate and initialize per-BSS data
1339 * @hapd_iface: Pointer to interface data
1340 * @conf: Pointer to per-interface configuration
1341 * @bss: Pointer to per-BSS configuration for this BSS
1342 * Returns: Pointer to allocated BSS data
1343 *
1344 * This function is used to allocate per-BSS data structure. This data will be
1345 * freed after hostapd_cleanup() is called for it during interface
1346 * deinitialization.
1347 */
1348struct hostapd_data *
1349hostapd_alloc_bss_data(struct hostapd_iface *hapd_iface,
1350 struct hostapd_config *conf,
1351 struct hostapd_bss_config *bss)
1352{
1353 struct hostapd_data *hapd;
1354
1355 hapd = os_zalloc(sizeof(*hapd));
1356 if (hapd == NULL)
1357 return NULL;
1358
1359 hapd->new_assoc_sta_cb = hostapd_new_assoc_sta;
1360 hapd->iconf = conf;
1361 hapd->conf = bss;
1362 hapd->iface = hapd_iface;
1363 hapd->driver = hapd->iconf->driver;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001364 hapd->ctrl_sock = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001365
1366 return hapd;
1367}
1368
1369
Dmitry Shmidt54605472013-11-08 11:10:19 -08001370static void hostapd_bss_deinit(struct hostapd_data *hapd)
1371{
1372 wpa_printf(MSG_DEBUG, "%s: deinit bss %s", __func__,
1373 hapd->conf->iface);
Dmitry Shmidt71757432014-06-02 13:50:35 -07001374 hostapd_bss_deinit_no_free(hapd);
Dmitry Shmidt54605472013-11-08 11:10:19 -08001375 hostapd_cleanup(hapd);
1376}
1377
1378
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001379void hostapd_interface_deinit(struct hostapd_iface *iface)
1380{
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001381 int j;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001382
Dmitry Shmidt54605472013-11-08 11:10:19 -08001383 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001384 if (iface == NULL)
1385 return;
1386
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07001387#ifdef CONFIG_IEEE80211N
1388#ifdef NEED_AP_MLME
1389 hostapd_stop_setup_timers(iface);
1390 eloop_cancel_timeout(ap_ht2040_timeout, iface, NULL);
1391#endif /* NEED_AP_MLME */
1392#endif /* CONFIG_IEEE80211N */
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001393 eloop_cancel_timeout(channel_list_update_timeout, iface, NULL);
1394 iface->wait_channel_update = 0;
1395
Dmitry Shmidt54605472013-11-08 11:10:19 -08001396 for (j = iface->num_bss - 1; j >= 0; j--)
1397 hostapd_bss_deinit(iface->bss[j]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001398}
1399
1400
1401void hostapd_interface_free(struct hostapd_iface *iface)
1402{
1403 size_t j;
Dmitry Shmidt54605472013-11-08 11:10:19 -08001404 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
1405 for (j = 0; j < iface->num_bss; j++) {
1406 wpa_printf(MSG_DEBUG, "%s: free hapd %p",
1407 __func__, iface->bss[j]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001408 os_free(iface->bss[j]);
Dmitry Shmidt54605472013-11-08 11:10:19 -08001409 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001410 hostapd_cleanup_iface(iface);
1411}
1412
1413
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001414/**
1415 * hostapd_init - Allocate and initialize per-interface data
1416 * @config_file: Path to the configuration file
1417 * Returns: Pointer to the allocated interface data or %NULL on failure
1418 *
1419 * This function is used to allocate main data structures for per-interface
1420 * data. The allocated data buffer will be freed by calling
1421 * hostapd_cleanup_iface().
1422 */
1423struct hostapd_iface * hostapd_init(struct hapd_interfaces *interfaces,
1424 const char *config_file)
1425{
1426 struct hostapd_iface *hapd_iface = NULL;
1427 struct hostapd_config *conf = NULL;
1428 struct hostapd_data *hapd;
1429 size_t i;
1430
1431 hapd_iface = os_zalloc(sizeof(*hapd_iface));
1432 if (hapd_iface == NULL)
1433 goto fail;
1434
1435 hapd_iface->config_fname = os_strdup(config_file);
1436 if (hapd_iface->config_fname == NULL)
1437 goto fail;
1438
1439 conf = interfaces->config_read_cb(hapd_iface->config_fname);
1440 if (conf == NULL)
1441 goto fail;
1442 hapd_iface->conf = conf;
1443
1444 hapd_iface->num_bss = conf->num_bss;
1445 hapd_iface->bss = os_calloc(conf->num_bss,
1446 sizeof(struct hostapd_data *));
1447 if (hapd_iface->bss == NULL)
1448 goto fail;
1449
1450 for (i = 0; i < conf->num_bss; i++) {
1451 hapd = hapd_iface->bss[i] =
1452 hostapd_alloc_bss_data(hapd_iface, conf,
1453 conf->bss[i]);
1454 if (hapd == NULL)
1455 goto fail;
1456 hapd->msg_ctx = hapd;
1457 }
1458
1459 return hapd_iface;
1460
1461fail:
1462 wpa_printf(MSG_ERROR, "Failed to set up interface with %s",
1463 config_file);
1464 if (conf)
1465 hostapd_config_free(conf);
1466 if (hapd_iface) {
1467 os_free(hapd_iface->config_fname);
1468 os_free(hapd_iface->bss);
Dmitry Shmidt54605472013-11-08 11:10:19 -08001469 wpa_printf(MSG_DEBUG, "%s: free iface %p",
1470 __func__, hapd_iface);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001471 os_free(hapd_iface);
1472 }
1473 return NULL;
1474}
1475
1476
1477static int ifname_in_use(struct hapd_interfaces *interfaces, const char *ifname)
1478{
1479 size_t i, j;
1480
1481 for (i = 0; i < interfaces->count; i++) {
1482 struct hostapd_iface *iface = interfaces->iface[i];
1483 for (j = 0; j < iface->num_bss; j++) {
1484 struct hostapd_data *hapd = iface->bss[j];
1485 if (os_strcmp(ifname, hapd->conf->iface) == 0)
1486 return 1;
1487 }
1488 }
1489
1490 return 0;
1491}
1492
1493
1494/**
1495 * hostapd_interface_init_bss - Read configuration file and init BSS data
1496 *
1497 * This function is used to parse configuration file for a BSS. This BSS is
1498 * added to an existing interface sharing the same radio (if any) or a new
1499 * interface is created if this is the first interface on a radio. This
1500 * allocate memory for the BSS. No actual driver operations are started.
1501 *
1502 * This is similar to hostapd_interface_init(), but for a case where the
1503 * configuration is used to add a single BSS instead of all BSSes for a radio.
1504 */
1505struct hostapd_iface *
1506hostapd_interface_init_bss(struct hapd_interfaces *interfaces, const char *phy,
1507 const char *config_fname, int debug)
1508{
1509 struct hostapd_iface *new_iface = NULL, *iface = NULL;
1510 struct hostapd_data *hapd;
1511 int k;
1512 size_t i, bss_idx;
1513
1514 if (!phy || !*phy)
1515 return NULL;
1516
1517 for (i = 0; i < interfaces->count; i++) {
1518 if (os_strcmp(interfaces->iface[i]->phy, phy) == 0) {
1519 iface = interfaces->iface[i];
1520 break;
1521 }
1522 }
1523
1524 wpa_printf(MSG_INFO, "Configuration file: %s (phy %s)%s",
1525 config_fname, phy, iface ? "" : " --> new PHY");
1526 if (iface) {
1527 struct hostapd_config *conf;
1528 struct hostapd_bss_config **tmp_conf;
1529 struct hostapd_data **tmp_bss;
1530 struct hostapd_bss_config *bss;
1531 const char *ifname;
1532
1533 /* Add new BSS to existing iface */
1534 conf = interfaces->config_read_cb(config_fname);
1535 if (conf == NULL)
1536 return NULL;
1537 if (conf->num_bss > 1) {
1538 wpa_printf(MSG_ERROR, "Multiple BSSes specified in BSS-config");
1539 hostapd_config_free(conf);
1540 return NULL;
1541 }
1542
1543 ifname = conf->bss[0]->iface;
1544 if (ifname[0] != '\0' && ifname_in_use(interfaces, ifname)) {
1545 wpa_printf(MSG_ERROR,
1546 "Interface name %s already in use", ifname);
1547 hostapd_config_free(conf);
1548 return NULL;
1549 }
1550
1551 tmp_conf = os_realloc_array(
1552 iface->conf->bss, iface->conf->num_bss + 1,
1553 sizeof(struct hostapd_bss_config *));
1554 tmp_bss = os_realloc_array(iface->bss, iface->num_bss + 1,
1555 sizeof(struct hostapd_data *));
1556 if (tmp_bss)
1557 iface->bss = tmp_bss;
1558 if (tmp_conf) {
1559 iface->conf->bss = tmp_conf;
1560 iface->conf->last_bss = tmp_conf[0];
1561 }
1562 if (tmp_bss == NULL || tmp_conf == NULL) {
1563 hostapd_config_free(conf);
1564 return NULL;
1565 }
1566 bss = iface->conf->bss[iface->conf->num_bss] = conf->bss[0];
1567 iface->conf->num_bss++;
1568
1569 hapd = hostapd_alloc_bss_data(iface, iface->conf, bss);
1570 if (hapd == NULL) {
1571 iface->conf->num_bss--;
1572 hostapd_config_free(conf);
1573 return NULL;
1574 }
1575 iface->conf->last_bss = bss;
1576 iface->bss[iface->num_bss] = hapd;
1577 hapd->msg_ctx = hapd;
1578
1579 bss_idx = iface->num_bss++;
1580 conf->num_bss--;
1581 conf->bss[0] = NULL;
1582 hostapd_config_free(conf);
1583 } else {
1584 /* Add a new iface with the first BSS */
1585 new_iface = iface = hostapd_init(interfaces, config_fname);
1586 if (!iface)
1587 return NULL;
1588 os_strlcpy(iface->phy, phy, sizeof(iface->phy));
1589 iface->interfaces = interfaces;
1590 bss_idx = 0;
1591 }
1592
1593 for (k = 0; k < debug; k++) {
1594 if (iface->bss[bss_idx]->conf->logger_stdout_level > 0)
1595 iface->bss[bss_idx]->conf->logger_stdout_level--;
1596 }
1597
1598 if (iface->conf->bss[bss_idx]->iface[0] == '\0' &&
1599 !hostapd_drv_none(iface->bss[bss_idx])) {
1600 wpa_printf(MSG_ERROR, "Interface name not specified in %s",
1601 config_fname);
1602 if (new_iface)
1603 hostapd_interface_deinit_free(new_iface);
1604 return NULL;
1605 }
1606
1607 return iface;
1608}
1609
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001610
1611void hostapd_interface_deinit_free(struct hostapd_iface *iface)
1612{
1613 const struct wpa_driver_ops *driver;
1614 void *drv_priv;
Dmitry Shmidt54605472013-11-08 11:10:19 -08001615
1616 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001617 if (iface == NULL)
1618 return;
Dmitry Shmidt54605472013-11-08 11:10:19 -08001619 wpa_printf(MSG_DEBUG, "%s: num_bss=%u conf->num_bss=%u",
1620 __func__, (unsigned int) iface->num_bss,
1621 (unsigned int) iface->conf->num_bss);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001622 driver = iface->bss[0]->driver;
1623 drv_priv = iface->bss[0]->drv_priv;
1624 hostapd_interface_deinit(iface);
Dmitry Shmidt54605472013-11-08 11:10:19 -08001625 wpa_printf(MSG_DEBUG, "%s: driver=%p drv_priv=%p -> hapd_deinit",
1626 __func__, driver, drv_priv);
Dmitry Shmidt71757432014-06-02 13:50:35 -07001627 if (driver && driver->hapd_deinit && drv_priv) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001628 driver->hapd_deinit(drv_priv);
Dmitry Shmidt71757432014-06-02 13:50:35 -07001629 iface->bss[0]->drv_priv = NULL;
1630 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001631 hostapd_interface_free(iface);
1632}
1633
1634
Dmitry Shmidt15907092014-03-25 10:42:57 -07001635static void hostapd_deinit_driver(const struct wpa_driver_ops *driver,
1636 void *drv_priv,
1637 struct hostapd_iface *hapd_iface)
1638{
1639 size_t j;
1640
1641 wpa_printf(MSG_DEBUG, "%s: driver=%p drv_priv=%p -> hapd_deinit",
1642 __func__, driver, drv_priv);
1643 if (driver && driver->hapd_deinit && drv_priv) {
1644 driver->hapd_deinit(drv_priv);
1645 for (j = 0; j < hapd_iface->num_bss; j++) {
1646 wpa_printf(MSG_DEBUG, "%s:bss[%d]->drv_priv=%p",
1647 __func__, (int) j,
1648 hapd_iface->bss[j]->drv_priv);
1649 if (hapd_iface->bss[j]->drv_priv == drv_priv)
1650 hapd_iface->bss[j]->drv_priv = NULL;
1651 }
1652 }
1653}
1654
1655
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001656int hostapd_enable_iface(struct hostapd_iface *hapd_iface)
1657{
Dmitry Shmidt71757432014-06-02 13:50:35 -07001658 size_t j;
1659
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001660 if (hapd_iface->bss[0]->drv_priv != NULL) {
1661 wpa_printf(MSG_ERROR, "Interface %s already enabled",
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001662 hapd_iface->conf->bss[0]->iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001663 return -1;
1664 }
1665
1666 wpa_printf(MSG_DEBUG, "Enable interface %s",
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001667 hapd_iface->conf->bss[0]->iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001668
Dmitry Shmidt71757432014-06-02 13:50:35 -07001669 for (j = 0; j < hapd_iface->num_bss; j++)
1670 hostapd_set_security_params(hapd_iface->conf->bss[j], 1);
Dmitry Shmidt344abd32014-01-14 13:17:00 -08001671 if (hostapd_config_check(hapd_iface->conf, 1) < 0) {
1672 wpa_printf(MSG_INFO, "Invalid configuration - cannot enable");
1673 return -1;
1674 }
1675
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001676 if (hapd_iface->interfaces == NULL ||
1677 hapd_iface->interfaces->driver_init == NULL ||
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001678 hapd_iface->interfaces->driver_init(hapd_iface))
1679 return -1;
1680
1681 if (hostapd_setup_interface(hapd_iface)) {
Dmitry Shmidt15907092014-03-25 10:42:57 -07001682 hostapd_deinit_driver(hapd_iface->bss[0]->driver,
1683 hapd_iface->bss[0]->drv_priv,
1684 hapd_iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001685 return -1;
1686 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001687
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001688 return 0;
1689}
1690
1691
1692int hostapd_reload_iface(struct hostapd_iface *hapd_iface)
1693{
1694 size_t j;
1695
1696 wpa_printf(MSG_DEBUG, "Reload interface %s",
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001697 hapd_iface->conf->bss[0]->iface);
1698 for (j = 0; j < hapd_iface->num_bss; j++)
Dmitry Shmidt71757432014-06-02 13:50:35 -07001699 hostapd_set_security_params(hapd_iface->conf->bss[j], 1);
Dmitry Shmidt344abd32014-01-14 13:17:00 -08001700 if (hostapd_config_check(hapd_iface->conf, 1) < 0) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001701 wpa_printf(MSG_ERROR, "Updated configuration is invalid");
1702 return -1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001703 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001704 hostapd_clear_old(hapd_iface);
1705 for (j = 0; j < hapd_iface->num_bss; j++)
1706 hostapd_reload_bss(hapd_iface->bss[j]);
1707
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001708 return 0;
1709}
1710
1711
1712int hostapd_disable_iface(struct hostapd_iface *hapd_iface)
1713{
1714 size_t j;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001715 const struct wpa_driver_ops *driver;
1716 void *drv_priv;
1717
1718 if (hapd_iface == NULL)
1719 return -1;
Dmitry Shmidt71757432014-06-02 13:50:35 -07001720
1721 if (hapd_iface->bss[0]->drv_priv == NULL) {
1722 wpa_printf(MSG_INFO, "Interface %s already disabled",
1723 hapd_iface->conf->bss[0]->iface);
1724 return -1;
1725 }
1726
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001727 wpa_msg(hapd_iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001728 driver = hapd_iface->bss[0]->driver;
1729 drv_priv = hapd_iface->bss[0]->drv_priv;
1730
Dmitry Shmidta38abf92014-03-06 13:38:44 -08001731 hapd_iface->driver_ap_teardown =
1732 !!(hapd_iface->drv_flags &
1733 WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
1734
1735 /* same as hostapd_interface_deinit without deinitializing ctrl-iface */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001736 for (j = 0; j < hapd_iface->num_bss; j++) {
1737 struct hostapd_data *hapd = hapd_iface->bss[j];
Dmitry Shmidt71757432014-06-02 13:50:35 -07001738 hostapd_bss_deinit_no_free(hapd);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001739 hostapd_free_hapd_data(hapd);
1740 }
1741
Dmitry Shmidt15907092014-03-25 10:42:57 -07001742 hostapd_deinit_driver(driver, drv_priv, hapd_iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001743
1744 /* From hostapd_cleanup_iface: These were initialized in
1745 * hostapd_setup_interface and hostapd_setup_interface_complete
1746 */
1747 hostapd_cleanup_iface_partial(hapd_iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001748
Dmitry Shmidt56052862013-10-04 10:23:25 -07001749 wpa_printf(MSG_DEBUG, "Interface %s disabled",
1750 hapd_iface->bss[0]->conf->iface);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001751 hostapd_set_state(hapd_iface, HAPD_IFACE_DISABLED);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001752 return 0;
1753}
1754
1755
1756static struct hostapd_iface *
1757hostapd_iface_alloc(struct hapd_interfaces *interfaces)
1758{
1759 struct hostapd_iface **iface, *hapd_iface;
1760
1761 iface = os_realloc_array(interfaces->iface, interfaces->count + 1,
1762 sizeof(struct hostapd_iface *));
1763 if (iface == NULL)
1764 return NULL;
1765 interfaces->iface = iface;
1766 hapd_iface = interfaces->iface[interfaces->count] =
1767 os_zalloc(sizeof(*hapd_iface));
1768 if (hapd_iface == NULL) {
1769 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory for "
1770 "the interface", __func__);
1771 return NULL;
1772 }
1773 interfaces->count++;
1774 hapd_iface->interfaces = interfaces;
1775
1776 return hapd_iface;
1777}
1778
1779
1780static struct hostapd_config *
1781hostapd_config_alloc(struct hapd_interfaces *interfaces, const char *ifname,
1782 const char *ctrl_iface)
1783{
1784 struct hostapd_bss_config *bss;
1785 struct hostapd_config *conf;
1786
1787 /* Allocates memory for bss and conf */
1788 conf = hostapd_config_defaults();
1789 if (conf == NULL) {
1790 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory for "
1791 "configuration", __func__);
1792 return NULL;
1793 }
1794
1795 conf->driver = wpa_drivers[0];
1796 if (conf->driver == NULL) {
1797 wpa_printf(MSG_ERROR, "No driver wrappers registered!");
1798 hostapd_config_free(conf);
1799 return NULL;
1800 }
1801
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001802 bss = conf->last_bss = conf->bss[0];
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001803
1804 os_strlcpy(bss->iface, ifname, sizeof(bss->iface));
1805 bss->ctrl_interface = os_strdup(ctrl_iface);
1806 if (bss->ctrl_interface == NULL) {
1807 hostapd_config_free(conf);
1808 return NULL;
1809 }
1810
1811 /* Reading configuration file skipped, will be done in SET!
1812 * From reading the configuration till the end has to be done in
1813 * SET
1814 */
1815 return conf;
1816}
1817
1818
1819static struct hostapd_iface * hostapd_data_alloc(
1820 struct hapd_interfaces *interfaces, struct hostapd_config *conf)
1821{
1822 size_t i;
1823 struct hostapd_iface *hapd_iface =
1824 interfaces->iface[interfaces->count - 1];
1825 struct hostapd_data *hapd;
1826
1827 hapd_iface->conf = conf;
1828 hapd_iface->num_bss = conf->num_bss;
1829
1830 hapd_iface->bss = os_zalloc(conf->num_bss *
1831 sizeof(struct hostapd_data *));
1832 if (hapd_iface->bss == NULL)
1833 return NULL;
1834
1835 for (i = 0; i < conf->num_bss; i++) {
1836 hapd = hapd_iface->bss[i] =
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001837 hostapd_alloc_bss_data(hapd_iface, conf, conf->bss[i]);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001838 if (hapd == NULL)
1839 return NULL;
1840 hapd->msg_ctx = hapd;
1841 }
1842
1843 hapd_iface->interfaces = interfaces;
1844
1845 return hapd_iface;
1846}
1847
1848
1849int hostapd_add_iface(struct hapd_interfaces *interfaces, char *buf)
1850{
1851 struct hostapd_config *conf = NULL;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001852 struct hostapd_iface *hapd_iface = NULL, *new_iface = NULL;
1853 struct hostapd_data *hapd;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001854 char *ptr;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001855 size_t i, j;
1856 const char *conf_file = NULL, *phy_name = NULL;
1857
1858 if (os_strncmp(buf, "bss_config=", 11) == 0) {
1859 char *pos;
1860 phy_name = buf + 11;
1861 pos = os_strchr(phy_name, ':');
1862 if (!pos)
1863 return -1;
1864 *pos++ = '\0';
1865 conf_file = pos;
1866 if (!os_strlen(conf_file))
1867 return -1;
1868
1869 hapd_iface = hostapd_interface_init_bss(interfaces, phy_name,
1870 conf_file, 0);
1871 if (!hapd_iface)
1872 return -1;
1873 for (j = 0; j < interfaces->count; j++) {
1874 if (interfaces->iface[j] == hapd_iface)
1875 break;
1876 }
1877 if (j == interfaces->count) {
1878 struct hostapd_iface **tmp;
1879 tmp = os_realloc_array(interfaces->iface,
1880 interfaces->count + 1,
1881 sizeof(struct hostapd_iface *));
1882 if (!tmp) {
1883 hostapd_interface_deinit_free(hapd_iface);
1884 return -1;
1885 }
1886 interfaces->iface = tmp;
1887 interfaces->iface[interfaces->count++] = hapd_iface;
1888 new_iface = hapd_iface;
1889 }
1890
1891 if (new_iface) {
1892 if (interfaces->driver_init(hapd_iface) ||
1893 hostapd_setup_interface(hapd_iface)) {
1894 interfaces->count--;
1895 goto fail;
1896 }
1897 } else {
1898 /* Assign new BSS with bss[0]'s driver info */
1899 hapd = hapd_iface->bss[hapd_iface->num_bss - 1];
1900 hapd->driver = hapd_iface->bss[0]->driver;
1901 hapd->drv_priv = hapd_iface->bss[0]->drv_priv;
1902 os_memcpy(hapd->own_addr, hapd_iface->bss[0]->own_addr,
1903 ETH_ALEN);
1904
1905 if (start_ctrl_iface_bss(hapd) < 0 ||
Dmitry Shmidt54605472013-11-08 11:10:19 -08001906 (hapd_iface->state == HAPD_IFACE_ENABLED &&
1907 hostapd_setup_bss(hapd, -1))) {
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001908 hostapd_cleanup(hapd);
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08001909 hapd_iface->bss[hapd_iface->num_bss - 1] = NULL;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001910 hapd_iface->conf->num_bss--;
1911 hapd_iface->num_bss--;
Dmitry Shmidt54605472013-11-08 11:10:19 -08001912 wpa_printf(MSG_DEBUG, "%s: free hapd %p %s",
1913 __func__, hapd, hapd->conf->iface);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001914 os_free(hapd);
1915 return -1;
1916 }
1917 }
1918 return 0;
1919 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001920
1921 ptr = os_strchr(buf, ' ');
1922 if (ptr == NULL)
1923 return -1;
1924 *ptr++ = '\0';
1925
Dmitry Shmidt56052862013-10-04 10:23:25 -07001926 if (os_strncmp(ptr, "config=", 7) == 0)
1927 conf_file = ptr + 7;
1928
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001929 for (i = 0; i < interfaces->count; i++) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001930 if (!os_strcmp(interfaces->iface[i]->conf->bss[0]->iface,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001931 buf)) {
1932 wpa_printf(MSG_INFO, "Cannot add interface - it "
1933 "already exists");
1934 return -1;
1935 }
1936 }
1937
1938 hapd_iface = hostapd_iface_alloc(interfaces);
1939 if (hapd_iface == NULL) {
1940 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
1941 "for interface", __func__);
1942 goto fail;
1943 }
1944
Dmitry Shmidt56052862013-10-04 10:23:25 -07001945 if (conf_file && interfaces->config_read_cb) {
1946 conf = interfaces->config_read_cb(conf_file);
1947 if (conf && conf->bss)
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001948 os_strlcpy(conf->bss[0]->iface, buf,
1949 sizeof(conf->bss[0]->iface));
Dmitry Shmidt56052862013-10-04 10:23:25 -07001950 } else
1951 conf = hostapd_config_alloc(interfaces, buf, ptr);
1952 if (conf == NULL || conf->bss == NULL) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001953 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
1954 "for configuration", __func__);
1955 goto fail;
1956 }
1957
1958 hapd_iface = hostapd_data_alloc(interfaces, conf);
1959 if (hapd_iface == NULL) {
1960 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
1961 "for hostapd", __func__);
1962 goto fail;
1963 }
1964
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001965 if (start_ctrl_iface(hapd_iface) < 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001966 goto fail;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001967
1968 wpa_printf(MSG_INFO, "Add interface '%s'", conf->bss[0]->iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001969
1970 return 0;
1971
1972fail:
1973 if (conf)
1974 hostapd_config_free(conf);
1975 if (hapd_iface) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001976 if (hapd_iface->bss) {
Dmitry Shmidt54605472013-11-08 11:10:19 -08001977 for (i = 0; i < hapd_iface->num_bss; i++) {
1978 hapd = hapd_iface->bss[i];
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08001979 if (!hapd)
1980 continue;
1981 if (hapd_iface->interfaces &&
Dmitry Shmidt54605472013-11-08 11:10:19 -08001982 hapd_iface->interfaces->ctrl_iface_deinit)
1983 hapd_iface->interfaces->
1984 ctrl_iface_deinit(hapd);
1985 wpa_printf(MSG_DEBUG, "%s: free hapd %p (%s)",
1986 __func__, hapd_iface->bss[i],
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08001987 hapd->conf->iface);
1988 os_free(hapd);
1989 hapd_iface->bss[i] = NULL;
Dmitry Shmidt54605472013-11-08 11:10:19 -08001990 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001991 os_free(hapd_iface->bss);
1992 }
Dmitry Shmidt54605472013-11-08 11:10:19 -08001993 wpa_printf(MSG_DEBUG, "%s: free iface %p",
1994 __func__, hapd_iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001995 os_free(hapd_iface);
1996 }
1997 return -1;
1998}
1999
2000
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002001static int hostapd_remove_bss(struct hostapd_iface *iface, unsigned int idx)
2002{
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002003 size_t i;
2004
Dmitry Shmidt54605472013-11-08 11:10:19 -08002005 wpa_printf(MSG_INFO, "Remove BSS '%s'", iface->conf->bss[idx]->iface);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002006
Dmitry Shmidt54605472013-11-08 11:10:19 -08002007 /* Remove hostapd_data only if it has already been initialized */
2008 if (idx < iface->num_bss) {
2009 struct hostapd_data *hapd = iface->bss[idx];
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002010
Dmitry Shmidt54605472013-11-08 11:10:19 -08002011 hostapd_bss_deinit(hapd);
2012 wpa_printf(MSG_DEBUG, "%s: free hapd %p (%s)",
2013 __func__, hapd, hapd->conf->iface);
2014 hostapd_config_free_bss(hapd->conf);
2015 os_free(hapd);
2016
2017 iface->num_bss--;
2018
2019 for (i = idx; i < iface->num_bss; i++)
2020 iface->bss[i] = iface->bss[i + 1];
2021 } else {
2022 hostapd_config_free_bss(iface->conf->bss[idx]);
2023 iface->conf->bss[idx] = NULL;
2024 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002025
2026 iface->conf->num_bss--;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002027 for (i = idx; i < iface->conf->num_bss; i++)
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002028 iface->conf->bss[i] = iface->conf->bss[i + 1];
2029
2030 return 0;
2031}
2032
2033
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002034int hostapd_remove_iface(struct hapd_interfaces *interfaces, char *buf)
2035{
2036 struct hostapd_iface *hapd_iface;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002037 size_t i, j, k = 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002038
2039 for (i = 0; i < interfaces->count; i++) {
2040 hapd_iface = interfaces->iface[i];
2041 if (hapd_iface == NULL)
2042 return -1;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002043 if (!os_strcmp(hapd_iface->conf->bss[0]->iface, buf)) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002044 wpa_printf(MSG_INFO, "Remove interface '%s'", buf);
Dmitry Shmidta38abf92014-03-06 13:38:44 -08002045 hapd_iface->driver_ap_teardown =
2046 !!(hapd_iface->drv_flags &
2047 WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
2048
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002049 hostapd_interface_deinit_free(hapd_iface);
2050 k = i;
2051 while (k < (interfaces->count - 1)) {
2052 interfaces->iface[k] =
2053 interfaces->iface[k + 1];
2054 k++;
2055 }
2056 interfaces->count--;
2057 return 0;
2058 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002059
2060 for (j = 0; j < hapd_iface->conf->num_bss; j++) {
Dmitry Shmidta38abf92014-03-06 13:38:44 -08002061 if (!os_strcmp(hapd_iface->conf->bss[j]->iface, buf)) {
2062 hapd_iface->driver_ap_teardown =
2063 !(hapd_iface->drv_flags &
2064 WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002065 return hostapd_remove_bss(hapd_iface, j);
Dmitry Shmidta38abf92014-03-06 13:38:44 -08002066 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002067 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002068 }
2069 return -1;
2070}
2071
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002072
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002073/**
2074 * hostapd_new_assoc_sta - Notify that a new station associated with the AP
2075 * @hapd: Pointer to BSS data
2076 * @sta: Pointer to the associated STA data
2077 * @reassoc: 1 to indicate this was a re-association; 0 = first association
2078 *
2079 * This function will be called whenever a station associates with the AP. It
2080 * can be called from ieee802_11.c for drivers that export MLME to hostapd and
2081 * from drv_callbacks.c based on driver events for drivers that take care of
2082 * management frames (IEEE 802.11 authentication and association) internally.
2083 */
2084void hostapd_new_assoc_sta(struct hostapd_data *hapd, struct sta_info *sta,
2085 int reassoc)
2086{
2087 if (hapd->tkip_countermeasures) {
2088 hostapd_drv_sta_deauth(hapd, sta->addr,
2089 WLAN_REASON_MICHAEL_MIC_FAILURE);
2090 return;
2091 }
2092
2093 hostapd_prune_associations(hapd, sta->addr);
2094
2095 /* IEEE 802.11F (IAPP) */
2096 if (hapd->conf->ieee802_11f)
2097 iapp_new_station(hapd->iapp, sta);
2098
2099#ifdef CONFIG_P2P
2100 if (sta->p2p_ie == NULL && !sta->no_p2p_set) {
2101 sta->no_p2p_set = 1;
2102 hapd->num_sta_no_p2p++;
2103 if (hapd->num_sta_no_p2p == 1)
2104 hostapd_p2p_non_p2p_sta_connected(hapd);
2105 }
2106#endif /* CONFIG_P2P */
2107
2108 /* Start accounting here, if IEEE 802.1X and WPA are not used.
2109 * IEEE 802.1X/WPA code will start accounting after the station has
2110 * been authorized. */
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -08002111 if (!hapd->conf->ieee802_1x && !hapd->conf->wpa && !hapd->conf->osen) {
2112 ap_sta_set_authorized(hapd, sta, 1);
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002113 os_get_reltime(&sta->connected_time);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002114 accounting_sta_start(hapd, sta);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002115 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002116
2117 /* Start IEEE 802.1X authentication process for new stations */
2118 ieee802_1x_new_station(hapd, sta);
2119 if (reassoc) {
2120 if (sta->auth_alg != WLAN_AUTH_FT &&
2121 !(sta->flags & (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS)))
2122 wpa_auth_sm_event(sta->wpa_sm, WPA_REAUTH);
2123 } else
2124 wpa_auth_sta_associated(hapd->wpa_auth, sta->wpa_sm);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002125
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08002126 if (!(hapd->iface->drv_flags & WPA_DRIVER_FLAGS_INACTIVITY_TIMER)) {
2127 wpa_printf(MSG_DEBUG, "%s: reschedule ap_handle_timer timeout "
2128 "for " MACSTR " (%d seconds - ap_max_inactivity)",
2129 __func__, MAC2STR(sta->addr),
2130 hapd->conf->ap_max_inactivity);
2131 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
2132 eloop_register_timeout(hapd->conf->ap_max_inactivity, 0,
2133 ap_handle_timer, hapd, sta);
2134 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002135}
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002136
2137
2138const char * hostapd_state_text(enum hostapd_iface_state s)
2139{
2140 switch (s) {
2141 case HAPD_IFACE_UNINITIALIZED:
2142 return "UNINITIALIZED";
2143 case HAPD_IFACE_DISABLED:
2144 return "DISABLED";
2145 case HAPD_IFACE_COUNTRY_UPDATE:
2146 return "COUNTRY_UPDATE";
2147 case HAPD_IFACE_ACS:
2148 return "ACS";
2149 case HAPD_IFACE_HT_SCAN:
2150 return "HT_SCAN";
2151 case HAPD_IFACE_DFS:
2152 return "DFS";
2153 case HAPD_IFACE_ENABLED:
2154 return "ENABLED";
2155 }
2156
2157 return "UNKNOWN";
2158}
2159
2160
2161void hostapd_set_state(struct hostapd_iface *iface, enum hostapd_iface_state s)
2162{
2163 wpa_printf(MSG_INFO, "%s: interface state %s->%s",
2164 iface->conf->bss[0]->iface, hostapd_state_text(iface->state),
2165 hostapd_state_text(s));
2166 iface->state = s;
2167}
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002168
2169
2170#ifdef NEED_AP_MLME
2171
2172static void free_beacon_data(struct beacon_data *beacon)
2173{
2174 os_free(beacon->head);
2175 beacon->head = NULL;
2176 os_free(beacon->tail);
2177 beacon->tail = NULL;
2178 os_free(beacon->probe_resp);
2179 beacon->probe_resp = NULL;
2180 os_free(beacon->beacon_ies);
2181 beacon->beacon_ies = NULL;
2182 os_free(beacon->proberesp_ies);
2183 beacon->proberesp_ies = NULL;
2184 os_free(beacon->assocresp_ies);
2185 beacon->assocresp_ies = NULL;
2186}
2187
2188
2189static int hostapd_build_beacon_data(struct hostapd_iface *iface,
2190 struct beacon_data *beacon)
2191{
2192 struct wpabuf *beacon_extra, *proberesp_extra, *assocresp_extra;
2193 struct wpa_driver_ap_params params;
2194 int ret;
2195 struct hostapd_data *hapd = iface->bss[0];
2196
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08002197 os_memset(beacon, 0, sizeof(*beacon));
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002198 ret = ieee802_11_build_ap_params(hapd, &params);
2199 if (ret < 0)
2200 return ret;
2201
2202 ret = hostapd_build_ap_extra_ies(hapd, &beacon_extra,
2203 &proberesp_extra,
2204 &assocresp_extra);
2205 if (ret)
2206 goto free_ap_params;
2207
2208 ret = -1;
2209 beacon->head = os_malloc(params.head_len);
2210 if (!beacon->head)
2211 goto free_ap_extra_ies;
2212
2213 os_memcpy(beacon->head, params.head, params.head_len);
2214 beacon->head_len = params.head_len;
2215
2216 beacon->tail = os_malloc(params.tail_len);
2217 if (!beacon->tail)
2218 goto free_beacon;
2219
2220 os_memcpy(beacon->tail, params.tail, params.tail_len);
2221 beacon->tail_len = params.tail_len;
2222
2223 if (params.proberesp != NULL) {
2224 beacon->probe_resp = os_malloc(params.proberesp_len);
2225 if (!beacon->probe_resp)
2226 goto free_beacon;
2227
2228 os_memcpy(beacon->probe_resp, params.proberesp,
2229 params.proberesp_len);
2230 beacon->probe_resp_len = params.proberesp_len;
2231 }
2232
2233 /* copy the extra ies */
2234 if (beacon_extra) {
2235 beacon->beacon_ies = os_malloc(wpabuf_len(beacon_extra));
2236 if (!beacon->beacon_ies)
2237 goto free_beacon;
2238
2239 os_memcpy(beacon->beacon_ies,
2240 beacon_extra->buf, wpabuf_len(beacon_extra));
2241 beacon->beacon_ies_len = wpabuf_len(beacon_extra);
2242 }
2243
2244 if (proberesp_extra) {
2245 beacon->proberesp_ies =
2246 os_malloc(wpabuf_len(proberesp_extra));
2247 if (!beacon->proberesp_ies)
2248 goto free_beacon;
2249
2250 os_memcpy(beacon->proberesp_ies, proberesp_extra->buf,
2251 wpabuf_len(proberesp_extra));
2252 beacon->proberesp_ies_len = wpabuf_len(proberesp_extra);
2253 }
2254
2255 if (assocresp_extra) {
2256 beacon->assocresp_ies =
2257 os_malloc(wpabuf_len(assocresp_extra));
2258 if (!beacon->assocresp_ies)
2259 goto free_beacon;
2260
2261 os_memcpy(beacon->assocresp_ies, assocresp_extra->buf,
2262 wpabuf_len(assocresp_extra));
2263 beacon->assocresp_ies_len = wpabuf_len(assocresp_extra);
2264 }
2265
2266 ret = 0;
2267free_beacon:
2268 /* if the function fails, the caller should not free beacon data */
2269 if (ret)
2270 free_beacon_data(beacon);
2271
2272free_ap_extra_ies:
2273 hostapd_free_ap_extra_ies(hapd, beacon_extra, proberesp_extra,
2274 assocresp_extra);
2275free_ap_params:
2276 ieee802_11_free_ap_params(&params);
2277 return ret;
2278}
2279
2280
2281/*
2282 * TODO: This flow currently supports only changing frequency within the
2283 * same hw_mode. Any other changes to MAC parameters or provided settings (even
2284 * width) are not supported.
2285 */
2286static int hostapd_change_config_freq(struct hostapd_data *hapd,
2287 struct hostapd_config *conf,
2288 struct hostapd_freq_params *params,
2289 struct hostapd_freq_params *old_params)
2290{
2291 int channel;
2292
2293 if (!params->channel) {
2294 /* check if the new channel is supported by hw */
2295 channel = hostapd_hw_get_channel(hapd, params->freq);
2296 if (!channel)
2297 return -1;
2298 } else {
2299 channel = params->channel;
2300 }
2301
2302 /* if a pointer to old_params is provided we save previous state */
2303 if (old_params) {
2304 old_params->channel = conf->channel;
2305 old_params->ht_enabled = conf->ieee80211n;
2306 old_params->sec_channel_offset = conf->secondary_channel;
2307 }
2308
2309 conf->channel = channel;
2310 conf->ieee80211n = params->ht_enabled;
2311 conf->secondary_channel = params->sec_channel_offset;
2312
2313 /* TODO: maybe call here hostapd_config_check here? */
2314
2315 return 0;
2316}
2317
2318
2319static int hostapd_fill_csa_settings(struct hostapd_iface *iface,
2320 struct csa_settings *settings)
2321{
2322 struct hostapd_freq_params old_freq;
2323 int ret;
2324
2325 os_memset(&old_freq, 0, sizeof(old_freq));
2326 if (!iface || !iface->freq || iface->csa_in_progress)
2327 return -1;
2328
2329 ret = hostapd_change_config_freq(iface->bss[0], iface->conf,
2330 &settings->freq_params,
2331 &old_freq);
2332 if (ret)
2333 return ret;
2334
2335 ret = hostapd_build_beacon_data(iface, &settings->beacon_after);
2336
2337 /* change back the configuration */
2338 hostapd_change_config_freq(iface->bss[0], iface->conf,
2339 &old_freq, NULL);
2340
2341 if (ret)
2342 return ret;
2343
2344 /* set channel switch parameters for csa ie */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002345 iface->cs_freq_params = settings->freq_params;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002346 iface->cs_count = settings->cs_count;
2347 iface->cs_block_tx = settings->block_tx;
2348
2349 ret = hostapd_build_beacon_data(iface, &settings->beacon_csa);
2350 if (ret) {
2351 free_beacon_data(&settings->beacon_after);
2352 return ret;
2353 }
2354
2355 settings->counter_offset_beacon = iface->cs_c_off_beacon;
2356 settings->counter_offset_presp = iface->cs_c_off_proberesp;
2357
2358 return 0;
2359}
2360
2361
2362void hostapd_cleanup_cs_params(struct hostapd_data *hapd)
2363{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002364 os_memset(&hapd->iface->cs_freq_params, 0,
2365 sizeof(hapd->iface->cs_freq_params));
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002366 hapd->iface->cs_count = 0;
2367 hapd->iface->cs_block_tx = 0;
2368 hapd->iface->cs_c_off_beacon = 0;
2369 hapd->iface->cs_c_off_proberesp = 0;
2370 hapd->iface->csa_in_progress = 0;
2371}
2372
2373
2374int hostapd_switch_channel(struct hostapd_data *hapd,
2375 struct csa_settings *settings)
2376{
2377 int ret;
2378 ret = hostapd_fill_csa_settings(hapd->iface, settings);
2379 if (ret)
2380 return ret;
2381
2382 ret = hostapd_drv_switch_channel(hapd, settings);
2383 free_beacon_data(&settings->beacon_csa);
2384 free_beacon_data(&settings->beacon_after);
2385
2386 if (ret) {
2387 /* if we failed, clean cs parameters */
2388 hostapd_cleanup_cs_params(hapd);
2389 return ret;
2390 }
2391
2392 hapd->iface->csa_in_progress = 1;
2393 return 0;
2394}
2395
2396#endif /* NEED_AP_MLME */