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