Line data Source code
1 : /*
2 : * Wi-Fi Direct - P2P Invitation procedure
3 : * Copyright (c) 2010, Atheros Communications
4 : *
5 : * This software may be distributed under the terms of the BSD license.
6 : * See README for more details.
7 : */
8 :
9 : #include "includes.h"
10 :
11 : #include "common.h"
12 : #include "common/ieee802_11_defs.h"
13 : #include "common/wpa_ctrl.h"
14 : #include "p2p_i.h"
15 : #include "p2p.h"
16 :
17 :
18 48 : static struct wpabuf * p2p_build_invitation_req(struct p2p_data *p2p,
19 : struct p2p_device *peer,
20 : const u8 *go_dev_addr,
21 : int dev_pw_id)
22 : {
23 : struct wpabuf *buf;
24 : u8 *len;
25 : const u8 *dev_addr;
26 48 : size_t extra = 0;
27 :
28 : #ifdef CONFIG_WIFI_DISPLAY
29 48 : struct wpabuf *wfd_ie = p2p->wfd_ie_invitation;
30 48 : if (wfd_ie && p2p->inv_role == P2P_INVITE_ROLE_ACTIVE_GO) {
31 : size_t i;
32 1 : for (i = 0; i < p2p->num_groups; i++) {
33 1 : struct p2p_group *g = p2p->groups[i];
34 : struct wpabuf *ie;
35 1 : if (os_memcmp(p2p_group_get_interface_addr(g),
36 : p2p->inv_bssid, ETH_ALEN) != 0)
37 0 : continue;
38 1 : ie = p2p_group_get_wfd_ie(g);
39 1 : if (ie) {
40 1 : wfd_ie = ie;
41 1 : break;
42 : }
43 : }
44 : }
45 48 : if (wfd_ie)
46 4 : extra = wpabuf_len(wfd_ie);
47 : #endif /* CONFIG_WIFI_DISPLAY */
48 :
49 48 : if (p2p->vendor_elem && p2p->vendor_elem[VENDOR_ELEM_P2P_INV_REQ])
50 0 : extra += wpabuf_len(p2p->vendor_elem[VENDOR_ELEM_P2P_INV_REQ]);
51 :
52 48 : buf = wpabuf_alloc(1000 + extra);
53 48 : if (buf == NULL)
54 0 : return NULL;
55 :
56 48 : peer->dialog_token++;
57 48 : if (peer->dialog_token == 0)
58 0 : peer->dialog_token = 1;
59 48 : p2p_buf_add_public_action_hdr(buf, P2P_INVITATION_REQ,
60 48 : peer->dialog_token);
61 :
62 48 : len = p2p_buf_add_ie_hdr(buf);
63 48 : if (p2p->inv_role == P2P_INVITE_ROLE_ACTIVE_GO || !p2p->inv_persistent)
64 12 : p2p_buf_add_config_timeout(buf, 0, 0);
65 : else
66 36 : p2p_buf_add_config_timeout(buf, p2p->go_timeout,
67 36 : p2p->client_timeout);
68 48 : p2p_buf_add_invitation_flags(buf, p2p->inv_persistent ?
69 : P2P_INVITATION_FLAGS_TYPE : 0);
70 65 : if (p2p->inv_role != P2P_INVITE_ROLE_CLIENT ||
71 17 : !(peer->flags & P2P_DEV_NO_PREF_CHAN))
72 76 : p2p_buf_add_operating_channel(buf, p2p->cfg->country,
73 38 : p2p->op_reg_class,
74 38 : p2p->op_channel);
75 48 : if (p2p->inv_bssid_set)
76 34 : p2p_buf_add_group_bssid(buf, p2p->inv_bssid);
77 48 : p2p_buf_add_channel_list(buf, p2p->cfg->country, &p2p->channels);
78 48 : if (go_dev_addr)
79 12 : dev_addr = go_dev_addr;
80 36 : else if (p2p->inv_role == P2P_INVITE_ROLE_CLIENT)
81 14 : dev_addr = peer->info.p2p_device_addr;
82 : else
83 22 : dev_addr = p2p->cfg->dev_addr;
84 48 : p2p_buf_add_group_id(buf, dev_addr, p2p->inv_ssid, p2p->inv_ssid_len);
85 48 : p2p_buf_add_device_info(buf, p2p, peer);
86 48 : p2p_buf_update_ie_hdr(buf, len);
87 :
88 : #ifdef CONFIG_WIFI_DISPLAY
89 48 : if (wfd_ie)
90 4 : wpabuf_put_buf(buf, wfd_ie);
91 : #endif /* CONFIG_WIFI_DISPLAY */
92 :
93 48 : if (p2p->vendor_elem && p2p->vendor_elem[VENDOR_ELEM_P2P_INV_REQ])
94 0 : wpabuf_put_buf(buf, p2p->vendor_elem[VENDOR_ELEM_P2P_INV_REQ]);
95 :
96 48 : if (dev_pw_id >= 0) {
97 : /* WSC IE in Invitation Request for NFC static handover */
98 3 : p2p_build_wps_ie(p2p, buf, dev_pw_id, 0);
99 : }
100 :
101 48 : return buf;
102 : }
103 :
104 :
105 53 : 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)
111 : {
112 : struct wpabuf *buf;
113 : u8 *len;
114 53 : size_t extra = 0;
115 :
116 : #ifdef CONFIG_WIFI_DISPLAY
117 53 : struct wpabuf *wfd_ie = p2p->wfd_ie_invitation;
118 53 : if (wfd_ie && group_bssid) {
119 : size_t i;
120 2 : for (i = 0; i < p2p->num_groups; i++) {
121 1 : struct p2p_group *g = p2p->groups[i];
122 : struct wpabuf *ie;
123 1 : if (os_memcmp(p2p_group_get_interface_addr(g),
124 : group_bssid, ETH_ALEN) != 0)
125 0 : continue;
126 1 : ie = p2p_group_get_wfd_ie(g);
127 1 : if (ie) {
128 1 : wfd_ie = ie;
129 1 : break;
130 : }
131 : }
132 : }
133 53 : if (wfd_ie)
134 4 : extra = wpabuf_len(wfd_ie);
135 : #endif /* CONFIG_WIFI_DISPLAY */
136 :
137 53 : buf = wpabuf_alloc(1000 + extra);
138 53 : if (buf == NULL)
139 0 : return NULL;
140 :
141 53 : p2p_buf_add_public_action_hdr(buf, P2P_INVITATION_RESP,
142 : dialog_token);
143 :
144 53 : len = p2p_buf_add_ie_hdr(buf);
145 53 : p2p_buf_add_status(buf, status);
146 53 : p2p_buf_add_config_timeout(buf, 0, 0); /* FIX */
147 53 : if (reg_class && channel)
148 31 : p2p_buf_add_operating_channel(buf, p2p->cfg->country,
149 : reg_class, channel);
150 53 : if (group_bssid)
151 13 : p2p_buf_add_group_bssid(buf, group_bssid);
152 53 : if (channels)
153 29 : p2p_buf_add_channel_list(buf, p2p->cfg->country, channels);
154 53 : p2p_buf_update_ie_hdr(buf, len);
155 :
156 : #ifdef CONFIG_WIFI_DISPLAY
157 53 : if (wfd_ie)
158 4 : wpabuf_put_buf(buf, wfd_ie);
159 : #endif /* CONFIG_WIFI_DISPLAY */
160 :
161 53 : return buf;
162 : }
163 :
164 :
165 82 : void 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 82 : struct wpabuf *resp = NULL;
171 82 : u8 status = P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
172 : int freq;
173 82 : int go = 0;
174 : u8 group_bssid[ETH_ALEN], *bssid;
175 82 : int op_freq = 0;
176 82 : u8 reg_class = 0, channel = 0;
177 82 : struct p2p_channels all_channels, intersection, *channels = NULL;
178 : int persistent;
179 :
180 82 : os_memset(group_bssid, 0, sizeof(group_bssid));
181 :
182 492 : p2p_dbg(p2p, "Received Invitation Request from " MACSTR " (freq=%d)",
183 492 : MAC2STR(sa), rx_freq);
184 :
185 82 : if (p2p_parse(data, len, &msg))
186 111 : return;
187 :
188 53 : dev = p2p_get_device(p2p, sa);
189 53 : if (dev == NULL || (dev->flags & P2P_DEV_PROBE_REQ_ONLY)) {
190 60 : p2p_dbg(p2p, "Invitation Request from unknown peer " MACSTR,
191 60 : MAC2STR(sa));
192 :
193 10 : if (p2p_add_device(p2p, sa, rx_freq, NULL, 0, data + 1, len - 1,
194 : 0)) {
195 6 : p2p_dbg(p2p, "Invitation Request add device failed "
196 6 : MACSTR, MAC2STR(sa));
197 1 : status = P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
198 1 : goto fail;
199 : }
200 :
201 9 : dev = p2p_get_device(p2p, sa);
202 9 : if (dev == NULL) {
203 6 : p2p_dbg(p2p, "Reject Invitation Request from unknown peer "
204 6 : MACSTR, MAC2STR(sa));
205 1 : status = P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
206 1 : goto fail;
207 : }
208 : }
209 :
210 51 : if (!msg.group_id || !msg.channel_list) {
211 18 : p2p_dbg(p2p, "Mandatory attribute missing in Invitation Request from "
212 18 : MACSTR, MAC2STR(sa));
213 3 : status = P2P_SC_FAIL_INVALID_PARAMS;
214 3 : goto fail;
215 : }
216 :
217 48 : if (msg.invitation_flags)
218 47 : 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 : */
225 1 : p2p_dbg(p2p, "Mandatory Invitation Flags attribute missing from Invitation Request");
226 1 : persistent = 1;
227 : }
228 :
229 48 : p2p_channels_union(&p2p->cfg->channels, &p2p->cfg->cli_channels,
230 : &all_channels);
231 :
232 48 : if (p2p_peer_channels_check(p2p, &all_channels, dev,
233 48 : msg.channel_list, msg.channel_list_len) <
234 : 0) {
235 1 : p2p_dbg(p2p, "No common channels found");
236 1 : status = P2P_SC_FAIL_NO_COMMON_CHANNELS;
237 1 : goto fail;
238 : }
239 :
240 47 : p2p_channels_dump(p2p, "own channels", &p2p->cfg->channels);
241 47 : p2p_channels_dump(p2p, "own client channels", &all_channels);
242 47 : p2p_channels_dump(p2p, "peer channels", &dev->channels);
243 47 : p2p_channels_intersect(&all_channels, &dev->channels,
244 : &intersection);
245 47 : p2p_channels_dump(p2p, "intersection", &intersection);
246 :
247 47 : if (p2p->cfg->invitation_process) {
248 238 : status = p2p->cfg->invitation_process(
249 47 : p2p->cfg->cb_ctx, sa, msg.group_bssid, msg.group_id,
250 94 : msg.group_id + ETH_ALEN, msg.group_id_len - ETH_ALEN,
251 : &go, group_bssid, &op_freq, persistent, &intersection,
252 50 : msg.dev_password_id_present ? msg.dev_password_id : -1);
253 : }
254 :
255 47 : if (go) {
256 15 : p2p_channels_intersect(&p2p->cfg->channels, &dev->channels,
257 : &intersection);
258 15 : p2p_channels_dump(p2p, "intersection(GO)", &intersection);
259 15 : if (intersection.reg_classes == 0) {
260 0 : p2p_dbg(p2p, "No common channels found (GO)");
261 0 : status = P2P_SC_FAIL_NO_COMMON_CHANNELS;
262 0 : goto fail;
263 : }
264 : }
265 :
266 47 : if (op_freq) {
267 6 : p2p_dbg(p2p, "Invitation processing forced frequency %d MHz",
268 : op_freq);
269 6 : if (p2p_freq_to_channel(op_freq, ®_class, &channel) < 0) {
270 0 : p2p_dbg(p2p, "Unknown forced freq %d MHz from invitation_process()",
271 : op_freq);
272 0 : status = P2P_SC_FAIL_NO_COMMON_CHANNELS;
273 0 : goto fail;
274 : }
275 :
276 6 : if (!p2p_channels_includes(&intersection, reg_class, channel))
277 : {
278 2 : p2p_dbg(p2p, "forced freq %d MHz not in the supported channels interaction",
279 : op_freq);
280 2 : status = P2P_SC_FAIL_NO_COMMON_CHANNELS;
281 2 : goto fail;
282 : }
283 :
284 4 : if (status == P2P_SC_SUCCESS)
285 4 : channels = &intersection;
286 : } else {
287 41 : p2p_dbg(p2p, "No forced channel from invitation processing - figure out best one to use");
288 :
289 : /* Default to own configuration as a starting point */
290 41 : p2p->op_reg_class = p2p->cfg->op_reg_class;
291 41 : p2p->op_channel = p2p->cfg->op_channel;
292 82 : p2p_dbg(p2p, "Own default op_class %d channel %d",
293 82 : p2p->op_reg_class, p2p->op_channel);
294 :
295 : /* Use peer preference if specified and compatible */
296 41 : if (msg.operating_channel) {
297 : int req_freq;
298 66 : req_freq = p2p_channel_to_freq(
299 33 : msg.operating_channel[3],
300 33 : msg.operating_channel[4]);
301 33 : p2p_dbg(p2p, "Peer operating channel preference: %d MHz",
302 : req_freq);
303 65 : if (req_freq > 0 &&
304 64 : p2p_channels_includes(&intersection,
305 32 : msg.operating_channel[3],
306 32 : msg.operating_channel[4])) {
307 32 : p2p->op_reg_class = msg.operating_channel[3];
308 32 : p2p->op_channel = msg.operating_channel[4];
309 64 : p2p_dbg(p2p, "Use peer preference op_class %d channel %d",
310 64 : p2p->op_reg_class, p2p->op_channel);
311 : } else {
312 1 : p2p_dbg(p2p, "Cannot use peer channel preference");
313 : }
314 : }
315 :
316 : /* Reselect the channel only for the case of the GO */
317 50 : if (go &&
318 9 : !p2p_channels_includes(&intersection, p2p->op_reg_class,
319 9 : p2p->op_channel)) {
320 0 : p2p_dbg(p2p, "Initially selected channel (op_class %d channel %d) not in channel intersection - try to reselect",
321 0 : p2p->op_reg_class, p2p->op_channel);
322 0 : p2p_reselect_channel(p2p, &intersection);
323 0 : p2p_dbg(p2p, "Re-selection result: op_class %d channel %d",
324 0 : p2p->op_reg_class, p2p->op_channel);
325 0 : if (!p2p_channels_includes(&intersection,
326 0 : p2p->op_reg_class,
327 0 : p2p->op_channel)) {
328 0 : p2p_dbg(p2p, "Peer does not support selected operating channel (reg_class=%u channel=%u)",
329 0 : p2p->op_reg_class, p2p->op_channel);
330 0 : status = P2P_SC_FAIL_NO_COMMON_CHANNELS;
331 0 : goto fail;
332 : }
333 49 : } else if (go && !(dev->flags & P2P_DEV_FORCE_FREQ) &&
334 8 : !p2p->cfg->cfg_op_channel) {
335 16 : p2p_dbg(p2p, "Try to reselect channel selection with peer information received; previously selected op_class %u channel %u",
336 16 : p2p->op_reg_class, p2p->op_channel);
337 8 : p2p_reselect_channel(p2p, &intersection);
338 : }
339 :
340 41 : op_freq = p2p_channel_to_freq(p2p->op_reg_class,
341 41 : p2p->op_channel);
342 41 : if (op_freq < 0) {
343 0 : p2p_dbg(p2p, "Unknown operational channel (country=%c%c reg_class=%u channel=%u)",
344 0 : p2p->cfg->country[0], p2p->cfg->country[1],
345 0 : p2p->op_reg_class, p2p->op_channel);
346 0 : status = P2P_SC_FAIL_NO_COMMON_CHANNELS;
347 0 : goto fail;
348 : }
349 41 : p2p_dbg(p2p, "Selected operating channel - %d MHz", op_freq);
350 :
351 41 : if (status == P2P_SC_SUCCESS) {
352 25 : reg_class = p2p->op_reg_class;
353 25 : channel = p2p->op_channel;
354 25 : channels = &intersection;
355 : }
356 : }
357 :
358 : fail:
359 53 : if (go && status == P2P_SC_SUCCESS && !is_zero_ether_addr(group_bssid))
360 13 : bssid = group_bssid;
361 : else
362 40 : bssid = NULL;
363 53 : resp = p2p_build_invitation_resp(p2p, dev, msg.dialog_token, status,
364 : bssid, reg_class, channel, channels);
365 :
366 53 : if (resp == NULL)
367 0 : goto out;
368 :
369 53 : if (rx_freq > 0)
370 53 : freq = rx_freq;
371 : else
372 0 : freq = p2p_channel_to_freq(p2p->cfg->reg_class,
373 0 : p2p->cfg->channel);
374 53 : if (freq < 0) {
375 0 : p2p_dbg(p2p, "Unknown regulatory class/channel");
376 0 : 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 53 : os_memcpy(p2p->inv_sa, sa, ETH_ALEN);
384 53 : if (msg.group_bssid) {
385 35 : os_memcpy(p2p->inv_group_bssid, msg.group_bssid, ETH_ALEN);
386 35 : p2p->inv_group_bssid_ptr = p2p->inv_group_bssid;
387 : } else
388 18 : p2p->inv_group_bssid_ptr = NULL;
389 53 : if (msg.group_id) {
390 50 : if (msg.group_id_len - ETH_ALEN <= 32) {
391 50 : os_memcpy(p2p->inv_ssid, msg.group_id + ETH_ALEN,
392 : msg.group_id_len - ETH_ALEN);
393 50 : p2p->inv_ssid_len = msg.group_id_len - ETH_ALEN;
394 : }
395 50 : os_memcpy(p2p->inv_go_dev_addr, msg.group_id, ETH_ALEN);
396 : } else {
397 3 : p2p->inv_ssid_len = 0;
398 3 : os_memset(p2p->inv_go_dev_addr, 0, ETH_ALEN);
399 : }
400 53 : p2p->inv_status = status;
401 53 : p2p->inv_op_freq = op_freq;
402 :
403 53 : p2p->pending_action_state = P2P_PENDING_INVITATION_RESPONSE;
404 106 : if (p2p_send_action(p2p, freq, sa, p2p->cfg->dev_addr,
405 53 : p2p->cfg->dev_addr,
406 53 : wpabuf_head(resp), wpabuf_len(resp), 200) < 0) {
407 0 : p2p_dbg(p2p, "Failed to send Action frame");
408 : }
409 :
410 : out:
411 53 : wpabuf_free(resp);
412 53 : p2p_parse_free(&msg);
413 : }
414 :
415 :
416 50 : void 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;
421 50 : struct p2p_channels intersection, *channels = NULL;
422 :
423 300 : p2p_dbg(p2p, "Received Invitation Response from " MACSTR,
424 300 : MAC2STR(sa));
425 :
426 50 : dev = p2p_get_device(p2p, sa);
427 50 : if (dev == NULL) {
428 6 : p2p_dbg(p2p, "Ignore Invitation Response from unknown peer "
429 6 : MACSTR, MAC2STR(sa));
430 1 : p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
431 1 : return;
432 : }
433 :
434 49 : if (dev != p2p->invite_peer) {
435 6 : p2p_dbg(p2p, "Ignore unexpected Invitation Response from peer "
436 6 : MACSTR, MAC2STR(sa));
437 1 : p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
438 1 : return;
439 : }
440 :
441 48 : if (p2p_parse(data, len, &msg)) {
442 1 : p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
443 1 : return;
444 : }
445 :
446 47 : if (!msg.status) {
447 6 : p2p_dbg(p2p, "Mandatory Status attribute missing in Invitation Response from "
448 6 : MACSTR, MAC2STR(sa));
449 1 : p2p_parse_free(&msg);
450 1 : p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
451 1 : return;
452 : }
453 :
454 : /*
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 46 : if (*msg.status == P2P_SC_FAIL_NO_COMMON_CHANNELS &&
465 1 : p2p->retry_invite_req_sent &&
466 1 : msg.dialog_token != dev->dialog_token) {
467 2 : p2p_dbg(p2p, "Unexpected Dialog Token %u (expected %u)",
468 2 : msg.dialog_token, dev->dialog_token);
469 1 : p2p_parse_free(&msg);
470 1 : return;
471 : }
472 :
473 45 : if (*msg.status == P2P_SC_FAIL_NO_COMMON_CHANNELS &&
474 2 : p2p->retry_invite_req &&
475 2 : p2p_channel_random_social(&p2p->cfg->channels, &p2p->op_reg_class,
476 : &p2p->op_channel) == 0) {
477 2 : p2p->retry_invite_req = 0;
478 2 : p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
479 2 : p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
480 2 : p2p_set_state(p2p, P2P_INVITE);
481 4 : p2p_dbg(p2p, "Resend Invitation Request setting op_class %u channel %u as operating channel",
482 4 : p2p->op_reg_class, p2p->op_channel);
483 2 : p2p->retry_invite_req_sent = 1;
484 2 : p2p_invite_send(p2p, p2p->invite_peer, p2p->invite_go_dev_addr,
485 : p2p->invite_dev_pw_id);
486 2 : p2p_parse_free(&msg);
487 2 : return;
488 : }
489 43 : p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
490 43 : p2p->retry_invite_req = 0;
491 :
492 43 : if (!msg.channel_list && *msg.status == P2P_SC_SUCCESS) {
493 6 : p2p_dbg(p2p, "Mandatory Channel List attribute missing in Invitation Response from "
494 6 : MACSTR, MAC2STR(sa));
495 : #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 1 : channels = &p2p->channels;
501 42 : } else if (!msg.channel_list) {
502 : /* Non-success cases are not required to include Channel List */
503 11 : channels = &p2p->channels;
504 31 : } else if (p2p_peer_channels_check(p2p, &p2p->channels, dev,
505 : msg.channel_list,
506 31 : msg.channel_list_len) < 0) {
507 2 : p2p_dbg(p2p, "No common channels found");
508 2 : p2p_parse_free(&msg);
509 2 : return;
510 : } else {
511 29 : p2p_channels_intersect(&p2p->channels, &dev->channels,
512 : &intersection);
513 29 : channels = &intersection;
514 : }
515 :
516 41 : if (p2p->cfg->invitation_result) {
517 41 : int peer_oper_freq = 0;
518 41 : int freq = p2p_channel_to_freq(p2p->op_reg_class,
519 41 : p2p->op_channel);
520 41 : if (freq < 0)
521 0 : freq = 0;
522 :
523 41 : if (msg.operating_channel) {
524 58 : peer_oper_freq = p2p_channel_to_freq(
525 29 : msg.operating_channel[3],
526 29 : msg.operating_channel[4]);
527 29 : if (peer_oper_freq < 0)
528 0 : peer_oper_freq = 0;
529 : }
530 :
531 41 : p2p->cfg->invitation_result(p2p->cfg->cb_ctx, *msg.status,
532 : msg.group_bssid, channels, sa,
533 : freq, peer_oper_freq);
534 : }
535 :
536 41 : p2p_parse_free(&msg);
537 :
538 41 : p2p_clear_timeout(p2p);
539 41 : p2p_set_state(p2p, P2P_IDLE);
540 41 : p2p->invite_peer = NULL;
541 : }
542 :
543 :
544 48 : int p2p_invite_send(struct p2p_data *p2p, struct p2p_device *dev,
545 : const u8 *go_dev_addr, int dev_pw_id)
546 : {
547 : struct wpabuf *req;
548 : int freq;
549 :
550 48 : freq = dev->listen_freq > 0 ? dev->listen_freq : dev->oper_freq;
551 48 : if (freq <= 0)
552 3 : freq = dev->oob_go_neg_freq;
553 48 : if (freq <= 0) {
554 0 : p2p_dbg(p2p, "No Listen/Operating frequency known for the peer "
555 : MACSTR " to send Invitation Request",
556 0 : MAC2STR(dev->info.p2p_device_addr));
557 0 : return -1;
558 : }
559 :
560 48 : req = p2p_build_invitation_req(p2p, dev, go_dev_addr, dev_pw_id);
561 48 : if (req == NULL)
562 0 : return -1;
563 48 : if (p2p->state != P2P_IDLE)
564 3 : p2p_stop_listen_for_freq(p2p, freq);
565 48 : p2p_dbg(p2p, "Sending Invitation Request");
566 48 : p2p_set_state(p2p, P2P_INVITE);
567 48 : p2p->pending_action_state = P2P_PENDING_INVITATION_REQUEST;
568 48 : p2p->invite_peer = dev;
569 48 : dev->invitation_reqs++;
570 96 : if (p2p_send_action(p2p, freq, dev->info.p2p_device_addr,
571 48 : p2p->cfg->dev_addr, dev->info.p2p_device_addr,
572 48 : wpabuf_head(req), wpabuf_len(req), 500) < 0) {
573 1 : p2p_dbg(p2p, "Failed to send Action frame");
574 : /* Use P2P find to recover and retry */
575 1 : p2p_set_timeout(p2p, 0, 0);
576 : } else {
577 47 : dev->flags |= P2P_DEV_WAIT_INV_REQ_ACK;
578 : }
579 :
580 48 : wpabuf_free(req);
581 :
582 48 : return 0;
583 : }
584 :
585 :
586 47 : void p2p_invitation_req_cb(struct p2p_data *p2p, int success)
587 : {
588 47 : p2p_dbg(p2p, "Invitation Request TX callback: success=%d", success);
589 :
590 47 : if (p2p->invite_peer == NULL) {
591 0 : p2p_dbg(p2p, "No pending Invite");
592 47 : return;
593 : }
594 :
595 47 : if (success)
596 47 : p2p->invite_peer->flags &= ~P2P_DEV_WAIT_INV_REQ_ACK;
597 :
598 : /*
599 : * Use P2P find, if needed, to find the other device from its listen
600 : * channel.
601 : */
602 47 : p2p_set_state(p2p, P2P_INVITE);
603 47 : p2p_set_timeout(p2p, 0, success ? 500000 : 100000);
604 : }
605 :
606 :
607 53 : void p2p_invitation_resp_cb(struct p2p_data *p2p, int success)
608 : {
609 53 : p2p_dbg(p2p, "Invitation Response TX callback: success=%d", success);
610 53 : p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
611 :
612 53 : if (!success)
613 0 : p2p_dbg(p2p, "Assume Invitation Response was actually received by the peer even though Ack was not reported");
614 :
615 53 : if (p2p->cfg->invitation_received) {
616 212 : p2p->cfg->invitation_received(p2p->cfg->cb_ctx,
617 53 : p2p->inv_sa,
618 53 : p2p->inv_group_bssid_ptr,
619 53 : p2p->inv_ssid, p2p->inv_ssid_len,
620 53 : p2p->inv_go_dev_addr,
621 53 : p2p->inv_status,
622 : p2p->inv_op_freq);
623 : }
624 53 : }
625 :
626 :
627 46 : 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)
631 : {
632 : struct p2p_device *dev;
633 :
634 276 : p2p_dbg(p2p, "Request to invite peer " MACSTR " role=%d persistent=%d "
635 : "force_freq=%u",
636 276 : MAC2STR(peer), role, persistent_group, force_freq);
637 46 : if (bssid)
638 32 : p2p_dbg(p2p, "Invitation for BSSID " MACSTR, MAC2STR(bssid));
639 46 : if (go_dev_addr) {
640 72 : p2p_dbg(p2p, "Invitation for GO Device Address " MACSTR,
641 72 : MAC2STR(go_dev_addr));
642 12 : os_memcpy(p2p->invite_go_dev_addr_buf, go_dev_addr, ETH_ALEN);
643 12 : p2p->invite_go_dev_addr = p2p->invite_go_dev_addr_buf;
644 : } else
645 34 : p2p->invite_go_dev_addr = NULL;
646 46 : wpa_hexdump_ascii(MSG_DEBUG, "Invitation for SSID",
647 : ssid, ssid_len);
648 46 : if (dev_pw_id >= 0) {
649 3 : p2p_dbg(p2p, "Invitation to use Device Password ID %d",
650 : dev_pw_id);
651 : }
652 46 : p2p->invite_dev_pw_id = dev_pw_id;
653 66 : p2p->retry_invite_req = role == P2P_INVITE_ROLE_GO &&
654 66 : persistent_group && !force_freq;
655 46 : p2p->retry_invite_req_sent = 0;
656 :
657 46 : dev = p2p_get_device(p2p, peer);
658 49 : if (dev == NULL || (dev->listen_freq <= 0 && dev->oper_freq <= 0 &&
659 3 : dev->oob_go_neg_freq <= 0)) {
660 6 : p2p_dbg(p2p, "Cannot invite unknown P2P Device " MACSTR,
661 6 : MAC2STR(peer));
662 1 : return -1;
663 : }
664 :
665 45 : if (p2p_prepare_channel(p2p, dev, force_freq, pref_freq,
666 : role != P2P_INVITE_ROLE_CLIENT) < 0)
667 0 : return -1;
668 :
669 45 : if (persistent_group && role == P2P_INVITE_ROLE_CLIENT && !force_freq &&
670 : !pref_freq)
671 10 : dev->flags |= P2P_DEV_NO_PREF_CHAN;
672 : else
673 35 : dev->flags &= ~P2P_DEV_NO_PREF_CHAN;
674 :
675 45 : if (dev->flags & P2P_DEV_GROUP_CLIENT_ONLY) {
676 0 : if (!(dev->info.dev_capab &
677 : P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY)) {
678 0 : p2p_dbg(p2p, "Cannot invite a P2P Device " MACSTR
679 : " that is in a group and is not discoverable",
680 0 : MAC2STR(peer));
681 : }
682 : /* TODO: use device discoverability request through GO */
683 : }
684 :
685 45 : dev->invitation_reqs = 0;
686 :
687 45 : if (p2p->state != P2P_IDLE)
688 1 : p2p_stop_find(p2p);
689 :
690 45 : p2p->inv_role = role;
691 45 : p2p->inv_bssid_set = bssid != NULL;
692 45 : if (bssid)
693 31 : os_memcpy(p2p->inv_bssid, bssid, ETH_ALEN);
694 45 : os_memcpy(p2p->inv_ssid, ssid, ssid_len);
695 45 : p2p->inv_ssid_len = ssid_len;
696 45 : p2p->inv_persistent = persistent_group;
697 45 : return p2p_invite_send(p2p, dev, go_dev_addr, dev_pw_id);
698 : }
|