1/*
2 * Wi-Fi Direct - P2P Device Discoverability procedure
3 * Copyright (c) 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 "p2p_i.h"
14#include "p2p.h"
15
16
17static struct wpabuf * p2p_build_dev_disc_req(struct p2p_data *p2p,
18					      struct p2p_device *go,
19					      const u8 *dev_id)
20{
21	struct wpabuf *buf;
22	u8 *len;
23
24	buf = wpabuf_alloc(100);
25	if (buf == NULL)
26		return NULL;
27
28	go->dialog_token++;
29	if (go->dialog_token == 0)
30		go->dialog_token = 1;
31	p2p_buf_add_public_action_hdr(buf, P2P_DEV_DISC_REQ, go->dialog_token);
32
33	len = p2p_buf_add_ie_hdr(buf);
34	p2p_buf_add_device_id(buf, dev_id);
35	p2p_buf_add_group_id(buf, go->info.p2p_device_addr, go->oper_ssid,
36			     go->oper_ssid_len);
37	p2p_buf_update_ie_hdr(buf, len);
38
39	return buf;
40}
41
42
43void p2p_dev_disc_req_cb(struct p2p_data *p2p, int success)
44{
45	p2p_dbg(p2p, "Device Discoverability Request TX callback: success=%d",
46		success);
47
48	if (!success) {
49		/*
50		 * Use P2P find, if needed, to find the other device or to
51		 * retry device discoverability.
52		 */
53		p2p_set_state(p2p, P2P_CONNECT);
54		p2p_set_timeout(p2p, 0, 100000);
55		return;
56	}
57
58	p2p_dbg(p2p, "GO acknowledged Device Discoverability Request - wait for response");
59	/*
60	 * TODO: is the remain-on-channel from Action frame TX long enough for
61	 * most cases or should we try to increase its duration and/or start
62	 * another remain-on-channel if needed once the previous one expires?
63	 */
64}
65
66
67int p2p_send_dev_disc_req(struct p2p_data *p2p, struct p2p_device *dev)
68{
69	struct p2p_device *go;
70	struct wpabuf *req;
71	unsigned int wait_time;
72
73	go = p2p_get_device(p2p, dev->member_in_go_dev);
74	if (go == NULL || dev->oper_freq <= 0) {
75		p2p_dbg(p2p, "Could not find peer entry for GO and frequency to send Device Discoverability Request");
76		return -1;
77	}
78
79	req = p2p_build_dev_disc_req(p2p, go, dev->info.p2p_device_addr);
80	if (req == NULL)
81		return -1;
82
83	p2p_dbg(p2p, "Sending Device Discoverability Request to GO " MACSTR
84		" for client " MACSTR,
85		MAC2STR(go->info.p2p_device_addr),
86		MAC2STR(dev->info.p2p_device_addr));
87
88	p2p->pending_client_disc_go = go;
89	os_memcpy(p2p->pending_client_disc_addr, dev->info.p2p_device_addr,
90		  ETH_ALEN);
91	p2p->pending_action_state = P2P_PENDING_DEV_DISC_REQUEST;
92	wait_time = 1000;
93	if (p2p->cfg->max_listen && wait_time > p2p->cfg->max_listen)
94		wait_time = p2p->cfg->max_listen;
95	if (p2p_send_action(p2p, dev->oper_freq, go->info.p2p_device_addr,
96			    p2p->cfg->dev_addr, go->info.p2p_device_addr,
97			    wpabuf_head(req), wpabuf_len(req), wait_time) < 0) {
98		p2p_dbg(p2p, "Failed to send Action frame");
99		wpabuf_free(req);
100		/* TODO: how to recover from failure? */
101		return -1;
102	}
103
104	wpabuf_free(req);
105
106	return 0;
107}
108
109
110static struct wpabuf * p2p_build_dev_disc_resp(u8 dialog_token, u8 status)
111{
112	struct wpabuf *buf;
113	u8 *len;
114
115	buf = wpabuf_alloc(100);
116	if (buf == NULL)
117		return NULL;
118
119	p2p_buf_add_public_action_hdr(buf, P2P_DEV_DISC_RESP, dialog_token);
120
121	len = p2p_buf_add_ie_hdr(buf);
122	p2p_buf_add_status(buf, status);
123	p2p_buf_update_ie_hdr(buf, len);
124
125	return buf;
126}
127
128
129void p2p_dev_disc_resp_cb(struct p2p_data *p2p, int success)
130{
131	p2p_dbg(p2p, "Device Discoverability Response TX callback: success=%d",
132		success);
133	p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
134}
135
136
137static void p2p_send_dev_disc_resp(struct p2p_data *p2p, u8 dialog_token,
138				   const u8 *addr, int freq, u8 status)
139{
140	struct wpabuf *resp;
141
142	resp = p2p_build_dev_disc_resp(dialog_token, status);
143	if (resp == NULL)
144		return;
145
146	p2p_dbg(p2p, "Sending Device Discoverability Response to " MACSTR
147		" (status %u freq %d)",
148		MAC2STR(addr), status, freq);
149
150	p2p->pending_action_state = P2P_PENDING_DEV_DISC_RESPONSE;
151	if (p2p_send_action(p2p, freq, addr, p2p->cfg->dev_addr,
152			    p2p->cfg->dev_addr,
153			    wpabuf_head(resp), wpabuf_len(resp), 200) < 0) {
154		p2p_dbg(p2p, "Failed to send Action frame");
155	}
156
157	wpabuf_free(resp);
158}
159
160
161void p2p_process_dev_disc_req(struct p2p_data *p2p, const u8 *sa,
162			      const u8 *data, size_t len, int rx_freq)
163{
164	struct p2p_message msg;
165	size_t g;
166
167	p2p_dbg(p2p, "Received Device Discoverability Request from " MACSTR
168		" (freq=%d)", MAC2STR(sa), rx_freq);
169
170	if (p2p_parse(data, len, &msg))
171		return;
172
173	if (msg.dialog_token == 0) {
174		p2p_dbg(p2p, "Invalid Dialog Token 0 (must be nonzero) in Device Discoverability Request");
175		p2p_send_dev_disc_resp(p2p, msg.dialog_token, sa, rx_freq,
176				       P2P_SC_FAIL_INVALID_PARAMS);
177		p2p_parse_free(&msg);
178		return;
179	}
180
181	if (msg.device_id == NULL) {
182		p2p_dbg(p2p, "P2P Device ID attribute missing from Device Discoverability Request");
183		p2p_send_dev_disc_resp(p2p, msg.dialog_token, sa, rx_freq,
184				       P2P_SC_FAIL_INVALID_PARAMS);
185		p2p_parse_free(&msg);
186		return;
187	}
188
189	for (g = 0; g < p2p->num_groups; g++) {
190		if (p2p_group_go_discover(p2p->groups[g], msg.device_id, sa,
191					  rx_freq) == 0) {
192			p2p_dbg(p2p, "Scheduled GO Discoverability Request for the target device");
193			/*
194			 * P2P group code will use a callback to indicate TX
195			 * status, so that we can reply to the request once the
196			 * target client has acknowledged the request or it has
197			 * timed out.
198			 */
199			p2p->pending_dev_disc_dialog_token = msg.dialog_token;
200			os_memcpy(p2p->pending_dev_disc_addr, sa, ETH_ALEN);
201			p2p->pending_dev_disc_freq = rx_freq;
202			p2p_parse_free(&msg);
203			return;
204		}
205	}
206
207	p2p_dbg(p2p, "Requested client was not found in any group or did not support client discoverability");
208	p2p_send_dev_disc_resp(p2p, msg.dialog_token, sa, rx_freq,
209			       P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE);
210	p2p_parse_free(&msg);
211}
212
213
214void p2p_process_dev_disc_resp(struct p2p_data *p2p, const u8 *sa,
215			       const u8 *data, size_t len)
216{
217	struct p2p_message msg;
218	struct p2p_device *go;
219	u8 status;
220
221	p2p_dbg(p2p, "Received Device Discoverability Response from " MACSTR,
222		MAC2STR(sa));
223
224	go = p2p->pending_client_disc_go;
225	if (go == NULL ||
226	    os_memcmp(sa, go->info.p2p_device_addr, ETH_ALEN) != 0) {
227		p2p_dbg(p2p, "Ignore unexpected Device Discoverability Response");
228		return;
229	}
230
231	if (p2p_parse(data, len, &msg))
232		return;
233
234	if (msg.status == NULL) {
235		p2p_parse_free(&msg);
236		return;
237	}
238
239	if (msg.dialog_token != go->dialog_token) {
240		p2p_dbg(p2p, "Ignore Device Discoverability Response with unexpected dialog token %u (expected %u)",
241			msg.dialog_token, go->dialog_token);
242		p2p_parse_free(&msg);
243		return;
244	}
245
246	status = *msg.status;
247	p2p_parse_free(&msg);
248
249	p2p_dbg(p2p, "Device Discoverability Response status %u", status);
250
251	if (p2p->go_neg_peer == NULL ||
252	    os_memcmp(p2p->pending_client_disc_addr,
253		      p2p->go_neg_peer->info.p2p_device_addr, ETH_ALEN) != 0 ||
254	    os_memcmp(p2p->go_neg_peer->member_in_go_dev,
255		      go->info.p2p_device_addr, ETH_ALEN) != 0) {
256		p2p_dbg(p2p, "No pending operation with the client discoverability peer anymore");
257		return;
258	}
259
260	if (status == 0) {
261		/*
262		 * Peer is expected to be awake for at least 100 TU; try to
263		 * connect immediately.
264		 */
265		p2p_dbg(p2p, "Client discoverability request succeeded");
266		if (p2p->state == P2P_CONNECT) {
267			/*
268			 * Change state to force the timeout to start in
269			 * P2P_CONNECT again without going through the short
270			 * Listen state.
271			 */
272			p2p_set_state(p2p, P2P_CONNECT_LISTEN);
273			p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
274		}
275		p2p_set_timeout(p2p, 0, 0);
276	} else {
277		/*
278		 * Client discoverability request failed; try to connect from
279		 * timeout.
280		 */
281		p2p_dbg(p2p, "Client discoverability request failed");
282		p2p_set_timeout(p2p, 0, 500000);
283	}
284
285}
286
287
288void p2p_go_disc_req_cb(struct p2p_data *p2p, int success)
289{
290	p2p_dbg(p2p, "GO Discoverability Request TX callback: success=%d",
291		success);
292	p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
293
294	if (p2p->pending_dev_disc_dialog_token == 0) {
295		p2p_dbg(p2p, "No pending Device Discoverability Request");
296		return;
297	}
298
299	p2p_send_dev_disc_resp(p2p, p2p->pending_dev_disc_dialog_token,
300			       p2p->pending_dev_disc_addr,
301			       p2p->pending_dev_disc_freq,
302			       success ? P2P_SC_SUCCESS :
303			       P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE);
304
305	p2p->pending_dev_disc_dialog_token = 0;
306}
307
308
309void p2p_process_go_disc_req(struct p2p_data *p2p, const u8 *da, const u8 *sa,
310			     const u8 *data, size_t len, int rx_freq)
311{
312	unsigned int tu;
313	struct wpabuf *ies;
314
315	p2p_dbg(p2p, "Received GO Discoverability Request - remain awake for 100 TU");
316
317	ies = p2p_build_probe_resp_ies(p2p);
318	if (ies == NULL)
319		return;
320
321	/* Remain awake 100 TU on operating channel */
322	p2p->pending_client_disc_freq = rx_freq;
323	tu = 100;
324	if (p2p->cfg->start_listen(p2p->cfg->cb_ctx, rx_freq, 1024 * tu / 1000,
325		    ies) < 0) {
326		p2p_dbg(p2p, "Failed to start listen mode for client discoverability");
327	}
328	wpabuf_free(ies);
329}
330