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