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