Handle NULL return from os_zalloc() in sta_track_add()
[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         if (info == NULL)
636                 return;
637         os_memcpy(info->addr, addr, ETH_ALEN);
638         os_get_reltime(&info->last_seen);
639
640         if (iface->num_sta_seen >= iface->conf->track_sta_max_num) {
641                 /* Expire oldest entry to make room for a new one */
642                 sta_track_expire(iface, 1);
643         }
644
645         wpa_printf(MSG_MSGDUMP, "%s: Add STA tracking entry for "
646                    MACSTR, iface->bss[0]->conf->iface, MAC2STR(addr));
647         dl_list_add_tail(&iface->sta_seen, &info->list);
648         iface->num_sta_seen++;
649 }
650
651
652 struct hostapd_data *
653 sta_track_seen_on(struct hostapd_iface *iface, const u8 *addr,
654                   const char *ifname)
655 {
656         struct hapd_interfaces *interfaces = iface->interfaces;
657         size_t i, j;
658
659         for (i = 0; i < interfaces->count; i++) {
660                 struct hostapd_data *hapd = NULL;
661
662                 iface = interfaces->iface[i];
663                 for (j = 0; j < iface->num_bss; j++) {
664                         hapd = iface->bss[j];
665                         if (os_strcmp(ifname, hapd->conf->iface) == 0)
666                                 break;
667                         hapd = NULL;
668                 }
669
670                 if (hapd && sta_track_get(iface, addr))
671                         return hapd;
672         }
673
674         return NULL;
675 }
676
677
678 void handle_probe_req(struct hostapd_data *hapd,
679                       const struct ieee80211_mgmt *mgmt, size_t len,
680                       int ssi_signal)
681 {
682         u8 *resp;
683         struct ieee802_11_elems elems;
684         const u8 *ie;
685         size_t ie_len;
686         size_t i, resp_len;
687         int noack;
688         enum ssid_match_result res;
689         int ret;
690         u16 csa_offs[2];
691         size_t csa_offs_len;
692
693         if (len < IEEE80211_HDRLEN)
694                 return;
695         ie = ((const u8 *) mgmt) + IEEE80211_HDRLEN;
696         if (hapd->iconf->track_sta_max_num)
697                 sta_track_add(hapd->iface, mgmt->sa);
698         ie_len = len - IEEE80211_HDRLEN;
699
700         for (i = 0; hapd->probereq_cb && i < hapd->num_probereq_cb; i++)
701                 if (hapd->probereq_cb[i].cb(hapd->probereq_cb[i].ctx,
702                                             mgmt->sa, mgmt->da, mgmt->bssid,
703                                             ie, ie_len, ssi_signal) > 0)
704                         return;
705
706         if (!hapd->iconf->send_probe_response)
707                 return;
708
709         if (ieee802_11_parse_elems(ie, ie_len, &elems, 0) == ParseFailed) {
710                 wpa_printf(MSG_DEBUG, "Could not parse ProbeReq from " MACSTR,
711                            MAC2STR(mgmt->sa));
712                 return;
713         }
714
715         if ((!elems.ssid || !elems.supp_rates)) {
716                 wpa_printf(MSG_DEBUG, "STA " MACSTR " sent probe request "
717                            "without SSID or supported rates element",
718                            MAC2STR(mgmt->sa));
719                 return;
720         }
721
722         /*
723          * No need to reply if the Probe Request frame was sent on an adjacent
724          * channel. IEEE Std 802.11-2012 describes this as a requirement for an
725          * AP with dot11RadioMeasurementActivated set to true, but strictly
726          * speaking does not allow such ignoring of Probe Request frames if
727          * dot11RadioMeasurementActivated is false. Anyway, this can help reduce
728          * number of unnecessary Probe Response frames for cases where the STA
729          * is less likely to see them (Probe Request frame sent on a
730          * neighboring, but partially overlapping, channel).
731          */
732         if (elems.ds_params &&
733             hapd->iface->current_mode &&
734             (hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G ||
735              hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211B) &&
736             hapd->iconf->channel != elems.ds_params[0]) {
737                 wpa_printf(MSG_DEBUG,
738                            "Ignore Probe Request due to DS Params mismatch: chan=%u != ds.chan=%u",
739                            hapd->iconf->channel, elems.ds_params[0]);
740                 return;
741         }
742
743 #ifdef CONFIG_P2P
744         if (hapd->p2p && hapd->p2p_group && elems.wps_ie) {
745                 struct wpabuf *wps;
746                 wps = ieee802_11_vendor_ie_concat(ie, ie_len, WPS_DEV_OUI_WFA);
747                 if (wps && !p2p_group_match_dev_type(hapd->p2p_group, wps)) {
748                         wpa_printf(MSG_MSGDUMP, "P2P: Ignore Probe Request "
749                                    "due to mismatch with Requested Device "
750                                    "Type");
751                         wpabuf_free(wps);
752                         return;
753                 }
754                 wpabuf_free(wps);
755         }
756
757         if (hapd->p2p && hapd->p2p_group && elems.p2p) {
758                 struct wpabuf *p2p;
759                 p2p = ieee802_11_vendor_ie_concat(ie, ie_len, P2P_IE_VENDOR_TYPE);
760                 if (p2p && !p2p_group_match_dev_id(hapd->p2p_group, p2p)) {
761                         wpa_printf(MSG_MSGDUMP, "P2P: Ignore Probe Request "
762                                    "due to mismatch with Device ID");
763                         wpabuf_free(p2p);
764                         return;
765                 }
766                 wpabuf_free(p2p);
767         }
768 #endif /* CONFIG_P2P */
769
770         if (hapd->conf->ignore_broadcast_ssid && elems.ssid_len == 0 &&
771             elems.ssid_list_len == 0) {
772                 wpa_printf(MSG_MSGDUMP, "Probe Request from " MACSTR " for "
773                            "broadcast SSID ignored", MAC2STR(mgmt->sa));
774                 return;
775         }
776
777 #ifdef CONFIG_P2P
778         if ((hapd->conf->p2p & P2P_GROUP_OWNER) &&
779             elems.ssid_len == P2P_WILDCARD_SSID_LEN &&
780             os_memcmp(elems.ssid, P2P_WILDCARD_SSID,
781                       P2P_WILDCARD_SSID_LEN) == 0) {
782                 /* Process P2P Wildcard SSID like Wildcard SSID */
783                 elems.ssid_len = 0;
784         }
785 #endif /* CONFIG_P2P */
786
787         res = ssid_match(hapd, elems.ssid, elems.ssid_len,
788                          elems.ssid_list, elems.ssid_list_len);
789         if (res == NO_SSID_MATCH) {
790                 if (!(mgmt->da[0] & 0x01)) {
791                         wpa_printf(MSG_MSGDUMP, "Probe Request from " MACSTR
792                                    " for foreign SSID '%s' (DA " MACSTR ")%s",
793                                    MAC2STR(mgmt->sa),
794                                    wpa_ssid_txt(elems.ssid, elems.ssid_len),
795                                    MAC2STR(mgmt->da),
796                                    elems.ssid_list ? " (SSID list)" : "");
797                 }
798                 return;
799         }
800
801 #ifdef CONFIG_INTERWORKING
802         if (hapd->conf->interworking &&
803             elems.interworking && elems.interworking_len >= 1) {
804                 u8 ant = elems.interworking[0] & 0x0f;
805                 if (ant != INTERWORKING_ANT_WILDCARD &&
806                     ant != hapd->conf->access_network_type) {
807                         wpa_printf(MSG_MSGDUMP, "Probe Request from " MACSTR
808                                    " for mismatching ANT %u ignored",
809                                    MAC2STR(mgmt->sa), ant);
810                         return;
811                 }
812         }
813
814         if (hapd->conf->interworking && elems.interworking &&
815             (elems.interworking_len == 7 || elems.interworking_len == 9)) {
816                 const u8 *hessid;
817                 if (elems.interworking_len == 7)
818                         hessid = elems.interworking + 1;
819                 else
820                         hessid = elems.interworking + 1 + 2;
821                 if (!is_broadcast_ether_addr(hessid) &&
822                     os_memcmp(hessid, hapd->conf->hessid, ETH_ALEN) != 0) {
823                         wpa_printf(MSG_MSGDUMP, "Probe Request from " MACSTR
824                                    " for mismatching HESSID " MACSTR
825                                    " ignored",
826                                    MAC2STR(mgmt->sa), MAC2STR(hessid));
827                         return;
828                 }
829         }
830 #endif /* CONFIG_INTERWORKING */
831
832 #ifdef CONFIG_P2P
833         if ((hapd->conf->p2p & P2P_GROUP_OWNER) &&
834             supp_rates_11b_only(&elems)) {
835                 /* Indicates support for 11b rates only */
836                 wpa_printf(MSG_EXCESSIVE, "P2P: Ignore Probe Request from "
837                            MACSTR " with only 802.11b rates",
838                            MAC2STR(mgmt->sa));
839                 return;
840         }
841 #endif /* CONFIG_P2P */
842
843         /* TODO: verify that supp_rates contains at least one matching rate
844          * with AP configuration */
845
846         if (hapd->conf->no_probe_resp_if_seen_on &&
847             is_multicast_ether_addr(mgmt->da) &&
848             is_multicast_ether_addr(mgmt->bssid) &&
849             sta_track_seen_on(hapd->iface, mgmt->sa,
850                               hapd->conf->no_probe_resp_if_seen_on)) {
851                 wpa_printf(MSG_MSGDUMP, "%s: Ignore Probe Request from " MACSTR
852                            " since STA has been seen on %s",
853                            hapd->conf->iface, MAC2STR(mgmt->sa),
854                            hapd->conf->no_probe_resp_if_seen_on);
855                 return;
856         }
857
858         if (hapd->conf->no_probe_resp_if_max_sta &&
859             is_multicast_ether_addr(mgmt->da) &&
860             is_multicast_ether_addr(mgmt->bssid) &&
861             hapd->num_sta >= hapd->conf->max_num_sta &&
862             !ap_get_sta(hapd, mgmt->sa)) {
863                 wpa_printf(MSG_MSGDUMP, "%s: Ignore Probe Request from " MACSTR
864                            " since no room for additional STA",
865                            hapd->conf->iface, MAC2STR(mgmt->sa));
866                 return;
867         }
868
869 #ifdef CONFIG_TESTING_OPTIONS
870         if (hapd->iconf->ignore_probe_probability > 0.0 &&
871             drand48() < hapd->iconf->ignore_probe_probability) {
872                 wpa_printf(MSG_INFO,
873                            "TESTING: ignoring probe request from " MACSTR,
874                            MAC2STR(mgmt->sa));
875                 return;
876         }
877 #endif /* CONFIG_TESTING_OPTIONS */
878
879         resp = hostapd_gen_probe_resp(hapd, mgmt, elems.p2p != NULL,
880                                       &resp_len);
881         if (resp == NULL)
882                 return;
883
884         /*
885          * If this is a broadcast probe request, apply no ack policy to avoid
886          * excessive retries.
887          */
888         noack = !!(res == WILDCARD_SSID_MATCH &&
889                    is_broadcast_ether_addr(mgmt->da));
890
891         csa_offs_len = 0;
892         if (hapd->csa_in_progress) {
893                 if (hapd->cs_c_off_proberesp)
894                         csa_offs[csa_offs_len++] =
895                                 hapd->cs_c_off_proberesp;
896
897                 if (hapd->cs_c_off_ecsa_proberesp)
898                         csa_offs[csa_offs_len++] =
899                                 hapd->cs_c_off_ecsa_proberesp;
900         }
901
902         ret = hostapd_drv_send_mlme_csa(hapd, resp, resp_len, noack,
903                                         csa_offs_len ? csa_offs : NULL,
904                                         csa_offs_len);
905
906         if (ret < 0)
907                 wpa_printf(MSG_INFO, "handle_probe_req: send failed");
908
909         os_free(resp);
910
911         wpa_printf(MSG_EXCESSIVE, "STA " MACSTR " sent probe request for %s "
912                    "SSID", MAC2STR(mgmt->sa),
913                    elems.ssid_len == 0 ? "broadcast" : "our");
914 }
915
916
917 static u8 * hostapd_probe_resp_offloads(struct hostapd_data *hapd,
918                                         size_t *resp_len)
919 {
920         /* check probe response offloading caps and print warnings */
921         if (!(hapd->iface->drv_flags & WPA_DRIVER_FLAGS_PROBE_RESP_OFFLOAD))
922                 return NULL;
923
924 #ifdef CONFIG_WPS
925         if (hapd->conf->wps_state && hapd->wps_probe_resp_ie &&
926             (!(hapd->iface->probe_resp_offloads &
927                (WPA_DRIVER_PROBE_RESP_OFFLOAD_WPS |
928                 WPA_DRIVER_PROBE_RESP_OFFLOAD_WPS2))))
929                 wpa_printf(MSG_WARNING, "Device is trying to offload WPS "
930                            "Probe Response while not supporting this");
931 #endif /* CONFIG_WPS */
932
933 #ifdef CONFIG_P2P
934         if ((hapd->conf->p2p & P2P_ENABLED) && hapd->p2p_probe_resp_ie &&
935             !(hapd->iface->probe_resp_offloads &
936               WPA_DRIVER_PROBE_RESP_OFFLOAD_P2P))
937                 wpa_printf(MSG_WARNING, "Device is trying to offload P2P "
938                            "Probe Response while not supporting this");
939 #endif  /* CONFIG_P2P */
940
941         if (hapd->conf->interworking &&
942             !(hapd->iface->probe_resp_offloads &
943               WPA_DRIVER_PROBE_RESP_OFFLOAD_INTERWORKING))
944                 wpa_printf(MSG_WARNING, "Device is trying to offload "
945                            "Interworking Probe Response while not supporting "
946                            "this");
947
948         /* Generate a Probe Response template for the non-P2P case */
949         return hostapd_gen_probe_resp(hapd, NULL, 0, resp_len);
950 }
951
952 #endif /* NEED_AP_MLME */
953
954
955 int ieee802_11_build_ap_params(struct hostapd_data *hapd,
956                                struct wpa_driver_ap_params *params)
957 {
958         struct ieee80211_mgmt *head = NULL;
959         u8 *tail = NULL;
960         size_t head_len = 0, tail_len = 0;
961         u8 *resp = NULL;
962         size_t resp_len = 0;
963 #ifdef NEED_AP_MLME
964         u16 capab_info;
965         u8 *pos, *tailpos, *csa_pos;
966
967 #define BEACON_HEAD_BUF_SIZE 256
968 #define BEACON_TAIL_BUF_SIZE 512
969         head = os_zalloc(BEACON_HEAD_BUF_SIZE);
970         tail_len = BEACON_TAIL_BUF_SIZE;
971 #ifdef CONFIG_WPS
972         if (hapd->conf->wps_state && hapd->wps_beacon_ie)
973                 tail_len += wpabuf_len(hapd->wps_beacon_ie);
974 #endif /* CONFIG_WPS */
975 #ifdef CONFIG_P2P
976         if (hapd->p2p_beacon_ie)
977                 tail_len += wpabuf_len(hapd->p2p_beacon_ie);
978 #endif /* CONFIG_P2P */
979 #ifdef CONFIG_FST
980         if (hapd->iface->fst_ies)
981                 tail_len += wpabuf_len(hapd->iface->fst_ies);
982 #endif /* CONFIG_FST */
983         if (hapd->conf->vendor_elements)
984                 tail_len += wpabuf_len(hapd->conf->vendor_elements);
985
986 #ifdef CONFIG_IEEE80211AC
987         if (hapd->conf->vendor_vht) {
988                 tail_len += 5 + 2 + sizeof(struct ieee80211_vht_capabilities) +
989                         2 + sizeof(struct ieee80211_vht_operation);
990         }
991 #endif /* CONFIG_IEEE80211AC */
992
993         tail_len += hostapd_mbo_ie_len(hapd);
994
995         tailpos = tail = os_malloc(tail_len);
996         if (head == NULL || tail == NULL) {
997                 wpa_printf(MSG_ERROR, "Failed to set beacon data");
998                 os_free(head);
999                 os_free(tail);
1000                 return -1;
1001         }
1002
1003         head->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
1004                                            WLAN_FC_STYPE_BEACON);
1005         head->duration = host_to_le16(0);
1006         os_memset(head->da, 0xff, ETH_ALEN);
1007
1008         os_memcpy(head->sa, hapd->own_addr, ETH_ALEN);
1009         os_memcpy(head->bssid, hapd->own_addr, ETH_ALEN);
1010         head->u.beacon.beacon_int =
1011                 host_to_le16(hapd->iconf->beacon_int);
1012
1013         /* hardware or low-level driver will setup seq_ctrl and timestamp */
1014         capab_info = hostapd_own_capab_info(hapd);
1015         head->u.beacon.capab_info = host_to_le16(capab_info);
1016         pos = &head->u.beacon.variable[0];
1017
1018         /* SSID */
1019         *pos++ = WLAN_EID_SSID;
1020         if (hapd->conf->ignore_broadcast_ssid == 2) {
1021                 /* clear the data, but keep the correct length of the SSID */
1022                 *pos++ = hapd->conf->ssid.ssid_len;
1023                 os_memset(pos, 0, hapd->conf->ssid.ssid_len);
1024                 pos += hapd->conf->ssid.ssid_len;
1025         } else if (hapd->conf->ignore_broadcast_ssid) {
1026                 *pos++ = 0; /* empty SSID */
1027         } else {
1028                 *pos++ = hapd->conf->ssid.ssid_len;
1029                 os_memcpy(pos, hapd->conf->ssid.ssid,
1030                           hapd->conf->ssid.ssid_len);
1031                 pos += hapd->conf->ssid.ssid_len;
1032         }
1033
1034         /* Supported rates */
1035         pos = hostapd_eid_supp_rates(hapd, pos);
1036
1037         /* DS Params */
1038         pos = hostapd_eid_ds_params(hapd, pos);
1039
1040         head_len = pos - (u8 *) head;
1041
1042         tailpos = hostapd_eid_country(hapd, tailpos,
1043                                       tail + BEACON_TAIL_BUF_SIZE - tailpos);
1044
1045         /* Power Constraint element */
1046         tailpos = hostapd_eid_pwr_constraint(hapd, tailpos);
1047
1048         /* CSA IE */
1049         csa_pos = hostapd_eid_csa(hapd, tailpos);
1050         if (csa_pos != tailpos)
1051                 hapd->cs_c_off_beacon = csa_pos - tail - 1;
1052         tailpos = csa_pos;
1053
1054         /* ERP Information element */
1055         tailpos = hostapd_eid_erp_info(hapd, tailpos);
1056
1057         /* Extended supported rates */
1058         tailpos = hostapd_eid_ext_supp_rates(hapd, tailpos);
1059
1060         /* RSN, MDIE, WPA */
1061         tailpos = hostapd_eid_wpa(hapd, tailpos, tail + BEACON_TAIL_BUF_SIZE -
1062                                   tailpos);
1063
1064         tailpos = hostapd_eid_rm_enabled_capab(hapd, tailpos,
1065                                                tail + BEACON_TAIL_BUF_SIZE -
1066                                                tailpos);
1067
1068         tailpos = hostapd_eid_bss_load(hapd, tailpos,
1069                                        tail + BEACON_TAIL_BUF_SIZE - tailpos);
1070
1071         /* eCSA IE */
1072         csa_pos = hostapd_eid_ecsa(hapd, tailpos);
1073         if (csa_pos != tailpos)
1074                 hapd->cs_c_off_ecsa_beacon = csa_pos - tail - 1;
1075         tailpos = csa_pos;
1076
1077         tailpos = hostapd_eid_supported_op_classes(hapd, tailpos);
1078
1079 #ifdef CONFIG_IEEE80211N
1080         /* Secondary Channel Offset element */
1081         /* TODO: The standard doesn't specify a position for this element. */
1082         tailpos = hostapd_eid_secondary_channel(hapd, tailpos);
1083
1084         tailpos = hostapd_eid_ht_capabilities(hapd, tailpos);
1085         tailpos = hostapd_eid_ht_operation(hapd, tailpos);
1086 #endif /* CONFIG_IEEE80211N */
1087
1088         tailpos = hostapd_eid_ext_capab(hapd, tailpos);
1089
1090         /*
1091          * TODO: Time Advertisement element should only be included in some
1092          * DTIM Beacon frames.
1093          */
1094         tailpos = hostapd_eid_time_adv(hapd, tailpos);
1095
1096         tailpos = hostapd_eid_interworking(hapd, tailpos);
1097         tailpos = hostapd_eid_adv_proto(hapd, tailpos);
1098         tailpos = hostapd_eid_roaming_consortium(hapd, tailpos);
1099
1100 #ifdef CONFIG_FST
1101         if (hapd->iface->fst_ies) {
1102                 os_memcpy(tailpos, wpabuf_head(hapd->iface->fst_ies),
1103                           wpabuf_len(hapd->iface->fst_ies));
1104                 tailpos += wpabuf_len(hapd->iface->fst_ies);
1105         }
1106 #endif /* CONFIG_FST */
1107
1108 #ifdef CONFIG_IEEE80211AC
1109         if (hapd->iconf->ieee80211ac && !hapd->conf->disable_11ac) {
1110                 tailpos = hostapd_eid_vht_capabilities(hapd, tailpos, 0);
1111                 tailpos = hostapd_eid_vht_operation(hapd, tailpos);
1112                 tailpos = hostapd_eid_txpower_envelope(hapd, tailpos);
1113                 tailpos = hostapd_eid_wb_chsw_wrapper(hapd, tailpos);
1114         }
1115         if (hapd->conf->vendor_vht)
1116                 tailpos = hostapd_eid_vendor_vht(hapd, tailpos);
1117 #endif /* CONFIG_IEEE80211AC */
1118
1119         /* Wi-Fi Alliance WMM */
1120         tailpos = hostapd_eid_wmm(hapd, tailpos);
1121
1122 #ifdef CONFIG_WPS
1123         if (hapd->conf->wps_state && hapd->wps_beacon_ie) {
1124                 os_memcpy(tailpos, wpabuf_head(hapd->wps_beacon_ie),
1125                           wpabuf_len(hapd->wps_beacon_ie));
1126                 tailpos += wpabuf_len(hapd->wps_beacon_ie);
1127         }
1128 #endif /* CONFIG_WPS */
1129
1130 #ifdef CONFIG_P2P
1131         if ((hapd->conf->p2p & P2P_ENABLED) && hapd->p2p_beacon_ie) {
1132                 os_memcpy(tailpos, wpabuf_head(hapd->p2p_beacon_ie),
1133                           wpabuf_len(hapd->p2p_beacon_ie));
1134                 tailpos += wpabuf_len(hapd->p2p_beacon_ie);
1135         }
1136 #endif /* CONFIG_P2P */
1137 #ifdef CONFIG_P2P_MANAGER
1138         if ((hapd->conf->p2p & (P2P_MANAGE | P2P_ENABLED | P2P_GROUP_OWNER)) ==
1139             P2P_MANAGE)
1140                 tailpos = hostapd_eid_p2p_manage(hapd, tailpos);
1141 #endif /* CONFIG_P2P_MANAGER */
1142
1143 #ifdef CONFIG_HS20
1144         tailpos = hostapd_eid_hs20_indication(hapd, tailpos);
1145         tailpos = hostapd_eid_osen(hapd, tailpos);
1146 #endif /* CONFIG_HS20 */
1147
1148         tailpos = hostapd_eid_mbo(hapd, tailpos, tail + tail_len - tailpos);
1149
1150         if (hapd->conf->vendor_elements) {
1151                 os_memcpy(tailpos, wpabuf_head(hapd->conf->vendor_elements),
1152                           wpabuf_len(hapd->conf->vendor_elements));
1153                 tailpos += wpabuf_len(hapd->conf->vendor_elements);
1154         }
1155
1156         tail_len = tailpos > tail ? tailpos - tail : 0;
1157
1158         resp = hostapd_probe_resp_offloads(hapd, &resp_len);
1159 #endif /* NEED_AP_MLME */
1160
1161         os_memset(params, 0, sizeof(*params));
1162         params->head = (u8 *) head;
1163         params->head_len = head_len;
1164         params->tail = tail;
1165         params->tail_len = tail_len;
1166         params->proberesp = resp;
1167         params->proberesp_len = resp_len;
1168         params->dtim_period = hapd->conf->dtim_period;
1169         params->beacon_int = hapd->iconf->beacon_int;
1170         params->basic_rates = hapd->iface->basic_rates;
1171         params->ssid = hapd->conf->ssid.ssid;
1172         params->ssid_len = hapd->conf->ssid.ssid_len;
1173         if ((hapd->conf->wpa & (WPA_PROTO_WPA | WPA_PROTO_RSN)) ==
1174             (WPA_PROTO_WPA | WPA_PROTO_RSN))
1175                 params->pairwise_ciphers = hapd->conf->wpa_pairwise |
1176                         hapd->conf->rsn_pairwise;
1177         else if (hapd->conf->wpa & WPA_PROTO_RSN)
1178                 params->pairwise_ciphers = hapd->conf->rsn_pairwise;
1179         else if (hapd->conf->wpa & WPA_PROTO_WPA)
1180                 params->pairwise_ciphers = hapd->conf->wpa_pairwise;
1181         params->group_cipher = hapd->conf->wpa_group;
1182         params->key_mgmt_suites = hapd->conf->wpa_key_mgmt;
1183         params->auth_algs = hapd->conf->auth_algs;
1184         params->wpa_version = hapd->conf->wpa;
1185         params->privacy = hapd->conf->ssid.wep.keys_set || hapd->conf->wpa ||
1186                 (hapd->conf->ieee802_1x &&
1187                  (hapd->conf->default_wep_key_len ||
1188                   hapd->conf->individual_wep_key_len));
1189         switch (hapd->conf->ignore_broadcast_ssid) {
1190         case 0:
1191                 params->hide_ssid = NO_SSID_HIDING;
1192                 break;
1193         case 1:
1194                 params->hide_ssid = HIDDEN_SSID_ZERO_LEN;
1195                 break;
1196         case 2:
1197                 params->hide_ssid = HIDDEN_SSID_ZERO_CONTENTS;
1198                 break;
1199         }
1200         params->isolate = hapd->conf->isolate;
1201         params->smps_mode = hapd->iconf->ht_capab & HT_CAP_INFO_SMPS_MASK;
1202 #ifdef NEED_AP_MLME
1203         params->cts_protect = !!(ieee802_11_erp_info(hapd) &
1204                                 ERP_INFO_USE_PROTECTION);
1205         params->preamble = hapd->iface->num_sta_no_short_preamble == 0 &&
1206                 hapd->iconf->preamble == SHORT_PREAMBLE;
1207         if (hapd->iface->current_mode &&
1208             hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G)
1209                 params->short_slot_time =
1210                         hapd->iface->num_sta_no_short_slot_time > 0 ? 0 : 1;
1211         else
1212                 params->short_slot_time = -1;
1213         if (!hapd->iconf->ieee80211n || hapd->conf->disable_11n)
1214                 params->ht_opmode = -1;
1215         else
1216                 params->ht_opmode = hapd->iface->ht_op_mode;
1217 #endif /* NEED_AP_MLME */
1218         params->interworking = hapd->conf->interworking;
1219         if (hapd->conf->interworking &&
1220             !is_zero_ether_addr(hapd->conf->hessid))
1221                 params->hessid = hapd->conf->hessid;
1222         params->access_network_type = hapd->conf->access_network_type;
1223         params->ap_max_inactivity = hapd->conf->ap_max_inactivity;
1224 #ifdef CONFIG_P2P
1225         params->p2p_go_ctwindow = hapd->iconf->p2p_go_ctwindow;
1226 #endif /* CONFIG_P2P */
1227 #ifdef CONFIG_HS20
1228         params->disable_dgaf = hapd->conf->disable_dgaf;
1229         if (hapd->conf->osen) {
1230                 params->privacy = 1;
1231                 params->osen = 1;
1232         }
1233 #endif /* CONFIG_HS20 */
1234         params->pbss = hapd->conf->pbss;
1235         return 0;
1236 }
1237
1238
1239 void ieee802_11_free_ap_params(struct wpa_driver_ap_params *params)
1240 {
1241         os_free(params->tail);
1242         params->tail = NULL;
1243         os_free(params->head);
1244         params->head = NULL;
1245         os_free(params->proberesp);
1246         params->proberesp = NULL;
1247 }
1248
1249
1250 int ieee802_11_set_beacon(struct hostapd_data *hapd)
1251 {
1252         struct wpa_driver_ap_params params;
1253         struct hostapd_freq_params freq;
1254         struct hostapd_iface *iface = hapd->iface;
1255         struct hostapd_config *iconf = iface->conf;
1256         struct wpabuf *beacon, *proberesp, *assocresp;
1257         int res, ret = -1;
1258
1259         if (hapd->csa_in_progress) {
1260                 wpa_printf(MSG_ERROR, "Cannot set beacons during CSA period");
1261                 return -1;
1262         }
1263
1264         hapd->beacon_set_done = 1;
1265
1266         if (ieee802_11_build_ap_params(hapd, &params) < 0)
1267                 return -1;
1268
1269         if (hostapd_build_ap_extra_ies(hapd, &beacon, &proberesp, &assocresp) <
1270             0)
1271                 goto fail;
1272
1273         params.beacon_ies = beacon;
1274         params.proberesp_ies = proberesp;
1275         params.assocresp_ies = assocresp;
1276         params.reenable = hapd->reenable_beacon;
1277         hapd->reenable_beacon = 0;
1278
1279         if (iface->current_mode &&
1280             hostapd_set_freq_params(&freq, iconf->hw_mode, iface->freq,
1281                                     iconf->channel, iconf->ieee80211n,
1282                                     iconf->ieee80211ac,
1283                                     iconf->secondary_channel,
1284                                     iconf->vht_oper_chwidth,
1285                                     iconf->vht_oper_centr_freq_seg0_idx,
1286                                     iconf->vht_oper_centr_freq_seg1_idx,
1287                                     iface->current_mode->vht_capab) == 0)
1288                 params.freq = &freq;
1289
1290         res = hostapd_drv_set_ap(hapd, &params);
1291         hostapd_free_ap_extra_ies(hapd, beacon, proberesp, assocresp);
1292         if (res)
1293                 wpa_printf(MSG_ERROR, "Failed to set beacon parameters");
1294         else
1295                 ret = 0;
1296 fail:
1297         ieee802_11_free_ap_params(&params);
1298         return ret;
1299 }
1300
1301
1302 int ieee802_11_set_beacons(struct hostapd_iface *iface)
1303 {
1304         size_t i;
1305         int ret = 0;
1306
1307         for (i = 0; i < iface->num_bss; i++) {
1308                 if (iface->bss[i]->started &&
1309                     ieee802_11_set_beacon(iface->bss[i]) < 0)
1310                         ret = -1;
1311         }
1312
1313         return ret;
1314 }
1315
1316
1317 /* only update beacons if started */
1318 int ieee802_11_update_beacons(struct hostapd_iface *iface)
1319 {
1320         size_t i;
1321         int ret = 0;
1322
1323         for (i = 0; i < iface->num_bss; i++) {
1324                 if (iface->bss[i]->beacon_set_done && iface->bss[i]->started &&
1325                     ieee802_11_set_beacon(iface->bss[i]) < 0)
1326                         ret = -1;
1327         }
1328
1329         return ret;
1330 }
1331
1332 #endif /* CONFIG_NATIVE_WINDOWS */