2 * Wi-Fi Direct - P2P Invitation procedure
3 * Copyright (c) 2010, Atheros Communications
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
12 #include "common/ieee802_11_defs.h"
13 #include "common/wpa_ctrl.h"
18 static struct wpabuf * p2p_build_invitation_req(struct p2p_data *p2p,
19 struct p2p_device *peer,
20 const u8 *go_dev_addr,
28 #ifdef CONFIG_WIFI_DISPLAY
29 struct wpabuf *wfd_ie = p2p->wfd_ie_invitation;
30 if (wfd_ie && p2p->inv_role == P2P_INVITE_ROLE_ACTIVE_GO) {
32 for (i = 0; i < p2p->num_groups; i++) {
33 struct p2p_group *g = p2p->groups[i];
35 if (os_memcmp(p2p_group_get_interface_addr(g),
36 p2p->inv_bssid, ETH_ALEN) != 0)
38 ie = p2p_group_get_wfd_ie(g);
46 extra = wpabuf_len(wfd_ie);
47 #endif /* CONFIG_WIFI_DISPLAY */
49 if (p2p->vendor_elem && p2p->vendor_elem[VENDOR_ELEM_P2P_INV_REQ])
50 extra += wpabuf_len(p2p->vendor_elem[VENDOR_ELEM_P2P_INV_REQ]);
52 buf = wpabuf_alloc(1000 + extra);
57 if (peer->dialog_token == 0)
58 peer->dialog_token = 1;
59 p2p_buf_add_public_action_hdr(buf, P2P_INVITATION_REQ,
62 len = p2p_buf_add_ie_hdr(buf);
63 if (p2p->inv_role == P2P_INVITE_ROLE_ACTIVE_GO || !p2p->inv_persistent)
64 p2p_buf_add_config_timeout(buf, 0, 0);
66 p2p_buf_add_config_timeout(buf, p2p->go_timeout,
68 p2p_buf_add_invitation_flags(buf, p2p->inv_persistent ?
69 P2P_INVITATION_FLAGS_TYPE : 0);
70 if (p2p->inv_role != P2P_INVITE_ROLE_CLIENT ||
71 !(peer->flags & P2P_DEV_NO_PREF_CHAN))
72 p2p_buf_add_operating_channel(buf, p2p->cfg->country,
75 if (p2p->inv_bssid_set)
76 p2p_buf_add_group_bssid(buf, p2p->inv_bssid);
77 p2p_buf_add_channel_list(buf, p2p->cfg->country, &p2p->channels);
79 dev_addr = go_dev_addr;
80 else if (p2p->inv_role == P2P_INVITE_ROLE_CLIENT)
81 dev_addr = peer->info.p2p_device_addr;
83 dev_addr = p2p->cfg->dev_addr;
84 p2p_buf_add_group_id(buf, dev_addr, p2p->inv_ssid, p2p->inv_ssid_len);
85 p2p_buf_add_device_info(buf, p2p, peer);
86 p2p_buf_update_ie_hdr(buf, len);
88 #ifdef CONFIG_WIFI_DISPLAY
90 wpabuf_put_buf(buf, wfd_ie);
91 #endif /* CONFIG_WIFI_DISPLAY */
93 if (p2p->vendor_elem && p2p->vendor_elem[VENDOR_ELEM_P2P_INV_REQ])
94 wpabuf_put_buf(buf, p2p->vendor_elem[VENDOR_ELEM_P2P_INV_REQ]);
97 /* WSC IE in Invitation Request for NFC static handover */
98 p2p_build_wps_ie(p2p, buf, dev_pw_id, 0);
105 static struct wpabuf * p2p_build_invitation_resp(struct p2p_data *p2p,
106 struct p2p_device *peer,
107 u8 dialog_token, u8 status,
108 const u8 *group_bssid,
109 u8 reg_class, u8 channel,
110 struct p2p_channels *channels)
116 #ifdef CONFIG_WIFI_DISPLAY
117 struct wpabuf *wfd_ie = p2p->wfd_ie_invitation;
118 if (wfd_ie && group_bssid) {
120 for (i = 0; i < p2p->num_groups; i++) {
121 struct p2p_group *g = p2p->groups[i];
123 if (os_memcmp(p2p_group_get_interface_addr(g),
124 group_bssid, ETH_ALEN) != 0)
126 ie = p2p_group_get_wfd_ie(g);
134 extra = wpabuf_len(wfd_ie);
135 #endif /* CONFIG_WIFI_DISPLAY */
137 buf = wpabuf_alloc(1000 + extra);
141 p2p_buf_add_public_action_hdr(buf, P2P_INVITATION_RESP,
144 len = p2p_buf_add_ie_hdr(buf);
145 p2p_buf_add_status(buf, status);
146 p2p_buf_add_config_timeout(buf, 0, 0); /* FIX */
147 if (reg_class && channel)
148 p2p_buf_add_operating_channel(buf, p2p->cfg->country,
151 p2p_buf_add_group_bssid(buf, group_bssid);
153 p2p_buf_add_channel_list(buf, p2p->cfg->country, channels);
154 p2p_buf_update_ie_hdr(buf, len);
156 #ifdef CONFIG_WIFI_DISPLAY
158 wpabuf_put_buf(buf, wfd_ie);
159 #endif /* CONFIG_WIFI_DISPLAY */
165 void p2p_process_invitation_req(struct p2p_data *p2p, const u8 *sa,
166 const u8 *data, size_t len, int rx_freq)
168 struct p2p_device *dev;
169 struct p2p_message msg;
170 struct wpabuf *resp = NULL;
171 u8 status = P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
174 u8 group_bssid[ETH_ALEN], *bssid;
176 u8 reg_class = 0, channel = 0;
177 struct p2p_channels all_channels, intersection, *channels = NULL;
180 os_memset(group_bssid, 0, sizeof(group_bssid));
182 p2p_dbg(p2p, "Received Invitation Request from " MACSTR " (freq=%d)",
183 MAC2STR(sa), rx_freq);
185 if (p2p_parse(data, len, &msg))
188 dev = p2p_get_device(p2p, sa);
189 if (dev == NULL || (dev->flags & P2P_DEV_PROBE_REQ_ONLY)) {
190 p2p_dbg(p2p, "Invitation Request from unknown peer " MACSTR,
193 if (p2p_add_device(p2p, sa, rx_freq, NULL, 0, data + 1, len - 1,
195 p2p_dbg(p2p, "Invitation Request add device failed "
196 MACSTR, MAC2STR(sa));
197 status = P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
201 dev = p2p_get_device(p2p, sa);
203 p2p_dbg(p2p, "Reject Invitation Request from unknown peer "
204 MACSTR, MAC2STR(sa));
205 status = P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
210 if (!msg.group_id || !msg.channel_list) {
211 p2p_dbg(p2p, "Mandatory attribute missing in Invitation Request from "
212 MACSTR, MAC2STR(sa));
213 status = P2P_SC_FAIL_INVALID_PARAMS;
217 if (msg.invitation_flags)
218 persistent = *msg.invitation_flags & P2P_INVITATION_FLAGS_TYPE;
220 /* Invitation Flags is a mandatory attribute starting from P2P
221 * spec 1.06. As a backwards compatibility mechanism, assume
222 * the request was for a persistent group if the attribute is
225 p2p_dbg(p2p, "Mandatory Invitation Flags attribute missing from Invitation Request");
229 p2p_channels_union(&p2p->cfg->channels, &p2p->cfg->cli_channels,
232 if (p2p_peer_channels_check(p2p, &all_channels, dev,
233 msg.channel_list, msg.channel_list_len) <
235 p2p_dbg(p2p, "No common channels found");
236 status = P2P_SC_FAIL_NO_COMMON_CHANNELS;
240 p2p_channels_dump(p2p, "own channels", &p2p->cfg->channels);
241 p2p_channels_dump(p2p, "own client channels", &all_channels);
242 p2p_channels_dump(p2p, "peer channels", &dev->channels);
243 p2p_channels_intersect(&all_channels, &dev->channels,
245 p2p_channels_dump(p2p, "intersection", &intersection);
247 if (p2p->cfg->invitation_process) {
248 status = p2p->cfg->invitation_process(
249 p2p->cfg->cb_ctx, sa, msg.group_bssid, msg.group_id,
250 msg.group_id + ETH_ALEN, msg.group_id_len - ETH_ALEN,
251 &go, group_bssid, &op_freq, persistent, &intersection,
252 msg.dev_password_id_present ? msg.dev_password_id : -1);
256 p2p_channels_intersect(&p2p->cfg->channels, &dev->channels,
258 p2p_channels_dump(p2p, "intersection(GO)", &intersection);
259 if (intersection.reg_classes == 0) {
260 p2p_dbg(p2p, "No common channels found (GO)");
261 status = P2P_SC_FAIL_NO_COMMON_CHANNELS;
267 p2p_dbg(p2p, "Invitation processing forced frequency %d MHz",
269 if (p2p_freq_to_channel(op_freq, ®_class, &channel) < 0) {
270 p2p_dbg(p2p, "Unknown forced freq %d MHz from invitation_process()",
272 status = P2P_SC_FAIL_NO_COMMON_CHANNELS;
276 if (!p2p_channels_includes(&intersection, reg_class, channel))
278 p2p_dbg(p2p, "forced freq %d MHz not in the supported channels interaction",
280 status = P2P_SC_FAIL_NO_COMMON_CHANNELS;
284 if (status == P2P_SC_SUCCESS)
285 channels = &intersection;
287 p2p_dbg(p2p, "No forced channel from invitation processing - figure out best one to use");
289 /* Default to own configuration as a starting point */
290 p2p->op_reg_class = p2p->cfg->op_reg_class;
291 p2p->op_channel = p2p->cfg->op_channel;
292 p2p_dbg(p2p, "Own default op_class %d channel %d",
293 p2p->op_reg_class, p2p->op_channel);
295 /* Use peer preference if specified and compatible */
296 if (msg.operating_channel) {
298 req_freq = p2p_channel_to_freq(
299 msg.operating_channel[3],
300 msg.operating_channel[4]);
301 p2p_dbg(p2p, "Peer operating channel preference: %d MHz",
304 p2p_channels_includes(&intersection,
305 msg.operating_channel[3],
306 msg.operating_channel[4])) {
307 p2p->op_reg_class = msg.operating_channel[3];
308 p2p->op_channel = msg.operating_channel[4];
309 p2p_dbg(p2p, "Use peer preference op_class %d channel %d",
310 p2p->op_reg_class, p2p->op_channel);
312 p2p_dbg(p2p, "Cannot use peer channel preference");
316 /* Reselect the channel only for the case of the GO */
318 !p2p_channels_includes(&intersection, p2p->op_reg_class,
320 p2p_dbg(p2p, "Initially selected channel (op_class %d channel %d) not in channel intersection - try to reselect",
321 p2p->op_reg_class, p2p->op_channel);
322 p2p_reselect_channel(p2p, &intersection);
323 p2p_dbg(p2p, "Re-selection result: op_class %d channel %d",
324 p2p->op_reg_class, p2p->op_channel);
325 if (!p2p_channels_includes(&intersection,
328 p2p_dbg(p2p, "Peer does not support selected operating channel (reg_class=%u channel=%u)",
329 p2p->op_reg_class, p2p->op_channel);
330 status = P2P_SC_FAIL_NO_COMMON_CHANNELS;
333 } else if (go && !(dev->flags & P2P_DEV_FORCE_FREQ) &&
334 !p2p->cfg->cfg_op_channel) {
335 p2p_dbg(p2p, "Try to reselect channel selection with peer information received; previously selected op_class %u channel %u",
336 p2p->op_reg_class, p2p->op_channel);
337 p2p_reselect_channel(p2p, &intersection);
340 op_freq = p2p_channel_to_freq(p2p->op_reg_class,
343 p2p_dbg(p2p, "Unknown operational channel (country=%c%c reg_class=%u channel=%u)",
344 p2p->cfg->country[0], p2p->cfg->country[1],
345 p2p->op_reg_class, p2p->op_channel);
346 status = P2P_SC_FAIL_NO_COMMON_CHANNELS;
349 p2p_dbg(p2p, "Selected operating channel - %d MHz", op_freq);
351 if (status == P2P_SC_SUCCESS) {
352 reg_class = p2p->op_reg_class;
353 channel = p2p->op_channel;
354 channels = &intersection;
359 if (go && status == P2P_SC_SUCCESS && !is_zero_ether_addr(group_bssid))
363 resp = p2p_build_invitation_resp(p2p, dev, msg.dialog_token, status,
364 bssid, reg_class, channel, channels);
372 freq = p2p_channel_to_freq(p2p->cfg->reg_class,
375 p2p_dbg(p2p, "Unknown regulatory class/channel");
380 * Store copy of invitation data to be used when processing TX status
381 * callback for the Acton frame.
383 os_memcpy(p2p->inv_sa, sa, ETH_ALEN);
384 if (msg.group_bssid) {
385 os_memcpy(p2p->inv_group_bssid, msg.group_bssid, ETH_ALEN);
386 p2p->inv_group_bssid_ptr = p2p->inv_group_bssid;
388 p2p->inv_group_bssid_ptr = NULL;
390 if (msg.group_id_len - ETH_ALEN <= 32) {
391 os_memcpy(p2p->inv_ssid, msg.group_id + ETH_ALEN,
392 msg.group_id_len - ETH_ALEN);
393 p2p->inv_ssid_len = msg.group_id_len - ETH_ALEN;
395 os_memcpy(p2p->inv_go_dev_addr, msg.group_id, ETH_ALEN);
397 p2p->inv_ssid_len = 0;
398 os_memset(p2p->inv_go_dev_addr, 0, ETH_ALEN);
400 p2p->inv_status = status;
401 p2p->inv_op_freq = op_freq;
403 p2p->pending_action_state = P2P_PENDING_INVITATION_RESPONSE;
404 if (p2p_send_action(p2p, freq, sa, p2p->cfg->dev_addr,
406 wpabuf_head(resp), wpabuf_len(resp), 200) < 0) {
407 p2p_dbg(p2p, "Failed to send Action frame");
412 p2p_parse_free(&msg);
416 void p2p_process_invitation_resp(struct p2p_data *p2p, const u8 *sa,
417 const u8 *data, size_t len)
419 struct p2p_device *dev;
420 struct p2p_message msg;
421 struct p2p_channels intersection, *channels = NULL;
423 p2p_dbg(p2p, "Received Invitation Response from " MACSTR,
426 dev = p2p_get_device(p2p, sa);
428 p2p_dbg(p2p, "Ignore Invitation Response from unknown peer "
429 MACSTR, MAC2STR(sa));
430 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
434 if (dev != p2p->invite_peer) {
435 p2p_dbg(p2p, "Ignore unexpected Invitation Response from peer "
436 MACSTR, MAC2STR(sa));
437 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
441 if (p2p_parse(data, len, &msg)) {
442 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
447 p2p_dbg(p2p, "Mandatory Status attribute missing in Invitation Response from "
448 MACSTR, MAC2STR(sa));
449 p2p_parse_free(&msg);
450 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
455 * We should not really receive a replayed response twice since
456 * duplicate frames are supposed to be dropped. However, not all drivers
457 * do that for pre-association frames. We did not use to verify dialog
458 * token matches for invitation response frames, but that check can be
459 * safely used to drop a replayed response to the previous Invitation
460 * Request in case the suggested operating channel was changed. This
461 * allows a duplicated reject frame to be dropped with the assumption
462 * that the real response follows after it.
464 if (*msg.status == P2P_SC_FAIL_NO_COMMON_CHANNELS &&
465 p2p->retry_invite_req_sent &&
466 msg.dialog_token != dev->dialog_token) {
467 p2p_dbg(p2p, "Unexpected Dialog Token %u (expected %u)",
468 msg.dialog_token, dev->dialog_token);
469 p2p_parse_free(&msg);
473 if (*msg.status == P2P_SC_FAIL_NO_COMMON_CHANNELS &&
474 p2p->retry_invite_req &&
475 p2p_channel_random_social(&p2p->cfg->channels, &p2p->op_reg_class,
476 &p2p->op_channel) == 0) {
477 p2p->retry_invite_req = 0;
478 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
479 p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
480 p2p_set_state(p2p, P2P_INVITE);
481 p2p_dbg(p2p, "Resend Invitation Request setting op_class %u channel %u as operating channel",
482 p2p->op_reg_class, p2p->op_channel);
483 p2p->retry_invite_req_sent = 1;
484 p2p_invite_send(p2p, p2p->invite_peer, p2p->invite_go_dev_addr,
485 p2p->invite_dev_pw_id);
486 p2p_parse_free(&msg);
489 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
490 p2p->retry_invite_req = 0;
492 if (!msg.channel_list && *msg.status == P2P_SC_SUCCESS) {
493 p2p_dbg(p2p, "Mandatory Channel List attribute missing in Invitation Response from "
494 MACSTR, MAC2STR(sa));
495 #ifdef CONFIG_P2P_STRICT
496 p2p_parse_free(&msg);
498 #endif /* CONFIG_P2P_STRICT */
499 /* Try to survive without peer channel list */
500 channels = &p2p->channels;
501 } else if (!msg.channel_list) {
502 /* Non-success cases are not required to include Channel List */
503 channels = &p2p->channels;
504 } else if (p2p_peer_channels_check(p2p, &p2p->channels, dev,
506 msg.channel_list_len) < 0) {
507 p2p_dbg(p2p, "No common channels found");
508 p2p_parse_free(&msg);
511 p2p_channels_intersect(&p2p->channels, &dev->channels,
513 channels = &intersection;
516 if (p2p->cfg->invitation_result) {
517 int peer_oper_freq = 0;
518 int freq = p2p_channel_to_freq(p2p->op_reg_class,
523 if (msg.operating_channel) {
524 peer_oper_freq = p2p_channel_to_freq(
525 msg.operating_channel[3],
526 msg.operating_channel[4]);
527 if (peer_oper_freq < 0)
531 p2p->cfg->invitation_result(p2p->cfg->cb_ctx, *msg.status,
532 msg.group_bssid, channels, sa,
533 freq, peer_oper_freq);
536 p2p_parse_free(&msg);
538 p2p_clear_timeout(p2p);
539 p2p_set_state(p2p, P2P_IDLE);
540 p2p->invite_peer = NULL;
544 int p2p_invite_send(struct p2p_data *p2p, struct p2p_device *dev,
545 const u8 *go_dev_addr, int dev_pw_id)
550 freq = dev->listen_freq > 0 ? dev->listen_freq : dev->oper_freq;
552 freq = dev->oob_go_neg_freq;
554 p2p_dbg(p2p, "No Listen/Operating frequency known for the peer "
555 MACSTR " to send Invitation Request",
556 MAC2STR(dev->info.p2p_device_addr));
560 req = p2p_build_invitation_req(p2p, dev, go_dev_addr, dev_pw_id);
563 if (p2p->state != P2P_IDLE)
564 p2p_stop_listen_for_freq(p2p, freq);
565 p2p_dbg(p2p, "Sending Invitation Request");
566 p2p_set_state(p2p, P2P_INVITE);
567 p2p->pending_action_state = P2P_PENDING_INVITATION_REQUEST;
568 p2p->invite_peer = dev;
569 dev->invitation_reqs++;
570 if (p2p_send_action(p2p, freq, dev->info.p2p_device_addr,
571 p2p->cfg->dev_addr, dev->info.p2p_device_addr,
572 wpabuf_head(req), wpabuf_len(req), 500) < 0) {
573 p2p_dbg(p2p, "Failed to send Action frame");
574 /* Use P2P find to recover and retry */
575 p2p_set_timeout(p2p, 0, 0);
577 dev->flags |= P2P_DEV_WAIT_INV_REQ_ACK;
586 void p2p_invitation_req_cb(struct p2p_data *p2p, int success)
588 p2p_dbg(p2p, "Invitation Request TX callback: success=%d", success);
590 if (p2p->invite_peer == NULL) {
591 p2p_dbg(p2p, "No pending Invite");
596 p2p->invite_peer->flags &= ~P2P_DEV_WAIT_INV_REQ_ACK;
599 * Use P2P find, if needed, to find the other device from its listen
602 p2p_set_state(p2p, P2P_INVITE);
603 p2p_set_timeout(p2p, 0, success ? 500000 : 100000);
607 void p2p_invitation_resp_cb(struct p2p_data *p2p, int success)
609 p2p_dbg(p2p, "Invitation Response TX callback: success=%d", success);
610 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
613 p2p_dbg(p2p, "Assume Invitation Response was actually received by the peer even though Ack was not reported");
615 if (p2p->cfg->invitation_received) {
616 p2p->cfg->invitation_received(p2p->cfg->cb_ctx,
618 p2p->inv_group_bssid_ptr,
619 p2p->inv_ssid, p2p->inv_ssid_len,
620 p2p->inv_go_dev_addr,
627 int p2p_invite(struct p2p_data *p2p, const u8 *peer, enum p2p_invite_role role,
628 const u8 *bssid, const u8 *ssid, size_t ssid_len,
629 unsigned int force_freq, const u8 *go_dev_addr,
630 int persistent_group, unsigned int pref_freq, int dev_pw_id)
632 struct p2p_device *dev;
634 p2p_dbg(p2p, "Request to invite peer " MACSTR " role=%d persistent=%d "
636 MAC2STR(peer), role, persistent_group, force_freq);
638 p2p_dbg(p2p, "Invitation for BSSID " MACSTR, MAC2STR(bssid));
640 p2p_dbg(p2p, "Invitation for GO Device Address " MACSTR,
641 MAC2STR(go_dev_addr));
642 os_memcpy(p2p->invite_go_dev_addr_buf, go_dev_addr, ETH_ALEN);
643 p2p->invite_go_dev_addr = p2p->invite_go_dev_addr_buf;
645 p2p->invite_go_dev_addr = NULL;
646 wpa_hexdump_ascii(MSG_DEBUG, "Invitation for SSID",
648 if (dev_pw_id >= 0) {
649 p2p_dbg(p2p, "Invitation to use Device Password ID %d",
652 p2p->invite_dev_pw_id = dev_pw_id;
653 p2p->retry_invite_req = role == P2P_INVITE_ROLE_GO &&
654 persistent_group && !force_freq;
655 p2p->retry_invite_req_sent = 0;
657 dev = p2p_get_device(p2p, peer);
658 if (dev == NULL || (dev->listen_freq <= 0 && dev->oper_freq <= 0 &&
659 dev->oob_go_neg_freq <= 0)) {
660 p2p_dbg(p2p, "Cannot invite unknown P2P Device " MACSTR,
665 if (p2p_prepare_channel(p2p, dev, force_freq, pref_freq,
666 role != P2P_INVITE_ROLE_CLIENT) < 0)
669 if (persistent_group && role == P2P_INVITE_ROLE_CLIENT && !force_freq &&
671 dev->flags |= P2P_DEV_NO_PREF_CHAN;
673 dev->flags &= ~P2P_DEV_NO_PREF_CHAN;
675 if (dev->flags & P2P_DEV_GROUP_CLIENT_ONLY) {
676 if (!(dev->info.dev_capab &
677 P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY)) {
678 p2p_dbg(p2p, "Cannot invite a P2P Device " MACSTR
679 " that is in a group and is not discoverable",
682 /* TODO: use device discoverability request through GO */
685 dev->invitation_reqs = 0;
687 if (p2p->state != P2P_IDLE)
690 p2p->inv_role = role;
691 p2p->inv_bssid_set = bssid != NULL;
693 os_memcpy(p2p->inv_bssid, bssid, ETH_ALEN);
694 os_memcpy(p2p->inv_ssid, ssid, ssid_len);
695 p2p->inv_ssid_len = ssid_len;
696 p2p->inv_persistent = persistent_group;
697 return p2p_invite_send(p2p, dev, go_dev_addr, dev_pw_id);