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