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"
17 static struct wpabuf * p2p_build_invitation_req(struct p2p_data *p2p,
18 struct p2p_device *peer,
19 const u8 *go_dev_addr,
27 #ifdef CONFIG_WIFI_DISPLAY
28 struct wpabuf *wfd_ie = p2p->wfd_ie_invitation;
29 if (wfd_ie && p2p->inv_role == P2P_INVITE_ROLE_ACTIVE_GO) {
31 for (i = 0; i < p2p->num_groups; i++) {
32 struct p2p_group *g = p2p->groups[i];
34 if (os_memcmp(p2p_group_get_interface_addr(g),
35 p2p->inv_bssid, ETH_ALEN) != 0)
37 ie = p2p_group_get_wfd_ie(g);
45 extra = wpabuf_len(wfd_ie);
46 #endif /* CONFIG_WIFI_DISPLAY */
48 buf = wpabuf_alloc(1000 + extra);
53 if (peer->dialog_token == 0)
54 peer->dialog_token = 1;
55 p2p_buf_add_public_action_hdr(buf, P2P_INVITATION_REQ,
58 len = p2p_buf_add_ie_hdr(buf);
59 if (p2p->inv_role == P2P_INVITE_ROLE_ACTIVE_GO || !p2p->inv_persistent)
60 p2p_buf_add_config_timeout(buf, 0, 0);
62 p2p_buf_add_config_timeout(buf, p2p->go_timeout,
64 p2p_buf_add_invitation_flags(buf, p2p->inv_persistent ?
65 P2P_INVITATION_FLAGS_TYPE : 0);
66 if (p2p->inv_role != P2P_INVITE_ROLE_CLIENT ||
67 !(peer->flags & P2P_DEV_NO_PREF_CHAN))
68 p2p_buf_add_operating_channel(buf, p2p->cfg->country,
71 if (p2p->inv_bssid_set)
72 p2p_buf_add_group_bssid(buf, p2p->inv_bssid);
73 p2p_buf_add_channel_list(buf, p2p->cfg->country, &p2p->channels);
75 dev_addr = go_dev_addr;
76 else if (p2p->inv_role == P2P_INVITE_ROLE_CLIENT)
77 dev_addr = peer->info.p2p_device_addr;
79 dev_addr = p2p->cfg->dev_addr;
80 p2p_buf_add_group_id(buf, dev_addr, p2p->inv_ssid, p2p->inv_ssid_len);
81 p2p_buf_add_device_info(buf, p2p, peer);
82 p2p_buf_update_ie_hdr(buf, len);
84 #ifdef CONFIG_WIFI_DISPLAY
86 wpabuf_put_buf(buf, wfd_ie);
87 #endif /* CONFIG_WIFI_DISPLAY */
90 /* WSC IE in Invitation Request for NFC static handover */
91 p2p_build_wps_ie(p2p, buf, dev_pw_id, 0);
98 static struct wpabuf * p2p_build_invitation_resp(struct p2p_data *p2p,
99 struct p2p_device *peer,
100 u8 dialog_token, u8 status,
101 const u8 *group_bssid,
102 u8 reg_class, u8 channel,
103 struct p2p_channels *channels)
109 #ifdef CONFIG_WIFI_DISPLAY
110 struct wpabuf *wfd_ie = p2p->wfd_ie_invitation;
111 if (wfd_ie && group_bssid) {
113 for (i = 0; i < p2p->num_groups; i++) {
114 struct p2p_group *g = p2p->groups[i];
116 if (os_memcmp(p2p_group_get_interface_addr(g),
117 group_bssid, ETH_ALEN) != 0)
119 ie = p2p_group_get_wfd_ie(g);
127 extra = wpabuf_len(wfd_ie);
128 #endif /* CONFIG_WIFI_DISPLAY */
130 buf = wpabuf_alloc(1000 + extra);
134 p2p_buf_add_public_action_hdr(buf, P2P_INVITATION_RESP,
137 len = p2p_buf_add_ie_hdr(buf);
138 p2p_buf_add_status(buf, status);
139 p2p_buf_add_config_timeout(buf, 0, 0); /* FIX */
140 if (reg_class && channel)
141 p2p_buf_add_operating_channel(buf, p2p->cfg->country,
144 p2p_buf_add_group_bssid(buf, group_bssid);
146 p2p_buf_add_channel_list(buf, p2p->cfg->country, channels);
147 p2p_buf_update_ie_hdr(buf, len);
149 #ifdef CONFIG_WIFI_DISPLAY
151 wpabuf_put_buf(buf, wfd_ie);
152 #endif /* CONFIG_WIFI_DISPLAY */
158 void p2p_process_invitation_req(struct p2p_data *p2p, const u8 *sa,
159 const u8 *data, size_t len, int rx_freq)
161 struct p2p_device *dev;
162 struct p2p_message msg;
163 struct wpabuf *resp = NULL;
164 u8 status = P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
167 u8 group_bssid[ETH_ALEN], *bssid;
169 u8 reg_class = 0, channel = 0;
170 struct p2p_channels intersection, *channels = NULL;
173 os_memset(group_bssid, 0, sizeof(group_bssid));
175 p2p_dbg(p2p, "Received Invitation Request from " MACSTR " (freq=%d)",
176 MAC2STR(sa), rx_freq);
178 if (p2p_parse(data, len, &msg))
181 dev = p2p_get_device(p2p, sa);
182 if (dev == NULL || (dev->flags & P2P_DEV_PROBE_REQ_ONLY)) {
183 p2p_dbg(p2p, "Invitation Request from unknown peer " MACSTR,
186 if (p2p_add_device(p2p, sa, rx_freq, NULL, 0, data + 1, len - 1,
188 p2p_dbg(p2p, "Invitation Request add device failed "
189 MACSTR, MAC2STR(sa));
190 status = P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
194 dev = p2p_get_device(p2p, sa);
196 p2p_dbg(p2p, "Reject Invitation Request from unknown peer "
197 MACSTR, MAC2STR(sa));
198 status = P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
203 if (!msg.group_id || !msg.channel_list) {
204 p2p_dbg(p2p, "Mandatory attribute missing in Invitation Request from "
205 MACSTR, MAC2STR(sa));
206 status = P2P_SC_FAIL_INVALID_PARAMS;
210 if (msg.invitation_flags)
211 persistent = *msg.invitation_flags & P2P_INVITATION_FLAGS_TYPE;
213 /* Invitation Flags is a mandatory attribute starting from P2P
214 * spec 1.06. As a backwards compatibility mechanism, assume
215 * the request was for a persistent group if the attribute is
218 p2p_dbg(p2p, "Mandatory Invitation Flags attribute missing from Invitation Request");
222 if (p2p_peer_channels_check(p2p, &p2p->cfg->channels, dev,
223 msg.channel_list, msg.channel_list_len) <
225 p2p_dbg(p2p, "No common channels found");
226 status = P2P_SC_FAIL_NO_COMMON_CHANNELS;
230 p2p_channels_intersect(&p2p->cfg->channels, &dev->channels,
233 if (p2p->cfg->invitation_process) {
234 status = p2p->cfg->invitation_process(
235 p2p->cfg->cb_ctx, sa, msg.group_bssid, msg.group_id,
236 msg.group_id + ETH_ALEN, msg.group_id_len - ETH_ALEN,
237 &go, group_bssid, &op_freq, persistent, &intersection,
238 msg.dev_password_id_present ? msg.dev_password_id : -1);
242 p2p_dbg(p2p, "Invitation processing forced frequency %d MHz",
244 if (p2p_freq_to_channel(op_freq, ®_class, &channel) < 0) {
245 p2p_dbg(p2p, "Unknown forced freq %d MHz from invitation_process()",
247 status = P2P_SC_FAIL_NO_COMMON_CHANNELS;
251 if (!p2p_channels_includes(&intersection, reg_class, channel))
253 p2p_dbg(p2p, "forced freq %d MHz not in the supported channels interaction",
255 status = P2P_SC_FAIL_NO_COMMON_CHANNELS;
259 if (status == P2P_SC_SUCCESS)
260 channels = &intersection;
262 p2p_dbg(p2p, "No forced channel from invitation processing - figure out best one to use");
264 /* Default to own configuration as a starting point */
265 p2p->op_reg_class = p2p->cfg->op_reg_class;
266 p2p->op_channel = p2p->cfg->op_channel;
267 p2p_dbg(p2p, "Own default op_class %d channel %d",
268 p2p->op_reg_class, p2p->op_channel);
270 /* Use peer preference if specified and compatible */
271 if (msg.operating_channel) {
273 req_freq = p2p_channel_to_freq(
274 msg.operating_channel[3],
275 msg.operating_channel[4]);
276 p2p_dbg(p2p, "Peer operating channel preference: %d MHz",
279 p2p_channels_includes(&intersection,
280 msg.operating_channel[3],
281 msg.operating_channel[4])) {
282 p2p->op_reg_class = msg.operating_channel[3];
283 p2p->op_channel = msg.operating_channel[4];
284 p2p_dbg(p2p, "Use peer preference op_class %d channel %d",
285 p2p->op_reg_class, p2p->op_channel);
287 p2p_dbg(p2p, "Cannot use peer channel preference");
291 /* Reselect the channel only for the case of the GO */
293 !p2p_channels_includes(&intersection, p2p->op_reg_class,
295 p2p_dbg(p2p, "Initially selected channel (op_class %d channel %d) not in channel intersection - try to reselect",
296 p2p->op_reg_class, p2p->op_channel);
297 p2p_reselect_channel(p2p, &intersection);
298 p2p_dbg(p2p, "Re-selection result: op_class %d channel %d",
299 p2p->op_reg_class, p2p->op_channel);
300 if (!p2p_channels_includes(&intersection,
303 p2p_dbg(p2p, "Peer does not support selected operating channel (reg_class=%u channel=%u)",
304 p2p->op_reg_class, p2p->op_channel);
305 status = P2P_SC_FAIL_NO_COMMON_CHANNELS;
308 } else if (go && !(dev->flags & P2P_DEV_FORCE_FREQ) &&
309 !p2p->cfg->cfg_op_channel) {
310 p2p_dbg(p2p, "Try to reselect channel selection with peer information received; previously selected op_class %u channel %u",
311 p2p->op_reg_class, p2p->op_channel);
312 p2p_reselect_channel(p2p, &intersection);
315 op_freq = p2p_channel_to_freq(p2p->op_reg_class,
318 p2p_dbg(p2p, "Unknown operational channel (country=%c%c reg_class=%u channel=%u)",
319 p2p->cfg->country[0], p2p->cfg->country[1],
320 p2p->op_reg_class, p2p->op_channel);
321 status = P2P_SC_FAIL_NO_COMMON_CHANNELS;
324 p2p_dbg(p2p, "Selected operating channel - %d MHz", op_freq);
326 if (status == P2P_SC_SUCCESS) {
327 reg_class = p2p->op_reg_class;
328 channel = p2p->op_channel;
329 channels = &intersection;
334 if (go && status == P2P_SC_SUCCESS && !is_zero_ether_addr(group_bssid))
338 resp = p2p_build_invitation_resp(p2p, dev, msg.dialog_token, status,
339 bssid, reg_class, channel, channels);
347 freq = p2p_channel_to_freq(p2p->cfg->reg_class,
350 p2p_dbg(p2p, "Unknown regulatory class/channel");
355 * Store copy of invitation data to be used when processing TX status
356 * callback for the Acton frame.
358 os_memcpy(p2p->inv_sa, sa, ETH_ALEN);
359 if (msg.group_bssid) {
360 os_memcpy(p2p->inv_group_bssid, msg.group_bssid, ETH_ALEN);
361 p2p->inv_group_bssid_ptr = p2p->inv_group_bssid;
363 p2p->inv_group_bssid_ptr = NULL;
365 if (msg.group_id_len - ETH_ALEN <= 32) {
366 os_memcpy(p2p->inv_ssid, msg.group_id + ETH_ALEN,
367 msg.group_id_len - ETH_ALEN);
368 p2p->inv_ssid_len = msg.group_id_len - ETH_ALEN;
370 os_memcpy(p2p->inv_go_dev_addr, msg.group_id, ETH_ALEN);
372 p2p->inv_ssid_len = 0;
373 os_memset(p2p->inv_go_dev_addr, 0, ETH_ALEN);
375 p2p->inv_status = status;
376 p2p->inv_op_freq = op_freq;
378 p2p->pending_action_state = P2P_PENDING_INVITATION_RESPONSE;
379 if (p2p_send_action(p2p, freq, sa, p2p->cfg->dev_addr,
381 wpabuf_head(resp), wpabuf_len(resp), 200) < 0) {
382 p2p_dbg(p2p, "Failed to send Action frame");
387 p2p_parse_free(&msg);
391 void p2p_process_invitation_resp(struct p2p_data *p2p, const u8 *sa,
392 const u8 *data, size_t len)
394 struct p2p_device *dev;
395 struct p2p_message msg;
396 struct p2p_channels intersection, *channels = NULL;
398 p2p_dbg(p2p, "Received Invitation Response from " MACSTR,
401 dev = p2p_get_device(p2p, sa);
403 p2p_dbg(p2p, "Ignore Invitation Response from unknown peer "
404 MACSTR, MAC2STR(sa));
408 if (dev != p2p->invite_peer) {
409 p2p_dbg(p2p, "Ignore unexpected Invitation Response from peer "
410 MACSTR, MAC2STR(sa));
414 if (p2p_parse(data, len, &msg))
418 p2p_dbg(p2p, "Mandatory Status attribute missing in Invitation Response from "
419 MACSTR, MAC2STR(sa));
420 p2p_parse_free(&msg);
424 if (!msg.channel_list && *msg.status == P2P_SC_SUCCESS) {
425 p2p_dbg(p2p, "Mandatory Channel List attribute missing in Invitation Response from "
426 MACSTR, MAC2STR(sa));
427 #ifdef CONFIG_P2P_STRICT
428 p2p_parse_free(&msg);
430 #endif /* CONFIG_P2P_STRICT */
431 /* Try to survive without peer channel list */
432 channels = &p2p->channels;
433 } else if (!msg.channel_list) {
434 /* Non-success cases are not required to include Channel List */
435 channels = &p2p->channels;
436 } else if (p2p_peer_channels_check(p2p, &p2p->channels, dev,
438 msg.channel_list_len) < 0) {
439 p2p_dbg(p2p, "No common channels found");
440 p2p_parse_free(&msg);
443 p2p_channels_intersect(&p2p->channels, &dev->channels,
445 channels = &intersection;
448 if (p2p->cfg->invitation_result) {
449 int peer_oper_freq = 0;
450 int freq = p2p_channel_to_freq(p2p->op_reg_class,
455 if (msg.operating_channel) {
456 peer_oper_freq = p2p_channel_to_freq(
457 msg.operating_channel[3],
458 msg.operating_channel[4]);
459 if (peer_oper_freq < 0)
463 p2p->cfg->invitation_result(p2p->cfg->cb_ctx, *msg.status,
464 msg.group_bssid, channels, sa,
465 freq, peer_oper_freq);
468 p2p_parse_free(&msg);
470 p2p_clear_timeout(p2p);
471 p2p_set_state(p2p, P2P_IDLE);
472 p2p->invite_peer = NULL;
476 int p2p_invite_send(struct p2p_data *p2p, struct p2p_device *dev,
477 const u8 *go_dev_addr, int dev_pw_id)
482 freq = dev->listen_freq > 0 ? dev->listen_freq : dev->oper_freq;
484 freq = dev->oob_go_neg_freq;
486 p2p_dbg(p2p, "No Listen/Operating frequency known for the peer "
487 MACSTR " to send Invitation Request",
488 MAC2STR(dev->info.p2p_device_addr));
492 req = p2p_build_invitation_req(p2p, dev, go_dev_addr, dev_pw_id);
495 if (p2p->state != P2P_IDLE)
496 p2p_stop_listen_for_freq(p2p, freq);
497 p2p_dbg(p2p, "Sending Invitation Request");
498 p2p_set_state(p2p, P2P_INVITE);
499 p2p->pending_action_state = P2P_PENDING_INVITATION_REQUEST;
500 p2p->invite_peer = dev;
501 dev->invitation_reqs++;
502 if (p2p_send_action(p2p, freq, dev->info.p2p_device_addr,
503 p2p->cfg->dev_addr, dev->info.p2p_device_addr,
504 wpabuf_head(req), wpabuf_len(req), 500) < 0) {
505 p2p_dbg(p2p, "Failed to send Action frame");
506 /* Use P2P find to recover and retry */
507 p2p_set_timeout(p2p, 0, 0);
509 dev->flags |= P2P_DEV_WAIT_INV_REQ_ACK;
518 void p2p_invitation_req_cb(struct p2p_data *p2p, int success)
520 p2p_dbg(p2p, "Invitation Request TX callback: success=%d", success);
522 if (p2p->invite_peer == NULL) {
523 p2p_dbg(p2p, "No pending Invite");
528 p2p->invite_peer->flags &= ~P2P_DEV_WAIT_INV_REQ_ACK;
531 * Use P2P find, if needed, to find the other device from its listen
534 p2p_set_state(p2p, P2P_INVITE);
535 p2p_set_timeout(p2p, 0, success ? 500000 : 100000);
539 void p2p_invitation_resp_cb(struct p2p_data *p2p, int success)
541 p2p_dbg(p2p, "Invitation Response TX callback: success=%d", success);
542 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
545 p2p_dbg(p2p, "Assume Invitation Response was actually received by the peer even though Ack was not reported");
547 if (p2p->cfg->invitation_received) {
548 p2p->cfg->invitation_received(p2p->cfg->cb_ctx,
550 p2p->inv_group_bssid_ptr,
551 p2p->inv_ssid, p2p->inv_ssid_len,
552 p2p->inv_go_dev_addr,
559 int p2p_invite(struct p2p_data *p2p, const u8 *peer, enum p2p_invite_role role,
560 const u8 *bssid, const u8 *ssid, size_t ssid_len,
561 unsigned int force_freq, const u8 *go_dev_addr,
562 int persistent_group, unsigned int pref_freq, int dev_pw_id)
564 struct p2p_device *dev;
566 p2p_dbg(p2p, "Request to invite peer " MACSTR " role=%d persistent=%d "
568 MAC2STR(peer), role, persistent_group, force_freq);
570 p2p_dbg(p2p, "Invitation for BSSID " MACSTR, MAC2STR(bssid));
572 p2p_dbg(p2p, "Invitation for GO Device Address " MACSTR,
573 MAC2STR(go_dev_addr));
574 os_memcpy(p2p->invite_go_dev_addr_buf, go_dev_addr, ETH_ALEN);
575 p2p->invite_go_dev_addr = p2p->invite_go_dev_addr_buf;
577 p2p->invite_go_dev_addr = NULL;
578 wpa_hexdump_ascii(MSG_DEBUG, "Invitation for SSID",
580 if (dev_pw_id >= 0) {
581 p2p_dbg(p2p, "Invitation to use Device Password ID %d",
584 p2p->invite_dev_pw_id = dev_pw_id;
586 dev = p2p_get_device(p2p, peer);
587 if (dev == NULL || (dev->listen_freq <= 0 && dev->oper_freq <= 0 &&
588 dev->oob_go_neg_freq <= 0)) {
589 p2p_dbg(p2p, "Cannot invite unknown P2P Device " MACSTR,
594 if (p2p_prepare_channel(p2p, dev, force_freq, pref_freq,
595 role != P2P_INVITE_ROLE_CLIENT) < 0)
598 if (persistent_group && role == P2P_INVITE_ROLE_CLIENT && !force_freq &&
600 dev->flags |= P2P_DEV_NO_PREF_CHAN;
602 dev->flags &= ~P2P_DEV_NO_PREF_CHAN;
604 if (dev->flags & P2P_DEV_GROUP_CLIENT_ONLY) {
605 if (!(dev->info.dev_capab &
606 P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY)) {
607 p2p_dbg(p2p, "Cannot invite a P2P Device " MACSTR
608 " that is in a group and is not discoverable",
611 /* TODO: use device discoverability request through GO */
614 dev->invitation_reqs = 0;
616 if (p2p->state != P2P_IDLE)
619 p2p->inv_role = role;
620 p2p->inv_bssid_set = bssid != NULL;
622 os_memcpy(p2p->inv_bssid, bssid, ETH_ALEN);
623 os_memcpy(p2p->inv_ssid, ssid, ssid_len);
624 p2p->inv_ssid_len = ssid_len;
625 p2p->inv_persistent = persistent_group;
626 return p2p_invite_send(p2p, dev, go_dev_addr, dev_pw_id);