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"
37 static u8
ieee802_11_erp_info(struct hostapd_data
*hapd
)
41 if (hapd
->iface
->current_mode
== NULL
||
42 hapd
->iface
->current_mode
->mode
!= HOSTAPD_MODE_IEEE80211G
)
45 switch (hapd
->iconf
->cts_protection_type
) {
46 case CTS_PROTECTION_FORCE_ENABLED
:
47 erp
|= ERP_INFO_NON_ERP_PRESENT
| ERP_INFO_USE_PROTECTION
;
49 case CTS_PROTECTION_FORCE_DISABLED
:
52 case CTS_PROTECTION_AUTOMATIC
:
53 if (hapd
->iface
->olbc
)
54 erp
|= ERP_INFO_USE_PROTECTION
;
56 case CTS_PROTECTION_AUTOMATIC_NO_OLBC
:
57 if (hapd
->iface
->num_sta_non_erp
> 0) {
58 erp
|= ERP_INFO_NON_ERP_PRESENT
|
59 ERP_INFO_USE_PROTECTION
;
63 if (hapd
->iface
->num_sta_no_short_preamble
> 0 ||
64 hapd
->iconf
->preamble
== LONG_PREAMBLE
)
65 erp
|= ERP_INFO_BARKER_PREAMBLE_MODE
;
71 static u8
* hostapd_eid_ds_params(struct hostapd_data
*hapd
, u8
*eid
)
73 *eid
++ = WLAN_EID_DS_PARAMS
;
75 *eid
++ = hapd
->iconf
->channel
;
80 static u8
* hostapd_eid_erp_info(struct hostapd_data
*hapd
, u8
*eid
)
82 if (hapd
->iface
->current_mode
== NULL
||
83 hapd
->iface
->current_mode
->mode
!= HOSTAPD_MODE_IEEE80211G
)
86 /* Set NonERP_present and use_protection bits if there
87 * are any associated NonERP stations. */
88 /* TODO: use_protection bit can be set to zero even if
89 * there are NonERP stations present. This optimization
90 * might be useful if NonERP stations are "quiet".
91 * See 802.11g/D6 E-1 for recommended practice.
92 * In addition, Non ERP present might be set, if AP detects Non ERP
93 * operation on other APs. */
95 /* Add ERP Information element */
96 *eid
++ = WLAN_EID_ERP_INFO
;
98 *eid
++ = ieee802_11_erp_info(hapd
);
104 static u8
* hostapd_eid_country_add(u8
*pos
, u8
*end
, int chan_spacing
,
105 struct hostapd_channel_data
*start
,
106 struct hostapd_channel_data
*prev
)
111 /* first channel number */
112 *pos
++ = start
->chan
;
113 /* number of channels */
114 *pos
++ = (prev
->chan
- start
->chan
) / chan_spacing
+ 1;
115 /* maximum transmit power level */
116 *pos
++ = start
->max_tx_power
;
122 static u8
* hostapd_eid_country(struct hostapd_data
*hapd
, u8
*eid
,
126 u8
*end
= eid
+ max_len
;
128 struct hostapd_hw_modes
*mode
;
129 struct hostapd_channel_data
*start
, *prev
;
130 int chan_spacing
= 1;
132 if (!hapd
->iconf
->ieee80211d
|| max_len
< 6 ||
133 hapd
->iface
->current_mode
== NULL
)
136 *pos
++ = WLAN_EID_COUNTRY
;
137 pos
++; /* length will be set later */
138 os_memcpy(pos
, hapd
->iconf
->country
, 3); /* e.g., 'US ' */
141 mode
= hapd
->iface
->current_mode
;
142 if (mode
->mode
== HOSTAPD_MODE_IEEE80211A
)
146 for (i
= 0; i
< mode
->num_channels
; i
++) {
147 struct hostapd_channel_data
*chan
= &mode
->channels
[i
];
148 if (chan
->flag
& HOSTAPD_CHAN_DISABLED
)
151 prev
->chan
+ chan_spacing
== chan
->chan
&&
152 start
->max_tx_power
== chan
->max_tx_power
) {
154 continue; /* can use same entry */
158 pos
= hostapd_eid_country_add(pos
, end
, chan_spacing
,
163 /* Start new group */
168 pos
= hostapd_eid_country_add(pos
, end
, chan_spacing
,
172 if ((pos
- eid
) & 1) {
175 *pos
++ = 0; /* pad for 16-bit alignment */
178 eid
[1] = (pos
- eid
) - 2;
184 static u8
* hostapd_eid_wpa(struct hostapd_data
*hapd
, u8
*eid
, size_t len
,
185 struct sta_info
*sta
)
190 ie
= wpa_auth_get_wpa_ie(hapd
->wpa_auth
, &ielen
);
191 if (ie
== NULL
|| ielen
> len
)
194 os_memcpy(eid
, ie
, ielen
);
199 void handle_probe_req(struct hostapd_data
*hapd
,
200 const struct ieee80211_mgmt
*mgmt
, size_t len
)
202 struct ieee80211_mgmt
*resp
;
203 struct ieee802_11_elems elems
;
207 size_t ssid_len
, ie_len
;
208 struct sta_info
*sta
= NULL
;
212 ie
= mgmt
->u
.probe_req
.variable
;
213 ie_len
= len
- (IEEE80211_HDRLEN
+ sizeof(mgmt
->u
.probe_req
));
215 for (i
= 0; hapd
->probereq_cb
&& i
< hapd
->num_probereq_cb
; i
++)
216 if (hapd
->probereq_cb
[i
].cb(hapd
->probereq_cb
[i
].ctx
,
217 mgmt
->sa
, ie
, ie_len
) > 0)
220 if (!hapd
->iconf
->send_probe_response
)
223 if (ieee802_11_parse_elems(ie
, ie_len
, &elems
, 0) == ParseFailed
) {
224 wpa_printf(MSG_DEBUG
, "Could not parse ProbeReq from " MACSTR
,
232 if ((!elems
.ssid
|| !elems
.supp_rates
)) {
233 wpa_printf(MSG_DEBUG
, "STA " MACSTR
" sent probe request "
234 "without SSID or supported rates element",
240 if (hapd
->p2p
&& elems
.wps_ie
) {
242 wps
= ieee802_11_vendor_ie_concat(ie
, ie_len
, WPS_DEV_OUI_WFA
);
243 if (wps
&& !p2p_group_match_dev_type(hapd
->p2p_group
, wps
)) {
244 wpa_printf(MSG_MSGDUMP
, "P2P: Ignore Probe Request "
245 "due to mismatch with Requested Device "
252 #endif /* CONFIG_P2P */
254 if (hapd
->conf
->ignore_broadcast_ssid
&& elems
.ssid_len
== 0) {
255 wpa_printf(MSG_MSGDUMP
, "Probe Request from " MACSTR
" for "
256 "broadcast SSID ignored", MAC2STR(mgmt
->sa
));
260 sta
= ap_get_sta(hapd
, mgmt
->sa
);
263 if ((hapd
->conf
->p2p
& P2P_GROUP_OWNER
) &&
264 elems
.ssid_len
== P2P_WILDCARD_SSID_LEN
&&
265 os_memcmp(elems
.ssid
, P2P_WILDCARD_SSID
,
266 P2P_WILDCARD_SSID_LEN
) == 0) {
267 /* Process P2P Wildcard SSID like Wildcard SSID */
270 #endif /* CONFIG_P2P */
272 if (elems
.ssid_len
== 0 ||
273 (elems
.ssid_len
== hapd
->conf
->ssid
.ssid_len
&&
274 os_memcmp(elems
.ssid
, hapd
->conf
->ssid
.ssid
, elems
.ssid_len
) ==
276 ssid
= hapd
->conf
->ssid
.ssid
;
277 ssid_len
= hapd
->conf
->ssid
.ssid_len
;
279 sta
->ssid_probe
= &hapd
->conf
->ssid
;
283 if (!(mgmt
->da
[0] & 0x01)) {
285 ieee802_11_print_ssid(ssid_txt
, elems
.ssid
,
287 wpa_printf(MSG_MSGDUMP
, "Probe Request from " MACSTR
288 " for foreign SSID '%s' (DA " MACSTR
")",
289 MAC2STR(mgmt
->sa
), ssid_txt
,
295 /* TODO: verify that supp_rates contains at least one matching rate
296 * with AP configuration */
297 #define MAX_PROBERESP_LEN 768
298 buflen
= MAX_PROBERESP_LEN
;
300 if (hapd
->wps_probe_resp_ie
)
301 buflen
+= wpabuf_len(hapd
->wps_probe_resp_ie
);
302 #endif /* CONFIG_WPS */
304 if (hapd
->p2p_probe_resp_ie
)
305 buflen
+= wpabuf_len(hapd
->p2p_probe_resp_ie
);
306 #endif /* CONFIG_P2P */
307 resp
= os_zalloc(buflen
);
310 epos
= ((u8
*) resp
) + MAX_PROBERESP_LEN
;
312 resp
->frame_control
= IEEE80211_FC(WLAN_FC_TYPE_MGMT
,
313 WLAN_FC_STYPE_PROBE_RESP
);
314 os_memcpy(resp
->da
, mgmt
->sa
, ETH_ALEN
);
315 os_memcpy(resp
->sa
, hapd
->own_addr
, ETH_ALEN
);
317 os_memcpy(resp
->bssid
, hapd
->own_addr
, ETH_ALEN
);
318 resp
->u
.probe_resp
.beacon_int
=
319 host_to_le16(hapd
->iconf
->beacon_int
);
321 /* hardware or low-level driver will setup seq_ctrl and timestamp */
322 resp
->u
.probe_resp
.capab_info
=
323 host_to_le16(hostapd_own_capab_info(hapd
, sta
, 1));
325 pos
= resp
->u
.probe_resp
.variable
;
326 *pos
++ = WLAN_EID_SSID
;
328 os_memcpy(pos
, ssid
, ssid_len
);
331 /* Supported rates */
332 pos
= hostapd_eid_supp_rates(hapd
, pos
);
335 pos
= hostapd_eid_ds_params(hapd
, pos
);
337 pos
= hostapd_eid_country(hapd
, pos
, epos
- pos
);
339 /* ERP Information element */
340 pos
= hostapd_eid_erp_info(hapd
, pos
);
342 /* Extended supported rates */
343 pos
= hostapd_eid_ext_supp_rates(hapd
, pos
);
346 pos
= hostapd_eid_wpa(hapd
, pos
, epos
- pos
, sta
);
348 #ifdef CONFIG_IEEE80211N
349 pos
= hostapd_eid_ht_capabilities(hapd
, pos
);
350 pos
= hostapd_eid_ht_operation(hapd
, pos
);
351 #endif /* CONFIG_IEEE80211N */
353 /* Wi-Fi Alliance WMM */
354 pos
= hostapd_eid_wmm(hapd
, pos
);
357 if (hapd
->conf
->wps_state
&& hapd
->wps_probe_resp_ie
) {
358 os_memcpy(pos
, wpabuf_head(hapd
->wps_probe_resp_ie
),
359 wpabuf_len(hapd
->wps_probe_resp_ie
));
360 pos
+= wpabuf_len(hapd
->wps_probe_resp_ie
);
362 #endif /* CONFIG_WPS */
365 if ((hapd
->conf
->p2p
& P2P_ENABLED
) && elems
.p2p
&&
366 hapd
->p2p_probe_resp_ie
) {
367 os_memcpy(pos
, wpabuf_head(hapd
->p2p_probe_resp_ie
),
368 wpabuf_len(hapd
->p2p_probe_resp_ie
));
369 pos
+= wpabuf_len(hapd
->p2p_probe_resp_ie
);
371 #endif /* CONFIG_P2P */
372 #ifdef CONFIG_P2P_MANAGER
373 if ((hapd
->conf
->p2p
& (P2P_MANAGE
| P2P_ENABLED
| P2P_GROUP_OWNER
)) ==
375 pos
= hostapd_eid_p2p_manage(hapd
, pos
);
376 #endif /* CONFIG_P2P_MANAGER */
378 if (hapd
->drv
.send_mgmt_frame(hapd
, resp
, pos
- (u8
*) resp
) < 0)
379 perror("handle_probe_req: send");
383 wpa_printf(MSG_EXCESSIVE
, "STA " MACSTR
" sent probe request for %s "
384 "SSID", MAC2STR(mgmt
->sa
),
385 elems
.ssid_len
== 0 ? "broadcast" : "our");
389 void ieee802_11_set_beacon(struct hostapd_data
*hapd
)
391 struct ieee80211_mgmt
*head
;
392 u8
*pos
, *tail
, *tailpos
;
394 size_t head_len
, tail_len
;
397 if ((hapd
->conf
->p2p
& (P2P_ENABLED
| P2P_GROUP_OWNER
)) == P2P_ENABLED
)
399 #endif /* CONFIG_P2P */
401 #define BEACON_HEAD_BUF_SIZE 256
402 #define BEACON_TAIL_BUF_SIZE 512
403 head
= os_zalloc(BEACON_HEAD_BUF_SIZE
);
404 tail_len
= BEACON_TAIL_BUF_SIZE
;
406 if (hapd
->conf
->wps_state
&& hapd
->wps_beacon_ie
)
407 tail_len
+= wpabuf_len(hapd
->wps_beacon_ie
);
408 #endif /* CONFIG_WPS */
410 if (hapd
->p2p_beacon_ie
)
411 tail_len
+= wpabuf_len(hapd
->p2p_beacon_ie
);
412 #endif /* CONFIG_P2P */
413 tailpos
= tail
= os_malloc(tail_len
);
414 if (head
== NULL
|| tail
== NULL
) {
415 wpa_printf(MSG_ERROR
, "Failed to set beacon data");
421 head
->frame_control
= IEEE80211_FC(WLAN_FC_TYPE_MGMT
,
422 WLAN_FC_STYPE_BEACON
);
423 head
->duration
= host_to_le16(0);
424 os_memset(head
->da
, 0xff, ETH_ALEN
);
426 os_memcpy(head
->sa
, hapd
->own_addr
, ETH_ALEN
);
427 os_memcpy(head
->bssid
, hapd
->own_addr
, ETH_ALEN
);
428 head
->u
.beacon
.beacon_int
=
429 host_to_le16(hapd
->iconf
->beacon_int
);
431 /* hardware or low-level driver will setup seq_ctrl and timestamp */
432 capab_info
= hostapd_own_capab_info(hapd
, NULL
, 0);
433 head
->u
.beacon
.capab_info
= host_to_le16(capab_info
);
434 pos
= &head
->u
.beacon
.variable
[0];
437 *pos
++ = WLAN_EID_SSID
;
438 if (hapd
->conf
->ignore_broadcast_ssid
== 2) {
439 /* clear the data, but keep the correct length of the SSID */
440 *pos
++ = hapd
->conf
->ssid
.ssid_len
;
441 os_memset(pos
, 0, hapd
->conf
->ssid
.ssid_len
);
442 pos
+= hapd
->conf
->ssid
.ssid_len
;
443 } else if (hapd
->conf
->ignore_broadcast_ssid
) {
444 *pos
++ = 0; /* empty SSID */
446 *pos
++ = hapd
->conf
->ssid
.ssid_len
;
447 os_memcpy(pos
, hapd
->conf
->ssid
.ssid
,
448 hapd
->conf
->ssid
.ssid_len
);
449 pos
+= hapd
->conf
->ssid
.ssid_len
;
452 /* Supported rates */
453 pos
= hostapd_eid_supp_rates(hapd
, pos
);
456 pos
= hostapd_eid_ds_params(hapd
, pos
);
458 head_len
= pos
- (u8
*) head
;
460 tailpos
= hostapd_eid_country(hapd
, tailpos
,
461 tail
+ BEACON_TAIL_BUF_SIZE
- tailpos
);
463 /* ERP Information element */
464 tailpos
= hostapd_eid_erp_info(hapd
, tailpos
);
466 /* Extended supported rates */
467 tailpos
= hostapd_eid_ext_supp_rates(hapd
, tailpos
);
470 tailpos
= hostapd_eid_wpa(hapd
, tailpos
, tail
+ BEACON_TAIL_BUF_SIZE
-
473 #ifdef CONFIG_IEEE80211N
474 tailpos
= hostapd_eid_ht_capabilities(hapd
, tailpos
);
475 tailpos
= hostapd_eid_ht_operation(hapd
, tailpos
);
476 #endif /* CONFIG_IEEE80211N */
478 /* Wi-Fi Alliance WMM */
479 tailpos
= hostapd_eid_wmm(hapd
, tailpos
);
482 if (hapd
->conf
->wps_state
&& hapd
->wps_beacon_ie
) {
483 os_memcpy(tailpos
, wpabuf_head(hapd
->wps_beacon_ie
),
484 wpabuf_len(hapd
->wps_beacon_ie
));
485 tailpos
+= wpabuf_len(hapd
->wps_beacon_ie
);
487 #endif /* CONFIG_WPS */
490 if ((hapd
->conf
->p2p
& P2P_ENABLED
) && hapd
->p2p_beacon_ie
) {
491 os_memcpy(tailpos
, wpabuf_head(hapd
->p2p_beacon_ie
),
492 wpabuf_len(hapd
->p2p_beacon_ie
));
493 tailpos
+= wpabuf_len(hapd
->p2p_beacon_ie
);
495 #endif /* CONFIG_P2P */
496 #ifdef CONFIG_P2P_MANAGER
497 if ((hapd
->conf
->p2p
& (P2P_MANAGE
| P2P_ENABLED
| P2P_GROUP_OWNER
)) ==
499 tailpos
= hostapd_eid_p2p_manage(hapd
, tailpos
);
500 #endif /* CONFIG_P2P_MANAGER */
502 tail_len
= tailpos
> tail
? tailpos
- tail
: 0;
504 if (hapd
->drv
.set_beacon(hapd
, (u8
*) head
, head_len
,
505 tail
, tail_len
, hapd
->conf
->dtim_period
,
506 hapd
->iconf
->beacon_int
))
507 wpa_printf(MSG_ERROR
, "Failed to set beacon head/tail or DTIM "
515 #endif /* CONFIG_P2P */
516 hapd
->drv
.set_bss_params(hapd
, !!(ieee802_11_erp_info(hapd
) &
517 ERP_INFO_USE_PROTECTION
));
521 void ieee802_11_set_beacons(struct hostapd_iface
*iface
)
524 for (i
= 0; i
< iface
->num_bss
; i
++)
525 ieee802_11_set_beacon(iface
->bss
[i
]);
528 #endif /* CONFIG_NATIVE_WINDOWS */