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 void handle_probe_req(struct hostapd_data *hapd,
199 const struct ieee80211_mgmt *mgmt, size_t len)
201 struct ieee80211_mgmt *resp;
202 struct ieee802_11_elems elems;
206 size_t ssid_len, ie_len;
207 struct sta_info *sta = NULL;
211 ie = mgmt->u.probe_req.variable;
212 ie_len = len - (IEEE80211_HDRLEN + sizeof(mgmt->u.probe_req));
214 for (i = 0; hapd->probereq_cb && i < hapd->num_probereq_cb; i++)
215 if (hapd->probereq_cb[i].cb(hapd->probereq_cb[i].ctx,
216 mgmt->sa, ie, ie_len) > 0)
219 if (!hapd->iconf->send_probe_response)
222 if (ieee802_11_parse_elems(ie, ie_len, &elems, 0) == ParseFailed) {
223 wpa_printf(MSG_DEBUG, "Could not parse ProbeReq from " MACSTR,
231 if ((!elems.ssid || !elems.supp_rates)) {
232 wpa_printf(MSG_DEBUG, "STA " MACSTR " sent probe request "
233 "without SSID or supported rates element",
239 if (hapd->p2p && elems.wps_ie) {
241 wps = ieee802_11_vendor_ie_concat(ie, ie_len, WPS_DEV_OUI_WFA);
242 if (wps && !p2p_group_match_dev_type(hapd->p2p_group, wps)) {
243 wpa_printf(MSG_MSGDUMP, "P2P: Ignore Probe Request "
244 "due to mismatch with Requested Device "
251 #endif /* CONFIG_P2P */
253 if (hapd->conf->ignore_broadcast_ssid && elems.ssid_len == 0) {
254 wpa_printf(MSG_MSGDUMP, "Probe Request from " MACSTR " for "
255 "broadcast SSID ignored", MAC2STR(mgmt->sa));
259 sta = ap_get_sta(hapd, mgmt->sa);
262 if ((hapd->conf->p2p & P2P_GROUP_OWNER) &&
263 elems.ssid_len == P2P_WILDCARD_SSID_LEN &&
264 os_memcmp(elems.ssid, P2P_WILDCARD_SSID,
265 P2P_WILDCARD_SSID_LEN) == 0) {
266 /* Process P2P Wildcard SSID like Wildcard SSID */
269 #endif /* CONFIG_P2P */
271 if (elems.ssid_len == 0 ||
272 (elems.ssid_len == hapd->conf->ssid.ssid_len &&
273 os_memcmp(elems.ssid, hapd->conf->ssid.ssid, elems.ssid_len) ==
275 ssid = hapd->conf->ssid.ssid;
276 ssid_len = hapd->conf->ssid.ssid_len;
278 sta->ssid_probe = &hapd->conf->ssid;
282 if (!(mgmt->da[0] & 0x01)) {
284 ieee802_11_print_ssid(ssid_txt, elems.ssid,
286 wpa_printf(MSG_MSGDUMP, "Probe Request from " MACSTR
287 " for foreign SSID '%s' (DA " MACSTR ")",
288 MAC2STR(mgmt->sa), ssid_txt,
294 /* TODO: verify that supp_rates contains at least one matching rate
295 * with AP configuration */
296 #define MAX_PROBERESP_LEN 768
297 buflen = MAX_PROBERESP_LEN;
299 if (hapd->wps_probe_resp_ie)
300 buflen += wpabuf_len(hapd->wps_probe_resp_ie);
301 #endif /* CONFIG_WPS */
303 if (hapd->p2p_probe_resp_ie)
304 buflen += wpabuf_len(hapd->p2p_probe_resp_ie);
305 #endif /* CONFIG_P2P */
306 resp = os_zalloc(buflen);
309 epos = ((u8 *) resp) + MAX_PROBERESP_LEN;
311 resp->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
312 WLAN_FC_STYPE_PROBE_RESP);
313 os_memcpy(resp->da, mgmt->sa, ETH_ALEN);
314 os_memcpy(resp->sa, hapd->own_addr, ETH_ALEN);
316 os_memcpy(resp->bssid, hapd->own_addr, ETH_ALEN);
317 resp->u.probe_resp.beacon_int =
318 host_to_le16(hapd->iconf->beacon_int);
320 /* hardware or low-level driver will setup seq_ctrl and timestamp */
321 resp->u.probe_resp.capab_info =
322 host_to_le16(hostapd_own_capab_info(hapd, sta, 1));
324 pos = resp->u.probe_resp.variable;
325 *pos++ = WLAN_EID_SSID;
327 os_memcpy(pos, ssid, ssid_len);
330 /* Supported rates */
331 pos = hostapd_eid_supp_rates(hapd, pos);
334 pos = hostapd_eid_ds_params(hapd, pos);
336 pos = hostapd_eid_country(hapd, pos, epos - pos);
338 /* ERP Information element */
339 pos = hostapd_eid_erp_info(hapd, pos);
341 /* Extended supported rates */
342 pos = hostapd_eid_ext_supp_rates(hapd, pos);
345 pos = hostapd_eid_wpa(hapd, pos, epos - pos, sta);
347 #ifdef CONFIG_IEEE80211N
348 pos = hostapd_eid_ht_capabilities(hapd, pos);
349 pos = hostapd_eid_ht_operation(hapd, pos);
350 #endif /* CONFIG_IEEE80211N */
352 /* Wi-Fi Alliance WMM */
353 pos = hostapd_eid_wmm(hapd, pos);
356 if (hapd->conf->wps_state && hapd->wps_probe_resp_ie) {
357 os_memcpy(pos, wpabuf_head(hapd->wps_probe_resp_ie),
358 wpabuf_len(hapd->wps_probe_resp_ie));
359 pos += wpabuf_len(hapd->wps_probe_resp_ie);
361 #endif /* CONFIG_WPS */
364 if ((hapd->conf->p2p & P2P_ENABLED) && elems.p2p &&
365 hapd->p2p_probe_resp_ie) {
366 os_memcpy(pos, wpabuf_head(hapd->p2p_probe_resp_ie),
367 wpabuf_len(hapd->p2p_probe_resp_ie));
368 pos += wpabuf_len(hapd->p2p_probe_resp_ie);
370 #endif /* CONFIG_P2P */
372 if (hapd->drv.send_mgmt_frame(hapd, resp, pos - (u8 *) resp) < 0)
373 perror("handle_probe_req: send");
377 wpa_printf(MSG_EXCESSIVE, "STA " MACSTR " sent probe request for %s "
378 "SSID", MAC2STR(mgmt->sa),
379 elems.ssid_len == 0 ? "broadcast" : "our");
383 void ieee802_11_set_beacon(struct hostapd_data *hapd)
385 struct ieee80211_mgmt *head;
386 u8 *pos, *tail, *tailpos;
388 size_t head_len, tail_len;
391 if ((hapd->conf->p2p & (P2P_ENABLED | P2P_GROUP_OWNER)) == P2P_ENABLED)
393 #endif /* CONFIG_P2P */
395 #define BEACON_HEAD_BUF_SIZE 256
396 #define BEACON_TAIL_BUF_SIZE 512
397 head = os_zalloc(BEACON_HEAD_BUF_SIZE);
398 tail_len = BEACON_TAIL_BUF_SIZE;
400 if (hapd->conf->wps_state && hapd->wps_beacon_ie)
401 tail_len += wpabuf_len(hapd->wps_beacon_ie);
402 #endif /* CONFIG_WPS */
404 if (hapd->p2p_beacon_ie)
405 tail_len += wpabuf_len(hapd->p2p_beacon_ie);
406 #endif /* CONFIG_P2P */
407 tailpos = tail = os_malloc(tail_len);
408 if (head == NULL || tail == NULL) {
409 wpa_printf(MSG_ERROR, "Failed to set beacon data");
415 head->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
416 WLAN_FC_STYPE_BEACON);
417 head->duration = host_to_le16(0);
418 os_memset(head->da, 0xff, ETH_ALEN);
420 os_memcpy(head->sa, hapd->own_addr, ETH_ALEN);
421 os_memcpy(head->bssid, hapd->own_addr, ETH_ALEN);
422 head->u.beacon.beacon_int =
423 host_to_le16(hapd->iconf->beacon_int);
425 /* hardware or low-level driver will setup seq_ctrl and timestamp */
426 capab_info = hostapd_own_capab_info(hapd, NULL, 0);
427 head->u.beacon.capab_info = host_to_le16(capab_info);
428 pos = &head->u.beacon.variable[0];
431 *pos++ = WLAN_EID_SSID;
432 if (hapd->conf->ignore_broadcast_ssid == 2) {
433 /* clear the data, but keep the correct length of the SSID */
434 *pos++ = hapd->conf->ssid.ssid_len;
435 os_memset(pos, 0, hapd->conf->ssid.ssid_len);
436 pos += hapd->conf->ssid.ssid_len;
437 } else if (hapd->conf->ignore_broadcast_ssid) {
438 *pos++ = 0; /* empty SSID */
440 *pos++ = hapd->conf->ssid.ssid_len;
441 os_memcpy(pos, hapd->conf->ssid.ssid,
442 hapd->conf->ssid.ssid_len);
443 pos += hapd->conf->ssid.ssid_len;
446 /* Supported rates */
447 pos = hostapd_eid_supp_rates(hapd, pos);
450 pos = hostapd_eid_ds_params(hapd, pos);
452 head_len = pos - (u8 *) head;
454 tailpos = hostapd_eid_country(hapd, tailpos,
455 tail + BEACON_TAIL_BUF_SIZE - tailpos);
457 /* ERP Information element */
458 tailpos = hostapd_eid_erp_info(hapd, tailpos);
460 /* Extended supported rates */
461 tailpos = hostapd_eid_ext_supp_rates(hapd, tailpos);
464 tailpos = hostapd_eid_wpa(hapd, tailpos, tail + BEACON_TAIL_BUF_SIZE -
467 #ifdef CONFIG_IEEE80211N
468 tailpos = hostapd_eid_ht_capabilities(hapd, tailpos);
469 tailpos = hostapd_eid_ht_operation(hapd, tailpos);
470 #endif /* CONFIG_IEEE80211N */
472 /* Wi-Fi Alliance WMM */
473 tailpos = hostapd_eid_wmm(hapd, tailpos);
476 if (hapd->conf->wps_state && hapd->wps_beacon_ie) {
477 os_memcpy(tailpos, wpabuf_head(hapd->wps_beacon_ie),
478 wpabuf_len(hapd->wps_beacon_ie));
479 tailpos += wpabuf_len(hapd->wps_beacon_ie);
481 #endif /* CONFIG_WPS */
484 if ((hapd->conf->p2p & P2P_ENABLED) && hapd->p2p_beacon_ie) {
485 os_memcpy(tailpos, wpabuf_head(hapd->p2p_beacon_ie),
486 wpabuf_len(hapd->p2p_beacon_ie));
487 tailpos += wpabuf_len(hapd->p2p_beacon_ie);
489 #endif /* CONFIG_P2P */
491 tail_len = tailpos > tail ? tailpos - tail : 0;
493 if (hapd->drv.set_beacon(hapd, (u8 *) head, head_len,
494 tail, tail_len, hapd->conf->dtim_period,
495 hapd->iconf->beacon_int))
496 wpa_printf(MSG_ERROR, "Failed to set beacon head/tail or DTIM "
504 #endif /* CONFIG_P2P */
505 hapd->drv.set_bss_params(hapd, !!(ieee802_11_erp_info(hapd) &
506 ERP_INFO_USE_PROTECTION));
510 void ieee802_11_set_beacons(struct hostapd_iface *iface)
513 for (i = 0; i < iface->num_bss; i++)
514 ieee802_11_set_beacon(iface->bss[i]);
517 #endif /* CONFIG_NATIVE_WINDOWS */