P2P: Add P2P configuration and callbacks in hostapd code
[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 void handle_probe_req(struct hostapd_data *hapd,
199                       const struct ieee80211_mgmt *mgmt, size_t len)
200 {
201         struct ieee80211_mgmt *resp;
202         struct ieee802_11_elems elems;
203         char *ssid;
204         u8 *pos, *epos;
205         const u8 *ie;
206         size_t ssid_len, ie_len;
207         struct sta_info *sta = NULL;
208         size_t buflen;
209         size_t i;
210
211         ie = mgmt->u.probe_req.variable;
212         ie_len = len - (IEEE80211_HDRLEN + sizeof(mgmt->u.probe_req));
213
214         for (i = 0; hapd->probereq_cb && i < hapd->num_probereq_cb; i++)
215                 if (hapd->probereq_cb[i].cb(hapd->probereq_cb[i].ctx,
216                                             mgmt->sa, ie, ie_len) > 0)
217                         return;
218
219         if (!hapd->iconf->send_probe_response)
220                 return;
221
222         if (ieee802_11_parse_elems(ie, ie_len, &elems, 0) == ParseFailed) {
223                 wpa_printf(MSG_DEBUG, "Could not parse ProbeReq from " MACSTR,
224                            MAC2STR(mgmt->sa));
225                 return;
226         }
227
228         ssid = NULL;
229         ssid_len = 0;
230
231         if ((!elems.ssid || !elems.supp_rates)) {
232                 wpa_printf(MSG_DEBUG, "STA " MACSTR " sent probe request "
233                            "without SSID or supported rates element",
234                            MAC2STR(mgmt->sa));
235                 return;
236         }
237
238 #ifdef CONFIG_P2P
239         if (hapd->p2p && elems.wps_ie) {
240                 struct wpabuf *wps;
241                 wps = ieee802_11_vendor_ie_concat(ie, ie_len, WPS_DEV_OUI_WFA);
242                 if (wps && !p2p_group_match_dev_type(hapd->p2p_group, wps)) {
243                         wpa_printf(MSG_MSGDUMP, "P2P: Ignore Probe Request "
244                                    "due to mismatch with Requested Device "
245                                    "Type");
246                         wpabuf_free(wps);
247                         return;
248                 }
249                 wpabuf_free(wps);
250         }
251 #endif /* CONFIG_P2P */
252
253         if (hapd->conf->ignore_broadcast_ssid && elems.ssid_len == 0) {
254                 wpa_printf(MSG_MSGDUMP, "Probe Request from " MACSTR " for "
255                            "broadcast SSID ignored", MAC2STR(mgmt->sa));
256                 return;
257         }
258
259         sta = ap_get_sta(hapd, mgmt->sa);
260
261 #ifdef CONFIG_P2P
262         if ((hapd->conf->p2p & P2P_GROUP_OWNER) &&
263             elems.ssid_len == P2P_WILDCARD_SSID_LEN &&
264             os_memcmp(elems.ssid, P2P_WILDCARD_SSID,
265                       P2P_WILDCARD_SSID_LEN) == 0) {
266                 /* Process P2P Wildcard SSID like Wildcard SSID */
267                 elems.ssid_len = 0;
268         }
269 #endif /* CONFIG_P2P */
270
271         if (elems.ssid_len == 0 ||
272             (elems.ssid_len == hapd->conf->ssid.ssid_len &&
273              os_memcmp(elems.ssid, hapd->conf->ssid.ssid, elems.ssid_len) ==
274              0)) {
275                 ssid = hapd->conf->ssid.ssid;
276                 ssid_len = hapd->conf->ssid.ssid_len;
277                 if (sta)
278                         sta->ssid_probe = &hapd->conf->ssid;
279         }
280
281         if (!ssid) {
282                 if (!(mgmt->da[0] & 0x01)) {
283                         char ssid_txt[33];
284                         ieee802_11_print_ssid(ssid_txt, elems.ssid,
285                                               elems.ssid_len);
286                         wpa_printf(MSG_MSGDUMP, "Probe Request from " MACSTR
287                                    " for foreign SSID '%s' (DA " MACSTR ")",
288                                    MAC2STR(mgmt->sa), ssid_txt,
289                                    MAC2STR(mgmt->da));
290                 }
291                 return;
292         }
293
294         /* TODO: verify that supp_rates contains at least one matching rate
295          * with AP configuration */
296 #define MAX_PROBERESP_LEN 768
297         buflen = MAX_PROBERESP_LEN;
298 #ifdef CONFIG_WPS
299         if (hapd->wps_probe_resp_ie)
300                 buflen += wpabuf_len(hapd->wps_probe_resp_ie);
301 #endif /* CONFIG_WPS */
302 #ifdef CONFIG_P2P
303         if (hapd->p2p_probe_resp_ie)
304                 buflen += wpabuf_len(hapd->p2p_probe_resp_ie);
305 #endif /* CONFIG_P2P */
306         resp = os_zalloc(buflen);
307         if (resp == NULL)
308                 return;
309         epos = ((u8 *) resp) + MAX_PROBERESP_LEN;
310
311         resp->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
312                                            WLAN_FC_STYPE_PROBE_RESP);
313         os_memcpy(resp->da, mgmt->sa, ETH_ALEN);
314         os_memcpy(resp->sa, hapd->own_addr, ETH_ALEN);
315
316         os_memcpy(resp->bssid, hapd->own_addr, ETH_ALEN);
317         resp->u.probe_resp.beacon_int =
318                 host_to_le16(hapd->iconf->beacon_int);
319
320         /* hardware or low-level driver will setup seq_ctrl and timestamp */
321         resp->u.probe_resp.capab_info =
322                 host_to_le16(hostapd_own_capab_info(hapd, sta, 1));
323
324         pos = resp->u.probe_resp.variable;
325         *pos++ = WLAN_EID_SSID;
326         *pos++ = ssid_len;
327         os_memcpy(pos, ssid, ssid_len);
328         pos += ssid_len;
329
330         /* Supported rates */
331         pos = hostapd_eid_supp_rates(hapd, pos);
332
333         /* DS Params */
334         pos = hostapd_eid_ds_params(hapd, pos);
335
336         pos = hostapd_eid_country(hapd, pos, epos - pos);
337
338         /* ERP Information element */
339         pos = hostapd_eid_erp_info(hapd, pos);
340
341         /* Extended supported rates */
342         pos = hostapd_eid_ext_supp_rates(hapd, pos);
343
344         /* RSN, MDIE, WPA */
345         pos = hostapd_eid_wpa(hapd, pos, epos - pos, sta);
346
347 #ifdef CONFIG_IEEE80211N
348         pos = hostapd_eid_ht_capabilities(hapd, pos);
349         pos = hostapd_eid_ht_operation(hapd, pos);
350 #endif /* CONFIG_IEEE80211N */
351
352         /* Wi-Fi Alliance WMM */
353         pos = hostapd_eid_wmm(hapd, pos);
354
355 #ifdef CONFIG_WPS
356         if (hapd->conf->wps_state && hapd->wps_probe_resp_ie) {
357                 os_memcpy(pos, wpabuf_head(hapd->wps_probe_resp_ie),
358                           wpabuf_len(hapd->wps_probe_resp_ie));
359                 pos += wpabuf_len(hapd->wps_probe_resp_ie);
360         }
361 #endif /* CONFIG_WPS */
362
363 #ifdef CONFIG_P2P
364         if ((hapd->conf->p2p & P2P_ENABLED) && elems.p2p &&
365             hapd->p2p_probe_resp_ie) {
366                 os_memcpy(pos, wpabuf_head(hapd->p2p_probe_resp_ie),
367                           wpabuf_len(hapd->p2p_probe_resp_ie));
368                 pos += wpabuf_len(hapd->p2p_probe_resp_ie);
369         }
370 #endif /* CONFIG_P2P */
371
372         if (hapd->drv.send_mgmt_frame(hapd, resp, pos - (u8 *) resp) < 0)
373                 perror("handle_probe_req: send");
374
375         os_free(resp);
376
377         wpa_printf(MSG_EXCESSIVE, "STA " MACSTR " sent probe request for %s "
378                    "SSID", MAC2STR(mgmt->sa),
379                    elems.ssid_len == 0 ? "broadcast" : "our");
380 }
381
382
383 void ieee802_11_set_beacon(struct hostapd_data *hapd)
384 {
385         struct ieee80211_mgmt *head;
386         u8 *pos, *tail, *tailpos;
387         u16 capab_info;
388         size_t head_len, tail_len;
389
390 #ifdef CONFIG_P2P
391         if ((hapd->conf->p2p & (P2P_ENABLED | P2P_GROUP_OWNER)) == P2P_ENABLED)
392                 goto no_beacon;
393 #endif /* CONFIG_P2P */
394
395 #define BEACON_HEAD_BUF_SIZE 256
396 #define BEACON_TAIL_BUF_SIZE 512
397         head = os_zalloc(BEACON_HEAD_BUF_SIZE);
398         tail_len = BEACON_TAIL_BUF_SIZE;
399 #ifdef CONFIG_WPS
400         if (hapd->conf->wps_state && hapd->wps_beacon_ie)
401                 tail_len += wpabuf_len(hapd->wps_beacon_ie);
402 #endif /* CONFIG_WPS */
403 #ifdef CONFIG_P2P
404         if (hapd->p2p_beacon_ie)
405                 tail_len += wpabuf_len(hapd->p2p_beacon_ie);
406 #endif /* CONFIG_P2P */
407         tailpos = tail = os_malloc(tail_len);
408         if (head == NULL || tail == NULL) {
409                 wpa_printf(MSG_ERROR, "Failed to set beacon data");
410                 os_free(head);
411                 os_free(tail);
412                 return;
413         }
414
415         head->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
416                                            WLAN_FC_STYPE_BEACON);
417         head->duration = host_to_le16(0);
418         os_memset(head->da, 0xff, ETH_ALEN);
419
420         os_memcpy(head->sa, hapd->own_addr, ETH_ALEN);
421         os_memcpy(head->bssid, hapd->own_addr, ETH_ALEN);
422         head->u.beacon.beacon_int =
423                 host_to_le16(hapd->iconf->beacon_int);
424
425         /* hardware or low-level driver will setup seq_ctrl and timestamp */
426         capab_info = hostapd_own_capab_info(hapd, NULL, 0);
427         head->u.beacon.capab_info = host_to_le16(capab_info);
428         pos = &head->u.beacon.variable[0];
429
430         /* SSID */
431         *pos++ = WLAN_EID_SSID;
432         if (hapd->conf->ignore_broadcast_ssid == 2) {
433                 /* clear the data, but keep the correct length of the SSID */
434                 *pos++ = hapd->conf->ssid.ssid_len;
435                 os_memset(pos, 0, hapd->conf->ssid.ssid_len);
436                 pos += hapd->conf->ssid.ssid_len;
437         } else if (hapd->conf->ignore_broadcast_ssid) {
438                 *pos++ = 0; /* empty SSID */
439         } else {
440                 *pos++ = hapd->conf->ssid.ssid_len;
441                 os_memcpy(pos, hapd->conf->ssid.ssid,
442                           hapd->conf->ssid.ssid_len);
443                 pos += hapd->conf->ssid.ssid_len;
444         }
445
446         /* Supported rates */
447         pos = hostapd_eid_supp_rates(hapd, pos);
448
449         /* DS Params */
450         pos = hostapd_eid_ds_params(hapd, pos);
451
452         head_len = pos - (u8 *) head;
453
454         tailpos = hostapd_eid_country(hapd, tailpos,
455                                       tail + BEACON_TAIL_BUF_SIZE - tailpos);
456
457         /* ERP Information element */
458         tailpos = hostapd_eid_erp_info(hapd, tailpos);
459
460         /* Extended supported rates */
461         tailpos = hostapd_eid_ext_supp_rates(hapd, tailpos);
462
463         /* RSN, MDIE, WPA */
464         tailpos = hostapd_eid_wpa(hapd, tailpos, tail + BEACON_TAIL_BUF_SIZE -
465                                   tailpos, NULL);
466
467 #ifdef CONFIG_IEEE80211N
468         tailpos = hostapd_eid_ht_capabilities(hapd, tailpos);
469         tailpos = hostapd_eid_ht_operation(hapd, tailpos);
470 #endif /* CONFIG_IEEE80211N */
471
472         /* Wi-Fi Alliance WMM */
473         tailpos = hostapd_eid_wmm(hapd, tailpos);
474
475 #ifdef CONFIG_WPS
476         if (hapd->conf->wps_state && hapd->wps_beacon_ie) {
477                 os_memcpy(tailpos, wpabuf_head(hapd->wps_beacon_ie),
478                           wpabuf_len(hapd->wps_beacon_ie));
479                 tailpos += wpabuf_len(hapd->wps_beacon_ie);
480         }
481 #endif /* CONFIG_WPS */
482
483 #ifdef CONFIG_P2P
484         if ((hapd->conf->p2p & P2P_ENABLED) && hapd->p2p_beacon_ie) {
485                 os_memcpy(tailpos, wpabuf_head(hapd->p2p_beacon_ie),
486                           wpabuf_len(hapd->p2p_beacon_ie));
487                 tailpos += wpabuf_len(hapd->p2p_beacon_ie);
488         }
489 #endif /* CONFIG_P2P */
490
491         tail_len = tailpos > tail ? tailpos - tail : 0;
492
493         if (hapd->drv.set_beacon(hapd, (u8 *) head, head_len,
494                                  tail, tail_len, hapd->conf->dtim_period,
495                                  hapd->iconf->beacon_int))
496                 wpa_printf(MSG_ERROR, "Failed to set beacon head/tail or DTIM "
497                            "period");
498
499         os_free(tail);
500         os_free(head);
501
502 #ifdef CONFIG_P2P
503 no_beacon:
504 #endif /* CONFIG_P2P */
505         hapd->drv.set_bss_params(hapd, !!(ieee802_11_erp_info(hapd) &
506                                           ERP_INFO_USE_PROTECTION));
507 }
508
509
510 void ieee802_11_set_beacons(struct hostapd_iface *iface)
511 {
512         size_t i;
513         for (i = 0; i < iface->num_bss; i++)
514                 ieee802_11_set_beacon(iface->bss[i]);
515 }
516
517 #endif /* CONFIG_NATIVE_WINDOWS */