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