xref: /freebsd/contrib/wpa/src/ap/hostapd.c (revision a90b9d0159070121c221b966469c3e36d912bf82)
1 /*
2  * hostapd / Initialization and configuration
3  * Copyright (c) 2002-2021, Jouni Malinen <j@w1.fi>
4  *
5  * This software may be distributed under the terms of the BSD license.
6  * See README for more details.
7  */
8 
9 #include "utils/includes.h"
10 #ifdef CONFIG_SQLITE
11 #include <sqlite3.h>
12 #endif /* CONFIG_SQLITE */
13 
14 #include "utils/common.h"
15 #include "utils/eloop.h"
16 #include "utils/crc32.h"
17 #include "common/ieee802_11_defs.h"
18 #include "common/wpa_ctrl.h"
19 #include "common/hw_features_common.h"
20 #include "radius/radius_client.h"
21 #include "radius/radius_das.h"
22 #include "eap_server/tncs.h"
23 #include "eapol_auth/eapol_auth_sm.h"
24 #include "eapol_auth/eapol_auth_sm_i.h"
25 #include "fst/fst.h"
26 #include "hostapd.h"
27 #include "authsrv.h"
28 #include "sta_info.h"
29 #include "accounting.h"
30 #include "ap_list.h"
31 #include "beacon.h"
32 #include "ieee802_1x.h"
33 #include "ieee802_11_auth.h"
34 #include "vlan_init.h"
35 #include "wpa_auth.h"
36 #include "wps_hostapd.h"
37 #include "dpp_hostapd.h"
38 #include "nan_usd_ap.h"
39 #include "gas_query_ap.h"
40 #include "hw_features.h"
41 #include "wpa_auth_glue.h"
42 #include "ap_drv_ops.h"
43 #include "ap_config.h"
44 #include "p2p_hostapd.h"
45 #include "gas_serv.h"
46 #include "dfs.h"
47 #include "ieee802_11.h"
48 #include "bss_load.h"
49 #include "x_snoop.h"
50 #include "dhcp_snoop.h"
51 #include "ndisc_snoop.h"
52 #include "neighbor_db.h"
53 #include "rrm.h"
54 #include "fils_hlp.h"
55 #include "acs.h"
56 #include "hs20.h"
57 #include "airtime_policy.h"
58 #include "wpa_auth_kay.h"
59 #include "hw_features.h"
60 
61 
62 static int hostapd_flush_old_stations(struct hostapd_data *hapd, u16 reason);
63 #ifdef CONFIG_WEP
64 static int hostapd_setup_encryption(char *iface, struct hostapd_data *hapd);
65 static int hostapd_broadcast_wep_clear(struct hostapd_data *hapd);
66 #endif /* CONFIG_WEP */
67 static int setup_interface2(struct hostapd_iface *iface);
68 static void channel_list_update_timeout(void *eloop_ctx, void *timeout_ctx);
69 static void hostapd_interface_setup_failure_handler(void *eloop_ctx,
70 						    void *timeout_ctx);
71 #ifdef CONFIG_IEEE80211AX
72 static void hostapd_switch_color_timeout_handler(void *eloop_data,
73 						 void *user_ctx);
74 #endif /* CONFIG_IEEE80211AX */
75 
76 
hostapd_for_each_interface(struct hapd_interfaces * interfaces,int (* cb)(struct hostapd_iface * iface,void * ctx),void * ctx)77 int hostapd_for_each_interface(struct hapd_interfaces *interfaces,
78 			       int (*cb)(struct hostapd_iface *iface,
79 					 void *ctx), void *ctx)
80 {
81 	size_t i;
82 	int ret;
83 
84 	for (i = 0; i < interfaces->count; i++) {
85 		if (!interfaces->iface[i])
86 			continue;
87 		ret = cb(interfaces->iface[i], ctx);
88 		if (ret)
89 			return ret;
90 	}
91 
92 	return 0;
93 }
94 
95 
hostapd_mbssid_get_tx_bss(struct hostapd_data * hapd)96 struct hostapd_data * hostapd_mbssid_get_tx_bss(struct hostapd_data *hapd)
97 {
98 	if (hapd->iconf->mbssid)
99 		return hapd->iface->bss[0];
100 
101 	return hapd;
102 }
103 
104 
hostapd_mbssid_get_bss_index(struct hostapd_data * hapd)105 int hostapd_mbssid_get_bss_index(struct hostapd_data *hapd)
106 {
107 	if (hapd->iconf->mbssid) {
108 		size_t i;
109 
110 		for (i = 1; i < hapd->iface->num_bss; i++)
111 			if (hapd->iface->bss[i] == hapd)
112 				return i;
113 	}
114 
115 	return 0;
116 }
117 
118 
hostapd_reconfig_encryption(struct hostapd_data * hapd)119 void hostapd_reconfig_encryption(struct hostapd_data *hapd)
120 {
121 	if (hapd->wpa_auth)
122 		return;
123 
124 	hostapd_set_privacy(hapd, 0);
125 #ifdef CONFIG_WEP
126 	hostapd_setup_encryption(hapd->conf->iface, hapd);
127 #endif /* CONFIG_WEP */
128 }
129 
130 
hostapd_reload_bss(struct hostapd_data * hapd)131 static void hostapd_reload_bss(struct hostapd_data *hapd)
132 {
133 	struct hostapd_ssid *ssid;
134 
135 	if (!hapd->started)
136 		return;
137 
138 	if (hapd->conf->wmm_enabled < 0)
139 		hapd->conf->wmm_enabled = hapd->iconf->ieee80211n |
140 			hapd->iconf->ieee80211ax;
141 
142 #ifndef CONFIG_NO_RADIUS
143 	radius_client_reconfig(hapd->radius, hapd->conf->radius);
144 #endif /* CONFIG_NO_RADIUS */
145 
146 	ssid = &hapd->conf->ssid;
147 	if (!ssid->wpa_psk_set && ssid->wpa_psk && !ssid->wpa_psk->next &&
148 	    ssid->wpa_passphrase_set && ssid->wpa_passphrase) {
149 		/*
150 		 * Force PSK to be derived again since SSID or passphrase may
151 		 * have changed.
152 		 */
153 		hostapd_config_clear_wpa_psk(&hapd->conf->ssid.wpa_psk);
154 	}
155 	if (hostapd_setup_wpa_psk(hapd->conf)) {
156 		wpa_printf(MSG_ERROR, "Failed to re-configure WPA PSK "
157 			   "after reloading configuration");
158 	}
159 
160 	if (hapd->conf->ieee802_1x || hapd->conf->wpa)
161 		hostapd_set_drv_ieee8021x(hapd, hapd->conf->iface, 1);
162 	else
163 		hostapd_set_drv_ieee8021x(hapd, hapd->conf->iface, 0);
164 
165 	if ((hapd->conf->wpa || hapd->conf->osen) && hapd->wpa_auth == NULL) {
166 		hostapd_setup_wpa(hapd);
167 		if (hapd->wpa_auth)
168 			wpa_init_keys(hapd->wpa_auth);
169 	} else if (hapd->conf->wpa) {
170 		const u8 *wpa_ie;
171 		size_t wpa_ie_len;
172 		hostapd_reconfig_wpa(hapd);
173 		wpa_ie = wpa_auth_get_wpa_ie(hapd->wpa_auth, &wpa_ie_len);
174 		if (hostapd_set_generic_elem(hapd, wpa_ie, wpa_ie_len))
175 			wpa_printf(MSG_ERROR, "Failed to configure WPA IE for "
176 				   "the kernel driver.");
177 	} else if (hapd->wpa_auth) {
178 		wpa_deinit(hapd->wpa_auth);
179 		hapd->wpa_auth = NULL;
180 		hostapd_set_privacy(hapd, 0);
181 #ifdef CONFIG_WEP
182 		hostapd_setup_encryption(hapd->conf->iface, hapd);
183 #endif /* CONFIG_WEP */
184 		hostapd_set_generic_elem(hapd, (u8 *) "", 0);
185 	}
186 
187 	hostapd_neighbor_sync_own_report(hapd);
188 
189 	ieee802_11_set_beacon(hapd);
190 	hostapd_update_wps(hapd);
191 
192 	if (hapd->conf->ssid.ssid_set &&
193 	    hostapd_set_ssid(hapd, hapd->conf->ssid.ssid,
194 			     hapd->conf->ssid.ssid_len)) {
195 		wpa_printf(MSG_ERROR, "Could not set SSID for kernel driver");
196 		/* try to continue */
197 	}
198 	wpa_printf(MSG_DEBUG, "Reconfigured interface %s", hapd->conf->iface);
199 }
200 
201 
hostapd_clear_old_bss(struct hostapd_data * bss)202 static void hostapd_clear_old_bss(struct hostapd_data *bss)
203 {
204 	wpa_printf(MSG_DEBUG, "BSS %s changed - clear old state",
205 		   bss->conf->iface);
206 
207 	/*
208 	 * Deauthenticate all stations since the new configuration may not
209 	 * allow them to use the BSS anymore.
210 	 */
211 	hostapd_flush_old_stations(bss, WLAN_REASON_PREV_AUTH_NOT_VALID);
212 #ifdef CONFIG_WEP
213 	hostapd_broadcast_wep_clear(bss);
214 #endif /* CONFIG_WEP */
215 
216 #ifndef CONFIG_NO_RADIUS
217 	/* TODO: update dynamic data based on changed configuration
218 	 * items (e.g., open/close sockets, etc.) */
219 	radius_client_flush(bss->radius, 0);
220 #endif /* CONFIG_NO_RADIUS */
221 }
222 
223 
hostapd_clear_old(struct hostapd_iface * iface)224 static void hostapd_clear_old(struct hostapd_iface *iface)
225 {
226 	size_t j;
227 
228 	for (j = 0; j < iface->num_bss; j++)
229 		hostapd_clear_old_bss(iface->bss[j]);
230 }
231 
232 
hostapd_iface_conf_changed(struct hostapd_config * newconf,struct hostapd_config * oldconf)233 static int hostapd_iface_conf_changed(struct hostapd_config *newconf,
234 				      struct hostapd_config *oldconf)
235 {
236 	size_t i;
237 
238 	if (newconf->num_bss != oldconf->num_bss)
239 		return 1;
240 
241 	for (i = 0; i < newconf->num_bss; i++) {
242 		if (os_strcmp(newconf->bss[i]->iface,
243 			      oldconf->bss[i]->iface) != 0)
244 			return 1;
245 	}
246 
247 	return 0;
248 }
249 
250 
hostapd_reload_config(struct hostapd_iface * iface)251 int hostapd_reload_config(struct hostapd_iface *iface)
252 {
253 	struct hapd_interfaces *interfaces = iface->interfaces;
254 	struct hostapd_data *hapd = iface->bss[0];
255 	struct hostapd_config *newconf, *oldconf;
256 	size_t j;
257 
258 	if (iface->config_fname == NULL) {
259 		/* Only in-memory config in use - assume it has been updated */
260 		hostapd_clear_old(iface);
261 		for (j = 0; j < iface->num_bss; j++)
262 			hostapd_reload_bss(iface->bss[j]);
263 		return 0;
264 	}
265 
266 	if (iface->interfaces == NULL ||
267 	    iface->interfaces->config_read_cb == NULL)
268 		return -1;
269 	newconf = iface->interfaces->config_read_cb(iface->config_fname);
270 	if (newconf == NULL)
271 		return -1;
272 
273 	oldconf = hapd->iconf;
274 	if (hostapd_iface_conf_changed(newconf, oldconf)) {
275 		char *fname;
276 		int res;
277 
278 		hostapd_clear_old(iface);
279 
280 		wpa_printf(MSG_DEBUG,
281 			   "Configuration changes include interface/BSS modification - force full disable+enable sequence");
282 		fname = os_strdup(iface->config_fname);
283 		if (!fname) {
284 			hostapd_config_free(newconf);
285 			return -1;
286 		}
287 		hostapd_remove_iface(interfaces, hapd->conf->iface);
288 		iface = hostapd_init(interfaces, fname);
289 		os_free(fname);
290 		hostapd_config_free(newconf);
291 		if (!iface) {
292 			wpa_printf(MSG_ERROR,
293 				   "Failed to initialize interface on config reload");
294 			return -1;
295 		}
296 		iface->interfaces = interfaces;
297 		interfaces->iface[interfaces->count] = iface;
298 		interfaces->count++;
299 		res = hostapd_enable_iface(iface);
300 		if (res < 0)
301 			wpa_printf(MSG_ERROR,
302 				   "Failed to enable interface on config reload");
303 		return res;
304 	}
305 	iface->conf = newconf;
306 
307 	for (j = 0; j < iface->num_bss; j++) {
308 		hapd = iface->bss[j];
309 		if (!hapd->conf->config_id || !newconf->bss[j]->config_id ||
310 		    os_strcmp(hapd->conf->config_id,
311 			      newconf->bss[j]->config_id) != 0)
312 			hostapd_clear_old_bss(hapd);
313 		hapd->iconf = newconf;
314 		hapd->iconf->channel = oldconf->channel;
315 		hapd->iconf->acs = oldconf->acs;
316 		hapd->iconf->secondary_channel = oldconf->secondary_channel;
317 		hapd->iconf->ieee80211n = oldconf->ieee80211n;
318 		hapd->iconf->ieee80211ac = oldconf->ieee80211ac;
319 		hapd->iconf->ht_capab = oldconf->ht_capab;
320 		hapd->iconf->vht_capab = oldconf->vht_capab;
321 		hostapd_set_oper_chwidth(hapd->iconf,
322 					 hostapd_get_oper_chwidth(oldconf));
323 		hostapd_set_oper_centr_freq_seg0_idx(
324 			hapd->iconf,
325 			hostapd_get_oper_centr_freq_seg0_idx(oldconf));
326 		hostapd_set_oper_centr_freq_seg1_idx(
327 			hapd->iconf,
328 			hostapd_get_oper_centr_freq_seg1_idx(oldconf));
329 		hapd->conf = newconf->bss[j];
330 		hostapd_reload_bss(hapd);
331 	}
332 
333 	hostapd_config_free(oldconf);
334 
335 
336 	return 0;
337 }
338 
339 
340 #ifdef CONFIG_WEP
341 
hostapd_broadcast_key_clear_iface(struct hostapd_data * hapd,const char * ifname)342 static void hostapd_broadcast_key_clear_iface(struct hostapd_data *hapd,
343 					      const char *ifname)
344 {
345 	int i;
346 
347 	if (!ifname || !hapd->drv_priv)
348 		return;
349 	for (i = 0; i < NUM_WEP_KEYS; i++) {
350 		if (hostapd_drv_set_key(ifname, hapd, WPA_ALG_NONE, NULL, i, 0,
351 					0, NULL, 0, NULL, 0, KEY_FLAG_GROUP)) {
352 			wpa_printf(MSG_DEBUG, "Failed to clear default "
353 				   "encryption keys (ifname=%s keyidx=%d)",
354 				   ifname, i);
355 		}
356 	}
357 	if (hapd->conf->ieee80211w) {
358 		for (i = NUM_WEP_KEYS; i < NUM_WEP_KEYS + 2; i++) {
359 			if (hostapd_drv_set_key(ifname, hapd, WPA_ALG_NONE,
360 						NULL, i, 0, 0, NULL,
361 						0, NULL, 0, KEY_FLAG_GROUP)) {
362 				wpa_printf(MSG_DEBUG, "Failed to clear "
363 					   "default mgmt encryption keys "
364 					   "(ifname=%s keyidx=%d)", ifname, i);
365 			}
366 		}
367 	}
368 }
369 
370 
hostapd_broadcast_wep_clear(struct hostapd_data * hapd)371 static int hostapd_broadcast_wep_clear(struct hostapd_data *hapd)
372 {
373 	hostapd_broadcast_key_clear_iface(hapd, hapd->conf->iface);
374 	return 0;
375 }
376 
377 
hostapd_broadcast_wep_set(struct hostapd_data * hapd)378 static int hostapd_broadcast_wep_set(struct hostapd_data *hapd)
379 {
380 	int errors = 0, idx;
381 	struct hostapd_ssid *ssid = &hapd->conf->ssid;
382 
383 	idx = ssid->wep.idx;
384 	if (ssid->wep.default_len && ssid->wep.key[idx] &&
385 	    hostapd_drv_set_key(hapd->conf->iface,
386 				hapd, WPA_ALG_WEP, broadcast_ether_addr, idx, 0,
387 				1, NULL, 0, ssid->wep.key[idx],
388 				ssid->wep.len[idx],
389 				KEY_FLAG_GROUP_RX_TX_DEFAULT)) {
390 		wpa_printf(MSG_WARNING, "Could not set WEP encryption.");
391 		errors++;
392 	}
393 
394 	return errors;
395 }
396 
397 #endif /* CONFIG_WEP */
398 
399 
400 #ifdef CONFIG_IEEE80211BE
401 #ifdef CONFIG_TESTING_OPTIONS
402 
403 #define TU_TO_USEC(_val) ((_val) * 1024)
404 
hostapd_link_remove_timeout_handler(void * eloop_data,void * user_ctx)405 static void hostapd_link_remove_timeout_handler(void *eloop_data,
406 						void *user_ctx)
407 {
408 	struct hostapd_data *hapd = (struct hostapd_data *) eloop_data;
409 
410 	if (hapd->eht_mld_link_removal_count == 0)
411 		return;
412 	hapd->eht_mld_link_removal_count--;
413 
414 	wpa_printf(MSG_DEBUG, "MLD: Remove link_id=%u in %u beacons",
415 		   hapd->mld_link_id,
416 		   hapd->eht_mld_link_removal_count);
417 
418 	ieee802_11_set_beacon(hapd);
419 
420 	if (!hapd->eht_mld_link_removal_count) {
421 		hostapd_free_link_stas(hapd);
422 		hostapd_disable_iface(hapd->iface);
423 		return;
424 	}
425 
426 	eloop_register_timeout(0, TU_TO_USEC(hapd->iconf->beacon_int),
427 			       hostapd_link_remove_timeout_handler,
428 			       hapd, NULL);
429 }
430 
431 
hostapd_link_remove(struct hostapd_data * hapd,u32 count)432 int hostapd_link_remove(struct hostapd_data *hapd, u32 count)
433 {
434 	if (!hapd->conf->mld_ap)
435 		return -1;
436 
437 	wpa_printf(MSG_DEBUG,
438 		   "MLD: Remove link_id=%u in %u beacons",
439 		   hapd->mld_link_id, count);
440 
441 	hapd->eht_mld_link_removal_count = count;
442 	hapd->eht_mld_bss_param_change++;
443 
444 	eloop_register_timeout(0, TU_TO_USEC(hapd->iconf->beacon_int),
445 			       hostapd_link_remove_timeout_handler,
446 			       hapd, NULL);
447 
448 	ieee802_11_set_beacon(hapd);
449 	return 0;
450 }
451 
452 #endif /* CONFIG_TESTING_OPTIONS */
453 #endif /* CONFIG_IEEE80211BE */
454 
455 
hostapd_free_hapd_data(struct hostapd_data * hapd)456 void hostapd_free_hapd_data(struct hostapd_data *hapd)
457 {
458 	os_free(hapd->probereq_cb);
459 	hapd->probereq_cb = NULL;
460 	hapd->num_probereq_cb = 0;
461 
462 #ifdef CONFIG_P2P
463 	wpabuf_free(hapd->p2p_beacon_ie);
464 	hapd->p2p_beacon_ie = NULL;
465 	wpabuf_free(hapd->p2p_probe_resp_ie);
466 	hapd->p2p_probe_resp_ie = NULL;
467 #endif /* CONFIG_P2P */
468 
469 	if (!hapd->started) {
470 		wpa_printf(MSG_ERROR, "%s: Interface %s wasn't started",
471 			   __func__, hapd->conf ? hapd->conf->iface : "N/A");
472 		return;
473 	}
474 	hapd->started = 0;
475 	hapd->beacon_set_done = 0;
476 
477 	wpa_printf(MSG_DEBUG, "%s(%s)", __func__, hapd->conf->iface);
478 	accounting_deinit(hapd);
479 	hostapd_deinit_wpa(hapd);
480 	vlan_deinit(hapd);
481 	hostapd_acl_deinit(hapd);
482 #ifndef CONFIG_NO_RADIUS
483 	if (hostapd_mld_is_first_bss(hapd)) {
484 #ifdef CONFIG_IEEE80211BE
485 		struct hapd_interfaces *ifaces = hapd->iface->interfaces;
486 		size_t i;
487 
488 		for (i = 0; i < ifaces->count; i++) {
489 			struct hostapd_iface *iface = ifaces->iface[i];
490 			size_t j;
491 
492 			for (j = 0; iface && j < iface->num_bss; j++) {
493 				struct hostapd_data *h = iface->bss[j];
494 
495 				if (hapd == h)
496 					continue;
497 				if (h->radius == hapd->radius)
498 					h->radius = NULL;
499 				if (h->radius_das == hapd->radius_das)
500 					h->radius_das = NULL;
501 			}
502 		}
503 #endif /* CONFIG_IEEE80211BE */
504 		radius_client_deinit(hapd->radius);
505 		radius_das_deinit(hapd->radius_das);
506 	}
507 	hapd->radius = NULL;
508 	hapd->radius_das = NULL;
509 #endif /* CONFIG_NO_RADIUS */
510 
511 	hostapd_deinit_wps(hapd);
512 	ieee802_1x_dealloc_kay_sm_hapd(hapd);
513 #ifdef CONFIG_DPP
514 	hostapd_dpp_deinit(hapd);
515 	gas_query_ap_deinit(hapd->gas);
516 	hapd->gas = NULL;
517 #endif /* CONFIG_DPP */
518 #ifdef CONFIG_NAN_USD
519 	hostapd_nan_usd_deinit(hapd);
520 #endif /* CONFIG_NAN_USD */
521 
522 	authsrv_deinit(hapd);
523 
524 	if (hapd->interface_added) {
525 		hapd->interface_added = 0;
526 		if (hostapd_if_remove(hapd, WPA_IF_AP_BSS, hapd->conf->iface)) {
527 			wpa_printf(MSG_WARNING,
528 				   "Failed to remove BSS interface %s",
529 				   hapd->conf->iface);
530 			hapd->interface_added = 1;
531 		} else {
532 			/*
533 			 * Since this was a dynamically added interface, the
534 			 * driver wrapper may have removed its internal instance
535 			 * and hapd->drv_priv is not valid anymore.
536 			 */
537 			hapd->drv_priv = NULL;
538 		}
539 	}
540 
541 #ifdef CONFIG_IEEE80211BE
542 	/* If the interface was not added as well as it is not the first BSS,
543 	 * at least the link should be removed here since deinit will take care
544 	 * of only the first BSS. */
545 	if (hapd->conf->mld_ap && !hapd->interface_added &&
546 	    hapd->iface->bss[0] != hapd)
547 		hostapd_if_link_remove(hapd, WPA_IF_AP_BSS, hapd->conf->iface,
548 				       hapd->mld_link_id);
549 #endif /* CONFIG_IEEE80211BE */
550 
551 	wpabuf_free(hapd->time_adv);
552 	hapd->time_adv = NULL;
553 
554 #ifdef CONFIG_INTERWORKING
555 	gas_serv_deinit(hapd);
556 #endif /* CONFIG_INTERWORKING */
557 
558 	bss_load_update_deinit(hapd);
559 	ndisc_snoop_deinit(hapd);
560 	dhcp_snoop_deinit(hapd);
561 	x_snoop_deinit(hapd);
562 
563 #ifdef CONFIG_SQLITE
564 	bin_clear_free(hapd->tmp_eap_user.identity,
565 		       hapd->tmp_eap_user.identity_len);
566 	bin_clear_free(hapd->tmp_eap_user.password,
567 		       hapd->tmp_eap_user.password_len);
568 	os_memset(&hapd->tmp_eap_user, 0, sizeof(hapd->tmp_eap_user));
569 #endif /* CONFIG_SQLITE */
570 
571 #ifdef CONFIG_MESH
572 	wpabuf_free(hapd->mesh_pending_auth);
573 	hapd->mesh_pending_auth = NULL;
574 	/* handling setup failure is already done */
575 	hapd->setup_complete_cb = NULL;
576 #endif /* CONFIG_MESH */
577 
578 #ifndef CONFIG_NO_RRM
579 	hostapd_clean_rrm(hapd);
580 #endif /* CONFIG_NO_RRM */
581 	fils_hlp_deinit(hapd);
582 
583 #ifdef CONFIG_OCV
584 	eloop_cancel_timeout(hostapd_ocv_check_csa_sa_query, hapd, NULL);
585 #endif /* CONFIG_OCV */
586 
587 #ifdef CONFIG_SAE
588 	{
589 		struct hostapd_sae_commit_queue *q;
590 
591 		while ((q = dl_list_first(&hapd->sae_commit_queue,
592 					  struct hostapd_sae_commit_queue,
593 					  list))) {
594 			dl_list_del(&q->list);
595 			os_free(q);
596 		}
597 	}
598 	eloop_cancel_timeout(auth_sae_process_commit, hapd, NULL);
599 #endif /* CONFIG_SAE */
600 
601 #ifdef CONFIG_IEEE80211AX
602 	eloop_cancel_timeout(hostapd_switch_color_timeout_handler, hapd, NULL);
603 #ifdef CONFIG_TESTING_OPTIONS
604 #ifdef CONFIG_IEEE80211BE
605 	eloop_cancel_timeout(hostapd_link_remove_timeout_handler, hapd, NULL);
606 #endif /* CONFIG_IEEE80211BE */
607 #endif /* CONFIG_TESTING_OPTIONS */
608 
609 #endif /* CONFIG_IEEE80211AX */
610 }
611 
612 
613 /* hostapd_bss_link_deinit - Per-BSS ML cleanup (deinitialization)
614  * @hapd: Pointer to BSS data
615  *
616  * This function is used to unlink the BSS from the AP MLD.
617  * If the BSS being removed is the first link, the next link becomes the first
618  * link.
619  */
hostapd_bss_link_deinit(struct hostapd_data * hapd)620 static void hostapd_bss_link_deinit(struct hostapd_data *hapd)
621 {
622 #ifdef CONFIG_IEEE80211BE
623 	if (!hapd->conf || !hapd->conf->mld_ap)
624 		return;
625 
626 	if (!hapd->mld->num_links)
627 		return;
628 
629 	/* If not started, not yet linked to the MLD. However, the first
630 	 * BSS is always linked since it is linked during driver_init(), and
631 	 * hence, need to remove it from the AP MLD.
632 	 */
633 	if (!hapd->started && hapd->iface->bss[0] != hapd)
634 		return;
635 
636 	/* The first BSS can also be only linked when at least driver_init() is
637 	 * executed. But if previous interface fails, it is not, and hence,
638 	 * safe to skip.
639 	 */
640 	if (hapd->iface->bss[0] == hapd && !hapd->drv_priv)
641 		return;
642 
643 	hostapd_mld_remove_link(hapd);
644 #endif /* CONFIG_IEEE80211BE */
645 }
646 
647 
648 /**
649  * hostapd_cleanup - Per-BSS cleanup (deinitialization)
650  * @hapd: Pointer to BSS data
651  *
652  * This function is used to free all per-BSS data structures and resources.
653  * Most of the modules that are initialized in hostapd_setup_bss() are
654  * deinitialized here.
655  */
hostapd_cleanup(struct hostapd_data * hapd)656 static void hostapd_cleanup(struct hostapd_data *hapd)
657 {
658 	wpa_printf(MSG_DEBUG, "%s(hapd=%p (%s))", __func__, hapd,
659 		   hapd->conf ? hapd->conf->iface : "N/A");
660 	if (hapd->iface->interfaces &&
661 	    hapd->iface->interfaces->ctrl_iface_deinit) {
662 		wpa_msg(hapd->msg_ctx, MSG_INFO, WPA_EVENT_TERMINATING);
663 		hapd->iface->interfaces->ctrl_iface_deinit(hapd);
664 	}
665 	hostapd_free_hapd_data(hapd);
666 }
667 
668 
sta_track_deinit(struct hostapd_iface * iface)669 static void sta_track_deinit(struct hostapd_iface *iface)
670 {
671 	struct hostapd_sta_info *info;
672 
673 	if (!iface->num_sta_seen)
674 		return;
675 
676 	while ((info = dl_list_first(&iface->sta_seen, struct hostapd_sta_info,
677 				     list))) {
678 		dl_list_del(&info->list);
679 		iface->num_sta_seen--;
680 		sta_track_del(info);
681 	}
682 }
683 
684 
hostapd_cleanup_iface_partial(struct hostapd_iface * iface)685 void hostapd_cleanup_iface_partial(struct hostapd_iface *iface)
686 {
687 	wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
688 	eloop_cancel_timeout(channel_list_update_timeout, iface, NULL);
689 #ifdef NEED_AP_MLME
690 	hostapd_stop_setup_timers(iface);
691 #endif /* NEED_AP_MLME */
692 	if (iface->current_mode)
693 		acs_cleanup(iface);
694 	hostapd_free_hw_features(iface->hw_features, iface->num_hw_features);
695 	iface->hw_features = NULL;
696 	iface->current_mode = NULL;
697 	os_free(iface->current_rates);
698 	iface->current_rates = NULL;
699 	os_free(iface->basic_rates);
700 	iface->basic_rates = NULL;
701 	iface->cac_started = 0;
702 	ap_list_deinit(iface);
703 	sta_track_deinit(iface);
704 	airtime_policy_update_deinit(iface);
705 }
706 
707 
708 /**
709  * hostapd_cleanup_iface - Complete per-interface cleanup
710  * @iface: Pointer to interface data
711  *
712  * This function is called after per-BSS data structures are deinitialized
713  * with hostapd_cleanup().
714  */
hostapd_cleanup_iface(struct hostapd_iface * iface)715 static void hostapd_cleanup_iface(struct hostapd_iface *iface)
716 {
717 	wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
718 	eloop_cancel_timeout(hostapd_interface_setup_failure_handler, iface,
719 			     NULL);
720 
721 	hostapd_cleanup_iface_partial(iface);
722 	hostapd_config_free(iface->conf);
723 	iface->conf = NULL;
724 
725 	os_free(iface->config_fname);
726 	os_free(iface->bss);
727 	wpa_printf(MSG_DEBUG, "%s: free iface=%p", __func__, iface);
728 	os_free(iface);
729 }
730 
731 
732 #ifdef CONFIG_WEP
733 
hostapd_clear_wep(struct hostapd_data * hapd)734 static void hostapd_clear_wep(struct hostapd_data *hapd)
735 {
736 	if (hapd->drv_priv && !hapd->iface->driver_ap_teardown && hapd->conf) {
737 		hostapd_set_privacy(hapd, 0);
738 		hostapd_broadcast_wep_clear(hapd);
739 	}
740 }
741 
742 
hostapd_setup_encryption(char * iface,struct hostapd_data * hapd)743 static int hostapd_setup_encryption(char *iface, struct hostapd_data *hapd)
744 {
745 	int i;
746 
747 	hostapd_broadcast_wep_set(hapd);
748 
749 	if (hapd->conf->ssid.wep.default_len) {
750 		hostapd_set_privacy(hapd, 1);
751 		return 0;
752 	}
753 
754 	/*
755 	 * When IEEE 802.1X is not enabled, the driver may need to know how to
756 	 * set authentication algorithms for static WEP.
757 	 */
758 	hostapd_drv_set_authmode(hapd, hapd->conf->auth_algs);
759 
760 	for (i = 0; i < 4; i++) {
761 		if (hapd->conf->ssid.wep.key[i] &&
762 		    hostapd_drv_set_key(iface, hapd, WPA_ALG_WEP, NULL, i, 0,
763 					i == hapd->conf->ssid.wep.idx, NULL, 0,
764 					hapd->conf->ssid.wep.key[i],
765 					hapd->conf->ssid.wep.len[i],
766 					i == hapd->conf->ssid.wep.idx ?
767 					KEY_FLAG_GROUP_RX_TX_DEFAULT :
768 					KEY_FLAG_GROUP_RX_TX)) {
769 			wpa_printf(MSG_WARNING, "Could not set WEP "
770 				   "encryption.");
771 			return -1;
772 		}
773 		if (hapd->conf->ssid.wep.key[i] &&
774 		    i == hapd->conf->ssid.wep.idx)
775 			hostapd_set_privacy(hapd, 1);
776 	}
777 
778 	return 0;
779 }
780 
781 #endif /* CONFIG_WEP */
782 
783 
hostapd_flush_old_stations(struct hostapd_data * hapd,u16 reason)784 static int hostapd_flush_old_stations(struct hostapd_data *hapd, u16 reason)
785 {
786 	int ret = 0;
787 	u8 addr[ETH_ALEN];
788 
789 	if (hostapd_drv_none(hapd) || hapd->drv_priv == NULL)
790 		return 0;
791 
792 	if (!hapd->iface->driver_ap_teardown) {
793 		wpa_dbg(hapd->msg_ctx, MSG_DEBUG,
794 			"Flushing old station entries");
795 
796 		if (hostapd_flush(hapd)) {
797 			wpa_msg(hapd->msg_ctx, MSG_WARNING,
798 				"Could not connect to kernel driver");
799 			ret = -1;
800 		}
801 	}
802 	if (hapd->conf && hapd->conf->broadcast_deauth) {
803 		wpa_dbg(hapd->msg_ctx, MSG_DEBUG,
804 			"Deauthenticate all stations");
805 		os_memset(addr, 0xff, ETH_ALEN);
806 		hostapd_drv_sta_deauth(hapd, addr, reason);
807 	}
808 	hostapd_free_stas(hapd);
809 
810 	return ret;
811 }
812 
813 
hostapd_bss_deinit_no_free(struct hostapd_data * hapd)814 void hostapd_bss_deinit_no_free(struct hostapd_data *hapd)
815 {
816 	hostapd_free_stas(hapd);
817 	hostapd_flush_old_stations(hapd, WLAN_REASON_DEAUTH_LEAVING);
818 #ifdef CONFIG_WEP
819 	hostapd_clear_wep(hapd);
820 #endif /* CONFIG_WEP */
821 }
822 
823 
824 /**
825  * hostapd_validate_bssid_configuration - Validate BSSID configuration
826  * @iface: Pointer to interface data
827  * Returns: 0 on success, -1 on failure
828  *
829  * This function is used to validate that the configured BSSIDs are valid.
830  */
hostapd_validate_bssid_configuration(struct hostapd_iface * iface)831 static int hostapd_validate_bssid_configuration(struct hostapd_iface *iface)
832 {
833 	u8 mask[ETH_ALEN] = { 0 };
834 	struct hostapd_data *hapd = iface->bss[0];
835 	unsigned int i = iface->conf->num_bss, bits = 0, j;
836 	int auto_addr = 0;
837 
838 	if (hostapd_drv_none(hapd))
839 		return 0;
840 
841 	if (iface->conf->use_driver_iface_addr)
842 		return 0;
843 
844 	/* Generate BSSID mask that is large enough to cover the BSSIDs. */
845 
846 	/* Determine the bits necessary to cover the number of BSSIDs. */
847 	for (i--; i; i >>= 1)
848 		bits++;
849 
850 	/* Determine the bits necessary to any configured BSSIDs,
851 	   if they are higher than the number of BSSIDs. */
852 	for (j = 0; j < iface->conf->num_bss; j++) {
853 		if (is_zero_ether_addr(iface->conf->bss[j]->bssid)) {
854 			if (j)
855 				auto_addr++;
856 			continue;
857 		}
858 
859 		for (i = 0; i < ETH_ALEN; i++) {
860 			mask[i] |=
861 				iface->conf->bss[j]->bssid[i] ^
862 				hapd->own_addr[i];
863 		}
864 	}
865 
866 	if (!auto_addr)
867 		goto skip_mask_ext;
868 
869 	for (i = 0; i < ETH_ALEN && mask[i] == 0; i++)
870 		;
871 	j = 0;
872 	if (i < ETH_ALEN) {
873 		j = (5 - i) * 8;
874 
875 		while (mask[i] != 0) {
876 			mask[i] >>= 1;
877 			j++;
878 		}
879 	}
880 
881 	if (bits < j)
882 		bits = j;
883 
884 	if (bits > 40) {
885 		wpa_printf(MSG_ERROR, "Too many bits in the BSSID mask (%u)",
886 			   bits);
887 		return -1;
888 	}
889 
890 	os_memset(mask, 0xff, ETH_ALEN);
891 	j = bits / 8;
892 	for (i = 5; i > 5 - j; i--)
893 		mask[i] = 0;
894 	j = bits % 8;
895 	while (j) {
896 		j--;
897 		mask[i] <<= 1;
898 	}
899 
900 skip_mask_ext:
901 	wpa_printf(MSG_DEBUG, "BSS count %lu, BSSID mask " MACSTR " (%d bits)",
902 		   (unsigned long) iface->conf->num_bss, MAC2STR(mask), bits);
903 
904 	if (!auto_addr)
905 		return 0;
906 
907 	for (i = 0; i < ETH_ALEN; i++) {
908 		if ((hapd->own_addr[i] & mask[i]) != hapd->own_addr[i]) {
909 			wpa_printf(MSG_ERROR, "Invalid BSSID mask " MACSTR
910 				   " for start address " MACSTR ".",
911 				   MAC2STR(mask), MAC2STR(hapd->own_addr));
912 			wpa_printf(MSG_ERROR, "Start address must be the "
913 				   "first address in the block (i.e., addr "
914 				   "AND mask == addr).");
915 			return -1;
916 		}
917 	}
918 
919 	return 0;
920 }
921 
922 
mac_in_conf(struct hostapd_config * conf,const void * a)923 static int mac_in_conf(struct hostapd_config *conf, const void *a)
924 {
925 	size_t i;
926 
927 	for (i = 0; i < conf->num_bss; i++) {
928 		if (hostapd_mac_comp(conf->bss[i]->bssid, a) == 0) {
929 			return 1;
930 		}
931 	}
932 
933 	return 0;
934 }
935 
936 
937 #ifndef CONFIG_NO_RADIUS
938 
hostapd_das_nas_mismatch(struct hostapd_data * hapd,struct radius_das_attrs * attr)939 static int hostapd_das_nas_mismatch(struct hostapd_data *hapd,
940 				    struct radius_das_attrs *attr)
941 {
942 	if (attr->nas_identifier &&
943 	    (!hapd->conf->nas_identifier ||
944 	     os_strlen(hapd->conf->nas_identifier) !=
945 	     attr->nas_identifier_len ||
946 	     os_memcmp(hapd->conf->nas_identifier, attr->nas_identifier,
947 		       attr->nas_identifier_len) != 0)) {
948 		wpa_printf(MSG_DEBUG, "RADIUS DAS: NAS-Identifier mismatch");
949 		return 1;
950 	}
951 
952 	if (attr->nas_ip_addr &&
953 	    (hapd->conf->own_ip_addr.af != AF_INET ||
954 	     os_memcmp(&hapd->conf->own_ip_addr.u.v4, attr->nas_ip_addr, 4) !=
955 	     0)) {
956 		wpa_printf(MSG_DEBUG, "RADIUS DAS: NAS-IP-Address mismatch");
957 		return 1;
958 	}
959 
960 #ifdef CONFIG_IPV6
961 	if (attr->nas_ipv6_addr &&
962 	    (hapd->conf->own_ip_addr.af != AF_INET6 ||
963 	     os_memcmp(&hapd->conf->own_ip_addr.u.v6, attr->nas_ipv6_addr, 16)
964 	     != 0)) {
965 		wpa_printf(MSG_DEBUG, "RADIUS DAS: NAS-IPv6-Address mismatch");
966 		return 1;
967 	}
968 #endif /* CONFIG_IPV6 */
969 
970 	return 0;
971 }
972 
973 
hostapd_das_find_sta(struct hostapd_data * hapd,struct radius_das_attrs * attr,int * multi)974 static struct sta_info * hostapd_das_find_sta(struct hostapd_data *hapd,
975 					      struct radius_das_attrs *attr,
976 					      int *multi)
977 {
978 	struct sta_info *selected, *sta;
979 	char buf[128];
980 	int num_attr = 0;
981 	int count;
982 
983 	*multi = 0;
984 
985 	for (sta = hapd->sta_list; sta; sta = sta->next)
986 		sta->radius_das_match = 1;
987 
988 	if (attr->sta_addr) {
989 		num_attr++;
990 		sta = ap_get_sta(hapd, attr->sta_addr);
991 		if (!sta) {
992 			wpa_printf(MSG_DEBUG,
993 				   "RADIUS DAS: No Calling-Station-Id match");
994 			return NULL;
995 		}
996 
997 		selected = sta;
998 		for (sta = hapd->sta_list; sta; sta = sta->next) {
999 			if (sta != selected)
1000 				sta->radius_das_match = 0;
1001 		}
1002 		wpa_printf(MSG_DEBUG, "RADIUS DAS: Calling-Station-Id match");
1003 	}
1004 
1005 	if (attr->acct_session_id) {
1006 		num_attr++;
1007 		if (attr->acct_session_id_len != 16) {
1008 			wpa_printf(MSG_DEBUG,
1009 				   "RADIUS DAS: Acct-Session-Id cannot match");
1010 			return NULL;
1011 		}
1012 		count = 0;
1013 
1014 		for (sta = hapd->sta_list; sta; sta = sta->next) {
1015 			if (!sta->radius_das_match)
1016 				continue;
1017 			os_snprintf(buf, sizeof(buf), "%016llX",
1018 				    (unsigned long long) sta->acct_session_id);
1019 			if (os_memcmp(attr->acct_session_id, buf, 16) != 0)
1020 				sta->radius_das_match = 0;
1021 			else
1022 				count++;
1023 		}
1024 
1025 		if (count == 0) {
1026 			wpa_printf(MSG_DEBUG,
1027 				   "RADIUS DAS: No matches remaining after Acct-Session-Id check");
1028 			return NULL;
1029 		}
1030 		wpa_printf(MSG_DEBUG, "RADIUS DAS: Acct-Session-Id match");
1031 	}
1032 
1033 	if (attr->acct_multi_session_id) {
1034 		num_attr++;
1035 		if (attr->acct_multi_session_id_len != 16) {
1036 			wpa_printf(MSG_DEBUG,
1037 				   "RADIUS DAS: Acct-Multi-Session-Id cannot match");
1038 			return NULL;
1039 		}
1040 		count = 0;
1041 
1042 		for (sta = hapd->sta_list; sta; sta = sta->next) {
1043 			if (!sta->radius_das_match)
1044 				continue;
1045 			if (!sta->eapol_sm ||
1046 			    !sta->eapol_sm->acct_multi_session_id) {
1047 				sta->radius_das_match = 0;
1048 				continue;
1049 			}
1050 			os_snprintf(buf, sizeof(buf), "%016llX",
1051 				    (unsigned long long)
1052 				    sta->eapol_sm->acct_multi_session_id);
1053 			if (os_memcmp(attr->acct_multi_session_id, buf, 16) !=
1054 			    0)
1055 				sta->radius_das_match = 0;
1056 			else
1057 				count++;
1058 		}
1059 
1060 		if (count == 0) {
1061 			wpa_printf(MSG_DEBUG,
1062 				   "RADIUS DAS: No matches remaining after Acct-Multi-Session-Id check");
1063 			return NULL;
1064 		}
1065 		wpa_printf(MSG_DEBUG,
1066 			   "RADIUS DAS: Acct-Multi-Session-Id match");
1067 	}
1068 
1069 	if (attr->cui) {
1070 		num_attr++;
1071 		count = 0;
1072 
1073 		for (sta = hapd->sta_list; sta; sta = sta->next) {
1074 			struct wpabuf *cui;
1075 
1076 			if (!sta->radius_das_match)
1077 				continue;
1078 			cui = ieee802_1x_get_radius_cui(sta->eapol_sm);
1079 			if (!cui || wpabuf_len(cui) != attr->cui_len ||
1080 			    os_memcmp(wpabuf_head(cui), attr->cui,
1081 				      attr->cui_len) != 0)
1082 				sta->radius_das_match = 0;
1083 			else
1084 				count++;
1085 		}
1086 
1087 		if (count == 0) {
1088 			wpa_printf(MSG_DEBUG,
1089 				   "RADIUS DAS: No matches remaining after Chargeable-User-Identity check");
1090 			return NULL;
1091 		}
1092 		wpa_printf(MSG_DEBUG,
1093 			   "RADIUS DAS: Chargeable-User-Identity match");
1094 	}
1095 
1096 	if (attr->user_name) {
1097 		num_attr++;
1098 		count = 0;
1099 
1100 		for (sta = hapd->sta_list; sta; sta = sta->next) {
1101 			u8 *identity;
1102 			size_t identity_len;
1103 
1104 			if (!sta->radius_das_match)
1105 				continue;
1106 			identity = ieee802_1x_get_identity(sta->eapol_sm,
1107 							   &identity_len);
1108 			if (!identity ||
1109 			    identity_len != attr->user_name_len ||
1110 			    os_memcmp(identity, attr->user_name, identity_len)
1111 			    != 0)
1112 				sta->radius_das_match = 0;
1113 			else
1114 				count++;
1115 		}
1116 
1117 		if (count == 0) {
1118 			wpa_printf(MSG_DEBUG,
1119 				   "RADIUS DAS: No matches remaining after User-Name check");
1120 			return NULL;
1121 		}
1122 		wpa_printf(MSG_DEBUG,
1123 			   "RADIUS DAS: User-Name match");
1124 	}
1125 
1126 	if (num_attr == 0) {
1127 		/*
1128 		 * In theory, we could match all current associations, but it
1129 		 * seems safer to just reject requests that do not include any
1130 		 * session identification attributes.
1131 		 */
1132 		wpa_printf(MSG_DEBUG,
1133 			   "RADIUS DAS: No session identification attributes included");
1134 		return NULL;
1135 	}
1136 
1137 	selected = NULL;
1138 	for (sta = hapd->sta_list; sta; sta = sta->next) {
1139 		if (sta->radius_das_match) {
1140 			if (selected) {
1141 				*multi = 1;
1142 				return NULL;
1143 			}
1144 			selected = sta;
1145 		}
1146 	}
1147 
1148 	return selected;
1149 }
1150 
1151 
hostapd_das_disconnect_pmksa(struct hostapd_data * hapd,struct radius_das_attrs * attr)1152 static int hostapd_das_disconnect_pmksa(struct hostapd_data *hapd,
1153 					struct radius_das_attrs *attr)
1154 {
1155 	if (!hapd->wpa_auth)
1156 		return -1;
1157 	return wpa_auth_radius_das_disconnect_pmksa(hapd->wpa_auth, attr);
1158 }
1159 
1160 
1161 static enum radius_das_res
hostapd_das_disconnect(void * ctx,struct radius_das_attrs * attr)1162 hostapd_das_disconnect(void *ctx, struct radius_das_attrs *attr)
1163 {
1164 	struct hostapd_data *hapd = ctx;
1165 	struct sta_info *sta;
1166 	int multi;
1167 
1168 	if (hostapd_das_nas_mismatch(hapd, attr))
1169 		return RADIUS_DAS_NAS_MISMATCH;
1170 
1171 	sta = hostapd_das_find_sta(hapd, attr, &multi);
1172 	if (sta == NULL) {
1173 		if (multi) {
1174 			wpa_printf(MSG_DEBUG,
1175 				   "RADIUS DAS: Multiple sessions match - not supported");
1176 			return RADIUS_DAS_MULTI_SESSION_MATCH;
1177 		}
1178 		if (hostapd_das_disconnect_pmksa(hapd, attr) == 0) {
1179 			wpa_printf(MSG_DEBUG,
1180 				   "RADIUS DAS: PMKSA cache entry matched");
1181 			return RADIUS_DAS_SUCCESS;
1182 		}
1183 		wpa_printf(MSG_DEBUG, "RADIUS DAS: No matching session found");
1184 		return RADIUS_DAS_SESSION_NOT_FOUND;
1185 	}
1186 
1187 	wpa_printf(MSG_DEBUG, "RADIUS DAS: Found a matching session " MACSTR
1188 		   " - disconnecting", MAC2STR(sta->addr));
1189 	wpa_auth_pmksa_remove(hapd->wpa_auth, sta->addr);
1190 
1191 	hostapd_drv_sta_deauth(hapd, sta->addr,
1192 			       WLAN_REASON_PREV_AUTH_NOT_VALID);
1193 	ap_sta_deauthenticate(hapd, sta, WLAN_REASON_PREV_AUTH_NOT_VALID);
1194 
1195 	return RADIUS_DAS_SUCCESS;
1196 }
1197 
1198 
1199 #ifdef CONFIG_HS20
1200 static enum radius_das_res
hostapd_das_coa(void * ctx,struct radius_das_attrs * attr)1201 hostapd_das_coa(void *ctx, struct radius_das_attrs *attr)
1202 {
1203 	struct hostapd_data *hapd = ctx;
1204 	struct sta_info *sta;
1205 	int multi;
1206 
1207 	if (hostapd_das_nas_mismatch(hapd, attr))
1208 		return RADIUS_DAS_NAS_MISMATCH;
1209 
1210 	sta = hostapd_das_find_sta(hapd, attr, &multi);
1211 	if (!sta) {
1212 		if (multi) {
1213 			wpa_printf(MSG_DEBUG,
1214 				   "RADIUS DAS: Multiple sessions match - not supported");
1215 			return RADIUS_DAS_MULTI_SESSION_MATCH;
1216 		}
1217 		wpa_printf(MSG_DEBUG, "RADIUS DAS: No matching session found");
1218 		return RADIUS_DAS_SESSION_NOT_FOUND;
1219 	}
1220 
1221 	wpa_printf(MSG_DEBUG, "RADIUS DAS: Found a matching session " MACSTR
1222 		   " - CoA", MAC2STR(sta->addr));
1223 
1224 	if (attr->hs20_t_c_filtering) {
1225 		if (attr->hs20_t_c_filtering[0] & BIT(0)) {
1226 			wpa_printf(MSG_DEBUG,
1227 				   "HS 2.0: Unexpected Terms and Conditions filtering required in CoA-Request");
1228 			return RADIUS_DAS_COA_FAILED;
1229 		}
1230 
1231 		hs20_t_c_filtering(hapd, sta, 0);
1232 	}
1233 
1234 	return RADIUS_DAS_SUCCESS;
1235 }
1236 #else /* CONFIG_HS20 */
1237 #define hostapd_das_coa NULL
1238 #endif /* CONFIG_HS20 */
1239 
1240 
1241 #ifdef CONFIG_SQLITE
1242 
db_table_exists(sqlite3 * db,const char * name)1243 static int db_table_exists(sqlite3 *db, const char *name)
1244 {
1245 	char cmd[128];
1246 
1247 	os_snprintf(cmd, sizeof(cmd), "SELECT 1 FROM %s;", name);
1248 	return sqlite3_exec(db, cmd, NULL, NULL, NULL) == SQLITE_OK;
1249 }
1250 
1251 
db_table_create_radius_attributes(sqlite3 * db)1252 static int db_table_create_radius_attributes(sqlite3 *db)
1253 {
1254 	char *err = NULL;
1255 	const char *sql =
1256 		"CREATE TABLE radius_attributes("
1257 		" id INTEGER PRIMARY KEY,"
1258 		" sta TEXT,"
1259 		" reqtype TEXT,"
1260 		" attr TEXT"
1261 		");"
1262 		"CREATE INDEX idx_sta_reqtype ON radius_attributes(sta,reqtype);";
1263 
1264 	wpa_printf(MSG_DEBUG,
1265 		   "Adding database table for RADIUS attribute information");
1266 	if (sqlite3_exec(db, sql, NULL, NULL, &err) != SQLITE_OK) {
1267 		wpa_printf(MSG_ERROR, "SQLite error: %s", err);
1268 		sqlite3_free(err);
1269 		return -1;
1270 	}
1271 
1272 	return 0;
1273 }
1274 
1275 #endif /* CONFIG_SQLITE */
1276 
1277 #endif /* CONFIG_NO_RADIUS */
1278 
1279 
hostapd_start_beacon(struct hostapd_data * hapd,bool flush_old_stations)1280 static int hostapd_start_beacon(struct hostapd_data *hapd,
1281 				bool flush_old_stations)
1282 {
1283 	struct hostapd_bss_config *conf = hapd->conf;
1284 
1285 	if (!conf->start_disabled && ieee802_11_set_beacon(hapd) < 0)
1286 		return -1;
1287 
1288 	if (flush_old_stations && !conf->start_disabled &&
1289 	    conf->broadcast_deauth) {
1290 		u8 addr[ETH_ALEN];
1291 
1292 		/* Should any previously associated STA not have noticed that
1293 		 * the AP had stopped and restarted, send one more
1294 		 * deauthentication notification now that the AP is ready to
1295 		 * operate. */
1296 		wpa_dbg(hapd->msg_ctx, MSG_DEBUG,
1297 			"Deauthenticate all stations at BSS start");
1298 		os_memset(addr, 0xff, ETH_ALEN);
1299 		hostapd_drv_sta_deauth(hapd, addr,
1300 				       WLAN_REASON_PREV_AUTH_NOT_VALID);
1301 	}
1302 
1303 	if (hapd->driver && hapd->driver->set_operstate)
1304 		hapd->driver->set_operstate(hapd->drv_priv, 1);
1305 
1306 	return 0;
1307 }
1308 
1309 
1310 #ifndef CONFIG_NO_RADIUS
hostapd_bss_radius_init(struct hostapd_data * hapd)1311 static int hostapd_bss_radius_init(struct hostapd_data *hapd)
1312 {
1313 	struct hostapd_bss_config *conf;
1314 
1315 	if (!hapd)
1316 		return -1;
1317 
1318 	conf = hapd->conf;
1319 
1320 	if (hapd->radius) {
1321 		wpa_printf(MSG_DEBUG,
1322 			   "Skipping RADIUS client init (already done)");
1323 		return 0;
1324 	}
1325 
1326 	hapd->radius = radius_client_init(hapd, conf->radius);
1327 	if (!hapd->radius) {
1328 		wpa_printf(MSG_ERROR,
1329 			   "RADIUS client initialization failed.");
1330 		return -1;
1331 	}
1332 
1333 	if (conf->radius_das_port) {
1334 		struct radius_das_conf das_conf;
1335 
1336 		os_memset(&das_conf, 0, sizeof(das_conf));
1337 		das_conf.port = conf->radius_das_port;
1338 		das_conf.shared_secret = conf->radius_das_shared_secret;
1339 		das_conf.shared_secret_len =
1340 			conf->radius_das_shared_secret_len;
1341 		das_conf.client_addr = &conf->radius_das_client_addr;
1342 		das_conf.time_window = conf->radius_das_time_window;
1343 		das_conf.require_event_timestamp =
1344 			conf->radius_das_require_event_timestamp;
1345 		das_conf.require_message_authenticator =
1346 			conf->radius_das_require_message_authenticator;
1347 		das_conf.ctx = hapd;
1348 		das_conf.disconnect = hostapd_das_disconnect;
1349 		das_conf.coa = hostapd_das_coa;
1350 		hapd->radius_das = radius_das_init(&das_conf);
1351 		if (!hapd->radius_das) {
1352 			wpa_printf(MSG_ERROR,
1353 				   "RADIUS DAS initialization failed.");
1354 			return -1;
1355 		}
1356 	}
1357 
1358 	return 0;
1359 }
1360 #endif /* CONFIG_NO_RADIUS */
1361 
1362 
1363 /**
1364  * hostapd_setup_bss - Per-BSS setup (initialization)
1365  * @hapd: Pointer to BSS data
1366  * @first: Whether this BSS is the first BSS of an interface; -1 = not first,
1367  *	but interface may exist
1368  * @start_beacon: Whether Beacon frame template should be configured and
1369  *	transmission of Beaconf rames started at this time. This is used when
1370  *	MBSSID element is enabled where the information regarding all BSSes
1371  *	should be retrieved before configuring the Beacon frame template. The
1372  *	calling functions are responsible for configuring the Beacon frame
1373  *	explicitly if this is set to false.
1374  *
1375  * This function is used to initialize all per-BSS data structures and
1376  * resources. This gets called in a loop for each BSS when an interface is
1377  * initialized. Most of the modules that are initialized here will be
1378  * deinitialized in hostapd_cleanup().
1379  */
hostapd_setup_bss(struct hostapd_data * hapd,int first,bool start_beacon)1380 static int hostapd_setup_bss(struct hostapd_data *hapd, int first,
1381 			     bool start_beacon)
1382 {
1383 	struct hostapd_bss_config *conf = hapd->conf;
1384 	u8 ssid[SSID_MAX_LEN + 1];
1385 	int ssid_len, set_ssid;
1386 	char force_ifname[IFNAMSIZ];
1387 	u8 if_addr[ETH_ALEN];
1388 	int flush_old_stations = 1;
1389 
1390 	if (!hostapd_mld_is_first_bss(hapd))
1391 		wpa_printf(MSG_DEBUG,
1392 			   "MLD: %s: Setting non-first BSS", __func__);
1393 
1394 	wpa_printf(MSG_DEBUG, "%s(hapd=%p (%s), first=%d)",
1395 		   __func__, hapd, conf->iface, first);
1396 
1397 #ifdef EAP_SERVER_TNC
1398 	if (conf->tnc && tncs_global_init() < 0) {
1399 		wpa_printf(MSG_ERROR, "Failed to initialize TNCS");
1400 		return -1;
1401 	}
1402 #endif /* EAP_SERVER_TNC */
1403 
1404 	if (hapd->started) {
1405 		wpa_printf(MSG_ERROR, "%s: Interface %s was already started",
1406 			   __func__, conf->iface);
1407 		return -1;
1408 	}
1409 	hapd->started = 1;
1410 
1411 	if (!first || first == -1) {
1412 		u8 *addr = hapd->own_addr;
1413 
1414 		if (!is_zero_ether_addr(conf->bssid)) {
1415 			/* Allocate the configured BSSID. */
1416 			os_memcpy(hapd->own_addr, conf->bssid, ETH_ALEN);
1417 
1418 			if (hostapd_mac_comp(hapd->own_addr,
1419 					     hapd->iface->bss[0]->own_addr) ==
1420 			    0) {
1421 				wpa_printf(MSG_ERROR, "BSS '%s' may not have "
1422 					   "BSSID set to the MAC address of "
1423 					   "the radio", conf->iface);
1424 				return -1;
1425 			}
1426 		} else if (hapd->iconf->use_driver_iface_addr) {
1427 			addr = NULL;
1428 		} else {
1429 			/* Allocate the next available BSSID. */
1430 			do {
1431 				inc_byte_array(hapd->own_addr, ETH_ALEN);
1432 			} while (mac_in_conf(hapd->iconf, hapd->own_addr));
1433 		}
1434 
1435 #ifdef CONFIG_IEEE80211BE
1436 		if (conf->mld_ap) {
1437 			struct hostapd_data *h_hapd;
1438 
1439 			h_hapd = hostapd_mld_get_first_bss(hapd);
1440 			if (h_hapd) {
1441 				hapd->drv_priv = h_hapd->drv_priv;
1442 				hapd->interface_added = h_hapd->interface_added;
1443 				hostapd_mld_add_link(hapd);
1444 				wpa_printf(MSG_DEBUG,
1445 					   "Setup of non first link (%d) BSS of MLD %s",
1446 					   hapd->mld_link_id, hapd->conf->iface);
1447 				goto setup_mld;
1448 			}
1449 		}
1450 #endif /* CONFIG_IEEE80211BE */
1451 
1452 		hapd->interface_added = 1;
1453 		if (hostapd_if_add(hapd->iface->bss[0], WPA_IF_AP_BSS,
1454 				   conf->iface, addr, hapd,
1455 				   &hapd->drv_priv, force_ifname, if_addr,
1456 				   conf->bridge[0] ? conf->bridge : NULL,
1457 				   first == -1)) {
1458 			wpa_printf(MSG_ERROR, "Failed to add BSS (BSSID="
1459 				   MACSTR ")", MAC2STR(hapd->own_addr));
1460 			hapd->interface_added = 0;
1461 			return -1;
1462 		}
1463 
1464 		if (!addr)
1465 			os_memcpy(hapd->own_addr, if_addr, ETH_ALEN);
1466 
1467 #ifdef CONFIG_IEEE80211BE
1468 		if (hapd->conf->mld_ap) {
1469 			wpa_printf(MSG_DEBUG,
1470 				   "Setup of first link (%d) BSS of MLD %s",
1471 				   hapd->mld_link_id, hapd->conf->iface);
1472 			os_memcpy(hapd->mld->mld_addr, hapd->own_addr,
1473 				  ETH_ALEN);
1474 			hostapd_mld_add_link(hapd);
1475 		}
1476 #endif /* CONFIG_IEEE80211BE */
1477 	}
1478 
1479 #ifdef CONFIG_IEEE80211BE
1480 setup_mld:
1481 	if (hapd->conf->mld_ap && !first) {
1482 		wpa_printf(MSG_DEBUG,
1483 			   "MLD: Set link_id=%u, mld_addr=" MACSTR
1484 			   ", own_addr=" MACSTR,
1485 			   hapd->mld_link_id, MAC2STR(hapd->mld->mld_addr),
1486 			   MAC2STR(hapd->own_addr));
1487 
1488 		if (hostapd_drv_link_add(hapd, hapd->mld_link_id,
1489 					 hapd->own_addr))
1490 			return -1;
1491 	}
1492 #endif /* CONFIG_IEEE80211BE */
1493 
1494 	if (conf->wmm_enabled < 0)
1495 		conf->wmm_enabled = hapd->iconf->ieee80211n |
1496 			hapd->iconf->ieee80211ax;
1497 
1498 #ifdef CONFIG_IEEE80211R_AP
1499 	if (is_zero_ether_addr(conf->r1_key_holder))
1500 		os_memcpy(conf->r1_key_holder, hapd->own_addr, ETH_ALEN);
1501 #endif /* CONFIG_IEEE80211R_AP */
1502 
1503 #ifdef CONFIG_MESH
1504 	if ((hapd->conf->mesh & MESH_ENABLED) && hapd->iface->mconf == NULL)
1505 		flush_old_stations = 0;
1506 #endif /* CONFIG_MESH */
1507 
1508 	if (flush_old_stations)
1509 		hostapd_flush(hapd);
1510 	hostapd_set_privacy(hapd, 0);
1511 
1512 #ifdef CONFIG_WEP
1513 	if (!hostapd_drv_nl80211(hapd))
1514 		hostapd_broadcast_wep_clear(hapd);
1515 	if (hostapd_setup_encryption(conf->iface, hapd))
1516 		return -1;
1517 #endif /* CONFIG_WEP */
1518 
1519 	/*
1520 	 * Fetch the SSID from the system and use it or,
1521 	 * if one was specified in the config file, verify they
1522 	 * match.
1523 	 */
1524 	ssid_len = hostapd_get_ssid(hapd, ssid, sizeof(ssid));
1525 	if (ssid_len < 0) {
1526 		wpa_printf(MSG_ERROR, "Could not read SSID from system");
1527 		return -1;
1528 	}
1529 	if (conf->ssid.ssid_set) {
1530 		/*
1531 		 * If SSID is specified in the config file and it differs
1532 		 * from what is being used then force installation of the
1533 		 * new SSID.
1534 		 */
1535 		set_ssid = (conf->ssid.ssid_len != (size_t) ssid_len ||
1536 			    os_memcmp(conf->ssid.ssid, ssid, ssid_len) != 0);
1537 	} else {
1538 		/*
1539 		 * No SSID in the config file; just use the one we got
1540 		 * from the system.
1541 		 */
1542 		set_ssid = 0;
1543 		conf->ssid.ssid_len = ssid_len;
1544 		os_memcpy(conf->ssid.ssid, ssid, conf->ssid.ssid_len);
1545 	}
1546 
1547 	/*
1548 	 * Short SSID calculation is identical to FCS and it is defined in
1549 	 * IEEE P802.11-REVmd/D3.0, 9.4.2.170.3 (Calculating the Short-SSID).
1550 	 */
1551 	conf->ssid.short_ssid = ieee80211_crc32(conf->ssid.ssid,
1552 						conf->ssid.ssid_len);
1553 
1554 	if (!hostapd_drv_none(hapd)) {
1555 		wpa_printf(MSG_DEBUG, "Using interface %s with hwaddr " MACSTR
1556 			   " and ssid \"%s\"",
1557 			   conf->iface, MAC2STR(hapd->own_addr),
1558 			   wpa_ssid_txt(conf->ssid.ssid, conf->ssid.ssid_len));
1559 	}
1560 
1561 	if (hostapd_setup_wpa_psk(conf)) {
1562 		wpa_printf(MSG_ERROR, "WPA-PSK setup failed.");
1563 		return -1;
1564 	}
1565 
1566 	/* Set SSID for the kernel driver (to be used in beacon and probe
1567 	 * response frames) */
1568 	if (set_ssid && hostapd_set_ssid(hapd, conf->ssid.ssid,
1569 					 conf->ssid.ssid_len)) {
1570 		wpa_printf(MSG_ERROR, "Could not set SSID for kernel driver");
1571 		return -1;
1572 	}
1573 
1574 	if (wpa_debug_level <= MSG_MSGDUMP)
1575 		conf->radius->msg_dumps = 1;
1576 #ifndef CONFIG_NO_RADIUS
1577 
1578 #ifdef CONFIG_SQLITE
1579 	if (conf->radius_req_attr_sqlite) {
1580 		if (sqlite3_open(conf->radius_req_attr_sqlite,
1581 				 &hapd->rad_attr_db)) {
1582 			wpa_printf(MSG_ERROR, "Could not open SQLite file '%s'",
1583 				   conf->radius_req_attr_sqlite);
1584 			return -1;
1585 		}
1586 
1587 		wpa_printf(MSG_DEBUG, "Opening RADIUS attribute database: %s",
1588 			   conf->radius_req_attr_sqlite);
1589 		if (!db_table_exists(hapd->rad_attr_db, "radius_attributes") &&
1590 		    db_table_create_radius_attributes(hapd->rad_attr_db) < 0)
1591 			return -1;
1592 	}
1593 #endif /* CONFIG_SQLITE */
1594 
1595 	if (hostapd_mld_is_first_bss(hapd)) {
1596 		if (hostapd_bss_radius_init(hapd))
1597 			return -1;
1598 	} else {
1599 #ifdef CONFIG_IEEE80211BE
1600 		struct hostapd_data *f_bss;
1601 
1602 		f_bss = hostapd_mld_get_first_bss(hapd);
1603 		if (!f_bss)
1604 			return -1;
1605 
1606 		if (!f_bss->radius) {
1607 			wpa_printf(MSG_DEBUG,
1608 				   "MLD: First BSS RADIUS client does not exist. Init on its behalf");
1609 
1610 			if (hostapd_bss_radius_init(f_bss))
1611 				return -1;
1612 		}
1613 
1614 		wpa_printf(MSG_DEBUG,
1615 			   "MLD: Using RADIUS client of the first BSS");
1616 		hapd->radius = f_bss->radius;
1617 		hapd->radius_das = f_bss->radius_das;
1618 #endif /* CONFIG_IEEE80211BE */
1619 	}
1620 #endif /* CONFIG_NO_RADIUS */
1621 
1622 	if (hostapd_acl_init(hapd)) {
1623 		wpa_printf(MSG_ERROR, "ACL initialization failed.");
1624 		return -1;
1625 	}
1626 	if (hostapd_init_wps(hapd, conf))
1627 		return -1;
1628 
1629 #ifdef CONFIG_DPP
1630 	hapd->gas = gas_query_ap_init(hapd, hapd->msg_ctx);
1631 	if (!hapd->gas)
1632 		return -1;
1633 	if (hostapd_dpp_init(hapd))
1634 		return -1;
1635 #endif /* CONFIG_DPP */
1636 
1637 #ifdef CONFIG_NAN_USD
1638 	if (hostapd_nan_usd_init(hapd) < 0)
1639 		return -1;
1640 #endif /* CONFIG_NAN_USD */
1641 
1642 	if (authsrv_init(hapd) < 0)
1643 		return -1;
1644 
1645 	if (ieee802_1x_init(hapd)) {
1646 		wpa_printf(MSG_ERROR, "IEEE 802.1X initialization failed.");
1647 		return -1;
1648 	}
1649 
1650 	if ((conf->wpa || conf->osen) && hostapd_setup_wpa(hapd))
1651 		return -1;
1652 
1653 	if (accounting_init(hapd)) {
1654 		wpa_printf(MSG_ERROR, "Accounting initialization failed.");
1655 		return -1;
1656 	}
1657 
1658 #ifdef CONFIG_INTERWORKING
1659 	if (gas_serv_init(hapd)) {
1660 		wpa_printf(MSG_ERROR, "GAS server initialization failed");
1661 		return -1;
1662 	}
1663 #endif /* CONFIG_INTERWORKING */
1664 
1665 	if (conf->qos_map_set_len &&
1666 	    hostapd_drv_set_qos_map(hapd, conf->qos_map_set,
1667 				    conf->qos_map_set_len)) {
1668 		wpa_printf(MSG_ERROR, "Failed to initialize QoS Map");
1669 		return -1;
1670 	}
1671 
1672 	if (conf->bss_load_update_period && bss_load_update_init(hapd)) {
1673 		wpa_printf(MSG_ERROR, "BSS Load initialization failed");
1674 		return -1;
1675 	}
1676 
1677 	if (conf->bridge[0]) {
1678 		/* Set explicitly configured bridge parameters that might have
1679 		 * been lost if the interface has been removed out of the
1680 		 * bridge. */
1681 
1682 		/* multicast to unicast on bridge ports */
1683 		if (conf->bridge_multicast_to_unicast)
1684 			hostapd_drv_br_port_set_attr(
1685 				hapd, DRV_BR_PORT_ATTR_MCAST2UCAST, 1);
1686 
1687 		/* hairpin mode */
1688 		if (conf->bridge_hairpin)
1689 			hostapd_drv_br_port_set_attr(
1690 				hapd, DRV_BR_PORT_ATTR_HAIRPIN_MODE, 1);
1691 	}
1692 
1693 	if (conf->proxy_arp) {
1694 		if (x_snoop_init(hapd)) {
1695 			wpa_printf(MSG_ERROR,
1696 				   "Generic snooping infrastructure initialization failed");
1697 			return -1;
1698 		}
1699 
1700 		if (dhcp_snoop_init(hapd)) {
1701 			wpa_printf(MSG_ERROR,
1702 				   "DHCP snooping initialization failed");
1703 			return -1;
1704 		}
1705 
1706 		if (ndisc_snoop_init(hapd)) {
1707 			wpa_printf(MSG_ERROR,
1708 				   "Neighbor Discovery snooping initialization failed");
1709 			return -1;
1710 		}
1711 	}
1712 
1713 	if (!hostapd_drv_none(hapd) && vlan_init(hapd)) {
1714 		wpa_printf(MSG_ERROR, "VLAN initialization failed.");
1715 		return -1;
1716 	}
1717 
1718 	if (start_beacon && hostapd_start_beacon(hapd, flush_old_stations) < 0)
1719 		return -1;
1720 
1721 	if (hapd->wpa_auth && wpa_init_keys(hapd->wpa_auth) < 0)
1722 		return -1;
1723 
1724 	return 0;
1725 }
1726 
1727 
hostapd_tx_queue_params(struct hostapd_iface * iface)1728 static void hostapd_tx_queue_params(struct hostapd_iface *iface)
1729 {
1730 	struct hostapd_data *hapd = iface->bss[0];
1731 	int i;
1732 	struct hostapd_tx_queue_params *p;
1733 
1734 #ifdef CONFIG_MESH
1735 	if ((hapd->conf->mesh & MESH_ENABLED) && iface->mconf == NULL)
1736 		return;
1737 #endif /* CONFIG_MESH */
1738 
1739 	for (i = 0; i < NUM_TX_QUEUES; i++) {
1740 		p = &iface->conf->tx_queue[i];
1741 
1742 		if (hostapd_set_tx_queue_params(hapd, i, p->aifs, p->cwmin,
1743 						p->cwmax, p->burst)) {
1744 			wpa_printf(MSG_DEBUG, "Failed to set TX queue "
1745 				   "parameters for queue %d.", i);
1746 			/* Continue anyway */
1747 		}
1748 	}
1749 }
1750 
1751 
hostapd_set_acl_list(struct hostapd_data * hapd,struct mac_acl_entry * mac_acl,int n_entries,u8 accept_acl)1752 static int hostapd_set_acl_list(struct hostapd_data *hapd,
1753 				struct mac_acl_entry *mac_acl,
1754 				int n_entries, u8 accept_acl)
1755 {
1756 	struct hostapd_acl_params *acl_params;
1757 	int i, err;
1758 
1759 	acl_params = os_zalloc(sizeof(*acl_params) +
1760 			       (n_entries * sizeof(acl_params->mac_acl[0])));
1761 	if (!acl_params)
1762 		return -ENOMEM;
1763 
1764 	for (i = 0; i < n_entries; i++)
1765 		os_memcpy(acl_params->mac_acl[i].addr, mac_acl[i].addr,
1766 			  ETH_ALEN);
1767 
1768 	acl_params->acl_policy = accept_acl;
1769 	acl_params->num_mac_acl = n_entries;
1770 
1771 	err = hostapd_drv_set_acl(hapd, acl_params);
1772 
1773 	os_free(acl_params);
1774 
1775 	return err;
1776 }
1777 
1778 
hostapd_set_acl(struct hostapd_data * hapd)1779 int hostapd_set_acl(struct hostapd_data *hapd)
1780 {
1781 	struct hostapd_config *conf = hapd->iconf;
1782 	int err = 0;
1783 	u8 accept_acl;
1784 
1785 	if (hapd->iface->drv_max_acl_mac_addrs == 0)
1786 		return 0;
1787 
1788 	if (conf->bss[0]->macaddr_acl == DENY_UNLESS_ACCEPTED) {
1789 		accept_acl = 1;
1790 		err = hostapd_set_acl_list(hapd, conf->bss[0]->accept_mac,
1791 					   conf->bss[0]->num_accept_mac,
1792 					   accept_acl);
1793 		if (err) {
1794 			wpa_printf(MSG_DEBUG, "Failed to set accept acl");
1795 			return -1;
1796 		}
1797 	} else if (conf->bss[0]->macaddr_acl == ACCEPT_UNLESS_DENIED) {
1798 		accept_acl = 0;
1799 		err = hostapd_set_acl_list(hapd, conf->bss[0]->deny_mac,
1800 					   conf->bss[0]->num_deny_mac,
1801 					   accept_acl);
1802 		if (err) {
1803 			wpa_printf(MSG_DEBUG, "Failed to set deny acl");
1804 			return -1;
1805 		}
1806 	}
1807 	return err;
1808 }
1809 
1810 
start_ctrl_iface_bss(struct hostapd_data * hapd)1811 static int start_ctrl_iface_bss(struct hostapd_data *hapd)
1812 {
1813 	if (!hapd->iface->interfaces ||
1814 	    !hapd->iface->interfaces->ctrl_iface_init)
1815 		return 0;
1816 
1817 	if (hapd->iface->interfaces->ctrl_iface_init(hapd)) {
1818 		wpa_printf(MSG_ERROR,
1819 			   "Failed to setup control interface for %s",
1820 			   hapd->conf->iface);
1821 		return -1;
1822 	}
1823 
1824 	return 0;
1825 }
1826 
1827 
start_ctrl_iface(struct hostapd_iface * iface)1828 static int start_ctrl_iface(struct hostapd_iface *iface)
1829 {
1830 	size_t i;
1831 
1832 	if (!iface->interfaces || !iface->interfaces->ctrl_iface_init)
1833 		return 0;
1834 
1835 	for (i = 0; i < iface->num_bss; i++) {
1836 		struct hostapd_data *hapd = iface->bss[i];
1837 		if (iface->interfaces->ctrl_iface_init(hapd)) {
1838 			wpa_printf(MSG_ERROR,
1839 				   "Failed to setup control interface for %s",
1840 				   hapd->conf->iface);
1841 			return -1;
1842 		}
1843 	}
1844 
1845 	return 0;
1846 }
1847 
1848 
1849 /* When NO_IR flag is set and AP is stopped, clean up BSS parameters without
1850  * deinitializing the driver and the control interfaces. A subsequent
1851  * REG_CHANGE event can bring the AP back up.
1852  */
hostapd_no_ir_cleanup(struct hostapd_data * bss)1853 static void hostapd_no_ir_cleanup(struct hostapd_data *bss)
1854 {
1855 	hostapd_bss_deinit_no_free(bss);
1856 	hostapd_bss_link_deinit(bss);
1857 	hostapd_free_hapd_data(bss);
1858 	hostapd_cleanup_iface_partial(bss->iface);
1859 }
1860 
1861 
hostapd_no_ir_channel_list_updated(struct hostapd_iface * iface,void * ctx)1862 static int hostapd_no_ir_channel_list_updated(struct hostapd_iface *iface,
1863 					      void *ctx)
1864 {
1865 	bool all_no_ir, is_6ghz;
1866 	int i, j;
1867 	struct hostapd_hw_modes *mode = NULL;
1868 
1869 	if (hostapd_get_hw_features(iface))
1870 		return 0;
1871 
1872 	all_no_ir = true;
1873 	is_6ghz = false;
1874 
1875 	for (i = 0; i < iface->num_hw_features; i++) {
1876 		mode = &iface->hw_features[i];
1877 
1878 		if (mode->mode == iface->conf->hw_mode) {
1879 			if (iface->freq > 0 &&
1880 			    !hw_mode_get_channel(mode, iface->freq, NULL)) {
1881 				mode = NULL;
1882 				continue;
1883 			}
1884 
1885 			for (j = 0; j < mode->num_channels; j++) {
1886 				if (!(mode->channels[j].flag &
1887 				      HOSTAPD_CHAN_NO_IR))
1888 					all_no_ir = false;
1889 
1890 				if (is_6ghz_freq(mode->channels[j].freq))
1891 					is_6ghz = true;
1892 			}
1893 			break;
1894 		}
1895 	}
1896 
1897 	if (!mode || !is_6ghz)
1898 		return 0;
1899 	iface->current_mode = mode;
1900 
1901 	if (iface->state == HAPD_IFACE_ENABLED) {
1902 		if (!all_no_ir) {
1903 			struct hostapd_channel_data *chan;
1904 
1905 			chan = hw_get_channel_freq(iface->current_mode->mode,
1906 						   iface->freq, NULL,
1907 						   iface->hw_features,
1908 						   iface->num_hw_features);
1909 
1910 			if (!chan) {
1911 				wpa_printf(MSG_ERROR,
1912 					   "NO_IR: Could not derive chan from freq");
1913 				return 0;
1914 			}
1915 
1916 			if (!(chan->flag & HOSTAPD_CHAN_NO_IR))
1917 				return 0;
1918 			wpa_printf(MSG_DEBUG,
1919 				   "NO_IR: The current channel has NO_IR flag now, stop AP.");
1920 		} else {
1921 			wpa_printf(MSG_DEBUG,
1922 				   "NO_IR: All chan in new chanlist are NO_IR, stop AP.");
1923 		}
1924 
1925 		hostapd_set_state(iface, HAPD_IFACE_NO_IR);
1926 		iface->is_no_ir = true;
1927 		hostapd_drv_stop_ap(iface->bss[0]);
1928 		hostapd_no_ir_cleanup(iface->bss[0]);
1929 		wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_NO_IR);
1930 	} else if (iface->state == HAPD_IFACE_NO_IR) {
1931 		if (all_no_ir) {
1932 			wpa_printf(MSG_DEBUG,
1933 				   "NO_IR: AP in NO_IR and all chan in the new chanlist are NO_IR. Ignore");
1934 			return 0;
1935 		}
1936 
1937 		if (!iface->conf->acs) {
1938 			struct hostapd_channel_data *chan;
1939 
1940 			chan = hw_get_channel_freq(iface->current_mode->mode,
1941 						   iface->freq, NULL,
1942 						   iface->hw_features,
1943 						   iface->num_hw_features);
1944 			if (!chan) {
1945 				wpa_printf(MSG_ERROR,
1946 					   "NO_IR: Could not derive chan from freq");
1947 				return 0;
1948 			}
1949 
1950 			/* If the last operating channel is NO_IR, trigger ACS.
1951 			 */
1952 			if (chan->flag & HOSTAPD_CHAN_NO_IR) {
1953 				iface->freq = 0;
1954 				iface->conf->channel = 0;
1955 				if (acs_init(iface) != HOSTAPD_CHAN_ACS)
1956 					wpa_printf(MSG_ERROR,
1957 						   "NO_IR: Could not start ACS");
1958 				return 0;
1959 			}
1960 		}
1961 
1962 		setup_interface2(iface);
1963 	}
1964 
1965 	return 0;
1966 }
1967 
1968 
channel_list_update_timeout(void * eloop_ctx,void * timeout_ctx)1969 static void channel_list_update_timeout(void *eloop_ctx, void *timeout_ctx)
1970 {
1971 	struct hostapd_iface *iface = eloop_ctx;
1972 
1973 	if (!iface->wait_channel_update) {
1974 		wpa_printf(MSG_INFO, "Channel list update timeout, but interface was not waiting for it");
1975 		return;
1976 	}
1977 
1978 	/*
1979 	 * It is possible that the existing channel list is acceptable, so try
1980 	 * to proceed.
1981 	 */
1982 	wpa_printf(MSG_DEBUG, "Channel list update timeout - try to continue anyway");
1983 	setup_interface2(iface);
1984 }
1985 
1986 
hostapd_channel_list_updated(struct hostapd_iface * iface,int initiator)1987 void hostapd_channel_list_updated(struct hostapd_iface *iface, int initiator)
1988 {
1989 	if (initiator == REGDOM_SET_BY_DRIVER) {
1990 		hostapd_for_each_interface(iface->interfaces,
1991 					   hostapd_no_ir_channel_list_updated,
1992 					   NULL);
1993 		return;
1994 	}
1995 
1996 	if (!iface->wait_channel_update || initiator != REGDOM_SET_BY_USER)
1997 		return;
1998 
1999 	wpa_printf(MSG_DEBUG, "Channel list updated - continue setup");
2000 	eloop_cancel_timeout(channel_list_update_timeout, iface, NULL);
2001 	setup_interface2(iface);
2002 }
2003 
2004 
setup_interface(struct hostapd_iface * iface)2005 static int setup_interface(struct hostapd_iface *iface)
2006 {
2007 	struct hostapd_data *hapd = iface->bss[0];
2008 	size_t i;
2009 
2010 	/*
2011 	 * It is possible that setup_interface() is called after the interface
2012 	 * was disabled etc., in which case driver_ap_teardown is possibly set
2013 	 * to 1. Clear it here so any other key/station deletion, which is not
2014 	 * part of a teardown flow, would also call the relevant driver
2015 	 * callbacks.
2016 	 */
2017 	iface->driver_ap_teardown = 0;
2018 
2019 	if (!iface->phy[0]) {
2020 		const char *phy = hostapd_drv_get_radio_name(hapd);
2021 		if (phy) {
2022 			wpa_printf(MSG_DEBUG, "phy: %s", phy);
2023 			os_strlcpy(iface->phy, phy, sizeof(iface->phy));
2024 		}
2025 	}
2026 
2027 	/*
2028 	 * Make sure that all BSSes get configured with a pointer to the same
2029 	 * driver interface.
2030 	 */
2031 	for (i = 1; i < iface->num_bss; i++) {
2032 		iface->bss[i]->driver = hapd->driver;
2033 		iface->bss[i]->drv_priv = hapd->drv_priv;
2034 	}
2035 
2036 	if (hostapd_validate_bssid_configuration(iface))
2037 		return -1;
2038 
2039 	/*
2040 	 * Initialize control interfaces early to allow external monitoring of
2041 	 * channel setup operations that may take considerable amount of time
2042 	 * especially for DFS cases.
2043 	 */
2044 	if (start_ctrl_iface(iface))
2045 		return -1;
2046 
2047 	if (hapd->iconf->country[0] && hapd->iconf->country[1]) {
2048 		char country[4], previous_country[4];
2049 
2050 		hostapd_set_state(iface, HAPD_IFACE_COUNTRY_UPDATE);
2051 		if (hostapd_get_country(hapd, previous_country) < 0)
2052 			previous_country[0] = '\0';
2053 
2054 		os_memcpy(country, hapd->iconf->country, 3);
2055 		country[3] = '\0';
2056 		if (hostapd_set_country(hapd, country) < 0) {
2057 			wpa_printf(MSG_ERROR, "Failed to set country code");
2058 			return -1;
2059 		}
2060 
2061 		wpa_printf(MSG_DEBUG, "Previous country code %s, new country code %s",
2062 			   previous_country, country);
2063 
2064 		if (os_strncmp(previous_country, country, 2) != 0) {
2065 			wpa_printf(MSG_DEBUG, "Continue interface setup after channel list update");
2066 			iface->wait_channel_update = 1;
2067 			eloop_register_timeout(5, 0,
2068 					       channel_list_update_timeout,
2069 					       iface, NULL);
2070 			return 0;
2071 		}
2072 	}
2073 
2074 	return setup_interface2(iface);
2075 }
2076 
2077 
configured_fixed_chan_to_freq(struct hostapd_iface * iface)2078 static int configured_fixed_chan_to_freq(struct hostapd_iface *iface)
2079 {
2080 	int freq, i, j;
2081 
2082 	if (!iface->conf->channel)
2083 		return 0;
2084 	if (iface->conf->op_class) {
2085 		freq = ieee80211_chan_to_freq(NULL, iface->conf->op_class,
2086 					      iface->conf->channel);
2087 		if (freq < 0) {
2088 			wpa_printf(MSG_INFO,
2089 				   "Could not convert op_class %u channel %u to operating frequency",
2090 				   iface->conf->op_class, iface->conf->channel);
2091 			return -1;
2092 		}
2093 		iface->freq = freq;
2094 		return 0;
2095 	}
2096 
2097 	/* Old configurations using only 2.4/5/60 GHz bands may not specify the
2098 	 * op_class parameter. Select a matching channel from the configured
2099 	 * mode using the channel parameter for these cases.
2100 	 */
2101 	for (j = 0; j < iface->num_hw_features; j++) {
2102 		struct hostapd_hw_modes *mode = &iface->hw_features[j];
2103 
2104 		if (iface->conf->hw_mode != HOSTAPD_MODE_IEEE80211ANY &&
2105 		    iface->conf->hw_mode != mode->mode)
2106 			continue;
2107 		for (i = 0; i < mode->num_channels; i++) {
2108 			struct hostapd_channel_data *chan = &mode->channels[i];
2109 
2110 			if (chan->chan == iface->conf->channel &&
2111 			    !is_6ghz_freq(chan->freq)) {
2112 				iface->freq = chan->freq;
2113 				return 0;
2114 			}
2115 		}
2116 	}
2117 
2118 	wpa_printf(MSG_INFO, "Could not determine operating frequency");
2119 	return -1;
2120 }
2121 
2122 
hostapd_set_6ghz_sec_chan(struct hostapd_iface * iface)2123 static void hostapd_set_6ghz_sec_chan(struct hostapd_iface *iface)
2124 {
2125 	int bw;
2126 
2127 	if (!is_6ghz_op_class(iface->conf->op_class))
2128 		return;
2129 
2130 	bw = op_class_to_bandwidth(iface->conf->op_class);
2131 	/* Assign the secondary channel if absent in config for
2132 	 * bandwidths > 20 MHz */
2133 	if (bw >= 40 && !iface->conf->secondary_channel) {
2134 		if (((iface->conf->channel - 1) / 4) % 2)
2135 			iface->conf->secondary_channel = -1;
2136 		else
2137 			iface->conf->secondary_channel = 1;
2138 	}
2139 }
2140 
2141 
setup_interface2(struct hostapd_iface * iface)2142 static int setup_interface2(struct hostapd_iface *iface)
2143 {
2144 	iface->wait_channel_update = 0;
2145 	iface->is_no_ir = false;
2146 
2147 #ifdef __FreeBSD
2148 	/* XXX hostapd_get_hw_features() is an inline that always returns -1
2149 	 * because MLME will not build under FreeBSD due to its use of
2150 	 * Linux definitions. Normally FreeBSD would uncondionally execute the
2151 	 * "Not all drivers support..." block. Instead we #ifdef out the entire
2152 	 * block of code instead of maintaining the fallacy that
2153 	 * hostapd_get_hw_features() returns anything meaninful.
2154 	 *
2155 	 * Ideally WANT_AP_MLME should be taught about FreeBSD data structures
2156 	 * and defintions. Instead we do this to enable channel selection in
2157 	 * hostapd.conf.
2158 	 */
2159 	iface->freq = iface->conf->channel;
2160 #else
2161 	if (hostapd_get_hw_features(iface)) {
2162 		/* Not all drivers support this yet, so continue without hw
2163 		 * feature data. */
2164 	} else {
2165 		int ret;
2166 
2167 		if (iface->conf->acs && !iface->is_ch_switch_dfs) {
2168 			iface->freq = 0;
2169 			iface->conf->channel = 0;
2170 		}
2171 		iface->is_ch_switch_dfs = false;
2172 
2173 		ret = configured_fixed_chan_to_freq(iface);
2174 		if (ret < 0)
2175 			goto fail;
2176 
2177 		if (iface->conf->op_class) {
2178 			enum oper_chan_width ch_width;
2179 
2180 			ch_width = op_class_to_ch_width(iface->conf->op_class);
2181 			hostapd_set_oper_chwidth(iface->conf, ch_width);
2182 			hostapd_set_6ghz_sec_chan(iface);
2183 		}
2184 
2185 		ret = hostapd_select_hw_mode(iface);
2186 		if (ret < 0) {
2187 			wpa_printf(MSG_ERROR, "Could not select hw_mode and "
2188 				   "channel. (%d)", ret);
2189 			goto fail;
2190 		}
2191 		if (ret == 1) {
2192 			wpa_printf(MSG_DEBUG, "Interface initialization will be completed in a callback (ACS)");
2193 			return 0;
2194 		}
2195 		ret = hostapd_check_edmg_capab(iface);
2196 		if (ret < 0)
2197 			goto fail;
2198 		ret = hostapd_check_he_6ghz_capab(iface);
2199 		if (ret < 0)
2200 			goto fail;
2201 		ret = hostapd_check_ht_capab(iface);
2202 		if (ret < 0)
2203 			goto fail;
2204 		if (ret == 1) {
2205 			wpa_printf(MSG_DEBUG, "Interface initialization will "
2206 				   "be completed in a callback");
2207 			return 0;
2208 		}
2209 
2210 		if (iface->conf->ieee80211h)
2211 			wpa_printf(MSG_DEBUG, "DFS support is enabled");
2212 	}
2213 #endif
2214 	return hostapd_setup_interface_complete(iface, 0);
2215 
2216 fail:
2217 	if (iface->is_no_ir) {
2218 		/* If AP is in NO_IR state, it can be reenabled by the driver
2219 		 * regulatory update and EVENT_CHANNEL_LIST_CHANGED. */
2220 		hostapd_set_state(iface, HAPD_IFACE_NO_IR);
2221 		wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_NO_IR);
2222 		return 0;
2223 	}
2224 
2225 	hostapd_set_state(iface, HAPD_IFACE_DISABLED);
2226 	wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
2227 	if (iface->interfaces && iface->interfaces->terminate_on_error)
2228 		eloop_terminate();
2229 	return -1;
2230 }
2231 
2232 
2233 #ifdef CONFIG_FST
2234 
fst_hostapd_get_bssid_cb(void * ctx)2235 static const u8 * fst_hostapd_get_bssid_cb(void *ctx)
2236 {
2237 	struct hostapd_data *hapd = ctx;
2238 
2239 	return hapd->own_addr;
2240 }
2241 
2242 
fst_hostapd_get_channel_info_cb(void * ctx,enum hostapd_hw_mode * hw_mode,u8 * channel)2243 static void fst_hostapd_get_channel_info_cb(void *ctx,
2244 					    enum hostapd_hw_mode *hw_mode,
2245 					    u8 *channel)
2246 {
2247 	struct hostapd_data *hapd = ctx;
2248 
2249 	*hw_mode = ieee80211_freq_to_chan(hapd->iface->freq, channel);
2250 }
2251 
2252 
fst_hostapd_get_hw_modes_cb(void * ctx,struct hostapd_hw_modes ** modes)2253 static int fst_hostapd_get_hw_modes_cb(void *ctx,
2254 				       struct hostapd_hw_modes **modes)
2255 {
2256 	struct hostapd_data *hapd = ctx;
2257 
2258 	*modes = hapd->iface->hw_features;
2259 	return hapd->iface->num_hw_features;
2260 }
2261 
2262 
fst_hostapd_set_ies_cb(void * ctx,const struct wpabuf * fst_ies)2263 static void fst_hostapd_set_ies_cb(void *ctx, const struct wpabuf *fst_ies)
2264 {
2265 	struct hostapd_data *hapd = ctx;
2266 
2267 	if (hapd->iface->fst_ies != fst_ies) {
2268 		hapd->iface->fst_ies = fst_ies;
2269 		if (ieee802_11_set_beacon(hapd))
2270 			wpa_printf(MSG_WARNING, "FST: Cannot set beacon");
2271 	}
2272 }
2273 
2274 
fst_hostapd_send_action_cb(void * ctx,const u8 * da,struct wpabuf * buf)2275 static int fst_hostapd_send_action_cb(void *ctx, const u8 *da,
2276 				      struct wpabuf *buf)
2277 {
2278 	struct hostapd_data *hapd = ctx;
2279 
2280 	return hostapd_drv_send_action(hapd, hapd->iface->freq, 0, da,
2281 				       wpabuf_head(buf), wpabuf_len(buf));
2282 }
2283 
2284 
fst_hostapd_get_mb_ie_cb(void * ctx,const u8 * addr)2285 static const struct wpabuf * fst_hostapd_get_mb_ie_cb(void *ctx, const u8 *addr)
2286 {
2287 	struct hostapd_data *hapd = ctx;
2288 	struct sta_info *sta = ap_get_sta(hapd, addr);
2289 
2290 	return sta ? sta->mb_ies : NULL;
2291 }
2292 
2293 
fst_hostapd_update_mb_ie_cb(void * ctx,const u8 * addr,const u8 * buf,size_t size)2294 static void fst_hostapd_update_mb_ie_cb(void *ctx, const u8 *addr,
2295 					const u8 *buf, size_t size)
2296 {
2297 	struct hostapd_data *hapd = ctx;
2298 	struct sta_info *sta = ap_get_sta(hapd, addr);
2299 
2300 	if (sta) {
2301 		struct mb_ies_info info;
2302 
2303 		if (!mb_ies_info_by_ies(&info, buf, size)) {
2304 			wpabuf_free(sta->mb_ies);
2305 			sta->mb_ies = mb_ies_by_info(&info);
2306 		}
2307 	}
2308 }
2309 
2310 
fst_hostapd_get_sta(struct fst_get_peer_ctx ** get_ctx,bool mb_only)2311 static const u8 * fst_hostapd_get_sta(struct fst_get_peer_ctx **get_ctx,
2312 				      bool mb_only)
2313 {
2314 	struct sta_info *s = (struct sta_info *) *get_ctx;
2315 
2316 	if (mb_only) {
2317 		for (; s && !s->mb_ies; s = s->next)
2318 			;
2319 	}
2320 
2321 	if (s) {
2322 		*get_ctx = (struct fst_get_peer_ctx *) s->next;
2323 
2324 		return s->addr;
2325 	}
2326 
2327 	*get_ctx = NULL;
2328 	return NULL;
2329 }
2330 
2331 
fst_hostapd_get_peer_first(void * ctx,struct fst_get_peer_ctx ** get_ctx,bool mb_only)2332 static const u8 * fst_hostapd_get_peer_first(void *ctx,
2333 					     struct fst_get_peer_ctx **get_ctx,
2334 					     bool mb_only)
2335 {
2336 	struct hostapd_data *hapd = ctx;
2337 
2338 	*get_ctx = (struct fst_get_peer_ctx *) hapd->sta_list;
2339 
2340 	return fst_hostapd_get_sta(get_ctx, mb_only);
2341 }
2342 
2343 
fst_hostapd_get_peer_next(void * ctx,struct fst_get_peer_ctx ** get_ctx,bool mb_only)2344 static const u8 * fst_hostapd_get_peer_next(void *ctx,
2345 					    struct fst_get_peer_ctx **get_ctx,
2346 					    bool mb_only)
2347 {
2348 	return fst_hostapd_get_sta(get_ctx, mb_only);
2349 }
2350 
2351 
fst_hostapd_fill_iface_obj(struct hostapd_data * hapd,struct fst_wpa_obj * iface_obj)2352 void fst_hostapd_fill_iface_obj(struct hostapd_data *hapd,
2353 				struct fst_wpa_obj *iface_obj)
2354 {
2355 	os_memset(iface_obj, 0, sizeof(*iface_obj));
2356 	iface_obj->ctx = hapd;
2357 	iface_obj->get_bssid = fst_hostapd_get_bssid_cb;
2358 	iface_obj->get_channel_info = fst_hostapd_get_channel_info_cb;
2359 	iface_obj->get_hw_modes = fst_hostapd_get_hw_modes_cb;
2360 	iface_obj->set_ies = fst_hostapd_set_ies_cb;
2361 	iface_obj->send_action = fst_hostapd_send_action_cb;
2362 	iface_obj->get_mb_ie = fst_hostapd_get_mb_ie_cb;
2363 	iface_obj->update_mb_ie = fst_hostapd_update_mb_ie_cb;
2364 	iface_obj->get_peer_first = fst_hostapd_get_peer_first;
2365 	iface_obj->get_peer_next = fst_hostapd_get_peer_next;
2366 }
2367 
2368 #endif /* CONFIG_FST */
2369 
2370 #ifdef CONFIG_OWE
2371 
hostapd_owe_iface_iter(struct hostapd_iface * iface,void * ctx)2372 static int hostapd_owe_iface_iter(struct hostapd_iface *iface, void *ctx)
2373 {
2374 	struct hostapd_data *hapd = ctx;
2375 	size_t i;
2376 
2377 	for (i = 0; i < iface->num_bss; i++) {
2378 		struct hostapd_data *bss = iface->bss[i];
2379 
2380 		if (os_strcmp(hapd->conf->owe_transition_ifname,
2381 			      bss->conf->iface) != 0)
2382 			continue;
2383 
2384 		wpa_printf(MSG_DEBUG,
2385 			   "OWE: ifname=%s found transition mode ifname=%s BSSID "
2386 			   MACSTR " SSID %s",
2387 			   hapd->conf->iface, bss->conf->iface,
2388 			   MAC2STR(bss->own_addr),
2389 			   wpa_ssid_txt(bss->conf->ssid.ssid,
2390 					bss->conf->ssid.ssid_len));
2391 		if (!bss->conf->ssid.ssid_set || !bss->conf->ssid.ssid_len ||
2392 		    is_zero_ether_addr(bss->own_addr))
2393 			continue;
2394 
2395 		os_memcpy(hapd->conf->owe_transition_bssid, bss->own_addr,
2396 			  ETH_ALEN);
2397 		os_memcpy(hapd->conf->owe_transition_ssid,
2398 			  bss->conf->ssid.ssid, bss->conf->ssid.ssid_len);
2399 		hapd->conf->owe_transition_ssid_len = bss->conf->ssid.ssid_len;
2400 		wpa_printf(MSG_DEBUG,
2401 			   "OWE: Copied transition mode information");
2402 		return 1;
2403 	}
2404 
2405 	return 0;
2406 }
2407 
2408 
hostapd_owe_trans_get_info(struct hostapd_data * hapd)2409 int hostapd_owe_trans_get_info(struct hostapd_data *hapd)
2410 {
2411 	if (hapd->conf->owe_transition_ssid_len > 0 &&
2412 	    !is_zero_ether_addr(hapd->conf->owe_transition_bssid))
2413 		return 0;
2414 
2415 	/* Find transition mode SSID/BSSID information from a BSS operated by
2416 	 * this hostapd instance. */
2417 	if (!hapd->iface->interfaces ||
2418 	    !hapd->iface->interfaces->for_each_interface)
2419 		return hostapd_owe_iface_iter(hapd->iface, hapd);
2420 	else
2421 		return hapd->iface->interfaces->for_each_interface(
2422 			hapd->iface->interfaces, hostapd_owe_iface_iter, hapd);
2423 }
2424 
2425 
hostapd_owe_iface_iter2(struct hostapd_iface * iface,void * ctx)2426 static int hostapd_owe_iface_iter2(struct hostapd_iface *iface, void *ctx)
2427 {
2428 	size_t i;
2429 
2430 	for (i = 0; i < iface->num_bss; i++) {
2431 		struct hostapd_data *bss = iface->bss[i];
2432 		int res;
2433 
2434 		if (!bss->conf->owe_transition_ifname[0])
2435 			continue;
2436 		if (bss->iface->state != HAPD_IFACE_ENABLED) {
2437 			wpa_printf(MSG_DEBUG,
2438 				   "OWE: Interface %s state %s - defer beacon update",
2439 				   bss->conf->iface,
2440 				   hostapd_state_text(bss->iface->state));
2441 			continue;
2442 		}
2443 		res = hostapd_owe_trans_get_info(bss);
2444 		if (res == 0)
2445 			continue;
2446 		wpa_printf(MSG_DEBUG,
2447 			   "OWE: Matching transition mode interface enabled - update beacon data for %s",
2448 			   bss->conf->iface);
2449 		ieee802_11_set_beacon(bss);
2450 	}
2451 
2452 	return 0;
2453 }
2454 
2455 #endif /* CONFIG_OWE */
2456 
2457 
hostapd_owe_update_trans(struct hostapd_iface * iface)2458 static void hostapd_owe_update_trans(struct hostapd_iface *iface)
2459 {
2460 #ifdef CONFIG_OWE
2461 	/* Check whether the enabled BSS can complete OWE transition mode
2462 	 * configuration for any pending interface. */
2463 	if (!iface->interfaces ||
2464 	    !iface->interfaces->for_each_interface)
2465 		hostapd_owe_iface_iter2(iface, NULL);
2466 	else
2467 		iface->interfaces->for_each_interface(
2468 			iface->interfaces, hostapd_owe_iface_iter2, NULL);
2469 #endif /* CONFIG_OWE */
2470 }
2471 
2472 
hostapd_interface_setup_failure_handler(void * eloop_ctx,void * timeout_ctx)2473 static void hostapd_interface_setup_failure_handler(void *eloop_ctx,
2474 						    void *timeout_ctx)
2475 {
2476 	struct hostapd_iface *iface = eloop_ctx;
2477 	struct hostapd_data *hapd;
2478 
2479 	if (iface->num_bss < 1 || !iface->bss || !iface->bss[0])
2480 		return;
2481 	hapd = iface->bss[0];
2482 	if (hapd->setup_complete_cb)
2483 		hapd->setup_complete_cb(hapd->setup_complete_cb_ctx);
2484 }
2485 
2486 
hostapd_setup_interface_complete_sync(struct hostapd_iface * iface,int err)2487 static int hostapd_setup_interface_complete_sync(struct hostapd_iface *iface,
2488 						 int err)
2489 {
2490 	struct hostapd_data *hapd = iface->bss[0];
2491 	size_t j;
2492 	u8 *prev_addr;
2493 	int delay_apply_cfg = 0;
2494 	int res_dfs_offload = 0;
2495 
2496 	if (err)
2497 		goto fail;
2498 
2499 	wpa_printf(MSG_DEBUG, "Completing interface initialization");
2500 	if (iface->freq) {
2501 #ifdef NEED_AP_MLME
2502 		int res;
2503 #endif /* NEED_AP_MLME */
2504 
2505 		wpa_printf(MSG_DEBUG, "Mode: %s  Channel: %d  "
2506 			   "Frequency: %d MHz",
2507 			   hostapd_hw_mode_txt(iface->conf->hw_mode),
2508 			   iface->conf->channel, iface->freq);
2509 
2510 #ifdef NEED_AP_MLME
2511 		/* Handle DFS only if it is not offloaded to the driver */
2512 		if (!(iface->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD)) {
2513 			/* Check DFS */
2514 			res = hostapd_handle_dfs(iface);
2515 			if (res <= 0) {
2516 				if (res < 0)
2517 					goto fail;
2518 				return res;
2519 			}
2520 		} else {
2521 			/* If DFS is offloaded to the driver */
2522 			res_dfs_offload = hostapd_handle_dfs_offload(iface);
2523 			if (res_dfs_offload <= 0) {
2524 				if (res_dfs_offload < 0)
2525 					goto fail;
2526 			} else {
2527 				wpa_printf(MSG_DEBUG,
2528 					   "Proceed with AP/channel setup");
2529 				/*
2530 				 * If this is a DFS channel, move to completing
2531 				 * AP setup.
2532 				 */
2533 				if (res_dfs_offload == 1)
2534 					goto dfs_offload;
2535 				/* Otherwise fall through. */
2536 			}
2537 		}
2538 #endif /* NEED_AP_MLME */
2539 
2540 #ifdef CONFIG_MESH
2541 		if (iface->mconf != NULL) {
2542 			wpa_printf(MSG_DEBUG,
2543 				   "%s: Mesh configuration will be applied while joining the mesh network",
2544 				   iface->bss[0]->conf->iface);
2545 			delay_apply_cfg = 1;
2546 		}
2547 #endif /* CONFIG_MESH */
2548 
2549 		if (!delay_apply_cfg &&
2550 		    hostapd_set_freq(hapd, hapd->iconf->hw_mode, iface->freq,
2551 				     hapd->iconf->channel,
2552 				     hapd->iconf->enable_edmg,
2553 				     hapd->iconf->edmg_channel,
2554 				     hapd->iconf->ieee80211n,
2555 				     hapd->iconf->ieee80211ac,
2556 				     hapd->iconf->ieee80211ax,
2557 				     hapd->iconf->ieee80211be,
2558 				     hapd->iconf->secondary_channel,
2559 				     hostapd_get_oper_chwidth(hapd->iconf),
2560 				     hostapd_get_oper_centr_freq_seg0_idx(
2561 					     hapd->iconf),
2562 				     hostapd_get_oper_centr_freq_seg1_idx(
2563 					     hapd->iconf))) {
2564 			wpa_printf(MSG_ERROR, "Could not set channel for "
2565 				   "kernel driver");
2566 			goto fail;
2567 		}
2568 	}
2569 
2570 	if (iface->current_mode) {
2571 		if (hostapd_prepare_rates(iface, iface->current_mode)) {
2572 			wpa_printf(MSG_ERROR, "Failed to prepare rates "
2573 				   "table.");
2574 			hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
2575 				       HOSTAPD_LEVEL_WARNING,
2576 				       "Failed to prepare rates table.");
2577 			goto fail;
2578 		}
2579 	}
2580 
2581 	if (hapd->iconf->rts_threshold >= -1 &&
2582 	    hostapd_set_rts(hapd, hapd->iconf->rts_threshold) &&
2583 	    hapd->iconf->rts_threshold >= -1) {
2584 		wpa_printf(MSG_ERROR, "Could not set RTS threshold for "
2585 			   "kernel driver");
2586 		goto fail;
2587 	}
2588 
2589 	if (hapd->iconf->fragm_threshold >= -1 &&
2590 	    hostapd_set_frag(hapd, hapd->iconf->fragm_threshold) &&
2591 	    hapd->iconf->fragm_threshold != -1) {
2592 		wpa_printf(MSG_ERROR, "Could not set fragmentation threshold "
2593 			   "for kernel driver");
2594 		goto fail;
2595 	}
2596 
2597 	prev_addr = hapd->own_addr;
2598 
2599 	for (j = 0; j < iface->num_bss; j++) {
2600 		hapd = iface->bss[j];
2601 		if (j)
2602 			os_memcpy(hapd->own_addr, prev_addr, ETH_ALEN);
2603 		if (hostapd_setup_bss(hapd, j == 0, !iface->conf->mbssid)) {
2604 			for (;;) {
2605 				hapd = iface->bss[j];
2606 				hostapd_bss_deinit_no_free(hapd);
2607 				hostapd_free_hapd_data(hapd);
2608 				if (j == 0)
2609 					break;
2610 				j--;
2611 			}
2612 			goto fail;
2613 		}
2614 		if (is_zero_ether_addr(hapd->conf->bssid))
2615 			prev_addr = hapd->own_addr;
2616 	}
2617 
2618 	if (hapd->iconf->mbssid) {
2619 		for (j = 0; hapd->iconf->mbssid && j < iface->num_bss; j++) {
2620 			hapd = iface->bss[j];
2621 			if (hostapd_start_beacon(hapd, true)) {
2622 				for (;;) {
2623 					hapd = iface->bss[j];
2624 					hostapd_bss_deinit_no_free(hapd);
2625 					hostapd_free_hapd_data(hapd);
2626 					if (j == 0)
2627 						break;
2628 					j--;
2629 				}
2630 				goto fail;
2631 			}
2632 		}
2633 	}
2634 
2635 	hapd = iface->bss[0];
2636 
2637 	hostapd_tx_queue_params(iface);
2638 
2639 	ap_list_init(iface);
2640 
2641 	hostapd_set_acl(hapd);
2642 
2643 	if (hostapd_driver_commit(hapd) < 0) {
2644 		wpa_printf(MSG_ERROR, "%s: Failed to commit driver "
2645 			   "configuration", __func__);
2646 		goto fail;
2647 	}
2648 
2649 	/*
2650 	 * WPS UPnP module can be initialized only when the "upnp_iface" is up.
2651 	 * If "interface" and "upnp_iface" are the same (e.g., non-bridge
2652 	 * mode), the interface is up only after driver_commit, so initialize
2653 	 * WPS after driver_commit.
2654 	 */
2655 	for (j = 0; j < iface->num_bss; j++) {
2656 		if (hostapd_init_wps_complete(iface->bss[j]))
2657 			goto fail;
2658 	}
2659 
2660 	if ((iface->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
2661 	    !res_dfs_offload) {
2662 		/*
2663 		 * If freq is DFS, and DFS is offloaded to the driver, then wait
2664 		 * for CAC to complete.
2665 		 */
2666 		wpa_printf(MSG_DEBUG, "%s: Wait for CAC to complete", __func__);
2667 		return res_dfs_offload;
2668 	}
2669 
2670 #ifdef NEED_AP_MLME
2671 dfs_offload:
2672 #endif /* NEED_AP_MLME */
2673 
2674 #ifdef CONFIG_FST
2675 	if (hapd->iconf->fst_cfg.group_id[0]) {
2676 		struct fst_wpa_obj iface_obj;
2677 
2678 		fst_hostapd_fill_iface_obj(hapd, &iface_obj);
2679 		iface->fst = fst_attach(hapd->conf->iface, hapd->own_addr,
2680 					&iface_obj, &hapd->iconf->fst_cfg);
2681 		if (!iface->fst) {
2682 			wpa_printf(MSG_ERROR, "Could not attach to FST %s",
2683 				   hapd->iconf->fst_cfg.group_id);
2684 			goto fail;
2685 		}
2686 	}
2687 #endif /* CONFIG_FST */
2688 
2689 	hostapd_set_state(iface, HAPD_IFACE_ENABLED);
2690 	hostapd_owe_update_trans(iface);
2691 	airtime_policy_update_init(iface);
2692 	wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_ENABLED);
2693 	if (hapd->setup_complete_cb)
2694 		hapd->setup_complete_cb(hapd->setup_complete_cb_ctx);
2695 
2696 #ifdef CONFIG_MESH
2697 	if (delay_apply_cfg && !iface->mconf) {
2698 		wpa_printf(MSG_ERROR, "Error while completing mesh init");
2699 		goto fail;
2700 	}
2701 #endif /* CONFIG_MESH */
2702 
2703 	wpa_printf(MSG_DEBUG, "%s: Setup of interface done.",
2704 		   iface->bss[0]->conf->iface);
2705 	if (iface->interfaces && iface->interfaces->terminate_on_error > 0)
2706 		iface->interfaces->terminate_on_error--;
2707 
2708 	for (j = 0; j < iface->num_bss; j++)
2709 		hostapd_neighbor_set_own_report(iface->bss[j]);
2710 
2711 	if (iface->interfaces && iface->interfaces->count > 1)
2712 		ieee802_11_set_beacons(iface);
2713 
2714 	return 0;
2715 
2716 fail:
2717 	wpa_printf(MSG_ERROR, "Interface initialization failed");
2718 
2719 	if (iface->is_no_ir) {
2720 		hostapd_set_state(iface, HAPD_IFACE_NO_IR);
2721 		wpa_msg(hapd->msg_ctx, MSG_INFO, AP_EVENT_NO_IR);
2722 		return 0;
2723 	}
2724 
2725 	hostapd_set_state(iface, HAPD_IFACE_DISABLED);
2726 	wpa_msg(hapd->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
2727 #ifdef CONFIG_FST
2728 	if (iface->fst) {
2729 		fst_detach(iface->fst);
2730 		iface->fst = NULL;
2731 	}
2732 #endif /* CONFIG_FST */
2733 
2734 	if (iface->interfaces && iface->interfaces->terminate_on_error) {
2735 		eloop_terminate();
2736 	} else if (hapd->setup_complete_cb) {
2737 		/*
2738 		 * Calling hapd->setup_complete_cb directly may cause iface
2739 		 * deinitialization which may be accessed later by the caller.
2740 		 */
2741 		eloop_register_timeout(0, 0,
2742 				       hostapd_interface_setup_failure_handler,
2743 				       iface, NULL);
2744 	}
2745 
2746 	return -1;
2747 }
2748 
2749 
2750 /**
2751  * hostapd_setup_interface_complete - Complete interface setup
2752  *
2753  * This function is called when previous steps in the interface setup has been
2754  * completed. This can also start operations, e.g., DFS, that will require
2755  * additional processing before interface is ready to be enabled. Such
2756  * operations will call this function from eloop callbacks when finished.
2757  */
hostapd_setup_interface_complete(struct hostapd_iface * iface,int err)2758 int hostapd_setup_interface_complete(struct hostapd_iface *iface, int err)
2759 {
2760 	struct hapd_interfaces *interfaces = iface->interfaces;
2761 	struct hostapd_data *hapd = iface->bss[0];
2762 	unsigned int i;
2763 	int not_ready_in_sync_ifaces = 0;
2764 
2765 	if (!iface->need_to_start_in_sync)
2766 		return hostapd_setup_interface_complete_sync(iface, err);
2767 
2768 	if (err) {
2769 		wpa_printf(MSG_ERROR, "Interface initialization failed");
2770 		iface->need_to_start_in_sync = 0;
2771 
2772 		if (iface->is_no_ir) {
2773 			hostapd_set_state(iface, HAPD_IFACE_NO_IR);
2774 			wpa_msg(hapd->msg_ctx, MSG_INFO, AP_EVENT_NO_IR);
2775 			return 0;
2776 		}
2777 
2778 		hostapd_set_state(iface, HAPD_IFACE_DISABLED);
2779 		wpa_msg(hapd->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
2780 		if (interfaces && interfaces->terminate_on_error)
2781 			eloop_terminate();
2782 		return -1;
2783 	}
2784 
2785 	if (iface->ready_to_start_in_sync) {
2786 		/* Already in ready and waiting. should never happpen */
2787 		return 0;
2788 	}
2789 
2790 	for (i = 0; i < interfaces->count; i++) {
2791 		if (interfaces->iface[i]->need_to_start_in_sync &&
2792 		    !interfaces->iface[i]->ready_to_start_in_sync)
2793 			not_ready_in_sync_ifaces++;
2794 	}
2795 
2796 	/*
2797 	 * Check if this is the last interface, if yes then start all the other
2798 	 * waiting interfaces. If not, add this interface to the waiting list.
2799 	 */
2800 	if (not_ready_in_sync_ifaces > 1 && iface->state == HAPD_IFACE_DFS) {
2801 		/*
2802 		 * If this interface went through CAC, do not synchronize, just
2803 		 * start immediately.
2804 		 */
2805 		iface->need_to_start_in_sync = 0;
2806 		wpa_printf(MSG_INFO,
2807 			   "%s: Finished CAC - bypass sync and start interface",
2808 			   iface->bss[0]->conf->iface);
2809 		return hostapd_setup_interface_complete_sync(iface, err);
2810 	}
2811 
2812 	if (not_ready_in_sync_ifaces > 1) {
2813 		/* need to wait as there are other interfaces still coming up */
2814 		iface->ready_to_start_in_sync = 1;
2815 		wpa_printf(MSG_INFO,
2816 			   "%s: Interface waiting to sync with other interfaces",
2817 			   iface->bss[0]->conf->iface);
2818 		return 0;
2819 	}
2820 
2821 	wpa_printf(MSG_INFO,
2822 		   "%s: Last interface to sync - starting all interfaces",
2823 		   iface->bss[0]->conf->iface);
2824 	iface->need_to_start_in_sync = 0;
2825 	hostapd_setup_interface_complete_sync(iface, err);
2826 	for (i = 0; i < interfaces->count; i++) {
2827 		if (interfaces->iface[i]->need_to_start_in_sync &&
2828 		    interfaces->iface[i]->ready_to_start_in_sync) {
2829 			hostapd_setup_interface_complete_sync(
2830 				interfaces->iface[i], 0);
2831 			/* Only once the interfaces are sync started */
2832 			interfaces->iface[i]->need_to_start_in_sync = 0;
2833 		}
2834 	}
2835 
2836 	return 0;
2837 }
2838 
2839 
2840 /**
2841  * hostapd_setup_interface - Setup of an interface
2842  * @iface: Pointer to interface data.
2843  * Returns: 0 on success, -1 on failure
2844  *
2845  * Initializes the driver interface, validates the configuration,
2846  * and sets driver parameters based on the configuration.
2847  * Flushes old stations, sets the channel, encryption,
2848  * beacons, and WDS links based on the configuration.
2849  *
2850  * If interface setup requires more time, e.g., to perform HT co-ex scans, ACS,
2851  * or DFS operations, this function returns 0 before such operations have been
2852  * completed. The pending operations are registered into eloop and will be
2853  * completed from eloop callbacks. Those callbacks end up calling
2854  * hostapd_setup_interface_complete() once setup has been completed.
2855  */
hostapd_setup_interface(struct hostapd_iface * iface)2856 int hostapd_setup_interface(struct hostapd_iface *iface)
2857 {
2858 	int ret;
2859 
2860 	if (!iface->conf)
2861 		return -1;
2862 	ret = setup_interface(iface);
2863 	if (ret) {
2864 		wpa_printf(MSG_ERROR, "%s: Unable to setup interface.",
2865 			   iface->conf->bss[0]->iface);
2866 		return -1;
2867 	}
2868 
2869 	return 0;
2870 }
2871 
2872 
2873 /**
2874  * hostapd_alloc_bss_data - Allocate and initialize per-BSS data
2875  * @hapd_iface: Pointer to interface data
2876  * @conf: Pointer to per-interface configuration
2877  * @bss: Pointer to per-BSS configuration for this BSS
2878  * Returns: Pointer to allocated BSS data
2879  *
2880  * This function is used to allocate per-BSS data structure. This data will be
2881  * freed after hostapd_cleanup() is called for it during interface
2882  * deinitialization.
2883  */
2884 struct hostapd_data *
hostapd_alloc_bss_data(struct hostapd_iface * hapd_iface,struct hostapd_config * conf,struct hostapd_bss_config * bss)2885 hostapd_alloc_bss_data(struct hostapd_iface *hapd_iface,
2886 		       struct hostapd_config *conf,
2887 		       struct hostapd_bss_config *bss)
2888 {
2889 	struct hostapd_data *hapd;
2890 
2891 	hapd = os_zalloc(sizeof(*hapd));
2892 	if (hapd == NULL)
2893 		return NULL;
2894 
2895 	hapd->new_assoc_sta_cb = hostapd_new_assoc_sta;
2896 	hapd->iconf = conf;
2897 	hapd->conf = bss;
2898 	hapd->iface = hapd_iface;
2899 	if (conf)
2900 		hapd->driver = conf->driver;
2901 	hapd->ctrl_sock = -1;
2902 	dl_list_init(&hapd->ctrl_dst);
2903 	dl_list_init(&hapd->nr_db);
2904 	hapd->dhcp_sock = -1;
2905 #ifdef CONFIG_IEEE80211R_AP
2906 	dl_list_init(&hapd->l2_queue);
2907 	dl_list_init(&hapd->l2_oui_queue);
2908 #endif /* CONFIG_IEEE80211R_AP */
2909 #ifdef CONFIG_SAE
2910 	dl_list_init(&hapd->sae_commit_queue);
2911 #endif /* CONFIG_SAE */
2912 
2913 	return hapd;
2914 }
2915 
2916 
hostapd_bss_deinit(struct hostapd_data * hapd)2917 static void hostapd_bss_deinit(struct hostapd_data *hapd)
2918 {
2919 	if (!hapd)
2920 		return;
2921 	wpa_printf(MSG_DEBUG, "%s: deinit bss %s", __func__,
2922 		   hapd->conf ? hapd->conf->iface : "N/A");
2923 	hostapd_bss_deinit_no_free(hapd);
2924 	wpa_msg(hapd->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
2925 #ifdef CONFIG_SQLITE
2926 	if (hapd->rad_attr_db) {
2927 		sqlite3_close(hapd->rad_attr_db);
2928 		hapd->rad_attr_db = NULL;
2929 	}
2930 #endif /* CONFIG_SQLITE */
2931 
2932 	hostapd_bss_link_deinit(hapd);
2933 	hostapd_cleanup(hapd);
2934 }
2935 
2936 
hostapd_interface_deinit(struct hostapd_iface * iface)2937 void hostapd_interface_deinit(struct hostapd_iface *iface)
2938 {
2939 	int j;
2940 
2941 	wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
2942 	if (iface == NULL)
2943 		return;
2944 
2945 	hostapd_set_state(iface, HAPD_IFACE_DISABLED);
2946 
2947 	eloop_cancel_timeout(channel_list_update_timeout, iface, NULL);
2948 	iface->wait_channel_update = 0;
2949 	iface->is_no_ir = false;
2950 
2951 #ifdef CONFIG_FST
2952 	if (iface->fst) {
2953 		fst_detach(iface->fst);
2954 		iface->fst = NULL;
2955 	}
2956 #endif /* CONFIG_FST */
2957 
2958 	for (j = (int) iface->num_bss - 1; j >= 0; j--) {
2959 		if (!iface->bss)
2960 			break;
2961 		hostapd_bss_deinit(iface->bss[j]);
2962 	}
2963 
2964 #ifdef NEED_AP_MLME
2965 	hostapd_stop_setup_timers(iface);
2966 	eloop_cancel_timeout(ap_ht2040_timeout, iface, NULL);
2967 #endif /* NEED_AP_MLME */
2968 }
2969 
2970 
2971 #ifdef CONFIG_IEEE80211BE
2972 
hostapd_mld_ref_inc(struct hostapd_mld * mld)2973 static void hostapd_mld_ref_inc(struct hostapd_mld *mld)
2974 {
2975 	if (!mld)
2976 		return;
2977 
2978 	if (mld->refcount == HOSTAPD_MLD_MAX_REF_COUNT) {
2979 		wpa_printf(MSG_ERROR, "AP MLD %s: Ref count overflow",
2980 			   mld->name);
2981 		return;
2982 	}
2983 
2984 	mld->refcount++;
2985 }
2986 
2987 
hostapd_mld_ref_dec(struct hostapd_mld * mld)2988 static void hostapd_mld_ref_dec(struct hostapd_mld *mld)
2989 {
2990 	if (!mld)
2991 		return;
2992 
2993 	if (!mld->refcount) {
2994 		wpa_printf(MSG_ERROR, "AP MLD %s: Ref count underflow",
2995 			   mld->name);
2996 		return;
2997 	}
2998 
2999 	mld->refcount--;
3000 }
3001 
3002 #endif /* CONFIG_IEEE80211BE */
3003 
3004 
hostapd_interface_free(struct hostapd_iface * iface)3005 void hostapd_interface_free(struct hostapd_iface *iface)
3006 {
3007 	size_t j;
3008 	wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
3009 	for (j = 0; j < iface->num_bss; j++) {
3010 		if (!iface->bss)
3011 			break;
3012 #ifdef CONFIG_IEEE80211BE
3013 		if (iface->bss[j])
3014 			hostapd_mld_ref_dec(iface->bss[j]->mld);
3015 #endif /* CONFIG_IEEE80211BE */
3016 		wpa_printf(MSG_DEBUG, "%s: free hapd %p",
3017 			   __func__, iface->bss[j]);
3018 		os_free(iface->bss[j]);
3019 	}
3020 	hostapd_cleanup_iface(iface);
3021 }
3022 
3023 
hostapd_alloc_iface(void)3024 struct hostapd_iface * hostapd_alloc_iface(void)
3025 {
3026 	struct hostapd_iface *hapd_iface;
3027 
3028 	hapd_iface = os_zalloc(sizeof(*hapd_iface));
3029 	if (!hapd_iface)
3030 		return NULL;
3031 
3032 	dl_list_init(&hapd_iface->sta_seen);
3033 
3034 	return hapd_iface;
3035 }
3036 
3037 
3038 #ifdef CONFIG_IEEE80211BE
hostapd_bss_alloc_link_id(struct hostapd_data * hapd)3039 static void hostapd_bss_alloc_link_id(struct hostapd_data *hapd)
3040 {
3041 	hapd->mld_link_id = hapd->mld->next_link_id++;
3042 	wpa_printf(MSG_DEBUG, "AP MLD: %s: Link ID %d assigned.",
3043 		   hapd->mld->name, hapd->mld_link_id);
3044 }
3045 #endif /* CONFIG_IEEE80211BE */
3046 
3047 
hostapd_bss_setup_multi_link(struct hostapd_data * hapd,struct hapd_interfaces * interfaces)3048 static void hostapd_bss_setup_multi_link(struct hostapd_data *hapd,
3049 					 struct hapd_interfaces *interfaces)
3050 {
3051 #ifdef CONFIG_IEEE80211BE
3052 	struct hostapd_mld *mld, **all_mld;
3053 	struct hostapd_bss_config *conf;
3054 	size_t i;
3055 
3056 	conf = hapd->conf;
3057 
3058 	if (!hapd->iconf || !hapd->iconf->ieee80211be || !conf->mld_ap ||
3059 	    conf->disable_11be)
3060 		return;
3061 
3062 	for (i = 0; i < interfaces->mld_count; i++) {
3063 		mld = interfaces->mld[i];
3064 
3065 		if (!mld || os_strcmp(conf->iface, mld->name) != 0)
3066 			continue;
3067 
3068 		hapd->mld = mld;
3069 		hostapd_mld_ref_inc(mld);
3070 		hostapd_bss_alloc_link_id(hapd);
3071 		break;
3072 	}
3073 
3074 	if (hapd->mld)
3075 		return;
3076 
3077 	mld = os_zalloc(sizeof(struct hostapd_mld));
3078 	if (!mld)
3079 		goto fail;
3080 
3081 	os_strlcpy(mld->name, conf->iface, sizeof(conf->iface));
3082 	dl_list_init(&mld->links);
3083 
3084 	wpa_printf(MSG_DEBUG, "AP MLD %s created", mld->name);
3085 
3086 	hapd->mld = mld;
3087 	hostapd_mld_ref_inc(mld);
3088 	hostapd_bss_alloc_link_id(hapd);
3089 
3090 	all_mld = os_realloc_array(interfaces->mld, interfaces->mld_count + 1,
3091 				   sizeof(struct hostapd_mld *));
3092 	if (!all_mld)
3093 		goto fail;
3094 
3095 	interfaces->mld = all_mld;
3096 	interfaces->mld[interfaces->mld_count] = mld;
3097 	interfaces->mld_count++;
3098 
3099 	return;
3100 fail:
3101 	if (!mld)
3102 		return;
3103 
3104 	wpa_printf(MSG_DEBUG, "AP MLD %s: free mld %p", mld->name, mld);
3105 	os_free(mld);
3106 	hapd->mld = NULL;
3107 #endif /* CONFIG_IEEE80211BE */
3108 }
3109 
3110 
hostapd_cleanup_unused_mlds(struct hapd_interfaces * interfaces)3111 static void hostapd_cleanup_unused_mlds(struct hapd_interfaces *interfaces)
3112 {
3113 #ifdef CONFIG_IEEE80211BE
3114 	struct hostapd_mld *mld, **all_mld;
3115 	size_t i, j, num_mlds;
3116 	bool forced_remove, remove;
3117 
3118 	if (!interfaces->mld)
3119 		return;
3120 
3121 	num_mlds = interfaces->mld_count;
3122 
3123 	for (i = 0; i < interfaces->mld_count; i++) {
3124 		mld = interfaces->mld[i];
3125 		if (!mld)
3126 			continue;
3127 
3128 		remove = false;
3129 		forced_remove = false;
3130 
3131 		if (!mld->refcount)
3132 			remove = true;
3133 
3134 		/* If MLD is still being referenced but the number of interfaces
3135 		 * is zero, it is safe to force its deletion. Normally, this
3136 		 * should not happen but even if it does, let us free the
3137 		 * memory.
3138 		 */
3139 		if (!remove && !interfaces->count)
3140 			forced_remove = true;
3141 
3142 		if (!remove && !forced_remove)
3143 			continue;
3144 
3145 		wpa_printf(MSG_DEBUG, "AP MLD %s: Freed%s", mld->name,
3146 			   forced_remove ? " (forced)" : "");
3147 		os_free(mld);
3148 		interfaces->mld[i] = NULL;
3149 		num_mlds--;
3150 	}
3151 
3152 	if (!num_mlds) {
3153 		interfaces->mld_count = 0;
3154 		os_free(interfaces->mld);
3155 		interfaces->mld = NULL;
3156 		return;
3157 	}
3158 
3159 	all_mld = os_zalloc(num_mlds * sizeof(struct hostapd_mld *));
3160 	if (!all_mld) {
3161 		wpa_printf(MSG_ERROR,
3162 			   "AP MLD: Failed to re-allocate the MLDs. Expect issues");
3163 		return;
3164 	}
3165 
3166 	for (i = 0, j = 0; i < interfaces->mld_count; i++) {
3167 		mld = interfaces->mld[i];
3168 		if (!mld)
3169 			continue;
3170 
3171 		all_mld[j++] = mld;
3172 	}
3173 
3174 	/* This should not happen */
3175 	if (j != num_mlds) {
3176 		wpa_printf(MSG_DEBUG,
3177 			   "AP MLD: Some error occurred while reallocating MLDs. Expect issues.");
3178 		os_free(all_mld);
3179 		return;
3180 	}
3181 
3182 	os_free(interfaces->mld);
3183 	interfaces->mld = all_mld;
3184 	interfaces->mld_count = num_mlds;
3185 #endif /* CONFIG_IEEE80211BE */
3186 }
3187 
3188 
3189 /**
3190  * hostapd_init - Allocate and initialize per-interface data
3191  * @config_file: Path to the configuration file
3192  * Returns: Pointer to the allocated interface data or %NULL on failure
3193  *
3194  * This function is used to allocate main data structures for per-interface
3195  * data. The allocated data buffer will be freed by calling
3196  * hostapd_cleanup_iface().
3197  */
hostapd_init(struct hapd_interfaces * interfaces,const char * config_file)3198 struct hostapd_iface * hostapd_init(struct hapd_interfaces *interfaces,
3199 				    const char *config_file)
3200 {
3201 	struct hostapd_iface *hapd_iface = NULL;
3202 	struct hostapd_config *conf = NULL;
3203 	struct hostapd_data *hapd;
3204 	size_t i;
3205 
3206 	hapd_iface = hostapd_alloc_iface();
3207 	if (hapd_iface == NULL)
3208 		goto fail;
3209 
3210 	hapd_iface->config_fname = os_strdup(config_file);
3211 	if (hapd_iface->config_fname == NULL)
3212 		goto fail;
3213 
3214 	conf = interfaces->config_read_cb(hapd_iface->config_fname);
3215 	if (conf == NULL)
3216 		goto fail;
3217 	hapd_iface->conf = conf;
3218 
3219 	hapd_iface->num_bss = conf->num_bss;
3220 	hapd_iface->bss = os_calloc(conf->num_bss,
3221 				    sizeof(struct hostapd_data *));
3222 	if (hapd_iface->bss == NULL)
3223 		goto fail;
3224 
3225 	for (i = 0; i < conf->num_bss; i++) {
3226 		hapd = hapd_iface->bss[i] =
3227 			hostapd_alloc_bss_data(hapd_iface, conf,
3228 					       conf->bss[i]);
3229 		if (hapd == NULL)
3230 			goto fail;
3231 		hapd->msg_ctx = hapd;
3232 		hostapd_bss_setup_multi_link(hapd, interfaces);
3233 	}
3234 
3235 	hapd_iface->is_ch_switch_dfs = false;
3236 	return hapd_iface;
3237 
3238 fail:
3239 	wpa_printf(MSG_ERROR, "Failed to set up interface with %s",
3240 		   config_file);
3241 	if (conf)
3242 		hostapd_config_free(conf);
3243 	if (hapd_iface) {
3244 		os_free(hapd_iface->config_fname);
3245 		os_free(hapd_iface->bss);
3246 		wpa_printf(MSG_DEBUG, "%s: free iface %p",
3247 			   __func__, hapd_iface);
3248 		os_free(hapd_iface);
3249 	}
3250 	return NULL;
3251 }
3252 
3253 
ifname_in_use(struct hapd_interfaces * interfaces,const char * ifname)3254 static int ifname_in_use(struct hapd_interfaces *interfaces, const char *ifname)
3255 {
3256 	size_t i, j;
3257 
3258 	for (i = 0; i < interfaces->count; i++) {
3259 		struct hostapd_iface *iface = interfaces->iface[i];
3260 		for (j = 0; j < iface->num_bss; j++) {
3261 			struct hostapd_data *hapd = iface->bss[j];
3262 			if (os_strcmp(ifname, hapd->conf->iface) == 0)
3263 				return 1;
3264 		}
3265 	}
3266 
3267 	return 0;
3268 }
3269 
3270 
3271 /**
3272  * hostapd_interface_init_bss - Read configuration file and init BSS data
3273  *
3274  * This function is used to parse configuration file for a BSS. This BSS is
3275  * added to an existing interface sharing the same radio (if any) or a new
3276  * interface is created if this is the first interface on a radio. This
3277  * allocate memory for the BSS. No actual driver operations are started.
3278  *
3279  * This is similar to hostapd_interface_init(), but for a case where the
3280  * configuration is used to add a single BSS instead of all BSSes for a radio.
3281  */
3282 struct hostapd_iface *
hostapd_interface_init_bss(struct hapd_interfaces * interfaces,const char * phy,const char * config_fname,int debug)3283 hostapd_interface_init_bss(struct hapd_interfaces *interfaces, const char *phy,
3284 			   const char *config_fname, int debug)
3285 {
3286 	struct hostapd_iface *new_iface = NULL, *iface = NULL;
3287 	struct hostapd_data *hapd;
3288 	int k;
3289 	size_t i, bss_idx;
3290 
3291 	if (!phy || !*phy)
3292 		return NULL;
3293 
3294 	for (i = 0; i < interfaces->count; i++) {
3295 		if (os_strcmp(interfaces->iface[i]->phy, phy) == 0) {
3296 			iface = interfaces->iface[i];
3297 			break;
3298 		}
3299 	}
3300 
3301 	wpa_printf(MSG_INFO, "Configuration file: %s (phy %s)%s",
3302 		   config_fname, phy, iface ? "" : " --> new PHY");
3303 	if (iface) {
3304 		struct hostapd_config *conf;
3305 		struct hostapd_bss_config **tmp_conf;
3306 		struct hostapd_data **tmp_bss;
3307 		struct hostapd_bss_config *bss;
3308 		const char *ifname;
3309 
3310 		/* Add new BSS to existing iface */
3311 		conf = interfaces->config_read_cb(config_fname);
3312 		if (conf == NULL)
3313 			return NULL;
3314 		if (conf->num_bss > 1) {
3315 			wpa_printf(MSG_ERROR, "Multiple BSSes specified in BSS-config");
3316 			hostapd_config_free(conf);
3317 			return NULL;
3318 		}
3319 
3320 		ifname = conf->bss[0]->iface;
3321 		if (ifname[0] != '\0' && ifname_in_use(interfaces, ifname)) {
3322 			wpa_printf(MSG_ERROR,
3323 				   "Interface name %s already in use", ifname);
3324 			hostapd_config_free(conf);
3325 			return NULL;
3326 		}
3327 
3328 		tmp_conf = os_realloc_array(
3329 			iface->conf->bss, iface->conf->num_bss + 1,
3330 			sizeof(struct hostapd_bss_config *));
3331 		tmp_bss = os_realloc_array(iface->bss, iface->num_bss + 1,
3332 					   sizeof(struct hostapd_data *));
3333 		if (tmp_bss)
3334 			iface->bss = tmp_bss;
3335 		if (tmp_conf) {
3336 			iface->conf->bss = tmp_conf;
3337 			iface->conf->last_bss = tmp_conf[0];
3338 		}
3339 		if (tmp_bss == NULL || tmp_conf == NULL) {
3340 			hostapd_config_free(conf);
3341 			return NULL;
3342 		}
3343 		bss = iface->conf->bss[iface->conf->num_bss] = conf->bss[0];
3344 		iface->conf->num_bss++;
3345 
3346 		hapd = hostapd_alloc_bss_data(iface, iface->conf, bss);
3347 		if (hapd == NULL) {
3348 			iface->conf->num_bss--;
3349 			hostapd_config_free(conf);
3350 			return NULL;
3351 		}
3352 		iface->conf->last_bss = bss;
3353 		iface->bss[iface->num_bss] = hapd;
3354 		hapd->msg_ctx = hapd;
3355 		hostapd_bss_setup_multi_link(hapd, interfaces);
3356 
3357 
3358 		bss_idx = iface->num_bss++;
3359 		conf->num_bss--;
3360 		conf->bss[0] = NULL;
3361 		hostapd_config_free(conf);
3362 	} else {
3363 		/* Add a new iface with the first BSS */
3364 		new_iface = iface = hostapd_init(interfaces, config_fname);
3365 		if (!iface)
3366 			return NULL;
3367 		os_strlcpy(iface->phy, phy, sizeof(iface->phy));
3368 		iface->interfaces = interfaces;
3369 		bss_idx = 0;
3370 	}
3371 
3372 	for (k = 0; k < debug; k++) {
3373 		if (iface->bss[bss_idx]->conf->logger_stdout_level > 0)
3374 			iface->bss[bss_idx]->conf->logger_stdout_level--;
3375 	}
3376 
3377 	if (iface->conf->bss[bss_idx]->iface[0] == '\0' &&
3378 	    !hostapd_drv_none(iface->bss[bss_idx])) {
3379 		wpa_printf(MSG_ERROR, "Interface name not specified in %s",
3380 			   config_fname);
3381 		if (new_iface)
3382 			hostapd_interface_deinit_free(new_iface);
3383 		return NULL;
3384 	}
3385 
3386 	return iface;
3387 }
3388 
3389 
hostapd_cleanup_driver(const struct wpa_driver_ops * driver,void * drv_priv,struct hostapd_iface * iface)3390 static void hostapd_cleanup_driver(const struct wpa_driver_ops *driver,
3391 				   void *drv_priv, struct hostapd_iface *iface)
3392 {
3393 	if (!driver || !driver->hapd_deinit || !drv_priv)
3394 		return;
3395 
3396 #ifdef CONFIG_IEEE80211BE
3397 	/* In case of non-ML operation, de-init. But if ML operation exist,
3398 	 * even if that's the last BSS in the interface, the driver (drv) could
3399 	 * be in use for a different AP MLD. Hence, need to check if drv is
3400 	 * still being used by some other BSS before de-initiallizing. */
3401 	if (!iface->bss[0]->conf->mld_ap) {
3402 		driver->hapd_deinit(drv_priv);
3403 	} else if (hostapd_mld_is_first_bss(iface->bss[0]) &&
3404 		   driver->is_drv_shared &&
3405 		   !driver->is_drv_shared(drv_priv, iface->bss[0])) {
3406 		driver->hapd_deinit(drv_priv);
3407 	} else if (hostapd_if_link_remove(iface->bss[0],
3408 					  WPA_IF_AP_BSS,
3409 					  iface->bss[0]->conf->iface,
3410 					  iface->bss[0]->mld_link_id)) {
3411 		wpa_printf(MSG_WARNING, "Failed to remove BSS interface %s",
3412 			   iface->bss[0]->conf->iface);
3413 	}
3414 #else /* CONFIG_IEEE80211BE */
3415 	driver->hapd_deinit(drv_priv);
3416 #endif /* CONFIG_IEEE80211BE */
3417 	iface->bss[0]->drv_priv = NULL;
3418 }
3419 
3420 
hostapd_interface_deinit_free(struct hostapd_iface * iface)3421 void hostapd_interface_deinit_free(struct hostapd_iface *iface)
3422 {
3423 	const struct wpa_driver_ops *driver;
3424 	void *drv_priv;
3425 
3426 	wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
3427 	if (iface == NULL)
3428 		return;
3429 	wpa_printf(MSG_DEBUG, "%s: num_bss=%u conf->num_bss=%u",
3430 		   __func__, (unsigned int) iface->num_bss,
3431 		   (unsigned int) iface->conf->num_bss);
3432 	driver = iface->bss[0]->driver;
3433 	drv_priv = iface->bss[0]->drv_priv;
3434 	hostapd_interface_deinit(iface);
3435 	wpa_printf(MSG_DEBUG, "%s: driver=%p drv_priv=%p -> hapd_deinit",
3436 		   __func__, driver, drv_priv);
3437 	hostapd_cleanup_driver(driver, drv_priv, iface);
3438 	hostapd_interface_free(iface);
3439 }
3440 
3441 
hostapd_deinit_driver(const struct wpa_driver_ops * driver,void * drv_priv,struct hostapd_iface * hapd_iface)3442 static void hostapd_deinit_driver(const struct wpa_driver_ops *driver,
3443 				  void *drv_priv,
3444 				  struct hostapd_iface *hapd_iface)
3445 {
3446 	size_t j;
3447 
3448 	wpa_printf(MSG_DEBUG, "%s: driver=%p drv_priv=%p -> hapd_deinit",
3449 		   __func__, driver, drv_priv);
3450 
3451 	hostapd_cleanup_driver(driver, drv_priv, hapd_iface);
3452 
3453 	if (driver && driver->hapd_deinit && drv_priv) {
3454 		for (j = 0; j < hapd_iface->num_bss; j++) {
3455 			wpa_printf(MSG_DEBUG, "%s:bss[%d]->drv_priv=%p",
3456 				   __func__, (int) j,
3457 				   hapd_iface->bss[j]->drv_priv);
3458 			if (hapd_iface->bss[j]->drv_priv == drv_priv) {
3459 				hapd_iface->bss[j]->drv_priv = NULL;
3460 				hapd_iface->extended_capa = NULL;
3461 				hapd_iface->extended_capa_mask = NULL;
3462 				hapd_iface->extended_capa_len = 0;
3463 			}
3464 		}
3465 	}
3466 }
3467 
3468 
hostapd_refresh_all_iface_beacons(struct hostapd_iface * hapd_iface)3469 static void hostapd_refresh_all_iface_beacons(struct hostapd_iface *hapd_iface)
3470 {
3471 	size_t j;
3472 
3473 	if (!hapd_iface->interfaces || hapd_iface->interfaces->count <= 1)
3474 		return;
3475 
3476 	for (j = 0; j < hapd_iface->interfaces->count; j++) {
3477 		if (hapd_iface->interfaces->iface[j] == hapd_iface)
3478 			continue;
3479 
3480 		ieee802_11_update_beacons(hapd_iface->interfaces->iface[j]);
3481 	}
3482 }
3483 
3484 
hostapd_enable_iface(struct hostapd_iface * hapd_iface)3485 int hostapd_enable_iface(struct hostapd_iface *hapd_iface)
3486 {
3487 	size_t j;
3488 
3489 	if (!hapd_iface)
3490 		return -1;
3491 
3492 	if (hapd_iface->enable_iface_cb)
3493 		return hapd_iface->enable_iface_cb(hapd_iface);
3494 
3495 	if (hapd_iface->bss[0]->drv_priv != NULL) {
3496 		wpa_printf(MSG_ERROR, "Interface %s already enabled",
3497 			   hapd_iface->conf->bss[0]->iface);
3498 		return -1;
3499 	}
3500 
3501 	wpa_printf(MSG_DEBUG, "Enable interface %s",
3502 		   hapd_iface->conf->bss[0]->iface);
3503 
3504 	for (j = 0; j < hapd_iface->num_bss; j++)
3505 		hostapd_set_security_params(hapd_iface->conf->bss[j], 1);
3506 	if (hostapd_config_check(hapd_iface->conf, 1) < 0) {
3507 		wpa_printf(MSG_INFO, "Invalid configuration - cannot enable");
3508 		return -1;
3509 	}
3510 
3511 	if (hapd_iface->interfaces == NULL ||
3512 	    hapd_iface->interfaces->driver_init == NULL ||
3513 	    hapd_iface->interfaces->driver_init(hapd_iface))
3514 		return -1;
3515 
3516 	if (hostapd_setup_interface(hapd_iface)) {
3517 		hostapd_deinit_driver(hapd_iface->bss[0]->driver,
3518 				      hapd_iface->bss[0]->drv_priv,
3519 				      hapd_iface);
3520 		return -1;
3521 	}
3522 
3523 	hostapd_refresh_all_iface_beacons(hapd_iface);
3524 
3525 	return 0;
3526 }
3527 
3528 
hostapd_reload_iface(struct hostapd_iface * hapd_iface)3529 int hostapd_reload_iface(struct hostapd_iface *hapd_iface)
3530 {
3531 	size_t j;
3532 
3533 	wpa_printf(MSG_DEBUG, "Reload interface %s",
3534 		   hapd_iface->conf->bss[0]->iface);
3535 	for (j = 0; j < hapd_iface->num_bss; j++)
3536 		hostapd_set_security_params(hapd_iface->conf->bss[j], 1);
3537 	if (hostapd_config_check(hapd_iface->conf, 1) < 0) {
3538 		wpa_printf(MSG_ERROR, "Updated configuration is invalid");
3539 		return -1;
3540 	}
3541 	hostapd_clear_old(hapd_iface);
3542 	for (j = 0; j < hapd_iface->num_bss; j++)
3543 		hostapd_reload_bss(hapd_iface->bss[j]);
3544 
3545 	return 0;
3546 }
3547 
3548 
hostapd_reload_bss_only(struct hostapd_data * bss)3549 int hostapd_reload_bss_only(struct hostapd_data *bss)
3550 {
3551 
3552 	wpa_printf(MSG_DEBUG, "Reload BSS %s", bss->conf->iface);
3553 	hostapd_set_security_params(bss->conf, 1);
3554 	if (hostapd_config_check(bss->iconf, 1) < 0) {
3555 		wpa_printf(MSG_ERROR, "Updated BSS configuration is invalid");
3556 		return -1;
3557 	}
3558 	hostapd_clear_old_bss(bss);
3559 	hostapd_reload_bss(bss);
3560 	return 0;
3561 }
3562 
3563 
hostapd_disable_iface(struct hostapd_iface * hapd_iface)3564 int hostapd_disable_iface(struct hostapd_iface *hapd_iface)
3565 {
3566 	size_t j;
3567 	const struct wpa_driver_ops *driver;
3568 	void *drv_priv;
3569 
3570 	if (hapd_iface == NULL)
3571 		return -1;
3572 
3573 	if (hapd_iface->disable_iface_cb)
3574 		return hapd_iface->disable_iface_cb(hapd_iface);
3575 
3576 	if (hapd_iface->bss[0]->drv_priv == NULL) {
3577 		wpa_printf(MSG_INFO, "Interface %s already disabled",
3578 			   hapd_iface->conf->bss[0]->iface);
3579 		return -1;
3580 	}
3581 
3582 	wpa_msg(hapd_iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
3583 	driver = hapd_iface->bss[0]->driver;
3584 	drv_priv = hapd_iface->bss[0]->drv_priv;
3585 
3586 	hapd_iface->driver_ap_teardown =
3587 		!!(hapd_iface->drv_flags &
3588 		   WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
3589 
3590 #ifdef NEED_AP_MLME
3591 	for (j = 0; j < hapd_iface->num_bss; j++)
3592 		hostapd_cleanup_cs_params(hapd_iface->bss[j]);
3593 #endif /* NEED_AP_MLME */
3594 
3595 	/* same as hostapd_interface_deinit without deinitializing ctrl-iface */
3596 	for (j = 0; j < hapd_iface->num_bss; j++) {
3597 		struct hostapd_data *hapd = hapd_iface->bss[j];
3598 		hostapd_bss_deinit_no_free(hapd);
3599 		hostapd_bss_link_deinit(hapd);
3600 		hostapd_free_hapd_data(hapd);
3601 	}
3602 
3603 	hostapd_deinit_driver(driver, drv_priv, hapd_iface);
3604 
3605 	/* From hostapd_cleanup_iface: These were initialized in
3606 	 * hostapd_setup_interface and hostapd_setup_interface_complete
3607 	 */
3608 	hostapd_cleanup_iface_partial(hapd_iface);
3609 
3610 	wpa_printf(MSG_DEBUG, "Interface %s disabled",
3611 		   hapd_iface->bss[0]->conf->iface);
3612 	hostapd_set_state(hapd_iface, HAPD_IFACE_DISABLED);
3613 	hostapd_refresh_all_iface_beacons(hapd_iface);
3614 	return 0;
3615 }
3616 
3617 
3618 static struct hostapd_iface *
hostapd_iface_alloc(struct hapd_interfaces * interfaces)3619 hostapd_iface_alloc(struct hapd_interfaces *interfaces)
3620 {
3621 	struct hostapd_iface **iface, *hapd_iface;
3622 
3623 	iface = os_realloc_array(interfaces->iface, interfaces->count + 1,
3624 				 sizeof(struct hostapd_iface *));
3625 	if (iface == NULL)
3626 		return NULL;
3627 	interfaces->iface = iface;
3628 	hapd_iface = interfaces->iface[interfaces->count] =
3629 		hostapd_alloc_iface();
3630 	if (hapd_iface == NULL) {
3631 		wpa_printf(MSG_ERROR, "%s: Failed to allocate memory for "
3632 			   "the interface", __func__);
3633 		return NULL;
3634 	}
3635 	interfaces->count++;
3636 	hapd_iface->interfaces = interfaces;
3637 
3638 	return hapd_iface;
3639 }
3640 
3641 
3642 static struct hostapd_config *
hostapd_config_alloc(struct hapd_interfaces * interfaces,const char * ifname,const char * ctrl_iface,const char * driver)3643 hostapd_config_alloc(struct hapd_interfaces *interfaces, const char *ifname,
3644 		     const char *ctrl_iface, const char *driver)
3645 {
3646 	struct hostapd_bss_config *bss;
3647 	struct hostapd_config *conf;
3648 
3649 	/* Allocates memory for bss and conf */
3650 	conf = hostapd_config_defaults();
3651 	if (conf == NULL) {
3652 		 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory for "
3653 				"configuration", __func__);
3654 		 return NULL;
3655 	}
3656 
3657 	if (driver) {
3658 		int j;
3659 
3660 		for (j = 0; wpa_drivers[j]; j++) {
3661 			if (os_strcmp(driver, wpa_drivers[j]->name) == 0) {
3662 				conf->driver = wpa_drivers[j];
3663 				goto skip;
3664 			}
3665 		}
3666 
3667 		wpa_printf(MSG_ERROR,
3668 			   "Invalid/unknown driver '%s' - registering the default driver",
3669 			   driver);
3670 	}
3671 
3672 	conf->driver = wpa_drivers[0];
3673 	if (conf->driver == NULL) {
3674 		wpa_printf(MSG_ERROR, "No driver wrappers registered!");
3675 		hostapd_config_free(conf);
3676 		return NULL;
3677 	}
3678 
3679 skip:
3680 	bss = conf->last_bss = conf->bss[0];
3681 
3682 	os_strlcpy(bss->iface, ifname, sizeof(bss->iface));
3683 	bss->ctrl_interface = os_strdup(ctrl_iface);
3684 	if (bss->ctrl_interface == NULL) {
3685 		hostapd_config_free(conf);
3686 		return NULL;
3687 	}
3688 
3689 	/* Reading configuration file skipped, will be done in SET!
3690 	 * From reading the configuration till the end has to be done in
3691 	 * SET
3692 	 */
3693 	return conf;
3694 }
3695 
3696 
hostapd_data_alloc(struct hostapd_iface * hapd_iface,struct hostapd_config * conf)3697 static int hostapd_data_alloc(struct hostapd_iface *hapd_iface,
3698 			      struct hostapd_config *conf)
3699 {
3700 	size_t i;
3701 	struct hostapd_data *hapd;
3702 
3703 	hapd_iface->bss = os_calloc(conf->num_bss,
3704 				    sizeof(struct hostapd_data *));
3705 	if (hapd_iface->bss == NULL)
3706 		return -1;
3707 
3708 	for (i = 0; i < conf->num_bss; i++) {
3709 		hapd = hapd_iface->bss[i] =
3710 			hostapd_alloc_bss_data(hapd_iface, conf, conf->bss[i]);
3711 		if (hapd == NULL) {
3712 			while (i > 0) {
3713 				i--;
3714 				os_free(hapd_iface->bss[i]);
3715 				hapd_iface->bss[i] = NULL;
3716 			}
3717 			os_free(hapd_iface->bss);
3718 			hapd_iface->bss = NULL;
3719 			return -1;
3720 		}
3721 		hapd->msg_ctx = hapd;
3722 		hostapd_bss_setup_multi_link(hapd, hapd_iface->interfaces);
3723 	}
3724 
3725 	hapd_iface->conf = conf;
3726 	hapd_iface->num_bss = conf->num_bss;
3727 
3728 	return 0;
3729 }
3730 
3731 
hostapd_add_iface(struct hapd_interfaces * interfaces,char * buf)3732 int hostapd_add_iface(struct hapd_interfaces *interfaces, char *buf)
3733 {
3734 	struct hostapd_config *conf = NULL;
3735 	struct hostapd_iface *hapd_iface = NULL, *new_iface = NULL;
3736 	struct hostapd_data *hapd;
3737 	char *ptr;
3738 	size_t i, j;
3739 	const char *conf_file = NULL, *phy_name = NULL;
3740 
3741 	if (os_strncmp(buf, "bss_config=", 11) == 0) {
3742 		char *pos;
3743 		phy_name = buf + 11;
3744 		pos = os_strchr(phy_name, ':');
3745 		if (!pos)
3746 			return -1;
3747 		*pos++ = '\0';
3748 		conf_file = pos;
3749 		if (!os_strlen(conf_file))
3750 			return -1;
3751 
3752 		hapd_iface = hostapd_interface_init_bss(interfaces, phy_name,
3753 							conf_file, 0);
3754 		if (!hapd_iface)
3755 			return -1;
3756 		for (j = 0; j < interfaces->count; j++) {
3757 			if (interfaces->iface[j] == hapd_iface)
3758 				break;
3759 		}
3760 		if (j == interfaces->count) {
3761 			struct hostapd_iface **tmp;
3762 			tmp = os_realloc_array(interfaces->iface,
3763 					       interfaces->count + 1,
3764 					       sizeof(struct hostapd_iface *));
3765 			if (!tmp) {
3766 				hostapd_interface_deinit_free(hapd_iface);
3767 				return -1;
3768 			}
3769 			interfaces->iface = tmp;
3770 			interfaces->iface[interfaces->count++] = hapd_iface;
3771 			new_iface = hapd_iface;
3772 		}
3773 
3774 		if (new_iface) {
3775 			if (interfaces->driver_init(hapd_iface))
3776 				goto fail;
3777 
3778 			if (hostapd_setup_interface(hapd_iface)) {
3779 				hostapd_deinit_driver(
3780 					hapd_iface->bss[0]->driver,
3781 					hapd_iface->bss[0]->drv_priv,
3782 					hapd_iface);
3783 				goto fail;
3784 			}
3785 		} else {
3786 			/* Assign new BSS with bss[0]'s driver info */
3787 			hapd = hapd_iface->bss[hapd_iface->num_bss - 1];
3788 			hapd->driver = hapd_iface->bss[0]->driver;
3789 			hapd->drv_priv = hapd_iface->bss[0]->drv_priv;
3790 			os_memcpy(hapd->own_addr, hapd_iface->bss[0]->own_addr,
3791 				  ETH_ALEN);
3792 
3793 			if (start_ctrl_iface_bss(hapd) < 0 ||
3794 			    (hapd_iface->state == HAPD_IFACE_ENABLED &&
3795 			     hostapd_setup_bss(hapd, -1, true))) {
3796 				hostapd_bss_link_deinit(hapd);
3797 				hostapd_cleanup(hapd);
3798 				hapd_iface->bss[hapd_iface->num_bss - 1] = NULL;
3799 				hapd_iface->conf->num_bss--;
3800 				hapd_iface->num_bss--;
3801 				wpa_printf(MSG_DEBUG, "%s: free hapd %p %s",
3802 					   __func__, hapd, hapd->conf->iface);
3803 				hostapd_config_free_bss(hapd->conf);
3804 				hapd->conf = NULL;
3805 #ifdef CONFIG_IEEE80211BE
3806 				hostapd_mld_ref_dec(hapd->mld);
3807 #endif /* CONFIG_IEEE80211BE */
3808 				os_free(hapd);
3809 				return -1;
3810 			}
3811 		}
3812 		hostapd_owe_update_trans(hapd_iface);
3813 		return 0;
3814 	}
3815 
3816 	ptr = os_strchr(buf, ' ');
3817 	if (ptr == NULL)
3818 		return -1;
3819 	*ptr++ = '\0';
3820 
3821 	if (os_strncmp(ptr, "config=", 7) == 0)
3822 		conf_file = ptr + 7;
3823 
3824 	for (i = 0; i < interfaces->count; i++) {
3825 		bool mld_ap = false;
3826 
3827 #ifdef CONFIG_IEEE80211BE
3828 		mld_ap = interfaces->iface[i]->conf->bss[0]->mld_ap;
3829 #endif /* CONFIG_IEEE80211BE */
3830 
3831 		if (!os_strcmp(interfaces->iface[i]->conf->bss[0]->iface,
3832 			       buf) && !mld_ap) {
3833 			wpa_printf(MSG_INFO, "Cannot add interface - it "
3834 				   "already exists");
3835 			return -1;
3836 		}
3837 	}
3838 
3839 	hapd_iface = hostapd_iface_alloc(interfaces);
3840 	if (hapd_iface == NULL) {
3841 		wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
3842 			   "for interface", __func__);
3843 		goto fail;
3844 	}
3845 	new_iface = hapd_iface;
3846 
3847 	if (conf_file && interfaces->config_read_cb) {
3848 		conf = interfaces->config_read_cb(conf_file);
3849 		if (conf && conf->bss)
3850 			os_strlcpy(conf->bss[0]->iface, buf,
3851 				   sizeof(conf->bss[0]->iface));
3852 	} else {
3853 		char *driver = os_strchr(ptr, ' ');
3854 
3855 		if (driver)
3856 			*driver++ = '\0';
3857 		conf = hostapd_config_alloc(interfaces, buf, ptr, driver);
3858 	}
3859 
3860 	if (conf == NULL || conf->bss == NULL) {
3861 		wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
3862 			   "for configuration", __func__);
3863 		goto fail;
3864 	}
3865 
3866 	if (hostapd_data_alloc(hapd_iface, conf) < 0) {
3867 		wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
3868 			   "for hostapd", __func__);
3869 		goto fail;
3870 	}
3871 	conf = NULL;
3872 
3873 	if (start_ctrl_iface(hapd_iface) < 0)
3874 		goto fail;
3875 
3876 	wpa_printf(MSG_INFO, "Add interface '%s'",
3877 		   hapd_iface->conf->bss[0]->iface);
3878 
3879 	return 0;
3880 
3881 fail:
3882 	if (conf)
3883 		hostapd_config_free(conf);
3884 	if (hapd_iface) {
3885 		if (hapd_iface->bss) {
3886 			for (i = 0; i < hapd_iface->num_bss; i++) {
3887 				hapd = hapd_iface->bss[i];
3888 				if (!hapd)
3889 					continue;
3890 				if (hapd_iface->interfaces &&
3891 				    hapd_iface->interfaces->ctrl_iface_deinit)
3892 					hapd_iface->interfaces->
3893 						ctrl_iface_deinit(hapd);
3894 				wpa_printf(MSG_DEBUG, "%s: free hapd %p (%s)",
3895 					   __func__, hapd_iface->bss[i],
3896 					   hapd->conf->iface);
3897 				hostapd_bss_link_deinit(hapd);
3898 				hostapd_cleanup(hapd);
3899 #ifdef CONFIG_IEEE80211BE
3900 				hostapd_mld_ref_dec(hapd->mld);
3901 #endif /* CONFIG_IEEE80211BE */
3902 				os_free(hapd);
3903 				hapd_iface->bss[i] = NULL;
3904 			}
3905 			os_free(hapd_iface->bss);
3906 			hapd_iface->bss = NULL;
3907 		}
3908 		if (new_iface) {
3909 			interfaces->count--;
3910 			interfaces->iface[interfaces->count] = NULL;
3911 			hostapd_cleanup_unused_mlds(interfaces);
3912 		}
3913 		hostapd_cleanup_iface(hapd_iface);
3914 	}
3915 	return -1;
3916 }
3917 
3918 
hostapd_remove_bss(struct hostapd_iface * iface,unsigned int idx)3919 static int hostapd_remove_bss(struct hostapd_iface *iface, unsigned int idx)
3920 {
3921 	size_t i;
3922 
3923 	wpa_printf(MSG_INFO, "Remove BSS '%s'", iface->conf->bss[idx]->iface);
3924 
3925 	/* Remove hostapd_data only if it has already been initialized */
3926 	if (idx < iface->num_bss) {
3927 		struct hostapd_data *hapd = iface->bss[idx];
3928 
3929 		hostapd_bss_deinit(hapd);
3930 		wpa_printf(MSG_DEBUG, "%s: free hapd %p (%s)",
3931 			   __func__, hapd, hapd->conf->iface);
3932 		hostapd_config_free_bss(hapd->conf);
3933 		hapd->conf = NULL;
3934 #ifdef CONFIG_IEEE80211BE
3935 		hostapd_mld_ref_dec(hapd->mld);
3936 #endif /* CONFIG_IEEE80211BE */
3937 		os_free(hapd);
3938 
3939 		iface->num_bss--;
3940 
3941 		for (i = idx; i < iface->num_bss; i++)
3942 			iface->bss[i] = iface->bss[i + 1];
3943 	} else {
3944 		hostapd_config_free_bss(iface->conf->bss[idx]);
3945 		iface->conf->bss[idx] = NULL;
3946 	}
3947 
3948 	iface->conf->num_bss--;
3949 	for (i = idx; i < iface->conf->num_bss; i++)
3950 		iface->conf->bss[i] = iface->conf->bss[i + 1];
3951 
3952 	return 0;
3953 }
3954 
3955 
hostapd_remove_iface(struct hapd_interfaces * interfaces,char * buf)3956 int hostapd_remove_iface(struct hapd_interfaces *interfaces, char *buf)
3957 {
3958 	struct hostapd_iface *hapd_iface;
3959 	size_t i, j, k = 0;
3960 
3961 	for (i = 0; i < interfaces->count; i++) {
3962 		hapd_iface = interfaces->iface[i];
3963 		if (hapd_iface == NULL)
3964 			return -1;
3965 		if (!os_strcmp(hapd_iface->conf->bss[0]->iface, buf)) {
3966 			wpa_printf(MSG_INFO, "Remove interface '%s'", buf);
3967 			hapd_iface->driver_ap_teardown =
3968 				!!(hapd_iface->drv_flags &
3969 				   WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
3970 
3971 			hostapd_interface_deinit_free(hapd_iface);
3972 			k = i;
3973 			while (k < (interfaces->count - 1)) {
3974 				interfaces->iface[k] =
3975 					interfaces->iface[k + 1];
3976 				k++;
3977 			}
3978 			interfaces->count--;
3979 			hostapd_cleanup_unused_mlds(interfaces);
3980 
3981 			return 0;
3982 		}
3983 
3984 		for (j = 0; j < hapd_iface->conf->num_bss; j++) {
3985 			if (!os_strcmp(hapd_iface->conf->bss[j]->iface, buf)) {
3986 				hapd_iface->driver_ap_teardown =
3987 					!(hapd_iface->drv_flags &
3988 					  WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
3989 				return hostapd_remove_bss(hapd_iface, j);
3990 			}
3991 		}
3992 	}
3993 	return -1;
3994 }
3995 
3996 
3997 /**
3998  * hostapd_new_assoc_sta - Notify that a new station associated with the AP
3999  * @hapd: Pointer to BSS data
4000  * @sta: Pointer to the associated STA data
4001  * @reassoc: 1 to indicate this was a re-association; 0 = first association
4002  *
4003  * This function will be called whenever a station associates with the AP. It
4004  * can be called from ieee802_11.c for drivers that export MLME to hostapd and
4005  * from drv_callbacks.c based on driver events for drivers that take care of
4006  * management frames (IEEE 802.11 authentication and association) internally.
4007  */
hostapd_new_assoc_sta(struct hostapd_data * hapd,struct sta_info * sta,int reassoc)4008 void hostapd_new_assoc_sta(struct hostapd_data *hapd, struct sta_info *sta,
4009 			   int reassoc)
4010 {
4011 	if (hapd->tkip_countermeasures) {
4012 		hostapd_drv_sta_deauth(hapd, sta->addr,
4013 				       WLAN_REASON_MICHAEL_MIC_FAILURE);
4014 		return;
4015 	}
4016 
4017 #ifdef CONFIG_IEEE80211BE
4018 	if (ap_sta_is_mld(hapd, sta) &&
4019 	    sta->mld_assoc_link_id != hapd->mld_link_id)
4020 		return;
4021 #endif /* CONFIG_IEEE80211BE */
4022 
4023 	ap_sta_clear_disconnect_timeouts(hapd, sta);
4024 	sta->post_csa_sa_query = 0;
4025 
4026 #ifdef CONFIG_P2P
4027 	if (sta->p2p_ie == NULL && !sta->no_p2p_set) {
4028 		sta->no_p2p_set = 1;
4029 		hapd->num_sta_no_p2p++;
4030 		if (hapd->num_sta_no_p2p == 1)
4031 			hostapd_p2p_non_p2p_sta_connected(hapd);
4032 	}
4033 #endif /* CONFIG_P2P */
4034 
4035 	airtime_policy_new_sta(hapd, sta);
4036 
4037 	/* Start accounting here, if IEEE 802.1X and WPA are not used.
4038 	 * IEEE 802.1X/WPA code will start accounting after the station has
4039 	 * been authorized. */
4040 	if (!hapd->conf->ieee802_1x && !hapd->conf->wpa && !hapd->conf->osen) {
4041 		ap_sta_set_authorized(hapd, sta, 1);
4042 		os_get_reltime(&sta->connected_time);
4043 		accounting_sta_start(hapd, sta);
4044 	}
4045 
4046 	/* Start IEEE 802.1X authentication process for new stations */
4047 	ieee802_1x_new_station(hapd, sta);
4048 	if (reassoc) {
4049 		if (sta->auth_alg != WLAN_AUTH_FT &&
4050 		    sta->auth_alg != WLAN_AUTH_FILS_SK &&
4051 		    sta->auth_alg != WLAN_AUTH_FILS_SK_PFS &&
4052 		    sta->auth_alg != WLAN_AUTH_FILS_PK &&
4053 		    !(sta->flags & (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS)))
4054 			wpa_auth_sm_event(sta->wpa_sm, WPA_REAUTH);
4055 	} else if (!(hapd->iface->drv_flags2 &
4056 		     WPA_DRIVER_FLAGS2_4WAY_HANDSHAKE_AP_PSK)) {
4057 		/* The 4-way handshake offloaded case will have this handled
4058 		 * based on the port authorized event. */
4059 		wpa_auth_sta_associated(hapd->wpa_auth, sta->wpa_sm);
4060 	}
4061 
4062 	if (hapd->iface->drv_flags & WPA_DRIVER_FLAGS_WIRED) {
4063 		if (eloop_cancel_timeout(ap_handle_timer, hapd, sta) > 0) {
4064 			wpa_printf(MSG_DEBUG,
4065 				   "%s: %s: canceled wired ap_handle_timer timeout for "
4066 				   MACSTR,
4067 				   hapd->conf->iface, __func__,
4068 				   MAC2STR(sta->addr));
4069 		}
4070 	} else if (!(hapd->iface->drv_flags &
4071 		     WPA_DRIVER_FLAGS_INACTIVITY_TIMER)) {
4072 		wpa_printf(MSG_DEBUG,
4073 			   "%s: %s: reschedule ap_handle_timer timeout for "
4074 			   MACSTR " (%d seconds - ap_max_inactivity)",
4075 			   hapd->conf->iface, __func__, MAC2STR(sta->addr),
4076 			   hapd->conf->ap_max_inactivity);
4077 		eloop_cancel_timeout(ap_handle_timer, hapd, sta);
4078 		eloop_register_timeout(hapd->conf->ap_max_inactivity, 0,
4079 				       ap_handle_timer, hapd, sta);
4080 	}
4081 
4082 #ifdef CONFIG_MACSEC
4083 	if (hapd->conf->wpa_key_mgmt == WPA_KEY_MGMT_NONE &&
4084 	    hapd->conf->mka_psk_set)
4085 		ieee802_1x_create_preshared_mka_hapd(hapd, sta);
4086 	else
4087 		ieee802_1x_alloc_kay_sm_hapd(hapd, sta);
4088 #endif /* CONFIG_MACSEC */
4089 }
4090 
4091 
hostapd_state_text(enum hostapd_iface_state s)4092 const char * hostapd_state_text(enum hostapd_iface_state s)
4093 {
4094 	switch (s) {
4095 	case HAPD_IFACE_UNINITIALIZED:
4096 		return "UNINITIALIZED";
4097 	case HAPD_IFACE_DISABLED:
4098 		return "DISABLED";
4099 	case HAPD_IFACE_COUNTRY_UPDATE:
4100 		return "COUNTRY_UPDATE";
4101 	case HAPD_IFACE_ACS:
4102 		return "ACS";
4103 	case HAPD_IFACE_HT_SCAN:
4104 		return "HT_SCAN";
4105 	case HAPD_IFACE_DFS:
4106 		return "DFS";
4107 	case HAPD_IFACE_ENABLED:
4108 		return "ENABLED";
4109 	case HAPD_IFACE_NO_IR:
4110 		return "NO_IR";
4111 	}
4112 
4113 	return "UNKNOWN";
4114 }
4115 
4116 
hostapd_set_state(struct hostapd_iface * iface,enum hostapd_iface_state s)4117 void hostapd_set_state(struct hostapd_iface *iface, enum hostapd_iface_state s)
4118 {
4119 	wpa_printf(MSG_INFO, "%s: interface state %s->%s",
4120 		   iface->conf ? iface->conf->bss[0]->iface : "N/A",
4121 		   hostapd_state_text(iface->state), hostapd_state_text(s));
4122 	iface->state = s;
4123 }
4124 
4125 
hostapd_csa_in_progress(struct hostapd_iface * iface)4126 int hostapd_csa_in_progress(struct hostapd_iface *iface)
4127 {
4128 	unsigned int i;
4129 
4130 	for (i = 0; i < iface->num_bss; i++)
4131 		if (iface->bss[i]->csa_in_progress)
4132 			return 1;
4133 	return 0;
4134 }
4135 
4136 
4137 #ifdef NEED_AP_MLME
4138 
free_beacon_data(struct beacon_data * beacon)4139 void free_beacon_data(struct beacon_data *beacon)
4140 {
4141 	os_free(beacon->head);
4142 	beacon->head = NULL;
4143 	os_free(beacon->tail);
4144 	beacon->tail = NULL;
4145 	os_free(beacon->probe_resp);
4146 	beacon->probe_resp = NULL;
4147 	os_free(beacon->beacon_ies);
4148 	beacon->beacon_ies = NULL;
4149 	os_free(beacon->proberesp_ies);
4150 	beacon->proberesp_ies = NULL;
4151 	os_free(beacon->assocresp_ies);
4152 	beacon->assocresp_ies = NULL;
4153 }
4154 
4155 
hostapd_build_beacon_data(struct hostapd_data * hapd,struct beacon_data * beacon)4156 static int hostapd_build_beacon_data(struct hostapd_data *hapd,
4157 				     struct beacon_data *beacon)
4158 {
4159 	struct wpabuf *beacon_extra, *proberesp_extra, *assocresp_extra;
4160 	struct wpa_driver_ap_params params;
4161 	int ret;
4162 
4163 	os_memset(beacon, 0, sizeof(*beacon));
4164 	ret = ieee802_11_build_ap_params(hapd, &params);
4165 	if (ret < 0)
4166 		return ret;
4167 
4168 	ret = hostapd_build_ap_extra_ies(hapd, &beacon_extra,
4169 					 &proberesp_extra,
4170 					 &assocresp_extra);
4171 	if (ret)
4172 		goto free_ap_params;
4173 
4174 	ret = -1;
4175 	beacon->head = os_memdup(params.head, params.head_len);
4176 	if (!beacon->head)
4177 		goto free_ap_extra_ies;
4178 
4179 	beacon->head_len = params.head_len;
4180 
4181 	beacon->tail = os_memdup(params.tail, params.tail_len);
4182 	if (!beacon->tail)
4183 		goto free_beacon;
4184 
4185 	beacon->tail_len = params.tail_len;
4186 
4187 	if (params.proberesp != NULL) {
4188 		beacon->probe_resp = os_memdup(params.proberesp,
4189 					       params.proberesp_len);
4190 		if (!beacon->probe_resp)
4191 			goto free_beacon;
4192 
4193 		beacon->probe_resp_len = params.proberesp_len;
4194 	}
4195 
4196 	/* copy the extra ies */
4197 	if (beacon_extra) {
4198 		beacon->beacon_ies = os_memdup(beacon_extra->buf,
4199 					       wpabuf_len(beacon_extra));
4200 		if (!beacon->beacon_ies)
4201 			goto free_beacon;
4202 
4203 		beacon->beacon_ies_len = wpabuf_len(beacon_extra);
4204 	}
4205 
4206 	if (proberesp_extra) {
4207 		beacon->proberesp_ies = os_memdup(proberesp_extra->buf,
4208 						  wpabuf_len(proberesp_extra));
4209 		if (!beacon->proberesp_ies)
4210 			goto free_beacon;
4211 
4212 		beacon->proberesp_ies_len = wpabuf_len(proberesp_extra);
4213 	}
4214 
4215 	if (assocresp_extra) {
4216 		beacon->assocresp_ies = os_memdup(assocresp_extra->buf,
4217 						  wpabuf_len(assocresp_extra));
4218 		if (!beacon->assocresp_ies)
4219 			goto free_beacon;
4220 
4221 		beacon->assocresp_ies_len = wpabuf_len(assocresp_extra);
4222 	}
4223 
4224 	ret = 0;
4225 free_beacon:
4226 	/* if the function fails, the caller should not free beacon data */
4227 	if (ret)
4228 		free_beacon_data(beacon);
4229 
4230 free_ap_extra_ies:
4231 	hostapd_free_ap_extra_ies(hapd, beacon_extra, proberesp_extra,
4232 				  assocresp_extra);
4233 free_ap_params:
4234 	ieee802_11_free_ap_params(&params);
4235 	return ret;
4236 }
4237 
4238 
4239 /*
4240  * TODO: This flow currently supports only changing channel and width within
4241  * the same hw_mode. Any other changes to MAC parameters or provided settings
4242  * are not supported.
4243  */
hostapd_change_config_freq(struct hostapd_data * hapd,struct hostapd_config * conf,struct hostapd_freq_params * params,struct hostapd_freq_params * old_params)4244 static int hostapd_change_config_freq(struct hostapd_data *hapd,
4245 				      struct hostapd_config *conf,
4246 				      struct hostapd_freq_params *params,
4247 				      struct hostapd_freq_params *old_params)
4248 {
4249 	int channel;
4250 	u8 seg0 = 0, seg1 = 0;
4251 	struct hostapd_hw_modes *mode;
4252 
4253 	if (!params->channel) {
4254 		/* check if the new channel is supported by hw */
4255 		params->channel = hostapd_hw_get_channel(hapd, params->freq);
4256 	}
4257 
4258 	channel = params->channel;
4259 	if (!channel)
4260 		return -1;
4261 
4262 	hostapd_determine_mode(hapd->iface);
4263 	mode = hapd->iface->current_mode;
4264 
4265 	/* if a pointer to old_params is provided we save previous state */
4266 	if (old_params &&
4267 	    hostapd_set_freq_params(old_params, conf->hw_mode,
4268 				    hostapd_hw_get_freq(hapd, conf->channel),
4269 				    conf->channel, conf->enable_edmg,
4270 				    conf->edmg_channel, conf->ieee80211n,
4271 				    conf->ieee80211ac, conf->ieee80211ax,
4272 				    conf->ieee80211be, conf->secondary_channel,
4273 				    hostapd_get_oper_chwidth(conf),
4274 				    hostapd_get_oper_centr_freq_seg0_idx(conf),
4275 				    hostapd_get_oper_centr_freq_seg1_idx(conf),
4276 				    conf->vht_capab,
4277 				    mode ? &mode->he_capab[IEEE80211_MODE_AP] :
4278 				    NULL,
4279 				    mode ? &mode->eht_capab[IEEE80211_MODE_AP] :
4280 				    NULL,
4281 				    hostapd_get_punct_bitmap(hapd)))
4282 		return -1;
4283 
4284 	switch (params->bandwidth) {
4285 	case 0:
4286 	case 20:
4287 		conf->ht_capab &= ~HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET;
4288 		break;
4289 	case 40:
4290 	case 80:
4291 	case 160:
4292 	case 320:
4293 		conf->ht_capab |= HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET;
4294 		break;
4295 	default:
4296 		return -1;
4297 	}
4298 
4299 	switch (params->bandwidth) {
4300 	case 0:
4301 	case 20:
4302 	case 40:
4303 		hostapd_set_oper_chwidth(conf, CONF_OPER_CHWIDTH_USE_HT);
4304 		break;
4305 	case 80:
4306 		if (params->center_freq2)
4307 			hostapd_set_oper_chwidth(conf,
4308 						 CONF_OPER_CHWIDTH_80P80MHZ);
4309 		else
4310 			hostapd_set_oper_chwidth(conf,
4311 						 CONF_OPER_CHWIDTH_80MHZ);
4312 		break;
4313 	case 160:
4314 		hostapd_set_oper_chwidth(conf, CONF_OPER_CHWIDTH_160MHZ);
4315 		break;
4316 	case 320:
4317 		hostapd_set_oper_chwidth(conf, CONF_OPER_CHWIDTH_320MHZ);
4318 		break;
4319 	default:
4320 		return -1;
4321 	}
4322 
4323 	conf->channel = channel;
4324 	conf->ieee80211n = params->ht_enabled;
4325 	conf->ieee80211ac = params->vht_enabled;
4326 	conf->secondary_channel = params->sec_channel_offset;
4327 	if (params->center_freq1 &&
4328 	    ieee80211_freq_to_chan(params->center_freq1, &seg0) ==
4329 	    NUM_HOSTAPD_MODES)
4330 		return -1;
4331 	if (params->center_freq2 &&
4332 	    ieee80211_freq_to_chan(params->center_freq2,
4333 				   &seg1) == NUM_HOSTAPD_MODES)
4334 		return -1;
4335 	hostapd_set_oper_centr_freq_seg0_idx(conf, seg0);
4336 	hostapd_set_oper_centr_freq_seg1_idx(conf, seg1);
4337 
4338 	/* TODO: maybe call here hostapd_config_check here? */
4339 
4340 	return 0;
4341 }
4342 
4343 
hostapd_fill_csa_settings(struct hostapd_data * hapd,struct csa_settings * settings)4344 static int hostapd_fill_csa_settings(struct hostapd_data *hapd,
4345 				     struct csa_settings *settings)
4346 {
4347 	struct hostapd_iface *iface = hapd->iface;
4348 	struct hostapd_freq_params old_freq;
4349 	int ret;
4350 #ifdef CONFIG_IEEE80211BE
4351 	u16 old_punct_bitmap;
4352 #endif /* CONFIG_IEEE80211BE */
4353 	u8 chan, bandwidth;
4354 
4355 	os_memset(&old_freq, 0, sizeof(old_freq));
4356 	if (!iface || !iface->freq || hapd->csa_in_progress)
4357 		return -1;
4358 
4359 	switch (settings->freq_params.bandwidth) {
4360 	case 80:
4361 		if (settings->freq_params.center_freq2)
4362 			bandwidth = CONF_OPER_CHWIDTH_80P80MHZ;
4363 		else
4364 			bandwidth = CONF_OPER_CHWIDTH_80MHZ;
4365 		break;
4366 	case 160:
4367 		bandwidth = CONF_OPER_CHWIDTH_160MHZ;
4368 		break;
4369 	case 320:
4370 		bandwidth = CONF_OPER_CHWIDTH_320MHZ;
4371 		break;
4372 	default:
4373 		bandwidth = CONF_OPER_CHWIDTH_USE_HT;
4374 		break;
4375 	}
4376 
4377 	if (ieee80211_freq_to_channel_ext(
4378 		    settings->freq_params.freq,
4379 		    settings->freq_params.sec_channel_offset,
4380 		    bandwidth,
4381 		    &hapd->iface->cs_oper_class,
4382 		    &chan) == NUM_HOSTAPD_MODES) {
4383 		wpa_printf(MSG_DEBUG,
4384 			   "invalid frequency for channel switch (freq=%d, sec_channel_offset=%d, vht_enabled=%d, he_enabled=%d, eht_enabled=%d)",
4385 			   settings->freq_params.freq,
4386 			   settings->freq_params.sec_channel_offset,
4387 			   settings->freq_params.vht_enabled,
4388 			   settings->freq_params.he_enabled,
4389 			   settings->freq_params.eht_enabled);
4390 		return -1;
4391 	}
4392 
4393 	settings->freq_params.channel = chan;
4394 
4395 	ret = hostapd_change_config_freq(iface->bss[0], iface->conf,
4396 					 &settings->freq_params,
4397 					 &old_freq);
4398 	if (ret)
4399 		return ret;
4400 
4401 #ifdef CONFIG_IEEE80211BE
4402 	old_punct_bitmap = iface->conf->punct_bitmap;
4403 	iface->conf->punct_bitmap = settings->punct_bitmap;
4404 #endif /* CONFIG_IEEE80211BE */
4405 	ret = hostapd_build_beacon_data(hapd, &settings->beacon_after);
4406 
4407 	/* change back the configuration */
4408 #ifdef CONFIG_IEEE80211BE
4409 	iface->conf->punct_bitmap = old_punct_bitmap;
4410 #endif /* CONFIG_IEEE80211BE */
4411 	hostapd_change_config_freq(iface->bss[0], iface->conf,
4412 				   &old_freq, NULL);
4413 
4414 	if (ret)
4415 		return ret;
4416 
4417 	/* set channel switch parameters for csa ie */
4418 	hapd->cs_freq_params = settings->freq_params;
4419 	hapd->cs_count = settings->cs_count;
4420 	hapd->cs_block_tx = settings->block_tx;
4421 
4422 	ret = hostapd_build_beacon_data(hapd, &settings->beacon_csa);
4423 	if (ret) {
4424 		free_beacon_data(&settings->beacon_after);
4425 		return ret;
4426 	}
4427 
4428 	settings->counter_offset_beacon[0] = hapd->cs_c_off_beacon;
4429 	settings->counter_offset_presp[0] = hapd->cs_c_off_proberesp;
4430 	settings->counter_offset_beacon[1] = hapd->cs_c_off_ecsa_beacon;
4431 	settings->counter_offset_presp[1] = hapd->cs_c_off_ecsa_proberesp;
4432 	settings->link_id = -1;
4433 #ifdef CONFIG_IEEE80211BE
4434 	if (hapd->conf->mld_ap)
4435 		settings->link_id = hapd->mld_link_id;
4436 #endif /* CONFIG_IEEE80211BE */
4437 
4438 #ifdef CONFIG_IEEE80211AX
4439 	settings->ubpr.unsol_bcast_probe_resp_tmpl =
4440 		hostapd_unsol_bcast_probe_resp(hapd, &settings->ubpr);
4441 #endif /* CONFIG_IEEE80211AX */
4442 
4443 	return 0;
4444 }
4445 
4446 
hostapd_cleanup_cs_params(struct hostapd_data * hapd)4447 void hostapd_cleanup_cs_params(struct hostapd_data *hapd)
4448 {
4449 	os_memset(&hapd->cs_freq_params, 0, sizeof(hapd->cs_freq_params));
4450 	hapd->cs_count = 0;
4451 	hapd->cs_block_tx = 0;
4452 	hapd->cs_c_off_beacon = 0;
4453 	hapd->cs_c_off_proberesp = 0;
4454 	hapd->csa_in_progress = 0;
4455 	hapd->cs_c_off_ecsa_beacon = 0;
4456 	hapd->cs_c_off_ecsa_proberesp = 0;
4457 }
4458 
4459 
hostapd_chan_switch_config(struct hostapd_data * hapd,struct hostapd_freq_params * freq_params)4460 void hostapd_chan_switch_config(struct hostapd_data *hapd,
4461 				struct hostapd_freq_params *freq_params)
4462 {
4463 	if (freq_params->eht_enabled)
4464 		hapd->iconf->ch_switch_eht_config |= CH_SWITCH_EHT_ENABLED;
4465 	else
4466 		hapd->iconf->ch_switch_eht_config |= CH_SWITCH_EHT_DISABLED;
4467 
4468 	if (freq_params->he_enabled)
4469 		hapd->iconf->ch_switch_he_config |= CH_SWITCH_HE_ENABLED;
4470 	else
4471 		hapd->iconf->ch_switch_he_config |= CH_SWITCH_HE_DISABLED;
4472 
4473 	if (freq_params->vht_enabled)
4474 		hapd->iconf->ch_switch_vht_config |= CH_SWITCH_VHT_ENABLED;
4475 	else
4476 		hapd->iconf->ch_switch_vht_config |= CH_SWITCH_VHT_DISABLED;
4477 
4478 	hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
4479 		       HOSTAPD_LEVEL_INFO,
4480 		       "CHAN_SWITCH EHT config 0x%x HE config 0x%x VHT config 0x%x",
4481 		       hapd->iconf->ch_switch_eht_config,
4482 		       hapd->iconf->ch_switch_he_config,
4483 		       hapd->iconf->ch_switch_vht_config);
4484 }
4485 
4486 
hostapd_switch_channel(struct hostapd_data * hapd,struct csa_settings * settings)4487 int hostapd_switch_channel(struct hostapd_data *hapd,
4488 			   struct csa_settings *settings)
4489 {
4490 	int ret;
4491 
4492 	if (!(hapd->iface->drv_flags & WPA_DRIVER_FLAGS_AP_CSA)) {
4493 		wpa_printf(MSG_INFO, "CSA is not supported");
4494 		return -1;
4495 	}
4496 
4497 	ret = hostapd_fill_csa_settings(hapd, settings);
4498 	if (ret)
4499 		return ret;
4500 
4501 	ret = hostapd_drv_switch_channel(hapd, settings);
4502 	free_beacon_data(&settings->beacon_csa);
4503 	free_beacon_data(&settings->beacon_after);
4504 #ifdef CONFIG_IEEE80211AX
4505 	os_free(settings->ubpr.unsol_bcast_probe_resp_tmpl);
4506 #endif /* CONFIG_IEEE80211AX */
4507 
4508 	if (ret) {
4509 		/* if we failed, clean cs parameters */
4510 		hostapd_cleanup_cs_params(hapd);
4511 		return ret;
4512 	}
4513 
4514 	hapd->csa_in_progress = 1;
4515 	return 0;
4516 }
4517 
4518 
4519 void
hostapd_switch_channel_fallback(struct hostapd_iface * iface,const struct hostapd_freq_params * freq_params)4520 hostapd_switch_channel_fallback(struct hostapd_iface *iface,
4521 				const struct hostapd_freq_params *freq_params)
4522 {
4523 	u8 seg0_idx = 0, seg1_idx = 0;
4524 	enum oper_chan_width bw = CONF_OPER_CHWIDTH_USE_HT;
4525 	u8 op_class, chan = 0;
4526 
4527 	wpa_printf(MSG_DEBUG, "Restarting all CSA-related BSSes");
4528 
4529 	if (freq_params->center_freq1)
4530 		ieee80211_freq_to_chan(freq_params->center_freq1, &seg0_idx);
4531 	if (freq_params->center_freq2)
4532 		ieee80211_freq_to_chan(freq_params->center_freq2, &seg1_idx);
4533 
4534 	switch (freq_params->bandwidth) {
4535 	case 0:
4536 	case 20:
4537 	case 40:
4538 		bw = CONF_OPER_CHWIDTH_USE_HT;
4539 		break;
4540 	case 80:
4541 		if (freq_params->center_freq2) {
4542 			bw = CONF_OPER_CHWIDTH_80P80MHZ;
4543 			iface->conf->vht_capab |=
4544 				VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ;
4545 		} else {
4546 			bw = CONF_OPER_CHWIDTH_80MHZ;
4547 		}
4548 		break;
4549 	case 160:
4550 		bw = CONF_OPER_CHWIDTH_160MHZ;
4551 		iface->conf->vht_capab |= VHT_CAP_SUPP_CHAN_WIDTH_160MHZ;
4552 		break;
4553 	case 320:
4554 		bw = CONF_OPER_CHWIDTH_320MHZ;
4555 		break;
4556 	default:
4557 		wpa_printf(MSG_WARNING, "Unknown CSA bandwidth: %d",
4558 			   freq_params->bandwidth);
4559 		break;
4560 	}
4561 
4562 	iface->freq = freq_params->freq;
4563 	iface->conf->channel = freq_params->channel;
4564 	iface->conf->secondary_channel = freq_params->sec_channel_offset;
4565 	if (ieee80211_freq_to_channel_ext(freq_params->freq,
4566 					  freq_params->sec_channel_offset, bw,
4567 					  &op_class, &chan) ==
4568 	    NUM_HOSTAPD_MODES ||
4569 	    chan != freq_params->channel)
4570 		wpa_printf(MSG_INFO, "CSA: Channel mismatch: %d -> %d",
4571 			   freq_params->channel, chan);
4572 
4573 	iface->conf->op_class = op_class;
4574 	hostapd_set_oper_centr_freq_seg0_idx(iface->conf, seg0_idx);
4575 	hostapd_set_oper_centr_freq_seg1_idx(iface->conf, seg1_idx);
4576 	hostapd_set_oper_chwidth(iface->conf, bw);
4577 	iface->conf->ieee80211n = freq_params->ht_enabled;
4578 	iface->conf->ieee80211ac = freq_params->vht_enabled;
4579 	iface->conf->ieee80211ax = freq_params->he_enabled;
4580 	iface->conf->ieee80211be = freq_params->eht_enabled;
4581 
4582 	/*
4583 	 * cs_params must not be cleared earlier because the freq_params
4584 	 * argument may actually point to one of these.
4585 	 * These params will be cleared during interface disable below.
4586 	 */
4587 	hostapd_disable_iface(iface);
4588 	hostapd_enable_iface(iface);
4589 }
4590 
4591 
4592 #ifdef CONFIG_IEEE80211AX
4593 
hostapd_cleanup_cca_params(struct hostapd_data * hapd)4594 void hostapd_cleanup_cca_params(struct hostapd_data *hapd)
4595 {
4596 	hapd->cca_count = 0;
4597 	hapd->cca_color = 0;
4598 	hapd->cca_c_off_beacon = 0;
4599 	hapd->cca_c_off_proberesp = 0;
4600 	hapd->cca_in_progress = false;
4601 }
4602 
4603 
hostapd_fill_cca_settings(struct hostapd_data * hapd,struct cca_settings * settings)4604 int hostapd_fill_cca_settings(struct hostapd_data *hapd,
4605 			      struct cca_settings *settings)
4606 {
4607 	struct hostapd_iface *iface = hapd->iface;
4608 	u8 old_color;
4609 	int ret;
4610 
4611 	if (!iface || iface->conf->he_op.he_bss_color_disabled)
4612 		return -1;
4613 
4614 	settings->link_id = -1;
4615 #ifdef CONFIG_IEEE80211BE
4616 	if (hapd->conf->mld_ap)
4617 		settings->link_id = hapd->mld_link_id;
4618 #endif /* CONFIG_IEEE80211BE */
4619 
4620 	old_color = iface->conf->he_op.he_bss_color;
4621 	iface->conf->he_op.he_bss_color = hapd->cca_color;
4622 	ret = hostapd_build_beacon_data(hapd, &settings->beacon_after);
4623 	if (ret)
4624 		return ret;
4625 
4626 	iface->conf->he_op.he_bss_color = old_color;
4627 
4628 	settings->cca_count = hapd->cca_count;
4629 	settings->cca_color = hapd->cca_color,
4630 	hapd->cca_in_progress = true;
4631 
4632 	ret = hostapd_build_beacon_data(hapd, &settings->beacon_cca);
4633 	if (ret) {
4634 		free_beacon_data(&settings->beacon_after);
4635 		return ret;
4636 	}
4637 
4638 	settings->ubpr.unsol_bcast_probe_resp_tmpl =
4639 		hostapd_unsol_bcast_probe_resp(hapd, &settings->ubpr);
4640 
4641 	settings->counter_offset_beacon = hapd->cca_c_off_beacon;
4642 	settings->counter_offset_presp = hapd->cca_c_off_proberesp;
4643 
4644 	return 0;
4645 }
4646 
4647 
hostapd_switch_color_timeout_handler(void * eloop_data,void * user_ctx)4648 static void hostapd_switch_color_timeout_handler(void *eloop_data,
4649 						 void *user_ctx)
4650 {
4651 	struct hostapd_data *hapd = (struct hostapd_data *) eloop_data;
4652 	os_time_t delta_t;
4653 	unsigned int b;
4654 	int i, r;
4655 
4656 	 /* CCA can be triggered once the handler constantly receives
4657 	  * color collision events to for at least
4658 	  * DOT11BSS_COLOR_COLLISION_AP_PERIOD (50 s by default). */
4659 	delta_t = hapd->last_color_collision.sec -
4660 		hapd->first_color_collision.sec;
4661 	if (delta_t < DOT11BSS_COLOR_COLLISION_AP_PERIOD)
4662 		return;
4663 
4664 	r = os_random() % HE_OPERATION_BSS_COLOR_MAX;
4665 	for (i = 0; i < HE_OPERATION_BSS_COLOR_MAX; i++) {
4666 		if (r && !(hapd->color_collision_bitmap & (1ULL << r)))
4667 			break;
4668 
4669 		r = (r + 1) % HE_OPERATION_BSS_COLOR_MAX;
4670 	}
4671 
4672 	if (i == HE_OPERATION_BSS_COLOR_MAX) {
4673 		/* There are no free colors so turn BSS coloring off */
4674 		wpa_printf(MSG_INFO,
4675 			   "No free colors left, turning off BSS coloring");
4676 		hapd->iface->conf->he_op.he_bss_color_disabled = 1;
4677 		hapd->iface->conf->he_op.he_bss_color = os_random() % 63 + 1;
4678 		for (b = 0; b < hapd->iface->num_bss; b++)
4679 			ieee802_11_set_beacon(hapd->iface->bss[b]);
4680 		return;
4681 	}
4682 
4683 	for (b = 0; b < hapd->iface->num_bss; b++) {
4684 		struct hostapd_data *bss = hapd->iface->bss[b];
4685 		struct cca_settings settings;
4686 		int ret;
4687 
4688 		hostapd_cleanup_cca_params(bss);
4689 		bss->cca_color = r;
4690 		bss->cca_count = 10;
4691 
4692 		if (hostapd_fill_cca_settings(bss, &settings)) {
4693 			hostapd_cleanup_cca_params(bss);
4694 			continue;
4695 		}
4696 
4697 		ret = hostapd_drv_switch_color(bss, &settings);
4698 		if (ret)
4699 			hostapd_cleanup_cca_params(bss);
4700 
4701 		free_beacon_data(&settings.beacon_cca);
4702 		free_beacon_data(&settings.beacon_after);
4703 		os_free(settings.ubpr.unsol_bcast_probe_resp_tmpl);
4704 	}
4705 }
4706 
4707 
hostapd_switch_color(struct hostapd_data * hapd,u64 bitmap)4708 void hostapd_switch_color(struct hostapd_data *hapd, u64 bitmap)
4709 {
4710 	struct os_reltime now;
4711 
4712 	if (hapd->cca_in_progress)
4713 		return;
4714 
4715 	if (os_get_reltime(&now))
4716 		return;
4717 
4718 	hapd->color_collision_bitmap = bitmap;
4719 	hapd->last_color_collision = now;
4720 
4721 	if (eloop_is_timeout_registered(hostapd_switch_color_timeout_handler,
4722 					hapd, NULL))
4723 		return;
4724 
4725 	hapd->first_color_collision = now;
4726 	/* 10 s window as margin for persistent color collision reporting */
4727 	eloop_register_timeout(DOT11BSS_COLOR_COLLISION_AP_PERIOD + 10, 0,
4728 			       hostapd_switch_color_timeout_handler,
4729 			       hapd, NULL);
4730 }
4731 
4732 #endif /* CONFIG_IEEE80211AX */
4733 
4734 #endif /* NEED_AP_MLME */
4735 
4736 
hostapd_get_iface(struct hapd_interfaces * interfaces,const char * ifname)4737 struct hostapd_data * hostapd_get_iface(struct hapd_interfaces *interfaces,
4738 					const char *ifname)
4739 {
4740 	size_t i, j;
4741 
4742 	for (i = 0; i < interfaces->count; i++) {
4743 		struct hostapd_iface *iface = interfaces->iface[i];
4744 
4745 		for (j = 0; j < iface->num_bss; j++) {
4746 			struct hostapd_data *hapd = iface->bss[j];
4747 
4748 			if (os_strcmp(ifname, hapd->conf->iface) == 0)
4749 				return hapd;
4750 		}
4751 	}
4752 
4753 	return NULL;
4754 }
4755 
4756 
hostapd_periodic_iface(struct hostapd_iface * iface)4757 void hostapd_periodic_iface(struct hostapd_iface *iface)
4758 {
4759 	size_t i;
4760 
4761 	ap_list_timer(iface);
4762 
4763 	for (i = 0; i < iface->num_bss; i++) {
4764 		struct hostapd_data *hapd = iface->bss[i];
4765 
4766 		if (!hapd->started)
4767 			continue;
4768 
4769 #ifndef CONFIG_NO_RADIUS
4770 		hostapd_acl_expire(hapd);
4771 #endif /* CONFIG_NO_RADIUS */
4772 	}
4773 }
4774 
4775 
4776 #ifdef CONFIG_OCV
hostapd_ocv_check_csa_sa_query(void * eloop_ctx,void * timeout_ctx)4777 void hostapd_ocv_check_csa_sa_query(void *eloop_ctx, void *timeout_ctx)
4778 {
4779 	struct hostapd_data *hapd = eloop_ctx;
4780 	struct sta_info *sta;
4781 
4782 	wpa_printf(MSG_DEBUG, "OCV: Post-CSA SA Query initiation check");
4783 
4784 	for (sta = hapd->sta_list; sta; sta = sta->next) {
4785 		if (!sta->post_csa_sa_query)
4786 			continue;
4787 
4788 		wpa_printf(MSG_DEBUG, "OCV: OCVC STA " MACSTR
4789 			   " did not start SA Query after CSA - disconnect",
4790 			   MAC2STR(sta->addr));
4791 		ap_sta_disconnect(hapd, sta, sta->addr,
4792 				  WLAN_REASON_PREV_AUTH_NOT_VALID);
4793 	}
4794 }
4795 #endif /* CONFIG_OCV */
4796 
4797 
4798 #ifdef CONFIG_IEEE80211BE
4799 
hostapd_mld_get_link_bss(struct hostapd_data * hapd,u8 link_id)4800 struct hostapd_data * hostapd_mld_get_link_bss(struct hostapd_data *hapd,
4801 					       u8 link_id)
4802 {
4803 	struct hostapd_iface *iface;
4804 	struct hostapd_data *bss;
4805 	unsigned int i, j;
4806 
4807 	for (i = 0; i < hapd->iface->interfaces->count; i++) {
4808 		iface = hapd->iface->interfaces->iface[i];
4809 		if (!iface)
4810 			continue;
4811 
4812 		for (j = 0; j < iface->num_bss; j++) {
4813 			bss = iface->bss[j];
4814 
4815 			if (!bss->conf->mld_ap ||
4816 			    !hostapd_is_ml_partner(hapd, bss))
4817 				continue;
4818 
4819 			if (!bss->drv_priv)
4820 				continue;
4821 
4822 			if (bss->mld_link_id == link_id)
4823 				return bss;
4824 		}
4825 	}
4826 
4827 	return NULL;
4828 }
4829 
4830 
hostapd_is_ml_partner(struct hostapd_data * hapd1,struct hostapd_data * hapd2)4831 bool hostapd_is_ml_partner(struct hostapd_data *hapd1,
4832 			   struct hostapd_data *hapd2)
4833 {
4834 	if (!hapd1->conf->mld_ap || !hapd2->conf->mld_ap)
4835 		return false;
4836 
4837 	return !os_strcmp(hapd1->conf->iface, hapd2->conf->iface);
4838 }
4839 
4840 
hostapd_get_mld_id(struct hostapd_data * hapd)4841 u8 hostapd_get_mld_id(struct hostapd_data *hapd)
4842 {
4843 	if (!hapd->conf->mld_ap)
4844 		return 255;
4845 
4846 	/* MLD ID 0 represents self */
4847 	return 0;
4848 
4849 	/* TODO: MLD ID for Multiple BSS cases */
4850 }
4851 
4852 
hostapd_mld_add_link(struct hostapd_data * hapd)4853 int hostapd_mld_add_link(struct hostapd_data *hapd)
4854 {
4855 	struct hostapd_mld *mld = hapd->mld;
4856 
4857 	if (!hapd->conf->mld_ap)
4858 		return 0;
4859 
4860 	/* Should not happen */
4861 	if (!mld)
4862 		return -1;
4863 
4864 	dl_list_add_tail(&mld->links, &hapd->link);
4865 	mld->num_links++;
4866 
4867 	wpa_printf(MSG_DEBUG, "AP MLD %s: Link ID %d added. num_links: %d",
4868 		   mld->name, hapd->mld_link_id, mld->num_links);
4869 
4870 	if (mld->fbss)
4871 		return 0;
4872 
4873 	mld->fbss = hapd;
4874 	wpa_printf(MSG_DEBUG, "AP MLD %s: First link BSS set to %p",
4875 		   mld->name, mld->fbss);
4876 	return 0;
4877 }
4878 
4879 
hostapd_mld_remove_link(struct hostapd_data * hapd)4880 int hostapd_mld_remove_link(struct hostapd_data *hapd)
4881 {
4882 	struct hostapd_mld *mld = hapd->mld;
4883 	struct hostapd_data *next_fbss;
4884 
4885 	if (!hapd->conf->mld_ap)
4886 		return 0;
4887 
4888 	/* Should not happen */
4889 	if (!mld)
4890 		return -1;
4891 
4892 	dl_list_del(&hapd->link);
4893 	mld->num_links--;
4894 
4895 	wpa_printf(MSG_DEBUG, "AP MLD %s: Link ID %d removed. num_links: %d",
4896 		   mld->name, hapd->mld_link_id, mld->num_links);
4897 
4898 	if (mld->fbss != hapd)
4899 		return 0;
4900 
4901 	/* If the list is empty, all links are removed */
4902 	if (dl_list_empty(&mld->links)) {
4903 		mld->fbss = NULL;
4904 	} else {
4905 		next_fbss = dl_list_entry(mld->links.next, struct hostapd_data,
4906 					  link);
4907 		mld->fbss = next_fbss;
4908 	}
4909 
4910 	wpa_printf(MSG_DEBUG, "AP MLD %s: First link BSS set to %p",
4911 		   mld->name, mld->fbss);
4912 	return 0;
4913 }
4914 
4915 
hostapd_mld_is_first_bss(struct hostapd_data * hapd)4916 bool hostapd_mld_is_first_bss(struct hostapd_data *hapd)
4917 {
4918 	struct hostapd_mld *mld = hapd->mld;
4919 
4920 	if (!hapd->conf->mld_ap)
4921 		return true;
4922 
4923 	/* Should not happen */
4924 	if (!mld)
4925 		return false;
4926 
4927 	/* If fbss is not set, it is safe to assume the caller is the first BSS.
4928 	 */
4929 	if (!mld->fbss)
4930 		return true;
4931 
4932 	return hapd == mld->fbss;
4933 }
4934 
4935 
hostapd_mld_get_first_bss(struct hostapd_data * hapd)4936 struct hostapd_data * hostapd_mld_get_first_bss(struct hostapd_data *hapd)
4937 {
4938 	struct hostapd_mld *mld = hapd->mld;
4939 
4940 	if (!hapd->conf->mld_ap)
4941 		return NULL;
4942 
4943 	/* Should not happen */
4944 	if (!mld)
4945 		return NULL;
4946 
4947 	return mld->fbss;
4948 }
4949 
4950 #endif /* CONFIG_IEEE80211BE */
4951 
4952 
hostapd_get_punct_bitmap(struct hostapd_data * hapd)4953 u16 hostapd_get_punct_bitmap(struct hostapd_data *hapd)
4954 {
4955 	u16 punct_bitmap = 0;
4956 
4957 #ifdef CONFIG_IEEE80211BE
4958 	punct_bitmap = hapd->iconf->punct_bitmap;
4959 #ifdef CONFIG_TESTING_OPTIONS
4960 	if (!punct_bitmap)
4961 		punct_bitmap = hapd->conf->eht_oper_puncturing_override;
4962 #endif /* CONFIG_TESTING_OPTIONS */
4963 #endif /* CONFIG_IEEE80211BE */
4964 
4965 	return punct_bitmap;
4966 }
4967