xref: /freebsd/contrib/wpa/src/p2p/p2p_pd.c (revision 2e3507c25e42292b45a5482e116d278f5515d04d)
1 /*
2  * Wi-Fi Direct - P2P provision discovery
3  * Copyright (c) 2009-2010, Atheros Communications
4  *
5  * This software may be distributed under the terms of the BSD license.
6  * See README for more details.
7  */
8 
9 #include "includes.h"
10 
11 #include "common.h"
12 #include "common/ieee802_11_defs.h"
13 #include "common/wpa_ctrl.h"
14 #include "wps/wps_defs.h"
15 #include "p2p_i.h"
16 #include "p2p.h"
17 
18 
19 /*
20  * Number of retries to attempt for provision discovery requests
21  * in case the peer is not listening.
22  */
23 #define MAX_PROV_DISC_REQ_RETRIES 120
24 
25 
26 static 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 
43 static void p2ps_add_new_group_info(struct p2p_data *p2p,
44 				    struct p2p_device *dev,
45 				    struct wpabuf *buf)
46 {
47 	int found;
48 	u8 intended_addr[ETH_ALEN];
49 	u8 ssid[SSID_MAX_LEN];
50 	size_t ssid_len;
51 	int group_iface;
52 	unsigned int force_freq;
53 
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,
59 		&ssid_len, &group_iface, &force_freq);
60 	if (found) {
61 		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 		}
68 		p2p_buf_add_group_id(buf, p2p->cfg->dev_addr,
69 				     ssid, ssid_len);
70 
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);
75 	} 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 
95 static 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;
99 	struct p2ps_feature_capab fcap = { prov->cpt_mask, 0 };
100 	int shared_group = 0;
101 	u8 ssid[SSID_MAX_LEN];
102 	size_t ssid_len;
103 	u8 go_dev_addr[ETH_ALEN];
104 	u8 intended_addr[ETH_ALEN];
105 	int follow_on_req_fail = prov->status >= 0 &&
106 		prov->status != P2P_SC_SUCCESS_DEFERRED;
107 
108 	/* If we might be explicite group owner, add GO details */
109 	if (!follow_on_req_fail &&
110 	    (prov->conncap & (P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_NEW)))
111 		p2ps_add_new_group_info(p2p, dev, buf);
112 
113 	if (prov->status >= 0)
114 		p2p_buf_add_status(buf, (u8) prov->status);
115 	else
116 		prov->method = config_methods;
117 
118 	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 		}
125 
126 		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);
130 
131 		if ((shared_group && !is_zero_ether_addr(intended_addr)) ||
132 		    (prov->conncap & (P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_NEW)))
133 			p2p_buf_add_operating_channel(buf, p2p->cfg->country,
134 						      p2p->op_reg_class,
135 						      p2p->op_channel);
136 	}
137 
138 	if (prov->status < 0 && prov->info[0])
139 		p2p_buf_add_session_info(buf, prov->info);
140 
141 	if (!follow_on_req_fail)
142 		p2p_buf_add_connection_capability(buf, prov->conncap);
143 
144 	p2p_buf_add_advertisement_id(buf, prov->adv_id, prov->adv_mac);
145 
146 	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 		}
156 
157 		p2p_buf_add_listen_channel(buf, p2p->cfg->country,
158 					   p2p->cfg->reg_class,
159 					   p2p->cfg->channel);
160 	}
161 
162 	p2p_buf_add_session_id(buf, prov->session_id, prov->session_mac);
163 
164 	p2p_buf_add_feature_capability(buf, sizeof(fcap), (const u8 *) &fcap);
165 
166 	if (shared_group) {
167 		p2p_buf_add_persistent_group_info(buf, go_dev_addr,
168 						  ssid, ssid_len);
169 		/* 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 	}
175 }
176 
177 
178 static struct wpabuf * p2p_build_prov_disc_req(struct p2p_data *p2p,
179 					       struct p2p_device *dev,
180 					       int join)
181 {
182 	struct wpabuf *buf;
183 	u8 *len;
184 	size_t extra = 0;
185 	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;
189 
190 #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 
195 	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 
198 	if (p2p->p2ps_prov)
199 		extra += os_strlen(p2p->p2ps_prov->info) + 1 +
200 			sizeof(struct p2ps_provision);
201 
202 	buf = wpabuf_alloc(1000 + extra);
203 	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);
209 
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 	}
219 	p2p_buf_add_capability(buf, p2p->dev_capab &
220 			       ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY,
221 			       group_capab);
222 	p2p_buf_add_device_info(buf, p2p, NULL);
223 	if (p2p->p2ps_prov) {
224 		p2ps_add_pd_req_attrs(p2p, dev, buf, config_methods);
225 	} else if (go) {
226 		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 
234 #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 
239 	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 
242 	return buf;
243 }
244 
245 
246 static struct wpabuf * p2p_build_prov_disc_resp(struct p2p_data *p2p,
247 						struct p2p_device *dev,
248 						u8 dialog_token,
249 						enum p2p_status_code status,
250 						u16 config_methods,
251 						u32 adv_id,
252 						const u8 *group_id,
253 						size_t group_id_len,
254 						const u8 *persist_ssid,
255 						size_t persist_ssid_len,
256 						const u8 *fcap,
257 						u16 fcap_len)
258 {
259 	struct wpabuf *buf;
260 	size_t extra = 0;
261 	int persist = 0;
262 
263 #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 
284 	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 
287 	buf = wpabuf_alloc(1000 + extra);
288 	if (buf == NULL)
289 		return NULL;
290 
291 	p2p_buf_add_public_action_hdr(buf, P2P_PROV_DISC_RESP, dialog_token);
292 
293 	/* Add P2P IE for P2PS */
294 	if (p2p->p2ps_prov && p2p->p2ps_prov->adv_id == adv_id) {
295 		u8 *len = p2p_buf_add_ie_hdr(buf);
296 		struct p2ps_provision *prov = p2p->p2ps_prov;
297 		u8 group_capab;
298 		u8 conncap = 0;
299 
300 		if (status == P2P_SC_SUCCESS ||
301 		    status == P2P_SC_SUCCESS_DEFERRED)
302 			conncap = prov->conncap;
303 
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 
319 		if (persist_ssid && p2p->cfg->get_persistent_group && dev &&
320 		    (status == P2P_SC_SUCCESS ||
321 		     status == P2P_SC_SUCCESS_DEFERRED)) {
322 			u8 ssid[SSID_MAX_LEN];
323 			size_t ssid_len;
324 			u8 go_dev_addr[ETH_ALEN];
325 			u8 intended_addr[ETH_ALEN];
326 
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,
331 				ssid, &ssid_len, intended_addr);
332 			if (persist) {
333 				p2p_buf_add_persistent_group_info(
334 					buf, go_dev_addr, ssid, ssid_len);
335 				if (!is_zero_ether_addr(intended_addr))
336 					p2p_buf_add_intended_addr(
337 						buf, intended_addr);
338 			}
339 		}
340 
341 		if (!persist && (conncap & P2PS_SETUP_GROUP_OWNER))
342 			p2ps_add_new_group_info(p2p, dev, buf);
343 
344 		/* Add Operating Channel if conncap indicates GO */
345 		if (persist || (conncap & P2PS_SETUP_GROUP_OWNER)) {
346 			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 
358 		if (persist ||
359 		    (conncap & (P2PS_SETUP_CLIENT | P2PS_SETUP_GROUP_OWNER)))
360 			p2p_buf_add_channel_list(buf, p2p->cfg->country,
361 						 &p2p->channels);
362 
363 		if (!persist && conncap)
364 			p2p_buf_add_connection_capability(buf, conncap);
365 
366 		p2p_buf_add_advertisement_id(buf, adv_id, prov->adv_mac);
367 
368 		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);
372 
373 		p2p_buf_add_session_id(buf, prov->session_id,
374 				       prov->session_mac);
375 
376 		p2p_buf_add_feature_capability(buf, fcap_len, fcap);
377 		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 
390 	/* WPS IE with Config Methods attribute */
391 	p2p_build_wps_ie_config_methods(buf, config_methods);
392 
393 #ifdef CONFIG_WIFI_DISPLAY
394 	if (wfd_ie)
395 		wpabuf_put_buf(buf, wfd_ie);
396 #endif /* CONFIG_WIFI_DISPLAY */
397 
398 	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 
401 	return buf;
402 }
403 
404 
405 static 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 
431 static 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 */
444 static 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) \
455 do { \
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 
554 void 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;
560 	enum p2p_status_code reject = P2P_SC_FAIL_INCOMPATIBLE_PARAMS;
561 	struct wpabuf *resp;
562 	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;
567 	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;
570 	int passwd_id = DEV_PW_DEFAULT;
571 	u16 config_methods;
572 	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;
577 
578 	if (p2p_parse(data, len, &msg))
579 		return;
580 
581 	p2p_dbg(p2p, "Received Provision Discovery Request from " MACSTR
582 		" with config methods 0x%x (freq=%d)",
583 		MAC2STR(sa), msg.wps_config_methods, rx_freq);
584 	group_mac = msg.intended_addr;
585 
586 	dev = p2p_get_device(p2p, sa);
587 	if (dev == NULL || (dev->flags & P2P_DEV_PROBE_REQ_ONLY)) {
588 		p2p_dbg(p2p, "Provision Discovery Request from unknown peer "
589 			MACSTR, MAC2STR(sa));
590 
591 		if (p2p_add_device(p2p, sa, rx_freq, NULL, 0, data + 1, len - 1,
592 				   0)) {
593 			p2p_dbg(p2p, "Provision Discovery Request add device failed "
594 				MACSTR, MAC2STR(sa));
595 			goto out;
596 		}
597 
598 		dev = p2p_get_device(p2p, sa);
599 		if (!dev) {
600 			p2p_dbg(p2p,
601 				"Provision Discovery device not found "
602 				MACSTR, MAC2STR(sa));
603 			goto out;
604 		}
605 	} else if (msg.wfd_subelems) {
606 		wpabuf_free(dev->info.wfd_subelems);
607 		dev->info.wfd_subelems = wpabuf_dup(msg.wfd_subelems);
608 	}
609 
610 	if (!msg.adv_id) {
611 		allowed_config_methods |= WPS_CONFIG_PUSHBUTTON;
612 		if (!(msg.wps_config_methods & allowed_config_methods)) {
613 			p2p_dbg(p2p,
614 				"Unsupported Config Methods in Provision Discovery Request");
615 			goto out;
616 		}
617 
618 		/* Legacy (non-P2PS) - Unknown groups allowed for P2PS */
619 		if (msg.group_id) {
620 			size_t i;
621 
622 			for (i = 0; i < p2p->num_groups; i++) {
623 				if (p2p_group_is_group_id_match(
624 					    p2p->groups[i],
625 					    msg.group_id, msg.group_id_len))
626 					break;
627 			}
628 			if (i == p2p->num_groups) {
629 				p2p_dbg(p2p,
630 					"PD request for unknown P2P Group ID - reject");
631 				goto out;
632 			}
633 		}
634 	} else {
635 		allowed_config_methods |= WPS_CONFIG_P2PS;
636 
637 		/*
638 		 * Set adv_id here, so in case of an error, a P2PS PD Response
639 		 * will be sent.
640 		 */
641 		adv_id = WPA_GET_LE32(msg.adv_id);
642 		if (p2ps_validate_pd_req(p2p, &msg, sa) < 0) {
643 			reject = P2P_SC_FAIL_INVALID_PARAMS;
644 			goto out;
645 		}
646 
647 		req_fcap = (struct p2ps_feature_capab *) msg.feature_cap;
648 
649 		os_memcpy(session_mac, msg.session_mac, ETH_ALEN);
650 		os_memcpy(adv_mac, msg.adv_mac, ETH_ALEN);
651 
652 		session_id = WPA_GET_LE32(msg.session_id);
653 
654 		if (msg.conn_cap)
655 			conncap = *msg.conn_cap;
656 
657 		/*
658 		 * We need to verify a P2PS config methog in an initial PD
659 		 * request or in a follow-on PD request with the status
660 		 * SUCCESS_DEFERRED.
661 		 */
662 		if ((!msg.status || *msg.status == P2P_SC_SUCCESS_DEFERRED) &&
663 		    !(msg.wps_config_methods & allowed_config_methods)) {
664 			p2p_dbg(p2p,
665 				"Unsupported Config Methods in Provision Discovery Request");
666 			goto out;
667 		}
668 
669 		/*
670 		 * TODO: since we don't support multiple PD, reject PD request
671 		 * if we are in the middle of P2PS PD with some other peer
672 		 */
673 	}
674 
675 	dev->flags &= ~(P2P_DEV_PD_PEER_DISPLAY |
676 			P2P_DEV_PD_PEER_KEYPAD |
677 			P2P_DEV_PD_PEER_P2PS);
678 
679 	if (msg.wps_config_methods & WPS_CONFIG_DISPLAY) {
680 		p2p_dbg(p2p, "Peer " MACSTR
681 			" requested us to show a PIN on display", MAC2STR(sa));
682 		dev->flags |= P2P_DEV_PD_PEER_KEYPAD;
683 		passwd_id = DEV_PW_USER_SPECIFIED;
684 	} else if (msg.wps_config_methods & WPS_CONFIG_KEYPAD) {
685 		p2p_dbg(p2p, "Peer " MACSTR
686 			" requested us to write its PIN using keypad",
687 			MAC2STR(sa));
688 		dev->flags |= P2P_DEV_PD_PEER_DISPLAY;
689 		passwd_id = DEV_PW_REGISTRAR_SPECIFIED;
690 	} else if (msg.wps_config_methods & WPS_CONFIG_P2PS) {
691 		p2p_dbg(p2p, "Peer " MACSTR " requesting P2PS PIN",
692 			MAC2STR(sa));
693 		dev->flags |= P2P_DEV_PD_PEER_P2PS;
694 		passwd_id = DEV_PW_P2PS_DEFAULT;
695 	}
696 
697 	/* Remove stale persistent groups */
698 	if (p2p->cfg->remove_stale_groups) {
699 		p2p->cfg->remove_stale_groups(
700 			p2p->cfg->cb_ctx, dev->info.p2p_device_addr,
701 			msg.persistent_dev,
702 			msg.persistent_ssid, msg.persistent_ssid_len);
703 	}
704 
705 	reject = P2P_SC_SUCCESS;
706 
707 	/*
708 	 * End of a legacy P2P PD Request processing, from this point continue
709 	 * with P2PS one.
710 	 */
711 	if (!msg.adv_id)
712 		goto out;
713 
714 	remote_conncap = conncap;
715 
716 	if (!msg.status) {
717 		unsigned int forced_freq, pref_freq;
718 
719 		if (os_memcmp(p2p->cfg->dev_addr, msg.adv_mac, ETH_ALEN)) {
720 			p2p_dbg(p2p,
721 				"P2PS PD adv mac does not match the local one");
722 			reject = P2P_SC_FAIL_INCOMPATIBLE_PARAMS;
723 			goto out;
724 		}
725 
726 		p2ps_adv = p2p_service_p2ps_id(p2p, adv_id);
727 		if (!p2ps_adv) {
728 			p2p_dbg(p2p, "P2PS PD invalid adv_id=0x%X", adv_id);
729 			reject = P2P_SC_FAIL_INCOMPATIBLE_PARAMS;
730 			goto out;
731 		}
732 		p2p_dbg(p2p, "adv_id: 0x%X, p2ps_adv: %p", adv_id, p2ps_adv);
733 
734 		auto_accept = p2ps_adv->auto_accept;
735 		conncap = p2p->cfg->p2ps_group_capability(p2p->cfg->cb_ctx,
736 							  conncap, auto_accept,
737 							  &forced_freq,
738 							  &pref_freq);
739 
740 		p2p_dbg(p2p, "Conncap: local:%d remote:%d result:%d",
741 			auto_accept, remote_conncap, conncap);
742 
743 		p2p_prepare_channel(p2p, dev, forced_freq, pref_freq, 0);
744 
745 		resp_fcap.cpt = p2ps_own_preferred_cpt(p2ps_adv->cpt_priority,
746 						       req_fcap->cpt);
747 
748 		p2p_dbg(p2p, "cpt: service:0x%x remote:0x%x result:0x%x",
749 			p2ps_adv->cpt_mask, req_fcap->cpt, resp_fcap.cpt);
750 
751 		if (!resp_fcap.cpt) {
752 			p2p_dbg(p2p,
753 				"Incompatible P2PS feature capability CPT bitmask");
754 			reject = P2P_SC_FAIL_INCOMPATIBLE_PARAMS;
755 		} else if (p2ps_adv->config_methods &&
756 			   !(msg.wps_config_methods &
757 			     p2ps_adv->config_methods)) {
758 			p2p_dbg(p2p,
759 				"Unsupported config methods in Provision Discovery Request (own=0x%x peer=0x%x)",
760 				p2ps_adv->config_methods,
761 				msg.wps_config_methods);
762 			reject = P2P_SC_FAIL_INCOMPATIBLE_PARAMS;
763 		} else if (!p2ps_adv->state) {
764 			p2p_dbg(p2p, "P2PS state unavailable");
765 			reject = P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
766 		} else if (!conncap) {
767 			p2p_dbg(p2p, "Conncap resolution failed");
768 			reject = P2P_SC_FAIL_INCOMPATIBLE_PARAMS;
769 		}
770 
771 		if (msg.wps_config_methods & WPS_CONFIG_KEYPAD) {
772 			p2p_dbg(p2p, "Keypad - always defer");
773 			auto_accept = 0;
774 		}
775 
776 		if ((remote_conncap & (P2PS_SETUP_NEW | P2PS_SETUP_CLIENT) ||
777 		     msg.persistent_dev) && conncap != P2PS_SETUP_NEW &&
778 		    msg.channel_list && msg.channel_list_len &&
779 		    p2p_peer_channels_check(p2p, &p2p->channels, dev,
780 					    msg.channel_list,
781 					    msg.channel_list_len) < 0) {
782 			p2p_dbg(p2p,
783 				"No common channels - force deferred flow");
784 			auto_accept = 0;
785 		}
786 
787 		if (((remote_conncap & P2PS_SETUP_GROUP_OWNER) ||
788 		     msg.persistent_dev) && msg.operating_channel) {
789 			struct p2p_channels intersect;
790 
791 			/*
792 			 * There are cases where only the operating channel is
793 			 * provided. This requires saving the channel as the
794 			 * supported channel list, and verifying that it is
795 			 * supported.
796 			 */
797 			if (dev->channels.reg_classes == 0 ||
798 			    !p2p_channels_includes(&dev->channels,
799 						   msg.operating_channel[3],
800 						   msg.operating_channel[4])) {
801 				struct p2p_channels *ch = &dev->channels;
802 
803 				os_memset(ch, 0, sizeof(*ch));
804 				ch->reg_class[0].reg_class =
805 					msg.operating_channel[3];
806 				ch->reg_class[0].channel[0] =
807 					msg.operating_channel[4];
808 				ch->reg_class[0].channels = 1;
809 				ch->reg_classes = 1;
810 			}
811 
812 			p2p_channels_intersect(&p2p->channels, &dev->channels,
813 					       &intersect);
814 
815 			if (intersect.reg_classes == 0) {
816 				p2p_dbg(p2p,
817 					"No common channels - force deferred flow");
818 				auto_accept = 0;
819 			}
820 		}
821 
822 		if (auto_accept || reject != P2P_SC_SUCCESS) {
823 			struct p2ps_provision *tmp;
824 
825 			if (p2ps_setup_p2ps_prov(p2p, adv_id, session_id,
826 						 msg.wps_config_methods,
827 						 session_mac, adv_mac) < 0) {
828 				reject = P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
829 				goto out;
830 			}
831 
832 			tmp = p2p->p2ps_prov;
833 			tmp->force_freq = forced_freq;
834 			tmp->pref_freq = pref_freq;
835 			if (conncap) {
836 				tmp->conncap = conncap;
837 				tmp->status = P2P_SC_SUCCESS;
838 			} else {
839 				tmp->conncap = auto_accept;
840 				tmp->status = P2P_SC_FAIL_INCOMPATIBLE_PARAMS;
841 			}
842 
843 			if (reject != P2P_SC_SUCCESS)
844 				goto out;
845 		}
846 	}
847 
848 	if (!msg.status && !auto_accept &&
849 	    (!p2p->p2ps_prov || p2p->p2ps_prov->adv_id != adv_id)) {
850 		struct p2ps_provision *tmp;
851 
852 		if (!conncap) {
853 			reject = P2P_SC_FAIL_INCOMPATIBLE_PARAMS;
854 			goto out;
855 		}
856 
857 		if (p2ps_setup_p2ps_prov(p2p, adv_id, session_id,
858 					 msg.wps_config_methods,
859 					 session_mac, adv_mac) < 0) {
860 			reject = P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
861 			goto out;
862 		}
863 		tmp = p2p->p2ps_prov;
864 		reject = P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
865 		tmp->status = reject;
866 	}
867 
868 	/* Not a P2PS Follow-on PD */
869 	if (!msg.status)
870 		goto out;
871 
872 	if (*msg.status && *msg.status != P2P_SC_SUCCESS_DEFERRED) {
873 		reject = *msg.status;
874 		goto out;
875 	}
876 
877 	if (*msg.status != P2P_SC_SUCCESS_DEFERRED || !p2p->p2ps_prov)
878 		goto out;
879 
880 	if (p2p->p2ps_prov->adv_id != adv_id ||
881 	    os_memcmp(p2p->p2ps_prov->adv_mac, msg.adv_mac, ETH_ALEN)) {
882 		p2p_dbg(p2p,
883 			"P2PS Follow-on PD with mismatch Advertisement ID/MAC");
884 		goto out;
885 	}
886 
887 	if (p2p->p2ps_prov->session_id != session_id ||
888 	    os_memcmp(p2p->p2ps_prov->session_mac, msg.session_mac, ETH_ALEN)) {
889 		p2p_dbg(p2p, "P2PS Follow-on PD with mismatch Session ID/MAC");
890 		goto out;
891 	}
892 
893 	method = p2p->p2ps_prov->method;
894 
895 	conncap = p2p->cfg->p2ps_group_capability(p2p->cfg->cb_ctx,
896 						  remote_conncap,
897 						  p2p->p2ps_prov->conncap,
898 						  &p2p->p2ps_prov->force_freq,
899 						  &p2p->p2ps_prov->pref_freq);
900 
901 	resp_fcap.cpt = p2ps_own_preferred_cpt(p2p->p2ps_prov->cpt_priority,
902 					       req_fcap->cpt);
903 
904 	p2p_dbg(p2p, "cpt: local:0x%x remote:0x%x result:0x%x",
905 		p2p->p2ps_prov->cpt_mask, req_fcap->cpt, resp_fcap.cpt);
906 
907 	p2p_prepare_channel(p2p, dev, p2p->p2ps_prov->force_freq,
908 			    p2p->p2ps_prov->pref_freq, 0);
909 
910 	/*
911 	 * Ensure that if we asked for PIN originally, our method is consistent
912 	 * with original request.
913 	 */
914 	if (method & WPS_CONFIG_DISPLAY)
915 		method = WPS_CONFIG_KEYPAD;
916 	else if (method & WPS_CONFIG_KEYPAD)
917 		method = WPS_CONFIG_DISPLAY;
918 
919 	if (!conncap || !(msg.wps_config_methods & method)) {
920 		/*
921 		 * Reject this "Deferred Accept*
922 		 * if incompatible conncap or method
923 		 */
924 		reject = P2P_SC_FAIL_INCOMPATIBLE_PARAMS;
925 	} else if (!resp_fcap.cpt) {
926 		p2p_dbg(p2p,
927 			"Incompatible P2PS feature capability CPT bitmask");
928 		reject = P2P_SC_FAIL_INCOMPATIBLE_PARAMS;
929 	} else if ((remote_conncap & (P2PS_SETUP_NEW | P2PS_SETUP_CLIENT) ||
930 		    msg.persistent_dev) && conncap != P2PS_SETUP_NEW &&
931 		   msg.channel_list && msg.channel_list_len &&
932 		   p2p_peer_channels_check(p2p, &p2p->channels, dev,
933 					   msg.channel_list,
934 					   msg.channel_list_len) < 0) {
935 		p2p_dbg(p2p,
936 			"No common channels in Follow-On Provision Discovery Request");
937 		reject = P2P_SC_FAIL_NO_COMMON_CHANNELS;
938 	} else {
939 		reject = P2P_SC_SUCCESS;
940 	}
941 
942 	dev->oper_freq = 0;
943 	if (reject == P2P_SC_SUCCESS || reject == P2P_SC_SUCCESS_DEFERRED) {
944 		u8 tmp;
945 
946 		if (msg.operating_channel)
947 			dev->oper_freq =
948 				p2p_channel_to_freq(msg.operating_channel[3],
949 						    msg.operating_channel[4]);
950 
951 		if ((conncap & P2PS_SETUP_GROUP_OWNER) &&
952 		    p2p_go_select_channel(p2p, dev, &tmp) < 0)
953 			reject = P2P_SC_FAIL_NO_COMMON_CHANNELS;
954 	}
955 
956 	p2p->p2ps_prov->status = reject;
957 	p2p->p2ps_prov->conncap = conncap;
958 
959 out:
960 	if (reject == P2P_SC_SUCCESS ||
961 	    reject == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE)
962 		config_methods = msg.wps_config_methods;
963 	else
964 		config_methods = 0;
965 
966 	/*
967 	 * Send PD Response for an initial PD Request or for follow-on
968 	 * PD Request with P2P_SC_SUCCESS_DEFERRED status.
969 	 */
970 	if (!msg.status || *msg.status == P2P_SC_SUCCESS_DEFERRED) {
971 		resp = p2p_build_prov_disc_resp(p2p, dev, msg.dialog_token,
972 						reject, config_methods, adv_id,
973 						msg.group_id, msg.group_id_len,
974 						msg.persistent_ssid,
975 						msg.persistent_ssid_len,
976 						(const u8 *) &resp_fcap,
977 						sizeof(resp_fcap));
978 		if (!resp) {
979 			p2p_parse_free(&msg);
980 			return;
981 		}
982 		p2p_dbg(p2p, "Sending Provision Discovery Response");
983 		if (rx_freq > 0)
984 			freq = rx_freq;
985 		else
986 			freq = p2p_channel_to_freq(p2p->cfg->reg_class,
987 						   p2p->cfg->channel);
988 		if (freq < 0) {
989 			p2p_dbg(p2p, "Unknown regulatory class/channel");
990 			wpabuf_free(resp);
991 			p2p_parse_free(&msg);
992 			return;
993 		}
994 		p2p->pending_action_state = P2P_PENDING_PD_RESPONSE;
995 		if (p2p_send_action(p2p, freq, sa, p2p->cfg->dev_addr,
996 				    p2p->cfg->dev_addr,
997 				    wpabuf_head(resp), wpabuf_len(resp),
998 				    50) < 0)
999 			p2p_dbg(p2p, "Failed to send Action frame");
1000 		else
1001 			p2p->send_action_in_progress = 1;
1002 
1003 		wpabuf_free(resp);
1004 	}
1005 
1006 	if (!dev) {
1007 		p2p_parse_free(&msg);
1008 		return;
1009 	}
1010 
1011 	freq = 0;
1012 	if (reject == P2P_SC_SUCCESS && conncap == P2PS_SETUP_GROUP_OWNER) {
1013 		freq = p2p_channel_to_freq(p2p->op_reg_class,
1014 					   p2p->op_channel);
1015 		if (freq < 0)
1016 			freq = 0;
1017 	}
1018 
1019 	if (!p2p->cfg->p2ps_prov_complete) {
1020 		/* Don't emit anything */
1021 	} else if (msg.status && *msg.status != P2P_SC_SUCCESS &&
1022 		   *msg.status != P2P_SC_SUCCESS_DEFERRED) {
1023 		reject = *msg.status;
1024 		p2p->cfg->p2ps_prov_complete(p2p->cfg->cb_ctx, reject,
1025 					     sa, adv_mac, session_mac,
1026 					     NULL, adv_id, session_id,
1027 					     0, 0, msg.persistent_ssid,
1028 					     msg.persistent_ssid_len,
1029 					     0, 0, NULL, NULL, 0, freq,
1030 					     NULL, 0);
1031 	} else if (msg.status && *msg.status == P2P_SC_SUCCESS_DEFERRED &&
1032 		   p2p->p2ps_prov) {
1033 		p2p->p2ps_prov->status = reject;
1034 		p2p->p2ps_prov->conncap = conncap;
1035 
1036 		if (reject != P2P_SC_SUCCESS)
1037 			p2p->cfg->p2ps_prov_complete(p2p->cfg->cb_ctx, reject,
1038 						     sa, adv_mac, session_mac,
1039 						     NULL, adv_id,
1040 						     session_id, conncap, 0,
1041 						     msg.persistent_ssid,
1042 						     msg.persistent_ssid_len, 0,
1043 						     0, NULL, NULL, 0, freq,
1044 						     NULL, 0);
1045 		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,
1054 						     0, NULL,
1055 						     (const u8 *) &resp_fcap,
1056 						     sizeof(resp_fcap), freq,
1057 						     NULL, 0);
1058 	} else if (msg.status && p2p->p2ps_prov) {
1059 		p2p->p2ps_prov->status = P2P_SC_SUCCESS;
1060 		p2p->cfg->p2ps_prov_complete(p2p->cfg->cb_ctx, *msg.status, sa,
1061 					     adv_mac, session_mac, group_mac,
1062 					     adv_id, session_id, conncap,
1063 					     passwd_id,
1064 					     msg.persistent_ssid,
1065 					     msg.persistent_ssid_len,
1066 					     0, 0, NULL,
1067 					     (const u8 *) &resp_fcap,
1068 					     sizeof(resp_fcap), freq, NULL, 0);
1069 	} else if (msg.status) {
1070 	} else if (auto_accept && reject == P2P_SC_SUCCESS) {
1071 		p2p->cfg->p2ps_prov_complete(p2p->cfg->cb_ctx, P2P_SC_SUCCESS,
1072 					     sa, adv_mac, session_mac,
1073 					     group_mac, adv_id, session_id,
1074 					     conncap, passwd_id,
1075 					     msg.persistent_ssid,
1076 					     msg.persistent_ssid_len,
1077 					     0, 0, NULL,
1078 					     (const u8 *) &resp_fcap,
1079 					     sizeof(resp_fcap), freq,
1080 					     msg.group_id ?
1081 					     msg.group_id + ETH_ALEN : NULL,
1082 					     msg.group_id ?
1083 					     msg.group_id_len - ETH_ALEN : 0);
1084 	} else if (reject == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE &&
1085 		   (!msg.session_info || !msg.session_info_len)) {
1086 		p2p->p2ps_prov->method = msg.wps_config_methods;
1087 
1088 		p2p->cfg->p2ps_prov_complete(p2p->cfg->cb_ctx, P2P_SC_SUCCESS,
1089 					     sa, adv_mac, session_mac,
1090 					     group_mac, adv_id, session_id,
1091 					     conncap, passwd_id,
1092 					     msg.persistent_ssid,
1093 					     msg.persistent_ssid_len,
1094 					     0, 1, NULL,
1095 					     (const u8 *) &resp_fcap,
1096 					     sizeof(resp_fcap), freq, NULL, 0);
1097 	} else if (reject == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
1098 		size_t buf_len = msg.session_info_len;
1099 		char *buf = os_malloc(2 * buf_len + 1);
1100 
1101 		if (buf) {
1102 			p2p->p2ps_prov->method = msg.wps_config_methods;
1103 
1104 			utf8_escape((char *) msg.session_info, buf_len,
1105 				    buf, 2 * buf_len + 1);
1106 
1107 			p2p->cfg->p2ps_prov_complete(
1108 				p2p->cfg->cb_ctx, P2P_SC_SUCCESS, sa,
1109 				adv_mac, session_mac, group_mac, adv_id,
1110 				session_id, conncap, passwd_id,
1111 				msg.persistent_ssid, msg.persistent_ssid_len,
1112 				0, 1, buf,
1113 				(const u8 *) &resp_fcap, sizeof(resp_fcap),
1114 				freq, NULL, 0);
1115 
1116 			os_free(buf);
1117 		}
1118 	}
1119 
1120 	/*
1121 	 * prov_disc_req callback is used to generate P2P-PROV-DISC-ENTER-PIN,
1122 	 * P2P-PROV-DISC-SHOW-PIN, and P2P-PROV-DISC-PBC-REQ events.
1123 	 * Call it either on legacy P2P PD or on P2PS PD only if we need to
1124 	 * enter/show PIN.
1125 	 *
1126 	 * The callback is called in the following cases:
1127 	 * 1. Legacy P2P PD request, response status SUCCESS
1128 	 * 2. P2PS advertiser, method: DISPLAY, autoaccept: TRUE,
1129 	 *    response status: SUCCESS
1130 	 * 3. P2PS advertiser, method  DISPLAY, autoaccept: FALSE,
1131 	 *    response status: INFO_CURRENTLY_UNAVAILABLE
1132 	 * 4. P2PS advertiser, method: KEYPAD, autoaccept==any,
1133 	 *    response status: INFO_CURRENTLY_UNAVAILABLE
1134 	 * 5. P2PS follow-on with SUCCESS_DEFERRED,
1135 	 *    advertiser role: DISPLAY, autoaccept: FALSE,
1136 	 *    seeker: KEYPAD, response status: SUCCESS
1137 	 */
1138 	if (p2p->cfg->prov_disc_req &&
1139 	    ((reject == P2P_SC_SUCCESS && !msg.adv_id) ||
1140 	     (!msg.status &&
1141 	     (reject == P2P_SC_SUCCESS ||
1142 	      reject == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) &&
1143 	      passwd_id == DEV_PW_USER_SPECIFIED) ||
1144 	     (!msg.status &&
1145 	      reject == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE &&
1146 	      passwd_id == DEV_PW_REGISTRAR_SPECIFIED) ||
1147 	     (reject == P2P_SC_SUCCESS &&
1148 	      msg.status && *msg.status == P2P_SC_SUCCESS_DEFERRED &&
1149 	       passwd_id == DEV_PW_REGISTRAR_SPECIFIED))) {
1150 		const u8 *dev_addr = sa;
1151 
1152 		if (msg.p2p_device_addr)
1153 			dev_addr = msg.p2p_device_addr;
1154 		p2p->cfg->prov_disc_req(p2p->cfg->cb_ctx, sa,
1155 					msg.wps_config_methods,
1156 					dev_addr, msg.pri_dev_type,
1157 					msg.device_name, msg.config_methods,
1158 					msg.capability ? msg.capability[0] : 0,
1159 					msg.capability ? msg.capability[1] :
1160 					0,
1161 					msg.group_id, msg.group_id_len);
1162 	}
1163 
1164 	if (reject != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE)
1165 		p2ps_prov_free(p2p);
1166 
1167 	if (reject == P2P_SC_SUCCESS) {
1168 		switch (config_methods) {
1169 		case WPS_CONFIG_DISPLAY:
1170 			dev->wps_prov_info = WPS_CONFIG_KEYPAD;
1171 			break;
1172 		case WPS_CONFIG_KEYPAD:
1173 			dev->wps_prov_info = WPS_CONFIG_DISPLAY;
1174 			break;
1175 		case WPS_CONFIG_PUSHBUTTON:
1176 			dev->wps_prov_info = WPS_CONFIG_PUSHBUTTON;
1177 			break;
1178 		case WPS_CONFIG_P2PS:
1179 			dev->wps_prov_info = WPS_CONFIG_P2PS;
1180 			break;
1181 		default:
1182 			dev->wps_prov_info = 0;
1183 			break;
1184 		}
1185 
1186 		if (msg.intended_addr)
1187 			os_memcpy(dev->interface_addr, msg.intended_addr,
1188 				  ETH_ALEN);
1189 	}
1190 	p2p_parse_free(&msg);
1191 }
1192 
1193 
1194 static int p2p_validate_p2ps_pd_resp(struct p2p_data *p2p,
1195 				     struct p2p_message *msg)
1196 {
1197 	u8 conn_cap_go = 0;
1198 	u8 conn_cap_cli = 0;
1199 	u32 session_id;
1200 	u32 adv_id;
1201 
1202 #define P2PS_PD_RESP_CHECK(_val, _attr) \
1203 	do { \
1204 		if ((_val) && !msg->_attr) { \
1205 			p2p_dbg(p2p, "P2PS PD Response missing " #_attr); \
1206 			return -1; \
1207 		} \
1208 	} while (0)
1209 
1210 	P2PS_PD_RESP_CHECK(1, status);
1211 	P2PS_PD_RESP_CHECK(1, adv_id);
1212 	P2PS_PD_RESP_CHECK(1, adv_mac);
1213 	P2PS_PD_RESP_CHECK(1, capability);
1214 	P2PS_PD_RESP_CHECK(1, p2p_device_info);
1215 	P2PS_PD_RESP_CHECK(1, session_id);
1216 	P2PS_PD_RESP_CHECK(1, session_mac);
1217 	P2PS_PD_RESP_CHECK(1, feature_cap);
1218 
1219 	session_id = WPA_GET_LE32(msg->session_id);
1220 	adv_id = WPA_GET_LE32(msg->adv_id);
1221 
1222 	if (p2p->p2ps_prov->session_id != session_id) {
1223 		p2p_dbg(p2p,
1224 			"Ignore PD Response with unexpected Session ID");
1225 		return -1;
1226 	}
1227 
1228 	if (os_memcmp(p2p->p2ps_prov->session_mac, msg->session_mac,
1229 		      ETH_ALEN)) {
1230 		p2p_dbg(p2p,
1231 			"Ignore PD Response with unexpected Session MAC");
1232 		return -1;
1233 	}
1234 
1235 	if (p2p->p2ps_prov->adv_id != adv_id) {
1236 		p2p_dbg(p2p,
1237 			"Ignore PD Response with unexpected Advertisement ID");
1238 		return -1;
1239 	}
1240 
1241 	if (os_memcmp(p2p->p2ps_prov->adv_mac, msg->adv_mac, ETH_ALEN) != 0) {
1242 		p2p_dbg(p2p,
1243 			"Ignore PD Response with unexpected Advertisement MAC");
1244 		return -1;
1245 	}
1246 
1247 	if (msg->listen_channel) {
1248 		p2p_dbg(p2p,
1249 			"Ignore malformed PD Response - unexpected Listen Channel");
1250 		return -1;
1251 	}
1252 
1253 	if (*msg->status == P2P_SC_SUCCESS &&
1254 	    !(!!msg->conn_cap ^ !!msg->persistent_dev)) {
1255 		p2p_dbg(p2p,
1256 			"Ignore malformed PD Response - either conn_cap or persistent group should be present");
1257 		return -1;
1258 	}
1259 
1260 	if (msg->persistent_dev && *msg->status != P2P_SC_SUCCESS) {
1261 		p2p_dbg(p2p,
1262 			"Ignore malformed PD Response - persistent group is present, but the status isn't success");
1263 		return -1;
1264 	}
1265 
1266 	if (msg->conn_cap) {
1267 		conn_cap_go = *msg->conn_cap == P2PS_SETUP_GROUP_OWNER;
1268 		conn_cap_cli = *msg->conn_cap == P2PS_SETUP_CLIENT;
1269 	}
1270 
1271 	P2PS_PD_RESP_CHECK(msg->persistent_dev || conn_cap_go || conn_cap_cli,
1272 			   channel_list);
1273 	P2PS_PD_RESP_CHECK(msg->persistent_dev || conn_cap_go || conn_cap_cli,
1274 			   config_timeout);
1275 
1276 	P2PS_PD_RESP_CHECK(conn_cap_go, group_id);
1277 	P2PS_PD_RESP_CHECK(conn_cap_go, intended_addr);
1278 	P2PS_PD_RESP_CHECK(conn_cap_go, operating_channel);
1279 	/*
1280 	 * TODO: Also validate that operating channel is present if the device
1281 	 * is a GO in a persistent group. We can't do it here since we don't
1282 	 * know what is the role of the peer. It should be probably done in
1283 	 * p2ps_prov_complete callback, but currently operating channel isn't
1284 	 * passed to it.
1285 	 */
1286 
1287 #undef P2PS_PD_RESP_CHECK
1288 
1289 	return 0;
1290 }
1291 
1292 
1293 void p2p_process_prov_disc_resp(struct p2p_data *p2p, const u8 *sa,
1294 				const u8 *data, size_t len)
1295 {
1296 	struct p2p_message msg;
1297 	struct p2p_device *dev;
1298 	u16 report_config_methods = 0, req_config_methods;
1299 	u8 status = P2P_SC_SUCCESS;
1300 	u32 adv_id = 0;
1301 	u8 conncap = P2PS_SETUP_NEW;
1302 	u8 adv_mac[ETH_ALEN];
1303 	const u8 *group_mac;
1304 	int passwd_id = DEV_PW_DEFAULT;
1305 	int p2ps_seeker;
1306 
1307 	if (p2p_parse(data, len, &msg))
1308 		return;
1309 
1310 	if (p2p->p2ps_prov && p2p_validate_p2ps_pd_resp(p2p, &msg)) {
1311 		p2p_parse_free(&msg);
1312 		return;
1313 	}
1314 
1315 	/* Parse the P2PS members present */
1316 	if (msg.status)
1317 		status = *msg.status;
1318 
1319 	group_mac = msg.intended_addr;
1320 
1321 	if (msg.adv_mac)
1322 		os_memcpy(adv_mac, msg.adv_mac, ETH_ALEN);
1323 	else
1324 		os_memset(adv_mac, 0, ETH_ALEN);
1325 
1326 	if (msg.adv_id)
1327 		adv_id = WPA_GET_LE32(msg.adv_id);
1328 
1329 	if (msg.conn_cap) {
1330 		conncap = *msg.conn_cap;
1331 
1332 		/* Switch bits to local relative */
1333 		switch (conncap) {
1334 		case P2PS_SETUP_GROUP_OWNER:
1335 			conncap = P2PS_SETUP_CLIENT;
1336 			break;
1337 		case P2PS_SETUP_CLIENT:
1338 			conncap = P2PS_SETUP_GROUP_OWNER;
1339 			break;
1340 		}
1341 	}
1342 
1343 	p2p_dbg(p2p, "Received Provision Discovery Response from " MACSTR
1344 		" with config methods 0x%x",
1345 		MAC2STR(sa), msg.wps_config_methods);
1346 
1347 	dev = p2p_get_device(p2p, sa);
1348 	if (dev == NULL || !dev->req_config_methods) {
1349 		p2p_dbg(p2p, "Ignore Provision Discovery Response from " MACSTR
1350 			" with no pending request", MAC2STR(sa));
1351 		p2p_parse_free(&msg);
1352 		return;
1353 	} else if (msg.wfd_subelems) {
1354 		wpabuf_free(dev->info.wfd_subelems);
1355 		dev->info.wfd_subelems = wpabuf_dup(msg.wfd_subelems);
1356 	}
1357 
1358 	if (dev->dialog_token != msg.dialog_token) {
1359 		p2p_dbg(p2p, "Ignore Provision Discovery Response with unexpected Dialog Token %u (expected %u)",
1360 			msg.dialog_token, dev->dialog_token);
1361 		p2p_parse_free(&msg);
1362 		return;
1363 	}
1364 
1365 	if (p2p->pending_action_state == P2P_PENDING_PD) {
1366 		os_memset(p2p->pending_pd_devaddr, 0, ETH_ALEN);
1367 		p2p->pending_action_state = P2P_NO_PENDING_ACTION;
1368 	}
1369 
1370 	p2ps_seeker = p2p->p2ps_prov && p2p->p2ps_prov->pd_seeker;
1371 
1372 	/*
1373 	 * Use a local copy of the requested config methods since
1374 	 * p2p_reset_pending_pd() can clear this in the peer entry.
1375 	 */
1376 	req_config_methods = dev->req_config_methods;
1377 
1378 	/*
1379 	 * If the response is from the peer to whom a user initiated request
1380 	 * was sent earlier, we reset that state info here.
1381 	 */
1382 	if (p2p->user_initiated_pd &&
1383 	    os_memcmp(p2p->pending_pd_devaddr, sa, ETH_ALEN) == 0)
1384 		p2p_reset_pending_pd(p2p);
1385 
1386 	if (msg.wps_config_methods != req_config_methods) {
1387 		p2p_dbg(p2p, "Peer rejected our Provision Discovery Request (received config_methods 0x%x expected 0x%x",
1388 			msg.wps_config_methods, req_config_methods);
1389 		if (p2p->cfg->prov_disc_fail)
1390 			p2p->cfg->prov_disc_fail(p2p->cfg->cb_ctx, sa,
1391 						 P2P_PROV_DISC_REJECTED,
1392 						 adv_id, adv_mac, NULL);
1393 		p2p_parse_free(&msg);
1394 		p2ps_prov_free(p2p);
1395 		goto out;
1396 	}
1397 
1398 	report_config_methods = req_config_methods;
1399 	dev->flags &= ~(P2P_DEV_PD_PEER_DISPLAY |
1400 			P2P_DEV_PD_PEER_KEYPAD |
1401 			P2P_DEV_PD_PEER_P2PS);
1402 	if (req_config_methods & WPS_CONFIG_DISPLAY) {
1403 		p2p_dbg(p2p, "Peer " MACSTR
1404 			" accepted to show a PIN on display", MAC2STR(sa));
1405 		dev->flags |= P2P_DEV_PD_PEER_DISPLAY;
1406 		passwd_id = DEV_PW_REGISTRAR_SPECIFIED;
1407 	} else if (msg.wps_config_methods & WPS_CONFIG_KEYPAD) {
1408 		p2p_dbg(p2p, "Peer " MACSTR
1409 			" accepted to write our PIN using keypad",
1410 			MAC2STR(sa));
1411 		dev->flags |= P2P_DEV_PD_PEER_KEYPAD;
1412 		passwd_id = DEV_PW_USER_SPECIFIED;
1413 	} else if (msg.wps_config_methods & WPS_CONFIG_P2PS) {
1414 		p2p_dbg(p2p, "Peer " MACSTR " accepted P2PS PIN",
1415 			MAC2STR(sa));
1416 		dev->flags |= P2P_DEV_PD_PEER_P2PS;
1417 		passwd_id = DEV_PW_P2PS_DEFAULT;
1418 	}
1419 
1420 	if ((status == P2P_SC_SUCCESS || status == P2P_SC_SUCCESS_DEFERRED) &&
1421 	    p2p->p2ps_prov) {
1422 		dev->oper_freq = 0;
1423 
1424 		/*
1425 		 * Save the reported channel list and operating frequency.
1426 		 * Note that the specification mandates that the responder
1427 		 * should include in the channel list only channels reported by
1428 		 * the initiator, so this is only a validity check, and if this
1429 		 * fails the flow would continue, although it would probably
1430 		 * fail. Same is true for the operating channel.
1431 		 */
1432 		if (msg.channel_list && msg.channel_list_len &&
1433 		    p2p_peer_channels_check(p2p, &p2p->channels, dev,
1434 					    msg.channel_list,
1435 					    msg.channel_list_len) < 0)
1436 			p2p_dbg(p2p, "P2PS PD Response - no common channels");
1437 
1438 		if (msg.operating_channel) {
1439 			if (p2p_channels_includes(&p2p->channels,
1440 						  msg.operating_channel[3],
1441 						  msg.operating_channel[4]) &&
1442 			    p2p_channels_includes(&dev->channels,
1443 						  msg.operating_channel[3],
1444 						  msg.operating_channel[4])) {
1445 				dev->oper_freq =
1446 					p2p_channel_to_freq(
1447 						msg.operating_channel[3],
1448 						msg.operating_channel[4]);
1449 			} else {
1450 				p2p_dbg(p2p,
1451 					"P2PS PD Response - invalid operating channel");
1452 			}
1453 		}
1454 
1455 		if (p2p->cfg->p2ps_prov_complete) {
1456 			int freq = 0;
1457 
1458 			if (conncap == P2PS_SETUP_GROUP_OWNER) {
1459 				u8 tmp;
1460 
1461 				/*
1462 				 * Re-select the operating channel as it is
1463 				 * possible that original channel is no longer
1464 				 * valid. This should not really fail.
1465 				 */
1466 				if (p2p_go_select_channel(p2p, dev, &tmp) < 0)
1467 					p2p_dbg(p2p,
1468 						"P2PS PD channel selection failed");
1469 
1470 				freq = p2p_channel_to_freq(p2p->op_reg_class,
1471 							   p2p->op_channel);
1472 				if (freq < 0)
1473 					freq = 0;
1474 			}
1475 
1476 			p2p->cfg->p2ps_prov_complete(
1477 				p2p->cfg->cb_ctx, status, sa, adv_mac,
1478 				p2p->p2ps_prov->session_mac,
1479 				group_mac, adv_id, p2p->p2ps_prov->session_id,
1480 				conncap, passwd_id, msg.persistent_ssid,
1481 				msg.persistent_ssid_len, 1, 0, NULL,
1482 				msg.feature_cap, msg.feature_cap_len, freq,
1483 				msg.group_id ? msg.group_id + ETH_ALEN : NULL,
1484 				msg.group_id ? msg.group_id_len - ETH_ALEN : 0);
1485 		}
1486 		p2ps_prov_free(p2p);
1487 	} else if (status != P2P_SC_SUCCESS &&
1488 		   status != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE &&
1489 		   status != P2P_SC_SUCCESS_DEFERRED && p2p->p2ps_prov) {
1490 		if (p2p->cfg->p2ps_prov_complete)
1491 			p2p->cfg->p2ps_prov_complete(
1492 				p2p->cfg->cb_ctx, status, sa, adv_mac,
1493 				p2p->p2ps_prov->session_mac,
1494 				group_mac, adv_id, p2p->p2ps_prov->session_id,
1495 				0, 0, NULL, 0, 1, 0, NULL, NULL, 0, 0, NULL, 0);
1496 		p2ps_prov_free(p2p);
1497 	}
1498 
1499 	if (status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
1500 		if (p2p->cfg->remove_stale_groups) {
1501 			p2p->cfg->remove_stale_groups(p2p->cfg->cb_ctx,
1502 						      dev->info.p2p_device_addr,
1503 						      NULL, NULL, 0);
1504 		}
1505 
1506 		if (msg.session_info && msg.session_info_len) {
1507 			size_t info_len = msg.session_info_len;
1508 			char *deferred_sess_resp = os_malloc(2 * info_len + 1);
1509 
1510 			if (!deferred_sess_resp) {
1511 				p2p_parse_free(&msg);
1512 				p2ps_prov_free(p2p);
1513 				goto out;
1514 			}
1515 			utf8_escape((char *) msg.session_info, info_len,
1516 				    deferred_sess_resp, 2 * info_len + 1);
1517 
1518 			if (p2p->cfg->prov_disc_fail)
1519 				p2p->cfg->prov_disc_fail(
1520 					p2p->cfg->cb_ctx, sa,
1521 					P2P_PROV_DISC_INFO_UNAVAILABLE,
1522 					adv_id, adv_mac,
1523 					deferred_sess_resp);
1524 			os_free(deferred_sess_resp);
1525 		} else
1526 			if (p2p->cfg->prov_disc_fail)
1527 				p2p->cfg->prov_disc_fail(
1528 					p2p->cfg->cb_ctx, sa,
1529 					P2P_PROV_DISC_INFO_UNAVAILABLE,
1530 					adv_id, adv_mac, NULL);
1531 	} else if (status != P2P_SC_SUCCESS) {
1532 		p2p_dbg(p2p, "Peer rejected our Provision Discovery Request");
1533 		if (p2p->cfg->prov_disc_fail)
1534 			p2p->cfg->prov_disc_fail(p2p->cfg->cb_ctx, sa,
1535 						 P2P_PROV_DISC_REJECTED,
1536 						 adv_id, adv_mac, NULL);
1537 		p2p_parse_free(&msg);
1538 		p2ps_prov_free(p2p);
1539 		goto out;
1540 	}
1541 
1542 	/* Store the provisioning info */
1543 	dev->wps_prov_info = msg.wps_config_methods;
1544 	if (msg.intended_addr)
1545 		os_memcpy(dev->interface_addr, msg.intended_addr, ETH_ALEN);
1546 
1547 	p2p_parse_free(&msg);
1548 
1549 out:
1550 	dev->req_config_methods = 0;
1551 	p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
1552 	if (dev->flags & P2P_DEV_PD_BEFORE_GO_NEG) {
1553 		p2p_dbg(p2p, "Start GO Neg after the PD-before-GO-Neg workaround with "
1554 			MACSTR, MAC2STR(dev->info.p2p_device_addr));
1555 		dev->flags &= ~P2P_DEV_PD_BEFORE_GO_NEG;
1556 		p2p_connect_send(p2p, dev);
1557 		return;
1558 	}
1559 
1560 	/*
1561 	 * prov_disc_resp callback is used to generate P2P-PROV-DISC-ENTER-PIN,
1562 	 * P2P-PROV-DISC-SHOW-PIN, and P2P-PROV-DISC-PBC-REQ events.
1563 	 * Call it only for a legacy P2P PD or for P2PS PD scenarios where
1564 	 * show/enter PIN events are needed.
1565 	 *
1566 	 * The callback is called in the following cases:
1567 	 * 1. Legacy P2P PD response with a status SUCCESS
1568 	 * 2. P2PS, advertiser method: DISPLAY, autoaccept: true,
1569 	 *    response status: SUCCESS, local method KEYPAD
1570 	 * 3. P2PS, advertiser method: KEYPAD,Seeker side,
1571 	 *    response status: INFO_CURRENTLY_UNAVAILABLE,
1572 	 *    local method: DISPLAY
1573 	 */
1574 	if (p2p->cfg->prov_disc_resp &&
1575 	    ((status == P2P_SC_SUCCESS && !adv_id) ||
1576 	     (p2ps_seeker && status == P2P_SC_SUCCESS &&
1577 	      passwd_id == DEV_PW_REGISTRAR_SPECIFIED) ||
1578 	     (p2ps_seeker &&
1579 	      status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE &&
1580 	      passwd_id == DEV_PW_USER_SPECIFIED)))
1581 		p2p->cfg->prov_disc_resp(p2p->cfg->cb_ctx, sa,
1582 					 report_config_methods);
1583 
1584 	if (p2p->state == P2P_PD_DURING_FIND) {
1585 		p2p_stop_listen_for_freq(p2p, 0);
1586 		p2p_continue_find(p2p);
1587 	}
1588 }
1589 
1590 
1591 int p2p_send_prov_disc_req(struct p2p_data *p2p, struct p2p_device *dev,
1592 			   int join, int force_freq)
1593 {
1594 	struct wpabuf *req;
1595 	int freq;
1596 
1597 	if (force_freq > 0)
1598 		freq = force_freq;
1599 	else
1600 		freq = dev->listen_freq > 0 ? dev->listen_freq :
1601 			dev->oper_freq;
1602 	if (freq <= 0) {
1603 		p2p_dbg(p2p, "No Listen/Operating frequency known for the peer "
1604 			MACSTR " to send Provision Discovery Request",
1605 			MAC2STR(dev->info.p2p_device_addr));
1606 		return -1;
1607 	}
1608 
1609 	if (dev->flags & P2P_DEV_GROUP_CLIENT_ONLY) {
1610 		if (!(dev->info.dev_capab &
1611 		      P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY)) {
1612 			p2p_dbg(p2p, "Cannot use PD with P2P Device " MACSTR
1613 				" that is in a group and is not discoverable",
1614 				MAC2STR(dev->info.p2p_device_addr));
1615 			return -1;
1616 		}
1617 		/* TODO: use device discoverability request through GO */
1618 	}
1619 
1620 	if (p2p->p2ps_prov) {
1621 		if (p2p->p2ps_prov->status == P2P_SC_SUCCESS_DEFERRED) {
1622 			if (p2p->p2ps_prov->method == WPS_CONFIG_DISPLAY)
1623 				dev->req_config_methods = WPS_CONFIG_KEYPAD;
1624 			else if (p2p->p2ps_prov->method == WPS_CONFIG_KEYPAD)
1625 				dev->req_config_methods = WPS_CONFIG_DISPLAY;
1626 			else
1627 				dev->req_config_methods = WPS_CONFIG_P2PS;
1628 		} else {
1629 			/* Order of preference, based on peer's capabilities */
1630 			if (p2p->p2ps_prov->method)
1631 				dev->req_config_methods =
1632 					p2p->p2ps_prov->method;
1633 			else if (dev->info.config_methods & WPS_CONFIG_P2PS)
1634 				dev->req_config_methods = WPS_CONFIG_P2PS;
1635 			else if (dev->info.config_methods & WPS_CONFIG_DISPLAY)
1636 				dev->req_config_methods = WPS_CONFIG_DISPLAY;
1637 			else
1638 				dev->req_config_methods = WPS_CONFIG_KEYPAD;
1639 		}
1640 		p2p_dbg(p2p,
1641 			"Building PD Request based on P2PS config method 0x%x status %d --> req_config_methods 0x%x",
1642 			p2p->p2ps_prov->method, p2p->p2ps_prov->status,
1643 			dev->req_config_methods);
1644 
1645 		if (p2p_prepare_channel(p2p, dev, p2p->p2ps_prov->force_freq,
1646 					p2p->p2ps_prov->pref_freq, 1) < 0)
1647 			return -1;
1648 	}
1649 
1650 	req = p2p_build_prov_disc_req(p2p, dev, join);
1651 	if (req == NULL)
1652 		return -1;
1653 
1654 	if (p2p->state != P2P_IDLE)
1655 		p2p_stop_listen_for_freq(p2p, freq);
1656 	p2p->pending_action_state = P2P_PENDING_PD;
1657 	if (p2p_send_action(p2p, freq, dev->info.p2p_device_addr,
1658 			    p2p->cfg->dev_addr, dev->info.p2p_device_addr,
1659 			    wpabuf_head(req), wpabuf_len(req), 200) < 0) {
1660 		p2p_dbg(p2p, "Failed to send Action frame");
1661 		wpabuf_free(req);
1662 		return -1;
1663 	}
1664 
1665 	os_memcpy(p2p->pending_pd_devaddr, dev->info.p2p_device_addr, ETH_ALEN);
1666 
1667 	wpabuf_free(req);
1668 	return 0;
1669 }
1670 
1671 
1672 int p2p_prov_disc_req(struct p2p_data *p2p, const u8 *peer_addr,
1673 		      struct p2ps_provision *p2ps_prov,
1674 		      u16 config_methods, int join, int force_freq,
1675 		      int user_initiated_pd)
1676 {
1677 	struct p2p_device *dev;
1678 
1679 	dev = p2p_get_device(p2p, peer_addr);
1680 	if (dev == NULL)
1681 		dev = p2p_get_device_interface(p2p, peer_addr);
1682 	if (dev == NULL || (dev->flags & P2P_DEV_PROBE_REQ_ONLY)) {
1683 		p2p_dbg(p2p, "Provision Discovery Request destination " MACSTR
1684 			" not yet known", MAC2STR(peer_addr));
1685 		os_free(p2ps_prov);
1686 		return -1;
1687 	}
1688 
1689 	p2p_dbg(p2p, "Provision Discovery Request with " MACSTR
1690 		" (config methods 0x%x)",
1691 		MAC2STR(peer_addr), config_methods);
1692 	if (config_methods == 0 && !p2ps_prov) {
1693 		os_free(p2ps_prov);
1694 		return -1;
1695 	}
1696 
1697 	if (p2ps_prov && p2ps_prov->status == P2P_SC_SUCCESS_DEFERRED &&
1698 	    p2p->p2ps_prov) {
1699 		/* Use cached method from deferred provisioning */
1700 		p2ps_prov->method = p2p->p2ps_prov->method;
1701 	}
1702 
1703 	/* Reset provisioning info */
1704 	dev->wps_prov_info = 0;
1705 	p2ps_prov_free(p2p);
1706 	p2p->p2ps_prov = p2ps_prov;
1707 
1708 	dev->req_config_methods = config_methods;
1709 	if (join)
1710 		dev->flags |= P2P_DEV_PD_FOR_JOIN;
1711 	else
1712 		dev->flags &= ~P2P_DEV_PD_FOR_JOIN;
1713 
1714 	if (p2p->state != P2P_IDLE && p2p->state != P2P_SEARCH &&
1715 	    p2p->state != P2P_LISTEN_ONLY) {
1716 		p2p_dbg(p2p, "Busy with other operations; postpone Provision Discovery Request with "
1717 			MACSTR " (config methods 0x%x)",
1718 			MAC2STR(peer_addr), config_methods);
1719 		return 0;
1720 	}
1721 
1722 	p2p->user_initiated_pd = user_initiated_pd;
1723 	p2p->pd_force_freq = force_freq;
1724 
1725 	if (p2p->user_initiated_pd)
1726 		p2p->pd_retries = MAX_PROV_DISC_REQ_RETRIES;
1727 
1728 	/*
1729 	 * Assign dialog token here to use the same value in each retry within
1730 	 * the same PD exchange.
1731 	 */
1732 	dev->dialog_token++;
1733 	if (dev->dialog_token == 0)
1734 		dev->dialog_token = 1;
1735 
1736 	return p2p_send_prov_disc_req(p2p, dev, join, force_freq);
1737 }
1738 
1739 
1740 void p2p_reset_pending_pd(struct p2p_data *p2p)
1741 {
1742 	struct p2p_device *dev;
1743 
1744 	dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
1745 		if (os_memcmp(p2p->pending_pd_devaddr,
1746 			      dev->info.p2p_device_addr, ETH_ALEN))
1747 			continue;
1748 		if (!dev->req_config_methods)
1749 			continue;
1750 		if (dev->flags & P2P_DEV_PD_FOR_JOIN)
1751 			continue;
1752 		/* Reset the config methods of the device */
1753 		dev->req_config_methods = 0;
1754 	}
1755 
1756 	p2p->user_initiated_pd = 0;
1757 	os_memset(p2p->pending_pd_devaddr, 0, ETH_ALEN);
1758 	p2p->pd_retries = 0;
1759 	p2p->pd_force_freq = 0;
1760 }
1761 
1762 
1763 void p2ps_prov_free(struct p2p_data *p2p)
1764 {
1765 	os_free(p2p->p2ps_prov);
1766 	p2p->p2ps_prov = NULL;
1767 }
1768