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