Re-initialize hostapd/wpa_supplicant git repository based on 0.6.3 release
[wpasupplicant] / 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  *
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
21 #include "hostapd.h"
22 #include "ieee802_11.h"
23 #include "wpa.h"
24 #include "wme.h"
25 #include "beacon.h"
26 #include "hw_features.h"
27 #include "driver.h"
28 #include "sta_info.h"
29 #include "ieee802_11h.h"
30
31
32 static u8 ieee802_11_erp_info(struct hostapd_data *hapd)
33 {
34         u8 erp = 0;
35
36         if (hapd->iface->current_mode == NULL ||
37             hapd->iface->current_mode->mode != HOSTAPD_MODE_IEEE80211G)
38                 return 0;
39
40         switch (hapd->iconf->cts_protection_type) {
41         case CTS_PROTECTION_FORCE_ENABLED:
42                 erp |= ERP_INFO_NON_ERP_PRESENT | ERP_INFO_USE_PROTECTION;
43                 break;
44         case CTS_PROTECTION_FORCE_DISABLED:
45                 erp = 0;
46                 break;
47         case CTS_PROTECTION_AUTOMATIC:
48                 if (hapd->iface->olbc)
49                         erp |= ERP_INFO_USE_PROTECTION;
50                 /* continue */
51         case CTS_PROTECTION_AUTOMATIC_NO_OLBC:
52                 if (hapd->iface->num_sta_non_erp > 0) {
53                         erp |= ERP_INFO_NON_ERP_PRESENT |
54                                 ERP_INFO_USE_PROTECTION;
55                 }
56                 break;
57         }
58         if (hapd->iface->num_sta_no_short_preamble > 0)
59                 erp |= ERP_INFO_BARKER_PREAMBLE_MODE;
60
61         return erp;
62 }
63
64
65 static u8 * hostapd_eid_ds_params(struct hostapd_data *hapd, u8 *eid)
66 {
67         *eid++ = WLAN_EID_DS_PARAMS;
68         *eid++ = 1;
69         *eid++ = hapd->iconf->channel;
70         return eid;
71 }
72
73
74 static u8 * hostapd_eid_erp_info(struct hostapd_data *hapd, u8 *eid)
75 {
76         if (hapd->iface->current_mode == NULL ||
77             hapd->iface->current_mode->mode != HOSTAPD_MODE_IEEE80211G)
78                 return eid;
79
80         /* Set NonERP_present and use_protection bits if there
81          * are any associated NonERP stations. */
82         /* TODO: use_protection bit can be set to zero even if
83          * there are NonERP stations present. This optimization
84          * might be useful if NonERP stations are "quiet".
85          * See 802.11g/D6 E-1 for recommended practice.
86          * In addition, Non ERP present might be set, if AP detects Non ERP
87          * operation on other APs. */
88
89         /* Add ERP Information element */
90         *eid++ = WLAN_EID_ERP_INFO;
91         *eid++ = 1;
92         *eid++ = ieee802_11_erp_info(hapd);
93
94         return eid;
95 }
96
97
98 static u8 * hostapd_eid_country(struct hostapd_data *hapd, u8 *eid,
99                                 int max_len)
100 {
101         u8 *pos = eid;
102
103         if ((!hapd->iconf->ieee80211d && !hapd->iface->dfs_enable) ||
104             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_power_constraint(struct hostapd_data *hapd, u8 *eid)
122
123 {
124         if (!hapd->iface->dfs_enable)
125                 return eid;
126         *eid++ = WLAN_EID_PWR_CONSTRAINT;
127         *eid++ = 1;
128         *eid++ = hapd->iface->pwr_const;
129         return eid;
130 }
131
132
133 static u8 * hostapd_eid_tpc_report(struct hostapd_data *hapd, u8 *eid)
134
135 {
136         if (!hapd->iface->dfs_enable)
137                 return eid;
138         *eid++ = WLAN_EID_TPC_REPORT;
139         *eid++ = 2;
140         *eid++ = hapd->iface->tx_power; /* TX POWER */
141         *eid++ = 0; /* Link Margin */
142         return eid;
143 }
144
145 static u8 * hostapd_eid_channel_switch(struct hostapd_data *hapd, u8 *eid)
146
147 {
148         if (!hapd->iface->dfs_enable || !hapd->iface->channel_switch)
149                 return eid;
150         *eid++ = WLAN_EID_CHANNEL_SWITCH;
151         *eid++ = 3;
152         *eid++ = CHAN_SWITCH_MODE_QUIET;
153         *eid++ = hapd->iface->channel_switch; /* New channel */
154         /* 0 - very soon; 1 - before next TBTT; num - after num beacons */
155         *eid++ = 0;
156         return eid;
157 }
158
159
160 static u8 * hostapd_eid_wpa(struct hostapd_data *hapd, u8 *eid, size_t len,
161                             struct sta_info *sta)
162 {
163         const u8 *ie;
164         size_t ielen;
165
166         ie = wpa_auth_get_wpa_ie(hapd->wpa_auth, &ielen);
167         if (ie == NULL || ielen > len)
168                 return eid;
169
170         os_memcpy(eid, ie, ielen);
171         return eid + ielen;
172 }
173
174
175 void handle_probe_req(struct hostapd_data *hapd, struct ieee80211_mgmt *mgmt,
176                       size_t len)
177 {
178         struct ieee80211_mgmt *resp;
179         struct ieee802_11_elems elems;
180         char *ssid;
181         u8 *pos, *epos, *ie;
182         size_t ssid_len, ie_len;
183         struct sta_info *sta = NULL;
184
185         ie = mgmt->u.probe_req.variable;
186         ie_len = len - (IEEE80211_HDRLEN + sizeof(mgmt->u.probe_req));
187
188         if (!hapd->iconf->send_probe_response)
189                 return;
190
191         if (ieee802_11_parse_elems(hapd, ie, ie_len, &elems, 0) == ParseFailed)
192         {
193                 wpa_printf(MSG_DEBUG, "Could not parse ProbeReq from " MACSTR,
194                            MAC2STR(mgmt->sa));
195                 return;
196         }
197
198         ssid = NULL;
199         ssid_len = 0;
200
201         if ((!elems.ssid || !elems.supp_rates)) {
202                 wpa_printf(MSG_DEBUG, "STA " MACSTR " sent probe request "
203                            "without SSID or supported rates element",
204                            MAC2STR(mgmt->sa));
205                 return;
206         }
207
208         if (hapd->conf->ignore_broadcast_ssid && elems.ssid_len == 0) {
209                 wpa_printf(MSG_MSGDUMP, "Probe Request from " MACSTR " for "
210                            "broadcast SSID ignored", MAC2STR(mgmt->sa));
211                 return;
212         }
213
214         sta = ap_get_sta(hapd, mgmt->sa);
215
216         if (elems.ssid_len == 0 ||
217             (elems.ssid_len == hapd->conf->ssid.ssid_len &&
218              os_memcmp(elems.ssid, hapd->conf->ssid.ssid, elems.ssid_len) ==
219              0)) {
220                 ssid = hapd->conf->ssid.ssid;
221                 ssid_len = hapd->conf->ssid.ssid_len;
222                 if (sta)
223                         sta->ssid_probe = &hapd->conf->ssid;
224         }
225
226         if (!ssid) {
227                 if (!(mgmt->da[0] & 0x01)) {
228                         char ssid_txt[33];
229                         ieee802_11_print_ssid(ssid_txt, elems.ssid,
230                                               elems.ssid_len);
231                         wpa_printf(MSG_MSGDUMP, "Probe Request from " MACSTR
232                                    " for foreign SSID '%s'",
233                                    MAC2STR(mgmt->sa), ssid_txt);
234                 }
235                 return;
236         }
237
238         /* TODO: verify that supp_rates contains at least one matching rate
239          * with AP configuration */
240 #define MAX_PROBERESP_LEN 768
241         resp = os_zalloc(MAX_PROBERESP_LEN);
242         if (resp == NULL)
243                 return;
244         epos = ((u8 *) resp) + MAX_PROBERESP_LEN;
245
246         resp->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
247                                            WLAN_FC_STYPE_PROBE_RESP);
248         os_memcpy(resp->da, mgmt->sa, ETH_ALEN);
249         os_memcpy(resp->sa, hapd->own_addr, ETH_ALEN);
250
251         os_memcpy(resp->bssid, hapd->own_addr, ETH_ALEN);
252         resp->u.probe_resp.beacon_int =
253                 host_to_le16(hapd->iconf->beacon_int);
254
255         /* hardware or low-level driver will setup seq_ctrl and timestamp */
256         resp->u.probe_resp.capab_info =
257                 host_to_le16(hostapd_own_capab_info(hapd, sta, 1));
258
259         pos = resp->u.probe_resp.variable;
260         *pos++ = WLAN_EID_SSID;
261         *pos++ = ssid_len;
262         os_memcpy(pos, ssid, ssid_len);
263         pos += ssid_len;
264
265         /* Supported rates */
266         pos = hostapd_eid_supp_rates(hapd, pos);
267
268         /* DS Params */
269         pos = hostapd_eid_ds_params(hapd, pos);
270
271         pos = hostapd_eid_country(hapd, pos, epos - pos);
272
273         pos = hostapd_eid_power_constraint(hapd, pos);
274         pos = hostapd_eid_tpc_report(hapd, pos);
275
276         /* ERP Information element */
277         pos = hostapd_eid_erp_info(hapd, pos);
278
279         /* Extended supported rates */
280         pos = hostapd_eid_ext_supp_rates(hapd, pos);
281
282         pos = hostapd_eid_wpa(hapd, pos, epos - pos, sta);
283
284         /* Wi-Fi Wireless Multimedia Extensions */
285         if (hapd->conf->wme_enabled)
286                 pos = hostapd_eid_wme(hapd, pos);
287
288         if (hostapd_send_mgmt_frame(hapd, resp, pos - (u8 *) resp, 0) < 0)
289                 perror("handle_probe_req: send");
290
291         os_free(resp);
292
293         wpa_printf(MSG_MSGDUMP, "STA " MACSTR " sent probe request for %s "
294                    "SSID", MAC2STR(mgmt->sa),
295                    elems.ssid_len == 0 ? "broadcast" : "our");
296 }
297
298
299 void ieee802_11_set_beacon(struct hostapd_data *hapd)
300 {
301         struct ieee80211_mgmt *head;
302         u8 *pos, *tail, *tailpos;
303         int preamble;
304         u16 capab_info;
305         size_t head_len, tail_len;
306         int cts_protection = ((ieee802_11_erp_info(hapd) &
307                               ERP_INFO_USE_PROTECTION) ? 1 : 0);
308
309 #define BEACON_HEAD_BUF_SIZE 256
310 #define BEACON_TAIL_BUF_SIZE 512
311         head = os_zalloc(BEACON_HEAD_BUF_SIZE);
312         tailpos = tail = os_malloc(BEACON_TAIL_BUF_SIZE);
313         if (head == NULL || tail == NULL) {
314                 wpa_printf(MSG_ERROR, "Failed to set beacon data");
315                 os_free(head);
316                 os_free(tail);
317                 return;
318         }
319
320         head->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
321                                            WLAN_FC_STYPE_BEACON);
322         head->duration = host_to_le16(0);
323         os_memset(head->da, 0xff, ETH_ALEN);
324
325         os_memcpy(head->sa, hapd->own_addr, ETH_ALEN);
326         os_memcpy(head->bssid, hapd->own_addr, ETH_ALEN);
327         head->u.beacon.beacon_int =
328                 host_to_le16(hapd->iconf->beacon_int);
329
330         /* hardware or low-level driver will setup seq_ctrl and timestamp */
331         capab_info = hostapd_own_capab_info(hapd, NULL, 0);
332         head->u.beacon.capab_info = host_to_le16(capab_info);
333         pos = &head->u.beacon.variable[0];
334
335         /* SSID */
336         *pos++ = WLAN_EID_SSID;
337         if (hapd->conf->ignore_broadcast_ssid == 2) {
338                 /* clear the data, but keep the correct length of the SSID */
339                 *pos++ = hapd->conf->ssid.ssid_len;
340                 os_memset(pos, 0, hapd->conf->ssid.ssid_len);
341                 pos += hapd->conf->ssid.ssid_len;
342         } else if (hapd->conf->ignore_broadcast_ssid) {
343                 *pos++ = 0; /* empty SSID */
344         } else {
345                 *pos++ = hapd->conf->ssid.ssid_len;
346                 os_memcpy(pos, hapd->conf->ssid.ssid,
347                           hapd->conf->ssid.ssid_len);
348                 pos += hapd->conf->ssid.ssid_len;
349         }
350
351         /* Supported rates */
352         pos = hostapd_eid_supp_rates(hapd, pos);
353
354         /* DS Params */
355         pos = hostapd_eid_ds_params(hapd, pos);
356
357         head_len = pos - (u8 *) head;
358
359         tailpos = hostapd_eid_country(hapd, tailpos,
360                                       tail + BEACON_TAIL_BUF_SIZE - tailpos);
361
362         tailpos = hostapd_eid_power_constraint(hapd, tailpos);
363         tailpos = hostapd_eid_channel_switch(hapd, tailpos);
364         tailpos = hostapd_eid_tpc_report(hapd, tailpos);
365
366         /* ERP Information element */
367         tailpos = hostapd_eid_erp_info(hapd, tailpos);
368
369         /* Extended supported rates */
370         tailpos = hostapd_eid_ext_supp_rates(hapd, tailpos);
371
372         tailpos = hostapd_eid_wpa(hapd, tailpos, tail + BEACON_TAIL_BUF_SIZE -
373                                   tailpos, NULL);
374
375         /* Wi-Fi Wireless Multimedia Extensions */
376         if (hapd->conf->wme_enabled)
377                 tailpos = hostapd_eid_wme(hapd, tailpos);
378
379         tail_len = tailpos > tail ? tailpos - tail : 0;
380
381         if (hostapd_set_beacon(hapd->conf->iface, hapd, (u8 *) head, head_len,
382                                tail, tail_len))
383                 wpa_printf(MSG_ERROR, "Failed to set beacon head/tail");
384
385         os_free(tail);
386         os_free(head);
387
388         if (hostapd_set_cts_protect(hapd, cts_protection))
389                 wpa_printf(MSG_ERROR, "Failed to set CTS protect in kernel "
390                            "driver");
391
392         if (hapd->iface->current_mode &&
393             hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G &&
394             hostapd_set_short_slot_time(hapd,
395                                         hapd->iface->num_sta_no_short_slot_time
396                                         > 0 ? 0 : 1))
397                 wpa_printf(MSG_ERROR, "Failed to set Short Slot Time option "
398                            "in kernel driver");
399
400         if (hapd->iface->num_sta_no_short_preamble == 0 &&
401             hapd->iconf->preamble == SHORT_PREAMBLE)
402                 preamble = SHORT_PREAMBLE;
403         else
404                 preamble = LONG_PREAMBLE;
405         if (hostapd_set_preamble(hapd, preamble))
406                 wpa_printf(MSG_ERROR, "Could not set preamble for kernel "
407                            "driver");
408 }
409
410
411 void ieee802_11_set_beacons(struct hostapd_iface *iface)
412 {
413         size_t i;
414         for (i = 0; i < iface->num_bss; i++)
415                 ieee802_11_set_beacon(iface->bss[i]);
416 }
417
418 #endif /* CONFIG_NATIVE_WINDOWS */