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