P2P: Extend P2P manager functionality to work with driver MLME
[libeap.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-2009, Jouni Malinen <j@w1.fi>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License version 2 as
9  * published by the Free Software Foundation.
10  *
11  * Alternatively, this software may be distributed under the terms of BSD
12  * license.
13  *
14  * See README and COPYING for more details.
15  */
16
17 #include "utils/includes.h"
18
19 #ifndef CONFIG_NATIVE_WINDOWS
20
21 #include "utils/common.h"
22 #include "common/ieee802_11_defs.h"
23 #include "common/ieee802_11_common.h"
24 #include "drivers/driver.h"
25 #include "wps/wps_defs.h"
26 #include "p2p/p2p.h"
27 #include "hostapd.h"
28 #include "ieee802_11.h"
29 #include "wpa_auth.h"
30 #include "wmm.h"
31 #include "ap_config.h"
32 #include "sta_info.h"
33 #include "p2p_hostapd.h"
34 #include "beacon.h"
35
36
37 static u8 ieee802_11_erp_info(struct hostapd_data *hapd)
38 {
39         u8 erp = 0;
40
41         if (hapd->iface->current_mode == NULL ||
42             hapd->iface->current_mode->mode != HOSTAPD_MODE_IEEE80211G)
43                 return 0;
44
45         switch (hapd->iconf->cts_protection_type) {
46         case CTS_PROTECTION_FORCE_ENABLED:
47                 erp |= ERP_INFO_NON_ERP_PRESENT | ERP_INFO_USE_PROTECTION;
48                 break;
49         case CTS_PROTECTION_FORCE_DISABLED:
50                 erp = 0;
51                 break;
52         case CTS_PROTECTION_AUTOMATIC:
53                 if (hapd->iface->olbc)
54                         erp |= ERP_INFO_USE_PROTECTION;
55                 /* continue */
56         case CTS_PROTECTION_AUTOMATIC_NO_OLBC:
57                 if (hapd->iface->num_sta_non_erp > 0) {
58                         erp |= ERP_INFO_NON_ERP_PRESENT |
59                                 ERP_INFO_USE_PROTECTION;
60                 }
61                 break;
62         }
63         if (hapd->iface->num_sta_no_short_preamble > 0 ||
64             hapd->iconf->preamble == LONG_PREAMBLE)
65                 erp |= ERP_INFO_BARKER_PREAMBLE_MODE;
66
67         return erp;
68 }
69
70
71 static u8 * hostapd_eid_ds_params(struct hostapd_data *hapd, u8 *eid)
72 {
73         *eid++ = WLAN_EID_DS_PARAMS;
74         *eid++ = 1;
75         *eid++ = hapd->iconf->channel;
76         return eid;
77 }
78
79
80 static u8 * hostapd_eid_erp_info(struct hostapd_data *hapd, u8 *eid)
81 {
82         if (hapd->iface->current_mode == NULL ||
83             hapd->iface->current_mode->mode != HOSTAPD_MODE_IEEE80211G)
84                 return eid;
85
86         /* Set NonERP_present and use_protection bits if there
87          * are any associated NonERP stations. */
88         /* TODO: use_protection bit can be set to zero even if
89          * there are NonERP stations present. This optimization
90          * might be useful if NonERP stations are "quiet".
91          * See 802.11g/D6 E-1 for recommended practice.
92          * In addition, Non ERP present might be set, if AP detects Non ERP
93          * operation on other APs. */
94
95         /* Add ERP Information element */
96         *eid++ = WLAN_EID_ERP_INFO;
97         *eid++ = 1;
98         *eid++ = ieee802_11_erp_info(hapd);
99
100         return eid;
101 }
102
103
104 static u8 * hostapd_eid_country_add(u8 *pos, u8 *end, int chan_spacing,
105                                     struct hostapd_channel_data *start,
106                                     struct hostapd_channel_data *prev)
107 {
108         if (end - pos < 3)
109                 return pos;
110
111         /* first channel number */
112         *pos++ = start->chan;
113         /* number of channels */
114         *pos++ = (prev->chan - start->chan) / chan_spacing + 1;
115         /* maximum transmit power level */
116         *pos++ = start->max_tx_power;
117
118         return pos;
119 }
120
121
122 static u8 * hostapd_eid_country(struct hostapd_data *hapd, u8 *eid,
123                                 int max_len)
124 {
125         u8 *pos = eid;
126         u8 *end = eid + max_len;
127         int i;
128         struct hostapd_hw_modes *mode;
129         struct hostapd_channel_data *start, *prev;
130         int chan_spacing = 1;
131
132         if (!hapd->iconf->ieee80211d || max_len < 6 ||
133             hapd->iface->current_mode == NULL)
134                 return eid;
135
136         *pos++ = WLAN_EID_COUNTRY;
137         pos++; /* length will be set later */
138         os_memcpy(pos, hapd->iconf->country, 3); /* e.g., 'US ' */
139         pos += 3;
140
141         mode = hapd->iface->current_mode;
142         if (mode->mode == HOSTAPD_MODE_IEEE80211A)
143                 chan_spacing = 4;
144
145         start = prev = NULL;
146         for (i = 0; i < mode->num_channels; i++) {
147                 struct hostapd_channel_data *chan = &mode->channels[i];
148                 if (chan->flag & HOSTAPD_CHAN_DISABLED)
149                         continue;
150                 if (start && prev &&
151                     prev->chan + chan_spacing == chan->chan &&
152                     start->max_tx_power == chan->max_tx_power) {
153                         prev = chan;
154                         continue; /* can use same entry */
155                 }
156
157                 if (start) {
158                         pos = hostapd_eid_country_add(pos, end, chan_spacing,
159                                                       start, prev);
160                         start = NULL;
161                 }
162
163                 /* Start new group */
164                 start = prev = chan;
165         }
166
167         if (start) {
168                 pos = hostapd_eid_country_add(pos, end, chan_spacing,
169                                               start, prev);
170         }
171
172         if ((pos - eid) & 1) {
173                 if (end - pos < 1)
174                         return eid;
175                 *pos++ = 0; /* pad for 16-bit alignment */
176         }
177
178         eid[1] = (pos - eid) - 2;
179
180         return pos;
181 }
182
183
184 static u8 * hostapd_eid_wpa(struct hostapd_data *hapd, u8 *eid, size_t len,
185                             struct sta_info *sta)
186 {
187         const u8 *ie;
188         size_t ielen;
189
190         ie = wpa_auth_get_wpa_ie(hapd->wpa_auth, &ielen);
191         if (ie == NULL || ielen > len)
192                 return eid;
193
194         os_memcpy(eid, ie, ielen);
195         return eid + ielen;
196 }
197
198
199 void handle_probe_req(struct hostapd_data *hapd,
200                       const struct ieee80211_mgmt *mgmt, size_t len)
201 {
202         struct ieee80211_mgmt *resp;
203         struct ieee802_11_elems elems;
204         char *ssid;
205         u8 *pos, *epos;
206         const u8 *ie;
207         size_t ssid_len, ie_len;
208         struct sta_info *sta = NULL;
209         size_t buflen;
210         size_t i;
211
212         ie = mgmt->u.probe_req.variable;
213         ie_len = len - (IEEE80211_HDRLEN + sizeof(mgmt->u.probe_req));
214
215         for (i = 0; hapd->probereq_cb && i < hapd->num_probereq_cb; i++)
216                 if (hapd->probereq_cb[i].cb(hapd->probereq_cb[i].ctx,
217                                             mgmt->sa, ie, ie_len) > 0)
218                         return;
219
220         if (!hapd->iconf->send_probe_response)
221                 return;
222
223         if (ieee802_11_parse_elems(ie, ie_len, &elems, 0) == ParseFailed) {
224                 wpa_printf(MSG_DEBUG, "Could not parse ProbeReq from " MACSTR,
225                            MAC2STR(mgmt->sa));
226                 return;
227         }
228
229         ssid = NULL;
230         ssid_len = 0;
231
232         if ((!elems.ssid || !elems.supp_rates)) {
233                 wpa_printf(MSG_DEBUG, "STA " MACSTR " sent probe request "
234                            "without SSID or supported rates element",
235                            MAC2STR(mgmt->sa));
236                 return;
237         }
238
239 #ifdef CONFIG_P2P
240         if (hapd->p2p && elems.wps_ie) {
241                 struct wpabuf *wps;
242                 wps = ieee802_11_vendor_ie_concat(ie, ie_len, WPS_DEV_OUI_WFA);
243                 if (wps && !p2p_group_match_dev_type(hapd->p2p_group, wps)) {
244                         wpa_printf(MSG_MSGDUMP, "P2P: Ignore Probe Request "
245                                    "due to mismatch with Requested Device "
246                                    "Type");
247                         wpabuf_free(wps);
248                         return;
249                 }
250                 wpabuf_free(wps);
251         }
252 #endif /* CONFIG_P2P */
253
254         if (hapd->conf->ignore_broadcast_ssid && elems.ssid_len == 0) {
255                 wpa_printf(MSG_MSGDUMP, "Probe Request from " MACSTR " for "
256                            "broadcast SSID ignored", MAC2STR(mgmt->sa));
257                 return;
258         }
259
260         sta = ap_get_sta(hapd, mgmt->sa);
261
262 #ifdef CONFIG_P2P
263         if ((hapd->conf->p2p & P2P_GROUP_OWNER) &&
264             elems.ssid_len == P2P_WILDCARD_SSID_LEN &&
265             os_memcmp(elems.ssid, P2P_WILDCARD_SSID,
266                       P2P_WILDCARD_SSID_LEN) == 0) {
267                 /* Process P2P Wildcard SSID like Wildcard SSID */
268                 elems.ssid_len = 0;
269         }
270 #endif /* CONFIG_P2P */
271
272         if (elems.ssid_len == 0 ||
273             (elems.ssid_len == hapd->conf->ssid.ssid_len &&
274              os_memcmp(elems.ssid, hapd->conf->ssid.ssid, elems.ssid_len) ==
275              0)) {
276                 ssid = hapd->conf->ssid.ssid;
277                 ssid_len = hapd->conf->ssid.ssid_len;
278                 if (sta)
279                         sta->ssid_probe = &hapd->conf->ssid;
280         }
281
282         if (!ssid) {
283                 if (!(mgmt->da[0] & 0x01)) {
284                         char ssid_txt[33];
285                         ieee802_11_print_ssid(ssid_txt, elems.ssid,
286                                               elems.ssid_len);
287                         wpa_printf(MSG_MSGDUMP, "Probe Request from " MACSTR
288                                    " for foreign SSID '%s' (DA " MACSTR ")",
289                                    MAC2STR(mgmt->sa), ssid_txt,
290                                    MAC2STR(mgmt->da));
291                 }
292                 return;
293         }
294
295         /* TODO: verify that supp_rates contains at least one matching rate
296          * with AP configuration */
297 #define MAX_PROBERESP_LEN 768
298         buflen = MAX_PROBERESP_LEN;
299 #ifdef CONFIG_WPS
300         if (hapd->wps_probe_resp_ie)
301                 buflen += wpabuf_len(hapd->wps_probe_resp_ie);
302 #endif /* CONFIG_WPS */
303 #ifdef CONFIG_P2P
304         if (hapd->p2p_probe_resp_ie)
305                 buflen += wpabuf_len(hapd->p2p_probe_resp_ie);
306 #endif /* CONFIG_P2P */
307         resp = os_zalloc(buflen);
308         if (resp == NULL)
309                 return;
310         epos = ((u8 *) resp) + MAX_PROBERESP_LEN;
311
312         resp->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
313                                            WLAN_FC_STYPE_PROBE_RESP);
314         os_memcpy(resp->da, mgmt->sa, ETH_ALEN);
315         os_memcpy(resp->sa, hapd->own_addr, ETH_ALEN);
316
317         os_memcpy(resp->bssid, hapd->own_addr, ETH_ALEN);
318         resp->u.probe_resp.beacon_int =
319                 host_to_le16(hapd->iconf->beacon_int);
320
321         /* hardware or low-level driver will setup seq_ctrl and timestamp */
322         resp->u.probe_resp.capab_info =
323                 host_to_le16(hostapd_own_capab_info(hapd, sta, 1));
324
325         pos = resp->u.probe_resp.variable;
326         *pos++ = WLAN_EID_SSID;
327         *pos++ = ssid_len;
328         os_memcpy(pos, ssid, ssid_len);
329         pos += ssid_len;
330
331         /* Supported rates */
332         pos = hostapd_eid_supp_rates(hapd, pos);
333
334         /* DS Params */
335         pos = hostapd_eid_ds_params(hapd, pos);
336
337         pos = hostapd_eid_country(hapd, pos, epos - pos);
338
339         /* ERP Information element */
340         pos = hostapd_eid_erp_info(hapd, pos);
341
342         /* Extended supported rates */
343         pos = hostapd_eid_ext_supp_rates(hapd, pos);
344
345         /* RSN, MDIE, WPA */
346         pos = hostapd_eid_wpa(hapd, pos, epos - pos, sta);
347
348 #ifdef CONFIG_IEEE80211N
349         pos = hostapd_eid_ht_capabilities(hapd, pos);
350         pos = hostapd_eid_ht_operation(hapd, pos);
351 #endif /* CONFIG_IEEE80211N */
352
353         /* Wi-Fi Alliance WMM */
354         pos = hostapd_eid_wmm(hapd, pos);
355
356 #ifdef CONFIG_WPS
357         if (hapd->conf->wps_state && hapd->wps_probe_resp_ie) {
358                 os_memcpy(pos, wpabuf_head(hapd->wps_probe_resp_ie),
359                           wpabuf_len(hapd->wps_probe_resp_ie));
360                 pos += wpabuf_len(hapd->wps_probe_resp_ie);
361         }
362 #endif /* CONFIG_WPS */
363
364 #ifdef CONFIG_P2P
365         if ((hapd->conf->p2p & P2P_ENABLED) && elems.p2p &&
366             hapd->p2p_probe_resp_ie) {
367                 os_memcpy(pos, wpabuf_head(hapd->p2p_probe_resp_ie),
368                           wpabuf_len(hapd->p2p_probe_resp_ie));
369                 pos += wpabuf_len(hapd->p2p_probe_resp_ie);
370         }
371 #endif /* CONFIG_P2P */
372 #ifdef CONFIG_P2P_MANAGER
373         if ((hapd->conf->p2p & (P2P_MANAGE | P2P_ENABLED | P2P_GROUP_OWNER)) ==
374             P2P_MANAGE)
375                 pos = hostapd_eid_p2p_manage(hapd, pos);
376 #endif /* CONFIG_P2P_MANAGER */
377
378         if (hapd->drv.send_mgmt_frame(hapd, resp, pos - (u8 *) resp) < 0)
379                 perror("handle_probe_req: send");
380
381         os_free(resp);
382
383         wpa_printf(MSG_EXCESSIVE, "STA " MACSTR " sent probe request for %s "
384                    "SSID", MAC2STR(mgmt->sa),
385                    elems.ssid_len == 0 ? "broadcast" : "our");
386 }
387
388
389 void ieee802_11_set_beacon(struct hostapd_data *hapd)
390 {
391         struct ieee80211_mgmt *head;
392         u8 *pos, *tail, *tailpos;
393         u16 capab_info;
394         size_t head_len, tail_len;
395
396 #ifdef CONFIG_P2P
397         if ((hapd->conf->p2p & (P2P_ENABLED | P2P_GROUP_OWNER)) == P2P_ENABLED)
398                 goto no_beacon;
399 #endif /* CONFIG_P2P */
400
401 #define BEACON_HEAD_BUF_SIZE 256
402 #define BEACON_TAIL_BUF_SIZE 512
403         head = os_zalloc(BEACON_HEAD_BUF_SIZE);
404         tail_len = BEACON_TAIL_BUF_SIZE;
405 #ifdef CONFIG_WPS
406         if (hapd->conf->wps_state && hapd->wps_beacon_ie)
407                 tail_len += wpabuf_len(hapd->wps_beacon_ie);
408 #endif /* CONFIG_WPS */
409 #ifdef CONFIG_P2P
410         if (hapd->p2p_beacon_ie)
411                 tail_len += wpabuf_len(hapd->p2p_beacon_ie);
412 #endif /* CONFIG_P2P */
413         tailpos = tail = os_malloc(tail_len);
414         if (head == NULL || tail == NULL) {
415                 wpa_printf(MSG_ERROR, "Failed to set beacon data");
416                 os_free(head);
417                 os_free(tail);
418                 return;
419         }
420
421         head->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
422                                            WLAN_FC_STYPE_BEACON);
423         head->duration = host_to_le16(0);
424         os_memset(head->da, 0xff, ETH_ALEN);
425
426         os_memcpy(head->sa, hapd->own_addr, ETH_ALEN);
427         os_memcpy(head->bssid, hapd->own_addr, ETH_ALEN);
428         head->u.beacon.beacon_int =
429                 host_to_le16(hapd->iconf->beacon_int);
430
431         /* hardware or low-level driver will setup seq_ctrl and timestamp */
432         capab_info = hostapd_own_capab_info(hapd, NULL, 0);
433         head->u.beacon.capab_info = host_to_le16(capab_info);
434         pos = &head->u.beacon.variable[0];
435
436         /* SSID */
437         *pos++ = WLAN_EID_SSID;
438         if (hapd->conf->ignore_broadcast_ssid == 2) {
439                 /* clear the data, but keep the correct length of the SSID */
440                 *pos++ = hapd->conf->ssid.ssid_len;
441                 os_memset(pos, 0, hapd->conf->ssid.ssid_len);
442                 pos += hapd->conf->ssid.ssid_len;
443         } else if (hapd->conf->ignore_broadcast_ssid) {
444                 *pos++ = 0; /* empty SSID */
445         } else {
446                 *pos++ = hapd->conf->ssid.ssid_len;
447                 os_memcpy(pos, hapd->conf->ssid.ssid,
448                           hapd->conf->ssid.ssid_len);
449                 pos += hapd->conf->ssid.ssid_len;
450         }
451
452         /* Supported rates */
453         pos = hostapd_eid_supp_rates(hapd, pos);
454
455         /* DS Params */
456         pos = hostapd_eid_ds_params(hapd, pos);
457
458         head_len = pos - (u8 *) head;
459
460         tailpos = hostapd_eid_country(hapd, tailpos,
461                                       tail + BEACON_TAIL_BUF_SIZE - tailpos);
462
463         /* ERP Information element */
464         tailpos = hostapd_eid_erp_info(hapd, tailpos);
465
466         /* Extended supported rates */
467         tailpos = hostapd_eid_ext_supp_rates(hapd, tailpos);
468
469         /* RSN, MDIE, WPA */
470         tailpos = hostapd_eid_wpa(hapd, tailpos, tail + BEACON_TAIL_BUF_SIZE -
471                                   tailpos, NULL);
472
473 #ifdef CONFIG_IEEE80211N
474         tailpos = hostapd_eid_ht_capabilities(hapd, tailpos);
475         tailpos = hostapd_eid_ht_operation(hapd, tailpos);
476 #endif /* CONFIG_IEEE80211N */
477
478         /* Wi-Fi Alliance WMM */
479         tailpos = hostapd_eid_wmm(hapd, tailpos);
480
481 #ifdef CONFIG_WPS
482         if (hapd->conf->wps_state && hapd->wps_beacon_ie) {
483                 os_memcpy(tailpos, wpabuf_head(hapd->wps_beacon_ie),
484                           wpabuf_len(hapd->wps_beacon_ie));
485                 tailpos += wpabuf_len(hapd->wps_beacon_ie);
486         }
487 #endif /* CONFIG_WPS */
488
489 #ifdef CONFIG_P2P
490         if ((hapd->conf->p2p & P2P_ENABLED) && hapd->p2p_beacon_ie) {
491                 os_memcpy(tailpos, wpabuf_head(hapd->p2p_beacon_ie),
492                           wpabuf_len(hapd->p2p_beacon_ie));
493                 tailpos += wpabuf_len(hapd->p2p_beacon_ie);
494         }
495 #endif /* CONFIG_P2P */
496 #ifdef CONFIG_P2P_MANAGER
497         if ((hapd->conf->p2p & (P2P_MANAGE | P2P_ENABLED | P2P_GROUP_OWNER)) ==
498             P2P_MANAGE)
499                 tailpos = hostapd_eid_p2p_manage(hapd, tailpos);
500 #endif /* CONFIG_P2P_MANAGER */
501
502         tail_len = tailpos > tail ? tailpos - tail : 0;
503
504         if (hapd->drv.set_beacon(hapd, (u8 *) head, head_len,
505                                  tail, tail_len, hapd->conf->dtim_period,
506                                  hapd->iconf->beacon_int))
507                 wpa_printf(MSG_ERROR, "Failed to set beacon head/tail or DTIM "
508                            "period");
509
510         os_free(tail);
511         os_free(head);
512
513 #ifdef CONFIG_P2P
514 no_beacon:
515 #endif /* CONFIG_P2P */
516         hapd->drv.set_bss_params(hapd, !!(ieee802_11_erp_info(hapd) &
517                                           ERP_INFO_USE_PROTECTION));
518 }
519
520
521 void ieee802_11_set_beacons(struct hostapd_iface *iface)
522 {
523         size_t i;
524         for (i = 0; i < iface->num_bss; i++)
525                 ieee802_11_set_beacon(iface->bss[i]);
526 }
527
528 #endif /* CONFIG_NATIVE_WINDOWS */