blob: b7fde1ba7f885c469fd711a83ef15935c3c76585 [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * Wi-Fi Direct - P2P module
3 * Copyright (c) 2009-2010, Atheros Communications
4 *
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007 */
8
9#include "includes.h"
10
Jimmy Chen1b737ee2020-11-20 01:24:12 +080011#include <log/log.h>
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070012#include "common.h"
13#include "eloop.h"
Dmitry Shmidt9c175262016-03-03 10:20:07 -080014#include "common/defs.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070015#include "common/ieee802_11_defs.h"
16#include "common/ieee802_11_common.h"
Dmitry Shmidt2e67f062014-07-16 09:55:28 -070017#include "common/wpa_ctrl.h"
Dmitry Shmidt216983b2015-02-06 10:50:36 -080018#include "crypto/sha256.h"
19#include "crypto/crypto.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070020#include "wps/wps_i.h"
21#include "p2p_i.h"
22#include "p2p.h"
23
24
25static void p2p_state_timeout(void *eloop_ctx, void *timeout_ctx);
26static void p2p_device_free(struct p2p_data *p2p, struct p2p_device *dev);
27static void p2p_process_presence_req(struct p2p_data *p2p, const u8 *da,
28 const u8 *sa, const u8 *data, size_t len,
29 int rx_freq);
30static void p2p_process_presence_resp(struct p2p_data *p2p, const u8 *da,
31 const u8 *sa, const u8 *data,
32 size_t len);
33static void p2p_ext_listen_timeout(void *eloop_ctx, void *timeout_ctx);
34static void p2p_scan_timeout(void *eloop_ctx, void *timeout_ctx);
35
36
37/*
38 * p2p_scan recovery timeout
39 *
40 * Many drivers are using 30 second timeout on scan results. Allow a bit larger
41 * timeout for this to avoid hitting P2P timeout unnecessarily.
42 */
43#define P2P_SCAN_TIMEOUT 35
44
45/**
46 * P2P_PEER_EXPIRATION_AGE - Number of seconds after which inactive peer
47 * entries will be removed
48 */
Dmitry Shmidt2093d062014-01-17 10:58:50 -080049#ifndef P2P_PEER_EXPIRATION_AGE
50#define P2P_PEER_EXPIRATION_AGE 60
Dmitry Shmidt18463232014-01-24 12:29:41 -080051#endif /* P2P_PEER_EXPIRATION_AGE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070052
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070053
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080054void p2p_expire_peers(struct p2p_data *p2p)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070055{
56 struct p2p_device *dev, *n;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -080057 struct os_reltime now;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -080058 size_t i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070059
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -080060 os_get_reltime(&now);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070061 dl_list_for_each_safe(dev, n, &p2p->devices, struct p2p_device, list) {
62 if (dev->last_seen.sec + P2P_PEER_EXPIRATION_AGE >= now.sec)
63 continue;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -080064
Dmitry Shmidt9e3f8ee2014-01-17 10:52:01 -080065 if (dev == p2p->go_neg_peer) {
66 /*
67 * GO Negotiation is in progress with the peer, so
68 * don't expire the peer entry until GO Negotiation
69 * fails or times out.
70 */
71 continue;
72 }
73
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -080074 if (p2p->cfg->go_connected &&
75 p2p->cfg->go_connected(p2p->cfg->cb_ctx,
76 dev->info.p2p_device_addr)) {
77 /*
78 * We are connected as a client to a group in which the
79 * peer is the GO, so do not expire the peer entry.
80 */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -080081 os_get_reltime(&dev->last_seen);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -080082 continue;
83 }
84
85 for (i = 0; i < p2p->num_groups; i++) {
86 if (p2p_group_is_client_connected(
87 p2p->groups[i], dev->info.p2p_device_addr))
88 break;
89 }
90 if (i < p2p->num_groups) {
91 /*
92 * The peer is connected as a client in a group where
93 * we are the GO, so do not expire the peer entry.
94 */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -080095 os_get_reltime(&dev->last_seen);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -080096 continue;
97 }
98
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -070099 p2p_dbg(p2p, "Expiring old peer entry " MACSTR,
100 MAC2STR(dev->info.p2p_device_addr));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700101 dl_list_del(&dev->list);
102 p2p_device_free(p2p, dev);
103 }
104}
105
106
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700107static const char * p2p_state_txt(int state)
108{
109 switch (state) {
110 case P2P_IDLE:
111 return "IDLE";
112 case P2P_SEARCH:
113 return "SEARCH";
114 case P2P_CONNECT:
115 return "CONNECT";
116 case P2P_CONNECT_LISTEN:
117 return "CONNECT_LISTEN";
118 case P2P_GO_NEG:
119 return "GO_NEG";
120 case P2P_LISTEN_ONLY:
121 return "LISTEN_ONLY";
122 case P2P_WAIT_PEER_CONNECT:
123 return "WAIT_PEER_CONNECT";
124 case P2P_WAIT_PEER_IDLE:
125 return "WAIT_PEER_IDLE";
126 case P2P_SD_DURING_FIND:
127 return "SD_DURING_FIND";
128 case P2P_PROVISIONING:
129 return "PROVISIONING";
130 case P2P_PD_DURING_FIND:
131 return "PD_DURING_FIND";
132 case P2P_INVITE:
133 return "INVITE";
134 case P2P_INVITE_LISTEN:
135 return "INVITE_LISTEN";
136 default:
137 return "?";
138 }
139}
140
141
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700142const char * p2p_get_state_txt(struct p2p_data *p2p)
143{
144 return p2p_state_txt(p2p->state);
145}
146
147
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800148struct p2ps_advertisement * p2p_get_p2ps_adv_list(struct p2p_data *p2p)
149{
150 return p2p ? p2p->p2ps_adv_list : NULL;
151}
152
153
154void p2p_set_intended_addr(struct p2p_data *p2p, const u8 *intended_addr)
155{
156 if (p2p && intended_addr)
157 os_memcpy(p2p->intended_addr, intended_addr, ETH_ALEN);
158}
159
160
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800161u16 p2p_get_provisioning_info(struct p2p_data *p2p, const u8 *addr)
162{
163 struct p2p_device *dev = NULL;
164
165 if (!addr || !p2p)
166 return 0;
167
168 dev = p2p_get_device(p2p, addr);
169 if (dev)
170 return dev->wps_prov_info;
171 else
172 return 0;
173}
174
175
Dmitry Shmidt04949592012-07-19 12:16:46 -0700176void p2p_clear_provisioning_info(struct p2p_data *p2p, const u8 *addr)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800177{
178 struct p2p_device *dev = NULL;
179
Dmitry Shmidt04949592012-07-19 12:16:46 -0700180 if (!addr || !p2p)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800181 return;
182
Dmitry Shmidt04949592012-07-19 12:16:46 -0700183 dev = p2p_get_device(p2p, addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800184 if (dev)
185 dev->wps_prov_info = 0;
186}
187
188
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700189void p2p_set_state(struct p2p_data *p2p, int new_state)
190{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700191 p2p_dbg(p2p, "State %s -> %s",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700192 p2p_state_txt(p2p->state), p2p_state_txt(new_state));
193 p2p->state = new_state;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700194
195 if (new_state == P2P_IDLE && p2p->pending_channel) {
196 p2p_dbg(p2p, "Apply change in listen channel");
197 p2p->cfg->reg_class = p2p->pending_reg_class;
198 p2p->cfg->channel = p2p->pending_channel;
199 p2p->pending_reg_class = 0;
200 p2p->pending_channel = 0;
201 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700202}
203
204
205void p2p_set_timeout(struct p2p_data *p2p, unsigned int sec, unsigned int usec)
206{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700207 p2p_dbg(p2p, "Set timeout (state=%s): %u.%06u sec",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700208 p2p_state_txt(p2p->state), sec, usec);
209 eloop_cancel_timeout(p2p_state_timeout, p2p, NULL);
210 eloop_register_timeout(sec, usec, p2p_state_timeout, p2p, NULL);
211}
212
213
214void p2p_clear_timeout(struct p2p_data *p2p)
215{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700216 p2p_dbg(p2p, "Clear timeout (state=%s)", p2p_state_txt(p2p->state));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700217 eloop_cancel_timeout(p2p_state_timeout, p2p, NULL);
218}
219
220
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800221void p2p_go_neg_failed(struct p2p_data *p2p, int status)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700222{
223 struct p2p_go_neg_results res;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800224 struct p2p_device *peer = p2p->go_neg_peer;
225
226 if (!peer)
227 return;
228
229 eloop_cancel_timeout(p2p_go_neg_wait_timeout, p2p, NULL);
230 if (p2p->state != P2P_SEARCH) {
231 /*
232 * Clear timeouts related to GO Negotiation if no new p2p_find
233 * has been started.
234 */
235 p2p_clear_timeout(p2p);
236 p2p_set_state(p2p, P2P_IDLE);
Dmitry Shmidt8c652892013-03-01 10:14:01 -0800237 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800238
239 peer->flags &= ~P2P_DEV_PEER_WAITING_RESPONSE;
240 peer->wps_method = WPS_NOT_READY;
241 peer->oob_pw_id = 0;
242 wpabuf_free(peer->go_neg_conf);
243 peer->go_neg_conf = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700244 p2p->go_neg_peer = NULL;
245
246 os_memset(&res, 0, sizeof(res));
247 res.status = status;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800248 os_memcpy(res.peer_device_addr, peer->info.p2p_device_addr, ETH_ALEN);
249 os_memcpy(res.peer_interface_addr, peer->intended_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700250 p2p->cfg->go_neg_completed(p2p->cfg->cb_ctx, &res);
251}
252
253
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800254static void p2p_listen_in_find(struct p2p_data *p2p, int dev_disc)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700255{
256 unsigned int r, tu;
257 int freq;
258 struct wpabuf *ies;
259
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700260 p2p_dbg(p2p, "Starting short listen state (state=%s)",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700261 p2p_state_txt(p2p->state));
262
Dmitry Shmidt3c57b3f2014-05-22 15:13:07 -0700263 if (p2p->pending_listen_freq) {
264 /* We have a pending p2p_listen request */
265 p2p_dbg(p2p, "p2p_listen command pending already");
266 return;
267 }
268
Dmitry Shmidt4b060592013-04-29 16:42:49 -0700269 freq = p2p_channel_to_freq(p2p->cfg->reg_class, p2p->cfg->channel);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700270 if (freq < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700271 p2p_dbg(p2p, "Unknown regulatory class/channel");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700272 return;
273 }
274
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700275 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
276 r = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700277 tu = (r % ((p2p->max_disc_int - p2p->min_disc_int) + 1) +
278 p2p->min_disc_int) * 100;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800279 if (p2p->max_disc_tu >= 0 && tu > (unsigned int) p2p->max_disc_tu)
280 tu = p2p->max_disc_tu;
281 if (!dev_disc && tu < 100)
282 tu = 100; /* Need to wait in non-device discovery use cases */
283 if (p2p->cfg->max_listen && 1024 * tu / 1000 > p2p->cfg->max_listen)
284 tu = p2p->cfg->max_listen * 1000 / 1024;
285
286 if (tu == 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700287 p2p_dbg(p2p, "Skip listen state since duration was 0 TU");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800288 p2p_set_timeout(p2p, 0, 0);
289 return;
290 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700291
Dmitry Shmidta3dc3092015-06-23 11:21:28 -0700292 ies = p2p_build_probe_resp_ies(p2p, NULL, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700293 if (ies == NULL)
294 return;
295
Dmitry Shmidt3c57b3f2014-05-22 15:13:07 -0700296 p2p->pending_listen_freq = freq;
297 p2p->pending_listen_sec = 0;
298 p2p->pending_listen_usec = 1024 * tu;
299
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700300 if (p2p->cfg->start_listen(p2p->cfg->cb_ctx, freq, 1024 * tu / 1000,
301 ies) < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700302 p2p_dbg(p2p, "Failed to start listen mode");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700303 p2p->pending_listen_freq = 0;
304 }
305 wpabuf_free(ies);
306}
307
308
309int p2p_listen(struct p2p_data *p2p, unsigned int timeout)
310{
311 int freq;
312 struct wpabuf *ies;
313
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700314 p2p_dbg(p2p, "Going to listen(only) state");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700315
Dmitry Shmidt3c57b3f2014-05-22 15:13:07 -0700316 if (p2p->pending_listen_freq) {
317 /* We have a pending p2p_listen request */
318 p2p_dbg(p2p, "p2p_listen command pending already");
319 return -1;
320 }
321
Dmitry Shmidt4b060592013-04-29 16:42:49 -0700322 freq = p2p_channel_to_freq(p2p->cfg->reg_class, p2p->cfg->channel);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700323 if (freq < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700324 p2p_dbg(p2p, "Unknown regulatory class/channel");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700325 return -1;
326 }
327
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700328 p2p->pending_listen_sec = timeout / 1000;
329 p2p->pending_listen_usec = (timeout % 1000) * 1000;
330
331 if (p2p->p2p_scan_running) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700332 if (p2p->start_after_scan == P2P_AFTER_SCAN_CONNECT) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700333 p2p_dbg(p2p, "p2p_scan running - connect is already pending - skip listen");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800334 return 0;
335 }
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700336 p2p_dbg(p2p, "p2p_scan running - delay start of listen state");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700337 p2p->start_after_scan = P2P_AFTER_SCAN_LISTEN;
338 return 0;
339 }
340
Dmitry Shmidta3dc3092015-06-23 11:21:28 -0700341 ies = p2p_build_probe_resp_ies(p2p, NULL, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700342 if (ies == NULL)
343 return -1;
344
Dmitry Shmidt3c57b3f2014-05-22 15:13:07 -0700345 p2p->pending_listen_freq = freq;
346
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700347 if (p2p->cfg->start_listen(p2p->cfg->cb_ctx, freq, timeout, ies) < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700348 p2p_dbg(p2p, "Failed to start listen mode");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700349 p2p->pending_listen_freq = 0;
350 wpabuf_free(ies);
351 return -1;
352 }
353 wpabuf_free(ies);
354
355 p2p_set_state(p2p, P2P_LISTEN_ONLY);
356
357 return 0;
358}
359
360
361static void p2p_device_clear_reported(struct p2p_data *p2p)
362{
363 struct p2p_device *dev;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800364 dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700365 dev->flags &= ~P2P_DEV_REPORTED;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800366 dev->sd_reqs = 0;
367 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700368}
369
370
371/**
372 * p2p_get_device - Fetch a peer entry
373 * @p2p: P2P module context from p2p_init()
374 * @addr: P2P Device Address of the peer
375 * Returns: Pointer to the device entry or %NULL if not found
376 */
377struct p2p_device * p2p_get_device(struct p2p_data *p2p, const u8 *addr)
378{
379 struct p2p_device *dev;
380 dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
381 if (os_memcmp(dev->info.p2p_device_addr, addr, ETH_ALEN) == 0)
382 return dev;
383 }
384 return NULL;
385}
386
387
388/**
389 * p2p_get_device_interface - Fetch a peer entry based on P2P Interface Address
390 * @p2p: P2P module context from p2p_init()
391 * @addr: P2P Interface Address of the peer
392 * Returns: Pointer to the device entry or %NULL if not found
393 */
394struct p2p_device * p2p_get_device_interface(struct p2p_data *p2p,
395 const u8 *addr)
396{
397 struct p2p_device *dev;
398 dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
399 if (os_memcmp(dev->interface_addr, addr, ETH_ALEN) == 0)
400 return dev;
401 }
402 return NULL;
403}
404
405
406/**
407 * p2p_create_device - Create a peer entry
408 * @p2p: P2P module context from p2p_init()
409 * @addr: P2P Device Address of the peer
410 * Returns: Pointer to the device entry or %NULL on failure
411 *
412 * If there is already an entry for the peer, it will be returned instead of
413 * creating a new one.
414 */
415static struct p2p_device * p2p_create_device(struct p2p_data *p2p,
416 const u8 *addr)
417{
418 struct p2p_device *dev, *oldest = NULL;
419 size_t count = 0;
420
421 dev = p2p_get_device(p2p, addr);
422 if (dev)
423 return dev;
424
425 dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
426 count++;
427 if (oldest == NULL ||
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800428 os_reltime_before(&dev->last_seen, &oldest->last_seen))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700429 oldest = dev;
430 }
431 if (count + 1 > p2p->cfg->max_peers && oldest) {
Hai Shaloma20dcd72022-02-04 13:43:00 -0800432 p2p_dbg(p2p,
433 "Remove oldest peer entry to make room for a new peer "
434 MACSTR, MAC2STR(oldest->info.p2p_device_addr));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700435 dl_list_del(&oldest->list);
436 p2p_device_free(p2p, oldest);
437 }
438
439 dev = os_zalloc(sizeof(*dev));
440 if (dev == NULL)
441 return NULL;
442 dl_list_add(&p2p->devices, &dev->list);
443 os_memcpy(dev->info.p2p_device_addr, addr, ETH_ALEN);
444
445 return dev;
446}
447
448
449static void p2p_copy_client_info(struct p2p_device *dev,
450 struct p2p_client_info *cli)
451{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800452 p2p_copy_filter_devname(dev->info.device_name,
453 sizeof(dev->info.device_name),
454 cli->dev_name, cli->dev_name_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700455 dev->info.dev_capab = cli->dev_capab;
456 dev->info.config_methods = cli->config_methods;
457 os_memcpy(dev->info.pri_dev_type, cli->pri_dev_type, 8);
458 dev->info.wps_sec_dev_type_list_len = 8 * cli->num_sec_dev_types;
Jimmy Chen1b737ee2020-11-20 01:24:12 +0800459 if (dev->info.wps_sec_dev_type_list_len > WPS_SEC_DEV_TYPE_MAX_LEN) {
460 android_errorWriteLog(0x534e4554, "172937525");
Jimmy Chene12b6972020-11-09 11:43:12 +0200461 dev->info.wps_sec_dev_type_list_len = WPS_SEC_DEV_TYPE_MAX_LEN;
Jimmy Chen1b737ee2020-11-20 01:24:12 +0800462 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700463 os_memcpy(dev->info.wps_sec_dev_type_list, cli->sec_dev_types,
464 dev->info.wps_sec_dev_type_list_len);
465}
466
467
468static int p2p_add_group_clients(struct p2p_data *p2p, const u8 *go_dev_addr,
469 const u8 *go_interface_addr, int freq,
Dmitry Shmidta3dc3092015-06-23 11:21:28 -0700470 const u8 *gi, size_t gi_len,
471 struct os_reltime *rx_time)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700472{
473 struct p2p_group_info info;
474 size_t c;
475 struct p2p_device *dev;
476
477 if (gi == NULL)
478 return 0;
479
480 if (p2p_group_info_parse(gi, gi_len, &info) < 0)
481 return -1;
482
483 /*
484 * Clear old data for this group; if the devices are still in the
485 * group, the information will be restored in the loop following this.
486 */
487 dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800488 if (os_memcmp(dev->member_in_go_iface, go_interface_addr,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700489 ETH_ALEN) == 0) {
490 os_memset(dev->member_in_go_iface, 0, ETH_ALEN);
491 os_memset(dev->member_in_go_dev, 0, ETH_ALEN);
492 }
493 }
494
495 for (c = 0; c < info.num_clients; c++) {
496 struct p2p_client_info *cli = &info.client[c];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800497 if (os_memcmp(cli->p2p_device_addr, p2p->cfg->dev_addr,
498 ETH_ALEN) == 0)
499 continue; /* ignore our own entry */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700500 dev = p2p_get_device(p2p, cli->p2p_device_addr);
501 if (dev) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700502 if (dev->flags & (P2P_DEV_GROUP_CLIENT_ONLY |
Dmitry Shmidt04949592012-07-19 12:16:46 -0700503 P2P_DEV_PROBE_REQ_ONLY)) {
504 /*
505 * Update information since we have not
506 * received this directly from the client.
507 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700508 p2p_copy_client_info(dev, cli);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700509 } else {
510 /*
511 * Need to update P2P Client Discoverability
512 * flag since it is valid only in P2P Group
513 * Info attribute.
514 */
515 dev->info.dev_capab &=
516 ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
517 dev->info.dev_capab |=
518 cli->dev_capab &
519 P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
520 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700521 if (dev->flags & P2P_DEV_PROBE_REQ_ONLY) {
522 dev->flags &= ~P2P_DEV_PROBE_REQ_ONLY;
523 }
524 } else {
525 dev = p2p_create_device(p2p, cli->p2p_device_addr);
526 if (dev == NULL)
527 continue;
528 dev->flags |= P2P_DEV_GROUP_CLIENT_ONLY;
529 p2p_copy_client_info(dev, cli);
530 dev->oper_freq = freq;
531 p2p->cfg->dev_found(p2p->cfg->cb_ctx,
532 dev->info.p2p_device_addr,
533 &dev->info, 1);
534 dev->flags |= P2P_DEV_REPORTED | P2P_DEV_REPORTED_ONCE;
535 }
536
537 os_memcpy(dev->interface_addr, cli->p2p_interface_addr,
538 ETH_ALEN);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -0700539 os_memcpy(&dev->last_seen, rx_time, sizeof(struct os_reltime));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700540 os_memcpy(dev->member_in_go_dev, go_dev_addr, ETH_ALEN);
541 os_memcpy(dev->member_in_go_iface, go_interface_addr,
542 ETH_ALEN);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -0700543 dev->flags |= P2P_DEV_LAST_SEEN_AS_GROUP_CLIENT;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700544 }
545
546 return 0;
547}
548
549
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700550static void p2p_copy_wps_info(struct p2p_data *p2p, struct p2p_device *dev,
551 int probe_req, const struct p2p_message *msg)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700552{
553 os_memcpy(dev->info.device_name, msg->device_name,
554 sizeof(dev->info.device_name));
555
556 if (msg->manufacturer &&
557 msg->manufacturer_len < sizeof(dev->info.manufacturer)) {
558 os_memset(dev->info.manufacturer, 0,
559 sizeof(dev->info.manufacturer));
560 os_memcpy(dev->info.manufacturer, msg->manufacturer,
561 msg->manufacturer_len);
562 }
563
564 if (msg->model_name &&
565 msg->model_name_len < sizeof(dev->info.model_name)) {
566 os_memset(dev->info.model_name, 0,
567 sizeof(dev->info.model_name));
568 os_memcpy(dev->info.model_name, msg->model_name,
569 msg->model_name_len);
570 }
571
572 if (msg->model_number &&
573 msg->model_number_len < sizeof(dev->info.model_number)) {
574 os_memset(dev->info.model_number, 0,
575 sizeof(dev->info.model_number));
576 os_memcpy(dev->info.model_number, msg->model_number,
577 msg->model_number_len);
578 }
579
580 if (msg->serial_number &&
581 msg->serial_number_len < sizeof(dev->info.serial_number)) {
582 os_memset(dev->info.serial_number, 0,
583 sizeof(dev->info.serial_number));
584 os_memcpy(dev->info.serial_number, msg->serial_number,
585 msg->serial_number_len);
586 }
587
588 if (msg->pri_dev_type)
589 os_memcpy(dev->info.pri_dev_type, msg->pri_dev_type,
590 sizeof(dev->info.pri_dev_type));
591 else if (msg->wps_pri_dev_type)
592 os_memcpy(dev->info.pri_dev_type, msg->wps_pri_dev_type,
593 sizeof(dev->info.pri_dev_type));
594
595 if (msg->wps_sec_dev_type_list) {
596 os_memcpy(dev->info.wps_sec_dev_type_list,
597 msg->wps_sec_dev_type_list,
598 msg->wps_sec_dev_type_list_len);
599 dev->info.wps_sec_dev_type_list_len =
600 msg->wps_sec_dev_type_list_len;
601 }
602
603 if (msg->capability) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700604 /*
605 * P2P Client Discoverability bit is reserved in all frames
606 * that use this function, so do not change its value here.
607 */
608 dev->info.dev_capab &= P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
609 dev->info.dev_capab |= msg->capability[0] &
610 ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700611 dev->info.group_capab = msg->capability[1];
612 }
613
614 if (msg->ext_listen_timing) {
615 dev->ext_listen_period = WPA_GET_LE16(msg->ext_listen_timing);
616 dev->ext_listen_interval =
617 WPA_GET_LE16(msg->ext_listen_timing + 2);
618 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700619
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700620 if (!probe_req) {
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800621 u16 new_config_methods;
622 new_config_methods = msg->config_methods ?
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700623 msg->config_methods : msg->wps_config_methods;
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800624 if (new_config_methods &&
625 dev->info.config_methods != new_config_methods) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700626 p2p_dbg(p2p, "Update peer " MACSTR
627 " config_methods 0x%x -> 0x%x",
628 MAC2STR(dev->info.p2p_device_addr),
629 dev->info.config_methods,
630 new_config_methods);
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800631 dev->info.config_methods = new_config_methods;
632 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700633 }
634}
635
636
Dmitry Shmidt2e67f062014-07-16 09:55:28 -0700637static void p2p_update_peer_vendor_elems(struct p2p_device *dev, const u8 *ies,
638 size_t ies_len)
639{
640 const u8 *pos, *end;
641 u8 id, len;
642
643 wpabuf_free(dev->info.vendor_elems);
644 dev->info.vendor_elems = NULL;
645
646 end = ies + ies_len;
647
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800648 for (pos = ies; end - pos > 1; pos += len) {
Dmitry Shmidt2e67f062014-07-16 09:55:28 -0700649 id = *pos++;
650 len = *pos++;
651
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800652 if (len > end - pos)
Dmitry Shmidt2e67f062014-07-16 09:55:28 -0700653 break;
654
655 if (id != WLAN_EID_VENDOR_SPECIFIC || len < 3)
656 continue;
657
658 if (len >= 4) {
659 u32 type = WPA_GET_BE32(pos);
660
661 if (type == WPA_IE_VENDOR_TYPE ||
662 type == WMM_IE_VENDOR_TYPE ||
663 type == WPS_IE_VENDOR_TYPE ||
664 type == P2P_IE_VENDOR_TYPE ||
665 type == WFD_IE_VENDOR_TYPE)
666 continue;
667 }
668
669 /* Unknown vendor element - make raw IE data available */
670 if (wpabuf_resize(&dev->info.vendor_elems, 2 + len) < 0)
671 break;
672 wpabuf_put_data(dev->info.vendor_elems, pos - 2, 2 + len);
Hai Shalom60840252021-02-19 19:02:11 -0800673 if (wpabuf_size(dev->info.vendor_elems) > 2000)
674 break;
Dmitry Shmidt2e67f062014-07-16 09:55:28 -0700675 }
676}
677
678
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800679static int p2p_compare_wfd_info(struct p2p_device *dev,
680 const struct p2p_message *msg)
681{
682 if (dev->info.wfd_subelems && msg->wfd_subelems) {
683 if (dev->info.wfd_subelems->used != msg->wfd_subelems->used)
684 return 1;
685
686 return os_memcmp(dev->info.wfd_subelems->buf,
687 msg->wfd_subelems->buf,
688 dev->info.wfd_subelems->used);
689 }
690 if (dev->info.wfd_subelems || msg->wfd_subelems)
691 return 1;
692
693 return 0;
694}
695
696
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700697/**
Dmitry Shmidt04949592012-07-19 12:16:46 -0700698 * p2p_add_device - Add peer entries based on scan results or P2P frames
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700699 * @p2p: P2P module context from p2p_init()
700 * @addr: Source address of Beacon or Probe Response frame (may be either
701 * P2P Device Address or P2P Interface Address)
702 * @level: Signal level (signal strength of the received frame from the peer)
703 * @freq: Frequency on which the Beacon or Probe Response frame was received
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800704 * @rx_time: Time when the result was received
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700705 * @ies: IEs from the Beacon or Probe Response frame
706 * @ies_len: Length of ies buffer in octets
Dmitry Shmidt04949592012-07-19 12:16:46 -0700707 * @scan_res: Whether this was based on scan results
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700708 * Returns: 0 on success, -1 on failure
709 *
710 * If the scan result is for a GO, the clients in the group will also be added
711 * to the peer table. This function can also be used with some other frames
712 * like Provision Discovery Request that contains P2P Capability and P2P Device
713 * Info attributes.
714 */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800715int p2p_add_device(struct p2p_data *p2p, const u8 *addr, int freq,
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800716 struct os_reltime *rx_time, int level, const u8 *ies,
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800717 size_t ies_len, int scan_res)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700718{
719 struct p2p_device *dev;
720 struct p2p_message msg;
721 const u8 *p2p_dev_addr;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800722 int wfd_changed;
Dmitry Shmidt29333592017-01-09 12:27:11 -0800723 int dev_name_changed;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700724 int i;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800725 struct os_reltime time_now;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700726
727 os_memset(&msg, 0, sizeof(msg));
728 if (p2p_parse_ies(ies, ies_len, &msg)) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700729 p2p_dbg(p2p, "Failed to parse P2P IE for a device entry");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700730 p2p_parse_free(&msg);
731 return -1;
732 }
733
734 if (msg.p2p_device_addr)
735 p2p_dev_addr = msg.p2p_device_addr;
736 else if (msg.device_id)
737 p2p_dev_addr = msg.device_id;
738 else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700739 p2p_dbg(p2p, "Ignore scan data without P2P Device Info or P2P Device Id");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700740 p2p_parse_free(&msg);
741 return -1;
742 }
743
744 if (!is_zero_ether_addr(p2p->peer_filter) &&
745 os_memcmp(p2p_dev_addr, p2p->peer_filter, ETH_ALEN) != 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700746 p2p_dbg(p2p, "Do not add peer filter for " MACSTR
747 " due to peer filter", MAC2STR(p2p_dev_addr));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800748 p2p_parse_free(&msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700749 return 0;
750 }
751
752 dev = p2p_create_device(p2p, p2p_dev_addr);
753 if (dev == NULL) {
754 p2p_parse_free(&msg);
755 return -1;
756 }
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800757
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800758 if (rx_time == NULL) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800759 os_get_reltime(&time_now);
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800760 rx_time = &time_now;
761 }
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800762
763 /*
764 * Update the device entry only if the new peer
Dmitry Shmidta3dc3092015-06-23 11:21:28 -0700765 * entry is newer than the one previously stored, or if
766 * the device was previously seen as a P2P Client in a group
767 * and the new entry isn't older than a threshold.
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800768 */
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800769 if (dev->last_seen.sec > 0 &&
Dmitry Shmidta3dc3092015-06-23 11:21:28 -0700770 os_reltime_before(rx_time, &dev->last_seen) &&
771 (!(dev->flags & P2P_DEV_LAST_SEEN_AS_GROUP_CLIENT) ||
772 os_reltime_expired(&dev->last_seen, rx_time,
773 P2P_DEV_GROUP_CLIENT_RESP_THRESHOLD))) {
774 p2p_dbg(p2p,
775 "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 -0800776 (unsigned int) rx_time->sec,
777 (unsigned int) rx_time->usec,
778 (unsigned int) dev->last_seen.sec,
Dmitry Shmidta3dc3092015-06-23 11:21:28 -0700779 (unsigned int) dev->last_seen.usec,
780 dev->flags);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800781 p2p_parse_free(&msg);
782 return -1;
783 }
784
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800785 os_memcpy(&dev->last_seen, rx_time, sizeof(struct os_reltime));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800786
Dmitry Shmidta3dc3092015-06-23 11:21:28 -0700787 dev->flags &= ~(P2P_DEV_PROBE_REQ_ONLY | P2P_DEV_GROUP_CLIENT_ONLY |
788 P2P_DEV_LAST_SEEN_AS_GROUP_CLIENT);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700789
790 if (os_memcmp(addr, p2p_dev_addr, ETH_ALEN) != 0)
791 os_memcpy(dev->interface_addr, addr, ETH_ALEN);
792 if (msg.ssid &&
Jouni Malinenfdb708a2015-04-07 11:32:11 +0300793 msg.ssid[1] <= sizeof(dev->oper_ssid) &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700794 (msg.ssid[1] != P2P_WILDCARD_SSID_LEN ||
795 os_memcmp(msg.ssid + 2, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN)
796 != 0)) {
797 os_memcpy(dev->oper_ssid, msg.ssid + 2, msg.ssid[1]);
798 dev->oper_ssid_len = msg.ssid[1];
799 }
800
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -0700801 wpabuf_free(dev->info.p2ps_instance);
802 dev->info.p2ps_instance = NULL;
803 if (msg.adv_service_instance && msg.adv_service_instance_len)
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800804 dev->info.p2ps_instance = wpabuf_alloc_copy(
805 msg.adv_service_instance, msg.adv_service_instance_len);
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800806
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700807 if (freq >= 2412 && freq <= 2484 && msg.ds_params &&
808 *msg.ds_params >= 1 && *msg.ds_params <= 14) {
809 int ds_freq;
810 if (*msg.ds_params == 14)
811 ds_freq = 2484;
812 else
813 ds_freq = 2407 + *msg.ds_params * 5;
814 if (freq != ds_freq) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700815 p2p_dbg(p2p, "Update Listen frequency based on DS Parameter Set IE: %d -> %d MHz",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700816 freq, ds_freq);
817 freq = ds_freq;
818 }
819 }
820
Dmitry Shmidt04949592012-07-19 12:16:46 -0700821 if (dev->listen_freq && dev->listen_freq != freq && scan_res) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700822 p2p_dbg(p2p, "Update Listen frequency based on scan results ("
823 MACSTR " %d -> %d MHz (DS param %d)",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700824 MAC2STR(dev->info.p2p_device_addr), dev->listen_freq,
825 freq, msg.ds_params ? *msg.ds_params : -1);
826 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700827 if (scan_res) {
828 dev->listen_freq = freq;
829 if (msg.group_info)
830 dev->oper_freq = freq;
831 }
Jouni Malinen75ecf522011-06-27 15:19:46 -0700832 dev->info.level = level;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700833
Dmitry Shmidt29333592017-01-09 12:27:11 -0800834 dev_name_changed = os_strncmp(dev->info.device_name, msg.device_name,
835 WPS_DEV_NAME_MAX_LEN) != 0;
836
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700837 p2p_copy_wps_info(p2p, dev, 0, &msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700838
839 for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) {
840 wpabuf_free(dev->info.wps_vendor_ext[i]);
841 dev->info.wps_vendor_ext[i] = NULL;
842 }
843
844 for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) {
845 if (msg.wps_vendor_ext[i] == NULL)
846 break;
847 dev->info.wps_vendor_ext[i] = wpabuf_alloc_copy(
848 msg.wps_vendor_ext[i], msg.wps_vendor_ext_len[i]);
849 if (dev->info.wps_vendor_ext[i] == NULL)
850 break;
851 }
852
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800853 wfd_changed = p2p_compare_wfd_info(dev, &msg);
854
Dmitry Shmidt29333592017-01-09 12:27:11 -0800855 if (wfd_changed) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700856 wpabuf_free(dev->info.wfd_subelems);
Dmitry Shmidt29333592017-01-09 12:27:11 -0800857 if (msg.wfd_subelems)
858 dev->info.wfd_subelems = wpabuf_dup(msg.wfd_subelems);
859 else
860 dev->info.wfd_subelems = NULL;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700861 }
862
Dmitry Shmidt04949592012-07-19 12:16:46 -0700863 if (scan_res) {
864 p2p_add_group_clients(p2p, p2p_dev_addr, addr, freq,
Dmitry Shmidta3dc3092015-06-23 11:21:28 -0700865 msg.group_info, msg.group_info_len,
866 rx_time);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700867 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700868
869 p2p_parse_free(&msg);
870
Dmitry Shmidt2e67f062014-07-16 09:55:28 -0700871 p2p_update_peer_vendor_elems(dev, ies, ies_len);
872
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800873 if (dev->flags & P2P_DEV_REPORTED && !wfd_changed &&
Dmitry Shmidt29333592017-01-09 12:27:11 -0800874 !dev_name_changed &&
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800875 (!msg.adv_service_instance ||
876 (dev->flags & P2P_DEV_P2PS_REPORTED)))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700877 return 0;
878
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700879 p2p_dbg(p2p, "Peer found with Listen frequency %d MHz (rx_time=%u.%06u)",
880 freq, (unsigned int) rx_time->sec,
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800881 (unsigned int) rx_time->usec);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700882 if (dev->flags & P2P_DEV_USER_REJECTED) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700883 p2p_dbg(p2p, "Do not report rejected device");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700884 return 0;
885 }
886
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800887 if (dev->info.config_methods == 0 &&
888 (freq == 2412 || freq == 2437 || freq == 2462)) {
889 /*
890 * If we have only seen a Beacon frame from a GO, we do not yet
891 * know what WPS config methods it supports. Since some
892 * applications use config_methods value from P2P-DEVICE-FOUND
893 * events, postpone reporting this peer until we've fully
894 * discovered its capabilities.
895 *
896 * At least for now, do this only if the peer was detected on
897 * one of the social channels since that peer can be easily be
898 * found again and there are no limitations of having to use
899 * passive scan on this channels, so this can be done through
900 * Probe Response frame that includes the config_methods
901 * information.
902 */
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700903 p2p_dbg(p2p, "Do not report peer " MACSTR
904 " with unknown config methods", MAC2STR(addr));
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800905 return 0;
906 }
907
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700908 p2p->cfg->dev_found(p2p->cfg->cb_ctx, addr, &dev->info,
909 !(dev->flags & P2P_DEV_REPORTED_ONCE));
910 dev->flags |= P2P_DEV_REPORTED | P2P_DEV_REPORTED_ONCE;
911
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800912 if (msg.adv_service_instance)
913 dev->flags |= P2P_DEV_P2PS_REPORTED;
914
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700915 return 0;
916}
917
918
919static void p2p_device_free(struct p2p_data *p2p, struct p2p_device *dev)
920{
921 int i;
922
Dmitry Shmidt497c1d52011-07-21 15:19:46 -0700923 if (p2p->go_neg_peer == dev) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800924 /*
925 * If GO Negotiation is in progress, report that it has failed.
926 */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800927 p2p_go_neg_failed(p2p, -1);
Dmitry Shmidt497c1d52011-07-21 15:19:46 -0700928 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700929 if (p2p->invite_peer == dev)
930 p2p->invite_peer = NULL;
931 if (p2p->sd_peer == dev)
932 p2p->sd_peer = NULL;
933 if (p2p->pending_client_disc_go == dev)
934 p2p->pending_client_disc_go = NULL;
935
Dmitry Shmidtc55524a2011-07-07 11:18:38 -0700936 /* dev_lost() device, but only if it was previously dev_found() */
937 if (dev->flags & P2P_DEV_REPORTED_ONCE)
938 p2p->cfg->dev_lost(p2p->cfg->cb_ctx,
939 dev->info.p2p_device_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700940
941 for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) {
942 wpabuf_free(dev->info.wps_vendor_ext[i]);
943 dev->info.wps_vendor_ext[i] = NULL;
944 }
945
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700946 wpabuf_free(dev->info.wfd_subelems);
Dmitry Shmidt2e67f062014-07-16 09:55:28 -0700947 wpabuf_free(dev->info.vendor_elems);
Dmitry Shmidt413dde72014-04-11 10:23:22 -0700948 wpabuf_free(dev->go_neg_conf);
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800949 wpabuf_free(dev->info.p2ps_instance);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700950
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700951 os_free(dev);
952}
953
954
955static int p2p_get_next_prog_freq(struct p2p_data *p2p)
956{
957 struct p2p_channels *c;
958 struct p2p_reg_class *cla;
959 size_t cl, ch;
960 int found = 0;
961 u8 reg_class;
962 u8 channel;
963 int freq;
964
965 c = &p2p->cfg->channels;
966 for (cl = 0; cl < c->reg_classes; cl++) {
967 cla = &c->reg_class[cl];
968 if (cla->reg_class != p2p->last_prog_scan_class)
969 continue;
970 for (ch = 0; ch < cla->channels; ch++) {
971 if (cla->channel[ch] == p2p->last_prog_scan_chan) {
972 found = 1;
973 break;
974 }
975 }
976 if (found)
977 break;
978 }
979
980 if (!found) {
981 /* Start from beginning */
982 reg_class = c->reg_class[0].reg_class;
983 channel = c->reg_class[0].channel[0];
984 } else {
985 /* Pick the next channel */
986 ch++;
987 if (ch == cla->channels) {
988 cl++;
989 if (cl == c->reg_classes)
990 cl = 0;
991 ch = 0;
992 }
993 reg_class = c->reg_class[cl].reg_class;
994 channel = c->reg_class[cl].channel[ch];
995 }
996
Dmitry Shmidt4b060592013-04-29 16:42:49 -0700997 freq = p2p_channel_to_freq(reg_class, channel);
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700998 p2p_dbg(p2p, "Next progressive search channel: reg_class %u channel %u -> %d MHz",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700999 reg_class, channel, freq);
1000 p2p->last_prog_scan_class = reg_class;
1001 p2p->last_prog_scan_chan = channel;
1002
1003 if (freq == 2412 || freq == 2437 || freq == 2462)
1004 return 0; /* No need to add social channels */
1005 return freq;
1006}
1007
1008
1009static void p2p_search(struct p2p_data *p2p)
1010{
1011 int freq = 0;
1012 enum p2p_scan_type type;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001013 u16 pw_id = DEV_PW_DEFAULT;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001014 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001015
1016 if (p2p->drv_in_listen) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001017 p2p_dbg(p2p, "Driver is still in Listen state - wait for it to end before continuing");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001018 return;
1019 }
1020 p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
1021
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001022 if (p2p->find_pending_full &&
1023 (p2p->find_type == P2P_FIND_PROGRESSIVE ||
1024 p2p->find_type == P2P_FIND_START_WITH_FULL)) {
1025 type = P2P_SCAN_FULL;
1026 p2p_dbg(p2p, "Starting search (pending full scan)");
1027 p2p->find_pending_full = 0;
1028 } else if ((p2p->find_type == P2P_FIND_PROGRESSIVE &&
1029 (freq = p2p_get_next_prog_freq(p2p)) > 0) ||
1030 (p2p->find_type == P2P_FIND_START_WITH_FULL &&
1031 (freq = p2p->find_specified_freq) > 0)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001032 type = P2P_SCAN_SOCIAL_PLUS_ONE;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001033 p2p_dbg(p2p, "Starting search (+ freq %u)", freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001034 } else {
1035 type = P2P_SCAN_SOCIAL;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001036 p2p_dbg(p2p, "Starting search");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001037 }
1038
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001039 res = p2p->cfg->p2p_scan(p2p->cfg->cb_ctx, type, freq,
1040 p2p->num_req_dev_types, p2p->req_dev_types,
Hai Shaloma20dcd72022-02-04 13:43:00 -08001041 p2p->find_dev_id, pw_id, p2p->include_6ghz);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001042 if (res < 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001043 p2p_dbg(p2p, "Scan request schedule failed");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001044 p2p_continue_find(p2p);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001045 }
1046}
1047
1048
1049static void p2p_find_timeout(void *eloop_ctx, void *timeout_ctx)
1050{
1051 struct p2p_data *p2p = eloop_ctx;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001052 p2p_dbg(p2p, "Find timeout -> stop");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001053 p2p_stop_find(p2p);
1054}
1055
1056
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001057void p2p_notify_scan_trigger_status(struct p2p_data *p2p, int status)
1058{
1059 if (status != 0) {
1060 p2p_dbg(p2p, "Scan request failed");
1061 /* Do continue find even for the first p2p_find_scan */
1062 p2p_continue_find(p2p);
1063 } else {
1064 p2p_dbg(p2p, "Running p2p_scan");
1065 p2p->p2p_scan_running = 1;
1066 eloop_cancel_timeout(p2p_scan_timeout, p2p, NULL);
1067 eloop_register_timeout(P2P_SCAN_TIMEOUT, 0, p2p_scan_timeout,
1068 p2p, NULL);
1069 }
1070}
1071
1072
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001073static int p2p_run_after_scan(struct p2p_data *p2p)
1074{
1075 struct p2p_device *dev;
1076 enum p2p_after_scan op;
1077
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001078 op = p2p->start_after_scan;
1079 p2p->start_after_scan = P2P_AFTER_SCAN_NOTHING;
1080 switch (op) {
1081 case P2P_AFTER_SCAN_NOTHING:
1082 break;
1083 case P2P_AFTER_SCAN_LISTEN:
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001084 p2p_dbg(p2p, "Start previously requested Listen state");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001085 p2p_listen(p2p, p2p->pending_listen_sec * 1000 +
1086 p2p->pending_listen_usec / 1000);
1087 return 1;
1088 case P2P_AFTER_SCAN_CONNECT:
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001089 p2p_dbg(p2p, "Start previously requested connect with " MACSTR,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001090 MAC2STR(p2p->after_scan_peer));
1091 dev = p2p_get_device(p2p, p2p->after_scan_peer);
1092 if (dev == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001093 p2p_dbg(p2p, "Peer not known anymore");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001094 break;
1095 }
1096 p2p_connect_send(p2p, dev);
1097 return 1;
1098 }
1099
1100 return 0;
1101}
1102
1103
1104static void p2p_scan_timeout(void *eloop_ctx, void *timeout_ctx)
1105{
1106 struct p2p_data *p2p = eloop_ctx;
1107 int running;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001108 p2p_dbg(p2p, "p2p_scan timeout (running=%d)", p2p->p2p_scan_running);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001109 running = p2p->p2p_scan_running;
1110 /* Make sure we recover from missed scan results callback */
1111 p2p->p2p_scan_running = 0;
1112
1113 if (running)
1114 p2p_run_after_scan(p2p);
1115}
1116
1117
1118static void p2p_free_req_dev_types(struct p2p_data *p2p)
1119{
1120 p2p->num_req_dev_types = 0;
1121 os_free(p2p->req_dev_types);
1122 p2p->req_dev_types = NULL;
1123}
1124
1125
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001126static int p2ps_gen_hash(struct p2p_data *p2p, const char *str, u8 *hash)
1127{
1128 u8 buf[SHA256_MAC_LEN];
1129 char str_buf[256];
1130 const u8 *adv_array;
1131 size_t i, adv_len;
1132
1133 if (!str || !hash)
1134 return 0;
1135
1136 if (!str[0]) {
1137 os_memcpy(hash, p2p->wild_card_hash, P2PS_HASH_LEN);
1138 return 1;
1139 }
1140
1141 adv_array = (u8 *) str_buf;
1142 adv_len = os_strlen(str);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07001143 if (adv_len >= sizeof(str_buf))
1144 return 0;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001145
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001146 for (i = 0; i < adv_len; i++) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001147 if (str[i] >= 'A' && str[i] <= 'Z')
1148 str_buf[i] = str[i] - 'A' + 'a';
1149 else
1150 str_buf[i] = str[i];
1151 }
1152
1153 if (sha256_vector(1, &adv_array, &adv_len, buf))
1154 return 0;
1155
1156 os_memcpy(hash, buf, P2PS_HASH_LEN);
1157 return 1;
1158}
1159
1160
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001161int p2p_find(struct p2p_data *p2p, unsigned int timeout,
1162 enum p2p_discovery_type type,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001163 unsigned int num_req_dev_types, const u8 *req_dev_types,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001164 const u8 *dev_id, unsigned int search_delay,
Hai Shaloma20dcd72022-02-04 13:43:00 -08001165 u8 seek_count, const char **seek, int freq, bool include_6ghz)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001166{
1167 int res;
Hai Shalom74f70d42019-02-11 14:42:39 -08001168 struct os_reltime start;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001169
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001170 p2p_dbg(p2p, "Starting find (type=%d)", type);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001171 if (p2p->p2p_scan_running) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001172 p2p_dbg(p2p, "p2p_scan is already running");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001173 }
1174
1175 p2p_free_req_dev_types(p2p);
1176 if (req_dev_types && num_req_dev_types) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001177 p2p->req_dev_types = os_memdup(req_dev_types,
1178 num_req_dev_types *
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001179 WPS_DEV_TYPE_LEN);
1180 if (p2p->req_dev_types == NULL)
1181 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001182 p2p->num_req_dev_types = num_req_dev_types;
1183 }
1184
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001185 if (dev_id) {
1186 os_memcpy(p2p->find_dev_id_buf, dev_id, ETH_ALEN);
1187 p2p->find_dev_id = p2p->find_dev_id_buf;
1188 } else
1189 p2p->find_dev_id = NULL;
Hai Shaloma20dcd72022-02-04 13:43:00 -08001190 p2p->include_6ghz = p2p_wfd_enabled(p2p) && include_6ghz;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001191 if (seek_count == 0 || !seek) {
1192 /* Not an ASP search */
1193 p2p->p2ps_seek = 0;
1194 } else if (seek_count == 1 && seek && (!seek[0] || !seek[0][0])) {
1195 /*
1196 * An empty seek string means no hash values, but still an ASP
1197 * search.
1198 */
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07001199 p2p_dbg(p2p, "ASP search");
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001200 p2p->p2ps_seek_count = 0;
1201 p2p->p2ps_seek = 1;
1202 } else if (seek && seek_count <= P2P_MAX_QUERY_HASH) {
1203 u8 buf[P2PS_HASH_LEN];
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07001204 int i, count = 0;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001205
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001206 for (i = 0; i < seek_count; i++) {
1207 if (!p2ps_gen_hash(p2p, seek[i], buf))
1208 continue;
1209
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07001210 p2p_dbg(p2p, "Seek service %s hash " MACSTR,
1211 seek[i], MAC2STR(buf));
1212 os_memcpy(&p2p->p2ps_seek_hash[count * P2PS_HASH_LEN],
1213 buf, P2PS_HASH_LEN);
1214 count++;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001215 }
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07001216
1217 p2p->p2ps_seek_count = count;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001218 p2p->p2ps_seek = 1;
1219 } else {
1220 p2p->p2ps_seek_count = 0;
1221 p2p->p2ps_seek = 1;
1222 }
1223
1224 /* Special case to perform wildcard search */
1225 if (p2p->p2ps_seek_count == 0 && p2p->p2ps_seek) {
1226 p2p->p2ps_seek_count = 1;
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07001227 os_memcpy(&p2p->p2ps_seek_hash, p2p->wild_card_hash,
1228 P2PS_HASH_LEN);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001229 }
1230
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001231 p2p->start_after_scan = P2P_AFTER_SCAN_NOTHING;
1232 p2p_clear_timeout(p2p);
Dmitry Shmidtde47be72016-01-07 12:52:55 -08001233 if (p2p->pending_listen_freq) {
1234 p2p_dbg(p2p, "Clear pending_listen_freq for p2p_find");
1235 p2p->pending_listen_freq = 0;
1236 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001237 p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001238 p2p->find_pending_full = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001239 p2p->find_type = type;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001240 if (freq != 2412 && freq != 2437 && freq != 2462 && freq != 60480)
1241 p2p->find_specified_freq = freq;
1242 else
1243 p2p->find_specified_freq = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001244 p2p_device_clear_reported(p2p);
Dmitry Shmidtde47be72016-01-07 12:52:55 -08001245 os_memset(p2p->sd_query_no_ack, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001246 p2p_set_state(p2p, P2P_SEARCH);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001247 p2p->search_delay = search_delay;
1248 p2p->in_search_delay = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001249 eloop_cancel_timeout(p2p_find_timeout, p2p, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001250 p2p->last_p2p_find_timeout = timeout;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001251 if (timeout)
1252 eloop_register_timeout(timeout, 0, p2p_find_timeout,
1253 p2p, NULL);
Hai Shalom74f70d42019-02-11 14:42:39 -08001254 os_get_reltime(&start);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001255 switch (type) {
1256 case P2P_FIND_START_WITH_FULL:
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001257 if (freq > 0) {
1258 /*
1259 * Start with the specified channel and then move to
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001260 * scans for social channels and this specific channel.
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001261 */
1262 res = p2p->cfg->p2p_scan(p2p->cfg->cb_ctx,
1263 P2P_SCAN_SPECIFIC, freq,
1264 p2p->num_req_dev_types,
1265 p2p->req_dev_types, dev_id,
Hai Shaloma20dcd72022-02-04 13:43:00 -08001266 DEV_PW_DEFAULT,
1267 p2p->include_6ghz);
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001268 break;
1269 }
1270 /* fall through */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001271 case P2P_FIND_PROGRESSIVE:
1272 res = p2p->cfg->p2p_scan(p2p->cfg->cb_ctx, P2P_SCAN_FULL, 0,
1273 p2p->num_req_dev_types,
Dmitry Shmidt04949592012-07-19 12:16:46 -07001274 p2p->req_dev_types, dev_id,
Hai Shaloma20dcd72022-02-04 13:43:00 -08001275 DEV_PW_DEFAULT, p2p->include_6ghz);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001276 break;
1277 case P2P_FIND_ONLY_SOCIAL:
1278 res = p2p->cfg->p2p_scan(p2p->cfg->cb_ctx, P2P_SCAN_SOCIAL, 0,
1279 p2p->num_req_dev_types,
Dmitry Shmidt04949592012-07-19 12:16:46 -07001280 p2p->req_dev_types, dev_id,
Hai Shaloma20dcd72022-02-04 13:43:00 -08001281 DEV_PW_DEFAULT, p2p->include_6ghz);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001282 break;
1283 default:
1284 return -1;
1285 }
1286
Hai Shalom74f70d42019-02-11 14:42:39 -08001287 if (!res)
1288 p2p->find_start = start;
1289
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001290 if (res != 0 && p2p->p2p_scan_running) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001291 p2p_dbg(p2p, "Failed to start p2p_scan - another p2p_scan was already running");
1292 /* wait for the previous p2p_scan to complete */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001293 if (type == P2P_FIND_PROGRESSIVE ||
1294 (type == P2P_FIND_START_WITH_FULL && freq == 0))
1295 p2p->find_pending_full = 1;
Dmitry Shmidt96be6222014-02-13 10:16:51 -08001296 res = 0; /* do not report failure */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001297 } else if (res != 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001298 p2p_dbg(p2p, "Failed to start p2p_scan");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001299 p2p_set_state(p2p, P2P_IDLE);
1300 eloop_cancel_timeout(p2p_find_timeout, p2p, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001301 }
1302
1303 return res;
1304}
1305
1306
1307void p2p_stop_find_for_freq(struct p2p_data *p2p, int freq)
1308{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001309 p2p_dbg(p2p, "Stopping find");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001310 eloop_cancel_timeout(p2p_find_timeout, p2p, NULL);
1311 p2p_clear_timeout(p2p);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001312 if (p2p->state == P2P_SEARCH || p2p->state == P2P_SD_DURING_FIND)
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001313 p2p->cfg->find_stopped(p2p->cfg->cb_ctx);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001314
1315 p2p->p2ps_seek_count = 0;
1316
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001317 p2p_set_state(p2p, P2P_IDLE);
1318 p2p_free_req_dev_types(p2p);
1319 p2p->start_after_scan = P2P_AFTER_SCAN_NOTHING;
Dmitry Shmidt8c652892013-03-01 10:14:01 -08001320 if (p2p->go_neg_peer)
1321 p2p->go_neg_peer->flags &= ~P2P_DEV_PEER_WAITING_RESPONSE;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001322 p2p->go_neg_peer = NULL;
1323 p2p->sd_peer = NULL;
1324 p2p->invite_peer = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001325 p2p_stop_listen_for_freq(p2p, freq);
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001326 p2p->send_action_in_progress = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001327}
1328
1329
1330void p2p_stop_listen_for_freq(struct p2p_data *p2p, int freq)
1331{
Hai Shaloma20dcd72022-02-04 13:43:00 -08001332 if (freq > 0 &&
1333 ((p2p->drv_in_listen == freq && p2p->in_listen) ||
1334 p2p->pending_listen_freq == (unsigned int) freq)) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001335 p2p_dbg(p2p, "Skip stop_listen since we are on correct channel for response");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001336 return;
1337 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001338 if (p2p->in_listen) {
1339 p2p->in_listen = 0;
1340 p2p_clear_timeout(p2p);
1341 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001342 if (p2p->drv_in_listen) {
1343 /*
1344 * The driver may not deliver callback to p2p_listen_end()
1345 * when the operation gets canceled, so clear the internal
1346 * variable that is tracking driver state.
1347 */
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001348 p2p_dbg(p2p, "Clear drv_in_listen (%d)", p2p->drv_in_listen);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001349 p2p->drv_in_listen = 0;
1350 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001351 p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
1352}
1353
1354
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001355void p2p_stop_listen(struct p2p_data *p2p)
1356{
1357 if (p2p->state != P2P_LISTEN_ONLY) {
1358 p2p_dbg(p2p, "Skip stop_listen since not in listen_only state.");
1359 return;
1360 }
1361
1362 p2p_stop_listen_for_freq(p2p, 0);
1363 p2p_set_state(p2p, P2P_IDLE);
1364}
1365
1366
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001367void p2p_stop_find(struct p2p_data *p2p)
1368{
Dmitry Shmidt3c57b3f2014-05-22 15:13:07 -07001369 p2p->pending_listen_freq = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001370 p2p_stop_find_for_freq(p2p, 0);
1371}
1372
1373
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001374static int p2p_prepare_channel_pref(struct p2p_data *p2p,
1375 unsigned int force_freq,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001376 unsigned int pref_freq, int go)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001377{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001378 u8 op_class, op_channel;
1379 unsigned int freq = force_freq ? force_freq : pref_freq;
1380
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001381 p2p_dbg(p2p, "Prepare channel pref - force_freq=%u pref_freq=%u go=%d",
1382 force_freq, pref_freq, go);
Dmitry Shmidt4b060592013-04-29 16:42:49 -07001383 if (p2p_freq_to_channel(freq, &op_class, &op_channel) < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001384 p2p_dbg(p2p, "Unsupported frequency %u MHz", freq);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001385 return -1;
1386 }
1387
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001388 if (!p2p_channels_includes(&p2p->cfg->channels, op_class, op_channel) &&
1389 (go || !p2p_channels_includes(&p2p->cfg->cli_channels, op_class,
1390 op_channel))) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001391 p2p_dbg(p2p, "Frequency %u MHz (oper_class %u channel %u) not allowed for P2P",
1392 freq, op_class, op_channel);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001393 return -1;
1394 }
1395
1396 p2p->op_reg_class = op_class;
1397 p2p->op_channel = op_channel;
1398
1399 if (force_freq) {
1400 p2p->channels.reg_classes = 1;
1401 p2p->channels.reg_class[0].channels = 1;
1402 p2p->channels.reg_class[0].reg_class = p2p->op_reg_class;
1403 p2p->channels.reg_class[0].channel[0] = p2p->op_channel;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001404 } else {
Hai Shaloma20dcd72022-02-04 13:43:00 -08001405 p2p_copy_channels(&p2p->channels, &p2p->cfg->channels,
1406 p2p->allow_6ghz);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001407 }
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001408
1409 return 0;
1410}
1411
1412
1413static void p2p_prepare_channel_best(struct p2p_data *p2p)
1414{
1415 u8 op_class, op_channel;
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07001416 const int op_classes_5ghz[] = { 124, 125, 115, 0 };
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08001417 const int op_classes_ht40[] = { 126, 127, 116, 117, 0 };
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001418 const int op_classes_vht[] = { 128, 0 };
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001419 const int op_classes_edmg[] = { 181, 182, 183, 0 };
Hai Shaloma20dcd72022-02-04 13:43:00 -08001420 const int op_classes_6ghz[] = { 131, 0 };
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001421
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07001422 p2p_dbg(p2p, "Prepare channel best");
1423
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001424 if (!p2p->cfg->cfg_op_channel && p2p->best_freq_overall > 0 &&
1425 p2p_supported_freq(p2p, p2p->best_freq_overall) &&
Dmitry Shmidt4b060592013-04-29 16:42:49 -07001426 p2p_freq_to_channel(p2p->best_freq_overall, &op_class, &op_channel)
1427 == 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001428 p2p_dbg(p2p, "Select best overall channel as operating channel preference");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001429 p2p->op_reg_class = op_class;
1430 p2p->op_channel = op_channel;
1431 } else if (!p2p->cfg->cfg_op_channel && p2p->best_freq_5 > 0 &&
1432 p2p_supported_freq(p2p, p2p->best_freq_5) &&
Dmitry Shmidt4b060592013-04-29 16:42:49 -07001433 p2p_freq_to_channel(p2p->best_freq_5, &op_class, &op_channel)
1434 == 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001435 p2p_dbg(p2p, "Select best 5 GHz channel as operating channel preference");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001436 p2p->op_reg_class = op_class;
1437 p2p->op_channel = op_channel;
1438 } else if (!p2p->cfg->cfg_op_channel && p2p->best_freq_24 > 0 &&
1439 p2p_supported_freq(p2p, p2p->best_freq_24) &&
Dmitry Shmidt4b060592013-04-29 16:42:49 -07001440 p2p_freq_to_channel(p2p->best_freq_24, &op_class,
1441 &op_channel) == 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001442 p2p_dbg(p2p, "Select best 2.4 GHz channel as operating channel preference");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001443 p2p->op_reg_class = op_class;
1444 p2p->op_channel = op_channel;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07001445 } else if (p2p->cfg->num_pref_chan > 0 &&
1446 p2p_channels_includes(&p2p->cfg->channels,
1447 p2p->cfg->pref_chan[0].op_class,
1448 p2p->cfg->pref_chan[0].chan)) {
1449 p2p_dbg(p2p, "Select first pref_chan entry as operating channel preference");
1450 p2p->op_reg_class = p2p->cfg->pref_chan[0].op_class;
1451 p2p->op_channel = p2p->cfg->pref_chan[0].chan;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001452 } else if (p2p_channel_select(&p2p->cfg->channels, op_classes_edmg,
1453 &p2p->op_reg_class, &p2p->op_channel) ==
1454 0) {
1455 p2p_dbg(p2p, "Select possible EDMG channel (op_class %u channel %u) as operating channel preference",
1456 p2p->op_reg_class, p2p->op_channel);
Hai Shaloma20dcd72022-02-04 13:43:00 -08001457 } else if (p2p->allow_6ghz &&
1458 (p2p_channel_select(&p2p->cfg->channels, op_classes_6ghz,
1459 &p2p->op_reg_class, &p2p->op_channel) ==
1460 0)) {
1461 p2p_dbg(p2p, "Select possible 6 GHz channel (op_class %u channel %u) as operating channel preference",
1462 p2p->op_reg_class, p2p->op_channel);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001463 } else if (p2p_channel_select(&p2p->cfg->channels, op_classes_vht,
1464 &p2p->op_reg_class, &p2p->op_channel) ==
1465 0) {
1466 p2p_dbg(p2p, "Select possible VHT channel (op_class %u channel %u) as operating channel preference",
1467 p2p->op_reg_class, p2p->op_channel);
1468 } else if (p2p_channel_select(&p2p->cfg->channels, op_classes_ht40,
1469 &p2p->op_reg_class, &p2p->op_channel) ==
1470 0) {
1471 p2p_dbg(p2p, "Select possible HT40 channel (op_class %u channel %u) as operating channel preference",
1472 p2p->op_reg_class, p2p->op_channel);
1473 } else if (p2p_channel_select(&p2p->cfg->channels, op_classes_5ghz,
1474 &p2p->op_reg_class, &p2p->op_channel) ==
1475 0) {
1476 p2p_dbg(p2p, "Select possible 5 GHz channel (op_class %u channel %u) as operating channel preference",
1477 p2p->op_reg_class, p2p->op_channel);
Dmitry Shmidtd11f0192014-03-24 12:09:47 -07001478 } else if (p2p_channels_includes(&p2p->cfg->channels,
1479 p2p->cfg->op_reg_class,
1480 p2p->cfg->op_channel)) {
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07001481 p2p_dbg(p2p, "Select pre-configured channel as operating channel preference");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001482 p2p->op_reg_class = p2p->cfg->op_reg_class;
1483 p2p->op_channel = p2p->cfg->op_channel;
Dmitry Shmidtd11f0192014-03-24 12:09:47 -07001484 } else if (p2p_channel_random_social(&p2p->cfg->channels,
1485 &p2p->op_reg_class,
Hai Shalom74f70d42019-02-11 14:42:39 -08001486 &p2p->op_channel,
1487 NULL, NULL) == 0) {
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001488 p2p_dbg(p2p, "Select random available social channel (op_class %u channel %u) as operating channel preference",
1489 p2p->op_reg_class, p2p->op_channel);
Dmitry Shmidtd11f0192014-03-24 12:09:47 -07001490 } else {
1491 /* Select any random available channel from the first available
1492 * operating class */
1493 p2p_channel_select(&p2p->cfg->channels, NULL,
1494 &p2p->op_reg_class,
1495 &p2p->op_channel);
1496 p2p_dbg(p2p, "Select random available channel %d from operating class %d as operating channel preference",
1497 p2p->op_channel, p2p->op_reg_class);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001498 }
1499
Hai Shaloma20dcd72022-02-04 13:43:00 -08001500 p2p_copy_channels(&p2p->channels, &p2p->cfg->channels, p2p->allow_6ghz);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001501}
1502
1503
1504/**
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001505 * p2p_prepare_channel - Select operating channel for GO Negotiation or P2PS PD
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001506 * @p2p: P2P module context from p2p_init()
1507 * @dev: Selected peer device
1508 * @force_freq: Forced frequency in MHz or 0 if not forced
1509 * @pref_freq: Preferred frequency in MHz or 0 if no preference
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001510 * @go: Whether the local end will be forced to be GO
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001511 * Returns: 0 on success, -1 on failure (channel not supported for P2P)
1512 *
1513 * This function is used to do initial operating channel selection for GO
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001514 * Negotiation prior to having received peer information or for P2PS PD
1515 * signalling. The selected channel may be further optimized in
1516 * p2p_reselect_channel() once the peer information is available.
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001517 */
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08001518int p2p_prepare_channel(struct p2p_data *p2p, struct p2p_device *dev,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001519 unsigned int force_freq, unsigned int pref_freq, int go)
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001520{
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001521 p2p_dbg(p2p, "Prepare channel - force_freq=%u pref_freq=%u go=%d",
1522 force_freq, pref_freq, go);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001523 if (force_freq || pref_freq) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001524 if (p2p_prepare_channel_pref(p2p, force_freq, pref_freq, go) <
1525 0)
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001526 return -1;
1527 } else {
1528 p2p_prepare_channel_best(p2p);
1529 }
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001530 p2p_channels_dump(p2p, "prepared channels", &p2p->channels);
1531 if (go)
1532 p2p_channels_remove_freqs(&p2p->channels, &p2p->no_go_freq);
1533 else if (!force_freq)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001534 p2p_channels_union_inplace(&p2p->channels,
1535 &p2p->cfg->cli_channels);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001536 p2p_channels_dump(p2p, "after go/cli filter/add", &p2p->channels);
1537
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001538 p2p_dbg(p2p, "Own preference for operation channel: Operating Class %u Channel %u%s",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001539 p2p->op_reg_class, p2p->op_channel,
1540 force_freq ? " (forced)" : "");
1541
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001542 if (force_freq)
1543 dev->flags |= P2P_DEV_FORCE_FREQ;
1544 else
1545 dev->flags &= ~P2P_DEV_FORCE_FREQ;
1546
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001547 return 0;
1548}
1549
1550
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001551static void p2p_set_dev_persistent(struct p2p_device *dev,
1552 int persistent_group)
1553{
1554 switch (persistent_group) {
1555 case 0:
1556 dev->flags &= ~(P2P_DEV_PREFER_PERSISTENT_GROUP |
1557 P2P_DEV_PREFER_PERSISTENT_RECONN);
1558 break;
1559 case 1:
1560 dev->flags |= P2P_DEV_PREFER_PERSISTENT_GROUP;
1561 dev->flags &= ~P2P_DEV_PREFER_PERSISTENT_RECONN;
1562 break;
1563 case 2:
1564 dev->flags |= P2P_DEV_PREFER_PERSISTENT_GROUP |
1565 P2P_DEV_PREFER_PERSISTENT_RECONN;
1566 break;
1567 }
1568}
1569
1570
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001571int p2p_connect(struct p2p_data *p2p, const u8 *peer_addr,
1572 enum p2p_wps_method wps_method,
1573 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07001574 unsigned int force_freq, int persistent_group,
1575 const u8 *force_ssid, size_t force_ssid_len,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001576 int pd_before_go_neg, unsigned int pref_freq, u16 oob_pw_id)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001577{
1578 struct p2p_device *dev;
1579
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001580 p2p_dbg(p2p, "Request to start group negotiation - peer=" MACSTR
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001581 " GO Intent=%d Intended Interface Address=" MACSTR
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001582 " wps_method=%d persistent_group=%d pd_before_go_neg=%d "
Hai Shaloma20dcd72022-02-04 13:43:00 -08001583 "oob_pw_id=%u allow_6ghz=%d",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001584 MAC2STR(peer_addr), go_intent, MAC2STR(own_interface_addr),
Hai Shaloma20dcd72022-02-04 13:43:00 -08001585 wps_method, persistent_group, pd_before_go_neg, oob_pw_id,
1586 p2p->allow_6ghz);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001587
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001588 dev = p2p_get_device(p2p, peer_addr);
1589 if (dev == NULL || (dev->flags & P2P_DEV_PROBE_REQ_ONLY)) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001590 p2p_dbg(p2p, "Cannot connect to unknown P2P Device " MACSTR,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001591 MAC2STR(peer_addr));
1592 return -1;
1593 }
1594
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001595 if (p2p_prepare_channel(p2p, dev, force_freq, pref_freq,
1596 go_intent == 15) < 0)
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001597 return -1;
1598
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001599 if (dev->flags & P2P_DEV_GROUP_CLIENT_ONLY) {
1600 if (!(dev->info.dev_capab &
1601 P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY)) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001602 p2p_dbg(p2p, "Cannot connect to P2P Device " MACSTR
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001603 " that is in a group and is not discoverable",
1604 MAC2STR(peer_addr));
1605 return -1;
1606 }
1607 if (dev->oper_freq <= 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001608 p2p_dbg(p2p, "Cannot connect to P2P Device " MACSTR
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001609 " with incomplete information",
1610 MAC2STR(peer_addr));
1611 return -1;
1612 }
1613
1614 /*
1615 * First, try to connect directly. If the peer does not
1616 * acknowledge frames, assume it is sleeping and use device
1617 * discoverability via the GO at that point.
1618 */
1619 }
1620
Dmitry Shmidt04949592012-07-19 12:16:46 -07001621 p2p->ssid_set = 0;
1622 if (force_ssid) {
1623 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Forced SSID",
1624 force_ssid, force_ssid_len);
1625 os_memcpy(p2p->ssid, force_ssid, force_ssid_len);
1626 p2p->ssid_len = force_ssid_len;
1627 p2p->ssid_set = 1;
1628 }
1629
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001630 dev->flags &= ~P2P_DEV_NOT_YET_READY;
1631 dev->flags &= ~P2P_DEV_USER_REJECTED;
1632 dev->flags &= ~P2P_DEV_WAIT_GO_NEG_RESPONSE;
1633 dev->flags &= ~P2P_DEV_WAIT_GO_NEG_CONFIRM;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001634 if (pd_before_go_neg)
1635 dev->flags |= P2P_DEV_PD_BEFORE_GO_NEG;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001636 else {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001637 dev->flags &= ~P2P_DEV_PD_BEFORE_GO_NEG;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001638 /*
Dmitry Shmidt9cdf1b92013-02-27 12:58:50 -08001639 * Assign dialog token and tie breaker here to use the same
1640 * values in each retry within the same GO Negotiation exchange.
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001641 */
1642 dev->dialog_token++;
1643 if (dev->dialog_token == 0)
1644 dev->dialog_token = 1;
Dmitry Shmidt9cdf1b92013-02-27 12:58:50 -08001645 dev->tie_breaker = p2p->next_tie_breaker;
1646 p2p->next_tie_breaker = !p2p->next_tie_breaker;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001647 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001648 dev->connect_reqs = 0;
1649 dev->go_neg_req_sent = 0;
1650 dev->go_state = UNKNOWN_GO;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001651 p2p_set_dev_persistent(dev, persistent_group);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001652 p2p->go_intent = go_intent;
1653 os_memcpy(p2p->intended_addr, own_interface_addr, ETH_ALEN);
1654
1655 if (p2p->state != P2P_IDLE)
1656 p2p_stop_find(p2p);
1657
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001658 dev->wps_method = wps_method;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001659 dev->oob_pw_id = oob_pw_id;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001660 dev->status = P2P_SC_SUCCESS;
1661
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001662 if (p2p->p2p_scan_running) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001663 p2p_dbg(p2p, "p2p_scan running - delay connect send");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001664 p2p->start_after_scan = P2P_AFTER_SCAN_CONNECT;
1665 os_memcpy(p2p->after_scan_peer, peer_addr, ETH_ALEN);
1666 return 0;
1667 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001668
1669 return p2p_connect_send(p2p, dev);
1670}
1671
1672
1673int p2p_authorize(struct p2p_data *p2p, const u8 *peer_addr,
1674 enum p2p_wps_method wps_method,
1675 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07001676 unsigned int force_freq, int persistent_group,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001677 const u8 *force_ssid, size_t force_ssid_len,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001678 unsigned int pref_freq, u16 oob_pw_id)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001679{
1680 struct p2p_device *dev;
1681
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001682 p2p_dbg(p2p, "Request to authorize group negotiation - peer=" MACSTR
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001683 " GO Intent=%d Intended Interface Address=" MACSTR
Hai Shaloma20dcd72022-02-04 13:43:00 -08001684 " wps_method=%d persistent_group=%d oob_pw_id=%u allow_6ghz=%d",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001685 MAC2STR(peer_addr), go_intent, MAC2STR(own_interface_addr),
Hai Shaloma20dcd72022-02-04 13:43:00 -08001686 wps_method, persistent_group, oob_pw_id, p2p->allow_6ghz);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001687
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001688 dev = p2p_get_device(p2p, peer_addr);
1689 if (dev == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001690 p2p_dbg(p2p, "Cannot authorize unknown P2P Device " MACSTR,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001691 MAC2STR(peer_addr));
1692 return -1;
1693 }
1694
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001695 if (p2p_prepare_channel(p2p, dev, force_freq, pref_freq, go_intent ==
1696 15) < 0)
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001697 return -1;
1698
Dmitry Shmidt04949592012-07-19 12:16:46 -07001699 p2p->ssid_set = 0;
1700 if (force_ssid) {
1701 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Forced SSID",
1702 force_ssid, force_ssid_len);
1703 os_memcpy(p2p->ssid, force_ssid, force_ssid_len);
1704 p2p->ssid_len = force_ssid_len;
1705 p2p->ssid_set = 1;
1706 }
1707
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001708 dev->flags &= ~P2P_DEV_NOT_YET_READY;
1709 dev->flags &= ~P2P_DEV_USER_REJECTED;
1710 dev->go_neg_req_sent = 0;
1711 dev->go_state = UNKNOWN_GO;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001712 p2p_set_dev_persistent(dev, persistent_group);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001713 p2p->go_intent = go_intent;
1714 os_memcpy(p2p->intended_addr, own_interface_addr, ETH_ALEN);
1715
1716 dev->wps_method = wps_method;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001717 dev->oob_pw_id = oob_pw_id;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001718 dev->status = P2P_SC_SUCCESS;
1719
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001720 return 0;
1721}
1722
1723
1724void p2p_add_dev_info(struct p2p_data *p2p, const u8 *addr,
1725 struct p2p_device *dev, struct p2p_message *msg)
1726{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001727 os_get_reltime(&dev->last_seen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001728
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001729 p2p_copy_wps_info(p2p, dev, 0, msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001730
1731 if (msg->listen_channel) {
1732 int freq;
Dmitry Shmidt4b060592013-04-29 16:42:49 -07001733 freq = p2p_channel_to_freq(msg->listen_channel[3],
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001734 msg->listen_channel[4]);
1735 if (freq < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001736 p2p_dbg(p2p, "Unknown peer Listen channel: "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001737 "country=%c%c(0x%02x) reg_class=%u channel=%u",
1738 msg->listen_channel[0],
1739 msg->listen_channel[1],
1740 msg->listen_channel[2],
1741 msg->listen_channel[3],
1742 msg->listen_channel[4]);
1743 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001744 p2p_dbg(p2p, "Update peer " MACSTR
1745 " Listen channel: %u -> %u MHz",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001746 MAC2STR(dev->info.p2p_device_addr),
1747 dev->listen_freq, freq);
1748 dev->listen_freq = freq;
1749 }
1750 }
1751
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001752 if (msg->wfd_subelems) {
1753 wpabuf_free(dev->info.wfd_subelems);
1754 dev->info.wfd_subelems = wpabuf_dup(msg->wfd_subelems);
1755 }
1756
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001757 if (dev->flags & P2P_DEV_PROBE_REQ_ONLY) {
1758 dev->flags &= ~P2P_DEV_PROBE_REQ_ONLY;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001759 p2p_dbg(p2p, "Completed device entry based on data from GO Negotiation Request");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001760 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001761 p2p_dbg(p2p, "Created device entry based on GO Neg Req: "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001762 MACSTR " dev_capab=0x%x group_capab=0x%x name='%s' "
1763 "listen_freq=%d",
1764 MAC2STR(dev->info.p2p_device_addr),
1765 dev->info.dev_capab, dev->info.group_capab,
1766 dev->info.device_name, dev->listen_freq);
1767 }
1768
1769 dev->flags &= ~P2P_DEV_GROUP_CLIENT_ONLY;
1770
1771 if (dev->flags & P2P_DEV_USER_REJECTED) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001772 p2p_dbg(p2p, "Do not report rejected device");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001773 return;
1774 }
1775
1776 p2p->cfg->dev_found(p2p->cfg->cb_ctx, addr, &dev->info,
1777 !(dev->flags & P2P_DEV_REPORTED_ONCE));
1778 dev->flags |= P2P_DEV_REPORTED | P2P_DEV_REPORTED_ONCE;
1779}
1780
1781
1782void p2p_build_ssid(struct p2p_data *p2p, u8 *ssid, size_t *ssid_len)
1783{
1784 os_memcpy(ssid, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN);
1785 p2p_random((char *) &ssid[P2P_WILDCARD_SSID_LEN], 2);
1786 os_memcpy(&ssid[P2P_WILDCARD_SSID_LEN + 2],
1787 p2p->cfg->ssid_postfix, p2p->cfg->ssid_postfix_len);
1788 *ssid_len = P2P_WILDCARD_SSID_LEN + 2 + p2p->cfg->ssid_postfix_len;
1789}
1790
1791
1792int p2p_go_params(struct p2p_data *p2p, struct p2p_go_neg_results *params)
1793{
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001794 if (p2p->ssid_set) {
1795 os_memcpy(params->ssid, p2p->ssid, p2p->ssid_len);
1796 params->ssid_len = p2p->ssid_len;
1797 } else {
1798 p2p_build_ssid(p2p, params->ssid, &params->ssid_len);
1799 }
1800 p2p->ssid_set = 0;
1801
Jimmy Chen6d7e3902018-11-20 10:15:16 +08001802 if (p2p->passphrase_set) {
1803 os_memcpy(params->passphrase, p2p->passphrase, os_strlen(p2p->passphrase));
1804 } else {
1805 p2p_random(params->passphrase, p2p->cfg->passphrase_len);
Sunil8cd6f4d2022-06-28 18:40:46 +00001806 params->passphrase[p2p->cfg->passphrase_len] = '\0';
Jimmy Chen6d7e3902018-11-20 10:15:16 +08001807 }
1808 p2p->passphrase_set = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001809 return 0;
1810}
1811
1812
1813void p2p_go_complete(struct p2p_data *p2p, struct p2p_device *peer)
1814{
1815 struct p2p_go_neg_results res;
1816 int go = peer->go_state == LOCAL_GO;
1817 struct p2p_channels intersection;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001818
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001819 p2p_dbg(p2p, "GO Negotiation with " MACSTR " completed (%s will be GO)",
1820 MAC2STR(peer->info.p2p_device_addr), go ? "local end" : "peer");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001821
1822 os_memset(&res, 0, sizeof(res));
1823 res.role_go = go;
1824 os_memcpy(res.peer_device_addr, peer->info.p2p_device_addr, ETH_ALEN);
1825 os_memcpy(res.peer_interface_addr, peer->intended_addr, ETH_ALEN);
1826 res.wps_method = peer->wps_method;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001827 if (peer->flags & P2P_DEV_PREFER_PERSISTENT_GROUP) {
1828 if (peer->flags & P2P_DEV_PREFER_PERSISTENT_RECONN)
1829 res.persistent_group = 2;
1830 else
1831 res.persistent_group = 1;
1832 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001833
1834 if (go) {
1835 /* Setup AP mode for WPS provisioning */
Dmitry Shmidt4b060592013-04-29 16:42:49 -07001836 res.freq = p2p_channel_to_freq(p2p->op_reg_class,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001837 p2p->op_channel);
1838 os_memcpy(res.ssid, p2p->ssid, p2p->ssid_len);
1839 res.ssid_len = p2p->ssid_len;
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07001840 p2p_random(res.passphrase, p2p->cfg->passphrase_len);
Sunil8cd6f4d2022-06-28 18:40:46 +00001841 res.passphrase[p2p->cfg->passphrase_len] = '\0';
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001842 } else {
1843 res.freq = peer->oper_freq;
1844 if (p2p->ssid_len) {
1845 os_memcpy(res.ssid, p2p->ssid, p2p->ssid_len);
1846 res.ssid_len = p2p->ssid_len;
1847 }
1848 }
1849
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001850 p2p_channels_dump(p2p, "own channels", &p2p->channels);
1851 p2p_channels_dump(p2p, "peer channels", &peer->channels);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001852 p2p_channels_intersect(&p2p->channels, &peer->channels,
1853 &intersection);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001854 if (go) {
1855 p2p_channels_remove_freqs(&intersection, &p2p->no_go_freq);
1856 p2p_channels_dump(p2p, "intersection after no-GO removal",
1857 &intersection);
1858 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001859
1860 p2p_channels_to_freqs(&intersection, res.freq_list,
1861 P2P_MAX_CHANNELS);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001862
1863 res.peer_config_timeout = go ? peer->client_timeout : peer->go_timeout;
1864
1865 p2p_clear_timeout(p2p);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001866 p2p->ssid_set = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001867 peer->go_neg_req_sent = 0;
Dmitry Shmidt29333592017-01-09 12:27:11 -08001868 peer->flags &= ~P2P_DEV_PEER_WAITING_RESPONSE;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001869 peer->wps_method = WPS_NOT_READY;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001870 peer->oob_pw_id = 0;
Dmitry Shmidt413dde72014-04-11 10:23:22 -07001871 wpabuf_free(peer->go_neg_conf);
1872 peer->go_neg_conf = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001873
1874 p2p_set_state(p2p, P2P_PROVISIONING);
1875 p2p->cfg->go_neg_completed(p2p->cfg->cb_ctx, &res);
1876}
1877
1878
1879static void p2p_rx_p2p_action(struct p2p_data *p2p, const u8 *sa,
1880 const u8 *data, size_t len, int rx_freq)
1881{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001882 p2p_dbg(p2p, "RX P2P Public Action from " MACSTR, MAC2STR(sa));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001883 wpa_hexdump(MSG_MSGDUMP, "P2P: P2P Public Action contents", data, len);
1884
1885 if (len < 1)
1886 return;
1887
1888 switch (data[0]) {
1889 case P2P_GO_NEG_REQ:
1890 p2p_process_go_neg_req(p2p, sa, data + 1, len - 1, rx_freq);
1891 break;
1892 case P2P_GO_NEG_RESP:
1893 p2p_process_go_neg_resp(p2p, sa, data + 1, len - 1, rx_freq);
1894 break;
1895 case P2P_GO_NEG_CONF:
1896 p2p_process_go_neg_conf(p2p, sa, data + 1, len - 1);
1897 break;
1898 case P2P_INVITATION_REQ:
1899 p2p_process_invitation_req(p2p, sa, data + 1, len - 1,
1900 rx_freq);
1901 break;
1902 case P2P_INVITATION_RESP:
1903 p2p_process_invitation_resp(p2p, sa, data + 1, len - 1);
1904 break;
1905 case P2P_PROV_DISC_REQ:
1906 p2p_process_prov_disc_req(p2p, sa, data + 1, len - 1, rx_freq);
1907 break;
1908 case P2P_PROV_DISC_RESP:
1909 p2p_process_prov_disc_resp(p2p, sa, data + 1, len - 1);
1910 break;
1911 case P2P_DEV_DISC_REQ:
1912 p2p_process_dev_disc_req(p2p, sa, data + 1, len - 1, rx_freq);
1913 break;
1914 case P2P_DEV_DISC_RESP:
1915 p2p_process_dev_disc_resp(p2p, sa, data + 1, len - 1);
1916 break;
1917 default:
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001918 p2p_dbg(p2p, "Unsupported P2P Public Action frame type %d",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001919 data[0]);
1920 break;
1921 }
1922}
1923
1924
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001925static void p2p_rx_action_public(struct p2p_data *p2p, const u8 *da,
1926 const u8 *sa, const u8 *bssid, const u8 *data,
1927 size_t len, int freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001928{
1929 if (len < 1)
1930 return;
1931
1932 switch (data[0]) {
1933 case WLAN_PA_VENDOR_SPECIFIC:
1934 data++;
1935 len--;
Dmitry Shmidta38abf92014-03-06 13:38:44 -08001936 if (len < 4)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001937 return;
Dmitry Shmidta38abf92014-03-06 13:38:44 -08001938 if (WPA_GET_BE32(data) != P2P_IE_VENDOR_TYPE)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001939 return;
1940
Dmitry Shmidta38abf92014-03-06 13:38:44 -08001941 data += 4;
1942 len -= 4;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001943
Dmitry Shmidta38abf92014-03-06 13:38:44 -08001944 p2p_rx_p2p_action(p2p, sa, data, len, freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001945 break;
1946 case WLAN_PA_GAS_INITIAL_REQ:
1947 p2p_rx_gas_initial_req(p2p, sa, data + 1, len - 1, freq);
1948 break;
1949 case WLAN_PA_GAS_INITIAL_RESP:
1950 p2p_rx_gas_initial_resp(p2p, sa, data + 1, len - 1, freq);
1951 break;
1952 case WLAN_PA_GAS_COMEBACK_REQ:
1953 p2p_rx_gas_comeback_req(p2p, sa, data + 1, len - 1, freq);
1954 break;
1955 case WLAN_PA_GAS_COMEBACK_RESP:
1956 p2p_rx_gas_comeback_resp(p2p, sa, data + 1, len - 1, freq);
1957 break;
1958 }
1959}
1960
1961
1962void p2p_rx_action(struct p2p_data *p2p, const u8 *da, const u8 *sa,
1963 const u8 *bssid, u8 category,
1964 const u8 *data, size_t len, int freq)
1965{
1966 if (category == WLAN_ACTION_PUBLIC) {
1967 p2p_rx_action_public(p2p, da, sa, bssid, data, len, freq);
1968 return;
1969 }
1970
1971 if (category != WLAN_ACTION_VENDOR_SPECIFIC)
1972 return;
1973
1974 if (len < 4)
1975 return;
1976
Dmitry Shmidta38abf92014-03-06 13:38:44 -08001977 if (WPA_GET_BE32(data) != P2P_IE_VENDOR_TYPE)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001978 return;
Dmitry Shmidta38abf92014-03-06 13:38:44 -08001979 data += 4;
1980 len -= 4;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001981
1982 /* P2P action frame */
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001983 p2p_dbg(p2p, "RX P2P Action from " MACSTR, MAC2STR(sa));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001984 wpa_hexdump(MSG_MSGDUMP, "P2P: P2P Action contents", data, len);
1985
1986 if (len < 1)
1987 return;
1988 switch (data[0]) {
1989 case P2P_NOA:
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001990 p2p_dbg(p2p, "Received P2P Action - Notice of Absence");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001991 /* TODO */
1992 break;
1993 case P2P_PRESENCE_REQ:
1994 p2p_process_presence_req(p2p, da, sa, data + 1, len - 1, freq);
1995 break;
1996 case P2P_PRESENCE_RESP:
1997 p2p_process_presence_resp(p2p, da, sa, data + 1, len - 1);
1998 break;
1999 case P2P_GO_DISC_REQ:
2000 p2p_process_go_disc_req(p2p, da, sa, data + 1, len - 1, freq);
2001 break;
2002 default:
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002003 p2p_dbg(p2p, "Received P2P Action - unknown type %u", data[0]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002004 break;
2005 }
2006}
2007
2008
2009static void p2p_go_neg_start(void *eloop_ctx, void *timeout_ctx)
2010{
2011 struct p2p_data *p2p = eloop_ctx;
2012 if (p2p->go_neg_peer == NULL)
2013 return;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07002014 if (p2p->pending_listen_freq) {
2015 p2p_dbg(p2p, "Clear pending_listen_freq for p2p_go_neg_start");
2016 p2p->pending_listen_freq = 0;
2017 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002018 p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
2019 p2p->go_neg_peer->status = P2P_SC_SUCCESS;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07002020 /*
2021 * Set new timeout to make sure a previously set one does not expire
2022 * too quickly while waiting for the GO Negotiation to complete.
2023 */
2024 p2p_set_timeout(p2p, 0, 500000);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002025 p2p_connect_send(p2p, p2p->go_neg_peer);
2026}
2027
2028
2029static void p2p_invite_start(void *eloop_ctx, void *timeout_ctx)
2030{
2031 struct p2p_data *p2p = eloop_ctx;
2032 if (p2p->invite_peer == NULL)
2033 return;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07002034 if (p2p->pending_listen_freq) {
2035 p2p_dbg(p2p, "Clear pending_listen_freq for p2p_invite_start");
2036 p2p->pending_listen_freq = 0;
2037 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002038 p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002039 p2p_invite_send(p2p, p2p->invite_peer, p2p->invite_go_dev_addr,
2040 p2p->invite_dev_pw_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002041}
2042
2043
2044static void p2p_add_dev_from_probe_req(struct p2p_data *p2p, const u8 *addr,
2045 const u8 *ie, size_t ie_len)
2046{
2047 struct p2p_message msg;
2048 struct p2p_device *dev;
2049
2050 os_memset(&msg, 0, sizeof(msg));
2051 if (p2p_parse_ies(ie, ie_len, &msg) < 0 || msg.p2p_attributes == NULL)
2052 {
2053 p2p_parse_free(&msg);
2054 return; /* not a P2P probe */
2055 }
2056
2057 if (msg.ssid == NULL || msg.ssid[1] != P2P_WILDCARD_SSID_LEN ||
2058 os_memcmp(msg.ssid + 2, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN)
2059 != 0) {
2060 /* The Probe Request is not part of P2P Device Discovery. It is
2061 * not known whether the source address of the frame is the P2P
2062 * Device Address or P2P Interface Address. Do not add a new
2063 * peer entry based on this frames.
2064 */
2065 p2p_parse_free(&msg);
2066 return;
2067 }
2068
2069 dev = p2p_get_device(p2p, addr);
2070 if (dev) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002071 if (msg.listen_channel) {
2072 int freq;
2073
2074 if (dev->country[0] == 0)
2075 os_memcpy(dev->country, msg.listen_channel, 3);
2076
2077 freq = p2p_channel_to_freq(msg.listen_channel[3],
2078 msg.listen_channel[4]);
2079
2080 if (freq > 0 && dev->listen_freq != freq) {
2081 p2p_dbg(p2p,
2082 "Updated peer " MACSTR " Listen channel (Probe Request): %d -> %d MHz",
2083 MAC2STR(addr), dev->listen_freq, freq);
2084 dev->listen_freq = freq;
2085 }
2086 }
2087
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002088 os_get_reltime(&dev->last_seen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002089 p2p_parse_free(&msg);
2090 return; /* already known */
2091 }
2092
2093 dev = p2p_create_device(p2p, addr);
2094 if (dev == NULL) {
2095 p2p_parse_free(&msg);
2096 return;
2097 }
2098
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002099 os_get_reltime(&dev->last_seen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002100 dev->flags |= P2P_DEV_PROBE_REQ_ONLY;
2101
2102 if (msg.listen_channel) {
2103 os_memcpy(dev->country, msg.listen_channel, 3);
Dmitry Shmidt4b060592013-04-29 16:42:49 -07002104 dev->listen_freq = p2p_channel_to_freq(msg.listen_channel[3],
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002105 msg.listen_channel[4]);
2106 }
2107
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002108 p2p_copy_wps_info(p2p, dev, 1, &msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002109
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002110 if (msg.wfd_subelems) {
2111 wpabuf_free(dev->info.wfd_subelems);
2112 dev->info.wfd_subelems = wpabuf_dup(msg.wfd_subelems);
2113 }
2114
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002115 p2p_parse_free(&msg);
2116
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002117 p2p_dbg(p2p, "Created device entry based on Probe Req: " MACSTR
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002118 " dev_capab=0x%x group_capab=0x%x name='%s' listen_freq=%d",
2119 MAC2STR(dev->info.p2p_device_addr), dev->info.dev_capab,
2120 dev->info.group_capab, dev->info.device_name,
2121 dev->listen_freq);
2122}
2123
2124
2125struct p2p_device * p2p_add_dev_from_go_neg_req(struct p2p_data *p2p,
2126 const u8 *addr,
2127 struct p2p_message *msg)
2128{
2129 struct p2p_device *dev;
2130
2131 dev = p2p_get_device(p2p, addr);
2132 if (dev) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002133 os_get_reltime(&dev->last_seen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002134 return dev; /* already known */
2135 }
2136
2137 dev = p2p_create_device(p2p, addr);
2138 if (dev == NULL)
2139 return NULL;
2140
2141 p2p_add_dev_info(p2p, addr, dev, msg);
2142
2143 return dev;
2144}
2145
2146
2147static int dev_type_match(const u8 *dev_type, const u8 *req_dev_type)
2148{
2149 if (os_memcmp(dev_type, req_dev_type, WPS_DEV_TYPE_LEN) == 0)
2150 return 1;
2151 if (os_memcmp(dev_type, req_dev_type, 2) == 0 &&
2152 WPA_GET_BE32(&req_dev_type[2]) == 0 &&
2153 WPA_GET_BE16(&req_dev_type[6]) == 0)
2154 return 1; /* Category match with wildcard OUI/sub-category */
2155 return 0;
2156}
2157
2158
2159int dev_type_list_match(const u8 *dev_type, const u8 *req_dev_type[],
2160 size_t num_req_dev_type)
2161{
2162 size_t i;
2163 for (i = 0; i < num_req_dev_type; i++) {
2164 if (dev_type_match(dev_type, req_dev_type[i]))
2165 return 1;
2166 }
2167 return 0;
2168}
2169
2170
2171/**
2172 * p2p_match_dev_type - Match local device type with requested type
2173 * @p2p: P2P module context from p2p_init()
2174 * @wps: WPS TLVs from Probe Request frame (concatenated WPS IEs)
2175 * Returns: 1 on match, 0 on mismatch
2176 *
2177 * This function can be used to match the Requested Device Type attribute in
2178 * WPS IE with the local device types for deciding whether to reply to a Probe
2179 * Request frame.
2180 */
2181int p2p_match_dev_type(struct p2p_data *p2p, struct wpabuf *wps)
2182{
2183 struct wps_parse_attr attr;
2184 size_t i;
2185
2186 if (wps_parse_msg(wps, &attr))
2187 return 1; /* assume no Requested Device Type attributes */
2188
2189 if (attr.num_req_dev_type == 0)
2190 return 1; /* no Requested Device Type attributes -> match */
2191
2192 if (dev_type_list_match(p2p->cfg->pri_dev_type, attr.req_dev_type,
2193 attr.num_req_dev_type))
2194 return 1; /* Own Primary Device Type matches */
2195
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002196 for (i = 0; i < p2p->cfg->num_sec_dev_types; i++) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002197 if (dev_type_list_match(p2p->cfg->sec_dev_type[i],
2198 attr.req_dev_type,
2199 attr.num_req_dev_type))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002200 return 1; /* Own Secondary Device Type matches */
2201 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002202
2203 /* No matching device type found */
2204 return 0;
2205}
2206
2207
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002208struct wpabuf * p2p_build_probe_resp_ies(struct p2p_data *p2p,
2209 const u8 *query_hash,
2210 u8 query_count)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002211{
2212 struct wpabuf *buf;
2213 u8 *len;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002214 int pw_id = -1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002215 size_t extra = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002216
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002217#ifdef CONFIG_WIFI_DISPLAY
2218 if (p2p->wfd_ie_probe_resp)
2219 extra = wpabuf_len(p2p->wfd_ie_probe_resp);
2220#endif /* CONFIG_WIFI_DISPLAY */
2221
Dmitry Shmidt2e67f062014-07-16 09:55:28 -07002222 if (p2p->vendor_elem && p2p->vendor_elem[VENDOR_ELEM_PROBE_RESP_P2P])
2223 extra += wpabuf_len(p2p->vendor_elem[VENDOR_ELEM_PROBE_RESP_P2P]);
2224
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002225 if (query_count)
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002226 extra += MAX_SVC_ADV_IE_LEN;
2227
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002228 buf = wpabuf_alloc(1000 + extra);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002229 if (buf == NULL)
2230 return NULL;
2231
Dmitry Shmidt04949592012-07-19 12:16:46 -07002232 if (p2p->go_neg_peer) {
2233 /* Advertise immediate availability of WPS credential */
2234 pw_id = p2p_wps_method_pw_id(p2p->go_neg_peer->wps_method);
2235 }
2236
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002237 if (p2p_build_wps_ie(p2p, buf, pw_id, 1) < 0) {
2238 p2p_dbg(p2p, "Failed to build WPS IE for Probe Response");
2239 wpabuf_free(buf);
2240 return NULL;
2241 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002242
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002243#ifdef CONFIG_WIFI_DISPLAY
2244 if (p2p->wfd_ie_probe_resp)
2245 wpabuf_put_buf(buf, p2p->wfd_ie_probe_resp);
2246#endif /* CONFIG_WIFI_DISPLAY */
2247
Dmitry Shmidt2e67f062014-07-16 09:55:28 -07002248 if (p2p->vendor_elem && p2p->vendor_elem[VENDOR_ELEM_PROBE_RESP_P2P])
2249 wpabuf_put_buf(buf,
2250 p2p->vendor_elem[VENDOR_ELEM_PROBE_RESP_P2P]);
2251
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002252 /* P2P IE */
2253 len = p2p_buf_add_ie_hdr(buf);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002254 p2p_buf_add_capability(buf, p2p->dev_capab &
2255 ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002256 if (p2p->ext_listen_interval)
2257 p2p_buf_add_ext_listen_timing(buf, p2p->ext_listen_period,
2258 p2p->ext_listen_interval);
2259 p2p_buf_add_device_info(buf, p2p, NULL);
2260 p2p_buf_update_ie_hdr(buf, len);
2261
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002262 if (query_count) {
2263 p2p_buf_add_service_instance(buf, p2p, query_count, query_hash,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002264 p2p->p2ps_adv_list);
2265 }
2266
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002267 return buf;
2268}
2269
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07002270static int p2p_build_probe_resp_buf(struct p2p_data *p2p, struct wpabuf *buf,
2271 struct wpabuf *ies,
2272 const u8 *addr, int rx_freq)
2273{
2274 struct ieee80211_mgmt *resp;
2275 u8 channel, op_class;
2276
2277 resp = wpabuf_put(buf, offsetof(struct ieee80211_mgmt,
2278 u.probe_resp.variable));
2279
2280 resp->frame_control = host_to_le16((WLAN_FC_TYPE_MGMT << 2) |
2281 (WLAN_FC_STYPE_PROBE_RESP << 4));
2282 os_memcpy(resp->da, addr, ETH_ALEN);
2283 os_memcpy(resp->sa, p2p->cfg->dev_addr, ETH_ALEN);
2284 os_memcpy(resp->bssid, p2p->cfg->dev_addr, ETH_ALEN);
2285 resp->u.probe_resp.beacon_int = host_to_le16(100);
2286 /* hardware or low-level driver will setup seq_ctrl and timestamp */
2287 resp->u.probe_resp.capab_info =
2288 host_to_le16(WLAN_CAPABILITY_SHORT_PREAMBLE |
2289 WLAN_CAPABILITY_PRIVACY |
2290 WLAN_CAPABILITY_SHORT_SLOT_TIME);
2291
2292 wpabuf_put_u8(buf, WLAN_EID_SSID);
2293 wpabuf_put_u8(buf, P2P_WILDCARD_SSID_LEN);
2294 wpabuf_put_data(buf, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN);
2295
2296 wpabuf_put_u8(buf, WLAN_EID_SUPP_RATES);
2297 wpabuf_put_u8(buf, 8);
2298 wpabuf_put_u8(buf, (60 / 5) | 0x80);
2299 wpabuf_put_u8(buf, 90 / 5);
2300 wpabuf_put_u8(buf, (120 / 5) | 0x80);
2301 wpabuf_put_u8(buf, 180 / 5);
2302 wpabuf_put_u8(buf, (240 / 5) | 0x80);
2303 wpabuf_put_u8(buf, 360 / 5);
2304 wpabuf_put_u8(buf, 480 / 5);
2305 wpabuf_put_u8(buf, 540 / 5);
2306
2307 if (!rx_freq) {
2308 channel = p2p->cfg->channel;
2309 } else if (p2p_freq_to_channel(rx_freq, &op_class, &channel)) {
2310 p2p_err(p2p, "Failed to convert freq to channel");
2311 return -1;
2312 }
2313
2314 wpabuf_put_u8(buf, WLAN_EID_DS_PARAMS);
2315 wpabuf_put_u8(buf, 1);
2316 wpabuf_put_u8(buf, channel);
2317
2318 wpabuf_put_buf(buf, ies);
2319
2320 return 0;
2321}
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002322
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002323static int p2p_service_find_asp(struct p2p_data *p2p, const u8 *hash)
2324{
2325 struct p2ps_advertisement *adv_data;
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002326 int any_wfa;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002327
2328 p2p_dbg(p2p, "ASP find - ASP list: %p", p2p->p2ps_adv_list);
2329
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002330 /* Wildcard org.wi-fi.wfds matches any WFA spec defined service */
2331 any_wfa = os_memcmp(hash, p2p->wild_card_hash, P2PS_HASH_LEN) == 0;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002332
2333 adv_data = p2p->p2ps_adv_list;
2334 while (adv_data) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002335 if (os_memcmp(hash, adv_data->hash, P2PS_HASH_LEN) == 0)
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002336 return 1; /* exact hash match */
2337 if (any_wfa &&
2338 os_strncmp(adv_data->svc_name, P2PS_WILD_HASH_STR,
2339 os_strlen(P2PS_WILD_HASH_STR)) == 0)
2340 return 1; /* WFA service match */
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002341 adv_data = adv_data->next;
2342 }
2343
2344 return 0;
2345}
2346
2347
Dmitry Shmidt04949592012-07-19 12:16:46 -07002348static enum p2p_probe_req_status
2349p2p_reply_probe(struct p2p_data *p2p, const u8 *addr, const u8 *dst,
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002350 const u8 *bssid, const u8 *ie, size_t ie_len,
2351 unsigned int rx_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002352{
2353 struct ieee802_11_elems elems;
2354 struct wpabuf *buf;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002355 struct p2p_message msg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002356 struct wpabuf *ies;
2357
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002358 if (ieee802_11_parse_elems((u8 *) ie, ie_len, &elems, 0) ==
2359 ParseFailed) {
2360 /* Ignore invalid Probe Request frames */
Dmitry Shmidtec58b162014-02-19 12:44:18 -08002361 p2p_dbg(p2p, "Could not parse Probe Request frame - ignore it");
Dmitry Shmidt04949592012-07-19 12:16:46 -07002362 return P2P_PREQ_MALFORMED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002363 }
2364
2365 if (elems.p2p == NULL) {
2366 /* not a P2P probe - ignore it */
Dmitry Shmidtec58b162014-02-19 12:44:18 -08002367 p2p_dbg(p2p, "Not a P2P probe - ignore it");
Dmitry Shmidt04949592012-07-19 12:16:46 -07002368 return P2P_PREQ_NOT_P2P;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002369 }
2370
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002371 if (dst && !is_broadcast_ether_addr(dst) &&
2372 os_memcmp(dst, p2p->cfg->dev_addr, ETH_ALEN) != 0) {
2373 /* Not sent to the broadcast address or our P2P Device Address
2374 */
Dmitry Shmidtec58b162014-02-19 12:44:18 -08002375 p2p_dbg(p2p, "Probe Req DA " MACSTR " not ours - ignore it",
2376 MAC2STR(dst));
Dmitry Shmidt04949592012-07-19 12:16:46 -07002377 return P2P_PREQ_NOT_PROCESSED;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002378 }
2379
2380 if (bssid && !is_broadcast_ether_addr(bssid)) {
2381 /* Not sent to the Wildcard BSSID */
Dmitry Shmidtec58b162014-02-19 12:44:18 -08002382 p2p_dbg(p2p, "Probe Req BSSID " MACSTR " not wildcard - ignore it",
2383 MAC2STR(bssid));
Dmitry Shmidt04949592012-07-19 12:16:46 -07002384 return P2P_PREQ_NOT_PROCESSED;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002385 }
2386
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002387 if (elems.ssid == NULL || elems.ssid_len != P2P_WILDCARD_SSID_LEN ||
2388 os_memcmp(elems.ssid, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN) !=
2389 0) {
2390 /* not using P2P Wildcard SSID - ignore */
Dmitry Shmidtec58b162014-02-19 12:44:18 -08002391 p2p_dbg(p2p, "Probe Req not using P2P Wildcard SSID - ignore it");
Dmitry Shmidt04949592012-07-19 12:16:46 -07002392 return P2P_PREQ_NOT_PROCESSED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002393 }
2394
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002395 if (supp_rates_11b_only(&elems)) {
2396 /* Indicates support for 11b rates only */
Dmitry Shmidtec58b162014-02-19 12:44:18 -08002397 p2p_dbg(p2p, "Probe Req with 11b rates only supported - ignore it");
Dmitry Shmidt04949592012-07-19 12:16:46 -07002398 return P2P_PREQ_NOT_P2P;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002399 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002400
2401 os_memset(&msg, 0, sizeof(msg));
2402 if (p2p_parse_ies(ie, ie_len, &msg) < 0) {
2403 /* Could not parse P2P attributes */
Dmitry Shmidtec58b162014-02-19 12:44:18 -08002404 p2p_dbg(p2p, "Could not parse P2P attributes in Probe Req - ignore it");
Dmitry Shmidt04949592012-07-19 12:16:46 -07002405 return P2P_PREQ_NOT_P2P;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002406 }
2407
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002408 if (msg.service_hash && msg.service_hash_count) {
2409 const u8 *hash = msg.service_hash;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002410 u8 i;
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002411 int p2ps_svc_found = 0;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002412
Dmitry Shmidt41712582015-06-29 11:02:15 -07002413 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",
2414 p2p->in_listen, p2p->drv_in_listen, rx_freq,
2415 p2p->cfg->channel, p2p->pending_listen_freq);
2416
2417 if (!p2p->in_listen && !p2p->drv_in_listen &&
2418 p2p->pending_listen_freq && rx_freq &&
2419 rx_freq != p2p->pending_listen_freq) {
2420 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",
2421 rx_freq, p2p->pending_listen_freq);
2422 p2p_parse_free(&msg);
2423 return P2P_PREQ_NOT_LISTEN;
2424 }
2425
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002426 for (i = 0; i < msg.service_hash_count; i++) {
2427 if (p2p_service_find_asp(p2p, hash)) {
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002428 p2p_dbg(p2p, "Service Hash match found: "
2429 MACSTR, MAC2STR(hash));
2430 p2ps_svc_found = 1;
2431 break;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002432 }
2433 hash += P2PS_HASH_LEN;
2434 }
2435
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002436 /* Probed hash unknown */
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002437 if (!p2ps_svc_found) {
2438 p2p_dbg(p2p, "No Service Hash match found");
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002439 p2p_parse_free(&msg);
2440 return P2P_PREQ_NOT_PROCESSED;
2441 }
2442 } else {
2443 /* This is not a P2PS Probe Request */
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002444 p2p_dbg(p2p, "No P2PS Hash in Probe Request");
2445
2446 if (!p2p->in_listen || !p2p->drv_in_listen) {
2447 /* not in Listen state - ignore Probe Request */
2448 p2p_dbg(p2p, "Not in Listen state (in_listen=%d drv_in_listen=%d) - ignore Probe Request",
2449 p2p->in_listen, p2p->drv_in_listen);
2450 p2p_parse_free(&msg);
2451 return P2P_PREQ_NOT_LISTEN;
2452 }
2453 }
2454
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002455 if (msg.device_id &&
Dmitry Shmidt04949592012-07-19 12:16:46 -07002456 os_memcmp(msg.device_id, p2p->cfg->dev_addr, ETH_ALEN) != 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002457 /* Device ID did not match */
Dmitry Shmidtec58b162014-02-19 12:44:18 -08002458 p2p_dbg(p2p, "Probe Req requested Device ID " MACSTR " did not match - ignore it",
2459 MAC2STR(msg.device_id));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002460 p2p_parse_free(&msg);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002461 return P2P_PREQ_NOT_PROCESSED;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002462 }
2463
2464 /* Check Requested Device Type match */
2465 if (msg.wps_attributes &&
2466 !p2p_match_dev_type(p2p, msg.wps_attributes)) {
2467 /* No match with Requested Device Type */
Hai Shalom021b0b52019-04-10 11:17:58 -07002468 p2p_dbg(p2p, "Probe Req requested Device Type did not match - ignore it");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002469 p2p_parse_free(&msg);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002470 return P2P_PREQ_NOT_PROCESSED;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002471 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002472
Dmitry Shmidt04949592012-07-19 12:16:46 -07002473 if (!p2p->cfg->send_probe_resp) {
2474 /* Response generated elsewhere */
Dmitry Shmidtec58b162014-02-19 12:44:18 -08002475 p2p_dbg(p2p, "Probe Resp generated elsewhere - do not generate additional response");
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002476 p2p_parse_free(&msg);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002477 return P2P_PREQ_NOT_PROCESSED;
2478 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002479
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002480 p2p_dbg(p2p, "Reply to P2P Probe Request in Listen state");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002481
2482 /*
2483 * We do not really have a specific BSS that this frame is advertising,
2484 * so build a frame that has some information in valid format. This is
2485 * really only used for discovery purposes, not to learn exact BSS
2486 * parameters.
2487 */
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002488 ies = p2p_build_probe_resp_ies(p2p, msg.service_hash,
2489 msg.service_hash_count);
2490 p2p_parse_free(&msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002491 if (ies == NULL)
Dmitry Shmidt04949592012-07-19 12:16:46 -07002492 return P2P_PREQ_NOT_PROCESSED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002493
2494 buf = wpabuf_alloc(200 + wpabuf_len(ies));
2495 if (buf == NULL) {
2496 wpabuf_free(ies);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002497 return P2P_PREQ_NOT_PROCESSED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002498 }
2499
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07002500 if (p2p_build_probe_resp_buf(p2p, buf, ies, addr, rx_freq)) {
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002501 wpabuf_free(ies);
2502 wpabuf_free(buf);
2503 return P2P_PREQ_NOT_PROCESSED;
2504 }
2505
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002506 wpabuf_free(ies);
2507
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002508 p2p->cfg->send_probe_resp(p2p->cfg->cb_ctx, buf, rx_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002509
2510 wpabuf_free(buf);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002511
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002512 return P2P_PREQ_PROCESSED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002513}
2514
2515
Dmitry Shmidt04949592012-07-19 12:16:46 -07002516enum p2p_probe_req_status
2517p2p_probe_req_rx(struct p2p_data *p2p, const u8 *addr, const u8 *dst,
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002518 const u8 *bssid, const u8 *ie, size_t ie_len,
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07002519 unsigned int rx_freq, int p2p_lo_started)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002520{
Dmitry Shmidt04949592012-07-19 12:16:46 -07002521 enum p2p_probe_req_status res;
2522
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002523 p2p_add_dev_from_probe_req(p2p, addr, ie, ie_len);
2524
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07002525 if (p2p_lo_started) {
2526 p2p_dbg(p2p,
2527 "Probe Response is offloaded, do not reply Probe Request");
2528 return P2P_PREQ_PROCESSED;
2529 }
2530
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002531 res = p2p_reply_probe(p2p, addr, dst, bssid, ie, ie_len, rx_freq);
2532 if (res != P2P_PREQ_PROCESSED && res != P2P_PREQ_NOT_PROCESSED)
2533 return res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002534
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002535 /*
2536 * Activate a pending GO Negotiation/Invite flow if a received Probe
2537 * Request frame is from an expected peer. Some devices may share the
2538 * same address for P2P and non-P2P STA running simultaneously. The
2539 * P2P_PREQ_PROCESSED and P2P_PREQ_NOT_PROCESSED p2p_reply_probe()
2540 * return values verified above ensure we are handling a Probe Request
2541 * frame from a P2P peer.
2542 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002543 if ((p2p->state == P2P_CONNECT || p2p->state == P2P_CONNECT_LISTEN) &&
2544 p2p->go_neg_peer &&
2545 os_memcmp(addr, p2p->go_neg_peer->info.p2p_device_addr, ETH_ALEN)
Dmitry Shmidt9cdf1b92013-02-27 12:58:50 -08002546 == 0 &&
2547 !(p2p->go_neg_peer->flags & P2P_DEV_WAIT_GO_NEG_CONFIRM)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002548 /* Received a Probe Request from GO Negotiation peer */
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002549 p2p_dbg(p2p, "Found GO Negotiation peer - try to start GO negotiation from timeout");
Dmitry Shmidt9cdf1b92013-02-27 12:58:50 -08002550 eloop_cancel_timeout(p2p_go_neg_start, p2p, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002551 eloop_register_timeout(0, 0, p2p_go_neg_start, p2p, NULL);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002552 return res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002553 }
2554
2555 if ((p2p->state == P2P_INVITE || p2p->state == P2P_INVITE_LISTEN) &&
2556 p2p->invite_peer &&
Dmitry Shmidt3c479372014-02-04 10:50:36 -08002557 (p2p->invite_peer->flags & P2P_DEV_WAIT_INV_REQ_ACK) &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002558 os_memcmp(addr, p2p->invite_peer->info.p2p_device_addr, ETH_ALEN)
2559 == 0) {
2560 /* Received a Probe Request from Invite peer */
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002561 p2p_dbg(p2p, "Found Invite peer - try to start Invite from timeout");
Dmitry Shmidt7f93d6f2014-02-21 11:22:49 -08002562 eloop_cancel_timeout(p2p_invite_start, p2p, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002563 eloop_register_timeout(0, 0, p2p_invite_start, p2p, NULL);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002564 return res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002565 }
2566
Dmitry Shmidt04949592012-07-19 12:16:46 -07002567 return res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002568}
2569
2570
2571static int p2p_assoc_req_ie_wlan_ap(struct p2p_data *p2p, const u8 *bssid,
2572 u8 *buf, size_t len, struct wpabuf *p2p_ie)
2573{
2574 struct wpabuf *tmp;
2575 u8 *lpos;
2576 size_t tmplen;
2577 int res;
2578 u8 group_capab;
Dmitry Shmidt05df46a2015-05-19 11:02:01 -07002579 struct p2p_message msg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002580
2581 if (p2p_ie == NULL)
2582 return 0; /* WLAN AP is not a P2P manager */
2583
Dmitry Shmidt05df46a2015-05-19 11:02:01 -07002584 os_memset(&msg, 0, sizeof(msg));
2585 if (p2p_parse_p2p_ie(p2p_ie, &msg) < 0)
2586 return 0;
2587
2588 p2p_dbg(p2p, "BSS P2P manageability %s",
2589 msg.manageability ? "enabled" : "disabled");
2590
2591 if (!msg.manageability)
2592 return 0;
2593
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002594 /*
2595 * (Re)Association Request - P2P IE
2596 * P2P Capability attribute (shall be present)
2597 * P2P Interface attribute (present if concurrent device and
2598 * P2P Management is enabled)
2599 */
2600 tmp = wpabuf_alloc(200);
2601 if (tmp == NULL)
2602 return -1;
2603
2604 lpos = p2p_buf_add_ie_hdr(tmp);
2605 group_capab = 0;
2606 if (p2p->num_groups > 0) {
2607 group_capab |= P2P_GROUP_CAPAB_GROUP_OWNER;
2608 if ((p2p->dev_capab & P2P_DEV_CAPAB_CONCURRENT_OPER) &&
2609 (p2p->dev_capab & P2P_DEV_CAPAB_INFRA_MANAGED) &&
2610 p2p->cross_connect)
2611 group_capab |= P2P_GROUP_CAPAB_CROSS_CONN;
2612 }
2613 p2p_buf_add_capability(tmp, p2p->dev_capab, group_capab);
2614 if ((p2p->dev_capab & P2P_DEV_CAPAB_CONCURRENT_OPER) &&
2615 (p2p->dev_capab & P2P_DEV_CAPAB_INFRA_MANAGED))
2616 p2p_buf_add_p2p_interface(tmp, p2p);
2617 p2p_buf_update_ie_hdr(tmp, lpos);
2618
2619 tmplen = wpabuf_len(tmp);
2620 if (tmplen > len)
2621 res = -1;
2622 else {
2623 os_memcpy(buf, wpabuf_head(tmp), tmplen);
2624 res = tmplen;
2625 }
2626 wpabuf_free(tmp);
2627
2628 return res;
2629}
2630
2631
2632int p2p_assoc_req_ie(struct p2p_data *p2p, const u8 *bssid, u8 *buf,
2633 size_t len, int p2p_group, struct wpabuf *p2p_ie)
2634{
2635 struct wpabuf *tmp;
2636 u8 *lpos;
2637 struct p2p_device *peer;
2638 size_t tmplen;
2639 int res;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002640 size_t extra = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002641
2642 if (!p2p_group)
2643 return p2p_assoc_req_ie_wlan_ap(p2p, bssid, buf, len, p2p_ie);
2644
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002645#ifdef CONFIG_WIFI_DISPLAY
2646 if (p2p->wfd_ie_assoc_req)
2647 extra = wpabuf_len(p2p->wfd_ie_assoc_req);
2648#endif /* CONFIG_WIFI_DISPLAY */
2649
Dmitry Shmidt2e67f062014-07-16 09:55:28 -07002650 if (p2p->vendor_elem && p2p->vendor_elem[VENDOR_ELEM_P2P_ASSOC_REQ])
2651 extra += wpabuf_len(p2p->vendor_elem[VENDOR_ELEM_P2P_ASSOC_REQ]);
2652
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002653 /*
2654 * (Re)Association Request - P2P IE
2655 * P2P Capability attribute (shall be present)
2656 * Extended Listen Timing (may be present)
2657 * P2P Device Info attribute (shall be present)
2658 */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002659 tmp = wpabuf_alloc(200 + extra);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002660 if (tmp == NULL)
2661 return -1;
2662
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002663#ifdef CONFIG_WIFI_DISPLAY
2664 if (p2p->wfd_ie_assoc_req)
2665 wpabuf_put_buf(tmp, p2p->wfd_ie_assoc_req);
2666#endif /* CONFIG_WIFI_DISPLAY */
2667
Dmitry Shmidt2e67f062014-07-16 09:55:28 -07002668 if (p2p->vendor_elem && p2p->vendor_elem[VENDOR_ELEM_P2P_ASSOC_REQ])
2669 wpabuf_put_buf(tmp,
2670 p2p->vendor_elem[VENDOR_ELEM_P2P_ASSOC_REQ]);
2671
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002672 peer = bssid ? p2p_get_device(p2p, bssid) : NULL;
2673
2674 lpos = p2p_buf_add_ie_hdr(tmp);
2675 p2p_buf_add_capability(tmp, p2p->dev_capab, 0);
2676 if (p2p->ext_listen_interval)
2677 p2p_buf_add_ext_listen_timing(tmp, p2p->ext_listen_period,
2678 p2p->ext_listen_interval);
2679 p2p_buf_add_device_info(tmp, p2p, peer);
2680 p2p_buf_update_ie_hdr(tmp, lpos);
2681
2682 tmplen = wpabuf_len(tmp);
2683 if (tmplen > len)
2684 res = -1;
2685 else {
2686 os_memcpy(buf, wpabuf_head(tmp), tmplen);
2687 res = tmplen;
2688 }
2689 wpabuf_free(tmp);
2690
2691 return res;
2692}
2693
2694
2695int p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf, char *end)
2696{
2697 struct wpabuf *p2p_ie;
2698 int ret;
2699
2700 p2p_ie = ieee802_11_vendor_ie_concat(ies, ies_len, P2P_IE_VENDOR_TYPE);
2701 if (p2p_ie == NULL)
2702 return 0;
2703
2704 ret = p2p_attr_text(p2p_ie, buf, end);
2705 wpabuf_free(p2p_ie);
2706 return ret;
2707}
2708
2709
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002710struct p2ps_advertisement *
2711p2p_service_p2ps_id(struct p2p_data *p2p, u32 adv_id)
2712{
2713 struct p2ps_advertisement *adv_data;
2714
2715 if (!p2p)
2716 return NULL;
2717
2718 adv_data = p2p->p2ps_adv_list;
2719 while (adv_data) {
2720 if (adv_data->id == adv_id)
2721 return adv_data;
2722 adv_data = adv_data->next;
2723 }
2724
2725 return NULL;
2726}
2727
2728
2729int p2p_service_del_asp(struct p2p_data *p2p, u32 adv_id)
2730{
2731 struct p2ps_advertisement *adv_data;
2732 struct p2ps_advertisement **prior;
2733
2734 if (!p2p)
2735 return -1;
2736
2737 adv_data = p2p->p2ps_adv_list;
2738 prior = &p2p->p2ps_adv_list;
2739 while (adv_data) {
2740 if (adv_data->id == adv_id) {
2741 p2p_dbg(p2p, "Delete ASP adv_id=0x%x", adv_id);
2742 *prior = adv_data->next;
2743 os_free(adv_data);
2744 return 0;
2745 }
2746 prior = &adv_data->next;
2747 adv_data = adv_data->next;
2748 }
2749
2750 return -1;
2751}
2752
2753
2754int p2p_service_add_asp(struct p2p_data *p2p, int auto_accept, u32 adv_id,
2755 const char *adv_str, u8 svc_state, u16 config_methods,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002756 const char *svc_info, const u8 *cpt_priority)
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002757{
2758 struct p2ps_advertisement *adv_data, *tmp, **prev;
2759 u8 buf[P2PS_HASH_LEN];
2760 size_t adv_data_len, adv_len, info_len = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002761 int i;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002762
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002763 if (!p2p || !adv_str || !adv_str[0] || !cpt_priority)
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002764 return -1;
2765
2766 if (!(config_methods & p2p->cfg->config_methods)) {
2767 p2p_dbg(p2p, "Config methods not supported svc: 0x%x dev: 0x%x",
2768 config_methods, p2p->cfg->config_methods);
2769 return -1;
2770 }
2771
2772 if (!p2ps_gen_hash(p2p, adv_str, buf))
2773 return -1;
2774
2775 if (svc_info)
2776 info_len = os_strlen(svc_info);
2777 adv_len = os_strlen(adv_str);
2778 adv_data_len = sizeof(struct p2ps_advertisement) + adv_len + 1 +
2779 info_len + 1;
2780
2781 adv_data = os_zalloc(adv_data_len);
2782 if (!adv_data)
2783 return -1;
2784
2785 os_memcpy(adv_data->hash, buf, P2PS_HASH_LEN);
2786 adv_data->id = adv_id;
2787 adv_data->state = svc_state;
2788 adv_data->config_methods = config_methods & p2p->cfg->config_methods;
2789 adv_data->auto_accept = (u8) auto_accept;
2790 os_memcpy(adv_data->svc_name, adv_str, adv_len);
2791
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002792 for (i = 0; cpt_priority[i] && i < P2PS_FEATURE_CAPAB_CPT_MAX; i++) {
2793 adv_data->cpt_priority[i] = cpt_priority[i];
2794 adv_data->cpt_mask |= cpt_priority[i];
2795 }
2796
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002797 if (svc_info && info_len) {
2798 adv_data->svc_info = &adv_data->svc_name[adv_len + 1];
2799 os_memcpy(adv_data->svc_info, svc_info, info_len);
2800 }
2801
2802 /*
2803 * Group Advertisements by service string. They do not need to be
2804 * sorted, but groups allow easier Probe Response instance grouping
2805 */
2806 tmp = p2p->p2ps_adv_list;
2807 prev = &p2p->p2ps_adv_list;
2808 while (tmp) {
2809 if (tmp->id == adv_data->id) {
2810 if (os_strcmp(tmp->svc_name, adv_data->svc_name) != 0) {
2811 os_free(adv_data);
2812 return -1;
2813 }
2814 adv_data->next = tmp->next;
2815 *prev = adv_data;
2816 os_free(tmp);
2817 goto inserted;
2818 } else {
2819 if (os_strcmp(tmp->svc_name, adv_data->svc_name) == 0) {
2820 adv_data->next = tmp->next;
2821 tmp->next = adv_data;
2822 goto inserted;
2823 }
2824 }
2825 prev = &tmp->next;
2826 tmp = tmp->next;
2827 }
2828
2829 /* No svc_name match found */
2830 adv_data->next = p2p->p2ps_adv_list;
2831 p2p->p2ps_adv_list = adv_data;
2832
2833inserted:
2834 p2p_dbg(p2p,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002835 "Added ASP advertisement adv_id=0x%x config_methods=0x%x svc_state=0x%x adv_str='%s' cpt_mask=0x%x",
2836 adv_id, adv_data->config_methods, svc_state, adv_str,
2837 adv_data->cpt_mask);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002838
2839 return 0;
2840}
2841
2842
Dmitry Shmidtdda10c22015-03-24 16:05:01 -07002843void p2p_service_flush_asp(struct p2p_data *p2p)
2844{
2845 struct p2ps_advertisement *adv, *prev;
2846
2847 if (!p2p)
2848 return;
2849
2850 adv = p2p->p2ps_adv_list;
2851 while (adv) {
2852 prev = adv;
2853 adv = adv->next;
2854 os_free(prev);
2855 }
2856
2857 p2p->p2ps_adv_list = NULL;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002858 p2ps_prov_free(p2p);
Dmitry Shmidtdda10c22015-03-24 16:05:01 -07002859 p2p_dbg(p2p, "All ASP advertisements flushed");
2860}
2861
2862
Dmitry Shmidt04949592012-07-19 12:16:46 -07002863int p2p_parse_dev_addr_in_p2p_ie(struct wpabuf *p2p_ie, u8 *dev_addr)
2864{
2865 struct p2p_message msg;
2866
2867 os_memset(&msg, 0, sizeof(msg));
2868 if (p2p_parse_p2p_ie(p2p_ie, &msg))
2869 return -1;
2870
2871 if (msg.p2p_device_addr) {
2872 os_memcpy(dev_addr, msg.p2p_device_addr, ETH_ALEN);
2873 return 0;
2874 } else if (msg.device_id) {
2875 os_memcpy(dev_addr, msg.device_id, ETH_ALEN);
2876 return 0;
2877 }
2878 return -1;
2879}
2880
2881
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002882int p2p_parse_dev_addr(const u8 *ies, size_t ies_len, u8 *dev_addr)
2883{
2884 struct wpabuf *p2p_ie;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002885 int ret;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002886
2887 p2p_ie = ieee802_11_vendor_ie_concat(ies, ies_len,
2888 P2P_IE_VENDOR_TYPE);
2889 if (p2p_ie == NULL)
2890 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002891 ret = p2p_parse_dev_addr_in_p2p_ie(p2p_ie, dev_addr);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002892 wpabuf_free(p2p_ie);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002893 return ret;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002894}
2895
2896
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002897static void p2p_clear_go_neg(struct p2p_data *p2p)
2898{
2899 p2p->go_neg_peer = NULL;
2900 p2p_clear_timeout(p2p);
2901 p2p_set_state(p2p, P2P_IDLE);
2902}
2903
2904
2905void p2p_wps_success_cb(struct p2p_data *p2p, const u8 *mac_addr)
2906{
2907 if (p2p->go_neg_peer == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002908 p2p_dbg(p2p, "No pending Group Formation - ignore WPS registration success notification");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002909 return; /* No pending Group Formation */
2910 }
2911
2912 if (os_memcmp(mac_addr, p2p->go_neg_peer->intended_addr, ETH_ALEN) !=
2913 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002914 p2p_dbg(p2p, "Ignore WPS registration success notification for "
2915 MACSTR " (GO Negotiation peer " MACSTR ")",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002916 MAC2STR(mac_addr),
2917 MAC2STR(p2p->go_neg_peer->intended_addr));
2918 return; /* Ignore unexpected peer address */
2919 }
2920
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002921 p2p_dbg(p2p, "Group Formation completed successfully with " MACSTR,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002922 MAC2STR(mac_addr));
2923
2924 p2p_clear_go_neg(p2p);
2925}
2926
2927
2928void p2p_group_formation_failed(struct p2p_data *p2p)
2929{
2930 if (p2p->go_neg_peer == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002931 p2p_dbg(p2p, "No pending Group Formation - ignore group formation failure notification");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002932 return; /* No pending Group Formation */
2933 }
2934
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002935 p2p_dbg(p2p, "Group Formation failed with " MACSTR,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002936 MAC2STR(p2p->go_neg_peer->intended_addr));
2937
2938 p2p_clear_go_neg(p2p);
2939}
2940
2941
Hai Shalom60840252021-02-19 19:02:11 -08002942bool is_p2p_6ghz_disabled(struct p2p_data *p2p)
2943{
2944 if (p2p)
2945 return p2p->cfg->p2p_6ghz_disable;
2946 return false;
2947}
2948
2949
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002950struct p2p_data * p2p_init(const struct p2p_config *cfg)
2951{
2952 struct p2p_data *p2p;
2953
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07002954 if (cfg->max_peers < 1 ||
2955 cfg->passphrase_len < 8 || cfg->passphrase_len > 63)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002956 return NULL;
2957
2958 p2p = os_zalloc(sizeof(*p2p) + sizeof(*cfg));
2959 if (p2p == NULL)
2960 return NULL;
2961 p2p->cfg = (struct p2p_config *) (p2p + 1);
2962 os_memcpy(p2p->cfg, cfg, sizeof(*cfg));
2963 if (cfg->dev_name)
2964 p2p->cfg->dev_name = os_strdup(cfg->dev_name);
2965 if (cfg->manufacturer)
2966 p2p->cfg->manufacturer = os_strdup(cfg->manufacturer);
2967 if (cfg->model_name)
2968 p2p->cfg->model_name = os_strdup(cfg->model_name);
2969 if (cfg->model_number)
2970 p2p->cfg->model_number = os_strdup(cfg->model_number);
2971 if (cfg->serial_number)
2972 p2p->cfg->serial_number = os_strdup(cfg->serial_number);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002973 if (cfg->pref_chan) {
2974 p2p->cfg->pref_chan = os_malloc(cfg->num_pref_chan *
2975 sizeof(struct p2p_channel));
2976 if (p2p->cfg->pref_chan) {
2977 os_memcpy(p2p->cfg->pref_chan, cfg->pref_chan,
2978 cfg->num_pref_chan *
2979 sizeof(struct p2p_channel));
2980 } else
2981 p2p->cfg->num_pref_chan = 0;
2982 }
2983
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002984 p2ps_gen_hash(p2p, P2PS_WILD_HASH_STR, p2p->wild_card_hash);
2985
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002986 p2p->min_disc_int = 1;
2987 p2p->max_disc_int = 3;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002988 p2p->max_disc_tu = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002989
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07002990 if (os_get_random(&p2p->next_tie_breaker, 1) < 0)
2991 p2p->next_tie_breaker = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002992 p2p->next_tie_breaker &= 0x01;
2993 if (cfg->sd_request)
2994 p2p->dev_capab |= P2P_DEV_CAPAB_SERVICE_DISCOVERY;
2995 p2p->dev_capab |= P2P_DEV_CAPAB_INVITATION_PROCEDURE;
2996 if (cfg->concurrent_operations)
2997 p2p->dev_capab |= P2P_DEV_CAPAB_CONCURRENT_OPER;
2998 p2p->dev_capab |= P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
2999
3000 dl_list_init(&p2p->devices);
3001
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003002 p2p->go_timeout = 100;
3003 p2p->client_timeout = 20;
Dmitry Shmidt13ca8d82014-02-20 10:18:40 -08003004 p2p->num_p2p_sd_queries = 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003005
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003006 p2p_dbg(p2p, "initialized");
3007 p2p_channels_dump(p2p, "channels", &p2p->cfg->channels);
3008 p2p_channels_dump(p2p, "cli_channels", &p2p->cfg->cli_channels);
3009
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003010 return p2p;
3011}
3012
3013
3014void p2p_deinit(struct p2p_data *p2p)
3015{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003016#ifdef CONFIG_WIFI_DISPLAY
3017 wpabuf_free(p2p->wfd_ie_beacon);
3018 wpabuf_free(p2p->wfd_ie_probe_req);
3019 wpabuf_free(p2p->wfd_ie_probe_resp);
3020 wpabuf_free(p2p->wfd_ie_assoc_req);
3021 wpabuf_free(p2p->wfd_ie_invitation);
3022 wpabuf_free(p2p->wfd_ie_prov_disc_req);
3023 wpabuf_free(p2p->wfd_ie_prov_disc_resp);
3024 wpabuf_free(p2p->wfd_ie_go_neg);
3025 wpabuf_free(p2p->wfd_dev_info);
3026 wpabuf_free(p2p->wfd_assoc_bssid);
3027 wpabuf_free(p2p->wfd_coupled_sink_info);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003028 wpabuf_free(p2p->wfd_r2_dev_info);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003029#endif /* CONFIG_WIFI_DISPLAY */
3030
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003031 eloop_cancel_timeout(p2p_scan_timeout, p2p, NULL);
Dmitry Shmidt9cdf1b92013-02-27 12:58:50 -08003032 eloop_cancel_timeout(p2p_go_neg_start, p2p, NULL);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003033 eloop_cancel_timeout(p2p_go_neg_wait_timeout, p2p, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003034 p2p_flush(p2p);
3035 p2p_free_req_dev_types(p2p);
3036 os_free(p2p->cfg->dev_name);
3037 os_free(p2p->cfg->manufacturer);
3038 os_free(p2p->cfg->model_name);
3039 os_free(p2p->cfg->model_number);
3040 os_free(p2p->cfg->serial_number);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003041 os_free(p2p->cfg->pref_chan);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003042 os_free(p2p->groups);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07003043 p2ps_prov_free(p2p);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003044 wpabuf_free(p2p->sd_resp);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003045 p2p_remove_wps_vendor_extensions(p2p);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003046 os_free(p2p->no_go_freq.range);
Dmitry Shmidtdda10c22015-03-24 16:05:01 -07003047 p2p_service_flush_asp(p2p);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003048
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003049 os_free(p2p);
3050}
3051
3052
3053void p2p_flush(struct p2p_data *p2p)
3054{
3055 struct p2p_device *dev, *prev;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003056
3057 p2p_ext_listen(p2p, 0, 0);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003058 p2p_stop_find(p2p);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003059 dl_list_for_each_safe(dev, prev, &p2p->devices, struct p2p_device,
3060 list) {
3061 dl_list_del(&dev->list);
3062 p2p_device_free(p2p, dev);
3063 }
3064 p2p_free_sd_queries(p2p);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003065 p2p->ssid_set = 0;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003066 p2ps_prov_free(p2p);
3067 p2p_reset_pending_pd(p2p);
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08003068 p2p->override_pref_op_class = 0;
3069 p2p->override_pref_channel = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003070}
3071
3072
3073int p2p_unauthorize(struct p2p_data *p2p, const u8 *addr)
3074{
3075 struct p2p_device *dev;
3076
3077 dev = p2p_get_device(p2p, addr);
3078 if (dev == NULL)
3079 return -1;
3080
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003081 p2p_dbg(p2p, "Unauthorizing " MACSTR, MAC2STR(addr));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003082
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003083 if (p2p->go_neg_peer == dev) {
3084 eloop_cancel_timeout(p2p_go_neg_wait_timeout, p2p, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003085 p2p->go_neg_peer = NULL;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003086 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003087
3088 dev->wps_method = WPS_NOT_READY;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003089 dev->oob_pw_id = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003090 dev->flags &= ~P2P_DEV_WAIT_GO_NEG_RESPONSE;
3091 dev->flags &= ~P2P_DEV_WAIT_GO_NEG_CONFIRM;
3092
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003093 return 0;
3094}
3095
3096
3097int p2p_set_dev_name(struct p2p_data *p2p, const char *dev_name)
3098{
3099 os_free(p2p->cfg->dev_name);
3100 if (dev_name) {
3101 p2p->cfg->dev_name = os_strdup(dev_name);
3102 if (p2p->cfg->dev_name == NULL)
3103 return -1;
3104 } else
3105 p2p->cfg->dev_name = NULL;
3106 return 0;
3107}
3108
3109
3110int p2p_set_manufacturer(struct p2p_data *p2p, const char *manufacturer)
3111{
3112 os_free(p2p->cfg->manufacturer);
3113 p2p->cfg->manufacturer = NULL;
3114 if (manufacturer) {
3115 p2p->cfg->manufacturer = os_strdup(manufacturer);
3116 if (p2p->cfg->manufacturer == NULL)
3117 return -1;
3118 }
3119
3120 return 0;
3121}
3122
3123
3124int p2p_set_model_name(struct p2p_data *p2p, const char *model_name)
3125{
3126 os_free(p2p->cfg->model_name);
3127 p2p->cfg->model_name = NULL;
3128 if (model_name) {
3129 p2p->cfg->model_name = os_strdup(model_name);
3130 if (p2p->cfg->model_name == NULL)
3131 return -1;
3132 }
3133
3134 return 0;
3135}
3136
3137
3138int p2p_set_model_number(struct p2p_data *p2p, const char *model_number)
3139{
3140 os_free(p2p->cfg->model_number);
3141 p2p->cfg->model_number = NULL;
3142 if (model_number) {
3143 p2p->cfg->model_number = os_strdup(model_number);
3144 if (p2p->cfg->model_number == NULL)
3145 return -1;
3146 }
3147
3148 return 0;
3149}
3150
3151
3152int p2p_set_serial_number(struct p2p_data *p2p, const char *serial_number)
3153{
3154 os_free(p2p->cfg->serial_number);
3155 p2p->cfg->serial_number = NULL;
3156 if (serial_number) {
3157 p2p->cfg->serial_number = os_strdup(serial_number);
3158 if (p2p->cfg->serial_number == NULL)
3159 return -1;
3160 }
3161
3162 return 0;
3163}
3164
3165
3166void p2p_set_config_methods(struct p2p_data *p2p, u16 config_methods)
3167{
3168 p2p->cfg->config_methods = config_methods;
3169}
3170
3171
3172void p2p_set_uuid(struct p2p_data *p2p, const u8 *uuid)
3173{
3174 os_memcpy(p2p->cfg->uuid, uuid, 16);
3175}
3176
3177
3178int p2p_set_pri_dev_type(struct p2p_data *p2p, const u8 *pri_dev_type)
3179{
3180 os_memcpy(p2p->cfg->pri_dev_type, pri_dev_type, 8);
3181 return 0;
3182}
3183
3184
3185int p2p_set_sec_dev_types(struct p2p_data *p2p, const u8 dev_types[][8],
3186 size_t num_dev_types)
3187{
3188 if (num_dev_types > P2P_SEC_DEVICE_TYPES)
3189 num_dev_types = P2P_SEC_DEVICE_TYPES;
3190 p2p->cfg->num_sec_dev_types = num_dev_types;
3191 os_memcpy(p2p->cfg->sec_dev_type, dev_types, num_dev_types * 8);
3192 return 0;
3193}
3194
3195
3196void p2p_remove_wps_vendor_extensions(struct p2p_data *p2p)
3197{
3198 int i;
3199
3200 for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) {
3201 wpabuf_free(p2p->wps_vendor_ext[i]);
3202 p2p->wps_vendor_ext[i] = NULL;
3203 }
3204}
3205
3206
3207int p2p_add_wps_vendor_extension(struct p2p_data *p2p,
3208 const struct wpabuf *vendor_ext)
3209{
3210 int i;
3211
3212 if (vendor_ext == NULL)
3213 return -1;
3214
3215 for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) {
3216 if (p2p->wps_vendor_ext[i] == NULL)
3217 break;
3218 }
3219 if (i >= P2P_MAX_WPS_VENDOR_EXT)
3220 return -1;
3221
3222 p2p->wps_vendor_ext[i] = wpabuf_dup(vendor_ext);
3223 if (p2p->wps_vendor_ext[i] == NULL)
3224 return -1;
3225
3226 return 0;
3227}
3228
3229
3230int p2p_set_country(struct p2p_data *p2p, const char *country)
3231{
3232 os_memcpy(p2p->cfg->country, country, 3);
3233 return 0;
3234}
3235
3236
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003237static int p2p_pre_find_operation(struct p2p_data *p2p, struct p2p_device *dev)
3238{
Dmitry Shmidtde47be72016-01-07 12:52:55 -08003239 int res;
3240
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003241 if (dev->sd_pending_bcast_queries == 0) {
3242 /* Initialize with total number of registered broadcast
3243 * SD queries. */
3244 dev->sd_pending_bcast_queries = p2p->num_p2p_sd_queries;
3245 }
3246
Dmitry Shmidtde47be72016-01-07 12:52:55 -08003247 res = p2p_start_sd(p2p, dev);
3248 if (res == -2)
3249 return -2;
3250 if (res == 0)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003251 return 1;
3252
3253 if (dev->req_config_methods &&
3254 !(dev->flags & P2P_DEV_PD_FOR_JOIN)) {
3255 p2p_dbg(p2p, "Send pending Provision Discovery Request to "
3256 MACSTR " (config methods 0x%x)",
3257 MAC2STR(dev->info.p2p_device_addr),
3258 dev->req_config_methods);
3259 if (p2p_send_prov_disc_req(p2p, dev, 0, 0) == 0)
3260 return 1;
3261 }
3262
3263 return 0;
3264}
3265
3266
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003267void p2p_continue_find(struct p2p_data *p2p)
3268{
3269 struct p2p_device *dev;
Dmitry Shmidtde47be72016-01-07 12:52:55 -08003270 int found, res;
Dmitry Shmidt13ca8d82014-02-20 10:18:40 -08003271
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003272 p2p_set_state(p2p, P2P_SEARCH);
3273
3274 /* Continue from the device following the last iteration */
3275 found = 0;
3276 dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
3277 if (dev == p2p->last_p2p_find_oper) {
3278 found = 1;
3279 continue;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003280 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003281 if (!found)
3282 continue;
Dmitry Shmidtde47be72016-01-07 12:52:55 -08003283 res = p2p_pre_find_operation(p2p, dev);
3284 if (res > 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003285 p2p->last_p2p_find_oper = dev;
3286 return;
3287 }
Dmitry Shmidtde47be72016-01-07 12:52:55 -08003288 if (res == -2)
3289 goto skip_sd;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003290 }
3291
3292 /*
3293 * Wrap around to the beginning of the list and continue until the last
3294 * iteration device.
3295 */
3296 dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
Dmitry Shmidtde47be72016-01-07 12:52:55 -08003297 res = p2p_pre_find_operation(p2p, dev);
3298 if (res > 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003299 p2p->last_p2p_find_oper = dev;
3300 return;
3301 }
Dmitry Shmidtde47be72016-01-07 12:52:55 -08003302 if (res == -2)
3303 goto skip_sd;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003304 if (dev == p2p->last_p2p_find_oper)
3305 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003306 }
3307
Dmitry Shmidtde47be72016-01-07 12:52:55 -08003308skip_sd:
3309 os_memset(p2p->sd_query_no_ack, 0, ETH_ALEN);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003310 p2p_listen_in_find(p2p, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003311}
3312
3313
3314static void p2p_sd_cb(struct p2p_data *p2p, int success)
3315{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003316 p2p_dbg(p2p, "Service Discovery Query TX callback: success=%d",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003317 success);
3318 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
3319
3320 if (!success) {
Dmitry Shmidtde47be72016-01-07 12:52:55 -08003321 if (p2p->sd_peer) {
3322 if (is_zero_ether_addr(p2p->sd_query_no_ack)) {
3323 os_memcpy(p2p->sd_query_no_ack,
3324 p2p->sd_peer->info.p2p_device_addr,
3325 ETH_ALEN);
3326 p2p_dbg(p2p,
3327 "First SD Query no-ACK in this search iteration: "
3328 MACSTR, MAC2STR(p2p->sd_query_no_ack));
3329 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003330 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
Dmitry Shmidtde47be72016-01-07 12:52:55 -08003331 }
Dmitry Shmidt13ca8d82014-02-20 10:18:40 -08003332 p2p->sd_peer = NULL;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003333 if (p2p->state != P2P_IDLE)
3334 p2p_continue_find(p2p);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003335 return;
3336 }
3337
3338 if (p2p->sd_peer == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003339 p2p_dbg(p2p, "No SD peer entry known");
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003340 if (p2p->state != P2P_IDLE)
3341 p2p_continue_find(p2p);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003342 return;
3343 }
3344
Dmitry Shmidt807291d2015-01-27 13:40:23 -08003345 if (p2p->sd_query && p2p->sd_query->for_all_peers) {
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07003346 /* Update the pending broadcast SD query count for this device
3347 */
3348 p2p->sd_peer->sd_pending_bcast_queries--;
3349
3350 /*
3351 * If there are no pending broadcast queries for this device,
3352 * mark it as done (-1).
3353 */
3354 if (p2p->sd_peer->sd_pending_bcast_queries == 0)
3355 p2p->sd_peer->sd_pending_bcast_queries = -1;
3356 }
3357
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003358 /* Wait for response from the peer */
3359 p2p_set_state(p2p, P2P_SD_DURING_FIND);
3360 p2p_set_timeout(p2p, 0, 200000);
3361}
3362
Jouni Malinen75ecf522011-06-27 15:19:46 -07003363
3364/**
3365 * p2p_retry_pd - Retry any pending provision disc requests in IDLE state
3366 * @p2p: P2P module context from p2p_init()
3367 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003368static void p2p_retry_pd(struct p2p_data *p2p)
Jouni Malinen75ecf522011-06-27 15:19:46 -07003369{
3370 struct p2p_device *dev;
3371
Jouni Malinen75ecf522011-06-27 15:19:46 -07003372 /*
3373 * Retry the prov disc req attempt only for the peer that the user had
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003374 * requested.
Jouni Malinen75ecf522011-06-27 15:19:46 -07003375 */
3376
3377 dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
3378 if (os_memcmp(p2p->pending_pd_devaddr,
3379 dev->info.p2p_device_addr, ETH_ALEN) != 0)
3380 continue;
3381 if (!dev->req_config_methods)
3382 continue;
Jouni Malinen75ecf522011-06-27 15:19:46 -07003383
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003384 p2p_dbg(p2p, "Send pending Provision Discovery Request to "
Jouni Malinen75ecf522011-06-27 15:19:46 -07003385 MACSTR " (config methods 0x%x)",
3386 MAC2STR(dev->info.p2p_device_addr),
3387 dev->req_config_methods);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003388 p2p_send_prov_disc_req(p2p, dev,
Dmitry Shmidt051af732013-10-22 13:52:46 -07003389 dev->flags & P2P_DEV_PD_FOR_JOIN,
3390 p2p->pd_force_freq);
Jouni Malinen75ecf522011-06-27 15:19:46 -07003391 return;
3392 }
3393}
3394
3395
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003396static void p2p_prov_disc_cb(struct p2p_data *p2p, int success)
3397{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003398 p2p_dbg(p2p, "Provision Discovery Request TX callback: success=%d",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003399 success);
Jouni Malinen75ecf522011-06-27 15:19:46 -07003400
3401 /*
3402 * Postpone resetting the pending action state till after we actually
3403 * time out. This allows us to take some action like notifying any
3404 * interested parties about no response to the request.
3405 *
3406 * When the timer (below) goes off we check in IDLE, SEARCH, or
3407 * LISTEN_ONLY state, which are the only allowed states to issue a PD
3408 * requests in, if this was still pending and then raise notification.
3409 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003410
3411 if (!success) {
Jouni Malinen75ecf522011-06-27 15:19:46 -07003412 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
3413
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003414 if (p2p->user_initiated_pd &&
3415 (p2p->state == P2P_SEARCH || p2p->state == P2P_LISTEN_ONLY))
3416 {
3417 /* Retry request from timeout to avoid busy loops */
3418 p2p->pending_action_state = P2P_PENDING_PD;
3419 p2p_set_timeout(p2p, 0, 50000);
3420 } else if (p2p->state != P2P_IDLE)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003421 p2p_continue_find(p2p);
Jouni Malinen75ecf522011-06-27 15:19:46 -07003422 else if (p2p->user_initiated_pd) {
3423 p2p->pending_action_state = P2P_PENDING_PD;
3424 p2p_set_timeout(p2p, 0, 300000);
3425 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003426 return;
3427 }
3428
Jouni Malinen75ecf522011-06-27 15:19:46 -07003429 /*
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003430 * If after PD Request the peer doesn't expect to receive PD Response
3431 * the PD Request ACK indicates a completion of the current PD. This
3432 * happens only on the advertiser side sending the follow-on PD Request
3433 * with the status different than 12 (Success: accepted by user).
3434 */
3435 if (p2p->p2ps_prov && !p2p->p2ps_prov->pd_seeker &&
3436 p2p->p2ps_prov->status != P2P_SC_SUCCESS_DEFERRED) {
3437 p2p_dbg(p2p, "P2PS PD completion on Follow-on PD Request ACK");
3438
3439 if (p2p->send_action_in_progress) {
3440 p2p->send_action_in_progress = 0;
3441 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
3442 }
3443
3444 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
3445
3446 if (p2p->cfg->p2ps_prov_complete) {
3447 p2p->cfg->p2ps_prov_complete(
3448 p2p->cfg->cb_ctx,
3449 p2p->p2ps_prov->status,
3450 p2p->p2ps_prov->adv_mac,
3451 p2p->p2ps_prov->adv_mac,
3452 p2p->p2ps_prov->session_mac,
3453 NULL, p2p->p2ps_prov->adv_id,
3454 p2p->p2ps_prov->session_id,
3455 0, 0, NULL, 0, 0, 0,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08003456 NULL, NULL, 0, 0, NULL, 0);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003457 }
3458
3459 if (p2p->user_initiated_pd)
3460 p2p_reset_pending_pd(p2p);
3461
3462 p2ps_prov_free(p2p);
3463 return;
3464 }
3465
3466 /*
Jouni Malinen75ecf522011-06-27 15:19:46 -07003467 * This postponing, of resetting pending_action_state, needs to be
3468 * done only for user initiated PD requests and not internal ones.
3469 */
3470 if (p2p->user_initiated_pd)
3471 p2p->pending_action_state = P2P_PENDING_PD;
3472 else
3473 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
3474
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003475 /* Wait for response from the peer */
3476 if (p2p->state == P2P_SEARCH)
3477 p2p_set_state(p2p, P2P_PD_DURING_FIND);
3478 p2p_set_timeout(p2p, 0, 200000);
3479}
3480
3481
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003482static void p2p_prov_disc_resp_cb(struct p2p_data *p2p, int success)
3483{
3484 p2p_dbg(p2p, "Provision Discovery Response TX callback: success=%d",
3485 success);
3486
3487 if (p2p->send_action_in_progress) {
3488 p2p->send_action_in_progress = 0;
3489 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
3490 }
3491
3492 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
3493
Jimmy Chen5ef7aad2019-10-15 15:45:26 +08003494 if (!success) {
3495 if (p2p->state == P2P_SEARCH)
3496 p2p_continue_find(p2p);
Hai Shalom81f62d82019-07-22 12:10:00 -07003497 return;
Jimmy Chen5ef7aad2019-10-15 15:45:26 +08003498 }
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003499
3500 if (!p2p->cfg->prov_disc_resp_cb ||
Jimmy Chen5ef7aad2019-10-15 15:45:26 +08003501 p2p->cfg->prov_disc_resp_cb(p2p->cfg->cb_ctx) < 1) {
3502 if (p2p->state == P2P_SEARCH)
3503 p2p_continue_find(p2p);
Hai Shalom81f62d82019-07-22 12:10:00 -07003504 return;
Jimmy Chen5ef7aad2019-10-15 15:45:26 +08003505 }
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003506
3507 p2p_dbg(p2p,
3508 "Post-Provision Discovery operations started - do not try to continue other P2P operations");
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003509}
3510
3511
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003512int p2p_scan_res_handler(struct p2p_data *p2p, const u8 *bssid, int freq,
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003513 struct os_reltime *rx_time, int level, const u8 *ies,
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003514 size_t ies_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003515{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003516 if (os_reltime_before(rx_time, &p2p->find_start)) {
Dmitry Shmidtf8623282013-02-20 14:34:59 -08003517 /*
3518 * The driver may have cached (e.g., in cfg80211 BSS table) the
3519 * scan results for relatively long time. To avoid reporting
3520 * stale information, update P2P peers only based on results
3521 * that have based on frames received after the last p2p_find
3522 * operation was started.
3523 */
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003524 p2p_dbg(p2p, "Ignore old scan result for " MACSTR
Dmitry Shmidtde47be72016-01-07 12:52:55 -08003525 " (rx_time=%u.%06u find_start=%u.%06u)",
Dmitry Shmidtf8623282013-02-20 14:34:59 -08003526 MAC2STR(bssid), (unsigned int) rx_time->sec,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08003527 (unsigned int) rx_time->usec,
3528 (unsigned int) p2p->find_start.sec,
3529 (unsigned int) p2p->find_start.usec);
Dmitry Shmidtf8623282013-02-20 14:34:59 -08003530 return 0;
3531 }
3532
3533 p2p_add_device(p2p, bssid, freq, rx_time, level, ies, ies_len, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003534
3535 return 0;
3536}
3537
3538
Hai Shalom60840252021-02-19 19:02:11 -08003539void p2p_scan_res_handled(struct p2p_data *p2p, unsigned int delay)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003540{
3541 if (!p2p->p2p_scan_running) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003542 p2p_dbg(p2p, "p2p_scan was not running, but scan results received");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003543 }
3544 p2p->p2p_scan_running = 0;
Hai Shalom60840252021-02-19 19:02:11 -08003545
3546 /* Use this delay only when p2p_find doesn't set it */
3547 if (!p2p->search_delay)
3548 p2p->search_delay = delay;
3549
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003550 eloop_cancel_timeout(p2p_scan_timeout, p2p, NULL);
3551
3552 if (p2p_run_after_scan(p2p))
3553 return;
3554 if (p2p->state == P2P_SEARCH)
3555 p2p_continue_find(p2p);
3556}
3557
3558
Dmitry Shmidt9c175262016-03-03 10:20:07 -08003559void p2p_scan_ie(struct p2p_data *p2p, struct wpabuf *ies, const u8 *dev_id,
3560 unsigned int bands)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003561{
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003562 u8 dev_capab;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003563 u8 *len;
3564
3565#ifdef CONFIG_WIFI_DISPLAY
3566 if (p2p->wfd_ie_probe_req)
3567 wpabuf_put_buf(ies, p2p->wfd_ie_probe_req);
3568#endif /* CONFIG_WIFI_DISPLAY */
3569
Dmitry Shmidt2e67f062014-07-16 09:55:28 -07003570 if (p2p->vendor_elem && p2p->vendor_elem[VENDOR_ELEM_PROBE_REQ_P2P])
3571 wpabuf_put_buf(ies,
3572 p2p->vendor_elem[VENDOR_ELEM_PROBE_REQ_P2P]);
3573
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003574 len = p2p_buf_add_ie_hdr(ies);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003575
3576 dev_capab = p2p->dev_capab & ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
3577
3578 /* P2PS requires Probe Request frames to include SD bit */
3579 if (p2p->p2ps_seek && p2p->p2ps_seek_count)
3580 dev_capab |= P2P_DEV_CAPAB_SERVICE_DISCOVERY;
3581
3582 p2p_buf_add_capability(ies, dev_capab, 0);
3583
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003584 if (dev_id)
3585 p2p_buf_add_device_id(ies, dev_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003586 if (p2p->cfg->reg_class && p2p->cfg->channel)
3587 p2p_buf_add_listen_channel(ies, p2p->cfg->country,
3588 p2p->cfg->reg_class,
3589 p2p->cfg->channel);
3590 if (p2p->ext_listen_interval)
3591 p2p_buf_add_ext_listen_timing(ies, p2p->ext_listen_period,
3592 p2p->ext_listen_interval);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003593
Dmitry Shmidt9c175262016-03-03 10:20:07 -08003594 if (bands & BAND_60_GHZ)
3595 p2p_buf_add_device_info(ies, p2p, NULL);
3596
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003597 if (p2p->p2ps_seek && p2p->p2ps_seek_count)
3598 p2p_buf_add_service_hash(ies, p2p);
3599
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003600 /* TODO: p2p_buf_add_operating_channel() if GO */
3601 p2p_buf_update_ie_hdr(ies, len);
3602}
3603
3604
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003605size_t p2p_scan_ie_buf_len(struct p2p_data *p2p)
3606{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003607 size_t len = 100;
3608
3609#ifdef CONFIG_WIFI_DISPLAY
3610 if (p2p && p2p->wfd_ie_probe_req)
3611 len += wpabuf_len(p2p->wfd_ie_probe_req);
3612#endif /* CONFIG_WIFI_DISPLAY */
3613
Dmitry Shmidt2e67f062014-07-16 09:55:28 -07003614 if (p2p && p2p->vendor_elem &&
3615 p2p->vendor_elem[VENDOR_ELEM_PROBE_REQ_P2P])
3616 len += wpabuf_len(p2p->vendor_elem[VENDOR_ELEM_PROBE_REQ_P2P]);
3617
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003618 return len;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003619}
3620
3621
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003622int p2p_ie_text(struct wpabuf *p2p_ie, char *buf, char *end)
3623{
3624 return p2p_attr_text(p2p_ie, buf, end);
3625}
3626
3627
3628static void p2p_go_neg_req_cb(struct p2p_data *p2p, int success)
3629{
3630 struct p2p_device *dev = p2p->go_neg_peer;
Dmitry Shmidt8c652892013-03-01 10:14:01 -08003631 int timeout;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003632
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003633 p2p_dbg(p2p, "GO Negotiation Request TX callback: success=%d", success);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003634
3635 if (dev == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003636 p2p_dbg(p2p, "No pending GO Negotiation");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003637 return;
3638 }
3639
3640 if (success) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003641 if (dev->flags & P2P_DEV_USER_REJECTED) {
3642 p2p_set_state(p2p, P2P_IDLE);
3643 return;
3644 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07003645 } else if (dev->go_neg_req_sent) {
3646 /* Cancel the increment from p2p_connect_send() on failure */
Dmitry Shmidt98f9e762012-05-30 11:18:46 -07003647 dev->go_neg_req_sent--;
3648 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003649
3650 if (!success &&
3651 (dev->info.dev_capab & P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY) &&
3652 !is_zero_ether_addr(dev->member_in_go_dev)) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003653 p2p_dbg(p2p, "Peer " MACSTR " did not acknowledge request - try to use device discoverability through its GO",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003654 MAC2STR(dev->info.p2p_device_addr));
3655 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
3656 p2p_send_dev_disc_req(p2p, dev);
3657 return;
3658 }
3659
3660 /*
3661 * Use P2P find, if needed, to find the other device from its listen
3662 * channel.
3663 */
3664 p2p_set_state(p2p, P2P_CONNECT);
Dmitry Shmidt8c652892013-03-01 10:14:01 -08003665 timeout = success ? 500000 : 100000;
3666 if (!success && p2p->go_neg_peer &&
3667 (p2p->go_neg_peer->flags & P2P_DEV_PEER_WAITING_RESPONSE)) {
3668 unsigned int r;
3669 /*
3670 * Peer is expected to wait our response and we will skip the
3671 * listen phase. Add some randomness to the wait time here to
3672 * make it less likely to hit cases where we could end up in
3673 * sync with peer not listening.
3674 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07003675 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
3676 r = 0;
Dmitry Shmidt8c652892013-03-01 10:14:01 -08003677 timeout += r % 100000;
3678 }
3679 p2p_set_timeout(p2p, 0, timeout);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003680}
3681
3682
3683static void p2p_go_neg_resp_cb(struct p2p_data *p2p, int success)
3684{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003685 p2p_dbg(p2p, "GO Negotiation Response TX callback: success=%d",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003686 success);
3687 if (!p2p->go_neg_peer && p2p->state == P2P_PROVISIONING) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003688 p2p_dbg(p2p, "Ignore TX callback event - GO Negotiation is not running anymore");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003689 return;
3690 }
3691 p2p_set_state(p2p, P2P_CONNECT);
Dmitry Shmidt8c652892013-03-01 10:14:01 -08003692 p2p_set_timeout(p2p, 0, 500000);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003693}
3694
3695
Dmitry Shmidt4b060592013-04-29 16:42:49 -07003696static void p2p_go_neg_resp_failure_cb(struct p2p_data *p2p, int success,
3697 const u8 *addr)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003698{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003699 p2p_dbg(p2p, "GO Negotiation Response (failure) TX callback: success=%d", success);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003700 if (p2p->go_neg_peer && p2p->go_neg_peer->status != P2P_SC_SUCCESS) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003701 p2p_go_neg_failed(p2p, p2p->go_neg_peer->status);
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08003702 return;
3703 }
3704
3705 if (success) {
Dmitry Shmidt4b060592013-04-29 16:42:49 -07003706 struct p2p_device *dev;
3707 dev = p2p_get_device(p2p, addr);
3708 if (dev &&
Dmitry Shmidt34c12022015-03-05 14:11:20 -08003709 dev->status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE)
Dmitry Shmidt4b060592013-04-29 16:42:49 -07003710 dev->flags |= P2P_DEV_PEER_WAITING_RESPONSE;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003711 }
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08003712
3713 if (p2p->state == P2P_SEARCH || p2p->state == P2P_SD_DURING_FIND)
3714 p2p_continue_find(p2p);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003715}
3716
3717
3718static void p2p_go_neg_conf_cb(struct p2p_data *p2p,
3719 enum p2p_send_action_result result)
3720{
3721 struct p2p_device *dev;
3722
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003723 p2p_dbg(p2p, "GO Negotiation Confirm TX callback: result=%d", result);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003724 if (result == P2P_SEND_ACTION_FAILED) {
Dmitry Shmidt413dde72014-04-11 10:23:22 -07003725 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003726 p2p_go_neg_failed(p2p, -1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003727 return;
3728 }
Dmitry Shmidt413dde72014-04-11 10:23:22 -07003729
3730 dev = p2p->go_neg_peer;
3731
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003732 if (result == P2P_SEND_ACTION_NO_ACK) {
3733 /*
Dmitry Shmidt413dde72014-04-11 10:23:22 -07003734 * Retry GO Negotiation Confirmation
3735 * P2P_GO_NEG_CNF_MAX_RETRY_COUNT times if we did not receive
3736 * ACK for confirmation.
3737 */
3738 if (dev && dev->go_neg_conf &&
3739 dev->go_neg_conf_sent <= P2P_GO_NEG_CNF_MAX_RETRY_COUNT) {
3740 p2p_dbg(p2p, "GO Negotiation Confirm retry %d",
3741 dev->go_neg_conf_sent);
3742 p2p->pending_action_state = P2P_PENDING_GO_NEG_CONFIRM;
3743 if (p2p_send_action(p2p, dev->go_neg_conf_freq,
3744 dev->info.p2p_device_addr,
3745 p2p->cfg->dev_addr,
3746 dev->info.p2p_device_addr,
3747 wpabuf_head(dev->go_neg_conf),
3748 wpabuf_len(dev->go_neg_conf), 0) >=
3749 0) {
3750 dev->go_neg_conf_sent++;
3751 return;
3752 }
3753 p2p_dbg(p2p, "Failed to re-send Action frame");
3754
3755 /*
3756 * Continue with the assumption that the first attempt
3757 * went through and just the ACK frame was lost.
3758 */
3759 }
3760
3761 /*
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003762 * It looks like the TX status for GO Negotiation Confirm is
3763 * often showing failure even when the peer has actually
3764 * received the frame. Since the peer may change channels
3765 * immediately after having received the frame, we may not see
3766 * an Ack for retries, so just dropping a single frame may
3767 * trigger this. To allow the group formation to succeed if the
3768 * peer did indeed receive the frame, continue regardless of
3769 * the TX status.
3770 */
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003771 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 -07003772 }
3773
Dmitry Shmidt413dde72014-04-11 10:23:22 -07003774 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
3775
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003776 if (dev == NULL)
3777 return;
3778
3779 p2p_go_complete(p2p, dev);
3780}
3781
3782
3783void p2p_send_action_cb(struct p2p_data *p2p, unsigned int freq, const u8 *dst,
3784 const u8 *src, const u8 *bssid,
3785 enum p2p_send_action_result result)
3786{
3787 enum p2p_pending_action_state state;
3788 int success;
3789
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003790 p2p_dbg(p2p, "Action frame TX callback (state=%d freq=%u dst=" MACSTR
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003791 " src=" MACSTR " bssid=" MACSTR " result=%d p2p_state=%s)",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003792 p2p->pending_action_state, freq, MAC2STR(dst), MAC2STR(src),
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003793 MAC2STR(bssid), result, p2p_state_txt(p2p->state));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003794 success = result == P2P_SEND_ACTION_SUCCESS;
3795 state = p2p->pending_action_state;
3796 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
3797 switch (state) {
3798 case P2P_NO_PENDING_ACTION:
Dmitry Shmidt4582d2a2014-02-28 11:14:23 -08003799 if (p2p->send_action_in_progress) {
3800 p2p->send_action_in_progress = 0;
3801 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
3802 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003803 break;
3804 case P2P_PENDING_GO_NEG_REQUEST:
3805 p2p_go_neg_req_cb(p2p, success);
3806 break;
3807 case P2P_PENDING_GO_NEG_RESPONSE:
3808 p2p_go_neg_resp_cb(p2p, success);
3809 break;
3810 case P2P_PENDING_GO_NEG_RESPONSE_FAILURE:
Dmitry Shmidt4b060592013-04-29 16:42:49 -07003811 p2p_go_neg_resp_failure_cb(p2p, success, dst);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003812 break;
3813 case P2P_PENDING_GO_NEG_CONFIRM:
3814 p2p_go_neg_conf_cb(p2p, result);
3815 break;
3816 case P2P_PENDING_SD:
3817 p2p_sd_cb(p2p, success);
3818 break;
3819 case P2P_PENDING_PD:
3820 p2p_prov_disc_cb(p2p, success);
3821 break;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003822 case P2P_PENDING_PD_RESPONSE:
3823 p2p_prov_disc_resp_cb(p2p, success);
3824 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003825 case P2P_PENDING_INVITATION_REQUEST:
3826 p2p_invitation_req_cb(p2p, success);
3827 break;
3828 case P2P_PENDING_INVITATION_RESPONSE:
3829 p2p_invitation_resp_cb(p2p, success);
3830 break;
3831 case P2P_PENDING_DEV_DISC_REQUEST:
3832 p2p_dev_disc_req_cb(p2p, success);
3833 break;
3834 case P2P_PENDING_DEV_DISC_RESPONSE:
3835 p2p_dev_disc_resp_cb(p2p, success);
3836 break;
3837 case P2P_PENDING_GO_DISC_REQ:
3838 p2p_go_disc_req_cb(p2p, success);
3839 break;
3840 }
3841}
3842
3843
3844void p2p_listen_cb(struct p2p_data *p2p, unsigned int freq,
3845 unsigned int duration)
3846{
3847 if (freq == p2p->pending_client_disc_freq) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003848 p2p_dbg(p2p, "Client discoverability remain-awake completed");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003849 p2p->pending_client_disc_freq = 0;
3850 return;
3851 }
3852
3853 if (freq != p2p->pending_listen_freq) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003854 p2p_dbg(p2p, "Unexpected listen callback for freq=%u duration=%u (pending_listen_freq=%u)",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003855 freq, duration, p2p->pending_listen_freq);
3856 return;
3857 }
3858
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003859 p2p_dbg(p2p, "Starting Listen timeout(%u,%u) on freq=%u based on callback",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003860 p2p->pending_listen_sec, p2p->pending_listen_usec,
3861 p2p->pending_listen_freq);
3862 p2p->in_listen = 1;
3863 p2p->drv_in_listen = freq;
3864 if (p2p->pending_listen_sec || p2p->pending_listen_usec) {
3865 /*
3866 * Add 20 msec extra wait to avoid race condition with driver
3867 * remain-on-channel end event, i.e., give driver more time to
3868 * complete the operation before our timeout expires.
3869 */
3870 p2p_set_timeout(p2p, p2p->pending_listen_sec,
3871 p2p->pending_listen_usec + 20000);
3872 }
3873
3874 p2p->pending_listen_freq = 0;
3875}
3876
3877
3878int p2p_listen_end(struct p2p_data *p2p, unsigned int freq)
3879{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003880 p2p_dbg(p2p, "Driver ended Listen state (freq=%u)", freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003881 p2p->drv_in_listen = 0;
3882 if (p2p->in_listen)
3883 return 0; /* Internal timeout will trigger the next step */
3884
Roshan Pius3a1667e2018-07-03 15:17:14 -07003885 if (p2p->state == P2P_WAIT_PEER_CONNECT && p2p->go_neg_peer &&
3886 p2p->pending_listen_freq) {
3887 /*
3888 * Better wait a bit if the driver is unable to start
3889 * offchannel operation for some reason to continue with
3890 * P2P_WAIT_PEER_(IDLE/CONNECT) state transitions.
3891 */
3892 p2p_dbg(p2p,
3893 "Listen operation did not seem to start - delay idle phase to avoid busy loop");
3894 p2p_set_timeout(p2p, 0, 100000);
3895 return 1;
3896 }
3897
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003898 if (p2p->state == P2P_CONNECT_LISTEN && p2p->go_neg_peer) {
3899 if (p2p->go_neg_peer->connect_reqs >= 120) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003900 p2p_dbg(p2p, "Timeout on sending GO Negotiation Request without getting response");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003901 p2p_go_neg_failed(p2p, -1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003902 return 0;
3903 }
3904
3905 p2p_set_state(p2p, P2P_CONNECT);
3906 p2p_connect_send(p2p, p2p->go_neg_peer);
3907 return 1;
3908 } else if (p2p->state == P2P_SEARCH) {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003909 if (p2p->p2p_scan_running) {
3910 /*
3911 * Search is already in progress. This can happen if
3912 * an Action frame RX is reported immediately after
3913 * the end of a remain-on-channel operation and the
3914 * response frame to that is sent using an offchannel
3915 * operation while in p2p_find. Avoid an attempt to
3916 * restart a scan here.
3917 */
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003918 p2p_dbg(p2p, "p2p_scan already in progress - do not try to start a new one");
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003919 return 1;
3920 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07003921 if (p2p->pending_listen_freq) {
3922 /*
3923 * Better wait a bit if the driver is unable to start
3924 * offchannel operation for some reason. p2p_search()
3925 * will be started from internal timeout.
3926 */
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003927 p2p_dbg(p2p, "Listen operation did not seem to start - delay search phase to avoid busy loop");
Dmitry Shmidt04949592012-07-19 12:16:46 -07003928 p2p_set_timeout(p2p, 0, 100000);
3929 return 1;
3930 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003931 if (p2p->search_delay) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003932 p2p_dbg(p2p, "Delay search operation by %u ms",
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003933 p2p->search_delay);
3934 p2p_set_timeout(p2p, p2p->search_delay / 1000,
3935 (p2p->search_delay % 1000) * 1000);
3936 return 1;
3937 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003938 p2p_search(p2p);
3939 return 1;
3940 }
3941
3942 return 0;
3943}
3944
3945
3946static void p2p_timeout_connect(struct p2p_data *p2p)
3947{
3948 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003949 if (p2p->go_neg_peer &&
3950 (p2p->go_neg_peer->flags & P2P_DEV_WAIT_GO_NEG_CONFIRM)) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003951 p2p_dbg(p2p, "Wait for GO Negotiation Confirm timed out - assume GO Negotiation failed");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003952 p2p_go_neg_failed(p2p, -1);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003953 return;
3954 }
Dmitry Shmidt8c652892013-03-01 10:14:01 -08003955 if (p2p->go_neg_peer &&
3956 (p2p->go_neg_peer->flags & P2P_DEV_PEER_WAITING_RESPONSE) &&
3957 p2p->go_neg_peer->connect_reqs < 120) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003958 p2p_dbg(p2p, "Peer expected to wait our response - skip listen");
Dmitry Shmidt8c652892013-03-01 10:14:01 -08003959 p2p_connect_send(p2p, p2p->go_neg_peer);
3960 return;
3961 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003962 if (p2p->go_neg_peer && p2p->go_neg_peer->oob_go_neg_freq > 0) {
3963 p2p_dbg(p2p, "Skip connect-listen since GO Neg channel known (OOB)");
3964 p2p_set_state(p2p, P2P_CONNECT_LISTEN);
3965 p2p_set_timeout(p2p, 0, 30000);
3966 return;
3967 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003968 p2p_set_state(p2p, P2P_CONNECT_LISTEN);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003969 p2p_listen_in_find(p2p, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003970}
3971
3972
3973static void p2p_timeout_connect_listen(struct p2p_data *p2p)
3974{
3975 if (p2p->go_neg_peer) {
3976 if (p2p->drv_in_listen) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003977 p2p_dbg(p2p, "Driver is still in Listen state; wait for it to complete");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003978 return;
3979 }
3980
3981 if (p2p->go_neg_peer->connect_reqs >= 120) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003982 p2p_dbg(p2p, "Timeout on sending GO Negotiation Request without getting response");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003983 p2p_go_neg_failed(p2p, -1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003984 return;
3985 }
3986
3987 p2p_set_state(p2p, P2P_CONNECT);
3988 p2p_connect_send(p2p, p2p->go_neg_peer);
3989 } else
3990 p2p_set_state(p2p, P2P_IDLE);
3991}
3992
3993
3994static void p2p_timeout_wait_peer_connect(struct p2p_data *p2p)
3995{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003996 p2p_set_state(p2p, P2P_WAIT_PEER_IDLE);
Dmitry Shmidt18463232014-01-24 12:29:41 -08003997
3998 if (p2p->cfg->is_concurrent_session_active &&
3999 p2p->cfg->is_concurrent_session_active(p2p->cfg->cb_ctx))
4000 p2p_set_timeout(p2p, 0, 500000);
4001 else
4002 p2p_set_timeout(p2p, 0, 200000);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004003}
4004
4005
4006static void p2p_timeout_wait_peer_idle(struct p2p_data *p2p)
4007{
4008 struct p2p_device *dev = p2p->go_neg_peer;
4009
4010 if (dev == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004011 p2p_dbg(p2p, "Unknown GO Neg peer - stop GO Neg wait");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004012 return;
4013 }
4014
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004015 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 -07004016 p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
Hai Shaloma20dcd72022-02-04 13:43:00 -08004017 if (p2p->pending_listen_freq) {
4018 p2p_dbg(p2p, "Clear pending_listen_freq for %s", __func__);
4019 p2p->pending_listen_freq = 0;
4020 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004021 p2p_set_state(p2p, P2P_WAIT_PEER_CONNECT);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004022 p2p_listen_in_find(p2p, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004023}
4024
4025
4026static void p2p_timeout_sd_during_find(struct p2p_data *p2p)
4027{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004028 p2p_dbg(p2p, "Service Discovery Query timeout");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004029 if (p2p->sd_peer) {
4030 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004031 p2p->sd_peer = NULL;
4032 }
4033 p2p_continue_find(p2p);
4034}
4035
4036
4037static void p2p_timeout_prov_disc_during_find(struct p2p_data *p2p)
4038{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004039 p2p_dbg(p2p, "Provision Discovery Request timeout");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004040 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
4041 p2p_continue_find(p2p);
4042}
4043
4044
Jouni Malinen75ecf522011-06-27 15:19:46 -07004045static void p2p_timeout_prov_disc_req(struct p2p_data *p2p)
4046{
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004047 u32 adv_id = 0;
4048 u8 *adv_mac = NULL;
4049
Jouni Malinen75ecf522011-06-27 15:19:46 -07004050 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
4051
4052 /*
4053 * For user initiated PD requests that we have not gotten any responses
4054 * for while in IDLE state, we retry them a couple of times before
4055 * giving up.
4056 */
4057 if (!p2p->user_initiated_pd)
4058 return;
4059
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004060 p2p_dbg(p2p, "User initiated Provision Discovery Request timeout");
Jouni Malinen75ecf522011-06-27 15:19:46 -07004061
4062 if (p2p->pd_retries) {
4063 p2p->pd_retries--;
4064 p2p_retry_pd(p2p);
4065 } else {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004066 struct p2p_device *dev;
4067 int for_join = 0;
4068
4069 dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
4070 if (os_memcmp(p2p->pending_pd_devaddr,
4071 dev->info.p2p_device_addr, ETH_ALEN) != 0)
4072 continue;
4073 if (dev->req_config_methods &&
4074 (dev->flags & P2P_DEV_PD_FOR_JOIN))
4075 for_join = 1;
4076 }
4077
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004078 if (p2p->p2ps_prov) {
4079 adv_id = p2p->p2ps_prov->adv_id;
4080 adv_mac = p2p->p2ps_prov->adv_mac;
4081 }
4082
Jouni Malinen75ecf522011-06-27 15:19:46 -07004083 if (p2p->cfg->prov_disc_fail)
4084 p2p->cfg->prov_disc_fail(p2p->cfg->cb_ctx,
4085 p2p->pending_pd_devaddr,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004086 for_join ?
4087 P2P_PROV_DISC_TIMEOUT_JOIN :
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004088 P2P_PROV_DISC_TIMEOUT,
4089 adv_id, adv_mac, NULL);
Jouni Malinen75ecf522011-06-27 15:19:46 -07004090 p2p_reset_pending_pd(p2p);
4091 }
4092}
4093
4094
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004095static void p2p_timeout_invite(struct p2p_data *p2p)
4096{
4097 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
4098 p2p_set_state(p2p, P2P_INVITE_LISTEN);
4099 if (p2p->inv_role == P2P_INVITE_ROLE_ACTIVE_GO) {
4100 /*
4101 * Better remain on operating channel instead of listen channel
4102 * when running a group.
4103 */
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004104 p2p_dbg(p2p, "Inviting in active GO role - wait on operating channel");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004105 p2p_set_timeout(p2p, 0, 100000);
4106 return;
4107 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004108 p2p_listen_in_find(p2p, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004109}
4110
4111
4112static void p2p_timeout_invite_listen(struct p2p_data *p2p)
4113{
4114 if (p2p->invite_peer && p2p->invite_peer->invitation_reqs < 100) {
4115 p2p_set_state(p2p, P2P_INVITE);
4116 p2p_invite_send(p2p, p2p->invite_peer,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004117 p2p->invite_go_dev_addr, p2p->invite_dev_pw_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004118 } else {
4119 if (p2p->invite_peer) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004120 p2p_dbg(p2p, "Invitation Request retry limit reached");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004121 if (p2p->cfg->invitation_result)
4122 p2p->cfg->invitation_result(
Dmitry Shmidt700a1372013-03-15 14:14:44 -07004123 p2p->cfg->cb_ctx, -1, NULL, NULL,
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004124 p2p->invite_peer->info.p2p_device_addr,
Dmitry Shmidt15907092014-03-25 10:42:57 -07004125 0, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004126 }
4127 p2p_set_state(p2p, P2P_IDLE);
4128 }
4129}
4130
4131
4132static void p2p_state_timeout(void *eloop_ctx, void *timeout_ctx)
4133{
4134 struct p2p_data *p2p = eloop_ctx;
4135
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004136 p2p_dbg(p2p, "Timeout (state=%s)", p2p_state_txt(p2p->state));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004137
4138 p2p->in_listen = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004139 if (p2p->drv_in_listen) {
4140 p2p_dbg(p2p, "Driver is still in listen state - stop it");
4141 p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
4142 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004143
4144 switch (p2p->state) {
4145 case P2P_IDLE:
Jouni Malinen75ecf522011-06-27 15:19:46 -07004146 /* Check if we timed out waiting for PD req */
4147 if (p2p->pending_action_state == P2P_PENDING_PD)
4148 p2p_timeout_prov_disc_req(p2p);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004149 break;
4150 case P2P_SEARCH:
Jouni Malinen75ecf522011-06-27 15:19:46 -07004151 /* Check if we timed out waiting for PD req */
4152 if (p2p->pending_action_state == P2P_PENDING_PD)
4153 p2p_timeout_prov_disc_req(p2p);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004154 if (p2p->search_delay && !p2p->in_search_delay) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004155 p2p_dbg(p2p, "Delay search operation by %u ms",
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004156 p2p->search_delay);
4157 p2p->in_search_delay = 1;
4158 p2p_set_timeout(p2p, p2p->search_delay / 1000,
4159 (p2p->search_delay % 1000) * 1000);
4160 break;
4161 }
4162 p2p->in_search_delay = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004163 p2p_search(p2p);
4164 break;
4165 case P2P_CONNECT:
4166 p2p_timeout_connect(p2p);
4167 break;
4168 case P2P_CONNECT_LISTEN:
4169 p2p_timeout_connect_listen(p2p);
4170 break;
4171 case P2P_GO_NEG:
4172 break;
4173 case P2P_LISTEN_ONLY:
Jouni Malinen75ecf522011-06-27 15:19:46 -07004174 /* Check if we timed out waiting for PD req */
4175 if (p2p->pending_action_state == P2P_PENDING_PD)
4176 p2p_timeout_prov_disc_req(p2p);
4177
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004178 if (p2p->ext_listen_only) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004179 p2p_dbg(p2p, "Extended Listen Timing - Listen State completed");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004180 p2p->ext_listen_only = 0;
4181 p2p_set_state(p2p, P2P_IDLE);
4182 }
4183 break;
4184 case P2P_WAIT_PEER_CONNECT:
4185 p2p_timeout_wait_peer_connect(p2p);
4186 break;
4187 case P2P_WAIT_PEER_IDLE:
4188 p2p_timeout_wait_peer_idle(p2p);
4189 break;
4190 case P2P_SD_DURING_FIND:
4191 p2p_timeout_sd_during_find(p2p);
4192 break;
4193 case P2P_PROVISIONING:
4194 break;
4195 case P2P_PD_DURING_FIND:
4196 p2p_timeout_prov_disc_during_find(p2p);
4197 break;
4198 case P2P_INVITE:
4199 p2p_timeout_invite(p2p);
4200 break;
4201 case P2P_INVITE_LISTEN:
4202 p2p_timeout_invite_listen(p2p);
4203 break;
4204 }
4205}
4206
4207
4208int p2p_reject(struct p2p_data *p2p, const u8 *peer_addr)
4209{
4210 struct p2p_device *dev;
4211
4212 dev = p2p_get_device(p2p, peer_addr);
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004213 p2p_dbg(p2p, "Local request to reject connection attempts by peer "
4214 MACSTR, MAC2STR(peer_addr));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004215 if (dev == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004216 p2p_dbg(p2p, "Peer " MACSTR " unknown", MAC2STR(peer_addr));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004217 return -1;
4218 }
4219 dev->status = P2P_SC_FAIL_REJECTED_BY_USER;
4220 dev->flags |= P2P_DEV_USER_REJECTED;
4221 return 0;
4222}
4223
4224
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004225const char * p2p_wps_method_text(enum p2p_wps_method method)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004226{
4227 switch (method) {
4228 case WPS_NOT_READY:
4229 return "not-ready";
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004230 case WPS_PIN_DISPLAY:
4231 return "Display";
4232 case WPS_PIN_KEYPAD:
4233 return "Keypad";
4234 case WPS_PBC:
4235 return "PBC";
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004236 case WPS_NFC:
4237 return "NFC";
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004238 case WPS_P2PS:
4239 return "P2PS";
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004240 }
4241
4242 return "??";
4243}
4244
4245
4246static const char * p2p_go_state_text(enum p2p_go_state go_state)
4247{
4248 switch (go_state) {
4249 case UNKNOWN_GO:
4250 return "unknown";
4251 case LOCAL_GO:
4252 return "local";
4253 case REMOTE_GO:
4254 return "remote";
4255 }
4256
4257 return "??";
4258}
4259
4260
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004261const struct p2p_peer_info * p2p_get_peer_info(struct p2p_data *p2p,
4262 const u8 *addr, int next)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004263{
4264 struct p2p_device *dev;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004265
4266 if (addr)
4267 dev = p2p_get_device(p2p, addr);
4268 else
4269 dev = dl_list_first(&p2p->devices, struct p2p_device, list);
4270
4271 if (dev && next) {
4272 dev = dl_list_first(&dev->list, struct p2p_device, list);
4273 if (&dev->list == &p2p->devices)
4274 dev = NULL;
4275 }
4276
4277 if (dev == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004278 return NULL;
4279
4280 return &dev->info;
4281}
4282
4283
4284int p2p_get_peer_info_txt(const struct p2p_peer_info *info,
4285 char *buf, size_t buflen)
4286{
4287 struct p2p_device *dev;
4288 int res;
4289 char *pos, *end;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004290 struct os_reltime now;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004291
4292 if (info == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004293 return -1;
4294
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004295 dev = (struct p2p_device *) (((u8 *) info) -
4296 offsetof(struct p2p_device, info));
4297
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004298 pos = buf;
4299 end = buf + buflen;
4300
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004301 os_get_reltime(&now);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004302 res = os_snprintf(pos, end - pos,
4303 "age=%d\n"
4304 "listen_freq=%d\n"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004305 "wps_method=%s\n"
4306 "interface_addr=" MACSTR "\n"
4307 "member_in_go_dev=" MACSTR "\n"
4308 "member_in_go_iface=" MACSTR "\n"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004309 "go_neg_req_sent=%d\n"
4310 "go_state=%s\n"
4311 "dialog_token=%u\n"
4312 "intended_addr=" MACSTR "\n"
4313 "country=%c%c\n"
4314 "oper_freq=%d\n"
4315 "req_config_methods=0x%x\n"
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07004316 "flags=%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004317 "status=%d\n"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004318 "invitation_reqs=%u\n",
4319 (int) (now.sec - dev->last_seen.sec),
4320 dev->listen_freq,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004321 p2p_wps_method_text(dev->wps_method),
4322 MAC2STR(dev->interface_addr),
4323 MAC2STR(dev->member_in_go_dev),
4324 MAC2STR(dev->member_in_go_iface),
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004325 dev->go_neg_req_sent,
4326 p2p_go_state_text(dev->go_state),
4327 dev->dialog_token,
4328 MAC2STR(dev->intended_addr),
4329 dev->country[0] ? dev->country[0] : '_',
4330 dev->country[1] ? dev->country[1] : '_',
4331 dev->oper_freq,
4332 dev->req_config_methods,
4333 dev->flags & P2P_DEV_PROBE_REQ_ONLY ?
4334 "[PROBE_REQ_ONLY]" : "",
4335 dev->flags & P2P_DEV_REPORTED ? "[REPORTED]" : "",
4336 dev->flags & P2P_DEV_NOT_YET_READY ?
4337 "[NOT_YET_READY]" : "",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004338 dev->flags & P2P_DEV_PD_PEER_DISPLAY ?
4339 "[PD_PEER_DISPLAY]" : "",
4340 dev->flags & P2P_DEV_PD_PEER_KEYPAD ?
4341 "[PD_PEER_KEYPAD]" : "",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004342 dev->flags & P2P_DEV_PD_PEER_P2PS ?
4343 "[PD_PEER_P2PS]" : "",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004344 dev->flags & P2P_DEV_USER_REJECTED ?
4345 "[USER_REJECTED]" : "",
4346 dev->flags & P2P_DEV_PEER_WAITING_RESPONSE ?
4347 "[PEER_WAITING_RESPONSE]" : "",
4348 dev->flags & P2P_DEV_PREFER_PERSISTENT_GROUP ?
4349 "[PREFER_PERSISTENT_GROUP]" : "",
4350 dev->flags & P2P_DEV_WAIT_GO_NEG_RESPONSE ?
4351 "[WAIT_GO_NEG_RESPONSE]" : "",
4352 dev->flags & P2P_DEV_WAIT_GO_NEG_CONFIRM ?
4353 "[WAIT_GO_NEG_CONFIRM]" : "",
4354 dev->flags & P2P_DEV_GROUP_CLIENT_ONLY ?
4355 "[GROUP_CLIENT_ONLY]" : "",
4356 dev->flags & P2P_DEV_FORCE_FREQ ?
4357 "[FORCE_FREQ]" : "",
4358 dev->flags & P2P_DEV_PD_FOR_JOIN ?
4359 "[PD_FOR_JOIN]" : "",
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07004360 dev->flags & P2P_DEV_LAST_SEEN_AS_GROUP_CLIENT ?
4361 "[LAST_SEEN_AS_GROUP_CLIENT]" : "",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004362 dev->status,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004363 dev->invitation_reqs);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004364 if (os_snprintf_error(end - pos, res))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004365 return pos - buf;
4366 pos += res;
4367
4368 if (dev->ext_listen_period) {
4369 res = os_snprintf(pos, end - pos,
4370 "ext_listen_period=%u\n"
4371 "ext_listen_interval=%u\n",
4372 dev->ext_listen_period,
4373 dev->ext_listen_interval);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004374 if (os_snprintf_error(end - pos, res))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004375 return pos - buf;
4376 pos += res;
4377 }
4378
4379 if (dev->oper_ssid_len) {
4380 res = os_snprintf(pos, end - pos,
4381 "oper_ssid=%s\n",
4382 wpa_ssid_txt(dev->oper_ssid,
4383 dev->oper_ssid_len));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004384 if (os_snprintf_error(end - pos, res))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004385 return pos - buf;
4386 pos += res;
4387 }
4388
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004389#ifdef CONFIG_WIFI_DISPLAY
4390 if (dev->info.wfd_subelems) {
4391 res = os_snprintf(pos, end - pos, "wfd_subelems=");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004392 if (os_snprintf_error(end - pos, res))
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004393 return pos - buf;
4394 pos += res;
4395
4396 pos += wpa_snprintf_hex(pos, end - pos,
4397 wpabuf_head(dev->info.wfd_subelems),
4398 wpabuf_len(dev->info.wfd_subelems));
4399
4400 res = os_snprintf(pos, end - pos, "\n");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004401 if (os_snprintf_error(end - pos, res))
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004402 return pos - buf;
4403 pos += res;
4404 }
4405#endif /* CONFIG_WIFI_DISPLAY */
4406
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004407 return pos - buf;
4408}
4409
4410
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004411int p2p_peer_known(struct p2p_data *p2p, const u8 *addr)
4412{
4413 return p2p_get_device(p2p, addr) != NULL;
4414}
4415
4416
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004417void p2p_set_client_discoverability(struct p2p_data *p2p, int enabled)
4418{
4419 if (enabled) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004420 p2p_dbg(p2p, "Client discoverability enabled");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004421 p2p->dev_capab |= P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
4422 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004423 p2p_dbg(p2p, "Client discoverability disabled");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004424 p2p->dev_capab &= ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
4425 }
4426}
4427
4428
4429static struct wpabuf * p2p_build_presence_req(u32 duration1, u32 interval1,
4430 u32 duration2, u32 interval2)
4431{
4432 struct wpabuf *req;
4433 struct p2p_noa_desc desc1, desc2, *ptr1 = NULL, *ptr2 = NULL;
4434 u8 *len;
4435
4436 req = wpabuf_alloc(100);
4437 if (req == NULL)
4438 return NULL;
4439
4440 if (duration1 || interval1) {
4441 os_memset(&desc1, 0, sizeof(desc1));
4442 desc1.count_type = 1;
4443 desc1.duration = duration1;
4444 desc1.interval = interval1;
4445 ptr1 = &desc1;
4446
4447 if (duration2 || interval2) {
4448 os_memset(&desc2, 0, sizeof(desc2));
4449 desc2.count_type = 2;
4450 desc2.duration = duration2;
4451 desc2.interval = interval2;
4452 ptr2 = &desc2;
4453 }
4454 }
4455
4456 p2p_buf_add_action_hdr(req, P2P_PRESENCE_REQ, 1);
4457 len = p2p_buf_add_ie_hdr(req);
4458 p2p_buf_add_noa(req, 0, 0, 0, ptr1, ptr2);
4459 p2p_buf_update_ie_hdr(req, len);
4460
4461 return req;
4462}
4463
4464
4465int p2p_presence_req(struct p2p_data *p2p, const u8 *go_interface_addr,
4466 const u8 *own_interface_addr, unsigned int freq,
4467 u32 duration1, u32 interval1, u32 duration2,
4468 u32 interval2)
4469{
4470 struct wpabuf *req;
4471
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004472 p2p_dbg(p2p, "Send Presence Request to GO " MACSTR
4473 " (own interface " MACSTR ") freq=%u dur1=%u int1=%u "
4474 "dur2=%u int2=%u",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004475 MAC2STR(go_interface_addr), MAC2STR(own_interface_addr),
4476 freq, duration1, interval1, duration2, interval2);
4477
4478 req = p2p_build_presence_req(duration1, interval1, duration2,
4479 interval2);
4480 if (req == NULL)
4481 return -1;
4482
4483 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
4484 if (p2p_send_action(p2p, freq, go_interface_addr, own_interface_addr,
4485 go_interface_addr,
4486 wpabuf_head(req), wpabuf_len(req), 200) < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004487 p2p_dbg(p2p, "Failed to send Action frame");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004488 }
4489 wpabuf_free(req);
4490
4491 return 0;
4492}
4493
4494
4495static struct wpabuf * p2p_build_presence_resp(u8 status, const u8 *noa,
4496 size_t noa_len, u8 dialog_token)
4497{
4498 struct wpabuf *resp;
4499 u8 *len;
4500
4501 resp = wpabuf_alloc(100 + noa_len);
4502 if (resp == NULL)
4503 return NULL;
4504
4505 p2p_buf_add_action_hdr(resp, P2P_PRESENCE_RESP, dialog_token);
4506 len = p2p_buf_add_ie_hdr(resp);
4507 p2p_buf_add_status(resp, status);
4508 if (noa) {
4509 wpabuf_put_u8(resp, P2P_ATTR_NOTICE_OF_ABSENCE);
4510 wpabuf_put_le16(resp, noa_len);
4511 wpabuf_put_data(resp, noa, noa_len);
4512 } else
4513 p2p_buf_add_noa(resp, 0, 0, 0, NULL, NULL);
4514 p2p_buf_update_ie_hdr(resp, len);
4515
4516 return resp;
4517}
4518
4519
4520static void p2p_process_presence_req(struct p2p_data *p2p, const u8 *da,
4521 const u8 *sa, const u8 *data, size_t len,
4522 int rx_freq)
4523{
4524 struct p2p_message msg;
4525 u8 status;
4526 struct wpabuf *resp;
4527 size_t g;
4528 struct p2p_group *group = NULL;
4529 int parsed = 0;
4530 u8 noa[50];
4531 int noa_len;
4532
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004533 p2p_dbg(p2p, "Received P2P Action - P2P Presence Request");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004534
4535 for (g = 0; g < p2p->num_groups; g++) {
4536 if (os_memcmp(da, p2p_group_get_interface_addr(p2p->groups[g]),
4537 ETH_ALEN) == 0) {
4538 group = p2p->groups[g];
4539 break;
4540 }
4541 }
4542 if (group == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004543 p2p_dbg(p2p, "Ignore P2P Presence Request for unknown group "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004544 MACSTR, MAC2STR(da));
4545 return;
4546 }
4547
4548 if (p2p_parse(data, len, &msg) < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004549 p2p_dbg(p2p, "Failed to parse P2P Presence Request");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004550 status = P2P_SC_FAIL_INVALID_PARAMS;
4551 goto fail;
4552 }
4553 parsed = 1;
4554
4555 if (msg.noa == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004556 p2p_dbg(p2p, "No NoA attribute in P2P Presence Request");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004557 status = P2P_SC_FAIL_INVALID_PARAMS;
4558 goto fail;
4559 }
4560
4561 status = p2p_group_presence_req(group, sa, msg.noa, msg.noa_len);
4562
4563fail:
4564 if (p2p->cfg->get_noa)
4565 noa_len = p2p->cfg->get_noa(p2p->cfg->cb_ctx, da, noa,
4566 sizeof(noa));
4567 else
4568 noa_len = -1;
4569 resp = p2p_build_presence_resp(status, noa_len > 0 ? noa : NULL,
4570 noa_len > 0 ? noa_len : 0,
4571 msg.dialog_token);
4572 if (parsed)
4573 p2p_parse_free(&msg);
4574 if (resp == NULL)
4575 return;
4576
4577 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
4578 if (p2p_send_action(p2p, rx_freq, sa, da, da,
4579 wpabuf_head(resp), wpabuf_len(resp), 200) < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004580 p2p_dbg(p2p, "Failed to send Action frame");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004581 }
4582 wpabuf_free(resp);
4583}
4584
4585
4586static void p2p_process_presence_resp(struct p2p_data *p2p, const u8 *da,
4587 const u8 *sa, const u8 *data, size_t len)
4588{
4589 struct p2p_message msg;
4590
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004591 p2p_dbg(p2p, "Received P2P Action - P2P Presence Response");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004592
4593 if (p2p_parse(data, len, &msg) < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004594 p2p_dbg(p2p, "Failed to parse P2P Presence Response");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004595 return;
4596 }
4597
4598 if (msg.status == NULL || msg.noa == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004599 p2p_dbg(p2p, "No Status or NoA attribute in P2P Presence Response");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004600 p2p_parse_free(&msg);
4601 return;
4602 }
4603
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004604 if (p2p->cfg->presence_resp) {
4605 p2p->cfg->presence_resp(p2p->cfg->cb_ctx, sa, *msg.status,
4606 msg.noa, msg.noa_len);
4607 }
4608
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004609 if (*msg.status) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004610 p2p_dbg(p2p, "P2P Presence Request was rejected: status %u",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004611 *msg.status);
4612 p2p_parse_free(&msg);
4613 return;
4614 }
4615
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004616 p2p_dbg(p2p, "P2P Presence Request was accepted");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004617 wpa_hexdump(MSG_DEBUG, "P2P: P2P Presence Response - NoA",
4618 msg.noa, msg.noa_len);
4619 /* TODO: process NoA */
4620 p2p_parse_free(&msg);
4621}
4622
4623
4624static void p2p_ext_listen_timeout(void *eloop_ctx, void *timeout_ctx)
4625{
4626 struct p2p_data *p2p = eloop_ctx;
4627
4628 if (p2p->ext_listen_interval) {
4629 /* Schedule next extended listen timeout */
4630 eloop_register_timeout(p2p->ext_listen_interval_sec,
4631 p2p->ext_listen_interval_usec,
4632 p2p_ext_listen_timeout, p2p, NULL);
4633 }
4634
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -07004635 if ((p2p->cfg->is_p2p_in_progress &&
4636 p2p->cfg->is_p2p_in_progress(p2p->cfg->cb_ctx)) ||
4637 (p2p->pending_action_state == P2P_PENDING_PD &&
4638 p2p->pd_retries > 0)) {
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -07004639 p2p_dbg(p2p, "Operation in progress - skip Extended Listen timeout (%s)",
4640 p2p_state_txt(p2p->state));
4641 return;
4642 }
4643
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004644 if (p2p->state == P2P_LISTEN_ONLY && p2p->ext_listen_only) {
4645 /*
4646 * This should not really happen, but it looks like the Listen
4647 * command may fail is something else (e.g., a scan) was
4648 * running at an inconvenient time. As a workaround, allow new
4649 * Extended Listen operation to be started.
4650 */
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004651 p2p_dbg(p2p, "Previous Extended Listen operation had not been completed - try again");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004652 p2p->ext_listen_only = 0;
4653 p2p_set_state(p2p, P2P_IDLE);
4654 }
4655
4656 if (p2p->state != P2P_IDLE) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004657 p2p_dbg(p2p, "Skip Extended Listen timeout in active state (%s)", p2p_state_txt(p2p->state));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004658 return;
4659 }
4660
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004661 p2p_dbg(p2p, "Extended Listen timeout");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004662 p2p->ext_listen_only = 1;
4663 if (p2p_listen(p2p, p2p->ext_listen_period) < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004664 p2p_dbg(p2p, "Failed to start Listen state for Extended Listen Timing");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004665 p2p->ext_listen_only = 0;
4666 }
4667}
4668
4669
4670int p2p_ext_listen(struct p2p_data *p2p, unsigned int period,
4671 unsigned int interval)
4672{
4673 if (period > 65535 || interval > 65535 || period > interval ||
4674 (period == 0 && interval > 0) || (period > 0 && interval == 0)) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004675 p2p_dbg(p2p, "Invalid Extended Listen Timing request: period=%u interval=%u",
4676 period, interval);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004677 return -1;
4678 }
4679
4680 eloop_cancel_timeout(p2p_ext_listen_timeout, p2p, NULL);
4681
4682 if (interval == 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004683 p2p_dbg(p2p, "Disabling Extended Listen Timing");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004684 p2p->ext_listen_period = 0;
4685 p2p->ext_listen_interval = 0;
4686 return 0;
4687 }
4688
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004689 p2p_dbg(p2p, "Enabling Extended Listen Timing: period %u msec, interval %u msec",
4690 period, interval);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004691 p2p->ext_listen_period = period;
4692 p2p->ext_listen_interval = interval;
4693 p2p->ext_listen_interval_sec = interval / 1000;
4694 p2p->ext_listen_interval_usec = (interval % 1000) * 1000;
4695
4696 eloop_register_timeout(p2p->ext_listen_interval_sec,
4697 p2p->ext_listen_interval_usec,
4698 p2p_ext_listen_timeout, p2p, NULL);
4699
4700 return 0;
4701}
4702
4703
4704void p2p_deauth_notif(struct p2p_data *p2p, const u8 *bssid, u16 reason_code,
4705 const u8 *ie, size_t ie_len)
4706{
4707 struct p2p_message msg;
4708
4709 if (bssid == NULL || ie == NULL)
4710 return;
4711
4712 os_memset(&msg, 0, sizeof(msg));
4713 if (p2p_parse_ies(ie, ie_len, &msg))
4714 return;
Dmitry Shmidt97672262014-02-03 13:02:54 -08004715 if (msg.minor_reason_code == NULL) {
4716 p2p_parse_free(&msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004717 return;
Dmitry Shmidt97672262014-02-03 13:02:54 -08004718 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004719
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004720 p2p_dbg(p2p, "Deauthentication notification BSSID " MACSTR
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004721 " reason_code=%u minor_reason_code=%u",
4722 MAC2STR(bssid), reason_code, *msg.minor_reason_code);
4723
4724 p2p_parse_free(&msg);
4725}
4726
4727
4728void p2p_disassoc_notif(struct p2p_data *p2p, const u8 *bssid, u16 reason_code,
4729 const u8 *ie, size_t ie_len)
4730{
4731 struct p2p_message msg;
4732
4733 if (bssid == NULL || ie == NULL)
4734 return;
4735
4736 os_memset(&msg, 0, sizeof(msg));
4737 if (p2p_parse_ies(ie, ie_len, &msg))
4738 return;
Dmitry Shmidt97672262014-02-03 13:02:54 -08004739 if (msg.minor_reason_code == NULL) {
4740 p2p_parse_free(&msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004741 return;
Dmitry Shmidt97672262014-02-03 13:02:54 -08004742 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004743
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004744 p2p_dbg(p2p, "Disassociation notification BSSID " MACSTR
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004745 " reason_code=%u minor_reason_code=%u",
4746 MAC2STR(bssid), reason_code, *msg.minor_reason_code);
4747
4748 p2p_parse_free(&msg);
4749}
4750
4751
4752void p2p_set_managed_oper(struct p2p_data *p2p, int enabled)
4753{
4754 if (enabled) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004755 p2p_dbg(p2p, "Managed P2P Device operations enabled");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004756 p2p->dev_capab |= P2P_DEV_CAPAB_INFRA_MANAGED;
4757 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004758 p2p_dbg(p2p, "Managed P2P Device operations disabled");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004759 p2p->dev_capab &= ~P2P_DEV_CAPAB_INFRA_MANAGED;
4760 }
4761}
4762
4763
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004764int p2p_config_get_random_social(struct p2p_config *p2p, u8 *op_class,
Hai Shalom74f70d42019-02-11 14:42:39 -08004765 u8 *op_channel,
4766 struct wpa_freq_range_list *avoid_list,
4767 struct wpa_freq_range_list *disallow_list)
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004768{
Hai Shalom74f70d42019-02-11 14:42:39 -08004769 return p2p_channel_random_social(&p2p->channels, op_class, op_channel,
4770 avoid_list, disallow_list);
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004771}
4772
4773
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004774int p2p_set_listen_channel(struct p2p_data *p2p, u8 reg_class, u8 channel,
4775 u8 forced)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004776{
Dmitry Shmidt4b060592013-04-29 16:42:49 -07004777 if (p2p_channel_to_freq(reg_class, channel) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004778 return -1;
4779
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004780 /*
4781 * Listen channel was set in configuration or set by control interface;
4782 * cannot override it.
4783 */
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004784 if (p2p->cfg->channel_forced && forced == 0) {
4785 p2p_dbg(p2p,
4786 "Listen channel was previously configured - do not override based on optimization");
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004787 return -1;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004788 }
4789
4790 p2p_dbg(p2p, "Set Listen channel: reg_class %u channel %u",
4791 reg_class, channel);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004792
4793 if (p2p->state == P2P_IDLE) {
4794 p2p->cfg->reg_class = reg_class;
4795 p2p->cfg->channel = channel;
4796 p2p->cfg->channel_forced = forced;
4797 } else {
4798 p2p_dbg(p2p, "Defer setting listen channel");
4799 p2p->pending_reg_class = reg_class;
4800 p2p->pending_channel = channel;
4801 p2p->pending_channel_forced = forced;
4802 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004803
4804 return 0;
4805}
4806
4807
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004808u8 p2p_get_listen_channel(struct p2p_data *p2p)
4809{
4810 return p2p->cfg->channel;
4811}
4812
4813
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004814int p2p_set_ssid_postfix(struct p2p_data *p2p, const u8 *postfix, size_t len)
4815{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004816 p2p_dbg(p2p, "New SSID postfix: %s", wpa_ssid_txt(postfix, len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004817 if (postfix == NULL) {
4818 p2p->cfg->ssid_postfix_len = 0;
4819 return 0;
4820 }
4821 if (len > sizeof(p2p->cfg->ssid_postfix))
4822 return -1;
4823 os_memcpy(p2p->cfg->ssid_postfix, postfix, len);
4824 p2p->cfg->ssid_postfix_len = len;
4825 return 0;
4826}
4827
4828
Jouni Malinen75ecf522011-06-27 15:19:46 -07004829int p2p_set_oper_channel(struct p2p_data *p2p, u8 op_reg_class, u8 op_channel,
4830 int cfg_op_channel)
4831{
Dmitry Shmidt4b060592013-04-29 16:42:49 -07004832 if (p2p_channel_to_freq(op_reg_class, op_channel) < 0)
Jouni Malinen75ecf522011-06-27 15:19:46 -07004833 return -1;
4834
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004835 p2p_dbg(p2p, "Set Operating channel: reg_class %u channel %u",
4836 op_reg_class, op_channel);
Jouni Malinen75ecf522011-06-27 15:19:46 -07004837 p2p->cfg->op_reg_class = op_reg_class;
4838 p2p->cfg->op_channel = op_channel;
4839 p2p->cfg->cfg_op_channel = cfg_op_channel;
4840 return 0;
4841}
4842
4843
Dmitry Shmidt04949592012-07-19 12:16:46 -07004844int p2p_set_pref_chan(struct p2p_data *p2p, unsigned int num_pref_chan,
4845 const struct p2p_channel *pref_chan)
4846{
4847 struct p2p_channel *n;
4848
4849 if (pref_chan) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004850 n = os_memdup(pref_chan,
4851 num_pref_chan * sizeof(struct p2p_channel));
Dmitry Shmidt04949592012-07-19 12:16:46 -07004852 if (n == NULL)
4853 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004854 } else
4855 n = NULL;
4856
4857 os_free(p2p->cfg->pref_chan);
4858 p2p->cfg->pref_chan = n;
4859 p2p->cfg->num_pref_chan = num_pref_chan;
4860
4861 return 0;
4862}
4863
4864
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004865int p2p_set_no_go_freq(struct p2p_data *p2p,
4866 const struct wpa_freq_range_list *list)
4867{
4868 struct wpa_freq_range *tmp;
4869
4870 if (list == NULL || list->num == 0) {
4871 os_free(p2p->no_go_freq.range);
4872 p2p->no_go_freq.range = NULL;
4873 p2p->no_go_freq.num = 0;
4874 return 0;
4875 }
4876
4877 tmp = os_calloc(list->num, sizeof(struct wpa_freq_range));
4878 if (tmp == NULL)
4879 return -1;
4880 os_memcpy(tmp, list->range, list->num * sizeof(struct wpa_freq_range));
4881 os_free(p2p->no_go_freq.range);
4882 p2p->no_go_freq.range = tmp;
4883 p2p->no_go_freq.num = list->num;
4884 p2p_dbg(p2p, "Updated no GO chan list");
4885
4886 return 0;
4887}
4888
4889
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004890int p2p_get_interface_addr(struct p2p_data *p2p, const u8 *dev_addr,
4891 u8 *iface_addr)
4892{
4893 struct p2p_device *dev = p2p_get_device(p2p, dev_addr);
4894 if (dev == NULL || is_zero_ether_addr(dev->interface_addr))
4895 return -1;
4896 os_memcpy(iface_addr, dev->interface_addr, ETH_ALEN);
4897 return 0;
4898}
4899
4900
4901int p2p_get_dev_addr(struct p2p_data *p2p, const u8 *iface_addr,
4902 u8 *dev_addr)
4903{
4904 struct p2p_device *dev = p2p_get_device_interface(p2p, iface_addr);
4905 if (dev == NULL)
4906 return -1;
4907 os_memcpy(dev_addr, dev->info.p2p_device_addr, ETH_ALEN);
4908 return 0;
4909}
4910
4911
4912void p2p_set_peer_filter(struct p2p_data *p2p, const u8 *addr)
4913{
4914 os_memcpy(p2p->peer_filter, addr, ETH_ALEN);
4915 if (is_zero_ether_addr(p2p->peer_filter))
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004916 p2p_dbg(p2p, "Disable peer filter");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004917 else
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004918 p2p_dbg(p2p, "Enable peer filter for " MACSTR,
4919 MAC2STR(p2p->peer_filter));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004920}
4921
4922
4923void p2p_set_cross_connect(struct p2p_data *p2p, int enabled)
4924{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004925 p2p_dbg(p2p, "Cross connection %s", enabled ? "enabled" : "disabled");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004926 if (p2p->cross_connect == enabled)
4927 return;
4928 p2p->cross_connect = enabled;
4929 /* TODO: may need to tear down any action group where we are GO(?) */
4930}
4931
4932
4933int p2p_get_oper_freq(struct p2p_data *p2p, const u8 *iface_addr)
4934{
4935 struct p2p_device *dev = p2p_get_device_interface(p2p, iface_addr);
4936 if (dev == NULL)
4937 return -1;
4938 if (dev->oper_freq <= 0)
4939 return -1;
4940 return dev->oper_freq;
4941}
4942
4943
4944void p2p_set_intra_bss_dist(struct p2p_data *p2p, int enabled)
4945{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004946 p2p_dbg(p2p, "Intra BSS distribution %s",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004947 enabled ? "enabled" : "disabled");
4948 p2p->cfg->p2p_intra_bss = enabled;
4949}
4950
4951
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004952void p2p_update_channel_list(struct p2p_data *p2p,
4953 const struct p2p_channels *chan,
4954 const struct p2p_channels *cli_chan)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004955{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004956 p2p_dbg(p2p, "Update channel list");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004957 os_memcpy(&p2p->cfg->channels, chan, sizeof(struct p2p_channels));
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004958 p2p_channels_dump(p2p, "channels", &p2p->cfg->channels);
4959 os_memcpy(&p2p->cfg->cli_channels, cli_chan,
4960 sizeof(struct p2p_channels));
4961 p2p_channels_dump(p2p, "cli_channels", &p2p->cfg->cli_channels);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004962}
4963
4964
4965int p2p_send_action(struct p2p_data *p2p, unsigned int freq, const u8 *dst,
4966 const u8 *src, const u8 *bssid, const u8 *buf,
4967 size_t len, unsigned int wait_time)
4968{
Hai Shalom021b0b52019-04-10 11:17:58 -07004969 int res, scheduled;
4970
Hai Shalom021b0b52019-04-10 11:17:58 -07004971 res = p2p->cfg->send_action(p2p->cfg->cb_ctx, freq, dst, src, bssid,
4972 buf, len, wait_time, &scheduled);
4973 if (res == 0 && scheduled && p2p->in_listen && freq > 0 &&
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08004974 p2p->drv_in_listen > 0 &&
Hai Shalom021b0b52019-04-10 11:17:58 -07004975 (unsigned int) p2p->drv_in_listen != freq) {
4976 p2p_dbg(p2p,
4977 "Stop listen on %d MHz to allow a frame to be sent immediately on %d MHz",
4978 p2p->drv_in_listen, freq);
4979 p2p_stop_listen_for_freq(p2p, freq);
4980 }
4981 return res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004982}
4983
4984
4985void p2p_set_best_channels(struct p2p_data *p2p, int freq_24, int freq_5,
4986 int freq_overall)
4987{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004988 p2p_dbg(p2p, "Best channel: 2.4 GHz: %d, 5 GHz: %d, overall: %d",
4989 freq_24, freq_5, freq_overall);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004990 p2p->best_freq_24 = freq_24;
4991 p2p->best_freq_5 = freq_5;
4992 p2p->best_freq_overall = freq_overall;
4993}
4994
4995
Dmitry Shmidt700a1372013-03-15 14:14:44 -07004996void p2p_set_own_freq_preference(struct p2p_data *p2p, int freq)
4997{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004998 p2p_dbg(p2p, "Own frequency preference: %d MHz", freq);
Dmitry Shmidt700a1372013-03-15 14:14:44 -07004999 p2p->own_freq_preference = freq;
5000}
5001
5002
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005003const u8 * p2p_get_go_neg_peer(struct p2p_data *p2p)
5004{
5005 if (p2p == NULL || p2p->go_neg_peer == NULL)
5006 return NULL;
5007 return p2p->go_neg_peer->info.p2p_device_addr;
5008}
5009
5010
5011const struct p2p_peer_info *
5012p2p_get_peer_found(struct p2p_data *p2p, const u8 *addr, int next)
5013{
5014 struct p2p_device *dev;
5015
5016 if (addr) {
5017 dev = p2p_get_device(p2p, addr);
5018 if (!dev)
5019 return NULL;
5020
5021 if (!next) {
5022 if (dev->flags & P2P_DEV_PROBE_REQ_ONLY)
5023 return NULL;
5024
5025 return &dev->info;
5026 } else {
5027 do {
5028 dev = dl_list_first(&dev->list,
5029 struct p2p_device,
5030 list);
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07005031 if (!dev || &dev->list == &p2p->devices)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005032 return NULL;
5033 } while (dev->flags & P2P_DEV_PROBE_REQ_ONLY);
5034 }
5035 } else {
5036 dev = dl_list_first(&p2p->devices, struct p2p_device, list);
5037 if (!dev)
5038 return NULL;
5039 while (dev->flags & P2P_DEV_PROBE_REQ_ONLY) {
5040 dev = dl_list_first(&dev->list,
5041 struct p2p_device,
5042 list);
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07005043 if (!dev || &dev->list == &p2p->devices)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005044 return NULL;
5045 }
5046 }
5047
5048 return &dev->info;
5049}
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005050
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005051
5052int p2p_in_progress(struct p2p_data *p2p)
5053{
5054 if (p2p == NULL)
5055 return 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005056 if (p2p->state == P2P_SEARCH)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005057 return 2;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005058 return p2p->state != P2P_IDLE && p2p->state != P2P_PROVISIONING;
5059}
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005060
5061
5062void p2p_set_config_timeout(struct p2p_data *p2p, u8 go_timeout,
5063 u8 client_timeout)
5064{
5065 if (p2p) {
5066 p2p->go_timeout = go_timeout;
5067 p2p->client_timeout = client_timeout;
5068 }
5069}
5070
5071
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005072#ifdef CONFIG_WIFI_DISPLAY
5073
5074static void p2p_update_wfd_ie_groups(struct p2p_data *p2p)
5075{
5076 size_t g;
5077 struct p2p_group *group;
5078
5079 for (g = 0; g < p2p->num_groups; g++) {
5080 group = p2p->groups[g];
Dmitry Shmidtb96dad42013-11-05 10:07:29 -08005081 p2p_group_force_beacon_update_ies(group);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005082 }
5083}
5084
5085
5086int p2p_set_wfd_ie_beacon(struct p2p_data *p2p, struct wpabuf *ie)
5087{
5088 wpabuf_free(p2p->wfd_ie_beacon);
5089 p2p->wfd_ie_beacon = ie;
5090 p2p_update_wfd_ie_groups(p2p);
5091 return 0;
5092}
5093
5094
5095int p2p_set_wfd_ie_probe_req(struct p2p_data *p2p, struct wpabuf *ie)
5096{
5097 wpabuf_free(p2p->wfd_ie_probe_req);
5098 p2p->wfd_ie_probe_req = ie;
5099 return 0;
5100}
5101
5102
5103int p2p_set_wfd_ie_probe_resp(struct p2p_data *p2p, struct wpabuf *ie)
5104{
5105 wpabuf_free(p2p->wfd_ie_probe_resp);
5106 p2p->wfd_ie_probe_resp = ie;
5107 p2p_update_wfd_ie_groups(p2p);
5108 return 0;
5109}
5110
5111
5112int p2p_set_wfd_ie_assoc_req(struct p2p_data *p2p, struct wpabuf *ie)
5113{
5114 wpabuf_free(p2p->wfd_ie_assoc_req);
5115 p2p->wfd_ie_assoc_req = ie;
5116 return 0;
5117}
5118
5119
5120int p2p_set_wfd_ie_invitation(struct p2p_data *p2p, struct wpabuf *ie)
5121{
5122 wpabuf_free(p2p->wfd_ie_invitation);
5123 p2p->wfd_ie_invitation = ie;
5124 return 0;
5125}
5126
5127
5128int p2p_set_wfd_ie_prov_disc_req(struct p2p_data *p2p, struct wpabuf *ie)
5129{
5130 wpabuf_free(p2p->wfd_ie_prov_disc_req);
5131 p2p->wfd_ie_prov_disc_req = ie;
5132 return 0;
5133}
5134
5135
5136int p2p_set_wfd_ie_prov_disc_resp(struct p2p_data *p2p, struct wpabuf *ie)
5137{
5138 wpabuf_free(p2p->wfd_ie_prov_disc_resp);
5139 p2p->wfd_ie_prov_disc_resp = ie;
5140 return 0;
5141}
5142
5143
5144int p2p_set_wfd_ie_go_neg(struct p2p_data *p2p, struct wpabuf *ie)
5145{
5146 wpabuf_free(p2p->wfd_ie_go_neg);
5147 p2p->wfd_ie_go_neg = ie;
5148 return 0;
5149}
5150
5151
5152int p2p_set_wfd_dev_info(struct p2p_data *p2p, const struct wpabuf *elem)
5153{
5154 wpabuf_free(p2p->wfd_dev_info);
5155 if (elem) {
5156 p2p->wfd_dev_info = wpabuf_dup(elem);
5157 if (p2p->wfd_dev_info == NULL)
5158 return -1;
5159 } else
5160 p2p->wfd_dev_info = NULL;
5161
5162 return 0;
5163}
5164
5165
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005166int p2p_set_wfd_r2_dev_info(struct p2p_data *p2p, const struct wpabuf *elem)
5167{
5168 wpabuf_free(p2p->wfd_r2_dev_info);
5169 if (elem) {
5170 p2p->wfd_r2_dev_info = wpabuf_dup(elem);
5171 if (p2p->wfd_r2_dev_info == NULL)
5172 return -1;
5173 } else
5174 p2p->wfd_r2_dev_info = NULL;
5175
5176 return 0;
5177}
5178
5179
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005180int p2p_set_wfd_assoc_bssid(struct p2p_data *p2p, const struct wpabuf *elem)
5181{
5182 wpabuf_free(p2p->wfd_assoc_bssid);
5183 if (elem) {
5184 p2p->wfd_assoc_bssid = wpabuf_dup(elem);
5185 if (p2p->wfd_assoc_bssid == NULL)
5186 return -1;
5187 } else
5188 p2p->wfd_assoc_bssid = NULL;
5189
5190 return 0;
5191}
5192
5193
5194int p2p_set_wfd_coupled_sink_info(struct p2p_data *p2p,
5195 const struct wpabuf *elem)
5196{
5197 wpabuf_free(p2p->wfd_coupled_sink_info);
5198 if (elem) {
5199 p2p->wfd_coupled_sink_info = wpabuf_dup(elem);
5200 if (p2p->wfd_coupled_sink_info == NULL)
5201 return -1;
5202 } else
5203 p2p->wfd_coupled_sink_info = NULL;
5204
5205 return 0;
5206}
5207
5208#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005209
5210
5211int p2p_set_disc_int(struct p2p_data *p2p, int min_disc_int, int max_disc_int,
5212 int max_disc_tu)
5213{
5214 if (min_disc_int > max_disc_int || min_disc_int < 0 || max_disc_int < 0)
5215 return -1;
5216
5217 p2p->min_disc_int = min_disc_int;
5218 p2p->max_disc_int = max_disc_int;
5219 p2p->max_disc_tu = max_disc_tu;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005220 p2p_dbg(p2p, "Set discoverable interval: min=%d max=%d max_tu=%d",
5221 min_disc_int, max_disc_int, max_disc_tu);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005222
5223 return 0;
5224}
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005225
5226
5227void p2p_dbg(struct p2p_data *p2p, const char *fmt, ...)
5228{
5229 va_list ap;
5230 char buf[500];
5231
5232 if (!p2p->cfg->debug_print)
5233 return;
5234
5235 va_start(ap, fmt);
5236 vsnprintf(buf, sizeof(buf), fmt, ap);
5237 buf[sizeof(buf) - 1] = '\0';
5238 va_end(ap);
5239 p2p->cfg->debug_print(p2p->cfg->cb_ctx, MSG_DEBUG, buf);
5240}
5241
5242
5243void p2p_info(struct p2p_data *p2p, const char *fmt, ...)
5244{
5245 va_list ap;
5246 char buf[500];
5247
5248 if (!p2p->cfg->debug_print)
5249 return;
5250
5251 va_start(ap, fmt);
5252 vsnprintf(buf, sizeof(buf), fmt, ap);
5253 buf[sizeof(buf) - 1] = '\0';
5254 va_end(ap);
5255 p2p->cfg->debug_print(p2p->cfg->cb_ctx, MSG_INFO, buf);
5256}
5257
5258
5259void p2p_err(struct p2p_data *p2p, const char *fmt, ...)
5260{
5261 va_list ap;
5262 char buf[500];
5263
5264 if (!p2p->cfg->debug_print)
5265 return;
5266
5267 va_start(ap, fmt);
5268 vsnprintf(buf, sizeof(buf), fmt, ap);
5269 buf[sizeof(buf) - 1] = '\0';
5270 va_end(ap);
5271 p2p->cfg->debug_print(p2p->cfg->cb_ctx, MSG_ERROR, buf);
5272}
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005273
5274
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07005275void p2p_loop_on_known_peers(struct p2p_data *p2p,
5276 void (*peer_callback)(struct p2p_peer_info *peer,
5277 void *user_data),
5278 void *user_data)
5279{
5280 struct p2p_device *dev, *n;
5281
5282 dl_list_for_each_safe(dev, n, &p2p->devices, struct p2p_device, list) {
5283 peer_callback(&dev->info, user_data);
5284 }
5285}
5286
5287
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005288#ifdef CONFIG_WPS_NFC
5289
5290static struct wpabuf * p2p_build_nfc_handover(struct p2p_data *p2p,
5291 int client_freq,
5292 const u8 *go_dev_addr,
5293 const u8 *ssid, size_t ssid_len)
5294{
5295 struct wpabuf *buf;
5296 u8 op_class, channel;
5297 enum p2p_role_indication role = P2P_DEVICE_NOT_IN_GROUP;
5298
5299 buf = wpabuf_alloc(1000);
5300 if (buf == NULL)
5301 return NULL;
5302
5303 op_class = p2p->cfg->reg_class;
5304 channel = p2p->cfg->channel;
5305
5306 p2p_buf_add_capability(buf, p2p->dev_capab &
5307 ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY, 0);
5308 p2p_buf_add_device_info(buf, p2p, NULL);
5309
5310 if (p2p->num_groups > 0) {
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07005311 int freq = p2p_group_get_freq(p2p->groups[0]);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005312 role = P2P_GO_IN_A_GROUP;
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07005313 if (p2p_freq_to_channel(freq, &op_class, &channel) < 0) {
5314 p2p_dbg(p2p,
5315 "Unknown GO operating frequency %d MHz for NFC handover",
5316 freq);
5317 wpabuf_free(buf);
5318 return NULL;
5319 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005320 } else if (client_freq > 0) {
5321 role = P2P_CLIENT_IN_A_GROUP;
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07005322 if (p2p_freq_to_channel(client_freq, &op_class, &channel) < 0) {
5323 p2p_dbg(p2p,
5324 "Unknown client operating frequency %d MHz for NFC handover",
5325 client_freq);
5326 wpabuf_free(buf);
5327 return NULL;
5328 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005329 }
5330
5331 p2p_buf_add_oob_go_neg_channel(buf, p2p->cfg->country, op_class,
5332 channel, role);
5333
5334 if (p2p->num_groups > 0) {
5335 /* Limit number of clients to avoid very long message */
5336 p2p_buf_add_group_info(p2p->groups[0], buf, 5);
5337 p2p_group_buf_add_id(p2p->groups[0], buf);
5338 } else if (client_freq > 0 &&
5339 go_dev_addr && !is_zero_ether_addr(go_dev_addr) &&
5340 ssid && ssid_len > 0) {
5341 /*
5342 * Add the optional P2P Group ID to indicate in which group this
5343 * device is a P2P Client.
5344 */
5345 p2p_buf_add_group_id(buf, go_dev_addr, ssid, ssid_len);
5346 }
5347
5348 return buf;
5349}
5350
5351
5352struct wpabuf * p2p_build_nfc_handover_req(struct p2p_data *p2p,
5353 int client_freq,
5354 const u8 *go_dev_addr,
5355 const u8 *ssid, size_t ssid_len)
5356{
5357 return p2p_build_nfc_handover(p2p, client_freq, go_dev_addr, ssid,
5358 ssid_len);
5359}
5360
5361
5362struct wpabuf * p2p_build_nfc_handover_sel(struct p2p_data *p2p,
5363 int client_freq,
5364 const u8 *go_dev_addr,
5365 const u8 *ssid, size_t ssid_len)
5366{
5367 return p2p_build_nfc_handover(p2p, client_freq, go_dev_addr, ssid,
5368 ssid_len);
5369}
5370
5371
5372int p2p_process_nfc_connection_handover(struct p2p_data *p2p,
5373 struct p2p_nfc_params *params)
5374{
5375 struct p2p_message msg;
5376 struct p2p_device *dev;
5377 const u8 *p2p_dev_addr;
5378 int freq;
5379 enum p2p_role_indication role;
5380
5381 params->next_step = NO_ACTION;
5382
5383 if (p2p_parse_ies_separate(params->wsc_attr, params->wsc_len,
5384 params->p2p_attr, params->p2p_len, &msg)) {
5385 p2p_dbg(p2p, "Failed to parse WSC/P2P attributes from NFC");
5386 p2p_parse_free(&msg);
5387 return -1;
5388 }
5389
5390 if (msg.p2p_device_addr)
5391 p2p_dev_addr = msg.p2p_device_addr;
5392 else if (msg.device_id)
5393 p2p_dev_addr = msg.device_id;
5394 else {
5395 p2p_dbg(p2p, "Ignore scan data without P2P Device Info or P2P Device Id");
5396 p2p_parse_free(&msg);
5397 return -1;
5398 }
5399
5400 if (msg.oob_dev_password) {
5401 os_memcpy(params->oob_dev_pw, msg.oob_dev_password,
5402 msg.oob_dev_password_len);
5403 params->oob_dev_pw_len = msg.oob_dev_password_len;
5404 }
5405
5406 dev = p2p_create_device(p2p, p2p_dev_addr);
5407 if (dev == NULL) {
5408 p2p_parse_free(&msg);
5409 return -1;
5410 }
5411
5412 params->peer = &dev->info;
5413
5414 os_get_reltime(&dev->last_seen);
5415 dev->flags &= ~(P2P_DEV_PROBE_REQ_ONLY | P2P_DEV_GROUP_CLIENT_ONLY);
5416 p2p_copy_wps_info(p2p, dev, 0, &msg);
5417
5418 if (!msg.oob_go_neg_channel) {
5419 p2p_dbg(p2p, "OOB GO Negotiation Channel attribute not included");
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07005420 p2p_parse_free(&msg);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005421 return -1;
5422 }
5423
5424 if (msg.oob_go_neg_channel[3] == 0 &&
5425 msg.oob_go_neg_channel[4] == 0)
5426 freq = 0;
5427 else
5428 freq = p2p_channel_to_freq(msg.oob_go_neg_channel[3],
5429 msg.oob_go_neg_channel[4]);
5430 if (freq < 0) {
5431 p2p_dbg(p2p, "Unknown peer OOB GO Neg channel");
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07005432 p2p_parse_free(&msg);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005433 return -1;
5434 }
5435 role = msg.oob_go_neg_channel[5];
5436
5437 if (role == P2P_GO_IN_A_GROUP) {
5438 p2p_dbg(p2p, "Peer OOB GO operating channel: %u MHz", freq);
5439 params->go_freq = freq;
5440 } else if (role == P2P_CLIENT_IN_A_GROUP) {
5441 p2p_dbg(p2p, "Peer (client) OOB GO operating channel: %u MHz",
5442 freq);
5443 params->go_freq = freq;
5444 } else
5445 p2p_dbg(p2p, "Peer OOB GO Neg channel: %u MHz", freq);
5446 dev->oob_go_neg_freq = freq;
5447
5448 if (!params->sel && role != P2P_GO_IN_A_GROUP) {
5449 freq = p2p_channel_to_freq(p2p->cfg->reg_class,
5450 p2p->cfg->channel);
5451 if (freq < 0) {
5452 p2p_dbg(p2p, "Own listen channel not known");
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07005453 p2p_parse_free(&msg);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005454 return -1;
5455 }
5456 p2p_dbg(p2p, "Use own Listen channel as OOB GO Neg channel: %u MHz", freq);
5457 dev->oob_go_neg_freq = freq;
5458 }
5459
5460 if (msg.group_id) {
5461 os_memcpy(params->go_dev_addr, msg.group_id, ETH_ALEN);
5462 params->go_ssid_len = msg.group_id_len - ETH_ALEN;
5463 os_memcpy(params->go_ssid, msg.group_id + ETH_ALEN,
5464 params->go_ssid_len);
5465 }
5466
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005467 if (dev->flags & P2P_DEV_USER_REJECTED) {
5468 p2p_dbg(p2p, "Do not report rejected device");
Dmitry Shmidt71757432014-06-02 13:50:35 -07005469 p2p_parse_free(&msg);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005470 return 0;
5471 }
5472
5473 if (!(dev->flags & P2P_DEV_REPORTED)) {
5474 p2p->cfg->dev_found(p2p->cfg->cb_ctx, p2p_dev_addr, &dev->info,
5475 !(dev->flags & P2P_DEV_REPORTED_ONCE));
5476 dev->flags |= P2P_DEV_REPORTED | P2P_DEV_REPORTED_ONCE;
5477 }
Dmitry Shmidt71757432014-06-02 13:50:35 -07005478 p2p_parse_free(&msg);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005479
5480 if (role == P2P_GO_IN_A_GROUP && p2p->num_groups > 0)
5481 params->next_step = BOTH_GO;
5482 else if (role == P2P_GO_IN_A_GROUP)
5483 params->next_step = JOIN_GROUP;
5484 else if (role == P2P_CLIENT_IN_A_GROUP) {
5485 dev->flags |= P2P_DEV_GROUP_CLIENT_ONLY;
5486 params->next_step = PEER_CLIENT;
5487 } else if (p2p->num_groups > 0)
5488 params->next_step = AUTH_JOIN;
5489 else if (params->sel)
5490 params->next_step = INIT_GO_NEG;
5491 else
5492 params->next_step = RESP_GO_NEG;
5493
5494 return 0;
5495}
5496
5497
5498void p2p_set_authorized_oob_dev_pw_id(struct p2p_data *p2p, u16 dev_pw_id,
5499 int go_intent,
5500 const u8 *own_interface_addr)
5501{
5502
5503 p2p->authorized_oob_dev_pw_id = dev_pw_id;
5504 if (dev_pw_id == 0) {
5505 p2p_dbg(p2p, "NFC OOB Password unauthorized for static handover");
5506 return;
5507 }
5508
5509 p2p_dbg(p2p, "NFC OOB Password (id=%u) authorized for static handover",
5510 dev_pw_id);
5511
5512 p2p->go_intent = go_intent;
5513 os_memcpy(p2p->intended_addr, own_interface_addr, ETH_ALEN);
5514}
5515
5516#endif /* CONFIG_WPS_NFC */
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07005517
5518
5519int p2p_set_passphrase_len(struct p2p_data *p2p, unsigned int len)
5520{
5521 if (len < 8 || len > 63)
5522 return -1;
5523 p2p->cfg->passphrase_len = len;
5524 return 0;
5525}
Dmitry Shmidt2e67f062014-07-16 09:55:28 -07005526
5527
5528void p2p_set_vendor_elems(struct p2p_data *p2p, struct wpabuf **vendor_elem)
5529{
5530 p2p->vendor_elem = vendor_elem;
5531}
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005532
5533
5534void p2p_go_neg_wait_timeout(void *eloop_ctx, void *timeout_ctx)
5535{
5536 struct p2p_data *p2p = eloop_ctx;
5537
5538 p2p_dbg(p2p,
5539 "Timeout on waiting peer to become ready for GO Negotiation");
5540 p2p_go_neg_failed(p2p, -1);
5541}
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005542
5543
5544void p2p_set_own_pref_freq_list(struct p2p_data *p2p,
Sunil8cd6f4d2022-06-28 18:40:46 +00005545 const struct weighted_pcl *pref_freq_list,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005546 unsigned int size)
5547{
5548 unsigned int i;
5549
5550 if (size > P2P_MAX_PREF_CHANNELS)
5551 size = P2P_MAX_PREF_CHANNELS;
5552 p2p->num_pref_freq = size;
Sunil8cd6f4d2022-06-28 18:40:46 +00005553 os_memcpy(p2p->pref_freq_list, pref_freq_list,
5554 size * sizeof(struct weighted_pcl));
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005555 for (i = 0; i < size; i++) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005556 p2p_dbg(p2p, "Own preferred frequency list[%u]=%u MHz",
Sunil8cd6f4d2022-06-28 18:40:46 +00005557 i, p2p->pref_freq_list[i].freq);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005558 }
5559}
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07005560
5561
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08005562void p2p_set_override_pref_op_chan(struct p2p_data *p2p, u8 op_class,
5563 u8 chan)
5564{
5565 p2p->override_pref_op_class = op_class;
5566 p2p->override_pref_channel = chan;
5567}
5568
5569
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07005570struct wpabuf * p2p_build_probe_resp_template(struct p2p_data *p2p,
5571 unsigned int freq)
5572{
5573 struct wpabuf *ies, *buf;
5574 u8 addr[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
5575 int ret;
5576
5577 ies = p2p_build_probe_resp_ies(p2p, NULL, 0);
5578 if (!ies) {
5579 wpa_printf(MSG_ERROR,
5580 "CTRL: Failed to build Probe Response IEs");
5581 return NULL;
5582 }
5583
5584 buf = wpabuf_alloc(200 + wpabuf_len(ies));
5585 if (!buf) {
5586 wpabuf_free(ies);
5587 return NULL;
5588 }
5589
5590 ret = p2p_build_probe_resp_buf(p2p, buf, ies, addr, freq);
5591 wpabuf_free(ies);
5592 if (ret) {
5593 wpabuf_free(buf);
5594 return NULL;
5595 }
5596
5597 return buf;
5598}
Hai Shaloma20dcd72022-02-04 13:43:00 -08005599
5600
5601bool p2p_is_peer_6ghz_capab(struct p2p_data *p2p, const u8 *addr)
5602{
5603 struct p2p_device *dev;
5604
5605 dev = p2p_get_device(p2p, addr);
5606 if (!dev)
5607 return false;
5608
5609 return !!(dev->info.dev_capab & P2P_DEV_CAPAB_6GHZ_BAND_CAPABLE);
5610}
5611
5612
5613void p2p_set_6ghz_dev_capab(struct p2p_data *p2p, bool allow_6ghz)
5614{
5615 p2p->p2p_6ghz_capable = allow_6ghz;
5616 p2p->allow_6ghz = allow_6ghz;
5617 p2p_dbg(p2p, "Set 6 GHz capability to %d", allow_6ghz);
5618
5619 if (allow_6ghz)
5620 p2p->dev_capab |= P2P_DEV_CAPAB_6GHZ_BAND_CAPABLE;
5621 else
5622 p2p->dev_capab &= ~P2P_DEV_CAPAB_6GHZ_BAND_CAPABLE;
5623}
5624
5625
5626bool is_p2p_6ghz_capable(struct p2p_data *p2p)
5627{
5628 return p2p->p2p_6ghz_capable;
5629}
5630
5631
5632bool p2p_wfd_enabled(struct p2p_data *p2p)
5633{
5634#ifdef CONFIG_WIFI_DISPLAY
5635 return p2p->wfd_ie_probe_req != NULL;
5636#else /* CONFIG_WIFI_DISPLAY */
5637 return false;
5638#endif /* CONFIG_WIFI_DISPLAY */
5639}
5640
5641
5642bool p2p_peer_wfd_enabled(struct p2p_data *p2p, const u8 *peer_addr)
5643{
5644#ifdef CONFIG_WIFI_DISPLAY
5645 struct p2p_device *dev;
5646
5647 dev = p2p_get_device(p2p, peer_addr);
5648 return dev && dev->info.wfd_subelems != NULL;
5649#else /* CONFIG_WIFI_DISPLAY */
5650 return false;
5651#endif /* CONFIG_WIFI_DISPLAY */
5652}
5653
5654
5655bool is_p2p_allow_6ghz(struct p2p_data *p2p)
5656{
5657 return p2p->allow_6ghz;
5658}
5659
5660
5661void set_p2p_allow_6ghz(struct p2p_data *p2p, bool value)
5662{
5663 p2p->allow_6ghz = value;
5664}