blob: 60224ccbfd2d16570b92f7c3691cf9dd04895040 [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * hostapd / Initialization and configuration
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003 * Copyright (c) 2002-2013, 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 Shmidt8d520ff2011-05-09 14:06:53 -070017#include "drivers/driver.h"
18#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 Shmidt8d520ff2011-05-09 14:06:53 -070037
38
Dmitry Shmidt04949592012-07-19 12:16:46 -070039static int hostapd_flush_old_stations(struct hostapd_data *hapd, u16 reason);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070040static int hostapd_setup_encryption(char *iface, struct hostapd_data *hapd);
Dmitry Shmidtc55524a2011-07-07 11:18:38 -070041static int hostapd_broadcast_wep_clear(struct hostapd_data *hapd);
Dmitry Shmidtcce06662013-11-04 18:44:24 -080042static int setup_interface2(struct hostapd_iface *iface);
43static void channel_list_update_timeout(void *eloop_ctx, void *timeout_ctx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070044
45extern int wpa_debug_level;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070046extern struct wpa_driver_ops *wpa_drivers[];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070047
48
Dmitry Shmidt04949592012-07-19 12:16:46 -070049int hostapd_for_each_interface(struct hapd_interfaces *interfaces,
50 int (*cb)(struct hostapd_iface *iface,
51 void *ctx), void *ctx)
52{
53 size_t i;
54 int ret;
55
56 for (i = 0; i < interfaces->count; i++) {
57 ret = cb(interfaces->iface[i], ctx);
58 if (ret)
59 return ret;
60 }
61
62 return 0;
63}
64
65
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070066static void hostapd_reload_bss(struct hostapd_data *hapd)
67{
Dmitry Shmidtcce06662013-11-04 18:44:24 -080068 struct hostapd_ssid *ssid;
69
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070070#ifndef CONFIG_NO_RADIUS
71 radius_client_reconfig(hapd->radius, hapd->conf->radius);
72#endif /* CONFIG_NO_RADIUS */
73
Dmitry Shmidtcce06662013-11-04 18:44:24 -080074 ssid = &hapd->conf->ssid;
75 if (!ssid->wpa_psk_set && ssid->wpa_psk && !ssid->wpa_psk->next &&
76 ssid->wpa_passphrase_set && ssid->wpa_passphrase) {
77 /*
78 * Force PSK to be derived again since SSID or passphrase may
79 * have changed.
80 */
81 os_free(ssid->wpa_psk);
82 ssid->wpa_psk = NULL;
83 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070084 if (hostapd_setup_wpa_psk(hapd->conf)) {
85 wpa_printf(MSG_ERROR, "Failed to re-configure WPA PSK "
86 "after reloading configuration");
87 }
88
89 if (hapd->conf->ieee802_1x || hapd->conf->wpa)
90 hostapd_set_drv_ieee8021x(hapd, hapd->conf->iface, 1);
91 else
92 hostapd_set_drv_ieee8021x(hapd, hapd->conf->iface, 0);
93
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080094 if (hapd->conf->wpa && hapd->wpa_auth == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070095 hostapd_setup_wpa(hapd);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080096 if (hapd->wpa_auth)
97 wpa_init_keys(hapd->wpa_auth);
98 } else if (hapd->conf->wpa) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070099 const u8 *wpa_ie;
100 size_t wpa_ie_len;
101 hostapd_reconfig_wpa(hapd);
102 wpa_ie = wpa_auth_get_wpa_ie(hapd->wpa_auth, &wpa_ie_len);
103 if (hostapd_set_generic_elem(hapd, wpa_ie, wpa_ie_len))
104 wpa_printf(MSG_ERROR, "Failed to configure WPA IE for "
105 "the kernel driver.");
106 } else if (hapd->wpa_auth) {
107 wpa_deinit(hapd->wpa_auth);
108 hapd->wpa_auth = NULL;
109 hostapd_set_privacy(hapd, 0);
110 hostapd_setup_encryption(hapd->conf->iface, hapd);
111 hostapd_set_generic_elem(hapd, (u8 *) "", 0);
112 }
113
114 ieee802_11_set_beacon(hapd);
115 hostapd_update_wps(hapd);
116
117 if (hapd->conf->ssid.ssid_set &&
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700118 hostapd_set_ssid(hapd, hapd->conf->ssid.ssid,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700119 hapd->conf->ssid.ssid_len)) {
120 wpa_printf(MSG_ERROR, "Could not set SSID for kernel driver");
121 /* try to continue */
122 }
123 wpa_printf(MSG_DEBUG, "Reconfigured interface %s", hapd->conf->iface);
124}
125
126
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700127static void hostapd_clear_old(struct hostapd_iface *iface)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700128{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700129 size_t j;
130
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700131 /*
132 * Deauthenticate all stations since the new configuration may not
133 * allow them to use the BSS anymore.
134 */
135 for (j = 0; j < iface->num_bss; j++) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700136 hostapd_flush_old_stations(iface->bss[j],
137 WLAN_REASON_PREV_AUTH_NOT_VALID);
Dmitry Shmidtc55524a2011-07-07 11:18:38 -0700138 hostapd_broadcast_wep_clear(iface->bss[j]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700139
140#ifndef CONFIG_NO_RADIUS
141 /* TODO: update dynamic data based on changed configuration
142 * items (e.g., open/close sockets, etc.) */
143 radius_client_flush(iface->bss[j]->radius, 0);
144#endif /* CONFIG_NO_RADIUS */
145 }
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700146}
147
148
149int hostapd_reload_config(struct hostapd_iface *iface)
150{
151 struct hostapd_data *hapd = iface->bss[0];
152 struct hostapd_config *newconf, *oldconf;
153 size_t j;
154
155 if (iface->config_fname == NULL) {
156 /* Only in-memory config in use - assume it has been updated */
157 hostapd_clear_old(iface);
158 for (j = 0; j < iface->num_bss; j++)
159 hostapd_reload_bss(iface->bss[j]);
160 return 0;
161 }
162
163 if (iface->interfaces == NULL ||
164 iface->interfaces->config_read_cb == NULL)
165 return -1;
166 newconf = iface->interfaces->config_read_cb(iface->config_fname);
167 if (newconf == NULL)
168 return -1;
169
170 hostapd_clear_old(iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700171
172 oldconf = hapd->iconf;
173 iface->conf = newconf;
174
175 for (j = 0; j < iface->num_bss; j++) {
176 hapd = iface->bss[j];
177 hapd->iconf = newconf;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800178 hapd->conf = newconf->bss[j];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700179 hostapd_reload_bss(hapd);
180 }
181
182 hostapd_config_free(oldconf);
183
184
185 return 0;
186}
187
188
189static void hostapd_broadcast_key_clear_iface(struct hostapd_data *hapd,
190 char *ifname)
191{
192 int i;
193
194 for (i = 0; i < NUM_WEP_KEYS; i++) {
195 if (hostapd_drv_set_key(ifname, hapd, WPA_ALG_NONE, NULL, i,
196 0, NULL, 0, NULL, 0)) {
197 wpa_printf(MSG_DEBUG, "Failed to clear default "
198 "encryption keys (ifname=%s keyidx=%d)",
199 ifname, i);
200 }
201 }
202#ifdef CONFIG_IEEE80211W
203 if (hapd->conf->ieee80211w) {
204 for (i = NUM_WEP_KEYS; i < NUM_WEP_KEYS + 2; i++) {
205 if (hostapd_drv_set_key(ifname, hapd, WPA_ALG_NONE,
206 NULL, i, 0, NULL,
207 0, NULL, 0)) {
208 wpa_printf(MSG_DEBUG, "Failed to clear "
209 "default mgmt encryption keys "
210 "(ifname=%s keyidx=%d)", ifname, i);
211 }
212 }
213 }
214#endif /* CONFIG_IEEE80211W */
215}
216
217
218static int hostapd_broadcast_wep_clear(struct hostapd_data *hapd)
219{
220 hostapd_broadcast_key_clear_iface(hapd, hapd->conf->iface);
221 return 0;
222}
223
224
225static int hostapd_broadcast_wep_set(struct hostapd_data *hapd)
226{
227 int errors = 0, idx;
228 struct hostapd_ssid *ssid = &hapd->conf->ssid;
229
230 idx = ssid->wep.idx;
231 if (ssid->wep.default_len &&
232 hostapd_drv_set_key(hapd->conf->iface,
233 hapd, WPA_ALG_WEP, broadcast_ether_addr, idx,
234 1, NULL, 0, ssid->wep.key[idx],
235 ssid->wep.len[idx])) {
236 wpa_printf(MSG_WARNING, "Could not set WEP encryption.");
237 errors++;
238 }
239
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700240 return errors;
241}
242
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700243
Dmitry Shmidt04949592012-07-19 12:16:46 -0700244static void hostapd_free_hapd_data(struct hostapd_data *hapd)
245{
Dmitry Shmidt54605472013-11-08 11:10:19 -0800246 wpa_printf(MSG_DEBUG, "%s(%s)", __func__, hapd->conf->iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700247 iapp_deinit(hapd->iapp);
248 hapd->iapp = NULL;
249 accounting_deinit(hapd);
250 hostapd_deinit_wpa(hapd);
251 vlan_deinit(hapd);
252 hostapd_acl_deinit(hapd);
253#ifndef CONFIG_NO_RADIUS
254 radius_client_deinit(hapd->radius);
255 hapd->radius = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700256 radius_das_deinit(hapd->radius_das);
257 hapd->radius_das = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700258#endif /* CONFIG_NO_RADIUS */
259
260 hostapd_deinit_wps(hapd);
261
262 authsrv_deinit(hapd);
263
Dmitry Shmidt54605472013-11-08 11:10:19 -0800264 if (hapd->interface_added &&
265 hostapd_if_remove(hapd, WPA_IF_AP_BSS, hapd->conf->iface)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700266 wpa_printf(MSG_WARNING, "Failed to remove BSS interface %s",
267 hapd->conf->iface);
268 }
269
270 os_free(hapd->probereq_cb);
271 hapd->probereq_cb = NULL;
272
273#ifdef CONFIG_P2P
274 wpabuf_free(hapd->p2p_beacon_ie);
275 hapd->p2p_beacon_ie = NULL;
276 wpabuf_free(hapd->p2p_probe_resp_ie);
277 hapd->p2p_probe_resp_ie = NULL;
278#endif /* CONFIG_P2P */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800279
280 wpabuf_free(hapd->time_adv);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700281
282#ifdef CONFIG_INTERWORKING
283 gas_serv_deinit(hapd);
284#endif /* CONFIG_INTERWORKING */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800285
286#ifdef CONFIG_SQLITE
287 os_free(hapd->tmp_eap_user.identity);
288 os_free(hapd->tmp_eap_user.password);
289#endif /* CONFIG_SQLITE */
Dmitry Shmidt04949592012-07-19 12:16:46 -0700290}
291
292
293/**
294 * hostapd_cleanup - Per-BSS cleanup (deinitialization)
295 * @hapd: Pointer to BSS data
296 *
297 * This function is used to free all per-BSS data structures and resources.
Dmitry Shmidt54605472013-11-08 11:10:19 -0800298 * Most of the modules that are initialized in hostapd_setup_bss() are
299 * deinitialized here.
Dmitry Shmidt04949592012-07-19 12:16:46 -0700300 */
301static void hostapd_cleanup(struct hostapd_data *hapd)
302{
Dmitry Shmidt54605472013-11-08 11:10:19 -0800303 wpa_printf(MSG_DEBUG, "%s(hapd=%p (%s))", __func__, hapd,
304 hapd->conf->iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700305 if (hapd->iface->interfaces &&
306 hapd->iface->interfaces->ctrl_iface_deinit)
307 hapd->iface->interfaces->ctrl_iface_deinit(hapd);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700308 hostapd_free_hapd_data(hapd);
Dmitry Shmidt54605472013-11-08 11:10:19 -0800309 hapd->started = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700310}
311
312
Dmitry Shmidt04949592012-07-19 12:16:46 -0700313static void hostapd_cleanup_iface_partial(struct hostapd_iface *iface)
314{
Dmitry Shmidt54605472013-11-08 11:10:19 -0800315 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700316 hostapd_free_hw_features(iface->hw_features, iface->num_hw_features);
317 iface->hw_features = NULL;
318 os_free(iface->current_rates);
319 iface->current_rates = NULL;
320 os_free(iface->basic_rates);
321 iface->basic_rates = NULL;
322 ap_list_deinit(iface);
323}
324
325
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700326/**
327 * hostapd_cleanup_iface - Complete per-interface cleanup
328 * @iface: Pointer to interface data
329 *
330 * This function is called after per-BSS data structures are deinitialized
331 * with hostapd_cleanup().
332 */
333static void hostapd_cleanup_iface(struct hostapd_iface *iface)
334{
Dmitry Shmidt54605472013-11-08 11:10:19 -0800335 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800336 eloop_cancel_timeout(channel_list_update_timeout, iface, NULL);
337
Dmitry Shmidt04949592012-07-19 12:16:46 -0700338 hostapd_cleanup_iface_partial(iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700339 hostapd_config_free(iface->conf);
340 iface->conf = NULL;
341
342 os_free(iface->config_fname);
343 os_free(iface->bss);
Dmitry Shmidt54605472013-11-08 11:10:19 -0800344 wpa_printf(MSG_DEBUG, "%s: free iface=%p", __func__, iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700345 os_free(iface);
346}
347
348
Dmitry Shmidt04949592012-07-19 12:16:46 -0700349static void hostapd_clear_wep(struct hostapd_data *hapd)
350{
351 if (hapd->drv_priv) {
352 hostapd_set_privacy(hapd, 0);
353 hostapd_broadcast_wep_clear(hapd);
354 }
355}
356
357
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700358static int hostapd_setup_encryption(char *iface, struct hostapd_data *hapd)
359{
360 int i;
361
362 hostapd_broadcast_wep_set(hapd);
363
364 if (hapd->conf->ssid.wep.default_len) {
365 hostapd_set_privacy(hapd, 1);
366 return 0;
367 }
368
Jouni Malinen75ecf522011-06-27 15:19:46 -0700369 /*
370 * When IEEE 802.1X is not enabled, the driver may need to know how to
371 * set authentication algorithms for static WEP.
372 */
373 hostapd_drv_set_authmode(hapd, hapd->conf->auth_algs);
374
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700375 for (i = 0; i < 4; i++) {
376 if (hapd->conf->ssid.wep.key[i] &&
377 hostapd_drv_set_key(iface, hapd, WPA_ALG_WEP, NULL, i,
378 i == hapd->conf->ssid.wep.idx, NULL, 0,
379 hapd->conf->ssid.wep.key[i],
380 hapd->conf->ssid.wep.len[i])) {
381 wpa_printf(MSG_WARNING, "Could not set WEP "
382 "encryption.");
383 return -1;
384 }
385 if (hapd->conf->ssid.wep.key[i] &&
386 i == hapd->conf->ssid.wep.idx)
387 hostapd_set_privacy(hapd, 1);
388 }
389
390 return 0;
391}
392
393
Dmitry Shmidt04949592012-07-19 12:16:46 -0700394static int hostapd_flush_old_stations(struct hostapd_data *hapd, u16 reason)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700395{
396 int ret = 0;
397 u8 addr[ETH_ALEN];
398
399 if (hostapd_drv_none(hapd) || hapd->drv_priv == NULL)
400 return 0;
401
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800402 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "Flushing old station entries");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700403 if (hostapd_flush(hapd)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800404 wpa_msg(hapd->msg_ctx, MSG_WARNING, "Could not connect to "
405 "kernel driver");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700406 ret = -1;
407 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800408 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "Deauthenticate all stations");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700409 os_memset(addr, 0xff, ETH_ALEN);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700410 hostapd_drv_sta_deauth(hapd, addr, reason);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700411 hostapd_free_stas(hapd);
412
413 return ret;
414}
415
416
417/**
418 * hostapd_validate_bssid_configuration - Validate BSSID configuration
419 * @iface: Pointer to interface data
420 * Returns: 0 on success, -1 on failure
421 *
422 * This function is used to validate that the configured BSSIDs are valid.
423 */
424static int hostapd_validate_bssid_configuration(struct hostapd_iface *iface)
425{
426 u8 mask[ETH_ALEN] = { 0 };
427 struct hostapd_data *hapd = iface->bss[0];
428 unsigned int i = iface->conf->num_bss, bits = 0, j;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700429 int auto_addr = 0;
430
431 if (hostapd_drv_none(hapd))
432 return 0;
433
434 /* Generate BSSID mask that is large enough to cover the BSSIDs. */
435
436 /* Determine the bits necessary to cover the number of BSSIDs. */
437 for (i--; i; i >>= 1)
438 bits++;
439
440 /* Determine the bits necessary to any configured BSSIDs,
441 if they are higher than the number of BSSIDs. */
442 for (j = 0; j < iface->conf->num_bss; j++) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800443 if (hostapd_mac_comp_empty(iface->conf->bss[j]->bssid) == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700444 if (j)
445 auto_addr++;
446 continue;
447 }
448
449 for (i = 0; i < ETH_ALEN; i++) {
450 mask[i] |=
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800451 iface->conf->bss[j]->bssid[i] ^
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700452 hapd->own_addr[i];
453 }
454 }
455
456 if (!auto_addr)
457 goto skip_mask_ext;
458
459 for (i = 0; i < ETH_ALEN && mask[i] == 0; i++)
460 ;
461 j = 0;
462 if (i < ETH_ALEN) {
463 j = (5 - i) * 8;
464
465 while (mask[i] != 0) {
466 mask[i] >>= 1;
467 j++;
468 }
469 }
470
471 if (bits < j)
472 bits = j;
473
474 if (bits > 40) {
475 wpa_printf(MSG_ERROR, "Too many bits in the BSSID mask (%u)",
476 bits);
477 return -1;
478 }
479
480 os_memset(mask, 0xff, ETH_ALEN);
481 j = bits / 8;
482 for (i = 5; i > 5 - j; i--)
483 mask[i] = 0;
484 j = bits % 8;
485 while (j--)
486 mask[i] <<= 1;
487
488skip_mask_ext:
489 wpa_printf(MSG_DEBUG, "BSS count %lu, BSSID mask " MACSTR " (%d bits)",
490 (unsigned long) iface->conf->num_bss, MAC2STR(mask), bits);
491
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700492 if (!auto_addr)
493 return 0;
494
495 for (i = 0; i < ETH_ALEN; i++) {
496 if ((hapd->own_addr[i] & mask[i]) != hapd->own_addr[i]) {
497 wpa_printf(MSG_ERROR, "Invalid BSSID mask " MACSTR
498 " for start address " MACSTR ".",
499 MAC2STR(mask), MAC2STR(hapd->own_addr));
500 wpa_printf(MSG_ERROR, "Start address must be the "
501 "first address in the block (i.e., addr "
502 "AND mask == addr).");
503 return -1;
504 }
505 }
506
507 return 0;
508}
509
510
511static int mac_in_conf(struct hostapd_config *conf, const void *a)
512{
513 size_t i;
514
515 for (i = 0; i < conf->num_bss; i++) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800516 if (hostapd_mac_comp(conf->bss[i]->bssid, a) == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700517 return 1;
518 }
519 }
520
521 return 0;
522}
523
524
Dmitry Shmidt04949592012-07-19 12:16:46 -0700525#ifndef CONFIG_NO_RADIUS
526
527static int hostapd_das_nas_mismatch(struct hostapd_data *hapd,
528 struct radius_das_attrs *attr)
529{
530 /* TODO */
531 return 0;
532}
533
534
535static struct sta_info * hostapd_das_find_sta(struct hostapd_data *hapd,
536 struct radius_das_attrs *attr)
537{
538 struct sta_info *sta = NULL;
539 char buf[128];
540
541 if (attr->sta_addr)
542 sta = ap_get_sta(hapd, attr->sta_addr);
543
544 if (sta == NULL && attr->acct_session_id &&
545 attr->acct_session_id_len == 17) {
546 for (sta = hapd->sta_list; sta; sta = sta->next) {
547 os_snprintf(buf, sizeof(buf), "%08X-%08X",
548 sta->acct_session_id_hi,
549 sta->acct_session_id_lo);
550 if (os_memcmp(attr->acct_session_id, buf, 17) == 0)
551 break;
552 }
553 }
554
555 if (sta == NULL && attr->cui) {
556 for (sta = hapd->sta_list; sta; sta = sta->next) {
557 struct wpabuf *cui;
558 cui = ieee802_1x_get_radius_cui(sta->eapol_sm);
559 if (cui && wpabuf_len(cui) == attr->cui_len &&
560 os_memcmp(wpabuf_head(cui), attr->cui,
561 attr->cui_len) == 0)
562 break;
563 }
564 }
565
566 if (sta == NULL && attr->user_name) {
567 for (sta = hapd->sta_list; sta; sta = sta->next) {
568 u8 *identity;
569 size_t identity_len;
570 identity = ieee802_1x_get_identity(sta->eapol_sm,
571 &identity_len);
572 if (identity &&
573 identity_len == attr->user_name_len &&
574 os_memcmp(identity, attr->user_name, identity_len)
575 == 0)
576 break;
577 }
578 }
579
580 return sta;
581}
582
583
584static enum radius_das_res
585hostapd_das_disconnect(void *ctx, struct radius_das_attrs *attr)
586{
587 struct hostapd_data *hapd = ctx;
588 struct sta_info *sta;
589
590 if (hostapd_das_nas_mismatch(hapd, attr))
591 return RADIUS_DAS_NAS_MISMATCH;
592
593 sta = hostapd_das_find_sta(hapd, attr);
594 if (sta == NULL)
595 return RADIUS_DAS_SESSION_NOT_FOUND;
596
597 hostapd_drv_sta_deauth(hapd, sta->addr,
598 WLAN_REASON_PREV_AUTH_NOT_VALID);
599 ap_sta_deauthenticate(hapd, sta, WLAN_REASON_PREV_AUTH_NOT_VALID);
600
601 return RADIUS_DAS_SUCCESS;
602}
603
604#endif /* CONFIG_NO_RADIUS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700605
606
607/**
608 * hostapd_setup_bss - Per-BSS setup (initialization)
609 * @hapd: Pointer to BSS data
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800610 * @first: Whether this BSS is the first BSS of an interface; -1 = not first,
611 * but interface may exist
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700612 *
613 * This function is used to initialize all per-BSS data structures and
614 * resources. This gets called in a loop for each BSS when an interface is
615 * initialized. Most of the modules that are initialized here will be
616 * deinitialized in hostapd_cleanup().
617 */
618static int hostapd_setup_bss(struct hostapd_data *hapd, int first)
619{
620 struct hostapd_bss_config *conf = hapd->conf;
621 u8 ssid[HOSTAPD_MAX_SSID_LEN + 1];
622 int ssid_len, set_ssid;
623 char force_ifname[IFNAMSIZ];
624 u8 if_addr[ETH_ALEN];
625
Dmitry Shmidt54605472013-11-08 11:10:19 -0800626 wpa_printf(MSG_DEBUG, "%s(hapd=%p (%s), first=%d)",
627 __func__, hapd, hapd->conf->iface, first);
628
629 if (hapd->started) {
630 wpa_printf(MSG_ERROR, "%s: Interface %s was already started",
631 __func__, hapd->conf->iface);
632 return -1;
633 }
634 hapd->started = 1;
635
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800636 if (!first || first == -1) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700637 if (hostapd_mac_comp_empty(hapd->conf->bssid) == 0) {
638 /* Allocate the next available BSSID. */
639 do {
640 inc_byte_array(hapd->own_addr, ETH_ALEN);
641 } while (mac_in_conf(hapd->iconf, hapd->own_addr));
642 } else {
643 /* Allocate the configured BSSID. */
644 os_memcpy(hapd->own_addr, hapd->conf->bssid, ETH_ALEN);
645
646 if (hostapd_mac_comp(hapd->own_addr,
647 hapd->iface->bss[0]->own_addr) ==
648 0) {
649 wpa_printf(MSG_ERROR, "BSS '%s' may not have "
650 "BSSID set to the MAC address of "
651 "the radio", hapd->conf->iface);
652 return -1;
653 }
654 }
655
Dmitry Shmidt54605472013-11-08 11:10:19 -0800656 hapd->interface_added = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700657 if (hostapd_if_add(hapd->iface->bss[0], WPA_IF_AP_BSS,
658 hapd->conf->iface, hapd->own_addr, hapd,
659 &hapd->drv_priv, force_ifname, if_addr,
660 hapd->conf->bridge[0] ? hapd->conf->bridge :
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800661 NULL, first == -1)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700662 wpa_printf(MSG_ERROR, "Failed to add BSS (BSSID="
663 MACSTR ")", MAC2STR(hapd->own_addr));
664 return -1;
665 }
666 }
667
668 if (conf->wmm_enabled < 0)
669 conf->wmm_enabled = hapd->iconf->ieee80211n;
670
Dmitry Shmidt04949592012-07-19 12:16:46 -0700671 hostapd_flush_old_stations(hapd, WLAN_REASON_PREV_AUTH_NOT_VALID);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700672 hostapd_set_privacy(hapd, 0);
673
674 hostapd_broadcast_wep_clear(hapd);
675 if (hostapd_setup_encryption(hapd->conf->iface, hapd))
676 return -1;
677
678 /*
679 * Fetch the SSID from the system and use it or,
680 * if one was specified in the config file, verify they
681 * match.
682 */
683 ssid_len = hostapd_get_ssid(hapd, ssid, sizeof(ssid));
684 if (ssid_len < 0) {
685 wpa_printf(MSG_ERROR, "Could not read SSID from system");
686 return -1;
687 }
688 if (conf->ssid.ssid_set) {
689 /*
690 * If SSID is specified in the config file and it differs
691 * from what is being used then force installation of the
692 * new SSID.
693 */
694 set_ssid = (conf->ssid.ssid_len != (size_t) ssid_len ||
695 os_memcmp(conf->ssid.ssid, ssid, ssid_len) != 0);
696 } else {
697 /*
698 * No SSID in the config file; just use the one we got
699 * from the system.
700 */
701 set_ssid = 0;
702 conf->ssid.ssid_len = ssid_len;
703 os_memcpy(conf->ssid.ssid, ssid, conf->ssid.ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700704 }
705
706 if (!hostapd_drv_none(hapd)) {
707 wpa_printf(MSG_ERROR, "Using interface %s with hwaddr " MACSTR
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700708 " and ssid \"%s\"",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700709 hapd->conf->iface, MAC2STR(hapd->own_addr),
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700710 wpa_ssid_txt(hapd->conf->ssid.ssid,
711 hapd->conf->ssid.ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700712 }
713
714 if (hostapd_setup_wpa_psk(conf)) {
715 wpa_printf(MSG_ERROR, "WPA-PSK setup failed.");
716 return -1;
717 }
718
719 /* Set SSID for the kernel driver (to be used in beacon and probe
720 * response frames) */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700721 if (set_ssid && hostapd_set_ssid(hapd, conf->ssid.ssid,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700722 conf->ssid.ssid_len)) {
723 wpa_printf(MSG_ERROR, "Could not set SSID for kernel driver");
724 return -1;
725 }
726
727 if (wpa_debug_level == MSG_MSGDUMP)
728 conf->radius->msg_dumps = 1;
729#ifndef CONFIG_NO_RADIUS
730 hapd->radius = radius_client_init(hapd, conf->radius);
731 if (hapd->radius == NULL) {
732 wpa_printf(MSG_ERROR, "RADIUS client initialization failed.");
733 return -1;
734 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700735
736 if (hapd->conf->radius_das_port) {
737 struct radius_das_conf das_conf;
738 os_memset(&das_conf, 0, sizeof(das_conf));
739 das_conf.port = hapd->conf->radius_das_port;
740 das_conf.shared_secret = hapd->conf->radius_das_shared_secret;
741 das_conf.shared_secret_len =
742 hapd->conf->radius_das_shared_secret_len;
743 das_conf.client_addr = &hapd->conf->radius_das_client_addr;
744 das_conf.time_window = hapd->conf->radius_das_time_window;
745 das_conf.require_event_timestamp =
746 hapd->conf->radius_das_require_event_timestamp;
747 das_conf.ctx = hapd;
748 das_conf.disconnect = hostapd_das_disconnect;
749 hapd->radius_das = radius_das_init(&das_conf);
750 if (hapd->radius_das == NULL) {
751 wpa_printf(MSG_ERROR, "RADIUS DAS initialization "
752 "failed.");
753 return -1;
754 }
755 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700756#endif /* CONFIG_NO_RADIUS */
757
758 if (hostapd_acl_init(hapd)) {
759 wpa_printf(MSG_ERROR, "ACL initialization failed.");
760 return -1;
761 }
762 if (hostapd_init_wps(hapd, conf))
763 return -1;
764
765 if (authsrv_init(hapd) < 0)
766 return -1;
767
768 if (ieee802_1x_init(hapd)) {
769 wpa_printf(MSG_ERROR, "IEEE 802.1X initialization failed.");
770 return -1;
771 }
772
773 if (hapd->conf->wpa && hostapd_setup_wpa(hapd))
774 return -1;
775
776 if (accounting_init(hapd)) {
777 wpa_printf(MSG_ERROR, "Accounting initialization failed.");
778 return -1;
779 }
780
781 if (hapd->conf->ieee802_11f &&
782 (hapd->iapp = iapp_init(hapd, hapd->conf->iapp_iface)) == NULL) {
783 wpa_printf(MSG_ERROR, "IEEE 802.11F (IAPP) initialization "
784 "failed.");
785 return -1;
786 }
787
Dmitry Shmidt04949592012-07-19 12:16:46 -0700788#ifdef CONFIG_INTERWORKING
789 if (gas_serv_init(hapd)) {
790 wpa_printf(MSG_ERROR, "GAS server initialization failed");
791 return -1;
792 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700793
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800794 if (conf->qos_map_set_len &&
795 hostapd_drv_set_qos_map(hapd, conf->qos_map_set,
796 conf->qos_map_set_len)) {
797 wpa_printf(MSG_ERROR, "Failed to initialize QoS Map");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700798 return -1;
799 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800800#endif /* CONFIG_INTERWORKING */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700801
802 if (!hostapd_drv_none(hapd) && vlan_init(hapd)) {
803 wpa_printf(MSG_ERROR, "VLAN initialization failed.");
804 return -1;
805 }
806
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700807 if (!hapd->conf->start_disabled)
808 ieee802_11_set_beacon(hapd);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700809
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800810 if (hapd->wpa_auth && wpa_init_keys(hapd->wpa_auth) < 0)
811 return -1;
812
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700813 if (hapd->driver && hapd->driver->set_operstate)
814 hapd->driver->set_operstate(hapd->drv_priv, 1);
815
816 return 0;
817}
818
819
820static void hostapd_tx_queue_params(struct hostapd_iface *iface)
821{
822 struct hostapd_data *hapd = iface->bss[0];
823 int i;
824 struct hostapd_tx_queue_params *p;
825
826 for (i = 0; i < NUM_TX_QUEUES; i++) {
827 p = &iface->conf->tx_queue[i];
828
829 if (hostapd_set_tx_queue_params(hapd, i, p->aifs, p->cwmin,
830 p->cwmax, p->burst)) {
831 wpa_printf(MSG_DEBUG, "Failed to set TX queue "
832 "parameters for queue %d.", i);
833 /* Continue anyway */
834 }
835 }
836}
837
838
Dmitry Shmidt8bae4132013-06-06 11:25:10 -0700839static int hostapd_set_acl_list(struct hostapd_data *hapd,
840 struct mac_acl_entry *mac_acl,
841 int n_entries, u8 accept_acl)
842{
843 struct hostapd_acl_params *acl_params;
844 int i, err;
845
846 acl_params = os_zalloc(sizeof(*acl_params) +
847 (n_entries * sizeof(acl_params->mac_acl[0])));
848 if (!acl_params)
849 return -ENOMEM;
850
851 for (i = 0; i < n_entries; i++)
852 os_memcpy(acl_params->mac_acl[i].addr, mac_acl[i].addr,
853 ETH_ALEN);
854
855 acl_params->acl_policy = accept_acl;
856 acl_params->num_mac_acl = n_entries;
857
858 err = hostapd_drv_set_acl(hapd, acl_params);
859
860 os_free(acl_params);
861
862 return err;
863}
864
865
866static void hostapd_set_acl(struct hostapd_data *hapd)
867{
868 struct hostapd_config *conf = hapd->iconf;
869 int err;
870 u8 accept_acl;
871
872 if (hapd->iface->drv_max_acl_mac_addrs == 0)
873 return;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800874 if (!(conf->bss[0]->num_accept_mac || conf->bss[0]->num_deny_mac))
Dmitry Shmidt8bae4132013-06-06 11:25:10 -0700875 return;
876
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800877 if (conf->bss[0]->macaddr_acl == DENY_UNLESS_ACCEPTED) {
878 if (conf->bss[0]->num_accept_mac) {
Dmitry Shmidt8bae4132013-06-06 11:25:10 -0700879 accept_acl = 1;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800880 err = hostapd_set_acl_list(hapd,
881 conf->bss[0]->accept_mac,
882 conf->bss[0]->num_accept_mac,
Dmitry Shmidt8bae4132013-06-06 11:25:10 -0700883 accept_acl);
884 if (err) {
885 wpa_printf(MSG_DEBUG, "Failed to set accept acl");
886 return;
887 }
888 } else {
889 wpa_printf(MSG_DEBUG, "Mismatch between ACL Policy & Accept/deny lists file");
890 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800891 } else if (conf->bss[0]->macaddr_acl == ACCEPT_UNLESS_DENIED) {
892 if (conf->bss[0]->num_deny_mac) {
Dmitry Shmidt8bae4132013-06-06 11:25:10 -0700893 accept_acl = 0;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800894 err = hostapd_set_acl_list(hapd, conf->bss[0]->deny_mac,
895 conf->bss[0]->num_deny_mac,
Dmitry Shmidt8bae4132013-06-06 11:25:10 -0700896 accept_acl);
897 if (err) {
898 wpa_printf(MSG_DEBUG, "Failed to set deny acl");
899 return;
900 }
901 } else {
902 wpa_printf(MSG_DEBUG, "Mismatch between ACL Policy & Accept/deny lists file");
903 }
904 }
905}
906
907
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800908static int start_ctrl_iface_bss(struct hostapd_data *hapd)
909{
910 if (!hapd->iface->interfaces ||
911 !hapd->iface->interfaces->ctrl_iface_init)
912 return 0;
913
914 if (hapd->iface->interfaces->ctrl_iface_init(hapd)) {
915 wpa_printf(MSG_ERROR,
916 "Failed to setup control interface for %s",
917 hapd->conf->iface);
918 return -1;
919 }
920
921 return 0;
922}
923
924
925static int start_ctrl_iface(struct hostapd_iface *iface)
926{
927 size_t i;
928
929 if (!iface->interfaces || !iface->interfaces->ctrl_iface_init)
930 return 0;
931
932 for (i = 0; i < iface->num_bss; i++) {
933 struct hostapd_data *hapd = iface->bss[i];
934 if (iface->interfaces->ctrl_iface_init(hapd)) {
935 wpa_printf(MSG_ERROR,
936 "Failed to setup control interface for %s",
937 hapd->conf->iface);
938 return -1;
939 }
940 }
941
942 return 0;
943}
944
945
946static void channel_list_update_timeout(void *eloop_ctx, void *timeout_ctx)
947{
948 struct hostapd_iface *iface = eloop_ctx;
949
950 if (!iface->wait_channel_update) {
951 wpa_printf(MSG_INFO, "Channel list update timeout, but interface was not waiting for it");
952 return;
953 }
954
955 /*
956 * It is possible that the existing channel list is acceptable, so try
957 * to proceed.
958 */
959 wpa_printf(MSG_DEBUG, "Channel list update timeout - try to continue anyway");
960 setup_interface2(iface);
961}
962
963
964void hostapd_channel_list_updated(struct hostapd_iface *iface)
965{
966 if (!iface->wait_channel_update)
967 return;
968
969 wpa_printf(MSG_DEBUG, "Channel list updated - continue setup");
970 eloop_cancel_timeout(channel_list_update_timeout, iface, NULL);
971 setup_interface2(iface);
972}
973
974
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700975static int setup_interface(struct hostapd_iface *iface)
976{
977 struct hostapd_data *hapd = iface->bss[0];
978 size_t i;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800979
980 if (!iface->phy[0]) {
981 const char *phy = hostapd_drv_get_radio_name(hapd);
982 if (phy) {
983 wpa_printf(MSG_DEBUG, "phy: %s", phy);
984 os_strlcpy(iface->phy, phy, sizeof(iface->phy));
985 }
986 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700987
988 /*
989 * Make sure that all BSSes get configured with a pointer to the same
990 * driver interface.
991 */
992 for (i = 1; i < iface->num_bss; i++) {
993 iface->bss[i]->driver = hapd->driver;
994 iface->bss[i]->drv_priv = hapd->drv_priv;
995 }
996
997 if (hostapd_validate_bssid_configuration(iface))
998 return -1;
999
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001000 /*
1001 * Initialize control interfaces early to allow external monitoring of
1002 * channel setup operations that may take considerable amount of time
1003 * especially for DFS cases.
1004 */
1005 if (start_ctrl_iface(iface))
1006 return -1;
1007
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001008 if (hapd->iconf->country[0] && hapd->iconf->country[1]) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001009 char country[4], previous_country[4];
1010
1011 hostapd_set_state(iface, HAPD_IFACE_COUNTRY_UPDATE);
1012 if (hostapd_get_country(hapd, previous_country) < 0)
1013 previous_country[0] = '\0';
1014
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001015 os_memcpy(country, hapd->iconf->country, 3);
1016 country[3] = '\0';
1017 if (hostapd_set_country(hapd, country) < 0) {
1018 wpa_printf(MSG_ERROR, "Failed to set country code");
1019 return -1;
1020 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001021
1022 wpa_printf(MSG_DEBUG, "Previous country code %s, new country code %s",
1023 previous_country, country);
1024
1025 if (os_strncmp(previous_country, country, 2) != 0) {
1026 wpa_printf(MSG_DEBUG, "Continue interface setup after channel list update");
1027 iface->wait_channel_update = 1;
1028 eloop_register_timeout(1, 0,
1029 channel_list_update_timeout,
1030 iface, NULL);
1031 return 0;
1032 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001033 }
1034
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001035 return setup_interface2(iface);
1036}
1037
1038
1039static int setup_interface2(struct hostapd_iface *iface)
1040{
1041 iface->wait_channel_update = 0;
1042
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001043 if (hostapd_get_hw_features(iface)) {
1044 /* Not all drivers support this yet, so continue without hw
1045 * feature data. */
1046 } else {
1047 int ret = hostapd_select_hw_mode(iface);
1048 if (ret < 0) {
1049 wpa_printf(MSG_ERROR, "Could not select hw_mode and "
1050 "channel. (%d)", ret);
1051 return -1;
1052 }
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001053 if (ret == 1) {
1054 wpa_printf(MSG_DEBUG, "Interface initialization will be completed in a callback (ACS)");
1055 return 0;
1056 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001057 ret = hostapd_check_ht_capab(iface);
1058 if (ret < 0)
1059 return -1;
1060 if (ret == 1) {
1061 wpa_printf(MSG_DEBUG, "Interface initialization will "
1062 "be completed in a callback");
1063 return 0;
1064 }
Dmitry Shmidt051af732013-10-22 13:52:46 -07001065
1066 if (iface->conf->ieee80211h)
1067 wpa_printf(MSG_DEBUG, "DFS support is enabled");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001068 }
1069 return hostapd_setup_interface_complete(iface, 0);
1070}
1071
1072
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001073/**
1074 * hostapd_setup_interface_complete - Complete interface setup
1075 *
1076 * This function is called when previous steps in the interface setup has been
1077 * completed. This can also start operations, e.g., DFS, that will require
1078 * additional processing before interface is ready to be enabled. Such
1079 * operations will call this function from eloop callbacks when finished.
1080 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001081int hostapd_setup_interface_complete(struct hostapd_iface *iface, int err)
1082{
1083 struct hostapd_data *hapd = iface->bss[0];
1084 size_t j;
1085 u8 *prev_addr;
1086
1087 if (err) {
1088 wpa_printf(MSG_ERROR, "Interface initialization failed");
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001089 hostapd_set_state(iface, HAPD_IFACE_DISABLED);
Dmitry Shmidtb96dad42013-11-05 10:07:29 -08001090 if (iface->interfaces && iface->interfaces->terminate_on_error)
1091 eloop_terminate();
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001092 return -1;
1093 }
1094
1095 wpa_printf(MSG_DEBUG, "Completing interface initialization");
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001096 if (iface->conf->channel) {
Dmitry Shmidt051af732013-10-22 13:52:46 -07001097#ifdef NEED_AP_MLME
1098 int res;
1099#endif /* NEED_AP_MLME */
1100
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001101 iface->freq = hostapd_hw_get_freq(hapd, iface->conf->channel);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001102 wpa_printf(MSG_DEBUG, "Mode: %s Channel: %d "
1103 "Frequency: %d MHz",
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001104 hostapd_hw_mode_txt(iface->conf->hw_mode),
1105 iface->conf->channel, iface->freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001106
Dmitry Shmidt051af732013-10-22 13:52:46 -07001107#ifdef NEED_AP_MLME
1108 /* Check DFS */
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001109 res = hostapd_handle_dfs(iface);
Dmitry Shmidt051af732013-10-22 13:52:46 -07001110 if (res <= 0)
1111 return res;
1112#endif /* NEED_AP_MLME */
1113
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001114 if (hostapd_set_freq(hapd, hapd->iconf->hw_mode, iface->freq,
1115 hapd->iconf->channel,
1116 hapd->iconf->ieee80211n,
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001117 hapd->iconf->ieee80211ac,
1118 hapd->iconf->secondary_channel,
1119 hapd->iconf->vht_oper_chwidth,
1120 hapd->iconf->vht_oper_centr_freq_seg0_idx,
1121 hapd->iconf->vht_oper_centr_freq_seg1_idx)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001122 wpa_printf(MSG_ERROR, "Could not set channel for "
1123 "kernel driver");
1124 return -1;
1125 }
1126 }
1127
1128 if (iface->current_mode) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001129 if (hostapd_prepare_rates(iface, iface->current_mode)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001130 wpa_printf(MSG_ERROR, "Failed to prepare rates "
1131 "table.");
1132 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
1133 HOSTAPD_LEVEL_WARNING,
1134 "Failed to prepare rates table.");
1135 return -1;
1136 }
1137 }
1138
1139 if (hapd->iconf->rts_threshold > -1 &&
1140 hostapd_set_rts(hapd, hapd->iconf->rts_threshold)) {
1141 wpa_printf(MSG_ERROR, "Could not set RTS threshold for "
1142 "kernel driver");
1143 return -1;
1144 }
1145
1146 if (hapd->iconf->fragm_threshold > -1 &&
1147 hostapd_set_frag(hapd, hapd->iconf->fragm_threshold)) {
1148 wpa_printf(MSG_ERROR, "Could not set fragmentation threshold "
1149 "for kernel driver");
1150 return -1;
1151 }
1152
1153 prev_addr = hapd->own_addr;
1154
1155 for (j = 0; j < iface->num_bss; j++) {
1156 hapd = iface->bss[j];
1157 if (j)
1158 os_memcpy(hapd->own_addr, prev_addr, ETH_ALEN);
1159 if (hostapd_setup_bss(hapd, j == 0))
1160 return -1;
1161 if (hostapd_mac_comp_empty(hapd->conf->bssid) == 0)
1162 prev_addr = hapd->own_addr;
1163 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001164 hapd = iface->bss[0];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001165
1166 hostapd_tx_queue_params(iface);
1167
1168 ap_list_init(iface);
1169
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001170 hostapd_set_acl(hapd);
1171
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001172 if (hostapd_driver_commit(hapd) < 0) {
1173 wpa_printf(MSG_ERROR, "%s: Failed to commit driver "
1174 "configuration", __func__);
1175 return -1;
1176 }
1177
Jouni Malinen87fd2792011-05-16 18:35:42 +03001178 /*
1179 * WPS UPnP module can be initialized only when the "upnp_iface" is up.
1180 * If "interface" and "upnp_iface" are the same (e.g., non-bridge
1181 * mode), the interface is up only after driver_commit, so initialize
1182 * WPS after driver_commit.
1183 */
1184 for (j = 0; j < iface->num_bss; j++) {
1185 if (hostapd_init_wps_complete(iface->bss[j]))
1186 return -1;
1187 }
1188
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001189 hostapd_set_state(iface, HAPD_IFACE_ENABLED);
1190 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_ENABLED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001191 if (hapd->setup_complete_cb)
1192 hapd->setup_complete_cb(hapd->setup_complete_cb_ctx);
1193
1194 wpa_printf(MSG_DEBUG, "%s: Setup of interface done.",
1195 iface->bss[0]->conf->iface);
Dmitry Shmidtb96dad42013-11-05 10:07:29 -08001196 if (iface->interfaces && iface->interfaces->terminate_on_error > 0)
1197 iface->interfaces->terminate_on_error--;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001198
1199 return 0;
1200}
1201
1202
1203/**
1204 * hostapd_setup_interface - Setup of an interface
1205 * @iface: Pointer to interface data.
1206 * Returns: 0 on success, -1 on failure
1207 *
1208 * Initializes the driver interface, validates the configuration,
1209 * and sets driver parameters based on the configuration.
1210 * Flushes old stations, sets the channel, encryption,
1211 * beacons, and WDS links based on the configuration.
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001212 *
1213 * If interface setup requires more time, e.g., to perform HT co-ex scans, ACS,
1214 * or DFS operations, this function returns 0 before such operations have been
1215 * completed. The pending operations are registered into eloop and will be
1216 * completed from eloop callbacks. Those callbacks end up calling
1217 * hostapd_setup_interface_complete() once setup has been completed.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001218 */
1219int hostapd_setup_interface(struct hostapd_iface *iface)
1220{
1221 int ret;
1222
1223 ret = setup_interface(iface);
1224 if (ret) {
1225 wpa_printf(MSG_ERROR, "%s: Unable to setup interface.",
1226 iface->bss[0]->conf->iface);
1227 return -1;
1228 }
1229
1230 return 0;
1231}
1232
1233
1234/**
1235 * hostapd_alloc_bss_data - Allocate and initialize per-BSS data
1236 * @hapd_iface: Pointer to interface data
1237 * @conf: Pointer to per-interface configuration
1238 * @bss: Pointer to per-BSS configuration for this BSS
1239 * Returns: Pointer to allocated BSS data
1240 *
1241 * This function is used to allocate per-BSS data structure. This data will be
1242 * freed after hostapd_cleanup() is called for it during interface
1243 * deinitialization.
1244 */
1245struct hostapd_data *
1246hostapd_alloc_bss_data(struct hostapd_iface *hapd_iface,
1247 struct hostapd_config *conf,
1248 struct hostapd_bss_config *bss)
1249{
1250 struct hostapd_data *hapd;
1251
1252 hapd = os_zalloc(sizeof(*hapd));
1253 if (hapd == NULL)
1254 return NULL;
1255
1256 hapd->new_assoc_sta_cb = hostapd_new_assoc_sta;
1257 hapd->iconf = conf;
1258 hapd->conf = bss;
1259 hapd->iface = hapd_iface;
1260 hapd->driver = hapd->iconf->driver;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001261 hapd->ctrl_sock = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001262
1263 return hapd;
1264}
1265
1266
Dmitry Shmidt54605472013-11-08 11:10:19 -08001267static void hostapd_bss_deinit(struct hostapd_data *hapd)
1268{
1269 wpa_printf(MSG_DEBUG, "%s: deinit bss %s", __func__,
1270 hapd->conf->iface);
1271 hostapd_free_stas(hapd);
1272 hostapd_flush_old_stations(hapd, WLAN_REASON_DEAUTH_LEAVING);
1273 hostapd_clear_wep(hapd);
1274 hostapd_cleanup(hapd);
1275}
1276
1277
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001278void hostapd_interface_deinit(struct hostapd_iface *iface)
1279{
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001280 int j;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001281
Dmitry Shmidt54605472013-11-08 11:10:19 -08001282 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001283 if (iface == NULL)
1284 return;
1285
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001286 eloop_cancel_timeout(channel_list_update_timeout, iface, NULL);
1287 iface->wait_channel_update = 0;
1288
Dmitry Shmidt54605472013-11-08 11:10:19 -08001289 for (j = iface->num_bss - 1; j >= 0; j--)
1290 hostapd_bss_deinit(iface->bss[j]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001291}
1292
1293
1294void hostapd_interface_free(struct hostapd_iface *iface)
1295{
1296 size_t j;
Dmitry Shmidt54605472013-11-08 11:10:19 -08001297 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
1298 for (j = 0; j < iface->num_bss; j++) {
1299 wpa_printf(MSG_DEBUG, "%s: free hapd %p",
1300 __func__, iface->bss[j]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001301 os_free(iface->bss[j]);
Dmitry Shmidt54605472013-11-08 11:10:19 -08001302 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001303 hostapd_cleanup_iface(iface);
1304}
1305
1306
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001307/**
1308 * hostapd_init - Allocate and initialize per-interface data
1309 * @config_file: Path to the configuration file
1310 * Returns: Pointer to the allocated interface data or %NULL on failure
1311 *
1312 * This function is used to allocate main data structures for per-interface
1313 * data. The allocated data buffer will be freed by calling
1314 * hostapd_cleanup_iface().
1315 */
1316struct hostapd_iface * hostapd_init(struct hapd_interfaces *interfaces,
1317 const char *config_file)
1318{
1319 struct hostapd_iface *hapd_iface = NULL;
1320 struct hostapd_config *conf = NULL;
1321 struct hostapd_data *hapd;
1322 size_t i;
1323
1324 hapd_iface = os_zalloc(sizeof(*hapd_iface));
1325 if (hapd_iface == NULL)
1326 goto fail;
1327
1328 hapd_iface->config_fname = os_strdup(config_file);
1329 if (hapd_iface->config_fname == NULL)
1330 goto fail;
1331
1332 conf = interfaces->config_read_cb(hapd_iface->config_fname);
1333 if (conf == NULL)
1334 goto fail;
1335 hapd_iface->conf = conf;
1336
1337 hapd_iface->num_bss = conf->num_bss;
1338 hapd_iface->bss = os_calloc(conf->num_bss,
1339 sizeof(struct hostapd_data *));
1340 if (hapd_iface->bss == NULL)
1341 goto fail;
1342
1343 for (i = 0; i < conf->num_bss; i++) {
1344 hapd = hapd_iface->bss[i] =
1345 hostapd_alloc_bss_data(hapd_iface, conf,
1346 conf->bss[i]);
1347 if (hapd == NULL)
1348 goto fail;
1349 hapd->msg_ctx = hapd;
1350 }
1351
1352 return hapd_iface;
1353
1354fail:
1355 wpa_printf(MSG_ERROR, "Failed to set up interface with %s",
1356 config_file);
1357 if (conf)
1358 hostapd_config_free(conf);
1359 if (hapd_iface) {
1360 os_free(hapd_iface->config_fname);
1361 os_free(hapd_iface->bss);
Dmitry Shmidt54605472013-11-08 11:10:19 -08001362 wpa_printf(MSG_DEBUG, "%s: free iface %p",
1363 __func__, hapd_iface);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001364 os_free(hapd_iface);
1365 }
1366 return NULL;
1367}
1368
1369
1370static int ifname_in_use(struct hapd_interfaces *interfaces, const char *ifname)
1371{
1372 size_t i, j;
1373
1374 for (i = 0; i < interfaces->count; i++) {
1375 struct hostapd_iface *iface = interfaces->iface[i];
1376 for (j = 0; j < iface->num_bss; j++) {
1377 struct hostapd_data *hapd = iface->bss[j];
1378 if (os_strcmp(ifname, hapd->conf->iface) == 0)
1379 return 1;
1380 }
1381 }
1382
1383 return 0;
1384}
1385
1386
1387/**
1388 * hostapd_interface_init_bss - Read configuration file and init BSS data
1389 *
1390 * This function is used to parse configuration file for a BSS. This BSS is
1391 * added to an existing interface sharing the same radio (if any) or a new
1392 * interface is created if this is the first interface on a radio. This
1393 * allocate memory for the BSS. No actual driver operations are started.
1394 *
1395 * This is similar to hostapd_interface_init(), but for a case where the
1396 * configuration is used to add a single BSS instead of all BSSes for a radio.
1397 */
1398struct hostapd_iface *
1399hostapd_interface_init_bss(struct hapd_interfaces *interfaces, const char *phy,
1400 const char *config_fname, int debug)
1401{
1402 struct hostapd_iface *new_iface = NULL, *iface = NULL;
1403 struct hostapd_data *hapd;
1404 int k;
1405 size_t i, bss_idx;
1406
1407 if (!phy || !*phy)
1408 return NULL;
1409
1410 for (i = 0; i < interfaces->count; i++) {
1411 if (os_strcmp(interfaces->iface[i]->phy, phy) == 0) {
1412 iface = interfaces->iface[i];
1413 break;
1414 }
1415 }
1416
1417 wpa_printf(MSG_INFO, "Configuration file: %s (phy %s)%s",
1418 config_fname, phy, iface ? "" : " --> new PHY");
1419 if (iface) {
1420 struct hostapd_config *conf;
1421 struct hostapd_bss_config **tmp_conf;
1422 struct hostapd_data **tmp_bss;
1423 struct hostapd_bss_config *bss;
1424 const char *ifname;
1425
1426 /* Add new BSS to existing iface */
1427 conf = interfaces->config_read_cb(config_fname);
1428 if (conf == NULL)
1429 return NULL;
1430 if (conf->num_bss > 1) {
1431 wpa_printf(MSG_ERROR, "Multiple BSSes specified in BSS-config");
1432 hostapd_config_free(conf);
1433 return NULL;
1434 }
1435
1436 ifname = conf->bss[0]->iface;
1437 if (ifname[0] != '\0' && ifname_in_use(interfaces, ifname)) {
1438 wpa_printf(MSG_ERROR,
1439 "Interface name %s already in use", ifname);
1440 hostapd_config_free(conf);
1441 return NULL;
1442 }
1443
1444 tmp_conf = os_realloc_array(
1445 iface->conf->bss, iface->conf->num_bss + 1,
1446 sizeof(struct hostapd_bss_config *));
1447 tmp_bss = os_realloc_array(iface->bss, iface->num_bss + 1,
1448 sizeof(struct hostapd_data *));
1449 if (tmp_bss)
1450 iface->bss = tmp_bss;
1451 if (tmp_conf) {
1452 iface->conf->bss = tmp_conf;
1453 iface->conf->last_bss = tmp_conf[0];
1454 }
1455 if (tmp_bss == NULL || tmp_conf == NULL) {
1456 hostapd_config_free(conf);
1457 return NULL;
1458 }
1459 bss = iface->conf->bss[iface->conf->num_bss] = conf->bss[0];
1460 iface->conf->num_bss++;
1461
1462 hapd = hostapd_alloc_bss_data(iface, iface->conf, bss);
1463 if (hapd == NULL) {
1464 iface->conf->num_bss--;
1465 hostapd_config_free(conf);
1466 return NULL;
1467 }
1468 iface->conf->last_bss = bss;
1469 iface->bss[iface->num_bss] = hapd;
1470 hapd->msg_ctx = hapd;
1471
1472 bss_idx = iface->num_bss++;
1473 conf->num_bss--;
1474 conf->bss[0] = NULL;
1475 hostapd_config_free(conf);
1476 } else {
1477 /* Add a new iface with the first BSS */
1478 new_iface = iface = hostapd_init(interfaces, config_fname);
1479 if (!iface)
1480 return NULL;
1481 os_strlcpy(iface->phy, phy, sizeof(iface->phy));
1482 iface->interfaces = interfaces;
1483 bss_idx = 0;
1484 }
1485
1486 for (k = 0; k < debug; k++) {
1487 if (iface->bss[bss_idx]->conf->logger_stdout_level > 0)
1488 iface->bss[bss_idx]->conf->logger_stdout_level--;
1489 }
1490
1491 if (iface->conf->bss[bss_idx]->iface[0] == '\0' &&
1492 !hostapd_drv_none(iface->bss[bss_idx])) {
1493 wpa_printf(MSG_ERROR, "Interface name not specified in %s",
1494 config_fname);
1495 if (new_iface)
1496 hostapd_interface_deinit_free(new_iface);
1497 return NULL;
1498 }
1499
1500 return iface;
1501}
1502
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001503
1504void hostapd_interface_deinit_free(struct hostapd_iface *iface)
1505{
1506 const struct wpa_driver_ops *driver;
1507 void *drv_priv;
Dmitry Shmidt54605472013-11-08 11:10:19 -08001508
1509 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001510 if (iface == NULL)
1511 return;
Dmitry Shmidt54605472013-11-08 11:10:19 -08001512 wpa_printf(MSG_DEBUG, "%s: num_bss=%u conf->num_bss=%u",
1513 __func__, (unsigned int) iface->num_bss,
1514 (unsigned int) iface->conf->num_bss);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001515 driver = iface->bss[0]->driver;
1516 drv_priv = iface->bss[0]->drv_priv;
1517 hostapd_interface_deinit(iface);
Dmitry Shmidt54605472013-11-08 11:10:19 -08001518 wpa_printf(MSG_DEBUG, "%s: driver=%p drv_priv=%p -> hapd_deinit",
1519 __func__, driver, drv_priv);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001520 if (driver && driver->hapd_deinit && drv_priv)
1521 driver->hapd_deinit(drv_priv);
1522 hostapd_interface_free(iface);
1523}
1524
1525
1526int hostapd_enable_iface(struct hostapd_iface *hapd_iface)
1527{
1528 if (hapd_iface->bss[0]->drv_priv != NULL) {
1529 wpa_printf(MSG_ERROR, "Interface %s already enabled",
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001530 hapd_iface->conf->bss[0]->iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001531 return -1;
1532 }
1533
1534 wpa_printf(MSG_DEBUG, "Enable interface %s",
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001535 hapd_iface->conf->bss[0]->iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001536
1537 if (hapd_iface->interfaces == NULL ||
1538 hapd_iface->interfaces->driver_init == NULL ||
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001539 hapd_iface->interfaces->driver_init(hapd_iface))
1540 return -1;
1541
1542 if (hostapd_setup_interface(hapd_iface)) {
1543 const struct wpa_driver_ops *driver;
1544 void *drv_priv;
1545
1546 driver = hapd_iface->bss[0]->driver;
1547 drv_priv = hapd_iface->bss[0]->drv_priv;
Dmitry Shmidt54605472013-11-08 11:10:19 -08001548 wpa_printf(MSG_DEBUG, "%s: driver=%p drv_priv=%p -> hapd_deinit",
1549 __func__, driver, drv_priv);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001550 if (driver && driver->hapd_deinit && drv_priv) {
1551 driver->hapd_deinit(drv_priv);
1552 hapd_iface->bss[0]->drv_priv = NULL;
1553 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001554 return -1;
1555 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001556
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001557 return 0;
1558}
1559
1560
1561int hostapd_reload_iface(struct hostapd_iface *hapd_iface)
1562{
1563 size_t j;
1564
1565 wpa_printf(MSG_DEBUG, "Reload interface %s",
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001566 hapd_iface->conf->bss[0]->iface);
1567 for (j = 0; j < hapd_iface->num_bss; j++)
1568 hostapd_set_security_params(hapd_iface->conf->bss[j]);
1569 if (hostapd_config_check(hapd_iface->conf) < 0) {
1570 wpa_printf(MSG_ERROR, "Updated configuration is invalid");
1571 return -1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001572 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001573 hostapd_clear_old(hapd_iface);
1574 for (j = 0; j < hapd_iface->num_bss; j++)
1575 hostapd_reload_bss(hapd_iface->bss[j]);
1576
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001577 return 0;
1578}
1579
1580
1581int hostapd_disable_iface(struct hostapd_iface *hapd_iface)
1582{
1583 size_t j;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001584 const struct wpa_driver_ops *driver;
1585 void *drv_priv;
1586
1587 if (hapd_iface == NULL)
1588 return -1;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001589 wpa_msg(hapd_iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001590 driver = hapd_iface->bss[0]->driver;
1591 drv_priv = hapd_iface->bss[0]->drv_priv;
1592
1593 /* whatever hostapd_interface_deinit does */
1594 for (j = 0; j < hapd_iface->num_bss; j++) {
1595 struct hostapd_data *hapd = hapd_iface->bss[j];
1596 hostapd_free_stas(hapd);
1597 hostapd_flush_old_stations(hapd, WLAN_REASON_DEAUTH_LEAVING);
1598 hostapd_clear_wep(hapd);
1599 hostapd_free_hapd_data(hapd);
1600 }
1601
Dmitry Shmidt54605472013-11-08 11:10:19 -08001602 wpa_printf(MSG_DEBUG, "%s: driver=%p drv_priv=%p -> hapd_deinit",
1603 __func__, driver, drv_priv);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001604 if (driver && driver->hapd_deinit && drv_priv) {
1605 driver->hapd_deinit(drv_priv);
1606 hapd_iface->bss[0]->drv_priv = NULL;
1607 }
1608
1609 /* From hostapd_cleanup_iface: These were initialized in
1610 * hostapd_setup_interface and hostapd_setup_interface_complete
1611 */
1612 hostapd_cleanup_iface_partial(hapd_iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001613
Dmitry Shmidt56052862013-10-04 10:23:25 -07001614 wpa_printf(MSG_DEBUG, "Interface %s disabled",
1615 hapd_iface->bss[0]->conf->iface);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001616 hostapd_set_state(hapd_iface, HAPD_IFACE_DISABLED);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001617 return 0;
1618}
1619
1620
1621static struct hostapd_iface *
1622hostapd_iface_alloc(struct hapd_interfaces *interfaces)
1623{
1624 struct hostapd_iface **iface, *hapd_iface;
1625
1626 iface = os_realloc_array(interfaces->iface, interfaces->count + 1,
1627 sizeof(struct hostapd_iface *));
1628 if (iface == NULL)
1629 return NULL;
1630 interfaces->iface = iface;
1631 hapd_iface = interfaces->iface[interfaces->count] =
1632 os_zalloc(sizeof(*hapd_iface));
1633 if (hapd_iface == NULL) {
1634 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory for "
1635 "the interface", __func__);
1636 return NULL;
1637 }
1638 interfaces->count++;
1639 hapd_iface->interfaces = interfaces;
1640
1641 return hapd_iface;
1642}
1643
1644
1645static struct hostapd_config *
1646hostapd_config_alloc(struct hapd_interfaces *interfaces, const char *ifname,
1647 const char *ctrl_iface)
1648{
1649 struct hostapd_bss_config *bss;
1650 struct hostapd_config *conf;
1651
1652 /* Allocates memory for bss and conf */
1653 conf = hostapd_config_defaults();
1654 if (conf == NULL) {
1655 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory for "
1656 "configuration", __func__);
1657 return NULL;
1658 }
1659
1660 conf->driver = wpa_drivers[0];
1661 if (conf->driver == NULL) {
1662 wpa_printf(MSG_ERROR, "No driver wrappers registered!");
1663 hostapd_config_free(conf);
1664 return NULL;
1665 }
1666
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001667 bss = conf->last_bss = conf->bss[0];
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001668
1669 os_strlcpy(bss->iface, ifname, sizeof(bss->iface));
1670 bss->ctrl_interface = os_strdup(ctrl_iface);
1671 if (bss->ctrl_interface == NULL) {
1672 hostapd_config_free(conf);
1673 return NULL;
1674 }
1675
1676 /* Reading configuration file skipped, will be done in SET!
1677 * From reading the configuration till the end has to be done in
1678 * SET
1679 */
1680 return conf;
1681}
1682
1683
1684static struct hostapd_iface * hostapd_data_alloc(
1685 struct hapd_interfaces *interfaces, struct hostapd_config *conf)
1686{
1687 size_t i;
1688 struct hostapd_iface *hapd_iface =
1689 interfaces->iface[interfaces->count - 1];
1690 struct hostapd_data *hapd;
1691
1692 hapd_iface->conf = conf;
1693 hapd_iface->num_bss = conf->num_bss;
1694
1695 hapd_iface->bss = os_zalloc(conf->num_bss *
1696 sizeof(struct hostapd_data *));
1697 if (hapd_iface->bss == NULL)
1698 return NULL;
1699
1700 for (i = 0; i < conf->num_bss; i++) {
1701 hapd = hapd_iface->bss[i] =
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001702 hostapd_alloc_bss_data(hapd_iface, conf, conf->bss[i]);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001703 if (hapd == NULL)
1704 return NULL;
1705 hapd->msg_ctx = hapd;
1706 }
1707
1708 hapd_iface->interfaces = interfaces;
1709
1710 return hapd_iface;
1711}
1712
1713
1714int hostapd_add_iface(struct hapd_interfaces *interfaces, char *buf)
1715{
1716 struct hostapd_config *conf = NULL;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001717 struct hostapd_iface *hapd_iface = NULL, *new_iface = NULL;
1718 struct hostapd_data *hapd;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001719 char *ptr;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001720 size_t i, j;
1721 const char *conf_file = NULL, *phy_name = NULL;
1722
1723 if (os_strncmp(buf, "bss_config=", 11) == 0) {
1724 char *pos;
1725 phy_name = buf + 11;
1726 pos = os_strchr(phy_name, ':');
1727 if (!pos)
1728 return -1;
1729 *pos++ = '\0';
1730 conf_file = pos;
1731 if (!os_strlen(conf_file))
1732 return -1;
1733
1734 hapd_iface = hostapd_interface_init_bss(interfaces, phy_name,
1735 conf_file, 0);
1736 if (!hapd_iface)
1737 return -1;
1738 for (j = 0; j < interfaces->count; j++) {
1739 if (interfaces->iface[j] == hapd_iface)
1740 break;
1741 }
1742 if (j == interfaces->count) {
1743 struct hostapd_iface **tmp;
1744 tmp = os_realloc_array(interfaces->iface,
1745 interfaces->count + 1,
1746 sizeof(struct hostapd_iface *));
1747 if (!tmp) {
1748 hostapd_interface_deinit_free(hapd_iface);
1749 return -1;
1750 }
1751 interfaces->iface = tmp;
1752 interfaces->iface[interfaces->count++] = hapd_iface;
1753 new_iface = hapd_iface;
1754 }
1755
1756 if (new_iface) {
1757 if (interfaces->driver_init(hapd_iface) ||
1758 hostapd_setup_interface(hapd_iface)) {
1759 interfaces->count--;
1760 goto fail;
1761 }
1762 } else {
1763 /* Assign new BSS with bss[0]'s driver info */
1764 hapd = hapd_iface->bss[hapd_iface->num_bss - 1];
1765 hapd->driver = hapd_iface->bss[0]->driver;
1766 hapd->drv_priv = hapd_iface->bss[0]->drv_priv;
1767 os_memcpy(hapd->own_addr, hapd_iface->bss[0]->own_addr,
1768 ETH_ALEN);
1769
1770 if (start_ctrl_iface_bss(hapd) < 0 ||
Dmitry Shmidt54605472013-11-08 11:10:19 -08001771 (hapd_iface->state == HAPD_IFACE_ENABLED &&
1772 hostapd_setup_bss(hapd, -1))) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001773 hapd_iface->conf->num_bss--;
1774 hapd_iface->num_bss--;
Dmitry Shmidt54605472013-11-08 11:10:19 -08001775 wpa_printf(MSG_DEBUG, "%s: free hapd %p %s",
1776 __func__, hapd, hapd->conf->iface);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001777 os_free(hapd);
1778 return -1;
1779 }
1780 }
1781 return 0;
1782 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001783
1784 ptr = os_strchr(buf, ' ');
1785 if (ptr == NULL)
1786 return -1;
1787 *ptr++ = '\0';
1788
Dmitry Shmidt56052862013-10-04 10:23:25 -07001789 if (os_strncmp(ptr, "config=", 7) == 0)
1790 conf_file = ptr + 7;
1791
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001792 for (i = 0; i < interfaces->count; i++) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001793 if (!os_strcmp(interfaces->iface[i]->conf->bss[0]->iface,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001794 buf)) {
1795 wpa_printf(MSG_INFO, "Cannot add interface - it "
1796 "already exists");
1797 return -1;
1798 }
1799 }
1800
1801 hapd_iface = hostapd_iface_alloc(interfaces);
1802 if (hapd_iface == NULL) {
1803 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
1804 "for interface", __func__);
1805 goto fail;
1806 }
1807
Dmitry Shmidt56052862013-10-04 10:23:25 -07001808 if (conf_file && interfaces->config_read_cb) {
1809 conf = interfaces->config_read_cb(conf_file);
1810 if (conf && conf->bss)
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001811 os_strlcpy(conf->bss[0]->iface, buf,
1812 sizeof(conf->bss[0]->iface));
Dmitry Shmidt56052862013-10-04 10:23:25 -07001813 } else
1814 conf = hostapd_config_alloc(interfaces, buf, ptr);
1815 if (conf == NULL || conf->bss == NULL) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001816 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
1817 "for configuration", __func__);
1818 goto fail;
1819 }
1820
1821 hapd_iface = hostapd_data_alloc(interfaces, conf);
1822 if (hapd_iface == NULL) {
1823 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
1824 "for hostapd", __func__);
1825 goto fail;
1826 }
1827
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001828 if (start_ctrl_iface(hapd_iface) < 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001829 goto fail;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001830
1831 wpa_printf(MSG_INFO, "Add interface '%s'", conf->bss[0]->iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001832
1833 return 0;
1834
1835fail:
1836 if (conf)
1837 hostapd_config_free(conf);
1838 if (hapd_iface) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001839 if (hapd_iface->bss) {
Dmitry Shmidt54605472013-11-08 11:10:19 -08001840 for (i = 0; i < hapd_iface->num_bss; i++) {
1841 hapd = hapd_iface->bss[i];
1842 if (hapd && hapd_iface->interfaces &&
1843 hapd_iface->interfaces->ctrl_iface_deinit)
1844 hapd_iface->interfaces->
1845 ctrl_iface_deinit(hapd);
1846 wpa_printf(MSG_DEBUG, "%s: free hapd %p (%s)",
1847 __func__, hapd_iface->bss[i],
1848 hapd_iface->bss[i]->conf->iface);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001849 os_free(hapd_iface->bss[i]);
Dmitry Shmidt54605472013-11-08 11:10:19 -08001850 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001851 os_free(hapd_iface->bss);
1852 }
Dmitry Shmidt54605472013-11-08 11:10:19 -08001853 wpa_printf(MSG_DEBUG, "%s: free iface %p",
1854 __func__, hapd_iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001855 os_free(hapd_iface);
1856 }
1857 return -1;
1858}
1859
1860
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001861static int hostapd_remove_bss(struct hostapd_iface *iface, unsigned int idx)
1862{
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001863 size_t i;
1864
Dmitry Shmidt54605472013-11-08 11:10:19 -08001865 wpa_printf(MSG_INFO, "Remove BSS '%s'", iface->conf->bss[idx]->iface);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001866
Dmitry Shmidt54605472013-11-08 11:10:19 -08001867 /* Remove hostapd_data only if it has already been initialized */
1868 if (idx < iface->num_bss) {
1869 struct hostapd_data *hapd = iface->bss[idx];
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001870
Dmitry Shmidt54605472013-11-08 11:10:19 -08001871 hostapd_bss_deinit(hapd);
1872 wpa_printf(MSG_DEBUG, "%s: free hapd %p (%s)",
1873 __func__, hapd, hapd->conf->iface);
1874 hostapd_config_free_bss(hapd->conf);
1875 os_free(hapd);
1876
1877 iface->num_bss--;
1878
1879 for (i = idx; i < iface->num_bss; i++)
1880 iface->bss[i] = iface->bss[i + 1];
1881 } else {
1882 hostapd_config_free_bss(iface->conf->bss[idx]);
1883 iface->conf->bss[idx] = NULL;
1884 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001885
1886 iface->conf->num_bss--;
Dmitry Shmidt54605472013-11-08 11:10:19 -08001887 for (i = idx; i < iface->conf->num_bss; i++)
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001888 iface->conf->bss[i] = iface->conf->bss[i + 1];
1889
1890 return 0;
1891}
1892
1893
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001894int hostapd_remove_iface(struct hapd_interfaces *interfaces, char *buf)
1895{
1896 struct hostapd_iface *hapd_iface;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001897 size_t i, j, k = 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001898
1899 for (i = 0; i < interfaces->count; i++) {
1900 hapd_iface = interfaces->iface[i];
1901 if (hapd_iface == NULL)
1902 return -1;
Dmitry Shmidt54605472013-11-08 11:10:19 -08001903 if (!os_strcmp(hapd_iface->conf->bss[0]->iface, buf)) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001904 wpa_printf(MSG_INFO, "Remove interface '%s'", buf);
1905 hostapd_interface_deinit_free(hapd_iface);
1906 k = i;
1907 while (k < (interfaces->count - 1)) {
1908 interfaces->iface[k] =
1909 interfaces->iface[k + 1];
1910 k++;
1911 }
1912 interfaces->count--;
1913 return 0;
1914 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001915
1916 for (j = 0; j < hapd_iface->conf->num_bss; j++) {
1917 if (!os_strcmp(hapd_iface->conf->bss[j]->iface, buf))
1918 return hostapd_remove_bss(hapd_iface, j);
1919 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001920 }
1921 return -1;
1922}
1923
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001924
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001925/**
1926 * hostapd_new_assoc_sta - Notify that a new station associated with the AP
1927 * @hapd: Pointer to BSS data
1928 * @sta: Pointer to the associated STA data
1929 * @reassoc: 1 to indicate this was a re-association; 0 = first association
1930 *
1931 * This function will be called whenever a station associates with the AP. It
1932 * can be called from ieee802_11.c for drivers that export MLME to hostapd and
1933 * from drv_callbacks.c based on driver events for drivers that take care of
1934 * management frames (IEEE 802.11 authentication and association) internally.
1935 */
1936void hostapd_new_assoc_sta(struct hostapd_data *hapd, struct sta_info *sta,
1937 int reassoc)
1938{
1939 if (hapd->tkip_countermeasures) {
1940 hostapd_drv_sta_deauth(hapd, sta->addr,
1941 WLAN_REASON_MICHAEL_MIC_FAILURE);
1942 return;
1943 }
1944
1945 hostapd_prune_associations(hapd, sta->addr);
1946
1947 /* IEEE 802.11F (IAPP) */
1948 if (hapd->conf->ieee802_11f)
1949 iapp_new_station(hapd->iapp, sta);
1950
1951#ifdef CONFIG_P2P
1952 if (sta->p2p_ie == NULL && !sta->no_p2p_set) {
1953 sta->no_p2p_set = 1;
1954 hapd->num_sta_no_p2p++;
1955 if (hapd->num_sta_no_p2p == 1)
1956 hostapd_p2p_non_p2p_sta_connected(hapd);
1957 }
1958#endif /* CONFIG_P2P */
1959
1960 /* Start accounting here, if IEEE 802.1X and WPA are not used.
1961 * IEEE 802.1X/WPA code will start accounting after the station has
1962 * been authorized. */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001963 if (!hapd->conf->ieee802_1x && !hapd->conf->wpa) {
1964 os_get_time(&sta->connected_time);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001965 accounting_sta_start(hapd, sta);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001966 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001967
1968 /* Start IEEE 802.1X authentication process for new stations */
1969 ieee802_1x_new_station(hapd, sta);
1970 if (reassoc) {
1971 if (sta->auth_alg != WLAN_AUTH_FT &&
1972 !(sta->flags & (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS)))
1973 wpa_auth_sm_event(sta->wpa_sm, WPA_REAUTH);
1974 } else
1975 wpa_auth_sta_associated(hapd->wpa_auth, sta->wpa_sm);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001976
1977 wpa_printf(MSG_DEBUG, "%s: reschedule ap_handle_timer timeout "
1978 "for " MACSTR " (%d seconds - ap_max_inactivity)",
1979 __func__, MAC2STR(sta->addr),
1980 hapd->conf->ap_max_inactivity);
1981 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
1982 eloop_register_timeout(hapd->conf->ap_max_inactivity, 0,
1983 ap_handle_timer, hapd, sta);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001984}
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001985
1986
1987const char * hostapd_state_text(enum hostapd_iface_state s)
1988{
1989 switch (s) {
1990 case HAPD_IFACE_UNINITIALIZED:
1991 return "UNINITIALIZED";
1992 case HAPD_IFACE_DISABLED:
1993 return "DISABLED";
1994 case HAPD_IFACE_COUNTRY_UPDATE:
1995 return "COUNTRY_UPDATE";
1996 case HAPD_IFACE_ACS:
1997 return "ACS";
1998 case HAPD_IFACE_HT_SCAN:
1999 return "HT_SCAN";
2000 case HAPD_IFACE_DFS:
2001 return "DFS";
2002 case HAPD_IFACE_ENABLED:
2003 return "ENABLED";
2004 }
2005
2006 return "UNKNOWN";
2007}
2008
2009
2010void hostapd_set_state(struct hostapd_iface *iface, enum hostapd_iface_state s)
2011{
2012 wpa_printf(MSG_INFO, "%s: interface state %s->%s",
2013 iface->conf->bss[0]->iface, hostapd_state_text(iface->state),
2014 hostapd_state_text(s));
2015 iface->state = s;
2016}