]> git.ipfire.org Git - thirdparty/hostap.git/blame - src/ap/beacon.c
wlantest: Add command for adding WEP keys during run time
[thirdparty/hostap.git] / src / ap / 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
6226e38d 17#include "utils/includes.h"
6fc6879b
JM
18
19#ifndef CONFIG_NATIVE_WINDOWS
20
6226e38d 21#include "utils/common.h"
81f4f619
JM
22#include "common/ieee802_11_defs.h"
23#include "common/ieee802_11_common.h"
6226e38d 24#include "drivers/driver.h"
e44f8bf2
JM
25#include "wps/wps_defs.h"
26#include "p2p/p2p.h"
6fc6879b
JM
27#include "hostapd.h"
28#include "ieee802_11.h"
6226e38d 29#include "wpa_auth.h"
1057d78e 30#include "wmm.h"
6226e38d 31#include "ap_config.h"
6fc6879b 32#include "sta_info.h"
dce044cc 33#include "p2p_hostapd.h"
cee7d66b 34#include "ap_drv_ops.h"
6226e38d 35#include "beacon.h"
6fc6879b
JM
36
37
38static u8 ieee802_11_erp_info(struct hostapd_data *hapd)
39{
40 u8 erp = 0;
41
42 if (hapd->iface->current_mode == NULL ||
43 hapd->iface->current_mode->mode != HOSTAPD_MODE_IEEE80211G)
44 return 0;
45
46 switch (hapd->iconf->cts_protection_type) {
47 case CTS_PROTECTION_FORCE_ENABLED:
48 erp |= ERP_INFO_NON_ERP_PRESENT | ERP_INFO_USE_PROTECTION;
49 break;
50 case CTS_PROTECTION_FORCE_DISABLED:
51 erp = 0;
52 break;
53 case CTS_PROTECTION_AUTOMATIC:
54 if (hapd->iface->olbc)
55 erp |= ERP_INFO_USE_PROTECTION;
56 /* continue */
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;
61 }
62 break;
63 }
a0fad210
JM
64 if (hapd->iface->num_sta_no_short_preamble > 0 ||
65 hapd->iconf->preamble == LONG_PREAMBLE)
6fc6879b
JM
66 erp |= ERP_INFO_BARKER_PREAMBLE_MODE;
67
68 return erp;
69}
70
71
72static u8 * hostapd_eid_ds_params(struct hostapd_data *hapd, u8 *eid)
73{
74 *eid++ = WLAN_EID_DS_PARAMS;
75 *eid++ = 1;
76 *eid++ = hapd->iconf->channel;
77 return eid;
78}
79
80
81static u8 * hostapd_eid_erp_info(struct hostapd_data *hapd, u8 *eid)
82{
83 if (hapd->iface->current_mode == NULL ||
84 hapd->iface->current_mode->mode != HOSTAPD_MODE_IEEE80211G)
85 return eid;
86
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. */
95
96 /* Add ERP Information element */
97 *eid++ = WLAN_EID_ERP_INFO;
98 *eid++ = 1;
99 *eid++ = ieee802_11_erp_info(hapd);
100
101 return eid;
102}
103
104
df73d284
JM
105static u8 * hostapd_eid_country_add(u8 *pos, u8 *end, int chan_spacing,
106 struct hostapd_channel_data *start,
107 struct hostapd_channel_data *prev)
108{
109 if (end - pos < 3)
110 return pos;
111
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;
118
119 return pos;
120}
121
122
6fc6879b
JM
123static u8 * hostapd_eid_country(struct hostapd_data *hapd, u8 *eid,
124 int max_len)
125{
126 u8 *pos = eid;
df73d284
JM
127 u8 *end = eid + max_len;
128 int i;
129 struct hostapd_hw_modes *mode;
130 struct hostapd_channel_data *start, *prev;
131 int chan_spacing = 1;
132
133 if (!hapd->iconf->ieee80211d || max_len < 6 ||
134 hapd->iface->current_mode == NULL)
6fc6879b
JM
135 return eid;
136
137 *pos++ = WLAN_EID_COUNTRY;
138 pos++; /* length will be set later */
139 os_memcpy(pos, hapd->iconf->country, 3); /* e.g., 'US ' */
140 pos += 3;
141
df73d284
JM
142 mode = hapd->iface->current_mode;
143 if (mode->mode == HOSTAPD_MODE_IEEE80211A)
144 chan_spacing = 4;
145
146 start = prev = NULL;
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)
150 continue;
151 if (start && prev &&
152 prev->chan + chan_spacing == chan->chan &&
153 start->max_tx_power == chan->max_tx_power) {
154 prev = chan;
155 continue; /* can use same entry */
156 }
157
158 if (start) {
159 pos = hostapd_eid_country_add(pos, end, chan_spacing,
160 start, prev);
161 start = NULL;
162 }
163
164 /* Start new group */
165 start = prev = chan;
166 }
167
168 if (start) {
169 pos = hostapd_eid_country_add(pos, end, chan_spacing,
170 start, prev);
171 }
172
173 if ((pos - eid) & 1) {
174 if (end - pos < 1)
175 return eid;
6fc6879b 176 *pos++ = 0; /* pad for 16-bit alignment */
df73d284 177 }
6fc6879b
JM
178
179 eid[1] = (pos - eid) - 2;
180
181 return pos;
182}
183
184
6fc6879b
JM
185static u8 * hostapd_eid_wpa(struct hostapd_data *hapd, u8 *eid, size_t len,
186 struct sta_info *sta)
187{
188 const u8 *ie;
189 size_t ielen;
190
191 ie = wpa_auth_get_wpa_ie(hapd->wpa_auth, &ielen);
192 if (ie == NULL || ielen > len)
193 return eid;
194
195 os_memcpy(eid, ie, ielen);
196 return eid + ielen;
197}
198
199
b57e086c
JM
200void handle_probe_req(struct hostapd_data *hapd,
201 const struct ieee80211_mgmt *mgmt, size_t len)
6fc6879b
JM
202{
203 struct ieee80211_mgmt *resp;
204 struct ieee802_11_elems elems;
205 char *ssid;
b57e086c
JM
206 u8 *pos, *epos;
207 const u8 *ie;
6fc6879b
JM
208 size_t ssid_len, ie_len;
209 struct sta_info *sta = NULL;
1c08f8c0 210 size_t buflen;
94709ea3 211 size_t i;
6fc6879b
JM
212
213 ie = mgmt->u.probe_req.variable;
cbcf92b4
JM
214 if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.probe_req))
215 return;
6fc6879b
JM
216 ie_len = len - (IEEE80211_HDRLEN + sizeof(mgmt->u.probe_req));
217
94709ea3 218 for (i = 0; hapd->probereq_cb && i < hapd->num_probereq_cb; i++)
cd7d80f3
JM
219 if (hapd->probereq_cb[i].cb(hapd->probereq_cb[i].ctx,
220 mgmt->sa, ie, ie_len) > 0)
221 return;
ad08c363 222
6fc6879b
JM
223 if (!hapd->iconf->send_probe_response)
224 return;
225
3d536eb4 226 if (ieee802_11_parse_elems(ie, ie_len, &elems, 0) == ParseFailed) {
6fc6879b
JM
227 wpa_printf(MSG_DEBUG, "Could not parse ProbeReq from " MACSTR,
228 MAC2STR(mgmt->sa));
229 return;
230 }
231
232 ssid = NULL;
233 ssid_len = 0;
234
235 if ((!elems.ssid || !elems.supp_rates)) {
236 wpa_printf(MSG_DEBUG, "STA " MACSTR " sent probe request "
237 "without SSID or supported rates element",
238 MAC2STR(mgmt->sa));
239 return;
240 }
241
e44f8bf2
JM
242#ifdef CONFIG_P2P
243 if (hapd->p2p && elems.wps_ie) {
244 struct wpabuf *wps;
245 wps = ieee802_11_vendor_ie_concat(ie, ie_len, WPS_DEV_OUI_WFA);
246 if (wps && !p2p_group_match_dev_type(hapd->p2p_group, wps)) {
247 wpa_printf(MSG_MSGDUMP, "P2P: Ignore Probe Request "
248 "due to mismatch with Requested Device "
249 "Type");
250 wpabuf_free(wps);
251 return;
252 }
253 wpabuf_free(wps);
254 }
255#endif /* CONFIG_P2P */
256
6fc6879b
JM
257 if (hapd->conf->ignore_broadcast_ssid && elems.ssid_len == 0) {
258 wpa_printf(MSG_MSGDUMP, "Probe Request from " MACSTR " for "
259 "broadcast SSID ignored", MAC2STR(mgmt->sa));
260 return;
261 }
262
263 sta = ap_get_sta(hapd, mgmt->sa);
264
e44f8bf2
JM
265#ifdef CONFIG_P2P
266 if ((hapd->conf->p2p & P2P_GROUP_OWNER) &&
267 elems.ssid_len == P2P_WILDCARD_SSID_LEN &&
268 os_memcmp(elems.ssid, P2P_WILDCARD_SSID,
269 P2P_WILDCARD_SSID_LEN) == 0) {
270 /* Process P2P Wildcard SSID like Wildcard SSID */
271 elems.ssid_len = 0;
272 }
273#endif /* CONFIG_P2P */
274
6fc6879b
JM
275 if (elems.ssid_len == 0 ||
276 (elems.ssid_len == hapd->conf->ssid.ssid_len &&
277 os_memcmp(elems.ssid, hapd->conf->ssid.ssid, elems.ssid_len) ==
278 0)) {
279 ssid = hapd->conf->ssid.ssid;
280 ssid_len = hapd->conf->ssid.ssid_len;
281 if (sta)
282 sta->ssid_probe = &hapd->conf->ssid;
283 }
284
285 if (!ssid) {
286 if (!(mgmt->da[0] & 0x01)) {
287 char ssid_txt[33];
288 ieee802_11_print_ssid(ssid_txt, elems.ssid,
289 elems.ssid_len);
290 wpa_printf(MSG_MSGDUMP, "Probe Request from " MACSTR
2d8bf732
JM
291 " for foreign SSID '%s' (DA " MACSTR ")",
292 MAC2STR(mgmt->sa), ssid_txt,
293 MAC2STR(mgmt->da));
6fc6879b
JM
294 }
295 return;
296 }
297
298 /* TODO: verify that supp_rates contains at least one matching rate
299 * with AP configuration */
300#define MAX_PROBERESP_LEN 768
1c08f8c0
JM
301 buflen = MAX_PROBERESP_LEN;
302#ifdef CONFIG_WPS
303 if (hapd->wps_probe_resp_ie)
304 buflen += wpabuf_len(hapd->wps_probe_resp_ie);
305#endif /* CONFIG_WPS */
e44f8bf2
JM
306#ifdef CONFIG_P2P
307 if (hapd->p2p_probe_resp_ie)
308 buflen += wpabuf_len(hapd->p2p_probe_resp_ie);
309#endif /* CONFIG_P2P */
1c08f8c0 310 resp = os_zalloc(buflen);
6fc6879b
JM
311 if (resp == NULL)
312 return;
313 epos = ((u8 *) resp) + MAX_PROBERESP_LEN;
314
315 resp->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
316 WLAN_FC_STYPE_PROBE_RESP);
317 os_memcpy(resp->da, mgmt->sa, ETH_ALEN);
318 os_memcpy(resp->sa, hapd->own_addr, ETH_ALEN);
319
320 os_memcpy(resp->bssid, hapd->own_addr, ETH_ALEN);
321 resp->u.probe_resp.beacon_int =
322 host_to_le16(hapd->iconf->beacon_int);
323
324 /* hardware or low-level driver will setup seq_ctrl and timestamp */
325 resp->u.probe_resp.capab_info =
326 host_to_le16(hostapd_own_capab_info(hapd, sta, 1));
327
328 pos = resp->u.probe_resp.variable;
329 *pos++ = WLAN_EID_SSID;
330 *pos++ = ssid_len;
331 os_memcpy(pos, ssid, ssid_len);
332 pos += ssid_len;
333
334 /* Supported rates */
335 pos = hostapd_eid_supp_rates(hapd, pos);
336
337 /* DS Params */
338 pos = hostapd_eid_ds_params(hapd, pos);
339
340 pos = hostapd_eid_country(hapd, pos, epos - pos);
341
6fc6879b
JM
342 /* ERP Information element */
343 pos = hostapd_eid_erp_info(hapd, pos);
344
345 /* Extended supported rates */
346 pos = hostapd_eid_ext_supp_rates(hapd, pos);
347
1bc774a1 348 /* RSN, MDIE, WPA */
6fc6879b
JM
349 pos = hostapd_eid_wpa(hapd, pos, epos - pos, sta);
350
d45354be 351#ifdef CONFIG_IEEE80211N
a49148fd 352 pos = hostapd_eid_ht_capabilities(hapd, pos);
9d2a76a2 353 pos = hostapd_eid_ht_operation(hapd, pos);
d45354be 354#endif /* CONFIG_IEEE80211N */
de9289c8 355
1bc774a1
JM
356 /* Wi-Fi Alliance WMM */
357 pos = hostapd_eid_wmm(hapd, pos);
358
ad08c363
JM
359#ifdef CONFIG_WPS
360 if (hapd->conf->wps_state && hapd->wps_probe_resp_ie) {
14f79386
JM
361 os_memcpy(pos, wpabuf_head(hapd->wps_probe_resp_ie),
362 wpabuf_len(hapd->wps_probe_resp_ie));
363 pos += wpabuf_len(hapd->wps_probe_resp_ie);
ad08c363
JM
364 }
365#endif /* CONFIG_WPS */
366
e44f8bf2
JM
367#ifdef CONFIG_P2P
368 if ((hapd->conf->p2p & P2P_ENABLED) && elems.p2p &&
369 hapd->p2p_probe_resp_ie) {
370 os_memcpy(pos, wpabuf_head(hapd->p2p_probe_resp_ie),
371 wpabuf_len(hapd->p2p_probe_resp_ie));
372 pos += wpabuf_len(hapd->p2p_probe_resp_ie);
373 }
374#endif /* CONFIG_P2P */
962473c1
JM
375#ifdef CONFIG_P2P_MANAGER
376 if ((hapd->conf->p2p & (P2P_MANAGE | P2P_ENABLED | P2P_GROUP_OWNER)) ==
377 P2P_MANAGE)
378 pos = hostapd_eid_p2p_manage(hapd, pos);
379#endif /* CONFIG_P2P_MANAGER */
e44f8bf2 380
cee7d66b 381 if (hostapd_drv_send_mlme(hapd, resp, pos - (u8 *) resp) < 0)
6fc6879b
JM
382 perror("handle_probe_req: send");
383
384 os_free(resp);
385
2d8bf732 386 wpa_printf(MSG_EXCESSIVE, "STA " MACSTR " sent probe request for %s "
6fc6879b
JM
387 "SSID", MAC2STR(mgmt->sa),
388 elems.ssid_len == 0 ? "broadcast" : "our");
389}
390
391
392void ieee802_11_set_beacon(struct hostapd_data *hapd)
393{
394 struct ieee80211_mgmt *head;
395 u8 *pos, *tail, *tailpos;
6fc6879b
JM
396 u16 capab_info;
397 size_t head_len, tail_len;
6fc6879b 398
e44f8bf2
JM
399#ifdef CONFIG_P2P
400 if ((hapd->conf->p2p & (P2P_ENABLED | P2P_GROUP_OWNER)) == P2P_ENABLED)
401 goto no_beacon;
402#endif /* CONFIG_P2P */
403
6fc6879b
JM
404#define BEACON_HEAD_BUF_SIZE 256
405#define BEACON_TAIL_BUF_SIZE 512
406 head = os_zalloc(BEACON_HEAD_BUF_SIZE);
1c08f8c0
JM
407 tail_len = BEACON_TAIL_BUF_SIZE;
408#ifdef CONFIG_WPS
409 if (hapd->conf->wps_state && hapd->wps_beacon_ie)
410 tail_len += wpabuf_len(hapd->wps_beacon_ie);
411#endif /* CONFIG_WPS */
e44f8bf2
JM
412#ifdef CONFIG_P2P
413 if (hapd->p2p_beacon_ie)
414 tail_len += wpabuf_len(hapd->p2p_beacon_ie);
415#endif /* CONFIG_P2P */
1c08f8c0 416 tailpos = tail = os_malloc(tail_len);
6fc6879b
JM
417 if (head == NULL || tail == NULL) {
418 wpa_printf(MSG_ERROR, "Failed to set beacon data");
419 os_free(head);
420 os_free(tail);
421 return;
422 }
423
424 head->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
425 WLAN_FC_STYPE_BEACON);
426 head->duration = host_to_le16(0);
427 os_memset(head->da, 0xff, ETH_ALEN);
428
429 os_memcpy(head->sa, hapd->own_addr, ETH_ALEN);
430 os_memcpy(head->bssid, hapd->own_addr, ETH_ALEN);
431 head->u.beacon.beacon_int =
432 host_to_le16(hapd->iconf->beacon_int);
433
434 /* hardware or low-level driver will setup seq_ctrl and timestamp */
435 capab_info = hostapd_own_capab_info(hapd, NULL, 0);
436 head->u.beacon.capab_info = host_to_le16(capab_info);
437 pos = &head->u.beacon.variable[0];
438
439 /* SSID */
440 *pos++ = WLAN_EID_SSID;
441 if (hapd->conf->ignore_broadcast_ssid == 2) {
442 /* clear the data, but keep the correct length of the SSID */
443 *pos++ = hapd->conf->ssid.ssid_len;
444 os_memset(pos, 0, hapd->conf->ssid.ssid_len);
445 pos += hapd->conf->ssid.ssid_len;
446 } else if (hapd->conf->ignore_broadcast_ssid) {
447 *pos++ = 0; /* empty SSID */
448 } else {
449 *pos++ = hapd->conf->ssid.ssid_len;
450 os_memcpy(pos, hapd->conf->ssid.ssid,
451 hapd->conf->ssid.ssid_len);
452 pos += hapd->conf->ssid.ssid_len;
453 }
454
455 /* Supported rates */
456 pos = hostapd_eid_supp_rates(hapd, pos);
457
458 /* DS Params */
459 pos = hostapd_eid_ds_params(hapd, pos);
460
461 head_len = pos - (u8 *) head;
462
463 tailpos = hostapd_eid_country(hapd, tailpos,
464 tail + BEACON_TAIL_BUF_SIZE - tailpos);
465
6fc6879b
JM
466 /* ERP Information element */
467 tailpos = hostapd_eid_erp_info(hapd, tailpos);
468
469 /* Extended supported rates */
470 tailpos = hostapd_eid_ext_supp_rates(hapd, tailpos);
471
1bc774a1 472 /* RSN, MDIE, WPA */
6fc6879b
JM
473 tailpos = hostapd_eid_wpa(hapd, tailpos, tail + BEACON_TAIL_BUF_SIZE -
474 tailpos, NULL);
475
de9289c8 476#ifdef CONFIG_IEEE80211N
677449b9
JM
477 tailpos = hostapd_eid_ht_capabilities(hapd, tailpos);
478 tailpos = hostapd_eid_ht_operation(hapd, tailpos);
de9289c8
JM
479#endif /* CONFIG_IEEE80211N */
480
1bc774a1
JM
481 /* Wi-Fi Alliance WMM */
482 tailpos = hostapd_eid_wmm(hapd, tailpos);
483
ad08c363
JM
484#ifdef CONFIG_WPS
485 if (hapd->conf->wps_state && hapd->wps_beacon_ie) {
14f79386
JM
486 os_memcpy(tailpos, wpabuf_head(hapd->wps_beacon_ie),
487 wpabuf_len(hapd->wps_beacon_ie));
488 tailpos += wpabuf_len(hapd->wps_beacon_ie);
ad08c363
JM
489 }
490#endif /* CONFIG_WPS */
491
e44f8bf2
JM
492#ifdef CONFIG_P2P
493 if ((hapd->conf->p2p & P2P_ENABLED) && hapd->p2p_beacon_ie) {
494 os_memcpy(tailpos, wpabuf_head(hapd->p2p_beacon_ie),
495 wpabuf_len(hapd->p2p_beacon_ie));
496 tailpos += wpabuf_len(hapd->p2p_beacon_ie);
497 }
498#endif /* CONFIG_P2P */
962473c1
JM
499#ifdef CONFIG_P2P_MANAGER
500 if ((hapd->conf->p2p & (P2P_MANAGE | P2P_ENABLED | P2P_GROUP_OWNER)) ==
501 P2P_MANAGE)
502 tailpos = hostapd_eid_p2p_manage(hapd, tailpos);
503#endif /* CONFIG_P2P_MANAGER */
e44f8bf2 504
6fc6879b
JM
505 tail_len = tailpos > tail ? tailpos - tail : 0;
506
3acdf771
JM
507 if (hostapd_drv_set_beacon(hapd, (u8 *) head, head_len,
508 tail, tail_len, hapd->conf->dtim_period,
509 hapd->iconf->beacon_int))
74f2ad32
JM
510 wpa_printf(MSG_ERROR, "Failed to set beacon head/tail or DTIM "
511 "period");
6fc6879b
JM
512
513 os_free(tail);
514 os_free(head);
515
e44f8bf2
JM
516#ifdef CONFIG_P2P
517no_beacon:
518#endif /* CONFIG_P2P */
0e8a96a9
JM
519 hostapd_set_bss_params(hapd, !!(ieee802_11_erp_info(hapd) &
520 ERP_INFO_USE_PROTECTION));
6fc6879b
JM
521}
522
523
524void ieee802_11_set_beacons(struct hostapd_iface *iface)
525{
526 size_t i;
527 for (i = 0; i < iface->num_bss; i++)
528 ieee802_11_set_beacon(iface->bss[i]);
529}
530
531#endif /* CONFIG_NATIVE_WINDOWS */