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