a464db73233eb10afbee0bd45e1f073c7c0046d1
[mech_eap.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, 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 "hostapd.h"
23 #include "ieee802_11.h"
24 #include "wpa.h"
25 #include "wme.h"
26 #include "beacon.h"
27 #include "hw_features.h"
28 #include "driver.h"
29 #include "sta_info.h"
30 #include "wps_hostapd.h"
31
32
33 static 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
66 static 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
75 static 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
99 static u8 * hostapd_eid_country(struct hostapd_data *hapd, u8 *eid,
100                                 int max_len)
101 {
102         u8 *pos = eid;
103
104         if (!hapd->iconf->ieee80211d || max_len < 6)
105                 return eid;
106
107         *pos++ = WLAN_EID_COUNTRY;
108         pos++; /* length will be set later */
109         os_memcpy(pos, hapd->iconf->country, 3); /* e.g., 'US ' */
110         pos += 3;
111
112         if ((pos - eid) & 1)
113                 *pos++ = 0; /* pad for 16-bit alignment */
114
115         eid[1] = (pos - eid) - 2;
116
117         return pos;
118 }
119
120
121 static u8 * hostapd_eid_wpa(struct hostapd_data *hapd, u8 *eid, size_t len,
122                             struct sta_info *sta)
123 {
124         const u8 *ie;
125         size_t ielen;
126
127         ie = wpa_auth_get_wpa_ie(hapd->wpa_auth, &ielen);
128         if (ie == NULL || ielen > len)
129                 return eid;
130
131         os_memcpy(eid, ie, ielen);
132         return eid + ielen;
133 }
134
135
136 void handle_probe_req(struct hostapd_data *hapd, struct ieee80211_mgmt *mgmt,
137                       size_t len)
138 {
139         struct ieee80211_mgmt *resp;
140         struct ieee802_11_elems elems;
141         char *ssid;
142         u8 *pos, *epos, *ie;
143         size_t ssid_len, ie_len;
144         struct sta_info *sta = NULL;
145
146         ie = mgmt->u.probe_req.variable;
147         ie_len = len - (IEEE80211_HDRLEN + sizeof(mgmt->u.probe_req));
148
149         hostapd_wps_probe_req_rx(hapd, mgmt->sa, ie, ie_len);
150
151         if (!hapd->iconf->send_probe_response)
152                 return;
153
154         if (ieee802_11_parse_elems(ie, ie_len, &elems, 0) == ParseFailed) {
155                 wpa_printf(MSG_DEBUG, "Could not parse ProbeReq from " MACSTR,
156                            MAC2STR(mgmt->sa));
157                 return;
158         }
159
160         ssid = NULL;
161         ssid_len = 0;
162
163         if ((!elems.ssid || !elems.supp_rates)) {
164                 wpa_printf(MSG_DEBUG, "STA " MACSTR " sent probe request "
165                            "without SSID or supported rates element",
166                            MAC2STR(mgmt->sa));
167                 return;
168         }
169
170         if (hapd->conf->ignore_broadcast_ssid && elems.ssid_len == 0) {
171                 wpa_printf(MSG_MSGDUMP, "Probe Request from " MACSTR " for "
172                            "broadcast SSID ignored", MAC2STR(mgmt->sa));
173                 return;
174         }
175
176         sta = ap_get_sta(hapd, mgmt->sa);
177
178         if (elems.ssid_len == 0 ||
179             (elems.ssid_len == hapd->conf->ssid.ssid_len &&
180              os_memcmp(elems.ssid, hapd->conf->ssid.ssid, elems.ssid_len) ==
181              0)) {
182                 ssid = hapd->conf->ssid.ssid;
183                 ssid_len = hapd->conf->ssid.ssid_len;
184                 if (sta)
185                         sta->ssid_probe = &hapd->conf->ssid;
186         }
187
188         if (!ssid) {
189                 if (!(mgmt->da[0] & 0x01)) {
190                         char ssid_txt[33];
191                         ieee802_11_print_ssid(ssid_txt, elems.ssid,
192                                               elems.ssid_len);
193                         wpa_printf(MSG_MSGDUMP, "Probe Request from " MACSTR
194                                    " for foreign SSID '%s'",
195                                    MAC2STR(mgmt->sa), ssid_txt);
196                 }
197                 return;
198         }
199
200         /* TODO: verify that supp_rates contains at least one matching rate
201          * with AP configuration */
202 #define MAX_PROBERESP_LEN 768
203         resp = os_zalloc(MAX_PROBERESP_LEN);
204         if (resp == NULL)
205                 return;
206         epos = ((u8 *) resp) + MAX_PROBERESP_LEN;
207
208         resp->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
209                                            WLAN_FC_STYPE_PROBE_RESP);
210         os_memcpy(resp->da, mgmt->sa, ETH_ALEN);
211         os_memcpy(resp->sa, hapd->own_addr, ETH_ALEN);
212
213         os_memcpy(resp->bssid, hapd->own_addr, ETH_ALEN);
214         resp->u.probe_resp.beacon_int =
215                 host_to_le16(hapd->iconf->beacon_int);
216
217         /* hardware or low-level driver will setup seq_ctrl and timestamp */
218         resp->u.probe_resp.capab_info =
219                 host_to_le16(hostapd_own_capab_info(hapd, sta, 1));
220
221         pos = resp->u.probe_resp.variable;
222         *pos++ = WLAN_EID_SSID;
223         *pos++ = ssid_len;
224         os_memcpy(pos, ssid, ssid_len);
225         pos += ssid_len;
226
227         /* Supported rates */
228         pos = hostapd_eid_supp_rates(hapd, pos);
229
230         /* DS Params */
231         pos = hostapd_eid_ds_params(hapd, pos);
232
233         pos = hostapd_eid_country(hapd, pos, epos - pos);
234
235         /* ERP Information element */
236         pos = hostapd_eid_erp_info(hapd, pos);
237
238         /* Extended supported rates */
239         pos = hostapd_eid_ext_supp_rates(hapd, pos);
240
241         pos = hostapd_eid_wpa(hapd, pos, epos - pos, sta);
242
243         /* Wi-Fi Wireless Multimedia Extensions */
244         pos = hostapd_eid_wme(hapd, pos);
245
246         pos = hostapd_eid_ht_capabilities_info(hapd, pos);
247         pos = hostapd_eid_ht_operation(hapd, pos);
248
249 #ifdef CONFIG_WPS
250         if (hapd->conf->wps_state && hapd->wps_probe_resp_ie) {
251                 os_memcpy(pos, hapd->wps_probe_resp_ie,
252                           hapd->wps_probe_resp_ie_len);
253                 pos += hapd->wps_probe_resp_ie_len;
254         }
255 #endif /* CONFIG_WPS */
256
257         if (hostapd_send_mgmt_frame(hapd, resp, pos - (u8 *) resp, 0) < 0)
258                 perror("handle_probe_req: send");
259
260         os_free(resp);
261
262         wpa_printf(MSG_MSGDUMP, "STA " MACSTR " sent probe request for %s "
263                    "SSID", MAC2STR(mgmt->sa),
264                    elems.ssid_len == 0 ? "broadcast" : "our");
265 }
266
267
268 void ieee802_11_set_beacon(struct hostapd_data *hapd)
269 {
270         struct ieee80211_mgmt *head;
271         u8 *pos, *tail, *tailpos;
272         int preamble;
273         u16 capab_info;
274         size_t head_len, tail_len;
275         int cts_protection = ((ieee802_11_erp_info(hapd) &
276                               ERP_INFO_USE_PROTECTION) ? 1 : 0);
277
278 #define BEACON_HEAD_BUF_SIZE 256
279 #define BEACON_TAIL_BUF_SIZE 512
280         head = os_zalloc(BEACON_HEAD_BUF_SIZE);
281         tailpos = tail = os_malloc(BEACON_TAIL_BUF_SIZE);
282         if (head == NULL || tail == NULL) {
283                 wpa_printf(MSG_ERROR, "Failed to set beacon data");
284                 os_free(head);
285                 os_free(tail);
286                 return;
287         }
288
289         head->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
290                                            WLAN_FC_STYPE_BEACON);
291         head->duration = host_to_le16(0);
292         os_memset(head->da, 0xff, ETH_ALEN);
293
294         os_memcpy(head->sa, hapd->own_addr, ETH_ALEN);
295         os_memcpy(head->bssid, hapd->own_addr, ETH_ALEN);
296         head->u.beacon.beacon_int =
297                 host_to_le16(hapd->iconf->beacon_int);
298
299         /* hardware or low-level driver will setup seq_ctrl and timestamp */
300         capab_info = hostapd_own_capab_info(hapd, NULL, 0);
301         head->u.beacon.capab_info = host_to_le16(capab_info);
302         pos = &head->u.beacon.variable[0];
303
304         /* SSID */
305         *pos++ = WLAN_EID_SSID;
306         if (hapd->conf->ignore_broadcast_ssid == 2) {
307                 /* clear the data, but keep the correct length of the SSID */
308                 *pos++ = hapd->conf->ssid.ssid_len;
309                 os_memset(pos, 0, hapd->conf->ssid.ssid_len);
310                 pos += hapd->conf->ssid.ssid_len;
311         } else if (hapd->conf->ignore_broadcast_ssid) {
312                 *pos++ = 0; /* empty SSID */
313         } else {
314                 *pos++ = hapd->conf->ssid.ssid_len;
315                 os_memcpy(pos, hapd->conf->ssid.ssid,
316                           hapd->conf->ssid.ssid_len);
317                 pos += hapd->conf->ssid.ssid_len;
318         }
319
320         /* Supported rates */
321         pos = hostapd_eid_supp_rates(hapd, pos);
322
323         /* DS Params */
324         pos = hostapd_eid_ds_params(hapd, pos);
325
326         head_len = pos - (u8 *) head;
327
328         tailpos = hostapd_eid_country(hapd, tailpos,
329                                       tail + BEACON_TAIL_BUF_SIZE - tailpos);
330
331         /* ERP Information element */
332         tailpos = hostapd_eid_erp_info(hapd, tailpos);
333
334         /* Extended supported rates */
335         tailpos = hostapd_eid_ext_supp_rates(hapd, tailpos);
336
337         tailpos = hostapd_eid_wpa(hapd, tailpos, tail + BEACON_TAIL_BUF_SIZE -
338                                   tailpos, NULL);
339
340         /* Wi-Fi Wireless Multimedia Extensions */
341         tailpos = hostapd_eid_wme(hapd, tailpos);
342
343 #ifdef CONFIG_IEEE80211N
344         if (hapd->iconf->ieee80211n) {
345                 u8 *start;
346                 start = tailpos;
347                 tailpos = hostapd_eid_ht_capabilities_info(hapd, tailpos);
348                 if (hostapd_set_ht_capability(hapd->conf->iface, hapd,
349                                               start + 2)) {
350                         wpa_printf(MSG_ERROR, "Could not set HT capabilities "
351                                    "for kernel driver");
352                 }
353
354                 start = tailpos;
355                 tailpos = hostapd_eid_ht_operation(hapd, tailpos);
356                 if (hostapd_set_ht_operation(hapd->conf->iface, hapd,
357                                              start + 2))
358                         wpa_printf(MSG_ERROR, "Could not set HT operation for "
359                                    "kernel driver");
360         }
361 #endif /* CONFIG_IEEE80211N */
362
363 #ifdef CONFIG_WPS
364         if (hapd->conf->wps_state && hapd->wps_beacon_ie) {
365                 os_memcpy(tailpos, hapd->wps_beacon_ie,
366                           hapd->wps_beacon_ie_len);
367                 tailpos += hapd->wps_beacon_ie_len;
368         }
369 #endif /* CONFIG_WPS */
370
371         tail_len = tailpos > tail ? tailpos - tail : 0;
372
373         if (hostapd_set_beacon(hapd->conf->iface, hapd, (u8 *) head, head_len,
374                                tail, tail_len))
375                 wpa_printf(MSG_ERROR, "Failed to set beacon head/tail");
376
377         os_free(tail);
378         os_free(head);
379
380         if (hostapd_set_cts_protect(hapd, cts_protection))
381                 wpa_printf(MSG_ERROR, "Failed to set CTS protect in kernel "
382                            "driver");
383
384         if (hapd->iface->current_mode &&
385             hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G &&
386             hostapd_set_short_slot_time(hapd,
387                                         hapd->iface->num_sta_no_short_slot_time
388                                         > 0 ? 0 : 1))
389                 wpa_printf(MSG_ERROR, "Failed to set Short Slot Time option "
390                            "in kernel driver");
391
392         if (hapd->iface->num_sta_no_short_preamble == 0 &&
393             hapd->iconf->preamble == SHORT_PREAMBLE)
394                 preamble = SHORT_PREAMBLE;
395         else
396                 preamble = LONG_PREAMBLE;
397         if (hostapd_set_preamble(hapd, preamble))
398                 wpa_printf(MSG_ERROR, "Could not set preamble for kernel "
399                            "driver");
400 }
401
402
403 void ieee802_11_set_beacons(struct hostapd_iface *iface)
404 {
405         size_t i;
406         for (i = 0; i < iface->num_bss; i++)
407                 ieee802_11_set_beacon(iface->bss[i]);
408 }
409
410 #endif /* CONFIG_NATIVE_WINDOWS */