2 * Wi-Fi Direct - P2P Device Discoverability procedure
3 * Copyright (c) 2010, Atheros Communications
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
9 * Alternatively, this software may be distributed under the terms of BSD
12 * See README and COPYING for more details.
18 #include "common/ieee802_11_defs.h"
23 static struct wpabuf * p2p_build_dev_disc_req(struct p2p_data *p2p,
24 struct p2p_device *go,
30 buf = wpabuf_alloc(100);
35 if (go->dialog_token == 0)
37 p2p_buf_add_public_action_hdr(buf, P2P_DEV_DISC_REQ, go->dialog_token);
39 len = p2p_buf_add_ie_hdr(buf);
40 p2p_buf_add_device_id(buf, dev_id);
41 p2p_buf_add_group_id(buf, go->p2p_device_addr, go->oper_ssid,
43 p2p_buf_update_ie_hdr(buf, len);
49 void p2p_dev_disc_req_cb(struct p2p_data *p2p, int success)
51 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
52 "P2P: Device Discoverability Request TX callback: success=%d",
57 * Use P2P find, if needed, to find the other device or to
58 * retry device discoverability.
60 p2p_set_state(p2p, P2P_CONNECT);
61 p2p_set_timeout(p2p, 0, 100000);
65 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
66 "P2P: GO acknowledged Device Discoverability Request - wait "
69 * TODO: is the remain-on-channel from Action frame TX long enough for
70 * most cases or should we try to increase its duration and/or start
71 * another remain-on-channel if needed once the previous one expires?
76 int p2p_send_dev_disc_req(struct p2p_data *p2p, struct p2p_device *dev)
78 struct p2p_device *go;
81 go = p2p_get_device(p2p, dev->member_in_go_dev);
82 if (go == NULL || dev->oper_freq <= 0) {
83 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
84 "P2P: Could not find peer entry for GO and frequency "
85 "to send Device Discoverability Request");
89 req = p2p_build_dev_disc_req(p2p, go, dev->p2p_device_addr);
93 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
94 "P2P: Sending Device Discoverability Request to GO " MACSTR
95 " for client " MACSTR,
96 MAC2STR(go->p2p_device_addr), MAC2STR(dev->p2p_device_addr));
98 p2p->pending_client_disc_go = go;
99 os_memcpy(p2p->pending_client_disc_addr, dev->p2p_device_addr,
101 p2p->pending_action_state = P2P_PENDING_DEV_DISC_REQUEST;
102 if (p2p->cfg->send_action(p2p->cfg->cb_ctx, dev->oper_freq,
103 go->p2p_device_addr, p2p->cfg->dev_addr,
105 wpabuf_head(req), wpabuf_len(req), 1000) < 0)
107 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
108 "P2P: Failed to send Action frame");
110 /* TODO: how to recover from failure? */
120 static struct wpabuf * p2p_build_dev_disc_resp(u8 dialog_token, u8 status)
125 buf = wpabuf_alloc(100);
129 p2p_buf_add_public_action_hdr(buf, P2P_DEV_DISC_RESP, dialog_token);
131 len = p2p_buf_add_ie_hdr(buf);
132 p2p_buf_add_status(buf, status);
133 p2p_buf_update_ie_hdr(buf, len);
139 void p2p_dev_disc_resp_cb(struct p2p_data *p2p, int success)
141 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
142 "P2P: Device Discoverability Response TX callback: success=%d",
144 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
148 static void p2p_send_dev_disc_resp(struct p2p_data *p2p, u8 dialog_token,
149 const u8 *addr, int freq, u8 status)
153 resp = p2p_build_dev_disc_resp(dialog_token, status);
157 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
158 "P2P: Sending Device Discoverability Response to " MACSTR
159 " (status %u freq %d)",
160 MAC2STR(addr), status, freq);
162 p2p->pending_action_state = P2P_PENDING_DEV_DISC_RESPONSE;
163 if (p2p->cfg->send_action(p2p->cfg->cb_ctx, freq, addr,
164 p2p->cfg->dev_addr, p2p->cfg->dev_addr,
165 wpabuf_head(resp), wpabuf_len(resp), 200) <
167 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
168 "P2P: Failed to send Action frame");
175 void p2p_process_dev_disc_req(struct p2p_data *p2p, const u8 *sa,
176 const u8 *data, size_t len, int rx_freq)
178 struct p2p_message msg;
181 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
182 "P2P: Received Device Discoverability Request from " MACSTR
183 " (freq=%d)", MAC2STR(sa), rx_freq);
185 if (p2p_parse(data, len, &msg))
188 if (msg.dialog_token == 0) {
189 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
190 "P2P: Invalid Dialog Token 0 (must be nonzero) in "
191 "Device Discoverability Request");
192 p2p_send_dev_disc_resp(p2p, msg.dialog_token, sa, rx_freq,
193 P2P_SC_FAIL_INVALID_PARAMS);
194 p2p_parse_free(&msg);
198 if (msg.device_id == NULL) {
199 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
200 "P2P: P2P Device ID attribute missing from Device "
201 "Discoverability Request");
202 p2p_send_dev_disc_resp(p2p, msg.dialog_token, sa, rx_freq,
203 P2P_SC_FAIL_INVALID_PARAMS);
204 p2p_parse_free(&msg);
208 for (g = 0; g < p2p->num_groups; g++) {
209 if (p2p_group_go_discover(p2p->groups[g], msg.device_id, sa,
211 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Scheduled "
212 "GO Discoverability Request for the target "
215 * P2P group code will use a callback to indicate TX
216 * status, so that we can reply to the request once the
217 * target client has acknowledged the request or it has
220 p2p->pending_dev_disc_dialog_token = msg.dialog_token;
221 os_memcpy(p2p->pending_dev_disc_addr, sa, ETH_ALEN);
222 p2p->pending_dev_disc_freq = rx_freq;
223 p2p_parse_free(&msg);
228 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Requested client "
229 "was not found in any group or did not support client "
231 p2p_send_dev_disc_resp(p2p, msg.dialog_token, sa, rx_freq,
232 P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE);
233 p2p_parse_free(&msg);
237 void p2p_process_dev_disc_resp(struct p2p_data *p2p, const u8 *sa,
238 const u8 *data, size_t len)
240 struct p2p_message msg;
241 struct p2p_device *go;
244 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
245 "P2P: Received Device Discoverability Response from " MACSTR,
248 go = p2p->pending_client_disc_go;
249 if (go == NULL || os_memcmp(sa, go->p2p_device_addr, ETH_ALEN) != 0) {
250 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Ignore unexpected "
251 "Device Discoverability Response");
255 if (p2p_parse(data, len, &msg))
258 if (msg.status == NULL) {
259 p2p_parse_free(&msg);
263 if (msg.dialog_token != go->dialog_token) {
264 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Ignore Device "
265 "Discoverability Response with unexpected dialog "
266 "token %u (expected %u)",
267 msg.dialog_token, go->dialog_token);
268 p2p_parse_free(&msg);
272 status = *msg.status;
273 p2p_parse_free(&msg);
275 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
276 "P2P: Device Discoverability Response status %u", status);
278 if (p2p->go_neg_peer == NULL ||
279 os_memcmp(p2p->pending_client_disc_addr,
280 p2p->go_neg_peer->p2p_device_addr, ETH_ALEN) != 0 ||
281 os_memcmp(p2p->go_neg_peer->member_in_go_dev, go->p2p_device_addr,
283 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: No pending "
284 "operation with the client discoverability peer "
291 * Peer is expected to be awake for at least 100 TU; try to
292 * connect immediately.
294 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
295 "P2P: Client discoverability request succeeded");
296 if (p2p->state == P2P_CONNECT) {
298 * Change state to force the timeout to start in
299 * P2P_CONNECT again without going through the short
302 p2p_set_state(p2p, P2P_CONNECT_LISTEN);
303 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
305 p2p_set_timeout(p2p, 0, 0);
308 * Client discoverability request failed; try to connect from
311 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
312 "P2P: Client discoverability request failed");
313 p2p_set_timeout(p2p, 0, 500000);
319 void p2p_go_disc_req_cb(struct p2p_data *p2p, int success)
321 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
322 "P2P: GO Discoverability Request TX callback: success=%d",
324 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
326 if (p2p->pending_dev_disc_dialog_token == 0) {
327 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: No pending Device "
328 "Discoverability Request");
332 p2p_send_dev_disc_resp(p2p, p2p->pending_dev_disc_dialog_token,
333 p2p->pending_dev_disc_addr,
334 p2p->pending_dev_disc_freq,
335 success ? P2P_SC_SUCCESS :
336 P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE);
338 p2p->pending_dev_disc_dialog_token = 0;
342 void p2p_process_go_disc_req(struct p2p_data *p2p, const u8 *da, const u8 *sa,
343 const u8 *data, size_t len, int rx_freq)
348 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
349 "P2P: Received GO Discoverability Request - remain awake for "
352 ies = p2p_build_probe_resp_ies(p2p);
356 /* Remain awake 100 TU on operating channel */
357 p2p->pending_client_disc_freq = rx_freq;
359 if (p2p->cfg->start_listen(p2p->cfg->cb_ctx, rx_freq, 1024 * tu / 1000,
361 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
362 "P2P: Failed to start listen mode for client "