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 static u8 * hostapd_eid_interworking(struct hostapd_data *hapd, u8 *eid)
203 #ifdef CONFIG_INTERWORKING
206 if (!hapd->conf->interworking)
209 *pos++ = WLAN_EID_INTERWORKING;
212 *pos = hapd->conf->access_network_type;
213 if (hapd->conf->internet)
214 *pos |= INTERWORKING_ANO_INTERNET;
215 if (hapd->conf->asra)
216 *pos |= INTERWORKING_ANO_ASRA;
218 *pos |= INTERWORKING_ANO_ESR;
219 if (hapd->conf->uesa)
220 *pos |= INTERWORKING_ANO_UESA;
223 if (hapd->conf->venue_info_set) {
224 *pos++ = hapd->conf->venue_group;
225 *pos++ = hapd->conf->venue_type;
228 if (!is_zero_ether_addr(hapd->conf->hessid)) {
229 os_memcpy(pos, hapd->conf->hessid, ETH_ALEN);
233 *len = pos - len - 1;
234 #endif /* CONFIG_INTERWORKING */
240 void handle_probe_req(struct hostapd_data *hapd,
241 const struct ieee80211_mgmt *mgmt, size_t len)
243 struct ieee80211_mgmt *resp;
244 struct ieee802_11_elems elems;
248 size_t ssid_len, ie_len;
249 struct sta_info *sta = NULL;
253 ie = mgmt->u.probe_req.variable;
254 if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.probe_req))
256 ie_len = len - (IEEE80211_HDRLEN + sizeof(mgmt->u.probe_req));
258 for (i = 0; hapd->probereq_cb && i < hapd->num_probereq_cb; i++)
259 if (hapd->probereq_cb[i].cb(hapd->probereq_cb[i].ctx,
260 mgmt->sa, mgmt->da, mgmt->bssid,
264 if (!hapd->iconf->send_probe_response)
267 if (ieee802_11_parse_elems(ie, ie_len, &elems, 0) == ParseFailed) {
268 wpa_printf(MSG_DEBUG, "Could not parse ProbeReq from " MACSTR,
276 if ((!elems.ssid || !elems.supp_rates)) {
277 wpa_printf(MSG_DEBUG, "STA " MACSTR " sent probe request "
278 "without SSID or supported rates element",
284 if (hapd->p2p && elems.wps_ie) {
286 wps = ieee802_11_vendor_ie_concat(ie, ie_len, WPS_DEV_OUI_WFA);
287 if (wps && !p2p_group_match_dev_type(hapd->p2p_group, wps)) {
288 wpa_printf(MSG_MSGDUMP, "P2P: Ignore Probe Request "
289 "due to mismatch with Requested Device "
296 #endif /* CONFIG_P2P */
298 if (hapd->conf->ignore_broadcast_ssid && elems.ssid_len == 0) {
299 wpa_printf(MSG_MSGDUMP, "Probe Request from " MACSTR " for "
300 "broadcast SSID ignored", MAC2STR(mgmt->sa));
304 sta = ap_get_sta(hapd, mgmt->sa);
307 if ((hapd->conf->p2p & P2P_GROUP_OWNER) &&
308 elems.ssid_len == P2P_WILDCARD_SSID_LEN &&
309 os_memcmp(elems.ssid, P2P_WILDCARD_SSID,
310 P2P_WILDCARD_SSID_LEN) == 0) {
311 /* Process P2P Wildcard SSID like Wildcard SSID */
314 #endif /* CONFIG_P2P */
316 if (elems.ssid_len == 0 ||
317 (elems.ssid_len == hapd->conf->ssid.ssid_len &&
318 os_memcmp(elems.ssid, hapd->conf->ssid.ssid, elems.ssid_len) ==
320 ssid = hapd->conf->ssid.ssid;
321 ssid_len = hapd->conf->ssid.ssid_len;
323 sta->ssid_probe = &hapd->conf->ssid;
327 if (!(mgmt->da[0] & 0x01)) {
329 ieee802_11_print_ssid(ssid_txt, elems.ssid,
331 wpa_printf(MSG_MSGDUMP, "Probe Request from " MACSTR
332 " for foreign SSID '%s' (DA " MACSTR ")",
333 MAC2STR(mgmt->sa), ssid_txt,
339 /* TODO: verify that supp_rates contains at least one matching rate
340 * with AP configuration */
341 #define MAX_PROBERESP_LEN 768
342 buflen = MAX_PROBERESP_LEN;
344 if (hapd->wps_probe_resp_ie)
345 buflen += wpabuf_len(hapd->wps_probe_resp_ie);
346 #endif /* CONFIG_WPS */
348 if (hapd->p2p_probe_resp_ie)
349 buflen += wpabuf_len(hapd->p2p_probe_resp_ie);
350 #endif /* CONFIG_P2P */
351 resp = os_zalloc(buflen);
354 epos = ((u8 *) resp) + MAX_PROBERESP_LEN;
356 resp->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
357 WLAN_FC_STYPE_PROBE_RESP);
358 os_memcpy(resp->da, mgmt->sa, ETH_ALEN);
359 os_memcpy(resp->sa, hapd->own_addr, ETH_ALEN);
361 os_memcpy(resp->bssid, hapd->own_addr, ETH_ALEN);
362 resp->u.probe_resp.beacon_int =
363 host_to_le16(hapd->iconf->beacon_int);
365 /* hardware or low-level driver will setup seq_ctrl and timestamp */
366 resp->u.probe_resp.capab_info =
367 host_to_le16(hostapd_own_capab_info(hapd, sta, 1));
369 pos = resp->u.probe_resp.variable;
370 *pos++ = WLAN_EID_SSID;
372 os_memcpy(pos, ssid, ssid_len);
375 /* Supported rates */
376 pos = hostapd_eid_supp_rates(hapd, pos);
379 pos = hostapd_eid_ds_params(hapd, pos);
381 pos = hostapd_eid_country(hapd, pos, epos - pos);
383 /* ERP Information element */
384 pos = hostapd_eid_erp_info(hapd, pos);
386 /* Extended supported rates */
387 pos = hostapd_eid_ext_supp_rates(hapd, pos);
390 pos = hostapd_eid_wpa(hapd, pos, epos - pos, sta);
392 #ifdef CONFIG_IEEE80211N
393 pos = hostapd_eid_ht_capabilities(hapd, pos);
394 pos = hostapd_eid_ht_operation(hapd, pos);
395 #endif /* CONFIG_IEEE80211N */
397 pos = hostapd_eid_ext_capab(hapd, pos);
399 pos = hostapd_eid_interworking(hapd, pos);
401 /* Wi-Fi Alliance WMM */
402 pos = hostapd_eid_wmm(hapd, pos);
405 if (hapd->conf->wps_state && hapd->wps_probe_resp_ie) {
406 os_memcpy(pos, wpabuf_head(hapd->wps_probe_resp_ie),
407 wpabuf_len(hapd->wps_probe_resp_ie));
408 pos += wpabuf_len(hapd->wps_probe_resp_ie);
410 #endif /* CONFIG_WPS */
413 if ((hapd->conf->p2p & P2P_ENABLED) && elems.p2p &&
414 hapd->p2p_probe_resp_ie) {
415 os_memcpy(pos, wpabuf_head(hapd->p2p_probe_resp_ie),
416 wpabuf_len(hapd->p2p_probe_resp_ie));
417 pos += wpabuf_len(hapd->p2p_probe_resp_ie);
419 #endif /* CONFIG_P2P */
420 #ifdef CONFIG_P2P_MANAGER
421 if ((hapd->conf->p2p & (P2P_MANAGE | P2P_ENABLED | P2P_GROUP_OWNER)) ==
423 pos = hostapd_eid_p2p_manage(hapd, pos);
424 #endif /* CONFIG_P2P_MANAGER */
426 if (hostapd_drv_send_mlme(hapd, resp, pos - (u8 *) resp) < 0)
427 perror("handle_probe_req: send");
431 wpa_printf(MSG_EXCESSIVE, "STA " MACSTR " sent probe request for %s "
432 "SSID", MAC2STR(mgmt->sa),
433 elems.ssid_len == 0 ? "broadcast" : "our");
437 void ieee802_11_set_beacon(struct hostapd_data *hapd)
439 struct ieee80211_mgmt *head;
440 u8 *pos, *tail, *tailpos;
442 size_t head_len, tail_len;
443 struct wpa_driver_ap_params params;
444 struct wpabuf *beacon, *proberesp, *assocresp;
447 if ((hapd->conf->p2p & (P2P_ENABLED | P2P_GROUP_OWNER)) == P2P_ENABLED)
449 #endif /* CONFIG_P2P */
450 hapd->beacon_set_done = 1;
452 #define BEACON_HEAD_BUF_SIZE 256
453 #define BEACON_TAIL_BUF_SIZE 512
454 head = os_zalloc(BEACON_HEAD_BUF_SIZE);
455 tail_len = BEACON_TAIL_BUF_SIZE;
457 if (hapd->conf->wps_state && hapd->wps_beacon_ie)
458 tail_len += wpabuf_len(hapd->wps_beacon_ie);
459 #endif /* CONFIG_WPS */
461 if (hapd->p2p_beacon_ie)
462 tail_len += wpabuf_len(hapd->p2p_beacon_ie);
463 #endif /* CONFIG_P2P */
464 tailpos = tail = os_malloc(tail_len);
465 if (head == NULL || tail == NULL) {
466 wpa_printf(MSG_ERROR, "Failed to set beacon data");
472 head->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
473 WLAN_FC_STYPE_BEACON);
474 head->duration = host_to_le16(0);
475 os_memset(head->da, 0xff, ETH_ALEN);
477 os_memcpy(head->sa, hapd->own_addr, ETH_ALEN);
478 os_memcpy(head->bssid, hapd->own_addr, ETH_ALEN);
479 head->u.beacon.beacon_int =
480 host_to_le16(hapd->iconf->beacon_int);
482 /* hardware or low-level driver will setup seq_ctrl and timestamp */
483 capab_info = hostapd_own_capab_info(hapd, NULL, 0);
484 head->u.beacon.capab_info = host_to_le16(capab_info);
485 pos = &head->u.beacon.variable[0];
488 *pos++ = WLAN_EID_SSID;
489 if (hapd->conf->ignore_broadcast_ssid == 2) {
490 /* clear the data, but keep the correct length of the SSID */
491 *pos++ = hapd->conf->ssid.ssid_len;
492 os_memset(pos, 0, hapd->conf->ssid.ssid_len);
493 pos += hapd->conf->ssid.ssid_len;
494 } else if (hapd->conf->ignore_broadcast_ssid) {
495 *pos++ = 0; /* empty SSID */
497 *pos++ = hapd->conf->ssid.ssid_len;
498 os_memcpy(pos, hapd->conf->ssid.ssid,
499 hapd->conf->ssid.ssid_len);
500 pos += hapd->conf->ssid.ssid_len;
503 /* Supported rates */
504 pos = hostapd_eid_supp_rates(hapd, pos);
507 pos = hostapd_eid_ds_params(hapd, pos);
509 head_len = pos - (u8 *) head;
511 tailpos = hostapd_eid_country(hapd, tailpos,
512 tail + BEACON_TAIL_BUF_SIZE - tailpos);
514 /* ERP Information element */
515 tailpos = hostapd_eid_erp_info(hapd, tailpos);
517 /* Extended supported rates */
518 tailpos = hostapd_eid_ext_supp_rates(hapd, tailpos);
521 tailpos = hostapd_eid_wpa(hapd, tailpos, tail + BEACON_TAIL_BUF_SIZE -
524 #ifdef CONFIG_IEEE80211N
525 tailpos = hostapd_eid_ht_capabilities(hapd, tailpos);
526 tailpos = hostapd_eid_ht_operation(hapd, tailpos);
527 #endif /* CONFIG_IEEE80211N */
529 tailpos = hostapd_eid_ext_capab(hapd, tailpos);
531 tailpos = hostapd_eid_interworking(hapd, tailpos);
533 /* Wi-Fi Alliance WMM */
534 tailpos = hostapd_eid_wmm(hapd, tailpos);
537 if (hapd->conf->wps_state && hapd->wps_beacon_ie) {
538 os_memcpy(tailpos, wpabuf_head(hapd->wps_beacon_ie),
539 wpabuf_len(hapd->wps_beacon_ie));
540 tailpos += wpabuf_len(hapd->wps_beacon_ie);
542 #endif /* CONFIG_WPS */
545 if ((hapd->conf->p2p & P2P_ENABLED) && hapd->p2p_beacon_ie) {
546 os_memcpy(tailpos, wpabuf_head(hapd->p2p_beacon_ie),
547 wpabuf_len(hapd->p2p_beacon_ie));
548 tailpos += wpabuf_len(hapd->p2p_beacon_ie);
550 #endif /* CONFIG_P2P */
551 #ifdef CONFIG_P2P_MANAGER
552 if ((hapd->conf->p2p & (P2P_MANAGE | P2P_ENABLED | P2P_GROUP_OWNER)) ==
554 tailpos = hostapd_eid_p2p_manage(hapd, tailpos);
555 #endif /* CONFIG_P2P_MANAGER */
557 tail_len = tailpos > tail ? tailpos - tail : 0;
559 os_memset(¶ms, 0, sizeof(params));
560 params.head = (u8 *) head;
561 params.head_len = head_len;
563 params.tail_len = tail_len;
564 params.dtim_period = hapd->conf->dtim_period;
565 params.beacon_int = hapd->iconf->beacon_int;
566 params.ssid = (u8 *) hapd->conf->ssid.ssid;
567 params.ssid_len = hapd->conf->ssid.ssid_len;
568 params.pairwise_ciphers = hapd->conf->rsn_pairwise ?
569 hapd->conf->rsn_pairwise : hapd->conf->wpa_pairwise;
570 params.group_cipher = hapd->conf->wpa_group;
571 params.key_mgmt_suites = hapd->conf->wpa_key_mgmt;
572 params.auth_algs = hapd->conf->auth_algs;
573 params.wpa_version = hapd->conf->wpa;
574 params.privacy = hapd->conf->ssid.wep.keys_set || hapd->conf->wpa ||
575 (hapd->conf->ieee802_1x &&
576 (hapd->conf->default_wep_key_len ||
577 hapd->conf->individual_wep_key_len));
578 switch (hapd->conf->ignore_broadcast_ssid) {
580 params.hide_ssid = NO_SSID_HIDING;
583 params.hide_ssid = HIDDEN_SSID_ZERO_LEN;
586 params.hide_ssid = HIDDEN_SSID_ZERO_CONTENTS;
589 hostapd_build_ap_extra_ies(hapd, &beacon, &proberesp, &assocresp);
590 params.beacon_ies = beacon;
591 params.proberesp_ies = proberesp;
592 params.assocresp_ies = assocresp;
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 #endif /* CONFIG_P2P */
603 hostapd_set_bss_params(hapd, !!(ieee802_11_erp_info(hapd) &
604 ERP_INFO_USE_PROTECTION));
608 void ieee802_11_set_beacons(struct hostapd_iface *iface)
611 for (i = 0; i < iface->num_bss; i++)
612 ieee802_11_set_beacon(iface->bss[i]);
615 #endif /* CONFIG_NATIVE_WINDOWS */