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"
25 #include "wps/wps_defs.h"
28 #include "ieee802_11.h"
31 #include "ap_config.h"
36 static u8 ieee802_11_erp_info(struct hostapd_data *hapd)
40 if (hapd->iface->current_mode == NULL ||
41 hapd->iface->current_mode->mode != HOSTAPD_MODE_IEEE80211G)
44 switch (hapd->iconf->cts_protection_type) {
45 case CTS_PROTECTION_FORCE_ENABLED:
46 erp |= ERP_INFO_NON_ERP_PRESENT | ERP_INFO_USE_PROTECTION;
48 case CTS_PROTECTION_FORCE_DISABLED:
51 case CTS_PROTECTION_AUTOMATIC:
52 if (hapd->iface->olbc)
53 erp |= ERP_INFO_USE_PROTECTION;
55 case CTS_PROTECTION_AUTOMATIC_NO_OLBC:
56 if (hapd->iface->num_sta_non_erp > 0) {
57 erp |= ERP_INFO_NON_ERP_PRESENT |
58 ERP_INFO_USE_PROTECTION;
62 if (hapd->iface->num_sta_no_short_preamble > 0 ||
63 hapd->iconf->preamble == LONG_PREAMBLE)
64 erp |= ERP_INFO_BARKER_PREAMBLE_MODE;
70 static u8 * hostapd_eid_ds_params(struct hostapd_data *hapd, u8 *eid)
72 *eid++ = WLAN_EID_DS_PARAMS;
74 *eid++ = hapd->iconf->channel;
79 static u8 * hostapd_eid_erp_info(struct hostapd_data *hapd, u8 *eid)
81 if (hapd->iface->current_mode == NULL ||
82 hapd->iface->current_mode->mode != HOSTAPD_MODE_IEEE80211G)
85 /* Set NonERP_present and use_protection bits if there
86 * are any associated NonERP stations. */
87 /* TODO: use_protection bit can be set to zero even if
88 * there are NonERP stations present. This optimization
89 * might be useful if NonERP stations are "quiet".
90 * See 802.11g/D6 E-1 for recommended practice.
91 * In addition, Non ERP present might be set, if AP detects Non ERP
92 * operation on other APs. */
94 /* Add ERP Information element */
95 *eid++ = WLAN_EID_ERP_INFO;
97 *eid++ = ieee802_11_erp_info(hapd);
103 static u8 * hostapd_eid_country_add(u8 *pos, u8 *end, int chan_spacing,
104 struct hostapd_channel_data *start,
105 struct hostapd_channel_data *prev)
110 /* first channel number */
111 *pos++ = start->chan;
112 /* number of channels */
113 *pos++ = (prev->chan - start->chan) / chan_spacing + 1;
114 /* maximum transmit power level */
115 *pos++ = start->max_tx_power;
121 static u8 * hostapd_eid_country(struct hostapd_data *hapd, u8 *eid,
125 u8 *end = eid + max_len;
127 struct hostapd_hw_modes *mode;
128 struct hostapd_channel_data *start, *prev;
129 int chan_spacing = 1;
131 if (!hapd->iconf->ieee80211d || max_len < 6 ||
132 hapd->iface->current_mode == NULL)
135 *pos++ = WLAN_EID_COUNTRY;
136 pos++; /* length will be set later */
137 os_memcpy(pos, hapd->iconf->country, 3); /* e.g., 'US ' */
140 mode = hapd->iface->current_mode;
141 if (mode->mode == HOSTAPD_MODE_IEEE80211A)
145 for (i = 0; i < mode->num_channels; i++) {
146 struct hostapd_channel_data *chan = &mode->channels[i];
147 if (chan->flag & HOSTAPD_CHAN_DISABLED)
150 prev->chan + chan_spacing == chan->chan &&
151 start->max_tx_power == chan->max_tx_power) {
153 continue; /* can use same entry */
157 pos = hostapd_eid_country_add(pos, end, chan_spacing,
162 /* Start new group */
167 pos = hostapd_eid_country_add(pos, end, chan_spacing,
171 if ((pos - eid) & 1) {
174 *pos++ = 0; /* pad for 16-bit alignment */
177 eid[1] = (pos - eid) - 2;
183 static u8 * hostapd_eid_wpa(struct hostapd_data *hapd, u8 *eid, size_t len,
184 struct sta_info *sta)
189 ie = wpa_auth_get_wpa_ie(hapd->wpa_auth, &ielen);
190 if (ie == NULL || ielen > len)
193 os_memcpy(eid, ie, ielen);
198 #ifdef CONFIG_P2P_MANAGER
199 u8 * hostapd_eid_p2p_manage(struct hostapd_data *hapd, u8 *eid)
202 *eid++ = WLAN_EID_VENDOR_SPECIFIC;
204 WPA_PUT_BE24(eid, OUI_WFA);
206 *eid++ = P2P_OUI_TYPE;
208 *eid++ = P2P_ATTR_MANAGEABILITY;
209 WPA_PUT_LE16(eid, 1);
211 bitmap = BIT(0); /* P2P Device Management */
212 if (hapd->conf->p2p & P2P_ALLOW_CROSS_CONNECTION)
213 bitmap |= BIT(1); /* Cross Connection Permitted */
218 #endif /* CONFIG_P2P_MANAGER */
221 void handle_probe_req(struct hostapd_data *hapd,
222 const struct ieee80211_mgmt *mgmt, size_t len)
224 struct ieee80211_mgmt *resp;
225 struct ieee802_11_elems elems;
229 size_t ssid_len, ie_len;
230 struct sta_info *sta = NULL;
234 ie = mgmt->u.probe_req.variable;
235 ie_len = len - (IEEE80211_HDRLEN + sizeof(mgmt->u.probe_req));
237 for (i = 0; hapd->probereq_cb && i < hapd->num_probereq_cb; i++)
238 if (hapd->probereq_cb[i].cb(hapd->probereq_cb[i].ctx,
239 mgmt->sa, ie, ie_len) > 0)
242 if (!hapd->iconf->send_probe_response)
245 if (ieee802_11_parse_elems(ie, ie_len, &elems, 0) == ParseFailed) {
246 wpa_printf(MSG_DEBUG, "Could not parse ProbeReq from " MACSTR,
254 if ((!elems.ssid || !elems.supp_rates)) {
255 wpa_printf(MSG_DEBUG, "STA " MACSTR " sent probe request "
256 "without SSID or supported rates element",
262 if (hapd->p2p && elems.wps_ie) {
264 wps = ieee802_11_vendor_ie_concat(ie, ie_len, WPS_DEV_OUI_WFA);
265 if (wps && !p2p_group_match_dev_type(hapd->p2p_group, wps)) {
266 wpa_printf(MSG_MSGDUMP, "P2P: Ignore Probe Request "
267 "due to mismatch with Requested Device "
274 #endif /* CONFIG_P2P */
276 if (hapd->conf->ignore_broadcast_ssid && elems.ssid_len == 0) {
277 wpa_printf(MSG_MSGDUMP, "Probe Request from " MACSTR " for "
278 "broadcast SSID ignored", MAC2STR(mgmt->sa));
282 sta = ap_get_sta(hapd, mgmt->sa);
285 if ((hapd->conf->p2p & P2P_GROUP_OWNER) &&
286 elems.ssid_len == P2P_WILDCARD_SSID_LEN &&
287 os_memcmp(elems.ssid, P2P_WILDCARD_SSID,
288 P2P_WILDCARD_SSID_LEN) == 0) {
289 /* Process P2P Wildcard SSID like Wildcard SSID */
292 #endif /* CONFIG_P2P */
294 if (elems.ssid_len == 0 ||
295 (elems.ssid_len == hapd->conf->ssid.ssid_len &&
296 os_memcmp(elems.ssid, hapd->conf->ssid.ssid, elems.ssid_len) ==
298 ssid = hapd->conf->ssid.ssid;
299 ssid_len = hapd->conf->ssid.ssid_len;
301 sta->ssid_probe = &hapd->conf->ssid;
305 if (!(mgmt->da[0] & 0x01)) {
307 ieee802_11_print_ssid(ssid_txt, elems.ssid,
309 wpa_printf(MSG_MSGDUMP, "Probe Request from " MACSTR
310 " for foreign SSID '%s' (DA " MACSTR ")",
311 MAC2STR(mgmt->sa), ssid_txt,
317 /* TODO: verify that supp_rates contains at least one matching rate
318 * with AP configuration */
319 #define MAX_PROBERESP_LEN 768
320 buflen = MAX_PROBERESP_LEN;
322 if (hapd->wps_probe_resp_ie)
323 buflen += wpabuf_len(hapd->wps_probe_resp_ie);
324 #endif /* CONFIG_WPS */
326 if (hapd->p2p_probe_resp_ie)
327 buflen += wpabuf_len(hapd->p2p_probe_resp_ie);
328 #endif /* CONFIG_P2P */
329 resp = os_zalloc(buflen);
332 epos = ((u8 *) resp) + MAX_PROBERESP_LEN;
334 resp->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
335 WLAN_FC_STYPE_PROBE_RESP);
336 os_memcpy(resp->da, mgmt->sa, ETH_ALEN);
337 os_memcpy(resp->sa, hapd->own_addr, ETH_ALEN);
339 os_memcpy(resp->bssid, hapd->own_addr, ETH_ALEN);
340 resp->u.probe_resp.beacon_int =
341 host_to_le16(hapd->iconf->beacon_int);
343 /* hardware or low-level driver will setup seq_ctrl and timestamp */
344 resp->u.probe_resp.capab_info =
345 host_to_le16(hostapd_own_capab_info(hapd, sta, 1));
347 pos = resp->u.probe_resp.variable;
348 *pos++ = WLAN_EID_SSID;
350 os_memcpy(pos, ssid, ssid_len);
353 /* Supported rates */
354 pos = hostapd_eid_supp_rates(hapd, pos);
357 pos = hostapd_eid_ds_params(hapd, pos);
359 pos = hostapd_eid_country(hapd, pos, epos - pos);
361 /* ERP Information element */
362 pos = hostapd_eid_erp_info(hapd, pos);
364 /* Extended supported rates */
365 pos = hostapd_eid_ext_supp_rates(hapd, pos);
368 pos = hostapd_eid_wpa(hapd, pos, epos - pos, sta);
370 #ifdef CONFIG_IEEE80211N
371 pos = hostapd_eid_ht_capabilities(hapd, pos);
372 pos = hostapd_eid_ht_operation(hapd, pos);
373 #endif /* CONFIG_IEEE80211N */
375 /* Wi-Fi Alliance WMM */
376 pos = hostapd_eid_wmm(hapd, pos);
379 if (hapd->conf->wps_state && hapd->wps_probe_resp_ie) {
380 os_memcpy(pos, wpabuf_head(hapd->wps_probe_resp_ie),
381 wpabuf_len(hapd->wps_probe_resp_ie));
382 pos += wpabuf_len(hapd->wps_probe_resp_ie);
384 #endif /* CONFIG_WPS */
387 if ((hapd->conf->p2p & P2P_ENABLED) && elems.p2p &&
388 hapd->p2p_probe_resp_ie) {
389 os_memcpy(pos, wpabuf_head(hapd->p2p_probe_resp_ie),
390 wpabuf_len(hapd->p2p_probe_resp_ie));
391 pos += wpabuf_len(hapd->p2p_probe_resp_ie);
393 #endif /* CONFIG_P2P */
394 #ifdef CONFIG_P2P_MANAGER
395 if ((hapd->conf->p2p & (P2P_MANAGE | P2P_ENABLED | P2P_GROUP_OWNER)) ==
397 pos = hostapd_eid_p2p_manage(hapd, pos);
398 #endif /* CONFIG_P2P_MANAGER */
400 if (hapd->drv.send_mgmt_frame(hapd, resp, pos - (u8 *) resp) < 0)
401 perror("handle_probe_req: send");
405 wpa_printf(MSG_EXCESSIVE, "STA " MACSTR " sent probe request for %s "
406 "SSID", MAC2STR(mgmt->sa),
407 elems.ssid_len == 0 ? "broadcast" : "our");
411 void ieee802_11_set_beacon(struct hostapd_data *hapd)
413 struct ieee80211_mgmt *head;
414 u8 *pos, *tail, *tailpos;
416 size_t head_len, tail_len;
419 if ((hapd->conf->p2p & (P2P_ENABLED | P2P_GROUP_OWNER)) == P2P_ENABLED)
421 #endif /* CONFIG_P2P */
423 #define BEACON_HEAD_BUF_SIZE 256
424 #define BEACON_TAIL_BUF_SIZE 512
425 head = os_zalloc(BEACON_HEAD_BUF_SIZE);
426 tail_len = BEACON_TAIL_BUF_SIZE;
428 if (hapd->conf->wps_state && hapd->wps_beacon_ie)
429 tail_len += wpabuf_len(hapd->wps_beacon_ie);
430 #endif /* CONFIG_WPS */
432 if (hapd->p2p_beacon_ie)
433 tail_len += wpabuf_len(hapd->p2p_beacon_ie);
434 #endif /* CONFIG_P2P */
435 tailpos = tail = os_malloc(tail_len);
436 if (head == NULL || tail == NULL) {
437 wpa_printf(MSG_ERROR, "Failed to set beacon data");
443 head->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
444 WLAN_FC_STYPE_BEACON);
445 head->duration = host_to_le16(0);
446 os_memset(head->da, 0xff, ETH_ALEN);
448 os_memcpy(head->sa, hapd->own_addr, ETH_ALEN);
449 os_memcpy(head->bssid, hapd->own_addr, ETH_ALEN);
450 head->u.beacon.beacon_int =
451 host_to_le16(hapd->iconf->beacon_int);
453 /* hardware or low-level driver will setup seq_ctrl and timestamp */
454 capab_info = hostapd_own_capab_info(hapd, NULL, 0);
455 head->u.beacon.capab_info = host_to_le16(capab_info);
456 pos = &head->u.beacon.variable[0];
459 *pos++ = WLAN_EID_SSID;
460 if (hapd->conf->ignore_broadcast_ssid == 2) {
461 /* clear the data, but keep the correct length of the SSID */
462 *pos++ = hapd->conf->ssid.ssid_len;
463 os_memset(pos, 0, hapd->conf->ssid.ssid_len);
464 pos += hapd->conf->ssid.ssid_len;
465 } else if (hapd->conf->ignore_broadcast_ssid) {
466 *pos++ = 0; /* empty SSID */
468 *pos++ = hapd->conf->ssid.ssid_len;
469 os_memcpy(pos, hapd->conf->ssid.ssid,
470 hapd->conf->ssid.ssid_len);
471 pos += hapd->conf->ssid.ssid_len;
474 /* Supported rates */
475 pos = hostapd_eid_supp_rates(hapd, pos);
478 pos = hostapd_eid_ds_params(hapd, pos);
480 head_len = pos - (u8 *) head;
482 tailpos = hostapd_eid_country(hapd, tailpos,
483 tail + BEACON_TAIL_BUF_SIZE - tailpos);
485 /* ERP Information element */
486 tailpos = hostapd_eid_erp_info(hapd, tailpos);
488 /* Extended supported rates */
489 tailpos = hostapd_eid_ext_supp_rates(hapd, tailpos);
492 tailpos = hostapd_eid_wpa(hapd, tailpos, tail + BEACON_TAIL_BUF_SIZE -
495 #ifdef CONFIG_IEEE80211N
496 tailpos = hostapd_eid_ht_capabilities(hapd, tailpos);
497 tailpos = hostapd_eid_ht_operation(hapd, tailpos);
498 #endif /* CONFIG_IEEE80211N */
500 /* Wi-Fi Alliance WMM */
501 tailpos = hostapd_eid_wmm(hapd, tailpos);
504 if (hapd->conf->wps_state && hapd->wps_beacon_ie) {
505 os_memcpy(tailpos, wpabuf_head(hapd->wps_beacon_ie),
506 wpabuf_len(hapd->wps_beacon_ie));
507 tailpos += wpabuf_len(hapd->wps_beacon_ie);
509 #endif /* CONFIG_WPS */
512 if ((hapd->conf->p2p & P2P_ENABLED) && hapd->p2p_beacon_ie) {
513 os_memcpy(tailpos, wpabuf_head(hapd->p2p_beacon_ie),
514 wpabuf_len(hapd->p2p_beacon_ie));
515 tailpos += wpabuf_len(hapd->p2p_beacon_ie);
517 #endif /* CONFIG_P2P */
518 #ifdef CONFIG_P2P_MANAGER
519 if ((hapd->conf->p2p & (P2P_MANAGE | P2P_ENABLED | P2P_GROUP_OWNER)) ==
521 tailpos = hostapd_eid_p2p_manage(hapd, tailpos);
522 #endif /* CONFIG_P2P_MANAGER */
524 tail_len = tailpos > tail ? tailpos - tail : 0;
526 if (hapd->drv.set_beacon(hapd, (u8 *) head, head_len,
527 tail, tail_len, hapd->conf->dtim_period,
528 hapd->iconf->beacon_int))
529 wpa_printf(MSG_ERROR, "Failed to set beacon head/tail or DTIM "
537 #endif /* CONFIG_P2P */
538 hapd->drv.set_bss_params(hapd, !!(ieee802_11_erp_info(hapd) &
539 ERP_INFO_USE_PROTECTION));
543 void ieee802_11_set_beacons(struct hostapd_iface *iface)
546 for (i = 0; i < iface->num_bss; i++)
547 ieee802_11_set_beacon(iface->bss[i]);
550 #endif /* CONFIG_NATIVE_WINDOWS */