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