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