fsck_msdosfs: check next cluster index for validity before using it
[dragonfly.git] / contrib / hostapd-0.5.8 / beacon.c
blob7af2bc1754e2cac4af7aeb5ac020d4a9950f83f8
1 /*
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
11 * license.
13 * See README and COPYING for more details.
16 #include "includes.h"
18 #ifndef CONFIG_NATIVE_WINDOWS
20 #include "hostapd.h"
21 #include "ieee802_11.h"
22 #include "wpa.h"
23 #include "wme.h"
24 #include "beacon.h"
25 #include "hw_features.h"
26 #include "driver.h"
27 #include "sta_info.h"
28 #include "ieee802_11h.h"
31 static u8 ieee802_11_erp_info(struct hostapd_data *hapd)
33 u8 erp = 0;
35 if (hapd->iface == NULL || hapd->iface->current_mode == NULL ||
36 hapd->iface->current_mode->mode != HOSTAPD_MODE_IEEE80211G)
37 return 0;
39 switch (hapd->iconf->cts_protection_type) {
40 case CTS_PROTECTION_FORCE_ENABLED:
41 erp |= ERP_INFO_NON_ERP_PRESENT | ERP_INFO_USE_PROTECTION;
42 break;
43 case CTS_PROTECTION_FORCE_DISABLED:
44 erp = 0;
45 break;
46 case CTS_PROTECTION_AUTOMATIC:
47 if (hapd->iface->olbc)
48 erp |= ERP_INFO_USE_PROTECTION;
49 /* continue */
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;
55 break;
57 if (hapd->iface->num_sta_no_short_preamble > 0)
58 erp |= ERP_INFO_BARKER_PREAMBLE_MODE;
60 return erp;
64 static u8 * hostapd_eid_ds_params(struct hostapd_data *hapd, u8 *eid)
66 *eid++ = WLAN_EID_DS_PARAMS;
67 *eid++ = 1;
68 *eid++ = hapd->iconf->channel;
69 return eid;
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)
77 return eid;
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;
90 *eid++ = 1;
91 *eid++ = ieee802_11_erp_info(hapd);
93 return eid;
97 static u8 * hostapd_eid_country(struct hostapd_data *hapd, u8 *eid,
98 int max_len)
100 int left;
101 u8 *pos = eid;
103 if ((!hapd->iconf->ieee80211d && !hapd->iface->dfs_enable) ||
104 max_len < 6)
105 return eid;
107 *pos++ = WLAN_EID_COUNTRY;
108 pos++; /* length will be set later */
109 memcpy(pos, hapd->iconf->country, 3); /* e.g., 'US ' */
110 pos += 3;
111 left = max_len - 3;
113 if ((pos - eid) & 1) {
114 if (left < 1)
115 return eid;
116 *pos++ = 0; /* pad for 16-bit alignment */
117 left--;
120 eid[1] = (pos - eid) - 2;
122 return pos;
126 static u8 * hostapd_eid_power_constraint(struct hostapd_data *hapd, u8 *eid)
129 if (!hapd->iface->dfs_enable)
130 return eid;
131 *eid++ = WLAN_EID_PWR_CONSTRAINT;
132 *eid++ = 1;
133 *eid++ = hapd->iface->pwr_const;
134 return eid;
138 static u8 * hostapd_eid_tpc_report(struct hostapd_data *hapd, u8 *eid)
141 if (!hapd->iface->dfs_enable)
142 return eid;
143 *eid++ = WLAN_EID_TPC_REPORT;
144 *eid++ = 2;
145 *eid++ = hapd->iface->tx_power; /* TX POWER */
146 *eid++ = 0; /* Link Margin */
147 return eid;
150 static u8 * hostapd_eid_channel_switch(struct hostapd_data *hapd, u8 *eid)
153 if (!hapd->iface->dfs_enable || !hapd->iface->channel_switch)
154 return eid;
155 *eid++ = WLAN_EID_CHANNEL_SWITCH;
156 *eid++ = 3;
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 */
160 *eid++ = 0;
161 return eid;
165 static u8 * hostapd_eid_wpa(struct hostapd_data *hapd, u8 *eid, size_t len,
166 struct sta_info *sta)
168 const u8 *ie;
169 size_t ielen;
171 ie = wpa_auth_get_wpa_ie(hapd->wpa_auth, &ielen);
172 if (ie == NULL || ielen > len)
173 return eid;
175 memcpy(eid, ie, ielen);
176 return eid + ielen;
180 void handle_probe_req(struct hostapd_data *hapd, struct ieee80211_mgmt *mgmt,
181 size_t len)
183 struct ieee80211_mgmt *resp;
184 struct ieee802_11_elems elems;
185 char *ssid;
186 u8 *pos, *epos;
187 size_t ssid_len;
188 struct sta_info *sta = NULL;
190 if (!hapd->iconf->send_probe_response)
191 return;
193 if (ieee802_11_parse_elems(hapd, mgmt->u.probe_req.variable,
194 len - (IEEE80211_HDRLEN +
195 sizeof(mgmt->u.probe_req)), &elems,
197 == ParseFailed) {
198 HOSTAPD_DEBUG(HOSTAPD_DEBUG_MINIMAL,
199 "Could not parse ProbeReq from " MACSTR "\n",
200 MAC2STR(mgmt->sa));
201 return;
204 ssid = NULL;
205 ssid_len = 0;
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));
210 return;
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));
217 return;
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;
227 if (sta)
228 sta->ssid_probe = &hapd->conf->ssid;
231 if (!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);
236 printf("'\n");
238 return;
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);
245 if (resp == NULL)
246 return;
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;
264 *pos++ = ssid_len;
265 memcpy(pos, ssid, ssid_len);
266 pos += ssid_len;
268 /* Supported rates */
269 pos = hostapd_eid_supp_rates(hapd, pos);
271 /* DS Params */
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");
294 free(resp);
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" :
299 "our");
303 void ieee802_11_set_beacon(struct hostapd_data *hapd)
305 struct ieee80211_mgmt *head;
306 u8 *pos, *tail, *tailpos;
307 int preamble;
308 u16 capab_info;
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");
319 free(head);
320 free(tail);
321 return;
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];
339 /* SSID */
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 */
348 } else {
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);
357 /* DS Params */
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 -
376 tailpos, NULL);
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,
385 tail, tail_len))
386 printf("Failed to set beacon head/tail\n");
388 free(tail);
389 free(head);
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
398 > 0 ? 0 : 1))
399 printf("Failed to set Short Slot Time option in kernel "
400 "driver\n");
402 if (hapd->iface && hapd->iface->num_sta_no_short_preamble == 0 &&
403 hapd->iconf->preamble == SHORT_PREAMBLE)
404 preamble = SHORT_PREAMBLE;
405 else
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)
414 size_t i;
415 for (i = 0; i < iface->num_bss; i++)
416 ieee802_11_set_beacon(iface->bss[i]);
419 #endif /* CONFIG_NATIVE_WINDOWS */