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, Jouni Malinen <j@w1.fi>
6 * Copyright (c) 2007-2008, Intel Corporation
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
12 * Alternatively, this software may be distributed under the terms of BSD
15 * See README and COPYING for more details.
20 #ifndef CONFIG_NATIVE_WINDOWS
23 #include "ieee802_11.h"
27 #include "hw_features.h"
32 static u8 ieee802_11_erp_info(struct hostapd_data *hapd)
36 if (hapd->iface->current_mode == NULL ||
37 hapd->iface->current_mode->mode != HOSTAPD_MODE_IEEE80211G)
40 switch (hapd->iconf->cts_protection_type) {
41 case CTS_PROTECTION_FORCE_ENABLED:
42 erp |= ERP_INFO_NON_ERP_PRESENT | ERP_INFO_USE_PROTECTION;
44 case CTS_PROTECTION_FORCE_DISABLED:
47 case CTS_PROTECTION_AUTOMATIC:
48 if (hapd->iface->olbc)
49 erp |= ERP_INFO_USE_PROTECTION;
51 case CTS_PROTECTION_AUTOMATIC_NO_OLBC:
52 if (hapd->iface->num_sta_non_erp > 0) {
53 erp |= ERP_INFO_NON_ERP_PRESENT |
54 ERP_INFO_USE_PROTECTION;
58 if (hapd->iface->num_sta_no_short_preamble > 0)
59 erp |= ERP_INFO_BARKER_PREAMBLE_MODE;
65 static u8 * hostapd_eid_ds_params(struct hostapd_data *hapd, u8 *eid)
67 *eid++ = WLAN_EID_DS_PARAMS;
69 *eid++ = hapd->iconf->channel;
74 static u8 * hostapd_eid_erp_info(struct hostapd_data *hapd, u8 *eid)
76 if (hapd->iface->current_mode == NULL ||
77 hapd->iface->current_mode->mode != HOSTAPD_MODE_IEEE80211G)
80 /* Set NonERP_present and use_protection bits if there
81 * are any associated NonERP stations. */
82 /* TODO: use_protection bit can be set to zero even if
83 * there are NonERP stations present. This optimization
84 * might be useful if NonERP stations are "quiet".
85 * See 802.11g/D6 E-1 for recommended practice.
86 * In addition, Non ERP present might be set, if AP detects Non ERP
87 * operation on other APs. */
89 /* Add ERP Information element */
90 *eid++ = WLAN_EID_ERP_INFO;
92 *eid++ = ieee802_11_erp_info(hapd);
98 static u8 * hostapd_eid_country(struct hostapd_data *hapd, u8 *eid,
103 if (!hapd->iconf->ieee80211d || max_len < 6)
106 *pos++ = WLAN_EID_COUNTRY;
107 pos++; /* length will be set later */
108 os_memcpy(pos, hapd->iconf->country, 3); /* e.g., 'US ' */
112 *pos++ = 0; /* pad for 16-bit alignment */
114 eid[1] = (pos - eid) - 2;
120 static u8 * hostapd_eid_wpa(struct hostapd_data *hapd, u8 *eid, size_t len,
121 struct sta_info *sta)
126 ie = wpa_auth_get_wpa_ie(hapd->wpa_auth, &ielen);
127 if (ie == NULL || ielen > len)
130 os_memcpy(eid, ie, ielen);
135 void handle_probe_req(struct hostapd_data *hapd, struct ieee80211_mgmt *mgmt,
138 struct ieee80211_mgmt *resp;
139 struct ieee802_11_elems elems;
142 size_t ssid_len, ie_len;
143 struct sta_info *sta = NULL;
145 ie = mgmt->u.probe_req.variable;
146 ie_len = len - (IEEE80211_HDRLEN + sizeof(mgmt->u.probe_req));
148 if (!hapd->iconf->send_probe_response)
151 if (ieee802_11_parse_elems(ie, ie_len, &elems, 0) == ParseFailed) {
152 wpa_printf(MSG_DEBUG, "Could not parse ProbeReq from " MACSTR,
160 if ((!elems.ssid || !elems.supp_rates)) {
161 wpa_printf(MSG_DEBUG, "STA " MACSTR " sent probe request "
162 "without SSID or supported rates element",
167 if (hapd->conf->ignore_broadcast_ssid && elems.ssid_len == 0) {
168 wpa_printf(MSG_MSGDUMP, "Probe Request from " MACSTR " for "
169 "broadcast SSID ignored", MAC2STR(mgmt->sa));
173 sta = ap_get_sta(hapd, mgmt->sa);
175 if (elems.ssid_len == 0 ||
176 (elems.ssid_len == hapd->conf->ssid.ssid_len &&
177 os_memcmp(elems.ssid, hapd->conf->ssid.ssid, elems.ssid_len) ==
179 ssid = hapd->conf->ssid.ssid;
180 ssid_len = hapd->conf->ssid.ssid_len;
182 sta->ssid_probe = &hapd->conf->ssid;
186 if (!(mgmt->da[0] & 0x01)) {
188 ieee802_11_print_ssid(ssid_txt, elems.ssid,
190 wpa_printf(MSG_MSGDUMP, "Probe Request from " MACSTR
191 " for foreign SSID '%s'",
192 MAC2STR(mgmt->sa), ssid_txt);
197 /* TODO: verify that supp_rates contains at least one matching rate
198 * with AP configuration */
199 #define MAX_PROBERESP_LEN 768
200 resp = os_zalloc(MAX_PROBERESP_LEN);
203 epos = ((u8 *) resp) + MAX_PROBERESP_LEN;
205 resp->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
206 WLAN_FC_STYPE_PROBE_RESP);
207 os_memcpy(resp->da, mgmt->sa, ETH_ALEN);
208 os_memcpy(resp->sa, hapd->own_addr, ETH_ALEN);
210 os_memcpy(resp->bssid, hapd->own_addr, ETH_ALEN);
211 resp->u.probe_resp.beacon_int =
212 host_to_le16(hapd->iconf->beacon_int);
214 /* hardware or low-level driver will setup seq_ctrl and timestamp */
215 resp->u.probe_resp.capab_info =
216 host_to_le16(hostapd_own_capab_info(hapd, sta, 1));
218 pos = resp->u.probe_resp.variable;
219 *pos++ = WLAN_EID_SSID;
221 os_memcpy(pos, ssid, ssid_len);
224 /* Supported rates */
225 pos = hostapd_eid_supp_rates(hapd, pos);
228 pos = hostapd_eid_ds_params(hapd, pos);
230 pos = hostapd_eid_country(hapd, pos, epos - pos);
232 /* ERP Information element */
233 pos = hostapd_eid_erp_info(hapd, pos);
235 /* Extended supported rates */
236 pos = hostapd_eid_ext_supp_rates(hapd, pos);
238 pos = hostapd_eid_wpa(hapd, pos, epos - pos, sta);
240 /* Wi-Fi Wireless Multimedia Extensions */
241 pos = hostapd_eid_wme(hapd, pos);
243 pos = hostapd_eid_ht_capabilities_info(hapd, pos);
244 pos = hostapd_eid_ht_operation(hapd, pos);
246 if (hostapd_send_mgmt_frame(hapd, resp, pos - (u8 *) resp, 0) < 0)
247 perror("handle_probe_req: send");
251 wpa_printf(MSG_MSGDUMP, "STA " MACSTR " sent probe request for %s "
252 "SSID", MAC2STR(mgmt->sa),
253 elems.ssid_len == 0 ? "broadcast" : "our");
257 void ieee802_11_set_beacon(struct hostapd_data *hapd)
259 struct ieee80211_mgmt *head;
260 u8 *pos, *tail, *tailpos;
263 size_t head_len, tail_len;
264 int cts_protection = ((ieee802_11_erp_info(hapd) &
265 ERP_INFO_USE_PROTECTION) ? 1 : 0);
267 #define BEACON_HEAD_BUF_SIZE 256
268 #define BEACON_TAIL_BUF_SIZE 512
269 head = os_zalloc(BEACON_HEAD_BUF_SIZE);
270 tailpos = tail = os_malloc(BEACON_TAIL_BUF_SIZE);
271 if (head == NULL || tail == NULL) {
272 wpa_printf(MSG_ERROR, "Failed to set beacon data");
278 head->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
279 WLAN_FC_STYPE_BEACON);
280 head->duration = host_to_le16(0);
281 os_memset(head->da, 0xff, ETH_ALEN);
283 os_memcpy(head->sa, hapd->own_addr, ETH_ALEN);
284 os_memcpy(head->bssid, hapd->own_addr, ETH_ALEN);
285 head->u.beacon.beacon_int =
286 host_to_le16(hapd->iconf->beacon_int);
288 /* hardware or low-level driver will setup seq_ctrl and timestamp */
289 capab_info = hostapd_own_capab_info(hapd, NULL, 0);
290 head->u.beacon.capab_info = host_to_le16(capab_info);
291 pos = &head->u.beacon.variable[0];
294 *pos++ = WLAN_EID_SSID;
295 if (hapd->conf->ignore_broadcast_ssid == 2) {
296 /* clear the data, but keep the correct length of the SSID */
297 *pos++ = hapd->conf->ssid.ssid_len;
298 os_memset(pos, 0, hapd->conf->ssid.ssid_len);
299 pos += hapd->conf->ssid.ssid_len;
300 } else if (hapd->conf->ignore_broadcast_ssid) {
301 *pos++ = 0; /* empty SSID */
303 *pos++ = hapd->conf->ssid.ssid_len;
304 os_memcpy(pos, hapd->conf->ssid.ssid,
305 hapd->conf->ssid.ssid_len);
306 pos += hapd->conf->ssid.ssid_len;
309 /* Supported rates */
310 pos = hostapd_eid_supp_rates(hapd, pos);
313 pos = hostapd_eid_ds_params(hapd, pos);
315 head_len = pos - (u8 *) head;
317 tailpos = hostapd_eid_country(hapd, tailpos,
318 tail + BEACON_TAIL_BUF_SIZE - tailpos);
320 /* ERP Information element */
321 tailpos = hostapd_eid_erp_info(hapd, tailpos);
323 /* Extended supported rates */
324 tailpos = hostapd_eid_ext_supp_rates(hapd, tailpos);
326 tailpos = hostapd_eid_wpa(hapd, tailpos, tail + BEACON_TAIL_BUF_SIZE -
329 /* Wi-Fi Wireless Multimedia Extensions */
330 tailpos = hostapd_eid_wme(hapd, tailpos);
332 #ifdef CONFIG_IEEE80211N
333 if (hapd->iconf->ieee80211n) {
336 tailpos = hostapd_eid_ht_capabilities_info(hapd, tailpos);
337 if (hostapd_set_ht_capability(hapd->conf->iface, hapd,
339 wpa_printf(MSG_ERROR, "Could not set HT capabilities "
340 "for kernel driver");
344 tailpos = hostapd_eid_ht_operation(hapd, tailpos);
345 if (hostapd_set_ht_operation(hapd->conf->iface, hapd,
347 wpa_printf(MSG_ERROR, "Could not set HT operation for "
350 #endif /* CONFIG_IEEE80211N */
352 tail_len = tailpos > tail ? tailpos - tail : 0;
354 if (hostapd_set_beacon(hapd->conf->iface, hapd, (u8 *) head, head_len,
356 wpa_printf(MSG_ERROR, "Failed to set beacon head/tail");
361 if (hostapd_set_cts_protect(hapd, cts_protection))
362 wpa_printf(MSG_ERROR, "Failed to set CTS protect in kernel "
365 if (hapd->iface->current_mode &&
366 hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G &&
367 hostapd_set_short_slot_time(hapd,
368 hapd->iface->num_sta_no_short_slot_time
370 wpa_printf(MSG_ERROR, "Failed to set Short Slot Time option "
373 if (hapd->iface->num_sta_no_short_preamble == 0 &&
374 hapd->iconf->preamble == SHORT_PREAMBLE)
375 preamble = SHORT_PREAMBLE;
377 preamble = LONG_PREAMBLE;
378 if (hostapd_set_preamble(hapd, preamble))
379 wpa_printf(MSG_ERROR, "Could not set preamble for kernel "
384 void ieee802_11_set_beacons(struct hostapd_iface *iface)
387 for (i = 0; i < iface->num_bss; i++)
388 ieee802_11_set_beacon(iface->bss[i]);
391 #endif /* CONFIG_NATIVE_WINDOWS */