hostapd.c revision c2ebb4b85d69b65f552fee71ac68f44e8d87b39e
1/*
2 * hostapd / Initialization and configuration
3 * Copyright (c) 2002-2012, Jouni Malinen <j@w1.fi>
4 *
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
7 */
8
9#include "utils/includes.h"
10
11#include "utils/common.h"
12#include "utils/eloop.h"
13#include "common/ieee802_11_defs.h"
14#include "radius/radius_client.h"
15#include "radius/radius_das.h"
16#include "drivers/driver.h"
17#include "hostapd.h"
18#include "authsrv.h"
19#include "sta_info.h"
20#include "accounting.h"
21#include "ap_list.h"
22#include "beacon.h"
23#include "iapp.h"
24#include "ieee802_1x.h"
25#include "ieee802_11_auth.h"
26#include "vlan_init.h"
27#include "wpa_auth.h"
28#include "wps_hostapd.h"
29#include "hw_features.h"
30#include "wpa_auth_glue.h"
31#include "ap_drv_ops.h"
32#include "ap_config.h"
33#include "p2p_hostapd.h"
34#include "gas_serv.h"
35
36
37static int hostapd_flush_old_stations(struct hostapd_data *hapd, u16 reason);
38static int hostapd_setup_encryption(char *iface, struct hostapd_data *hapd);
39static int hostapd_broadcast_wep_clear(struct hostapd_data *hapd);
40
41extern int wpa_debug_level;
42extern struct wpa_driver_ops *wpa_drivers[];
43
44
45int hostapd_for_each_interface(struct hapd_interfaces *interfaces,
46			       int (*cb)(struct hostapd_iface *iface,
47					 void *ctx), void *ctx)
48{
49	size_t i;
50	int ret;
51
52	for (i = 0; i < interfaces->count; i++) {
53		ret = cb(interfaces->iface[i], ctx);
54		if (ret)
55			return ret;
56	}
57
58	return 0;
59}
60
61
62static void hostapd_reload_bss(struct hostapd_data *hapd)
63{
64#ifndef CONFIG_NO_RADIUS
65	radius_client_reconfig(hapd->radius, hapd->conf->radius);
66#endif /* CONFIG_NO_RADIUS */
67
68	if (hostapd_setup_wpa_psk(hapd->conf)) {
69		wpa_printf(MSG_ERROR, "Failed to re-configure WPA PSK "
70			   "after reloading configuration");
71	}
72
73	if (hapd->conf->ieee802_1x || hapd->conf->wpa)
74		hostapd_set_drv_ieee8021x(hapd, hapd->conf->iface, 1);
75	else
76		hostapd_set_drv_ieee8021x(hapd, hapd->conf->iface, 0);
77
78	if (hapd->conf->wpa && hapd->wpa_auth == NULL) {
79		hostapd_setup_wpa(hapd);
80		if (hapd->wpa_auth)
81			wpa_init_keys(hapd->wpa_auth);
82	} else if (hapd->conf->wpa) {
83		const u8 *wpa_ie;
84		size_t wpa_ie_len;
85		hostapd_reconfig_wpa(hapd);
86		wpa_ie = wpa_auth_get_wpa_ie(hapd->wpa_auth, &wpa_ie_len);
87		if (hostapd_set_generic_elem(hapd, wpa_ie, wpa_ie_len))
88			wpa_printf(MSG_ERROR, "Failed to configure WPA IE for "
89				   "the kernel driver.");
90	} else if (hapd->wpa_auth) {
91		wpa_deinit(hapd->wpa_auth);
92		hapd->wpa_auth = NULL;
93		hostapd_set_privacy(hapd, 0);
94		hostapd_setup_encryption(hapd->conf->iface, hapd);
95		hostapd_set_generic_elem(hapd, (u8 *) "", 0);
96	}
97
98	ieee802_11_set_beacon(hapd);
99	hostapd_update_wps(hapd);
100
101	if (hapd->conf->ssid.ssid_set &&
102	    hostapd_set_ssid(hapd, hapd->conf->ssid.ssid,
103			     hapd->conf->ssid.ssid_len)) {
104		wpa_printf(MSG_ERROR, "Could not set SSID for kernel driver");
105		/* try to continue */
106	}
107	wpa_printf(MSG_DEBUG, "Reconfigured interface %s", hapd->conf->iface);
108}
109
110
111static void hostapd_clear_old(struct hostapd_iface *iface)
112{
113	size_t j;
114
115	/*
116	 * Deauthenticate all stations since the new configuration may not
117	 * allow them to use the BSS anymore.
118	 */
119	for (j = 0; j < iface->num_bss; j++) {
120		hostapd_flush_old_stations(iface->bss[j],
121					   WLAN_REASON_PREV_AUTH_NOT_VALID);
122		hostapd_broadcast_wep_clear(iface->bss[j]);
123
124#ifndef CONFIG_NO_RADIUS
125		/* TODO: update dynamic data based on changed configuration
126		 * items (e.g., open/close sockets, etc.) */
127		radius_client_flush(iface->bss[j]->radius, 0);
128#endif /* CONFIG_NO_RADIUS */
129	}
130}
131
132
133int hostapd_reload_config(struct hostapd_iface *iface)
134{
135	struct hostapd_data *hapd = iface->bss[0];
136	struct hostapd_config *newconf, *oldconf;
137	size_t j;
138
139	if (iface->config_fname == NULL) {
140		/* Only in-memory config in use - assume it has been updated */
141		hostapd_clear_old(iface);
142		for (j = 0; j < iface->num_bss; j++)
143			hostapd_reload_bss(iface->bss[j]);
144		return 0;
145	}
146
147	if (iface->interfaces == NULL ||
148	    iface->interfaces->config_read_cb == NULL)
149		return -1;
150	newconf = iface->interfaces->config_read_cb(iface->config_fname);
151	if (newconf == NULL)
152		return -1;
153
154	hostapd_clear_old(iface);
155
156	oldconf = hapd->iconf;
157	iface->conf = newconf;
158
159	for (j = 0; j < iface->num_bss; j++) {
160		hapd = iface->bss[j];
161		hapd->iconf = newconf;
162		hapd->conf = &newconf->bss[j];
163		hostapd_reload_bss(hapd);
164	}
165
166	hostapd_config_free(oldconf);
167
168
169	return 0;
170}
171
172
173static void hostapd_broadcast_key_clear_iface(struct hostapd_data *hapd,
174					      char *ifname)
175{
176	int i;
177
178	for (i = 0; i < NUM_WEP_KEYS; i++) {
179		if (hostapd_drv_set_key(ifname, hapd, WPA_ALG_NONE, NULL, i,
180					0, NULL, 0, NULL, 0)) {
181			wpa_printf(MSG_DEBUG, "Failed to clear default "
182				   "encryption keys (ifname=%s keyidx=%d)",
183				   ifname, i);
184		}
185	}
186#ifdef CONFIG_IEEE80211W
187	if (hapd->conf->ieee80211w) {
188		for (i = NUM_WEP_KEYS; i < NUM_WEP_KEYS + 2; i++) {
189			if (hostapd_drv_set_key(ifname, hapd, WPA_ALG_NONE,
190						NULL, i, 0, NULL,
191						0, NULL, 0)) {
192				wpa_printf(MSG_DEBUG, "Failed to clear "
193					   "default mgmt encryption keys "
194					   "(ifname=%s keyidx=%d)", ifname, i);
195			}
196		}
197	}
198#endif /* CONFIG_IEEE80211W */
199}
200
201
202static int hostapd_broadcast_wep_clear(struct hostapd_data *hapd)
203{
204	hostapd_broadcast_key_clear_iface(hapd, hapd->conf->iface);
205	return 0;
206}
207
208
209static int hostapd_broadcast_wep_set(struct hostapd_data *hapd)
210{
211	int errors = 0, idx;
212	struct hostapd_ssid *ssid = &hapd->conf->ssid;
213
214	idx = ssid->wep.idx;
215	if (ssid->wep.default_len &&
216	    hostapd_drv_set_key(hapd->conf->iface,
217				hapd, WPA_ALG_WEP, broadcast_ether_addr, idx,
218				1, NULL, 0, ssid->wep.key[idx],
219				ssid->wep.len[idx])) {
220		wpa_printf(MSG_WARNING, "Could not set WEP encryption.");
221		errors++;
222	}
223
224	if (ssid->dyn_vlan_keys) {
225		size_t i;
226		for (i = 0; i <= ssid->max_dyn_vlan_keys; i++) {
227			const char *ifname;
228			struct hostapd_wep_keys *key = ssid->dyn_vlan_keys[i];
229			if (key == NULL)
230				continue;
231			ifname = hostapd_get_vlan_id_ifname(hapd->conf->vlan,
232							    i);
233			if (ifname == NULL)
234				continue;
235
236			idx = key->idx;
237			if (hostapd_drv_set_key(ifname, hapd, WPA_ALG_WEP,
238						broadcast_ether_addr, idx, 1,
239						NULL, 0, key->key[idx],
240						key->len[idx])) {
241				wpa_printf(MSG_WARNING, "Could not set "
242					   "dynamic VLAN WEP encryption.");
243				errors++;
244			}
245		}
246	}
247
248	return errors;
249}
250
251
252static void hostapd_free_hapd_data(struct hostapd_data *hapd)
253{
254	iapp_deinit(hapd->iapp);
255	hapd->iapp = NULL;
256	accounting_deinit(hapd);
257	hostapd_deinit_wpa(hapd);
258	vlan_deinit(hapd);
259	hostapd_acl_deinit(hapd);
260#ifndef CONFIG_NO_RADIUS
261	radius_client_deinit(hapd->radius);
262	hapd->radius = NULL;
263	radius_das_deinit(hapd->radius_das);
264	hapd->radius_das = NULL;
265#endif /* CONFIG_NO_RADIUS */
266
267	hostapd_deinit_wps(hapd);
268
269	authsrv_deinit(hapd);
270
271	if (hapd->interface_added &&
272	    hostapd_if_remove(hapd, WPA_IF_AP_BSS, hapd->conf->iface)) {
273		wpa_printf(MSG_WARNING, "Failed to remove BSS interface %s",
274			   hapd->conf->iface);
275	}
276
277	os_free(hapd->probereq_cb);
278	hapd->probereq_cb = NULL;
279
280#ifdef CONFIG_P2P
281	wpabuf_free(hapd->p2p_beacon_ie);
282	hapd->p2p_beacon_ie = NULL;
283	wpabuf_free(hapd->p2p_probe_resp_ie);
284	hapd->p2p_probe_resp_ie = NULL;
285#endif /* CONFIG_P2P */
286
287	wpabuf_free(hapd->time_adv);
288
289#ifdef CONFIG_INTERWORKING
290	gas_serv_deinit(hapd);
291#endif /* CONFIG_INTERWORKING */
292
293#ifdef CONFIG_SQLITE
294	os_free(hapd->tmp_eap_user.identity);
295	os_free(hapd->tmp_eap_user.password);
296#endif /* CONFIG_SQLITE */
297}
298
299
300/**
301 * hostapd_cleanup - Per-BSS cleanup (deinitialization)
302 * @hapd: Pointer to BSS data
303 *
304 * This function is used to free all per-BSS data structures and resources.
305 * This gets called in a loop for each BSS between calls to
306 * hostapd_cleanup_iface_pre() and hostapd_cleanup_iface() when an interface
307 * is deinitialized. Most of the modules that are initialized in
308 * hostapd_setup_bss() are deinitialized here.
309 */
310static void hostapd_cleanup(struct hostapd_data *hapd)
311{
312	if (hapd->iface->interfaces &&
313	    hapd->iface->interfaces->ctrl_iface_deinit)
314		hapd->iface->interfaces->ctrl_iface_deinit(hapd);
315	hostapd_free_hapd_data(hapd);
316}
317
318
319/**
320 * hostapd_cleanup_iface_pre - Preliminary per-interface cleanup
321 * @iface: Pointer to interface data
322 *
323 * This function is called before per-BSS data structures are deinitialized
324 * with hostapd_cleanup().
325 */
326static void hostapd_cleanup_iface_pre(struct hostapd_iface *iface)
327{
328}
329
330
331static void hostapd_cleanup_iface_partial(struct hostapd_iface *iface)
332{
333	hostapd_free_hw_features(iface->hw_features, iface->num_hw_features);
334	iface->hw_features = NULL;
335	os_free(iface->current_rates);
336	iface->current_rates = NULL;
337	os_free(iface->basic_rates);
338	iface->basic_rates = NULL;
339	ap_list_deinit(iface);
340}
341
342
343/**
344 * hostapd_cleanup_iface - Complete per-interface cleanup
345 * @iface: Pointer to interface data
346 *
347 * This function is called after per-BSS data structures are deinitialized
348 * with hostapd_cleanup().
349 */
350static void hostapd_cleanup_iface(struct hostapd_iface *iface)
351{
352	hostapd_cleanup_iface_partial(iface);
353	hostapd_config_free(iface->conf);
354	iface->conf = NULL;
355
356	os_free(iface->config_fname);
357	os_free(iface->bss);
358	os_free(iface);
359}
360
361
362static void hostapd_clear_wep(struct hostapd_data *hapd)
363{
364	if (hapd->drv_priv) {
365		hostapd_set_privacy(hapd, 0);
366		hostapd_broadcast_wep_clear(hapd);
367	}
368}
369
370
371static int hostapd_setup_encryption(char *iface, struct hostapd_data *hapd)
372{
373	int i;
374
375	hostapd_broadcast_wep_set(hapd);
376
377	if (hapd->conf->ssid.wep.default_len) {
378		hostapd_set_privacy(hapd, 1);
379		return 0;
380	}
381
382	/*
383	 * When IEEE 802.1X is not enabled, the driver may need to know how to
384	 * set authentication algorithms for static WEP.
385	 */
386	hostapd_drv_set_authmode(hapd, hapd->conf->auth_algs);
387
388	for (i = 0; i < 4; i++) {
389		if (hapd->conf->ssid.wep.key[i] &&
390		    hostapd_drv_set_key(iface, hapd, WPA_ALG_WEP, NULL, i,
391					i == hapd->conf->ssid.wep.idx, NULL, 0,
392					hapd->conf->ssid.wep.key[i],
393					hapd->conf->ssid.wep.len[i])) {
394			wpa_printf(MSG_WARNING, "Could not set WEP "
395				   "encryption.");
396			return -1;
397		}
398		if (hapd->conf->ssid.wep.key[i] &&
399		    i == hapd->conf->ssid.wep.idx)
400			hostapd_set_privacy(hapd, 1);
401	}
402
403	return 0;
404}
405
406
407static int hostapd_flush_old_stations(struct hostapd_data *hapd, u16 reason)
408{
409	int ret = 0;
410	u8 addr[ETH_ALEN];
411
412	if (hostapd_drv_none(hapd) || hapd->drv_priv == NULL)
413		return 0;
414
415	wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "Flushing old station entries");
416	if (hostapd_flush(hapd)) {
417		wpa_msg(hapd->msg_ctx, MSG_WARNING, "Could not connect to "
418			"kernel driver");
419		ret = -1;
420	}
421	wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "Deauthenticate all stations");
422	os_memset(addr, 0xff, ETH_ALEN);
423	hostapd_drv_sta_deauth(hapd, addr, reason);
424	hostapd_free_stas(hapd);
425
426	return ret;
427}
428
429
430/**
431 * hostapd_validate_bssid_configuration - Validate BSSID configuration
432 * @iface: Pointer to interface data
433 * Returns: 0 on success, -1 on failure
434 *
435 * This function is used to validate that the configured BSSIDs are valid.
436 */
437static int hostapd_validate_bssid_configuration(struct hostapd_iface *iface)
438{
439	u8 mask[ETH_ALEN] = { 0 };
440	struct hostapd_data *hapd = iface->bss[0];
441	unsigned int i = iface->conf->num_bss, bits = 0, j;
442	int auto_addr = 0;
443
444	if (hostapd_drv_none(hapd))
445		return 0;
446
447	/* Generate BSSID mask that is large enough to cover the BSSIDs. */
448
449	/* Determine the bits necessary to cover the number of BSSIDs. */
450	for (i--; i; i >>= 1)
451		bits++;
452
453	/* Determine the bits necessary to any configured BSSIDs,
454	   if they are higher than the number of BSSIDs. */
455	for (j = 0; j < iface->conf->num_bss; j++) {
456		if (hostapd_mac_comp_empty(iface->conf->bss[j].bssid) == 0) {
457			if (j)
458				auto_addr++;
459			continue;
460		}
461
462		for (i = 0; i < ETH_ALEN; i++) {
463			mask[i] |=
464				iface->conf->bss[j].bssid[i] ^
465				hapd->own_addr[i];
466		}
467	}
468
469	if (!auto_addr)
470		goto skip_mask_ext;
471
472	for (i = 0; i < ETH_ALEN && mask[i] == 0; i++)
473		;
474	j = 0;
475	if (i < ETH_ALEN) {
476		j = (5 - i) * 8;
477
478		while (mask[i] != 0) {
479			mask[i] >>= 1;
480			j++;
481		}
482	}
483
484	if (bits < j)
485		bits = j;
486
487	if (bits > 40) {
488		wpa_printf(MSG_ERROR, "Too many bits in the BSSID mask (%u)",
489			   bits);
490		return -1;
491	}
492
493	os_memset(mask, 0xff, ETH_ALEN);
494	j = bits / 8;
495	for (i = 5; i > 5 - j; i--)
496		mask[i] = 0;
497	j = bits % 8;
498	while (j--)
499		mask[i] <<= 1;
500
501skip_mask_ext:
502	wpa_printf(MSG_DEBUG, "BSS count %lu, BSSID mask " MACSTR " (%d bits)",
503		   (unsigned long) iface->conf->num_bss, MAC2STR(mask), bits);
504
505	if (!auto_addr)
506		return 0;
507
508	for (i = 0; i < ETH_ALEN; i++) {
509		if ((hapd->own_addr[i] & mask[i]) != hapd->own_addr[i]) {
510			wpa_printf(MSG_ERROR, "Invalid BSSID mask " MACSTR
511				   " for start address " MACSTR ".",
512				   MAC2STR(mask), MAC2STR(hapd->own_addr));
513			wpa_printf(MSG_ERROR, "Start address must be the "
514				   "first address in the block (i.e., addr "
515				   "AND mask == addr).");
516			return -1;
517		}
518	}
519
520	return 0;
521}
522
523
524static int mac_in_conf(struct hostapd_config *conf, const void *a)
525{
526	size_t i;
527
528	for (i = 0; i < conf->num_bss; i++) {
529		if (hostapd_mac_comp(conf->bss[i].bssid, a) == 0) {
530			return 1;
531		}
532	}
533
534	return 0;
535}
536
537
538#ifndef CONFIG_NO_RADIUS
539
540static int hostapd_das_nas_mismatch(struct hostapd_data *hapd,
541				    struct radius_das_attrs *attr)
542{
543	/* TODO */
544	return 0;
545}
546
547
548static struct sta_info * hostapd_das_find_sta(struct hostapd_data *hapd,
549					      struct radius_das_attrs *attr)
550{
551	struct sta_info *sta = NULL;
552	char buf[128];
553
554	if (attr->sta_addr)
555		sta = ap_get_sta(hapd, attr->sta_addr);
556
557	if (sta == NULL && attr->acct_session_id &&
558	    attr->acct_session_id_len == 17) {
559		for (sta = hapd->sta_list; sta; sta = sta->next) {
560			os_snprintf(buf, sizeof(buf), "%08X-%08X",
561				    sta->acct_session_id_hi,
562				    sta->acct_session_id_lo);
563			if (os_memcmp(attr->acct_session_id, buf, 17) == 0)
564				break;
565		}
566	}
567
568	if (sta == NULL && attr->cui) {
569		for (sta = hapd->sta_list; sta; sta = sta->next) {
570			struct wpabuf *cui;
571			cui = ieee802_1x_get_radius_cui(sta->eapol_sm);
572			if (cui && wpabuf_len(cui) == attr->cui_len &&
573			    os_memcmp(wpabuf_head(cui), attr->cui,
574				      attr->cui_len) == 0)
575				break;
576		}
577	}
578
579	if (sta == NULL && attr->user_name) {
580		for (sta = hapd->sta_list; sta; sta = sta->next) {
581			u8 *identity;
582			size_t identity_len;
583			identity = ieee802_1x_get_identity(sta->eapol_sm,
584							   &identity_len);
585			if (identity &&
586			    identity_len == attr->user_name_len &&
587			    os_memcmp(identity, attr->user_name, identity_len)
588			    == 0)
589				break;
590		}
591	}
592
593	return sta;
594}
595
596
597static enum radius_das_res
598hostapd_das_disconnect(void *ctx, struct radius_das_attrs *attr)
599{
600	struct hostapd_data *hapd = ctx;
601	struct sta_info *sta;
602
603	if (hostapd_das_nas_mismatch(hapd, attr))
604		return RADIUS_DAS_NAS_MISMATCH;
605
606	sta = hostapd_das_find_sta(hapd, attr);
607	if (sta == NULL)
608		return RADIUS_DAS_SESSION_NOT_FOUND;
609
610	hostapd_drv_sta_deauth(hapd, sta->addr,
611			       WLAN_REASON_PREV_AUTH_NOT_VALID);
612	ap_sta_deauthenticate(hapd, sta, WLAN_REASON_PREV_AUTH_NOT_VALID);
613
614	return RADIUS_DAS_SUCCESS;
615}
616
617#endif /* CONFIG_NO_RADIUS */
618
619
620/**
621 * hostapd_setup_bss - Per-BSS setup (initialization)
622 * @hapd: Pointer to BSS data
623 * @first: Whether this BSS is the first BSS of an interface
624 *
625 * This function is used to initialize all per-BSS data structures and
626 * resources. This gets called in a loop for each BSS when an interface is
627 * initialized. Most of the modules that are initialized here will be
628 * deinitialized in hostapd_cleanup().
629 */
630static int hostapd_setup_bss(struct hostapd_data *hapd, int first)
631{
632	struct hostapd_bss_config *conf = hapd->conf;
633	u8 ssid[HOSTAPD_MAX_SSID_LEN + 1];
634	int ssid_len, set_ssid;
635	char force_ifname[IFNAMSIZ];
636	u8 if_addr[ETH_ALEN];
637
638	if (!first) {
639		if (hostapd_mac_comp_empty(hapd->conf->bssid) == 0) {
640			/* Allocate the next available BSSID. */
641			do {
642				inc_byte_array(hapd->own_addr, ETH_ALEN);
643			} while (mac_in_conf(hapd->iconf, hapd->own_addr));
644		} else {
645			/* Allocate the configured BSSID. */
646			os_memcpy(hapd->own_addr, hapd->conf->bssid, ETH_ALEN);
647
648			if (hostapd_mac_comp(hapd->own_addr,
649					     hapd->iface->bss[0]->own_addr) ==
650			    0) {
651				wpa_printf(MSG_ERROR, "BSS '%s' may not have "
652					   "BSSID set to the MAC address of "
653					   "the radio", hapd->conf->iface);
654				return -1;
655			}
656		}
657
658		hapd->interface_added = 1;
659		if (hostapd_if_add(hapd->iface->bss[0], WPA_IF_AP_BSS,
660				   hapd->conf->iface, hapd->own_addr, hapd,
661				   &hapd->drv_priv, force_ifname, if_addr,
662				   hapd->conf->bridge[0] ? hapd->conf->bridge :
663				   NULL)) {
664			wpa_printf(MSG_ERROR, "Failed to add BSS (BSSID="
665				   MACSTR ")", MAC2STR(hapd->own_addr));
666			return -1;
667		}
668	}
669
670	if (conf->wmm_enabled < 0)
671		conf->wmm_enabled = hapd->iconf->ieee80211n;
672
673	hostapd_flush_old_stations(hapd, WLAN_REASON_PREV_AUTH_NOT_VALID);
674	hostapd_set_privacy(hapd, 0);
675
676	hostapd_broadcast_wep_clear(hapd);
677	if (hostapd_setup_encryption(hapd->conf->iface, hapd))
678		return -1;
679
680	/*
681	 * Fetch the SSID from the system and use it or,
682	 * if one was specified in the config file, verify they
683	 * match.
684	 */
685	ssid_len = hostapd_get_ssid(hapd, ssid, sizeof(ssid));
686	if (ssid_len < 0) {
687		wpa_printf(MSG_ERROR, "Could not read SSID from system");
688		return -1;
689	}
690	if (conf->ssid.ssid_set) {
691		/*
692		 * If SSID is specified in the config file and it differs
693		 * from what is being used then force installation of the
694		 * new SSID.
695		 */
696		set_ssid = (conf->ssid.ssid_len != (size_t) ssid_len ||
697			    os_memcmp(conf->ssid.ssid, ssid, ssid_len) != 0);
698	} else {
699		/*
700		 * No SSID in the config file; just use the one we got
701		 * from the system.
702		 */
703		set_ssid = 0;
704		conf->ssid.ssid_len = ssid_len;
705		os_memcpy(conf->ssid.ssid, ssid, conf->ssid.ssid_len);
706	}
707
708	if (!hostapd_drv_none(hapd)) {
709		wpa_printf(MSG_ERROR, "Using interface %s with hwaddr " MACSTR
710			   " and ssid \"%s\"",
711			   hapd->conf->iface, MAC2STR(hapd->own_addr),
712			   wpa_ssid_txt(hapd->conf->ssid.ssid,
713					hapd->conf->ssid.ssid_len));
714	}
715
716	if (hostapd_setup_wpa_psk(conf)) {
717		wpa_printf(MSG_ERROR, "WPA-PSK setup failed.");
718		return -1;
719	}
720
721	/* Set SSID for the kernel driver (to be used in beacon and probe
722	 * response frames) */
723	if (set_ssid && hostapd_set_ssid(hapd, conf->ssid.ssid,
724					 conf->ssid.ssid_len)) {
725		wpa_printf(MSG_ERROR, "Could not set SSID for kernel driver");
726		return -1;
727	}
728
729	if (wpa_debug_level == MSG_MSGDUMP)
730		conf->radius->msg_dumps = 1;
731#ifndef CONFIG_NO_RADIUS
732	hapd->radius = radius_client_init(hapd, conf->radius);
733	if (hapd->radius == NULL) {
734		wpa_printf(MSG_ERROR, "RADIUS client initialization failed.");
735		return -1;
736	}
737
738	if (hapd->conf->radius_das_port) {
739		struct radius_das_conf das_conf;
740		os_memset(&das_conf, 0, sizeof(das_conf));
741		das_conf.port = hapd->conf->radius_das_port;
742		das_conf.shared_secret = hapd->conf->radius_das_shared_secret;
743		das_conf.shared_secret_len =
744			hapd->conf->radius_das_shared_secret_len;
745		das_conf.client_addr = &hapd->conf->radius_das_client_addr;
746		das_conf.time_window = hapd->conf->radius_das_time_window;
747		das_conf.require_event_timestamp =
748			hapd->conf->radius_das_require_event_timestamp;
749		das_conf.ctx = hapd;
750		das_conf.disconnect = hostapd_das_disconnect;
751		hapd->radius_das = radius_das_init(&das_conf);
752		if (hapd->radius_das == NULL) {
753			wpa_printf(MSG_ERROR, "RADIUS DAS initialization "
754				   "failed.");
755			return -1;
756		}
757	}
758#endif /* CONFIG_NO_RADIUS */
759
760	if (hostapd_acl_init(hapd)) {
761		wpa_printf(MSG_ERROR, "ACL initialization failed.");
762		return -1;
763	}
764	if (hostapd_init_wps(hapd, conf))
765		return -1;
766
767	if (authsrv_init(hapd) < 0)
768		return -1;
769
770	if (ieee802_1x_init(hapd)) {
771		wpa_printf(MSG_ERROR, "IEEE 802.1X initialization failed.");
772		return -1;
773	}
774
775	if (hapd->conf->wpa && hostapd_setup_wpa(hapd))
776		return -1;
777
778	if (accounting_init(hapd)) {
779		wpa_printf(MSG_ERROR, "Accounting initialization failed.");
780		return -1;
781	}
782
783	if (hapd->conf->ieee802_11f &&
784	    (hapd->iapp = iapp_init(hapd, hapd->conf->iapp_iface)) == NULL) {
785		wpa_printf(MSG_ERROR, "IEEE 802.11F (IAPP) initialization "
786			   "failed.");
787		return -1;
788	}
789
790#ifdef CONFIG_INTERWORKING
791	if (gas_serv_init(hapd)) {
792		wpa_printf(MSG_ERROR, "GAS server initialization failed");
793		return -1;
794	}
795#endif /* CONFIG_INTERWORKING */
796
797	if (hapd->iface->interfaces &&
798	    hapd->iface->interfaces->ctrl_iface_init &&
799	    hapd->iface->interfaces->ctrl_iface_init(hapd)) {
800		wpa_printf(MSG_ERROR, "Failed to setup control interface");
801		return -1;
802	}
803
804	if (!hostapd_drv_none(hapd) && vlan_init(hapd)) {
805		wpa_printf(MSG_ERROR, "VLAN initialization failed.");
806		return -1;
807	}
808
809	if (!hapd->conf->start_disabled)
810		ieee802_11_set_beacon(hapd);
811
812	if (hapd->wpa_auth && wpa_init_keys(hapd->wpa_auth) < 0)
813		return -1;
814
815	if (hapd->driver && hapd->driver->set_operstate)
816		hapd->driver->set_operstate(hapd->drv_priv, 1);
817
818	return 0;
819}
820
821
822static void hostapd_tx_queue_params(struct hostapd_iface *iface)
823{
824	struct hostapd_data *hapd = iface->bss[0];
825	int i;
826	struct hostapd_tx_queue_params *p;
827
828	for (i = 0; i < NUM_TX_QUEUES; i++) {
829		p = &iface->conf->tx_queue[i];
830
831		if (hostapd_set_tx_queue_params(hapd, i, p->aifs, p->cwmin,
832						p->cwmax, p->burst)) {
833			wpa_printf(MSG_DEBUG, "Failed to set TX queue "
834				   "parameters for queue %d.", i);
835			/* Continue anyway */
836		}
837	}
838}
839
840
841static int hostapd_set_acl_list(struct hostapd_data *hapd,
842				struct mac_acl_entry *mac_acl,
843				int n_entries, u8 accept_acl)
844{
845	struct hostapd_acl_params *acl_params;
846	int i, err;
847
848	acl_params = os_zalloc(sizeof(*acl_params) +
849			       (n_entries * sizeof(acl_params->mac_acl[0])));
850	if (!acl_params)
851		return -ENOMEM;
852
853	for (i = 0; i < n_entries; i++)
854		os_memcpy(acl_params->mac_acl[i].addr, mac_acl[i].addr,
855			  ETH_ALEN);
856
857	acl_params->acl_policy = accept_acl;
858	acl_params->num_mac_acl = n_entries;
859
860	err = hostapd_drv_set_acl(hapd, acl_params);
861
862	os_free(acl_params);
863
864	return err;
865}
866
867
868static void hostapd_set_acl(struct hostapd_data *hapd)
869{
870	struct hostapd_config *conf = hapd->iconf;
871	int err;
872	u8 accept_acl;
873
874	if (hapd->iface->drv_max_acl_mac_addrs == 0)
875		return;
876	if (!(conf->bss->num_accept_mac || conf->bss->num_deny_mac))
877		return;
878
879	if (conf->bss->macaddr_acl == DENY_UNLESS_ACCEPTED) {
880		if (conf->bss->num_accept_mac) {
881			accept_acl = 1;
882			err = hostapd_set_acl_list(hapd, conf->bss->accept_mac,
883						   conf->bss->num_accept_mac,
884						   accept_acl);
885			if (err) {
886				wpa_printf(MSG_DEBUG, "Failed to set accept acl");
887				return;
888			}
889		} else {
890			wpa_printf(MSG_DEBUG, "Mismatch between ACL Policy & Accept/deny lists file");
891		}
892	} else if (conf->bss->macaddr_acl == ACCEPT_UNLESS_DENIED) {
893		if (conf->bss->num_deny_mac) {
894			accept_acl = 0;
895			err = hostapd_set_acl_list(hapd, conf->bss->deny_mac,
896						   conf->bss->num_deny_mac,
897						   accept_acl);
898			if (err) {
899				wpa_printf(MSG_DEBUG, "Failed to set deny acl");
900				return;
901			}
902		} else {
903			wpa_printf(MSG_DEBUG, "Mismatch between ACL Policy & Accept/deny lists file");
904		}
905	}
906}
907
908
909static int setup_interface(struct hostapd_iface *iface)
910{
911	struct hostapd_data *hapd = iface->bss[0];
912	size_t i;
913	char country[4];
914
915	/*
916	 * Make sure that all BSSes get configured with a pointer to the same
917	 * driver interface.
918	 */
919	for (i = 1; i < iface->num_bss; i++) {
920		iface->bss[i]->driver = hapd->driver;
921		iface->bss[i]->drv_priv = hapd->drv_priv;
922	}
923
924	if (hostapd_validate_bssid_configuration(iface))
925		return -1;
926
927	if (hapd->iconf->country[0] && hapd->iconf->country[1]) {
928		os_memcpy(country, hapd->iconf->country, 3);
929		country[3] = '\0';
930		if (hostapd_set_country(hapd, country) < 0) {
931			wpa_printf(MSG_ERROR, "Failed to set country code");
932			return -1;
933		}
934	}
935
936	if (hostapd_get_hw_features(iface)) {
937		/* Not all drivers support this yet, so continue without hw
938		 * feature data. */
939	} else {
940		int ret = hostapd_select_hw_mode(iface);
941		if (ret < 0) {
942			wpa_printf(MSG_ERROR, "Could not select hw_mode and "
943				   "channel. (%d)", ret);
944			return -1;
945		}
946		ret = hostapd_check_ht_capab(iface);
947		if (ret < 0)
948			return -1;
949		if (ret == 1) {
950			wpa_printf(MSG_DEBUG, "Interface initialization will "
951				   "be completed in a callback");
952			return 0;
953		}
954	}
955	return hostapd_setup_interface_complete(iface, 0);
956}
957
958
959int hostapd_setup_interface_complete(struct hostapd_iface *iface, int err)
960{
961	struct hostapd_data *hapd = iface->bss[0];
962	size_t j;
963	u8 *prev_addr;
964
965	if (err) {
966		wpa_printf(MSG_ERROR, "Interface initialization failed");
967		eloop_terminate();
968		return -1;
969	}
970
971	wpa_printf(MSG_DEBUG, "Completing interface initialization");
972	if (hapd->iconf->channel) {
973		iface->freq = hostapd_hw_get_freq(hapd, hapd->iconf->channel);
974		wpa_printf(MSG_DEBUG, "Mode: %s  Channel: %d  "
975			   "Frequency: %d MHz",
976			   hostapd_hw_mode_txt(hapd->iconf->hw_mode),
977			   hapd->iconf->channel, iface->freq);
978
979		if (hostapd_set_freq(hapd, hapd->iconf->hw_mode, iface->freq,
980				     hapd->iconf->channel,
981				     hapd->iconf->ieee80211n,
982				     hapd->iconf->ieee80211ac,
983				     hapd->iconf->secondary_channel,
984				     hapd->iconf->vht_oper_chwidth,
985				     hapd->iconf->vht_oper_centr_freq_seg0_idx,
986				     hapd->iconf->vht_oper_centr_freq_seg1_idx)) {
987			wpa_printf(MSG_ERROR, "Could not set channel for "
988				   "kernel driver");
989			return -1;
990		}
991	}
992
993	if (iface->current_mode) {
994		if (hostapd_prepare_rates(iface, iface->current_mode)) {
995			wpa_printf(MSG_ERROR, "Failed to prepare rates "
996				   "table.");
997			hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
998				       HOSTAPD_LEVEL_WARNING,
999				       "Failed to prepare rates table.");
1000			return -1;
1001		}
1002	}
1003
1004	if (hapd->iconf->rts_threshold > -1 &&
1005	    hostapd_set_rts(hapd, hapd->iconf->rts_threshold)) {
1006		wpa_printf(MSG_ERROR, "Could not set RTS threshold for "
1007			   "kernel driver");
1008		return -1;
1009	}
1010
1011	if (hapd->iconf->fragm_threshold > -1 &&
1012	    hostapd_set_frag(hapd, hapd->iconf->fragm_threshold)) {
1013		wpa_printf(MSG_ERROR, "Could not set fragmentation threshold "
1014			   "for kernel driver");
1015		return -1;
1016	}
1017
1018	prev_addr = hapd->own_addr;
1019
1020	for (j = 0; j < iface->num_bss; j++) {
1021		hapd = iface->bss[j];
1022		if (j)
1023			os_memcpy(hapd->own_addr, prev_addr, ETH_ALEN);
1024		if (hostapd_setup_bss(hapd, j == 0))
1025			return -1;
1026		if (hostapd_mac_comp_empty(hapd->conf->bssid) == 0)
1027			prev_addr = hapd->own_addr;
1028	}
1029
1030	hostapd_tx_queue_params(iface);
1031
1032	ap_list_init(iface);
1033
1034	hostapd_set_acl(hapd);
1035
1036	if (hostapd_driver_commit(hapd) < 0) {
1037		wpa_printf(MSG_ERROR, "%s: Failed to commit driver "
1038			   "configuration", __func__);
1039		return -1;
1040	}
1041
1042	/*
1043	 * WPS UPnP module can be initialized only when the "upnp_iface" is up.
1044	 * If "interface" and "upnp_iface" are the same (e.g., non-bridge
1045	 * mode), the interface is up only after driver_commit, so initialize
1046	 * WPS after driver_commit.
1047	 */
1048	for (j = 0; j < iface->num_bss; j++) {
1049		if (hostapd_init_wps_complete(iface->bss[j]))
1050			return -1;
1051	}
1052
1053	if (hapd->setup_complete_cb)
1054		hapd->setup_complete_cb(hapd->setup_complete_cb_ctx);
1055
1056	wpa_printf(MSG_DEBUG, "%s: Setup of interface done.",
1057		   iface->bss[0]->conf->iface);
1058
1059	return 0;
1060}
1061
1062
1063/**
1064 * hostapd_setup_interface - Setup of an interface
1065 * @iface: Pointer to interface data.
1066 * Returns: 0 on success, -1 on failure
1067 *
1068 * Initializes the driver interface, validates the configuration,
1069 * and sets driver parameters based on the configuration.
1070 * Flushes old stations, sets the channel, encryption,
1071 * beacons, and WDS links based on the configuration.
1072 */
1073int hostapd_setup_interface(struct hostapd_iface *iface)
1074{
1075	int ret;
1076
1077	ret = setup_interface(iface);
1078	if (ret) {
1079		wpa_printf(MSG_ERROR, "%s: Unable to setup interface.",
1080			   iface->bss[0]->conf->iface);
1081		return -1;
1082	}
1083
1084	return 0;
1085}
1086
1087
1088/**
1089 * hostapd_alloc_bss_data - Allocate and initialize per-BSS data
1090 * @hapd_iface: Pointer to interface data
1091 * @conf: Pointer to per-interface configuration
1092 * @bss: Pointer to per-BSS configuration for this BSS
1093 * Returns: Pointer to allocated BSS data
1094 *
1095 * This function is used to allocate per-BSS data structure. This data will be
1096 * freed after hostapd_cleanup() is called for it during interface
1097 * deinitialization.
1098 */
1099struct hostapd_data *
1100hostapd_alloc_bss_data(struct hostapd_iface *hapd_iface,
1101		       struct hostapd_config *conf,
1102		       struct hostapd_bss_config *bss)
1103{
1104	struct hostapd_data *hapd;
1105
1106	hapd = os_zalloc(sizeof(*hapd));
1107	if (hapd == NULL)
1108		return NULL;
1109
1110	hapd->new_assoc_sta_cb = hostapd_new_assoc_sta;
1111	hapd->iconf = conf;
1112	hapd->conf = bss;
1113	hapd->iface = hapd_iface;
1114	hapd->driver = hapd->iconf->driver;
1115	hapd->ctrl_sock = -1;
1116
1117	return hapd;
1118}
1119
1120
1121void hostapd_interface_deinit(struct hostapd_iface *iface)
1122{
1123	size_t j;
1124
1125	if (iface == NULL)
1126		return;
1127
1128	hostapd_cleanup_iface_pre(iface);
1129	for (j = 0; j < iface->num_bss; j++) {
1130		struct hostapd_data *hapd = iface->bss[j];
1131		hostapd_free_stas(hapd);
1132		hostapd_flush_old_stations(hapd, WLAN_REASON_DEAUTH_LEAVING);
1133		hostapd_clear_wep(hapd);
1134		hostapd_cleanup(hapd);
1135	}
1136}
1137
1138
1139void hostapd_interface_free(struct hostapd_iface *iface)
1140{
1141	size_t j;
1142	for (j = 0; j < iface->num_bss; j++)
1143		os_free(iface->bss[j]);
1144	hostapd_cleanup_iface(iface);
1145}
1146
1147
1148#ifdef HOSTAPD
1149
1150void hostapd_interface_deinit_free(struct hostapd_iface *iface)
1151{
1152	const struct wpa_driver_ops *driver;
1153	void *drv_priv;
1154	if (iface == NULL)
1155		return;
1156	driver = iface->bss[0]->driver;
1157	drv_priv = iface->bss[0]->drv_priv;
1158	hostapd_interface_deinit(iface);
1159	if (driver && driver->hapd_deinit && drv_priv)
1160		driver->hapd_deinit(drv_priv);
1161	hostapd_interface_free(iface);
1162}
1163
1164
1165int hostapd_enable_iface(struct hostapd_iface *hapd_iface)
1166{
1167	if (hapd_iface->bss[0]->drv_priv != NULL) {
1168		wpa_printf(MSG_ERROR, "Interface %s already enabled",
1169			   hapd_iface->conf->bss[0].iface);
1170		return -1;
1171	}
1172
1173	wpa_printf(MSG_DEBUG, "Enable interface %s",
1174		   hapd_iface->conf->bss[0].iface);
1175
1176	if (hapd_iface->interfaces == NULL ||
1177	    hapd_iface->interfaces->driver_init == NULL ||
1178	    hapd_iface->interfaces->driver_init(hapd_iface) ||
1179	    hostapd_setup_interface(hapd_iface)) {
1180		hostapd_interface_deinit_free(hapd_iface);
1181		return -1;
1182	}
1183	return 0;
1184}
1185
1186
1187int hostapd_reload_iface(struct hostapd_iface *hapd_iface)
1188{
1189	size_t j;
1190
1191	wpa_printf(MSG_DEBUG, "Reload interface %s",
1192		   hapd_iface->conf->bss[0].iface);
1193	for (j = 0; j < hapd_iface->num_bss; j++) {
1194		hostapd_flush_old_stations(hapd_iface->bss[j],
1195					   WLAN_REASON_PREV_AUTH_NOT_VALID);
1196
1197#ifndef CONFIG_NO_RADIUS
1198		/* TODO: update dynamic data based on changed configuration
1199		 * items (e.g., open/close sockets, etc.) */
1200		radius_client_flush(hapd_iface->bss[j]->radius, 0);
1201#endif  /* CONFIG_NO_RADIUS */
1202
1203		hostapd_reload_bss(hapd_iface->bss[j]);
1204	}
1205	return 0;
1206}
1207
1208
1209int hostapd_disable_iface(struct hostapd_iface *hapd_iface)
1210{
1211	size_t j;
1212	struct hostapd_bss_config *bss;
1213	const struct wpa_driver_ops *driver;
1214	void *drv_priv;
1215
1216	if (hapd_iface == NULL)
1217		return -1;
1218	bss = hapd_iface->bss[0]->conf;
1219	driver = hapd_iface->bss[0]->driver;
1220	drv_priv = hapd_iface->bss[0]->drv_priv;
1221
1222	/* whatever hostapd_interface_deinit does */
1223	for (j = 0; j < hapd_iface->num_bss; j++) {
1224		struct hostapd_data *hapd = hapd_iface->bss[j];
1225		hostapd_free_stas(hapd);
1226		hostapd_flush_old_stations(hapd, WLAN_REASON_DEAUTH_LEAVING);
1227		hostapd_clear_wep(hapd);
1228		hostapd_free_hapd_data(hapd);
1229	}
1230
1231	if (driver && driver->hapd_deinit && drv_priv) {
1232		driver->hapd_deinit(drv_priv);
1233		hapd_iface->bss[0]->drv_priv = NULL;
1234	}
1235
1236	/* From hostapd_cleanup_iface: These were initialized in
1237	 * hostapd_setup_interface and hostapd_setup_interface_complete
1238	 */
1239	hostapd_cleanup_iface_partial(hapd_iface);
1240	bss->wpa = 0;
1241	bss->wpa_key_mgmt = -1;
1242	bss->wpa_pairwise = -1;
1243
1244	wpa_printf(MSG_DEBUG, "Interface %s disabled", bss->iface);
1245	return 0;
1246}
1247
1248
1249static struct hostapd_iface *
1250hostapd_iface_alloc(struct hapd_interfaces *interfaces)
1251{
1252	struct hostapd_iface **iface, *hapd_iface;
1253
1254	iface = os_realloc_array(interfaces->iface, interfaces->count + 1,
1255				 sizeof(struct hostapd_iface *));
1256	if (iface == NULL)
1257		return NULL;
1258	interfaces->iface = iface;
1259	hapd_iface = interfaces->iface[interfaces->count] =
1260		os_zalloc(sizeof(*hapd_iface));
1261	if (hapd_iface == NULL) {
1262		wpa_printf(MSG_ERROR, "%s: Failed to allocate memory for "
1263			   "the interface", __func__);
1264		return NULL;
1265	}
1266	interfaces->count++;
1267	hapd_iface->interfaces = interfaces;
1268
1269	return hapd_iface;
1270}
1271
1272
1273static struct hostapd_config *
1274hostapd_config_alloc(struct hapd_interfaces *interfaces, const char *ifname,
1275		     const char *ctrl_iface)
1276{
1277	struct hostapd_bss_config *bss;
1278	struct hostapd_config *conf;
1279
1280	/* Allocates memory for bss and conf */
1281	conf = hostapd_config_defaults();
1282	if (conf == NULL) {
1283		 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory for "
1284				"configuration", __func__);
1285		return NULL;
1286	}
1287
1288	conf->driver = wpa_drivers[0];
1289	if (conf->driver == NULL) {
1290		wpa_printf(MSG_ERROR, "No driver wrappers registered!");
1291		hostapd_config_free(conf);
1292		return NULL;
1293	}
1294
1295	bss = conf->last_bss = conf->bss;
1296
1297	os_strlcpy(bss->iface, ifname, sizeof(bss->iface));
1298	bss->ctrl_interface = os_strdup(ctrl_iface);
1299	if (bss->ctrl_interface == NULL) {
1300		hostapd_config_free(conf);
1301		return NULL;
1302	}
1303
1304	/* Reading configuration file skipped, will be done in SET!
1305	 * From reading the configuration till the end has to be done in
1306	 * SET
1307	 */
1308	return conf;
1309}
1310
1311
1312static struct hostapd_iface * hostapd_data_alloc(
1313	struct hapd_interfaces *interfaces, struct hostapd_config *conf)
1314{
1315	size_t i;
1316	struct hostapd_iface *hapd_iface =
1317		interfaces->iface[interfaces->count - 1];
1318	struct hostapd_data *hapd;
1319
1320	hapd_iface->conf = conf;
1321	hapd_iface->num_bss = conf->num_bss;
1322
1323	hapd_iface->bss = os_zalloc(conf->num_bss *
1324				    sizeof(struct hostapd_data *));
1325	if (hapd_iface->bss == NULL)
1326		return NULL;
1327
1328	for (i = 0; i < conf->num_bss; i++) {
1329		hapd = hapd_iface->bss[i] =
1330			hostapd_alloc_bss_data(hapd_iface, conf,
1331					       &conf->bss[i]);
1332		if (hapd == NULL)
1333			return NULL;
1334		hapd->msg_ctx = hapd;
1335	}
1336
1337	hapd_iface->interfaces = interfaces;
1338
1339	return hapd_iface;
1340}
1341
1342
1343int hostapd_add_iface(struct hapd_interfaces *interfaces, char *buf)
1344{
1345	struct hostapd_config *conf = NULL;
1346	struct hostapd_iface *hapd_iface = NULL;
1347	char *ptr;
1348	size_t i;
1349
1350	ptr = os_strchr(buf, ' ');
1351	if (ptr == NULL)
1352		return -1;
1353	*ptr++ = '\0';
1354
1355	for (i = 0; i < interfaces->count; i++) {
1356		if (!os_strcmp(interfaces->iface[i]->conf->bss[0].iface,
1357			       buf)) {
1358			wpa_printf(MSG_INFO, "Cannot add interface - it "
1359				   "already exists");
1360			return -1;
1361		}
1362	}
1363
1364	hapd_iface = hostapd_iface_alloc(interfaces);
1365	if (hapd_iface == NULL) {
1366		wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
1367			   "for interface", __func__);
1368		goto fail;
1369	}
1370
1371	conf = hostapd_config_alloc(interfaces, buf, ptr);
1372	if (conf == NULL) {
1373		wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
1374			   "for configuration", __func__);
1375		goto fail;
1376	}
1377
1378	hapd_iface = hostapd_data_alloc(interfaces, conf);
1379	if (hapd_iface == NULL) {
1380		wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
1381			   "for hostapd", __func__);
1382		goto fail;
1383	}
1384
1385	if (hapd_iface->interfaces &&
1386	    hapd_iface->interfaces->ctrl_iface_init &&
1387	    hapd_iface->interfaces->ctrl_iface_init(hapd_iface->bss[0])) {
1388		wpa_printf(MSG_ERROR, "%s: Failed to setup control "
1389			   "interface", __func__);
1390		goto fail;
1391	}
1392	wpa_printf(MSG_INFO, "Add interface '%s'", conf->bss[0].iface);
1393
1394	return 0;
1395
1396fail:
1397	if (conf)
1398		hostapd_config_free(conf);
1399	if (hapd_iface) {
1400		os_free(hapd_iface->bss[interfaces->count]);
1401		os_free(hapd_iface);
1402	}
1403	return -1;
1404}
1405
1406
1407int hostapd_remove_iface(struct hapd_interfaces *interfaces, char *buf)
1408{
1409	struct hostapd_iface *hapd_iface;
1410	size_t i, k = 0;
1411
1412	for (i = 0; i < interfaces->count; i++) {
1413		hapd_iface = interfaces->iface[i];
1414		if (hapd_iface == NULL)
1415			return -1;
1416		if (!os_strcmp(hapd_iface->conf->bss[0].iface, buf)) {
1417			wpa_printf(MSG_INFO, "Remove interface '%s'", buf);
1418			hostapd_interface_deinit_free(hapd_iface);
1419			k = i;
1420			while (k < (interfaces->count - 1)) {
1421				interfaces->iface[k] =
1422					interfaces->iface[k + 1];
1423				k++;
1424			}
1425			interfaces->count--;
1426			return 0;
1427		}
1428	}
1429	return -1;
1430}
1431
1432#endif /* HOSTAPD */
1433
1434
1435/**
1436 * hostapd_new_assoc_sta - Notify that a new station associated with the AP
1437 * @hapd: Pointer to BSS data
1438 * @sta: Pointer to the associated STA data
1439 * @reassoc: 1 to indicate this was a re-association; 0 = first association
1440 *
1441 * This function will be called whenever a station associates with the AP. It
1442 * can be called from ieee802_11.c for drivers that export MLME to hostapd and
1443 * from drv_callbacks.c based on driver events for drivers that take care of
1444 * management frames (IEEE 802.11 authentication and association) internally.
1445 */
1446void hostapd_new_assoc_sta(struct hostapd_data *hapd, struct sta_info *sta,
1447			   int reassoc)
1448{
1449	if (hapd->tkip_countermeasures) {
1450		hostapd_drv_sta_deauth(hapd, sta->addr,
1451				       WLAN_REASON_MICHAEL_MIC_FAILURE);
1452		return;
1453	}
1454
1455	hostapd_prune_associations(hapd, sta->addr);
1456
1457	/* IEEE 802.11F (IAPP) */
1458	if (hapd->conf->ieee802_11f)
1459		iapp_new_station(hapd->iapp, sta);
1460
1461#ifdef CONFIG_P2P
1462	if (sta->p2p_ie == NULL && !sta->no_p2p_set) {
1463		sta->no_p2p_set = 1;
1464		hapd->num_sta_no_p2p++;
1465		if (hapd->num_sta_no_p2p == 1)
1466			hostapd_p2p_non_p2p_sta_connected(hapd);
1467	}
1468#endif /* CONFIG_P2P */
1469
1470	/* Start accounting here, if IEEE 802.1X and WPA are not used.
1471	 * IEEE 802.1X/WPA code will start accounting after the station has
1472	 * been authorized. */
1473	if (!hapd->conf->ieee802_1x && !hapd->conf->wpa) {
1474		os_get_time(&sta->connected_time);
1475		accounting_sta_start(hapd, sta);
1476	}
1477
1478	/* Start IEEE 802.1X authentication process for new stations */
1479	ieee802_1x_new_station(hapd, sta);
1480	if (reassoc) {
1481		if (sta->auth_alg != WLAN_AUTH_FT &&
1482		    !(sta->flags & (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS)))
1483			wpa_auth_sm_event(sta->wpa_sm, WPA_REAUTH);
1484	} else
1485		wpa_auth_sta_associated(hapd->wpa_auth, sta->wpa_sm);
1486
1487	wpa_printf(MSG_DEBUG, "%s: reschedule ap_handle_timer timeout "
1488		   "for " MACSTR " (%d seconds - ap_max_inactivity)",
1489		   __func__, MAC2STR(sta->addr),
1490		   hapd->conf->ap_max_inactivity);
1491	eloop_cancel_timeout(ap_handle_timer, hapd, sta);
1492	eloop_register_timeout(hapd->conf->ap_max_inactivity, 0,
1493			       ap_handle_timer, hapd, sta);
1494}
1495