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 "hw_features.h"
30 #include "wps_hostapd.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_add(u8 *pos, u8 *end, int chan_spacing,
100 struct hostapd_channel_data *start,
101 struct hostapd_channel_data *prev)
106 /* first channel number */
107 *pos++ = start->chan;
108 /* number of channels */
109 *pos++ = (prev->chan - start->chan) / chan_spacing + 1;
110 /* maximum transmit power level */
111 *pos++ = start->max_tx_power;
117 static u8 * hostapd_eid_country(struct hostapd_data *hapd, u8 *eid,
121 u8 *end = eid + max_len;
123 struct hostapd_hw_modes *mode;
124 struct hostapd_channel_data *start, *prev;
125 int chan_spacing = 1;
127 if (!hapd->iconf->ieee80211d || max_len < 6 ||
128 hapd->iface->current_mode == NULL)
131 *pos++ = WLAN_EID_COUNTRY;
132 pos++; /* length will be set later */
133 os_memcpy(pos, hapd->iconf->country, 3); /* e.g., 'US ' */
136 mode = hapd->iface->current_mode;
137 if (mode->mode == HOSTAPD_MODE_IEEE80211A)
141 for (i = 0; i < mode->num_channels; i++) {
142 struct hostapd_channel_data *chan = &mode->channels[i];
143 if (chan->flag & HOSTAPD_CHAN_DISABLED)
146 prev->chan + chan_spacing == chan->chan &&
147 start->max_tx_power == chan->max_tx_power) {
149 continue; /* can use same entry */
153 pos = hostapd_eid_country_add(pos, end, chan_spacing,
158 /* Start new group */
163 pos = hostapd_eid_country_add(pos, end, chan_spacing,
167 if ((pos - eid) & 1) {
170 *pos++ = 0; /* pad for 16-bit alignment */
173 eid[1] = (pos - eid) - 2;
179 static u8 * hostapd_eid_wpa(struct hostapd_data *hapd, u8 *eid, size_t len,
180 struct sta_info *sta)
185 ie = wpa_auth_get_wpa_ie(hapd->wpa_auth, &ielen);
186 if (ie == NULL || ielen > len)
189 os_memcpy(eid, ie, ielen);
194 void handle_probe_req(struct hostapd_data *hapd,
195 const struct ieee80211_mgmt *mgmt, size_t len)
197 struct ieee80211_mgmt *resp;
198 struct ieee802_11_elems elems;
202 size_t ssid_len, ie_len;
203 struct sta_info *sta = NULL;
205 ie = mgmt->u.probe_req.variable;
206 ie_len = len - (IEEE80211_HDRLEN + sizeof(mgmt->u.probe_req));
208 hostapd_probe_req_rx(hapd, mgmt->sa, ie, ie_len);
210 if (!hapd->iconf->send_probe_response)
213 if (ieee802_11_parse_elems(ie, ie_len, &elems, 0) == ParseFailed) {
214 wpa_printf(MSG_DEBUG, "Could not parse ProbeReq from " MACSTR,
222 if ((!elems.ssid || !elems.supp_rates)) {
223 wpa_printf(MSG_DEBUG, "STA " MACSTR " sent probe request "
224 "without SSID or supported rates element",
229 if (hapd->conf->ignore_broadcast_ssid && elems.ssid_len == 0) {
230 wpa_printf(MSG_MSGDUMP, "Probe Request from " MACSTR " for "
231 "broadcast SSID ignored", MAC2STR(mgmt->sa));
235 sta = ap_get_sta(hapd, mgmt->sa);
237 if (elems.ssid_len == 0 ||
238 (elems.ssid_len == hapd->conf->ssid.ssid_len &&
239 os_memcmp(elems.ssid, hapd->conf->ssid.ssid, elems.ssid_len) ==
241 ssid = hapd->conf->ssid.ssid;
242 ssid_len = hapd->conf->ssid.ssid_len;
244 sta->ssid_probe = &hapd->conf->ssid;
248 if (!(mgmt->da[0] & 0x01)) {
250 ieee802_11_print_ssid(ssid_txt, elems.ssid,
252 wpa_printf(MSG_MSGDUMP, "Probe Request from " MACSTR
253 " for foreign SSID '%s'",
254 MAC2STR(mgmt->sa), ssid_txt);
259 /* TODO: verify that supp_rates contains at least one matching rate
260 * with AP configuration */
261 #define MAX_PROBERESP_LEN 768
262 resp = os_zalloc(MAX_PROBERESP_LEN);
265 epos = ((u8 *) resp) + MAX_PROBERESP_LEN;
267 resp->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
268 WLAN_FC_STYPE_PROBE_RESP);
269 os_memcpy(resp->da, mgmt->sa, ETH_ALEN);
270 os_memcpy(resp->sa, hapd->own_addr, ETH_ALEN);
272 os_memcpy(resp->bssid, hapd->own_addr, ETH_ALEN);
273 resp->u.probe_resp.beacon_int =
274 host_to_le16(hapd->iconf->beacon_int);
276 /* hardware or low-level driver will setup seq_ctrl and timestamp */
277 resp->u.probe_resp.capab_info =
278 host_to_le16(hostapd_own_capab_info(hapd, sta, 1));
280 pos = resp->u.probe_resp.variable;
281 *pos++ = WLAN_EID_SSID;
283 os_memcpy(pos, ssid, ssid_len);
286 /* Supported rates */
287 pos = hostapd_eid_supp_rates(hapd, pos);
290 pos = hostapd_eid_ds_params(hapd, pos);
292 pos = hostapd_eid_country(hapd, pos, epos - pos);
294 /* ERP Information element */
295 pos = hostapd_eid_erp_info(hapd, pos);
297 /* Extended supported rates */
298 pos = hostapd_eid_ext_supp_rates(hapd, pos);
300 pos = hostapd_eid_wpa(hapd, pos, epos - pos, sta);
302 /* Wi-Fi Alliance WMM */
303 pos = hostapd_eid_wmm(hapd, pos);
305 #ifdef CONFIG_IEEE80211N
306 pos = hostapd_eid_ht_capabilities(hapd, pos);
307 pos = hostapd_eid_ht_operation(hapd, pos);
308 #endif /* CONFIG_IEEE80211N */
311 if (hapd->conf->wps_state && hapd->wps_probe_resp_ie) {
312 os_memcpy(pos, hapd->wps_probe_resp_ie,
313 hapd->wps_probe_resp_ie_len);
314 pos += hapd->wps_probe_resp_ie_len;
316 #endif /* CONFIG_WPS */
318 if (hostapd_send_mgmt_frame(hapd, resp, pos - (u8 *) resp) < 0)
319 perror("handle_probe_req: send");
323 wpa_printf(MSG_MSGDUMP, "STA " MACSTR " sent probe request for %s "
324 "SSID", MAC2STR(mgmt->sa),
325 elems.ssid_len == 0 ? "broadcast" : "our");
329 void ieee802_11_set_beacon(struct hostapd_data *hapd)
331 struct ieee80211_mgmt *head;
332 u8 *pos, *tail, *tailpos;
335 size_t head_len, tail_len;
336 int cts_protection = ((ieee802_11_erp_info(hapd) &
337 ERP_INFO_USE_PROTECTION) ? 1 : 0);
339 #define BEACON_HEAD_BUF_SIZE 256
340 #define BEACON_TAIL_BUF_SIZE 512
341 head = os_zalloc(BEACON_HEAD_BUF_SIZE);
342 tailpos = tail = os_malloc(BEACON_TAIL_BUF_SIZE);
343 if (head == NULL || tail == NULL) {
344 wpa_printf(MSG_ERROR, "Failed to set beacon data");
350 head->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
351 WLAN_FC_STYPE_BEACON);
352 head->duration = host_to_le16(0);
353 os_memset(head->da, 0xff, ETH_ALEN);
355 os_memcpy(head->sa, hapd->own_addr, ETH_ALEN);
356 os_memcpy(head->bssid, hapd->own_addr, ETH_ALEN);
357 head->u.beacon.beacon_int =
358 host_to_le16(hapd->iconf->beacon_int);
360 /* hardware or low-level driver will setup seq_ctrl and timestamp */
361 capab_info = hostapd_own_capab_info(hapd, NULL, 0);
362 head->u.beacon.capab_info = host_to_le16(capab_info);
363 pos = &head->u.beacon.variable[0];
366 *pos++ = WLAN_EID_SSID;
367 if (hapd->conf->ignore_broadcast_ssid == 2) {
368 /* clear the data, but keep the correct length of the SSID */
369 *pos++ = hapd->conf->ssid.ssid_len;
370 os_memset(pos, 0, hapd->conf->ssid.ssid_len);
371 pos += hapd->conf->ssid.ssid_len;
372 } else if (hapd->conf->ignore_broadcast_ssid) {
373 *pos++ = 0; /* empty SSID */
375 *pos++ = hapd->conf->ssid.ssid_len;
376 os_memcpy(pos, hapd->conf->ssid.ssid,
377 hapd->conf->ssid.ssid_len);
378 pos += hapd->conf->ssid.ssid_len;
381 /* Supported rates */
382 pos = hostapd_eid_supp_rates(hapd, pos);
385 pos = hostapd_eid_ds_params(hapd, pos);
387 head_len = pos - (u8 *) head;
389 tailpos = hostapd_eid_country(hapd, tailpos,
390 tail + BEACON_TAIL_BUF_SIZE - tailpos);
392 /* ERP Information element */
393 tailpos = hostapd_eid_erp_info(hapd, tailpos);
395 /* Extended supported rates */
396 tailpos = hostapd_eid_ext_supp_rates(hapd, tailpos);
398 tailpos = hostapd_eid_wpa(hapd, tailpos, tail + BEACON_TAIL_BUF_SIZE -
401 /* Wi-Fi Alliance WMM */
402 tailpos = hostapd_eid_wmm(hapd, tailpos);
404 #ifdef CONFIG_IEEE80211N
405 if (hapd->iconf->ieee80211n) {
406 u8 *ht_capab, *ht_oper;
408 tailpos = hostapd_eid_ht_capabilities(hapd, tailpos);
411 tailpos = hostapd_eid_ht_operation(hapd, tailpos);
413 if (tailpos > ht_oper && ht_oper > ht_capab &&
414 hostapd_set_ht_params(hapd->conf->iface, hapd,
415 ht_capab + 2, ht_capab[1],
416 ht_oper + 2, ht_oper[1])) {
417 wpa_printf(MSG_ERROR, "Could not set HT capabilities "
418 "for kernel driver");
421 #endif /* CONFIG_IEEE80211N */
424 if (hapd->conf->wps_state && hapd->wps_beacon_ie) {
425 os_memcpy(tailpos, hapd->wps_beacon_ie,
426 hapd->wps_beacon_ie_len);
427 tailpos += hapd->wps_beacon_ie_len;
429 #endif /* CONFIG_WPS */
431 tail_len = tailpos > tail ? tailpos - tail : 0;
433 if (hostapd_set_beacon(hapd->conf->iface, hapd, (u8 *) head, head_len,
434 tail, tail_len, hapd->conf->dtim_period,
435 hapd->iconf->beacon_int))
436 wpa_printf(MSG_ERROR, "Failed to set beacon head/tail or DTIM "
442 if (hostapd_set_cts_protect(hapd, cts_protection))
443 wpa_printf(MSG_ERROR, "Failed to set CTS protect in kernel "
446 if (hapd->iface->current_mode &&
447 hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G &&
448 hostapd_set_short_slot_time(hapd,
449 hapd->iface->num_sta_no_short_slot_time
451 wpa_printf(MSG_ERROR, "Failed to set Short Slot Time option "
454 if (hapd->iface->num_sta_no_short_preamble == 0 &&
455 hapd->iconf->preamble == SHORT_PREAMBLE)
456 preamble = SHORT_PREAMBLE;
458 preamble = LONG_PREAMBLE;
459 if (hostapd_set_preamble(hapd, preamble))
460 wpa_printf(MSG_ERROR, "Could not set preamble for kernel "
465 void ieee802_11_set_beacons(struct hostapd_iface *iface)
468 for (i = 0; i < iface->num_bss; i++)
469 ieee802_11_set_beacon(iface->bss[i]);
472 #endif /* CONFIG_NATIVE_WINDOWS */