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-2009, Jouni Malinen <j@w1.fi>
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.
11 * Alternatively, this software may be distributed under the terms of BSD
14 * See README and COPYING for more details.
17 #include "utils/includes.h"
19 #ifndef CONFIG_NATIVE_WINDOWS
21 #include "utils/common.h"
22 #include "common/ieee802_11_defs.h"
23 #include "common/ieee802_11_common.h"
24 #include "drivers/driver.h"
26 #include "ieee802_11.h"
29 #include "ap_config.h"
34 static u8 ieee802_11_erp_info(struct hostapd_data *hapd)
38 if (hapd->iface->current_mode == NULL ||
39 hapd->iface->current_mode->mode != HOSTAPD_MODE_IEEE80211G)
42 switch (hapd->iconf->cts_protection_type) {
43 case CTS_PROTECTION_FORCE_ENABLED:
44 erp |= ERP_INFO_NON_ERP_PRESENT | ERP_INFO_USE_PROTECTION;
46 case CTS_PROTECTION_FORCE_DISABLED:
49 case CTS_PROTECTION_AUTOMATIC:
50 if (hapd->iface->olbc)
51 erp |= ERP_INFO_USE_PROTECTION;
53 case CTS_PROTECTION_AUTOMATIC_NO_OLBC:
54 if (hapd->iface->num_sta_non_erp > 0) {
55 erp |= ERP_INFO_NON_ERP_PRESENT |
56 ERP_INFO_USE_PROTECTION;
60 if (hapd->iface->num_sta_no_short_preamble > 0)
61 erp |= ERP_INFO_BARKER_PREAMBLE_MODE;
67 static u8 * hostapd_eid_ds_params(struct hostapd_data *hapd, u8 *eid)
69 *eid++ = WLAN_EID_DS_PARAMS;
71 *eid++ = hapd->iconf->channel;
76 static u8 * hostapd_eid_erp_info(struct hostapd_data *hapd, u8 *eid)
78 if (hapd->iface->current_mode == NULL ||
79 hapd->iface->current_mode->mode != HOSTAPD_MODE_IEEE80211G)
82 /* Set NonERP_present and use_protection bits if there
83 * are any associated NonERP stations. */
84 /* TODO: use_protection bit can be set to zero even if
85 * there are NonERP stations present. This optimization
86 * might be useful if NonERP stations are "quiet".
87 * See 802.11g/D6 E-1 for recommended practice.
88 * In addition, Non ERP present might be set, if AP detects Non ERP
89 * operation on other APs. */
91 /* Add ERP Information element */
92 *eid++ = WLAN_EID_ERP_INFO;
94 *eid++ = ieee802_11_erp_info(hapd);
100 static u8 * hostapd_eid_country_add(u8 *pos, u8 *end, int chan_spacing,
101 struct hostapd_channel_data *start,
102 struct hostapd_channel_data *prev)
107 /* first channel number */
108 *pos++ = start->chan;
109 /* number of channels */
110 *pos++ = (prev->chan - start->chan) / chan_spacing + 1;
111 /* maximum transmit power level */
112 *pos++ = start->max_tx_power;
118 static u8 * hostapd_eid_country(struct hostapd_data *hapd, u8 *eid,
122 u8 *end = eid + max_len;
124 struct hostapd_hw_modes *mode;
125 struct hostapd_channel_data *start, *prev;
126 int chan_spacing = 1;
128 if (!hapd->iconf->ieee80211d || max_len < 6 ||
129 hapd->iface->current_mode == NULL)
132 *pos++ = WLAN_EID_COUNTRY;
133 pos++; /* length will be set later */
134 os_memcpy(pos, hapd->iconf->country, 3); /* e.g., 'US ' */
137 mode = hapd->iface->current_mode;
138 if (mode->mode == HOSTAPD_MODE_IEEE80211A)
142 for (i = 0; i < mode->num_channels; i++) {
143 struct hostapd_channel_data *chan = &mode->channels[i];
144 if (chan->flag & HOSTAPD_CHAN_DISABLED)
147 prev->chan + chan_spacing == chan->chan &&
148 start->max_tx_power == chan->max_tx_power) {
150 continue; /* can use same entry */
154 pos = hostapd_eid_country_add(pos, end, chan_spacing,
159 /* Start new group */
164 pos = hostapd_eid_country_add(pos, end, chan_spacing,
168 if ((pos - eid) & 1) {
171 *pos++ = 0; /* pad for 16-bit alignment */
174 eid[1] = (pos - eid) - 2;
180 static u8 * hostapd_eid_wpa(struct hostapd_data *hapd, u8 *eid, size_t len,
181 struct sta_info *sta)
186 ie = wpa_auth_get_wpa_ie(hapd->wpa_auth, &ielen);
187 if (ie == NULL || ielen > len)
190 os_memcpy(eid, ie, ielen);
195 void handle_probe_req(struct hostapd_data *hapd,
196 const struct ieee80211_mgmt *mgmt, size_t len)
198 struct ieee80211_mgmt *resp;
199 struct ieee802_11_elems elems;
203 size_t ssid_len, ie_len;
204 struct sta_info *sta = NULL;
208 ie = mgmt->u.probe_req.variable;
209 ie_len = len - (IEEE80211_HDRLEN + sizeof(mgmt->u.probe_req));
211 for (i = 0; hapd->probereq_cb && i < hapd->num_probereq_cb; i++)
212 hapd->probereq_cb[i].cb(hapd->probereq_cb[i].ctx,
213 mgmt->sa, ie, ie_len);
215 if (!hapd->iconf->send_probe_response)
218 if (ieee802_11_parse_elems(ie, ie_len, &elems, 0) == ParseFailed) {
219 wpa_printf(MSG_DEBUG, "Could not parse ProbeReq from " MACSTR,
227 if ((!elems.ssid || !elems.supp_rates)) {
228 wpa_printf(MSG_DEBUG, "STA " MACSTR " sent probe request "
229 "without SSID or supported rates element",
234 if (hapd->conf->ignore_broadcast_ssid && elems.ssid_len == 0) {
235 wpa_printf(MSG_MSGDUMP, "Probe Request from " MACSTR " for "
236 "broadcast SSID ignored", MAC2STR(mgmt->sa));
240 sta = ap_get_sta(hapd, mgmt->sa);
242 if (elems.ssid_len == 0 ||
243 (elems.ssid_len == hapd->conf->ssid.ssid_len &&
244 os_memcmp(elems.ssid, hapd->conf->ssid.ssid, elems.ssid_len) ==
246 ssid = hapd->conf->ssid.ssid;
247 ssid_len = hapd->conf->ssid.ssid_len;
249 sta->ssid_probe = &hapd->conf->ssid;
253 if (!(mgmt->da[0] & 0x01)) {
255 ieee802_11_print_ssid(ssid_txt, elems.ssid,
257 wpa_printf(MSG_MSGDUMP, "Probe Request from " MACSTR
258 " for foreign SSID '%s'",
259 MAC2STR(mgmt->sa), ssid_txt);
264 /* TODO: verify that supp_rates contains at least one matching rate
265 * with AP configuration */
266 #define MAX_PROBERESP_LEN 768
267 buflen = MAX_PROBERESP_LEN;
269 if (hapd->wps_probe_resp_ie)
270 buflen += wpabuf_len(hapd->wps_probe_resp_ie);
271 #endif /* CONFIG_WPS */
272 resp = os_zalloc(buflen);
275 epos = ((u8 *) resp) + MAX_PROBERESP_LEN;
277 resp->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
278 WLAN_FC_STYPE_PROBE_RESP);
279 os_memcpy(resp->da, mgmt->sa, ETH_ALEN);
280 os_memcpy(resp->sa, hapd->own_addr, ETH_ALEN);
282 os_memcpy(resp->bssid, hapd->own_addr, ETH_ALEN);
283 resp->u.probe_resp.beacon_int =
284 host_to_le16(hapd->iconf->beacon_int);
286 /* hardware or low-level driver will setup seq_ctrl and timestamp */
287 resp->u.probe_resp.capab_info =
288 host_to_le16(hostapd_own_capab_info(hapd, sta, 1));
290 pos = resp->u.probe_resp.variable;
291 *pos++ = WLAN_EID_SSID;
293 os_memcpy(pos, ssid, ssid_len);
296 /* Supported rates */
297 pos = hostapd_eid_supp_rates(hapd, pos);
300 pos = hostapd_eid_ds_params(hapd, pos);
302 pos = hostapd_eid_country(hapd, pos, epos - pos);
304 /* ERP Information element */
305 pos = hostapd_eid_erp_info(hapd, pos);
307 /* Extended supported rates */
308 pos = hostapd_eid_ext_supp_rates(hapd, pos);
310 pos = hostapd_eid_wpa(hapd, pos, epos - pos, sta);
312 /* Wi-Fi Alliance WMM */
313 pos = hostapd_eid_wmm(hapd, pos);
315 #ifdef CONFIG_IEEE80211N
316 pos = hostapd_eid_ht_capabilities(hapd, pos);
317 pos = hostapd_eid_ht_operation(hapd, pos);
318 #endif /* CONFIG_IEEE80211N */
321 if (hapd->conf->wps_state && hapd->wps_probe_resp_ie) {
322 os_memcpy(pos, wpabuf_head(hapd->wps_probe_resp_ie),
323 wpabuf_len(hapd->wps_probe_resp_ie));
324 pos += wpabuf_len(hapd->wps_probe_resp_ie);
326 #endif /* CONFIG_WPS */
328 if (hapd->drv.send_mgmt_frame(hapd, resp, pos - (u8 *) resp) < 0)
329 perror("handle_probe_req: send");
333 wpa_printf(MSG_MSGDUMP, "STA " MACSTR " sent probe request for %s "
334 "SSID", MAC2STR(mgmt->sa),
335 elems.ssid_len == 0 ? "broadcast" : "our");
339 void ieee802_11_set_beacon(struct hostapd_data *hapd)
341 struct ieee80211_mgmt *head;
342 u8 *pos, *tail, *tailpos;
344 size_t head_len, tail_len;
346 #define BEACON_HEAD_BUF_SIZE 256
347 #define BEACON_TAIL_BUF_SIZE 512
348 head = os_zalloc(BEACON_HEAD_BUF_SIZE);
349 tail_len = BEACON_TAIL_BUF_SIZE;
351 if (hapd->conf->wps_state && hapd->wps_beacon_ie)
352 tail_len += wpabuf_len(hapd->wps_beacon_ie);
353 #endif /* CONFIG_WPS */
354 tailpos = tail = os_malloc(tail_len);
355 if (head == NULL || tail == NULL) {
356 wpa_printf(MSG_ERROR, "Failed to set beacon data");
362 head->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
363 WLAN_FC_STYPE_BEACON);
364 head->duration = host_to_le16(0);
365 os_memset(head->da, 0xff, ETH_ALEN);
367 os_memcpy(head->sa, hapd->own_addr, ETH_ALEN);
368 os_memcpy(head->bssid, hapd->own_addr, ETH_ALEN);
369 head->u.beacon.beacon_int =
370 host_to_le16(hapd->iconf->beacon_int);
372 /* hardware or low-level driver will setup seq_ctrl and timestamp */
373 capab_info = hostapd_own_capab_info(hapd, NULL, 0);
374 head->u.beacon.capab_info = host_to_le16(capab_info);
375 pos = &head->u.beacon.variable[0];
378 *pos++ = WLAN_EID_SSID;
379 if (hapd->conf->ignore_broadcast_ssid == 2) {
380 /* clear the data, but keep the correct length of the SSID */
381 *pos++ = hapd->conf->ssid.ssid_len;
382 os_memset(pos, 0, hapd->conf->ssid.ssid_len);
383 pos += hapd->conf->ssid.ssid_len;
384 } else if (hapd->conf->ignore_broadcast_ssid) {
385 *pos++ = 0; /* empty SSID */
387 *pos++ = hapd->conf->ssid.ssid_len;
388 os_memcpy(pos, hapd->conf->ssid.ssid,
389 hapd->conf->ssid.ssid_len);
390 pos += hapd->conf->ssid.ssid_len;
393 /* Supported rates */
394 pos = hostapd_eid_supp_rates(hapd, pos);
397 pos = hostapd_eid_ds_params(hapd, pos);
399 head_len = pos - (u8 *) head;
401 tailpos = hostapd_eid_country(hapd, tailpos,
402 tail + BEACON_TAIL_BUF_SIZE - tailpos);
404 /* ERP Information element */
405 tailpos = hostapd_eid_erp_info(hapd, tailpos);
407 /* Extended supported rates */
408 tailpos = hostapd_eid_ext_supp_rates(hapd, tailpos);
410 tailpos = hostapd_eid_wpa(hapd, tailpos, tail + BEACON_TAIL_BUF_SIZE -
413 /* Wi-Fi Alliance WMM */
414 tailpos = hostapd_eid_wmm(hapd, tailpos);
416 #ifdef CONFIG_IEEE80211N
417 tailpos = hostapd_eid_ht_capabilities(hapd, tailpos);
418 tailpos = hostapd_eid_ht_operation(hapd, tailpos);
419 #endif /* CONFIG_IEEE80211N */
422 if (hapd->conf->wps_state && hapd->wps_beacon_ie) {
423 os_memcpy(tailpos, wpabuf_head(hapd->wps_beacon_ie),
424 wpabuf_len(hapd->wps_beacon_ie));
425 tailpos += wpabuf_len(hapd->wps_beacon_ie);
427 #endif /* CONFIG_WPS */
429 tail_len = tailpos > tail ? tailpos - tail : 0;
431 if (hapd->drv.set_beacon(hapd->conf->iface, hapd,
432 (u8 *) head, head_len,
433 tail, tail_len, hapd->conf->dtim_period,
434 hapd->iconf->beacon_int))
435 wpa_printf(MSG_ERROR, "Failed to set beacon head/tail or DTIM "
441 hapd->drv.set_bss_params(hapd, !!(ieee802_11_erp_info(hapd) &
442 ERP_INFO_USE_PROTECTION));
446 void ieee802_11_set_beacons(struct hostapd_iface *iface)
449 for (i = 0; i < iface->num_bss; i++)
450 ieee802_11_set_beacon(iface->bss[i]);
453 #endif /* CONFIG_NATIVE_WINDOWS */