blob: 68d79d23995a8481d01b6288b1f0ff52c9e143be [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"
13#include "wps/wps_defs.h"
14#include "p2p_i.h"
15#include "p2p.h"
16
17
Jouni Malinen75ecf522011-06-27 15:19:46 -070018/*
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070019 * Number of retries to attempt for provision discovery requests
20 * in case the peer is not listening.
Jouni Malinen75ecf522011-06-27 15:19:46 -070021 */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -080022#define MAX_PROV_DISC_REQ_RETRIES 120
Jouni Malinen75ecf522011-06-27 15:19:46 -070023
24
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070025static void p2p_build_wps_ie_config_methods(struct wpabuf *buf,
26 u16 config_methods)
27{
28 u8 *len;
29 wpabuf_put_u8(buf, WLAN_EID_VENDOR_SPECIFIC);
30 len = wpabuf_put(buf, 1);
31 wpabuf_put_be32(buf, WPS_DEV_OUI_WFA);
32
33 /* Config Methods */
34 wpabuf_put_be16(buf, ATTR_CONFIG_METHODS);
35 wpabuf_put_be16(buf, 2);
36 wpabuf_put_be16(buf, config_methods);
37
38 p2p_buf_update_ie_hdr(buf, len);
39}
40
41
42static struct wpabuf * p2p_build_prov_disc_req(struct p2p_data *p2p,
43 u8 dialog_token,
44 u16 config_methods,
45 struct p2p_device *go)
46{
47 struct wpabuf *buf;
48 u8 *len;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070049 size_t extra = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070050
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070051#ifdef CONFIG_WIFI_DISPLAY
52 if (p2p->wfd_ie_prov_disc_req)
53 extra = wpabuf_len(p2p->wfd_ie_prov_disc_req);
54#endif /* CONFIG_WIFI_DISPLAY */
55
56 buf = wpabuf_alloc(1000 + extra);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070057 if (buf == NULL)
58 return NULL;
59
60 p2p_buf_add_public_action_hdr(buf, P2P_PROV_DISC_REQ, dialog_token);
61
62 len = p2p_buf_add_ie_hdr(buf);
Dmitry Shmidt04949592012-07-19 12:16:46 -070063 p2p_buf_add_capability(buf, p2p->dev_capab &
64 ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070065 p2p_buf_add_device_info(buf, p2p, NULL);
66 if (go) {
67 p2p_buf_add_group_id(buf, go->info.p2p_device_addr,
68 go->oper_ssid, go->oper_ssid_len);
69 }
70 p2p_buf_update_ie_hdr(buf, len);
71
72 /* WPS IE with Config Methods attribute */
73 p2p_build_wps_ie_config_methods(buf, config_methods);
74
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070075#ifdef CONFIG_WIFI_DISPLAY
76 if (p2p->wfd_ie_prov_disc_req)
77 wpabuf_put_buf(buf, p2p->wfd_ie_prov_disc_req);
78#endif /* CONFIG_WIFI_DISPLAY */
79
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070080 return buf;
81}
82
83
84static struct wpabuf * p2p_build_prov_disc_resp(struct p2p_data *p2p,
85 u8 dialog_token,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070086 u16 config_methods,
87 const u8 *group_id,
88 size_t group_id_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070089{
90 struct wpabuf *buf;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070091 size_t extra = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070092
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070093#ifdef CONFIG_WIFI_DISPLAY
94 struct wpabuf *wfd_ie = p2p->wfd_ie_prov_disc_resp;
95 if (wfd_ie && group_id) {
96 size_t i;
97 for (i = 0; i < p2p->num_groups; i++) {
98 struct p2p_group *g = p2p->groups[i];
99 struct wpabuf *ie;
100 if (!p2p_group_is_group_id_match(g, group_id,
101 group_id_len))
102 continue;
103 ie = p2p_group_get_wfd_ie(g);
104 if (ie) {
105 wfd_ie = ie;
106 break;
107 }
108 }
109 }
110 if (wfd_ie)
111 extra = wpabuf_len(wfd_ie);
112#endif /* CONFIG_WIFI_DISPLAY */
113
114 buf = wpabuf_alloc(100 + extra);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700115 if (buf == NULL)
116 return NULL;
117
118 p2p_buf_add_public_action_hdr(buf, P2P_PROV_DISC_RESP, dialog_token);
119
120 /* WPS IE with Config Methods attribute */
121 p2p_build_wps_ie_config_methods(buf, config_methods);
122
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700123#ifdef CONFIG_WIFI_DISPLAY
124 if (wfd_ie)
125 wpabuf_put_buf(buf, wfd_ie);
126#endif /* CONFIG_WIFI_DISPLAY */
127
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700128 return buf;
129}
130
131
132void p2p_process_prov_disc_req(struct p2p_data *p2p, const u8 *sa,
133 const u8 *data, size_t len, int rx_freq)
134{
135 struct p2p_message msg;
136 struct p2p_device *dev;
137 int freq;
138 int reject = 1;
139 struct wpabuf *resp;
140
141 if (p2p_parse(data, len, &msg))
142 return;
143
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700144 p2p_dbg(p2p, "Received Provision Discovery Request from " MACSTR
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700145 " with config methods 0x%x (freq=%d)",
146 MAC2STR(sa), msg.wps_config_methods, rx_freq);
147
148 dev = p2p_get_device(p2p, sa);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800149 if (dev == NULL || (dev->flags & P2P_DEV_PROBE_REQ_ONLY)) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700150 p2p_dbg(p2p, "Provision Discovery Request from unknown peer "
151 MACSTR, MAC2STR(sa));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800152
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800153 if (p2p_add_device(p2p, sa, rx_freq, NULL, 0, data + 1, len - 1,
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800154 0)) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700155 p2p_dbg(p2p, "Provision Discovery Request add device failed "
156 MACSTR, MAC2STR(sa));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700157 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700158 } else if (msg.wfd_subelems) {
159 wpabuf_free(dev->info.wfd_subelems);
160 dev->info.wfd_subelems = wpabuf_dup(msg.wfd_subelems);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700161 }
162
163 if (!(msg.wps_config_methods &
164 (WPS_CONFIG_DISPLAY | WPS_CONFIG_KEYPAD |
165 WPS_CONFIG_PUSHBUTTON))) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700166 p2p_dbg(p2p, "Unsupported Config Methods in Provision Discovery Request");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700167 goto out;
168 }
169
Dmitry Shmidt04949592012-07-19 12:16:46 -0700170 if (msg.group_id) {
171 size_t i;
172 for (i = 0; i < p2p->num_groups; i++) {
173 if (p2p_group_is_group_id_match(p2p->groups[i],
174 msg.group_id,
175 msg.group_id_len))
176 break;
177 }
178 if (i == p2p->num_groups) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700179 p2p_dbg(p2p, "PD request for unknown P2P Group ID - reject");
Dmitry Shmidt04949592012-07-19 12:16:46 -0700180 goto out;
181 }
182 }
183
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700184 if (dev)
185 dev->flags &= ~(P2P_DEV_PD_PEER_DISPLAY |
186 P2P_DEV_PD_PEER_KEYPAD);
187 if (msg.wps_config_methods & WPS_CONFIG_DISPLAY) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700188 p2p_dbg(p2p, "Peer " MACSTR
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700189 " requested us to show a PIN on display", MAC2STR(sa));
190 if (dev)
191 dev->flags |= P2P_DEV_PD_PEER_KEYPAD;
192 } else if (msg.wps_config_methods & WPS_CONFIG_KEYPAD) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700193 p2p_dbg(p2p, "Peer " MACSTR
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700194 " requested us to write its PIN using keypad",
195 MAC2STR(sa));
196 if (dev)
197 dev->flags |= P2P_DEV_PD_PEER_DISPLAY;
198 }
199
200 reject = 0;
201
202out:
203 resp = p2p_build_prov_disc_resp(p2p, msg.dialog_token,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700204 reject ? 0 : msg.wps_config_methods,
205 msg.group_id, msg.group_id_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700206 if (resp == NULL) {
207 p2p_parse_free(&msg);
208 return;
209 }
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700210 p2p_dbg(p2p, "Sending Provision Discovery Response");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700211 if (rx_freq > 0)
212 freq = rx_freq;
213 else
Dmitry Shmidt4b060592013-04-29 16:42:49 -0700214 freq = p2p_channel_to_freq(p2p->cfg->reg_class,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700215 p2p->cfg->channel);
216 if (freq < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700217 p2p_dbg(p2p, "Unknown regulatory class/channel");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700218 wpabuf_free(resp);
219 p2p_parse_free(&msg);
220 return;
221 }
222 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
223 if (p2p_send_action(p2p, freq, sa, p2p->cfg->dev_addr,
224 p2p->cfg->dev_addr,
225 wpabuf_head(resp), wpabuf_len(resp), 200) < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700226 p2p_dbg(p2p, "Failed to send Action frame");
Dmitry Shmidt4582d2a2014-02-28 11:14:23 -0800227 } else
228 p2p->send_action_in_progress = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700229
230 wpabuf_free(resp);
231
232 if (!reject && p2p->cfg->prov_disc_req) {
233 const u8 *dev_addr = sa;
234 if (msg.p2p_device_addr)
235 dev_addr = msg.p2p_device_addr;
236 p2p->cfg->prov_disc_req(p2p->cfg->cb_ctx, sa,
237 msg.wps_config_methods,
238 dev_addr, msg.pri_dev_type,
239 msg.device_name, msg.config_methods,
240 msg.capability ? msg.capability[0] : 0,
241 msg.capability ? msg.capability[1] :
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800242 0,
243 msg.group_id, msg.group_id_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700244 }
245 p2p_parse_free(&msg);
246}
247
248
249void p2p_process_prov_disc_resp(struct p2p_data *p2p, const u8 *sa,
250 const u8 *data, size_t len)
251{
252 struct p2p_message msg;
253 struct p2p_device *dev;
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700254 u16 report_config_methods = 0, req_config_methods;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700255 int success = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700256
257 if (p2p_parse(data, len, &msg))
258 return;
259
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700260 p2p_dbg(p2p, "Received Provision Discovery Response from " MACSTR
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700261 " with config methods 0x%x",
262 MAC2STR(sa), msg.wps_config_methods);
263
264 dev = p2p_get_device(p2p, sa);
265 if (dev == NULL || !dev->req_config_methods) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700266 p2p_dbg(p2p, "Ignore Provision Discovery Response from " MACSTR
267 " with no pending request", MAC2STR(sa));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700268 p2p_parse_free(&msg);
269 return;
270 }
271
272 if (dev->dialog_token != msg.dialog_token) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700273 p2p_dbg(p2p, "Ignore Provision Discovery Response with unexpected Dialog Token %u (expected %u)",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700274 msg.dialog_token, dev->dialog_token);
275 p2p_parse_free(&msg);
276 return;
277 }
278
Dmitry Shmidt91c40cd2012-09-25 14:23:53 -0700279 if (p2p->pending_action_state == P2P_PENDING_PD) {
280 os_memset(p2p->pending_pd_devaddr, 0, ETH_ALEN);
281 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
282 }
283
Jouni Malinen75ecf522011-06-27 15:19:46 -0700284 /*
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700285 * Use a local copy of the requested config methods since
286 * p2p_reset_pending_pd() can clear this in the peer entry.
287 */
288 req_config_methods = dev->req_config_methods;
289
290 /*
Jouni Malinen75ecf522011-06-27 15:19:46 -0700291 * If the response is from the peer to whom a user initiated request
292 * was sent earlier, we reset that state info here.
293 */
294 if (p2p->user_initiated_pd &&
295 os_memcmp(p2p->pending_pd_devaddr, sa, ETH_ALEN) == 0)
296 p2p_reset_pending_pd(p2p);
297
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700298 if (msg.wps_config_methods != req_config_methods) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700299 p2p_dbg(p2p, "Peer rejected our Provision Discovery Request (received config_methods 0x%x expected 0x%x",
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700300 msg.wps_config_methods, req_config_methods);
Jouni Malinen75ecf522011-06-27 15:19:46 -0700301 if (p2p->cfg->prov_disc_fail)
302 p2p->cfg->prov_disc_fail(p2p->cfg->cb_ctx, sa,
303 P2P_PROV_DISC_REJECTED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700304 p2p_parse_free(&msg);
305 goto out;
306 }
307
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700308 report_config_methods = req_config_methods;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700309 dev->flags &= ~(P2P_DEV_PD_PEER_DISPLAY |
310 P2P_DEV_PD_PEER_KEYPAD);
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700311 if (req_config_methods & WPS_CONFIG_DISPLAY) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700312 p2p_dbg(p2p, "Peer " MACSTR
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700313 " accepted to show a PIN on display", MAC2STR(sa));
314 dev->flags |= P2P_DEV_PD_PEER_DISPLAY;
315 } else if (msg.wps_config_methods & WPS_CONFIG_KEYPAD) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700316 p2p_dbg(p2p, "Peer " MACSTR
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700317 " accepted to write our PIN using keypad",
318 MAC2STR(sa));
319 dev->flags |= P2P_DEV_PD_PEER_KEYPAD;
320 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800321
322 /* Store the provisioning info */
323 dev->wps_prov_info = msg.wps_config_methods;
324
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700325 p2p_parse_free(&msg);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700326 success = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700327
328out:
329 dev->req_config_methods = 0;
330 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700331 if (dev->flags & P2P_DEV_PD_BEFORE_GO_NEG) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700332 p2p_dbg(p2p, "Start GO Neg after the PD-before-GO-Neg workaround with "
333 MACSTR, MAC2STR(dev->info.p2p_device_addr));
Dmitry Shmidt04949592012-07-19 12:16:46 -0700334 dev->flags &= ~P2P_DEV_PD_BEFORE_GO_NEG;
335 p2p_connect_send(p2p, dev);
336 return;
337 }
338 if (success && p2p->cfg->prov_disc_resp)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700339 p2p->cfg->prov_disc_resp(p2p->cfg->cb_ctx, sa,
340 report_config_methods);
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -0700341
342 if (p2p->state == P2P_PD_DURING_FIND) {
343 p2p_clear_timeout(p2p);
344 p2p_continue_find(p2p);
345 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700346}
347
348
349int p2p_send_prov_disc_req(struct p2p_data *p2p, struct p2p_device *dev,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800350 int join, int force_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700351{
352 struct wpabuf *req;
353 int freq;
Dmitry Shmidt497c1d52011-07-21 15:19:46 -0700354
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800355 if (force_freq > 0)
356 freq = force_freq;
357 else
358 freq = dev->listen_freq > 0 ? dev->listen_freq :
359 dev->oper_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700360 if (freq <= 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700361 p2p_dbg(p2p, "No Listen/Operating frequency known for the peer "
362 MACSTR " to send Provision Discovery Request",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700363 MAC2STR(dev->info.p2p_device_addr));
364 return -1;
365 }
366
367 if (dev->flags & P2P_DEV_GROUP_CLIENT_ONLY) {
368 if (!(dev->info.dev_capab &
369 P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY)) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700370 p2p_dbg(p2p, "Cannot use PD with P2P Device " MACSTR
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700371 " that is in a group and is not discoverable",
372 MAC2STR(dev->info.p2p_device_addr));
373 return -1;
374 }
375 /* TODO: use device discoverability request through GO */
376 }
377
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700378 req = p2p_build_prov_disc_req(p2p, dev->dialog_token,
379 dev->req_config_methods,
380 join ? dev : NULL);
381 if (req == NULL)
382 return -1;
383
Dmitry Shmidt04949592012-07-19 12:16:46 -0700384 if (p2p->state != P2P_IDLE)
385 p2p_stop_listen_for_freq(p2p, freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700386 p2p->pending_action_state = P2P_PENDING_PD;
387 if (p2p_send_action(p2p, freq, dev->info.p2p_device_addr,
388 p2p->cfg->dev_addr, dev->info.p2p_device_addr,
389 wpabuf_head(req), wpabuf_len(req), 200) < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700390 p2p_dbg(p2p, "Failed to send Action frame");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700391 wpabuf_free(req);
392 return -1;
393 }
394
Jouni Malinen75ecf522011-06-27 15:19:46 -0700395 os_memcpy(p2p->pending_pd_devaddr, dev->info.p2p_device_addr, ETH_ALEN);
396
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700397 wpabuf_free(req);
398 return 0;
399}
400
401
402int p2p_prov_disc_req(struct p2p_data *p2p, const u8 *peer_addr,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800403 u16 config_methods, int join, int force_freq,
404 int user_initiated_pd)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700405{
406 struct p2p_device *dev;
407
408 dev = p2p_get_device(p2p, peer_addr);
409 if (dev == NULL)
410 dev = p2p_get_device_interface(p2p, peer_addr);
411 if (dev == NULL || (dev->flags & P2P_DEV_PROBE_REQ_ONLY)) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700412 p2p_dbg(p2p, "Provision Discovery Request destination " MACSTR
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700413 " not yet known", MAC2STR(peer_addr));
414 return -1;
415 }
416
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700417 p2p_dbg(p2p, "Provision Discovery Request with " MACSTR
418 " (config methods 0x%x)",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700419 MAC2STR(peer_addr), config_methods);
420 if (config_methods == 0)
421 return -1;
422
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800423 /* Reset provisioning info */
424 dev->wps_prov_info = 0;
425
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700426 dev->req_config_methods = config_methods;
427 if (join)
428 dev->flags |= P2P_DEV_PD_FOR_JOIN;
429 else
430 dev->flags &= ~P2P_DEV_PD_FOR_JOIN;
431
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800432 if (p2p->state != P2P_IDLE && p2p->state != P2P_SEARCH &&
433 p2p->state != P2P_LISTEN_ONLY) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700434 p2p_dbg(p2p, "Busy with other operations; postpone Provision Discovery Request with "
435 MACSTR " (config methods 0x%x)",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700436 MAC2STR(peer_addr), config_methods);
437 return 0;
438 }
439
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800440 p2p->user_initiated_pd = user_initiated_pd;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700441 p2p->pd_force_freq = force_freq;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700442
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700443 if (p2p->user_initiated_pd)
Jouni Malinen75ecf522011-06-27 15:19:46 -0700444 p2p->pd_retries = MAX_PROV_DISC_REQ_RETRIES;
445
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800446 /*
447 * Assign dialog token here to use the same value in each retry within
448 * the same PD exchange.
449 */
450 dev->dialog_token++;
451 if (dev->dialog_token == 0)
452 dev->dialog_token = 1;
453
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800454 return p2p_send_prov_disc_req(p2p, dev, join, force_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700455}
Jouni Malinen75ecf522011-06-27 15:19:46 -0700456
457
458void p2p_reset_pending_pd(struct p2p_data *p2p)
459{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800460 struct p2p_device *dev;
461
462 dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
463 if (os_memcmp(p2p->pending_pd_devaddr,
464 dev->info.p2p_device_addr, ETH_ALEN))
465 continue;
466 if (!dev->req_config_methods)
467 continue;
468 if (dev->flags & P2P_DEV_PD_FOR_JOIN)
469 continue;
470 /* Reset the config methods of the device */
471 dev->req_config_methods = 0;
472 }
473
Jouni Malinen75ecf522011-06-27 15:19:46 -0700474 p2p->user_initiated_pd = 0;
475 os_memset(p2p->pending_pd_devaddr, 0, ETH_ALEN);
476 p2p->pd_retries = 0;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700477 p2p->pd_force_freq = 0;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700478}