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 program is free software; you can redistribute it and/or modify
      8  * it under the terms of the GNU General Public License version 2 as
      9  * published by the Free Software Foundation.
     10  *
     11  * Alternatively, this software may be distributed under the terms of BSD
     12  * license.
     13  *
     14  * See README and COPYING for more details.
     15  */
     16 
     17 #include "utils/includes.h"
     18 
     19 #ifndef CONFIG_NATIVE_WINDOWS
     20 
     21 #include "utils/common.h"
     22 #include "common/ieee802_11_defs.h"
     23 #include "common/ieee802_11_common.h"
     24 #include "drivers/driver.h"
     25 #include "wps/wps_defs.h"
     26 #include "p2p/p2p.h"
     27 #include "hostapd.h"
     28 #include "ieee802_11.h"
     29 #include "wpa_auth.h"
     30 #include "wmm.h"
     31 #include "ap_config.h"
     32 #include "sta_info.h"
     33 #include "p2p_hostapd.h"
     34 #include "ap_drv_ops.h"
     35 #include "beacon.h"
     36 #include "hs20.h"
     37 
     38 
     39 #ifdef NEED_AP_MLME
     40 
     41 static u8 ieee802_11_erp_info(struct hostapd_data *hapd)
     42 {
     43 	u8 erp = 0;
     44 
     45 	if (hapd->iface->current_mode == NULL ||
     46 	    hapd->iface->current_mode->mode != HOSTAPD_MODE_IEEE80211G)
     47 		return 0;
     48 
     49 	if (hapd->iface->olbc)
     50 		erp |= ERP_INFO_USE_PROTECTION;
     51 	if (hapd->iface->num_sta_non_erp > 0) {
     52 		erp |= ERP_INFO_NON_ERP_PRESENT |
     53 			ERP_INFO_USE_PROTECTION;
     54 	}
     55 	if (hapd->iface->num_sta_no_short_preamble > 0 ||
     56 	    hapd->iconf->preamble == LONG_PREAMBLE)
     57 		erp |= ERP_INFO_BARKER_PREAMBLE_MODE;
     58 
     59 	return erp;
     60 }
     61 
     62 
     63 static u8 * hostapd_eid_ds_params(struct hostapd_data *hapd, u8 *eid)
     64 {
     65 	*eid++ = WLAN_EID_DS_PARAMS;
     66 	*eid++ = 1;
     67 	*eid++ = hapd->iconf->channel;
     68 	return eid;
     69 }
     70 
     71 
     72 static u8 * hostapd_eid_erp_info(struct hostapd_data *hapd, u8 *eid)
     73 {
     74 	if (hapd->iface->current_mode == NULL ||
     75 	    hapd->iface->current_mode->mode != HOSTAPD_MODE_IEEE80211G)
     76 		return eid;
     77 
     78 	/* Set NonERP_present and use_protection bits if there
     79 	 * are any associated NonERP stations. */
     80 	/* TODO: use_protection bit can be set to zero even if
     81 	 * there are NonERP stations present. This optimization
     82 	 * might be useful if NonERP stations are "quiet".
     83 	 * See 802.11g/D6 E-1 for recommended practice.
     84 	 * In addition, Non ERP present might be set, if AP detects Non ERP
     85 	 * operation on other APs. */
     86 
     87 	/* Add ERP Information element */
     88 	*eid++ = WLAN_EID_ERP_INFO;
     89 	*eid++ = 1;
     90 	*eid++ = ieee802_11_erp_info(hapd);
     91 
     92 	return eid;
     93 }
     94 
     95 
     96 static u8 * hostapd_eid_country_add(u8 *pos, u8 *end, int chan_spacing,
     97 				    struct hostapd_channel_data *start,
     98 				    struct hostapd_channel_data *prev)
     99 {
    100 	if (end - pos < 3)
    101 		return pos;
    102 
    103 	/* first channel number */
    104 	*pos++ = start->chan;
    105 	/* number of channels */
    106 	*pos++ = (prev->chan - start->chan) / chan_spacing + 1;
    107 	/* maximum transmit power level */
    108 	*pos++ = start->max_tx_power;
    109 
    110 	return pos;
    111 }
    112 
    113 
    114 static u8 * hostapd_eid_country(struct hostapd_data *hapd, u8 *eid,
    115 				int max_len)
    116 {
    117 	u8 *pos = eid;
    118 	u8 *end = eid + max_len;
    119 	int i;
    120 	struct hostapd_hw_modes *mode;
    121 	struct hostapd_channel_data *start, *prev;
    122 	int chan_spacing = 1;
    123 
    124 	if (!hapd->iconf->ieee80211d || max_len < 6 ||
    125 	    hapd->iface->current_mode == NULL)
    126 		return eid;
    127 
    128 	*pos++ = WLAN_EID_COUNTRY;
    129 	pos++; /* length will be set later */
    130 	os_memcpy(pos, hapd->iconf->country, 3); /* e.g., 'US ' */
    131 	pos += 3;
    132 
    133 	mode = hapd->iface->current_mode;
    134 	if (mode->mode == HOSTAPD_MODE_IEEE80211A)
    135 		chan_spacing = 4;
    136 
    137 	start = prev = NULL;
    138 	for (i = 0; i < mode->num_channels; i++) {
    139 		struct hostapd_channel_data *chan = &mode->channels[i];
    140 		if (chan->flag & HOSTAPD_CHAN_DISABLED)
    141 			continue;
    142 		if (start && prev &&
    143 		    prev->chan + chan_spacing == chan->chan &&
    144 		    start->max_tx_power == chan->max_tx_power) {
    145 			prev = chan;
    146 			continue; /* can use same entry */
    147 		}
    148 
    149 		if (start) {
    150 			pos = hostapd_eid_country_add(pos, end, chan_spacing,
    151 						      start, prev);
    152 			start = NULL;
    153 		}
    154 
    155 		/* Start new group */
    156 		start = prev = chan;
    157 	}
    158 
    159 	if (start) {
    160 		pos = hostapd_eid_country_add(pos, end, chan_spacing,
    161 					      start, prev);
    162 	}
    163 
    164 	if ((pos - eid) & 1) {
    165 		if (end - pos < 1)
    166 			return eid;
    167 		*pos++ = 0; /* pad for 16-bit alignment */
    168 	}
    169 
    170 	eid[1] = (pos - eid) - 2;
    171 
    172 	return pos;
    173 }
    174 
    175 
    176 static u8 * hostapd_eid_wpa(struct hostapd_data *hapd, u8 *eid, size_t len)
    177 {
    178 	const u8 *ie;
    179 	size_t ielen;
    180 
    181 	ie = wpa_auth_get_wpa_ie(hapd->wpa_auth, &ielen);
    182 	if (ie == NULL || ielen > len)
    183 		return eid;
    184 
    185 	os_memcpy(eid, ie, ielen);
    186 	return eid + ielen;
    187 }
    188 
    189 
    190 static u8 * hostapd_gen_probe_resp(struct hostapd_data *hapd,
    191 				   struct sta_info *sta,
    192 				   const struct ieee80211_mgmt *req,
    193 				   int is_p2p, size_t *resp_len)
    194 {
    195 	struct ieee80211_mgmt *resp;
    196 	u8 *pos, *epos;
    197 	size_t buflen;
    198 
    199 #define MAX_PROBERESP_LEN 768
    200 	buflen = MAX_PROBERESP_LEN;
    201 #ifdef CONFIG_WPS
    202 	if (hapd->wps_probe_resp_ie)
    203 		buflen += wpabuf_len(hapd->wps_probe_resp_ie);
    204 #endif /* CONFIG_WPS */
    205 #ifdef CONFIG_P2P
    206 	if (hapd->p2p_probe_resp_ie)
    207 		buflen += wpabuf_len(hapd->p2p_probe_resp_ie);
    208 #endif /* CONFIG_P2P */
    209 	if (hapd->conf->vendor_elements)
    210 		buflen += wpabuf_len(hapd->conf->vendor_elements);
    211 	resp = os_zalloc(buflen);
    212 	if (resp == NULL)
    213 		return NULL;
    214 
    215 	epos = ((u8 *) resp) + MAX_PROBERESP_LEN;
    216 
    217 	resp->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
    218 					   WLAN_FC_STYPE_PROBE_RESP);
    219 	if (req)
    220 		os_memcpy(resp->da, req->sa, ETH_ALEN);
    221 	os_memcpy(resp->sa, hapd->own_addr, ETH_ALEN);
    222 
    223 	os_memcpy(resp->bssid, hapd->own_addr, ETH_ALEN);
    224 	resp->u.probe_resp.beacon_int =
    225 		host_to_le16(hapd->iconf->beacon_int);
    226 
    227 	/* hardware or low-level driver will setup seq_ctrl and timestamp */
    228 	resp->u.probe_resp.capab_info =
    229 		host_to_le16(hostapd_own_capab_info(hapd, sta, 1));
    230 
    231 	pos = resp->u.probe_resp.variable;
    232 	*pos++ = WLAN_EID_SSID;
    233 	*pos++ = hapd->conf->ssid.ssid_len;
    234 	os_memcpy(pos, hapd->conf->ssid.ssid, hapd->conf->ssid.ssid_len);
    235 	pos += hapd->conf->ssid.ssid_len;
    236 
    237 	/* Supported rates */
    238 	pos = hostapd_eid_supp_rates(hapd, pos);
    239 
    240 	/* DS Params */
    241 	pos = hostapd_eid_ds_params(hapd, pos);
    242 
    243 	pos = hostapd_eid_country(hapd, pos, epos - pos);
    244 
    245 	/* ERP Information element */
    246 	pos = hostapd_eid_erp_info(hapd, pos);
    247 
    248 	/* Extended supported rates */
    249 	pos = hostapd_eid_ext_supp_rates(hapd, pos);
    250 
    251 	/* RSN, MDIE, WPA */
    252 	pos = hostapd_eid_wpa(hapd, pos, epos - pos);
    253 
    254 #ifdef CONFIG_IEEE80211N
    255 	pos = hostapd_eid_ht_capabilities(hapd, pos);
    256 	pos = hostapd_eid_ht_operation(hapd, pos);
    257 #endif /* CONFIG_IEEE80211N */
    258 
    259 	pos = hostapd_eid_ext_capab(hapd, pos);
    260 
    261 	pos = hostapd_eid_time_adv(hapd, pos);
    262 	pos = hostapd_eid_time_zone(hapd, pos);
    263 
    264 	pos = hostapd_eid_interworking(hapd, pos);
    265 	pos = hostapd_eid_adv_proto(hapd, pos);
    266 	pos = hostapd_eid_roaming_consortium(hapd, pos);
    267 
    268 #ifdef CONFIG_IEEE80211AC
    269 	pos = hostapd_eid_vht_capabilities(hapd, pos);
    270 	pos = hostapd_eid_vht_operation(hapd, pos);
    271 #endif /* CONFIG_IEEE80211AC */
    272 
    273 	/* Wi-Fi Alliance WMM */
    274 	pos = hostapd_eid_wmm(hapd, pos);
    275 
    276 #ifdef CONFIG_WPS
    277 	if (hapd->conf->wps_state && hapd->wps_probe_resp_ie) {
    278 		os_memcpy(pos, wpabuf_head(hapd->wps_probe_resp_ie),
    279 			  wpabuf_len(hapd->wps_probe_resp_ie));
    280 		pos += wpabuf_len(hapd->wps_probe_resp_ie);
    281 	}
    282 #endif /* CONFIG_WPS */
    283 
    284 #ifdef CONFIG_P2P
    285 	if ((hapd->conf->p2p & P2P_ENABLED) && is_p2p &&
    286 	    hapd->p2p_probe_resp_ie) {
    287 		os_memcpy(pos, wpabuf_head(hapd->p2p_probe_resp_ie),
    288 			  wpabuf_len(hapd->p2p_probe_resp_ie));
    289 		pos += wpabuf_len(hapd->p2p_probe_resp_ie);
    290 	}
    291 #endif /* CONFIG_P2P */
    292 #ifdef CONFIG_P2P_MANAGER
    293 	if ((hapd->conf->p2p & (P2P_MANAGE | P2P_ENABLED | P2P_GROUP_OWNER)) ==
    294 	    P2P_MANAGE)
    295 		pos = hostapd_eid_p2p_manage(hapd, pos);
    296 #endif /* CONFIG_P2P_MANAGER */
    297 
    298 #ifdef CONFIG_HS20
    299 	pos = hostapd_eid_hs20_indication(hapd, pos);
    300 #endif /* CONFIG_HS20 */
    301 
    302 	if (hapd->conf->vendor_elements) {
    303 		os_memcpy(pos, wpabuf_head(hapd->conf->vendor_elements),
    304 			  wpabuf_len(hapd->conf->vendor_elements));
    305 		pos += wpabuf_len(hapd->conf->vendor_elements);
    306 	}
    307 
    308 	*resp_len = pos - (u8 *) resp;
    309 	return (u8 *) resp;
    310 }
    311 
    312 
    313 enum ssid_match_result {
    314 	NO_SSID_MATCH,
    315 	EXACT_SSID_MATCH,
    316 	WILDCARD_SSID_MATCH
    317 };
    318 
    319 static enum ssid_match_result ssid_match(struct hostapd_data *hapd,
    320 					 const u8 *ssid, size_t ssid_len,
    321 					 const u8 *ssid_list,
    322 					 size_t ssid_list_len)
    323 {
    324 	const u8 *pos, *end;
    325 	int wildcard = 0;
    326 
    327 	if (ssid_len == 0)
    328 		wildcard = 1;
    329 	if (ssid_len == hapd->conf->ssid.ssid_len &&
    330 	    os_memcmp(ssid, hapd->conf->ssid.ssid, ssid_len) == 0)
    331 		return EXACT_SSID_MATCH;
    332 
    333 	if (ssid_list == NULL)
    334 		return wildcard ? WILDCARD_SSID_MATCH : NO_SSID_MATCH;
    335 
    336 	pos = ssid_list;
    337 	end = ssid_list + ssid_list_len;
    338 	while (pos + 1 <= end) {
    339 		if (pos + 2 + pos[1] > end)
    340 			break;
    341 		if (pos[1] == 0)
    342 			wildcard = 1;
    343 		if (pos[1] == hapd->conf->ssid.ssid_len &&
    344 		    os_memcmp(pos + 2, hapd->conf->ssid.ssid, pos[1]) == 0)
    345 			return EXACT_SSID_MATCH;
    346 		pos += 2 + pos[1];
    347 	}
    348 
    349 	return wildcard ? WILDCARD_SSID_MATCH : NO_SSID_MATCH;
    350 }
    351 
    352 
    353 void handle_probe_req(struct hostapd_data *hapd,
    354 		      const struct ieee80211_mgmt *mgmt, size_t len,
    355 		      int ssi_signal)
    356 {
    357 	u8 *resp;
    358 	struct ieee802_11_elems elems;
    359 	const u8 *ie;
    360 	size_t ie_len;
    361 	struct sta_info *sta = NULL;
    362 	size_t i, resp_len;
    363 	int noack;
    364 	enum ssid_match_result res;
    365 
    366 	ie = mgmt->u.probe_req.variable;
    367 	if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.probe_req))
    368 		return;
    369 	ie_len = len - (IEEE80211_HDRLEN + sizeof(mgmt->u.probe_req));
    370 
    371 	for (i = 0; hapd->probereq_cb && i < hapd->num_probereq_cb; i++)
    372 		if (hapd->probereq_cb[i].cb(hapd->probereq_cb[i].ctx,
    373 					    mgmt->sa, mgmt->da, mgmt->bssid,
    374 					    ie, ie_len, ssi_signal) > 0)
    375 			return;
    376 
    377 	if (!hapd->iconf->send_probe_response)
    378 		return;
    379 
    380 	if (ieee802_11_parse_elems(ie, ie_len, &elems, 0) == ParseFailed) {
    381 		wpa_printf(MSG_DEBUG, "Could not parse ProbeReq from " MACSTR,
    382 			   MAC2STR(mgmt->sa));
    383 		return;
    384 	}
    385 
    386 	if ((!elems.ssid || !elems.supp_rates)) {
    387 		wpa_printf(MSG_DEBUG, "STA " MACSTR " sent probe request "
    388 			   "without SSID or supported rates element",
    389 			   MAC2STR(mgmt->sa));
    390 		return;
    391 	}
    392 
    393 #ifdef CONFIG_P2P
    394 	if (hapd->p2p && elems.wps_ie) {
    395 		struct wpabuf *wps;
    396 		wps = ieee802_11_vendor_ie_concat(ie, ie_len, WPS_DEV_OUI_WFA);
    397 		if (wps && !p2p_group_match_dev_type(hapd->p2p_group, wps)) {
    398 			wpa_printf(MSG_MSGDUMP, "P2P: Ignore Probe Request "
    399 				   "due to mismatch with Requested Device "
    400 				   "Type");
    401 			wpabuf_free(wps);
    402 			return;
    403 		}
    404 		wpabuf_free(wps);
    405 	}
    406 
    407 	if (hapd->p2p && elems.p2p) {
    408 		struct wpabuf *p2p;
    409 		p2p = ieee802_11_vendor_ie_concat(ie, ie_len, P2P_IE_VENDOR_TYPE);
    410 		if (p2p && !p2p_group_match_dev_id(hapd->p2p_group, p2p)) {
    411 			wpa_printf(MSG_MSGDUMP, "P2P: Ignore Probe Request "
    412 				   "due to mismatch with Device ID");
    413 			wpabuf_free(p2p);
    414 			return;
    415 		}
    416 		wpabuf_free(p2p);
    417 	}
    418 #endif /* CONFIG_P2P */
    419 
    420 	if (hapd->conf->ignore_broadcast_ssid && elems.ssid_len == 0 &&
    421 	    elems.ssid_list_len == 0) {
    422 		wpa_printf(MSG_MSGDUMP, "Probe Request from " MACSTR " for "
    423 			   "broadcast SSID ignored", MAC2STR(mgmt->sa));
    424 		return;
    425 	}
    426 
    427 	sta = ap_get_sta(hapd, mgmt->sa);
    428 
    429 #ifdef CONFIG_P2P
    430 	if ((hapd->conf->p2p & P2P_GROUP_OWNER) &&
    431 	    elems.ssid_len == P2P_WILDCARD_SSID_LEN &&
    432 	    os_memcmp(elems.ssid, P2P_WILDCARD_SSID,
    433 		      P2P_WILDCARD_SSID_LEN) == 0) {
    434 		/* Process P2P Wildcard SSID like Wildcard SSID */
    435 		elems.ssid_len = 0;
    436 	}
    437 #endif /* CONFIG_P2P */
    438 
    439 	res = ssid_match(hapd, elems.ssid, elems.ssid_len,
    440 			 elems.ssid_list, elems.ssid_list_len);
    441 	if (res != NO_SSID_MATCH) {
    442 		if (sta)
    443 			sta->ssid_probe = &hapd->conf->ssid;
    444 	} else {
    445 		if (!(mgmt->da[0] & 0x01)) {
    446 			char ssid_txt[33];
    447 			ieee802_11_print_ssid(ssid_txt, elems.ssid,
    448 					      elems.ssid_len);
    449 			wpa_printf(MSG_MSGDUMP, "Probe Request from " MACSTR
    450 				   " for foreign SSID '%s' (DA " MACSTR ")%s",
    451 				   MAC2STR(mgmt->sa), ssid_txt,
    452 				   MAC2STR(mgmt->da),
    453 				   elems.ssid_list ? " (SSID list)" : "");
    454 		}
    455 		return;
    456 	}
    457 
    458 #ifdef CONFIG_INTERWORKING
    459 	if (elems.interworking && elems.interworking_len >= 1) {
    460 		u8 ant = elems.interworking[0] & 0x0f;
    461 		if (ant != INTERWORKING_ANT_WILDCARD &&
    462 		    ant != hapd->conf->access_network_type) {
    463 			wpa_printf(MSG_MSGDUMP, "Probe Request from " MACSTR
    464 				   " for mismatching ANT %u ignored",
    465 				   MAC2STR(mgmt->sa), ant);
    466 			return;
    467 		}
    468 	}
    469 
    470 	if (elems.interworking &&
    471 	    (elems.interworking_len == 7 || elems.interworking_len == 9)) {
    472 		const u8 *hessid;
    473 		if (elems.interworking_len == 7)
    474 			hessid = elems.interworking + 1;
    475 		else
    476 			hessid = elems.interworking + 1 + 2;
    477 		if (!is_broadcast_ether_addr(hessid) &&
    478 		    os_memcmp(hessid, hapd->conf->hessid, ETH_ALEN) != 0) {
    479 			wpa_printf(MSG_MSGDUMP, "Probe Request from " MACSTR
    480 				   " for mismatching HESSID " MACSTR
    481 				   " ignored",
    482 				   MAC2STR(mgmt->sa), MAC2STR(hessid));
    483 			return;
    484 		}
    485 	}
    486 #endif /* CONFIG_INTERWORKING */
    487 
    488 #ifdef CONFIG_P2P
    489 	if ((hapd->conf->p2p & P2P_GROUP_OWNER) &&
    490 	    supp_rates_11b_only(&elems)) {
    491 		/* Indicates support for 11b rates only */
    492 		wpa_printf(MSG_EXCESSIVE, "P2P: Ignore Probe Request from "
    493 			   MACSTR " with only 802.11b rates",
    494 			   MAC2STR(mgmt->sa));
    495 		return;
    496 	}
    497 #endif /* CONFIG_P2P */
    498 
    499 	/* TODO: verify that supp_rates contains at least one matching rate
    500 	 * with AP configuration */
    501 
    502 #ifdef CONFIG_TESTING_OPTIONS
    503 	if (hapd->iconf->ignore_probe_probability > 0.0d &&
    504 	    drand48() < hapd->iconf->ignore_probe_probability) {
    505 		wpa_printf(MSG_INFO,
    506 			   "TESTING: ignoring probe request from " MACSTR,
    507 			   MAC2STR(mgmt->sa));
    508 		return;
    509 	}
    510 #endif /* CONFIG_TESTING_OPTIONS */
    511 
    512 	resp = hostapd_gen_probe_resp(hapd, sta, mgmt, elems.p2p != NULL,
    513 				      &resp_len);
    514 	if (resp == NULL)
    515 		return;
    516 
    517 	/*
    518 	 * If this is a broadcast probe request, apply no ack policy to avoid
    519 	 * excessive retries.
    520 	 */
    521 	noack = !!(res == WILDCARD_SSID_MATCH &&
    522 		   is_broadcast_ether_addr(mgmt->da));
    523 
    524 	if (hostapd_drv_send_mlme(hapd, resp, resp_len, noack) < 0)
    525 		perror("handle_probe_req: send");
    526 
    527 	os_free(resp);
    528 
    529 	wpa_printf(MSG_EXCESSIVE, "STA " MACSTR " sent probe request for %s "
    530 		   "SSID", MAC2STR(mgmt->sa),
    531 		   elems.ssid_len == 0 ? "broadcast" : "our");
    532 }
    533 
    534 
    535 static u8 * hostapd_probe_resp_offloads(struct hostapd_data *hapd,
    536 					size_t *resp_len)
    537 {
    538 	/* check probe response offloading caps and print warnings */
    539 	if (!(hapd->iface->drv_flags & WPA_DRIVER_FLAGS_PROBE_RESP_OFFLOAD))
    540 		return NULL;
    541 
    542 #ifdef CONFIG_WPS
    543 	if (hapd->conf->wps_state && hapd->wps_probe_resp_ie &&
    544 	    (!(hapd->iface->probe_resp_offloads &
    545 	       (WPA_DRIVER_PROBE_RESP_OFFLOAD_WPS |
    546 		WPA_DRIVER_PROBE_RESP_OFFLOAD_WPS2))))
    547 		wpa_printf(MSG_WARNING, "Device is trying to offload WPS "
    548 			   "Probe Response while not supporting this");
    549 #endif /* CONFIG_WPS */
    550 
    551 #ifdef CONFIG_P2P
    552 	if ((hapd->conf->p2p & P2P_ENABLED) && hapd->p2p_probe_resp_ie &&
    553 	    !(hapd->iface->probe_resp_offloads &
    554 	      WPA_DRIVER_PROBE_RESP_OFFLOAD_P2P))
    555 		wpa_printf(MSG_WARNING, "Device is trying to offload P2P "
    556 			   "Probe Response while not supporting this");
    557 #endif  /* CONFIG_P2P */
    558 
    559 	if (hapd->conf->interworking &&
    560 	    !(hapd->iface->probe_resp_offloads &
    561 	      WPA_DRIVER_PROBE_RESP_OFFLOAD_INTERWORKING))
    562 		wpa_printf(MSG_WARNING, "Device is trying to offload "
    563 			   "Interworking Probe Response while not supporting "
    564 			   "this");
    565 
    566 	/* Generate a Probe Response template for the non-P2P case */
    567 	return hostapd_gen_probe_resp(hapd, NULL, NULL, 0, resp_len);
    568 }
    569 
    570 #endif /* NEED_AP_MLME */
    571 
    572 
    573 void ieee802_11_set_beacon(struct hostapd_data *hapd)
    574 {
    575 	struct ieee80211_mgmt *head = NULL;
    576 	u8 *tail = NULL;
    577 	size_t head_len = 0, tail_len = 0;
    578 	u8 *resp = NULL;
    579 	size_t resp_len = 0;
    580 	struct wpa_driver_ap_params params;
    581 	struct wpabuf *beacon, *proberesp, *assocresp;
    582 #ifdef NEED_AP_MLME
    583 	u16 capab_info;
    584 	u8 *pos, *tailpos;
    585 #endif /* NEED_AP_MLME */
    586 
    587 	hapd->beacon_set_done = 1;
    588 
    589 #ifdef NEED_AP_MLME
    590 
    591 #define BEACON_HEAD_BUF_SIZE 256
    592 #define BEACON_TAIL_BUF_SIZE 512
    593 	head = os_zalloc(BEACON_HEAD_BUF_SIZE);
    594 	tail_len = BEACON_TAIL_BUF_SIZE;
    595 #ifdef CONFIG_WPS
    596 	if (hapd->conf->wps_state && hapd->wps_beacon_ie)
    597 		tail_len += wpabuf_len(hapd->wps_beacon_ie);
    598 #endif /* CONFIG_WPS */
    599 #ifdef CONFIG_P2P
    600 	if (hapd->p2p_beacon_ie)
    601 		tail_len += wpabuf_len(hapd->p2p_beacon_ie);
    602 #endif /* CONFIG_P2P */
    603 	if (hapd->conf->vendor_elements)
    604 		tail_len += wpabuf_len(hapd->conf->vendor_elements);
    605 	tailpos = tail = os_malloc(tail_len);
    606 	if (head == NULL || tail == NULL) {
    607 		wpa_printf(MSG_ERROR, "Failed to set beacon data");
    608 		os_free(head);
    609 		os_free(tail);
    610 		return;
    611 	}
    612 
    613 	head->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
    614 					   WLAN_FC_STYPE_BEACON);
    615 	head->duration = host_to_le16(0);
    616 	os_memset(head->da, 0xff, ETH_ALEN);
    617 
    618 	os_memcpy(head->sa, hapd->own_addr, ETH_ALEN);
    619 	os_memcpy(head->bssid, hapd->own_addr, ETH_ALEN);
    620 	head->u.beacon.beacon_int =
    621 		host_to_le16(hapd->iconf->beacon_int);
    622 
    623 	/* hardware or low-level driver will setup seq_ctrl and timestamp */
    624 	capab_info = hostapd_own_capab_info(hapd, NULL, 0);
    625 	head->u.beacon.capab_info = host_to_le16(capab_info);
    626 	pos = &head->u.beacon.variable[0];
    627 
    628 	/* SSID */
    629 	*pos++ = WLAN_EID_SSID;
    630 	if (hapd->conf->ignore_broadcast_ssid == 2) {
    631 		/* clear the data, but keep the correct length of the SSID */
    632 		*pos++ = hapd->conf->ssid.ssid_len;
    633 		os_memset(pos, 0, hapd->conf->ssid.ssid_len);
    634 		pos += hapd->conf->ssid.ssid_len;
    635 	} else if (hapd->conf->ignore_broadcast_ssid) {
    636 		*pos++ = 0; /* empty SSID */
    637 	} else {
    638 		*pos++ = hapd->conf->ssid.ssid_len;
    639 		os_memcpy(pos, hapd->conf->ssid.ssid,
    640 			  hapd->conf->ssid.ssid_len);
    641 		pos += hapd->conf->ssid.ssid_len;
    642 	}
    643 
    644 	/* Supported rates */
    645 	pos = hostapd_eid_supp_rates(hapd, pos);
    646 
    647 	/* DS Params */
    648 	pos = hostapd_eid_ds_params(hapd, pos);
    649 
    650 	head_len = pos - (u8 *) head;
    651 
    652 	tailpos = hostapd_eid_country(hapd, tailpos,
    653 				      tail + BEACON_TAIL_BUF_SIZE - tailpos);
    654 
    655 	/* ERP Information element */
    656 	tailpos = hostapd_eid_erp_info(hapd, tailpos);
    657 
    658 	/* Extended supported rates */
    659 	tailpos = hostapd_eid_ext_supp_rates(hapd, tailpos);
    660 
    661 	/* RSN, MDIE, WPA */
    662 	tailpos = hostapd_eid_wpa(hapd, tailpos, tail + BEACON_TAIL_BUF_SIZE -
    663 				  tailpos);
    664 
    665 #ifdef CONFIG_IEEE80211N
    666 	tailpos = hostapd_eid_ht_capabilities(hapd, tailpos);
    667 	tailpos = hostapd_eid_ht_operation(hapd, tailpos);
    668 #endif /* CONFIG_IEEE80211N */
    669 
    670 	tailpos = hostapd_eid_ext_capab(hapd, tailpos);
    671 
    672 	/*
    673 	 * TODO: Time Advertisement element should only be included in some
    674 	 * DTIM Beacon frames.
    675 	 */
    676 	tailpos = hostapd_eid_time_adv(hapd, tailpos);
    677 
    678 	tailpos = hostapd_eid_interworking(hapd, tailpos);
    679 	tailpos = hostapd_eid_adv_proto(hapd, tailpos);
    680 	tailpos = hostapd_eid_roaming_consortium(hapd, tailpos);
    681 
    682 #ifdef CONFIG_IEEE80211AC
    683 	tailpos = hostapd_eid_vht_capabilities(hapd, tailpos);
    684 	tailpos = hostapd_eid_vht_operation(hapd, tailpos);
    685 #endif /* CONFIG_IEEE80211AC */
    686 
    687 	/* Wi-Fi Alliance WMM */
    688 	tailpos = hostapd_eid_wmm(hapd, tailpos);
    689 
    690 #ifdef CONFIG_WPS
    691 	if (hapd->conf->wps_state && hapd->wps_beacon_ie) {
    692 		os_memcpy(tailpos, wpabuf_head(hapd->wps_beacon_ie),
    693 			  wpabuf_len(hapd->wps_beacon_ie));
    694 		tailpos += wpabuf_len(hapd->wps_beacon_ie);
    695 	}
    696 #endif /* CONFIG_WPS */
    697 
    698 #ifdef CONFIG_P2P
    699 	if ((hapd->conf->p2p & P2P_ENABLED) && hapd->p2p_beacon_ie) {
    700 		os_memcpy(tailpos, wpabuf_head(hapd->p2p_beacon_ie),
    701 			  wpabuf_len(hapd->p2p_beacon_ie));
    702 		tailpos += wpabuf_len(hapd->p2p_beacon_ie);
    703 	}
    704 #endif /* CONFIG_P2P */
    705 #ifdef CONFIG_P2P_MANAGER
    706 	if ((hapd->conf->p2p & (P2P_MANAGE | P2P_ENABLED | P2P_GROUP_OWNER)) ==
    707 	    P2P_MANAGE)
    708 		tailpos = hostapd_eid_p2p_manage(hapd, tailpos);
    709 #endif /* CONFIG_P2P_MANAGER */
    710 
    711 #ifdef CONFIG_HS20
    712 	tailpos = hostapd_eid_hs20_indication(hapd, tailpos);
    713 #endif /* CONFIG_HS20 */
    714 
    715 	if (hapd->conf->vendor_elements) {
    716 		os_memcpy(tailpos, wpabuf_head(hapd->conf->vendor_elements),
    717 			  wpabuf_len(hapd->conf->vendor_elements));
    718 		tailpos += wpabuf_len(hapd->conf->vendor_elements);
    719 	}
    720 
    721 	tail_len = tailpos > tail ? tailpos - tail : 0;
    722 
    723 	resp = hostapd_probe_resp_offloads(hapd, &resp_len);
    724 #endif /* NEED_AP_MLME */
    725 
    726 	os_memset(&params, 0, sizeof(params));
    727 	params.head = (u8 *) head;
    728 	params.head_len = head_len;
    729 	params.tail = tail;
    730 	params.tail_len = tail_len;
    731 	params.proberesp = resp;
    732 	params.proberesp_len = resp_len;
    733 	params.dtim_period = hapd->conf->dtim_period;
    734 	params.beacon_int = hapd->iconf->beacon_int;
    735 	params.basic_rates = hapd->iface->basic_rates;
    736 	params.ssid = hapd->conf->ssid.ssid;
    737 	params.ssid_len = hapd->conf->ssid.ssid_len;
    738 	params.pairwise_ciphers = hapd->conf->rsn_pairwise ?
    739 		hapd->conf->rsn_pairwise : hapd->conf->wpa_pairwise;
    740 	params.group_cipher = hapd->conf->wpa_group;
    741 	params.key_mgmt_suites = hapd->conf->wpa_key_mgmt;
    742 	params.auth_algs = hapd->conf->auth_algs;
    743 	params.wpa_version = hapd->conf->wpa;
    744 	params.privacy = hapd->conf->ssid.wep.keys_set || hapd->conf->wpa ||
    745 		(hapd->conf->ieee802_1x &&
    746 		 (hapd->conf->default_wep_key_len ||
    747 		  hapd->conf->individual_wep_key_len));
    748 	switch (hapd->conf->ignore_broadcast_ssid) {
    749 	case 0:
    750 		params.hide_ssid = NO_SSID_HIDING;
    751 		break;
    752 	case 1:
    753 		params.hide_ssid = HIDDEN_SSID_ZERO_LEN;
    754 		break;
    755 	case 2:
    756 		params.hide_ssid = HIDDEN_SSID_ZERO_CONTENTS;
    757 		break;
    758 	}
    759 	hostapd_build_ap_extra_ies(hapd, &beacon, &proberesp, &assocresp);
    760 	params.beacon_ies = beacon;
    761 	params.proberesp_ies = proberesp;
    762 	params.assocresp_ies = assocresp;
    763 	params.isolate = hapd->conf->isolate;
    764 #ifdef NEED_AP_MLME
    765 	params.cts_protect = !!(ieee802_11_erp_info(hapd) &
    766 				ERP_INFO_USE_PROTECTION);
    767 	params.preamble = hapd->iface->num_sta_no_short_preamble == 0 &&
    768 		hapd->iconf->preamble == SHORT_PREAMBLE;
    769 	if (hapd->iface->current_mode &&
    770 	    hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G)
    771 		params.short_slot_time =
    772 			hapd->iface->num_sta_no_short_slot_time > 0 ? 0 : 1;
    773 	else
    774 		params.short_slot_time = -1;
    775 	if (!hapd->iconf->ieee80211n || hapd->conf->disable_11n)
    776 		params.ht_opmode = -1;
    777 	else
    778 		params.ht_opmode = hapd->iface->ht_op_mode;
    779 #endif /* NEED_AP_MLME */
    780 	params.interworking = hapd->conf->interworking;
    781 	if (hapd->conf->interworking &&
    782 	    !is_zero_ether_addr(hapd->conf->hessid))
    783 		params.hessid = hapd->conf->hessid;
    784 	params.access_network_type = hapd->conf->access_network_type;
    785 	params.ap_max_inactivity = hapd->conf->ap_max_inactivity;
    786 #ifdef CONFIG_HS20
    787 	params.disable_dgaf = hapd->conf->disable_dgaf;
    788 #endif /* CONFIG_HS20 */
    789 	if (hostapd_drv_set_ap(hapd, &params))
    790 		wpa_printf(MSG_ERROR, "Failed to set beacon parameters");
    791 	hostapd_free_ap_extra_ies(hapd, beacon, proberesp, assocresp);
    792 
    793 	os_free(tail);
    794 	os_free(head);
    795 	os_free(resp);
    796 }
    797 
    798 
    799 void ieee802_11_set_beacons(struct hostapd_iface *iface)
    800 {
    801 	size_t i;
    802 	for (i = 0; i < iface->num_bss; i++)
    803 		ieee802_11_set_beacon(iface->bss[i]);
    804 }
    805 
    806 
    807 /* only update beacons if started */
    808 void ieee802_11_update_beacons(struct hostapd_iface *iface)
    809 {
    810 	size_t i;
    811 	for (i = 0; i < iface->num_bss; i++)
    812 		if (iface->bss[i]->beacon_set_done)
    813 			ieee802_11_set_beacon(iface->bss[i]);
    814 }
    815 
    816 #endif /* CONFIG_NATIVE_WINDOWS */
    817