blob: 766b63e6510ab7ea1fce8df7fce2c317759b19c0 [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"
Sunil Ravi79e6c4f2025-01-04 00:47:06 +000014#include "common/sae.h"
15#include "crypto/sha384.h"
16#include "common/wpa_common.h"
17#include "pasn/pasn_common.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070018#include "p2p_i.h"
19#include "p2p.h"
20
21
Sunil Ravi79e6c4f2025-01-04 00:47:06 +000022struct wpabuf * p2p_build_invitation_req(struct p2p_data *p2p,
23 struct p2p_device *peer,
24 const u8 *go_dev_addr, int dev_pw_id)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070025{
26 struct wpabuf *buf;
27 u8 *len;
28 const u8 *dev_addr;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070029 size_t extra = 0;
Sunil8cd6f4d2022-06-28 18:40:46 +000030 bool is_6ghz_capab;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070031
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070032#ifdef CONFIG_WIFI_DISPLAY
33 struct wpabuf *wfd_ie = p2p->wfd_ie_invitation;
34 if (wfd_ie && p2p->inv_role == P2P_INVITE_ROLE_ACTIVE_GO) {
35 size_t i;
36 for (i = 0; i < p2p->num_groups; i++) {
37 struct p2p_group *g = p2p->groups[i];
38 struct wpabuf *ie;
Sunil Ravib0ac25f2024-07-12 01:42:03 +000039 if (!ether_addr_equal(p2p_group_get_interface_addr(g),
40 p2p->inv_bssid))
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -070041 continue;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070042 ie = p2p_group_get_wfd_ie(g);
43 if (ie) {
44 wfd_ie = ie;
45 break;
46 }
47 }
48 }
49 if (wfd_ie)
50 extra = wpabuf_len(wfd_ie);
51#endif /* CONFIG_WIFI_DISPLAY */
52
Dmitry Shmidt2e67f062014-07-16 09:55:28 -070053 if (p2p->vendor_elem && p2p->vendor_elem[VENDOR_ELEM_P2P_INV_REQ])
54 extra += wpabuf_len(p2p->vendor_elem[VENDOR_ELEM_P2P_INV_REQ]);
55
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070056 buf = wpabuf_alloc(1000 + extra);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070057 if (buf == NULL)
58 return NULL;
59
60 peer->dialog_token++;
61 if (peer->dialog_token == 0)
62 peer->dialog_token = 1;
63 p2p_buf_add_public_action_hdr(buf, P2P_INVITATION_REQ,
64 peer->dialog_token);
65
66 len = p2p_buf_add_ie_hdr(buf);
67 if (p2p->inv_role == P2P_INVITE_ROLE_ACTIVE_GO || !p2p->inv_persistent)
68 p2p_buf_add_config_timeout(buf, 0, 0);
69 else
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070070 p2p_buf_add_config_timeout(buf, p2p->go_timeout,
71 p2p->client_timeout);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070072 p2p_buf_add_invitation_flags(buf, p2p->inv_persistent ?
73 P2P_INVITATION_FLAGS_TYPE : 0);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -080074 if (p2p->inv_role != P2P_INVITE_ROLE_CLIENT ||
75 !(peer->flags & P2P_DEV_NO_PREF_CHAN))
76 p2p_buf_add_operating_channel(buf, p2p->cfg->country,
77 p2p->op_reg_class,
78 p2p->op_channel);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070079 if (p2p->inv_bssid_set)
80 p2p_buf_add_group_bssid(buf, p2p->inv_bssid);
Sunil8cd6f4d2022-06-28 18:40:46 +000081 is_6ghz_capab = is_p2p_6ghz_capable(p2p) &&
82 p2p_is_peer_6ghz_capab(p2p, peer->info.p2p_device_addr);
83 p2p_buf_add_channel_list(buf, p2p->cfg->country, &p2p->channels,
84 is_6ghz_capab);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070085 if (go_dev_addr)
86 dev_addr = go_dev_addr;
87 else if (p2p->inv_role == P2P_INVITE_ROLE_CLIENT)
88 dev_addr = peer->info.p2p_device_addr;
89 else
90 dev_addr = p2p->cfg->dev_addr;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070091 p2p_buf_add_group_id(buf, dev_addr, p2p->inv_ssid, p2p->inv_ssid_len);
92 p2p_buf_add_device_info(buf, p2p, peer);
93 p2p_buf_update_ie_hdr(buf, len);
94
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080095 p2p_buf_add_pref_channel_list(buf, p2p->pref_freq_list,
96 p2p->num_pref_freq);
97
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070098#ifdef CONFIG_WIFI_DISPLAY
99 if (wfd_ie)
100 wpabuf_put_buf(buf, wfd_ie);
101#endif /* CONFIG_WIFI_DISPLAY */
102
Dmitry Shmidt2e67f062014-07-16 09:55:28 -0700103 if (p2p->vendor_elem && p2p->vendor_elem[VENDOR_ELEM_P2P_INV_REQ])
104 wpabuf_put_buf(buf, p2p->vendor_elem[VENDOR_ELEM_P2P_INV_REQ]);
105
Sunil Ravi79e6c4f2025-01-04 00:47:06 +0000106 if (dev_pw_id >= 0 && !peer->p2p2) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800107 /* WSC IE in Invitation Request for NFC static handover */
108 p2p_build_wps_ie(p2p, buf, dev_pw_id, 0);
109 }
110
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700111 return buf;
112}
113
114
115static struct wpabuf * p2p_build_invitation_resp(struct p2p_data *p2p,
116 struct p2p_device *peer,
117 u8 dialog_token, u8 status,
118 const u8 *group_bssid,
119 u8 reg_class, u8 channel,
120 struct p2p_channels *channels)
121{
122 struct wpabuf *buf;
123 u8 *len;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700124 size_t extra = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700125
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700126#ifdef CONFIG_WIFI_DISPLAY
127 struct wpabuf *wfd_ie = p2p->wfd_ie_invitation;
128 if (wfd_ie && group_bssid) {
129 size_t i;
130 for (i = 0; i < p2p->num_groups; i++) {
131 struct p2p_group *g = p2p->groups[i];
132 struct wpabuf *ie;
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000133 if (!ether_addr_equal(p2p_group_get_interface_addr(g),
134 group_bssid))
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700135 continue;
136 ie = p2p_group_get_wfd_ie(g);
137 if (ie) {
138 wfd_ie = ie;
139 break;
140 }
141 }
142 }
143 if (wfd_ie)
144 extra = wpabuf_len(wfd_ie);
145#endif /* CONFIG_WIFI_DISPLAY */
146
Dmitry Shmidta3dc3092015-06-23 11:21:28 -0700147 if (p2p->vendor_elem && p2p->vendor_elem[VENDOR_ELEM_P2P_INV_RESP])
148 extra += wpabuf_len(p2p->vendor_elem[VENDOR_ELEM_P2P_INV_RESP]);
149
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700150 buf = wpabuf_alloc(1000 + extra);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700151 if (buf == NULL)
152 return NULL;
153
154 p2p_buf_add_public_action_hdr(buf, P2P_INVITATION_RESP,
155 dialog_token);
156
157 len = p2p_buf_add_ie_hdr(buf);
158 p2p_buf_add_status(buf, status);
159 p2p_buf_add_config_timeout(buf, 0, 0); /* FIX */
160 if (reg_class && channel)
161 p2p_buf_add_operating_channel(buf, p2p->cfg->country,
162 reg_class, channel);
163 if (group_bssid)
164 p2p_buf_add_group_bssid(buf, group_bssid);
Sunil8cd6f4d2022-06-28 18:40:46 +0000165 if (channels) {
166 bool is_6ghz_capab;
167
168 is_6ghz_capab = is_p2p_6ghz_capable(p2p) &&
169 p2p_is_peer_6ghz_capab(p2p, peer->info.p2p_device_addr);
170 p2p_buf_add_channel_list(buf, p2p->cfg->country, channels,
171 is_6ghz_capab);
172 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700173 p2p_buf_update_ie_hdr(buf, len);
174
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700175#ifdef CONFIG_WIFI_DISPLAY
176 if (wfd_ie)
177 wpabuf_put_buf(buf, wfd_ie);
178#endif /* CONFIG_WIFI_DISPLAY */
179
Dmitry Shmidta3dc3092015-06-23 11:21:28 -0700180 if (p2p->vendor_elem && p2p->vendor_elem[VENDOR_ELEM_P2P_INV_RESP])
181 wpabuf_put_buf(buf, p2p->vendor_elem[VENDOR_ELEM_P2P_INV_RESP]);
182
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700183 return buf;
184}
185
186
Sunil Ravic0f5d412024-09-11 22:12:49 +0000187struct wpabuf * p2p_process_invitation_req(struct p2p_data *p2p, const u8 *sa,
188 const u8 *data, size_t len,
Sunil Ravi79e6c4f2025-01-04 00:47:06 +0000189 int rx_freq, bool p2p2)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700190{
191 struct p2p_device *dev;
192 struct p2p_message msg;
193 struct wpabuf *resp = NULL;
194 u8 status = P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700195 int go = 0;
196 u8 group_bssid[ETH_ALEN], *bssid;
197 int op_freq = 0;
198 u8 reg_class = 0, channel = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800199 struct p2p_channels all_channels, intersection, *channels = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700200 int persistent;
201
202 os_memset(group_bssid, 0, sizeof(group_bssid));
203
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700204 p2p_dbg(p2p, "Received Invitation Request from " MACSTR " (freq=%d)",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700205 MAC2STR(sa), rx_freq);
206
207 if (p2p_parse(data, len, &msg))
Sunil Ravic0f5d412024-09-11 22:12:49 +0000208 return NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700209
210 dev = p2p_get_device(p2p, sa);
211 if (dev == NULL || (dev->flags & P2P_DEV_PROBE_REQ_ONLY)) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700212 p2p_dbg(p2p, "Invitation Request from unknown peer " MACSTR,
213 MAC2STR(sa));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700214
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800215 if (p2p_add_device(p2p, sa, rx_freq, NULL, 0, data + 1, len - 1,
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800216 0)) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700217 p2p_dbg(p2p, "Invitation Request add device failed "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700218 MACSTR, MAC2STR(sa));
219 status = P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
220 goto fail;
221 }
222
223 dev = p2p_get_device(p2p, sa);
224 if (dev == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700225 p2p_dbg(p2p, "Reject Invitation Request from unknown peer "
226 MACSTR, MAC2STR(sa));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700227 status = P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
228 goto fail;
229 }
230 }
231
232 if (!msg.group_id || !msg.channel_list) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700233 p2p_dbg(p2p, "Mandatory attribute missing in Invitation Request from "
234 MACSTR, MAC2STR(sa));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700235 status = P2P_SC_FAIL_INVALID_PARAMS;
236 goto fail;
237 }
238
239 if (msg.invitation_flags)
240 persistent = *msg.invitation_flags & P2P_INVITATION_FLAGS_TYPE;
241 else {
242 /* Invitation Flags is a mandatory attribute starting from P2P
243 * spec 1.06. As a backwards compatibility mechanism, assume
244 * the request was for a persistent group if the attribute is
245 * missing.
246 */
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700247 p2p_dbg(p2p, "Mandatory Invitation Flags attribute missing from Invitation Request");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700248 persistent = 1;
249 }
250
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800251 p2p_channels_union(&p2p->cfg->channels, &p2p->cfg->cli_channels,
252 &all_channels);
253
254 if (p2p_peer_channels_check(p2p, &all_channels, dev,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700255 msg.channel_list, msg.channel_list_len) <
256 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700257 p2p_dbg(p2p, "No common channels found");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700258 status = P2P_SC_FAIL_NO_COMMON_CHANNELS;
259 goto fail;
260 }
261
Dmitry Shmidt71757432014-06-02 13:50:35 -0700262 p2p_channels_dump(p2p, "own channels", &p2p->cfg->channels);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800263 p2p_channels_dump(p2p, "own client channels", &all_channels);
Dmitry Shmidt71757432014-06-02 13:50:35 -0700264 p2p_channels_dump(p2p, "peer channels", &dev->channels);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800265 p2p_channels_intersect(&all_channels, &dev->channels,
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -0700266 &intersection);
Dmitry Shmidt71757432014-06-02 13:50:35 -0700267 p2p_channels_dump(p2p, "intersection", &intersection);
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -0700268
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700269 if (p2p->cfg->invitation_process) {
270 status = p2p->cfg->invitation_process(
271 p2p->cfg->cb_ctx, sa, msg.group_bssid, msg.group_id,
272 msg.group_id + ETH_ALEN, msg.group_id_len - ETH_ALEN,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800273 &go, group_bssid, &op_freq, persistent, &intersection,
Sunil Ravi79e6c4f2025-01-04 00:47:06 +0000274 msg.dev_password_id_present ? msg.dev_password_id : -1,
275 p2p2);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700276 }
277
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800278 if (go) {
279 p2p_channels_intersect(&p2p->cfg->channels, &dev->channels,
280 &intersection);
281 p2p_channels_dump(p2p, "intersection(GO)", &intersection);
282 if (intersection.reg_classes == 0) {
283 p2p_dbg(p2p, "No common channels found (GO)");
284 status = P2P_SC_FAIL_NO_COMMON_CHANNELS;
285 goto fail;
286 }
287 }
288
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700289 if (op_freq) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700290 p2p_dbg(p2p, "Invitation processing forced frequency %d MHz",
291 op_freq);
Dmitry Shmidt4b060592013-04-29 16:42:49 -0700292 if (p2p_freq_to_channel(op_freq, &reg_class, &channel) < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700293 p2p_dbg(p2p, "Unknown forced freq %d MHz from invitation_process()",
294 op_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700295 status = P2P_SC_FAIL_NO_COMMON_CHANNELS;
296 goto fail;
297 }
298
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700299 if (!p2p_channels_includes(&intersection, reg_class, channel))
300 {
Dmitry Shmidtb70d0bb2015-11-16 10:43:06 -0800301 p2p_dbg(p2p, "forced freq %d MHz not in the supported channels intersection",
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700302 op_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700303 status = P2P_SC_FAIL_NO_COMMON_CHANNELS;
304 goto fail;
305 }
306
307 if (status == P2P_SC_SUCCESS)
308 channels = &intersection;
309 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700310 p2p_dbg(p2p, "No forced channel from invitation processing - figure out best one to use");
Dmitry Shmidt91c40cd2012-09-25 14:23:53 -0700311
Dmitry Shmidt91c40cd2012-09-25 14:23:53 -0700312 /* Default to own configuration as a starting point */
313 p2p->op_reg_class = p2p->cfg->op_reg_class;
314 p2p->op_channel = p2p->cfg->op_channel;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700315 p2p_dbg(p2p, "Own default op_class %d channel %d",
Dmitry Shmidt91c40cd2012-09-25 14:23:53 -0700316 p2p->op_reg_class, p2p->op_channel);
317
Sunil Ravi79e6c4f2025-01-04 00:47:06 +0000318#ifdef CONFIG_TESTING_OPTIONS
319 if (p2p->cfg->inv_op_class) {
320 /* Override configuration as a starting point */
321 p2p->op_reg_class = p2p->cfg->inv_op_class;
322 p2p->op_channel = p2p->cfg->inv_op_channel;
323 p2p_dbg(p2p,
324 "Override Invitation op_class %d channel %d",
325 p2p->op_reg_class, p2p->op_channel);
326 }
327#endif /* CONFIG_TESTING_OPTIONS */
328
Dmitry Shmidt91c40cd2012-09-25 14:23:53 -0700329 /* Use peer preference if specified and compatible */
330 if (msg.operating_channel) {
331 int req_freq;
332 req_freq = p2p_channel_to_freq(
Dmitry Shmidt91c40cd2012-09-25 14:23:53 -0700333 msg.operating_channel[3],
334 msg.operating_channel[4]);
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700335 p2p_dbg(p2p, "Peer operating channel preference: %d MHz",
Dmitry Shmidt91c40cd2012-09-25 14:23:53 -0700336 req_freq);
337 if (req_freq > 0 &&
338 p2p_channels_includes(&intersection,
339 msg.operating_channel[3],
340 msg.operating_channel[4])) {
341 p2p->op_reg_class = msg.operating_channel[3];
342 p2p->op_channel = msg.operating_channel[4];
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700343 p2p_dbg(p2p, "Use peer preference op_class %d channel %d",
Dmitry Shmidt91c40cd2012-09-25 14:23:53 -0700344 p2p->op_reg_class, p2p->op_channel);
345 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700346 p2p_dbg(p2p, "Cannot use peer channel preference");
Dmitry Shmidt91c40cd2012-09-25 14:23:53 -0700347 }
348 }
349
Dmitry Shmidt15907092014-03-25 10:42:57 -0700350 /* Reselect the channel only for the case of the GO */
351 if (go &&
352 !p2p_channels_includes(&intersection, p2p->op_reg_class,
Dmitry Shmidt91c40cd2012-09-25 14:23:53 -0700353 p2p->op_channel)) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700354 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 -0700355 p2p->op_reg_class, p2p->op_channel);
356 p2p_reselect_channel(p2p, &intersection);
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700357 p2p_dbg(p2p, "Re-selection result: op_class %d channel %d",
Dmitry Shmidt91c40cd2012-09-25 14:23:53 -0700358 p2p->op_reg_class, p2p->op_channel);
359 if (!p2p_channels_includes(&intersection,
360 p2p->op_reg_class,
361 p2p->op_channel)) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700362 p2p_dbg(p2p, "Peer does not support selected operating channel (reg_class=%u channel=%u)",
Dmitry Shmidt91c40cd2012-09-25 14:23:53 -0700363 p2p->op_reg_class, p2p->op_channel);
364 status = P2P_SC_FAIL_NO_COMMON_CHANNELS;
365 goto fail;
366 }
Dmitry Shmidt15907092014-03-25 10:42:57 -0700367 } else if (go && !(dev->flags & P2P_DEV_FORCE_FREQ) &&
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700368 !p2p->cfg->cfg_op_channel) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700369 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 -0700370 p2p->op_reg_class, p2p->op_channel);
371 p2p_reselect_channel(p2p, &intersection);
Dmitry Shmidt91c40cd2012-09-25 14:23:53 -0700372 }
373
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800374 /*
375 * Use the driver preferred frequency list extension if
376 * supported.
377 */
378 p2p_check_pref_chan(p2p, go, dev, &msg);
379
Dmitry Shmidt4b060592013-04-29 16:42:49 -0700380 op_freq = p2p_channel_to_freq(p2p->op_reg_class,
Dmitry Shmidt91c40cd2012-09-25 14:23:53 -0700381 p2p->op_channel);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700382 if (op_freq < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700383 p2p_dbg(p2p, "Unknown operational channel (country=%c%c reg_class=%u channel=%u)",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700384 p2p->cfg->country[0], p2p->cfg->country[1],
Dmitry Shmidt91c40cd2012-09-25 14:23:53 -0700385 p2p->op_reg_class, p2p->op_channel);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700386 status = P2P_SC_FAIL_NO_COMMON_CHANNELS;
387 goto fail;
388 }
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700389 p2p_dbg(p2p, "Selected operating channel - %d MHz", op_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700390
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700391 if (status == P2P_SC_SUCCESS) {
Dmitry Shmidt91c40cd2012-09-25 14:23:53 -0700392 reg_class = p2p->op_reg_class;
393 channel = p2p->op_channel;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700394 channels = &intersection;
395 }
396 }
397
398fail:
399 if (go && status == P2P_SC_SUCCESS && !is_zero_ether_addr(group_bssid))
400 bssid = group_bssid;
401 else
402 bssid = NULL;
403 resp = p2p_build_invitation_resp(p2p, dev, msg.dialog_token, status,
404 bssid, reg_class, channel, channels);
405
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700406 /*
407 * Store copy of invitation data to be used when processing TX status
408 * callback for the Acton frame.
409 */
410 os_memcpy(p2p->inv_sa, sa, ETH_ALEN);
411 if (msg.group_bssid) {
412 os_memcpy(p2p->inv_group_bssid, msg.group_bssid, ETH_ALEN);
413 p2p->inv_group_bssid_ptr = p2p->inv_group_bssid;
414 } else
415 p2p->inv_group_bssid_ptr = NULL;
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -0800416 if (msg.group_id) {
Dmitry Shmidt9d9e6022015-04-23 10:34:55 -0700417 if (msg.group_id_len - ETH_ALEN <= SSID_MAX_LEN) {
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -0800418 os_memcpy(p2p->inv_ssid, msg.group_id + ETH_ALEN,
419 msg.group_id_len - ETH_ALEN);
420 p2p->inv_ssid_len = msg.group_id_len - ETH_ALEN;
421 }
422 os_memcpy(p2p->inv_go_dev_addr, msg.group_id, ETH_ALEN);
423 } else {
424 p2p->inv_ssid_len = 0;
425 os_memset(p2p->inv_go_dev_addr, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700426 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700427 p2p->inv_status = status;
428 p2p->inv_op_freq = op_freq;
Sunil Ravic0f5d412024-09-11 22:12:49 +0000429 p2p_parse_free(&msg);
430 return resp;
431}
432
433
434void p2p_handle_invitation_req(struct p2p_data *p2p, const u8 *sa,
435 const u8 *data, size_t len, int rx_freq)
436{
437 int freq;
438 struct wpabuf *resp;
439
Sunil Ravi79e6c4f2025-01-04 00:47:06 +0000440 resp = p2p_process_invitation_req(p2p, sa, data, len, rx_freq, false);
Sunil Ravic0f5d412024-09-11 22:12:49 +0000441 if (!resp)
442 return;
443
444 if (rx_freq > 0)
445 freq = rx_freq;
446 else
447 freq = p2p_channel_to_freq(p2p->cfg->reg_class,
448 p2p->cfg->channel);
449 if (freq < 0)
450 p2p_dbg(p2p, "Unknown regulatory class/channel");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700451
452 p2p->pending_action_state = P2P_PENDING_INVITATION_RESPONSE;
453 if (p2p_send_action(p2p, freq, sa, p2p->cfg->dev_addr,
454 p2p->cfg->dev_addr,
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800455 wpabuf_head(resp), wpabuf_len(resp), 50) < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700456 p2p_dbg(p2p, "Failed to send Action frame");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700457 }
458
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700459 wpabuf_free(resp);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700460}
461
462
463void p2p_process_invitation_resp(struct p2p_data *p2p, const u8 *sa,
464 const u8 *data, size_t len)
465{
466 struct p2p_device *dev;
467 struct p2p_message msg;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -0800468 struct p2p_channels intersection, *channels = NULL;
Sunil Ravi79e6c4f2025-01-04 00:47:06 +0000469 bool all_channels = false;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700470
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700471 p2p_dbg(p2p, "Received Invitation Response from " MACSTR,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700472 MAC2STR(sa));
473
474 dev = p2p_get_device(p2p, sa);
475 if (dev == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700476 p2p_dbg(p2p, "Ignore Invitation Response from unknown peer "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700477 MACSTR, MAC2STR(sa));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800478 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700479 return;
480 }
481
482 if (dev != p2p->invite_peer) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700483 p2p_dbg(p2p, "Ignore unexpected Invitation Response from peer "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700484 MACSTR, MAC2STR(sa));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800485 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700486 return;
487 }
488
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800489 if (p2p_parse(data, len, &msg)) {
490 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700491 return;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800492 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700493
494 if (!msg.status) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700495 p2p_dbg(p2p, "Mandatory Status attribute missing in Invitation Response from "
496 MACSTR, MAC2STR(sa));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700497 p2p_parse_free(&msg);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800498 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700499 return;
500 }
501
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800502 /*
503 * We should not really receive a replayed response twice since
504 * duplicate frames are supposed to be dropped. However, not all drivers
505 * do that for pre-association frames. We did not use to verify dialog
506 * token matches for invitation response frames, but that check can be
507 * safely used to drop a replayed response to the previous Invitation
508 * Request in case the suggested operating channel was changed. This
509 * allows a duplicated reject frame to be dropped with the assumption
510 * that the real response follows after it.
511 */
512 if (*msg.status == P2P_SC_FAIL_NO_COMMON_CHANNELS &&
513 p2p->retry_invite_req_sent &&
514 msg.dialog_token != dev->dialog_token) {
515 p2p_dbg(p2p, "Unexpected Dialog Token %u (expected %u)",
516 msg.dialog_token, dev->dialog_token);
517 p2p_parse_free(&msg);
518 return;
519 }
520
521 if (*msg.status == P2P_SC_FAIL_NO_COMMON_CHANNELS &&
522 p2p->retry_invite_req &&
523 p2p_channel_random_social(&p2p->cfg->channels, &p2p->op_reg_class,
Hai Shalom74f70d42019-02-11 14:42:39 -0800524 &p2p->op_channel, NULL, NULL) == 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800525 p2p->retry_invite_req = 0;
526 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
527 p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
528 p2p_set_state(p2p, P2P_INVITE);
529 p2p_dbg(p2p, "Resend Invitation Request setting op_class %u channel %u as operating channel",
530 p2p->op_reg_class, p2p->op_channel);
531 p2p->retry_invite_req_sent = 1;
532 p2p_invite_send(p2p, p2p->invite_peer, p2p->invite_go_dev_addr,
533 p2p->invite_dev_pw_id);
534 p2p_parse_free(&msg);
535 return;
536 }
537 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
538 p2p->retry_invite_req = 0;
539
Dmitry Shmidt051af732013-10-22 13:52:46 -0700540 if (!msg.channel_list && *msg.status == P2P_SC_SUCCESS) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700541 p2p_dbg(p2p, "Mandatory Channel List attribute missing in Invitation Response from "
542 MACSTR, MAC2STR(sa));
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -0800543#ifdef CONFIG_P2P_STRICT
544 p2p_parse_free(&msg);
545 return;
546#endif /* CONFIG_P2P_STRICT */
547 /* Try to survive without peer channel list */
548 channels = &p2p->channels;
Sunil Ravi79e6c4f2025-01-04 00:47:06 +0000549 all_channels = true;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700550 } else if (!msg.channel_list) {
551 /* Non-success cases are not required to include Channel List */
552 channels = &p2p->channels;
Sunil Ravi79e6c4f2025-01-04 00:47:06 +0000553 all_channels = true;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -0800554 } else if (p2p_peer_channels_check(p2p, &p2p->channels, dev,
555 msg.channel_list,
556 msg.channel_list_len) < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700557 p2p_dbg(p2p, "No common channels found");
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -0800558 p2p_parse_free(&msg);
Sunil Ravi79e6c4f2025-01-04 00:47:06 +0000559 dev->inv_reject = true;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -0800560 return;
561 } else {
562 p2p_channels_intersect(&p2p->channels, &dev->channels,
563 &intersection);
564 channels = &intersection;
565 }
566
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -0800567 if (p2p->cfg->invitation_result) {
Dmitry Shmidt15907092014-03-25 10:42:57 -0700568 int peer_oper_freq = 0;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -0800569 int freq = p2p_channel_to_freq(p2p->op_reg_class,
570 p2p->op_channel);
571 if (freq < 0)
572 freq = 0;
Dmitry Shmidt15907092014-03-25 10:42:57 -0700573
574 if (msg.operating_channel) {
575 peer_oper_freq = p2p_channel_to_freq(
576 msg.operating_channel[3],
577 msg.operating_channel[4]);
578 if (peer_oper_freq < 0)
579 peer_oper_freq = 0;
580 }
581
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800582 /*
583 * Use the driver preferred frequency list extension if
584 * supported.
585 */
586 p2p_check_pref_chan(p2p, 0, dev, &msg);
587
Sunil Ravi79e6c4f2025-01-04 00:47:06 +0000588 if (dev->p2p2) {
589 dev->inv_freq = freq;
590 dev->inv_status = *msg.status;
591 dev->inv_all_channels = all_channels;
592 dev->inv_peer_oper_freq = peer_oper_freq;
593 if (msg.group_bssid)
594 os_memcpy(dev->inv_bssid, msg.group_bssid,
595 ETH_ALEN);
596 goto out;
597 }
598
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700599 p2p->cfg->invitation_result(p2p->cfg->cb_ctx, *msg.status,
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -0800600 msg.group_bssid, channels, sa,
Sunil Ravi79e6c4f2025-01-04 00:47:06 +0000601 freq, peer_oper_freq, NULL, NULL,
602 0);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -0800603 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700604
Sunil Ravi79e6c4f2025-01-04 00:47:06 +0000605 p2p_clear_timeout(p2p);
606 p2p_set_state(p2p, P2P_IDLE);
607 p2p->invite_peer = NULL;
608
609out:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700610 p2p_parse_free(&msg);
Sunil Ravi79e6c4f2025-01-04 00:47:06 +0000611}
612
613
614#ifdef CONFIG_PASN
615void p2p_start_invitation_connect(struct p2p_data *p2p, struct p2p_device *dev)
616{
617 size_t pmk_len = 0;
618 u8 pmkid[PMKID_LEN];
619 u8 pmk[PMK_LEN_MAX];
620 struct p2p_channels intersection;
621 const struct p2p_channels *inv_channels;
622
623 if (!p2p || !dev || dev->inv_reject || !dev->pasn)
624 return;
625
626 if (dev->inv_all_channels) {
627 inv_channels = &p2p->channels;
628 } else {
629 p2p_channels_intersect(&p2p->channels, &dev->channels,
630 &intersection);
631 inv_channels = &intersection;
632 }
633
634 pasn_initiator_pmksa_cache_get(dev->pasn->pmksa, dev->pasn->peer_addr,
635 pmkid, pmk, &pmk_len);
636
637 wpa_pasn_reset(dev->pasn);
638 p2p_dbg(p2p, "Invitation connect: msg status %d", dev->inv_status);
639 if (p2p->cfg->invitation_result)
640 p2p->cfg->invitation_result(p2p->cfg->cb_ctx, dev->inv_status,
641 dev->inv_bssid, inv_channels,
642 dev->info.p2p_device_addr,
643 dev->inv_freq,
644 dev->inv_peer_oper_freq, pmkid,
645 pmk, pmk_len);
646
647 /* Reset PMK and PMKID from stack */
648 forced_memzero(pmkid, sizeof(pmkid));
649 forced_memzero(pmk, sizeof(pmk));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700650
651 p2p_clear_timeout(p2p);
652 p2p_set_state(p2p, P2P_IDLE);
653 p2p->invite_peer = NULL;
654}
Sunil Ravi79e6c4f2025-01-04 00:47:06 +0000655#endif /* CONFIG_PASN */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700656
657
658int p2p_invite_send(struct p2p_data *p2p, struct p2p_device *dev,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800659 const u8 *go_dev_addr, int dev_pw_id)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700660{
661 struct wpabuf *req;
662 int freq;
663
664 freq = dev->listen_freq > 0 ? dev->listen_freq : dev->oper_freq;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800665 if (freq <= 0)
666 freq = dev->oob_go_neg_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700667 if (freq <= 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700668 p2p_dbg(p2p, "No Listen/Operating frequency known for the peer "
669 MACSTR " to send Invitation Request",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700670 MAC2STR(dev->info.p2p_device_addr));
671 return -1;
672 }
673
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800674 req = p2p_build_invitation_req(p2p, dev, go_dev_addr, dev_pw_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700675 if (req == NULL)
676 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700677 if (p2p->state != P2P_IDLE)
678 p2p_stop_listen_for_freq(p2p, freq);
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700679 p2p_dbg(p2p, "Sending Invitation Request");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700680 p2p_set_state(p2p, P2P_INVITE);
681 p2p->pending_action_state = P2P_PENDING_INVITATION_REQUEST;
682 p2p->invite_peer = dev;
683 dev->invitation_reqs++;
Sunil Raviaf8751c2023-03-29 11:35:17 -0700684
685 /* In case of an active P2P GO use a shorter wait time to avoid
686 * issues if not sending out multiple consecutive Beacon frames. */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700687 if (p2p_send_action(p2p, freq, dev->info.p2p_device_addr,
688 p2p->cfg->dev_addr, dev->info.p2p_device_addr,
Sunil Raviaf8751c2023-03-29 11:35:17 -0700689 wpabuf_head(req), wpabuf_len(req),
690 p2p->inv_role == P2P_INVITE_ROLE_ACTIVE_GO ?
691 150 : 500) < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700692 p2p_dbg(p2p, "Failed to send Action frame");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700693 /* Use P2P find to recover and retry */
694 p2p_set_timeout(p2p, 0, 0);
Dmitry Shmidt3c479372014-02-04 10:50:36 -0800695 } else {
696 dev->flags |= P2P_DEV_WAIT_INV_REQ_ACK;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700697 }
698
699 wpabuf_free(req);
700
701 return 0;
702}
703
704
705void p2p_invitation_req_cb(struct p2p_data *p2p, int success)
706{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700707 p2p_dbg(p2p, "Invitation Request TX callback: success=%d", success);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700708
709 if (p2p->invite_peer == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700710 p2p_dbg(p2p, "No pending Invite");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700711 return;
712 }
713
Dmitry Shmidt3c479372014-02-04 10:50:36 -0800714 if (success)
715 p2p->invite_peer->flags &= ~P2P_DEV_WAIT_INV_REQ_ACK;
716
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700717 /*
718 * Use P2P find, if needed, to find the other device from its listen
719 * channel.
720 */
721 p2p_set_state(p2p, P2P_INVITE);
Dmitry Shmidt96571392013-10-14 12:54:46 -0700722 p2p_set_timeout(p2p, 0, success ? 500000 : 100000);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700723}
724
725
Sunil Ravi79e6c4f2025-01-04 00:47:06 +0000726void p2p_invitation_resp_cb(struct p2p_data *p2p, const u8 *peer, int success)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700727{
Sunil Ravi79e6c4f2025-01-04 00:47:06 +0000728 size_t pmk_len = 0;
729 const u8 *pmkid = NULL, *pmk = NULL;
730
731#ifdef CONFIG_PASN
732 u8 _pmkid[PMKID_LEN];
733 u8 _pmk[PMK_LEN_MAX];
734 struct p2p_device *dev;
735
736 dev = p2p_get_device(p2p, peer);
737 if (dev && dev->pasn) {
738 pasn_responder_pmksa_cache_get(dev->pasn->pmksa,
739 dev->pasn->peer_addr, _pmkid,
740 _pmk, &pmk_len);
741 pmkid = _pmkid;
742 pmk = _pmk;
743 }
744#endif /* CONFIG_PASN */
745
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700746 p2p_dbg(p2p, "Invitation Response TX callback: success=%d", success);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700747 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
748
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -0700749 if (!success)
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700750 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 -0700751
752 if (p2p->cfg->invitation_received) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700753 p2p->cfg->invitation_received(p2p->cfg->cb_ctx,
754 p2p->inv_sa,
755 p2p->inv_group_bssid_ptr,
756 p2p->inv_ssid, p2p->inv_ssid_len,
757 p2p->inv_go_dev_addr,
758 p2p->inv_status,
Sunil Ravi79e6c4f2025-01-04 00:47:06 +0000759 p2p->inv_op_freq, pmkid, pmk,
760 pmk_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700761 }
Sunil Ravi79e6c4f2025-01-04 00:47:06 +0000762
763#ifdef CONFIG_PASN
764 /* Reset PMK and PMKID from stack */
765 forced_memzero(_pmkid, sizeof(_pmkid));
766 forced_memzero(_pmk, sizeof(_pmk));
767#endif /* CONFIG_PASN */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700768}
769
770
771int p2p_invite(struct p2p_data *p2p, const u8 *peer, enum p2p_invite_role role,
772 const u8 *bssid, const u8 *ssid, size_t ssid_len,
773 unsigned int force_freq, const u8 *go_dev_addr,
Sunil Ravi79e6c4f2025-01-04 00:47:06 +0000774 int persistent_group, unsigned int pref_freq, int dev_pw_id,
775 bool p2p2)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700776{
777 struct p2p_device *dev;
778
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700779 p2p_dbg(p2p, "Request to invite peer " MACSTR " role=%d persistent=%d "
Hai Shaloma20dcd72022-02-04 13:43:00 -0800780 "force_freq=%u allow_6ghz=%d",
781 MAC2STR(peer), role, persistent_group, force_freq,
782 p2p->allow_6ghz);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700783 if (bssid)
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700784 p2p_dbg(p2p, "Invitation for BSSID " MACSTR, MAC2STR(bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700785 if (go_dev_addr) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700786 p2p_dbg(p2p, "Invitation for GO Device Address " MACSTR,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700787 MAC2STR(go_dev_addr));
788 os_memcpy(p2p->invite_go_dev_addr_buf, go_dev_addr, ETH_ALEN);
789 p2p->invite_go_dev_addr = p2p->invite_go_dev_addr_buf;
790 } else
791 p2p->invite_go_dev_addr = NULL;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700792 wpa_hexdump_ascii(MSG_DEBUG, "Invitation for SSID",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700793 ssid, ssid_len);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800794 if (dev_pw_id >= 0) {
795 p2p_dbg(p2p, "Invitation to use Device Password ID %d",
796 dev_pw_id);
797 }
798 p2p->invite_dev_pw_id = dev_pw_id;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800799 p2p->retry_invite_req = role == P2P_INVITE_ROLE_GO &&
800 persistent_group && !force_freq;
801 p2p->retry_invite_req_sent = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700802
803 dev = p2p_get_device(p2p, peer);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800804 if (dev == NULL || (dev->listen_freq <= 0 && dev->oper_freq <= 0 &&
805 dev->oob_go_neg_freq <= 0)) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700806 p2p_dbg(p2p, "Cannot invite unknown P2P Device " MACSTR,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700807 MAC2STR(peer));
808 return -1;
809 }
810
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700811 if (p2p_prepare_channel(p2p, dev, force_freq, pref_freq,
812 role != P2P_INVITE_ROLE_CLIENT) < 0)
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -0800813 return -1;
814
815 if (persistent_group && role == P2P_INVITE_ROLE_CLIENT && !force_freq &&
816 !pref_freq)
817 dev->flags |= P2P_DEV_NO_PREF_CHAN;
818 else
819 dev->flags &= ~P2P_DEV_NO_PREF_CHAN;
820
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700821 if (dev->flags & P2P_DEV_GROUP_CLIENT_ONLY) {
822 if (!(dev->info.dev_capab &
823 P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY)) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700824 p2p_dbg(p2p, "Cannot invite a P2P Device " MACSTR
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700825 " that is in a group and is not discoverable",
826 MAC2STR(peer));
827 }
828 /* TODO: use device discoverability request through GO */
829 }
830
831 dev->invitation_reqs = 0;
832
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700833 if (p2p->state != P2P_IDLE)
834 p2p_stop_find(p2p);
835
836 p2p->inv_role = role;
837 p2p->inv_bssid_set = bssid != NULL;
838 if (bssid)
839 os_memcpy(p2p->inv_bssid, bssid, ETH_ALEN);
840 os_memcpy(p2p->inv_ssid, ssid, ssid_len);
841 p2p->inv_ssid_len = ssid_len;
842 p2p->inv_persistent = persistent_group;
Sunil Ravi79e6c4f2025-01-04 00:47:06 +0000843 if (p2p2)
844 return 0;
845
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800846 return p2p_invite_send(p2p, dev, go_dev_addr, dev_pw_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700847}