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.
19 #ifndef CONFIG_NATIVE_WINDOWS
23 #include "ieee802_11.h"
27 #include "drivers/driver.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_add(u8 *pos, u8 *end, int chan_spacing,
99 struct hostapd_channel_data *start,
100 struct hostapd_channel_data *prev)
105 /* first channel number */
106 *pos++ = start->chan;
107 /* number of channels */
108 *pos++ = (prev->chan - start->chan) / chan_spacing + 1;
109 /* maximum transmit power level */
110 *pos++ = start->max_tx_power;
116 static u8 * hostapd_eid_country(struct hostapd_data *hapd, u8 *eid,
120 u8 *end = eid + max_len;
122 struct hostapd_hw_modes *mode;
123 struct hostapd_channel_data *start, *prev;
124 int chan_spacing = 1;
126 if (!hapd->iconf->ieee80211d || max_len < 6 ||
127 hapd->iface->current_mode == NULL)
130 *pos++ = WLAN_EID_COUNTRY;
131 pos++; /* length will be set later */
132 os_memcpy(pos, hapd->iconf->country, 3); /* e.g., 'US ' */
135 mode = hapd->iface->current_mode;
136 if (mode->mode == HOSTAPD_MODE_IEEE80211A)
140 for (i = 0; i < mode->num_channels; i++) {
141 struct hostapd_channel_data *chan = &mode->channels[i];
142 if (chan->flag & HOSTAPD_CHAN_DISABLED)
145 prev->chan + chan_spacing == chan->chan &&
146 start->max_tx_power == chan->max_tx_power) {
148 continue; /* can use same entry */
152 pos = hostapd_eid_country_add(pos, end, chan_spacing,
157 /* Start new group */
162 pos = hostapd_eid_country_add(pos, end, chan_spacing,
166 if ((pos - eid) & 1) {
169 *pos++ = 0; /* pad for 16-bit alignment */
172 eid[1] = (pos - eid) - 2;
178 static u8 * hostapd_eid_wpa(struct hostapd_data *hapd, u8 *eid, size_t len,
179 struct sta_info *sta)
184 ie = wpa_auth_get_wpa_ie(hapd->wpa_auth, &ielen);
185 if (ie == NULL || ielen > len)
188 os_memcpy(eid, ie, ielen);
193 void handle_probe_req(struct hostapd_data *hapd,
194 const struct ieee80211_mgmt *mgmt, size_t len)
196 struct ieee80211_mgmt *resp;
197 struct ieee802_11_elems elems;
201 size_t ssid_len, ie_len;
202 struct sta_info *sta = NULL;
205 ie = mgmt->u.probe_req.variable;
206 ie_len = len - (IEEE80211_HDRLEN + sizeof(mgmt->u.probe_req));
208 for (i = 0; hapd->probereq_cb && i < hapd->num_probereq_cb; i++)
209 hapd->probereq_cb[i].cb(hapd->probereq_cb[i].ctx,
210 mgmt->sa, ie, ie_len);
212 if (!hapd->iconf->send_probe_response)
215 if (ieee802_11_parse_elems(ie, ie_len, &elems, 0) == ParseFailed) {
216 wpa_printf(MSG_DEBUG, "Could not parse ProbeReq from " MACSTR,
224 if ((!elems.ssid || !elems.supp_rates)) {
225 wpa_printf(MSG_DEBUG, "STA " MACSTR " sent probe request "
226 "without SSID or supported rates element",
231 if (hapd->conf->ignore_broadcast_ssid && elems.ssid_len == 0) {
232 wpa_printf(MSG_MSGDUMP, "Probe Request from " MACSTR " for "
233 "broadcast SSID ignored", MAC2STR(mgmt->sa));
237 sta = ap_get_sta(hapd, mgmt->sa);
239 if (elems.ssid_len == 0 ||
240 (elems.ssid_len == hapd->conf->ssid.ssid_len &&
241 os_memcmp(elems.ssid, hapd->conf->ssid.ssid, elems.ssid_len) ==
243 ssid = hapd->conf->ssid.ssid;
244 ssid_len = hapd->conf->ssid.ssid_len;
246 sta->ssid_probe = &hapd->conf->ssid;
250 if (!(mgmt->da[0] & 0x01)) {
252 ieee802_11_print_ssid(ssid_txt, elems.ssid,
254 wpa_printf(MSG_MSGDUMP, "Probe Request from " MACSTR
255 " for foreign SSID '%s'",
256 MAC2STR(mgmt->sa), ssid_txt);
261 /* TODO: verify that supp_rates contains at least one matching rate
262 * with AP configuration */
263 #define MAX_PROBERESP_LEN 768
264 resp = os_zalloc(MAX_PROBERESP_LEN);
267 epos = ((u8 *) resp) + MAX_PROBERESP_LEN;
269 resp->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
270 WLAN_FC_STYPE_PROBE_RESP);
271 os_memcpy(resp->da, mgmt->sa, ETH_ALEN);
272 os_memcpy(resp->sa, hapd->own_addr, ETH_ALEN);
274 os_memcpy(resp->bssid, hapd->own_addr, ETH_ALEN);
275 resp->u.probe_resp.beacon_int =
276 host_to_le16(hapd->iconf->beacon_int);
278 /* hardware or low-level driver will setup seq_ctrl and timestamp */
279 resp->u.probe_resp.capab_info =
280 host_to_le16(hostapd_own_capab_info(hapd, sta, 1));
282 pos = resp->u.probe_resp.variable;
283 *pos++ = WLAN_EID_SSID;
285 os_memcpy(pos, ssid, ssid_len);
288 /* Supported rates */
289 pos = hostapd_eid_supp_rates(hapd, pos);
292 pos = hostapd_eid_ds_params(hapd, pos);
294 pos = hostapd_eid_country(hapd, pos, epos - pos);
296 /* ERP Information element */
297 pos = hostapd_eid_erp_info(hapd, pos);
299 /* Extended supported rates */
300 pos = hostapd_eid_ext_supp_rates(hapd, pos);
302 pos = hostapd_eid_wpa(hapd, pos, epos - pos, sta);
304 /* Wi-Fi Alliance WMM */
305 pos = hostapd_eid_wmm(hapd, pos);
307 #ifdef CONFIG_IEEE80211N
308 pos = hostapd_eid_ht_capabilities(hapd, pos);
309 pos = hostapd_eid_ht_operation(hapd, pos);
310 #endif /* CONFIG_IEEE80211N */
313 if (hapd->conf->wps_state && hapd->wps_probe_resp_ie) {
314 os_memcpy(pos, wpabuf_head(hapd->wps_probe_resp_ie),
315 wpabuf_len(hapd->wps_probe_resp_ie));
316 pos += wpabuf_len(hapd->wps_probe_resp_ie);
318 #endif /* CONFIG_WPS */
320 if (hapd->drv.send_mgmt_frame(hapd, resp, pos - (u8 *) resp) < 0)
321 perror("handle_probe_req: send");
325 wpa_printf(MSG_MSGDUMP, "STA " MACSTR " sent probe request for %s "
326 "SSID", MAC2STR(mgmt->sa),
327 elems.ssid_len == 0 ? "broadcast" : "our");
331 void ieee802_11_set_beacon(struct hostapd_data *hapd)
333 struct ieee80211_mgmt *head;
334 u8 *pos, *tail, *tailpos;
336 size_t head_len, tail_len;
338 #define BEACON_HEAD_BUF_SIZE 256
339 #define BEACON_TAIL_BUF_SIZE 512
340 head = os_zalloc(BEACON_HEAD_BUF_SIZE);
341 tailpos = tail = os_malloc(BEACON_TAIL_BUF_SIZE);
342 if (head == NULL || tail == NULL) {
343 wpa_printf(MSG_ERROR, "Failed to set beacon data");
349 head->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
350 WLAN_FC_STYPE_BEACON);
351 head->duration = host_to_le16(0);
352 os_memset(head->da, 0xff, ETH_ALEN);
354 os_memcpy(head->sa, hapd->own_addr, ETH_ALEN);
355 os_memcpy(head->bssid, hapd->own_addr, ETH_ALEN);
356 head->u.beacon.beacon_int =
357 host_to_le16(hapd->iconf->beacon_int);
359 /* hardware or low-level driver will setup seq_ctrl and timestamp */
360 capab_info = hostapd_own_capab_info(hapd, NULL, 0);
361 head->u.beacon.capab_info = host_to_le16(capab_info);
362 pos = &head->u.beacon.variable[0];
365 *pos++ = WLAN_EID_SSID;
366 if (hapd->conf->ignore_broadcast_ssid == 2) {
367 /* clear the data, but keep the correct length of the SSID */
368 *pos++ = hapd->conf->ssid.ssid_len;
369 os_memset(pos, 0, hapd->conf->ssid.ssid_len);
370 pos += hapd->conf->ssid.ssid_len;
371 } else if (hapd->conf->ignore_broadcast_ssid) {
372 *pos++ = 0; /* empty SSID */
374 *pos++ = hapd->conf->ssid.ssid_len;
375 os_memcpy(pos, hapd->conf->ssid.ssid,
376 hapd->conf->ssid.ssid_len);
377 pos += hapd->conf->ssid.ssid_len;
380 /* Supported rates */
381 pos = hostapd_eid_supp_rates(hapd, pos);
384 pos = hostapd_eid_ds_params(hapd, pos);
386 head_len = pos - (u8 *) head;
388 tailpos = hostapd_eid_country(hapd, tailpos,
389 tail + BEACON_TAIL_BUF_SIZE - tailpos);
391 /* ERP Information element */
392 tailpos = hostapd_eid_erp_info(hapd, tailpos);
394 /* Extended supported rates */
395 tailpos = hostapd_eid_ext_supp_rates(hapd, tailpos);
397 tailpos = hostapd_eid_wpa(hapd, tailpos, tail + BEACON_TAIL_BUF_SIZE -
400 /* Wi-Fi Alliance WMM */
401 tailpos = hostapd_eid_wmm(hapd, tailpos);
403 #ifdef CONFIG_IEEE80211N
404 tailpos = hostapd_eid_ht_capabilities(hapd, tailpos);
405 tailpos = hostapd_eid_ht_operation(hapd, tailpos);
406 #endif /* CONFIG_IEEE80211N */
409 if (hapd->conf->wps_state && hapd->wps_beacon_ie) {
410 os_memcpy(tailpos, wpabuf_head(hapd->wps_beacon_ie),
411 wpabuf_len(hapd->wps_beacon_ie));
412 tailpos += wpabuf_len(hapd->wps_beacon_ie);
414 #endif /* CONFIG_WPS */
416 tail_len = tailpos > tail ? tailpos - tail : 0;
418 if (hapd->drv.set_beacon(hapd->conf->iface, hapd,
419 (u8 *) head, head_len,
420 tail, tail_len, hapd->conf->dtim_period,
421 hapd->iconf->beacon_int))
422 wpa_printf(MSG_ERROR, "Failed to set beacon head/tail or DTIM "
428 hapd->drv.set_bss_params(hapd, !!(ieee802_11_erp_info(hapd) &
429 ERP_INFO_USE_PROTECTION));
433 void ieee802_11_set_beacons(struct hostapd_iface *iface)
436 for (i = 0; i < iface->num_bss; i++)
437 ieee802_11_set_beacon(iface->bss[i]);
440 #endif /* CONFIG_NATIVE_WINDOWS */