blob: f3742ea5f0a99b8ae66acb3eb089fac67d5a892b [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"
Sunil Ravic0f5d412024-09-11 22:12:49 +000017#include "common/wpa_common.h"
Dmitry Shmidt2e67f062014-07-16 09:55:28 -070018#include "common/wpa_ctrl.h"
Sunil Ravic0f5d412024-09-11 22:12:49 +000019#include "common/sae.h"
Dmitry Shmidt216983b2015-02-06 10:50:36 -080020#include "crypto/sha256.h"
Sunil Ravic0f5d412024-09-11 22:12:49 +000021#include "crypto/sha384.h"
Dmitry Shmidt216983b2015-02-06 10:50:36 -080022#include "crypto/crypto.h"
Sunil Ravic0f5d412024-09-11 22:12:49 +000023#include "pasn/pasn_common.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070024#include "wps/wps_i.h"
25#include "p2p_i.h"
26#include "p2p.h"
27
28
29static void p2p_state_timeout(void *eloop_ctx, void *timeout_ctx);
30static void p2p_device_free(struct p2p_data *p2p, struct p2p_device *dev);
31static void p2p_process_presence_req(struct p2p_data *p2p, const u8 *da,
32 const u8 *sa, const u8 *data, size_t len,
33 int rx_freq);
34static void p2p_process_presence_resp(struct p2p_data *p2p, const u8 *da,
35 const u8 *sa, const u8 *data,
36 size_t len);
37static void p2p_ext_listen_timeout(void *eloop_ctx, void *timeout_ctx);
38static void p2p_scan_timeout(void *eloop_ctx, void *timeout_ctx);
39
40
41/*
42 * p2p_scan recovery timeout
43 *
44 * Many drivers are using 30 second timeout on scan results. Allow a bit larger
45 * timeout for this to avoid hitting P2P timeout unnecessarily.
46 */
47#define P2P_SCAN_TIMEOUT 35
48
49/**
50 * P2P_PEER_EXPIRATION_AGE - Number of seconds after which inactive peer
51 * entries will be removed
52 */
Dmitry Shmidt2093d062014-01-17 10:58:50 -080053#ifndef P2P_PEER_EXPIRATION_AGE
54#define P2P_PEER_EXPIRATION_AGE 60
Dmitry Shmidt18463232014-01-24 12:29:41 -080055#endif /* P2P_PEER_EXPIRATION_AGE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070056
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070057
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080058void p2p_expire_peers(struct p2p_data *p2p)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070059{
60 struct p2p_device *dev, *n;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -080061 struct os_reltime now;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -080062 size_t i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070063
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -080064 os_get_reltime(&now);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070065 dl_list_for_each_safe(dev, n, &p2p->devices, struct p2p_device, list) {
66 if (dev->last_seen.sec + P2P_PEER_EXPIRATION_AGE >= now.sec)
67 continue;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -080068
Dmitry Shmidt9e3f8ee2014-01-17 10:52:01 -080069 if (dev == p2p->go_neg_peer) {
70 /*
71 * GO Negotiation is in progress with the peer, so
72 * don't expire the peer entry until GO Negotiation
73 * fails or times out.
74 */
75 continue;
76 }
77
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -080078 if (p2p->cfg->go_connected &&
79 p2p->cfg->go_connected(p2p->cfg->cb_ctx,
80 dev->info.p2p_device_addr)) {
81 /*
82 * We are connected as a client to a group in which the
83 * peer is the GO, so do not expire the peer entry.
84 */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -080085 os_get_reltime(&dev->last_seen);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -080086 continue;
87 }
88
89 for (i = 0; i < p2p->num_groups; i++) {
90 if (p2p_group_is_client_connected(
91 p2p->groups[i], dev->info.p2p_device_addr))
92 break;
93 }
94 if (i < p2p->num_groups) {
95 /*
96 * The peer is connected as a client in a group where
97 * we are the GO, so do not expire the peer entry.
98 */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -080099 os_get_reltime(&dev->last_seen);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800100 continue;
101 }
102
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700103 p2p_dbg(p2p, "Expiring old peer entry " MACSTR,
104 MAC2STR(dev->info.p2p_device_addr));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700105 dl_list_del(&dev->list);
106 p2p_device_free(p2p, dev);
107 }
108}
109
110
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700111static const char * p2p_state_txt(int state)
112{
113 switch (state) {
114 case P2P_IDLE:
115 return "IDLE";
116 case P2P_SEARCH:
117 return "SEARCH";
118 case P2P_CONNECT:
119 return "CONNECT";
120 case P2P_CONNECT_LISTEN:
121 return "CONNECT_LISTEN";
122 case P2P_GO_NEG:
123 return "GO_NEG";
124 case P2P_LISTEN_ONLY:
125 return "LISTEN_ONLY";
126 case P2P_WAIT_PEER_CONNECT:
127 return "WAIT_PEER_CONNECT";
128 case P2P_WAIT_PEER_IDLE:
129 return "WAIT_PEER_IDLE";
130 case P2P_SD_DURING_FIND:
131 return "SD_DURING_FIND";
132 case P2P_PROVISIONING:
133 return "PROVISIONING";
134 case P2P_PD_DURING_FIND:
135 return "PD_DURING_FIND";
136 case P2P_INVITE:
137 return "INVITE";
138 case P2P_INVITE_LISTEN:
139 return "INVITE_LISTEN";
140 default:
141 return "?";
142 }
143}
144
145
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700146const char * p2p_get_state_txt(struct p2p_data *p2p)
147{
148 return p2p_state_txt(p2p->state);
149}
150
151
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800152struct p2ps_advertisement * p2p_get_p2ps_adv_list(struct p2p_data *p2p)
153{
154 return p2p ? p2p->p2ps_adv_list : NULL;
155}
156
157
158void p2p_set_intended_addr(struct p2p_data *p2p, const u8 *intended_addr)
159{
160 if (p2p && intended_addr)
161 os_memcpy(p2p->intended_addr, intended_addr, ETH_ALEN);
162}
163
164
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800165u16 p2p_get_provisioning_info(struct p2p_data *p2p, const u8 *addr)
166{
167 struct p2p_device *dev = NULL;
168
169 if (!addr || !p2p)
170 return 0;
171
172 dev = p2p_get_device(p2p, addr);
173 if (dev)
174 return dev->wps_prov_info;
175 else
176 return 0;
177}
178
179
Dmitry Shmidt04949592012-07-19 12:16:46 -0700180void p2p_clear_provisioning_info(struct p2p_data *p2p, const u8 *addr)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800181{
182 struct p2p_device *dev = NULL;
183
Dmitry Shmidt04949592012-07-19 12:16:46 -0700184 if (!addr || !p2p)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800185 return;
186
Dmitry Shmidt04949592012-07-19 12:16:46 -0700187 dev = p2p_get_device(p2p, addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800188 if (dev)
189 dev->wps_prov_info = 0;
190}
191
192
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700193void p2p_set_state(struct p2p_data *p2p, int new_state)
194{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700195 p2p_dbg(p2p, "State %s -> %s",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700196 p2p_state_txt(p2p->state), p2p_state_txt(new_state));
197 p2p->state = new_state;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700198
199 if (new_state == P2P_IDLE && p2p->pending_channel) {
200 p2p_dbg(p2p, "Apply change in listen channel");
201 p2p->cfg->reg_class = p2p->pending_reg_class;
202 p2p->cfg->channel = p2p->pending_channel;
203 p2p->pending_reg_class = 0;
204 p2p->pending_channel = 0;
205 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700206}
207
208
209void p2p_set_timeout(struct p2p_data *p2p, unsigned int sec, unsigned int usec)
210{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700211 p2p_dbg(p2p, "Set timeout (state=%s): %u.%06u sec",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700212 p2p_state_txt(p2p->state), sec, usec);
213 eloop_cancel_timeout(p2p_state_timeout, p2p, NULL);
214 eloop_register_timeout(sec, usec, p2p_state_timeout, p2p, NULL);
215}
216
217
218void p2p_clear_timeout(struct p2p_data *p2p)
219{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700220 p2p_dbg(p2p, "Clear timeout (state=%s)", p2p_state_txt(p2p->state));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700221 eloop_cancel_timeout(p2p_state_timeout, p2p, NULL);
222}
223
224
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800225void p2p_go_neg_failed(struct p2p_data *p2p, int status)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700226{
227 struct p2p_go_neg_results res;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800228 struct p2p_device *peer = p2p->go_neg_peer;
229
230 if (!peer)
231 return;
232
233 eloop_cancel_timeout(p2p_go_neg_wait_timeout, p2p, NULL);
234 if (p2p->state != P2P_SEARCH) {
235 /*
236 * Clear timeouts related to GO Negotiation if no new p2p_find
237 * has been started.
238 */
239 p2p_clear_timeout(p2p);
240 p2p_set_state(p2p, P2P_IDLE);
Dmitry Shmidt8c652892013-03-01 10:14:01 -0800241 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800242
243 peer->flags &= ~P2P_DEV_PEER_WAITING_RESPONSE;
244 peer->wps_method = WPS_NOT_READY;
245 peer->oob_pw_id = 0;
246 wpabuf_free(peer->go_neg_conf);
247 peer->go_neg_conf = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700248 p2p->go_neg_peer = NULL;
249
250 os_memset(&res, 0, sizeof(res));
251 res.status = status;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800252 os_memcpy(res.peer_device_addr, peer->info.p2p_device_addr, ETH_ALEN);
253 os_memcpy(res.peer_interface_addr, peer->intended_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700254 p2p->cfg->go_neg_completed(p2p->cfg->cb_ctx, &res);
255}
256
257
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800258static void p2p_listen_in_find(struct p2p_data *p2p, int dev_disc)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700259{
260 unsigned int r, tu;
261 int freq;
262 struct wpabuf *ies;
263
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700264 p2p_dbg(p2p, "Starting short listen state (state=%s)",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700265 p2p_state_txt(p2p->state));
266
Dmitry Shmidt3c57b3f2014-05-22 15:13:07 -0700267 if (p2p->pending_listen_freq) {
268 /* We have a pending p2p_listen request */
269 p2p_dbg(p2p, "p2p_listen command pending already");
270 return;
271 }
272
Dmitry Shmidt4b060592013-04-29 16:42:49 -0700273 freq = p2p_channel_to_freq(p2p->cfg->reg_class, p2p->cfg->channel);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700274 if (freq < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700275 p2p_dbg(p2p, "Unknown regulatory class/channel");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700276 return;
277 }
278
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700279 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
280 r = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700281 tu = (r % ((p2p->max_disc_int - p2p->min_disc_int) + 1) +
282 p2p->min_disc_int) * 100;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800283 if (p2p->max_disc_tu >= 0 && tu > (unsigned int) p2p->max_disc_tu)
284 tu = p2p->max_disc_tu;
285 if (!dev_disc && tu < 100)
286 tu = 100; /* Need to wait in non-device discovery use cases */
287 if (p2p->cfg->max_listen && 1024 * tu / 1000 > p2p->cfg->max_listen)
288 tu = p2p->cfg->max_listen * 1000 / 1024;
289
290 if (tu == 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700291 p2p_dbg(p2p, "Skip listen state since duration was 0 TU");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800292 p2p_set_timeout(p2p, 0, 0);
293 return;
294 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700295
Dmitry Shmidta3dc3092015-06-23 11:21:28 -0700296 ies = p2p_build_probe_resp_ies(p2p, NULL, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700297 if (ies == NULL)
298 return;
299
Dmitry Shmidt3c57b3f2014-05-22 15:13:07 -0700300 p2p->pending_listen_freq = freq;
301 p2p->pending_listen_sec = 0;
302 p2p->pending_listen_usec = 1024 * tu;
303
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700304 if (p2p->cfg->start_listen(p2p->cfg->cb_ctx, freq, 1024 * tu / 1000,
305 ies) < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700306 p2p_dbg(p2p, "Failed to start listen mode");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700307 p2p->pending_listen_freq = 0;
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000308 } else {
309 p2p->pending_listen_wait_drv = true;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700310 }
311 wpabuf_free(ies);
312}
313
314
315int p2p_listen(struct p2p_data *p2p, unsigned int timeout)
316{
317 int freq;
318 struct wpabuf *ies;
319
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700320 p2p_dbg(p2p, "Going to listen(only) state");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700321
Dmitry Shmidt3c57b3f2014-05-22 15:13:07 -0700322 if (p2p->pending_listen_freq) {
323 /* We have a pending p2p_listen request */
324 p2p_dbg(p2p, "p2p_listen command pending already");
325 return -1;
326 }
327
Dmitry Shmidt4b060592013-04-29 16:42:49 -0700328 freq = p2p_channel_to_freq(p2p->cfg->reg_class, p2p->cfg->channel);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700329 if (freq < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700330 p2p_dbg(p2p, "Unknown regulatory class/channel");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700331 return -1;
332 }
333
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700334 p2p->pending_listen_sec = timeout / 1000;
335 p2p->pending_listen_usec = (timeout % 1000) * 1000;
336
337 if (p2p->p2p_scan_running) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700338 if (p2p->start_after_scan == P2P_AFTER_SCAN_CONNECT) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700339 p2p_dbg(p2p, "p2p_scan running - connect is already pending - skip listen");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800340 return 0;
341 }
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700342 p2p_dbg(p2p, "p2p_scan running - delay start of listen state");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700343 p2p->start_after_scan = P2P_AFTER_SCAN_LISTEN;
344 return 0;
345 }
346
Dmitry Shmidta3dc3092015-06-23 11:21:28 -0700347 ies = p2p_build_probe_resp_ies(p2p, NULL, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700348 if (ies == NULL)
349 return -1;
350
Dmitry Shmidt3c57b3f2014-05-22 15:13:07 -0700351 p2p->pending_listen_freq = freq;
352
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700353 if (p2p->cfg->start_listen(p2p->cfg->cb_ctx, freq, timeout, ies) < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700354 p2p_dbg(p2p, "Failed to start listen mode");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700355 p2p->pending_listen_freq = 0;
356 wpabuf_free(ies);
357 return -1;
358 }
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000359 p2p->pending_listen_wait_drv = true;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700360 wpabuf_free(ies);
361
362 p2p_set_state(p2p, P2P_LISTEN_ONLY);
363
364 return 0;
365}
366
367
368static void p2p_device_clear_reported(struct p2p_data *p2p)
369{
370 struct p2p_device *dev;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800371 dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700372 dev->flags &= ~P2P_DEV_REPORTED;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800373 dev->sd_reqs = 0;
374 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700375}
376
377
378/**
379 * p2p_get_device - Fetch a peer entry
380 * @p2p: P2P module context from p2p_init()
381 * @addr: P2P Device Address of the peer
382 * Returns: Pointer to the device entry or %NULL if not found
383 */
384struct p2p_device * p2p_get_device(struct p2p_data *p2p, const u8 *addr)
385{
386 struct p2p_device *dev;
387 dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000388 if (ether_addr_equal(dev->info.p2p_device_addr, addr))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700389 return dev;
390 }
391 return NULL;
392}
393
394
395/**
396 * p2p_get_device_interface - Fetch a peer entry based on P2P Interface Address
397 * @p2p: P2P module context from p2p_init()
398 * @addr: P2P Interface Address of the peer
399 * Returns: Pointer to the device entry or %NULL if not found
400 */
401struct p2p_device * p2p_get_device_interface(struct p2p_data *p2p,
402 const u8 *addr)
403{
404 struct p2p_device *dev;
405 dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000406 if (ether_addr_equal(dev->interface_addr, addr))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700407 return dev;
408 }
409 return NULL;
410}
411
412
413/**
414 * p2p_create_device - Create a peer entry
415 * @p2p: P2P module context from p2p_init()
416 * @addr: P2P Device Address of the peer
417 * Returns: Pointer to the device entry or %NULL on failure
418 *
419 * If there is already an entry for the peer, it will be returned instead of
420 * creating a new one.
421 */
422static struct p2p_device * p2p_create_device(struct p2p_data *p2p,
423 const u8 *addr)
424{
425 struct p2p_device *dev, *oldest = NULL;
426 size_t count = 0;
427
428 dev = p2p_get_device(p2p, addr);
429 if (dev)
430 return dev;
431
432 dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
433 count++;
434 if (oldest == NULL ||
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800435 os_reltime_before(&dev->last_seen, &oldest->last_seen))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700436 oldest = dev;
437 }
438 if (count + 1 > p2p->cfg->max_peers && oldest) {
Hai Shaloma20dcd72022-02-04 13:43:00 -0800439 p2p_dbg(p2p,
440 "Remove oldest peer entry to make room for a new peer "
441 MACSTR, MAC2STR(oldest->info.p2p_device_addr));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700442 dl_list_del(&oldest->list);
443 p2p_device_free(p2p, oldest);
444 }
445
446 dev = os_zalloc(sizeof(*dev));
447 if (dev == NULL)
448 return NULL;
449 dl_list_add(&p2p->devices, &dev->list);
450 os_memcpy(dev->info.p2p_device_addr, addr, ETH_ALEN);
Sunil Ravi77d572f2023-01-17 23:58:31 +0000451 dev->support_6ghz = false;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700452
453 return dev;
454}
455
456
457static void p2p_copy_client_info(struct p2p_device *dev,
458 struct p2p_client_info *cli)
459{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800460 p2p_copy_filter_devname(dev->info.device_name,
461 sizeof(dev->info.device_name),
462 cli->dev_name, cli->dev_name_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700463 dev->info.dev_capab = cli->dev_capab;
464 dev->info.config_methods = cli->config_methods;
465 os_memcpy(dev->info.pri_dev_type, cli->pri_dev_type, 8);
466 dev->info.wps_sec_dev_type_list_len = 8 * cli->num_sec_dev_types;
Jimmy Chen1b737ee2020-11-20 01:24:12 +0800467 if (dev->info.wps_sec_dev_type_list_len > WPS_SEC_DEV_TYPE_MAX_LEN) {
468 android_errorWriteLog(0x534e4554, "172937525");
Jimmy Chene12b6972020-11-09 11:43:12 +0200469 dev->info.wps_sec_dev_type_list_len = WPS_SEC_DEV_TYPE_MAX_LEN;
Jimmy Chen1b737ee2020-11-20 01:24:12 +0800470 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700471 os_memcpy(dev->info.wps_sec_dev_type_list, cli->sec_dev_types,
472 dev->info.wps_sec_dev_type_list_len);
473}
474
475
476static int p2p_add_group_clients(struct p2p_data *p2p, const u8 *go_dev_addr,
477 const u8 *go_interface_addr, int freq,
Dmitry Shmidta3dc3092015-06-23 11:21:28 -0700478 const u8 *gi, size_t gi_len,
479 struct os_reltime *rx_time)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700480{
481 struct p2p_group_info info;
482 size_t c;
483 struct p2p_device *dev;
484
485 if (gi == NULL)
486 return 0;
487
488 if (p2p_group_info_parse(gi, gi_len, &info) < 0)
489 return -1;
490
491 /*
492 * Clear old data for this group; if the devices are still in the
493 * group, the information will be restored in the loop following this.
494 */
495 dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000496 if (ether_addr_equal(dev->member_in_go_iface,
497 go_interface_addr)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700498 os_memset(dev->member_in_go_iface, 0, ETH_ALEN);
499 os_memset(dev->member_in_go_dev, 0, ETH_ALEN);
500 }
501 }
502
503 for (c = 0; c < info.num_clients; c++) {
504 struct p2p_client_info *cli = &info.client[c];
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000505 if (ether_addr_equal(cli->p2p_device_addr, p2p->cfg->dev_addr))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800506 continue; /* ignore our own entry */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700507 dev = p2p_get_device(p2p, cli->p2p_device_addr);
508 if (dev) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700509 if (dev->flags & (P2P_DEV_GROUP_CLIENT_ONLY |
Dmitry Shmidt04949592012-07-19 12:16:46 -0700510 P2P_DEV_PROBE_REQ_ONLY)) {
511 /*
512 * Update information since we have not
513 * received this directly from the client.
514 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700515 p2p_copy_client_info(dev, cli);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700516 } else {
517 /*
518 * Need to update P2P Client Discoverability
519 * flag since it is valid only in P2P Group
520 * Info attribute.
521 */
522 dev->info.dev_capab &=
523 ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
524 dev->info.dev_capab |=
525 cli->dev_capab &
526 P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
527 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700528 if (dev->flags & P2P_DEV_PROBE_REQ_ONLY) {
529 dev->flags &= ~P2P_DEV_PROBE_REQ_ONLY;
530 }
531 } else {
532 dev = p2p_create_device(p2p, cli->p2p_device_addr);
533 if (dev == NULL)
534 continue;
535 dev->flags |= P2P_DEV_GROUP_CLIENT_ONLY;
536 p2p_copy_client_info(dev, cli);
537 dev->oper_freq = freq;
538 p2p->cfg->dev_found(p2p->cfg->cb_ctx,
539 dev->info.p2p_device_addr,
540 &dev->info, 1);
541 dev->flags |= P2P_DEV_REPORTED | P2P_DEV_REPORTED_ONCE;
542 }
543
544 os_memcpy(dev->interface_addr, cli->p2p_interface_addr,
545 ETH_ALEN);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -0700546 os_memcpy(&dev->last_seen, rx_time, sizeof(struct os_reltime));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700547 os_memcpy(dev->member_in_go_dev, go_dev_addr, ETH_ALEN);
548 os_memcpy(dev->member_in_go_iface, go_interface_addr,
549 ETH_ALEN);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -0700550 dev->flags |= P2P_DEV_LAST_SEEN_AS_GROUP_CLIENT;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700551 }
552
553 return 0;
554}
555
556
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700557static void p2p_copy_wps_info(struct p2p_data *p2p, struct p2p_device *dev,
558 int probe_req, const struct p2p_message *msg)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700559{
560 os_memcpy(dev->info.device_name, msg->device_name,
561 sizeof(dev->info.device_name));
562
563 if (msg->manufacturer &&
564 msg->manufacturer_len < sizeof(dev->info.manufacturer)) {
565 os_memset(dev->info.manufacturer, 0,
566 sizeof(dev->info.manufacturer));
567 os_memcpy(dev->info.manufacturer, msg->manufacturer,
568 msg->manufacturer_len);
569 }
570
571 if (msg->model_name &&
572 msg->model_name_len < sizeof(dev->info.model_name)) {
573 os_memset(dev->info.model_name, 0,
574 sizeof(dev->info.model_name));
575 os_memcpy(dev->info.model_name, msg->model_name,
576 msg->model_name_len);
577 }
578
579 if (msg->model_number &&
580 msg->model_number_len < sizeof(dev->info.model_number)) {
581 os_memset(dev->info.model_number, 0,
582 sizeof(dev->info.model_number));
583 os_memcpy(dev->info.model_number, msg->model_number,
584 msg->model_number_len);
585 }
586
587 if (msg->serial_number &&
588 msg->serial_number_len < sizeof(dev->info.serial_number)) {
589 os_memset(dev->info.serial_number, 0,
590 sizeof(dev->info.serial_number));
591 os_memcpy(dev->info.serial_number, msg->serial_number,
592 msg->serial_number_len);
593 }
594
595 if (msg->pri_dev_type)
596 os_memcpy(dev->info.pri_dev_type, msg->pri_dev_type,
597 sizeof(dev->info.pri_dev_type));
598 else if (msg->wps_pri_dev_type)
599 os_memcpy(dev->info.pri_dev_type, msg->wps_pri_dev_type,
600 sizeof(dev->info.pri_dev_type));
601
602 if (msg->wps_sec_dev_type_list) {
603 os_memcpy(dev->info.wps_sec_dev_type_list,
604 msg->wps_sec_dev_type_list,
605 msg->wps_sec_dev_type_list_len);
606 dev->info.wps_sec_dev_type_list_len =
607 msg->wps_sec_dev_type_list_len;
608 }
609
610 if (msg->capability) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700611 /*
612 * P2P Client Discoverability bit is reserved in all frames
613 * that use this function, so do not change its value here.
614 */
615 dev->info.dev_capab &= P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
616 dev->info.dev_capab |= msg->capability[0] &
617 ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700618 dev->info.group_capab = msg->capability[1];
619 }
620
Sunil Ravi77d572f2023-01-17 23:58:31 +0000621 p2p_update_peer_6ghz_capab(dev, msg);
622
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700623 if (msg->ext_listen_timing) {
624 dev->ext_listen_period = WPA_GET_LE16(msg->ext_listen_timing);
625 dev->ext_listen_interval =
626 WPA_GET_LE16(msg->ext_listen_timing + 2);
627 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700628
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700629 if (!probe_req) {
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800630 u16 new_config_methods;
631 new_config_methods = msg->config_methods ?
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700632 msg->config_methods : msg->wps_config_methods;
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800633 if (new_config_methods &&
634 dev->info.config_methods != new_config_methods) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700635 p2p_dbg(p2p, "Update peer " MACSTR
636 " config_methods 0x%x -> 0x%x",
637 MAC2STR(dev->info.p2p_device_addr),
638 dev->info.config_methods,
639 new_config_methods);
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800640 dev->info.config_methods = new_config_methods;
641 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700642 }
643}
644
645
Sunil Ravi77d572f2023-01-17 23:58:31 +0000646void p2p_update_peer_6ghz_capab(struct p2p_device *dev,
647 const struct p2p_message *msg)
648{
649 if (msg->capability &&
650 (msg->capability[0] & P2P_DEV_CAPAB_6GHZ_BAND_CAPABLE))
651 dev->support_6ghz = true;
652}
653
654
Dmitry Shmidt2e67f062014-07-16 09:55:28 -0700655static void p2p_update_peer_vendor_elems(struct p2p_device *dev, const u8 *ies,
656 size_t ies_len)
657{
658 const u8 *pos, *end;
659 u8 id, len;
660
661 wpabuf_free(dev->info.vendor_elems);
662 dev->info.vendor_elems = NULL;
663
664 end = ies + ies_len;
665
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800666 for (pos = ies; end - pos > 1; pos += len) {
Dmitry Shmidt2e67f062014-07-16 09:55:28 -0700667 id = *pos++;
668 len = *pos++;
669
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800670 if (len > end - pos)
Dmitry Shmidt2e67f062014-07-16 09:55:28 -0700671 break;
672
673 if (id != WLAN_EID_VENDOR_SPECIFIC || len < 3)
674 continue;
675
676 if (len >= 4) {
677 u32 type = WPA_GET_BE32(pos);
678
679 if (type == WPA_IE_VENDOR_TYPE ||
680 type == WMM_IE_VENDOR_TYPE ||
681 type == WPS_IE_VENDOR_TYPE ||
682 type == P2P_IE_VENDOR_TYPE ||
683 type == WFD_IE_VENDOR_TYPE)
684 continue;
685 }
686
687 /* Unknown vendor element - make raw IE data available */
688 if (wpabuf_resize(&dev->info.vendor_elems, 2 + len) < 0)
689 break;
690 wpabuf_put_data(dev->info.vendor_elems, pos - 2, 2 + len);
Hai Shalom60840252021-02-19 19:02:11 -0800691 if (wpabuf_size(dev->info.vendor_elems) > 2000)
692 break;
Dmitry Shmidt2e67f062014-07-16 09:55:28 -0700693 }
694}
695
696
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800697static int p2p_compare_wfd_info(struct p2p_device *dev,
698 const struct p2p_message *msg)
699{
700 if (dev->info.wfd_subelems && msg->wfd_subelems) {
701 if (dev->info.wfd_subelems->used != msg->wfd_subelems->used)
702 return 1;
703
704 return os_memcmp(dev->info.wfd_subelems->buf,
705 msg->wfd_subelems->buf,
706 dev->info.wfd_subelems->used);
707 }
708 if (dev->info.wfd_subelems || msg->wfd_subelems)
709 return 1;
710
711 return 0;
712}
713
714
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700715/**
Dmitry Shmidt04949592012-07-19 12:16:46 -0700716 * p2p_add_device - Add peer entries based on scan results or P2P frames
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700717 * @p2p: P2P module context from p2p_init()
718 * @addr: Source address of Beacon or Probe Response frame (may be either
719 * P2P Device Address or P2P Interface Address)
720 * @level: Signal level (signal strength of the received frame from the peer)
721 * @freq: Frequency on which the Beacon or Probe Response frame was received
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800722 * @rx_time: Time when the result was received
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700723 * @ies: IEs from the Beacon or Probe Response frame
724 * @ies_len: Length of ies buffer in octets
Dmitry Shmidt04949592012-07-19 12:16:46 -0700725 * @scan_res: Whether this was based on scan results
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700726 * Returns: 0 on success, -1 on failure
727 *
728 * If the scan result is for a GO, the clients in the group will also be added
729 * to the peer table. This function can also be used with some other frames
730 * like Provision Discovery Request that contains P2P Capability and P2P Device
731 * Info attributes.
732 */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800733int p2p_add_device(struct p2p_data *p2p, const u8 *addr, int freq,
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800734 struct os_reltime *rx_time, int level, const u8 *ies,
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800735 size_t ies_len, int scan_res)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700736{
737 struct p2p_device *dev;
738 struct p2p_message msg;
739 const u8 *p2p_dev_addr;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800740 int wfd_changed;
Dmitry Shmidt29333592017-01-09 12:27:11 -0800741 int dev_name_changed;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700742 int i;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800743 struct os_reltime time_now;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700744
745 os_memset(&msg, 0, sizeof(msg));
746 if (p2p_parse_ies(ies, ies_len, &msg)) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700747 p2p_dbg(p2p, "Failed to parse P2P IE for a device entry");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700748 p2p_parse_free(&msg);
749 return -1;
750 }
751
752 if (msg.p2p_device_addr)
753 p2p_dev_addr = msg.p2p_device_addr;
754 else if (msg.device_id)
755 p2p_dev_addr = msg.device_id;
756 else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700757 p2p_dbg(p2p, "Ignore scan data without P2P Device Info or P2P Device Id");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700758 p2p_parse_free(&msg);
759 return -1;
760 }
761
762 if (!is_zero_ether_addr(p2p->peer_filter) &&
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000763 !ether_addr_equal(p2p_dev_addr, p2p->peer_filter)) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700764 p2p_dbg(p2p, "Do not add peer filter for " MACSTR
765 " due to peer filter", MAC2STR(p2p_dev_addr));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800766 p2p_parse_free(&msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700767 return 0;
768 }
769
770 dev = p2p_create_device(p2p, p2p_dev_addr);
771 if (dev == NULL) {
772 p2p_parse_free(&msg);
773 return -1;
774 }
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800775
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800776 if (rx_time == NULL) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800777 os_get_reltime(&time_now);
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800778 rx_time = &time_now;
779 }
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800780
781 /*
782 * Update the device entry only if the new peer
Dmitry Shmidta3dc3092015-06-23 11:21:28 -0700783 * entry is newer than the one previously stored, or if
784 * the device was previously seen as a P2P Client in a group
785 * and the new entry isn't older than a threshold.
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800786 */
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800787 if (dev->last_seen.sec > 0 &&
Dmitry Shmidta3dc3092015-06-23 11:21:28 -0700788 os_reltime_before(rx_time, &dev->last_seen) &&
789 (!(dev->flags & P2P_DEV_LAST_SEEN_AS_GROUP_CLIENT) ||
790 os_reltime_expired(&dev->last_seen, rx_time,
791 P2P_DEV_GROUP_CLIENT_RESP_THRESHOLD))) {
792 p2p_dbg(p2p,
793 "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 -0800794 (unsigned int) rx_time->sec,
795 (unsigned int) rx_time->usec,
796 (unsigned int) dev->last_seen.sec,
Dmitry Shmidta3dc3092015-06-23 11:21:28 -0700797 (unsigned int) dev->last_seen.usec,
798 dev->flags);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800799 p2p_parse_free(&msg);
800 return -1;
801 }
802
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800803 os_memcpy(&dev->last_seen, rx_time, sizeof(struct os_reltime));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800804
Dmitry Shmidta3dc3092015-06-23 11:21:28 -0700805 dev->flags &= ~(P2P_DEV_PROBE_REQ_ONLY | P2P_DEV_GROUP_CLIENT_ONLY |
806 P2P_DEV_LAST_SEEN_AS_GROUP_CLIENT);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700807
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000808 if (!ether_addr_equal(addr, p2p_dev_addr))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700809 os_memcpy(dev->interface_addr, addr, ETH_ALEN);
810 if (msg.ssid &&
Jouni Malinenfdb708a2015-04-07 11:32:11 +0300811 msg.ssid[1] <= sizeof(dev->oper_ssid) &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700812 (msg.ssid[1] != P2P_WILDCARD_SSID_LEN ||
813 os_memcmp(msg.ssid + 2, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN)
814 != 0)) {
815 os_memcpy(dev->oper_ssid, msg.ssid + 2, msg.ssid[1]);
816 dev->oper_ssid_len = msg.ssid[1];
817 }
818
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -0700819 wpabuf_free(dev->info.p2ps_instance);
820 dev->info.p2ps_instance = NULL;
821 if (msg.adv_service_instance && msg.adv_service_instance_len)
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800822 dev->info.p2ps_instance = wpabuf_alloc_copy(
823 msg.adv_service_instance, msg.adv_service_instance_len);
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800824
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700825 if (freq >= 2412 && freq <= 2484 && msg.ds_params &&
826 *msg.ds_params >= 1 && *msg.ds_params <= 14) {
827 int ds_freq;
828 if (*msg.ds_params == 14)
829 ds_freq = 2484;
830 else
831 ds_freq = 2407 + *msg.ds_params * 5;
832 if (freq != ds_freq) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700833 p2p_dbg(p2p, "Update Listen frequency based on DS Parameter Set IE: %d -> %d MHz",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700834 freq, ds_freq);
835 freq = ds_freq;
836 }
837 }
838
Dmitry Shmidt04949592012-07-19 12:16:46 -0700839 if (dev->listen_freq && dev->listen_freq != freq && scan_res) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700840 p2p_dbg(p2p, "Update Listen frequency based on scan results ("
841 MACSTR " %d -> %d MHz (DS param %d)",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700842 MAC2STR(dev->info.p2p_device_addr), dev->listen_freq,
843 freq, msg.ds_params ? *msg.ds_params : -1);
844 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700845 if (scan_res) {
846 dev->listen_freq = freq;
847 if (msg.group_info)
848 dev->oper_freq = freq;
849 }
Jouni Malinen75ecf522011-06-27 15:19:46 -0700850 dev->info.level = level;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700851
Dmitry Shmidt29333592017-01-09 12:27:11 -0800852 dev_name_changed = os_strncmp(dev->info.device_name, msg.device_name,
853 WPS_DEV_NAME_MAX_LEN) != 0;
854
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700855 p2p_copy_wps_info(p2p, dev, 0, &msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700856
857 for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) {
858 wpabuf_free(dev->info.wps_vendor_ext[i]);
859 dev->info.wps_vendor_ext[i] = NULL;
860 }
861
862 for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) {
863 if (msg.wps_vendor_ext[i] == NULL)
864 break;
865 dev->info.wps_vendor_ext[i] = wpabuf_alloc_copy(
866 msg.wps_vendor_ext[i], msg.wps_vendor_ext_len[i]);
867 if (dev->info.wps_vendor_ext[i] == NULL)
868 break;
869 }
870
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800871 wfd_changed = p2p_compare_wfd_info(dev, &msg);
872
Dmitry Shmidt29333592017-01-09 12:27:11 -0800873 if (wfd_changed) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700874 wpabuf_free(dev->info.wfd_subelems);
Dmitry Shmidt29333592017-01-09 12:27:11 -0800875 if (msg.wfd_subelems)
876 dev->info.wfd_subelems = wpabuf_dup(msg.wfd_subelems);
877 else
878 dev->info.wfd_subelems = NULL;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700879 }
880
Dmitry Shmidt04949592012-07-19 12:16:46 -0700881 if (scan_res) {
882 p2p_add_group_clients(p2p, p2p_dev_addr, addr, freq,
Dmitry Shmidta3dc3092015-06-23 11:21:28 -0700883 msg.group_info, msg.group_info_len,
884 rx_time);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700885 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700886
887 p2p_parse_free(&msg);
888
Dmitry Shmidt2e67f062014-07-16 09:55:28 -0700889 p2p_update_peer_vendor_elems(dev, ies, ies_len);
890
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800891 if (dev->flags & P2P_DEV_REPORTED && !wfd_changed &&
Dmitry Shmidt29333592017-01-09 12:27:11 -0800892 !dev_name_changed &&
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800893 (!msg.adv_service_instance ||
894 (dev->flags & P2P_DEV_P2PS_REPORTED)))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700895 return 0;
896
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700897 p2p_dbg(p2p, "Peer found with Listen frequency %d MHz (rx_time=%u.%06u)",
898 freq, (unsigned int) rx_time->sec,
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800899 (unsigned int) rx_time->usec);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700900 if (dev->flags & P2P_DEV_USER_REJECTED) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700901 p2p_dbg(p2p, "Do not report rejected device");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700902 return 0;
903 }
904
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800905 if (dev->info.config_methods == 0 &&
906 (freq == 2412 || freq == 2437 || freq == 2462)) {
907 /*
908 * If we have only seen a Beacon frame from a GO, we do not yet
909 * know what WPS config methods it supports. Since some
910 * applications use config_methods value from P2P-DEVICE-FOUND
911 * events, postpone reporting this peer until we've fully
912 * discovered its capabilities.
913 *
914 * At least for now, do this only if the peer was detected on
915 * one of the social channels since that peer can be easily be
916 * found again and there are no limitations of having to use
917 * passive scan on this channels, so this can be done through
918 * Probe Response frame that includes the config_methods
919 * information.
920 */
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700921 p2p_dbg(p2p, "Do not report peer " MACSTR
922 " with unknown config methods", MAC2STR(addr));
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800923 return 0;
924 }
925
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700926 p2p->cfg->dev_found(p2p->cfg->cb_ctx, addr, &dev->info,
927 !(dev->flags & P2P_DEV_REPORTED_ONCE));
928 dev->flags |= P2P_DEV_REPORTED | P2P_DEV_REPORTED_ONCE;
929
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800930 if (msg.adv_service_instance)
931 dev->flags |= P2P_DEV_P2PS_REPORTED;
932
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700933 return 0;
934}
935
936
937static void p2p_device_free(struct p2p_data *p2p, struct p2p_device *dev)
938{
939 int i;
940
Dmitry Shmidt497c1d52011-07-21 15:19:46 -0700941 if (p2p->go_neg_peer == dev) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800942 /*
943 * If GO Negotiation is in progress, report that it has failed.
944 */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800945 p2p_go_neg_failed(p2p, -1);
Dmitry Shmidt497c1d52011-07-21 15:19:46 -0700946 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700947 if (p2p->invite_peer == dev)
948 p2p->invite_peer = NULL;
949 if (p2p->sd_peer == dev)
950 p2p->sd_peer = NULL;
951 if (p2p->pending_client_disc_go == dev)
952 p2p->pending_client_disc_go = NULL;
953
Dmitry Shmidtc55524a2011-07-07 11:18:38 -0700954 /* dev_lost() device, but only if it was previously dev_found() */
955 if (dev->flags & P2P_DEV_REPORTED_ONCE)
956 p2p->cfg->dev_lost(p2p->cfg->cb_ctx,
957 dev->info.p2p_device_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700958
959 for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) {
960 wpabuf_free(dev->info.wps_vendor_ext[i]);
961 dev->info.wps_vendor_ext[i] = NULL;
962 }
963
Sunil Ravic0f5d412024-09-11 22:12:49 +0000964 os_free(dev->bootstrap_params);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700965 wpabuf_free(dev->info.wfd_subelems);
Dmitry Shmidt2e67f062014-07-16 09:55:28 -0700966 wpabuf_free(dev->info.vendor_elems);
Dmitry Shmidt413dde72014-04-11 10:23:22 -0700967 wpabuf_free(dev->go_neg_conf);
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800968 wpabuf_free(dev->info.p2ps_instance);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700969
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700970 os_free(dev);
971}
972
973
974static int p2p_get_next_prog_freq(struct p2p_data *p2p)
975{
976 struct p2p_channels *c;
977 struct p2p_reg_class *cla;
978 size_t cl, ch;
979 int found = 0;
980 u8 reg_class;
981 u8 channel;
982 int freq;
983
984 c = &p2p->cfg->channels;
985 for (cl = 0; cl < c->reg_classes; cl++) {
986 cla = &c->reg_class[cl];
987 if (cla->reg_class != p2p->last_prog_scan_class)
988 continue;
989 for (ch = 0; ch < cla->channels; ch++) {
990 if (cla->channel[ch] == p2p->last_prog_scan_chan) {
991 found = 1;
992 break;
993 }
994 }
995 if (found)
996 break;
997 }
998
999 if (!found) {
1000 /* Start from beginning */
1001 reg_class = c->reg_class[0].reg_class;
1002 channel = c->reg_class[0].channel[0];
1003 } else {
1004 /* Pick the next channel */
1005 ch++;
1006 if (ch == cla->channels) {
1007 cl++;
1008 if (cl == c->reg_classes)
1009 cl = 0;
1010 ch = 0;
1011 }
1012 reg_class = c->reg_class[cl].reg_class;
1013 channel = c->reg_class[cl].channel[ch];
1014 }
1015
Dmitry Shmidt4b060592013-04-29 16:42:49 -07001016 freq = p2p_channel_to_freq(reg_class, channel);
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001017 p2p_dbg(p2p, "Next progressive search channel: reg_class %u channel %u -> %d MHz",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001018 reg_class, channel, freq);
1019 p2p->last_prog_scan_class = reg_class;
1020 p2p->last_prog_scan_chan = channel;
1021
1022 if (freq == 2412 || freq == 2437 || freq == 2462)
1023 return 0; /* No need to add social channels */
1024 return freq;
1025}
1026
1027
1028static void p2p_search(struct p2p_data *p2p)
1029{
1030 int freq = 0;
1031 enum p2p_scan_type type;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001032 u16 pw_id = DEV_PW_DEFAULT;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001033 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001034
1035 if (p2p->drv_in_listen) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001036 p2p_dbg(p2p, "Driver is still in Listen state - wait for it to end before continuing");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001037 return;
1038 }
1039 p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001040 p2p->pending_listen_wait_drv = false;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001041
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001042 if (p2p->find_pending_full &&
1043 (p2p->find_type == P2P_FIND_PROGRESSIVE ||
1044 p2p->find_type == P2P_FIND_START_WITH_FULL)) {
1045 type = P2P_SCAN_FULL;
1046 p2p_dbg(p2p, "Starting search (pending full scan)");
1047 p2p->find_pending_full = 0;
1048 } else if ((p2p->find_type == P2P_FIND_PROGRESSIVE &&
1049 (freq = p2p_get_next_prog_freq(p2p)) > 0) ||
1050 (p2p->find_type == P2P_FIND_START_WITH_FULL &&
1051 (freq = p2p->find_specified_freq) > 0)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001052 type = P2P_SCAN_SOCIAL_PLUS_ONE;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001053 p2p_dbg(p2p, "Starting search (+ freq %u)", freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001054 } else {
1055 type = P2P_SCAN_SOCIAL;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001056 p2p_dbg(p2p, "Starting search");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001057 }
1058
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001059 res = p2p->cfg->p2p_scan(p2p->cfg->cb_ctx, type, freq,
1060 p2p->num_req_dev_types, p2p->req_dev_types,
Hai Shaloma20dcd72022-02-04 13:43:00 -08001061 p2p->find_dev_id, pw_id, p2p->include_6ghz);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001062 if (res < 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001063 p2p_dbg(p2p, "Scan request schedule failed");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001064 p2p_continue_find(p2p);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001065 }
1066}
1067
1068
1069static void p2p_find_timeout(void *eloop_ctx, void *timeout_ctx)
1070{
1071 struct p2p_data *p2p = eloop_ctx;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001072 p2p_dbg(p2p, "Find timeout -> stop");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001073 p2p_stop_find(p2p);
1074}
1075
1076
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001077void p2p_notify_scan_trigger_status(struct p2p_data *p2p, int status)
1078{
1079 if (status != 0) {
1080 p2p_dbg(p2p, "Scan request failed");
1081 /* Do continue find even for the first p2p_find_scan */
1082 p2p_continue_find(p2p);
1083 } else {
1084 p2p_dbg(p2p, "Running p2p_scan");
1085 p2p->p2p_scan_running = 1;
1086 eloop_cancel_timeout(p2p_scan_timeout, p2p, NULL);
1087 eloop_register_timeout(P2P_SCAN_TIMEOUT, 0, p2p_scan_timeout,
1088 p2p, NULL);
1089 }
1090}
1091
1092
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001093static int p2p_run_after_scan(struct p2p_data *p2p)
1094{
1095 struct p2p_device *dev;
1096 enum p2p_after_scan op;
1097
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001098 op = p2p->start_after_scan;
1099 p2p->start_after_scan = P2P_AFTER_SCAN_NOTHING;
1100 switch (op) {
1101 case P2P_AFTER_SCAN_NOTHING:
1102 break;
1103 case P2P_AFTER_SCAN_LISTEN:
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001104 p2p_dbg(p2p, "Start previously requested Listen state");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001105 p2p_listen(p2p, p2p->pending_listen_sec * 1000 +
1106 p2p->pending_listen_usec / 1000);
1107 return 1;
1108 case P2P_AFTER_SCAN_CONNECT:
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001109 p2p_dbg(p2p, "Start previously requested connect with " MACSTR,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001110 MAC2STR(p2p->after_scan_peer));
1111 dev = p2p_get_device(p2p, p2p->after_scan_peer);
1112 if (dev == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001113 p2p_dbg(p2p, "Peer not known anymore");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001114 break;
1115 }
1116 p2p_connect_send(p2p, dev);
1117 return 1;
1118 }
1119
1120 return 0;
1121}
1122
1123
1124static void p2p_scan_timeout(void *eloop_ctx, void *timeout_ctx)
1125{
1126 struct p2p_data *p2p = eloop_ctx;
1127 int running;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001128 p2p_dbg(p2p, "p2p_scan timeout (running=%d)", p2p->p2p_scan_running);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001129 running = p2p->p2p_scan_running;
1130 /* Make sure we recover from missed scan results callback */
1131 p2p->p2p_scan_running = 0;
1132
1133 if (running)
1134 p2p_run_after_scan(p2p);
1135}
1136
1137
1138static void p2p_free_req_dev_types(struct p2p_data *p2p)
1139{
1140 p2p->num_req_dev_types = 0;
1141 os_free(p2p->req_dev_types);
1142 p2p->req_dev_types = NULL;
1143}
1144
1145
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001146static int p2ps_gen_hash(struct p2p_data *p2p, const char *str, u8 *hash)
1147{
1148 u8 buf[SHA256_MAC_LEN];
1149 char str_buf[256];
1150 const u8 *adv_array;
1151 size_t i, adv_len;
1152
1153 if (!str || !hash)
1154 return 0;
1155
1156 if (!str[0]) {
1157 os_memcpy(hash, p2p->wild_card_hash, P2PS_HASH_LEN);
1158 return 1;
1159 }
1160
1161 adv_array = (u8 *) str_buf;
1162 adv_len = os_strlen(str);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07001163 if (adv_len >= sizeof(str_buf))
1164 return 0;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001165
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001166 for (i = 0; i < adv_len; i++) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001167 if (str[i] >= 'A' && str[i] <= 'Z')
1168 str_buf[i] = str[i] - 'A' + 'a';
1169 else
1170 str_buf[i] = str[i];
1171 }
1172
1173 if (sha256_vector(1, &adv_array, &adv_len, buf))
1174 return 0;
1175
1176 os_memcpy(hash, buf, P2PS_HASH_LEN);
1177 return 1;
1178}
1179
1180
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001181int p2p_find(struct p2p_data *p2p, unsigned int timeout,
1182 enum p2p_discovery_type type,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001183 unsigned int num_req_dev_types, const u8 *req_dev_types,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001184 const u8 *dev_id, unsigned int search_delay,
Hai Shaloma20dcd72022-02-04 13:43:00 -08001185 u8 seek_count, const char **seek, int freq, bool include_6ghz)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001186{
1187 int res;
Hai Shalom74f70d42019-02-11 14:42:39 -08001188 struct os_reltime start;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001189
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001190 p2p_dbg(p2p, "Starting find (type=%d)", type);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001191 if (p2p->p2p_scan_running) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001192 p2p_dbg(p2p, "p2p_scan is already running");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001193 }
1194
1195 p2p_free_req_dev_types(p2p);
1196 if (req_dev_types && num_req_dev_types) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001197 p2p->req_dev_types = os_memdup(req_dev_types,
1198 num_req_dev_types *
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001199 WPS_DEV_TYPE_LEN);
1200 if (p2p->req_dev_types == NULL)
1201 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001202 p2p->num_req_dev_types = num_req_dev_types;
1203 }
1204
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001205 if (dev_id) {
1206 os_memcpy(p2p->find_dev_id_buf, dev_id, ETH_ALEN);
1207 p2p->find_dev_id = p2p->find_dev_id_buf;
1208 } else
1209 p2p->find_dev_id = NULL;
Hai Shaloma20dcd72022-02-04 13:43:00 -08001210 p2p->include_6ghz = p2p_wfd_enabled(p2p) && include_6ghz;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001211 if (seek_count == 0 || !seek) {
1212 /* Not an ASP search */
1213 p2p->p2ps_seek = 0;
1214 } else if (seek_count == 1 && seek && (!seek[0] || !seek[0][0])) {
1215 /*
1216 * An empty seek string means no hash values, but still an ASP
1217 * search.
1218 */
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07001219 p2p_dbg(p2p, "ASP search");
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001220 p2p->p2ps_seek_count = 0;
1221 p2p->p2ps_seek = 1;
1222 } else if (seek && seek_count <= P2P_MAX_QUERY_HASH) {
1223 u8 buf[P2PS_HASH_LEN];
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07001224 int i, count = 0;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001225
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001226 for (i = 0; i < seek_count; i++) {
1227 if (!p2ps_gen_hash(p2p, seek[i], buf))
1228 continue;
1229
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07001230 p2p_dbg(p2p, "Seek service %s hash " MACSTR,
1231 seek[i], MAC2STR(buf));
1232 os_memcpy(&p2p->p2ps_seek_hash[count * P2PS_HASH_LEN],
1233 buf, P2PS_HASH_LEN);
1234 count++;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001235 }
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07001236
1237 p2p->p2ps_seek_count = count;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001238 p2p->p2ps_seek = 1;
1239 } else {
1240 p2p->p2ps_seek_count = 0;
1241 p2p->p2ps_seek = 1;
1242 }
1243
1244 /* Special case to perform wildcard search */
1245 if (p2p->p2ps_seek_count == 0 && p2p->p2ps_seek) {
1246 p2p->p2ps_seek_count = 1;
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07001247 os_memcpy(&p2p->p2ps_seek_hash, p2p->wild_card_hash,
1248 P2PS_HASH_LEN);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001249 }
1250
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001251 p2p->start_after_scan = P2P_AFTER_SCAN_NOTHING;
1252 p2p_clear_timeout(p2p);
Dmitry Shmidtde47be72016-01-07 12:52:55 -08001253 if (p2p->pending_listen_freq) {
1254 p2p_dbg(p2p, "Clear pending_listen_freq for p2p_find");
1255 p2p->pending_listen_freq = 0;
1256 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001257 p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001258 p2p->pending_listen_wait_drv = false;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001259 p2p->find_pending_full = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001260 p2p->find_type = type;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001261 if (freq != 2412 && freq != 2437 && freq != 2462 && freq != 60480)
1262 p2p->find_specified_freq = freq;
1263 else
1264 p2p->find_specified_freq = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001265 p2p_device_clear_reported(p2p);
Dmitry Shmidtde47be72016-01-07 12:52:55 -08001266 os_memset(p2p->sd_query_no_ack, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001267 p2p_set_state(p2p, P2P_SEARCH);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001268 p2p->search_delay = search_delay;
1269 p2p->in_search_delay = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001270 eloop_cancel_timeout(p2p_find_timeout, p2p, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001271 p2p->last_p2p_find_timeout = timeout;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001272 if (timeout)
1273 eloop_register_timeout(timeout, 0, p2p_find_timeout,
1274 p2p, NULL);
Hai Shalom74f70d42019-02-11 14:42:39 -08001275 os_get_reltime(&start);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001276 switch (type) {
1277 case P2P_FIND_START_WITH_FULL:
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001278 if (freq > 0) {
1279 /*
1280 * Start with the specified channel and then move to
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001281 * scans for social channels and this specific channel.
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001282 */
1283 res = p2p->cfg->p2p_scan(p2p->cfg->cb_ctx,
1284 P2P_SCAN_SPECIFIC, freq,
1285 p2p->num_req_dev_types,
1286 p2p->req_dev_types, dev_id,
Hai Shaloma20dcd72022-02-04 13:43:00 -08001287 DEV_PW_DEFAULT,
1288 p2p->include_6ghz);
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001289 break;
1290 }
1291 /* fall through */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001292 case P2P_FIND_PROGRESSIVE:
1293 res = p2p->cfg->p2p_scan(p2p->cfg->cb_ctx, P2P_SCAN_FULL, 0,
1294 p2p->num_req_dev_types,
Dmitry Shmidt04949592012-07-19 12:16:46 -07001295 p2p->req_dev_types, dev_id,
Hai Shaloma20dcd72022-02-04 13:43:00 -08001296 DEV_PW_DEFAULT, p2p->include_6ghz);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001297 break;
1298 case P2P_FIND_ONLY_SOCIAL:
1299 res = p2p->cfg->p2p_scan(p2p->cfg->cb_ctx, P2P_SCAN_SOCIAL, 0,
1300 p2p->num_req_dev_types,
Dmitry Shmidt04949592012-07-19 12:16:46 -07001301 p2p->req_dev_types, dev_id,
Hai Shaloma20dcd72022-02-04 13:43:00 -08001302 DEV_PW_DEFAULT, p2p->include_6ghz);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001303 break;
1304 default:
1305 return -1;
1306 }
1307
Hai Shalom74f70d42019-02-11 14:42:39 -08001308 if (!res)
1309 p2p->find_start = start;
1310
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001311 if (res != 0 && p2p->p2p_scan_running) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001312 p2p_dbg(p2p, "Failed to start p2p_scan - another p2p_scan was already running");
1313 /* wait for the previous p2p_scan to complete */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001314 if (type == P2P_FIND_PROGRESSIVE ||
1315 (type == P2P_FIND_START_WITH_FULL && freq == 0))
1316 p2p->find_pending_full = 1;
Dmitry Shmidt96be6222014-02-13 10:16:51 -08001317 res = 0; /* do not report failure */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001318 } else if (res != 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001319 p2p_dbg(p2p, "Failed to start p2p_scan");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001320 p2p_set_state(p2p, P2P_IDLE);
1321 eloop_cancel_timeout(p2p_find_timeout, p2p, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001322 }
1323
1324 return res;
1325}
1326
1327
1328void p2p_stop_find_for_freq(struct p2p_data *p2p, int freq)
1329{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001330 p2p_dbg(p2p, "Stopping find");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001331 eloop_cancel_timeout(p2p_find_timeout, p2p, NULL);
1332 p2p_clear_timeout(p2p);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001333 if (p2p->state == P2P_SEARCH || p2p->state == P2P_SD_DURING_FIND)
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001334 p2p->cfg->find_stopped(p2p->cfg->cb_ctx);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001335
1336 p2p->p2ps_seek_count = 0;
1337
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001338 p2p_set_state(p2p, P2P_IDLE);
1339 p2p_free_req_dev_types(p2p);
1340 p2p->start_after_scan = P2P_AFTER_SCAN_NOTHING;
Dmitry Shmidt8c652892013-03-01 10:14:01 -08001341 if (p2p->go_neg_peer)
1342 p2p->go_neg_peer->flags &= ~P2P_DEV_PEER_WAITING_RESPONSE;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001343 p2p->go_neg_peer = NULL;
1344 p2p->sd_peer = NULL;
1345 p2p->invite_peer = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001346 p2p_stop_listen_for_freq(p2p, freq);
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001347 p2p->send_action_in_progress = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001348}
1349
1350
1351void p2p_stop_listen_for_freq(struct p2p_data *p2p, int freq)
1352{
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001353 p2p_dbg(p2p,
1354 "%s(freq=%d) pending_listen_freq=%d in_listen=%d drv_in_listen=%d",
1355 __func__, freq, p2p->pending_listen_freq, p2p->in_listen,
1356 p2p->drv_in_listen);
Hai Shaloma20dcd72022-02-04 13:43:00 -08001357 if (freq > 0 &&
1358 ((p2p->drv_in_listen == freq && p2p->in_listen) ||
1359 p2p->pending_listen_freq == (unsigned int) freq)) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001360 p2p_dbg(p2p, "Skip stop_listen since we are on correct channel for response");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001361 return;
1362 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001363 if (p2p->in_listen) {
1364 p2p->in_listen = 0;
1365 p2p_clear_timeout(p2p);
1366 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001367 if (p2p->drv_in_listen) {
1368 /*
1369 * The driver may not deliver callback to p2p_listen_end()
1370 * when the operation gets canceled, so clear the internal
1371 * variable that is tracking driver state.
1372 */
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001373 p2p_dbg(p2p, "Clear drv_in_listen (%d)", p2p->drv_in_listen);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001374 p2p->drv_in_listen = 0;
1375 }
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001376 if (p2p->pending_listen_freq &&
1377 p2p->pending_listen_freq != (unsigned int) freq &&
1378 !p2p->drv_in_listen && p2p->pending_listen_wait_drv) {
1379 p2p_dbg(p2p,
1380 "Clear pending_listen_freq since the started listen did not complete before being stopped");
1381 p2p->pending_listen_freq = 0;
1382 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001383 p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001384 p2p->pending_listen_wait_drv = false;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001385}
1386
1387
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001388void p2p_stop_listen(struct p2p_data *p2p)
1389{
1390 if (p2p->state != P2P_LISTEN_ONLY) {
1391 p2p_dbg(p2p, "Skip stop_listen since not in listen_only state.");
1392 return;
1393 }
1394
1395 p2p_stop_listen_for_freq(p2p, 0);
1396 p2p_set_state(p2p, P2P_IDLE);
1397}
1398
1399
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001400void p2p_stop_find(struct p2p_data *p2p)
1401{
Dmitry Shmidt3c57b3f2014-05-22 15:13:07 -07001402 p2p->pending_listen_freq = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001403 p2p_stop_find_for_freq(p2p, 0);
1404}
1405
1406
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001407static int p2p_prepare_channel_pref(struct p2p_data *p2p,
1408 unsigned int force_freq,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001409 unsigned int pref_freq, int go)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001410{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001411 u8 op_class, op_channel;
1412 unsigned int freq = force_freq ? force_freq : pref_freq;
1413
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001414 p2p_dbg(p2p, "Prepare channel pref - force_freq=%u pref_freq=%u go=%d",
1415 force_freq, pref_freq, go);
Dmitry Shmidt4b060592013-04-29 16:42:49 -07001416 if (p2p_freq_to_channel(freq, &op_class, &op_channel) < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001417 p2p_dbg(p2p, "Unsupported frequency %u MHz", freq);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001418 return -1;
1419 }
1420
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001421 if (!p2p_channels_includes(&p2p->cfg->channels, op_class, op_channel) &&
1422 (go || !p2p_channels_includes(&p2p->cfg->cli_channels, op_class,
1423 op_channel))) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001424 p2p_dbg(p2p, "Frequency %u MHz (oper_class %u channel %u) not allowed for P2P",
1425 freq, op_class, op_channel);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001426 return -1;
1427 }
1428
1429 p2p->op_reg_class = op_class;
1430 p2p->op_channel = op_channel;
1431
1432 if (force_freq) {
1433 p2p->channels.reg_classes = 1;
1434 p2p->channels.reg_class[0].channels = 1;
1435 p2p->channels.reg_class[0].reg_class = p2p->op_reg_class;
1436 p2p->channels.reg_class[0].channel[0] = p2p->op_channel;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001437 } else {
Hai Shaloma20dcd72022-02-04 13:43:00 -08001438 p2p_copy_channels(&p2p->channels, &p2p->cfg->channels,
1439 p2p->allow_6ghz);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001440 }
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001441
1442 return 0;
1443}
1444
1445
1446static void p2p_prepare_channel_best(struct p2p_data *p2p)
1447{
1448 u8 op_class, op_channel;
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07001449 const int op_classes_5ghz[] = { 124, 125, 115, 0 };
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08001450 const int op_classes_ht40[] = { 126, 127, 116, 117, 0 };
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001451 const int op_classes_vht[] = { 128, 0 };
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001452 const int op_classes_edmg[] = { 181, 182, 183, 0 };
Hai Shaloma20dcd72022-02-04 13:43:00 -08001453 const int op_classes_6ghz[] = { 131, 0 };
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001454
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07001455 p2p_dbg(p2p, "Prepare channel best");
1456
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001457 if (!p2p->cfg->cfg_op_channel && p2p->best_freq_overall > 0 &&
1458 p2p_supported_freq(p2p, p2p->best_freq_overall) &&
Dmitry Shmidt4b060592013-04-29 16:42:49 -07001459 p2p_freq_to_channel(p2p->best_freq_overall, &op_class, &op_channel)
1460 == 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001461 p2p_dbg(p2p, "Select best overall channel as operating channel preference");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001462 p2p->op_reg_class = op_class;
1463 p2p->op_channel = op_channel;
1464 } else if (!p2p->cfg->cfg_op_channel && p2p->best_freq_5 > 0 &&
1465 p2p_supported_freq(p2p, p2p->best_freq_5) &&
Dmitry Shmidt4b060592013-04-29 16:42:49 -07001466 p2p_freq_to_channel(p2p->best_freq_5, &op_class, &op_channel)
1467 == 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001468 p2p_dbg(p2p, "Select best 5 GHz channel as operating channel preference");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001469 p2p->op_reg_class = op_class;
1470 p2p->op_channel = op_channel;
1471 } else if (!p2p->cfg->cfg_op_channel && p2p->best_freq_24 > 0 &&
1472 p2p_supported_freq(p2p, p2p->best_freq_24) &&
Dmitry Shmidt4b060592013-04-29 16:42:49 -07001473 p2p_freq_to_channel(p2p->best_freq_24, &op_class,
1474 &op_channel) == 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001475 p2p_dbg(p2p, "Select best 2.4 GHz channel as operating channel preference");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001476 p2p->op_reg_class = op_class;
1477 p2p->op_channel = op_channel;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07001478 } else if (p2p->cfg->num_pref_chan > 0 &&
1479 p2p_channels_includes(&p2p->cfg->channels,
1480 p2p->cfg->pref_chan[0].op_class,
1481 p2p->cfg->pref_chan[0].chan)) {
1482 p2p_dbg(p2p, "Select first pref_chan entry as operating channel preference");
1483 p2p->op_reg_class = p2p->cfg->pref_chan[0].op_class;
1484 p2p->op_channel = p2p->cfg->pref_chan[0].chan;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001485 } else if (p2p_channel_select(&p2p->cfg->channels, op_classes_edmg,
1486 &p2p->op_reg_class, &p2p->op_channel) ==
1487 0) {
1488 p2p_dbg(p2p, "Select possible EDMG channel (op_class %u channel %u) as operating channel preference",
1489 p2p->op_reg_class, p2p->op_channel);
Hai Shaloma20dcd72022-02-04 13:43:00 -08001490 } else if (p2p->allow_6ghz &&
1491 (p2p_channel_select(&p2p->cfg->channels, op_classes_6ghz,
1492 &p2p->op_reg_class, &p2p->op_channel) ==
1493 0)) {
1494 p2p_dbg(p2p, "Select possible 6 GHz channel (op_class %u channel %u) as operating channel preference",
1495 p2p->op_reg_class, p2p->op_channel);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001496 } else if (p2p_channel_select(&p2p->cfg->channels, op_classes_vht,
1497 &p2p->op_reg_class, &p2p->op_channel) ==
1498 0) {
1499 p2p_dbg(p2p, "Select possible VHT 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_ht40,
1502 &p2p->op_reg_class, &p2p->op_channel) ==
1503 0) {
1504 p2p_dbg(p2p, "Select possible HT40 channel (op_class %u channel %u) as operating channel preference",
1505 p2p->op_reg_class, p2p->op_channel);
1506 } else if (p2p_channel_select(&p2p->cfg->channels, op_classes_5ghz,
1507 &p2p->op_reg_class, &p2p->op_channel) ==
1508 0) {
1509 p2p_dbg(p2p, "Select possible 5 GHz channel (op_class %u channel %u) as operating channel preference",
1510 p2p->op_reg_class, p2p->op_channel);
Dmitry Shmidtd11f0192014-03-24 12:09:47 -07001511 } else if (p2p_channels_includes(&p2p->cfg->channels,
1512 p2p->cfg->op_reg_class,
1513 p2p->cfg->op_channel)) {
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07001514 p2p_dbg(p2p, "Select pre-configured channel as operating channel preference");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001515 p2p->op_reg_class = p2p->cfg->op_reg_class;
1516 p2p->op_channel = p2p->cfg->op_channel;
Dmitry Shmidtd11f0192014-03-24 12:09:47 -07001517 } else if (p2p_channel_random_social(&p2p->cfg->channels,
1518 &p2p->op_reg_class,
Hai Shalom74f70d42019-02-11 14:42:39 -08001519 &p2p->op_channel,
1520 NULL, NULL) == 0) {
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001521 p2p_dbg(p2p, "Select random available social channel (op_class %u channel %u) as operating channel preference",
1522 p2p->op_reg_class, p2p->op_channel);
Dmitry Shmidtd11f0192014-03-24 12:09:47 -07001523 } else {
1524 /* Select any random available channel from the first available
1525 * operating class */
Sunil Ravi2a14cf12023-11-21 00:54:38 +00001526 if (p2p_channel_select(&p2p->cfg->channels, NULL,
1527 &p2p->op_reg_class,
1528 &p2p->op_channel) == 0)
1529 p2p_dbg(p2p,
1530 "Select random available channel %d from operating class %d as operating channel preference",
1531 p2p->op_channel, p2p->op_reg_class);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001532 }
1533
Hai Shaloma20dcd72022-02-04 13:43:00 -08001534 p2p_copy_channels(&p2p->channels, &p2p->cfg->channels, p2p->allow_6ghz);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001535}
1536
1537
1538/**
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001539 * p2p_prepare_channel - Select operating channel for GO Negotiation or P2PS PD
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001540 * @p2p: P2P module context from p2p_init()
1541 * @dev: Selected peer device
1542 * @force_freq: Forced frequency in MHz or 0 if not forced
1543 * @pref_freq: Preferred frequency in MHz or 0 if no preference
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001544 * @go: Whether the local end will be forced to be GO
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001545 * Returns: 0 on success, -1 on failure (channel not supported for P2P)
1546 *
1547 * This function is used to do initial operating channel selection for GO
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001548 * Negotiation prior to having received peer information or for P2PS PD
1549 * signalling. The selected channel may be further optimized in
1550 * p2p_reselect_channel() once the peer information is available.
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001551 */
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08001552int p2p_prepare_channel(struct p2p_data *p2p, struct p2p_device *dev,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001553 unsigned int force_freq, unsigned int pref_freq, int go)
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001554{
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001555 p2p_dbg(p2p, "Prepare channel - force_freq=%u pref_freq=%u go=%d",
1556 force_freq, pref_freq, go);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001557 if (force_freq || pref_freq) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001558 if (p2p_prepare_channel_pref(p2p, force_freq, pref_freq, go) <
1559 0)
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001560 return -1;
1561 } else {
1562 p2p_prepare_channel_best(p2p);
1563 }
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001564 p2p_channels_dump(p2p, "prepared channels", &p2p->channels);
1565 if (go)
1566 p2p_channels_remove_freqs(&p2p->channels, &p2p->no_go_freq);
1567 else if (!force_freq)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001568 p2p_channels_union_inplace(&p2p->channels,
1569 &p2p->cfg->cli_channels);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001570 p2p_channels_dump(p2p, "after go/cli filter/add", &p2p->channels);
1571
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001572 p2p_dbg(p2p, "Own preference for operation channel: Operating Class %u Channel %u%s",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001573 p2p->op_reg_class, p2p->op_channel,
1574 force_freq ? " (forced)" : "");
1575
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001576 if (force_freq)
1577 dev->flags |= P2P_DEV_FORCE_FREQ;
1578 else
1579 dev->flags &= ~P2P_DEV_FORCE_FREQ;
1580
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001581 return 0;
1582}
1583
1584
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001585static void p2p_set_dev_persistent(struct p2p_device *dev,
1586 int persistent_group)
1587{
1588 switch (persistent_group) {
1589 case 0:
1590 dev->flags &= ~(P2P_DEV_PREFER_PERSISTENT_GROUP |
1591 P2P_DEV_PREFER_PERSISTENT_RECONN);
1592 break;
1593 case 1:
1594 dev->flags |= P2P_DEV_PREFER_PERSISTENT_GROUP;
1595 dev->flags &= ~P2P_DEV_PREFER_PERSISTENT_RECONN;
1596 break;
1597 case 2:
1598 dev->flags |= P2P_DEV_PREFER_PERSISTENT_GROUP |
1599 P2P_DEV_PREFER_PERSISTENT_RECONN;
1600 break;
1601 }
1602}
1603
1604
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001605int p2p_connect(struct p2p_data *p2p, const u8 *peer_addr,
1606 enum p2p_wps_method wps_method,
1607 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07001608 unsigned int force_freq, int persistent_group,
1609 const u8 *force_ssid, size_t force_ssid_len,
Sunil Ravic0f5d412024-09-11 22:12:49 +00001610 int pd_before_go_neg, unsigned int pref_freq, u16 oob_pw_id,
1611 bool p2p2, u16 bootstrap, const char *password)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001612{
1613 struct p2p_device *dev;
1614
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001615 p2p_dbg(p2p, "Request to start group negotiation - peer=" MACSTR
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001616 " GO Intent=%d Intended Interface Address=" MACSTR
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001617 " wps_method=%d persistent_group=%d pd_before_go_neg=%d "
Hai Shaloma20dcd72022-02-04 13:43:00 -08001618 "oob_pw_id=%u allow_6ghz=%d",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001619 MAC2STR(peer_addr), go_intent, MAC2STR(own_interface_addr),
Hai Shaloma20dcd72022-02-04 13:43:00 -08001620 wps_method, persistent_group, pd_before_go_neg, oob_pw_id,
1621 p2p->allow_6ghz);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001622
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001623 dev = p2p_get_device(p2p, peer_addr);
1624 if (dev == NULL || (dev->flags & P2P_DEV_PROBE_REQ_ONLY)) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001625 p2p_dbg(p2p, "Cannot connect to unknown P2P Device " MACSTR,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001626 MAC2STR(peer_addr));
1627 return -1;
1628 }
1629
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001630 if (p2p_prepare_channel(p2p, dev, force_freq, pref_freq,
1631 go_intent == 15) < 0)
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001632 return -1;
1633
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001634 if (dev->flags & P2P_DEV_GROUP_CLIENT_ONLY) {
1635 if (!(dev->info.dev_capab &
1636 P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY)) {
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 " that is in a group and is not discoverable",
1639 MAC2STR(peer_addr));
1640 return -1;
1641 }
1642 if (dev->oper_freq <= 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001643 p2p_dbg(p2p, "Cannot connect to P2P Device " MACSTR
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001644 " with incomplete information",
1645 MAC2STR(peer_addr));
1646 return -1;
1647 }
1648
1649 /*
1650 * First, try to connect directly. If the peer does not
1651 * acknowledge frames, assume it is sleeping and use device
1652 * discoverability via the GO at that point.
1653 */
1654 }
1655
Dmitry Shmidt04949592012-07-19 12:16:46 -07001656 p2p->ssid_set = 0;
1657 if (force_ssid) {
1658 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Forced SSID",
1659 force_ssid, force_ssid_len);
1660 os_memcpy(p2p->ssid, force_ssid, force_ssid_len);
1661 p2p->ssid_len = force_ssid_len;
1662 p2p->ssid_set = 1;
1663 }
1664
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001665 dev->flags &= ~P2P_DEV_NOT_YET_READY;
1666 dev->flags &= ~P2P_DEV_USER_REJECTED;
1667 dev->flags &= ~P2P_DEV_WAIT_GO_NEG_RESPONSE;
1668 dev->flags &= ~P2P_DEV_WAIT_GO_NEG_CONFIRM;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001669 if (pd_before_go_neg)
1670 dev->flags |= P2P_DEV_PD_BEFORE_GO_NEG;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001671 else {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001672 dev->flags &= ~P2P_DEV_PD_BEFORE_GO_NEG;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001673 /*
Dmitry Shmidt9cdf1b92013-02-27 12:58:50 -08001674 * Assign dialog token and tie breaker here to use the same
1675 * values in each retry within the same GO Negotiation exchange.
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001676 */
1677 dev->dialog_token++;
1678 if (dev->dialog_token == 0)
1679 dev->dialog_token = 1;
Dmitry Shmidt9cdf1b92013-02-27 12:58:50 -08001680 dev->tie_breaker = p2p->next_tie_breaker;
1681 p2p->next_tie_breaker = !p2p->next_tie_breaker;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001682 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001683 dev->connect_reqs = 0;
1684 dev->go_neg_req_sent = 0;
1685 dev->go_state = UNKNOWN_GO;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001686 p2p_set_dev_persistent(dev, persistent_group);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001687 p2p->go_intent = go_intent;
1688 os_memcpy(p2p->intended_addr, own_interface_addr, ETH_ALEN);
1689
1690 if (p2p->state != P2P_IDLE)
1691 p2p_stop_find(p2p);
1692
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001693 dev->wps_method = wps_method;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001694 dev->oob_pw_id = oob_pw_id;
Sunil Ravic0f5d412024-09-11 22:12:49 +00001695 dev->p2p2 = p2p2;
1696 dev->req_bootstrap_method = bootstrap;
1697 if (password && os_strlen(password) < sizeof(dev->password))
1698 os_strlcpy(dev->password, password, sizeof(dev->password));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001699 dev->status = P2P_SC_SUCCESS;
1700
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001701 if (p2p->p2p_scan_running) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001702 p2p_dbg(p2p, "p2p_scan running - delay connect send");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001703 p2p->start_after_scan = P2P_AFTER_SCAN_CONNECT;
1704 os_memcpy(p2p->after_scan_peer, peer_addr, ETH_ALEN);
1705 return 0;
1706 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001707
1708 return p2p_connect_send(p2p, dev);
1709}
1710
1711
1712int p2p_authorize(struct p2p_data *p2p, const u8 *peer_addr,
1713 enum p2p_wps_method wps_method,
1714 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07001715 unsigned int force_freq, int persistent_group,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001716 const u8 *force_ssid, size_t force_ssid_len,
Sunil Ravic0f5d412024-09-11 22:12:49 +00001717 unsigned int pref_freq, u16 oob_pw_id, u16 bootstrap,
1718 const char *password)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001719{
1720 struct p2p_device *dev;
1721
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001722 p2p_dbg(p2p, "Request to authorize group negotiation - peer=" MACSTR
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001723 " GO Intent=%d Intended Interface Address=" MACSTR
Hai Shaloma20dcd72022-02-04 13:43:00 -08001724 " wps_method=%d persistent_group=%d oob_pw_id=%u allow_6ghz=%d",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001725 MAC2STR(peer_addr), go_intent, MAC2STR(own_interface_addr),
Hai Shaloma20dcd72022-02-04 13:43:00 -08001726 wps_method, persistent_group, oob_pw_id, p2p->allow_6ghz);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001727
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001728 dev = p2p_get_device(p2p, peer_addr);
1729 if (dev == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001730 p2p_dbg(p2p, "Cannot authorize unknown P2P Device " MACSTR,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001731 MAC2STR(peer_addr));
1732 return -1;
1733 }
1734
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001735 if (p2p_prepare_channel(p2p, dev, force_freq, pref_freq, go_intent ==
1736 15) < 0)
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001737 return -1;
1738
Dmitry Shmidt04949592012-07-19 12:16:46 -07001739 p2p->ssid_set = 0;
1740 if (force_ssid) {
1741 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Forced SSID",
1742 force_ssid, force_ssid_len);
1743 os_memcpy(p2p->ssid, force_ssid, force_ssid_len);
1744 p2p->ssid_len = force_ssid_len;
1745 p2p->ssid_set = 1;
1746 }
1747
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001748 dev->flags &= ~P2P_DEV_NOT_YET_READY;
1749 dev->flags &= ~P2P_DEV_USER_REJECTED;
1750 dev->go_neg_req_sent = 0;
1751 dev->go_state = UNKNOWN_GO;
Sunil Ravic0f5d412024-09-11 22:12:49 +00001752 dev->req_bootstrap_method = bootstrap;
1753
1754 if (password && os_strlen(password) < sizeof(dev->password))
1755 os_strlcpy(dev->password, password, sizeof(dev->password));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001756 p2p_set_dev_persistent(dev, persistent_group);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001757 p2p->go_intent = go_intent;
1758 os_memcpy(p2p->intended_addr, own_interface_addr, ETH_ALEN);
1759
1760 dev->wps_method = wps_method;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001761 dev->oob_pw_id = oob_pw_id;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001762 dev->status = P2P_SC_SUCCESS;
1763
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001764 return 0;
1765}
1766
1767
1768void p2p_add_dev_info(struct p2p_data *p2p, const u8 *addr,
1769 struct p2p_device *dev, struct p2p_message *msg)
1770{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001771 os_get_reltime(&dev->last_seen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001772
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001773 p2p_copy_wps_info(p2p, dev, 0, msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001774
1775 if (msg->listen_channel) {
1776 int freq;
Dmitry Shmidt4b060592013-04-29 16:42:49 -07001777 freq = p2p_channel_to_freq(msg->listen_channel[3],
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001778 msg->listen_channel[4]);
1779 if (freq < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001780 p2p_dbg(p2p, "Unknown peer Listen channel: "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001781 "country=%c%c(0x%02x) reg_class=%u channel=%u",
1782 msg->listen_channel[0],
1783 msg->listen_channel[1],
1784 msg->listen_channel[2],
1785 msg->listen_channel[3],
1786 msg->listen_channel[4]);
1787 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001788 p2p_dbg(p2p, "Update peer " MACSTR
1789 " Listen channel: %u -> %u MHz",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001790 MAC2STR(dev->info.p2p_device_addr),
1791 dev->listen_freq, freq);
1792 dev->listen_freq = freq;
1793 }
1794 }
1795
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001796 if (msg->wfd_subelems) {
1797 wpabuf_free(dev->info.wfd_subelems);
1798 dev->info.wfd_subelems = wpabuf_dup(msg->wfd_subelems);
1799 }
1800
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001801 if (dev->flags & P2P_DEV_PROBE_REQ_ONLY) {
1802 dev->flags &= ~P2P_DEV_PROBE_REQ_ONLY;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001803 p2p_dbg(p2p, "Completed device entry based on data from GO Negotiation Request");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001804 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001805 p2p_dbg(p2p, "Created device entry based on GO Neg Req: "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001806 MACSTR " dev_capab=0x%x group_capab=0x%x name='%s' "
1807 "listen_freq=%d",
1808 MAC2STR(dev->info.p2p_device_addr),
1809 dev->info.dev_capab, dev->info.group_capab,
1810 dev->info.device_name, dev->listen_freq);
1811 }
1812
1813 dev->flags &= ~P2P_DEV_GROUP_CLIENT_ONLY;
1814
1815 if (dev->flags & P2P_DEV_USER_REJECTED) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001816 p2p_dbg(p2p, "Do not report rejected device");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001817 return;
1818 }
1819
1820 p2p->cfg->dev_found(p2p->cfg->cb_ctx, addr, &dev->info,
1821 !(dev->flags & P2P_DEV_REPORTED_ONCE));
1822 dev->flags |= P2P_DEV_REPORTED | P2P_DEV_REPORTED_ONCE;
1823}
1824
1825
1826void p2p_build_ssid(struct p2p_data *p2p, u8 *ssid, size_t *ssid_len)
1827{
1828 os_memcpy(ssid, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN);
1829 p2p_random((char *) &ssid[P2P_WILDCARD_SSID_LEN], 2);
1830 os_memcpy(&ssid[P2P_WILDCARD_SSID_LEN + 2],
1831 p2p->cfg->ssid_postfix, p2p->cfg->ssid_postfix_len);
1832 *ssid_len = P2P_WILDCARD_SSID_LEN + 2 + p2p->cfg->ssid_postfix_len;
1833}
1834
1835
1836int p2p_go_params(struct p2p_data *p2p, struct p2p_go_neg_results *params)
1837{
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001838 if (p2p->ssid_set) {
1839 os_memcpy(params->ssid, p2p->ssid, p2p->ssid_len);
1840 params->ssid_len = p2p->ssid_len;
1841 } else {
1842 p2p_build_ssid(p2p, params->ssid, &params->ssid_len);
1843 }
1844 p2p->ssid_set = 0;
1845
Jimmy Chen6d7e3902018-11-20 10:15:16 +08001846 if (p2p->passphrase_set) {
1847 os_memcpy(params->passphrase, p2p->passphrase, os_strlen(p2p->passphrase));
1848 } else {
1849 p2p_random(params->passphrase, p2p->cfg->passphrase_len);
Sunil8cd6f4d2022-06-28 18:40:46 +00001850 params->passphrase[p2p->cfg->passphrase_len] = '\0';
Jimmy Chen6d7e3902018-11-20 10:15:16 +08001851 }
1852 p2p->passphrase_set = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001853 return 0;
1854}
1855
1856
1857void p2p_go_complete(struct p2p_data *p2p, struct p2p_device *peer)
1858{
1859 struct p2p_go_neg_results res;
1860 int go = peer->go_state == LOCAL_GO;
1861 struct p2p_channels intersection;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001862
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001863 p2p_dbg(p2p, "GO Negotiation with " MACSTR " completed (%s will be GO)",
1864 MAC2STR(peer->info.p2p_device_addr), go ? "local end" : "peer");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001865
1866 os_memset(&res, 0, sizeof(res));
1867 res.role_go = go;
1868 os_memcpy(res.peer_device_addr, peer->info.p2p_device_addr, ETH_ALEN);
1869 os_memcpy(res.peer_interface_addr, peer->intended_addr, ETH_ALEN);
1870 res.wps_method = peer->wps_method;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001871 if (peer->flags & P2P_DEV_PREFER_PERSISTENT_GROUP) {
1872 if (peer->flags & P2P_DEV_PREFER_PERSISTENT_RECONN)
1873 res.persistent_group = 2;
1874 else
1875 res.persistent_group = 1;
1876 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001877
1878 if (go) {
1879 /* Setup AP mode for WPS provisioning */
Dmitry Shmidt4b060592013-04-29 16:42:49 -07001880 res.freq = p2p_channel_to_freq(p2p->op_reg_class,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001881 p2p->op_channel);
1882 os_memcpy(res.ssid, p2p->ssid, p2p->ssid_len);
1883 res.ssid_len = p2p->ssid_len;
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07001884 p2p_random(res.passphrase, p2p->cfg->passphrase_len);
Sunil8cd6f4d2022-06-28 18:40:46 +00001885 res.passphrase[p2p->cfg->passphrase_len] = '\0';
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001886 } else {
1887 res.freq = peer->oper_freq;
1888 if (p2p->ssid_len) {
1889 os_memcpy(res.ssid, p2p->ssid, p2p->ssid_len);
1890 res.ssid_len = p2p->ssid_len;
1891 }
1892 }
1893
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001894 p2p_channels_dump(p2p, "own channels", &p2p->channels);
1895 p2p_channels_dump(p2p, "peer channels", &peer->channels);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001896 p2p_channels_intersect(&p2p->channels, &peer->channels,
1897 &intersection);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001898 if (go) {
1899 p2p_channels_remove_freqs(&intersection, &p2p->no_go_freq);
1900 p2p_channels_dump(p2p, "intersection after no-GO removal",
1901 &intersection);
1902 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001903
1904 p2p_channels_to_freqs(&intersection, res.freq_list,
1905 P2P_MAX_CHANNELS);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001906
1907 res.peer_config_timeout = go ? peer->client_timeout : peer->go_timeout;
1908
1909 p2p_clear_timeout(p2p);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001910 p2p->ssid_set = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001911 peer->go_neg_req_sent = 0;
Dmitry Shmidt29333592017-01-09 12:27:11 -08001912 peer->flags &= ~P2P_DEV_PEER_WAITING_RESPONSE;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001913 peer->wps_method = WPS_NOT_READY;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001914 peer->oob_pw_id = 0;
Dmitry Shmidt413dde72014-04-11 10:23:22 -07001915 wpabuf_free(peer->go_neg_conf);
1916 peer->go_neg_conf = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001917
1918 p2p_set_state(p2p, P2P_PROVISIONING);
1919 p2p->cfg->go_neg_completed(p2p->cfg->cb_ctx, &res);
1920}
1921
1922
1923static void p2p_rx_p2p_action(struct p2p_data *p2p, const u8 *sa,
1924 const u8 *data, size_t len, int rx_freq)
1925{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001926 p2p_dbg(p2p, "RX P2P Public Action from " MACSTR, MAC2STR(sa));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001927 wpa_hexdump(MSG_MSGDUMP, "P2P: P2P Public Action contents", data, len);
1928
1929 if (len < 1)
1930 return;
1931
1932 switch (data[0]) {
1933 case P2P_GO_NEG_REQ:
Sunil Ravic0f5d412024-09-11 22:12:49 +00001934 p2p_handle_go_neg_req(p2p, sa, data + 1, len - 1, rx_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001935 break;
1936 case P2P_GO_NEG_RESP:
Sunil Ravic0f5d412024-09-11 22:12:49 +00001937 p2p_handle_go_neg_resp(p2p, sa, data + 1, len - 1, rx_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001938 break;
1939 case P2P_GO_NEG_CONF:
Sunil Ravic0f5d412024-09-11 22:12:49 +00001940 p2p_handle_go_neg_conf(p2p, sa, data + 1, len - 1, false);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001941 break;
1942 case P2P_INVITATION_REQ:
Sunil Ravic0f5d412024-09-11 22:12:49 +00001943 p2p_handle_invitation_req(p2p, sa, data + 1, len - 1, rx_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001944 break;
1945 case P2P_INVITATION_RESP:
1946 p2p_process_invitation_resp(p2p, sa, data + 1, len - 1);
1947 break;
1948 case P2P_PROV_DISC_REQ:
Sunil Ravic0f5d412024-09-11 22:12:49 +00001949 p2p_handle_prov_disc_req(p2p, sa, data + 1, len - 1, rx_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001950 break;
1951 case P2P_PROV_DISC_RESP:
Sunil Ravic0f5d412024-09-11 22:12:49 +00001952 p2p_handle_prov_disc_resp(p2p, sa, data + 1, len - 1, rx_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001953 break;
1954 case P2P_DEV_DISC_REQ:
1955 p2p_process_dev_disc_req(p2p, sa, data + 1, len - 1, rx_freq);
1956 break;
1957 case P2P_DEV_DISC_RESP:
1958 p2p_process_dev_disc_resp(p2p, sa, data + 1, len - 1);
1959 break;
1960 default:
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001961 p2p_dbg(p2p, "Unsupported P2P Public Action frame type %d",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001962 data[0]);
1963 break;
1964 }
1965}
1966
1967
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001968static void p2p_rx_action_public(struct p2p_data *p2p, const u8 *da,
1969 const u8 *sa, const u8 *bssid, const u8 *data,
1970 size_t len, int freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001971{
1972 if (len < 1)
1973 return;
1974
1975 switch (data[0]) {
1976 case WLAN_PA_VENDOR_SPECIFIC:
1977 data++;
1978 len--;
Dmitry Shmidta38abf92014-03-06 13:38:44 -08001979 if (len < 4)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001980 return;
Dmitry Shmidta38abf92014-03-06 13:38:44 -08001981 if (WPA_GET_BE32(data) != P2P_IE_VENDOR_TYPE)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001982 return;
1983
Dmitry Shmidta38abf92014-03-06 13:38:44 -08001984 data += 4;
1985 len -= 4;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001986
Dmitry Shmidta38abf92014-03-06 13:38:44 -08001987 p2p_rx_p2p_action(p2p, sa, data, len, freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001988 break;
1989 case WLAN_PA_GAS_INITIAL_REQ:
1990 p2p_rx_gas_initial_req(p2p, sa, data + 1, len - 1, freq);
1991 break;
1992 case WLAN_PA_GAS_INITIAL_RESP:
1993 p2p_rx_gas_initial_resp(p2p, sa, data + 1, len - 1, freq);
1994 break;
1995 case WLAN_PA_GAS_COMEBACK_REQ:
1996 p2p_rx_gas_comeback_req(p2p, sa, data + 1, len - 1, freq);
1997 break;
1998 case WLAN_PA_GAS_COMEBACK_RESP:
1999 p2p_rx_gas_comeback_resp(p2p, sa, data + 1, len - 1, freq);
2000 break;
2001 }
2002}
2003
2004
2005void p2p_rx_action(struct p2p_data *p2p, const u8 *da, const u8 *sa,
2006 const u8 *bssid, u8 category,
2007 const u8 *data, size_t len, int freq)
2008{
2009 if (category == WLAN_ACTION_PUBLIC) {
2010 p2p_rx_action_public(p2p, da, sa, bssid, data, len, freq);
2011 return;
2012 }
2013
2014 if (category != WLAN_ACTION_VENDOR_SPECIFIC)
2015 return;
2016
2017 if (len < 4)
2018 return;
2019
Dmitry Shmidta38abf92014-03-06 13:38:44 -08002020 if (WPA_GET_BE32(data) != P2P_IE_VENDOR_TYPE)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002021 return;
Dmitry Shmidta38abf92014-03-06 13:38:44 -08002022 data += 4;
2023 len -= 4;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002024
2025 /* P2P action frame */
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002026 p2p_dbg(p2p, "RX P2P Action from " MACSTR, MAC2STR(sa));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002027 wpa_hexdump(MSG_MSGDUMP, "P2P: P2P Action contents", data, len);
2028
2029 if (len < 1)
2030 return;
2031 switch (data[0]) {
2032 case P2P_NOA:
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002033 p2p_dbg(p2p, "Received P2P Action - Notice of Absence");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002034 /* TODO */
2035 break;
2036 case P2P_PRESENCE_REQ:
2037 p2p_process_presence_req(p2p, da, sa, data + 1, len - 1, freq);
2038 break;
2039 case P2P_PRESENCE_RESP:
2040 p2p_process_presence_resp(p2p, da, sa, data + 1, len - 1);
2041 break;
2042 case P2P_GO_DISC_REQ:
2043 p2p_process_go_disc_req(p2p, da, sa, data + 1, len - 1, freq);
2044 break;
2045 default:
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002046 p2p_dbg(p2p, "Received P2P Action - unknown type %u", data[0]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002047 break;
2048 }
2049}
2050
2051
2052static void p2p_go_neg_start(void *eloop_ctx, void *timeout_ctx)
2053{
2054 struct p2p_data *p2p = eloop_ctx;
2055 if (p2p->go_neg_peer == NULL)
2056 return;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07002057 if (p2p->pending_listen_freq) {
2058 p2p_dbg(p2p, "Clear pending_listen_freq for p2p_go_neg_start");
2059 p2p->pending_listen_freq = 0;
2060 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002061 p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
Sunil Ravib0ac25f2024-07-12 01:42:03 +00002062 p2p->pending_listen_wait_drv = false;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002063 p2p->go_neg_peer->status = P2P_SC_SUCCESS;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07002064 /*
2065 * Set new timeout to make sure a previously set one does not expire
2066 * too quickly while waiting for the GO Negotiation to complete.
2067 */
2068 p2p_set_timeout(p2p, 0, 500000);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002069 p2p_connect_send(p2p, p2p->go_neg_peer);
2070}
2071
2072
2073static void p2p_invite_start(void *eloop_ctx, void *timeout_ctx)
2074{
2075 struct p2p_data *p2p = eloop_ctx;
2076 if (p2p->invite_peer == NULL)
2077 return;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07002078 if (p2p->pending_listen_freq) {
2079 p2p_dbg(p2p, "Clear pending_listen_freq for p2p_invite_start");
2080 p2p->pending_listen_freq = 0;
2081 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002082 p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
Sunil Ravib0ac25f2024-07-12 01:42:03 +00002083 p2p->pending_listen_wait_drv = false;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002084 p2p_invite_send(p2p, p2p->invite_peer, p2p->invite_go_dev_addr,
2085 p2p->invite_dev_pw_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002086}
2087
2088
2089static void p2p_add_dev_from_probe_req(struct p2p_data *p2p, const u8 *addr,
2090 const u8 *ie, size_t ie_len)
2091{
2092 struct p2p_message msg;
2093 struct p2p_device *dev;
2094
2095 os_memset(&msg, 0, sizeof(msg));
2096 if (p2p_parse_ies(ie, ie_len, &msg) < 0 || msg.p2p_attributes == NULL)
2097 {
2098 p2p_parse_free(&msg);
2099 return; /* not a P2P probe */
2100 }
2101
2102 if (msg.ssid == NULL || msg.ssid[1] != P2P_WILDCARD_SSID_LEN ||
2103 os_memcmp(msg.ssid + 2, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN)
2104 != 0) {
2105 /* The Probe Request is not part of P2P Device Discovery. It is
2106 * not known whether the source address of the frame is the P2P
2107 * Device Address or P2P Interface Address. Do not add a new
2108 * peer entry based on this frames.
2109 */
2110 p2p_parse_free(&msg);
2111 return;
2112 }
2113
2114 dev = p2p_get_device(p2p, addr);
2115 if (dev) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002116 if (msg.listen_channel) {
2117 int freq;
2118
2119 if (dev->country[0] == 0)
2120 os_memcpy(dev->country, msg.listen_channel, 3);
2121
2122 freq = p2p_channel_to_freq(msg.listen_channel[3],
2123 msg.listen_channel[4]);
2124
2125 if (freq > 0 && dev->listen_freq != freq) {
2126 p2p_dbg(p2p,
2127 "Updated peer " MACSTR " Listen channel (Probe Request): %d -> %d MHz",
2128 MAC2STR(addr), dev->listen_freq, freq);
2129 dev->listen_freq = freq;
2130 }
2131 }
2132
Sunil Ravi77d572f2023-01-17 23:58:31 +00002133 p2p_update_peer_6ghz_capab(dev, &msg);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002134 os_get_reltime(&dev->last_seen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002135 p2p_parse_free(&msg);
2136 return; /* already known */
2137 }
2138
2139 dev = p2p_create_device(p2p, addr);
2140 if (dev == NULL) {
2141 p2p_parse_free(&msg);
2142 return;
2143 }
2144
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002145 os_get_reltime(&dev->last_seen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002146 dev->flags |= P2P_DEV_PROBE_REQ_ONLY;
2147
2148 if (msg.listen_channel) {
2149 os_memcpy(dev->country, msg.listen_channel, 3);
Dmitry Shmidt4b060592013-04-29 16:42:49 -07002150 dev->listen_freq = p2p_channel_to_freq(msg.listen_channel[3],
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002151 msg.listen_channel[4]);
2152 }
2153
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002154 p2p_copy_wps_info(p2p, dev, 1, &msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002155
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002156 if (msg.wfd_subelems) {
2157 wpabuf_free(dev->info.wfd_subelems);
2158 dev->info.wfd_subelems = wpabuf_dup(msg.wfd_subelems);
2159 }
2160
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002161 p2p_parse_free(&msg);
2162
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002163 p2p_dbg(p2p, "Created device entry based on Probe Req: " MACSTR
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002164 " dev_capab=0x%x group_capab=0x%x name='%s' listen_freq=%d",
2165 MAC2STR(dev->info.p2p_device_addr), dev->info.dev_capab,
2166 dev->info.group_capab, dev->info.device_name,
2167 dev->listen_freq);
2168}
2169
2170
2171struct p2p_device * p2p_add_dev_from_go_neg_req(struct p2p_data *p2p,
2172 const u8 *addr,
2173 struct p2p_message *msg)
2174{
2175 struct p2p_device *dev;
2176
2177 dev = p2p_get_device(p2p, addr);
2178 if (dev) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002179 os_get_reltime(&dev->last_seen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002180 return dev; /* already known */
2181 }
2182
2183 dev = p2p_create_device(p2p, addr);
2184 if (dev == NULL)
2185 return NULL;
2186
2187 p2p_add_dev_info(p2p, addr, dev, msg);
2188
2189 return dev;
2190}
2191
2192
2193static int dev_type_match(const u8 *dev_type, const u8 *req_dev_type)
2194{
2195 if (os_memcmp(dev_type, req_dev_type, WPS_DEV_TYPE_LEN) == 0)
2196 return 1;
2197 if (os_memcmp(dev_type, req_dev_type, 2) == 0 &&
2198 WPA_GET_BE32(&req_dev_type[2]) == 0 &&
2199 WPA_GET_BE16(&req_dev_type[6]) == 0)
2200 return 1; /* Category match with wildcard OUI/sub-category */
2201 return 0;
2202}
2203
2204
2205int dev_type_list_match(const u8 *dev_type, const u8 *req_dev_type[],
2206 size_t num_req_dev_type)
2207{
2208 size_t i;
2209 for (i = 0; i < num_req_dev_type; i++) {
2210 if (dev_type_match(dev_type, req_dev_type[i]))
2211 return 1;
2212 }
2213 return 0;
2214}
2215
2216
2217/**
2218 * p2p_match_dev_type - Match local device type with requested type
2219 * @p2p: P2P module context from p2p_init()
2220 * @wps: WPS TLVs from Probe Request frame (concatenated WPS IEs)
2221 * Returns: 1 on match, 0 on mismatch
2222 *
2223 * This function can be used to match the Requested Device Type attribute in
2224 * WPS IE with the local device types for deciding whether to reply to a Probe
2225 * Request frame.
2226 */
2227int p2p_match_dev_type(struct p2p_data *p2p, struct wpabuf *wps)
2228{
2229 struct wps_parse_attr attr;
2230 size_t i;
2231
2232 if (wps_parse_msg(wps, &attr))
2233 return 1; /* assume no Requested Device Type attributes */
2234
2235 if (attr.num_req_dev_type == 0)
2236 return 1; /* no Requested Device Type attributes -> match */
2237
2238 if (dev_type_list_match(p2p->cfg->pri_dev_type, attr.req_dev_type,
2239 attr.num_req_dev_type))
2240 return 1; /* Own Primary Device Type matches */
2241
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002242 for (i = 0; i < p2p->cfg->num_sec_dev_types; i++) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002243 if (dev_type_list_match(p2p->cfg->sec_dev_type[i],
2244 attr.req_dev_type,
2245 attr.num_req_dev_type))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002246 return 1; /* Own Secondary Device Type matches */
2247 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002248
2249 /* No matching device type found */
2250 return 0;
2251}
2252
2253
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002254struct wpabuf * p2p_build_probe_resp_ies(struct p2p_data *p2p,
2255 const u8 *query_hash,
2256 u8 query_count)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002257{
2258 struct wpabuf *buf;
2259 u8 *len;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002260 int pw_id = -1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002261 size_t extra = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002262
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002263#ifdef CONFIG_WIFI_DISPLAY
2264 if (p2p->wfd_ie_probe_resp)
2265 extra = wpabuf_len(p2p->wfd_ie_probe_resp);
2266#endif /* CONFIG_WIFI_DISPLAY */
2267
Dmitry Shmidt2e67f062014-07-16 09:55:28 -07002268 if (p2p->vendor_elem && p2p->vendor_elem[VENDOR_ELEM_PROBE_RESP_P2P])
2269 extra += wpabuf_len(p2p->vendor_elem[VENDOR_ELEM_PROBE_RESP_P2P]);
2270
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002271 if (query_count)
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002272 extra += MAX_SVC_ADV_IE_LEN;
2273
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002274 buf = wpabuf_alloc(1000 + extra);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002275 if (buf == NULL)
2276 return NULL;
2277
Dmitry Shmidt04949592012-07-19 12:16:46 -07002278 if (p2p->go_neg_peer) {
2279 /* Advertise immediate availability of WPS credential */
2280 pw_id = p2p_wps_method_pw_id(p2p->go_neg_peer->wps_method);
2281 }
2282
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002283 if (p2p_build_wps_ie(p2p, buf, pw_id, 1) < 0) {
2284 p2p_dbg(p2p, "Failed to build WPS IE for Probe Response");
2285 wpabuf_free(buf);
2286 return NULL;
2287 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002288
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002289#ifdef CONFIG_WIFI_DISPLAY
2290 if (p2p->wfd_ie_probe_resp)
2291 wpabuf_put_buf(buf, p2p->wfd_ie_probe_resp);
2292#endif /* CONFIG_WIFI_DISPLAY */
2293
Dmitry Shmidt2e67f062014-07-16 09:55:28 -07002294 if (p2p->vendor_elem && p2p->vendor_elem[VENDOR_ELEM_PROBE_RESP_P2P])
2295 wpabuf_put_buf(buf,
2296 p2p->vendor_elem[VENDOR_ELEM_PROBE_RESP_P2P]);
2297
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002298 /* P2P IE */
2299 len = p2p_buf_add_ie_hdr(buf);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002300 p2p_buf_add_capability(buf, p2p->dev_capab &
2301 ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002302 if (p2p->ext_listen_interval)
2303 p2p_buf_add_ext_listen_timing(buf, p2p->ext_listen_period,
2304 p2p->ext_listen_interval);
2305 p2p_buf_add_device_info(buf, p2p, NULL);
2306 p2p_buf_update_ie_hdr(buf, len);
2307
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002308 if (query_count) {
2309 p2p_buf_add_service_instance(buf, p2p, query_count, query_hash,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002310 p2p->p2ps_adv_list);
2311 }
2312
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002313 return buf;
2314}
2315
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07002316static int p2p_build_probe_resp_buf(struct p2p_data *p2p, struct wpabuf *buf,
2317 struct wpabuf *ies,
2318 const u8 *addr, int rx_freq)
2319{
2320 struct ieee80211_mgmt *resp;
2321 u8 channel, op_class;
2322
2323 resp = wpabuf_put(buf, offsetof(struct ieee80211_mgmt,
2324 u.probe_resp.variable));
2325
2326 resp->frame_control = host_to_le16((WLAN_FC_TYPE_MGMT << 2) |
2327 (WLAN_FC_STYPE_PROBE_RESP << 4));
2328 os_memcpy(resp->da, addr, ETH_ALEN);
2329 os_memcpy(resp->sa, p2p->cfg->dev_addr, ETH_ALEN);
2330 os_memcpy(resp->bssid, p2p->cfg->dev_addr, ETH_ALEN);
2331 resp->u.probe_resp.beacon_int = host_to_le16(100);
2332 /* hardware or low-level driver will setup seq_ctrl and timestamp */
2333 resp->u.probe_resp.capab_info =
2334 host_to_le16(WLAN_CAPABILITY_SHORT_PREAMBLE |
2335 WLAN_CAPABILITY_PRIVACY |
2336 WLAN_CAPABILITY_SHORT_SLOT_TIME);
2337
2338 wpabuf_put_u8(buf, WLAN_EID_SSID);
2339 wpabuf_put_u8(buf, P2P_WILDCARD_SSID_LEN);
2340 wpabuf_put_data(buf, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN);
2341
2342 wpabuf_put_u8(buf, WLAN_EID_SUPP_RATES);
2343 wpabuf_put_u8(buf, 8);
2344 wpabuf_put_u8(buf, (60 / 5) | 0x80);
2345 wpabuf_put_u8(buf, 90 / 5);
2346 wpabuf_put_u8(buf, (120 / 5) | 0x80);
2347 wpabuf_put_u8(buf, 180 / 5);
2348 wpabuf_put_u8(buf, (240 / 5) | 0x80);
2349 wpabuf_put_u8(buf, 360 / 5);
2350 wpabuf_put_u8(buf, 480 / 5);
2351 wpabuf_put_u8(buf, 540 / 5);
2352
2353 if (!rx_freq) {
2354 channel = p2p->cfg->channel;
2355 } else if (p2p_freq_to_channel(rx_freq, &op_class, &channel)) {
2356 p2p_err(p2p, "Failed to convert freq to channel");
2357 return -1;
2358 }
2359
2360 wpabuf_put_u8(buf, WLAN_EID_DS_PARAMS);
2361 wpabuf_put_u8(buf, 1);
2362 wpabuf_put_u8(buf, channel);
2363
2364 wpabuf_put_buf(buf, ies);
2365
2366 return 0;
2367}
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002368
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002369static int p2p_service_find_asp(struct p2p_data *p2p, const u8 *hash)
2370{
2371 struct p2ps_advertisement *adv_data;
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002372 int any_wfa;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002373
2374 p2p_dbg(p2p, "ASP find - ASP list: %p", p2p->p2ps_adv_list);
2375
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002376 /* Wildcard org.wi-fi.wfds matches any WFA spec defined service */
2377 any_wfa = os_memcmp(hash, p2p->wild_card_hash, P2PS_HASH_LEN) == 0;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002378
2379 adv_data = p2p->p2ps_adv_list;
2380 while (adv_data) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002381 if (os_memcmp(hash, adv_data->hash, P2PS_HASH_LEN) == 0)
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002382 return 1; /* exact hash match */
2383 if (any_wfa &&
2384 os_strncmp(adv_data->svc_name, P2PS_WILD_HASH_STR,
2385 os_strlen(P2PS_WILD_HASH_STR)) == 0)
2386 return 1; /* WFA service match */
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002387 adv_data = adv_data->next;
2388 }
2389
2390 return 0;
2391}
2392
2393
Dmitry Shmidt04949592012-07-19 12:16:46 -07002394static enum p2p_probe_req_status
2395p2p_reply_probe(struct p2p_data *p2p, const u8 *addr, const u8 *dst,
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002396 const u8 *bssid, const u8 *ie, size_t ie_len,
2397 unsigned int rx_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002398{
2399 struct ieee802_11_elems elems;
2400 struct wpabuf *buf;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002401 struct p2p_message msg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002402 struct wpabuf *ies;
2403
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002404 if (ieee802_11_parse_elems((u8 *) ie, ie_len, &elems, 0) ==
2405 ParseFailed) {
2406 /* Ignore invalid Probe Request frames */
Dmitry Shmidtec58b162014-02-19 12:44:18 -08002407 p2p_dbg(p2p, "Could not parse Probe Request frame - ignore it");
Dmitry Shmidt04949592012-07-19 12:16:46 -07002408 return P2P_PREQ_MALFORMED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002409 }
2410
2411 if (elems.p2p == NULL) {
2412 /* not a P2P probe - ignore it */
Dmitry Shmidtec58b162014-02-19 12:44:18 -08002413 p2p_dbg(p2p, "Not a P2P probe - ignore it");
Dmitry Shmidt04949592012-07-19 12:16:46 -07002414 return P2P_PREQ_NOT_P2P;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002415 }
2416
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002417 if (dst && !is_broadcast_ether_addr(dst) &&
Sunil Ravib0ac25f2024-07-12 01:42:03 +00002418 !ether_addr_equal(dst, p2p->cfg->dev_addr)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002419 /* Not sent to the broadcast address or our P2P Device Address
2420 */
Dmitry Shmidtec58b162014-02-19 12:44:18 -08002421 p2p_dbg(p2p, "Probe Req DA " MACSTR " not ours - ignore it",
2422 MAC2STR(dst));
Dmitry Shmidt04949592012-07-19 12:16:46 -07002423 return P2P_PREQ_NOT_PROCESSED;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002424 }
2425
2426 if (bssid && !is_broadcast_ether_addr(bssid)) {
2427 /* Not sent to the Wildcard BSSID */
Dmitry Shmidtec58b162014-02-19 12:44:18 -08002428 p2p_dbg(p2p, "Probe Req BSSID " MACSTR " not wildcard - ignore it",
2429 MAC2STR(bssid));
Dmitry Shmidt04949592012-07-19 12:16:46 -07002430 return P2P_PREQ_NOT_PROCESSED;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002431 }
2432
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002433 if (elems.ssid == NULL || elems.ssid_len != P2P_WILDCARD_SSID_LEN ||
2434 os_memcmp(elems.ssid, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN) !=
2435 0) {
2436 /* not using P2P Wildcard SSID - ignore */
Dmitry Shmidtec58b162014-02-19 12:44:18 -08002437 p2p_dbg(p2p, "Probe Req not using P2P Wildcard SSID - ignore it");
Dmitry Shmidt04949592012-07-19 12:16:46 -07002438 return P2P_PREQ_NOT_PROCESSED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002439 }
2440
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002441 if (supp_rates_11b_only(&elems)) {
2442 /* Indicates support for 11b rates only */
Dmitry Shmidtec58b162014-02-19 12:44:18 -08002443 p2p_dbg(p2p, "Probe Req with 11b rates only supported - ignore it");
Dmitry Shmidt04949592012-07-19 12:16:46 -07002444 return P2P_PREQ_NOT_P2P;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002445 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002446
2447 os_memset(&msg, 0, sizeof(msg));
2448 if (p2p_parse_ies(ie, ie_len, &msg) < 0) {
2449 /* Could not parse P2P attributes */
Dmitry Shmidtec58b162014-02-19 12:44:18 -08002450 p2p_dbg(p2p, "Could not parse P2P attributes in Probe Req - ignore it");
Dmitry Shmidt04949592012-07-19 12:16:46 -07002451 return P2P_PREQ_NOT_P2P;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002452 }
2453
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002454 if (msg.service_hash && msg.service_hash_count) {
2455 const u8 *hash = msg.service_hash;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002456 u8 i;
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002457 int p2ps_svc_found = 0;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002458
Dmitry Shmidt41712582015-06-29 11:02:15 -07002459 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",
2460 p2p->in_listen, p2p->drv_in_listen, rx_freq,
2461 p2p->cfg->channel, p2p->pending_listen_freq);
2462
2463 if (!p2p->in_listen && !p2p->drv_in_listen &&
2464 p2p->pending_listen_freq && rx_freq &&
2465 rx_freq != p2p->pending_listen_freq) {
2466 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",
2467 rx_freq, p2p->pending_listen_freq);
2468 p2p_parse_free(&msg);
2469 return P2P_PREQ_NOT_LISTEN;
2470 }
2471
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002472 for (i = 0; i < msg.service_hash_count; i++) {
2473 if (p2p_service_find_asp(p2p, hash)) {
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002474 p2p_dbg(p2p, "Service Hash match found: "
2475 MACSTR, MAC2STR(hash));
2476 p2ps_svc_found = 1;
2477 break;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002478 }
2479 hash += P2PS_HASH_LEN;
2480 }
2481
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002482 /* Probed hash unknown */
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002483 if (!p2ps_svc_found) {
2484 p2p_dbg(p2p, "No Service Hash match found");
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002485 p2p_parse_free(&msg);
2486 return P2P_PREQ_NOT_PROCESSED;
2487 }
2488 } else {
2489 /* This is not a P2PS Probe Request */
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002490 p2p_dbg(p2p, "No P2PS Hash in Probe Request");
2491
2492 if (!p2p->in_listen || !p2p->drv_in_listen) {
2493 /* not in Listen state - ignore Probe Request */
2494 p2p_dbg(p2p, "Not in Listen state (in_listen=%d drv_in_listen=%d) - ignore Probe Request",
2495 p2p->in_listen, p2p->drv_in_listen);
2496 p2p_parse_free(&msg);
2497 return P2P_PREQ_NOT_LISTEN;
2498 }
2499 }
2500
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002501 if (msg.device_id &&
Sunil Ravib0ac25f2024-07-12 01:42:03 +00002502 !ether_addr_equal(msg.device_id, p2p->cfg->dev_addr)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002503 /* Device ID did not match */
Dmitry Shmidtec58b162014-02-19 12:44:18 -08002504 p2p_dbg(p2p, "Probe Req requested Device ID " MACSTR " did not match - ignore it",
2505 MAC2STR(msg.device_id));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002506 p2p_parse_free(&msg);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002507 return P2P_PREQ_NOT_PROCESSED;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002508 }
2509
2510 /* Check Requested Device Type match */
2511 if (msg.wps_attributes &&
2512 !p2p_match_dev_type(p2p, msg.wps_attributes)) {
2513 /* No match with Requested Device Type */
Hai Shalom021b0b52019-04-10 11:17:58 -07002514 p2p_dbg(p2p, "Probe Req requested Device Type did not match - ignore it");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002515 p2p_parse_free(&msg);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002516 return P2P_PREQ_NOT_PROCESSED;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002517 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002518
Dmitry Shmidt04949592012-07-19 12:16:46 -07002519 if (!p2p->cfg->send_probe_resp) {
2520 /* Response generated elsewhere */
Dmitry Shmidtec58b162014-02-19 12:44:18 -08002521 p2p_dbg(p2p, "Probe Resp generated elsewhere - do not generate additional response");
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002522 p2p_parse_free(&msg);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002523 return P2P_PREQ_NOT_PROCESSED;
2524 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002525
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002526 p2p_dbg(p2p, "Reply to P2P Probe Request in Listen state");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002527
2528 /*
2529 * We do not really have a specific BSS that this frame is advertising,
2530 * so build a frame that has some information in valid format. This is
2531 * really only used for discovery purposes, not to learn exact BSS
2532 * parameters.
2533 */
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002534 ies = p2p_build_probe_resp_ies(p2p, msg.service_hash,
2535 msg.service_hash_count);
2536 p2p_parse_free(&msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002537 if (ies == NULL)
Dmitry Shmidt04949592012-07-19 12:16:46 -07002538 return P2P_PREQ_NOT_PROCESSED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002539
2540 buf = wpabuf_alloc(200 + wpabuf_len(ies));
2541 if (buf == NULL) {
2542 wpabuf_free(ies);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002543 return P2P_PREQ_NOT_PROCESSED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002544 }
2545
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07002546 if (p2p_build_probe_resp_buf(p2p, buf, ies, addr, rx_freq)) {
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002547 wpabuf_free(ies);
2548 wpabuf_free(buf);
2549 return P2P_PREQ_NOT_PROCESSED;
2550 }
2551
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002552 wpabuf_free(ies);
2553
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002554 p2p->cfg->send_probe_resp(p2p->cfg->cb_ctx, buf, rx_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002555
2556 wpabuf_free(buf);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002557
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002558 return P2P_PREQ_PROCESSED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002559}
2560
2561
Dmitry Shmidt04949592012-07-19 12:16:46 -07002562enum p2p_probe_req_status
2563p2p_probe_req_rx(struct p2p_data *p2p, const u8 *addr, const u8 *dst,
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002564 const u8 *bssid, const u8 *ie, size_t ie_len,
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07002565 unsigned int rx_freq, int p2p_lo_started)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002566{
Dmitry Shmidt04949592012-07-19 12:16:46 -07002567 enum p2p_probe_req_status res;
2568
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002569 p2p_add_dev_from_probe_req(p2p, addr, ie, ie_len);
2570
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07002571 if (p2p_lo_started) {
2572 p2p_dbg(p2p,
2573 "Probe Response is offloaded, do not reply Probe Request");
2574 return P2P_PREQ_PROCESSED;
2575 }
2576
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002577 res = p2p_reply_probe(p2p, addr, dst, bssid, ie, ie_len, rx_freq);
2578 if (res != P2P_PREQ_PROCESSED && res != P2P_PREQ_NOT_PROCESSED)
2579 return res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002580
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002581 /*
2582 * Activate a pending GO Negotiation/Invite flow if a received Probe
2583 * Request frame is from an expected peer. Some devices may share the
2584 * same address for P2P and non-P2P STA running simultaneously. The
2585 * P2P_PREQ_PROCESSED and P2P_PREQ_NOT_PROCESSED p2p_reply_probe()
2586 * return values verified above ensure we are handling a Probe Request
2587 * frame from a P2P peer.
2588 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002589 if ((p2p->state == P2P_CONNECT || p2p->state == P2P_CONNECT_LISTEN) &&
2590 p2p->go_neg_peer &&
Sunil Ravib0ac25f2024-07-12 01:42:03 +00002591 ether_addr_equal(addr, p2p->go_neg_peer->info.p2p_device_addr) &&
Dmitry Shmidt9cdf1b92013-02-27 12:58:50 -08002592 !(p2p->go_neg_peer->flags & P2P_DEV_WAIT_GO_NEG_CONFIRM)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002593 /* Received a Probe Request from GO Negotiation peer */
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002594 p2p_dbg(p2p, "Found GO Negotiation peer - try to start GO negotiation from timeout");
Dmitry Shmidt9cdf1b92013-02-27 12:58:50 -08002595 eloop_cancel_timeout(p2p_go_neg_start, p2p, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002596 eloop_register_timeout(0, 0, p2p_go_neg_start, p2p, NULL);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002597 return res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002598 }
2599
2600 if ((p2p->state == P2P_INVITE || p2p->state == P2P_INVITE_LISTEN) &&
2601 p2p->invite_peer &&
Dmitry Shmidt3c479372014-02-04 10:50:36 -08002602 (p2p->invite_peer->flags & P2P_DEV_WAIT_INV_REQ_ACK) &&
Sunil Ravib0ac25f2024-07-12 01:42:03 +00002603 ether_addr_equal(addr, p2p->invite_peer->info.p2p_device_addr)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002604 /* Received a Probe Request from Invite peer */
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002605 p2p_dbg(p2p, "Found Invite peer - try to start Invite from timeout");
Dmitry Shmidt7f93d6f2014-02-21 11:22:49 -08002606 eloop_cancel_timeout(p2p_invite_start, p2p, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002607 eloop_register_timeout(0, 0, p2p_invite_start, p2p, NULL);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002608 return res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002609 }
2610
Dmitry Shmidt04949592012-07-19 12:16:46 -07002611 return res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002612}
2613
2614
2615static int p2p_assoc_req_ie_wlan_ap(struct p2p_data *p2p, const u8 *bssid,
2616 u8 *buf, size_t len, struct wpabuf *p2p_ie)
2617{
2618 struct wpabuf *tmp;
2619 u8 *lpos;
2620 size_t tmplen;
2621 int res;
2622 u8 group_capab;
Dmitry Shmidt05df46a2015-05-19 11:02:01 -07002623 struct p2p_message msg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002624
2625 if (p2p_ie == NULL)
2626 return 0; /* WLAN AP is not a P2P manager */
2627
Dmitry Shmidt05df46a2015-05-19 11:02:01 -07002628 os_memset(&msg, 0, sizeof(msg));
2629 if (p2p_parse_p2p_ie(p2p_ie, &msg) < 0)
2630 return 0;
2631
2632 p2p_dbg(p2p, "BSS P2P manageability %s",
2633 msg.manageability ? "enabled" : "disabled");
2634
2635 if (!msg.manageability)
2636 return 0;
2637
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002638 /*
2639 * (Re)Association Request - P2P IE
2640 * P2P Capability attribute (shall be present)
2641 * P2P Interface attribute (present if concurrent device and
2642 * P2P Management is enabled)
2643 */
2644 tmp = wpabuf_alloc(200);
2645 if (tmp == NULL)
2646 return -1;
2647
2648 lpos = p2p_buf_add_ie_hdr(tmp);
2649 group_capab = 0;
2650 if (p2p->num_groups > 0) {
2651 group_capab |= P2P_GROUP_CAPAB_GROUP_OWNER;
2652 if ((p2p->dev_capab & P2P_DEV_CAPAB_CONCURRENT_OPER) &&
2653 (p2p->dev_capab & P2P_DEV_CAPAB_INFRA_MANAGED) &&
2654 p2p->cross_connect)
2655 group_capab |= P2P_GROUP_CAPAB_CROSS_CONN;
2656 }
2657 p2p_buf_add_capability(tmp, p2p->dev_capab, group_capab);
2658 if ((p2p->dev_capab & P2P_DEV_CAPAB_CONCURRENT_OPER) &&
2659 (p2p->dev_capab & P2P_DEV_CAPAB_INFRA_MANAGED))
2660 p2p_buf_add_p2p_interface(tmp, p2p);
2661 p2p_buf_update_ie_hdr(tmp, lpos);
2662
2663 tmplen = wpabuf_len(tmp);
2664 if (tmplen > len)
2665 res = -1;
2666 else {
2667 os_memcpy(buf, wpabuf_head(tmp), tmplen);
2668 res = tmplen;
2669 }
2670 wpabuf_free(tmp);
2671
2672 return res;
2673}
2674
2675
2676int p2p_assoc_req_ie(struct p2p_data *p2p, const u8 *bssid, u8 *buf,
2677 size_t len, int p2p_group, struct wpabuf *p2p_ie)
2678{
2679 struct wpabuf *tmp;
2680 u8 *lpos;
2681 struct p2p_device *peer;
2682 size_t tmplen;
2683 int res;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002684 size_t extra = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002685
2686 if (!p2p_group)
2687 return p2p_assoc_req_ie_wlan_ap(p2p, bssid, buf, len, p2p_ie);
2688
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002689#ifdef CONFIG_WIFI_DISPLAY
2690 if (p2p->wfd_ie_assoc_req)
2691 extra = wpabuf_len(p2p->wfd_ie_assoc_req);
2692#endif /* CONFIG_WIFI_DISPLAY */
2693
Dmitry Shmidt2e67f062014-07-16 09:55:28 -07002694 if (p2p->vendor_elem && p2p->vendor_elem[VENDOR_ELEM_P2P_ASSOC_REQ])
2695 extra += wpabuf_len(p2p->vendor_elem[VENDOR_ELEM_P2P_ASSOC_REQ]);
2696
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002697 /*
2698 * (Re)Association Request - P2P IE
2699 * P2P Capability attribute (shall be present)
2700 * Extended Listen Timing (may be present)
2701 * P2P Device Info attribute (shall be present)
2702 */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002703 tmp = wpabuf_alloc(200 + extra);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002704 if (tmp == NULL)
2705 return -1;
2706
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002707#ifdef CONFIG_WIFI_DISPLAY
2708 if (p2p->wfd_ie_assoc_req)
2709 wpabuf_put_buf(tmp, p2p->wfd_ie_assoc_req);
2710#endif /* CONFIG_WIFI_DISPLAY */
2711
Dmitry Shmidt2e67f062014-07-16 09:55:28 -07002712 if (p2p->vendor_elem && p2p->vendor_elem[VENDOR_ELEM_P2P_ASSOC_REQ])
2713 wpabuf_put_buf(tmp,
2714 p2p->vendor_elem[VENDOR_ELEM_P2P_ASSOC_REQ]);
2715
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002716 peer = bssid ? p2p_get_device(p2p, bssid) : NULL;
2717
2718 lpos = p2p_buf_add_ie_hdr(tmp);
2719 p2p_buf_add_capability(tmp, p2p->dev_capab, 0);
2720 if (p2p->ext_listen_interval)
2721 p2p_buf_add_ext_listen_timing(tmp, p2p->ext_listen_period,
2722 p2p->ext_listen_interval);
2723 p2p_buf_add_device_info(tmp, p2p, peer);
2724 p2p_buf_update_ie_hdr(tmp, lpos);
2725
2726 tmplen = wpabuf_len(tmp);
2727 if (tmplen > len)
2728 res = -1;
2729 else {
2730 os_memcpy(buf, wpabuf_head(tmp), tmplen);
2731 res = tmplen;
2732 }
2733 wpabuf_free(tmp);
2734
2735 return res;
2736}
2737
2738
2739int p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf, char *end)
2740{
2741 struct wpabuf *p2p_ie;
2742 int ret;
2743
2744 p2p_ie = ieee802_11_vendor_ie_concat(ies, ies_len, P2P_IE_VENDOR_TYPE);
2745 if (p2p_ie == NULL)
2746 return 0;
2747
2748 ret = p2p_attr_text(p2p_ie, buf, end);
2749 wpabuf_free(p2p_ie);
2750 return ret;
2751}
2752
2753
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002754struct p2ps_advertisement *
2755p2p_service_p2ps_id(struct p2p_data *p2p, u32 adv_id)
2756{
2757 struct p2ps_advertisement *adv_data;
2758
2759 if (!p2p)
2760 return NULL;
2761
2762 adv_data = p2p->p2ps_adv_list;
2763 while (adv_data) {
2764 if (adv_data->id == adv_id)
2765 return adv_data;
2766 adv_data = adv_data->next;
2767 }
2768
2769 return NULL;
2770}
2771
2772
2773int p2p_service_del_asp(struct p2p_data *p2p, u32 adv_id)
2774{
2775 struct p2ps_advertisement *adv_data;
2776 struct p2ps_advertisement **prior;
2777
2778 if (!p2p)
2779 return -1;
2780
2781 adv_data = p2p->p2ps_adv_list;
2782 prior = &p2p->p2ps_adv_list;
2783 while (adv_data) {
2784 if (adv_data->id == adv_id) {
2785 p2p_dbg(p2p, "Delete ASP adv_id=0x%x", adv_id);
2786 *prior = adv_data->next;
2787 os_free(adv_data);
2788 return 0;
2789 }
2790 prior = &adv_data->next;
2791 adv_data = adv_data->next;
2792 }
2793
2794 return -1;
2795}
2796
2797
2798int p2p_service_add_asp(struct p2p_data *p2p, int auto_accept, u32 adv_id,
2799 const char *adv_str, u8 svc_state, u16 config_methods,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002800 const char *svc_info, const u8 *cpt_priority)
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002801{
2802 struct p2ps_advertisement *adv_data, *tmp, **prev;
2803 u8 buf[P2PS_HASH_LEN];
2804 size_t adv_data_len, adv_len, info_len = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002805 int i;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002806
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002807 if (!p2p || !adv_str || !adv_str[0] || !cpt_priority)
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002808 return -1;
2809
2810 if (!(config_methods & p2p->cfg->config_methods)) {
2811 p2p_dbg(p2p, "Config methods not supported svc: 0x%x dev: 0x%x",
2812 config_methods, p2p->cfg->config_methods);
2813 return -1;
2814 }
2815
2816 if (!p2ps_gen_hash(p2p, adv_str, buf))
2817 return -1;
2818
2819 if (svc_info)
2820 info_len = os_strlen(svc_info);
2821 adv_len = os_strlen(adv_str);
2822 adv_data_len = sizeof(struct p2ps_advertisement) + adv_len + 1 +
2823 info_len + 1;
2824
2825 adv_data = os_zalloc(adv_data_len);
2826 if (!adv_data)
2827 return -1;
2828
2829 os_memcpy(adv_data->hash, buf, P2PS_HASH_LEN);
2830 adv_data->id = adv_id;
2831 adv_data->state = svc_state;
2832 adv_data->config_methods = config_methods & p2p->cfg->config_methods;
2833 adv_data->auto_accept = (u8) auto_accept;
2834 os_memcpy(adv_data->svc_name, adv_str, adv_len);
2835
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002836 for (i = 0; cpt_priority[i] && i < P2PS_FEATURE_CAPAB_CPT_MAX; i++) {
2837 adv_data->cpt_priority[i] = cpt_priority[i];
2838 adv_data->cpt_mask |= cpt_priority[i];
2839 }
2840
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002841 if (svc_info && info_len) {
2842 adv_data->svc_info = &adv_data->svc_name[adv_len + 1];
2843 os_memcpy(adv_data->svc_info, svc_info, info_len);
2844 }
2845
2846 /*
2847 * Group Advertisements by service string. They do not need to be
2848 * sorted, but groups allow easier Probe Response instance grouping
2849 */
2850 tmp = p2p->p2ps_adv_list;
2851 prev = &p2p->p2ps_adv_list;
2852 while (tmp) {
2853 if (tmp->id == adv_data->id) {
2854 if (os_strcmp(tmp->svc_name, adv_data->svc_name) != 0) {
2855 os_free(adv_data);
2856 return -1;
2857 }
2858 adv_data->next = tmp->next;
2859 *prev = adv_data;
2860 os_free(tmp);
2861 goto inserted;
2862 } else {
2863 if (os_strcmp(tmp->svc_name, adv_data->svc_name) == 0) {
2864 adv_data->next = tmp->next;
2865 tmp->next = adv_data;
2866 goto inserted;
2867 }
2868 }
2869 prev = &tmp->next;
2870 tmp = tmp->next;
2871 }
2872
2873 /* No svc_name match found */
2874 adv_data->next = p2p->p2ps_adv_list;
2875 p2p->p2ps_adv_list = adv_data;
2876
2877inserted:
2878 p2p_dbg(p2p,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002879 "Added ASP advertisement adv_id=0x%x config_methods=0x%x svc_state=0x%x adv_str='%s' cpt_mask=0x%x",
2880 adv_id, adv_data->config_methods, svc_state, adv_str,
2881 adv_data->cpt_mask);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002882
2883 return 0;
2884}
2885
2886
Dmitry Shmidtdda10c22015-03-24 16:05:01 -07002887void p2p_service_flush_asp(struct p2p_data *p2p)
2888{
2889 struct p2ps_advertisement *adv, *prev;
2890
2891 if (!p2p)
2892 return;
2893
2894 adv = p2p->p2ps_adv_list;
2895 while (adv) {
2896 prev = adv;
2897 adv = adv->next;
2898 os_free(prev);
2899 }
2900
2901 p2p->p2ps_adv_list = NULL;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002902 p2ps_prov_free(p2p);
Dmitry Shmidtdda10c22015-03-24 16:05:01 -07002903 p2p_dbg(p2p, "All ASP advertisements flushed");
2904}
2905
2906
Dmitry Shmidt04949592012-07-19 12:16:46 -07002907int p2p_parse_dev_addr_in_p2p_ie(struct wpabuf *p2p_ie, u8 *dev_addr)
2908{
2909 struct p2p_message msg;
2910
2911 os_memset(&msg, 0, sizeof(msg));
2912 if (p2p_parse_p2p_ie(p2p_ie, &msg))
2913 return -1;
2914
2915 if (msg.p2p_device_addr) {
2916 os_memcpy(dev_addr, msg.p2p_device_addr, ETH_ALEN);
2917 return 0;
2918 } else if (msg.device_id) {
2919 os_memcpy(dev_addr, msg.device_id, ETH_ALEN);
2920 return 0;
2921 }
2922 return -1;
2923}
2924
2925
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002926int p2p_parse_dev_addr(const u8 *ies, size_t ies_len, u8 *dev_addr)
2927{
2928 struct wpabuf *p2p_ie;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002929 int ret;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002930
2931 p2p_ie = ieee802_11_vendor_ie_concat(ies, ies_len,
2932 P2P_IE_VENDOR_TYPE);
2933 if (p2p_ie == NULL)
2934 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002935 ret = p2p_parse_dev_addr_in_p2p_ie(p2p_ie, dev_addr);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002936 wpabuf_free(p2p_ie);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002937 return ret;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002938}
2939
2940
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002941static void p2p_clear_go_neg(struct p2p_data *p2p)
2942{
2943 p2p->go_neg_peer = NULL;
2944 p2p_clear_timeout(p2p);
2945 p2p_set_state(p2p, P2P_IDLE);
2946}
2947
2948
2949void p2p_wps_success_cb(struct p2p_data *p2p, const u8 *mac_addr)
2950{
2951 if (p2p->go_neg_peer == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002952 p2p_dbg(p2p, "No pending Group Formation - ignore WPS registration success notification");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002953 return; /* No pending Group Formation */
2954 }
2955
Sunil Ravib0ac25f2024-07-12 01:42:03 +00002956 if (!ether_addr_equal(mac_addr, p2p->go_neg_peer->intended_addr)) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002957 p2p_dbg(p2p, "Ignore WPS registration success notification for "
2958 MACSTR " (GO Negotiation peer " MACSTR ")",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002959 MAC2STR(mac_addr),
2960 MAC2STR(p2p->go_neg_peer->intended_addr));
2961 return; /* Ignore unexpected peer address */
2962 }
2963
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002964 p2p_dbg(p2p, "Group Formation completed successfully with " MACSTR,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002965 MAC2STR(mac_addr));
2966
2967 p2p_clear_go_neg(p2p);
2968}
2969
2970
2971void p2p_group_formation_failed(struct p2p_data *p2p)
2972{
2973 if (p2p->go_neg_peer == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002974 p2p_dbg(p2p, "No pending Group Formation - ignore group formation failure notification");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002975 return; /* No pending Group Formation */
2976 }
2977
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002978 p2p_dbg(p2p, "Group Formation failed with " MACSTR,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002979 MAC2STR(p2p->go_neg_peer->intended_addr));
2980
2981 p2p_clear_go_neg(p2p);
2982}
2983
2984
Hai Shalom60840252021-02-19 19:02:11 -08002985bool is_p2p_6ghz_disabled(struct p2p_data *p2p)
2986{
2987 if (p2p)
2988 return p2p->cfg->p2p_6ghz_disable;
2989 return false;
2990}
2991
2992
Shuibing Daie2fad412023-05-05 14:08:11 -07002993bool is_p2p_dfs_chan_enabled(struct p2p_data *p2p)
2994{
2995 if (p2p)
2996 return p2p->cfg->p2p_dfs_chan_enable;
2997 return false;
2998}
2999
3000
Sunil Ravic0f5d412024-09-11 22:12:49 +00003001static void p2p_pairing_info_deinit(struct p2p_data *p2p)
3002{
3003#ifdef CONFIG_PASN
3004 pasn_initiator_pmksa_cache_deinit(p2p->initiator_pmksa);
3005 pasn_responder_pmksa_cache_deinit(p2p->responder_pmksa);
3006#endif /* CONFIG_PASN */
3007 os_free(p2p->pairing_info);
3008}
3009
3010
3011static int p2p_pairing_info_init(struct p2p_data *p2p)
3012{
3013 struct p2p_pairing_info *pairing_info;
3014
3015 if (p2p->cfg->pairing_config.dik_len > DEVICE_IDENTITY_KEY_MAX_LEN)
3016 return -1;
3017
3018 pairing_info = os_zalloc(sizeof(struct p2p_pairing_info));
3019 if (!pairing_info)
3020 return -1;
3021
3022 pairing_info->enable_pairing_setup =
3023 p2p->cfg->pairing_config.enable_pairing_setup;
3024 pairing_info->enable_pairing_cache =
3025 p2p->cfg->pairing_config.enable_pairing_cache;
3026 pairing_info->supported_bootstrap =
3027 p2p->cfg->pairing_config.bootstrap_methods;
3028
3029 pairing_info->dev_ik.cipher_version =
3030 p2p->cfg->pairing_config.dik_cipher;
3031 pairing_info->dev_ik.dik_len = p2p->cfg->pairing_config.dik_len;
3032 os_memcpy(pairing_info->dev_ik.dik_data,
3033 p2p->cfg->pairing_config.dik_data,
3034 p2p->cfg->pairing_config.dik_len);
3035
3036 p2p_pairing_info_deinit(p2p);
3037 p2p->pairing_info = pairing_info;
3038#ifdef CONFIG_PASN
3039 p2p->initiator_pmksa = pasn_initiator_pmksa_cache_init();
3040 p2p->responder_pmksa = pasn_responder_pmksa_cache_init();
3041#endif /* CONFIG_PASN */
3042
3043 return 0;
3044}
3045
3046
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003047struct p2p_data * p2p_init(const struct p2p_config *cfg)
3048{
3049 struct p2p_data *p2p;
3050
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07003051 if (cfg->max_peers < 1 ||
3052 cfg->passphrase_len < 8 || cfg->passphrase_len > 63)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003053 return NULL;
3054
3055 p2p = os_zalloc(sizeof(*p2p) + sizeof(*cfg));
3056 if (p2p == NULL)
3057 return NULL;
3058 p2p->cfg = (struct p2p_config *) (p2p + 1);
3059 os_memcpy(p2p->cfg, cfg, sizeof(*cfg));
3060 if (cfg->dev_name)
3061 p2p->cfg->dev_name = os_strdup(cfg->dev_name);
3062 if (cfg->manufacturer)
3063 p2p->cfg->manufacturer = os_strdup(cfg->manufacturer);
3064 if (cfg->model_name)
3065 p2p->cfg->model_name = os_strdup(cfg->model_name);
3066 if (cfg->model_number)
3067 p2p->cfg->model_number = os_strdup(cfg->model_number);
3068 if (cfg->serial_number)
3069 p2p->cfg->serial_number = os_strdup(cfg->serial_number);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003070 if (cfg->pref_chan) {
3071 p2p->cfg->pref_chan = os_malloc(cfg->num_pref_chan *
3072 sizeof(struct p2p_channel));
3073 if (p2p->cfg->pref_chan) {
3074 os_memcpy(p2p->cfg->pref_chan, cfg->pref_chan,
3075 cfg->num_pref_chan *
3076 sizeof(struct p2p_channel));
3077 } else
3078 p2p->cfg->num_pref_chan = 0;
3079 }
3080
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003081 p2ps_gen_hash(p2p, P2PS_WILD_HASH_STR, p2p->wild_card_hash);
3082
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003083 p2p->min_disc_int = 1;
3084 p2p->max_disc_int = 3;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003085 p2p->max_disc_tu = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003086
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07003087 if (os_get_random(&p2p->next_tie_breaker, 1) < 0)
3088 p2p->next_tie_breaker = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003089 p2p->next_tie_breaker &= 0x01;
3090 if (cfg->sd_request)
3091 p2p->dev_capab |= P2P_DEV_CAPAB_SERVICE_DISCOVERY;
3092 p2p->dev_capab |= P2P_DEV_CAPAB_INVITATION_PROCEDURE;
3093 if (cfg->concurrent_operations)
3094 p2p->dev_capab |= P2P_DEV_CAPAB_CONCURRENT_OPER;
3095 p2p->dev_capab |= P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
3096
3097 dl_list_init(&p2p->devices);
3098
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003099 p2p->go_timeout = 100;
3100 p2p->client_timeout = 20;
Dmitry Shmidt13ca8d82014-02-20 10:18:40 -08003101 p2p->num_p2p_sd_queries = 0;
Sunil Ravic0f5d412024-09-11 22:12:49 +00003102 /* Default comeback after one second */
3103 if (!p2p->cfg->comeback_after)
3104 p2p->cfg->comeback_after = 977; /* TUs */
3105 p2p_pairing_info_init(p2p);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003106
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003107 p2p_dbg(p2p, "initialized");
3108 p2p_channels_dump(p2p, "channels", &p2p->cfg->channels);
3109 p2p_channels_dump(p2p, "cli_channels", &p2p->cfg->cli_channels);
3110
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003111 return p2p;
3112}
3113
3114
3115void p2p_deinit(struct p2p_data *p2p)
3116{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003117#ifdef CONFIG_WIFI_DISPLAY
3118 wpabuf_free(p2p->wfd_ie_beacon);
3119 wpabuf_free(p2p->wfd_ie_probe_req);
3120 wpabuf_free(p2p->wfd_ie_probe_resp);
3121 wpabuf_free(p2p->wfd_ie_assoc_req);
3122 wpabuf_free(p2p->wfd_ie_invitation);
3123 wpabuf_free(p2p->wfd_ie_prov_disc_req);
3124 wpabuf_free(p2p->wfd_ie_prov_disc_resp);
3125 wpabuf_free(p2p->wfd_ie_go_neg);
3126 wpabuf_free(p2p->wfd_dev_info);
3127 wpabuf_free(p2p->wfd_assoc_bssid);
3128 wpabuf_free(p2p->wfd_coupled_sink_info);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003129 wpabuf_free(p2p->wfd_r2_dev_info);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003130#endif /* CONFIG_WIFI_DISPLAY */
3131
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003132 eloop_cancel_timeout(p2p_scan_timeout, p2p, NULL);
Dmitry Shmidt9cdf1b92013-02-27 12:58:50 -08003133 eloop_cancel_timeout(p2p_go_neg_start, p2p, NULL);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003134 eloop_cancel_timeout(p2p_go_neg_wait_timeout, p2p, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003135 p2p_flush(p2p);
3136 p2p_free_req_dev_types(p2p);
3137 os_free(p2p->cfg->dev_name);
3138 os_free(p2p->cfg->manufacturer);
3139 os_free(p2p->cfg->model_name);
3140 os_free(p2p->cfg->model_number);
3141 os_free(p2p->cfg->serial_number);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003142 os_free(p2p->cfg->pref_chan);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003143 os_free(p2p->groups);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07003144 p2ps_prov_free(p2p);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003145 wpabuf_free(p2p->sd_resp);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003146 p2p_remove_wps_vendor_extensions(p2p);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003147 os_free(p2p->no_go_freq.range);
Dmitry Shmidtdda10c22015-03-24 16:05:01 -07003148 p2p_service_flush_asp(p2p);
Sunil Ravic0f5d412024-09-11 22:12:49 +00003149 p2p_pairing_info_deinit(p2p);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003150
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003151 os_free(p2p);
3152}
3153
3154
3155void p2p_flush(struct p2p_data *p2p)
3156{
3157 struct p2p_device *dev, *prev;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003158
3159 p2p_ext_listen(p2p, 0, 0);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003160 p2p_stop_find(p2p);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003161 dl_list_for_each_safe(dev, prev, &p2p->devices, struct p2p_device,
3162 list) {
3163 dl_list_del(&dev->list);
3164 p2p_device_free(p2p, dev);
3165 }
3166 p2p_free_sd_queries(p2p);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003167 p2p->ssid_set = 0;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003168 p2ps_prov_free(p2p);
3169 p2p_reset_pending_pd(p2p);
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08003170 p2p->override_pref_op_class = 0;
3171 p2p->override_pref_channel = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003172}
3173
3174
3175int p2p_unauthorize(struct p2p_data *p2p, const u8 *addr)
3176{
3177 struct p2p_device *dev;
3178
3179 dev = p2p_get_device(p2p, addr);
3180 if (dev == NULL)
3181 return -1;
3182
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003183 p2p_dbg(p2p, "Unauthorizing " MACSTR, MAC2STR(addr));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003184
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003185 if (p2p->go_neg_peer == dev) {
3186 eloop_cancel_timeout(p2p_go_neg_wait_timeout, p2p, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003187 p2p->go_neg_peer = NULL;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003188 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003189
3190 dev->wps_method = WPS_NOT_READY;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003191 dev->oob_pw_id = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003192 dev->flags &= ~P2P_DEV_WAIT_GO_NEG_RESPONSE;
3193 dev->flags &= ~P2P_DEV_WAIT_GO_NEG_CONFIRM;
3194
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003195 return 0;
3196}
3197
3198
3199int p2p_set_dev_name(struct p2p_data *p2p, const char *dev_name)
3200{
3201 os_free(p2p->cfg->dev_name);
3202 if (dev_name) {
3203 p2p->cfg->dev_name = os_strdup(dev_name);
3204 if (p2p->cfg->dev_name == NULL)
3205 return -1;
3206 } else
3207 p2p->cfg->dev_name = NULL;
3208 return 0;
3209}
3210
3211
3212int p2p_set_manufacturer(struct p2p_data *p2p, const char *manufacturer)
3213{
3214 os_free(p2p->cfg->manufacturer);
3215 p2p->cfg->manufacturer = NULL;
3216 if (manufacturer) {
3217 p2p->cfg->manufacturer = os_strdup(manufacturer);
3218 if (p2p->cfg->manufacturer == NULL)
3219 return -1;
3220 }
3221
3222 return 0;
3223}
3224
3225
3226int p2p_set_model_name(struct p2p_data *p2p, const char *model_name)
3227{
3228 os_free(p2p->cfg->model_name);
3229 p2p->cfg->model_name = NULL;
3230 if (model_name) {
3231 p2p->cfg->model_name = os_strdup(model_name);
3232 if (p2p->cfg->model_name == NULL)
3233 return -1;
3234 }
3235
3236 return 0;
3237}
3238
3239
3240int p2p_set_model_number(struct p2p_data *p2p, const char *model_number)
3241{
3242 os_free(p2p->cfg->model_number);
3243 p2p->cfg->model_number = NULL;
3244 if (model_number) {
3245 p2p->cfg->model_number = os_strdup(model_number);
3246 if (p2p->cfg->model_number == NULL)
3247 return -1;
3248 }
3249
3250 return 0;
3251}
3252
3253
3254int p2p_set_serial_number(struct p2p_data *p2p, const char *serial_number)
3255{
3256 os_free(p2p->cfg->serial_number);
3257 p2p->cfg->serial_number = NULL;
3258 if (serial_number) {
3259 p2p->cfg->serial_number = os_strdup(serial_number);
3260 if (p2p->cfg->serial_number == NULL)
3261 return -1;
3262 }
3263
3264 return 0;
3265}
3266
3267
3268void p2p_set_config_methods(struct p2p_data *p2p, u16 config_methods)
3269{
3270 p2p->cfg->config_methods = config_methods;
3271}
3272
3273
3274void p2p_set_uuid(struct p2p_data *p2p, const u8 *uuid)
3275{
3276 os_memcpy(p2p->cfg->uuid, uuid, 16);
3277}
3278
3279
3280int p2p_set_pri_dev_type(struct p2p_data *p2p, const u8 *pri_dev_type)
3281{
3282 os_memcpy(p2p->cfg->pri_dev_type, pri_dev_type, 8);
3283 return 0;
3284}
3285
3286
3287int p2p_set_sec_dev_types(struct p2p_data *p2p, const u8 dev_types[][8],
3288 size_t num_dev_types)
3289{
3290 if (num_dev_types > P2P_SEC_DEVICE_TYPES)
3291 num_dev_types = P2P_SEC_DEVICE_TYPES;
3292 p2p->cfg->num_sec_dev_types = num_dev_types;
3293 os_memcpy(p2p->cfg->sec_dev_type, dev_types, num_dev_types * 8);
3294 return 0;
3295}
3296
3297
3298void p2p_remove_wps_vendor_extensions(struct p2p_data *p2p)
3299{
3300 int i;
3301
3302 for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) {
3303 wpabuf_free(p2p->wps_vendor_ext[i]);
3304 p2p->wps_vendor_ext[i] = NULL;
3305 }
3306}
3307
3308
3309int p2p_add_wps_vendor_extension(struct p2p_data *p2p,
3310 const struct wpabuf *vendor_ext)
3311{
3312 int i;
3313
3314 if (vendor_ext == NULL)
3315 return -1;
3316
3317 for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) {
3318 if (p2p->wps_vendor_ext[i] == NULL)
3319 break;
3320 }
3321 if (i >= P2P_MAX_WPS_VENDOR_EXT)
3322 return -1;
3323
3324 p2p->wps_vendor_ext[i] = wpabuf_dup(vendor_ext);
3325 if (p2p->wps_vendor_ext[i] == NULL)
3326 return -1;
3327
3328 return 0;
3329}
3330
3331
3332int p2p_set_country(struct p2p_data *p2p, const char *country)
3333{
3334 os_memcpy(p2p->cfg->country, country, 3);
3335 return 0;
3336}
3337
3338
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003339static int p2p_pre_find_operation(struct p2p_data *p2p, struct p2p_device *dev)
3340{
Dmitry Shmidtde47be72016-01-07 12:52:55 -08003341 int res;
3342
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003343 if (dev->sd_pending_bcast_queries == 0) {
3344 /* Initialize with total number of registered broadcast
3345 * SD queries. */
3346 dev->sd_pending_bcast_queries = p2p->num_p2p_sd_queries;
3347 }
3348
Dmitry Shmidtde47be72016-01-07 12:52:55 -08003349 res = p2p_start_sd(p2p, dev);
3350 if (res == -2)
3351 return -2;
3352 if (res == 0)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003353 return 1;
3354
3355 if (dev->req_config_methods &&
3356 !(dev->flags & P2P_DEV_PD_FOR_JOIN)) {
3357 p2p_dbg(p2p, "Send pending Provision Discovery Request to "
3358 MACSTR " (config methods 0x%x)",
3359 MAC2STR(dev->info.p2p_device_addr),
3360 dev->req_config_methods);
3361 if (p2p_send_prov_disc_req(p2p, dev, 0, 0) == 0)
3362 return 1;
3363 }
3364
3365 return 0;
3366}
3367
3368
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003369void p2p_continue_find(struct p2p_data *p2p)
3370{
3371 struct p2p_device *dev;
Dmitry Shmidtde47be72016-01-07 12:52:55 -08003372 int found, res;
Dmitry Shmidt13ca8d82014-02-20 10:18:40 -08003373
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003374 p2p_set_state(p2p, P2P_SEARCH);
3375
3376 /* Continue from the device following the last iteration */
3377 found = 0;
3378 dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
3379 if (dev == p2p->last_p2p_find_oper) {
3380 found = 1;
3381 continue;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003382 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003383 if (!found)
3384 continue;
Dmitry Shmidtde47be72016-01-07 12:52:55 -08003385 res = p2p_pre_find_operation(p2p, dev);
3386 if (res > 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003387 p2p->last_p2p_find_oper = dev;
3388 return;
3389 }
Dmitry Shmidtde47be72016-01-07 12:52:55 -08003390 if (res == -2)
3391 goto skip_sd;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003392 }
3393
3394 /*
3395 * Wrap around to the beginning of the list and continue until the last
3396 * iteration device.
3397 */
3398 dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
Dmitry Shmidtde47be72016-01-07 12:52:55 -08003399 res = p2p_pre_find_operation(p2p, dev);
3400 if (res > 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003401 p2p->last_p2p_find_oper = dev;
3402 return;
3403 }
Dmitry Shmidtde47be72016-01-07 12:52:55 -08003404 if (res == -2)
3405 goto skip_sd;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003406 if (dev == p2p->last_p2p_find_oper)
3407 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003408 }
3409
Dmitry Shmidtde47be72016-01-07 12:52:55 -08003410skip_sd:
3411 os_memset(p2p->sd_query_no_ack, 0, ETH_ALEN);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003412 p2p_listen_in_find(p2p, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003413}
3414
3415
Sunil Ravi7f769292024-07-23 22:21:32 +00003416void p2p_sd_query_cb(struct p2p_data *p2p, int success)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003417{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003418 p2p_dbg(p2p, "Service Discovery Query TX callback: success=%d",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003419 success);
3420 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
3421
3422 if (!success) {
Dmitry Shmidtde47be72016-01-07 12:52:55 -08003423 if (p2p->sd_peer) {
3424 if (is_zero_ether_addr(p2p->sd_query_no_ack)) {
3425 os_memcpy(p2p->sd_query_no_ack,
3426 p2p->sd_peer->info.p2p_device_addr,
3427 ETH_ALEN);
3428 p2p_dbg(p2p,
3429 "First SD Query no-ACK in this search iteration: "
3430 MACSTR, MAC2STR(p2p->sd_query_no_ack));
3431 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003432 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
Dmitry Shmidtde47be72016-01-07 12:52:55 -08003433 }
Dmitry Shmidt13ca8d82014-02-20 10:18:40 -08003434 p2p->sd_peer = NULL;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003435 if (p2p->state != P2P_IDLE)
3436 p2p_continue_find(p2p);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003437 return;
3438 }
3439
3440 if (p2p->sd_peer == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003441 p2p_dbg(p2p, "No SD peer entry known");
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003442 if (p2p->state != P2P_IDLE)
3443 p2p_continue_find(p2p);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003444 return;
3445 }
3446
Dmitry Shmidt807291d2015-01-27 13:40:23 -08003447 if (p2p->sd_query && p2p->sd_query->for_all_peers) {
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07003448 /* Update the pending broadcast SD query count for this device
3449 */
3450 p2p->sd_peer->sd_pending_bcast_queries--;
3451
3452 /*
3453 * If there are no pending broadcast queries for this device,
3454 * mark it as done (-1).
3455 */
3456 if (p2p->sd_peer->sd_pending_bcast_queries == 0)
3457 p2p->sd_peer->sd_pending_bcast_queries = -1;
3458 }
3459
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003460 /* Wait for response from the peer */
3461 p2p_set_state(p2p, P2P_SD_DURING_FIND);
3462 p2p_set_timeout(p2p, 0, 200000);
3463}
3464
Jouni Malinen75ecf522011-06-27 15:19:46 -07003465
3466/**
3467 * p2p_retry_pd - Retry any pending provision disc requests in IDLE state
3468 * @p2p: P2P module context from p2p_init()
3469 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003470static void p2p_retry_pd(struct p2p_data *p2p)
Jouni Malinen75ecf522011-06-27 15:19:46 -07003471{
3472 struct p2p_device *dev;
3473
Jouni Malinen75ecf522011-06-27 15:19:46 -07003474 /*
3475 * Retry the prov disc req attempt only for the peer that the user had
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003476 * requested.
Jouni Malinen75ecf522011-06-27 15:19:46 -07003477 */
3478
3479 dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
Sunil Ravib0ac25f2024-07-12 01:42:03 +00003480 if (!ether_addr_equal(p2p->pending_pd_devaddr,
3481 dev->info.p2p_device_addr))
Jouni Malinen75ecf522011-06-27 15:19:46 -07003482 continue;
Sunil Ravic0f5d412024-09-11 22:12:49 +00003483 if (!dev->req_config_methods && !dev->req_bootstrap_method)
Jouni Malinen75ecf522011-06-27 15:19:46 -07003484 continue;
Jouni Malinen75ecf522011-06-27 15:19:46 -07003485
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003486 p2p_dbg(p2p, "Send pending Provision Discovery Request to "
Jouni Malinen75ecf522011-06-27 15:19:46 -07003487 MACSTR " (config methods 0x%x)",
3488 MAC2STR(dev->info.p2p_device_addr),
3489 dev->req_config_methods);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003490 p2p_send_prov_disc_req(p2p, dev,
Dmitry Shmidt051af732013-10-22 13:52:46 -07003491 dev->flags & P2P_DEV_PD_FOR_JOIN,
3492 p2p->pd_force_freq);
Jouni Malinen75ecf522011-06-27 15:19:46 -07003493 return;
3494 }
3495}
3496
3497
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003498static void p2p_prov_disc_cb(struct p2p_data *p2p, int success)
3499{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003500 p2p_dbg(p2p, "Provision Discovery Request TX callback: success=%d",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003501 success);
Jouni Malinen75ecf522011-06-27 15:19:46 -07003502
3503 /*
3504 * Postpone resetting the pending action state till after we actually
3505 * time out. This allows us to take some action like notifying any
3506 * interested parties about no response to the request.
3507 *
3508 * When the timer (below) goes off we check in IDLE, SEARCH, or
3509 * LISTEN_ONLY state, which are the only allowed states to issue a PD
3510 * requests in, if this was still pending and then raise notification.
3511 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003512
3513 if (!success) {
Jouni Malinen75ecf522011-06-27 15:19:46 -07003514 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
3515
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003516 if (p2p->user_initiated_pd &&
3517 (p2p->state == P2P_SEARCH || p2p->state == P2P_LISTEN_ONLY))
3518 {
3519 /* Retry request from timeout to avoid busy loops */
3520 p2p->pending_action_state = P2P_PENDING_PD;
3521 p2p_set_timeout(p2p, 0, 50000);
3522 } else if (p2p->state != P2P_IDLE)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003523 p2p_continue_find(p2p);
Jouni Malinen75ecf522011-06-27 15:19:46 -07003524 else if (p2p->user_initiated_pd) {
3525 p2p->pending_action_state = P2P_PENDING_PD;
3526 p2p_set_timeout(p2p, 0, 300000);
3527 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003528 return;
3529 }
3530
Jouni Malinen75ecf522011-06-27 15:19:46 -07003531 /*
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003532 * If after PD Request the peer doesn't expect to receive PD Response
3533 * the PD Request ACK indicates a completion of the current PD. This
3534 * happens only on the advertiser side sending the follow-on PD Request
3535 * with the status different than 12 (Success: accepted by user).
3536 */
3537 if (p2p->p2ps_prov && !p2p->p2ps_prov->pd_seeker &&
3538 p2p->p2ps_prov->status != P2P_SC_SUCCESS_DEFERRED) {
3539 p2p_dbg(p2p, "P2PS PD completion on Follow-on PD Request ACK");
3540
3541 if (p2p->send_action_in_progress) {
3542 p2p->send_action_in_progress = 0;
3543 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
3544 }
3545
3546 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
3547
3548 if (p2p->cfg->p2ps_prov_complete) {
3549 p2p->cfg->p2ps_prov_complete(
3550 p2p->cfg->cb_ctx,
3551 p2p->p2ps_prov->status,
3552 p2p->p2ps_prov->adv_mac,
3553 p2p->p2ps_prov->adv_mac,
3554 p2p->p2ps_prov->session_mac,
3555 NULL, p2p->p2ps_prov->adv_id,
3556 p2p->p2ps_prov->session_id,
3557 0, 0, NULL, 0, 0, 0,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08003558 NULL, NULL, 0, 0, NULL, 0);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003559 }
3560
3561 if (p2p->user_initiated_pd)
3562 p2p_reset_pending_pd(p2p);
3563
3564 p2ps_prov_free(p2p);
3565 return;
3566 }
3567
3568 /*
Jouni Malinen75ecf522011-06-27 15:19:46 -07003569 * This postponing, of resetting pending_action_state, needs to be
3570 * done only for user initiated PD requests and not internal ones.
3571 */
3572 if (p2p->user_initiated_pd)
3573 p2p->pending_action_state = P2P_PENDING_PD;
3574 else
3575 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
3576
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003577 /* Wait for response from the peer */
3578 if (p2p->state == P2P_SEARCH)
3579 p2p_set_state(p2p, P2P_PD_DURING_FIND);
3580 p2p_set_timeout(p2p, 0, 200000);
3581}
3582
3583
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003584static void p2p_prov_disc_resp_cb(struct p2p_data *p2p, int success)
3585{
3586 p2p_dbg(p2p, "Provision Discovery Response TX callback: success=%d",
3587 success);
3588
3589 if (p2p->send_action_in_progress) {
3590 p2p->send_action_in_progress = 0;
3591 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
3592 }
3593
3594 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
3595
Jimmy Chen5ef7aad2019-10-15 15:45:26 +08003596 if (!success) {
3597 if (p2p->state == P2P_SEARCH)
3598 p2p_continue_find(p2p);
Hai Shalom81f62d82019-07-22 12:10:00 -07003599 return;
Jimmy Chen5ef7aad2019-10-15 15:45:26 +08003600 }
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003601
3602 if (!p2p->cfg->prov_disc_resp_cb ||
Jimmy Chen5ef7aad2019-10-15 15:45:26 +08003603 p2p->cfg->prov_disc_resp_cb(p2p->cfg->cb_ctx) < 1) {
3604 if (p2p->state == P2P_SEARCH)
3605 p2p_continue_find(p2p);
Hai Shalom81f62d82019-07-22 12:10:00 -07003606 return;
Jimmy Chen5ef7aad2019-10-15 15:45:26 +08003607 }
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003608
3609 p2p_dbg(p2p,
3610 "Post-Provision Discovery operations started - do not try to continue other P2P operations");
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003611}
3612
3613
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003614int p2p_scan_res_handler(struct p2p_data *p2p, const u8 *bssid, int freq,
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003615 struct os_reltime *rx_time, int level, const u8 *ies,
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003616 size_t ies_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003617{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003618 if (os_reltime_before(rx_time, &p2p->find_start)) {
Dmitry Shmidtf8623282013-02-20 14:34:59 -08003619 /*
3620 * The driver may have cached (e.g., in cfg80211 BSS table) the
3621 * scan results for relatively long time. To avoid reporting
3622 * stale information, update P2P peers only based on results
3623 * that have based on frames received after the last p2p_find
3624 * operation was started.
3625 */
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003626 p2p_dbg(p2p, "Ignore old scan result for " MACSTR
Dmitry Shmidtde47be72016-01-07 12:52:55 -08003627 " (rx_time=%u.%06u find_start=%u.%06u)",
Dmitry Shmidtf8623282013-02-20 14:34:59 -08003628 MAC2STR(bssid), (unsigned int) rx_time->sec,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08003629 (unsigned int) rx_time->usec,
3630 (unsigned int) p2p->find_start.sec,
3631 (unsigned int) p2p->find_start.usec);
Dmitry Shmidtf8623282013-02-20 14:34:59 -08003632 return 0;
3633 }
3634
3635 p2p_add_device(p2p, bssid, freq, rx_time, level, ies, ies_len, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003636
3637 return 0;
3638}
3639
3640
Hai Shalom60840252021-02-19 19:02:11 -08003641void p2p_scan_res_handled(struct p2p_data *p2p, unsigned int delay)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003642{
3643 if (!p2p->p2p_scan_running) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003644 p2p_dbg(p2p, "p2p_scan was not running, but scan results received");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003645 }
3646 p2p->p2p_scan_running = 0;
Hai Shalom60840252021-02-19 19:02:11 -08003647
3648 /* Use this delay only when p2p_find doesn't set it */
3649 if (!p2p->search_delay)
3650 p2p->search_delay = delay;
3651
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003652 eloop_cancel_timeout(p2p_scan_timeout, p2p, NULL);
3653
3654 if (p2p_run_after_scan(p2p))
3655 return;
3656 if (p2p->state == P2P_SEARCH)
3657 p2p_continue_find(p2p);
3658}
3659
3660
Dmitry Shmidt9c175262016-03-03 10:20:07 -08003661void p2p_scan_ie(struct p2p_data *p2p, struct wpabuf *ies, const u8 *dev_id,
3662 unsigned int bands)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003663{
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003664 u8 dev_capab;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003665 u8 *len;
3666
3667#ifdef CONFIG_WIFI_DISPLAY
3668 if (p2p->wfd_ie_probe_req)
3669 wpabuf_put_buf(ies, p2p->wfd_ie_probe_req);
3670#endif /* CONFIG_WIFI_DISPLAY */
3671
Dmitry Shmidt2e67f062014-07-16 09:55:28 -07003672 if (p2p->vendor_elem && p2p->vendor_elem[VENDOR_ELEM_PROBE_REQ_P2P])
3673 wpabuf_put_buf(ies,
3674 p2p->vendor_elem[VENDOR_ELEM_PROBE_REQ_P2P]);
3675
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003676 len = p2p_buf_add_ie_hdr(ies);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003677
3678 dev_capab = p2p->dev_capab & ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
3679
3680 /* P2PS requires Probe Request frames to include SD bit */
3681 if (p2p->p2ps_seek && p2p->p2ps_seek_count)
3682 dev_capab |= P2P_DEV_CAPAB_SERVICE_DISCOVERY;
3683
3684 p2p_buf_add_capability(ies, dev_capab, 0);
3685
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003686 if (dev_id)
3687 p2p_buf_add_device_id(ies, dev_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003688 if (p2p->cfg->reg_class && p2p->cfg->channel)
3689 p2p_buf_add_listen_channel(ies, p2p->cfg->country,
3690 p2p->cfg->reg_class,
3691 p2p->cfg->channel);
3692 if (p2p->ext_listen_interval)
3693 p2p_buf_add_ext_listen_timing(ies, p2p->ext_listen_period,
3694 p2p->ext_listen_interval);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003695
Dmitry Shmidt9c175262016-03-03 10:20:07 -08003696 if (bands & BAND_60_GHZ)
3697 p2p_buf_add_device_info(ies, p2p, NULL);
3698
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003699 if (p2p->p2ps_seek && p2p->p2ps_seek_count)
3700 p2p_buf_add_service_hash(ies, p2p);
3701
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003702 /* TODO: p2p_buf_add_operating_channel() if GO */
3703 p2p_buf_update_ie_hdr(ies, len);
3704}
3705
3706
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003707size_t p2p_scan_ie_buf_len(struct p2p_data *p2p)
3708{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003709 size_t len = 100;
3710
3711#ifdef CONFIG_WIFI_DISPLAY
3712 if (p2p && p2p->wfd_ie_probe_req)
3713 len += wpabuf_len(p2p->wfd_ie_probe_req);
3714#endif /* CONFIG_WIFI_DISPLAY */
3715
Dmitry Shmidt2e67f062014-07-16 09:55:28 -07003716 if (p2p && p2p->vendor_elem &&
3717 p2p->vendor_elem[VENDOR_ELEM_PROBE_REQ_P2P])
3718 len += wpabuf_len(p2p->vendor_elem[VENDOR_ELEM_PROBE_REQ_P2P]);
3719
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003720 return len;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003721}
3722
3723
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003724int p2p_ie_text(struct wpabuf *p2p_ie, char *buf, char *end)
3725{
3726 return p2p_attr_text(p2p_ie, buf, end);
3727}
3728
3729
3730static void p2p_go_neg_req_cb(struct p2p_data *p2p, int success)
3731{
3732 struct p2p_device *dev = p2p->go_neg_peer;
Dmitry Shmidt8c652892013-03-01 10:14:01 -08003733 int timeout;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003734
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003735 p2p_dbg(p2p, "GO Negotiation Request TX callback: success=%d", success);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003736
3737 if (dev == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003738 p2p_dbg(p2p, "No pending GO Negotiation");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003739 return;
3740 }
3741
3742 if (success) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003743 if (dev->flags & P2P_DEV_USER_REJECTED) {
3744 p2p_set_state(p2p, P2P_IDLE);
3745 return;
3746 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07003747 } else if (dev->go_neg_req_sent) {
3748 /* Cancel the increment from p2p_connect_send() on failure */
Dmitry Shmidt98f9e762012-05-30 11:18:46 -07003749 dev->go_neg_req_sent--;
3750 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003751
3752 if (!success &&
3753 (dev->info.dev_capab & P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY) &&
3754 !is_zero_ether_addr(dev->member_in_go_dev)) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003755 p2p_dbg(p2p, "Peer " MACSTR " did not acknowledge request - try to use device discoverability through its GO",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003756 MAC2STR(dev->info.p2p_device_addr));
3757 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
3758 p2p_send_dev_disc_req(p2p, dev);
3759 return;
3760 }
3761
3762 /*
3763 * Use P2P find, if needed, to find the other device from its listen
3764 * channel.
3765 */
3766 p2p_set_state(p2p, P2P_CONNECT);
Dmitry Shmidt8c652892013-03-01 10:14:01 -08003767 timeout = success ? 500000 : 100000;
3768 if (!success && p2p->go_neg_peer &&
3769 (p2p->go_neg_peer->flags & P2P_DEV_PEER_WAITING_RESPONSE)) {
3770 unsigned int r;
3771 /*
3772 * Peer is expected to wait our response and we will skip the
3773 * listen phase. Add some randomness to the wait time here to
3774 * make it less likely to hit cases where we could end up in
3775 * sync with peer not listening.
3776 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07003777 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
3778 r = 0;
Dmitry Shmidt8c652892013-03-01 10:14:01 -08003779 timeout += r % 100000;
3780 }
3781 p2p_set_timeout(p2p, 0, timeout);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003782}
3783
3784
3785static void p2p_go_neg_resp_cb(struct p2p_data *p2p, int success)
3786{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003787 p2p_dbg(p2p, "GO Negotiation Response TX callback: success=%d",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003788 success);
3789 if (!p2p->go_neg_peer && p2p->state == P2P_PROVISIONING) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003790 p2p_dbg(p2p, "Ignore TX callback event - GO Negotiation is not running anymore");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003791 return;
3792 }
3793 p2p_set_state(p2p, P2P_CONNECT);
Dmitry Shmidt8c652892013-03-01 10:14:01 -08003794 p2p_set_timeout(p2p, 0, 500000);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003795}
3796
3797
Dmitry Shmidt4b060592013-04-29 16:42:49 -07003798static void p2p_go_neg_resp_failure_cb(struct p2p_data *p2p, int success,
3799 const u8 *addr)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003800{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003801 p2p_dbg(p2p, "GO Negotiation Response (failure) TX callback: success=%d", success);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003802 if (p2p->go_neg_peer && p2p->go_neg_peer->status != P2P_SC_SUCCESS) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003803 p2p_go_neg_failed(p2p, p2p->go_neg_peer->status);
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08003804 return;
3805 }
3806
3807 if (success) {
Dmitry Shmidt4b060592013-04-29 16:42:49 -07003808 struct p2p_device *dev;
3809 dev = p2p_get_device(p2p, addr);
3810 if (dev &&
Dmitry Shmidt34c12022015-03-05 14:11:20 -08003811 dev->status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE)
Dmitry Shmidt4b060592013-04-29 16:42:49 -07003812 dev->flags |= P2P_DEV_PEER_WAITING_RESPONSE;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003813 }
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08003814
3815 if (p2p->state == P2P_SEARCH || p2p->state == P2P_SD_DURING_FIND)
3816 p2p_continue_find(p2p);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003817}
3818
3819
3820static void p2p_go_neg_conf_cb(struct p2p_data *p2p,
3821 enum p2p_send_action_result result)
3822{
3823 struct p2p_device *dev;
3824
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003825 p2p_dbg(p2p, "GO Negotiation Confirm TX callback: result=%d", result);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003826 if (result == P2P_SEND_ACTION_FAILED) {
Dmitry Shmidt413dde72014-04-11 10:23:22 -07003827 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003828 p2p_go_neg_failed(p2p, -1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003829 return;
3830 }
Dmitry Shmidt413dde72014-04-11 10:23:22 -07003831
3832 dev = p2p->go_neg_peer;
3833
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003834 if (result == P2P_SEND_ACTION_NO_ACK) {
3835 /*
Dmitry Shmidt413dde72014-04-11 10:23:22 -07003836 * Retry GO Negotiation Confirmation
3837 * P2P_GO_NEG_CNF_MAX_RETRY_COUNT times if we did not receive
3838 * ACK for confirmation.
3839 */
3840 if (dev && dev->go_neg_conf &&
3841 dev->go_neg_conf_sent <= P2P_GO_NEG_CNF_MAX_RETRY_COUNT) {
3842 p2p_dbg(p2p, "GO Negotiation Confirm retry %d",
3843 dev->go_neg_conf_sent);
3844 p2p->pending_action_state = P2P_PENDING_GO_NEG_CONFIRM;
3845 if (p2p_send_action(p2p, dev->go_neg_conf_freq,
3846 dev->info.p2p_device_addr,
3847 p2p->cfg->dev_addr,
3848 dev->info.p2p_device_addr,
3849 wpabuf_head(dev->go_neg_conf),
3850 wpabuf_len(dev->go_neg_conf), 0) >=
3851 0) {
3852 dev->go_neg_conf_sent++;
3853 return;
3854 }
3855 p2p_dbg(p2p, "Failed to re-send Action frame");
3856
3857 /*
3858 * Continue with the assumption that the first attempt
3859 * went through and just the ACK frame was lost.
3860 */
3861 }
3862
3863 /*
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003864 * It looks like the TX status for GO Negotiation Confirm is
3865 * often showing failure even when the peer has actually
3866 * received the frame. Since the peer may change channels
3867 * immediately after having received the frame, we may not see
3868 * an Ack for retries, so just dropping a single frame may
3869 * trigger this. To allow the group formation to succeed if the
3870 * peer did indeed receive the frame, continue regardless of
3871 * the TX status.
3872 */
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003873 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 -07003874 }
3875
Dmitry Shmidt413dde72014-04-11 10:23:22 -07003876 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
3877
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003878 if (dev == NULL)
3879 return;
3880
3881 p2p_go_complete(p2p, dev);
3882}
3883
3884
3885void p2p_send_action_cb(struct p2p_data *p2p, unsigned int freq, const u8 *dst,
3886 const u8 *src, const u8 *bssid,
3887 enum p2p_send_action_result result)
3888{
3889 enum p2p_pending_action_state state;
3890 int success;
3891
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003892 p2p_dbg(p2p, "Action frame TX callback (state=%d freq=%u dst=" MACSTR
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003893 " src=" MACSTR " bssid=" MACSTR " result=%d p2p_state=%s)",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003894 p2p->pending_action_state, freq, MAC2STR(dst), MAC2STR(src),
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003895 MAC2STR(bssid), result, p2p_state_txt(p2p->state));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003896 success = result == P2P_SEND_ACTION_SUCCESS;
3897 state = p2p->pending_action_state;
3898 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
3899 switch (state) {
3900 case P2P_NO_PENDING_ACTION:
Dmitry Shmidt4582d2a2014-02-28 11:14:23 -08003901 if (p2p->send_action_in_progress) {
3902 p2p->send_action_in_progress = 0;
3903 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
3904 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003905 break;
3906 case P2P_PENDING_GO_NEG_REQUEST:
3907 p2p_go_neg_req_cb(p2p, success);
3908 break;
3909 case P2P_PENDING_GO_NEG_RESPONSE:
3910 p2p_go_neg_resp_cb(p2p, success);
3911 break;
3912 case P2P_PENDING_GO_NEG_RESPONSE_FAILURE:
Dmitry Shmidt4b060592013-04-29 16:42:49 -07003913 p2p_go_neg_resp_failure_cb(p2p, success, dst);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003914 break;
3915 case P2P_PENDING_GO_NEG_CONFIRM:
3916 p2p_go_neg_conf_cb(p2p, result);
3917 break;
3918 case P2P_PENDING_SD:
Sunil Ravi7f769292024-07-23 22:21:32 +00003919 p2p_sd_query_cb(p2p, success);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003920 break;
3921 case P2P_PENDING_PD:
3922 p2p_prov_disc_cb(p2p, success);
3923 break;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003924 case P2P_PENDING_PD_RESPONSE:
3925 p2p_prov_disc_resp_cb(p2p, success);
3926 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003927 case P2P_PENDING_INVITATION_REQUEST:
3928 p2p_invitation_req_cb(p2p, success);
3929 break;
3930 case P2P_PENDING_INVITATION_RESPONSE:
3931 p2p_invitation_resp_cb(p2p, success);
3932 break;
3933 case P2P_PENDING_DEV_DISC_REQUEST:
3934 p2p_dev_disc_req_cb(p2p, success);
3935 break;
3936 case P2P_PENDING_DEV_DISC_RESPONSE:
3937 p2p_dev_disc_resp_cb(p2p, success);
3938 break;
3939 case P2P_PENDING_GO_DISC_REQ:
3940 p2p_go_disc_req_cb(p2p, success);
3941 break;
3942 }
3943}
3944
3945
3946void p2p_listen_cb(struct p2p_data *p2p, unsigned int freq,
3947 unsigned int duration)
3948{
3949 if (freq == p2p->pending_client_disc_freq) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003950 p2p_dbg(p2p, "Client discoverability remain-awake completed");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003951 p2p->pending_client_disc_freq = 0;
3952 return;
3953 }
3954
3955 if (freq != p2p->pending_listen_freq) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003956 p2p_dbg(p2p, "Unexpected listen callback for freq=%u duration=%u (pending_listen_freq=%u)",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003957 freq, duration, p2p->pending_listen_freq);
3958 return;
3959 }
3960
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003961 p2p_dbg(p2p, "Starting Listen timeout(%u,%u) on freq=%u based on callback",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003962 p2p->pending_listen_sec, p2p->pending_listen_usec,
3963 p2p->pending_listen_freq);
Sunil Ravib0ac25f2024-07-12 01:42:03 +00003964 p2p->pending_listen_wait_drv = false;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003965 p2p->in_listen = 1;
3966 p2p->drv_in_listen = freq;
3967 if (p2p->pending_listen_sec || p2p->pending_listen_usec) {
3968 /*
3969 * Add 20 msec extra wait to avoid race condition with driver
3970 * remain-on-channel end event, i.e., give driver more time to
3971 * complete the operation before our timeout expires.
3972 */
3973 p2p_set_timeout(p2p, p2p->pending_listen_sec,
3974 p2p->pending_listen_usec + 20000);
3975 }
3976
3977 p2p->pending_listen_freq = 0;
3978}
3979
3980
3981int p2p_listen_end(struct p2p_data *p2p, unsigned int freq)
3982{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003983 p2p_dbg(p2p, "Driver ended Listen state (freq=%u)", freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003984 p2p->drv_in_listen = 0;
3985 if (p2p->in_listen)
3986 return 0; /* Internal timeout will trigger the next step */
3987
Roshan Pius3a1667e2018-07-03 15:17:14 -07003988 if (p2p->state == P2P_WAIT_PEER_CONNECT && p2p->go_neg_peer &&
3989 p2p->pending_listen_freq) {
3990 /*
3991 * Better wait a bit if the driver is unable to start
3992 * offchannel operation for some reason to continue with
3993 * P2P_WAIT_PEER_(IDLE/CONNECT) state transitions.
3994 */
3995 p2p_dbg(p2p,
3996 "Listen operation did not seem to start - delay idle phase to avoid busy loop");
3997 p2p_set_timeout(p2p, 0, 100000);
3998 return 1;
3999 }
4000
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004001 if (p2p->state == P2P_CONNECT_LISTEN && p2p->go_neg_peer) {
4002 if (p2p->go_neg_peer->connect_reqs >= 120) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004003 p2p_dbg(p2p, "Timeout on sending GO Negotiation Request without getting response");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004004 p2p_go_neg_failed(p2p, -1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004005 return 0;
4006 }
4007
4008 p2p_set_state(p2p, P2P_CONNECT);
4009 p2p_connect_send(p2p, p2p->go_neg_peer);
4010 return 1;
4011 } else if (p2p->state == P2P_SEARCH) {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004012 if (p2p->p2p_scan_running) {
4013 /*
4014 * Search is already in progress. This can happen if
4015 * an Action frame RX is reported immediately after
4016 * the end of a remain-on-channel operation and the
4017 * response frame to that is sent using an offchannel
4018 * operation while in p2p_find. Avoid an attempt to
4019 * restart a scan here.
4020 */
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004021 p2p_dbg(p2p, "p2p_scan already in progress - do not try to start a new one");
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004022 return 1;
4023 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07004024 if (p2p->pending_listen_freq) {
4025 /*
4026 * Better wait a bit if the driver is unable to start
4027 * offchannel operation for some reason. p2p_search()
4028 * will be started from internal timeout.
4029 */
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004030 p2p_dbg(p2p, "Listen operation did not seem to start - delay search phase to avoid busy loop");
Dmitry Shmidt04949592012-07-19 12:16:46 -07004031 p2p_set_timeout(p2p, 0, 100000);
4032 return 1;
4033 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004034 if (p2p->search_delay) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004035 p2p_dbg(p2p, "Delay search operation by %u ms",
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004036 p2p->search_delay);
4037 p2p_set_timeout(p2p, p2p->search_delay / 1000,
4038 (p2p->search_delay % 1000) * 1000);
4039 return 1;
4040 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004041 p2p_search(p2p);
4042 return 1;
4043 }
4044
4045 return 0;
4046}
4047
4048
4049static void p2p_timeout_connect(struct p2p_data *p2p)
4050{
4051 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004052 if (p2p->go_neg_peer &&
4053 (p2p->go_neg_peer->flags & P2P_DEV_WAIT_GO_NEG_CONFIRM)) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004054 p2p_dbg(p2p, "Wait for GO Negotiation Confirm timed out - assume GO Negotiation failed");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004055 p2p_go_neg_failed(p2p, -1);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004056 return;
4057 }
Dmitry Shmidt8c652892013-03-01 10:14:01 -08004058 if (p2p->go_neg_peer &&
4059 (p2p->go_neg_peer->flags & P2P_DEV_PEER_WAITING_RESPONSE) &&
4060 p2p->go_neg_peer->connect_reqs < 120) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004061 p2p_dbg(p2p, "Peer expected to wait our response - skip listen");
Dmitry Shmidt8c652892013-03-01 10:14:01 -08004062 p2p_connect_send(p2p, p2p->go_neg_peer);
4063 return;
4064 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004065 if (p2p->go_neg_peer && p2p->go_neg_peer->oob_go_neg_freq > 0) {
4066 p2p_dbg(p2p, "Skip connect-listen since GO Neg channel known (OOB)");
4067 p2p_set_state(p2p, P2P_CONNECT_LISTEN);
4068 p2p_set_timeout(p2p, 0, 30000);
4069 return;
4070 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004071 p2p_set_state(p2p, P2P_CONNECT_LISTEN);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004072 p2p_listen_in_find(p2p, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004073}
4074
4075
4076static void p2p_timeout_connect_listen(struct p2p_data *p2p)
4077{
4078 if (p2p->go_neg_peer) {
4079 if (p2p->drv_in_listen) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004080 p2p_dbg(p2p, "Driver is still in Listen state; wait for it to complete");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004081 return;
4082 }
4083
4084 if (p2p->go_neg_peer->connect_reqs >= 120) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004085 p2p_dbg(p2p, "Timeout on sending GO Negotiation Request without getting response");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004086 p2p_go_neg_failed(p2p, -1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004087 return;
4088 }
4089
4090 p2p_set_state(p2p, P2P_CONNECT);
4091 p2p_connect_send(p2p, p2p->go_neg_peer);
4092 } else
4093 p2p_set_state(p2p, P2P_IDLE);
4094}
4095
4096
4097static void p2p_timeout_wait_peer_connect(struct p2p_data *p2p)
4098{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004099 p2p_set_state(p2p, P2P_WAIT_PEER_IDLE);
Dmitry Shmidt18463232014-01-24 12:29:41 -08004100
4101 if (p2p->cfg->is_concurrent_session_active &&
4102 p2p->cfg->is_concurrent_session_active(p2p->cfg->cb_ctx))
4103 p2p_set_timeout(p2p, 0, 500000);
4104 else
4105 p2p_set_timeout(p2p, 0, 200000);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004106}
4107
4108
4109static void p2p_timeout_wait_peer_idle(struct p2p_data *p2p)
4110{
4111 struct p2p_device *dev = p2p->go_neg_peer;
4112
4113 if (dev == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004114 p2p_dbg(p2p, "Unknown GO Neg peer - stop GO Neg wait");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004115 return;
4116 }
4117
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004118 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 -07004119 p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
Sunil Ravib0ac25f2024-07-12 01:42:03 +00004120 p2p->pending_listen_wait_drv = false;
Hai Shaloma20dcd72022-02-04 13:43:00 -08004121 if (p2p->pending_listen_freq) {
4122 p2p_dbg(p2p, "Clear pending_listen_freq for %s", __func__);
4123 p2p->pending_listen_freq = 0;
4124 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004125 p2p_set_state(p2p, P2P_WAIT_PEER_CONNECT);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004126 p2p_listen_in_find(p2p, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004127}
4128
4129
4130static void p2p_timeout_sd_during_find(struct p2p_data *p2p)
4131{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004132 p2p_dbg(p2p, "Service Discovery Query timeout");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004133 if (p2p->sd_peer) {
4134 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004135 p2p->sd_peer = NULL;
4136 }
4137 p2p_continue_find(p2p);
4138}
4139
4140
4141static void p2p_timeout_prov_disc_during_find(struct p2p_data *p2p)
4142{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004143 p2p_dbg(p2p, "Provision Discovery Request timeout");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004144 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
4145 p2p_continue_find(p2p);
4146}
4147
4148
Jouni Malinen75ecf522011-06-27 15:19:46 -07004149static void p2p_timeout_prov_disc_req(struct p2p_data *p2p)
4150{
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004151 u32 adv_id = 0;
4152 u8 *adv_mac = NULL;
4153
Jouni Malinen75ecf522011-06-27 15:19:46 -07004154 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
4155
4156 /*
4157 * For user initiated PD requests that we have not gotten any responses
4158 * for while in IDLE state, we retry them a couple of times before
4159 * giving up.
4160 */
4161 if (!p2p->user_initiated_pd)
4162 return;
4163
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004164 p2p_dbg(p2p, "User initiated Provision Discovery Request timeout");
Jouni Malinen75ecf522011-06-27 15:19:46 -07004165
4166 if (p2p->pd_retries) {
4167 p2p->pd_retries--;
4168 p2p_retry_pd(p2p);
4169 } else {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004170 struct p2p_device *dev;
4171 int for_join = 0;
4172
4173 dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
Sunil Ravib0ac25f2024-07-12 01:42:03 +00004174 if (!ether_addr_equal(p2p->pending_pd_devaddr,
4175 dev->info.p2p_device_addr))
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004176 continue;
4177 if (dev->req_config_methods &&
4178 (dev->flags & P2P_DEV_PD_FOR_JOIN))
4179 for_join = 1;
4180 }
4181
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004182 if (p2p->p2ps_prov) {
4183 adv_id = p2p->p2ps_prov->adv_id;
4184 adv_mac = p2p->p2ps_prov->adv_mac;
4185 }
4186
Jouni Malinen75ecf522011-06-27 15:19:46 -07004187 if (p2p->cfg->prov_disc_fail)
4188 p2p->cfg->prov_disc_fail(p2p->cfg->cb_ctx,
4189 p2p->pending_pd_devaddr,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004190 for_join ?
4191 P2P_PROV_DISC_TIMEOUT_JOIN :
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004192 P2P_PROV_DISC_TIMEOUT,
4193 adv_id, adv_mac, NULL);
Jouni Malinen75ecf522011-06-27 15:19:46 -07004194 p2p_reset_pending_pd(p2p);
4195 }
4196}
4197
4198
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004199static void p2p_timeout_invite(struct p2p_data *p2p)
4200{
4201 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
4202 p2p_set_state(p2p, P2P_INVITE_LISTEN);
4203 if (p2p->inv_role == P2P_INVITE_ROLE_ACTIVE_GO) {
4204 /*
4205 * Better remain on operating channel instead of listen channel
4206 * when running a group.
Sunil Ravi036cec52023-03-29 11:35:17 -07004207 * Wait 120 ms to let the P2P GO to send its beacon on the
4208 * intended TBTT.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004209 */
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004210 p2p_dbg(p2p, "Inviting in active GO role - wait on operating channel");
Sunil Ravi036cec52023-03-29 11:35:17 -07004211 p2p_set_timeout(p2p, 0, 120000);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004212 return;
4213 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004214 p2p_listen_in_find(p2p, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004215}
4216
4217
4218static void p2p_timeout_invite_listen(struct p2p_data *p2p)
4219{
4220 if (p2p->invite_peer && p2p->invite_peer->invitation_reqs < 100) {
4221 p2p_set_state(p2p, P2P_INVITE);
4222 p2p_invite_send(p2p, p2p->invite_peer,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004223 p2p->invite_go_dev_addr, p2p->invite_dev_pw_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004224 } else {
4225 if (p2p->invite_peer) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004226 p2p_dbg(p2p, "Invitation Request retry limit reached");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004227 if (p2p->cfg->invitation_result)
4228 p2p->cfg->invitation_result(
Dmitry Shmidt700a1372013-03-15 14:14:44 -07004229 p2p->cfg->cb_ctx, -1, NULL, NULL,
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004230 p2p->invite_peer->info.p2p_device_addr,
Dmitry Shmidt15907092014-03-25 10:42:57 -07004231 0, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004232 }
4233 p2p_set_state(p2p, P2P_IDLE);
4234 }
4235}
4236
4237
4238static void p2p_state_timeout(void *eloop_ctx, void *timeout_ctx)
4239{
4240 struct p2p_data *p2p = eloop_ctx;
4241
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004242 p2p_dbg(p2p, "Timeout (state=%s)", p2p_state_txt(p2p->state));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004243
4244 p2p->in_listen = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004245 if (p2p->drv_in_listen) {
4246 p2p_dbg(p2p, "Driver is still in listen state - stop it");
4247 p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
Sunil Ravib0ac25f2024-07-12 01:42:03 +00004248 p2p->pending_listen_wait_drv = false;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004249 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004250
4251 switch (p2p->state) {
4252 case P2P_IDLE:
Jouni Malinen75ecf522011-06-27 15:19:46 -07004253 /* Check if we timed out waiting for PD req */
4254 if (p2p->pending_action_state == P2P_PENDING_PD)
4255 p2p_timeout_prov_disc_req(p2p);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004256 break;
4257 case P2P_SEARCH:
Jouni Malinen75ecf522011-06-27 15:19:46 -07004258 /* Check if we timed out waiting for PD req */
4259 if (p2p->pending_action_state == P2P_PENDING_PD)
4260 p2p_timeout_prov_disc_req(p2p);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004261 if (p2p->search_delay && !p2p->in_search_delay) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004262 p2p_dbg(p2p, "Delay search operation by %u ms",
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004263 p2p->search_delay);
4264 p2p->in_search_delay = 1;
4265 p2p_set_timeout(p2p, p2p->search_delay / 1000,
4266 (p2p->search_delay % 1000) * 1000);
4267 break;
4268 }
4269 p2p->in_search_delay = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004270 p2p_search(p2p);
4271 break;
4272 case P2P_CONNECT:
4273 p2p_timeout_connect(p2p);
4274 break;
4275 case P2P_CONNECT_LISTEN:
4276 p2p_timeout_connect_listen(p2p);
4277 break;
4278 case P2P_GO_NEG:
4279 break;
4280 case P2P_LISTEN_ONLY:
Jouni Malinen75ecf522011-06-27 15:19:46 -07004281 /* Check if we timed out waiting for PD req */
4282 if (p2p->pending_action_state == P2P_PENDING_PD)
4283 p2p_timeout_prov_disc_req(p2p);
4284
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004285 if (p2p->ext_listen_only) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004286 p2p_dbg(p2p, "Extended Listen Timing - Listen State completed");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004287 p2p->ext_listen_only = 0;
4288 p2p_set_state(p2p, P2P_IDLE);
4289 }
4290 break;
4291 case P2P_WAIT_PEER_CONNECT:
4292 p2p_timeout_wait_peer_connect(p2p);
4293 break;
4294 case P2P_WAIT_PEER_IDLE:
4295 p2p_timeout_wait_peer_idle(p2p);
4296 break;
4297 case P2P_SD_DURING_FIND:
4298 p2p_timeout_sd_during_find(p2p);
4299 break;
4300 case P2P_PROVISIONING:
4301 break;
4302 case P2P_PD_DURING_FIND:
4303 p2p_timeout_prov_disc_during_find(p2p);
4304 break;
4305 case P2P_INVITE:
4306 p2p_timeout_invite(p2p);
4307 break;
4308 case P2P_INVITE_LISTEN:
4309 p2p_timeout_invite_listen(p2p);
4310 break;
4311 }
4312}
4313
4314
4315int p2p_reject(struct p2p_data *p2p, const u8 *peer_addr)
4316{
4317 struct p2p_device *dev;
4318
4319 dev = p2p_get_device(p2p, peer_addr);
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004320 p2p_dbg(p2p, "Local request to reject connection attempts by peer "
4321 MACSTR, MAC2STR(peer_addr));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004322 if (dev == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004323 p2p_dbg(p2p, "Peer " MACSTR " unknown", MAC2STR(peer_addr));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004324 return -1;
4325 }
4326 dev->status = P2P_SC_FAIL_REJECTED_BY_USER;
4327 dev->flags |= P2P_DEV_USER_REJECTED;
4328 return 0;
4329}
4330
4331
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004332const char * p2p_wps_method_text(enum p2p_wps_method method)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004333{
4334 switch (method) {
4335 case WPS_NOT_READY:
4336 return "not-ready";
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004337 case WPS_PIN_DISPLAY:
4338 return "Display";
4339 case WPS_PIN_KEYPAD:
4340 return "Keypad";
4341 case WPS_PBC:
4342 return "PBC";
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004343 case WPS_NFC:
4344 return "NFC";
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004345 case WPS_P2PS:
4346 return "P2PS";
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004347 }
4348
4349 return "??";
4350}
4351
4352
4353static const char * p2p_go_state_text(enum p2p_go_state go_state)
4354{
4355 switch (go_state) {
4356 case UNKNOWN_GO:
4357 return "unknown";
4358 case LOCAL_GO:
4359 return "local";
4360 case REMOTE_GO:
4361 return "remote";
4362 }
4363
4364 return "??";
4365}
4366
4367
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004368const struct p2p_peer_info * p2p_get_peer_info(struct p2p_data *p2p,
4369 const u8 *addr, int next)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004370{
4371 struct p2p_device *dev;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004372
4373 if (addr)
4374 dev = p2p_get_device(p2p, addr);
4375 else
4376 dev = dl_list_first(&p2p->devices, struct p2p_device, list);
4377
4378 if (dev && next) {
4379 dev = dl_list_first(&dev->list, struct p2p_device, list);
4380 if (&dev->list == &p2p->devices)
4381 dev = NULL;
4382 }
4383
4384 if (dev == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004385 return NULL;
4386
4387 return &dev->info;
4388}
4389
4390
4391int p2p_get_peer_info_txt(const struct p2p_peer_info *info,
4392 char *buf, size_t buflen)
4393{
4394 struct p2p_device *dev;
4395 int res;
4396 char *pos, *end;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004397 struct os_reltime now;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004398
4399 if (info == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004400 return -1;
4401
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004402 dev = (struct p2p_device *) (((u8 *) info) -
4403 offsetof(struct p2p_device, info));
4404
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004405 pos = buf;
4406 end = buf + buflen;
4407
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004408 os_get_reltime(&now);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004409 res = os_snprintf(pos, end - pos,
4410 "age=%d\n"
4411 "listen_freq=%d\n"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004412 "wps_method=%s\n"
4413 "interface_addr=" MACSTR "\n"
4414 "member_in_go_dev=" MACSTR "\n"
4415 "member_in_go_iface=" MACSTR "\n"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004416 "go_neg_req_sent=%d\n"
4417 "go_state=%s\n"
4418 "dialog_token=%u\n"
4419 "intended_addr=" MACSTR "\n"
4420 "country=%c%c\n"
4421 "oper_freq=%d\n"
4422 "req_config_methods=0x%x\n"
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07004423 "flags=%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004424 "status=%d\n"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004425 "invitation_reqs=%u\n",
4426 (int) (now.sec - dev->last_seen.sec),
4427 dev->listen_freq,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004428 p2p_wps_method_text(dev->wps_method),
4429 MAC2STR(dev->interface_addr),
4430 MAC2STR(dev->member_in_go_dev),
4431 MAC2STR(dev->member_in_go_iface),
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004432 dev->go_neg_req_sent,
4433 p2p_go_state_text(dev->go_state),
4434 dev->dialog_token,
4435 MAC2STR(dev->intended_addr),
4436 dev->country[0] ? dev->country[0] : '_',
4437 dev->country[1] ? dev->country[1] : '_',
4438 dev->oper_freq,
4439 dev->req_config_methods,
4440 dev->flags & P2P_DEV_PROBE_REQ_ONLY ?
4441 "[PROBE_REQ_ONLY]" : "",
4442 dev->flags & P2P_DEV_REPORTED ? "[REPORTED]" : "",
4443 dev->flags & P2P_DEV_NOT_YET_READY ?
4444 "[NOT_YET_READY]" : "",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004445 dev->flags & P2P_DEV_PD_PEER_DISPLAY ?
4446 "[PD_PEER_DISPLAY]" : "",
4447 dev->flags & P2P_DEV_PD_PEER_KEYPAD ?
4448 "[PD_PEER_KEYPAD]" : "",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004449 dev->flags & P2P_DEV_PD_PEER_P2PS ?
4450 "[PD_PEER_P2PS]" : "",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004451 dev->flags & P2P_DEV_USER_REJECTED ?
4452 "[USER_REJECTED]" : "",
4453 dev->flags & P2P_DEV_PEER_WAITING_RESPONSE ?
4454 "[PEER_WAITING_RESPONSE]" : "",
4455 dev->flags & P2P_DEV_PREFER_PERSISTENT_GROUP ?
4456 "[PREFER_PERSISTENT_GROUP]" : "",
4457 dev->flags & P2P_DEV_WAIT_GO_NEG_RESPONSE ?
4458 "[WAIT_GO_NEG_RESPONSE]" : "",
4459 dev->flags & P2P_DEV_WAIT_GO_NEG_CONFIRM ?
4460 "[WAIT_GO_NEG_CONFIRM]" : "",
4461 dev->flags & P2P_DEV_GROUP_CLIENT_ONLY ?
4462 "[GROUP_CLIENT_ONLY]" : "",
4463 dev->flags & P2P_DEV_FORCE_FREQ ?
4464 "[FORCE_FREQ]" : "",
4465 dev->flags & P2P_DEV_PD_FOR_JOIN ?
4466 "[PD_FOR_JOIN]" : "",
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07004467 dev->flags & P2P_DEV_LAST_SEEN_AS_GROUP_CLIENT ?
4468 "[LAST_SEEN_AS_GROUP_CLIENT]" : "",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004469 dev->status,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004470 dev->invitation_reqs);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004471 if (os_snprintf_error(end - pos, res))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004472 return pos - buf;
4473 pos += res;
4474
4475 if (dev->ext_listen_period) {
4476 res = os_snprintf(pos, end - pos,
4477 "ext_listen_period=%u\n"
4478 "ext_listen_interval=%u\n",
4479 dev->ext_listen_period,
4480 dev->ext_listen_interval);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004481 if (os_snprintf_error(end - pos, res))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004482 return pos - buf;
4483 pos += res;
4484 }
4485
4486 if (dev->oper_ssid_len) {
4487 res = os_snprintf(pos, end - pos,
4488 "oper_ssid=%s\n",
4489 wpa_ssid_txt(dev->oper_ssid,
4490 dev->oper_ssid_len));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004491 if (os_snprintf_error(end - pos, res))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004492 return pos - buf;
4493 pos += res;
4494 }
4495
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004496#ifdef CONFIG_WIFI_DISPLAY
4497 if (dev->info.wfd_subelems) {
4498 res = os_snprintf(pos, end - pos, "wfd_subelems=");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004499 if (os_snprintf_error(end - pos, res))
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004500 return pos - buf;
4501 pos += res;
4502
4503 pos += wpa_snprintf_hex(pos, end - pos,
4504 wpabuf_head(dev->info.wfd_subelems),
4505 wpabuf_len(dev->info.wfd_subelems));
4506
4507 res = os_snprintf(pos, end - pos, "\n");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004508 if (os_snprintf_error(end - pos, res))
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004509 return pos - buf;
4510 pos += res;
4511 }
4512#endif /* CONFIG_WIFI_DISPLAY */
4513
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004514 return pos - buf;
4515}
4516
4517
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004518int p2p_peer_known(struct p2p_data *p2p, const u8 *addr)
4519{
4520 return p2p_get_device(p2p, addr) != NULL;
4521}
4522
4523
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004524void p2p_set_client_discoverability(struct p2p_data *p2p, int enabled)
4525{
4526 if (enabled) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004527 p2p_dbg(p2p, "Client discoverability enabled");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004528 p2p->dev_capab |= P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
4529 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004530 p2p_dbg(p2p, "Client discoverability disabled");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004531 p2p->dev_capab &= ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
4532 }
4533}
4534
4535
4536static struct wpabuf * p2p_build_presence_req(u32 duration1, u32 interval1,
4537 u32 duration2, u32 interval2)
4538{
4539 struct wpabuf *req;
4540 struct p2p_noa_desc desc1, desc2, *ptr1 = NULL, *ptr2 = NULL;
4541 u8 *len;
4542
4543 req = wpabuf_alloc(100);
4544 if (req == NULL)
4545 return NULL;
4546
4547 if (duration1 || interval1) {
4548 os_memset(&desc1, 0, sizeof(desc1));
4549 desc1.count_type = 1;
4550 desc1.duration = duration1;
4551 desc1.interval = interval1;
4552 ptr1 = &desc1;
4553
4554 if (duration2 || interval2) {
4555 os_memset(&desc2, 0, sizeof(desc2));
4556 desc2.count_type = 2;
4557 desc2.duration = duration2;
4558 desc2.interval = interval2;
4559 ptr2 = &desc2;
4560 }
4561 }
4562
4563 p2p_buf_add_action_hdr(req, P2P_PRESENCE_REQ, 1);
4564 len = p2p_buf_add_ie_hdr(req);
4565 p2p_buf_add_noa(req, 0, 0, 0, ptr1, ptr2);
4566 p2p_buf_update_ie_hdr(req, len);
4567
4568 return req;
4569}
4570
4571
4572int p2p_presence_req(struct p2p_data *p2p, const u8 *go_interface_addr,
4573 const u8 *own_interface_addr, unsigned int freq,
4574 u32 duration1, u32 interval1, u32 duration2,
4575 u32 interval2)
4576{
4577 struct wpabuf *req;
4578
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004579 p2p_dbg(p2p, "Send Presence Request to GO " MACSTR
4580 " (own interface " MACSTR ") freq=%u dur1=%u int1=%u "
4581 "dur2=%u int2=%u",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004582 MAC2STR(go_interface_addr), MAC2STR(own_interface_addr),
4583 freq, duration1, interval1, duration2, interval2);
4584
4585 req = p2p_build_presence_req(duration1, interval1, duration2,
4586 interval2);
4587 if (req == NULL)
4588 return -1;
4589
4590 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
4591 if (p2p_send_action(p2p, freq, go_interface_addr, own_interface_addr,
4592 go_interface_addr,
4593 wpabuf_head(req), wpabuf_len(req), 200) < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004594 p2p_dbg(p2p, "Failed to send Action frame");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004595 }
4596 wpabuf_free(req);
4597
4598 return 0;
4599}
4600
4601
4602static struct wpabuf * p2p_build_presence_resp(u8 status, const u8 *noa,
4603 size_t noa_len, u8 dialog_token)
4604{
4605 struct wpabuf *resp;
4606 u8 *len;
4607
4608 resp = wpabuf_alloc(100 + noa_len);
4609 if (resp == NULL)
4610 return NULL;
4611
4612 p2p_buf_add_action_hdr(resp, P2P_PRESENCE_RESP, dialog_token);
4613 len = p2p_buf_add_ie_hdr(resp);
4614 p2p_buf_add_status(resp, status);
4615 if (noa) {
4616 wpabuf_put_u8(resp, P2P_ATTR_NOTICE_OF_ABSENCE);
4617 wpabuf_put_le16(resp, noa_len);
4618 wpabuf_put_data(resp, noa, noa_len);
4619 } else
4620 p2p_buf_add_noa(resp, 0, 0, 0, NULL, NULL);
4621 p2p_buf_update_ie_hdr(resp, len);
4622
4623 return resp;
4624}
4625
4626
4627static void p2p_process_presence_req(struct p2p_data *p2p, const u8 *da,
4628 const u8 *sa, const u8 *data, size_t len,
4629 int rx_freq)
4630{
4631 struct p2p_message msg;
4632 u8 status;
4633 struct wpabuf *resp;
4634 size_t g;
4635 struct p2p_group *group = NULL;
4636 int parsed = 0;
4637 u8 noa[50];
4638 int noa_len;
4639
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004640 p2p_dbg(p2p, "Received P2P Action - P2P Presence Request");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004641
4642 for (g = 0; g < p2p->num_groups; g++) {
Sunil Ravib0ac25f2024-07-12 01:42:03 +00004643 if (ether_addr_equal(
4644 da, p2p_group_get_interface_addr(p2p->groups[g]))) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004645 group = p2p->groups[g];
4646 break;
4647 }
4648 }
4649 if (group == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004650 p2p_dbg(p2p, "Ignore P2P Presence Request for unknown group "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004651 MACSTR, MAC2STR(da));
4652 return;
4653 }
4654
4655 if (p2p_parse(data, len, &msg) < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004656 p2p_dbg(p2p, "Failed to parse P2P Presence Request");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004657 status = P2P_SC_FAIL_INVALID_PARAMS;
4658 goto fail;
4659 }
4660 parsed = 1;
4661
4662 if (msg.noa == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004663 p2p_dbg(p2p, "No NoA attribute in P2P Presence Request");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004664 status = P2P_SC_FAIL_INVALID_PARAMS;
4665 goto fail;
4666 }
4667
4668 status = p2p_group_presence_req(group, sa, msg.noa, msg.noa_len);
4669
4670fail:
4671 if (p2p->cfg->get_noa)
4672 noa_len = p2p->cfg->get_noa(p2p->cfg->cb_ctx, da, noa,
4673 sizeof(noa));
4674 else
4675 noa_len = -1;
4676 resp = p2p_build_presence_resp(status, noa_len > 0 ? noa : NULL,
4677 noa_len > 0 ? noa_len : 0,
4678 msg.dialog_token);
4679 if (parsed)
4680 p2p_parse_free(&msg);
4681 if (resp == NULL)
4682 return;
4683
4684 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
4685 if (p2p_send_action(p2p, rx_freq, sa, da, da,
4686 wpabuf_head(resp), wpabuf_len(resp), 200) < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004687 p2p_dbg(p2p, "Failed to send Action frame");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004688 }
4689 wpabuf_free(resp);
4690}
4691
4692
4693static void p2p_process_presence_resp(struct p2p_data *p2p, const u8 *da,
4694 const u8 *sa, const u8 *data, size_t len)
4695{
4696 struct p2p_message msg;
4697
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004698 p2p_dbg(p2p, "Received P2P Action - P2P Presence Response");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004699
4700 if (p2p_parse(data, len, &msg) < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004701 p2p_dbg(p2p, "Failed to parse P2P Presence Response");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004702 return;
4703 }
4704
4705 if (msg.status == NULL || msg.noa == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004706 p2p_dbg(p2p, "No Status or NoA attribute in P2P Presence Response");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004707 p2p_parse_free(&msg);
4708 return;
4709 }
4710
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004711 if (p2p->cfg->presence_resp) {
4712 p2p->cfg->presence_resp(p2p->cfg->cb_ctx, sa, *msg.status,
4713 msg.noa, msg.noa_len);
4714 }
4715
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004716 if (*msg.status) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004717 p2p_dbg(p2p, "P2P Presence Request was rejected: status %u",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004718 *msg.status);
4719 p2p_parse_free(&msg);
4720 return;
4721 }
4722
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004723 p2p_dbg(p2p, "P2P Presence Request was accepted");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004724 wpa_hexdump(MSG_DEBUG, "P2P: P2P Presence Response - NoA",
4725 msg.noa, msg.noa_len);
4726 /* TODO: process NoA */
4727 p2p_parse_free(&msg);
4728}
4729
4730
4731static void p2p_ext_listen_timeout(void *eloop_ctx, void *timeout_ctx)
4732{
4733 struct p2p_data *p2p = eloop_ctx;
4734
4735 if (p2p->ext_listen_interval) {
4736 /* Schedule next extended listen timeout */
4737 eloop_register_timeout(p2p->ext_listen_interval_sec,
4738 p2p->ext_listen_interval_usec,
4739 p2p_ext_listen_timeout, p2p, NULL);
4740 }
4741
4742 if (p2p->state == P2P_LISTEN_ONLY && p2p->ext_listen_only) {
4743 /*
4744 * This should not really happen, but it looks like the Listen
4745 * command may fail is something else (e.g., a scan) was
4746 * running at an inconvenient time. As a workaround, allow new
4747 * Extended Listen operation to be started.
4748 */
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004749 p2p_dbg(p2p, "Previous Extended Listen operation had not been completed - try again");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004750 p2p->ext_listen_only = 0;
4751 p2p_set_state(p2p, P2P_IDLE);
4752 }
4753
Sunil Ravib0ac25f2024-07-12 01:42:03 +00004754 if ((p2p->cfg->is_p2p_in_progress &&
4755 p2p->cfg->is_p2p_in_progress(p2p->cfg->cb_ctx)) ||
4756 (p2p->pending_action_state == P2P_PENDING_PD &&
4757 p2p->pd_retries > 0)) {
4758 p2p_dbg(p2p, "Operation in progress - skip Extended Listen timeout (%s)",
4759 p2p_state_txt(p2p->state));
4760 return;
4761 }
4762
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004763 if (p2p->state != P2P_IDLE) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004764 p2p_dbg(p2p, "Skip Extended Listen timeout in active state (%s)", p2p_state_txt(p2p->state));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004765 return;
4766 }
4767
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004768 p2p_dbg(p2p, "Extended Listen timeout");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004769 p2p->ext_listen_only = 1;
4770 if (p2p_listen(p2p, p2p->ext_listen_period) < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004771 p2p_dbg(p2p, "Failed to start Listen state for Extended Listen Timing");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004772 p2p->ext_listen_only = 0;
4773 }
4774}
4775
4776
4777int p2p_ext_listen(struct p2p_data *p2p, unsigned int period,
4778 unsigned int interval)
4779{
4780 if (period > 65535 || interval > 65535 || period > interval ||
4781 (period == 0 && interval > 0) || (period > 0 && interval == 0)) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004782 p2p_dbg(p2p, "Invalid Extended Listen Timing request: period=%u interval=%u",
4783 period, interval);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004784 return -1;
4785 }
4786
4787 eloop_cancel_timeout(p2p_ext_listen_timeout, p2p, NULL);
4788
4789 if (interval == 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004790 p2p_dbg(p2p, "Disabling Extended Listen Timing");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004791 p2p->ext_listen_period = 0;
4792 p2p->ext_listen_interval = 0;
4793 return 0;
4794 }
4795
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004796 p2p_dbg(p2p, "Enabling Extended Listen Timing: period %u msec, interval %u msec",
4797 period, interval);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004798 p2p->ext_listen_period = period;
4799 p2p->ext_listen_interval = interval;
4800 p2p->ext_listen_interval_sec = interval / 1000;
4801 p2p->ext_listen_interval_usec = (interval % 1000) * 1000;
4802
4803 eloop_register_timeout(p2p->ext_listen_interval_sec,
4804 p2p->ext_listen_interval_usec,
4805 p2p_ext_listen_timeout, p2p, NULL);
4806
4807 return 0;
4808}
4809
4810
4811void p2p_deauth_notif(struct p2p_data *p2p, const u8 *bssid, u16 reason_code,
4812 const u8 *ie, size_t ie_len)
4813{
4814 struct p2p_message msg;
4815
4816 if (bssid == NULL || ie == NULL)
4817 return;
4818
4819 os_memset(&msg, 0, sizeof(msg));
4820 if (p2p_parse_ies(ie, ie_len, &msg))
4821 return;
Dmitry Shmidt97672262014-02-03 13:02:54 -08004822 if (msg.minor_reason_code == NULL) {
4823 p2p_parse_free(&msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004824 return;
Dmitry Shmidt97672262014-02-03 13:02:54 -08004825 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004826
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004827 p2p_dbg(p2p, "Deauthentication notification BSSID " MACSTR
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004828 " reason_code=%u minor_reason_code=%u",
4829 MAC2STR(bssid), reason_code, *msg.minor_reason_code);
4830
4831 p2p_parse_free(&msg);
4832}
4833
4834
4835void p2p_disassoc_notif(struct p2p_data *p2p, const u8 *bssid, u16 reason_code,
4836 const u8 *ie, size_t ie_len)
4837{
4838 struct p2p_message msg;
4839
4840 if (bssid == NULL || ie == NULL)
4841 return;
4842
4843 os_memset(&msg, 0, sizeof(msg));
4844 if (p2p_parse_ies(ie, ie_len, &msg))
4845 return;
Dmitry Shmidt97672262014-02-03 13:02:54 -08004846 if (msg.minor_reason_code == NULL) {
4847 p2p_parse_free(&msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004848 return;
Dmitry Shmidt97672262014-02-03 13:02:54 -08004849 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004850
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004851 p2p_dbg(p2p, "Disassociation notification BSSID " MACSTR
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004852 " reason_code=%u minor_reason_code=%u",
4853 MAC2STR(bssid), reason_code, *msg.minor_reason_code);
4854
4855 p2p_parse_free(&msg);
4856}
4857
4858
4859void p2p_set_managed_oper(struct p2p_data *p2p, int enabled)
4860{
4861 if (enabled) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004862 p2p_dbg(p2p, "Managed P2P Device operations enabled");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004863 p2p->dev_capab |= P2P_DEV_CAPAB_INFRA_MANAGED;
4864 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004865 p2p_dbg(p2p, "Managed P2P Device operations disabled");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004866 p2p->dev_capab &= ~P2P_DEV_CAPAB_INFRA_MANAGED;
4867 }
4868}
4869
4870
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004871int p2p_config_get_random_social(struct p2p_config *p2p, u8 *op_class,
Hai Shalom74f70d42019-02-11 14:42:39 -08004872 u8 *op_channel,
4873 struct wpa_freq_range_list *avoid_list,
4874 struct wpa_freq_range_list *disallow_list)
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004875{
Hai Shalom74f70d42019-02-11 14:42:39 -08004876 return p2p_channel_random_social(&p2p->channels, op_class, op_channel,
4877 avoid_list, disallow_list);
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004878}
4879
4880
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004881int p2p_set_listen_channel(struct p2p_data *p2p, u8 reg_class, u8 channel,
4882 u8 forced)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004883{
Dmitry Shmidt4b060592013-04-29 16:42:49 -07004884 if (p2p_channel_to_freq(reg_class, channel) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004885 return -1;
4886
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004887 /*
4888 * Listen channel was set in configuration or set by control interface;
4889 * cannot override it.
4890 */
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004891 if (p2p->cfg->channel_forced && forced == 0) {
4892 p2p_dbg(p2p,
4893 "Listen channel was previously configured - do not override based on optimization");
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004894 return -1;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004895 }
4896
4897 p2p_dbg(p2p, "Set Listen channel: reg_class %u channel %u",
4898 reg_class, channel);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004899
4900 if (p2p->state == P2P_IDLE) {
4901 p2p->cfg->reg_class = reg_class;
4902 p2p->cfg->channel = channel;
4903 p2p->cfg->channel_forced = forced;
4904 } else {
4905 p2p_dbg(p2p, "Defer setting listen channel");
4906 p2p->pending_reg_class = reg_class;
4907 p2p->pending_channel = channel;
4908 p2p->pending_channel_forced = forced;
4909 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004910
4911 return 0;
4912}
4913
4914
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004915u8 p2p_get_listen_channel(struct p2p_data *p2p)
4916{
4917 return p2p->cfg->channel;
4918}
4919
4920
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004921int p2p_set_ssid_postfix(struct p2p_data *p2p, const u8 *postfix, size_t len)
4922{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004923 p2p_dbg(p2p, "New SSID postfix: %s", wpa_ssid_txt(postfix, len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004924 if (postfix == NULL) {
4925 p2p->cfg->ssid_postfix_len = 0;
4926 return 0;
4927 }
4928 if (len > sizeof(p2p->cfg->ssid_postfix))
4929 return -1;
4930 os_memcpy(p2p->cfg->ssid_postfix, postfix, len);
4931 p2p->cfg->ssid_postfix_len = len;
4932 return 0;
4933}
4934
4935
Jouni Malinen75ecf522011-06-27 15:19:46 -07004936int p2p_set_oper_channel(struct p2p_data *p2p, u8 op_reg_class, u8 op_channel,
4937 int cfg_op_channel)
4938{
Dmitry Shmidt4b060592013-04-29 16:42:49 -07004939 if (p2p_channel_to_freq(op_reg_class, op_channel) < 0)
Jouni Malinen75ecf522011-06-27 15:19:46 -07004940 return -1;
4941
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004942 p2p_dbg(p2p, "Set Operating channel: reg_class %u channel %u",
4943 op_reg_class, op_channel);
Jouni Malinen75ecf522011-06-27 15:19:46 -07004944 p2p->cfg->op_reg_class = op_reg_class;
4945 p2p->cfg->op_channel = op_channel;
4946 p2p->cfg->cfg_op_channel = cfg_op_channel;
4947 return 0;
4948}
4949
4950
Dmitry Shmidt04949592012-07-19 12:16:46 -07004951int p2p_set_pref_chan(struct p2p_data *p2p, unsigned int num_pref_chan,
4952 const struct p2p_channel *pref_chan)
4953{
4954 struct p2p_channel *n;
4955
4956 if (pref_chan) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004957 n = os_memdup(pref_chan,
4958 num_pref_chan * sizeof(struct p2p_channel));
Dmitry Shmidt04949592012-07-19 12:16:46 -07004959 if (n == NULL)
4960 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004961 } else
4962 n = NULL;
4963
4964 os_free(p2p->cfg->pref_chan);
4965 p2p->cfg->pref_chan = n;
4966 p2p->cfg->num_pref_chan = num_pref_chan;
4967
4968 return 0;
4969}
4970
4971
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004972int p2p_set_no_go_freq(struct p2p_data *p2p,
4973 const struct wpa_freq_range_list *list)
4974{
4975 struct wpa_freq_range *tmp;
4976
4977 if (list == NULL || list->num == 0) {
4978 os_free(p2p->no_go_freq.range);
4979 p2p->no_go_freq.range = NULL;
4980 p2p->no_go_freq.num = 0;
4981 return 0;
4982 }
4983
4984 tmp = os_calloc(list->num, sizeof(struct wpa_freq_range));
4985 if (tmp == NULL)
4986 return -1;
4987 os_memcpy(tmp, list->range, list->num * sizeof(struct wpa_freq_range));
4988 os_free(p2p->no_go_freq.range);
4989 p2p->no_go_freq.range = tmp;
4990 p2p->no_go_freq.num = list->num;
4991 p2p_dbg(p2p, "Updated no GO chan list");
4992
4993 return 0;
4994}
4995
4996
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004997int p2p_get_interface_addr(struct p2p_data *p2p, const u8 *dev_addr,
4998 u8 *iface_addr)
4999{
5000 struct p2p_device *dev = p2p_get_device(p2p, dev_addr);
Sunil Ravic0f5d412024-09-11 22:12:49 +00005001
5002 if (!dev || is_zero_ether_addr(dev->interface_addr)) {
5003 p2p_dbg(p2p,
5004 "P2P: Failed to get interface address from device addr "
5005 MACSTR, MAC2STR(dev_addr));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005006 return -1;
Sunil Ravic0f5d412024-09-11 22:12:49 +00005007 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005008 os_memcpy(iface_addr, dev->interface_addr, ETH_ALEN);
5009 return 0;
5010}
5011
5012
5013int p2p_get_dev_addr(struct p2p_data *p2p, const u8 *iface_addr,
5014 u8 *dev_addr)
5015{
5016 struct p2p_device *dev = p2p_get_device_interface(p2p, iface_addr);
Sunil Ravic0f5d412024-09-11 22:12:49 +00005017
5018 if (!dev) {
5019 p2p_dbg(p2p,
5020 "P2P: Failed to get device address from interface address "
5021 MACSTR, MAC2STR(iface_addr));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005022 return -1;
Sunil Ravic0f5d412024-09-11 22:12:49 +00005023 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005024 os_memcpy(dev_addr, dev->info.p2p_device_addr, ETH_ALEN);
5025 return 0;
5026}
5027
5028
5029void p2p_set_peer_filter(struct p2p_data *p2p, const u8 *addr)
5030{
5031 os_memcpy(p2p->peer_filter, addr, ETH_ALEN);
5032 if (is_zero_ether_addr(p2p->peer_filter))
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005033 p2p_dbg(p2p, "Disable peer filter");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005034 else
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005035 p2p_dbg(p2p, "Enable peer filter for " MACSTR,
5036 MAC2STR(p2p->peer_filter));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005037}
5038
5039
5040void p2p_set_cross_connect(struct p2p_data *p2p, int enabled)
5041{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005042 p2p_dbg(p2p, "Cross connection %s", enabled ? "enabled" : "disabled");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005043 if (p2p->cross_connect == enabled)
5044 return;
5045 p2p->cross_connect = enabled;
5046 /* TODO: may need to tear down any action group where we are GO(?) */
5047}
5048
5049
5050int p2p_get_oper_freq(struct p2p_data *p2p, const u8 *iface_addr)
5051{
5052 struct p2p_device *dev = p2p_get_device_interface(p2p, iface_addr);
5053 if (dev == NULL)
5054 return -1;
5055 if (dev->oper_freq <= 0)
5056 return -1;
5057 return dev->oper_freq;
5058}
5059
5060
5061void p2p_set_intra_bss_dist(struct p2p_data *p2p, int enabled)
5062{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005063 p2p_dbg(p2p, "Intra BSS distribution %s",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005064 enabled ? "enabled" : "disabled");
5065 p2p->cfg->p2p_intra_bss = enabled;
5066}
5067
5068
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005069void p2p_update_channel_list(struct p2p_data *p2p,
5070 const struct p2p_channels *chan,
5071 const struct p2p_channels *cli_chan)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005072{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005073 p2p_dbg(p2p, "Update channel list");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005074 os_memcpy(&p2p->cfg->channels, chan, sizeof(struct p2p_channels));
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005075 p2p_channels_dump(p2p, "channels", &p2p->cfg->channels);
5076 os_memcpy(&p2p->cfg->cli_channels, cli_chan,
5077 sizeof(struct p2p_channels));
5078 p2p_channels_dump(p2p, "cli_channels", &p2p->cfg->cli_channels);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005079}
5080
5081
5082int p2p_send_action(struct p2p_data *p2p, unsigned int freq, const u8 *dst,
5083 const u8 *src, const u8 *bssid, const u8 *buf,
5084 size_t len, unsigned int wait_time)
5085{
Hai Shalom021b0b52019-04-10 11:17:58 -07005086 int res, scheduled;
5087
Hai Shalom021b0b52019-04-10 11:17:58 -07005088 res = p2p->cfg->send_action(p2p->cfg->cb_ctx, freq, dst, src, bssid,
5089 buf, len, wait_time, &scheduled);
5090 if (res == 0 && scheduled && p2p->in_listen && freq > 0 &&
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08005091 p2p->drv_in_listen > 0 &&
Hai Shalom021b0b52019-04-10 11:17:58 -07005092 (unsigned int) p2p->drv_in_listen != freq) {
5093 p2p_dbg(p2p,
5094 "Stop listen on %d MHz to allow a frame to be sent immediately on %d MHz",
5095 p2p->drv_in_listen, freq);
5096 p2p_stop_listen_for_freq(p2p, freq);
5097 }
5098 return res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005099}
5100
5101
5102void p2p_set_best_channels(struct p2p_data *p2p, int freq_24, int freq_5,
5103 int freq_overall)
5104{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005105 p2p_dbg(p2p, "Best channel: 2.4 GHz: %d, 5 GHz: %d, overall: %d",
5106 freq_24, freq_5, freq_overall);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005107 p2p->best_freq_24 = freq_24;
5108 p2p->best_freq_5 = freq_5;
5109 p2p->best_freq_overall = freq_overall;
5110}
5111
5112
Dmitry Shmidt700a1372013-03-15 14:14:44 -07005113void p2p_set_own_freq_preference(struct p2p_data *p2p, int freq)
5114{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005115 p2p_dbg(p2p, "Own frequency preference: %d MHz", freq);
Dmitry Shmidt700a1372013-03-15 14:14:44 -07005116 p2p->own_freq_preference = freq;
5117}
5118
5119
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005120const u8 * p2p_get_go_neg_peer(struct p2p_data *p2p)
5121{
5122 if (p2p == NULL || p2p->go_neg_peer == NULL)
5123 return NULL;
5124 return p2p->go_neg_peer->info.p2p_device_addr;
5125}
5126
5127
5128const struct p2p_peer_info *
5129p2p_get_peer_found(struct p2p_data *p2p, const u8 *addr, int next)
5130{
5131 struct p2p_device *dev;
5132
5133 if (addr) {
5134 dev = p2p_get_device(p2p, addr);
5135 if (!dev)
5136 return NULL;
5137
5138 if (!next) {
5139 if (dev->flags & P2P_DEV_PROBE_REQ_ONLY)
5140 return NULL;
5141
5142 return &dev->info;
5143 } else {
5144 do {
5145 dev = dl_list_first(&dev->list,
5146 struct p2p_device,
5147 list);
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07005148 if (!dev || &dev->list == &p2p->devices)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005149 return NULL;
5150 } while (dev->flags & P2P_DEV_PROBE_REQ_ONLY);
5151 }
5152 } else {
5153 dev = dl_list_first(&p2p->devices, struct p2p_device, list);
5154 if (!dev)
5155 return NULL;
5156 while (dev->flags & P2P_DEV_PROBE_REQ_ONLY) {
5157 dev = dl_list_first(&dev->list,
5158 struct p2p_device,
5159 list);
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07005160 if (!dev || &dev->list == &p2p->devices)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005161 return NULL;
5162 }
5163 }
5164
5165 return &dev->info;
5166}
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005167
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005168
5169int p2p_in_progress(struct p2p_data *p2p)
5170{
5171 if (p2p == NULL)
5172 return 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005173 if (p2p->state == P2P_SEARCH)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005174 return 2;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005175 return p2p->state != P2P_IDLE && p2p->state != P2P_PROVISIONING;
5176}
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005177
5178
5179void p2p_set_config_timeout(struct p2p_data *p2p, u8 go_timeout,
5180 u8 client_timeout)
5181{
5182 if (p2p) {
5183 p2p->go_timeout = go_timeout;
5184 p2p->client_timeout = client_timeout;
5185 }
5186}
5187
5188
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005189#ifdef CONFIG_WIFI_DISPLAY
5190
5191static void p2p_update_wfd_ie_groups(struct p2p_data *p2p)
5192{
5193 size_t g;
5194 struct p2p_group *group;
5195
5196 for (g = 0; g < p2p->num_groups; g++) {
5197 group = p2p->groups[g];
Dmitry Shmidtb96dad42013-11-05 10:07:29 -08005198 p2p_group_force_beacon_update_ies(group);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005199 }
5200}
5201
5202
5203int p2p_set_wfd_ie_beacon(struct p2p_data *p2p, struct wpabuf *ie)
5204{
5205 wpabuf_free(p2p->wfd_ie_beacon);
5206 p2p->wfd_ie_beacon = ie;
5207 p2p_update_wfd_ie_groups(p2p);
5208 return 0;
5209}
5210
5211
5212int p2p_set_wfd_ie_probe_req(struct p2p_data *p2p, struct wpabuf *ie)
5213{
5214 wpabuf_free(p2p->wfd_ie_probe_req);
5215 p2p->wfd_ie_probe_req = ie;
5216 return 0;
5217}
5218
5219
5220int p2p_set_wfd_ie_probe_resp(struct p2p_data *p2p, struct wpabuf *ie)
5221{
5222 wpabuf_free(p2p->wfd_ie_probe_resp);
5223 p2p->wfd_ie_probe_resp = ie;
5224 p2p_update_wfd_ie_groups(p2p);
5225 return 0;
5226}
5227
5228
5229int p2p_set_wfd_ie_assoc_req(struct p2p_data *p2p, struct wpabuf *ie)
5230{
5231 wpabuf_free(p2p->wfd_ie_assoc_req);
5232 p2p->wfd_ie_assoc_req = ie;
5233 return 0;
5234}
5235
5236
5237int p2p_set_wfd_ie_invitation(struct p2p_data *p2p, struct wpabuf *ie)
5238{
5239 wpabuf_free(p2p->wfd_ie_invitation);
5240 p2p->wfd_ie_invitation = ie;
5241 return 0;
5242}
5243
5244
5245int p2p_set_wfd_ie_prov_disc_req(struct p2p_data *p2p, struct wpabuf *ie)
5246{
5247 wpabuf_free(p2p->wfd_ie_prov_disc_req);
5248 p2p->wfd_ie_prov_disc_req = ie;
5249 return 0;
5250}
5251
5252
5253int p2p_set_wfd_ie_prov_disc_resp(struct p2p_data *p2p, struct wpabuf *ie)
5254{
5255 wpabuf_free(p2p->wfd_ie_prov_disc_resp);
5256 p2p->wfd_ie_prov_disc_resp = ie;
5257 return 0;
5258}
5259
5260
5261int p2p_set_wfd_ie_go_neg(struct p2p_data *p2p, struct wpabuf *ie)
5262{
5263 wpabuf_free(p2p->wfd_ie_go_neg);
5264 p2p->wfd_ie_go_neg = ie;
5265 return 0;
5266}
5267
5268
5269int p2p_set_wfd_dev_info(struct p2p_data *p2p, const struct wpabuf *elem)
5270{
5271 wpabuf_free(p2p->wfd_dev_info);
5272 if (elem) {
5273 p2p->wfd_dev_info = wpabuf_dup(elem);
5274 if (p2p->wfd_dev_info == NULL)
5275 return -1;
5276 } else
5277 p2p->wfd_dev_info = NULL;
5278
5279 return 0;
5280}
5281
5282
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005283int p2p_set_wfd_r2_dev_info(struct p2p_data *p2p, const struct wpabuf *elem)
5284{
5285 wpabuf_free(p2p->wfd_r2_dev_info);
5286 if (elem) {
5287 p2p->wfd_r2_dev_info = wpabuf_dup(elem);
5288 if (p2p->wfd_r2_dev_info == NULL)
5289 return -1;
5290 } else
5291 p2p->wfd_r2_dev_info = NULL;
5292
5293 return 0;
5294}
5295
5296
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005297int p2p_set_wfd_assoc_bssid(struct p2p_data *p2p, const struct wpabuf *elem)
5298{
5299 wpabuf_free(p2p->wfd_assoc_bssid);
5300 if (elem) {
5301 p2p->wfd_assoc_bssid = wpabuf_dup(elem);
5302 if (p2p->wfd_assoc_bssid == NULL)
5303 return -1;
5304 } else
5305 p2p->wfd_assoc_bssid = NULL;
5306
5307 return 0;
5308}
5309
5310
5311int p2p_set_wfd_coupled_sink_info(struct p2p_data *p2p,
5312 const struct wpabuf *elem)
5313{
5314 wpabuf_free(p2p->wfd_coupled_sink_info);
5315 if (elem) {
5316 p2p->wfd_coupled_sink_info = wpabuf_dup(elem);
5317 if (p2p->wfd_coupled_sink_info == NULL)
5318 return -1;
5319 } else
5320 p2p->wfd_coupled_sink_info = NULL;
5321
5322 return 0;
5323}
5324
5325#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005326
5327
5328int p2p_set_disc_int(struct p2p_data *p2p, int min_disc_int, int max_disc_int,
5329 int max_disc_tu)
5330{
5331 if (min_disc_int > max_disc_int || min_disc_int < 0 || max_disc_int < 0)
5332 return -1;
5333
5334 p2p->min_disc_int = min_disc_int;
5335 p2p->max_disc_int = max_disc_int;
5336 p2p->max_disc_tu = max_disc_tu;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005337 p2p_dbg(p2p, "Set discoverable interval: min=%d max=%d max_tu=%d",
5338 min_disc_int, max_disc_int, max_disc_tu);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005339
5340 return 0;
5341}
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005342
5343
5344void p2p_dbg(struct p2p_data *p2p, const char *fmt, ...)
5345{
5346 va_list ap;
5347 char buf[500];
5348
5349 if (!p2p->cfg->debug_print)
5350 return;
5351
5352 va_start(ap, fmt);
5353 vsnprintf(buf, sizeof(buf), fmt, ap);
5354 buf[sizeof(buf) - 1] = '\0';
5355 va_end(ap);
5356 p2p->cfg->debug_print(p2p->cfg->cb_ctx, MSG_DEBUG, buf);
5357}
5358
5359
5360void p2p_info(struct p2p_data *p2p, const char *fmt, ...)
5361{
5362 va_list ap;
5363 char buf[500];
5364
5365 if (!p2p->cfg->debug_print)
5366 return;
5367
5368 va_start(ap, fmt);
5369 vsnprintf(buf, sizeof(buf), fmt, ap);
5370 buf[sizeof(buf) - 1] = '\0';
5371 va_end(ap);
5372 p2p->cfg->debug_print(p2p->cfg->cb_ctx, MSG_INFO, buf);
5373}
5374
5375
5376void p2p_err(struct p2p_data *p2p, const char *fmt, ...)
5377{
5378 va_list ap;
5379 char buf[500];
5380
5381 if (!p2p->cfg->debug_print)
5382 return;
5383
5384 va_start(ap, fmt);
5385 vsnprintf(buf, sizeof(buf), fmt, ap);
5386 buf[sizeof(buf) - 1] = '\0';
5387 va_end(ap);
5388 p2p->cfg->debug_print(p2p->cfg->cb_ctx, MSG_ERROR, buf);
5389}
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005390
5391
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07005392void p2p_loop_on_known_peers(struct p2p_data *p2p,
5393 void (*peer_callback)(struct p2p_peer_info *peer,
5394 void *user_data),
5395 void *user_data)
5396{
5397 struct p2p_device *dev, *n;
5398
5399 dl_list_for_each_safe(dev, n, &p2p->devices, struct p2p_device, list) {
5400 peer_callback(&dev->info, user_data);
5401 }
5402}
5403
5404
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005405#ifdef CONFIG_WPS_NFC
5406
5407static struct wpabuf * p2p_build_nfc_handover(struct p2p_data *p2p,
5408 int client_freq,
5409 const u8 *go_dev_addr,
5410 const u8 *ssid, size_t ssid_len)
5411{
5412 struct wpabuf *buf;
5413 u8 op_class, channel;
5414 enum p2p_role_indication role = P2P_DEVICE_NOT_IN_GROUP;
5415
5416 buf = wpabuf_alloc(1000);
5417 if (buf == NULL)
5418 return NULL;
5419
5420 op_class = p2p->cfg->reg_class;
5421 channel = p2p->cfg->channel;
5422
5423 p2p_buf_add_capability(buf, p2p->dev_capab &
5424 ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY, 0);
5425 p2p_buf_add_device_info(buf, p2p, NULL);
5426
5427 if (p2p->num_groups > 0) {
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07005428 int freq = p2p_group_get_freq(p2p->groups[0]);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005429 role = P2P_GO_IN_A_GROUP;
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07005430 if (p2p_freq_to_channel(freq, &op_class, &channel) < 0) {
5431 p2p_dbg(p2p,
5432 "Unknown GO operating frequency %d MHz for NFC handover",
5433 freq);
5434 wpabuf_free(buf);
5435 return NULL;
5436 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005437 } else if (client_freq > 0) {
5438 role = P2P_CLIENT_IN_A_GROUP;
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07005439 if (p2p_freq_to_channel(client_freq, &op_class, &channel) < 0) {
5440 p2p_dbg(p2p,
5441 "Unknown client operating frequency %d MHz for NFC handover",
5442 client_freq);
5443 wpabuf_free(buf);
5444 return NULL;
5445 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005446 }
5447
5448 p2p_buf_add_oob_go_neg_channel(buf, p2p->cfg->country, op_class,
5449 channel, role);
5450
5451 if (p2p->num_groups > 0) {
5452 /* Limit number of clients to avoid very long message */
5453 p2p_buf_add_group_info(p2p->groups[0], buf, 5);
5454 p2p_group_buf_add_id(p2p->groups[0], buf);
5455 } else if (client_freq > 0 &&
5456 go_dev_addr && !is_zero_ether_addr(go_dev_addr) &&
5457 ssid && ssid_len > 0) {
5458 /*
5459 * Add the optional P2P Group ID to indicate in which group this
5460 * device is a P2P Client.
5461 */
5462 p2p_buf_add_group_id(buf, go_dev_addr, ssid, ssid_len);
5463 }
5464
5465 return buf;
5466}
5467
5468
5469struct wpabuf * p2p_build_nfc_handover_req(struct p2p_data *p2p,
5470 int client_freq,
5471 const u8 *go_dev_addr,
5472 const u8 *ssid, size_t ssid_len)
5473{
5474 return p2p_build_nfc_handover(p2p, client_freq, go_dev_addr, ssid,
5475 ssid_len);
5476}
5477
5478
5479struct wpabuf * p2p_build_nfc_handover_sel(struct p2p_data *p2p,
5480 int client_freq,
5481 const u8 *go_dev_addr,
5482 const u8 *ssid, size_t ssid_len)
5483{
5484 return p2p_build_nfc_handover(p2p, client_freq, go_dev_addr, ssid,
5485 ssid_len);
5486}
5487
5488
5489int p2p_process_nfc_connection_handover(struct p2p_data *p2p,
5490 struct p2p_nfc_params *params)
5491{
5492 struct p2p_message msg;
5493 struct p2p_device *dev;
5494 const u8 *p2p_dev_addr;
5495 int freq;
5496 enum p2p_role_indication role;
5497
5498 params->next_step = NO_ACTION;
5499
5500 if (p2p_parse_ies_separate(params->wsc_attr, params->wsc_len,
5501 params->p2p_attr, params->p2p_len, &msg)) {
5502 p2p_dbg(p2p, "Failed to parse WSC/P2P attributes from NFC");
5503 p2p_parse_free(&msg);
5504 return -1;
5505 }
5506
5507 if (msg.p2p_device_addr)
5508 p2p_dev_addr = msg.p2p_device_addr;
5509 else if (msg.device_id)
5510 p2p_dev_addr = msg.device_id;
5511 else {
5512 p2p_dbg(p2p, "Ignore scan data without P2P Device Info or P2P Device Id");
5513 p2p_parse_free(&msg);
5514 return -1;
5515 }
5516
5517 if (msg.oob_dev_password) {
5518 os_memcpy(params->oob_dev_pw, msg.oob_dev_password,
5519 msg.oob_dev_password_len);
5520 params->oob_dev_pw_len = msg.oob_dev_password_len;
5521 }
5522
5523 dev = p2p_create_device(p2p, p2p_dev_addr);
5524 if (dev == NULL) {
5525 p2p_parse_free(&msg);
5526 return -1;
5527 }
5528
5529 params->peer = &dev->info;
5530
5531 os_get_reltime(&dev->last_seen);
5532 dev->flags &= ~(P2P_DEV_PROBE_REQ_ONLY | P2P_DEV_GROUP_CLIENT_ONLY);
5533 p2p_copy_wps_info(p2p, dev, 0, &msg);
5534
5535 if (!msg.oob_go_neg_channel) {
5536 p2p_dbg(p2p, "OOB GO Negotiation Channel attribute not included");
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07005537 p2p_parse_free(&msg);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005538 return -1;
5539 }
5540
5541 if (msg.oob_go_neg_channel[3] == 0 &&
5542 msg.oob_go_neg_channel[4] == 0)
5543 freq = 0;
5544 else
5545 freq = p2p_channel_to_freq(msg.oob_go_neg_channel[3],
5546 msg.oob_go_neg_channel[4]);
5547 if (freq < 0) {
5548 p2p_dbg(p2p, "Unknown peer OOB GO Neg channel");
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07005549 p2p_parse_free(&msg);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005550 return -1;
5551 }
5552 role = msg.oob_go_neg_channel[5];
5553
5554 if (role == P2P_GO_IN_A_GROUP) {
5555 p2p_dbg(p2p, "Peer OOB GO operating channel: %u MHz", freq);
5556 params->go_freq = freq;
5557 } else if (role == P2P_CLIENT_IN_A_GROUP) {
5558 p2p_dbg(p2p, "Peer (client) OOB GO operating channel: %u MHz",
5559 freq);
5560 params->go_freq = freq;
5561 } else
5562 p2p_dbg(p2p, "Peer OOB GO Neg channel: %u MHz", freq);
5563 dev->oob_go_neg_freq = freq;
5564
5565 if (!params->sel && role != P2P_GO_IN_A_GROUP) {
5566 freq = p2p_channel_to_freq(p2p->cfg->reg_class,
5567 p2p->cfg->channel);
5568 if (freq < 0) {
5569 p2p_dbg(p2p, "Own listen channel not known");
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07005570 p2p_parse_free(&msg);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005571 return -1;
5572 }
5573 p2p_dbg(p2p, "Use own Listen channel as OOB GO Neg channel: %u MHz", freq);
5574 dev->oob_go_neg_freq = freq;
5575 }
5576
5577 if (msg.group_id) {
5578 os_memcpy(params->go_dev_addr, msg.group_id, ETH_ALEN);
5579 params->go_ssid_len = msg.group_id_len - ETH_ALEN;
5580 os_memcpy(params->go_ssid, msg.group_id + ETH_ALEN,
5581 params->go_ssid_len);
5582 }
5583
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005584 if (dev->flags & P2P_DEV_USER_REJECTED) {
5585 p2p_dbg(p2p, "Do not report rejected device");
Dmitry Shmidt71757432014-06-02 13:50:35 -07005586 p2p_parse_free(&msg);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005587 return 0;
5588 }
5589
5590 if (!(dev->flags & P2P_DEV_REPORTED)) {
5591 p2p->cfg->dev_found(p2p->cfg->cb_ctx, p2p_dev_addr, &dev->info,
5592 !(dev->flags & P2P_DEV_REPORTED_ONCE));
5593 dev->flags |= P2P_DEV_REPORTED | P2P_DEV_REPORTED_ONCE;
5594 }
Dmitry Shmidt71757432014-06-02 13:50:35 -07005595 p2p_parse_free(&msg);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005596
5597 if (role == P2P_GO_IN_A_GROUP && p2p->num_groups > 0)
5598 params->next_step = BOTH_GO;
5599 else if (role == P2P_GO_IN_A_GROUP)
5600 params->next_step = JOIN_GROUP;
5601 else if (role == P2P_CLIENT_IN_A_GROUP) {
5602 dev->flags |= P2P_DEV_GROUP_CLIENT_ONLY;
5603 params->next_step = PEER_CLIENT;
5604 } else if (p2p->num_groups > 0)
5605 params->next_step = AUTH_JOIN;
5606 else if (params->sel)
5607 params->next_step = INIT_GO_NEG;
5608 else
5609 params->next_step = RESP_GO_NEG;
5610
5611 return 0;
5612}
5613
5614
5615void p2p_set_authorized_oob_dev_pw_id(struct p2p_data *p2p, u16 dev_pw_id,
5616 int go_intent,
5617 const u8 *own_interface_addr)
5618{
5619
5620 p2p->authorized_oob_dev_pw_id = dev_pw_id;
5621 if (dev_pw_id == 0) {
5622 p2p_dbg(p2p, "NFC OOB Password unauthorized for static handover");
5623 return;
5624 }
5625
5626 p2p_dbg(p2p, "NFC OOB Password (id=%u) authorized for static handover",
5627 dev_pw_id);
5628
5629 p2p->go_intent = go_intent;
5630 os_memcpy(p2p->intended_addr, own_interface_addr, ETH_ALEN);
5631}
5632
5633#endif /* CONFIG_WPS_NFC */
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07005634
5635
5636int p2p_set_passphrase_len(struct p2p_data *p2p, unsigned int len)
5637{
5638 if (len < 8 || len > 63)
5639 return -1;
5640 p2p->cfg->passphrase_len = len;
5641 return 0;
5642}
Dmitry Shmidt2e67f062014-07-16 09:55:28 -07005643
5644
5645void p2p_set_vendor_elems(struct p2p_data *p2p, struct wpabuf **vendor_elem)
5646{
5647 p2p->vendor_elem = vendor_elem;
5648}
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005649
5650
5651void p2p_go_neg_wait_timeout(void *eloop_ctx, void *timeout_ctx)
5652{
5653 struct p2p_data *p2p = eloop_ctx;
5654
5655 p2p_dbg(p2p,
5656 "Timeout on waiting peer to become ready for GO Negotiation");
5657 p2p_go_neg_failed(p2p, -1);
5658}
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005659
5660
5661void p2p_set_own_pref_freq_list(struct p2p_data *p2p,
Sunil8cd6f4d2022-06-28 18:40:46 +00005662 const struct weighted_pcl *pref_freq_list,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005663 unsigned int size)
5664{
5665 unsigned int i;
5666
5667 if (size > P2P_MAX_PREF_CHANNELS)
5668 size = P2P_MAX_PREF_CHANNELS;
5669 p2p->num_pref_freq = size;
Sunil8cd6f4d2022-06-28 18:40:46 +00005670 os_memcpy(p2p->pref_freq_list, pref_freq_list,
5671 size * sizeof(struct weighted_pcl));
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005672 for (i = 0; i < size; i++) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005673 p2p_dbg(p2p, "Own preferred frequency list[%u]=%u MHz",
Sunil8cd6f4d2022-06-28 18:40:46 +00005674 i, p2p->pref_freq_list[i].freq);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005675 }
5676}
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07005677
5678
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08005679void p2p_set_override_pref_op_chan(struct p2p_data *p2p, u8 op_class,
5680 u8 chan)
5681{
5682 p2p->override_pref_op_class = op_class;
5683 p2p->override_pref_channel = chan;
5684}
5685
5686
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07005687struct wpabuf * p2p_build_probe_resp_template(struct p2p_data *p2p,
5688 unsigned int freq)
5689{
5690 struct wpabuf *ies, *buf;
5691 u8 addr[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
5692 int ret;
5693
5694 ies = p2p_build_probe_resp_ies(p2p, NULL, 0);
5695 if (!ies) {
5696 wpa_printf(MSG_ERROR,
5697 "CTRL: Failed to build Probe Response IEs");
5698 return NULL;
5699 }
5700
5701 buf = wpabuf_alloc(200 + wpabuf_len(ies));
5702 if (!buf) {
5703 wpabuf_free(ies);
5704 return NULL;
5705 }
5706
5707 ret = p2p_build_probe_resp_buf(p2p, buf, ies, addr, freq);
5708 wpabuf_free(ies);
5709 if (ret) {
5710 wpabuf_free(buf);
5711 return NULL;
5712 }
5713
5714 return buf;
5715}
Hai Shaloma20dcd72022-02-04 13:43:00 -08005716
5717
5718bool p2p_is_peer_6ghz_capab(struct p2p_data *p2p, const u8 *addr)
5719{
5720 struct p2p_device *dev;
5721
5722 dev = p2p_get_device(p2p, addr);
5723 if (!dev)
5724 return false;
5725
Sunil Ravi77d572f2023-01-17 23:58:31 +00005726 return dev->support_6ghz;
Hai Shaloma20dcd72022-02-04 13:43:00 -08005727}
5728
5729
5730void p2p_set_6ghz_dev_capab(struct p2p_data *p2p, bool allow_6ghz)
5731{
5732 p2p->p2p_6ghz_capable = allow_6ghz;
5733 p2p->allow_6ghz = allow_6ghz;
5734 p2p_dbg(p2p, "Set 6 GHz capability to %d", allow_6ghz);
5735
5736 if (allow_6ghz)
5737 p2p->dev_capab |= P2P_DEV_CAPAB_6GHZ_BAND_CAPABLE;
5738 else
5739 p2p->dev_capab &= ~P2P_DEV_CAPAB_6GHZ_BAND_CAPABLE;
5740}
5741
5742
5743bool is_p2p_6ghz_capable(struct p2p_data *p2p)
5744{
5745 return p2p->p2p_6ghz_capable;
5746}
5747
5748
5749bool p2p_wfd_enabled(struct p2p_data *p2p)
5750{
5751#ifdef CONFIG_WIFI_DISPLAY
5752 return p2p->wfd_ie_probe_req != NULL;
5753#else /* CONFIG_WIFI_DISPLAY */
5754 return false;
5755#endif /* CONFIG_WIFI_DISPLAY */
5756}
5757
5758
5759bool p2p_peer_wfd_enabled(struct p2p_data *p2p, const u8 *peer_addr)
5760{
5761#ifdef CONFIG_WIFI_DISPLAY
5762 struct p2p_device *dev;
5763
5764 dev = p2p_get_device(p2p, peer_addr);
5765 return dev && dev->info.wfd_subelems != NULL;
5766#else /* CONFIG_WIFI_DISPLAY */
5767 return false;
5768#endif /* CONFIG_WIFI_DISPLAY */
5769}
5770
5771
5772bool is_p2p_allow_6ghz(struct p2p_data *p2p)
5773{
5774 return p2p->allow_6ghz;
5775}
5776
5777
5778void set_p2p_allow_6ghz(struct p2p_data *p2p, bool value)
5779{
5780 p2p->allow_6ghz = value;
5781}
Sunil Ravic0f5d412024-09-11 22:12:49 +00005782
5783
5784static int p2p_derive_nonce_tag(struct p2p_data *p2p)
5785{
5786 u8 dira_nonce[DEVICE_IDENTITY_NONCE_LEN];
5787 u8 dira_tag[DEVICE_MAX_HASH_LEN];
5788 u8 data[DIR_STR_LEN + DEVICE_IDENTITY_NONCE_LEN + ETH_ALEN];
5789 struct p2p_id_key *dev_ik;
5790
5791 dev_ik = &p2p->pairing_info->dev_ik;
5792
5793 if (dev_ik->cipher_version != DIRA_CIPHER_VERSION_128) {
5794 wpa_printf(MSG_INFO,
5795 "P2P: Unsupported DIRA Cipher version = %d",
5796 dev_ik->cipher_version);
5797 return -1;
5798 }
5799
5800 if (dev_ik->dik_len != DEVICE_IDENTITY_KEY_LEN) {
5801 wpa_printf(MSG_INFO, "P2P: Invalid DIK length = %zu",
5802 dev_ik->dik_len);
5803 return -1;
5804 }
5805
5806 os_memset(data, 0, sizeof(data));
5807
5808 if (os_get_random(dira_nonce, DEVICE_IDENTITY_NONCE_LEN) < 0) {
5809 wpa_printf(MSG_ERROR, "P2P: Failed to generate DIRA nonce");
5810 return -1;
5811 }
5812
5813 /* Tag = Truncate-64(HMAC-SHA-256(DevIK,
5814 * "DIR" || P2P Device Address || Nonce))
5815 */
5816 os_memcpy(data, "DIR", DIR_STR_LEN);
5817 os_memcpy(&data[DIR_STR_LEN], p2p->cfg->dev_addr, ETH_ALEN);
5818 os_memcpy(&data[DIR_STR_LEN + ETH_ALEN], dira_nonce,
5819 DEVICE_IDENTITY_NONCE_LEN);
5820
5821 if (hmac_sha256(dev_ik->dik_data, dev_ik->dik_len, data, sizeof(data),
5822 dira_tag) < 0) {
5823 wpa_printf(MSG_ERROR, "P2P: Could not derive DIRA tag");
5824 return -1;
5825 }
5826
5827 dev_ik->dira_nonce_len = DEVICE_IDENTITY_NONCE_LEN;
5828 os_memcpy(dev_ik->dira_nonce, dira_nonce, DEVICE_IDENTITY_NONCE_LEN);
5829 dev_ik->dira_tag_len = DEVICE_IDENTITY_TAG_LEN;
5830 os_memcpy(dev_ik->dira_tag, dira_tag, DEVICE_IDENTITY_TAG_LEN);
5831
5832 wpa_hexdump_key(MSG_DEBUG, "P2P: DIK", dev_ik->dik_data,
5833 dev_ik->dik_len);
5834 wpa_hexdump_key(MSG_DEBUG, "P2P: DIRA-NONCE", dev_ik->dira_nonce,
5835 dev_ik->dira_nonce_len);
5836 wpa_hexdump_key(MSG_DEBUG, "P2P: DIRA-TAG", dev_ik->dira_tag,
5837 dev_ik->dira_tag_len);
5838 return 0;
5839}
5840
5841
5842struct wpabuf * p2p_usd_elems(struct p2p_data *p2p)
5843{
5844 struct wpabuf *buf;
5845 u8 *len;
5846 u8 group_capab;
5847
5848 buf = wpabuf_alloc(1000);
5849 if (!buf)
5850 return NULL;
5851
5852 len = p2p_buf_add_ie_hdr(buf);
5853
5854 /* P2P Capability attribute */
5855 group_capab = 0;
5856 if (p2p->num_groups) {
5857 group_capab |= P2P_GROUP_CAPAB_GROUP_OWNER;
5858 if ((p2p->dev_capab & P2P_DEV_CAPAB_CONCURRENT_OPER) &&
5859 (p2p->dev_capab & P2P_DEV_CAPAB_INFRA_MANAGED) &&
5860 p2p->cross_connect)
5861 group_capab |= P2P_GROUP_CAPAB_CROSS_CONN;
5862 }
5863 if (p2p->cfg->p2p_intra_bss)
5864 group_capab |= P2P_GROUP_CAPAB_INTRA_BSS_DIST;
5865 p2p_buf_add_capability(buf, p2p->dev_capab &
5866 ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY,
5867 group_capab);
5868
5869 /* P2P Device Info attribute */
5870 p2p_buf_add_device_info(buf, p2p, NULL);
5871
5872 p2p_buf_update_ie_hdr(buf, len);
5873
5874 len = p2p_buf_add_p2p2_ie_hdr(buf);
5875
5876 /* P2P Capability Extension attribute */
5877 p2p_buf_add_pcea(buf, p2p);
5878
5879 /* P2P Pairing Bootstrapping Method attribute */
5880 p2p_buf_add_pbma(buf, p2p->cfg->pairing_config.bootstrap_methods, NULL,
5881 0, 0);
5882
5883 /* P2P Device Identity Resolution attribute */
5884 if (p2p->pairing_info &&
5885 p2p->cfg->pairing_config.pairing_capable &&
5886 p2p->cfg->pairing_config.enable_pairing_cache &&
5887 p2p->cfg->pairing_config.enable_pairing_verification &&
5888 p2p_derive_nonce_tag(p2p) == 0)
5889 p2p_buf_add_dira(buf, p2p);
5890
5891 p2p_buf_update_ie_hdr(buf, len);
5892
5893 return buf;
5894}
5895
5896
5897void p2p_process_usd_elems(struct p2p_data *p2p, const u8 *ies, u16 ies_len,
5898 const u8 *peer_addr, unsigned int freq)
5899{
5900 struct p2p_device *dev;
5901 struct p2p_message msg;
5902 const u8 *p2p_dev_addr;
5903
5904 os_memset(&msg, 0, sizeof(msg));
5905 if (p2p_parse_ies(ies, ies_len, &msg)) {
5906 p2p_dbg(p2p, "Failed to parse P2P IE for a device entry");
5907 p2p_parse_free(&msg);
5908 return;
5909 }
5910 if (msg.p2p_device_addr)
5911 p2p_dev_addr = msg.p2p_device_addr;
5912 else
5913 p2p_dev_addr = peer_addr;
5914
5915 dev = p2p_create_device(p2p, p2p_dev_addr);
5916 if (!dev) {
5917 p2p_parse_free(&msg);
5918 p2p_dbg(p2p, "Failed to add a peer P2P Device");
5919 return;
5920 }
5921
5922 dev->p2p2 = true;
5923 /* Reset info from old IEs */
5924 dev->info.reg_info = 0;
5925 os_memset(&dev->info.pairing_config, 0,
5926 sizeof(struct p2p_pairing_config));
5927
5928 os_get_reltime(&dev->last_seen);
5929 dev->listen_freq = freq;
5930 dev->oper_freq = freq;
5931
5932 if (msg.capability) {
5933 /*
5934 * P2P Client Discoverability bit is reserved in all frames
5935 * that use this function, so do not change its value here.
5936 */
5937 dev->info.dev_capab &= P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
5938 dev->info.dev_capab |= msg.capability[0] &
5939 ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
5940 dev->info.group_capab = msg.capability[1];
5941 }
5942
5943 if (msg.pcea_info && msg.pcea_info_len >= 2)
5944 p2p_process_pcea(p2p, &msg, dev);
5945
5946 if (msg.pbma_info && msg.pbma_info_len == 2)
5947 dev->info.pairing_config.bootstrap_methods =
5948 WPA_GET_LE16(msg.pbma_info);
5949
5950 if (!ether_addr_equal(peer_addr, p2p_dev_addr))
5951 os_memcpy(dev->interface_addr, peer_addr, ETH_ALEN);
5952
5953 p2p_dbg(p2p, "Updated device entry based on USD frame: " MACSTR
5954 " dev_capab=0x%x group_capab=0x%x listen_freq=%d",
5955 MAC2STR(dev->info.p2p_device_addr), dev->info.dev_capab,
5956 dev->info.group_capab, dev->listen_freq);
5957
5958 p2p->cfg->dev_found(p2p->cfg->cb_ctx, dev->info.p2p_device_addr,
5959 &dev->info, !(dev->flags & P2P_DEV_REPORTED_ONCE));
5960
5961 p2p_parse_free(&msg);
5962}