xref: /freebsd/contrib/wpa/src/ap/beacon.c (revision 3e5d0784b9b5296bda801add034b057ad68237f7)
1 /*
2  * hostapd / IEEE 802.11 Management: Beacon and Probe Request/Response
3  * Copyright (c) 2002-2004, Instant802 Networks, Inc.
4  * Copyright (c) 2005-2006, Devicescape Software, Inc.
5  * Copyright (c) 2008-2012, Jouni Malinen <j@w1.fi>
6  *
7  * This software may be distributed under the terms of the BSD license.
8  * See README for more details.
9  */
10 
11 #include "utils/includes.h"
12 
13 #ifndef CONFIG_NATIVE_WINDOWS
14 
15 #include "utils/common.h"
16 #include "common/ieee802_11_defs.h"
17 #include "common/ieee802_11_common.h"
18 #include "common/hw_features_common.h"
19 #include "common/wpa_ctrl.h"
20 #include "wps/wps_defs.h"
21 #include "p2p/p2p.h"
22 #include "hostapd.h"
23 #include "ieee802_11.h"
24 #include "wpa_auth.h"
25 #include "wmm.h"
26 #include "ap_config.h"
27 #include "sta_info.h"
28 #include "p2p_hostapd.h"
29 #include "ap_drv_ops.h"
30 #include "beacon.h"
31 #include "hs20.h"
32 #include "dfs.h"
33 #include "taxonomy.h"
34 #include "ieee802_11_auth.h"
35 
36 
37 #ifdef NEED_AP_MLME
38 
39 static u8 * hostapd_eid_bss_load(struct hostapd_data *hapd, u8 *eid, size_t len)
40 {
41 	if (len < 2 + 5)
42 		return eid;
43 
44 #ifdef CONFIG_TESTING_OPTIONS
45 	if (hapd->conf->bss_load_test_set) {
46 		*eid++ = WLAN_EID_BSS_LOAD;
47 		*eid++ = 5;
48 		os_memcpy(eid, hapd->conf->bss_load_test, 5);
49 		eid += 5;
50 		return eid;
51 	}
52 #endif /* CONFIG_TESTING_OPTIONS */
53 	if (hapd->conf->bss_load_update_period) {
54 		*eid++ = WLAN_EID_BSS_LOAD;
55 		*eid++ = 5;
56 		WPA_PUT_LE16(eid, hapd->num_sta);
57 		eid += 2;
58 		*eid++ = hapd->iface->channel_utilization;
59 		WPA_PUT_LE16(eid, 0); /* no available admission capabity */
60 		eid += 2;
61 	}
62 	return eid;
63 }
64 
65 
66 static u8 ieee802_11_erp_info(struct hostapd_data *hapd)
67 {
68 	u8 erp = 0;
69 
70 	if (hapd->iface->current_mode == NULL ||
71 	    hapd->iface->current_mode->mode != HOSTAPD_MODE_IEEE80211G)
72 		return 0;
73 
74 	if (hapd->iface->olbc)
75 		erp |= ERP_INFO_USE_PROTECTION;
76 	if (hapd->iface->num_sta_non_erp > 0) {
77 		erp |= ERP_INFO_NON_ERP_PRESENT |
78 			ERP_INFO_USE_PROTECTION;
79 	}
80 	if (hapd->iface->num_sta_no_short_preamble > 0 ||
81 	    hapd->iconf->preamble == LONG_PREAMBLE)
82 		erp |= ERP_INFO_BARKER_PREAMBLE_MODE;
83 
84 	return erp;
85 }
86 
87 
88 static u8 * hostapd_eid_ds_params(struct hostapd_data *hapd, u8 *eid)
89 {
90 	*eid++ = WLAN_EID_DS_PARAMS;
91 	*eid++ = 1;
92 	*eid++ = hapd->iconf->channel;
93 	return eid;
94 }
95 
96 
97 static u8 * hostapd_eid_erp_info(struct hostapd_data *hapd, u8 *eid)
98 {
99 	if (hapd->iface->current_mode == NULL ||
100 	    hapd->iface->current_mode->mode != HOSTAPD_MODE_IEEE80211G)
101 		return eid;
102 
103 	/* Set NonERP_present and use_protection bits if there
104 	 * are any associated NonERP stations. */
105 	/* TODO: use_protection bit can be set to zero even if
106 	 * there are NonERP stations present. This optimization
107 	 * might be useful if NonERP stations are "quiet".
108 	 * See 802.11g/D6 E-1 for recommended practice.
109 	 * In addition, Non ERP present might be set, if AP detects Non ERP
110 	 * operation on other APs. */
111 
112 	/* Add ERP Information element */
113 	*eid++ = WLAN_EID_ERP_INFO;
114 	*eid++ = 1;
115 	*eid++ = ieee802_11_erp_info(hapd);
116 
117 	return eid;
118 }
119 
120 
121 static u8 * hostapd_eid_pwr_constraint(struct hostapd_data *hapd, u8 *eid)
122 {
123 	u8 *pos = eid;
124 	u8 local_pwr_constraint = 0;
125 	int dfs;
126 
127 	if (hapd->iface->current_mode == NULL ||
128 	    hapd->iface->current_mode->mode != HOSTAPD_MODE_IEEE80211A)
129 		return eid;
130 
131 	/* Let host drivers add this IE if DFS support is offloaded */
132 	if (hapd->iface->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD)
133 		return eid;
134 
135 	/*
136 	 * There is no DFS support and power constraint was not directly
137 	 * requested by config option.
138 	 */
139 	if (!hapd->iconf->ieee80211h &&
140 	    hapd->iconf->local_pwr_constraint == -1)
141 		return eid;
142 
143 	/* Check if DFS is required by regulatory. */
144 	dfs = hostapd_is_dfs_required(hapd->iface);
145 	if (dfs < 0) {
146 		wpa_printf(MSG_WARNING, "Failed to check if DFS is required; ret=%d",
147 			   dfs);
148 		dfs = 0;
149 	}
150 
151 	if (dfs == 0 && hapd->iconf->local_pwr_constraint == -1)
152 		return eid;
153 
154 	/*
155 	 * ieee80211h (DFS) is enabled so Power Constraint element shall
156 	 * be added when running on DFS channel whenever local_pwr_constraint
157 	 * is configured or not. In order to meet regulations when TPC is not
158 	 * implemented using a transmit power that is below the legal maximum
159 	 * (including any mitigation factor) should help. In this case,
160 	 * indicate 3 dB below maximum allowed transmit power.
161 	 */
162 	if (hapd->iconf->local_pwr_constraint == -1)
163 		local_pwr_constraint = 3;
164 
165 	/*
166 	 * A STA that is not an AP shall use a transmit power less than or
167 	 * equal to the local maximum transmit power level for the channel.
168 	 * The local maximum transmit power can be calculated from the formula:
169 	 * local max TX pwr = max TX pwr - local pwr constraint
170 	 * Where max TX pwr is maximum transmit power level specified for
171 	 * channel in Country element and local pwr constraint is specified
172 	 * for channel in this Power Constraint element.
173 	 */
174 
175 	/* Element ID */
176 	*pos++ = WLAN_EID_PWR_CONSTRAINT;
177 	/* Length */
178 	*pos++ = 1;
179 	/* Local Power Constraint */
180 	if (local_pwr_constraint)
181 		*pos++ = local_pwr_constraint;
182 	else
183 		*pos++ = hapd->iconf->local_pwr_constraint;
184 
185 	return pos;
186 }
187 
188 
189 static u8 * hostapd_eid_country_add(u8 *pos, u8 *end, int chan_spacing,
190 				    struct hostapd_channel_data *start,
191 				    struct hostapd_channel_data *prev)
192 {
193 	if (end - pos < 3)
194 		return pos;
195 
196 	/* first channel number */
197 	*pos++ = start->chan;
198 	/* number of channels */
199 	*pos++ = (prev->chan - start->chan) / chan_spacing + 1;
200 	/* maximum transmit power level */
201 	*pos++ = start->max_tx_power;
202 
203 	return pos;
204 }
205 
206 
207 static u8 * hostapd_eid_country(struct hostapd_data *hapd, u8 *eid,
208 				int max_len)
209 {
210 	u8 *pos = eid;
211 	u8 *end = eid + max_len;
212 	int i;
213 	struct hostapd_hw_modes *mode;
214 	struct hostapd_channel_data *start, *prev;
215 	int chan_spacing = 1;
216 
217 	if (!hapd->iconf->ieee80211d || max_len < 6 ||
218 	    hapd->iface->current_mode == NULL)
219 		return eid;
220 
221 	*pos++ = WLAN_EID_COUNTRY;
222 	pos++; /* length will be set later */
223 	os_memcpy(pos, hapd->iconf->country, 3); /* e.g., 'US ' */
224 	pos += 3;
225 
226 	mode = hapd->iface->current_mode;
227 	if (mode->mode == HOSTAPD_MODE_IEEE80211A)
228 		chan_spacing = 4;
229 
230 	start = prev = NULL;
231 	for (i = 0; i < mode->num_channels; i++) {
232 		struct hostapd_channel_data *chan = &mode->channels[i];
233 		if (chan->flag & HOSTAPD_CHAN_DISABLED)
234 			continue;
235 		if (start && prev &&
236 		    prev->chan + chan_spacing == chan->chan &&
237 		    start->max_tx_power == chan->max_tx_power) {
238 			prev = chan;
239 			continue; /* can use same entry */
240 		}
241 
242 		if (start && prev) {
243 			pos = hostapd_eid_country_add(pos, end, chan_spacing,
244 						      start, prev);
245 			start = NULL;
246 		}
247 
248 		/* Start new group */
249 		start = prev = chan;
250 	}
251 
252 	if (start) {
253 		pos = hostapd_eid_country_add(pos, end, chan_spacing,
254 					      start, prev);
255 	}
256 
257 	if ((pos - eid) & 1) {
258 		if (end - pos < 1)
259 			return eid;
260 		*pos++ = 0; /* pad for 16-bit alignment */
261 	}
262 
263 	eid[1] = (pos - eid) - 2;
264 
265 	return pos;
266 }
267 
268 
269 const u8 * hostapd_wpa_ie(struct hostapd_data *hapd, u8 eid)
270 {
271 	const u8 *ies;
272 	size_t ies_len;
273 
274 	ies = wpa_auth_get_wpa_ie(hapd->wpa_auth, &ies_len);
275 	if (!ies)
276 		return NULL;
277 
278 	return get_ie(ies, ies_len, eid);
279 }
280 
281 
282 static const u8 * hostapd_vendor_wpa_ie(struct hostapd_data *hapd,
283 					u32 vendor_type)
284 {
285 	const u8 *ies;
286 	size_t ies_len;
287 
288 	ies = wpa_auth_get_wpa_ie(hapd->wpa_auth, &ies_len);
289 	if (!ies)
290 		return NULL;
291 
292 	return get_vendor_ie(ies, ies_len, vendor_type);
293 }
294 
295 
296 static u8 * hostapd_get_rsne(struct hostapd_data *hapd, u8 *pos, size_t len)
297 {
298 	const u8 *ie;
299 
300 	ie = hostapd_wpa_ie(hapd, WLAN_EID_RSN);
301 	if (!ie || 2U + ie[1] > len)
302 		return pos;
303 
304 	os_memcpy(pos, ie, 2 + ie[1]);
305 	return pos + 2 + ie[1];
306 }
307 
308 
309 static u8 * hostapd_get_mde(struct hostapd_data *hapd, u8 *pos, size_t len)
310 {
311 	const u8 *ie;
312 
313 	ie = hostapd_wpa_ie(hapd, WLAN_EID_MOBILITY_DOMAIN);
314 	if (!ie || 2U + ie[1] > len)
315 		return pos;
316 
317 	os_memcpy(pos, ie, 2 + ie[1]);
318 	return pos + 2 + ie[1];
319 }
320 
321 
322 static u8 * hostapd_get_rsnxe(struct hostapd_data *hapd, u8 *pos, size_t len)
323 {
324 	const u8 *ie;
325 
326 #ifdef CONFIG_TESTING_OPTIONS
327 	if (hapd->conf->no_beacon_rsnxe) {
328 		wpa_printf(MSG_INFO, "TESTING: Do not add RSNXE into Beacon");
329 		return pos;
330 	}
331 #endif /* CONFIG_TESTING_OPTIONS */
332 	ie = hostapd_wpa_ie(hapd, WLAN_EID_RSNX);
333 	if (!ie || 2U + ie[1] > len)
334 		return pos;
335 
336 	os_memcpy(pos, ie, 2 + ie[1]);
337 	return pos + 2 + ie[1];
338 }
339 
340 
341 static u8 * hostapd_get_wpa_ie(struct hostapd_data *hapd, u8 *pos, size_t len)
342 {
343 	const u8 *ie;
344 
345 	ie = hostapd_vendor_wpa_ie(hapd, WPA_IE_VENDOR_TYPE);
346 	if (!ie || 2U + ie[1] > len)
347 		return pos;
348 
349 	os_memcpy(pos, ie, 2 + ie[1]);
350 	return pos + 2 + ie[1];
351 }
352 
353 
354 static u8 * hostapd_get_osen_ie(struct hostapd_data *hapd, u8 *pos, size_t len)
355 {
356 	const u8 *ie;
357 
358 	ie = hostapd_vendor_wpa_ie(hapd, OSEN_IE_VENDOR_TYPE);
359 	if (!ie || 2U + ie[1] > len)
360 		return pos;
361 
362 	os_memcpy(pos, ie, 2 + ie[1]);
363 	return pos + 2 + ie[1];
364 }
365 
366 
367 static u8 * hostapd_eid_csa(struct hostapd_data *hapd, u8 *eid)
368 {
369 #ifdef CONFIG_TESTING_OPTIONS
370 	if (hapd->iface->cs_oper_class && hapd->iconf->ecsa_ie_only)
371 		return eid;
372 #endif /* CONFIG_TESTING_OPTIONS */
373 
374 	if (!hapd->cs_freq_params.channel)
375 		return eid;
376 
377 	*eid++ = WLAN_EID_CHANNEL_SWITCH;
378 	*eid++ = 3;
379 	*eid++ = hapd->cs_block_tx;
380 	*eid++ = hapd->cs_freq_params.channel;
381 	*eid++ = hapd->cs_count;
382 
383 	return eid;
384 }
385 
386 
387 static u8 * hostapd_eid_ecsa(struct hostapd_data *hapd, u8 *eid)
388 {
389 	if (!hapd->cs_freq_params.channel || !hapd->iface->cs_oper_class)
390 		return eid;
391 
392 	*eid++ = WLAN_EID_EXT_CHANSWITCH_ANN;
393 	*eid++ = 4;
394 	*eid++ = hapd->cs_block_tx;
395 	*eid++ = hapd->iface->cs_oper_class;
396 	*eid++ = hapd->cs_freq_params.channel;
397 	*eid++ = hapd->cs_count;
398 
399 	return eid;
400 }
401 
402 
403 static u8 * hostapd_eid_supported_op_classes(struct hostapd_data *hapd, u8 *eid)
404 {
405 	u8 op_class, channel;
406 
407 	if (!(hapd->iface->drv_flags & WPA_DRIVER_FLAGS_AP_CSA) ||
408 	    !hapd->iface->freq)
409 		return eid;
410 
411 	if (ieee80211_freq_to_channel_ext(hapd->iface->freq,
412 					  hapd->iconf->secondary_channel,
413 					  hostapd_get_oper_chwidth(hapd->iconf),
414 					  &op_class, &channel) ==
415 	    NUM_HOSTAPD_MODES)
416 		return eid;
417 
418 	*eid++ = WLAN_EID_SUPPORTED_OPERATING_CLASSES;
419 	*eid++ = 2;
420 
421 	/* Current Operating Class */
422 	*eid++ = op_class;
423 
424 	/* TODO: Advertise all the supported operating classes */
425 	*eid++ = 0;
426 
427 	return eid;
428 }
429 
430 
431 static u8 * hostapd_gen_probe_resp(struct hostapd_data *hapd,
432 				   const struct ieee80211_mgmt *req,
433 				   int is_p2p, size_t *resp_len)
434 {
435 	struct ieee80211_mgmt *resp;
436 	u8 *pos, *epos, *csa_pos;
437 	size_t buflen;
438 
439 #define MAX_PROBERESP_LEN 768
440 	buflen = MAX_PROBERESP_LEN;
441 #ifdef CONFIG_WPS
442 	if (hapd->wps_probe_resp_ie)
443 		buflen += wpabuf_len(hapd->wps_probe_resp_ie);
444 #endif /* CONFIG_WPS */
445 #ifdef CONFIG_P2P
446 	if (hapd->p2p_probe_resp_ie)
447 		buflen += wpabuf_len(hapd->p2p_probe_resp_ie);
448 #endif /* CONFIG_P2P */
449 #ifdef CONFIG_FST
450 	if (hapd->iface->fst_ies)
451 		buflen += wpabuf_len(hapd->iface->fst_ies);
452 #endif /* CONFIG_FST */
453 	if (hapd->conf->vendor_elements)
454 		buflen += wpabuf_len(hapd->conf->vendor_elements);
455 	if (hapd->conf->vendor_vht) {
456 		buflen += 5 + 2 + sizeof(struct ieee80211_vht_capabilities) +
457 			2 + sizeof(struct ieee80211_vht_operation);
458 	}
459 
460 #ifdef CONFIG_IEEE80211AX
461 	if (hapd->iconf->ieee80211ax && !hapd->conf->disable_11ax) {
462 		buflen += 3 + sizeof(struct ieee80211_he_capabilities) +
463 			3 + sizeof(struct ieee80211_he_operation) +
464 			3 + sizeof(struct ieee80211_he_mu_edca_parameter_set) +
465 			3 + sizeof(struct ieee80211_spatial_reuse);
466 		if (is_6ghz_op_class(hapd->iconf->op_class))
467 			buflen += sizeof(struct ieee80211_he_6ghz_oper_info) +
468 				3 + sizeof(struct ieee80211_he_6ghz_band_cap);
469 	}
470 #endif /* CONFIG_IEEE80211AX */
471 
472 	buflen += hostapd_eid_rnr_len(hapd, WLAN_FC_STYPE_PROBE_RESP);
473 	buflen += hostapd_mbo_ie_len(hapd);
474 	buflen += hostapd_eid_owe_trans_len(hapd);
475 	buflen += hostapd_eid_dpp_cc_len(hapd);
476 
477 	resp = os_zalloc(buflen);
478 	if (resp == NULL)
479 		return NULL;
480 
481 	epos = ((u8 *) resp) + MAX_PROBERESP_LEN;
482 
483 	resp->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
484 					   WLAN_FC_STYPE_PROBE_RESP);
485 	if (req)
486 		os_memcpy(resp->da, req->sa, ETH_ALEN);
487 	os_memcpy(resp->sa, hapd->own_addr, ETH_ALEN);
488 
489 	os_memcpy(resp->bssid, hapd->own_addr, ETH_ALEN);
490 	resp->u.probe_resp.beacon_int =
491 		host_to_le16(hapd->iconf->beacon_int);
492 
493 	/* hardware or low-level driver will setup seq_ctrl and timestamp */
494 	resp->u.probe_resp.capab_info =
495 		host_to_le16(hostapd_own_capab_info(hapd));
496 
497 	pos = resp->u.probe_resp.variable;
498 	*pos++ = WLAN_EID_SSID;
499 	*pos++ = hapd->conf->ssid.ssid_len;
500 	os_memcpy(pos, hapd->conf->ssid.ssid, hapd->conf->ssid.ssid_len);
501 	pos += hapd->conf->ssid.ssid_len;
502 
503 	/* Supported rates */
504 	pos = hostapd_eid_supp_rates(hapd, pos);
505 
506 	/* DS Params */
507 	pos = hostapd_eid_ds_params(hapd, pos);
508 
509 	pos = hostapd_eid_country(hapd, pos, epos - pos);
510 
511 	/* Power Constraint element */
512 	pos = hostapd_eid_pwr_constraint(hapd, pos);
513 
514 	/* CSA IE */
515 	csa_pos = hostapd_eid_csa(hapd, pos);
516 	if (csa_pos != pos)
517 		hapd->cs_c_off_proberesp = csa_pos - (u8 *) resp - 1;
518 	pos = csa_pos;
519 
520 	/* ERP Information element */
521 	pos = hostapd_eid_erp_info(hapd, pos);
522 
523 	/* Extended supported rates */
524 	pos = hostapd_eid_ext_supp_rates(hapd, pos);
525 
526 	pos = hostapd_get_rsne(hapd, pos, epos - pos);
527 	pos = hostapd_eid_bss_load(hapd, pos, epos - pos);
528 	pos = hostapd_eid_rm_enabled_capab(hapd, pos, epos - pos);
529 	pos = hostapd_get_mde(hapd, pos, epos - pos);
530 
531 	/* eCSA IE */
532 	csa_pos = hostapd_eid_ecsa(hapd, pos);
533 	if (csa_pos != pos)
534 		hapd->cs_c_off_ecsa_proberesp = csa_pos - (u8 *) resp - 1;
535 	pos = csa_pos;
536 
537 	pos = hostapd_eid_supported_op_classes(hapd, pos);
538 	pos = hostapd_eid_ht_capabilities(hapd, pos);
539 	pos = hostapd_eid_ht_operation(hapd, pos);
540 
541 	pos = hostapd_eid_ext_capab(hapd, pos);
542 
543 	pos = hostapd_eid_time_adv(hapd, pos);
544 	pos = hostapd_eid_time_zone(hapd, pos);
545 
546 	pos = hostapd_eid_interworking(hapd, pos);
547 	pos = hostapd_eid_adv_proto(hapd, pos);
548 	pos = hostapd_eid_roaming_consortium(hapd, pos);
549 
550 #ifdef CONFIG_FST
551 	if (hapd->iface->fst_ies) {
552 		os_memcpy(pos, wpabuf_head(hapd->iface->fst_ies),
553 			  wpabuf_len(hapd->iface->fst_ies));
554 		pos += wpabuf_len(hapd->iface->fst_ies);
555 	}
556 #endif /* CONFIG_FST */
557 
558 #ifdef CONFIG_IEEE80211AC
559 	if (hapd->iconf->ieee80211ac && !hapd->conf->disable_11ac &&
560 	    !is_6ghz_op_class(hapd->iconf->op_class)) {
561 		pos = hostapd_eid_vht_capabilities(hapd, pos, 0);
562 		pos = hostapd_eid_vht_operation(hapd, pos);
563 		pos = hostapd_eid_txpower_envelope(hapd, pos);
564 	}
565 #endif /* CONFIG_IEEE80211AC */
566 
567 #ifdef CONFIG_IEEE80211AX
568 	if (hapd->iconf->ieee80211ax && !hapd->conf->disable_11ax &&
569 	    is_6ghz_op_class(hapd->iconf->op_class))
570 		pos = hostapd_eid_txpower_envelope(hapd, pos);
571 #endif /* CONFIG_IEEE80211AX */
572 
573 	pos = hostapd_eid_wb_chsw_wrapper(hapd, pos);
574 
575 	pos = hostapd_eid_rnr(hapd, pos, WLAN_FC_STYPE_PROBE_RESP);
576 	pos = hostapd_eid_fils_indic(hapd, pos, 0);
577 	pos = hostapd_get_rsnxe(hapd, pos, epos - pos);
578 
579 #ifdef CONFIG_IEEE80211AX
580 	if (hapd->iconf->ieee80211ax && !hapd->conf->disable_11ax) {
581 		pos = hostapd_eid_he_capab(hapd, pos, IEEE80211_MODE_AP);
582 		pos = hostapd_eid_he_operation(hapd, pos);
583 		pos = hostapd_eid_spatial_reuse(hapd, pos);
584 		pos = hostapd_eid_he_mu_edca_parameter_set(hapd, pos);
585 		pos = hostapd_eid_he_6ghz_band_cap(hapd, pos);
586 	}
587 #endif /* CONFIG_IEEE80211AX */
588 
589 #ifdef CONFIG_IEEE80211AC
590 	if (hapd->conf->vendor_vht)
591 		pos = hostapd_eid_vendor_vht(hapd, pos);
592 #endif /* CONFIG_IEEE80211AC */
593 
594 	/* WPA / OSEN */
595 	pos = hostapd_get_wpa_ie(hapd, pos, epos - pos);
596 	pos = hostapd_get_osen_ie(hapd, pos, epos - pos);
597 
598 	/* Wi-Fi Alliance WMM */
599 	pos = hostapd_eid_wmm(hapd, pos);
600 
601 #ifdef CONFIG_WPS
602 	if (hapd->conf->wps_state && hapd->wps_probe_resp_ie) {
603 		os_memcpy(pos, wpabuf_head(hapd->wps_probe_resp_ie),
604 			  wpabuf_len(hapd->wps_probe_resp_ie));
605 		pos += wpabuf_len(hapd->wps_probe_resp_ie);
606 	}
607 #endif /* CONFIG_WPS */
608 
609 #ifdef CONFIG_P2P
610 	if ((hapd->conf->p2p & P2P_ENABLED) && is_p2p &&
611 	    hapd->p2p_probe_resp_ie) {
612 		os_memcpy(pos, wpabuf_head(hapd->p2p_probe_resp_ie),
613 			  wpabuf_len(hapd->p2p_probe_resp_ie));
614 		pos += wpabuf_len(hapd->p2p_probe_resp_ie);
615 	}
616 #endif /* CONFIG_P2P */
617 #ifdef CONFIG_P2P_MANAGER
618 	if ((hapd->conf->p2p & (P2P_MANAGE | P2P_ENABLED | P2P_GROUP_OWNER)) ==
619 	    P2P_MANAGE)
620 		pos = hostapd_eid_p2p_manage(hapd, pos);
621 #endif /* CONFIG_P2P_MANAGER */
622 
623 #ifdef CONFIG_HS20
624 	pos = hostapd_eid_hs20_indication(hapd, pos);
625 #endif /* CONFIG_HS20 */
626 
627 	pos = hostapd_eid_mbo(hapd, pos, (u8 *) resp + buflen - pos);
628 	pos = hostapd_eid_owe_trans(hapd, pos, (u8 *) resp + buflen - pos);
629 	pos = hostapd_eid_dpp_cc(hapd, pos, (u8 *) resp + buflen - pos);
630 
631 	if (hapd->conf->vendor_elements) {
632 		os_memcpy(pos, wpabuf_head(hapd->conf->vendor_elements),
633 			  wpabuf_len(hapd->conf->vendor_elements));
634 		pos += wpabuf_len(hapd->conf->vendor_elements);
635 	}
636 
637 	*resp_len = pos - (u8 *) resp;
638 	return (u8 *) resp;
639 }
640 
641 
642 enum ssid_match_result {
643 	NO_SSID_MATCH,
644 	EXACT_SSID_MATCH,
645 	WILDCARD_SSID_MATCH,
646 	CO_LOCATED_SSID_MATCH,
647 };
648 
649 static enum ssid_match_result ssid_match(struct hostapd_data *hapd,
650 					 const u8 *ssid, size_t ssid_len,
651 					 const u8 *ssid_list,
652 					 size_t ssid_list_len,
653 					 const u8 *short_ssid_list,
654 					 size_t short_ssid_list_len)
655 {
656 	const u8 *pos, *end;
657 	struct hostapd_iface *iface = hapd->iface;
658 	int wildcard = 0;
659 	size_t i, j;
660 
661 	if (ssid_len == 0)
662 		wildcard = 1;
663 	if (ssid_len == hapd->conf->ssid.ssid_len &&
664 	    os_memcmp(ssid, hapd->conf->ssid.ssid, ssid_len) == 0)
665 		return EXACT_SSID_MATCH;
666 
667 	if (ssid_list) {
668 		pos = ssid_list;
669 		end = ssid_list + ssid_list_len;
670 		while (end - pos >= 2) {
671 			if (2 + pos[1] > end - pos)
672 				break;
673 			if (pos[1] == 0)
674 				wildcard = 1;
675 			if (pos[1] == hapd->conf->ssid.ssid_len &&
676 			    os_memcmp(pos + 2, hapd->conf->ssid.ssid,
677 				      pos[1]) == 0)
678 				return EXACT_SSID_MATCH;
679 			pos += 2 + pos[1];
680 		}
681 	}
682 
683 	if (short_ssid_list) {
684 		pos = short_ssid_list;
685 		end = short_ssid_list + short_ssid_list_len;
686 		while (end - pos >= 4) {
687 			if (hapd->conf->ssid.short_ssid == WPA_GET_LE32(pos))
688 				return EXACT_SSID_MATCH;
689 			pos += 4;
690 		}
691 	}
692 
693 	if (wildcard)
694 		return WILDCARD_SSID_MATCH;
695 
696 	if (!iface->interfaces || iface->interfaces->count <= 1 ||
697 	    is_6ghz_op_class(hapd->iconf->op_class))
698 		return NO_SSID_MATCH;
699 
700 	for (i = 0; i < iface->interfaces->count; i++) {
701 		struct hostapd_iface *colocated;
702 
703 		colocated = iface->interfaces->iface[i];
704 
705 		if (colocated == iface ||
706 		    !is_6ghz_op_class(colocated->conf->op_class))
707 			continue;
708 
709 		for (j = 0; j < colocated->num_bss; j++) {
710 			struct hostapd_bss_config *conf;
711 
712 			conf = colocated->bss[j]->conf;
713 			if (ssid_len == conf->ssid.ssid_len &&
714 			    os_memcmp(ssid, conf->ssid.ssid, ssid_len) == 0)
715 				return CO_LOCATED_SSID_MATCH;
716 		}
717 	}
718 
719 	return NO_SSID_MATCH;
720 }
721 
722 
723 void sta_track_expire(struct hostapd_iface *iface, int force)
724 {
725 	struct os_reltime now;
726 	struct hostapd_sta_info *info;
727 
728 	if (!iface->num_sta_seen)
729 		return;
730 
731 	os_get_reltime(&now);
732 	while ((info = dl_list_first(&iface->sta_seen, struct hostapd_sta_info,
733 				     list))) {
734 		if (!force &&
735 		    !os_reltime_expired(&now, &info->last_seen,
736 					iface->conf->track_sta_max_age))
737 			break;
738 		force = 0;
739 
740 		wpa_printf(MSG_MSGDUMP, "%s: Expire STA tracking entry for "
741 			   MACSTR, iface->bss[0]->conf->iface,
742 			   MAC2STR(info->addr));
743 		dl_list_del(&info->list);
744 		iface->num_sta_seen--;
745 		sta_track_del(info);
746 	}
747 }
748 
749 
750 static struct hostapd_sta_info * sta_track_get(struct hostapd_iface *iface,
751 					       const u8 *addr)
752 {
753 	struct hostapd_sta_info *info;
754 
755 	dl_list_for_each(info, &iface->sta_seen, struct hostapd_sta_info, list)
756 		if (os_memcmp(addr, info->addr, ETH_ALEN) == 0)
757 			return info;
758 
759 	return NULL;
760 }
761 
762 
763 void sta_track_add(struct hostapd_iface *iface, const u8 *addr, int ssi_signal)
764 {
765 	struct hostapd_sta_info *info;
766 
767 	info = sta_track_get(iface, addr);
768 	if (info) {
769 		/* Move the most recent entry to the end of the list */
770 		dl_list_del(&info->list);
771 		dl_list_add_tail(&iface->sta_seen, &info->list);
772 		os_get_reltime(&info->last_seen);
773 		info->ssi_signal = ssi_signal;
774 		return;
775 	}
776 
777 	/* Add a new entry */
778 	info = os_zalloc(sizeof(*info));
779 	if (info == NULL)
780 		return;
781 	os_memcpy(info->addr, addr, ETH_ALEN);
782 	os_get_reltime(&info->last_seen);
783 	info->ssi_signal = ssi_signal;
784 
785 	if (iface->num_sta_seen >= iface->conf->track_sta_max_num) {
786 		/* Expire oldest entry to make room for a new one */
787 		sta_track_expire(iface, 1);
788 	}
789 
790 	wpa_printf(MSG_MSGDUMP, "%s: Add STA tracking entry for "
791 		   MACSTR, iface->bss[0]->conf->iface, MAC2STR(addr));
792 	dl_list_add_tail(&iface->sta_seen, &info->list);
793 	iface->num_sta_seen++;
794 }
795 
796 
797 struct hostapd_data *
798 sta_track_seen_on(struct hostapd_iface *iface, const u8 *addr,
799 		  const char *ifname)
800 {
801 	struct hapd_interfaces *interfaces = iface->interfaces;
802 	size_t i, j;
803 
804 	for (i = 0; i < interfaces->count; i++) {
805 		struct hostapd_data *hapd = NULL;
806 
807 		iface = interfaces->iface[i];
808 		for (j = 0; j < iface->num_bss; j++) {
809 			hapd = iface->bss[j];
810 			if (os_strcmp(ifname, hapd->conf->iface) == 0)
811 				break;
812 			hapd = NULL;
813 		}
814 
815 		if (hapd && sta_track_get(iface, addr))
816 			return hapd;
817 	}
818 
819 	return NULL;
820 }
821 
822 
823 #ifdef CONFIG_TAXONOMY
824 void sta_track_claim_taxonomy_info(struct hostapd_iface *iface, const u8 *addr,
825 				   struct wpabuf **probe_ie_taxonomy)
826 {
827 	struct hostapd_sta_info *info;
828 
829 	info = sta_track_get(iface, addr);
830 	if (!info)
831 		return;
832 
833 	wpabuf_free(*probe_ie_taxonomy);
834 	*probe_ie_taxonomy = info->probe_ie_taxonomy;
835 	info->probe_ie_taxonomy = NULL;
836 }
837 #endif /* CONFIG_TAXONOMY */
838 
839 
840 void handle_probe_req(struct hostapd_data *hapd,
841 		      const struct ieee80211_mgmt *mgmt, size_t len,
842 		      int ssi_signal)
843 {
844 	u8 *resp;
845 	struct ieee802_11_elems elems;
846 	const u8 *ie;
847 	size_t ie_len;
848 	size_t i, resp_len;
849 	int noack;
850 	enum ssid_match_result res;
851 	int ret;
852 	u16 csa_offs[2];
853 	size_t csa_offs_len;
854 	struct radius_sta rad_info;
855 
856 	if (hapd->iconf->rssi_ignore_probe_request && ssi_signal &&
857 	    ssi_signal < hapd->iconf->rssi_ignore_probe_request)
858 		return;
859 
860 	if (len < IEEE80211_HDRLEN)
861 		return;
862 	ie = ((const u8 *) mgmt) + IEEE80211_HDRLEN;
863 	if (hapd->iconf->track_sta_max_num)
864 		sta_track_add(hapd->iface, mgmt->sa, ssi_signal);
865 	ie_len = len - IEEE80211_HDRLEN;
866 
867 	ret = hostapd_allowed_address(hapd, mgmt->sa, (const u8 *) mgmt, len,
868 				      &rad_info, 1);
869 	if (ret == HOSTAPD_ACL_REJECT) {
870 		wpa_msg(hapd->msg_ctx, MSG_DEBUG,
871 			"Ignore Probe Request frame from " MACSTR
872 			" due to ACL reject ", MAC2STR(mgmt->sa));
873 		return;
874 	}
875 
876 	for (i = 0; hapd->probereq_cb && i < hapd->num_probereq_cb; i++)
877 		if (hapd->probereq_cb[i].cb(hapd->probereq_cb[i].ctx,
878 					    mgmt->sa, mgmt->da, mgmt->bssid,
879 					    ie, ie_len, ssi_signal) > 0)
880 			return;
881 
882 	if (!hapd->conf->send_probe_response)
883 		return;
884 
885 	if (ieee802_11_parse_elems(ie, ie_len, &elems, 0) == ParseFailed) {
886 		wpa_printf(MSG_DEBUG, "Could not parse ProbeReq from " MACSTR,
887 			   MAC2STR(mgmt->sa));
888 		return;
889 	}
890 
891 	if ((!elems.ssid || !elems.supp_rates)) {
892 		wpa_printf(MSG_DEBUG, "STA " MACSTR " sent probe request "
893 			   "without SSID or supported rates element",
894 			   MAC2STR(mgmt->sa));
895 		return;
896 	}
897 
898 	/*
899 	 * No need to reply if the Probe Request frame was sent on an adjacent
900 	 * channel. IEEE Std 802.11-2012 describes this as a requirement for an
901 	 * AP with dot11RadioMeasurementActivated set to true, but strictly
902 	 * speaking does not allow such ignoring of Probe Request frames if
903 	 * dot11RadioMeasurementActivated is false. Anyway, this can help reduce
904 	 * number of unnecessary Probe Response frames for cases where the STA
905 	 * is less likely to see them (Probe Request frame sent on a
906 	 * neighboring, but partially overlapping, channel).
907 	 */
908 	if (elems.ds_params &&
909 	    hapd->iface->current_mode &&
910 	    (hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G ||
911 	     hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211B) &&
912 	    hapd->iconf->channel != elems.ds_params[0]) {
913 		wpa_printf(MSG_DEBUG,
914 			   "Ignore Probe Request due to DS Params mismatch: chan=%u != ds.chan=%u",
915 			   hapd->iconf->channel, elems.ds_params[0]);
916 		return;
917 	}
918 
919 #ifdef CONFIG_P2P
920 	if (hapd->p2p && hapd->p2p_group && elems.wps_ie) {
921 		struct wpabuf *wps;
922 		wps = ieee802_11_vendor_ie_concat(ie, ie_len, WPS_DEV_OUI_WFA);
923 		if (wps && !p2p_group_match_dev_type(hapd->p2p_group, wps)) {
924 			wpa_printf(MSG_MSGDUMP, "P2P: Ignore Probe Request "
925 				   "due to mismatch with Requested Device "
926 				   "Type");
927 			wpabuf_free(wps);
928 			return;
929 		}
930 		wpabuf_free(wps);
931 	}
932 
933 	if (hapd->p2p && hapd->p2p_group && elems.p2p) {
934 		struct wpabuf *p2p;
935 		p2p = ieee802_11_vendor_ie_concat(ie, ie_len, P2P_IE_VENDOR_TYPE);
936 		if (p2p && !p2p_group_match_dev_id(hapd->p2p_group, p2p)) {
937 			wpa_printf(MSG_MSGDUMP, "P2P: Ignore Probe Request "
938 				   "due to mismatch with Device ID");
939 			wpabuf_free(p2p);
940 			return;
941 		}
942 		wpabuf_free(p2p);
943 	}
944 #endif /* CONFIG_P2P */
945 
946 	if (hapd->conf->ignore_broadcast_ssid && elems.ssid_len == 0 &&
947 	    elems.ssid_list_len == 0 && elems.short_ssid_list_len == 0) {
948 		wpa_printf(MSG_MSGDUMP, "Probe Request from " MACSTR " for "
949 			   "broadcast SSID ignored", MAC2STR(mgmt->sa));
950 		return;
951 	}
952 
953 #ifdef CONFIG_P2P
954 	if ((hapd->conf->p2p & P2P_GROUP_OWNER) &&
955 	    elems.ssid_len == P2P_WILDCARD_SSID_LEN &&
956 	    os_memcmp(elems.ssid, P2P_WILDCARD_SSID,
957 		      P2P_WILDCARD_SSID_LEN) == 0) {
958 		/* Process P2P Wildcard SSID like Wildcard SSID */
959 		elems.ssid_len = 0;
960 	}
961 #endif /* CONFIG_P2P */
962 
963 #ifdef CONFIG_TAXONOMY
964 	{
965 		struct sta_info *sta;
966 		struct hostapd_sta_info *info;
967 
968 		if ((sta = ap_get_sta(hapd, mgmt->sa)) != NULL) {
969 			taxonomy_sta_info_probe_req(hapd, sta, ie, ie_len);
970 		} else if ((info = sta_track_get(hapd->iface,
971 						 mgmt->sa)) != NULL) {
972 			taxonomy_hostapd_sta_info_probe_req(hapd, info,
973 							    ie, ie_len);
974 		}
975 	}
976 #endif /* CONFIG_TAXONOMY */
977 
978 	res = ssid_match(hapd, elems.ssid, elems.ssid_len,
979 			 elems.ssid_list, elems.ssid_list_len,
980 			 elems.short_ssid_list, elems.short_ssid_list_len);
981 	if (res == NO_SSID_MATCH) {
982 		if (!(mgmt->da[0] & 0x01)) {
983 			wpa_printf(MSG_MSGDUMP, "Probe Request from " MACSTR
984 				   " for foreign SSID '%s' (DA " MACSTR ")%s",
985 				   MAC2STR(mgmt->sa),
986 				   wpa_ssid_txt(elems.ssid, elems.ssid_len),
987 				   MAC2STR(mgmt->da),
988 				   elems.ssid_list ? " (SSID list)" : "");
989 		}
990 		return;
991 	}
992 
993 	if (hapd->conf->ignore_broadcast_ssid && res == WILDCARD_SSID_MATCH) {
994 		wpa_printf(MSG_MSGDUMP, "Probe Request from " MACSTR " for "
995 			   "broadcast SSID ignored", MAC2STR(mgmt->sa));
996 		return;
997 	}
998 
999 #ifdef CONFIG_INTERWORKING
1000 	if (hapd->conf->interworking &&
1001 	    elems.interworking && elems.interworking_len >= 1) {
1002 		u8 ant = elems.interworking[0] & 0x0f;
1003 		if (ant != INTERWORKING_ANT_WILDCARD &&
1004 		    ant != hapd->conf->access_network_type) {
1005 			wpa_printf(MSG_MSGDUMP, "Probe Request from " MACSTR
1006 				   " for mismatching ANT %u ignored",
1007 				   MAC2STR(mgmt->sa), ant);
1008 			return;
1009 		}
1010 	}
1011 
1012 	if (hapd->conf->interworking && elems.interworking &&
1013 	    (elems.interworking_len == 7 || elems.interworking_len == 9)) {
1014 		const u8 *hessid;
1015 		if (elems.interworking_len == 7)
1016 			hessid = elems.interworking + 1;
1017 		else
1018 			hessid = elems.interworking + 1 + 2;
1019 		if (!is_broadcast_ether_addr(hessid) &&
1020 		    os_memcmp(hessid, hapd->conf->hessid, ETH_ALEN) != 0) {
1021 			wpa_printf(MSG_MSGDUMP, "Probe Request from " MACSTR
1022 				   " for mismatching HESSID " MACSTR
1023 				   " ignored",
1024 				   MAC2STR(mgmt->sa), MAC2STR(hessid));
1025 			return;
1026 		}
1027 	}
1028 #endif /* CONFIG_INTERWORKING */
1029 
1030 #ifdef CONFIG_P2P
1031 	if ((hapd->conf->p2p & P2P_GROUP_OWNER) &&
1032 	    supp_rates_11b_only(&elems)) {
1033 		/* Indicates support for 11b rates only */
1034 		wpa_printf(MSG_EXCESSIVE, "P2P: Ignore Probe Request from "
1035 			   MACSTR " with only 802.11b rates",
1036 			   MAC2STR(mgmt->sa));
1037 		return;
1038 	}
1039 #endif /* CONFIG_P2P */
1040 
1041 	/* TODO: verify that supp_rates contains at least one matching rate
1042 	 * with AP configuration */
1043 
1044 	if (hapd->conf->no_probe_resp_if_seen_on &&
1045 	    is_multicast_ether_addr(mgmt->da) &&
1046 	    is_multicast_ether_addr(mgmt->bssid) &&
1047 	    sta_track_seen_on(hapd->iface, mgmt->sa,
1048 			      hapd->conf->no_probe_resp_if_seen_on)) {
1049 		wpa_printf(MSG_MSGDUMP, "%s: Ignore Probe Request from " MACSTR
1050 			   " since STA has been seen on %s",
1051 			   hapd->conf->iface, MAC2STR(mgmt->sa),
1052 			   hapd->conf->no_probe_resp_if_seen_on);
1053 		return;
1054 	}
1055 
1056 	if (hapd->conf->no_probe_resp_if_max_sta &&
1057 	    is_multicast_ether_addr(mgmt->da) &&
1058 	    is_multicast_ether_addr(mgmt->bssid) &&
1059 	    hapd->num_sta >= hapd->conf->max_num_sta &&
1060 	    !ap_get_sta(hapd, mgmt->sa)) {
1061 		wpa_printf(MSG_MSGDUMP, "%s: Ignore Probe Request from " MACSTR
1062 			   " since no room for additional STA",
1063 			   hapd->conf->iface, MAC2STR(mgmt->sa));
1064 		return;
1065 	}
1066 
1067 #ifdef CONFIG_TESTING_OPTIONS
1068 	if (hapd->iconf->ignore_probe_probability > 0.0 &&
1069 	    drand48() < hapd->iconf->ignore_probe_probability) {
1070 		wpa_printf(MSG_INFO,
1071 			   "TESTING: ignoring probe request from " MACSTR,
1072 			   MAC2STR(mgmt->sa));
1073 		return;
1074 	}
1075 #endif /* CONFIG_TESTING_OPTIONS */
1076 
1077 	wpa_msg_ctrl(hapd->msg_ctx, MSG_INFO, RX_PROBE_REQUEST "sa=" MACSTR
1078 		     " signal=%d", MAC2STR(mgmt->sa), ssi_signal);
1079 
1080 	resp = hostapd_gen_probe_resp(hapd, mgmt, elems.p2p != NULL,
1081 				      &resp_len);
1082 	if (resp == NULL)
1083 		return;
1084 
1085 	/*
1086 	 * If this is a broadcast probe request, apply no ack policy to avoid
1087 	 * excessive retries.
1088 	 */
1089 	noack = !!(res == WILDCARD_SSID_MATCH &&
1090 		   is_broadcast_ether_addr(mgmt->da));
1091 
1092 	csa_offs_len = 0;
1093 	if (hapd->csa_in_progress) {
1094 		if (hapd->cs_c_off_proberesp)
1095 			csa_offs[csa_offs_len++] =
1096 				hapd->cs_c_off_proberesp;
1097 
1098 		if (hapd->cs_c_off_ecsa_proberesp)
1099 			csa_offs[csa_offs_len++] =
1100 				hapd->cs_c_off_ecsa_proberesp;
1101 	}
1102 
1103 	ret = hostapd_drv_send_mlme(hapd, resp, resp_len, noack,
1104 				    csa_offs_len ? csa_offs : NULL,
1105 				    csa_offs_len, 0);
1106 
1107 	if (ret < 0)
1108 		wpa_printf(MSG_INFO, "handle_probe_req: send failed");
1109 
1110 	os_free(resp);
1111 
1112 	wpa_printf(MSG_EXCESSIVE, "STA " MACSTR " sent probe request for %s "
1113 		   "SSID", MAC2STR(mgmt->sa),
1114 		   elems.ssid_len == 0 ? "broadcast" : "our");
1115 }
1116 
1117 
1118 static u8 * hostapd_probe_resp_offloads(struct hostapd_data *hapd,
1119 					size_t *resp_len)
1120 {
1121 	/* check probe response offloading caps and print warnings */
1122 	if (!(hapd->iface->drv_flags & WPA_DRIVER_FLAGS_PROBE_RESP_OFFLOAD))
1123 		return NULL;
1124 
1125 #ifdef CONFIG_WPS
1126 	if (hapd->conf->wps_state && hapd->wps_probe_resp_ie &&
1127 	    (!(hapd->iface->probe_resp_offloads &
1128 	       (WPA_DRIVER_PROBE_RESP_OFFLOAD_WPS |
1129 		WPA_DRIVER_PROBE_RESP_OFFLOAD_WPS2))))
1130 		wpa_printf(MSG_WARNING, "Device is trying to offload WPS "
1131 			   "Probe Response while not supporting this");
1132 #endif /* CONFIG_WPS */
1133 
1134 #ifdef CONFIG_P2P
1135 	if ((hapd->conf->p2p & P2P_ENABLED) && hapd->p2p_probe_resp_ie &&
1136 	    !(hapd->iface->probe_resp_offloads &
1137 	      WPA_DRIVER_PROBE_RESP_OFFLOAD_P2P))
1138 		wpa_printf(MSG_WARNING, "Device is trying to offload P2P "
1139 			   "Probe Response while not supporting this");
1140 #endif  /* CONFIG_P2P */
1141 
1142 	if (hapd->conf->interworking &&
1143 	    !(hapd->iface->probe_resp_offloads &
1144 	      WPA_DRIVER_PROBE_RESP_OFFLOAD_INTERWORKING))
1145 		wpa_printf(MSG_WARNING, "Device is trying to offload "
1146 			   "Interworking Probe Response while not supporting "
1147 			   "this");
1148 
1149 	/* Generate a Probe Response template for the non-P2P case */
1150 	return hostapd_gen_probe_resp(hapd, NULL, 0, resp_len);
1151 }
1152 
1153 #endif /* NEED_AP_MLME */
1154 
1155 
1156 #ifdef CONFIG_IEEE80211AX
1157 /* Unsolicited broadcast Probe Response transmission, 6 GHz only */
1158 static u8 * hostapd_unsol_bcast_probe_resp(struct hostapd_data *hapd,
1159 					   struct wpa_driver_ap_params *params)
1160 {
1161 	if (!is_6ghz_op_class(hapd->iconf->op_class))
1162 		return NULL;
1163 
1164 	params->unsol_bcast_probe_resp_interval =
1165 		hapd->conf->unsol_bcast_probe_resp_interval;
1166 
1167 	return hostapd_gen_probe_resp(hapd, NULL, 0,
1168 				      &params->unsol_bcast_probe_resp_tmpl_len);
1169 }
1170 #endif /* CONFIG_IEEE80211AX */
1171 
1172 
1173 void sta_track_del(struct hostapd_sta_info *info)
1174 {
1175 #ifdef CONFIG_TAXONOMY
1176 	wpabuf_free(info->probe_ie_taxonomy);
1177 	info->probe_ie_taxonomy = NULL;
1178 #endif /* CONFIG_TAXONOMY */
1179 	os_free(info);
1180 }
1181 
1182 
1183 #ifdef CONFIG_FILS
1184 
1185 static u16 hostapd_fils_discovery_cap(struct hostapd_data *hapd)
1186 {
1187 	u16 cap_info, phy_index = 0;
1188 	u8 chwidth = FD_CAP_BSS_CHWIDTH_20, mcs_nss_size = 4;
1189 	struct hostapd_hw_modes *mode = hapd->iface->current_mode;
1190 
1191 	cap_info = FD_CAP_ESS;
1192 	if (hapd->conf->wpa)
1193 		cap_info |= FD_CAP_PRIVACY;
1194 
1195 	if (is_6ghz_op_class(hapd->iconf->op_class)) {
1196 		phy_index = FD_CAP_PHY_INDEX_HE;
1197 
1198 		switch (hapd->iconf->op_class) {
1199 		case 135:
1200 			mcs_nss_size += 4;
1201 			/* fallthrough */
1202 		case 134:
1203 			mcs_nss_size += 4;
1204 			chwidth = FD_CAP_BSS_CHWIDTH_160_80_80;
1205 			break;
1206 		case 133:
1207 			chwidth = FD_CAP_BSS_CHWIDTH_80;
1208 			break;
1209 		case 132:
1210 			chwidth = FD_CAP_BSS_CHWIDTH_40;
1211 			break;
1212 		}
1213 	} else {
1214 		switch (hostapd_get_oper_chwidth(hapd->iconf)) {
1215 		case CHANWIDTH_80P80MHZ:
1216 			mcs_nss_size += 4;
1217 			/* fallthrough */
1218 		case CHANWIDTH_160MHZ:
1219 			mcs_nss_size += 4;
1220 			chwidth = FD_CAP_BSS_CHWIDTH_160_80_80;
1221 			break;
1222 		case CHANWIDTH_80MHZ:
1223 			chwidth = FD_CAP_BSS_CHWIDTH_80;
1224 			break;
1225 		case CHANWIDTH_USE_HT:
1226 			if (hapd->iconf->secondary_channel)
1227 				chwidth = FD_CAP_BSS_CHWIDTH_40;
1228 			else
1229 				chwidth = FD_CAP_BSS_CHWIDTH_20;
1230 			break;
1231 		}
1232 
1233 #ifdef CONFIG_IEEE80211AX
1234 		if (hapd->iconf->ieee80211ax && !hapd->conf->disable_11ax)
1235 			phy_index = FD_CAP_PHY_INDEX_HE;
1236 #endif /* CONFIG_IEEE80211AX */
1237 #ifdef CONFIG_IEEE80211AC
1238 		if (!phy_index &&
1239 		    hapd->iconf->ieee80211ac && !hapd->conf->disable_11ac)
1240 			phy_index = FD_CAP_PHY_INDEX_VHT;
1241 #endif /* CONFIG_IEEE80211AC */
1242 		if (!phy_index &&
1243 		    hapd->iconf->ieee80211n && !hapd->conf->disable_11n)
1244 			phy_index = FD_CAP_PHY_INDEX_HT;
1245 	}
1246 
1247 	cap_info |= phy_index << FD_CAP_PHY_INDEX_SHIFT;
1248 	cap_info |= chwidth << FD_CAP_BSS_CHWIDTH_SHIFT;
1249 
1250 	if (mode) {
1251 		u16 *mcs = (u16 *) mode->he_capab[IEEE80211_MODE_AP].mcs;
1252 		int i;
1253 		u16 nss = 0;
1254 
1255 		for (i = 0; i < HE_NSS_MAX_STREAMS; i++) {
1256 			u16 nss_mask = 0x3 << (i * 2);
1257 
1258 			if (mcs_nss_size == 4 &&
1259 			    (((mcs[0] & nss_mask) == nss_mask) ||
1260 			     ((mcs[1] & nss_mask) == nss_mask)))
1261 				continue;
1262 
1263 			if (mcs_nss_size == 8 &&
1264 			    (((mcs[2] & nss_mask) == nss_mask) ||
1265 			     ((mcs[3] & nss_mask) == nss_mask)))
1266 				continue;
1267 
1268 			if (mcs_nss_size == 12 &&
1269 			    (((mcs[4] & nss_mask) == nss_mask) ||
1270 			     ((mcs[5] & nss_mask) == nss_mask)))
1271 				continue;
1272 
1273 			nss++;
1274 		}
1275 
1276 		if (nss > 4)
1277 			cap_info |= FD_CAP_NSS_5_8 << FD_CAP_NSS_SHIFT;
1278 		else if (nss)
1279 			cap_info |= (nss - 1) << FD_CAP_NSS_SHIFT;
1280 	}
1281 
1282 	return cap_info;
1283 }
1284 
1285 
1286 static u8 * hostapd_gen_fils_discovery(struct hostapd_data *hapd, size_t *len)
1287 {
1288 	struct ieee80211_mgmt *head;
1289 	const u8 *mobility_domain;
1290 	u8 *pos, *length_pos, buf[200];
1291 	u16 ctl = 0;
1292 	u8 fd_rsn_info[5];
1293 	size_t total_len, buf_len;
1294 
1295 	total_len = 24 + 2 + 12;
1296 
1297 	/* FILS Discovery Frame Control */
1298 	ctl = (sizeof(hapd->conf->ssid.short_ssid) - 1) |
1299 		FD_FRAME_CTL_SHORT_SSID_PRESENT |
1300 		FD_FRAME_CTL_LENGTH_PRESENT |
1301 		FD_FRAME_CTL_CAP_PRESENT;
1302 	total_len += 4 + 1 + 2;
1303 
1304 	/* Check for optional subfields and calculate length */
1305 	if (wpa_auth_write_fd_rsn_info(hapd->wpa_auth, fd_rsn_info)) {
1306 		ctl |= FD_FRAME_CTL_RSN_INFO_PRESENT;
1307 		total_len += sizeof(fd_rsn_info);
1308 	}
1309 
1310 	mobility_domain = hostapd_wpa_ie(hapd, WLAN_EID_MOBILITY_DOMAIN);
1311 	if (mobility_domain) {
1312 		ctl |= FD_FRAME_CTL_MD_PRESENT;
1313 		total_len += 3;
1314 	}
1315 
1316 	total_len += hostapd_eid_rnr_len(hapd, WLAN_FC_STYPE_ACTION);
1317 
1318 	pos = hostapd_eid_fils_indic(hapd, buf, 0);
1319 	buf_len = pos - buf;
1320 	total_len += buf_len;
1321 
1322 	head = os_zalloc(total_len);
1323 	if (!head)
1324 		return NULL;
1325 
1326 	head->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
1327 					   WLAN_FC_STYPE_ACTION);
1328 	os_memset(head->da, 0xff, ETH_ALEN);
1329 	os_memcpy(head->sa, hapd->own_addr, ETH_ALEN);
1330 	os_memcpy(head->bssid, hapd->own_addr, ETH_ALEN);
1331 
1332 	head->u.action.category = WLAN_ACTION_PUBLIC;
1333 	head->u.action.u.public_action.action = WLAN_PA_FILS_DISCOVERY;
1334 
1335 	pos = &head->u.action.u.public_action.variable[0];
1336 
1337 	/* FILS Discovery Information field */
1338 
1339 	/* FILS Discovery Frame Control */
1340 	WPA_PUT_LE16(pos, ctl);
1341 	pos += 2;
1342 
1343 	/* Hardware or low-level driver will fill in the Timestamp value */
1344 	pos += 8;
1345 
1346 	/* Beacon Interval */
1347 	WPA_PUT_LE16(pos, hapd->iconf->beacon_int);
1348 	pos += 2;
1349 
1350 	/* Short SSID */
1351 	WPA_PUT_LE32(pos, hapd->conf->ssid.short_ssid);
1352 	pos += sizeof(hapd->conf->ssid.short_ssid);
1353 
1354 	/* Store position of FILS discovery information element Length field */
1355 	length_pos = pos++;
1356 
1357 	/* FD Capability */
1358 	WPA_PUT_LE16(pos, hostapd_fils_discovery_cap(hapd));
1359 	pos += 2;
1360 
1361 	/* Operating Class - not present */
1362 
1363 	/* Primary Channel - not present */
1364 
1365 	/* AP Configuration Sequence Number - not present */
1366 
1367 	/* Access Network Options - not present */
1368 
1369 	/* FD RSN Information */
1370 	if (ctl & FD_FRAME_CTL_RSN_INFO_PRESENT) {
1371 		os_memcpy(pos, fd_rsn_info, sizeof(fd_rsn_info));
1372 		pos += sizeof(fd_rsn_info);
1373 	}
1374 
1375 	/* Channel Center Frequency Segment 1 - not present */
1376 
1377 	/* Mobility Domain */
1378 	if (ctl & FD_FRAME_CTL_MD_PRESENT) {
1379 		os_memcpy(pos, &mobility_domain[2], 3);
1380 		pos += 3;
1381 	}
1382 
1383 	/* Fill in the Length field value */
1384 	*length_pos = pos - (length_pos + 1);
1385 
1386 	pos = hostapd_eid_rnr(hapd, pos, WLAN_FC_STYPE_ACTION);
1387 
1388 	/* FILS Indication element */
1389 	if (buf_len) {
1390 		os_memcpy(pos, buf, buf_len);
1391 		pos += buf_len;
1392 	}
1393 
1394 	*len = pos - (u8 *) head;
1395 	wpa_hexdump(MSG_DEBUG, "FILS Discovery frame template",
1396 		    head, pos - (u8 *) head);
1397 	return (u8 *) head;
1398 }
1399 
1400 
1401 /* Configure FILS Discovery frame transmission parameters */
1402 static u8 * hostapd_fils_discovery(struct hostapd_data *hapd,
1403 				   struct wpa_driver_ap_params *params)
1404 {
1405 	params->fd_max_int = hapd->conf->fils_discovery_max_int;
1406 	if (is_6ghz_op_class(hapd->iconf->op_class) &&
1407 	    params->fd_max_int > FD_MAX_INTERVAL_6GHZ)
1408 		params->fd_max_int = FD_MAX_INTERVAL_6GHZ;
1409 
1410 	params->fd_min_int = hapd->conf->fils_discovery_min_int;
1411 	if (params->fd_min_int > params->fd_max_int)
1412 		params->fd_min_int = params->fd_max_int;
1413 
1414 	if (params->fd_max_int)
1415 		return hostapd_gen_fils_discovery(hapd,
1416 						  &params->fd_frame_tmpl_len);
1417 
1418 	return NULL;
1419 }
1420 
1421 #endif /* CONFIG_FILS */
1422 
1423 
1424 int ieee802_11_build_ap_params(struct hostapd_data *hapd,
1425 			       struct wpa_driver_ap_params *params)
1426 {
1427 	struct ieee80211_mgmt *head = NULL;
1428 	u8 *tail = NULL;
1429 	size_t head_len = 0, tail_len = 0;
1430 	u8 *resp = NULL;
1431 	size_t resp_len = 0;
1432 #ifdef NEED_AP_MLME
1433 	u16 capab_info;
1434 	u8 *pos, *tailpos, *tailend, *csa_pos;
1435 
1436 #define BEACON_HEAD_BUF_SIZE 256
1437 #define BEACON_TAIL_BUF_SIZE 512
1438 	head = os_zalloc(BEACON_HEAD_BUF_SIZE);
1439 	tail_len = BEACON_TAIL_BUF_SIZE;
1440 #ifdef CONFIG_WPS
1441 	if (hapd->conf->wps_state && hapd->wps_beacon_ie)
1442 		tail_len += wpabuf_len(hapd->wps_beacon_ie);
1443 #endif /* CONFIG_WPS */
1444 #ifdef CONFIG_P2P
1445 	if (hapd->p2p_beacon_ie)
1446 		tail_len += wpabuf_len(hapd->p2p_beacon_ie);
1447 #endif /* CONFIG_P2P */
1448 #ifdef CONFIG_FST
1449 	if (hapd->iface->fst_ies)
1450 		tail_len += wpabuf_len(hapd->iface->fst_ies);
1451 #endif /* CONFIG_FST */
1452 	if (hapd->conf->vendor_elements)
1453 		tail_len += wpabuf_len(hapd->conf->vendor_elements);
1454 
1455 #ifdef CONFIG_IEEE80211AC
1456 	if (hapd->conf->vendor_vht) {
1457 		tail_len += 5 + 2 + sizeof(struct ieee80211_vht_capabilities) +
1458 			2 + sizeof(struct ieee80211_vht_operation);
1459 	}
1460 #endif /* CONFIG_IEEE80211AC */
1461 
1462 #ifdef CONFIG_IEEE80211AX
1463 	if (hapd->iconf->ieee80211ax && !hapd->conf->disable_11ax) {
1464 		tail_len += 3 + sizeof(struct ieee80211_he_capabilities) +
1465 			3 + sizeof(struct ieee80211_he_operation) +
1466 			3 + sizeof(struct ieee80211_he_mu_edca_parameter_set) +
1467 			3 + sizeof(struct ieee80211_spatial_reuse);
1468 		if (is_6ghz_op_class(hapd->iconf->op_class))
1469 			tail_len += sizeof(struct ieee80211_he_6ghz_oper_info) +
1470 				3 + sizeof(struct ieee80211_he_6ghz_band_cap);
1471 	}
1472 #endif /* CONFIG_IEEE80211AX */
1473 
1474 	tail_len += hostapd_eid_rnr_len(hapd, WLAN_FC_STYPE_BEACON);
1475 	tail_len += hostapd_mbo_ie_len(hapd);
1476 	tail_len += hostapd_eid_owe_trans_len(hapd);
1477 	tail_len += hostapd_eid_dpp_cc_len(hapd);
1478 
1479 	tailpos = tail = os_malloc(tail_len);
1480 	if (head == NULL || tail == NULL) {
1481 		wpa_printf(MSG_ERROR, "Failed to set beacon data");
1482 		os_free(head);
1483 		os_free(tail);
1484 		return -1;
1485 	}
1486 	tailend = tail + tail_len;
1487 
1488 	head->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
1489 					   WLAN_FC_STYPE_BEACON);
1490 	head->duration = host_to_le16(0);
1491 	os_memset(head->da, 0xff, ETH_ALEN);
1492 
1493 	os_memcpy(head->sa, hapd->own_addr, ETH_ALEN);
1494 	os_memcpy(head->bssid, hapd->own_addr, ETH_ALEN);
1495 	head->u.beacon.beacon_int =
1496 		host_to_le16(hapd->iconf->beacon_int);
1497 
1498 	/* hardware or low-level driver will setup seq_ctrl and timestamp */
1499 	capab_info = hostapd_own_capab_info(hapd);
1500 	head->u.beacon.capab_info = host_to_le16(capab_info);
1501 	pos = &head->u.beacon.variable[0];
1502 
1503 	/* SSID */
1504 	*pos++ = WLAN_EID_SSID;
1505 	if (hapd->conf->ignore_broadcast_ssid == 2) {
1506 		/* clear the data, but keep the correct length of the SSID */
1507 		*pos++ = hapd->conf->ssid.ssid_len;
1508 		os_memset(pos, 0, hapd->conf->ssid.ssid_len);
1509 		pos += hapd->conf->ssid.ssid_len;
1510 	} else if (hapd->conf->ignore_broadcast_ssid) {
1511 		*pos++ = 0; /* empty SSID */
1512 	} else {
1513 		*pos++ = hapd->conf->ssid.ssid_len;
1514 		os_memcpy(pos, hapd->conf->ssid.ssid,
1515 			  hapd->conf->ssid.ssid_len);
1516 		pos += hapd->conf->ssid.ssid_len;
1517 	}
1518 
1519 	/* Supported rates */
1520 	pos = hostapd_eid_supp_rates(hapd, pos);
1521 
1522 	/* DS Params */
1523 	pos = hostapd_eid_ds_params(hapd, pos);
1524 
1525 	head_len = pos - (u8 *) head;
1526 
1527 	tailpos = hostapd_eid_country(hapd, tailpos, tailend - tailpos);
1528 
1529 	/* Power Constraint element */
1530 	tailpos = hostapd_eid_pwr_constraint(hapd, tailpos);
1531 
1532 	/* CSA IE */
1533 	csa_pos = hostapd_eid_csa(hapd, tailpos);
1534 	if (csa_pos != tailpos)
1535 		hapd->cs_c_off_beacon = csa_pos - tail - 1;
1536 	tailpos = csa_pos;
1537 
1538 	/* ERP Information element */
1539 	tailpos = hostapd_eid_erp_info(hapd, tailpos);
1540 
1541 	/* Extended supported rates */
1542 	tailpos = hostapd_eid_ext_supp_rates(hapd, tailpos);
1543 
1544 	tailpos = hostapd_get_rsne(hapd, tailpos, tailend - tailpos);
1545 	tailpos = hostapd_eid_bss_load(hapd, tailpos, tailend - tailpos);
1546 	tailpos = hostapd_eid_rm_enabled_capab(hapd, tailpos,
1547 					       tailend - tailpos);
1548 	tailpos = hostapd_get_mde(hapd, tailpos, tailend - tailpos);
1549 
1550 	/* eCSA IE */
1551 	csa_pos = hostapd_eid_ecsa(hapd, tailpos);
1552 	if (csa_pos != tailpos)
1553 		hapd->cs_c_off_ecsa_beacon = csa_pos - tail - 1;
1554 	tailpos = csa_pos;
1555 
1556 	tailpos = hostapd_eid_supported_op_classes(hapd, tailpos);
1557 	tailpos = hostapd_eid_ht_capabilities(hapd, tailpos);
1558 	tailpos = hostapd_eid_ht_operation(hapd, tailpos);
1559 
1560 	tailpos = hostapd_eid_ext_capab(hapd, tailpos);
1561 
1562 	/*
1563 	 * TODO: Time Advertisement element should only be included in some
1564 	 * DTIM Beacon frames.
1565 	 */
1566 	tailpos = hostapd_eid_time_adv(hapd, tailpos);
1567 
1568 	tailpos = hostapd_eid_interworking(hapd, tailpos);
1569 	tailpos = hostapd_eid_adv_proto(hapd, tailpos);
1570 	tailpos = hostapd_eid_roaming_consortium(hapd, tailpos);
1571 
1572 #ifdef CONFIG_FST
1573 	if (hapd->iface->fst_ies) {
1574 		os_memcpy(tailpos, wpabuf_head(hapd->iface->fst_ies),
1575 			  wpabuf_len(hapd->iface->fst_ies));
1576 		tailpos += wpabuf_len(hapd->iface->fst_ies);
1577 	}
1578 #endif /* CONFIG_FST */
1579 
1580 #ifdef CONFIG_IEEE80211AC
1581 	if (hapd->iconf->ieee80211ac && !hapd->conf->disable_11ac &&
1582 	    !is_6ghz_op_class(hapd->iconf->op_class)) {
1583 		tailpos = hostapd_eid_vht_capabilities(hapd, tailpos, 0);
1584 		tailpos = hostapd_eid_vht_operation(hapd, tailpos);
1585 		tailpos = hostapd_eid_txpower_envelope(hapd, tailpos);
1586 	}
1587 #endif /* CONFIG_IEEE80211AC */
1588 
1589 #ifdef CONFIG_IEEE80211AX
1590 	if (hapd->iconf->ieee80211ax && !hapd->conf->disable_11ax &&
1591 	    is_6ghz_op_class(hapd->iconf->op_class))
1592 		tailpos = hostapd_eid_txpower_envelope(hapd, tailpos);
1593 #endif /* CONFIG_IEEE80211AX */
1594 
1595 	tailpos = hostapd_eid_wb_chsw_wrapper(hapd, tailpos);
1596 
1597 	tailpos = hostapd_eid_rnr(hapd, tailpos, WLAN_FC_STYPE_BEACON);
1598 	tailpos = hostapd_eid_fils_indic(hapd, tailpos, 0);
1599 	tailpos = hostapd_get_rsnxe(hapd, tailpos, tailend - tailpos);
1600 
1601 #ifdef CONFIG_IEEE80211AX
1602 	if (hapd->iconf->ieee80211ax && !hapd->conf->disable_11ax) {
1603 		tailpos = hostapd_eid_he_capab(hapd, tailpos,
1604 					       IEEE80211_MODE_AP);
1605 		tailpos = hostapd_eid_he_operation(hapd, tailpos);
1606 		tailpos = hostapd_eid_spatial_reuse(hapd, tailpos);
1607 		tailpos = hostapd_eid_he_mu_edca_parameter_set(hapd, tailpos);
1608 		tailpos = hostapd_eid_he_6ghz_band_cap(hapd, tailpos);
1609 	}
1610 #endif /* CONFIG_IEEE80211AX */
1611 
1612 #ifdef CONFIG_IEEE80211AC
1613 	if (hapd->conf->vendor_vht)
1614 		tailpos = hostapd_eid_vendor_vht(hapd, tailpos);
1615 #endif /* CONFIG_IEEE80211AC */
1616 
1617 	/* WPA / OSEN */
1618 	tailpos = hostapd_get_wpa_ie(hapd, tailpos, tailend - tailpos);
1619 	tailpos = hostapd_get_osen_ie(hapd, tailpos, tailend - tailpos);
1620 
1621 	/* Wi-Fi Alliance WMM */
1622 	tailpos = hostapd_eid_wmm(hapd, tailpos);
1623 
1624 #ifdef CONFIG_WPS
1625 	if (hapd->conf->wps_state && hapd->wps_beacon_ie) {
1626 		os_memcpy(tailpos, wpabuf_head(hapd->wps_beacon_ie),
1627 			  wpabuf_len(hapd->wps_beacon_ie));
1628 		tailpos += wpabuf_len(hapd->wps_beacon_ie);
1629 	}
1630 #endif /* CONFIG_WPS */
1631 
1632 #ifdef CONFIG_P2P
1633 	if ((hapd->conf->p2p & P2P_ENABLED) && hapd->p2p_beacon_ie) {
1634 		os_memcpy(tailpos, wpabuf_head(hapd->p2p_beacon_ie),
1635 			  wpabuf_len(hapd->p2p_beacon_ie));
1636 		tailpos += wpabuf_len(hapd->p2p_beacon_ie);
1637 	}
1638 #endif /* CONFIG_P2P */
1639 #ifdef CONFIG_P2P_MANAGER
1640 	if ((hapd->conf->p2p & (P2P_MANAGE | P2P_ENABLED | P2P_GROUP_OWNER)) ==
1641 	    P2P_MANAGE)
1642 		tailpos = hostapd_eid_p2p_manage(hapd, tailpos);
1643 #endif /* CONFIG_P2P_MANAGER */
1644 
1645 #ifdef CONFIG_HS20
1646 	tailpos = hostapd_eid_hs20_indication(hapd, tailpos);
1647 #endif /* CONFIG_HS20 */
1648 
1649 	tailpos = hostapd_eid_mbo(hapd, tailpos, tail + tail_len - tailpos);
1650 	tailpos = hostapd_eid_owe_trans(hapd, tailpos,
1651 					tail + tail_len - tailpos);
1652 	tailpos = hostapd_eid_dpp_cc(hapd, tailpos, tail + tail_len - tailpos);
1653 
1654 	if (hapd->conf->vendor_elements) {
1655 		os_memcpy(tailpos, wpabuf_head(hapd->conf->vendor_elements),
1656 			  wpabuf_len(hapd->conf->vendor_elements));
1657 		tailpos += wpabuf_len(hapd->conf->vendor_elements);
1658 	}
1659 
1660 	tail_len = tailpos > tail ? tailpos - tail : 0;
1661 
1662 	resp = hostapd_probe_resp_offloads(hapd, &resp_len);
1663 #endif /* NEED_AP_MLME */
1664 
1665 	os_memset(params, 0, sizeof(*params));
1666 	params->head = (u8 *) head;
1667 	params->head_len = head_len;
1668 	params->tail = tail;
1669 	params->tail_len = tail_len;
1670 	params->proberesp = resp;
1671 	params->proberesp_len = resp_len;
1672 	params->dtim_period = hapd->conf->dtim_period;
1673 	params->beacon_int = hapd->iconf->beacon_int;
1674 	params->basic_rates = hapd->iface->basic_rates;
1675 	params->beacon_rate = hapd->iconf->beacon_rate;
1676 	params->rate_type = hapd->iconf->rate_type;
1677 	params->ssid = hapd->conf->ssid.ssid;
1678 	params->ssid_len = hapd->conf->ssid.ssid_len;
1679 	if ((hapd->conf->wpa & (WPA_PROTO_WPA | WPA_PROTO_RSN)) ==
1680 	    (WPA_PROTO_WPA | WPA_PROTO_RSN))
1681 		params->pairwise_ciphers = hapd->conf->wpa_pairwise |
1682 			hapd->conf->rsn_pairwise;
1683 	else if (hapd->conf->wpa & WPA_PROTO_RSN)
1684 		params->pairwise_ciphers = hapd->conf->rsn_pairwise;
1685 	else if (hapd->conf->wpa & WPA_PROTO_WPA)
1686 		params->pairwise_ciphers = hapd->conf->wpa_pairwise;
1687 	params->group_cipher = hapd->conf->wpa_group;
1688 	params->key_mgmt_suites = hapd->conf->wpa_key_mgmt;
1689 	params->auth_algs = hapd->conf->auth_algs;
1690 	params->wpa_version = hapd->conf->wpa;
1691 	params->privacy = hapd->conf->wpa;
1692 #ifdef CONFIG_WEP
1693 	params->privacy |= hapd->conf->ssid.wep.keys_set ||
1694 		(hapd->conf->ieee802_1x &&
1695 		 (hapd->conf->default_wep_key_len ||
1696 		  hapd->conf->individual_wep_key_len));
1697 #endif /* CONFIG_WEP */
1698 	switch (hapd->conf->ignore_broadcast_ssid) {
1699 	case 0:
1700 		params->hide_ssid = NO_SSID_HIDING;
1701 		break;
1702 	case 1:
1703 		params->hide_ssid = HIDDEN_SSID_ZERO_LEN;
1704 		break;
1705 	case 2:
1706 		params->hide_ssid = HIDDEN_SSID_ZERO_CONTENTS;
1707 		break;
1708 	}
1709 	params->isolate = hapd->conf->isolate;
1710 #ifdef NEED_AP_MLME
1711 	params->cts_protect = !!(ieee802_11_erp_info(hapd) &
1712 				ERP_INFO_USE_PROTECTION);
1713 	params->preamble = hapd->iface->num_sta_no_short_preamble == 0 &&
1714 		hapd->iconf->preamble == SHORT_PREAMBLE;
1715 	if (hapd->iface->current_mode &&
1716 	    hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G)
1717 		params->short_slot_time =
1718 			hapd->iface->num_sta_no_short_slot_time > 0 ? 0 : 1;
1719 	else
1720 		params->short_slot_time = -1;
1721 	if (!hapd->iconf->ieee80211n || hapd->conf->disable_11n)
1722 		params->ht_opmode = -1;
1723 	else
1724 		params->ht_opmode = hapd->iface->ht_op_mode;
1725 #endif /* NEED_AP_MLME */
1726 	params->interworking = hapd->conf->interworking;
1727 	if (hapd->conf->interworking &&
1728 	    !is_zero_ether_addr(hapd->conf->hessid))
1729 		params->hessid = hapd->conf->hessid;
1730 	params->access_network_type = hapd->conf->access_network_type;
1731 	params->ap_max_inactivity = hapd->conf->ap_max_inactivity;
1732 #ifdef CONFIG_P2P
1733 	params->p2p_go_ctwindow = hapd->iconf->p2p_go_ctwindow;
1734 #endif /* CONFIG_P2P */
1735 #ifdef CONFIG_HS20
1736 	params->disable_dgaf = hapd->conf->disable_dgaf;
1737 	if (hapd->conf->osen) {
1738 		params->privacy = 1;
1739 		params->osen = 1;
1740 	}
1741 #endif /* CONFIG_HS20 */
1742 	params->multicast_to_unicast = hapd->conf->multicast_to_unicast;
1743 	params->pbss = hapd->conf->pbss;
1744 
1745 	if (hapd->conf->ftm_responder) {
1746 		if (hapd->iface->drv_flags & WPA_DRIVER_FLAGS_FTM_RESPONDER) {
1747 			params->ftm_responder = 1;
1748 			params->lci = hapd->iface->conf->lci;
1749 			params->civic = hapd->iface->conf->civic;
1750 		} else {
1751 			wpa_printf(MSG_WARNING,
1752 				   "Not configuring FTM responder as the driver doesn't advertise support for it");
1753 		}
1754 	}
1755 
1756 	return 0;
1757 }
1758 
1759 
1760 void ieee802_11_free_ap_params(struct wpa_driver_ap_params *params)
1761 {
1762 	os_free(params->tail);
1763 	params->tail = NULL;
1764 	os_free(params->head);
1765 	params->head = NULL;
1766 	os_free(params->proberesp);
1767 	params->proberesp = NULL;
1768 #ifdef CONFIG_FILS
1769 	os_free(params->fd_frame_tmpl);
1770 	params->fd_frame_tmpl = NULL;
1771 #endif /* CONFIG_FILS */
1772 #ifdef CONFIG_IEEE80211AX
1773 	os_free(params->unsol_bcast_probe_resp_tmpl);
1774 	params->unsol_bcast_probe_resp_tmpl = NULL;
1775 #endif /* CONFIG_IEEE80211AX */
1776 }
1777 
1778 
1779 static int __ieee802_11_set_beacon(struct hostapd_data *hapd)
1780 {
1781 	struct wpa_driver_ap_params params;
1782 	struct hostapd_freq_params freq;
1783 	struct hostapd_iface *iface = hapd->iface;
1784 	struct hostapd_config *iconf = iface->conf;
1785 	struct hostapd_hw_modes *cmode = iface->current_mode;
1786 	struct wpabuf *beacon, *proberesp, *assocresp;
1787 	int res, ret = -1;
1788 
1789 	if (!hapd->drv_priv) {
1790 		wpa_printf(MSG_ERROR, "Interface is disabled");
1791 		return -1;
1792 	}
1793 
1794 	if (hapd->csa_in_progress) {
1795 		wpa_printf(MSG_ERROR, "Cannot set beacons during CSA period");
1796 		return -1;
1797 	}
1798 
1799 	hapd->beacon_set_done = 1;
1800 
1801 	if (ieee802_11_build_ap_params(hapd, &params) < 0)
1802 		return -1;
1803 
1804 	if (hostapd_build_ap_extra_ies(hapd, &beacon, &proberesp, &assocresp) <
1805 	    0)
1806 		goto fail;
1807 
1808 	params.beacon_ies = beacon;
1809 	params.proberesp_ies = proberesp;
1810 	params.assocresp_ies = assocresp;
1811 	params.reenable = hapd->reenable_beacon;
1812 #ifdef CONFIG_IEEE80211AX
1813 	params.he_spr_ctrl = hapd->iface->conf->spr.sr_control;
1814 	params.he_spr_non_srg_obss_pd_max_offset =
1815 		hapd->iface->conf->spr.non_srg_obss_pd_max_offset;
1816 	params.he_spr_srg_obss_pd_min_offset =
1817 		hapd->iface->conf->spr.srg_obss_pd_min_offset;
1818 	params.he_spr_srg_obss_pd_max_offset =
1819 		hapd->iface->conf->spr.srg_obss_pd_max_offset;
1820 	os_memcpy(params.he_spr_bss_color_bitmap,
1821 		  hapd->iface->conf->spr.srg_bss_color_bitmap, 8);
1822 	os_memcpy(params.he_spr_partial_bssid_bitmap,
1823 		  hapd->iface->conf->spr.srg_partial_bssid_bitmap, 8);
1824 	params.he_bss_color_disabled =
1825 		hapd->iface->conf->he_op.he_bss_color_disabled;
1826 	params.he_bss_color_partial =
1827 		hapd->iface->conf->he_op.he_bss_color_partial;
1828 	params.he_bss_color = hapd->iface->conf->he_op.he_bss_color;
1829 	params.twt_responder = hostapd_get_he_twt_responder(hapd,
1830 							    IEEE80211_MODE_AP);
1831 	params.unsol_bcast_probe_resp_tmpl =
1832 		hostapd_unsol_bcast_probe_resp(hapd, &params);
1833 #endif /* CONFIG_IEEE80211AX */
1834 	hapd->reenable_beacon = 0;
1835 #ifdef CONFIG_SAE
1836 	params.sae_pwe = hapd->conf->sae_pwe;
1837 #endif /* CONFIG_SAE */
1838 
1839 #ifdef CONFIG_FILS
1840 	params.fd_frame_tmpl = hostapd_fils_discovery(hapd, &params);
1841 #endif /* CONFIG_FILS */
1842 
1843 	if (cmode &&
1844 	    hostapd_set_freq_params(&freq, iconf->hw_mode, iface->freq,
1845 				    iconf->channel, iconf->enable_edmg,
1846 				    iconf->edmg_channel, iconf->ieee80211n,
1847 				    iconf->ieee80211ac, iconf->ieee80211ax,
1848 				    iconf->secondary_channel,
1849 				    hostapd_get_oper_chwidth(iconf),
1850 				    hostapd_get_oper_centr_freq_seg0_idx(iconf),
1851 				    hostapd_get_oper_centr_freq_seg1_idx(iconf),
1852 				    cmode->vht_capab,
1853 				    &cmode->he_capab[IEEE80211_MODE_AP]) == 0)
1854 		params.freq = &freq;
1855 
1856 	res = hostapd_drv_set_ap(hapd, &params);
1857 	hostapd_free_ap_extra_ies(hapd, beacon, proberesp, assocresp);
1858 	if (res)
1859 		wpa_printf(MSG_ERROR, "Failed to set beacon parameters");
1860 	else
1861 		ret = 0;
1862 fail:
1863 	ieee802_11_free_ap_params(&params);
1864 	return ret;
1865 }
1866 
1867 
1868 int ieee802_11_set_beacon(struct hostapd_data *hapd)
1869 {
1870 	struct hostapd_iface *iface = hapd->iface;
1871 	int ret;
1872 	size_t i, j;
1873 	bool is_6g;
1874 
1875 	ret = __ieee802_11_set_beacon(hapd);
1876 	if (ret != 0)
1877 		return ret;
1878 
1879 	if (!iface->interfaces || iface->interfaces->count <= 1)
1880 		return 0;
1881 
1882 	/* Update Beacon frames in case of 6 GHz colocation */
1883 	is_6g = is_6ghz_op_class(iface->conf->op_class);
1884 	for (j = 0; j < iface->interfaces->count; j++) {
1885 		struct hostapd_iface *colocated;
1886 
1887 		colocated = iface->interfaces->iface[j];
1888 		if (colocated == iface || !colocated || !colocated->conf)
1889 			continue;
1890 
1891 		if (is_6g == is_6ghz_op_class(colocated->conf->op_class))
1892 			continue;
1893 
1894 		for (i = 0; i < colocated->num_bss; i++) {
1895 			if (colocated->bss[i] && colocated->bss[i]->started)
1896 				__ieee802_11_set_beacon(colocated->bss[i]);
1897 		}
1898 	}
1899 
1900 	return 0;
1901 }
1902 
1903 
1904 int ieee802_11_set_beacons(struct hostapd_iface *iface)
1905 {
1906 	size_t i;
1907 	int ret = 0;
1908 
1909 	for (i = 0; i < iface->num_bss; i++) {
1910 		if (iface->bss[i]->started &&
1911 		    ieee802_11_set_beacon(iface->bss[i]) < 0)
1912 			ret = -1;
1913 	}
1914 
1915 	return ret;
1916 }
1917 
1918 
1919 /* only update beacons if started */
1920 int ieee802_11_update_beacons(struct hostapd_iface *iface)
1921 {
1922 	size_t i;
1923 	int ret = 0;
1924 
1925 	for (i = 0; i < iface->num_bss; i++) {
1926 		if (iface->bss[i]->beacon_set_done && iface->bss[i]->started &&
1927 		    ieee802_11_set_beacon(iface->bss[i]) < 0)
1928 			ret = -1;
1929 	}
1930 
1931 	return ret;
1932 }
1933 
1934 #endif /* CONFIG_NATIVE_WINDOWS */
1935