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.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
10 * Alternatively, this software may be distributed under the terms of BSD
13 * See README and COPYING for more details.
18 #ifndef CONFIG_NATIVE_WINDOWS
21 #include "ieee802_11.h"
25 #include "hw_features.h"
28 #include "ieee802_11h.h"
31 static u8
ieee802_11_erp_info(struct hostapd_data
*hapd
)
35 if (hapd
->iface
== NULL
|| hapd
->iface
->current_mode
== NULL
||
36 hapd
->iface
->current_mode
->mode
!= HOSTAPD_MODE_IEEE80211G
)
39 switch (hapd
->iconf
->cts_protection_type
) {
40 case CTS_PROTECTION_FORCE_ENABLED
:
41 erp
|= ERP_INFO_NON_ERP_PRESENT
| ERP_INFO_USE_PROTECTION
;
43 case CTS_PROTECTION_FORCE_DISABLED
:
46 case CTS_PROTECTION_AUTOMATIC
:
47 if (hapd
->iface
->olbc
)
48 erp
|= ERP_INFO_USE_PROTECTION
;
50 case CTS_PROTECTION_AUTOMATIC_NO_OLBC
:
51 if (hapd
->iface
->num_sta_non_erp
> 0) {
52 erp
|= ERP_INFO_NON_ERP_PRESENT
|
53 ERP_INFO_USE_PROTECTION
;
57 if (hapd
->iface
->num_sta_no_short_preamble
> 0)
58 erp
|= ERP_INFO_BARKER_PREAMBLE_MODE
;
64 static u8
* hostapd_eid_ds_params(struct hostapd_data
*hapd
, u8
*eid
)
66 *eid
++ = WLAN_EID_DS_PARAMS
;
68 *eid
++ = hapd
->iconf
->channel
;
73 static u8
* hostapd_eid_erp_info(struct hostapd_data
*hapd
, u8
*eid
)
75 if (hapd
->iface
== NULL
|| hapd
->iface
->current_mode
== NULL
||
76 hapd
->iface
->current_mode
->mode
!= HOSTAPD_MODE_IEEE80211G
)
79 /* Set NonERP_present and use_protection bits if there
80 * are any associated NonERP stations. */
81 /* TODO: use_protection bit can be set to zero even if
82 * there are NonERP stations present. This optimization
83 * might be useful if NonERP stations are "quiet".
84 * See 802.11g/D6 E-1 for recommended practice.
85 * In addition, Non ERP present might be set, if AP detects Non ERP
86 * operation on other APs. */
88 /* Add ERP Information element */
89 *eid
++ = WLAN_EID_ERP_INFO
;
91 *eid
++ = ieee802_11_erp_info(hapd
);
97 static u8
* hostapd_eid_country(struct hostapd_data
*hapd
, u8
*eid
,
103 if ((!hapd
->iconf
->ieee80211d
&& !hapd
->iface
->dfs_enable
) ||
107 *pos
++ = WLAN_EID_COUNTRY
;
108 pos
++; /* length will be set later */
109 memcpy(pos
, hapd
->iconf
->country
, 3); /* e.g., 'US ' */
113 if ((pos
- eid
) & 1) {
116 *pos
++ = 0; /* pad for 16-bit alignment */
120 eid
[1] = (pos
- eid
) - 2;
126 static u8
* hostapd_eid_power_constraint(struct hostapd_data
*hapd
, u8
*eid
)
129 if (!hapd
->iface
->dfs_enable
)
131 *eid
++ = WLAN_EID_PWR_CONSTRAINT
;
133 *eid
++ = hapd
->iface
->pwr_const
;
138 static u8
* hostapd_eid_tpc_report(struct hostapd_data
*hapd
, u8
*eid
)
141 if (!hapd
->iface
->dfs_enable
)
143 *eid
++ = WLAN_EID_TPC_REPORT
;
145 *eid
++ = hapd
->iface
->tx_power
; /* TX POWER */
146 *eid
++ = 0; /* Link Margin */
150 static u8
* hostapd_eid_channel_switch(struct hostapd_data
*hapd
, u8
*eid
)
153 if (!hapd
->iface
->dfs_enable
|| !hapd
->iface
->channel_switch
)
155 *eid
++ = WLAN_EID_CHANNEL_SWITCH
;
157 *eid
++ = CHAN_SWITCH_MODE_QUIET
;
158 *eid
++ = hapd
->iface
->channel_switch
; /* New channel */
159 /* 0 - very soon; 1 - before next TBTT; num - after num beacons */
165 static u8
* hostapd_eid_wpa(struct hostapd_data
*hapd
, u8
*eid
, size_t len
,
166 struct sta_info
*sta
)
171 ie
= wpa_auth_get_wpa_ie(hapd
->wpa_auth
, &ielen
);
172 if (ie
== NULL
|| ielen
> len
)
175 memcpy(eid
, ie
, ielen
);
180 void handle_probe_req(struct hostapd_data
*hapd
, struct ieee80211_mgmt
*mgmt
,
183 struct ieee80211_mgmt
*resp
;
184 struct ieee802_11_elems elems
;
188 struct sta_info
*sta
= NULL
;
190 if (!hapd
->iconf
->send_probe_response
)
193 if (ieee802_11_parse_elems(hapd
, mgmt
->u
.probe_req
.variable
,
194 len
- (IEEE80211_HDRLEN
+
195 sizeof(mgmt
->u
.probe_req
)), &elems
,
198 HOSTAPD_DEBUG(HOSTAPD_DEBUG_MINIMAL
,
199 "Could not parse ProbeReq from " MACSTR
"\n",
207 if ((!elems
.ssid
|| !elems
.supp_rates
)) {
208 printf("STA " MACSTR
" sent probe request without SSID or "
209 "supported rates element\n", MAC2STR(mgmt
->sa
));
213 if (hapd
->conf
->ignore_broadcast_ssid
&& elems
.ssid_len
== 0) {
214 HOSTAPD_DEBUG(HOSTAPD_DEBUG_MSGDUMPS
,
215 "Probe Request from " MACSTR
" for broadcast "
216 "SSID ignored\n", MAC2STR(mgmt
->sa
));
220 sta
= ap_get_sta(hapd
, mgmt
->sa
);
222 if (elems
.ssid_len
== 0 ||
223 (elems
.ssid_len
== hapd
->conf
->ssid
.ssid_len
&&
224 memcmp(elems
.ssid
, hapd
->conf
->ssid
.ssid
, elems
.ssid_len
) == 0)) {
225 ssid
= hapd
->conf
->ssid
.ssid
;
226 ssid_len
= hapd
->conf
->ssid
.ssid_len
;
228 sta
->ssid_probe
= &hapd
->conf
->ssid
;
232 if (HOSTAPD_DEBUG_COND(HOSTAPD_DEBUG_MSGDUMPS
)) {
233 printf("Probe Request from " MACSTR
" for foreign "
234 "SSID '", MAC2STR(mgmt
->sa
));
235 ieee802_11_print_ssid(elems
.ssid
, elems
.ssid_len
);
241 /* TODO: verify that supp_rates contains at least one matching rate
242 * with AP configuration */
243 #define MAX_PROBERESP_LEN 512
244 resp
= wpa_zalloc(MAX_PROBERESP_LEN
);
247 epos
= ((u8
*) resp
) + MAX_PROBERESP_LEN
;
249 resp
->frame_control
= IEEE80211_FC(WLAN_FC_TYPE_MGMT
,
250 WLAN_FC_STYPE_PROBE_RESP
);
251 memcpy(resp
->da
, mgmt
->sa
, ETH_ALEN
);
252 memcpy(resp
->sa
, hapd
->own_addr
, ETH_ALEN
);
254 memcpy(resp
->bssid
, hapd
->own_addr
, ETH_ALEN
);
255 resp
->u
.probe_resp
.beacon_int
=
256 host_to_le16(hapd
->iconf
->beacon_int
);
258 /* hardware or low-level driver will setup seq_ctrl and timestamp */
259 resp
->u
.probe_resp
.capab_info
=
260 host_to_le16(hostapd_own_capab_info(hapd
, sta
, 1));
262 pos
= resp
->u
.probe_resp
.variable
;
263 *pos
++ = WLAN_EID_SSID
;
265 memcpy(pos
, ssid
, ssid_len
);
268 /* Supported rates */
269 pos
= hostapd_eid_supp_rates(hapd
, pos
);
272 pos
= hostapd_eid_ds_params(hapd
, pos
);
274 pos
= hostapd_eid_country(hapd
, pos
, epos
- pos
);
276 pos
= hostapd_eid_power_constraint(hapd
, pos
);
277 pos
= hostapd_eid_tpc_report(hapd
, pos
);
279 /* ERP Information element */
280 pos
= hostapd_eid_erp_info(hapd
, pos
);
282 /* Extended supported rates */
283 pos
= hostapd_eid_ext_supp_rates(hapd
, pos
);
285 pos
= hostapd_eid_wpa(hapd
, pos
, epos
- pos
, sta
);
287 /* Wi-Fi Wireless Multimedia Extensions */
288 if (hapd
->conf
->wme_enabled
)
289 pos
= hostapd_eid_wme(hapd
, pos
);
291 if (hostapd_send_mgmt_frame(hapd
, resp
, pos
- (u8
*) resp
, 0) < 0)
292 perror("handle_probe_req: send");
296 HOSTAPD_DEBUG(HOSTAPD_DEBUG_MSGDUMPS
, "STA " MACSTR
297 " sent probe request for %s SSID\n",
298 MAC2STR(mgmt
->sa
), elems
.ssid_len
== 0 ? "broadcast" :
303 void ieee802_11_set_beacon(struct hostapd_data
*hapd
)
305 struct ieee80211_mgmt
*head
;
306 u8
*pos
, *tail
, *tailpos
;
309 size_t head_len
, tail_len
;
310 int cts_protection
= ((ieee802_11_erp_info(hapd
) &
311 ERP_INFO_USE_PROTECTION
) ? 1 : 0);
313 #define BEACON_HEAD_BUF_SIZE 256
314 #define BEACON_TAIL_BUF_SIZE 256
315 head
= wpa_zalloc(BEACON_HEAD_BUF_SIZE
);
316 tailpos
= tail
= malloc(BEACON_TAIL_BUF_SIZE
);
317 if (head
== NULL
|| tail
== NULL
) {
318 printf("Failed to set beacon data\n");
324 head
->frame_control
= IEEE80211_FC(WLAN_FC_TYPE_MGMT
,
325 WLAN_FC_STYPE_BEACON
);
326 head
->duration
= host_to_le16(0);
327 memset(head
->da
, 0xff, ETH_ALEN
);
329 memcpy(head
->sa
, hapd
->own_addr
, ETH_ALEN
);
330 memcpy(head
->bssid
, hapd
->own_addr
, ETH_ALEN
);
331 head
->u
.beacon
.beacon_int
=
332 host_to_le16(hapd
->iconf
->beacon_int
);
334 /* hardware or low-level driver will setup seq_ctrl and timestamp */
335 capab_info
= hostapd_own_capab_info(hapd
, NULL
, 0);
336 head
->u
.beacon
.capab_info
= host_to_le16(capab_info
);
337 pos
= &head
->u
.beacon
.variable
[0];
340 *pos
++ = WLAN_EID_SSID
;
341 if (hapd
->conf
->ignore_broadcast_ssid
== 2) {
342 /* clear the data, but keep the correct length of the SSID */
343 *pos
++ = hapd
->conf
->ssid
.ssid_len
;
344 memset(pos
, 0, hapd
->conf
->ssid
.ssid_len
);
345 pos
+= hapd
->conf
->ssid
.ssid_len
;
346 } else if (hapd
->conf
->ignore_broadcast_ssid
) {
347 *pos
++ = 0; /* empty SSID */
349 *pos
++ = hapd
->conf
->ssid
.ssid_len
;
350 memcpy(pos
, hapd
->conf
->ssid
.ssid
, hapd
->conf
->ssid
.ssid_len
);
351 pos
+= hapd
->conf
->ssid
.ssid_len
;
354 /* Supported rates */
355 pos
= hostapd_eid_supp_rates(hapd
, pos
);
358 pos
= hostapd_eid_ds_params(hapd
, pos
);
360 head_len
= pos
- (u8
*) head
;
362 tailpos
= hostapd_eid_country(hapd
, tailpos
,
363 tail
+ BEACON_TAIL_BUF_SIZE
- tailpos
);
365 tailpos
= hostapd_eid_power_constraint(hapd
, tailpos
);
366 tailpos
= hostapd_eid_channel_switch(hapd
, tailpos
);
367 tailpos
= hostapd_eid_tpc_report(hapd
, tailpos
);
369 /* ERP Information element */
370 tailpos
= hostapd_eid_erp_info(hapd
, tailpos
);
372 /* Extended supported rates */
373 tailpos
= hostapd_eid_ext_supp_rates(hapd
, tailpos
);
375 tailpos
= hostapd_eid_wpa(hapd
, tailpos
, tail
+ BEACON_TAIL_BUF_SIZE
-
378 /* Wi-Fi Wireless Multimedia Extensions */
379 if (hapd
->conf
->wme_enabled
)
380 tailpos
= hostapd_eid_wme(hapd
, tailpos
);
382 tail_len
= tailpos
> tail
? tailpos
- tail
: 0;
384 if (hostapd_set_beacon(hapd
->conf
->iface
, hapd
, (u8
*) head
, head_len
,
386 printf("Failed to set beacon head/tail\n");
391 if (hostapd_set_cts_protect(hapd
, cts_protection
))
392 printf("Failed to set CTS protect in kernel driver\n");
394 if (hapd
->iface
&& hapd
->iface
->current_mode
&&
395 hapd
->iface
->current_mode
->mode
== HOSTAPD_MODE_IEEE80211G
&&
396 hostapd_set_short_slot_time(hapd
,
397 hapd
->iface
->num_sta_no_short_slot_time
399 printf("Failed to set Short Slot Time option in kernel "
402 if (hapd
->iface
&& hapd
->iface
->num_sta_no_short_preamble
== 0 &&
403 hapd
->iconf
->preamble
== SHORT_PREAMBLE
)
404 preamble
= SHORT_PREAMBLE
;
406 preamble
= LONG_PREAMBLE
;
407 if (hostapd_set_preamble(hapd
, preamble
))
408 printf("Could not set preamble for kernel driver\n");
412 void ieee802_11_set_beacons(struct hostapd_iface
*iface
)
415 for (i
= 0; i
< iface
->num_bss
; i
++)
416 ieee802_11_set_beacon(iface
->bss
[i
]);
419 #endif /* CONFIG_NATIVE_WINDOWS */