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 if (hapd->iface->olbc)
49 erp |= ERP_INFO_USE_PROTECTION;
50 if (hapd->iface->num_sta_non_erp > 0) {
51 erp |= ERP_INFO_NON_ERP_PRESENT |
52 ERP_INFO_USE_PROTECTION;
54 if (hapd->iface->num_sta_no_short_preamble > 0 ||
55 hapd->iconf->preamble == LONG_PREAMBLE)
56 erp |= ERP_INFO_BARKER_PREAMBLE_MODE;
62 static u8 * hostapd_eid_ds_params(struct hostapd_data *hapd, u8 *eid)
64 *eid++ = WLAN_EID_DS_PARAMS;
66 *eid++ = hapd->iconf->channel;
71 static u8 * hostapd_eid_erp_info(struct hostapd_data *hapd, u8 *eid)
73 if (hapd->iface->current_mode == NULL ||
74 hapd->iface->current_mode->mode != HOSTAPD_MODE_IEEE80211G)
77 /* Set NonERP_present and use_protection bits if there
78 * are any associated NonERP stations. */
79 /* TODO: use_protection bit can be set to zero even if
80 * there are NonERP stations present. This optimization
81 * might be useful if NonERP stations are "quiet".
82 * See 802.11g/D6 E-1 for recommended practice.
83 * In addition, Non ERP present might be set, if AP detects Non ERP
84 * operation on other APs. */
86 /* Add ERP Information element */
87 *eid++ = WLAN_EID_ERP_INFO;
89 *eid++ = ieee802_11_erp_info(hapd);
95 static u8 * hostapd_eid_country_add(u8 *pos, u8 *end, int chan_spacing,
96 struct hostapd_channel_data *start,
97 struct hostapd_channel_data *prev)
102 /* first channel number */
103 *pos++ = start->chan;
104 /* number of channels */
105 *pos++ = (prev->chan - start->chan) / chan_spacing + 1;
106 /* maximum transmit power level */
107 *pos++ = start->max_tx_power;
113 static u8 * hostapd_eid_country(struct hostapd_data *hapd, u8 *eid,
117 u8 *end = eid + max_len;
119 struct hostapd_hw_modes *mode;
120 struct hostapd_channel_data *start, *prev;
121 int chan_spacing = 1;
123 if (!hapd->iconf->ieee80211d || max_len < 6 ||
124 hapd->iface->current_mode == NULL)
127 *pos++ = WLAN_EID_COUNTRY;
128 pos++; /* length will be set later */
129 os_memcpy(pos, hapd->iconf->country, 3); /* e.g., 'US ' */
132 mode = hapd->iface->current_mode;
133 if (mode->mode == HOSTAPD_MODE_IEEE80211A)
137 for (i = 0; i < mode->num_channels; i++) {
138 struct hostapd_channel_data *chan = &mode->channels[i];
139 if (chan->flag & HOSTAPD_CHAN_DISABLED)
142 prev->chan + chan_spacing == chan->chan &&
143 start->max_tx_power == chan->max_tx_power) {
145 continue; /* can use same entry */
149 pos = hostapd_eid_country_add(pos, end, chan_spacing,
154 /* Start new group */
159 pos = hostapd_eid_country_add(pos, end, chan_spacing,
163 if ((pos - eid) & 1) {
166 *pos++ = 0; /* pad for 16-bit alignment */
169 eid[1] = (pos - eid) - 2;
175 static u8 * hostapd_eid_wpa(struct hostapd_data *hapd, u8 *eid, size_t len)
180 ie = wpa_auth_get_wpa_ie(hapd->wpa_auth, &ielen);
181 if (ie == NULL || ielen > len)
184 os_memcpy(eid, ie, ielen);
189 void handle_probe_req(struct hostapd_data *hapd,
190 const struct ieee80211_mgmt *mgmt, size_t len)
192 struct ieee80211_mgmt *resp;
193 struct ieee802_11_elems elems;
197 size_t ssid_len, ie_len;
198 struct sta_info *sta = NULL;
202 ie = mgmt->u.probe_req.variable;
203 if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.probe_req))
205 ie_len = len - (IEEE80211_HDRLEN + sizeof(mgmt->u.probe_req));
207 for (i = 0; hapd->probereq_cb && i < hapd->num_probereq_cb; i++)
208 if (hapd->probereq_cb[i].cb(hapd->probereq_cb[i].ctx,
209 mgmt->sa, mgmt->da, mgmt->bssid,
213 if (!hapd->iconf->send_probe_response)
216 if (ieee802_11_parse_elems(ie, ie_len, &elems, 0) == ParseFailed) {
217 wpa_printf(MSG_DEBUG, "Could not parse ProbeReq from " MACSTR,
225 if ((!elems.ssid || !elems.supp_rates)) {
226 wpa_printf(MSG_DEBUG, "STA " MACSTR " sent probe request "
227 "without SSID or supported rates element",
233 if (hapd->p2p && elems.wps_ie) {
235 wps = ieee802_11_vendor_ie_concat(ie, ie_len, WPS_DEV_OUI_WFA);
236 if (wps && !p2p_group_match_dev_type(hapd->p2p_group, wps)) {
237 wpa_printf(MSG_MSGDUMP, "P2P: Ignore Probe Request "
238 "due to mismatch with Requested Device "
245 #endif /* CONFIG_P2P */
247 if (hapd->conf->ignore_broadcast_ssid && elems.ssid_len == 0) {
248 wpa_printf(MSG_MSGDUMP, "Probe Request from " MACSTR " for "
249 "broadcast SSID ignored", MAC2STR(mgmt->sa));
253 sta = ap_get_sta(hapd, mgmt->sa);
256 if ((hapd->conf->p2p & P2P_GROUP_OWNER) &&
257 elems.ssid_len == P2P_WILDCARD_SSID_LEN &&
258 os_memcmp(elems.ssid, P2P_WILDCARD_SSID,
259 P2P_WILDCARD_SSID_LEN) == 0) {
260 /* Process P2P Wildcard SSID like Wildcard SSID */
263 #endif /* CONFIG_P2P */
265 if (elems.ssid_len == 0 ||
266 (elems.ssid_len == hapd->conf->ssid.ssid_len &&
267 os_memcmp(elems.ssid, hapd->conf->ssid.ssid, elems.ssid_len) ==
269 ssid = hapd->conf->ssid.ssid;
270 ssid_len = hapd->conf->ssid.ssid_len;
272 sta->ssid_probe = &hapd->conf->ssid;
276 if (!(mgmt->da[0] & 0x01)) {
278 ieee802_11_print_ssid(ssid_txt, elems.ssid,
280 wpa_printf(MSG_MSGDUMP, "Probe Request from " MACSTR
281 " for foreign SSID '%s' (DA " MACSTR ")",
282 MAC2STR(mgmt->sa), ssid_txt,
288 #ifdef CONFIG_INTERWORKING
289 if (elems.interworking && elems.interworking_len >= 1) {
290 u8 ant = elems.interworking[0] & 0x0f;
291 if (ant != INTERWORKING_ANT_WILDCARD &&
292 ant != hapd->conf->access_network_type) {
293 wpa_printf(MSG_MSGDUMP, "Probe Request from " MACSTR
294 " for mismatching ANT %u ignored",
295 MAC2STR(mgmt->sa), ant);
300 if (elems.interworking &&
301 (elems.interworking_len == 7 || elems.interworking_len == 9)) {
303 if (elems.interworking_len == 7)
304 hessid = elems.interworking + 1;
306 hessid = elems.interworking + 1 + 2;
307 if (!is_broadcast_ether_addr(hessid) &&
308 os_memcmp(hessid, hapd->conf->hessid, ETH_ALEN) != 0) {
309 wpa_printf(MSG_MSGDUMP, "Probe Request from " MACSTR
310 " for mismatching HESSID " MACSTR
312 MAC2STR(mgmt->sa), MAC2STR(hessid));
316 #endif /* CONFIG_INTERWORKING */
318 /* TODO: verify that supp_rates contains at least one matching rate
319 * with AP configuration */
320 #define MAX_PROBERESP_LEN 768
321 buflen = MAX_PROBERESP_LEN;
323 if (hapd->wps_probe_resp_ie)
324 buflen += wpabuf_len(hapd->wps_probe_resp_ie);
325 #endif /* CONFIG_WPS */
327 if (hapd->p2p_probe_resp_ie)
328 buflen += wpabuf_len(hapd->p2p_probe_resp_ie);
329 #endif /* CONFIG_P2P */
330 resp = os_zalloc(buflen);
333 epos = ((u8 *) resp) + MAX_PROBERESP_LEN;
335 resp->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
336 WLAN_FC_STYPE_PROBE_RESP);
337 os_memcpy(resp->da, mgmt->sa, ETH_ALEN);
338 os_memcpy(resp->sa, hapd->own_addr, ETH_ALEN);
340 os_memcpy(resp->bssid, hapd->own_addr, ETH_ALEN);
341 resp->u.probe_resp.beacon_int =
342 host_to_le16(hapd->iconf->beacon_int);
344 /* hardware or low-level driver will setup seq_ctrl and timestamp */
345 resp->u.probe_resp.capab_info =
346 host_to_le16(hostapd_own_capab_info(hapd, sta, 1));
348 pos = resp->u.probe_resp.variable;
349 *pos++ = WLAN_EID_SSID;
351 os_memcpy(pos, ssid, ssid_len);
354 /* Supported rates */
355 pos = hostapd_eid_supp_rates(hapd, pos);
358 pos = hostapd_eid_ds_params(hapd, pos);
360 pos = hostapd_eid_country(hapd, pos, epos - pos);
362 /* ERP Information element */
363 pos = hostapd_eid_erp_info(hapd, pos);
365 /* Extended supported rates */
366 pos = hostapd_eid_ext_supp_rates(hapd, pos);
369 pos = hostapd_eid_wpa(hapd, pos, epos - pos);
371 #ifdef CONFIG_IEEE80211N
372 pos = hostapd_eid_ht_capabilities(hapd, pos);
373 pos = hostapd_eid_ht_operation(hapd, pos);
374 #endif /* CONFIG_IEEE80211N */
376 pos = hostapd_eid_ext_capab(hapd, pos);
378 pos = hostapd_eid_time_adv(hapd, pos);
379 pos = hostapd_eid_time_zone(hapd, pos);
381 pos = hostapd_eid_interworking(hapd, pos);
382 pos = hostapd_eid_adv_proto(hapd, pos);
383 pos = hostapd_eid_roaming_consortium(hapd, pos);
385 /* Wi-Fi Alliance WMM */
386 pos = hostapd_eid_wmm(hapd, pos);
389 if (hapd->conf->wps_state && hapd->wps_probe_resp_ie) {
390 os_memcpy(pos, wpabuf_head(hapd->wps_probe_resp_ie),
391 wpabuf_len(hapd->wps_probe_resp_ie));
392 pos += wpabuf_len(hapd->wps_probe_resp_ie);
394 #endif /* CONFIG_WPS */
397 if ((hapd->conf->p2p & P2P_ENABLED) && elems.p2p &&
398 hapd->p2p_probe_resp_ie) {
399 os_memcpy(pos, wpabuf_head(hapd->p2p_probe_resp_ie),
400 wpabuf_len(hapd->p2p_probe_resp_ie));
401 pos += wpabuf_len(hapd->p2p_probe_resp_ie);
403 #endif /* CONFIG_P2P */
404 #ifdef CONFIG_P2P_MANAGER
405 if ((hapd->conf->p2p & (P2P_MANAGE | P2P_ENABLED | P2P_GROUP_OWNER)) ==
407 pos = hostapd_eid_p2p_manage(hapd, pos);
408 #endif /* CONFIG_P2P_MANAGER */
410 if (hostapd_drv_send_mlme(hapd, resp, pos - (u8 *) resp, 0) < 0)
411 perror("handle_probe_req: send");
415 wpa_printf(MSG_EXCESSIVE, "STA " MACSTR " sent probe request for %s "
416 "SSID", MAC2STR(mgmt->sa),
417 elems.ssid_len == 0 ? "broadcast" : "our");
420 #endif /* NEED_AP_MLME */
423 void ieee802_11_set_beacon(struct hostapd_data *hapd)
425 struct ieee80211_mgmt *head = NULL;
427 size_t head_len = 0, tail_len = 0;
428 struct wpa_driver_ap_params params;
429 struct wpabuf *beacon, *proberesp, *assocresp;
433 #endif /* NEED_AP_MLME */
435 hapd->beacon_set_done = 1;
439 #define BEACON_HEAD_BUF_SIZE 256
440 #define BEACON_TAIL_BUF_SIZE 512
441 head = os_zalloc(BEACON_HEAD_BUF_SIZE);
442 tail_len = BEACON_TAIL_BUF_SIZE;
444 if (hapd->conf->wps_state && hapd->wps_beacon_ie)
445 tail_len += wpabuf_len(hapd->wps_beacon_ie);
446 #endif /* CONFIG_WPS */
448 if (hapd->p2p_beacon_ie)
449 tail_len += wpabuf_len(hapd->p2p_beacon_ie);
450 #endif /* CONFIG_P2P */
451 tailpos = tail = os_malloc(tail_len);
452 if (head == NULL || tail == NULL) {
453 wpa_printf(MSG_ERROR, "Failed to set beacon data");
459 head->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
460 WLAN_FC_STYPE_BEACON);
461 head->duration = host_to_le16(0);
462 os_memset(head->da, 0xff, ETH_ALEN);
464 os_memcpy(head->sa, hapd->own_addr, ETH_ALEN);
465 os_memcpy(head->bssid, hapd->own_addr, ETH_ALEN);
466 head->u.beacon.beacon_int =
467 host_to_le16(hapd->iconf->beacon_int);
469 /* hardware or low-level driver will setup seq_ctrl and timestamp */
470 capab_info = hostapd_own_capab_info(hapd, NULL, 0);
471 head->u.beacon.capab_info = host_to_le16(capab_info);
472 pos = &head->u.beacon.variable[0];
475 *pos++ = WLAN_EID_SSID;
476 if (hapd->conf->ignore_broadcast_ssid == 2) {
477 /* clear the data, but keep the correct length of the SSID */
478 *pos++ = hapd->conf->ssid.ssid_len;
479 os_memset(pos, 0, hapd->conf->ssid.ssid_len);
480 pos += hapd->conf->ssid.ssid_len;
481 } else if (hapd->conf->ignore_broadcast_ssid) {
482 *pos++ = 0; /* empty SSID */
484 *pos++ = hapd->conf->ssid.ssid_len;
485 os_memcpy(pos, hapd->conf->ssid.ssid,
486 hapd->conf->ssid.ssid_len);
487 pos += hapd->conf->ssid.ssid_len;
490 /* Supported rates */
491 pos = hostapd_eid_supp_rates(hapd, pos);
494 pos = hostapd_eid_ds_params(hapd, pos);
496 head_len = pos - (u8 *) head;
498 tailpos = hostapd_eid_country(hapd, tailpos,
499 tail + BEACON_TAIL_BUF_SIZE - tailpos);
501 /* ERP Information element */
502 tailpos = hostapd_eid_erp_info(hapd, tailpos);
504 /* Extended supported rates */
505 tailpos = hostapd_eid_ext_supp_rates(hapd, tailpos);
508 tailpos = hostapd_eid_wpa(hapd, tailpos, tail + BEACON_TAIL_BUF_SIZE -
511 #ifdef CONFIG_IEEE80211N
512 tailpos = hostapd_eid_ht_capabilities(hapd, tailpos);
513 tailpos = hostapd_eid_ht_operation(hapd, tailpos);
514 #endif /* CONFIG_IEEE80211N */
516 tailpos = hostapd_eid_ext_capab(hapd, tailpos);
519 * TODO: Time Advertisement element should only be included in some
520 * DTIM Beacon frames.
522 tailpos = hostapd_eid_time_adv(hapd, tailpos);
524 tailpos = hostapd_eid_interworking(hapd, tailpos);
525 tailpos = hostapd_eid_adv_proto(hapd, tailpos);
526 tailpos = hostapd_eid_roaming_consortium(hapd, tailpos);
528 /* Wi-Fi Alliance WMM */
529 tailpos = hostapd_eid_wmm(hapd, tailpos);
532 if (hapd->conf->wps_state && hapd->wps_beacon_ie) {
533 os_memcpy(tailpos, wpabuf_head(hapd->wps_beacon_ie),
534 wpabuf_len(hapd->wps_beacon_ie));
535 tailpos += wpabuf_len(hapd->wps_beacon_ie);
537 #endif /* CONFIG_WPS */
540 if ((hapd->conf->p2p & P2P_ENABLED) && hapd->p2p_beacon_ie) {
541 os_memcpy(tailpos, wpabuf_head(hapd->p2p_beacon_ie),
542 wpabuf_len(hapd->p2p_beacon_ie));
543 tailpos += wpabuf_len(hapd->p2p_beacon_ie);
545 #endif /* CONFIG_P2P */
546 #ifdef CONFIG_P2P_MANAGER
547 if ((hapd->conf->p2p & (P2P_MANAGE | P2P_ENABLED | P2P_GROUP_OWNER)) ==
549 tailpos = hostapd_eid_p2p_manage(hapd, tailpos);
550 #endif /* CONFIG_P2P_MANAGER */
552 tail_len = tailpos > tail ? tailpos - tail : 0;
554 #endif /* NEED_AP_MLME */
556 os_memset(¶ms, 0, sizeof(params));
557 params.head = (u8 *) head;
558 params.head_len = head_len;
560 params.tail_len = tail_len;
561 params.dtim_period = hapd->conf->dtim_period;
562 params.beacon_int = hapd->iconf->beacon_int;
563 params.ssid = (u8 *) hapd->conf->ssid.ssid;
564 params.ssid_len = hapd->conf->ssid.ssid_len;
565 params.pairwise_ciphers = hapd->conf->rsn_pairwise ?
566 hapd->conf->rsn_pairwise : hapd->conf->wpa_pairwise;
567 params.group_cipher = hapd->conf->wpa_group;
568 params.key_mgmt_suites = hapd->conf->wpa_key_mgmt;
569 params.auth_algs = hapd->conf->auth_algs;
570 params.wpa_version = hapd->conf->wpa;
571 params.privacy = hapd->conf->ssid.wep.keys_set || hapd->conf->wpa ||
572 (hapd->conf->ieee802_1x &&
573 (hapd->conf->default_wep_key_len ||
574 hapd->conf->individual_wep_key_len));
575 switch (hapd->conf->ignore_broadcast_ssid) {
577 params.hide_ssid = NO_SSID_HIDING;
580 params.hide_ssid = HIDDEN_SSID_ZERO_LEN;
583 params.hide_ssid = HIDDEN_SSID_ZERO_CONTENTS;
586 hostapd_build_ap_extra_ies(hapd, &beacon, &proberesp, &assocresp);
587 params.beacon_ies = beacon;
588 params.proberesp_ies = proberesp;
589 params.assocresp_ies = assocresp;
590 params.isolate = hapd->conf->isolate;
592 params.cts_protect = !!(ieee802_11_erp_info(hapd) &
593 ERP_INFO_USE_PROTECTION);
594 params.preamble = hapd->iface->num_sta_no_short_preamble == 0 &&
595 hapd->iconf->preamble == SHORT_PREAMBLE;
596 if (hapd->iface->current_mode &&
597 hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G)
598 params.short_slot_time =
599 hapd->iface->num_sta_no_short_slot_time > 0 ? 0 : 1;
601 params.short_slot_time = -1;
602 if (!hapd->iconf->ieee80211n || hapd->conf->disable_11n)
603 params.ht_opmode = -1;
605 params.ht_opmode = hapd->iface->ht_op_mode;
606 #endif /* NEED_AP_MLME */
607 params.interworking = hapd->conf->interworking;
608 if (hapd->conf->interworking &&
609 !is_zero_ether_addr(hapd->conf->hessid))
610 params.hessid = hapd->conf->hessid;
611 params.access_network_type = hapd->conf->access_network_type;
612 if (hostapd_drv_set_ap(hapd, ¶ms))
613 wpa_printf(MSG_ERROR, "Failed to set beacon parameters");
614 hostapd_free_ap_extra_ies(hapd, beacon, proberesp, assocresp);
621 void ieee802_11_set_beacons(struct hostapd_iface *iface)
624 for (i = 0; i < iface->num_bss; i++)
625 ieee802_11_set_beacon(iface->bss[i]);
628 #endif /* CONFIG_NATIVE_WINDOWS */