hostapd.c revision 2ac5f6049e74103a8fe8e9c78b330020081d7df4
1/*
2 * hostapd / Initialization and configuration
3 * Copyright (c) 2002-2014, 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 "common/wpa_ctrl.h"
15#include "radius/radius_client.h"
16#include "radius/radius_das.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#include "dfs.h"
36
37
38static int hostapd_flush_old_stations(struct hostapd_data *hapd, u16 reason);
39static int hostapd_setup_encryption(char *iface, struct hostapd_data *hapd);
40static int hostapd_broadcast_wep_clear(struct hostapd_data *hapd);
41static int setup_interface2(struct hostapd_iface *iface);
42static void channel_list_update_timeout(void *eloop_ctx, void *timeout_ctx);
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	struct hostapd_ssid *ssid;
65
66#ifndef CONFIG_NO_RADIUS
67	radius_client_reconfig(hapd->radius, hapd->conf->radius);
68#endif /* CONFIG_NO_RADIUS */
69
70	ssid = &hapd->conf->ssid;
71	if (!ssid->wpa_psk_set && ssid->wpa_psk && !ssid->wpa_psk->next &&
72	    ssid->wpa_passphrase_set && ssid->wpa_passphrase) {
73		/*
74		 * Force PSK to be derived again since SSID or passphrase may
75		 * have changed.
76		 */
77		os_free(ssid->wpa_psk);
78		ssid->wpa_psk = NULL;
79	}
80	if (hostapd_setup_wpa_psk(hapd->conf)) {
81		wpa_printf(MSG_ERROR, "Failed to re-configure WPA PSK "
82			   "after reloading configuration");
83	}
84
85	if (hapd->conf->ieee802_1x || hapd->conf->wpa)
86		hostapd_set_drv_ieee8021x(hapd, hapd->conf->iface, 1);
87	else
88		hostapd_set_drv_ieee8021x(hapd, hapd->conf->iface, 0);
89
90	if ((hapd->conf->wpa || hapd->conf->osen) && hapd->wpa_auth == NULL) {
91		hostapd_setup_wpa(hapd);
92		if (hapd->wpa_auth)
93			wpa_init_keys(hapd->wpa_auth);
94	} else if (hapd->conf->wpa) {
95		const u8 *wpa_ie;
96		size_t wpa_ie_len;
97		hostapd_reconfig_wpa(hapd);
98		wpa_ie = wpa_auth_get_wpa_ie(hapd->wpa_auth, &wpa_ie_len);
99		if (hostapd_set_generic_elem(hapd, wpa_ie, wpa_ie_len))
100			wpa_printf(MSG_ERROR, "Failed to configure WPA IE for "
101				   "the kernel driver.");
102	} else if (hapd->wpa_auth) {
103		wpa_deinit(hapd->wpa_auth);
104		hapd->wpa_auth = NULL;
105		hostapd_set_privacy(hapd, 0);
106		hostapd_setup_encryption(hapd->conf->iface, hapd);
107		hostapd_set_generic_elem(hapd, (u8 *) "", 0);
108	}
109
110	ieee802_11_set_beacon(hapd);
111	hostapd_update_wps(hapd);
112
113	if (hapd->conf->ssid.ssid_set &&
114	    hostapd_set_ssid(hapd, hapd->conf->ssid.ssid,
115			     hapd->conf->ssid.ssid_len)) {
116		wpa_printf(MSG_ERROR, "Could not set SSID for kernel driver");
117		/* try to continue */
118	}
119	wpa_printf(MSG_DEBUG, "Reconfigured interface %s", hapd->conf->iface);
120}
121
122
123static void hostapd_clear_old(struct hostapd_iface *iface)
124{
125	size_t j;
126
127	/*
128	 * Deauthenticate all stations since the new configuration may not
129	 * allow them to use the BSS anymore.
130	 */
131	for (j = 0; j < iface->num_bss; j++) {
132		hostapd_flush_old_stations(iface->bss[j],
133					   WLAN_REASON_PREV_AUTH_NOT_VALID);
134		hostapd_broadcast_wep_clear(iface->bss[j]);
135
136#ifndef CONFIG_NO_RADIUS
137		/* TODO: update dynamic data based on changed configuration
138		 * items (e.g., open/close sockets, etc.) */
139		radius_client_flush(iface->bss[j]->radius, 0);
140#endif /* CONFIG_NO_RADIUS */
141	}
142}
143
144
145int hostapd_reload_config(struct hostapd_iface *iface)
146{
147	struct hostapd_data *hapd = iface->bss[0];
148	struct hostapd_config *newconf, *oldconf;
149	size_t j;
150
151	if (iface->config_fname == NULL) {
152		/* Only in-memory config in use - assume it has been updated */
153		hostapd_clear_old(iface);
154		for (j = 0; j < iface->num_bss; j++)
155			hostapd_reload_bss(iface->bss[j]);
156		return 0;
157	}
158
159	if (iface->interfaces == NULL ||
160	    iface->interfaces->config_read_cb == NULL)
161		return -1;
162	newconf = iface->interfaces->config_read_cb(iface->config_fname);
163	if (newconf == NULL)
164		return -1;
165
166	hostapd_clear_old(iface);
167
168	oldconf = hapd->iconf;
169	iface->conf = newconf;
170
171	for (j = 0; j < iface->num_bss; j++) {
172		hapd = iface->bss[j];
173		hapd->iconf = newconf;
174		hapd->conf = newconf->bss[j];
175		hostapd_reload_bss(hapd);
176	}
177
178	hostapd_config_free(oldconf);
179
180
181	return 0;
182}
183
184
185static void hostapd_broadcast_key_clear_iface(struct hostapd_data *hapd,
186					      char *ifname)
187{
188	int i;
189
190	for (i = 0; i < NUM_WEP_KEYS; i++) {
191		if (hostapd_drv_set_key(ifname, hapd, WPA_ALG_NONE, NULL, i,
192					0, NULL, 0, NULL, 0)) {
193			wpa_printf(MSG_DEBUG, "Failed to clear default "
194				   "encryption keys (ifname=%s keyidx=%d)",
195				   ifname, i);
196		}
197	}
198#ifdef CONFIG_IEEE80211W
199	if (hapd->conf->ieee80211w) {
200		for (i = NUM_WEP_KEYS; i < NUM_WEP_KEYS + 2; i++) {
201			if (hostapd_drv_set_key(ifname, hapd, WPA_ALG_NONE,
202						NULL, i, 0, NULL,
203						0, NULL, 0)) {
204				wpa_printf(MSG_DEBUG, "Failed to clear "
205					   "default mgmt encryption keys "
206					   "(ifname=%s keyidx=%d)", ifname, i);
207			}
208		}
209	}
210#endif /* CONFIG_IEEE80211W */
211}
212
213
214static int hostapd_broadcast_wep_clear(struct hostapd_data *hapd)
215{
216	hostapd_broadcast_key_clear_iface(hapd, hapd->conf->iface);
217	return 0;
218}
219
220
221static int hostapd_broadcast_wep_set(struct hostapd_data *hapd)
222{
223	int errors = 0, idx;
224	struct hostapd_ssid *ssid = &hapd->conf->ssid;
225
226	idx = ssid->wep.idx;
227	if (ssid->wep.default_len &&
228	    hostapd_drv_set_key(hapd->conf->iface,
229				hapd, WPA_ALG_WEP, broadcast_ether_addr, idx,
230				1, NULL, 0, ssid->wep.key[idx],
231				ssid->wep.len[idx])) {
232		wpa_printf(MSG_WARNING, "Could not set WEP encryption.");
233		errors++;
234	}
235
236	return errors;
237}
238
239
240static void hostapd_free_hapd_data(struct hostapd_data *hapd)
241{
242	if (!hapd->started) {
243		wpa_printf(MSG_ERROR, "%s: Interface %s wasn't started",
244			   __func__, hapd->conf->iface);
245		return;
246	}
247	hapd->started = 0;
248
249	wpa_printf(MSG_DEBUG, "%s(%s)", __func__, hapd->conf->iface);
250	iapp_deinit(hapd->iapp);
251	hapd->iapp = NULL;
252	accounting_deinit(hapd);
253	hostapd_deinit_wpa(hapd);
254	vlan_deinit(hapd);
255	hostapd_acl_deinit(hapd);
256#ifndef CONFIG_NO_RADIUS
257	radius_client_deinit(hapd->radius);
258	hapd->radius = NULL;
259	radius_das_deinit(hapd->radius_das);
260	hapd->radius_das = NULL;
261#endif /* CONFIG_NO_RADIUS */
262
263	hostapd_deinit_wps(hapd);
264
265	authsrv_deinit(hapd);
266
267	if (hapd->interface_added &&
268	    hostapd_if_remove(hapd, WPA_IF_AP_BSS, hapd->conf->iface)) {
269		wpa_printf(MSG_WARNING, "Failed to remove BSS interface %s",
270			   hapd->conf->iface);
271	}
272
273	os_free(hapd->probereq_cb);
274	hapd->probereq_cb = NULL;
275
276#ifdef CONFIG_P2P
277	wpabuf_free(hapd->p2p_beacon_ie);
278	hapd->p2p_beacon_ie = NULL;
279	wpabuf_free(hapd->p2p_probe_resp_ie);
280	hapd->p2p_probe_resp_ie = NULL;
281#endif /* CONFIG_P2P */
282
283	wpabuf_free(hapd->time_adv);
284
285#ifdef CONFIG_INTERWORKING
286	gas_serv_deinit(hapd);
287#endif /* CONFIG_INTERWORKING */
288
289#ifdef CONFIG_SQLITE
290	os_free(hapd->tmp_eap_user.identity);
291	os_free(hapd->tmp_eap_user.password);
292#endif /* CONFIG_SQLITE */
293}
294
295
296/**
297 * hostapd_cleanup - Per-BSS cleanup (deinitialization)
298 * @hapd: Pointer to BSS data
299 *
300 * This function is used to free all per-BSS data structures and resources.
301 * Most of the modules that are initialized in hostapd_setup_bss() are
302 * deinitialized here.
303 */
304static void hostapd_cleanup(struct hostapd_data *hapd)
305{
306	wpa_printf(MSG_DEBUG, "%s(hapd=%p (%s))", __func__, hapd,
307		   hapd->conf->iface);
308	if (hapd->iface->interfaces &&
309	    hapd->iface->interfaces->ctrl_iface_deinit)
310		hapd->iface->interfaces->ctrl_iface_deinit(hapd);
311	hostapd_free_hapd_data(hapd);
312}
313
314
315static void hostapd_cleanup_iface_partial(struct hostapd_iface *iface)
316{
317	wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
318	hostapd_free_hw_features(iface->hw_features, iface->num_hw_features);
319	iface->hw_features = NULL;
320	os_free(iface->current_rates);
321	iface->current_rates = NULL;
322	os_free(iface->basic_rates);
323	iface->basic_rates = NULL;
324	ap_list_deinit(iface);
325}
326
327
328/**
329 * hostapd_cleanup_iface - Complete per-interface cleanup
330 * @iface: Pointer to interface data
331 *
332 * This function is called after per-BSS data structures are deinitialized
333 * with hostapd_cleanup().
334 */
335static void hostapd_cleanup_iface(struct hostapd_iface *iface)
336{
337	wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
338	eloop_cancel_timeout(channel_list_update_timeout, iface, NULL);
339
340	hostapd_cleanup_iface_partial(iface);
341	hostapd_config_free(iface->conf);
342	iface->conf = NULL;
343
344	os_free(iface->config_fname);
345	os_free(iface->bss);
346	wpa_printf(MSG_DEBUG, "%s: free iface=%p", __func__, iface);
347	os_free(iface);
348}
349
350
351static void hostapd_clear_wep(struct hostapd_data *hapd)
352{
353	if (hapd->drv_priv && !hapd->iface->driver_ap_teardown) {
354		hostapd_set_privacy(hapd, 0);
355		hostapd_broadcast_wep_clear(hapd);
356	}
357}
358
359
360static int hostapd_setup_encryption(char *iface, struct hostapd_data *hapd)
361{
362	int i;
363
364	hostapd_broadcast_wep_set(hapd);
365
366	if (hapd->conf->ssid.wep.default_len) {
367		hostapd_set_privacy(hapd, 1);
368		return 0;
369	}
370
371	/*
372	 * When IEEE 802.1X is not enabled, the driver may need to know how to
373	 * set authentication algorithms for static WEP.
374	 */
375	hostapd_drv_set_authmode(hapd, hapd->conf->auth_algs);
376
377	for (i = 0; i < 4; i++) {
378		if (hapd->conf->ssid.wep.key[i] &&
379		    hostapd_drv_set_key(iface, hapd, WPA_ALG_WEP, NULL, i,
380					i == hapd->conf->ssid.wep.idx, NULL, 0,
381					hapd->conf->ssid.wep.key[i],
382					hapd->conf->ssid.wep.len[i])) {
383			wpa_printf(MSG_WARNING, "Could not set WEP "
384				   "encryption.");
385			return -1;
386		}
387		if (hapd->conf->ssid.wep.key[i] &&
388		    i == hapd->conf->ssid.wep.idx)
389			hostapd_set_privacy(hapd, 1);
390	}
391
392	return 0;
393}
394
395
396static int hostapd_flush_old_stations(struct hostapd_data *hapd, u16 reason)
397{
398	int ret = 0;
399	u8 addr[ETH_ALEN];
400
401	if (hostapd_drv_none(hapd) || hapd->drv_priv == NULL)
402		return 0;
403
404	if (!hapd->iface->driver_ap_teardown) {
405		wpa_dbg(hapd->msg_ctx, MSG_DEBUG,
406			"Flushing old station entries");
407
408		if (hostapd_flush(hapd)) {
409			wpa_msg(hapd->msg_ctx, MSG_WARNING,
410				"Could not connect to kernel driver");
411			ret = -1;
412		}
413	}
414	wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "Deauthenticate all stations");
415	os_memset(addr, 0xff, ETH_ALEN);
416	hostapd_drv_sta_deauth(hapd, addr, reason);
417	hostapd_free_stas(hapd);
418
419	return ret;
420}
421
422
423/**
424 * hostapd_validate_bssid_configuration - Validate BSSID configuration
425 * @iface: Pointer to interface data
426 * Returns: 0 on success, -1 on failure
427 *
428 * This function is used to validate that the configured BSSIDs are valid.
429 */
430static int hostapd_validate_bssid_configuration(struct hostapd_iface *iface)
431{
432	u8 mask[ETH_ALEN] = { 0 };
433	struct hostapd_data *hapd = iface->bss[0];
434	unsigned int i = iface->conf->num_bss, bits = 0, j;
435	int auto_addr = 0;
436
437	if (hostapd_drv_none(hapd))
438		return 0;
439
440	/* Generate BSSID mask that is large enough to cover the BSSIDs. */
441
442	/* Determine the bits necessary to cover the number of BSSIDs. */
443	for (i--; i; i >>= 1)
444		bits++;
445
446	/* Determine the bits necessary to any configured BSSIDs,
447	   if they are higher than the number of BSSIDs. */
448	for (j = 0; j < iface->conf->num_bss; j++) {
449		if (hostapd_mac_comp_empty(iface->conf->bss[j]->bssid) == 0) {
450			if (j)
451				auto_addr++;
452			continue;
453		}
454
455		for (i = 0; i < ETH_ALEN; i++) {
456			mask[i] |=
457				iface->conf->bss[j]->bssid[i] ^
458				hapd->own_addr[i];
459		}
460	}
461
462	if (!auto_addr)
463		goto skip_mask_ext;
464
465	for (i = 0; i < ETH_ALEN && mask[i] == 0; i++)
466		;
467	j = 0;
468	if (i < ETH_ALEN) {
469		j = (5 - i) * 8;
470
471		while (mask[i] != 0) {
472			mask[i] >>= 1;
473			j++;
474		}
475	}
476
477	if (bits < j)
478		bits = j;
479
480	if (bits > 40) {
481		wpa_printf(MSG_ERROR, "Too many bits in the BSSID mask (%u)",
482			   bits);
483		return -1;
484	}
485
486	os_memset(mask, 0xff, ETH_ALEN);
487	j = bits / 8;
488	for (i = 5; i > 5 - j; i--)
489		mask[i] = 0;
490	j = bits % 8;
491	while (j--)
492		mask[i] <<= 1;
493
494skip_mask_ext:
495	wpa_printf(MSG_DEBUG, "BSS count %lu, BSSID mask " MACSTR " (%d bits)",
496		   (unsigned long) iface->conf->num_bss, MAC2STR(mask), bits);
497
498	if (!auto_addr)
499		return 0;
500
501	for (i = 0; i < ETH_ALEN; i++) {
502		if ((hapd->own_addr[i] & mask[i]) != hapd->own_addr[i]) {
503			wpa_printf(MSG_ERROR, "Invalid BSSID mask " MACSTR
504				   " for start address " MACSTR ".",
505				   MAC2STR(mask), MAC2STR(hapd->own_addr));
506			wpa_printf(MSG_ERROR, "Start address must be the "
507				   "first address in the block (i.e., addr "
508				   "AND mask == addr).");
509			return -1;
510		}
511	}
512
513	return 0;
514}
515
516
517static int mac_in_conf(struct hostapd_config *conf, const void *a)
518{
519	size_t i;
520
521	for (i = 0; i < conf->num_bss; i++) {
522		if (hostapd_mac_comp(conf->bss[i]->bssid, a) == 0) {
523			return 1;
524		}
525	}
526
527	return 0;
528}
529
530
531#ifndef CONFIG_NO_RADIUS
532
533static int hostapd_das_nas_mismatch(struct hostapd_data *hapd,
534				    struct radius_das_attrs *attr)
535{
536	if (attr->nas_identifier &&
537	    (!hapd->conf->nas_identifier ||
538	     os_strlen(hapd->conf->nas_identifier) !=
539	     attr->nas_identifier_len ||
540	     os_memcmp(hapd->conf->nas_identifier, attr->nas_identifier,
541		       attr->nas_identifier_len) != 0)) {
542		wpa_printf(MSG_DEBUG, "RADIUS DAS: NAS-Identifier mismatch");
543		return 1;
544	}
545
546	if (attr->nas_ip_addr &&
547	    (hapd->conf->own_ip_addr.af != AF_INET ||
548	     os_memcmp(&hapd->conf->own_ip_addr.u.v4, attr->nas_ip_addr, 4) !=
549	     0)) {
550		wpa_printf(MSG_DEBUG, "RADIUS DAS: NAS-IP-Address mismatch");
551		return 1;
552	}
553
554#ifdef CONFIG_IPV6
555	if (attr->nas_ipv6_addr &&
556	    (hapd->conf->own_ip_addr.af != AF_INET6 ||
557	     os_memcmp(&hapd->conf->own_ip_addr.u.v6, attr->nas_ipv6_addr, 16)
558	     != 0)) {
559		wpa_printf(MSG_DEBUG, "RADIUS DAS: NAS-IPv6-Address mismatch");
560		return 1;
561	}
562#endif /* CONFIG_IPV6 */
563
564	return 0;
565}
566
567
568static struct sta_info * hostapd_das_find_sta(struct hostapd_data *hapd,
569					      struct radius_das_attrs *attr)
570{
571	struct sta_info *sta = NULL;
572	char buf[128];
573
574	if (attr->sta_addr)
575		sta = ap_get_sta(hapd, attr->sta_addr);
576
577	if (sta == NULL && attr->acct_session_id &&
578	    attr->acct_session_id_len == 17) {
579		for (sta = hapd->sta_list; sta; sta = sta->next) {
580			os_snprintf(buf, sizeof(buf), "%08X-%08X",
581				    sta->acct_session_id_hi,
582				    sta->acct_session_id_lo);
583			if (os_memcmp(attr->acct_session_id, buf, 17) == 0)
584				break;
585		}
586	}
587
588	if (sta == NULL && attr->cui) {
589		for (sta = hapd->sta_list; sta; sta = sta->next) {
590			struct wpabuf *cui;
591			cui = ieee802_1x_get_radius_cui(sta->eapol_sm);
592			if (cui && wpabuf_len(cui) == attr->cui_len &&
593			    os_memcmp(wpabuf_head(cui), attr->cui,
594				      attr->cui_len) == 0)
595				break;
596		}
597	}
598
599	if (sta == NULL && attr->user_name) {
600		for (sta = hapd->sta_list; sta; sta = sta->next) {
601			u8 *identity;
602			size_t identity_len;
603			identity = ieee802_1x_get_identity(sta->eapol_sm,
604							   &identity_len);
605			if (identity &&
606			    identity_len == attr->user_name_len &&
607			    os_memcmp(identity, attr->user_name, identity_len)
608			    == 0)
609				break;
610		}
611	}
612
613	return sta;
614}
615
616
617static enum radius_das_res
618hostapd_das_disconnect(void *ctx, struct radius_das_attrs *attr)
619{
620	struct hostapd_data *hapd = ctx;
621	struct sta_info *sta;
622
623	if (hostapd_das_nas_mismatch(hapd, attr))
624		return RADIUS_DAS_NAS_MISMATCH;
625
626	sta = hostapd_das_find_sta(hapd, attr);
627	if (sta == NULL)
628		return RADIUS_DAS_SESSION_NOT_FOUND;
629
630	wpa_auth_pmksa_remove(hapd->wpa_auth, sta->addr);
631
632	hostapd_drv_sta_deauth(hapd, sta->addr,
633			       WLAN_REASON_PREV_AUTH_NOT_VALID);
634	ap_sta_deauthenticate(hapd, sta, WLAN_REASON_PREV_AUTH_NOT_VALID);
635
636	return RADIUS_DAS_SUCCESS;
637}
638
639#endif /* CONFIG_NO_RADIUS */
640
641
642/**
643 * hostapd_setup_bss - Per-BSS setup (initialization)
644 * @hapd: Pointer to BSS data
645 * @first: Whether this BSS is the first BSS of an interface; -1 = not first,
646 *	but interface may exist
647 *
648 * This function is used to initialize all per-BSS data structures and
649 * resources. This gets called in a loop for each BSS when an interface is
650 * initialized. Most of the modules that are initialized here will be
651 * deinitialized in hostapd_cleanup().
652 */
653static int hostapd_setup_bss(struct hostapd_data *hapd, int first)
654{
655	struct hostapd_bss_config *conf = hapd->conf;
656	u8 ssid[HOSTAPD_MAX_SSID_LEN + 1];
657	int ssid_len, set_ssid;
658	char force_ifname[IFNAMSIZ];
659	u8 if_addr[ETH_ALEN];
660
661	wpa_printf(MSG_DEBUG, "%s(hapd=%p (%s), first=%d)",
662		   __func__, hapd, hapd->conf->iface, first);
663
664	if (hapd->started) {
665		wpa_printf(MSG_ERROR, "%s: Interface %s was already started",
666			   __func__, hapd->conf->iface);
667		return -1;
668	}
669	hapd->started = 1;
670
671	if (!first || first == -1) {
672		if (hostapd_mac_comp_empty(hapd->conf->bssid) == 0) {
673			/* Allocate the next available BSSID. */
674			do {
675				inc_byte_array(hapd->own_addr, ETH_ALEN);
676			} while (mac_in_conf(hapd->iconf, hapd->own_addr));
677		} else {
678			/* Allocate the configured BSSID. */
679			os_memcpy(hapd->own_addr, hapd->conf->bssid, ETH_ALEN);
680
681			if (hostapd_mac_comp(hapd->own_addr,
682					     hapd->iface->bss[0]->own_addr) ==
683			    0) {
684				wpa_printf(MSG_ERROR, "BSS '%s' may not have "
685					   "BSSID set to the MAC address of "
686					   "the radio", hapd->conf->iface);
687				return -1;
688			}
689		}
690
691		hapd->interface_added = 1;
692		if (hostapd_if_add(hapd->iface->bss[0], WPA_IF_AP_BSS,
693				   hapd->conf->iface, hapd->own_addr, hapd,
694				   &hapd->drv_priv, force_ifname, if_addr,
695				   hapd->conf->bridge[0] ? hapd->conf->bridge :
696				   NULL, first == -1)) {
697			wpa_printf(MSG_ERROR, "Failed to add BSS (BSSID="
698				   MACSTR ")", MAC2STR(hapd->own_addr));
699			hapd->interface_added = 0;
700			return -1;
701		}
702	}
703
704	if (conf->wmm_enabled < 0)
705		conf->wmm_enabled = hapd->iconf->ieee80211n;
706
707	hostapd_flush_old_stations(hapd, WLAN_REASON_PREV_AUTH_NOT_VALID);
708	hostapd_set_privacy(hapd, 0);
709
710	hostapd_broadcast_wep_clear(hapd);
711	if (hostapd_setup_encryption(hapd->conf->iface, hapd))
712		return -1;
713
714	/*
715	 * Fetch the SSID from the system and use it or,
716	 * if one was specified in the config file, verify they
717	 * match.
718	 */
719	ssid_len = hostapd_get_ssid(hapd, ssid, sizeof(ssid));
720	if (ssid_len < 0) {
721		wpa_printf(MSG_ERROR, "Could not read SSID from system");
722		return -1;
723	}
724	if (conf->ssid.ssid_set) {
725		/*
726		 * If SSID is specified in the config file and it differs
727		 * from what is being used then force installation of the
728		 * new SSID.
729		 */
730		set_ssid = (conf->ssid.ssid_len != (size_t) ssid_len ||
731			    os_memcmp(conf->ssid.ssid, ssid, ssid_len) != 0);
732	} else {
733		/*
734		 * No SSID in the config file; just use the one we got
735		 * from the system.
736		 */
737		set_ssid = 0;
738		conf->ssid.ssid_len = ssid_len;
739		os_memcpy(conf->ssid.ssid, ssid, conf->ssid.ssid_len);
740	}
741
742	if (!hostapd_drv_none(hapd)) {
743		wpa_printf(MSG_ERROR, "Using interface %s with hwaddr " MACSTR
744			   " and ssid \"%s\"",
745			   hapd->conf->iface, MAC2STR(hapd->own_addr),
746			   wpa_ssid_txt(hapd->conf->ssid.ssid,
747					hapd->conf->ssid.ssid_len));
748	}
749
750	if (hostapd_setup_wpa_psk(conf)) {
751		wpa_printf(MSG_ERROR, "WPA-PSK setup failed.");
752		return -1;
753	}
754
755	/* Set SSID for the kernel driver (to be used in beacon and probe
756	 * response frames) */
757	if (set_ssid && hostapd_set_ssid(hapd, conf->ssid.ssid,
758					 conf->ssid.ssid_len)) {
759		wpa_printf(MSG_ERROR, "Could not set SSID for kernel driver");
760		return -1;
761	}
762
763	if (wpa_debug_level == MSG_MSGDUMP)
764		conf->radius->msg_dumps = 1;
765#ifndef CONFIG_NO_RADIUS
766	hapd->radius = radius_client_init(hapd, conf->radius);
767	if (hapd->radius == NULL) {
768		wpa_printf(MSG_ERROR, "RADIUS client initialization failed.");
769		return -1;
770	}
771
772	if (hapd->conf->radius_das_port) {
773		struct radius_das_conf das_conf;
774		os_memset(&das_conf, 0, sizeof(das_conf));
775		das_conf.port = hapd->conf->radius_das_port;
776		das_conf.shared_secret = hapd->conf->radius_das_shared_secret;
777		das_conf.shared_secret_len =
778			hapd->conf->radius_das_shared_secret_len;
779		das_conf.client_addr = &hapd->conf->radius_das_client_addr;
780		das_conf.time_window = hapd->conf->radius_das_time_window;
781		das_conf.require_event_timestamp =
782			hapd->conf->radius_das_require_event_timestamp;
783		das_conf.ctx = hapd;
784		das_conf.disconnect = hostapd_das_disconnect;
785		hapd->radius_das = radius_das_init(&das_conf);
786		if (hapd->radius_das == NULL) {
787			wpa_printf(MSG_ERROR, "RADIUS DAS initialization "
788				   "failed.");
789			return -1;
790		}
791	}
792#endif /* CONFIG_NO_RADIUS */
793
794	if (hostapd_acl_init(hapd)) {
795		wpa_printf(MSG_ERROR, "ACL initialization failed.");
796		return -1;
797	}
798	if (hostapd_init_wps(hapd, conf))
799		return -1;
800
801	if (authsrv_init(hapd) < 0)
802		return -1;
803
804	if (ieee802_1x_init(hapd)) {
805		wpa_printf(MSG_ERROR, "IEEE 802.1X initialization failed.");
806		return -1;
807	}
808
809	if ((hapd->conf->wpa || hapd->conf->osen) && hostapd_setup_wpa(hapd))
810		return -1;
811
812	if (accounting_init(hapd)) {
813		wpa_printf(MSG_ERROR, "Accounting initialization failed.");
814		return -1;
815	}
816
817	if (hapd->conf->ieee802_11f &&
818	    (hapd->iapp = iapp_init(hapd, hapd->conf->iapp_iface)) == NULL) {
819		wpa_printf(MSG_ERROR, "IEEE 802.11F (IAPP) initialization "
820			   "failed.");
821		return -1;
822	}
823
824#ifdef CONFIG_INTERWORKING
825	if (gas_serv_init(hapd)) {
826		wpa_printf(MSG_ERROR, "GAS server initialization failed");
827		return -1;
828	}
829
830	if (conf->qos_map_set_len &&
831	    hostapd_drv_set_qos_map(hapd, conf->qos_map_set,
832				    conf->qos_map_set_len)) {
833		wpa_printf(MSG_ERROR, "Failed to initialize QoS Map");
834		return -1;
835	}
836#endif /* CONFIG_INTERWORKING */
837
838	if (!hostapd_drv_none(hapd) && vlan_init(hapd)) {
839		wpa_printf(MSG_ERROR, "VLAN initialization failed.");
840		return -1;
841	}
842
843	if (!hapd->conf->start_disabled && ieee802_11_set_beacon(hapd) < 0)
844		return -1;
845
846	if (hapd->wpa_auth && wpa_init_keys(hapd->wpa_auth) < 0)
847		return -1;
848
849	if (hapd->driver && hapd->driver->set_operstate)
850		hapd->driver->set_operstate(hapd->drv_priv, 1);
851
852	return 0;
853}
854
855
856static void hostapd_tx_queue_params(struct hostapd_iface *iface)
857{
858	struct hostapd_data *hapd = iface->bss[0];
859	int i;
860	struct hostapd_tx_queue_params *p;
861
862	for (i = 0; i < NUM_TX_QUEUES; i++) {
863		p = &iface->conf->tx_queue[i];
864
865		if (hostapd_set_tx_queue_params(hapd, i, p->aifs, p->cwmin,
866						p->cwmax, p->burst)) {
867			wpa_printf(MSG_DEBUG, "Failed to set TX queue "
868				   "parameters for queue %d.", i);
869			/* Continue anyway */
870		}
871	}
872}
873
874
875static int hostapd_set_acl_list(struct hostapd_data *hapd,
876				struct mac_acl_entry *mac_acl,
877				int n_entries, u8 accept_acl)
878{
879	struct hostapd_acl_params *acl_params;
880	int i, err;
881
882	acl_params = os_zalloc(sizeof(*acl_params) +
883			       (n_entries * sizeof(acl_params->mac_acl[0])));
884	if (!acl_params)
885		return -ENOMEM;
886
887	for (i = 0; i < n_entries; i++)
888		os_memcpy(acl_params->mac_acl[i].addr, mac_acl[i].addr,
889			  ETH_ALEN);
890
891	acl_params->acl_policy = accept_acl;
892	acl_params->num_mac_acl = n_entries;
893
894	err = hostapd_drv_set_acl(hapd, acl_params);
895
896	os_free(acl_params);
897
898	return err;
899}
900
901
902static void hostapd_set_acl(struct hostapd_data *hapd)
903{
904	struct hostapd_config *conf = hapd->iconf;
905	int err;
906	u8 accept_acl;
907
908	if (hapd->iface->drv_max_acl_mac_addrs == 0)
909		return;
910	if (!(conf->bss[0]->num_accept_mac || conf->bss[0]->num_deny_mac))
911		return;
912
913	if (conf->bss[0]->macaddr_acl == DENY_UNLESS_ACCEPTED) {
914		if (conf->bss[0]->num_accept_mac) {
915			accept_acl = 1;
916			err = hostapd_set_acl_list(hapd,
917						   conf->bss[0]->accept_mac,
918						   conf->bss[0]->num_accept_mac,
919						   accept_acl);
920			if (err) {
921				wpa_printf(MSG_DEBUG, "Failed to set accept acl");
922				return;
923			}
924		} else {
925			wpa_printf(MSG_DEBUG, "Mismatch between ACL Policy & Accept/deny lists file");
926		}
927	} else if (conf->bss[0]->macaddr_acl == ACCEPT_UNLESS_DENIED) {
928		if (conf->bss[0]->num_deny_mac) {
929			accept_acl = 0;
930			err = hostapd_set_acl_list(hapd, conf->bss[0]->deny_mac,
931						   conf->bss[0]->num_deny_mac,
932						   accept_acl);
933			if (err) {
934				wpa_printf(MSG_DEBUG, "Failed to set deny acl");
935				return;
936			}
937		} else {
938			wpa_printf(MSG_DEBUG, "Mismatch between ACL Policy & Accept/deny lists file");
939		}
940	}
941}
942
943
944static int start_ctrl_iface_bss(struct hostapd_data *hapd)
945{
946	if (!hapd->iface->interfaces ||
947	    !hapd->iface->interfaces->ctrl_iface_init)
948		return 0;
949
950	if (hapd->iface->interfaces->ctrl_iface_init(hapd)) {
951		wpa_printf(MSG_ERROR,
952			   "Failed to setup control interface for %s",
953			   hapd->conf->iface);
954		return -1;
955	}
956
957	return 0;
958}
959
960
961static int start_ctrl_iface(struct hostapd_iface *iface)
962{
963	size_t i;
964
965	if (!iface->interfaces || !iface->interfaces->ctrl_iface_init)
966		return 0;
967
968	for (i = 0; i < iface->num_bss; i++) {
969		struct hostapd_data *hapd = iface->bss[i];
970		if (iface->interfaces->ctrl_iface_init(hapd)) {
971			wpa_printf(MSG_ERROR,
972				   "Failed to setup control interface for %s",
973				   hapd->conf->iface);
974			return -1;
975		}
976	}
977
978	return 0;
979}
980
981
982static void channel_list_update_timeout(void *eloop_ctx, void *timeout_ctx)
983{
984	struct hostapd_iface *iface = eloop_ctx;
985
986	if (!iface->wait_channel_update) {
987		wpa_printf(MSG_INFO, "Channel list update timeout, but interface was not waiting for it");
988		return;
989	}
990
991	/*
992	 * It is possible that the existing channel list is acceptable, so try
993	 * to proceed.
994	 */
995	wpa_printf(MSG_DEBUG, "Channel list update timeout - try to continue anyway");
996	setup_interface2(iface);
997}
998
999
1000void hostapd_channel_list_updated(struct hostapd_iface *iface, int initiator)
1001{
1002	if (!iface->wait_channel_update || initiator != REGDOM_SET_BY_USER)
1003		return;
1004
1005	wpa_printf(MSG_DEBUG, "Channel list updated - continue setup");
1006	eloop_cancel_timeout(channel_list_update_timeout, iface, NULL);
1007	setup_interface2(iface);
1008}
1009
1010
1011static int setup_interface(struct hostapd_iface *iface)
1012{
1013	struct hostapd_data *hapd = iface->bss[0];
1014	size_t i;
1015
1016	/*
1017	 * It is possible that setup_interface() is called after the interface
1018	 * was disabled etc., in which case driver_ap_teardown is possibly set
1019	 * to 1. Clear it here so any other key/station deletion, which is not
1020	 * part of a teardown flow, would also call the relevant driver
1021	 * callbacks.
1022	 */
1023	iface->driver_ap_teardown = 0;
1024
1025	if (!iface->phy[0]) {
1026		const char *phy = hostapd_drv_get_radio_name(hapd);
1027		if (phy) {
1028			wpa_printf(MSG_DEBUG, "phy: %s", phy);
1029			os_strlcpy(iface->phy, phy, sizeof(iface->phy));
1030		}
1031	}
1032
1033	/*
1034	 * Make sure that all BSSes get configured with a pointer to the same
1035	 * driver interface.
1036	 */
1037	for (i = 1; i < iface->num_bss; i++) {
1038		iface->bss[i]->driver = hapd->driver;
1039		iface->bss[i]->drv_priv = hapd->drv_priv;
1040	}
1041
1042	if (hostapd_validate_bssid_configuration(iface))
1043		return -1;
1044
1045	/*
1046	 * Initialize control interfaces early to allow external monitoring of
1047	 * channel setup operations that may take considerable amount of time
1048	 * especially for DFS cases.
1049	 */
1050	if (start_ctrl_iface(iface))
1051		return -1;
1052
1053	if (hapd->iconf->country[0] && hapd->iconf->country[1]) {
1054		char country[4], previous_country[4];
1055
1056		hostapd_set_state(iface, HAPD_IFACE_COUNTRY_UPDATE);
1057		if (hostapd_get_country(hapd, previous_country) < 0)
1058			previous_country[0] = '\0';
1059
1060		os_memcpy(country, hapd->iconf->country, 3);
1061		country[3] = '\0';
1062		if (hostapd_set_country(hapd, country) < 0) {
1063			wpa_printf(MSG_ERROR, "Failed to set country code");
1064			return -1;
1065		}
1066
1067		wpa_printf(MSG_DEBUG, "Previous country code %s, new country code %s",
1068			   previous_country, country);
1069
1070		if (os_strncmp(previous_country, country, 2) != 0) {
1071			wpa_printf(MSG_DEBUG, "Continue interface setup after channel list update");
1072			iface->wait_channel_update = 1;
1073			eloop_register_timeout(5, 0,
1074					       channel_list_update_timeout,
1075					       iface, NULL);
1076			return 0;
1077		}
1078	}
1079
1080	return setup_interface2(iface);
1081}
1082
1083
1084static int setup_interface2(struct hostapd_iface *iface)
1085{
1086	iface->wait_channel_update = 0;
1087
1088	if (hostapd_get_hw_features(iface)) {
1089		/* Not all drivers support this yet, so continue without hw
1090		 * feature data. */
1091	} else {
1092		int ret = hostapd_select_hw_mode(iface);
1093		if (ret < 0) {
1094			wpa_printf(MSG_ERROR, "Could not select hw_mode and "
1095				   "channel. (%d)", ret);
1096			return -1;
1097		}
1098		if (ret == 1) {
1099			wpa_printf(MSG_DEBUG, "Interface initialization will be completed in a callback (ACS)");
1100			return 0;
1101		}
1102		ret = hostapd_check_ht_capab(iface);
1103		if (ret < 0)
1104			return -1;
1105		if (ret == 1) {
1106			wpa_printf(MSG_DEBUG, "Interface initialization will "
1107				   "be completed in a callback");
1108			return 0;
1109		}
1110
1111		if (iface->conf->ieee80211h)
1112			wpa_printf(MSG_DEBUG, "DFS support is enabled");
1113	}
1114	return hostapd_setup_interface_complete(iface, 0);
1115}
1116
1117
1118/**
1119 * hostapd_setup_interface_complete - Complete interface setup
1120 *
1121 * This function is called when previous steps in the interface setup has been
1122 * completed. This can also start operations, e.g., DFS, that will require
1123 * additional processing before interface is ready to be enabled. Such
1124 * operations will call this function from eloop callbacks when finished.
1125 */
1126int hostapd_setup_interface_complete(struct hostapd_iface *iface, int err)
1127{
1128	struct hostapd_data *hapd = iface->bss[0];
1129	size_t j;
1130	u8 *prev_addr;
1131
1132	if (err) {
1133		wpa_printf(MSG_ERROR, "Interface initialization failed");
1134		hostapd_set_state(iface, HAPD_IFACE_DISABLED);
1135		if (iface->interfaces && iface->interfaces->terminate_on_error)
1136			eloop_terminate();
1137		return -1;
1138	}
1139
1140	wpa_printf(MSG_DEBUG, "Completing interface initialization");
1141	if (iface->conf->channel) {
1142#ifdef NEED_AP_MLME
1143		int res;
1144#endif /* NEED_AP_MLME */
1145
1146		iface->freq = hostapd_hw_get_freq(hapd, iface->conf->channel);
1147		wpa_printf(MSG_DEBUG, "Mode: %s  Channel: %d  "
1148			   "Frequency: %d MHz",
1149			   hostapd_hw_mode_txt(iface->conf->hw_mode),
1150			   iface->conf->channel, iface->freq);
1151
1152#ifdef NEED_AP_MLME
1153		/* Check DFS */
1154		res = hostapd_handle_dfs(iface);
1155		if (res <= 0)
1156			return res;
1157#endif /* NEED_AP_MLME */
1158
1159		if (hostapd_set_freq(hapd, hapd->iconf->hw_mode, iface->freq,
1160				     hapd->iconf->channel,
1161				     hapd->iconf->ieee80211n,
1162				     hapd->iconf->ieee80211ac,
1163				     hapd->iconf->secondary_channel,
1164				     hapd->iconf->vht_oper_chwidth,
1165				     hapd->iconf->vht_oper_centr_freq_seg0_idx,
1166				     hapd->iconf->vht_oper_centr_freq_seg1_idx)) {
1167			wpa_printf(MSG_ERROR, "Could not set channel for "
1168				   "kernel driver");
1169			return -1;
1170		}
1171	}
1172
1173	if (iface->current_mode) {
1174		if (hostapd_prepare_rates(iface, iface->current_mode)) {
1175			wpa_printf(MSG_ERROR, "Failed to prepare rates "
1176				   "table.");
1177			hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
1178				       HOSTAPD_LEVEL_WARNING,
1179				       "Failed to prepare rates table.");
1180			return -1;
1181		}
1182	}
1183
1184	if (hapd->iconf->rts_threshold > -1 &&
1185	    hostapd_set_rts(hapd, hapd->iconf->rts_threshold)) {
1186		wpa_printf(MSG_ERROR, "Could not set RTS threshold for "
1187			   "kernel driver");
1188		return -1;
1189	}
1190
1191	if (hapd->iconf->fragm_threshold > -1 &&
1192	    hostapd_set_frag(hapd, hapd->iconf->fragm_threshold)) {
1193		wpa_printf(MSG_ERROR, "Could not set fragmentation threshold "
1194			   "for kernel driver");
1195		return -1;
1196	}
1197
1198	prev_addr = hapd->own_addr;
1199
1200	for (j = 0; j < iface->num_bss; j++) {
1201		hapd = iface->bss[j];
1202		if (j)
1203			os_memcpy(hapd->own_addr, prev_addr, ETH_ALEN);
1204		if (hostapd_setup_bss(hapd, j == 0))
1205			return -1;
1206		if (hostapd_mac_comp_empty(hapd->conf->bssid) == 0)
1207			prev_addr = hapd->own_addr;
1208	}
1209	hapd = iface->bss[0];
1210
1211	hostapd_tx_queue_params(iface);
1212
1213	ap_list_init(iface);
1214
1215	hostapd_set_acl(hapd);
1216
1217	if (hostapd_driver_commit(hapd) < 0) {
1218		wpa_printf(MSG_ERROR, "%s: Failed to commit driver "
1219			   "configuration", __func__);
1220		return -1;
1221	}
1222
1223	/*
1224	 * WPS UPnP module can be initialized only when the "upnp_iface" is up.
1225	 * If "interface" and "upnp_iface" are the same (e.g., non-bridge
1226	 * mode), the interface is up only after driver_commit, so initialize
1227	 * WPS after driver_commit.
1228	 */
1229	for (j = 0; j < iface->num_bss; j++) {
1230		if (hostapd_init_wps_complete(iface->bss[j]))
1231			return -1;
1232	}
1233
1234	hostapd_set_state(iface, HAPD_IFACE_ENABLED);
1235	wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_ENABLED);
1236	if (hapd->setup_complete_cb)
1237		hapd->setup_complete_cb(hapd->setup_complete_cb_ctx);
1238
1239	wpa_printf(MSG_DEBUG, "%s: Setup of interface done.",
1240		   iface->bss[0]->conf->iface);
1241	if (iface->interfaces && iface->interfaces->terminate_on_error > 0)
1242		iface->interfaces->terminate_on_error--;
1243
1244	return 0;
1245}
1246
1247
1248/**
1249 * hostapd_setup_interface - Setup of an interface
1250 * @iface: Pointer to interface data.
1251 * Returns: 0 on success, -1 on failure
1252 *
1253 * Initializes the driver interface, validates the configuration,
1254 * and sets driver parameters based on the configuration.
1255 * Flushes old stations, sets the channel, encryption,
1256 * beacons, and WDS links based on the configuration.
1257 *
1258 * If interface setup requires more time, e.g., to perform HT co-ex scans, ACS,
1259 * or DFS operations, this function returns 0 before such operations have been
1260 * completed. The pending operations are registered into eloop and will be
1261 * completed from eloop callbacks. Those callbacks end up calling
1262 * hostapd_setup_interface_complete() once setup has been completed.
1263 */
1264int hostapd_setup_interface(struct hostapd_iface *iface)
1265{
1266	int ret;
1267
1268	ret = setup_interface(iface);
1269	if (ret) {
1270		wpa_printf(MSG_ERROR, "%s: Unable to setup interface.",
1271			   iface->bss[0]->conf->iface);
1272		return -1;
1273	}
1274
1275	return 0;
1276}
1277
1278
1279/**
1280 * hostapd_alloc_bss_data - Allocate and initialize per-BSS data
1281 * @hapd_iface: Pointer to interface data
1282 * @conf: Pointer to per-interface configuration
1283 * @bss: Pointer to per-BSS configuration for this BSS
1284 * Returns: Pointer to allocated BSS data
1285 *
1286 * This function is used to allocate per-BSS data structure. This data will be
1287 * freed after hostapd_cleanup() is called for it during interface
1288 * deinitialization.
1289 */
1290struct hostapd_data *
1291hostapd_alloc_bss_data(struct hostapd_iface *hapd_iface,
1292		       struct hostapd_config *conf,
1293		       struct hostapd_bss_config *bss)
1294{
1295	struct hostapd_data *hapd;
1296
1297	hapd = os_zalloc(sizeof(*hapd));
1298	if (hapd == NULL)
1299		return NULL;
1300
1301	hapd->new_assoc_sta_cb = hostapd_new_assoc_sta;
1302	hapd->iconf = conf;
1303	hapd->conf = bss;
1304	hapd->iface = hapd_iface;
1305	hapd->driver = hapd->iconf->driver;
1306	hapd->ctrl_sock = -1;
1307
1308	return hapd;
1309}
1310
1311
1312static void hostapd_bss_deinit(struct hostapd_data *hapd)
1313{
1314	wpa_printf(MSG_DEBUG, "%s: deinit bss %s", __func__,
1315		   hapd->conf->iface);
1316	hostapd_free_stas(hapd);
1317	hostapd_flush_old_stations(hapd, WLAN_REASON_DEAUTH_LEAVING);
1318	hostapd_clear_wep(hapd);
1319	hostapd_cleanup(hapd);
1320}
1321
1322
1323void hostapd_interface_deinit(struct hostapd_iface *iface)
1324{
1325	int j;
1326
1327	wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
1328	if (iface == NULL)
1329		return;
1330
1331	eloop_cancel_timeout(channel_list_update_timeout, iface, NULL);
1332	iface->wait_channel_update = 0;
1333
1334	for (j = iface->num_bss - 1; j >= 0; j--)
1335		hostapd_bss_deinit(iface->bss[j]);
1336}
1337
1338
1339void hostapd_interface_free(struct hostapd_iface *iface)
1340{
1341	size_t j;
1342	wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
1343	for (j = 0; j < iface->num_bss; j++) {
1344		wpa_printf(MSG_DEBUG, "%s: free hapd %p",
1345			   __func__, iface->bss[j]);
1346		os_free(iface->bss[j]);
1347	}
1348	hostapd_cleanup_iface(iface);
1349}
1350
1351
1352/**
1353 * hostapd_init - Allocate and initialize per-interface data
1354 * @config_file: Path to the configuration file
1355 * Returns: Pointer to the allocated interface data or %NULL on failure
1356 *
1357 * This function is used to allocate main data structures for per-interface
1358 * data. The allocated data buffer will be freed by calling
1359 * hostapd_cleanup_iface().
1360 */
1361struct hostapd_iface * hostapd_init(struct hapd_interfaces *interfaces,
1362				    const char *config_file)
1363{
1364	struct hostapd_iface *hapd_iface = NULL;
1365	struct hostapd_config *conf = NULL;
1366	struct hostapd_data *hapd;
1367	size_t i;
1368
1369	hapd_iface = os_zalloc(sizeof(*hapd_iface));
1370	if (hapd_iface == NULL)
1371		goto fail;
1372
1373	hapd_iface->config_fname = os_strdup(config_file);
1374	if (hapd_iface->config_fname == NULL)
1375		goto fail;
1376
1377	conf = interfaces->config_read_cb(hapd_iface->config_fname);
1378	if (conf == NULL)
1379		goto fail;
1380	hapd_iface->conf = conf;
1381
1382	hapd_iface->num_bss = conf->num_bss;
1383	hapd_iface->bss = os_calloc(conf->num_bss,
1384				    sizeof(struct hostapd_data *));
1385	if (hapd_iface->bss == NULL)
1386		goto fail;
1387
1388	for (i = 0; i < conf->num_bss; i++) {
1389		hapd = hapd_iface->bss[i] =
1390			hostapd_alloc_bss_data(hapd_iface, conf,
1391					       conf->bss[i]);
1392		if (hapd == NULL)
1393			goto fail;
1394		hapd->msg_ctx = hapd;
1395	}
1396
1397	return hapd_iface;
1398
1399fail:
1400	wpa_printf(MSG_ERROR, "Failed to set up interface with %s",
1401		   config_file);
1402	if (conf)
1403		hostapd_config_free(conf);
1404	if (hapd_iface) {
1405		os_free(hapd_iface->config_fname);
1406		os_free(hapd_iface->bss);
1407		wpa_printf(MSG_DEBUG, "%s: free iface %p",
1408			   __func__, hapd_iface);
1409		os_free(hapd_iface);
1410	}
1411	return NULL;
1412}
1413
1414
1415static int ifname_in_use(struct hapd_interfaces *interfaces, const char *ifname)
1416{
1417	size_t i, j;
1418
1419	for (i = 0; i < interfaces->count; i++) {
1420		struct hostapd_iface *iface = interfaces->iface[i];
1421		for (j = 0; j < iface->num_bss; j++) {
1422			struct hostapd_data *hapd = iface->bss[j];
1423			if (os_strcmp(ifname, hapd->conf->iface) == 0)
1424				return 1;
1425		}
1426	}
1427
1428	return 0;
1429}
1430
1431
1432/**
1433 * hostapd_interface_init_bss - Read configuration file and init BSS data
1434 *
1435 * This function is used to parse configuration file for a BSS. This BSS is
1436 * added to an existing interface sharing the same radio (if any) or a new
1437 * interface is created if this is the first interface on a radio. This
1438 * allocate memory for the BSS. No actual driver operations are started.
1439 *
1440 * This is similar to hostapd_interface_init(), but for a case where the
1441 * configuration is used to add a single BSS instead of all BSSes for a radio.
1442 */
1443struct hostapd_iface *
1444hostapd_interface_init_bss(struct hapd_interfaces *interfaces, const char *phy,
1445			   const char *config_fname, int debug)
1446{
1447	struct hostapd_iface *new_iface = NULL, *iface = NULL;
1448	struct hostapd_data *hapd;
1449	int k;
1450	size_t i, bss_idx;
1451
1452	if (!phy || !*phy)
1453		return NULL;
1454
1455	for (i = 0; i < interfaces->count; i++) {
1456		if (os_strcmp(interfaces->iface[i]->phy, phy) == 0) {
1457			iface = interfaces->iface[i];
1458			break;
1459		}
1460	}
1461
1462	wpa_printf(MSG_INFO, "Configuration file: %s (phy %s)%s",
1463		   config_fname, phy, iface ? "" : " --> new PHY");
1464	if (iface) {
1465		struct hostapd_config *conf;
1466		struct hostapd_bss_config **tmp_conf;
1467		struct hostapd_data **tmp_bss;
1468		struct hostapd_bss_config *bss;
1469		const char *ifname;
1470
1471		/* Add new BSS to existing iface */
1472		conf = interfaces->config_read_cb(config_fname);
1473		if (conf == NULL)
1474			return NULL;
1475		if (conf->num_bss > 1) {
1476			wpa_printf(MSG_ERROR, "Multiple BSSes specified in BSS-config");
1477			hostapd_config_free(conf);
1478			return NULL;
1479		}
1480
1481		ifname = conf->bss[0]->iface;
1482		if (ifname[0] != '\0' && ifname_in_use(interfaces, ifname)) {
1483			wpa_printf(MSG_ERROR,
1484				   "Interface name %s already in use", ifname);
1485			hostapd_config_free(conf);
1486			return NULL;
1487		}
1488
1489		tmp_conf = os_realloc_array(
1490			iface->conf->bss, iface->conf->num_bss + 1,
1491			sizeof(struct hostapd_bss_config *));
1492		tmp_bss = os_realloc_array(iface->bss, iface->num_bss + 1,
1493					   sizeof(struct hostapd_data *));
1494		if (tmp_bss)
1495			iface->bss = tmp_bss;
1496		if (tmp_conf) {
1497			iface->conf->bss = tmp_conf;
1498			iface->conf->last_bss = tmp_conf[0];
1499		}
1500		if (tmp_bss == NULL || tmp_conf == NULL) {
1501			hostapd_config_free(conf);
1502			return NULL;
1503		}
1504		bss = iface->conf->bss[iface->conf->num_bss] = conf->bss[0];
1505		iface->conf->num_bss++;
1506
1507		hapd = hostapd_alloc_bss_data(iface, iface->conf, bss);
1508		if (hapd == NULL) {
1509			iface->conf->num_bss--;
1510			hostapd_config_free(conf);
1511			return NULL;
1512		}
1513		iface->conf->last_bss = bss;
1514		iface->bss[iface->num_bss] = hapd;
1515		hapd->msg_ctx = hapd;
1516
1517		bss_idx = iface->num_bss++;
1518		conf->num_bss--;
1519		conf->bss[0] = NULL;
1520		hostapd_config_free(conf);
1521	} else {
1522		/* Add a new iface with the first BSS */
1523		new_iface = iface = hostapd_init(interfaces, config_fname);
1524		if (!iface)
1525			return NULL;
1526		os_strlcpy(iface->phy, phy, sizeof(iface->phy));
1527		iface->interfaces = interfaces;
1528		bss_idx = 0;
1529	}
1530
1531	for (k = 0; k < debug; k++) {
1532		if (iface->bss[bss_idx]->conf->logger_stdout_level > 0)
1533			iface->bss[bss_idx]->conf->logger_stdout_level--;
1534	}
1535
1536	if (iface->conf->bss[bss_idx]->iface[0] == '\0' &&
1537	    !hostapd_drv_none(iface->bss[bss_idx])) {
1538		wpa_printf(MSG_ERROR, "Interface name not specified in %s",
1539			   config_fname);
1540		if (new_iface)
1541			hostapd_interface_deinit_free(new_iface);
1542		return NULL;
1543	}
1544
1545	return iface;
1546}
1547
1548
1549void hostapd_interface_deinit_free(struct hostapd_iface *iface)
1550{
1551	const struct wpa_driver_ops *driver;
1552	void *drv_priv;
1553
1554	wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
1555	if (iface == NULL)
1556		return;
1557	wpa_printf(MSG_DEBUG, "%s: num_bss=%u conf->num_bss=%u",
1558		   __func__, (unsigned int) iface->num_bss,
1559		   (unsigned int) iface->conf->num_bss);
1560	driver = iface->bss[0]->driver;
1561	drv_priv = iface->bss[0]->drv_priv;
1562	hostapd_interface_deinit(iface);
1563	wpa_printf(MSG_DEBUG, "%s: driver=%p drv_priv=%p -> hapd_deinit",
1564		   __func__, driver, drv_priv);
1565	if (driver && driver->hapd_deinit && drv_priv)
1566		driver->hapd_deinit(drv_priv);
1567	hostapd_interface_free(iface);
1568}
1569
1570
1571int hostapd_enable_iface(struct hostapd_iface *hapd_iface)
1572{
1573	if (hapd_iface->bss[0]->drv_priv != NULL) {
1574		wpa_printf(MSG_ERROR, "Interface %s already enabled",
1575			   hapd_iface->conf->bss[0]->iface);
1576		return -1;
1577	}
1578
1579	wpa_printf(MSG_DEBUG, "Enable interface %s",
1580		   hapd_iface->conf->bss[0]->iface);
1581
1582	if (hostapd_config_check(hapd_iface->conf, 1) < 0) {
1583		wpa_printf(MSG_INFO, "Invalid configuration - cannot enable");
1584		return -1;
1585	}
1586
1587	if (hapd_iface->interfaces == NULL ||
1588	    hapd_iface->interfaces->driver_init == NULL ||
1589	    hapd_iface->interfaces->driver_init(hapd_iface))
1590		return -1;
1591
1592	if (hostapd_setup_interface(hapd_iface)) {
1593		const struct wpa_driver_ops *driver;
1594		void *drv_priv;
1595
1596		driver = hapd_iface->bss[0]->driver;
1597		drv_priv = hapd_iface->bss[0]->drv_priv;
1598		wpa_printf(MSG_DEBUG, "%s: driver=%p drv_priv=%p -> hapd_deinit",
1599			   __func__, driver, drv_priv);
1600		if (driver && driver->hapd_deinit && drv_priv) {
1601			driver->hapd_deinit(drv_priv);
1602			hapd_iface->bss[0]->drv_priv = NULL;
1603		}
1604		return -1;
1605	}
1606
1607	return 0;
1608}
1609
1610
1611int hostapd_reload_iface(struct hostapd_iface *hapd_iface)
1612{
1613	size_t j;
1614
1615	wpa_printf(MSG_DEBUG, "Reload interface %s",
1616		   hapd_iface->conf->bss[0]->iface);
1617	for (j = 0; j < hapd_iface->num_bss; j++)
1618		hostapd_set_security_params(hapd_iface->conf->bss[j]);
1619	if (hostapd_config_check(hapd_iface->conf, 1) < 0) {
1620		wpa_printf(MSG_ERROR, "Updated configuration is invalid");
1621		return -1;
1622	}
1623	hostapd_clear_old(hapd_iface);
1624	for (j = 0; j < hapd_iface->num_bss; j++)
1625		hostapd_reload_bss(hapd_iface->bss[j]);
1626
1627	return 0;
1628}
1629
1630
1631int hostapd_disable_iface(struct hostapd_iface *hapd_iface)
1632{
1633	size_t j;
1634	const struct wpa_driver_ops *driver;
1635	void *drv_priv;
1636
1637	if (hapd_iface == NULL)
1638		return -1;
1639	wpa_msg(hapd_iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
1640	driver = hapd_iface->bss[0]->driver;
1641	drv_priv = hapd_iface->bss[0]->drv_priv;
1642
1643	hapd_iface->driver_ap_teardown =
1644		!!(hapd_iface->drv_flags &
1645		   WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
1646
1647	/* same as hostapd_interface_deinit without deinitializing ctrl-iface */
1648	for (j = 0; j < hapd_iface->num_bss; j++) {
1649		struct hostapd_data *hapd = hapd_iface->bss[j];
1650		hostapd_free_stas(hapd);
1651		hostapd_flush_old_stations(hapd, WLAN_REASON_DEAUTH_LEAVING);
1652		hostapd_clear_wep(hapd);
1653		hostapd_free_hapd_data(hapd);
1654	}
1655
1656	wpa_printf(MSG_DEBUG, "%s: driver=%p drv_priv=%p -> hapd_deinit",
1657		   __func__, driver, drv_priv);
1658	if (driver && driver->hapd_deinit && drv_priv) {
1659		driver->hapd_deinit(drv_priv);
1660		hapd_iface->bss[0]->drv_priv = NULL;
1661	}
1662
1663	/* From hostapd_cleanup_iface: These were initialized in
1664	 * hostapd_setup_interface and hostapd_setup_interface_complete
1665	 */
1666	hostapd_cleanup_iface_partial(hapd_iface);
1667
1668	wpa_printf(MSG_DEBUG, "Interface %s disabled",
1669		   hapd_iface->bss[0]->conf->iface);
1670	hostapd_set_state(hapd_iface, HAPD_IFACE_DISABLED);
1671	return 0;
1672}
1673
1674
1675static struct hostapd_iface *
1676hostapd_iface_alloc(struct hapd_interfaces *interfaces)
1677{
1678	struct hostapd_iface **iface, *hapd_iface;
1679
1680	iface = os_realloc_array(interfaces->iface, interfaces->count + 1,
1681				 sizeof(struct hostapd_iface *));
1682	if (iface == NULL)
1683		return NULL;
1684	interfaces->iface = iface;
1685	hapd_iface = interfaces->iface[interfaces->count] =
1686		os_zalloc(sizeof(*hapd_iface));
1687	if (hapd_iface == NULL) {
1688		wpa_printf(MSG_ERROR, "%s: Failed to allocate memory for "
1689			   "the interface", __func__);
1690		return NULL;
1691	}
1692	interfaces->count++;
1693	hapd_iface->interfaces = interfaces;
1694
1695	return hapd_iface;
1696}
1697
1698
1699static struct hostapd_config *
1700hostapd_config_alloc(struct hapd_interfaces *interfaces, const char *ifname,
1701		     const char *ctrl_iface)
1702{
1703	struct hostapd_bss_config *bss;
1704	struct hostapd_config *conf;
1705
1706	/* Allocates memory for bss and conf */
1707	conf = hostapd_config_defaults();
1708	if (conf == NULL) {
1709		 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory for "
1710				"configuration", __func__);
1711		return NULL;
1712	}
1713
1714	conf->driver = wpa_drivers[0];
1715	if (conf->driver == NULL) {
1716		wpa_printf(MSG_ERROR, "No driver wrappers registered!");
1717		hostapd_config_free(conf);
1718		return NULL;
1719	}
1720
1721	bss = conf->last_bss = conf->bss[0];
1722
1723	os_strlcpy(bss->iface, ifname, sizeof(bss->iface));
1724	bss->ctrl_interface = os_strdup(ctrl_iface);
1725	if (bss->ctrl_interface == NULL) {
1726		hostapd_config_free(conf);
1727		return NULL;
1728	}
1729
1730	/* Reading configuration file skipped, will be done in SET!
1731	 * From reading the configuration till the end has to be done in
1732	 * SET
1733	 */
1734	return conf;
1735}
1736
1737
1738static struct hostapd_iface * hostapd_data_alloc(
1739	struct hapd_interfaces *interfaces, struct hostapd_config *conf)
1740{
1741	size_t i;
1742	struct hostapd_iface *hapd_iface =
1743		interfaces->iface[interfaces->count - 1];
1744	struct hostapd_data *hapd;
1745
1746	hapd_iface->conf = conf;
1747	hapd_iface->num_bss = conf->num_bss;
1748
1749	hapd_iface->bss = os_zalloc(conf->num_bss *
1750				    sizeof(struct hostapd_data *));
1751	if (hapd_iface->bss == NULL)
1752		return NULL;
1753
1754	for (i = 0; i < conf->num_bss; i++) {
1755		hapd = hapd_iface->bss[i] =
1756			hostapd_alloc_bss_data(hapd_iface, conf, conf->bss[i]);
1757		if (hapd == NULL)
1758			return NULL;
1759		hapd->msg_ctx = hapd;
1760	}
1761
1762	hapd_iface->interfaces = interfaces;
1763
1764	return hapd_iface;
1765}
1766
1767
1768int hostapd_add_iface(struct hapd_interfaces *interfaces, char *buf)
1769{
1770	struct hostapd_config *conf = NULL;
1771	struct hostapd_iface *hapd_iface = NULL, *new_iface = NULL;
1772	struct hostapd_data *hapd;
1773	char *ptr;
1774	size_t i, j;
1775	const char *conf_file = NULL, *phy_name = NULL;
1776
1777	if (os_strncmp(buf, "bss_config=", 11) == 0) {
1778		char *pos;
1779		phy_name = buf + 11;
1780		pos = os_strchr(phy_name, ':');
1781		if (!pos)
1782			return -1;
1783		*pos++ = '\0';
1784		conf_file = pos;
1785		if (!os_strlen(conf_file))
1786			return -1;
1787
1788		hapd_iface = hostapd_interface_init_bss(interfaces, phy_name,
1789							conf_file, 0);
1790		if (!hapd_iface)
1791			return -1;
1792		for (j = 0; j < interfaces->count; j++) {
1793			if (interfaces->iface[j] == hapd_iface)
1794				break;
1795		}
1796		if (j == interfaces->count) {
1797			struct hostapd_iface **tmp;
1798			tmp = os_realloc_array(interfaces->iface,
1799					       interfaces->count + 1,
1800					       sizeof(struct hostapd_iface *));
1801			if (!tmp) {
1802				hostapd_interface_deinit_free(hapd_iface);
1803				return -1;
1804			}
1805			interfaces->iface = tmp;
1806			interfaces->iface[interfaces->count++] = hapd_iface;
1807			new_iface = hapd_iface;
1808		}
1809
1810		if (new_iface) {
1811			if (interfaces->driver_init(hapd_iface) ||
1812			    hostapd_setup_interface(hapd_iface)) {
1813				interfaces->count--;
1814				goto fail;
1815			}
1816		} else {
1817			/* Assign new BSS with bss[0]'s driver info */
1818			hapd = hapd_iface->bss[hapd_iface->num_bss - 1];
1819			hapd->driver = hapd_iface->bss[0]->driver;
1820			hapd->drv_priv = hapd_iface->bss[0]->drv_priv;
1821			os_memcpy(hapd->own_addr, hapd_iface->bss[0]->own_addr,
1822				  ETH_ALEN);
1823
1824			if (start_ctrl_iface_bss(hapd) < 0 ||
1825			    (hapd_iface->state == HAPD_IFACE_ENABLED &&
1826			     hostapd_setup_bss(hapd, -1))) {
1827				hapd_iface->bss[hapd_iface->num_bss - 1] = NULL;
1828				hapd_iface->conf->num_bss--;
1829				hapd_iface->num_bss--;
1830				wpa_printf(MSG_DEBUG, "%s: free hapd %p %s",
1831					   __func__, hapd, hapd->conf->iface);
1832				os_free(hapd);
1833				return -1;
1834			}
1835		}
1836		return 0;
1837	}
1838
1839	ptr = os_strchr(buf, ' ');
1840	if (ptr == NULL)
1841		return -1;
1842	*ptr++ = '\0';
1843
1844	if (os_strncmp(ptr, "config=", 7) == 0)
1845		conf_file = ptr + 7;
1846
1847	for (i = 0; i < interfaces->count; i++) {
1848		if (!os_strcmp(interfaces->iface[i]->conf->bss[0]->iface,
1849			       buf)) {
1850			wpa_printf(MSG_INFO, "Cannot add interface - it "
1851				   "already exists");
1852			return -1;
1853		}
1854	}
1855
1856	hapd_iface = hostapd_iface_alloc(interfaces);
1857	if (hapd_iface == NULL) {
1858		wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
1859			   "for interface", __func__);
1860		goto fail;
1861	}
1862
1863	if (conf_file && interfaces->config_read_cb) {
1864		conf = interfaces->config_read_cb(conf_file);
1865		if (conf && conf->bss)
1866			os_strlcpy(conf->bss[0]->iface, buf,
1867				   sizeof(conf->bss[0]->iface));
1868	} else
1869		conf = hostapd_config_alloc(interfaces, buf, ptr);
1870	if (conf == NULL || conf->bss == NULL) {
1871		wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
1872			   "for configuration", __func__);
1873		goto fail;
1874	}
1875
1876	hapd_iface = hostapd_data_alloc(interfaces, conf);
1877	if (hapd_iface == NULL) {
1878		wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
1879			   "for hostapd", __func__);
1880		goto fail;
1881	}
1882
1883	if (start_ctrl_iface(hapd_iface) < 0)
1884		goto fail;
1885
1886	wpa_printf(MSG_INFO, "Add interface '%s'", conf->bss[0]->iface);
1887
1888	return 0;
1889
1890fail:
1891	if (conf)
1892		hostapd_config_free(conf);
1893	if (hapd_iface) {
1894		if (hapd_iface->bss) {
1895			for (i = 0; i < hapd_iface->num_bss; i++) {
1896				hapd = hapd_iface->bss[i];
1897				if (!hapd)
1898					continue;
1899				if (hapd_iface->interfaces &&
1900				    hapd_iface->interfaces->ctrl_iface_deinit)
1901					hapd_iface->interfaces->
1902						ctrl_iface_deinit(hapd);
1903				wpa_printf(MSG_DEBUG, "%s: free hapd %p (%s)",
1904					   __func__, hapd_iface->bss[i],
1905					   hapd->conf->iface);
1906				os_free(hapd);
1907				hapd_iface->bss[i] = NULL;
1908			}
1909			os_free(hapd_iface->bss);
1910		}
1911		wpa_printf(MSG_DEBUG, "%s: free iface %p",
1912			   __func__, hapd_iface);
1913		os_free(hapd_iface);
1914	}
1915	return -1;
1916}
1917
1918
1919static int hostapd_remove_bss(struct hostapd_iface *iface, unsigned int idx)
1920{
1921	size_t i;
1922
1923	wpa_printf(MSG_INFO, "Remove BSS '%s'", iface->conf->bss[idx]->iface);
1924
1925	/* Remove hostapd_data only if it has already been initialized */
1926	if (idx < iface->num_bss) {
1927		struct hostapd_data *hapd = iface->bss[idx];
1928
1929		hostapd_bss_deinit(hapd);
1930		wpa_printf(MSG_DEBUG, "%s: free hapd %p (%s)",
1931			   __func__, hapd, hapd->conf->iface);
1932		hostapd_config_free_bss(hapd->conf);
1933		os_free(hapd);
1934
1935		iface->num_bss--;
1936
1937		for (i = idx; i < iface->num_bss; i++)
1938			iface->bss[i] = iface->bss[i + 1];
1939	} else {
1940		hostapd_config_free_bss(iface->conf->bss[idx]);
1941		iface->conf->bss[idx] = NULL;
1942	}
1943
1944	iface->conf->num_bss--;
1945	for (i = idx; i < iface->conf->num_bss; i++)
1946		iface->conf->bss[i] = iface->conf->bss[i + 1];
1947
1948	return 0;
1949}
1950
1951
1952int hostapd_remove_iface(struct hapd_interfaces *interfaces, char *buf)
1953{
1954	struct hostapd_iface *hapd_iface;
1955	size_t i, j, k = 0;
1956
1957	for (i = 0; i < interfaces->count; i++) {
1958		hapd_iface = interfaces->iface[i];
1959		if (hapd_iface == NULL)
1960			return -1;
1961		if (!os_strcmp(hapd_iface->conf->bss[0]->iface, buf)) {
1962			wpa_printf(MSG_INFO, "Remove interface '%s'", buf);
1963			hapd_iface->driver_ap_teardown =
1964				!!(hapd_iface->drv_flags &
1965				   WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
1966
1967			hostapd_interface_deinit_free(hapd_iface);
1968			k = i;
1969			while (k < (interfaces->count - 1)) {
1970				interfaces->iface[k] =
1971					interfaces->iface[k + 1];
1972				k++;
1973			}
1974			interfaces->count--;
1975			return 0;
1976		}
1977
1978		for (j = 0; j < hapd_iface->conf->num_bss; j++) {
1979			if (!os_strcmp(hapd_iface->conf->bss[j]->iface, buf)) {
1980				hapd_iface->driver_ap_teardown =
1981					!(hapd_iface->drv_flags &
1982					  WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
1983				return hostapd_remove_bss(hapd_iface, j);
1984			}
1985		}
1986	}
1987	return -1;
1988}
1989
1990
1991/**
1992 * hostapd_new_assoc_sta - Notify that a new station associated with the AP
1993 * @hapd: Pointer to BSS data
1994 * @sta: Pointer to the associated STA data
1995 * @reassoc: 1 to indicate this was a re-association; 0 = first association
1996 *
1997 * This function will be called whenever a station associates with the AP. It
1998 * can be called from ieee802_11.c for drivers that export MLME to hostapd and
1999 * from drv_callbacks.c based on driver events for drivers that take care of
2000 * management frames (IEEE 802.11 authentication and association) internally.
2001 */
2002void hostapd_new_assoc_sta(struct hostapd_data *hapd, struct sta_info *sta,
2003			   int reassoc)
2004{
2005	if (hapd->tkip_countermeasures) {
2006		hostapd_drv_sta_deauth(hapd, sta->addr,
2007				       WLAN_REASON_MICHAEL_MIC_FAILURE);
2008		return;
2009	}
2010
2011	hostapd_prune_associations(hapd, sta->addr);
2012
2013	/* IEEE 802.11F (IAPP) */
2014	if (hapd->conf->ieee802_11f)
2015		iapp_new_station(hapd->iapp, sta);
2016
2017#ifdef CONFIG_P2P
2018	if (sta->p2p_ie == NULL && !sta->no_p2p_set) {
2019		sta->no_p2p_set = 1;
2020		hapd->num_sta_no_p2p++;
2021		if (hapd->num_sta_no_p2p == 1)
2022			hostapd_p2p_non_p2p_sta_connected(hapd);
2023	}
2024#endif /* CONFIG_P2P */
2025
2026	/* Start accounting here, if IEEE 802.1X and WPA are not used.
2027	 * IEEE 802.1X/WPA code will start accounting after the station has
2028	 * been authorized. */
2029	if (!hapd->conf->ieee802_1x && !hapd->conf->wpa && !hapd->conf->osen) {
2030		ap_sta_set_authorized(hapd, sta, 1);
2031		os_get_reltime(&sta->connected_time);
2032		accounting_sta_start(hapd, sta);
2033	}
2034
2035	/* Start IEEE 802.1X authentication process for new stations */
2036	ieee802_1x_new_station(hapd, sta);
2037	if (reassoc) {
2038		if (sta->auth_alg != WLAN_AUTH_FT &&
2039		    !(sta->flags & (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS)))
2040			wpa_auth_sm_event(sta->wpa_sm, WPA_REAUTH);
2041	} else
2042		wpa_auth_sta_associated(hapd->wpa_auth, sta->wpa_sm);
2043
2044	if (!(hapd->iface->drv_flags & WPA_DRIVER_FLAGS_INACTIVITY_TIMER)) {
2045		wpa_printf(MSG_DEBUG, "%s: reschedule ap_handle_timer timeout "
2046			   "for " MACSTR " (%d seconds - ap_max_inactivity)",
2047			   __func__, MAC2STR(sta->addr),
2048			   hapd->conf->ap_max_inactivity);
2049		eloop_cancel_timeout(ap_handle_timer, hapd, sta);
2050		eloop_register_timeout(hapd->conf->ap_max_inactivity, 0,
2051				       ap_handle_timer, hapd, sta);
2052	}
2053}
2054
2055
2056const char * hostapd_state_text(enum hostapd_iface_state s)
2057{
2058	switch (s) {
2059	case HAPD_IFACE_UNINITIALIZED:
2060		return "UNINITIALIZED";
2061	case HAPD_IFACE_DISABLED:
2062		return "DISABLED";
2063	case HAPD_IFACE_COUNTRY_UPDATE:
2064		return "COUNTRY_UPDATE";
2065	case HAPD_IFACE_ACS:
2066		return "ACS";
2067	case HAPD_IFACE_HT_SCAN:
2068		return "HT_SCAN";
2069	case HAPD_IFACE_DFS:
2070		return "DFS";
2071	case HAPD_IFACE_ENABLED:
2072		return "ENABLED";
2073	}
2074
2075	return "UNKNOWN";
2076}
2077
2078
2079void hostapd_set_state(struct hostapd_iface *iface, enum hostapd_iface_state s)
2080{
2081	wpa_printf(MSG_INFO, "%s: interface state %s->%s",
2082		   iface->conf->bss[0]->iface, hostapd_state_text(iface->state),
2083		   hostapd_state_text(s));
2084	iface->state = s;
2085}
2086
2087
2088#ifdef NEED_AP_MLME
2089
2090static void free_beacon_data(struct beacon_data *beacon)
2091{
2092	os_free(beacon->head);
2093	beacon->head = NULL;
2094	os_free(beacon->tail);
2095	beacon->tail = NULL;
2096	os_free(beacon->probe_resp);
2097	beacon->probe_resp = NULL;
2098	os_free(beacon->beacon_ies);
2099	beacon->beacon_ies = NULL;
2100	os_free(beacon->proberesp_ies);
2101	beacon->proberesp_ies = NULL;
2102	os_free(beacon->assocresp_ies);
2103	beacon->assocresp_ies = NULL;
2104}
2105
2106
2107static int hostapd_build_beacon_data(struct hostapd_iface *iface,
2108				     struct beacon_data *beacon)
2109{
2110	struct wpabuf *beacon_extra, *proberesp_extra, *assocresp_extra;
2111	struct wpa_driver_ap_params params;
2112	int ret;
2113	struct hostapd_data *hapd = iface->bss[0];
2114
2115	os_memset(beacon, 0, sizeof(*beacon));
2116	ret = ieee802_11_build_ap_params(hapd, &params);
2117	if (ret < 0)
2118		return ret;
2119
2120	ret = hostapd_build_ap_extra_ies(hapd, &beacon_extra,
2121					 &proberesp_extra,
2122					 &assocresp_extra);
2123	if (ret)
2124		goto free_ap_params;
2125
2126	ret = -1;
2127	beacon->head = os_malloc(params.head_len);
2128	if (!beacon->head)
2129		goto free_ap_extra_ies;
2130
2131	os_memcpy(beacon->head, params.head, params.head_len);
2132	beacon->head_len = params.head_len;
2133
2134	beacon->tail = os_malloc(params.tail_len);
2135	if (!beacon->tail)
2136		goto free_beacon;
2137
2138	os_memcpy(beacon->tail, params.tail, params.tail_len);
2139	beacon->tail_len = params.tail_len;
2140
2141	if (params.proberesp != NULL) {
2142		beacon->probe_resp = os_malloc(params.proberesp_len);
2143		if (!beacon->probe_resp)
2144			goto free_beacon;
2145
2146		os_memcpy(beacon->probe_resp, params.proberesp,
2147			  params.proberesp_len);
2148		beacon->probe_resp_len = params.proberesp_len;
2149	}
2150
2151	/* copy the extra ies */
2152	if (beacon_extra) {
2153		beacon->beacon_ies = os_malloc(wpabuf_len(beacon_extra));
2154		if (!beacon->beacon_ies)
2155			goto free_beacon;
2156
2157		os_memcpy(beacon->beacon_ies,
2158			  beacon_extra->buf, wpabuf_len(beacon_extra));
2159		beacon->beacon_ies_len = wpabuf_len(beacon_extra);
2160	}
2161
2162	if (proberesp_extra) {
2163		beacon->proberesp_ies =
2164			os_malloc(wpabuf_len(proberesp_extra));
2165		if (!beacon->proberesp_ies)
2166			goto free_beacon;
2167
2168		os_memcpy(beacon->proberesp_ies, proberesp_extra->buf,
2169			  wpabuf_len(proberesp_extra));
2170		beacon->proberesp_ies_len = wpabuf_len(proberesp_extra);
2171	}
2172
2173	if (assocresp_extra) {
2174		beacon->assocresp_ies =
2175			os_malloc(wpabuf_len(assocresp_extra));
2176		if (!beacon->assocresp_ies)
2177			goto free_beacon;
2178
2179		os_memcpy(beacon->assocresp_ies, assocresp_extra->buf,
2180			  wpabuf_len(assocresp_extra));
2181		beacon->assocresp_ies_len = wpabuf_len(assocresp_extra);
2182	}
2183
2184	ret = 0;
2185free_beacon:
2186	/* if the function fails, the caller should not free beacon data */
2187	if (ret)
2188		free_beacon_data(beacon);
2189
2190free_ap_extra_ies:
2191	hostapd_free_ap_extra_ies(hapd, beacon_extra, proberesp_extra,
2192				  assocresp_extra);
2193free_ap_params:
2194	ieee802_11_free_ap_params(&params);
2195	return ret;
2196}
2197
2198
2199/*
2200 * TODO: This flow currently supports only changing frequency within the
2201 * same hw_mode. Any other changes to MAC parameters or provided settings (even
2202 * width) are not supported.
2203 */
2204static int hostapd_change_config_freq(struct hostapd_data *hapd,
2205				      struct hostapd_config *conf,
2206				      struct hostapd_freq_params *params,
2207				      struct hostapd_freq_params *old_params)
2208{
2209	int channel;
2210
2211	if (!params->channel) {
2212		/* check if the new channel is supported by hw */
2213		channel = hostapd_hw_get_channel(hapd, params->freq);
2214		if (!channel)
2215			return -1;
2216	} else {
2217		channel = params->channel;
2218	}
2219
2220	/* if a pointer to old_params is provided we save previous state */
2221	if (old_params) {
2222		old_params->channel = conf->channel;
2223		old_params->ht_enabled = conf->ieee80211n;
2224		old_params->sec_channel_offset = conf->secondary_channel;
2225	}
2226
2227	conf->channel = channel;
2228	conf->ieee80211n = params->ht_enabled;
2229	conf->secondary_channel = params->sec_channel_offset;
2230
2231	/* TODO: maybe call here hostapd_config_check here? */
2232
2233	return 0;
2234}
2235
2236
2237static int hostapd_fill_csa_settings(struct hostapd_iface *iface,
2238				     struct csa_settings *settings)
2239{
2240	struct hostapd_freq_params old_freq;
2241	int ret;
2242
2243	os_memset(&old_freq, 0, sizeof(old_freq));
2244	if (!iface || !iface->freq || iface->csa_in_progress)
2245		return -1;
2246
2247	ret = hostapd_change_config_freq(iface->bss[0], iface->conf,
2248					 &settings->freq_params,
2249					 &old_freq);
2250	if (ret)
2251		return ret;
2252
2253	ret = hostapd_build_beacon_data(iface, &settings->beacon_after);
2254
2255	/* change back the configuration */
2256	hostapd_change_config_freq(iface->bss[0], iface->conf,
2257				   &old_freq, NULL);
2258
2259	if (ret)
2260		return ret;
2261
2262	/* set channel switch parameters for csa ie */
2263	iface->cs_freq_params = settings->freq_params;
2264	iface->cs_count = settings->cs_count;
2265	iface->cs_block_tx = settings->block_tx;
2266
2267	ret = hostapd_build_beacon_data(iface, &settings->beacon_csa);
2268	if (ret) {
2269		free_beacon_data(&settings->beacon_after);
2270		return ret;
2271	}
2272
2273	settings->counter_offset_beacon = iface->cs_c_off_beacon;
2274	settings->counter_offset_presp = iface->cs_c_off_proberesp;
2275
2276	return 0;
2277}
2278
2279
2280void hostapd_cleanup_cs_params(struct hostapd_data *hapd)
2281{
2282	os_memset(&hapd->iface->cs_freq_params, 0,
2283		  sizeof(hapd->iface->cs_freq_params));
2284	hapd->iface->cs_count = 0;
2285	hapd->iface->cs_block_tx = 0;
2286	hapd->iface->cs_c_off_beacon = 0;
2287	hapd->iface->cs_c_off_proberesp = 0;
2288	hapd->iface->csa_in_progress = 0;
2289}
2290
2291
2292int hostapd_switch_channel(struct hostapd_data *hapd,
2293			   struct csa_settings *settings)
2294{
2295	int ret;
2296	ret = hostapd_fill_csa_settings(hapd->iface, settings);
2297	if (ret)
2298		return ret;
2299
2300	ret = hostapd_drv_switch_channel(hapd, settings);
2301	free_beacon_data(&settings->beacon_csa);
2302	free_beacon_data(&settings->beacon_after);
2303
2304	if (ret) {
2305		/* if we failed, clean cs parameters */
2306		hostapd_cleanup_cs_params(hapd);
2307		return ret;
2308	}
2309
2310	hapd->iface->csa_in_progress = 1;
2311	return 0;
2312}
2313
2314#endif /* NEED_AP_MLME */
2315