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 "common/hw_features_common.h"
16#include "radius/radius_client.h"
17#include "radius/radius_das.h"
18#include "eap_server/tncs.h"
19#include "eapol_auth/eapol_auth_sm.h"
20#include "eapol_auth/eapol_auth_sm_i.h"
21#include "fst/fst.h"
22#include "hostapd.h"
23#include "authsrv.h"
24#include "sta_info.h"
25#include "accounting.h"
26#include "ap_list.h"
27#include "beacon.h"
28#include "iapp.h"
29#include "ieee802_1x.h"
30#include "ieee802_11_auth.h"
31#include "vlan_init.h"
32#include "wpa_auth.h"
33#include "wps_hostapd.h"
34#include "hw_features.h"
35#include "wpa_auth_glue.h"
36#include "ap_drv_ops.h"
37#include "ap_config.h"
38#include "p2p_hostapd.h"
39#include "gas_serv.h"
40#include "dfs.h"
41#include "ieee802_11.h"
42#include "bss_load.h"
43#include "x_snoop.h"
44#include "dhcp_snoop.h"
45#include "ndisc_snoop.h"
46
47
48static int hostapd_flush_old_stations(struct hostapd_data *hapd, u16 reason);
49static int hostapd_setup_encryption(char *iface, struct hostapd_data *hapd);
50static int hostapd_broadcast_wep_clear(struct hostapd_data *hapd);
51static int setup_interface2(struct hostapd_iface *iface);
52static void channel_list_update_timeout(void *eloop_ctx, void *timeout_ctx);
53
54
55int hostapd_for_each_interface(struct hapd_interfaces *interfaces,
56			       int (*cb)(struct hostapd_iface *iface,
57					 void *ctx), void *ctx)
58{
59	size_t i;
60	int ret;
61
62	for (i = 0; i < interfaces->count; i++) {
63		ret = cb(interfaces->iface[i], ctx);
64		if (ret)
65			return ret;
66	}
67
68	return 0;
69}
70
71
72static void hostapd_reload_bss(struct hostapd_data *hapd)
73{
74	struct hostapd_ssid *ssid;
75
76#ifndef CONFIG_NO_RADIUS
77	radius_client_reconfig(hapd->radius, hapd->conf->radius);
78#endif /* CONFIG_NO_RADIUS */
79
80	ssid = &hapd->conf->ssid;
81	if (!ssid->wpa_psk_set && ssid->wpa_psk && !ssid->wpa_psk->next &&
82	    ssid->wpa_passphrase_set && ssid->wpa_passphrase) {
83		/*
84		 * Force PSK to be derived again since SSID or passphrase may
85		 * have changed.
86		 */
87		hostapd_config_clear_wpa_psk(&hapd->conf->ssid.wpa_psk);
88	}
89	if (hostapd_setup_wpa_psk(hapd->conf)) {
90		wpa_printf(MSG_ERROR, "Failed to re-configure WPA PSK "
91			   "after reloading configuration");
92	}
93
94	if (hapd->conf->ieee802_1x || hapd->conf->wpa)
95		hostapd_set_drv_ieee8021x(hapd, hapd->conf->iface, 1);
96	else
97		hostapd_set_drv_ieee8021x(hapd, hapd->conf->iface, 0);
98
99	if ((hapd->conf->wpa || hapd->conf->osen) && hapd->wpa_auth == NULL) {
100		hostapd_setup_wpa(hapd);
101		if (hapd->wpa_auth)
102			wpa_init_keys(hapd->wpa_auth);
103	} else if (hapd->conf->wpa) {
104		const u8 *wpa_ie;
105		size_t wpa_ie_len;
106		hostapd_reconfig_wpa(hapd);
107		wpa_ie = wpa_auth_get_wpa_ie(hapd->wpa_auth, &wpa_ie_len);
108		if (hostapd_set_generic_elem(hapd, wpa_ie, wpa_ie_len))
109			wpa_printf(MSG_ERROR, "Failed to configure WPA IE for "
110				   "the kernel driver.");
111	} else if (hapd->wpa_auth) {
112		wpa_deinit(hapd->wpa_auth);
113		hapd->wpa_auth = NULL;
114		hostapd_set_privacy(hapd, 0);
115		hostapd_setup_encryption(hapd->conf->iface, hapd);
116		hostapd_set_generic_elem(hapd, (u8 *) "", 0);
117	}
118
119	ieee802_11_set_beacon(hapd);
120	hostapd_update_wps(hapd);
121
122	if (hapd->conf->ssid.ssid_set &&
123	    hostapd_set_ssid(hapd, hapd->conf->ssid.ssid,
124			     hapd->conf->ssid.ssid_len)) {
125		wpa_printf(MSG_ERROR, "Could not set SSID for kernel driver");
126		/* try to continue */
127	}
128	wpa_printf(MSG_DEBUG, "Reconfigured interface %s", hapd->conf->iface);
129}
130
131
132static void hostapd_clear_old(struct hostapd_iface *iface)
133{
134	size_t j;
135
136	/*
137	 * Deauthenticate all stations since the new configuration may not
138	 * allow them to use the BSS anymore.
139	 */
140	for (j = 0; j < iface->num_bss; j++) {
141		hostapd_flush_old_stations(iface->bss[j],
142					   WLAN_REASON_PREV_AUTH_NOT_VALID);
143		hostapd_broadcast_wep_clear(iface->bss[j]);
144
145#ifndef CONFIG_NO_RADIUS
146		/* TODO: update dynamic data based on changed configuration
147		 * items (e.g., open/close sockets, etc.) */
148		radius_client_flush(iface->bss[j]->radius, 0);
149#endif /* CONFIG_NO_RADIUS */
150	}
151}
152
153
154int hostapd_reload_config(struct hostapd_iface *iface)
155{
156	struct hostapd_data *hapd = iface->bss[0];
157	struct hostapd_config *newconf, *oldconf;
158	size_t j;
159
160	if (iface->config_fname == NULL) {
161		/* Only in-memory config in use - assume it has been updated */
162		hostapd_clear_old(iface);
163		for (j = 0; j < iface->num_bss; j++)
164			hostapd_reload_bss(iface->bss[j]);
165		return 0;
166	}
167
168	if (iface->interfaces == NULL ||
169	    iface->interfaces->config_read_cb == NULL)
170		return -1;
171	newconf = iface->interfaces->config_read_cb(iface->config_fname);
172	if (newconf == NULL)
173		return -1;
174
175	hostapd_clear_old(iface);
176
177	oldconf = hapd->iconf;
178	iface->conf = newconf;
179
180	for (j = 0; j < iface->num_bss; j++) {
181		hapd = iface->bss[j];
182		hapd->iconf = newconf;
183		hapd->iconf->channel = oldconf->channel;
184		hapd->iconf->acs = oldconf->acs;
185		hapd->iconf->secondary_channel = oldconf->secondary_channel;
186		hapd->iconf->ieee80211n = oldconf->ieee80211n;
187		hapd->iconf->ieee80211ac = oldconf->ieee80211ac;
188		hapd->iconf->ht_capab = oldconf->ht_capab;
189		hapd->iconf->vht_capab = oldconf->vht_capab;
190		hapd->iconf->vht_oper_chwidth = oldconf->vht_oper_chwidth;
191		hapd->iconf->vht_oper_centr_freq_seg0_idx =
192			oldconf->vht_oper_centr_freq_seg0_idx;
193		hapd->iconf->vht_oper_centr_freq_seg1_idx =
194			oldconf->vht_oper_centr_freq_seg1_idx;
195		hapd->conf = newconf->bss[j];
196		hostapd_reload_bss(hapd);
197	}
198
199	hostapd_config_free(oldconf);
200
201
202	return 0;
203}
204
205
206static void hostapd_broadcast_key_clear_iface(struct hostapd_data *hapd,
207					      char *ifname)
208{
209	int i;
210
211	for (i = 0; i < NUM_WEP_KEYS; i++) {
212		if (hostapd_drv_set_key(ifname, hapd, WPA_ALG_NONE, NULL, i,
213					0, NULL, 0, NULL, 0)) {
214			wpa_printf(MSG_DEBUG, "Failed to clear default "
215				   "encryption keys (ifname=%s keyidx=%d)",
216				   ifname, i);
217		}
218	}
219#ifdef CONFIG_IEEE80211W
220	if (hapd->conf->ieee80211w) {
221		for (i = NUM_WEP_KEYS; i < NUM_WEP_KEYS + 2; i++) {
222			if (hostapd_drv_set_key(ifname, hapd, WPA_ALG_NONE,
223						NULL, i, 0, NULL,
224						0, NULL, 0)) {
225				wpa_printf(MSG_DEBUG, "Failed to clear "
226					   "default mgmt encryption keys "
227					   "(ifname=%s keyidx=%d)", ifname, i);
228			}
229		}
230	}
231#endif /* CONFIG_IEEE80211W */
232}
233
234
235static int hostapd_broadcast_wep_clear(struct hostapd_data *hapd)
236{
237	hostapd_broadcast_key_clear_iface(hapd, hapd->conf->iface);
238	return 0;
239}
240
241
242static int hostapd_broadcast_wep_set(struct hostapd_data *hapd)
243{
244	int errors = 0, idx;
245	struct hostapd_ssid *ssid = &hapd->conf->ssid;
246
247	idx = ssid->wep.idx;
248	if (ssid->wep.default_len &&
249	    hostapd_drv_set_key(hapd->conf->iface,
250				hapd, WPA_ALG_WEP, broadcast_ether_addr, idx,
251				1, NULL, 0, ssid->wep.key[idx],
252				ssid->wep.len[idx])) {
253		wpa_printf(MSG_WARNING, "Could not set WEP encryption.");
254		errors++;
255	}
256
257	return errors;
258}
259
260
261static void hostapd_free_hapd_data(struct hostapd_data *hapd)
262{
263	os_free(hapd->probereq_cb);
264	hapd->probereq_cb = NULL;
265	hapd->num_probereq_cb = 0;
266
267#ifdef CONFIG_P2P
268	wpabuf_free(hapd->p2p_beacon_ie);
269	hapd->p2p_beacon_ie = NULL;
270	wpabuf_free(hapd->p2p_probe_resp_ie);
271	hapd->p2p_probe_resp_ie = NULL;
272#endif /* CONFIG_P2P */
273
274	if (!hapd->started) {
275		wpa_printf(MSG_ERROR, "%s: Interface %s wasn't started",
276			   __func__, hapd->conf->iface);
277		return;
278	}
279	hapd->started = 0;
280
281	wpa_printf(MSG_DEBUG, "%s(%s)", __func__, hapd->conf->iface);
282	iapp_deinit(hapd->iapp);
283	hapd->iapp = NULL;
284	accounting_deinit(hapd);
285	hostapd_deinit_wpa(hapd);
286	vlan_deinit(hapd);
287	hostapd_acl_deinit(hapd);
288#ifndef CONFIG_NO_RADIUS
289	radius_client_deinit(hapd->radius);
290	hapd->radius = NULL;
291	radius_das_deinit(hapd->radius_das);
292	hapd->radius_das = NULL;
293#endif /* CONFIG_NO_RADIUS */
294
295	hostapd_deinit_wps(hapd);
296
297	authsrv_deinit(hapd);
298
299	if (hapd->interface_added) {
300		hapd->interface_added = 0;
301		if (hostapd_if_remove(hapd, WPA_IF_AP_BSS, hapd->conf->iface)) {
302			wpa_printf(MSG_WARNING,
303				   "Failed to remove BSS interface %s",
304				   hapd->conf->iface);
305			hapd->interface_added = 1;
306		} else {
307			/*
308			 * Since this was a dynamically added interface, the
309			 * driver wrapper may have removed its internal instance
310			 * and hapd->drv_priv is not valid anymore.
311			 */
312			hapd->drv_priv = NULL;
313		}
314	}
315
316	wpabuf_free(hapd->time_adv);
317
318#ifdef CONFIG_INTERWORKING
319	gas_serv_deinit(hapd);
320#endif /* CONFIG_INTERWORKING */
321
322	bss_load_update_deinit(hapd);
323	ndisc_snoop_deinit(hapd);
324	dhcp_snoop_deinit(hapd);
325	x_snoop_deinit(hapd);
326
327#ifdef CONFIG_SQLITE
328	bin_clear_free(hapd->tmp_eap_user.identity,
329		       hapd->tmp_eap_user.identity_len);
330	bin_clear_free(hapd->tmp_eap_user.password,
331		       hapd->tmp_eap_user.password_len);
332#endif /* CONFIG_SQLITE */
333
334#ifdef CONFIG_MESH
335	wpabuf_free(hapd->mesh_pending_auth);
336	hapd->mesh_pending_auth = NULL;
337#endif /* CONFIG_MESH */
338}
339
340
341/**
342 * hostapd_cleanup - Per-BSS cleanup (deinitialization)
343 * @hapd: Pointer to BSS data
344 *
345 * This function is used to free all per-BSS data structures and resources.
346 * Most of the modules that are initialized in hostapd_setup_bss() are
347 * deinitialized here.
348 */
349static void hostapd_cleanup(struct hostapd_data *hapd)
350{
351	wpa_printf(MSG_DEBUG, "%s(hapd=%p (%s))", __func__, hapd,
352		   hapd->conf->iface);
353	if (hapd->iface->interfaces &&
354	    hapd->iface->interfaces->ctrl_iface_deinit)
355		hapd->iface->interfaces->ctrl_iface_deinit(hapd);
356	hostapd_free_hapd_data(hapd);
357}
358
359
360static void sta_track_deinit(struct hostapd_iface *iface)
361{
362	struct hostapd_sta_info *info;
363
364	if (!iface->num_sta_seen)
365		return;
366
367	while ((info = dl_list_first(&iface->sta_seen, struct hostapd_sta_info,
368				     list))) {
369		dl_list_del(&info->list);
370		iface->num_sta_seen--;
371		os_free(info);
372	}
373}
374
375
376static void hostapd_cleanup_iface_partial(struct hostapd_iface *iface)
377{
378	wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
379#ifdef CONFIG_IEEE80211N
380#ifdef NEED_AP_MLME
381	hostapd_stop_setup_timers(iface);
382#endif /* NEED_AP_MLME */
383#endif /* CONFIG_IEEE80211N */
384	hostapd_free_hw_features(iface->hw_features, iface->num_hw_features);
385	iface->hw_features = NULL;
386	os_free(iface->current_rates);
387	iface->current_rates = NULL;
388	os_free(iface->basic_rates);
389	iface->basic_rates = NULL;
390	ap_list_deinit(iface);
391	sta_track_deinit(iface);
392}
393
394
395/**
396 * hostapd_cleanup_iface - Complete per-interface cleanup
397 * @iface: Pointer to interface data
398 *
399 * This function is called after per-BSS data structures are deinitialized
400 * with hostapd_cleanup().
401 */
402static void hostapd_cleanup_iface(struct hostapd_iface *iface)
403{
404	wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
405	eloop_cancel_timeout(channel_list_update_timeout, iface, NULL);
406
407	hostapd_cleanup_iface_partial(iface);
408	hostapd_config_free(iface->conf);
409	iface->conf = NULL;
410
411	os_free(iface->config_fname);
412	os_free(iface->bss);
413	wpa_printf(MSG_DEBUG, "%s: free iface=%p", __func__, iface);
414	os_free(iface);
415}
416
417
418static void hostapd_clear_wep(struct hostapd_data *hapd)
419{
420	if (hapd->drv_priv && !hapd->iface->driver_ap_teardown) {
421		hostapd_set_privacy(hapd, 0);
422		hostapd_broadcast_wep_clear(hapd);
423	}
424}
425
426
427static int hostapd_setup_encryption(char *iface, struct hostapd_data *hapd)
428{
429	int i;
430
431	hostapd_broadcast_wep_set(hapd);
432
433	if (hapd->conf->ssid.wep.default_len) {
434		hostapd_set_privacy(hapd, 1);
435		return 0;
436	}
437
438	/*
439	 * When IEEE 802.1X is not enabled, the driver may need to know how to
440	 * set authentication algorithms for static WEP.
441	 */
442	hostapd_drv_set_authmode(hapd, hapd->conf->auth_algs);
443
444	for (i = 0; i < 4; i++) {
445		if (hapd->conf->ssid.wep.key[i] &&
446		    hostapd_drv_set_key(iface, hapd, WPA_ALG_WEP, NULL, i,
447					i == hapd->conf->ssid.wep.idx, NULL, 0,
448					hapd->conf->ssid.wep.key[i],
449					hapd->conf->ssid.wep.len[i])) {
450			wpa_printf(MSG_WARNING, "Could not set WEP "
451				   "encryption.");
452			return -1;
453		}
454		if (hapd->conf->ssid.wep.key[i] &&
455		    i == hapd->conf->ssid.wep.idx)
456			hostapd_set_privacy(hapd, 1);
457	}
458
459	return 0;
460}
461
462
463static int hostapd_flush_old_stations(struct hostapd_data *hapd, u16 reason)
464{
465	int ret = 0;
466	u8 addr[ETH_ALEN];
467
468	if (hostapd_drv_none(hapd) || hapd->drv_priv == NULL)
469		return 0;
470
471	if (!hapd->iface->driver_ap_teardown) {
472		wpa_dbg(hapd->msg_ctx, MSG_DEBUG,
473			"Flushing old station entries");
474
475		if (hostapd_flush(hapd)) {
476			wpa_msg(hapd->msg_ctx, MSG_WARNING,
477				"Could not connect to kernel driver");
478			ret = -1;
479		}
480	}
481	wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "Deauthenticate all stations");
482	os_memset(addr, 0xff, ETH_ALEN);
483	hostapd_drv_sta_deauth(hapd, addr, reason);
484	hostapd_free_stas(hapd);
485
486	return ret;
487}
488
489
490static void hostapd_bss_deinit_no_free(struct hostapd_data *hapd)
491{
492	hostapd_free_stas(hapd);
493	hostapd_flush_old_stations(hapd, WLAN_REASON_DEAUTH_LEAVING);
494	hostapd_clear_wep(hapd);
495}
496
497
498/**
499 * hostapd_validate_bssid_configuration - Validate BSSID configuration
500 * @iface: Pointer to interface data
501 * Returns: 0 on success, -1 on failure
502 *
503 * This function is used to validate that the configured BSSIDs are valid.
504 */
505static int hostapd_validate_bssid_configuration(struct hostapd_iface *iface)
506{
507	u8 mask[ETH_ALEN] = { 0 };
508	struct hostapd_data *hapd = iface->bss[0];
509	unsigned int i = iface->conf->num_bss, bits = 0, j;
510	int auto_addr = 0;
511
512	if (hostapd_drv_none(hapd))
513		return 0;
514
515	if (iface->conf->use_driver_iface_addr)
516		return 0;
517
518	/* Generate BSSID mask that is large enough to cover the BSSIDs. */
519
520	/* Determine the bits necessary to cover the number of BSSIDs. */
521	for (i--; i; i >>= 1)
522		bits++;
523
524	/* Determine the bits necessary to any configured BSSIDs,
525	   if they are higher than the number of BSSIDs. */
526	for (j = 0; j < iface->conf->num_bss; j++) {
527		if (is_zero_ether_addr(iface->conf->bss[j]->bssid)) {
528			if (j)
529				auto_addr++;
530			continue;
531		}
532
533		for (i = 0; i < ETH_ALEN; i++) {
534			mask[i] |=
535				iface->conf->bss[j]->bssid[i] ^
536				hapd->own_addr[i];
537		}
538	}
539
540	if (!auto_addr)
541		goto skip_mask_ext;
542
543	for (i = 0; i < ETH_ALEN && mask[i] == 0; i++)
544		;
545	j = 0;
546	if (i < ETH_ALEN) {
547		j = (5 - i) * 8;
548
549		while (mask[i] != 0) {
550			mask[i] >>= 1;
551			j++;
552		}
553	}
554
555	if (bits < j)
556		bits = j;
557
558	if (bits > 40) {
559		wpa_printf(MSG_ERROR, "Too many bits in the BSSID mask (%u)",
560			   bits);
561		return -1;
562	}
563
564	os_memset(mask, 0xff, ETH_ALEN);
565	j = bits / 8;
566	for (i = 5; i > 5 - j; i--)
567		mask[i] = 0;
568	j = bits % 8;
569	while (j--)
570		mask[i] <<= 1;
571
572skip_mask_ext:
573	wpa_printf(MSG_DEBUG, "BSS count %lu, BSSID mask " MACSTR " (%d bits)",
574		   (unsigned long) iface->conf->num_bss, MAC2STR(mask), bits);
575
576	if (!auto_addr)
577		return 0;
578
579	for (i = 0; i < ETH_ALEN; i++) {
580		if ((hapd->own_addr[i] & mask[i]) != hapd->own_addr[i]) {
581			wpa_printf(MSG_ERROR, "Invalid BSSID mask " MACSTR
582				   " for start address " MACSTR ".",
583				   MAC2STR(mask), MAC2STR(hapd->own_addr));
584			wpa_printf(MSG_ERROR, "Start address must be the "
585				   "first address in the block (i.e., addr "
586				   "AND mask == addr).");
587			return -1;
588		}
589	}
590
591	return 0;
592}
593
594
595static int mac_in_conf(struct hostapd_config *conf, const void *a)
596{
597	size_t i;
598
599	for (i = 0; i < conf->num_bss; i++) {
600		if (hostapd_mac_comp(conf->bss[i]->bssid, a) == 0) {
601			return 1;
602		}
603	}
604
605	return 0;
606}
607
608
609#ifndef CONFIG_NO_RADIUS
610
611static int hostapd_das_nas_mismatch(struct hostapd_data *hapd,
612				    struct radius_das_attrs *attr)
613{
614	if (attr->nas_identifier &&
615	    (!hapd->conf->nas_identifier ||
616	     os_strlen(hapd->conf->nas_identifier) !=
617	     attr->nas_identifier_len ||
618	     os_memcmp(hapd->conf->nas_identifier, attr->nas_identifier,
619		       attr->nas_identifier_len) != 0)) {
620		wpa_printf(MSG_DEBUG, "RADIUS DAS: NAS-Identifier mismatch");
621		return 1;
622	}
623
624	if (attr->nas_ip_addr &&
625	    (hapd->conf->own_ip_addr.af != AF_INET ||
626	     os_memcmp(&hapd->conf->own_ip_addr.u.v4, attr->nas_ip_addr, 4) !=
627	     0)) {
628		wpa_printf(MSG_DEBUG, "RADIUS DAS: NAS-IP-Address mismatch");
629		return 1;
630	}
631
632#ifdef CONFIG_IPV6
633	if (attr->nas_ipv6_addr &&
634	    (hapd->conf->own_ip_addr.af != AF_INET6 ||
635	     os_memcmp(&hapd->conf->own_ip_addr.u.v6, attr->nas_ipv6_addr, 16)
636	     != 0)) {
637		wpa_printf(MSG_DEBUG, "RADIUS DAS: NAS-IPv6-Address mismatch");
638		return 1;
639	}
640#endif /* CONFIG_IPV6 */
641
642	return 0;
643}
644
645
646static struct sta_info * hostapd_das_find_sta(struct hostapd_data *hapd,
647					      struct radius_das_attrs *attr,
648					      int *multi)
649{
650	struct sta_info *selected, *sta;
651	char buf[128];
652	int num_attr = 0;
653	int count;
654
655	*multi = 0;
656
657	for (sta = hapd->sta_list; sta; sta = sta->next)
658		sta->radius_das_match = 1;
659
660	if (attr->sta_addr) {
661		num_attr++;
662		sta = ap_get_sta(hapd, attr->sta_addr);
663		if (!sta) {
664			wpa_printf(MSG_DEBUG,
665				   "RADIUS DAS: No Calling-Station-Id match");
666			return NULL;
667		}
668
669		selected = sta;
670		for (sta = hapd->sta_list; sta; sta = sta->next) {
671			if (sta != selected)
672				sta->radius_das_match = 0;
673		}
674		wpa_printf(MSG_DEBUG, "RADIUS DAS: Calling-Station-Id match");
675	}
676
677	if (attr->acct_session_id) {
678		num_attr++;
679		if (attr->acct_session_id_len != 16) {
680			wpa_printf(MSG_DEBUG,
681				   "RADIUS DAS: Acct-Session-Id cannot match");
682			return NULL;
683		}
684		count = 0;
685
686		for (sta = hapd->sta_list; sta; sta = sta->next) {
687			if (!sta->radius_das_match)
688				continue;
689			os_snprintf(buf, sizeof(buf), "%016llX",
690				    (unsigned long long) sta->acct_session_id);
691			if (os_memcmp(attr->acct_session_id, buf, 16) != 0)
692				sta->radius_das_match = 0;
693			else
694				count++;
695		}
696
697		if (count == 0) {
698			wpa_printf(MSG_DEBUG,
699				   "RADIUS DAS: No matches remaining after Acct-Session-Id check");
700			return NULL;
701		}
702		wpa_printf(MSG_DEBUG, "RADIUS DAS: Acct-Session-Id match");
703	}
704
705	if (attr->acct_multi_session_id) {
706		num_attr++;
707		if (attr->acct_multi_session_id_len != 16) {
708			wpa_printf(MSG_DEBUG,
709				   "RADIUS DAS: Acct-Multi-Session-Id cannot match");
710			return NULL;
711		}
712		count = 0;
713
714		for (sta = hapd->sta_list; sta; sta = sta->next) {
715			if (!sta->radius_das_match)
716				continue;
717			if (!sta->eapol_sm ||
718			    !sta->eapol_sm->acct_multi_session_id) {
719				sta->radius_das_match = 0;
720				continue;
721			}
722			os_snprintf(buf, sizeof(buf), "%016llX",
723				    (unsigned long long)
724				    sta->eapol_sm->acct_multi_session_id);
725			if (os_memcmp(attr->acct_multi_session_id, buf, 16) !=
726			    0)
727				sta->radius_das_match = 0;
728			else
729				count++;
730		}
731
732		if (count == 0) {
733			wpa_printf(MSG_DEBUG,
734				   "RADIUS DAS: No matches remaining after Acct-Multi-Session-Id check");
735			return NULL;
736		}
737		wpa_printf(MSG_DEBUG,
738			   "RADIUS DAS: Acct-Multi-Session-Id match");
739	}
740
741	if (attr->cui) {
742		num_attr++;
743		count = 0;
744
745		for (sta = hapd->sta_list; sta; sta = sta->next) {
746			struct wpabuf *cui;
747
748			if (!sta->radius_das_match)
749				continue;
750			cui = ieee802_1x_get_radius_cui(sta->eapol_sm);
751			if (!cui || wpabuf_len(cui) != attr->cui_len ||
752			    os_memcmp(wpabuf_head(cui), attr->cui,
753				      attr->cui_len) != 0)
754				sta->radius_das_match = 0;
755			else
756				count++;
757		}
758
759		if (count == 0) {
760			wpa_printf(MSG_DEBUG,
761				   "RADIUS DAS: No matches remaining after Chargeable-User-Identity check");
762			return NULL;
763		}
764		wpa_printf(MSG_DEBUG,
765			   "RADIUS DAS: Chargeable-User-Identity match");
766	}
767
768	if (attr->user_name) {
769		num_attr++;
770		count = 0;
771
772		for (sta = hapd->sta_list; sta; sta = sta->next) {
773			u8 *identity;
774			size_t identity_len;
775
776			if (!sta->radius_das_match)
777				continue;
778			identity = ieee802_1x_get_identity(sta->eapol_sm,
779							   &identity_len);
780			if (!identity ||
781			    identity_len != attr->user_name_len ||
782			    os_memcmp(identity, attr->user_name, identity_len)
783			    != 0)
784				sta->radius_das_match = 0;
785			else
786				count++;
787		}
788
789		if (count == 0) {
790			wpa_printf(MSG_DEBUG,
791				   "RADIUS DAS: No matches remaining after User-Name check");
792			return NULL;
793		}
794		wpa_printf(MSG_DEBUG,
795			   "RADIUS DAS: User-Name match");
796	}
797
798	if (num_attr == 0) {
799		/*
800		 * In theory, we could match all current associations, but it
801		 * seems safer to just reject requests that do not include any
802		 * session identification attributes.
803		 */
804		wpa_printf(MSG_DEBUG,
805			   "RADIUS DAS: No session identification attributes included");
806		return NULL;
807	}
808
809	selected = NULL;
810	for (sta = hapd->sta_list; sta; sta = sta->next) {
811		if (sta->radius_das_match) {
812			if (selected) {
813				*multi = 1;
814				return NULL;
815			}
816			selected = sta;
817		}
818	}
819
820	return selected;
821}
822
823
824static int hostapd_das_disconnect_pmksa(struct hostapd_data *hapd,
825					struct radius_das_attrs *attr)
826{
827	if (!hapd->wpa_auth)
828		return -1;
829	return wpa_auth_radius_das_disconnect_pmksa(hapd->wpa_auth, attr);
830}
831
832
833static enum radius_das_res
834hostapd_das_disconnect(void *ctx, struct radius_das_attrs *attr)
835{
836	struct hostapd_data *hapd = ctx;
837	struct sta_info *sta;
838	int multi;
839
840	if (hostapd_das_nas_mismatch(hapd, attr))
841		return RADIUS_DAS_NAS_MISMATCH;
842
843	sta = hostapd_das_find_sta(hapd, attr, &multi);
844	if (sta == NULL) {
845		if (multi) {
846			wpa_printf(MSG_DEBUG,
847				   "RADIUS DAS: Multiple sessions match - not supported");
848			return RADIUS_DAS_MULTI_SESSION_MATCH;
849		}
850		if (hostapd_das_disconnect_pmksa(hapd, attr) == 0) {
851			wpa_printf(MSG_DEBUG,
852				   "RADIUS DAS: PMKSA cache entry matched");
853			return RADIUS_DAS_SUCCESS;
854		}
855		wpa_printf(MSG_DEBUG, "RADIUS DAS: No matching session found");
856		return RADIUS_DAS_SESSION_NOT_FOUND;
857	}
858
859	wpa_printf(MSG_DEBUG, "RADIUS DAS: Found a matching session " MACSTR
860		   " - disconnecting", MAC2STR(sta->addr));
861	wpa_auth_pmksa_remove(hapd->wpa_auth, sta->addr);
862
863	hostapd_drv_sta_deauth(hapd, sta->addr,
864			       WLAN_REASON_PREV_AUTH_NOT_VALID);
865	ap_sta_deauthenticate(hapd, sta, WLAN_REASON_PREV_AUTH_NOT_VALID);
866
867	return RADIUS_DAS_SUCCESS;
868}
869
870#endif /* CONFIG_NO_RADIUS */
871
872
873/**
874 * hostapd_setup_bss - Per-BSS setup (initialization)
875 * @hapd: Pointer to BSS data
876 * @first: Whether this BSS is the first BSS of an interface; -1 = not first,
877 *	but interface may exist
878 *
879 * This function is used to initialize all per-BSS data structures and
880 * resources. This gets called in a loop for each BSS when an interface is
881 * initialized. Most of the modules that are initialized here will be
882 * deinitialized in hostapd_cleanup().
883 */
884static int hostapd_setup_bss(struct hostapd_data *hapd, int first)
885{
886	struct hostapd_bss_config *conf = hapd->conf;
887	u8 ssid[SSID_MAX_LEN + 1];
888	int ssid_len, set_ssid;
889	char force_ifname[IFNAMSIZ];
890	u8 if_addr[ETH_ALEN];
891	int flush_old_stations = 1;
892
893	wpa_printf(MSG_DEBUG, "%s(hapd=%p (%s), first=%d)",
894		   __func__, hapd, conf->iface, first);
895
896#ifdef EAP_SERVER_TNC
897	if (conf->tnc && tncs_global_init() < 0) {
898		wpa_printf(MSG_ERROR, "Failed to initialize TNCS");
899		return -1;
900	}
901#endif /* EAP_SERVER_TNC */
902
903	if (hapd->started) {
904		wpa_printf(MSG_ERROR, "%s: Interface %s was already started",
905			   __func__, conf->iface);
906		return -1;
907	}
908	hapd->started = 1;
909
910	if (!first || first == -1) {
911		u8 *addr = hapd->own_addr;
912
913		if (!is_zero_ether_addr(conf->bssid)) {
914			/* Allocate the configured BSSID. */
915			os_memcpy(hapd->own_addr, conf->bssid, ETH_ALEN);
916
917			if (hostapd_mac_comp(hapd->own_addr,
918					     hapd->iface->bss[0]->own_addr) ==
919			    0) {
920				wpa_printf(MSG_ERROR, "BSS '%s' may not have "
921					   "BSSID set to the MAC address of "
922					   "the radio", conf->iface);
923				return -1;
924			}
925		} else if (hapd->iconf->use_driver_iface_addr) {
926			addr = NULL;
927		} else {
928			/* Allocate the next available BSSID. */
929			do {
930				inc_byte_array(hapd->own_addr, ETH_ALEN);
931			} while (mac_in_conf(hapd->iconf, hapd->own_addr));
932		}
933
934		hapd->interface_added = 1;
935		if (hostapd_if_add(hapd->iface->bss[0], WPA_IF_AP_BSS,
936				   conf->iface, addr, hapd,
937				   &hapd->drv_priv, force_ifname, if_addr,
938				   conf->bridge[0] ? conf->bridge : NULL,
939				   first == -1)) {
940			wpa_printf(MSG_ERROR, "Failed to add BSS (BSSID="
941				   MACSTR ")", MAC2STR(hapd->own_addr));
942			hapd->interface_added = 0;
943			return -1;
944		}
945
946		if (!addr)
947			os_memcpy(hapd->own_addr, if_addr, ETH_ALEN);
948	}
949
950	if (conf->wmm_enabled < 0)
951		conf->wmm_enabled = hapd->iconf->ieee80211n;
952
953#ifdef CONFIG_IEEE80211R
954	if (is_zero_ether_addr(conf->r1_key_holder))
955		os_memcpy(conf->r1_key_holder, hapd->own_addr, ETH_ALEN);
956#endif /* CONFIG_IEEE80211R */
957
958#ifdef CONFIG_MESH
959	if (hapd->iface->mconf == NULL)
960		flush_old_stations = 0;
961#endif /* CONFIG_MESH */
962
963	if (flush_old_stations)
964		hostapd_flush_old_stations(hapd,
965					   WLAN_REASON_PREV_AUTH_NOT_VALID);
966	hostapd_set_privacy(hapd, 0);
967
968	hostapd_broadcast_wep_clear(hapd);
969	if (hostapd_setup_encryption(conf->iface, hapd))
970		return -1;
971
972	/*
973	 * Fetch the SSID from the system and use it or,
974	 * if one was specified in the config file, verify they
975	 * match.
976	 */
977	ssid_len = hostapd_get_ssid(hapd, ssid, sizeof(ssid));
978	if (ssid_len < 0) {
979		wpa_printf(MSG_ERROR, "Could not read SSID from system");
980		return -1;
981	}
982	if (conf->ssid.ssid_set) {
983		/*
984		 * If SSID is specified in the config file and it differs
985		 * from what is being used then force installation of the
986		 * new SSID.
987		 */
988		set_ssid = (conf->ssid.ssid_len != (size_t) ssid_len ||
989			    os_memcmp(conf->ssid.ssid, ssid, ssid_len) != 0);
990	} else {
991		/*
992		 * No SSID in the config file; just use the one we got
993		 * from the system.
994		 */
995		set_ssid = 0;
996		conf->ssid.ssid_len = ssid_len;
997		os_memcpy(conf->ssid.ssid, ssid, conf->ssid.ssid_len);
998	}
999
1000	if (!hostapd_drv_none(hapd)) {
1001		wpa_printf(MSG_ERROR, "Using interface %s with hwaddr " MACSTR
1002			   " and ssid \"%s\"",
1003			   conf->iface, MAC2STR(hapd->own_addr),
1004			   wpa_ssid_txt(conf->ssid.ssid, conf->ssid.ssid_len));
1005	}
1006
1007	if (hostapd_setup_wpa_psk(conf)) {
1008		wpa_printf(MSG_ERROR, "WPA-PSK setup failed.");
1009		return -1;
1010	}
1011
1012	/* Set SSID for the kernel driver (to be used in beacon and probe
1013	 * response frames) */
1014	if (set_ssid && hostapd_set_ssid(hapd, conf->ssid.ssid,
1015					 conf->ssid.ssid_len)) {
1016		wpa_printf(MSG_ERROR, "Could not set SSID for kernel driver");
1017		return -1;
1018	}
1019
1020	if (wpa_debug_level <= MSG_MSGDUMP)
1021		conf->radius->msg_dumps = 1;
1022#ifndef CONFIG_NO_RADIUS
1023	hapd->radius = radius_client_init(hapd, conf->radius);
1024	if (hapd->radius == NULL) {
1025		wpa_printf(MSG_ERROR, "RADIUS client initialization failed.");
1026		return -1;
1027	}
1028
1029	if (conf->radius_das_port) {
1030		struct radius_das_conf das_conf;
1031		os_memset(&das_conf, 0, sizeof(das_conf));
1032		das_conf.port = conf->radius_das_port;
1033		das_conf.shared_secret = conf->radius_das_shared_secret;
1034		das_conf.shared_secret_len =
1035			conf->radius_das_shared_secret_len;
1036		das_conf.client_addr = &conf->radius_das_client_addr;
1037		das_conf.time_window = conf->radius_das_time_window;
1038		das_conf.require_event_timestamp =
1039			conf->radius_das_require_event_timestamp;
1040		das_conf.ctx = hapd;
1041		das_conf.disconnect = hostapd_das_disconnect;
1042		hapd->radius_das = radius_das_init(&das_conf);
1043		if (hapd->radius_das == NULL) {
1044			wpa_printf(MSG_ERROR, "RADIUS DAS initialization "
1045				   "failed.");
1046			return -1;
1047		}
1048	}
1049#endif /* CONFIG_NO_RADIUS */
1050
1051	if (hostapd_acl_init(hapd)) {
1052		wpa_printf(MSG_ERROR, "ACL initialization failed.");
1053		return -1;
1054	}
1055	if (hostapd_init_wps(hapd, conf))
1056		return -1;
1057
1058	if (authsrv_init(hapd) < 0)
1059		return -1;
1060
1061	if (ieee802_1x_init(hapd)) {
1062		wpa_printf(MSG_ERROR, "IEEE 802.1X initialization failed.");
1063		return -1;
1064	}
1065
1066	if ((conf->wpa || conf->osen) && hostapd_setup_wpa(hapd))
1067		return -1;
1068
1069	if (accounting_init(hapd)) {
1070		wpa_printf(MSG_ERROR, "Accounting initialization failed.");
1071		return -1;
1072	}
1073
1074	if (conf->ieee802_11f &&
1075	    (hapd->iapp = iapp_init(hapd, conf->iapp_iface)) == NULL) {
1076		wpa_printf(MSG_ERROR, "IEEE 802.11F (IAPP) initialization "
1077			   "failed.");
1078		return -1;
1079	}
1080
1081#ifdef CONFIG_INTERWORKING
1082	if (gas_serv_init(hapd)) {
1083		wpa_printf(MSG_ERROR, "GAS server initialization failed");
1084		return -1;
1085	}
1086
1087	if (conf->qos_map_set_len &&
1088	    hostapd_drv_set_qos_map(hapd, conf->qos_map_set,
1089				    conf->qos_map_set_len)) {
1090		wpa_printf(MSG_ERROR, "Failed to initialize QoS Map");
1091		return -1;
1092	}
1093#endif /* CONFIG_INTERWORKING */
1094
1095	if (conf->bss_load_update_period && bss_load_update_init(hapd)) {
1096		wpa_printf(MSG_ERROR, "BSS Load initialization failed");
1097		return -1;
1098	}
1099
1100	if (conf->proxy_arp) {
1101		if (x_snoop_init(hapd)) {
1102			wpa_printf(MSG_ERROR,
1103				   "Generic snooping infrastructure initialization failed");
1104			return -1;
1105		}
1106
1107		if (dhcp_snoop_init(hapd)) {
1108			wpa_printf(MSG_ERROR,
1109				   "DHCP snooping initialization failed");
1110			return -1;
1111		}
1112
1113		if (ndisc_snoop_init(hapd)) {
1114			wpa_printf(MSG_ERROR,
1115				   "Neighbor Discovery snooping initialization failed");
1116			return -1;
1117		}
1118	}
1119
1120	if (!hostapd_drv_none(hapd) && vlan_init(hapd)) {
1121		wpa_printf(MSG_ERROR, "VLAN initialization failed.");
1122		return -1;
1123	}
1124
1125	if (!conf->start_disabled && ieee802_11_set_beacon(hapd) < 0)
1126		return -1;
1127
1128	if (hapd->wpa_auth && wpa_init_keys(hapd->wpa_auth) < 0)
1129		return -1;
1130
1131	if (hapd->driver && hapd->driver->set_operstate)
1132		hapd->driver->set_operstate(hapd->drv_priv, 1);
1133
1134	return 0;
1135}
1136
1137
1138static void hostapd_tx_queue_params(struct hostapd_iface *iface)
1139{
1140	struct hostapd_data *hapd = iface->bss[0];
1141	int i;
1142	struct hostapd_tx_queue_params *p;
1143
1144#ifdef CONFIG_MESH
1145	if (iface->mconf == NULL)
1146		return;
1147#endif /* CONFIG_MESH */
1148
1149	for (i = 0; i < NUM_TX_QUEUES; i++) {
1150		p = &iface->conf->tx_queue[i];
1151
1152		if (hostapd_set_tx_queue_params(hapd, i, p->aifs, p->cwmin,
1153						p->cwmax, p->burst)) {
1154			wpa_printf(MSG_DEBUG, "Failed to set TX queue "
1155				   "parameters for queue %d.", i);
1156			/* Continue anyway */
1157		}
1158	}
1159}
1160
1161
1162static int hostapd_set_acl_list(struct hostapd_data *hapd,
1163				struct mac_acl_entry *mac_acl,
1164				int n_entries, u8 accept_acl)
1165{
1166	struct hostapd_acl_params *acl_params;
1167	int i, err;
1168
1169	acl_params = os_zalloc(sizeof(*acl_params) +
1170			       (n_entries * sizeof(acl_params->mac_acl[0])));
1171	if (!acl_params)
1172		return -ENOMEM;
1173
1174	for (i = 0; i < n_entries; i++)
1175		os_memcpy(acl_params->mac_acl[i].addr, mac_acl[i].addr,
1176			  ETH_ALEN);
1177
1178	acl_params->acl_policy = accept_acl;
1179	acl_params->num_mac_acl = n_entries;
1180
1181	err = hostapd_drv_set_acl(hapd, acl_params);
1182
1183	os_free(acl_params);
1184
1185	return err;
1186}
1187
1188
1189static void hostapd_set_acl(struct hostapd_data *hapd)
1190{
1191	struct hostapd_config *conf = hapd->iconf;
1192	int err;
1193	u8 accept_acl;
1194
1195	if (hapd->iface->drv_max_acl_mac_addrs == 0)
1196		return;
1197
1198	if (conf->bss[0]->macaddr_acl == DENY_UNLESS_ACCEPTED) {
1199		accept_acl = 1;
1200		err = hostapd_set_acl_list(hapd, conf->bss[0]->accept_mac,
1201					   conf->bss[0]->num_accept_mac,
1202					   accept_acl);
1203		if (err) {
1204			wpa_printf(MSG_DEBUG, "Failed to set accept acl");
1205			return;
1206		}
1207	} else if (conf->bss[0]->macaddr_acl == ACCEPT_UNLESS_DENIED) {
1208		accept_acl = 0;
1209		err = hostapd_set_acl_list(hapd, conf->bss[0]->deny_mac,
1210					   conf->bss[0]->num_deny_mac,
1211					   accept_acl);
1212		if (err) {
1213			wpa_printf(MSG_DEBUG, "Failed to set deny acl");
1214			return;
1215		}
1216	}
1217}
1218
1219
1220static int start_ctrl_iface_bss(struct hostapd_data *hapd)
1221{
1222	if (!hapd->iface->interfaces ||
1223	    !hapd->iface->interfaces->ctrl_iface_init)
1224		return 0;
1225
1226	if (hapd->iface->interfaces->ctrl_iface_init(hapd)) {
1227		wpa_printf(MSG_ERROR,
1228			   "Failed to setup control interface for %s",
1229			   hapd->conf->iface);
1230		return -1;
1231	}
1232
1233	return 0;
1234}
1235
1236
1237static int start_ctrl_iface(struct hostapd_iface *iface)
1238{
1239	size_t i;
1240
1241	if (!iface->interfaces || !iface->interfaces->ctrl_iface_init)
1242		return 0;
1243
1244	for (i = 0; i < iface->num_bss; i++) {
1245		struct hostapd_data *hapd = iface->bss[i];
1246		if (iface->interfaces->ctrl_iface_init(hapd)) {
1247			wpa_printf(MSG_ERROR,
1248				   "Failed to setup control interface for %s",
1249				   hapd->conf->iface);
1250			return -1;
1251		}
1252	}
1253
1254	return 0;
1255}
1256
1257
1258static void channel_list_update_timeout(void *eloop_ctx, void *timeout_ctx)
1259{
1260	struct hostapd_iface *iface = eloop_ctx;
1261
1262	if (!iface->wait_channel_update) {
1263		wpa_printf(MSG_INFO, "Channel list update timeout, but interface was not waiting for it");
1264		return;
1265	}
1266
1267	/*
1268	 * It is possible that the existing channel list is acceptable, so try
1269	 * to proceed.
1270	 */
1271	wpa_printf(MSG_DEBUG, "Channel list update timeout - try to continue anyway");
1272	setup_interface2(iface);
1273}
1274
1275
1276void hostapd_channel_list_updated(struct hostapd_iface *iface, int initiator)
1277{
1278	if (!iface->wait_channel_update || initiator != REGDOM_SET_BY_USER)
1279		return;
1280
1281	wpa_printf(MSG_DEBUG, "Channel list updated - continue setup");
1282	eloop_cancel_timeout(channel_list_update_timeout, iface, NULL);
1283	setup_interface2(iface);
1284}
1285
1286
1287static int setup_interface(struct hostapd_iface *iface)
1288{
1289	struct hostapd_data *hapd = iface->bss[0];
1290	size_t i;
1291
1292	/*
1293	 * It is possible that setup_interface() is called after the interface
1294	 * was disabled etc., in which case driver_ap_teardown is possibly set
1295	 * to 1. Clear it here so any other key/station deletion, which is not
1296	 * part of a teardown flow, would also call the relevant driver
1297	 * callbacks.
1298	 */
1299	iface->driver_ap_teardown = 0;
1300
1301	if (!iface->phy[0]) {
1302		const char *phy = hostapd_drv_get_radio_name(hapd);
1303		if (phy) {
1304			wpa_printf(MSG_DEBUG, "phy: %s", phy);
1305			os_strlcpy(iface->phy, phy, sizeof(iface->phy));
1306		}
1307	}
1308
1309	/*
1310	 * Make sure that all BSSes get configured with a pointer to the same
1311	 * driver interface.
1312	 */
1313	for (i = 1; i < iface->num_bss; i++) {
1314		iface->bss[i]->driver = hapd->driver;
1315		iface->bss[i]->drv_priv = hapd->drv_priv;
1316	}
1317
1318	if (hostapd_validate_bssid_configuration(iface))
1319		return -1;
1320
1321	/*
1322	 * Initialize control interfaces early to allow external monitoring of
1323	 * channel setup operations that may take considerable amount of time
1324	 * especially for DFS cases.
1325	 */
1326	if (start_ctrl_iface(iface))
1327		return -1;
1328
1329	if (hapd->iconf->country[0] && hapd->iconf->country[1]) {
1330		char country[4], previous_country[4];
1331
1332		hostapd_set_state(iface, HAPD_IFACE_COUNTRY_UPDATE);
1333		if (hostapd_get_country(hapd, previous_country) < 0)
1334			previous_country[0] = '\0';
1335
1336		os_memcpy(country, hapd->iconf->country, 3);
1337		country[3] = '\0';
1338		if (hostapd_set_country(hapd, country) < 0) {
1339			wpa_printf(MSG_ERROR, "Failed to set country code");
1340			return -1;
1341		}
1342
1343		wpa_printf(MSG_DEBUG, "Previous country code %s, new country code %s",
1344			   previous_country, country);
1345
1346		if (os_strncmp(previous_country, country, 2) != 0) {
1347			wpa_printf(MSG_DEBUG, "Continue interface setup after channel list update");
1348			iface->wait_channel_update = 1;
1349			eloop_register_timeout(5, 0,
1350					       channel_list_update_timeout,
1351					       iface, NULL);
1352			return 0;
1353		}
1354	}
1355
1356	return setup_interface2(iface);
1357}
1358
1359
1360static int setup_interface2(struct hostapd_iface *iface)
1361{
1362	iface->wait_channel_update = 0;
1363
1364	if (hostapd_get_hw_features(iface)) {
1365		/* Not all drivers support this yet, so continue without hw
1366		 * feature data. */
1367	} else {
1368		int ret = hostapd_select_hw_mode(iface);
1369		if (ret < 0) {
1370			wpa_printf(MSG_ERROR, "Could not select hw_mode and "
1371				   "channel. (%d)", ret);
1372			goto fail;
1373		}
1374		if (ret == 1) {
1375			wpa_printf(MSG_DEBUG, "Interface initialization will be completed in a callback (ACS)");
1376			return 0;
1377		}
1378		ret = hostapd_check_ht_capab(iface);
1379		if (ret < 0)
1380			goto fail;
1381		if (ret == 1) {
1382			wpa_printf(MSG_DEBUG, "Interface initialization will "
1383				   "be completed in a callback");
1384			return 0;
1385		}
1386
1387		if (iface->conf->ieee80211h)
1388			wpa_printf(MSG_DEBUG, "DFS support is enabled");
1389	}
1390	return hostapd_setup_interface_complete(iface, 0);
1391
1392fail:
1393	hostapd_set_state(iface, HAPD_IFACE_DISABLED);
1394	wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
1395	if (iface->interfaces && iface->interfaces->terminate_on_error)
1396		eloop_terminate();
1397	return -1;
1398}
1399
1400
1401#ifdef CONFIG_FST
1402
1403static const u8 * fst_hostapd_get_bssid_cb(void *ctx)
1404{
1405	struct hostapd_data *hapd = ctx;
1406
1407	return hapd->own_addr;
1408}
1409
1410
1411static void fst_hostapd_get_channel_info_cb(void *ctx,
1412					    enum hostapd_hw_mode *hw_mode,
1413					    u8 *channel)
1414{
1415	struct hostapd_data *hapd = ctx;
1416
1417	*hw_mode = ieee80211_freq_to_chan(hapd->iface->freq, channel);
1418}
1419
1420
1421static void fst_hostapd_set_ies_cb(void *ctx, const struct wpabuf *fst_ies)
1422{
1423	struct hostapd_data *hapd = ctx;
1424
1425	if (hapd->iface->fst_ies != fst_ies) {
1426		hapd->iface->fst_ies = fst_ies;
1427		if (ieee802_11_set_beacon(hapd))
1428			wpa_printf(MSG_WARNING, "FST: Cannot set beacon");
1429	}
1430}
1431
1432
1433static int fst_hostapd_send_action_cb(void *ctx, const u8 *da,
1434				      struct wpabuf *buf)
1435{
1436	struct hostapd_data *hapd = ctx;
1437
1438	return hostapd_drv_send_action(hapd, hapd->iface->freq, 0, da,
1439				       wpabuf_head(buf), wpabuf_len(buf));
1440}
1441
1442
1443static const struct wpabuf * fst_hostapd_get_mb_ie_cb(void *ctx, const u8 *addr)
1444{
1445	struct hostapd_data *hapd = ctx;
1446	struct sta_info *sta = ap_get_sta(hapd, addr);
1447
1448	return sta ? sta->mb_ies : NULL;
1449}
1450
1451
1452static void fst_hostapd_update_mb_ie_cb(void *ctx, const u8 *addr,
1453					const u8 *buf, size_t size)
1454{
1455	struct hostapd_data *hapd = ctx;
1456	struct sta_info *sta = ap_get_sta(hapd, addr);
1457
1458	if (sta) {
1459		struct mb_ies_info info;
1460
1461		if (!mb_ies_info_by_ies(&info, buf, size)) {
1462			wpabuf_free(sta->mb_ies);
1463			sta->mb_ies = mb_ies_by_info(&info);
1464		}
1465	}
1466}
1467
1468
1469static const u8 * fst_hostapd_get_sta(struct fst_get_peer_ctx **get_ctx,
1470				      Boolean mb_only)
1471{
1472	struct sta_info *s = (struct sta_info *) *get_ctx;
1473
1474	if (mb_only) {
1475		for (; s && !s->mb_ies; s = s->next)
1476			;
1477	}
1478
1479	if (s) {
1480		*get_ctx = (struct fst_get_peer_ctx *) s->next;
1481
1482		return s->addr;
1483	}
1484
1485	*get_ctx = NULL;
1486	return NULL;
1487}
1488
1489
1490static const u8 * fst_hostapd_get_peer_first(void *ctx,
1491					     struct fst_get_peer_ctx **get_ctx,
1492					     Boolean mb_only)
1493{
1494	struct hostapd_data *hapd = ctx;
1495
1496	*get_ctx = (struct fst_get_peer_ctx *) hapd->sta_list;
1497
1498	return fst_hostapd_get_sta(get_ctx, mb_only);
1499}
1500
1501
1502static const u8 * fst_hostapd_get_peer_next(void *ctx,
1503					    struct fst_get_peer_ctx **get_ctx,
1504					    Boolean mb_only)
1505{
1506	return fst_hostapd_get_sta(get_ctx, mb_only);
1507}
1508
1509
1510void fst_hostapd_fill_iface_obj(struct hostapd_data *hapd,
1511				struct fst_wpa_obj *iface_obj)
1512{
1513	iface_obj->ctx = hapd;
1514	iface_obj->get_bssid = fst_hostapd_get_bssid_cb;
1515	iface_obj->get_channel_info = fst_hostapd_get_channel_info_cb;
1516	iface_obj->set_ies = fst_hostapd_set_ies_cb;
1517	iface_obj->send_action = fst_hostapd_send_action_cb;
1518	iface_obj->get_mb_ie = fst_hostapd_get_mb_ie_cb;
1519	iface_obj->update_mb_ie = fst_hostapd_update_mb_ie_cb;
1520	iface_obj->get_peer_first = fst_hostapd_get_peer_first;
1521	iface_obj->get_peer_next = fst_hostapd_get_peer_next;
1522}
1523
1524#endif /* CONFIG_FST */
1525
1526
1527static int hostapd_setup_interface_complete_sync(struct hostapd_iface *iface,
1528						 int err)
1529{
1530	struct hostapd_data *hapd = iface->bss[0];
1531	size_t j;
1532	u8 *prev_addr;
1533	int delay_apply_cfg = 0;
1534	int res_dfs_offload = 0;
1535
1536	if (err)
1537		goto fail;
1538
1539	wpa_printf(MSG_DEBUG, "Completing interface initialization");
1540	if (iface->conf->channel) {
1541#ifdef NEED_AP_MLME
1542		int res;
1543#endif /* NEED_AP_MLME */
1544
1545		iface->freq = hostapd_hw_get_freq(hapd, iface->conf->channel);
1546		wpa_printf(MSG_DEBUG, "Mode: %s  Channel: %d  "
1547			   "Frequency: %d MHz",
1548			   hostapd_hw_mode_txt(iface->conf->hw_mode),
1549			   iface->conf->channel, iface->freq);
1550
1551#ifdef NEED_AP_MLME
1552		/* Handle DFS only if it is not offloaded to the driver */
1553		if (!(iface->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD)) {
1554			/* Check DFS */
1555			res = hostapd_handle_dfs(iface);
1556			if (res <= 0) {
1557				if (res < 0)
1558					goto fail;
1559				return res;
1560			}
1561		} else {
1562			/* If DFS is offloaded to the driver */
1563			res_dfs_offload = hostapd_handle_dfs_offload(iface);
1564			if (res_dfs_offload <= 0) {
1565				if (res_dfs_offload < 0)
1566					goto fail;
1567			} else {
1568				wpa_printf(MSG_DEBUG,
1569					   "Proceed with AP/channel setup");
1570				/*
1571				 * If this is a DFS channel, move to completing
1572				 * AP setup.
1573				 */
1574				if (res_dfs_offload == 1)
1575					goto dfs_offload;
1576				/* Otherwise fall through. */
1577			}
1578		}
1579#endif /* NEED_AP_MLME */
1580
1581#ifdef CONFIG_MESH
1582		if (iface->mconf != NULL) {
1583			wpa_printf(MSG_DEBUG,
1584				   "%s: Mesh configuration will be applied while joining the mesh network",
1585				   iface->bss[0]->conf->iface);
1586			delay_apply_cfg = 1;
1587		}
1588#endif /* CONFIG_MESH */
1589
1590		if (!delay_apply_cfg &&
1591		    hostapd_set_freq(hapd, hapd->iconf->hw_mode, iface->freq,
1592				     hapd->iconf->channel,
1593				     hapd->iconf->ieee80211n,
1594				     hapd->iconf->ieee80211ac,
1595				     hapd->iconf->secondary_channel,
1596				     hapd->iconf->vht_oper_chwidth,
1597				     hapd->iconf->vht_oper_centr_freq_seg0_idx,
1598				     hapd->iconf->vht_oper_centr_freq_seg1_idx)) {
1599			wpa_printf(MSG_ERROR, "Could not set channel for "
1600				   "kernel driver");
1601			goto fail;
1602		}
1603	}
1604
1605	if (iface->current_mode) {
1606		if (hostapd_prepare_rates(iface, iface->current_mode)) {
1607			wpa_printf(MSG_ERROR, "Failed to prepare rates "
1608				   "table.");
1609			hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
1610				       HOSTAPD_LEVEL_WARNING,
1611				       "Failed to prepare rates table.");
1612			goto fail;
1613		}
1614	}
1615
1616	if (hapd->iconf->rts_threshold > -1 &&
1617	    hostapd_set_rts(hapd, hapd->iconf->rts_threshold)) {
1618		wpa_printf(MSG_ERROR, "Could not set RTS threshold for "
1619			   "kernel driver");
1620		goto fail;
1621	}
1622
1623	if (hapd->iconf->fragm_threshold > -1 &&
1624	    hostapd_set_frag(hapd, hapd->iconf->fragm_threshold)) {
1625		wpa_printf(MSG_ERROR, "Could not set fragmentation threshold "
1626			   "for kernel driver");
1627		goto fail;
1628	}
1629
1630	prev_addr = hapd->own_addr;
1631
1632	for (j = 0; j < iface->num_bss; j++) {
1633		hapd = iface->bss[j];
1634		if (j)
1635			os_memcpy(hapd->own_addr, prev_addr, ETH_ALEN);
1636		if (hostapd_setup_bss(hapd, j == 0)) {
1637			do {
1638				hapd = iface->bss[j];
1639				hostapd_bss_deinit_no_free(hapd);
1640				hostapd_free_hapd_data(hapd);
1641			} while (j-- > 0);
1642			goto fail;
1643		}
1644		if (is_zero_ether_addr(hapd->conf->bssid))
1645			prev_addr = hapd->own_addr;
1646	}
1647	hapd = iface->bss[0];
1648
1649	hostapd_tx_queue_params(iface);
1650
1651	ap_list_init(iface);
1652	dl_list_init(&iface->sta_seen);
1653
1654	hostapd_set_acl(hapd);
1655
1656	if (hostapd_driver_commit(hapd) < 0) {
1657		wpa_printf(MSG_ERROR, "%s: Failed to commit driver "
1658			   "configuration", __func__);
1659		goto fail;
1660	}
1661
1662	/*
1663	 * WPS UPnP module can be initialized only when the "upnp_iface" is up.
1664	 * If "interface" and "upnp_iface" are the same (e.g., non-bridge
1665	 * mode), the interface is up only after driver_commit, so initialize
1666	 * WPS after driver_commit.
1667	 */
1668	for (j = 0; j < iface->num_bss; j++) {
1669		if (hostapd_init_wps_complete(iface->bss[j]))
1670			goto fail;
1671	}
1672
1673	if ((iface->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
1674	    !res_dfs_offload) {
1675		/*
1676		 * If freq is DFS, and DFS is offloaded to the driver, then wait
1677		 * for CAC to complete.
1678		 */
1679		wpa_printf(MSG_DEBUG, "%s: Wait for CAC to complete", __func__);
1680		return res_dfs_offload;
1681	}
1682
1683#ifdef NEED_AP_MLME
1684dfs_offload:
1685#endif /* NEED_AP_MLME */
1686
1687#ifdef CONFIG_FST
1688	if (hapd->iconf->fst_cfg.group_id[0]) {
1689		struct fst_wpa_obj iface_obj;
1690
1691		fst_hostapd_fill_iface_obj(hapd, &iface_obj);
1692		iface->fst = fst_attach(hapd->conf->iface, hapd->own_addr,
1693					&iface_obj, &hapd->iconf->fst_cfg);
1694		if (!iface->fst) {
1695			wpa_printf(MSG_ERROR, "Could not attach to FST %s",
1696				   hapd->iconf->fst_cfg.group_id);
1697			goto fail;
1698		}
1699	}
1700#endif /* CONFIG_FST */
1701
1702	hostapd_set_state(iface, HAPD_IFACE_ENABLED);
1703	wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_ENABLED);
1704	if (hapd->setup_complete_cb)
1705		hapd->setup_complete_cb(hapd->setup_complete_cb_ctx);
1706
1707	wpa_printf(MSG_DEBUG, "%s: Setup of interface done.",
1708		   iface->bss[0]->conf->iface);
1709	if (iface->interfaces && iface->interfaces->terminate_on_error > 0)
1710		iface->interfaces->terminate_on_error--;
1711
1712	return 0;
1713
1714fail:
1715	wpa_printf(MSG_ERROR, "Interface initialization failed");
1716	hostapd_set_state(iface, HAPD_IFACE_DISABLED);
1717	wpa_msg(hapd->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
1718#ifdef CONFIG_FST
1719	if (iface->fst) {
1720		fst_detach(iface->fst);
1721		iface->fst = NULL;
1722	}
1723#endif /* CONFIG_FST */
1724	if (iface->interfaces && iface->interfaces->terminate_on_error)
1725		eloop_terminate();
1726	return -1;
1727}
1728
1729
1730/**
1731 * hostapd_setup_interface_complete - Complete interface setup
1732 *
1733 * This function is called when previous steps in the interface setup has been
1734 * completed. This can also start operations, e.g., DFS, that will require
1735 * additional processing before interface is ready to be enabled. Such
1736 * operations will call this function from eloop callbacks when finished.
1737 */
1738int hostapd_setup_interface_complete(struct hostapd_iface *iface, int err)
1739{
1740	struct hapd_interfaces *interfaces = iface->interfaces;
1741	struct hostapd_data *hapd = iface->bss[0];
1742	unsigned int i;
1743	int not_ready_in_sync_ifaces = 0;
1744
1745	if (!iface->need_to_start_in_sync)
1746		return hostapd_setup_interface_complete_sync(iface, err);
1747
1748	if (err) {
1749		wpa_printf(MSG_ERROR, "Interface initialization failed");
1750		hostapd_set_state(iface, HAPD_IFACE_DISABLED);
1751		iface->need_to_start_in_sync = 0;
1752		wpa_msg(hapd->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
1753		if (interfaces && interfaces->terminate_on_error)
1754			eloop_terminate();
1755		return -1;
1756	}
1757
1758	if (iface->ready_to_start_in_sync) {
1759		/* Already in ready and waiting. should never happpen */
1760		return 0;
1761	}
1762
1763	for (i = 0; i < interfaces->count; i++) {
1764		if (interfaces->iface[i]->need_to_start_in_sync &&
1765		    !interfaces->iface[i]->ready_to_start_in_sync)
1766			not_ready_in_sync_ifaces++;
1767	}
1768
1769	/*
1770	 * Check if this is the last interface, if yes then start all the other
1771	 * waiting interfaces. If not, add this interface to the waiting list.
1772	 */
1773	if (not_ready_in_sync_ifaces > 1 && iface->state == HAPD_IFACE_DFS) {
1774		/*
1775		 * If this interface went through CAC, do not synchronize, just
1776		 * start immediately.
1777		 */
1778		iface->need_to_start_in_sync = 0;
1779		wpa_printf(MSG_INFO,
1780			   "%s: Finished CAC - bypass sync and start interface",
1781			   iface->bss[0]->conf->iface);
1782		return hostapd_setup_interface_complete_sync(iface, err);
1783	}
1784
1785	if (not_ready_in_sync_ifaces > 1) {
1786		/* need to wait as there are other interfaces still coming up */
1787		iface->ready_to_start_in_sync = 1;
1788		wpa_printf(MSG_INFO,
1789			   "%s: Interface waiting to sync with other interfaces",
1790			   iface->bss[0]->conf->iface);
1791		return 0;
1792	}
1793
1794	wpa_printf(MSG_INFO,
1795		   "%s: Last interface to sync - starting all interfaces",
1796		   iface->bss[0]->conf->iface);
1797	iface->need_to_start_in_sync = 0;
1798	hostapd_setup_interface_complete_sync(iface, err);
1799	for (i = 0; i < interfaces->count; i++) {
1800		if (interfaces->iface[i]->need_to_start_in_sync &&
1801		    interfaces->iface[i]->ready_to_start_in_sync) {
1802			hostapd_setup_interface_complete_sync(
1803				interfaces->iface[i], 0);
1804			/* Only once the interfaces are sync started */
1805			interfaces->iface[i]->need_to_start_in_sync = 0;
1806		}
1807	}
1808
1809	return 0;
1810}
1811
1812
1813/**
1814 * hostapd_setup_interface - Setup of an interface
1815 * @iface: Pointer to interface data.
1816 * Returns: 0 on success, -1 on failure
1817 *
1818 * Initializes the driver interface, validates the configuration,
1819 * and sets driver parameters based on the configuration.
1820 * Flushes old stations, sets the channel, encryption,
1821 * beacons, and WDS links based on the configuration.
1822 *
1823 * If interface setup requires more time, e.g., to perform HT co-ex scans, ACS,
1824 * or DFS operations, this function returns 0 before such operations have been
1825 * completed. The pending operations are registered into eloop and will be
1826 * completed from eloop callbacks. Those callbacks end up calling
1827 * hostapd_setup_interface_complete() once setup has been completed.
1828 */
1829int hostapd_setup_interface(struct hostapd_iface *iface)
1830{
1831	int ret;
1832
1833	ret = setup_interface(iface);
1834	if (ret) {
1835		wpa_printf(MSG_ERROR, "%s: Unable to setup interface.",
1836			   iface->bss[0]->conf->iface);
1837		return -1;
1838	}
1839
1840	return 0;
1841}
1842
1843
1844/**
1845 * hostapd_alloc_bss_data - Allocate and initialize per-BSS data
1846 * @hapd_iface: Pointer to interface data
1847 * @conf: Pointer to per-interface configuration
1848 * @bss: Pointer to per-BSS configuration for this BSS
1849 * Returns: Pointer to allocated BSS data
1850 *
1851 * This function is used to allocate per-BSS data structure. This data will be
1852 * freed after hostapd_cleanup() is called for it during interface
1853 * deinitialization.
1854 */
1855struct hostapd_data *
1856hostapd_alloc_bss_data(struct hostapd_iface *hapd_iface,
1857		       struct hostapd_config *conf,
1858		       struct hostapd_bss_config *bss)
1859{
1860	struct hostapd_data *hapd;
1861
1862	hapd = os_zalloc(sizeof(*hapd));
1863	if (hapd == NULL)
1864		return NULL;
1865
1866	hapd->new_assoc_sta_cb = hostapd_new_assoc_sta;
1867	hapd->iconf = conf;
1868	hapd->conf = bss;
1869	hapd->iface = hapd_iface;
1870	hapd->driver = hapd->iconf->driver;
1871	hapd->ctrl_sock = -1;
1872	dl_list_init(&hapd->ctrl_dst);
1873
1874	return hapd;
1875}
1876
1877
1878static void hostapd_bss_deinit(struct hostapd_data *hapd)
1879{
1880	wpa_printf(MSG_DEBUG, "%s: deinit bss %s", __func__,
1881		   hapd->conf->iface);
1882	hostapd_bss_deinit_no_free(hapd);
1883	wpa_msg(hapd->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
1884	hostapd_cleanup(hapd);
1885}
1886
1887
1888void hostapd_interface_deinit(struct hostapd_iface *iface)
1889{
1890	int j;
1891
1892	wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
1893	if (iface == NULL)
1894		return;
1895
1896	hostapd_set_state(iface, HAPD_IFACE_DISABLED);
1897
1898#ifdef CONFIG_IEEE80211N
1899#ifdef NEED_AP_MLME
1900	hostapd_stop_setup_timers(iface);
1901	eloop_cancel_timeout(ap_ht2040_timeout, iface, NULL);
1902#endif /* NEED_AP_MLME */
1903#endif /* CONFIG_IEEE80211N */
1904	eloop_cancel_timeout(channel_list_update_timeout, iface, NULL);
1905	iface->wait_channel_update = 0;
1906
1907#ifdef CONFIG_FST
1908	if (iface->fst) {
1909		fst_detach(iface->fst);
1910		iface->fst = NULL;
1911	}
1912#endif /* CONFIG_FST */
1913
1914	for (j = iface->num_bss - 1; j >= 0; j--)
1915		hostapd_bss_deinit(iface->bss[j]);
1916}
1917
1918
1919void hostapd_interface_free(struct hostapd_iface *iface)
1920{
1921	size_t j;
1922	wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
1923	for (j = 0; j < iface->num_bss; j++) {
1924		wpa_printf(MSG_DEBUG, "%s: free hapd %p",
1925			   __func__, iface->bss[j]);
1926		os_free(iface->bss[j]);
1927	}
1928	hostapd_cleanup_iface(iface);
1929}
1930
1931
1932/**
1933 * hostapd_init - Allocate and initialize per-interface data
1934 * @config_file: Path to the configuration file
1935 * Returns: Pointer to the allocated interface data or %NULL on failure
1936 *
1937 * This function is used to allocate main data structures for per-interface
1938 * data. The allocated data buffer will be freed by calling
1939 * hostapd_cleanup_iface().
1940 */
1941struct hostapd_iface * hostapd_init(struct hapd_interfaces *interfaces,
1942				    const char *config_file)
1943{
1944	struct hostapd_iface *hapd_iface = NULL;
1945	struct hostapd_config *conf = NULL;
1946	struct hostapd_data *hapd;
1947	size_t i;
1948
1949	hapd_iface = os_zalloc(sizeof(*hapd_iface));
1950	if (hapd_iface == NULL)
1951		goto fail;
1952
1953	hapd_iface->config_fname = os_strdup(config_file);
1954	if (hapd_iface->config_fname == NULL)
1955		goto fail;
1956
1957	conf = interfaces->config_read_cb(hapd_iface->config_fname);
1958	if (conf == NULL)
1959		goto fail;
1960	hapd_iface->conf = conf;
1961
1962	hapd_iface->num_bss = conf->num_bss;
1963	hapd_iface->bss = os_calloc(conf->num_bss,
1964				    sizeof(struct hostapd_data *));
1965	if (hapd_iface->bss == NULL)
1966		goto fail;
1967
1968	for (i = 0; i < conf->num_bss; i++) {
1969		hapd = hapd_iface->bss[i] =
1970			hostapd_alloc_bss_data(hapd_iface, conf,
1971					       conf->bss[i]);
1972		if (hapd == NULL)
1973			goto fail;
1974		hapd->msg_ctx = hapd;
1975	}
1976
1977	return hapd_iface;
1978
1979fail:
1980	wpa_printf(MSG_ERROR, "Failed to set up interface with %s",
1981		   config_file);
1982	if (conf)
1983		hostapd_config_free(conf);
1984	if (hapd_iface) {
1985		os_free(hapd_iface->config_fname);
1986		os_free(hapd_iface->bss);
1987		wpa_printf(MSG_DEBUG, "%s: free iface %p",
1988			   __func__, hapd_iface);
1989		os_free(hapd_iface);
1990	}
1991	return NULL;
1992}
1993
1994
1995static int ifname_in_use(struct hapd_interfaces *interfaces, const char *ifname)
1996{
1997	size_t i, j;
1998
1999	for (i = 0; i < interfaces->count; i++) {
2000		struct hostapd_iface *iface = interfaces->iface[i];
2001		for (j = 0; j < iface->num_bss; j++) {
2002			struct hostapd_data *hapd = iface->bss[j];
2003			if (os_strcmp(ifname, hapd->conf->iface) == 0)
2004				return 1;
2005		}
2006	}
2007
2008	return 0;
2009}
2010
2011
2012/**
2013 * hostapd_interface_init_bss - Read configuration file and init BSS data
2014 *
2015 * This function is used to parse configuration file for a BSS. This BSS is
2016 * added to an existing interface sharing the same radio (if any) or a new
2017 * interface is created if this is the first interface on a radio. This
2018 * allocate memory for the BSS. No actual driver operations are started.
2019 *
2020 * This is similar to hostapd_interface_init(), but for a case where the
2021 * configuration is used to add a single BSS instead of all BSSes for a radio.
2022 */
2023struct hostapd_iface *
2024hostapd_interface_init_bss(struct hapd_interfaces *interfaces, const char *phy,
2025			   const char *config_fname, int debug)
2026{
2027	struct hostapd_iface *new_iface = NULL, *iface = NULL;
2028	struct hostapd_data *hapd;
2029	int k;
2030	size_t i, bss_idx;
2031
2032	if (!phy || !*phy)
2033		return NULL;
2034
2035	for (i = 0; i < interfaces->count; i++) {
2036		if (os_strcmp(interfaces->iface[i]->phy, phy) == 0) {
2037			iface = interfaces->iface[i];
2038			break;
2039		}
2040	}
2041
2042	wpa_printf(MSG_INFO, "Configuration file: %s (phy %s)%s",
2043		   config_fname, phy, iface ? "" : " --> new PHY");
2044	if (iface) {
2045		struct hostapd_config *conf;
2046		struct hostapd_bss_config **tmp_conf;
2047		struct hostapd_data **tmp_bss;
2048		struct hostapd_bss_config *bss;
2049		const char *ifname;
2050
2051		/* Add new BSS to existing iface */
2052		conf = interfaces->config_read_cb(config_fname);
2053		if (conf == NULL)
2054			return NULL;
2055		if (conf->num_bss > 1) {
2056			wpa_printf(MSG_ERROR, "Multiple BSSes specified in BSS-config");
2057			hostapd_config_free(conf);
2058			return NULL;
2059		}
2060
2061		ifname = conf->bss[0]->iface;
2062		if (ifname[0] != '\0' && ifname_in_use(interfaces, ifname)) {
2063			wpa_printf(MSG_ERROR,
2064				   "Interface name %s already in use", ifname);
2065			hostapd_config_free(conf);
2066			return NULL;
2067		}
2068
2069		tmp_conf = os_realloc_array(
2070			iface->conf->bss, iface->conf->num_bss + 1,
2071			sizeof(struct hostapd_bss_config *));
2072		tmp_bss = os_realloc_array(iface->bss, iface->num_bss + 1,
2073					   sizeof(struct hostapd_data *));
2074		if (tmp_bss)
2075			iface->bss = tmp_bss;
2076		if (tmp_conf) {
2077			iface->conf->bss = tmp_conf;
2078			iface->conf->last_bss = tmp_conf[0];
2079		}
2080		if (tmp_bss == NULL || tmp_conf == NULL) {
2081			hostapd_config_free(conf);
2082			return NULL;
2083		}
2084		bss = iface->conf->bss[iface->conf->num_bss] = conf->bss[0];
2085		iface->conf->num_bss++;
2086
2087		hapd = hostapd_alloc_bss_data(iface, iface->conf, bss);
2088		if (hapd == NULL) {
2089			iface->conf->num_bss--;
2090			hostapd_config_free(conf);
2091			return NULL;
2092		}
2093		iface->conf->last_bss = bss;
2094		iface->bss[iface->num_bss] = hapd;
2095		hapd->msg_ctx = hapd;
2096
2097		bss_idx = iface->num_bss++;
2098		conf->num_bss--;
2099		conf->bss[0] = NULL;
2100		hostapd_config_free(conf);
2101	} else {
2102		/* Add a new iface with the first BSS */
2103		new_iface = iface = hostapd_init(interfaces, config_fname);
2104		if (!iface)
2105			return NULL;
2106		os_strlcpy(iface->phy, phy, sizeof(iface->phy));
2107		iface->interfaces = interfaces;
2108		bss_idx = 0;
2109	}
2110
2111	for (k = 0; k < debug; k++) {
2112		if (iface->bss[bss_idx]->conf->logger_stdout_level > 0)
2113			iface->bss[bss_idx]->conf->logger_stdout_level--;
2114	}
2115
2116	if (iface->conf->bss[bss_idx]->iface[0] == '\0' &&
2117	    !hostapd_drv_none(iface->bss[bss_idx])) {
2118		wpa_printf(MSG_ERROR, "Interface name not specified in %s",
2119			   config_fname);
2120		if (new_iface)
2121			hostapd_interface_deinit_free(new_iface);
2122		return NULL;
2123	}
2124
2125	return iface;
2126}
2127
2128
2129void hostapd_interface_deinit_free(struct hostapd_iface *iface)
2130{
2131	const struct wpa_driver_ops *driver;
2132	void *drv_priv;
2133
2134	wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
2135	if (iface == NULL)
2136		return;
2137	wpa_printf(MSG_DEBUG, "%s: num_bss=%u conf->num_bss=%u",
2138		   __func__, (unsigned int) iface->num_bss,
2139		   (unsigned int) iface->conf->num_bss);
2140	driver = iface->bss[0]->driver;
2141	drv_priv = iface->bss[0]->drv_priv;
2142	hostapd_interface_deinit(iface);
2143	wpa_printf(MSG_DEBUG, "%s: driver=%p drv_priv=%p -> hapd_deinit",
2144		   __func__, driver, drv_priv);
2145	if (driver && driver->hapd_deinit && drv_priv) {
2146		driver->hapd_deinit(drv_priv);
2147		iface->bss[0]->drv_priv = NULL;
2148	}
2149	hostapd_interface_free(iface);
2150}
2151
2152
2153static void hostapd_deinit_driver(const struct wpa_driver_ops *driver,
2154				  void *drv_priv,
2155				  struct hostapd_iface *hapd_iface)
2156{
2157	size_t j;
2158
2159	wpa_printf(MSG_DEBUG, "%s: driver=%p drv_priv=%p -> hapd_deinit",
2160		   __func__, driver, drv_priv);
2161	if (driver && driver->hapd_deinit && drv_priv) {
2162		driver->hapd_deinit(drv_priv);
2163		for (j = 0; j < hapd_iface->num_bss; j++) {
2164			wpa_printf(MSG_DEBUG, "%s:bss[%d]->drv_priv=%p",
2165				   __func__, (int) j,
2166				   hapd_iface->bss[j]->drv_priv);
2167			if (hapd_iface->bss[j]->drv_priv == drv_priv)
2168				hapd_iface->bss[j]->drv_priv = NULL;
2169		}
2170	}
2171}
2172
2173
2174int hostapd_enable_iface(struct hostapd_iface *hapd_iface)
2175{
2176	size_t j;
2177
2178	if (hapd_iface->bss[0]->drv_priv != NULL) {
2179		wpa_printf(MSG_ERROR, "Interface %s already enabled",
2180			   hapd_iface->conf->bss[0]->iface);
2181		return -1;
2182	}
2183
2184	wpa_printf(MSG_DEBUG, "Enable interface %s",
2185		   hapd_iface->conf->bss[0]->iface);
2186
2187	for (j = 0; j < hapd_iface->num_bss; j++)
2188		hostapd_set_security_params(hapd_iface->conf->bss[j], 1);
2189	if (hostapd_config_check(hapd_iface->conf, 1) < 0) {
2190		wpa_printf(MSG_INFO, "Invalid configuration - cannot enable");
2191		return -1;
2192	}
2193
2194	if (hapd_iface->interfaces == NULL ||
2195	    hapd_iface->interfaces->driver_init == NULL ||
2196	    hapd_iface->interfaces->driver_init(hapd_iface))
2197		return -1;
2198
2199	if (hostapd_setup_interface(hapd_iface)) {
2200		hostapd_deinit_driver(hapd_iface->bss[0]->driver,
2201				      hapd_iface->bss[0]->drv_priv,
2202				      hapd_iface);
2203		return -1;
2204	}
2205
2206	return 0;
2207}
2208
2209
2210int hostapd_reload_iface(struct hostapd_iface *hapd_iface)
2211{
2212	size_t j;
2213
2214	wpa_printf(MSG_DEBUG, "Reload interface %s",
2215		   hapd_iface->conf->bss[0]->iface);
2216	for (j = 0; j < hapd_iface->num_bss; j++)
2217		hostapd_set_security_params(hapd_iface->conf->bss[j], 1);
2218	if (hostapd_config_check(hapd_iface->conf, 1) < 0) {
2219		wpa_printf(MSG_ERROR, "Updated configuration is invalid");
2220		return -1;
2221	}
2222	hostapd_clear_old(hapd_iface);
2223	for (j = 0; j < hapd_iface->num_bss; j++)
2224		hostapd_reload_bss(hapd_iface->bss[j]);
2225
2226	return 0;
2227}
2228
2229
2230int hostapd_disable_iface(struct hostapd_iface *hapd_iface)
2231{
2232	size_t j;
2233	const struct wpa_driver_ops *driver;
2234	void *drv_priv;
2235
2236	if (hapd_iface == NULL)
2237		return -1;
2238
2239	if (hapd_iface->bss[0]->drv_priv == NULL) {
2240		wpa_printf(MSG_INFO, "Interface %s already disabled",
2241			   hapd_iface->conf->bss[0]->iface);
2242		return -1;
2243	}
2244
2245	wpa_msg(hapd_iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
2246	driver = hapd_iface->bss[0]->driver;
2247	drv_priv = hapd_iface->bss[0]->drv_priv;
2248
2249	hapd_iface->driver_ap_teardown =
2250		!!(hapd_iface->drv_flags &
2251		   WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
2252
2253	/* same as hostapd_interface_deinit without deinitializing ctrl-iface */
2254	for (j = 0; j < hapd_iface->num_bss; j++) {
2255		struct hostapd_data *hapd = hapd_iface->bss[j];
2256		hostapd_bss_deinit_no_free(hapd);
2257		hostapd_free_hapd_data(hapd);
2258	}
2259
2260	hostapd_deinit_driver(driver, drv_priv, hapd_iface);
2261
2262	/* From hostapd_cleanup_iface: These were initialized in
2263	 * hostapd_setup_interface and hostapd_setup_interface_complete
2264	 */
2265	hostapd_cleanup_iface_partial(hapd_iface);
2266
2267	wpa_printf(MSG_DEBUG, "Interface %s disabled",
2268		   hapd_iface->bss[0]->conf->iface);
2269	hostapd_set_state(hapd_iface, HAPD_IFACE_DISABLED);
2270	return 0;
2271}
2272
2273
2274static struct hostapd_iface *
2275hostapd_iface_alloc(struct hapd_interfaces *interfaces)
2276{
2277	struct hostapd_iface **iface, *hapd_iface;
2278
2279	iface = os_realloc_array(interfaces->iface, interfaces->count + 1,
2280				 sizeof(struct hostapd_iface *));
2281	if (iface == NULL)
2282		return NULL;
2283	interfaces->iface = iface;
2284	hapd_iface = interfaces->iface[interfaces->count] =
2285		os_zalloc(sizeof(*hapd_iface));
2286	if (hapd_iface == NULL) {
2287		wpa_printf(MSG_ERROR, "%s: Failed to allocate memory for "
2288			   "the interface", __func__);
2289		return NULL;
2290	}
2291	interfaces->count++;
2292	hapd_iface->interfaces = interfaces;
2293
2294	return hapd_iface;
2295}
2296
2297
2298static struct hostapd_config *
2299hostapd_config_alloc(struct hapd_interfaces *interfaces, const char *ifname,
2300		     const char *ctrl_iface, const char *driver)
2301{
2302	struct hostapd_bss_config *bss;
2303	struct hostapd_config *conf;
2304
2305	/* Allocates memory for bss and conf */
2306	conf = hostapd_config_defaults();
2307	if (conf == NULL) {
2308		 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory for "
2309				"configuration", __func__);
2310		return NULL;
2311	}
2312
2313	if (driver) {
2314		int j;
2315
2316		for (j = 0; wpa_drivers[j]; j++) {
2317			if (os_strcmp(driver, wpa_drivers[j]->name) == 0) {
2318				conf->driver = wpa_drivers[j];
2319				goto skip;
2320			}
2321		}
2322
2323		wpa_printf(MSG_ERROR,
2324			   "Invalid/unknown driver '%s' - registering the default driver",
2325			   driver);
2326	}
2327
2328	conf->driver = wpa_drivers[0];
2329	if (conf->driver == NULL) {
2330		wpa_printf(MSG_ERROR, "No driver wrappers registered!");
2331		hostapd_config_free(conf);
2332		return NULL;
2333	}
2334
2335skip:
2336	bss = conf->last_bss = conf->bss[0];
2337
2338	os_strlcpy(bss->iface, ifname, sizeof(bss->iface));
2339	bss->ctrl_interface = os_strdup(ctrl_iface);
2340	if (bss->ctrl_interface == NULL) {
2341		hostapd_config_free(conf);
2342		return NULL;
2343	}
2344
2345	/* Reading configuration file skipped, will be done in SET!
2346	 * From reading the configuration till the end has to be done in
2347	 * SET
2348	 */
2349	return conf;
2350}
2351
2352
2353static int hostapd_data_alloc(struct hostapd_iface *hapd_iface,
2354			      struct hostapd_config *conf)
2355{
2356	size_t i;
2357	struct hostapd_data *hapd;
2358
2359	hapd_iface->bss = os_calloc(conf->num_bss,
2360				    sizeof(struct hostapd_data *));
2361	if (hapd_iface->bss == NULL)
2362		return -1;
2363
2364	for (i = 0; i < conf->num_bss; i++) {
2365		hapd = hapd_iface->bss[i] =
2366			hostapd_alloc_bss_data(hapd_iface, conf, conf->bss[i]);
2367		if (hapd == NULL) {
2368			while (i > 0) {
2369				i--;
2370				os_free(hapd_iface->bss[i]);
2371				hapd_iface->bss[i] = NULL;
2372			}
2373			os_free(hapd_iface->bss);
2374			hapd_iface->bss = NULL;
2375			return -1;
2376		}
2377		hapd->msg_ctx = hapd;
2378	}
2379
2380	hapd_iface->conf = conf;
2381	hapd_iface->num_bss = conf->num_bss;
2382
2383	return 0;
2384}
2385
2386
2387int hostapd_add_iface(struct hapd_interfaces *interfaces, char *buf)
2388{
2389	struct hostapd_config *conf = NULL;
2390	struct hostapd_iface *hapd_iface = NULL, *new_iface = NULL;
2391	struct hostapd_data *hapd;
2392	char *ptr;
2393	size_t i, j;
2394	const char *conf_file = NULL, *phy_name = NULL;
2395
2396	if (os_strncmp(buf, "bss_config=", 11) == 0) {
2397		char *pos;
2398		phy_name = buf + 11;
2399		pos = os_strchr(phy_name, ':');
2400		if (!pos)
2401			return -1;
2402		*pos++ = '\0';
2403		conf_file = pos;
2404		if (!os_strlen(conf_file))
2405			return -1;
2406
2407		hapd_iface = hostapd_interface_init_bss(interfaces, phy_name,
2408							conf_file, 0);
2409		if (!hapd_iface)
2410			return -1;
2411		for (j = 0; j < interfaces->count; j++) {
2412			if (interfaces->iface[j] == hapd_iface)
2413				break;
2414		}
2415		if (j == interfaces->count) {
2416			struct hostapd_iface **tmp;
2417			tmp = os_realloc_array(interfaces->iface,
2418					       interfaces->count + 1,
2419					       sizeof(struct hostapd_iface *));
2420			if (!tmp) {
2421				hostapd_interface_deinit_free(hapd_iface);
2422				return -1;
2423			}
2424			interfaces->iface = tmp;
2425			interfaces->iface[interfaces->count++] = hapd_iface;
2426			new_iface = hapd_iface;
2427		}
2428
2429		if (new_iface) {
2430			if (interfaces->driver_init(hapd_iface))
2431				goto fail;
2432
2433			if (hostapd_setup_interface(hapd_iface)) {
2434				hostapd_deinit_driver(
2435					hapd_iface->bss[0]->driver,
2436					hapd_iface->bss[0]->drv_priv,
2437					hapd_iface);
2438				goto fail;
2439			}
2440		} else {
2441			/* Assign new BSS with bss[0]'s driver info */
2442			hapd = hapd_iface->bss[hapd_iface->num_bss - 1];
2443			hapd->driver = hapd_iface->bss[0]->driver;
2444			hapd->drv_priv = hapd_iface->bss[0]->drv_priv;
2445			os_memcpy(hapd->own_addr, hapd_iface->bss[0]->own_addr,
2446				  ETH_ALEN);
2447
2448			if (start_ctrl_iface_bss(hapd) < 0 ||
2449			    (hapd_iface->state == HAPD_IFACE_ENABLED &&
2450			     hostapd_setup_bss(hapd, -1))) {
2451				hostapd_cleanup(hapd);
2452				hapd_iface->bss[hapd_iface->num_bss - 1] = NULL;
2453				hapd_iface->conf->num_bss--;
2454				hapd_iface->num_bss--;
2455				wpa_printf(MSG_DEBUG, "%s: free hapd %p %s",
2456					   __func__, hapd, hapd->conf->iface);
2457				hostapd_config_free_bss(hapd->conf);
2458				hapd->conf = NULL;
2459				os_free(hapd);
2460				return -1;
2461			}
2462		}
2463		return 0;
2464	}
2465
2466	ptr = os_strchr(buf, ' ');
2467	if (ptr == NULL)
2468		return -1;
2469	*ptr++ = '\0';
2470
2471	if (os_strncmp(ptr, "config=", 7) == 0)
2472		conf_file = ptr + 7;
2473
2474	for (i = 0; i < interfaces->count; i++) {
2475		if (!os_strcmp(interfaces->iface[i]->conf->bss[0]->iface,
2476			       buf)) {
2477			wpa_printf(MSG_INFO, "Cannot add interface - it "
2478				   "already exists");
2479			return -1;
2480		}
2481	}
2482
2483	hapd_iface = hostapd_iface_alloc(interfaces);
2484	if (hapd_iface == NULL) {
2485		wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
2486			   "for interface", __func__);
2487		goto fail;
2488	}
2489	new_iface = hapd_iface;
2490
2491	if (conf_file && interfaces->config_read_cb) {
2492		conf = interfaces->config_read_cb(conf_file);
2493		if (conf && conf->bss)
2494			os_strlcpy(conf->bss[0]->iface, buf,
2495				   sizeof(conf->bss[0]->iface));
2496	} else {
2497		char *driver = os_strchr(ptr, ' ');
2498
2499		if (driver)
2500			*driver++ = '\0';
2501		conf = hostapd_config_alloc(interfaces, buf, ptr, driver);
2502	}
2503
2504	if (conf == NULL || conf->bss == NULL) {
2505		wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
2506			   "for configuration", __func__);
2507		goto fail;
2508	}
2509
2510	if (hostapd_data_alloc(hapd_iface, conf) < 0) {
2511		wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
2512			   "for hostapd", __func__);
2513		goto fail;
2514	}
2515	conf = NULL;
2516
2517	if (start_ctrl_iface(hapd_iface) < 0)
2518		goto fail;
2519
2520	wpa_printf(MSG_INFO, "Add interface '%s'",
2521		   hapd_iface->conf->bss[0]->iface);
2522
2523	return 0;
2524
2525fail:
2526	if (conf)
2527		hostapd_config_free(conf);
2528	if (hapd_iface) {
2529		if (hapd_iface->bss) {
2530			for (i = 0; i < hapd_iface->num_bss; i++) {
2531				hapd = hapd_iface->bss[i];
2532				if (!hapd)
2533					continue;
2534				if (hapd_iface->interfaces &&
2535				    hapd_iface->interfaces->ctrl_iface_deinit)
2536					hapd_iface->interfaces->
2537						ctrl_iface_deinit(hapd);
2538				wpa_printf(MSG_DEBUG, "%s: free hapd %p (%s)",
2539					   __func__, hapd_iface->bss[i],
2540					   hapd->conf->iface);
2541				hostapd_cleanup(hapd);
2542				os_free(hapd);
2543				hapd_iface->bss[i] = NULL;
2544			}
2545			os_free(hapd_iface->bss);
2546			hapd_iface->bss = NULL;
2547		}
2548		if (new_iface) {
2549			interfaces->count--;
2550			interfaces->iface[interfaces->count] = NULL;
2551		}
2552		hostapd_cleanup_iface(hapd_iface);
2553	}
2554	return -1;
2555}
2556
2557
2558static int hostapd_remove_bss(struct hostapd_iface *iface, unsigned int idx)
2559{
2560	size_t i;
2561
2562	wpa_printf(MSG_INFO, "Remove BSS '%s'", iface->conf->bss[idx]->iface);
2563
2564	/* Remove hostapd_data only if it has already been initialized */
2565	if (idx < iface->num_bss) {
2566		struct hostapd_data *hapd = iface->bss[idx];
2567
2568		hostapd_bss_deinit(hapd);
2569		wpa_printf(MSG_DEBUG, "%s: free hapd %p (%s)",
2570			   __func__, hapd, hapd->conf->iface);
2571		hostapd_config_free_bss(hapd->conf);
2572		hapd->conf = NULL;
2573		os_free(hapd);
2574
2575		iface->num_bss--;
2576
2577		for (i = idx; i < iface->num_bss; i++)
2578			iface->bss[i] = iface->bss[i + 1];
2579	} else {
2580		hostapd_config_free_bss(iface->conf->bss[idx]);
2581		iface->conf->bss[idx] = NULL;
2582	}
2583
2584	iface->conf->num_bss--;
2585	for (i = idx; i < iface->conf->num_bss; i++)
2586		iface->conf->bss[i] = iface->conf->bss[i + 1];
2587
2588	return 0;
2589}
2590
2591
2592int hostapd_remove_iface(struct hapd_interfaces *interfaces, char *buf)
2593{
2594	struct hostapd_iface *hapd_iface;
2595	size_t i, j, k = 0;
2596
2597	for (i = 0; i < interfaces->count; i++) {
2598		hapd_iface = interfaces->iface[i];
2599		if (hapd_iface == NULL)
2600			return -1;
2601		if (!os_strcmp(hapd_iface->conf->bss[0]->iface, buf)) {
2602			wpa_printf(MSG_INFO, "Remove interface '%s'", buf);
2603			hapd_iface->driver_ap_teardown =
2604				!!(hapd_iface->drv_flags &
2605				   WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
2606
2607			hostapd_interface_deinit_free(hapd_iface);
2608			k = i;
2609			while (k < (interfaces->count - 1)) {
2610				interfaces->iface[k] =
2611					interfaces->iface[k + 1];
2612				k++;
2613			}
2614			interfaces->count--;
2615			return 0;
2616		}
2617
2618		for (j = 0; j < hapd_iface->conf->num_bss; j++) {
2619			if (!os_strcmp(hapd_iface->conf->bss[j]->iface, buf)) {
2620				hapd_iface->driver_ap_teardown =
2621					!(hapd_iface->drv_flags &
2622					  WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
2623				return hostapd_remove_bss(hapd_iface, j);
2624			}
2625		}
2626	}
2627	return -1;
2628}
2629
2630
2631/**
2632 * hostapd_new_assoc_sta - Notify that a new station associated with the AP
2633 * @hapd: Pointer to BSS data
2634 * @sta: Pointer to the associated STA data
2635 * @reassoc: 1 to indicate this was a re-association; 0 = first association
2636 *
2637 * This function will be called whenever a station associates with the AP. It
2638 * can be called from ieee802_11.c for drivers that export MLME to hostapd and
2639 * from drv_callbacks.c based on driver events for drivers that take care of
2640 * management frames (IEEE 802.11 authentication and association) internally.
2641 */
2642void hostapd_new_assoc_sta(struct hostapd_data *hapd, struct sta_info *sta,
2643			   int reassoc)
2644{
2645	if (hapd->tkip_countermeasures) {
2646		hostapd_drv_sta_deauth(hapd, sta->addr,
2647				       WLAN_REASON_MICHAEL_MIC_FAILURE);
2648		return;
2649	}
2650
2651	hostapd_prune_associations(hapd, sta->addr);
2652	ap_sta_clear_disconnect_timeouts(hapd, sta);
2653
2654	/* IEEE 802.11F (IAPP) */
2655	if (hapd->conf->ieee802_11f)
2656		iapp_new_station(hapd->iapp, sta);
2657
2658#ifdef CONFIG_P2P
2659	if (sta->p2p_ie == NULL && !sta->no_p2p_set) {
2660		sta->no_p2p_set = 1;
2661		hapd->num_sta_no_p2p++;
2662		if (hapd->num_sta_no_p2p == 1)
2663			hostapd_p2p_non_p2p_sta_connected(hapd);
2664	}
2665#endif /* CONFIG_P2P */
2666
2667	/* Start accounting here, if IEEE 802.1X and WPA are not used.
2668	 * IEEE 802.1X/WPA code will start accounting after the station has
2669	 * been authorized. */
2670	if (!hapd->conf->ieee802_1x && !hapd->conf->wpa && !hapd->conf->osen) {
2671		ap_sta_set_authorized(hapd, sta, 1);
2672		os_get_reltime(&sta->connected_time);
2673		accounting_sta_start(hapd, sta);
2674	}
2675
2676	/* Start IEEE 802.1X authentication process for new stations */
2677	ieee802_1x_new_station(hapd, sta);
2678	if (reassoc) {
2679		if (sta->auth_alg != WLAN_AUTH_FT &&
2680		    !(sta->flags & (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS)))
2681			wpa_auth_sm_event(sta->wpa_sm, WPA_REAUTH);
2682	} else
2683		wpa_auth_sta_associated(hapd->wpa_auth, sta->wpa_sm);
2684
2685	if (!(hapd->iface->drv_flags & WPA_DRIVER_FLAGS_INACTIVITY_TIMER)) {
2686		wpa_printf(MSG_DEBUG,
2687			   "%s: %s: reschedule ap_handle_timer timeout for "
2688			   MACSTR " (%d seconds - ap_max_inactivity)",
2689			   hapd->conf->iface, __func__, MAC2STR(sta->addr),
2690			   hapd->conf->ap_max_inactivity);
2691		eloop_cancel_timeout(ap_handle_timer, hapd, sta);
2692		eloop_register_timeout(hapd->conf->ap_max_inactivity, 0,
2693				       ap_handle_timer, hapd, sta);
2694	}
2695}
2696
2697
2698const char * hostapd_state_text(enum hostapd_iface_state s)
2699{
2700	switch (s) {
2701	case HAPD_IFACE_UNINITIALIZED:
2702		return "UNINITIALIZED";
2703	case HAPD_IFACE_DISABLED:
2704		return "DISABLED";
2705	case HAPD_IFACE_COUNTRY_UPDATE:
2706		return "COUNTRY_UPDATE";
2707	case HAPD_IFACE_ACS:
2708		return "ACS";
2709	case HAPD_IFACE_HT_SCAN:
2710		return "HT_SCAN";
2711	case HAPD_IFACE_DFS:
2712		return "DFS";
2713	case HAPD_IFACE_ENABLED:
2714		return "ENABLED";
2715	}
2716
2717	return "UNKNOWN";
2718}
2719
2720
2721void hostapd_set_state(struct hostapd_iface *iface, enum hostapd_iface_state s)
2722{
2723	wpa_printf(MSG_INFO, "%s: interface state %s->%s",
2724		   iface->conf->bss[0]->iface, hostapd_state_text(iface->state),
2725		   hostapd_state_text(s));
2726	iface->state = s;
2727}
2728
2729
2730int hostapd_csa_in_progress(struct hostapd_iface *iface)
2731{
2732	unsigned int i;
2733
2734	for (i = 0; i < iface->num_bss; i++)
2735		if (iface->bss[i]->csa_in_progress)
2736			return 1;
2737	return 0;
2738}
2739
2740
2741#ifdef NEED_AP_MLME
2742
2743static void free_beacon_data(struct beacon_data *beacon)
2744{
2745	os_free(beacon->head);
2746	beacon->head = NULL;
2747	os_free(beacon->tail);
2748	beacon->tail = NULL;
2749	os_free(beacon->probe_resp);
2750	beacon->probe_resp = NULL;
2751	os_free(beacon->beacon_ies);
2752	beacon->beacon_ies = NULL;
2753	os_free(beacon->proberesp_ies);
2754	beacon->proberesp_ies = NULL;
2755	os_free(beacon->assocresp_ies);
2756	beacon->assocresp_ies = NULL;
2757}
2758
2759
2760static int hostapd_build_beacon_data(struct hostapd_data *hapd,
2761				     struct beacon_data *beacon)
2762{
2763	struct wpabuf *beacon_extra, *proberesp_extra, *assocresp_extra;
2764	struct wpa_driver_ap_params params;
2765	int ret;
2766
2767	os_memset(beacon, 0, sizeof(*beacon));
2768	ret = ieee802_11_build_ap_params(hapd, &params);
2769	if (ret < 0)
2770		return ret;
2771
2772	ret = hostapd_build_ap_extra_ies(hapd, &beacon_extra,
2773					 &proberesp_extra,
2774					 &assocresp_extra);
2775	if (ret)
2776		goto free_ap_params;
2777
2778	ret = -1;
2779	beacon->head = os_malloc(params.head_len);
2780	if (!beacon->head)
2781		goto free_ap_extra_ies;
2782
2783	os_memcpy(beacon->head, params.head, params.head_len);
2784	beacon->head_len = params.head_len;
2785
2786	beacon->tail = os_malloc(params.tail_len);
2787	if (!beacon->tail)
2788		goto free_beacon;
2789
2790	os_memcpy(beacon->tail, params.tail, params.tail_len);
2791	beacon->tail_len = params.tail_len;
2792
2793	if (params.proberesp != NULL) {
2794		beacon->probe_resp = os_malloc(params.proberesp_len);
2795		if (!beacon->probe_resp)
2796			goto free_beacon;
2797
2798		os_memcpy(beacon->probe_resp, params.proberesp,
2799			  params.proberesp_len);
2800		beacon->probe_resp_len = params.proberesp_len;
2801	}
2802
2803	/* copy the extra ies */
2804	if (beacon_extra) {
2805		beacon->beacon_ies = os_malloc(wpabuf_len(beacon_extra));
2806		if (!beacon->beacon_ies)
2807			goto free_beacon;
2808
2809		os_memcpy(beacon->beacon_ies,
2810			  beacon_extra->buf, wpabuf_len(beacon_extra));
2811		beacon->beacon_ies_len = wpabuf_len(beacon_extra);
2812	}
2813
2814	if (proberesp_extra) {
2815		beacon->proberesp_ies =
2816			os_malloc(wpabuf_len(proberesp_extra));
2817		if (!beacon->proberesp_ies)
2818			goto free_beacon;
2819
2820		os_memcpy(beacon->proberesp_ies, proberesp_extra->buf,
2821			  wpabuf_len(proberesp_extra));
2822		beacon->proberesp_ies_len = wpabuf_len(proberesp_extra);
2823	}
2824
2825	if (assocresp_extra) {
2826		beacon->assocresp_ies =
2827			os_malloc(wpabuf_len(assocresp_extra));
2828		if (!beacon->assocresp_ies)
2829			goto free_beacon;
2830
2831		os_memcpy(beacon->assocresp_ies, assocresp_extra->buf,
2832			  wpabuf_len(assocresp_extra));
2833		beacon->assocresp_ies_len = wpabuf_len(assocresp_extra);
2834	}
2835
2836	ret = 0;
2837free_beacon:
2838	/* if the function fails, the caller should not free beacon data */
2839	if (ret)
2840		free_beacon_data(beacon);
2841
2842free_ap_extra_ies:
2843	hostapd_free_ap_extra_ies(hapd, beacon_extra, proberesp_extra,
2844				  assocresp_extra);
2845free_ap_params:
2846	ieee802_11_free_ap_params(&params);
2847	return ret;
2848}
2849
2850
2851/*
2852 * TODO: This flow currently supports only changing channel and width within
2853 * the same hw_mode. Any other changes to MAC parameters or provided settings
2854 * are not supported.
2855 */
2856static int hostapd_change_config_freq(struct hostapd_data *hapd,
2857				      struct hostapd_config *conf,
2858				      struct hostapd_freq_params *params,
2859				      struct hostapd_freq_params *old_params)
2860{
2861	int channel;
2862
2863	if (!params->channel) {
2864		/* check if the new channel is supported by hw */
2865		params->channel = hostapd_hw_get_channel(hapd, params->freq);
2866	}
2867
2868	channel = params->channel;
2869	if (!channel)
2870		return -1;
2871
2872	/* if a pointer to old_params is provided we save previous state */
2873	if (old_params &&
2874	    hostapd_set_freq_params(old_params, conf->hw_mode,
2875				    hostapd_hw_get_freq(hapd, conf->channel),
2876				    conf->channel, conf->ieee80211n,
2877				    conf->ieee80211ac,
2878				    conf->secondary_channel,
2879				    conf->vht_oper_chwidth,
2880				    conf->vht_oper_centr_freq_seg0_idx,
2881				    conf->vht_oper_centr_freq_seg1_idx,
2882				    conf->vht_capab))
2883		return -1;
2884
2885	switch (params->bandwidth) {
2886	case 0:
2887	case 20:
2888	case 40:
2889		conf->vht_oper_chwidth = VHT_CHANWIDTH_USE_HT;
2890		break;
2891	case 80:
2892		if (params->center_freq2)
2893			conf->vht_oper_chwidth = VHT_CHANWIDTH_80P80MHZ;
2894		else
2895			conf->vht_oper_chwidth = VHT_CHANWIDTH_80MHZ;
2896		break;
2897	case 160:
2898		conf->vht_oper_chwidth = VHT_CHANWIDTH_160MHZ;
2899		break;
2900	default:
2901		return -1;
2902	}
2903
2904	conf->channel = channel;
2905	conf->ieee80211n = params->ht_enabled;
2906	conf->secondary_channel = params->sec_channel_offset;
2907	ieee80211_freq_to_chan(params->center_freq1,
2908			       &conf->vht_oper_centr_freq_seg0_idx);
2909	ieee80211_freq_to_chan(params->center_freq2,
2910			       &conf->vht_oper_centr_freq_seg1_idx);
2911
2912	/* TODO: maybe call here hostapd_config_check here? */
2913
2914	return 0;
2915}
2916
2917
2918static int hostapd_fill_csa_settings(struct hostapd_data *hapd,
2919				     struct csa_settings *settings)
2920{
2921	struct hostapd_iface *iface = hapd->iface;
2922	struct hostapd_freq_params old_freq;
2923	int ret;
2924	u8 chan, vht_bandwidth;
2925
2926	os_memset(&old_freq, 0, sizeof(old_freq));
2927	if (!iface || !iface->freq || hapd->csa_in_progress)
2928		return -1;
2929
2930	switch (settings->freq_params.bandwidth) {
2931	case 80:
2932		if (settings->freq_params.center_freq2)
2933			vht_bandwidth = VHT_CHANWIDTH_80P80MHZ;
2934		else
2935			vht_bandwidth = VHT_CHANWIDTH_80MHZ;
2936		break;
2937	case 160:
2938		vht_bandwidth = VHT_CHANWIDTH_160MHZ;
2939		break;
2940	default:
2941		vht_bandwidth = VHT_CHANWIDTH_USE_HT;
2942		break;
2943	}
2944
2945	if (ieee80211_freq_to_channel_ext(
2946		    settings->freq_params.freq,
2947		    settings->freq_params.sec_channel_offset,
2948		    vht_bandwidth,
2949		    &hapd->iface->cs_oper_class,
2950		    &chan) == NUM_HOSTAPD_MODES) {
2951		wpa_printf(MSG_DEBUG,
2952			   "invalid frequency for channel switch (freq=%d, sec_channel_offset=%d, vht_enabled=%d)",
2953			   settings->freq_params.freq,
2954			   settings->freq_params.sec_channel_offset,
2955			   settings->freq_params.vht_enabled);
2956		return -1;
2957	}
2958
2959	settings->freq_params.channel = chan;
2960
2961	ret = hostapd_change_config_freq(iface->bss[0], iface->conf,
2962					 &settings->freq_params,
2963					 &old_freq);
2964	if (ret)
2965		return ret;
2966
2967	ret = hostapd_build_beacon_data(hapd, &settings->beacon_after);
2968
2969	/* change back the configuration */
2970	hostapd_change_config_freq(iface->bss[0], iface->conf,
2971				   &old_freq, NULL);
2972
2973	if (ret)
2974		return ret;
2975
2976	/* set channel switch parameters for csa ie */
2977	hapd->cs_freq_params = settings->freq_params;
2978	hapd->cs_count = settings->cs_count;
2979	hapd->cs_block_tx = settings->block_tx;
2980
2981	ret = hostapd_build_beacon_data(hapd, &settings->beacon_csa);
2982	if (ret) {
2983		free_beacon_data(&settings->beacon_after);
2984		return ret;
2985	}
2986
2987	settings->counter_offset_beacon[0] = hapd->cs_c_off_beacon;
2988	settings->counter_offset_presp[0] = hapd->cs_c_off_proberesp;
2989	settings->counter_offset_beacon[1] = hapd->cs_c_off_ecsa_beacon;
2990	settings->counter_offset_presp[1] = hapd->cs_c_off_ecsa_proberesp;
2991
2992	return 0;
2993}
2994
2995
2996void hostapd_cleanup_cs_params(struct hostapd_data *hapd)
2997{
2998	os_memset(&hapd->cs_freq_params, 0, sizeof(hapd->cs_freq_params));
2999	hapd->cs_count = 0;
3000	hapd->cs_block_tx = 0;
3001	hapd->cs_c_off_beacon = 0;
3002	hapd->cs_c_off_proberesp = 0;
3003	hapd->csa_in_progress = 0;
3004	hapd->cs_c_off_ecsa_beacon = 0;
3005	hapd->cs_c_off_ecsa_proberesp = 0;
3006}
3007
3008
3009int hostapd_switch_channel(struct hostapd_data *hapd,
3010			   struct csa_settings *settings)
3011{
3012	int ret;
3013
3014	if (!(hapd->iface->drv_flags & WPA_DRIVER_FLAGS_AP_CSA)) {
3015		wpa_printf(MSG_INFO, "CSA is not supported");
3016		return -1;
3017	}
3018
3019	ret = hostapd_fill_csa_settings(hapd, settings);
3020	if (ret)
3021		return ret;
3022
3023	ret = hostapd_drv_switch_channel(hapd, settings);
3024	free_beacon_data(&settings->beacon_csa);
3025	free_beacon_data(&settings->beacon_after);
3026
3027	if (ret) {
3028		/* if we failed, clean cs parameters */
3029		hostapd_cleanup_cs_params(hapd);
3030		return ret;
3031	}
3032
3033	hapd->csa_in_progress = 1;
3034	return 0;
3035}
3036
3037
3038void
3039hostapd_switch_channel_fallback(struct hostapd_iface *iface,
3040				const struct hostapd_freq_params *freq_params)
3041{
3042	int vht_seg0_idx = 0, vht_seg1_idx = 0, vht_bw = VHT_CHANWIDTH_USE_HT;
3043	unsigned int i;
3044
3045	wpa_printf(MSG_DEBUG, "Restarting all CSA-related BSSes");
3046
3047	if (freq_params->center_freq1)
3048		vht_seg0_idx = 36 + (freq_params->center_freq1 - 5180) / 5;
3049	if (freq_params->center_freq2)
3050		vht_seg1_idx = 36 + (freq_params->center_freq2 - 5180) / 5;
3051
3052	switch (freq_params->bandwidth) {
3053	case 0:
3054	case 20:
3055	case 40:
3056		vht_bw = VHT_CHANWIDTH_USE_HT;
3057		break;
3058	case 80:
3059		if (freq_params->center_freq2)
3060			vht_bw = VHT_CHANWIDTH_80P80MHZ;
3061		else
3062			vht_bw = VHT_CHANWIDTH_80MHZ;
3063		break;
3064	case 160:
3065		vht_bw = VHT_CHANWIDTH_160MHZ;
3066		break;
3067	default:
3068		wpa_printf(MSG_WARNING, "Unknown CSA bandwidth: %d",
3069			   freq_params->bandwidth);
3070		break;
3071	}
3072
3073	iface->freq = freq_params->freq;
3074	iface->conf->channel = freq_params->channel;
3075	iface->conf->secondary_channel = freq_params->sec_channel_offset;
3076	iface->conf->vht_oper_centr_freq_seg0_idx = vht_seg0_idx;
3077	iface->conf->vht_oper_centr_freq_seg1_idx = vht_seg1_idx;
3078	iface->conf->vht_oper_chwidth = vht_bw;
3079	iface->conf->ieee80211n = freq_params->ht_enabled;
3080	iface->conf->ieee80211ac = freq_params->vht_enabled;
3081
3082	/*
3083	 * cs_params must not be cleared earlier because the freq_params
3084	 * argument may actually point to one of these.
3085	 */
3086	for (i = 0; i < iface->num_bss; i++)
3087		hostapd_cleanup_cs_params(iface->bss[i]);
3088
3089	hostapd_disable_iface(iface);
3090	hostapd_enable_iface(iface);
3091}
3092
3093#endif /* NEED_AP_MLME */
3094
3095
3096struct hostapd_data * hostapd_get_iface(struct hapd_interfaces *interfaces,
3097					const char *ifname)
3098{
3099	size_t i, j;
3100
3101	for (i = 0; i < interfaces->count; i++) {
3102		struct hostapd_iface *iface = interfaces->iface[i];
3103
3104		for (j = 0; j < iface->num_bss; j++) {
3105			struct hostapd_data *hapd = iface->bss[j];
3106
3107			if (os_strcmp(ifname, hapd->conf->iface) == 0)
3108				return hapd;
3109		}
3110	}
3111
3112	return NULL;
3113}
3114
3115
3116void hostapd_periodic_iface(struct hostapd_iface *iface)
3117{
3118	size_t i;
3119
3120	ap_list_timer(iface);
3121
3122	for (i = 0; i < iface->num_bss; i++) {
3123		struct hostapd_data *hapd = iface->bss[i];
3124
3125		if (!hapd->started)
3126			continue;
3127
3128#ifndef CONFIG_NO_RADIUS
3129		hostapd_acl_expire(hapd);
3130#endif /* CONFIG_NO_RADIUS */
3131	}
3132}
3133