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