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