p2p_pd.c revision 444d567b27731d8572ef37697dd12fd1c37c2f24
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 "wps/wps_defs.h"
14#include "p2p_i.h"
15#include "p2p.h"
16
17
18/*
19 * Number of retries to attempt for provision discovery requests
20 * in case the peer is not listening.
21 */
22#define MAX_PROV_DISC_REQ_RETRIES 120
23
24
25static 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;
49	size_t extra = 0;
50
51#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);
57	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);
63	p2p_buf_add_capability(buf, p2p->dev_capab &
64			       ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY, 0);
65	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
75#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
80	return buf;
81}
82
83
84static struct wpabuf * p2p_build_prov_disc_resp(struct p2p_data *p2p,
85						u8 dialog_token,
86						u16 config_methods,
87						const u8 *group_id,
88						size_t group_id_len)
89{
90	struct wpabuf *buf;
91	size_t extra = 0;
92
93#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);
115	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
123#ifdef CONFIG_WIFI_DISPLAY
124	if (wfd_ie)
125		wpabuf_put_buf(buf, wfd_ie);
126#endif /* CONFIG_WIFI_DISPLAY */
127
128	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
144	wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
145		"P2P: Received Provision Discovery Request from " MACSTR
146		" with config methods 0x%x (freq=%d)",
147		MAC2STR(sa), msg.wps_config_methods, rx_freq);
148
149	dev = p2p_get_device(p2p, sa);
150	if (dev == NULL || (dev->flags & P2P_DEV_PROBE_REQ_ONLY)) {
151		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
152			"P2P: Provision Discovery Request from "
153			"unknown peer " MACSTR, MAC2STR(sa));
154
155		if (p2p_add_device(p2p, sa, rx_freq, NULL, 0, data + 1, len - 1,
156				   0)) {
157			wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
158			        "P2P: Provision Discovery Request add device "
159				"failed " MACSTR, MAC2STR(sa));
160		}
161	} else if (msg.wfd_subelems) {
162		wpabuf_free(dev->info.wfd_subelems);
163		dev->info.wfd_subelems = wpabuf_dup(msg.wfd_subelems);
164	}
165
166	if (!(msg.wps_config_methods &
167	      (WPS_CONFIG_DISPLAY | WPS_CONFIG_KEYPAD |
168	       WPS_CONFIG_PUSHBUTTON))) {
169		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Unsupported "
170			"Config Methods in Provision Discovery Request");
171		goto out;
172	}
173
174	if (msg.group_id) {
175		size_t i;
176		for (i = 0; i < p2p->num_groups; i++) {
177			if (p2p_group_is_group_id_match(p2p->groups[i],
178							msg.group_id,
179							msg.group_id_len))
180				break;
181		}
182		if (i == p2p->num_groups) {
183			wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: PD "
184				"request for unknown P2P Group ID - reject");
185			goto out;
186		}
187	}
188
189	if (dev)
190		dev->flags &= ~(P2P_DEV_PD_PEER_DISPLAY |
191				P2P_DEV_PD_PEER_KEYPAD);
192	if (msg.wps_config_methods & WPS_CONFIG_DISPLAY) {
193		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Peer " MACSTR
194			" requested us to show a PIN on display", MAC2STR(sa));
195		if (dev)
196			dev->flags |= P2P_DEV_PD_PEER_KEYPAD;
197	} else if (msg.wps_config_methods & WPS_CONFIG_KEYPAD) {
198		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Peer " MACSTR
199			" requested us to write its PIN using keypad",
200			MAC2STR(sa));
201		if (dev)
202			dev->flags |= P2P_DEV_PD_PEER_DISPLAY;
203	}
204
205	reject = 0;
206
207out:
208	resp = p2p_build_prov_disc_resp(p2p, msg.dialog_token,
209					reject ? 0 : msg.wps_config_methods,
210					msg.group_id, msg.group_id_len);
211	if (resp == NULL) {
212		p2p_parse_free(&msg);
213		return;
214	}
215	wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
216		"P2P: Sending Provision Discovery Response");
217	if (rx_freq > 0)
218		freq = rx_freq;
219	else
220		freq = p2p_channel_to_freq(p2p->cfg->country,
221					   p2p->cfg->reg_class,
222					   p2p->cfg->channel);
223	if (freq < 0) {
224		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
225			"P2P: Unknown regulatory class/channel");
226		wpabuf_free(resp);
227		p2p_parse_free(&msg);
228		return;
229	}
230	p2p->pending_action_state = P2P_NO_PENDING_ACTION;
231	if (p2p_send_action(p2p, freq, sa, p2p->cfg->dev_addr,
232			    p2p->cfg->dev_addr,
233			    wpabuf_head(resp), wpabuf_len(resp), 200) < 0) {
234		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
235			"P2P: Failed to send Action frame");
236	}
237
238	wpabuf_free(resp);
239
240	if (!reject && p2p->cfg->prov_disc_req) {
241		const u8 *dev_addr = sa;
242		if (msg.p2p_device_addr)
243			dev_addr = msg.p2p_device_addr;
244		p2p->cfg->prov_disc_req(p2p->cfg->cb_ctx, sa,
245					msg.wps_config_methods,
246					dev_addr, msg.pri_dev_type,
247					msg.device_name, msg.config_methods,
248					msg.capability ? msg.capability[0] : 0,
249					msg.capability ? msg.capability[1] :
250					0,
251					msg.group_id, msg.group_id_len);
252	}
253	p2p_parse_free(&msg);
254}
255
256
257void p2p_process_prov_disc_resp(struct p2p_data *p2p, const u8 *sa,
258				const u8 *data, size_t len)
259{
260	struct p2p_message msg;
261	struct p2p_device *dev;
262	u16 report_config_methods = 0, req_config_methods;
263	int success = 0;
264
265	if (p2p_parse(data, len, &msg))
266		return;
267
268	wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
269		"P2P: Received Provision Discovery Response from " MACSTR
270		" with config methods 0x%x",
271		MAC2STR(sa), msg.wps_config_methods);
272
273	dev = p2p_get_device(p2p, sa);
274	if (dev == NULL || !dev->req_config_methods) {
275		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
276			"P2P: Ignore Provision Discovery Response from "
277			MACSTR " with no pending request", MAC2STR(sa));
278		p2p_parse_free(&msg);
279		return;
280	}
281
282	if (dev->dialog_token != msg.dialog_token) {
283		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
284			"P2P: Ignore Provision Discovery Response with "
285			"unexpected Dialog Token %u (expected %u)",
286			msg.dialog_token, dev->dialog_token);
287		p2p_parse_free(&msg);
288		return;
289	}
290
291	if (p2p->pending_action_state == P2P_PENDING_PD) {
292		os_memset(p2p->pending_pd_devaddr, 0, ETH_ALEN);
293		p2p->pending_action_state = P2P_NO_PENDING_ACTION;
294	}
295
296	/*
297	 * Use a local copy of the requested config methods since
298	 * p2p_reset_pending_pd() can clear this in the peer entry.
299	 */
300	req_config_methods = dev->req_config_methods;
301
302	/*
303	 * If the response is from the peer to whom a user initiated request
304	 * was sent earlier, we reset that state info here.
305	 */
306	if (p2p->user_initiated_pd &&
307	    os_memcmp(p2p->pending_pd_devaddr, sa, ETH_ALEN) == 0)
308		p2p_reset_pending_pd(p2p);
309
310	if (msg.wps_config_methods != req_config_methods) {
311		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Peer rejected "
312			"our Provision Discovery Request (received "
313			"config_methods 0x%x expected 0x%x",
314			msg.wps_config_methods, req_config_methods);
315		if (p2p->cfg->prov_disc_fail)
316			p2p->cfg->prov_disc_fail(p2p->cfg->cb_ctx, sa,
317						 P2P_PROV_DISC_REJECTED);
318		p2p_parse_free(&msg);
319		goto out;
320	}
321
322	report_config_methods = req_config_methods;
323	dev->flags &= ~(P2P_DEV_PD_PEER_DISPLAY |
324			P2P_DEV_PD_PEER_KEYPAD);
325	if (req_config_methods & WPS_CONFIG_DISPLAY) {
326		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Peer " MACSTR
327			" accepted to show a PIN on display", MAC2STR(sa));
328		dev->flags |= P2P_DEV_PD_PEER_DISPLAY;
329	} else if (msg.wps_config_methods & WPS_CONFIG_KEYPAD) {
330		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Peer " MACSTR
331			" accepted to write our PIN using keypad",
332			MAC2STR(sa));
333		dev->flags |= P2P_DEV_PD_PEER_KEYPAD;
334	}
335
336	/* Store the provisioning info */
337	dev->wps_prov_info = msg.wps_config_methods;
338
339	p2p_parse_free(&msg);
340	success = 1;
341
342out:
343	dev->req_config_methods = 0;
344	p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
345	if (dev->flags & P2P_DEV_PD_BEFORE_GO_NEG) {
346		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
347			"P2P: Start GO Neg after the PD-before-GO-Neg "
348			"workaround with " MACSTR,
349			MAC2STR(dev->info.p2p_device_addr));
350		dev->flags &= ~P2P_DEV_PD_BEFORE_GO_NEG;
351		p2p_connect_send(p2p, dev);
352		return;
353	}
354	if (success && p2p->cfg->prov_disc_resp)
355		p2p->cfg->prov_disc_resp(p2p->cfg->cb_ctx, sa,
356					 report_config_methods);
357
358	if (p2p->state == P2P_PD_DURING_FIND) {
359		p2p_clear_timeout(p2p);
360		p2p_continue_find(p2p);
361	}
362}
363
364
365int p2p_send_prov_disc_req(struct p2p_data *p2p, struct p2p_device *dev,
366			   int join, int force_freq)
367{
368	struct wpabuf *req;
369	int freq;
370
371	if (force_freq > 0)
372		freq = force_freq;
373	else
374		freq = dev->listen_freq > 0 ? dev->listen_freq :
375			dev->oper_freq;
376	if (freq <= 0) {
377		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
378			"P2P: No Listen/Operating frequency known for the "
379			"peer " MACSTR " to send Provision Discovery Request",
380			MAC2STR(dev->info.p2p_device_addr));
381		return -1;
382	}
383
384	if (dev->flags & P2P_DEV_GROUP_CLIENT_ONLY) {
385		if (!(dev->info.dev_capab &
386		      P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY)) {
387			wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
388				"P2P: Cannot use PD with P2P Device " MACSTR
389				" that is in a group and is not discoverable",
390				MAC2STR(dev->info.p2p_device_addr));
391			return -1;
392		}
393		/* TODO: use device discoverability request through GO */
394	}
395
396	req = p2p_build_prov_disc_req(p2p, dev->dialog_token,
397				      dev->req_config_methods,
398				      join ? dev : NULL);
399	if (req == NULL)
400		return -1;
401
402	if (p2p->state != P2P_IDLE)
403		p2p_stop_listen_for_freq(p2p, freq);
404	p2p->pending_action_state = P2P_PENDING_PD;
405	if (p2p_send_action(p2p, freq, dev->info.p2p_device_addr,
406			    p2p->cfg->dev_addr, dev->info.p2p_device_addr,
407			    wpabuf_head(req), wpabuf_len(req), 200) < 0) {
408		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
409			"P2P: Failed to send Action frame");
410		wpabuf_free(req);
411		return -1;
412	}
413
414	os_memcpy(p2p->pending_pd_devaddr, dev->info.p2p_device_addr, ETH_ALEN);
415
416	wpabuf_free(req);
417	return 0;
418}
419
420
421int p2p_prov_disc_req(struct p2p_data *p2p, const u8 *peer_addr,
422		      u16 config_methods, int join, int force_freq,
423		      int user_initiated_pd)
424{
425	struct p2p_device *dev;
426
427	dev = p2p_get_device(p2p, peer_addr);
428	if (dev == NULL)
429		dev = p2p_get_device_interface(p2p, peer_addr);
430	if (dev == NULL || (dev->flags & P2P_DEV_PROBE_REQ_ONLY)) {
431		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Provision "
432			"Discovery Request destination " MACSTR
433			" not yet known", MAC2STR(peer_addr));
434		return -1;
435	}
436
437	wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Provision Discovery "
438		"Request with " MACSTR " (config methods 0x%x)",
439		MAC2STR(peer_addr), config_methods);
440	if (config_methods == 0)
441		return -1;
442
443	/* Reset provisioning info */
444	dev->wps_prov_info = 0;
445
446	dev->req_config_methods = config_methods;
447	if (join)
448		dev->flags |= P2P_DEV_PD_FOR_JOIN;
449	else
450		dev->flags &= ~P2P_DEV_PD_FOR_JOIN;
451
452	if (p2p->state != P2P_IDLE && p2p->state != P2P_SEARCH &&
453	    p2p->state != P2P_LISTEN_ONLY) {
454		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Busy with other "
455			"operations; postpone Provision Discovery Request "
456			"with " MACSTR " (config methods 0x%x)",
457			MAC2STR(peer_addr), config_methods);
458		return 0;
459	}
460
461	p2p->user_initiated_pd = user_initiated_pd;
462
463	if (p2p->user_initiated_pd)
464		p2p->pd_retries = MAX_PROV_DISC_REQ_RETRIES;
465
466	/*
467	 * Assign dialog token here to use the same value in each retry within
468	 * the same PD exchange.
469	 */
470	dev->dialog_token++;
471	if (dev->dialog_token == 0)
472		dev->dialog_token = 1;
473
474	return p2p_send_prov_disc_req(p2p, dev, join, force_freq);
475}
476
477
478void p2p_reset_pending_pd(struct p2p_data *p2p)
479{
480	struct p2p_device *dev;
481
482	dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
483		if (os_memcmp(p2p->pending_pd_devaddr,
484			      dev->info.p2p_device_addr, ETH_ALEN))
485			continue;
486		if (!dev->req_config_methods)
487			continue;
488		if (dev->flags & P2P_DEV_PD_FOR_JOIN)
489			continue;
490		/* Reset the config methods of the device */
491		dev->req_config_methods = 0;
492	}
493
494	p2p->user_initiated_pd = 0;
495	os_memset(p2p->pending_pd_devaddr, 0, ETH_ALEN);
496	p2p->pd_retries = 0;
497}
498