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