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