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