blob: 05822c656fa711dc547ccadd356da642ef58bb1e [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * Wi-Fi Direct - P2P Invitation procedure
3 * Copyright (c) 2010, Atheros Communications
4 *
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007 */
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_invitation_req(struct p2p_data *p2p,
18 struct p2p_device *peer,
19 const u8 *go_dev_addr)
20{
21 struct wpabuf *buf;
22 u8 *len;
23 const u8 *dev_addr;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070024 size_t extra = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070025
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070026#ifdef CONFIG_WIFI_DISPLAY
27 struct wpabuf *wfd_ie = p2p->wfd_ie_invitation;
28 if (wfd_ie && p2p->inv_role == P2P_INVITE_ROLE_ACTIVE_GO) {
29 size_t i;
30 for (i = 0; i < p2p->num_groups; i++) {
31 struct p2p_group *g = p2p->groups[i];
32 struct wpabuf *ie;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -070033 if (os_memcmp(p2p_group_get_interface_addr(g),
34 p2p->inv_bssid, ETH_ALEN) != 0)
35 continue;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070036 ie = p2p_group_get_wfd_ie(g);
37 if (ie) {
38 wfd_ie = ie;
39 break;
40 }
41 }
42 }
43 if (wfd_ie)
44 extra = wpabuf_len(wfd_ie);
45#endif /* CONFIG_WIFI_DISPLAY */
46
47 buf = wpabuf_alloc(1000 + extra);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070048 if (buf == NULL)
49 return NULL;
50
51 peer->dialog_token++;
52 if (peer->dialog_token == 0)
53 peer->dialog_token = 1;
54 p2p_buf_add_public_action_hdr(buf, P2P_INVITATION_REQ,
55 peer->dialog_token);
56
57 len = p2p_buf_add_ie_hdr(buf);
58 if (p2p->inv_role == P2P_INVITE_ROLE_ACTIVE_GO || !p2p->inv_persistent)
59 p2p_buf_add_config_timeout(buf, 0, 0);
60 else
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070061 p2p_buf_add_config_timeout(buf, p2p->go_timeout,
62 p2p->client_timeout);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070063 p2p_buf_add_invitation_flags(buf, p2p->inv_persistent ?
64 P2P_INVITATION_FLAGS_TYPE : 0);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -080065 if (p2p->inv_role != P2P_INVITE_ROLE_CLIENT ||
66 !(peer->flags & P2P_DEV_NO_PREF_CHAN))
67 p2p_buf_add_operating_channel(buf, p2p->cfg->country,
68 p2p->op_reg_class,
69 p2p->op_channel);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070070 if (p2p->inv_bssid_set)
71 p2p_buf_add_group_bssid(buf, p2p->inv_bssid);
Irfan Sheriff069fa2c2012-09-24 23:59:04 -070072 p2p_buf_add_channel_list(buf, p2p->cfg->country, &p2p->channels);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070073 if (go_dev_addr)
74 dev_addr = go_dev_addr;
75 else if (p2p->inv_role == P2P_INVITE_ROLE_CLIENT)
76 dev_addr = peer->info.p2p_device_addr;
77 else
78 dev_addr = p2p->cfg->dev_addr;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070079 p2p_buf_add_group_id(buf, dev_addr, p2p->inv_ssid, p2p->inv_ssid_len);
80 p2p_buf_add_device_info(buf, p2p, peer);
81 p2p_buf_update_ie_hdr(buf, len);
82
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070083#ifdef CONFIG_WIFI_DISPLAY
84 if (wfd_ie)
85 wpabuf_put_buf(buf, wfd_ie);
86#endif /* CONFIG_WIFI_DISPLAY */
87
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070088 return buf;
89}
90
91
92static struct wpabuf * p2p_build_invitation_resp(struct p2p_data *p2p,
93 struct p2p_device *peer,
94 u8 dialog_token, u8 status,
95 const u8 *group_bssid,
96 u8 reg_class, u8 channel,
97 struct p2p_channels *channels)
98{
99 struct wpabuf *buf;
100 u8 *len;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700101 size_t extra = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700102
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700103#ifdef CONFIG_WIFI_DISPLAY
104 struct wpabuf *wfd_ie = p2p->wfd_ie_invitation;
105 if (wfd_ie && group_bssid) {
106 size_t i;
107 for (i = 0; i < p2p->num_groups; i++) {
108 struct p2p_group *g = p2p->groups[i];
109 struct wpabuf *ie;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700110 if (os_memcmp(p2p_group_get_interface_addr(g),
111 group_bssid, ETH_ALEN) != 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700112 continue;
113 ie = p2p_group_get_wfd_ie(g);
114 if (ie) {
115 wfd_ie = ie;
116 break;
117 }
118 }
119 }
120 if (wfd_ie)
121 extra = wpabuf_len(wfd_ie);
122#endif /* CONFIG_WIFI_DISPLAY */
123
124 buf = wpabuf_alloc(1000 + extra);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700125 if (buf == NULL)
126 return NULL;
127
128 p2p_buf_add_public_action_hdr(buf, P2P_INVITATION_RESP,
129 dialog_token);
130
131 len = p2p_buf_add_ie_hdr(buf);
132 p2p_buf_add_status(buf, status);
133 p2p_buf_add_config_timeout(buf, 0, 0); /* FIX */
134 if (reg_class && channel)
135 p2p_buf_add_operating_channel(buf, p2p->cfg->country,
136 reg_class, channel);
137 if (group_bssid)
138 p2p_buf_add_group_bssid(buf, group_bssid);
Irfan Sheriff069fa2c2012-09-24 23:59:04 -0700139 if (channels)
140 p2p_buf_add_channel_list(buf, p2p->cfg->country, channels);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700141 p2p_buf_update_ie_hdr(buf, len);
142
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700143#ifdef CONFIG_WIFI_DISPLAY
144 if (wfd_ie)
145 wpabuf_put_buf(buf, wfd_ie);
146#endif /* CONFIG_WIFI_DISPLAY */
147
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700148 return buf;
149}
150
151
152void p2p_process_invitation_req(struct p2p_data *p2p, const u8 *sa,
153 const u8 *data, size_t len, int rx_freq)
154{
155 struct p2p_device *dev;
156 struct p2p_message msg;
157 struct wpabuf *resp = NULL;
158 u8 status = P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
159 int freq;
160 int go = 0;
161 u8 group_bssid[ETH_ALEN], *bssid;
162 int op_freq = 0;
163 u8 reg_class = 0, channel = 0;
164 struct p2p_channels intersection, *channels = NULL;
165 int persistent;
166
167 os_memset(group_bssid, 0, sizeof(group_bssid));
168
169 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
170 "P2P: Received Invitation Request from " MACSTR " (freq=%d)",
171 MAC2STR(sa), rx_freq);
172
173 if (p2p_parse(data, len, &msg))
174 return;
175
176 dev = p2p_get_device(p2p, sa);
177 if (dev == NULL || (dev->flags & P2P_DEV_PROBE_REQ_ONLY)) {
178 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
179 "P2P: Invitation Request from unknown peer "
180 MACSTR, MAC2STR(sa));
181
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800182 if (p2p_add_device(p2p, sa, rx_freq, NULL, 0, data + 1, len - 1,
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800183 0)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700184 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
185 "P2P: Invitation Request add device failed "
186 MACSTR, MAC2STR(sa));
187 status = P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
188 goto fail;
189 }
190
191 dev = p2p_get_device(p2p, sa);
192 if (dev == NULL) {
193 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
194 "P2P: Reject Invitation Request from unknown "
195 "peer " MACSTR, MAC2STR(sa));
196 status = P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
197 goto fail;
198 }
199 }
200
201 if (!msg.group_id || !msg.channel_list) {
202 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
203 "P2P: Mandatory attribute missing in Invitation "
204 "Request from " MACSTR, MAC2STR(sa));
205 status = P2P_SC_FAIL_INVALID_PARAMS;
206 goto fail;
207 }
208
209 if (msg.invitation_flags)
210 persistent = *msg.invitation_flags & P2P_INVITATION_FLAGS_TYPE;
211 else {
212 /* Invitation Flags is a mandatory attribute starting from P2P
213 * spec 1.06. As a backwards compatibility mechanism, assume
214 * the request was for a persistent group if the attribute is
215 * missing.
216 */
217 wpa_printf(MSG_DEBUG, "P2P: Mandatory Invitation Flags "
218 "attribute missing from Invitation Request");
219 persistent = 1;
220 }
221
222 if (p2p_peer_channels_check(p2p, &p2p->cfg->channels, dev,
223 msg.channel_list, msg.channel_list_len) <
224 0) {
225 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
226 "P2P: No common channels found");
227 status = P2P_SC_FAIL_NO_COMMON_CHANNELS;
228 goto fail;
229 }
230
231 if (p2p->cfg->invitation_process) {
232 status = p2p->cfg->invitation_process(
233 p2p->cfg->cb_ctx, sa, msg.group_bssid, msg.group_id,
234 msg.group_id + ETH_ALEN, msg.group_id_len - ETH_ALEN,
235 &go, group_bssid, &op_freq, persistent);
236 }
237
238 if (op_freq) {
Dmitry Shmidt91c40cd2012-09-25 14:23:53 -0700239 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Invitation "
240 "processing forced frequency %d MHz", op_freq);
Dmitry Shmidt4b060592013-04-29 16:42:49 -0700241 if (p2p_freq_to_channel(op_freq, &reg_class, &channel) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700242 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
243 "P2P: Unknown forced freq %d MHz from "
244 "invitation_process()", op_freq);
245 status = P2P_SC_FAIL_NO_COMMON_CHANNELS;
246 goto fail;
247 }
248
249 p2p_channels_intersect(&p2p->cfg->channels, &dev->channels,
250 &intersection);
251 if (!p2p_channels_includes(&intersection, reg_class, channel))
252 {
253 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
254 "P2P: forced freq %d MHz not in the supported "
255 "channels interaction", op_freq);
256 status = P2P_SC_FAIL_NO_COMMON_CHANNELS;
257 goto fail;
258 }
259
260 if (status == P2P_SC_SUCCESS)
261 channels = &intersection;
262 } else {
Dmitry Shmidt91c40cd2012-09-25 14:23:53 -0700263 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
264 "P2P: No forced channel from invitation processing - "
265 "figure out best one to use");
266
267 p2p_channels_intersect(&p2p->cfg->channels, &dev->channels,
268 &intersection);
269 /* Default to own configuration as a starting point */
270 p2p->op_reg_class = p2p->cfg->op_reg_class;
271 p2p->op_channel = p2p->cfg->op_channel;
272 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Own default "
273 "op_class %d channel %d",
274 p2p->op_reg_class, p2p->op_channel);
275
276 /* Use peer preference if specified and compatible */
277 if (msg.operating_channel) {
278 int req_freq;
279 req_freq = p2p_channel_to_freq(
Dmitry Shmidt91c40cd2012-09-25 14:23:53 -0700280 msg.operating_channel[3],
281 msg.operating_channel[4]);
282 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Peer "
283 "operating channel preference: %d MHz",
284 req_freq);
285 if (req_freq > 0 &&
286 p2p_channels_includes(&intersection,
287 msg.operating_channel[3],
288 msg.operating_channel[4])) {
289 p2p->op_reg_class = msg.operating_channel[3];
290 p2p->op_channel = msg.operating_channel[4];
291 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
292 "P2P: Use peer preference op_class %d "
293 "channel %d",
294 p2p->op_reg_class, p2p->op_channel);
295 } else {
296 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
297 "P2P: Cannot use peer channel "
298 "preference");
299 }
300 }
301
302 if (!p2p_channels_includes(&intersection, p2p->op_reg_class,
303 p2p->op_channel)) {
304 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
305 "P2P: Initially selected channel (op_class %d "
306 "channel %d) not in channel intersection - try "
307 "to reselect",
308 p2p->op_reg_class, p2p->op_channel);
309 p2p_reselect_channel(p2p, &intersection);
310 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
311 "P2P: Re-selection result: op_class %d "
312 "channel %d",
313 p2p->op_reg_class, p2p->op_channel);
314 if (!p2p_channels_includes(&intersection,
315 p2p->op_reg_class,
316 p2p->op_channel)) {
317 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
318 "P2P: Peer does not support selected "
319 "operating channel (reg_class=%u "
320 "channel=%u)",
321 p2p->op_reg_class, p2p->op_channel);
322 status = P2P_SC_FAIL_NO_COMMON_CHANNELS;
323 goto fail;
324 }
325 }
326
Dmitry Shmidt4b060592013-04-29 16:42:49 -0700327 op_freq = p2p_channel_to_freq(p2p->op_reg_class,
Dmitry Shmidt91c40cd2012-09-25 14:23:53 -0700328 p2p->op_channel);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700329 if (op_freq < 0) {
330 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
331 "P2P: Unknown operational channel "
332 "(country=%c%c reg_class=%u channel=%u)",
333 p2p->cfg->country[0], p2p->cfg->country[1],
Dmitry Shmidt91c40cd2012-09-25 14:23:53 -0700334 p2p->op_reg_class, p2p->op_channel);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700335 status = P2P_SC_FAIL_NO_COMMON_CHANNELS;
336 goto fail;
337 }
Dmitry Shmidt91c40cd2012-09-25 14:23:53 -0700338 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Selected operating "
339 "channel - %d MHz", op_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700340
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700341 if (status == P2P_SC_SUCCESS) {
Dmitry Shmidt91c40cd2012-09-25 14:23:53 -0700342 reg_class = p2p->op_reg_class;
343 channel = p2p->op_channel;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700344 channels = &intersection;
345 }
346 }
347
348fail:
349 if (go && status == P2P_SC_SUCCESS && !is_zero_ether_addr(group_bssid))
350 bssid = group_bssid;
351 else
352 bssid = NULL;
353 resp = p2p_build_invitation_resp(p2p, dev, msg.dialog_token, status,
354 bssid, reg_class, channel, channels);
355
356 if (resp == NULL)
357 goto out;
358
359 if (rx_freq > 0)
360 freq = rx_freq;
361 else
Dmitry Shmidt4b060592013-04-29 16:42:49 -0700362 freq = p2p_channel_to_freq(p2p->cfg->reg_class,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700363 p2p->cfg->channel);
364 if (freq < 0) {
365 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
366 "P2P: Unknown regulatory class/channel");
367 goto out;
368 }
369
370 /*
371 * Store copy of invitation data to be used when processing TX status
372 * callback for the Acton frame.
373 */
374 os_memcpy(p2p->inv_sa, sa, ETH_ALEN);
375 if (msg.group_bssid) {
376 os_memcpy(p2p->inv_group_bssid, msg.group_bssid, ETH_ALEN);
377 p2p->inv_group_bssid_ptr = p2p->inv_group_bssid;
378 } else
379 p2p->inv_group_bssid_ptr = NULL;
380 if (msg.group_id_len - ETH_ALEN <= 32) {
381 os_memcpy(p2p->inv_ssid, msg.group_id + ETH_ALEN,
382 msg.group_id_len - ETH_ALEN);
383 p2p->inv_ssid_len = msg.group_id_len - ETH_ALEN;
384 }
385 os_memcpy(p2p->inv_go_dev_addr, msg.group_id, ETH_ALEN);
386 p2p->inv_status = status;
387 p2p->inv_op_freq = op_freq;
388
389 p2p->pending_action_state = P2P_PENDING_INVITATION_RESPONSE;
390 if (p2p_send_action(p2p, freq, sa, p2p->cfg->dev_addr,
391 p2p->cfg->dev_addr,
392 wpabuf_head(resp), wpabuf_len(resp), 200) < 0) {
393 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
394 "P2P: Failed to send Action frame");
395 }
396
397out:
398 wpabuf_free(resp);
399 p2p_parse_free(&msg);
400}
401
402
403void p2p_process_invitation_resp(struct p2p_data *p2p, const u8 *sa,
404 const u8 *data, size_t len)
405{
406 struct p2p_device *dev;
407 struct p2p_message msg;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -0800408 struct p2p_channels intersection, *channels = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700409
410 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
411 "P2P: Received Invitation Response from " MACSTR,
412 MAC2STR(sa));
413
414 dev = p2p_get_device(p2p, sa);
415 if (dev == NULL) {
416 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
417 "P2P: Ignore Invitation Response from unknown peer "
418 MACSTR, MAC2STR(sa));
419 return;
420 }
421
422 if (dev != p2p->invite_peer) {
423 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
424 "P2P: Ignore unexpected Invitation Response from peer "
425 MACSTR, MAC2STR(sa));
426 return;
427 }
428
429 if (p2p_parse(data, len, &msg))
430 return;
431
432 if (!msg.status) {
433 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
434 "P2P: Mandatory Status attribute missing in "
435 "Invitation Response from " MACSTR, MAC2STR(sa));
436 p2p_parse_free(&msg);
437 return;
438 }
439
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -0800440 if (!msg.channel_list) {
441 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
442 "P2P: Mandatory Channel List attribute missing in "
443 "Invitation Response from " MACSTR, MAC2STR(sa));
444#ifdef CONFIG_P2P_STRICT
445 p2p_parse_free(&msg);
446 return;
447#endif /* CONFIG_P2P_STRICT */
448 /* Try to survive without peer channel list */
449 channels = &p2p->channels;
450 } else if (p2p_peer_channels_check(p2p, &p2p->channels, dev,
451 msg.channel_list,
452 msg.channel_list_len) < 0) {
453 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
454 "P2P: No common channels found");
455 p2p_parse_free(&msg);
456 return;
457 } else {
458 p2p_channels_intersect(&p2p->channels, &dev->channels,
459 &intersection);
460 channels = &intersection;
461 }
462
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700463 if (p2p->cfg->invitation_result)
464 p2p->cfg->invitation_result(p2p->cfg->cb_ctx, *msg.status,
Dmitry Shmidt700a1372013-03-15 14:14:44 -0700465 msg.group_bssid, channels, sa);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700466
467 p2p_parse_free(&msg);
468
469 p2p_clear_timeout(p2p);
470 p2p_set_state(p2p, P2P_IDLE);
471 p2p->invite_peer = NULL;
472}
473
474
475int p2p_invite_send(struct p2p_data *p2p, struct p2p_device *dev,
476 const u8 *go_dev_addr)
477{
478 struct wpabuf *req;
479 int freq;
480
481 freq = dev->listen_freq > 0 ? dev->listen_freq : dev->oper_freq;
482 if (freq <= 0) {
483 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
484 "P2P: No Listen/Operating frequency known for the "
485 "peer " MACSTR " to send Invitation Request",
486 MAC2STR(dev->info.p2p_device_addr));
487 return -1;
488 }
489
490 req = p2p_build_invitation_req(p2p, dev, go_dev_addr);
491 if (req == NULL)
492 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700493 if (p2p->state != P2P_IDLE)
494 p2p_stop_listen_for_freq(p2p, freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700495 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
496 "P2P: Sending Invitation Request");
497 p2p_set_state(p2p, P2P_INVITE);
498 p2p->pending_action_state = P2P_PENDING_INVITATION_REQUEST;
499 p2p->invite_peer = dev;
500 dev->invitation_reqs++;
501 if (p2p_send_action(p2p, freq, dev->info.p2p_device_addr,
502 p2p->cfg->dev_addr, dev->info.p2p_device_addr,
503 wpabuf_head(req), wpabuf_len(req), 200) < 0) {
504 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
505 "P2P: Failed to send Action frame");
506 /* Use P2P find to recover and retry */
507 p2p_set_timeout(p2p, 0, 0);
508 }
509
510 wpabuf_free(req);
511
512 return 0;
513}
514
515
516void p2p_invitation_req_cb(struct p2p_data *p2p, int success)
517{
518 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
519 "P2P: Invitation Request TX callback: success=%d", success);
520
521 if (p2p->invite_peer == NULL) {
522 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
523 "P2P: No pending Invite");
524 return;
525 }
526
527 /*
528 * Use P2P find, if needed, to find the other device from its listen
529 * channel.
530 */
531 p2p_set_state(p2p, P2P_INVITE);
Irfan Sheriff1a2ce112012-10-30 22:22:52 -0700532#ifdef ANDROID_P2P
533 p2p_set_timeout(p2p, 0, 350000);
534#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700535 p2p_set_timeout(p2p, 0, 100000);
Irfan Sheriff1a2ce112012-10-30 22:22:52 -0700536#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700537}
538
539
540void p2p_invitation_resp_cb(struct p2p_data *p2p, int success)
541{
542 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
543 "P2P: Invitation Response TX callback: success=%d", success);
544 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
545
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -0700546 if (!success)
547 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
548 "P2P: Assume Invitation Response was actually "
549 "received by the peer even though Ack was not "
550 "reported");
551
552 if (p2p->cfg->invitation_received) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700553 p2p->cfg->invitation_received(p2p->cfg->cb_ctx,
554 p2p->inv_sa,
555 p2p->inv_group_bssid_ptr,
556 p2p->inv_ssid, p2p->inv_ssid_len,
557 p2p->inv_go_dev_addr,
558 p2p->inv_status,
559 p2p->inv_op_freq);
560 }
561}
562
563
564int p2p_invite(struct p2p_data *p2p, const u8 *peer, enum p2p_invite_role role,
565 const u8 *bssid, const u8 *ssid, size_t ssid_len,
566 unsigned int force_freq, const u8 *go_dev_addr,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -0800567 int persistent_group, unsigned int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700568{
569 struct p2p_device *dev;
570
571 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
572 "P2P: Request to invite peer " MACSTR " role=%d persistent=%d "
573 "force_freq=%u",
574 MAC2STR(peer), role, persistent_group, force_freq);
575 if (bssid)
576 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
577 "P2P: Invitation for BSSID " MACSTR, MAC2STR(bssid));
578 if (go_dev_addr) {
579 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
580 "P2P: Invitation for GO Device Address " MACSTR,
581 MAC2STR(go_dev_addr));
582 os_memcpy(p2p->invite_go_dev_addr_buf, go_dev_addr, ETH_ALEN);
583 p2p->invite_go_dev_addr = p2p->invite_go_dev_addr_buf;
584 } else
585 p2p->invite_go_dev_addr = NULL;
586 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Invitation for SSID",
587 ssid, ssid_len);
588
589 dev = p2p_get_device(p2p, peer);
590 if (dev == NULL || (dev->listen_freq <= 0 && dev->oper_freq <= 0)) {
591 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
592 "P2P: Cannot invite unknown P2P Device " MACSTR,
593 MAC2STR(peer));
594 return -1;
595 }
596
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -0800597 if (p2p_prepare_channel(p2p, dev, force_freq, pref_freq) < 0)
598 return -1;
599
600 if (persistent_group && role == P2P_INVITE_ROLE_CLIENT && !force_freq &&
601 !pref_freq)
602 dev->flags |= P2P_DEV_NO_PREF_CHAN;
603 else
604 dev->flags &= ~P2P_DEV_NO_PREF_CHAN;
605
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700606 if (dev->flags & P2P_DEV_GROUP_CLIENT_ONLY) {
607 if (!(dev->info.dev_capab &
608 P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY)) {
609 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
610 "P2P: Cannot invite a P2P Device " MACSTR
611 " that is in a group and is not discoverable",
612 MAC2STR(peer));
613 }
614 /* TODO: use device discoverability request through GO */
615 }
616
617 dev->invitation_reqs = 0;
618
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700619 if (p2p->state != P2P_IDLE)
620 p2p_stop_find(p2p);
621
622 p2p->inv_role = role;
623 p2p->inv_bssid_set = bssid != NULL;
624 if (bssid)
625 os_memcpy(p2p->inv_bssid, bssid, ETH_ALEN);
626 os_memcpy(p2p->inv_ssid, ssid, ssid_len);
627 p2p->inv_ssid_len = ssid_len;
628 p2p->inv_persistent = persistent_group;
629 return p2p_invite_send(p2p, dev, go_dev_addr);
630}