Move HT CSA related IE function
[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_add_csa_elems(struct hostapd_data *hapd, u8 *pos,
330                                   u8 *start, unsigned int *csa_counter_off,
331                                   unsigned int *ecsa_counter_off)
332 {
333         u8 *curr_pos = pos;
334         u8 *csa_pos = pos;
335
336         if (!csa_counter_off || !ecsa_counter_off)
337                 return pos;
338
339         *csa_counter_off = 0;
340         *ecsa_counter_off = 0;
341
342         curr_pos = hostapd_eid_csa(hapd, curr_pos);
343
344         /* save an offset to the csa counter - should be last byte */
345         if (curr_pos != pos)
346                 *csa_counter_off = curr_pos - start - 1;
347
348         csa_pos = curr_pos;
349         curr_pos = hostapd_eid_ecsa(hapd, curr_pos);
350
351         /* save an offset to the eCSA counter - should be last byte */
352         if (curr_pos != csa_pos)
353                 *ecsa_counter_off = curr_pos - start - 1;
354
355         /* at least one of ies is added */
356         if (pos != curr_pos) {
357 #ifdef CONFIG_IEEE80211N
358                 curr_pos = hostapd_eid_secondary_channel(hapd, curr_pos);
359 #endif /* CONFIG_IEEE80211N */
360 #ifdef CONFIG_IEEE80211AC
361                 curr_pos = hostapd_eid_wb_chsw_wrapper(hapd, curr_pos);
362 #endif /* CONFIG_IEEE80211AC */
363         }
364         return curr_pos;
365 }
366
367
368 static u8 * hostapd_gen_probe_resp(struct hostapd_data *hapd,
369                                    const struct ieee80211_mgmt *req,
370                                    int is_p2p, size_t *resp_len)
371 {
372         struct ieee80211_mgmt *resp;
373         u8 *pos, *epos;
374         size_t buflen;
375
376 #define MAX_PROBERESP_LEN 768
377         buflen = MAX_PROBERESP_LEN;
378 #ifdef CONFIG_WPS
379         if (hapd->wps_probe_resp_ie)
380                 buflen += wpabuf_len(hapd->wps_probe_resp_ie);
381 #endif /* CONFIG_WPS */
382 #ifdef CONFIG_P2P
383         if (hapd->p2p_probe_resp_ie)
384                 buflen += wpabuf_len(hapd->p2p_probe_resp_ie);
385 #endif /* CONFIG_P2P */
386 #ifdef CONFIG_FST
387         if (hapd->iface->fst_ies)
388                 buflen += wpabuf_len(hapd->iface->fst_ies);
389 #endif /* CONFIG_FST */
390         if (hapd->conf->vendor_elements)
391                 buflen += wpabuf_len(hapd->conf->vendor_elements);
392         if (hapd->conf->vendor_vht) {
393                 buflen += 5 + 2 + sizeof(struct ieee80211_vht_capabilities) +
394                         2 + sizeof(struct ieee80211_vht_operation);
395         }
396         resp = os_zalloc(buflen);
397         if (resp == NULL)
398                 return NULL;
399
400         epos = ((u8 *) resp) + MAX_PROBERESP_LEN;
401
402         resp->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
403                                            WLAN_FC_STYPE_PROBE_RESP);
404         if (req)
405                 os_memcpy(resp->da, req->sa, ETH_ALEN);
406         os_memcpy(resp->sa, hapd->own_addr, ETH_ALEN);
407
408         os_memcpy(resp->bssid, hapd->own_addr, ETH_ALEN);
409         resp->u.probe_resp.beacon_int =
410                 host_to_le16(hapd->iconf->beacon_int);
411
412         /* hardware or low-level driver will setup seq_ctrl and timestamp */
413         resp->u.probe_resp.capab_info =
414                 host_to_le16(hostapd_own_capab_info(hapd));
415
416         pos = resp->u.probe_resp.variable;
417         *pos++ = WLAN_EID_SSID;
418         *pos++ = hapd->conf->ssid.ssid_len;
419         os_memcpy(pos, hapd->conf->ssid.ssid, hapd->conf->ssid.ssid_len);
420         pos += hapd->conf->ssid.ssid_len;
421
422         /* Supported rates */
423         pos = hostapd_eid_supp_rates(hapd, pos);
424
425         /* DS Params */
426         pos = hostapd_eid_ds_params(hapd, pos);
427
428         pos = hostapd_eid_country(hapd, pos, epos - pos);
429
430         /* Power Constraint element */
431         pos = hostapd_eid_pwr_constraint(hapd, pos);
432
433         /* ERP Information element */
434         pos = hostapd_eid_erp_info(hapd, pos);
435
436         /* Extended supported rates */
437         pos = hostapd_eid_ext_supp_rates(hapd, pos);
438
439         /* RSN, MDIE, WPA */
440         pos = hostapd_eid_wpa(hapd, pos, epos - pos);
441
442         pos = hostapd_eid_bss_load(hapd, pos, epos - pos);
443
444         pos = hostapd_eid_rm_enabled_capab(hapd, pos, epos - pos);
445
446 #ifdef CONFIG_IEEE80211N
447         pos = hostapd_eid_ht_capabilities(hapd, pos);
448         pos = hostapd_eid_ht_operation(hapd, pos);
449 #endif /* CONFIG_IEEE80211N */
450
451         pos = hostapd_eid_ext_capab(hapd, pos);
452
453         pos = hostapd_eid_time_adv(hapd, pos);
454         pos = hostapd_eid_time_zone(hapd, pos);
455
456         pos = hostapd_eid_interworking(hapd, pos);
457         pos = hostapd_eid_adv_proto(hapd, pos);
458         pos = hostapd_eid_roaming_consortium(hapd, pos);
459
460         pos = hostapd_add_csa_elems(hapd, pos, (u8 *)resp,
461                                     &hapd->cs_c_off_proberesp,
462                                     &hapd->cs_c_off_ecsa_proberesp);
463
464 #ifdef CONFIG_FST
465         if (hapd->iface->fst_ies) {
466                 os_memcpy(pos, wpabuf_head(hapd->iface->fst_ies),
467                           wpabuf_len(hapd->iface->fst_ies));
468                 pos += wpabuf_len(hapd->iface->fst_ies);
469         }
470 #endif /* CONFIG_FST */
471
472 #ifdef CONFIG_IEEE80211AC
473         if (hapd->iconf->ieee80211ac && !hapd->conf->disable_11ac) {
474                 pos = hostapd_eid_vht_capabilities(hapd, pos);
475                 pos = hostapd_eid_vht_operation(hapd, pos);
476         }
477         if (hapd->conf->vendor_vht)
478                 pos = hostapd_eid_vendor_vht(hapd, pos);
479 #endif /* CONFIG_IEEE80211AC */
480
481         /* Wi-Fi Alliance WMM */
482         pos = hostapd_eid_wmm(hapd, pos);
483
484 #ifdef CONFIG_WPS
485         if (hapd->conf->wps_state && hapd->wps_probe_resp_ie) {
486                 os_memcpy(pos, wpabuf_head(hapd->wps_probe_resp_ie),
487                           wpabuf_len(hapd->wps_probe_resp_ie));
488                 pos += wpabuf_len(hapd->wps_probe_resp_ie);
489         }
490 #endif /* CONFIG_WPS */
491
492 #ifdef CONFIG_P2P
493         if ((hapd->conf->p2p & P2P_ENABLED) && is_p2p &&
494             hapd->p2p_probe_resp_ie) {
495                 os_memcpy(pos, wpabuf_head(hapd->p2p_probe_resp_ie),
496                           wpabuf_len(hapd->p2p_probe_resp_ie));
497                 pos += wpabuf_len(hapd->p2p_probe_resp_ie);
498         }
499 #endif /* CONFIG_P2P */
500 #ifdef CONFIG_P2P_MANAGER
501         if ((hapd->conf->p2p & (P2P_MANAGE | P2P_ENABLED | P2P_GROUP_OWNER)) ==
502             P2P_MANAGE)
503                 pos = hostapd_eid_p2p_manage(hapd, pos);
504 #endif /* CONFIG_P2P_MANAGER */
505
506 #ifdef CONFIG_HS20
507         pos = hostapd_eid_hs20_indication(hapd, pos);
508         pos = hostapd_eid_osen(hapd, pos);
509 #endif /* CONFIG_HS20 */
510
511         if (hapd->conf->vendor_elements) {
512                 os_memcpy(pos, wpabuf_head(hapd->conf->vendor_elements),
513                           wpabuf_len(hapd->conf->vendor_elements));
514                 pos += wpabuf_len(hapd->conf->vendor_elements);
515         }
516
517         *resp_len = pos - (u8 *) resp;
518         return (u8 *) resp;
519 }
520
521
522 enum ssid_match_result {
523         NO_SSID_MATCH,
524         EXACT_SSID_MATCH,
525         WILDCARD_SSID_MATCH
526 };
527
528 static enum ssid_match_result ssid_match(struct hostapd_data *hapd,
529                                          const u8 *ssid, size_t ssid_len,
530                                          const u8 *ssid_list,
531                                          size_t ssid_list_len)
532 {
533         const u8 *pos, *end;
534         int wildcard = 0;
535
536         if (ssid_len == 0)
537                 wildcard = 1;
538         if (ssid_len == hapd->conf->ssid.ssid_len &&
539             os_memcmp(ssid, hapd->conf->ssid.ssid, ssid_len) == 0)
540                 return EXACT_SSID_MATCH;
541
542         if (ssid_list == NULL)
543                 return wildcard ? WILDCARD_SSID_MATCH : NO_SSID_MATCH;
544
545         pos = ssid_list;
546         end = ssid_list + ssid_list_len;
547         while (pos + 1 <= end) {
548                 if (pos + 2 + pos[1] > end)
549                         break;
550                 if (pos[1] == 0)
551                         wildcard = 1;
552                 if (pos[1] == hapd->conf->ssid.ssid_len &&
553                     os_memcmp(pos + 2, hapd->conf->ssid.ssid, pos[1]) == 0)
554                         return EXACT_SSID_MATCH;
555                 pos += 2 + pos[1];
556         }
557
558         return wildcard ? WILDCARD_SSID_MATCH : NO_SSID_MATCH;
559 }
560
561
562 void sta_track_expire(struct hostapd_iface *iface, int force)
563 {
564         struct os_reltime now;
565         struct hostapd_sta_info *info;
566
567         if (!iface->num_sta_seen)
568                 return;
569
570         os_get_reltime(&now);
571         while ((info = dl_list_first(&iface->sta_seen, struct hostapd_sta_info,
572                                      list))) {
573                 if (!force &&
574                     !os_reltime_expired(&now, &info->last_seen,
575                                         iface->conf->track_sta_max_age))
576                         break;
577                 force = 0;
578
579                 wpa_printf(MSG_MSGDUMP, "%s: Expire STA tracking entry for "
580                            MACSTR, iface->bss[0]->conf->iface,
581                            MAC2STR(info->addr));
582                 dl_list_del(&info->list);
583                 iface->num_sta_seen--;
584                 os_free(info);
585         }
586 }
587
588
589 static struct hostapd_sta_info * sta_track_get(struct hostapd_iface *iface,
590                                                const u8 *addr)
591 {
592         struct hostapd_sta_info *info;
593
594         dl_list_for_each(info, &iface->sta_seen, struct hostapd_sta_info, list)
595                 if (os_memcmp(addr, info->addr, ETH_ALEN) == 0)
596                         return info;
597
598         return NULL;
599 }
600
601
602 void sta_track_add(struct hostapd_iface *iface, const u8 *addr)
603 {
604         struct hostapd_sta_info *info;
605
606         info = sta_track_get(iface, addr);
607         if (info) {
608                 /* Move the most recent entry to the end of the list */
609                 dl_list_del(&info->list);
610                 dl_list_add_tail(&iface->sta_seen, &info->list);
611                 os_get_reltime(&info->last_seen);
612                 return;
613         }
614
615         /* Add a new entry */
616         info = os_zalloc(sizeof(*info));
617         os_memcpy(info->addr, addr, ETH_ALEN);
618         os_get_reltime(&info->last_seen);
619
620         if (iface->num_sta_seen >= iface->conf->track_sta_max_num) {
621                 /* Expire oldest entry to make room for a new one */
622                 sta_track_expire(iface, 1);
623         }
624
625         wpa_printf(MSG_MSGDUMP, "%s: Add STA tracking entry for "
626                    MACSTR, iface->bss[0]->conf->iface, MAC2STR(addr));
627         dl_list_add_tail(&iface->sta_seen, &info->list);
628         iface->num_sta_seen++;
629 }
630
631
632 struct hostapd_data *
633 sta_track_seen_on(struct hostapd_iface *iface, const u8 *addr,
634                   const char *ifname)
635 {
636         struct hapd_interfaces *interfaces = iface->interfaces;
637         size_t i, j;
638
639         for (i = 0; i < interfaces->count; i++) {
640                 struct hostapd_data *hapd = NULL;
641
642                 iface = interfaces->iface[i];
643                 for (j = 0; j < iface->num_bss; j++) {
644                         hapd = iface->bss[j];
645                         if (os_strcmp(ifname, hapd->conf->iface) == 0)
646                                 break;
647                         hapd = NULL;
648                 }
649
650                 if (hapd && sta_track_get(iface, addr))
651                         return hapd;
652         }
653
654         return NULL;
655 }
656
657
658 void handle_probe_req(struct hostapd_data *hapd,
659                       const struct ieee80211_mgmt *mgmt, size_t len,
660                       int ssi_signal)
661 {
662         u8 *resp;
663         struct ieee802_11_elems elems;
664         const u8 *ie;
665         size_t ie_len;
666         size_t i, resp_len;
667         int noack;
668         enum ssid_match_result res;
669
670         ie = mgmt->u.probe_req.variable;
671         if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.probe_req))
672                 return;
673         if (hapd->iconf->track_sta_max_num)
674                 sta_track_add(hapd->iface, mgmt->sa);
675         ie_len = len - (IEEE80211_HDRLEN + sizeof(mgmt->u.probe_req));
676
677         for (i = 0; hapd->probereq_cb && i < hapd->num_probereq_cb; i++)
678                 if (hapd->probereq_cb[i].cb(hapd->probereq_cb[i].ctx,
679                                             mgmt->sa, mgmt->da, mgmt->bssid,
680                                             ie, ie_len, ssi_signal) > 0)
681                         return;
682
683         if (!hapd->iconf->send_probe_response)
684                 return;
685
686         if (ieee802_11_parse_elems(ie, ie_len, &elems, 0) == ParseFailed) {
687                 wpa_printf(MSG_DEBUG, "Could not parse ProbeReq from " MACSTR,
688                            MAC2STR(mgmt->sa));
689                 return;
690         }
691
692         if ((!elems.ssid || !elems.supp_rates)) {
693                 wpa_printf(MSG_DEBUG, "STA " MACSTR " sent probe request "
694                            "without SSID or supported rates element",
695                            MAC2STR(mgmt->sa));
696                 return;
697         }
698
699         /*
700          * No need to reply if the Probe Request frame was sent on an adjacent
701          * channel. IEEE Std 802.11-2012 describes this as a requirement for an
702          * AP with dot11RadioMeasurementActivated set to true, but strictly
703          * speaking does not allow such ignoring of Probe Request frames if
704          * dot11RadioMeasurementActivated is false. Anyway, this can help reduce
705          * number of unnecessary Probe Response frames for cases where the STA
706          * is less likely to see them (Probe Request frame sent on a
707          * neighboring, but partially overlapping, channel).
708          */
709         if (elems.ds_params &&
710             hapd->iface->current_mode &&
711             (hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G ||
712              hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211B) &&
713             hapd->iconf->channel != elems.ds_params[0]) {
714                 wpa_printf(MSG_DEBUG,
715                            "Ignore Probe Request due to DS Params mismatch: chan=%u != ds.chan=%u",
716                            hapd->iconf->channel, elems.ds_params[0]);
717                 return;
718         }
719
720 #ifdef CONFIG_P2P
721         if (hapd->p2p && elems.wps_ie) {
722                 struct wpabuf *wps;
723                 wps = ieee802_11_vendor_ie_concat(ie, ie_len, WPS_DEV_OUI_WFA);
724                 if (wps && !p2p_group_match_dev_type(hapd->p2p_group, wps)) {
725                         wpa_printf(MSG_MSGDUMP, "P2P: Ignore Probe Request "
726                                    "due to mismatch with Requested Device "
727                                    "Type");
728                         wpabuf_free(wps);
729                         return;
730                 }
731                 wpabuf_free(wps);
732         }
733
734         if (hapd->p2p && elems.p2p) {
735                 struct wpabuf *p2p;
736                 p2p = ieee802_11_vendor_ie_concat(ie, ie_len, P2P_IE_VENDOR_TYPE);
737                 if (p2p && !p2p_group_match_dev_id(hapd->p2p_group, p2p)) {
738                         wpa_printf(MSG_MSGDUMP, "P2P: Ignore Probe Request "
739                                    "due to mismatch with Device ID");
740                         wpabuf_free(p2p);
741                         return;
742                 }
743                 wpabuf_free(p2p);
744         }
745 #endif /* CONFIG_P2P */
746
747         if (hapd->conf->ignore_broadcast_ssid && elems.ssid_len == 0 &&
748             elems.ssid_list_len == 0) {
749                 wpa_printf(MSG_MSGDUMP, "Probe Request from " MACSTR " for "
750                            "broadcast SSID ignored", MAC2STR(mgmt->sa));
751                 return;
752         }
753
754 #ifdef CONFIG_P2P
755         if ((hapd->conf->p2p & P2P_GROUP_OWNER) &&
756             elems.ssid_len == P2P_WILDCARD_SSID_LEN &&
757             os_memcmp(elems.ssid, P2P_WILDCARD_SSID,
758                       P2P_WILDCARD_SSID_LEN) == 0) {
759                 /* Process P2P Wildcard SSID like Wildcard SSID */
760                 elems.ssid_len = 0;
761         }
762 #endif /* CONFIG_P2P */
763
764         res = ssid_match(hapd, elems.ssid, elems.ssid_len,
765                          elems.ssid_list, elems.ssid_list_len);
766         if (res == NO_SSID_MATCH) {
767                 if (!(mgmt->da[0] & 0x01)) {
768                         wpa_printf(MSG_MSGDUMP, "Probe Request from " MACSTR
769                                    " for foreign SSID '%s' (DA " MACSTR ")%s",
770                                    MAC2STR(mgmt->sa),
771                                    wpa_ssid_txt(elems.ssid, elems.ssid_len),
772                                    MAC2STR(mgmt->da),
773                                    elems.ssid_list ? " (SSID list)" : "");
774                 }
775                 return;
776         }
777
778 #ifdef CONFIG_INTERWORKING
779         if (hapd->conf->interworking &&
780             elems.interworking && elems.interworking_len >= 1) {
781                 u8 ant = elems.interworking[0] & 0x0f;
782                 if (ant != INTERWORKING_ANT_WILDCARD &&
783                     ant != hapd->conf->access_network_type) {
784                         wpa_printf(MSG_MSGDUMP, "Probe Request from " MACSTR
785                                    " for mismatching ANT %u ignored",
786                                    MAC2STR(mgmt->sa), ant);
787                         return;
788                 }
789         }
790
791         if (hapd->conf->interworking && elems.interworking &&
792             (elems.interworking_len == 7 || elems.interworking_len == 9)) {
793                 const u8 *hessid;
794                 if (elems.interworking_len == 7)
795                         hessid = elems.interworking + 1;
796                 else
797                         hessid = elems.interworking + 1 + 2;
798                 if (!is_broadcast_ether_addr(hessid) &&
799                     os_memcmp(hessid, hapd->conf->hessid, ETH_ALEN) != 0) {
800                         wpa_printf(MSG_MSGDUMP, "Probe Request from " MACSTR
801                                    " for mismatching HESSID " MACSTR
802                                    " ignored",
803                                    MAC2STR(mgmt->sa), MAC2STR(hessid));
804                         return;
805                 }
806         }
807 #endif /* CONFIG_INTERWORKING */
808
809 #ifdef CONFIG_P2P
810         if ((hapd->conf->p2p & P2P_GROUP_OWNER) &&
811             supp_rates_11b_only(&elems)) {
812                 /* Indicates support for 11b rates only */
813                 wpa_printf(MSG_EXCESSIVE, "P2P: Ignore Probe Request from "
814                            MACSTR " with only 802.11b rates",
815                            MAC2STR(mgmt->sa));
816                 return;
817         }
818 #endif /* CONFIG_P2P */
819
820         /* TODO: verify that supp_rates contains at least one matching rate
821          * with AP configuration */
822
823         if (hapd->conf->no_probe_resp_if_seen_on &&
824             is_multicast_ether_addr(mgmt->da) &&
825             is_multicast_ether_addr(mgmt->bssid) &&
826             sta_track_seen_on(hapd->iface, mgmt->sa,
827                               hapd->conf->no_probe_resp_if_seen_on)) {
828                 wpa_printf(MSG_MSGDUMP, "%s: Ignore Probe Request from " MACSTR
829                            " since STA has been seen on %s",
830                            hapd->conf->iface, MAC2STR(mgmt->sa),
831                            hapd->conf->no_probe_resp_if_seen_on);
832                 return;
833         }
834
835 #ifdef CONFIG_TESTING_OPTIONS
836         if (hapd->iconf->ignore_probe_probability > 0.0 &&
837             drand48() < hapd->iconf->ignore_probe_probability) {
838                 wpa_printf(MSG_INFO,
839                            "TESTING: ignoring probe request from " MACSTR,
840                            MAC2STR(mgmt->sa));
841                 return;
842         }
843 #endif /* CONFIG_TESTING_OPTIONS */
844
845         resp = hostapd_gen_probe_resp(hapd, mgmt, elems.p2p != NULL,
846                                       &resp_len);
847         if (resp == NULL)
848                 return;
849
850         /*
851          * If this is a broadcast probe request, apply no ack policy to avoid
852          * excessive retries.
853          */
854         noack = !!(res == WILDCARD_SSID_MATCH &&
855                    is_broadcast_ether_addr(mgmt->da));
856
857         if (hostapd_drv_send_mlme(hapd, resp, resp_len, noack) < 0)
858                 wpa_printf(MSG_INFO, "handle_probe_req: send failed");
859
860         os_free(resp);
861
862         wpa_printf(MSG_EXCESSIVE, "STA " MACSTR " sent probe request for %s "
863                    "SSID", MAC2STR(mgmt->sa),
864                    elems.ssid_len == 0 ? "broadcast" : "our");
865 }
866
867
868 static u8 * hostapd_probe_resp_offloads(struct hostapd_data *hapd,
869                                         size_t *resp_len)
870 {
871         /* check probe response offloading caps and print warnings */
872         if (!(hapd->iface->drv_flags & WPA_DRIVER_FLAGS_PROBE_RESP_OFFLOAD))
873                 return NULL;
874
875 #ifdef CONFIG_WPS
876         if (hapd->conf->wps_state && hapd->wps_probe_resp_ie &&
877             (!(hapd->iface->probe_resp_offloads &
878                (WPA_DRIVER_PROBE_RESP_OFFLOAD_WPS |
879                 WPA_DRIVER_PROBE_RESP_OFFLOAD_WPS2))))
880                 wpa_printf(MSG_WARNING, "Device is trying to offload WPS "
881                            "Probe Response while not supporting this");
882 #endif /* CONFIG_WPS */
883
884 #ifdef CONFIG_P2P
885         if ((hapd->conf->p2p & P2P_ENABLED) && hapd->p2p_probe_resp_ie &&
886             !(hapd->iface->probe_resp_offloads &
887               WPA_DRIVER_PROBE_RESP_OFFLOAD_P2P))
888                 wpa_printf(MSG_WARNING, "Device is trying to offload P2P "
889                            "Probe Response while not supporting this");
890 #endif  /* CONFIG_P2P */
891
892         if (hapd->conf->interworking &&
893             !(hapd->iface->probe_resp_offloads &
894               WPA_DRIVER_PROBE_RESP_OFFLOAD_INTERWORKING))
895                 wpa_printf(MSG_WARNING, "Device is trying to offload "
896                            "Interworking Probe Response while not supporting "
897                            "this");
898
899         /* Generate a Probe Response template for the non-P2P case */
900         return hostapd_gen_probe_resp(hapd, NULL, 0, resp_len);
901 }
902
903 #endif /* NEED_AP_MLME */
904
905
906 int ieee802_11_build_ap_params(struct hostapd_data *hapd,
907                                struct wpa_driver_ap_params *params)
908 {
909         struct ieee80211_mgmt *head = NULL;
910         u8 *tail = NULL;
911         size_t head_len = 0, tail_len = 0;
912         u8 *resp = NULL;
913         size_t resp_len = 0;
914 #ifdef NEED_AP_MLME
915         u16 capab_info;
916         u8 *pos, *tailpos;
917
918 #define BEACON_HEAD_BUF_SIZE 256
919 #define BEACON_TAIL_BUF_SIZE 512
920         head = os_zalloc(BEACON_HEAD_BUF_SIZE);
921         tail_len = BEACON_TAIL_BUF_SIZE;
922 #ifdef CONFIG_WPS
923         if (hapd->conf->wps_state && hapd->wps_beacon_ie)
924                 tail_len += wpabuf_len(hapd->wps_beacon_ie);
925 #endif /* CONFIG_WPS */
926 #ifdef CONFIG_P2P
927         if (hapd->p2p_beacon_ie)
928                 tail_len += wpabuf_len(hapd->p2p_beacon_ie);
929 #endif /* CONFIG_P2P */
930 #ifdef CONFIG_FST
931         if (hapd->iface->fst_ies)
932                 tail_len += wpabuf_len(hapd->iface->fst_ies);
933 #endif /* CONFIG_FST */
934         if (hapd->conf->vendor_elements)
935                 tail_len += wpabuf_len(hapd->conf->vendor_elements);
936
937 #ifdef CONFIG_IEEE80211AC
938         if (hapd->conf->vendor_vht) {
939                 tail_len += 5 + 2 + sizeof(struct ieee80211_vht_capabilities) +
940                         2 + sizeof(struct ieee80211_vht_operation);
941         }
942 #endif /* CONFIG_IEEE80211AC */
943
944         tailpos = tail = os_malloc(tail_len);
945         if (head == NULL || tail == NULL) {
946                 wpa_printf(MSG_ERROR, "Failed to set beacon data");
947                 os_free(head);
948                 os_free(tail);
949                 return -1;
950         }
951
952         head->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
953                                            WLAN_FC_STYPE_BEACON);
954         head->duration = host_to_le16(0);
955         os_memset(head->da, 0xff, ETH_ALEN);
956
957         os_memcpy(head->sa, hapd->own_addr, ETH_ALEN);
958         os_memcpy(head->bssid, hapd->own_addr, ETH_ALEN);
959         head->u.beacon.beacon_int =
960                 host_to_le16(hapd->iconf->beacon_int);
961
962         /* hardware or low-level driver will setup seq_ctrl and timestamp */
963         capab_info = hostapd_own_capab_info(hapd);
964         head->u.beacon.capab_info = host_to_le16(capab_info);
965         pos = &head->u.beacon.variable[0];
966
967         /* SSID */
968         *pos++ = WLAN_EID_SSID;
969         if (hapd->conf->ignore_broadcast_ssid == 2) {
970                 /* clear the data, but keep the correct length of the SSID */
971                 *pos++ = hapd->conf->ssid.ssid_len;
972                 os_memset(pos, 0, hapd->conf->ssid.ssid_len);
973                 pos += hapd->conf->ssid.ssid_len;
974         } else if (hapd->conf->ignore_broadcast_ssid) {
975                 *pos++ = 0; /* empty SSID */
976         } else {
977                 *pos++ = hapd->conf->ssid.ssid_len;
978                 os_memcpy(pos, hapd->conf->ssid.ssid,
979                           hapd->conf->ssid.ssid_len);
980                 pos += hapd->conf->ssid.ssid_len;
981         }
982
983         /* Supported rates */
984         pos = hostapd_eid_supp_rates(hapd, pos);
985
986         /* DS Params */
987         pos = hostapd_eid_ds_params(hapd, pos);
988
989         head_len = pos - (u8 *) head;
990
991         tailpos = hostapd_eid_country(hapd, tailpos,
992                                       tail + BEACON_TAIL_BUF_SIZE - tailpos);
993
994         /* Power Constraint element */
995         tailpos = hostapd_eid_pwr_constraint(hapd, tailpos);
996
997         /* ERP Information element */
998         tailpos = hostapd_eid_erp_info(hapd, tailpos);
999
1000         /* Extended supported rates */
1001         tailpos = hostapd_eid_ext_supp_rates(hapd, tailpos);
1002
1003         /* RSN, MDIE, WPA */
1004         tailpos = hostapd_eid_wpa(hapd, tailpos, tail + BEACON_TAIL_BUF_SIZE -
1005                                   tailpos);
1006
1007         tailpos = hostapd_eid_rm_enabled_capab(hapd, tailpos,
1008                                                tail + BEACON_TAIL_BUF_SIZE -
1009                                                tailpos);
1010
1011         tailpos = hostapd_eid_bss_load(hapd, tailpos,
1012                                        tail + BEACON_TAIL_BUF_SIZE - tailpos);
1013
1014 #ifdef CONFIG_IEEE80211N
1015         tailpos = hostapd_eid_ht_capabilities(hapd, tailpos);
1016         tailpos = hostapd_eid_ht_operation(hapd, tailpos);
1017 #endif /* CONFIG_IEEE80211N */
1018
1019         tailpos = hostapd_eid_ext_capab(hapd, tailpos);
1020
1021         /*
1022          * TODO: Time Advertisement element should only be included in some
1023          * DTIM Beacon frames.
1024          */
1025         tailpos = hostapd_eid_time_adv(hapd, tailpos);
1026
1027         tailpos = hostapd_eid_interworking(hapd, tailpos);
1028         tailpos = hostapd_eid_adv_proto(hapd, tailpos);
1029         tailpos = hostapd_eid_roaming_consortium(hapd, tailpos);
1030         tailpos = hostapd_add_csa_elems(hapd, tailpos, tail,
1031                                         &hapd->cs_c_off_beacon,
1032                                         &hapd->cs_c_off_ecsa_beacon);
1033
1034 #ifdef CONFIG_FST
1035         if (hapd->iface->fst_ies) {
1036                 os_memcpy(tailpos, wpabuf_head(hapd->iface->fst_ies),
1037                           wpabuf_len(hapd->iface->fst_ies));
1038                 tailpos += wpabuf_len(hapd->iface->fst_ies);
1039         }
1040 #endif /* CONFIG_FST */
1041
1042 #ifdef CONFIG_IEEE80211AC
1043         if (hapd->iconf->ieee80211ac && !hapd->conf->disable_11ac) {
1044                 tailpos = hostapd_eid_vht_capabilities(hapd, tailpos);
1045                 tailpos = hostapd_eid_vht_operation(hapd, tailpos);
1046         }
1047         if (hapd->conf->vendor_vht)
1048                 tailpos = hostapd_eid_vendor_vht(hapd, tailpos);
1049 #endif /* CONFIG_IEEE80211AC */
1050
1051         /* Wi-Fi Alliance WMM */
1052         tailpos = hostapd_eid_wmm(hapd, tailpos);
1053
1054 #ifdef CONFIG_WPS
1055         if (hapd->conf->wps_state && hapd->wps_beacon_ie) {
1056                 os_memcpy(tailpos, wpabuf_head(hapd->wps_beacon_ie),
1057                           wpabuf_len(hapd->wps_beacon_ie));
1058                 tailpos += wpabuf_len(hapd->wps_beacon_ie);
1059         }
1060 #endif /* CONFIG_WPS */
1061
1062 #ifdef CONFIG_P2P
1063         if ((hapd->conf->p2p & P2P_ENABLED) && hapd->p2p_beacon_ie) {
1064                 os_memcpy(tailpos, wpabuf_head(hapd->p2p_beacon_ie),
1065                           wpabuf_len(hapd->p2p_beacon_ie));
1066                 tailpos += wpabuf_len(hapd->p2p_beacon_ie);
1067         }
1068 #endif /* CONFIG_P2P */
1069 #ifdef CONFIG_P2P_MANAGER
1070         if ((hapd->conf->p2p & (P2P_MANAGE | P2P_ENABLED | P2P_GROUP_OWNER)) ==
1071             P2P_MANAGE)
1072                 tailpos = hostapd_eid_p2p_manage(hapd, tailpos);
1073 #endif /* CONFIG_P2P_MANAGER */
1074
1075 #ifdef CONFIG_HS20
1076         tailpos = hostapd_eid_hs20_indication(hapd, tailpos);
1077         tailpos = hostapd_eid_osen(hapd, tailpos);
1078 #endif /* CONFIG_HS20 */
1079
1080         if (hapd->conf->vendor_elements) {
1081                 os_memcpy(tailpos, wpabuf_head(hapd->conf->vendor_elements),
1082                           wpabuf_len(hapd->conf->vendor_elements));
1083                 tailpos += wpabuf_len(hapd->conf->vendor_elements);
1084         }
1085
1086         tail_len = tailpos > tail ? tailpos - tail : 0;
1087
1088         resp = hostapd_probe_resp_offloads(hapd, &resp_len);
1089 #endif /* NEED_AP_MLME */
1090
1091         os_memset(params, 0, sizeof(*params));
1092         params->head = (u8 *) head;
1093         params->head_len = head_len;
1094         params->tail = tail;
1095         params->tail_len = tail_len;
1096         params->proberesp = resp;
1097         params->proberesp_len = resp_len;
1098         params->dtim_period = hapd->conf->dtim_period;
1099         params->beacon_int = hapd->iconf->beacon_int;
1100         params->basic_rates = hapd->iface->basic_rates;
1101         params->ssid = hapd->conf->ssid.ssid;
1102         params->ssid_len = hapd->conf->ssid.ssid_len;
1103         if ((hapd->conf->wpa & (WPA_PROTO_WPA | WPA_PROTO_RSN)) ==
1104             (WPA_PROTO_WPA | WPA_PROTO_RSN))
1105                 params->pairwise_ciphers = hapd->conf->wpa_pairwise |
1106                         hapd->conf->rsn_pairwise;
1107         else if (hapd->conf->wpa & WPA_PROTO_RSN)
1108                 params->pairwise_ciphers = hapd->conf->rsn_pairwise;
1109         else if (hapd->conf->wpa & WPA_PROTO_WPA)
1110                 params->pairwise_ciphers = hapd->conf->wpa_pairwise;
1111         params->group_cipher = hapd->conf->wpa_group;
1112         params->key_mgmt_suites = hapd->conf->wpa_key_mgmt;
1113         params->auth_algs = hapd->conf->auth_algs;
1114         params->wpa_version = hapd->conf->wpa;
1115         params->privacy = hapd->conf->ssid.wep.keys_set || hapd->conf->wpa ||
1116                 (hapd->conf->ieee802_1x &&
1117                  (hapd->conf->default_wep_key_len ||
1118                   hapd->conf->individual_wep_key_len));
1119         switch (hapd->conf->ignore_broadcast_ssid) {
1120         case 0:
1121                 params->hide_ssid = NO_SSID_HIDING;
1122                 break;
1123         case 1:
1124                 params->hide_ssid = HIDDEN_SSID_ZERO_LEN;
1125                 break;
1126         case 2:
1127                 params->hide_ssid = HIDDEN_SSID_ZERO_CONTENTS;
1128                 break;
1129         }
1130         params->isolate = hapd->conf->isolate;
1131         params->smps_mode = hapd->iconf->ht_capab & HT_CAP_INFO_SMPS_MASK;
1132 #ifdef NEED_AP_MLME
1133         params->cts_protect = !!(ieee802_11_erp_info(hapd) &
1134                                 ERP_INFO_USE_PROTECTION);
1135         params->preamble = hapd->iface->num_sta_no_short_preamble == 0 &&
1136                 hapd->iconf->preamble == SHORT_PREAMBLE;
1137         if (hapd->iface->current_mode &&
1138             hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G)
1139                 params->short_slot_time =
1140                         hapd->iface->num_sta_no_short_slot_time > 0 ? 0 : 1;
1141         else
1142                 params->short_slot_time = -1;
1143         if (!hapd->iconf->ieee80211n || hapd->conf->disable_11n)
1144                 params->ht_opmode = -1;
1145         else
1146                 params->ht_opmode = hapd->iface->ht_op_mode;
1147 #endif /* NEED_AP_MLME */
1148         params->interworking = hapd->conf->interworking;
1149         if (hapd->conf->interworking &&
1150             !is_zero_ether_addr(hapd->conf->hessid))
1151                 params->hessid = hapd->conf->hessid;
1152         params->access_network_type = hapd->conf->access_network_type;
1153         params->ap_max_inactivity = hapd->conf->ap_max_inactivity;
1154 #ifdef CONFIG_P2P
1155         params->p2p_go_ctwindow = hapd->iconf->p2p_go_ctwindow;
1156 #endif /* CONFIG_P2P */
1157 #ifdef CONFIG_HS20
1158         params->disable_dgaf = hapd->conf->disable_dgaf;
1159         if (hapd->conf->osen) {
1160                 params->privacy = 1;
1161                 params->osen = 1;
1162         }
1163 #endif /* CONFIG_HS20 */
1164         return 0;
1165 }
1166
1167
1168 void ieee802_11_free_ap_params(struct wpa_driver_ap_params *params)
1169 {
1170         os_free(params->tail);
1171         params->tail = NULL;
1172         os_free(params->head);
1173         params->head = NULL;
1174         os_free(params->proberesp);
1175         params->proberesp = NULL;
1176 }
1177
1178
1179 int ieee802_11_set_beacon(struct hostapd_data *hapd)
1180 {
1181         struct wpa_driver_ap_params params;
1182         struct hostapd_freq_params freq;
1183         struct hostapd_iface *iface = hapd->iface;
1184         struct hostapd_config *iconf = iface->conf;
1185         struct wpabuf *beacon, *proberesp, *assocresp;
1186         int res, ret = -1;
1187
1188         if (hapd->csa_in_progress) {
1189                 wpa_printf(MSG_ERROR, "Cannot set beacons during CSA period");
1190                 return -1;
1191         }
1192
1193         hapd->beacon_set_done = 1;
1194
1195         if (ieee802_11_build_ap_params(hapd, &params) < 0)
1196                 return -1;
1197
1198         if (hostapd_build_ap_extra_ies(hapd, &beacon, &proberesp, &assocresp) <
1199             0)
1200                 goto fail;
1201
1202         params.beacon_ies = beacon;
1203         params.proberesp_ies = proberesp;
1204         params.assocresp_ies = assocresp;
1205         params.reenable = hapd->reenable_beacon;
1206         hapd->reenable_beacon = 0;
1207
1208         if (iface->current_mode &&
1209             hostapd_set_freq_params(&freq, iconf->hw_mode, iface->freq,
1210                                     iconf->channel, iconf->ieee80211n,
1211                                     iconf->ieee80211ac,
1212                                     iconf->secondary_channel,
1213                                     iconf->vht_oper_chwidth,
1214                                     iconf->vht_oper_centr_freq_seg0_idx,
1215                                     iconf->vht_oper_centr_freq_seg1_idx,
1216                                     iface->current_mode->vht_capab) == 0)
1217                 params.freq = &freq;
1218
1219         res = hostapd_drv_set_ap(hapd, &params);
1220         hostapd_free_ap_extra_ies(hapd, beacon, proberesp, assocresp);
1221         if (res)
1222                 wpa_printf(MSG_ERROR, "Failed to set beacon parameters");
1223         else
1224                 ret = 0;
1225 fail:
1226         ieee802_11_free_ap_params(&params);
1227         return ret;
1228 }
1229
1230
1231 int ieee802_11_set_beacons(struct hostapd_iface *iface)
1232 {
1233         size_t i;
1234         int ret = 0;
1235
1236         for (i = 0; i < iface->num_bss; i++) {
1237                 if (iface->bss[i]->started &&
1238                     ieee802_11_set_beacon(iface->bss[i]) < 0)
1239                         ret = -1;
1240         }
1241
1242         return ret;
1243 }
1244
1245
1246 /* only update beacons if started */
1247 int ieee802_11_update_beacons(struct hostapd_iface *iface)
1248 {
1249         size_t i;
1250         int ret = 0;
1251
1252         for (i = 0; i < iface->num_bss; i++) {
1253                 if (iface->bss[i]->beacon_set_done && iface->bss[i]->started &&
1254                     ieee802_11_set_beacon(iface->bss[i]) < 0)
1255                         ret = -1;
1256         }
1257
1258         return ret;
1259 }
1260
1261 #endif /* CONFIG_NATIVE_WINDOWS */