hostapd.c revision 8bae4138a0356709720a96f3e50b4d734e532c12
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	ieee802_11_set_beacon(hapd);
810
811	if (hapd->wpa_auth && wpa_init_keys(hapd->wpa_auth) < 0)
812		return -1;
813
814	if (hapd->driver && hapd->driver->set_operstate)
815		hapd->driver->set_operstate(hapd->drv_priv, 1);
816
817	return 0;
818}
819
820
821static void hostapd_tx_queue_params(struct hostapd_iface *iface)
822{
823	struct hostapd_data *hapd = iface->bss[0];
824	int i;
825	struct hostapd_tx_queue_params *p;
826
827	for (i = 0; i < NUM_TX_QUEUES; i++) {
828		p = &iface->conf->tx_queue[i];
829
830		if (hostapd_set_tx_queue_params(hapd, i, p->aifs, p->cwmin,
831						p->cwmax, p->burst)) {
832			wpa_printf(MSG_DEBUG, "Failed to set TX queue "
833				   "parameters for queue %d.", i);
834			/* Continue anyway */
835		}
836	}
837}
838
839
840static int hostapd_set_acl_list(struct hostapd_data *hapd,
841				struct mac_acl_entry *mac_acl,
842				int n_entries, u8 accept_acl)
843{
844	struct hostapd_acl_params *acl_params;
845	int i, err;
846
847	acl_params = os_zalloc(sizeof(*acl_params) +
848			       (n_entries * sizeof(acl_params->mac_acl[0])));
849	if (!acl_params)
850		return -ENOMEM;
851
852	for (i = 0; i < n_entries; i++)
853		os_memcpy(acl_params->mac_acl[i].addr, mac_acl[i].addr,
854			  ETH_ALEN);
855
856	acl_params->acl_policy = accept_acl;
857	acl_params->num_mac_acl = n_entries;
858
859	err = hostapd_drv_set_acl(hapd, acl_params);
860
861	os_free(acl_params);
862
863	return err;
864}
865
866
867static void hostapd_set_acl(struct hostapd_data *hapd)
868{
869	struct hostapd_config *conf = hapd->iconf;
870	int err;
871	u8 accept_acl;
872
873	if (hapd->iface->drv_max_acl_mac_addrs == 0)
874		return;
875	if (!(conf->bss->num_accept_mac || conf->bss->num_deny_mac))
876		return;
877
878	if (conf->bss->macaddr_acl == DENY_UNLESS_ACCEPTED) {
879		if (conf->bss->num_accept_mac) {
880			accept_acl = 1;
881			err = hostapd_set_acl_list(hapd, conf->bss->accept_mac,
882						   conf->bss->num_accept_mac,
883						   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		}
891	} else if (conf->bss->macaddr_acl == ACCEPT_UNLESS_DENIED) {
892		if (conf->bss->num_deny_mac) {
893			accept_acl = 0;
894			err = hostapd_set_acl_list(hapd, conf->bss->deny_mac,
895						   conf->bss->num_deny_mac,
896						   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
908static int setup_interface(struct hostapd_iface *iface)
909{
910	struct hostapd_data *hapd = iface->bss[0];
911	size_t i;
912	char country[4];
913
914	/*
915	 * Make sure that all BSSes get configured with a pointer to the same
916	 * driver interface.
917	 */
918	for (i = 1; i < iface->num_bss; i++) {
919		iface->bss[i]->driver = hapd->driver;
920		iface->bss[i]->drv_priv = hapd->drv_priv;
921	}
922
923	if (hostapd_validate_bssid_configuration(iface))
924		return -1;
925
926	if (hapd->iconf->country[0] && hapd->iconf->country[1]) {
927		os_memcpy(country, hapd->iconf->country, 3);
928		country[3] = '\0';
929		if (hostapd_set_country(hapd, country) < 0) {
930			wpa_printf(MSG_ERROR, "Failed to set country code");
931			return -1;
932		}
933	}
934
935	if (hostapd_get_hw_features(iface)) {
936		/* Not all drivers support this yet, so continue without hw
937		 * feature data. */
938	} else {
939		int ret = hostapd_select_hw_mode(iface);
940		if (ret < 0) {
941			wpa_printf(MSG_ERROR, "Could not select hw_mode and "
942				   "channel. (%d)", ret);
943			return -1;
944		}
945		ret = hostapd_check_ht_capab(iface);
946		if (ret < 0)
947			return -1;
948		if (ret == 1) {
949			wpa_printf(MSG_DEBUG, "Interface initialization will "
950				   "be completed in a callback");
951			return 0;
952		}
953	}
954	return hostapd_setup_interface_complete(iface, 0);
955}
956
957
958int hostapd_setup_interface_complete(struct hostapd_iface *iface, int err)
959{
960	struct hostapd_data *hapd = iface->bss[0];
961	size_t j;
962	u8 *prev_addr;
963
964	if (err) {
965		wpa_printf(MSG_ERROR, "Interface initialization failed");
966		eloop_terminate();
967		return -1;
968	}
969
970	wpa_printf(MSG_DEBUG, "Completing interface initialization");
971	if (hapd->iconf->channel) {
972		iface->freq = hostapd_hw_get_freq(hapd, hapd->iconf->channel);
973		wpa_printf(MSG_DEBUG, "Mode: %s  Channel: %d  "
974			   "Frequency: %d MHz",
975			   hostapd_hw_mode_txt(hapd->iconf->hw_mode),
976			   hapd->iconf->channel, iface->freq);
977
978		if (hostapd_set_freq(hapd, hapd->iconf->hw_mode, iface->freq,
979				     hapd->iconf->channel,
980				     hapd->iconf->ieee80211n,
981				     hapd->iconf->ieee80211ac,
982				     hapd->iconf->secondary_channel,
983				     hapd->iconf->vht_oper_chwidth,
984				     hapd->iconf->vht_oper_centr_freq_seg0_idx,
985				     hapd->iconf->vht_oper_centr_freq_seg1_idx)) {
986			wpa_printf(MSG_ERROR, "Could not set channel for "
987				   "kernel driver");
988			return -1;
989		}
990	}
991
992	if (iface->current_mode) {
993		if (hostapd_prepare_rates(iface, iface->current_mode)) {
994			wpa_printf(MSG_ERROR, "Failed to prepare rates "
995				   "table.");
996			hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
997				       HOSTAPD_LEVEL_WARNING,
998				       "Failed to prepare rates table.");
999			return -1;
1000		}
1001	}
1002
1003	if (hapd->iconf->rts_threshold > -1 &&
1004	    hostapd_set_rts(hapd, hapd->iconf->rts_threshold)) {
1005		wpa_printf(MSG_ERROR, "Could not set RTS threshold for "
1006			   "kernel driver");
1007		return -1;
1008	}
1009
1010	if (hapd->iconf->fragm_threshold > -1 &&
1011	    hostapd_set_frag(hapd, hapd->iconf->fragm_threshold)) {
1012		wpa_printf(MSG_ERROR, "Could not set fragmentation threshold "
1013			   "for kernel driver");
1014		return -1;
1015	}
1016
1017	prev_addr = hapd->own_addr;
1018
1019	for (j = 0; j < iface->num_bss; j++) {
1020		hapd = iface->bss[j];
1021		if (j)
1022			os_memcpy(hapd->own_addr, prev_addr, ETH_ALEN);
1023		if (hostapd_setup_bss(hapd, j == 0))
1024			return -1;
1025		if (hostapd_mac_comp_empty(hapd->conf->bssid) == 0)
1026			prev_addr = hapd->own_addr;
1027	}
1028
1029	hostapd_tx_queue_params(iface);
1030
1031	ap_list_init(iface);
1032
1033	hostapd_set_acl(hapd);
1034
1035	if (hostapd_driver_commit(hapd) < 0) {
1036		wpa_printf(MSG_ERROR, "%s: Failed to commit driver "
1037			   "configuration", __func__);
1038		return -1;
1039	}
1040
1041	/*
1042	 * WPS UPnP module can be initialized only when the "upnp_iface" is up.
1043	 * If "interface" and "upnp_iface" are the same (e.g., non-bridge
1044	 * mode), the interface is up only after driver_commit, so initialize
1045	 * WPS after driver_commit.
1046	 */
1047	for (j = 0; j < iface->num_bss; j++) {
1048		if (hostapd_init_wps_complete(iface->bss[j]))
1049			return -1;
1050	}
1051
1052	if (hapd->setup_complete_cb)
1053		hapd->setup_complete_cb(hapd->setup_complete_cb_ctx);
1054
1055	wpa_printf(MSG_DEBUG, "%s: Setup of interface done.",
1056		   iface->bss[0]->conf->iface);
1057
1058	return 0;
1059}
1060
1061
1062/**
1063 * hostapd_setup_interface - Setup of an interface
1064 * @iface: Pointer to interface data.
1065 * Returns: 0 on success, -1 on failure
1066 *
1067 * Initializes the driver interface, validates the configuration,
1068 * and sets driver parameters based on the configuration.
1069 * Flushes old stations, sets the channel, encryption,
1070 * beacons, and WDS links based on the configuration.
1071 */
1072int hostapd_setup_interface(struct hostapd_iface *iface)
1073{
1074	int ret;
1075
1076	ret = setup_interface(iface);
1077	if (ret) {
1078		wpa_printf(MSG_ERROR, "%s: Unable to setup interface.",
1079			   iface->bss[0]->conf->iface);
1080		return -1;
1081	}
1082
1083	return 0;
1084}
1085
1086
1087/**
1088 * hostapd_alloc_bss_data - Allocate and initialize per-BSS data
1089 * @hapd_iface: Pointer to interface data
1090 * @conf: Pointer to per-interface configuration
1091 * @bss: Pointer to per-BSS configuration for this BSS
1092 * Returns: Pointer to allocated BSS data
1093 *
1094 * This function is used to allocate per-BSS data structure. This data will be
1095 * freed after hostapd_cleanup() is called for it during interface
1096 * deinitialization.
1097 */
1098struct hostapd_data *
1099hostapd_alloc_bss_data(struct hostapd_iface *hapd_iface,
1100		       struct hostapd_config *conf,
1101		       struct hostapd_bss_config *bss)
1102{
1103	struct hostapd_data *hapd;
1104
1105	hapd = os_zalloc(sizeof(*hapd));
1106	if (hapd == NULL)
1107		return NULL;
1108
1109	hapd->new_assoc_sta_cb = hostapd_new_assoc_sta;
1110	hapd->iconf = conf;
1111	hapd->conf = bss;
1112	hapd->iface = hapd_iface;
1113	hapd->driver = hapd->iconf->driver;
1114	hapd->ctrl_sock = -1;
1115
1116	return hapd;
1117}
1118
1119
1120void hostapd_interface_deinit(struct hostapd_iface *iface)
1121{
1122	size_t j;
1123
1124	if (iface == NULL)
1125		return;
1126
1127	hostapd_cleanup_iface_pre(iface);
1128	for (j = 0; j < iface->num_bss; j++) {
1129		struct hostapd_data *hapd = iface->bss[j];
1130		hostapd_free_stas(hapd);
1131		hostapd_flush_old_stations(hapd, WLAN_REASON_DEAUTH_LEAVING);
1132		hostapd_clear_wep(hapd);
1133		hostapd_cleanup(hapd);
1134	}
1135}
1136
1137
1138void hostapd_interface_free(struct hostapd_iface *iface)
1139{
1140	size_t j;
1141	for (j = 0; j < iface->num_bss; j++)
1142		os_free(iface->bss[j]);
1143	hostapd_cleanup_iface(iface);
1144}
1145
1146
1147#ifdef HOSTAPD
1148
1149void hostapd_interface_deinit_free(struct hostapd_iface *iface)
1150{
1151	const struct wpa_driver_ops *driver;
1152	void *drv_priv;
1153	if (iface == NULL)
1154		return;
1155	driver = iface->bss[0]->driver;
1156	drv_priv = iface->bss[0]->drv_priv;
1157	hostapd_interface_deinit(iface);
1158	if (driver && driver->hapd_deinit && drv_priv)
1159		driver->hapd_deinit(drv_priv);
1160	hostapd_interface_free(iface);
1161}
1162
1163
1164int hostapd_enable_iface(struct hostapd_iface *hapd_iface)
1165{
1166	if (hapd_iface->bss[0]->drv_priv != NULL) {
1167		wpa_printf(MSG_ERROR, "Interface %s already enabled",
1168			   hapd_iface->conf->bss[0].iface);
1169		return -1;
1170	}
1171
1172	wpa_printf(MSG_DEBUG, "Enable interface %s",
1173		   hapd_iface->conf->bss[0].iface);
1174
1175	if (hapd_iface->interfaces == NULL ||
1176	    hapd_iface->interfaces->driver_init == NULL ||
1177	    hapd_iface->interfaces->driver_init(hapd_iface) ||
1178	    hostapd_setup_interface(hapd_iface)) {
1179		hostapd_interface_deinit_free(hapd_iface);
1180		return -1;
1181	}
1182	return 0;
1183}
1184
1185
1186int hostapd_reload_iface(struct hostapd_iface *hapd_iface)
1187{
1188	size_t j;
1189
1190	wpa_printf(MSG_DEBUG, "Reload interface %s",
1191		   hapd_iface->conf->bss[0].iface);
1192	for (j = 0; j < hapd_iface->num_bss; j++) {
1193		hostapd_flush_old_stations(hapd_iface->bss[j],
1194					   WLAN_REASON_PREV_AUTH_NOT_VALID);
1195
1196#ifndef CONFIG_NO_RADIUS
1197		/* TODO: update dynamic data based on changed configuration
1198		 * items (e.g., open/close sockets, etc.) */
1199		radius_client_flush(hapd_iface->bss[j]->radius, 0);
1200#endif  /* CONFIG_NO_RADIUS */
1201
1202		hostapd_reload_bss(hapd_iface->bss[j]);
1203	}
1204	return 0;
1205}
1206
1207
1208int hostapd_disable_iface(struct hostapd_iface *hapd_iface)
1209{
1210	size_t j;
1211	struct hostapd_bss_config *bss;
1212	const struct wpa_driver_ops *driver;
1213	void *drv_priv;
1214
1215	if (hapd_iface == NULL)
1216		return -1;
1217	bss = hapd_iface->bss[0]->conf;
1218	driver = hapd_iface->bss[0]->driver;
1219	drv_priv = hapd_iface->bss[0]->drv_priv;
1220
1221	/* whatever hostapd_interface_deinit does */
1222	for (j = 0; j < hapd_iface->num_bss; j++) {
1223		struct hostapd_data *hapd = hapd_iface->bss[j];
1224		hostapd_free_stas(hapd);
1225		hostapd_flush_old_stations(hapd, WLAN_REASON_DEAUTH_LEAVING);
1226		hostapd_clear_wep(hapd);
1227		hostapd_free_hapd_data(hapd);
1228	}
1229
1230	if (driver && driver->hapd_deinit && drv_priv) {
1231		driver->hapd_deinit(drv_priv);
1232		hapd_iface->bss[0]->drv_priv = NULL;
1233	}
1234
1235	/* From hostapd_cleanup_iface: These were initialized in
1236	 * hostapd_setup_interface and hostapd_setup_interface_complete
1237	 */
1238	hostapd_cleanup_iface_partial(hapd_iface);
1239	bss->wpa = 0;
1240	bss->wpa_key_mgmt = -1;
1241	bss->wpa_pairwise = -1;
1242
1243	wpa_printf(MSG_DEBUG, "Interface %s disabled", bss->iface);
1244	return 0;
1245}
1246
1247
1248static struct hostapd_iface *
1249hostapd_iface_alloc(struct hapd_interfaces *interfaces)
1250{
1251	struct hostapd_iface **iface, *hapd_iface;
1252
1253	iface = os_realloc_array(interfaces->iface, interfaces->count + 1,
1254				 sizeof(struct hostapd_iface *));
1255	if (iface == NULL)
1256		return NULL;
1257	interfaces->iface = iface;
1258	hapd_iface = interfaces->iface[interfaces->count] =
1259		os_zalloc(sizeof(*hapd_iface));
1260	if (hapd_iface == NULL) {
1261		wpa_printf(MSG_ERROR, "%s: Failed to allocate memory for "
1262			   "the interface", __func__);
1263		return NULL;
1264	}
1265	interfaces->count++;
1266	hapd_iface->interfaces = interfaces;
1267
1268	return hapd_iface;
1269}
1270
1271
1272static struct hostapd_config *
1273hostapd_config_alloc(struct hapd_interfaces *interfaces, const char *ifname,
1274		     const char *ctrl_iface)
1275{
1276	struct hostapd_bss_config *bss;
1277	struct hostapd_config *conf;
1278
1279	/* Allocates memory for bss and conf */
1280	conf = hostapd_config_defaults();
1281	if (conf == NULL) {
1282		 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory for "
1283				"configuration", __func__);
1284		return NULL;
1285	}
1286
1287	conf->driver = wpa_drivers[0];
1288	if (conf->driver == NULL) {
1289		wpa_printf(MSG_ERROR, "No driver wrappers registered!");
1290		hostapd_config_free(conf);
1291		return NULL;
1292	}
1293
1294	bss = conf->last_bss = conf->bss;
1295
1296	os_strlcpy(bss->iface, ifname, sizeof(bss->iface));
1297	bss->ctrl_interface = os_strdup(ctrl_iface);
1298	if (bss->ctrl_interface == NULL) {
1299		hostapd_config_free(conf);
1300		return NULL;
1301	}
1302
1303	/* Reading configuration file skipped, will be done in SET!
1304	 * From reading the configuration till the end has to be done in
1305	 * SET
1306	 */
1307	return conf;
1308}
1309
1310
1311static struct hostapd_iface * hostapd_data_alloc(
1312	struct hapd_interfaces *interfaces, struct hostapd_config *conf)
1313{
1314	size_t i;
1315	struct hostapd_iface *hapd_iface =
1316		interfaces->iface[interfaces->count - 1];
1317	struct hostapd_data *hapd;
1318
1319	hapd_iface->conf = conf;
1320	hapd_iface->num_bss = conf->num_bss;
1321
1322	hapd_iface->bss = os_zalloc(conf->num_bss *
1323				    sizeof(struct hostapd_data *));
1324	if (hapd_iface->bss == NULL)
1325		return NULL;
1326
1327	for (i = 0; i < conf->num_bss; i++) {
1328		hapd = hapd_iface->bss[i] =
1329			hostapd_alloc_bss_data(hapd_iface, conf,
1330					       &conf->bss[i]);
1331		if (hapd == NULL)
1332			return NULL;
1333		hapd->msg_ctx = hapd;
1334	}
1335
1336	hapd_iface->interfaces = interfaces;
1337
1338	return hapd_iface;
1339}
1340
1341
1342int hostapd_add_iface(struct hapd_interfaces *interfaces, char *buf)
1343{
1344	struct hostapd_config *conf = NULL;
1345	struct hostapd_iface *hapd_iface = NULL;
1346	char *ptr;
1347	size_t i;
1348
1349	ptr = os_strchr(buf, ' ');
1350	if (ptr == NULL)
1351		return -1;
1352	*ptr++ = '\0';
1353
1354	for (i = 0; i < interfaces->count; i++) {
1355		if (!os_strcmp(interfaces->iface[i]->conf->bss[0].iface,
1356			       buf)) {
1357			wpa_printf(MSG_INFO, "Cannot add interface - it "
1358				   "already exists");
1359			return -1;
1360		}
1361	}
1362
1363	hapd_iface = hostapd_iface_alloc(interfaces);
1364	if (hapd_iface == NULL) {
1365		wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
1366			   "for interface", __func__);
1367		goto fail;
1368	}
1369
1370	conf = hostapd_config_alloc(interfaces, buf, ptr);
1371	if (conf == NULL) {
1372		wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
1373			   "for configuration", __func__);
1374		goto fail;
1375	}
1376
1377	hapd_iface = hostapd_data_alloc(interfaces, conf);
1378	if (hapd_iface == NULL) {
1379		wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
1380			   "for hostapd", __func__);
1381		goto fail;
1382	}
1383
1384	if (hapd_iface->interfaces &&
1385	    hapd_iface->interfaces->ctrl_iface_init &&
1386	    hapd_iface->interfaces->ctrl_iface_init(hapd_iface->bss[0])) {
1387		wpa_printf(MSG_ERROR, "%s: Failed to setup control "
1388			   "interface", __func__);
1389		goto fail;
1390	}
1391	wpa_printf(MSG_INFO, "Add interface '%s'", conf->bss[0].iface);
1392
1393	return 0;
1394
1395fail:
1396	if (conf)
1397		hostapd_config_free(conf);
1398	if (hapd_iface) {
1399		os_free(hapd_iface->bss[interfaces->count]);
1400		os_free(hapd_iface);
1401	}
1402	return -1;
1403}
1404
1405
1406int hostapd_remove_iface(struct hapd_interfaces *interfaces, char *buf)
1407{
1408	struct hostapd_iface *hapd_iface;
1409	size_t i, k = 0;
1410
1411	for (i = 0; i < interfaces->count; i++) {
1412		hapd_iface = interfaces->iface[i];
1413		if (hapd_iface == NULL)
1414			return -1;
1415		if (!os_strcmp(hapd_iface->conf->bss[0].iface, buf)) {
1416			wpa_printf(MSG_INFO, "Remove interface '%s'", buf);
1417			hostapd_interface_deinit_free(hapd_iface);
1418			k = i;
1419			while (k < (interfaces->count - 1)) {
1420				interfaces->iface[k] =
1421					interfaces->iface[k + 1];
1422				k++;
1423			}
1424			interfaces->count--;
1425			return 0;
1426		}
1427	}
1428	return -1;
1429}
1430
1431#endif /* HOSTAPD */
1432
1433
1434/**
1435 * hostapd_new_assoc_sta - Notify that a new station associated with the AP
1436 * @hapd: Pointer to BSS data
1437 * @sta: Pointer to the associated STA data
1438 * @reassoc: 1 to indicate this was a re-association; 0 = first association
1439 *
1440 * This function will be called whenever a station associates with the AP. It
1441 * can be called from ieee802_11.c for drivers that export MLME to hostapd and
1442 * from drv_callbacks.c based on driver events for drivers that take care of
1443 * management frames (IEEE 802.11 authentication and association) internally.
1444 */
1445void hostapd_new_assoc_sta(struct hostapd_data *hapd, struct sta_info *sta,
1446			   int reassoc)
1447{
1448	if (hapd->tkip_countermeasures) {
1449		hostapd_drv_sta_deauth(hapd, sta->addr,
1450				       WLAN_REASON_MICHAEL_MIC_FAILURE);
1451		return;
1452	}
1453
1454	hostapd_prune_associations(hapd, sta->addr);
1455
1456	/* IEEE 802.11F (IAPP) */
1457	if (hapd->conf->ieee802_11f)
1458		iapp_new_station(hapd->iapp, sta);
1459
1460#ifdef CONFIG_P2P
1461	if (sta->p2p_ie == NULL && !sta->no_p2p_set) {
1462		sta->no_p2p_set = 1;
1463		hapd->num_sta_no_p2p++;
1464		if (hapd->num_sta_no_p2p == 1)
1465			hostapd_p2p_non_p2p_sta_connected(hapd);
1466	}
1467#endif /* CONFIG_P2P */
1468
1469	/* Start accounting here, if IEEE 802.1X and WPA are not used.
1470	 * IEEE 802.1X/WPA code will start accounting after the station has
1471	 * been authorized. */
1472	if (!hapd->conf->ieee802_1x && !hapd->conf->wpa) {
1473		os_get_time(&sta->connected_time);
1474		accounting_sta_start(hapd, sta);
1475	}
1476
1477	/* Start IEEE 802.1X authentication process for new stations */
1478	ieee802_1x_new_station(hapd, sta);
1479	if (reassoc) {
1480		if (sta->auth_alg != WLAN_AUTH_FT &&
1481		    !(sta->flags & (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS)))
1482			wpa_auth_sm_event(sta->wpa_sm, WPA_REAUTH);
1483	} else
1484		wpa_auth_sta_associated(hapd->wpa_auth, sta->wpa_sm);
1485
1486	wpa_printf(MSG_DEBUG, "%s: reschedule ap_handle_timer timeout "
1487		   "for " MACSTR " (%d seconds - ap_max_inactivity)",
1488		   __func__, MAC2STR(sta->addr),
1489		   hapd->conf->ap_max_inactivity);
1490	eloop_cancel_timeout(ap_handle_timer, hapd, sta);
1491	eloop_register_timeout(hapd->conf->ap_max_inactivity, 0,
1492			       ap_handle_timer, hapd, sta);
1493}
1494