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