blob: df24c6454235dce5c8f877a554daefdb90693fb4 [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;
33 ie = p2p_group_get_wfd_ie(g);
34 if (ie) {
35 wfd_ie = ie;
36 break;
37 }
38 }
39 }
40 if (wfd_ie)
41 extra = wpabuf_len(wfd_ie);
42#endif /* CONFIG_WIFI_DISPLAY */
43
44 buf = wpabuf_alloc(1000 + extra);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070045 if (buf == NULL)
46 return NULL;
47
48 peer->dialog_token++;
49 if (peer->dialog_token == 0)
50 peer->dialog_token = 1;
51 p2p_buf_add_public_action_hdr(buf, P2P_INVITATION_REQ,
52 peer->dialog_token);
53
54 len = p2p_buf_add_ie_hdr(buf);
55 if (p2p->inv_role == P2P_INVITE_ROLE_ACTIVE_GO || !p2p->inv_persistent)
56 p2p_buf_add_config_timeout(buf, 0, 0);
57 else
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070058 p2p_buf_add_config_timeout(buf, p2p->go_timeout,
59 p2p->client_timeout);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070060 p2p_buf_add_invitation_flags(buf, p2p->inv_persistent ?
61 P2P_INVITATION_FLAGS_TYPE : 0);
62 p2p_buf_add_operating_channel(buf, p2p->cfg->country,
63 p2p->op_reg_class, p2p->op_channel);
64 if (p2p->inv_bssid_set)
65 p2p_buf_add_group_bssid(buf, p2p->inv_bssid);
66 p2p_buf_add_channel_list(buf, p2p->cfg->country, &p2p->channels);
67 if (go_dev_addr)
68 dev_addr = go_dev_addr;
69 else if (p2p->inv_role == P2P_INVITE_ROLE_CLIENT)
70 dev_addr = peer->info.p2p_device_addr;
71 else
72 dev_addr = p2p->cfg->dev_addr;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070073 p2p_buf_add_group_id(buf, dev_addr, p2p->inv_ssid, p2p->inv_ssid_len);
74 p2p_buf_add_device_info(buf, p2p, peer);
75 p2p_buf_update_ie_hdr(buf, len);
76
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070077#ifdef CONFIG_WIFI_DISPLAY
78 if (wfd_ie)
79 wpabuf_put_buf(buf, wfd_ie);
80#endif /* CONFIG_WIFI_DISPLAY */
81
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070082 return buf;
83}
84
85
86static struct wpabuf * p2p_build_invitation_resp(struct p2p_data *p2p,
87 struct p2p_device *peer,
88 u8 dialog_token, u8 status,
89 const u8 *group_bssid,
90 u8 reg_class, u8 channel,
91 struct p2p_channels *channels)
92{
93 struct wpabuf *buf;
94 u8 *len;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070095 size_t extra = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070096
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070097#ifdef CONFIG_WIFI_DISPLAY
98 struct wpabuf *wfd_ie = p2p->wfd_ie_invitation;
99 if (wfd_ie && group_bssid) {
100 size_t i;
101 for (i = 0; i < p2p->num_groups; i++) {
102 struct p2p_group *g = p2p->groups[i];
103 struct wpabuf *ie;
104 if (!p2p_group_is_group_id_match(g, group_bssid,
105 ETH_ALEN))
106 continue;
107 ie = p2p_group_get_wfd_ie(g);
108 if (ie) {
109 wfd_ie = ie;
110 break;
111 }
112 }
113 }
114 if (wfd_ie)
115 extra = wpabuf_len(wfd_ie);
116#endif /* CONFIG_WIFI_DISPLAY */
117
118 buf = wpabuf_alloc(1000 + extra);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700119 if (buf == NULL)
120 return NULL;
121
122 p2p_buf_add_public_action_hdr(buf, P2P_INVITATION_RESP,
123 dialog_token);
124
125 len = p2p_buf_add_ie_hdr(buf);
126 p2p_buf_add_status(buf, status);
127 p2p_buf_add_config_timeout(buf, 0, 0); /* FIX */
128 if (reg_class && channel)
129 p2p_buf_add_operating_channel(buf, p2p->cfg->country,
130 reg_class, channel);
131 if (group_bssid)
132 p2p_buf_add_group_bssid(buf, group_bssid);
133 if (channels)
134 p2p_buf_add_channel_list(buf, p2p->cfg->country, channels);
135 p2p_buf_update_ie_hdr(buf, len);
136
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700137#ifdef CONFIG_WIFI_DISPLAY
138 if (wfd_ie)
139 wpabuf_put_buf(buf, wfd_ie);
140#endif /* CONFIG_WIFI_DISPLAY */
141
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700142 return buf;
143}
144
145
146void p2p_process_invitation_req(struct p2p_data *p2p, const u8 *sa,
147 const u8 *data, size_t len, int rx_freq)
148{
149 struct p2p_device *dev;
150 struct p2p_message msg;
151 struct wpabuf *resp = NULL;
152 u8 status = P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
153 int freq;
154 int go = 0;
155 u8 group_bssid[ETH_ALEN], *bssid;
156 int op_freq = 0;
157 u8 reg_class = 0, channel = 0;
158 struct p2p_channels intersection, *channels = NULL;
159 int persistent;
160
161 os_memset(group_bssid, 0, sizeof(group_bssid));
162
163 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
164 "P2P: Received Invitation Request from " MACSTR " (freq=%d)",
165 MAC2STR(sa), rx_freq);
166
167 if (p2p_parse(data, len, &msg))
168 return;
169
170 dev = p2p_get_device(p2p, sa);
171 if (dev == NULL || (dev->flags & P2P_DEV_PROBE_REQ_ONLY)) {
172 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
173 "P2P: Invitation Request from unknown peer "
174 MACSTR, MAC2STR(sa));
175
Dmitry Shmidt04949592012-07-19 12:16:46 -0700176 if (p2p_add_device(p2p, sa, rx_freq, 0, data + 1, len - 1, 0))
177 {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700178 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
179 "P2P: Invitation Request add device failed "
180 MACSTR, MAC2STR(sa));
181 status = P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
182 goto fail;
183 }
184
185 dev = p2p_get_device(p2p, sa);
186 if (dev == NULL) {
187 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
188 "P2P: Reject Invitation Request from unknown "
189 "peer " MACSTR, MAC2STR(sa));
190 status = P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
191 goto fail;
192 }
193 }
194
195 if (!msg.group_id || !msg.channel_list) {
196 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
197 "P2P: Mandatory attribute missing in Invitation "
198 "Request from " MACSTR, MAC2STR(sa));
199 status = P2P_SC_FAIL_INVALID_PARAMS;
200 goto fail;
201 }
202
203 if (msg.invitation_flags)
204 persistent = *msg.invitation_flags & P2P_INVITATION_FLAGS_TYPE;
205 else {
206 /* Invitation Flags is a mandatory attribute starting from P2P
207 * spec 1.06. As a backwards compatibility mechanism, assume
208 * the request was for a persistent group if the attribute is
209 * missing.
210 */
211 wpa_printf(MSG_DEBUG, "P2P: Mandatory Invitation Flags "
212 "attribute missing from Invitation Request");
213 persistent = 1;
214 }
215
216 if (p2p_peer_channels_check(p2p, &p2p->cfg->channels, dev,
217 msg.channel_list, msg.channel_list_len) <
218 0) {
219 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
220 "P2P: No common channels found");
221 status = P2P_SC_FAIL_NO_COMMON_CHANNELS;
222 goto fail;
223 }
224
225 if (p2p->cfg->invitation_process) {
226 status = p2p->cfg->invitation_process(
227 p2p->cfg->cb_ctx, sa, msg.group_bssid, msg.group_id,
228 msg.group_id + ETH_ALEN, msg.group_id_len - ETH_ALEN,
229 &go, group_bssid, &op_freq, persistent);
230 }
231
232 if (op_freq) {
233 if (p2p_freq_to_channel(p2p->cfg->country, op_freq,
234 &reg_class, &channel) < 0) {
235 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
236 "P2P: Unknown forced freq %d MHz from "
237 "invitation_process()", op_freq);
238 status = P2P_SC_FAIL_NO_COMMON_CHANNELS;
239 goto fail;
240 }
241
242 p2p_channels_intersect(&p2p->cfg->channels, &dev->channels,
243 &intersection);
244 if (!p2p_channels_includes(&intersection, reg_class, channel))
245 {
246 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
247 "P2P: forced freq %d MHz not in the supported "
248 "channels interaction", op_freq);
249 status = P2P_SC_FAIL_NO_COMMON_CHANNELS;
250 goto fail;
251 }
252
253 if (status == P2P_SC_SUCCESS)
254 channels = &intersection;
255 } else {
256 op_freq = p2p_channel_to_freq(p2p->cfg->country,
257 p2p->cfg->op_reg_class,
258 p2p->cfg->op_channel);
259 if (op_freq < 0) {
260 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
261 "P2P: Unknown operational channel "
262 "(country=%c%c reg_class=%u channel=%u)",
263 p2p->cfg->country[0], p2p->cfg->country[1],
264 p2p->cfg->op_reg_class, p2p->cfg->op_channel);
265 status = P2P_SC_FAIL_NO_COMMON_CHANNELS;
266 goto fail;
267 }
268
269 p2p_channels_intersect(&p2p->cfg->channels, &dev->channels,
270 &intersection);
271 if (status == P2P_SC_SUCCESS) {
272 reg_class = p2p->cfg->op_reg_class;
273 channel = p2p->cfg->op_channel;
274 channels = &intersection;
275 }
276 }
277
278fail:
279 if (go && status == P2P_SC_SUCCESS && !is_zero_ether_addr(group_bssid))
280 bssid = group_bssid;
281 else
282 bssid = NULL;
283 resp = p2p_build_invitation_resp(p2p, dev, msg.dialog_token, status,
284 bssid, reg_class, channel, channels);
285
286 if (resp == NULL)
287 goto out;
288
289 if (rx_freq > 0)
290 freq = rx_freq;
291 else
292 freq = p2p_channel_to_freq(p2p->cfg->country,
293 p2p->cfg->reg_class,
294 p2p->cfg->channel);
295 if (freq < 0) {
296 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
297 "P2P: Unknown regulatory class/channel");
298 goto out;
299 }
300
301 /*
302 * Store copy of invitation data to be used when processing TX status
303 * callback for the Acton frame.
304 */
305 os_memcpy(p2p->inv_sa, sa, ETH_ALEN);
306 if (msg.group_bssid) {
307 os_memcpy(p2p->inv_group_bssid, msg.group_bssid, ETH_ALEN);
308 p2p->inv_group_bssid_ptr = p2p->inv_group_bssid;
309 } else
310 p2p->inv_group_bssid_ptr = NULL;
311 if (msg.group_id_len - ETH_ALEN <= 32) {
312 os_memcpy(p2p->inv_ssid, msg.group_id + ETH_ALEN,
313 msg.group_id_len - ETH_ALEN);
314 p2p->inv_ssid_len = msg.group_id_len - ETH_ALEN;
315 }
316 os_memcpy(p2p->inv_go_dev_addr, msg.group_id, ETH_ALEN);
317 p2p->inv_status = status;
318 p2p->inv_op_freq = op_freq;
319
320 p2p->pending_action_state = P2P_PENDING_INVITATION_RESPONSE;
321 if (p2p_send_action(p2p, freq, sa, p2p->cfg->dev_addr,
322 p2p->cfg->dev_addr,
323 wpabuf_head(resp), wpabuf_len(resp), 200) < 0) {
324 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
325 "P2P: Failed to send Action frame");
326 }
327
328out:
329 wpabuf_free(resp);
330 p2p_parse_free(&msg);
331}
332
333
334void p2p_process_invitation_resp(struct p2p_data *p2p, const u8 *sa,
335 const u8 *data, size_t len)
336{
337 struct p2p_device *dev;
338 struct p2p_message msg;
339
340 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
341 "P2P: Received Invitation Response from " MACSTR,
342 MAC2STR(sa));
343
344 dev = p2p_get_device(p2p, sa);
345 if (dev == NULL) {
346 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
347 "P2P: Ignore Invitation Response from unknown peer "
348 MACSTR, MAC2STR(sa));
349 return;
350 }
351
352 if (dev != p2p->invite_peer) {
353 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
354 "P2P: Ignore unexpected Invitation Response from peer "
355 MACSTR, MAC2STR(sa));
356 return;
357 }
358
359 if (p2p_parse(data, len, &msg))
360 return;
361
362 if (!msg.status) {
363 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
364 "P2P: Mandatory Status attribute missing in "
365 "Invitation Response from " MACSTR, MAC2STR(sa));
366 p2p_parse_free(&msg);
367 return;
368 }
369
370 if (p2p->cfg->invitation_result)
371 p2p->cfg->invitation_result(p2p->cfg->cb_ctx, *msg.status,
372 msg.group_bssid);
373
374 p2p_parse_free(&msg);
375
376 p2p_clear_timeout(p2p);
377 p2p_set_state(p2p, P2P_IDLE);
378 p2p->invite_peer = NULL;
379}
380
381
382int p2p_invite_send(struct p2p_data *p2p, struct p2p_device *dev,
383 const u8 *go_dev_addr)
384{
385 struct wpabuf *req;
386 int freq;
387
388 freq = dev->listen_freq > 0 ? dev->listen_freq : dev->oper_freq;
389 if (freq <= 0) {
390 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
391 "P2P: No Listen/Operating frequency known for the "
392 "peer " MACSTR " to send Invitation Request",
393 MAC2STR(dev->info.p2p_device_addr));
394 return -1;
395 }
396
397 req = p2p_build_invitation_req(p2p, dev, go_dev_addr);
398 if (req == NULL)
399 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700400 if (p2p->state != P2P_IDLE)
401 p2p_stop_listen_for_freq(p2p, freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700402 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
403 "P2P: Sending Invitation Request");
404 p2p_set_state(p2p, P2P_INVITE);
405 p2p->pending_action_state = P2P_PENDING_INVITATION_REQUEST;
406 p2p->invite_peer = dev;
407 dev->invitation_reqs++;
408 if (p2p_send_action(p2p, freq, dev->info.p2p_device_addr,
409 p2p->cfg->dev_addr, dev->info.p2p_device_addr,
410 wpabuf_head(req), wpabuf_len(req), 200) < 0) {
411 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
412 "P2P: Failed to send Action frame");
413 /* Use P2P find to recover and retry */
414 p2p_set_timeout(p2p, 0, 0);
415 }
416
417 wpabuf_free(req);
418
419 return 0;
420}
421
422
423void p2p_invitation_req_cb(struct p2p_data *p2p, int success)
424{
425 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
426 "P2P: Invitation Request TX callback: success=%d", success);
427
428 if (p2p->invite_peer == NULL) {
429 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
430 "P2P: No pending Invite");
431 return;
432 }
433
434 /*
435 * Use P2P find, if needed, to find the other device from its listen
436 * channel.
437 */
438 p2p_set_state(p2p, P2P_INVITE);
439 p2p_set_timeout(p2p, 0, 100000);
440}
441
442
443void p2p_invitation_resp_cb(struct p2p_data *p2p, int success)
444{
445 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
446 "P2P: Invitation Response TX callback: success=%d", success);
447 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
448
449 if (success && p2p->cfg->invitation_received) {
450 p2p->cfg->invitation_received(p2p->cfg->cb_ctx,
451 p2p->inv_sa,
452 p2p->inv_group_bssid_ptr,
453 p2p->inv_ssid, p2p->inv_ssid_len,
454 p2p->inv_go_dev_addr,
455 p2p->inv_status,
456 p2p->inv_op_freq);
457 }
458}
459
460
461int p2p_invite(struct p2p_data *p2p, const u8 *peer, enum p2p_invite_role role,
462 const u8 *bssid, const u8 *ssid, size_t ssid_len,
463 unsigned int force_freq, const u8 *go_dev_addr,
464 int persistent_group)
465{
466 struct p2p_device *dev;
467
468 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
469 "P2P: Request to invite peer " MACSTR " role=%d persistent=%d "
470 "force_freq=%u",
471 MAC2STR(peer), role, persistent_group, force_freq);
472 if (bssid)
473 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
474 "P2P: Invitation for BSSID " MACSTR, MAC2STR(bssid));
475 if (go_dev_addr) {
476 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
477 "P2P: Invitation for GO Device Address " MACSTR,
478 MAC2STR(go_dev_addr));
479 os_memcpy(p2p->invite_go_dev_addr_buf, go_dev_addr, ETH_ALEN);
480 p2p->invite_go_dev_addr = p2p->invite_go_dev_addr_buf;
481 } else
482 p2p->invite_go_dev_addr = NULL;
483 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Invitation for SSID",
484 ssid, ssid_len);
485
486 dev = p2p_get_device(p2p, peer);
487 if (dev == NULL || (dev->listen_freq <= 0 && dev->oper_freq <= 0)) {
488 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
489 "P2P: Cannot invite unknown P2P Device " MACSTR,
490 MAC2STR(peer));
491 return -1;
492 }
493
494 if (dev->flags & P2P_DEV_GROUP_CLIENT_ONLY) {
495 if (!(dev->info.dev_capab &
496 P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY)) {
497 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
498 "P2P: Cannot invite a P2P Device " MACSTR
499 " that is in a group and is not discoverable",
500 MAC2STR(peer));
501 }
502 /* TODO: use device discoverability request through GO */
503 }
504
505 dev->invitation_reqs = 0;
506
507 if (force_freq) {
508 if (p2p_freq_to_channel(p2p->cfg->country, force_freq,
509 &p2p->op_reg_class, &p2p->op_channel) <
510 0) {
511 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
512 "P2P: Unsupported frequency %u MHz",
513 force_freq);
514 return -1;
515 }
516 p2p->channels.reg_classes = 1;
517 p2p->channels.reg_class[0].channels = 1;
518 p2p->channels.reg_class[0].reg_class = p2p->op_reg_class;
519 p2p->channels.reg_class[0].channel[0] = p2p->op_channel;
520 } else {
521 p2p->op_reg_class = p2p->cfg->op_reg_class;
522 p2p->op_channel = p2p->cfg->op_channel;
523 os_memcpy(&p2p->channels, &p2p->cfg->channels,
524 sizeof(struct p2p_channels));
525 }
526
527 if (p2p->state != P2P_IDLE)
528 p2p_stop_find(p2p);
529
530 p2p->inv_role = role;
531 p2p->inv_bssid_set = bssid != NULL;
532 if (bssid)
533 os_memcpy(p2p->inv_bssid, bssid, ETH_ALEN);
534 os_memcpy(p2p->inv_ssid, ssid, ssid_len);
535 p2p->inv_ssid_len = ssid_len;
536 p2p->inv_persistent = persistent_group;
537 return p2p_invite_send(p2p, dev, go_dev_addr);
538}