blob: c416ab605b75120ffb0e62cd757dd7378be406c7 [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * Wi-Fi Direct - P2P provision discovery
3 * Copyright (c) 2009-2010, Atheros Communications
4 *
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007 */
8
9#include "includes.h"
10
11#include "common.h"
12#include "common/ieee802_11_defs.h"
Dmitry Shmidt2e67f062014-07-16 09:55:28 -070013#include "common/wpa_ctrl.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070014#include "wps/wps_defs.h"
15#include "p2p_i.h"
16#include "p2p.h"
17
18
Jouni Malinen75ecf522011-06-27 15:19:46 -070019/*
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070020 * Number of retries to attempt for provision discovery requests
21 * in case the peer is not listening.
Jouni Malinen75ecf522011-06-27 15:19:46 -070022 */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -080023#define MAX_PROV_DISC_REQ_RETRIES 120
Jouni Malinen75ecf522011-06-27 15:19:46 -070024
25
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070026static void p2p_build_wps_ie_config_methods(struct wpabuf *buf,
27 u16 config_methods)
28{
29 u8 *len;
30 wpabuf_put_u8(buf, WLAN_EID_VENDOR_SPECIFIC);
31 len = wpabuf_put(buf, 1);
32 wpabuf_put_be32(buf, WPS_DEV_OUI_WFA);
33
34 /* Config Methods */
35 wpabuf_put_be16(buf, ATTR_CONFIG_METHODS);
36 wpabuf_put_be16(buf, 2);
37 wpabuf_put_be16(buf, config_methods);
38
39 p2p_buf_update_ie_hdr(buf, len);
40}
41
42
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080043static void p2ps_add_new_group_info(struct p2p_data *p2p,
44 struct p2p_device *dev,
45 struct wpabuf *buf)
Dmitry Shmidt216983b2015-02-06 10:50:36 -080046{
47 int found;
48 u8 intended_addr[ETH_ALEN];
Dmitry Shmidt9d9e6022015-04-23 10:34:55 -070049 u8 ssid[SSID_MAX_LEN];
Dmitry Shmidt216983b2015-02-06 10:50:36 -080050 size_t ssid_len;
51 int group_iface;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080052 unsigned int force_freq;
Dmitry Shmidt216983b2015-02-06 10:50:36 -080053
54 if (!p2p->cfg->get_go_info)
55 return;
56
57 found = p2p->cfg->get_go_info(
58 p2p->cfg->cb_ctx, intended_addr, ssid,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080059 &ssid_len, &group_iface, &force_freq);
Dmitry Shmidt216983b2015-02-06 10:50:36 -080060 if (found) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080061 if (force_freq > 0) {
62 p2p->p2ps_prov->force_freq = force_freq;
63 p2p->p2ps_prov->pref_freq = 0;
64
65 if (dev)
66 p2p_prepare_channel(p2p, dev, force_freq, 0, 0);
67 }
Dmitry Shmidt216983b2015-02-06 10:50:36 -080068 p2p_buf_add_group_id(buf, p2p->cfg->dev_addr,
69 ssid, ssid_len);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080070
71 if (group_iface)
72 p2p_buf_add_intended_addr(buf, p2p->intended_addr);
73 else
74 p2p_buf_add_intended_addr(buf, intended_addr);
Dmitry Shmidt216983b2015-02-06 10:50:36 -080075 } else {
76 if (!p2p->ssid_set) {
77 p2p_build_ssid(p2p, p2p->ssid, &p2p->ssid_len);
78 p2p->ssid_set = 1;
79 }
80
81 /* Add pre-composed P2P Group ID */
82 p2p_buf_add_group_id(buf, p2p->cfg->dev_addr,
83 p2p->ssid, p2p->ssid_len);
84
85 if (group_iface)
86 p2p_buf_add_intended_addr(
87 buf, p2p->intended_addr);
88 else
89 p2p_buf_add_intended_addr(
90 buf, p2p->cfg->dev_addr);
91 }
92}
93
94
95static void p2ps_add_pd_req_attrs(struct p2p_data *p2p, struct p2p_device *dev,
96 struct wpabuf *buf, u16 config_methods)
97{
98 struct p2ps_provision *prov = p2p->p2ps_prov;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080099 struct p2ps_feature_capab fcap = { prov->cpt_mask, 0 };
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800100 int shared_group = 0;
Dmitry Shmidt9d9e6022015-04-23 10:34:55 -0700101 u8 ssid[SSID_MAX_LEN];
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800102 size_t ssid_len;
103 u8 go_dev_addr[ETH_ALEN];
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800104 u8 intended_addr[ETH_ALEN];
105 int follow_on_req_fail = prov->status >= 0 &&
106 prov->status != P2P_SC_SUCCESS_DEFERRED;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800107
108 /* If we might be explicite group owner, add GO details */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800109 if (!follow_on_req_fail &&
110 (prov->conncap & (P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_NEW)))
111 p2ps_add_new_group_info(p2p, dev, buf);
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800112
113 if (prov->status >= 0)
114 p2p_buf_add_status(buf, (u8) prov->status);
115 else
116 prov->method = config_methods;
117
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800118 if (!follow_on_req_fail) {
119 if (p2p->cfg->get_persistent_group) {
120 shared_group = p2p->cfg->get_persistent_group(
121 p2p->cfg->cb_ctx, dev->info.p2p_device_addr,
122 NULL, 0, go_dev_addr, ssid, &ssid_len,
123 intended_addr);
124 }
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800125
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800126 if (shared_group ||
127 (prov->conncap & (P2PS_SETUP_CLIENT | P2PS_SETUP_NEW)))
128 p2p_buf_add_channel_list(buf, p2p->cfg->country,
129 &p2p->channels);
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800130
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800131 if ((shared_group && !is_zero_ether_addr(intended_addr)) ||
132 (prov->conncap & (P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_NEW)))
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800133 p2p_buf_add_operating_channel(buf, p2p->cfg->country,
134 p2p->op_reg_class,
135 p2p->op_channel);
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800136 }
137
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800138 if (prov->status < 0 && prov->info[0])
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800139 p2p_buf_add_session_info(buf, prov->info);
140
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800141 if (!follow_on_req_fail)
142 p2p_buf_add_connection_capability(buf, prov->conncap);
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800143
144 p2p_buf_add_advertisement_id(buf, prov->adv_id, prov->adv_mac);
145
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800146 if (!follow_on_req_fail) {
147 if (shared_group || prov->conncap == P2PS_SETUP_NEW ||
148 prov->conncap ==
149 (P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_NEW) ||
150 prov->conncap ==
151 (P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_CLIENT)) {
152 /* Add Config Timeout */
153 p2p_buf_add_config_timeout(buf, p2p->go_timeout,
154 p2p->client_timeout);
155 }
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800156
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800157 p2p_buf_add_listen_channel(buf, p2p->cfg->country,
158 p2p->cfg->reg_class,
159 p2p->cfg->channel);
160 }
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800161
162 p2p_buf_add_session_id(buf, prov->session_id, prov->session_mac);
163
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800164 p2p_buf_add_feature_capability(buf, sizeof(fcap), (const u8 *) &fcap);
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800165
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800166 if (shared_group) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800167 p2p_buf_add_persistent_group_info(buf, go_dev_addr,
168 ssid, ssid_len);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800169 /* Add intended interface address if it is not added yet */
170 if ((prov->conncap == P2PS_SETUP_NONE ||
171 prov->conncap == P2PS_SETUP_CLIENT) &&
172 !is_zero_ether_addr(intended_addr))
173 p2p_buf_add_intended_addr(buf, intended_addr);
174 }
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800175}
176
177
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700178static struct wpabuf * p2p_build_prov_disc_req(struct p2p_data *p2p,
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800179 struct p2p_device *dev,
180 int join)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700181{
182 struct wpabuf *buf;
183 u8 *len;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700184 size_t extra = 0;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800185 u8 dialog_token = dev->dialog_token;
186 u16 config_methods = dev->req_config_methods;
187 struct p2p_device *go = join ? dev : NULL;
188 u8 group_capab;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700189
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700190#ifdef CONFIG_WIFI_DISPLAY
191 if (p2p->wfd_ie_prov_disc_req)
192 extra = wpabuf_len(p2p->wfd_ie_prov_disc_req);
193#endif /* CONFIG_WIFI_DISPLAY */
194
Dmitry Shmidt2e67f062014-07-16 09:55:28 -0700195 if (p2p->vendor_elem && p2p->vendor_elem[VENDOR_ELEM_P2P_PD_REQ])
196 extra += wpabuf_len(p2p->vendor_elem[VENDOR_ELEM_P2P_PD_REQ]);
197
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800198 if (p2p->p2ps_prov)
199 extra += os_strlen(p2p->p2ps_prov->info) + 1 +
200 sizeof(struct p2ps_provision);
201
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700202 buf = wpabuf_alloc(1000 + extra);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700203 if (buf == NULL)
204 return NULL;
205
206 p2p_buf_add_public_action_hdr(buf, P2P_PROV_DISC_REQ, dialog_token);
207
208 len = p2p_buf_add_ie_hdr(buf);
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800209
210 group_capab = 0;
211 if (p2p->p2ps_prov) {
212 group_capab |= P2P_GROUP_CAPAB_PERSISTENT_GROUP;
213 group_capab |= P2P_GROUP_CAPAB_PERSISTENT_RECONN;
214 if (p2p->cross_connect)
215 group_capab |= P2P_GROUP_CAPAB_CROSS_CONN;
216 if (p2p->cfg->p2p_intra_bss)
217 group_capab |= P2P_GROUP_CAPAB_INTRA_BSS_DIST;
218 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700219 p2p_buf_add_capability(buf, p2p->dev_capab &
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800220 ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY,
221 group_capab);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700222 p2p_buf_add_device_info(buf, p2p, NULL);
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800223 if (p2p->p2ps_prov) {
224 p2ps_add_pd_req_attrs(p2p, dev, buf, config_methods);
225 } else if (go) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700226 p2p_buf_add_group_id(buf, go->info.p2p_device_addr,
227 go->oper_ssid, go->oper_ssid_len);
228 }
229 p2p_buf_update_ie_hdr(buf, len);
230
231 /* WPS IE with Config Methods attribute */
232 p2p_build_wps_ie_config_methods(buf, config_methods);
233
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700234#ifdef CONFIG_WIFI_DISPLAY
235 if (p2p->wfd_ie_prov_disc_req)
236 wpabuf_put_buf(buf, p2p->wfd_ie_prov_disc_req);
237#endif /* CONFIG_WIFI_DISPLAY */
238
Dmitry Shmidt2e67f062014-07-16 09:55:28 -0700239 if (p2p->vendor_elem && p2p->vendor_elem[VENDOR_ELEM_P2P_PD_REQ])
240 wpabuf_put_buf(buf, p2p->vendor_elem[VENDOR_ELEM_P2P_PD_REQ]);
241
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700242 return buf;
243}
244
245
246static struct wpabuf * p2p_build_prov_disc_resp(struct p2p_data *p2p,
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800247 struct p2p_device *dev,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700248 u8 dialog_token,
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800249 enum p2p_status_code status,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700250 u16 config_methods,
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800251 u32 adv_id,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700252 const u8 *group_id,
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800253 size_t group_id_len,
254 const u8 *persist_ssid,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800255 size_t persist_ssid_len,
256 const u8 *fcap,
257 u16 fcap_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700258{
259 struct wpabuf *buf;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700260 size_t extra = 0;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800261 int persist = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700262
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700263#ifdef CONFIG_WIFI_DISPLAY
264 struct wpabuf *wfd_ie = p2p->wfd_ie_prov_disc_resp;
265 if (wfd_ie && group_id) {
266 size_t i;
267 for (i = 0; i < p2p->num_groups; i++) {
268 struct p2p_group *g = p2p->groups[i];
269 struct wpabuf *ie;
270 if (!p2p_group_is_group_id_match(g, group_id,
271 group_id_len))
272 continue;
273 ie = p2p_group_get_wfd_ie(g);
274 if (ie) {
275 wfd_ie = ie;
276 break;
277 }
278 }
279 }
280 if (wfd_ie)
281 extra = wpabuf_len(wfd_ie);
282#endif /* CONFIG_WIFI_DISPLAY */
283
Dmitry Shmidt2e67f062014-07-16 09:55:28 -0700284 if (p2p->vendor_elem && p2p->vendor_elem[VENDOR_ELEM_P2P_PD_RESP])
285 extra += wpabuf_len(p2p->vendor_elem[VENDOR_ELEM_P2P_PD_RESP]);
286
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800287 buf = wpabuf_alloc(1000 + extra);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700288 if (buf == NULL)
289 return NULL;
290
291 p2p_buf_add_public_action_hdr(buf, P2P_PROV_DISC_RESP, dialog_token);
292
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800293 /* Add P2P IE for P2PS */
294 if (p2p->p2ps_prov && p2p->p2ps_prov->adv_id == adv_id) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800295 u8 *len = p2p_buf_add_ie_hdr(buf);
296 struct p2ps_provision *prov = p2p->p2ps_prov;
297 u8 group_capab;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800298 u8 conncap = 0;
299
300 if (status == P2P_SC_SUCCESS ||
301 status == P2P_SC_SUCCESS_DEFERRED)
302 conncap = prov->conncap;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800303
304 if (!status && prov->status != -1)
305 status = prov->status;
306
307 p2p_buf_add_status(buf, status);
308 group_capab = P2P_GROUP_CAPAB_PERSISTENT_GROUP |
309 P2P_GROUP_CAPAB_PERSISTENT_RECONN;
310 if (p2p->cross_connect)
311 group_capab |= P2P_GROUP_CAPAB_CROSS_CONN;
312 if (p2p->cfg->p2p_intra_bss)
313 group_capab |= P2P_GROUP_CAPAB_INTRA_BSS_DIST;
314 p2p_buf_add_capability(buf, p2p->dev_capab &
315 ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY,
316 group_capab);
317 p2p_buf_add_device_info(buf, p2p, NULL);
318
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800319 if (persist_ssid && p2p->cfg->get_persistent_group && dev &&
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800320 (status == P2P_SC_SUCCESS ||
321 status == P2P_SC_SUCCESS_DEFERRED)) {
Dmitry Shmidt9d9e6022015-04-23 10:34:55 -0700322 u8 ssid[SSID_MAX_LEN];
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800323 size_t ssid_len;
324 u8 go_dev_addr[ETH_ALEN];
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800325 u8 intended_addr[ETH_ALEN];
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800326
327 persist = p2p->cfg->get_persistent_group(
328 p2p->cfg->cb_ctx,
329 dev->info.p2p_device_addr,
330 persist_ssid, persist_ssid_len, go_dev_addr,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800331 ssid, &ssid_len, intended_addr);
332 if (persist) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800333 p2p_buf_add_persistent_group_info(
334 buf, go_dev_addr, ssid, ssid_len);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800335 if (!is_zero_ether_addr(intended_addr))
336 p2p_buf_add_intended_addr(
337 buf, intended_addr);
338 }
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800339 }
340
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800341 if (!persist && (conncap & P2PS_SETUP_GROUP_OWNER))
342 p2ps_add_new_group_info(p2p, dev, buf);
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800343
344 /* Add Operating Channel if conncap indicates GO */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800345 if (persist || (conncap & P2PS_SETUP_GROUP_OWNER)) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800346 if (p2p->op_reg_class && p2p->op_channel)
347 p2p_buf_add_operating_channel(
348 buf, p2p->cfg->country,
349 p2p->op_reg_class,
350 p2p->op_channel);
351 else
352 p2p_buf_add_operating_channel(
353 buf, p2p->cfg->country,
354 p2p->cfg->op_reg_class,
355 p2p->cfg->op_channel);
356 }
357
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800358 if (persist ||
359 (conncap & (P2PS_SETUP_CLIENT | P2PS_SETUP_GROUP_OWNER)))
360 p2p_buf_add_channel_list(buf, p2p->cfg->country,
361 &p2p->channels);
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800362
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800363 if (!persist && conncap)
364 p2p_buf_add_connection_capability(buf, conncap);
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800365
366 p2p_buf_add_advertisement_id(buf, adv_id, prov->adv_mac);
367
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800368 if (persist ||
369 (conncap & (P2PS_SETUP_CLIENT | P2PS_SETUP_GROUP_OWNER)))
370 p2p_buf_add_config_timeout(buf, p2p->go_timeout,
371 p2p->client_timeout);
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800372
373 p2p_buf_add_session_id(buf, prov->session_id,
374 prov->session_mac);
375
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800376 p2p_buf_add_feature_capability(buf, fcap_len, fcap);
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800377 p2p_buf_update_ie_hdr(buf, len);
378 } else if (status != P2P_SC_SUCCESS || adv_id) {
379 u8 *len = p2p_buf_add_ie_hdr(buf);
380
381 p2p_buf_add_status(buf, status);
382
383 if (p2p->p2ps_prov)
384 p2p_buf_add_advertisement_id(buf, adv_id,
385 p2p->p2ps_prov->adv_mac);
386
387 p2p_buf_update_ie_hdr(buf, len);
388 }
389
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700390 /* WPS IE with Config Methods attribute */
391 p2p_build_wps_ie_config_methods(buf, config_methods);
392
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700393#ifdef CONFIG_WIFI_DISPLAY
394 if (wfd_ie)
395 wpabuf_put_buf(buf, wfd_ie);
396#endif /* CONFIG_WIFI_DISPLAY */
397
Dmitry Shmidt2e67f062014-07-16 09:55:28 -0700398 if (p2p->vendor_elem && p2p->vendor_elem[VENDOR_ELEM_P2P_PD_RESP])
399 wpabuf_put_buf(buf, p2p->vendor_elem[VENDOR_ELEM_P2P_PD_RESP]);
400
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700401 return buf;
402}
403
404
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800405static int p2ps_setup_p2ps_prov(struct p2p_data *p2p, u32 adv_id,
406 u32 session_id, u16 method,
407 const u8 *session_mac, const u8 *adv_mac)
408{
409 struct p2ps_provision *tmp;
410
411 if (!p2p->p2ps_prov) {
412 p2p->p2ps_prov = os_zalloc(sizeof(struct p2ps_provision) + 1);
413 if (!p2p->p2ps_prov)
414 return -1;
415 } else {
416 os_memset(p2p->p2ps_prov, 0, sizeof(struct p2ps_provision) + 1);
417 }
418
419 tmp = p2p->p2ps_prov;
420 tmp->adv_id = adv_id;
421 tmp->session_id = session_id;
422 tmp->method = method;
423 os_memcpy(tmp->session_mac, session_mac, ETH_ALEN);
424 os_memcpy(tmp->adv_mac, adv_mac, ETH_ALEN);
425 tmp->info[0] = '\0';
426
427 return 0;
428}
429
430
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800431static u8 p2ps_own_preferred_cpt(const u8 *cpt_priority, u8 req_cpt_mask)
432{
433 int i;
434
435 for (i = 0; cpt_priority[i]; i++)
436 if (req_cpt_mask & cpt_priority[i])
437 return cpt_priority[i];
438
439 return 0;
440}
441
442
443/* Check if the message contains a valid P2PS PD Request */
444static int p2ps_validate_pd_req(struct p2p_data *p2p, struct p2p_message *msg,
445 const u8 *addr)
446{
447 u8 group_id = 0;
448 u8 intended_addr = 0;
449 u8 operating_channel = 0;
450 u8 channel_list = 0;
451 u8 config_timeout = 0;
452 u8 listen_channel = 0;
453
454#define P2PS_PD_REQ_CHECK(_val, _attr) \
455do { \
456 if ((_val) && !msg->_attr) { \
457 p2p_dbg(p2p, "Not P2PS PD Request. Missing %s", #_attr); \
458 return -1; \
459 } \
460} while (0)
461
462 P2PS_PD_REQ_CHECK(1, adv_id);
463 P2PS_PD_REQ_CHECK(1, session_id);
464 P2PS_PD_REQ_CHECK(1, session_mac);
465 P2PS_PD_REQ_CHECK(1, adv_mac);
466 P2PS_PD_REQ_CHECK(1, capability);
467 P2PS_PD_REQ_CHECK(1, p2p_device_info);
468 P2PS_PD_REQ_CHECK(1, feature_cap);
469
470 /*
471 * We don't need to check Connection Capability, Persistent Group,
472 * and related attributes for follow-on PD Request with a status
473 * other than SUCCESS_DEFERRED.
474 */
475 if (msg->status && *msg->status != P2P_SC_SUCCESS_DEFERRED)
476 return 0;
477
478 P2PS_PD_REQ_CHECK(1, conn_cap);
479
480 /*
481 * Note 1: A feature capability attribute structure can be changed
482 * in the future. The assumption is that such modifications are
483 * backward compatible, therefore we allow processing of msg.feature_cap
484 * exceeding the size of the p2ps_feature_capab structure.
485 * Note 2: Verification of msg.feature_cap_len below has to be changed
486 * to allow 2 byte feature capability processing if
487 * struct p2ps_feature_capab is extended to include additional fields
488 * and it affects the structure size.
489 */
490 if (msg->feature_cap_len < sizeof(struct p2ps_feature_capab)) {
491 p2p_dbg(p2p, "P2PS: Invalid feature capability len");
492 return -1;
493 }
494
495 switch (*msg->conn_cap) {
496 case P2PS_SETUP_NEW:
497 group_id = 1;
498 intended_addr = 1;
499 operating_channel = 1;
500 channel_list = 1;
501 config_timeout = 1;
502 listen_channel = 1;
503 break;
504 case P2PS_SETUP_CLIENT:
505 channel_list = 1;
506 listen_channel = 1;
507 break;
508 case P2PS_SETUP_GROUP_OWNER:
509 group_id = 1;
510 intended_addr = 1;
511 operating_channel = 1;
512 break;
513 case P2PS_SETUP_NEW | P2PS_SETUP_GROUP_OWNER:
514 group_id = 1;
515 operating_channel = 1;
516 intended_addr = 1;
517 channel_list = 1;
518 config_timeout = 1;
519 break;
520 case P2PS_SETUP_CLIENT | P2PS_SETUP_GROUP_OWNER:
521 group_id = 1;
522 intended_addr = 1;
523 operating_channel = 1;
524 channel_list = 1;
525 config_timeout = 1;
526 break;
527 default:
528 p2p_dbg(p2p, "Invalid P2PS PD connection capability");
529 return -1;
530 }
531
532 if (msg->persistent_dev) {
533 channel_list = 1;
534 config_timeout = 1;
535 if (os_memcmp(msg->persistent_dev, addr, ETH_ALEN) == 0) {
536 intended_addr = 1;
537 operating_channel = 1;
538 }
539 }
540
541 P2PS_PD_REQ_CHECK(group_id, group_id);
542 P2PS_PD_REQ_CHECK(intended_addr, intended_addr);
543 P2PS_PD_REQ_CHECK(operating_channel, operating_channel);
544 P2PS_PD_REQ_CHECK(channel_list, channel_list);
545 P2PS_PD_REQ_CHECK(config_timeout, config_timeout);
546 P2PS_PD_REQ_CHECK(listen_channel, listen_channel);
547
548#undef P2PS_PD_REQ_CHECK
549
550 return 0;
551}
552
553
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700554void p2p_process_prov_disc_req(struct p2p_data *p2p, const u8 *sa,
555 const u8 *data, size_t len, int rx_freq)
556{
557 struct p2p_message msg;
558 struct p2p_device *dev;
559 int freq;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800560 enum p2p_status_code reject = P2P_SC_FAIL_INCOMPATIBLE_PARAMS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700561 struct wpabuf *resp;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800562 u32 adv_id = 0;
563 struct p2ps_advertisement *p2ps_adv = NULL;
564 u8 conncap = P2PS_SETUP_NEW;
565 u8 auto_accept = 0;
566 u32 session_id = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800567 u8 session_mac[ETH_ALEN] = { 0, 0, 0, 0, 0, 0 };
568 u8 adv_mac[ETH_ALEN] = { 0, 0, 0, 0, 0, 0 };
569 const u8 *group_mac;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800570 int passwd_id = DEV_PW_DEFAULT;
571 u16 config_methods;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800572 u16 allowed_config_methods = WPS_CONFIG_DISPLAY | WPS_CONFIG_KEYPAD;
573 struct p2ps_feature_capab resp_fcap = { 0, 0 };
574 struct p2ps_feature_capab *req_fcap = NULL;
575 u8 remote_conncap;
576 u16 method;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700577
578 if (p2p_parse(data, len, &msg))
579 return;
580
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700581 p2p_dbg(p2p, "Received Provision Discovery Request from " MACSTR
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700582 " with config methods 0x%x (freq=%d)",
583 MAC2STR(sa), msg.wps_config_methods, rx_freq);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800584 group_mac = msg.intended_addr;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700585
586 dev = p2p_get_device(p2p, sa);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800587 if (dev == NULL || (dev->flags & P2P_DEV_PROBE_REQ_ONLY)) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700588 p2p_dbg(p2p, "Provision Discovery Request from unknown peer "
589 MACSTR, MAC2STR(sa));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800590
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800591 if (p2p_add_device(p2p, sa, rx_freq, NULL, 0, data + 1, len - 1,
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800592 0)) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700593 p2p_dbg(p2p, "Provision Discovery Request add device failed "
594 MACSTR, MAC2STR(sa));
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800595 goto out;
596 }
597
598 if (!dev) {
599 dev = p2p_get_device(p2p, sa);
600 if (!dev) {
601 p2p_dbg(p2p,
602 "Provision Discovery device not found "
603 MACSTR, MAC2STR(sa));
604 goto out;
605 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700606 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700607 } else if (msg.wfd_subelems) {
608 wpabuf_free(dev->info.wfd_subelems);
609 dev->info.wfd_subelems = wpabuf_dup(msg.wfd_subelems);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700610 }
611
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800612 if (!msg.adv_id) {
613 allowed_config_methods |= WPS_CONFIG_PUSHBUTTON;
614 if (!(msg.wps_config_methods & allowed_config_methods)) {
615 p2p_dbg(p2p,
616 "Unsupported Config Methods in Provision Discovery Request");
Dmitry Shmidt04949592012-07-19 12:16:46 -0700617 goto out;
618 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700619
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800620 /* Legacy (non-P2PS) - Unknown groups allowed for P2PS */
621 if (msg.group_id) {
622 size_t i;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800623
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800624 for (i = 0; i < p2p->num_groups; i++) {
625 if (p2p_group_is_group_id_match(
626 p2p->groups[i],
627 msg.group_id, msg.group_id_len))
628 break;
629 }
630 if (i == p2p->num_groups) {
631 p2p_dbg(p2p,
632 "PD request for unknown P2P Group ID - reject");
633 goto out;
634 }
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800635 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800636 } else {
637 allowed_config_methods |= WPS_CONFIG_P2PS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700638
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800639 /*
640 * Set adv_id here, so in case of an error, a P2PS PD Response
641 * will be sent.
642 */
643 adv_id = WPA_GET_LE32(msg.adv_id);
644 if (p2ps_validate_pd_req(p2p, &msg, sa) < 0) {
645 reject = P2P_SC_FAIL_INVALID_PARAMS;
646 goto out;
647 }
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800648
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800649 req_fcap = (struct p2ps_feature_capab *) msg.feature_cap;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800650
651 os_memcpy(session_mac, msg.session_mac, ETH_ALEN);
652 os_memcpy(adv_mac, msg.adv_mac, ETH_ALEN);
653
654 session_id = WPA_GET_LE32(msg.session_id);
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800655
656 if (msg.conn_cap)
657 conncap = *msg.conn_cap;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800658
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800659 /*
660 * We need to verify a P2PS config methog in an initial PD
661 * request or in a follow-on PD request with the status
662 * SUCCESS_DEFERRED.
663 */
664 if ((!msg.status || *msg.status == P2P_SC_SUCCESS_DEFERRED) &&
665 !(msg.wps_config_methods & allowed_config_methods)) {
666 p2p_dbg(p2p,
667 "Unsupported Config Methods in Provision Discovery Request");
668 goto out;
669 }
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800670
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800671 /*
672 * TODO: since we don't support multiple PD, reject PD request
673 * if we are in the middle of P2PS PD with some other peer
674 */
675 }
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800676
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800677 dev->flags &= ~(P2P_DEV_PD_PEER_DISPLAY |
678 P2P_DEV_PD_PEER_KEYPAD |
679 P2P_DEV_PD_PEER_P2PS);
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800680
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800681 if (msg.wps_config_methods & WPS_CONFIG_DISPLAY) {
682 p2p_dbg(p2p, "Peer " MACSTR
683 " requested us to show a PIN on display", MAC2STR(sa));
684 dev->flags |= P2P_DEV_PD_PEER_KEYPAD;
685 passwd_id = DEV_PW_USER_SPECIFIED;
686 } else if (msg.wps_config_methods & WPS_CONFIG_KEYPAD) {
687 p2p_dbg(p2p, "Peer " MACSTR
688 " requested us to write its PIN using keypad",
689 MAC2STR(sa));
690 dev->flags |= P2P_DEV_PD_PEER_DISPLAY;
691 passwd_id = DEV_PW_REGISTRAR_SPECIFIED;
692 } else if (msg.wps_config_methods & WPS_CONFIG_P2PS) {
693 p2p_dbg(p2p, "Peer " MACSTR " requesting P2PS PIN",
694 MAC2STR(sa));
695 dev->flags |= P2P_DEV_PD_PEER_P2PS;
696 passwd_id = DEV_PW_P2PS_DEFAULT;
697 }
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800698
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800699 /* Remove stale persistent groups */
700 if (p2p->cfg->remove_stale_groups) {
701 p2p->cfg->remove_stale_groups(
702 p2p->cfg->cb_ctx, dev->info.p2p_device_addr,
703 msg.persistent_dev,
704 msg.persistent_ssid, msg.persistent_ssid_len);
705 }
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800706
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800707 reject = P2P_SC_SUCCESS;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800708
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800709 /*
710 * End of a legacy P2P PD Request processing, from this point continue
711 * with P2PS one.
712 */
713 if (!msg.adv_id)
714 goto out;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800715
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800716 remote_conncap = conncap;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800717
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800718 if (!msg.status) {
719 unsigned int forced_freq, pref_freq;
720
721 if (os_memcmp(p2p->cfg->dev_addr, msg.adv_mac, ETH_ALEN)) {
722 p2p_dbg(p2p,
723 "P2PS PD adv mac does not match the local one");
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800724 reject = P2P_SC_FAIL_INCOMPATIBLE_PARAMS;
725 goto out;
726 }
727
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800728 p2ps_adv = p2p_service_p2ps_id(p2p, adv_id);
729 if (!p2ps_adv) {
730 p2p_dbg(p2p, "P2PS PD invalid adv_id=0x%X", adv_id);
731 reject = P2P_SC_FAIL_INCOMPATIBLE_PARAMS;
732 goto out;
733 }
734 p2p_dbg(p2p, "adv_id: 0x%X, p2ps_adv: %p", adv_id, p2ps_adv);
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800735
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800736 auto_accept = p2ps_adv->auto_accept;
737 conncap = p2p->cfg->p2ps_group_capability(p2p->cfg->cb_ctx,
738 conncap, auto_accept,
739 &forced_freq,
740 &pref_freq);
741
742 p2p_dbg(p2p, "Conncap: local:%d remote:%d result:%d",
743 auto_accept, remote_conncap, conncap);
744
745 p2p_prepare_channel(p2p, dev, forced_freq, pref_freq, 0);
746
747 resp_fcap.cpt = p2ps_own_preferred_cpt(p2ps_adv->cpt_priority,
748 req_fcap->cpt);
749
750 p2p_dbg(p2p, "cpt: service:0x%x remote:0x%x result:0x%x",
751 p2ps_adv->cpt_mask, req_fcap->cpt, resp_fcap.cpt);
752
753 if (!resp_fcap.cpt) {
754 p2p_dbg(p2p,
755 "Incompatible P2PS feature capability CPT bitmask");
756 reject = P2P_SC_FAIL_INCOMPATIBLE_PARAMS;
757 } else if (p2ps_adv->config_methods &&
758 !(msg.wps_config_methods &
759 p2ps_adv->config_methods)) {
760 p2p_dbg(p2p,
761 "Unsupported config methods in Provision Discovery Request (own=0x%x peer=0x%x)",
762 p2ps_adv->config_methods,
763 msg.wps_config_methods);
764 reject = P2P_SC_FAIL_INCOMPATIBLE_PARAMS;
765 } else if (!p2ps_adv->state) {
766 p2p_dbg(p2p, "P2PS state unavailable");
767 reject = P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
768 } else if (!conncap) {
769 p2p_dbg(p2p, "Conncap resolution failed");
770 reject = P2P_SC_FAIL_INCOMPATIBLE_PARAMS;
771 }
772
773 if (msg.wps_config_methods & WPS_CONFIG_KEYPAD) {
774 p2p_dbg(p2p, "Keypad - always defer");
775 auto_accept = 0;
776 }
777
778 if ((remote_conncap & (P2PS_SETUP_NEW | P2PS_SETUP_CLIENT) ||
779 msg.persistent_dev) && conncap != P2PS_SETUP_NEW &&
780 msg.channel_list && msg.channel_list_len &&
781 p2p_peer_channels_check(p2p, &p2p->channels, dev,
782 msg.channel_list,
783 msg.channel_list_len) < 0) {
784 p2p_dbg(p2p,
785 "No common channels - force deferred flow");
786 auto_accept = 0;
787 }
788
789 if (((remote_conncap & P2PS_SETUP_GROUP_OWNER) ||
790 msg.persistent_dev) && msg.operating_channel) {
791 struct p2p_channels intersect;
792
793 /*
794 * There are cases where only the operating channel is
795 * provided. This requires saving the channel as the
796 * supported channel list, and verifying that it is
797 * supported.
798 */
799 if (dev->channels.reg_classes == 0 ||
800 !p2p_channels_includes(&dev->channels,
801 msg.operating_channel[3],
802 msg.operating_channel[4])) {
803 struct p2p_channels *ch = &dev->channels;
804
805 os_memset(ch, 0, sizeof(*ch));
806 ch->reg_class[0].reg_class =
807 msg.operating_channel[3];
808 ch->reg_class[0].channel[0] =
809 msg.operating_channel[4];
810 ch->reg_class[0].channels = 1;
811 ch->reg_classes = 1;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800812 }
813
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800814 p2p_channels_intersect(&p2p->channels, &dev->channels,
815 &intersect);
816
817 if (intersect.reg_classes == 0) {
818 p2p_dbg(p2p,
819 "No common channels - force deferred flow");
820 auto_accept = 0;
821 }
822 }
823
824 if (auto_accept || reject != P2P_SC_SUCCESS) {
825 struct p2ps_provision *tmp;
826
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800827 if (p2ps_setup_p2ps_prov(p2p, adv_id, session_id,
828 msg.wps_config_methods,
829 session_mac, adv_mac) < 0) {
830 reject = P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
831 goto out;
832 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800833
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800834 tmp = p2p->p2ps_prov;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800835 tmp->force_freq = forced_freq;
836 tmp->pref_freq = pref_freq;
837 if (conncap) {
838 tmp->conncap = conncap;
839 tmp->status = P2P_SC_SUCCESS;
840 } else {
841 tmp->conncap = auto_accept;
842 tmp->status = P2P_SC_FAIL_INCOMPATIBLE_PARAMS;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800843 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800844
845 if (reject != P2P_SC_SUCCESS)
846 goto out;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800847 }
848 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700849
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800850 if (!msg.status && !auto_accept &&
851 (!p2p->p2ps_prov || p2p->p2ps_prov->adv_id != adv_id)) {
852 struct p2ps_provision *tmp;
853
854 if (!conncap) {
855 reject = P2P_SC_FAIL_INCOMPATIBLE_PARAMS;
856 goto out;
857 }
858
859 if (p2ps_setup_p2ps_prov(p2p, adv_id, session_id,
860 msg.wps_config_methods,
861 session_mac, adv_mac) < 0) {
862 reject = P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
863 goto out;
864 }
865 tmp = p2p->p2ps_prov;
866 reject = P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
867 tmp->status = reject;
868 }
869
870 /* Not a P2PS Follow-on PD */
871 if (!msg.status)
872 goto out;
873
874 if (*msg.status && *msg.status != P2P_SC_SUCCESS_DEFERRED) {
875 reject = *msg.status;
876 goto out;
877 }
878
879 if (*msg.status != P2P_SC_SUCCESS_DEFERRED || !p2p->p2ps_prov)
880 goto out;
881
882 if (p2p->p2ps_prov->adv_id != adv_id ||
883 os_memcmp(p2p->p2ps_prov->adv_mac, msg.adv_mac, ETH_ALEN)) {
884 p2p_dbg(p2p,
885 "P2PS Follow-on PD with mismatch Advertisement ID/MAC");
886 goto out;
887 }
888
889 if (p2p->p2ps_prov->session_id != session_id ||
890 os_memcmp(p2p->p2ps_prov->session_mac, msg.session_mac, ETH_ALEN)) {
891 p2p_dbg(p2p, "P2PS Follow-on PD with mismatch Session ID/MAC");
892 goto out;
893 }
894
895 method = p2p->p2ps_prov->method;
896
897 conncap = p2p->cfg->p2ps_group_capability(p2p->cfg->cb_ctx,
898 remote_conncap,
899 p2p->p2ps_prov->conncap,
900 &p2p->p2ps_prov->force_freq,
901 &p2p->p2ps_prov->pref_freq);
902
903 resp_fcap.cpt = p2ps_own_preferred_cpt(p2p->p2ps_prov->cpt_priority,
904 req_fcap->cpt);
905
906 p2p_dbg(p2p, "cpt: local:0x%x remote:0x%x result:0x%x",
907 p2p->p2ps_prov->cpt_mask, req_fcap->cpt, resp_fcap.cpt);
908
909 p2p_prepare_channel(p2p, dev, p2p->p2ps_prov->force_freq,
910 p2p->p2ps_prov->pref_freq, 0);
911
912 /*
913 * Ensure that if we asked for PIN originally, our method is consistent
914 * with original request.
915 */
916 if (method & WPS_CONFIG_DISPLAY)
917 method = WPS_CONFIG_KEYPAD;
918 else if (method & WPS_CONFIG_KEYPAD)
919 method = WPS_CONFIG_DISPLAY;
920
921 if (!conncap || !(msg.wps_config_methods & method)) {
922 /*
923 * Reject this "Deferred Accept*
924 * if incompatible conncap or method
925 */
926 reject = P2P_SC_FAIL_INCOMPATIBLE_PARAMS;
927 } else if (!resp_fcap.cpt) {
928 p2p_dbg(p2p,
929 "Incompatible P2PS feature capability CPT bitmask");
930 reject = P2P_SC_FAIL_INCOMPATIBLE_PARAMS;
931 } else if ((remote_conncap & (P2PS_SETUP_NEW | P2PS_SETUP_CLIENT) ||
932 msg.persistent_dev) && conncap != P2PS_SETUP_NEW &&
933 msg.channel_list && msg.channel_list_len &&
934 p2p_peer_channels_check(p2p, &p2p->channels, dev,
935 msg.channel_list,
936 msg.channel_list_len) < 0) {
937 p2p_dbg(p2p,
938 "No common channels in Follow-On Provision Discovery Request");
939 reject = P2P_SC_FAIL_NO_COMMON_CHANNELS;
940 } else {
941 reject = P2P_SC_SUCCESS;
942 }
943
944 dev->oper_freq = 0;
945 if (reject == P2P_SC_SUCCESS || reject == P2P_SC_SUCCESS_DEFERRED) {
946 u8 tmp;
947
948 if (msg.operating_channel)
949 dev->oper_freq =
950 p2p_channel_to_freq(msg.operating_channel[3],
951 msg.operating_channel[4]);
952
953 if ((conncap & P2PS_SETUP_GROUP_OWNER) &&
954 p2p_go_select_channel(p2p, dev, &tmp) < 0)
955 reject = P2P_SC_FAIL_NO_COMMON_CHANNELS;
956 }
957
958 p2p->p2ps_prov->status = reject;
959 p2p->p2ps_prov->conncap = conncap;
960
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700961out:
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800962 if (reject == P2P_SC_SUCCESS ||
963 reject == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE)
964 config_methods = msg.wps_config_methods;
965 else
966 config_methods = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700967
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800968 /*
969 * Send PD Response for an initial PD Request or for follow-on
970 * PD Request with P2P_SC_SUCCESS_DEFERRED status.
971 */
972 if (!msg.status || *msg.status == P2P_SC_SUCCESS_DEFERRED) {
973 resp = p2p_build_prov_disc_resp(p2p, dev, msg.dialog_token,
974 reject, config_methods, adv_id,
975 msg.group_id, msg.group_id_len,
976 msg.persistent_ssid,
977 msg.persistent_ssid_len,
978 (const u8 *) &resp_fcap,
979 sizeof(resp_fcap));
980 if (!resp) {
981 p2p_parse_free(&msg);
982 return;
983 }
984 p2p_dbg(p2p, "Sending Provision Discovery Response");
985 if (rx_freq > 0)
986 freq = rx_freq;
987 else
988 freq = p2p_channel_to_freq(p2p->cfg->reg_class,
989 p2p->cfg->channel);
990 if (freq < 0) {
991 p2p_dbg(p2p, "Unknown regulatory class/channel");
992 wpabuf_free(resp);
993 p2p_parse_free(&msg);
994 return;
995 }
996 p2p->pending_action_state = P2P_PENDING_PD_RESPONSE;
997 if (p2p_send_action(p2p, freq, sa, p2p->cfg->dev_addr,
998 p2p->cfg->dev_addr,
999 wpabuf_head(resp), wpabuf_len(resp),
1000 200) < 0)
1001 p2p_dbg(p2p, "Failed to send Action frame");
1002 else
1003 p2p->send_action_in_progress = 1;
1004
1005 wpabuf_free(resp);
1006 }
1007
1008 if (!dev) {
1009 p2p_parse_free(&msg);
1010 return;
1011 }
1012
1013 freq = 0;
1014 if (reject == P2P_SC_SUCCESS && conncap == P2PS_SETUP_GROUP_OWNER) {
1015 freq = p2p_channel_to_freq(p2p->op_reg_class,
1016 p2p->op_channel);
1017 if (freq < 0)
1018 freq = 0;
1019 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001020
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001021 if (!p2p->cfg->p2ps_prov_complete) {
1022 /* Don't emit anything */
1023 } else if (msg.status && *msg.status != P2P_SC_SUCCESS &&
1024 *msg.status != P2P_SC_SUCCESS_DEFERRED) {
1025 reject = *msg.status;
1026 p2p->cfg->p2ps_prov_complete(p2p->cfg->cb_ctx, reject,
1027 sa, adv_mac, session_mac,
1028 NULL, adv_id, session_id,
1029 0, 0, msg.persistent_ssid,
1030 msg.persistent_ssid_len,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001031 0, 0, NULL, NULL, 0, freq);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001032 } else if (msg.status && *msg.status == P2P_SC_SUCCESS_DEFERRED &&
1033 p2p->p2ps_prov) {
1034 p2p->p2ps_prov->status = reject;
1035 p2p->p2ps_prov->conncap = conncap;
1036
1037 if (reject != P2P_SC_SUCCESS)
1038 p2p->cfg->p2ps_prov_complete(p2p->cfg->cb_ctx, reject,
1039 sa, adv_mac, session_mac,
1040 NULL, adv_id,
1041 session_id, conncap, 0,
1042 msg.persistent_ssid,
1043 msg.persistent_ssid_len, 0,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001044 0, NULL, NULL, 0, freq);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001045 else
1046 p2p->cfg->p2ps_prov_complete(p2p->cfg->cb_ctx,
1047 *msg.status,
1048 sa, adv_mac, session_mac,
1049 group_mac, adv_id,
1050 session_id, conncap,
1051 passwd_id,
1052 msg.persistent_ssid,
1053 msg.persistent_ssid_len, 0,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001054 0, NULL,
1055 (const u8 *) &resp_fcap,
1056 sizeof(resp_fcap), freq);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001057 } else if (msg.status && p2p->p2ps_prov) {
1058 p2p->p2ps_prov->status = P2P_SC_SUCCESS;
1059 p2p->cfg->p2ps_prov_complete(p2p->cfg->cb_ctx, *msg.status, sa,
1060 adv_mac, session_mac, group_mac,
1061 adv_id, session_id, conncap,
1062 passwd_id,
1063 msg.persistent_ssid,
1064 msg.persistent_ssid_len,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001065 0, 0, NULL,
1066 (const u8 *) &resp_fcap,
1067 sizeof(resp_fcap), freq);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001068 } else if (msg.status) {
1069 } else if (auto_accept && reject == P2P_SC_SUCCESS) {
1070 p2p->cfg->p2ps_prov_complete(p2p->cfg->cb_ctx, P2P_SC_SUCCESS,
1071 sa, adv_mac, session_mac,
1072 group_mac, adv_id, session_id,
1073 conncap, passwd_id,
1074 msg.persistent_ssid,
1075 msg.persistent_ssid_len,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001076 0, 0, NULL,
1077 (const u8 *) &resp_fcap,
1078 sizeof(resp_fcap), freq);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001079 } else if (reject == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE &&
1080 (!msg.session_info || !msg.session_info_len)) {
1081 p2p->p2ps_prov->method = msg.wps_config_methods;
1082
1083 p2p->cfg->p2ps_prov_complete(p2p->cfg->cb_ctx, P2P_SC_SUCCESS,
1084 sa, adv_mac, session_mac,
1085 group_mac, adv_id, session_id,
1086 conncap, passwd_id,
1087 msg.persistent_ssid,
1088 msg.persistent_ssid_len,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001089 0, 1, NULL,
1090 (const u8 *) &resp_fcap,
1091 sizeof(resp_fcap), freq);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001092 } else if (reject == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
1093 size_t buf_len = msg.session_info_len;
1094 char *buf = os_malloc(2 * buf_len + 1);
1095
1096 if (buf) {
1097 p2p->p2ps_prov->method = msg.wps_config_methods;
1098
1099 utf8_escape((char *) msg.session_info, buf_len,
1100 buf, 2 * buf_len + 1);
1101
1102 p2p->cfg->p2ps_prov_complete(
1103 p2p->cfg->cb_ctx, P2P_SC_SUCCESS, sa,
1104 adv_mac, session_mac, group_mac, adv_id,
1105 session_id, conncap, passwd_id,
1106 msg.persistent_ssid, msg.persistent_ssid_len,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001107 0, 1, buf,
1108 (const u8 *) &resp_fcap, sizeof(resp_fcap),
1109 freq);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001110
1111 os_free(buf);
1112 }
1113 }
1114
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001115 /*
1116 * prov_disc_req callback is used to generate P2P-PROV-DISC-ENTER-PIN,
1117 * P2P-PROV-DISC-SHOW-PIN, and P2P-PROV-DISC-PBC-REQ events.
1118 * Call it either on legacy P2P PD or on P2PS PD only if we need to
1119 * enter/show PIN.
1120 *
1121 * The callback is called in the following cases:
1122 * 1. Legacy P2P PD request, response status SUCCESS
1123 * 2. P2PS advertiser, method: DISPLAY, autoaccept: TRUE,
1124 * response status: SUCCESS
1125 * 3. P2PS advertiser, method DISPLAY, autoaccept: FALSE,
1126 * response status: INFO_CURRENTLY_UNAVAILABLE
1127 * 4. P2PS advertiser, method: KEYPAD, autoaccept==any,
1128 * response status: INFO_CURRENTLY_UNAVAILABLE
1129 * 5. P2PS follow-on with SUCCESS_DEFERRED,
1130 * advertiser role: DISPLAY, autoaccept: FALSE,
1131 * seeker: KEYPAD, response status: SUCCESS
1132 */
1133 if (p2p->cfg->prov_disc_req &&
1134 ((reject == P2P_SC_SUCCESS && !msg.adv_id) ||
1135 (!msg.status &&
1136 (reject == P2P_SC_SUCCESS ||
1137 reject == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) &&
1138 passwd_id == DEV_PW_USER_SPECIFIED) ||
1139 (!msg.status &&
1140 reject == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE &&
1141 passwd_id == DEV_PW_REGISTRAR_SPECIFIED) ||
1142 (reject == P2P_SC_SUCCESS &&
1143 msg.status && *msg.status == P2P_SC_SUCCESS_DEFERRED &&
1144 passwd_id == DEV_PW_REGISTRAR_SPECIFIED))) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001145 const u8 *dev_addr = sa;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001146
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001147 if (msg.p2p_device_addr)
1148 dev_addr = msg.p2p_device_addr;
1149 p2p->cfg->prov_disc_req(p2p->cfg->cb_ctx, sa,
1150 msg.wps_config_methods,
1151 dev_addr, msg.pri_dev_type,
1152 msg.device_name, msg.config_methods,
1153 msg.capability ? msg.capability[0] : 0,
1154 msg.capability ? msg.capability[1] :
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001155 0,
1156 msg.group_id, msg.group_id_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001157 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001158
1159 if (reject == P2P_SC_SUCCESS) {
1160 switch (config_methods) {
1161 case WPS_CONFIG_DISPLAY:
1162 dev->wps_prov_info = WPS_CONFIG_KEYPAD;
1163 break;
1164 case WPS_CONFIG_KEYPAD:
1165 dev->wps_prov_info = WPS_CONFIG_DISPLAY;
1166 break;
1167 case WPS_CONFIG_PUSHBUTTON:
1168 dev->wps_prov_info = WPS_CONFIG_PUSHBUTTON;
1169 break;
1170 case WPS_CONFIG_P2PS:
1171 dev->wps_prov_info = WPS_CONFIG_P2PS;
1172 break;
1173 default:
1174 dev->wps_prov_info = 0;
1175 break;
1176 }
1177
1178 if (msg.intended_addr)
1179 os_memcpy(dev->interface_addr, msg.intended_addr,
1180 ETH_ALEN);
1181 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001182 p2p_parse_free(&msg);
1183}
1184
1185
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001186static int p2p_validate_p2ps_pd_resp(struct p2p_data *p2p,
1187 struct p2p_message *msg)
1188{
1189 u8 conn_cap_go = 0;
1190 u8 conn_cap_cli = 0;
1191 u32 session_id;
1192 u32 adv_id;
1193
1194#define P2PS_PD_RESP_CHECK(_val, _attr) \
1195 do { \
1196 if ((_val) && !msg->_attr) { \
1197 p2p_dbg(p2p, "P2PS PD Response missing " #_attr); \
1198 return -1; \
1199 } \
1200 } while (0)
1201
1202 P2PS_PD_RESP_CHECK(1, status);
1203 P2PS_PD_RESP_CHECK(1, adv_id);
1204 P2PS_PD_RESP_CHECK(1, adv_mac);
1205 P2PS_PD_RESP_CHECK(1, capability);
1206 P2PS_PD_RESP_CHECK(1, p2p_device_info);
1207 P2PS_PD_RESP_CHECK(1, session_id);
1208 P2PS_PD_RESP_CHECK(1, session_mac);
1209 P2PS_PD_RESP_CHECK(1, feature_cap);
1210
1211 session_id = WPA_GET_LE32(msg->session_id);
1212 adv_id = WPA_GET_LE32(msg->adv_id);
1213
1214 if (p2p->p2ps_prov->session_id != session_id) {
1215 p2p_dbg(p2p,
1216 "Ignore PD Response with unexpected Session ID");
1217 return -1;
1218 }
1219
1220 if (os_memcmp(p2p->p2ps_prov->session_mac, msg->session_mac,
1221 ETH_ALEN)) {
1222 p2p_dbg(p2p,
1223 "Ignore PD Response with unexpected Session MAC");
1224 return -1;
1225 }
1226
1227 if (p2p->p2ps_prov->adv_id != adv_id) {
1228 p2p_dbg(p2p,
1229 "Ignore PD Response with unexpected Advertisement ID");
1230 return -1;
1231 }
1232
1233 if (os_memcmp(p2p->p2ps_prov->adv_mac, msg->adv_mac, ETH_ALEN) != 0) {
1234 p2p_dbg(p2p,
1235 "Ignore PD Response with unexpected Advertisement MAC");
1236 return -1;
1237 }
1238
1239 if (msg->listen_channel) {
1240 p2p_dbg(p2p,
1241 "Ignore malformed PD Response - unexpected Listen Channel");
1242 return -1;
1243 }
1244
1245 if (*msg->status == P2P_SC_SUCCESS &&
1246 !(!!msg->conn_cap ^ !!msg->persistent_dev)) {
1247 p2p_dbg(p2p,
1248 "Ignore malformed PD Response - either conn_cap or persistent group should be present");
1249 return -1;
1250 }
1251
1252 if (msg->persistent_dev && *msg->status != P2P_SC_SUCCESS) {
1253 p2p_dbg(p2p,
1254 "Ignore malformed PD Response - persistent group is present, but the status isn't success");
1255 return -1;
1256 }
1257
1258 if (msg->conn_cap) {
1259 conn_cap_go = *msg->conn_cap == P2PS_SETUP_GROUP_OWNER;
1260 conn_cap_cli = *msg->conn_cap == P2PS_SETUP_CLIENT;
1261 }
1262
1263 P2PS_PD_RESP_CHECK(msg->persistent_dev || conn_cap_go || conn_cap_cli,
1264 channel_list);
1265 P2PS_PD_RESP_CHECK(msg->persistent_dev || conn_cap_go || conn_cap_cli,
1266 config_timeout);
1267
1268 P2PS_PD_RESP_CHECK(conn_cap_go, group_id);
1269 P2PS_PD_RESP_CHECK(conn_cap_go, intended_addr);
1270 P2PS_PD_RESP_CHECK(conn_cap_go, operating_channel);
1271 /*
1272 * TODO: Also validate that operating channel is present if the device
1273 * is a GO in a persistent group. We can't do it here since we don't
1274 * know what is the role of the peer. It should be probably done in
1275 * p2ps_prov_complete callback, but currently operating channel isn't
1276 * passed to it.
1277 */
1278
1279#undef P2PS_PD_RESP_CHECK
1280
1281 return 0;
1282}
1283
1284
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001285void p2p_process_prov_disc_resp(struct p2p_data *p2p, const u8 *sa,
1286 const u8 *data, size_t len)
1287{
1288 struct p2p_message msg;
1289 struct p2p_device *dev;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07001290 u16 report_config_methods = 0, req_config_methods;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001291 u8 status = P2P_SC_SUCCESS;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001292 u32 adv_id = 0;
1293 u8 conncap = P2PS_SETUP_NEW;
1294 u8 adv_mac[ETH_ALEN];
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001295 const u8 *group_mac;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001296 int passwd_id = DEV_PW_DEFAULT;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001297 int p2ps_seeker;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001298
1299 if (p2p_parse(data, len, &msg))
1300 return;
1301
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001302 if (p2p->p2ps_prov && p2p_validate_p2ps_pd_resp(p2p, &msg)) {
1303 p2p_parse_free(&msg);
1304 return;
1305 }
1306
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001307 /* Parse the P2PS members present */
1308 if (msg.status)
1309 status = *msg.status;
1310
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001311 group_mac = msg.intended_addr;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001312
1313 if (msg.adv_mac)
1314 os_memcpy(adv_mac, msg.adv_mac, ETH_ALEN);
1315 else
1316 os_memset(adv_mac, 0, ETH_ALEN);
1317
1318 if (msg.adv_id)
1319 adv_id = WPA_GET_LE32(msg.adv_id);
1320
1321 if (msg.conn_cap) {
1322 conncap = *msg.conn_cap;
1323
1324 /* Switch bits to local relative */
1325 switch (conncap) {
1326 case P2PS_SETUP_GROUP_OWNER:
1327 conncap = P2PS_SETUP_CLIENT;
1328 break;
1329 case P2PS_SETUP_CLIENT:
1330 conncap = P2PS_SETUP_GROUP_OWNER;
1331 break;
1332 }
1333 }
1334
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001335 p2p_dbg(p2p, "Received Provision Discovery Response from " MACSTR
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001336 " with config methods 0x%x",
1337 MAC2STR(sa), msg.wps_config_methods);
1338
1339 dev = p2p_get_device(p2p, sa);
1340 if (dev == NULL || !dev->req_config_methods) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001341 p2p_dbg(p2p, "Ignore Provision Discovery Response from " MACSTR
1342 " with no pending request", MAC2STR(sa));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001343 p2p_parse_free(&msg);
1344 return;
1345 }
1346
1347 if (dev->dialog_token != msg.dialog_token) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001348 p2p_dbg(p2p, "Ignore Provision Discovery Response with unexpected Dialog Token %u (expected %u)",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001349 msg.dialog_token, dev->dialog_token);
1350 p2p_parse_free(&msg);
1351 return;
1352 }
1353
Dmitry Shmidt91c40cd2012-09-25 14:23:53 -07001354 if (p2p->pending_action_state == P2P_PENDING_PD) {
1355 os_memset(p2p->pending_pd_devaddr, 0, ETH_ALEN);
1356 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
1357 }
1358
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001359 p2ps_seeker = p2p->p2ps_prov && p2p->p2ps_prov->pd_seeker;
1360
Jouni Malinen75ecf522011-06-27 15:19:46 -07001361 /*
Dmitry Shmidt444d5672013-04-01 13:08:44 -07001362 * Use a local copy of the requested config methods since
1363 * p2p_reset_pending_pd() can clear this in the peer entry.
1364 */
1365 req_config_methods = dev->req_config_methods;
1366
1367 /*
Jouni Malinen75ecf522011-06-27 15:19:46 -07001368 * If the response is from the peer to whom a user initiated request
1369 * was sent earlier, we reset that state info here.
1370 */
1371 if (p2p->user_initiated_pd &&
1372 os_memcmp(p2p->pending_pd_devaddr, sa, ETH_ALEN) == 0)
1373 p2p_reset_pending_pd(p2p);
1374
Dmitry Shmidt444d5672013-04-01 13:08:44 -07001375 if (msg.wps_config_methods != req_config_methods) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001376 p2p_dbg(p2p, "Peer rejected our Provision Discovery Request (received config_methods 0x%x expected 0x%x",
Dmitry Shmidt444d5672013-04-01 13:08:44 -07001377 msg.wps_config_methods, req_config_methods);
Jouni Malinen75ecf522011-06-27 15:19:46 -07001378 if (p2p->cfg->prov_disc_fail)
1379 p2p->cfg->prov_disc_fail(p2p->cfg->cb_ctx, sa,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001380 P2P_PROV_DISC_REJECTED,
1381 adv_id, adv_mac, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001382 p2p_parse_free(&msg);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07001383 p2ps_prov_free(p2p);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001384 goto out;
1385 }
1386
Dmitry Shmidt444d5672013-04-01 13:08:44 -07001387 report_config_methods = req_config_methods;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001388 dev->flags &= ~(P2P_DEV_PD_PEER_DISPLAY |
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001389 P2P_DEV_PD_PEER_KEYPAD |
1390 P2P_DEV_PD_PEER_P2PS);
Dmitry Shmidt444d5672013-04-01 13:08:44 -07001391 if (req_config_methods & WPS_CONFIG_DISPLAY) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001392 p2p_dbg(p2p, "Peer " MACSTR
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001393 " accepted to show a PIN on display", MAC2STR(sa));
1394 dev->flags |= P2P_DEV_PD_PEER_DISPLAY;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001395 passwd_id = DEV_PW_REGISTRAR_SPECIFIED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001396 } else if (msg.wps_config_methods & WPS_CONFIG_KEYPAD) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001397 p2p_dbg(p2p, "Peer " MACSTR
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001398 " accepted to write our PIN using keypad",
1399 MAC2STR(sa));
1400 dev->flags |= P2P_DEV_PD_PEER_KEYPAD;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001401 passwd_id = DEV_PW_USER_SPECIFIED;
1402 } else if (msg.wps_config_methods & WPS_CONFIG_P2PS) {
1403 p2p_dbg(p2p, "Peer " MACSTR " accepted P2PS PIN",
1404 MAC2STR(sa));
1405 dev->flags |= P2P_DEV_PD_PEER_P2PS;
1406 passwd_id = DEV_PW_P2PS_DEFAULT;
1407 }
1408
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001409 if ((status == P2P_SC_SUCCESS || status == P2P_SC_SUCCESS_DEFERRED) &&
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001410 p2p->p2ps_prov) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001411 dev->oper_freq = 0;
1412
1413 /*
1414 * Save the reported channel list and operating frequency.
1415 * Note that the specification mandates that the responder
1416 * should include in the channel list only channels reported by
1417 * the initiator, so this is only a sanity check, and if this
1418 * fails the flow would continue, although it would probably
1419 * fail. Same is true for the operating channel.
1420 */
1421 if (msg.channel_list && msg.channel_list_len &&
1422 p2p_peer_channels_check(p2p, &p2p->channels, dev,
1423 msg.channel_list,
1424 msg.channel_list_len) < 0)
1425 p2p_dbg(p2p, "P2PS PD Response - no common channels");
1426
1427 if (msg.operating_channel) {
1428 if (p2p_channels_includes(&p2p->channels,
1429 msg.operating_channel[3],
1430 msg.operating_channel[4]) &&
1431 p2p_channels_includes(&dev->channels,
1432 msg.operating_channel[3],
1433 msg.operating_channel[4])) {
1434 dev->oper_freq =
1435 p2p_channel_to_freq(
1436 msg.operating_channel[3],
1437 msg.operating_channel[4]);
1438 } else {
1439 p2p_dbg(p2p,
1440 "P2PS PD Response - invalid operating channel");
1441 }
1442 }
1443
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001444 if (p2p->cfg->p2ps_prov_complete) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001445 int freq = 0;
1446
1447 if (conncap == P2PS_SETUP_GROUP_OWNER) {
1448 u8 tmp;
1449
1450 /*
1451 * Re-select the operating channel as it is
1452 * possible that original channel is no longer
1453 * valid. This should not really fail.
1454 */
1455 if (p2p_go_select_channel(p2p, dev, &tmp) < 0)
1456 p2p_dbg(p2p,
1457 "P2PS PD channel selection failed");
1458
1459 freq = p2p_channel_to_freq(p2p->op_reg_class,
1460 p2p->op_channel);
1461 if (freq < 0)
1462 freq = 0;
1463 }
1464
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001465 p2p->cfg->p2ps_prov_complete(
1466 p2p->cfg->cb_ctx, status, sa, adv_mac,
1467 p2p->p2ps_prov->session_mac,
1468 group_mac, adv_id, p2p->p2ps_prov->session_id,
1469 conncap, passwd_id, msg.persistent_ssid,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001470 msg.persistent_ssid_len, 1, 0, NULL,
1471 msg.feature_cap, msg.feature_cap_len, freq);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001472 }
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07001473 p2ps_prov_free(p2p);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001474 } else if (status != P2P_SC_SUCCESS &&
1475 status != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE &&
1476 status != P2P_SC_SUCCESS_DEFERRED && p2p->p2ps_prov) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001477 if (p2p->cfg->p2ps_prov_complete)
1478 p2p->cfg->p2ps_prov_complete(
1479 p2p->cfg->cb_ctx, status, sa, adv_mac,
1480 p2p->p2ps_prov->session_mac,
1481 group_mac, adv_id, p2p->p2ps_prov->session_id,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001482 0, 0, NULL, 0, 1, 0, NULL, NULL, 0, 0);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07001483 p2ps_prov_free(p2p);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001484 }
1485
1486 if (status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
1487 if (p2p->cfg->remove_stale_groups) {
1488 p2p->cfg->remove_stale_groups(p2p->cfg->cb_ctx,
1489 dev->info.p2p_device_addr,
1490 NULL, NULL, 0);
1491 }
1492
1493 if (msg.session_info && msg.session_info_len) {
1494 size_t info_len = msg.session_info_len;
1495 char *deferred_sess_resp = os_malloc(2 * info_len + 1);
1496
1497 if (!deferred_sess_resp) {
1498 p2p_parse_free(&msg);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07001499 p2ps_prov_free(p2p);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001500 goto out;
1501 }
1502 utf8_escape((char *) msg.session_info, info_len,
1503 deferred_sess_resp, 2 * info_len + 1);
1504
1505 if (p2p->cfg->prov_disc_fail)
1506 p2p->cfg->prov_disc_fail(
1507 p2p->cfg->cb_ctx, sa,
1508 P2P_PROV_DISC_INFO_UNAVAILABLE,
1509 adv_id, adv_mac,
1510 deferred_sess_resp);
1511 os_free(deferred_sess_resp);
1512 } else
1513 if (p2p->cfg->prov_disc_fail)
1514 p2p->cfg->prov_disc_fail(
1515 p2p->cfg->cb_ctx, sa,
1516 P2P_PROV_DISC_INFO_UNAVAILABLE,
1517 adv_id, adv_mac, NULL);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001518 } else if (status != P2P_SC_SUCCESS) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001519 p2p_dbg(p2p, "Peer rejected our Provision Discovery Request");
1520 if (p2p->cfg->prov_disc_fail)
1521 p2p->cfg->prov_disc_fail(p2p->cfg->cb_ctx, sa,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001522 P2P_PROV_DISC_REJECTED,
1523 adv_id, adv_mac, NULL);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001524 p2p_parse_free(&msg);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07001525 p2ps_prov_free(p2p);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001526 goto out;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001527 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001528
1529 /* Store the provisioning info */
1530 dev->wps_prov_info = msg.wps_config_methods;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001531 if (msg.intended_addr)
1532 os_memcpy(dev->interface_addr, msg.intended_addr, ETH_ALEN);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001533
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001534 p2p_parse_free(&msg);
1535
1536out:
1537 dev->req_config_methods = 0;
1538 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001539 if (dev->flags & P2P_DEV_PD_BEFORE_GO_NEG) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001540 p2p_dbg(p2p, "Start GO Neg after the PD-before-GO-Neg workaround with "
1541 MACSTR, MAC2STR(dev->info.p2p_device_addr));
Dmitry Shmidt04949592012-07-19 12:16:46 -07001542 dev->flags &= ~P2P_DEV_PD_BEFORE_GO_NEG;
1543 p2p_connect_send(p2p, dev);
1544 return;
1545 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001546
1547 /*
1548 * prov_disc_resp callback is used to generate P2P-PROV-DISC-ENTER-PIN,
1549 * P2P-PROV-DISC-SHOW-PIN, and P2P-PROV-DISC-PBC-REQ events.
1550 * Call it only for a legacy P2P PD or for P2PS PD scenarios where
1551 * show/enter PIN events are needed.
1552 *
1553 * The callback is called in the following cases:
1554 * 1. Legacy P2P PD response with a status SUCCESS
1555 * 2. P2PS, advertiser method: DISPLAY, autoaccept: true,
1556 * response status: SUCCESS, local method KEYPAD
1557 * 3. P2PS, advertiser method: KEYPAD,Seeker side,
1558 * response status: INFO_CURRENTLY_UNAVAILABLE,
1559 * local method: DISPLAY
1560 */
1561 if (p2p->cfg->prov_disc_resp &&
1562 ((status == P2P_SC_SUCCESS && !adv_id) ||
1563 (p2ps_seeker && status == P2P_SC_SUCCESS &&
1564 passwd_id == DEV_PW_REGISTRAR_SPECIFIED) ||
1565 (p2ps_seeker &&
1566 status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE &&
1567 passwd_id == DEV_PW_USER_SPECIFIED)))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001568 p2p->cfg->prov_disc_resp(p2p->cfg->cb_ctx, sa,
1569 report_config_methods);
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07001570
1571 if (p2p->state == P2P_PD_DURING_FIND) {
1572 p2p_clear_timeout(p2p);
1573 p2p_continue_find(p2p);
1574 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001575}
1576
1577
1578int p2p_send_prov_disc_req(struct p2p_data *p2p, struct p2p_device *dev,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001579 int join, int force_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001580{
1581 struct wpabuf *req;
1582 int freq;
Dmitry Shmidt497c1d52011-07-21 15:19:46 -07001583
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001584 if (force_freq > 0)
1585 freq = force_freq;
1586 else
1587 freq = dev->listen_freq > 0 ? dev->listen_freq :
1588 dev->oper_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001589 if (freq <= 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001590 p2p_dbg(p2p, "No Listen/Operating frequency known for the peer "
1591 MACSTR " to send Provision Discovery Request",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001592 MAC2STR(dev->info.p2p_device_addr));
1593 return -1;
1594 }
1595
1596 if (dev->flags & P2P_DEV_GROUP_CLIENT_ONLY) {
1597 if (!(dev->info.dev_capab &
1598 P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY)) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001599 p2p_dbg(p2p, "Cannot use PD with P2P Device " MACSTR
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001600 " that is in a group and is not discoverable",
1601 MAC2STR(dev->info.p2p_device_addr));
1602 return -1;
1603 }
1604 /* TODO: use device discoverability request through GO */
1605 }
1606
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001607 if (p2p->p2ps_prov) {
1608 if (p2p->p2ps_prov->status == P2P_SC_SUCCESS_DEFERRED) {
1609 if (p2p->p2ps_prov->method == WPS_CONFIG_DISPLAY)
1610 dev->req_config_methods = WPS_CONFIG_KEYPAD;
1611 else if (p2p->p2ps_prov->method == WPS_CONFIG_KEYPAD)
1612 dev->req_config_methods = WPS_CONFIG_DISPLAY;
1613 else
1614 dev->req_config_methods = WPS_CONFIG_P2PS;
1615 } else {
1616 /* Order of preference, based on peer's capabilities */
1617 if (p2p->p2ps_prov->method)
1618 dev->req_config_methods =
1619 p2p->p2ps_prov->method;
1620 else if (dev->info.config_methods & WPS_CONFIG_P2PS)
1621 dev->req_config_methods = WPS_CONFIG_P2PS;
1622 else if (dev->info.config_methods & WPS_CONFIG_DISPLAY)
1623 dev->req_config_methods = WPS_CONFIG_DISPLAY;
1624 else
1625 dev->req_config_methods = WPS_CONFIG_KEYPAD;
1626 }
1627 p2p_dbg(p2p,
1628 "Building PD Request based on P2PS config method 0x%x status %d --> req_config_methods 0x%x",
1629 p2p->p2ps_prov->method, p2p->p2ps_prov->status,
1630 dev->req_config_methods);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001631
1632 if (p2p_prepare_channel(p2p, dev, p2p->p2ps_prov->force_freq,
1633 p2p->p2ps_prov->pref_freq, 1) < 0)
1634 return -1;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001635 }
1636
1637 req = p2p_build_prov_disc_req(p2p, dev, join);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001638 if (req == NULL)
1639 return -1;
1640
Dmitry Shmidt04949592012-07-19 12:16:46 -07001641 if (p2p->state != P2P_IDLE)
1642 p2p_stop_listen_for_freq(p2p, freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001643 p2p->pending_action_state = P2P_PENDING_PD;
1644 if (p2p_send_action(p2p, freq, dev->info.p2p_device_addr,
1645 p2p->cfg->dev_addr, dev->info.p2p_device_addr,
1646 wpabuf_head(req), wpabuf_len(req), 200) < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001647 p2p_dbg(p2p, "Failed to send Action frame");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001648 wpabuf_free(req);
1649 return -1;
1650 }
1651
Jouni Malinen75ecf522011-06-27 15:19:46 -07001652 os_memcpy(p2p->pending_pd_devaddr, dev->info.p2p_device_addr, ETH_ALEN);
1653
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001654 wpabuf_free(req);
1655 return 0;
1656}
1657
1658
1659int p2p_prov_disc_req(struct p2p_data *p2p, const u8 *peer_addr,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001660 struct p2ps_provision *p2ps_prov,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001661 u16 config_methods, int join, int force_freq,
1662 int user_initiated_pd)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001663{
1664 struct p2p_device *dev;
1665
1666 dev = p2p_get_device(p2p, peer_addr);
1667 if (dev == NULL)
1668 dev = p2p_get_device_interface(p2p, peer_addr);
1669 if (dev == NULL || (dev->flags & P2P_DEV_PROBE_REQ_ONLY)) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001670 p2p_dbg(p2p, "Provision Discovery Request destination " MACSTR
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001671 " not yet known", MAC2STR(peer_addr));
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001672 os_free(p2ps_prov);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001673 return -1;
1674 }
1675
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001676 p2p_dbg(p2p, "Provision Discovery Request with " MACSTR
1677 " (config methods 0x%x)",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001678 MAC2STR(peer_addr), config_methods);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001679 if (config_methods == 0 && !p2ps_prov) {
1680 os_free(p2ps_prov);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001681 return -1;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001682 }
1683
1684 if (p2ps_prov && p2ps_prov->status == P2P_SC_SUCCESS_DEFERRED &&
1685 p2p->p2ps_prov) {
1686 /* Use cached method from deferred provisioning */
1687 p2ps_prov->method = p2p->p2ps_prov->method;
1688 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001689
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001690 /* Reset provisioning info */
1691 dev->wps_prov_info = 0;
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07001692 p2ps_prov_free(p2p);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001693 p2p->p2ps_prov = p2ps_prov;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001694
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001695 dev->req_config_methods = config_methods;
1696 if (join)
1697 dev->flags |= P2P_DEV_PD_FOR_JOIN;
1698 else
1699 dev->flags &= ~P2P_DEV_PD_FOR_JOIN;
1700
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001701 if (p2p->state != P2P_IDLE && p2p->state != P2P_SEARCH &&
1702 p2p->state != P2P_LISTEN_ONLY) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001703 p2p_dbg(p2p, "Busy with other operations; postpone Provision Discovery Request with "
1704 MACSTR " (config methods 0x%x)",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001705 MAC2STR(peer_addr), config_methods);
1706 return 0;
1707 }
1708
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001709 p2p->user_initiated_pd = user_initiated_pd;
Dmitry Shmidt051af732013-10-22 13:52:46 -07001710 p2p->pd_force_freq = force_freq;
Jouni Malinen75ecf522011-06-27 15:19:46 -07001711
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001712 if (p2p->user_initiated_pd)
Jouni Malinen75ecf522011-06-27 15:19:46 -07001713 p2p->pd_retries = MAX_PROV_DISC_REQ_RETRIES;
1714
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001715 /*
1716 * Assign dialog token here to use the same value in each retry within
1717 * the same PD exchange.
1718 */
1719 dev->dialog_token++;
1720 if (dev->dialog_token == 0)
1721 dev->dialog_token = 1;
1722
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001723 return p2p_send_prov_disc_req(p2p, dev, join, force_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001724}
Jouni Malinen75ecf522011-06-27 15:19:46 -07001725
1726
1727void p2p_reset_pending_pd(struct p2p_data *p2p)
1728{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001729 struct p2p_device *dev;
1730
1731 dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
1732 if (os_memcmp(p2p->pending_pd_devaddr,
1733 dev->info.p2p_device_addr, ETH_ALEN))
1734 continue;
1735 if (!dev->req_config_methods)
1736 continue;
1737 if (dev->flags & P2P_DEV_PD_FOR_JOIN)
1738 continue;
1739 /* Reset the config methods of the device */
1740 dev->req_config_methods = 0;
1741 }
1742
Jouni Malinen75ecf522011-06-27 15:19:46 -07001743 p2p->user_initiated_pd = 0;
1744 os_memset(p2p->pending_pd_devaddr, 0, ETH_ALEN);
1745 p2p->pd_retries = 0;
Dmitry Shmidt051af732013-10-22 13:52:46 -07001746 p2p->pd_force_freq = 0;
Jouni Malinen75ecf522011-06-27 15:19:46 -07001747}
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07001748
1749
1750void p2ps_prov_free(struct p2p_data *p2p)
1751{
1752 os_free(p2p->p2ps_prov);
1753 p2p->p2ps_prov = NULL;
1754}