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_rm_enabled_capab(struct hostapd_data *hapd, u8 *eid,
40 					 size_t len)
41 {
42 	size_t i;
43 
44 	for (i = 0; i < RRM_CAPABILITIES_IE_LEN; i++) {
45 		if (hapd->conf->radio_measurements[i])
46 			break;
47 	}
48 
49 	if (i == RRM_CAPABILITIES_IE_LEN || len < 2 + RRM_CAPABILITIES_IE_LEN)
50 		return eid;
51 
52 	*eid++ = WLAN_EID_RRM_ENABLED_CAPABILITIES;
53 	*eid++ = RRM_CAPABILITIES_IE_LEN;
54 	os_memcpy(eid, hapd->conf->radio_measurements, RRM_CAPABILITIES_IE_LEN);
55 
56 	return eid + RRM_CAPABILITIES_IE_LEN;
57 }
58 
59 
60 static u8 * hostapd_eid_bss_load(struct hostapd_data *hapd, u8 *eid, size_t len)
61 {
62 	if (len < 2 + 5)
63 		return eid;
64 
65 #ifdef CONFIG_TESTING_OPTIONS
66 	if (hapd->conf->bss_load_test_set) {
67 		*eid++ = WLAN_EID_BSS_LOAD;
68 		*eid++ = 5;
69 		os_memcpy(eid, hapd->conf->bss_load_test, 5);
70 		eid += 5;
71 		return eid;
72 	}
73 #endif /* CONFIG_TESTING_OPTIONS */
74 	if (hapd->conf->bss_load_update_period) {
75 		*eid++ = WLAN_EID_BSS_LOAD;
76 		*eid++ = 5;
77 		WPA_PUT_LE16(eid, hapd->num_sta);
78 		eid += 2;
79 		*eid++ = hapd->iface->channel_utilization;
80 		WPA_PUT_LE16(eid, 0); /* no available admission capabity */
81 		eid += 2;
82 	}
83 	return eid;
84 }
85 
86 
87 static u8 ieee802_11_erp_info(struct hostapd_data *hapd)
88 {
89 	u8 erp = 0;
90 
91 	if (hapd->iface->current_mode == NULL ||
92 	    hapd->iface->current_mode->mode != HOSTAPD_MODE_IEEE80211G)
93 		return 0;
94 
95 	if (hapd->iface->olbc)
96 		erp |= ERP_INFO_USE_PROTECTION;
97 	if (hapd->iface->num_sta_non_erp > 0) {
98 		erp |= ERP_INFO_NON_ERP_PRESENT |
99 			ERP_INFO_USE_PROTECTION;
100 	}
101 	if (hapd->iface->num_sta_no_short_preamble > 0 ||
102 	    hapd->iconf->preamble == LONG_PREAMBLE)
103 		erp |= ERP_INFO_BARKER_PREAMBLE_MODE;
104 
105 	return erp;
106 }
107 
108 
109 static u8 * hostapd_eid_ds_params(struct hostapd_data *hapd, u8 *eid)
110 {
111 	*eid++ = WLAN_EID_DS_PARAMS;
112 	*eid++ = 1;
113 	*eid++ = hapd->iconf->channel;
114 	return eid;
115 }
116 
117 
118 static u8 * hostapd_eid_erp_info(struct hostapd_data *hapd, u8 *eid)
119 {
120 	if (hapd->iface->current_mode == NULL ||
121 	    hapd->iface->current_mode->mode != HOSTAPD_MODE_IEEE80211G)
122 		return eid;
123 
124 	/* Set NonERP_present and use_protection bits if there
125 	 * are any associated NonERP stations. */
126 	/* TODO: use_protection bit can be set to zero even if
127 	 * there are NonERP stations present. This optimization
128 	 * might be useful if NonERP stations are "quiet".
129 	 * See 802.11g/D6 E-1 for recommended practice.
130 	 * In addition, Non ERP present might be set, if AP detects Non ERP
131 	 * operation on other APs. */
132 
133 	/* Add ERP Information element */
134 	*eid++ = WLAN_EID_ERP_INFO;
135 	*eid++ = 1;
136 	*eid++ = ieee802_11_erp_info(hapd);
137 
138 	return eid;
139 }
140 
141 
142 static u8 * hostapd_eid_pwr_constraint(struct hostapd_data *hapd, u8 *eid)
143 {
144 	u8 *pos = eid;
145 	u8 local_pwr_constraint = 0;
146 	int dfs;
147 
148 	if (hapd->iface->current_mode == NULL ||
149 	    hapd->iface->current_mode->mode != HOSTAPD_MODE_IEEE80211A)
150 		return eid;
151 
152 	/* Let host drivers add this IE if DFS support is offloaded */
153 	if (hapd->iface->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD)
154 		return eid;
155 
156 	/*
157 	 * There is no DFS support and power constraint was not directly
158 	 * requested by config option.
159 	 */
160 	if (!hapd->iconf->ieee80211h &&
161 	    hapd->iconf->local_pwr_constraint == -1)
162 		return eid;
163 
164 	/* Check if DFS is required by regulatory. */
165 	dfs = hostapd_is_dfs_required(hapd->iface);
166 	if (dfs < 0) {
167 		wpa_printf(MSG_WARNING, "Failed to check if DFS is required; ret=%d",
168 			   dfs);
169 		dfs = 0;
170 	}
171 
172 	if (dfs == 0 && hapd->iconf->local_pwr_constraint == -1)
173 		return eid;
174 
175 	/*
176 	 * ieee80211h (DFS) is enabled so Power Constraint element shall
177 	 * be added when running on DFS channel whenever local_pwr_constraint
178 	 * is configured or not. In order to meet regulations when TPC is not
179 	 * implemented using a transmit power that is below the legal maximum
180 	 * (including any mitigation factor) should help. In this case,
181 	 * indicate 3 dB below maximum allowed transmit power.
182 	 */
183 	if (hapd->iconf->local_pwr_constraint == -1)
184 		local_pwr_constraint = 3;
185 
186 	/*
187 	 * A STA that is not an AP shall use a transmit power less than or
188 	 * equal to the local maximum transmit power level for the channel.
189 	 * The local maximum transmit power can be calculated from the formula:
190 	 * local max TX pwr = max TX pwr - local pwr constraint
191 	 * Where max TX pwr is maximum transmit power level specified for
192 	 * channel in Country element and local pwr constraint is specified
193 	 * for channel in this Power Constraint element.
194 	 */
195 
196 	/* Element ID */
197 	*pos++ = WLAN_EID_PWR_CONSTRAINT;
198 	/* Length */
199 	*pos++ = 1;
200 	/* Local Power Constraint */
201 	if (local_pwr_constraint)
202 		*pos++ = local_pwr_constraint;
203 	else
204 		*pos++ = hapd->iconf->local_pwr_constraint;
205 
206 	return pos;
207 }
208 
209 
210 static u8 * hostapd_eid_country_add(u8 *pos, u8 *end, int chan_spacing,
211 				    struct hostapd_channel_data *start,
212 				    struct hostapd_channel_data *prev)
213 {
214 	if (end - pos < 3)
215 		return pos;
216 
217 	/* first channel number */
218 	*pos++ = start->chan;
219 	/* number of channels */
220 	*pos++ = (prev->chan - start->chan) / chan_spacing + 1;
221 	/* maximum transmit power level */
222 	*pos++ = start->max_tx_power;
223 
224 	return pos;
225 }
226 
227 
228 static u8 * hostapd_eid_country(struct hostapd_data *hapd, u8 *eid,
229 				int max_len)
230 {
231 	u8 *pos = eid;
232 	u8 *end = eid + max_len;
233 	int i;
234 	struct hostapd_hw_modes *mode;
235 	struct hostapd_channel_data *start, *prev;
236 	int chan_spacing = 1;
237 
238 	if (!hapd->iconf->ieee80211d || max_len < 6 ||
239 	    hapd->iface->current_mode == NULL)
240 		return eid;
241 
242 	*pos++ = WLAN_EID_COUNTRY;
243 	pos++; /* length will be set later */
244 	os_memcpy(pos, hapd->iconf->country, 3); /* e.g., 'US ' */
245 	pos += 3;
246 
247 	mode = hapd->iface->current_mode;
248 	if (mode->mode == HOSTAPD_MODE_IEEE80211A)
249 		chan_spacing = 4;
250 
251 	start = prev = NULL;
252 	for (i = 0; i < mode->num_channels; i++) {
253 		struct hostapd_channel_data *chan = &mode->channels[i];
254 		if (chan->flag & HOSTAPD_CHAN_DISABLED)
255 			continue;
256 		if (start && prev &&
257 		    prev->chan + chan_spacing == chan->chan &&
258 		    start->max_tx_power == chan->max_tx_power) {
259 			prev = chan;
260 			continue; /* can use same entry */
261 		}
262 
263 		if (start && prev) {
264 			pos = hostapd_eid_country_add(pos, end, chan_spacing,
265 						      start, prev);
266 			start = NULL;
267 		}
268 
269 		/* Start new group */
270 		start = prev = chan;
271 	}
272 
273 	if (start) {
274 		pos = hostapd_eid_country_add(pos, end, chan_spacing,
275 					      start, prev);
276 	}
277 
278 	if ((pos - eid) & 1) {
279 		if (end - pos < 1)
280 			return eid;
281 		*pos++ = 0; /* pad for 16-bit alignment */
282 	}
283 
284 	eid[1] = (pos - eid) - 2;
285 
286 	return pos;
287 }
288 
289 
290 static u8 * hostapd_eid_wpa(struct hostapd_data *hapd, u8 *eid, size_t len)
291 {
292 	const u8 *ie;
293 	size_t ielen;
294 
295 	ie = wpa_auth_get_wpa_ie(hapd->wpa_auth, &ielen);
296 	if (ie == NULL || ielen > len)
297 		return eid;
298 
299 	os_memcpy(eid, ie, ielen);
300 	return eid + ielen;
301 }
302 
303 
304 static u8 * hostapd_eid_csa(struct hostapd_data *hapd, u8 *eid)
305 {
306 #ifdef CONFIG_TESTING_OPTIONS
307 	if (hapd->iface->cs_oper_class && hapd->iconf->ecsa_ie_only)
308 		return eid;
309 #endif /* CONFIG_TESTING_OPTIONS */
310 
311 	if (!hapd->cs_freq_params.channel)
312 		return eid;
313 
314 	*eid++ = WLAN_EID_CHANNEL_SWITCH;
315 	*eid++ = 3;
316 	*eid++ = hapd->cs_block_tx;
317 	*eid++ = hapd->cs_freq_params.channel;
318 	*eid++ = hapd->cs_count;
319 
320 	return eid;
321 }
322 
323 
324 static u8 * hostapd_eid_ecsa(struct hostapd_data *hapd, u8 *eid)
325 {
326 	if (!hapd->cs_freq_params.channel || !hapd->iface->cs_oper_class)
327 		return eid;
328 
329 	*eid++ = WLAN_EID_EXT_CHANSWITCH_ANN;
330 	*eid++ = 4;
331 	*eid++ = hapd->cs_block_tx;
332 	*eid++ = hapd->iface->cs_oper_class;
333 	*eid++ = hapd->cs_freq_params.channel;
334 	*eid++ = hapd->cs_count;
335 
336 	return eid;
337 }
338 
339 
340 static u8 * hostapd_eid_supported_op_classes(struct hostapd_data *hapd, u8 *eid)
341 {
342 	u8 op_class, channel;
343 
344 	if (!(hapd->iface->drv_flags & WPA_DRIVER_FLAGS_AP_CSA) ||
345 	    !hapd->iface->freq)
346 		return eid;
347 
348 	if (ieee80211_freq_to_channel_ext(hapd->iface->freq,
349 					  hapd->iconf->secondary_channel,
350 					  hostapd_get_oper_chwidth(hapd->iconf),
351 					  &op_class, &channel) ==
352 	    NUM_HOSTAPD_MODES)
353 		return eid;
354 
355 	*eid++ = WLAN_EID_SUPPORTED_OPERATING_CLASSES;
356 	*eid++ = 2;
357 
358 	/* Current Operating Class */
359 	*eid++ = op_class;
360 
361 	/* TODO: Advertise all the supported operating classes */
362 	*eid++ = 0;
363 
364 	return eid;
365 }
366 
367 
368 static u8 * hostapd_gen_probe_resp(struct hostapd_data *hapd,
369 				   const struct ieee80211_mgmt *req,
370 				   int is_p2p, size_t *resp_len)
371 {
372 	struct ieee80211_mgmt *resp;
373 	u8 *pos, *epos, *csa_pos;
374 	size_t buflen;
375 
376 #define MAX_PROBERESP_LEN 768
377 	buflen = MAX_PROBERESP_LEN;
378 #ifdef CONFIG_WPS
379 	if (hapd->wps_probe_resp_ie)
380 		buflen += wpabuf_len(hapd->wps_probe_resp_ie);
381 #endif /* CONFIG_WPS */
382 #ifdef CONFIG_P2P
383 	if (hapd->p2p_probe_resp_ie)
384 		buflen += wpabuf_len(hapd->p2p_probe_resp_ie);
385 #endif /* CONFIG_P2P */
386 #ifdef CONFIG_FST
387 	if (hapd->iface->fst_ies)
388 		buflen += wpabuf_len(hapd->iface->fst_ies);
389 #endif /* CONFIG_FST */
390 	if (hapd->conf->vendor_elements)
391 		buflen += wpabuf_len(hapd->conf->vendor_elements);
392 	if (hapd->conf->vendor_vht) {
393 		buflen += 5 + 2 + sizeof(struct ieee80211_vht_capabilities) +
394 			2 + sizeof(struct ieee80211_vht_operation);
395 	}
396 
397 #ifdef CONFIG_IEEE80211AX
398 	if (hapd->iconf->ieee80211ax) {
399 		buflen += 3 + sizeof(struct ieee80211_he_capabilities) +
400 			3 + sizeof(struct ieee80211_he_operation) +
401 			3 + sizeof(struct ieee80211_he_mu_edca_parameter_set) +
402 			3 + sizeof(struct ieee80211_spatial_reuse);
403 	}
404 #endif /* CONFIG_IEEE80211AX */
405 
406 	buflen += hostapd_mbo_ie_len(hapd);
407 	buflen += hostapd_eid_owe_trans_len(hapd);
408 
409 	resp = os_zalloc(buflen);
410 	if (resp == NULL)
411 		return NULL;
412 
413 	epos = ((u8 *) resp) + MAX_PROBERESP_LEN;
414 
415 	resp->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
416 					   WLAN_FC_STYPE_PROBE_RESP);
417 	if (req)
418 		os_memcpy(resp->da, req->sa, ETH_ALEN);
419 	os_memcpy(resp->sa, hapd->own_addr, ETH_ALEN);
420 
421 	os_memcpy(resp->bssid, hapd->own_addr, ETH_ALEN);
422 	resp->u.probe_resp.beacon_int =
423 		host_to_le16(hapd->iconf->beacon_int);
424 
425 	/* hardware or low-level driver will setup seq_ctrl and timestamp */
426 	resp->u.probe_resp.capab_info =
427 		host_to_le16(hostapd_own_capab_info(hapd));
428 
429 	pos = resp->u.probe_resp.variable;
430 	*pos++ = WLAN_EID_SSID;
431 	*pos++ = hapd->conf->ssid.ssid_len;
432 	os_memcpy(pos, hapd->conf->ssid.ssid, hapd->conf->ssid.ssid_len);
433 	pos += hapd->conf->ssid.ssid_len;
434 
435 	/* Supported rates */
436 	pos = hostapd_eid_supp_rates(hapd, pos);
437 
438 	/* DS Params */
439 	pos = hostapd_eid_ds_params(hapd, pos);
440 
441 	pos = hostapd_eid_country(hapd, pos, epos - pos);
442 
443 	/* Power Constraint element */
444 	pos = hostapd_eid_pwr_constraint(hapd, pos);
445 
446 	/* CSA IE */
447 	csa_pos = hostapd_eid_csa(hapd, pos);
448 	if (csa_pos != pos)
449 		hapd->cs_c_off_proberesp = csa_pos - (u8 *) resp - 1;
450 	pos = csa_pos;
451 
452 	/* ERP Information element */
453 	pos = hostapd_eid_erp_info(hapd, pos);
454 
455 	/* Extended supported rates */
456 	pos = hostapd_eid_ext_supp_rates(hapd, pos);
457 
458 	/* RSN, MDIE */
459 	if (hapd->conf->wpa != WPA_PROTO_WPA)
460 		pos = hostapd_eid_wpa(hapd, pos, epos - pos);
461 
462 	pos = hostapd_eid_bss_load(hapd, pos, epos - pos);
463 
464 	pos = hostapd_eid_rm_enabled_capab(hapd, pos, epos - pos);
465 
466 	/* eCSA IE */
467 	csa_pos = hostapd_eid_ecsa(hapd, pos);
468 	if (csa_pos != pos)
469 		hapd->cs_c_off_ecsa_proberesp = csa_pos - (u8 *) resp - 1;
470 	pos = csa_pos;
471 
472 	pos = hostapd_eid_supported_op_classes(hapd, pos);
473 
474 #ifdef CONFIG_IEEE80211N
475 	/* Secondary Channel Offset element */
476 	/* TODO: The standard doesn't specify a position for this element. */
477 	pos = hostapd_eid_secondary_channel(hapd, pos);
478 
479 	pos = hostapd_eid_ht_capabilities(hapd, pos);
480 	pos = hostapd_eid_ht_operation(hapd, pos);
481 #endif /* CONFIG_IEEE80211N */
482 
483 	pos = hostapd_eid_ext_capab(hapd, pos);
484 
485 	pos = hostapd_eid_time_adv(hapd, pos);
486 	pos = hostapd_eid_time_zone(hapd, pos);
487 
488 	pos = hostapd_eid_interworking(hapd, pos);
489 	pos = hostapd_eid_adv_proto(hapd, pos);
490 	pos = hostapd_eid_roaming_consortium(hapd, pos);
491 
492 #ifdef CONFIG_FST
493 	if (hapd->iface->fst_ies) {
494 		os_memcpy(pos, wpabuf_head(hapd->iface->fst_ies),
495 			  wpabuf_len(hapd->iface->fst_ies));
496 		pos += wpabuf_len(hapd->iface->fst_ies);
497 	}
498 #endif /* CONFIG_FST */
499 
500 #ifdef CONFIG_IEEE80211AC
501 	if (hapd->iconf->ieee80211ac && !hapd->conf->disable_11ac) {
502 		pos = hostapd_eid_vht_capabilities(hapd, pos, 0);
503 		pos = hostapd_eid_vht_operation(hapd, pos);
504 		pos = hostapd_eid_txpower_envelope(hapd, pos);
505 		pos = hostapd_eid_wb_chsw_wrapper(hapd, pos);
506 	}
507 #endif /* CONFIG_IEEE80211AC */
508 
509 	pos = hostapd_eid_fils_indic(hapd, pos, 0);
510 
511 #ifdef CONFIG_IEEE80211AX
512 	if (hapd->iconf->ieee80211ax) {
513 		pos = hostapd_eid_he_capab(hapd, pos, IEEE80211_MODE_AP);
514 		pos = hostapd_eid_he_operation(hapd, pos);
515 		pos = hostapd_eid_he_mu_edca_parameter_set(hapd, pos);
516 		pos = hostapd_eid_spatial_reuse(hapd, pos);
517 	}
518 #endif /* CONFIG_IEEE80211AX */
519 
520 #ifdef CONFIG_IEEE80211AC
521 	if (hapd->conf->vendor_vht)
522 		pos = hostapd_eid_vendor_vht(hapd, pos);
523 #endif /* CONFIG_IEEE80211AC */
524 
525 	/* WPA */
526 	if (hapd->conf->wpa == WPA_PROTO_WPA)
527 		pos = hostapd_eid_wpa(hapd, pos, epos - pos);
528 
529 	/* Wi-Fi Alliance WMM */
530 	pos = hostapd_eid_wmm(hapd, pos);
531 
532 #ifdef CONFIG_WPS
533 	if (hapd->conf->wps_state && hapd->wps_probe_resp_ie) {
534 		os_memcpy(pos, wpabuf_head(hapd->wps_probe_resp_ie),
535 			  wpabuf_len(hapd->wps_probe_resp_ie));
536 		pos += wpabuf_len(hapd->wps_probe_resp_ie);
537 	}
538 #endif /* CONFIG_WPS */
539 
540 #ifdef CONFIG_P2P
541 	if ((hapd->conf->p2p & P2P_ENABLED) && is_p2p &&
542 	    hapd->p2p_probe_resp_ie) {
543 		os_memcpy(pos, wpabuf_head(hapd->p2p_probe_resp_ie),
544 			  wpabuf_len(hapd->p2p_probe_resp_ie));
545 		pos += wpabuf_len(hapd->p2p_probe_resp_ie);
546 	}
547 #endif /* CONFIG_P2P */
548 #ifdef CONFIG_P2P_MANAGER
549 	if ((hapd->conf->p2p & (P2P_MANAGE | P2P_ENABLED | P2P_GROUP_OWNER)) ==
550 	    P2P_MANAGE)
551 		pos = hostapd_eid_p2p_manage(hapd, pos);
552 #endif /* CONFIG_P2P_MANAGER */
553 
554 #ifdef CONFIG_HS20
555 	pos = hostapd_eid_hs20_indication(hapd, pos);
556 	pos = hostapd_eid_osen(hapd, pos);
557 #endif /* CONFIG_HS20 */
558 
559 	pos = hostapd_eid_mbo(hapd, pos, (u8 *) resp + buflen - pos);
560 	pos = hostapd_eid_owe_trans(hapd, pos, (u8 *) resp + buflen - pos);
561 
562 	if (hapd->conf->vendor_elements) {
563 		os_memcpy(pos, wpabuf_head(hapd->conf->vendor_elements),
564 			  wpabuf_len(hapd->conf->vendor_elements));
565 		pos += wpabuf_len(hapd->conf->vendor_elements);
566 	}
567 
568 	*resp_len = pos - (u8 *) resp;
569 	return (u8 *) resp;
570 }
571 
572 
573 enum ssid_match_result {
574 	NO_SSID_MATCH,
575 	EXACT_SSID_MATCH,
576 	WILDCARD_SSID_MATCH
577 };
578 
579 static enum ssid_match_result ssid_match(struct hostapd_data *hapd,
580 					 const u8 *ssid, size_t ssid_len,
581 					 const u8 *ssid_list,
582 					 size_t ssid_list_len)
583 {
584 	const u8 *pos, *end;
585 	int wildcard = 0;
586 
587 	if (ssid_len == 0)
588 		wildcard = 1;
589 	if (ssid_len == hapd->conf->ssid.ssid_len &&
590 	    os_memcmp(ssid, hapd->conf->ssid.ssid, ssid_len) == 0)
591 		return EXACT_SSID_MATCH;
592 
593 	if (ssid_list == NULL)
594 		return wildcard ? WILDCARD_SSID_MATCH : NO_SSID_MATCH;
595 
596 	pos = ssid_list;
597 	end = ssid_list + ssid_list_len;
598 	while (end - pos >= 2) {
599 		if (2 + pos[1] > end - pos)
600 			break;
601 		if (pos[1] == 0)
602 			wildcard = 1;
603 		if (pos[1] == hapd->conf->ssid.ssid_len &&
604 		    os_memcmp(pos + 2, hapd->conf->ssid.ssid, pos[1]) == 0)
605 			return EXACT_SSID_MATCH;
606 		pos += 2 + pos[1];
607 	}
608 
609 	return wildcard ? WILDCARD_SSID_MATCH : NO_SSID_MATCH;
610 }
611 
612 
613 void sta_track_expire(struct hostapd_iface *iface, int force)
614 {
615 	struct os_reltime now;
616 	struct hostapd_sta_info *info;
617 
618 	if (!iface->num_sta_seen)
619 		return;
620 
621 	os_get_reltime(&now);
622 	while ((info = dl_list_first(&iface->sta_seen, struct hostapd_sta_info,
623 				     list))) {
624 		if (!force &&
625 		    !os_reltime_expired(&now, &info->last_seen,
626 					iface->conf->track_sta_max_age))
627 			break;
628 		force = 0;
629 
630 		wpa_printf(MSG_MSGDUMP, "%s: Expire STA tracking entry for "
631 			   MACSTR, iface->bss[0]->conf->iface,
632 			   MAC2STR(info->addr));
633 		dl_list_del(&info->list);
634 		iface->num_sta_seen--;
635 		sta_track_del(info);
636 	}
637 }
638 
639 
640 static struct hostapd_sta_info * sta_track_get(struct hostapd_iface *iface,
641 					       const u8 *addr)
642 {
643 	struct hostapd_sta_info *info;
644 
645 	dl_list_for_each(info, &iface->sta_seen, struct hostapd_sta_info, list)
646 		if (os_memcmp(addr, info->addr, ETH_ALEN) == 0)
647 			return info;
648 
649 	return NULL;
650 }
651 
652 
653 void sta_track_add(struct hostapd_iface *iface, const u8 *addr, int ssi_signal)
654 {
655 	struct hostapd_sta_info *info;
656 
657 	info = sta_track_get(iface, addr);
658 	if (info) {
659 		/* Move the most recent entry to the end of the list */
660 		dl_list_del(&info->list);
661 		dl_list_add_tail(&iface->sta_seen, &info->list);
662 		os_get_reltime(&info->last_seen);
663 		info->ssi_signal = ssi_signal;
664 		return;
665 	}
666 
667 	/* Add a new entry */
668 	info = os_zalloc(sizeof(*info));
669 	if (info == NULL)
670 		return;
671 	os_memcpy(info->addr, addr, ETH_ALEN);
672 	os_get_reltime(&info->last_seen);
673 	info->ssi_signal = ssi_signal;
674 
675 	if (iface->num_sta_seen >= iface->conf->track_sta_max_num) {
676 		/* Expire oldest entry to make room for a new one */
677 		sta_track_expire(iface, 1);
678 	}
679 
680 	wpa_printf(MSG_MSGDUMP, "%s: Add STA tracking entry for "
681 		   MACSTR, iface->bss[0]->conf->iface, MAC2STR(addr));
682 	dl_list_add_tail(&iface->sta_seen, &info->list);
683 	iface->num_sta_seen++;
684 }
685 
686 
687 struct hostapd_data *
688 sta_track_seen_on(struct hostapd_iface *iface, const u8 *addr,
689 		  const char *ifname)
690 {
691 	struct hapd_interfaces *interfaces = iface->interfaces;
692 	size_t i, j;
693 
694 	for (i = 0; i < interfaces->count; i++) {
695 		struct hostapd_data *hapd = NULL;
696 
697 		iface = interfaces->iface[i];
698 		for (j = 0; j < iface->num_bss; j++) {
699 			hapd = iface->bss[j];
700 			if (os_strcmp(ifname, hapd->conf->iface) == 0)
701 				break;
702 			hapd = NULL;
703 		}
704 
705 		if (hapd && sta_track_get(iface, addr))
706 			return hapd;
707 	}
708 
709 	return NULL;
710 }
711 
712 
713 #ifdef CONFIG_TAXONOMY
714 void sta_track_claim_taxonomy_info(struct hostapd_iface *iface, const u8 *addr,
715 				   struct wpabuf **probe_ie_taxonomy)
716 {
717 	struct hostapd_sta_info *info;
718 
719 	info = sta_track_get(iface, addr);
720 	if (!info)
721 		return;
722 
723 	wpabuf_free(*probe_ie_taxonomy);
724 	*probe_ie_taxonomy = info->probe_ie_taxonomy;
725 	info->probe_ie_taxonomy = NULL;
726 }
727 #endif /* CONFIG_TAXONOMY */
728 
729 
730 void handle_probe_req(struct hostapd_data *hapd,
731 		      const struct ieee80211_mgmt *mgmt, size_t len,
732 		      int ssi_signal)
733 {
734 	u8 *resp;
735 	struct ieee802_11_elems elems;
736 	const u8 *ie;
737 	size_t ie_len;
738 	size_t i, resp_len;
739 	int noack;
740 	enum ssid_match_result res;
741 	int ret;
742 	u16 csa_offs[2];
743 	size_t csa_offs_len;
744 	u32 session_timeout, acct_interim_interval;
745 	struct vlan_description vlan_id;
746 	struct hostapd_sta_wpa_psk_short *psk = NULL;
747 	char *identity = NULL;
748 	char *radius_cui = NULL;
749 
750 	if (len < IEEE80211_HDRLEN)
751 		return;
752 	ie = ((const u8 *) mgmt) + IEEE80211_HDRLEN;
753 	if (hapd->iconf->track_sta_max_num)
754 		sta_track_add(hapd->iface, mgmt->sa, ssi_signal);
755 	ie_len = len - IEEE80211_HDRLEN;
756 
757 	ret = ieee802_11_allowed_address(hapd, mgmt->sa, (const u8 *) mgmt, len,
758 					 &session_timeout,
759 					 &acct_interim_interval, &vlan_id,
760 					 &psk, &identity, &radius_cui, 1);
761 	if (ret == HOSTAPD_ACL_REJECT) {
762 		wpa_msg(hapd->msg_ctx, MSG_DEBUG,
763 			"Ignore Probe Request frame from " MACSTR
764 			" due to ACL reject ", MAC2STR(mgmt->sa));
765 		return;
766 	}
767 
768 	for (i = 0; hapd->probereq_cb && i < hapd->num_probereq_cb; i++)
769 		if (hapd->probereq_cb[i].cb(hapd->probereq_cb[i].ctx,
770 					    mgmt->sa, mgmt->da, mgmt->bssid,
771 					    ie, ie_len, ssi_signal) > 0)
772 			return;
773 
774 	if (!hapd->conf->send_probe_response)
775 		return;
776 
777 	if (ieee802_11_parse_elems(ie, ie_len, &elems, 0) == ParseFailed) {
778 		wpa_printf(MSG_DEBUG, "Could not parse ProbeReq from " MACSTR,
779 			   MAC2STR(mgmt->sa));
780 		return;
781 	}
782 
783 	if ((!elems.ssid || !elems.supp_rates)) {
784 		wpa_printf(MSG_DEBUG, "STA " MACSTR " sent probe request "
785 			   "without SSID or supported rates element",
786 			   MAC2STR(mgmt->sa));
787 		return;
788 	}
789 
790 	/*
791 	 * No need to reply if the Probe Request frame was sent on an adjacent
792 	 * channel. IEEE Std 802.11-2012 describes this as a requirement for an
793 	 * AP with dot11RadioMeasurementActivated set to true, but strictly
794 	 * speaking does not allow such ignoring of Probe Request frames if
795 	 * dot11RadioMeasurementActivated is false. Anyway, this can help reduce
796 	 * number of unnecessary Probe Response frames for cases where the STA
797 	 * is less likely to see them (Probe Request frame sent on a
798 	 * neighboring, but partially overlapping, channel).
799 	 */
800 	if (elems.ds_params &&
801 	    hapd->iface->current_mode &&
802 	    (hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G ||
803 	     hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211B) &&
804 	    hapd->iconf->channel != elems.ds_params[0]) {
805 		wpa_printf(MSG_DEBUG,
806 			   "Ignore Probe Request due to DS Params mismatch: chan=%u != ds.chan=%u",
807 			   hapd->iconf->channel, elems.ds_params[0]);
808 		return;
809 	}
810 
811 #ifdef CONFIG_P2P
812 	if (hapd->p2p && hapd->p2p_group && elems.wps_ie) {
813 		struct wpabuf *wps;
814 		wps = ieee802_11_vendor_ie_concat(ie, ie_len, WPS_DEV_OUI_WFA);
815 		if (wps && !p2p_group_match_dev_type(hapd->p2p_group, wps)) {
816 			wpa_printf(MSG_MSGDUMP, "P2P: Ignore Probe Request "
817 				   "due to mismatch with Requested Device "
818 				   "Type");
819 			wpabuf_free(wps);
820 			return;
821 		}
822 		wpabuf_free(wps);
823 	}
824 
825 	if (hapd->p2p && hapd->p2p_group && elems.p2p) {
826 		struct wpabuf *p2p;
827 		p2p = ieee802_11_vendor_ie_concat(ie, ie_len, P2P_IE_VENDOR_TYPE);
828 		if (p2p && !p2p_group_match_dev_id(hapd->p2p_group, p2p)) {
829 			wpa_printf(MSG_MSGDUMP, "P2P: Ignore Probe Request "
830 				   "due to mismatch with Device ID");
831 			wpabuf_free(p2p);
832 			return;
833 		}
834 		wpabuf_free(p2p);
835 	}
836 #endif /* CONFIG_P2P */
837 
838 	if (hapd->conf->ignore_broadcast_ssid && elems.ssid_len == 0 &&
839 	    elems.ssid_list_len == 0) {
840 		wpa_printf(MSG_MSGDUMP, "Probe Request from " MACSTR " for "
841 			   "broadcast SSID ignored", MAC2STR(mgmt->sa));
842 		return;
843 	}
844 
845 #ifdef CONFIG_P2P
846 	if ((hapd->conf->p2p & P2P_GROUP_OWNER) &&
847 	    elems.ssid_len == P2P_WILDCARD_SSID_LEN &&
848 	    os_memcmp(elems.ssid, P2P_WILDCARD_SSID,
849 		      P2P_WILDCARD_SSID_LEN) == 0) {
850 		/* Process P2P Wildcard SSID like Wildcard SSID */
851 		elems.ssid_len = 0;
852 	}
853 #endif /* CONFIG_P2P */
854 
855 #ifdef CONFIG_TAXONOMY
856 	{
857 		struct sta_info *sta;
858 		struct hostapd_sta_info *info;
859 
860 		if ((sta = ap_get_sta(hapd, mgmt->sa)) != NULL) {
861 			taxonomy_sta_info_probe_req(hapd, sta, ie, ie_len);
862 		} else if ((info = sta_track_get(hapd->iface,
863 						 mgmt->sa)) != NULL) {
864 			taxonomy_hostapd_sta_info_probe_req(hapd, info,
865 							    ie, ie_len);
866 		}
867 	}
868 #endif /* CONFIG_TAXONOMY */
869 
870 	res = ssid_match(hapd, elems.ssid, elems.ssid_len,
871 			 elems.ssid_list, elems.ssid_list_len);
872 	if (res == NO_SSID_MATCH) {
873 		if (!(mgmt->da[0] & 0x01)) {
874 			wpa_printf(MSG_MSGDUMP, "Probe Request from " MACSTR
875 				   " for foreign SSID '%s' (DA " MACSTR ")%s",
876 				   MAC2STR(mgmt->sa),
877 				   wpa_ssid_txt(elems.ssid, elems.ssid_len),
878 				   MAC2STR(mgmt->da),
879 				   elems.ssid_list ? " (SSID list)" : "");
880 		}
881 		return;
882 	}
883 
884 #ifdef CONFIG_INTERWORKING
885 	if (hapd->conf->interworking &&
886 	    elems.interworking && elems.interworking_len >= 1) {
887 		u8 ant = elems.interworking[0] & 0x0f;
888 		if (ant != INTERWORKING_ANT_WILDCARD &&
889 		    ant != hapd->conf->access_network_type) {
890 			wpa_printf(MSG_MSGDUMP, "Probe Request from " MACSTR
891 				   " for mismatching ANT %u ignored",
892 				   MAC2STR(mgmt->sa), ant);
893 			return;
894 		}
895 	}
896 
897 	if (hapd->conf->interworking && elems.interworking &&
898 	    (elems.interworking_len == 7 || elems.interworking_len == 9)) {
899 		const u8 *hessid;
900 		if (elems.interworking_len == 7)
901 			hessid = elems.interworking + 1;
902 		else
903 			hessid = elems.interworking + 1 + 2;
904 		if (!is_broadcast_ether_addr(hessid) &&
905 		    os_memcmp(hessid, hapd->conf->hessid, ETH_ALEN) != 0) {
906 			wpa_printf(MSG_MSGDUMP, "Probe Request from " MACSTR
907 				   " for mismatching HESSID " MACSTR
908 				   " ignored",
909 				   MAC2STR(mgmt->sa), MAC2STR(hessid));
910 			return;
911 		}
912 	}
913 #endif /* CONFIG_INTERWORKING */
914 
915 #ifdef CONFIG_P2P
916 	if ((hapd->conf->p2p & P2P_GROUP_OWNER) &&
917 	    supp_rates_11b_only(&elems)) {
918 		/* Indicates support for 11b rates only */
919 		wpa_printf(MSG_EXCESSIVE, "P2P: Ignore Probe Request from "
920 			   MACSTR " with only 802.11b rates",
921 			   MAC2STR(mgmt->sa));
922 		return;
923 	}
924 #endif /* CONFIG_P2P */
925 
926 	/* TODO: verify that supp_rates contains at least one matching rate
927 	 * with AP configuration */
928 
929 	if (hapd->conf->no_probe_resp_if_seen_on &&
930 	    is_multicast_ether_addr(mgmt->da) &&
931 	    is_multicast_ether_addr(mgmt->bssid) &&
932 	    sta_track_seen_on(hapd->iface, mgmt->sa,
933 			      hapd->conf->no_probe_resp_if_seen_on)) {
934 		wpa_printf(MSG_MSGDUMP, "%s: Ignore Probe Request from " MACSTR
935 			   " since STA has been seen on %s",
936 			   hapd->conf->iface, MAC2STR(mgmt->sa),
937 			   hapd->conf->no_probe_resp_if_seen_on);
938 		return;
939 	}
940 
941 	if (hapd->conf->no_probe_resp_if_max_sta &&
942 	    is_multicast_ether_addr(mgmt->da) &&
943 	    is_multicast_ether_addr(mgmt->bssid) &&
944 	    hapd->num_sta >= hapd->conf->max_num_sta &&
945 	    !ap_get_sta(hapd, mgmt->sa)) {
946 		wpa_printf(MSG_MSGDUMP, "%s: Ignore Probe Request from " MACSTR
947 			   " since no room for additional STA",
948 			   hapd->conf->iface, MAC2STR(mgmt->sa));
949 		return;
950 	}
951 
952 #ifdef CONFIG_TESTING_OPTIONS
953 	if (hapd->iconf->ignore_probe_probability > 0.0 &&
954 	    drand48() < hapd->iconf->ignore_probe_probability) {
955 		wpa_printf(MSG_INFO,
956 			   "TESTING: ignoring probe request from " MACSTR,
957 			   MAC2STR(mgmt->sa));
958 		return;
959 	}
960 #endif /* CONFIG_TESTING_OPTIONS */
961 
962 	wpa_msg_ctrl(hapd->msg_ctx, MSG_INFO, RX_PROBE_REQUEST "sa=" MACSTR
963 		     " signal=%d", MAC2STR(mgmt->sa), ssi_signal);
964 
965 	resp = hostapd_gen_probe_resp(hapd, mgmt, elems.p2p != NULL,
966 				      &resp_len);
967 	if (resp == NULL)
968 		return;
969 
970 	/*
971 	 * If this is a broadcast probe request, apply no ack policy to avoid
972 	 * excessive retries.
973 	 */
974 	noack = !!(res == WILDCARD_SSID_MATCH &&
975 		   is_broadcast_ether_addr(mgmt->da));
976 
977 	csa_offs_len = 0;
978 	if (hapd->csa_in_progress) {
979 		if (hapd->cs_c_off_proberesp)
980 			csa_offs[csa_offs_len++] =
981 				hapd->cs_c_off_proberesp;
982 
983 		if (hapd->cs_c_off_ecsa_proberesp)
984 			csa_offs[csa_offs_len++] =
985 				hapd->cs_c_off_ecsa_proberesp;
986 	}
987 
988 	ret = hostapd_drv_send_mlme_csa(hapd, resp, resp_len, noack,
989 					csa_offs_len ? csa_offs : NULL,
990 					csa_offs_len);
991 
992 	if (ret < 0)
993 		wpa_printf(MSG_INFO, "handle_probe_req: send failed");
994 
995 	os_free(resp);
996 
997 	wpa_printf(MSG_EXCESSIVE, "STA " MACSTR " sent probe request for %s "
998 		   "SSID", MAC2STR(mgmt->sa),
999 		   elems.ssid_len == 0 ? "broadcast" : "our");
1000 }
1001 
1002 
1003 static u8 * hostapd_probe_resp_offloads(struct hostapd_data *hapd,
1004 					size_t *resp_len)
1005 {
1006 	/* check probe response offloading caps and print warnings */
1007 	if (!(hapd->iface->drv_flags & WPA_DRIVER_FLAGS_PROBE_RESP_OFFLOAD))
1008 		return NULL;
1009 
1010 #ifdef CONFIG_WPS
1011 	if (hapd->conf->wps_state && hapd->wps_probe_resp_ie &&
1012 	    (!(hapd->iface->probe_resp_offloads &
1013 	       (WPA_DRIVER_PROBE_RESP_OFFLOAD_WPS |
1014 		WPA_DRIVER_PROBE_RESP_OFFLOAD_WPS2))))
1015 		wpa_printf(MSG_WARNING, "Device is trying to offload WPS "
1016 			   "Probe Response while not supporting this");
1017 #endif /* CONFIG_WPS */
1018 
1019 #ifdef CONFIG_P2P
1020 	if ((hapd->conf->p2p & P2P_ENABLED) && hapd->p2p_probe_resp_ie &&
1021 	    !(hapd->iface->probe_resp_offloads &
1022 	      WPA_DRIVER_PROBE_RESP_OFFLOAD_P2P))
1023 		wpa_printf(MSG_WARNING, "Device is trying to offload P2P "
1024 			   "Probe Response while not supporting this");
1025 #endif  /* CONFIG_P2P */
1026 
1027 	if (hapd->conf->interworking &&
1028 	    !(hapd->iface->probe_resp_offloads &
1029 	      WPA_DRIVER_PROBE_RESP_OFFLOAD_INTERWORKING))
1030 		wpa_printf(MSG_WARNING, "Device is trying to offload "
1031 			   "Interworking Probe Response while not supporting "
1032 			   "this");
1033 
1034 	/* Generate a Probe Response template for the non-P2P case */
1035 	return hostapd_gen_probe_resp(hapd, NULL, 0, resp_len);
1036 }
1037 
1038 #endif /* NEED_AP_MLME */
1039 
1040 
1041 void sta_track_del(struct hostapd_sta_info *info)
1042 {
1043 #ifdef CONFIG_TAXONOMY
1044 	wpabuf_free(info->probe_ie_taxonomy);
1045 	info->probe_ie_taxonomy = NULL;
1046 #endif /* CONFIG_TAXONOMY */
1047 	os_free(info);
1048 }
1049 
1050 
1051 int ieee802_11_build_ap_params(struct hostapd_data *hapd,
1052 			       struct wpa_driver_ap_params *params)
1053 {
1054 	struct ieee80211_mgmt *head = NULL;
1055 	u8 *tail = NULL;
1056 	size_t head_len = 0, tail_len = 0;
1057 	u8 *resp = NULL;
1058 	size_t resp_len = 0;
1059 #ifdef NEED_AP_MLME
1060 	u16 capab_info;
1061 	u8 *pos, *tailpos, *csa_pos;
1062 
1063 #define BEACON_HEAD_BUF_SIZE 256
1064 #define BEACON_TAIL_BUF_SIZE 512
1065 	head = os_zalloc(BEACON_HEAD_BUF_SIZE);
1066 	tail_len = BEACON_TAIL_BUF_SIZE;
1067 #ifdef CONFIG_WPS
1068 	if (hapd->conf->wps_state && hapd->wps_beacon_ie)
1069 		tail_len += wpabuf_len(hapd->wps_beacon_ie);
1070 #endif /* CONFIG_WPS */
1071 #ifdef CONFIG_P2P
1072 	if (hapd->p2p_beacon_ie)
1073 		tail_len += wpabuf_len(hapd->p2p_beacon_ie);
1074 #endif /* CONFIG_P2P */
1075 #ifdef CONFIG_FST
1076 	if (hapd->iface->fst_ies)
1077 		tail_len += wpabuf_len(hapd->iface->fst_ies);
1078 #endif /* CONFIG_FST */
1079 	if (hapd->conf->vendor_elements)
1080 		tail_len += wpabuf_len(hapd->conf->vendor_elements);
1081 
1082 #ifdef CONFIG_IEEE80211AC
1083 	if (hapd->conf->vendor_vht) {
1084 		tail_len += 5 + 2 + sizeof(struct ieee80211_vht_capabilities) +
1085 			2 + sizeof(struct ieee80211_vht_operation);
1086 	}
1087 #endif /* CONFIG_IEEE80211AC */
1088 
1089 #ifdef CONFIG_IEEE80211AX
1090 	if (hapd->iconf->ieee80211ax) {
1091 		tail_len += 3 + sizeof(struct ieee80211_he_capabilities) +
1092 			3 + sizeof(struct ieee80211_he_operation) +
1093 			3 + sizeof(struct ieee80211_he_mu_edca_parameter_set) +
1094 			3 + sizeof(struct ieee80211_spatial_reuse);
1095 	}
1096 #endif /* CONFIG_IEEE80211AX */
1097 
1098 	tail_len += hostapd_mbo_ie_len(hapd);
1099 	tail_len += hostapd_eid_owe_trans_len(hapd);
1100 
1101 	tailpos = tail = os_malloc(tail_len);
1102 	if (head == NULL || tail == NULL) {
1103 		wpa_printf(MSG_ERROR, "Failed to set beacon data");
1104 		os_free(head);
1105 		os_free(tail);
1106 		return -1;
1107 	}
1108 
1109 	head->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
1110 					   WLAN_FC_STYPE_BEACON);
1111 	head->duration = host_to_le16(0);
1112 	os_memset(head->da, 0xff, ETH_ALEN);
1113 
1114 	os_memcpy(head->sa, hapd->own_addr, ETH_ALEN);
1115 	os_memcpy(head->bssid, hapd->own_addr, ETH_ALEN);
1116 	head->u.beacon.beacon_int =
1117 		host_to_le16(hapd->iconf->beacon_int);
1118 
1119 	/* hardware or low-level driver will setup seq_ctrl and timestamp */
1120 	capab_info = hostapd_own_capab_info(hapd);
1121 	head->u.beacon.capab_info = host_to_le16(capab_info);
1122 	pos = &head->u.beacon.variable[0];
1123 
1124 	/* SSID */
1125 	*pos++ = WLAN_EID_SSID;
1126 	if (hapd->conf->ignore_broadcast_ssid == 2) {
1127 		/* clear the data, but keep the correct length of the SSID */
1128 		*pos++ = hapd->conf->ssid.ssid_len;
1129 		os_memset(pos, 0, hapd->conf->ssid.ssid_len);
1130 		pos += hapd->conf->ssid.ssid_len;
1131 	} else if (hapd->conf->ignore_broadcast_ssid) {
1132 		*pos++ = 0; /* empty SSID */
1133 	} else {
1134 		*pos++ = hapd->conf->ssid.ssid_len;
1135 		os_memcpy(pos, hapd->conf->ssid.ssid,
1136 			  hapd->conf->ssid.ssid_len);
1137 		pos += hapd->conf->ssid.ssid_len;
1138 	}
1139 
1140 	/* Supported rates */
1141 	pos = hostapd_eid_supp_rates(hapd, pos);
1142 
1143 	/* DS Params */
1144 	pos = hostapd_eid_ds_params(hapd, pos);
1145 
1146 	head_len = pos - (u8 *) head;
1147 
1148 	tailpos = hostapd_eid_country(hapd, tailpos,
1149 				      tail + BEACON_TAIL_BUF_SIZE - tailpos);
1150 
1151 	/* Power Constraint element */
1152 	tailpos = hostapd_eid_pwr_constraint(hapd, tailpos);
1153 
1154 	/* CSA IE */
1155 	csa_pos = hostapd_eid_csa(hapd, tailpos);
1156 	if (csa_pos != tailpos)
1157 		hapd->cs_c_off_beacon = csa_pos - tail - 1;
1158 	tailpos = csa_pos;
1159 
1160 	/* ERP Information element */
1161 	tailpos = hostapd_eid_erp_info(hapd, tailpos);
1162 
1163 	/* Extended supported rates */
1164 	tailpos = hostapd_eid_ext_supp_rates(hapd, tailpos);
1165 
1166 	/* RSN, MDIE */
1167 	if (hapd->conf->wpa != WPA_PROTO_WPA)
1168 		tailpos = hostapd_eid_wpa(hapd, tailpos,
1169 					  tail + BEACON_TAIL_BUF_SIZE -
1170 					  tailpos);
1171 
1172 	tailpos = hostapd_eid_rm_enabled_capab(hapd, tailpos,
1173 					       tail + BEACON_TAIL_BUF_SIZE -
1174 					       tailpos);
1175 
1176 	tailpos = hostapd_eid_bss_load(hapd, tailpos,
1177 				       tail + BEACON_TAIL_BUF_SIZE - tailpos);
1178 
1179 	/* eCSA IE */
1180 	csa_pos = hostapd_eid_ecsa(hapd, tailpos);
1181 	if (csa_pos != tailpos)
1182 		hapd->cs_c_off_ecsa_beacon = csa_pos - tail - 1;
1183 	tailpos = csa_pos;
1184 
1185 	tailpos = hostapd_eid_supported_op_classes(hapd, tailpos);
1186 
1187 #ifdef CONFIG_IEEE80211N
1188 	/* Secondary Channel Offset element */
1189 	/* TODO: The standard doesn't specify a position for this element. */
1190 	tailpos = hostapd_eid_secondary_channel(hapd, tailpos);
1191 
1192 	tailpos = hostapd_eid_ht_capabilities(hapd, tailpos);
1193 	tailpos = hostapd_eid_ht_operation(hapd, tailpos);
1194 #endif /* CONFIG_IEEE80211N */
1195 
1196 	tailpos = hostapd_eid_ext_capab(hapd, tailpos);
1197 
1198 	/*
1199 	 * TODO: Time Advertisement element should only be included in some
1200 	 * DTIM Beacon frames.
1201 	 */
1202 	tailpos = hostapd_eid_time_adv(hapd, tailpos);
1203 
1204 	tailpos = hostapd_eid_interworking(hapd, tailpos);
1205 	tailpos = hostapd_eid_adv_proto(hapd, tailpos);
1206 	tailpos = hostapd_eid_roaming_consortium(hapd, tailpos);
1207 
1208 #ifdef CONFIG_FST
1209 	if (hapd->iface->fst_ies) {
1210 		os_memcpy(tailpos, wpabuf_head(hapd->iface->fst_ies),
1211 			  wpabuf_len(hapd->iface->fst_ies));
1212 		tailpos += wpabuf_len(hapd->iface->fst_ies);
1213 	}
1214 #endif /* CONFIG_FST */
1215 
1216 #ifdef CONFIG_IEEE80211AC
1217 	if (hapd->iconf->ieee80211ac && !hapd->conf->disable_11ac) {
1218 		tailpos = hostapd_eid_vht_capabilities(hapd, tailpos, 0);
1219 		tailpos = hostapd_eid_vht_operation(hapd, tailpos);
1220 		tailpos = hostapd_eid_txpower_envelope(hapd, tailpos);
1221 		tailpos = hostapd_eid_wb_chsw_wrapper(hapd, tailpos);
1222 	}
1223 #endif /* CONFIG_IEEE80211AC */
1224 
1225 	tailpos = hostapd_eid_fils_indic(hapd, tailpos, 0);
1226 
1227 #ifdef CONFIG_IEEE80211AX
1228 	if (hapd->iconf->ieee80211ax) {
1229 		tailpos = hostapd_eid_he_capab(hapd, tailpos,
1230 					       IEEE80211_MODE_AP);
1231 		tailpos = hostapd_eid_he_operation(hapd, tailpos);
1232 		tailpos = hostapd_eid_he_mu_edca_parameter_set(hapd, tailpos);
1233 		tailpos = hostapd_eid_spatial_reuse(hapd, tailpos);
1234 	}
1235 #endif /* CONFIG_IEEE80211AX */
1236 
1237 #ifdef CONFIG_IEEE80211AC
1238 	if (hapd->conf->vendor_vht)
1239 		tailpos = hostapd_eid_vendor_vht(hapd, tailpos);
1240 #endif /* CONFIG_IEEE80211AC */
1241 
1242 	/* WPA */
1243 	if (hapd->conf->wpa == WPA_PROTO_WPA)
1244 		tailpos = hostapd_eid_wpa(hapd, tailpos,
1245 					  tail + BEACON_TAIL_BUF_SIZE -
1246 					  tailpos);
1247 
1248 	/* Wi-Fi Alliance WMM */
1249 	tailpos = hostapd_eid_wmm(hapd, tailpos);
1250 
1251 #ifdef CONFIG_WPS
1252 	if (hapd->conf->wps_state && hapd->wps_beacon_ie) {
1253 		os_memcpy(tailpos, wpabuf_head(hapd->wps_beacon_ie),
1254 			  wpabuf_len(hapd->wps_beacon_ie));
1255 		tailpos += wpabuf_len(hapd->wps_beacon_ie);
1256 	}
1257 #endif /* CONFIG_WPS */
1258 
1259 #ifdef CONFIG_P2P
1260 	if ((hapd->conf->p2p & P2P_ENABLED) && hapd->p2p_beacon_ie) {
1261 		os_memcpy(tailpos, wpabuf_head(hapd->p2p_beacon_ie),
1262 			  wpabuf_len(hapd->p2p_beacon_ie));
1263 		tailpos += wpabuf_len(hapd->p2p_beacon_ie);
1264 	}
1265 #endif /* CONFIG_P2P */
1266 #ifdef CONFIG_P2P_MANAGER
1267 	if ((hapd->conf->p2p & (P2P_MANAGE | P2P_ENABLED | P2P_GROUP_OWNER)) ==
1268 	    P2P_MANAGE)
1269 		tailpos = hostapd_eid_p2p_manage(hapd, tailpos);
1270 #endif /* CONFIG_P2P_MANAGER */
1271 
1272 #ifdef CONFIG_HS20
1273 	tailpos = hostapd_eid_hs20_indication(hapd, tailpos);
1274 	tailpos = hostapd_eid_osen(hapd, tailpos);
1275 #endif /* CONFIG_HS20 */
1276 
1277 	tailpos = hostapd_eid_mbo(hapd, tailpos, tail + tail_len - tailpos);
1278 	tailpos = hostapd_eid_owe_trans(hapd, tailpos,
1279 					tail + tail_len - tailpos);
1280 
1281 	if (hapd->conf->vendor_elements) {
1282 		os_memcpy(tailpos, wpabuf_head(hapd->conf->vendor_elements),
1283 			  wpabuf_len(hapd->conf->vendor_elements));
1284 		tailpos += wpabuf_len(hapd->conf->vendor_elements);
1285 	}
1286 
1287 	tail_len = tailpos > tail ? tailpos - tail : 0;
1288 
1289 	resp = hostapd_probe_resp_offloads(hapd, &resp_len);
1290 #endif /* NEED_AP_MLME */
1291 
1292 	os_memset(params, 0, sizeof(*params));
1293 	params->head = (u8 *) head;
1294 	params->head_len = head_len;
1295 	params->tail = tail;
1296 	params->tail_len = tail_len;
1297 	params->proberesp = resp;
1298 	params->proberesp_len = resp_len;
1299 	params->dtim_period = hapd->conf->dtim_period;
1300 	params->beacon_int = hapd->iconf->beacon_int;
1301 	params->basic_rates = hapd->iface->basic_rates;
1302 	params->beacon_rate = hapd->iconf->beacon_rate;
1303 	params->rate_type = hapd->iconf->rate_type;
1304 	params->ssid = hapd->conf->ssid.ssid;
1305 	params->ssid_len = hapd->conf->ssid.ssid_len;
1306 	if ((hapd->conf->wpa & (WPA_PROTO_WPA | WPA_PROTO_RSN)) ==
1307 	    (WPA_PROTO_WPA | WPA_PROTO_RSN))
1308 		params->pairwise_ciphers = hapd->conf->wpa_pairwise |
1309 			hapd->conf->rsn_pairwise;
1310 	else if (hapd->conf->wpa & WPA_PROTO_RSN)
1311 		params->pairwise_ciphers = hapd->conf->rsn_pairwise;
1312 	else if (hapd->conf->wpa & WPA_PROTO_WPA)
1313 		params->pairwise_ciphers = hapd->conf->wpa_pairwise;
1314 	params->group_cipher = hapd->conf->wpa_group;
1315 	params->key_mgmt_suites = hapd->conf->wpa_key_mgmt;
1316 	params->auth_algs = hapd->conf->auth_algs;
1317 	params->wpa_version = hapd->conf->wpa;
1318 	params->privacy = hapd->conf->ssid.wep.keys_set || hapd->conf->wpa ||
1319 		(hapd->conf->ieee802_1x &&
1320 		 (hapd->conf->default_wep_key_len ||
1321 		  hapd->conf->individual_wep_key_len));
1322 	switch (hapd->conf->ignore_broadcast_ssid) {
1323 	case 0:
1324 		params->hide_ssid = NO_SSID_HIDING;
1325 		break;
1326 	case 1:
1327 		params->hide_ssid = HIDDEN_SSID_ZERO_LEN;
1328 		break;
1329 	case 2:
1330 		params->hide_ssid = HIDDEN_SSID_ZERO_CONTENTS;
1331 		break;
1332 	}
1333 	params->isolate = hapd->conf->isolate;
1334 	params->smps_mode = hapd->iconf->ht_capab & HT_CAP_INFO_SMPS_MASK;
1335 #ifdef NEED_AP_MLME
1336 	params->cts_protect = !!(ieee802_11_erp_info(hapd) &
1337 				ERP_INFO_USE_PROTECTION);
1338 	params->preamble = hapd->iface->num_sta_no_short_preamble == 0 &&
1339 		hapd->iconf->preamble == SHORT_PREAMBLE;
1340 	if (hapd->iface->current_mode &&
1341 	    hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G)
1342 		params->short_slot_time =
1343 			hapd->iface->num_sta_no_short_slot_time > 0 ? 0 : 1;
1344 	else
1345 		params->short_slot_time = -1;
1346 	if (!hapd->iconf->ieee80211n || hapd->conf->disable_11n)
1347 		params->ht_opmode = -1;
1348 	else
1349 		params->ht_opmode = hapd->iface->ht_op_mode;
1350 #endif /* NEED_AP_MLME */
1351 	params->interworking = hapd->conf->interworking;
1352 	if (hapd->conf->interworking &&
1353 	    !is_zero_ether_addr(hapd->conf->hessid))
1354 		params->hessid = hapd->conf->hessid;
1355 	params->access_network_type = hapd->conf->access_network_type;
1356 	params->ap_max_inactivity = hapd->conf->ap_max_inactivity;
1357 #ifdef CONFIG_P2P
1358 	params->p2p_go_ctwindow = hapd->iconf->p2p_go_ctwindow;
1359 #endif /* CONFIG_P2P */
1360 #ifdef CONFIG_HS20
1361 	params->disable_dgaf = hapd->conf->disable_dgaf;
1362 	if (hapd->conf->osen) {
1363 		params->privacy = 1;
1364 		params->osen = 1;
1365 	}
1366 #endif /* CONFIG_HS20 */
1367 	params->multicast_to_unicast = hapd->conf->multicast_to_unicast;
1368 	params->pbss = hapd->conf->pbss;
1369 
1370 	if (hapd->conf->ftm_responder) {
1371 		if (hapd->iface->drv_flags & WPA_DRIVER_FLAGS_FTM_RESPONDER) {
1372 			params->ftm_responder = 1;
1373 			params->lci = hapd->iface->conf->lci;
1374 			params->civic = hapd->iface->conf->civic;
1375 		} else {
1376 			wpa_printf(MSG_WARNING,
1377 				   "Not configuring FTM responder as the driver doesn't advertise support for it");
1378 		}
1379 	}
1380 
1381 	return 0;
1382 }
1383 
1384 
1385 void ieee802_11_free_ap_params(struct wpa_driver_ap_params *params)
1386 {
1387 	os_free(params->tail);
1388 	params->tail = NULL;
1389 	os_free(params->head);
1390 	params->head = NULL;
1391 	os_free(params->proberesp);
1392 	params->proberesp = NULL;
1393 }
1394 
1395 
1396 int ieee802_11_set_beacon(struct hostapd_data *hapd)
1397 {
1398 	struct wpa_driver_ap_params params;
1399 	struct hostapd_freq_params freq;
1400 	struct hostapd_iface *iface = hapd->iface;
1401 	struct hostapd_config *iconf = iface->conf;
1402 	struct hostapd_hw_modes *cmode = iface->current_mode;
1403 	struct wpabuf *beacon, *proberesp, *assocresp;
1404 	int res, ret = -1;
1405 
1406 	if (hapd->csa_in_progress) {
1407 		wpa_printf(MSG_ERROR, "Cannot set beacons during CSA period");
1408 		return -1;
1409 	}
1410 
1411 	hapd->beacon_set_done = 1;
1412 
1413 	if (ieee802_11_build_ap_params(hapd, &params) < 0)
1414 		return -1;
1415 
1416 	if (hostapd_build_ap_extra_ies(hapd, &beacon, &proberesp, &assocresp) <
1417 	    0)
1418 		goto fail;
1419 
1420 	params.beacon_ies = beacon;
1421 	params.proberesp_ies = proberesp;
1422 	params.assocresp_ies = assocresp;
1423 	params.reenable = hapd->reenable_beacon;
1424 	hapd->reenable_beacon = 0;
1425 
1426 	if (cmode &&
1427 	    hostapd_set_freq_params(&freq, iconf->hw_mode, iface->freq,
1428 				    iconf->channel, iconf->ieee80211n,
1429 				    iconf->ieee80211ac, iconf->ieee80211ax,
1430 				    iconf->secondary_channel,
1431 				    hostapd_get_oper_chwidth(iconf),
1432 				    hostapd_get_oper_centr_freq_seg0_idx(iconf),
1433 				    hostapd_get_oper_centr_freq_seg1_idx(iconf),
1434 				    cmode->vht_capab,
1435 				    &cmode->he_capab[IEEE80211_MODE_AP]) == 0)
1436 		params.freq = &freq;
1437 
1438 	res = hostapd_drv_set_ap(hapd, &params);
1439 	hostapd_free_ap_extra_ies(hapd, beacon, proberesp, assocresp);
1440 	if (res)
1441 		wpa_printf(MSG_ERROR, "Failed to set beacon parameters");
1442 	else
1443 		ret = 0;
1444 fail:
1445 	ieee802_11_free_ap_params(&params);
1446 	return ret;
1447 }
1448 
1449 
1450 int ieee802_11_set_beacons(struct hostapd_iface *iface)
1451 {
1452 	size_t i;
1453 	int ret = 0;
1454 
1455 	for (i = 0; i < iface->num_bss; i++) {
1456 		if (iface->bss[i]->started &&
1457 		    ieee802_11_set_beacon(iface->bss[i]) < 0)
1458 			ret = -1;
1459 	}
1460 
1461 	return ret;
1462 }
1463 
1464 
1465 /* only update beacons if started */
1466 int ieee802_11_update_beacons(struct hostapd_iface *iface)
1467 {
1468 	size_t i;
1469 	int ret = 0;
1470 
1471 	for (i = 0; i < iface->num_bss; i++) {
1472 		if (iface->bss[i]->beacon_set_done && iface->bss[i]->started &&
1473 		    ieee802_11_set_beacon(iface->bss[i]) < 0)
1474 			ret = -1;
1475 	}
1476 
1477 	return ret;
1478 }
1479 
1480 #endif /* CONFIG_NATIVE_WINDOWS */
1481