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"
40 static u8 ieee802_11_erp_info(struct hostapd_data *hapd)
44 if (hapd->iface->current_mode == NULL ||
45 hapd->iface->current_mode->mode != HOSTAPD_MODE_IEEE80211G)
48 switch (hapd->iconf->cts_protection_type) {
49 case CTS_PROTECTION_FORCE_ENABLED:
50 erp |= ERP_INFO_NON_ERP_PRESENT | ERP_INFO_USE_PROTECTION;
52 case CTS_PROTECTION_FORCE_DISABLED:
55 case CTS_PROTECTION_AUTOMATIC:
56 if (hapd->iface->olbc)
57 erp |= ERP_INFO_USE_PROTECTION;
59 case CTS_PROTECTION_AUTOMATIC_NO_OLBC:
60 if (hapd->iface->num_sta_non_erp > 0) {
61 erp |= ERP_INFO_NON_ERP_PRESENT |
62 ERP_INFO_USE_PROTECTION;
66 if (hapd->iface->num_sta_no_short_preamble > 0 ||
67 hapd->iconf->preamble == LONG_PREAMBLE)
68 erp |= ERP_INFO_BARKER_PREAMBLE_MODE;
74 static u8 * hostapd_eid_ds_params(struct hostapd_data *hapd, u8 *eid)
76 *eid++ = WLAN_EID_DS_PARAMS;
78 *eid++ = hapd->iconf->channel;
83 static u8 * hostapd_eid_erp_info(struct hostapd_data *hapd, u8 *eid)
85 if (hapd->iface->current_mode == NULL ||
86 hapd->iface->current_mode->mode != HOSTAPD_MODE_IEEE80211G)
89 /* Set NonERP_present and use_protection bits if there
90 * are any associated NonERP stations. */
91 /* TODO: use_protection bit can be set to zero even if
92 * there are NonERP stations present. This optimization
93 * might be useful if NonERP stations are "quiet".
94 * See 802.11g/D6 E-1 for recommended practice.
95 * In addition, Non ERP present might be set, if AP detects Non ERP
96 * operation on other APs. */
98 /* Add ERP Information element */
99 *eid++ = WLAN_EID_ERP_INFO;
101 *eid++ = ieee802_11_erp_info(hapd);
107 static u8 * hostapd_eid_country_add(u8 *pos, u8 *end, int chan_spacing,
108 struct hostapd_channel_data *start,
109 struct hostapd_channel_data *prev)
114 /* first channel number */
115 *pos++ = start->chan;
116 /* number of channels */
117 *pos++ = (prev->chan - start->chan) / chan_spacing + 1;
118 /* maximum transmit power level */
119 *pos++ = start->max_tx_power;
125 static u8 * hostapd_eid_country(struct hostapd_data *hapd, u8 *eid,
129 u8 *end = eid + max_len;
131 struct hostapd_hw_modes *mode;
132 struct hostapd_channel_data *start, *prev;
133 int chan_spacing = 1;
135 if (!hapd->iconf->ieee80211d || max_len < 6 ||
136 hapd->iface->current_mode == NULL)
139 *pos++ = WLAN_EID_COUNTRY;
140 pos++; /* length will be set later */
141 os_memcpy(pos, hapd->iconf->country, 3); /* e.g., 'US ' */
144 mode = hapd->iface->current_mode;
145 if (mode->mode == HOSTAPD_MODE_IEEE80211A)
149 for (i = 0; i < mode->num_channels; i++) {
150 struct hostapd_channel_data *chan = &mode->channels[i];
151 if (chan->flag & HOSTAPD_CHAN_DISABLED)
154 prev->chan + chan_spacing == chan->chan &&
155 start->max_tx_power == chan->max_tx_power) {
157 continue; /* can use same entry */
161 pos = hostapd_eid_country_add(pos, end, chan_spacing,
166 /* Start new group */
171 pos = hostapd_eid_country_add(pos, end, chan_spacing,
175 if ((pos - eid) & 1) {
178 *pos++ = 0; /* pad for 16-bit alignment */
181 eid[1] = (pos - eid) - 2;
187 static u8 * hostapd_eid_wpa(struct hostapd_data *hapd, u8 *eid, size_t len)
192 ie = wpa_auth_get_wpa_ie(hapd->wpa_auth, &ielen);
193 if (ie == NULL || ielen > len)
196 os_memcpy(eid, ie, ielen);
201 void handle_probe_req(struct hostapd_data *hapd,
202 const struct ieee80211_mgmt *mgmt, size_t len)
204 struct ieee80211_mgmt *resp;
205 struct ieee802_11_elems elems;
209 size_t ssid_len, ie_len;
210 struct sta_info *sta = NULL;
214 ie = mgmt->u.probe_req.variable;
215 if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.probe_req))
217 ie_len = len - (IEEE80211_HDRLEN + sizeof(mgmt->u.probe_req));
219 for (i = 0; hapd->probereq_cb && i < hapd->num_probereq_cb; i++)
220 if (hapd->probereq_cb[i].cb(hapd->probereq_cb[i].ctx,
221 mgmt->sa, mgmt->da, mgmt->bssid,
225 if (!hapd->iconf->send_probe_response)
228 if (ieee802_11_parse_elems(ie, ie_len, &elems, 0) == ParseFailed) {
229 wpa_printf(MSG_DEBUG, "Could not parse ProbeReq from " MACSTR,
237 if ((!elems.ssid || !elems.supp_rates)) {
238 wpa_printf(MSG_DEBUG, "STA " MACSTR " sent probe request "
239 "without SSID or supported rates element",
245 if (hapd->p2p && elems.wps_ie) {
247 wps = ieee802_11_vendor_ie_concat(ie, ie_len, WPS_DEV_OUI_WFA);
248 if (wps && !p2p_group_match_dev_type(hapd->p2p_group, wps)) {
249 wpa_printf(MSG_MSGDUMP, "P2P: Ignore Probe Request "
250 "due to mismatch with Requested Device "
257 #endif /* CONFIG_P2P */
259 if (hapd->conf->ignore_broadcast_ssid && elems.ssid_len == 0) {
260 wpa_printf(MSG_MSGDUMP, "Probe Request from " MACSTR " for "
261 "broadcast SSID ignored", MAC2STR(mgmt->sa));
265 sta = ap_get_sta(hapd, mgmt->sa);
268 if ((hapd->conf->p2p & P2P_GROUP_OWNER) &&
269 elems.ssid_len == P2P_WILDCARD_SSID_LEN &&
270 os_memcmp(elems.ssid, P2P_WILDCARD_SSID,
271 P2P_WILDCARD_SSID_LEN) == 0) {
272 /* Process P2P Wildcard SSID like Wildcard SSID */
275 #endif /* CONFIG_P2P */
277 if (elems.ssid_len == 0 ||
278 (elems.ssid_len == hapd->conf->ssid.ssid_len &&
279 os_memcmp(elems.ssid, hapd->conf->ssid.ssid, elems.ssid_len) ==
281 ssid = hapd->conf->ssid.ssid;
282 ssid_len = hapd->conf->ssid.ssid_len;
284 sta->ssid_probe = &hapd->conf->ssid;
288 if (!(mgmt->da[0] & 0x01)) {
290 ieee802_11_print_ssid(ssid_txt, elems.ssid,
292 wpa_printf(MSG_MSGDUMP, "Probe Request from " MACSTR
293 " for foreign SSID '%s' (DA " MACSTR ")",
294 MAC2STR(mgmt->sa), ssid_txt,
300 /* TODO: verify that supp_rates contains at least one matching rate
301 * with AP configuration */
302 #define MAX_PROBERESP_LEN 768
303 buflen = MAX_PROBERESP_LEN;
305 if (hapd->wps_probe_resp_ie)
306 buflen += wpabuf_len(hapd->wps_probe_resp_ie);
307 #endif /* CONFIG_WPS */
309 if (hapd->p2p_probe_resp_ie)
310 buflen += wpabuf_len(hapd->p2p_probe_resp_ie);
311 #endif /* CONFIG_P2P */
312 resp = os_zalloc(buflen);
315 epos = ((u8 *) resp) + MAX_PROBERESP_LEN;
317 resp->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
318 WLAN_FC_STYPE_PROBE_RESP);
319 os_memcpy(resp->da, mgmt->sa, ETH_ALEN);
320 os_memcpy(resp->sa, hapd->own_addr, ETH_ALEN);
322 os_memcpy(resp->bssid, hapd->own_addr, ETH_ALEN);
323 resp->u.probe_resp.beacon_int =
324 host_to_le16(hapd->iconf->beacon_int);
326 /* hardware or low-level driver will setup seq_ctrl and timestamp */
327 resp->u.probe_resp.capab_info =
328 host_to_le16(hostapd_own_capab_info(hapd, sta, 1));
330 pos = resp->u.probe_resp.variable;
331 *pos++ = WLAN_EID_SSID;
333 os_memcpy(pos, ssid, ssid_len);
336 /* Supported rates */
337 pos = hostapd_eid_supp_rates(hapd, pos);
340 pos = hostapd_eid_ds_params(hapd, pos);
342 pos = hostapd_eid_country(hapd, pos, epos - pos);
344 /* ERP Information element */
345 pos = hostapd_eid_erp_info(hapd, pos);
347 /* Extended supported rates */
348 pos = hostapd_eid_ext_supp_rates(hapd, pos);
351 pos = hostapd_eid_wpa(hapd, pos, epos - pos);
353 #ifdef CONFIG_IEEE80211N
354 pos = hostapd_eid_ht_capabilities(hapd, pos);
355 pos = hostapd_eid_ht_operation(hapd, pos);
356 #endif /* CONFIG_IEEE80211N */
358 pos = hostapd_eid_ext_capab(hapd, pos);
360 pos = hostapd_eid_time_adv(hapd, pos);
361 pos = hostapd_eid_time_zone(hapd, pos);
363 pos = hostapd_eid_interworking(hapd, pos);
364 pos = hostapd_eid_adv_proto(hapd, pos);
365 pos = hostapd_eid_roaming_consortium(hapd, pos);
367 /* Wi-Fi Alliance WMM */
368 pos = hostapd_eid_wmm(hapd, pos);
371 if (hapd->conf->wps_state && hapd->wps_probe_resp_ie) {
372 os_memcpy(pos, wpabuf_head(hapd->wps_probe_resp_ie),
373 wpabuf_len(hapd->wps_probe_resp_ie));
374 pos += wpabuf_len(hapd->wps_probe_resp_ie);
376 #endif /* CONFIG_WPS */
379 if ((hapd->conf->p2p & P2P_ENABLED) && elems.p2p &&
380 hapd->p2p_probe_resp_ie) {
381 os_memcpy(pos, wpabuf_head(hapd->p2p_probe_resp_ie),
382 wpabuf_len(hapd->p2p_probe_resp_ie));
383 pos += wpabuf_len(hapd->p2p_probe_resp_ie);
385 #endif /* CONFIG_P2P */
386 #ifdef CONFIG_P2P_MANAGER
387 if ((hapd->conf->p2p & (P2P_MANAGE | P2P_ENABLED | P2P_GROUP_OWNER)) ==
389 pos = hostapd_eid_p2p_manage(hapd, pos);
390 #endif /* CONFIG_P2P_MANAGER */
392 if (hostapd_drv_send_mlme(hapd, resp, pos - (u8 *) resp) < 0)
393 perror("handle_probe_req: send");
397 wpa_printf(MSG_EXCESSIVE, "STA " MACSTR " sent probe request for %s "
398 "SSID", MAC2STR(mgmt->sa),
399 elems.ssid_len == 0 ? "broadcast" : "our");
402 #endif /* NEED_AP_MLME */
405 void ieee802_11_set_beacon(struct hostapd_data *hapd)
407 struct ieee80211_mgmt *head = NULL;
409 size_t head_len = 0, tail_len = 0;
410 struct wpa_driver_ap_params params;
411 struct wpabuf *beacon, *proberesp, *assocresp;
415 #endif /* NEED_AP_MLME */
417 hapd->beacon_set_done = 1;
421 #define BEACON_HEAD_BUF_SIZE 256
422 #define BEACON_TAIL_BUF_SIZE 512
423 head = os_zalloc(BEACON_HEAD_BUF_SIZE);
424 tail_len = BEACON_TAIL_BUF_SIZE;
426 if (hapd->conf->wps_state && hapd->wps_beacon_ie)
427 tail_len += wpabuf_len(hapd->wps_beacon_ie);
428 #endif /* CONFIG_WPS */
430 if (hapd->p2p_beacon_ie)
431 tail_len += wpabuf_len(hapd->p2p_beacon_ie);
432 #endif /* CONFIG_P2P */
433 tailpos = tail = os_malloc(tail_len);
434 if (head == NULL || tail == NULL) {
435 wpa_printf(MSG_ERROR, "Failed to set beacon data");
441 head->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
442 WLAN_FC_STYPE_BEACON);
443 head->duration = host_to_le16(0);
444 os_memset(head->da, 0xff, ETH_ALEN);
446 os_memcpy(head->sa, hapd->own_addr, ETH_ALEN);
447 os_memcpy(head->bssid, hapd->own_addr, ETH_ALEN);
448 head->u.beacon.beacon_int =
449 host_to_le16(hapd->iconf->beacon_int);
451 /* hardware or low-level driver will setup seq_ctrl and timestamp */
452 capab_info = hostapd_own_capab_info(hapd, NULL, 0);
453 head->u.beacon.capab_info = host_to_le16(capab_info);
454 pos = &head->u.beacon.variable[0];
457 *pos++ = WLAN_EID_SSID;
458 if (hapd->conf->ignore_broadcast_ssid == 2) {
459 /* clear the data, but keep the correct length of the SSID */
460 *pos++ = hapd->conf->ssid.ssid_len;
461 os_memset(pos, 0, hapd->conf->ssid.ssid_len);
462 pos += hapd->conf->ssid.ssid_len;
463 } else if (hapd->conf->ignore_broadcast_ssid) {
464 *pos++ = 0; /* empty SSID */
466 *pos++ = hapd->conf->ssid.ssid_len;
467 os_memcpy(pos, hapd->conf->ssid.ssid,
468 hapd->conf->ssid.ssid_len);
469 pos += hapd->conf->ssid.ssid_len;
472 /* Supported rates */
473 pos = hostapd_eid_supp_rates(hapd, pos);
476 pos = hostapd_eid_ds_params(hapd, pos);
478 head_len = pos - (u8 *) head;
480 tailpos = hostapd_eid_country(hapd, tailpos,
481 tail + BEACON_TAIL_BUF_SIZE - tailpos);
483 /* ERP Information element */
484 tailpos = hostapd_eid_erp_info(hapd, tailpos);
486 /* Extended supported rates */
487 tailpos = hostapd_eid_ext_supp_rates(hapd, tailpos);
490 tailpos = hostapd_eid_wpa(hapd, tailpos, tail + BEACON_TAIL_BUF_SIZE -
493 #ifdef CONFIG_IEEE80211N
494 tailpos = hostapd_eid_ht_capabilities(hapd, tailpos);
495 tailpos = hostapd_eid_ht_operation(hapd, tailpos);
496 #endif /* CONFIG_IEEE80211N */
498 tailpos = hostapd_eid_ext_capab(hapd, tailpos);
501 * TODO: Time Advertisement element should only be included in some
502 * DTIM Beacon frames.
504 tailpos = hostapd_eid_time_adv(hapd, tailpos);
506 tailpos = hostapd_eid_interworking(hapd, tailpos);
507 tailpos = hostapd_eid_adv_proto(hapd, tailpos);
508 tailpos = hostapd_eid_roaming_consortium(hapd, tailpos);
510 /* Wi-Fi Alliance WMM */
511 tailpos = hostapd_eid_wmm(hapd, tailpos);
514 if (hapd->conf->wps_state && hapd->wps_beacon_ie) {
515 os_memcpy(tailpos, wpabuf_head(hapd->wps_beacon_ie),
516 wpabuf_len(hapd->wps_beacon_ie));
517 tailpos += wpabuf_len(hapd->wps_beacon_ie);
519 #endif /* CONFIG_WPS */
522 if ((hapd->conf->p2p & P2P_ENABLED) && hapd->p2p_beacon_ie) {
523 os_memcpy(tailpos, wpabuf_head(hapd->p2p_beacon_ie),
524 wpabuf_len(hapd->p2p_beacon_ie));
525 tailpos += wpabuf_len(hapd->p2p_beacon_ie);
527 #endif /* CONFIG_P2P */
528 #ifdef CONFIG_P2P_MANAGER
529 if ((hapd->conf->p2p & (P2P_MANAGE | P2P_ENABLED | P2P_GROUP_OWNER)) ==
531 tailpos = hostapd_eid_p2p_manage(hapd, tailpos);
532 #endif /* CONFIG_P2P_MANAGER */
534 tail_len = tailpos > tail ? tailpos - tail : 0;
536 #endif /* NEED_AP_MLME */
538 os_memset(¶ms, 0, sizeof(params));
539 params.head = (u8 *) head;
540 params.head_len = head_len;
542 params.tail_len = tail_len;
543 params.dtim_period = hapd->conf->dtim_period;
544 params.beacon_int = hapd->iconf->beacon_int;
545 params.ssid = (u8 *) hapd->conf->ssid.ssid;
546 params.ssid_len = hapd->conf->ssid.ssid_len;
547 params.pairwise_ciphers = hapd->conf->rsn_pairwise ?
548 hapd->conf->rsn_pairwise : hapd->conf->wpa_pairwise;
549 params.group_cipher = hapd->conf->wpa_group;
550 params.key_mgmt_suites = hapd->conf->wpa_key_mgmt;
551 params.auth_algs = hapd->conf->auth_algs;
552 params.wpa_version = hapd->conf->wpa;
553 params.privacy = hapd->conf->ssid.wep.keys_set || hapd->conf->wpa ||
554 (hapd->conf->ieee802_1x &&
555 (hapd->conf->default_wep_key_len ||
556 hapd->conf->individual_wep_key_len));
557 switch (hapd->conf->ignore_broadcast_ssid) {
559 params.hide_ssid = NO_SSID_HIDING;
562 params.hide_ssid = HIDDEN_SSID_ZERO_LEN;
565 params.hide_ssid = HIDDEN_SSID_ZERO_CONTENTS;
568 hostapd_build_ap_extra_ies(hapd, &beacon, &proberesp, &assocresp);
569 params.beacon_ies = beacon;
570 params.proberesp_ies = proberesp;
571 params.assocresp_ies = assocresp;
572 params.isolate = hapd->conf->isolate;
574 params.cts_protect = !!(ieee802_11_erp_info(hapd) &
575 ERP_INFO_USE_PROTECTION);
576 params.preamble = hapd->iface->num_sta_no_short_preamble == 0 &&
577 hapd->iconf->preamble == SHORT_PREAMBLE;
578 if (hapd->iface->current_mode &&
579 hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G)
580 params.short_slot_time =
581 hapd->iface->num_sta_no_short_slot_time > 0 ? 0 : 1;
583 params.short_slot_time = -1;
584 if (!hapd->iconf->ieee80211n || hapd->conf->disable_11n)
585 params.ht_opmode = -1;
587 params.ht_opmode = hapd->iface->ht_op_mode;
588 #endif /* NEED_AP_MLME */
589 params.interworking = hapd->conf->interworking;
590 if (hapd->conf->interworking &&
591 !is_zero_ether_addr(hapd->conf->hessid))
592 params.hessid = hapd->conf->hessid;
593 if (hostapd_drv_set_ap(hapd, ¶ms))
594 wpa_printf(MSG_ERROR, "Failed to set beacon parameters");
595 hostapd_free_ap_extra_ies(hapd, beacon, proberesp, assocresp);
602 void ieee802_11_set_beacons(struct hostapd_iface *iface)
605 for (i = 0; i < iface->num_bss; i++)
606 ieee802_11_set_beacon(iface->bss[i]);
609 #endif /* CONFIG_NATIVE_WINDOWS */