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