blob: 558c6dd0c58faf4a1319a6e59a0d4eb08f6b2ab5 [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"
Dmitry Shmidt2e67f062014-07-16 09:55:28 -070013#include "common/wpa_ctrl.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070014#include "p2p_i.h"
15#include "p2p.h"
16
17
18static struct wpabuf * p2p_build_invitation_req(struct p2p_data *p2p,
19 struct p2p_device *peer,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -080020 const u8 *go_dev_addr,
21 int dev_pw_id)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070022{
23 struct wpabuf *buf;
24 u8 *len;
25 const u8 *dev_addr;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070026 size_t extra = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070027
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070028#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) {
31 size_t i;
32 for (i = 0; i < p2p->num_groups; i++) {
33 struct p2p_group *g = p2p->groups[i];
34 struct wpabuf *ie;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -070035 if (os_memcmp(p2p_group_get_interface_addr(g),
36 p2p->inv_bssid, ETH_ALEN) != 0)
37 continue;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070038 ie = p2p_group_get_wfd_ie(g);
39 if (ie) {
40 wfd_ie = ie;
41 break;
42 }
43 }
44 }
45 if (wfd_ie)
46 extra = wpabuf_len(wfd_ie);
47#endif /* CONFIG_WIFI_DISPLAY */
48
Dmitry Shmidt2e67f062014-07-16 09:55:28 -070049 if (p2p->vendor_elem && p2p->vendor_elem[VENDOR_ELEM_P2P_INV_REQ])
50 extra += wpabuf_len(p2p->vendor_elem[VENDOR_ELEM_P2P_INV_REQ]);
51
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070052 buf = wpabuf_alloc(1000 + extra);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070053 if (buf == NULL)
54 return NULL;
55
56 peer->dialog_token++;
57 if (peer->dialog_token == 0)
58 peer->dialog_token = 1;
59 p2p_buf_add_public_action_hdr(buf, P2P_INVITATION_REQ,
60 peer->dialog_token);
61
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);
65 else
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070066 p2p_buf_add_config_timeout(buf, p2p->go_timeout,
67 p2p->client_timeout);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070068 p2p_buf_add_invitation_flags(buf, p2p->inv_persistent ?
69 P2P_INVITATION_FLAGS_TYPE : 0);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -080070 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,
73 p2p->op_reg_class,
74 p2p->op_channel);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070075 if (p2p->inv_bssid_set)
76 p2p_buf_add_group_bssid(buf, p2p->inv_bssid);
Irfan Sheriff069fa2c2012-09-24 23:59:04 -070077 p2p_buf_add_channel_list(buf, p2p->cfg->country, &p2p->channels);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070078 if (go_dev_addr)
79 dev_addr = go_dev_addr;
80 else if (p2p->inv_role == P2P_INVITE_ROLE_CLIENT)
81 dev_addr = peer->info.p2p_device_addr;
82 else
83 dev_addr = p2p->cfg->dev_addr;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070084 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);
87
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070088#ifdef CONFIG_WIFI_DISPLAY
89 if (wfd_ie)
90 wpabuf_put_buf(buf, wfd_ie);
91#endif /* CONFIG_WIFI_DISPLAY */
92
Dmitry Shmidt2e67f062014-07-16 09:55:28 -070093 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]);
95
Dmitry Shmidtcf32e602014-01-28 10:57:39 -080096 if (dev_pw_id >= 0) {
97 /* WSC IE in Invitation Request for NFC static handover */
98 p2p_build_wps_ie(p2p, buf, dev_pw_id, 0);
99 }
100
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700101 return buf;
102}
103
104
105static 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)
111{
112 struct wpabuf *buf;
113 u8 *len;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700114 size_t extra = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700115
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700116#ifdef CONFIG_WIFI_DISPLAY
117 struct wpabuf *wfd_ie = p2p->wfd_ie_invitation;
118 if (wfd_ie && group_bssid) {
119 size_t i;
120 for (i = 0; i < p2p->num_groups; i++) {
121 struct p2p_group *g = p2p->groups[i];
122 struct wpabuf *ie;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700123 if (os_memcmp(p2p_group_get_interface_addr(g),
124 group_bssid, ETH_ALEN) != 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700125 continue;
126 ie = p2p_group_get_wfd_ie(g);
127 if (ie) {
128 wfd_ie = ie;
129 break;
130 }
131 }
132 }
133 if (wfd_ie)
134 extra = wpabuf_len(wfd_ie);
135#endif /* CONFIG_WIFI_DISPLAY */
136
137 buf = wpabuf_alloc(1000 + extra);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700138 if (buf == NULL)
139 return NULL;
140
141 p2p_buf_add_public_action_hdr(buf, P2P_INVITATION_RESP,
142 dialog_token);
143
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,
149 reg_class, channel);
150 if (group_bssid)
151 p2p_buf_add_group_bssid(buf, group_bssid);
Irfan Sheriff069fa2c2012-09-24 23:59:04 -0700152 if (channels)
153 p2p_buf_add_channel_list(buf, p2p->cfg->country, channels);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700154 p2p_buf_update_ie_hdr(buf, len);
155
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700156#ifdef CONFIG_WIFI_DISPLAY
157 if (wfd_ie)
158 wpabuf_put_buf(buf, wfd_ie);
159#endif /* CONFIG_WIFI_DISPLAY */
160
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700161 return buf;
162}
163
164
165void p2p_process_invitation_req(struct p2p_data *p2p, const u8 *sa,
166 const u8 *data, size_t len, int rx_freq)
167{
168 struct p2p_device *dev;
169 struct p2p_message msg;
170 struct wpabuf *resp = NULL;
171 u8 status = P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
172 int freq;
173 int go = 0;
174 u8 group_bssid[ETH_ALEN], *bssid;
175 int op_freq = 0;
176 u8 reg_class = 0, channel = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800177 struct p2p_channels all_channels, intersection, *channels = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700178 int persistent;
179
180 os_memset(group_bssid, 0, sizeof(group_bssid));
181
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700182 p2p_dbg(p2p, "Received Invitation Request from " MACSTR " (freq=%d)",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700183 MAC2STR(sa), rx_freq);
184
185 if (p2p_parse(data, len, &msg))
186 return;
187
188 dev = p2p_get_device(p2p, sa);
189 if (dev == NULL || (dev->flags & P2P_DEV_PROBE_REQ_ONLY)) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700190 p2p_dbg(p2p, "Invitation Request from unknown peer " MACSTR,
191 MAC2STR(sa));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700192
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800193 if (p2p_add_device(p2p, sa, rx_freq, NULL, 0, data + 1, len - 1,
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800194 0)) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700195 p2p_dbg(p2p, "Invitation Request add device failed "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700196 MACSTR, MAC2STR(sa));
197 status = P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
198 goto fail;
199 }
200
201 dev = p2p_get_device(p2p, sa);
202 if (dev == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700203 p2p_dbg(p2p, "Reject Invitation Request from unknown peer "
204 MACSTR, MAC2STR(sa));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700205 status = P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
206 goto fail;
207 }
208 }
209
210 if (!msg.group_id || !msg.channel_list) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700211 p2p_dbg(p2p, "Mandatory attribute missing in Invitation Request from "
212 MACSTR, MAC2STR(sa));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700213 status = P2P_SC_FAIL_INVALID_PARAMS;
214 goto fail;
215 }
216
217 if (msg.invitation_flags)
218 persistent = *msg.invitation_flags & P2P_INVITATION_FLAGS_TYPE;
219 else {
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
223 * missing.
224 */
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700225 p2p_dbg(p2p, "Mandatory Invitation Flags attribute missing from Invitation Request");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700226 persistent = 1;
227 }
228
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800229 p2p_channels_union(&p2p->cfg->channels, &p2p->cfg->cli_channels,
230 &all_channels);
231
232 if (p2p_peer_channels_check(p2p, &all_channels, dev,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700233 msg.channel_list, msg.channel_list_len) <
234 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700235 p2p_dbg(p2p, "No common channels found");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700236 status = P2P_SC_FAIL_NO_COMMON_CHANNELS;
237 goto fail;
238 }
239
Dmitry Shmidt71757432014-06-02 13:50:35 -0700240 p2p_channels_dump(p2p, "own channels", &p2p->cfg->channels);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800241 p2p_channels_dump(p2p, "own client channels", &all_channels);
Dmitry Shmidt71757432014-06-02 13:50:35 -0700242 p2p_channels_dump(p2p, "peer channels", &dev->channels);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800243 p2p_channels_intersect(&all_channels, &dev->channels,
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -0700244 &intersection);
Dmitry Shmidt71757432014-06-02 13:50:35 -0700245 p2p_channels_dump(p2p, "intersection", &intersection);
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -0700246
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700247 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,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800251 &go, group_bssid, &op_freq, persistent, &intersection,
252 msg.dev_password_id_present ? msg.dev_password_id : -1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700253 }
254
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800255 if (go) {
256 p2p_channels_intersect(&p2p->cfg->channels, &dev->channels,
257 &intersection);
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;
262 goto fail;
263 }
264 }
265
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700266 if (op_freq) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700267 p2p_dbg(p2p, "Invitation processing forced frequency %d MHz",
268 op_freq);
Dmitry Shmidt4b060592013-04-29 16:42:49 -0700269 if (p2p_freq_to_channel(op_freq, &reg_class, &channel) < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700270 p2p_dbg(p2p, "Unknown forced freq %d MHz from invitation_process()",
271 op_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700272 status = P2P_SC_FAIL_NO_COMMON_CHANNELS;
273 goto fail;
274 }
275
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700276 if (!p2p_channels_includes(&intersection, reg_class, channel))
277 {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700278 p2p_dbg(p2p, "forced freq %d MHz not in the supported channels interaction",
279 op_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700280 status = P2P_SC_FAIL_NO_COMMON_CHANNELS;
281 goto fail;
282 }
283
284 if (status == P2P_SC_SUCCESS)
285 channels = &intersection;
286 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700287 p2p_dbg(p2p, "No forced channel from invitation processing - figure out best one to use");
Dmitry Shmidt91c40cd2012-09-25 14:23:53 -0700288
Dmitry Shmidt91c40cd2012-09-25 14:23:53 -0700289 /* 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;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700292 p2p_dbg(p2p, "Own default op_class %d channel %d",
Dmitry Shmidt91c40cd2012-09-25 14:23:53 -0700293 p2p->op_reg_class, p2p->op_channel);
294
295 /* Use peer preference if specified and compatible */
296 if (msg.operating_channel) {
297 int req_freq;
298 req_freq = p2p_channel_to_freq(
Dmitry Shmidt91c40cd2012-09-25 14:23:53 -0700299 msg.operating_channel[3],
300 msg.operating_channel[4]);
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700301 p2p_dbg(p2p, "Peer operating channel preference: %d MHz",
Dmitry Shmidt91c40cd2012-09-25 14:23:53 -0700302 req_freq);
303 if (req_freq > 0 &&
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];
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700309 p2p_dbg(p2p, "Use peer preference op_class %d channel %d",
Dmitry Shmidt91c40cd2012-09-25 14:23:53 -0700310 p2p->op_reg_class, p2p->op_channel);
311 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700312 p2p_dbg(p2p, "Cannot use peer channel preference");
Dmitry Shmidt91c40cd2012-09-25 14:23:53 -0700313 }
314 }
315
Dmitry Shmidt15907092014-03-25 10:42:57 -0700316 /* Reselect the channel only for the case of the GO */
317 if (go &&
318 !p2p_channels_includes(&intersection, p2p->op_reg_class,
Dmitry Shmidt91c40cd2012-09-25 14:23:53 -0700319 p2p->op_channel)) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700320 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 -0700321 p2p->op_reg_class, p2p->op_channel);
322 p2p_reselect_channel(p2p, &intersection);
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700323 p2p_dbg(p2p, "Re-selection result: op_class %d channel %d",
Dmitry Shmidt91c40cd2012-09-25 14:23:53 -0700324 p2p->op_reg_class, p2p->op_channel);
325 if (!p2p_channels_includes(&intersection,
326 p2p->op_reg_class,
327 p2p->op_channel)) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700328 p2p_dbg(p2p, "Peer does not support selected operating channel (reg_class=%u channel=%u)",
Dmitry Shmidt91c40cd2012-09-25 14:23:53 -0700329 p2p->op_reg_class, p2p->op_channel);
330 status = P2P_SC_FAIL_NO_COMMON_CHANNELS;
331 goto fail;
332 }
Dmitry Shmidt15907092014-03-25 10:42:57 -0700333 } else if (go && !(dev->flags & P2P_DEV_FORCE_FREQ) &&
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700334 !p2p->cfg->cfg_op_channel) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700335 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 -0700336 p2p->op_reg_class, p2p->op_channel);
337 p2p_reselect_channel(p2p, &intersection);
Dmitry Shmidt91c40cd2012-09-25 14:23:53 -0700338 }
339
Dmitry Shmidt4b060592013-04-29 16:42:49 -0700340 op_freq = p2p_channel_to_freq(p2p->op_reg_class,
Dmitry Shmidt91c40cd2012-09-25 14:23:53 -0700341 p2p->op_channel);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700342 if (op_freq < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700343 p2p_dbg(p2p, "Unknown operational channel (country=%c%c reg_class=%u channel=%u)",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700344 p2p->cfg->country[0], p2p->cfg->country[1],
Dmitry Shmidt91c40cd2012-09-25 14:23:53 -0700345 p2p->op_reg_class, p2p->op_channel);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700346 status = P2P_SC_FAIL_NO_COMMON_CHANNELS;
347 goto fail;
348 }
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700349 p2p_dbg(p2p, "Selected operating channel - %d MHz", op_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700350
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700351 if (status == P2P_SC_SUCCESS) {
Dmitry Shmidt91c40cd2012-09-25 14:23:53 -0700352 reg_class = p2p->op_reg_class;
353 channel = p2p->op_channel;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700354 channels = &intersection;
355 }
356 }
357
358fail:
359 if (go && status == P2P_SC_SUCCESS && !is_zero_ether_addr(group_bssid))
360 bssid = group_bssid;
361 else
362 bssid = NULL;
363 resp = p2p_build_invitation_resp(p2p, dev, msg.dialog_token, status,
364 bssid, reg_class, channel, channels);
365
366 if (resp == NULL)
367 goto out;
368
369 if (rx_freq > 0)
370 freq = rx_freq;
371 else
Dmitry Shmidt4b060592013-04-29 16:42:49 -0700372 freq = p2p_channel_to_freq(p2p->cfg->reg_class,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700373 p2p->cfg->channel);
374 if (freq < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700375 p2p_dbg(p2p, "Unknown regulatory class/channel");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700376 goto out;
377 }
378
379 /*
380 * Store copy of invitation data to be used when processing TX status
381 * callback for the Acton frame.
382 */
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;
387 } else
388 p2p->inv_group_bssid_ptr = NULL;
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -0800389 if (msg.group_id) {
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;
394 }
395 os_memcpy(p2p->inv_go_dev_addr, msg.group_id, ETH_ALEN);
396 } else {
397 p2p->inv_ssid_len = 0;
398 os_memset(p2p->inv_go_dev_addr, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700399 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700400 p2p->inv_status = status;
401 p2p->inv_op_freq = op_freq;
402
403 p2p->pending_action_state = P2P_PENDING_INVITATION_RESPONSE;
404 if (p2p_send_action(p2p, freq, sa, p2p->cfg->dev_addr,
405 p2p->cfg->dev_addr,
406 wpabuf_head(resp), wpabuf_len(resp), 200) < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700407 p2p_dbg(p2p, "Failed to send Action frame");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700408 }
409
410out:
411 wpabuf_free(resp);
412 p2p_parse_free(&msg);
413}
414
415
416void p2p_process_invitation_resp(struct p2p_data *p2p, const u8 *sa,
417 const u8 *data, size_t len)
418{
419 struct p2p_device *dev;
420 struct p2p_message msg;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -0800421 struct p2p_channels intersection, *channels = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700422
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700423 p2p_dbg(p2p, "Received Invitation Response from " MACSTR,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700424 MAC2STR(sa));
425
426 dev = p2p_get_device(p2p, sa);
427 if (dev == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700428 p2p_dbg(p2p, "Ignore Invitation Response from unknown peer "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700429 MACSTR, MAC2STR(sa));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800430 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700431 return;
432 }
433
434 if (dev != p2p->invite_peer) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700435 p2p_dbg(p2p, "Ignore unexpected Invitation Response from peer "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700436 MACSTR, MAC2STR(sa));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800437 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700438 return;
439 }
440
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800441 if (p2p_parse(data, len, &msg)) {
442 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700443 return;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800444 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700445
446 if (!msg.status) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700447 p2p_dbg(p2p, "Mandatory Status attribute missing in Invitation Response from "
448 MACSTR, MAC2STR(sa));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700449 p2p_parse_free(&msg);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800450 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700451 return;
452 }
453
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800454 /*
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.
463 */
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);
470 return;
471 }
472
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);
487 return;
488 }
489 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
490 p2p->retry_invite_req = 0;
491
Dmitry Shmidt051af732013-10-22 13:52:46 -0700492 if (!msg.channel_list && *msg.status == P2P_SC_SUCCESS) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700493 p2p_dbg(p2p, "Mandatory Channel List attribute missing in Invitation Response from "
494 MACSTR, MAC2STR(sa));
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -0800495#ifdef CONFIG_P2P_STRICT
496 p2p_parse_free(&msg);
497 return;
498#endif /* CONFIG_P2P_STRICT */
499 /* Try to survive without peer channel list */
500 channels = &p2p->channels;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700501 } else if (!msg.channel_list) {
502 /* Non-success cases are not required to include Channel List */
503 channels = &p2p->channels;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -0800504 } else if (p2p_peer_channels_check(p2p, &p2p->channels, dev,
505 msg.channel_list,
506 msg.channel_list_len) < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700507 p2p_dbg(p2p, "No common channels found");
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -0800508 p2p_parse_free(&msg);
509 return;
510 } else {
511 p2p_channels_intersect(&p2p->channels, &dev->channels,
512 &intersection);
513 channels = &intersection;
514 }
515
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -0800516 if (p2p->cfg->invitation_result) {
Dmitry Shmidt15907092014-03-25 10:42:57 -0700517 int peer_oper_freq = 0;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -0800518 int freq = p2p_channel_to_freq(p2p->op_reg_class,
519 p2p->op_channel);
520 if (freq < 0)
521 freq = 0;
Dmitry Shmidt15907092014-03-25 10:42:57 -0700522
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)
528 peer_oper_freq = 0;
529 }
530
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700531 p2p->cfg->invitation_result(p2p->cfg->cb_ctx, *msg.status,
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -0800532 msg.group_bssid, channels, sa,
Dmitry Shmidt15907092014-03-25 10:42:57 -0700533 freq, peer_oper_freq);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -0800534 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700535
536 p2p_parse_free(&msg);
537
538 p2p_clear_timeout(p2p);
539 p2p_set_state(p2p, P2P_IDLE);
540 p2p->invite_peer = NULL;
541}
542
543
544int p2p_invite_send(struct p2p_data *p2p, struct p2p_device *dev,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800545 const u8 *go_dev_addr, int dev_pw_id)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700546{
547 struct wpabuf *req;
548 int freq;
549
550 freq = dev->listen_freq > 0 ? dev->listen_freq : dev->oper_freq;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800551 if (freq <= 0)
552 freq = dev->oob_go_neg_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700553 if (freq <= 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700554 p2p_dbg(p2p, "No Listen/Operating frequency known for the peer "
555 MACSTR " to send Invitation Request",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700556 MAC2STR(dev->info.p2p_device_addr));
557 return -1;
558 }
559
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800560 req = p2p_build_invitation_req(p2p, dev, go_dev_addr, dev_pw_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700561 if (req == NULL)
562 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700563 if (p2p->state != P2P_IDLE)
564 p2p_stop_listen_for_freq(p2p, freq);
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700565 p2p_dbg(p2p, "Sending Invitation Request");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700566 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,
Dmitry Shmidt96571392013-10-14 12:54:46 -0700572 wpabuf_head(req), wpabuf_len(req), 500) < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700573 p2p_dbg(p2p, "Failed to send Action frame");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700574 /* Use P2P find to recover and retry */
575 p2p_set_timeout(p2p, 0, 0);
Dmitry Shmidt3c479372014-02-04 10:50:36 -0800576 } else {
577 dev->flags |= P2P_DEV_WAIT_INV_REQ_ACK;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700578 }
579
580 wpabuf_free(req);
581
582 return 0;
583}
584
585
586void p2p_invitation_req_cb(struct p2p_data *p2p, int success)
587{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700588 p2p_dbg(p2p, "Invitation Request TX callback: success=%d", success);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700589
590 if (p2p->invite_peer == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700591 p2p_dbg(p2p, "No pending Invite");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700592 return;
593 }
594
Dmitry Shmidt3c479372014-02-04 10:50:36 -0800595 if (success)
596 p2p->invite_peer->flags &= ~P2P_DEV_WAIT_INV_REQ_ACK;
597
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700598 /*
599 * Use P2P find, if needed, to find the other device from its listen
600 * channel.
601 */
602 p2p_set_state(p2p, P2P_INVITE);
Dmitry Shmidt96571392013-10-14 12:54:46 -0700603 p2p_set_timeout(p2p, 0, success ? 500000 : 100000);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700604}
605
606
607void p2p_invitation_resp_cb(struct p2p_data *p2p, int success)
608{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700609 p2p_dbg(p2p, "Invitation Response TX callback: success=%d", success);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700610 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
611
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -0700612 if (!success)
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700613 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 -0700614
615 if (p2p->cfg->invitation_received) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700616 p2p->cfg->invitation_received(p2p->cfg->cb_ctx,
617 p2p->inv_sa,
618 p2p->inv_group_bssid_ptr,
619 p2p->inv_ssid, p2p->inv_ssid_len,
620 p2p->inv_go_dev_addr,
621 p2p->inv_status,
622 p2p->inv_op_freq);
623 }
624}
625
626
627int 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,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800630 int persistent_group, unsigned int pref_freq, int dev_pw_id)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700631{
632 struct p2p_device *dev;
633
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700634 p2p_dbg(p2p, "Request to invite peer " MACSTR " role=%d persistent=%d "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700635 "force_freq=%u",
636 MAC2STR(peer), role, persistent_group, force_freq);
637 if (bssid)
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700638 p2p_dbg(p2p, "Invitation for BSSID " MACSTR, MAC2STR(bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700639 if (go_dev_addr) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700640 p2p_dbg(p2p, "Invitation for GO Device Address " MACSTR,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700641 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;
644 } else
645 p2p->invite_go_dev_addr = NULL;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700646 wpa_hexdump_ascii(MSG_DEBUG, "Invitation for SSID",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700647 ssid, ssid_len);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800648 if (dev_pw_id >= 0) {
649 p2p_dbg(p2p, "Invitation to use Device Password ID %d",
650 dev_pw_id);
651 }
652 p2p->invite_dev_pw_id = dev_pw_id;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800653 p2p->retry_invite_req = role == P2P_INVITE_ROLE_GO &&
654 persistent_group && !force_freq;
655 p2p->retry_invite_req_sent = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700656
657 dev = p2p_get_device(p2p, peer);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800658 if (dev == NULL || (dev->listen_freq <= 0 && dev->oper_freq <= 0 &&
659 dev->oob_go_neg_freq <= 0)) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700660 p2p_dbg(p2p, "Cannot invite unknown P2P Device " MACSTR,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700661 MAC2STR(peer));
662 return -1;
663 }
664
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700665 if (p2p_prepare_channel(p2p, dev, force_freq, pref_freq,
666 role != P2P_INVITE_ROLE_CLIENT) < 0)
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -0800667 return -1;
668
669 if (persistent_group && role == P2P_INVITE_ROLE_CLIENT && !force_freq &&
670 !pref_freq)
671 dev->flags |= P2P_DEV_NO_PREF_CHAN;
672 else
673 dev->flags &= ~P2P_DEV_NO_PREF_CHAN;
674
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700675 if (dev->flags & P2P_DEV_GROUP_CLIENT_ONLY) {
676 if (!(dev->info.dev_capab &
677 P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY)) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700678 p2p_dbg(p2p, "Cannot invite a P2P Device " MACSTR
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700679 " that is in a group and is not discoverable",
680 MAC2STR(peer));
681 }
682 /* TODO: use device discoverability request through GO */
683 }
684
685 dev->invitation_reqs = 0;
686
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700687 if (p2p->state != P2P_IDLE)
688 p2p_stop_find(p2p);
689
690 p2p->inv_role = role;
691 p2p->inv_bssid_set = bssid != NULL;
692 if (bssid)
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;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800697 return p2p_invite_send(p2p, dev, go_dev_addr, dev_pw_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700698}