Verify that beacon setup succeeds before proceeding
[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_params.freq)
210                 return eid;
211
212         if (ieee80211_freq_to_chan(hapd->iface->cs_freq_params.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_eid_secondary_channel(struct hostapd_data *hapd, u8 *eid)
227 {
228         u8 sec_ch;
229
230         if (!hapd->iface->cs_freq_params.sec_channel_offset)
231                 return eid;
232
233         if (hapd->iface->cs_freq_params.sec_channel_offset == -1)
234                 sec_ch = HT_INFO_HT_PARAM_SECONDARY_CHNL_BELOW;
235         else if (hapd->iface->cs_freq_params.sec_channel_offset == 1)
236                 sec_ch = HT_INFO_HT_PARAM_SECONDARY_CHNL_ABOVE;
237         else
238                 return eid;
239
240         *eid++ = WLAN_EID_SECONDARY_CHANNEL_OFFSET;
241         *eid++ = 1;
242         *eid++ = sec_ch;
243
244         return eid;
245 }
246
247
248 static u8 * hostapd_add_csa_elems(struct hostapd_data *hapd, u8 *pos,
249                                   u8 *start, unsigned int *csa_counter_off)
250 {
251         u8 *old_pos = pos;
252
253         if (!csa_counter_off)
254                 return pos;
255
256         *csa_counter_off = 0;
257         pos = hostapd_eid_csa(hapd, pos);
258
259         if (pos != old_pos) {
260                 /* save an offset to the counter - should be last byte */
261                 *csa_counter_off = pos - start - 1;
262                 pos = hostapd_eid_secondary_channel(hapd, pos);
263         }
264
265         return pos;
266 }
267
268
269 static u8 * hostapd_gen_probe_resp(struct hostapd_data *hapd,
270                                    struct sta_info *sta,
271                                    const struct ieee80211_mgmt *req,
272                                    int is_p2p, size_t *resp_len)
273 {
274         struct ieee80211_mgmt *resp;
275         u8 *pos, *epos;
276         size_t buflen;
277
278 #define MAX_PROBERESP_LEN 768
279         buflen = MAX_PROBERESP_LEN;
280 #ifdef CONFIG_WPS
281         if (hapd->wps_probe_resp_ie)
282                 buflen += wpabuf_len(hapd->wps_probe_resp_ie);
283 #endif /* CONFIG_WPS */
284 #ifdef CONFIG_P2P
285         if (hapd->p2p_probe_resp_ie)
286                 buflen += wpabuf_len(hapd->p2p_probe_resp_ie);
287 #endif /* CONFIG_P2P */
288         if (hapd->conf->vendor_elements)
289                 buflen += wpabuf_len(hapd->conf->vendor_elements);
290         resp = os_zalloc(buflen);
291         if (resp == NULL)
292                 return NULL;
293
294         epos = ((u8 *) resp) + MAX_PROBERESP_LEN;
295
296         resp->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
297                                            WLAN_FC_STYPE_PROBE_RESP);
298         if (req)
299                 os_memcpy(resp->da, req->sa, ETH_ALEN);
300         os_memcpy(resp->sa, hapd->own_addr, ETH_ALEN);
301
302         os_memcpy(resp->bssid, hapd->own_addr, ETH_ALEN);
303         resp->u.probe_resp.beacon_int =
304                 host_to_le16(hapd->iconf->beacon_int);
305
306         /* hardware or low-level driver will setup seq_ctrl and timestamp */
307         resp->u.probe_resp.capab_info =
308                 host_to_le16(hostapd_own_capab_info(hapd, sta, 1));
309
310         pos = resp->u.probe_resp.variable;
311         *pos++ = WLAN_EID_SSID;
312         *pos++ = hapd->conf->ssid.ssid_len;
313         os_memcpy(pos, hapd->conf->ssid.ssid, hapd->conf->ssid.ssid_len);
314         pos += hapd->conf->ssid.ssid_len;
315
316         /* Supported rates */
317         pos = hostapd_eid_supp_rates(hapd, pos);
318
319         /* DS Params */
320         pos = hostapd_eid_ds_params(hapd, pos);
321
322         pos = hostapd_eid_country(hapd, pos, epos - pos);
323
324         /* ERP Information element */
325         pos = hostapd_eid_erp_info(hapd, pos);
326
327         /* Extended supported rates */
328         pos = hostapd_eid_ext_supp_rates(hapd, pos);
329
330         /* RSN, MDIE, WPA */
331         pos = hostapd_eid_wpa(hapd, pos, epos - pos);
332
333         pos = hostapd_eid_bss_load(hapd, pos, epos - pos);
334
335 #ifdef CONFIG_IEEE80211N
336         pos = hostapd_eid_ht_capabilities(hapd, pos);
337         pos = hostapd_eid_ht_operation(hapd, pos);
338 #endif /* CONFIG_IEEE80211N */
339
340         pos = hostapd_eid_ext_capab(hapd, pos);
341
342         pos = hostapd_eid_time_adv(hapd, pos);
343         pos = hostapd_eid_time_zone(hapd, pos);
344
345         pos = hostapd_eid_interworking(hapd, pos);
346         pos = hostapd_eid_adv_proto(hapd, pos);
347         pos = hostapd_eid_roaming_consortium(hapd, pos);
348
349         pos = hostapd_add_csa_elems(hapd, pos, (u8 *)resp,
350                                     &hapd->iface->cs_c_off_proberesp);
351 #ifdef CONFIG_IEEE80211AC
352         pos = hostapd_eid_vht_capabilities(hapd, pos);
353         pos = hostapd_eid_vht_operation(hapd, pos);
354 #endif /* CONFIG_IEEE80211AC */
355
356         /* Wi-Fi Alliance WMM */
357         pos = hostapd_eid_wmm(hapd, pos);
358
359 #ifdef CONFIG_WPS
360         if (hapd->conf->wps_state && hapd->wps_probe_resp_ie) {
361                 os_memcpy(pos, wpabuf_head(hapd->wps_probe_resp_ie),
362                           wpabuf_len(hapd->wps_probe_resp_ie));
363                 pos += wpabuf_len(hapd->wps_probe_resp_ie);
364         }
365 #endif /* CONFIG_WPS */
366
367 #ifdef CONFIG_P2P
368         if ((hapd->conf->p2p & P2P_ENABLED) && is_p2p &&
369             hapd->p2p_probe_resp_ie) {
370                 os_memcpy(pos, wpabuf_head(hapd->p2p_probe_resp_ie),
371                           wpabuf_len(hapd->p2p_probe_resp_ie));
372                 pos += wpabuf_len(hapd->p2p_probe_resp_ie);
373         }
374 #endif /* CONFIG_P2P */
375 #ifdef CONFIG_P2P_MANAGER
376         if ((hapd->conf->p2p & (P2P_MANAGE | P2P_ENABLED | P2P_GROUP_OWNER)) ==
377             P2P_MANAGE)
378                 pos = hostapd_eid_p2p_manage(hapd, pos);
379 #endif /* CONFIG_P2P_MANAGER */
380
381 #ifdef CONFIG_HS20
382         pos = hostapd_eid_hs20_indication(hapd, pos);
383 #endif /* CONFIG_HS20 */
384
385         if (hapd->conf->vendor_elements) {
386                 os_memcpy(pos, wpabuf_head(hapd->conf->vendor_elements),
387                           wpabuf_len(hapd->conf->vendor_elements));
388                 pos += wpabuf_len(hapd->conf->vendor_elements);
389         }
390
391         *resp_len = pos - (u8 *) resp;
392         return (u8 *) resp;
393 }
394
395
396 enum ssid_match_result {
397         NO_SSID_MATCH,
398         EXACT_SSID_MATCH,
399         WILDCARD_SSID_MATCH
400 };
401
402 static enum ssid_match_result ssid_match(struct hostapd_data *hapd,
403                                          const u8 *ssid, size_t ssid_len,
404                                          const u8 *ssid_list,
405                                          size_t ssid_list_len)
406 {
407         const u8 *pos, *end;
408         int wildcard = 0;
409
410         if (ssid_len == 0)
411                 wildcard = 1;
412         if (ssid_len == hapd->conf->ssid.ssid_len &&
413             os_memcmp(ssid, hapd->conf->ssid.ssid, ssid_len) == 0)
414                 return EXACT_SSID_MATCH;
415
416         if (ssid_list == NULL)
417                 return wildcard ? WILDCARD_SSID_MATCH : NO_SSID_MATCH;
418
419         pos = ssid_list;
420         end = ssid_list + ssid_list_len;
421         while (pos + 1 <= end) {
422                 if (pos + 2 + pos[1] > end)
423                         break;
424                 if (pos[1] == 0)
425                         wildcard = 1;
426                 if (pos[1] == hapd->conf->ssid.ssid_len &&
427                     os_memcmp(pos + 2, hapd->conf->ssid.ssid, pos[1]) == 0)
428                         return EXACT_SSID_MATCH;
429                 pos += 2 + pos[1];
430         }
431
432         return wildcard ? WILDCARD_SSID_MATCH : NO_SSID_MATCH;
433 }
434
435
436 void handle_probe_req(struct hostapd_data *hapd,
437                       const struct ieee80211_mgmt *mgmt, size_t len,
438                       int ssi_signal)
439 {
440         u8 *resp;
441         struct ieee802_11_elems elems;
442         const u8 *ie;
443         size_t ie_len;
444         struct sta_info *sta = NULL;
445         size_t i, resp_len;
446         int noack;
447         enum ssid_match_result res;
448
449         ie = mgmt->u.probe_req.variable;
450         if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.probe_req))
451                 return;
452         ie_len = len - (IEEE80211_HDRLEN + sizeof(mgmt->u.probe_req));
453
454         for (i = 0; hapd->probereq_cb && i < hapd->num_probereq_cb; i++)
455                 if (hapd->probereq_cb[i].cb(hapd->probereq_cb[i].ctx,
456                                             mgmt->sa, mgmt->da, mgmt->bssid,
457                                             ie, ie_len, ssi_signal) > 0)
458                         return;
459
460         if (!hapd->iconf->send_probe_response)
461                 return;
462
463         if (ieee802_11_parse_elems(ie, ie_len, &elems, 0) == ParseFailed) {
464                 wpa_printf(MSG_DEBUG, "Could not parse ProbeReq from " MACSTR,
465                            MAC2STR(mgmt->sa));
466                 return;
467         }
468
469         if ((!elems.ssid || !elems.supp_rates)) {
470                 wpa_printf(MSG_DEBUG, "STA " MACSTR " sent probe request "
471                            "without SSID or supported rates element",
472                            MAC2STR(mgmt->sa));
473                 return;
474         }
475
476 #ifdef CONFIG_P2P
477         if (hapd->p2p && elems.wps_ie) {
478                 struct wpabuf *wps;
479                 wps = ieee802_11_vendor_ie_concat(ie, ie_len, WPS_DEV_OUI_WFA);
480                 if (wps && !p2p_group_match_dev_type(hapd->p2p_group, wps)) {
481                         wpa_printf(MSG_MSGDUMP, "P2P: Ignore Probe Request "
482                                    "due to mismatch with Requested Device "
483                                    "Type");
484                         wpabuf_free(wps);
485                         return;
486                 }
487                 wpabuf_free(wps);
488         }
489
490         if (hapd->p2p && elems.p2p) {
491                 struct wpabuf *p2p;
492                 p2p = ieee802_11_vendor_ie_concat(ie, ie_len, P2P_IE_VENDOR_TYPE);
493                 if (p2p && !p2p_group_match_dev_id(hapd->p2p_group, p2p)) {
494                         wpa_printf(MSG_MSGDUMP, "P2P: Ignore Probe Request "
495                                    "due to mismatch with Device ID");
496                         wpabuf_free(p2p);
497                         return;
498                 }
499                 wpabuf_free(p2p);
500         }
501 #endif /* CONFIG_P2P */
502
503         if (hapd->conf->ignore_broadcast_ssid && elems.ssid_len == 0 &&
504             elems.ssid_list_len == 0) {
505                 wpa_printf(MSG_MSGDUMP, "Probe Request from " MACSTR " for "
506                            "broadcast SSID ignored", MAC2STR(mgmt->sa));
507                 return;
508         }
509
510         sta = ap_get_sta(hapd, mgmt->sa);
511
512 #ifdef CONFIG_P2P
513         if ((hapd->conf->p2p & P2P_GROUP_OWNER) &&
514             elems.ssid_len == P2P_WILDCARD_SSID_LEN &&
515             os_memcmp(elems.ssid, P2P_WILDCARD_SSID,
516                       P2P_WILDCARD_SSID_LEN) == 0) {
517                 /* Process P2P Wildcard SSID like Wildcard SSID */
518                 elems.ssid_len = 0;
519         }
520 #endif /* CONFIG_P2P */
521
522         res = ssid_match(hapd, elems.ssid, elems.ssid_len,
523                          elems.ssid_list, elems.ssid_list_len);
524         if (res != NO_SSID_MATCH) {
525                 if (sta)
526                         sta->ssid_probe = &hapd->conf->ssid;
527         } else {
528                 if (!(mgmt->da[0] & 0x01)) {
529                         char ssid_txt[33];
530                         ieee802_11_print_ssid(ssid_txt, elems.ssid,
531                                               elems.ssid_len);
532                         wpa_printf(MSG_MSGDUMP, "Probe Request from " MACSTR
533                                    " for foreign SSID '%s' (DA " MACSTR ")%s",
534                                    MAC2STR(mgmt->sa), ssid_txt,
535                                    MAC2STR(mgmt->da),
536                                    elems.ssid_list ? " (SSID list)" : "");
537                 }
538                 return;
539         }
540
541 #ifdef CONFIG_INTERWORKING
542         if (elems.interworking && elems.interworking_len >= 1) {
543                 u8 ant = elems.interworking[0] & 0x0f;
544                 if (ant != INTERWORKING_ANT_WILDCARD &&
545                     ant != hapd->conf->access_network_type) {
546                         wpa_printf(MSG_MSGDUMP, "Probe Request from " MACSTR
547                                    " for mismatching ANT %u ignored",
548                                    MAC2STR(mgmt->sa), ant);
549                         return;
550                 }
551         }
552
553         if (elems.interworking &&
554             (elems.interworking_len == 7 || elems.interworking_len == 9)) {
555                 const u8 *hessid;
556                 if (elems.interworking_len == 7)
557                         hessid = elems.interworking + 1;
558                 else
559                         hessid = elems.interworking + 1 + 2;
560                 if (!is_broadcast_ether_addr(hessid) &&
561                     os_memcmp(hessid, hapd->conf->hessid, ETH_ALEN) != 0) {
562                         wpa_printf(MSG_MSGDUMP, "Probe Request from " MACSTR
563                                    " for mismatching HESSID " MACSTR
564                                    " ignored",
565                                    MAC2STR(mgmt->sa), MAC2STR(hessid));
566                         return;
567                 }
568         }
569 #endif /* CONFIG_INTERWORKING */
570
571 #ifdef CONFIG_P2P
572         if ((hapd->conf->p2p & P2P_GROUP_OWNER) &&
573             supp_rates_11b_only(&elems)) {
574                 /* Indicates support for 11b rates only */
575                 wpa_printf(MSG_EXCESSIVE, "P2P: Ignore Probe Request from "
576                            MACSTR " with only 802.11b rates",
577                            MAC2STR(mgmt->sa));
578                 return;
579         }
580 #endif /* CONFIG_P2P */
581
582         /* TODO: verify that supp_rates contains at least one matching rate
583          * with AP configuration */
584
585 #ifdef CONFIG_TESTING_OPTIONS
586         if (hapd->iconf->ignore_probe_probability > 0.0d &&
587             drand48() < hapd->iconf->ignore_probe_probability) {
588                 wpa_printf(MSG_INFO,
589                            "TESTING: ignoring probe request from " MACSTR,
590                            MAC2STR(mgmt->sa));
591                 return;
592         }
593 #endif /* CONFIG_TESTING_OPTIONS */
594
595         resp = hostapd_gen_probe_resp(hapd, sta, mgmt, elems.p2p != NULL,
596                                       &resp_len);
597         if (resp == NULL)
598                 return;
599
600         /*
601          * If this is a broadcast probe request, apply no ack policy to avoid
602          * excessive retries.
603          */
604         noack = !!(res == WILDCARD_SSID_MATCH &&
605                    is_broadcast_ether_addr(mgmt->da));
606
607         if (hostapd_drv_send_mlme(hapd, resp, resp_len, noack) < 0)
608                 wpa_printf(MSG_INFO, "handle_probe_req: send failed");
609
610         os_free(resp);
611
612         wpa_printf(MSG_EXCESSIVE, "STA " MACSTR " sent probe request for %s "
613                    "SSID", MAC2STR(mgmt->sa),
614                    elems.ssid_len == 0 ? "broadcast" : "our");
615 }
616
617
618 static u8 * hostapd_probe_resp_offloads(struct hostapd_data *hapd,
619                                         size_t *resp_len)
620 {
621         /* check probe response offloading caps and print warnings */
622         if (!(hapd->iface->drv_flags & WPA_DRIVER_FLAGS_PROBE_RESP_OFFLOAD))
623                 return NULL;
624
625 #ifdef CONFIG_WPS
626         if (hapd->conf->wps_state && hapd->wps_probe_resp_ie &&
627             (!(hapd->iface->probe_resp_offloads &
628                (WPA_DRIVER_PROBE_RESP_OFFLOAD_WPS |
629                 WPA_DRIVER_PROBE_RESP_OFFLOAD_WPS2))))
630                 wpa_printf(MSG_WARNING, "Device is trying to offload WPS "
631                            "Probe Response while not supporting this");
632 #endif /* CONFIG_WPS */
633
634 #ifdef CONFIG_P2P
635         if ((hapd->conf->p2p & P2P_ENABLED) && hapd->p2p_probe_resp_ie &&
636             !(hapd->iface->probe_resp_offloads &
637               WPA_DRIVER_PROBE_RESP_OFFLOAD_P2P))
638                 wpa_printf(MSG_WARNING, "Device is trying to offload P2P "
639                            "Probe Response while not supporting this");
640 #endif  /* CONFIG_P2P */
641
642         if (hapd->conf->interworking &&
643             !(hapd->iface->probe_resp_offloads &
644               WPA_DRIVER_PROBE_RESP_OFFLOAD_INTERWORKING))
645                 wpa_printf(MSG_WARNING, "Device is trying to offload "
646                            "Interworking Probe Response while not supporting "
647                            "this");
648
649         /* Generate a Probe Response template for the non-P2P case */
650         return hostapd_gen_probe_resp(hapd, NULL, NULL, 0, resp_len);
651 }
652
653 #endif /* NEED_AP_MLME */
654
655
656 int ieee802_11_build_ap_params(struct hostapd_data *hapd,
657                                struct wpa_driver_ap_params *params)
658 {
659         struct ieee80211_mgmt *head = NULL;
660         u8 *tail = NULL;
661         size_t head_len = 0, tail_len = 0;
662         u8 *resp = NULL;
663         size_t resp_len = 0;
664 #ifdef NEED_AP_MLME
665         u16 capab_info;
666         u8 *pos, *tailpos;
667
668 #define BEACON_HEAD_BUF_SIZE 256
669 #define BEACON_TAIL_BUF_SIZE 512
670         head = os_zalloc(BEACON_HEAD_BUF_SIZE);
671         tail_len = BEACON_TAIL_BUF_SIZE;
672 #ifdef CONFIG_WPS
673         if (hapd->conf->wps_state && hapd->wps_beacon_ie)
674                 tail_len += wpabuf_len(hapd->wps_beacon_ie);
675 #endif /* CONFIG_WPS */
676 #ifdef CONFIG_P2P
677         if (hapd->p2p_beacon_ie)
678                 tail_len += wpabuf_len(hapd->p2p_beacon_ie);
679 #endif /* CONFIG_P2P */
680         if (hapd->conf->vendor_elements)
681                 tail_len += wpabuf_len(hapd->conf->vendor_elements);
682         tailpos = tail = os_malloc(tail_len);
683         if (head == NULL || tail == NULL) {
684                 wpa_printf(MSG_ERROR, "Failed to set beacon data");
685                 os_free(head);
686                 os_free(tail);
687                 return -1;
688         }
689
690         head->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
691                                            WLAN_FC_STYPE_BEACON);
692         head->duration = host_to_le16(0);
693         os_memset(head->da, 0xff, ETH_ALEN);
694
695         os_memcpy(head->sa, hapd->own_addr, ETH_ALEN);
696         os_memcpy(head->bssid, hapd->own_addr, ETH_ALEN);
697         head->u.beacon.beacon_int =
698                 host_to_le16(hapd->iconf->beacon_int);
699
700         /* hardware or low-level driver will setup seq_ctrl and timestamp */
701         capab_info = hostapd_own_capab_info(hapd, NULL, 0);
702         head->u.beacon.capab_info = host_to_le16(capab_info);
703         pos = &head->u.beacon.variable[0];
704
705         /* SSID */
706         *pos++ = WLAN_EID_SSID;
707         if (hapd->conf->ignore_broadcast_ssid == 2) {
708                 /* clear the data, but keep the correct length of the SSID */
709                 *pos++ = hapd->conf->ssid.ssid_len;
710                 os_memset(pos, 0, hapd->conf->ssid.ssid_len);
711                 pos += hapd->conf->ssid.ssid_len;
712         } else if (hapd->conf->ignore_broadcast_ssid) {
713                 *pos++ = 0; /* empty SSID */
714         } else {
715                 *pos++ = hapd->conf->ssid.ssid_len;
716                 os_memcpy(pos, hapd->conf->ssid.ssid,
717                           hapd->conf->ssid.ssid_len);
718                 pos += hapd->conf->ssid.ssid_len;
719         }
720
721         /* Supported rates */
722         pos = hostapd_eid_supp_rates(hapd, pos);
723
724         /* DS Params */
725         pos = hostapd_eid_ds_params(hapd, pos);
726
727         head_len = pos - (u8 *) head;
728
729         tailpos = hostapd_eid_country(hapd, tailpos,
730                                       tail + BEACON_TAIL_BUF_SIZE - tailpos);
731
732         /* ERP Information element */
733         tailpos = hostapd_eid_erp_info(hapd, tailpos);
734
735         /* Extended supported rates */
736         tailpos = hostapd_eid_ext_supp_rates(hapd, tailpos);
737
738         /* RSN, MDIE, WPA */
739         tailpos = hostapd_eid_wpa(hapd, tailpos, tail + BEACON_TAIL_BUF_SIZE -
740                                   tailpos);
741
742         tailpos = hostapd_eid_bss_load(hapd, tailpos,
743                                        tail + BEACON_TAIL_BUF_SIZE - tailpos);
744
745 #ifdef CONFIG_IEEE80211N
746         tailpos = hostapd_eid_ht_capabilities(hapd, tailpos);
747         tailpos = hostapd_eid_ht_operation(hapd, tailpos);
748 #endif /* CONFIG_IEEE80211N */
749
750         tailpos = hostapd_eid_ext_capab(hapd, tailpos);
751
752         /*
753          * TODO: Time Advertisement element should only be included in some
754          * DTIM Beacon frames.
755          */
756         tailpos = hostapd_eid_time_adv(hapd, tailpos);
757
758         tailpos = hostapd_eid_interworking(hapd, tailpos);
759         tailpos = hostapd_eid_adv_proto(hapd, tailpos);
760         tailpos = hostapd_eid_roaming_consortium(hapd, tailpos);
761         tailpos = hostapd_add_csa_elems(hapd, tailpos, tail,
762                                         &hapd->iface->cs_c_off_beacon);
763 #ifdef CONFIG_IEEE80211AC
764         tailpos = hostapd_eid_vht_capabilities(hapd, tailpos);
765         tailpos = hostapd_eid_vht_operation(hapd, tailpos);
766 #endif /* CONFIG_IEEE80211AC */
767
768         /* Wi-Fi Alliance WMM */
769         tailpos = hostapd_eid_wmm(hapd, tailpos);
770
771 #ifdef CONFIG_WPS
772         if (hapd->conf->wps_state && hapd->wps_beacon_ie) {
773                 os_memcpy(tailpos, wpabuf_head(hapd->wps_beacon_ie),
774                           wpabuf_len(hapd->wps_beacon_ie));
775                 tailpos += wpabuf_len(hapd->wps_beacon_ie);
776         }
777 #endif /* CONFIG_WPS */
778
779 #ifdef CONFIG_P2P
780         if ((hapd->conf->p2p & P2P_ENABLED) && hapd->p2p_beacon_ie) {
781                 os_memcpy(tailpos, wpabuf_head(hapd->p2p_beacon_ie),
782                           wpabuf_len(hapd->p2p_beacon_ie));
783                 tailpos += wpabuf_len(hapd->p2p_beacon_ie);
784         }
785 #endif /* CONFIG_P2P */
786 #ifdef CONFIG_P2P_MANAGER
787         if ((hapd->conf->p2p & (P2P_MANAGE | P2P_ENABLED | P2P_GROUP_OWNER)) ==
788             P2P_MANAGE)
789                 tailpos = hostapd_eid_p2p_manage(hapd, tailpos);
790 #endif /* CONFIG_P2P_MANAGER */
791
792 #ifdef CONFIG_HS20
793         tailpos = hostapd_eid_hs20_indication(hapd, tailpos);
794 #endif /* CONFIG_HS20 */
795
796         if (hapd->conf->vendor_elements) {
797                 os_memcpy(tailpos, wpabuf_head(hapd->conf->vendor_elements),
798                           wpabuf_len(hapd->conf->vendor_elements));
799                 tailpos += wpabuf_len(hapd->conf->vendor_elements);
800         }
801
802         tail_len = tailpos > tail ? tailpos - tail : 0;
803
804         resp = hostapd_probe_resp_offloads(hapd, &resp_len);
805 #endif /* NEED_AP_MLME */
806
807         os_memset(params, 0, sizeof(*params));
808         params->head = (u8 *) head;
809         params->head_len = head_len;
810         params->tail = tail;
811         params->tail_len = tail_len;
812         params->proberesp = resp;
813         params->proberesp_len = resp_len;
814         params->dtim_period = hapd->conf->dtim_period;
815         params->beacon_int = hapd->iconf->beacon_int;
816         params->basic_rates = hapd->iface->basic_rates;
817         params->ssid = hapd->conf->ssid.ssid;
818         params->ssid_len = hapd->conf->ssid.ssid_len;
819         params->pairwise_ciphers = hapd->conf->rsn_pairwise ?
820                 hapd->conf->rsn_pairwise : hapd->conf->wpa_pairwise;
821         params->group_cipher = hapd->conf->wpa_group;
822         params->key_mgmt_suites = hapd->conf->wpa_key_mgmt;
823         params->auth_algs = hapd->conf->auth_algs;
824         params->wpa_version = hapd->conf->wpa;
825         params->privacy = hapd->conf->ssid.wep.keys_set || hapd->conf->wpa ||
826                 (hapd->conf->ieee802_1x &&
827                  (hapd->conf->default_wep_key_len ||
828                   hapd->conf->individual_wep_key_len));
829         switch (hapd->conf->ignore_broadcast_ssid) {
830         case 0:
831                 params->hide_ssid = NO_SSID_HIDING;
832                 break;
833         case 1:
834                 params->hide_ssid = HIDDEN_SSID_ZERO_LEN;
835                 break;
836         case 2:
837                 params->hide_ssid = HIDDEN_SSID_ZERO_CONTENTS;
838                 break;
839         }
840         params->isolate = hapd->conf->isolate;
841 #ifdef NEED_AP_MLME
842         params->cts_protect = !!(ieee802_11_erp_info(hapd) &
843                                 ERP_INFO_USE_PROTECTION);
844         params->preamble = hapd->iface->num_sta_no_short_preamble == 0 &&
845                 hapd->iconf->preamble == SHORT_PREAMBLE;
846         if (hapd->iface->current_mode &&
847             hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G)
848                 params->short_slot_time =
849                         hapd->iface->num_sta_no_short_slot_time > 0 ? 0 : 1;
850         else
851                 params->short_slot_time = -1;
852         if (!hapd->iconf->ieee80211n || hapd->conf->disable_11n)
853                 params->ht_opmode = -1;
854         else
855                 params->ht_opmode = hapd->iface->ht_op_mode;
856 #endif /* NEED_AP_MLME */
857         params->interworking = hapd->conf->interworking;
858         if (hapd->conf->interworking &&
859             !is_zero_ether_addr(hapd->conf->hessid))
860                 params->hessid = hapd->conf->hessid;
861         params->access_network_type = hapd->conf->access_network_type;
862         params->ap_max_inactivity = hapd->conf->ap_max_inactivity;
863 #ifdef CONFIG_HS20
864         params->disable_dgaf = hapd->conf->disable_dgaf;
865 #endif /* CONFIG_HS20 */
866         return 0;
867 }
868
869
870 void ieee802_11_free_ap_params(struct wpa_driver_ap_params *params)
871 {
872         os_free(params->tail);
873         params->tail = NULL;
874         os_free(params->head);
875         params->head = NULL;
876         os_free(params->proberesp);
877         params->proberesp = NULL;
878 }
879
880
881 int ieee802_11_set_beacon(struct hostapd_data *hapd)
882 {
883         struct wpa_driver_ap_params params;
884         struct wpabuf *beacon, *proberesp, *assocresp;
885         int res, ret = -1;
886
887         if (hapd->iface->csa_in_progress) {
888                 wpa_printf(MSG_ERROR, "Cannot set beacons during CSA period");
889                 return -1;
890         }
891
892         hapd->beacon_set_done = 1;
893
894         if (ieee802_11_build_ap_params(hapd, &params) < 0)
895                 return -1;
896
897         if (hostapd_build_ap_extra_ies(hapd, &beacon, &proberesp, &assocresp) <
898             0)
899                 goto fail;
900
901         params.beacon_ies = beacon;
902         params.proberesp_ies = proberesp;
903         params.assocresp_ies = assocresp;
904
905         res = hostapd_drv_set_ap(hapd, &params);
906         hostapd_free_ap_extra_ies(hapd, beacon, proberesp, assocresp);
907         if (res)
908                 wpa_printf(MSG_ERROR, "Failed to set beacon parameters");
909         else
910                 ret = 0;
911 fail:
912         ieee802_11_free_ap_params(&params);
913         return ret;
914 }
915
916
917 int ieee802_11_set_beacons(struct hostapd_iface *iface)
918 {
919         size_t i;
920         int ret = 0;
921
922         for (i = 0; i < iface->num_bss; i++) {
923                 if (iface->bss[i]->started &&
924                     ieee802_11_set_beacon(iface->bss[i]) < 0)
925                         ret = -1;
926         }
927
928         return ret;
929 }
930
931
932 /* only update beacons if started */
933 int ieee802_11_update_beacons(struct hostapd_iface *iface)
934 {
935         size_t i;
936         int ret = 0;
937
938         for (i = 0; i < iface->num_bss; i++) {
939                 if (iface->bss[i]->beacon_set_done && iface->bss[i]->started &&
940                     ieee802_11_set_beacon(iface->bss[i]) < 0)
941                         ret = -1;
942         }
943
944         return ret;
945 }
946
947 #endif /* CONFIG_NATIVE_WINDOWS */