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