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