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