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