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