• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "crypto/sha1.h"
21 #include "wps/wps_defs.h"
22 #include "p2p/p2p.h"
23 #include "hostapd.h"
24 #include "ieee802_11.h"
25 #include "wpa_auth.h"
26 #include "wmm.h"
27 #include "ap_config.h"
28 #include "sta_info.h"
29 #include "p2p_hostapd.h"
30 #include "ap_drv_ops.h"
31 #include "beacon.h"
32 #include "hs20.h"
33 #include "dfs.h"
34 #include "taxonomy.h"
35 #include "ieee802_11_auth.h"
36 
37 
38 #ifdef NEED_AP_MLME
39 
hostapd_eid_bss_load(struct hostapd_data * hapd,u8 * eid,size_t len)40 static u8 * hostapd_eid_bss_load(struct hostapd_data *hapd, u8 *eid, size_t len)
41 {
42 	if (len < 2 + 5)
43 		return eid;
44 
45 #ifdef CONFIG_TESTING_OPTIONS
46 	if (hapd->conf->bss_load_test_set) {
47 		*eid++ = WLAN_EID_BSS_LOAD;
48 		*eid++ = 5;
49 		os_memcpy(eid, hapd->conf->bss_load_test, 5);
50 		eid += 5;
51 		return eid;
52 	}
53 #endif /* CONFIG_TESTING_OPTIONS */
54 	if (hapd->conf->bss_load_update_period) {
55 		*eid++ = WLAN_EID_BSS_LOAD;
56 		*eid++ = 5;
57 		WPA_PUT_LE16(eid, hapd->num_sta);
58 		eid += 2;
59 		*eid++ = hapd->iface->channel_utilization;
60 		WPA_PUT_LE16(eid, 0); /* no available admission capabity */
61 		eid += 2;
62 	}
63 	return eid;
64 }
65 
66 
ieee802_11_erp_info(struct hostapd_data * hapd)67 static u8 ieee802_11_erp_info(struct hostapd_data *hapd)
68 {
69 	u8 erp = 0;
70 
71 	if (hapd->iface->current_mode == NULL ||
72 	    hapd->iface->current_mode->mode != HOSTAPD_MODE_IEEE80211G)
73 		return 0;
74 
75 	if (hapd->iface->olbc)
76 		erp |= ERP_INFO_USE_PROTECTION;
77 	if (hapd->iface->num_sta_non_erp > 0) {
78 		erp |= ERP_INFO_NON_ERP_PRESENT |
79 			ERP_INFO_USE_PROTECTION;
80 	}
81 	if (hapd->iface->num_sta_no_short_preamble > 0 ||
82 	    hapd->iconf->preamble == LONG_PREAMBLE)
83 		erp |= ERP_INFO_BARKER_PREAMBLE_MODE;
84 
85 	return erp;
86 }
87 
88 
hostapd_eid_ds_params(struct hostapd_data * hapd,u8 * eid)89 static u8 * hostapd_eid_ds_params(struct hostapd_data *hapd, u8 *eid)
90 {
91 	enum hostapd_hw_mode hw_mode = hapd->iconf->hw_mode;
92 
93 	if (hw_mode != HOSTAPD_MODE_IEEE80211G &&
94 	    hw_mode != HOSTAPD_MODE_IEEE80211B)
95 		return eid;
96 
97 	*eid++ = WLAN_EID_DS_PARAMS;
98 	*eid++ = 1;
99 	*eid++ = hapd->iconf->channel;
100 	return eid;
101 }
102 
103 
hostapd_eid_erp_info(struct hostapd_data * hapd,u8 * eid)104 static u8 * hostapd_eid_erp_info(struct hostapd_data *hapd, u8 *eid)
105 {
106 	if (hapd->iface->current_mode == NULL ||
107 	    hapd->iface->current_mode->mode != HOSTAPD_MODE_IEEE80211G)
108 		return eid;
109 
110 	/* Set NonERP_present and use_protection bits if there
111 	 * are any associated NonERP stations. */
112 	/* TODO: use_protection bit can be set to zero even if
113 	 * there are NonERP stations present. This optimization
114 	 * might be useful if NonERP stations are "quiet".
115 	 * See 802.11g/D6 E-1 for recommended practice.
116 	 * In addition, Non ERP present might be set, if AP detects Non ERP
117 	 * operation on other APs. */
118 
119 	/* Add ERP Information element */
120 	*eid++ = WLAN_EID_ERP_INFO;
121 	*eid++ = 1;
122 	*eid++ = ieee802_11_erp_info(hapd);
123 
124 	return eid;
125 }
126 
127 
hostapd_eid_pwr_constraint(struct hostapd_data * hapd,u8 * eid)128 static u8 * hostapd_eid_pwr_constraint(struct hostapd_data *hapd, u8 *eid)
129 {
130 	u8 *pos = eid;
131 	u8 local_pwr_constraint = 0;
132 	int dfs;
133 
134 	if (hapd->iface->current_mode == NULL ||
135 	    hapd->iface->current_mode->mode != HOSTAPD_MODE_IEEE80211A)
136 		return eid;
137 
138 	/* Let host drivers add this IE if DFS support is offloaded */
139 	if (hapd->iface->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD)
140 		return eid;
141 
142 	/*
143 	 * There is no DFS support and power constraint was not directly
144 	 * requested by config option.
145 	 */
146 	if (!hapd->iconf->ieee80211h &&
147 	    hapd->iconf->local_pwr_constraint == -1)
148 		return eid;
149 
150 	/* Check if DFS is required by regulatory. */
151 	dfs = hostapd_is_dfs_required(hapd->iface);
152 	if (dfs < 0) {
153 		wpa_printf(MSG_WARNING, "Failed to check if DFS is required; ret=%d",
154 			   dfs);
155 		dfs = 0;
156 	}
157 
158 	if (dfs == 0 && hapd->iconf->local_pwr_constraint == -1)
159 		return eid;
160 
161 	/*
162 	 * ieee80211h (DFS) is enabled so Power Constraint element shall
163 	 * be added when running on DFS channel whenever local_pwr_constraint
164 	 * is configured or not. In order to meet regulations when TPC is not
165 	 * implemented using a transmit power that is below the legal maximum
166 	 * (including any mitigation factor) should help. In this case,
167 	 * indicate 3 dB below maximum allowed transmit power.
168 	 */
169 	if (hapd->iconf->local_pwr_constraint == -1)
170 		local_pwr_constraint = 3;
171 
172 	/*
173 	 * A STA that is not an AP shall use a transmit power less than or
174 	 * equal to the local maximum transmit power level for the channel.
175 	 * The local maximum transmit power can be calculated from the formula:
176 	 * local max TX pwr = max TX pwr - local pwr constraint
177 	 * Where max TX pwr is maximum transmit power level specified for
178 	 * channel in Country element and local pwr constraint is specified
179 	 * for channel in this Power Constraint element.
180 	 */
181 
182 	/* Element ID */
183 	*pos++ = WLAN_EID_PWR_CONSTRAINT;
184 	/* Length */
185 	*pos++ = 1;
186 	/* Local Power Constraint */
187 	if (local_pwr_constraint)
188 		*pos++ = local_pwr_constraint;
189 	else
190 		*pos++ = hapd->iconf->local_pwr_constraint;
191 
192 	return pos;
193 }
194 
195 
hostapd_eid_country_add(struct hostapd_data * hapd,u8 * pos,u8 * end,int chan_spacing,struct hostapd_channel_data * start,struct hostapd_channel_data * prev)196 static u8 * hostapd_eid_country_add(struct hostapd_data *hapd, u8 *pos,
197 				    u8 *end, int chan_spacing,
198 				    struct hostapd_channel_data *start,
199 				    struct hostapd_channel_data *prev)
200 {
201 	if (end - pos < 3)
202 		return pos;
203 
204 	/* first channel number */
205 	*pos++ = start->chan;
206 	/* number of channels */
207 	*pos++ = (prev->chan - start->chan) / chan_spacing + 1;
208 	/* maximum transmit power level */
209 	if (!is_6ghz_op_class(hapd->iconf->op_class))
210 		*pos++ = start->max_tx_power;
211 	else
212 		*pos++ = 0; /* Reserved when operating on the 6 GHz band */
213 
214 	return pos;
215 }
216 
217 
hostapd_fill_subband_triplets(struct hostapd_data * hapd,u8 * pos,u8 * end)218 static u8 * hostapd_fill_subband_triplets(struct hostapd_data *hapd, u8 *pos,
219 					    u8 *end)
220 {
221 	int i;
222 	struct hostapd_hw_modes *mode;
223 	struct hostapd_channel_data *start, *prev;
224 	int chan_spacing = 1;
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(hapd, pos, end,
244 						      chan_spacing,
245 						      start, prev);
246 			start = NULL;
247 		}
248 
249 		/* Start new group */
250 		start = prev = chan;
251 	}
252 
253 	if (start) {
254 		pos = hostapd_eid_country_add(hapd, pos, end, chan_spacing,
255 					      start, prev);
256 	}
257 
258 	return pos;
259 }
260 
261 
hostapd_eid_country(struct hostapd_data * hapd,u8 * eid,int max_len)262 static u8 * hostapd_eid_country(struct hostapd_data *hapd, u8 *eid,
263 				int max_len)
264 {
265 	u8 *pos = eid;
266 	u8 *end = eid + max_len;
267 
268 	if (!hapd->iconf->ieee80211d || max_len < 6 ||
269 	    hapd->iface->current_mode == NULL)
270 		return eid;
271 
272 	*pos++ = WLAN_EID_COUNTRY;
273 	pos++; /* length will be set later */
274 	os_memcpy(pos, hapd->iconf->country, 3); /* e.g., 'US ' */
275 	pos += 3;
276 
277 	if (is_6ghz_op_class(hapd->iconf->op_class)) {
278 		/* Force the third octet of the country string to indicate
279 		 * Global Operating Class (Table E-4) */
280 		eid[4] = 0x04;
281 
282 		/* Operating Triplet field */
283 		/* Operating Extension Identifier (>= 201 to indicate this is
284 		 * not a Subband Triplet field) */
285 		*pos++ = 201;
286 		/* Operating Class */
287 		*pos++ = hapd->iconf->op_class;
288 		/* Coverage Class */
289 		*pos++ = 0;
290 		/* Subband Triplets are required only for the 20 MHz case */
291 		if (hapd->iconf->op_class == 131 ||
292 		    hapd->iconf->op_class == 136)
293 			pos = hostapd_fill_subband_triplets(hapd, pos, end);
294 	} else {
295 		pos = hostapd_fill_subband_triplets(hapd, pos, end);
296 	}
297 
298 	if ((pos - eid) & 1) {
299 		if (end - pos < 1)
300 			return eid;
301 		*pos++ = 0; /* pad for 16-bit alignment */
302 	}
303 
304 	eid[1] = (pos - eid) - 2;
305 
306 	return pos;
307 }
308 
309 
hostapd_wpa_ie(struct hostapd_data * hapd,u8 eid)310 const u8 * hostapd_wpa_ie(struct hostapd_data *hapd, u8 eid)
311 {
312 	const u8 *ies;
313 	size_t ies_len;
314 
315 	ies = wpa_auth_get_wpa_ie(hapd->wpa_auth, &ies_len);
316 	if (!ies)
317 		return NULL;
318 
319 	return get_ie(ies, ies_len, eid);
320 }
321 
322 
hostapd_vendor_wpa_ie(struct hostapd_data * hapd,u32 vendor_type)323 static const u8 * hostapd_vendor_wpa_ie(struct hostapd_data *hapd,
324 					u32 vendor_type)
325 {
326 	const u8 *ies;
327 	size_t ies_len;
328 
329 	ies = wpa_auth_get_wpa_ie(hapd->wpa_auth, &ies_len);
330 	if (!ies)
331 		return NULL;
332 
333 	return get_vendor_ie(ies, ies_len, vendor_type);
334 }
335 
336 
hostapd_get_rsne(struct hostapd_data * hapd,u8 * pos,size_t len)337 static u8 * hostapd_get_rsne(struct hostapd_data *hapd, u8 *pos, size_t len)
338 {
339 	const u8 *ie;
340 
341 	ie = hostapd_wpa_ie(hapd, WLAN_EID_RSN);
342 	if (!ie || 2U + ie[1] > len)
343 		return pos;
344 
345 	os_memcpy(pos, ie, 2 + ie[1]);
346 	return pos + 2 + ie[1];
347 }
348 
349 
hostapd_get_mde(struct hostapd_data * hapd,u8 * pos,size_t len)350 static u8 * hostapd_get_mde(struct hostapd_data *hapd, u8 *pos, size_t len)
351 {
352 	const u8 *ie;
353 
354 	ie = hostapd_wpa_ie(hapd, WLAN_EID_MOBILITY_DOMAIN);
355 	if (!ie || 2U + ie[1] > len)
356 		return pos;
357 
358 	os_memcpy(pos, ie, 2 + ie[1]);
359 	return pos + 2 + ie[1];
360 }
361 
362 
hostapd_get_rsnxe(struct hostapd_data * hapd,u8 * pos,size_t len)363 static u8 * hostapd_get_rsnxe(struct hostapd_data *hapd, u8 *pos, size_t len)
364 {
365 	const u8 *ie;
366 
367 #ifdef CONFIG_TESTING_OPTIONS
368 	if (hapd->conf->no_beacon_rsnxe) {
369 		wpa_printf(MSG_INFO, "TESTING: Do not add RSNXE into Beacon");
370 		return pos;
371 	}
372 #endif /* CONFIG_TESTING_OPTIONS */
373 	ie = hostapd_wpa_ie(hapd, WLAN_EID_RSNX);
374 	if (!ie || 2U + ie[1] > len)
375 		return pos;
376 
377 	os_memcpy(pos, ie, 2 + ie[1]);
378 	return pos + 2 + ie[1];
379 }
380 
381 
hostapd_get_wpa_ie(struct hostapd_data * hapd,u8 * pos,size_t len)382 static u8 * hostapd_get_wpa_ie(struct hostapd_data *hapd, u8 *pos, size_t len)
383 {
384 	const u8 *ie;
385 
386 	ie = hostapd_vendor_wpa_ie(hapd, WPA_IE_VENDOR_TYPE);
387 	if (!ie || 2U + ie[1] > len)
388 		return pos;
389 
390 	os_memcpy(pos, ie, 2 + ie[1]);
391 	return pos + 2 + ie[1];
392 }
393 
394 
hostapd_get_osen_ie(struct hostapd_data * hapd,u8 * pos,size_t len)395 static u8 * hostapd_get_osen_ie(struct hostapd_data *hapd, u8 *pos, size_t len)
396 {
397 	const u8 *ie;
398 
399 	ie = hostapd_vendor_wpa_ie(hapd, OSEN_IE_VENDOR_TYPE);
400 	if (!ie || 2U + ie[1] > len)
401 		return pos;
402 
403 	os_memcpy(pos, ie, 2 + ie[1]);
404 	return pos + 2 + ie[1];
405 }
406 
407 
hostapd_eid_csa(struct hostapd_data * hapd,u8 * eid)408 static u8 * hostapd_eid_csa(struct hostapd_data *hapd, u8 *eid)
409 {
410 #ifdef CONFIG_TESTING_OPTIONS
411 	if (hapd->iface->cs_oper_class && hapd->iconf->ecsa_ie_only)
412 		return eid;
413 #endif /* CONFIG_TESTING_OPTIONS */
414 
415 	if (!hapd->cs_freq_params.channel)
416 		return eid;
417 
418 	*eid++ = WLAN_EID_CHANNEL_SWITCH;
419 	*eid++ = 3;
420 	*eid++ = hapd->cs_block_tx;
421 	*eid++ = hapd->cs_freq_params.channel;
422 	*eid++ = hapd->cs_count;
423 
424 	return eid;
425 }
426 
427 
hostapd_eid_ecsa(struct hostapd_data * hapd,u8 * eid)428 static u8 * hostapd_eid_ecsa(struct hostapd_data *hapd, u8 *eid)
429 {
430 	if (!hapd->cs_freq_params.channel || !hapd->iface->cs_oper_class)
431 		return eid;
432 
433 	*eid++ = WLAN_EID_EXT_CHANSWITCH_ANN;
434 	*eid++ = 4;
435 	*eid++ = hapd->cs_block_tx;
436 	*eid++ = hapd->iface->cs_oper_class;
437 	*eid++ = hapd->cs_freq_params.channel;
438 	*eid++ = hapd->cs_count;
439 
440 	return eid;
441 }
442 
443 
hostapd_eid_supported_op_classes(struct hostapd_data * hapd,u8 * eid)444 static u8 * hostapd_eid_supported_op_classes(struct hostapd_data *hapd, u8 *eid)
445 {
446 	u8 op_class, channel;
447 
448 	if (!(hapd->iface->drv_flags & WPA_DRIVER_FLAGS_AP_CSA) ||
449 	    !hapd->iface->freq)
450 		return eid;
451 
452 	if (ieee80211_freq_to_channel_ext(hapd->iface->freq,
453 					  hapd->iconf->secondary_channel,
454 					  hostapd_get_oper_chwidth(hapd->iconf),
455 					  &op_class, &channel) ==
456 	    NUM_HOSTAPD_MODES)
457 		return eid;
458 
459 	*eid++ = WLAN_EID_SUPPORTED_OPERATING_CLASSES;
460 	*eid++ = 2;
461 
462 	/* Current Operating Class */
463 	*eid++ = op_class;
464 
465 	/* TODO: Advertise all the supported operating classes */
466 	*eid++ = 0;
467 
468 	return eid;
469 }
470 
471 
472 static int
ieee802_11_build_ap_params_mbssid(struct hostapd_data * hapd,struct wpa_driver_ap_params * params)473 ieee802_11_build_ap_params_mbssid(struct hostapd_data *hapd,
474 				  struct wpa_driver_ap_params *params)
475 {
476 	struct hostapd_iface *iface = hapd->iface;
477 	struct hostapd_data *tx_bss;
478 	size_t len, rnr_len = 0;
479 	u8 elem_count = 0, *elem = NULL, **elem_offset = NULL, *end;
480 	u8 rnr_elem_count = 0, *rnr_elem = NULL, **rnr_elem_offset = NULL;
481 	size_t i;
482 
483 	if (!iface->mbssid_max_interfaces ||
484 	    iface->num_bss > iface->mbssid_max_interfaces ||
485 	    (iface->conf->mbssid == ENHANCED_MBSSID_ENABLED &&
486 	     !iface->ema_max_periodicity))
487 		goto fail;
488 
489 	/* Make sure bss->xrates_supported is set for all BSSs to know whether
490 	 * it need to be non-inherited. */
491 	for (i = 0; i < iface->num_bss; i++) {
492 		u8 buf[100];
493 
494 		hostapd_eid_ext_supp_rates(iface->bss[i], buf);
495 	}
496 
497 	tx_bss = hostapd_mbssid_get_tx_bss(hapd);
498 	len = hostapd_eid_mbssid_len(tx_bss, WLAN_FC_STYPE_BEACON, &elem_count,
499 				     NULL, 0, &rnr_len);
500 	if (!len || (iface->conf->mbssid == ENHANCED_MBSSID_ENABLED &&
501 		     elem_count > iface->ema_max_periodicity))
502 		goto fail;
503 
504 	elem = os_zalloc(len);
505 	if (!elem)
506 		goto fail;
507 
508 	elem_offset = os_zalloc(elem_count * sizeof(u8 *));
509 	if (!elem_offset)
510 		goto fail;
511 
512 	if (rnr_len) {
513 		rnr_elem = os_zalloc(rnr_len);
514 		if (!rnr_elem)
515 			goto fail;
516 
517 		rnr_elem_offset = os_calloc(elem_count + 1, sizeof(u8 *));
518 		if (!rnr_elem_offset)
519 			goto fail;
520 	}
521 
522 	end = hostapd_eid_mbssid(tx_bss, elem, elem + len, WLAN_FC_STYPE_BEACON,
523 				 elem_count, elem_offset, NULL, 0, rnr_elem,
524 				 &rnr_elem_count, rnr_elem_offset, rnr_len);
525 
526 	params->mbssid_tx_iface = tx_bss->conf->iface;
527 	params->mbssid_index = hostapd_mbssid_get_bss_index(hapd);
528 	params->mbssid_elem = elem;
529 	params->mbssid_elem_len = end - elem;
530 	params->mbssid_elem_count = elem_count;
531 	params->mbssid_elem_offset = elem_offset;
532 	params->rnr_elem = rnr_elem;
533 	params->rnr_elem_len = rnr_len;
534 	params->rnr_elem_count = rnr_elem_count;
535 	params->rnr_elem_offset = rnr_elem_offset;
536 	if (iface->conf->mbssid == ENHANCED_MBSSID_ENABLED)
537 		params->ema = true;
538 
539 	return 0;
540 
541 fail:
542 	os_free(rnr_elem);
543 	os_free(rnr_elem_offset);
544 	os_free(elem_offset);
545 	os_free(elem);
546 	wpa_printf(MSG_ERROR, "MBSSID: Configuration failed");
547 	return -1;
548 }
549 
550 
hostapd_eid_mbssid_config(struct hostapd_data * hapd,u8 * eid,u8 mbssid_elem_count)551 static u8 * hostapd_eid_mbssid_config(struct hostapd_data *hapd, u8 *eid,
552 				      u8 mbssid_elem_count)
553 {
554 	struct hostapd_iface *iface = hapd->iface;
555 
556 	if (iface->conf->mbssid == ENHANCED_MBSSID_ENABLED) {
557 		*eid++ = WLAN_EID_EXTENSION;
558 		*eid++ = 3;
559 		*eid++ = WLAN_EID_EXT_MULTIPLE_BSSID_CONFIGURATION;
560 		*eid++ = iface->num_bss;
561 		*eid++ = mbssid_elem_count;
562 	}
563 
564 	return eid;
565 }
566 
567 
hostapd_gen_probe_resp(struct hostapd_data * hapd,const struct ieee80211_mgmt * req,int is_p2p,size_t * resp_len,const u8 * known_bss,u8 known_bss_len)568 static u8 * hostapd_gen_probe_resp(struct hostapd_data *hapd,
569 				   const struct ieee80211_mgmt *req,
570 				   int is_p2p, size_t *resp_len,
571 				   const u8 *known_bss, u8 known_bss_len)
572 {
573 	struct ieee80211_mgmt *resp;
574 	u8 *pos, *epos, *csa_pos;
575 	size_t buflen;
576 
577 	hapd = hostapd_mbssid_get_tx_bss(hapd);
578 
579 #define MAX_PROBERESP_LEN 768
580 	buflen = MAX_PROBERESP_LEN;
581 #ifdef CONFIG_WPS
582 	if (hapd->wps_probe_resp_ie)
583 		buflen += wpabuf_len(hapd->wps_probe_resp_ie);
584 #endif /* CONFIG_WPS */
585 #ifdef CONFIG_P2P
586 	if (hapd->p2p_probe_resp_ie)
587 		buflen += wpabuf_len(hapd->p2p_probe_resp_ie);
588 #endif /* CONFIG_P2P */
589 #ifdef CONFIG_FST
590 	if (hapd->iface->fst_ies)
591 		buflen += wpabuf_len(hapd->iface->fst_ies);
592 #endif /* CONFIG_FST */
593 	if (hapd->conf->vendor_elements)
594 		buflen += wpabuf_len(hapd->conf->vendor_elements);
595 	if (hapd->conf->vendor_vht) {
596 		buflen += 5 + 2 + sizeof(struct ieee80211_vht_capabilities) +
597 			2 + sizeof(struct ieee80211_vht_operation);
598 	}
599 
600 #ifdef CONFIG_IEEE80211AX
601 	if (hapd->iconf->ieee80211ax && !hapd->conf->disable_11ax) {
602 		buflen += 3 + sizeof(struct ieee80211_he_capabilities) +
603 			3 + sizeof(struct ieee80211_he_operation) +
604 			3 + sizeof(struct ieee80211_he_mu_edca_parameter_set) +
605 			3 + sizeof(struct ieee80211_spatial_reuse);
606 		if (is_6ghz_op_class(hapd->iconf->op_class)) {
607 			buflen += sizeof(struct ieee80211_he_6ghz_oper_info) +
608 				3 + sizeof(struct ieee80211_he_6ghz_band_cap);
609 			 /* An additional Transmit Power Envelope element for
610 			  * subordinate client */
611 			if (hapd->iconf->he_6ghz_reg_pwr_type ==
612 			    HE_6GHZ_INDOOR_AP)
613 				buflen += 4;
614 		}
615 	}
616 #endif /* CONFIG_IEEE80211AX */
617 
618 #ifdef CONFIG_IEEE80211BE
619 	if (hapd->iconf->ieee80211be && !hapd->conf->disable_11be) {
620 		buflen += hostapd_eid_eht_capab_len(hapd, IEEE80211_MODE_AP);
621 		buflen += 3 + sizeof(struct ieee80211_eht_operation);
622 		if (hapd->iconf->punct_bitmap)
623 			buflen += EHT_OPER_DISABLED_SUBCHAN_BITMAP_SIZE;
624 
625 		/*
626 		 * TODO: Multi-Link element has variable length and can be
627 		 * long based on the common info and number of per
628 		 * station profiles. For now use 256.
629 		 */
630 		if (hapd->conf->mld_ap)
631 			buflen += 256;
632 	}
633 #endif /* CONFIG_IEEE80211BE */
634 
635 	buflen += hostapd_eid_mbssid_len(hapd, WLAN_FC_STYPE_PROBE_RESP, NULL,
636 					 known_bss, known_bss_len, NULL);
637 	buflen += hostapd_eid_rnr_len(hapd, WLAN_FC_STYPE_PROBE_RESP);
638 	buflen += hostapd_mbo_ie_len(hapd);
639 	buflen += hostapd_eid_owe_trans_len(hapd);
640 	buflen += hostapd_eid_dpp_cc_len(hapd);
641 
642 	resp = os_zalloc(buflen);
643 	if (resp == NULL)
644 		return NULL;
645 
646 	epos = ((u8 *) resp) + MAX_PROBERESP_LEN;
647 
648 	resp->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
649 					   WLAN_FC_STYPE_PROBE_RESP);
650 	/* Unicast the response to all requests on bands other than 6 GHz. For
651 	 * the 6 GHz, unicast is used only if the actual SSID is not included in
652 	 * the Beacon frames. Otherwise, broadcast response is used per IEEE
653 	 * Std 802.11ax-2021, 26.17.2.3.2. Broadcast address is also used for
654 	 * the Probe Response frame template for the unsolicited (i.e., not as
655 	 * a response to a specific request) case. */
656 	if (req && (!is_6ghz_op_class(hapd->iconf->op_class) ||
657 		    hapd->conf->ignore_broadcast_ssid))
658 		os_memcpy(resp->da, req->sa, ETH_ALEN);
659 	else
660 		os_memset(resp->da, 0xff, ETH_ALEN);
661 
662 	os_memcpy(resp->sa, hapd->own_addr, ETH_ALEN);
663 
664 	os_memcpy(resp->bssid, hapd->own_addr, ETH_ALEN);
665 	resp->u.probe_resp.beacon_int =
666 		host_to_le16(hapd->iconf->beacon_int);
667 
668 	/* hardware or low-level driver will setup seq_ctrl and timestamp */
669 	resp->u.probe_resp.capab_info =
670 		host_to_le16(hostapd_own_capab_info(hapd));
671 
672 	pos = resp->u.probe_resp.variable;
673 	*pos++ = WLAN_EID_SSID;
674 	*pos++ = hapd->conf->ssid.ssid_len;
675 	os_memcpy(pos, hapd->conf->ssid.ssid, hapd->conf->ssid.ssid_len);
676 	pos += hapd->conf->ssid.ssid_len;
677 
678 	/* Supported rates */
679 	pos = hostapd_eid_supp_rates(hapd, pos);
680 
681 	/* DS Params */
682 	pos = hostapd_eid_ds_params(hapd, pos);
683 
684 	pos = hostapd_eid_country(hapd, pos, epos - pos);
685 
686 	/* Power Constraint element */
687 	pos = hostapd_eid_pwr_constraint(hapd, pos);
688 
689 	/* CSA IE */
690 	csa_pos = hostapd_eid_csa(hapd, pos);
691 	if (csa_pos != pos)
692 		hapd->cs_c_off_proberesp = csa_pos - (u8 *) resp - 1;
693 	pos = csa_pos;
694 
695 	/* ERP Information element */
696 	pos = hostapd_eid_erp_info(hapd, pos);
697 
698 	/* Extended supported rates */
699 	pos = hostapd_eid_ext_supp_rates(hapd, pos);
700 
701 	pos = hostapd_get_rsne(hapd, pos, epos - pos);
702 	pos = hostapd_eid_bss_load(hapd, pos, epos - pos);
703 	pos = hostapd_eid_mbssid(hapd, pos, epos, WLAN_FC_STYPE_PROBE_RESP, 0,
704 				 NULL, known_bss, known_bss_len, NULL, NULL,
705 				 NULL, 0);
706 	pos = hostapd_eid_rm_enabled_capab(hapd, pos, epos - pos);
707 	pos = hostapd_get_mde(hapd, pos, epos - pos);
708 
709 	/* eCSA IE */
710 	csa_pos = hostapd_eid_ecsa(hapd, pos);
711 	if (csa_pos != pos)
712 		hapd->cs_c_off_ecsa_proberesp = csa_pos - (u8 *) resp - 1;
713 	pos = csa_pos;
714 
715 	pos = hostapd_eid_supported_op_classes(hapd, pos);
716 	pos = hostapd_eid_ht_capabilities(hapd, pos);
717 	pos = hostapd_eid_ht_operation(hapd, pos);
718 
719 	/* Probe Response frames always include all non-TX profiles except
720 	 * when a list of known BSSes is included in the Probe Request frame. */
721 	pos = hostapd_eid_ext_capab(hapd, pos,
722 				    hapd->iconf->mbssid >= MBSSID_ENABLED &&
723 				    !known_bss_len);
724 
725 	pos = hostapd_eid_time_adv(hapd, pos);
726 	pos = hostapd_eid_time_zone(hapd, pos);
727 
728 	pos = hostapd_eid_interworking(hapd, pos);
729 	pos = hostapd_eid_adv_proto(hapd, pos);
730 	pos = hostapd_eid_roaming_consortium(hapd, pos);
731 
732 #ifdef CONFIG_FST
733 	if (hapd->iface->fst_ies) {
734 		os_memcpy(pos, wpabuf_head(hapd->iface->fst_ies),
735 			  wpabuf_len(hapd->iface->fst_ies));
736 		pos += wpabuf_len(hapd->iface->fst_ies);
737 	}
738 #endif /* CONFIG_FST */
739 
740 #ifdef CONFIG_IEEE80211AC
741 	if (hapd->iconf->ieee80211ac && !hapd->conf->disable_11ac &&
742 	    !is_6ghz_op_class(hapd->iconf->op_class)) {
743 		pos = hostapd_eid_vht_capabilities(hapd, pos, 0);
744 		pos = hostapd_eid_vht_operation(hapd, pos);
745 		pos = hostapd_eid_txpower_envelope(hapd, pos);
746 	}
747 #endif /* CONFIG_IEEE80211AC */
748 
749 #ifdef CONFIG_IEEE80211AX
750 	if (hapd->iconf->ieee80211ax && !hapd->conf->disable_11ax &&
751 	    is_6ghz_op_class(hapd->iconf->op_class))
752 		pos = hostapd_eid_txpower_envelope(hapd, pos);
753 #endif /* CONFIG_IEEE80211AX */
754 
755 	pos = hostapd_eid_wb_chsw_wrapper(hapd, pos);
756 
757 	pos = hostapd_eid_rnr(hapd, pos, WLAN_FC_STYPE_PROBE_RESP);
758 	pos = hostapd_eid_fils_indic(hapd, pos, 0);
759 	pos = hostapd_get_rsnxe(hapd, pos, epos - pos);
760 
761 #ifdef CONFIG_IEEE80211AX
762 	if (hapd->iconf->ieee80211ax && !hapd->conf->disable_11ax) {
763 		u8 *cca_pos;
764 
765 		pos = hostapd_eid_he_capab(hapd, pos, IEEE80211_MODE_AP);
766 		pos = hostapd_eid_he_operation(hapd, pos);
767 
768 		/* BSS Color Change Announcement element */
769 		cca_pos = hostapd_eid_cca(hapd, pos);
770 		if (cca_pos != pos)
771 			hapd->cca_c_off_proberesp = cca_pos - (u8 *) resp - 2;
772 		pos = cca_pos;
773 
774 		pos = hostapd_eid_spatial_reuse(hapd, pos);
775 		pos = hostapd_eid_he_mu_edca_parameter_set(hapd, pos);
776 		pos = hostapd_eid_he_6ghz_band_cap(hapd, pos);
777 	}
778 #endif /* CONFIG_IEEE80211AX */
779 
780 #ifdef CONFIG_IEEE80211BE
781 	if (hapd->iconf->ieee80211be && !hapd->conf->disable_11be) {
782 		if (hapd->conf->mld_ap)
783 			pos = hostapd_eid_eht_basic_ml(hapd, pos, NULL, true);
784 		pos = hostapd_eid_eht_capab(hapd, pos, IEEE80211_MODE_AP);
785 		pos = hostapd_eid_eht_operation(hapd, pos);
786 	}
787 #endif /* CONFIG_IEEE80211BE */
788 
789 #ifdef CONFIG_IEEE80211AC
790 	if (hapd->conf->vendor_vht)
791 		pos = hostapd_eid_vendor_vht(hapd, pos);
792 #endif /* CONFIG_IEEE80211AC */
793 
794 	/* WPA / OSEN */
795 	pos = hostapd_get_wpa_ie(hapd, pos, epos - pos);
796 	pos = hostapd_get_osen_ie(hapd, pos, epos - pos);
797 
798 	/* Wi-Fi Alliance WMM */
799 	pos = hostapd_eid_wmm(hapd, pos);
800 
801 #ifdef CONFIG_WPS
802 	if (hapd->conf->wps_state && hapd->wps_probe_resp_ie) {
803 		os_memcpy(pos, wpabuf_head(hapd->wps_probe_resp_ie),
804 			  wpabuf_len(hapd->wps_probe_resp_ie));
805 		pos += wpabuf_len(hapd->wps_probe_resp_ie);
806 	}
807 #endif /* CONFIG_WPS */
808 
809 #ifdef CONFIG_P2P
810 	if ((hapd->conf->p2p & P2P_ENABLED) && is_p2p &&
811 	    hapd->p2p_probe_resp_ie) {
812 		os_memcpy(pos, wpabuf_head(hapd->p2p_probe_resp_ie),
813 			  wpabuf_len(hapd->p2p_probe_resp_ie));
814 		pos += wpabuf_len(hapd->p2p_probe_resp_ie);
815 	}
816 #endif /* CONFIG_P2P */
817 #ifdef CONFIG_P2P_MANAGER
818 	if ((hapd->conf->p2p & (P2P_MANAGE | P2P_ENABLED | P2P_GROUP_OWNER)) ==
819 	    P2P_MANAGE)
820 		pos = hostapd_eid_p2p_manage(hapd, pos);
821 #endif /* CONFIG_P2P_MANAGER */
822 
823 #ifdef CONFIG_HS20
824 	pos = hostapd_eid_hs20_indication(hapd, pos);
825 #endif /* CONFIG_HS20 */
826 
827 	pos = hostapd_eid_mbo(hapd, pos, (u8 *) resp + buflen - pos);
828 	pos = hostapd_eid_owe_trans(hapd, pos, (u8 *) resp + buflen - pos);
829 	pos = hostapd_eid_dpp_cc(hapd, pos, (u8 *) resp + buflen - pos);
830 
831 	if (hapd->conf->vendor_elements) {
832 		os_memcpy(pos, wpabuf_head(hapd->conf->vendor_elements),
833 			  wpabuf_len(hapd->conf->vendor_elements));
834 		pos += wpabuf_len(hapd->conf->vendor_elements);
835 	}
836 
837 	*resp_len = pos - (u8 *) resp;
838 	return (u8 *) resp;
839 }
840 
841 
842 enum ssid_match_result {
843 	NO_SSID_MATCH,
844 	EXACT_SSID_MATCH,
845 	WILDCARD_SSID_MATCH,
846 	CO_LOCATED_SSID_MATCH,
847 };
848 
ssid_match(struct hostapd_data * hapd,const u8 * ssid,size_t ssid_len,const u8 * ssid_list,size_t ssid_list_len,const u8 * short_ssid_list,size_t short_ssid_list_len)849 static enum ssid_match_result ssid_match(struct hostapd_data *hapd,
850 					 const u8 *ssid, size_t ssid_len,
851 					 const u8 *ssid_list,
852 					 size_t ssid_list_len,
853 					 const u8 *short_ssid_list,
854 					 size_t short_ssid_list_len)
855 {
856 	const u8 *pos, *end;
857 	struct hostapd_iface *iface = hapd->iface;
858 	int wildcard = 0;
859 	size_t i, j;
860 
861 	if (ssid_len == 0)
862 		wildcard = 1;
863 	if (ssid_len == hapd->conf->ssid.ssid_len &&
864 	    os_memcmp(ssid, hapd->conf->ssid.ssid, ssid_len) == 0)
865 		return EXACT_SSID_MATCH;
866 
867 	if (ssid_list) {
868 		pos = ssid_list;
869 		end = ssid_list + ssid_list_len;
870 		while (end - pos >= 2) {
871 			if (2 + pos[1] > end - pos)
872 				break;
873 			if (pos[1] == 0)
874 				wildcard = 1;
875 			if (pos[1] == hapd->conf->ssid.ssid_len &&
876 			    os_memcmp(pos + 2, hapd->conf->ssid.ssid,
877 				      pos[1]) == 0)
878 				return EXACT_SSID_MATCH;
879 			pos += 2 + pos[1];
880 		}
881 	}
882 
883 	if (short_ssid_list) {
884 		pos = short_ssid_list;
885 		end = short_ssid_list + short_ssid_list_len;
886 		while (end - pos >= 4) {
887 			if (hapd->conf->ssid.short_ssid == WPA_GET_LE32(pos))
888 				return EXACT_SSID_MATCH;
889 			pos += 4;
890 		}
891 	}
892 
893 	if (wildcard)
894 		return WILDCARD_SSID_MATCH;
895 
896 	if (!iface->interfaces || iface->interfaces->count <= 1 ||
897 	    is_6ghz_op_class(hapd->iconf->op_class))
898 		return NO_SSID_MATCH;
899 
900 	for (i = 0; i < iface->interfaces->count; i++) {
901 		struct hostapd_iface *colocated;
902 
903 		colocated = iface->interfaces->iface[i];
904 
905 		if (colocated == iface ||
906 		    !is_6ghz_op_class(colocated->conf->op_class))
907 			continue;
908 
909 		for (j = 0; j < colocated->num_bss; j++) {
910 			struct hostapd_bss_config *conf;
911 
912 			conf = colocated->bss[j]->conf;
913 			if (ssid_len == conf->ssid.ssid_len &&
914 			    os_memcmp(ssid, conf->ssid.ssid, ssid_len) == 0)
915 				return CO_LOCATED_SSID_MATCH;
916 		}
917 	}
918 
919 	return NO_SSID_MATCH;
920 }
921 
922 
sta_track_expire(struct hostapd_iface * iface,int force)923 void sta_track_expire(struct hostapd_iface *iface, int force)
924 {
925 	struct os_reltime now;
926 	struct hostapd_sta_info *info;
927 
928 	if (!iface->num_sta_seen)
929 		return;
930 
931 	os_get_reltime(&now);
932 	while ((info = dl_list_first(&iface->sta_seen, struct hostapd_sta_info,
933 				     list))) {
934 		if (!force &&
935 		    !os_reltime_expired(&now, &info->last_seen,
936 					iface->conf->track_sta_max_age))
937 			break;
938 		force = 0;
939 
940 		wpa_printf(MSG_MSGDUMP, "%s: Expire STA tracking entry for "
941 			   MACSTR, iface->bss[0]->conf->iface,
942 			   MAC2STR(info->addr));
943 		dl_list_del(&info->list);
944 		iface->num_sta_seen--;
945 		sta_track_del(info);
946 	}
947 }
948 
949 
sta_track_get(struct hostapd_iface * iface,const u8 * addr)950 static struct hostapd_sta_info * sta_track_get(struct hostapd_iface *iface,
951 					       const u8 *addr)
952 {
953 	struct hostapd_sta_info *info;
954 
955 	dl_list_for_each(info, &iface->sta_seen, struct hostapd_sta_info, list)
956 		if (os_memcmp(addr, info->addr, ETH_ALEN) == 0)
957 			return info;
958 
959 	return NULL;
960 }
961 
962 
sta_track_add(struct hostapd_iface * iface,const u8 * addr,int ssi_signal)963 void sta_track_add(struct hostapd_iface *iface, const u8 *addr, int ssi_signal)
964 {
965 	struct hostapd_sta_info *info;
966 
967 	info = sta_track_get(iface, addr);
968 	if (info) {
969 		/* Move the most recent entry to the end of the list */
970 		dl_list_del(&info->list);
971 		dl_list_add_tail(&iface->sta_seen, &info->list);
972 		os_get_reltime(&info->last_seen);
973 		info->ssi_signal = ssi_signal;
974 		return;
975 	}
976 
977 	/* Add a new entry */
978 	info = os_zalloc(sizeof(*info));
979 	if (info == NULL)
980 		return;
981 	os_memcpy(info->addr, addr, ETH_ALEN);
982 	os_get_reltime(&info->last_seen);
983 	info->ssi_signal = ssi_signal;
984 
985 	if (iface->num_sta_seen >= iface->conf->track_sta_max_num) {
986 		/* Expire oldest entry to make room for a new one */
987 		sta_track_expire(iface, 1);
988 	}
989 
990 	wpa_printf(MSG_MSGDUMP, "%s: Add STA tracking entry for "
991 		   MACSTR, iface->bss[0]->conf->iface, MAC2STR(addr));
992 	dl_list_add_tail(&iface->sta_seen, &info->list);
993 	iface->num_sta_seen++;
994 }
995 
996 
997 struct hostapd_data *
sta_track_seen_on(struct hostapd_iface * iface,const u8 * addr,const char * ifname)998 sta_track_seen_on(struct hostapd_iface *iface, const u8 *addr,
999 		  const char *ifname)
1000 {
1001 	struct hapd_interfaces *interfaces = iface->interfaces;
1002 	size_t i, j;
1003 
1004 	for (i = 0; i < interfaces->count; i++) {
1005 		struct hostapd_data *hapd = NULL;
1006 
1007 		iface = interfaces->iface[i];
1008 		for (j = 0; j < iface->num_bss; j++) {
1009 			hapd = iface->bss[j];
1010 			if (os_strcmp(ifname, hapd->conf->iface) == 0)
1011 				break;
1012 			hapd = NULL;
1013 		}
1014 
1015 		if (hapd && sta_track_get(iface, addr))
1016 			return hapd;
1017 	}
1018 
1019 	return NULL;
1020 }
1021 
1022 
1023 #ifdef CONFIG_TAXONOMY
sta_track_claim_taxonomy_info(struct hostapd_iface * iface,const u8 * addr,struct wpabuf ** probe_ie_taxonomy)1024 void sta_track_claim_taxonomy_info(struct hostapd_iface *iface, const u8 *addr,
1025 				   struct wpabuf **probe_ie_taxonomy)
1026 {
1027 	struct hostapd_sta_info *info;
1028 
1029 	info = sta_track_get(iface, addr);
1030 	if (!info)
1031 		return;
1032 
1033 	wpabuf_free(*probe_ie_taxonomy);
1034 	*probe_ie_taxonomy = info->probe_ie_taxonomy;
1035 	info->probe_ie_taxonomy = NULL;
1036 }
1037 #endif /* CONFIG_TAXONOMY */
1038 
1039 
handle_probe_req(struct hostapd_data * hapd,const struct ieee80211_mgmt * mgmt,size_t len,int ssi_signal)1040 void handle_probe_req(struct hostapd_data *hapd,
1041 		      const struct ieee80211_mgmt *mgmt, size_t len,
1042 		      int ssi_signal)
1043 {
1044 	u8 *resp;
1045 	struct ieee802_11_elems elems;
1046 	const u8 *ie;
1047 	size_t ie_len;
1048 	size_t i, resp_len;
1049 	int noack;
1050 	enum ssid_match_result res;
1051 	int ret;
1052 	u16 csa_offs[2];
1053 	size_t csa_offs_len;
1054 	struct radius_sta rad_info;
1055 
1056 	if (hapd->iconf->rssi_ignore_probe_request && ssi_signal &&
1057 	    ssi_signal < hapd->iconf->rssi_ignore_probe_request)
1058 		return;
1059 
1060 	if (len < IEEE80211_HDRLEN)
1061 		return;
1062 	ie = ((const u8 *) mgmt) + IEEE80211_HDRLEN;
1063 	if (hapd->iconf->track_sta_max_num)
1064 		sta_track_add(hapd->iface, mgmt->sa, ssi_signal);
1065 	ie_len = len - IEEE80211_HDRLEN;
1066 
1067 	ret = hostapd_allowed_address(hapd, mgmt->sa, (const u8 *) mgmt, len,
1068 				      &rad_info, 1);
1069 	if (ret == HOSTAPD_ACL_REJECT) {
1070 		wpa_msg(hapd->msg_ctx, MSG_DEBUG,
1071 			"Ignore Probe Request frame from " MACSTR
1072 			" due to ACL reject ", MAC2STR(mgmt->sa));
1073 		return;
1074 	}
1075 
1076 	for (i = 0; hapd->probereq_cb && i < hapd->num_probereq_cb; i++)
1077 		if (hapd->probereq_cb[i].cb(hapd->probereq_cb[i].ctx,
1078 					    mgmt->sa, mgmt->da, mgmt->bssid,
1079 					    ie, ie_len, ssi_signal) > 0)
1080 			return;
1081 
1082 	if (!hapd->conf->send_probe_response)
1083 		return;
1084 
1085 	if (ieee802_11_parse_elems(ie, ie_len, &elems, 0) == ParseFailed) {
1086 		wpa_printf(MSG_DEBUG, "Could not parse ProbeReq from " MACSTR,
1087 			   MAC2STR(mgmt->sa));
1088 		return;
1089 	}
1090 
1091 	if ((!elems.ssid || !elems.supp_rates)) {
1092 		wpa_printf(MSG_DEBUG, "STA " MACSTR " sent probe request "
1093 			   "without SSID or supported rates element",
1094 			   MAC2STR(mgmt->sa));
1095 		return;
1096 	}
1097 
1098 	/*
1099 	 * No need to reply if the Probe Request frame was sent on an adjacent
1100 	 * channel. IEEE Std 802.11-2012 describes this as a requirement for an
1101 	 * AP with dot11RadioMeasurementActivated set to true, but strictly
1102 	 * speaking does not allow such ignoring of Probe Request frames if
1103 	 * dot11RadioMeasurementActivated is false. Anyway, this can help reduce
1104 	 * number of unnecessary Probe Response frames for cases where the STA
1105 	 * is less likely to see them (Probe Request frame sent on a
1106 	 * neighboring, but partially overlapping, channel).
1107 	 */
1108 	if (elems.ds_params &&
1109 	    hapd->iface->current_mode &&
1110 	    (hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G ||
1111 	     hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211B) &&
1112 	    hapd->iconf->channel != elems.ds_params[0]) {
1113 		wpa_printf(MSG_DEBUG,
1114 			   "Ignore Probe Request due to DS Params mismatch: chan=%u != ds.chan=%u",
1115 			   hapd->iconf->channel, elems.ds_params[0]);
1116 		return;
1117 	}
1118 
1119 #ifdef CONFIG_P2P
1120 	if (hapd->p2p && hapd->p2p_group && elems.wps_ie) {
1121 		struct wpabuf *wps;
1122 		wps = ieee802_11_vendor_ie_concat(ie, ie_len, WPS_DEV_OUI_WFA);
1123 		if (wps && !p2p_group_match_dev_type(hapd->p2p_group, wps)) {
1124 			wpa_printf(MSG_MSGDUMP, "P2P: Ignore Probe Request "
1125 				   "due to mismatch with Requested Device "
1126 				   "Type");
1127 			wpabuf_free(wps);
1128 			return;
1129 		}
1130 		wpabuf_free(wps);
1131 	}
1132 
1133 	if (hapd->p2p && hapd->p2p_group && elems.p2p) {
1134 		struct wpabuf *p2p;
1135 		p2p = ieee802_11_vendor_ie_concat(ie, ie_len, P2P_IE_VENDOR_TYPE);
1136 		if (p2p && !p2p_group_match_dev_id(hapd->p2p_group, p2p)) {
1137 			wpa_printf(MSG_MSGDUMP, "P2P: Ignore Probe Request "
1138 				   "due to mismatch with Device ID");
1139 			wpabuf_free(p2p);
1140 			return;
1141 		}
1142 		wpabuf_free(p2p);
1143 	}
1144 #endif /* CONFIG_P2P */
1145 
1146 	if (hapd->conf->ignore_broadcast_ssid && elems.ssid_len == 0 &&
1147 	    elems.ssid_list_len == 0 && elems.short_ssid_list_len == 0) {
1148 		wpa_printf(MSG_MSGDUMP, "Probe Request from " MACSTR " for "
1149 			   "broadcast SSID ignored", MAC2STR(mgmt->sa));
1150 		return;
1151 	}
1152 
1153 #ifdef CONFIG_P2P
1154 	if ((hapd->conf->p2p & P2P_GROUP_OWNER) &&
1155 	    elems.ssid_len == P2P_WILDCARD_SSID_LEN &&
1156 	    os_memcmp(elems.ssid, P2P_WILDCARD_SSID,
1157 		      P2P_WILDCARD_SSID_LEN) == 0) {
1158 		/* Process P2P Wildcard SSID like Wildcard SSID */
1159 		elems.ssid_len = 0;
1160 	}
1161 #endif /* CONFIG_P2P */
1162 
1163 #ifdef CONFIG_TAXONOMY
1164 	{
1165 		struct sta_info *sta;
1166 		struct hostapd_sta_info *info;
1167 
1168 		if ((sta = ap_get_sta(hapd, mgmt->sa)) != NULL) {
1169 			taxonomy_sta_info_probe_req(hapd, sta, ie, ie_len);
1170 		} else if ((info = sta_track_get(hapd->iface,
1171 						 mgmt->sa)) != NULL) {
1172 			taxonomy_hostapd_sta_info_probe_req(hapd, info,
1173 							    ie, ie_len);
1174 		}
1175 	}
1176 #endif /* CONFIG_TAXONOMY */
1177 
1178 	res = ssid_match(hapd, elems.ssid, elems.ssid_len,
1179 			 elems.ssid_list, elems.ssid_list_len,
1180 			 elems.short_ssid_list, elems.short_ssid_list_len);
1181 	if (res == NO_SSID_MATCH) {
1182 		if (!(mgmt->da[0] & 0x01)) {
1183 			wpa_printf(MSG_MSGDUMP, "Probe Request from " MACSTR
1184 				   " for foreign SSID '%s' (DA " MACSTR ")%s",
1185 				   MAC2STR(mgmt->sa),
1186 				   wpa_ssid_txt(elems.ssid, elems.ssid_len),
1187 				   MAC2STR(mgmt->da),
1188 				   elems.ssid_list ? " (SSID list)" : "");
1189 		}
1190 		return;
1191 	}
1192 
1193 	if (hapd->conf->ignore_broadcast_ssid && res == WILDCARD_SSID_MATCH) {
1194 		wpa_printf(MSG_MSGDUMP, "Probe Request from " MACSTR " for "
1195 			   "broadcast SSID ignored", MAC2STR(mgmt->sa));
1196 		return;
1197 	}
1198 
1199 #ifdef CONFIG_INTERWORKING
1200 	if (hapd->conf->interworking &&
1201 	    elems.interworking && elems.interworking_len >= 1) {
1202 		u8 ant = elems.interworking[0] & 0x0f;
1203 		if (ant != INTERWORKING_ANT_WILDCARD &&
1204 		    ant != hapd->conf->access_network_type) {
1205 			wpa_printf(MSG_MSGDUMP, "Probe Request from " MACSTR
1206 				   " for mismatching ANT %u ignored",
1207 				   MAC2STR(mgmt->sa), ant);
1208 			return;
1209 		}
1210 	}
1211 
1212 	if (hapd->conf->interworking && elems.interworking &&
1213 	    (elems.interworking_len == 7 || elems.interworking_len == 9)) {
1214 		const u8 *hessid;
1215 		if (elems.interworking_len == 7)
1216 			hessid = elems.interworking + 1;
1217 		else
1218 			hessid = elems.interworking + 1 + 2;
1219 		if (!is_broadcast_ether_addr(hessid) &&
1220 		    os_memcmp(hessid, hapd->conf->hessid, ETH_ALEN) != 0) {
1221 			wpa_printf(MSG_MSGDUMP, "Probe Request from " MACSTR
1222 				   " for mismatching HESSID " MACSTR
1223 				   " ignored",
1224 				   MAC2STR(mgmt->sa), MAC2STR(hessid));
1225 			return;
1226 		}
1227 	}
1228 #endif /* CONFIG_INTERWORKING */
1229 
1230 #ifdef CONFIG_P2P
1231 	if ((hapd->conf->p2p & P2P_GROUP_OWNER) &&
1232 	    supp_rates_11b_only(&elems)) {
1233 		/* Indicates support for 11b rates only */
1234 		wpa_printf(MSG_EXCESSIVE, "P2P: Ignore Probe Request from "
1235 			   MACSTR " with only 802.11b rates",
1236 			   MAC2STR(mgmt->sa));
1237 		return;
1238 	}
1239 #endif /* CONFIG_P2P */
1240 
1241 	/* TODO: verify that supp_rates contains at least one matching rate
1242 	 * with AP configuration */
1243 
1244 	if (hapd->conf->no_probe_resp_if_seen_on &&
1245 	    is_multicast_ether_addr(mgmt->da) &&
1246 	    is_multicast_ether_addr(mgmt->bssid) &&
1247 	    sta_track_seen_on(hapd->iface, mgmt->sa,
1248 			      hapd->conf->no_probe_resp_if_seen_on)) {
1249 		wpa_printf(MSG_MSGDUMP, "%s: Ignore Probe Request from " MACSTR
1250 			   " since STA has been seen on %s",
1251 			   hapd->conf->iface, MAC2STR(mgmt->sa),
1252 			   hapd->conf->no_probe_resp_if_seen_on);
1253 		return;
1254 	}
1255 
1256 	if (hapd->conf->no_probe_resp_if_max_sta &&
1257 	    is_multicast_ether_addr(mgmt->da) &&
1258 	    is_multicast_ether_addr(mgmt->bssid) &&
1259 	    hapd->num_sta >= hapd->conf->max_num_sta &&
1260 	    !ap_get_sta(hapd, mgmt->sa)) {
1261 		wpa_printf(MSG_MSGDUMP, "%s: Ignore Probe Request from " MACSTR
1262 			   " since no room for additional STA",
1263 			   hapd->conf->iface, MAC2STR(mgmt->sa));
1264 		return;
1265 	}
1266 
1267 #ifdef CONFIG_TESTING_OPTIONS
1268 	if (hapd->iconf->ignore_probe_probability > 0.0 &&
1269 	    drand48() < hapd->iconf->ignore_probe_probability) {
1270 		wpa_printf(MSG_INFO,
1271 			   "TESTING: ignoring probe request from " MACSTR,
1272 			   MAC2STR(mgmt->sa));
1273 		return;
1274 	}
1275 #endif /* CONFIG_TESTING_OPTIONS */
1276 
1277 	/* Do not send Probe Response frame from a non-transmitting multiple
1278 	 * BSSID profile unless the Probe Request frame is directed at that
1279 	 * particular BSS. */
1280 	if (hapd != hostapd_mbssid_get_tx_bss(hapd) && res != EXACT_SSID_MATCH)
1281 		return;
1282 
1283 	wpa_msg_ctrl(hapd->msg_ctx, MSG_INFO, RX_PROBE_REQUEST "sa=" MACSTR
1284 		     " signal=%d", MAC2STR(mgmt->sa), ssi_signal);
1285 
1286 	resp = hostapd_gen_probe_resp(hapd, mgmt, elems.p2p != NULL,
1287 				      &resp_len, elems.mbssid_known_bss,
1288 				      elems.mbssid_known_bss_len);
1289 	if (resp == NULL)
1290 		return;
1291 
1292 	/*
1293 	 * If this is a broadcast probe request, apply no ack policy to avoid
1294 	 * excessive retries.
1295 	 */
1296 	noack = !!(res == WILDCARD_SSID_MATCH &&
1297 		   is_broadcast_ether_addr(mgmt->da));
1298 
1299 	csa_offs_len = 0;
1300 	if (hapd->csa_in_progress) {
1301 		if (hapd->cs_c_off_proberesp)
1302 			csa_offs[csa_offs_len++] =
1303 				hapd->cs_c_off_proberesp;
1304 
1305 		if (hapd->cs_c_off_ecsa_proberesp)
1306 			csa_offs[csa_offs_len++] =
1307 				hapd->cs_c_off_ecsa_proberesp;
1308 	}
1309 
1310 	ret = hostapd_drv_send_mlme(hostapd_mbssid_get_tx_bss(hapd), resp,
1311 				    resp_len, noack,
1312 				    csa_offs_len ? csa_offs : NULL,
1313 				    csa_offs_len, 0);
1314 
1315 	if (ret < 0)
1316 		wpa_printf(MSG_INFO, "handle_probe_req: send failed");
1317 
1318 	os_free(resp);
1319 
1320 	wpa_printf(MSG_EXCESSIVE, "STA " MACSTR " sent probe request for %s "
1321 		   "SSID", MAC2STR(mgmt->sa),
1322 		   elems.ssid_len == 0 ? "broadcast" : "our");
1323 }
1324 
1325 
hostapd_probe_resp_offloads(struct hostapd_data * hapd,size_t * resp_len)1326 static u8 * hostapd_probe_resp_offloads(struct hostapd_data *hapd,
1327 					size_t *resp_len)
1328 {
1329 	/* check probe response offloading caps and print warnings */
1330 	if (!(hapd->iface->drv_flags & WPA_DRIVER_FLAGS_PROBE_RESP_OFFLOAD))
1331 		return NULL;
1332 
1333 #ifdef CONFIG_WPS
1334 	if (hapd->conf->wps_state && hapd->wps_probe_resp_ie &&
1335 	    (!(hapd->iface->probe_resp_offloads &
1336 	       (WPA_DRIVER_PROBE_RESP_OFFLOAD_WPS |
1337 		WPA_DRIVER_PROBE_RESP_OFFLOAD_WPS2))))
1338 		wpa_printf(MSG_WARNING, "Device is trying to offload WPS "
1339 			   "Probe Response while not supporting this");
1340 #endif /* CONFIG_WPS */
1341 
1342 #ifdef CONFIG_P2P
1343 	if ((hapd->conf->p2p & P2P_ENABLED) && hapd->p2p_probe_resp_ie &&
1344 	    !(hapd->iface->probe_resp_offloads &
1345 	      WPA_DRIVER_PROBE_RESP_OFFLOAD_P2P))
1346 		wpa_printf(MSG_WARNING, "Device is trying to offload P2P "
1347 			   "Probe Response while not supporting this");
1348 #endif  /* CONFIG_P2P */
1349 
1350 	if (hapd->conf->interworking &&
1351 	    !(hapd->iface->probe_resp_offloads &
1352 	      WPA_DRIVER_PROBE_RESP_OFFLOAD_INTERWORKING))
1353 		wpa_printf(MSG_WARNING, "Device is trying to offload "
1354 			   "Interworking Probe Response while not supporting "
1355 			   "this");
1356 
1357 	/* Generate a Probe Response template for the non-P2P case */
1358 	return hostapd_gen_probe_resp(hapd, NULL, 0, resp_len, NULL, 0);
1359 }
1360 
1361 #endif /* NEED_AP_MLME */
1362 
1363 
1364 #ifdef CONFIG_IEEE80211AX
1365 /* Unsolicited broadcast Probe Response transmission, 6 GHz only */
hostapd_unsol_bcast_probe_resp(struct hostapd_data * hapd,struct wpa_driver_ap_params * params)1366 static u8 * hostapd_unsol_bcast_probe_resp(struct hostapd_data *hapd,
1367 					   struct wpa_driver_ap_params *params)
1368 {
1369 	if (!is_6ghz_op_class(hapd->iconf->op_class))
1370 		return NULL;
1371 
1372 	params->unsol_bcast_probe_resp_interval =
1373 		hapd->conf->unsol_bcast_probe_resp_interval;
1374 
1375 	return hostapd_gen_probe_resp(hapd, NULL, 0,
1376 				      &params->unsol_bcast_probe_resp_tmpl_len,
1377 				      NULL, 0);
1378 }
1379 #endif /* CONFIG_IEEE80211AX */
1380 
1381 
sta_track_del(struct hostapd_sta_info * info)1382 void sta_track_del(struct hostapd_sta_info *info)
1383 {
1384 #ifdef CONFIG_TAXONOMY
1385 	wpabuf_free(info->probe_ie_taxonomy);
1386 	info->probe_ie_taxonomy = NULL;
1387 #endif /* CONFIG_TAXONOMY */
1388 	os_free(info);
1389 }
1390 
1391 
1392 #ifdef CONFIG_FILS
1393 
hostapd_gen_fils_discovery_phy_index(struct hostapd_data * hapd)1394 static u16 hostapd_gen_fils_discovery_phy_index(struct hostapd_data *hapd)
1395 {
1396 #ifdef CONFIG_IEEE80211BE
1397 	if (hapd->iconf->ieee80211be && !hapd->conf->disable_11be)
1398 		return FD_CAP_PHY_INDEX_EHT;
1399 #endif /* CONFIG_IEEE80211BE */
1400 
1401 #ifdef CONFIG_IEEE80211AX
1402 	if (hapd->iconf->ieee80211ax && !hapd->conf->disable_11ax)
1403 		return FD_CAP_PHY_INDEX_HE;
1404 #endif /* CONFIG_IEEE80211AX */
1405 
1406 #ifdef CONFIG_IEEE80211AC
1407 	if (hapd->iconf->ieee80211ac && !hapd->conf->disable_11ac)
1408 		return FD_CAP_PHY_INDEX_VHT;
1409 #endif /* CONFIG_IEEE80211AC */
1410 
1411 	if (hapd->iconf->ieee80211n && !hapd->conf->disable_11n)
1412 		return FD_CAP_PHY_INDEX_HT;
1413 
1414 	return 0;
1415 }
1416 
1417 
hostapd_gen_fils_discovery_nss(struct hostapd_hw_modes * mode,u16 phy_index,u8 he_mcs_nss_size)1418 static u16 hostapd_gen_fils_discovery_nss(struct hostapd_hw_modes *mode,
1419 					  u16 phy_index, u8 he_mcs_nss_size)
1420 {
1421 	u16 nss = 0;
1422 
1423 	if (!mode)
1424 		return 0;
1425 
1426 	if (phy_index == FD_CAP_PHY_INDEX_HE) {
1427 		const u8 *he_mcs = mode->he_capab[IEEE80211_MODE_AP].mcs;
1428 		int i;
1429 		u16 mcs[6];
1430 
1431 		os_memset(mcs, 0xff, 6 * sizeof(u16));
1432 
1433 		if (he_mcs_nss_size == 4) {
1434 			mcs[0] = WPA_GET_LE16(&he_mcs[0]);
1435 			mcs[1] = WPA_GET_LE16(&he_mcs[2]);
1436 		}
1437 
1438 		if (he_mcs_nss_size == 8) {
1439 			mcs[2] = WPA_GET_LE16(&he_mcs[4]);
1440 			mcs[3] = WPA_GET_LE16(&he_mcs[6]);
1441 		}
1442 
1443 		if (he_mcs_nss_size == 12) {
1444 			mcs[4] = WPA_GET_LE16(&he_mcs[8]);
1445 			mcs[5] = WPA_GET_LE16(&he_mcs[10]);
1446 		}
1447 
1448 		for (i = 0; i < HE_NSS_MAX_STREAMS; i++) {
1449 			u16 nss_mask = 0x3 << (i * 2);
1450 
1451 			/*
1452 			 * If Tx and/or Rx indicate support for a given NSS,
1453 			 * count it towards the maximum NSS.
1454 			 */
1455 			if (he_mcs_nss_size == 4 &&
1456 			    (((mcs[0] & nss_mask) != nss_mask) ||
1457 			     ((mcs[1] & nss_mask) != nss_mask))) {
1458 				nss++;
1459 				continue;
1460 			}
1461 
1462 			if (he_mcs_nss_size == 8 &&
1463 			    (((mcs[2] & nss_mask) != nss_mask) ||
1464 			     ((mcs[3] & nss_mask) != nss_mask))) {
1465 				nss++;
1466 				continue;
1467 			}
1468 
1469 			if (he_mcs_nss_size == 12 &&
1470 			    (((mcs[4] & nss_mask) != nss_mask) ||
1471 			     ((mcs[5] & nss_mask) != nss_mask))) {
1472 				nss++;
1473 				continue;
1474 			}
1475 		}
1476 	} else if (phy_index == FD_CAP_PHY_INDEX_EHT) {
1477 		u8 rx_nss, tx_nss, max_nss = 0, i;
1478 		u8 *mcs = mode->eht_capab[IEEE80211_MODE_AP].mcs;
1479 
1480 		/*
1481 		 * The Supported EHT-MCS And NSS Set field for the AP contains
1482 		 * one to three EHT-MCS Map fields based on the supported
1483 		 * bandwidth. Check the first byte (max NSS for Rx/Tx that
1484 		 * supports EHT-MCS 0-9) for each bandwidth (<= 80,
1485 		 * 160, 320) to find the maximum NSS. This assumes that
1486 		 * the lowest MCS rates support the largest number of spatial
1487 		 * streams. If values are different between Tx, Rx or the
1488 		 * bandwidths, choose the highest value.
1489 		 */
1490 		for (i = 0; i < 3; i++) {
1491 			rx_nss = mcs[3 * i] & 0x0F;
1492 			if (rx_nss > max_nss)
1493 				max_nss = rx_nss;
1494 
1495 			tx_nss = (mcs[3 * i] & 0xF0) >> 4;
1496 			if (tx_nss > max_nss)
1497 				max_nss = tx_nss;
1498 		}
1499 
1500 		nss = max_nss;
1501 	}
1502 
1503 	if (nss > 4)
1504 		return FD_CAP_NSS_5_8 << FD_CAP_NSS_SHIFT;
1505 	if (nss)
1506 		return (nss - 1) << FD_CAP_NSS_SHIFT;
1507 
1508 	return 0;
1509 }
1510 
1511 
hostapd_fils_discovery_cap(struct hostapd_data * hapd)1512 static u16 hostapd_fils_discovery_cap(struct hostapd_data *hapd)
1513 {
1514 	u16 cap_info, phy_index;
1515 	u8 chwidth = FD_CAP_BSS_CHWIDTH_20, he_mcs_nss_size = 4;
1516 	struct hostapd_hw_modes *mode = hapd->iface->current_mode;
1517 
1518 	cap_info = FD_CAP_ESS;
1519 	if (hapd->conf->wpa)
1520 		cap_info |= FD_CAP_PRIVACY;
1521 
1522 	if (is_6ghz_op_class(hapd->iconf->op_class)) {
1523 		switch (hapd->iconf->op_class) {
1524 		case 137:
1525 			chwidth = FD_CAP_BSS_CHWIDTH_320;
1526 			break;
1527 		case 135:
1528 			he_mcs_nss_size += 4;
1529 			/* fallthrough */
1530 		case 134:
1531 			he_mcs_nss_size += 4;
1532 			chwidth = FD_CAP_BSS_CHWIDTH_160_80_80;
1533 			break;
1534 		case 133:
1535 			chwidth = FD_CAP_BSS_CHWIDTH_80;
1536 			break;
1537 		case 132:
1538 			chwidth = FD_CAP_BSS_CHWIDTH_40;
1539 			break;
1540 		}
1541 	} else {
1542 		switch (hostapd_get_oper_chwidth(hapd->iconf)) {
1543 		case CONF_OPER_CHWIDTH_80P80MHZ:
1544 			he_mcs_nss_size += 4;
1545 			/* fallthrough */
1546 		case CONF_OPER_CHWIDTH_160MHZ:
1547 			he_mcs_nss_size += 4;
1548 			chwidth = FD_CAP_BSS_CHWIDTH_160_80_80;
1549 			break;
1550 		case CONF_OPER_CHWIDTH_80MHZ:
1551 			chwidth = FD_CAP_BSS_CHWIDTH_80;
1552 			break;
1553 		case CONF_OPER_CHWIDTH_USE_HT:
1554 			if (hapd->iconf->secondary_channel)
1555 				chwidth = FD_CAP_BSS_CHWIDTH_40;
1556 			else
1557 				chwidth = FD_CAP_BSS_CHWIDTH_20;
1558 			break;
1559 		default:
1560 			break;
1561 		}
1562 	}
1563 
1564 	phy_index = hostapd_gen_fils_discovery_phy_index(hapd);
1565 	cap_info |= phy_index << FD_CAP_PHY_INDEX_SHIFT;
1566 	cap_info |= chwidth << FD_CAP_BSS_CHWIDTH_SHIFT;
1567 	cap_info |= hostapd_gen_fils_discovery_nss(mode, phy_index,
1568 						   he_mcs_nss_size);
1569 	return cap_info;
1570 }
1571 
1572 
hostapd_gen_fils_discovery(struct hostapd_data * hapd,size_t * len)1573 static u8 * hostapd_gen_fils_discovery(struct hostapd_data *hapd, size_t *len)
1574 {
1575 	struct ieee80211_mgmt *head;
1576 	const u8 *mobility_domain;
1577 	u8 *pos, *length_pos, buf[200];
1578 	u16 ctl = 0;
1579 	u8 fd_rsn_info[5];
1580 	size_t total_len, buf_len;
1581 
1582 	total_len = 24 + 2 + 12;
1583 
1584 	/* FILS Discovery Frame Control */
1585 	ctl = (sizeof(hapd->conf->ssid.short_ssid) - 1) |
1586 		FD_FRAME_CTL_SHORT_SSID_PRESENT |
1587 		FD_FRAME_CTL_LENGTH_PRESENT |
1588 		FD_FRAME_CTL_CAP_PRESENT;
1589 	total_len += 4 + 1 + 2;
1590 
1591 	/* Check for optional subfields and calculate length */
1592 	if (wpa_auth_write_fd_rsn_info(hapd->wpa_auth, fd_rsn_info)) {
1593 		ctl |= FD_FRAME_CTL_RSN_INFO_PRESENT;
1594 		total_len += sizeof(fd_rsn_info);
1595 	}
1596 
1597 	mobility_domain = hostapd_wpa_ie(hapd, WLAN_EID_MOBILITY_DOMAIN);
1598 	if (mobility_domain) {
1599 		ctl |= FD_FRAME_CTL_MD_PRESENT;
1600 		total_len += 3;
1601 	}
1602 
1603 	total_len += hostapd_eid_rnr_len(hapd, WLAN_FC_STYPE_ACTION);
1604 
1605 	pos = hostapd_eid_fils_indic(hapd, buf, 0);
1606 	buf_len = pos - buf;
1607 	total_len += buf_len;
1608 
1609 #ifdef CONFIG_IEEE80211AX
1610 	/* Transmit Power Envelope element(s) */
1611 	if (is_6ghz_op_class(hapd->iconf->op_class)) {
1612 		total_len += 4;
1613 		if (hapd->iconf->he_6ghz_reg_pwr_type == HE_6GHZ_INDOOR_AP)
1614 			total_len += 4;
1615 	}
1616 #endif /* CONFIG_IEEE80211AX */
1617 
1618 	head = os_zalloc(total_len);
1619 	if (!head)
1620 		return NULL;
1621 
1622 	head->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
1623 					   WLAN_FC_STYPE_ACTION);
1624 	os_memset(head->da, 0xff, ETH_ALEN);
1625 	os_memcpy(head->sa, hapd->own_addr, ETH_ALEN);
1626 	os_memcpy(head->bssid, hapd->own_addr, ETH_ALEN);
1627 
1628 	head->u.action.category = WLAN_ACTION_PUBLIC;
1629 	head->u.action.u.public_action.action = WLAN_PA_FILS_DISCOVERY;
1630 
1631 	pos = &head->u.action.u.public_action.variable[0];
1632 
1633 	/* FILS Discovery Information field */
1634 
1635 	/* FILS Discovery Frame Control */
1636 	WPA_PUT_LE16(pos, ctl);
1637 	pos += 2;
1638 
1639 	/* Hardware or low-level driver will fill in the Timestamp value */
1640 	pos += 8;
1641 
1642 	/* Beacon Interval */
1643 	WPA_PUT_LE16(pos, hapd->iconf->beacon_int);
1644 	pos += 2;
1645 
1646 	/* Short SSID */
1647 	WPA_PUT_LE32(pos, hapd->conf->ssid.short_ssid);
1648 	pos += sizeof(hapd->conf->ssid.short_ssid);
1649 
1650 	/* Store position of FILS discovery information element Length field */
1651 	length_pos = pos++;
1652 
1653 	/* FD Capability */
1654 	WPA_PUT_LE16(pos, hostapd_fils_discovery_cap(hapd));
1655 	pos += 2;
1656 
1657 	/* Operating Class - not present */
1658 
1659 	/* Primary Channel - not present */
1660 
1661 	/* AP Configuration Sequence Number - not present */
1662 
1663 	/* Access Network Options - not present */
1664 
1665 	/* FD RSN Information */
1666 	if (ctl & FD_FRAME_CTL_RSN_INFO_PRESENT) {
1667 		os_memcpy(pos, fd_rsn_info, sizeof(fd_rsn_info));
1668 		pos += sizeof(fd_rsn_info);
1669 	}
1670 
1671 	/* Channel Center Frequency Segment 1 - not present */
1672 
1673 	/* Mobility Domain */
1674 	if (ctl & FD_FRAME_CTL_MD_PRESENT) {
1675 		os_memcpy(pos, &mobility_domain[2], 3);
1676 		pos += 3;
1677 	}
1678 
1679 	/* Fill in the Length field value */
1680 	*length_pos = pos - (length_pos + 1);
1681 
1682 	pos = hostapd_eid_rnr(hapd, pos, WLAN_FC_STYPE_ACTION);
1683 
1684 	/* FILS Indication element */
1685 	if (buf_len) {
1686 		os_memcpy(pos, buf, buf_len);
1687 		pos += buf_len;
1688 	}
1689 
1690 	if (is_6ghz_op_class(hapd->iconf->op_class))
1691 		pos = hostapd_eid_txpower_envelope(hapd, pos);
1692 
1693 	*len = pos - (u8 *) head;
1694 	wpa_hexdump(MSG_DEBUG, "FILS Discovery frame template",
1695 		    head, pos - (u8 *) head);
1696 	return (u8 *) head;
1697 }
1698 
1699 
1700 /* Configure FILS Discovery frame transmission parameters */
hostapd_fils_discovery(struct hostapd_data * hapd,struct wpa_driver_ap_params * params)1701 static u8 * hostapd_fils_discovery(struct hostapd_data *hapd,
1702 				   struct wpa_driver_ap_params *params)
1703 {
1704 	params->fd_max_int = hapd->conf->fils_discovery_max_int;
1705 	if (is_6ghz_op_class(hapd->iconf->op_class) &&
1706 	    params->fd_max_int > FD_MAX_INTERVAL_6GHZ)
1707 		params->fd_max_int = FD_MAX_INTERVAL_6GHZ;
1708 
1709 	params->fd_min_int = hapd->conf->fils_discovery_min_int;
1710 	if (params->fd_min_int > params->fd_max_int)
1711 		params->fd_min_int = params->fd_max_int;
1712 
1713 	if (params->fd_max_int)
1714 		return hostapd_gen_fils_discovery(hapd,
1715 						  &params->fd_frame_tmpl_len);
1716 
1717 	return NULL;
1718 }
1719 
1720 #endif /* CONFIG_FILS */
1721 
1722 
ieee802_11_build_ap_params(struct hostapd_data * hapd,struct wpa_driver_ap_params * params)1723 int ieee802_11_build_ap_params(struct hostapd_data *hapd,
1724 			       struct wpa_driver_ap_params *params)
1725 {
1726 	struct ieee80211_mgmt *head = NULL;
1727 	u8 *tail = NULL;
1728 	size_t head_len = 0, tail_len = 0;
1729 	u8 *resp = NULL;
1730 	size_t resp_len = 0;
1731 #ifdef NEED_AP_MLME
1732 	u16 capab_info;
1733 	u8 *pos, *tailpos, *tailend, *csa_pos;
1734 	bool complete = false;
1735 #endif /* NEED_AP_MLME */
1736 
1737 	os_memset(params, 0, sizeof(*params));
1738 
1739 #ifdef NEED_AP_MLME
1740 #define BEACON_HEAD_BUF_SIZE 256
1741 #define BEACON_TAIL_BUF_SIZE 512
1742 	head = os_zalloc(BEACON_HEAD_BUF_SIZE);
1743 	tail_len = BEACON_TAIL_BUF_SIZE;
1744 #ifdef CONFIG_WPS
1745 	if (hapd->conf->wps_state && hapd->wps_beacon_ie)
1746 		tail_len += wpabuf_len(hapd->wps_beacon_ie);
1747 #endif /* CONFIG_WPS */
1748 #ifdef CONFIG_P2P
1749 	if (hapd->p2p_beacon_ie)
1750 		tail_len += wpabuf_len(hapd->p2p_beacon_ie);
1751 #endif /* CONFIG_P2P */
1752 #ifdef CONFIG_FST
1753 	if (hapd->iface->fst_ies)
1754 		tail_len += wpabuf_len(hapd->iface->fst_ies);
1755 #endif /* CONFIG_FST */
1756 	if (hapd->conf->vendor_elements)
1757 		tail_len += wpabuf_len(hapd->conf->vendor_elements);
1758 
1759 #ifdef CONFIG_IEEE80211AC
1760 	if (hapd->conf->vendor_vht) {
1761 		tail_len += 5 + 2 + sizeof(struct ieee80211_vht_capabilities) +
1762 			2 + sizeof(struct ieee80211_vht_operation);
1763 	}
1764 #endif /* CONFIG_IEEE80211AC */
1765 
1766 #ifdef CONFIG_IEEE80211AX
1767 	if (hapd->iconf->ieee80211ax && !hapd->conf->disable_11ax) {
1768 		tail_len += 3 + sizeof(struct ieee80211_he_capabilities) +
1769 			3 + sizeof(struct ieee80211_he_operation) +
1770 			3 + sizeof(struct ieee80211_he_mu_edca_parameter_set) +
1771 			3 + sizeof(struct ieee80211_spatial_reuse);
1772 		if (is_6ghz_op_class(hapd->iconf->op_class)) {
1773 			tail_len += sizeof(struct ieee80211_he_6ghz_oper_info) +
1774 				3 + sizeof(struct ieee80211_he_6ghz_band_cap);
1775 			 /* An additional Transmit Power Envelope element for
1776 			  * subordinate client */
1777 			if (hapd->iconf->he_6ghz_reg_pwr_type ==
1778 			    HE_6GHZ_INDOOR_AP)
1779 				tail_len += 4;
1780 		}
1781 	}
1782 #endif /* CONFIG_IEEE80211AX */
1783 
1784 #ifdef CONFIG_IEEE80211BE
1785 	if (hapd->iconf->ieee80211be && !hapd->conf->disable_11be) {
1786 		tail_len += hostapd_eid_eht_capab_len(hapd, IEEE80211_MODE_AP);
1787 		tail_len += 3 + sizeof(struct ieee80211_eht_operation);
1788 		if (hapd->iconf->punct_bitmap)
1789 			tail_len += EHT_OPER_DISABLED_SUBCHAN_BITMAP_SIZE;
1790 
1791 		/*
1792 		 * TODO: Multi-Link element has variable length and can be
1793 		 * long based on the common info and number of per
1794 		 * station profiles. For now use 256.
1795 		 */
1796 		if (hapd->conf->mld_ap)
1797 			tail_len += 256;
1798 	}
1799 #endif /* CONFIG_IEEE80211BE */
1800 
1801 	if (hapd->iconf->mbssid == ENHANCED_MBSSID_ENABLED &&
1802 	    hapd == hostapd_mbssid_get_tx_bss(hapd))
1803 		tail_len += 5; /* Multiple BSSID Configuration element */
1804 	tail_len += hostapd_eid_rnr_len(hapd, WLAN_FC_STYPE_BEACON);
1805 	tail_len += hostapd_mbo_ie_len(hapd);
1806 	tail_len += hostapd_eid_owe_trans_len(hapd);
1807 	tail_len += hostapd_eid_dpp_cc_len(hapd);
1808 
1809 	tailpos = tail = os_malloc(tail_len);
1810 	if (head == NULL || tail == NULL) {
1811 		wpa_printf(MSG_ERROR, "Failed to set beacon data");
1812 		os_free(head);
1813 		os_free(tail);
1814 		return -1;
1815 	}
1816 	tailend = tail + tail_len;
1817 
1818 	head->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
1819 					   WLAN_FC_STYPE_BEACON);
1820 	head->duration = host_to_le16(0);
1821 	os_memset(head->da, 0xff, ETH_ALEN);
1822 
1823 	os_memcpy(head->sa, hapd->own_addr, ETH_ALEN);
1824 	os_memcpy(head->bssid, hapd->own_addr, ETH_ALEN);
1825 	head->u.beacon.beacon_int =
1826 		host_to_le16(hapd->iconf->beacon_int);
1827 
1828 	/* hardware or low-level driver will setup seq_ctrl and timestamp */
1829 	capab_info = hostapd_own_capab_info(hapd);
1830 	head->u.beacon.capab_info = host_to_le16(capab_info);
1831 	pos = &head->u.beacon.variable[0];
1832 
1833 	/* SSID */
1834 	*pos++ = WLAN_EID_SSID;
1835 	if (hapd->conf->ignore_broadcast_ssid == 2) {
1836 		/* clear the data, but keep the correct length of the SSID */
1837 		*pos++ = hapd->conf->ssid.ssid_len;
1838 		os_memset(pos, 0, hapd->conf->ssid.ssid_len);
1839 		pos += hapd->conf->ssid.ssid_len;
1840 	} else if (hapd->conf->ignore_broadcast_ssid) {
1841 		*pos++ = 0; /* empty SSID */
1842 	} else {
1843 		*pos++ = hapd->conf->ssid.ssid_len;
1844 		os_memcpy(pos, hapd->conf->ssid.ssid,
1845 			  hapd->conf->ssid.ssid_len);
1846 		pos += hapd->conf->ssid.ssid_len;
1847 	}
1848 
1849 	/* Supported rates */
1850 	pos = hostapd_eid_supp_rates(hapd, pos);
1851 
1852 	/* DS Params */
1853 	pos = hostapd_eid_ds_params(hapd, pos);
1854 
1855 	head_len = pos - (u8 *) head;
1856 
1857 	tailpos = hostapd_eid_country(hapd, tailpos, tailend - tailpos);
1858 
1859 	/* Power Constraint element */
1860 	tailpos = hostapd_eid_pwr_constraint(hapd, tailpos);
1861 
1862 	/* CSA IE */
1863 	csa_pos = hostapd_eid_csa(hapd, tailpos);
1864 	if (csa_pos != tailpos)
1865 		hapd->cs_c_off_beacon = csa_pos - tail - 1;
1866 	tailpos = csa_pos;
1867 
1868 	/* ERP Information element */
1869 	tailpos = hostapd_eid_erp_info(hapd, tailpos);
1870 
1871 	/* Extended supported rates */
1872 	tailpos = hostapd_eid_ext_supp_rates(hapd, tailpos);
1873 
1874 	tailpos = hostapd_get_rsne(hapd, tailpos, tailend - tailpos);
1875 	tailpos = hostapd_eid_bss_load(hapd, tailpos, tailend - tailpos);
1876 	tailpos = hostapd_eid_rm_enabled_capab(hapd, tailpos,
1877 					       tailend - tailpos);
1878 	tailpos = hostapd_get_mde(hapd, tailpos, tailend - tailpos);
1879 
1880 	/* eCSA IE */
1881 	csa_pos = hostapd_eid_ecsa(hapd, tailpos);
1882 	if (csa_pos != tailpos)
1883 		hapd->cs_c_off_ecsa_beacon = csa_pos - tail - 1;
1884 	tailpos = csa_pos;
1885 
1886 	tailpos = hostapd_eid_supported_op_classes(hapd, tailpos);
1887 	tailpos = hostapd_eid_ht_capabilities(hapd, tailpos);
1888 	tailpos = hostapd_eid_ht_operation(hapd, tailpos);
1889 
1890 	if (hapd->iconf->mbssid && hapd->iconf->num_bss > 1) {
1891 		if (ieee802_11_build_ap_params_mbssid(hapd, params)) {
1892 			os_free(head);
1893 			os_free(tail);
1894 			wpa_printf(MSG_ERROR,
1895 				   "MBSSID: Failed to set beacon data");
1896 			return -1;
1897 		}
1898 		complete = hapd->iconf->mbssid == MBSSID_ENABLED ||
1899 			(hapd->iconf->mbssid == ENHANCED_MBSSID_ENABLED &&
1900 			 params->mbssid_elem_count == 1);
1901 	}
1902 
1903 	tailpos = hostapd_eid_ext_capab(hapd, tailpos, complete);
1904 
1905 	/*
1906 	 * TODO: Time Advertisement element should only be included in some
1907 	 * DTIM Beacon frames.
1908 	 */
1909 	tailpos = hostapd_eid_time_adv(hapd, tailpos);
1910 
1911 	tailpos = hostapd_eid_interworking(hapd, tailpos);
1912 	tailpos = hostapd_eid_adv_proto(hapd, tailpos);
1913 	tailpos = hostapd_eid_roaming_consortium(hapd, tailpos);
1914 
1915 #ifdef CONFIG_FST
1916 	if (hapd->iface->fst_ies) {
1917 		os_memcpy(tailpos, wpabuf_head(hapd->iface->fst_ies),
1918 			  wpabuf_len(hapd->iface->fst_ies));
1919 		tailpos += wpabuf_len(hapd->iface->fst_ies);
1920 	}
1921 #endif /* CONFIG_FST */
1922 
1923 #ifdef CONFIG_IEEE80211AC
1924 	if (hapd->iconf->ieee80211ac && !hapd->conf->disable_11ac &&
1925 	    !is_6ghz_op_class(hapd->iconf->op_class)) {
1926 		tailpos = hostapd_eid_vht_capabilities(hapd, tailpos, 0);
1927 		tailpos = hostapd_eid_vht_operation(hapd, tailpos);
1928 		tailpos = hostapd_eid_txpower_envelope(hapd, tailpos);
1929 	}
1930 #endif /* CONFIG_IEEE80211AC */
1931 
1932 #ifdef CONFIG_IEEE80211AX
1933 	if (hapd->iconf->ieee80211ax && !hapd->conf->disable_11ax &&
1934 	    is_6ghz_op_class(hapd->iconf->op_class))
1935 		tailpos = hostapd_eid_txpower_envelope(hapd, tailpos);
1936 #endif /* CONFIG_IEEE80211AX */
1937 
1938 	tailpos = hostapd_eid_wb_chsw_wrapper(hapd, tailpos);
1939 
1940 	tailpos = hostapd_eid_rnr(hapd, tailpos, WLAN_FC_STYPE_BEACON);
1941 	tailpos = hostapd_eid_fils_indic(hapd, tailpos, 0);
1942 	tailpos = hostapd_get_rsnxe(hapd, tailpos, tailend - tailpos);
1943 	tailpos = hostapd_eid_mbssid_config(hapd, tailpos,
1944 					    params->mbssid_elem_count);
1945 
1946 #ifdef CONFIG_IEEE80211AX
1947 	if (hapd->iconf->ieee80211ax && !hapd->conf->disable_11ax) {
1948 		u8 *cca_pos;
1949 
1950 		tailpos = hostapd_eid_he_capab(hapd, tailpos,
1951 					       IEEE80211_MODE_AP);
1952 		tailpos = hostapd_eid_he_operation(hapd, tailpos);
1953 
1954 		/* BSS Color Change Announcement element */
1955 		cca_pos = hostapd_eid_cca(hapd, tailpos);
1956 		if (cca_pos != tailpos)
1957 			hapd->cca_c_off_beacon = cca_pos - tail - 2;
1958 		tailpos = cca_pos;
1959 
1960 		tailpos = hostapd_eid_spatial_reuse(hapd, tailpos);
1961 		tailpos = hostapd_eid_he_mu_edca_parameter_set(hapd, tailpos);
1962 		tailpos = hostapd_eid_he_6ghz_band_cap(hapd, tailpos);
1963 	}
1964 #endif /* CONFIG_IEEE80211AX */
1965 
1966 #ifdef CONFIG_IEEE80211BE
1967 	if (hapd->iconf->ieee80211be && !hapd->conf->disable_11be) {
1968 		if (hapd->conf->mld_ap)
1969 			tailpos = hostapd_eid_eht_basic_ml(hapd, tailpos, NULL,
1970 							   true);
1971 		tailpos = hostapd_eid_eht_capab(hapd, tailpos,
1972 						IEEE80211_MODE_AP);
1973 		tailpos = hostapd_eid_eht_operation(hapd, tailpos);
1974 	}
1975 #endif /* CONFIG_IEEE80211BE */
1976 
1977 #ifdef CONFIG_IEEE80211AC
1978 	if (hapd->conf->vendor_vht)
1979 		tailpos = hostapd_eid_vendor_vht(hapd, tailpos);
1980 #endif /* CONFIG_IEEE80211AC */
1981 
1982 	/* WPA / OSEN */
1983 	tailpos = hostapd_get_wpa_ie(hapd, tailpos, tailend - tailpos);
1984 	tailpos = hostapd_get_osen_ie(hapd, tailpos, tailend - tailpos);
1985 
1986 	/* Wi-Fi Alliance WMM */
1987 	tailpos = hostapd_eid_wmm(hapd, tailpos);
1988 
1989 #ifdef CONFIG_WPS
1990 	if (hapd->conf->wps_state && hapd->wps_beacon_ie) {
1991 		os_memcpy(tailpos, wpabuf_head(hapd->wps_beacon_ie),
1992 			  wpabuf_len(hapd->wps_beacon_ie));
1993 		tailpos += wpabuf_len(hapd->wps_beacon_ie);
1994 	}
1995 #endif /* CONFIG_WPS */
1996 
1997 #ifdef CONFIG_P2P
1998 	if ((hapd->conf->p2p & P2P_ENABLED) && hapd->p2p_beacon_ie) {
1999 		os_memcpy(tailpos, wpabuf_head(hapd->p2p_beacon_ie),
2000 			  wpabuf_len(hapd->p2p_beacon_ie));
2001 		tailpos += wpabuf_len(hapd->p2p_beacon_ie);
2002 	}
2003 #endif /* CONFIG_P2P */
2004 #ifdef CONFIG_P2P_MANAGER
2005 	if ((hapd->conf->p2p & (P2P_MANAGE | P2P_ENABLED | P2P_GROUP_OWNER)) ==
2006 	    P2P_MANAGE)
2007 		tailpos = hostapd_eid_p2p_manage(hapd, tailpos);
2008 #endif /* CONFIG_P2P_MANAGER */
2009 
2010 #ifdef CONFIG_HS20
2011 	tailpos = hostapd_eid_hs20_indication(hapd, tailpos);
2012 #endif /* CONFIG_HS20 */
2013 
2014 	tailpos = hostapd_eid_mbo(hapd, tailpos, tail + tail_len - tailpos);
2015 	tailpos = hostapd_eid_owe_trans(hapd, tailpos,
2016 					tail + tail_len - tailpos);
2017 	tailpos = hostapd_eid_dpp_cc(hapd, tailpos, tail + tail_len - tailpos);
2018 
2019 	if (hapd->conf->vendor_elements) {
2020 		os_memcpy(tailpos, wpabuf_head(hapd->conf->vendor_elements),
2021 			  wpabuf_len(hapd->conf->vendor_elements));
2022 		tailpos += wpabuf_len(hapd->conf->vendor_elements);
2023 	}
2024 
2025 	tail_len = tailpos > tail ? tailpos - tail : 0;
2026 
2027 	resp = hostapd_probe_resp_offloads(hapd, &resp_len);
2028 #endif /* NEED_AP_MLME */
2029 
2030 	/* If key management offload is enabled, configure PSK to the driver. */
2031 	if (wpa_key_mgmt_wpa_psk_no_sae(hapd->conf->wpa_key_mgmt) &&
2032 	    (hapd->iface->drv_flags2 &
2033 	     WPA_DRIVER_FLAGS2_4WAY_HANDSHAKE_AP_PSK)) {
2034 		if (hapd->conf->ssid.wpa_psk && hapd->conf->ssid.wpa_psk_set) {
2035 			os_memcpy(params->psk, hapd->conf->ssid.wpa_psk->psk,
2036 				  PMK_LEN);
2037 			params->psk_len = PMK_LEN;
2038 		} else if (hapd->conf->ssid.wpa_passphrase &&
2039 			   pbkdf2_sha1(hapd->conf->ssid.wpa_passphrase,
2040 				       hapd->conf->ssid.ssid,
2041 				       hapd->conf->ssid.ssid_len, 4096,
2042 				       params->psk, PMK_LEN) == 0) {
2043 			params->psk_len = PMK_LEN;
2044 		}
2045 	}
2046 
2047 #ifdef CONFIG_SAE
2048 	/* If SAE offload is enabled, provide password to lower layer for
2049 	 * SAE authentication and PMK generation.
2050 	 */
2051 	if (wpa_key_mgmt_sae(hapd->conf->wpa_key_mgmt) &&
2052 	    (hapd->iface->drv_flags2 & WPA_DRIVER_FLAGS2_SAE_OFFLOAD_AP)) {
2053 		if (hostapd_sae_pk_in_use(hapd->conf)) {
2054 			wpa_printf(MSG_ERROR,
2055 				   "SAE PK not supported with SAE offload");
2056 			return -1;
2057 		}
2058 
2059 		if (hostapd_sae_pw_id_in_use(hapd->conf)) {
2060 			wpa_printf(MSG_ERROR,
2061 				   "SAE Password Identifiers not supported with SAE offload");
2062 			return -1;
2063 		}
2064 
2065 		params->sae_password = sae_get_password(hapd, NULL, NULL, NULL,
2066 							NULL, NULL);
2067 		if (!params->sae_password) {
2068 			wpa_printf(MSG_ERROR, "SAE password not configured for offload");
2069 			return -1;
2070 		}
2071 	}
2072 #endif /* CONFIG_SAE */
2073 
2074 	params->head = (u8 *) head;
2075 	params->head_len = head_len;
2076 	params->tail = tail;
2077 	params->tail_len = tail_len;
2078 	params->proberesp = resp;
2079 	params->proberesp_len = resp_len;
2080 	params->dtim_period = hapd->conf->dtim_period;
2081 	params->beacon_int = hapd->iconf->beacon_int;
2082 	params->basic_rates = hapd->iface->basic_rates;
2083 	params->beacon_rate = hapd->iconf->beacon_rate;
2084 	params->rate_type = hapd->iconf->rate_type;
2085 	params->ssid = hapd->conf->ssid.ssid;
2086 	params->ssid_len = hapd->conf->ssid.ssid_len;
2087 	if ((hapd->conf->wpa & (WPA_PROTO_WPA | WPA_PROTO_RSN)) ==
2088 	    (WPA_PROTO_WPA | WPA_PROTO_RSN))
2089 		params->pairwise_ciphers = hapd->conf->wpa_pairwise |
2090 			hapd->conf->rsn_pairwise;
2091 	else if (hapd->conf->wpa & WPA_PROTO_RSN)
2092 		params->pairwise_ciphers = hapd->conf->rsn_pairwise;
2093 	else if (hapd->conf->wpa & WPA_PROTO_WPA)
2094 		params->pairwise_ciphers = hapd->conf->wpa_pairwise;
2095 	params->group_cipher = hapd->conf->wpa_group;
2096 	params->key_mgmt_suites = hapd->conf->wpa_key_mgmt;
2097 	params->auth_algs = hapd->conf->auth_algs;
2098 	params->wpa_version = hapd->conf->wpa;
2099 	params->privacy = hapd->conf->wpa;
2100 #ifdef CONFIG_WEP
2101 	params->privacy |= hapd->conf->ssid.wep.keys_set ||
2102 		(hapd->conf->ieee802_1x &&
2103 		 (hapd->conf->default_wep_key_len ||
2104 		  hapd->conf->individual_wep_key_len));
2105 #endif /* CONFIG_WEP */
2106 	switch (hapd->conf->ignore_broadcast_ssid) {
2107 	case 0:
2108 		params->hide_ssid = NO_SSID_HIDING;
2109 		break;
2110 	case 1:
2111 		params->hide_ssid = HIDDEN_SSID_ZERO_LEN;
2112 		break;
2113 	case 2:
2114 		params->hide_ssid = HIDDEN_SSID_ZERO_CONTENTS;
2115 		break;
2116 	}
2117 	params->isolate = hapd->conf->isolate;
2118 #ifdef NEED_AP_MLME
2119 	params->cts_protect = !!(ieee802_11_erp_info(hapd) &
2120 				ERP_INFO_USE_PROTECTION);
2121 	params->preamble = hapd->iface->num_sta_no_short_preamble == 0 &&
2122 		hapd->iconf->preamble == SHORT_PREAMBLE;
2123 	if (hapd->iface->current_mode &&
2124 	    hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G)
2125 		params->short_slot_time =
2126 			hapd->iface->num_sta_no_short_slot_time > 0 ? 0 : 1;
2127 	else
2128 		params->short_slot_time = -1;
2129 	if (!hapd->iconf->ieee80211n || hapd->conf->disable_11n)
2130 		params->ht_opmode = -1;
2131 	else
2132 		params->ht_opmode = hapd->iface->ht_op_mode;
2133 #endif /* NEED_AP_MLME */
2134 	params->interworking = hapd->conf->interworking;
2135 	if (hapd->conf->interworking &&
2136 	    !is_zero_ether_addr(hapd->conf->hessid))
2137 		params->hessid = hapd->conf->hessid;
2138 	params->access_network_type = hapd->conf->access_network_type;
2139 	params->ap_max_inactivity = hapd->conf->ap_max_inactivity;
2140 #ifdef CONFIG_P2P
2141 	params->p2p_go_ctwindow = hapd->iconf->p2p_go_ctwindow;
2142 #endif /* CONFIG_P2P */
2143 #ifdef CONFIG_HS20
2144 	params->disable_dgaf = hapd->conf->disable_dgaf;
2145 	if (hapd->conf->osen) {
2146 		params->privacy = 1;
2147 		params->osen = 1;
2148 	}
2149 #endif /* CONFIG_HS20 */
2150 	params->multicast_to_unicast = hapd->conf->multicast_to_unicast;
2151 	params->pbss = hapd->conf->pbss;
2152 
2153 	if (hapd->conf->ftm_responder) {
2154 		if (hapd->iface->drv_flags & WPA_DRIVER_FLAGS_FTM_RESPONDER) {
2155 			params->ftm_responder = 1;
2156 			params->lci = hapd->iface->conf->lci;
2157 			params->civic = hapd->iface->conf->civic;
2158 		} else {
2159 			wpa_printf(MSG_WARNING,
2160 				   "Not configuring FTM responder as the driver doesn't advertise support for it");
2161 		}
2162 	}
2163 
2164 #ifdef CONFIG_IEEE80211BE
2165 	if (hapd->conf->mld_ap && hapd->iconf->ieee80211be &&
2166 	    !hapd->conf->disable_11be) {
2167 		params->mld_ap = true;
2168 		params->mld_link_id = hapd->mld_link_id;
2169 	}
2170 #endif /* CONFIG_IEEE80211BE */
2171 
2172 	return 0;
2173 }
2174 
2175 
ieee802_11_free_ap_params(struct wpa_driver_ap_params * params)2176 void ieee802_11_free_ap_params(struct wpa_driver_ap_params *params)
2177 {
2178 	os_free(params->tail);
2179 	params->tail = NULL;
2180 	os_free(params->head);
2181 	params->head = NULL;
2182 	os_free(params->proberesp);
2183 	params->proberesp = NULL;
2184 	os_free(params->mbssid_elem);
2185 	params->mbssid_elem = NULL;
2186 	os_free(params->mbssid_elem_offset);
2187 	params->mbssid_elem_offset = NULL;
2188 	os_free(params->rnr_elem);
2189 	params->rnr_elem = NULL;
2190 	os_free(params->rnr_elem_offset);
2191 	params->rnr_elem_offset = NULL;
2192 #ifdef CONFIG_FILS
2193 	os_free(params->fd_frame_tmpl);
2194 	params->fd_frame_tmpl = NULL;
2195 #endif /* CONFIG_FILS */
2196 #ifdef CONFIG_IEEE80211AX
2197 	os_free(params->unsol_bcast_probe_resp_tmpl);
2198 	params->unsol_bcast_probe_resp_tmpl = NULL;
2199 #endif /* CONFIG_IEEE80211AX */
2200 	os_free(params->allowed_freqs);
2201 	params->allowed_freqs = NULL;
2202 }
2203 
2204 
__ieee802_11_set_beacon(struct hostapd_data * hapd)2205 static int __ieee802_11_set_beacon(struct hostapd_data *hapd)
2206 {
2207 	struct wpa_driver_ap_params params;
2208 	struct hostapd_freq_params freq;
2209 	struct hostapd_iface *iface = hapd->iface;
2210 	struct hostapd_config *iconf = iface->conf;
2211 	struct hostapd_hw_modes *cmode = iface->current_mode;
2212 	struct wpabuf *beacon, *proberesp, *assocresp;
2213 	int res, ret = -1, i;
2214 	struct hostapd_hw_modes *mode;
2215 
2216 	if (!hapd->drv_priv) {
2217 		wpa_printf(MSG_ERROR, "Interface is disabled");
2218 		return -1;
2219 	}
2220 
2221 	if (hapd->csa_in_progress) {
2222 		wpa_printf(MSG_ERROR, "Cannot set beacons during CSA period");
2223 		return -1;
2224 	}
2225 
2226 	hapd->beacon_set_done = 1;
2227 
2228 	if (ieee802_11_build_ap_params(hapd, &params) < 0)
2229 		return -1;
2230 
2231 	if (hostapd_build_ap_extra_ies(hapd, &beacon, &proberesp, &assocresp) <
2232 	    0)
2233 		goto fail;
2234 
2235 	params.beacon_ies = beacon;
2236 	params.proberesp_ies = proberesp;
2237 	params.assocresp_ies = assocresp;
2238 	params.reenable = hapd->reenable_beacon;
2239 #ifdef CONFIG_IEEE80211AX
2240 	params.he_spr_ctrl = hapd->iface->conf->spr.sr_control;
2241 	params.he_spr_non_srg_obss_pd_max_offset =
2242 		hapd->iface->conf->spr.non_srg_obss_pd_max_offset;
2243 	params.he_spr_srg_obss_pd_min_offset =
2244 		hapd->iface->conf->spr.srg_obss_pd_min_offset;
2245 	params.he_spr_srg_obss_pd_max_offset =
2246 		hapd->iface->conf->spr.srg_obss_pd_max_offset;
2247 	os_memcpy(params.he_spr_bss_color_bitmap,
2248 		  hapd->iface->conf->spr.srg_bss_color_bitmap, 8);
2249 	os_memcpy(params.he_spr_partial_bssid_bitmap,
2250 		  hapd->iface->conf->spr.srg_partial_bssid_bitmap, 8);
2251 	params.he_bss_color_disabled =
2252 		hapd->iface->conf->he_op.he_bss_color_disabled;
2253 	params.he_bss_color_partial =
2254 		hapd->iface->conf->he_op.he_bss_color_partial;
2255 	params.he_bss_color = hapd->iface->conf->he_op.he_bss_color;
2256 	params.twt_responder = hostapd_get_he_twt_responder(hapd,
2257 							    IEEE80211_MODE_AP);
2258 	params.unsol_bcast_probe_resp_tmpl =
2259 		hostapd_unsol_bcast_probe_resp(hapd, &params);
2260 #endif /* CONFIG_IEEE80211AX */
2261 	hapd->reenable_beacon = 0;
2262 #ifdef CONFIG_SAE
2263 	params.sae_pwe = hapd->conf->sae_pwe;
2264 #endif /* CONFIG_SAE */
2265 
2266 #ifdef CONFIG_FILS
2267 	params.fd_frame_tmpl = hostapd_fils_discovery(hapd, &params);
2268 #endif /* CONFIG_FILS */
2269 
2270 #ifdef CONFIG_IEEE80211BE
2271 	params.punct_bitmap = iconf->punct_bitmap;
2272 #endif /* CONFIG_IEEE80211BE */
2273 
2274 	if (cmode &&
2275 	    hostapd_set_freq_params(&freq, iconf->hw_mode, iface->freq,
2276 				    iconf->channel, iconf->enable_edmg,
2277 				    iconf->edmg_channel, iconf->ieee80211n,
2278 				    iconf->ieee80211ac, iconf->ieee80211ax,
2279 				    iconf->ieee80211be,
2280 				    iconf->secondary_channel,
2281 				    hostapd_get_oper_chwidth(iconf),
2282 				    hostapd_get_oper_centr_freq_seg0_idx(iconf),
2283 				    hostapd_get_oper_centr_freq_seg1_idx(iconf),
2284 				    cmode->vht_capab,
2285 				    &cmode->he_capab[IEEE80211_MODE_AP],
2286 				    &cmode->eht_capab[IEEE80211_MODE_AP]) == 0)
2287 		params.freq = &freq;
2288 
2289 	for (i = 0; i < hapd->iface->num_hw_features; i++) {
2290 		mode = &hapd->iface->hw_features[i];
2291 
2292 		if (iconf->hw_mode != HOSTAPD_MODE_IEEE80211ANY &&
2293 		    iconf->hw_mode != mode->mode)
2294 			continue;
2295 
2296 		hostapd_get_hw_mode_any_channels(hapd, mode,
2297 						 !(iconf->acs_freq_list.num ||
2298 						   iconf->acs_ch_list.num),
2299 						 true, &params.allowed_freqs);
2300 	}
2301 
2302 	res = hostapd_drv_set_ap(hapd, &params);
2303 	hostapd_free_ap_extra_ies(hapd, beacon, proberesp, assocresp);
2304 	if (res)
2305 		wpa_printf(MSG_ERROR, "Failed to set beacon parameters");
2306 	else
2307 		ret = 0;
2308 fail:
2309 	ieee802_11_free_ap_params(&params);
2310 	return ret;
2311 }
2312 
2313 
ieee802_11_set_beacon_per_bss_only(struct hostapd_data * hapd)2314 void ieee802_11_set_beacon_per_bss_only(struct hostapd_data *hapd)
2315 {
2316 	__ieee802_11_set_beacon(hapd);
2317 }
2318 
2319 
ieee802_11_set_beacon(struct hostapd_data * hapd)2320 int ieee802_11_set_beacon(struct hostapd_data *hapd)
2321 {
2322 	struct hostapd_iface *iface = hapd->iface;
2323 	int ret;
2324 	size_t i, j;
2325 	bool is_6g;
2326 
2327 	ret = __ieee802_11_set_beacon(hapd);
2328 	if (ret != 0)
2329 		return ret;
2330 
2331 	if (!iface->interfaces || iface->interfaces->count <= 1)
2332 		return 0;
2333 
2334 	/* Update Beacon frames in case of 6 GHz colocation or AP MLD */
2335 	is_6g = is_6ghz_op_class(iface->conf->op_class);
2336 	for (j = 0; j < iface->interfaces->count; j++) {
2337 		struct hostapd_iface *other;
2338 		bool mld_ap = false;
2339 
2340 		other = iface->interfaces->iface[j];
2341 		if (other == iface || !other || !other->conf)
2342 			continue;
2343 
2344 #ifdef CONFIG_IEEE80211BE
2345 		if (hapd->conf->mld_ap && other->bss[0]->conf->mld_ap &&
2346 		    hapd->conf->mld_id == other->bss[0]->conf->mld_id)
2347 			mld_ap = true;
2348 #endif /* CONFIG_IEEE80211BE */
2349 
2350 		if (is_6g == is_6ghz_op_class(other->conf->op_class) &&
2351 		    !mld_ap)
2352 			continue;
2353 
2354 		for (i = 0; i < other->num_bss; i++) {
2355 			if (other->bss[i] && other->bss[i]->started)
2356 				__ieee802_11_set_beacon(other->bss[i]);
2357 		}
2358 	}
2359 
2360 	return 0;
2361 }
2362 
2363 
ieee802_11_set_beacons(struct hostapd_iface * iface)2364 int ieee802_11_set_beacons(struct hostapd_iface *iface)
2365 {
2366 	size_t i;
2367 	int ret = 0;
2368 
2369 	for (i = 0; i < iface->num_bss; i++) {
2370 		if (iface->bss[i]->started &&
2371 		    ieee802_11_set_beacon(iface->bss[i]) < 0)
2372 			ret = -1;
2373 	}
2374 
2375 	return ret;
2376 }
2377 
2378 
2379 /* only update beacons if started */
ieee802_11_update_beacons(struct hostapd_iface * iface)2380 int ieee802_11_update_beacons(struct hostapd_iface *iface)
2381 {
2382 	size_t i;
2383 	int ret = 0;
2384 
2385 	for (i = 0; i < iface->num_bss; i++) {
2386 		if (iface->bss[i]->beacon_set_done && iface->bss[i]->started &&
2387 		    ieee802_11_set_beacon(iface->bss[i]) < 0)
2388 			ret = -1;
2389 	}
2390 
2391 	return ret;
2392 }
2393 
2394 #endif /* CONFIG_NATIVE_WINDOWS */
2395