blob: a45fe198d819475d530b996fdbe8ff9c1c3b9353 [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,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -080019 const u8 *go_dev_addr,
20 int dev_pw_id)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070021{
22 struct wpabuf *buf;
23 u8 *len;
24 const u8 *dev_addr;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070025 size_t extra = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070026
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070027#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) {
30 size_t i;
31 for (i = 0; i < p2p->num_groups; i++) {
32 struct p2p_group *g = p2p->groups[i];
33 struct wpabuf *ie;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -070034 if (os_memcmp(p2p_group_get_interface_addr(g),
35 p2p->inv_bssid, ETH_ALEN) != 0)
36 continue;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070037 ie = p2p_group_get_wfd_ie(g);
38 if (ie) {
39 wfd_ie = ie;
40 break;
41 }
42 }
43 }
44 if (wfd_ie)
45 extra = wpabuf_len(wfd_ie);
46#endif /* CONFIG_WIFI_DISPLAY */
47
48 buf = wpabuf_alloc(1000 + extra);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070049 if (buf == NULL)
50 return NULL;
51
52 peer->dialog_token++;
53 if (peer->dialog_token == 0)
54 peer->dialog_token = 1;
55 p2p_buf_add_public_action_hdr(buf, P2P_INVITATION_REQ,
56 peer->dialog_token);
57
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);
61 else
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070062 p2p_buf_add_config_timeout(buf, p2p->go_timeout,
63 p2p->client_timeout);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070064 p2p_buf_add_invitation_flags(buf, p2p->inv_persistent ?
65 P2P_INVITATION_FLAGS_TYPE : 0);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -080066 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,
69 p2p->op_reg_class,
70 p2p->op_channel);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070071 if (p2p->inv_bssid_set)
72 p2p_buf_add_group_bssid(buf, p2p->inv_bssid);
Irfan Sheriff069fa2c2012-09-24 23:59:04 -070073 p2p_buf_add_channel_list(buf, p2p->cfg->country, &p2p->channels);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070074 if (go_dev_addr)
75 dev_addr = go_dev_addr;
76 else if (p2p->inv_role == P2P_INVITE_ROLE_CLIENT)
77 dev_addr = peer->info.p2p_device_addr;
78 else
79 dev_addr = p2p->cfg->dev_addr;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070080 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);
83
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070084#ifdef CONFIG_WIFI_DISPLAY
85 if (wfd_ie)
86 wpabuf_put_buf(buf, wfd_ie);
87#endif /* CONFIG_WIFI_DISPLAY */
88
Dmitry Shmidtcf32e602014-01-28 10:57:39 -080089 if (dev_pw_id >= 0) {
90 /* WSC IE in Invitation Request for NFC static handover */
91 p2p_build_wps_ie(p2p, buf, dev_pw_id, 0);
92 }
93
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070094 return buf;
95}
96
97
98static 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)
104{
105 struct wpabuf *buf;
106 u8 *len;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700107 size_t extra = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700108
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700109#ifdef CONFIG_WIFI_DISPLAY
110 struct wpabuf *wfd_ie = p2p->wfd_ie_invitation;
111 if (wfd_ie && group_bssid) {
112 size_t i;
113 for (i = 0; i < p2p->num_groups; i++) {
114 struct p2p_group *g = p2p->groups[i];
115 struct wpabuf *ie;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700116 if (os_memcmp(p2p_group_get_interface_addr(g),
117 group_bssid, ETH_ALEN) != 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700118 continue;
119 ie = p2p_group_get_wfd_ie(g);
120 if (ie) {
121 wfd_ie = ie;
122 break;
123 }
124 }
125 }
126 if (wfd_ie)
127 extra = wpabuf_len(wfd_ie);
128#endif /* CONFIG_WIFI_DISPLAY */
129
130 buf = wpabuf_alloc(1000 + extra);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700131 if (buf == NULL)
132 return NULL;
133
134 p2p_buf_add_public_action_hdr(buf, P2P_INVITATION_RESP,
135 dialog_token);
136
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,
142 reg_class, channel);
143 if (group_bssid)
144 p2p_buf_add_group_bssid(buf, group_bssid);
Irfan Sheriff069fa2c2012-09-24 23:59:04 -0700145 if (channels)
146 p2p_buf_add_channel_list(buf, p2p->cfg->country, channels);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700147 p2p_buf_update_ie_hdr(buf, len);
148
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700149#ifdef CONFIG_WIFI_DISPLAY
150 if (wfd_ie)
151 wpabuf_put_buf(buf, wfd_ie);
152#endif /* CONFIG_WIFI_DISPLAY */
153
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700154 return buf;
155}
156
157
158void p2p_process_invitation_req(struct p2p_data *p2p, const u8 *sa,
159 const u8 *data, size_t len, int rx_freq)
160{
161 struct p2p_device *dev;
162 struct p2p_message msg;
163 struct wpabuf *resp = NULL;
164 u8 status = P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
165 int freq;
166 int go = 0;
167 u8 group_bssid[ETH_ALEN], *bssid;
168 int op_freq = 0;
169 u8 reg_class = 0, channel = 0;
170 struct p2p_channels intersection, *channels = NULL;
171 int persistent;
172
173 os_memset(group_bssid, 0, sizeof(group_bssid));
174
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700175 p2p_dbg(p2p, "Received Invitation Request from " MACSTR " (freq=%d)",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700176 MAC2STR(sa), rx_freq);
177
178 if (p2p_parse(data, len, &msg))
179 return;
180
181 dev = p2p_get_device(p2p, sa);
182 if (dev == NULL || (dev->flags & P2P_DEV_PROBE_REQ_ONLY)) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700183 p2p_dbg(p2p, "Invitation Request from unknown peer " MACSTR,
184 MAC2STR(sa));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700185
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800186 if (p2p_add_device(p2p, sa, rx_freq, NULL, 0, data + 1, len - 1,
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800187 0)) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700188 p2p_dbg(p2p, "Invitation Request add device failed "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700189 MACSTR, MAC2STR(sa));
190 status = P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
191 goto fail;
192 }
193
194 dev = p2p_get_device(p2p, sa);
195 if (dev == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700196 p2p_dbg(p2p, "Reject Invitation Request from unknown peer "
197 MACSTR, MAC2STR(sa));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700198 status = P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
199 goto fail;
200 }
201 }
202
203 if (!msg.group_id || !msg.channel_list) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700204 p2p_dbg(p2p, "Mandatory attribute missing in Invitation Request from "
205 MACSTR, MAC2STR(sa));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700206 status = P2P_SC_FAIL_INVALID_PARAMS;
207 goto fail;
208 }
209
210 if (msg.invitation_flags)
211 persistent = *msg.invitation_flags & P2P_INVITATION_FLAGS_TYPE;
212 else {
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
216 * missing.
217 */
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700218 p2p_dbg(p2p, "Mandatory Invitation Flags attribute missing from Invitation Request");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700219 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) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700225 p2p_dbg(p2p, "No common channels found");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700226 status = P2P_SC_FAIL_NO_COMMON_CHANNELS;
227 goto fail;
228 }
229
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -0700230 p2p_channels_intersect(&p2p->cfg->channels, &dev->channels,
231 &intersection);
232
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700233 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,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800237 &go, group_bssid, &op_freq, persistent, &intersection,
238 msg.dev_password_id_present ? msg.dev_password_id : -1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700239 }
240
241 if (op_freq) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700242 p2p_dbg(p2p, "Invitation processing forced frequency %d MHz",
243 op_freq);
Dmitry Shmidt4b060592013-04-29 16:42:49 -0700244 if (p2p_freq_to_channel(op_freq, &reg_class, &channel) < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700245 p2p_dbg(p2p, "Unknown forced freq %d MHz from invitation_process()",
246 op_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700247 status = P2P_SC_FAIL_NO_COMMON_CHANNELS;
248 goto fail;
249 }
250
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700251 if (!p2p_channels_includes(&intersection, reg_class, channel))
252 {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700253 p2p_dbg(p2p, "forced freq %d MHz not in the supported channels interaction",
254 op_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700255 status = P2P_SC_FAIL_NO_COMMON_CHANNELS;
256 goto fail;
257 }
258
259 if (status == P2P_SC_SUCCESS)
260 channels = &intersection;
261 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700262 p2p_dbg(p2p, "No forced channel from invitation processing - figure out best one to use");
Dmitry Shmidt91c40cd2012-09-25 14:23:53 -0700263
Dmitry Shmidt91c40cd2012-09-25 14:23:53 -0700264 /* 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;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700267 p2p_dbg(p2p, "Own default op_class %d channel %d",
Dmitry Shmidt91c40cd2012-09-25 14:23:53 -0700268 p2p->op_reg_class, p2p->op_channel);
269
270 /* Use peer preference if specified and compatible */
271 if (msg.operating_channel) {
272 int req_freq;
273 req_freq = p2p_channel_to_freq(
Dmitry Shmidt91c40cd2012-09-25 14:23:53 -0700274 msg.operating_channel[3],
275 msg.operating_channel[4]);
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700276 p2p_dbg(p2p, "Peer operating channel preference: %d MHz",
Dmitry Shmidt91c40cd2012-09-25 14:23:53 -0700277 req_freq);
278 if (req_freq > 0 &&
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];
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700284 p2p_dbg(p2p, "Use peer preference op_class %d channel %d",
Dmitry Shmidt91c40cd2012-09-25 14:23:53 -0700285 p2p->op_reg_class, p2p->op_channel);
286 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700287 p2p_dbg(p2p, "Cannot use peer channel preference");
Dmitry Shmidt91c40cd2012-09-25 14:23:53 -0700288 }
289 }
290
291 if (!p2p_channels_includes(&intersection, p2p->op_reg_class,
292 p2p->op_channel)) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700293 p2p_dbg(p2p, "Initially selected channel (op_class %d channel %d) not in channel intersection - try to reselect",
Dmitry Shmidt91c40cd2012-09-25 14:23:53 -0700294 p2p->op_reg_class, p2p->op_channel);
295 p2p_reselect_channel(p2p, &intersection);
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700296 p2p_dbg(p2p, "Re-selection result: op_class %d channel %d",
Dmitry Shmidt91c40cd2012-09-25 14:23:53 -0700297 p2p->op_reg_class, p2p->op_channel);
298 if (!p2p_channels_includes(&intersection,
299 p2p->op_reg_class,
300 p2p->op_channel)) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700301 p2p_dbg(p2p, "Peer does not support selected operating channel (reg_class=%u channel=%u)",
Dmitry Shmidt91c40cd2012-09-25 14:23:53 -0700302 p2p->op_reg_class, p2p->op_channel);
303 status = P2P_SC_FAIL_NO_COMMON_CHANNELS;
304 goto fail;
305 }
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700306 } else if (!(dev->flags & P2P_DEV_FORCE_FREQ) &&
307 !p2p->cfg->cfg_op_channel) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700308 p2p_dbg(p2p, "Try to reselect channel selection with peer information received; previously selected op_class %u channel %u",
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700309 p2p->op_reg_class, p2p->op_channel);
310 p2p_reselect_channel(p2p, &intersection);
Dmitry Shmidt91c40cd2012-09-25 14:23:53 -0700311 }
312
Dmitry Shmidt4b060592013-04-29 16:42:49 -0700313 op_freq = p2p_channel_to_freq(p2p->op_reg_class,
Dmitry Shmidt91c40cd2012-09-25 14:23:53 -0700314 p2p->op_channel);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700315 if (op_freq < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700316 p2p_dbg(p2p, "Unknown operational channel (country=%c%c reg_class=%u channel=%u)",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700317 p2p->cfg->country[0], p2p->cfg->country[1],
Dmitry Shmidt91c40cd2012-09-25 14:23:53 -0700318 p2p->op_reg_class, p2p->op_channel);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700319 status = P2P_SC_FAIL_NO_COMMON_CHANNELS;
320 goto fail;
321 }
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700322 p2p_dbg(p2p, "Selected operating channel - %d MHz", op_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700323
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700324 if (status == P2P_SC_SUCCESS) {
Dmitry Shmidt91c40cd2012-09-25 14:23:53 -0700325 reg_class = p2p->op_reg_class;
326 channel = p2p->op_channel;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700327 channels = &intersection;
328 }
329 }
330
331fail:
332 if (go && status == P2P_SC_SUCCESS && !is_zero_ether_addr(group_bssid))
333 bssid = group_bssid;
334 else
335 bssid = NULL;
336 resp = p2p_build_invitation_resp(p2p, dev, msg.dialog_token, status,
337 bssid, reg_class, channel, channels);
338
339 if (resp == NULL)
340 goto out;
341
342 if (rx_freq > 0)
343 freq = rx_freq;
344 else
Dmitry Shmidt4b060592013-04-29 16:42:49 -0700345 freq = p2p_channel_to_freq(p2p->cfg->reg_class,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700346 p2p->cfg->channel);
347 if (freq < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700348 p2p_dbg(p2p, "Unknown regulatory class/channel");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700349 goto out;
350 }
351
352 /*
353 * Store copy of invitation data to be used when processing TX status
354 * callback for the Acton frame.
355 */
356 os_memcpy(p2p->inv_sa, sa, ETH_ALEN);
357 if (msg.group_bssid) {
358 os_memcpy(p2p->inv_group_bssid, msg.group_bssid, ETH_ALEN);
359 p2p->inv_group_bssid_ptr = p2p->inv_group_bssid;
360 } else
361 p2p->inv_group_bssid_ptr = NULL;
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -0800362 if (msg.group_id) {
363 if (msg.group_id_len - ETH_ALEN <= 32) {
364 os_memcpy(p2p->inv_ssid, msg.group_id + ETH_ALEN,
365 msg.group_id_len - ETH_ALEN);
366 p2p->inv_ssid_len = msg.group_id_len - ETH_ALEN;
367 }
368 os_memcpy(p2p->inv_go_dev_addr, msg.group_id, ETH_ALEN);
369 } else {
370 p2p->inv_ssid_len = 0;
371 os_memset(p2p->inv_go_dev_addr, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700372 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700373 p2p->inv_status = status;
374 p2p->inv_op_freq = op_freq;
375
376 p2p->pending_action_state = P2P_PENDING_INVITATION_RESPONSE;
377 if (p2p_send_action(p2p, freq, sa, p2p->cfg->dev_addr,
378 p2p->cfg->dev_addr,
379 wpabuf_head(resp), wpabuf_len(resp), 200) < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700380 p2p_dbg(p2p, "Failed to send Action frame");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700381 }
382
383out:
384 wpabuf_free(resp);
385 p2p_parse_free(&msg);
386}
387
388
389void p2p_process_invitation_resp(struct p2p_data *p2p, const u8 *sa,
390 const u8 *data, size_t len)
391{
392 struct p2p_device *dev;
393 struct p2p_message msg;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -0800394 struct p2p_channels intersection, *channels = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700395
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700396 p2p_dbg(p2p, "Received Invitation Response from " MACSTR,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700397 MAC2STR(sa));
398
399 dev = p2p_get_device(p2p, sa);
400 if (dev == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700401 p2p_dbg(p2p, "Ignore Invitation Response from unknown peer "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700402 MACSTR, MAC2STR(sa));
403 return;
404 }
405
406 if (dev != p2p->invite_peer) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700407 p2p_dbg(p2p, "Ignore unexpected Invitation Response from peer "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700408 MACSTR, MAC2STR(sa));
409 return;
410 }
411
412 if (p2p_parse(data, len, &msg))
413 return;
414
415 if (!msg.status) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700416 p2p_dbg(p2p, "Mandatory Status attribute missing in Invitation Response from "
417 MACSTR, MAC2STR(sa));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700418 p2p_parse_free(&msg);
419 return;
420 }
421
Dmitry Shmidt051af732013-10-22 13:52:46 -0700422 if (!msg.channel_list && *msg.status == P2P_SC_SUCCESS) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700423 p2p_dbg(p2p, "Mandatory Channel List attribute missing in Invitation Response from "
424 MACSTR, MAC2STR(sa));
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -0800425#ifdef CONFIG_P2P_STRICT
426 p2p_parse_free(&msg);
427 return;
428#endif /* CONFIG_P2P_STRICT */
429 /* Try to survive without peer channel list */
430 channels = &p2p->channels;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700431 } else if (!msg.channel_list) {
432 /* Non-success cases are not required to include Channel List */
433 channels = &p2p->channels;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -0800434 } else if (p2p_peer_channels_check(p2p, &p2p->channels, dev,
435 msg.channel_list,
436 msg.channel_list_len) < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700437 p2p_dbg(p2p, "No common channels found");
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -0800438 p2p_parse_free(&msg);
439 return;
440 } else {
441 p2p_channels_intersect(&p2p->channels, &dev->channels,
442 &intersection);
443 channels = &intersection;
444 }
445
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -0800446 if (p2p->cfg->invitation_result) {
447 int freq = p2p_channel_to_freq(p2p->op_reg_class,
448 p2p->op_channel);
449 if (freq < 0)
450 freq = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700451 p2p->cfg->invitation_result(p2p->cfg->cb_ctx, *msg.status,
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -0800452 msg.group_bssid, channels, sa,
453 freq);
454 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700455
456 p2p_parse_free(&msg);
457
458 p2p_clear_timeout(p2p);
459 p2p_set_state(p2p, P2P_IDLE);
460 p2p->invite_peer = NULL;
461}
462
463
464int p2p_invite_send(struct p2p_data *p2p, struct p2p_device *dev,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800465 const u8 *go_dev_addr, int dev_pw_id)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700466{
467 struct wpabuf *req;
468 int freq;
469
470 freq = dev->listen_freq > 0 ? dev->listen_freq : dev->oper_freq;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800471 if (freq <= 0)
472 freq = dev->oob_go_neg_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700473 if (freq <= 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700474 p2p_dbg(p2p, "No Listen/Operating frequency known for the peer "
475 MACSTR " to send Invitation Request",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700476 MAC2STR(dev->info.p2p_device_addr));
477 return -1;
478 }
479
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800480 req = p2p_build_invitation_req(p2p, dev, go_dev_addr, dev_pw_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700481 if (req == NULL)
482 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700483 if (p2p->state != P2P_IDLE)
484 p2p_stop_listen_for_freq(p2p, freq);
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700485 p2p_dbg(p2p, "Sending Invitation Request");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700486 p2p_set_state(p2p, P2P_INVITE);
487 p2p->pending_action_state = P2P_PENDING_INVITATION_REQUEST;
488 p2p->invite_peer = dev;
489 dev->invitation_reqs++;
490 if (p2p_send_action(p2p, freq, dev->info.p2p_device_addr,
491 p2p->cfg->dev_addr, dev->info.p2p_device_addr,
Dmitry Shmidt96571392013-10-14 12:54:46 -0700492 wpabuf_head(req), wpabuf_len(req), 500) < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700493 p2p_dbg(p2p, "Failed to send Action frame");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700494 /* Use P2P find to recover and retry */
495 p2p_set_timeout(p2p, 0, 0);
Dmitry Shmidt3c479372014-02-04 10:50:36 -0800496 } else {
497 dev->flags |= P2P_DEV_WAIT_INV_REQ_ACK;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700498 }
499
500 wpabuf_free(req);
501
502 return 0;
503}
504
505
506void p2p_invitation_req_cb(struct p2p_data *p2p, int success)
507{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700508 p2p_dbg(p2p, "Invitation Request TX callback: success=%d", success);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700509
510 if (p2p->invite_peer == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700511 p2p_dbg(p2p, "No pending Invite");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700512 return;
513 }
514
Dmitry Shmidt3c479372014-02-04 10:50:36 -0800515 if (success)
516 p2p->invite_peer->flags &= ~P2P_DEV_WAIT_INV_REQ_ACK;
517
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700518 /*
519 * Use P2P find, if needed, to find the other device from its listen
520 * channel.
521 */
522 p2p_set_state(p2p, P2P_INVITE);
Dmitry Shmidt96571392013-10-14 12:54:46 -0700523 p2p_set_timeout(p2p, 0, success ? 500000 : 100000);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700524}
525
526
527void p2p_invitation_resp_cb(struct p2p_data *p2p, int success)
528{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700529 p2p_dbg(p2p, "Invitation Response TX callback: success=%d", success);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700530 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
531
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -0700532 if (!success)
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700533 p2p_dbg(p2p, "Assume Invitation Response was actually received by the peer even though Ack was not reported");
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -0700534
535 if (p2p->cfg->invitation_received) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700536 p2p->cfg->invitation_received(p2p->cfg->cb_ctx,
537 p2p->inv_sa,
538 p2p->inv_group_bssid_ptr,
539 p2p->inv_ssid, p2p->inv_ssid_len,
540 p2p->inv_go_dev_addr,
541 p2p->inv_status,
542 p2p->inv_op_freq);
543 }
544}
545
546
547int p2p_invite(struct p2p_data *p2p, const u8 *peer, enum p2p_invite_role role,
548 const u8 *bssid, const u8 *ssid, size_t ssid_len,
549 unsigned int force_freq, const u8 *go_dev_addr,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800550 int persistent_group, unsigned int pref_freq, int dev_pw_id)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700551{
552 struct p2p_device *dev;
553
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700554 p2p_dbg(p2p, "Request to invite peer " MACSTR " role=%d persistent=%d "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700555 "force_freq=%u",
556 MAC2STR(peer), role, persistent_group, force_freq);
557 if (bssid)
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700558 p2p_dbg(p2p, "Invitation for BSSID " MACSTR, MAC2STR(bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700559 if (go_dev_addr) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700560 p2p_dbg(p2p, "Invitation for GO Device Address " MACSTR,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700561 MAC2STR(go_dev_addr));
562 os_memcpy(p2p->invite_go_dev_addr_buf, go_dev_addr, ETH_ALEN);
563 p2p->invite_go_dev_addr = p2p->invite_go_dev_addr_buf;
564 } else
565 p2p->invite_go_dev_addr = NULL;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700566 wpa_hexdump_ascii(MSG_DEBUG, "Invitation for SSID",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700567 ssid, ssid_len);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800568 if (dev_pw_id >= 0) {
569 p2p_dbg(p2p, "Invitation to use Device Password ID %d",
570 dev_pw_id);
571 }
572 p2p->invite_dev_pw_id = dev_pw_id;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700573
574 dev = p2p_get_device(p2p, peer);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800575 if (dev == NULL || (dev->listen_freq <= 0 && dev->oper_freq <= 0 &&
576 dev->oob_go_neg_freq <= 0)) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700577 p2p_dbg(p2p, "Cannot invite unknown P2P Device " MACSTR,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700578 MAC2STR(peer));
579 return -1;
580 }
581
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700582 if (p2p_prepare_channel(p2p, dev, force_freq, pref_freq,
583 role != P2P_INVITE_ROLE_CLIENT) < 0)
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -0800584 return -1;
585
586 if (persistent_group && role == P2P_INVITE_ROLE_CLIENT && !force_freq &&
587 !pref_freq)
588 dev->flags |= P2P_DEV_NO_PREF_CHAN;
589 else
590 dev->flags &= ~P2P_DEV_NO_PREF_CHAN;
591
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700592 if (dev->flags & P2P_DEV_GROUP_CLIENT_ONLY) {
593 if (!(dev->info.dev_capab &
594 P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY)) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700595 p2p_dbg(p2p, "Cannot invite a P2P Device " MACSTR
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700596 " that is in a group and is not discoverable",
597 MAC2STR(peer));
598 }
599 /* TODO: use device discoverability request through GO */
600 }
601
602 dev->invitation_reqs = 0;
603
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700604 if (p2p->state != P2P_IDLE)
605 p2p_stop_find(p2p);
606
607 p2p->inv_role = role;
608 p2p->inv_bssid_set = bssid != NULL;
609 if (bssid)
610 os_memcpy(p2p->inv_bssid, bssid, ETH_ALEN);
611 os_memcpy(p2p->inv_ssid, ssid, ssid_len);
612 p2p->inv_ssid_len = ssid_len;
613 p2p->inv_persistent = persistent_group;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800614 return p2p_invite_send(p2p, dev, go_dev_addr, dev_pw_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700615}