blob: 8b443d6ee5b1810cedc7ccadc99f06bcc8bb199c [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) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700432 p2p_dbg(p2p, "Remove oldest peer entry to make room for a new peer");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700433 dl_list_del(&oldest->list);
434 p2p_device_free(p2p, oldest);
435 }
436
437 dev = os_zalloc(sizeof(*dev));
438 if (dev == NULL)
439 return NULL;
440 dl_list_add(&p2p->devices, &dev->list);
441 os_memcpy(dev->info.p2p_device_addr, addr, ETH_ALEN);
442
443 return dev;
444}
445
446
447static void p2p_copy_client_info(struct p2p_device *dev,
448 struct p2p_client_info *cli)
449{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800450 p2p_copy_filter_devname(dev->info.device_name,
451 sizeof(dev->info.device_name),
452 cli->dev_name, cli->dev_name_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700453 dev->info.dev_capab = cli->dev_capab;
454 dev->info.config_methods = cli->config_methods;
455 os_memcpy(dev->info.pri_dev_type, cli->pri_dev_type, 8);
456 dev->info.wps_sec_dev_type_list_len = 8 * cli->num_sec_dev_types;
Jimmy Chen1b737ee2020-11-20 01:24:12 +0800457 if (dev->info.wps_sec_dev_type_list_len > WPS_SEC_DEV_TYPE_MAX_LEN) {
458 android_errorWriteLog(0x534e4554, "172937525");
Jimmy Chene12b6972020-11-09 11:43:12 +0200459 dev->info.wps_sec_dev_type_list_len = WPS_SEC_DEV_TYPE_MAX_LEN;
Jimmy Chen1b737ee2020-11-20 01:24:12 +0800460 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700461 os_memcpy(dev->info.wps_sec_dev_type_list, cli->sec_dev_types,
462 dev->info.wps_sec_dev_type_list_len);
463}
464
465
466static int p2p_add_group_clients(struct p2p_data *p2p, const u8 *go_dev_addr,
467 const u8 *go_interface_addr, int freq,
Dmitry Shmidta3dc3092015-06-23 11:21:28 -0700468 const u8 *gi, size_t gi_len,
469 struct os_reltime *rx_time)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700470{
471 struct p2p_group_info info;
472 size_t c;
473 struct p2p_device *dev;
474
475 if (gi == NULL)
476 return 0;
477
478 if (p2p_group_info_parse(gi, gi_len, &info) < 0)
479 return -1;
480
481 /*
482 * Clear old data for this group; if the devices are still in the
483 * group, the information will be restored in the loop following this.
484 */
485 dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800486 if (os_memcmp(dev->member_in_go_iface, go_interface_addr,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700487 ETH_ALEN) == 0) {
488 os_memset(dev->member_in_go_iface, 0, ETH_ALEN);
489 os_memset(dev->member_in_go_dev, 0, ETH_ALEN);
490 }
491 }
492
493 for (c = 0; c < info.num_clients; c++) {
494 struct p2p_client_info *cli = &info.client[c];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800495 if (os_memcmp(cli->p2p_device_addr, p2p->cfg->dev_addr,
496 ETH_ALEN) == 0)
497 continue; /* ignore our own entry */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700498 dev = p2p_get_device(p2p, cli->p2p_device_addr);
499 if (dev) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700500 if (dev->flags & (P2P_DEV_GROUP_CLIENT_ONLY |
Dmitry Shmidt04949592012-07-19 12:16:46 -0700501 P2P_DEV_PROBE_REQ_ONLY)) {
502 /*
503 * Update information since we have not
504 * received this directly from the client.
505 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700506 p2p_copy_client_info(dev, cli);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700507 } else {
508 /*
509 * Need to update P2P Client Discoverability
510 * flag since it is valid only in P2P Group
511 * Info attribute.
512 */
513 dev->info.dev_capab &=
514 ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
515 dev->info.dev_capab |=
516 cli->dev_capab &
517 P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
518 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700519 if (dev->flags & P2P_DEV_PROBE_REQ_ONLY) {
520 dev->flags &= ~P2P_DEV_PROBE_REQ_ONLY;
521 }
522 } else {
523 dev = p2p_create_device(p2p, cli->p2p_device_addr);
524 if (dev == NULL)
525 continue;
526 dev->flags |= P2P_DEV_GROUP_CLIENT_ONLY;
527 p2p_copy_client_info(dev, cli);
528 dev->oper_freq = freq;
529 p2p->cfg->dev_found(p2p->cfg->cb_ctx,
530 dev->info.p2p_device_addr,
531 &dev->info, 1);
532 dev->flags |= P2P_DEV_REPORTED | P2P_DEV_REPORTED_ONCE;
533 }
534
535 os_memcpy(dev->interface_addr, cli->p2p_interface_addr,
536 ETH_ALEN);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -0700537 os_memcpy(&dev->last_seen, rx_time, sizeof(struct os_reltime));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700538 os_memcpy(dev->member_in_go_dev, go_dev_addr, ETH_ALEN);
539 os_memcpy(dev->member_in_go_iface, go_interface_addr,
540 ETH_ALEN);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -0700541 dev->flags |= P2P_DEV_LAST_SEEN_AS_GROUP_CLIENT;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700542 }
543
544 return 0;
545}
546
547
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700548static void p2p_copy_wps_info(struct p2p_data *p2p, struct p2p_device *dev,
549 int probe_req, const struct p2p_message *msg)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700550{
551 os_memcpy(dev->info.device_name, msg->device_name,
552 sizeof(dev->info.device_name));
553
554 if (msg->manufacturer &&
555 msg->manufacturer_len < sizeof(dev->info.manufacturer)) {
556 os_memset(dev->info.manufacturer, 0,
557 sizeof(dev->info.manufacturer));
558 os_memcpy(dev->info.manufacturer, msg->manufacturer,
559 msg->manufacturer_len);
560 }
561
562 if (msg->model_name &&
563 msg->model_name_len < sizeof(dev->info.model_name)) {
564 os_memset(dev->info.model_name, 0,
565 sizeof(dev->info.model_name));
566 os_memcpy(dev->info.model_name, msg->model_name,
567 msg->model_name_len);
568 }
569
570 if (msg->model_number &&
571 msg->model_number_len < sizeof(dev->info.model_number)) {
572 os_memset(dev->info.model_number, 0,
573 sizeof(dev->info.model_number));
574 os_memcpy(dev->info.model_number, msg->model_number,
575 msg->model_number_len);
576 }
577
578 if (msg->serial_number &&
579 msg->serial_number_len < sizeof(dev->info.serial_number)) {
580 os_memset(dev->info.serial_number, 0,
581 sizeof(dev->info.serial_number));
582 os_memcpy(dev->info.serial_number, msg->serial_number,
583 msg->serial_number_len);
584 }
585
586 if (msg->pri_dev_type)
587 os_memcpy(dev->info.pri_dev_type, msg->pri_dev_type,
588 sizeof(dev->info.pri_dev_type));
589 else if (msg->wps_pri_dev_type)
590 os_memcpy(dev->info.pri_dev_type, msg->wps_pri_dev_type,
591 sizeof(dev->info.pri_dev_type));
592
593 if (msg->wps_sec_dev_type_list) {
594 os_memcpy(dev->info.wps_sec_dev_type_list,
595 msg->wps_sec_dev_type_list,
596 msg->wps_sec_dev_type_list_len);
597 dev->info.wps_sec_dev_type_list_len =
598 msg->wps_sec_dev_type_list_len;
599 }
600
601 if (msg->capability) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700602 /*
603 * P2P Client Discoverability bit is reserved in all frames
604 * that use this function, so do not change its value here.
605 */
606 dev->info.dev_capab &= P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
607 dev->info.dev_capab |= msg->capability[0] &
608 ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700609 dev->info.group_capab = msg->capability[1];
610 }
611
612 if (msg->ext_listen_timing) {
613 dev->ext_listen_period = WPA_GET_LE16(msg->ext_listen_timing);
614 dev->ext_listen_interval =
615 WPA_GET_LE16(msg->ext_listen_timing + 2);
616 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700617
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700618 if (!probe_req) {
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800619 u16 new_config_methods;
620 new_config_methods = msg->config_methods ?
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700621 msg->config_methods : msg->wps_config_methods;
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800622 if (new_config_methods &&
623 dev->info.config_methods != new_config_methods) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700624 p2p_dbg(p2p, "Update peer " MACSTR
625 " config_methods 0x%x -> 0x%x",
626 MAC2STR(dev->info.p2p_device_addr),
627 dev->info.config_methods,
628 new_config_methods);
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800629 dev->info.config_methods = new_config_methods;
630 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700631 }
632}
633
634
Dmitry Shmidt2e67f062014-07-16 09:55:28 -0700635static void p2p_update_peer_vendor_elems(struct p2p_device *dev, const u8 *ies,
636 size_t ies_len)
637{
638 const u8 *pos, *end;
639 u8 id, len;
640
641 wpabuf_free(dev->info.vendor_elems);
642 dev->info.vendor_elems = NULL;
643
644 end = ies + ies_len;
645
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800646 for (pos = ies; end - pos > 1; pos += len) {
Dmitry Shmidt2e67f062014-07-16 09:55:28 -0700647 id = *pos++;
648 len = *pos++;
649
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800650 if (len > end - pos)
Dmitry Shmidt2e67f062014-07-16 09:55:28 -0700651 break;
652
653 if (id != WLAN_EID_VENDOR_SPECIFIC || len < 3)
654 continue;
655
656 if (len >= 4) {
657 u32 type = WPA_GET_BE32(pos);
658
659 if (type == WPA_IE_VENDOR_TYPE ||
660 type == WMM_IE_VENDOR_TYPE ||
661 type == WPS_IE_VENDOR_TYPE ||
662 type == P2P_IE_VENDOR_TYPE ||
663 type == WFD_IE_VENDOR_TYPE)
664 continue;
665 }
666
667 /* Unknown vendor element - make raw IE data available */
668 if (wpabuf_resize(&dev->info.vendor_elems, 2 + len) < 0)
669 break;
670 wpabuf_put_data(dev->info.vendor_elems, pos - 2, 2 + len);
Hai Shalom60840252021-02-19 19:02:11 -0800671 if (wpabuf_size(dev->info.vendor_elems) > 2000)
672 break;
Dmitry Shmidt2e67f062014-07-16 09:55:28 -0700673 }
674}
675
676
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800677static int p2p_compare_wfd_info(struct p2p_device *dev,
678 const struct p2p_message *msg)
679{
680 if (dev->info.wfd_subelems && msg->wfd_subelems) {
681 if (dev->info.wfd_subelems->used != msg->wfd_subelems->used)
682 return 1;
683
684 return os_memcmp(dev->info.wfd_subelems->buf,
685 msg->wfd_subelems->buf,
686 dev->info.wfd_subelems->used);
687 }
688 if (dev->info.wfd_subelems || msg->wfd_subelems)
689 return 1;
690
691 return 0;
692}
693
694
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700695/**
Dmitry Shmidt04949592012-07-19 12:16:46 -0700696 * p2p_add_device - Add peer entries based on scan results or P2P frames
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700697 * @p2p: P2P module context from p2p_init()
698 * @addr: Source address of Beacon or Probe Response frame (may be either
699 * P2P Device Address or P2P Interface Address)
700 * @level: Signal level (signal strength of the received frame from the peer)
701 * @freq: Frequency on which the Beacon or Probe Response frame was received
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800702 * @rx_time: Time when the result was received
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700703 * @ies: IEs from the Beacon or Probe Response frame
704 * @ies_len: Length of ies buffer in octets
Dmitry Shmidt04949592012-07-19 12:16:46 -0700705 * @scan_res: Whether this was based on scan results
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700706 * Returns: 0 on success, -1 on failure
707 *
708 * If the scan result is for a GO, the clients in the group will also be added
709 * to the peer table. This function can also be used with some other frames
710 * like Provision Discovery Request that contains P2P Capability and P2P Device
711 * Info attributes.
712 */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800713int p2p_add_device(struct p2p_data *p2p, const u8 *addr, int freq,
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800714 struct os_reltime *rx_time, int level, const u8 *ies,
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800715 size_t ies_len, int scan_res)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700716{
717 struct p2p_device *dev;
718 struct p2p_message msg;
719 const u8 *p2p_dev_addr;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800720 int wfd_changed;
Dmitry Shmidt29333592017-01-09 12:27:11 -0800721 int dev_name_changed;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700722 int i;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800723 struct os_reltime time_now;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700724
725 os_memset(&msg, 0, sizeof(msg));
726 if (p2p_parse_ies(ies, ies_len, &msg)) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700727 p2p_dbg(p2p, "Failed to parse P2P IE for a device entry");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700728 p2p_parse_free(&msg);
729 return -1;
730 }
731
732 if (msg.p2p_device_addr)
733 p2p_dev_addr = msg.p2p_device_addr;
734 else if (msg.device_id)
735 p2p_dev_addr = msg.device_id;
736 else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700737 p2p_dbg(p2p, "Ignore scan data without P2P Device Info or P2P Device Id");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700738 p2p_parse_free(&msg);
739 return -1;
740 }
741
742 if (!is_zero_ether_addr(p2p->peer_filter) &&
743 os_memcmp(p2p_dev_addr, p2p->peer_filter, ETH_ALEN) != 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700744 p2p_dbg(p2p, "Do not add peer filter for " MACSTR
745 " due to peer filter", MAC2STR(p2p_dev_addr));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800746 p2p_parse_free(&msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700747 return 0;
748 }
749
750 dev = p2p_create_device(p2p, p2p_dev_addr);
751 if (dev == NULL) {
752 p2p_parse_free(&msg);
753 return -1;
754 }
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800755
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800756 if (rx_time == NULL) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800757 os_get_reltime(&time_now);
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800758 rx_time = &time_now;
759 }
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800760
761 /*
762 * Update the device entry only if the new peer
Dmitry Shmidta3dc3092015-06-23 11:21:28 -0700763 * entry is newer than the one previously stored, or if
764 * the device was previously seen as a P2P Client in a group
765 * and the new entry isn't older than a threshold.
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800766 */
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800767 if (dev->last_seen.sec > 0 &&
Dmitry Shmidta3dc3092015-06-23 11:21:28 -0700768 os_reltime_before(rx_time, &dev->last_seen) &&
769 (!(dev->flags & P2P_DEV_LAST_SEEN_AS_GROUP_CLIENT) ||
770 os_reltime_expired(&dev->last_seen, rx_time,
771 P2P_DEV_GROUP_CLIENT_RESP_THRESHOLD))) {
772 p2p_dbg(p2p,
773 "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 -0800774 (unsigned int) rx_time->sec,
775 (unsigned int) rx_time->usec,
776 (unsigned int) dev->last_seen.sec,
Dmitry Shmidta3dc3092015-06-23 11:21:28 -0700777 (unsigned int) dev->last_seen.usec,
778 dev->flags);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800779 p2p_parse_free(&msg);
780 return -1;
781 }
782
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800783 os_memcpy(&dev->last_seen, rx_time, sizeof(struct os_reltime));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800784
Dmitry Shmidta3dc3092015-06-23 11:21:28 -0700785 dev->flags &= ~(P2P_DEV_PROBE_REQ_ONLY | P2P_DEV_GROUP_CLIENT_ONLY |
786 P2P_DEV_LAST_SEEN_AS_GROUP_CLIENT);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700787
788 if (os_memcmp(addr, p2p_dev_addr, ETH_ALEN) != 0)
789 os_memcpy(dev->interface_addr, addr, ETH_ALEN);
790 if (msg.ssid &&
Jouni Malinenfdb708a2015-04-07 11:32:11 +0300791 msg.ssid[1] <= sizeof(dev->oper_ssid) &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700792 (msg.ssid[1] != P2P_WILDCARD_SSID_LEN ||
793 os_memcmp(msg.ssid + 2, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN)
794 != 0)) {
795 os_memcpy(dev->oper_ssid, msg.ssid + 2, msg.ssid[1]);
796 dev->oper_ssid_len = msg.ssid[1];
797 }
798
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -0700799 wpabuf_free(dev->info.p2ps_instance);
800 dev->info.p2ps_instance = NULL;
801 if (msg.adv_service_instance && msg.adv_service_instance_len)
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800802 dev->info.p2ps_instance = wpabuf_alloc_copy(
803 msg.adv_service_instance, msg.adv_service_instance_len);
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800804
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700805 if (freq >= 2412 && freq <= 2484 && msg.ds_params &&
806 *msg.ds_params >= 1 && *msg.ds_params <= 14) {
807 int ds_freq;
808 if (*msg.ds_params == 14)
809 ds_freq = 2484;
810 else
811 ds_freq = 2407 + *msg.ds_params * 5;
812 if (freq != ds_freq) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700813 p2p_dbg(p2p, "Update Listen frequency based on DS Parameter Set IE: %d -> %d MHz",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700814 freq, ds_freq);
815 freq = ds_freq;
816 }
817 }
818
Dmitry Shmidt04949592012-07-19 12:16:46 -0700819 if (dev->listen_freq && dev->listen_freq != freq && scan_res) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700820 p2p_dbg(p2p, "Update Listen frequency based on scan results ("
821 MACSTR " %d -> %d MHz (DS param %d)",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700822 MAC2STR(dev->info.p2p_device_addr), dev->listen_freq,
823 freq, msg.ds_params ? *msg.ds_params : -1);
824 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700825 if (scan_res) {
826 dev->listen_freq = freq;
827 if (msg.group_info)
828 dev->oper_freq = freq;
829 }
Jouni Malinen75ecf522011-06-27 15:19:46 -0700830 dev->info.level = level;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700831
Dmitry Shmidt29333592017-01-09 12:27:11 -0800832 dev_name_changed = os_strncmp(dev->info.device_name, msg.device_name,
833 WPS_DEV_NAME_MAX_LEN) != 0;
834
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700835 p2p_copy_wps_info(p2p, dev, 0, &msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700836
837 for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) {
838 wpabuf_free(dev->info.wps_vendor_ext[i]);
839 dev->info.wps_vendor_ext[i] = NULL;
840 }
841
842 for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) {
843 if (msg.wps_vendor_ext[i] == NULL)
844 break;
845 dev->info.wps_vendor_ext[i] = wpabuf_alloc_copy(
846 msg.wps_vendor_ext[i], msg.wps_vendor_ext_len[i]);
847 if (dev->info.wps_vendor_ext[i] == NULL)
848 break;
849 }
850
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800851 wfd_changed = p2p_compare_wfd_info(dev, &msg);
852
Dmitry Shmidt29333592017-01-09 12:27:11 -0800853 if (wfd_changed) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700854 wpabuf_free(dev->info.wfd_subelems);
Dmitry Shmidt29333592017-01-09 12:27:11 -0800855 if (msg.wfd_subelems)
856 dev->info.wfd_subelems = wpabuf_dup(msg.wfd_subelems);
857 else
858 dev->info.wfd_subelems = NULL;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700859 }
860
Dmitry Shmidt04949592012-07-19 12:16:46 -0700861 if (scan_res) {
862 p2p_add_group_clients(p2p, p2p_dev_addr, addr, freq,
Dmitry Shmidta3dc3092015-06-23 11:21:28 -0700863 msg.group_info, msg.group_info_len,
864 rx_time);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700865 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700866
867 p2p_parse_free(&msg);
868
Dmitry Shmidt2e67f062014-07-16 09:55:28 -0700869 p2p_update_peer_vendor_elems(dev, ies, ies_len);
870
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800871 if (dev->flags & P2P_DEV_REPORTED && !wfd_changed &&
Dmitry Shmidt29333592017-01-09 12:27:11 -0800872 !dev_name_changed &&
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800873 (!msg.adv_service_instance ||
874 (dev->flags & P2P_DEV_P2PS_REPORTED)))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700875 return 0;
876
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700877 p2p_dbg(p2p, "Peer found with Listen frequency %d MHz (rx_time=%u.%06u)",
878 freq, (unsigned int) rx_time->sec,
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800879 (unsigned int) rx_time->usec);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700880 if (dev->flags & P2P_DEV_USER_REJECTED) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700881 p2p_dbg(p2p, "Do not report rejected device");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700882 return 0;
883 }
884
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800885 if (dev->info.config_methods == 0 &&
886 (freq == 2412 || freq == 2437 || freq == 2462)) {
887 /*
888 * If we have only seen a Beacon frame from a GO, we do not yet
889 * know what WPS config methods it supports. Since some
890 * applications use config_methods value from P2P-DEVICE-FOUND
891 * events, postpone reporting this peer until we've fully
892 * discovered its capabilities.
893 *
894 * At least for now, do this only if the peer was detected on
895 * one of the social channels since that peer can be easily be
896 * found again and there are no limitations of having to use
897 * passive scan on this channels, so this can be done through
898 * Probe Response frame that includes the config_methods
899 * information.
900 */
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700901 p2p_dbg(p2p, "Do not report peer " MACSTR
902 " with unknown config methods", MAC2STR(addr));
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800903 return 0;
904 }
905
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700906 p2p->cfg->dev_found(p2p->cfg->cb_ctx, addr, &dev->info,
907 !(dev->flags & P2P_DEV_REPORTED_ONCE));
908 dev->flags |= P2P_DEV_REPORTED | P2P_DEV_REPORTED_ONCE;
909
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800910 if (msg.adv_service_instance)
911 dev->flags |= P2P_DEV_P2PS_REPORTED;
912
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700913 return 0;
914}
915
916
917static void p2p_device_free(struct p2p_data *p2p, struct p2p_device *dev)
918{
919 int i;
920
Dmitry Shmidt497c1d52011-07-21 15:19:46 -0700921 if (p2p->go_neg_peer == dev) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800922 /*
923 * If GO Negotiation is in progress, report that it has failed.
924 */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800925 p2p_go_neg_failed(p2p, -1);
Dmitry Shmidt497c1d52011-07-21 15:19:46 -0700926 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700927 if (p2p->invite_peer == dev)
928 p2p->invite_peer = NULL;
929 if (p2p->sd_peer == dev)
930 p2p->sd_peer = NULL;
931 if (p2p->pending_client_disc_go == dev)
932 p2p->pending_client_disc_go = NULL;
933
Dmitry Shmidtc55524a2011-07-07 11:18:38 -0700934 /* dev_lost() device, but only if it was previously dev_found() */
935 if (dev->flags & P2P_DEV_REPORTED_ONCE)
936 p2p->cfg->dev_lost(p2p->cfg->cb_ctx,
937 dev->info.p2p_device_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700938
939 for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) {
940 wpabuf_free(dev->info.wps_vendor_ext[i]);
941 dev->info.wps_vendor_ext[i] = NULL;
942 }
943
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700944 wpabuf_free(dev->info.wfd_subelems);
Dmitry Shmidt2e67f062014-07-16 09:55:28 -0700945 wpabuf_free(dev->info.vendor_elems);
Dmitry Shmidt413dde72014-04-11 10:23:22 -0700946 wpabuf_free(dev->go_neg_conf);
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800947 wpabuf_free(dev->info.p2ps_instance);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700948
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700949 os_free(dev);
950}
951
952
953static int p2p_get_next_prog_freq(struct p2p_data *p2p)
954{
955 struct p2p_channels *c;
956 struct p2p_reg_class *cla;
957 size_t cl, ch;
958 int found = 0;
959 u8 reg_class;
960 u8 channel;
961 int freq;
962
963 c = &p2p->cfg->channels;
964 for (cl = 0; cl < c->reg_classes; cl++) {
965 cla = &c->reg_class[cl];
966 if (cla->reg_class != p2p->last_prog_scan_class)
967 continue;
968 for (ch = 0; ch < cla->channels; ch++) {
969 if (cla->channel[ch] == p2p->last_prog_scan_chan) {
970 found = 1;
971 break;
972 }
973 }
974 if (found)
975 break;
976 }
977
978 if (!found) {
979 /* Start from beginning */
980 reg_class = c->reg_class[0].reg_class;
981 channel = c->reg_class[0].channel[0];
982 } else {
983 /* Pick the next channel */
984 ch++;
985 if (ch == cla->channels) {
986 cl++;
987 if (cl == c->reg_classes)
988 cl = 0;
989 ch = 0;
990 }
991 reg_class = c->reg_class[cl].reg_class;
992 channel = c->reg_class[cl].channel[ch];
993 }
994
Dmitry Shmidt4b060592013-04-29 16:42:49 -0700995 freq = p2p_channel_to_freq(reg_class, channel);
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700996 p2p_dbg(p2p, "Next progressive search channel: reg_class %u channel %u -> %d MHz",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700997 reg_class, channel, freq);
998 p2p->last_prog_scan_class = reg_class;
999 p2p->last_prog_scan_chan = channel;
1000
1001 if (freq == 2412 || freq == 2437 || freq == 2462)
1002 return 0; /* No need to add social channels */
1003 return freq;
1004}
1005
1006
1007static void p2p_search(struct p2p_data *p2p)
1008{
1009 int freq = 0;
1010 enum p2p_scan_type type;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001011 u16 pw_id = DEV_PW_DEFAULT;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001012 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001013
1014 if (p2p->drv_in_listen) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001015 p2p_dbg(p2p, "Driver is still in Listen state - wait for it to end before continuing");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001016 return;
1017 }
1018 p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
1019
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001020 if (p2p->find_pending_full &&
1021 (p2p->find_type == P2P_FIND_PROGRESSIVE ||
1022 p2p->find_type == P2P_FIND_START_WITH_FULL)) {
1023 type = P2P_SCAN_FULL;
1024 p2p_dbg(p2p, "Starting search (pending full scan)");
1025 p2p->find_pending_full = 0;
1026 } else if ((p2p->find_type == P2P_FIND_PROGRESSIVE &&
1027 (freq = p2p_get_next_prog_freq(p2p)) > 0) ||
1028 (p2p->find_type == P2P_FIND_START_WITH_FULL &&
1029 (freq = p2p->find_specified_freq) > 0)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001030 type = P2P_SCAN_SOCIAL_PLUS_ONE;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001031 p2p_dbg(p2p, "Starting search (+ freq %u)", freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001032 } else {
1033 type = P2P_SCAN_SOCIAL;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001034 p2p_dbg(p2p, "Starting search");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001035 }
1036
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001037 res = p2p->cfg->p2p_scan(p2p->cfg->cb_ctx, type, freq,
1038 p2p->num_req_dev_types, p2p->req_dev_types,
1039 p2p->find_dev_id, pw_id);
1040 if (res < 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001041 p2p_dbg(p2p, "Scan request schedule failed");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001042 p2p_continue_find(p2p);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001043 }
1044}
1045
1046
1047static void p2p_find_timeout(void *eloop_ctx, void *timeout_ctx)
1048{
1049 struct p2p_data *p2p = eloop_ctx;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001050 p2p_dbg(p2p, "Find timeout -> stop");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001051 p2p_stop_find(p2p);
1052}
1053
1054
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001055void p2p_notify_scan_trigger_status(struct p2p_data *p2p, int status)
1056{
1057 if (status != 0) {
1058 p2p_dbg(p2p, "Scan request failed");
1059 /* Do continue find even for the first p2p_find_scan */
1060 p2p_continue_find(p2p);
1061 } else {
1062 p2p_dbg(p2p, "Running p2p_scan");
1063 p2p->p2p_scan_running = 1;
1064 eloop_cancel_timeout(p2p_scan_timeout, p2p, NULL);
1065 eloop_register_timeout(P2P_SCAN_TIMEOUT, 0, p2p_scan_timeout,
1066 p2p, NULL);
1067 }
1068}
1069
1070
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001071static int p2p_run_after_scan(struct p2p_data *p2p)
1072{
1073 struct p2p_device *dev;
1074 enum p2p_after_scan op;
1075
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001076 op = p2p->start_after_scan;
1077 p2p->start_after_scan = P2P_AFTER_SCAN_NOTHING;
1078 switch (op) {
1079 case P2P_AFTER_SCAN_NOTHING:
1080 break;
1081 case P2P_AFTER_SCAN_LISTEN:
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001082 p2p_dbg(p2p, "Start previously requested Listen state");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001083 p2p_listen(p2p, p2p->pending_listen_sec * 1000 +
1084 p2p->pending_listen_usec / 1000);
1085 return 1;
1086 case P2P_AFTER_SCAN_CONNECT:
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001087 p2p_dbg(p2p, "Start previously requested connect with " MACSTR,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001088 MAC2STR(p2p->after_scan_peer));
1089 dev = p2p_get_device(p2p, p2p->after_scan_peer);
1090 if (dev == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001091 p2p_dbg(p2p, "Peer not known anymore");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001092 break;
1093 }
1094 p2p_connect_send(p2p, dev);
1095 return 1;
1096 }
1097
1098 return 0;
1099}
1100
1101
1102static void p2p_scan_timeout(void *eloop_ctx, void *timeout_ctx)
1103{
1104 struct p2p_data *p2p = eloop_ctx;
1105 int running;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001106 p2p_dbg(p2p, "p2p_scan timeout (running=%d)", p2p->p2p_scan_running);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001107 running = p2p->p2p_scan_running;
1108 /* Make sure we recover from missed scan results callback */
1109 p2p->p2p_scan_running = 0;
1110
1111 if (running)
1112 p2p_run_after_scan(p2p);
1113}
1114
1115
1116static void p2p_free_req_dev_types(struct p2p_data *p2p)
1117{
1118 p2p->num_req_dev_types = 0;
1119 os_free(p2p->req_dev_types);
1120 p2p->req_dev_types = NULL;
1121}
1122
1123
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001124static int p2ps_gen_hash(struct p2p_data *p2p, const char *str, u8 *hash)
1125{
1126 u8 buf[SHA256_MAC_LEN];
1127 char str_buf[256];
1128 const u8 *adv_array;
1129 size_t i, adv_len;
1130
1131 if (!str || !hash)
1132 return 0;
1133
1134 if (!str[0]) {
1135 os_memcpy(hash, p2p->wild_card_hash, P2PS_HASH_LEN);
1136 return 1;
1137 }
1138
1139 adv_array = (u8 *) str_buf;
1140 adv_len = os_strlen(str);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07001141 if (adv_len >= sizeof(str_buf))
1142 return 0;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001143
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001144 for (i = 0; i < adv_len; i++) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001145 if (str[i] >= 'A' && str[i] <= 'Z')
1146 str_buf[i] = str[i] - 'A' + 'a';
1147 else
1148 str_buf[i] = str[i];
1149 }
1150
1151 if (sha256_vector(1, &adv_array, &adv_len, buf))
1152 return 0;
1153
1154 os_memcpy(hash, buf, P2PS_HASH_LEN);
1155 return 1;
1156}
1157
1158
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001159int p2p_find(struct p2p_data *p2p, unsigned int timeout,
1160 enum p2p_discovery_type type,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001161 unsigned int num_req_dev_types, const u8 *req_dev_types,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001162 const u8 *dev_id, unsigned int search_delay,
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001163 u8 seek_count, const char **seek, int freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001164{
1165 int res;
Hai Shalom74f70d42019-02-11 14:42:39 -08001166 struct os_reltime start;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001167
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001168 p2p_dbg(p2p, "Starting find (type=%d)", type);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001169 if (p2p->p2p_scan_running) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001170 p2p_dbg(p2p, "p2p_scan is already running");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001171 }
1172
1173 p2p_free_req_dev_types(p2p);
1174 if (req_dev_types && num_req_dev_types) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001175 p2p->req_dev_types = os_memdup(req_dev_types,
1176 num_req_dev_types *
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001177 WPS_DEV_TYPE_LEN);
1178 if (p2p->req_dev_types == NULL)
1179 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001180 p2p->num_req_dev_types = num_req_dev_types;
1181 }
1182
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001183 if (dev_id) {
1184 os_memcpy(p2p->find_dev_id_buf, dev_id, ETH_ALEN);
1185 p2p->find_dev_id = p2p->find_dev_id_buf;
1186 } else
1187 p2p->find_dev_id = NULL;
1188
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001189 if (seek_count == 0 || !seek) {
1190 /* Not an ASP search */
1191 p2p->p2ps_seek = 0;
1192 } else if (seek_count == 1 && seek && (!seek[0] || !seek[0][0])) {
1193 /*
1194 * An empty seek string means no hash values, but still an ASP
1195 * search.
1196 */
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07001197 p2p_dbg(p2p, "ASP search");
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001198 p2p->p2ps_seek_count = 0;
1199 p2p->p2ps_seek = 1;
1200 } else if (seek && seek_count <= P2P_MAX_QUERY_HASH) {
1201 u8 buf[P2PS_HASH_LEN];
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07001202 int i, count = 0;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001203
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001204 for (i = 0; i < seek_count; i++) {
1205 if (!p2ps_gen_hash(p2p, seek[i], buf))
1206 continue;
1207
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07001208 p2p_dbg(p2p, "Seek service %s hash " MACSTR,
1209 seek[i], MAC2STR(buf));
1210 os_memcpy(&p2p->p2ps_seek_hash[count * P2PS_HASH_LEN],
1211 buf, P2PS_HASH_LEN);
1212 count++;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001213 }
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07001214
1215 p2p->p2ps_seek_count = count;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001216 p2p->p2ps_seek = 1;
1217 } else {
1218 p2p->p2ps_seek_count = 0;
1219 p2p->p2ps_seek = 1;
1220 }
1221
1222 /* Special case to perform wildcard search */
1223 if (p2p->p2ps_seek_count == 0 && p2p->p2ps_seek) {
1224 p2p->p2ps_seek_count = 1;
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07001225 os_memcpy(&p2p->p2ps_seek_hash, p2p->wild_card_hash,
1226 P2PS_HASH_LEN);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001227 }
1228
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001229 p2p->start_after_scan = P2P_AFTER_SCAN_NOTHING;
1230 p2p_clear_timeout(p2p);
Dmitry Shmidtde47be72016-01-07 12:52:55 -08001231 if (p2p->pending_listen_freq) {
1232 p2p_dbg(p2p, "Clear pending_listen_freq for p2p_find");
1233 p2p->pending_listen_freq = 0;
1234 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001235 p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001236 p2p->find_pending_full = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001237 p2p->find_type = type;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001238 if (freq != 2412 && freq != 2437 && freq != 2462 && freq != 60480)
1239 p2p->find_specified_freq = freq;
1240 else
1241 p2p->find_specified_freq = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001242 p2p_device_clear_reported(p2p);
Dmitry Shmidtde47be72016-01-07 12:52:55 -08001243 os_memset(p2p->sd_query_no_ack, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001244 p2p_set_state(p2p, P2P_SEARCH);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001245 p2p->search_delay = search_delay;
1246 p2p->in_search_delay = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001247 eloop_cancel_timeout(p2p_find_timeout, p2p, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001248 p2p->last_p2p_find_timeout = timeout;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001249 if (timeout)
1250 eloop_register_timeout(timeout, 0, p2p_find_timeout,
1251 p2p, NULL);
Hai Shalom74f70d42019-02-11 14:42:39 -08001252 os_get_reltime(&start);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001253 switch (type) {
1254 case P2P_FIND_START_WITH_FULL:
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001255 if (freq > 0) {
1256 /*
1257 * Start with the specified channel and then move to
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001258 * scans for social channels and this specific channel.
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001259 */
1260 res = p2p->cfg->p2p_scan(p2p->cfg->cb_ctx,
1261 P2P_SCAN_SPECIFIC, freq,
1262 p2p->num_req_dev_types,
1263 p2p->req_dev_types, dev_id,
1264 DEV_PW_DEFAULT);
1265 break;
1266 }
1267 /* fall through */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001268 case P2P_FIND_PROGRESSIVE:
1269 res = p2p->cfg->p2p_scan(p2p->cfg->cb_ctx, P2P_SCAN_FULL, 0,
1270 p2p->num_req_dev_types,
Dmitry Shmidt04949592012-07-19 12:16:46 -07001271 p2p->req_dev_types, dev_id,
1272 DEV_PW_DEFAULT);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001273 break;
1274 case P2P_FIND_ONLY_SOCIAL:
1275 res = p2p->cfg->p2p_scan(p2p->cfg->cb_ctx, P2P_SCAN_SOCIAL, 0,
1276 p2p->num_req_dev_types,
Dmitry Shmidt04949592012-07-19 12:16:46 -07001277 p2p->req_dev_types, dev_id,
1278 DEV_PW_DEFAULT);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001279 break;
1280 default:
1281 return -1;
1282 }
1283
Hai Shalom74f70d42019-02-11 14:42:39 -08001284 if (!res)
1285 p2p->find_start = start;
1286
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001287 if (res != 0 && p2p->p2p_scan_running) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001288 p2p_dbg(p2p, "Failed to start p2p_scan - another p2p_scan was already running");
1289 /* wait for the previous p2p_scan to complete */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001290 if (type == P2P_FIND_PROGRESSIVE ||
1291 (type == P2P_FIND_START_WITH_FULL && freq == 0))
1292 p2p->find_pending_full = 1;
Dmitry Shmidt96be6222014-02-13 10:16:51 -08001293 res = 0; /* do not report failure */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001294 } else if (res != 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001295 p2p_dbg(p2p, "Failed to start p2p_scan");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001296 p2p_set_state(p2p, P2P_IDLE);
1297 eloop_cancel_timeout(p2p_find_timeout, p2p, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001298 }
1299
1300 return res;
1301}
1302
1303
1304void p2p_stop_find_for_freq(struct p2p_data *p2p, int freq)
1305{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001306 p2p_dbg(p2p, "Stopping find");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001307 eloop_cancel_timeout(p2p_find_timeout, p2p, NULL);
1308 p2p_clear_timeout(p2p);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001309 if (p2p->state == P2P_SEARCH || p2p->state == P2P_SD_DURING_FIND)
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001310 p2p->cfg->find_stopped(p2p->cfg->cb_ctx);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001311
1312 p2p->p2ps_seek_count = 0;
1313
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001314 p2p_set_state(p2p, P2P_IDLE);
1315 p2p_free_req_dev_types(p2p);
1316 p2p->start_after_scan = P2P_AFTER_SCAN_NOTHING;
Dmitry Shmidt8c652892013-03-01 10:14:01 -08001317 if (p2p->go_neg_peer)
1318 p2p->go_neg_peer->flags &= ~P2P_DEV_PEER_WAITING_RESPONSE;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001319 p2p->go_neg_peer = NULL;
1320 p2p->sd_peer = NULL;
1321 p2p->invite_peer = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001322 p2p_stop_listen_for_freq(p2p, freq);
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001323 p2p->send_action_in_progress = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001324}
1325
1326
1327void p2p_stop_listen_for_freq(struct p2p_data *p2p, int freq)
1328{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001329 if (freq > 0 && p2p->drv_in_listen == freq && p2p->in_listen) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001330 p2p_dbg(p2p, "Skip stop_listen since we are on correct channel for response");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001331 return;
1332 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001333 if (p2p->in_listen) {
1334 p2p->in_listen = 0;
1335 p2p_clear_timeout(p2p);
1336 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001337 if (p2p->drv_in_listen) {
1338 /*
1339 * The driver may not deliver callback to p2p_listen_end()
1340 * when the operation gets canceled, so clear the internal
1341 * variable that is tracking driver state.
1342 */
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001343 p2p_dbg(p2p, "Clear drv_in_listen (%d)", p2p->drv_in_listen);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001344 p2p->drv_in_listen = 0;
1345 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001346 p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
1347}
1348
1349
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001350void p2p_stop_listen(struct p2p_data *p2p)
1351{
1352 if (p2p->state != P2P_LISTEN_ONLY) {
1353 p2p_dbg(p2p, "Skip stop_listen since not in listen_only state.");
1354 return;
1355 }
1356
1357 p2p_stop_listen_for_freq(p2p, 0);
1358 p2p_set_state(p2p, P2P_IDLE);
1359}
1360
1361
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001362void p2p_stop_find(struct p2p_data *p2p)
1363{
Dmitry Shmidt3c57b3f2014-05-22 15:13:07 -07001364 p2p->pending_listen_freq = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001365 p2p_stop_find_for_freq(p2p, 0);
1366}
1367
1368
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001369static int p2p_prepare_channel_pref(struct p2p_data *p2p,
1370 unsigned int force_freq,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001371 unsigned int pref_freq, int go)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001372{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001373 u8 op_class, op_channel;
1374 unsigned int freq = force_freq ? force_freq : pref_freq;
1375
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001376 p2p_dbg(p2p, "Prepare channel pref - force_freq=%u pref_freq=%u go=%d",
1377 force_freq, pref_freq, go);
Dmitry Shmidt4b060592013-04-29 16:42:49 -07001378 if (p2p_freq_to_channel(freq, &op_class, &op_channel) < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001379 p2p_dbg(p2p, "Unsupported frequency %u MHz", freq);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001380 return -1;
1381 }
1382
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001383 if (!p2p_channels_includes(&p2p->cfg->channels, op_class, op_channel) &&
1384 (go || !p2p_channels_includes(&p2p->cfg->cli_channels, op_class,
1385 op_channel))) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001386 p2p_dbg(p2p, "Frequency %u MHz (oper_class %u channel %u) not allowed for P2P",
1387 freq, op_class, op_channel);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001388 return -1;
1389 }
1390
1391 p2p->op_reg_class = op_class;
1392 p2p->op_channel = op_channel;
1393
1394 if (force_freq) {
1395 p2p->channels.reg_classes = 1;
1396 p2p->channels.reg_class[0].channels = 1;
1397 p2p->channels.reg_class[0].reg_class = p2p->op_reg_class;
1398 p2p->channels.reg_class[0].channel[0] = p2p->op_channel;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001399 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001400 os_memcpy(&p2p->channels, &p2p->cfg->channels,
1401 sizeof(struct p2p_channels));
1402 }
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001403
1404 return 0;
1405}
1406
1407
1408static void p2p_prepare_channel_best(struct p2p_data *p2p)
1409{
1410 u8 op_class, op_channel;
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07001411 const int op_classes_5ghz[] = { 124, 125, 115, 0 };
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08001412 const int op_classes_ht40[] = { 126, 127, 116, 117, 0 };
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001413 const int op_classes_vht[] = { 128, 0 };
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001414 const int op_classes_edmg[] = { 181, 182, 183, 0 };
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001415
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07001416 p2p_dbg(p2p, "Prepare channel best");
1417
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001418 if (!p2p->cfg->cfg_op_channel && p2p->best_freq_overall > 0 &&
1419 p2p_supported_freq(p2p, p2p->best_freq_overall) &&
Dmitry Shmidt4b060592013-04-29 16:42:49 -07001420 p2p_freq_to_channel(p2p->best_freq_overall, &op_class, &op_channel)
1421 == 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001422 p2p_dbg(p2p, "Select best overall channel as operating channel preference");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001423 p2p->op_reg_class = op_class;
1424 p2p->op_channel = op_channel;
1425 } else if (!p2p->cfg->cfg_op_channel && p2p->best_freq_5 > 0 &&
1426 p2p_supported_freq(p2p, p2p->best_freq_5) &&
Dmitry Shmidt4b060592013-04-29 16:42:49 -07001427 p2p_freq_to_channel(p2p->best_freq_5, &op_class, &op_channel)
1428 == 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001429 p2p_dbg(p2p, "Select best 5 GHz channel as operating channel preference");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001430 p2p->op_reg_class = op_class;
1431 p2p->op_channel = op_channel;
1432 } else if (!p2p->cfg->cfg_op_channel && p2p->best_freq_24 > 0 &&
1433 p2p_supported_freq(p2p, p2p->best_freq_24) &&
Dmitry Shmidt4b060592013-04-29 16:42:49 -07001434 p2p_freq_to_channel(p2p->best_freq_24, &op_class,
1435 &op_channel) == 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001436 p2p_dbg(p2p, "Select best 2.4 GHz channel as operating channel preference");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001437 p2p->op_reg_class = op_class;
1438 p2p->op_channel = op_channel;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07001439 } else if (p2p->cfg->num_pref_chan > 0 &&
1440 p2p_channels_includes(&p2p->cfg->channels,
1441 p2p->cfg->pref_chan[0].op_class,
1442 p2p->cfg->pref_chan[0].chan)) {
1443 p2p_dbg(p2p, "Select first pref_chan entry as operating channel preference");
1444 p2p->op_reg_class = p2p->cfg->pref_chan[0].op_class;
1445 p2p->op_channel = p2p->cfg->pref_chan[0].chan;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001446 } else if (p2p_channel_select(&p2p->cfg->channels, op_classes_edmg,
1447 &p2p->op_reg_class, &p2p->op_channel) ==
1448 0) {
1449 p2p_dbg(p2p, "Select possible EDMG channel (op_class %u channel %u) as operating channel preference",
1450 p2p->op_reg_class, p2p->op_channel);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001451 } else if (p2p_channel_select(&p2p->cfg->channels, op_classes_vht,
1452 &p2p->op_reg_class, &p2p->op_channel) ==
1453 0) {
1454 p2p_dbg(p2p, "Select possible VHT channel (op_class %u channel %u) as operating channel preference",
1455 p2p->op_reg_class, p2p->op_channel);
1456 } else if (p2p_channel_select(&p2p->cfg->channels, op_classes_ht40,
1457 &p2p->op_reg_class, &p2p->op_channel) ==
1458 0) {
1459 p2p_dbg(p2p, "Select possible HT40 channel (op_class %u channel %u) as operating channel preference",
1460 p2p->op_reg_class, p2p->op_channel);
1461 } else if (p2p_channel_select(&p2p->cfg->channels, op_classes_5ghz,
1462 &p2p->op_reg_class, &p2p->op_channel) ==
1463 0) {
1464 p2p_dbg(p2p, "Select possible 5 GHz channel (op_class %u channel %u) as operating channel preference",
1465 p2p->op_reg_class, p2p->op_channel);
Dmitry Shmidtd11f0192014-03-24 12:09:47 -07001466 } else if (p2p_channels_includes(&p2p->cfg->channels,
1467 p2p->cfg->op_reg_class,
1468 p2p->cfg->op_channel)) {
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07001469 p2p_dbg(p2p, "Select pre-configured channel as operating channel preference");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001470 p2p->op_reg_class = p2p->cfg->op_reg_class;
1471 p2p->op_channel = p2p->cfg->op_channel;
Dmitry Shmidtd11f0192014-03-24 12:09:47 -07001472 } else if (p2p_channel_random_social(&p2p->cfg->channels,
1473 &p2p->op_reg_class,
Hai Shalom74f70d42019-02-11 14:42:39 -08001474 &p2p->op_channel,
1475 NULL, NULL) == 0) {
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001476 p2p_dbg(p2p, "Select random available social 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 {
1479 /* Select any random available channel from the first available
1480 * operating class */
1481 p2p_channel_select(&p2p->cfg->channels, NULL,
1482 &p2p->op_reg_class,
1483 &p2p->op_channel);
1484 p2p_dbg(p2p, "Select random available channel %d from operating class %d as operating channel preference",
1485 p2p->op_channel, p2p->op_reg_class);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001486 }
1487
1488 os_memcpy(&p2p->channels, &p2p->cfg->channels,
1489 sizeof(struct p2p_channels));
1490}
1491
1492
1493/**
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001494 * p2p_prepare_channel - Select operating channel for GO Negotiation or P2PS PD
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001495 * @p2p: P2P module context from p2p_init()
1496 * @dev: Selected peer device
1497 * @force_freq: Forced frequency in MHz or 0 if not forced
1498 * @pref_freq: Preferred frequency in MHz or 0 if no preference
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001499 * @go: Whether the local end will be forced to be GO
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001500 * Returns: 0 on success, -1 on failure (channel not supported for P2P)
1501 *
1502 * This function is used to do initial operating channel selection for GO
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001503 * Negotiation prior to having received peer information or for P2PS PD
1504 * signalling. The selected channel may be further optimized in
1505 * p2p_reselect_channel() once the peer information is available.
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001506 */
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08001507int p2p_prepare_channel(struct p2p_data *p2p, struct p2p_device *dev,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001508 unsigned int force_freq, unsigned int pref_freq, int go)
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001509{
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001510 p2p_dbg(p2p, "Prepare channel - force_freq=%u pref_freq=%u go=%d",
1511 force_freq, pref_freq, go);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001512 if (force_freq || pref_freq) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001513 if (p2p_prepare_channel_pref(p2p, force_freq, pref_freq, go) <
1514 0)
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001515 return -1;
1516 } else {
1517 p2p_prepare_channel_best(p2p);
1518 }
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001519 p2p_channels_dump(p2p, "prepared channels", &p2p->channels);
1520 if (go)
1521 p2p_channels_remove_freqs(&p2p->channels, &p2p->no_go_freq);
1522 else if (!force_freq)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001523 p2p_channels_union_inplace(&p2p->channels,
1524 &p2p->cfg->cli_channels);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001525 p2p_channels_dump(p2p, "after go/cli filter/add", &p2p->channels);
1526
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001527 p2p_dbg(p2p, "Own preference for operation channel: Operating Class %u Channel %u%s",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001528 p2p->op_reg_class, p2p->op_channel,
1529 force_freq ? " (forced)" : "");
1530
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001531 if (force_freq)
1532 dev->flags |= P2P_DEV_FORCE_FREQ;
1533 else
1534 dev->flags &= ~P2P_DEV_FORCE_FREQ;
1535
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001536 return 0;
1537}
1538
1539
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001540static void p2p_set_dev_persistent(struct p2p_device *dev,
1541 int persistent_group)
1542{
1543 switch (persistent_group) {
1544 case 0:
1545 dev->flags &= ~(P2P_DEV_PREFER_PERSISTENT_GROUP |
1546 P2P_DEV_PREFER_PERSISTENT_RECONN);
1547 break;
1548 case 1:
1549 dev->flags |= P2P_DEV_PREFER_PERSISTENT_GROUP;
1550 dev->flags &= ~P2P_DEV_PREFER_PERSISTENT_RECONN;
1551 break;
1552 case 2:
1553 dev->flags |= P2P_DEV_PREFER_PERSISTENT_GROUP |
1554 P2P_DEV_PREFER_PERSISTENT_RECONN;
1555 break;
1556 }
1557}
1558
1559
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001560int p2p_connect(struct p2p_data *p2p, const u8 *peer_addr,
1561 enum p2p_wps_method wps_method,
1562 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07001563 unsigned int force_freq, int persistent_group,
1564 const u8 *force_ssid, size_t force_ssid_len,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001565 int pd_before_go_neg, unsigned int pref_freq, u16 oob_pw_id)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001566{
1567 struct p2p_device *dev;
1568
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001569 p2p_dbg(p2p, "Request to start group negotiation - peer=" MACSTR
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001570 " GO Intent=%d Intended Interface Address=" MACSTR
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001571 " wps_method=%d persistent_group=%d pd_before_go_neg=%d "
1572 "oob_pw_id=%u",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001573 MAC2STR(peer_addr), go_intent, MAC2STR(own_interface_addr),
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001574 wps_method, persistent_group, pd_before_go_neg, oob_pw_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001575
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001576 dev = p2p_get_device(p2p, peer_addr);
1577 if (dev == NULL || (dev->flags & P2P_DEV_PROBE_REQ_ONLY)) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001578 p2p_dbg(p2p, "Cannot connect to unknown P2P Device " MACSTR,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001579 MAC2STR(peer_addr));
1580 return -1;
1581 }
1582
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001583 if (p2p_prepare_channel(p2p, dev, force_freq, pref_freq,
1584 go_intent == 15) < 0)
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001585 return -1;
1586
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001587 if (dev->flags & P2P_DEV_GROUP_CLIENT_ONLY) {
1588 if (!(dev->info.dev_capab &
1589 P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY)) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001590 p2p_dbg(p2p, "Cannot connect to P2P Device " MACSTR
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001591 " that is in a group and is not discoverable",
1592 MAC2STR(peer_addr));
1593 return -1;
1594 }
1595 if (dev->oper_freq <= 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001596 p2p_dbg(p2p, "Cannot connect to P2P Device " MACSTR
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001597 " with incomplete information",
1598 MAC2STR(peer_addr));
1599 return -1;
1600 }
1601
1602 /*
1603 * First, try to connect directly. If the peer does not
1604 * acknowledge frames, assume it is sleeping and use device
1605 * discoverability via the GO at that point.
1606 */
1607 }
1608
Dmitry Shmidt04949592012-07-19 12:16:46 -07001609 p2p->ssid_set = 0;
1610 if (force_ssid) {
1611 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Forced SSID",
1612 force_ssid, force_ssid_len);
1613 os_memcpy(p2p->ssid, force_ssid, force_ssid_len);
1614 p2p->ssid_len = force_ssid_len;
1615 p2p->ssid_set = 1;
1616 }
1617
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001618 dev->flags &= ~P2P_DEV_NOT_YET_READY;
1619 dev->flags &= ~P2P_DEV_USER_REJECTED;
1620 dev->flags &= ~P2P_DEV_WAIT_GO_NEG_RESPONSE;
1621 dev->flags &= ~P2P_DEV_WAIT_GO_NEG_CONFIRM;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001622 if (pd_before_go_neg)
1623 dev->flags |= P2P_DEV_PD_BEFORE_GO_NEG;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001624 else {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001625 dev->flags &= ~P2P_DEV_PD_BEFORE_GO_NEG;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001626 /*
Dmitry Shmidt9cdf1b92013-02-27 12:58:50 -08001627 * Assign dialog token and tie breaker here to use the same
1628 * values in each retry within the same GO Negotiation exchange.
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001629 */
1630 dev->dialog_token++;
1631 if (dev->dialog_token == 0)
1632 dev->dialog_token = 1;
Dmitry Shmidt9cdf1b92013-02-27 12:58:50 -08001633 dev->tie_breaker = p2p->next_tie_breaker;
1634 p2p->next_tie_breaker = !p2p->next_tie_breaker;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001635 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001636 dev->connect_reqs = 0;
1637 dev->go_neg_req_sent = 0;
1638 dev->go_state = UNKNOWN_GO;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001639 p2p_set_dev_persistent(dev, persistent_group);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001640 p2p->go_intent = go_intent;
1641 os_memcpy(p2p->intended_addr, own_interface_addr, ETH_ALEN);
1642
1643 if (p2p->state != P2P_IDLE)
1644 p2p_stop_find(p2p);
1645
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001646 dev->wps_method = wps_method;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001647 dev->oob_pw_id = oob_pw_id;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001648 dev->status = P2P_SC_SUCCESS;
1649
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001650 if (p2p->p2p_scan_running) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001651 p2p_dbg(p2p, "p2p_scan running - delay connect send");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001652 p2p->start_after_scan = P2P_AFTER_SCAN_CONNECT;
1653 os_memcpy(p2p->after_scan_peer, peer_addr, ETH_ALEN);
1654 return 0;
1655 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001656
1657 return p2p_connect_send(p2p, dev);
1658}
1659
1660
1661int p2p_authorize(struct p2p_data *p2p, const u8 *peer_addr,
1662 enum p2p_wps_method wps_method,
1663 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07001664 unsigned int force_freq, int persistent_group,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001665 const u8 *force_ssid, size_t force_ssid_len,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001666 unsigned int pref_freq, u16 oob_pw_id)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001667{
1668 struct p2p_device *dev;
1669
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001670 p2p_dbg(p2p, "Request to authorize group negotiation - peer=" MACSTR
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001671 " GO Intent=%d Intended Interface Address=" MACSTR
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001672 " wps_method=%d persistent_group=%d oob_pw_id=%u",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001673 MAC2STR(peer_addr), go_intent, MAC2STR(own_interface_addr),
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001674 wps_method, persistent_group, oob_pw_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001675
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001676 dev = p2p_get_device(p2p, peer_addr);
1677 if (dev == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001678 p2p_dbg(p2p, "Cannot authorize unknown P2P Device " MACSTR,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001679 MAC2STR(peer_addr));
1680 return -1;
1681 }
1682
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001683 if (p2p_prepare_channel(p2p, dev, force_freq, pref_freq, go_intent ==
1684 15) < 0)
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001685 return -1;
1686
Dmitry Shmidt04949592012-07-19 12:16:46 -07001687 p2p->ssid_set = 0;
1688 if (force_ssid) {
1689 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Forced SSID",
1690 force_ssid, force_ssid_len);
1691 os_memcpy(p2p->ssid, force_ssid, force_ssid_len);
1692 p2p->ssid_len = force_ssid_len;
1693 p2p->ssid_set = 1;
1694 }
1695
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001696 dev->flags &= ~P2P_DEV_NOT_YET_READY;
1697 dev->flags &= ~P2P_DEV_USER_REJECTED;
1698 dev->go_neg_req_sent = 0;
1699 dev->go_state = UNKNOWN_GO;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001700 p2p_set_dev_persistent(dev, persistent_group);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001701 p2p->go_intent = go_intent;
1702 os_memcpy(p2p->intended_addr, own_interface_addr, ETH_ALEN);
1703
1704 dev->wps_method = wps_method;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001705 dev->oob_pw_id = oob_pw_id;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001706 dev->status = P2P_SC_SUCCESS;
1707
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001708 return 0;
1709}
1710
1711
1712void p2p_add_dev_info(struct p2p_data *p2p, const u8 *addr,
1713 struct p2p_device *dev, struct p2p_message *msg)
1714{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001715 os_get_reltime(&dev->last_seen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001716
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001717 p2p_copy_wps_info(p2p, dev, 0, msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001718
1719 if (msg->listen_channel) {
1720 int freq;
Dmitry Shmidt4b060592013-04-29 16:42:49 -07001721 freq = p2p_channel_to_freq(msg->listen_channel[3],
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001722 msg->listen_channel[4]);
1723 if (freq < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001724 p2p_dbg(p2p, "Unknown peer Listen channel: "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001725 "country=%c%c(0x%02x) reg_class=%u channel=%u",
1726 msg->listen_channel[0],
1727 msg->listen_channel[1],
1728 msg->listen_channel[2],
1729 msg->listen_channel[3],
1730 msg->listen_channel[4]);
1731 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001732 p2p_dbg(p2p, "Update peer " MACSTR
1733 " Listen channel: %u -> %u MHz",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001734 MAC2STR(dev->info.p2p_device_addr),
1735 dev->listen_freq, freq);
1736 dev->listen_freq = freq;
1737 }
1738 }
1739
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001740 if (msg->wfd_subelems) {
1741 wpabuf_free(dev->info.wfd_subelems);
1742 dev->info.wfd_subelems = wpabuf_dup(msg->wfd_subelems);
1743 }
1744
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001745 if (dev->flags & P2P_DEV_PROBE_REQ_ONLY) {
1746 dev->flags &= ~P2P_DEV_PROBE_REQ_ONLY;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001747 p2p_dbg(p2p, "Completed device entry based on data from GO Negotiation Request");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001748 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001749 p2p_dbg(p2p, "Created device entry based on GO Neg Req: "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001750 MACSTR " dev_capab=0x%x group_capab=0x%x name='%s' "
1751 "listen_freq=%d",
1752 MAC2STR(dev->info.p2p_device_addr),
1753 dev->info.dev_capab, dev->info.group_capab,
1754 dev->info.device_name, dev->listen_freq);
1755 }
1756
1757 dev->flags &= ~P2P_DEV_GROUP_CLIENT_ONLY;
1758
1759 if (dev->flags & P2P_DEV_USER_REJECTED) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001760 p2p_dbg(p2p, "Do not report rejected device");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001761 return;
1762 }
1763
1764 p2p->cfg->dev_found(p2p->cfg->cb_ctx, addr, &dev->info,
1765 !(dev->flags & P2P_DEV_REPORTED_ONCE));
1766 dev->flags |= P2P_DEV_REPORTED | P2P_DEV_REPORTED_ONCE;
1767}
1768
1769
1770void p2p_build_ssid(struct p2p_data *p2p, u8 *ssid, size_t *ssid_len)
1771{
1772 os_memcpy(ssid, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN);
1773 p2p_random((char *) &ssid[P2P_WILDCARD_SSID_LEN], 2);
1774 os_memcpy(&ssid[P2P_WILDCARD_SSID_LEN + 2],
1775 p2p->cfg->ssid_postfix, p2p->cfg->ssid_postfix_len);
1776 *ssid_len = P2P_WILDCARD_SSID_LEN + 2 + p2p->cfg->ssid_postfix_len;
1777}
1778
1779
1780int p2p_go_params(struct p2p_data *p2p, struct p2p_go_neg_results *params)
1781{
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001782 if (p2p->ssid_set) {
1783 os_memcpy(params->ssid, p2p->ssid, p2p->ssid_len);
1784 params->ssid_len = p2p->ssid_len;
1785 } else {
1786 p2p_build_ssid(p2p, params->ssid, &params->ssid_len);
1787 }
1788 p2p->ssid_set = 0;
1789
Jimmy Chen6d7e3902018-11-20 10:15:16 +08001790 if (p2p->passphrase_set) {
1791 os_memcpy(params->passphrase, p2p->passphrase, os_strlen(p2p->passphrase));
1792 } else {
1793 p2p_random(params->passphrase, p2p->cfg->passphrase_len);
1794 }
1795 p2p->passphrase_set = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001796 return 0;
1797}
1798
1799
1800void p2p_go_complete(struct p2p_data *p2p, struct p2p_device *peer)
1801{
1802 struct p2p_go_neg_results res;
1803 int go = peer->go_state == LOCAL_GO;
1804 struct p2p_channels intersection;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001805
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001806 p2p_dbg(p2p, "GO Negotiation with " MACSTR " completed (%s will be GO)",
1807 MAC2STR(peer->info.p2p_device_addr), go ? "local end" : "peer");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001808
1809 os_memset(&res, 0, sizeof(res));
1810 res.role_go = go;
1811 os_memcpy(res.peer_device_addr, peer->info.p2p_device_addr, ETH_ALEN);
1812 os_memcpy(res.peer_interface_addr, peer->intended_addr, ETH_ALEN);
1813 res.wps_method = peer->wps_method;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001814 if (peer->flags & P2P_DEV_PREFER_PERSISTENT_GROUP) {
1815 if (peer->flags & P2P_DEV_PREFER_PERSISTENT_RECONN)
1816 res.persistent_group = 2;
1817 else
1818 res.persistent_group = 1;
1819 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001820
1821 if (go) {
1822 /* Setup AP mode for WPS provisioning */
Dmitry Shmidt4b060592013-04-29 16:42:49 -07001823 res.freq = p2p_channel_to_freq(p2p->op_reg_class,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001824 p2p->op_channel);
1825 os_memcpy(res.ssid, p2p->ssid, p2p->ssid_len);
1826 res.ssid_len = p2p->ssid_len;
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07001827 p2p_random(res.passphrase, p2p->cfg->passphrase_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001828 } else {
1829 res.freq = peer->oper_freq;
1830 if (p2p->ssid_len) {
1831 os_memcpy(res.ssid, p2p->ssid, p2p->ssid_len);
1832 res.ssid_len = p2p->ssid_len;
1833 }
1834 }
1835
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001836 p2p_channels_dump(p2p, "own channels", &p2p->channels);
1837 p2p_channels_dump(p2p, "peer channels", &peer->channels);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001838 p2p_channels_intersect(&p2p->channels, &peer->channels,
1839 &intersection);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001840 if (go) {
1841 p2p_channels_remove_freqs(&intersection, &p2p->no_go_freq);
1842 p2p_channels_dump(p2p, "intersection after no-GO removal",
1843 &intersection);
1844 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001845
1846 p2p_channels_to_freqs(&intersection, res.freq_list,
1847 P2P_MAX_CHANNELS);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001848
1849 res.peer_config_timeout = go ? peer->client_timeout : peer->go_timeout;
1850
1851 p2p_clear_timeout(p2p);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001852 p2p->ssid_set = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001853 peer->go_neg_req_sent = 0;
Dmitry Shmidt29333592017-01-09 12:27:11 -08001854 peer->flags &= ~P2P_DEV_PEER_WAITING_RESPONSE;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001855 peer->wps_method = WPS_NOT_READY;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001856 peer->oob_pw_id = 0;
Dmitry Shmidt413dde72014-04-11 10:23:22 -07001857 wpabuf_free(peer->go_neg_conf);
1858 peer->go_neg_conf = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001859
1860 p2p_set_state(p2p, P2P_PROVISIONING);
1861 p2p->cfg->go_neg_completed(p2p->cfg->cb_ctx, &res);
1862}
1863
1864
1865static void p2p_rx_p2p_action(struct p2p_data *p2p, const u8 *sa,
1866 const u8 *data, size_t len, int rx_freq)
1867{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001868 p2p_dbg(p2p, "RX P2P Public Action from " MACSTR, MAC2STR(sa));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001869 wpa_hexdump(MSG_MSGDUMP, "P2P: P2P Public Action contents", data, len);
1870
1871 if (len < 1)
1872 return;
1873
1874 switch (data[0]) {
1875 case P2P_GO_NEG_REQ:
1876 p2p_process_go_neg_req(p2p, sa, data + 1, len - 1, rx_freq);
1877 break;
1878 case P2P_GO_NEG_RESP:
1879 p2p_process_go_neg_resp(p2p, sa, data + 1, len - 1, rx_freq);
1880 break;
1881 case P2P_GO_NEG_CONF:
1882 p2p_process_go_neg_conf(p2p, sa, data + 1, len - 1);
1883 break;
1884 case P2P_INVITATION_REQ:
1885 p2p_process_invitation_req(p2p, sa, data + 1, len - 1,
1886 rx_freq);
1887 break;
1888 case P2P_INVITATION_RESP:
1889 p2p_process_invitation_resp(p2p, sa, data + 1, len - 1);
1890 break;
1891 case P2P_PROV_DISC_REQ:
1892 p2p_process_prov_disc_req(p2p, sa, data + 1, len - 1, rx_freq);
1893 break;
1894 case P2P_PROV_DISC_RESP:
1895 p2p_process_prov_disc_resp(p2p, sa, data + 1, len - 1);
1896 break;
1897 case P2P_DEV_DISC_REQ:
1898 p2p_process_dev_disc_req(p2p, sa, data + 1, len - 1, rx_freq);
1899 break;
1900 case P2P_DEV_DISC_RESP:
1901 p2p_process_dev_disc_resp(p2p, sa, data + 1, len - 1);
1902 break;
1903 default:
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001904 p2p_dbg(p2p, "Unsupported P2P Public Action frame type %d",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001905 data[0]);
1906 break;
1907 }
1908}
1909
1910
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001911static void p2p_rx_action_public(struct p2p_data *p2p, const u8 *da,
1912 const u8 *sa, const u8 *bssid, const u8 *data,
1913 size_t len, int freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001914{
1915 if (len < 1)
1916 return;
1917
1918 switch (data[0]) {
1919 case WLAN_PA_VENDOR_SPECIFIC:
1920 data++;
1921 len--;
Dmitry Shmidta38abf92014-03-06 13:38:44 -08001922 if (len < 4)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001923 return;
Dmitry Shmidta38abf92014-03-06 13:38:44 -08001924 if (WPA_GET_BE32(data) != P2P_IE_VENDOR_TYPE)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001925 return;
1926
Dmitry Shmidta38abf92014-03-06 13:38:44 -08001927 data += 4;
1928 len -= 4;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001929
Dmitry Shmidta38abf92014-03-06 13:38:44 -08001930 p2p_rx_p2p_action(p2p, sa, data, len, freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001931 break;
1932 case WLAN_PA_GAS_INITIAL_REQ:
1933 p2p_rx_gas_initial_req(p2p, sa, data + 1, len - 1, freq);
1934 break;
1935 case WLAN_PA_GAS_INITIAL_RESP:
1936 p2p_rx_gas_initial_resp(p2p, sa, data + 1, len - 1, freq);
1937 break;
1938 case WLAN_PA_GAS_COMEBACK_REQ:
1939 p2p_rx_gas_comeback_req(p2p, sa, data + 1, len - 1, freq);
1940 break;
1941 case WLAN_PA_GAS_COMEBACK_RESP:
1942 p2p_rx_gas_comeback_resp(p2p, sa, data + 1, len - 1, freq);
1943 break;
1944 }
1945}
1946
1947
1948void p2p_rx_action(struct p2p_data *p2p, const u8 *da, const u8 *sa,
1949 const u8 *bssid, u8 category,
1950 const u8 *data, size_t len, int freq)
1951{
1952 if (category == WLAN_ACTION_PUBLIC) {
1953 p2p_rx_action_public(p2p, da, sa, bssid, data, len, freq);
1954 return;
1955 }
1956
1957 if (category != WLAN_ACTION_VENDOR_SPECIFIC)
1958 return;
1959
1960 if (len < 4)
1961 return;
1962
Dmitry Shmidta38abf92014-03-06 13:38:44 -08001963 if (WPA_GET_BE32(data) != P2P_IE_VENDOR_TYPE)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001964 return;
Dmitry Shmidta38abf92014-03-06 13:38:44 -08001965 data += 4;
1966 len -= 4;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001967
1968 /* P2P action frame */
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001969 p2p_dbg(p2p, "RX P2P Action from " MACSTR, MAC2STR(sa));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001970 wpa_hexdump(MSG_MSGDUMP, "P2P: P2P Action contents", data, len);
1971
1972 if (len < 1)
1973 return;
1974 switch (data[0]) {
1975 case P2P_NOA:
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001976 p2p_dbg(p2p, "Received P2P Action - Notice of Absence");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001977 /* TODO */
1978 break;
1979 case P2P_PRESENCE_REQ:
1980 p2p_process_presence_req(p2p, da, sa, data + 1, len - 1, freq);
1981 break;
1982 case P2P_PRESENCE_RESP:
1983 p2p_process_presence_resp(p2p, da, sa, data + 1, len - 1);
1984 break;
1985 case P2P_GO_DISC_REQ:
1986 p2p_process_go_disc_req(p2p, da, sa, data + 1, len - 1, freq);
1987 break;
1988 default:
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001989 p2p_dbg(p2p, "Received P2P Action - unknown type %u", data[0]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001990 break;
1991 }
1992}
1993
1994
1995static void p2p_go_neg_start(void *eloop_ctx, void *timeout_ctx)
1996{
1997 struct p2p_data *p2p = eloop_ctx;
1998 if (p2p->go_neg_peer == NULL)
1999 return;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07002000 if (p2p->pending_listen_freq) {
2001 p2p_dbg(p2p, "Clear pending_listen_freq for p2p_go_neg_start");
2002 p2p->pending_listen_freq = 0;
2003 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002004 p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
2005 p2p->go_neg_peer->status = P2P_SC_SUCCESS;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07002006 /*
2007 * Set new timeout to make sure a previously set one does not expire
2008 * too quickly while waiting for the GO Negotiation to complete.
2009 */
2010 p2p_set_timeout(p2p, 0, 500000);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002011 p2p_connect_send(p2p, p2p->go_neg_peer);
2012}
2013
2014
2015static void p2p_invite_start(void *eloop_ctx, void *timeout_ctx)
2016{
2017 struct p2p_data *p2p = eloop_ctx;
2018 if (p2p->invite_peer == NULL)
2019 return;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07002020 if (p2p->pending_listen_freq) {
2021 p2p_dbg(p2p, "Clear pending_listen_freq for p2p_invite_start");
2022 p2p->pending_listen_freq = 0;
2023 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002024 p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002025 p2p_invite_send(p2p, p2p->invite_peer, p2p->invite_go_dev_addr,
2026 p2p->invite_dev_pw_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002027}
2028
2029
2030static void p2p_add_dev_from_probe_req(struct p2p_data *p2p, const u8 *addr,
2031 const u8 *ie, size_t ie_len)
2032{
2033 struct p2p_message msg;
2034 struct p2p_device *dev;
2035
2036 os_memset(&msg, 0, sizeof(msg));
2037 if (p2p_parse_ies(ie, ie_len, &msg) < 0 || msg.p2p_attributes == NULL)
2038 {
2039 p2p_parse_free(&msg);
2040 return; /* not a P2P probe */
2041 }
2042
2043 if (msg.ssid == NULL || msg.ssid[1] != P2P_WILDCARD_SSID_LEN ||
2044 os_memcmp(msg.ssid + 2, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN)
2045 != 0) {
2046 /* The Probe Request is not part of P2P Device Discovery. It is
2047 * not known whether the source address of the frame is the P2P
2048 * Device Address or P2P Interface Address. Do not add a new
2049 * peer entry based on this frames.
2050 */
2051 p2p_parse_free(&msg);
2052 return;
2053 }
2054
2055 dev = p2p_get_device(p2p, addr);
2056 if (dev) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002057 if (msg.listen_channel) {
2058 int freq;
2059
2060 if (dev->country[0] == 0)
2061 os_memcpy(dev->country, msg.listen_channel, 3);
2062
2063 freq = p2p_channel_to_freq(msg.listen_channel[3],
2064 msg.listen_channel[4]);
2065
2066 if (freq > 0 && dev->listen_freq != freq) {
2067 p2p_dbg(p2p,
2068 "Updated peer " MACSTR " Listen channel (Probe Request): %d -> %d MHz",
2069 MAC2STR(addr), dev->listen_freq, freq);
2070 dev->listen_freq = freq;
2071 }
2072 }
2073
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002074 os_get_reltime(&dev->last_seen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002075 p2p_parse_free(&msg);
2076 return; /* already known */
2077 }
2078
2079 dev = p2p_create_device(p2p, addr);
2080 if (dev == NULL) {
2081 p2p_parse_free(&msg);
2082 return;
2083 }
2084
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002085 os_get_reltime(&dev->last_seen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002086 dev->flags |= P2P_DEV_PROBE_REQ_ONLY;
2087
2088 if (msg.listen_channel) {
2089 os_memcpy(dev->country, msg.listen_channel, 3);
Dmitry Shmidt4b060592013-04-29 16:42:49 -07002090 dev->listen_freq = p2p_channel_to_freq(msg.listen_channel[3],
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002091 msg.listen_channel[4]);
2092 }
2093
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002094 p2p_copy_wps_info(p2p, dev, 1, &msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002095
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002096 if (msg.wfd_subelems) {
2097 wpabuf_free(dev->info.wfd_subelems);
2098 dev->info.wfd_subelems = wpabuf_dup(msg.wfd_subelems);
2099 }
2100
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002101 p2p_parse_free(&msg);
2102
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002103 p2p_dbg(p2p, "Created device entry based on Probe Req: " MACSTR
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002104 " dev_capab=0x%x group_capab=0x%x name='%s' listen_freq=%d",
2105 MAC2STR(dev->info.p2p_device_addr), dev->info.dev_capab,
2106 dev->info.group_capab, dev->info.device_name,
2107 dev->listen_freq);
2108}
2109
2110
2111struct p2p_device * p2p_add_dev_from_go_neg_req(struct p2p_data *p2p,
2112 const u8 *addr,
2113 struct p2p_message *msg)
2114{
2115 struct p2p_device *dev;
2116
2117 dev = p2p_get_device(p2p, addr);
2118 if (dev) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002119 os_get_reltime(&dev->last_seen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002120 return dev; /* already known */
2121 }
2122
2123 dev = p2p_create_device(p2p, addr);
2124 if (dev == NULL)
2125 return NULL;
2126
2127 p2p_add_dev_info(p2p, addr, dev, msg);
2128
2129 return dev;
2130}
2131
2132
2133static int dev_type_match(const u8 *dev_type, const u8 *req_dev_type)
2134{
2135 if (os_memcmp(dev_type, req_dev_type, WPS_DEV_TYPE_LEN) == 0)
2136 return 1;
2137 if (os_memcmp(dev_type, req_dev_type, 2) == 0 &&
2138 WPA_GET_BE32(&req_dev_type[2]) == 0 &&
2139 WPA_GET_BE16(&req_dev_type[6]) == 0)
2140 return 1; /* Category match with wildcard OUI/sub-category */
2141 return 0;
2142}
2143
2144
2145int dev_type_list_match(const u8 *dev_type, const u8 *req_dev_type[],
2146 size_t num_req_dev_type)
2147{
2148 size_t i;
2149 for (i = 0; i < num_req_dev_type; i++) {
2150 if (dev_type_match(dev_type, req_dev_type[i]))
2151 return 1;
2152 }
2153 return 0;
2154}
2155
2156
2157/**
2158 * p2p_match_dev_type - Match local device type with requested type
2159 * @p2p: P2P module context from p2p_init()
2160 * @wps: WPS TLVs from Probe Request frame (concatenated WPS IEs)
2161 * Returns: 1 on match, 0 on mismatch
2162 *
2163 * This function can be used to match the Requested Device Type attribute in
2164 * WPS IE with the local device types for deciding whether to reply to a Probe
2165 * Request frame.
2166 */
2167int p2p_match_dev_type(struct p2p_data *p2p, struct wpabuf *wps)
2168{
2169 struct wps_parse_attr attr;
2170 size_t i;
2171
2172 if (wps_parse_msg(wps, &attr))
2173 return 1; /* assume no Requested Device Type attributes */
2174
2175 if (attr.num_req_dev_type == 0)
2176 return 1; /* no Requested Device Type attributes -> match */
2177
2178 if (dev_type_list_match(p2p->cfg->pri_dev_type, attr.req_dev_type,
2179 attr.num_req_dev_type))
2180 return 1; /* Own Primary Device Type matches */
2181
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002182 for (i = 0; i < p2p->cfg->num_sec_dev_types; i++) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002183 if (dev_type_list_match(p2p->cfg->sec_dev_type[i],
2184 attr.req_dev_type,
2185 attr.num_req_dev_type))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002186 return 1; /* Own Secondary Device Type matches */
2187 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002188
2189 /* No matching device type found */
2190 return 0;
2191}
2192
2193
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002194struct wpabuf * p2p_build_probe_resp_ies(struct p2p_data *p2p,
2195 const u8 *query_hash,
2196 u8 query_count)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002197{
2198 struct wpabuf *buf;
2199 u8 *len;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002200 int pw_id = -1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002201 size_t extra = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002202
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002203#ifdef CONFIG_WIFI_DISPLAY
2204 if (p2p->wfd_ie_probe_resp)
2205 extra = wpabuf_len(p2p->wfd_ie_probe_resp);
2206#endif /* CONFIG_WIFI_DISPLAY */
2207
Dmitry Shmidt2e67f062014-07-16 09:55:28 -07002208 if (p2p->vendor_elem && p2p->vendor_elem[VENDOR_ELEM_PROBE_RESP_P2P])
2209 extra += wpabuf_len(p2p->vendor_elem[VENDOR_ELEM_PROBE_RESP_P2P]);
2210
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002211 if (query_count)
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002212 extra += MAX_SVC_ADV_IE_LEN;
2213
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002214 buf = wpabuf_alloc(1000 + extra);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002215 if (buf == NULL)
2216 return NULL;
2217
Dmitry Shmidt04949592012-07-19 12:16:46 -07002218 if (p2p->go_neg_peer) {
2219 /* Advertise immediate availability of WPS credential */
2220 pw_id = p2p_wps_method_pw_id(p2p->go_neg_peer->wps_method);
2221 }
2222
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002223 if (p2p_build_wps_ie(p2p, buf, pw_id, 1) < 0) {
2224 p2p_dbg(p2p, "Failed to build WPS IE for Probe Response");
2225 wpabuf_free(buf);
2226 return NULL;
2227 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002228
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002229#ifdef CONFIG_WIFI_DISPLAY
2230 if (p2p->wfd_ie_probe_resp)
2231 wpabuf_put_buf(buf, p2p->wfd_ie_probe_resp);
2232#endif /* CONFIG_WIFI_DISPLAY */
2233
Dmitry Shmidt2e67f062014-07-16 09:55:28 -07002234 if (p2p->vendor_elem && p2p->vendor_elem[VENDOR_ELEM_PROBE_RESP_P2P])
2235 wpabuf_put_buf(buf,
2236 p2p->vendor_elem[VENDOR_ELEM_PROBE_RESP_P2P]);
2237
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002238 /* P2P IE */
2239 len = p2p_buf_add_ie_hdr(buf);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002240 p2p_buf_add_capability(buf, p2p->dev_capab &
2241 ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002242 if (p2p->ext_listen_interval)
2243 p2p_buf_add_ext_listen_timing(buf, p2p->ext_listen_period,
2244 p2p->ext_listen_interval);
2245 p2p_buf_add_device_info(buf, p2p, NULL);
2246 p2p_buf_update_ie_hdr(buf, len);
2247
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002248 if (query_count) {
2249 p2p_buf_add_service_instance(buf, p2p, query_count, query_hash,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002250 p2p->p2ps_adv_list);
2251 }
2252
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002253 return buf;
2254}
2255
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07002256static int p2p_build_probe_resp_buf(struct p2p_data *p2p, struct wpabuf *buf,
2257 struct wpabuf *ies,
2258 const u8 *addr, int rx_freq)
2259{
2260 struct ieee80211_mgmt *resp;
2261 u8 channel, op_class;
2262
2263 resp = wpabuf_put(buf, offsetof(struct ieee80211_mgmt,
2264 u.probe_resp.variable));
2265
2266 resp->frame_control = host_to_le16((WLAN_FC_TYPE_MGMT << 2) |
2267 (WLAN_FC_STYPE_PROBE_RESP << 4));
2268 os_memcpy(resp->da, addr, ETH_ALEN);
2269 os_memcpy(resp->sa, p2p->cfg->dev_addr, ETH_ALEN);
2270 os_memcpy(resp->bssid, p2p->cfg->dev_addr, ETH_ALEN);
2271 resp->u.probe_resp.beacon_int = host_to_le16(100);
2272 /* hardware or low-level driver will setup seq_ctrl and timestamp */
2273 resp->u.probe_resp.capab_info =
2274 host_to_le16(WLAN_CAPABILITY_SHORT_PREAMBLE |
2275 WLAN_CAPABILITY_PRIVACY |
2276 WLAN_CAPABILITY_SHORT_SLOT_TIME);
2277
2278 wpabuf_put_u8(buf, WLAN_EID_SSID);
2279 wpabuf_put_u8(buf, P2P_WILDCARD_SSID_LEN);
2280 wpabuf_put_data(buf, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN);
2281
2282 wpabuf_put_u8(buf, WLAN_EID_SUPP_RATES);
2283 wpabuf_put_u8(buf, 8);
2284 wpabuf_put_u8(buf, (60 / 5) | 0x80);
2285 wpabuf_put_u8(buf, 90 / 5);
2286 wpabuf_put_u8(buf, (120 / 5) | 0x80);
2287 wpabuf_put_u8(buf, 180 / 5);
2288 wpabuf_put_u8(buf, (240 / 5) | 0x80);
2289 wpabuf_put_u8(buf, 360 / 5);
2290 wpabuf_put_u8(buf, 480 / 5);
2291 wpabuf_put_u8(buf, 540 / 5);
2292
2293 if (!rx_freq) {
2294 channel = p2p->cfg->channel;
2295 } else if (p2p_freq_to_channel(rx_freq, &op_class, &channel)) {
2296 p2p_err(p2p, "Failed to convert freq to channel");
2297 return -1;
2298 }
2299
2300 wpabuf_put_u8(buf, WLAN_EID_DS_PARAMS);
2301 wpabuf_put_u8(buf, 1);
2302 wpabuf_put_u8(buf, channel);
2303
2304 wpabuf_put_buf(buf, ies);
2305
2306 return 0;
2307}
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002308
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002309static int p2p_service_find_asp(struct p2p_data *p2p, const u8 *hash)
2310{
2311 struct p2ps_advertisement *adv_data;
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002312 int any_wfa;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002313
2314 p2p_dbg(p2p, "ASP find - ASP list: %p", p2p->p2ps_adv_list);
2315
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002316 /* Wildcard org.wi-fi.wfds matches any WFA spec defined service */
2317 any_wfa = os_memcmp(hash, p2p->wild_card_hash, P2PS_HASH_LEN) == 0;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002318
2319 adv_data = p2p->p2ps_adv_list;
2320 while (adv_data) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002321 if (os_memcmp(hash, adv_data->hash, P2PS_HASH_LEN) == 0)
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002322 return 1; /* exact hash match */
2323 if (any_wfa &&
2324 os_strncmp(adv_data->svc_name, P2PS_WILD_HASH_STR,
2325 os_strlen(P2PS_WILD_HASH_STR)) == 0)
2326 return 1; /* WFA service match */
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002327 adv_data = adv_data->next;
2328 }
2329
2330 return 0;
2331}
2332
2333
Dmitry Shmidt04949592012-07-19 12:16:46 -07002334static enum p2p_probe_req_status
2335p2p_reply_probe(struct p2p_data *p2p, const u8 *addr, const u8 *dst,
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002336 const u8 *bssid, const u8 *ie, size_t ie_len,
2337 unsigned int rx_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002338{
2339 struct ieee802_11_elems elems;
2340 struct wpabuf *buf;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002341 struct p2p_message msg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002342 struct wpabuf *ies;
2343
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002344 if (ieee802_11_parse_elems((u8 *) ie, ie_len, &elems, 0) ==
2345 ParseFailed) {
2346 /* Ignore invalid Probe Request frames */
Dmitry Shmidtec58b162014-02-19 12:44:18 -08002347 p2p_dbg(p2p, "Could not parse Probe Request frame - ignore it");
Dmitry Shmidt04949592012-07-19 12:16:46 -07002348 return P2P_PREQ_MALFORMED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002349 }
2350
2351 if (elems.p2p == NULL) {
2352 /* not a P2P probe - ignore it */
Dmitry Shmidtec58b162014-02-19 12:44:18 -08002353 p2p_dbg(p2p, "Not a P2P probe - ignore it");
Dmitry Shmidt04949592012-07-19 12:16:46 -07002354 return P2P_PREQ_NOT_P2P;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002355 }
2356
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002357 if (dst && !is_broadcast_ether_addr(dst) &&
2358 os_memcmp(dst, p2p->cfg->dev_addr, ETH_ALEN) != 0) {
2359 /* Not sent to the broadcast address or our P2P Device Address
2360 */
Dmitry Shmidtec58b162014-02-19 12:44:18 -08002361 p2p_dbg(p2p, "Probe Req DA " MACSTR " not ours - ignore it",
2362 MAC2STR(dst));
Dmitry Shmidt04949592012-07-19 12:16:46 -07002363 return P2P_PREQ_NOT_PROCESSED;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002364 }
2365
2366 if (bssid && !is_broadcast_ether_addr(bssid)) {
2367 /* Not sent to the Wildcard BSSID */
Dmitry Shmidtec58b162014-02-19 12:44:18 -08002368 p2p_dbg(p2p, "Probe Req BSSID " MACSTR " not wildcard - ignore it",
2369 MAC2STR(bssid));
Dmitry Shmidt04949592012-07-19 12:16:46 -07002370 return P2P_PREQ_NOT_PROCESSED;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002371 }
2372
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002373 if (elems.ssid == NULL || elems.ssid_len != P2P_WILDCARD_SSID_LEN ||
2374 os_memcmp(elems.ssid, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN) !=
2375 0) {
2376 /* not using P2P Wildcard SSID - ignore */
Dmitry Shmidtec58b162014-02-19 12:44:18 -08002377 p2p_dbg(p2p, "Probe Req not using P2P Wildcard SSID - ignore it");
Dmitry Shmidt04949592012-07-19 12:16:46 -07002378 return P2P_PREQ_NOT_PROCESSED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002379 }
2380
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002381 if (supp_rates_11b_only(&elems)) {
2382 /* Indicates support for 11b rates only */
Dmitry Shmidtec58b162014-02-19 12:44:18 -08002383 p2p_dbg(p2p, "Probe Req with 11b rates only supported - ignore it");
Dmitry Shmidt04949592012-07-19 12:16:46 -07002384 return P2P_PREQ_NOT_P2P;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002385 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002386
2387 os_memset(&msg, 0, sizeof(msg));
2388 if (p2p_parse_ies(ie, ie_len, &msg) < 0) {
2389 /* Could not parse P2P attributes */
Dmitry Shmidtec58b162014-02-19 12:44:18 -08002390 p2p_dbg(p2p, "Could not parse P2P attributes in Probe Req - ignore it");
Dmitry Shmidt04949592012-07-19 12:16:46 -07002391 return P2P_PREQ_NOT_P2P;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002392 }
2393
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002394 if (msg.service_hash && msg.service_hash_count) {
2395 const u8 *hash = msg.service_hash;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002396 u8 i;
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002397 int p2ps_svc_found = 0;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002398
Dmitry Shmidt41712582015-06-29 11:02:15 -07002399 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",
2400 p2p->in_listen, p2p->drv_in_listen, rx_freq,
2401 p2p->cfg->channel, p2p->pending_listen_freq);
2402
2403 if (!p2p->in_listen && !p2p->drv_in_listen &&
2404 p2p->pending_listen_freq && rx_freq &&
2405 rx_freq != p2p->pending_listen_freq) {
2406 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",
2407 rx_freq, p2p->pending_listen_freq);
2408 p2p_parse_free(&msg);
2409 return P2P_PREQ_NOT_LISTEN;
2410 }
2411
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002412 for (i = 0; i < msg.service_hash_count; i++) {
2413 if (p2p_service_find_asp(p2p, hash)) {
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002414 p2p_dbg(p2p, "Service Hash match found: "
2415 MACSTR, MAC2STR(hash));
2416 p2ps_svc_found = 1;
2417 break;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002418 }
2419 hash += P2PS_HASH_LEN;
2420 }
2421
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002422 /* Probed hash unknown */
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002423 if (!p2ps_svc_found) {
2424 p2p_dbg(p2p, "No Service Hash match found");
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002425 p2p_parse_free(&msg);
2426 return P2P_PREQ_NOT_PROCESSED;
2427 }
2428 } else {
2429 /* This is not a P2PS Probe Request */
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002430 p2p_dbg(p2p, "No P2PS Hash in Probe Request");
2431
2432 if (!p2p->in_listen || !p2p->drv_in_listen) {
2433 /* not in Listen state - ignore Probe Request */
2434 p2p_dbg(p2p, "Not in Listen state (in_listen=%d drv_in_listen=%d) - ignore Probe Request",
2435 p2p->in_listen, p2p->drv_in_listen);
2436 p2p_parse_free(&msg);
2437 return P2P_PREQ_NOT_LISTEN;
2438 }
2439 }
2440
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002441 if (msg.device_id &&
Dmitry Shmidt04949592012-07-19 12:16:46 -07002442 os_memcmp(msg.device_id, p2p->cfg->dev_addr, ETH_ALEN) != 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002443 /* Device ID did not match */
Dmitry Shmidtec58b162014-02-19 12:44:18 -08002444 p2p_dbg(p2p, "Probe Req requested Device ID " MACSTR " did not match - ignore it",
2445 MAC2STR(msg.device_id));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002446 p2p_parse_free(&msg);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002447 return P2P_PREQ_NOT_PROCESSED;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002448 }
2449
2450 /* Check Requested Device Type match */
2451 if (msg.wps_attributes &&
2452 !p2p_match_dev_type(p2p, msg.wps_attributes)) {
2453 /* No match with Requested Device Type */
Hai Shalom021b0b52019-04-10 11:17:58 -07002454 p2p_dbg(p2p, "Probe Req requested Device Type did not match - ignore it");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002455 p2p_parse_free(&msg);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002456 return P2P_PREQ_NOT_PROCESSED;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002457 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002458
Dmitry Shmidt04949592012-07-19 12:16:46 -07002459 if (!p2p->cfg->send_probe_resp) {
2460 /* Response generated elsewhere */
Dmitry Shmidtec58b162014-02-19 12:44:18 -08002461 p2p_dbg(p2p, "Probe Resp generated elsewhere - do not generate additional response");
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002462 p2p_parse_free(&msg);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002463 return P2P_PREQ_NOT_PROCESSED;
2464 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002465
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002466 p2p_dbg(p2p, "Reply to P2P Probe Request in Listen state");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002467
2468 /*
2469 * We do not really have a specific BSS that this frame is advertising,
2470 * so build a frame that has some information in valid format. This is
2471 * really only used for discovery purposes, not to learn exact BSS
2472 * parameters.
2473 */
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002474 ies = p2p_build_probe_resp_ies(p2p, msg.service_hash,
2475 msg.service_hash_count);
2476 p2p_parse_free(&msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002477 if (ies == NULL)
Dmitry Shmidt04949592012-07-19 12:16:46 -07002478 return P2P_PREQ_NOT_PROCESSED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002479
2480 buf = wpabuf_alloc(200 + wpabuf_len(ies));
2481 if (buf == NULL) {
2482 wpabuf_free(ies);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002483 return P2P_PREQ_NOT_PROCESSED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002484 }
2485
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07002486 if (p2p_build_probe_resp_buf(p2p, buf, ies, addr, rx_freq)) {
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002487 wpabuf_free(ies);
2488 wpabuf_free(buf);
2489 return P2P_PREQ_NOT_PROCESSED;
2490 }
2491
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002492 wpabuf_free(ies);
2493
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002494 p2p->cfg->send_probe_resp(p2p->cfg->cb_ctx, buf, rx_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002495
2496 wpabuf_free(buf);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002497
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002498 return P2P_PREQ_PROCESSED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002499}
2500
2501
Dmitry Shmidt04949592012-07-19 12:16:46 -07002502enum p2p_probe_req_status
2503p2p_probe_req_rx(struct p2p_data *p2p, const u8 *addr, const u8 *dst,
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002504 const u8 *bssid, const u8 *ie, size_t ie_len,
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07002505 unsigned int rx_freq, int p2p_lo_started)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002506{
Dmitry Shmidt04949592012-07-19 12:16:46 -07002507 enum p2p_probe_req_status res;
2508
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002509 p2p_add_dev_from_probe_req(p2p, addr, ie, ie_len);
2510
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07002511 if (p2p_lo_started) {
2512 p2p_dbg(p2p,
2513 "Probe Response is offloaded, do not reply Probe Request");
2514 return P2P_PREQ_PROCESSED;
2515 }
2516
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002517 res = p2p_reply_probe(p2p, addr, dst, bssid, ie, ie_len, rx_freq);
2518 if (res != P2P_PREQ_PROCESSED && res != P2P_PREQ_NOT_PROCESSED)
2519 return res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002520
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002521 /*
2522 * Activate a pending GO Negotiation/Invite flow if a received Probe
2523 * Request frame is from an expected peer. Some devices may share the
2524 * same address for P2P and non-P2P STA running simultaneously. The
2525 * P2P_PREQ_PROCESSED and P2P_PREQ_NOT_PROCESSED p2p_reply_probe()
2526 * return values verified above ensure we are handling a Probe Request
2527 * frame from a P2P peer.
2528 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002529 if ((p2p->state == P2P_CONNECT || p2p->state == P2P_CONNECT_LISTEN) &&
2530 p2p->go_neg_peer &&
2531 os_memcmp(addr, p2p->go_neg_peer->info.p2p_device_addr, ETH_ALEN)
Dmitry Shmidt9cdf1b92013-02-27 12:58:50 -08002532 == 0 &&
2533 !(p2p->go_neg_peer->flags & P2P_DEV_WAIT_GO_NEG_CONFIRM)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002534 /* Received a Probe Request from GO Negotiation peer */
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002535 p2p_dbg(p2p, "Found GO Negotiation peer - try to start GO negotiation from timeout");
Dmitry Shmidt9cdf1b92013-02-27 12:58:50 -08002536 eloop_cancel_timeout(p2p_go_neg_start, p2p, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002537 eloop_register_timeout(0, 0, p2p_go_neg_start, p2p, NULL);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002538 return res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002539 }
2540
2541 if ((p2p->state == P2P_INVITE || p2p->state == P2P_INVITE_LISTEN) &&
2542 p2p->invite_peer &&
Dmitry Shmidt3c479372014-02-04 10:50:36 -08002543 (p2p->invite_peer->flags & P2P_DEV_WAIT_INV_REQ_ACK) &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002544 os_memcmp(addr, p2p->invite_peer->info.p2p_device_addr, ETH_ALEN)
2545 == 0) {
2546 /* Received a Probe Request from Invite peer */
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002547 p2p_dbg(p2p, "Found Invite peer - try to start Invite from timeout");
Dmitry Shmidt7f93d6f2014-02-21 11:22:49 -08002548 eloop_cancel_timeout(p2p_invite_start, p2p, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002549 eloop_register_timeout(0, 0, p2p_invite_start, p2p, NULL);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002550 return res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002551 }
2552
Dmitry Shmidt04949592012-07-19 12:16:46 -07002553 return res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002554}
2555
2556
2557static int p2p_assoc_req_ie_wlan_ap(struct p2p_data *p2p, const u8 *bssid,
2558 u8 *buf, size_t len, struct wpabuf *p2p_ie)
2559{
2560 struct wpabuf *tmp;
2561 u8 *lpos;
2562 size_t tmplen;
2563 int res;
2564 u8 group_capab;
Dmitry Shmidt05df46a2015-05-19 11:02:01 -07002565 struct p2p_message msg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002566
2567 if (p2p_ie == NULL)
2568 return 0; /* WLAN AP is not a P2P manager */
2569
Dmitry Shmidt05df46a2015-05-19 11:02:01 -07002570 os_memset(&msg, 0, sizeof(msg));
2571 if (p2p_parse_p2p_ie(p2p_ie, &msg) < 0)
2572 return 0;
2573
2574 p2p_dbg(p2p, "BSS P2P manageability %s",
2575 msg.manageability ? "enabled" : "disabled");
2576
2577 if (!msg.manageability)
2578 return 0;
2579
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002580 /*
2581 * (Re)Association Request - P2P IE
2582 * P2P Capability attribute (shall be present)
2583 * P2P Interface attribute (present if concurrent device and
2584 * P2P Management is enabled)
2585 */
2586 tmp = wpabuf_alloc(200);
2587 if (tmp == NULL)
2588 return -1;
2589
2590 lpos = p2p_buf_add_ie_hdr(tmp);
2591 group_capab = 0;
2592 if (p2p->num_groups > 0) {
2593 group_capab |= P2P_GROUP_CAPAB_GROUP_OWNER;
2594 if ((p2p->dev_capab & P2P_DEV_CAPAB_CONCURRENT_OPER) &&
2595 (p2p->dev_capab & P2P_DEV_CAPAB_INFRA_MANAGED) &&
2596 p2p->cross_connect)
2597 group_capab |= P2P_GROUP_CAPAB_CROSS_CONN;
2598 }
2599 p2p_buf_add_capability(tmp, p2p->dev_capab, group_capab);
2600 if ((p2p->dev_capab & P2P_DEV_CAPAB_CONCURRENT_OPER) &&
2601 (p2p->dev_capab & P2P_DEV_CAPAB_INFRA_MANAGED))
2602 p2p_buf_add_p2p_interface(tmp, p2p);
2603 p2p_buf_update_ie_hdr(tmp, lpos);
2604
2605 tmplen = wpabuf_len(tmp);
2606 if (tmplen > len)
2607 res = -1;
2608 else {
2609 os_memcpy(buf, wpabuf_head(tmp), tmplen);
2610 res = tmplen;
2611 }
2612 wpabuf_free(tmp);
2613
2614 return res;
2615}
2616
2617
2618int p2p_assoc_req_ie(struct p2p_data *p2p, const u8 *bssid, u8 *buf,
2619 size_t len, int p2p_group, struct wpabuf *p2p_ie)
2620{
2621 struct wpabuf *tmp;
2622 u8 *lpos;
2623 struct p2p_device *peer;
2624 size_t tmplen;
2625 int res;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002626 size_t extra = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002627
2628 if (!p2p_group)
2629 return p2p_assoc_req_ie_wlan_ap(p2p, bssid, buf, len, p2p_ie);
2630
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002631#ifdef CONFIG_WIFI_DISPLAY
2632 if (p2p->wfd_ie_assoc_req)
2633 extra = wpabuf_len(p2p->wfd_ie_assoc_req);
2634#endif /* CONFIG_WIFI_DISPLAY */
2635
Dmitry Shmidt2e67f062014-07-16 09:55:28 -07002636 if (p2p->vendor_elem && p2p->vendor_elem[VENDOR_ELEM_P2P_ASSOC_REQ])
2637 extra += wpabuf_len(p2p->vendor_elem[VENDOR_ELEM_P2P_ASSOC_REQ]);
2638
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002639 /*
2640 * (Re)Association Request - P2P IE
2641 * P2P Capability attribute (shall be present)
2642 * Extended Listen Timing (may be present)
2643 * P2P Device Info attribute (shall be present)
2644 */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002645 tmp = wpabuf_alloc(200 + extra);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002646 if (tmp == NULL)
2647 return -1;
2648
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002649#ifdef CONFIG_WIFI_DISPLAY
2650 if (p2p->wfd_ie_assoc_req)
2651 wpabuf_put_buf(tmp, p2p->wfd_ie_assoc_req);
2652#endif /* CONFIG_WIFI_DISPLAY */
2653
Dmitry Shmidt2e67f062014-07-16 09:55:28 -07002654 if (p2p->vendor_elem && p2p->vendor_elem[VENDOR_ELEM_P2P_ASSOC_REQ])
2655 wpabuf_put_buf(tmp,
2656 p2p->vendor_elem[VENDOR_ELEM_P2P_ASSOC_REQ]);
2657
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002658 peer = bssid ? p2p_get_device(p2p, bssid) : NULL;
2659
2660 lpos = p2p_buf_add_ie_hdr(tmp);
2661 p2p_buf_add_capability(tmp, p2p->dev_capab, 0);
2662 if (p2p->ext_listen_interval)
2663 p2p_buf_add_ext_listen_timing(tmp, p2p->ext_listen_period,
2664 p2p->ext_listen_interval);
2665 p2p_buf_add_device_info(tmp, p2p, peer);
2666 p2p_buf_update_ie_hdr(tmp, lpos);
2667
2668 tmplen = wpabuf_len(tmp);
2669 if (tmplen > len)
2670 res = -1;
2671 else {
2672 os_memcpy(buf, wpabuf_head(tmp), tmplen);
2673 res = tmplen;
2674 }
2675 wpabuf_free(tmp);
2676
2677 return res;
2678}
2679
2680
2681int p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf, char *end)
2682{
2683 struct wpabuf *p2p_ie;
2684 int ret;
2685
2686 p2p_ie = ieee802_11_vendor_ie_concat(ies, ies_len, P2P_IE_VENDOR_TYPE);
2687 if (p2p_ie == NULL)
2688 return 0;
2689
2690 ret = p2p_attr_text(p2p_ie, buf, end);
2691 wpabuf_free(p2p_ie);
2692 return ret;
2693}
2694
2695
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002696struct p2ps_advertisement *
2697p2p_service_p2ps_id(struct p2p_data *p2p, u32 adv_id)
2698{
2699 struct p2ps_advertisement *adv_data;
2700
2701 if (!p2p)
2702 return NULL;
2703
2704 adv_data = p2p->p2ps_adv_list;
2705 while (adv_data) {
2706 if (adv_data->id == adv_id)
2707 return adv_data;
2708 adv_data = adv_data->next;
2709 }
2710
2711 return NULL;
2712}
2713
2714
2715int p2p_service_del_asp(struct p2p_data *p2p, u32 adv_id)
2716{
2717 struct p2ps_advertisement *adv_data;
2718 struct p2ps_advertisement **prior;
2719
2720 if (!p2p)
2721 return -1;
2722
2723 adv_data = p2p->p2ps_adv_list;
2724 prior = &p2p->p2ps_adv_list;
2725 while (adv_data) {
2726 if (adv_data->id == adv_id) {
2727 p2p_dbg(p2p, "Delete ASP adv_id=0x%x", adv_id);
2728 *prior = adv_data->next;
2729 os_free(adv_data);
2730 return 0;
2731 }
2732 prior = &adv_data->next;
2733 adv_data = adv_data->next;
2734 }
2735
2736 return -1;
2737}
2738
2739
2740int p2p_service_add_asp(struct p2p_data *p2p, int auto_accept, u32 adv_id,
2741 const char *adv_str, u8 svc_state, u16 config_methods,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002742 const char *svc_info, const u8 *cpt_priority)
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002743{
2744 struct p2ps_advertisement *adv_data, *tmp, **prev;
2745 u8 buf[P2PS_HASH_LEN];
2746 size_t adv_data_len, adv_len, info_len = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002747 int i;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002748
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002749 if (!p2p || !adv_str || !adv_str[0] || !cpt_priority)
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002750 return -1;
2751
2752 if (!(config_methods & p2p->cfg->config_methods)) {
2753 p2p_dbg(p2p, "Config methods not supported svc: 0x%x dev: 0x%x",
2754 config_methods, p2p->cfg->config_methods);
2755 return -1;
2756 }
2757
2758 if (!p2ps_gen_hash(p2p, adv_str, buf))
2759 return -1;
2760
2761 if (svc_info)
2762 info_len = os_strlen(svc_info);
2763 adv_len = os_strlen(adv_str);
2764 adv_data_len = sizeof(struct p2ps_advertisement) + adv_len + 1 +
2765 info_len + 1;
2766
2767 adv_data = os_zalloc(adv_data_len);
2768 if (!adv_data)
2769 return -1;
2770
2771 os_memcpy(adv_data->hash, buf, P2PS_HASH_LEN);
2772 adv_data->id = adv_id;
2773 adv_data->state = svc_state;
2774 adv_data->config_methods = config_methods & p2p->cfg->config_methods;
2775 adv_data->auto_accept = (u8) auto_accept;
2776 os_memcpy(adv_data->svc_name, adv_str, adv_len);
2777
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002778 for (i = 0; cpt_priority[i] && i < P2PS_FEATURE_CAPAB_CPT_MAX; i++) {
2779 adv_data->cpt_priority[i] = cpt_priority[i];
2780 adv_data->cpt_mask |= cpt_priority[i];
2781 }
2782
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002783 if (svc_info && info_len) {
2784 adv_data->svc_info = &adv_data->svc_name[adv_len + 1];
2785 os_memcpy(adv_data->svc_info, svc_info, info_len);
2786 }
2787
2788 /*
2789 * Group Advertisements by service string. They do not need to be
2790 * sorted, but groups allow easier Probe Response instance grouping
2791 */
2792 tmp = p2p->p2ps_adv_list;
2793 prev = &p2p->p2ps_adv_list;
2794 while (tmp) {
2795 if (tmp->id == adv_data->id) {
2796 if (os_strcmp(tmp->svc_name, adv_data->svc_name) != 0) {
2797 os_free(adv_data);
2798 return -1;
2799 }
2800 adv_data->next = tmp->next;
2801 *prev = adv_data;
2802 os_free(tmp);
2803 goto inserted;
2804 } else {
2805 if (os_strcmp(tmp->svc_name, adv_data->svc_name) == 0) {
2806 adv_data->next = tmp->next;
2807 tmp->next = adv_data;
2808 goto inserted;
2809 }
2810 }
2811 prev = &tmp->next;
2812 tmp = tmp->next;
2813 }
2814
2815 /* No svc_name match found */
2816 adv_data->next = p2p->p2ps_adv_list;
2817 p2p->p2ps_adv_list = adv_data;
2818
2819inserted:
2820 p2p_dbg(p2p,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002821 "Added ASP advertisement adv_id=0x%x config_methods=0x%x svc_state=0x%x adv_str='%s' cpt_mask=0x%x",
2822 adv_id, adv_data->config_methods, svc_state, adv_str,
2823 adv_data->cpt_mask);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002824
2825 return 0;
2826}
2827
2828
Dmitry Shmidtdda10c22015-03-24 16:05:01 -07002829void p2p_service_flush_asp(struct p2p_data *p2p)
2830{
2831 struct p2ps_advertisement *adv, *prev;
2832
2833 if (!p2p)
2834 return;
2835
2836 adv = p2p->p2ps_adv_list;
2837 while (adv) {
2838 prev = adv;
2839 adv = adv->next;
2840 os_free(prev);
2841 }
2842
2843 p2p->p2ps_adv_list = NULL;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002844 p2ps_prov_free(p2p);
Dmitry Shmidtdda10c22015-03-24 16:05:01 -07002845 p2p_dbg(p2p, "All ASP advertisements flushed");
2846}
2847
2848
Dmitry Shmidt04949592012-07-19 12:16:46 -07002849int p2p_parse_dev_addr_in_p2p_ie(struct wpabuf *p2p_ie, u8 *dev_addr)
2850{
2851 struct p2p_message msg;
2852
2853 os_memset(&msg, 0, sizeof(msg));
2854 if (p2p_parse_p2p_ie(p2p_ie, &msg))
2855 return -1;
2856
2857 if (msg.p2p_device_addr) {
2858 os_memcpy(dev_addr, msg.p2p_device_addr, ETH_ALEN);
2859 return 0;
2860 } else if (msg.device_id) {
2861 os_memcpy(dev_addr, msg.device_id, ETH_ALEN);
2862 return 0;
2863 }
2864 return -1;
2865}
2866
2867
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002868int p2p_parse_dev_addr(const u8 *ies, size_t ies_len, u8 *dev_addr)
2869{
2870 struct wpabuf *p2p_ie;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002871 int ret;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002872
2873 p2p_ie = ieee802_11_vendor_ie_concat(ies, ies_len,
2874 P2P_IE_VENDOR_TYPE);
2875 if (p2p_ie == NULL)
2876 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002877 ret = p2p_parse_dev_addr_in_p2p_ie(p2p_ie, dev_addr);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002878 wpabuf_free(p2p_ie);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002879 return ret;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002880}
2881
2882
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002883static void p2p_clear_go_neg(struct p2p_data *p2p)
2884{
2885 p2p->go_neg_peer = NULL;
2886 p2p_clear_timeout(p2p);
2887 p2p_set_state(p2p, P2P_IDLE);
2888}
2889
2890
2891void p2p_wps_success_cb(struct p2p_data *p2p, const u8 *mac_addr)
2892{
2893 if (p2p->go_neg_peer == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002894 p2p_dbg(p2p, "No pending Group Formation - ignore WPS registration success notification");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002895 return; /* No pending Group Formation */
2896 }
2897
2898 if (os_memcmp(mac_addr, p2p->go_neg_peer->intended_addr, ETH_ALEN) !=
2899 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002900 p2p_dbg(p2p, "Ignore WPS registration success notification for "
2901 MACSTR " (GO Negotiation peer " MACSTR ")",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002902 MAC2STR(mac_addr),
2903 MAC2STR(p2p->go_neg_peer->intended_addr));
2904 return; /* Ignore unexpected peer address */
2905 }
2906
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002907 p2p_dbg(p2p, "Group Formation completed successfully with " MACSTR,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002908 MAC2STR(mac_addr));
2909
2910 p2p_clear_go_neg(p2p);
2911}
2912
2913
2914void p2p_group_formation_failed(struct p2p_data *p2p)
2915{
2916 if (p2p->go_neg_peer == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002917 p2p_dbg(p2p, "No pending Group Formation - ignore group formation failure notification");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002918 return; /* No pending Group Formation */
2919 }
2920
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002921 p2p_dbg(p2p, "Group Formation failed with " MACSTR,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002922 MAC2STR(p2p->go_neg_peer->intended_addr));
2923
2924 p2p_clear_go_neg(p2p);
2925}
2926
2927
Hai Shalom60840252021-02-19 19:02:11 -08002928bool is_p2p_6ghz_disabled(struct p2p_data *p2p)
2929{
2930 if (p2p)
2931 return p2p->cfg->p2p_6ghz_disable;
2932 return false;
2933}
2934
2935
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002936struct p2p_data * p2p_init(const struct p2p_config *cfg)
2937{
2938 struct p2p_data *p2p;
2939
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07002940 if (cfg->max_peers < 1 ||
2941 cfg->passphrase_len < 8 || cfg->passphrase_len > 63)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002942 return NULL;
2943
2944 p2p = os_zalloc(sizeof(*p2p) + sizeof(*cfg));
2945 if (p2p == NULL)
2946 return NULL;
2947 p2p->cfg = (struct p2p_config *) (p2p + 1);
2948 os_memcpy(p2p->cfg, cfg, sizeof(*cfg));
2949 if (cfg->dev_name)
2950 p2p->cfg->dev_name = os_strdup(cfg->dev_name);
2951 if (cfg->manufacturer)
2952 p2p->cfg->manufacturer = os_strdup(cfg->manufacturer);
2953 if (cfg->model_name)
2954 p2p->cfg->model_name = os_strdup(cfg->model_name);
2955 if (cfg->model_number)
2956 p2p->cfg->model_number = os_strdup(cfg->model_number);
2957 if (cfg->serial_number)
2958 p2p->cfg->serial_number = os_strdup(cfg->serial_number);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002959 if (cfg->pref_chan) {
2960 p2p->cfg->pref_chan = os_malloc(cfg->num_pref_chan *
2961 sizeof(struct p2p_channel));
2962 if (p2p->cfg->pref_chan) {
2963 os_memcpy(p2p->cfg->pref_chan, cfg->pref_chan,
2964 cfg->num_pref_chan *
2965 sizeof(struct p2p_channel));
2966 } else
2967 p2p->cfg->num_pref_chan = 0;
2968 }
2969
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002970 p2ps_gen_hash(p2p, P2PS_WILD_HASH_STR, p2p->wild_card_hash);
2971
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002972 p2p->min_disc_int = 1;
2973 p2p->max_disc_int = 3;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002974 p2p->max_disc_tu = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002975
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07002976 if (os_get_random(&p2p->next_tie_breaker, 1) < 0)
2977 p2p->next_tie_breaker = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002978 p2p->next_tie_breaker &= 0x01;
2979 if (cfg->sd_request)
2980 p2p->dev_capab |= P2P_DEV_CAPAB_SERVICE_DISCOVERY;
2981 p2p->dev_capab |= P2P_DEV_CAPAB_INVITATION_PROCEDURE;
2982 if (cfg->concurrent_operations)
2983 p2p->dev_capab |= P2P_DEV_CAPAB_CONCURRENT_OPER;
2984 p2p->dev_capab |= P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
2985
2986 dl_list_init(&p2p->devices);
2987
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002988 p2p->go_timeout = 100;
2989 p2p->client_timeout = 20;
Dmitry Shmidt13ca8d82014-02-20 10:18:40 -08002990 p2p->num_p2p_sd_queries = 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002991
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002992 p2p_dbg(p2p, "initialized");
2993 p2p_channels_dump(p2p, "channels", &p2p->cfg->channels);
2994 p2p_channels_dump(p2p, "cli_channels", &p2p->cfg->cli_channels);
2995
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002996 return p2p;
2997}
2998
2999
3000void p2p_deinit(struct p2p_data *p2p)
3001{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003002#ifdef CONFIG_WIFI_DISPLAY
3003 wpabuf_free(p2p->wfd_ie_beacon);
3004 wpabuf_free(p2p->wfd_ie_probe_req);
3005 wpabuf_free(p2p->wfd_ie_probe_resp);
3006 wpabuf_free(p2p->wfd_ie_assoc_req);
3007 wpabuf_free(p2p->wfd_ie_invitation);
3008 wpabuf_free(p2p->wfd_ie_prov_disc_req);
3009 wpabuf_free(p2p->wfd_ie_prov_disc_resp);
3010 wpabuf_free(p2p->wfd_ie_go_neg);
3011 wpabuf_free(p2p->wfd_dev_info);
3012 wpabuf_free(p2p->wfd_assoc_bssid);
3013 wpabuf_free(p2p->wfd_coupled_sink_info);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003014 wpabuf_free(p2p->wfd_r2_dev_info);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003015#endif /* CONFIG_WIFI_DISPLAY */
3016
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003017 eloop_cancel_timeout(p2p_scan_timeout, p2p, NULL);
Dmitry Shmidt9cdf1b92013-02-27 12:58:50 -08003018 eloop_cancel_timeout(p2p_go_neg_start, p2p, NULL);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003019 eloop_cancel_timeout(p2p_go_neg_wait_timeout, p2p, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003020 p2p_flush(p2p);
3021 p2p_free_req_dev_types(p2p);
3022 os_free(p2p->cfg->dev_name);
3023 os_free(p2p->cfg->manufacturer);
3024 os_free(p2p->cfg->model_name);
3025 os_free(p2p->cfg->model_number);
3026 os_free(p2p->cfg->serial_number);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003027 os_free(p2p->cfg->pref_chan);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003028 os_free(p2p->groups);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07003029 p2ps_prov_free(p2p);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003030 wpabuf_free(p2p->sd_resp);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003031 p2p_remove_wps_vendor_extensions(p2p);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003032 os_free(p2p->no_go_freq.range);
Dmitry Shmidtdda10c22015-03-24 16:05:01 -07003033 p2p_service_flush_asp(p2p);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003034
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003035 os_free(p2p);
3036}
3037
3038
3039void p2p_flush(struct p2p_data *p2p)
3040{
3041 struct p2p_device *dev, *prev;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003042
3043 p2p_ext_listen(p2p, 0, 0);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003044 p2p_stop_find(p2p);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003045 dl_list_for_each_safe(dev, prev, &p2p->devices, struct p2p_device,
3046 list) {
3047 dl_list_del(&dev->list);
3048 p2p_device_free(p2p, dev);
3049 }
3050 p2p_free_sd_queries(p2p);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003051 p2p->ssid_set = 0;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003052 p2ps_prov_free(p2p);
3053 p2p_reset_pending_pd(p2p);
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08003054 p2p->override_pref_op_class = 0;
3055 p2p->override_pref_channel = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003056}
3057
3058
3059int p2p_unauthorize(struct p2p_data *p2p, const u8 *addr)
3060{
3061 struct p2p_device *dev;
3062
3063 dev = p2p_get_device(p2p, addr);
3064 if (dev == NULL)
3065 return -1;
3066
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003067 p2p_dbg(p2p, "Unauthorizing " MACSTR, MAC2STR(addr));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003068
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003069 if (p2p->go_neg_peer == dev) {
3070 eloop_cancel_timeout(p2p_go_neg_wait_timeout, p2p, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003071 p2p->go_neg_peer = NULL;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003072 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003073
3074 dev->wps_method = WPS_NOT_READY;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003075 dev->oob_pw_id = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003076 dev->flags &= ~P2P_DEV_WAIT_GO_NEG_RESPONSE;
3077 dev->flags &= ~P2P_DEV_WAIT_GO_NEG_CONFIRM;
3078
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003079 return 0;
3080}
3081
3082
3083int p2p_set_dev_name(struct p2p_data *p2p, const char *dev_name)
3084{
3085 os_free(p2p->cfg->dev_name);
3086 if (dev_name) {
3087 p2p->cfg->dev_name = os_strdup(dev_name);
3088 if (p2p->cfg->dev_name == NULL)
3089 return -1;
3090 } else
3091 p2p->cfg->dev_name = NULL;
3092 return 0;
3093}
3094
3095
3096int p2p_set_manufacturer(struct p2p_data *p2p, const char *manufacturer)
3097{
3098 os_free(p2p->cfg->manufacturer);
3099 p2p->cfg->manufacturer = NULL;
3100 if (manufacturer) {
3101 p2p->cfg->manufacturer = os_strdup(manufacturer);
3102 if (p2p->cfg->manufacturer == NULL)
3103 return -1;
3104 }
3105
3106 return 0;
3107}
3108
3109
3110int p2p_set_model_name(struct p2p_data *p2p, const char *model_name)
3111{
3112 os_free(p2p->cfg->model_name);
3113 p2p->cfg->model_name = NULL;
3114 if (model_name) {
3115 p2p->cfg->model_name = os_strdup(model_name);
3116 if (p2p->cfg->model_name == NULL)
3117 return -1;
3118 }
3119
3120 return 0;
3121}
3122
3123
3124int p2p_set_model_number(struct p2p_data *p2p, const char *model_number)
3125{
3126 os_free(p2p->cfg->model_number);
3127 p2p->cfg->model_number = NULL;
3128 if (model_number) {
3129 p2p->cfg->model_number = os_strdup(model_number);
3130 if (p2p->cfg->model_number == NULL)
3131 return -1;
3132 }
3133
3134 return 0;
3135}
3136
3137
3138int p2p_set_serial_number(struct p2p_data *p2p, const char *serial_number)
3139{
3140 os_free(p2p->cfg->serial_number);
3141 p2p->cfg->serial_number = NULL;
3142 if (serial_number) {
3143 p2p->cfg->serial_number = os_strdup(serial_number);
3144 if (p2p->cfg->serial_number == NULL)
3145 return -1;
3146 }
3147
3148 return 0;
3149}
3150
3151
3152void p2p_set_config_methods(struct p2p_data *p2p, u16 config_methods)
3153{
3154 p2p->cfg->config_methods = config_methods;
3155}
3156
3157
3158void p2p_set_uuid(struct p2p_data *p2p, const u8 *uuid)
3159{
3160 os_memcpy(p2p->cfg->uuid, uuid, 16);
3161}
3162
3163
3164int p2p_set_pri_dev_type(struct p2p_data *p2p, const u8 *pri_dev_type)
3165{
3166 os_memcpy(p2p->cfg->pri_dev_type, pri_dev_type, 8);
3167 return 0;
3168}
3169
3170
3171int p2p_set_sec_dev_types(struct p2p_data *p2p, const u8 dev_types[][8],
3172 size_t num_dev_types)
3173{
3174 if (num_dev_types > P2P_SEC_DEVICE_TYPES)
3175 num_dev_types = P2P_SEC_DEVICE_TYPES;
3176 p2p->cfg->num_sec_dev_types = num_dev_types;
3177 os_memcpy(p2p->cfg->sec_dev_type, dev_types, num_dev_types * 8);
3178 return 0;
3179}
3180
3181
3182void p2p_remove_wps_vendor_extensions(struct p2p_data *p2p)
3183{
3184 int i;
3185
3186 for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) {
3187 wpabuf_free(p2p->wps_vendor_ext[i]);
3188 p2p->wps_vendor_ext[i] = NULL;
3189 }
3190}
3191
3192
3193int p2p_add_wps_vendor_extension(struct p2p_data *p2p,
3194 const struct wpabuf *vendor_ext)
3195{
3196 int i;
3197
3198 if (vendor_ext == NULL)
3199 return -1;
3200
3201 for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) {
3202 if (p2p->wps_vendor_ext[i] == NULL)
3203 break;
3204 }
3205 if (i >= P2P_MAX_WPS_VENDOR_EXT)
3206 return -1;
3207
3208 p2p->wps_vendor_ext[i] = wpabuf_dup(vendor_ext);
3209 if (p2p->wps_vendor_ext[i] == NULL)
3210 return -1;
3211
3212 return 0;
3213}
3214
3215
3216int p2p_set_country(struct p2p_data *p2p, const char *country)
3217{
3218 os_memcpy(p2p->cfg->country, country, 3);
3219 return 0;
3220}
3221
3222
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003223static int p2p_pre_find_operation(struct p2p_data *p2p, struct p2p_device *dev)
3224{
Dmitry Shmidtde47be72016-01-07 12:52:55 -08003225 int res;
3226
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003227 if (dev->sd_pending_bcast_queries == 0) {
3228 /* Initialize with total number of registered broadcast
3229 * SD queries. */
3230 dev->sd_pending_bcast_queries = p2p->num_p2p_sd_queries;
3231 }
3232
Dmitry Shmidtde47be72016-01-07 12:52:55 -08003233 res = p2p_start_sd(p2p, dev);
3234 if (res == -2)
3235 return -2;
3236 if (res == 0)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003237 return 1;
3238
3239 if (dev->req_config_methods &&
3240 !(dev->flags & P2P_DEV_PD_FOR_JOIN)) {
3241 p2p_dbg(p2p, "Send pending Provision Discovery Request to "
3242 MACSTR " (config methods 0x%x)",
3243 MAC2STR(dev->info.p2p_device_addr),
3244 dev->req_config_methods);
3245 if (p2p_send_prov_disc_req(p2p, dev, 0, 0) == 0)
3246 return 1;
3247 }
3248
3249 return 0;
3250}
3251
3252
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003253void p2p_continue_find(struct p2p_data *p2p)
3254{
3255 struct p2p_device *dev;
Dmitry Shmidtde47be72016-01-07 12:52:55 -08003256 int found, res;
Dmitry Shmidt13ca8d82014-02-20 10:18:40 -08003257
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003258 p2p_set_state(p2p, P2P_SEARCH);
3259
3260 /* Continue from the device following the last iteration */
3261 found = 0;
3262 dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
3263 if (dev == p2p->last_p2p_find_oper) {
3264 found = 1;
3265 continue;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003266 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003267 if (!found)
3268 continue;
Dmitry Shmidtde47be72016-01-07 12:52:55 -08003269 res = p2p_pre_find_operation(p2p, dev);
3270 if (res > 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003271 p2p->last_p2p_find_oper = dev;
3272 return;
3273 }
Dmitry Shmidtde47be72016-01-07 12:52:55 -08003274 if (res == -2)
3275 goto skip_sd;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003276 }
3277
3278 /*
3279 * Wrap around to the beginning of the list and continue until the last
3280 * iteration device.
3281 */
3282 dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
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 if (dev == p2p->last_p2p_find_oper)
3291 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003292 }
3293
Dmitry Shmidtde47be72016-01-07 12:52:55 -08003294skip_sd:
3295 os_memset(p2p->sd_query_no_ack, 0, ETH_ALEN);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003296 p2p_listen_in_find(p2p, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003297}
3298
3299
3300static void p2p_sd_cb(struct p2p_data *p2p, int success)
3301{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003302 p2p_dbg(p2p, "Service Discovery Query TX callback: success=%d",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003303 success);
3304 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
3305
3306 if (!success) {
Dmitry Shmidtde47be72016-01-07 12:52:55 -08003307 if (p2p->sd_peer) {
3308 if (is_zero_ether_addr(p2p->sd_query_no_ack)) {
3309 os_memcpy(p2p->sd_query_no_ack,
3310 p2p->sd_peer->info.p2p_device_addr,
3311 ETH_ALEN);
3312 p2p_dbg(p2p,
3313 "First SD Query no-ACK in this search iteration: "
3314 MACSTR, MAC2STR(p2p->sd_query_no_ack));
3315 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003316 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
Dmitry Shmidtde47be72016-01-07 12:52:55 -08003317 }
Dmitry Shmidt13ca8d82014-02-20 10:18:40 -08003318 p2p->sd_peer = NULL;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003319 if (p2p->state != P2P_IDLE)
3320 p2p_continue_find(p2p);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003321 return;
3322 }
3323
3324 if (p2p->sd_peer == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003325 p2p_dbg(p2p, "No SD peer entry known");
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003326 if (p2p->state != P2P_IDLE)
3327 p2p_continue_find(p2p);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003328 return;
3329 }
3330
Dmitry Shmidt807291d2015-01-27 13:40:23 -08003331 if (p2p->sd_query && p2p->sd_query->for_all_peers) {
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07003332 /* Update the pending broadcast SD query count for this device
3333 */
3334 p2p->sd_peer->sd_pending_bcast_queries--;
3335
3336 /*
3337 * If there are no pending broadcast queries for this device,
3338 * mark it as done (-1).
3339 */
3340 if (p2p->sd_peer->sd_pending_bcast_queries == 0)
3341 p2p->sd_peer->sd_pending_bcast_queries = -1;
3342 }
3343
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003344 /* Wait for response from the peer */
3345 p2p_set_state(p2p, P2P_SD_DURING_FIND);
3346 p2p_set_timeout(p2p, 0, 200000);
3347}
3348
Jouni Malinen75ecf522011-06-27 15:19:46 -07003349
3350/**
3351 * p2p_retry_pd - Retry any pending provision disc requests in IDLE state
3352 * @p2p: P2P module context from p2p_init()
3353 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003354static void p2p_retry_pd(struct p2p_data *p2p)
Jouni Malinen75ecf522011-06-27 15:19:46 -07003355{
3356 struct p2p_device *dev;
3357
Jouni Malinen75ecf522011-06-27 15:19:46 -07003358 /*
3359 * Retry the prov disc req attempt only for the peer that the user had
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003360 * requested.
Jouni Malinen75ecf522011-06-27 15:19:46 -07003361 */
3362
3363 dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
3364 if (os_memcmp(p2p->pending_pd_devaddr,
3365 dev->info.p2p_device_addr, ETH_ALEN) != 0)
3366 continue;
3367 if (!dev->req_config_methods)
3368 continue;
Jouni Malinen75ecf522011-06-27 15:19:46 -07003369
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003370 p2p_dbg(p2p, "Send pending Provision Discovery Request to "
Jouni Malinen75ecf522011-06-27 15:19:46 -07003371 MACSTR " (config methods 0x%x)",
3372 MAC2STR(dev->info.p2p_device_addr),
3373 dev->req_config_methods);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003374 p2p_send_prov_disc_req(p2p, dev,
Dmitry Shmidt051af732013-10-22 13:52:46 -07003375 dev->flags & P2P_DEV_PD_FOR_JOIN,
3376 p2p->pd_force_freq);
Jouni Malinen75ecf522011-06-27 15:19:46 -07003377 return;
3378 }
3379}
3380
3381
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003382static void p2p_prov_disc_cb(struct p2p_data *p2p, int success)
3383{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003384 p2p_dbg(p2p, "Provision Discovery Request TX callback: success=%d",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003385 success);
Jouni Malinen75ecf522011-06-27 15:19:46 -07003386
3387 /*
3388 * Postpone resetting the pending action state till after we actually
3389 * time out. This allows us to take some action like notifying any
3390 * interested parties about no response to the request.
3391 *
3392 * When the timer (below) goes off we check in IDLE, SEARCH, or
3393 * LISTEN_ONLY state, which are the only allowed states to issue a PD
3394 * requests in, if this was still pending and then raise notification.
3395 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003396
3397 if (!success) {
Jouni Malinen75ecf522011-06-27 15:19:46 -07003398 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
3399
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003400 if (p2p->user_initiated_pd &&
3401 (p2p->state == P2P_SEARCH || p2p->state == P2P_LISTEN_ONLY))
3402 {
3403 /* Retry request from timeout to avoid busy loops */
3404 p2p->pending_action_state = P2P_PENDING_PD;
3405 p2p_set_timeout(p2p, 0, 50000);
3406 } else if (p2p->state != P2P_IDLE)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003407 p2p_continue_find(p2p);
Jouni Malinen75ecf522011-06-27 15:19:46 -07003408 else if (p2p->user_initiated_pd) {
3409 p2p->pending_action_state = P2P_PENDING_PD;
3410 p2p_set_timeout(p2p, 0, 300000);
3411 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003412 return;
3413 }
3414
Jouni Malinen75ecf522011-06-27 15:19:46 -07003415 /*
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003416 * If after PD Request the peer doesn't expect to receive PD Response
3417 * the PD Request ACK indicates a completion of the current PD. This
3418 * happens only on the advertiser side sending the follow-on PD Request
3419 * with the status different than 12 (Success: accepted by user).
3420 */
3421 if (p2p->p2ps_prov && !p2p->p2ps_prov->pd_seeker &&
3422 p2p->p2ps_prov->status != P2P_SC_SUCCESS_DEFERRED) {
3423 p2p_dbg(p2p, "P2PS PD completion on Follow-on PD Request ACK");
3424
3425 if (p2p->send_action_in_progress) {
3426 p2p->send_action_in_progress = 0;
3427 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
3428 }
3429
3430 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
3431
3432 if (p2p->cfg->p2ps_prov_complete) {
3433 p2p->cfg->p2ps_prov_complete(
3434 p2p->cfg->cb_ctx,
3435 p2p->p2ps_prov->status,
3436 p2p->p2ps_prov->adv_mac,
3437 p2p->p2ps_prov->adv_mac,
3438 p2p->p2ps_prov->session_mac,
3439 NULL, p2p->p2ps_prov->adv_id,
3440 p2p->p2ps_prov->session_id,
3441 0, 0, NULL, 0, 0, 0,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08003442 NULL, NULL, 0, 0, NULL, 0);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003443 }
3444
3445 if (p2p->user_initiated_pd)
3446 p2p_reset_pending_pd(p2p);
3447
3448 p2ps_prov_free(p2p);
3449 return;
3450 }
3451
3452 /*
Jouni Malinen75ecf522011-06-27 15:19:46 -07003453 * This postponing, of resetting pending_action_state, needs to be
3454 * done only for user initiated PD requests and not internal ones.
3455 */
3456 if (p2p->user_initiated_pd)
3457 p2p->pending_action_state = P2P_PENDING_PD;
3458 else
3459 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
3460
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003461 /* Wait for response from the peer */
3462 if (p2p->state == P2P_SEARCH)
3463 p2p_set_state(p2p, P2P_PD_DURING_FIND);
3464 p2p_set_timeout(p2p, 0, 200000);
3465}
3466
3467
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003468static void p2p_prov_disc_resp_cb(struct p2p_data *p2p, int success)
3469{
3470 p2p_dbg(p2p, "Provision Discovery Response TX callback: success=%d",
3471 success);
3472
3473 if (p2p->send_action_in_progress) {
3474 p2p->send_action_in_progress = 0;
3475 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
3476 }
3477
3478 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
3479
Jimmy Chen5ef7aad2019-10-15 15:45:26 +08003480 if (!success) {
3481 if (p2p->state == P2P_SEARCH)
3482 p2p_continue_find(p2p);
Hai Shalom81f62d82019-07-22 12:10:00 -07003483 return;
Jimmy Chen5ef7aad2019-10-15 15:45:26 +08003484 }
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003485
3486 if (!p2p->cfg->prov_disc_resp_cb ||
Jimmy Chen5ef7aad2019-10-15 15:45:26 +08003487 p2p->cfg->prov_disc_resp_cb(p2p->cfg->cb_ctx) < 1) {
3488 if (p2p->state == P2P_SEARCH)
3489 p2p_continue_find(p2p);
Hai Shalom81f62d82019-07-22 12:10:00 -07003490 return;
Jimmy Chen5ef7aad2019-10-15 15:45:26 +08003491 }
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003492
3493 p2p_dbg(p2p,
3494 "Post-Provision Discovery operations started - do not try to continue other P2P operations");
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003495}
3496
3497
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003498int p2p_scan_res_handler(struct p2p_data *p2p, const u8 *bssid, int freq,
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003499 struct os_reltime *rx_time, int level, const u8 *ies,
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003500 size_t ies_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003501{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003502 if (os_reltime_before(rx_time, &p2p->find_start)) {
Dmitry Shmidtf8623282013-02-20 14:34:59 -08003503 /*
3504 * The driver may have cached (e.g., in cfg80211 BSS table) the
3505 * scan results for relatively long time. To avoid reporting
3506 * stale information, update P2P peers only based on results
3507 * that have based on frames received after the last p2p_find
3508 * operation was started.
3509 */
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003510 p2p_dbg(p2p, "Ignore old scan result for " MACSTR
Dmitry Shmidtde47be72016-01-07 12:52:55 -08003511 " (rx_time=%u.%06u find_start=%u.%06u)",
Dmitry Shmidtf8623282013-02-20 14:34:59 -08003512 MAC2STR(bssid), (unsigned int) rx_time->sec,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08003513 (unsigned int) rx_time->usec,
3514 (unsigned int) p2p->find_start.sec,
3515 (unsigned int) p2p->find_start.usec);
Dmitry Shmidtf8623282013-02-20 14:34:59 -08003516 return 0;
3517 }
3518
3519 p2p_add_device(p2p, bssid, freq, rx_time, level, ies, ies_len, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003520
3521 return 0;
3522}
3523
3524
Hai Shalom60840252021-02-19 19:02:11 -08003525void p2p_scan_res_handled(struct p2p_data *p2p, unsigned int delay)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003526{
3527 if (!p2p->p2p_scan_running) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003528 p2p_dbg(p2p, "p2p_scan was not running, but scan results received");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003529 }
3530 p2p->p2p_scan_running = 0;
Hai Shalom60840252021-02-19 19:02:11 -08003531
3532 /* Use this delay only when p2p_find doesn't set it */
3533 if (!p2p->search_delay)
3534 p2p->search_delay = delay;
3535
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003536 eloop_cancel_timeout(p2p_scan_timeout, p2p, NULL);
3537
3538 if (p2p_run_after_scan(p2p))
3539 return;
3540 if (p2p->state == P2P_SEARCH)
3541 p2p_continue_find(p2p);
3542}
3543
3544
Dmitry Shmidt9c175262016-03-03 10:20:07 -08003545void p2p_scan_ie(struct p2p_data *p2p, struct wpabuf *ies, const u8 *dev_id,
3546 unsigned int bands)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003547{
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003548 u8 dev_capab;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003549 u8 *len;
3550
3551#ifdef CONFIG_WIFI_DISPLAY
3552 if (p2p->wfd_ie_probe_req)
3553 wpabuf_put_buf(ies, p2p->wfd_ie_probe_req);
3554#endif /* CONFIG_WIFI_DISPLAY */
3555
Dmitry Shmidt2e67f062014-07-16 09:55:28 -07003556 if (p2p->vendor_elem && p2p->vendor_elem[VENDOR_ELEM_PROBE_REQ_P2P])
3557 wpabuf_put_buf(ies,
3558 p2p->vendor_elem[VENDOR_ELEM_PROBE_REQ_P2P]);
3559
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003560 len = p2p_buf_add_ie_hdr(ies);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003561
3562 dev_capab = p2p->dev_capab & ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
3563
3564 /* P2PS requires Probe Request frames to include SD bit */
3565 if (p2p->p2ps_seek && p2p->p2ps_seek_count)
3566 dev_capab |= P2P_DEV_CAPAB_SERVICE_DISCOVERY;
3567
3568 p2p_buf_add_capability(ies, dev_capab, 0);
3569
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003570 if (dev_id)
3571 p2p_buf_add_device_id(ies, dev_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003572 if (p2p->cfg->reg_class && p2p->cfg->channel)
3573 p2p_buf_add_listen_channel(ies, p2p->cfg->country,
3574 p2p->cfg->reg_class,
3575 p2p->cfg->channel);
3576 if (p2p->ext_listen_interval)
3577 p2p_buf_add_ext_listen_timing(ies, p2p->ext_listen_period,
3578 p2p->ext_listen_interval);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003579
Dmitry Shmidt9c175262016-03-03 10:20:07 -08003580 if (bands & BAND_60_GHZ)
3581 p2p_buf_add_device_info(ies, p2p, NULL);
3582
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003583 if (p2p->p2ps_seek && p2p->p2ps_seek_count)
3584 p2p_buf_add_service_hash(ies, p2p);
3585
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003586 /* TODO: p2p_buf_add_operating_channel() if GO */
3587 p2p_buf_update_ie_hdr(ies, len);
3588}
3589
3590
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003591size_t p2p_scan_ie_buf_len(struct p2p_data *p2p)
3592{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003593 size_t len = 100;
3594
3595#ifdef CONFIG_WIFI_DISPLAY
3596 if (p2p && p2p->wfd_ie_probe_req)
3597 len += wpabuf_len(p2p->wfd_ie_probe_req);
3598#endif /* CONFIG_WIFI_DISPLAY */
3599
Dmitry Shmidt2e67f062014-07-16 09:55:28 -07003600 if (p2p && p2p->vendor_elem &&
3601 p2p->vendor_elem[VENDOR_ELEM_PROBE_REQ_P2P])
3602 len += wpabuf_len(p2p->vendor_elem[VENDOR_ELEM_PROBE_REQ_P2P]);
3603
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003604 return len;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003605}
3606
3607
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003608int p2p_ie_text(struct wpabuf *p2p_ie, char *buf, char *end)
3609{
3610 return p2p_attr_text(p2p_ie, buf, end);
3611}
3612
3613
3614static void p2p_go_neg_req_cb(struct p2p_data *p2p, int success)
3615{
3616 struct p2p_device *dev = p2p->go_neg_peer;
Dmitry Shmidt8c652892013-03-01 10:14:01 -08003617 int timeout;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003618
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003619 p2p_dbg(p2p, "GO Negotiation Request TX callback: success=%d", success);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003620
3621 if (dev == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003622 p2p_dbg(p2p, "No pending GO Negotiation");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003623 return;
3624 }
3625
3626 if (success) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003627 if (dev->flags & P2P_DEV_USER_REJECTED) {
3628 p2p_set_state(p2p, P2P_IDLE);
3629 return;
3630 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07003631 } else if (dev->go_neg_req_sent) {
3632 /* Cancel the increment from p2p_connect_send() on failure */
Dmitry Shmidt98f9e762012-05-30 11:18:46 -07003633 dev->go_neg_req_sent--;
3634 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003635
3636 if (!success &&
3637 (dev->info.dev_capab & P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY) &&
3638 !is_zero_ether_addr(dev->member_in_go_dev)) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003639 p2p_dbg(p2p, "Peer " MACSTR " did not acknowledge request - try to use device discoverability through its GO",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003640 MAC2STR(dev->info.p2p_device_addr));
3641 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
3642 p2p_send_dev_disc_req(p2p, dev);
3643 return;
3644 }
3645
3646 /*
3647 * Use P2P find, if needed, to find the other device from its listen
3648 * channel.
3649 */
3650 p2p_set_state(p2p, P2P_CONNECT);
Dmitry Shmidt8c652892013-03-01 10:14:01 -08003651 timeout = success ? 500000 : 100000;
3652 if (!success && p2p->go_neg_peer &&
3653 (p2p->go_neg_peer->flags & P2P_DEV_PEER_WAITING_RESPONSE)) {
3654 unsigned int r;
3655 /*
3656 * Peer is expected to wait our response and we will skip the
3657 * listen phase. Add some randomness to the wait time here to
3658 * make it less likely to hit cases where we could end up in
3659 * sync with peer not listening.
3660 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07003661 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
3662 r = 0;
Dmitry Shmidt8c652892013-03-01 10:14:01 -08003663 timeout += r % 100000;
3664 }
3665 p2p_set_timeout(p2p, 0, timeout);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003666}
3667
3668
3669static void p2p_go_neg_resp_cb(struct p2p_data *p2p, int success)
3670{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003671 p2p_dbg(p2p, "GO Negotiation Response TX callback: success=%d",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003672 success);
3673 if (!p2p->go_neg_peer && p2p->state == P2P_PROVISIONING) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003674 p2p_dbg(p2p, "Ignore TX callback event - GO Negotiation is not running anymore");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003675 return;
3676 }
3677 p2p_set_state(p2p, P2P_CONNECT);
Dmitry Shmidt8c652892013-03-01 10:14:01 -08003678 p2p_set_timeout(p2p, 0, 500000);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003679}
3680
3681
Dmitry Shmidt4b060592013-04-29 16:42:49 -07003682static void p2p_go_neg_resp_failure_cb(struct p2p_data *p2p, int success,
3683 const u8 *addr)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003684{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003685 p2p_dbg(p2p, "GO Negotiation Response (failure) TX callback: success=%d", success);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003686 if (p2p->go_neg_peer && p2p->go_neg_peer->status != P2P_SC_SUCCESS) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003687 p2p_go_neg_failed(p2p, p2p->go_neg_peer->status);
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08003688 return;
3689 }
3690
3691 if (success) {
Dmitry Shmidt4b060592013-04-29 16:42:49 -07003692 struct p2p_device *dev;
3693 dev = p2p_get_device(p2p, addr);
3694 if (dev &&
Dmitry Shmidt34c12022015-03-05 14:11:20 -08003695 dev->status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE)
Dmitry Shmidt4b060592013-04-29 16:42:49 -07003696 dev->flags |= P2P_DEV_PEER_WAITING_RESPONSE;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003697 }
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08003698
3699 if (p2p->state == P2P_SEARCH || p2p->state == P2P_SD_DURING_FIND)
3700 p2p_continue_find(p2p);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003701}
3702
3703
3704static void p2p_go_neg_conf_cb(struct p2p_data *p2p,
3705 enum p2p_send_action_result result)
3706{
3707 struct p2p_device *dev;
3708
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003709 p2p_dbg(p2p, "GO Negotiation Confirm TX callback: result=%d", result);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003710 if (result == P2P_SEND_ACTION_FAILED) {
Dmitry Shmidt413dde72014-04-11 10:23:22 -07003711 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003712 p2p_go_neg_failed(p2p, -1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003713 return;
3714 }
Dmitry Shmidt413dde72014-04-11 10:23:22 -07003715
3716 dev = p2p->go_neg_peer;
3717
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003718 if (result == P2P_SEND_ACTION_NO_ACK) {
3719 /*
Dmitry Shmidt413dde72014-04-11 10:23:22 -07003720 * Retry GO Negotiation Confirmation
3721 * P2P_GO_NEG_CNF_MAX_RETRY_COUNT times if we did not receive
3722 * ACK for confirmation.
3723 */
3724 if (dev && dev->go_neg_conf &&
3725 dev->go_neg_conf_sent <= P2P_GO_NEG_CNF_MAX_RETRY_COUNT) {
3726 p2p_dbg(p2p, "GO Negotiation Confirm retry %d",
3727 dev->go_neg_conf_sent);
3728 p2p->pending_action_state = P2P_PENDING_GO_NEG_CONFIRM;
3729 if (p2p_send_action(p2p, dev->go_neg_conf_freq,
3730 dev->info.p2p_device_addr,
3731 p2p->cfg->dev_addr,
3732 dev->info.p2p_device_addr,
3733 wpabuf_head(dev->go_neg_conf),
3734 wpabuf_len(dev->go_neg_conf), 0) >=
3735 0) {
3736 dev->go_neg_conf_sent++;
3737 return;
3738 }
3739 p2p_dbg(p2p, "Failed to re-send Action frame");
3740
3741 /*
3742 * Continue with the assumption that the first attempt
3743 * went through and just the ACK frame was lost.
3744 */
3745 }
3746
3747 /*
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003748 * It looks like the TX status for GO Negotiation Confirm is
3749 * often showing failure even when the peer has actually
3750 * received the frame. Since the peer may change channels
3751 * immediately after having received the frame, we may not see
3752 * an Ack for retries, so just dropping a single frame may
3753 * trigger this. To allow the group formation to succeed if the
3754 * peer did indeed receive the frame, continue regardless of
3755 * the TX status.
3756 */
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003757 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 -07003758 }
3759
Dmitry Shmidt413dde72014-04-11 10:23:22 -07003760 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
3761
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003762 if (dev == NULL)
3763 return;
3764
3765 p2p_go_complete(p2p, dev);
3766}
3767
3768
3769void p2p_send_action_cb(struct p2p_data *p2p, unsigned int freq, const u8 *dst,
3770 const u8 *src, const u8 *bssid,
3771 enum p2p_send_action_result result)
3772{
3773 enum p2p_pending_action_state state;
3774 int success;
3775
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003776 p2p_dbg(p2p, "Action frame TX callback (state=%d freq=%u dst=" MACSTR
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003777 " src=" MACSTR " bssid=" MACSTR " result=%d p2p_state=%s)",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003778 p2p->pending_action_state, freq, MAC2STR(dst), MAC2STR(src),
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003779 MAC2STR(bssid), result, p2p_state_txt(p2p->state));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003780 success = result == P2P_SEND_ACTION_SUCCESS;
3781 state = p2p->pending_action_state;
3782 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
3783 switch (state) {
3784 case P2P_NO_PENDING_ACTION:
Dmitry Shmidt4582d2a2014-02-28 11:14:23 -08003785 if (p2p->send_action_in_progress) {
3786 p2p->send_action_in_progress = 0;
3787 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
3788 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003789 break;
3790 case P2P_PENDING_GO_NEG_REQUEST:
3791 p2p_go_neg_req_cb(p2p, success);
3792 break;
3793 case P2P_PENDING_GO_NEG_RESPONSE:
3794 p2p_go_neg_resp_cb(p2p, success);
3795 break;
3796 case P2P_PENDING_GO_NEG_RESPONSE_FAILURE:
Dmitry Shmidt4b060592013-04-29 16:42:49 -07003797 p2p_go_neg_resp_failure_cb(p2p, success, dst);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003798 break;
3799 case P2P_PENDING_GO_NEG_CONFIRM:
3800 p2p_go_neg_conf_cb(p2p, result);
3801 break;
3802 case P2P_PENDING_SD:
3803 p2p_sd_cb(p2p, success);
3804 break;
3805 case P2P_PENDING_PD:
3806 p2p_prov_disc_cb(p2p, success);
3807 break;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003808 case P2P_PENDING_PD_RESPONSE:
3809 p2p_prov_disc_resp_cb(p2p, success);
3810 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003811 case P2P_PENDING_INVITATION_REQUEST:
3812 p2p_invitation_req_cb(p2p, success);
3813 break;
3814 case P2P_PENDING_INVITATION_RESPONSE:
3815 p2p_invitation_resp_cb(p2p, success);
3816 break;
3817 case P2P_PENDING_DEV_DISC_REQUEST:
3818 p2p_dev_disc_req_cb(p2p, success);
3819 break;
3820 case P2P_PENDING_DEV_DISC_RESPONSE:
3821 p2p_dev_disc_resp_cb(p2p, success);
3822 break;
3823 case P2P_PENDING_GO_DISC_REQ:
3824 p2p_go_disc_req_cb(p2p, success);
3825 break;
3826 }
3827}
3828
3829
3830void p2p_listen_cb(struct p2p_data *p2p, unsigned int freq,
3831 unsigned int duration)
3832{
3833 if (freq == p2p->pending_client_disc_freq) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003834 p2p_dbg(p2p, "Client discoverability remain-awake completed");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003835 p2p->pending_client_disc_freq = 0;
3836 return;
3837 }
3838
3839 if (freq != p2p->pending_listen_freq) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003840 p2p_dbg(p2p, "Unexpected listen callback for freq=%u duration=%u (pending_listen_freq=%u)",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003841 freq, duration, p2p->pending_listen_freq);
3842 return;
3843 }
3844
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003845 p2p_dbg(p2p, "Starting Listen timeout(%u,%u) on freq=%u based on callback",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003846 p2p->pending_listen_sec, p2p->pending_listen_usec,
3847 p2p->pending_listen_freq);
3848 p2p->in_listen = 1;
3849 p2p->drv_in_listen = freq;
3850 if (p2p->pending_listen_sec || p2p->pending_listen_usec) {
3851 /*
3852 * Add 20 msec extra wait to avoid race condition with driver
3853 * remain-on-channel end event, i.e., give driver more time to
3854 * complete the operation before our timeout expires.
3855 */
3856 p2p_set_timeout(p2p, p2p->pending_listen_sec,
3857 p2p->pending_listen_usec + 20000);
3858 }
3859
3860 p2p->pending_listen_freq = 0;
3861}
3862
3863
3864int p2p_listen_end(struct p2p_data *p2p, unsigned int freq)
3865{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003866 p2p_dbg(p2p, "Driver ended Listen state (freq=%u)", freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003867 p2p->drv_in_listen = 0;
3868 if (p2p->in_listen)
3869 return 0; /* Internal timeout will trigger the next step */
3870
Roshan Pius3a1667e2018-07-03 15:17:14 -07003871 if (p2p->state == P2P_WAIT_PEER_CONNECT && p2p->go_neg_peer &&
3872 p2p->pending_listen_freq) {
3873 /*
3874 * Better wait a bit if the driver is unable to start
3875 * offchannel operation for some reason to continue with
3876 * P2P_WAIT_PEER_(IDLE/CONNECT) state transitions.
3877 */
3878 p2p_dbg(p2p,
3879 "Listen operation did not seem to start - delay idle phase to avoid busy loop");
3880 p2p_set_timeout(p2p, 0, 100000);
3881 return 1;
3882 }
3883
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003884 if (p2p->state == P2P_CONNECT_LISTEN && p2p->go_neg_peer) {
3885 if (p2p->go_neg_peer->connect_reqs >= 120) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003886 p2p_dbg(p2p, "Timeout on sending GO Negotiation Request without getting response");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003887 p2p_go_neg_failed(p2p, -1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003888 return 0;
3889 }
3890
3891 p2p_set_state(p2p, P2P_CONNECT);
3892 p2p_connect_send(p2p, p2p->go_neg_peer);
3893 return 1;
3894 } else if (p2p->state == P2P_SEARCH) {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003895 if (p2p->p2p_scan_running) {
3896 /*
3897 * Search is already in progress. This can happen if
3898 * an Action frame RX is reported immediately after
3899 * the end of a remain-on-channel operation and the
3900 * response frame to that is sent using an offchannel
3901 * operation while in p2p_find. Avoid an attempt to
3902 * restart a scan here.
3903 */
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003904 p2p_dbg(p2p, "p2p_scan already in progress - do not try to start a new one");
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003905 return 1;
3906 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07003907 if (p2p->pending_listen_freq) {
3908 /*
3909 * Better wait a bit if the driver is unable to start
3910 * offchannel operation for some reason. p2p_search()
3911 * will be started from internal timeout.
3912 */
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003913 p2p_dbg(p2p, "Listen operation did not seem to start - delay search phase to avoid busy loop");
Dmitry Shmidt04949592012-07-19 12:16:46 -07003914 p2p_set_timeout(p2p, 0, 100000);
3915 return 1;
3916 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003917 if (p2p->search_delay) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003918 p2p_dbg(p2p, "Delay search operation by %u ms",
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003919 p2p->search_delay);
3920 p2p_set_timeout(p2p, p2p->search_delay / 1000,
3921 (p2p->search_delay % 1000) * 1000);
3922 return 1;
3923 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003924 p2p_search(p2p);
3925 return 1;
3926 }
3927
3928 return 0;
3929}
3930
3931
3932static void p2p_timeout_connect(struct p2p_data *p2p)
3933{
3934 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003935 if (p2p->go_neg_peer &&
3936 (p2p->go_neg_peer->flags & P2P_DEV_WAIT_GO_NEG_CONFIRM)) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003937 p2p_dbg(p2p, "Wait for GO Negotiation Confirm timed out - assume GO Negotiation failed");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003938 p2p_go_neg_failed(p2p, -1);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003939 return;
3940 }
Dmitry Shmidt8c652892013-03-01 10:14:01 -08003941 if (p2p->go_neg_peer &&
3942 (p2p->go_neg_peer->flags & P2P_DEV_PEER_WAITING_RESPONSE) &&
3943 p2p->go_neg_peer->connect_reqs < 120) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003944 p2p_dbg(p2p, "Peer expected to wait our response - skip listen");
Dmitry Shmidt8c652892013-03-01 10:14:01 -08003945 p2p_connect_send(p2p, p2p->go_neg_peer);
3946 return;
3947 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003948 if (p2p->go_neg_peer && p2p->go_neg_peer->oob_go_neg_freq > 0) {
3949 p2p_dbg(p2p, "Skip connect-listen since GO Neg channel known (OOB)");
3950 p2p_set_state(p2p, P2P_CONNECT_LISTEN);
3951 p2p_set_timeout(p2p, 0, 30000);
3952 return;
3953 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003954 p2p_set_state(p2p, P2P_CONNECT_LISTEN);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003955 p2p_listen_in_find(p2p, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003956}
3957
3958
3959static void p2p_timeout_connect_listen(struct p2p_data *p2p)
3960{
3961 if (p2p->go_neg_peer) {
3962 if (p2p->drv_in_listen) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003963 p2p_dbg(p2p, "Driver is still in Listen state; wait for it to complete");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003964 return;
3965 }
3966
3967 if (p2p->go_neg_peer->connect_reqs >= 120) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003968 p2p_dbg(p2p, "Timeout on sending GO Negotiation Request without getting response");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003969 p2p_go_neg_failed(p2p, -1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003970 return;
3971 }
3972
3973 p2p_set_state(p2p, P2P_CONNECT);
3974 p2p_connect_send(p2p, p2p->go_neg_peer);
3975 } else
3976 p2p_set_state(p2p, P2P_IDLE);
3977}
3978
3979
3980static void p2p_timeout_wait_peer_connect(struct p2p_data *p2p)
3981{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003982 p2p_set_state(p2p, P2P_WAIT_PEER_IDLE);
Dmitry Shmidt18463232014-01-24 12:29:41 -08003983
3984 if (p2p->cfg->is_concurrent_session_active &&
3985 p2p->cfg->is_concurrent_session_active(p2p->cfg->cb_ctx))
3986 p2p_set_timeout(p2p, 0, 500000);
3987 else
3988 p2p_set_timeout(p2p, 0, 200000);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003989}
3990
3991
3992static void p2p_timeout_wait_peer_idle(struct p2p_data *p2p)
3993{
3994 struct p2p_device *dev = p2p->go_neg_peer;
3995
3996 if (dev == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003997 p2p_dbg(p2p, "Unknown GO Neg peer - stop GO Neg wait");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003998 return;
3999 }
4000
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004001 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 -07004002 p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004003 p2p_set_state(p2p, P2P_WAIT_PEER_CONNECT);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004004 p2p_listen_in_find(p2p, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004005}
4006
4007
4008static void p2p_timeout_sd_during_find(struct p2p_data *p2p)
4009{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004010 p2p_dbg(p2p, "Service Discovery Query timeout");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004011 if (p2p->sd_peer) {
4012 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004013 p2p->sd_peer = NULL;
4014 }
4015 p2p_continue_find(p2p);
4016}
4017
4018
4019static void p2p_timeout_prov_disc_during_find(struct p2p_data *p2p)
4020{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004021 p2p_dbg(p2p, "Provision Discovery Request timeout");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004022 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
4023 p2p_continue_find(p2p);
4024}
4025
4026
Jouni Malinen75ecf522011-06-27 15:19:46 -07004027static void p2p_timeout_prov_disc_req(struct p2p_data *p2p)
4028{
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004029 u32 adv_id = 0;
4030 u8 *adv_mac = NULL;
4031
Jouni Malinen75ecf522011-06-27 15:19:46 -07004032 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
4033
4034 /*
4035 * For user initiated PD requests that we have not gotten any responses
4036 * for while in IDLE state, we retry them a couple of times before
4037 * giving up.
4038 */
4039 if (!p2p->user_initiated_pd)
4040 return;
4041
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004042 p2p_dbg(p2p, "User initiated Provision Discovery Request timeout");
Jouni Malinen75ecf522011-06-27 15:19:46 -07004043
4044 if (p2p->pd_retries) {
4045 p2p->pd_retries--;
4046 p2p_retry_pd(p2p);
4047 } else {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004048 struct p2p_device *dev;
4049 int for_join = 0;
4050
4051 dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
4052 if (os_memcmp(p2p->pending_pd_devaddr,
4053 dev->info.p2p_device_addr, ETH_ALEN) != 0)
4054 continue;
4055 if (dev->req_config_methods &&
4056 (dev->flags & P2P_DEV_PD_FOR_JOIN))
4057 for_join = 1;
4058 }
4059
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004060 if (p2p->p2ps_prov) {
4061 adv_id = p2p->p2ps_prov->adv_id;
4062 adv_mac = p2p->p2ps_prov->adv_mac;
4063 }
4064
Jouni Malinen75ecf522011-06-27 15:19:46 -07004065 if (p2p->cfg->prov_disc_fail)
4066 p2p->cfg->prov_disc_fail(p2p->cfg->cb_ctx,
4067 p2p->pending_pd_devaddr,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004068 for_join ?
4069 P2P_PROV_DISC_TIMEOUT_JOIN :
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004070 P2P_PROV_DISC_TIMEOUT,
4071 adv_id, adv_mac, NULL);
Jouni Malinen75ecf522011-06-27 15:19:46 -07004072 p2p_reset_pending_pd(p2p);
4073 }
4074}
4075
4076
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004077static void p2p_timeout_invite(struct p2p_data *p2p)
4078{
4079 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
4080 p2p_set_state(p2p, P2P_INVITE_LISTEN);
4081 if (p2p->inv_role == P2P_INVITE_ROLE_ACTIVE_GO) {
4082 /*
4083 * Better remain on operating channel instead of listen channel
4084 * when running a group.
4085 */
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004086 p2p_dbg(p2p, "Inviting in active GO role - wait on operating channel");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004087 p2p_set_timeout(p2p, 0, 100000);
4088 return;
4089 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004090 p2p_listen_in_find(p2p, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004091}
4092
4093
4094static void p2p_timeout_invite_listen(struct p2p_data *p2p)
4095{
4096 if (p2p->invite_peer && p2p->invite_peer->invitation_reqs < 100) {
4097 p2p_set_state(p2p, P2P_INVITE);
4098 p2p_invite_send(p2p, p2p->invite_peer,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004099 p2p->invite_go_dev_addr, p2p->invite_dev_pw_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004100 } else {
4101 if (p2p->invite_peer) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004102 p2p_dbg(p2p, "Invitation Request retry limit reached");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004103 if (p2p->cfg->invitation_result)
4104 p2p->cfg->invitation_result(
Dmitry Shmidt700a1372013-03-15 14:14:44 -07004105 p2p->cfg->cb_ctx, -1, NULL, NULL,
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004106 p2p->invite_peer->info.p2p_device_addr,
Dmitry Shmidt15907092014-03-25 10:42:57 -07004107 0, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004108 }
4109 p2p_set_state(p2p, P2P_IDLE);
4110 }
4111}
4112
4113
4114static void p2p_state_timeout(void *eloop_ctx, void *timeout_ctx)
4115{
4116 struct p2p_data *p2p = eloop_ctx;
4117
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004118 p2p_dbg(p2p, "Timeout (state=%s)", p2p_state_txt(p2p->state));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004119
4120 p2p->in_listen = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004121 if (p2p->drv_in_listen) {
4122 p2p_dbg(p2p, "Driver is still in listen state - stop it");
4123 p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
4124 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004125
4126 switch (p2p->state) {
4127 case P2P_IDLE:
Jouni Malinen75ecf522011-06-27 15:19:46 -07004128 /* Check if we timed out waiting for PD req */
4129 if (p2p->pending_action_state == P2P_PENDING_PD)
4130 p2p_timeout_prov_disc_req(p2p);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004131 break;
4132 case P2P_SEARCH:
Jouni Malinen75ecf522011-06-27 15:19:46 -07004133 /* Check if we timed out waiting for PD req */
4134 if (p2p->pending_action_state == P2P_PENDING_PD)
4135 p2p_timeout_prov_disc_req(p2p);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004136 if (p2p->search_delay && !p2p->in_search_delay) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004137 p2p_dbg(p2p, "Delay search operation by %u ms",
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004138 p2p->search_delay);
4139 p2p->in_search_delay = 1;
4140 p2p_set_timeout(p2p, p2p->search_delay / 1000,
4141 (p2p->search_delay % 1000) * 1000);
4142 break;
4143 }
4144 p2p->in_search_delay = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004145 p2p_search(p2p);
4146 break;
4147 case P2P_CONNECT:
4148 p2p_timeout_connect(p2p);
4149 break;
4150 case P2P_CONNECT_LISTEN:
4151 p2p_timeout_connect_listen(p2p);
4152 break;
4153 case P2P_GO_NEG:
4154 break;
4155 case P2P_LISTEN_ONLY:
Jouni Malinen75ecf522011-06-27 15:19:46 -07004156 /* Check if we timed out waiting for PD req */
4157 if (p2p->pending_action_state == P2P_PENDING_PD)
4158 p2p_timeout_prov_disc_req(p2p);
4159
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004160 if (p2p->ext_listen_only) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004161 p2p_dbg(p2p, "Extended Listen Timing - Listen State completed");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004162 p2p->ext_listen_only = 0;
4163 p2p_set_state(p2p, P2P_IDLE);
4164 }
4165 break;
4166 case P2P_WAIT_PEER_CONNECT:
4167 p2p_timeout_wait_peer_connect(p2p);
4168 break;
4169 case P2P_WAIT_PEER_IDLE:
4170 p2p_timeout_wait_peer_idle(p2p);
4171 break;
4172 case P2P_SD_DURING_FIND:
4173 p2p_timeout_sd_during_find(p2p);
4174 break;
4175 case P2P_PROVISIONING:
4176 break;
4177 case P2P_PD_DURING_FIND:
4178 p2p_timeout_prov_disc_during_find(p2p);
4179 break;
4180 case P2P_INVITE:
4181 p2p_timeout_invite(p2p);
4182 break;
4183 case P2P_INVITE_LISTEN:
4184 p2p_timeout_invite_listen(p2p);
4185 break;
4186 }
4187}
4188
4189
4190int p2p_reject(struct p2p_data *p2p, const u8 *peer_addr)
4191{
4192 struct p2p_device *dev;
4193
4194 dev = p2p_get_device(p2p, peer_addr);
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004195 p2p_dbg(p2p, "Local request to reject connection attempts by peer "
4196 MACSTR, MAC2STR(peer_addr));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004197 if (dev == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004198 p2p_dbg(p2p, "Peer " MACSTR " unknown", MAC2STR(peer_addr));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004199 return -1;
4200 }
4201 dev->status = P2P_SC_FAIL_REJECTED_BY_USER;
4202 dev->flags |= P2P_DEV_USER_REJECTED;
4203 return 0;
4204}
4205
4206
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004207const char * p2p_wps_method_text(enum p2p_wps_method method)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004208{
4209 switch (method) {
4210 case WPS_NOT_READY:
4211 return "not-ready";
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004212 case WPS_PIN_DISPLAY:
4213 return "Display";
4214 case WPS_PIN_KEYPAD:
4215 return "Keypad";
4216 case WPS_PBC:
4217 return "PBC";
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004218 case WPS_NFC:
4219 return "NFC";
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004220 case WPS_P2PS:
4221 return "P2PS";
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004222 }
4223
4224 return "??";
4225}
4226
4227
4228static const char * p2p_go_state_text(enum p2p_go_state go_state)
4229{
4230 switch (go_state) {
4231 case UNKNOWN_GO:
4232 return "unknown";
4233 case LOCAL_GO:
4234 return "local";
4235 case REMOTE_GO:
4236 return "remote";
4237 }
4238
4239 return "??";
4240}
4241
4242
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004243const struct p2p_peer_info * p2p_get_peer_info(struct p2p_data *p2p,
4244 const u8 *addr, int next)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004245{
4246 struct p2p_device *dev;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004247
4248 if (addr)
4249 dev = p2p_get_device(p2p, addr);
4250 else
4251 dev = dl_list_first(&p2p->devices, struct p2p_device, list);
4252
4253 if (dev && next) {
4254 dev = dl_list_first(&dev->list, struct p2p_device, list);
4255 if (&dev->list == &p2p->devices)
4256 dev = NULL;
4257 }
4258
4259 if (dev == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004260 return NULL;
4261
4262 return &dev->info;
4263}
4264
4265
4266int p2p_get_peer_info_txt(const struct p2p_peer_info *info,
4267 char *buf, size_t buflen)
4268{
4269 struct p2p_device *dev;
4270 int res;
4271 char *pos, *end;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004272 struct os_reltime now;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004273
4274 if (info == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004275 return -1;
4276
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004277 dev = (struct p2p_device *) (((u8 *) info) -
4278 offsetof(struct p2p_device, info));
4279
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004280 pos = buf;
4281 end = buf + buflen;
4282
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004283 os_get_reltime(&now);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004284 res = os_snprintf(pos, end - pos,
4285 "age=%d\n"
4286 "listen_freq=%d\n"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004287 "wps_method=%s\n"
4288 "interface_addr=" MACSTR "\n"
4289 "member_in_go_dev=" MACSTR "\n"
4290 "member_in_go_iface=" MACSTR "\n"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004291 "go_neg_req_sent=%d\n"
4292 "go_state=%s\n"
4293 "dialog_token=%u\n"
4294 "intended_addr=" MACSTR "\n"
4295 "country=%c%c\n"
4296 "oper_freq=%d\n"
4297 "req_config_methods=0x%x\n"
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07004298 "flags=%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004299 "status=%d\n"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004300 "invitation_reqs=%u\n",
4301 (int) (now.sec - dev->last_seen.sec),
4302 dev->listen_freq,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004303 p2p_wps_method_text(dev->wps_method),
4304 MAC2STR(dev->interface_addr),
4305 MAC2STR(dev->member_in_go_dev),
4306 MAC2STR(dev->member_in_go_iface),
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004307 dev->go_neg_req_sent,
4308 p2p_go_state_text(dev->go_state),
4309 dev->dialog_token,
4310 MAC2STR(dev->intended_addr),
4311 dev->country[0] ? dev->country[0] : '_',
4312 dev->country[1] ? dev->country[1] : '_',
4313 dev->oper_freq,
4314 dev->req_config_methods,
4315 dev->flags & P2P_DEV_PROBE_REQ_ONLY ?
4316 "[PROBE_REQ_ONLY]" : "",
4317 dev->flags & P2P_DEV_REPORTED ? "[REPORTED]" : "",
4318 dev->flags & P2P_DEV_NOT_YET_READY ?
4319 "[NOT_YET_READY]" : "",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004320 dev->flags & P2P_DEV_PD_PEER_DISPLAY ?
4321 "[PD_PEER_DISPLAY]" : "",
4322 dev->flags & P2P_DEV_PD_PEER_KEYPAD ?
4323 "[PD_PEER_KEYPAD]" : "",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004324 dev->flags & P2P_DEV_PD_PEER_P2PS ?
4325 "[PD_PEER_P2PS]" : "",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004326 dev->flags & P2P_DEV_USER_REJECTED ?
4327 "[USER_REJECTED]" : "",
4328 dev->flags & P2P_DEV_PEER_WAITING_RESPONSE ?
4329 "[PEER_WAITING_RESPONSE]" : "",
4330 dev->flags & P2P_DEV_PREFER_PERSISTENT_GROUP ?
4331 "[PREFER_PERSISTENT_GROUP]" : "",
4332 dev->flags & P2P_DEV_WAIT_GO_NEG_RESPONSE ?
4333 "[WAIT_GO_NEG_RESPONSE]" : "",
4334 dev->flags & P2P_DEV_WAIT_GO_NEG_CONFIRM ?
4335 "[WAIT_GO_NEG_CONFIRM]" : "",
4336 dev->flags & P2P_DEV_GROUP_CLIENT_ONLY ?
4337 "[GROUP_CLIENT_ONLY]" : "",
4338 dev->flags & P2P_DEV_FORCE_FREQ ?
4339 "[FORCE_FREQ]" : "",
4340 dev->flags & P2P_DEV_PD_FOR_JOIN ?
4341 "[PD_FOR_JOIN]" : "",
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07004342 dev->flags & P2P_DEV_LAST_SEEN_AS_GROUP_CLIENT ?
4343 "[LAST_SEEN_AS_GROUP_CLIENT]" : "",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004344 dev->status,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004345 dev->invitation_reqs);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004346 if (os_snprintf_error(end - pos, res))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004347 return pos - buf;
4348 pos += res;
4349
4350 if (dev->ext_listen_period) {
4351 res = os_snprintf(pos, end - pos,
4352 "ext_listen_period=%u\n"
4353 "ext_listen_interval=%u\n",
4354 dev->ext_listen_period,
4355 dev->ext_listen_interval);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004356 if (os_snprintf_error(end - pos, res))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004357 return pos - buf;
4358 pos += res;
4359 }
4360
4361 if (dev->oper_ssid_len) {
4362 res = os_snprintf(pos, end - pos,
4363 "oper_ssid=%s\n",
4364 wpa_ssid_txt(dev->oper_ssid,
4365 dev->oper_ssid_len));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004366 if (os_snprintf_error(end - pos, res))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004367 return pos - buf;
4368 pos += res;
4369 }
4370
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004371#ifdef CONFIG_WIFI_DISPLAY
4372 if (dev->info.wfd_subelems) {
4373 res = os_snprintf(pos, end - pos, "wfd_subelems=");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004374 if (os_snprintf_error(end - pos, res))
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004375 return pos - buf;
4376 pos += res;
4377
4378 pos += wpa_snprintf_hex(pos, end - pos,
4379 wpabuf_head(dev->info.wfd_subelems),
4380 wpabuf_len(dev->info.wfd_subelems));
4381
4382 res = os_snprintf(pos, end - pos, "\n");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004383 if (os_snprintf_error(end - pos, res))
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004384 return pos - buf;
4385 pos += res;
4386 }
4387#endif /* CONFIG_WIFI_DISPLAY */
4388
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004389 return pos - buf;
4390}
4391
4392
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004393int p2p_peer_known(struct p2p_data *p2p, const u8 *addr)
4394{
4395 return p2p_get_device(p2p, addr) != NULL;
4396}
4397
4398
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004399void p2p_set_client_discoverability(struct p2p_data *p2p, int enabled)
4400{
4401 if (enabled) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004402 p2p_dbg(p2p, "Client discoverability enabled");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004403 p2p->dev_capab |= P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
4404 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004405 p2p_dbg(p2p, "Client discoverability disabled");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004406 p2p->dev_capab &= ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
4407 }
4408}
4409
4410
4411static struct wpabuf * p2p_build_presence_req(u32 duration1, u32 interval1,
4412 u32 duration2, u32 interval2)
4413{
4414 struct wpabuf *req;
4415 struct p2p_noa_desc desc1, desc2, *ptr1 = NULL, *ptr2 = NULL;
4416 u8 *len;
4417
4418 req = wpabuf_alloc(100);
4419 if (req == NULL)
4420 return NULL;
4421
4422 if (duration1 || interval1) {
4423 os_memset(&desc1, 0, sizeof(desc1));
4424 desc1.count_type = 1;
4425 desc1.duration = duration1;
4426 desc1.interval = interval1;
4427 ptr1 = &desc1;
4428
4429 if (duration2 || interval2) {
4430 os_memset(&desc2, 0, sizeof(desc2));
4431 desc2.count_type = 2;
4432 desc2.duration = duration2;
4433 desc2.interval = interval2;
4434 ptr2 = &desc2;
4435 }
4436 }
4437
4438 p2p_buf_add_action_hdr(req, P2P_PRESENCE_REQ, 1);
4439 len = p2p_buf_add_ie_hdr(req);
4440 p2p_buf_add_noa(req, 0, 0, 0, ptr1, ptr2);
4441 p2p_buf_update_ie_hdr(req, len);
4442
4443 return req;
4444}
4445
4446
4447int p2p_presence_req(struct p2p_data *p2p, const u8 *go_interface_addr,
4448 const u8 *own_interface_addr, unsigned int freq,
4449 u32 duration1, u32 interval1, u32 duration2,
4450 u32 interval2)
4451{
4452 struct wpabuf *req;
4453
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004454 p2p_dbg(p2p, "Send Presence Request to GO " MACSTR
4455 " (own interface " MACSTR ") freq=%u dur1=%u int1=%u "
4456 "dur2=%u int2=%u",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004457 MAC2STR(go_interface_addr), MAC2STR(own_interface_addr),
4458 freq, duration1, interval1, duration2, interval2);
4459
4460 req = p2p_build_presence_req(duration1, interval1, duration2,
4461 interval2);
4462 if (req == NULL)
4463 return -1;
4464
4465 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
4466 if (p2p_send_action(p2p, freq, go_interface_addr, own_interface_addr,
4467 go_interface_addr,
4468 wpabuf_head(req), wpabuf_len(req), 200) < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004469 p2p_dbg(p2p, "Failed to send Action frame");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004470 }
4471 wpabuf_free(req);
4472
4473 return 0;
4474}
4475
4476
4477static struct wpabuf * p2p_build_presence_resp(u8 status, const u8 *noa,
4478 size_t noa_len, u8 dialog_token)
4479{
4480 struct wpabuf *resp;
4481 u8 *len;
4482
4483 resp = wpabuf_alloc(100 + noa_len);
4484 if (resp == NULL)
4485 return NULL;
4486
4487 p2p_buf_add_action_hdr(resp, P2P_PRESENCE_RESP, dialog_token);
4488 len = p2p_buf_add_ie_hdr(resp);
4489 p2p_buf_add_status(resp, status);
4490 if (noa) {
4491 wpabuf_put_u8(resp, P2P_ATTR_NOTICE_OF_ABSENCE);
4492 wpabuf_put_le16(resp, noa_len);
4493 wpabuf_put_data(resp, noa, noa_len);
4494 } else
4495 p2p_buf_add_noa(resp, 0, 0, 0, NULL, NULL);
4496 p2p_buf_update_ie_hdr(resp, len);
4497
4498 return resp;
4499}
4500
4501
4502static void p2p_process_presence_req(struct p2p_data *p2p, const u8 *da,
4503 const u8 *sa, const u8 *data, size_t len,
4504 int rx_freq)
4505{
4506 struct p2p_message msg;
4507 u8 status;
4508 struct wpabuf *resp;
4509 size_t g;
4510 struct p2p_group *group = NULL;
4511 int parsed = 0;
4512 u8 noa[50];
4513 int noa_len;
4514
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004515 p2p_dbg(p2p, "Received P2P Action - P2P Presence Request");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004516
4517 for (g = 0; g < p2p->num_groups; g++) {
4518 if (os_memcmp(da, p2p_group_get_interface_addr(p2p->groups[g]),
4519 ETH_ALEN) == 0) {
4520 group = p2p->groups[g];
4521 break;
4522 }
4523 }
4524 if (group == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004525 p2p_dbg(p2p, "Ignore P2P Presence Request for unknown group "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004526 MACSTR, MAC2STR(da));
4527 return;
4528 }
4529
4530 if (p2p_parse(data, len, &msg) < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004531 p2p_dbg(p2p, "Failed to parse P2P Presence Request");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004532 status = P2P_SC_FAIL_INVALID_PARAMS;
4533 goto fail;
4534 }
4535 parsed = 1;
4536
4537 if (msg.noa == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004538 p2p_dbg(p2p, "No NoA attribute in P2P Presence Request");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004539 status = P2P_SC_FAIL_INVALID_PARAMS;
4540 goto fail;
4541 }
4542
4543 status = p2p_group_presence_req(group, sa, msg.noa, msg.noa_len);
4544
4545fail:
4546 if (p2p->cfg->get_noa)
4547 noa_len = p2p->cfg->get_noa(p2p->cfg->cb_ctx, da, noa,
4548 sizeof(noa));
4549 else
4550 noa_len = -1;
4551 resp = p2p_build_presence_resp(status, noa_len > 0 ? noa : NULL,
4552 noa_len > 0 ? noa_len : 0,
4553 msg.dialog_token);
4554 if (parsed)
4555 p2p_parse_free(&msg);
4556 if (resp == NULL)
4557 return;
4558
4559 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
4560 if (p2p_send_action(p2p, rx_freq, sa, da, da,
4561 wpabuf_head(resp), wpabuf_len(resp), 200) < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004562 p2p_dbg(p2p, "Failed to send Action frame");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004563 }
4564 wpabuf_free(resp);
4565}
4566
4567
4568static void p2p_process_presence_resp(struct p2p_data *p2p, const u8 *da,
4569 const u8 *sa, const u8 *data, size_t len)
4570{
4571 struct p2p_message msg;
4572
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004573 p2p_dbg(p2p, "Received P2P Action - P2P Presence Response");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004574
4575 if (p2p_parse(data, len, &msg) < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004576 p2p_dbg(p2p, "Failed to parse P2P Presence Response");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004577 return;
4578 }
4579
4580 if (msg.status == NULL || msg.noa == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004581 p2p_dbg(p2p, "No Status or NoA attribute in P2P Presence Response");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004582 p2p_parse_free(&msg);
4583 return;
4584 }
4585
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004586 if (p2p->cfg->presence_resp) {
4587 p2p->cfg->presence_resp(p2p->cfg->cb_ctx, sa, *msg.status,
4588 msg.noa, msg.noa_len);
4589 }
4590
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004591 if (*msg.status) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004592 p2p_dbg(p2p, "P2P Presence Request was rejected: status %u",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004593 *msg.status);
4594 p2p_parse_free(&msg);
4595 return;
4596 }
4597
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004598 p2p_dbg(p2p, "P2P Presence Request was accepted");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004599 wpa_hexdump(MSG_DEBUG, "P2P: P2P Presence Response - NoA",
4600 msg.noa, msg.noa_len);
4601 /* TODO: process NoA */
4602 p2p_parse_free(&msg);
4603}
4604
4605
4606static void p2p_ext_listen_timeout(void *eloop_ctx, void *timeout_ctx)
4607{
4608 struct p2p_data *p2p = eloop_ctx;
4609
4610 if (p2p->ext_listen_interval) {
4611 /* Schedule next extended listen timeout */
4612 eloop_register_timeout(p2p->ext_listen_interval_sec,
4613 p2p->ext_listen_interval_usec,
4614 p2p_ext_listen_timeout, p2p, NULL);
4615 }
4616
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -07004617 if ((p2p->cfg->is_p2p_in_progress &&
4618 p2p->cfg->is_p2p_in_progress(p2p->cfg->cb_ctx)) ||
4619 (p2p->pending_action_state == P2P_PENDING_PD &&
4620 p2p->pd_retries > 0)) {
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -07004621 p2p_dbg(p2p, "Operation in progress - skip Extended Listen timeout (%s)",
4622 p2p_state_txt(p2p->state));
4623 return;
4624 }
4625
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004626 if (p2p->state == P2P_LISTEN_ONLY && p2p->ext_listen_only) {
4627 /*
4628 * This should not really happen, but it looks like the Listen
4629 * command may fail is something else (e.g., a scan) was
4630 * running at an inconvenient time. As a workaround, allow new
4631 * Extended Listen operation to be started.
4632 */
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004633 p2p_dbg(p2p, "Previous Extended Listen operation had not been completed - try again");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004634 p2p->ext_listen_only = 0;
4635 p2p_set_state(p2p, P2P_IDLE);
4636 }
4637
4638 if (p2p->state != P2P_IDLE) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004639 p2p_dbg(p2p, "Skip Extended Listen timeout in active state (%s)", p2p_state_txt(p2p->state));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004640 return;
4641 }
4642
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004643 p2p_dbg(p2p, "Extended Listen timeout");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004644 p2p->ext_listen_only = 1;
4645 if (p2p_listen(p2p, p2p->ext_listen_period) < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004646 p2p_dbg(p2p, "Failed to start Listen state for Extended Listen Timing");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004647 p2p->ext_listen_only = 0;
4648 }
4649}
4650
4651
4652int p2p_ext_listen(struct p2p_data *p2p, unsigned int period,
4653 unsigned int interval)
4654{
4655 if (period > 65535 || interval > 65535 || period > interval ||
4656 (period == 0 && interval > 0) || (period > 0 && interval == 0)) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004657 p2p_dbg(p2p, "Invalid Extended Listen Timing request: period=%u interval=%u",
4658 period, interval);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004659 return -1;
4660 }
4661
4662 eloop_cancel_timeout(p2p_ext_listen_timeout, p2p, NULL);
4663
4664 if (interval == 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004665 p2p_dbg(p2p, "Disabling Extended Listen Timing");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004666 p2p->ext_listen_period = 0;
4667 p2p->ext_listen_interval = 0;
4668 return 0;
4669 }
4670
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004671 p2p_dbg(p2p, "Enabling Extended Listen Timing: period %u msec, interval %u msec",
4672 period, interval);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004673 p2p->ext_listen_period = period;
4674 p2p->ext_listen_interval = interval;
4675 p2p->ext_listen_interval_sec = interval / 1000;
4676 p2p->ext_listen_interval_usec = (interval % 1000) * 1000;
4677
4678 eloop_register_timeout(p2p->ext_listen_interval_sec,
4679 p2p->ext_listen_interval_usec,
4680 p2p_ext_listen_timeout, p2p, NULL);
4681
4682 return 0;
4683}
4684
4685
4686void p2p_deauth_notif(struct p2p_data *p2p, const u8 *bssid, u16 reason_code,
4687 const u8 *ie, size_t ie_len)
4688{
4689 struct p2p_message msg;
4690
4691 if (bssid == NULL || ie == NULL)
4692 return;
4693
4694 os_memset(&msg, 0, sizeof(msg));
4695 if (p2p_parse_ies(ie, ie_len, &msg))
4696 return;
Dmitry Shmidt97672262014-02-03 13:02:54 -08004697 if (msg.minor_reason_code == NULL) {
4698 p2p_parse_free(&msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004699 return;
Dmitry Shmidt97672262014-02-03 13:02:54 -08004700 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004701
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004702 p2p_dbg(p2p, "Deauthentication notification BSSID " MACSTR
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004703 " reason_code=%u minor_reason_code=%u",
4704 MAC2STR(bssid), reason_code, *msg.minor_reason_code);
4705
4706 p2p_parse_free(&msg);
4707}
4708
4709
4710void p2p_disassoc_notif(struct p2p_data *p2p, const u8 *bssid, u16 reason_code,
4711 const u8 *ie, size_t ie_len)
4712{
4713 struct p2p_message msg;
4714
4715 if (bssid == NULL || ie == NULL)
4716 return;
4717
4718 os_memset(&msg, 0, sizeof(msg));
4719 if (p2p_parse_ies(ie, ie_len, &msg))
4720 return;
Dmitry Shmidt97672262014-02-03 13:02:54 -08004721 if (msg.minor_reason_code == NULL) {
4722 p2p_parse_free(&msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004723 return;
Dmitry Shmidt97672262014-02-03 13:02:54 -08004724 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004725
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004726 p2p_dbg(p2p, "Disassociation notification BSSID " MACSTR
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004727 " reason_code=%u minor_reason_code=%u",
4728 MAC2STR(bssid), reason_code, *msg.minor_reason_code);
4729
4730 p2p_parse_free(&msg);
4731}
4732
4733
4734void p2p_set_managed_oper(struct p2p_data *p2p, int enabled)
4735{
4736 if (enabled) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004737 p2p_dbg(p2p, "Managed P2P Device operations enabled");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004738 p2p->dev_capab |= P2P_DEV_CAPAB_INFRA_MANAGED;
4739 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004740 p2p_dbg(p2p, "Managed P2P Device operations disabled");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004741 p2p->dev_capab &= ~P2P_DEV_CAPAB_INFRA_MANAGED;
4742 }
4743}
4744
4745
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004746int p2p_config_get_random_social(struct p2p_config *p2p, u8 *op_class,
Hai Shalom74f70d42019-02-11 14:42:39 -08004747 u8 *op_channel,
4748 struct wpa_freq_range_list *avoid_list,
4749 struct wpa_freq_range_list *disallow_list)
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004750{
Hai Shalom74f70d42019-02-11 14:42:39 -08004751 return p2p_channel_random_social(&p2p->channels, op_class, op_channel,
4752 avoid_list, disallow_list);
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004753}
4754
4755
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004756int p2p_set_listen_channel(struct p2p_data *p2p, u8 reg_class, u8 channel,
4757 u8 forced)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004758{
Dmitry Shmidt4b060592013-04-29 16:42:49 -07004759 if (p2p_channel_to_freq(reg_class, channel) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004760 return -1;
4761
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004762 /*
4763 * Listen channel was set in configuration or set by control interface;
4764 * cannot override it.
4765 */
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004766 if (p2p->cfg->channel_forced && forced == 0) {
4767 p2p_dbg(p2p,
4768 "Listen channel was previously configured - do not override based on optimization");
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004769 return -1;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004770 }
4771
4772 p2p_dbg(p2p, "Set Listen channel: reg_class %u channel %u",
4773 reg_class, channel);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004774
4775 if (p2p->state == P2P_IDLE) {
4776 p2p->cfg->reg_class = reg_class;
4777 p2p->cfg->channel = channel;
4778 p2p->cfg->channel_forced = forced;
4779 } else {
4780 p2p_dbg(p2p, "Defer setting listen channel");
4781 p2p->pending_reg_class = reg_class;
4782 p2p->pending_channel = channel;
4783 p2p->pending_channel_forced = forced;
4784 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004785
4786 return 0;
4787}
4788
4789
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004790u8 p2p_get_listen_channel(struct p2p_data *p2p)
4791{
4792 return p2p->cfg->channel;
4793}
4794
4795
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004796int p2p_set_ssid_postfix(struct p2p_data *p2p, const u8 *postfix, size_t len)
4797{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004798 p2p_dbg(p2p, "New SSID postfix: %s", wpa_ssid_txt(postfix, len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004799 if (postfix == NULL) {
4800 p2p->cfg->ssid_postfix_len = 0;
4801 return 0;
4802 }
4803 if (len > sizeof(p2p->cfg->ssid_postfix))
4804 return -1;
4805 os_memcpy(p2p->cfg->ssid_postfix, postfix, len);
4806 p2p->cfg->ssid_postfix_len = len;
4807 return 0;
4808}
4809
4810
Jouni Malinen75ecf522011-06-27 15:19:46 -07004811int p2p_set_oper_channel(struct p2p_data *p2p, u8 op_reg_class, u8 op_channel,
4812 int cfg_op_channel)
4813{
Dmitry Shmidt4b060592013-04-29 16:42:49 -07004814 if (p2p_channel_to_freq(op_reg_class, op_channel) < 0)
Jouni Malinen75ecf522011-06-27 15:19:46 -07004815 return -1;
4816
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004817 p2p_dbg(p2p, "Set Operating channel: reg_class %u channel %u",
4818 op_reg_class, op_channel);
Jouni Malinen75ecf522011-06-27 15:19:46 -07004819 p2p->cfg->op_reg_class = op_reg_class;
4820 p2p->cfg->op_channel = op_channel;
4821 p2p->cfg->cfg_op_channel = cfg_op_channel;
4822 return 0;
4823}
4824
4825
Dmitry Shmidt04949592012-07-19 12:16:46 -07004826int p2p_set_pref_chan(struct p2p_data *p2p, unsigned int num_pref_chan,
4827 const struct p2p_channel *pref_chan)
4828{
4829 struct p2p_channel *n;
4830
4831 if (pref_chan) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004832 n = os_memdup(pref_chan,
4833 num_pref_chan * sizeof(struct p2p_channel));
Dmitry Shmidt04949592012-07-19 12:16:46 -07004834 if (n == NULL)
4835 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004836 } else
4837 n = NULL;
4838
4839 os_free(p2p->cfg->pref_chan);
4840 p2p->cfg->pref_chan = n;
4841 p2p->cfg->num_pref_chan = num_pref_chan;
4842
4843 return 0;
4844}
4845
4846
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004847int p2p_set_no_go_freq(struct p2p_data *p2p,
4848 const struct wpa_freq_range_list *list)
4849{
4850 struct wpa_freq_range *tmp;
4851
4852 if (list == NULL || list->num == 0) {
4853 os_free(p2p->no_go_freq.range);
4854 p2p->no_go_freq.range = NULL;
4855 p2p->no_go_freq.num = 0;
4856 return 0;
4857 }
4858
4859 tmp = os_calloc(list->num, sizeof(struct wpa_freq_range));
4860 if (tmp == NULL)
4861 return -1;
4862 os_memcpy(tmp, list->range, list->num * sizeof(struct wpa_freq_range));
4863 os_free(p2p->no_go_freq.range);
4864 p2p->no_go_freq.range = tmp;
4865 p2p->no_go_freq.num = list->num;
4866 p2p_dbg(p2p, "Updated no GO chan list");
4867
4868 return 0;
4869}
4870
4871
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004872int p2p_get_interface_addr(struct p2p_data *p2p, const u8 *dev_addr,
4873 u8 *iface_addr)
4874{
4875 struct p2p_device *dev = p2p_get_device(p2p, dev_addr);
4876 if (dev == NULL || is_zero_ether_addr(dev->interface_addr))
4877 return -1;
4878 os_memcpy(iface_addr, dev->interface_addr, ETH_ALEN);
4879 return 0;
4880}
4881
4882
4883int p2p_get_dev_addr(struct p2p_data *p2p, const u8 *iface_addr,
4884 u8 *dev_addr)
4885{
4886 struct p2p_device *dev = p2p_get_device_interface(p2p, iface_addr);
4887 if (dev == NULL)
4888 return -1;
4889 os_memcpy(dev_addr, dev->info.p2p_device_addr, ETH_ALEN);
4890 return 0;
4891}
4892
4893
4894void p2p_set_peer_filter(struct p2p_data *p2p, const u8 *addr)
4895{
4896 os_memcpy(p2p->peer_filter, addr, ETH_ALEN);
4897 if (is_zero_ether_addr(p2p->peer_filter))
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004898 p2p_dbg(p2p, "Disable peer filter");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004899 else
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004900 p2p_dbg(p2p, "Enable peer filter for " MACSTR,
4901 MAC2STR(p2p->peer_filter));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004902}
4903
4904
4905void p2p_set_cross_connect(struct p2p_data *p2p, int enabled)
4906{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004907 p2p_dbg(p2p, "Cross connection %s", enabled ? "enabled" : "disabled");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004908 if (p2p->cross_connect == enabled)
4909 return;
4910 p2p->cross_connect = enabled;
4911 /* TODO: may need to tear down any action group where we are GO(?) */
4912}
4913
4914
4915int p2p_get_oper_freq(struct p2p_data *p2p, const u8 *iface_addr)
4916{
4917 struct p2p_device *dev = p2p_get_device_interface(p2p, iface_addr);
4918 if (dev == NULL)
4919 return -1;
4920 if (dev->oper_freq <= 0)
4921 return -1;
4922 return dev->oper_freq;
4923}
4924
4925
4926void p2p_set_intra_bss_dist(struct p2p_data *p2p, int enabled)
4927{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004928 p2p_dbg(p2p, "Intra BSS distribution %s",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004929 enabled ? "enabled" : "disabled");
4930 p2p->cfg->p2p_intra_bss = enabled;
4931}
4932
4933
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004934void p2p_update_channel_list(struct p2p_data *p2p,
4935 const struct p2p_channels *chan,
4936 const struct p2p_channels *cli_chan)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004937{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004938 p2p_dbg(p2p, "Update channel list");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004939 os_memcpy(&p2p->cfg->channels, chan, sizeof(struct p2p_channels));
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004940 p2p_channels_dump(p2p, "channels", &p2p->cfg->channels);
4941 os_memcpy(&p2p->cfg->cli_channels, cli_chan,
4942 sizeof(struct p2p_channels));
4943 p2p_channels_dump(p2p, "cli_channels", &p2p->cfg->cli_channels);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004944}
4945
4946
4947int p2p_send_action(struct p2p_data *p2p, unsigned int freq, const u8 *dst,
4948 const u8 *src, const u8 *bssid, const u8 *buf,
4949 size_t len, unsigned int wait_time)
4950{
Hai Shalom021b0b52019-04-10 11:17:58 -07004951 int res, scheduled;
4952
Hai Shalom021b0b52019-04-10 11:17:58 -07004953 res = p2p->cfg->send_action(p2p->cfg->cb_ctx, freq, dst, src, bssid,
4954 buf, len, wait_time, &scheduled);
4955 if (res == 0 && scheduled && p2p->in_listen && freq > 0 &&
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08004956 p2p->drv_in_listen > 0 &&
Hai Shalom021b0b52019-04-10 11:17:58 -07004957 (unsigned int) p2p->drv_in_listen != freq) {
4958 p2p_dbg(p2p,
4959 "Stop listen on %d MHz to allow a frame to be sent immediately on %d MHz",
4960 p2p->drv_in_listen, freq);
4961 p2p_stop_listen_for_freq(p2p, freq);
4962 }
4963 return res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004964}
4965
4966
4967void p2p_set_best_channels(struct p2p_data *p2p, int freq_24, int freq_5,
4968 int freq_overall)
4969{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004970 p2p_dbg(p2p, "Best channel: 2.4 GHz: %d, 5 GHz: %d, overall: %d",
4971 freq_24, freq_5, freq_overall);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004972 p2p->best_freq_24 = freq_24;
4973 p2p->best_freq_5 = freq_5;
4974 p2p->best_freq_overall = freq_overall;
4975}
4976
4977
Dmitry Shmidt700a1372013-03-15 14:14:44 -07004978void p2p_set_own_freq_preference(struct p2p_data *p2p, int freq)
4979{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004980 p2p_dbg(p2p, "Own frequency preference: %d MHz", freq);
Dmitry Shmidt700a1372013-03-15 14:14:44 -07004981 p2p->own_freq_preference = freq;
4982}
4983
4984
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004985const u8 * p2p_get_go_neg_peer(struct p2p_data *p2p)
4986{
4987 if (p2p == NULL || p2p->go_neg_peer == NULL)
4988 return NULL;
4989 return p2p->go_neg_peer->info.p2p_device_addr;
4990}
4991
4992
4993const struct p2p_peer_info *
4994p2p_get_peer_found(struct p2p_data *p2p, const u8 *addr, int next)
4995{
4996 struct p2p_device *dev;
4997
4998 if (addr) {
4999 dev = p2p_get_device(p2p, addr);
5000 if (!dev)
5001 return NULL;
5002
5003 if (!next) {
5004 if (dev->flags & P2P_DEV_PROBE_REQ_ONLY)
5005 return NULL;
5006
5007 return &dev->info;
5008 } else {
5009 do {
5010 dev = dl_list_first(&dev->list,
5011 struct p2p_device,
5012 list);
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07005013 if (!dev || &dev->list == &p2p->devices)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005014 return NULL;
5015 } while (dev->flags & P2P_DEV_PROBE_REQ_ONLY);
5016 }
5017 } else {
5018 dev = dl_list_first(&p2p->devices, struct p2p_device, list);
5019 if (!dev)
5020 return NULL;
5021 while (dev->flags & P2P_DEV_PROBE_REQ_ONLY) {
5022 dev = dl_list_first(&dev->list,
5023 struct p2p_device,
5024 list);
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07005025 if (!dev || &dev->list == &p2p->devices)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005026 return NULL;
5027 }
5028 }
5029
5030 return &dev->info;
5031}
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005032
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005033
5034int p2p_in_progress(struct p2p_data *p2p)
5035{
5036 if (p2p == NULL)
5037 return 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005038 if (p2p->state == P2P_SEARCH)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005039 return 2;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005040 return p2p->state != P2P_IDLE && p2p->state != P2P_PROVISIONING;
5041}
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005042
5043
5044void p2p_set_config_timeout(struct p2p_data *p2p, u8 go_timeout,
5045 u8 client_timeout)
5046{
5047 if (p2p) {
5048 p2p->go_timeout = go_timeout;
5049 p2p->client_timeout = client_timeout;
5050 }
5051}
5052
5053
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005054#ifdef CONFIG_WIFI_DISPLAY
5055
5056static void p2p_update_wfd_ie_groups(struct p2p_data *p2p)
5057{
5058 size_t g;
5059 struct p2p_group *group;
5060
5061 for (g = 0; g < p2p->num_groups; g++) {
5062 group = p2p->groups[g];
Dmitry Shmidtb96dad42013-11-05 10:07:29 -08005063 p2p_group_force_beacon_update_ies(group);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005064 }
5065}
5066
5067
5068int p2p_set_wfd_ie_beacon(struct p2p_data *p2p, struct wpabuf *ie)
5069{
5070 wpabuf_free(p2p->wfd_ie_beacon);
5071 p2p->wfd_ie_beacon = ie;
5072 p2p_update_wfd_ie_groups(p2p);
5073 return 0;
5074}
5075
5076
5077int p2p_set_wfd_ie_probe_req(struct p2p_data *p2p, struct wpabuf *ie)
5078{
5079 wpabuf_free(p2p->wfd_ie_probe_req);
5080 p2p->wfd_ie_probe_req = ie;
5081 return 0;
5082}
5083
5084
5085int p2p_set_wfd_ie_probe_resp(struct p2p_data *p2p, struct wpabuf *ie)
5086{
5087 wpabuf_free(p2p->wfd_ie_probe_resp);
5088 p2p->wfd_ie_probe_resp = ie;
5089 p2p_update_wfd_ie_groups(p2p);
5090 return 0;
5091}
5092
5093
5094int p2p_set_wfd_ie_assoc_req(struct p2p_data *p2p, struct wpabuf *ie)
5095{
5096 wpabuf_free(p2p->wfd_ie_assoc_req);
5097 p2p->wfd_ie_assoc_req = ie;
5098 return 0;
5099}
5100
5101
5102int p2p_set_wfd_ie_invitation(struct p2p_data *p2p, struct wpabuf *ie)
5103{
5104 wpabuf_free(p2p->wfd_ie_invitation);
5105 p2p->wfd_ie_invitation = ie;
5106 return 0;
5107}
5108
5109
5110int p2p_set_wfd_ie_prov_disc_req(struct p2p_data *p2p, struct wpabuf *ie)
5111{
5112 wpabuf_free(p2p->wfd_ie_prov_disc_req);
5113 p2p->wfd_ie_prov_disc_req = ie;
5114 return 0;
5115}
5116
5117
5118int p2p_set_wfd_ie_prov_disc_resp(struct p2p_data *p2p, struct wpabuf *ie)
5119{
5120 wpabuf_free(p2p->wfd_ie_prov_disc_resp);
5121 p2p->wfd_ie_prov_disc_resp = ie;
5122 return 0;
5123}
5124
5125
5126int p2p_set_wfd_ie_go_neg(struct p2p_data *p2p, struct wpabuf *ie)
5127{
5128 wpabuf_free(p2p->wfd_ie_go_neg);
5129 p2p->wfd_ie_go_neg = ie;
5130 return 0;
5131}
5132
5133
5134int p2p_set_wfd_dev_info(struct p2p_data *p2p, const struct wpabuf *elem)
5135{
5136 wpabuf_free(p2p->wfd_dev_info);
5137 if (elem) {
5138 p2p->wfd_dev_info = wpabuf_dup(elem);
5139 if (p2p->wfd_dev_info == NULL)
5140 return -1;
5141 } else
5142 p2p->wfd_dev_info = NULL;
5143
5144 return 0;
5145}
5146
5147
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005148int p2p_set_wfd_r2_dev_info(struct p2p_data *p2p, const struct wpabuf *elem)
5149{
5150 wpabuf_free(p2p->wfd_r2_dev_info);
5151 if (elem) {
5152 p2p->wfd_r2_dev_info = wpabuf_dup(elem);
5153 if (p2p->wfd_r2_dev_info == NULL)
5154 return -1;
5155 } else
5156 p2p->wfd_r2_dev_info = NULL;
5157
5158 return 0;
5159}
5160
5161
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005162int p2p_set_wfd_assoc_bssid(struct p2p_data *p2p, const struct wpabuf *elem)
5163{
5164 wpabuf_free(p2p->wfd_assoc_bssid);
5165 if (elem) {
5166 p2p->wfd_assoc_bssid = wpabuf_dup(elem);
5167 if (p2p->wfd_assoc_bssid == NULL)
5168 return -1;
5169 } else
5170 p2p->wfd_assoc_bssid = NULL;
5171
5172 return 0;
5173}
5174
5175
5176int p2p_set_wfd_coupled_sink_info(struct p2p_data *p2p,
5177 const struct wpabuf *elem)
5178{
5179 wpabuf_free(p2p->wfd_coupled_sink_info);
5180 if (elem) {
5181 p2p->wfd_coupled_sink_info = wpabuf_dup(elem);
5182 if (p2p->wfd_coupled_sink_info == NULL)
5183 return -1;
5184 } else
5185 p2p->wfd_coupled_sink_info = NULL;
5186
5187 return 0;
5188}
5189
5190#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005191
5192
5193int p2p_set_disc_int(struct p2p_data *p2p, int min_disc_int, int max_disc_int,
5194 int max_disc_tu)
5195{
5196 if (min_disc_int > max_disc_int || min_disc_int < 0 || max_disc_int < 0)
5197 return -1;
5198
5199 p2p->min_disc_int = min_disc_int;
5200 p2p->max_disc_int = max_disc_int;
5201 p2p->max_disc_tu = max_disc_tu;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005202 p2p_dbg(p2p, "Set discoverable interval: min=%d max=%d max_tu=%d",
5203 min_disc_int, max_disc_int, max_disc_tu);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005204
5205 return 0;
5206}
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005207
5208
5209void p2p_dbg(struct p2p_data *p2p, const char *fmt, ...)
5210{
5211 va_list ap;
5212 char buf[500];
5213
5214 if (!p2p->cfg->debug_print)
5215 return;
5216
5217 va_start(ap, fmt);
5218 vsnprintf(buf, sizeof(buf), fmt, ap);
5219 buf[sizeof(buf) - 1] = '\0';
5220 va_end(ap);
5221 p2p->cfg->debug_print(p2p->cfg->cb_ctx, MSG_DEBUG, buf);
5222}
5223
5224
5225void p2p_info(struct p2p_data *p2p, const char *fmt, ...)
5226{
5227 va_list ap;
5228 char buf[500];
5229
5230 if (!p2p->cfg->debug_print)
5231 return;
5232
5233 va_start(ap, fmt);
5234 vsnprintf(buf, sizeof(buf), fmt, ap);
5235 buf[sizeof(buf) - 1] = '\0';
5236 va_end(ap);
5237 p2p->cfg->debug_print(p2p->cfg->cb_ctx, MSG_INFO, buf);
5238}
5239
5240
5241void p2p_err(struct p2p_data *p2p, const char *fmt, ...)
5242{
5243 va_list ap;
5244 char buf[500];
5245
5246 if (!p2p->cfg->debug_print)
5247 return;
5248
5249 va_start(ap, fmt);
5250 vsnprintf(buf, sizeof(buf), fmt, ap);
5251 buf[sizeof(buf) - 1] = '\0';
5252 va_end(ap);
5253 p2p->cfg->debug_print(p2p->cfg->cb_ctx, MSG_ERROR, buf);
5254}
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005255
5256
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07005257void p2p_loop_on_known_peers(struct p2p_data *p2p,
5258 void (*peer_callback)(struct p2p_peer_info *peer,
5259 void *user_data),
5260 void *user_data)
5261{
5262 struct p2p_device *dev, *n;
5263
5264 dl_list_for_each_safe(dev, n, &p2p->devices, struct p2p_device, list) {
5265 peer_callback(&dev->info, user_data);
5266 }
5267}
5268
5269
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005270#ifdef CONFIG_WPS_NFC
5271
5272static struct wpabuf * p2p_build_nfc_handover(struct p2p_data *p2p,
5273 int client_freq,
5274 const u8 *go_dev_addr,
5275 const u8 *ssid, size_t ssid_len)
5276{
5277 struct wpabuf *buf;
5278 u8 op_class, channel;
5279 enum p2p_role_indication role = P2P_DEVICE_NOT_IN_GROUP;
5280
5281 buf = wpabuf_alloc(1000);
5282 if (buf == NULL)
5283 return NULL;
5284
5285 op_class = p2p->cfg->reg_class;
5286 channel = p2p->cfg->channel;
5287
5288 p2p_buf_add_capability(buf, p2p->dev_capab &
5289 ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY, 0);
5290 p2p_buf_add_device_info(buf, p2p, NULL);
5291
5292 if (p2p->num_groups > 0) {
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07005293 int freq = p2p_group_get_freq(p2p->groups[0]);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005294 role = P2P_GO_IN_A_GROUP;
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07005295 if (p2p_freq_to_channel(freq, &op_class, &channel) < 0) {
5296 p2p_dbg(p2p,
5297 "Unknown GO operating frequency %d MHz for NFC handover",
5298 freq);
5299 wpabuf_free(buf);
5300 return NULL;
5301 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005302 } else if (client_freq > 0) {
5303 role = P2P_CLIENT_IN_A_GROUP;
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07005304 if (p2p_freq_to_channel(client_freq, &op_class, &channel) < 0) {
5305 p2p_dbg(p2p,
5306 "Unknown client operating frequency %d MHz for NFC handover",
5307 client_freq);
5308 wpabuf_free(buf);
5309 return NULL;
5310 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005311 }
5312
5313 p2p_buf_add_oob_go_neg_channel(buf, p2p->cfg->country, op_class,
5314 channel, role);
5315
5316 if (p2p->num_groups > 0) {
5317 /* Limit number of clients to avoid very long message */
5318 p2p_buf_add_group_info(p2p->groups[0], buf, 5);
5319 p2p_group_buf_add_id(p2p->groups[0], buf);
5320 } else if (client_freq > 0 &&
5321 go_dev_addr && !is_zero_ether_addr(go_dev_addr) &&
5322 ssid && ssid_len > 0) {
5323 /*
5324 * Add the optional P2P Group ID to indicate in which group this
5325 * device is a P2P Client.
5326 */
5327 p2p_buf_add_group_id(buf, go_dev_addr, ssid, ssid_len);
5328 }
5329
5330 return buf;
5331}
5332
5333
5334struct wpabuf * p2p_build_nfc_handover_req(struct p2p_data *p2p,
5335 int client_freq,
5336 const u8 *go_dev_addr,
5337 const u8 *ssid, size_t ssid_len)
5338{
5339 return p2p_build_nfc_handover(p2p, client_freq, go_dev_addr, ssid,
5340 ssid_len);
5341}
5342
5343
5344struct wpabuf * p2p_build_nfc_handover_sel(struct p2p_data *p2p,
5345 int client_freq,
5346 const u8 *go_dev_addr,
5347 const u8 *ssid, size_t ssid_len)
5348{
5349 return p2p_build_nfc_handover(p2p, client_freq, go_dev_addr, ssid,
5350 ssid_len);
5351}
5352
5353
5354int p2p_process_nfc_connection_handover(struct p2p_data *p2p,
5355 struct p2p_nfc_params *params)
5356{
5357 struct p2p_message msg;
5358 struct p2p_device *dev;
5359 const u8 *p2p_dev_addr;
5360 int freq;
5361 enum p2p_role_indication role;
5362
5363 params->next_step = NO_ACTION;
5364
5365 if (p2p_parse_ies_separate(params->wsc_attr, params->wsc_len,
5366 params->p2p_attr, params->p2p_len, &msg)) {
5367 p2p_dbg(p2p, "Failed to parse WSC/P2P attributes from NFC");
5368 p2p_parse_free(&msg);
5369 return -1;
5370 }
5371
5372 if (msg.p2p_device_addr)
5373 p2p_dev_addr = msg.p2p_device_addr;
5374 else if (msg.device_id)
5375 p2p_dev_addr = msg.device_id;
5376 else {
5377 p2p_dbg(p2p, "Ignore scan data without P2P Device Info or P2P Device Id");
5378 p2p_parse_free(&msg);
5379 return -1;
5380 }
5381
5382 if (msg.oob_dev_password) {
5383 os_memcpy(params->oob_dev_pw, msg.oob_dev_password,
5384 msg.oob_dev_password_len);
5385 params->oob_dev_pw_len = msg.oob_dev_password_len;
5386 }
5387
5388 dev = p2p_create_device(p2p, p2p_dev_addr);
5389 if (dev == NULL) {
5390 p2p_parse_free(&msg);
5391 return -1;
5392 }
5393
5394 params->peer = &dev->info;
5395
5396 os_get_reltime(&dev->last_seen);
5397 dev->flags &= ~(P2P_DEV_PROBE_REQ_ONLY | P2P_DEV_GROUP_CLIENT_ONLY);
5398 p2p_copy_wps_info(p2p, dev, 0, &msg);
5399
5400 if (!msg.oob_go_neg_channel) {
5401 p2p_dbg(p2p, "OOB GO Negotiation Channel attribute not included");
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07005402 p2p_parse_free(&msg);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005403 return -1;
5404 }
5405
5406 if (msg.oob_go_neg_channel[3] == 0 &&
5407 msg.oob_go_neg_channel[4] == 0)
5408 freq = 0;
5409 else
5410 freq = p2p_channel_to_freq(msg.oob_go_neg_channel[3],
5411 msg.oob_go_neg_channel[4]);
5412 if (freq < 0) {
5413 p2p_dbg(p2p, "Unknown peer OOB GO Neg channel");
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07005414 p2p_parse_free(&msg);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005415 return -1;
5416 }
5417 role = msg.oob_go_neg_channel[5];
5418
5419 if (role == P2P_GO_IN_A_GROUP) {
5420 p2p_dbg(p2p, "Peer OOB GO operating channel: %u MHz", freq);
5421 params->go_freq = freq;
5422 } else if (role == P2P_CLIENT_IN_A_GROUP) {
5423 p2p_dbg(p2p, "Peer (client) OOB GO operating channel: %u MHz",
5424 freq);
5425 params->go_freq = freq;
5426 } else
5427 p2p_dbg(p2p, "Peer OOB GO Neg channel: %u MHz", freq);
5428 dev->oob_go_neg_freq = freq;
5429
5430 if (!params->sel && role != P2P_GO_IN_A_GROUP) {
5431 freq = p2p_channel_to_freq(p2p->cfg->reg_class,
5432 p2p->cfg->channel);
5433 if (freq < 0) {
5434 p2p_dbg(p2p, "Own listen channel not known");
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07005435 p2p_parse_free(&msg);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005436 return -1;
5437 }
5438 p2p_dbg(p2p, "Use own Listen channel as OOB GO Neg channel: %u MHz", freq);
5439 dev->oob_go_neg_freq = freq;
5440 }
5441
5442 if (msg.group_id) {
5443 os_memcpy(params->go_dev_addr, msg.group_id, ETH_ALEN);
5444 params->go_ssid_len = msg.group_id_len - ETH_ALEN;
5445 os_memcpy(params->go_ssid, msg.group_id + ETH_ALEN,
5446 params->go_ssid_len);
5447 }
5448
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005449 if (dev->flags & P2P_DEV_USER_REJECTED) {
5450 p2p_dbg(p2p, "Do not report rejected device");
Dmitry Shmidt71757432014-06-02 13:50:35 -07005451 p2p_parse_free(&msg);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005452 return 0;
5453 }
5454
5455 if (!(dev->flags & P2P_DEV_REPORTED)) {
5456 p2p->cfg->dev_found(p2p->cfg->cb_ctx, p2p_dev_addr, &dev->info,
5457 !(dev->flags & P2P_DEV_REPORTED_ONCE));
5458 dev->flags |= P2P_DEV_REPORTED | P2P_DEV_REPORTED_ONCE;
5459 }
Dmitry Shmidt71757432014-06-02 13:50:35 -07005460 p2p_parse_free(&msg);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005461
5462 if (role == P2P_GO_IN_A_GROUP && p2p->num_groups > 0)
5463 params->next_step = BOTH_GO;
5464 else if (role == P2P_GO_IN_A_GROUP)
5465 params->next_step = JOIN_GROUP;
5466 else if (role == P2P_CLIENT_IN_A_GROUP) {
5467 dev->flags |= P2P_DEV_GROUP_CLIENT_ONLY;
5468 params->next_step = PEER_CLIENT;
5469 } else if (p2p->num_groups > 0)
5470 params->next_step = AUTH_JOIN;
5471 else if (params->sel)
5472 params->next_step = INIT_GO_NEG;
5473 else
5474 params->next_step = RESP_GO_NEG;
5475
5476 return 0;
5477}
5478
5479
5480void p2p_set_authorized_oob_dev_pw_id(struct p2p_data *p2p, u16 dev_pw_id,
5481 int go_intent,
5482 const u8 *own_interface_addr)
5483{
5484
5485 p2p->authorized_oob_dev_pw_id = dev_pw_id;
5486 if (dev_pw_id == 0) {
5487 p2p_dbg(p2p, "NFC OOB Password unauthorized for static handover");
5488 return;
5489 }
5490
5491 p2p_dbg(p2p, "NFC OOB Password (id=%u) authorized for static handover",
5492 dev_pw_id);
5493
5494 p2p->go_intent = go_intent;
5495 os_memcpy(p2p->intended_addr, own_interface_addr, ETH_ALEN);
5496}
5497
5498#endif /* CONFIG_WPS_NFC */
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07005499
5500
5501int p2p_set_passphrase_len(struct p2p_data *p2p, unsigned int len)
5502{
5503 if (len < 8 || len > 63)
5504 return -1;
5505 p2p->cfg->passphrase_len = len;
5506 return 0;
5507}
Dmitry Shmidt2e67f062014-07-16 09:55:28 -07005508
5509
5510void p2p_set_vendor_elems(struct p2p_data *p2p, struct wpabuf **vendor_elem)
5511{
5512 p2p->vendor_elem = vendor_elem;
5513}
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005514
5515
5516void p2p_go_neg_wait_timeout(void *eloop_ctx, void *timeout_ctx)
5517{
5518 struct p2p_data *p2p = eloop_ctx;
5519
5520 p2p_dbg(p2p,
5521 "Timeout on waiting peer to become ready for GO Negotiation");
5522 p2p_go_neg_failed(p2p, -1);
5523}
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005524
5525
5526void p2p_set_own_pref_freq_list(struct p2p_data *p2p,
5527 const unsigned int *pref_freq_list,
5528 unsigned int size)
5529{
5530 unsigned int i;
5531
5532 if (size > P2P_MAX_PREF_CHANNELS)
5533 size = P2P_MAX_PREF_CHANNELS;
5534 p2p->num_pref_freq = size;
5535 for (i = 0; i < size; i++) {
5536 p2p->pref_freq_list[i] = pref_freq_list[i];
5537 p2p_dbg(p2p, "Own preferred frequency list[%u]=%u MHz",
5538 i, p2p->pref_freq_list[i]);
5539 }
5540}
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07005541
5542
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08005543void p2p_set_override_pref_op_chan(struct p2p_data *p2p, u8 op_class,
5544 u8 chan)
5545{
5546 p2p->override_pref_op_class = op_class;
5547 p2p->override_pref_channel = chan;
5548}
5549
5550
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07005551struct wpabuf * p2p_build_probe_resp_template(struct p2p_data *p2p,
5552 unsigned int freq)
5553{
5554 struct wpabuf *ies, *buf;
5555 u8 addr[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
5556 int ret;
5557
5558 ies = p2p_build_probe_resp_ies(p2p, NULL, 0);
5559 if (!ies) {
5560 wpa_printf(MSG_ERROR,
5561 "CTRL: Failed to build Probe Response IEs");
5562 return NULL;
5563 }
5564
5565 buf = wpabuf_alloc(200 + wpabuf_len(ies));
5566 if (!buf) {
5567 wpabuf_free(ies);
5568 return NULL;
5569 }
5570
5571 ret = p2p_build_probe_resp_buf(p2p, buf, ies, addr, freq);
5572 wpabuf_free(ies);
5573 if (ret) {
5574 wpabuf_free(buf);
5575 return NULL;
5576 }
5577
5578 return buf;
5579}