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