Include driver.h in hostapd.h
[mech_eap.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-2012, 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 "wps/wps_defs.h"
25 #include "p2p/p2p.h"
26 #include "hostapd.h"
27 #include "ieee802_11.h"
28 #include "wpa_auth.h"
29 #include "wmm.h"
30 #include "ap_config.h"
31 #include "sta_info.h"
32 #include "p2p_hostapd.h"
33 #include "ap_drv_ops.h"
34 #include "beacon.h"
35 #include "hs20.h"
36
37
38 #ifdef NEED_AP_MLME
39
40 static u8 * hostapd_eid_bss_load(struct hostapd_data *hapd, u8 *eid, size_t len)
41 {
42 #ifdef CONFIG_TESTING_OPTIONS
43         if (hapd->conf->bss_load_test_set) {
44                 if (2 + 5 > len)
45                         return eid;
46                 *eid++ = WLAN_EID_BSS_LOAD;
47                 *eid++ = 5;
48                 os_memcpy(eid, hapd->conf->bss_load_test, 5);
49                 eid += 5;
50         }
51 #endif /* CONFIG_TESTING_OPTIONS */
52         return eid;
53 }
54
55
56 static u8 ieee802_11_erp_info(struct hostapd_data *hapd)
57 {
58         u8 erp = 0;
59
60         if (hapd->iface->current_mode == NULL ||
61             hapd->iface->current_mode->mode != HOSTAPD_MODE_IEEE80211G)
62                 return 0;
63
64         if (hapd->iface->olbc)
65                 erp |= ERP_INFO_USE_PROTECTION;
66         if (hapd->iface->num_sta_non_erp > 0) {
67                 erp |= ERP_INFO_NON_ERP_PRESENT |
68                         ERP_INFO_USE_PROTECTION;
69         }
70         if (hapd->iface->num_sta_no_short_preamble > 0 ||
71             hapd->iconf->preamble == LONG_PREAMBLE)
72                 erp |= ERP_INFO_BARKER_PREAMBLE_MODE;
73
74         return erp;
75 }
76
77
78 static u8 * hostapd_eid_ds_params(struct hostapd_data *hapd, u8 *eid)
79 {
80         *eid++ = WLAN_EID_DS_PARAMS;
81         *eid++ = 1;
82         *eid++ = hapd->iconf->channel;
83         return eid;
84 }
85
86
87 static u8 * hostapd_eid_erp_info(struct hostapd_data *hapd, u8 *eid)
88 {
89         if (hapd->iface->current_mode == NULL ||
90             hapd->iface->current_mode->mode != HOSTAPD_MODE_IEEE80211G)
91                 return eid;
92
93         /* Set NonERP_present and use_protection bits if there
94          * are any associated NonERP stations. */
95         /* TODO: use_protection bit can be set to zero even if
96          * there are NonERP stations present. This optimization
97          * might be useful if NonERP stations are "quiet".
98          * See 802.11g/D6 E-1 for recommended practice.
99          * In addition, Non ERP present might be set, if AP detects Non ERP
100          * operation on other APs. */
101
102         /* Add ERP Information element */
103         *eid++ = WLAN_EID_ERP_INFO;
104         *eid++ = 1;
105         *eid++ = ieee802_11_erp_info(hapd);
106
107         return eid;
108 }
109
110
111 static u8 * hostapd_eid_country_add(u8 *pos, u8 *end, int chan_spacing,
112                                     struct hostapd_channel_data *start,
113                                     struct hostapd_channel_data *prev)
114 {
115         if (end - pos < 3)
116                 return pos;
117
118         /* first channel number */
119         *pos++ = start->chan;
120         /* number of channels */
121         *pos++ = (prev->chan - start->chan) / chan_spacing + 1;
122         /* maximum transmit power level */
123         *pos++ = start->max_tx_power;
124
125         return pos;
126 }
127
128
129 static u8 * hostapd_eid_country(struct hostapd_data *hapd, u8 *eid,
130                                 int max_len)
131 {
132         u8 *pos = eid;
133         u8 *end = eid + max_len;
134         int i;
135         struct hostapd_hw_modes *mode;
136         struct hostapd_channel_data *start, *prev;
137         int chan_spacing = 1;
138
139         if (!hapd->iconf->ieee80211d || max_len < 6 ||
140             hapd->iface->current_mode == NULL)
141                 return eid;
142
143         *pos++ = WLAN_EID_COUNTRY;
144         pos++; /* length will be set later */
145         os_memcpy(pos, hapd->iconf->country, 3); /* e.g., 'US ' */
146         pos += 3;
147
148         mode = hapd->iface->current_mode;
149         if (mode->mode == HOSTAPD_MODE_IEEE80211A)
150                 chan_spacing = 4;
151
152         start = prev = NULL;
153         for (i = 0; i < mode->num_channels; i++) {
154                 struct hostapd_channel_data *chan = &mode->channels[i];
155                 if (chan->flag & HOSTAPD_CHAN_DISABLED)
156                         continue;
157                 if (start && prev &&
158                     prev->chan + chan_spacing == chan->chan &&
159                     start->max_tx_power == chan->max_tx_power) {
160                         prev = chan;
161                         continue; /* can use same entry */
162                 }
163
164                 if (start) {
165                         pos = hostapd_eid_country_add(pos, end, chan_spacing,
166                                                       start, prev);
167                         start = NULL;
168                 }
169
170                 /* Start new group */
171                 start = prev = chan;
172         }
173
174         if (start) {
175                 pos = hostapd_eid_country_add(pos, end, chan_spacing,
176                                               start, prev);
177         }
178
179         if ((pos - eid) & 1) {
180                 if (end - pos < 1)
181                         return eid;
182                 *pos++ = 0; /* pad for 16-bit alignment */
183         }
184
185         eid[1] = (pos - eid) - 2;
186
187         return pos;
188 }
189
190
191 static u8 * hostapd_eid_wpa(struct hostapd_data *hapd, u8 *eid, size_t len)
192 {
193         const u8 *ie;
194         size_t ielen;
195
196         ie = wpa_auth_get_wpa_ie(hapd->wpa_auth, &ielen);
197         if (ie == NULL || ielen > len)
198                 return eid;
199
200         os_memcpy(eid, ie, ielen);
201         return eid + ielen;
202 }
203
204
205 static u8 * hostapd_eid_csa(struct hostapd_data *hapd, u8 *eid)
206 {
207         u8 chan;
208
209         if (!hapd->iface->cs_freq)
210                 return eid;
211
212         if (ieee80211_freq_to_chan(hapd->iface->cs_freq, &chan) ==
213             NUM_HOSTAPD_MODES)
214                 return eid;
215
216         *eid++ = WLAN_EID_CHANNEL_SWITCH;
217         *eid++ = 3;
218         *eid++ = hapd->iface->cs_block_tx;
219         *eid++ = chan;
220         *eid++ = hapd->iface->cs_count;
221
222         return eid;
223 }
224
225
226 static u8 * hostapd_gen_probe_resp(struct hostapd_data *hapd,
227                                    struct sta_info *sta,
228                                    const struct ieee80211_mgmt *req,
229                                    int is_p2p, size_t *resp_len)
230 {
231         struct ieee80211_mgmt *resp;
232         u8 *pos, *epos, *old_pos;
233         size_t buflen;
234
235 #define MAX_PROBERESP_LEN 768
236         buflen = MAX_PROBERESP_LEN;
237 #ifdef CONFIG_WPS
238         if (hapd->wps_probe_resp_ie)
239                 buflen += wpabuf_len(hapd->wps_probe_resp_ie);
240 #endif /* CONFIG_WPS */
241 #ifdef CONFIG_P2P
242         if (hapd->p2p_probe_resp_ie)
243                 buflen += wpabuf_len(hapd->p2p_probe_resp_ie);
244 #endif /* CONFIG_P2P */
245         if (hapd->conf->vendor_elements)
246                 buflen += wpabuf_len(hapd->conf->vendor_elements);
247         resp = os_zalloc(buflen);
248         if (resp == NULL)
249                 return NULL;
250
251         epos = ((u8 *) resp) + MAX_PROBERESP_LEN;
252
253         resp->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
254                                            WLAN_FC_STYPE_PROBE_RESP);
255         if (req)
256                 os_memcpy(resp->da, req->sa, ETH_ALEN);
257         os_memcpy(resp->sa, hapd->own_addr, ETH_ALEN);
258
259         os_memcpy(resp->bssid, hapd->own_addr, ETH_ALEN);
260         resp->u.probe_resp.beacon_int =
261                 host_to_le16(hapd->iconf->beacon_int);
262
263         /* hardware or low-level driver will setup seq_ctrl and timestamp */
264         resp->u.probe_resp.capab_info =
265                 host_to_le16(hostapd_own_capab_info(hapd, sta, 1));
266
267         pos = resp->u.probe_resp.variable;
268         *pos++ = WLAN_EID_SSID;
269         *pos++ = hapd->conf->ssid.ssid_len;
270         os_memcpy(pos, hapd->conf->ssid.ssid, hapd->conf->ssid.ssid_len);
271         pos += hapd->conf->ssid.ssid_len;
272
273         /* Supported rates */
274         pos = hostapd_eid_supp_rates(hapd, pos);
275
276         /* DS Params */
277         pos = hostapd_eid_ds_params(hapd, pos);
278
279         pos = hostapd_eid_country(hapd, pos, epos - pos);
280
281         /* ERP Information element */
282         pos = hostapd_eid_erp_info(hapd, pos);
283
284         /* Extended supported rates */
285         pos = hostapd_eid_ext_supp_rates(hapd, pos);
286
287         /* RSN, MDIE, WPA */
288         pos = hostapd_eid_wpa(hapd, pos, epos - pos);
289
290         pos = hostapd_eid_bss_load(hapd, pos, epos - pos);
291
292 #ifdef CONFIG_IEEE80211N
293         pos = hostapd_eid_ht_capabilities(hapd, pos);
294         pos = hostapd_eid_ht_operation(hapd, pos);
295 #endif /* CONFIG_IEEE80211N */
296
297         pos = hostapd_eid_ext_capab(hapd, pos);
298
299         pos = hostapd_eid_time_adv(hapd, pos);
300         pos = hostapd_eid_time_zone(hapd, pos);
301
302         pos = hostapd_eid_interworking(hapd, pos);
303         pos = hostapd_eid_adv_proto(hapd, pos);
304         pos = hostapd_eid_roaming_consortium(hapd, pos);
305
306         old_pos = pos;
307         pos = hostapd_eid_csa(hapd, pos);
308
309         /* save an offset to the counter - should be last byte */
310         hapd->iface->cs_c_off_proberesp = (pos != old_pos) ?
311                 pos - (u8 *) resp - 1 : 0;
312
313 #ifdef CONFIG_IEEE80211AC
314         pos = hostapd_eid_vht_capabilities(hapd, pos);
315         pos = hostapd_eid_vht_operation(hapd, pos);
316 #endif /* CONFIG_IEEE80211AC */
317
318         /* Wi-Fi Alliance WMM */
319         pos = hostapd_eid_wmm(hapd, pos);
320
321 #ifdef CONFIG_WPS
322         if (hapd->conf->wps_state && hapd->wps_probe_resp_ie) {
323                 os_memcpy(pos, wpabuf_head(hapd->wps_probe_resp_ie),
324                           wpabuf_len(hapd->wps_probe_resp_ie));
325                 pos += wpabuf_len(hapd->wps_probe_resp_ie);
326         }
327 #endif /* CONFIG_WPS */
328
329 #ifdef CONFIG_P2P
330         if ((hapd->conf->p2p & P2P_ENABLED) && is_p2p &&
331             hapd->p2p_probe_resp_ie) {
332                 os_memcpy(pos, wpabuf_head(hapd->p2p_probe_resp_ie),
333                           wpabuf_len(hapd->p2p_probe_resp_ie));
334                 pos += wpabuf_len(hapd->p2p_probe_resp_ie);
335         }
336 #endif /* CONFIG_P2P */
337 #ifdef CONFIG_P2P_MANAGER
338         if ((hapd->conf->p2p & (P2P_MANAGE | P2P_ENABLED | P2P_GROUP_OWNER)) ==
339             P2P_MANAGE)
340                 pos = hostapd_eid_p2p_manage(hapd, pos);
341 #endif /* CONFIG_P2P_MANAGER */
342
343 #ifdef CONFIG_HS20
344         pos = hostapd_eid_hs20_indication(hapd, pos);
345 #endif /* CONFIG_HS20 */
346
347         if (hapd->conf->vendor_elements) {
348                 os_memcpy(pos, wpabuf_head(hapd->conf->vendor_elements),
349                           wpabuf_len(hapd->conf->vendor_elements));
350                 pos += wpabuf_len(hapd->conf->vendor_elements);
351         }
352
353         *resp_len = pos - (u8 *) resp;
354         return (u8 *) resp;
355 }
356
357
358 enum ssid_match_result {
359         NO_SSID_MATCH,
360         EXACT_SSID_MATCH,
361         WILDCARD_SSID_MATCH
362 };
363
364 static enum ssid_match_result ssid_match(struct hostapd_data *hapd,
365                                          const u8 *ssid, size_t ssid_len,
366                                          const u8 *ssid_list,
367                                          size_t ssid_list_len)
368 {
369         const u8 *pos, *end;
370         int wildcard = 0;
371
372         if (ssid_len == 0)
373                 wildcard = 1;
374         if (ssid_len == hapd->conf->ssid.ssid_len &&
375             os_memcmp(ssid, hapd->conf->ssid.ssid, ssid_len) == 0)
376                 return EXACT_SSID_MATCH;
377
378         if (ssid_list == NULL)
379                 return wildcard ? WILDCARD_SSID_MATCH : NO_SSID_MATCH;
380
381         pos = ssid_list;
382         end = ssid_list + ssid_list_len;
383         while (pos + 1 <= end) {
384                 if (pos + 2 + pos[1] > end)
385                         break;
386                 if (pos[1] == 0)
387                         wildcard = 1;
388                 if (pos[1] == hapd->conf->ssid.ssid_len &&
389                     os_memcmp(pos + 2, hapd->conf->ssid.ssid, pos[1]) == 0)
390                         return EXACT_SSID_MATCH;
391                 pos += 2 + pos[1];
392         }
393
394         return wildcard ? WILDCARD_SSID_MATCH : NO_SSID_MATCH;
395 }
396
397
398 void handle_probe_req(struct hostapd_data *hapd,
399                       const struct ieee80211_mgmt *mgmt, size_t len,
400                       int ssi_signal)
401 {
402         u8 *resp;
403         struct ieee802_11_elems elems;
404         const u8 *ie;
405         size_t ie_len;
406         struct sta_info *sta = NULL;
407         size_t i, resp_len;
408         int noack;
409         enum ssid_match_result res;
410
411         ie = mgmt->u.probe_req.variable;
412         if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.probe_req))
413                 return;
414         ie_len = len - (IEEE80211_HDRLEN + sizeof(mgmt->u.probe_req));
415
416         for (i = 0; hapd->probereq_cb && i < hapd->num_probereq_cb; i++)
417                 if (hapd->probereq_cb[i].cb(hapd->probereq_cb[i].ctx,
418                                             mgmt->sa, mgmt->da, mgmt->bssid,
419                                             ie, ie_len, ssi_signal) > 0)
420                         return;
421
422         if (!hapd->iconf->send_probe_response)
423                 return;
424
425         if (ieee802_11_parse_elems(ie, ie_len, &elems, 0) == ParseFailed) {
426                 wpa_printf(MSG_DEBUG, "Could not parse ProbeReq from " MACSTR,
427                            MAC2STR(mgmt->sa));
428                 return;
429         }
430
431         if ((!elems.ssid || !elems.supp_rates)) {
432                 wpa_printf(MSG_DEBUG, "STA " MACSTR " sent probe request "
433                            "without SSID or supported rates element",
434                            MAC2STR(mgmt->sa));
435                 return;
436         }
437
438 #ifdef CONFIG_P2P
439         if (hapd->p2p && elems.wps_ie) {
440                 struct wpabuf *wps;
441                 wps = ieee802_11_vendor_ie_concat(ie, ie_len, WPS_DEV_OUI_WFA);
442                 if (wps && !p2p_group_match_dev_type(hapd->p2p_group, wps)) {
443                         wpa_printf(MSG_MSGDUMP, "P2P: Ignore Probe Request "
444                                    "due to mismatch with Requested Device "
445                                    "Type");
446                         wpabuf_free(wps);
447                         return;
448                 }
449                 wpabuf_free(wps);
450         }
451
452         if (hapd->p2p && elems.p2p) {
453                 struct wpabuf *p2p;
454                 p2p = ieee802_11_vendor_ie_concat(ie, ie_len, P2P_IE_VENDOR_TYPE);
455                 if (p2p && !p2p_group_match_dev_id(hapd->p2p_group, p2p)) {
456                         wpa_printf(MSG_MSGDUMP, "P2P: Ignore Probe Request "
457                                    "due to mismatch with Device ID");
458                         wpabuf_free(p2p);
459                         return;
460                 }
461                 wpabuf_free(p2p);
462         }
463 #endif /* CONFIG_P2P */
464
465         if (hapd->conf->ignore_broadcast_ssid && elems.ssid_len == 0 &&
466             elems.ssid_list_len == 0) {
467                 wpa_printf(MSG_MSGDUMP, "Probe Request from " MACSTR " for "
468                            "broadcast SSID ignored", MAC2STR(mgmt->sa));
469                 return;
470         }
471
472         sta = ap_get_sta(hapd, mgmt->sa);
473
474 #ifdef CONFIG_P2P
475         if ((hapd->conf->p2p & P2P_GROUP_OWNER) &&
476             elems.ssid_len == P2P_WILDCARD_SSID_LEN &&
477             os_memcmp(elems.ssid, P2P_WILDCARD_SSID,
478                       P2P_WILDCARD_SSID_LEN) == 0) {
479                 /* Process P2P Wildcard SSID like Wildcard SSID */
480                 elems.ssid_len = 0;
481         }
482 #endif /* CONFIG_P2P */
483
484         res = ssid_match(hapd, elems.ssid, elems.ssid_len,
485                          elems.ssid_list, elems.ssid_list_len);
486         if (res != NO_SSID_MATCH) {
487                 if (sta)
488                         sta->ssid_probe = &hapd->conf->ssid;
489         } else {
490                 if (!(mgmt->da[0] & 0x01)) {
491                         char ssid_txt[33];
492                         ieee802_11_print_ssid(ssid_txt, elems.ssid,
493                                               elems.ssid_len);
494                         wpa_printf(MSG_MSGDUMP, "Probe Request from " MACSTR
495                                    " for foreign SSID '%s' (DA " MACSTR ")%s",
496                                    MAC2STR(mgmt->sa), ssid_txt,
497                                    MAC2STR(mgmt->da),
498                                    elems.ssid_list ? " (SSID list)" : "");
499                 }
500                 return;
501         }
502
503 #ifdef CONFIG_INTERWORKING
504         if (elems.interworking && elems.interworking_len >= 1) {
505                 u8 ant = elems.interworking[0] & 0x0f;
506                 if (ant != INTERWORKING_ANT_WILDCARD &&
507                     ant != hapd->conf->access_network_type) {
508                         wpa_printf(MSG_MSGDUMP, "Probe Request from " MACSTR
509                                    " for mismatching ANT %u ignored",
510                                    MAC2STR(mgmt->sa), ant);
511                         return;
512                 }
513         }
514
515         if (elems.interworking &&
516             (elems.interworking_len == 7 || elems.interworking_len == 9)) {
517                 const u8 *hessid;
518                 if (elems.interworking_len == 7)
519                         hessid = elems.interworking + 1;
520                 else
521                         hessid = elems.interworking + 1 + 2;
522                 if (!is_broadcast_ether_addr(hessid) &&
523                     os_memcmp(hessid, hapd->conf->hessid, ETH_ALEN) != 0) {
524                         wpa_printf(MSG_MSGDUMP, "Probe Request from " MACSTR
525                                    " for mismatching HESSID " MACSTR
526                                    " ignored",
527                                    MAC2STR(mgmt->sa), MAC2STR(hessid));
528                         return;
529                 }
530         }
531 #endif /* CONFIG_INTERWORKING */
532
533 #ifdef CONFIG_P2P
534         if ((hapd->conf->p2p & P2P_GROUP_OWNER) &&
535             supp_rates_11b_only(&elems)) {
536                 /* Indicates support for 11b rates only */
537                 wpa_printf(MSG_EXCESSIVE, "P2P: Ignore Probe Request from "
538                            MACSTR " with only 802.11b rates",
539                            MAC2STR(mgmt->sa));
540                 return;
541         }
542 #endif /* CONFIG_P2P */
543
544         /* TODO: verify that supp_rates contains at least one matching rate
545          * with AP configuration */
546
547 #ifdef CONFIG_TESTING_OPTIONS
548         if (hapd->iconf->ignore_probe_probability > 0.0d &&
549             drand48() < hapd->iconf->ignore_probe_probability) {
550                 wpa_printf(MSG_INFO,
551                            "TESTING: ignoring probe request from " MACSTR,
552                            MAC2STR(mgmt->sa));
553                 return;
554         }
555 #endif /* CONFIG_TESTING_OPTIONS */
556
557         resp = hostapd_gen_probe_resp(hapd, sta, mgmt, elems.p2p != NULL,
558                                       &resp_len);
559         if (resp == NULL)
560                 return;
561
562         /*
563          * If this is a broadcast probe request, apply no ack policy to avoid
564          * excessive retries.
565          */
566         noack = !!(res == WILDCARD_SSID_MATCH &&
567                    is_broadcast_ether_addr(mgmt->da));
568
569         if (hostapd_drv_send_mlme(hapd, resp, resp_len, noack) < 0)
570                 wpa_printf(MSG_INFO, "handle_probe_req: send failed");
571
572         os_free(resp);
573
574         wpa_printf(MSG_EXCESSIVE, "STA " MACSTR " sent probe request for %s "
575                    "SSID", MAC2STR(mgmt->sa),
576                    elems.ssid_len == 0 ? "broadcast" : "our");
577 }
578
579
580 static u8 * hostapd_probe_resp_offloads(struct hostapd_data *hapd,
581                                         size_t *resp_len)
582 {
583         /* check probe response offloading caps and print warnings */
584         if (!(hapd->iface->drv_flags & WPA_DRIVER_FLAGS_PROBE_RESP_OFFLOAD))
585                 return NULL;
586
587 #ifdef CONFIG_WPS
588         if (hapd->conf->wps_state && hapd->wps_probe_resp_ie &&
589             (!(hapd->iface->probe_resp_offloads &
590                (WPA_DRIVER_PROBE_RESP_OFFLOAD_WPS |
591                 WPA_DRIVER_PROBE_RESP_OFFLOAD_WPS2))))
592                 wpa_printf(MSG_WARNING, "Device is trying to offload WPS "
593                            "Probe Response while not supporting this");
594 #endif /* CONFIG_WPS */
595
596 #ifdef CONFIG_P2P
597         if ((hapd->conf->p2p & P2P_ENABLED) && hapd->p2p_probe_resp_ie &&
598             !(hapd->iface->probe_resp_offloads &
599               WPA_DRIVER_PROBE_RESP_OFFLOAD_P2P))
600                 wpa_printf(MSG_WARNING, "Device is trying to offload P2P "
601                            "Probe Response while not supporting this");
602 #endif  /* CONFIG_P2P */
603
604         if (hapd->conf->interworking &&
605             !(hapd->iface->probe_resp_offloads &
606               WPA_DRIVER_PROBE_RESP_OFFLOAD_INTERWORKING))
607                 wpa_printf(MSG_WARNING, "Device is trying to offload "
608                            "Interworking Probe Response while not supporting "
609                            "this");
610
611         /* Generate a Probe Response template for the non-P2P case */
612         return hostapd_gen_probe_resp(hapd, NULL, NULL, 0, resp_len);
613 }
614
615 #endif /* NEED_AP_MLME */
616
617
618 int ieee802_11_build_ap_params(struct hostapd_data *hapd,
619                                struct wpa_driver_ap_params *params)
620 {
621         struct ieee80211_mgmt *head = NULL;
622         u8 *tail = NULL;
623         size_t head_len = 0, tail_len = 0;
624         u8 *resp = NULL;
625         size_t resp_len = 0;
626 #ifdef NEED_AP_MLME
627         u16 capab_info;
628         u8 *pos, *tailpos, *old_pos;
629
630 #define BEACON_HEAD_BUF_SIZE 256
631 #define BEACON_TAIL_BUF_SIZE 512
632         head = os_zalloc(BEACON_HEAD_BUF_SIZE);
633         tail_len = BEACON_TAIL_BUF_SIZE;
634 #ifdef CONFIG_WPS
635         if (hapd->conf->wps_state && hapd->wps_beacon_ie)
636                 tail_len += wpabuf_len(hapd->wps_beacon_ie);
637 #endif /* CONFIG_WPS */
638 #ifdef CONFIG_P2P
639         if (hapd->p2p_beacon_ie)
640                 tail_len += wpabuf_len(hapd->p2p_beacon_ie);
641 #endif /* CONFIG_P2P */
642         if (hapd->conf->vendor_elements)
643                 tail_len += wpabuf_len(hapd->conf->vendor_elements);
644         tailpos = tail = os_malloc(tail_len);
645         if (head == NULL || tail == NULL) {
646                 wpa_printf(MSG_ERROR, "Failed to set beacon data");
647                 os_free(head);
648                 os_free(tail);
649                 return -1;
650         }
651
652         head->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
653                                            WLAN_FC_STYPE_BEACON);
654         head->duration = host_to_le16(0);
655         os_memset(head->da, 0xff, ETH_ALEN);
656
657         os_memcpy(head->sa, hapd->own_addr, ETH_ALEN);
658         os_memcpy(head->bssid, hapd->own_addr, ETH_ALEN);
659         head->u.beacon.beacon_int =
660                 host_to_le16(hapd->iconf->beacon_int);
661
662         /* hardware or low-level driver will setup seq_ctrl and timestamp */
663         capab_info = hostapd_own_capab_info(hapd, NULL, 0);
664         head->u.beacon.capab_info = host_to_le16(capab_info);
665         pos = &head->u.beacon.variable[0];
666
667         /* SSID */
668         *pos++ = WLAN_EID_SSID;
669         if (hapd->conf->ignore_broadcast_ssid == 2) {
670                 /* clear the data, but keep the correct length of the SSID */
671                 *pos++ = hapd->conf->ssid.ssid_len;
672                 os_memset(pos, 0, hapd->conf->ssid.ssid_len);
673                 pos += hapd->conf->ssid.ssid_len;
674         } else if (hapd->conf->ignore_broadcast_ssid) {
675                 *pos++ = 0; /* empty SSID */
676         } else {
677                 *pos++ = hapd->conf->ssid.ssid_len;
678                 os_memcpy(pos, hapd->conf->ssid.ssid,
679                           hapd->conf->ssid.ssid_len);
680                 pos += hapd->conf->ssid.ssid_len;
681         }
682
683         /* Supported rates */
684         pos = hostapd_eid_supp_rates(hapd, pos);
685
686         /* DS Params */
687         pos = hostapd_eid_ds_params(hapd, pos);
688
689         head_len = pos - (u8 *) head;
690
691         tailpos = hostapd_eid_country(hapd, tailpos,
692                                       tail + BEACON_TAIL_BUF_SIZE - tailpos);
693
694         /* ERP Information element */
695         tailpos = hostapd_eid_erp_info(hapd, tailpos);
696
697         /* Extended supported rates */
698         tailpos = hostapd_eid_ext_supp_rates(hapd, tailpos);
699
700         /* RSN, MDIE, WPA */
701         tailpos = hostapd_eid_wpa(hapd, tailpos, tail + BEACON_TAIL_BUF_SIZE -
702                                   tailpos);
703
704         tailpos = hostapd_eid_bss_load(hapd, tailpos,
705                                        tail + BEACON_TAIL_BUF_SIZE - tailpos);
706
707 #ifdef CONFIG_IEEE80211N
708         tailpos = hostapd_eid_ht_capabilities(hapd, tailpos);
709         tailpos = hostapd_eid_ht_operation(hapd, tailpos);
710 #endif /* CONFIG_IEEE80211N */
711
712         tailpos = hostapd_eid_ext_capab(hapd, tailpos);
713
714         /*
715          * TODO: Time Advertisement element should only be included in some
716          * DTIM Beacon frames.
717          */
718         tailpos = hostapd_eid_time_adv(hapd, tailpos);
719
720         tailpos = hostapd_eid_interworking(hapd, tailpos);
721         tailpos = hostapd_eid_adv_proto(hapd, tailpos);
722         tailpos = hostapd_eid_roaming_consortium(hapd, tailpos);
723         old_pos = tailpos;
724         tailpos = hostapd_eid_csa(hapd, tailpos);
725         hapd->iface->cs_c_off_beacon = (old_pos != tailpos) ?
726                 tailpos - tail - 1 : 0;
727
728 #ifdef CONFIG_IEEE80211AC
729         tailpos = hostapd_eid_vht_capabilities(hapd, tailpos);
730         tailpos = hostapd_eid_vht_operation(hapd, tailpos);
731 #endif /* CONFIG_IEEE80211AC */
732
733         /* Wi-Fi Alliance WMM */
734         tailpos = hostapd_eid_wmm(hapd, tailpos);
735
736 #ifdef CONFIG_WPS
737         if (hapd->conf->wps_state && hapd->wps_beacon_ie) {
738                 os_memcpy(tailpos, wpabuf_head(hapd->wps_beacon_ie),
739                           wpabuf_len(hapd->wps_beacon_ie));
740                 tailpos += wpabuf_len(hapd->wps_beacon_ie);
741         }
742 #endif /* CONFIG_WPS */
743
744 #ifdef CONFIG_P2P
745         if ((hapd->conf->p2p & P2P_ENABLED) && hapd->p2p_beacon_ie) {
746                 os_memcpy(tailpos, wpabuf_head(hapd->p2p_beacon_ie),
747                           wpabuf_len(hapd->p2p_beacon_ie));
748                 tailpos += wpabuf_len(hapd->p2p_beacon_ie);
749         }
750 #endif /* CONFIG_P2P */
751 #ifdef CONFIG_P2P_MANAGER
752         if ((hapd->conf->p2p & (P2P_MANAGE | P2P_ENABLED | P2P_GROUP_OWNER)) ==
753             P2P_MANAGE)
754                 tailpos = hostapd_eid_p2p_manage(hapd, tailpos);
755 #endif /* CONFIG_P2P_MANAGER */
756
757 #ifdef CONFIG_HS20
758         tailpos = hostapd_eid_hs20_indication(hapd, tailpos);
759 #endif /* CONFIG_HS20 */
760
761         if (hapd->conf->vendor_elements) {
762                 os_memcpy(tailpos, wpabuf_head(hapd->conf->vendor_elements),
763                           wpabuf_len(hapd->conf->vendor_elements));
764                 tailpos += wpabuf_len(hapd->conf->vendor_elements);
765         }
766
767         tail_len = tailpos > tail ? tailpos - tail : 0;
768
769         resp = hostapd_probe_resp_offloads(hapd, &resp_len);
770 #endif /* NEED_AP_MLME */
771
772         os_memset(params, 0, sizeof(*params));
773         params->head = (u8 *) head;
774         params->head_len = head_len;
775         params->tail = tail;
776         params->tail_len = tail_len;
777         params->proberesp = resp;
778         params->proberesp_len = resp_len;
779         params->dtim_period = hapd->conf->dtim_period;
780         params->beacon_int = hapd->iconf->beacon_int;
781         params->basic_rates = hapd->iface->basic_rates;
782         params->ssid = hapd->conf->ssid.ssid;
783         params->ssid_len = hapd->conf->ssid.ssid_len;
784         params->pairwise_ciphers = hapd->conf->rsn_pairwise ?
785                 hapd->conf->rsn_pairwise : hapd->conf->wpa_pairwise;
786         params->group_cipher = hapd->conf->wpa_group;
787         params->key_mgmt_suites = hapd->conf->wpa_key_mgmt;
788         params->auth_algs = hapd->conf->auth_algs;
789         params->wpa_version = hapd->conf->wpa;
790         params->privacy = hapd->conf->ssid.wep.keys_set || hapd->conf->wpa ||
791                 (hapd->conf->ieee802_1x &&
792                  (hapd->conf->default_wep_key_len ||
793                   hapd->conf->individual_wep_key_len));
794         switch (hapd->conf->ignore_broadcast_ssid) {
795         case 0:
796                 params->hide_ssid = NO_SSID_HIDING;
797                 break;
798         case 1:
799                 params->hide_ssid = HIDDEN_SSID_ZERO_LEN;
800                 break;
801         case 2:
802                 params->hide_ssid = HIDDEN_SSID_ZERO_CONTENTS;
803                 break;
804         }
805         params->isolate = hapd->conf->isolate;
806 #ifdef NEED_AP_MLME
807         params->cts_protect = !!(ieee802_11_erp_info(hapd) &
808                                 ERP_INFO_USE_PROTECTION);
809         params->preamble = hapd->iface->num_sta_no_short_preamble == 0 &&
810                 hapd->iconf->preamble == SHORT_PREAMBLE;
811         if (hapd->iface->current_mode &&
812             hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G)
813                 params->short_slot_time =
814                         hapd->iface->num_sta_no_short_slot_time > 0 ? 0 : 1;
815         else
816                 params->short_slot_time = -1;
817         if (!hapd->iconf->ieee80211n || hapd->conf->disable_11n)
818                 params->ht_opmode = -1;
819         else
820                 params->ht_opmode = hapd->iface->ht_op_mode;
821 #endif /* NEED_AP_MLME */
822         params->interworking = hapd->conf->interworking;
823         if (hapd->conf->interworking &&
824             !is_zero_ether_addr(hapd->conf->hessid))
825                 params->hessid = hapd->conf->hessid;
826         params->access_network_type = hapd->conf->access_network_type;
827         params->ap_max_inactivity = hapd->conf->ap_max_inactivity;
828 #ifdef CONFIG_HS20
829         params->disable_dgaf = hapd->conf->disable_dgaf;
830 #endif /* CONFIG_HS20 */
831         return 0;
832 }
833
834
835 void ieee802_11_free_ap_params(struct wpa_driver_ap_params *params)
836 {
837         os_free(params->tail);
838         params->tail = NULL;
839         os_free(params->head);
840         params->head = NULL;
841         os_free(params->proberesp);
842         params->proberesp = NULL;
843 }
844
845
846 void ieee802_11_set_beacon(struct hostapd_data *hapd)
847 {
848         struct wpa_driver_ap_params params;
849         struct wpabuf *beacon, *proberesp, *assocresp;
850
851         if (hapd->iface->csa_in_progress) {
852                 wpa_printf(MSG_ERROR, "Cannot set beacons during CSA period");
853                 return;
854         }
855
856         hapd->beacon_set_done = 1;
857
858         if (ieee802_11_build_ap_params(hapd, &params) < 0)
859                 return;
860
861         if (hostapd_build_ap_extra_ies(hapd, &beacon, &proberesp, &assocresp) <
862             0)
863                 goto fail;
864
865         params.beacon_ies = beacon;
866         params.proberesp_ies = proberesp;
867         params.assocresp_ies = assocresp;
868
869         if (hostapd_drv_set_ap(hapd, &params))
870                 wpa_printf(MSG_ERROR, "Failed to set beacon parameters");
871         hostapd_free_ap_extra_ies(hapd, beacon, proberesp, assocresp);
872 fail:
873         ieee802_11_free_ap_params(&params);
874 }
875
876
877 void ieee802_11_set_beacons(struct hostapd_iface *iface)
878 {
879         size_t i;
880         for (i = 0; i < iface->num_bss; i++) {
881                 if (iface->bss[i]->started)
882                         ieee802_11_set_beacon(iface->bss[i]);
883         }
884 }
885
886
887 /* only update beacons if started */
888 void ieee802_11_update_beacons(struct hostapd_iface *iface)
889 {
890         size_t i;
891         for (i = 0; i < iface->num_bss; i++)
892                 if (iface->bss[i]->beacon_set_done && iface->bss[i]->started)
893                         ieee802_11_set_beacon(iface->bss[i]);
894 }
895
896 #endif /* CONFIG_NATIVE_WINDOWS */