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 "wps/wps_defs.h"
     19 #include "p2p/p2p.h"
     20 #include "hostapd.h"
     21 #include "ieee802_11.h"
     22 #include "wpa_auth.h"
     23 #include "wmm.h"
     24 #include "ap_config.h"
     25 #include "sta_info.h"
     26 #include "p2p_hostapd.h"
     27 #include "ap_drv_ops.h"
     28 #include "beacon.h"
     29 #include "hs20.h"
     30 #include "dfs.h"
     31 
     32 
     33 #ifdef NEED_AP_MLME
     34 
     35 static u8 * hostapd_eid_bss_load(struct hostapd_data *hapd, u8 *eid, size_t len)
     36 {
     37 #ifdef CONFIG_TESTING_OPTIONS
     38 	if (hapd->conf->bss_load_test_set) {
     39 		if (2 + 5 > len)
     40 			return eid;
     41 		*eid++ = WLAN_EID_BSS_LOAD;
     42 		*eid++ = 5;
     43 		os_memcpy(eid, hapd->conf->bss_load_test, 5);
     44 		eid += 5;
     45 	}
     46 #endif /* CONFIG_TESTING_OPTIONS */
     47 	return eid;
     48 }
     49 
     50 
     51 static u8 ieee802_11_erp_info(struct hostapd_data *hapd)
     52 {
     53 	u8 erp = 0;
     54 
     55 	if (hapd->iface->current_mode == NULL ||
     56 	    hapd->iface->current_mode->mode != HOSTAPD_MODE_IEEE80211G)
     57 		return 0;
     58 
     59 	if (hapd->iface->olbc)
     60 		erp |= ERP_INFO_USE_PROTECTION;
     61 	if (hapd->iface->num_sta_non_erp > 0) {
     62 		erp |= ERP_INFO_NON_ERP_PRESENT |
     63 			ERP_INFO_USE_PROTECTION;
     64 	}
     65 	if (hapd->iface->num_sta_no_short_preamble > 0 ||
     66 	    hapd->iconf->preamble == LONG_PREAMBLE)
     67 		erp |= ERP_INFO_BARKER_PREAMBLE_MODE;
     68 
     69 	return erp;
     70 }
     71 
     72 
     73 static u8 * hostapd_eid_ds_params(struct hostapd_data *hapd, u8 *eid)
     74 {
     75 	*eid++ = WLAN_EID_DS_PARAMS;
     76 	*eid++ = 1;
     77 	*eid++ = hapd->iconf->channel;
     78 	return eid;
     79 }
     80 
     81 
     82 static u8 * hostapd_eid_erp_info(struct hostapd_data *hapd, u8 *eid)
     83 {
     84 	if (hapd->iface->current_mode == NULL ||
     85 	    hapd->iface->current_mode->mode != HOSTAPD_MODE_IEEE80211G)
     86 		return eid;
     87 
     88 	/* Set NonERP_present and use_protection bits if there
     89 	 * are any associated NonERP stations. */
     90 	/* TODO: use_protection bit can be set to zero even if
     91 	 * there are NonERP stations present. This optimization
     92 	 * might be useful if NonERP stations are "quiet".
     93 	 * See 802.11g/D6 E-1 for recommended practice.
     94 	 * In addition, Non ERP present might be set, if AP detects Non ERP
     95 	 * operation on other APs. */
     96 
     97 	/* Add ERP Information element */
     98 	*eid++ = WLAN_EID_ERP_INFO;
     99 	*eid++ = 1;
    100 	*eid++ = ieee802_11_erp_info(hapd);
    101 
    102 	return eid;
    103 }
    104 
    105 
    106 static u8 * hostapd_eid_pwr_constraint(struct hostapd_data *hapd, u8 *eid)
    107 {
    108 	u8 *pos = eid;
    109 	u8 local_pwr_constraint = 0;
    110 	int dfs;
    111 
    112 	if (hapd->iface->current_mode == NULL ||
    113 	    hapd->iface->current_mode->mode != HOSTAPD_MODE_IEEE80211A)
    114 		return eid;
    115 
    116 	/* Let host drivers add this IE if DFS support is offloaded */
    117 	if (hapd->iface->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD)
    118 		return eid;
    119 
    120 	/*
    121 	 * There is no DFS support and power constraint was not directly
    122 	 * requested by config option.
    123 	 */
    124 	if (!hapd->iconf->ieee80211h &&
    125 	    hapd->iconf->local_pwr_constraint == -1)
    126 		return eid;
    127 
    128 	/* Check if DFS is required by regulatory. */
    129 	dfs = hostapd_is_dfs_required(hapd->iface);
    130 	if (dfs < 0) {
    131 		wpa_printf(MSG_WARNING, "Failed to check if DFS is required; ret=%d",
    132 			   dfs);
    133 		dfs = 0;
    134 	}
    135 
    136 	if (dfs == 0 && hapd->iconf->local_pwr_constraint == -1)
    137 		return eid;
    138 
    139 	/*
    140 	 * ieee80211h (DFS) is enabled so Power Constraint element shall
    141 	 * be added when running on DFS channel whenever local_pwr_constraint
    142 	 * is configured or not. In order to meet regulations when TPC is not
    143 	 * implemented using a transmit power that is below the legal maximum
    144 	 * (including any mitigation factor) should help. In this case,
    145 	 * indicate 3 dB below maximum allowed transmit power.
    146 	 */
    147 	if (hapd->iconf->local_pwr_constraint == -1)
    148 		local_pwr_constraint = 3;
    149 
    150 	/*
    151 	 * A STA that is not an AP shall use a transmit power less than or
    152 	 * equal to the local maximum transmit power level for the channel.
    153 	 * The local maximum transmit power can be calculated from the formula:
    154 	 * local max TX pwr = max TX pwr - local pwr constraint
    155 	 * Where max TX pwr is maximum transmit power level specified for
    156 	 * channel in Country element and local pwr constraint is specified
    157 	 * for channel in this Power Constraint element.
    158 	 */
    159 
    160 	/* Element ID */
    161 	*pos++ = WLAN_EID_PWR_CONSTRAINT;
    162 	/* Length */
    163 	*pos++ = 1;
    164 	/* Local Power Constraint */
    165 	if (local_pwr_constraint)
    166 		*pos++ = local_pwr_constraint;
    167 	else
    168 		*pos++ = hapd->iconf->local_pwr_constraint;
    169 
    170 	return pos;
    171 }
    172 
    173 
    174 static u8 * hostapd_eid_country_add(u8 *pos, u8 *end, int chan_spacing,
    175 				    struct hostapd_channel_data *start,
    176 				    struct hostapd_channel_data *prev)
    177 {
    178 	if (end - pos < 3)
    179 		return pos;
    180 
    181 	/* first channel number */
    182 	*pos++ = start->chan;
    183 	/* number of channels */
    184 	*pos++ = (prev->chan - start->chan) / chan_spacing + 1;
    185 	/* maximum transmit power level */
    186 	*pos++ = start->max_tx_power;
    187 
    188 	return pos;
    189 }
    190 
    191 
    192 static u8 * hostapd_eid_country(struct hostapd_data *hapd, u8 *eid,
    193 				int max_len)
    194 {
    195 	u8 *pos = eid;
    196 	u8 *end = eid + max_len;
    197 	int i;
    198 	struct hostapd_hw_modes *mode;
    199 	struct hostapd_channel_data *start, *prev;
    200 	int chan_spacing = 1;
    201 
    202 	if (!hapd->iconf->ieee80211d || max_len < 6 ||
    203 	    hapd->iface->current_mode == NULL)
    204 		return eid;
    205 
    206 	*pos++ = WLAN_EID_COUNTRY;
    207 	pos++; /* length will be set later */
    208 	os_memcpy(pos, hapd->iconf->country, 3); /* e.g., 'US ' */
    209 	pos += 3;
    210 
    211 	mode = hapd->iface->current_mode;
    212 	if (mode->mode == HOSTAPD_MODE_IEEE80211A)
    213 		chan_spacing = 4;
    214 
    215 	start = prev = NULL;
    216 	for (i = 0; i < mode->num_channels; i++) {
    217 		struct hostapd_channel_data *chan = &mode->channels[i];
    218 		if (chan->flag & HOSTAPD_CHAN_DISABLED)
    219 			continue;
    220 		if (start && prev &&
    221 		    prev->chan + chan_spacing == chan->chan &&
    222 		    start->max_tx_power == chan->max_tx_power) {
    223 			prev = chan;
    224 			continue; /* can use same entry */
    225 		}
    226 
    227 		if (start && prev) {
    228 			pos = hostapd_eid_country_add(pos, end, chan_spacing,
    229 						      start, prev);
    230 			start = NULL;
    231 		}
    232 
    233 		/* Start new group */
    234 		start = prev = chan;
    235 	}
    236 
    237 	if (start) {
    238 		pos = hostapd_eid_country_add(pos, end, chan_spacing,
    239 					      start, prev);
    240 	}
    241 
    242 	if ((pos - eid) & 1) {
    243 		if (end - pos < 1)
    244 			return eid;
    245 		*pos++ = 0; /* pad for 16-bit alignment */
    246 	}
    247 
    248 	eid[1] = (pos - eid) - 2;
    249 
    250 	return pos;
    251 }
    252 
    253 
    254 static u8 * hostapd_eid_wpa(struct hostapd_data *hapd, u8 *eid, size_t len)
    255 {
    256 	const u8 *ie;
    257 	size_t ielen;
    258 
    259 	ie = wpa_auth_get_wpa_ie(hapd->wpa_auth, &ielen);
    260 	if (ie == NULL || ielen > len)
    261 		return eid;
    262 
    263 	os_memcpy(eid, ie, ielen);
    264 	return eid + ielen;
    265 }
    266 
    267 
    268 static u8 * hostapd_eid_csa(struct hostapd_data *hapd, u8 *eid)
    269 {
    270 	u8 chan;
    271 
    272 	if (!hapd->cs_freq_params.freq)
    273 		return eid;
    274 
    275 	if (ieee80211_freq_to_chan(hapd->cs_freq_params.freq, &chan) ==
    276 	    NUM_HOSTAPD_MODES)
    277 		return eid;
    278 
    279 	*eid++ = WLAN_EID_CHANNEL_SWITCH;
    280 	*eid++ = 3;
    281 	*eid++ = hapd->cs_block_tx;
    282 	*eid++ = chan;
    283 	*eid++ = hapd->cs_count;
    284 
    285 	return eid;
    286 }
    287 
    288 
    289 static u8 * hostapd_eid_secondary_channel(struct hostapd_data *hapd, u8 *eid)
    290 {
    291 	u8 sec_ch;
    292 
    293 	if (!hapd->cs_freq_params.sec_channel_offset)
    294 		return eid;
    295 
    296 	if (hapd->cs_freq_params.sec_channel_offset == -1)
    297 		sec_ch = HT_INFO_HT_PARAM_SECONDARY_CHNL_BELOW;
    298 	else if (hapd->cs_freq_params.sec_channel_offset == 1)
    299 		sec_ch = HT_INFO_HT_PARAM_SECONDARY_CHNL_ABOVE;
    300 	else
    301 		return eid;
    302 
    303 	*eid++ = WLAN_EID_SECONDARY_CHANNEL_OFFSET;
    304 	*eid++ = 1;
    305 	*eid++ = sec_ch;
    306 
    307 	return eid;
    308 }
    309 
    310 
    311 static u8 * hostapd_add_csa_elems(struct hostapd_data *hapd, u8 *pos,
    312 				  u8 *start, unsigned int *csa_counter_off)
    313 {
    314 	u8 *old_pos = pos;
    315 
    316 	if (!csa_counter_off)
    317 		return pos;
    318 
    319 	*csa_counter_off = 0;
    320 	pos = hostapd_eid_csa(hapd, pos);
    321 
    322 	if (pos != old_pos) {
    323 		/* save an offset to the counter - should be last byte */
    324 		*csa_counter_off = pos - start - 1;
    325 		pos = hostapd_eid_secondary_channel(hapd, pos);
    326 	}
    327 
    328 	return pos;
    329 }
    330 
    331 
    332 static u8 * hostapd_gen_probe_resp(struct hostapd_data *hapd,
    333 				   struct sta_info *sta,
    334 				   const struct ieee80211_mgmt *req,
    335 				   int is_p2p, size_t *resp_len)
    336 {
    337 	struct ieee80211_mgmt *resp;
    338 	u8 *pos, *epos;
    339 	size_t buflen;
    340 
    341 #define MAX_PROBERESP_LEN 768
    342 	buflen = MAX_PROBERESP_LEN;
    343 #ifdef CONFIG_WPS
    344 	if (hapd->wps_probe_resp_ie)
    345 		buflen += wpabuf_len(hapd->wps_probe_resp_ie);
    346 #endif /* CONFIG_WPS */
    347 #ifdef CONFIG_P2P
    348 	if (hapd->p2p_probe_resp_ie)
    349 		buflen += wpabuf_len(hapd->p2p_probe_resp_ie);
    350 #endif /* CONFIG_P2P */
    351 	if (hapd->conf->vendor_elements)
    352 		buflen += wpabuf_len(hapd->conf->vendor_elements);
    353 	resp = os_zalloc(buflen);
    354 	if (resp == NULL)
    355 		return NULL;
    356 
    357 	epos = ((u8 *) resp) + MAX_PROBERESP_LEN;
    358 
    359 	resp->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
    360 					   WLAN_FC_STYPE_PROBE_RESP);
    361 	if (req)
    362 		os_memcpy(resp->da, req->sa, ETH_ALEN);
    363 	os_memcpy(resp->sa, hapd->own_addr, ETH_ALEN);
    364 
    365 	os_memcpy(resp->bssid, hapd->own_addr, ETH_ALEN);
    366 	resp->u.probe_resp.beacon_int =
    367 		host_to_le16(hapd->iconf->beacon_int);
    368 
    369 	/* hardware or low-level driver will setup seq_ctrl and timestamp */
    370 	resp->u.probe_resp.capab_info =
    371 		host_to_le16(hostapd_own_capab_info(hapd, sta, 1));
    372 
    373 	pos = resp->u.probe_resp.variable;
    374 	*pos++ = WLAN_EID_SSID;
    375 	*pos++ = hapd->conf->ssid.ssid_len;
    376 	os_memcpy(pos, hapd->conf->ssid.ssid, hapd->conf->ssid.ssid_len);
    377 	pos += hapd->conf->ssid.ssid_len;
    378 
    379 	/* Supported rates */
    380 	pos = hostapd_eid_supp_rates(hapd, pos);
    381 
    382 	/* DS Params */
    383 	pos = hostapd_eid_ds_params(hapd, pos);
    384 
    385 	pos = hostapd_eid_country(hapd, pos, epos - pos);
    386 
    387 	/* Power Constraint element */
    388 	pos = hostapd_eid_pwr_constraint(hapd, pos);
    389 
    390 	/* ERP Information element */
    391 	pos = hostapd_eid_erp_info(hapd, pos);
    392 
    393 	/* Extended supported rates */
    394 	pos = hostapd_eid_ext_supp_rates(hapd, pos);
    395 
    396 	/* RSN, MDIE, WPA */
    397 	pos = hostapd_eid_wpa(hapd, pos, epos - pos);
    398 
    399 	pos = hostapd_eid_bss_load(hapd, pos, epos - pos);
    400 
    401 #ifdef CONFIG_IEEE80211N
    402 	pos = hostapd_eid_ht_capabilities(hapd, pos);
    403 	pos = hostapd_eid_ht_operation(hapd, pos);
    404 #endif /* CONFIG_IEEE80211N */
    405 
    406 	pos = hostapd_eid_ext_capab(hapd, pos);
    407 
    408 	pos = hostapd_eid_time_adv(hapd, pos);
    409 	pos = hostapd_eid_time_zone(hapd, pos);
    410 
    411 	pos = hostapd_eid_interworking(hapd, pos);
    412 	pos = hostapd_eid_adv_proto(hapd, pos);
    413 	pos = hostapd_eid_roaming_consortium(hapd, pos);
    414 
    415 	pos = hostapd_add_csa_elems(hapd, pos, (u8 *)resp,
    416 				    &hapd->cs_c_off_proberesp);
    417 #ifdef CONFIG_IEEE80211AC
    418 	pos = hostapd_eid_vht_capabilities(hapd, pos);
    419 	pos = hostapd_eid_vht_operation(hapd, pos);
    420 #endif /* CONFIG_IEEE80211AC */
    421 
    422 	/* Wi-Fi Alliance WMM */
    423 	pos = hostapd_eid_wmm(hapd, pos);
    424 
    425 #ifdef CONFIG_WPS
    426 	if (hapd->conf->wps_state && hapd->wps_probe_resp_ie) {
    427 		os_memcpy(pos, wpabuf_head(hapd->wps_probe_resp_ie),
    428 			  wpabuf_len(hapd->wps_probe_resp_ie));
    429 		pos += wpabuf_len(hapd->wps_probe_resp_ie);
    430 	}
    431 #endif /* CONFIG_WPS */
    432 
    433 #ifdef CONFIG_P2P
    434 	if ((hapd->conf->p2p & P2P_ENABLED) && is_p2p &&
    435 	    hapd->p2p_probe_resp_ie) {
    436 		os_memcpy(pos, wpabuf_head(hapd->p2p_probe_resp_ie),
    437 			  wpabuf_len(hapd->p2p_probe_resp_ie));
    438 		pos += wpabuf_len(hapd->p2p_probe_resp_ie);
    439 	}
    440 #endif /* CONFIG_P2P */
    441 #ifdef CONFIG_P2P_MANAGER
    442 	if ((hapd->conf->p2p & (P2P_MANAGE | P2P_ENABLED | P2P_GROUP_OWNER)) ==
    443 	    P2P_MANAGE)
    444 		pos = hostapd_eid_p2p_manage(hapd, pos);
    445 #endif /* CONFIG_P2P_MANAGER */
    446 
    447 #ifdef CONFIG_HS20
    448 	pos = hostapd_eid_hs20_indication(hapd, pos);
    449 	pos = hostapd_eid_osen(hapd, pos);
    450 #endif /* CONFIG_HS20 */
    451 
    452 	if (hapd->conf->vendor_elements) {
    453 		os_memcpy(pos, wpabuf_head(hapd->conf->vendor_elements),
    454 			  wpabuf_len(hapd->conf->vendor_elements));
    455 		pos += wpabuf_len(hapd->conf->vendor_elements);
    456 	}
    457 
    458 	*resp_len = pos - (u8 *) resp;
    459 	return (u8 *) resp;
    460 }
    461 
    462 
    463 enum ssid_match_result {
    464 	NO_SSID_MATCH,
    465 	EXACT_SSID_MATCH,
    466 	WILDCARD_SSID_MATCH
    467 };
    468 
    469 static enum ssid_match_result ssid_match(struct hostapd_data *hapd,
    470 					 const u8 *ssid, size_t ssid_len,
    471 					 const u8 *ssid_list,
    472 					 size_t ssid_list_len)
    473 {
    474 	const u8 *pos, *end;
    475 	int wildcard = 0;
    476 
    477 	if (ssid_len == 0)
    478 		wildcard = 1;
    479 	if (ssid_len == hapd->conf->ssid.ssid_len &&
    480 	    os_memcmp(ssid, hapd->conf->ssid.ssid, ssid_len) == 0)
    481 		return EXACT_SSID_MATCH;
    482 
    483 	if (ssid_list == NULL)
    484 		return wildcard ? WILDCARD_SSID_MATCH : NO_SSID_MATCH;
    485 
    486 	pos = ssid_list;
    487 	end = ssid_list + ssid_list_len;
    488 	while (pos + 1 <= end) {
    489 		if (pos + 2 + pos[1] > end)
    490 			break;
    491 		if (pos[1] == 0)
    492 			wildcard = 1;
    493 		if (pos[1] == hapd->conf->ssid.ssid_len &&
    494 		    os_memcmp(pos + 2, hapd->conf->ssid.ssid, pos[1]) == 0)
    495 			return EXACT_SSID_MATCH;
    496 		pos += 2 + pos[1];
    497 	}
    498 
    499 	return wildcard ? WILDCARD_SSID_MATCH : NO_SSID_MATCH;
    500 }
    501 
    502 
    503 void handle_probe_req(struct hostapd_data *hapd,
    504 		      const struct ieee80211_mgmt *mgmt, size_t len,
    505 		      int ssi_signal)
    506 {
    507 	u8 *resp;
    508 	struct ieee802_11_elems elems;
    509 	const u8 *ie;
    510 	size_t ie_len;
    511 	struct sta_info *sta = NULL;
    512 	size_t i, resp_len;
    513 	int noack;
    514 	enum ssid_match_result res;
    515 
    516 	ie = mgmt->u.probe_req.variable;
    517 	if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.probe_req))
    518 		return;
    519 	ie_len = len - (IEEE80211_HDRLEN + sizeof(mgmt->u.probe_req));
    520 
    521 	for (i = 0; hapd->probereq_cb && i < hapd->num_probereq_cb; i++)
    522 		if (hapd->probereq_cb[i].cb(hapd->probereq_cb[i].ctx,
    523 					    mgmt->sa, mgmt->da, mgmt->bssid,
    524 					    ie, ie_len, ssi_signal) > 0)
    525 			return;
    526 
    527 	if (!hapd->iconf->send_probe_response)
    528 		return;
    529 
    530 	if (ieee802_11_parse_elems(ie, ie_len, &elems, 0) == ParseFailed) {
    531 		wpa_printf(MSG_DEBUG, "Could not parse ProbeReq from " MACSTR,
    532 			   MAC2STR(mgmt->sa));
    533 		return;
    534 	}
    535 
    536 	if ((!elems.ssid || !elems.supp_rates)) {
    537 		wpa_printf(MSG_DEBUG, "STA " MACSTR " sent probe request "
    538 			   "without SSID or supported rates element",
    539 			   MAC2STR(mgmt->sa));
    540 		return;
    541 	}
    542 
    543 #ifdef CONFIG_P2P
    544 	if (hapd->p2p && elems.wps_ie) {
    545 		struct wpabuf *wps;
    546 		wps = ieee802_11_vendor_ie_concat(ie, ie_len, WPS_DEV_OUI_WFA);
    547 		if (wps && !p2p_group_match_dev_type(hapd->p2p_group, wps)) {
    548 			wpa_printf(MSG_MSGDUMP, "P2P: Ignore Probe Request "
    549 				   "due to mismatch with Requested Device "
    550 				   "Type");
    551 			wpabuf_free(wps);
    552 			return;
    553 		}
    554 		wpabuf_free(wps);
    555 	}
    556 
    557 	if (hapd->p2p && elems.p2p) {
    558 		struct wpabuf *p2p;
    559 		p2p = ieee802_11_vendor_ie_concat(ie, ie_len, P2P_IE_VENDOR_TYPE);
    560 		if (p2p && !p2p_group_match_dev_id(hapd->p2p_group, p2p)) {
    561 			wpa_printf(MSG_MSGDUMP, "P2P: Ignore Probe Request "
    562 				   "due to mismatch with Device ID");
    563 			wpabuf_free(p2p);
    564 			return;
    565 		}
    566 		wpabuf_free(p2p);
    567 	}
    568 #endif /* CONFIG_P2P */
    569 
    570 	if (hapd->conf->ignore_broadcast_ssid && elems.ssid_len == 0 &&
    571 	    elems.ssid_list_len == 0) {
    572 		wpa_printf(MSG_MSGDUMP, "Probe Request from " MACSTR " for "
    573 			   "broadcast SSID ignored", MAC2STR(mgmt->sa));
    574 		return;
    575 	}
    576 
    577 	sta = ap_get_sta(hapd, mgmt->sa);
    578 
    579 #ifdef CONFIG_P2P
    580 	if ((hapd->conf->p2p & P2P_GROUP_OWNER) &&
    581 	    elems.ssid_len == P2P_WILDCARD_SSID_LEN &&
    582 	    os_memcmp(elems.ssid, P2P_WILDCARD_SSID,
    583 		      P2P_WILDCARD_SSID_LEN) == 0) {
    584 		/* Process P2P Wildcard SSID like Wildcard SSID */
    585 		elems.ssid_len = 0;
    586 	}
    587 #endif /* CONFIG_P2P */
    588 
    589 	res = ssid_match(hapd, elems.ssid, elems.ssid_len,
    590 			 elems.ssid_list, elems.ssid_list_len);
    591 	if (res != NO_SSID_MATCH) {
    592 		if (sta)
    593 			sta->ssid_probe = &hapd->conf->ssid;
    594 	} else {
    595 		if (!(mgmt->da[0] & 0x01)) {
    596 			wpa_printf(MSG_MSGDUMP, "Probe Request from " MACSTR
    597 				   " for foreign SSID '%s' (DA " MACSTR ")%s",
    598 				   MAC2STR(mgmt->sa),
    599 				   wpa_ssid_txt(elems.ssid, elems.ssid_len),
    600 				   MAC2STR(mgmt->da),
    601 				   elems.ssid_list ? " (SSID list)" : "");
    602 		}
    603 		return;
    604 	}
    605 
    606 #ifdef CONFIG_INTERWORKING
    607 	if (hapd->conf->interworking &&
    608 	    elems.interworking && elems.interworking_len >= 1) {
    609 		u8 ant = elems.interworking[0] & 0x0f;
    610 		if (ant != INTERWORKING_ANT_WILDCARD &&
    611 		    ant != hapd->conf->access_network_type) {
    612 			wpa_printf(MSG_MSGDUMP, "Probe Request from " MACSTR
    613 				   " for mismatching ANT %u ignored",
    614 				   MAC2STR(mgmt->sa), ant);
    615 			return;
    616 		}
    617 	}
    618 
    619 	if (hapd->conf->interworking && elems.interworking &&
    620 	    (elems.interworking_len == 7 || elems.interworking_len == 9)) {
    621 		const u8 *hessid;
    622 		if (elems.interworking_len == 7)
    623 			hessid = elems.interworking + 1;
    624 		else
    625 			hessid = elems.interworking + 1 + 2;
    626 		if (!is_broadcast_ether_addr(hessid) &&
    627 		    os_memcmp(hessid, hapd->conf->hessid, ETH_ALEN) != 0) {
    628 			wpa_printf(MSG_MSGDUMP, "Probe Request from " MACSTR
    629 				   " for mismatching HESSID " MACSTR
    630 				   " ignored",
    631 				   MAC2STR(mgmt->sa), MAC2STR(hessid));
    632 			return;
    633 		}
    634 	}
    635 #endif /* CONFIG_INTERWORKING */
    636 
    637 #ifdef CONFIG_P2P
    638 	if ((hapd->conf->p2p & P2P_GROUP_OWNER) &&
    639 	    supp_rates_11b_only(&elems)) {
    640 		/* Indicates support for 11b rates only */
    641 		wpa_printf(MSG_EXCESSIVE, "P2P: Ignore Probe Request from "
    642 			   MACSTR " with only 802.11b rates",
    643 			   MAC2STR(mgmt->sa));
    644 		return;
    645 	}
    646 #endif /* CONFIG_P2P */
    647 
    648 	/* TODO: verify that supp_rates contains at least one matching rate
    649 	 * with AP configuration */
    650 
    651 #ifdef CONFIG_TESTING_OPTIONS
    652 	if (hapd->iconf->ignore_probe_probability > 0.0 &&
    653 	    drand48() < hapd->iconf->ignore_probe_probability) {
    654 		wpa_printf(MSG_INFO,
    655 			   "TESTING: ignoring probe request from " MACSTR,
    656 			   MAC2STR(mgmt->sa));
    657 		return;
    658 	}
    659 #endif /* CONFIG_TESTING_OPTIONS */
    660 
    661 	resp = hostapd_gen_probe_resp(hapd, sta, mgmt, elems.p2p != NULL,
    662 				      &resp_len);
    663 	if (resp == NULL)
    664 		return;
    665 
    666 	/*
    667 	 * If this is a broadcast probe request, apply no ack policy to avoid
    668 	 * excessive retries.
    669 	 */
    670 	noack = !!(res == WILDCARD_SSID_MATCH &&
    671 		   is_broadcast_ether_addr(mgmt->da));
    672 
    673 	if (hostapd_drv_send_mlme(hapd, resp, resp_len, noack) < 0)
    674 		wpa_printf(MSG_INFO, "handle_probe_req: send failed");
    675 
    676 	os_free(resp);
    677 
    678 	wpa_printf(MSG_EXCESSIVE, "STA " MACSTR " sent probe request for %s "
    679 		   "SSID", MAC2STR(mgmt->sa),
    680 		   elems.ssid_len == 0 ? "broadcast" : "our");
    681 }
    682 
    683 
    684 static u8 * hostapd_probe_resp_offloads(struct hostapd_data *hapd,
    685 					size_t *resp_len)
    686 {
    687 	/* check probe response offloading caps and print warnings */
    688 	if (!(hapd->iface->drv_flags & WPA_DRIVER_FLAGS_PROBE_RESP_OFFLOAD))
    689 		return NULL;
    690 
    691 #ifdef CONFIG_WPS
    692 	if (hapd->conf->wps_state && hapd->wps_probe_resp_ie &&
    693 	    (!(hapd->iface->probe_resp_offloads &
    694 	       (WPA_DRIVER_PROBE_RESP_OFFLOAD_WPS |
    695 		WPA_DRIVER_PROBE_RESP_OFFLOAD_WPS2))))
    696 		wpa_printf(MSG_WARNING, "Device is trying to offload WPS "
    697 			   "Probe Response while not supporting this");
    698 #endif /* CONFIG_WPS */
    699 
    700 #ifdef CONFIG_P2P
    701 	if ((hapd->conf->p2p & P2P_ENABLED) && hapd->p2p_probe_resp_ie &&
    702 	    !(hapd->iface->probe_resp_offloads &
    703 	      WPA_DRIVER_PROBE_RESP_OFFLOAD_P2P))
    704 		wpa_printf(MSG_WARNING, "Device is trying to offload P2P "
    705 			   "Probe Response while not supporting this");
    706 #endif  /* CONFIG_P2P */
    707 
    708 	if (hapd->conf->interworking &&
    709 	    !(hapd->iface->probe_resp_offloads &
    710 	      WPA_DRIVER_PROBE_RESP_OFFLOAD_INTERWORKING))
    711 		wpa_printf(MSG_WARNING, "Device is trying to offload "
    712 			   "Interworking Probe Response while not supporting "
    713 			   "this");
    714 
    715 	/* Generate a Probe Response template for the non-P2P case */
    716 	return hostapd_gen_probe_resp(hapd, NULL, NULL, 0, resp_len);
    717 }
    718 
    719 #endif /* NEED_AP_MLME */
    720 
    721 
    722 int ieee802_11_build_ap_params(struct hostapd_data *hapd,
    723 			       struct wpa_driver_ap_params *params)
    724 {
    725 	struct ieee80211_mgmt *head = NULL;
    726 	u8 *tail = NULL;
    727 	size_t head_len = 0, tail_len = 0;
    728 	u8 *resp = NULL;
    729 	size_t resp_len = 0;
    730 #ifdef NEED_AP_MLME
    731 	u16 capab_info;
    732 	u8 *pos, *tailpos;
    733 
    734 #define BEACON_HEAD_BUF_SIZE 256
    735 #define BEACON_TAIL_BUF_SIZE 512
    736 	head = os_zalloc(BEACON_HEAD_BUF_SIZE);
    737 	tail_len = BEACON_TAIL_BUF_SIZE;
    738 #ifdef CONFIG_WPS
    739 	if (hapd->conf->wps_state && hapd->wps_beacon_ie)
    740 		tail_len += wpabuf_len(hapd->wps_beacon_ie);
    741 #endif /* CONFIG_WPS */
    742 #ifdef CONFIG_P2P
    743 	if (hapd->p2p_beacon_ie)
    744 		tail_len += wpabuf_len(hapd->p2p_beacon_ie);
    745 #endif /* CONFIG_P2P */
    746 	if (hapd->conf->vendor_elements)
    747 		tail_len += wpabuf_len(hapd->conf->vendor_elements);
    748 	tailpos = tail = os_malloc(tail_len);
    749 	if (head == NULL || tail == NULL) {
    750 		wpa_printf(MSG_ERROR, "Failed to set beacon data");
    751 		os_free(head);
    752 		os_free(tail);
    753 		return -1;
    754 	}
    755 
    756 	head->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
    757 					   WLAN_FC_STYPE_BEACON);
    758 	head->duration = host_to_le16(0);
    759 	os_memset(head->da, 0xff, ETH_ALEN);
    760 
    761 	os_memcpy(head->sa, hapd->own_addr, ETH_ALEN);
    762 	os_memcpy(head->bssid, hapd->own_addr, ETH_ALEN);
    763 	head->u.beacon.beacon_int =
    764 		host_to_le16(hapd->iconf->beacon_int);
    765 
    766 	/* hardware or low-level driver will setup seq_ctrl and timestamp */
    767 	capab_info = hostapd_own_capab_info(hapd, NULL, 0);
    768 	head->u.beacon.capab_info = host_to_le16(capab_info);
    769 	pos = &head->u.beacon.variable[0];
    770 
    771 	/* SSID */
    772 	*pos++ = WLAN_EID_SSID;
    773 	if (hapd->conf->ignore_broadcast_ssid == 2) {
    774 		/* clear the data, but keep the correct length of the SSID */
    775 		*pos++ = hapd->conf->ssid.ssid_len;
    776 		os_memset(pos, 0, hapd->conf->ssid.ssid_len);
    777 		pos += hapd->conf->ssid.ssid_len;
    778 	} else if (hapd->conf->ignore_broadcast_ssid) {
    779 		*pos++ = 0; /* empty SSID */
    780 	} else {
    781 		*pos++ = hapd->conf->ssid.ssid_len;
    782 		os_memcpy(pos, hapd->conf->ssid.ssid,
    783 			  hapd->conf->ssid.ssid_len);
    784 		pos += hapd->conf->ssid.ssid_len;
    785 	}
    786 
    787 	/* Supported rates */
    788 	pos = hostapd_eid_supp_rates(hapd, pos);
    789 
    790 	/* DS Params */
    791 	pos = hostapd_eid_ds_params(hapd, pos);
    792 
    793 	head_len = pos - (u8 *) head;
    794 
    795 	tailpos = hostapd_eid_country(hapd, tailpos,
    796 				      tail + BEACON_TAIL_BUF_SIZE - tailpos);
    797 
    798 	/* Power Constraint element */
    799 	tailpos = hostapd_eid_pwr_constraint(hapd, tailpos);
    800 
    801 	/* ERP Information element */
    802 	tailpos = hostapd_eid_erp_info(hapd, tailpos);
    803 
    804 	/* Extended supported rates */
    805 	tailpos = hostapd_eid_ext_supp_rates(hapd, tailpos);
    806 
    807 	/* RSN, MDIE, WPA */
    808 	tailpos = hostapd_eid_wpa(hapd, tailpos, tail + BEACON_TAIL_BUF_SIZE -
    809 				  tailpos);
    810 
    811 	tailpos = hostapd_eid_bss_load(hapd, tailpos,
    812 				       tail + BEACON_TAIL_BUF_SIZE - tailpos);
    813 
    814 #ifdef CONFIG_IEEE80211N
    815 	tailpos = hostapd_eid_ht_capabilities(hapd, tailpos);
    816 	tailpos = hostapd_eid_ht_operation(hapd, tailpos);
    817 #endif /* CONFIG_IEEE80211N */
    818 
    819 	tailpos = hostapd_eid_ext_capab(hapd, tailpos);
    820 
    821 	/*
    822 	 * TODO: Time Advertisement element should only be included in some
    823 	 * DTIM Beacon frames.
    824 	 */
    825 	tailpos = hostapd_eid_time_adv(hapd, tailpos);
    826 
    827 	tailpos = hostapd_eid_interworking(hapd, tailpos);
    828 	tailpos = hostapd_eid_adv_proto(hapd, tailpos);
    829 	tailpos = hostapd_eid_roaming_consortium(hapd, tailpos);
    830 	tailpos = hostapd_add_csa_elems(hapd, tailpos, tail,
    831 					&hapd->cs_c_off_beacon);
    832 #ifdef CONFIG_IEEE80211AC
    833 	tailpos = hostapd_eid_vht_capabilities(hapd, tailpos);
    834 	tailpos = hostapd_eid_vht_operation(hapd, tailpos);
    835 #endif /* CONFIG_IEEE80211AC */
    836 
    837 	/* Wi-Fi Alliance WMM */
    838 	tailpos = hostapd_eid_wmm(hapd, tailpos);
    839 
    840 #ifdef CONFIG_WPS
    841 	if (hapd->conf->wps_state && hapd->wps_beacon_ie) {
    842 		os_memcpy(tailpos, wpabuf_head(hapd->wps_beacon_ie),
    843 			  wpabuf_len(hapd->wps_beacon_ie));
    844 		tailpos += wpabuf_len(hapd->wps_beacon_ie);
    845 	}
    846 #endif /* CONFIG_WPS */
    847 
    848 #ifdef CONFIG_P2P
    849 	if ((hapd->conf->p2p & P2P_ENABLED) && hapd->p2p_beacon_ie) {
    850 		os_memcpy(tailpos, wpabuf_head(hapd->p2p_beacon_ie),
    851 			  wpabuf_len(hapd->p2p_beacon_ie));
    852 		tailpos += wpabuf_len(hapd->p2p_beacon_ie);
    853 	}
    854 #endif /* CONFIG_P2P */
    855 #ifdef CONFIG_P2P_MANAGER
    856 	if ((hapd->conf->p2p & (P2P_MANAGE | P2P_ENABLED | P2P_GROUP_OWNER)) ==
    857 	    P2P_MANAGE)
    858 		tailpos = hostapd_eid_p2p_manage(hapd, tailpos);
    859 #endif /* CONFIG_P2P_MANAGER */
    860 
    861 #ifdef CONFIG_HS20
    862 	tailpos = hostapd_eid_hs20_indication(hapd, tailpos);
    863 	tailpos = hostapd_eid_osen(hapd, tailpos);
    864 #endif /* CONFIG_HS20 */
    865 
    866 	if (hapd->conf->vendor_elements) {
    867 		os_memcpy(tailpos, wpabuf_head(hapd->conf->vendor_elements),
    868 			  wpabuf_len(hapd->conf->vendor_elements));
    869 		tailpos += wpabuf_len(hapd->conf->vendor_elements);
    870 	}
    871 
    872 	tail_len = tailpos > tail ? tailpos - tail : 0;
    873 
    874 	resp = hostapd_probe_resp_offloads(hapd, &resp_len);
    875 #endif /* NEED_AP_MLME */
    876 
    877 	os_memset(params, 0, sizeof(*params));
    878 	params->head = (u8 *) head;
    879 	params->head_len = head_len;
    880 	params->tail = tail;
    881 	params->tail_len = tail_len;
    882 	params->proberesp = resp;
    883 	params->proberesp_len = resp_len;
    884 	params->dtim_period = hapd->conf->dtim_period;
    885 	params->beacon_int = hapd->iconf->beacon_int;
    886 	params->basic_rates = hapd->iface->basic_rates;
    887 	params->ssid = hapd->conf->ssid.ssid;
    888 	params->ssid_len = hapd->conf->ssid.ssid_len;
    889 	params->pairwise_ciphers = hapd->conf->wpa_pairwise |
    890 		hapd->conf->rsn_pairwise;
    891 	params->group_cipher = hapd->conf->wpa_group;
    892 	params->key_mgmt_suites = hapd->conf->wpa_key_mgmt;
    893 	params->auth_algs = hapd->conf->auth_algs;
    894 	params->wpa_version = hapd->conf->wpa;
    895 	params->privacy = hapd->conf->ssid.wep.keys_set || hapd->conf->wpa ||
    896 		(hapd->conf->ieee802_1x &&
    897 		 (hapd->conf->default_wep_key_len ||
    898 		  hapd->conf->individual_wep_key_len));
    899 	switch (hapd->conf->ignore_broadcast_ssid) {
    900 	case 0:
    901 		params->hide_ssid = NO_SSID_HIDING;
    902 		break;
    903 	case 1:
    904 		params->hide_ssid = HIDDEN_SSID_ZERO_LEN;
    905 		break;
    906 	case 2:
    907 		params->hide_ssid = HIDDEN_SSID_ZERO_CONTENTS;
    908 		break;
    909 	}
    910 	params->isolate = hapd->conf->isolate;
    911 #ifdef NEED_AP_MLME
    912 	params->cts_protect = !!(ieee802_11_erp_info(hapd) &
    913 				ERP_INFO_USE_PROTECTION);
    914 	params->preamble = hapd->iface->num_sta_no_short_preamble == 0 &&
    915 		hapd->iconf->preamble == SHORT_PREAMBLE;
    916 	if (hapd->iface->current_mode &&
    917 	    hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G)
    918 		params->short_slot_time =
    919 			hapd->iface->num_sta_no_short_slot_time > 0 ? 0 : 1;
    920 	else
    921 		params->short_slot_time = -1;
    922 	if (!hapd->iconf->ieee80211n || hapd->conf->disable_11n)
    923 		params->ht_opmode = -1;
    924 	else
    925 		params->ht_opmode = hapd->iface->ht_op_mode;
    926 #endif /* NEED_AP_MLME */
    927 	params->interworking = hapd->conf->interworking;
    928 	if (hapd->conf->interworking &&
    929 	    !is_zero_ether_addr(hapd->conf->hessid))
    930 		params->hessid = hapd->conf->hessid;
    931 	params->access_network_type = hapd->conf->access_network_type;
    932 	params->ap_max_inactivity = hapd->conf->ap_max_inactivity;
    933 #ifdef CONFIG_HS20
    934 	params->disable_dgaf = hapd->conf->disable_dgaf;
    935 	if (hapd->conf->osen) {
    936 		params->privacy = 1;
    937 		params->osen = 1;
    938 	}
    939 #endif /* CONFIG_HS20 */
    940 	return 0;
    941 }
    942 
    943 
    944 void ieee802_11_free_ap_params(struct wpa_driver_ap_params *params)
    945 {
    946 	os_free(params->tail);
    947 	params->tail = NULL;
    948 	os_free(params->head);
    949 	params->head = NULL;
    950 	os_free(params->proberesp);
    951 	params->proberesp = NULL;
    952 }
    953 
    954 
    955 int ieee802_11_set_beacon(struct hostapd_data *hapd)
    956 {
    957 	struct wpa_driver_ap_params params;
    958 	struct hostapd_freq_params freq;
    959 	struct hostapd_iface *iface = hapd->iface;
    960 	struct hostapd_config *iconf = iface->conf;
    961 	struct wpabuf *beacon, *proberesp, *assocresp;
    962 	int res, ret = -1;
    963 
    964 	if (hapd->csa_in_progress) {
    965 		wpa_printf(MSG_ERROR, "Cannot set beacons during CSA period");
    966 		return -1;
    967 	}
    968 
    969 	hapd->beacon_set_done = 1;
    970 
    971 	if (ieee802_11_build_ap_params(hapd, &params) < 0)
    972 		return -1;
    973 
    974 	if (hostapd_build_ap_extra_ies(hapd, &beacon, &proberesp, &assocresp) <
    975 	    0)
    976 		goto fail;
    977 
    978 	params.beacon_ies = beacon;
    979 	params.proberesp_ies = proberesp;
    980 	params.assocresp_ies = assocresp;
    981 
    982 	if (iface->current_mode &&
    983 	    hostapd_set_freq_params(&freq, iconf->hw_mode, iface->freq,
    984 				    iconf->channel, iconf->ieee80211n,
    985 				    iconf->ieee80211ac,
    986 				    iconf->secondary_channel,
    987 				    iconf->vht_oper_chwidth,
    988 				    iconf->vht_oper_centr_freq_seg0_idx,
    989 				    iconf->vht_oper_centr_freq_seg1_idx,
    990 				    iface->current_mode->vht_capab) == 0)
    991 		params.freq = &freq;
    992 
    993 	res = hostapd_drv_set_ap(hapd, &params);
    994 	hostapd_free_ap_extra_ies(hapd, beacon, proberesp, assocresp);
    995 	if (res)
    996 		wpa_printf(MSG_ERROR, "Failed to set beacon parameters");
    997 	else
    998 		ret = 0;
    999 fail:
   1000 	ieee802_11_free_ap_params(&params);
   1001 	return ret;
   1002 }
   1003 
   1004 
   1005 int ieee802_11_set_beacons(struct hostapd_iface *iface)
   1006 {
   1007 	size_t i;
   1008 	int ret = 0;
   1009 
   1010 	for (i = 0; i < iface->num_bss; i++) {
   1011 		if (iface->bss[i]->started &&
   1012 		    ieee802_11_set_beacon(iface->bss[i]) < 0)
   1013 			ret = -1;
   1014 	}
   1015 
   1016 	return ret;
   1017 }
   1018 
   1019 
   1020 /* only update beacons if started */
   1021 int ieee802_11_update_beacons(struct hostapd_iface *iface)
   1022 {
   1023 	size_t i;
   1024 	int ret = 0;
   1025 
   1026 	for (i = 0; i < iface->num_bss; i++) {
   1027 		if (iface->bss[i]->beacon_set_done && iface->bss[i]->started &&
   1028 		    ieee802_11_set_beacon(iface->bss[i]) < 0)
   1029 			ret = -1;
   1030 	}
   1031 
   1032 	return ret;
   1033 }
   1034 
   1035 #endif /* CONFIG_NATIVE_WINDOWS */
   1036