]> git.ipfire.org Git - thirdparty/hostap.git/blame - hostapd/beacon.c
Remove need for direct driver calls from ieee802_11_auth.c
[thirdparty/hostap.git] / hostapd / beacon.c
CommitLineData
6fc6879b
JM
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.
a49148fd 5 * Copyright (c) 2008-2009, Jouni Malinen <j@w1.fi>
6fc6879b
JM
6 *
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.
10 *
11 * Alternatively, this software may be distributed under the terms of BSD
12 * license.
13 *
14 * See README and COPYING for more details.
15 */
16
17#include "includes.h"
18
19#ifndef CONFIG_NATIVE_WINDOWS
20
4dbfe5c5 21#include "common.h"
6fc6879b
JM
22#include "hostapd.h"
23#include "ieee802_11.h"
24#include "wpa.h"
25#include "wme.h"
26#include "beacon.h"
27#include "hw_features.h"
bfddd95c 28#include "driver_i.h"
6fc6879b 29#include "sta_info.h"
ad08c363 30#include "wps_hostapd.h"
6fc6879b
JM
31
32
33static u8 ieee802_11_erp_info(struct hostapd_data *hapd)
34{
35 u8 erp = 0;
36
37 if (hapd->iface->current_mode == NULL ||
38 hapd->iface->current_mode->mode != HOSTAPD_MODE_IEEE80211G)
39 return 0;
40
41 switch (hapd->iconf->cts_protection_type) {
42 case CTS_PROTECTION_FORCE_ENABLED:
43 erp |= ERP_INFO_NON_ERP_PRESENT | ERP_INFO_USE_PROTECTION;
44 break;
45 case CTS_PROTECTION_FORCE_DISABLED:
46 erp = 0;
47 break;
48 case CTS_PROTECTION_AUTOMATIC:
49 if (hapd->iface->olbc)
50 erp |= ERP_INFO_USE_PROTECTION;
51 /* continue */
52 case CTS_PROTECTION_AUTOMATIC_NO_OLBC:
53 if (hapd->iface->num_sta_non_erp > 0) {
54 erp |= ERP_INFO_NON_ERP_PRESENT |
55 ERP_INFO_USE_PROTECTION;
56 }
57 break;
58 }
59 if (hapd->iface->num_sta_no_short_preamble > 0)
60 erp |= ERP_INFO_BARKER_PREAMBLE_MODE;
61
62 return erp;
63}
64
65
66static u8 * hostapd_eid_ds_params(struct hostapd_data *hapd, u8 *eid)
67{
68 *eid++ = WLAN_EID_DS_PARAMS;
69 *eid++ = 1;
70 *eid++ = hapd->iconf->channel;
71 return eid;
72}
73
74
75static u8 * hostapd_eid_erp_info(struct hostapd_data *hapd, u8 *eid)
76{
77 if (hapd->iface->current_mode == NULL ||
78 hapd->iface->current_mode->mode != HOSTAPD_MODE_IEEE80211G)
79 return eid;
80
81 /* Set NonERP_present and use_protection bits if there
82 * are any associated NonERP stations. */
83 /* TODO: use_protection bit can be set to zero even if
84 * there are NonERP stations present. This optimization
85 * might be useful if NonERP stations are "quiet".
86 * See 802.11g/D6 E-1 for recommended practice.
87 * In addition, Non ERP present might be set, if AP detects Non ERP
88 * operation on other APs. */
89
90 /* Add ERP Information element */
91 *eid++ = WLAN_EID_ERP_INFO;
92 *eid++ = 1;
93 *eid++ = ieee802_11_erp_info(hapd);
94
95 return eid;
96}
97
98
df73d284
JM
99static u8 * hostapd_eid_country_add(u8 *pos, u8 *end, int chan_spacing,
100 struct hostapd_channel_data *start,
101 struct hostapd_channel_data *prev)
102{
103 if (end - pos < 3)
104 return pos;
105
106 /* first channel number */
107 *pos++ = start->chan;
108 /* number of channels */
109 *pos++ = (prev->chan - start->chan) / chan_spacing + 1;
110 /* maximum transmit power level */
111 *pos++ = start->max_tx_power;
112
113 return pos;
114}
115
116
6fc6879b
JM
117static u8 * hostapd_eid_country(struct hostapd_data *hapd, u8 *eid,
118 int max_len)
119{
120 u8 *pos = eid;
df73d284
JM
121 u8 *end = eid + max_len;
122 int i;
123 struct hostapd_hw_modes *mode;
124 struct hostapd_channel_data *start, *prev;
125 int chan_spacing = 1;
126
127 if (!hapd->iconf->ieee80211d || max_len < 6 ||
128 hapd->iface->current_mode == NULL)
6fc6879b
JM
129 return eid;
130
131 *pos++ = WLAN_EID_COUNTRY;
132 pos++; /* length will be set later */
133 os_memcpy(pos, hapd->iconf->country, 3); /* e.g., 'US ' */
134 pos += 3;
135
df73d284
JM
136 mode = hapd->iface->current_mode;
137 if (mode->mode == HOSTAPD_MODE_IEEE80211A)
138 chan_spacing = 4;
139
140 start = prev = NULL;
141 for (i = 0; i < mode->num_channels; i++) {
142 struct hostapd_channel_data *chan = &mode->channels[i];
143 if (chan->flag & HOSTAPD_CHAN_DISABLED)
144 continue;
145 if (start && prev &&
146 prev->chan + chan_spacing == chan->chan &&
147 start->max_tx_power == chan->max_tx_power) {
148 prev = chan;
149 continue; /* can use same entry */
150 }
151
152 if (start) {
153 pos = hostapd_eid_country_add(pos, end, chan_spacing,
154 start, prev);
155 start = NULL;
156 }
157
158 /* Start new group */
159 start = prev = chan;
160 }
161
162 if (start) {
163 pos = hostapd_eid_country_add(pos, end, chan_spacing,
164 start, prev);
165 }
166
167 if ((pos - eid) & 1) {
168 if (end - pos < 1)
169 return eid;
6fc6879b 170 *pos++ = 0; /* pad for 16-bit alignment */
df73d284 171 }
6fc6879b
JM
172
173 eid[1] = (pos - eid) - 2;
174
175 return pos;
176}
177
178
6fc6879b
JM
179static u8 * hostapd_eid_wpa(struct hostapd_data *hapd, u8 *eid, size_t len,
180 struct sta_info *sta)
181{
182 const u8 *ie;
183 size_t ielen;
184
185 ie = wpa_auth_get_wpa_ie(hapd->wpa_auth, &ielen);
186 if (ie == NULL || ielen > len)
187 return eid;
188
189 os_memcpy(eid, ie, ielen);
190 return eid + ielen;
191}
192
193
b57e086c
JM
194void handle_probe_req(struct hostapd_data *hapd,
195 const struct ieee80211_mgmt *mgmt, size_t len)
6fc6879b
JM
196{
197 struct ieee80211_mgmt *resp;
198 struct ieee802_11_elems elems;
199 char *ssid;
b57e086c
JM
200 u8 *pos, *epos;
201 const u8 *ie;
6fc6879b
JM
202 size_t ssid_len, ie_len;
203 struct sta_info *sta = NULL;
94709ea3 204 size_t i;
6fc6879b
JM
205
206 ie = mgmt->u.probe_req.variable;
207 ie_len = len - (IEEE80211_HDRLEN + sizeof(mgmt->u.probe_req));
208
94709ea3
JM
209 for (i = 0; hapd->probereq_cb && i < hapd->num_probereq_cb; i++)
210 hapd->probereq_cb[i].cb(hapd->probereq_cb[i].ctx,
211 mgmt->sa, ie, ie_len);
ad08c363 212
6fc6879b
JM
213 if (!hapd->iconf->send_probe_response)
214 return;
215
3d536eb4 216 if (ieee802_11_parse_elems(ie, ie_len, &elems, 0) == ParseFailed) {
6fc6879b
JM
217 wpa_printf(MSG_DEBUG, "Could not parse ProbeReq from " MACSTR,
218 MAC2STR(mgmt->sa));
219 return;
220 }
221
222 ssid = NULL;
223 ssid_len = 0;
224
225 if ((!elems.ssid || !elems.supp_rates)) {
226 wpa_printf(MSG_DEBUG, "STA " MACSTR " sent probe request "
227 "without SSID or supported rates element",
228 MAC2STR(mgmt->sa));
229 return;
230 }
231
232 if (hapd->conf->ignore_broadcast_ssid && elems.ssid_len == 0) {
233 wpa_printf(MSG_MSGDUMP, "Probe Request from " MACSTR " for "
234 "broadcast SSID ignored", MAC2STR(mgmt->sa));
235 return;
236 }
237
238 sta = ap_get_sta(hapd, mgmt->sa);
239
240 if (elems.ssid_len == 0 ||
241 (elems.ssid_len == hapd->conf->ssid.ssid_len &&
242 os_memcmp(elems.ssid, hapd->conf->ssid.ssid, elems.ssid_len) ==
243 0)) {
244 ssid = hapd->conf->ssid.ssid;
245 ssid_len = hapd->conf->ssid.ssid_len;
246 if (sta)
247 sta->ssid_probe = &hapd->conf->ssid;
248 }
249
250 if (!ssid) {
251 if (!(mgmt->da[0] & 0x01)) {
252 char ssid_txt[33];
253 ieee802_11_print_ssid(ssid_txt, elems.ssid,
254 elems.ssid_len);
255 wpa_printf(MSG_MSGDUMP, "Probe Request from " MACSTR
256 " for foreign SSID '%s'",
257 MAC2STR(mgmt->sa), ssid_txt);
258 }
259 return;
260 }
261
262 /* TODO: verify that supp_rates contains at least one matching rate
263 * with AP configuration */
264#define MAX_PROBERESP_LEN 768
265 resp = os_zalloc(MAX_PROBERESP_LEN);
266 if (resp == NULL)
267 return;
268 epos = ((u8 *) resp) + MAX_PROBERESP_LEN;
269
270 resp->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
271 WLAN_FC_STYPE_PROBE_RESP);
272 os_memcpy(resp->da, mgmt->sa, ETH_ALEN);
273 os_memcpy(resp->sa, hapd->own_addr, ETH_ALEN);
274
275 os_memcpy(resp->bssid, hapd->own_addr, ETH_ALEN);
276 resp->u.probe_resp.beacon_int =
277 host_to_le16(hapd->iconf->beacon_int);
278
279 /* hardware or low-level driver will setup seq_ctrl and timestamp */
280 resp->u.probe_resp.capab_info =
281 host_to_le16(hostapd_own_capab_info(hapd, sta, 1));
282
283 pos = resp->u.probe_resp.variable;
284 *pos++ = WLAN_EID_SSID;
285 *pos++ = ssid_len;
286 os_memcpy(pos, ssid, ssid_len);
287 pos += ssid_len;
288
289 /* Supported rates */
290 pos = hostapd_eid_supp_rates(hapd, pos);
291
292 /* DS Params */
293 pos = hostapd_eid_ds_params(hapd, pos);
294
295 pos = hostapd_eid_country(hapd, pos, epos - pos);
296
6fc6879b
JM
297 /* ERP Information element */
298 pos = hostapd_eid_erp_info(hapd, pos);
299
300 /* Extended supported rates */
301 pos = hostapd_eid_ext_supp_rates(hapd, pos);
302
303 pos = hostapd_eid_wpa(hapd, pos, epos - pos, sta);
304
3ae0800c
JM
305 /* Wi-Fi Alliance WMM */
306 pos = hostapd_eid_wmm(hapd, pos);
6fc6879b 307
d45354be 308#ifdef CONFIG_IEEE80211N
a49148fd 309 pos = hostapd_eid_ht_capabilities(hapd, pos);
9d2a76a2 310 pos = hostapd_eid_ht_operation(hapd, pos);
d45354be 311#endif /* CONFIG_IEEE80211N */
de9289c8 312
ad08c363
JM
313#ifdef CONFIG_WPS
314 if (hapd->conf->wps_state && hapd->wps_probe_resp_ie) {
14f79386
JM
315 os_memcpy(pos, wpabuf_head(hapd->wps_probe_resp_ie),
316 wpabuf_len(hapd->wps_probe_resp_ie));
317 pos += wpabuf_len(hapd->wps_probe_resp_ie);
ad08c363
JM
318 }
319#endif /* CONFIG_WPS */
320
c90933d2 321 if (hapd->drv.send_mgmt_frame(hapd, resp, pos - (u8 *) resp) < 0)
6fc6879b
JM
322 perror("handle_probe_req: send");
323
324 os_free(resp);
325
326 wpa_printf(MSG_MSGDUMP, "STA " MACSTR " sent probe request for %s "
327 "SSID", MAC2STR(mgmt->sa),
328 elems.ssid_len == 0 ? "broadcast" : "our");
329}
330
331
332void ieee802_11_set_beacon(struct hostapd_data *hapd)
333{
334 struct ieee80211_mgmt *head;
335 u8 *pos, *tail, *tailpos;
336 int preamble;
337 u16 capab_info;
338 size_t head_len, tail_len;
339 int cts_protection = ((ieee802_11_erp_info(hapd) &
340 ERP_INFO_USE_PROTECTION) ? 1 : 0);
341
342#define BEACON_HEAD_BUF_SIZE 256
343#define BEACON_TAIL_BUF_SIZE 512
344 head = os_zalloc(BEACON_HEAD_BUF_SIZE);
345 tailpos = tail = os_malloc(BEACON_TAIL_BUF_SIZE);
346 if (head == NULL || tail == NULL) {
347 wpa_printf(MSG_ERROR, "Failed to set beacon data");
348 os_free(head);
349 os_free(tail);
350 return;
351 }
352
353 head->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
354 WLAN_FC_STYPE_BEACON);
355 head->duration = host_to_le16(0);
356 os_memset(head->da, 0xff, ETH_ALEN);
357
358 os_memcpy(head->sa, hapd->own_addr, ETH_ALEN);
359 os_memcpy(head->bssid, hapd->own_addr, ETH_ALEN);
360 head->u.beacon.beacon_int =
361 host_to_le16(hapd->iconf->beacon_int);
362
363 /* hardware or low-level driver will setup seq_ctrl and timestamp */
364 capab_info = hostapd_own_capab_info(hapd, NULL, 0);
365 head->u.beacon.capab_info = host_to_le16(capab_info);
366 pos = &head->u.beacon.variable[0];
367
368 /* SSID */
369 *pos++ = WLAN_EID_SSID;
370 if (hapd->conf->ignore_broadcast_ssid == 2) {
371 /* clear the data, but keep the correct length of the SSID */
372 *pos++ = hapd->conf->ssid.ssid_len;
373 os_memset(pos, 0, hapd->conf->ssid.ssid_len);
374 pos += hapd->conf->ssid.ssid_len;
375 } else if (hapd->conf->ignore_broadcast_ssid) {
376 *pos++ = 0; /* empty SSID */
377 } else {
378 *pos++ = hapd->conf->ssid.ssid_len;
379 os_memcpy(pos, hapd->conf->ssid.ssid,
380 hapd->conf->ssid.ssid_len);
381 pos += hapd->conf->ssid.ssid_len;
382 }
383
384 /* Supported rates */
385 pos = hostapd_eid_supp_rates(hapd, pos);
386
387 /* DS Params */
388 pos = hostapd_eid_ds_params(hapd, pos);
389
390 head_len = pos - (u8 *) head;
391
392 tailpos = hostapd_eid_country(hapd, tailpos,
393 tail + BEACON_TAIL_BUF_SIZE - tailpos);
394
6fc6879b
JM
395 /* ERP Information element */
396 tailpos = hostapd_eid_erp_info(hapd, tailpos);
397
398 /* Extended supported rates */
399 tailpos = hostapd_eid_ext_supp_rates(hapd, tailpos);
400
401 tailpos = hostapd_eid_wpa(hapd, tailpos, tail + BEACON_TAIL_BUF_SIZE -
402 tailpos, NULL);
403
3ae0800c
JM
404 /* Wi-Fi Alliance WMM */
405 tailpos = hostapd_eid_wmm(hapd, tailpos);
6fc6879b 406
de9289c8 407#ifdef CONFIG_IEEE80211N
9d2a76a2 408 if (hapd->iconf->ieee80211n) {
ffbcf648
JM
409 u8 *ht_capab, *ht_oper;
410 ht_capab = tailpos;
a49148fd 411 tailpos = hostapd_eid_ht_capabilities(hapd, tailpos);
ffbcf648
JM
412
413 ht_oper = tailpos;
414 tailpos = hostapd_eid_ht_operation(hapd, tailpos);
415
416 if (tailpos > ht_oper && ht_oper > ht_capab &&
417 hostapd_set_ht_params(hapd->conf->iface, hapd,
418 ht_capab + 2, ht_capab[1],
419 ht_oper + 2, ht_oper[1])) {
de9289c8
JM
420 wpa_printf(MSG_ERROR, "Could not set HT capabilities "
421 "for kernel driver");
422 }
de9289c8
JM
423 }
424#endif /* CONFIG_IEEE80211N */
425
ad08c363
JM
426#ifdef CONFIG_WPS
427 if (hapd->conf->wps_state && hapd->wps_beacon_ie) {
14f79386
JM
428 os_memcpy(tailpos, wpabuf_head(hapd->wps_beacon_ie),
429 wpabuf_len(hapd->wps_beacon_ie));
430 tailpos += wpabuf_len(hapd->wps_beacon_ie);
ad08c363
JM
431 }
432#endif /* CONFIG_WPS */
433
6fc6879b
JM
434 tail_len = tailpos > tail ? tailpos - tail : 0;
435
436 if (hostapd_set_beacon(hapd->conf->iface, hapd, (u8 *) head, head_len,
5d674872
JM
437 tail, tail_len, hapd->conf->dtim_period,
438 hapd->iconf->beacon_int))
74f2ad32
JM
439 wpa_printf(MSG_ERROR, "Failed to set beacon head/tail or DTIM "
440 "period");
6fc6879b
JM
441
442 os_free(tail);
443 os_free(head);
444
445 if (hostapd_set_cts_protect(hapd, cts_protection))
446 wpa_printf(MSG_ERROR, "Failed to set CTS protect in kernel "
447 "driver");
448
449 if (hapd->iface->current_mode &&
450 hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G &&
451 hostapd_set_short_slot_time(hapd,
452 hapd->iface->num_sta_no_short_slot_time
453 > 0 ? 0 : 1))
454 wpa_printf(MSG_ERROR, "Failed to set Short Slot Time option "
455 "in kernel driver");
456
457 if (hapd->iface->num_sta_no_short_preamble == 0 &&
458 hapd->iconf->preamble == SHORT_PREAMBLE)
459 preamble = SHORT_PREAMBLE;
460 else
461 preamble = LONG_PREAMBLE;
462 if (hostapd_set_preamble(hapd, preamble))
463 wpa_printf(MSG_ERROR, "Could not set preamble for kernel "
464 "driver");
465}
466
467
468void ieee802_11_set_beacons(struct hostapd_iface *iface)
469{
470 size_t i;
471 for (i = 0; i < iface->num_bss; i++)
472 ieee802_11_set_beacon(iface->bss[i]);
473}
474
475#endif /* CONFIG_NATIVE_WINDOWS */