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"
33 #include "p2p_hostapd.h"
34 #include "ap_drv_ops.h"
38 static u8 ieee802_11_erp_info(struct hostapd_data *hapd)
42 if (hapd->iface->current_mode == NULL ||
43 hapd->iface->current_mode->mode != HOSTAPD_MODE_IEEE80211G)
46 switch (hapd->iconf->cts_protection_type) {
47 case CTS_PROTECTION_FORCE_ENABLED:
48 erp |= ERP_INFO_NON_ERP_PRESENT | ERP_INFO_USE_PROTECTION;
50 case CTS_PROTECTION_FORCE_DISABLED:
53 case CTS_PROTECTION_AUTOMATIC:
54 if (hapd->iface->olbc)
55 erp |= ERP_INFO_USE_PROTECTION;
57 case CTS_PROTECTION_AUTOMATIC_NO_OLBC:
58 if (hapd->iface->num_sta_non_erp > 0) {
59 erp |= ERP_INFO_NON_ERP_PRESENT |
60 ERP_INFO_USE_PROTECTION;
64 if (hapd->iface->num_sta_no_short_preamble > 0 ||
65 hapd->iconf->preamble == LONG_PREAMBLE)
66 erp |= ERP_INFO_BARKER_PREAMBLE_MODE;
72 static u8 * hostapd_eid_ds_params(struct hostapd_data *hapd, u8 *eid)
74 *eid++ = WLAN_EID_DS_PARAMS;
76 *eid++ = hapd->iconf->channel;
81 static u8 * hostapd_eid_erp_info(struct hostapd_data *hapd, u8 *eid)
83 if (hapd->iface->current_mode == NULL ||
84 hapd->iface->current_mode->mode != HOSTAPD_MODE_IEEE80211G)
87 /* Set NonERP_present and use_protection bits if there
88 * are any associated NonERP stations. */
89 /* TODO: use_protection bit can be set to zero even if
90 * there are NonERP stations present. This optimization
91 * might be useful if NonERP stations are "quiet".
92 * See 802.11g/D6 E-1 for recommended practice.
93 * In addition, Non ERP present might be set, if AP detects Non ERP
94 * operation on other APs. */
96 /* Add ERP Information element */
97 *eid++ = WLAN_EID_ERP_INFO;
99 *eid++ = ieee802_11_erp_info(hapd);
105 static u8 * hostapd_eid_country_add(u8 *pos, u8 *end, int chan_spacing,
106 struct hostapd_channel_data *start,
107 struct hostapd_channel_data *prev)
112 /* first channel number */
113 *pos++ = start->chan;
114 /* number of channels */
115 *pos++ = (prev->chan - start->chan) / chan_spacing + 1;
116 /* maximum transmit power level */
117 *pos++ = start->max_tx_power;
123 static u8 * hostapd_eid_country(struct hostapd_data *hapd, u8 *eid,
127 u8 *end = eid + max_len;
129 struct hostapd_hw_modes *mode;
130 struct hostapd_channel_data *start, *prev;
131 int chan_spacing = 1;
133 if (!hapd->iconf->ieee80211d || max_len < 6 ||
134 hapd->iface->current_mode == NULL)
137 *pos++ = WLAN_EID_COUNTRY;
138 pos++; /* length will be set later */
139 os_memcpy(pos, hapd->iconf->country, 3); /* e.g., 'US ' */
142 mode = hapd->iface->current_mode;
143 if (mode->mode == HOSTAPD_MODE_IEEE80211A)
147 for (i = 0; i < mode->num_channels; i++) {
148 struct hostapd_channel_data *chan = &mode->channels[i];
149 if (chan->flag & HOSTAPD_CHAN_DISABLED)
152 prev->chan + chan_spacing == chan->chan &&
153 start->max_tx_power == chan->max_tx_power) {
155 continue; /* can use same entry */
159 pos = hostapd_eid_country_add(pos, end, chan_spacing,
164 /* Start new group */
169 pos = hostapd_eid_country_add(pos, end, chan_spacing,
173 if ((pos - eid) & 1) {
176 *pos++ = 0; /* pad for 16-bit alignment */
179 eid[1] = (pos - eid) - 2;
185 static u8 * hostapd_eid_wpa(struct hostapd_data *hapd, u8 *eid, size_t len,
186 struct sta_info *sta)
191 ie = wpa_auth_get_wpa_ie(hapd->wpa_auth, &ielen);
192 if (ie == NULL || ielen > len)
195 os_memcpy(eid, ie, ielen);
200 void handle_probe_req(struct hostapd_data *hapd,
201 const struct ieee80211_mgmt *mgmt, size_t len)
203 struct ieee80211_mgmt *resp;
204 struct ieee802_11_elems elems;
208 size_t ssid_len, ie_len;
209 struct sta_info *sta = NULL;
213 ie = mgmt->u.probe_req.variable;
214 ie_len = len - (IEEE80211_HDRLEN + sizeof(mgmt->u.probe_req));
216 for (i = 0; hapd->probereq_cb && i < hapd->num_probereq_cb; i++)
217 if (hapd->probereq_cb[i].cb(hapd->probereq_cb[i].ctx,
218 mgmt->sa, ie, ie_len) > 0)
221 if (!hapd->iconf->send_probe_response)
224 if (ieee802_11_parse_elems(ie, ie_len, &elems, 0) == ParseFailed) {
225 wpa_printf(MSG_DEBUG, "Could not parse ProbeReq from " MACSTR,
233 if ((!elems.ssid || !elems.supp_rates)) {
234 wpa_printf(MSG_DEBUG, "STA " MACSTR " sent probe request "
235 "without SSID or supported rates element",
241 if (hapd->p2p && elems.wps_ie) {
243 wps = ieee802_11_vendor_ie_concat(ie, ie_len, WPS_DEV_OUI_WFA);
244 if (wps && !p2p_group_match_dev_type(hapd->p2p_group, wps)) {
245 wpa_printf(MSG_MSGDUMP, "P2P: Ignore Probe Request "
246 "due to mismatch with Requested Device "
253 #endif /* CONFIG_P2P */
255 if (hapd->conf->ignore_broadcast_ssid && elems.ssid_len == 0) {
256 wpa_printf(MSG_MSGDUMP, "Probe Request from " MACSTR " for "
257 "broadcast SSID ignored", MAC2STR(mgmt->sa));
261 sta = ap_get_sta(hapd, mgmt->sa);
264 if ((hapd->conf->p2p & P2P_GROUP_OWNER) &&
265 elems.ssid_len == P2P_WILDCARD_SSID_LEN &&
266 os_memcmp(elems.ssid, P2P_WILDCARD_SSID,
267 P2P_WILDCARD_SSID_LEN) == 0) {
268 /* Process P2P Wildcard SSID like Wildcard SSID */
271 #endif /* CONFIG_P2P */
273 if (elems.ssid_len == 0 ||
274 (elems.ssid_len == hapd->conf->ssid.ssid_len &&
275 os_memcmp(elems.ssid, hapd->conf->ssid.ssid, elems.ssid_len) ==
277 ssid = hapd->conf->ssid.ssid;
278 ssid_len = hapd->conf->ssid.ssid_len;
280 sta->ssid_probe = &hapd->conf->ssid;
284 if (!(mgmt->da[0] & 0x01)) {
286 ieee802_11_print_ssid(ssid_txt, elems.ssid,
288 wpa_printf(MSG_MSGDUMP, "Probe Request from " MACSTR
289 " for foreign SSID '%s' (DA " MACSTR ")",
290 MAC2STR(mgmt->sa), ssid_txt,
296 /* TODO: verify that supp_rates contains at least one matching rate
297 * with AP configuration */
298 #define MAX_PROBERESP_LEN 768
299 buflen = MAX_PROBERESP_LEN;
301 if (hapd->wps_probe_resp_ie)
302 buflen += wpabuf_len(hapd->wps_probe_resp_ie);
303 #endif /* CONFIG_WPS */
305 if (hapd->p2p_probe_resp_ie)
306 buflen += wpabuf_len(hapd->p2p_probe_resp_ie);
307 #endif /* CONFIG_P2P */
308 resp = os_zalloc(buflen);
311 epos = ((u8 *) resp) + MAX_PROBERESP_LEN;
313 resp->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
314 WLAN_FC_STYPE_PROBE_RESP);
315 os_memcpy(resp->da, mgmt->sa, ETH_ALEN);
316 os_memcpy(resp->sa, hapd->own_addr, ETH_ALEN);
318 os_memcpy(resp->bssid, hapd->own_addr, ETH_ALEN);
319 resp->u.probe_resp.beacon_int =
320 host_to_le16(hapd->iconf->beacon_int);
322 /* hardware or low-level driver will setup seq_ctrl and timestamp */
323 resp->u.probe_resp.capab_info =
324 host_to_le16(hostapd_own_capab_info(hapd, sta, 1));
326 pos = resp->u.probe_resp.variable;
327 *pos++ = WLAN_EID_SSID;
329 os_memcpy(pos, ssid, ssid_len);
332 /* Supported rates */
333 pos = hostapd_eid_supp_rates(hapd, pos);
336 pos = hostapd_eid_ds_params(hapd, pos);
338 pos = hostapd_eid_country(hapd, pos, epos - pos);
340 /* ERP Information element */
341 pos = hostapd_eid_erp_info(hapd, pos);
343 /* Extended supported rates */
344 pos = hostapd_eid_ext_supp_rates(hapd, pos);
347 pos = hostapd_eid_wpa(hapd, pos, epos - pos, sta);
349 #ifdef CONFIG_IEEE80211N
350 pos = hostapd_eid_ht_capabilities(hapd, pos);
351 pos = hostapd_eid_ht_operation(hapd, pos);
352 #endif /* CONFIG_IEEE80211N */
354 /* Wi-Fi Alliance WMM */
355 pos = hostapd_eid_wmm(hapd, pos);
358 if (hapd->conf->wps_state && hapd->wps_probe_resp_ie) {
359 os_memcpy(pos, wpabuf_head(hapd->wps_probe_resp_ie),
360 wpabuf_len(hapd->wps_probe_resp_ie));
361 pos += wpabuf_len(hapd->wps_probe_resp_ie);
363 #endif /* CONFIG_WPS */
366 if ((hapd->conf->p2p & P2P_ENABLED) && elems.p2p &&
367 hapd->p2p_probe_resp_ie) {
368 os_memcpy(pos, wpabuf_head(hapd->p2p_probe_resp_ie),
369 wpabuf_len(hapd->p2p_probe_resp_ie));
370 pos += wpabuf_len(hapd->p2p_probe_resp_ie);
372 #endif /* CONFIG_P2P */
373 #ifdef CONFIG_P2P_MANAGER
374 if ((hapd->conf->p2p & (P2P_MANAGE | P2P_ENABLED | P2P_GROUP_OWNER)) ==
376 pos = hostapd_eid_p2p_manage(hapd, pos);
377 #endif /* CONFIG_P2P_MANAGER */
379 if (hostapd_drv_send_mlme(hapd, resp, pos - (u8 *) resp) < 0)
380 perror("handle_probe_req: send");
384 wpa_printf(MSG_EXCESSIVE, "STA " MACSTR " sent probe request for %s "
385 "SSID", MAC2STR(mgmt->sa),
386 elems.ssid_len == 0 ? "broadcast" : "our");
390 void ieee802_11_set_beacon(struct hostapd_data *hapd)
392 struct ieee80211_mgmt *head;
393 u8 *pos, *tail, *tailpos;
395 size_t head_len, tail_len;
398 if ((hapd->conf->p2p & (P2P_ENABLED | P2P_GROUP_OWNER)) == P2P_ENABLED)
400 #endif /* CONFIG_P2P */
402 #define BEACON_HEAD_BUF_SIZE 256
403 #define BEACON_TAIL_BUF_SIZE 512
404 head = os_zalloc(BEACON_HEAD_BUF_SIZE);
405 tail_len = BEACON_TAIL_BUF_SIZE;
407 if (hapd->conf->wps_state && hapd->wps_beacon_ie)
408 tail_len += wpabuf_len(hapd->wps_beacon_ie);
409 #endif /* CONFIG_WPS */
411 if (hapd->p2p_beacon_ie)
412 tail_len += wpabuf_len(hapd->p2p_beacon_ie);
413 #endif /* CONFIG_P2P */
414 tailpos = tail = os_malloc(tail_len);
415 if (head == NULL || tail == NULL) {
416 wpa_printf(MSG_ERROR, "Failed to set beacon data");
422 head->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
423 WLAN_FC_STYPE_BEACON);
424 head->duration = host_to_le16(0);
425 os_memset(head->da, 0xff, ETH_ALEN);
427 os_memcpy(head->sa, hapd->own_addr, ETH_ALEN);
428 os_memcpy(head->bssid, hapd->own_addr, ETH_ALEN);
429 head->u.beacon.beacon_int =
430 host_to_le16(hapd->iconf->beacon_int);
432 /* hardware or low-level driver will setup seq_ctrl and timestamp */
433 capab_info = hostapd_own_capab_info(hapd, NULL, 0);
434 head->u.beacon.capab_info = host_to_le16(capab_info);
435 pos = &head->u.beacon.variable[0];
438 *pos++ = WLAN_EID_SSID;
439 if (hapd->conf->ignore_broadcast_ssid == 2) {
440 /* clear the data, but keep the correct length of the SSID */
441 *pos++ = hapd->conf->ssid.ssid_len;
442 os_memset(pos, 0, hapd->conf->ssid.ssid_len);
443 pos += hapd->conf->ssid.ssid_len;
444 } else if (hapd->conf->ignore_broadcast_ssid) {
445 *pos++ = 0; /* empty SSID */
447 *pos++ = hapd->conf->ssid.ssid_len;
448 os_memcpy(pos, hapd->conf->ssid.ssid,
449 hapd->conf->ssid.ssid_len);
450 pos += hapd->conf->ssid.ssid_len;
453 /* Supported rates */
454 pos = hostapd_eid_supp_rates(hapd, pos);
457 pos = hostapd_eid_ds_params(hapd, pos);
459 head_len = pos - (u8 *) head;
461 tailpos = hostapd_eid_country(hapd, tailpos,
462 tail + BEACON_TAIL_BUF_SIZE - tailpos);
464 /* ERP Information element */
465 tailpos = hostapd_eid_erp_info(hapd, tailpos);
467 /* Extended supported rates */
468 tailpos = hostapd_eid_ext_supp_rates(hapd, tailpos);
471 tailpos = hostapd_eid_wpa(hapd, tailpos, tail + BEACON_TAIL_BUF_SIZE -
474 #ifdef CONFIG_IEEE80211N
475 tailpos = hostapd_eid_ht_capabilities(hapd, tailpos);
476 tailpos = hostapd_eid_ht_operation(hapd, tailpos);
477 #endif /* CONFIG_IEEE80211N */
479 /* Wi-Fi Alliance WMM */
480 tailpos = hostapd_eid_wmm(hapd, tailpos);
483 if (hapd->conf->wps_state && hapd->wps_beacon_ie) {
484 os_memcpy(tailpos, wpabuf_head(hapd->wps_beacon_ie),
485 wpabuf_len(hapd->wps_beacon_ie));
486 tailpos += wpabuf_len(hapd->wps_beacon_ie);
488 #endif /* CONFIG_WPS */
491 if ((hapd->conf->p2p & P2P_ENABLED) && hapd->p2p_beacon_ie) {
492 os_memcpy(tailpos, wpabuf_head(hapd->p2p_beacon_ie),
493 wpabuf_len(hapd->p2p_beacon_ie));
494 tailpos += wpabuf_len(hapd->p2p_beacon_ie);
496 #endif /* CONFIG_P2P */
497 #ifdef CONFIG_P2P_MANAGER
498 if ((hapd->conf->p2p & (P2P_MANAGE | P2P_ENABLED | P2P_GROUP_OWNER)) ==
500 tailpos = hostapd_eid_p2p_manage(hapd, tailpos);
501 #endif /* CONFIG_P2P_MANAGER */
503 tail_len = tailpos > tail ? tailpos - tail : 0;
505 if (hostapd_drv_set_beacon(hapd, (u8 *) head, head_len,
506 tail, tail_len, hapd->conf->dtim_period,
507 hapd->iconf->beacon_int))
508 wpa_printf(MSG_ERROR, "Failed to set beacon head/tail or DTIM "
516 #endif /* CONFIG_P2P */
517 hapd->drv.set_bss_params(hapd, !!(ieee802_11_erp_info(hapd) &
518 ERP_INFO_USE_PROTECTION));
522 void ieee802_11_set_beacons(struct hostapd_iface *iface)
525 for (i = 0; i < iface->num_bss; i++)
526 ieee802_11_set_beacon(iface->bss[i]);
529 #endif /* CONFIG_NATIVE_WINDOWS */