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