Advertise Supported Operating Classes elements in Beacon/Probe Response
[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 software may be distributed under the terms of the BSD license.
8  * See README for more details.
9  */
10
11 #include "utils/includes.h"
12
13 #ifndef CONFIG_NATIVE_WINDOWS
14
15 #include "utils/common.h"
16 #include "common/ieee802_11_defs.h"
17 #include "common/ieee802_11_common.h"
18 #include "common/hw_features_common.h"
19 #include "wps/wps_defs.h"
20 #include "p2p/p2p.h"
21 #include "hostapd.h"
22 #include "ieee802_11.h"
23 #include "wpa_auth.h"
24 #include "wmm.h"
25 #include "ap_config.h"
26 #include "sta_info.h"
27 #include "p2p_hostapd.h"
28 #include "ap_drv_ops.h"
29 #include "beacon.h"
30 #include "hs20.h"
31 #include "dfs.h"
32
33
34 #ifdef NEED_AP_MLME
35
36 static u8 * hostapd_eid_rm_enabled_capab(struct hostapd_data *hapd, u8 *eid,
37                                          size_t len)
38 {
39         if (!hapd->conf->radio_measurements || len < 2 + 4)
40                 return eid;
41
42         *eid++ = WLAN_EID_RRM_ENABLED_CAPABILITIES;
43         *eid++ = 5;
44         *eid++ = (hapd->conf->radio_measurements & BIT(0)) ?
45                 WLAN_RRM_CAPS_NEIGHBOR_REPORT : 0x00;
46         *eid++ = 0x00;
47         *eid++ = 0x00;
48         *eid++ = 0x00;
49         *eid++ = 0x00;
50         return eid;
51 }
52
53
54 static u8 * hostapd_eid_bss_load(struct hostapd_data *hapd, u8 *eid, size_t len)
55 {
56         if (len < 2 + 5)
57                 return eid;
58
59 #ifdef CONFIG_TESTING_OPTIONS
60         if (hapd->conf->bss_load_test_set) {
61                 *eid++ = WLAN_EID_BSS_LOAD;
62                 *eid++ = 5;
63                 os_memcpy(eid, hapd->conf->bss_load_test, 5);
64                 eid += 5;
65                 return eid;
66         }
67 #endif /* CONFIG_TESTING_OPTIONS */
68         if (hapd->conf->bss_load_update_period) {
69                 *eid++ = WLAN_EID_BSS_LOAD;
70                 *eid++ = 5;
71                 WPA_PUT_LE16(eid, hapd->num_sta);
72                 eid += 2;
73                 *eid++ = hapd->iface->channel_utilization;
74                 WPA_PUT_LE16(eid, 0); /* no available admission capabity */
75                 eid += 2;
76         }
77         return eid;
78 }
79
80
81 static u8 ieee802_11_erp_info(struct hostapd_data *hapd)
82 {
83         u8 erp = 0;
84
85         if (hapd->iface->current_mode == NULL ||
86             hapd->iface->current_mode->mode != HOSTAPD_MODE_IEEE80211G)
87                 return 0;
88
89         if (hapd->iface->olbc)
90                 erp |= ERP_INFO_USE_PROTECTION;
91         if (hapd->iface->num_sta_non_erp > 0) {
92                 erp |= ERP_INFO_NON_ERP_PRESENT |
93                         ERP_INFO_USE_PROTECTION;
94         }
95         if (hapd->iface->num_sta_no_short_preamble > 0 ||
96             hapd->iconf->preamble == LONG_PREAMBLE)
97                 erp |= ERP_INFO_BARKER_PREAMBLE_MODE;
98
99         return erp;
100 }
101
102
103 static u8 * hostapd_eid_ds_params(struct hostapd_data *hapd, u8 *eid)
104 {
105         *eid++ = WLAN_EID_DS_PARAMS;
106         *eid++ = 1;
107         *eid++ = hapd->iconf->channel;
108         return eid;
109 }
110
111
112 static u8 * hostapd_eid_erp_info(struct hostapd_data *hapd, u8 *eid)
113 {
114         if (hapd->iface->current_mode == NULL ||
115             hapd->iface->current_mode->mode != HOSTAPD_MODE_IEEE80211G)
116                 return eid;
117
118         /* Set NonERP_present and use_protection bits if there
119          * are any associated NonERP stations. */
120         /* TODO: use_protection bit can be set to zero even if
121          * there are NonERP stations present. This optimization
122          * might be useful if NonERP stations are "quiet".
123          * See 802.11g/D6 E-1 for recommended practice.
124          * In addition, Non ERP present might be set, if AP detects Non ERP
125          * operation on other APs. */
126
127         /* Add ERP Information element */
128         *eid++ = WLAN_EID_ERP_INFO;
129         *eid++ = 1;
130         *eid++ = ieee802_11_erp_info(hapd);
131
132         return eid;
133 }
134
135
136 static u8 * hostapd_eid_pwr_constraint(struct hostapd_data *hapd, u8 *eid)
137 {
138         u8 *pos = eid;
139         u8 local_pwr_constraint = 0;
140         int dfs;
141
142         if (hapd->iface->current_mode == NULL ||
143             hapd->iface->current_mode->mode != HOSTAPD_MODE_IEEE80211A)
144                 return eid;
145
146         /* Let host drivers add this IE if DFS support is offloaded */
147         if (hapd->iface->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD)
148                 return eid;
149
150         /*
151          * There is no DFS support and power constraint was not directly
152          * requested by config option.
153          */
154         if (!hapd->iconf->ieee80211h &&
155             hapd->iconf->local_pwr_constraint == -1)
156                 return eid;
157
158         /* Check if DFS is required by regulatory. */
159         dfs = hostapd_is_dfs_required(hapd->iface);
160         if (dfs < 0) {
161                 wpa_printf(MSG_WARNING, "Failed to check if DFS is required; ret=%d",
162                            dfs);
163                 dfs = 0;
164         }
165
166         if (dfs == 0 && hapd->iconf->local_pwr_constraint == -1)
167                 return eid;
168
169         /*
170          * ieee80211h (DFS) is enabled so Power Constraint element shall
171          * be added when running on DFS channel whenever local_pwr_constraint
172          * is configured or not. In order to meet regulations when TPC is not
173          * implemented using a transmit power that is below the legal maximum
174          * (including any mitigation factor) should help. In this case,
175          * indicate 3 dB below maximum allowed transmit power.
176          */
177         if (hapd->iconf->local_pwr_constraint == -1)
178                 local_pwr_constraint = 3;
179
180         /*
181          * A STA that is not an AP shall use a transmit power less than or
182          * equal to the local maximum transmit power level for the channel.
183          * The local maximum transmit power can be calculated from the formula:
184          * local max TX pwr = max TX pwr - local pwr constraint
185          * Where max TX pwr is maximum transmit power level specified for
186          * channel in Country element and local pwr constraint is specified
187          * for channel in this Power Constraint element.
188          */
189
190         /* Element ID */
191         *pos++ = WLAN_EID_PWR_CONSTRAINT;
192         /* Length */
193         *pos++ = 1;
194         /* Local Power Constraint */
195         if (local_pwr_constraint)
196                 *pos++ = local_pwr_constraint;
197         else
198                 *pos++ = hapd->iconf->local_pwr_constraint;
199
200         return pos;
201 }
202
203
204 static u8 * hostapd_eid_country_add(u8 *pos, u8 *end, int chan_spacing,
205                                     struct hostapd_channel_data *start,
206                                     struct hostapd_channel_data *prev)
207 {
208         if (end - pos < 3)
209                 return pos;
210
211         /* first channel number */
212         *pos++ = start->chan;
213         /* number of channels */
214         *pos++ = (prev->chan - start->chan) / chan_spacing + 1;
215         /* maximum transmit power level */
216         *pos++ = start->max_tx_power;
217
218         return pos;
219 }
220
221
222 static u8 * hostapd_eid_country(struct hostapd_data *hapd, u8 *eid,
223                                 int max_len)
224 {
225         u8 *pos = eid;
226         u8 *end = eid + max_len;
227         int i;
228         struct hostapd_hw_modes *mode;
229         struct hostapd_channel_data *start, *prev;
230         int chan_spacing = 1;
231
232         if (!hapd->iconf->ieee80211d || max_len < 6 ||
233             hapd->iface->current_mode == NULL)
234                 return eid;
235
236         *pos++ = WLAN_EID_COUNTRY;
237         pos++; /* length will be set later */
238         os_memcpy(pos, hapd->iconf->country, 3); /* e.g., 'US ' */
239         pos += 3;
240
241         mode = hapd->iface->current_mode;
242         if (mode->mode == HOSTAPD_MODE_IEEE80211A)
243                 chan_spacing = 4;
244
245         start = prev = NULL;
246         for (i = 0; i < mode->num_channels; i++) {
247                 struct hostapd_channel_data *chan = &mode->channels[i];
248                 if (chan->flag & HOSTAPD_CHAN_DISABLED)
249                         continue;
250                 if (start && prev &&
251                     prev->chan + chan_spacing == chan->chan &&
252                     start->max_tx_power == chan->max_tx_power) {
253                         prev = chan;
254                         continue; /* can use same entry */
255                 }
256
257                 if (start && prev) {
258                         pos = hostapd_eid_country_add(pos, end, chan_spacing,
259                                                       start, prev);
260                         start = NULL;
261                 }
262
263                 /* Start new group */
264                 start = prev = chan;
265         }
266
267         if (start) {
268                 pos = hostapd_eid_country_add(pos, end, chan_spacing,
269                                               start, prev);
270         }
271
272         if ((pos - eid) & 1) {
273                 if (end - pos < 1)
274                         return eid;
275                 *pos++ = 0; /* pad for 16-bit alignment */
276         }
277
278         eid[1] = (pos - eid) - 2;
279
280         return pos;
281 }
282
283
284 static u8 * hostapd_eid_wpa(struct hostapd_data *hapd, u8 *eid, size_t len)
285 {
286         const u8 *ie;
287         size_t ielen;
288
289         ie = wpa_auth_get_wpa_ie(hapd->wpa_auth, &ielen);
290         if (ie == NULL || ielen > len)
291                 return eid;
292
293         os_memcpy(eid, ie, ielen);
294         return eid + ielen;
295 }
296
297
298 static u8 * hostapd_eid_csa(struct hostapd_data *hapd, u8 *eid)
299 {
300         if (!hapd->cs_freq_params.channel)
301                 return eid;
302
303         *eid++ = WLAN_EID_CHANNEL_SWITCH;
304         *eid++ = 3;
305         *eid++ = hapd->cs_block_tx;
306         *eid++ = hapd->cs_freq_params.channel;
307         *eid++ = hapd->cs_count;
308
309         return eid;
310 }
311
312
313 static u8 * hostapd_eid_ecsa(struct hostapd_data *hapd, u8 *eid)
314 {
315         if (!hapd->cs_freq_params.channel || !hapd->iface->cs_oper_class)
316                 return eid;
317
318         *eid++ = WLAN_EID_EXT_CHANSWITCH_ANN;
319         *eid++ = 4;
320         *eid++ = hapd->cs_block_tx;
321         *eid++ = hapd->iface->cs_oper_class;
322         *eid++ = hapd->cs_freq_params.channel;
323         *eid++ = hapd->cs_count;
324
325         return eid;
326 }
327
328
329 static u8 * hostapd_eid_supported_op_classes(struct hostapd_data *hapd, u8 *eid)
330 {
331         u8 op_class, channel;
332
333         if (!(hapd->iface->drv_flags & WPA_DRIVER_FLAGS_AP_CSA) ||
334             !hapd->iface->freq)
335                 return eid;
336
337         if (ieee80211_freq_to_channel_ext(hapd->iface->freq,
338                                           hapd->iconf->secondary_channel,
339                                           hapd->iconf->vht_oper_chwidth,
340                                           &op_class, &channel) ==
341             NUM_HOSTAPD_MODES)
342                 return eid;
343
344         *eid++ = WLAN_EID_SUPPORTED_OPERATING_CLASSES;
345         *eid++ = 2;
346
347         /* Current Operating Class */
348         *eid++ = op_class;
349
350         /* TODO: Advertise all the supported operating classes */
351         *eid++ = 0;
352
353         return eid;
354 }
355
356
357 static u8 * hostapd_gen_probe_resp(struct hostapd_data *hapd,
358                                    const struct ieee80211_mgmt *req,
359                                    int is_p2p, size_t *resp_len)
360 {
361         struct ieee80211_mgmt *resp;
362         u8 *pos, *epos, *csa_pos;
363         size_t buflen;
364
365 #define MAX_PROBERESP_LEN 768
366         buflen = MAX_PROBERESP_LEN;
367 #ifdef CONFIG_WPS
368         if (hapd->wps_probe_resp_ie)
369                 buflen += wpabuf_len(hapd->wps_probe_resp_ie);
370 #endif /* CONFIG_WPS */
371 #ifdef CONFIG_P2P
372         if (hapd->p2p_probe_resp_ie)
373                 buflen += wpabuf_len(hapd->p2p_probe_resp_ie);
374 #endif /* CONFIG_P2P */
375 #ifdef CONFIG_FST
376         if (hapd->iface->fst_ies)
377                 buflen += wpabuf_len(hapd->iface->fst_ies);
378 #endif /* CONFIG_FST */
379         if (hapd->conf->vendor_elements)
380                 buflen += wpabuf_len(hapd->conf->vendor_elements);
381         if (hapd->conf->vendor_vht) {
382                 buflen += 5 + 2 + sizeof(struct ieee80211_vht_capabilities) +
383                         2 + sizeof(struct ieee80211_vht_operation);
384         }
385         resp = os_zalloc(buflen);
386         if (resp == NULL)
387                 return NULL;
388
389         epos = ((u8 *) resp) + MAX_PROBERESP_LEN;
390
391         resp->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
392                                            WLAN_FC_STYPE_PROBE_RESP);
393         if (req)
394                 os_memcpy(resp->da, req->sa, ETH_ALEN);
395         os_memcpy(resp->sa, hapd->own_addr, ETH_ALEN);
396
397         os_memcpy(resp->bssid, hapd->own_addr, ETH_ALEN);
398         resp->u.probe_resp.beacon_int =
399                 host_to_le16(hapd->iconf->beacon_int);
400
401         /* hardware or low-level driver will setup seq_ctrl and timestamp */
402         resp->u.probe_resp.capab_info =
403                 host_to_le16(hostapd_own_capab_info(hapd));
404
405         pos = resp->u.probe_resp.variable;
406         *pos++ = WLAN_EID_SSID;
407         *pos++ = hapd->conf->ssid.ssid_len;
408         os_memcpy(pos, hapd->conf->ssid.ssid, hapd->conf->ssid.ssid_len);
409         pos += hapd->conf->ssid.ssid_len;
410
411         /* Supported rates */
412         pos = hostapd_eid_supp_rates(hapd, pos);
413
414         /* DS Params */
415         pos = hostapd_eid_ds_params(hapd, pos);
416
417         pos = hostapd_eid_country(hapd, pos, epos - pos);
418
419         /* Power Constraint element */
420         pos = hostapd_eid_pwr_constraint(hapd, pos);
421
422         /* CSA IE */
423         csa_pos = hostapd_eid_csa(hapd, pos);
424         if (csa_pos != pos)
425                 hapd->cs_c_off_proberesp = csa_pos - (u8 *) resp - 1;
426         pos = csa_pos;
427
428         /* ERP Information element */
429         pos = hostapd_eid_erp_info(hapd, pos);
430
431         /* Extended supported rates */
432         pos = hostapd_eid_ext_supp_rates(hapd, pos);
433
434         /* RSN, MDIE, WPA */
435         pos = hostapd_eid_wpa(hapd, pos, epos - pos);
436
437         pos = hostapd_eid_bss_load(hapd, pos, epos - pos);
438
439         pos = hostapd_eid_rm_enabled_capab(hapd, pos, epos - pos);
440
441         /* eCSA IE */
442         csa_pos = hostapd_eid_ecsa(hapd, pos);
443         if (csa_pos != pos)
444                 hapd->cs_c_off_ecsa_proberesp = csa_pos - (u8 *) resp - 1;
445         pos = csa_pos;
446
447         pos = hostapd_eid_supported_op_classes(hapd, pos);
448
449 #ifdef CONFIG_IEEE80211N
450         /* Secondary Channel Offset element */
451         /* TODO: The standard doesn't specify a position for this element. */
452         pos = hostapd_eid_secondary_channel(hapd, pos);
453
454         pos = hostapd_eid_ht_capabilities(hapd, pos);
455         pos = hostapd_eid_ht_operation(hapd, pos);
456 #endif /* CONFIG_IEEE80211N */
457
458         pos = hostapd_eid_ext_capab(hapd, pos);
459
460         pos = hostapd_eid_time_adv(hapd, pos);
461         pos = hostapd_eid_time_zone(hapd, pos);
462
463         pos = hostapd_eid_interworking(hapd, pos);
464         pos = hostapd_eid_adv_proto(hapd, pos);
465         pos = hostapd_eid_roaming_consortium(hapd, pos);
466
467 #ifdef CONFIG_FST
468         if (hapd->iface->fst_ies) {
469                 os_memcpy(pos, wpabuf_head(hapd->iface->fst_ies),
470                           wpabuf_len(hapd->iface->fst_ies));
471                 pos += wpabuf_len(hapd->iface->fst_ies);
472         }
473 #endif /* CONFIG_FST */
474
475 #ifdef CONFIG_IEEE80211AC
476         if (hapd->iconf->ieee80211ac && !hapd->conf->disable_11ac) {
477                 pos = hostapd_eid_vht_capabilities(hapd, pos);
478                 pos = hostapd_eid_vht_operation(hapd, pos);
479                 pos = hostapd_eid_wb_chsw_wrapper(hapd, pos);
480         }
481         if (hapd->conf->vendor_vht)
482                 pos = hostapd_eid_vendor_vht(hapd, pos);
483 #endif /* CONFIG_IEEE80211AC */
484
485         /* Wi-Fi Alliance WMM */
486         pos = hostapd_eid_wmm(hapd, pos);
487
488 #ifdef CONFIG_WPS
489         if (hapd->conf->wps_state && hapd->wps_probe_resp_ie) {
490                 os_memcpy(pos, wpabuf_head(hapd->wps_probe_resp_ie),
491                           wpabuf_len(hapd->wps_probe_resp_ie));
492                 pos += wpabuf_len(hapd->wps_probe_resp_ie);
493         }
494 #endif /* CONFIG_WPS */
495
496 #ifdef CONFIG_P2P
497         if ((hapd->conf->p2p & P2P_ENABLED) && is_p2p &&
498             hapd->p2p_probe_resp_ie) {
499                 os_memcpy(pos, wpabuf_head(hapd->p2p_probe_resp_ie),
500                           wpabuf_len(hapd->p2p_probe_resp_ie));
501                 pos += wpabuf_len(hapd->p2p_probe_resp_ie);
502         }
503 #endif /* CONFIG_P2P */
504 #ifdef CONFIG_P2P_MANAGER
505         if ((hapd->conf->p2p & (P2P_MANAGE | P2P_ENABLED | P2P_GROUP_OWNER)) ==
506             P2P_MANAGE)
507                 pos = hostapd_eid_p2p_manage(hapd, pos);
508 #endif /* CONFIG_P2P_MANAGER */
509
510 #ifdef CONFIG_HS20
511         pos = hostapd_eid_hs20_indication(hapd, pos);
512         pos = hostapd_eid_osen(hapd, pos);
513 #endif /* CONFIG_HS20 */
514
515         if (hapd->conf->vendor_elements) {
516                 os_memcpy(pos, wpabuf_head(hapd->conf->vendor_elements),
517                           wpabuf_len(hapd->conf->vendor_elements));
518                 pos += wpabuf_len(hapd->conf->vendor_elements);
519         }
520
521         *resp_len = pos - (u8 *) resp;
522         return (u8 *) resp;
523 }
524
525
526 enum ssid_match_result {
527         NO_SSID_MATCH,
528         EXACT_SSID_MATCH,
529         WILDCARD_SSID_MATCH
530 };
531
532 static enum ssid_match_result ssid_match(struct hostapd_data *hapd,
533                                          const u8 *ssid, size_t ssid_len,
534                                          const u8 *ssid_list,
535                                          size_t ssid_list_len)
536 {
537         const u8 *pos, *end;
538         int wildcard = 0;
539
540         if (ssid_len == 0)
541                 wildcard = 1;
542         if (ssid_len == hapd->conf->ssid.ssid_len &&
543             os_memcmp(ssid, hapd->conf->ssid.ssid, ssid_len) == 0)
544                 return EXACT_SSID_MATCH;
545
546         if (ssid_list == NULL)
547                 return wildcard ? WILDCARD_SSID_MATCH : NO_SSID_MATCH;
548
549         pos = ssid_list;
550         end = ssid_list + ssid_list_len;
551         while (pos + 1 <= end) {
552                 if (pos + 2 + pos[1] > end)
553                         break;
554                 if (pos[1] == 0)
555                         wildcard = 1;
556                 if (pos[1] == hapd->conf->ssid.ssid_len &&
557                     os_memcmp(pos + 2, hapd->conf->ssid.ssid, pos[1]) == 0)
558                         return EXACT_SSID_MATCH;
559                 pos += 2 + pos[1];
560         }
561
562         return wildcard ? WILDCARD_SSID_MATCH : NO_SSID_MATCH;
563 }
564
565
566 void sta_track_expire(struct hostapd_iface *iface, int force)
567 {
568         struct os_reltime now;
569         struct hostapd_sta_info *info;
570
571         if (!iface->num_sta_seen)
572                 return;
573
574         os_get_reltime(&now);
575         while ((info = dl_list_first(&iface->sta_seen, struct hostapd_sta_info,
576                                      list))) {
577                 if (!force &&
578                     !os_reltime_expired(&now, &info->last_seen,
579                                         iface->conf->track_sta_max_age))
580                         break;
581                 force = 0;
582
583                 wpa_printf(MSG_MSGDUMP, "%s: Expire STA tracking entry for "
584                            MACSTR, iface->bss[0]->conf->iface,
585                            MAC2STR(info->addr));
586                 dl_list_del(&info->list);
587                 iface->num_sta_seen--;
588                 os_free(info);
589         }
590 }
591
592
593 static struct hostapd_sta_info * sta_track_get(struct hostapd_iface *iface,
594                                                const u8 *addr)
595 {
596         struct hostapd_sta_info *info;
597
598         dl_list_for_each(info, &iface->sta_seen, struct hostapd_sta_info, list)
599                 if (os_memcmp(addr, info->addr, ETH_ALEN) == 0)
600                         return info;
601
602         return NULL;
603 }
604
605
606 void sta_track_add(struct hostapd_iface *iface, const u8 *addr)
607 {
608         struct hostapd_sta_info *info;
609
610         info = sta_track_get(iface, addr);
611         if (info) {
612                 /* Move the most recent entry to the end of the list */
613                 dl_list_del(&info->list);
614                 dl_list_add_tail(&iface->sta_seen, &info->list);
615                 os_get_reltime(&info->last_seen);
616                 return;
617         }
618
619         /* Add a new entry */
620         info = os_zalloc(sizeof(*info));
621         os_memcpy(info->addr, addr, ETH_ALEN);
622         os_get_reltime(&info->last_seen);
623
624         if (iface->num_sta_seen >= iface->conf->track_sta_max_num) {
625                 /* Expire oldest entry to make room for a new one */
626                 sta_track_expire(iface, 1);
627         }
628
629         wpa_printf(MSG_MSGDUMP, "%s: Add STA tracking entry for "
630                    MACSTR, iface->bss[0]->conf->iface, MAC2STR(addr));
631         dl_list_add_tail(&iface->sta_seen, &info->list);
632         iface->num_sta_seen++;
633 }
634
635
636 struct hostapd_data *
637 sta_track_seen_on(struct hostapd_iface *iface, const u8 *addr,
638                   const char *ifname)
639 {
640         struct hapd_interfaces *interfaces = iface->interfaces;
641         size_t i, j;
642
643         for (i = 0; i < interfaces->count; i++) {
644                 struct hostapd_data *hapd = NULL;
645
646                 iface = interfaces->iface[i];
647                 for (j = 0; j < iface->num_bss; j++) {
648                         hapd = iface->bss[j];
649                         if (os_strcmp(ifname, hapd->conf->iface) == 0)
650                                 break;
651                         hapd = NULL;
652                 }
653
654                 if (hapd && sta_track_get(iface, addr))
655                         return hapd;
656         }
657
658         return NULL;
659 }
660
661
662 void handle_probe_req(struct hostapd_data *hapd,
663                       const struct ieee80211_mgmt *mgmt, size_t len,
664                       int ssi_signal)
665 {
666         u8 *resp;
667         struct ieee802_11_elems elems;
668         const u8 *ie;
669         size_t ie_len;
670         size_t i, resp_len;
671         int noack;
672         enum ssid_match_result res;
673
674         ie = mgmt->u.probe_req.variable;
675         if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.probe_req))
676                 return;
677         if (hapd->iconf->track_sta_max_num)
678                 sta_track_add(hapd->iface, mgmt->sa);
679         ie_len = len - (IEEE80211_HDRLEN + sizeof(mgmt->u.probe_req));
680
681         for (i = 0; hapd->probereq_cb && i < hapd->num_probereq_cb; i++)
682                 if (hapd->probereq_cb[i].cb(hapd->probereq_cb[i].ctx,
683                                             mgmt->sa, mgmt->da, mgmt->bssid,
684                                             ie, ie_len, ssi_signal) > 0)
685                         return;
686
687         if (!hapd->iconf->send_probe_response)
688                 return;
689
690         if (ieee802_11_parse_elems(ie, ie_len, &elems, 0) == ParseFailed) {
691                 wpa_printf(MSG_DEBUG, "Could not parse ProbeReq from " MACSTR,
692                            MAC2STR(mgmt->sa));
693                 return;
694         }
695
696         if ((!elems.ssid || !elems.supp_rates)) {
697                 wpa_printf(MSG_DEBUG, "STA " MACSTR " sent probe request "
698                            "without SSID or supported rates element",
699                            MAC2STR(mgmt->sa));
700                 return;
701         }
702
703         /*
704          * No need to reply if the Probe Request frame was sent on an adjacent
705          * channel. IEEE Std 802.11-2012 describes this as a requirement for an
706          * AP with dot11RadioMeasurementActivated set to true, but strictly
707          * speaking does not allow such ignoring of Probe Request frames if
708          * dot11RadioMeasurementActivated is false. Anyway, this can help reduce
709          * number of unnecessary Probe Response frames for cases where the STA
710          * is less likely to see them (Probe Request frame sent on a
711          * neighboring, but partially overlapping, channel).
712          */
713         if (elems.ds_params &&
714             hapd->iface->current_mode &&
715             (hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G ||
716              hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211B) &&
717             hapd->iconf->channel != elems.ds_params[0]) {
718                 wpa_printf(MSG_DEBUG,
719                            "Ignore Probe Request due to DS Params mismatch: chan=%u != ds.chan=%u",
720                            hapd->iconf->channel, elems.ds_params[0]);
721                 return;
722         }
723
724 #ifdef CONFIG_P2P
725         if (hapd->p2p && elems.wps_ie) {
726                 struct wpabuf *wps;
727                 wps = ieee802_11_vendor_ie_concat(ie, ie_len, WPS_DEV_OUI_WFA);
728                 if (wps && !p2p_group_match_dev_type(hapd->p2p_group, wps)) {
729                         wpa_printf(MSG_MSGDUMP, "P2P: Ignore Probe Request "
730                                    "due to mismatch with Requested Device "
731                                    "Type");
732                         wpabuf_free(wps);
733                         return;
734                 }
735                 wpabuf_free(wps);
736         }
737
738         if (hapd->p2p && elems.p2p) {
739                 struct wpabuf *p2p;
740                 p2p = ieee802_11_vendor_ie_concat(ie, ie_len, P2P_IE_VENDOR_TYPE);
741                 if (p2p && !p2p_group_match_dev_id(hapd->p2p_group, p2p)) {
742                         wpa_printf(MSG_MSGDUMP, "P2P: Ignore Probe Request "
743                                    "due to mismatch with Device ID");
744                         wpabuf_free(p2p);
745                         return;
746                 }
747                 wpabuf_free(p2p);
748         }
749 #endif /* CONFIG_P2P */
750
751         if (hapd->conf->ignore_broadcast_ssid && elems.ssid_len == 0 &&
752             elems.ssid_list_len == 0) {
753                 wpa_printf(MSG_MSGDUMP, "Probe Request from " MACSTR " for "
754                            "broadcast SSID ignored", MAC2STR(mgmt->sa));
755                 return;
756         }
757
758 #ifdef CONFIG_P2P
759         if ((hapd->conf->p2p & P2P_GROUP_OWNER) &&
760             elems.ssid_len == P2P_WILDCARD_SSID_LEN &&
761             os_memcmp(elems.ssid, P2P_WILDCARD_SSID,
762                       P2P_WILDCARD_SSID_LEN) == 0) {
763                 /* Process P2P Wildcard SSID like Wildcard SSID */
764                 elems.ssid_len = 0;
765         }
766 #endif /* CONFIG_P2P */
767
768         res = ssid_match(hapd, elems.ssid, elems.ssid_len,
769                          elems.ssid_list, elems.ssid_list_len);
770         if (res == NO_SSID_MATCH) {
771                 if (!(mgmt->da[0] & 0x01)) {
772                         wpa_printf(MSG_MSGDUMP, "Probe Request from " MACSTR
773                                    " for foreign SSID '%s' (DA " MACSTR ")%s",
774                                    MAC2STR(mgmt->sa),
775                                    wpa_ssid_txt(elems.ssid, elems.ssid_len),
776                                    MAC2STR(mgmt->da),
777                                    elems.ssid_list ? " (SSID list)" : "");
778                 }
779                 return;
780         }
781
782 #ifdef CONFIG_INTERWORKING
783         if (hapd->conf->interworking &&
784             elems.interworking && elems.interworking_len >= 1) {
785                 u8 ant = elems.interworking[0] & 0x0f;
786                 if (ant != INTERWORKING_ANT_WILDCARD &&
787                     ant != hapd->conf->access_network_type) {
788                         wpa_printf(MSG_MSGDUMP, "Probe Request from " MACSTR
789                                    " for mismatching ANT %u ignored",
790                                    MAC2STR(mgmt->sa), ant);
791                         return;
792                 }
793         }
794
795         if (hapd->conf->interworking && elems.interworking &&
796             (elems.interworking_len == 7 || elems.interworking_len == 9)) {
797                 const u8 *hessid;
798                 if (elems.interworking_len == 7)
799                         hessid = elems.interworking + 1;
800                 else
801                         hessid = elems.interworking + 1 + 2;
802                 if (!is_broadcast_ether_addr(hessid) &&
803                     os_memcmp(hessid, hapd->conf->hessid, ETH_ALEN) != 0) {
804                         wpa_printf(MSG_MSGDUMP, "Probe Request from " MACSTR
805                                    " for mismatching HESSID " MACSTR
806                                    " ignored",
807                                    MAC2STR(mgmt->sa), MAC2STR(hessid));
808                         return;
809                 }
810         }
811 #endif /* CONFIG_INTERWORKING */
812
813 #ifdef CONFIG_P2P
814         if ((hapd->conf->p2p & P2P_GROUP_OWNER) &&
815             supp_rates_11b_only(&elems)) {
816                 /* Indicates support for 11b rates only */
817                 wpa_printf(MSG_EXCESSIVE, "P2P: Ignore Probe Request from "
818                            MACSTR " with only 802.11b rates",
819                            MAC2STR(mgmt->sa));
820                 return;
821         }
822 #endif /* CONFIG_P2P */
823
824         /* TODO: verify that supp_rates contains at least one matching rate
825          * with AP configuration */
826
827         if (hapd->conf->no_probe_resp_if_seen_on &&
828             is_multicast_ether_addr(mgmt->da) &&
829             is_multicast_ether_addr(mgmt->bssid) &&
830             sta_track_seen_on(hapd->iface, mgmt->sa,
831                               hapd->conf->no_probe_resp_if_seen_on)) {
832                 wpa_printf(MSG_MSGDUMP, "%s: Ignore Probe Request from " MACSTR
833                            " since STA has been seen on %s",
834                            hapd->conf->iface, MAC2STR(mgmt->sa),
835                            hapd->conf->no_probe_resp_if_seen_on);
836                 return;
837         }
838
839 #ifdef CONFIG_TESTING_OPTIONS
840         if (hapd->iconf->ignore_probe_probability > 0.0 &&
841             drand48() < hapd->iconf->ignore_probe_probability) {
842                 wpa_printf(MSG_INFO,
843                            "TESTING: ignoring probe request from " MACSTR,
844                            MAC2STR(mgmt->sa));
845                 return;
846         }
847 #endif /* CONFIG_TESTING_OPTIONS */
848
849         resp = hostapd_gen_probe_resp(hapd, mgmt, elems.p2p != NULL,
850                                       &resp_len);
851         if (resp == NULL)
852                 return;
853
854         /*
855          * If this is a broadcast probe request, apply no ack policy to avoid
856          * excessive retries.
857          */
858         noack = !!(res == WILDCARD_SSID_MATCH &&
859                    is_broadcast_ether_addr(mgmt->da));
860
861         if (hostapd_drv_send_mlme(hapd, resp, resp_len, noack) < 0)
862                 wpa_printf(MSG_INFO, "handle_probe_req: send failed");
863
864         os_free(resp);
865
866         wpa_printf(MSG_EXCESSIVE, "STA " MACSTR " sent probe request for %s "
867                    "SSID", MAC2STR(mgmt->sa),
868                    elems.ssid_len == 0 ? "broadcast" : "our");
869 }
870
871
872 static u8 * hostapd_probe_resp_offloads(struct hostapd_data *hapd,
873                                         size_t *resp_len)
874 {
875         /* check probe response offloading caps and print warnings */
876         if (!(hapd->iface->drv_flags & WPA_DRIVER_FLAGS_PROBE_RESP_OFFLOAD))
877                 return NULL;
878
879 #ifdef CONFIG_WPS
880         if (hapd->conf->wps_state && hapd->wps_probe_resp_ie &&
881             (!(hapd->iface->probe_resp_offloads &
882                (WPA_DRIVER_PROBE_RESP_OFFLOAD_WPS |
883                 WPA_DRIVER_PROBE_RESP_OFFLOAD_WPS2))))
884                 wpa_printf(MSG_WARNING, "Device is trying to offload WPS "
885                            "Probe Response while not supporting this");
886 #endif /* CONFIG_WPS */
887
888 #ifdef CONFIG_P2P
889         if ((hapd->conf->p2p & P2P_ENABLED) && hapd->p2p_probe_resp_ie &&
890             !(hapd->iface->probe_resp_offloads &
891               WPA_DRIVER_PROBE_RESP_OFFLOAD_P2P))
892                 wpa_printf(MSG_WARNING, "Device is trying to offload P2P "
893                            "Probe Response while not supporting this");
894 #endif  /* CONFIG_P2P */
895
896         if (hapd->conf->interworking &&
897             !(hapd->iface->probe_resp_offloads &
898               WPA_DRIVER_PROBE_RESP_OFFLOAD_INTERWORKING))
899                 wpa_printf(MSG_WARNING, "Device is trying to offload "
900                            "Interworking Probe Response while not supporting "
901                            "this");
902
903         /* Generate a Probe Response template for the non-P2P case */
904         return hostapd_gen_probe_resp(hapd, NULL, 0, resp_len);
905 }
906
907 #endif /* NEED_AP_MLME */
908
909
910 int ieee802_11_build_ap_params(struct hostapd_data *hapd,
911                                struct wpa_driver_ap_params *params)
912 {
913         struct ieee80211_mgmt *head = NULL;
914         u8 *tail = NULL;
915         size_t head_len = 0, tail_len = 0;
916         u8 *resp = NULL;
917         size_t resp_len = 0;
918 #ifdef NEED_AP_MLME
919         u16 capab_info;
920         u8 *pos, *tailpos, *csa_pos;
921
922 #define BEACON_HEAD_BUF_SIZE 256
923 #define BEACON_TAIL_BUF_SIZE 512
924         head = os_zalloc(BEACON_HEAD_BUF_SIZE);
925         tail_len = BEACON_TAIL_BUF_SIZE;
926 #ifdef CONFIG_WPS
927         if (hapd->conf->wps_state && hapd->wps_beacon_ie)
928                 tail_len += wpabuf_len(hapd->wps_beacon_ie);
929 #endif /* CONFIG_WPS */
930 #ifdef CONFIG_P2P
931         if (hapd->p2p_beacon_ie)
932                 tail_len += wpabuf_len(hapd->p2p_beacon_ie);
933 #endif /* CONFIG_P2P */
934 #ifdef CONFIG_FST
935         if (hapd->iface->fst_ies)
936                 tail_len += wpabuf_len(hapd->iface->fst_ies);
937 #endif /* CONFIG_FST */
938         if (hapd->conf->vendor_elements)
939                 tail_len += wpabuf_len(hapd->conf->vendor_elements);
940
941 #ifdef CONFIG_IEEE80211AC
942         if (hapd->conf->vendor_vht) {
943                 tail_len += 5 + 2 + sizeof(struct ieee80211_vht_capabilities) +
944                         2 + sizeof(struct ieee80211_vht_operation);
945         }
946 #endif /* CONFIG_IEEE80211AC */
947
948         tailpos = tail = os_malloc(tail_len);
949         if (head == NULL || tail == NULL) {
950                 wpa_printf(MSG_ERROR, "Failed to set beacon data");
951                 os_free(head);
952                 os_free(tail);
953                 return -1;
954         }
955
956         head->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
957                                            WLAN_FC_STYPE_BEACON);
958         head->duration = host_to_le16(0);
959         os_memset(head->da, 0xff, ETH_ALEN);
960
961         os_memcpy(head->sa, hapd->own_addr, ETH_ALEN);
962         os_memcpy(head->bssid, hapd->own_addr, ETH_ALEN);
963         head->u.beacon.beacon_int =
964                 host_to_le16(hapd->iconf->beacon_int);
965
966         /* hardware or low-level driver will setup seq_ctrl and timestamp */
967         capab_info = hostapd_own_capab_info(hapd);
968         head->u.beacon.capab_info = host_to_le16(capab_info);
969         pos = &head->u.beacon.variable[0];
970
971         /* SSID */
972         *pos++ = WLAN_EID_SSID;
973         if (hapd->conf->ignore_broadcast_ssid == 2) {
974                 /* clear the data, but keep the correct length of the SSID */
975                 *pos++ = hapd->conf->ssid.ssid_len;
976                 os_memset(pos, 0, hapd->conf->ssid.ssid_len);
977                 pos += hapd->conf->ssid.ssid_len;
978         } else if (hapd->conf->ignore_broadcast_ssid) {
979                 *pos++ = 0; /* empty SSID */
980         } else {
981                 *pos++ = hapd->conf->ssid.ssid_len;
982                 os_memcpy(pos, hapd->conf->ssid.ssid,
983                           hapd->conf->ssid.ssid_len);
984                 pos += hapd->conf->ssid.ssid_len;
985         }
986
987         /* Supported rates */
988         pos = hostapd_eid_supp_rates(hapd, pos);
989
990         /* DS Params */
991         pos = hostapd_eid_ds_params(hapd, pos);
992
993         head_len = pos - (u8 *) head;
994
995         tailpos = hostapd_eid_country(hapd, tailpos,
996                                       tail + BEACON_TAIL_BUF_SIZE - tailpos);
997
998         /* Power Constraint element */
999         tailpos = hostapd_eid_pwr_constraint(hapd, tailpos);
1000
1001         /* CSA IE */
1002         csa_pos = hostapd_eid_csa(hapd, tailpos);
1003         if (csa_pos != tailpos)
1004                 hapd->cs_c_off_beacon = csa_pos - tail - 1;
1005         tailpos = csa_pos;
1006
1007         /* ERP Information element */
1008         tailpos = hostapd_eid_erp_info(hapd, tailpos);
1009
1010         /* Extended supported rates */
1011         tailpos = hostapd_eid_ext_supp_rates(hapd, tailpos);
1012
1013         /* RSN, MDIE, WPA */
1014         tailpos = hostapd_eid_wpa(hapd, tailpos, tail + BEACON_TAIL_BUF_SIZE -
1015                                   tailpos);
1016
1017         tailpos = hostapd_eid_rm_enabled_capab(hapd, tailpos,
1018                                                tail + BEACON_TAIL_BUF_SIZE -
1019                                                tailpos);
1020
1021         tailpos = hostapd_eid_bss_load(hapd, tailpos,
1022                                        tail + BEACON_TAIL_BUF_SIZE - tailpos);
1023
1024         /* eCSA IE */
1025         csa_pos = hostapd_eid_ecsa(hapd, tailpos);
1026         if (csa_pos != tailpos)
1027                 hapd->cs_c_off_ecsa_beacon = csa_pos - tail - 1;
1028         tailpos = csa_pos;
1029
1030         tailpos = hostapd_eid_supported_op_classes(hapd, tailpos);
1031
1032 #ifdef CONFIG_IEEE80211N
1033         /* Secondary Channel Offset element */
1034         /* TODO: The standard doesn't specify a position for this element. */
1035         tailpos = hostapd_eid_secondary_channel(hapd, tailpos);
1036
1037         tailpos = hostapd_eid_ht_capabilities(hapd, tailpos);
1038         tailpos = hostapd_eid_ht_operation(hapd, tailpos);
1039 #endif /* CONFIG_IEEE80211N */
1040
1041         tailpos = hostapd_eid_ext_capab(hapd, tailpos);
1042
1043         /*
1044          * TODO: Time Advertisement element should only be included in some
1045          * DTIM Beacon frames.
1046          */
1047         tailpos = hostapd_eid_time_adv(hapd, tailpos);
1048
1049         tailpos = hostapd_eid_interworking(hapd, tailpos);
1050         tailpos = hostapd_eid_adv_proto(hapd, tailpos);
1051         tailpos = hostapd_eid_roaming_consortium(hapd, tailpos);
1052
1053 #ifdef CONFIG_FST
1054         if (hapd->iface->fst_ies) {
1055                 os_memcpy(tailpos, wpabuf_head(hapd->iface->fst_ies),
1056                           wpabuf_len(hapd->iface->fst_ies));
1057                 tailpos += wpabuf_len(hapd->iface->fst_ies);
1058         }
1059 #endif /* CONFIG_FST */
1060
1061 #ifdef CONFIG_IEEE80211AC
1062         if (hapd->iconf->ieee80211ac && !hapd->conf->disable_11ac) {
1063                 tailpos = hostapd_eid_vht_capabilities(hapd, tailpos);
1064                 tailpos = hostapd_eid_vht_operation(hapd, tailpos);
1065                 tailpos = hostapd_eid_wb_chsw_wrapper(hapd, tailpos);
1066         }
1067         if (hapd->conf->vendor_vht)
1068                 tailpos = hostapd_eid_vendor_vht(hapd, tailpos);
1069 #endif /* CONFIG_IEEE80211AC */
1070
1071         /* Wi-Fi Alliance WMM */
1072         tailpos = hostapd_eid_wmm(hapd, tailpos);
1073
1074 #ifdef CONFIG_WPS
1075         if (hapd->conf->wps_state && hapd->wps_beacon_ie) {
1076                 os_memcpy(tailpos, wpabuf_head(hapd->wps_beacon_ie),
1077                           wpabuf_len(hapd->wps_beacon_ie));
1078                 tailpos += wpabuf_len(hapd->wps_beacon_ie);
1079         }
1080 #endif /* CONFIG_WPS */
1081
1082 #ifdef CONFIG_P2P
1083         if ((hapd->conf->p2p & P2P_ENABLED) && hapd->p2p_beacon_ie) {
1084                 os_memcpy(tailpos, wpabuf_head(hapd->p2p_beacon_ie),
1085                           wpabuf_len(hapd->p2p_beacon_ie));
1086                 tailpos += wpabuf_len(hapd->p2p_beacon_ie);
1087         }
1088 #endif /* CONFIG_P2P */
1089 #ifdef CONFIG_P2P_MANAGER
1090         if ((hapd->conf->p2p & (P2P_MANAGE | P2P_ENABLED | P2P_GROUP_OWNER)) ==
1091             P2P_MANAGE)
1092                 tailpos = hostapd_eid_p2p_manage(hapd, tailpos);
1093 #endif /* CONFIG_P2P_MANAGER */
1094
1095 #ifdef CONFIG_HS20
1096         tailpos = hostapd_eid_hs20_indication(hapd, tailpos);
1097         tailpos = hostapd_eid_osen(hapd, tailpos);
1098 #endif /* CONFIG_HS20 */
1099
1100         if (hapd->conf->vendor_elements) {
1101                 os_memcpy(tailpos, wpabuf_head(hapd->conf->vendor_elements),
1102                           wpabuf_len(hapd->conf->vendor_elements));
1103                 tailpos += wpabuf_len(hapd->conf->vendor_elements);
1104         }
1105
1106         tail_len = tailpos > tail ? tailpos - tail : 0;
1107
1108         resp = hostapd_probe_resp_offloads(hapd, &resp_len);
1109 #endif /* NEED_AP_MLME */
1110
1111         os_memset(params, 0, sizeof(*params));
1112         params->head = (u8 *) head;
1113         params->head_len = head_len;
1114         params->tail = tail;
1115         params->tail_len = tail_len;
1116         params->proberesp = resp;
1117         params->proberesp_len = resp_len;
1118         params->dtim_period = hapd->conf->dtim_period;
1119         params->beacon_int = hapd->iconf->beacon_int;
1120         params->basic_rates = hapd->iface->basic_rates;
1121         params->ssid = hapd->conf->ssid.ssid;
1122         params->ssid_len = hapd->conf->ssid.ssid_len;
1123         if ((hapd->conf->wpa & (WPA_PROTO_WPA | WPA_PROTO_RSN)) ==
1124             (WPA_PROTO_WPA | WPA_PROTO_RSN))
1125                 params->pairwise_ciphers = hapd->conf->wpa_pairwise |
1126                         hapd->conf->rsn_pairwise;
1127         else if (hapd->conf->wpa & WPA_PROTO_RSN)
1128                 params->pairwise_ciphers = hapd->conf->rsn_pairwise;
1129         else if (hapd->conf->wpa & WPA_PROTO_WPA)
1130                 params->pairwise_ciphers = hapd->conf->wpa_pairwise;
1131         params->group_cipher = hapd->conf->wpa_group;
1132         params->key_mgmt_suites = hapd->conf->wpa_key_mgmt;
1133         params->auth_algs = hapd->conf->auth_algs;
1134         params->wpa_version = hapd->conf->wpa;
1135         params->privacy = hapd->conf->ssid.wep.keys_set || hapd->conf->wpa ||
1136                 (hapd->conf->ieee802_1x &&
1137                  (hapd->conf->default_wep_key_len ||
1138                   hapd->conf->individual_wep_key_len));
1139         switch (hapd->conf->ignore_broadcast_ssid) {
1140         case 0:
1141                 params->hide_ssid = NO_SSID_HIDING;
1142                 break;
1143         case 1:
1144                 params->hide_ssid = HIDDEN_SSID_ZERO_LEN;
1145                 break;
1146         case 2:
1147                 params->hide_ssid = HIDDEN_SSID_ZERO_CONTENTS;
1148                 break;
1149         }
1150         params->isolate = hapd->conf->isolate;
1151         params->smps_mode = hapd->iconf->ht_capab & HT_CAP_INFO_SMPS_MASK;
1152 #ifdef NEED_AP_MLME
1153         params->cts_protect = !!(ieee802_11_erp_info(hapd) &
1154                                 ERP_INFO_USE_PROTECTION);
1155         params->preamble = hapd->iface->num_sta_no_short_preamble == 0 &&
1156                 hapd->iconf->preamble == SHORT_PREAMBLE;
1157         if (hapd->iface->current_mode &&
1158             hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G)
1159                 params->short_slot_time =
1160                         hapd->iface->num_sta_no_short_slot_time > 0 ? 0 : 1;
1161         else
1162                 params->short_slot_time = -1;
1163         if (!hapd->iconf->ieee80211n || hapd->conf->disable_11n)
1164                 params->ht_opmode = -1;
1165         else
1166                 params->ht_opmode = hapd->iface->ht_op_mode;
1167 #endif /* NEED_AP_MLME */
1168         params->interworking = hapd->conf->interworking;
1169         if (hapd->conf->interworking &&
1170             !is_zero_ether_addr(hapd->conf->hessid))
1171                 params->hessid = hapd->conf->hessid;
1172         params->access_network_type = hapd->conf->access_network_type;
1173         params->ap_max_inactivity = hapd->conf->ap_max_inactivity;
1174 #ifdef CONFIG_P2P
1175         params->p2p_go_ctwindow = hapd->iconf->p2p_go_ctwindow;
1176 #endif /* CONFIG_P2P */
1177 #ifdef CONFIG_HS20
1178         params->disable_dgaf = hapd->conf->disable_dgaf;
1179         if (hapd->conf->osen) {
1180                 params->privacy = 1;
1181                 params->osen = 1;
1182         }
1183 #endif /* CONFIG_HS20 */
1184         return 0;
1185 }
1186
1187
1188 void ieee802_11_free_ap_params(struct wpa_driver_ap_params *params)
1189 {
1190         os_free(params->tail);
1191         params->tail = NULL;
1192         os_free(params->head);
1193         params->head = NULL;
1194         os_free(params->proberesp);
1195         params->proberesp = NULL;
1196 }
1197
1198
1199 int ieee802_11_set_beacon(struct hostapd_data *hapd)
1200 {
1201         struct wpa_driver_ap_params params;
1202         struct hostapd_freq_params freq;
1203         struct hostapd_iface *iface = hapd->iface;
1204         struct hostapd_config *iconf = iface->conf;
1205         struct wpabuf *beacon, *proberesp, *assocresp;
1206         int res, ret = -1;
1207
1208         if (hapd->csa_in_progress) {
1209                 wpa_printf(MSG_ERROR, "Cannot set beacons during CSA period");
1210                 return -1;
1211         }
1212
1213         hapd->beacon_set_done = 1;
1214
1215         if (ieee802_11_build_ap_params(hapd, &params) < 0)
1216                 return -1;
1217
1218         if (hostapd_build_ap_extra_ies(hapd, &beacon, &proberesp, &assocresp) <
1219             0)
1220                 goto fail;
1221
1222         params.beacon_ies = beacon;
1223         params.proberesp_ies = proberesp;
1224         params.assocresp_ies = assocresp;
1225         params.reenable = hapd->reenable_beacon;
1226         hapd->reenable_beacon = 0;
1227
1228         if (iface->current_mode &&
1229             hostapd_set_freq_params(&freq, iconf->hw_mode, iface->freq,
1230                                     iconf->channel, iconf->ieee80211n,
1231                                     iconf->ieee80211ac,
1232                                     iconf->secondary_channel,
1233                                     iconf->vht_oper_chwidth,
1234                                     iconf->vht_oper_centr_freq_seg0_idx,
1235                                     iconf->vht_oper_centr_freq_seg1_idx,
1236                                     iface->current_mode->vht_capab) == 0)
1237                 params.freq = &freq;
1238
1239         res = hostapd_drv_set_ap(hapd, &params);
1240         hostapd_free_ap_extra_ies(hapd, beacon, proberesp, assocresp);
1241         if (res)
1242                 wpa_printf(MSG_ERROR, "Failed to set beacon parameters");
1243         else
1244                 ret = 0;
1245 fail:
1246         ieee802_11_free_ap_params(&params);
1247         return ret;
1248 }
1249
1250
1251 int ieee802_11_set_beacons(struct hostapd_iface *iface)
1252 {
1253         size_t i;
1254         int ret = 0;
1255
1256         for (i = 0; i < iface->num_bss; i++) {
1257                 if (iface->bss[i]->started &&
1258                     ieee802_11_set_beacon(iface->bss[i]) < 0)
1259                         ret = -1;
1260         }
1261
1262         return ret;
1263 }
1264
1265
1266 /* only update beacons if started */
1267 int ieee802_11_update_beacons(struct hostapd_iface *iface)
1268 {
1269         size_t i;
1270         int ret = 0;
1271
1272         for (i = 0; i < iface->num_bss; i++) {
1273                 if (iface->bss[i]->beacon_set_done && iface->bss[i]->started &&
1274                     ieee802_11_set_beacon(iface->bss[i]) < 0)
1275                         ret = -1;
1276         }
1277
1278         return ret;
1279 }
1280
1281 #endif /* CONFIG_NATIVE_WINDOWS */