blob: 0f9c92484bb096f97d8ba5f3e92ff3ee81ecf381 [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * Wi-Fi Direct - P2P module
3 * Copyright (c) 2009-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
Jimmy Chen1b737ee2020-11-20 01:24:12 +080011#include <log/log.h>
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070012#include "common.h"
13#include "eloop.h"
Dmitry Shmidt9c175262016-03-03 10:20:07 -080014#include "common/defs.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070015#include "common/ieee802_11_defs.h"
16#include "common/ieee802_11_common.h"
Dmitry Shmidt2e67f062014-07-16 09:55:28 -070017#include "common/wpa_ctrl.h"
Dmitry Shmidt216983b2015-02-06 10:50:36 -080018#include "crypto/sha256.h"
19#include "crypto/crypto.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070020#include "wps/wps_i.h"
21#include "p2p_i.h"
22#include "p2p.h"
23
24
25static void p2p_state_timeout(void *eloop_ctx, void *timeout_ctx);
26static void p2p_device_free(struct p2p_data *p2p, struct p2p_device *dev);
27static void p2p_process_presence_req(struct p2p_data *p2p, const u8 *da,
28 const u8 *sa, const u8 *data, size_t len,
29 int rx_freq);
30static void p2p_process_presence_resp(struct p2p_data *p2p, const u8 *da,
31 const u8 *sa, const u8 *data,
32 size_t len);
33static void p2p_ext_listen_timeout(void *eloop_ctx, void *timeout_ctx);
34static void p2p_scan_timeout(void *eloop_ctx, void *timeout_ctx);
35
36
37/*
38 * p2p_scan recovery timeout
39 *
40 * Many drivers are using 30 second timeout on scan results. Allow a bit larger
41 * timeout for this to avoid hitting P2P timeout unnecessarily.
42 */
43#define P2P_SCAN_TIMEOUT 35
44
45/**
46 * P2P_PEER_EXPIRATION_AGE - Number of seconds after which inactive peer
47 * entries will be removed
48 */
Dmitry Shmidt2093d062014-01-17 10:58:50 -080049#ifndef P2P_PEER_EXPIRATION_AGE
50#define P2P_PEER_EXPIRATION_AGE 60
Dmitry Shmidt18463232014-01-24 12:29:41 -080051#endif /* P2P_PEER_EXPIRATION_AGE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070052
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070053
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080054void p2p_expire_peers(struct p2p_data *p2p)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070055{
56 struct p2p_device *dev, *n;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -080057 struct os_reltime now;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -080058 size_t i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070059
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -080060 os_get_reltime(&now);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070061 dl_list_for_each_safe(dev, n, &p2p->devices, struct p2p_device, list) {
62 if (dev->last_seen.sec + P2P_PEER_EXPIRATION_AGE >= now.sec)
63 continue;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -080064
Dmitry Shmidt9e3f8ee2014-01-17 10:52:01 -080065 if (dev == p2p->go_neg_peer) {
66 /*
67 * GO Negotiation is in progress with the peer, so
68 * don't expire the peer entry until GO Negotiation
69 * fails or times out.
70 */
71 continue;
72 }
73
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -080074 if (p2p->cfg->go_connected &&
75 p2p->cfg->go_connected(p2p->cfg->cb_ctx,
76 dev->info.p2p_device_addr)) {
77 /*
78 * We are connected as a client to a group in which the
79 * peer is the GO, so do not expire the peer entry.
80 */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -080081 os_get_reltime(&dev->last_seen);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -080082 continue;
83 }
84
85 for (i = 0; i < p2p->num_groups; i++) {
86 if (p2p_group_is_client_connected(
87 p2p->groups[i], dev->info.p2p_device_addr))
88 break;
89 }
90 if (i < p2p->num_groups) {
91 /*
92 * The peer is connected as a client in a group where
93 * we are the GO, so do not expire the peer entry.
94 */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -080095 os_get_reltime(&dev->last_seen);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -080096 continue;
97 }
98
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -070099 p2p_dbg(p2p, "Expiring old peer entry " MACSTR,
100 MAC2STR(dev->info.p2p_device_addr));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700101 dl_list_del(&dev->list);
102 p2p_device_free(p2p, dev);
103 }
104}
105
106
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700107static const char * p2p_state_txt(int state)
108{
109 switch (state) {
110 case P2P_IDLE:
111 return "IDLE";
112 case P2P_SEARCH:
113 return "SEARCH";
114 case P2P_CONNECT:
115 return "CONNECT";
116 case P2P_CONNECT_LISTEN:
117 return "CONNECT_LISTEN";
118 case P2P_GO_NEG:
119 return "GO_NEG";
120 case P2P_LISTEN_ONLY:
121 return "LISTEN_ONLY";
122 case P2P_WAIT_PEER_CONNECT:
123 return "WAIT_PEER_CONNECT";
124 case P2P_WAIT_PEER_IDLE:
125 return "WAIT_PEER_IDLE";
126 case P2P_SD_DURING_FIND:
127 return "SD_DURING_FIND";
128 case P2P_PROVISIONING:
129 return "PROVISIONING";
130 case P2P_PD_DURING_FIND:
131 return "PD_DURING_FIND";
132 case P2P_INVITE:
133 return "INVITE";
134 case P2P_INVITE_LISTEN:
135 return "INVITE_LISTEN";
136 default:
137 return "?";
138 }
139}
140
141
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700142const char * p2p_get_state_txt(struct p2p_data *p2p)
143{
144 return p2p_state_txt(p2p->state);
145}
146
147
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800148struct p2ps_advertisement * p2p_get_p2ps_adv_list(struct p2p_data *p2p)
149{
150 return p2p ? p2p->p2ps_adv_list : NULL;
151}
152
153
154void p2p_set_intended_addr(struct p2p_data *p2p, const u8 *intended_addr)
155{
156 if (p2p && intended_addr)
157 os_memcpy(p2p->intended_addr, intended_addr, ETH_ALEN);
158}
159
160
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800161u16 p2p_get_provisioning_info(struct p2p_data *p2p, const u8 *addr)
162{
163 struct p2p_device *dev = NULL;
164
165 if (!addr || !p2p)
166 return 0;
167
168 dev = p2p_get_device(p2p, addr);
169 if (dev)
170 return dev->wps_prov_info;
171 else
172 return 0;
173}
174
175
Dmitry Shmidt04949592012-07-19 12:16:46 -0700176void p2p_clear_provisioning_info(struct p2p_data *p2p, const u8 *addr)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800177{
178 struct p2p_device *dev = NULL;
179
Dmitry Shmidt04949592012-07-19 12:16:46 -0700180 if (!addr || !p2p)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800181 return;
182
Dmitry Shmidt04949592012-07-19 12:16:46 -0700183 dev = p2p_get_device(p2p, addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800184 if (dev)
185 dev->wps_prov_info = 0;
186}
187
188
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700189void p2p_set_state(struct p2p_data *p2p, int new_state)
190{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700191 p2p_dbg(p2p, "State %s -> %s",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700192 p2p_state_txt(p2p->state), p2p_state_txt(new_state));
193 p2p->state = new_state;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700194
195 if (new_state == P2P_IDLE && p2p->pending_channel) {
196 p2p_dbg(p2p, "Apply change in listen channel");
197 p2p->cfg->reg_class = p2p->pending_reg_class;
198 p2p->cfg->channel = p2p->pending_channel;
199 p2p->pending_reg_class = 0;
200 p2p->pending_channel = 0;
201 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700202}
203
204
205void p2p_set_timeout(struct p2p_data *p2p, unsigned int sec, unsigned int usec)
206{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700207 p2p_dbg(p2p, "Set timeout (state=%s): %u.%06u sec",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700208 p2p_state_txt(p2p->state), sec, usec);
209 eloop_cancel_timeout(p2p_state_timeout, p2p, NULL);
210 eloop_register_timeout(sec, usec, p2p_state_timeout, p2p, NULL);
211}
212
213
214void p2p_clear_timeout(struct p2p_data *p2p)
215{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700216 p2p_dbg(p2p, "Clear timeout (state=%s)", p2p_state_txt(p2p->state));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700217 eloop_cancel_timeout(p2p_state_timeout, p2p, NULL);
218}
219
220
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800221void p2p_go_neg_failed(struct p2p_data *p2p, int status)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700222{
223 struct p2p_go_neg_results res;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800224 struct p2p_device *peer = p2p->go_neg_peer;
225
226 if (!peer)
227 return;
228
229 eloop_cancel_timeout(p2p_go_neg_wait_timeout, p2p, NULL);
230 if (p2p->state != P2P_SEARCH) {
231 /*
232 * Clear timeouts related to GO Negotiation if no new p2p_find
233 * has been started.
234 */
235 p2p_clear_timeout(p2p);
236 p2p_set_state(p2p, P2P_IDLE);
Dmitry Shmidt8c652892013-03-01 10:14:01 -0800237 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800238
239 peer->flags &= ~P2P_DEV_PEER_WAITING_RESPONSE;
240 peer->wps_method = WPS_NOT_READY;
241 peer->oob_pw_id = 0;
242 wpabuf_free(peer->go_neg_conf);
243 peer->go_neg_conf = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700244 p2p->go_neg_peer = NULL;
245
246 os_memset(&res, 0, sizeof(res));
247 res.status = status;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800248 os_memcpy(res.peer_device_addr, peer->info.p2p_device_addr, ETH_ALEN);
249 os_memcpy(res.peer_interface_addr, peer->intended_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700250 p2p->cfg->go_neg_completed(p2p->cfg->cb_ctx, &res);
251}
252
253
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800254static void p2p_listen_in_find(struct p2p_data *p2p, int dev_disc)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700255{
256 unsigned int r, tu;
257 int freq;
258 struct wpabuf *ies;
259
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700260 p2p_dbg(p2p, "Starting short listen state (state=%s)",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700261 p2p_state_txt(p2p->state));
262
Dmitry Shmidt3c57b3f2014-05-22 15:13:07 -0700263 if (p2p->pending_listen_freq) {
264 /* We have a pending p2p_listen request */
265 p2p_dbg(p2p, "p2p_listen command pending already");
266 return;
267 }
268
Dmitry Shmidt4b060592013-04-29 16:42:49 -0700269 freq = p2p_channel_to_freq(p2p->cfg->reg_class, p2p->cfg->channel);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700270 if (freq < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700271 p2p_dbg(p2p, "Unknown regulatory class/channel");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700272 return;
273 }
274
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700275 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
276 r = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700277 tu = (r % ((p2p->max_disc_int - p2p->min_disc_int) + 1) +
278 p2p->min_disc_int) * 100;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800279 if (p2p->max_disc_tu >= 0 && tu > (unsigned int) p2p->max_disc_tu)
280 tu = p2p->max_disc_tu;
281 if (!dev_disc && tu < 100)
282 tu = 100; /* Need to wait in non-device discovery use cases */
283 if (p2p->cfg->max_listen && 1024 * tu / 1000 > p2p->cfg->max_listen)
284 tu = p2p->cfg->max_listen * 1000 / 1024;
285
286 if (tu == 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700287 p2p_dbg(p2p, "Skip listen state since duration was 0 TU");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800288 p2p_set_timeout(p2p, 0, 0);
289 return;
290 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700291
Dmitry Shmidta3dc3092015-06-23 11:21:28 -0700292 ies = p2p_build_probe_resp_ies(p2p, NULL, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700293 if (ies == NULL)
294 return;
295
Dmitry Shmidt3c57b3f2014-05-22 15:13:07 -0700296 p2p->pending_listen_freq = freq;
297 p2p->pending_listen_sec = 0;
298 p2p->pending_listen_usec = 1024 * tu;
299
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700300 if (p2p->cfg->start_listen(p2p->cfg->cb_ctx, freq, 1024 * tu / 1000,
301 ies) < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700302 p2p_dbg(p2p, "Failed to start listen mode");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700303 p2p->pending_listen_freq = 0;
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000304 } else {
305 p2p->pending_listen_wait_drv = true;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700306 }
307 wpabuf_free(ies);
308}
309
310
311int p2p_listen(struct p2p_data *p2p, unsigned int timeout)
312{
313 int freq;
314 struct wpabuf *ies;
315
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700316 p2p_dbg(p2p, "Going to listen(only) state");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700317
Dmitry Shmidt3c57b3f2014-05-22 15:13:07 -0700318 if (p2p->pending_listen_freq) {
319 /* We have a pending p2p_listen request */
320 p2p_dbg(p2p, "p2p_listen command pending already");
321 return -1;
322 }
323
Dmitry Shmidt4b060592013-04-29 16:42:49 -0700324 freq = p2p_channel_to_freq(p2p->cfg->reg_class, p2p->cfg->channel);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700325 if (freq < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700326 p2p_dbg(p2p, "Unknown regulatory class/channel");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700327 return -1;
328 }
329
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700330 p2p->pending_listen_sec = timeout / 1000;
331 p2p->pending_listen_usec = (timeout % 1000) * 1000;
332
333 if (p2p->p2p_scan_running) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700334 if (p2p->start_after_scan == P2P_AFTER_SCAN_CONNECT) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700335 p2p_dbg(p2p, "p2p_scan running - connect is already pending - skip listen");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800336 return 0;
337 }
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700338 p2p_dbg(p2p, "p2p_scan running - delay start of listen state");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700339 p2p->start_after_scan = P2P_AFTER_SCAN_LISTEN;
340 return 0;
341 }
342
Dmitry Shmidta3dc3092015-06-23 11:21:28 -0700343 ies = p2p_build_probe_resp_ies(p2p, NULL, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700344 if (ies == NULL)
345 return -1;
346
Dmitry Shmidt3c57b3f2014-05-22 15:13:07 -0700347 p2p->pending_listen_freq = freq;
348
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700349 if (p2p->cfg->start_listen(p2p->cfg->cb_ctx, freq, timeout, ies) < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700350 p2p_dbg(p2p, "Failed to start listen mode");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700351 p2p->pending_listen_freq = 0;
352 wpabuf_free(ies);
353 return -1;
354 }
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000355 p2p->pending_listen_wait_drv = true;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700356 wpabuf_free(ies);
357
358 p2p_set_state(p2p, P2P_LISTEN_ONLY);
359
360 return 0;
361}
362
363
364static void p2p_device_clear_reported(struct p2p_data *p2p)
365{
366 struct p2p_device *dev;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800367 dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700368 dev->flags &= ~P2P_DEV_REPORTED;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800369 dev->sd_reqs = 0;
370 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700371}
372
373
374/**
375 * p2p_get_device - Fetch a peer entry
376 * @p2p: P2P module context from p2p_init()
377 * @addr: P2P Device Address of the peer
378 * Returns: Pointer to the device entry or %NULL if not found
379 */
380struct p2p_device * p2p_get_device(struct p2p_data *p2p, const u8 *addr)
381{
382 struct p2p_device *dev;
383 dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000384 if (ether_addr_equal(dev->info.p2p_device_addr, addr))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700385 return dev;
386 }
387 return NULL;
388}
389
390
391/**
392 * p2p_get_device_interface - Fetch a peer entry based on P2P Interface Address
393 * @p2p: P2P module context from p2p_init()
394 * @addr: P2P Interface Address of the peer
395 * Returns: Pointer to the device entry or %NULL if not found
396 */
397struct p2p_device * p2p_get_device_interface(struct p2p_data *p2p,
398 const u8 *addr)
399{
400 struct p2p_device *dev;
401 dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000402 if (ether_addr_equal(dev->interface_addr, addr))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700403 return dev;
404 }
405 return NULL;
406}
407
408
409/**
410 * p2p_create_device - Create a peer entry
411 * @p2p: P2P module context from p2p_init()
412 * @addr: P2P Device Address of the peer
413 * Returns: Pointer to the device entry or %NULL on failure
414 *
415 * If there is already an entry for the peer, it will be returned instead of
416 * creating a new one.
417 */
418static struct p2p_device * p2p_create_device(struct p2p_data *p2p,
419 const u8 *addr)
420{
421 struct p2p_device *dev, *oldest = NULL;
422 size_t count = 0;
423
424 dev = p2p_get_device(p2p, addr);
425 if (dev)
426 return dev;
427
428 dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
429 count++;
430 if (oldest == NULL ||
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800431 os_reltime_before(&dev->last_seen, &oldest->last_seen))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700432 oldest = dev;
433 }
434 if (count + 1 > p2p->cfg->max_peers && oldest) {
Hai Shaloma20dcd72022-02-04 13:43:00 -0800435 p2p_dbg(p2p,
436 "Remove oldest peer entry to make room for a new peer "
437 MACSTR, MAC2STR(oldest->info.p2p_device_addr));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700438 dl_list_del(&oldest->list);
439 p2p_device_free(p2p, oldest);
440 }
441
442 dev = os_zalloc(sizeof(*dev));
443 if (dev == NULL)
444 return NULL;
445 dl_list_add(&p2p->devices, &dev->list);
446 os_memcpy(dev->info.p2p_device_addr, addr, ETH_ALEN);
Sunil Ravi77d572f2023-01-17 23:58:31 +0000447 dev->support_6ghz = false;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700448
449 return dev;
450}
451
452
453static void p2p_copy_client_info(struct p2p_device *dev,
454 struct p2p_client_info *cli)
455{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800456 p2p_copy_filter_devname(dev->info.device_name,
457 sizeof(dev->info.device_name),
458 cli->dev_name, cli->dev_name_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700459 dev->info.dev_capab = cli->dev_capab;
460 dev->info.config_methods = cli->config_methods;
461 os_memcpy(dev->info.pri_dev_type, cli->pri_dev_type, 8);
462 dev->info.wps_sec_dev_type_list_len = 8 * cli->num_sec_dev_types;
Jimmy Chen1b737ee2020-11-20 01:24:12 +0800463 if (dev->info.wps_sec_dev_type_list_len > WPS_SEC_DEV_TYPE_MAX_LEN) {
464 android_errorWriteLog(0x534e4554, "172937525");
Jimmy Chene12b6972020-11-09 11:43:12 +0200465 dev->info.wps_sec_dev_type_list_len = WPS_SEC_DEV_TYPE_MAX_LEN;
Jimmy Chen1b737ee2020-11-20 01:24:12 +0800466 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700467 os_memcpy(dev->info.wps_sec_dev_type_list, cli->sec_dev_types,
468 dev->info.wps_sec_dev_type_list_len);
469}
470
471
472static int p2p_add_group_clients(struct p2p_data *p2p, const u8 *go_dev_addr,
473 const u8 *go_interface_addr, int freq,
Dmitry Shmidta3dc3092015-06-23 11:21:28 -0700474 const u8 *gi, size_t gi_len,
475 struct os_reltime *rx_time)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700476{
477 struct p2p_group_info info;
478 size_t c;
479 struct p2p_device *dev;
480
481 if (gi == NULL)
482 return 0;
483
484 if (p2p_group_info_parse(gi, gi_len, &info) < 0)
485 return -1;
486
487 /*
488 * Clear old data for this group; if the devices are still in the
489 * group, the information will be restored in the loop following this.
490 */
491 dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000492 if (ether_addr_equal(dev->member_in_go_iface,
493 go_interface_addr)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700494 os_memset(dev->member_in_go_iface, 0, ETH_ALEN);
495 os_memset(dev->member_in_go_dev, 0, ETH_ALEN);
496 }
497 }
498
499 for (c = 0; c < info.num_clients; c++) {
500 struct p2p_client_info *cli = &info.client[c];
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000501 if (ether_addr_equal(cli->p2p_device_addr, p2p->cfg->dev_addr))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800502 continue; /* ignore our own entry */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700503 dev = p2p_get_device(p2p, cli->p2p_device_addr);
504 if (dev) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700505 if (dev->flags & (P2P_DEV_GROUP_CLIENT_ONLY |
Dmitry Shmidt04949592012-07-19 12:16:46 -0700506 P2P_DEV_PROBE_REQ_ONLY)) {
507 /*
508 * Update information since we have not
509 * received this directly from the client.
510 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700511 p2p_copy_client_info(dev, cli);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700512 } else {
513 /*
514 * Need to update P2P Client Discoverability
515 * flag since it is valid only in P2P Group
516 * Info attribute.
517 */
518 dev->info.dev_capab &=
519 ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
520 dev->info.dev_capab |=
521 cli->dev_capab &
522 P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
523 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700524 if (dev->flags & P2P_DEV_PROBE_REQ_ONLY) {
525 dev->flags &= ~P2P_DEV_PROBE_REQ_ONLY;
526 }
527 } else {
528 dev = p2p_create_device(p2p, cli->p2p_device_addr);
529 if (dev == NULL)
530 continue;
531 dev->flags |= P2P_DEV_GROUP_CLIENT_ONLY;
532 p2p_copy_client_info(dev, cli);
533 dev->oper_freq = freq;
534 p2p->cfg->dev_found(p2p->cfg->cb_ctx,
535 dev->info.p2p_device_addr,
536 &dev->info, 1);
537 dev->flags |= P2P_DEV_REPORTED | P2P_DEV_REPORTED_ONCE;
538 }
539
540 os_memcpy(dev->interface_addr, cli->p2p_interface_addr,
541 ETH_ALEN);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -0700542 os_memcpy(&dev->last_seen, rx_time, sizeof(struct os_reltime));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700543 os_memcpy(dev->member_in_go_dev, go_dev_addr, ETH_ALEN);
544 os_memcpy(dev->member_in_go_iface, go_interface_addr,
545 ETH_ALEN);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -0700546 dev->flags |= P2P_DEV_LAST_SEEN_AS_GROUP_CLIENT;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700547 }
548
549 return 0;
550}
551
552
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700553static void p2p_copy_wps_info(struct p2p_data *p2p, struct p2p_device *dev,
554 int probe_req, const struct p2p_message *msg)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700555{
556 os_memcpy(dev->info.device_name, msg->device_name,
557 sizeof(dev->info.device_name));
558
559 if (msg->manufacturer &&
560 msg->manufacturer_len < sizeof(dev->info.manufacturer)) {
561 os_memset(dev->info.manufacturer, 0,
562 sizeof(dev->info.manufacturer));
563 os_memcpy(dev->info.manufacturer, msg->manufacturer,
564 msg->manufacturer_len);
565 }
566
567 if (msg->model_name &&
568 msg->model_name_len < sizeof(dev->info.model_name)) {
569 os_memset(dev->info.model_name, 0,
570 sizeof(dev->info.model_name));
571 os_memcpy(dev->info.model_name, msg->model_name,
572 msg->model_name_len);
573 }
574
575 if (msg->model_number &&
576 msg->model_number_len < sizeof(dev->info.model_number)) {
577 os_memset(dev->info.model_number, 0,
578 sizeof(dev->info.model_number));
579 os_memcpy(dev->info.model_number, msg->model_number,
580 msg->model_number_len);
581 }
582
583 if (msg->serial_number &&
584 msg->serial_number_len < sizeof(dev->info.serial_number)) {
585 os_memset(dev->info.serial_number, 0,
586 sizeof(dev->info.serial_number));
587 os_memcpy(dev->info.serial_number, msg->serial_number,
588 msg->serial_number_len);
589 }
590
591 if (msg->pri_dev_type)
592 os_memcpy(dev->info.pri_dev_type, msg->pri_dev_type,
593 sizeof(dev->info.pri_dev_type));
594 else if (msg->wps_pri_dev_type)
595 os_memcpy(dev->info.pri_dev_type, msg->wps_pri_dev_type,
596 sizeof(dev->info.pri_dev_type));
597
598 if (msg->wps_sec_dev_type_list) {
599 os_memcpy(dev->info.wps_sec_dev_type_list,
600 msg->wps_sec_dev_type_list,
601 msg->wps_sec_dev_type_list_len);
602 dev->info.wps_sec_dev_type_list_len =
603 msg->wps_sec_dev_type_list_len;
604 }
605
606 if (msg->capability) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700607 /*
608 * P2P Client Discoverability bit is reserved in all frames
609 * that use this function, so do not change its value here.
610 */
611 dev->info.dev_capab &= P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
612 dev->info.dev_capab |= msg->capability[0] &
613 ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700614 dev->info.group_capab = msg->capability[1];
615 }
616
Sunil Ravi77d572f2023-01-17 23:58:31 +0000617 p2p_update_peer_6ghz_capab(dev, msg);
618
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700619 if (msg->ext_listen_timing) {
620 dev->ext_listen_period = WPA_GET_LE16(msg->ext_listen_timing);
621 dev->ext_listen_interval =
622 WPA_GET_LE16(msg->ext_listen_timing + 2);
623 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700624
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700625 if (!probe_req) {
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800626 u16 new_config_methods;
627 new_config_methods = msg->config_methods ?
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700628 msg->config_methods : msg->wps_config_methods;
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800629 if (new_config_methods &&
630 dev->info.config_methods != new_config_methods) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700631 p2p_dbg(p2p, "Update peer " MACSTR
632 " config_methods 0x%x -> 0x%x",
633 MAC2STR(dev->info.p2p_device_addr),
634 dev->info.config_methods,
635 new_config_methods);
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800636 dev->info.config_methods = new_config_methods;
637 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700638 }
639}
640
641
Sunil Ravi77d572f2023-01-17 23:58:31 +0000642void p2p_update_peer_6ghz_capab(struct p2p_device *dev,
643 const struct p2p_message *msg)
644{
645 if (msg->capability &&
646 (msg->capability[0] & P2P_DEV_CAPAB_6GHZ_BAND_CAPABLE))
647 dev->support_6ghz = true;
648}
649
650
Dmitry Shmidt2e67f062014-07-16 09:55:28 -0700651static void p2p_update_peer_vendor_elems(struct p2p_device *dev, const u8 *ies,
652 size_t ies_len)
653{
654 const u8 *pos, *end;
655 u8 id, len;
656
657 wpabuf_free(dev->info.vendor_elems);
658 dev->info.vendor_elems = NULL;
659
660 end = ies + ies_len;
661
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800662 for (pos = ies; end - pos > 1; pos += len) {
Dmitry Shmidt2e67f062014-07-16 09:55:28 -0700663 id = *pos++;
664 len = *pos++;
665
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800666 if (len > end - pos)
Dmitry Shmidt2e67f062014-07-16 09:55:28 -0700667 break;
668
669 if (id != WLAN_EID_VENDOR_SPECIFIC || len < 3)
670 continue;
671
672 if (len >= 4) {
673 u32 type = WPA_GET_BE32(pos);
674
675 if (type == WPA_IE_VENDOR_TYPE ||
676 type == WMM_IE_VENDOR_TYPE ||
677 type == WPS_IE_VENDOR_TYPE ||
678 type == P2P_IE_VENDOR_TYPE ||
679 type == WFD_IE_VENDOR_TYPE)
680 continue;
681 }
682
683 /* Unknown vendor element - make raw IE data available */
684 if (wpabuf_resize(&dev->info.vendor_elems, 2 + len) < 0)
685 break;
686 wpabuf_put_data(dev->info.vendor_elems, pos - 2, 2 + len);
Hai Shalom60840252021-02-19 19:02:11 -0800687 if (wpabuf_size(dev->info.vendor_elems) > 2000)
688 break;
Dmitry Shmidt2e67f062014-07-16 09:55:28 -0700689 }
690}
691
692
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800693static int p2p_compare_wfd_info(struct p2p_device *dev,
694 const struct p2p_message *msg)
695{
696 if (dev->info.wfd_subelems && msg->wfd_subelems) {
697 if (dev->info.wfd_subelems->used != msg->wfd_subelems->used)
698 return 1;
699
700 return os_memcmp(dev->info.wfd_subelems->buf,
701 msg->wfd_subelems->buf,
702 dev->info.wfd_subelems->used);
703 }
704 if (dev->info.wfd_subelems || msg->wfd_subelems)
705 return 1;
706
707 return 0;
708}
709
710
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700711/**
Dmitry Shmidt04949592012-07-19 12:16:46 -0700712 * p2p_add_device - Add peer entries based on scan results or P2P frames
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700713 * @p2p: P2P module context from p2p_init()
714 * @addr: Source address of Beacon or Probe Response frame (may be either
715 * P2P Device Address or P2P Interface Address)
716 * @level: Signal level (signal strength of the received frame from the peer)
717 * @freq: Frequency on which the Beacon or Probe Response frame was received
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800718 * @rx_time: Time when the result was received
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700719 * @ies: IEs from the Beacon or Probe Response frame
720 * @ies_len: Length of ies buffer in octets
Dmitry Shmidt04949592012-07-19 12:16:46 -0700721 * @scan_res: Whether this was based on scan results
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700722 * Returns: 0 on success, -1 on failure
723 *
724 * If the scan result is for a GO, the clients in the group will also be added
725 * to the peer table. This function can also be used with some other frames
726 * like Provision Discovery Request that contains P2P Capability and P2P Device
727 * Info attributes.
728 */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800729int p2p_add_device(struct p2p_data *p2p, const u8 *addr, int freq,
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800730 struct os_reltime *rx_time, int level, const u8 *ies,
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800731 size_t ies_len, int scan_res)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700732{
733 struct p2p_device *dev;
734 struct p2p_message msg;
735 const u8 *p2p_dev_addr;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800736 int wfd_changed;
Dmitry Shmidt29333592017-01-09 12:27:11 -0800737 int dev_name_changed;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700738 int i;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800739 struct os_reltime time_now;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700740
741 os_memset(&msg, 0, sizeof(msg));
742 if (p2p_parse_ies(ies, ies_len, &msg)) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700743 p2p_dbg(p2p, "Failed to parse P2P IE for a device entry");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700744 p2p_parse_free(&msg);
745 return -1;
746 }
747
748 if (msg.p2p_device_addr)
749 p2p_dev_addr = msg.p2p_device_addr;
750 else if (msg.device_id)
751 p2p_dev_addr = msg.device_id;
752 else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700753 p2p_dbg(p2p, "Ignore scan data without P2P Device Info or P2P Device Id");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700754 p2p_parse_free(&msg);
755 return -1;
756 }
757
758 if (!is_zero_ether_addr(p2p->peer_filter) &&
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000759 !ether_addr_equal(p2p_dev_addr, p2p->peer_filter)) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700760 p2p_dbg(p2p, "Do not add peer filter for " MACSTR
761 " due to peer filter", MAC2STR(p2p_dev_addr));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800762 p2p_parse_free(&msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700763 return 0;
764 }
765
766 dev = p2p_create_device(p2p, p2p_dev_addr);
767 if (dev == NULL) {
768 p2p_parse_free(&msg);
769 return -1;
770 }
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800771
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800772 if (rx_time == NULL) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800773 os_get_reltime(&time_now);
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800774 rx_time = &time_now;
775 }
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800776
777 /*
778 * Update the device entry only if the new peer
Dmitry Shmidta3dc3092015-06-23 11:21:28 -0700779 * entry is newer than the one previously stored, or if
780 * the device was previously seen as a P2P Client in a group
781 * and the new entry isn't older than a threshold.
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800782 */
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800783 if (dev->last_seen.sec > 0 &&
Dmitry Shmidta3dc3092015-06-23 11:21:28 -0700784 os_reltime_before(rx_time, &dev->last_seen) &&
785 (!(dev->flags & P2P_DEV_LAST_SEEN_AS_GROUP_CLIENT) ||
786 os_reltime_expired(&dev->last_seen, rx_time,
787 P2P_DEV_GROUP_CLIENT_RESP_THRESHOLD))) {
788 p2p_dbg(p2p,
789 "Do not update peer entry based on old frame (rx_time=%u.%06u last_seen=%u.%06u flags=0x%x)",
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800790 (unsigned int) rx_time->sec,
791 (unsigned int) rx_time->usec,
792 (unsigned int) dev->last_seen.sec,
Dmitry Shmidta3dc3092015-06-23 11:21:28 -0700793 (unsigned int) dev->last_seen.usec,
794 dev->flags);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800795 p2p_parse_free(&msg);
796 return -1;
797 }
798
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800799 os_memcpy(&dev->last_seen, rx_time, sizeof(struct os_reltime));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800800
Dmitry Shmidta3dc3092015-06-23 11:21:28 -0700801 dev->flags &= ~(P2P_DEV_PROBE_REQ_ONLY | P2P_DEV_GROUP_CLIENT_ONLY |
802 P2P_DEV_LAST_SEEN_AS_GROUP_CLIENT);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700803
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000804 if (!ether_addr_equal(addr, p2p_dev_addr))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700805 os_memcpy(dev->interface_addr, addr, ETH_ALEN);
806 if (msg.ssid &&
Jouni Malinenfdb708a2015-04-07 11:32:11 +0300807 msg.ssid[1] <= sizeof(dev->oper_ssid) &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700808 (msg.ssid[1] != P2P_WILDCARD_SSID_LEN ||
809 os_memcmp(msg.ssid + 2, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN)
810 != 0)) {
811 os_memcpy(dev->oper_ssid, msg.ssid + 2, msg.ssid[1]);
812 dev->oper_ssid_len = msg.ssid[1];
813 }
814
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -0700815 wpabuf_free(dev->info.p2ps_instance);
816 dev->info.p2ps_instance = NULL;
817 if (msg.adv_service_instance && msg.adv_service_instance_len)
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800818 dev->info.p2ps_instance = wpabuf_alloc_copy(
819 msg.adv_service_instance, msg.adv_service_instance_len);
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800820
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700821 if (freq >= 2412 && freq <= 2484 && msg.ds_params &&
822 *msg.ds_params >= 1 && *msg.ds_params <= 14) {
823 int ds_freq;
824 if (*msg.ds_params == 14)
825 ds_freq = 2484;
826 else
827 ds_freq = 2407 + *msg.ds_params * 5;
828 if (freq != ds_freq) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700829 p2p_dbg(p2p, "Update Listen frequency based on DS Parameter Set IE: %d -> %d MHz",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700830 freq, ds_freq);
831 freq = ds_freq;
832 }
833 }
834
Dmitry Shmidt04949592012-07-19 12:16:46 -0700835 if (dev->listen_freq && dev->listen_freq != freq && scan_res) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700836 p2p_dbg(p2p, "Update Listen frequency based on scan results ("
837 MACSTR " %d -> %d MHz (DS param %d)",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700838 MAC2STR(dev->info.p2p_device_addr), dev->listen_freq,
839 freq, msg.ds_params ? *msg.ds_params : -1);
840 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700841 if (scan_res) {
842 dev->listen_freq = freq;
843 if (msg.group_info)
844 dev->oper_freq = freq;
845 }
Jouni Malinen75ecf522011-06-27 15:19:46 -0700846 dev->info.level = level;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700847
Dmitry Shmidt29333592017-01-09 12:27:11 -0800848 dev_name_changed = os_strncmp(dev->info.device_name, msg.device_name,
849 WPS_DEV_NAME_MAX_LEN) != 0;
850
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700851 p2p_copy_wps_info(p2p, dev, 0, &msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700852
853 for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) {
854 wpabuf_free(dev->info.wps_vendor_ext[i]);
855 dev->info.wps_vendor_ext[i] = NULL;
856 }
857
858 for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) {
859 if (msg.wps_vendor_ext[i] == NULL)
860 break;
861 dev->info.wps_vendor_ext[i] = wpabuf_alloc_copy(
862 msg.wps_vendor_ext[i], msg.wps_vendor_ext_len[i]);
863 if (dev->info.wps_vendor_ext[i] == NULL)
864 break;
865 }
866
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800867 wfd_changed = p2p_compare_wfd_info(dev, &msg);
868
Dmitry Shmidt29333592017-01-09 12:27:11 -0800869 if (wfd_changed) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700870 wpabuf_free(dev->info.wfd_subelems);
Dmitry Shmidt29333592017-01-09 12:27:11 -0800871 if (msg.wfd_subelems)
872 dev->info.wfd_subelems = wpabuf_dup(msg.wfd_subelems);
873 else
874 dev->info.wfd_subelems = NULL;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700875 }
876
Dmitry Shmidt04949592012-07-19 12:16:46 -0700877 if (scan_res) {
878 p2p_add_group_clients(p2p, p2p_dev_addr, addr, freq,
Dmitry Shmidta3dc3092015-06-23 11:21:28 -0700879 msg.group_info, msg.group_info_len,
880 rx_time);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700881 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700882
883 p2p_parse_free(&msg);
884
Dmitry Shmidt2e67f062014-07-16 09:55:28 -0700885 p2p_update_peer_vendor_elems(dev, ies, ies_len);
886
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800887 if (dev->flags & P2P_DEV_REPORTED && !wfd_changed &&
Dmitry Shmidt29333592017-01-09 12:27:11 -0800888 !dev_name_changed &&
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800889 (!msg.adv_service_instance ||
890 (dev->flags & P2P_DEV_P2PS_REPORTED)))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700891 return 0;
892
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700893 p2p_dbg(p2p, "Peer found with Listen frequency %d MHz (rx_time=%u.%06u)",
894 freq, (unsigned int) rx_time->sec,
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800895 (unsigned int) rx_time->usec);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700896 if (dev->flags & P2P_DEV_USER_REJECTED) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700897 p2p_dbg(p2p, "Do not report rejected device");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700898 return 0;
899 }
900
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800901 if (dev->info.config_methods == 0 &&
902 (freq == 2412 || freq == 2437 || freq == 2462)) {
903 /*
904 * If we have only seen a Beacon frame from a GO, we do not yet
905 * know what WPS config methods it supports. Since some
906 * applications use config_methods value from P2P-DEVICE-FOUND
907 * events, postpone reporting this peer until we've fully
908 * discovered its capabilities.
909 *
910 * At least for now, do this only if the peer was detected on
911 * one of the social channels since that peer can be easily be
912 * found again and there are no limitations of having to use
913 * passive scan on this channels, so this can be done through
914 * Probe Response frame that includes the config_methods
915 * information.
916 */
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700917 p2p_dbg(p2p, "Do not report peer " MACSTR
918 " with unknown config methods", MAC2STR(addr));
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800919 return 0;
920 }
921
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700922 p2p->cfg->dev_found(p2p->cfg->cb_ctx, addr, &dev->info,
923 !(dev->flags & P2P_DEV_REPORTED_ONCE));
924 dev->flags |= P2P_DEV_REPORTED | P2P_DEV_REPORTED_ONCE;
925
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800926 if (msg.adv_service_instance)
927 dev->flags |= P2P_DEV_P2PS_REPORTED;
928
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700929 return 0;
930}
931
932
933static void p2p_device_free(struct p2p_data *p2p, struct p2p_device *dev)
934{
935 int i;
936
Dmitry Shmidt497c1d52011-07-21 15:19:46 -0700937 if (p2p->go_neg_peer == dev) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800938 /*
939 * If GO Negotiation is in progress, report that it has failed.
940 */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800941 p2p_go_neg_failed(p2p, -1);
Dmitry Shmidt497c1d52011-07-21 15:19:46 -0700942 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700943 if (p2p->invite_peer == dev)
944 p2p->invite_peer = NULL;
945 if (p2p->sd_peer == dev)
946 p2p->sd_peer = NULL;
947 if (p2p->pending_client_disc_go == dev)
948 p2p->pending_client_disc_go = NULL;
949
Dmitry Shmidtc55524a2011-07-07 11:18:38 -0700950 /* dev_lost() device, but only if it was previously dev_found() */
951 if (dev->flags & P2P_DEV_REPORTED_ONCE)
952 p2p->cfg->dev_lost(p2p->cfg->cb_ctx,
953 dev->info.p2p_device_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700954
955 for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) {
956 wpabuf_free(dev->info.wps_vendor_ext[i]);
957 dev->info.wps_vendor_ext[i] = NULL;
958 }
959
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700960 wpabuf_free(dev->info.wfd_subelems);
Dmitry Shmidt2e67f062014-07-16 09:55:28 -0700961 wpabuf_free(dev->info.vendor_elems);
Dmitry Shmidt413dde72014-04-11 10:23:22 -0700962 wpabuf_free(dev->go_neg_conf);
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800963 wpabuf_free(dev->info.p2ps_instance);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700964
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700965 os_free(dev);
966}
967
968
969static int p2p_get_next_prog_freq(struct p2p_data *p2p)
970{
971 struct p2p_channels *c;
972 struct p2p_reg_class *cla;
973 size_t cl, ch;
974 int found = 0;
975 u8 reg_class;
976 u8 channel;
977 int freq;
978
979 c = &p2p->cfg->channels;
980 for (cl = 0; cl < c->reg_classes; cl++) {
981 cla = &c->reg_class[cl];
982 if (cla->reg_class != p2p->last_prog_scan_class)
983 continue;
984 for (ch = 0; ch < cla->channels; ch++) {
985 if (cla->channel[ch] == p2p->last_prog_scan_chan) {
986 found = 1;
987 break;
988 }
989 }
990 if (found)
991 break;
992 }
993
994 if (!found) {
995 /* Start from beginning */
996 reg_class = c->reg_class[0].reg_class;
997 channel = c->reg_class[0].channel[0];
998 } else {
999 /* Pick the next channel */
1000 ch++;
1001 if (ch == cla->channels) {
1002 cl++;
1003 if (cl == c->reg_classes)
1004 cl = 0;
1005 ch = 0;
1006 }
1007 reg_class = c->reg_class[cl].reg_class;
1008 channel = c->reg_class[cl].channel[ch];
1009 }
1010
Dmitry Shmidt4b060592013-04-29 16:42:49 -07001011 freq = p2p_channel_to_freq(reg_class, channel);
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001012 p2p_dbg(p2p, "Next progressive search channel: reg_class %u channel %u -> %d MHz",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001013 reg_class, channel, freq);
1014 p2p->last_prog_scan_class = reg_class;
1015 p2p->last_prog_scan_chan = channel;
1016
1017 if (freq == 2412 || freq == 2437 || freq == 2462)
1018 return 0; /* No need to add social channels */
1019 return freq;
1020}
1021
1022
1023static void p2p_search(struct p2p_data *p2p)
1024{
1025 int freq = 0;
1026 enum p2p_scan_type type;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001027 u16 pw_id = DEV_PW_DEFAULT;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001028 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001029
1030 if (p2p->drv_in_listen) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001031 p2p_dbg(p2p, "Driver is still in Listen state - wait for it to end before continuing");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001032 return;
1033 }
1034 p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001035 p2p->pending_listen_wait_drv = false;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001036
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001037 if (p2p->find_pending_full &&
1038 (p2p->find_type == P2P_FIND_PROGRESSIVE ||
1039 p2p->find_type == P2P_FIND_START_WITH_FULL)) {
1040 type = P2P_SCAN_FULL;
1041 p2p_dbg(p2p, "Starting search (pending full scan)");
1042 p2p->find_pending_full = 0;
1043 } else if ((p2p->find_type == P2P_FIND_PROGRESSIVE &&
1044 (freq = p2p_get_next_prog_freq(p2p)) > 0) ||
1045 (p2p->find_type == P2P_FIND_START_WITH_FULL &&
1046 (freq = p2p->find_specified_freq) > 0)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001047 type = P2P_SCAN_SOCIAL_PLUS_ONE;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001048 p2p_dbg(p2p, "Starting search (+ freq %u)", freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001049 } else {
1050 type = P2P_SCAN_SOCIAL;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001051 p2p_dbg(p2p, "Starting search");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001052 }
1053
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001054 res = p2p->cfg->p2p_scan(p2p->cfg->cb_ctx, type, freq,
1055 p2p->num_req_dev_types, p2p->req_dev_types,
Hai Shaloma20dcd72022-02-04 13:43:00 -08001056 p2p->find_dev_id, pw_id, p2p->include_6ghz);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001057 if (res < 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001058 p2p_dbg(p2p, "Scan request schedule failed");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001059 p2p_continue_find(p2p);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001060 }
1061}
1062
1063
1064static void p2p_find_timeout(void *eloop_ctx, void *timeout_ctx)
1065{
1066 struct p2p_data *p2p = eloop_ctx;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001067 p2p_dbg(p2p, "Find timeout -> stop");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001068 p2p_stop_find(p2p);
1069}
1070
1071
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001072void p2p_notify_scan_trigger_status(struct p2p_data *p2p, int status)
1073{
1074 if (status != 0) {
1075 p2p_dbg(p2p, "Scan request failed");
1076 /* Do continue find even for the first p2p_find_scan */
1077 p2p_continue_find(p2p);
1078 } else {
1079 p2p_dbg(p2p, "Running p2p_scan");
1080 p2p->p2p_scan_running = 1;
1081 eloop_cancel_timeout(p2p_scan_timeout, p2p, NULL);
1082 eloop_register_timeout(P2P_SCAN_TIMEOUT, 0, p2p_scan_timeout,
1083 p2p, NULL);
1084 }
1085}
1086
1087
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001088static int p2p_run_after_scan(struct p2p_data *p2p)
1089{
1090 struct p2p_device *dev;
1091 enum p2p_after_scan op;
1092
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001093 op = p2p->start_after_scan;
1094 p2p->start_after_scan = P2P_AFTER_SCAN_NOTHING;
1095 switch (op) {
1096 case P2P_AFTER_SCAN_NOTHING:
1097 break;
1098 case P2P_AFTER_SCAN_LISTEN:
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001099 p2p_dbg(p2p, "Start previously requested Listen state");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001100 p2p_listen(p2p, p2p->pending_listen_sec * 1000 +
1101 p2p->pending_listen_usec / 1000);
1102 return 1;
1103 case P2P_AFTER_SCAN_CONNECT:
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001104 p2p_dbg(p2p, "Start previously requested connect with " MACSTR,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001105 MAC2STR(p2p->after_scan_peer));
1106 dev = p2p_get_device(p2p, p2p->after_scan_peer);
1107 if (dev == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001108 p2p_dbg(p2p, "Peer not known anymore");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001109 break;
1110 }
1111 p2p_connect_send(p2p, dev);
1112 return 1;
1113 }
1114
1115 return 0;
1116}
1117
1118
1119static void p2p_scan_timeout(void *eloop_ctx, void *timeout_ctx)
1120{
1121 struct p2p_data *p2p = eloop_ctx;
1122 int running;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001123 p2p_dbg(p2p, "p2p_scan timeout (running=%d)", p2p->p2p_scan_running);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001124 running = p2p->p2p_scan_running;
1125 /* Make sure we recover from missed scan results callback */
1126 p2p->p2p_scan_running = 0;
1127
1128 if (running)
1129 p2p_run_after_scan(p2p);
1130}
1131
1132
1133static void p2p_free_req_dev_types(struct p2p_data *p2p)
1134{
1135 p2p->num_req_dev_types = 0;
1136 os_free(p2p->req_dev_types);
1137 p2p->req_dev_types = NULL;
1138}
1139
1140
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001141static int p2ps_gen_hash(struct p2p_data *p2p, const char *str, u8 *hash)
1142{
1143 u8 buf[SHA256_MAC_LEN];
1144 char str_buf[256];
1145 const u8 *adv_array;
1146 size_t i, adv_len;
1147
1148 if (!str || !hash)
1149 return 0;
1150
1151 if (!str[0]) {
1152 os_memcpy(hash, p2p->wild_card_hash, P2PS_HASH_LEN);
1153 return 1;
1154 }
1155
1156 adv_array = (u8 *) str_buf;
1157 adv_len = os_strlen(str);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07001158 if (adv_len >= sizeof(str_buf))
1159 return 0;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001160
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001161 for (i = 0; i < adv_len; i++) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001162 if (str[i] >= 'A' && str[i] <= 'Z')
1163 str_buf[i] = str[i] - 'A' + 'a';
1164 else
1165 str_buf[i] = str[i];
1166 }
1167
1168 if (sha256_vector(1, &adv_array, &adv_len, buf))
1169 return 0;
1170
1171 os_memcpy(hash, buf, P2PS_HASH_LEN);
1172 return 1;
1173}
1174
1175
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001176int p2p_find(struct p2p_data *p2p, unsigned int timeout,
1177 enum p2p_discovery_type type,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001178 unsigned int num_req_dev_types, const u8 *req_dev_types,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001179 const u8 *dev_id, unsigned int search_delay,
Hai Shaloma20dcd72022-02-04 13:43:00 -08001180 u8 seek_count, const char **seek, int freq, bool include_6ghz)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001181{
1182 int res;
Hai Shalom74f70d42019-02-11 14:42:39 -08001183 struct os_reltime start;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001184
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001185 p2p_dbg(p2p, "Starting find (type=%d)", type);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001186 if (p2p->p2p_scan_running) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001187 p2p_dbg(p2p, "p2p_scan is already running");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001188 }
1189
1190 p2p_free_req_dev_types(p2p);
1191 if (req_dev_types && num_req_dev_types) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001192 p2p->req_dev_types = os_memdup(req_dev_types,
1193 num_req_dev_types *
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001194 WPS_DEV_TYPE_LEN);
1195 if (p2p->req_dev_types == NULL)
1196 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001197 p2p->num_req_dev_types = num_req_dev_types;
1198 }
1199
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001200 if (dev_id) {
1201 os_memcpy(p2p->find_dev_id_buf, dev_id, ETH_ALEN);
1202 p2p->find_dev_id = p2p->find_dev_id_buf;
1203 } else
1204 p2p->find_dev_id = NULL;
Hai Shaloma20dcd72022-02-04 13:43:00 -08001205 p2p->include_6ghz = p2p_wfd_enabled(p2p) && include_6ghz;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001206 if (seek_count == 0 || !seek) {
1207 /* Not an ASP search */
1208 p2p->p2ps_seek = 0;
1209 } else if (seek_count == 1 && seek && (!seek[0] || !seek[0][0])) {
1210 /*
1211 * An empty seek string means no hash values, but still an ASP
1212 * search.
1213 */
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07001214 p2p_dbg(p2p, "ASP search");
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001215 p2p->p2ps_seek_count = 0;
1216 p2p->p2ps_seek = 1;
1217 } else if (seek && seek_count <= P2P_MAX_QUERY_HASH) {
1218 u8 buf[P2PS_HASH_LEN];
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07001219 int i, count = 0;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001220
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001221 for (i = 0; i < seek_count; i++) {
1222 if (!p2ps_gen_hash(p2p, seek[i], buf))
1223 continue;
1224
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07001225 p2p_dbg(p2p, "Seek service %s hash " MACSTR,
1226 seek[i], MAC2STR(buf));
1227 os_memcpy(&p2p->p2ps_seek_hash[count * P2PS_HASH_LEN],
1228 buf, P2PS_HASH_LEN);
1229 count++;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001230 }
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07001231
1232 p2p->p2ps_seek_count = count;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001233 p2p->p2ps_seek = 1;
1234 } else {
1235 p2p->p2ps_seek_count = 0;
1236 p2p->p2ps_seek = 1;
1237 }
1238
1239 /* Special case to perform wildcard search */
1240 if (p2p->p2ps_seek_count == 0 && p2p->p2ps_seek) {
1241 p2p->p2ps_seek_count = 1;
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07001242 os_memcpy(&p2p->p2ps_seek_hash, p2p->wild_card_hash,
1243 P2PS_HASH_LEN);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001244 }
1245
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001246 p2p->start_after_scan = P2P_AFTER_SCAN_NOTHING;
1247 p2p_clear_timeout(p2p);
Dmitry Shmidtde47be72016-01-07 12:52:55 -08001248 if (p2p->pending_listen_freq) {
1249 p2p_dbg(p2p, "Clear pending_listen_freq for p2p_find");
1250 p2p->pending_listen_freq = 0;
1251 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001252 p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001253 p2p->pending_listen_wait_drv = false;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001254 p2p->find_pending_full = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001255 p2p->find_type = type;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001256 if (freq != 2412 && freq != 2437 && freq != 2462 && freq != 60480)
1257 p2p->find_specified_freq = freq;
1258 else
1259 p2p->find_specified_freq = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001260 p2p_device_clear_reported(p2p);
Dmitry Shmidtde47be72016-01-07 12:52:55 -08001261 os_memset(p2p->sd_query_no_ack, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001262 p2p_set_state(p2p, P2P_SEARCH);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001263 p2p->search_delay = search_delay;
1264 p2p->in_search_delay = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001265 eloop_cancel_timeout(p2p_find_timeout, p2p, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001266 p2p->last_p2p_find_timeout = timeout;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001267 if (timeout)
1268 eloop_register_timeout(timeout, 0, p2p_find_timeout,
1269 p2p, NULL);
Hai Shalom74f70d42019-02-11 14:42:39 -08001270 os_get_reltime(&start);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001271 switch (type) {
1272 case P2P_FIND_START_WITH_FULL:
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001273 if (freq > 0) {
1274 /*
1275 * Start with the specified channel and then move to
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001276 * scans for social channels and this specific channel.
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001277 */
1278 res = p2p->cfg->p2p_scan(p2p->cfg->cb_ctx,
1279 P2P_SCAN_SPECIFIC, freq,
1280 p2p->num_req_dev_types,
1281 p2p->req_dev_types, dev_id,
Hai Shaloma20dcd72022-02-04 13:43:00 -08001282 DEV_PW_DEFAULT,
1283 p2p->include_6ghz);
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001284 break;
1285 }
1286 /* fall through */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001287 case P2P_FIND_PROGRESSIVE:
1288 res = p2p->cfg->p2p_scan(p2p->cfg->cb_ctx, P2P_SCAN_FULL, 0,
1289 p2p->num_req_dev_types,
Dmitry Shmidt04949592012-07-19 12:16:46 -07001290 p2p->req_dev_types, dev_id,
Hai Shaloma20dcd72022-02-04 13:43:00 -08001291 DEV_PW_DEFAULT, p2p->include_6ghz);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001292 break;
1293 case P2P_FIND_ONLY_SOCIAL:
1294 res = p2p->cfg->p2p_scan(p2p->cfg->cb_ctx, P2P_SCAN_SOCIAL, 0,
1295 p2p->num_req_dev_types,
Dmitry Shmidt04949592012-07-19 12:16:46 -07001296 p2p->req_dev_types, dev_id,
Hai Shaloma20dcd72022-02-04 13:43:00 -08001297 DEV_PW_DEFAULT, p2p->include_6ghz);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001298 break;
1299 default:
1300 return -1;
1301 }
1302
Hai Shalom74f70d42019-02-11 14:42:39 -08001303 if (!res)
1304 p2p->find_start = start;
1305
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001306 if (res != 0 && p2p->p2p_scan_running) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001307 p2p_dbg(p2p, "Failed to start p2p_scan - another p2p_scan was already running");
1308 /* wait for the previous p2p_scan to complete */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001309 if (type == P2P_FIND_PROGRESSIVE ||
1310 (type == P2P_FIND_START_WITH_FULL && freq == 0))
1311 p2p->find_pending_full = 1;
Dmitry Shmidt96be6222014-02-13 10:16:51 -08001312 res = 0; /* do not report failure */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001313 } else if (res != 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001314 p2p_dbg(p2p, "Failed to start p2p_scan");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001315 p2p_set_state(p2p, P2P_IDLE);
1316 eloop_cancel_timeout(p2p_find_timeout, p2p, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001317 }
1318
1319 return res;
1320}
1321
1322
1323void p2p_stop_find_for_freq(struct p2p_data *p2p, int freq)
1324{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001325 p2p_dbg(p2p, "Stopping find");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001326 eloop_cancel_timeout(p2p_find_timeout, p2p, NULL);
1327 p2p_clear_timeout(p2p);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001328 if (p2p->state == P2P_SEARCH || p2p->state == P2P_SD_DURING_FIND)
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001329 p2p->cfg->find_stopped(p2p->cfg->cb_ctx);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001330
1331 p2p->p2ps_seek_count = 0;
1332
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001333 p2p_set_state(p2p, P2P_IDLE);
1334 p2p_free_req_dev_types(p2p);
1335 p2p->start_after_scan = P2P_AFTER_SCAN_NOTHING;
Dmitry Shmidt8c652892013-03-01 10:14:01 -08001336 if (p2p->go_neg_peer)
1337 p2p->go_neg_peer->flags &= ~P2P_DEV_PEER_WAITING_RESPONSE;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001338 p2p->go_neg_peer = NULL;
1339 p2p->sd_peer = NULL;
1340 p2p->invite_peer = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001341 p2p_stop_listen_for_freq(p2p, freq);
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001342 p2p->send_action_in_progress = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001343}
1344
1345
1346void p2p_stop_listen_for_freq(struct p2p_data *p2p, int freq)
1347{
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001348 p2p_dbg(p2p,
1349 "%s(freq=%d) pending_listen_freq=%d in_listen=%d drv_in_listen=%d",
1350 __func__, freq, p2p->pending_listen_freq, p2p->in_listen,
1351 p2p->drv_in_listen);
Hai Shaloma20dcd72022-02-04 13:43:00 -08001352 if (freq > 0 &&
1353 ((p2p->drv_in_listen == freq && p2p->in_listen) ||
1354 p2p->pending_listen_freq == (unsigned int) freq)) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001355 p2p_dbg(p2p, "Skip stop_listen since we are on correct channel for response");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001356 return;
1357 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001358 if (p2p->in_listen) {
1359 p2p->in_listen = 0;
1360 p2p_clear_timeout(p2p);
1361 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001362 if (p2p->drv_in_listen) {
1363 /*
1364 * The driver may not deliver callback to p2p_listen_end()
1365 * when the operation gets canceled, so clear the internal
1366 * variable that is tracking driver state.
1367 */
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001368 p2p_dbg(p2p, "Clear drv_in_listen (%d)", p2p->drv_in_listen);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001369 p2p->drv_in_listen = 0;
1370 }
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001371 if (p2p->pending_listen_freq &&
1372 p2p->pending_listen_freq != (unsigned int) freq &&
1373 !p2p->drv_in_listen && p2p->pending_listen_wait_drv) {
1374 p2p_dbg(p2p,
1375 "Clear pending_listen_freq since the started listen did not complete before being stopped");
1376 p2p->pending_listen_freq = 0;
1377 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001378 p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001379 p2p->pending_listen_wait_drv = false;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001380}
1381
1382
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001383void p2p_stop_listen(struct p2p_data *p2p)
1384{
1385 if (p2p->state != P2P_LISTEN_ONLY) {
1386 p2p_dbg(p2p, "Skip stop_listen since not in listen_only state.");
1387 return;
1388 }
1389
1390 p2p_stop_listen_for_freq(p2p, 0);
1391 p2p_set_state(p2p, P2P_IDLE);
1392}
1393
1394
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001395void p2p_stop_find(struct p2p_data *p2p)
1396{
Dmitry Shmidt3c57b3f2014-05-22 15:13:07 -07001397 p2p->pending_listen_freq = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001398 p2p_stop_find_for_freq(p2p, 0);
1399}
1400
1401
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001402static int p2p_prepare_channel_pref(struct p2p_data *p2p,
1403 unsigned int force_freq,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001404 unsigned int pref_freq, int go)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001405{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001406 u8 op_class, op_channel;
1407 unsigned int freq = force_freq ? force_freq : pref_freq;
1408
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001409 p2p_dbg(p2p, "Prepare channel pref - force_freq=%u pref_freq=%u go=%d",
1410 force_freq, pref_freq, go);
Dmitry Shmidt4b060592013-04-29 16:42:49 -07001411 if (p2p_freq_to_channel(freq, &op_class, &op_channel) < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001412 p2p_dbg(p2p, "Unsupported frequency %u MHz", freq);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001413 return -1;
1414 }
1415
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001416 if (!p2p_channels_includes(&p2p->cfg->channels, op_class, op_channel) &&
1417 (go || !p2p_channels_includes(&p2p->cfg->cli_channels, op_class,
1418 op_channel))) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001419 p2p_dbg(p2p, "Frequency %u MHz (oper_class %u channel %u) not allowed for P2P",
1420 freq, op_class, op_channel);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001421 return -1;
1422 }
1423
1424 p2p->op_reg_class = op_class;
1425 p2p->op_channel = op_channel;
1426
1427 if (force_freq) {
1428 p2p->channels.reg_classes = 1;
1429 p2p->channels.reg_class[0].channels = 1;
1430 p2p->channels.reg_class[0].reg_class = p2p->op_reg_class;
1431 p2p->channels.reg_class[0].channel[0] = p2p->op_channel;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001432 } else {
Hai Shaloma20dcd72022-02-04 13:43:00 -08001433 p2p_copy_channels(&p2p->channels, &p2p->cfg->channels,
1434 p2p->allow_6ghz);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001435 }
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001436
1437 return 0;
1438}
1439
1440
1441static void p2p_prepare_channel_best(struct p2p_data *p2p)
1442{
1443 u8 op_class, op_channel;
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07001444 const int op_classes_5ghz[] = { 124, 125, 115, 0 };
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08001445 const int op_classes_ht40[] = { 126, 127, 116, 117, 0 };
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001446 const int op_classes_vht[] = { 128, 0 };
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001447 const int op_classes_edmg[] = { 181, 182, 183, 0 };
Hai Shaloma20dcd72022-02-04 13:43:00 -08001448 const int op_classes_6ghz[] = { 131, 0 };
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001449
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07001450 p2p_dbg(p2p, "Prepare channel best");
1451
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001452 if (!p2p->cfg->cfg_op_channel && p2p->best_freq_overall > 0 &&
1453 p2p_supported_freq(p2p, p2p->best_freq_overall) &&
Dmitry Shmidt4b060592013-04-29 16:42:49 -07001454 p2p_freq_to_channel(p2p->best_freq_overall, &op_class, &op_channel)
1455 == 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001456 p2p_dbg(p2p, "Select best overall channel as operating channel preference");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001457 p2p->op_reg_class = op_class;
1458 p2p->op_channel = op_channel;
1459 } else if (!p2p->cfg->cfg_op_channel && p2p->best_freq_5 > 0 &&
1460 p2p_supported_freq(p2p, p2p->best_freq_5) &&
Dmitry Shmidt4b060592013-04-29 16:42:49 -07001461 p2p_freq_to_channel(p2p->best_freq_5, &op_class, &op_channel)
1462 == 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001463 p2p_dbg(p2p, "Select best 5 GHz channel as operating channel preference");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001464 p2p->op_reg_class = op_class;
1465 p2p->op_channel = op_channel;
1466 } else if (!p2p->cfg->cfg_op_channel && p2p->best_freq_24 > 0 &&
1467 p2p_supported_freq(p2p, p2p->best_freq_24) &&
Dmitry Shmidt4b060592013-04-29 16:42:49 -07001468 p2p_freq_to_channel(p2p->best_freq_24, &op_class,
1469 &op_channel) == 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001470 p2p_dbg(p2p, "Select best 2.4 GHz channel as operating channel preference");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001471 p2p->op_reg_class = op_class;
1472 p2p->op_channel = op_channel;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07001473 } else if (p2p->cfg->num_pref_chan > 0 &&
1474 p2p_channels_includes(&p2p->cfg->channels,
1475 p2p->cfg->pref_chan[0].op_class,
1476 p2p->cfg->pref_chan[0].chan)) {
1477 p2p_dbg(p2p, "Select first pref_chan entry as operating channel preference");
1478 p2p->op_reg_class = p2p->cfg->pref_chan[0].op_class;
1479 p2p->op_channel = p2p->cfg->pref_chan[0].chan;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001480 } else if (p2p_channel_select(&p2p->cfg->channels, op_classes_edmg,
1481 &p2p->op_reg_class, &p2p->op_channel) ==
1482 0) {
1483 p2p_dbg(p2p, "Select possible EDMG channel (op_class %u channel %u) as operating channel preference",
1484 p2p->op_reg_class, p2p->op_channel);
Hai Shaloma20dcd72022-02-04 13:43:00 -08001485 } else if (p2p->allow_6ghz &&
1486 (p2p_channel_select(&p2p->cfg->channels, op_classes_6ghz,
1487 &p2p->op_reg_class, &p2p->op_channel) ==
1488 0)) {
1489 p2p_dbg(p2p, "Select possible 6 GHz channel (op_class %u channel %u) as operating channel preference",
1490 p2p->op_reg_class, p2p->op_channel);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001491 } else if (p2p_channel_select(&p2p->cfg->channels, op_classes_vht,
1492 &p2p->op_reg_class, &p2p->op_channel) ==
1493 0) {
1494 p2p_dbg(p2p, "Select possible VHT channel (op_class %u channel %u) as operating channel preference",
1495 p2p->op_reg_class, p2p->op_channel);
1496 } else if (p2p_channel_select(&p2p->cfg->channels, op_classes_ht40,
1497 &p2p->op_reg_class, &p2p->op_channel) ==
1498 0) {
1499 p2p_dbg(p2p, "Select possible HT40 channel (op_class %u channel %u) as operating channel preference",
1500 p2p->op_reg_class, p2p->op_channel);
1501 } else if (p2p_channel_select(&p2p->cfg->channels, op_classes_5ghz,
1502 &p2p->op_reg_class, &p2p->op_channel) ==
1503 0) {
1504 p2p_dbg(p2p, "Select possible 5 GHz channel (op_class %u channel %u) as operating channel preference",
1505 p2p->op_reg_class, p2p->op_channel);
Dmitry Shmidtd11f0192014-03-24 12:09:47 -07001506 } else if (p2p_channels_includes(&p2p->cfg->channels,
1507 p2p->cfg->op_reg_class,
1508 p2p->cfg->op_channel)) {
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07001509 p2p_dbg(p2p, "Select pre-configured channel as operating channel preference");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001510 p2p->op_reg_class = p2p->cfg->op_reg_class;
1511 p2p->op_channel = p2p->cfg->op_channel;
Dmitry Shmidtd11f0192014-03-24 12:09:47 -07001512 } else if (p2p_channel_random_social(&p2p->cfg->channels,
1513 &p2p->op_reg_class,
Hai Shalom74f70d42019-02-11 14:42:39 -08001514 &p2p->op_channel,
1515 NULL, NULL) == 0) {
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001516 p2p_dbg(p2p, "Select random available social channel (op_class %u channel %u) as operating channel preference",
1517 p2p->op_reg_class, p2p->op_channel);
Dmitry Shmidtd11f0192014-03-24 12:09:47 -07001518 } else {
1519 /* Select any random available channel from the first available
1520 * operating class */
Sunil Ravi2a14cf12023-11-21 00:54:38 +00001521 if (p2p_channel_select(&p2p->cfg->channels, NULL,
1522 &p2p->op_reg_class,
1523 &p2p->op_channel) == 0)
1524 p2p_dbg(p2p,
1525 "Select random available channel %d from operating class %d as operating channel preference",
1526 p2p->op_channel, p2p->op_reg_class);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001527 }
1528
Hai Shaloma20dcd72022-02-04 13:43:00 -08001529 p2p_copy_channels(&p2p->channels, &p2p->cfg->channels, p2p->allow_6ghz);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001530}
1531
1532
1533/**
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001534 * p2p_prepare_channel - Select operating channel for GO Negotiation or P2PS PD
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001535 * @p2p: P2P module context from p2p_init()
1536 * @dev: Selected peer device
1537 * @force_freq: Forced frequency in MHz or 0 if not forced
1538 * @pref_freq: Preferred frequency in MHz or 0 if no preference
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001539 * @go: Whether the local end will be forced to be GO
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001540 * Returns: 0 on success, -1 on failure (channel not supported for P2P)
1541 *
1542 * This function is used to do initial operating channel selection for GO
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001543 * Negotiation prior to having received peer information or for P2PS PD
1544 * signalling. The selected channel may be further optimized in
1545 * p2p_reselect_channel() once the peer information is available.
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001546 */
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08001547int p2p_prepare_channel(struct p2p_data *p2p, struct p2p_device *dev,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001548 unsigned int force_freq, unsigned int pref_freq, int go)
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001549{
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001550 p2p_dbg(p2p, "Prepare channel - force_freq=%u pref_freq=%u go=%d",
1551 force_freq, pref_freq, go);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001552 if (force_freq || pref_freq) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001553 if (p2p_prepare_channel_pref(p2p, force_freq, pref_freq, go) <
1554 0)
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001555 return -1;
1556 } else {
1557 p2p_prepare_channel_best(p2p);
1558 }
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001559 p2p_channels_dump(p2p, "prepared channels", &p2p->channels);
1560 if (go)
1561 p2p_channels_remove_freqs(&p2p->channels, &p2p->no_go_freq);
1562 else if (!force_freq)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001563 p2p_channels_union_inplace(&p2p->channels,
1564 &p2p->cfg->cli_channels);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001565 p2p_channels_dump(p2p, "after go/cli filter/add", &p2p->channels);
1566
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001567 p2p_dbg(p2p, "Own preference for operation channel: Operating Class %u Channel %u%s",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001568 p2p->op_reg_class, p2p->op_channel,
1569 force_freq ? " (forced)" : "");
1570
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001571 if (force_freq)
1572 dev->flags |= P2P_DEV_FORCE_FREQ;
1573 else
1574 dev->flags &= ~P2P_DEV_FORCE_FREQ;
1575
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001576 return 0;
1577}
1578
1579
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001580static void p2p_set_dev_persistent(struct p2p_device *dev,
1581 int persistent_group)
1582{
1583 switch (persistent_group) {
1584 case 0:
1585 dev->flags &= ~(P2P_DEV_PREFER_PERSISTENT_GROUP |
1586 P2P_DEV_PREFER_PERSISTENT_RECONN);
1587 break;
1588 case 1:
1589 dev->flags |= P2P_DEV_PREFER_PERSISTENT_GROUP;
1590 dev->flags &= ~P2P_DEV_PREFER_PERSISTENT_RECONN;
1591 break;
1592 case 2:
1593 dev->flags |= P2P_DEV_PREFER_PERSISTENT_GROUP |
1594 P2P_DEV_PREFER_PERSISTENT_RECONN;
1595 break;
1596 }
1597}
1598
1599
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001600int p2p_connect(struct p2p_data *p2p, const u8 *peer_addr,
1601 enum p2p_wps_method wps_method,
1602 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07001603 unsigned int force_freq, int persistent_group,
1604 const u8 *force_ssid, size_t force_ssid_len,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001605 int pd_before_go_neg, unsigned int pref_freq, u16 oob_pw_id)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001606{
1607 struct p2p_device *dev;
1608
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001609 p2p_dbg(p2p, "Request to start group negotiation - peer=" MACSTR
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001610 " GO Intent=%d Intended Interface Address=" MACSTR
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001611 " wps_method=%d persistent_group=%d pd_before_go_neg=%d "
Hai Shaloma20dcd72022-02-04 13:43:00 -08001612 "oob_pw_id=%u allow_6ghz=%d",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001613 MAC2STR(peer_addr), go_intent, MAC2STR(own_interface_addr),
Hai Shaloma20dcd72022-02-04 13:43:00 -08001614 wps_method, persistent_group, pd_before_go_neg, oob_pw_id,
1615 p2p->allow_6ghz);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001616
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001617 dev = p2p_get_device(p2p, peer_addr);
1618 if (dev == NULL || (dev->flags & P2P_DEV_PROBE_REQ_ONLY)) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001619 p2p_dbg(p2p, "Cannot connect to unknown P2P Device " MACSTR,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001620 MAC2STR(peer_addr));
1621 return -1;
1622 }
1623
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001624 if (p2p_prepare_channel(p2p, dev, force_freq, pref_freq,
1625 go_intent == 15) < 0)
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001626 return -1;
1627
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001628 if (dev->flags & P2P_DEV_GROUP_CLIENT_ONLY) {
1629 if (!(dev->info.dev_capab &
1630 P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY)) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001631 p2p_dbg(p2p, "Cannot connect to P2P Device " MACSTR
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001632 " that is in a group and is not discoverable",
1633 MAC2STR(peer_addr));
1634 return -1;
1635 }
1636 if (dev->oper_freq <= 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001637 p2p_dbg(p2p, "Cannot connect to P2P Device " MACSTR
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001638 " with incomplete information",
1639 MAC2STR(peer_addr));
1640 return -1;
1641 }
1642
1643 /*
1644 * First, try to connect directly. If the peer does not
1645 * acknowledge frames, assume it is sleeping and use device
1646 * discoverability via the GO at that point.
1647 */
1648 }
1649
Dmitry Shmidt04949592012-07-19 12:16:46 -07001650 p2p->ssid_set = 0;
1651 if (force_ssid) {
1652 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Forced SSID",
1653 force_ssid, force_ssid_len);
1654 os_memcpy(p2p->ssid, force_ssid, force_ssid_len);
1655 p2p->ssid_len = force_ssid_len;
1656 p2p->ssid_set = 1;
1657 }
1658
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001659 dev->flags &= ~P2P_DEV_NOT_YET_READY;
1660 dev->flags &= ~P2P_DEV_USER_REJECTED;
1661 dev->flags &= ~P2P_DEV_WAIT_GO_NEG_RESPONSE;
1662 dev->flags &= ~P2P_DEV_WAIT_GO_NEG_CONFIRM;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001663 if (pd_before_go_neg)
1664 dev->flags |= P2P_DEV_PD_BEFORE_GO_NEG;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001665 else {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001666 dev->flags &= ~P2P_DEV_PD_BEFORE_GO_NEG;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001667 /*
Dmitry Shmidt9cdf1b92013-02-27 12:58:50 -08001668 * Assign dialog token and tie breaker here to use the same
1669 * values in each retry within the same GO Negotiation exchange.
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001670 */
1671 dev->dialog_token++;
1672 if (dev->dialog_token == 0)
1673 dev->dialog_token = 1;
Dmitry Shmidt9cdf1b92013-02-27 12:58:50 -08001674 dev->tie_breaker = p2p->next_tie_breaker;
1675 p2p->next_tie_breaker = !p2p->next_tie_breaker;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001676 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001677 dev->connect_reqs = 0;
1678 dev->go_neg_req_sent = 0;
1679 dev->go_state = UNKNOWN_GO;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001680 p2p_set_dev_persistent(dev, persistent_group);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001681 p2p->go_intent = go_intent;
1682 os_memcpy(p2p->intended_addr, own_interface_addr, ETH_ALEN);
1683
1684 if (p2p->state != P2P_IDLE)
1685 p2p_stop_find(p2p);
1686
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001687 dev->wps_method = wps_method;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001688 dev->oob_pw_id = oob_pw_id;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001689 dev->status = P2P_SC_SUCCESS;
1690
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001691 if (p2p->p2p_scan_running) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001692 p2p_dbg(p2p, "p2p_scan running - delay connect send");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001693 p2p->start_after_scan = P2P_AFTER_SCAN_CONNECT;
1694 os_memcpy(p2p->after_scan_peer, peer_addr, ETH_ALEN);
1695 return 0;
1696 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001697
1698 return p2p_connect_send(p2p, dev);
1699}
1700
1701
1702int p2p_authorize(struct p2p_data *p2p, const u8 *peer_addr,
1703 enum p2p_wps_method wps_method,
1704 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07001705 unsigned int force_freq, int persistent_group,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001706 const u8 *force_ssid, size_t force_ssid_len,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001707 unsigned int pref_freq, u16 oob_pw_id)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001708{
1709 struct p2p_device *dev;
1710
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001711 p2p_dbg(p2p, "Request to authorize group negotiation - peer=" MACSTR
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001712 " GO Intent=%d Intended Interface Address=" MACSTR
Hai Shaloma20dcd72022-02-04 13:43:00 -08001713 " wps_method=%d persistent_group=%d oob_pw_id=%u allow_6ghz=%d",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001714 MAC2STR(peer_addr), go_intent, MAC2STR(own_interface_addr),
Hai Shaloma20dcd72022-02-04 13:43:00 -08001715 wps_method, persistent_group, oob_pw_id, p2p->allow_6ghz);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001716
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001717 dev = p2p_get_device(p2p, peer_addr);
1718 if (dev == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001719 p2p_dbg(p2p, "Cannot authorize unknown P2P Device " MACSTR,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001720 MAC2STR(peer_addr));
1721 return -1;
1722 }
1723
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001724 if (p2p_prepare_channel(p2p, dev, force_freq, pref_freq, go_intent ==
1725 15) < 0)
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001726 return -1;
1727
Dmitry Shmidt04949592012-07-19 12:16:46 -07001728 p2p->ssid_set = 0;
1729 if (force_ssid) {
1730 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Forced SSID",
1731 force_ssid, force_ssid_len);
1732 os_memcpy(p2p->ssid, force_ssid, force_ssid_len);
1733 p2p->ssid_len = force_ssid_len;
1734 p2p->ssid_set = 1;
1735 }
1736
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001737 dev->flags &= ~P2P_DEV_NOT_YET_READY;
1738 dev->flags &= ~P2P_DEV_USER_REJECTED;
1739 dev->go_neg_req_sent = 0;
1740 dev->go_state = UNKNOWN_GO;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001741 p2p_set_dev_persistent(dev, persistent_group);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001742 p2p->go_intent = go_intent;
1743 os_memcpy(p2p->intended_addr, own_interface_addr, ETH_ALEN);
1744
1745 dev->wps_method = wps_method;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001746 dev->oob_pw_id = oob_pw_id;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001747 dev->status = P2P_SC_SUCCESS;
1748
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001749 return 0;
1750}
1751
1752
1753void p2p_add_dev_info(struct p2p_data *p2p, const u8 *addr,
1754 struct p2p_device *dev, struct p2p_message *msg)
1755{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001756 os_get_reltime(&dev->last_seen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001757
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001758 p2p_copy_wps_info(p2p, dev, 0, msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001759
1760 if (msg->listen_channel) {
1761 int freq;
Dmitry Shmidt4b060592013-04-29 16:42:49 -07001762 freq = p2p_channel_to_freq(msg->listen_channel[3],
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001763 msg->listen_channel[4]);
1764 if (freq < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001765 p2p_dbg(p2p, "Unknown peer Listen channel: "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001766 "country=%c%c(0x%02x) reg_class=%u channel=%u",
1767 msg->listen_channel[0],
1768 msg->listen_channel[1],
1769 msg->listen_channel[2],
1770 msg->listen_channel[3],
1771 msg->listen_channel[4]);
1772 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001773 p2p_dbg(p2p, "Update peer " MACSTR
1774 " Listen channel: %u -> %u MHz",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001775 MAC2STR(dev->info.p2p_device_addr),
1776 dev->listen_freq, freq);
1777 dev->listen_freq = freq;
1778 }
1779 }
1780
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001781 if (msg->wfd_subelems) {
1782 wpabuf_free(dev->info.wfd_subelems);
1783 dev->info.wfd_subelems = wpabuf_dup(msg->wfd_subelems);
1784 }
1785
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001786 if (dev->flags & P2P_DEV_PROBE_REQ_ONLY) {
1787 dev->flags &= ~P2P_DEV_PROBE_REQ_ONLY;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001788 p2p_dbg(p2p, "Completed device entry based on data from GO Negotiation Request");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001789 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001790 p2p_dbg(p2p, "Created device entry based on GO Neg Req: "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001791 MACSTR " dev_capab=0x%x group_capab=0x%x name='%s' "
1792 "listen_freq=%d",
1793 MAC2STR(dev->info.p2p_device_addr),
1794 dev->info.dev_capab, dev->info.group_capab,
1795 dev->info.device_name, dev->listen_freq);
1796 }
1797
1798 dev->flags &= ~P2P_DEV_GROUP_CLIENT_ONLY;
1799
1800 if (dev->flags & P2P_DEV_USER_REJECTED) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001801 p2p_dbg(p2p, "Do not report rejected device");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001802 return;
1803 }
1804
1805 p2p->cfg->dev_found(p2p->cfg->cb_ctx, addr, &dev->info,
1806 !(dev->flags & P2P_DEV_REPORTED_ONCE));
1807 dev->flags |= P2P_DEV_REPORTED | P2P_DEV_REPORTED_ONCE;
1808}
1809
1810
1811void p2p_build_ssid(struct p2p_data *p2p, u8 *ssid, size_t *ssid_len)
1812{
1813 os_memcpy(ssid, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN);
1814 p2p_random((char *) &ssid[P2P_WILDCARD_SSID_LEN], 2);
1815 os_memcpy(&ssid[P2P_WILDCARD_SSID_LEN + 2],
1816 p2p->cfg->ssid_postfix, p2p->cfg->ssid_postfix_len);
1817 *ssid_len = P2P_WILDCARD_SSID_LEN + 2 + p2p->cfg->ssid_postfix_len;
1818}
1819
1820
1821int p2p_go_params(struct p2p_data *p2p, struct p2p_go_neg_results *params)
1822{
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001823 if (p2p->ssid_set) {
1824 os_memcpy(params->ssid, p2p->ssid, p2p->ssid_len);
1825 params->ssid_len = p2p->ssid_len;
1826 } else {
1827 p2p_build_ssid(p2p, params->ssid, &params->ssid_len);
1828 }
1829 p2p->ssid_set = 0;
1830
Jimmy Chen6d7e3902018-11-20 10:15:16 +08001831 if (p2p->passphrase_set) {
1832 os_memcpy(params->passphrase, p2p->passphrase, os_strlen(p2p->passphrase));
1833 } else {
1834 p2p_random(params->passphrase, p2p->cfg->passphrase_len);
Sunil8cd6f4d2022-06-28 18:40:46 +00001835 params->passphrase[p2p->cfg->passphrase_len] = '\0';
Jimmy Chen6d7e3902018-11-20 10:15:16 +08001836 }
1837 p2p->passphrase_set = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001838 return 0;
1839}
1840
1841
1842void p2p_go_complete(struct p2p_data *p2p, struct p2p_device *peer)
1843{
1844 struct p2p_go_neg_results res;
1845 int go = peer->go_state == LOCAL_GO;
1846 struct p2p_channels intersection;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001847
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001848 p2p_dbg(p2p, "GO Negotiation with " MACSTR " completed (%s will be GO)",
1849 MAC2STR(peer->info.p2p_device_addr), go ? "local end" : "peer");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001850
1851 os_memset(&res, 0, sizeof(res));
1852 res.role_go = go;
1853 os_memcpy(res.peer_device_addr, peer->info.p2p_device_addr, ETH_ALEN);
1854 os_memcpy(res.peer_interface_addr, peer->intended_addr, ETH_ALEN);
1855 res.wps_method = peer->wps_method;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001856 if (peer->flags & P2P_DEV_PREFER_PERSISTENT_GROUP) {
1857 if (peer->flags & P2P_DEV_PREFER_PERSISTENT_RECONN)
1858 res.persistent_group = 2;
1859 else
1860 res.persistent_group = 1;
1861 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001862
1863 if (go) {
1864 /* Setup AP mode for WPS provisioning */
Dmitry Shmidt4b060592013-04-29 16:42:49 -07001865 res.freq = p2p_channel_to_freq(p2p->op_reg_class,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001866 p2p->op_channel);
1867 os_memcpy(res.ssid, p2p->ssid, p2p->ssid_len);
1868 res.ssid_len = p2p->ssid_len;
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07001869 p2p_random(res.passphrase, p2p->cfg->passphrase_len);
Sunil8cd6f4d2022-06-28 18:40:46 +00001870 res.passphrase[p2p->cfg->passphrase_len] = '\0';
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001871 } else {
1872 res.freq = peer->oper_freq;
1873 if (p2p->ssid_len) {
1874 os_memcpy(res.ssid, p2p->ssid, p2p->ssid_len);
1875 res.ssid_len = p2p->ssid_len;
1876 }
1877 }
1878
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001879 p2p_channels_dump(p2p, "own channels", &p2p->channels);
1880 p2p_channels_dump(p2p, "peer channels", &peer->channels);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001881 p2p_channels_intersect(&p2p->channels, &peer->channels,
1882 &intersection);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001883 if (go) {
1884 p2p_channels_remove_freqs(&intersection, &p2p->no_go_freq);
1885 p2p_channels_dump(p2p, "intersection after no-GO removal",
1886 &intersection);
1887 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001888
1889 p2p_channels_to_freqs(&intersection, res.freq_list,
1890 P2P_MAX_CHANNELS);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001891
1892 res.peer_config_timeout = go ? peer->client_timeout : peer->go_timeout;
1893
1894 p2p_clear_timeout(p2p);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001895 p2p->ssid_set = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001896 peer->go_neg_req_sent = 0;
Dmitry Shmidt29333592017-01-09 12:27:11 -08001897 peer->flags &= ~P2P_DEV_PEER_WAITING_RESPONSE;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001898 peer->wps_method = WPS_NOT_READY;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001899 peer->oob_pw_id = 0;
Dmitry Shmidt413dde72014-04-11 10:23:22 -07001900 wpabuf_free(peer->go_neg_conf);
1901 peer->go_neg_conf = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001902
1903 p2p_set_state(p2p, P2P_PROVISIONING);
1904 p2p->cfg->go_neg_completed(p2p->cfg->cb_ctx, &res);
1905}
1906
1907
1908static void p2p_rx_p2p_action(struct p2p_data *p2p, const u8 *sa,
1909 const u8 *data, size_t len, int rx_freq)
1910{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001911 p2p_dbg(p2p, "RX P2P Public Action from " MACSTR, MAC2STR(sa));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001912 wpa_hexdump(MSG_MSGDUMP, "P2P: P2P Public Action contents", data, len);
1913
1914 if (len < 1)
1915 return;
1916
1917 switch (data[0]) {
1918 case P2P_GO_NEG_REQ:
1919 p2p_process_go_neg_req(p2p, sa, data + 1, len - 1, rx_freq);
1920 break;
1921 case P2P_GO_NEG_RESP:
1922 p2p_process_go_neg_resp(p2p, sa, data + 1, len - 1, rx_freq);
1923 break;
1924 case P2P_GO_NEG_CONF:
1925 p2p_process_go_neg_conf(p2p, sa, data + 1, len - 1);
1926 break;
1927 case P2P_INVITATION_REQ:
1928 p2p_process_invitation_req(p2p, sa, data + 1, len - 1,
1929 rx_freq);
1930 break;
1931 case P2P_INVITATION_RESP:
1932 p2p_process_invitation_resp(p2p, sa, data + 1, len - 1);
1933 break;
1934 case P2P_PROV_DISC_REQ:
1935 p2p_process_prov_disc_req(p2p, sa, data + 1, len - 1, rx_freq);
1936 break;
1937 case P2P_PROV_DISC_RESP:
1938 p2p_process_prov_disc_resp(p2p, sa, data + 1, len - 1);
1939 break;
1940 case P2P_DEV_DISC_REQ:
1941 p2p_process_dev_disc_req(p2p, sa, data + 1, len - 1, rx_freq);
1942 break;
1943 case P2P_DEV_DISC_RESP:
1944 p2p_process_dev_disc_resp(p2p, sa, data + 1, len - 1);
1945 break;
1946 default:
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001947 p2p_dbg(p2p, "Unsupported P2P Public Action frame type %d",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001948 data[0]);
1949 break;
1950 }
1951}
1952
1953
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001954static void p2p_rx_action_public(struct p2p_data *p2p, const u8 *da,
1955 const u8 *sa, const u8 *bssid, const u8 *data,
1956 size_t len, int freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001957{
1958 if (len < 1)
1959 return;
1960
1961 switch (data[0]) {
1962 case WLAN_PA_VENDOR_SPECIFIC:
1963 data++;
1964 len--;
Dmitry Shmidta38abf92014-03-06 13:38:44 -08001965 if (len < 4)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001966 return;
Dmitry Shmidta38abf92014-03-06 13:38:44 -08001967 if (WPA_GET_BE32(data) != P2P_IE_VENDOR_TYPE)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001968 return;
1969
Dmitry Shmidta38abf92014-03-06 13:38:44 -08001970 data += 4;
1971 len -= 4;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001972
Dmitry Shmidta38abf92014-03-06 13:38:44 -08001973 p2p_rx_p2p_action(p2p, sa, data, len, freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001974 break;
1975 case WLAN_PA_GAS_INITIAL_REQ:
1976 p2p_rx_gas_initial_req(p2p, sa, data + 1, len - 1, freq);
1977 break;
1978 case WLAN_PA_GAS_INITIAL_RESP:
1979 p2p_rx_gas_initial_resp(p2p, sa, data + 1, len - 1, freq);
1980 break;
1981 case WLAN_PA_GAS_COMEBACK_REQ:
1982 p2p_rx_gas_comeback_req(p2p, sa, data + 1, len - 1, freq);
1983 break;
1984 case WLAN_PA_GAS_COMEBACK_RESP:
1985 p2p_rx_gas_comeback_resp(p2p, sa, data + 1, len - 1, freq);
1986 break;
1987 }
1988}
1989
1990
1991void p2p_rx_action(struct p2p_data *p2p, const u8 *da, const u8 *sa,
1992 const u8 *bssid, u8 category,
1993 const u8 *data, size_t len, int freq)
1994{
1995 if (category == WLAN_ACTION_PUBLIC) {
1996 p2p_rx_action_public(p2p, da, sa, bssid, data, len, freq);
1997 return;
1998 }
1999
2000 if (category != WLAN_ACTION_VENDOR_SPECIFIC)
2001 return;
2002
2003 if (len < 4)
2004 return;
2005
Dmitry Shmidta38abf92014-03-06 13:38:44 -08002006 if (WPA_GET_BE32(data) != P2P_IE_VENDOR_TYPE)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002007 return;
Dmitry Shmidta38abf92014-03-06 13:38:44 -08002008 data += 4;
2009 len -= 4;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002010
2011 /* P2P action frame */
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002012 p2p_dbg(p2p, "RX P2P Action from " MACSTR, MAC2STR(sa));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002013 wpa_hexdump(MSG_MSGDUMP, "P2P: P2P Action contents", data, len);
2014
2015 if (len < 1)
2016 return;
2017 switch (data[0]) {
2018 case P2P_NOA:
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002019 p2p_dbg(p2p, "Received P2P Action - Notice of Absence");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002020 /* TODO */
2021 break;
2022 case P2P_PRESENCE_REQ:
2023 p2p_process_presence_req(p2p, da, sa, data + 1, len - 1, freq);
2024 break;
2025 case P2P_PRESENCE_RESP:
2026 p2p_process_presence_resp(p2p, da, sa, data + 1, len - 1);
2027 break;
2028 case P2P_GO_DISC_REQ:
2029 p2p_process_go_disc_req(p2p, da, sa, data + 1, len - 1, freq);
2030 break;
2031 default:
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002032 p2p_dbg(p2p, "Received P2P Action - unknown type %u", data[0]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002033 break;
2034 }
2035}
2036
2037
2038static void p2p_go_neg_start(void *eloop_ctx, void *timeout_ctx)
2039{
2040 struct p2p_data *p2p = eloop_ctx;
2041 if (p2p->go_neg_peer == NULL)
2042 return;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07002043 if (p2p->pending_listen_freq) {
2044 p2p_dbg(p2p, "Clear pending_listen_freq for p2p_go_neg_start");
2045 p2p->pending_listen_freq = 0;
2046 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002047 p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
Sunil Ravib0ac25f2024-07-12 01:42:03 +00002048 p2p->pending_listen_wait_drv = false;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002049 p2p->go_neg_peer->status = P2P_SC_SUCCESS;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07002050 /*
2051 * Set new timeout to make sure a previously set one does not expire
2052 * too quickly while waiting for the GO Negotiation to complete.
2053 */
2054 p2p_set_timeout(p2p, 0, 500000);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002055 p2p_connect_send(p2p, p2p->go_neg_peer);
2056}
2057
2058
2059static void p2p_invite_start(void *eloop_ctx, void *timeout_ctx)
2060{
2061 struct p2p_data *p2p = eloop_ctx;
2062 if (p2p->invite_peer == NULL)
2063 return;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07002064 if (p2p->pending_listen_freq) {
2065 p2p_dbg(p2p, "Clear pending_listen_freq for p2p_invite_start");
2066 p2p->pending_listen_freq = 0;
2067 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002068 p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
Sunil Ravib0ac25f2024-07-12 01:42:03 +00002069 p2p->pending_listen_wait_drv = false;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002070 p2p_invite_send(p2p, p2p->invite_peer, p2p->invite_go_dev_addr,
2071 p2p->invite_dev_pw_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002072}
2073
2074
2075static void p2p_add_dev_from_probe_req(struct p2p_data *p2p, const u8 *addr,
2076 const u8 *ie, size_t ie_len)
2077{
2078 struct p2p_message msg;
2079 struct p2p_device *dev;
2080
2081 os_memset(&msg, 0, sizeof(msg));
2082 if (p2p_parse_ies(ie, ie_len, &msg) < 0 || msg.p2p_attributes == NULL)
2083 {
2084 p2p_parse_free(&msg);
2085 return; /* not a P2P probe */
2086 }
2087
2088 if (msg.ssid == NULL || msg.ssid[1] != P2P_WILDCARD_SSID_LEN ||
2089 os_memcmp(msg.ssid + 2, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN)
2090 != 0) {
2091 /* The Probe Request is not part of P2P Device Discovery. It is
2092 * not known whether the source address of the frame is the P2P
2093 * Device Address or P2P Interface Address. Do not add a new
2094 * peer entry based on this frames.
2095 */
2096 p2p_parse_free(&msg);
2097 return;
2098 }
2099
2100 dev = p2p_get_device(p2p, addr);
2101 if (dev) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002102 if (msg.listen_channel) {
2103 int freq;
2104
2105 if (dev->country[0] == 0)
2106 os_memcpy(dev->country, msg.listen_channel, 3);
2107
2108 freq = p2p_channel_to_freq(msg.listen_channel[3],
2109 msg.listen_channel[4]);
2110
2111 if (freq > 0 && dev->listen_freq != freq) {
2112 p2p_dbg(p2p,
2113 "Updated peer " MACSTR " Listen channel (Probe Request): %d -> %d MHz",
2114 MAC2STR(addr), dev->listen_freq, freq);
2115 dev->listen_freq = freq;
2116 }
2117 }
2118
Sunil Ravi77d572f2023-01-17 23:58:31 +00002119 p2p_update_peer_6ghz_capab(dev, &msg);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002120 os_get_reltime(&dev->last_seen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002121 p2p_parse_free(&msg);
2122 return; /* already known */
2123 }
2124
2125 dev = p2p_create_device(p2p, addr);
2126 if (dev == NULL) {
2127 p2p_parse_free(&msg);
2128 return;
2129 }
2130
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002131 os_get_reltime(&dev->last_seen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002132 dev->flags |= P2P_DEV_PROBE_REQ_ONLY;
2133
2134 if (msg.listen_channel) {
2135 os_memcpy(dev->country, msg.listen_channel, 3);
Dmitry Shmidt4b060592013-04-29 16:42:49 -07002136 dev->listen_freq = p2p_channel_to_freq(msg.listen_channel[3],
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002137 msg.listen_channel[4]);
2138 }
2139
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002140 p2p_copy_wps_info(p2p, dev, 1, &msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002141
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002142 if (msg.wfd_subelems) {
2143 wpabuf_free(dev->info.wfd_subelems);
2144 dev->info.wfd_subelems = wpabuf_dup(msg.wfd_subelems);
2145 }
2146
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002147 p2p_parse_free(&msg);
2148
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002149 p2p_dbg(p2p, "Created device entry based on Probe Req: " MACSTR
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002150 " dev_capab=0x%x group_capab=0x%x name='%s' listen_freq=%d",
2151 MAC2STR(dev->info.p2p_device_addr), dev->info.dev_capab,
2152 dev->info.group_capab, dev->info.device_name,
2153 dev->listen_freq);
2154}
2155
2156
2157struct p2p_device * p2p_add_dev_from_go_neg_req(struct p2p_data *p2p,
2158 const u8 *addr,
2159 struct p2p_message *msg)
2160{
2161 struct p2p_device *dev;
2162
2163 dev = p2p_get_device(p2p, addr);
2164 if (dev) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002165 os_get_reltime(&dev->last_seen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002166 return dev; /* already known */
2167 }
2168
2169 dev = p2p_create_device(p2p, addr);
2170 if (dev == NULL)
2171 return NULL;
2172
2173 p2p_add_dev_info(p2p, addr, dev, msg);
2174
2175 return dev;
2176}
2177
2178
2179static int dev_type_match(const u8 *dev_type, const u8 *req_dev_type)
2180{
2181 if (os_memcmp(dev_type, req_dev_type, WPS_DEV_TYPE_LEN) == 0)
2182 return 1;
2183 if (os_memcmp(dev_type, req_dev_type, 2) == 0 &&
2184 WPA_GET_BE32(&req_dev_type[2]) == 0 &&
2185 WPA_GET_BE16(&req_dev_type[6]) == 0)
2186 return 1; /* Category match with wildcard OUI/sub-category */
2187 return 0;
2188}
2189
2190
2191int dev_type_list_match(const u8 *dev_type, const u8 *req_dev_type[],
2192 size_t num_req_dev_type)
2193{
2194 size_t i;
2195 for (i = 0; i < num_req_dev_type; i++) {
2196 if (dev_type_match(dev_type, req_dev_type[i]))
2197 return 1;
2198 }
2199 return 0;
2200}
2201
2202
2203/**
2204 * p2p_match_dev_type - Match local device type with requested type
2205 * @p2p: P2P module context from p2p_init()
2206 * @wps: WPS TLVs from Probe Request frame (concatenated WPS IEs)
2207 * Returns: 1 on match, 0 on mismatch
2208 *
2209 * This function can be used to match the Requested Device Type attribute in
2210 * WPS IE with the local device types for deciding whether to reply to a Probe
2211 * Request frame.
2212 */
2213int p2p_match_dev_type(struct p2p_data *p2p, struct wpabuf *wps)
2214{
2215 struct wps_parse_attr attr;
2216 size_t i;
2217
2218 if (wps_parse_msg(wps, &attr))
2219 return 1; /* assume no Requested Device Type attributes */
2220
2221 if (attr.num_req_dev_type == 0)
2222 return 1; /* no Requested Device Type attributes -> match */
2223
2224 if (dev_type_list_match(p2p->cfg->pri_dev_type, attr.req_dev_type,
2225 attr.num_req_dev_type))
2226 return 1; /* Own Primary Device Type matches */
2227
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002228 for (i = 0; i < p2p->cfg->num_sec_dev_types; i++) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002229 if (dev_type_list_match(p2p->cfg->sec_dev_type[i],
2230 attr.req_dev_type,
2231 attr.num_req_dev_type))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002232 return 1; /* Own Secondary Device Type matches */
2233 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002234
2235 /* No matching device type found */
2236 return 0;
2237}
2238
2239
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002240struct wpabuf * p2p_build_probe_resp_ies(struct p2p_data *p2p,
2241 const u8 *query_hash,
2242 u8 query_count)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002243{
2244 struct wpabuf *buf;
2245 u8 *len;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002246 int pw_id = -1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002247 size_t extra = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002248
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002249#ifdef CONFIG_WIFI_DISPLAY
2250 if (p2p->wfd_ie_probe_resp)
2251 extra = wpabuf_len(p2p->wfd_ie_probe_resp);
2252#endif /* CONFIG_WIFI_DISPLAY */
2253
Dmitry Shmidt2e67f062014-07-16 09:55:28 -07002254 if (p2p->vendor_elem && p2p->vendor_elem[VENDOR_ELEM_PROBE_RESP_P2P])
2255 extra += wpabuf_len(p2p->vendor_elem[VENDOR_ELEM_PROBE_RESP_P2P]);
2256
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002257 if (query_count)
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002258 extra += MAX_SVC_ADV_IE_LEN;
2259
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002260 buf = wpabuf_alloc(1000 + extra);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002261 if (buf == NULL)
2262 return NULL;
2263
Dmitry Shmidt04949592012-07-19 12:16:46 -07002264 if (p2p->go_neg_peer) {
2265 /* Advertise immediate availability of WPS credential */
2266 pw_id = p2p_wps_method_pw_id(p2p->go_neg_peer->wps_method);
2267 }
2268
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002269 if (p2p_build_wps_ie(p2p, buf, pw_id, 1) < 0) {
2270 p2p_dbg(p2p, "Failed to build WPS IE for Probe Response");
2271 wpabuf_free(buf);
2272 return NULL;
2273 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002274
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002275#ifdef CONFIG_WIFI_DISPLAY
2276 if (p2p->wfd_ie_probe_resp)
2277 wpabuf_put_buf(buf, p2p->wfd_ie_probe_resp);
2278#endif /* CONFIG_WIFI_DISPLAY */
2279
Dmitry Shmidt2e67f062014-07-16 09:55:28 -07002280 if (p2p->vendor_elem && p2p->vendor_elem[VENDOR_ELEM_PROBE_RESP_P2P])
2281 wpabuf_put_buf(buf,
2282 p2p->vendor_elem[VENDOR_ELEM_PROBE_RESP_P2P]);
2283
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002284 /* P2P IE */
2285 len = p2p_buf_add_ie_hdr(buf);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002286 p2p_buf_add_capability(buf, p2p->dev_capab &
2287 ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002288 if (p2p->ext_listen_interval)
2289 p2p_buf_add_ext_listen_timing(buf, p2p->ext_listen_period,
2290 p2p->ext_listen_interval);
2291 p2p_buf_add_device_info(buf, p2p, NULL);
2292 p2p_buf_update_ie_hdr(buf, len);
2293
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002294 if (query_count) {
2295 p2p_buf_add_service_instance(buf, p2p, query_count, query_hash,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002296 p2p->p2ps_adv_list);
2297 }
2298
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002299 return buf;
2300}
2301
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07002302static int p2p_build_probe_resp_buf(struct p2p_data *p2p, struct wpabuf *buf,
2303 struct wpabuf *ies,
2304 const u8 *addr, int rx_freq)
2305{
2306 struct ieee80211_mgmt *resp;
2307 u8 channel, op_class;
2308
2309 resp = wpabuf_put(buf, offsetof(struct ieee80211_mgmt,
2310 u.probe_resp.variable));
2311
2312 resp->frame_control = host_to_le16((WLAN_FC_TYPE_MGMT << 2) |
2313 (WLAN_FC_STYPE_PROBE_RESP << 4));
2314 os_memcpy(resp->da, addr, ETH_ALEN);
2315 os_memcpy(resp->sa, p2p->cfg->dev_addr, ETH_ALEN);
2316 os_memcpy(resp->bssid, p2p->cfg->dev_addr, ETH_ALEN);
2317 resp->u.probe_resp.beacon_int = host_to_le16(100);
2318 /* hardware or low-level driver will setup seq_ctrl and timestamp */
2319 resp->u.probe_resp.capab_info =
2320 host_to_le16(WLAN_CAPABILITY_SHORT_PREAMBLE |
2321 WLAN_CAPABILITY_PRIVACY |
2322 WLAN_CAPABILITY_SHORT_SLOT_TIME);
2323
2324 wpabuf_put_u8(buf, WLAN_EID_SSID);
2325 wpabuf_put_u8(buf, P2P_WILDCARD_SSID_LEN);
2326 wpabuf_put_data(buf, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN);
2327
2328 wpabuf_put_u8(buf, WLAN_EID_SUPP_RATES);
2329 wpabuf_put_u8(buf, 8);
2330 wpabuf_put_u8(buf, (60 / 5) | 0x80);
2331 wpabuf_put_u8(buf, 90 / 5);
2332 wpabuf_put_u8(buf, (120 / 5) | 0x80);
2333 wpabuf_put_u8(buf, 180 / 5);
2334 wpabuf_put_u8(buf, (240 / 5) | 0x80);
2335 wpabuf_put_u8(buf, 360 / 5);
2336 wpabuf_put_u8(buf, 480 / 5);
2337 wpabuf_put_u8(buf, 540 / 5);
2338
2339 if (!rx_freq) {
2340 channel = p2p->cfg->channel;
2341 } else if (p2p_freq_to_channel(rx_freq, &op_class, &channel)) {
2342 p2p_err(p2p, "Failed to convert freq to channel");
2343 return -1;
2344 }
2345
2346 wpabuf_put_u8(buf, WLAN_EID_DS_PARAMS);
2347 wpabuf_put_u8(buf, 1);
2348 wpabuf_put_u8(buf, channel);
2349
2350 wpabuf_put_buf(buf, ies);
2351
2352 return 0;
2353}
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002354
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002355static int p2p_service_find_asp(struct p2p_data *p2p, const u8 *hash)
2356{
2357 struct p2ps_advertisement *adv_data;
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002358 int any_wfa;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002359
2360 p2p_dbg(p2p, "ASP find - ASP list: %p", p2p->p2ps_adv_list);
2361
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002362 /* Wildcard org.wi-fi.wfds matches any WFA spec defined service */
2363 any_wfa = os_memcmp(hash, p2p->wild_card_hash, P2PS_HASH_LEN) == 0;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002364
2365 adv_data = p2p->p2ps_adv_list;
2366 while (adv_data) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002367 if (os_memcmp(hash, adv_data->hash, P2PS_HASH_LEN) == 0)
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002368 return 1; /* exact hash match */
2369 if (any_wfa &&
2370 os_strncmp(adv_data->svc_name, P2PS_WILD_HASH_STR,
2371 os_strlen(P2PS_WILD_HASH_STR)) == 0)
2372 return 1; /* WFA service match */
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002373 adv_data = adv_data->next;
2374 }
2375
2376 return 0;
2377}
2378
2379
Dmitry Shmidt04949592012-07-19 12:16:46 -07002380static enum p2p_probe_req_status
2381p2p_reply_probe(struct p2p_data *p2p, const u8 *addr, const u8 *dst,
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002382 const u8 *bssid, const u8 *ie, size_t ie_len,
2383 unsigned int rx_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002384{
2385 struct ieee802_11_elems elems;
2386 struct wpabuf *buf;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002387 struct p2p_message msg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002388 struct wpabuf *ies;
2389
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002390 if (ieee802_11_parse_elems((u8 *) ie, ie_len, &elems, 0) ==
2391 ParseFailed) {
2392 /* Ignore invalid Probe Request frames */
Dmitry Shmidtec58b162014-02-19 12:44:18 -08002393 p2p_dbg(p2p, "Could not parse Probe Request frame - ignore it");
Dmitry Shmidt04949592012-07-19 12:16:46 -07002394 return P2P_PREQ_MALFORMED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002395 }
2396
2397 if (elems.p2p == NULL) {
2398 /* not a P2P probe - ignore it */
Dmitry Shmidtec58b162014-02-19 12:44:18 -08002399 p2p_dbg(p2p, "Not a P2P probe - ignore it");
Dmitry Shmidt04949592012-07-19 12:16:46 -07002400 return P2P_PREQ_NOT_P2P;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002401 }
2402
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002403 if (dst && !is_broadcast_ether_addr(dst) &&
Sunil Ravib0ac25f2024-07-12 01:42:03 +00002404 !ether_addr_equal(dst, p2p->cfg->dev_addr)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002405 /* Not sent to the broadcast address or our P2P Device Address
2406 */
Dmitry Shmidtec58b162014-02-19 12:44:18 -08002407 p2p_dbg(p2p, "Probe Req DA " MACSTR " not ours - ignore it",
2408 MAC2STR(dst));
Dmitry Shmidt04949592012-07-19 12:16:46 -07002409 return P2P_PREQ_NOT_PROCESSED;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002410 }
2411
2412 if (bssid && !is_broadcast_ether_addr(bssid)) {
2413 /* Not sent to the Wildcard BSSID */
Dmitry Shmidtec58b162014-02-19 12:44:18 -08002414 p2p_dbg(p2p, "Probe Req BSSID " MACSTR " not wildcard - ignore it",
2415 MAC2STR(bssid));
Dmitry Shmidt04949592012-07-19 12:16:46 -07002416 return P2P_PREQ_NOT_PROCESSED;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002417 }
2418
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002419 if (elems.ssid == NULL || elems.ssid_len != P2P_WILDCARD_SSID_LEN ||
2420 os_memcmp(elems.ssid, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN) !=
2421 0) {
2422 /* not using P2P Wildcard SSID - ignore */
Dmitry Shmidtec58b162014-02-19 12:44:18 -08002423 p2p_dbg(p2p, "Probe Req not using P2P Wildcard SSID - ignore it");
Dmitry Shmidt04949592012-07-19 12:16:46 -07002424 return P2P_PREQ_NOT_PROCESSED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002425 }
2426
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002427 if (supp_rates_11b_only(&elems)) {
2428 /* Indicates support for 11b rates only */
Dmitry Shmidtec58b162014-02-19 12:44:18 -08002429 p2p_dbg(p2p, "Probe Req with 11b rates only supported - ignore it");
Dmitry Shmidt04949592012-07-19 12:16:46 -07002430 return P2P_PREQ_NOT_P2P;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002431 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002432
2433 os_memset(&msg, 0, sizeof(msg));
2434 if (p2p_parse_ies(ie, ie_len, &msg) < 0) {
2435 /* Could not parse P2P attributes */
Dmitry Shmidtec58b162014-02-19 12:44:18 -08002436 p2p_dbg(p2p, "Could not parse P2P attributes in Probe Req - ignore it");
Dmitry Shmidt04949592012-07-19 12:16:46 -07002437 return P2P_PREQ_NOT_P2P;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002438 }
2439
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002440 if (msg.service_hash && msg.service_hash_count) {
2441 const u8 *hash = msg.service_hash;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002442 u8 i;
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002443 int p2ps_svc_found = 0;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002444
Dmitry Shmidt41712582015-06-29 11:02:15 -07002445 p2p_dbg(p2p, "in_listen=%d drv_in_listen=%d when received P2PS Probe Request at %u MHz; own Listen channel %u, pending listen freq %u MHz",
2446 p2p->in_listen, p2p->drv_in_listen, rx_freq,
2447 p2p->cfg->channel, p2p->pending_listen_freq);
2448
2449 if (!p2p->in_listen && !p2p->drv_in_listen &&
2450 p2p->pending_listen_freq && rx_freq &&
2451 rx_freq != p2p->pending_listen_freq) {
2452 p2p_dbg(p2p, "Do not reply to Probe Request frame that was received on %u MHz while waiting to start Listen state on %u MHz",
2453 rx_freq, p2p->pending_listen_freq);
2454 p2p_parse_free(&msg);
2455 return P2P_PREQ_NOT_LISTEN;
2456 }
2457
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002458 for (i = 0; i < msg.service_hash_count; i++) {
2459 if (p2p_service_find_asp(p2p, hash)) {
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002460 p2p_dbg(p2p, "Service Hash match found: "
2461 MACSTR, MAC2STR(hash));
2462 p2ps_svc_found = 1;
2463 break;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002464 }
2465 hash += P2PS_HASH_LEN;
2466 }
2467
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002468 /* Probed hash unknown */
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002469 if (!p2ps_svc_found) {
2470 p2p_dbg(p2p, "No Service Hash match found");
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002471 p2p_parse_free(&msg);
2472 return P2P_PREQ_NOT_PROCESSED;
2473 }
2474 } else {
2475 /* This is not a P2PS Probe Request */
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002476 p2p_dbg(p2p, "No P2PS Hash in Probe Request");
2477
2478 if (!p2p->in_listen || !p2p->drv_in_listen) {
2479 /* not in Listen state - ignore Probe Request */
2480 p2p_dbg(p2p, "Not in Listen state (in_listen=%d drv_in_listen=%d) - ignore Probe Request",
2481 p2p->in_listen, p2p->drv_in_listen);
2482 p2p_parse_free(&msg);
2483 return P2P_PREQ_NOT_LISTEN;
2484 }
2485 }
2486
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002487 if (msg.device_id &&
Sunil Ravib0ac25f2024-07-12 01:42:03 +00002488 !ether_addr_equal(msg.device_id, p2p->cfg->dev_addr)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002489 /* Device ID did not match */
Dmitry Shmidtec58b162014-02-19 12:44:18 -08002490 p2p_dbg(p2p, "Probe Req requested Device ID " MACSTR " did not match - ignore it",
2491 MAC2STR(msg.device_id));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002492 p2p_parse_free(&msg);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002493 return P2P_PREQ_NOT_PROCESSED;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002494 }
2495
2496 /* Check Requested Device Type match */
2497 if (msg.wps_attributes &&
2498 !p2p_match_dev_type(p2p, msg.wps_attributes)) {
2499 /* No match with Requested Device Type */
Hai Shalom021b0b52019-04-10 11:17:58 -07002500 p2p_dbg(p2p, "Probe Req requested Device Type did not match - ignore it");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002501 p2p_parse_free(&msg);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002502 return P2P_PREQ_NOT_PROCESSED;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002503 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002504
Dmitry Shmidt04949592012-07-19 12:16:46 -07002505 if (!p2p->cfg->send_probe_resp) {
2506 /* Response generated elsewhere */
Dmitry Shmidtec58b162014-02-19 12:44:18 -08002507 p2p_dbg(p2p, "Probe Resp generated elsewhere - do not generate additional response");
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002508 p2p_parse_free(&msg);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002509 return P2P_PREQ_NOT_PROCESSED;
2510 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002511
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002512 p2p_dbg(p2p, "Reply to P2P Probe Request in Listen state");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002513
2514 /*
2515 * We do not really have a specific BSS that this frame is advertising,
2516 * so build a frame that has some information in valid format. This is
2517 * really only used for discovery purposes, not to learn exact BSS
2518 * parameters.
2519 */
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002520 ies = p2p_build_probe_resp_ies(p2p, msg.service_hash,
2521 msg.service_hash_count);
2522 p2p_parse_free(&msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002523 if (ies == NULL)
Dmitry Shmidt04949592012-07-19 12:16:46 -07002524 return P2P_PREQ_NOT_PROCESSED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002525
2526 buf = wpabuf_alloc(200 + wpabuf_len(ies));
2527 if (buf == NULL) {
2528 wpabuf_free(ies);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002529 return P2P_PREQ_NOT_PROCESSED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002530 }
2531
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07002532 if (p2p_build_probe_resp_buf(p2p, buf, ies, addr, rx_freq)) {
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002533 wpabuf_free(ies);
2534 wpabuf_free(buf);
2535 return P2P_PREQ_NOT_PROCESSED;
2536 }
2537
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002538 wpabuf_free(ies);
2539
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002540 p2p->cfg->send_probe_resp(p2p->cfg->cb_ctx, buf, rx_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002541
2542 wpabuf_free(buf);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002543
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002544 return P2P_PREQ_PROCESSED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002545}
2546
2547
Dmitry Shmidt04949592012-07-19 12:16:46 -07002548enum p2p_probe_req_status
2549p2p_probe_req_rx(struct p2p_data *p2p, const u8 *addr, const u8 *dst,
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002550 const u8 *bssid, const u8 *ie, size_t ie_len,
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07002551 unsigned int rx_freq, int p2p_lo_started)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002552{
Dmitry Shmidt04949592012-07-19 12:16:46 -07002553 enum p2p_probe_req_status res;
2554
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002555 p2p_add_dev_from_probe_req(p2p, addr, ie, ie_len);
2556
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07002557 if (p2p_lo_started) {
2558 p2p_dbg(p2p,
2559 "Probe Response is offloaded, do not reply Probe Request");
2560 return P2P_PREQ_PROCESSED;
2561 }
2562
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002563 res = p2p_reply_probe(p2p, addr, dst, bssid, ie, ie_len, rx_freq);
2564 if (res != P2P_PREQ_PROCESSED && res != P2P_PREQ_NOT_PROCESSED)
2565 return res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002566
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002567 /*
2568 * Activate a pending GO Negotiation/Invite flow if a received Probe
2569 * Request frame is from an expected peer. Some devices may share the
2570 * same address for P2P and non-P2P STA running simultaneously. The
2571 * P2P_PREQ_PROCESSED and P2P_PREQ_NOT_PROCESSED p2p_reply_probe()
2572 * return values verified above ensure we are handling a Probe Request
2573 * frame from a P2P peer.
2574 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002575 if ((p2p->state == P2P_CONNECT || p2p->state == P2P_CONNECT_LISTEN) &&
2576 p2p->go_neg_peer &&
Sunil Ravib0ac25f2024-07-12 01:42:03 +00002577 ether_addr_equal(addr, p2p->go_neg_peer->info.p2p_device_addr) &&
Dmitry Shmidt9cdf1b92013-02-27 12:58:50 -08002578 !(p2p->go_neg_peer->flags & P2P_DEV_WAIT_GO_NEG_CONFIRM)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002579 /* Received a Probe Request from GO Negotiation peer */
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002580 p2p_dbg(p2p, "Found GO Negotiation peer - try to start GO negotiation from timeout");
Dmitry Shmidt9cdf1b92013-02-27 12:58:50 -08002581 eloop_cancel_timeout(p2p_go_neg_start, p2p, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002582 eloop_register_timeout(0, 0, p2p_go_neg_start, p2p, NULL);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002583 return res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002584 }
2585
2586 if ((p2p->state == P2P_INVITE || p2p->state == P2P_INVITE_LISTEN) &&
2587 p2p->invite_peer &&
Dmitry Shmidt3c479372014-02-04 10:50:36 -08002588 (p2p->invite_peer->flags & P2P_DEV_WAIT_INV_REQ_ACK) &&
Sunil Ravib0ac25f2024-07-12 01:42:03 +00002589 ether_addr_equal(addr, p2p->invite_peer->info.p2p_device_addr)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002590 /* Received a Probe Request from Invite peer */
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002591 p2p_dbg(p2p, "Found Invite peer - try to start Invite from timeout");
Dmitry Shmidt7f93d6f2014-02-21 11:22:49 -08002592 eloop_cancel_timeout(p2p_invite_start, p2p, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002593 eloop_register_timeout(0, 0, p2p_invite_start, p2p, NULL);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002594 return res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002595 }
2596
Dmitry Shmidt04949592012-07-19 12:16:46 -07002597 return res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002598}
2599
2600
2601static int p2p_assoc_req_ie_wlan_ap(struct p2p_data *p2p, const u8 *bssid,
2602 u8 *buf, size_t len, struct wpabuf *p2p_ie)
2603{
2604 struct wpabuf *tmp;
2605 u8 *lpos;
2606 size_t tmplen;
2607 int res;
2608 u8 group_capab;
Dmitry Shmidt05df46a2015-05-19 11:02:01 -07002609 struct p2p_message msg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002610
2611 if (p2p_ie == NULL)
2612 return 0; /* WLAN AP is not a P2P manager */
2613
Dmitry Shmidt05df46a2015-05-19 11:02:01 -07002614 os_memset(&msg, 0, sizeof(msg));
2615 if (p2p_parse_p2p_ie(p2p_ie, &msg) < 0)
2616 return 0;
2617
2618 p2p_dbg(p2p, "BSS P2P manageability %s",
2619 msg.manageability ? "enabled" : "disabled");
2620
2621 if (!msg.manageability)
2622 return 0;
2623
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002624 /*
2625 * (Re)Association Request - P2P IE
2626 * P2P Capability attribute (shall be present)
2627 * P2P Interface attribute (present if concurrent device and
2628 * P2P Management is enabled)
2629 */
2630 tmp = wpabuf_alloc(200);
2631 if (tmp == NULL)
2632 return -1;
2633
2634 lpos = p2p_buf_add_ie_hdr(tmp);
2635 group_capab = 0;
2636 if (p2p->num_groups > 0) {
2637 group_capab |= P2P_GROUP_CAPAB_GROUP_OWNER;
2638 if ((p2p->dev_capab & P2P_DEV_CAPAB_CONCURRENT_OPER) &&
2639 (p2p->dev_capab & P2P_DEV_CAPAB_INFRA_MANAGED) &&
2640 p2p->cross_connect)
2641 group_capab |= P2P_GROUP_CAPAB_CROSS_CONN;
2642 }
2643 p2p_buf_add_capability(tmp, p2p->dev_capab, group_capab);
2644 if ((p2p->dev_capab & P2P_DEV_CAPAB_CONCURRENT_OPER) &&
2645 (p2p->dev_capab & P2P_DEV_CAPAB_INFRA_MANAGED))
2646 p2p_buf_add_p2p_interface(tmp, p2p);
2647 p2p_buf_update_ie_hdr(tmp, lpos);
2648
2649 tmplen = wpabuf_len(tmp);
2650 if (tmplen > len)
2651 res = -1;
2652 else {
2653 os_memcpy(buf, wpabuf_head(tmp), tmplen);
2654 res = tmplen;
2655 }
2656 wpabuf_free(tmp);
2657
2658 return res;
2659}
2660
2661
2662int p2p_assoc_req_ie(struct p2p_data *p2p, const u8 *bssid, u8 *buf,
2663 size_t len, int p2p_group, struct wpabuf *p2p_ie)
2664{
2665 struct wpabuf *tmp;
2666 u8 *lpos;
2667 struct p2p_device *peer;
2668 size_t tmplen;
2669 int res;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002670 size_t extra = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002671
2672 if (!p2p_group)
2673 return p2p_assoc_req_ie_wlan_ap(p2p, bssid, buf, len, p2p_ie);
2674
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002675#ifdef CONFIG_WIFI_DISPLAY
2676 if (p2p->wfd_ie_assoc_req)
2677 extra = wpabuf_len(p2p->wfd_ie_assoc_req);
2678#endif /* CONFIG_WIFI_DISPLAY */
2679
Dmitry Shmidt2e67f062014-07-16 09:55:28 -07002680 if (p2p->vendor_elem && p2p->vendor_elem[VENDOR_ELEM_P2P_ASSOC_REQ])
2681 extra += wpabuf_len(p2p->vendor_elem[VENDOR_ELEM_P2P_ASSOC_REQ]);
2682
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002683 /*
2684 * (Re)Association Request - P2P IE
2685 * P2P Capability attribute (shall be present)
2686 * Extended Listen Timing (may be present)
2687 * P2P Device Info attribute (shall be present)
2688 */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002689 tmp = wpabuf_alloc(200 + extra);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002690 if (tmp == NULL)
2691 return -1;
2692
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002693#ifdef CONFIG_WIFI_DISPLAY
2694 if (p2p->wfd_ie_assoc_req)
2695 wpabuf_put_buf(tmp, p2p->wfd_ie_assoc_req);
2696#endif /* CONFIG_WIFI_DISPLAY */
2697
Dmitry Shmidt2e67f062014-07-16 09:55:28 -07002698 if (p2p->vendor_elem && p2p->vendor_elem[VENDOR_ELEM_P2P_ASSOC_REQ])
2699 wpabuf_put_buf(tmp,
2700 p2p->vendor_elem[VENDOR_ELEM_P2P_ASSOC_REQ]);
2701
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002702 peer = bssid ? p2p_get_device(p2p, bssid) : NULL;
2703
2704 lpos = p2p_buf_add_ie_hdr(tmp);
2705 p2p_buf_add_capability(tmp, p2p->dev_capab, 0);
2706 if (p2p->ext_listen_interval)
2707 p2p_buf_add_ext_listen_timing(tmp, p2p->ext_listen_period,
2708 p2p->ext_listen_interval);
2709 p2p_buf_add_device_info(tmp, p2p, peer);
2710 p2p_buf_update_ie_hdr(tmp, lpos);
2711
2712 tmplen = wpabuf_len(tmp);
2713 if (tmplen > len)
2714 res = -1;
2715 else {
2716 os_memcpy(buf, wpabuf_head(tmp), tmplen);
2717 res = tmplen;
2718 }
2719 wpabuf_free(tmp);
2720
2721 return res;
2722}
2723
2724
2725int p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf, char *end)
2726{
2727 struct wpabuf *p2p_ie;
2728 int ret;
2729
2730 p2p_ie = ieee802_11_vendor_ie_concat(ies, ies_len, P2P_IE_VENDOR_TYPE);
2731 if (p2p_ie == NULL)
2732 return 0;
2733
2734 ret = p2p_attr_text(p2p_ie, buf, end);
2735 wpabuf_free(p2p_ie);
2736 return ret;
2737}
2738
2739
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002740struct p2ps_advertisement *
2741p2p_service_p2ps_id(struct p2p_data *p2p, u32 adv_id)
2742{
2743 struct p2ps_advertisement *adv_data;
2744
2745 if (!p2p)
2746 return NULL;
2747
2748 adv_data = p2p->p2ps_adv_list;
2749 while (adv_data) {
2750 if (adv_data->id == adv_id)
2751 return adv_data;
2752 adv_data = adv_data->next;
2753 }
2754
2755 return NULL;
2756}
2757
2758
2759int p2p_service_del_asp(struct p2p_data *p2p, u32 adv_id)
2760{
2761 struct p2ps_advertisement *adv_data;
2762 struct p2ps_advertisement **prior;
2763
2764 if (!p2p)
2765 return -1;
2766
2767 adv_data = p2p->p2ps_adv_list;
2768 prior = &p2p->p2ps_adv_list;
2769 while (adv_data) {
2770 if (adv_data->id == adv_id) {
2771 p2p_dbg(p2p, "Delete ASP adv_id=0x%x", adv_id);
2772 *prior = adv_data->next;
2773 os_free(adv_data);
2774 return 0;
2775 }
2776 prior = &adv_data->next;
2777 adv_data = adv_data->next;
2778 }
2779
2780 return -1;
2781}
2782
2783
2784int p2p_service_add_asp(struct p2p_data *p2p, int auto_accept, u32 adv_id,
2785 const char *adv_str, u8 svc_state, u16 config_methods,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002786 const char *svc_info, const u8 *cpt_priority)
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002787{
2788 struct p2ps_advertisement *adv_data, *tmp, **prev;
2789 u8 buf[P2PS_HASH_LEN];
2790 size_t adv_data_len, adv_len, info_len = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002791 int i;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002792
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002793 if (!p2p || !adv_str || !adv_str[0] || !cpt_priority)
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002794 return -1;
2795
2796 if (!(config_methods & p2p->cfg->config_methods)) {
2797 p2p_dbg(p2p, "Config methods not supported svc: 0x%x dev: 0x%x",
2798 config_methods, p2p->cfg->config_methods);
2799 return -1;
2800 }
2801
2802 if (!p2ps_gen_hash(p2p, adv_str, buf))
2803 return -1;
2804
2805 if (svc_info)
2806 info_len = os_strlen(svc_info);
2807 adv_len = os_strlen(adv_str);
2808 adv_data_len = sizeof(struct p2ps_advertisement) + adv_len + 1 +
2809 info_len + 1;
2810
2811 adv_data = os_zalloc(adv_data_len);
2812 if (!adv_data)
2813 return -1;
2814
2815 os_memcpy(adv_data->hash, buf, P2PS_HASH_LEN);
2816 adv_data->id = adv_id;
2817 adv_data->state = svc_state;
2818 adv_data->config_methods = config_methods & p2p->cfg->config_methods;
2819 adv_data->auto_accept = (u8) auto_accept;
2820 os_memcpy(adv_data->svc_name, adv_str, adv_len);
2821
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002822 for (i = 0; cpt_priority[i] && i < P2PS_FEATURE_CAPAB_CPT_MAX; i++) {
2823 adv_data->cpt_priority[i] = cpt_priority[i];
2824 adv_data->cpt_mask |= cpt_priority[i];
2825 }
2826
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002827 if (svc_info && info_len) {
2828 adv_data->svc_info = &adv_data->svc_name[adv_len + 1];
2829 os_memcpy(adv_data->svc_info, svc_info, info_len);
2830 }
2831
2832 /*
2833 * Group Advertisements by service string. They do not need to be
2834 * sorted, but groups allow easier Probe Response instance grouping
2835 */
2836 tmp = p2p->p2ps_adv_list;
2837 prev = &p2p->p2ps_adv_list;
2838 while (tmp) {
2839 if (tmp->id == adv_data->id) {
2840 if (os_strcmp(tmp->svc_name, adv_data->svc_name) != 0) {
2841 os_free(adv_data);
2842 return -1;
2843 }
2844 adv_data->next = tmp->next;
2845 *prev = adv_data;
2846 os_free(tmp);
2847 goto inserted;
2848 } else {
2849 if (os_strcmp(tmp->svc_name, adv_data->svc_name) == 0) {
2850 adv_data->next = tmp->next;
2851 tmp->next = adv_data;
2852 goto inserted;
2853 }
2854 }
2855 prev = &tmp->next;
2856 tmp = tmp->next;
2857 }
2858
2859 /* No svc_name match found */
2860 adv_data->next = p2p->p2ps_adv_list;
2861 p2p->p2ps_adv_list = adv_data;
2862
2863inserted:
2864 p2p_dbg(p2p,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002865 "Added ASP advertisement adv_id=0x%x config_methods=0x%x svc_state=0x%x adv_str='%s' cpt_mask=0x%x",
2866 adv_id, adv_data->config_methods, svc_state, adv_str,
2867 adv_data->cpt_mask);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002868
2869 return 0;
2870}
2871
2872
Dmitry Shmidtdda10c22015-03-24 16:05:01 -07002873void p2p_service_flush_asp(struct p2p_data *p2p)
2874{
2875 struct p2ps_advertisement *adv, *prev;
2876
2877 if (!p2p)
2878 return;
2879
2880 adv = p2p->p2ps_adv_list;
2881 while (adv) {
2882 prev = adv;
2883 adv = adv->next;
2884 os_free(prev);
2885 }
2886
2887 p2p->p2ps_adv_list = NULL;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002888 p2ps_prov_free(p2p);
Dmitry Shmidtdda10c22015-03-24 16:05:01 -07002889 p2p_dbg(p2p, "All ASP advertisements flushed");
2890}
2891
2892
Dmitry Shmidt04949592012-07-19 12:16:46 -07002893int p2p_parse_dev_addr_in_p2p_ie(struct wpabuf *p2p_ie, u8 *dev_addr)
2894{
2895 struct p2p_message msg;
2896
2897 os_memset(&msg, 0, sizeof(msg));
2898 if (p2p_parse_p2p_ie(p2p_ie, &msg))
2899 return -1;
2900
2901 if (msg.p2p_device_addr) {
2902 os_memcpy(dev_addr, msg.p2p_device_addr, ETH_ALEN);
2903 return 0;
2904 } else if (msg.device_id) {
2905 os_memcpy(dev_addr, msg.device_id, ETH_ALEN);
2906 return 0;
2907 }
2908 return -1;
2909}
2910
2911
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002912int p2p_parse_dev_addr(const u8 *ies, size_t ies_len, u8 *dev_addr)
2913{
2914 struct wpabuf *p2p_ie;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002915 int ret;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002916
2917 p2p_ie = ieee802_11_vendor_ie_concat(ies, ies_len,
2918 P2P_IE_VENDOR_TYPE);
2919 if (p2p_ie == NULL)
2920 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002921 ret = p2p_parse_dev_addr_in_p2p_ie(p2p_ie, dev_addr);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002922 wpabuf_free(p2p_ie);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002923 return ret;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002924}
2925
2926
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002927static void p2p_clear_go_neg(struct p2p_data *p2p)
2928{
2929 p2p->go_neg_peer = NULL;
2930 p2p_clear_timeout(p2p);
2931 p2p_set_state(p2p, P2P_IDLE);
2932}
2933
2934
2935void p2p_wps_success_cb(struct p2p_data *p2p, const u8 *mac_addr)
2936{
2937 if (p2p->go_neg_peer == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002938 p2p_dbg(p2p, "No pending Group Formation - ignore WPS registration success notification");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002939 return; /* No pending Group Formation */
2940 }
2941
Sunil Ravib0ac25f2024-07-12 01:42:03 +00002942 if (!ether_addr_equal(mac_addr, p2p->go_neg_peer->intended_addr)) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002943 p2p_dbg(p2p, "Ignore WPS registration success notification for "
2944 MACSTR " (GO Negotiation peer " MACSTR ")",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002945 MAC2STR(mac_addr),
2946 MAC2STR(p2p->go_neg_peer->intended_addr));
2947 return; /* Ignore unexpected peer address */
2948 }
2949
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002950 p2p_dbg(p2p, "Group Formation completed successfully with " MACSTR,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002951 MAC2STR(mac_addr));
2952
2953 p2p_clear_go_neg(p2p);
2954}
2955
2956
2957void p2p_group_formation_failed(struct p2p_data *p2p)
2958{
2959 if (p2p->go_neg_peer == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002960 p2p_dbg(p2p, "No pending Group Formation - ignore group formation failure notification");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002961 return; /* No pending Group Formation */
2962 }
2963
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002964 p2p_dbg(p2p, "Group Formation failed with " MACSTR,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002965 MAC2STR(p2p->go_neg_peer->intended_addr));
2966
2967 p2p_clear_go_neg(p2p);
2968}
2969
2970
Hai Shalom60840252021-02-19 19:02:11 -08002971bool is_p2p_6ghz_disabled(struct p2p_data *p2p)
2972{
2973 if (p2p)
2974 return p2p->cfg->p2p_6ghz_disable;
2975 return false;
2976}
2977
2978
Shuibing Daie2fad412023-05-05 14:08:11 -07002979bool is_p2p_dfs_chan_enabled(struct p2p_data *p2p)
2980{
2981 if (p2p)
2982 return p2p->cfg->p2p_dfs_chan_enable;
2983 return false;
2984}
2985
2986
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002987struct p2p_data * p2p_init(const struct p2p_config *cfg)
2988{
2989 struct p2p_data *p2p;
2990
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07002991 if (cfg->max_peers < 1 ||
2992 cfg->passphrase_len < 8 || cfg->passphrase_len > 63)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002993 return NULL;
2994
2995 p2p = os_zalloc(sizeof(*p2p) + sizeof(*cfg));
2996 if (p2p == NULL)
2997 return NULL;
2998 p2p->cfg = (struct p2p_config *) (p2p + 1);
2999 os_memcpy(p2p->cfg, cfg, sizeof(*cfg));
3000 if (cfg->dev_name)
3001 p2p->cfg->dev_name = os_strdup(cfg->dev_name);
3002 if (cfg->manufacturer)
3003 p2p->cfg->manufacturer = os_strdup(cfg->manufacturer);
3004 if (cfg->model_name)
3005 p2p->cfg->model_name = os_strdup(cfg->model_name);
3006 if (cfg->model_number)
3007 p2p->cfg->model_number = os_strdup(cfg->model_number);
3008 if (cfg->serial_number)
3009 p2p->cfg->serial_number = os_strdup(cfg->serial_number);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003010 if (cfg->pref_chan) {
3011 p2p->cfg->pref_chan = os_malloc(cfg->num_pref_chan *
3012 sizeof(struct p2p_channel));
3013 if (p2p->cfg->pref_chan) {
3014 os_memcpy(p2p->cfg->pref_chan, cfg->pref_chan,
3015 cfg->num_pref_chan *
3016 sizeof(struct p2p_channel));
3017 } else
3018 p2p->cfg->num_pref_chan = 0;
3019 }
3020
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003021 p2ps_gen_hash(p2p, P2PS_WILD_HASH_STR, p2p->wild_card_hash);
3022
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003023 p2p->min_disc_int = 1;
3024 p2p->max_disc_int = 3;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003025 p2p->max_disc_tu = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003026
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07003027 if (os_get_random(&p2p->next_tie_breaker, 1) < 0)
3028 p2p->next_tie_breaker = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003029 p2p->next_tie_breaker &= 0x01;
3030 if (cfg->sd_request)
3031 p2p->dev_capab |= P2P_DEV_CAPAB_SERVICE_DISCOVERY;
3032 p2p->dev_capab |= P2P_DEV_CAPAB_INVITATION_PROCEDURE;
3033 if (cfg->concurrent_operations)
3034 p2p->dev_capab |= P2P_DEV_CAPAB_CONCURRENT_OPER;
3035 p2p->dev_capab |= P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
3036
3037 dl_list_init(&p2p->devices);
3038
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003039 p2p->go_timeout = 100;
3040 p2p->client_timeout = 20;
Dmitry Shmidt13ca8d82014-02-20 10:18:40 -08003041 p2p->num_p2p_sd_queries = 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003042
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003043 p2p_dbg(p2p, "initialized");
3044 p2p_channels_dump(p2p, "channels", &p2p->cfg->channels);
3045 p2p_channels_dump(p2p, "cli_channels", &p2p->cfg->cli_channels);
3046
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003047 return p2p;
3048}
3049
3050
3051void p2p_deinit(struct p2p_data *p2p)
3052{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003053#ifdef CONFIG_WIFI_DISPLAY
3054 wpabuf_free(p2p->wfd_ie_beacon);
3055 wpabuf_free(p2p->wfd_ie_probe_req);
3056 wpabuf_free(p2p->wfd_ie_probe_resp);
3057 wpabuf_free(p2p->wfd_ie_assoc_req);
3058 wpabuf_free(p2p->wfd_ie_invitation);
3059 wpabuf_free(p2p->wfd_ie_prov_disc_req);
3060 wpabuf_free(p2p->wfd_ie_prov_disc_resp);
3061 wpabuf_free(p2p->wfd_ie_go_neg);
3062 wpabuf_free(p2p->wfd_dev_info);
3063 wpabuf_free(p2p->wfd_assoc_bssid);
3064 wpabuf_free(p2p->wfd_coupled_sink_info);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003065 wpabuf_free(p2p->wfd_r2_dev_info);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003066#endif /* CONFIG_WIFI_DISPLAY */
3067
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003068 eloop_cancel_timeout(p2p_scan_timeout, p2p, NULL);
Dmitry Shmidt9cdf1b92013-02-27 12:58:50 -08003069 eloop_cancel_timeout(p2p_go_neg_start, p2p, NULL);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003070 eloop_cancel_timeout(p2p_go_neg_wait_timeout, p2p, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003071 p2p_flush(p2p);
3072 p2p_free_req_dev_types(p2p);
3073 os_free(p2p->cfg->dev_name);
3074 os_free(p2p->cfg->manufacturer);
3075 os_free(p2p->cfg->model_name);
3076 os_free(p2p->cfg->model_number);
3077 os_free(p2p->cfg->serial_number);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003078 os_free(p2p->cfg->pref_chan);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003079 os_free(p2p->groups);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07003080 p2ps_prov_free(p2p);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003081 wpabuf_free(p2p->sd_resp);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003082 p2p_remove_wps_vendor_extensions(p2p);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003083 os_free(p2p->no_go_freq.range);
Dmitry Shmidtdda10c22015-03-24 16:05:01 -07003084 p2p_service_flush_asp(p2p);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003085
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003086 os_free(p2p);
3087}
3088
3089
3090void p2p_flush(struct p2p_data *p2p)
3091{
3092 struct p2p_device *dev, *prev;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003093
3094 p2p_ext_listen(p2p, 0, 0);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003095 p2p_stop_find(p2p);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003096 dl_list_for_each_safe(dev, prev, &p2p->devices, struct p2p_device,
3097 list) {
3098 dl_list_del(&dev->list);
3099 p2p_device_free(p2p, dev);
3100 }
3101 p2p_free_sd_queries(p2p);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003102 p2p->ssid_set = 0;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003103 p2ps_prov_free(p2p);
3104 p2p_reset_pending_pd(p2p);
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08003105 p2p->override_pref_op_class = 0;
3106 p2p->override_pref_channel = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003107}
3108
3109
3110int p2p_unauthorize(struct p2p_data *p2p, const u8 *addr)
3111{
3112 struct p2p_device *dev;
3113
3114 dev = p2p_get_device(p2p, addr);
3115 if (dev == NULL)
3116 return -1;
3117
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003118 p2p_dbg(p2p, "Unauthorizing " MACSTR, MAC2STR(addr));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003119
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003120 if (p2p->go_neg_peer == dev) {
3121 eloop_cancel_timeout(p2p_go_neg_wait_timeout, p2p, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003122 p2p->go_neg_peer = NULL;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003123 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003124
3125 dev->wps_method = WPS_NOT_READY;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003126 dev->oob_pw_id = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003127 dev->flags &= ~P2P_DEV_WAIT_GO_NEG_RESPONSE;
3128 dev->flags &= ~P2P_DEV_WAIT_GO_NEG_CONFIRM;
3129
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003130 return 0;
3131}
3132
3133
3134int p2p_set_dev_name(struct p2p_data *p2p, const char *dev_name)
3135{
3136 os_free(p2p->cfg->dev_name);
3137 if (dev_name) {
3138 p2p->cfg->dev_name = os_strdup(dev_name);
3139 if (p2p->cfg->dev_name == NULL)
3140 return -1;
3141 } else
3142 p2p->cfg->dev_name = NULL;
3143 return 0;
3144}
3145
3146
3147int p2p_set_manufacturer(struct p2p_data *p2p, const char *manufacturer)
3148{
3149 os_free(p2p->cfg->manufacturer);
3150 p2p->cfg->manufacturer = NULL;
3151 if (manufacturer) {
3152 p2p->cfg->manufacturer = os_strdup(manufacturer);
3153 if (p2p->cfg->manufacturer == NULL)
3154 return -1;
3155 }
3156
3157 return 0;
3158}
3159
3160
3161int p2p_set_model_name(struct p2p_data *p2p, const char *model_name)
3162{
3163 os_free(p2p->cfg->model_name);
3164 p2p->cfg->model_name = NULL;
3165 if (model_name) {
3166 p2p->cfg->model_name = os_strdup(model_name);
3167 if (p2p->cfg->model_name == NULL)
3168 return -1;
3169 }
3170
3171 return 0;
3172}
3173
3174
3175int p2p_set_model_number(struct p2p_data *p2p, const char *model_number)
3176{
3177 os_free(p2p->cfg->model_number);
3178 p2p->cfg->model_number = NULL;
3179 if (model_number) {
3180 p2p->cfg->model_number = os_strdup(model_number);
3181 if (p2p->cfg->model_number == NULL)
3182 return -1;
3183 }
3184
3185 return 0;
3186}
3187
3188
3189int p2p_set_serial_number(struct p2p_data *p2p, const char *serial_number)
3190{
3191 os_free(p2p->cfg->serial_number);
3192 p2p->cfg->serial_number = NULL;
3193 if (serial_number) {
3194 p2p->cfg->serial_number = os_strdup(serial_number);
3195 if (p2p->cfg->serial_number == NULL)
3196 return -1;
3197 }
3198
3199 return 0;
3200}
3201
3202
3203void p2p_set_config_methods(struct p2p_data *p2p, u16 config_methods)
3204{
3205 p2p->cfg->config_methods = config_methods;
3206}
3207
3208
3209void p2p_set_uuid(struct p2p_data *p2p, const u8 *uuid)
3210{
3211 os_memcpy(p2p->cfg->uuid, uuid, 16);
3212}
3213
3214
3215int p2p_set_pri_dev_type(struct p2p_data *p2p, const u8 *pri_dev_type)
3216{
3217 os_memcpy(p2p->cfg->pri_dev_type, pri_dev_type, 8);
3218 return 0;
3219}
3220
3221
3222int p2p_set_sec_dev_types(struct p2p_data *p2p, const u8 dev_types[][8],
3223 size_t num_dev_types)
3224{
3225 if (num_dev_types > P2P_SEC_DEVICE_TYPES)
3226 num_dev_types = P2P_SEC_DEVICE_TYPES;
3227 p2p->cfg->num_sec_dev_types = num_dev_types;
3228 os_memcpy(p2p->cfg->sec_dev_type, dev_types, num_dev_types * 8);
3229 return 0;
3230}
3231
3232
3233void p2p_remove_wps_vendor_extensions(struct p2p_data *p2p)
3234{
3235 int i;
3236
3237 for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) {
3238 wpabuf_free(p2p->wps_vendor_ext[i]);
3239 p2p->wps_vendor_ext[i] = NULL;
3240 }
3241}
3242
3243
3244int p2p_add_wps_vendor_extension(struct p2p_data *p2p,
3245 const struct wpabuf *vendor_ext)
3246{
3247 int i;
3248
3249 if (vendor_ext == NULL)
3250 return -1;
3251
3252 for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) {
3253 if (p2p->wps_vendor_ext[i] == NULL)
3254 break;
3255 }
3256 if (i >= P2P_MAX_WPS_VENDOR_EXT)
3257 return -1;
3258
3259 p2p->wps_vendor_ext[i] = wpabuf_dup(vendor_ext);
3260 if (p2p->wps_vendor_ext[i] == NULL)
3261 return -1;
3262
3263 return 0;
3264}
3265
3266
3267int p2p_set_country(struct p2p_data *p2p, const char *country)
3268{
3269 os_memcpy(p2p->cfg->country, country, 3);
3270 return 0;
3271}
3272
3273
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003274static int p2p_pre_find_operation(struct p2p_data *p2p, struct p2p_device *dev)
3275{
Dmitry Shmidtde47be72016-01-07 12:52:55 -08003276 int res;
3277
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003278 if (dev->sd_pending_bcast_queries == 0) {
3279 /* Initialize with total number of registered broadcast
3280 * SD queries. */
3281 dev->sd_pending_bcast_queries = p2p->num_p2p_sd_queries;
3282 }
3283
Dmitry Shmidtde47be72016-01-07 12:52:55 -08003284 res = p2p_start_sd(p2p, dev);
3285 if (res == -2)
3286 return -2;
3287 if (res == 0)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003288 return 1;
3289
3290 if (dev->req_config_methods &&
3291 !(dev->flags & P2P_DEV_PD_FOR_JOIN)) {
3292 p2p_dbg(p2p, "Send pending Provision Discovery Request to "
3293 MACSTR " (config methods 0x%x)",
3294 MAC2STR(dev->info.p2p_device_addr),
3295 dev->req_config_methods);
3296 if (p2p_send_prov_disc_req(p2p, dev, 0, 0) == 0)
3297 return 1;
3298 }
3299
3300 return 0;
3301}
3302
3303
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003304void p2p_continue_find(struct p2p_data *p2p)
3305{
3306 struct p2p_device *dev;
Dmitry Shmidtde47be72016-01-07 12:52:55 -08003307 int found, res;
Dmitry Shmidt13ca8d82014-02-20 10:18:40 -08003308
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003309 p2p_set_state(p2p, P2P_SEARCH);
3310
3311 /* Continue from the device following the last iteration */
3312 found = 0;
3313 dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
3314 if (dev == p2p->last_p2p_find_oper) {
3315 found = 1;
3316 continue;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003317 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003318 if (!found)
3319 continue;
Dmitry Shmidtde47be72016-01-07 12:52:55 -08003320 res = p2p_pre_find_operation(p2p, dev);
3321 if (res > 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003322 p2p->last_p2p_find_oper = dev;
3323 return;
3324 }
Dmitry Shmidtde47be72016-01-07 12:52:55 -08003325 if (res == -2)
3326 goto skip_sd;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003327 }
3328
3329 /*
3330 * Wrap around to the beginning of the list and continue until the last
3331 * iteration device.
3332 */
3333 dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
Dmitry Shmidtde47be72016-01-07 12:52:55 -08003334 res = p2p_pre_find_operation(p2p, dev);
3335 if (res > 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003336 p2p->last_p2p_find_oper = dev;
3337 return;
3338 }
Dmitry Shmidtde47be72016-01-07 12:52:55 -08003339 if (res == -2)
3340 goto skip_sd;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003341 if (dev == p2p->last_p2p_find_oper)
3342 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003343 }
3344
Dmitry Shmidtde47be72016-01-07 12:52:55 -08003345skip_sd:
3346 os_memset(p2p->sd_query_no_ack, 0, ETH_ALEN);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003347 p2p_listen_in_find(p2p, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003348}
3349
3350
3351static void p2p_sd_cb(struct p2p_data *p2p, int success)
3352{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003353 p2p_dbg(p2p, "Service Discovery Query TX callback: success=%d",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003354 success);
3355 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
3356
3357 if (!success) {
Dmitry Shmidtde47be72016-01-07 12:52:55 -08003358 if (p2p->sd_peer) {
3359 if (is_zero_ether_addr(p2p->sd_query_no_ack)) {
3360 os_memcpy(p2p->sd_query_no_ack,
3361 p2p->sd_peer->info.p2p_device_addr,
3362 ETH_ALEN);
3363 p2p_dbg(p2p,
3364 "First SD Query no-ACK in this search iteration: "
3365 MACSTR, MAC2STR(p2p->sd_query_no_ack));
3366 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003367 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
Dmitry Shmidtde47be72016-01-07 12:52:55 -08003368 }
Dmitry Shmidt13ca8d82014-02-20 10:18:40 -08003369 p2p->sd_peer = NULL;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003370 if (p2p->state != P2P_IDLE)
3371 p2p_continue_find(p2p);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003372 return;
3373 }
3374
3375 if (p2p->sd_peer == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003376 p2p_dbg(p2p, "No SD peer entry known");
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003377 if (p2p->state != P2P_IDLE)
3378 p2p_continue_find(p2p);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003379 return;
3380 }
3381
Dmitry Shmidt807291d2015-01-27 13:40:23 -08003382 if (p2p->sd_query && p2p->sd_query->for_all_peers) {
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07003383 /* Update the pending broadcast SD query count for this device
3384 */
3385 p2p->sd_peer->sd_pending_bcast_queries--;
3386
3387 /*
3388 * If there are no pending broadcast queries for this device,
3389 * mark it as done (-1).
3390 */
3391 if (p2p->sd_peer->sd_pending_bcast_queries == 0)
3392 p2p->sd_peer->sd_pending_bcast_queries = -1;
3393 }
3394
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003395 /* Wait for response from the peer */
3396 p2p_set_state(p2p, P2P_SD_DURING_FIND);
3397 p2p_set_timeout(p2p, 0, 200000);
3398}
3399
Jouni Malinen75ecf522011-06-27 15:19:46 -07003400
3401/**
3402 * p2p_retry_pd - Retry any pending provision disc requests in IDLE state
3403 * @p2p: P2P module context from p2p_init()
3404 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003405static void p2p_retry_pd(struct p2p_data *p2p)
Jouni Malinen75ecf522011-06-27 15:19:46 -07003406{
3407 struct p2p_device *dev;
3408
Jouni Malinen75ecf522011-06-27 15:19:46 -07003409 /*
3410 * Retry the prov disc req attempt only for the peer that the user had
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003411 * requested.
Jouni Malinen75ecf522011-06-27 15:19:46 -07003412 */
3413
3414 dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
Sunil Ravib0ac25f2024-07-12 01:42:03 +00003415 if (!ether_addr_equal(p2p->pending_pd_devaddr,
3416 dev->info.p2p_device_addr))
Jouni Malinen75ecf522011-06-27 15:19:46 -07003417 continue;
3418 if (!dev->req_config_methods)
3419 continue;
Jouni Malinen75ecf522011-06-27 15:19:46 -07003420
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003421 p2p_dbg(p2p, "Send pending Provision Discovery Request to "
Jouni Malinen75ecf522011-06-27 15:19:46 -07003422 MACSTR " (config methods 0x%x)",
3423 MAC2STR(dev->info.p2p_device_addr),
3424 dev->req_config_methods);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003425 p2p_send_prov_disc_req(p2p, dev,
Dmitry Shmidt051af732013-10-22 13:52:46 -07003426 dev->flags & P2P_DEV_PD_FOR_JOIN,
3427 p2p->pd_force_freq);
Jouni Malinen75ecf522011-06-27 15:19:46 -07003428 return;
3429 }
3430}
3431
3432
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003433static void p2p_prov_disc_cb(struct p2p_data *p2p, int success)
3434{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003435 p2p_dbg(p2p, "Provision Discovery Request TX callback: success=%d",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003436 success);
Jouni Malinen75ecf522011-06-27 15:19:46 -07003437
3438 /*
3439 * Postpone resetting the pending action state till after we actually
3440 * time out. This allows us to take some action like notifying any
3441 * interested parties about no response to the request.
3442 *
3443 * When the timer (below) goes off we check in IDLE, SEARCH, or
3444 * LISTEN_ONLY state, which are the only allowed states to issue a PD
3445 * requests in, if this was still pending and then raise notification.
3446 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003447
3448 if (!success) {
Jouni Malinen75ecf522011-06-27 15:19:46 -07003449 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
3450
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003451 if (p2p->user_initiated_pd &&
3452 (p2p->state == P2P_SEARCH || p2p->state == P2P_LISTEN_ONLY))
3453 {
3454 /* Retry request from timeout to avoid busy loops */
3455 p2p->pending_action_state = P2P_PENDING_PD;
3456 p2p_set_timeout(p2p, 0, 50000);
3457 } else if (p2p->state != P2P_IDLE)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003458 p2p_continue_find(p2p);
Jouni Malinen75ecf522011-06-27 15:19:46 -07003459 else if (p2p->user_initiated_pd) {
3460 p2p->pending_action_state = P2P_PENDING_PD;
3461 p2p_set_timeout(p2p, 0, 300000);
3462 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003463 return;
3464 }
3465
Jouni Malinen75ecf522011-06-27 15:19:46 -07003466 /*
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003467 * If after PD Request the peer doesn't expect to receive PD Response
3468 * the PD Request ACK indicates a completion of the current PD. This
3469 * happens only on the advertiser side sending the follow-on PD Request
3470 * with the status different than 12 (Success: accepted by user).
3471 */
3472 if (p2p->p2ps_prov && !p2p->p2ps_prov->pd_seeker &&
3473 p2p->p2ps_prov->status != P2P_SC_SUCCESS_DEFERRED) {
3474 p2p_dbg(p2p, "P2PS PD completion on Follow-on PD Request ACK");
3475
3476 if (p2p->send_action_in_progress) {
3477 p2p->send_action_in_progress = 0;
3478 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
3479 }
3480
3481 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
3482
3483 if (p2p->cfg->p2ps_prov_complete) {
3484 p2p->cfg->p2ps_prov_complete(
3485 p2p->cfg->cb_ctx,
3486 p2p->p2ps_prov->status,
3487 p2p->p2ps_prov->adv_mac,
3488 p2p->p2ps_prov->adv_mac,
3489 p2p->p2ps_prov->session_mac,
3490 NULL, p2p->p2ps_prov->adv_id,
3491 p2p->p2ps_prov->session_id,
3492 0, 0, NULL, 0, 0, 0,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08003493 NULL, NULL, 0, 0, NULL, 0);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003494 }
3495
3496 if (p2p->user_initiated_pd)
3497 p2p_reset_pending_pd(p2p);
3498
3499 p2ps_prov_free(p2p);
3500 return;
3501 }
3502
3503 /*
Jouni Malinen75ecf522011-06-27 15:19:46 -07003504 * This postponing, of resetting pending_action_state, needs to be
3505 * done only for user initiated PD requests and not internal ones.
3506 */
3507 if (p2p->user_initiated_pd)
3508 p2p->pending_action_state = P2P_PENDING_PD;
3509 else
3510 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
3511
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003512 /* Wait for response from the peer */
3513 if (p2p->state == P2P_SEARCH)
3514 p2p_set_state(p2p, P2P_PD_DURING_FIND);
3515 p2p_set_timeout(p2p, 0, 200000);
3516}
3517
3518
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003519static void p2p_prov_disc_resp_cb(struct p2p_data *p2p, int success)
3520{
3521 p2p_dbg(p2p, "Provision Discovery Response TX callback: success=%d",
3522 success);
3523
3524 if (p2p->send_action_in_progress) {
3525 p2p->send_action_in_progress = 0;
3526 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
3527 }
3528
3529 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
3530
Jimmy Chen5ef7aad2019-10-15 15:45:26 +08003531 if (!success) {
3532 if (p2p->state == P2P_SEARCH)
3533 p2p_continue_find(p2p);
Hai Shalom81f62d82019-07-22 12:10:00 -07003534 return;
Jimmy Chen5ef7aad2019-10-15 15:45:26 +08003535 }
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003536
3537 if (!p2p->cfg->prov_disc_resp_cb ||
Jimmy Chen5ef7aad2019-10-15 15:45:26 +08003538 p2p->cfg->prov_disc_resp_cb(p2p->cfg->cb_ctx) < 1) {
3539 if (p2p->state == P2P_SEARCH)
3540 p2p_continue_find(p2p);
Hai Shalom81f62d82019-07-22 12:10:00 -07003541 return;
Jimmy Chen5ef7aad2019-10-15 15:45:26 +08003542 }
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003543
3544 p2p_dbg(p2p,
3545 "Post-Provision Discovery operations started - do not try to continue other P2P operations");
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003546}
3547
3548
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003549int p2p_scan_res_handler(struct p2p_data *p2p, const u8 *bssid, int freq,
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003550 struct os_reltime *rx_time, int level, const u8 *ies,
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003551 size_t ies_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003552{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003553 if (os_reltime_before(rx_time, &p2p->find_start)) {
Dmitry Shmidtf8623282013-02-20 14:34:59 -08003554 /*
3555 * The driver may have cached (e.g., in cfg80211 BSS table) the
3556 * scan results for relatively long time. To avoid reporting
3557 * stale information, update P2P peers only based on results
3558 * that have based on frames received after the last p2p_find
3559 * operation was started.
3560 */
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003561 p2p_dbg(p2p, "Ignore old scan result for " MACSTR
Dmitry Shmidtde47be72016-01-07 12:52:55 -08003562 " (rx_time=%u.%06u find_start=%u.%06u)",
Dmitry Shmidtf8623282013-02-20 14:34:59 -08003563 MAC2STR(bssid), (unsigned int) rx_time->sec,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08003564 (unsigned int) rx_time->usec,
3565 (unsigned int) p2p->find_start.sec,
3566 (unsigned int) p2p->find_start.usec);
Dmitry Shmidtf8623282013-02-20 14:34:59 -08003567 return 0;
3568 }
3569
3570 p2p_add_device(p2p, bssid, freq, rx_time, level, ies, ies_len, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003571
3572 return 0;
3573}
3574
3575
Hai Shalom60840252021-02-19 19:02:11 -08003576void p2p_scan_res_handled(struct p2p_data *p2p, unsigned int delay)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003577{
3578 if (!p2p->p2p_scan_running) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003579 p2p_dbg(p2p, "p2p_scan was not running, but scan results received");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003580 }
3581 p2p->p2p_scan_running = 0;
Hai Shalom60840252021-02-19 19:02:11 -08003582
3583 /* Use this delay only when p2p_find doesn't set it */
3584 if (!p2p->search_delay)
3585 p2p->search_delay = delay;
3586
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003587 eloop_cancel_timeout(p2p_scan_timeout, p2p, NULL);
3588
3589 if (p2p_run_after_scan(p2p))
3590 return;
3591 if (p2p->state == P2P_SEARCH)
3592 p2p_continue_find(p2p);
3593}
3594
3595
Dmitry Shmidt9c175262016-03-03 10:20:07 -08003596void p2p_scan_ie(struct p2p_data *p2p, struct wpabuf *ies, const u8 *dev_id,
3597 unsigned int bands)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003598{
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003599 u8 dev_capab;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003600 u8 *len;
3601
3602#ifdef CONFIG_WIFI_DISPLAY
3603 if (p2p->wfd_ie_probe_req)
3604 wpabuf_put_buf(ies, p2p->wfd_ie_probe_req);
3605#endif /* CONFIG_WIFI_DISPLAY */
3606
Dmitry Shmidt2e67f062014-07-16 09:55:28 -07003607 if (p2p->vendor_elem && p2p->vendor_elem[VENDOR_ELEM_PROBE_REQ_P2P])
3608 wpabuf_put_buf(ies,
3609 p2p->vendor_elem[VENDOR_ELEM_PROBE_REQ_P2P]);
3610
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003611 len = p2p_buf_add_ie_hdr(ies);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003612
3613 dev_capab = p2p->dev_capab & ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
3614
3615 /* P2PS requires Probe Request frames to include SD bit */
3616 if (p2p->p2ps_seek && p2p->p2ps_seek_count)
3617 dev_capab |= P2P_DEV_CAPAB_SERVICE_DISCOVERY;
3618
3619 p2p_buf_add_capability(ies, dev_capab, 0);
3620
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003621 if (dev_id)
3622 p2p_buf_add_device_id(ies, dev_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003623 if (p2p->cfg->reg_class && p2p->cfg->channel)
3624 p2p_buf_add_listen_channel(ies, p2p->cfg->country,
3625 p2p->cfg->reg_class,
3626 p2p->cfg->channel);
3627 if (p2p->ext_listen_interval)
3628 p2p_buf_add_ext_listen_timing(ies, p2p->ext_listen_period,
3629 p2p->ext_listen_interval);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003630
Dmitry Shmidt9c175262016-03-03 10:20:07 -08003631 if (bands & BAND_60_GHZ)
3632 p2p_buf_add_device_info(ies, p2p, NULL);
3633
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003634 if (p2p->p2ps_seek && p2p->p2ps_seek_count)
3635 p2p_buf_add_service_hash(ies, p2p);
3636
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003637 /* TODO: p2p_buf_add_operating_channel() if GO */
3638 p2p_buf_update_ie_hdr(ies, len);
3639}
3640
3641
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003642size_t p2p_scan_ie_buf_len(struct p2p_data *p2p)
3643{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003644 size_t len = 100;
3645
3646#ifdef CONFIG_WIFI_DISPLAY
3647 if (p2p && p2p->wfd_ie_probe_req)
3648 len += wpabuf_len(p2p->wfd_ie_probe_req);
3649#endif /* CONFIG_WIFI_DISPLAY */
3650
Dmitry Shmidt2e67f062014-07-16 09:55:28 -07003651 if (p2p && p2p->vendor_elem &&
3652 p2p->vendor_elem[VENDOR_ELEM_PROBE_REQ_P2P])
3653 len += wpabuf_len(p2p->vendor_elem[VENDOR_ELEM_PROBE_REQ_P2P]);
3654
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003655 return len;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003656}
3657
3658
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003659int p2p_ie_text(struct wpabuf *p2p_ie, char *buf, char *end)
3660{
3661 return p2p_attr_text(p2p_ie, buf, end);
3662}
3663
3664
3665static void p2p_go_neg_req_cb(struct p2p_data *p2p, int success)
3666{
3667 struct p2p_device *dev = p2p->go_neg_peer;
Dmitry Shmidt8c652892013-03-01 10:14:01 -08003668 int timeout;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003669
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003670 p2p_dbg(p2p, "GO Negotiation Request TX callback: success=%d", success);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003671
3672 if (dev == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003673 p2p_dbg(p2p, "No pending GO Negotiation");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003674 return;
3675 }
3676
3677 if (success) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003678 if (dev->flags & P2P_DEV_USER_REJECTED) {
3679 p2p_set_state(p2p, P2P_IDLE);
3680 return;
3681 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07003682 } else if (dev->go_neg_req_sent) {
3683 /* Cancel the increment from p2p_connect_send() on failure */
Dmitry Shmidt98f9e762012-05-30 11:18:46 -07003684 dev->go_neg_req_sent--;
3685 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003686
3687 if (!success &&
3688 (dev->info.dev_capab & P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY) &&
3689 !is_zero_ether_addr(dev->member_in_go_dev)) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003690 p2p_dbg(p2p, "Peer " MACSTR " did not acknowledge request - try to use device discoverability through its GO",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003691 MAC2STR(dev->info.p2p_device_addr));
3692 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
3693 p2p_send_dev_disc_req(p2p, dev);
3694 return;
3695 }
3696
3697 /*
3698 * Use P2P find, if needed, to find the other device from its listen
3699 * channel.
3700 */
3701 p2p_set_state(p2p, P2P_CONNECT);
Dmitry Shmidt8c652892013-03-01 10:14:01 -08003702 timeout = success ? 500000 : 100000;
3703 if (!success && p2p->go_neg_peer &&
3704 (p2p->go_neg_peer->flags & P2P_DEV_PEER_WAITING_RESPONSE)) {
3705 unsigned int r;
3706 /*
3707 * Peer is expected to wait our response and we will skip the
3708 * listen phase. Add some randomness to the wait time here to
3709 * make it less likely to hit cases where we could end up in
3710 * sync with peer not listening.
3711 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07003712 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
3713 r = 0;
Dmitry Shmidt8c652892013-03-01 10:14:01 -08003714 timeout += r % 100000;
3715 }
3716 p2p_set_timeout(p2p, 0, timeout);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003717}
3718
3719
3720static void p2p_go_neg_resp_cb(struct p2p_data *p2p, int success)
3721{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003722 p2p_dbg(p2p, "GO Negotiation Response TX callback: success=%d",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003723 success);
3724 if (!p2p->go_neg_peer && p2p->state == P2P_PROVISIONING) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003725 p2p_dbg(p2p, "Ignore TX callback event - GO Negotiation is not running anymore");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003726 return;
3727 }
3728 p2p_set_state(p2p, P2P_CONNECT);
Dmitry Shmidt8c652892013-03-01 10:14:01 -08003729 p2p_set_timeout(p2p, 0, 500000);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003730}
3731
3732
Dmitry Shmidt4b060592013-04-29 16:42:49 -07003733static void p2p_go_neg_resp_failure_cb(struct p2p_data *p2p, int success,
3734 const u8 *addr)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003735{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003736 p2p_dbg(p2p, "GO Negotiation Response (failure) TX callback: success=%d", success);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003737 if (p2p->go_neg_peer && p2p->go_neg_peer->status != P2P_SC_SUCCESS) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003738 p2p_go_neg_failed(p2p, p2p->go_neg_peer->status);
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08003739 return;
3740 }
3741
3742 if (success) {
Dmitry Shmidt4b060592013-04-29 16:42:49 -07003743 struct p2p_device *dev;
3744 dev = p2p_get_device(p2p, addr);
3745 if (dev &&
Dmitry Shmidt34c12022015-03-05 14:11:20 -08003746 dev->status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE)
Dmitry Shmidt4b060592013-04-29 16:42:49 -07003747 dev->flags |= P2P_DEV_PEER_WAITING_RESPONSE;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003748 }
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08003749
3750 if (p2p->state == P2P_SEARCH || p2p->state == P2P_SD_DURING_FIND)
3751 p2p_continue_find(p2p);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003752}
3753
3754
3755static void p2p_go_neg_conf_cb(struct p2p_data *p2p,
3756 enum p2p_send_action_result result)
3757{
3758 struct p2p_device *dev;
3759
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003760 p2p_dbg(p2p, "GO Negotiation Confirm TX callback: result=%d", result);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003761 if (result == P2P_SEND_ACTION_FAILED) {
Dmitry Shmidt413dde72014-04-11 10:23:22 -07003762 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003763 p2p_go_neg_failed(p2p, -1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003764 return;
3765 }
Dmitry Shmidt413dde72014-04-11 10:23:22 -07003766
3767 dev = p2p->go_neg_peer;
3768
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003769 if (result == P2P_SEND_ACTION_NO_ACK) {
3770 /*
Dmitry Shmidt413dde72014-04-11 10:23:22 -07003771 * Retry GO Negotiation Confirmation
3772 * P2P_GO_NEG_CNF_MAX_RETRY_COUNT times if we did not receive
3773 * ACK for confirmation.
3774 */
3775 if (dev && dev->go_neg_conf &&
3776 dev->go_neg_conf_sent <= P2P_GO_NEG_CNF_MAX_RETRY_COUNT) {
3777 p2p_dbg(p2p, "GO Negotiation Confirm retry %d",
3778 dev->go_neg_conf_sent);
3779 p2p->pending_action_state = P2P_PENDING_GO_NEG_CONFIRM;
3780 if (p2p_send_action(p2p, dev->go_neg_conf_freq,
3781 dev->info.p2p_device_addr,
3782 p2p->cfg->dev_addr,
3783 dev->info.p2p_device_addr,
3784 wpabuf_head(dev->go_neg_conf),
3785 wpabuf_len(dev->go_neg_conf), 0) >=
3786 0) {
3787 dev->go_neg_conf_sent++;
3788 return;
3789 }
3790 p2p_dbg(p2p, "Failed to re-send Action frame");
3791
3792 /*
3793 * Continue with the assumption that the first attempt
3794 * went through and just the ACK frame was lost.
3795 */
3796 }
3797
3798 /*
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003799 * It looks like the TX status for GO Negotiation Confirm is
3800 * often showing failure even when the peer has actually
3801 * received the frame. Since the peer may change channels
3802 * immediately after having received the frame, we may not see
3803 * an Ack for retries, so just dropping a single frame may
3804 * trigger this. To allow the group formation to succeed if the
3805 * peer did indeed receive the frame, continue regardless of
3806 * the TX status.
3807 */
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003808 p2p_dbg(p2p, "Assume GO Negotiation Confirm TX was actually received by the peer even though Ack was not reported");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003809 }
3810
Dmitry Shmidt413dde72014-04-11 10:23:22 -07003811 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
3812
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003813 if (dev == NULL)
3814 return;
3815
3816 p2p_go_complete(p2p, dev);
3817}
3818
3819
3820void p2p_send_action_cb(struct p2p_data *p2p, unsigned int freq, const u8 *dst,
3821 const u8 *src, const u8 *bssid,
3822 enum p2p_send_action_result result)
3823{
3824 enum p2p_pending_action_state state;
3825 int success;
3826
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003827 p2p_dbg(p2p, "Action frame TX callback (state=%d freq=%u dst=" MACSTR
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003828 " src=" MACSTR " bssid=" MACSTR " result=%d p2p_state=%s)",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003829 p2p->pending_action_state, freq, MAC2STR(dst), MAC2STR(src),
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003830 MAC2STR(bssid), result, p2p_state_txt(p2p->state));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003831 success = result == P2P_SEND_ACTION_SUCCESS;
3832 state = p2p->pending_action_state;
3833 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
3834 switch (state) {
3835 case P2P_NO_PENDING_ACTION:
Dmitry Shmidt4582d2a2014-02-28 11:14:23 -08003836 if (p2p->send_action_in_progress) {
3837 p2p->send_action_in_progress = 0;
3838 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
3839 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003840 break;
3841 case P2P_PENDING_GO_NEG_REQUEST:
3842 p2p_go_neg_req_cb(p2p, success);
3843 break;
3844 case P2P_PENDING_GO_NEG_RESPONSE:
3845 p2p_go_neg_resp_cb(p2p, success);
3846 break;
3847 case P2P_PENDING_GO_NEG_RESPONSE_FAILURE:
Dmitry Shmidt4b060592013-04-29 16:42:49 -07003848 p2p_go_neg_resp_failure_cb(p2p, success, dst);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003849 break;
3850 case P2P_PENDING_GO_NEG_CONFIRM:
3851 p2p_go_neg_conf_cb(p2p, result);
3852 break;
3853 case P2P_PENDING_SD:
3854 p2p_sd_cb(p2p, success);
3855 break;
3856 case P2P_PENDING_PD:
3857 p2p_prov_disc_cb(p2p, success);
3858 break;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003859 case P2P_PENDING_PD_RESPONSE:
3860 p2p_prov_disc_resp_cb(p2p, success);
3861 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003862 case P2P_PENDING_INVITATION_REQUEST:
3863 p2p_invitation_req_cb(p2p, success);
3864 break;
3865 case P2P_PENDING_INVITATION_RESPONSE:
3866 p2p_invitation_resp_cb(p2p, success);
3867 break;
3868 case P2P_PENDING_DEV_DISC_REQUEST:
3869 p2p_dev_disc_req_cb(p2p, success);
3870 break;
3871 case P2P_PENDING_DEV_DISC_RESPONSE:
3872 p2p_dev_disc_resp_cb(p2p, success);
3873 break;
3874 case P2P_PENDING_GO_DISC_REQ:
3875 p2p_go_disc_req_cb(p2p, success);
3876 break;
3877 }
3878}
3879
3880
3881void p2p_listen_cb(struct p2p_data *p2p, unsigned int freq,
3882 unsigned int duration)
3883{
3884 if (freq == p2p->pending_client_disc_freq) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003885 p2p_dbg(p2p, "Client discoverability remain-awake completed");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003886 p2p->pending_client_disc_freq = 0;
3887 return;
3888 }
3889
3890 if (freq != p2p->pending_listen_freq) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003891 p2p_dbg(p2p, "Unexpected listen callback for freq=%u duration=%u (pending_listen_freq=%u)",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003892 freq, duration, p2p->pending_listen_freq);
3893 return;
3894 }
3895
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003896 p2p_dbg(p2p, "Starting Listen timeout(%u,%u) on freq=%u based on callback",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003897 p2p->pending_listen_sec, p2p->pending_listen_usec,
3898 p2p->pending_listen_freq);
Sunil Ravib0ac25f2024-07-12 01:42:03 +00003899 p2p->pending_listen_wait_drv = false;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003900 p2p->in_listen = 1;
3901 p2p->drv_in_listen = freq;
3902 if (p2p->pending_listen_sec || p2p->pending_listen_usec) {
3903 /*
3904 * Add 20 msec extra wait to avoid race condition with driver
3905 * remain-on-channel end event, i.e., give driver more time to
3906 * complete the operation before our timeout expires.
3907 */
3908 p2p_set_timeout(p2p, p2p->pending_listen_sec,
3909 p2p->pending_listen_usec + 20000);
3910 }
3911
3912 p2p->pending_listen_freq = 0;
3913}
3914
3915
3916int p2p_listen_end(struct p2p_data *p2p, unsigned int freq)
3917{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003918 p2p_dbg(p2p, "Driver ended Listen state (freq=%u)", freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003919 p2p->drv_in_listen = 0;
3920 if (p2p->in_listen)
3921 return 0; /* Internal timeout will trigger the next step */
3922
Roshan Pius3a1667e2018-07-03 15:17:14 -07003923 if (p2p->state == P2P_WAIT_PEER_CONNECT && p2p->go_neg_peer &&
3924 p2p->pending_listen_freq) {
3925 /*
3926 * Better wait a bit if the driver is unable to start
3927 * offchannel operation for some reason to continue with
3928 * P2P_WAIT_PEER_(IDLE/CONNECT) state transitions.
3929 */
3930 p2p_dbg(p2p,
3931 "Listen operation did not seem to start - delay idle phase to avoid busy loop");
3932 p2p_set_timeout(p2p, 0, 100000);
3933 return 1;
3934 }
3935
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003936 if (p2p->state == P2P_CONNECT_LISTEN && p2p->go_neg_peer) {
3937 if (p2p->go_neg_peer->connect_reqs >= 120) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003938 p2p_dbg(p2p, "Timeout on sending GO Negotiation Request without getting response");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003939 p2p_go_neg_failed(p2p, -1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003940 return 0;
3941 }
3942
3943 p2p_set_state(p2p, P2P_CONNECT);
3944 p2p_connect_send(p2p, p2p->go_neg_peer);
3945 return 1;
3946 } else if (p2p->state == P2P_SEARCH) {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003947 if (p2p->p2p_scan_running) {
3948 /*
3949 * Search is already in progress. This can happen if
3950 * an Action frame RX is reported immediately after
3951 * the end of a remain-on-channel operation and the
3952 * response frame to that is sent using an offchannel
3953 * operation while in p2p_find. Avoid an attempt to
3954 * restart a scan here.
3955 */
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003956 p2p_dbg(p2p, "p2p_scan already in progress - do not try to start a new one");
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003957 return 1;
3958 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07003959 if (p2p->pending_listen_freq) {
3960 /*
3961 * Better wait a bit if the driver is unable to start
3962 * offchannel operation for some reason. p2p_search()
3963 * will be started from internal timeout.
3964 */
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003965 p2p_dbg(p2p, "Listen operation did not seem to start - delay search phase to avoid busy loop");
Dmitry Shmidt04949592012-07-19 12:16:46 -07003966 p2p_set_timeout(p2p, 0, 100000);
3967 return 1;
3968 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003969 if (p2p->search_delay) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003970 p2p_dbg(p2p, "Delay search operation by %u ms",
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003971 p2p->search_delay);
3972 p2p_set_timeout(p2p, p2p->search_delay / 1000,
3973 (p2p->search_delay % 1000) * 1000);
3974 return 1;
3975 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003976 p2p_search(p2p);
3977 return 1;
3978 }
3979
3980 return 0;
3981}
3982
3983
3984static void p2p_timeout_connect(struct p2p_data *p2p)
3985{
3986 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003987 if (p2p->go_neg_peer &&
3988 (p2p->go_neg_peer->flags & P2P_DEV_WAIT_GO_NEG_CONFIRM)) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003989 p2p_dbg(p2p, "Wait for GO Negotiation Confirm timed out - assume GO Negotiation failed");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003990 p2p_go_neg_failed(p2p, -1);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003991 return;
3992 }
Dmitry Shmidt8c652892013-03-01 10:14:01 -08003993 if (p2p->go_neg_peer &&
3994 (p2p->go_neg_peer->flags & P2P_DEV_PEER_WAITING_RESPONSE) &&
3995 p2p->go_neg_peer->connect_reqs < 120) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003996 p2p_dbg(p2p, "Peer expected to wait our response - skip listen");
Dmitry Shmidt8c652892013-03-01 10:14:01 -08003997 p2p_connect_send(p2p, p2p->go_neg_peer);
3998 return;
3999 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004000 if (p2p->go_neg_peer && p2p->go_neg_peer->oob_go_neg_freq > 0) {
4001 p2p_dbg(p2p, "Skip connect-listen since GO Neg channel known (OOB)");
4002 p2p_set_state(p2p, P2P_CONNECT_LISTEN);
4003 p2p_set_timeout(p2p, 0, 30000);
4004 return;
4005 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004006 p2p_set_state(p2p, P2P_CONNECT_LISTEN);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004007 p2p_listen_in_find(p2p, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004008}
4009
4010
4011static void p2p_timeout_connect_listen(struct p2p_data *p2p)
4012{
4013 if (p2p->go_neg_peer) {
4014 if (p2p->drv_in_listen) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004015 p2p_dbg(p2p, "Driver is still in Listen state; wait for it to complete");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004016 return;
4017 }
4018
4019 if (p2p->go_neg_peer->connect_reqs >= 120) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004020 p2p_dbg(p2p, "Timeout on sending GO Negotiation Request without getting response");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004021 p2p_go_neg_failed(p2p, -1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004022 return;
4023 }
4024
4025 p2p_set_state(p2p, P2P_CONNECT);
4026 p2p_connect_send(p2p, p2p->go_neg_peer);
4027 } else
4028 p2p_set_state(p2p, P2P_IDLE);
4029}
4030
4031
4032static void p2p_timeout_wait_peer_connect(struct p2p_data *p2p)
4033{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004034 p2p_set_state(p2p, P2P_WAIT_PEER_IDLE);
Dmitry Shmidt18463232014-01-24 12:29:41 -08004035
4036 if (p2p->cfg->is_concurrent_session_active &&
4037 p2p->cfg->is_concurrent_session_active(p2p->cfg->cb_ctx))
4038 p2p_set_timeout(p2p, 0, 500000);
4039 else
4040 p2p_set_timeout(p2p, 0, 200000);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004041}
4042
4043
4044static void p2p_timeout_wait_peer_idle(struct p2p_data *p2p)
4045{
4046 struct p2p_device *dev = p2p->go_neg_peer;
4047
4048 if (dev == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004049 p2p_dbg(p2p, "Unknown GO Neg peer - stop GO Neg wait");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004050 return;
4051 }
4052
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004053 p2p_dbg(p2p, "Go to Listen state while waiting for the peer to become ready for GO Negotiation");
Hai Shalom899fcc72020-10-19 14:38:18 -07004054 p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
Sunil Ravib0ac25f2024-07-12 01:42:03 +00004055 p2p->pending_listen_wait_drv = false;
Hai Shaloma20dcd72022-02-04 13:43:00 -08004056 if (p2p->pending_listen_freq) {
4057 p2p_dbg(p2p, "Clear pending_listen_freq for %s", __func__);
4058 p2p->pending_listen_freq = 0;
4059 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004060 p2p_set_state(p2p, P2P_WAIT_PEER_CONNECT);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004061 p2p_listen_in_find(p2p, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004062}
4063
4064
4065static void p2p_timeout_sd_during_find(struct p2p_data *p2p)
4066{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004067 p2p_dbg(p2p, "Service Discovery Query timeout");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004068 if (p2p->sd_peer) {
4069 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004070 p2p->sd_peer = NULL;
4071 }
4072 p2p_continue_find(p2p);
4073}
4074
4075
4076static void p2p_timeout_prov_disc_during_find(struct p2p_data *p2p)
4077{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004078 p2p_dbg(p2p, "Provision Discovery Request timeout");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004079 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
4080 p2p_continue_find(p2p);
4081}
4082
4083
Jouni Malinen75ecf522011-06-27 15:19:46 -07004084static void p2p_timeout_prov_disc_req(struct p2p_data *p2p)
4085{
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004086 u32 adv_id = 0;
4087 u8 *adv_mac = NULL;
4088
Jouni Malinen75ecf522011-06-27 15:19:46 -07004089 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
4090
4091 /*
4092 * For user initiated PD requests that we have not gotten any responses
4093 * for while in IDLE state, we retry them a couple of times before
4094 * giving up.
4095 */
4096 if (!p2p->user_initiated_pd)
4097 return;
4098
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004099 p2p_dbg(p2p, "User initiated Provision Discovery Request timeout");
Jouni Malinen75ecf522011-06-27 15:19:46 -07004100
4101 if (p2p->pd_retries) {
4102 p2p->pd_retries--;
4103 p2p_retry_pd(p2p);
4104 } else {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004105 struct p2p_device *dev;
4106 int for_join = 0;
4107
4108 dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
Sunil Ravib0ac25f2024-07-12 01:42:03 +00004109 if (!ether_addr_equal(p2p->pending_pd_devaddr,
4110 dev->info.p2p_device_addr))
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004111 continue;
4112 if (dev->req_config_methods &&
4113 (dev->flags & P2P_DEV_PD_FOR_JOIN))
4114 for_join = 1;
4115 }
4116
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004117 if (p2p->p2ps_prov) {
4118 adv_id = p2p->p2ps_prov->adv_id;
4119 adv_mac = p2p->p2ps_prov->adv_mac;
4120 }
4121
Jouni Malinen75ecf522011-06-27 15:19:46 -07004122 if (p2p->cfg->prov_disc_fail)
4123 p2p->cfg->prov_disc_fail(p2p->cfg->cb_ctx,
4124 p2p->pending_pd_devaddr,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004125 for_join ?
4126 P2P_PROV_DISC_TIMEOUT_JOIN :
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004127 P2P_PROV_DISC_TIMEOUT,
4128 adv_id, adv_mac, NULL);
Jouni Malinen75ecf522011-06-27 15:19:46 -07004129 p2p_reset_pending_pd(p2p);
4130 }
4131}
4132
4133
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004134static void p2p_timeout_invite(struct p2p_data *p2p)
4135{
4136 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
4137 p2p_set_state(p2p, P2P_INVITE_LISTEN);
4138 if (p2p->inv_role == P2P_INVITE_ROLE_ACTIVE_GO) {
4139 /*
4140 * Better remain on operating channel instead of listen channel
4141 * when running a group.
Sunil Ravi036cec52023-03-29 11:35:17 -07004142 * Wait 120 ms to let the P2P GO to send its beacon on the
4143 * intended TBTT.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004144 */
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004145 p2p_dbg(p2p, "Inviting in active GO role - wait on operating channel");
Sunil Ravi036cec52023-03-29 11:35:17 -07004146 p2p_set_timeout(p2p, 0, 120000);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004147 return;
4148 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004149 p2p_listen_in_find(p2p, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004150}
4151
4152
4153static void p2p_timeout_invite_listen(struct p2p_data *p2p)
4154{
4155 if (p2p->invite_peer && p2p->invite_peer->invitation_reqs < 100) {
4156 p2p_set_state(p2p, P2P_INVITE);
4157 p2p_invite_send(p2p, p2p->invite_peer,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004158 p2p->invite_go_dev_addr, p2p->invite_dev_pw_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004159 } else {
4160 if (p2p->invite_peer) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004161 p2p_dbg(p2p, "Invitation Request retry limit reached");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004162 if (p2p->cfg->invitation_result)
4163 p2p->cfg->invitation_result(
Dmitry Shmidt700a1372013-03-15 14:14:44 -07004164 p2p->cfg->cb_ctx, -1, NULL, NULL,
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004165 p2p->invite_peer->info.p2p_device_addr,
Dmitry Shmidt15907092014-03-25 10:42:57 -07004166 0, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004167 }
4168 p2p_set_state(p2p, P2P_IDLE);
4169 }
4170}
4171
4172
4173static void p2p_state_timeout(void *eloop_ctx, void *timeout_ctx)
4174{
4175 struct p2p_data *p2p = eloop_ctx;
4176
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004177 p2p_dbg(p2p, "Timeout (state=%s)", p2p_state_txt(p2p->state));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004178
4179 p2p->in_listen = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004180 if (p2p->drv_in_listen) {
4181 p2p_dbg(p2p, "Driver is still in listen state - stop it");
4182 p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
Sunil Ravib0ac25f2024-07-12 01:42:03 +00004183 p2p->pending_listen_wait_drv = false;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004184 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004185
4186 switch (p2p->state) {
4187 case P2P_IDLE:
Jouni Malinen75ecf522011-06-27 15:19:46 -07004188 /* Check if we timed out waiting for PD req */
4189 if (p2p->pending_action_state == P2P_PENDING_PD)
4190 p2p_timeout_prov_disc_req(p2p);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004191 break;
4192 case P2P_SEARCH:
Jouni Malinen75ecf522011-06-27 15:19:46 -07004193 /* Check if we timed out waiting for PD req */
4194 if (p2p->pending_action_state == P2P_PENDING_PD)
4195 p2p_timeout_prov_disc_req(p2p);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004196 if (p2p->search_delay && !p2p->in_search_delay) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004197 p2p_dbg(p2p, "Delay search operation by %u ms",
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004198 p2p->search_delay);
4199 p2p->in_search_delay = 1;
4200 p2p_set_timeout(p2p, p2p->search_delay / 1000,
4201 (p2p->search_delay % 1000) * 1000);
4202 break;
4203 }
4204 p2p->in_search_delay = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004205 p2p_search(p2p);
4206 break;
4207 case P2P_CONNECT:
4208 p2p_timeout_connect(p2p);
4209 break;
4210 case P2P_CONNECT_LISTEN:
4211 p2p_timeout_connect_listen(p2p);
4212 break;
4213 case P2P_GO_NEG:
4214 break;
4215 case P2P_LISTEN_ONLY:
Jouni Malinen75ecf522011-06-27 15:19:46 -07004216 /* Check if we timed out waiting for PD req */
4217 if (p2p->pending_action_state == P2P_PENDING_PD)
4218 p2p_timeout_prov_disc_req(p2p);
4219
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004220 if (p2p->ext_listen_only) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004221 p2p_dbg(p2p, "Extended Listen Timing - Listen State completed");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004222 p2p->ext_listen_only = 0;
4223 p2p_set_state(p2p, P2P_IDLE);
4224 }
4225 break;
4226 case P2P_WAIT_PEER_CONNECT:
4227 p2p_timeout_wait_peer_connect(p2p);
4228 break;
4229 case P2P_WAIT_PEER_IDLE:
4230 p2p_timeout_wait_peer_idle(p2p);
4231 break;
4232 case P2P_SD_DURING_FIND:
4233 p2p_timeout_sd_during_find(p2p);
4234 break;
4235 case P2P_PROVISIONING:
4236 break;
4237 case P2P_PD_DURING_FIND:
4238 p2p_timeout_prov_disc_during_find(p2p);
4239 break;
4240 case P2P_INVITE:
4241 p2p_timeout_invite(p2p);
4242 break;
4243 case P2P_INVITE_LISTEN:
4244 p2p_timeout_invite_listen(p2p);
4245 break;
4246 }
4247}
4248
4249
4250int p2p_reject(struct p2p_data *p2p, const u8 *peer_addr)
4251{
4252 struct p2p_device *dev;
4253
4254 dev = p2p_get_device(p2p, peer_addr);
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004255 p2p_dbg(p2p, "Local request to reject connection attempts by peer "
4256 MACSTR, MAC2STR(peer_addr));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004257 if (dev == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004258 p2p_dbg(p2p, "Peer " MACSTR " unknown", MAC2STR(peer_addr));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004259 return -1;
4260 }
4261 dev->status = P2P_SC_FAIL_REJECTED_BY_USER;
4262 dev->flags |= P2P_DEV_USER_REJECTED;
4263 return 0;
4264}
4265
4266
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004267const char * p2p_wps_method_text(enum p2p_wps_method method)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004268{
4269 switch (method) {
4270 case WPS_NOT_READY:
4271 return "not-ready";
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004272 case WPS_PIN_DISPLAY:
4273 return "Display";
4274 case WPS_PIN_KEYPAD:
4275 return "Keypad";
4276 case WPS_PBC:
4277 return "PBC";
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004278 case WPS_NFC:
4279 return "NFC";
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004280 case WPS_P2PS:
4281 return "P2PS";
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004282 }
4283
4284 return "??";
4285}
4286
4287
4288static const char * p2p_go_state_text(enum p2p_go_state go_state)
4289{
4290 switch (go_state) {
4291 case UNKNOWN_GO:
4292 return "unknown";
4293 case LOCAL_GO:
4294 return "local";
4295 case REMOTE_GO:
4296 return "remote";
4297 }
4298
4299 return "??";
4300}
4301
4302
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004303const struct p2p_peer_info * p2p_get_peer_info(struct p2p_data *p2p,
4304 const u8 *addr, int next)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004305{
4306 struct p2p_device *dev;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004307
4308 if (addr)
4309 dev = p2p_get_device(p2p, addr);
4310 else
4311 dev = dl_list_first(&p2p->devices, struct p2p_device, list);
4312
4313 if (dev && next) {
4314 dev = dl_list_first(&dev->list, struct p2p_device, list);
4315 if (&dev->list == &p2p->devices)
4316 dev = NULL;
4317 }
4318
4319 if (dev == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004320 return NULL;
4321
4322 return &dev->info;
4323}
4324
4325
4326int p2p_get_peer_info_txt(const struct p2p_peer_info *info,
4327 char *buf, size_t buflen)
4328{
4329 struct p2p_device *dev;
4330 int res;
4331 char *pos, *end;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004332 struct os_reltime now;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004333
4334 if (info == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004335 return -1;
4336
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004337 dev = (struct p2p_device *) (((u8 *) info) -
4338 offsetof(struct p2p_device, info));
4339
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004340 pos = buf;
4341 end = buf + buflen;
4342
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004343 os_get_reltime(&now);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004344 res = os_snprintf(pos, end - pos,
4345 "age=%d\n"
4346 "listen_freq=%d\n"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004347 "wps_method=%s\n"
4348 "interface_addr=" MACSTR "\n"
4349 "member_in_go_dev=" MACSTR "\n"
4350 "member_in_go_iface=" MACSTR "\n"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004351 "go_neg_req_sent=%d\n"
4352 "go_state=%s\n"
4353 "dialog_token=%u\n"
4354 "intended_addr=" MACSTR "\n"
4355 "country=%c%c\n"
4356 "oper_freq=%d\n"
4357 "req_config_methods=0x%x\n"
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07004358 "flags=%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004359 "status=%d\n"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004360 "invitation_reqs=%u\n",
4361 (int) (now.sec - dev->last_seen.sec),
4362 dev->listen_freq,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004363 p2p_wps_method_text(dev->wps_method),
4364 MAC2STR(dev->interface_addr),
4365 MAC2STR(dev->member_in_go_dev),
4366 MAC2STR(dev->member_in_go_iface),
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004367 dev->go_neg_req_sent,
4368 p2p_go_state_text(dev->go_state),
4369 dev->dialog_token,
4370 MAC2STR(dev->intended_addr),
4371 dev->country[0] ? dev->country[0] : '_',
4372 dev->country[1] ? dev->country[1] : '_',
4373 dev->oper_freq,
4374 dev->req_config_methods,
4375 dev->flags & P2P_DEV_PROBE_REQ_ONLY ?
4376 "[PROBE_REQ_ONLY]" : "",
4377 dev->flags & P2P_DEV_REPORTED ? "[REPORTED]" : "",
4378 dev->flags & P2P_DEV_NOT_YET_READY ?
4379 "[NOT_YET_READY]" : "",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004380 dev->flags & P2P_DEV_PD_PEER_DISPLAY ?
4381 "[PD_PEER_DISPLAY]" : "",
4382 dev->flags & P2P_DEV_PD_PEER_KEYPAD ?
4383 "[PD_PEER_KEYPAD]" : "",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004384 dev->flags & P2P_DEV_PD_PEER_P2PS ?
4385 "[PD_PEER_P2PS]" : "",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004386 dev->flags & P2P_DEV_USER_REJECTED ?
4387 "[USER_REJECTED]" : "",
4388 dev->flags & P2P_DEV_PEER_WAITING_RESPONSE ?
4389 "[PEER_WAITING_RESPONSE]" : "",
4390 dev->flags & P2P_DEV_PREFER_PERSISTENT_GROUP ?
4391 "[PREFER_PERSISTENT_GROUP]" : "",
4392 dev->flags & P2P_DEV_WAIT_GO_NEG_RESPONSE ?
4393 "[WAIT_GO_NEG_RESPONSE]" : "",
4394 dev->flags & P2P_DEV_WAIT_GO_NEG_CONFIRM ?
4395 "[WAIT_GO_NEG_CONFIRM]" : "",
4396 dev->flags & P2P_DEV_GROUP_CLIENT_ONLY ?
4397 "[GROUP_CLIENT_ONLY]" : "",
4398 dev->flags & P2P_DEV_FORCE_FREQ ?
4399 "[FORCE_FREQ]" : "",
4400 dev->flags & P2P_DEV_PD_FOR_JOIN ?
4401 "[PD_FOR_JOIN]" : "",
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07004402 dev->flags & P2P_DEV_LAST_SEEN_AS_GROUP_CLIENT ?
4403 "[LAST_SEEN_AS_GROUP_CLIENT]" : "",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004404 dev->status,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004405 dev->invitation_reqs);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004406 if (os_snprintf_error(end - pos, res))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004407 return pos - buf;
4408 pos += res;
4409
4410 if (dev->ext_listen_period) {
4411 res = os_snprintf(pos, end - pos,
4412 "ext_listen_period=%u\n"
4413 "ext_listen_interval=%u\n",
4414 dev->ext_listen_period,
4415 dev->ext_listen_interval);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004416 if (os_snprintf_error(end - pos, res))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004417 return pos - buf;
4418 pos += res;
4419 }
4420
4421 if (dev->oper_ssid_len) {
4422 res = os_snprintf(pos, end - pos,
4423 "oper_ssid=%s\n",
4424 wpa_ssid_txt(dev->oper_ssid,
4425 dev->oper_ssid_len));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004426 if (os_snprintf_error(end - pos, res))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004427 return pos - buf;
4428 pos += res;
4429 }
4430
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004431#ifdef CONFIG_WIFI_DISPLAY
4432 if (dev->info.wfd_subelems) {
4433 res = os_snprintf(pos, end - pos, "wfd_subelems=");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004434 if (os_snprintf_error(end - pos, res))
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004435 return pos - buf;
4436 pos += res;
4437
4438 pos += wpa_snprintf_hex(pos, end - pos,
4439 wpabuf_head(dev->info.wfd_subelems),
4440 wpabuf_len(dev->info.wfd_subelems));
4441
4442 res = os_snprintf(pos, end - pos, "\n");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004443 if (os_snprintf_error(end - pos, res))
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004444 return pos - buf;
4445 pos += res;
4446 }
4447#endif /* CONFIG_WIFI_DISPLAY */
4448
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004449 return pos - buf;
4450}
4451
4452
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004453int p2p_peer_known(struct p2p_data *p2p, const u8 *addr)
4454{
4455 return p2p_get_device(p2p, addr) != NULL;
4456}
4457
4458
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004459void p2p_set_client_discoverability(struct p2p_data *p2p, int enabled)
4460{
4461 if (enabled) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004462 p2p_dbg(p2p, "Client discoverability enabled");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004463 p2p->dev_capab |= P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
4464 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004465 p2p_dbg(p2p, "Client discoverability disabled");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004466 p2p->dev_capab &= ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
4467 }
4468}
4469
4470
4471static struct wpabuf * p2p_build_presence_req(u32 duration1, u32 interval1,
4472 u32 duration2, u32 interval2)
4473{
4474 struct wpabuf *req;
4475 struct p2p_noa_desc desc1, desc2, *ptr1 = NULL, *ptr2 = NULL;
4476 u8 *len;
4477
4478 req = wpabuf_alloc(100);
4479 if (req == NULL)
4480 return NULL;
4481
4482 if (duration1 || interval1) {
4483 os_memset(&desc1, 0, sizeof(desc1));
4484 desc1.count_type = 1;
4485 desc1.duration = duration1;
4486 desc1.interval = interval1;
4487 ptr1 = &desc1;
4488
4489 if (duration2 || interval2) {
4490 os_memset(&desc2, 0, sizeof(desc2));
4491 desc2.count_type = 2;
4492 desc2.duration = duration2;
4493 desc2.interval = interval2;
4494 ptr2 = &desc2;
4495 }
4496 }
4497
4498 p2p_buf_add_action_hdr(req, P2P_PRESENCE_REQ, 1);
4499 len = p2p_buf_add_ie_hdr(req);
4500 p2p_buf_add_noa(req, 0, 0, 0, ptr1, ptr2);
4501 p2p_buf_update_ie_hdr(req, len);
4502
4503 return req;
4504}
4505
4506
4507int p2p_presence_req(struct p2p_data *p2p, const u8 *go_interface_addr,
4508 const u8 *own_interface_addr, unsigned int freq,
4509 u32 duration1, u32 interval1, u32 duration2,
4510 u32 interval2)
4511{
4512 struct wpabuf *req;
4513
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004514 p2p_dbg(p2p, "Send Presence Request to GO " MACSTR
4515 " (own interface " MACSTR ") freq=%u dur1=%u int1=%u "
4516 "dur2=%u int2=%u",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004517 MAC2STR(go_interface_addr), MAC2STR(own_interface_addr),
4518 freq, duration1, interval1, duration2, interval2);
4519
4520 req = p2p_build_presence_req(duration1, interval1, duration2,
4521 interval2);
4522 if (req == NULL)
4523 return -1;
4524
4525 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
4526 if (p2p_send_action(p2p, freq, go_interface_addr, own_interface_addr,
4527 go_interface_addr,
4528 wpabuf_head(req), wpabuf_len(req), 200) < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004529 p2p_dbg(p2p, "Failed to send Action frame");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004530 }
4531 wpabuf_free(req);
4532
4533 return 0;
4534}
4535
4536
4537static struct wpabuf * p2p_build_presence_resp(u8 status, const u8 *noa,
4538 size_t noa_len, u8 dialog_token)
4539{
4540 struct wpabuf *resp;
4541 u8 *len;
4542
4543 resp = wpabuf_alloc(100 + noa_len);
4544 if (resp == NULL)
4545 return NULL;
4546
4547 p2p_buf_add_action_hdr(resp, P2P_PRESENCE_RESP, dialog_token);
4548 len = p2p_buf_add_ie_hdr(resp);
4549 p2p_buf_add_status(resp, status);
4550 if (noa) {
4551 wpabuf_put_u8(resp, P2P_ATTR_NOTICE_OF_ABSENCE);
4552 wpabuf_put_le16(resp, noa_len);
4553 wpabuf_put_data(resp, noa, noa_len);
4554 } else
4555 p2p_buf_add_noa(resp, 0, 0, 0, NULL, NULL);
4556 p2p_buf_update_ie_hdr(resp, len);
4557
4558 return resp;
4559}
4560
4561
4562static void p2p_process_presence_req(struct p2p_data *p2p, const u8 *da,
4563 const u8 *sa, const u8 *data, size_t len,
4564 int rx_freq)
4565{
4566 struct p2p_message msg;
4567 u8 status;
4568 struct wpabuf *resp;
4569 size_t g;
4570 struct p2p_group *group = NULL;
4571 int parsed = 0;
4572 u8 noa[50];
4573 int noa_len;
4574
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004575 p2p_dbg(p2p, "Received P2P Action - P2P Presence Request");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004576
4577 for (g = 0; g < p2p->num_groups; g++) {
Sunil Ravib0ac25f2024-07-12 01:42:03 +00004578 if (ether_addr_equal(
4579 da, p2p_group_get_interface_addr(p2p->groups[g]))) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004580 group = p2p->groups[g];
4581 break;
4582 }
4583 }
4584 if (group == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004585 p2p_dbg(p2p, "Ignore P2P Presence Request for unknown group "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004586 MACSTR, MAC2STR(da));
4587 return;
4588 }
4589
4590 if (p2p_parse(data, len, &msg) < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004591 p2p_dbg(p2p, "Failed to parse P2P Presence Request");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004592 status = P2P_SC_FAIL_INVALID_PARAMS;
4593 goto fail;
4594 }
4595 parsed = 1;
4596
4597 if (msg.noa == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004598 p2p_dbg(p2p, "No NoA attribute in P2P Presence Request");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004599 status = P2P_SC_FAIL_INVALID_PARAMS;
4600 goto fail;
4601 }
4602
4603 status = p2p_group_presence_req(group, sa, msg.noa, msg.noa_len);
4604
4605fail:
4606 if (p2p->cfg->get_noa)
4607 noa_len = p2p->cfg->get_noa(p2p->cfg->cb_ctx, da, noa,
4608 sizeof(noa));
4609 else
4610 noa_len = -1;
4611 resp = p2p_build_presence_resp(status, noa_len > 0 ? noa : NULL,
4612 noa_len > 0 ? noa_len : 0,
4613 msg.dialog_token);
4614 if (parsed)
4615 p2p_parse_free(&msg);
4616 if (resp == NULL)
4617 return;
4618
4619 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
4620 if (p2p_send_action(p2p, rx_freq, sa, da, da,
4621 wpabuf_head(resp), wpabuf_len(resp), 200) < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004622 p2p_dbg(p2p, "Failed to send Action frame");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004623 }
4624 wpabuf_free(resp);
4625}
4626
4627
4628static void p2p_process_presence_resp(struct p2p_data *p2p, const u8 *da,
4629 const u8 *sa, const u8 *data, size_t len)
4630{
4631 struct p2p_message msg;
4632
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004633 p2p_dbg(p2p, "Received P2P Action - P2P Presence Response");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004634
4635 if (p2p_parse(data, len, &msg) < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004636 p2p_dbg(p2p, "Failed to parse P2P Presence Response");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004637 return;
4638 }
4639
4640 if (msg.status == NULL || msg.noa == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004641 p2p_dbg(p2p, "No Status or NoA attribute in P2P Presence Response");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004642 p2p_parse_free(&msg);
4643 return;
4644 }
4645
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004646 if (p2p->cfg->presence_resp) {
4647 p2p->cfg->presence_resp(p2p->cfg->cb_ctx, sa, *msg.status,
4648 msg.noa, msg.noa_len);
4649 }
4650
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004651 if (*msg.status) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004652 p2p_dbg(p2p, "P2P Presence Request was rejected: status %u",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004653 *msg.status);
4654 p2p_parse_free(&msg);
4655 return;
4656 }
4657
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004658 p2p_dbg(p2p, "P2P Presence Request was accepted");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004659 wpa_hexdump(MSG_DEBUG, "P2P: P2P Presence Response - NoA",
4660 msg.noa, msg.noa_len);
4661 /* TODO: process NoA */
4662 p2p_parse_free(&msg);
4663}
4664
4665
4666static void p2p_ext_listen_timeout(void *eloop_ctx, void *timeout_ctx)
4667{
4668 struct p2p_data *p2p = eloop_ctx;
4669
4670 if (p2p->ext_listen_interval) {
4671 /* Schedule next extended listen timeout */
4672 eloop_register_timeout(p2p->ext_listen_interval_sec,
4673 p2p->ext_listen_interval_usec,
4674 p2p_ext_listen_timeout, p2p, NULL);
4675 }
4676
4677 if (p2p->state == P2P_LISTEN_ONLY && p2p->ext_listen_only) {
4678 /*
4679 * This should not really happen, but it looks like the Listen
4680 * command may fail is something else (e.g., a scan) was
4681 * running at an inconvenient time. As a workaround, allow new
4682 * Extended Listen operation to be started.
4683 */
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004684 p2p_dbg(p2p, "Previous Extended Listen operation had not been completed - try again");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004685 p2p->ext_listen_only = 0;
4686 p2p_set_state(p2p, P2P_IDLE);
4687 }
4688
Sunil Ravib0ac25f2024-07-12 01:42:03 +00004689 if ((p2p->cfg->is_p2p_in_progress &&
4690 p2p->cfg->is_p2p_in_progress(p2p->cfg->cb_ctx)) ||
4691 (p2p->pending_action_state == P2P_PENDING_PD &&
4692 p2p->pd_retries > 0)) {
4693 p2p_dbg(p2p, "Operation in progress - skip Extended Listen timeout (%s)",
4694 p2p_state_txt(p2p->state));
4695 return;
4696 }
4697
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004698 if (p2p->state != P2P_IDLE) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004699 p2p_dbg(p2p, "Skip Extended Listen timeout in active state (%s)", p2p_state_txt(p2p->state));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004700 return;
4701 }
4702
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004703 p2p_dbg(p2p, "Extended Listen timeout");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004704 p2p->ext_listen_only = 1;
4705 if (p2p_listen(p2p, p2p->ext_listen_period) < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004706 p2p_dbg(p2p, "Failed to start Listen state for Extended Listen Timing");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004707 p2p->ext_listen_only = 0;
4708 }
4709}
4710
4711
4712int p2p_ext_listen(struct p2p_data *p2p, unsigned int period,
4713 unsigned int interval)
4714{
4715 if (period > 65535 || interval > 65535 || period > interval ||
4716 (period == 0 && interval > 0) || (period > 0 && interval == 0)) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004717 p2p_dbg(p2p, "Invalid Extended Listen Timing request: period=%u interval=%u",
4718 period, interval);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004719 return -1;
4720 }
4721
4722 eloop_cancel_timeout(p2p_ext_listen_timeout, p2p, NULL);
4723
4724 if (interval == 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004725 p2p_dbg(p2p, "Disabling Extended Listen Timing");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004726 p2p->ext_listen_period = 0;
4727 p2p->ext_listen_interval = 0;
4728 return 0;
4729 }
4730
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004731 p2p_dbg(p2p, "Enabling Extended Listen Timing: period %u msec, interval %u msec",
4732 period, interval);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004733 p2p->ext_listen_period = period;
4734 p2p->ext_listen_interval = interval;
4735 p2p->ext_listen_interval_sec = interval / 1000;
4736 p2p->ext_listen_interval_usec = (interval % 1000) * 1000;
4737
4738 eloop_register_timeout(p2p->ext_listen_interval_sec,
4739 p2p->ext_listen_interval_usec,
4740 p2p_ext_listen_timeout, p2p, NULL);
4741
4742 return 0;
4743}
4744
4745
4746void p2p_deauth_notif(struct p2p_data *p2p, const u8 *bssid, u16 reason_code,
4747 const u8 *ie, size_t ie_len)
4748{
4749 struct p2p_message msg;
4750
4751 if (bssid == NULL || ie == NULL)
4752 return;
4753
4754 os_memset(&msg, 0, sizeof(msg));
4755 if (p2p_parse_ies(ie, ie_len, &msg))
4756 return;
Dmitry Shmidt97672262014-02-03 13:02:54 -08004757 if (msg.minor_reason_code == NULL) {
4758 p2p_parse_free(&msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004759 return;
Dmitry Shmidt97672262014-02-03 13:02:54 -08004760 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004761
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004762 p2p_dbg(p2p, "Deauthentication notification BSSID " MACSTR
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004763 " reason_code=%u minor_reason_code=%u",
4764 MAC2STR(bssid), reason_code, *msg.minor_reason_code);
4765
4766 p2p_parse_free(&msg);
4767}
4768
4769
4770void p2p_disassoc_notif(struct p2p_data *p2p, const u8 *bssid, u16 reason_code,
4771 const u8 *ie, size_t ie_len)
4772{
4773 struct p2p_message msg;
4774
4775 if (bssid == NULL || ie == NULL)
4776 return;
4777
4778 os_memset(&msg, 0, sizeof(msg));
4779 if (p2p_parse_ies(ie, ie_len, &msg))
4780 return;
Dmitry Shmidt97672262014-02-03 13:02:54 -08004781 if (msg.minor_reason_code == NULL) {
4782 p2p_parse_free(&msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004783 return;
Dmitry Shmidt97672262014-02-03 13:02:54 -08004784 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004785
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004786 p2p_dbg(p2p, "Disassociation notification BSSID " MACSTR
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004787 " reason_code=%u minor_reason_code=%u",
4788 MAC2STR(bssid), reason_code, *msg.minor_reason_code);
4789
4790 p2p_parse_free(&msg);
4791}
4792
4793
4794void p2p_set_managed_oper(struct p2p_data *p2p, int enabled)
4795{
4796 if (enabled) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004797 p2p_dbg(p2p, "Managed P2P Device operations enabled");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004798 p2p->dev_capab |= P2P_DEV_CAPAB_INFRA_MANAGED;
4799 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004800 p2p_dbg(p2p, "Managed P2P Device operations disabled");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004801 p2p->dev_capab &= ~P2P_DEV_CAPAB_INFRA_MANAGED;
4802 }
4803}
4804
4805
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004806int p2p_config_get_random_social(struct p2p_config *p2p, u8 *op_class,
Hai Shalom74f70d42019-02-11 14:42:39 -08004807 u8 *op_channel,
4808 struct wpa_freq_range_list *avoid_list,
4809 struct wpa_freq_range_list *disallow_list)
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004810{
Hai Shalom74f70d42019-02-11 14:42:39 -08004811 return p2p_channel_random_social(&p2p->channels, op_class, op_channel,
4812 avoid_list, disallow_list);
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004813}
4814
4815
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004816int p2p_set_listen_channel(struct p2p_data *p2p, u8 reg_class, u8 channel,
4817 u8 forced)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004818{
Dmitry Shmidt4b060592013-04-29 16:42:49 -07004819 if (p2p_channel_to_freq(reg_class, channel) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004820 return -1;
4821
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004822 /*
4823 * Listen channel was set in configuration or set by control interface;
4824 * cannot override it.
4825 */
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004826 if (p2p->cfg->channel_forced && forced == 0) {
4827 p2p_dbg(p2p,
4828 "Listen channel was previously configured - do not override based on optimization");
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004829 return -1;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004830 }
4831
4832 p2p_dbg(p2p, "Set Listen channel: reg_class %u channel %u",
4833 reg_class, channel);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004834
4835 if (p2p->state == P2P_IDLE) {
4836 p2p->cfg->reg_class = reg_class;
4837 p2p->cfg->channel = channel;
4838 p2p->cfg->channel_forced = forced;
4839 } else {
4840 p2p_dbg(p2p, "Defer setting listen channel");
4841 p2p->pending_reg_class = reg_class;
4842 p2p->pending_channel = channel;
4843 p2p->pending_channel_forced = forced;
4844 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004845
4846 return 0;
4847}
4848
4849
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004850u8 p2p_get_listen_channel(struct p2p_data *p2p)
4851{
4852 return p2p->cfg->channel;
4853}
4854
4855
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004856int p2p_set_ssid_postfix(struct p2p_data *p2p, const u8 *postfix, size_t len)
4857{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004858 p2p_dbg(p2p, "New SSID postfix: %s", wpa_ssid_txt(postfix, len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004859 if (postfix == NULL) {
4860 p2p->cfg->ssid_postfix_len = 0;
4861 return 0;
4862 }
4863 if (len > sizeof(p2p->cfg->ssid_postfix))
4864 return -1;
4865 os_memcpy(p2p->cfg->ssid_postfix, postfix, len);
4866 p2p->cfg->ssid_postfix_len = len;
4867 return 0;
4868}
4869
4870
Jouni Malinen75ecf522011-06-27 15:19:46 -07004871int p2p_set_oper_channel(struct p2p_data *p2p, u8 op_reg_class, u8 op_channel,
4872 int cfg_op_channel)
4873{
Dmitry Shmidt4b060592013-04-29 16:42:49 -07004874 if (p2p_channel_to_freq(op_reg_class, op_channel) < 0)
Jouni Malinen75ecf522011-06-27 15:19:46 -07004875 return -1;
4876
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004877 p2p_dbg(p2p, "Set Operating channel: reg_class %u channel %u",
4878 op_reg_class, op_channel);
Jouni Malinen75ecf522011-06-27 15:19:46 -07004879 p2p->cfg->op_reg_class = op_reg_class;
4880 p2p->cfg->op_channel = op_channel;
4881 p2p->cfg->cfg_op_channel = cfg_op_channel;
4882 return 0;
4883}
4884
4885
Dmitry Shmidt04949592012-07-19 12:16:46 -07004886int p2p_set_pref_chan(struct p2p_data *p2p, unsigned int num_pref_chan,
4887 const struct p2p_channel *pref_chan)
4888{
4889 struct p2p_channel *n;
4890
4891 if (pref_chan) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004892 n = os_memdup(pref_chan,
4893 num_pref_chan * sizeof(struct p2p_channel));
Dmitry Shmidt04949592012-07-19 12:16:46 -07004894 if (n == NULL)
4895 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004896 } else
4897 n = NULL;
4898
4899 os_free(p2p->cfg->pref_chan);
4900 p2p->cfg->pref_chan = n;
4901 p2p->cfg->num_pref_chan = num_pref_chan;
4902
4903 return 0;
4904}
4905
4906
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004907int p2p_set_no_go_freq(struct p2p_data *p2p,
4908 const struct wpa_freq_range_list *list)
4909{
4910 struct wpa_freq_range *tmp;
4911
4912 if (list == NULL || list->num == 0) {
4913 os_free(p2p->no_go_freq.range);
4914 p2p->no_go_freq.range = NULL;
4915 p2p->no_go_freq.num = 0;
4916 return 0;
4917 }
4918
4919 tmp = os_calloc(list->num, sizeof(struct wpa_freq_range));
4920 if (tmp == NULL)
4921 return -1;
4922 os_memcpy(tmp, list->range, list->num * sizeof(struct wpa_freq_range));
4923 os_free(p2p->no_go_freq.range);
4924 p2p->no_go_freq.range = tmp;
4925 p2p->no_go_freq.num = list->num;
4926 p2p_dbg(p2p, "Updated no GO chan list");
4927
4928 return 0;
4929}
4930
4931
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004932int p2p_get_interface_addr(struct p2p_data *p2p, const u8 *dev_addr,
4933 u8 *iface_addr)
4934{
4935 struct p2p_device *dev = p2p_get_device(p2p, dev_addr);
4936 if (dev == NULL || is_zero_ether_addr(dev->interface_addr))
4937 return -1;
4938 os_memcpy(iface_addr, dev->interface_addr, ETH_ALEN);
4939 return 0;
4940}
4941
4942
4943int p2p_get_dev_addr(struct p2p_data *p2p, const u8 *iface_addr,
4944 u8 *dev_addr)
4945{
4946 struct p2p_device *dev = p2p_get_device_interface(p2p, iface_addr);
4947 if (dev == NULL)
4948 return -1;
4949 os_memcpy(dev_addr, dev->info.p2p_device_addr, ETH_ALEN);
4950 return 0;
4951}
4952
4953
4954void p2p_set_peer_filter(struct p2p_data *p2p, const u8 *addr)
4955{
4956 os_memcpy(p2p->peer_filter, addr, ETH_ALEN);
4957 if (is_zero_ether_addr(p2p->peer_filter))
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004958 p2p_dbg(p2p, "Disable peer filter");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004959 else
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004960 p2p_dbg(p2p, "Enable peer filter for " MACSTR,
4961 MAC2STR(p2p->peer_filter));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004962}
4963
4964
4965void p2p_set_cross_connect(struct p2p_data *p2p, int enabled)
4966{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004967 p2p_dbg(p2p, "Cross connection %s", enabled ? "enabled" : "disabled");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004968 if (p2p->cross_connect == enabled)
4969 return;
4970 p2p->cross_connect = enabled;
4971 /* TODO: may need to tear down any action group where we are GO(?) */
4972}
4973
4974
4975int p2p_get_oper_freq(struct p2p_data *p2p, const u8 *iface_addr)
4976{
4977 struct p2p_device *dev = p2p_get_device_interface(p2p, iface_addr);
4978 if (dev == NULL)
4979 return -1;
4980 if (dev->oper_freq <= 0)
4981 return -1;
4982 return dev->oper_freq;
4983}
4984
4985
4986void p2p_set_intra_bss_dist(struct p2p_data *p2p, int enabled)
4987{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004988 p2p_dbg(p2p, "Intra BSS distribution %s",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004989 enabled ? "enabled" : "disabled");
4990 p2p->cfg->p2p_intra_bss = enabled;
4991}
4992
4993
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004994void p2p_update_channel_list(struct p2p_data *p2p,
4995 const struct p2p_channels *chan,
4996 const struct p2p_channels *cli_chan)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004997{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004998 p2p_dbg(p2p, "Update channel list");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004999 os_memcpy(&p2p->cfg->channels, chan, sizeof(struct p2p_channels));
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005000 p2p_channels_dump(p2p, "channels", &p2p->cfg->channels);
5001 os_memcpy(&p2p->cfg->cli_channels, cli_chan,
5002 sizeof(struct p2p_channels));
5003 p2p_channels_dump(p2p, "cli_channels", &p2p->cfg->cli_channels);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005004}
5005
5006
5007int p2p_send_action(struct p2p_data *p2p, unsigned int freq, const u8 *dst,
5008 const u8 *src, const u8 *bssid, const u8 *buf,
5009 size_t len, unsigned int wait_time)
5010{
Hai Shalom021b0b52019-04-10 11:17:58 -07005011 int res, scheduled;
5012
Hai Shalom021b0b52019-04-10 11:17:58 -07005013 res = p2p->cfg->send_action(p2p->cfg->cb_ctx, freq, dst, src, bssid,
5014 buf, len, wait_time, &scheduled);
5015 if (res == 0 && scheduled && p2p->in_listen && freq > 0 &&
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08005016 p2p->drv_in_listen > 0 &&
Hai Shalom021b0b52019-04-10 11:17:58 -07005017 (unsigned int) p2p->drv_in_listen != freq) {
5018 p2p_dbg(p2p,
5019 "Stop listen on %d MHz to allow a frame to be sent immediately on %d MHz",
5020 p2p->drv_in_listen, freq);
5021 p2p_stop_listen_for_freq(p2p, freq);
5022 }
5023 return res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005024}
5025
5026
5027void p2p_set_best_channels(struct p2p_data *p2p, int freq_24, int freq_5,
5028 int freq_overall)
5029{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005030 p2p_dbg(p2p, "Best channel: 2.4 GHz: %d, 5 GHz: %d, overall: %d",
5031 freq_24, freq_5, freq_overall);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005032 p2p->best_freq_24 = freq_24;
5033 p2p->best_freq_5 = freq_5;
5034 p2p->best_freq_overall = freq_overall;
5035}
5036
5037
Dmitry Shmidt700a1372013-03-15 14:14:44 -07005038void p2p_set_own_freq_preference(struct p2p_data *p2p, int freq)
5039{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005040 p2p_dbg(p2p, "Own frequency preference: %d MHz", freq);
Dmitry Shmidt700a1372013-03-15 14:14:44 -07005041 p2p->own_freq_preference = freq;
5042}
5043
5044
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005045const u8 * p2p_get_go_neg_peer(struct p2p_data *p2p)
5046{
5047 if (p2p == NULL || p2p->go_neg_peer == NULL)
5048 return NULL;
5049 return p2p->go_neg_peer->info.p2p_device_addr;
5050}
5051
5052
5053const struct p2p_peer_info *
5054p2p_get_peer_found(struct p2p_data *p2p, const u8 *addr, int next)
5055{
5056 struct p2p_device *dev;
5057
5058 if (addr) {
5059 dev = p2p_get_device(p2p, addr);
5060 if (!dev)
5061 return NULL;
5062
5063 if (!next) {
5064 if (dev->flags & P2P_DEV_PROBE_REQ_ONLY)
5065 return NULL;
5066
5067 return &dev->info;
5068 } else {
5069 do {
5070 dev = dl_list_first(&dev->list,
5071 struct p2p_device,
5072 list);
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07005073 if (!dev || &dev->list == &p2p->devices)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005074 return NULL;
5075 } while (dev->flags & P2P_DEV_PROBE_REQ_ONLY);
5076 }
5077 } else {
5078 dev = dl_list_first(&p2p->devices, struct p2p_device, list);
5079 if (!dev)
5080 return NULL;
5081 while (dev->flags & P2P_DEV_PROBE_REQ_ONLY) {
5082 dev = dl_list_first(&dev->list,
5083 struct p2p_device,
5084 list);
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07005085 if (!dev || &dev->list == &p2p->devices)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005086 return NULL;
5087 }
5088 }
5089
5090 return &dev->info;
5091}
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005092
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005093
5094int p2p_in_progress(struct p2p_data *p2p)
5095{
5096 if (p2p == NULL)
5097 return 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005098 if (p2p->state == P2P_SEARCH)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005099 return 2;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005100 return p2p->state != P2P_IDLE && p2p->state != P2P_PROVISIONING;
5101}
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005102
5103
5104void p2p_set_config_timeout(struct p2p_data *p2p, u8 go_timeout,
5105 u8 client_timeout)
5106{
5107 if (p2p) {
5108 p2p->go_timeout = go_timeout;
5109 p2p->client_timeout = client_timeout;
5110 }
5111}
5112
5113
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005114#ifdef CONFIG_WIFI_DISPLAY
5115
5116static void p2p_update_wfd_ie_groups(struct p2p_data *p2p)
5117{
5118 size_t g;
5119 struct p2p_group *group;
5120
5121 for (g = 0; g < p2p->num_groups; g++) {
5122 group = p2p->groups[g];
Dmitry Shmidtb96dad42013-11-05 10:07:29 -08005123 p2p_group_force_beacon_update_ies(group);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005124 }
5125}
5126
5127
5128int p2p_set_wfd_ie_beacon(struct p2p_data *p2p, struct wpabuf *ie)
5129{
5130 wpabuf_free(p2p->wfd_ie_beacon);
5131 p2p->wfd_ie_beacon = ie;
5132 p2p_update_wfd_ie_groups(p2p);
5133 return 0;
5134}
5135
5136
5137int p2p_set_wfd_ie_probe_req(struct p2p_data *p2p, struct wpabuf *ie)
5138{
5139 wpabuf_free(p2p->wfd_ie_probe_req);
5140 p2p->wfd_ie_probe_req = ie;
5141 return 0;
5142}
5143
5144
5145int p2p_set_wfd_ie_probe_resp(struct p2p_data *p2p, struct wpabuf *ie)
5146{
5147 wpabuf_free(p2p->wfd_ie_probe_resp);
5148 p2p->wfd_ie_probe_resp = ie;
5149 p2p_update_wfd_ie_groups(p2p);
5150 return 0;
5151}
5152
5153
5154int p2p_set_wfd_ie_assoc_req(struct p2p_data *p2p, struct wpabuf *ie)
5155{
5156 wpabuf_free(p2p->wfd_ie_assoc_req);
5157 p2p->wfd_ie_assoc_req = ie;
5158 return 0;
5159}
5160
5161
5162int p2p_set_wfd_ie_invitation(struct p2p_data *p2p, struct wpabuf *ie)
5163{
5164 wpabuf_free(p2p->wfd_ie_invitation);
5165 p2p->wfd_ie_invitation = ie;
5166 return 0;
5167}
5168
5169
5170int p2p_set_wfd_ie_prov_disc_req(struct p2p_data *p2p, struct wpabuf *ie)
5171{
5172 wpabuf_free(p2p->wfd_ie_prov_disc_req);
5173 p2p->wfd_ie_prov_disc_req = ie;
5174 return 0;
5175}
5176
5177
5178int p2p_set_wfd_ie_prov_disc_resp(struct p2p_data *p2p, struct wpabuf *ie)
5179{
5180 wpabuf_free(p2p->wfd_ie_prov_disc_resp);
5181 p2p->wfd_ie_prov_disc_resp = ie;
5182 return 0;
5183}
5184
5185
5186int p2p_set_wfd_ie_go_neg(struct p2p_data *p2p, struct wpabuf *ie)
5187{
5188 wpabuf_free(p2p->wfd_ie_go_neg);
5189 p2p->wfd_ie_go_neg = ie;
5190 return 0;
5191}
5192
5193
5194int p2p_set_wfd_dev_info(struct p2p_data *p2p, const struct wpabuf *elem)
5195{
5196 wpabuf_free(p2p->wfd_dev_info);
5197 if (elem) {
5198 p2p->wfd_dev_info = wpabuf_dup(elem);
5199 if (p2p->wfd_dev_info == NULL)
5200 return -1;
5201 } else
5202 p2p->wfd_dev_info = NULL;
5203
5204 return 0;
5205}
5206
5207
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005208int p2p_set_wfd_r2_dev_info(struct p2p_data *p2p, const struct wpabuf *elem)
5209{
5210 wpabuf_free(p2p->wfd_r2_dev_info);
5211 if (elem) {
5212 p2p->wfd_r2_dev_info = wpabuf_dup(elem);
5213 if (p2p->wfd_r2_dev_info == NULL)
5214 return -1;
5215 } else
5216 p2p->wfd_r2_dev_info = NULL;
5217
5218 return 0;
5219}
5220
5221
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005222int p2p_set_wfd_assoc_bssid(struct p2p_data *p2p, const struct wpabuf *elem)
5223{
5224 wpabuf_free(p2p->wfd_assoc_bssid);
5225 if (elem) {
5226 p2p->wfd_assoc_bssid = wpabuf_dup(elem);
5227 if (p2p->wfd_assoc_bssid == NULL)
5228 return -1;
5229 } else
5230 p2p->wfd_assoc_bssid = NULL;
5231
5232 return 0;
5233}
5234
5235
5236int p2p_set_wfd_coupled_sink_info(struct p2p_data *p2p,
5237 const struct wpabuf *elem)
5238{
5239 wpabuf_free(p2p->wfd_coupled_sink_info);
5240 if (elem) {
5241 p2p->wfd_coupled_sink_info = wpabuf_dup(elem);
5242 if (p2p->wfd_coupled_sink_info == NULL)
5243 return -1;
5244 } else
5245 p2p->wfd_coupled_sink_info = NULL;
5246
5247 return 0;
5248}
5249
5250#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005251
5252
5253int p2p_set_disc_int(struct p2p_data *p2p, int min_disc_int, int max_disc_int,
5254 int max_disc_tu)
5255{
5256 if (min_disc_int > max_disc_int || min_disc_int < 0 || max_disc_int < 0)
5257 return -1;
5258
5259 p2p->min_disc_int = min_disc_int;
5260 p2p->max_disc_int = max_disc_int;
5261 p2p->max_disc_tu = max_disc_tu;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005262 p2p_dbg(p2p, "Set discoverable interval: min=%d max=%d max_tu=%d",
5263 min_disc_int, max_disc_int, max_disc_tu);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005264
5265 return 0;
5266}
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005267
5268
5269void p2p_dbg(struct p2p_data *p2p, const char *fmt, ...)
5270{
5271 va_list ap;
5272 char buf[500];
5273
5274 if (!p2p->cfg->debug_print)
5275 return;
5276
5277 va_start(ap, fmt);
5278 vsnprintf(buf, sizeof(buf), fmt, ap);
5279 buf[sizeof(buf) - 1] = '\0';
5280 va_end(ap);
5281 p2p->cfg->debug_print(p2p->cfg->cb_ctx, MSG_DEBUG, buf);
5282}
5283
5284
5285void p2p_info(struct p2p_data *p2p, const char *fmt, ...)
5286{
5287 va_list ap;
5288 char buf[500];
5289
5290 if (!p2p->cfg->debug_print)
5291 return;
5292
5293 va_start(ap, fmt);
5294 vsnprintf(buf, sizeof(buf), fmt, ap);
5295 buf[sizeof(buf) - 1] = '\0';
5296 va_end(ap);
5297 p2p->cfg->debug_print(p2p->cfg->cb_ctx, MSG_INFO, buf);
5298}
5299
5300
5301void p2p_err(struct p2p_data *p2p, const char *fmt, ...)
5302{
5303 va_list ap;
5304 char buf[500];
5305
5306 if (!p2p->cfg->debug_print)
5307 return;
5308
5309 va_start(ap, fmt);
5310 vsnprintf(buf, sizeof(buf), fmt, ap);
5311 buf[sizeof(buf) - 1] = '\0';
5312 va_end(ap);
5313 p2p->cfg->debug_print(p2p->cfg->cb_ctx, MSG_ERROR, buf);
5314}
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005315
5316
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07005317void p2p_loop_on_known_peers(struct p2p_data *p2p,
5318 void (*peer_callback)(struct p2p_peer_info *peer,
5319 void *user_data),
5320 void *user_data)
5321{
5322 struct p2p_device *dev, *n;
5323
5324 dl_list_for_each_safe(dev, n, &p2p->devices, struct p2p_device, list) {
5325 peer_callback(&dev->info, user_data);
5326 }
5327}
5328
5329
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005330#ifdef CONFIG_WPS_NFC
5331
5332static struct wpabuf * p2p_build_nfc_handover(struct p2p_data *p2p,
5333 int client_freq,
5334 const u8 *go_dev_addr,
5335 const u8 *ssid, size_t ssid_len)
5336{
5337 struct wpabuf *buf;
5338 u8 op_class, channel;
5339 enum p2p_role_indication role = P2P_DEVICE_NOT_IN_GROUP;
5340
5341 buf = wpabuf_alloc(1000);
5342 if (buf == NULL)
5343 return NULL;
5344
5345 op_class = p2p->cfg->reg_class;
5346 channel = p2p->cfg->channel;
5347
5348 p2p_buf_add_capability(buf, p2p->dev_capab &
5349 ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY, 0);
5350 p2p_buf_add_device_info(buf, p2p, NULL);
5351
5352 if (p2p->num_groups > 0) {
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07005353 int freq = p2p_group_get_freq(p2p->groups[0]);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005354 role = P2P_GO_IN_A_GROUP;
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07005355 if (p2p_freq_to_channel(freq, &op_class, &channel) < 0) {
5356 p2p_dbg(p2p,
5357 "Unknown GO operating frequency %d MHz for NFC handover",
5358 freq);
5359 wpabuf_free(buf);
5360 return NULL;
5361 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005362 } else if (client_freq > 0) {
5363 role = P2P_CLIENT_IN_A_GROUP;
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07005364 if (p2p_freq_to_channel(client_freq, &op_class, &channel) < 0) {
5365 p2p_dbg(p2p,
5366 "Unknown client operating frequency %d MHz for NFC handover",
5367 client_freq);
5368 wpabuf_free(buf);
5369 return NULL;
5370 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005371 }
5372
5373 p2p_buf_add_oob_go_neg_channel(buf, p2p->cfg->country, op_class,
5374 channel, role);
5375
5376 if (p2p->num_groups > 0) {
5377 /* Limit number of clients to avoid very long message */
5378 p2p_buf_add_group_info(p2p->groups[0], buf, 5);
5379 p2p_group_buf_add_id(p2p->groups[0], buf);
5380 } else if (client_freq > 0 &&
5381 go_dev_addr && !is_zero_ether_addr(go_dev_addr) &&
5382 ssid && ssid_len > 0) {
5383 /*
5384 * Add the optional P2P Group ID to indicate in which group this
5385 * device is a P2P Client.
5386 */
5387 p2p_buf_add_group_id(buf, go_dev_addr, ssid, ssid_len);
5388 }
5389
5390 return buf;
5391}
5392
5393
5394struct wpabuf * p2p_build_nfc_handover_req(struct p2p_data *p2p,
5395 int client_freq,
5396 const u8 *go_dev_addr,
5397 const u8 *ssid, size_t ssid_len)
5398{
5399 return p2p_build_nfc_handover(p2p, client_freq, go_dev_addr, ssid,
5400 ssid_len);
5401}
5402
5403
5404struct wpabuf * p2p_build_nfc_handover_sel(struct p2p_data *p2p,
5405 int client_freq,
5406 const u8 *go_dev_addr,
5407 const u8 *ssid, size_t ssid_len)
5408{
5409 return p2p_build_nfc_handover(p2p, client_freq, go_dev_addr, ssid,
5410 ssid_len);
5411}
5412
5413
5414int p2p_process_nfc_connection_handover(struct p2p_data *p2p,
5415 struct p2p_nfc_params *params)
5416{
5417 struct p2p_message msg;
5418 struct p2p_device *dev;
5419 const u8 *p2p_dev_addr;
5420 int freq;
5421 enum p2p_role_indication role;
5422
5423 params->next_step = NO_ACTION;
5424
5425 if (p2p_parse_ies_separate(params->wsc_attr, params->wsc_len,
5426 params->p2p_attr, params->p2p_len, &msg)) {
5427 p2p_dbg(p2p, "Failed to parse WSC/P2P attributes from NFC");
5428 p2p_parse_free(&msg);
5429 return -1;
5430 }
5431
5432 if (msg.p2p_device_addr)
5433 p2p_dev_addr = msg.p2p_device_addr;
5434 else if (msg.device_id)
5435 p2p_dev_addr = msg.device_id;
5436 else {
5437 p2p_dbg(p2p, "Ignore scan data without P2P Device Info or P2P Device Id");
5438 p2p_parse_free(&msg);
5439 return -1;
5440 }
5441
5442 if (msg.oob_dev_password) {
5443 os_memcpy(params->oob_dev_pw, msg.oob_dev_password,
5444 msg.oob_dev_password_len);
5445 params->oob_dev_pw_len = msg.oob_dev_password_len;
5446 }
5447
5448 dev = p2p_create_device(p2p, p2p_dev_addr);
5449 if (dev == NULL) {
5450 p2p_parse_free(&msg);
5451 return -1;
5452 }
5453
5454 params->peer = &dev->info;
5455
5456 os_get_reltime(&dev->last_seen);
5457 dev->flags &= ~(P2P_DEV_PROBE_REQ_ONLY | P2P_DEV_GROUP_CLIENT_ONLY);
5458 p2p_copy_wps_info(p2p, dev, 0, &msg);
5459
5460 if (!msg.oob_go_neg_channel) {
5461 p2p_dbg(p2p, "OOB GO Negotiation Channel attribute not included");
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07005462 p2p_parse_free(&msg);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005463 return -1;
5464 }
5465
5466 if (msg.oob_go_neg_channel[3] == 0 &&
5467 msg.oob_go_neg_channel[4] == 0)
5468 freq = 0;
5469 else
5470 freq = p2p_channel_to_freq(msg.oob_go_neg_channel[3],
5471 msg.oob_go_neg_channel[4]);
5472 if (freq < 0) {
5473 p2p_dbg(p2p, "Unknown peer OOB GO Neg channel");
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07005474 p2p_parse_free(&msg);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005475 return -1;
5476 }
5477 role = msg.oob_go_neg_channel[5];
5478
5479 if (role == P2P_GO_IN_A_GROUP) {
5480 p2p_dbg(p2p, "Peer OOB GO operating channel: %u MHz", freq);
5481 params->go_freq = freq;
5482 } else if (role == P2P_CLIENT_IN_A_GROUP) {
5483 p2p_dbg(p2p, "Peer (client) OOB GO operating channel: %u MHz",
5484 freq);
5485 params->go_freq = freq;
5486 } else
5487 p2p_dbg(p2p, "Peer OOB GO Neg channel: %u MHz", freq);
5488 dev->oob_go_neg_freq = freq;
5489
5490 if (!params->sel && role != P2P_GO_IN_A_GROUP) {
5491 freq = p2p_channel_to_freq(p2p->cfg->reg_class,
5492 p2p->cfg->channel);
5493 if (freq < 0) {
5494 p2p_dbg(p2p, "Own listen channel not known");
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07005495 p2p_parse_free(&msg);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005496 return -1;
5497 }
5498 p2p_dbg(p2p, "Use own Listen channel as OOB GO Neg channel: %u MHz", freq);
5499 dev->oob_go_neg_freq = freq;
5500 }
5501
5502 if (msg.group_id) {
5503 os_memcpy(params->go_dev_addr, msg.group_id, ETH_ALEN);
5504 params->go_ssid_len = msg.group_id_len - ETH_ALEN;
5505 os_memcpy(params->go_ssid, msg.group_id + ETH_ALEN,
5506 params->go_ssid_len);
5507 }
5508
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005509 if (dev->flags & P2P_DEV_USER_REJECTED) {
5510 p2p_dbg(p2p, "Do not report rejected device");
Dmitry Shmidt71757432014-06-02 13:50:35 -07005511 p2p_parse_free(&msg);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005512 return 0;
5513 }
5514
5515 if (!(dev->flags & P2P_DEV_REPORTED)) {
5516 p2p->cfg->dev_found(p2p->cfg->cb_ctx, p2p_dev_addr, &dev->info,
5517 !(dev->flags & P2P_DEV_REPORTED_ONCE));
5518 dev->flags |= P2P_DEV_REPORTED | P2P_DEV_REPORTED_ONCE;
5519 }
Dmitry Shmidt71757432014-06-02 13:50:35 -07005520 p2p_parse_free(&msg);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005521
5522 if (role == P2P_GO_IN_A_GROUP && p2p->num_groups > 0)
5523 params->next_step = BOTH_GO;
5524 else if (role == P2P_GO_IN_A_GROUP)
5525 params->next_step = JOIN_GROUP;
5526 else if (role == P2P_CLIENT_IN_A_GROUP) {
5527 dev->flags |= P2P_DEV_GROUP_CLIENT_ONLY;
5528 params->next_step = PEER_CLIENT;
5529 } else if (p2p->num_groups > 0)
5530 params->next_step = AUTH_JOIN;
5531 else if (params->sel)
5532 params->next_step = INIT_GO_NEG;
5533 else
5534 params->next_step = RESP_GO_NEG;
5535
5536 return 0;
5537}
5538
5539
5540void p2p_set_authorized_oob_dev_pw_id(struct p2p_data *p2p, u16 dev_pw_id,
5541 int go_intent,
5542 const u8 *own_interface_addr)
5543{
5544
5545 p2p->authorized_oob_dev_pw_id = dev_pw_id;
5546 if (dev_pw_id == 0) {
5547 p2p_dbg(p2p, "NFC OOB Password unauthorized for static handover");
5548 return;
5549 }
5550
5551 p2p_dbg(p2p, "NFC OOB Password (id=%u) authorized for static handover",
5552 dev_pw_id);
5553
5554 p2p->go_intent = go_intent;
5555 os_memcpy(p2p->intended_addr, own_interface_addr, ETH_ALEN);
5556}
5557
5558#endif /* CONFIG_WPS_NFC */
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07005559
5560
5561int p2p_set_passphrase_len(struct p2p_data *p2p, unsigned int len)
5562{
5563 if (len < 8 || len > 63)
5564 return -1;
5565 p2p->cfg->passphrase_len = len;
5566 return 0;
5567}
Dmitry Shmidt2e67f062014-07-16 09:55:28 -07005568
5569
5570void p2p_set_vendor_elems(struct p2p_data *p2p, struct wpabuf **vendor_elem)
5571{
5572 p2p->vendor_elem = vendor_elem;
5573}
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005574
5575
5576void p2p_go_neg_wait_timeout(void *eloop_ctx, void *timeout_ctx)
5577{
5578 struct p2p_data *p2p = eloop_ctx;
5579
5580 p2p_dbg(p2p,
5581 "Timeout on waiting peer to become ready for GO Negotiation");
5582 p2p_go_neg_failed(p2p, -1);
5583}
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005584
5585
5586void p2p_set_own_pref_freq_list(struct p2p_data *p2p,
Sunil8cd6f4d2022-06-28 18:40:46 +00005587 const struct weighted_pcl *pref_freq_list,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005588 unsigned int size)
5589{
5590 unsigned int i;
5591
5592 if (size > P2P_MAX_PREF_CHANNELS)
5593 size = P2P_MAX_PREF_CHANNELS;
5594 p2p->num_pref_freq = size;
Sunil8cd6f4d2022-06-28 18:40:46 +00005595 os_memcpy(p2p->pref_freq_list, pref_freq_list,
5596 size * sizeof(struct weighted_pcl));
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005597 for (i = 0; i < size; i++) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005598 p2p_dbg(p2p, "Own preferred frequency list[%u]=%u MHz",
Sunil8cd6f4d2022-06-28 18:40:46 +00005599 i, p2p->pref_freq_list[i].freq);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005600 }
5601}
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07005602
5603
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08005604void p2p_set_override_pref_op_chan(struct p2p_data *p2p, u8 op_class,
5605 u8 chan)
5606{
5607 p2p->override_pref_op_class = op_class;
5608 p2p->override_pref_channel = chan;
5609}
5610
5611
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07005612struct wpabuf * p2p_build_probe_resp_template(struct p2p_data *p2p,
5613 unsigned int freq)
5614{
5615 struct wpabuf *ies, *buf;
5616 u8 addr[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
5617 int ret;
5618
5619 ies = p2p_build_probe_resp_ies(p2p, NULL, 0);
5620 if (!ies) {
5621 wpa_printf(MSG_ERROR,
5622 "CTRL: Failed to build Probe Response IEs");
5623 return NULL;
5624 }
5625
5626 buf = wpabuf_alloc(200 + wpabuf_len(ies));
5627 if (!buf) {
5628 wpabuf_free(ies);
5629 return NULL;
5630 }
5631
5632 ret = p2p_build_probe_resp_buf(p2p, buf, ies, addr, freq);
5633 wpabuf_free(ies);
5634 if (ret) {
5635 wpabuf_free(buf);
5636 return NULL;
5637 }
5638
5639 return buf;
5640}
Hai Shaloma20dcd72022-02-04 13:43:00 -08005641
5642
5643bool p2p_is_peer_6ghz_capab(struct p2p_data *p2p, const u8 *addr)
5644{
5645 struct p2p_device *dev;
5646
5647 dev = p2p_get_device(p2p, addr);
5648 if (!dev)
5649 return false;
5650
Sunil Ravi77d572f2023-01-17 23:58:31 +00005651 return dev->support_6ghz;
Hai Shaloma20dcd72022-02-04 13:43:00 -08005652}
5653
5654
5655void p2p_set_6ghz_dev_capab(struct p2p_data *p2p, bool allow_6ghz)
5656{
5657 p2p->p2p_6ghz_capable = allow_6ghz;
5658 p2p->allow_6ghz = allow_6ghz;
5659 p2p_dbg(p2p, "Set 6 GHz capability to %d", allow_6ghz);
5660
5661 if (allow_6ghz)
5662 p2p->dev_capab |= P2P_DEV_CAPAB_6GHZ_BAND_CAPABLE;
5663 else
5664 p2p->dev_capab &= ~P2P_DEV_CAPAB_6GHZ_BAND_CAPABLE;
5665}
5666
5667
5668bool is_p2p_6ghz_capable(struct p2p_data *p2p)
5669{
5670 return p2p->p2p_6ghz_capable;
5671}
5672
5673
5674bool p2p_wfd_enabled(struct p2p_data *p2p)
5675{
5676#ifdef CONFIG_WIFI_DISPLAY
5677 return p2p->wfd_ie_probe_req != NULL;
5678#else /* CONFIG_WIFI_DISPLAY */
5679 return false;
5680#endif /* CONFIG_WIFI_DISPLAY */
5681}
5682
5683
5684bool p2p_peer_wfd_enabled(struct p2p_data *p2p, const u8 *peer_addr)
5685{
5686#ifdef CONFIG_WIFI_DISPLAY
5687 struct p2p_device *dev;
5688
5689 dev = p2p_get_device(p2p, peer_addr);
5690 return dev && dev->info.wfd_subelems != NULL;
5691#else /* CONFIG_WIFI_DISPLAY */
5692 return false;
5693#endif /* CONFIG_WIFI_DISPLAY */
5694}
5695
5696
5697bool is_p2p_allow_6ghz(struct p2p_data *p2p)
5698{
5699 return p2p->allow_6ghz;
5700}
5701
5702
5703void set_p2p_allow_6ghz(struct p2p_data *p2p, bool value)
5704{
5705 p2p->allow_6ghz = value;
5706}