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"
30 #include "ieee802_11h.h"
33 static u8 ieee802_11_erp_info(struct hostapd_data *hapd)
37 if (hapd->iface->current_mode == NULL ||
38 hapd->iface->current_mode->mode != HOSTAPD_MODE_IEEE80211G)
41 switch (hapd->iconf->cts_protection_type) {
42 case CTS_PROTECTION_FORCE_ENABLED:
43 erp |= ERP_INFO_NON_ERP_PRESENT | ERP_INFO_USE_PROTECTION;
45 case CTS_PROTECTION_FORCE_DISABLED:
48 case CTS_PROTECTION_AUTOMATIC:
49 if (hapd->iface->olbc)
50 erp |= ERP_INFO_USE_PROTECTION;
52 case CTS_PROTECTION_AUTOMATIC_NO_OLBC:
53 if (hapd->iface->num_sta_non_erp > 0) {
54 erp |= ERP_INFO_NON_ERP_PRESENT |
55 ERP_INFO_USE_PROTECTION;
59 if (hapd->iface->num_sta_no_short_preamble > 0)
60 erp |= ERP_INFO_BARKER_PREAMBLE_MODE;
66 static u8 * hostapd_eid_ds_params(struct hostapd_data *hapd, u8 *eid)
68 *eid++ = WLAN_EID_DS_PARAMS;
70 *eid++ = hapd->iconf->channel;
75 static u8 * hostapd_eid_erp_info(struct hostapd_data *hapd, u8 *eid)
77 if (hapd->iface->current_mode == NULL ||
78 hapd->iface->current_mode->mode != HOSTAPD_MODE_IEEE80211G)
81 /* Set NonERP_present and use_protection bits if there
82 * are any associated NonERP stations. */
83 /* TODO: use_protection bit can be set to zero even if
84 * there are NonERP stations present. This optimization
85 * might be useful if NonERP stations are "quiet".
86 * See 802.11g/D6 E-1 for recommended practice.
87 * In addition, Non ERP present might be set, if AP detects Non ERP
88 * operation on other APs. */
90 /* Add ERP Information element */
91 *eid++ = WLAN_EID_ERP_INFO;
93 *eid++ = ieee802_11_erp_info(hapd);
99 static u8 * hostapd_eid_country(struct hostapd_data *hapd, u8 *eid,
104 if ((!hapd->iconf->ieee80211d && !hapd->iface->dfs_enable) ||
108 *pos++ = WLAN_EID_COUNTRY;
109 pos++; /* length will be set later */
110 os_memcpy(pos, hapd->iconf->country, 3); /* e.g., 'US ' */
114 *pos++ = 0; /* pad for 16-bit alignment */
116 eid[1] = (pos - eid) - 2;
122 static u8 * hostapd_eid_power_constraint(struct hostapd_data *hapd, u8 *eid)
125 if (!hapd->iface->dfs_enable)
127 *eid++ = WLAN_EID_PWR_CONSTRAINT;
129 *eid++ = hapd->iface->pwr_const;
134 static u8 * hostapd_eid_tpc_report(struct hostapd_data *hapd, u8 *eid)
137 if (!hapd->iface->dfs_enable)
139 *eid++ = WLAN_EID_TPC_REPORT;
141 *eid++ = hapd->iface->tx_power; /* TX POWER */
142 *eid++ = 0; /* Link Margin */
146 static u8 * hostapd_eid_channel_switch(struct hostapd_data *hapd, u8 *eid)
149 if (!hapd->iface->dfs_enable || !hapd->iface->channel_switch)
151 *eid++ = WLAN_EID_CHANNEL_SWITCH;
153 *eid++ = CHAN_SWITCH_MODE_QUIET;
154 *eid++ = hapd->iface->channel_switch; /* New channel */
155 /* 0 - very soon; 1 - before next TBTT; num - after num beacons */
161 static u8 * hostapd_eid_wpa(struct hostapd_data *hapd, u8 *eid, size_t len,
162 struct sta_info *sta)
167 ie = wpa_auth_get_wpa_ie(hapd->wpa_auth, &ielen);
168 if (ie == NULL || ielen > len)
171 os_memcpy(eid, ie, ielen);
176 void handle_probe_req(struct hostapd_data *hapd, struct ieee80211_mgmt *mgmt,
179 struct ieee80211_mgmt *resp;
180 struct ieee802_11_elems elems;
183 size_t ssid_len, ie_len;
184 struct sta_info *sta = NULL;
186 ie = mgmt->u.probe_req.variable;
187 ie_len = len - (IEEE80211_HDRLEN + sizeof(mgmt->u.probe_req));
189 if (!hapd->iconf->send_probe_response)
192 if (ieee802_11_parse_elems(ie, ie_len, &elems, 0) == ParseFailed) {
193 wpa_printf(MSG_DEBUG, "Could not parse ProbeReq from " MACSTR,
201 if ((!elems.ssid || !elems.supp_rates)) {
202 wpa_printf(MSG_DEBUG, "STA " MACSTR " sent probe request "
203 "without SSID or supported rates element",
208 if (hapd->conf->ignore_broadcast_ssid && elems.ssid_len == 0) {
209 wpa_printf(MSG_MSGDUMP, "Probe Request from " MACSTR " for "
210 "broadcast SSID ignored", MAC2STR(mgmt->sa));
214 sta = ap_get_sta(hapd, mgmt->sa);
216 if (elems.ssid_len == 0 ||
217 (elems.ssid_len == hapd->conf->ssid.ssid_len &&
218 os_memcmp(elems.ssid, hapd->conf->ssid.ssid, elems.ssid_len) ==
220 ssid = hapd->conf->ssid.ssid;
221 ssid_len = hapd->conf->ssid.ssid_len;
223 sta->ssid_probe = &hapd->conf->ssid;
227 if (!(mgmt->da[0] & 0x01)) {
229 ieee802_11_print_ssid(ssid_txt, elems.ssid,
231 wpa_printf(MSG_MSGDUMP, "Probe Request from " MACSTR
232 " for foreign SSID '%s'",
233 MAC2STR(mgmt->sa), ssid_txt);
238 /* TODO: verify that supp_rates contains at least one matching rate
239 * with AP configuration */
240 #define MAX_PROBERESP_LEN 768
241 resp = os_zalloc(MAX_PROBERESP_LEN);
244 epos = ((u8 *) resp) + MAX_PROBERESP_LEN;
246 resp->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
247 WLAN_FC_STYPE_PROBE_RESP);
248 os_memcpy(resp->da, mgmt->sa, ETH_ALEN);
249 os_memcpy(resp->sa, hapd->own_addr, ETH_ALEN);
251 os_memcpy(resp->bssid, hapd->own_addr, ETH_ALEN);
252 resp->u.probe_resp.beacon_int =
253 host_to_le16(hapd->iconf->beacon_int);
255 /* hardware or low-level driver will setup seq_ctrl and timestamp */
256 resp->u.probe_resp.capab_info =
257 host_to_le16(hostapd_own_capab_info(hapd, sta, 1));
259 pos = resp->u.probe_resp.variable;
260 *pos++ = WLAN_EID_SSID;
262 os_memcpy(pos, ssid, ssid_len);
265 /* Supported rates */
266 pos = hostapd_eid_supp_rates(hapd, pos);
269 pos = hostapd_eid_ds_params(hapd, pos);
271 pos = hostapd_eid_country(hapd, pos, epos - pos);
273 pos = hostapd_eid_power_constraint(hapd, pos);
274 pos = hostapd_eid_tpc_report(hapd, pos);
276 /* ERP Information element */
277 pos = hostapd_eid_erp_info(hapd, pos);
279 /* Extended supported rates */
280 pos = hostapd_eid_ext_supp_rates(hapd, pos);
282 pos = hostapd_eid_wpa(hapd, pos, epos - pos, sta);
284 /* Wi-Fi Wireless Multimedia Extensions */
285 pos = hostapd_eid_wme(hapd, pos);
287 pos = hostapd_eid_ht_capabilities_info(hapd, pos);
288 pos = hostapd_eid_ht_operation(hapd, pos);
290 if (hostapd_send_mgmt_frame(hapd, resp, pos - (u8 *) resp, 0) < 0)
291 perror("handle_probe_req: send");
295 wpa_printf(MSG_MSGDUMP, "STA " MACSTR " sent probe request for %s "
296 "SSID", MAC2STR(mgmt->sa),
297 elems.ssid_len == 0 ? "broadcast" : "our");
301 void ieee802_11_set_beacon(struct hostapd_data *hapd)
303 struct ieee80211_mgmt *head;
304 u8 *pos, *tail, *tailpos;
307 size_t head_len, tail_len;
308 int cts_protection = ((ieee802_11_erp_info(hapd) &
309 ERP_INFO_USE_PROTECTION) ? 1 : 0);
311 #define BEACON_HEAD_BUF_SIZE 256
312 #define BEACON_TAIL_BUF_SIZE 512
313 head = os_zalloc(BEACON_HEAD_BUF_SIZE);
314 tailpos = tail = os_malloc(BEACON_TAIL_BUF_SIZE);
315 if (head == NULL || tail == NULL) {
316 wpa_printf(MSG_ERROR, "Failed to set beacon data");
322 head->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
323 WLAN_FC_STYPE_BEACON);
324 head->duration = host_to_le16(0);
325 os_memset(head->da, 0xff, ETH_ALEN);
327 os_memcpy(head->sa, hapd->own_addr, ETH_ALEN);
328 os_memcpy(head->bssid, hapd->own_addr, ETH_ALEN);
329 head->u.beacon.beacon_int =
330 host_to_le16(hapd->iconf->beacon_int);
332 /* hardware or low-level driver will setup seq_ctrl and timestamp */
333 capab_info = hostapd_own_capab_info(hapd, NULL, 0);
334 head->u.beacon.capab_info = host_to_le16(capab_info);
335 pos = &head->u.beacon.variable[0];
338 *pos++ = WLAN_EID_SSID;
339 if (hapd->conf->ignore_broadcast_ssid == 2) {
340 /* clear the data, but keep the correct length of the SSID */
341 *pos++ = hapd->conf->ssid.ssid_len;
342 os_memset(pos, 0, hapd->conf->ssid.ssid_len);
343 pos += hapd->conf->ssid.ssid_len;
344 } else if (hapd->conf->ignore_broadcast_ssid) {
345 *pos++ = 0; /* empty SSID */
347 *pos++ = hapd->conf->ssid.ssid_len;
348 os_memcpy(pos, hapd->conf->ssid.ssid,
349 hapd->conf->ssid.ssid_len);
350 pos += hapd->conf->ssid.ssid_len;
353 /* Supported rates */
354 pos = hostapd_eid_supp_rates(hapd, pos);
357 pos = hostapd_eid_ds_params(hapd, pos);
359 head_len = pos - (u8 *) head;
361 tailpos = hostapd_eid_country(hapd, tailpos,
362 tail + BEACON_TAIL_BUF_SIZE - tailpos);
364 tailpos = hostapd_eid_power_constraint(hapd, tailpos);
365 tailpos = hostapd_eid_channel_switch(hapd, tailpos);
366 tailpos = hostapd_eid_tpc_report(hapd, tailpos);
368 /* ERP Information element */
369 tailpos = hostapd_eid_erp_info(hapd, tailpos);
371 /* Extended supported rates */
372 tailpos = hostapd_eid_ext_supp_rates(hapd, tailpos);
374 tailpos = hostapd_eid_wpa(hapd, tailpos, tail + BEACON_TAIL_BUF_SIZE -
377 /* Wi-Fi Wireless Multimedia Extensions */
378 tailpos = hostapd_eid_wme(hapd, tailpos);
380 #ifdef CONFIG_IEEE80211N
381 if (hapd->iconf->ieee80211n) {
384 tailpos = hostapd_eid_ht_capabilities_info(hapd, tailpos);
385 if (hostapd_set_ht_capability(hapd->conf->iface, hapd,
387 wpa_printf(MSG_ERROR, "Could not set HT capabilities "
388 "for kernel driver");
392 tailpos = hostapd_eid_ht_operation(hapd, tailpos);
393 if (hostapd_set_ht_operation(hapd->conf->iface, hapd,
395 wpa_printf(MSG_ERROR, "Could not set HT operation for "
398 #endif /* CONFIG_IEEE80211N */
400 tail_len = tailpos > tail ? tailpos - tail : 0;
402 if (hostapd_set_beacon(hapd->conf->iface, hapd, (u8 *) head, head_len,
404 wpa_printf(MSG_ERROR, "Failed to set beacon head/tail");
409 if (hostapd_set_cts_protect(hapd, cts_protection))
410 wpa_printf(MSG_ERROR, "Failed to set CTS protect in kernel "
413 if (hapd->iface->current_mode &&
414 hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G &&
415 hostapd_set_short_slot_time(hapd,
416 hapd->iface->num_sta_no_short_slot_time
418 wpa_printf(MSG_ERROR, "Failed to set Short Slot Time option "
421 if (hapd->iface->num_sta_no_short_preamble == 0 &&
422 hapd->iconf->preamble == SHORT_PREAMBLE)
423 preamble = SHORT_PREAMBLE;
425 preamble = LONG_PREAMBLE;
426 if (hostapd_set_preamble(hapd, preamble))
427 wpa_printf(MSG_ERROR, "Could not set preamble for kernel "
432 void ieee802_11_set_beacons(struct hostapd_iface *iface)
435 for (i = 0; i < iface->num_bss; i++)
436 ieee802_11_set_beacon(iface->bss[i]);
439 #endif /* CONFIG_NATIVE_WINDOWS */