p2p_pd.c revision c5ec7f57ead87efa365800228aa0b09a12d9e6c4
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 during IDLE
20 * state in case the peer is not listening.
21 */
22#define MAX_PROV_DISC_REQ_RETRIES 10
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
50	buf = wpabuf_alloc(1000);
51	if (buf == NULL)
52		return NULL;
53
54	p2p_buf_add_public_action_hdr(buf, P2P_PROV_DISC_REQ, dialog_token);
55
56	len = p2p_buf_add_ie_hdr(buf);
57	p2p_buf_add_capability(buf, p2p->dev_capab, 0);
58	p2p_buf_add_device_info(buf, p2p, NULL);
59	if (go) {
60		p2p_buf_add_group_id(buf, go->info.p2p_device_addr,
61				     go->oper_ssid, go->oper_ssid_len);
62	}
63	p2p_buf_update_ie_hdr(buf, len);
64
65	/* WPS IE with Config Methods attribute */
66	p2p_build_wps_ie_config_methods(buf, config_methods);
67
68	return buf;
69}
70
71
72static struct wpabuf * p2p_build_prov_disc_resp(struct p2p_data *p2p,
73						u8 dialog_token,
74						u16 config_methods)
75{
76	struct wpabuf *buf;
77
78	buf = wpabuf_alloc(100);
79	if (buf == NULL)
80		return NULL;
81
82	p2p_buf_add_public_action_hdr(buf, P2P_PROV_DISC_RESP, dialog_token);
83
84	/* WPS IE with Config Methods attribute */
85	p2p_build_wps_ie_config_methods(buf, config_methods);
86
87	return buf;
88}
89
90
91void p2p_process_prov_disc_req(struct p2p_data *p2p, const u8 *sa,
92			       const u8 *data, size_t len, int rx_freq)
93{
94	struct p2p_message msg;
95	struct p2p_device *dev;
96	int freq;
97	int reject = 1;
98	struct wpabuf *resp;
99
100	if (p2p_parse(data, len, &msg))
101		return;
102
103	wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
104		"P2P: Received Provision Discovery Request from " MACSTR
105		" with config methods 0x%x (freq=%d)",
106		MAC2STR(sa), msg.wps_config_methods, rx_freq);
107
108	dev = p2p_get_device(p2p, sa);
109	if (dev == NULL || (dev->flags & P2P_DEV_PROBE_REQ_ONLY)) {
110		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
111			"P2P: Provision Discovery Request from "
112			"unknown peer " MACSTR, MAC2STR(sa));
113		if (p2p_add_device(p2p, sa, rx_freq, 0, data + 1, len - 1)) {
114			wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
115			        "P2P: Provision Discovery Request add device "
116				"failed " MACSTR, MAC2STR(sa));
117		}
118	}
119
120	if (!(msg.wps_config_methods &
121	      (WPS_CONFIG_DISPLAY | WPS_CONFIG_KEYPAD |
122	       WPS_CONFIG_PUSHBUTTON))) {
123		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Unsupported "
124			"Config Methods in Provision Discovery Request");
125		goto out;
126	}
127
128	if (dev)
129		dev->flags &= ~(P2P_DEV_PD_PEER_DISPLAY |
130				P2P_DEV_PD_PEER_KEYPAD);
131	if (msg.wps_config_methods & WPS_CONFIG_DISPLAY) {
132		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Peer " MACSTR
133			" requested us to show a PIN on display", MAC2STR(sa));
134		if (dev)
135			dev->flags |= P2P_DEV_PD_PEER_KEYPAD;
136	} else if (msg.wps_config_methods & WPS_CONFIG_KEYPAD) {
137		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Peer " MACSTR
138			" requested us to write its PIN using keypad",
139			MAC2STR(sa));
140		if (dev)
141			dev->flags |= P2P_DEV_PD_PEER_DISPLAY;
142	}
143
144	reject = 0;
145
146out:
147	resp = p2p_build_prov_disc_resp(p2p, msg.dialog_token,
148					reject ? 0 : msg.wps_config_methods);
149	if (resp == NULL) {
150		p2p_parse_free(&msg);
151		return;
152	}
153	wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
154		"P2P: Sending Provision Discovery Response");
155	if (rx_freq > 0)
156		freq = rx_freq;
157	else
158		freq = p2p_channel_to_freq(p2p->cfg->country,
159					   p2p->cfg->reg_class,
160					   p2p->cfg->channel);
161	if (freq < 0) {
162		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
163			"P2P: Unknown regulatory class/channel");
164		wpabuf_free(resp);
165		p2p_parse_free(&msg);
166		return;
167	}
168	p2p->pending_action_state = P2P_NO_PENDING_ACTION;
169	if (p2p_send_action(p2p, freq, sa, p2p->cfg->dev_addr,
170			    p2p->cfg->dev_addr,
171			    wpabuf_head(resp), wpabuf_len(resp), 200) < 0) {
172		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
173			"P2P: Failed to send Action frame");
174	}
175
176	wpabuf_free(resp);
177
178	if (!reject && p2p->cfg->prov_disc_req) {
179		const u8 *dev_addr = sa;
180		if (msg.p2p_device_addr)
181			dev_addr = msg.p2p_device_addr;
182		p2p->cfg->prov_disc_req(p2p->cfg->cb_ctx, sa,
183					msg.wps_config_methods,
184					dev_addr, msg.pri_dev_type,
185					msg.device_name, msg.config_methods,
186					msg.capability ? msg.capability[0] : 0,
187					msg.capability ? msg.capability[1] :
188					0,
189					msg.group_id, msg.group_id_len);
190	}
191	p2p_parse_free(&msg);
192}
193
194
195void p2p_process_prov_disc_resp(struct p2p_data *p2p, const u8 *sa,
196				const u8 *data, size_t len)
197{
198	struct p2p_message msg;
199	struct p2p_device *dev;
200	u16 report_config_methods = 0;
201
202	if (p2p_parse(data, len, &msg))
203		return;
204
205	wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
206		"P2P: Received Provision Discovery Response from " MACSTR
207		" with config methods 0x%x",
208		MAC2STR(sa), msg.wps_config_methods);
209
210	dev = p2p_get_device(p2p, sa);
211	if (dev == NULL || !dev->req_config_methods) {
212		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
213			"P2P: Ignore Provision Discovery Response from "
214			MACSTR " with no pending request", MAC2STR(sa));
215		p2p_parse_free(&msg);
216		return;
217	}
218
219	if (p2p->pending_action_state == P2P_PENDING_PD) {
220		os_memset(p2p->pending_pd_devaddr, 0, ETH_ALEN);
221		p2p->pending_action_state = P2P_NO_PENDING_ACTION;
222	}
223
224	if (dev->dialog_token != msg.dialog_token) {
225		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
226			"P2P: Ignore Provision Discovery Response with "
227			"unexpected Dialog Token %u (expected %u)",
228			msg.dialog_token, dev->dialog_token);
229		p2p_parse_free(&msg);
230		return;
231	}
232
233	/*
234	 * If the response is from the peer to whom a user initiated request
235	 * was sent earlier, we reset that state info here.
236	 */
237	if (p2p->user_initiated_pd &&
238	    os_memcmp(p2p->pending_pd_devaddr, sa, ETH_ALEN) == 0)
239		p2p_reset_pending_pd(p2p);
240
241	if (msg.wps_config_methods != dev->req_config_methods) {
242		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Peer rejected "
243			"our Provision Discovery Request");
244		if (p2p->cfg->prov_disc_fail)
245			p2p->cfg->prov_disc_fail(p2p->cfg->cb_ctx, sa,
246						 P2P_PROV_DISC_REJECTED);
247		p2p_parse_free(&msg);
248		goto out;
249	}
250
251	report_config_methods = dev->req_config_methods;
252	dev->flags &= ~(P2P_DEV_PD_PEER_DISPLAY |
253			P2P_DEV_PD_PEER_KEYPAD);
254	if (dev->req_config_methods & WPS_CONFIG_DISPLAY) {
255		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Peer " MACSTR
256			" accepted to show a PIN on display", MAC2STR(sa));
257		dev->flags |= P2P_DEV_PD_PEER_DISPLAY;
258	} else if (msg.wps_config_methods & WPS_CONFIG_KEYPAD) {
259		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Peer " MACSTR
260			" accepted to write our PIN using keypad",
261			MAC2STR(sa));
262		dev->flags |= P2P_DEV_PD_PEER_KEYPAD;
263	}
264
265	/* Store the provisioning info */
266	dev->wps_prov_info = msg.wps_config_methods;
267
268	p2p_parse_free(&msg);
269
270out:
271	dev->req_config_methods = 0;
272	p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
273	if (p2p->cfg->prov_disc_resp)
274		p2p->cfg->prov_disc_resp(p2p->cfg->cb_ctx, sa,
275					 report_config_methods);
276}
277
278
279int p2p_send_prov_disc_req(struct p2p_data *p2p, struct p2p_device *dev,
280			   int join, int force_freq)
281{
282	struct wpabuf *req;
283	int freq;
284
285	if (force_freq > 0)
286		freq = force_freq;
287	else
288		freq = dev->listen_freq > 0 ? dev->listen_freq :
289			dev->oper_freq;
290	if (freq <= 0) {
291		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
292			"P2P: No Listen/Operating frequency known for the "
293			"peer " MACSTR " to send Provision Discovery Request",
294			MAC2STR(dev->info.p2p_device_addr));
295		return -1;
296	}
297
298	if (dev->flags & P2P_DEV_GROUP_CLIENT_ONLY) {
299		if (!(dev->info.dev_capab &
300		      P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY)) {
301			wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
302				"P2P: Cannot use PD with P2P Device " MACSTR
303				" that is in a group and is not discoverable",
304				MAC2STR(dev->info.p2p_device_addr));
305			return -1;
306		}
307		/* TODO: use device discoverability request through GO */
308	}
309
310	dev->dialog_token++;
311	if (dev->dialog_token == 0)
312		dev->dialog_token = 1;
313	req = p2p_build_prov_disc_req(p2p, dev->dialog_token,
314				      dev->req_config_methods,
315				      join ? dev : NULL);
316	if (req == NULL)
317		return -1;
318
319	p2p->pending_action_state = P2P_PENDING_PD;
320	if (p2p_send_action(p2p, freq, dev->info.p2p_device_addr,
321			    p2p->cfg->dev_addr, dev->info.p2p_device_addr,
322			    wpabuf_head(req), wpabuf_len(req), 200) < 0) {
323		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
324			"P2P: Failed to send Action frame");
325		wpabuf_free(req);
326		return -1;
327	}
328
329	os_memcpy(p2p->pending_pd_devaddr, dev->info.p2p_device_addr, ETH_ALEN);
330
331	wpabuf_free(req);
332	return 0;
333}
334
335
336int p2p_prov_disc_req(struct p2p_data *p2p, const u8 *peer_addr,
337		      u16 config_methods, int join, int force_freq)
338{
339	struct p2p_device *dev;
340
341	dev = p2p_get_device(p2p, peer_addr);
342	if (dev == NULL)
343		dev = p2p_get_device_interface(p2p, peer_addr);
344	if (dev == NULL || (dev->flags & P2P_DEV_PROBE_REQ_ONLY)) {
345		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Provision "
346			"Discovery Request destination " MACSTR
347			" not yet known", MAC2STR(peer_addr));
348		return -1;
349	}
350
351	wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Provision Discovery "
352		"Request with " MACSTR " (config methods 0x%x)",
353		MAC2STR(peer_addr), config_methods);
354	if (config_methods == 0)
355		return -1;
356
357	/* Reset provisioning info */
358	dev->wps_prov_info = 0;
359
360	dev->req_config_methods = config_methods;
361	if (join)
362		dev->flags |= P2P_DEV_PD_FOR_JOIN;
363	else
364		dev->flags &= ~P2P_DEV_PD_FOR_JOIN;
365
366	if (p2p->state != P2P_IDLE && p2p->state != P2P_SEARCH &&
367	    p2p->state != P2P_LISTEN_ONLY) {
368		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Busy with other "
369			"operations; postpone Provision Discovery Request "
370			"with " MACSTR " (config methods 0x%x)",
371			MAC2STR(peer_addr), config_methods);
372		return 0;
373	}
374
375	/*
376	 * We use the join param as a cue to differentiate between user
377	 * initiated PD request and one issued during finds (internal).
378	 */
379	p2p->user_initiated_pd = !join;
380
381	/* Also set some retries to attempt in case of IDLE state */
382	if (p2p->user_initiated_pd && p2p->state == P2P_IDLE)
383		p2p->pd_retries = MAX_PROV_DISC_REQ_RETRIES;
384
385	return p2p_send_prov_disc_req(p2p, dev, join, force_freq);
386}
387
388
389void p2p_reset_pending_pd(struct p2p_data *p2p)
390{
391	struct p2p_device *dev;
392
393	dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
394		if (os_memcmp(p2p->pending_pd_devaddr,
395			      dev->info.p2p_device_addr, ETH_ALEN))
396			continue;
397		if (!dev->req_config_methods)
398			continue;
399		if (dev->flags & P2P_DEV_PD_FOR_JOIN)
400			continue;
401		/* Reset the config methods of the device */
402		dev->req_config_methods = 0;
403	}
404
405	p2p->user_initiated_pd = 0;
406	os_memset(p2p->pending_pd_devaddr, 0, ETH_ALEN);
407	p2p->pd_retries = 0;
408}
409