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