blob: 360ca60bda7b90405a11edb2bc8c583e82e4c67c [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);
671 }
672}
673
674
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800675static int p2p_compare_wfd_info(struct p2p_device *dev,
676 const struct p2p_message *msg)
677{
678 if (dev->info.wfd_subelems && msg->wfd_subelems) {
679 if (dev->info.wfd_subelems->used != msg->wfd_subelems->used)
680 return 1;
681
682 return os_memcmp(dev->info.wfd_subelems->buf,
683 msg->wfd_subelems->buf,
684 dev->info.wfd_subelems->used);
685 }
686 if (dev->info.wfd_subelems || msg->wfd_subelems)
687 return 1;
688
689 return 0;
690}
691
692
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700693/**
Dmitry Shmidt04949592012-07-19 12:16:46 -0700694 * p2p_add_device - Add peer entries based on scan results or P2P frames
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700695 * @p2p: P2P module context from p2p_init()
696 * @addr: Source address of Beacon or Probe Response frame (may be either
697 * P2P Device Address or P2P Interface Address)
698 * @level: Signal level (signal strength of the received frame from the peer)
699 * @freq: Frequency on which the Beacon or Probe Response frame was received
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800700 * @rx_time: Time when the result was received
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700701 * @ies: IEs from the Beacon or Probe Response frame
702 * @ies_len: Length of ies buffer in octets
Dmitry Shmidt04949592012-07-19 12:16:46 -0700703 * @scan_res: Whether this was based on scan results
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700704 * Returns: 0 on success, -1 on failure
705 *
706 * If the scan result is for a GO, the clients in the group will also be added
707 * to the peer table. This function can also be used with some other frames
708 * like Provision Discovery Request that contains P2P Capability and P2P Device
709 * Info attributes.
710 */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800711int p2p_add_device(struct p2p_data *p2p, const u8 *addr, int freq,
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800712 struct os_reltime *rx_time, int level, const u8 *ies,
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800713 size_t ies_len, int scan_res)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700714{
715 struct p2p_device *dev;
716 struct p2p_message msg;
717 const u8 *p2p_dev_addr;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800718 int wfd_changed;
Dmitry Shmidt29333592017-01-09 12:27:11 -0800719 int dev_name_changed;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700720 int i;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800721 struct os_reltime time_now;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700722
723 os_memset(&msg, 0, sizeof(msg));
724 if (p2p_parse_ies(ies, ies_len, &msg)) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700725 p2p_dbg(p2p, "Failed to parse P2P IE for a device entry");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700726 p2p_parse_free(&msg);
727 return -1;
728 }
729
730 if (msg.p2p_device_addr)
731 p2p_dev_addr = msg.p2p_device_addr;
732 else if (msg.device_id)
733 p2p_dev_addr = msg.device_id;
734 else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700735 p2p_dbg(p2p, "Ignore scan data without P2P Device Info or P2P Device Id");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700736 p2p_parse_free(&msg);
737 return -1;
738 }
739
740 if (!is_zero_ether_addr(p2p->peer_filter) &&
741 os_memcmp(p2p_dev_addr, p2p->peer_filter, ETH_ALEN) != 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700742 p2p_dbg(p2p, "Do not add peer filter for " MACSTR
743 " due to peer filter", MAC2STR(p2p_dev_addr));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800744 p2p_parse_free(&msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700745 return 0;
746 }
747
748 dev = p2p_create_device(p2p, p2p_dev_addr);
749 if (dev == NULL) {
750 p2p_parse_free(&msg);
751 return -1;
752 }
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800753
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800754 if (rx_time == NULL) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800755 os_get_reltime(&time_now);
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800756 rx_time = &time_now;
757 }
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800758
759 /*
760 * Update the device entry only if the new peer
Dmitry Shmidta3dc3092015-06-23 11:21:28 -0700761 * entry is newer than the one previously stored, or if
762 * the device was previously seen as a P2P Client in a group
763 * and the new entry isn't older than a threshold.
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800764 */
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800765 if (dev->last_seen.sec > 0 &&
Dmitry Shmidta3dc3092015-06-23 11:21:28 -0700766 os_reltime_before(rx_time, &dev->last_seen) &&
767 (!(dev->flags & P2P_DEV_LAST_SEEN_AS_GROUP_CLIENT) ||
768 os_reltime_expired(&dev->last_seen, rx_time,
769 P2P_DEV_GROUP_CLIENT_RESP_THRESHOLD))) {
770 p2p_dbg(p2p,
771 "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 -0800772 (unsigned int) rx_time->sec,
773 (unsigned int) rx_time->usec,
774 (unsigned int) dev->last_seen.sec,
Dmitry Shmidta3dc3092015-06-23 11:21:28 -0700775 (unsigned int) dev->last_seen.usec,
776 dev->flags);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800777 p2p_parse_free(&msg);
778 return -1;
779 }
780
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800781 os_memcpy(&dev->last_seen, rx_time, sizeof(struct os_reltime));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800782
Dmitry Shmidta3dc3092015-06-23 11:21:28 -0700783 dev->flags &= ~(P2P_DEV_PROBE_REQ_ONLY | P2P_DEV_GROUP_CLIENT_ONLY |
784 P2P_DEV_LAST_SEEN_AS_GROUP_CLIENT);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700785
786 if (os_memcmp(addr, p2p_dev_addr, ETH_ALEN) != 0)
787 os_memcpy(dev->interface_addr, addr, ETH_ALEN);
788 if (msg.ssid &&
Jouni Malinenfdb708a2015-04-07 11:32:11 +0300789 msg.ssid[1] <= sizeof(dev->oper_ssid) &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700790 (msg.ssid[1] != P2P_WILDCARD_SSID_LEN ||
791 os_memcmp(msg.ssid + 2, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN)
792 != 0)) {
793 os_memcpy(dev->oper_ssid, msg.ssid + 2, msg.ssid[1]);
794 dev->oper_ssid_len = msg.ssid[1];
795 }
796
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -0700797 wpabuf_free(dev->info.p2ps_instance);
798 dev->info.p2ps_instance = NULL;
799 if (msg.adv_service_instance && msg.adv_service_instance_len)
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800800 dev->info.p2ps_instance = wpabuf_alloc_copy(
801 msg.adv_service_instance, msg.adv_service_instance_len);
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800802
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700803 if (freq >= 2412 && freq <= 2484 && msg.ds_params &&
804 *msg.ds_params >= 1 && *msg.ds_params <= 14) {
805 int ds_freq;
806 if (*msg.ds_params == 14)
807 ds_freq = 2484;
808 else
809 ds_freq = 2407 + *msg.ds_params * 5;
810 if (freq != ds_freq) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700811 p2p_dbg(p2p, "Update Listen frequency based on DS Parameter Set IE: %d -> %d MHz",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700812 freq, ds_freq);
813 freq = ds_freq;
814 }
815 }
816
Dmitry Shmidt04949592012-07-19 12:16:46 -0700817 if (dev->listen_freq && dev->listen_freq != freq && scan_res) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700818 p2p_dbg(p2p, "Update Listen frequency based on scan results ("
819 MACSTR " %d -> %d MHz (DS param %d)",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700820 MAC2STR(dev->info.p2p_device_addr), dev->listen_freq,
821 freq, msg.ds_params ? *msg.ds_params : -1);
822 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700823 if (scan_res) {
824 dev->listen_freq = freq;
825 if (msg.group_info)
826 dev->oper_freq = freq;
827 }
Jouni Malinen75ecf522011-06-27 15:19:46 -0700828 dev->info.level = level;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700829
Dmitry Shmidt29333592017-01-09 12:27:11 -0800830 dev_name_changed = os_strncmp(dev->info.device_name, msg.device_name,
831 WPS_DEV_NAME_MAX_LEN) != 0;
832
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700833 p2p_copy_wps_info(p2p, dev, 0, &msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700834
835 for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) {
836 wpabuf_free(dev->info.wps_vendor_ext[i]);
837 dev->info.wps_vendor_ext[i] = NULL;
838 }
839
840 for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) {
841 if (msg.wps_vendor_ext[i] == NULL)
842 break;
843 dev->info.wps_vendor_ext[i] = wpabuf_alloc_copy(
844 msg.wps_vendor_ext[i], msg.wps_vendor_ext_len[i]);
845 if (dev->info.wps_vendor_ext[i] == NULL)
846 break;
847 }
848
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800849 wfd_changed = p2p_compare_wfd_info(dev, &msg);
850
Dmitry Shmidt29333592017-01-09 12:27:11 -0800851 if (wfd_changed) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700852 wpabuf_free(dev->info.wfd_subelems);
Dmitry Shmidt29333592017-01-09 12:27:11 -0800853 if (msg.wfd_subelems)
854 dev->info.wfd_subelems = wpabuf_dup(msg.wfd_subelems);
855 else
856 dev->info.wfd_subelems = NULL;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700857 }
858
Dmitry Shmidt04949592012-07-19 12:16:46 -0700859 if (scan_res) {
860 p2p_add_group_clients(p2p, p2p_dev_addr, addr, freq,
Dmitry Shmidta3dc3092015-06-23 11:21:28 -0700861 msg.group_info, msg.group_info_len,
862 rx_time);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700863 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700864
865 p2p_parse_free(&msg);
866
Dmitry Shmidt2e67f062014-07-16 09:55:28 -0700867 p2p_update_peer_vendor_elems(dev, ies, ies_len);
868
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800869 if (dev->flags & P2P_DEV_REPORTED && !wfd_changed &&
Dmitry Shmidt29333592017-01-09 12:27:11 -0800870 !dev_name_changed &&
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800871 (!msg.adv_service_instance ||
872 (dev->flags & P2P_DEV_P2PS_REPORTED)))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700873 return 0;
874
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700875 p2p_dbg(p2p, "Peer found with Listen frequency %d MHz (rx_time=%u.%06u)",
876 freq, (unsigned int) rx_time->sec,
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800877 (unsigned int) rx_time->usec);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700878 if (dev->flags & P2P_DEV_USER_REJECTED) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700879 p2p_dbg(p2p, "Do not report rejected device");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700880 return 0;
881 }
882
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800883 if (dev->info.config_methods == 0 &&
884 (freq == 2412 || freq == 2437 || freq == 2462)) {
885 /*
886 * If we have only seen a Beacon frame from a GO, we do not yet
887 * know what WPS config methods it supports. Since some
888 * applications use config_methods value from P2P-DEVICE-FOUND
889 * events, postpone reporting this peer until we've fully
890 * discovered its capabilities.
891 *
892 * At least for now, do this only if the peer was detected on
893 * one of the social channels since that peer can be easily be
894 * found again and there are no limitations of having to use
895 * passive scan on this channels, so this can be done through
896 * Probe Response frame that includes the config_methods
897 * information.
898 */
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700899 p2p_dbg(p2p, "Do not report peer " MACSTR
900 " with unknown config methods", MAC2STR(addr));
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800901 return 0;
902 }
903
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700904 p2p->cfg->dev_found(p2p->cfg->cb_ctx, addr, &dev->info,
905 !(dev->flags & P2P_DEV_REPORTED_ONCE));
906 dev->flags |= P2P_DEV_REPORTED | P2P_DEV_REPORTED_ONCE;
907
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800908 if (msg.adv_service_instance)
909 dev->flags |= P2P_DEV_P2PS_REPORTED;
910
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700911 return 0;
912}
913
914
915static void p2p_device_free(struct p2p_data *p2p, struct p2p_device *dev)
916{
917 int i;
918
Dmitry Shmidt497c1d52011-07-21 15:19:46 -0700919 if (p2p->go_neg_peer == dev) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800920 /*
921 * If GO Negotiation is in progress, report that it has failed.
922 */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800923 p2p_go_neg_failed(p2p, -1);
Dmitry Shmidt497c1d52011-07-21 15:19:46 -0700924 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700925 if (p2p->invite_peer == dev)
926 p2p->invite_peer = NULL;
927 if (p2p->sd_peer == dev)
928 p2p->sd_peer = NULL;
929 if (p2p->pending_client_disc_go == dev)
930 p2p->pending_client_disc_go = NULL;
931
Dmitry Shmidtc55524a2011-07-07 11:18:38 -0700932 /* dev_lost() device, but only if it was previously dev_found() */
933 if (dev->flags & P2P_DEV_REPORTED_ONCE)
934 p2p->cfg->dev_lost(p2p->cfg->cb_ctx,
935 dev->info.p2p_device_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700936
937 for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) {
938 wpabuf_free(dev->info.wps_vendor_ext[i]);
939 dev->info.wps_vendor_ext[i] = NULL;
940 }
941
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700942 wpabuf_free(dev->info.wfd_subelems);
Dmitry Shmidt2e67f062014-07-16 09:55:28 -0700943 wpabuf_free(dev->info.vendor_elems);
Dmitry Shmidt413dde72014-04-11 10:23:22 -0700944 wpabuf_free(dev->go_neg_conf);
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800945 wpabuf_free(dev->info.p2ps_instance);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700946
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700947 os_free(dev);
948}
949
950
951static int p2p_get_next_prog_freq(struct p2p_data *p2p)
952{
953 struct p2p_channels *c;
954 struct p2p_reg_class *cla;
955 size_t cl, ch;
956 int found = 0;
957 u8 reg_class;
958 u8 channel;
959 int freq;
960
961 c = &p2p->cfg->channels;
962 for (cl = 0; cl < c->reg_classes; cl++) {
963 cla = &c->reg_class[cl];
964 if (cla->reg_class != p2p->last_prog_scan_class)
965 continue;
966 for (ch = 0; ch < cla->channels; ch++) {
967 if (cla->channel[ch] == p2p->last_prog_scan_chan) {
968 found = 1;
969 break;
970 }
971 }
972 if (found)
973 break;
974 }
975
976 if (!found) {
977 /* Start from beginning */
978 reg_class = c->reg_class[0].reg_class;
979 channel = c->reg_class[0].channel[0];
980 } else {
981 /* Pick the next channel */
982 ch++;
983 if (ch == cla->channels) {
984 cl++;
985 if (cl == c->reg_classes)
986 cl = 0;
987 ch = 0;
988 }
989 reg_class = c->reg_class[cl].reg_class;
990 channel = c->reg_class[cl].channel[ch];
991 }
992
Dmitry Shmidt4b060592013-04-29 16:42:49 -0700993 freq = p2p_channel_to_freq(reg_class, channel);
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700994 p2p_dbg(p2p, "Next progressive search channel: reg_class %u channel %u -> %d MHz",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700995 reg_class, channel, freq);
996 p2p->last_prog_scan_class = reg_class;
997 p2p->last_prog_scan_chan = channel;
998
999 if (freq == 2412 || freq == 2437 || freq == 2462)
1000 return 0; /* No need to add social channels */
1001 return freq;
1002}
1003
1004
1005static void p2p_search(struct p2p_data *p2p)
1006{
1007 int freq = 0;
1008 enum p2p_scan_type type;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001009 u16 pw_id = DEV_PW_DEFAULT;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001010 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001011
1012 if (p2p->drv_in_listen) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001013 p2p_dbg(p2p, "Driver is still in Listen state - wait for it to end before continuing");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001014 return;
1015 }
1016 p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
1017
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001018 if (p2p->find_pending_full &&
1019 (p2p->find_type == P2P_FIND_PROGRESSIVE ||
1020 p2p->find_type == P2P_FIND_START_WITH_FULL)) {
1021 type = P2P_SCAN_FULL;
1022 p2p_dbg(p2p, "Starting search (pending full scan)");
1023 p2p->find_pending_full = 0;
1024 } else if ((p2p->find_type == P2P_FIND_PROGRESSIVE &&
1025 (freq = p2p_get_next_prog_freq(p2p)) > 0) ||
1026 (p2p->find_type == P2P_FIND_START_WITH_FULL &&
1027 (freq = p2p->find_specified_freq) > 0)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001028 type = P2P_SCAN_SOCIAL_PLUS_ONE;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001029 p2p_dbg(p2p, "Starting search (+ freq %u)", freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001030 } else {
1031 type = P2P_SCAN_SOCIAL;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001032 p2p_dbg(p2p, "Starting search");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001033 }
1034
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001035 res = p2p->cfg->p2p_scan(p2p->cfg->cb_ctx, type, freq,
1036 p2p->num_req_dev_types, p2p->req_dev_types,
1037 p2p->find_dev_id, pw_id);
1038 if (res < 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001039 p2p_dbg(p2p, "Scan request schedule failed");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001040 p2p_continue_find(p2p);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001041 }
1042}
1043
1044
1045static void p2p_find_timeout(void *eloop_ctx, void *timeout_ctx)
1046{
1047 struct p2p_data *p2p = eloop_ctx;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001048 p2p_dbg(p2p, "Find timeout -> stop");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001049 p2p_stop_find(p2p);
1050}
1051
1052
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001053void p2p_notify_scan_trigger_status(struct p2p_data *p2p, int status)
1054{
1055 if (status != 0) {
1056 p2p_dbg(p2p, "Scan request failed");
1057 /* Do continue find even for the first p2p_find_scan */
1058 p2p_continue_find(p2p);
1059 } else {
1060 p2p_dbg(p2p, "Running p2p_scan");
1061 p2p->p2p_scan_running = 1;
1062 eloop_cancel_timeout(p2p_scan_timeout, p2p, NULL);
1063 eloop_register_timeout(P2P_SCAN_TIMEOUT, 0, p2p_scan_timeout,
1064 p2p, NULL);
1065 }
1066}
1067
1068
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001069static int p2p_run_after_scan(struct p2p_data *p2p)
1070{
1071 struct p2p_device *dev;
1072 enum p2p_after_scan op;
1073
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001074 op = p2p->start_after_scan;
1075 p2p->start_after_scan = P2P_AFTER_SCAN_NOTHING;
1076 switch (op) {
1077 case P2P_AFTER_SCAN_NOTHING:
1078 break;
1079 case P2P_AFTER_SCAN_LISTEN:
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001080 p2p_dbg(p2p, "Start previously requested Listen state");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001081 p2p_listen(p2p, p2p->pending_listen_sec * 1000 +
1082 p2p->pending_listen_usec / 1000);
1083 return 1;
1084 case P2P_AFTER_SCAN_CONNECT:
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001085 p2p_dbg(p2p, "Start previously requested connect with " MACSTR,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001086 MAC2STR(p2p->after_scan_peer));
1087 dev = p2p_get_device(p2p, p2p->after_scan_peer);
1088 if (dev == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001089 p2p_dbg(p2p, "Peer not known anymore");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001090 break;
1091 }
1092 p2p_connect_send(p2p, dev);
1093 return 1;
1094 }
1095
1096 return 0;
1097}
1098
1099
1100static void p2p_scan_timeout(void *eloop_ctx, void *timeout_ctx)
1101{
1102 struct p2p_data *p2p = eloop_ctx;
1103 int running;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001104 p2p_dbg(p2p, "p2p_scan timeout (running=%d)", p2p->p2p_scan_running);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001105 running = p2p->p2p_scan_running;
1106 /* Make sure we recover from missed scan results callback */
1107 p2p->p2p_scan_running = 0;
1108
1109 if (running)
1110 p2p_run_after_scan(p2p);
1111}
1112
1113
1114static void p2p_free_req_dev_types(struct p2p_data *p2p)
1115{
1116 p2p->num_req_dev_types = 0;
1117 os_free(p2p->req_dev_types);
1118 p2p->req_dev_types = NULL;
1119}
1120
1121
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001122static int p2ps_gen_hash(struct p2p_data *p2p, const char *str, u8 *hash)
1123{
1124 u8 buf[SHA256_MAC_LEN];
1125 char str_buf[256];
1126 const u8 *adv_array;
1127 size_t i, adv_len;
1128
1129 if (!str || !hash)
1130 return 0;
1131
1132 if (!str[0]) {
1133 os_memcpy(hash, p2p->wild_card_hash, P2PS_HASH_LEN);
1134 return 1;
1135 }
1136
1137 adv_array = (u8 *) str_buf;
1138 adv_len = os_strlen(str);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07001139 if (adv_len >= sizeof(str_buf))
1140 return 0;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001141
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001142 for (i = 0; i < adv_len; i++) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001143 if (str[i] >= 'A' && str[i] <= 'Z')
1144 str_buf[i] = str[i] - 'A' + 'a';
1145 else
1146 str_buf[i] = str[i];
1147 }
1148
1149 if (sha256_vector(1, &adv_array, &adv_len, buf))
1150 return 0;
1151
1152 os_memcpy(hash, buf, P2PS_HASH_LEN);
1153 return 1;
1154}
1155
1156
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001157int p2p_find(struct p2p_data *p2p, unsigned int timeout,
1158 enum p2p_discovery_type type,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001159 unsigned int num_req_dev_types, const u8 *req_dev_types,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001160 const u8 *dev_id, unsigned int search_delay,
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001161 u8 seek_count, const char **seek, int freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001162{
1163 int res;
Hai Shalom74f70d42019-02-11 14:42:39 -08001164 struct os_reltime start;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001165
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001166 p2p_dbg(p2p, "Starting find (type=%d)", type);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001167 if (p2p->p2p_scan_running) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001168 p2p_dbg(p2p, "p2p_scan is already running");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001169 }
1170
1171 p2p_free_req_dev_types(p2p);
1172 if (req_dev_types && num_req_dev_types) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001173 p2p->req_dev_types = os_memdup(req_dev_types,
1174 num_req_dev_types *
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001175 WPS_DEV_TYPE_LEN);
1176 if (p2p->req_dev_types == NULL)
1177 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001178 p2p->num_req_dev_types = num_req_dev_types;
1179 }
1180
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001181 if (dev_id) {
1182 os_memcpy(p2p->find_dev_id_buf, dev_id, ETH_ALEN);
1183 p2p->find_dev_id = p2p->find_dev_id_buf;
1184 } else
1185 p2p->find_dev_id = NULL;
1186
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001187 if (seek_count == 0 || !seek) {
1188 /* Not an ASP search */
1189 p2p->p2ps_seek = 0;
1190 } else if (seek_count == 1 && seek && (!seek[0] || !seek[0][0])) {
1191 /*
1192 * An empty seek string means no hash values, but still an ASP
1193 * search.
1194 */
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07001195 p2p_dbg(p2p, "ASP search");
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001196 p2p->p2ps_seek_count = 0;
1197 p2p->p2ps_seek = 1;
1198 } else if (seek && seek_count <= P2P_MAX_QUERY_HASH) {
1199 u8 buf[P2PS_HASH_LEN];
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07001200 int i, count = 0;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001201
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001202 for (i = 0; i < seek_count; i++) {
1203 if (!p2ps_gen_hash(p2p, seek[i], buf))
1204 continue;
1205
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07001206 p2p_dbg(p2p, "Seek service %s hash " MACSTR,
1207 seek[i], MAC2STR(buf));
1208 os_memcpy(&p2p->p2ps_seek_hash[count * P2PS_HASH_LEN],
1209 buf, P2PS_HASH_LEN);
1210 count++;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001211 }
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07001212
1213 p2p->p2ps_seek_count = count;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001214 p2p->p2ps_seek = 1;
1215 } else {
1216 p2p->p2ps_seek_count = 0;
1217 p2p->p2ps_seek = 1;
1218 }
1219
1220 /* Special case to perform wildcard search */
1221 if (p2p->p2ps_seek_count == 0 && p2p->p2ps_seek) {
1222 p2p->p2ps_seek_count = 1;
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07001223 os_memcpy(&p2p->p2ps_seek_hash, p2p->wild_card_hash,
1224 P2PS_HASH_LEN);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001225 }
1226
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001227 p2p->start_after_scan = P2P_AFTER_SCAN_NOTHING;
1228 p2p_clear_timeout(p2p);
Dmitry Shmidtde47be72016-01-07 12:52:55 -08001229 if (p2p->pending_listen_freq) {
1230 p2p_dbg(p2p, "Clear pending_listen_freq for p2p_find");
1231 p2p->pending_listen_freq = 0;
1232 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001233 p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001234 p2p->find_pending_full = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001235 p2p->find_type = type;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001236 if (freq != 2412 && freq != 2437 && freq != 2462 && freq != 60480)
1237 p2p->find_specified_freq = freq;
1238 else
1239 p2p->find_specified_freq = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001240 p2p_device_clear_reported(p2p);
Dmitry Shmidtde47be72016-01-07 12:52:55 -08001241 os_memset(p2p->sd_query_no_ack, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001242 p2p_set_state(p2p, P2P_SEARCH);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001243 p2p->search_delay = search_delay;
1244 p2p->in_search_delay = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001245 eloop_cancel_timeout(p2p_find_timeout, p2p, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001246 p2p->last_p2p_find_timeout = timeout;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001247 if (timeout)
1248 eloop_register_timeout(timeout, 0, p2p_find_timeout,
1249 p2p, NULL);
Hai Shalom74f70d42019-02-11 14:42:39 -08001250 os_get_reltime(&start);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001251 switch (type) {
1252 case P2P_FIND_START_WITH_FULL:
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001253 if (freq > 0) {
1254 /*
1255 * Start with the specified channel and then move to
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001256 * scans for social channels and this specific channel.
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001257 */
1258 res = p2p->cfg->p2p_scan(p2p->cfg->cb_ctx,
1259 P2P_SCAN_SPECIFIC, freq,
1260 p2p->num_req_dev_types,
1261 p2p->req_dev_types, dev_id,
1262 DEV_PW_DEFAULT);
1263 break;
1264 }
1265 /* fall through */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001266 case P2P_FIND_PROGRESSIVE:
1267 res = p2p->cfg->p2p_scan(p2p->cfg->cb_ctx, P2P_SCAN_FULL, 0,
1268 p2p->num_req_dev_types,
Dmitry Shmidt04949592012-07-19 12:16:46 -07001269 p2p->req_dev_types, dev_id,
1270 DEV_PW_DEFAULT);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001271 break;
1272 case P2P_FIND_ONLY_SOCIAL:
1273 res = p2p->cfg->p2p_scan(p2p->cfg->cb_ctx, P2P_SCAN_SOCIAL, 0,
1274 p2p->num_req_dev_types,
Dmitry Shmidt04949592012-07-19 12:16:46 -07001275 p2p->req_dev_types, dev_id,
1276 DEV_PW_DEFAULT);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001277 break;
1278 default:
1279 return -1;
1280 }
1281
Hai Shalom74f70d42019-02-11 14:42:39 -08001282 if (!res)
1283 p2p->find_start = start;
1284
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001285 if (res != 0 && p2p->p2p_scan_running) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001286 p2p_dbg(p2p, "Failed to start p2p_scan - another p2p_scan was already running");
1287 /* wait for the previous p2p_scan to complete */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001288 if (type == P2P_FIND_PROGRESSIVE ||
1289 (type == P2P_FIND_START_WITH_FULL && freq == 0))
1290 p2p->find_pending_full = 1;
Dmitry Shmidt96be6222014-02-13 10:16:51 -08001291 res = 0; /* do not report failure */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001292 } else if (res != 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001293 p2p_dbg(p2p, "Failed to start p2p_scan");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001294 p2p_set_state(p2p, P2P_IDLE);
1295 eloop_cancel_timeout(p2p_find_timeout, p2p, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001296 }
1297
1298 return res;
1299}
1300
1301
1302void p2p_stop_find_for_freq(struct p2p_data *p2p, int freq)
1303{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001304 p2p_dbg(p2p, "Stopping find");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001305 eloop_cancel_timeout(p2p_find_timeout, p2p, NULL);
1306 p2p_clear_timeout(p2p);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001307 if (p2p->state == P2P_SEARCH || p2p->state == P2P_SD_DURING_FIND)
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001308 p2p->cfg->find_stopped(p2p->cfg->cb_ctx);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001309
1310 p2p->p2ps_seek_count = 0;
1311
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001312 p2p_set_state(p2p, P2P_IDLE);
1313 p2p_free_req_dev_types(p2p);
1314 p2p->start_after_scan = P2P_AFTER_SCAN_NOTHING;
Dmitry Shmidt8c652892013-03-01 10:14:01 -08001315 if (p2p->go_neg_peer)
1316 p2p->go_neg_peer->flags &= ~P2P_DEV_PEER_WAITING_RESPONSE;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001317 p2p->go_neg_peer = NULL;
1318 p2p->sd_peer = NULL;
1319 p2p->invite_peer = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001320 p2p_stop_listen_for_freq(p2p, freq);
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001321 p2p->send_action_in_progress = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001322}
1323
1324
1325void p2p_stop_listen_for_freq(struct p2p_data *p2p, int freq)
1326{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001327 if (freq > 0 && p2p->drv_in_listen == freq && p2p->in_listen) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001328 p2p_dbg(p2p, "Skip stop_listen since we are on correct channel for response");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001329 return;
1330 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001331 if (p2p->in_listen) {
1332 p2p->in_listen = 0;
1333 p2p_clear_timeout(p2p);
1334 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001335 if (p2p->drv_in_listen) {
1336 /*
1337 * The driver may not deliver callback to p2p_listen_end()
1338 * when the operation gets canceled, so clear the internal
1339 * variable that is tracking driver state.
1340 */
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001341 p2p_dbg(p2p, "Clear drv_in_listen (%d)", p2p->drv_in_listen);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001342 p2p->drv_in_listen = 0;
1343 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001344 p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
1345}
1346
1347
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001348void p2p_stop_listen(struct p2p_data *p2p)
1349{
1350 if (p2p->state != P2P_LISTEN_ONLY) {
1351 p2p_dbg(p2p, "Skip stop_listen since not in listen_only state.");
1352 return;
1353 }
1354
1355 p2p_stop_listen_for_freq(p2p, 0);
1356 p2p_set_state(p2p, P2P_IDLE);
1357}
1358
1359
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001360void p2p_stop_find(struct p2p_data *p2p)
1361{
Dmitry Shmidt3c57b3f2014-05-22 15:13:07 -07001362 p2p->pending_listen_freq = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001363 p2p_stop_find_for_freq(p2p, 0);
1364}
1365
1366
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001367static int p2p_prepare_channel_pref(struct p2p_data *p2p,
1368 unsigned int force_freq,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001369 unsigned int pref_freq, int go)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001370{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001371 u8 op_class, op_channel;
1372 unsigned int freq = force_freq ? force_freq : pref_freq;
1373
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001374 p2p_dbg(p2p, "Prepare channel pref - force_freq=%u pref_freq=%u go=%d",
1375 force_freq, pref_freq, go);
Dmitry Shmidt4b060592013-04-29 16:42:49 -07001376 if (p2p_freq_to_channel(freq, &op_class, &op_channel) < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001377 p2p_dbg(p2p, "Unsupported frequency %u MHz", freq);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001378 return -1;
1379 }
1380
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001381 if (!p2p_channels_includes(&p2p->cfg->channels, op_class, op_channel) &&
1382 (go || !p2p_channels_includes(&p2p->cfg->cli_channels, op_class,
1383 op_channel))) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001384 p2p_dbg(p2p, "Frequency %u MHz (oper_class %u channel %u) not allowed for P2P",
1385 freq, op_class, op_channel);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001386 return -1;
1387 }
1388
1389 p2p->op_reg_class = op_class;
1390 p2p->op_channel = op_channel;
1391
1392 if (force_freq) {
1393 p2p->channels.reg_classes = 1;
1394 p2p->channels.reg_class[0].channels = 1;
1395 p2p->channels.reg_class[0].reg_class = p2p->op_reg_class;
1396 p2p->channels.reg_class[0].channel[0] = p2p->op_channel;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001397 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001398 os_memcpy(&p2p->channels, &p2p->cfg->channels,
1399 sizeof(struct p2p_channels));
1400 }
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001401
1402 return 0;
1403}
1404
1405
1406static void p2p_prepare_channel_best(struct p2p_data *p2p)
1407{
1408 u8 op_class, op_channel;
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07001409 const int op_classes_5ghz[] = { 124, 125, 115, 0 };
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08001410 const int op_classes_ht40[] = { 126, 127, 116, 117, 0 };
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001411 const int op_classes_vht[] = { 128, 0 };
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001412 const int op_classes_edmg[] = { 181, 182, 183, 0 };
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001413
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07001414 p2p_dbg(p2p, "Prepare channel best");
1415
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001416 if (!p2p->cfg->cfg_op_channel && p2p->best_freq_overall > 0 &&
1417 p2p_supported_freq(p2p, p2p->best_freq_overall) &&
Dmitry Shmidt4b060592013-04-29 16:42:49 -07001418 p2p_freq_to_channel(p2p->best_freq_overall, &op_class, &op_channel)
1419 == 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001420 p2p_dbg(p2p, "Select best overall channel as operating channel preference");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001421 p2p->op_reg_class = op_class;
1422 p2p->op_channel = op_channel;
1423 } else if (!p2p->cfg->cfg_op_channel && p2p->best_freq_5 > 0 &&
1424 p2p_supported_freq(p2p, p2p->best_freq_5) &&
Dmitry Shmidt4b060592013-04-29 16:42:49 -07001425 p2p_freq_to_channel(p2p->best_freq_5, &op_class, &op_channel)
1426 == 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001427 p2p_dbg(p2p, "Select best 5 GHz channel as operating channel preference");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001428 p2p->op_reg_class = op_class;
1429 p2p->op_channel = op_channel;
1430 } else if (!p2p->cfg->cfg_op_channel && p2p->best_freq_24 > 0 &&
1431 p2p_supported_freq(p2p, p2p->best_freq_24) &&
Dmitry Shmidt4b060592013-04-29 16:42:49 -07001432 p2p_freq_to_channel(p2p->best_freq_24, &op_class,
1433 &op_channel) == 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001434 p2p_dbg(p2p, "Select best 2.4 GHz channel as operating channel preference");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001435 p2p->op_reg_class = op_class;
1436 p2p->op_channel = op_channel;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07001437 } else if (p2p->cfg->num_pref_chan > 0 &&
1438 p2p_channels_includes(&p2p->cfg->channels,
1439 p2p->cfg->pref_chan[0].op_class,
1440 p2p->cfg->pref_chan[0].chan)) {
1441 p2p_dbg(p2p, "Select first pref_chan entry as operating channel preference");
1442 p2p->op_reg_class = p2p->cfg->pref_chan[0].op_class;
1443 p2p->op_channel = p2p->cfg->pref_chan[0].chan;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001444 } else if (p2p_channel_select(&p2p->cfg->channels, op_classes_edmg,
1445 &p2p->op_reg_class, &p2p->op_channel) ==
1446 0) {
1447 p2p_dbg(p2p, "Select possible EDMG channel (op_class %u channel %u) as operating channel preference",
1448 p2p->op_reg_class, p2p->op_channel);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001449 } else if (p2p_channel_select(&p2p->cfg->channels, op_classes_vht,
1450 &p2p->op_reg_class, &p2p->op_channel) ==
1451 0) {
1452 p2p_dbg(p2p, "Select possible VHT channel (op_class %u channel %u) as operating channel preference",
1453 p2p->op_reg_class, p2p->op_channel);
1454 } else if (p2p_channel_select(&p2p->cfg->channels, op_classes_ht40,
1455 &p2p->op_reg_class, &p2p->op_channel) ==
1456 0) {
1457 p2p_dbg(p2p, "Select possible HT40 channel (op_class %u channel %u) as operating channel preference",
1458 p2p->op_reg_class, p2p->op_channel);
1459 } else if (p2p_channel_select(&p2p->cfg->channels, op_classes_5ghz,
1460 &p2p->op_reg_class, &p2p->op_channel) ==
1461 0) {
1462 p2p_dbg(p2p, "Select possible 5 GHz channel (op_class %u channel %u) as operating channel preference",
1463 p2p->op_reg_class, p2p->op_channel);
Dmitry Shmidtd11f0192014-03-24 12:09:47 -07001464 } else if (p2p_channels_includes(&p2p->cfg->channels,
1465 p2p->cfg->op_reg_class,
1466 p2p->cfg->op_channel)) {
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07001467 p2p_dbg(p2p, "Select pre-configured channel as operating channel preference");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001468 p2p->op_reg_class = p2p->cfg->op_reg_class;
1469 p2p->op_channel = p2p->cfg->op_channel;
Dmitry Shmidtd11f0192014-03-24 12:09:47 -07001470 } else if (p2p_channel_random_social(&p2p->cfg->channels,
1471 &p2p->op_reg_class,
Hai Shalom74f70d42019-02-11 14:42:39 -08001472 &p2p->op_channel,
1473 NULL, NULL) == 0) {
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001474 p2p_dbg(p2p, "Select random available social channel (op_class %u channel %u) as operating channel preference",
1475 p2p->op_reg_class, p2p->op_channel);
Dmitry Shmidtd11f0192014-03-24 12:09:47 -07001476 } else {
1477 /* Select any random available channel from the first available
1478 * operating class */
1479 p2p_channel_select(&p2p->cfg->channels, NULL,
1480 &p2p->op_reg_class,
1481 &p2p->op_channel);
1482 p2p_dbg(p2p, "Select random available channel %d from operating class %d as operating channel preference",
1483 p2p->op_channel, p2p->op_reg_class);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001484 }
1485
1486 os_memcpy(&p2p->channels, &p2p->cfg->channels,
1487 sizeof(struct p2p_channels));
1488}
1489
1490
1491/**
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001492 * p2p_prepare_channel - Select operating channel for GO Negotiation or P2PS PD
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001493 * @p2p: P2P module context from p2p_init()
1494 * @dev: Selected peer device
1495 * @force_freq: Forced frequency in MHz or 0 if not forced
1496 * @pref_freq: Preferred frequency in MHz or 0 if no preference
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001497 * @go: Whether the local end will be forced to be GO
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001498 * Returns: 0 on success, -1 on failure (channel not supported for P2P)
1499 *
1500 * This function is used to do initial operating channel selection for GO
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001501 * Negotiation prior to having received peer information or for P2PS PD
1502 * signalling. The selected channel may be further optimized in
1503 * p2p_reselect_channel() once the peer information is available.
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001504 */
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08001505int p2p_prepare_channel(struct p2p_data *p2p, struct p2p_device *dev,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001506 unsigned int force_freq, unsigned int pref_freq, int go)
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001507{
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001508 p2p_dbg(p2p, "Prepare channel - force_freq=%u pref_freq=%u go=%d",
1509 force_freq, pref_freq, go);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001510 if (force_freq || pref_freq) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001511 if (p2p_prepare_channel_pref(p2p, force_freq, pref_freq, go) <
1512 0)
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001513 return -1;
1514 } else {
1515 p2p_prepare_channel_best(p2p);
1516 }
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001517 p2p_channels_dump(p2p, "prepared channels", &p2p->channels);
1518 if (go)
1519 p2p_channels_remove_freqs(&p2p->channels, &p2p->no_go_freq);
1520 else if (!force_freq)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001521 p2p_channels_union_inplace(&p2p->channels,
1522 &p2p->cfg->cli_channels);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001523 p2p_channels_dump(p2p, "after go/cli filter/add", &p2p->channels);
1524
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001525 p2p_dbg(p2p, "Own preference for operation channel: Operating Class %u Channel %u%s",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001526 p2p->op_reg_class, p2p->op_channel,
1527 force_freq ? " (forced)" : "");
1528
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001529 if (force_freq)
1530 dev->flags |= P2P_DEV_FORCE_FREQ;
1531 else
1532 dev->flags &= ~P2P_DEV_FORCE_FREQ;
1533
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001534 return 0;
1535}
1536
1537
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001538static void p2p_set_dev_persistent(struct p2p_device *dev,
1539 int persistent_group)
1540{
1541 switch (persistent_group) {
1542 case 0:
1543 dev->flags &= ~(P2P_DEV_PREFER_PERSISTENT_GROUP |
1544 P2P_DEV_PREFER_PERSISTENT_RECONN);
1545 break;
1546 case 1:
1547 dev->flags |= P2P_DEV_PREFER_PERSISTENT_GROUP;
1548 dev->flags &= ~P2P_DEV_PREFER_PERSISTENT_RECONN;
1549 break;
1550 case 2:
1551 dev->flags |= P2P_DEV_PREFER_PERSISTENT_GROUP |
1552 P2P_DEV_PREFER_PERSISTENT_RECONN;
1553 break;
1554 }
1555}
1556
1557
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001558int p2p_connect(struct p2p_data *p2p, const u8 *peer_addr,
1559 enum p2p_wps_method wps_method,
1560 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07001561 unsigned int force_freq, int persistent_group,
1562 const u8 *force_ssid, size_t force_ssid_len,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001563 int pd_before_go_neg, unsigned int pref_freq, u16 oob_pw_id)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001564{
1565 struct p2p_device *dev;
1566
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001567 p2p_dbg(p2p, "Request to start group negotiation - peer=" MACSTR
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001568 " GO Intent=%d Intended Interface Address=" MACSTR
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001569 " wps_method=%d persistent_group=%d pd_before_go_neg=%d "
1570 "oob_pw_id=%u",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001571 MAC2STR(peer_addr), go_intent, MAC2STR(own_interface_addr),
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001572 wps_method, persistent_group, pd_before_go_neg, oob_pw_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001573
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001574 dev = p2p_get_device(p2p, peer_addr);
1575 if (dev == NULL || (dev->flags & P2P_DEV_PROBE_REQ_ONLY)) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001576 p2p_dbg(p2p, "Cannot connect to unknown P2P Device " MACSTR,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001577 MAC2STR(peer_addr));
1578 return -1;
1579 }
1580
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001581 if (p2p_prepare_channel(p2p, dev, force_freq, pref_freq,
1582 go_intent == 15) < 0)
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001583 return -1;
1584
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001585 if (dev->flags & P2P_DEV_GROUP_CLIENT_ONLY) {
1586 if (!(dev->info.dev_capab &
1587 P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY)) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001588 p2p_dbg(p2p, "Cannot connect to P2P Device " MACSTR
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001589 " that is in a group and is not discoverable",
1590 MAC2STR(peer_addr));
1591 return -1;
1592 }
1593 if (dev->oper_freq <= 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001594 p2p_dbg(p2p, "Cannot connect to P2P Device " MACSTR
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001595 " with incomplete information",
1596 MAC2STR(peer_addr));
1597 return -1;
1598 }
1599
1600 /*
1601 * First, try to connect directly. If the peer does not
1602 * acknowledge frames, assume it is sleeping and use device
1603 * discoverability via the GO at that point.
1604 */
1605 }
1606
Dmitry Shmidt04949592012-07-19 12:16:46 -07001607 p2p->ssid_set = 0;
1608 if (force_ssid) {
1609 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Forced SSID",
1610 force_ssid, force_ssid_len);
1611 os_memcpy(p2p->ssid, force_ssid, force_ssid_len);
1612 p2p->ssid_len = force_ssid_len;
1613 p2p->ssid_set = 1;
1614 }
1615
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001616 dev->flags &= ~P2P_DEV_NOT_YET_READY;
1617 dev->flags &= ~P2P_DEV_USER_REJECTED;
1618 dev->flags &= ~P2P_DEV_WAIT_GO_NEG_RESPONSE;
1619 dev->flags &= ~P2P_DEV_WAIT_GO_NEG_CONFIRM;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001620 if (pd_before_go_neg)
1621 dev->flags |= P2P_DEV_PD_BEFORE_GO_NEG;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001622 else {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001623 dev->flags &= ~P2P_DEV_PD_BEFORE_GO_NEG;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001624 /*
Dmitry Shmidt9cdf1b92013-02-27 12:58:50 -08001625 * Assign dialog token and tie breaker here to use the same
1626 * values in each retry within the same GO Negotiation exchange.
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001627 */
1628 dev->dialog_token++;
1629 if (dev->dialog_token == 0)
1630 dev->dialog_token = 1;
Dmitry Shmidt9cdf1b92013-02-27 12:58:50 -08001631 dev->tie_breaker = p2p->next_tie_breaker;
1632 p2p->next_tie_breaker = !p2p->next_tie_breaker;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001633 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001634 dev->connect_reqs = 0;
1635 dev->go_neg_req_sent = 0;
1636 dev->go_state = UNKNOWN_GO;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001637 p2p_set_dev_persistent(dev, persistent_group);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001638 p2p->go_intent = go_intent;
1639 os_memcpy(p2p->intended_addr, own_interface_addr, ETH_ALEN);
1640
1641 if (p2p->state != P2P_IDLE)
1642 p2p_stop_find(p2p);
1643
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001644 dev->wps_method = wps_method;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001645 dev->oob_pw_id = oob_pw_id;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001646 dev->status = P2P_SC_SUCCESS;
1647
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001648 if (p2p->p2p_scan_running) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001649 p2p_dbg(p2p, "p2p_scan running - delay connect send");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001650 p2p->start_after_scan = P2P_AFTER_SCAN_CONNECT;
1651 os_memcpy(p2p->after_scan_peer, peer_addr, ETH_ALEN);
1652 return 0;
1653 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001654
1655 return p2p_connect_send(p2p, dev);
1656}
1657
1658
1659int p2p_authorize(struct p2p_data *p2p, const u8 *peer_addr,
1660 enum p2p_wps_method wps_method,
1661 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07001662 unsigned int force_freq, int persistent_group,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001663 const u8 *force_ssid, size_t force_ssid_len,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001664 unsigned int pref_freq, u16 oob_pw_id)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001665{
1666 struct p2p_device *dev;
1667
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001668 p2p_dbg(p2p, "Request to authorize group negotiation - peer=" MACSTR
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001669 " GO Intent=%d Intended Interface Address=" MACSTR
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001670 " wps_method=%d persistent_group=%d oob_pw_id=%u",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001671 MAC2STR(peer_addr), go_intent, MAC2STR(own_interface_addr),
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001672 wps_method, persistent_group, oob_pw_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001673
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001674 dev = p2p_get_device(p2p, peer_addr);
1675 if (dev == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001676 p2p_dbg(p2p, "Cannot authorize unknown P2P Device " MACSTR,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001677 MAC2STR(peer_addr));
1678 return -1;
1679 }
1680
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001681 if (p2p_prepare_channel(p2p, dev, force_freq, pref_freq, go_intent ==
1682 15) < 0)
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001683 return -1;
1684
Dmitry Shmidt04949592012-07-19 12:16:46 -07001685 p2p->ssid_set = 0;
1686 if (force_ssid) {
1687 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Forced SSID",
1688 force_ssid, force_ssid_len);
1689 os_memcpy(p2p->ssid, force_ssid, force_ssid_len);
1690 p2p->ssid_len = force_ssid_len;
1691 p2p->ssid_set = 1;
1692 }
1693
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001694 dev->flags &= ~P2P_DEV_NOT_YET_READY;
1695 dev->flags &= ~P2P_DEV_USER_REJECTED;
1696 dev->go_neg_req_sent = 0;
1697 dev->go_state = UNKNOWN_GO;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001698 p2p_set_dev_persistent(dev, persistent_group);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001699 p2p->go_intent = go_intent;
1700 os_memcpy(p2p->intended_addr, own_interface_addr, ETH_ALEN);
1701
1702 dev->wps_method = wps_method;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001703 dev->oob_pw_id = oob_pw_id;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001704 dev->status = P2P_SC_SUCCESS;
1705
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001706 return 0;
1707}
1708
1709
1710void p2p_add_dev_info(struct p2p_data *p2p, const u8 *addr,
1711 struct p2p_device *dev, struct p2p_message *msg)
1712{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001713 os_get_reltime(&dev->last_seen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001714
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001715 p2p_copy_wps_info(p2p, dev, 0, msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001716
1717 if (msg->listen_channel) {
1718 int freq;
Dmitry Shmidt4b060592013-04-29 16:42:49 -07001719 freq = p2p_channel_to_freq(msg->listen_channel[3],
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001720 msg->listen_channel[4]);
1721 if (freq < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001722 p2p_dbg(p2p, "Unknown peer Listen channel: "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001723 "country=%c%c(0x%02x) reg_class=%u channel=%u",
1724 msg->listen_channel[0],
1725 msg->listen_channel[1],
1726 msg->listen_channel[2],
1727 msg->listen_channel[3],
1728 msg->listen_channel[4]);
1729 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001730 p2p_dbg(p2p, "Update peer " MACSTR
1731 " Listen channel: %u -> %u MHz",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001732 MAC2STR(dev->info.p2p_device_addr),
1733 dev->listen_freq, freq);
1734 dev->listen_freq = freq;
1735 }
1736 }
1737
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001738 if (msg->wfd_subelems) {
1739 wpabuf_free(dev->info.wfd_subelems);
1740 dev->info.wfd_subelems = wpabuf_dup(msg->wfd_subelems);
1741 }
1742
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001743 if (dev->flags & P2P_DEV_PROBE_REQ_ONLY) {
1744 dev->flags &= ~P2P_DEV_PROBE_REQ_ONLY;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001745 p2p_dbg(p2p, "Completed device entry based on data from GO Negotiation Request");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001746 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001747 p2p_dbg(p2p, "Created device entry based on GO Neg Req: "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001748 MACSTR " dev_capab=0x%x group_capab=0x%x name='%s' "
1749 "listen_freq=%d",
1750 MAC2STR(dev->info.p2p_device_addr),
1751 dev->info.dev_capab, dev->info.group_capab,
1752 dev->info.device_name, dev->listen_freq);
1753 }
1754
1755 dev->flags &= ~P2P_DEV_GROUP_CLIENT_ONLY;
1756
1757 if (dev->flags & P2P_DEV_USER_REJECTED) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001758 p2p_dbg(p2p, "Do not report rejected device");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001759 return;
1760 }
1761
1762 p2p->cfg->dev_found(p2p->cfg->cb_ctx, addr, &dev->info,
1763 !(dev->flags & P2P_DEV_REPORTED_ONCE));
1764 dev->flags |= P2P_DEV_REPORTED | P2P_DEV_REPORTED_ONCE;
1765}
1766
1767
1768void p2p_build_ssid(struct p2p_data *p2p, u8 *ssid, size_t *ssid_len)
1769{
1770 os_memcpy(ssid, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN);
1771 p2p_random((char *) &ssid[P2P_WILDCARD_SSID_LEN], 2);
1772 os_memcpy(&ssid[P2P_WILDCARD_SSID_LEN + 2],
1773 p2p->cfg->ssid_postfix, p2p->cfg->ssid_postfix_len);
1774 *ssid_len = P2P_WILDCARD_SSID_LEN + 2 + p2p->cfg->ssid_postfix_len;
1775}
1776
1777
1778int p2p_go_params(struct p2p_data *p2p, struct p2p_go_neg_results *params)
1779{
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001780 if (p2p->ssid_set) {
1781 os_memcpy(params->ssid, p2p->ssid, p2p->ssid_len);
1782 params->ssid_len = p2p->ssid_len;
1783 } else {
1784 p2p_build_ssid(p2p, params->ssid, &params->ssid_len);
1785 }
1786 p2p->ssid_set = 0;
1787
Jimmy Chen6d7e3902018-11-20 10:15:16 +08001788 if (p2p->passphrase_set) {
1789 os_memcpy(params->passphrase, p2p->passphrase, os_strlen(p2p->passphrase));
1790 } else {
1791 p2p_random(params->passphrase, p2p->cfg->passphrase_len);
1792 }
1793 p2p->passphrase_set = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001794 return 0;
1795}
1796
1797
1798void p2p_go_complete(struct p2p_data *p2p, struct p2p_device *peer)
1799{
1800 struct p2p_go_neg_results res;
1801 int go = peer->go_state == LOCAL_GO;
1802 struct p2p_channels intersection;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001803
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001804 p2p_dbg(p2p, "GO Negotiation with " MACSTR " completed (%s will be GO)",
1805 MAC2STR(peer->info.p2p_device_addr), go ? "local end" : "peer");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001806
1807 os_memset(&res, 0, sizeof(res));
1808 res.role_go = go;
1809 os_memcpy(res.peer_device_addr, peer->info.p2p_device_addr, ETH_ALEN);
1810 os_memcpy(res.peer_interface_addr, peer->intended_addr, ETH_ALEN);
1811 res.wps_method = peer->wps_method;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001812 if (peer->flags & P2P_DEV_PREFER_PERSISTENT_GROUP) {
1813 if (peer->flags & P2P_DEV_PREFER_PERSISTENT_RECONN)
1814 res.persistent_group = 2;
1815 else
1816 res.persistent_group = 1;
1817 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001818
1819 if (go) {
1820 /* Setup AP mode for WPS provisioning */
Dmitry Shmidt4b060592013-04-29 16:42:49 -07001821 res.freq = p2p_channel_to_freq(p2p->op_reg_class,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001822 p2p->op_channel);
1823 os_memcpy(res.ssid, p2p->ssid, p2p->ssid_len);
1824 res.ssid_len = p2p->ssid_len;
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07001825 p2p_random(res.passphrase, p2p->cfg->passphrase_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001826 } else {
1827 res.freq = peer->oper_freq;
1828 if (p2p->ssid_len) {
1829 os_memcpy(res.ssid, p2p->ssid, p2p->ssid_len);
1830 res.ssid_len = p2p->ssid_len;
1831 }
1832 }
1833
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001834 p2p_channels_dump(p2p, "own channels", &p2p->channels);
1835 p2p_channels_dump(p2p, "peer channels", &peer->channels);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001836 p2p_channels_intersect(&p2p->channels, &peer->channels,
1837 &intersection);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001838 if (go) {
1839 p2p_channels_remove_freqs(&intersection, &p2p->no_go_freq);
1840 p2p_channels_dump(p2p, "intersection after no-GO removal",
1841 &intersection);
1842 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001843
1844 p2p_channels_to_freqs(&intersection, res.freq_list,
1845 P2P_MAX_CHANNELS);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001846
1847 res.peer_config_timeout = go ? peer->client_timeout : peer->go_timeout;
1848
1849 p2p_clear_timeout(p2p);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001850 p2p->ssid_set = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001851 peer->go_neg_req_sent = 0;
Dmitry Shmidt29333592017-01-09 12:27:11 -08001852 peer->flags &= ~P2P_DEV_PEER_WAITING_RESPONSE;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001853 peer->wps_method = WPS_NOT_READY;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001854 peer->oob_pw_id = 0;
Dmitry Shmidt413dde72014-04-11 10:23:22 -07001855 wpabuf_free(peer->go_neg_conf);
1856 peer->go_neg_conf = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001857
1858 p2p_set_state(p2p, P2P_PROVISIONING);
1859 p2p->cfg->go_neg_completed(p2p->cfg->cb_ctx, &res);
1860}
1861
1862
1863static void p2p_rx_p2p_action(struct p2p_data *p2p, const u8 *sa,
1864 const u8 *data, size_t len, int rx_freq)
1865{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001866 p2p_dbg(p2p, "RX P2P Public Action from " MACSTR, MAC2STR(sa));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001867 wpa_hexdump(MSG_MSGDUMP, "P2P: P2P Public Action contents", data, len);
1868
1869 if (len < 1)
1870 return;
1871
1872 switch (data[0]) {
1873 case P2P_GO_NEG_REQ:
1874 p2p_process_go_neg_req(p2p, sa, data + 1, len - 1, rx_freq);
1875 break;
1876 case P2P_GO_NEG_RESP:
1877 p2p_process_go_neg_resp(p2p, sa, data + 1, len - 1, rx_freq);
1878 break;
1879 case P2P_GO_NEG_CONF:
1880 p2p_process_go_neg_conf(p2p, sa, data + 1, len - 1);
1881 break;
1882 case P2P_INVITATION_REQ:
1883 p2p_process_invitation_req(p2p, sa, data + 1, len - 1,
1884 rx_freq);
1885 break;
1886 case P2P_INVITATION_RESP:
1887 p2p_process_invitation_resp(p2p, sa, data + 1, len - 1);
1888 break;
1889 case P2P_PROV_DISC_REQ:
1890 p2p_process_prov_disc_req(p2p, sa, data + 1, len - 1, rx_freq);
1891 break;
1892 case P2P_PROV_DISC_RESP:
1893 p2p_process_prov_disc_resp(p2p, sa, data + 1, len - 1);
1894 break;
1895 case P2P_DEV_DISC_REQ:
1896 p2p_process_dev_disc_req(p2p, sa, data + 1, len - 1, rx_freq);
1897 break;
1898 case P2P_DEV_DISC_RESP:
1899 p2p_process_dev_disc_resp(p2p, sa, data + 1, len - 1);
1900 break;
1901 default:
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001902 p2p_dbg(p2p, "Unsupported P2P Public Action frame type %d",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001903 data[0]);
1904 break;
1905 }
1906}
1907
1908
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001909static void p2p_rx_action_public(struct p2p_data *p2p, const u8 *da,
1910 const u8 *sa, const u8 *bssid, const u8 *data,
1911 size_t len, int freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001912{
1913 if (len < 1)
1914 return;
1915
1916 switch (data[0]) {
1917 case WLAN_PA_VENDOR_SPECIFIC:
1918 data++;
1919 len--;
Dmitry Shmidta38abf92014-03-06 13:38:44 -08001920 if (len < 4)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001921 return;
Dmitry Shmidta38abf92014-03-06 13:38:44 -08001922 if (WPA_GET_BE32(data) != P2P_IE_VENDOR_TYPE)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001923 return;
1924
Dmitry Shmidta38abf92014-03-06 13:38:44 -08001925 data += 4;
1926 len -= 4;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001927
Dmitry Shmidta38abf92014-03-06 13:38:44 -08001928 p2p_rx_p2p_action(p2p, sa, data, len, freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001929 break;
1930 case WLAN_PA_GAS_INITIAL_REQ:
1931 p2p_rx_gas_initial_req(p2p, sa, data + 1, len - 1, freq);
1932 break;
1933 case WLAN_PA_GAS_INITIAL_RESP:
1934 p2p_rx_gas_initial_resp(p2p, sa, data + 1, len - 1, freq);
1935 break;
1936 case WLAN_PA_GAS_COMEBACK_REQ:
1937 p2p_rx_gas_comeback_req(p2p, sa, data + 1, len - 1, freq);
1938 break;
1939 case WLAN_PA_GAS_COMEBACK_RESP:
1940 p2p_rx_gas_comeback_resp(p2p, sa, data + 1, len - 1, freq);
1941 break;
1942 }
1943}
1944
1945
1946void p2p_rx_action(struct p2p_data *p2p, const u8 *da, const u8 *sa,
1947 const u8 *bssid, u8 category,
1948 const u8 *data, size_t len, int freq)
1949{
1950 if (category == WLAN_ACTION_PUBLIC) {
1951 p2p_rx_action_public(p2p, da, sa, bssid, data, len, freq);
1952 return;
1953 }
1954
1955 if (category != WLAN_ACTION_VENDOR_SPECIFIC)
1956 return;
1957
1958 if (len < 4)
1959 return;
1960
Dmitry Shmidta38abf92014-03-06 13:38:44 -08001961 if (WPA_GET_BE32(data) != P2P_IE_VENDOR_TYPE)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001962 return;
Dmitry Shmidta38abf92014-03-06 13:38:44 -08001963 data += 4;
1964 len -= 4;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001965
1966 /* P2P action frame */
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001967 p2p_dbg(p2p, "RX P2P Action from " MACSTR, MAC2STR(sa));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001968 wpa_hexdump(MSG_MSGDUMP, "P2P: P2P Action contents", data, len);
1969
1970 if (len < 1)
1971 return;
1972 switch (data[0]) {
1973 case P2P_NOA:
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001974 p2p_dbg(p2p, "Received P2P Action - Notice of Absence");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001975 /* TODO */
1976 break;
1977 case P2P_PRESENCE_REQ:
1978 p2p_process_presence_req(p2p, da, sa, data + 1, len - 1, freq);
1979 break;
1980 case P2P_PRESENCE_RESP:
1981 p2p_process_presence_resp(p2p, da, sa, data + 1, len - 1);
1982 break;
1983 case P2P_GO_DISC_REQ:
1984 p2p_process_go_disc_req(p2p, da, sa, data + 1, len - 1, freq);
1985 break;
1986 default:
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001987 p2p_dbg(p2p, "Received P2P Action - unknown type %u", data[0]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001988 break;
1989 }
1990}
1991
1992
1993static void p2p_go_neg_start(void *eloop_ctx, void *timeout_ctx)
1994{
1995 struct p2p_data *p2p = eloop_ctx;
1996 if (p2p->go_neg_peer == NULL)
1997 return;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001998 if (p2p->pending_listen_freq) {
1999 p2p_dbg(p2p, "Clear pending_listen_freq for p2p_go_neg_start");
2000 p2p->pending_listen_freq = 0;
2001 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002002 p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
2003 p2p->go_neg_peer->status = P2P_SC_SUCCESS;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07002004 /*
2005 * Set new timeout to make sure a previously set one does not expire
2006 * too quickly while waiting for the GO Negotiation to complete.
2007 */
2008 p2p_set_timeout(p2p, 0, 500000);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002009 p2p_connect_send(p2p, p2p->go_neg_peer);
2010}
2011
2012
2013static void p2p_invite_start(void *eloop_ctx, void *timeout_ctx)
2014{
2015 struct p2p_data *p2p = eloop_ctx;
2016 if (p2p->invite_peer == NULL)
2017 return;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07002018 if (p2p->pending_listen_freq) {
2019 p2p_dbg(p2p, "Clear pending_listen_freq for p2p_invite_start");
2020 p2p->pending_listen_freq = 0;
2021 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002022 p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002023 p2p_invite_send(p2p, p2p->invite_peer, p2p->invite_go_dev_addr,
2024 p2p->invite_dev_pw_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002025}
2026
2027
2028static void p2p_add_dev_from_probe_req(struct p2p_data *p2p, const u8 *addr,
2029 const u8 *ie, size_t ie_len)
2030{
2031 struct p2p_message msg;
2032 struct p2p_device *dev;
2033
2034 os_memset(&msg, 0, sizeof(msg));
2035 if (p2p_parse_ies(ie, ie_len, &msg) < 0 || msg.p2p_attributes == NULL)
2036 {
2037 p2p_parse_free(&msg);
2038 return; /* not a P2P probe */
2039 }
2040
2041 if (msg.ssid == NULL || msg.ssid[1] != P2P_WILDCARD_SSID_LEN ||
2042 os_memcmp(msg.ssid + 2, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN)
2043 != 0) {
2044 /* The Probe Request is not part of P2P Device Discovery. It is
2045 * not known whether the source address of the frame is the P2P
2046 * Device Address or P2P Interface Address. Do not add a new
2047 * peer entry based on this frames.
2048 */
2049 p2p_parse_free(&msg);
2050 return;
2051 }
2052
2053 dev = p2p_get_device(p2p, addr);
2054 if (dev) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002055 if (msg.listen_channel) {
2056 int freq;
2057
2058 if (dev->country[0] == 0)
2059 os_memcpy(dev->country, msg.listen_channel, 3);
2060
2061 freq = p2p_channel_to_freq(msg.listen_channel[3],
2062 msg.listen_channel[4]);
2063
2064 if (freq > 0 && dev->listen_freq != freq) {
2065 p2p_dbg(p2p,
2066 "Updated peer " MACSTR " Listen channel (Probe Request): %d -> %d MHz",
2067 MAC2STR(addr), dev->listen_freq, freq);
2068 dev->listen_freq = freq;
2069 }
2070 }
2071
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002072 os_get_reltime(&dev->last_seen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002073 p2p_parse_free(&msg);
2074 return; /* already known */
2075 }
2076
2077 dev = p2p_create_device(p2p, addr);
2078 if (dev == NULL) {
2079 p2p_parse_free(&msg);
2080 return;
2081 }
2082
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002083 os_get_reltime(&dev->last_seen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002084 dev->flags |= P2P_DEV_PROBE_REQ_ONLY;
2085
2086 if (msg.listen_channel) {
2087 os_memcpy(dev->country, msg.listen_channel, 3);
Dmitry Shmidt4b060592013-04-29 16:42:49 -07002088 dev->listen_freq = p2p_channel_to_freq(msg.listen_channel[3],
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002089 msg.listen_channel[4]);
2090 }
2091
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002092 p2p_copy_wps_info(p2p, dev, 1, &msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002093
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002094 if (msg.wfd_subelems) {
2095 wpabuf_free(dev->info.wfd_subelems);
2096 dev->info.wfd_subelems = wpabuf_dup(msg.wfd_subelems);
2097 }
2098
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002099 p2p_parse_free(&msg);
2100
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002101 p2p_dbg(p2p, "Created device entry based on Probe Req: " MACSTR
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002102 " dev_capab=0x%x group_capab=0x%x name='%s' listen_freq=%d",
2103 MAC2STR(dev->info.p2p_device_addr), dev->info.dev_capab,
2104 dev->info.group_capab, dev->info.device_name,
2105 dev->listen_freq);
2106}
2107
2108
2109struct p2p_device * p2p_add_dev_from_go_neg_req(struct p2p_data *p2p,
2110 const u8 *addr,
2111 struct p2p_message *msg)
2112{
2113 struct p2p_device *dev;
2114
2115 dev = p2p_get_device(p2p, addr);
2116 if (dev) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002117 os_get_reltime(&dev->last_seen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002118 return dev; /* already known */
2119 }
2120
2121 dev = p2p_create_device(p2p, addr);
2122 if (dev == NULL)
2123 return NULL;
2124
2125 p2p_add_dev_info(p2p, addr, dev, msg);
2126
2127 return dev;
2128}
2129
2130
2131static int dev_type_match(const u8 *dev_type, const u8 *req_dev_type)
2132{
2133 if (os_memcmp(dev_type, req_dev_type, WPS_DEV_TYPE_LEN) == 0)
2134 return 1;
2135 if (os_memcmp(dev_type, req_dev_type, 2) == 0 &&
2136 WPA_GET_BE32(&req_dev_type[2]) == 0 &&
2137 WPA_GET_BE16(&req_dev_type[6]) == 0)
2138 return 1; /* Category match with wildcard OUI/sub-category */
2139 return 0;
2140}
2141
2142
2143int dev_type_list_match(const u8 *dev_type, const u8 *req_dev_type[],
2144 size_t num_req_dev_type)
2145{
2146 size_t i;
2147 for (i = 0; i < num_req_dev_type; i++) {
2148 if (dev_type_match(dev_type, req_dev_type[i]))
2149 return 1;
2150 }
2151 return 0;
2152}
2153
2154
2155/**
2156 * p2p_match_dev_type - Match local device type with requested type
2157 * @p2p: P2P module context from p2p_init()
2158 * @wps: WPS TLVs from Probe Request frame (concatenated WPS IEs)
2159 * Returns: 1 on match, 0 on mismatch
2160 *
2161 * This function can be used to match the Requested Device Type attribute in
2162 * WPS IE with the local device types for deciding whether to reply to a Probe
2163 * Request frame.
2164 */
2165int p2p_match_dev_type(struct p2p_data *p2p, struct wpabuf *wps)
2166{
2167 struct wps_parse_attr attr;
2168 size_t i;
2169
2170 if (wps_parse_msg(wps, &attr))
2171 return 1; /* assume no Requested Device Type attributes */
2172
2173 if (attr.num_req_dev_type == 0)
2174 return 1; /* no Requested Device Type attributes -> match */
2175
2176 if (dev_type_list_match(p2p->cfg->pri_dev_type, attr.req_dev_type,
2177 attr.num_req_dev_type))
2178 return 1; /* Own Primary Device Type matches */
2179
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002180 for (i = 0; i < p2p->cfg->num_sec_dev_types; i++) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002181 if (dev_type_list_match(p2p->cfg->sec_dev_type[i],
2182 attr.req_dev_type,
2183 attr.num_req_dev_type))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002184 return 1; /* Own Secondary Device Type matches */
2185 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002186
2187 /* No matching device type found */
2188 return 0;
2189}
2190
2191
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002192struct wpabuf * p2p_build_probe_resp_ies(struct p2p_data *p2p,
2193 const u8 *query_hash,
2194 u8 query_count)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002195{
2196 struct wpabuf *buf;
2197 u8 *len;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002198 int pw_id = -1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002199 size_t extra = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002200
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002201#ifdef CONFIG_WIFI_DISPLAY
2202 if (p2p->wfd_ie_probe_resp)
2203 extra = wpabuf_len(p2p->wfd_ie_probe_resp);
2204#endif /* CONFIG_WIFI_DISPLAY */
2205
Dmitry Shmidt2e67f062014-07-16 09:55:28 -07002206 if (p2p->vendor_elem && p2p->vendor_elem[VENDOR_ELEM_PROBE_RESP_P2P])
2207 extra += wpabuf_len(p2p->vendor_elem[VENDOR_ELEM_PROBE_RESP_P2P]);
2208
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002209 if (query_count)
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002210 extra += MAX_SVC_ADV_IE_LEN;
2211
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002212 buf = wpabuf_alloc(1000 + extra);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002213 if (buf == NULL)
2214 return NULL;
2215
Dmitry Shmidt04949592012-07-19 12:16:46 -07002216 if (p2p->go_neg_peer) {
2217 /* Advertise immediate availability of WPS credential */
2218 pw_id = p2p_wps_method_pw_id(p2p->go_neg_peer->wps_method);
2219 }
2220
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002221 if (p2p_build_wps_ie(p2p, buf, pw_id, 1) < 0) {
2222 p2p_dbg(p2p, "Failed to build WPS IE for Probe Response");
2223 wpabuf_free(buf);
2224 return NULL;
2225 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002226
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002227#ifdef CONFIG_WIFI_DISPLAY
2228 if (p2p->wfd_ie_probe_resp)
2229 wpabuf_put_buf(buf, p2p->wfd_ie_probe_resp);
2230#endif /* CONFIG_WIFI_DISPLAY */
2231
Dmitry Shmidt2e67f062014-07-16 09:55:28 -07002232 if (p2p->vendor_elem && p2p->vendor_elem[VENDOR_ELEM_PROBE_RESP_P2P])
2233 wpabuf_put_buf(buf,
2234 p2p->vendor_elem[VENDOR_ELEM_PROBE_RESP_P2P]);
2235
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002236 /* P2P IE */
2237 len = p2p_buf_add_ie_hdr(buf);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002238 p2p_buf_add_capability(buf, p2p->dev_capab &
2239 ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002240 if (p2p->ext_listen_interval)
2241 p2p_buf_add_ext_listen_timing(buf, p2p->ext_listen_period,
2242 p2p->ext_listen_interval);
2243 p2p_buf_add_device_info(buf, p2p, NULL);
2244 p2p_buf_update_ie_hdr(buf, len);
2245
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002246 if (query_count) {
2247 p2p_buf_add_service_instance(buf, p2p, query_count, query_hash,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002248 p2p->p2ps_adv_list);
2249 }
2250
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002251 return buf;
2252}
2253
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07002254static int p2p_build_probe_resp_buf(struct p2p_data *p2p, struct wpabuf *buf,
2255 struct wpabuf *ies,
2256 const u8 *addr, int rx_freq)
2257{
2258 struct ieee80211_mgmt *resp;
2259 u8 channel, op_class;
2260
2261 resp = wpabuf_put(buf, offsetof(struct ieee80211_mgmt,
2262 u.probe_resp.variable));
2263
2264 resp->frame_control = host_to_le16((WLAN_FC_TYPE_MGMT << 2) |
2265 (WLAN_FC_STYPE_PROBE_RESP << 4));
2266 os_memcpy(resp->da, addr, ETH_ALEN);
2267 os_memcpy(resp->sa, p2p->cfg->dev_addr, ETH_ALEN);
2268 os_memcpy(resp->bssid, p2p->cfg->dev_addr, ETH_ALEN);
2269 resp->u.probe_resp.beacon_int = host_to_le16(100);
2270 /* hardware or low-level driver will setup seq_ctrl and timestamp */
2271 resp->u.probe_resp.capab_info =
2272 host_to_le16(WLAN_CAPABILITY_SHORT_PREAMBLE |
2273 WLAN_CAPABILITY_PRIVACY |
2274 WLAN_CAPABILITY_SHORT_SLOT_TIME);
2275
2276 wpabuf_put_u8(buf, WLAN_EID_SSID);
2277 wpabuf_put_u8(buf, P2P_WILDCARD_SSID_LEN);
2278 wpabuf_put_data(buf, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN);
2279
2280 wpabuf_put_u8(buf, WLAN_EID_SUPP_RATES);
2281 wpabuf_put_u8(buf, 8);
2282 wpabuf_put_u8(buf, (60 / 5) | 0x80);
2283 wpabuf_put_u8(buf, 90 / 5);
2284 wpabuf_put_u8(buf, (120 / 5) | 0x80);
2285 wpabuf_put_u8(buf, 180 / 5);
2286 wpabuf_put_u8(buf, (240 / 5) | 0x80);
2287 wpabuf_put_u8(buf, 360 / 5);
2288 wpabuf_put_u8(buf, 480 / 5);
2289 wpabuf_put_u8(buf, 540 / 5);
2290
2291 if (!rx_freq) {
2292 channel = p2p->cfg->channel;
2293 } else if (p2p_freq_to_channel(rx_freq, &op_class, &channel)) {
2294 p2p_err(p2p, "Failed to convert freq to channel");
2295 return -1;
2296 }
2297
2298 wpabuf_put_u8(buf, WLAN_EID_DS_PARAMS);
2299 wpabuf_put_u8(buf, 1);
2300 wpabuf_put_u8(buf, channel);
2301
2302 wpabuf_put_buf(buf, ies);
2303
2304 return 0;
2305}
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002306
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002307static int p2p_service_find_asp(struct p2p_data *p2p, const u8 *hash)
2308{
2309 struct p2ps_advertisement *adv_data;
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002310 int any_wfa;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002311
2312 p2p_dbg(p2p, "ASP find - ASP list: %p", p2p->p2ps_adv_list);
2313
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002314 /* Wildcard org.wi-fi.wfds matches any WFA spec defined service */
2315 any_wfa = os_memcmp(hash, p2p->wild_card_hash, P2PS_HASH_LEN) == 0;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002316
2317 adv_data = p2p->p2ps_adv_list;
2318 while (adv_data) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002319 if (os_memcmp(hash, adv_data->hash, P2PS_HASH_LEN) == 0)
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002320 return 1; /* exact hash match */
2321 if (any_wfa &&
2322 os_strncmp(adv_data->svc_name, P2PS_WILD_HASH_STR,
2323 os_strlen(P2PS_WILD_HASH_STR)) == 0)
2324 return 1; /* WFA service match */
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002325 adv_data = adv_data->next;
2326 }
2327
2328 return 0;
2329}
2330
2331
Dmitry Shmidt04949592012-07-19 12:16:46 -07002332static enum p2p_probe_req_status
2333p2p_reply_probe(struct p2p_data *p2p, const u8 *addr, const u8 *dst,
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002334 const u8 *bssid, const u8 *ie, size_t ie_len,
2335 unsigned int rx_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002336{
2337 struct ieee802_11_elems elems;
2338 struct wpabuf *buf;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002339 struct p2p_message msg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002340 struct wpabuf *ies;
2341
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002342 if (ieee802_11_parse_elems((u8 *) ie, ie_len, &elems, 0) ==
2343 ParseFailed) {
2344 /* Ignore invalid Probe Request frames */
Dmitry Shmidtec58b162014-02-19 12:44:18 -08002345 p2p_dbg(p2p, "Could not parse Probe Request frame - ignore it");
Dmitry Shmidt04949592012-07-19 12:16:46 -07002346 return P2P_PREQ_MALFORMED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002347 }
2348
2349 if (elems.p2p == NULL) {
2350 /* not a P2P probe - ignore it */
Dmitry Shmidtec58b162014-02-19 12:44:18 -08002351 p2p_dbg(p2p, "Not a P2P probe - ignore it");
Dmitry Shmidt04949592012-07-19 12:16:46 -07002352 return P2P_PREQ_NOT_P2P;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002353 }
2354
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002355 if (dst && !is_broadcast_ether_addr(dst) &&
2356 os_memcmp(dst, p2p->cfg->dev_addr, ETH_ALEN) != 0) {
2357 /* Not sent to the broadcast address or our P2P Device Address
2358 */
Dmitry Shmidtec58b162014-02-19 12:44:18 -08002359 p2p_dbg(p2p, "Probe Req DA " MACSTR " not ours - ignore it",
2360 MAC2STR(dst));
Dmitry Shmidt04949592012-07-19 12:16:46 -07002361 return P2P_PREQ_NOT_PROCESSED;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002362 }
2363
2364 if (bssid && !is_broadcast_ether_addr(bssid)) {
2365 /* Not sent to the Wildcard BSSID */
Dmitry Shmidtec58b162014-02-19 12:44:18 -08002366 p2p_dbg(p2p, "Probe Req BSSID " MACSTR " not wildcard - ignore it",
2367 MAC2STR(bssid));
Dmitry Shmidt04949592012-07-19 12:16:46 -07002368 return P2P_PREQ_NOT_PROCESSED;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002369 }
2370
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002371 if (elems.ssid == NULL || elems.ssid_len != P2P_WILDCARD_SSID_LEN ||
2372 os_memcmp(elems.ssid, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN) !=
2373 0) {
2374 /* not using P2P Wildcard SSID - ignore */
Dmitry Shmidtec58b162014-02-19 12:44:18 -08002375 p2p_dbg(p2p, "Probe Req not using P2P Wildcard SSID - ignore it");
Dmitry Shmidt04949592012-07-19 12:16:46 -07002376 return P2P_PREQ_NOT_PROCESSED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002377 }
2378
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002379 if (supp_rates_11b_only(&elems)) {
2380 /* Indicates support for 11b rates only */
Dmitry Shmidtec58b162014-02-19 12:44:18 -08002381 p2p_dbg(p2p, "Probe Req with 11b rates only supported - ignore it");
Dmitry Shmidt04949592012-07-19 12:16:46 -07002382 return P2P_PREQ_NOT_P2P;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002383 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002384
2385 os_memset(&msg, 0, sizeof(msg));
2386 if (p2p_parse_ies(ie, ie_len, &msg) < 0) {
2387 /* Could not parse P2P attributes */
Dmitry Shmidtec58b162014-02-19 12:44:18 -08002388 p2p_dbg(p2p, "Could not parse P2P attributes in Probe Req - ignore it");
Dmitry Shmidt04949592012-07-19 12:16:46 -07002389 return P2P_PREQ_NOT_P2P;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002390 }
2391
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002392 if (msg.service_hash && msg.service_hash_count) {
2393 const u8 *hash = msg.service_hash;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002394 u8 i;
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002395 int p2ps_svc_found = 0;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002396
Dmitry Shmidt41712582015-06-29 11:02:15 -07002397 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",
2398 p2p->in_listen, p2p->drv_in_listen, rx_freq,
2399 p2p->cfg->channel, p2p->pending_listen_freq);
2400
2401 if (!p2p->in_listen && !p2p->drv_in_listen &&
2402 p2p->pending_listen_freq && rx_freq &&
2403 rx_freq != p2p->pending_listen_freq) {
2404 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",
2405 rx_freq, p2p->pending_listen_freq);
2406 p2p_parse_free(&msg);
2407 return P2P_PREQ_NOT_LISTEN;
2408 }
2409
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002410 for (i = 0; i < msg.service_hash_count; i++) {
2411 if (p2p_service_find_asp(p2p, hash)) {
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002412 p2p_dbg(p2p, "Service Hash match found: "
2413 MACSTR, MAC2STR(hash));
2414 p2ps_svc_found = 1;
2415 break;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002416 }
2417 hash += P2PS_HASH_LEN;
2418 }
2419
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002420 /* Probed hash unknown */
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002421 if (!p2ps_svc_found) {
2422 p2p_dbg(p2p, "No Service Hash match found");
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002423 p2p_parse_free(&msg);
2424 return P2P_PREQ_NOT_PROCESSED;
2425 }
2426 } else {
2427 /* This is not a P2PS Probe Request */
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002428 p2p_dbg(p2p, "No P2PS Hash in Probe Request");
2429
2430 if (!p2p->in_listen || !p2p->drv_in_listen) {
2431 /* not in Listen state - ignore Probe Request */
2432 p2p_dbg(p2p, "Not in Listen state (in_listen=%d drv_in_listen=%d) - ignore Probe Request",
2433 p2p->in_listen, p2p->drv_in_listen);
2434 p2p_parse_free(&msg);
2435 return P2P_PREQ_NOT_LISTEN;
2436 }
2437 }
2438
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002439 if (msg.device_id &&
Dmitry Shmidt04949592012-07-19 12:16:46 -07002440 os_memcmp(msg.device_id, p2p->cfg->dev_addr, ETH_ALEN) != 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002441 /* Device ID did not match */
Dmitry Shmidtec58b162014-02-19 12:44:18 -08002442 p2p_dbg(p2p, "Probe Req requested Device ID " MACSTR " did not match - ignore it",
2443 MAC2STR(msg.device_id));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002444 p2p_parse_free(&msg);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002445 return P2P_PREQ_NOT_PROCESSED;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002446 }
2447
2448 /* Check Requested Device Type match */
2449 if (msg.wps_attributes &&
2450 !p2p_match_dev_type(p2p, msg.wps_attributes)) {
2451 /* No match with Requested Device Type */
Hai Shalom021b0b52019-04-10 11:17:58 -07002452 p2p_dbg(p2p, "Probe Req requested Device Type did not match - ignore it");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002453 p2p_parse_free(&msg);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002454 return P2P_PREQ_NOT_PROCESSED;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002455 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002456
Dmitry Shmidt04949592012-07-19 12:16:46 -07002457 if (!p2p->cfg->send_probe_resp) {
2458 /* Response generated elsewhere */
Dmitry Shmidtec58b162014-02-19 12:44:18 -08002459 p2p_dbg(p2p, "Probe Resp generated elsewhere - do not generate additional response");
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002460 p2p_parse_free(&msg);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002461 return P2P_PREQ_NOT_PROCESSED;
2462 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002463
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002464 p2p_dbg(p2p, "Reply to P2P Probe Request in Listen state");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002465
2466 /*
2467 * We do not really have a specific BSS that this frame is advertising,
2468 * so build a frame that has some information in valid format. This is
2469 * really only used for discovery purposes, not to learn exact BSS
2470 * parameters.
2471 */
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002472 ies = p2p_build_probe_resp_ies(p2p, msg.service_hash,
2473 msg.service_hash_count);
2474 p2p_parse_free(&msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002475 if (ies == NULL)
Dmitry Shmidt04949592012-07-19 12:16:46 -07002476 return P2P_PREQ_NOT_PROCESSED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002477
2478 buf = wpabuf_alloc(200 + wpabuf_len(ies));
2479 if (buf == NULL) {
2480 wpabuf_free(ies);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002481 return P2P_PREQ_NOT_PROCESSED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002482 }
2483
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07002484 if (p2p_build_probe_resp_buf(p2p, buf, ies, addr, rx_freq)) {
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002485 wpabuf_free(ies);
2486 wpabuf_free(buf);
2487 return P2P_PREQ_NOT_PROCESSED;
2488 }
2489
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002490 wpabuf_free(ies);
2491
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002492 p2p->cfg->send_probe_resp(p2p->cfg->cb_ctx, buf, rx_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002493
2494 wpabuf_free(buf);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002495
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002496 return P2P_PREQ_PROCESSED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002497}
2498
2499
Dmitry Shmidt04949592012-07-19 12:16:46 -07002500enum p2p_probe_req_status
2501p2p_probe_req_rx(struct p2p_data *p2p, const u8 *addr, const u8 *dst,
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002502 const u8 *bssid, const u8 *ie, size_t ie_len,
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07002503 unsigned int rx_freq, int p2p_lo_started)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002504{
Dmitry Shmidt04949592012-07-19 12:16:46 -07002505 enum p2p_probe_req_status res;
2506
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002507 p2p_add_dev_from_probe_req(p2p, addr, ie, ie_len);
2508
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07002509 if (p2p_lo_started) {
2510 p2p_dbg(p2p,
2511 "Probe Response is offloaded, do not reply Probe Request");
2512 return P2P_PREQ_PROCESSED;
2513 }
2514
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002515 res = p2p_reply_probe(p2p, addr, dst, bssid, ie, ie_len, rx_freq);
2516 if (res != P2P_PREQ_PROCESSED && res != P2P_PREQ_NOT_PROCESSED)
2517 return res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002518
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002519 /*
2520 * Activate a pending GO Negotiation/Invite flow if a received Probe
2521 * Request frame is from an expected peer. Some devices may share the
2522 * same address for P2P and non-P2P STA running simultaneously. The
2523 * P2P_PREQ_PROCESSED and P2P_PREQ_NOT_PROCESSED p2p_reply_probe()
2524 * return values verified above ensure we are handling a Probe Request
2525 * frame from a P2P peer.
2526 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002527 if ((p2p->state == P2P_CONNECT || p2p->state == P2P_CONNECT_LISTEN) &&
2528 p2p->go_neg_peer &&
2529 os_memcmp(addr, p2p->go_neg_peer->info.p2p_device_addr, ETH_ALEN)
Dmitry Shmidt9cdf1b92013-02-27 12:58:50 -08002530 == 0 &&
2531 !(p2p->go_neg_peer->flags & P2P_DEV_WAIT_GO_NEG_CONFIRM)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002532 /* Received a Probe Request from GO Negotiation peer */
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002533 p2p_dbg(p2p, "Found GO Negotiation peer - try to start GO negotiation from timeout");
Dmitry Shmidt9cdf1b92013-02-27 12:58:50 -08002534 eloop_cancel_timeout(p2p_go_neg_start, p2p, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002535 eloop_register_timeout(0, 0, p2p_go_neg_start, p2p, NULL);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002536 return res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002537 }
2538
2539 if ((p2p->state == P2P_INVITE || p2p->state == P2P_INVITE_LISTEN) &&
2540 p2p->invite_peer &&
Dmitry Shmidt3c479372014-02-04 10:50:36 -08002541 (p2p->invite_peer->flags & P2P_DEV_WAIT_INV_REQ_ACK) &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002542 os_memcmp(addr, p2p->invite_peer->info.p2p_device_addr, ETH_ALEN)
2543 == 0) {
2544 /* Received a Probe Request from Invite peer */
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002545 p2p_dbg(p2p, "Found Invite peer - try to start Invite from timeout");
Dmitry Shmidt7f93d6f2014-02-21 11:22:49 -08002546 eloop_cancel_timeout(p2p_invite_start, p2p, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002547 eloop_register_timeout(0, 0, p2p_invite_start, p2p, NULL);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002548 return res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002549 }
2550
Dmitry Shmidt04949592012-07-19 12:16:46 -07002551 return res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002552}
2553
2554
2555static int p2p_assoc_req_ie_wlan_ap(struct p2p_data *p2p, const u8 *bssid,
2556 u8 *buf, size_t len, struct wpabuf *p2p_ie)
2557{
2558 struct wpabuf *tmp;
2559 u8 *lpos;
2560 size_t tmplen;
2561 int res;
2562 u8 group_capab;
Dmitry Shmidt05df46a2015-05-19 11:02:01 -07002563 struct p2p_message msg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002564
2565 if (p2p_ie == NULL)
2566 return 0; /* WLAN AP is not a P2P manager */
2567
Dmitry Shmidt05df46a2015-05-19 11:02:01 -07002568 os_memset(&msg, 0, sizeof(msg));
2569 if (p2p_parse_p2p_ie(p2p_ie, &msg) < 0)
2570 return 0;
2571
2572 p2p_dbg(p2p, "BSS P2P manageability %s",
2573 msg.manageability ? "enabled" : "disabled");
2574
2575 if (!msg.manageability)
2576 return 0;
2577
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002578 /*
2579 * (Re)Association Request - P2P IE
2580 * P2P Capability attribute (shall be present)
2581 * P2P Interface attribute (present if concurrent device and
2582 * P2P Management is enabled)
2583 */
2584 tmp = wpabuf_alloc(200);
2585 if (tmp == NULL)
2586 return -1;
2587
2588 lpos = p2p_buf_add_ie_hdr(tmp);
2589 group_capab = 0;
2590 if (p2p->num_groups > 0) {
2591 group_capab |= P2P_GROUP_CAPAB_GROUP_OWNER;
2592 if ((p2p->dev_capab & P2P_DEV_CAPAB_CONCURRENT_OPER) &&
2593 (p2p->dev_capab & P2P_DEV_CAPAB_INFRA_MANAGED) &&
2594 p2p->cross_connect)
2595 group_capab |= P2P_GROUP_CAPAB_CROSS_CONN;
2596 }
2597 p2p_buf_add_capability(tmp, p2p->dev_capab, group_capab);
2598 if ((p2p->dev_capab & P2P_DEV_CAPAB_CONCURRENT_OPER) &&
2599 (p2p->dev_capab & P2P_DEV_CAPAB_INFRA_MANAGED))
2600 p2p_buf_add_p2p_interface(tmp, p2p);
2601 p2p_buf_update_ie_hdr(tmp, lpos);
2602
2603 tmplen = wpabuf_len(tmp);
2604 if (tmplen > len)
2605 res = -1;
2606 else {
2607 os_memcpy(buf, wpabuf_head(tmp), tmplen);
2608 res = tmplen;
2609 }
2610 wpabuf_free(tmp);
2611
2612 return res;
2613}
2614
2615
2616int p2p_assoc_req_ie(struct p2p_data *p2p, const u8 *bssid, u8 *buf,
2617 size_t len, int p2p_group, struct wpabuf *p2p_ie)
2618{
2619 struct wpabuf *tmp;
2620 u8 *lpos;
2621 struct p2p_device *peer;
2622 size_t tmplen;
2623 int res;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002624 size_t extra = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002625
2626 if (!p2p_group)
2627 return p2p_assoc_req_ie_wlan_ap(p2p, bssid, buf, len, p2p_ie);
2628
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002629#ifdef CONFIG_WIFI_DISPLAY
2630 if (p2p->wfd_ie_assoc_req)
2631 extra = wpabuf_len(p2p->wfd_ie_assoc_req);
2632#endif /* CONFIG_WIFI_DISPLAY */
2633
Dmitry Shmidt2e67f062014-07-16 09:55:28 -07002634 if (p2p->vendor_elem && p2p->vendor_elem[VENDOR_ELEM_P2P_ASSOC_REQ])
2635 extra += wpabuf_len(p2p->vendor_elem[VENDOR_ELEM_P2P_ASSOC_REQ]);
2636
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002637 /*
2638 * (Re)Association Request - P2P IE
2639 * P2P Capability attribute (shall be present)
2640 * Extended Listen Timing (may be present)
2641 * P2P Device Info attribute (shall be present)
2642 */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002643 tmp = wpabuf_alloc(200 + extra);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002644 if (tmp == NULL)
2645 return -1;
2646
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002647#ifdef CONFIG_WIFI_DISPLAY
2648 if (p2p->wfd_ie_assoc_req)
2649 wpabuf_put_buf(tmp, p2p->wfd_ie_assoc_req);
2650#endif /* CONFIG_WIFI_DISPLAY */
2651
Dmitry Shmidt2e67f062014-07-16 09:55:28 -07002652 if (p2p->vendor_elem && p2p->vendor_elem[VENDOR_ELEM_P2P_ASSOC_REQ])
2653 wpabuf_put_buf(tmp,
2654 p2p->vendor_elem[VENDOR_ELEM_P2P_ASSOC_REQ]);
2655
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002656 peer = bssid ? p2p_get_device(p2p, bssid) : NULL;
2657
2658 lpos = p2p_buf_add_ie_hdr(tmp);
2659 p2p_buf_add_capability(tmp, p2p->dev_capab, 0);
2660 if (p2p->ext_listen_interval)
2661 p2p_buf_add_ext_listen_timing(tmp, p2p->ext_listen_period,
2662 p2p->ext_listen_interval);
2663 p2p_buf_add_device_info(tmp, p2p, peer);
2664 p2p_buf_update_ie_hdr(tmp, lpos);
2665
2666 tmplen = wpabuf_len(tmp);
2667 if (tmplen > len)
2668 res = -1;
2669 else {
2670 os_memcpy(buf, wpabuf_head(tmp), tmplen);
2671 res = tmplen;
2672 }
2673 wpabuf_free(tmp);
2674
2675 return res;
2676}
2677
2678
2679int p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf, char *end)
2680{
2681 struct wpabuf *p2p_ie;
2682 int ret;
2683
2684 p2p_ie = ieee802_11_vendor_ie_concat(ies, ies_len, P2P_IE_VENDOR_TYPE);
2685 if (p2p_ie == NULL)
2686 return 0;
2687
2688 ret = p2p_attr_text(p2p_ie, buf, end);
2689 wpabuf_free(p2p_ie);
2690 return ret;
2691}
2692
2693
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002694struct p2ps_advertisement *
2695p2p_service_p2ps_id(struct p2p_data *p2p, u32 adv_id)
2696{
2697 struct p2ps_advertisement *adv_data;
2698
2699 if (!p2p)
2700 return NULL;
2701
2702 adv_data = p2p->p2ps_adv_list;
2703 while (adv_data) {
2704 if (adv_data->id == adv_id)
2705 return adv_data;
2706 adv_data = adv_data->next;
2707 }
2708
2709 return NULL;
2710}
2711
2712
2713int p2p_service_del_asp(struct p2p_data *p2p, u32 adv_id)
2714{
2715 struct p2ps_advertisement *adv_data;
2716 struct p2ps_advertisement **prior;
2717
2718 if (!p2p)
2719 return -1;
2720
2721 adv_data = p2p->p2ps_adv_list;
2722 prior = &p2p->p2ps_adv_list;
2723 while (adv_data) {
2724 if (adv_data->id == adv_id) {
2725 p2p_dbg(p2p, "Delete ASP adv_id=0x%x", adv_id);
2726 *prior = adv_data->next;
2727 os_free(adv_data);
2728 return 0;
2729 }
2730 prior = &adv_data->next;
2731 adv_data = adv_data->next;
2732 }
2733
2734 return -1;
2735}
2736
2737
2738int p2p_service_add_asp(struct p2p_data *p2p, int auto_accept, u32 adv_id,
2739 const char *adv_str, u8 svc_state, u16 config_methods,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002740 const char *svc_info, const u8 *cpt_priority)
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002741{
2742 struct p2ps_advertisement *adv_data, *tmp, **prev;
2743 u8 buf[P2PS_HASH_LEN];
2744 size_t adv_data_len, adv_len, info_len = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002745 int i;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002746
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002747 if (!p2p || !adv_str || !adv_str[0] || !cpt_priority)
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002748 return -1;
2749
2750 if (!(config_methods & p2p->cfg->config_methods)) {
2751 p2p_dbg(p2p, "Config methods not supported svc: 0x%x dev: 0x%x",
2752 config_methods, p2p->cfg->config_methods);
2753 return -1;
2754 }
2755
2756 if (!p2ps_gen_hash(p2p, adv_str, buf))
2757 return -1;
2758
2759 if (svc_info)
2760 info_len = os_strlen(svc_info);
2761 adv_len = os_strlen(adv_str);
2762 adv_data_len = sizeof(struct p2ps_advertisement) + adv_len + 1 +
2763 info_len + 1;
2764
2765 adv_data = os_zalloc(adv_data_len);
2766 if (!adv_data)
2767 return -1;
2768
2769 os_memcpy(adv_data->hash, buf, P2PS_HASH_LEN);
2770 adv_data->id = adv_id;
2771 adv_data->state = svc_state;
2772 adv_data->config_methods = config_methods & p2p->cfg->config_methods;
2773 adv_data->auto_accept = (u8) auto_accept;
2774 os_memcpy(adv_data->svc_name, adv_str, adv_len);
2775
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002776 for (i = 0; cpt_priority[i] && i < P2PS_FEATURE_CAPAB_CPT_MAX; i++) {
2777 adv_data->cpt_priority[i] = cpt_priority[i];
2778 adv_data->cpt_mask |= cpt_priority[i];
2779 }
2780
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002781 if (svc_info && info_len) {
2782 adv_data->svc_info = &adv_data->svc_name[adv_len + 1];
2783 os_memcpy(adv_data->svc_info, svc_info, info_len);
2784 }
2785
2786 /*
2787 * Group Advertisements by service string. They do not need to be
2788 * sorted, but groups allow easier Probe Response instance grouping
2789 */
2790 tmp = p2p->p2ps_adv_list;
2791 prev = &p2p->p2ps_adv_list;
2792 while (tmp) {
2793 if (tmp->id == adv_data->id) {
2794 if (os_strcmp(tmp->svc_name, adv_data->svc_name) != 0) {
2795 os_free(adv_data);
2796 return -1;
2797 }
2798 adv_data->next = tmp->next;
2799 *prev = adv_data;
2800 os_free(tmp);
2801 goto inserted;
2802 } else {
2803 if (os_strcmp(tmp->svc_name, adv_data->svc_name) == 0) {
2804 adv_data->next = tmp->next;
2805 tmp->next = adv_data;
2806 goto inserted;
2807 }
2808 }
2809 prev = &tmp->next;
2810 tmp = tmp->next;
2811 }
2812
2813 /* No svc_name match found */
2814 adv_data->next = p2p->p2ps_adv_list;
2815 p2p->p2ps_adv_list = adv_data;
2816
2817inserted:
2818 p2p_dbg(p2p,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002819 "Added ASP advertisement adv_id=0x%x config_methods=0x%x svc_state=0x%x adv_str='%s' cpt_mask=0x%x",
2820 adv_id, adv_data->config_methods, svc_state, adv_str,
2821 adv_data->cpt_mask);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002822
2823 return 0;
2824}
2825
2826
Dmitry Shmidtdda10c22015-03-24 16:05:01 -07002827void p2p_service_flush_asp(struct p2p_data *p2p)
2828{
2829 struct p2ps_advertisement *adv, *prev;
2830
2831 if (!p2p)
2832 return;
2833
2834 adv = p2p->p2ps_adv_list;
2835 while (adv) {
2836 prev = adv;
2837 adv = adv->next;
2838 os_free(prev);
2839 }
2840
2841 p2p->p2ps_adv_list = NULL;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002842 p2ps_prov_free(p2p);
Dmitry Shmidtdda10c22015-03-24 16:05:01 -07002843 p2p_dbg(p2p, "All ASP advertisements flushed");
2844}
2845
2846
Dmitry Shmidt04949592012-07-19 12:16:46 -07002847int p2p_parse_dev_addr_in_p2p_ie(struct wpabuf *p2p_ie, u8 *dev_addr)
2848{
2849 struct p2p_message msg;
2850
2851 os_memset(&msg, 0, sizeof(msg));
2852 if (p2p_parse_p2p_ie(p2p_ie, &msg))
2853 return -1;
2854
2855 if (msg.p2p_device_addr) {
2856 os_memcpy(dev_addr, msg.p2p_device_addr, ETH_ALEN);
2857 return 0;
2858 } else if (msg.device_id) {
2859 os_memcpy(dev_addr, msg.device_id, ETH_ALEN);
2860 return 0;
2861 }
2862 return -1;
2863}
2864
2865
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002866int p2p_parse_dev_addr(const u8 *ies, size_t ies_len, u8 *dev_addr)
2867{
2868 struct wpabuf *p2p_ie;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002869 int ret;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002870
2871 p2p_ie = ieee802_11_vendor_ie_concat(ies, ies_len,
2872 P2P_IE_VENDOR_TYPE);
2873 if (p2p_ie == NULL)
2874 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002875 ret = p2p_parse_dev_addr_in_p2p_ie(p2p_ie, dev_addr);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002876 wpabuf_free(p2p_ie);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002877 return ret;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002878}
2879
2880
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002881static void p2p_clear_go_neg(struct p2p_data *p2p)
2882{
2883 p2p->go_neg_peer = NULL;
2884 p2p_clear_timeout(p2p);
2885 p2p_set_state(p2p, P2P_IDLE);
2886}
2887
2888
2889void p2p_wps_success_cb(struct p2p_data *p2p, const u8 *mac_addr)
2890{
2891 if (p2p->go_neg_peer == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002892 p2p_dbg(p2p, "No pending Group Formation - ignore WPS registration success notification");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002893 return; /* No pending Group Formation */
2894 }
2895
2896 if (os_memcmp(mac_addr, p2p->go_neg_peer->intended_addr, ETH_ALEN) !=
2897 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002898 p2p_dbg(p2p, "Ignore WPS registration success notification for "
2899 MACSTR " (GO Negotiation peer " MACSTR ")",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002900 MAC2STR(mac_addr),
2901 MAC2STR(p2p->go_neg_peer->intended_addr));
2902 return; /* Ignore unexpected peer address */
2903 }
2904
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002905 p2p_dbg(p2p, "Group Formation completed successfully with " MACSTR,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002906 MAC2STR(mac_addr));
2907
2908 p2p_clear_go_neg(p2p);
2909}
2910
2911
2912void p2p_group_formation_failed(struct p2p_data *p2p)
2913{
2914 if (p2p->go_neg_peer == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002915 p2p_dbg(p2p, "No pending Group Formation - ignore group formation failure notification");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002916 return; /* No pending Group Formation */
2917 }
2918
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002919 p2p_dbg(p2p, "Group Formation failed with " MACSTR,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002920 MAC2STR(p2p->go_neg_peer->intended_addr));
2921
2922 p2p_clear_go_neg(p2p);
2923}
2924
2925
2926struct p2p_data * p2p_init(const struct p2p_config *cfg)
2927{
2928 struct p2p_data *p2p;
2929
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07002930 if (cfg->max_peers < 1 ||
2931 cfg->passphrase_len < 8 || cfg->passphrase_len > 63)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002932 return NULL;
2933
2934 p2p = os_zalloc(sizeof(*p2p) + sizeof(*cfg));
2935 if (p2p == NULL)
2936 return NULL;
2937 p2p->cfg = (struct p2p_config *) (p2p + 1);
2938 os_memcpy(p2p->cfg, cfg, sizeof(*cfg));
2939 if (cfg->dev_name)
2940 p2p->cfg->dev_name = os_strdup(cfg->dev_name);
2941 if (cfg->manufacturer)
2942 p2p->cfg->manufacturer = os_strdup(cfg->manufacturer);
2943 if (cfg->model_name)
2944 p2p->cfg->model_name = os_strdup(cfg->model_name);
2945 if (cfg->model_number)
2946 p2p->cfg->model_number = os_strdup(cfg->model_number);
2947 if (cfg->serial_number)
2948 p2p->cfg->serial_number = os_strdup(cfg->serial_number);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002949 if (cfg->pref_chan) {
2950 p2p->cfg->pref_chan = os_malloc(cfg->num_pref_chan *
2951 sizeof(struct p2p_channel));
2952 if (p2p->cfg->pref_chan) {
2953 os_memcpy(p2p->cfg->pref_chan, cfg->pref_chan,
2954 cfg->num_pref_chan *
2955 sizeof(struct p2p_channel));
2956 } else
2957 p2p->cfg->num_pref_chan = 0;
2958 }
2959
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002960 p2ps_gen_hash(p2p, P2PS_WILD_HASH_STR, p2p->wild_card_hash);
2961
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002962 p2p->min_disc_int = 1;
2963 p2p->max_disc_int = 3;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002964 p2p->max_disc_tu = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002965
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07002966 if (os_get_random(&p2p->next_tie_breaker, 1) < 0)
2967 p2p->next_tie_breaker = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002968 p2p->next_tie_breaker &= 0x01;
2969 if (cfg->sd_request)
2970 p2p->dev_capab |= P2P_DEV_CAPAB_SERVICE_DISCOVERY;
2971 p2p->dev_capab |= P2P_DEV_CAPAB_INVITATION_PROCEDURE;
2972 if (cfg->concurrent_operations)
2973 p2p->dev_capab |= P2P_DEV_CAPAB_CONCURRENT_OPER;
2974 p2p->dev_capab |= P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
2975
2976 dl_list_init(&p2p->devices);
2977
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002978 p2p->go_timeout = 100;
2979 p2p->client_timeout = 20;
Dmitry Shmidt13ca8d82014-02-20 10:18:40 -08002980 p2p->num_p2p_sd_queries = 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002981
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002982 p2p_dbg(p2p, "initialized");
2983 p2p_channels_dump(p2p, "channels", &p2p->cfg->channels);
2984 p2p_channels_dump(p2p, "cli_channels", &p2p->cfg->cli_channels);
2985
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002986 return p2p;
2987}
2988
2989
2990void p2p_deinit(struct p2p_data *p2p)
2991{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002992#ifdef CONFIG_WIFI_DISPLAY
2993 wpabuf_free(p2p->wfd_ie_beacon);
2994 wpabuf_free(p2p->wfd_ie_probe_req);
2995 wpabuf_free(p2p->wfd_ie_probe_resp);
2996 wpabuf_free(p2p->wfd_ie_assoc_req);
2997 wpabuf_free(p2p->wfd_ie_invitation);
2998 wpabuf_free(p2p->wfd_ie_prov_disc_req);
2999 wpabuf_free(p2p->wfd_ie_prov_disc_resp);
3000 wpabuf_free(p2p->wfd_ie_go_neg);
3001 wpabuf_free(p2p->wfd_dev_info);
3002 wpabuf_free(p2p->wfd_assoc_bssid);
3003 wpabuf_free(p2p->wfd_coupled_sink_info);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003004 wpabuf_free(p2p->wfd_r2_dev_info);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003005#endif /* CONFIG_WIFI_DISPLAY */
3006
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003007 eloop_cancel_timeout(p2p_scan_timeout, p2p, NULL);
Dmitry Shmidt9cdf1b92013-02-27 12:58:50 -08003008 eloop_cancel_timeout(p2p_go_neg_start, p2p, NULL);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003009 eloop_cancel_timeout(p2p_go_neg_wait_timeout, p2p, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003010 p2p_flush(p2p);
3011 p2p_free_req_dev_types(p2p);
3012 os_free(p2p->cfg->dev_name);
3013 os_free(p2p->cfg->manufacturer);
3014 os_free(p2p->cfg->model_name);
3015 os_free(p2p->cfg->model_number);
3016 os_free(p2p->cfg->serial_number);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003017 os_free(p2p->cfg->pref_chan);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003018 os_free(p2p->groups);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07003019 p2ps_prov_free(p2p);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003020 wpabuf_free(p2p->sd_resp);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003021 p2p_remove_wps_vendor_extensions(p2p);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003022 os_free(p2p->no_go_freq.range);
Dmitry Shmidtdda10c22015-03-24 16:05:01 -07003023 p2p_service_flush_asp(p2p);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003024
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003025 os_free(p2p);
3026}
3027
3028
3029void p2p_flush(struct p2p_data *p2p)
3030{
3031 struct p2p_device *dev, *prev;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003032
3033 p2p_ext_listen(p2p, 0, 0);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003034 p2p_stop_find(p2p);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003035 dl_list_for_each_safe(dev, prev, &p2p->devices, struct p2p_device,
3036 list) {
3037 dl_list_del(&dev->list);
3038 p2p_device_free(p2p, dev);
3039 }
3040 p2p_free_sd_queries(p2p);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003041 p2p->ssid_set = 0;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003042 p2ps_prov_free(p2p);
3043 p2p_reset_pending_pd(p2p);
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08003044 p2p->override_pref_op_class = 0;
3045 p2p->override_pref_channel = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003046}
3047
3048
3049int p2p_unauthorize(struct p2p_data *p2p, const u8 *addr)
3050{
3051 struct p2p_device *dev;
3052
3053 dev = p2p_get_device(p2p, addr);
3054 if (dev == NULL)
3055 return -1;
3056
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003057 p2p_dbg(p2p, "Unauthorizing " MACSTR, MAC2STR(addr));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003058
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003059 if (p2p->go_neg_peer == dev) {
3060 eloop_cancel_timeout(p2p_go_neg_wait_timeout, p2p, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003061 p2p->go_neg_peer = NULL;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003062 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003063
3064 dev->wps_method = WPS_NOT_READY;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003065 dev->oob_pw_id = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003066 dev->flags &= ~P2P_DEV_WAIT_GO_NEG_RESPONSE;
3067 dev->flags &= ~P2P_DEV_WAIT_GO_NEG_CONFIRM;
3068
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003069 return 0;
3070}
3071
3072
3073int p2p_set_dev_name(struct p2p_data *p2p, const char *dev_name)
3074{
3075 os_free(p2p->cfg->dev_name);
3076 if (dev_name) {
3077 p2p->cfg->dev_name = os_strdup(dev_name);
3078 if (p2p->cfg->dev_name == NULL)
3079 return -1;
3080 } else
3081 p2p->cfg->dev_name = NULL;
3082 return 0;
3083}
3084
3085
3086int p2p_set_manufacturer(struct p2p_data *p2p, const char *manufacturer)
3087{
3088 os_free(p2p->cfg->manufacturer);
3089 p2p->cfg->manufacturer = NULL;
3090 if (manufacturer) {
3091 p2p->cfg->manufacturer = os_strdup(manufacturer);
3092 if (p2p->cfg->manufacturer == NULL)
3093 return -1;
3094 }
3095
3096 return 0;
3097}
3098
3099
3100int p2p_set_model_name(struct p2p_data *p2p, const char *model_name)
3101{
3102 os_free(p2p->cfg->model_name);
3103 p2p->cfg->model_name = NULL;
3104 if (model_name) {
3105 p2p->cfg->model_name = os_strdup(model_name);
3106 if (p2p->cfg->model_name == NULL)
3107 return -1;
3108 }
3109
3110 return 0;
3111}
3112
3113
3114int p2p_set_model_number(struct p2p_data *p2p, const char *model_number)
3115{
3116 os_free(p2p->cfg->model_number);
3117 p2p->cfg->model_number = NULL;
3118 if (model_number) {
3119 p2p->cfg->model_number = os_strdup(model_number);
3120 if (p2p->cfg->model_number == NULL)
3121 return -1;
3122 }
3123
3124 return 0;
3125}
3126
3127
3128int p2p_set_serial_number(struct p2p_data *p2p, const char *serial_number)
3129{
3130 os_free(p2p->cfg->serial_number);
3131 p2p->cfg->serial_number = NULL;
3132 if (serial_number) {
3133 p2p->cfg->serial_number = os_strdup(serial_number);
3134 if (p2p->cfg->serial_number == NULL)
3135 return -1;
3136 }
3137
3138 return 0;
3139}
3140
3141
3142void p2p_set_config_methods(struct p2p_data *p2p, u16 config_methods)
3143{
3144 p2p->cfg->config_methods = config_methods;
3145}
3146
3147
3148void p2p_set_uuid(struct p2p_data *p2p, const u8 *uuid)
3149{
3150 os_memcpy(p2p->cfg->uuid, uuid, 16);
3151}
3152
3153
3154int p2p_set_pri_dev_type(struct p2p_data *p2p, const u8 *pri_dev_type)
3155{
3156 os_memcpy(p2p->cfg->pri_dev_type, pri_dev_type, 8);
3157 return 0;
3158}
3159
3160
3161int p2p_set_sec_dev_types(struct p2p_data *p2p, const u8 dev_types[][8],
3162 size_t num_dev_types)
3163{
3164 if (num_dev_types > P2P_SEC_DEVICE_TYPES)
3165 num_dev_types = P2P_SEC_DEVICE_TYPES;
3166 p2p->cfg->num_sec_dev_types = num_dev_types;
3167 os_memcpy(p2p->cfg->sec_dev_type, dev_types, num_dev_types * 8);
3168 return 0;
3169}
3170
3171
3172void p2p_remove_wps_vendor_extensions(struct p2p_data *p2p)
3173{
3174 int i;
3175
3176 for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) {
3177 wpabuf_free(p2p->wps_vendor_ext[i]);
3178 p2p->wps_vendor_ext[i] = NULL;
3179 }
3180}
3181
3182
3183int p2p_add_wps_vendor_extension(struct p2p_data *p2p,
3184 const struct wpabuf *vendor_ext)
3185{
3186 int i;
3187
3188 if (vendor_ext == NULL)
3189 return -1;
3190
3191 for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) {
3192 if (p2p->wps_vendor_ext[i] == NULL)
3193 break;
3194 }
3195 if (i >= P2P_MAX_WPS_VENDOR_EXT)
3196 return -1;
3197
3198 p2p->wps_vendor_ext[i] = wpabuf_dup(vendor_ext);
3199 if (p2p->wps_vendor_ext[i] == NULL)
3200 return -1;
3201
3202 return 0;
3203}
3204
3205
3206int p2p_set_country(struct p2p_data *p2p, const char *country)
3207{
3208 os_memcpy(p2p->cfg->country, country, 3);
3209 return 0;
3210}
3211
3212
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003213static int p2p_pre_find_operation(struct p2p_data *p2p, struct p2p_device *dev)
3214{
Dmitry Shmidtde47be72016-01-07 12:52:55 -08003215 int res;
3216
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003217 if (dev->sd_pending_bcast_queries == 0) {
3218 /* Initialize with total number of registered broadcast
3219 * SD queries. */
3220 dev->sd_pending_bcast_queries = p2p->num_p2p_sd_queries;
3221 }
3222
Dmitry Shmidtde47be72016-01-07 12:52:55 -08003223 res = p2p_start_sd(p2p, dev);
3224 if (res == -2)
3225 return -2;
3226 if (res == 0)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003227 return 1;
3228
3229 if (dev->req_config_methods &&
3230 !(dev->flags & P2P_DEV_PD_FOR_JOIN)) {
3231 p2p_dbg(p2p, "Send pending Provision Discovery Request to "
3232 MACSTR " (config methods 0x%x)",
3233 MAC2STR(dev->info.p2p_device_addr),
3234 dev->req_config_methods);
3235 if (p2p_send_prov_disc_req(p2p, dev, 0, 0) == 0)
3236 return 1;
3237 }
3238
3239 return 0;
3240}
3241
3242
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003243void p2p_continue_find(struct p2p_data *p2p)
3244{
3245 struct p2p_device *dev;
Dmitry Shmidtde47be72016-01-07 12:52:55 -08003246 int found, res;
Dmitry Shmidt13ca8d82014-02-20 10:18:40 -08003247
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003248 p2p_set_state(p2p, P2P_SEARCH);
3249
3250 /* Continue from the device following the last iteration */
3251 found = 0;
3252 dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
3253 if (dev == p2p->last_p2p_find_oper) {
3254 found = 1;
3255 continue;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003256 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003257 if (!found)
3258 continue;
Dmitry Shmidtde47be72016-01-07 12:52:55 -08003259 res = p2p_pre_find_operation(p2p, dev);
3260 if (res > 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003261 p2p->last_p2p_find_oper = dev;
3262 return;
3263 }
Dmitry Shmidtde47be72016-01-07 12:52:55 -08003264 if (res == -2)
3265 goto skip_sd;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003266 }
3267
3268 /*
3269 * Wrap around to the beginning of the list and continue until the last
3270 * iteration device.
3271 */
3272 dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
Dmitry Shmidtde47be72016-01-07 12:52:55 -08003273 res = p2p_pre_find_operation(p2p, dev);
3274 if (res > 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003275 p2p->last_p2p_find_oper = dev;
3276 return;
3277 }
Dmitry Shmidtde47be72016-01-07 12:52:55 -08003278 if (res == -2)
3279 goto skip_sd;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003280 if (dev == p2p->last_p2p_find_oper)
3281 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003282 }
3283
Dmitry Shmidtde47be72016-01-07 12:52:55 -08003284skip_sd:
3285 os_memset(p2p->sd_query_no_ack, 0, ETH_ALEN);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003286 p2p_listen_in_find(p2p, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003287}
3288
3289
3290static void p2p_sd_cb(struct p2p_data *p2p, int success)
3291{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003292 p2p_dbg(p2p, "Service Discovery Query TX callback: success=%d",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003293 success);
3294 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
3295
3296 if (!success) {
Dmitry Shmidtde47be72016-01-07 12:52:55 -08003297 if (p2p->sd_peer) {
3298 if (is_zero_ether_addr(p2p->sd_query_no_ack)) {
3299 os_memcpy(p2p->sd_query_no_ack,
3300 p2p->sd_peer->info.p2p_device_addr,
3301 ETH_ALEN);
3302 p2p_dbg(p2p,
3303 "First SD Query no-ACK in this search iteration: "
3304 MACSTR, MAC2STR(p2p->sd_query_no_ack));
3305 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003306 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
Dmitry Shmidtde47be72016-01-07 12:52:55 -08003307 }
Dmitry Shmidt13ca8d82014-02-20 10:18:40 -08003308 p2p->sd_peer = NULL;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003309 if (p2p->state != P2P_IDLE)
3310 p2p_continue_find(p2p);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003311 return;
3312 }
3313
3314 if (p2p->sd_peer == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003315 p2p_dbg(p2p, "No SD peer entry known");
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003316 if (p2p->state != P2P_IDLE)
3317 p2p_continue_find(p2p);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003318 return;
3319 }
3320
Dmitry Shmidt807291d2015-01-27 13:40:23 -08003321 if (p2p->sd_query && p2p->sd_query->for_all_peers) {
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07003322 /* Update the pending broadcast SD query count for this device
3323 */
3324 p2p->sd_peer->sd_pending_bcast_queries--;
3325
3326 /*
3327 * If there are no pending broadcast queries for this device,
3328 * mark it as done (-1).
3329 */
3330 if (p2p->sd_peer->sd_pending_bcast_queries == 0)
3331 p2p->sd_peer->sd_pending_bcast_queries = -1;
3332 }
3333
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003334 /* Wait for response from the peer */
3335 p2p_set_state(p2p, P2P_SD_DURING_FIND);
3336 p2p_set_timeout(p2p, 0, 200000);
3337}
3338
Jouni Malinen75ecf522011-06-27 15:19:46 -07003339
3340/**
3341 * p2p_retry_pd - Retry any pending provision disc requests in IDLE state
3342 * @p2p: P2P module context from p2p_init()
3343 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003344static void p2p_retry_pd(struct p2p_data *p2p)
Jouni Malinen75ecf522011-06-27 15:19:46 -07003345{
3346 struct p2p_device *dev;
3347
Jouni Malinen75ecf522011-06-27 15:19:46 -07003348 /*
3349 * Retry the prov disc req attempt only for the peer that the user had
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003350 * requested.
Jouni Malinen75ecf522011-06-27 15:19:46 -07003351 */
3352
3353 dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
3354 if (os_memcmp(p2p->pending_pd_devaddr,
3355 dev->info.p2p_device_addr, ETH_ALEN) != 0)
3356 continue;
3357 if (!dev->req_config_methods)
3358 continue;
Jouni Malinen75ecf522011-06-27 15:19:46 -07003359
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003360 p2p_dbg(p2p, "Send pending Provision Discovery Request to "
Jouni Malinen75ecf522011-06-27 15:19:46 -07003361 MACSTR " (config methods 0x%x)",
3362 MAC2STR(dev->info.p2p_device_addr),
3363 dev->req_config_methods);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003364 p2p_send_prov_disc_req(p2p, dev,
Dmitry Shmidt051af732013-10-22 13:52:46 -07003365 dev->flags & P2P_DEV_PD_FOR_JOIN,
3366 p2p->pd_force_freq);
Jouni Malinen75ecf522011-06-27 15:19:46 -07003367 return;
3368 }
3369}
3370
3371
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003372static void p2p_prov_disc_cb(struct p2p_data *p2p, int success)
3373{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003374 p2p_dbg(p2p, "Provision Discovery Request TX callback: success=%d",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003375 success);
Jouni Malinen75ecf522011-06-27 15:19:46 -07003376
3377 /*
3378 * Postpone resetting the pending action state till after we actually
3379 * time out. This allows us to take some action like notifying any
3380 * interested parties about no response to the request.
3381 *
3382 * When the timer (below) goes off we check in IDLE, SEARCH, or
3383 * LISTEN_ONLY state, which are the only allowed states to issue a PD
3384 * requests in, if this was still pending and then raise notification.
3385 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003386
3387 if (!success) {
Jouni Malinen75ecf522011-06-27 15:19:46 -07003388 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
3389
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003390 if (p2p->user_initiated_pd &&
3391 (p2p->state == P2P_SEARCH || p2p->state == P2P_LISTEN_ONLY))
3392 {
3393 /* Retry request from timeout to avoid busy loops */
3394 p2p->pending_action_state = P2P_PENDING_PD;
3395 p2p_set_timeout(p2p, 0, 50000);
3396 } else if (p2p->state != P2P_IDLE)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003397 p2p_continue_find(p2p);
Jouni Malinen75ecf522011-06-27 15:19:46 -07003398 else if (p2p->user_initiated_pd) {
3399 p2p->pending_action_state = P2P_PENDING_PD;
3400 p2p_set_timeout(p2p, 0, 300000);
3401 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003402 return;
3403 }
3404
Jouni Malinen75ecf522011-06-27 15:19:46 -07003405 /*
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003406 * If after PD Request the peer doesn't expect to receive PD Response
3407 * the PD Request ACK indicates a completion of the current PD. This
3408 * happens only on the advertiser side sending the follow-on PD Request
3409 * with the status different than 12 (Success: accepted by user).
3410 */
3411 if (p2p->p2ps_prov && !p2p->p2ps_prov->pd_seeker &&
3412 p2p->p2ps_prov->status != P2P_SC_SUCCESS_DEFERRED) {
3413 p2p_dbg(p2p, "P2PS PD completion on Follow-on PD Request ACK");
3414
3415 if (p2p->send_action_in_progress) {
3416 p2p->send_action_in_progress = 0;
3417 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
3418 }
3419
3420 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
3421
3422 if (p2p->cfg->p2ps_prov_complete) {
3423 p2p->cfg->p2ps_prov_complete(
3424 p2p->cfg->cb_ctx,
3425 p2p->p2ps_prov->status,
3426 p2p->p2ps_prov->adv_mac,
3427 p2p->p2ps_prov->adv_mac,
3428 p2p->p2ps_prov->session_mac,
3429 NULL, p2p->p2ps_prov->adv_id,
3430 p2p->p2ps_prov->session_id,
3431 0, 0, NULL, 0, 0, 0,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08003432 NULL, NULL, 0, 0, NULL, 0);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003433 }
3434
3435 if (p2p->user_initiated_pd)
3436 p2p_reset_pending_pd(p2p);
3437
3438 p2ps_prov_free(p2p);
3439 return;
3440 }
3441
3442 /*
Jouni Malinen75ecf522011-06-27 15:19:46 -07003443 * This postponing, of resetting pending_action_state, needs to be
3444 * done only for user initiated PD requests and not internal ones.
3445 */
3446 if (p2p->user_initiated_pd)
3447 p2p->pending_action_state = P2P_PENDING_PD;
3448 else
3449 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
3450
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003451 /* Wait for response from the peer */
3452 if (p2p->state == P2P_SEARCH)
3453 p2p_set_state(p2p, P2P_PD_DURING_FIND);
3454 p2p_set_timeout(p2p, 0, 200000);
3455}
3456
3457
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003458static void p2p_prov_disc_resp_cb(struct p2p_data *p2p, int success)
3459{
3460 p2p_dbg(p2p, "Provision Discovery Response TX callback: success=%d",
3461 success);
3462
3463 if (p2p->send_action_in_progress) {
3464 p2p->send_action_in_progress = 0;
3465 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
3466 }
3467
3468 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
3469
Jimmy Chen5ef7aad2019-10-15 15:45:26 +08003470 if (!success) {
3471 if (p2p->state == P2P_SEARCH)
3472 p2p_continue_find(p2p);
Hai Shalom81f62d82019-07-22 12:10:00 -07003473 return;
Jimmy Chen5ef7aad2019-10-15 15:45:26 +08003474 }
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003475
3476 if (!p2p->cfg->prov_disc_resp_cb ||
Jimmy Chen5ef7aad2019-10-15 15:45:26 +08003477 p2p->cfg->prov_disc_resp_cb(p2p->cfg->cb_ctx) < 1) {
3478 if (p2p->state == P2P_SEARCH)
3479 p2p_continue_find(p2p);
Hai Shalom81f62d82019-07-22 12:10:00 -07003480 return;
Jimmy Chen5ef7aad2019-10-15 15:45:26 +08003481 }
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003482
3483 p2p_dbg(p2p,
3484 "Post-Provision Discovery operations started - do not try to continue other P2P operations");
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003485}
3486
3487
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003488int p2p_scan_res_handler(struct p2p_data *p2p, const u8 *bssid, int freq,
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003489 struct os_reltime *rx_time, int level, const u8 *ies,
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003490 size_t ies_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003491{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003492 if (os_reltime_before(rx_time, &p2p->find_start)) {
Dmitry Shmidtf8623282013-02-20 14:34:59 -08003493 /*
3494 * The driver may have cached (e.g., in cfg80211 BSS table) the
3495 * scan results for relatively long time. To avoid reporting
3496 * stale information, update P2P peers only based on results
3497 * that have based on frames received after the last p2p_find
3498 * operation was started.
3499 */
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003500 p2p_dbg(p2p, "Ignore old scan result for " MACSTR
Dmitry Shmidtde47be72016-01-07 12:52:55 -08003501 " (rx_time=%u.%06u find_start=%u.%06u)",
Dmitry Shmidtf8623282013-02-20 14:34:59 -08003502 MAC2STR(bssid), (unsigned int) rx_time->sec,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08003503 (unsigned int) rx_time->usec,
3504 (unsigned int) p2p->find_start.sec,
3505 (unsigned int) p2p->find_start.usec);
Dmitry Shmidtf8623282013-02-20 14:34:59 -08003506 return 0;
3507 }
3508
3509 p2p_add_device(p2p, bssid, freq, rx_time, level, ies, ies_len, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003510
3511 return 0;
3512}
3513
3514
3515void p2p_scan_res_handled(struct p2p_data *p2p)
3516{
3517 if (!p2p->p2p_scan_running) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003518 p2p_dbg(p2p, "p2p_scan was not running, but scan results received");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003519 }
3520 p2p->p2p_scan_running = 0;
3521 eloop_cancel_timeout(p2p_scan_timeout, p2p, NULL);
3522
3523 if (p2p_run_after_scan(p2p))
3524 return;
3525 if (p2p->state == P2P_SEARCH)
3526 p2p_continue_find(p2p);
3527}
3528
3529
Dmitry Shmidt9c175262016-03-03 10:20:07 -08003530void p2p_scan_ie(struct p2p_data *p2p, struct wpabuf *ies, const u8 *dev_id,
3531 unsigned int bands)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003532{
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003533 u8 dev_capab;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003534 u8 *len;
3535
3536#ifdef CONFIG_WIFI_DISPLAY
3537 if (p2p->wfd_ie_probe_req)
3538 wpabuf_put_buf(ies, p2p->wfd_ie_probe_req);
3539#endif /* CONFIG_WIFI_DISPLAY */
3540
Dmitry Shmidt2e67f062014-07-16 09:55:28 -07003541 if (p2p->vendor_elem && p2p->vendor_elem[VENDOR_ELEM_PROBE_REQ_P2P])
3542 wpabuf_put_buf(ies,
3543 p2p->vendor_elem[VENDOR_ELEM_PROBE_REQ_P2P]);
3544
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003545 len = p2p_buf_add_ie_hdr(ies);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003546
3547 dev_capab = p2p->dev_capab & ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
3548
3549 /* P2PS requires Probe Request frames to include SD bit */
3550 if (p2p->p2ps_seek && p2p->p2ps_seek_count)
3551 dev_capab |= P2P_DEV_CAPAB_SERVICE_DISCOVERY;
3552
3553 p2p_buf_add_capability(ies, dev_capab, 0);
3554
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003555 if (dev_id)
3556 p2p_buf_add_device_id(ies, dev_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003557 if (p2p->cfg->reg_class && p2p->cfg->channel)
3558 p2p_buf_add_listen_channel(ies, p2p->cfg->country,
3559 p2p->cfg->reg_class,
3560 p2p->cfg->channel);
3561 if (p2p->ext_listen_interval)
3562 p2p_buf_add_ext_listen_timing(ies, p2p->ext_listen_period,
3563 p2p->ext_listen_interval);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003564
Dmitry Shmidt9c175262016-03-03 10:20:07 -08003565 if (bands & BAND_60_GHZ)
3566 p2p_buf_add_device_info(ies, p2p, NULL);
3567
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003568 if (p2p->p2ps_seek && p2p->p2ps_seek_count)
3569 p2p_buf_add_service_hash(ies, p2p);
3570
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003571 /* TODO: p2p_buf_add_operating_channel() if GO */
3572 p2p_buf_update_ie_hdr(ies, len);
3573}
3574
3575
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003576size_t p2p_scan_ie_buf_len(struct p2p_data *p2p)
3577{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003578 size_t len = 100;
3579
3580#ifdef CONFIG_WIFI_DISPLAY
3581 if (p2p && p2p->wfd_ie_probe_req)
3582 len += wpabuf_len(p2p->wfd_ie_probe_req);
3583#endif /* CONFIG_WIFI_DISPLAY */
3584
Dmitry Shmidt2e67f062014-07-16 09:55:28 -07003585 if (p2p && p2p->vendor_elem &&
3586 p2p->vendor_elem[VENDOR_ELEM_PROBE_REQ_P2P])
3587 len += wpabuf_len(p2p->vendor_elem[VENDOR_ELEM_PROBE_REQ_P2P]);
3588
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003589 return len;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003590}
3591
3592
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003593int p2p_ie_text(struct wpabuf *p2p_ie, char *buf, char *end)
3594{
3595 return p2p_attr_text(p2p_ie, buf, end);
3596}
3597
3598
3599static void p2p_go_neg_req_cb(struct p2p_data *p2p, int success)
3600{
3601 struct p2p_device *dev = p2p->go_neg_peer;
Dmitry Shmidt8c652892013-03-01 10:14:01 -08003602 int timeout;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003603
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003604 p2p_dbg(p2p, "GO Negotiation Request TX callback: success=%d", success);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003605
3606 if (dev == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003607 p2p_dbg(p2p, "No pending GO Negotiation");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003608 return;
3609 }
3610
3611 if (success) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003612 if (dev->flags & P2P_DEV_USER_REJECTED) {
3613 p2p_set_state(p2p, P2P_IDLE);
3614 return;
3615 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07003616 } else if (dev->go_neg_req_sent) {
3617 /* Cancel the increment from p2p_connect_send() on failure */
Dmitry Shmidt98f9e762012-05-30 11:18:46 -07003618 dev->go_neg_req_sent--;
3619 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003620
3621 if (!success &&
3622 (dev->info.dev_capab & P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY) &&
3623 !is_zero_ether_addr(dev->member_in_go_dev)) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003624 p2p_dbg(p2p, "Peer " MACSTR " did not acknowledge request - try to use device discoverability through its GO",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003625 MAC2STR(dev->info.p2p_device_addr));
3626 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
3627 p2p_send_dev_disc_req(p2p, dev);
3628 return;
3629 }
3630
3631 /*
3632 * Use P2P find, if needed, to find the other device from its listen
3633 * channel.
3634 */
3635 p2p_set_state(p2p, P2P_CONNECT);
Dmitry Shmidt8c652892013-03-01 10:14:01 -08003636 timeout = success ? 500000 : 100000;
3637 if (!success && p2p->go_neg_peer &&
3638 (p2p->go_neg_peer->flags & P2P_DEV_PEER_WAITING_RESPONSE)) {
3639 unsigned int r;
3640 /*
3641 * Peer is expected to wait our response and we will skip the
3642 * listen phase. Add some randomness to the wait time here to
3643 * make it less likely to hit cases where we could end up in
3644 * sync with peer not listening.
3645 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07003646 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
3647 r = 0;
Dmitry Shmidt8c652892013-03-01 10:14:01 -08003648 timeout += r % 100000;
3649 }
3650 p2p_set_timeout(p2p, 0, timeout);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003651}
3652
3653
3654static void p2p_go_neg_resp_cb(struct p2p_data *p2p, int success)
3655{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003656 p2p_dbg(p2p, "GO Negotiation Response TX callback: success=%d",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003657 success);
3658 if (!p2p->go_neg_peer && p2p->state == P2P_PROVISIONING) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003659 p2p_dbg(p2p, "Ignore TX callback event - GO Negotiation is not running anymore");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003660 return;
3661 }
3662 p2p_set_state(p2p, P2P_CONNECT);
Dmitry Shmidt8c652892013-03-01 10:14:01 -08003663 p2p_set_timeout(p2p, 0, 500000);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003664}
3665
3666
Dmitry Shmidt4b060592013-04-29 16:42:49 -07003667static void p2p_go_neg_resp_failure_cb(struct p2p_data *p2p, int success,
3668 const u8 *addr)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003669{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003670 p2p_dbg(p2p, "GO Negotiation Response (failure) TX callback: success=%d", success);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003671 if (p2p->go_neg_peer && p2p->go_neg_peer->status != P2P_SC_SUCCESS) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003672 p2p_go_neg_failed(p2p, p2p->go_neg_peer->status);
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08003673 return;
3674 }
3675
3676 if (success) {
Dmitry Shmidt4b060592013-04-29 16:42:49 -07003677 struct p2p_device *dev;
3678 dev = p2p_get_device(p2p, addr);
3679 if (dev &&
Dmitry Shmidt34c12022015-03-05 14:11:20 -08003680 dev->status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE)
Dmitry Shmidt4b060592013-04-29 16:42:49 -07003681 dev->flags |= P2P_DEV_PEER_WAITING_RESPONSE;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003682 }
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08003683
3684 if (p2p->state == P2P_SEARCH || p2p->state == P2P_SD_DURING_FIND)
3685 p2p_continue_find(p2p);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003686}
3687
3688
3689static void p2p_go_neg_conf_cb(struct p2p_data *p2p,
3690 enum p2p_send_action_result result)
3691{
3692 struct p2p_device *dev;
3693
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003694 p2p_dbg(p2p, "GO Negotiation Confirm TX callback: result=%d", result);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003695 if (result == P2P_SEND_ACTION_FAILED) {
Dmitry Shmidt413dde72014-04-11 10:23:22 -07003696 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003697 p2p_go_neg_failed(p2p, -1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003698 return;
3699 }
Dmitry Shmidt413dde72014-04-11 10:23:22 -07003700
3701 dev = p2p->go_neg_peer;
3702
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003703 if (result == P2P_SEND_ACTION_NO_ACK) {
3704 /*
Dmitry Shmidt413dde72014-04-11 10:23:22 -07003705 * Retry GO Negotiation Confirmation
3706 * P2P_GO_NEG_CNF_MAX_RETRY_COUNT times if we did not receive
3707 * ACK for confirmation.
3708 */
3709 if (dev && dev->go_neg_conf &&
3710 dev->go_neg_conf_sent <= P2P_GO_NEG_CNF_MAX_RETRY_COUNT) {
3711 p2p_dbg(p2p, "GO Negotiation Confirm retry %d",
3712 dev->go_neg_conf_sent);
3713 p2p->pending_action_state = P2P_PENDING_GO_NEG_CONFIRM;
3714 if (p2p_send_action(p2p, dev->go_neg_conf_freq,
3715 dev->info.p2p_device_addr,
3716 p2p->cfg->dev_addr,
3717 dev->info.p2p_device_addr,
3718 wpabuf_head(dev->go_neg_conf),
3719 wpabuf_len(dev->go_neg_conf), 0) >=
3720 0) {
3721 dev->go_neg_conf_sent++;
3722 return;
3723 }
3724 p2p_dbg(p2p, "Failed to re-send Action frame");
3725
3726 /*
3727 * Continue with the assumption that the first attempt
3728 * went through and just the ACK frame was lost.
3729 */
3730 }
3731
3732 /*
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003733 * It looks like the TX status for GO Negotiation Confirm is
3734 * often showing failure even when the peer has actually
3735 * received the frame. Since the peer may change channels
3736 * immediately after having received the frame, we may not see
3737 * an Ack for retries, so just dropping a single frame may
3738 * trigger this. To allow the group formation to succeed if the
3739 * peer did indeed receive the frame, continue regardless of
3740 * the TX status.
3741 */
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003742 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 -07003743 }
3744
Dmitry Shmidt413dde72014-04-11 10:23:22 -07003745 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
3746
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003747 if (dev == NULL)
3748 return;
3749
3750 p2p_go_complete(p2p, dev);
3751}
3752
3753
3754void p2p_send_action_cb(struct p2p_data *p2p, unsigned int freq, const u8 *dst,
3755 const u8 *src, const u8 *bssid,
3756 enum p2p_send_action_result result)
3757{
3758 enum p2p_pending_action_state state;
3759 int success;
3760
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003761 p2p_dbg(p2p, "Action frame TX callback (state=%d freq=%u dst=" MACSTR
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003762 " src=" MACSTR " bssid=" MACSTR " result=%d p2p_state=%s)",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003763 p2p->pending_action_state, freq, MAC2STR(dst), MAC2STR(src),
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003764 MAC2STR(bssid), result, p2p_state_txt(p2p->state));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003765 success = result == P2P_SEND_ACTION_SUCCESS;
3766 state = p2p->pending_action_state;
3767 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
3768 switch (state) {
3769 case P2P_NO_PENDING_ACTION:
Dmitry Shmidt4582d2a2014-02-28 11:14:23 -08003770 if (p2p->send_action_in_progress) {
3771 p2p->send_action_in_progress = 0;
3772 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
3773 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003774 break;
3775 case P2P_PENDING_GO_NEG_REQUEST:
3776 p2p_go_neg_req_cb(p2p, success);
3777 break;
3778 case P2P_PENDING_GO_NEG_RESPONSE:
3779 p2p_go_neg_resp_cb(p2p, success);
3780 break;
3781 case P2P_PENDING_GO_NEG_RESPONSE_FAILURE:
Dmitry Shmidt4b060592013-04-29 16:42:49 -07003782 p2p_go_neg_resp_failure_cb(p2p, success, dst);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003783 break;
3784 case P2P_PENDING_GO_NEG_CONFIRM:
3785 p2p_go_neg_conf_cb(p2p, result);
3786 break;
3787 case P2P_PENDING_SD:
3788 p2p_sd_cb(p2p, success);
3789 break;
3790 case P2P_PENDING_PD:
3791 p2p_prov_disc_cb(p2p, success);
3792 break;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003793 case P2P_PENDING_PD_RESPONSE:
3794 p2p_prov_disc_resp_cb(p2p, success);
3795 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003796 case P2P_PENDING_INVITATION_REQUEST:
3797 p2p_invitation_req_cb(p2p, success);
3798 break;
3799 case P2P_PENDING_INVITATION_RESPONSE:
3800 p2p_invitation_resp_cb(p2p, success);
3801 break;
3802 case P2P_PENDING_DEV_DISC_REQUEST:
3803 p2p_dev_disc_req_cb(p2p, success);
3804 break;
3805 case P2P_PENDING_DEV_DISC_RESPONSE:
3806 p2p_dev_disc_resp_cb(p2p, success);
3807 break;
3808 case P2P_PENDING_GO_DISC_REQ:
3809 p2p_go_disc_req_cb(p2p, success);
3810 break;
3811 }
3812}
3813
3814
3815void p2p_listen_cb(struct p2p_data *p2p, unsigned int freq,
3816 unsigned int duration)
3817{
3818 if (freq == p2p->pending_client_disc_freq) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003819 p2p_dbg(p2p, "Client discoverability remain-awake completed");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003820 p2p->pending_client_disc_freq = 0;
3821 return;
3822 }
3823
3824 if (freq != p2p->pending_listen_freq) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003825 p2p_dbg(p2p, "Unexpected listen callback for freq=%u duration=%u (pending_listen_freq=%u)",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003826 freq, duration, p2p->pending_listen_freq);
3827 return;
3828 }
3829
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003830 p2p_dbg(p2p, "Starting Listen timeout(%u,%u) on freq=%u based on callback",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003831 p2p->pending_listen_sec, p2p->pending_listen_usec,
3832 p2p->pending_listen_freq);
3833 p2p->in_listen = 1;
3834 p2p->drv_in_listen = freq;
3835 if (p2p->pending_listen_sec || p2p->pending_listen_usec) {
3836 /*
3837 * Add 20 msec extra wait to avoid race condition with driver
3838 * remain-on-channel end event, i.e., give driver more time to
3839 * complete the operation before our timeout expires.
3840 */
3841 p2p_set_timeout(p2p, p2p->pending_listen_sec,
3842 p2p->pending_listen_usec + 20000);
3843 }
3844
3845 p2p->pending_listen_freq = 0;
3846}
3847
3848
3849int p2p_listen_end(struct p2p_data *p2p, unsigned int freq)
3850{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003851 p2p_dbg(p2p, "Driver ended Listen state (freq=%u)", freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003852 p2p->drv_in_listen = 0;
3853 if (p2p->in_listen)
3854 return 0; /* Internal timeout will trigger the next step */
3855
Roshan Pius3a1667e2018-07-03 15:17:14 -07003856 if (p2p->state == P2P_WAIT_PEER_CONNECT && p2p->go_neg_peer &&
3857 p2p->pending_listen_freq) {
3858 /*
3859 * Better wait a bit if the driver is unable to start
3860 * offchannel operation for some reason to continue with
3861 * P2P_WAIT_PEER_(IDLE/CONNECT) state transitions.
3862 */
3863 p2p_dbg(p2p,
3864 "Listen operation did not seem to start - delay idle phase to avoid busy loop");
3865 p2p_set_timeout(p2p, 0, 100000);
3866 return 1;
3867 }
3868
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003869 if (p2p->state == P2P_CONNECT_LISTEN && p2p->go_neg_peer) {
3870 if (p2p->go_neg_peer->connect_reqs >= 120) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003871 p2p_dbg(p2p, "Timeout on sending GO Negotiation Request without getting response");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003872 p2p_go_neg_failed(p2p, -1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003873 return 0;
3874 }
3875
3876 p2p_set_state(p2p, P2P_CONNECT);
3877 p2p_connect_send(p2p, p2p->go_neg_peer);
3878 return 1;
3879 } else if (p2p->state == P2P_SEARCH) {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003880 if (p2p->p2p_scan_running) {
3881 /*
3882 * Search is already in progress. This can happen if
3883 * an Action frame RX is reported immediately after
3884 * the end of a remain-on-channel operation and the
3885 * response frame to that is sent using an offchannel
3886 * operation while in p2p_find. Avoid an attempt to
3887 * restart a scan here.
3888 */
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003889 p2p_dbg(p2p, "p2p_scan already in progress - do not try to start a new one");
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003890 return 1;
3891 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07003892 if (p2p->pending_listen_freq) {
3893 /*
3894 * Better wait a bit if the driver is unable to start
3895 * offchannel operation for some reason. p2p_search()
3896 * will be started from internal timeout.
3897 */
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003898 p2p_dbg(p2p, "Listen operation did not seem to start - delay search phase to avoid busy loop");
Dmitry Shmidt04949592012-07-19 12:16:46 -07003899 p2p_set_timeout(p2p, 0, 100000);
3900 return 1;
3901 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003902 if (p2p->search_delay) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003903 p2p_dbg(p2p, "Delay search operation by %u ms",
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003904 p2p->search_delay);
3905 p2p_set_timeout(p2p, p2p->search_delay / 1000,
3906 (p2p->search_delay % 1000) * 1000);
3907 return 1;
3908 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003909 p2p_search(p2p);
3910 return 1;
3911 }
3912
3913 return 0;
3914}
3915
3916
3917static void p2p_timeout_connect(struct p2p_data *p2p)
3918{
3919 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003920 if (p2p->go_neg_peer &&
3921 (p2p->go_neg_peer->flags & P2P_DEV_WAIT_GO_NEG_CONFIRM)) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003922 p2p_dbg(p2p, "Wait for GO Negotiation Confirm timed out - assume GO Negotiation failed");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003923 p2p_go_neg_failed(p2p, -1);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003924 return;
3925 }
Dmitry Shmidt8c652892013-03-01 10:14:01 -08003926 if (p2p->go_neg_peer &&
3927 (p2p->go_neg_peer->flags & P2P_DEV_PEER_WAITING_RESPONSE) &&
3928 p2p->go_neg_peer->connect_reqs < 120) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003929 p2p_dbg(p2p, "Peer expected to wait our response - skip listen");
Dmitry Shmidt8c652892013-03-01 10:14:01 -08003930 p2p_connect_send(p2p, p2p->go_neg_peer);
3931 return;
3932 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003933 if (p2p->go_neg_peer && p2p->go_neg_peer->oob_go_neg_freq > 0) {
3934 p2p_dbg(p2p, "Skip connect-listen since GO Neg channel known (OOB)");
3935 p2p_set_state(p2p, P2P_CONNECT_LISTEN);
3936 p2p_set_timeout(p2p, 0, 30000);
3937 return;
3938 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003939 p2p_set_state(p2p, P2P_CONNECT_LISTEN);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003940 p2p_listen_in_find(p2p, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003941}
3942
3943
3944static void p2p_timeout_connect_listen(struct p2p_data *p2p)
3945{
3946 if (p2p->go_neg_peer) {
3947 if (p2p->drv_in_listen) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003948 p2p_dbg(p2p, "Driver is still in Listen state; wait for it to complete");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003949 return;
3950 }
3951
3952 if (p2p->go_neg_peer->connect_reqs >= 120) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003953 p2p_dbg(p2p, "Timeout on sending GO Negotiation Request without getting response");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003954 p2p_go_neg_failed(p2p, -1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003955 return;
3956 }
3957
3958 p2p_set_state(p2p, P2P_CONNECT);
3959 p2p_connect_send(p2p, p2p->go_neg_peer);
3960 } else
3961 p2p_set_state(p2p, P2P_IDLE);
3962}
3963
3964
3965static void p2p_timeout_wait_peer_connect(struct p2p_data *p2p)
3966{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003967 p2p_set_state(p2p, P2P_WAIT_PEER_IDLE);
Dmitry Shmidt18463232014-01-24 12:29:41 -08003968
3969 if (p2p->cfg->is_concurrent_session_active &&
3970 p2p->cfg->is_concurrent_session_active(p2p->cfg->cb_ctx))
3971 p2p_set_timeout(p2p, 0, 500000);
3972 else
3973 p2p_set_timeout(p2p, 0, 200000);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003974}
3975
3976
3977static void p2p_timeout_wait_peer_idle(struct p2p_data *p2p)
3978{
3979 struct p2p_device *dev = p2p->go_neg_peer;
3980
3981 if (dev == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003982 p2p_dbg(p2p, "Unknown GO Neg peer - stop GO Neg wait");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003983 return;
3984 }
3985
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003986 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 -07003987 p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003988 p2p_set_state(p2p, P2P_WAIT_PEER_CONNECT);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003989 p2p_listen_in_find(p2p, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003990}
3991
3992
3993static void p2p_timeout_sd_during_find(struct p2p_data *p2p)
3994{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003995 p2p_dbg(p2p, "Service Discovery Query timeout");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003996 if (p2p->sd_peer) {
3997 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003998 p2p->sd_peer = NULL;
3999 }
4000 p2p_continue_find(p2p);
4001}
4002
4003
4004static void p2p_timeout_prov_disc_during_find(struct p2p_data *p2p)
4005{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004006 p2p_dbg(p2p, "Provision Discovery Request timeout");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004007 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
4008 p2p_continue_find(p2p);
4009}
4010
4011
Jouni Malinen75ecf522011-06-27 15:19:46 -07004012static void p2p_timeout_prov_disc_req(struct p2p_data *p2p)
4013{
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004014 u32 adv_id = 0;
4015 u8 *adv_mac = NULL;
4016
Jouni Malinen75ecf522011-06-27 15:19:46 -07004017 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
4018
4019 /*
4020 * For user initiated PD requests that we have not gotten any responses
4021 * for while in IDLE state, we retry them a couple of times before
4022 * giving up.
4023 */
4024 if (!p2p->user_initiated_pd)
4025 return;
4026
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004027 p2p_dbg(p2p, "User initiated Provision Discovery Request timeout");
Jouni Malinen75ecf522011-06-27 15:19:46 -07004028
4029 if (p2p->pd_retries) {
4030 p2p->pd_retries--;
4031 p2p_retry_pd(p2p);
4032 } else {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004033 struct p2p_device *dev;
4034 int for_join = 0;
4035
4036 dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
4037 if (os_memcmp(p2p->pending_pd_devaddr,
4038 dev->info.p2p_device_addr, ETH_ALEN) != 0)
4039 continue;
4040 if (dev->req_config_methods &&
4041 (dev->flags & P2P_DEV_PD_FOR_JOIN))
4042 for_join = 1;
4043 }
4044
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004045 if (p2p->p2ps_prov) {
4046 adv_id = p2p->p2ps_prov->adv_id;
4047 adv_mac = p2p->p2ps_prov->adv_mac;
4048 }
4049
Jouni Malinen75ecf522011-06-27 15:19:46 -07004050 if (p2p->cfg->prov_disc_fail)
4051 p2p->cfg->prov_disc_fail(p2p->cfg->cb_ctx,
4052 p2p->pending_pd_devaddr,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004053 for_join ?
4054 P2P_PROV_DISC_TIMEOUT_JOIN :
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004055 P2P_PROV_DISC_TIMEOUT,
4056 adv_id, adv_mac, NULL);
Jouni Malinen75ecf522011-06-27 15:19:46 -07004057 p2p_reset_pending_pd(p2p);
4058 }
4059}
4060
4061
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004062static void p2p_timeout_invite(struct p2p_data *p2p)
4063{
4064 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
4065 p2p_set_state(p2p, P2P_INVITE_LISTEN);
4066 if (p2p->inv_role == P2P_INVITE_ROLE_ACTIVE_GO) {
4067 /*
4068 * Better remain on operating channel instead of listen channel
4069 * when running a group.
4070 */
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004071 p2p_dbg(p2p, "Inviting in active GO role - wait on operating channel");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004072 p2p_set_timeout(p2p, 0, 100000);
4073 return;
4074 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004075 p2p_listen_in_find(p2p, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004076}
4077
4078
4079static void p2p_timeout_invite_listen(struct p2p_data *p2p)
4080{
4081 if (p2p->invite_peer && p2p->invite_peer->invitation_reqs < 100) {
4082 p2p_set_state(p2p, P2P_INVITE);
4083 p2p_invite_send(p2p, p2p->invite_peer,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004084 p2p->invite_go_dev_addr, p2p->invite_dev_pw_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004085 } else {
4086 if (p2p->invite_peer) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004087 p2p_dbg(p2p, "Invitation Request retry limit reached");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004088 if (p2p->cfg->invitation_result)
4089 p2p->cfg->invitation_result(
Dmitry Shmidt700a1372013-03-15 14:14:44 -07004090 p2p->cfg->cb_ctx, -1, NULL, NULL,
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004091 p2p->invite_peer->info.p2p_device_addr,
Dmitry Shmidt15907092014-03-25 10:42:57 -07004092 0, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004093 }
4094 p2p_set_state(p2p, P2P_IDLE);
4095 }
4096}
4097
4098
4099static void p2p_state_timeout(void *eloop_ctx, void *timeout_ctx)
4100{
4101 struct p2p_data *p2p = eloop_ctx;
4102
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004103 p2p_dbg(p2p, "Timeout (state=%s)", p2p_state_txt(p2p->state));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004104
4105 p2p->in_listen = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004106 if (p2p->drv_in_listen) {
4107 p2p_dbg(p2p, "Driver is still in listen state - stop it");
4108 p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
4109 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004110
4111 switch (p2p->state) {
4112 case P2P_IDLE:
Jouni Malinen75ecf522011-06-27 15:19:46 -07004113 /* Check if we timed out waiting for PD req */
4114 if (p2p->pending_action_state == P2P_PENDING_PD)
4115 p2p_timeout_prov_disc_req(p2p);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004116 break;
4117 case P2P_SEARCH:
Jouni Malinen75ecf522011-06-27 15:19:46 -07004118 /* Check if we timed out waiting for PD req */
4119 if (p2p->pending_action_state == P2P_PENDING_PD)
4120 p2p_timeout_prov_disc_req(p2p);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004121 if (p2p->search_delay && !p2p->in_search_delay) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004122 p2p_dbg(p2p, "Delay search operation by %u ms",
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004123 p2p->search_delay);
4124 p2p->in_search_delay = 1;
4125 p2p_set_timeout(p2p, p2p->search_delay / 1000,
4126 (p2p->search_delay % 1000) * 1000);
4127 break;
4128 }
4129 p2p->in_search_delay = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004130 p2p_search(p2p);
4131 break;
4132 case P2P_CONNECT:
4133 p2p_timeout_connect(p2p);
4134 break;
4135 case P2P_CONNECT_LISTEN:
4136 p2p_timeout_connect_listen(p2p);
4137 break;
4138 case P2P_GO_NEG:
4139 break;
4140 case P2P_LISTEN_ONLY:
Jouni Malinen75ecf522011-06-27 15:19:46 -07004141 /* Check if we timed out waiting for PD req */
4142 if (p2p->pending_action_state == P2P_PENDING_PD)
4143 p2p_timeout_prov_disc_req(p2p);
4144
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004145 if (p2p->ext_listen_only) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004146 p2p_dbg(p2p, "Extended Listen Timing - Listen State completed");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004147 p2p->ext_listen_only = 0;
4148 p2p_set_state(p2p, P2P_IDLE);
4149 }
4150 break;
4151 case P2P_WAIT_PEER_CONNECT:
4152 p2p_timeout_wait_peer_connect(p2p);
4153 break;
4154 case P2P_WAIT_PEER_IDLE:
4155 p2p_timeout_wait_peer_idle(p2p);
4156 break;
4157 case P2P_SD_DURING_FIND:
4158 p2p_timeout_sd_during_find(p2p);
4159 break;
4160 case P2P_PROVISIONING:
4161 break;
4162 case P2P_PD_DURING_FIND:
4163 p2p_timeout_prov_disc_during_find(p2p);
4164 break;
4165 case P2P_INVITE:
4166 p2p_timeout_invite(p2p);
4167 break;
4168 case P2P_INVITE_LISTEN:
4169 p2p_timeout_invite_listen(p2p);
4170 break;
4171 }
4172}
4173
4174
4175int p2p_reject(struct p2p_data *p2p, const u8 *peer_addr)
4176{
4177 struct p2p_device *dev;
4178
4179 dev = p2p_get_device(p2p, peer_addr);
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004180 p2p_dbg(p2p, "Local request to reject connection attempts by peer "
4181 MACSTR, MAC2STR(peer_addr));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004182 if (dev == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004183 p2p_dbg(p2p, "Peer " MACSTR " unknown", MAC2STR(peer_addr));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004184 return -1;
4185 }
4186 dev->status = P2P_SC_FAIL_REJECTED_BY_USER;
4187 dev->flags |= P2P_DEV_USER_REJECTED;
4188 return 0;
4189}
4190
4191
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004192const char * p2p_wps_method_text(enum p2p_wps_method method)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004193{
4194 switch (method) {
4195 case WPS_NOT_READY:
4196 return "not-ready";
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004197 case WPS_PIN_DISPLAY:
4198 return "Display";
4199 case WPS_PIN_KEYPAD:
4200 return "Keypad";
4201 case WPS_PBC:
4202 return "PBC";
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004203 case WPS_NFC:
4204 return "NFC";
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004205 case WPS_P2PS:
4206 return "P2PS";
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004207 }
4208
4209 return "??";
4210}
4211
4212
4213static const char * p2p_go_state_text(enum p2p_go_state go_state)
4214{
4215 switch (go_state) {
4216 case UNKNOWN_GO:
4217 return "unknown";
4218 case LOCAL_GO:
4219 return "local";
4220 case REMOTE_GO:
4221 return "remote";
4222 }
4223
4224 return "??";
4225}
4226
4227
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004228const struct p2p_peer_info * p2p_get_peer_info(struct p2p_data *p2p,
4229 const u8 *addr, int next)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004230{
4231 struct p2p_device *dev;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004232
4233 if (addr)
4234 dev = p2p_get_device(p2p, addr);
4235 else
4236 dev = dl_list_first(&p2p->devices, struct p2p_device, list);
4237
4238 if (dev && next) {
4239 dev = dl_list_first(&dev->list, struct p2p_device, list);
4240 if (&dev->list == &p2p->devices)
4241 dev = NULL;
4242 }
4243
4244 if (dev == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004245 return NULL;
4246
4247 return &dev->info;
4248}
4249
4250
4251int p2p_get_peer_info_txt(const struct p2p_peer_info *info,
4252 char *buf, size_t buflen)
4253{
4254 struct p2p_device *dev;
4255 int res;
4256 char *pos, *end;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004257 struct os_reltime now;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004258
4259 if (info == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004260 return -1;
4261
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004262 dev = (struct p2p_device *) (((u8 *) info) -
4263 offsetof(struct p2p_device, info));
4264
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004265 pos = buf;
4266 end = buf + buflen;
4267
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004268 os_get_reltime(&now);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004269 res = os_snprintf(pos, end - pos,
4270 "age=%d\n"
4271 "listen_freq=%d\n"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004272 "wps_method=%s\n"
4273 "interface_addr=" MACSTR "\n"
4274 "member_in_go_dev=" MACSTR "\n"
4275 "member_in_go_iface=" MACSTR "\n"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004276 "go_neg_req_sent=%d\n"
4277 "go_state=%s\n"
4278 "dialog_token=%u\n"
4279 "intended_addr=" MACSTR "\n"
4280 "country=%c%c\n"
4281 "oper_freq=%d\n"
4282 "req_config_methods=0x%x\n"
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07004283 "flags=%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004284 "status=%d\n"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004285 "invitation_reqs=%u\n",
4286 (int) (now.sec - dev->last_seen.sec),
4287 dev->listen_freq,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004288 p2p_wps_method_text(dev->wps_method),
4289 MAC2STR(dev->interface_addr),
4290 MAC2STR(dev->member_in_go_dev),
4291 MAC2STR(dev->member_in_go_iface),
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004292 dev->go_neg_req_sent,
4293 p2p_go_state_text(dev->go_state),
4294 dev->dialog_token,
4295 MAC2STR(dev->intended_addr),
4296 dev->country[0] ? dev->country[0] : '_',
4297 dev->country[1] ? dev->country[1] : '_',
4298 dev->oper_freq,
4299 dev->req_config_methods,
4300 dev->flags & P2P_DEV_PROBE_REQ_ONLY ?
4301 "[PROBE_REQ_ONLY]" : "",
4302 dev->flags & P2P_DEV_REPORTED ? "[REPORTED]" : "",
4303 dev->flags & P2P_DEV_NOT_YET_READY ?
4304 "[NOT_YET_READY]" : "",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004305 dev->flags & P2P_DEV_PD_PEER_DISPLAY ?
4306 "[PD_PEER_DISPLAY]" : "",
4307 dev->flags & P2P_DEV_PD_PEER_KEYPAD ?
4308 "[PD_PEER_KEYPAD]" : "",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004309 dev->flags & P2P_DEV_PD_PEER_P2PS ?
4310 "[PD_PEER_P2PS]" : "",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004311 dev->flags & P2P_DEV_USER_REJECTED ?
4312 "[USER_REJECTED]" : "",
4313 dev->flags & P2P_DEV_PEER_WAITING_RESPONSE ?
4314 "[PEER_WAITING_RESPONSE]" : "",
4315 dev->flags & P2P_DEV_PREFER_PERSISTENT_GROUP ?
4316 "[PREFER_PERSISTENT_GROUP]" : "",
4317 dev->flags & P2P_DEV_WAIT_GO_NEG_RESPONSE ?
4318 "[WAIT_GO_NEG_RESPONSE]" : "",
4319 dev->flags & P2P_DEV_WAIT_GO_NEG_CONFIRM ?
4320 "[WAIT_GO_NEG_CONFIRM]" : "",
4321 dev->flags & P2P_DEV_GROUP_CLIENT_ONLY ?
4322 "[GROUP_CLIENT_ONLY]" : "",
4323 dev->flags & P2P_DEV_FORCE_FREQ ?
4324 "[FORCE_FREQ]" : "",
4325 dev->flags & P2P_DEV_PD_FOR_JOIN ?
4326 "[PD_FOR_JOIN]" : "",
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07004327 dev->flags & P2P_DEV_LAST_SEEN_AS_GROUP_CLIENT ?
4328 "[LAST_SEEN_AS_GROUP_CLIENT]" : "",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004329 dev->status,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004330 dev->invitation_reqs);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004331 if (os_snprintf_error(end - pos, res))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004332 return pos - buf;
4333 pos += res;
4334
4335 if (dev->ext_listen_period) {
4336 res = os_snprintf(pos, end - pos,
4337 "ext_listen_period=%u\n"
4338 "ext_listen_interval=%u\n",
4339 dev->ext_listen_period,
4340 dev->ext_listen_interval);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004341 if (os_snprintf_error(end - pos, res))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004342 return pos - buf;
4343 pos += res;
4344 }
4345
4346 if (dev->oper_ssid_len) {
4347 res = os_snprintf(pos, end - pos,
4348 "oper_ssid=%s\n",
4349 wpa_ssid_txt(dev->oper_ssid,
4350 dev->oper_ssid_len));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004351 if (os_snprintf_error(end - pos, res))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004352 return pos - buf;
4353 pos += res;
4354 }
4355
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004356#ifdef CONFIG_WIFI_DISPLAY
4357 if (dev->info.wfd_subelems) {
4358 res = os_snprintf(pos, end - pos, "wfd_subelems=");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004359 if (os_snprintf_error(end - pos, res))
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004360 return pos - buf;
4361 pos += res;
4362
4363 pos += wpa_snprintf_hex(pos, end - pos,
4364 wpabuf_head(dev->info.wfd_subelems),
4365 wpabuf_len(dev->info.wfd_subelems));
4366
4367 res = os_snprintf(pos, end - pos, "\n");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004368 if (os_snprintf_error(end - pos, res))
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004369 return pos - buf;
4370 pos += res;
4371 }
4372#endif /* CONFIG_WIFI_DISPLAY */
4373
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004374 return pos - buf;
4375}
4376
4377
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004378int p2p_peer_known(struct p2p_data *p2p, const u8 *addr)
4379{
4380 return p2p_get_device(p2p, addr) != NULL;
4381}
4382
4383
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004384void p2p_set_client_discoverability(struct p2p_data *p2p, int enabled)
4385{
4386 if (enabled) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004387 p2p_dbg(p2p, "Client discoverability enabled");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004388 p2p->dev_capab |= P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
4389 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004390 p2p_dbg(p2p, "Client discoverability disabled");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004391 p2p->dev_capab &= ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
4392 }
4393}
4394
4395
4396static struct wpabuf * p2p_build_presence_req(u32 duration1, u32 interval1,
4397 u32 duration2, u32 interval2)
4398{
4399 struct wpabuf *req;
4400 struct p2p_noa_desc desc1, desc2, *ptr1 = NULL, *ptr2 = NULL;
4401 u8 *len;
4402
4403 req = wpabuf_alloc(100);
4404 if (req == NULL)
4405 return NULL;
4406
4407 if (duration1 || interval1) {
4408 os_memset(&desc1, 0, sizeof(desc1));
4409 desc1.count_type = 1;
4410 desc1.duration = duration1;
4411 desc1.interval = interval1;
4412 ptr1 = &desc1;
4413
4414 if (duration2 || interval2) {
4415 os_memset(&desc2, 0, sizeof(desc2));
4416 desc2.count_type = 2;
4417 desc2.duration = duration2;
4418 desc2.interval = interval2;
4419 ptr2 = &desc2;
4420 }
4421 }
4422
4423 p2p_buf_add_action_hdr(req, P2P_PRESENCE_REQ, 1);
4424 len = p2p_buf_add_ie_hdr(req);
4425 p2p_buf_add_noa(req, 0, 0, 0, ptr1, ptr2);
4426 p2p_buf_update_ie_hdr(req, len);
4427
4428 return req;
4429}
4430
4431
4432int p2p_presence_req(struct p2p_data *p2p, const u8 *go_interface_addr,
4433 const u8 *own_interface_addr, unsigned int freq,
4434 u32 duration1, u32 interval1, u32 duration2,
4435 u32 interval2)
4436{
4437 struct wpabuf *req;
4438
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004439 p2p_dbg(p2p, "Send Presence Request to GO " MACSTR
4440 " (own interface " MACSTR ") freq=%u dur1=%u int1=%u "
4441 "dur2=%u int2=%u",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004442 MAC2STR(go_interface_addr), MAC2STR(own_interface_addr),
4443 freq, duration1, interval1, duration2, interval2);
4444
4445 req = p2p_build_presence_req(duration1, interval1, duration2,
4446 interval2);
4447 if (req == NULL)
4448 return -1;
4449
4450 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
4451 if (p2p_send_action(p2p, freq, go_interface_addr, own_interface_addr,
4452 go_interface_addr,
4453 wpabuf_head(req), wpabuf_len(req), 200) < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004454 p2p_dbg(p2p, "Failed to send Action frame");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004455 }
4456 wpabuf_free(req);
4457
4458 return 0;
4459}
4460
4461
4462static struct wpabuf * p2p_build_presence_resp(u8 status, const u8 *noa,
4463 size_t noa_len, u8 dialog_token)
4464{
4465 struct wpabuf *resp;
4466 u8 *len;
4467
4468 resp = wpabuf_alloc(100 + noa_len);
4469 if (resp == NULL)
4470 return NULL;
4471
4472 p2p_buf_add_action_hdr(resp, P2P_PRESENCE_RESP, dialog_token);
4473 len = p2p_buf_add_ie_hdr(resp);
4474 p2p_buf_add_status(resp, status);
4475 if (noa) {
4476 wpabuf_put_u8(resp, P2P_ATTR_NOTICE_OF_ABSENCE);
4477 wpabuf_put_le16(resp, noa_len);
4478 wpabuf_put_data(resp, noa, noa_len);
4479 } else
4480 p2p_buf_add_noa(resp, 0, 0, 0, NULL, NULL);
4481 p2p_buf_update_ie_hdr(resp, len);
4482
4483 return resp;
4484}
4485
4486
4487static void p2p_process_presence_req(struct p2p_data *p2p, const u8 *da,
4488 const u8 *sa, const u8 *data, size_t len,
4489 int rx_freq)
4490{
4491 struct p2p_message msg;
4492 u8 status;
4493 struct wpabuf *resp;
4494 size_t g;
4495 struct p2p_group *group = NULL;
4496 int parsed = 0;
4497 u8 noa[50];
4498 int noa_len;
4499
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004500 p2p_dbg(p2p, "Received P2P Action - P2P Presence Request");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004501
4502 for (g = 0; g < p2p->num_groups; g++) {
4503 if (os_memcmp(da, p2p_group_get_interface_addr(p2p->groups[g]),
4504 ETH_ALEN) == 0) {
4505 group = p2p->groups[g];
4506 break;
4507 }
4508 }
4509 if (group == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004510 p2p_dbg(p2p, "Ignore P2P Presence Request for unknown group "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004511 MACSTR, MAC2STR(da));
4512 return;
4513 }
4514
4515 if (p2p_parse(data, len, &msg) < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004516 p2p_dbg(p2p, "Failed to parse P2P Presence Request");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004517 status = P2P_SC_FAIL_INVALID_PARAMS;
4518 goto fail;
4519 }
4520 parsed = 1;
4521
4522 if (msg.noa == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004523 p2p_dbg(p2p, "No NoA attribute in P2P Presence Request");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004524 status = P2P_SC_FAIL_INVALID_PARAMS;
4525 goto fail;
4526 }
4527
4528 status = p2p_group_presence_req(group, sa, msg.noa, msg.noa_len);
4529
4530fail:
4531 if (p2p->cfg->get_noa)
4532 noa_len = p2p->cfg->get_noa(p2p->cfg->cb_ctx, da, noa,
4533 sizeof(noa));
4534 else
4535 noa_len = -1;
4536 resp = p2p_build_presence_resp(status, noa_len > 0 ? noa : NULL,
4537 noa_len > 0 ? noa_len : 0,
4538 msg.dialog_token);
4539 if (parsed)
4540 p2p_parse_free(&msg);
4541 if (resp == NULL)
4542 return;
4543
4544 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
4545 if (p2p_send_action(p2p, rx_freq, sa, da, da,
4546 wpabuf_head(resp), wpabuf_len(resp), 200) < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004547 p2p_dbg(p2p, "Failed to send Action frame");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004548 }
4549 wpabuf_free(resp);
4550}
4551
4552
4553static void p2p_process_presence_resp(struct p2p_data *p2p, const u8 *da,
4554 const u8 *sa, const u8 *data, size_t len)
4555{
4556 struct p2p_message msg;
4557
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004558 p2p_dbg(p2p, "Received P2P Action - P2P Presence Response");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004559
4560 if (p2p_parse(data, len, &msg) < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004561 p2p_dbg(p2p, "Failed to parse P2P Presence Response");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004562 return;
4563 }
4564
4565 if (msg.status == NULL || msg.noa == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004566 p2p_dbg(p2p, "No Status or NoA attribute in P2P Presence Response");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004567 p2p_parse_free(&msg);
4568 return;
4569 }
4570
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004571 if (p2p->cfg->presence_resp) {
4572 p2p->cfg->presence_resp(p2p->cfg->cb_ctx, sa, *msg.status,
4573 msg.noa, msg.noa_len);
4574 }
4575
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004576 if (*msg.status) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004577 p2p_dbg(p2p, "P2P Presence Request was rejected: status %u",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004578 *msg.status);
4579 p2p_parse_free(&msg);
4580 return;
4581 }
4582
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004583 p2p_dbg(p2p, "P2P Presence Request was accepted");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004584 wpa_hexdump(MSG_DEBUG, "P2P: P2P Presence Response - NoA",
4585 msg.noa, msg.noa_len);
4586 /* TODO: process NoA */
4587 p2p_parse_free(&msg);
4588}
4589
4590
4591static void p2p_ext_listen_timeout(void *eloop_ctx, void *timeout_ctx)
4592{
4593 struct p2p_data *p2p = eloop_ctx;
4594
4595 if (p2p->ext_listen_interval) {
4596 /* Schedule next extended listen timeout */
4597 eloop_register_timeout(p2p->ext_listen_interval_sec,
4598 p2p->ext_listen_interval_usec,
4599 p2p_ext_listen_timeout, p2p, NULL);
4600 }
4601
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -07004602 if ((p2p->cfg->is_p2p_in_progress &&
4603 p2p->cfg->is_p2p_in_progress(p2p->cfg->cb_ctx)) ||
4604 (p2p->pending_action_state == P2P_PENDING_PD &&
4605 p2p->pd_retries > 0)) {
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -07004606 p2p_dbg(p2p, "Operation in progress - skip Extended Listen timeout (%s)",
4607 p2p_state_txt(p2p->state));
4608 return;
4609 }
4610
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004611 if (p2p->state == P2P_LISTEN_ONLY && p2p->ext_listen_only) {
4612 /*
4613 * This should not really happen, but it looks like the Listen
4614 * command may fail is something else (e.g., a scan) was
4615 * running at an inconvenient time. As a workaround, allow new
4616 * Extended Listen operation to be started.
4617 */
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004618 p2p_dbg(p2p, "Previous Extended Listen operation had not been completed - try again");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004619 p2p->ext_listen_only = 0;
4620 p2p_set_state(p2p, P2P_IDLE);
4621 }
4622
4623 if (p2p->state != P2P_IDLE) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004624 p2p_dbg(p2p, "Skip Extended Listen timeout in active state (%s)", p2p_state_txt(p2p->state));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004625 return;
4626 }
4627
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004628 p2p_dbg(p2p, "Extended Listen timeout");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004629 p2p->ext_listen_only = 1;
4630 if (p2p_listen(p2p, p2p->ext_listen_period) < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004631 p2p_dbg(p2p, "Failed to start Listen state for Extended Listen Timing");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004632 p2p->ext_listen_only = 0;
4633 }
4634}
4635
4636
4637int p2p_ext_listen(struct p2p_data *p2p, unsigned int period,
4638 unsigned int interval)
4639{
4640 if (period > 65535 || interval > 65535 || period > interval ||
4641 (period == 0 && interval > 0) || (period > 0 && interval == 0)) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004642 p2p_dbg(p2p, "Invalid Extended Listen Timing request: period=%u interval=%u",
4643 period, interval);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004644 return -1;
4645 }
4646
4647 eloop_cancel_timeout(p2p_ext_listen_timeout, p2p, NULL);
4648
4649 if (interval == 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004650 p2p_dbg(p2p, "Disabling Extended Listen Timing");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004651 p2p->ext_listen_period = 0;
4652 p2p->ext_listen_interval = 0;
4653 return 0;
4654 }
4655
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004656 p2p_dbg(p2p, "Enabling Extended Listen Timing: period %u msec, interval %u msec",
4657 period, interval);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004658 p2p->ext_listen_period = period;
4659 p2p->ext_listen_interval = interval;
4660 p2p->ext_listen_interval_sec = interval / 1000;
4661 p2p->ext_listen_interval_usec = (interval % 1000) * 1000;
4662
4663 eloop_register_timeout(p2p->ext_listen_interval_sec,
4664 p2p->ext_listen_interval_usec,
4665 p2p_ext_listen_timeout, p2p, NULL);
4666
4667 return 0;
4668}
4669
4670
4671void p2p_deauth_notif(struct p2p_data *p2p, const u8 *bssid, u16 reason_code,
4672 const u8 *ie, size_t ie_len)
4673{
4674 struct p2p_message msg;
4675
4676 if (bssid == NULL || ie == NULL)
4677 return;
4678
4679 os_memset(&msg, 0, sizeof(msg));
4680 if (p2p_parse_ies(ie, ie_len, &msg))
4681 return;
Dmitry Shmidt97672262014-02-03 13:02:54 -08004682 if (msg.minor_reason_code == NULL) {
4683 p2p_parse_free(&msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004684 return;
Dmitry Shmidt97672262014-02-03 13:02:54 -08004685 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004686
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004687 p2p_dbg(p2p, "Deauthentication notification BSSID " MACSTR
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004688 " reason_code=%u minor_reason_code=%u",
4689 MAC2STR(bssid), reason_code, *msg.minor_reason_code);
4690
4691 p2p_parse_free(&msg);
4692}
4693
4694
4695void p2p_disassoc_notif(struct p2p_data *p2p, const u8 *bssid, u16 reason_code,
4696 const u8 *ie, size_t ie_len)
4697{
4698 struct p2p_message msg;
4699
4700 if (bssid == NULL || ie == NULL)
4701 return;
4702
4703 os_memset(&msg, 0, sizeof(msg));
4704 if (p2p_parse_ies(ie, ie_len, &msg))
4705 return;
Dmitry Shmidt97672262014-02-03 13:02:54 -08004706 if (msg.minor_reason_code == NULL) {
4707 p2p_parse_free(&msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004708 return;
Dmitry Shmidt97672262014-02-03 13:02:54 -08004709 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004710
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004711 p2p_dbg(p2p, "Disassociation notification BSSID " MACSTR
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004712 " reason_code=%u minor_reason_code=%u",
4713 MAC2STR(bssid), reason_code, *msg.minor_reason_code);
4714
4715 p2p_parse_free(&msg);
4716}
4717
4718
4719void p2p_set_managed_oper(struct p2p_data *p2p, int enabled)
4720{
4721 if (enabled) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004722 p2p_dbg(p2p, "Managed P2P Device operations enabled");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004723 p2p->dev_capab |= P2P_DEV_CAPAB_INFRA_MANAGED;
4724 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004725 p2p_dbg(p2p, "Managed P2P Device operations disabled");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004726 p2p->dev_capab &= ~P2P_DEV_CAPAB_INFRA_MANAGED;
4727 }
4728}
4729
4730
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004731int p2p_config_get_random_social(struct p2p_config *p2p, u8 *op_class,
Hai Shalom74f70d42019-02-11 14:42:39 -08004732 u8 *op_channel,
4733 struct wpa_freq_range_list *avoid_list,
4734 struct wpa_freq_range_list *disallow_list)
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004735{
Hai Shalom74f70d42019-02-11 14:42:39 -08004736 return p2p_channel_random_social(&p2p->channels, op_class, op_channel,
4737 avoid_list, disallow_list);
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004738}
4739
4740
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004741int p2p_set_listen_channel(struct p2p_data *p2p, u8 reg_class, u8 channel,
4742 u8 forced)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004743{
Dmitry Shmidt4b060592013-04-29 16:42:49 -07004744 if (p2p_channel_to_freq(reg_class, channel) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004745 return -1;
4746
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004747 /*
4748 * Listen channel was set in configuration or set by control interface;
4749 * cannot override it.
4750 */
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004751 if (p2p->cfg->channel_forced && forced == 0) {
4752 p2p_dbg(p2p,
4753 "Listen channel was previously configured - do not override based on optimization");
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004754 return -1;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004755 }
4756
4757 p2p_dbg(p2p, "Set Listen channel: reg_class %u channel %u",
4758 reg_class, channel);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004759
4760 if (p2p->state == P2P_IDLE) {
4761 p2p->cfg->reg_class = reg_class;
4762 p2p->cfg->channel = channel;
4763 p2p->cfg->channel_forced = forced;
4764 } else {
4765 p2p_dbg(p2p, "Defer setting listen channel");
4766 p2p->pending_reg_class = reg_class;
4767 p2p->pending_channel = channel;
4768 p2p->pending_channel_forced = forced;
4769 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004770
4771 return 0;
4772}
4773
4774
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004775u8 p2p_get_listen_channel(struct p2p_data *p2p)
4776{
4777 return p2p->cfg->channel;
4778}
4779
4780
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004781int p2p_set_ssid_postfix(struct p2p_data *p2p, const u8 *postfix, size_t len)
4782{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004783 p2p_dbg(p2p, "New SSID postfix: %s", wpa_ssid_txt(postfix, len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004784 if (postfix == NULL) {
4785 p2p->cfg->ssid_postfix_len = 0;
4786 return 0;
4787 }
4788 if (len > sizeof(p2p->cfg->ssid_postfix))
4789 return -1;
4790 os_memcpy(p2p->cfg->ssid_postfix, postfix, len);
4791 p2p->cfg->ssid_postfix_len = len;
4792 return 0;
4793}
4794
4795
Jouni Malinen75ecf522011-06-27 15:19:46 -07004796int p2p_set_oper_channel(struct p2p_data *p2p, u8 op_reg_class, u8 op_channel,
4797 int cfg_op_channel)
4798{
Dmitry Shmidt4b060592013-04-29 16:42:49 -07004799 if (p2p_channel_to_freq(op_reg_class, op_channel) < 0)
Jouni Malinen75ecf522011-06-27 15:19:46 -07004800 return -1;
4801
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004802 p2p_dbg(p2p, "Set Operating channel: reg_class %u channel %u",
4803 op_reg_class, op_channel);
Jouni Malinen75ecf522011-06-27 15:19:46 -07004804 p2p->cfg->op_reg_class = op_reg_class;
4805 p2p->cfg->op_channel = op_channel;
4806 p2p->cfg->cfg_op_channel = cfg_op_channel;
4807 return 0;
4808}
4809
4810
Dmitry Shmidt04949592012-07-19 12:16:46 -07004811int p2p_set_pref_chan(struct p2p_data *p2p, unsigned int num_pref_chan,
4812 const struct p2p_channel *pref_chan)
4813{
4814 struct p2p_channel *n;
4815
4816 if (pref_chan) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004817 n = os_memdup(pref_chan,
4818 num_pref_chan * sizeof(struct p2p_channel));
Dmitry Shmidt04949592012-07-19 12:16:46 -07004819 if (n == NULL)
4820 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004821 } else
4822 n = NULL;
4823
4824 os_free(p2p->cfg->pref_chan);
4825 p2p->cfg->pref_chan = n;
4826 p2p->cfg->num_pref_chan = num_pref_chan;
4827
4828 return 0;
4829}
4830
4831
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004832int p2p_set_no_go_freq(struct p2p_data *p2p,
4833 const struct wpa_freq_range_list *list)
4834{
4835 struct wpa_freq_range *tmp;
4836
4837 if (list == NULL || list->num == 0) {
4838 os_free(p2p->no_go_freq.range);
4839 p2p->no_go_freq.range = NULL;
4840 p2p->no_go_freq.num = 0;
4841 return 0;
4842 }
4843
4844 tmp = os_calloc(list->num, sizeof(struct wpa_freq_range));
4845 if (tmp == NULL)
4846 return -1;
4847 os_memcpy(tmp, list->range, list->num * sizeof(struct wpa_freq_range));
4848 os_free(p2p->no_go_freq.range);
4849 p2p->no_go_freq.range = tmp;
4850 p2p->no_go_freq.num = list->num;
4851 p2p_dbg(p2p, "Updated no GO chan list");
4852
4853 return 0;
4854}
4855
4856
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004857int p2p_get_interface_addr(struct p2p_data *p2p, const u8 *dev_addr,
4858 u8 *iface_addr)
4859{
4860 struct p2p_device *dev = p2p_get_device(p2p, dev_addr);
4861 if (dev == NULL || is_zero_ether_addr(dev->interface_addr))
4862 return -1;
4863 os_memcpy(iface_addr, dev->interface_addr, ETH_ALEN);
4864 return 0;
4865}
4866
4867
4868int p2p_get_dev_addr(struct p2p_data *p2p, const u8 *iface_addr,
4869 u8 *dev_addr)
4870{
4871 struct p2p_device *dev = p2p_get_device_interface(p2p, iface_addr);
4872 if (dev == NULL)
4873 return -1;
4874 os_memcpy(dev_addr, dev->info.p2p_device_addr, ETH_ALEN);
4875 return 0;
4876}
4877
4878
4879void p2p_set_peer_filter(struct p2p_data *p2p, const u8 *addr)
4880{
4881 os_memcpy(p2p->peer_filter, addr, ETH_ALEN);
4882 if (is_zero_ether_addr(p2p->peer_filter))
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004883 p2p_dbg(p2p, "Disable peer filter");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004884 else
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004885 p2p_dbg(p2p, "Enable peer filter for " MACSTR,
4886 MAC2STR(p2p->peer_filter));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004887}
4888
4889
4890void p2p_set_cross_connect(struct p2p_data *p2p, int enabled)
4891{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004892 p2p_dbg(p2p, "Cross connection %s", enabled ? "enabled" : "disabled");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004893 if (p2p->cross_connect == enabled)
4894 return;
4895 p2p->cross_connect = enabled;
4896 /* TODO: may need to tear down any action group where we are GO(?) */
4897}
4898
4899
4900int p2p_get_oper_freq(struct p2p_data *p2p, const u8 *iface_addr)
4901{
4902 struct p2p_device *dev = p2p_get_device_interface(p2p, iface_addr);
4903 if (dev == NULL)
4904 return -1;
4905 if (dev->oper_freq <= 0)
4906 return -1;
4907 return dev->oper_freq;
4908}
4909
4910
4911void p2p_set_intra_bss_dist(struct p2p_data *p2p, int enabled)
4912{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004913 p2p_dbg(p2p, "Intra BSS distribution %s",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004914 enabled ? "enabled" : "disabled");
4915 p2p->cfg->p2p_intra_bss = enabled;
4916}
4917
4918
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004919void p2p_update_channel_list(struct p2p_data *p2p,
4920 const struct p2p_channels *chan,
4921 const struct p2p_channels *cli_chan)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004922{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004923 p2p_dbg(p2p, "Update channel list");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004924 os_memcpy(&p2p->cfg->channels, chan, sizeof(struct p2p_channels));
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004925 p2p_channels_dump(p2p, "channels", &p2p->cfg->channels);
4926 os_memcpy(&p2p->cfg->cli_channels, cli_chan,
4927 sizeof(struct p2p_channels));
4928 p2p_channels_dump(p2p, "cli_channels", &p2p->cfg->cli_channels);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004929}
4930
4931
4932int p2p_send_action(struct p2p_data *p2p, unsigned int freq, const u8 *dst,
4933 const u8 *src, const u8 *bssid, const u8 *buf,
4934 size_t len, unsigned int wait_time)
4935{
Hai Shalom021b0b52019-04-10 11:17:58 -07004936 int res, scheduled;
4937
Hai Shalom021b0b52019-04-10 11:17:58 -07004938 res = p2p->cfg->send_action(p2p->cfg->cb_ctx, freq, dst, src, bssid,
4939 buf, len, wait_time, &scheduled);
4940 if (res == 0 && scheduled && p2p->in_listen && freq > 0 &&
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08004941 p2p->drv_in_listen > 0 &&
Hai Shalom021b0b52019-04-10 11:17:58 -07004942 (unsigned int) p2p->drv_in_listen != freq) {
4943 p2p_dbg(p2p,
4944 "Stop listen on %d MHz to allow a frame to be sent immediately on %d MHz",
4945 p2p->drv_in_listen, freq);
4946 p2p_stop_listen_for_freq(p2p, freq);
4947 }
4948 return res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004949}
4950
4951
4952void p2p_set_best_channels(struct p2p_data *p2p, int freq_24, int freq_5,
4953 int freq_overall)
4954{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004955 p2p_dbg(p2p, "Best channel: 2.4 GHz: %d, 5 GHz: %d, overall: %d",
4956 freq_24, freq_5, freq_overall);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004957 p2p->best_freq_24 = freq_24;
4958 p2p->best_freq_5 = freq_5;
4959 p2p->best_freq_overall = freq_overall;
4960}
4961
4962
Dmitry Shmidt700a1372013-03-15 14:14:44 -07004963void p2p_set_own_freq_preference(struct p2p_data *p2p, int freq)
4964{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004965 p2p_dbg(p2p, "Own frequency preference: %d MHz", freq);
Dmitry Shmidt700a1372013-03-15 14:14:44 -07004966 p2p->own_freq_preference = freq;
4967}
4968
4969
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004970const u8 * p2p_get_go_neg_peer(struct p2p_data *p2p)
4971{
4972 if (p2p == NULL || p2p->go_neg_peer == NULL)
4973 return NULL;
4974 return p2p->go_neg_peer->info.p2p_device_addr;
4975}
4976
4977
4978const struct p2p_peer_info *
4979p2p_get_peer_found(struct p2p_data *p2p, const u8 *addr, int next)
4980{
4981 struct p2p_device *dev;
4982
4983 if (addr) {
4984 dev = p2p_get_device(p2p, addr);
4985 if (!dev)
4986 return NULL;
4987
4988 if (!next) {
4989 if (dev->flags & P2P_DEV_PROBE_REQ_ONLY)
4990 return NULL;
4991
4992 return &dev->info;
4993 } else {
4994 do {
4995 dev = dl_list_first(&dev->list,
4996 struct p2p_device,
4997 list);
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07004998 if (!dev || &dev->list == &p2p->devices)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004999 return NULL;
5000 } while (dev->flags & P2P_DEV_PROBE_REQ_ONLY);
5001 }
5002 } else {
5003 dev = dl_list_first(&p2p->devices, struct p2p_device, list);
5004 if (!dev)
5005 return NULL;
5006 while (dev->flags & P2P_DEV_PROBE_REQ_ONLY) {
5007 dev = dl_list_first(&dev->list,
5008 struct p2p_device,
5009 list);
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07005010 if (!dev || &dev->list == &p2p->devices)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005011 return NULL;
5012 }
5013 }
5014
5015 return &dev->info;
5016}
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005017
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005018
5019int p2p_in_progress(struct p2p_data *p2p)
5020{
5021 if (p2p == NULL)
5022 return 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005023 if (p2p->state == P2P_SEARCH)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005024 return 2;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005025 return p2p->state != P2P_IDLE && p2p->state != P2P_PROVISIONING;
5026}
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005027
5028
5029void p2p_set_config_timeout(struct p2p_data *p2p, u8 go_timeout,
5030 u8 client_timeout)
5031{
5032 if (p2p) {
5033 p2p->go_timeout = go_timeout;
5034 p2p->client_timeout = client_timeout;
5035 }
5036}
5037
5038
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005039#ifdef CONFIG_WIFI_DISPLAY
5040
5041static void p2p_update_wfd_ie_groups(struct p2p_data *p2p)
5042{
5043 size_t g;
5044 struct p2p_group *group;
5045
5046 for (g = 0; g < p2p->num_groups; g++) {
5047 group = p2p->groups[g];
Dmitry Shmidtb96dad42013-11-05 10:07:29 -08005048 p2p_group_force_beacon_update_ies(group);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005049 }
5050}
5051
5052
5053int p2p_set_wfd_ie_beacon(struct p2p_data *p2p, struct wpabuf *ie)
5054{
5055 wpabuf_free(p2p->wfd_ie_beacon);
5056 p2p->wfd_ie_beacon = ie;
5057 p2p_update_wfd_ie_groups(p2p);
5058 return 0;
5059}
5060
5061
5062int p2p_set_wfd_ie_probe_req(struct p2p_data *p2p, struct wpabuf *ie)
5063{
5064 wpabuf_free(p2p->wfd_ie_probe_req);
5065 p2p->wfd_ie_probe_req = ie;
5066 return 0;
5067}
5068
5069
5070int p2p_set_wfd_ie_probe_resp(struct p2p_data *p2p, struct wpabuf *ie)
5071{
5072 wpabuf_free(p2p->wfd_ie_probe_resp);
5073 p2p->wfd_ie_probe_resp = ie;
5074 p2p_update_wfd_ie_groups(p2p);
5075 return 0;
5076}
5077
5078
5079int p2p_set_wfd_ie_assoc_req(struct p2p_data *p2p, struct wpabuf *ie)
5080{
5081 wpabuf_free(p2p->wfd_ie_assoc_req);
5082 p2p->wfd_ie_assoc_req = ie;
5083 return 0;
5084}
5085
5086
5087int p2p_set_wfd_ie_invitation(struct p2p_data *p2p, struct wpabuf *ie)
5088{
5089 wpabuf_free(p2p->wfd_ie_invitation);
5090 p2p->wfd_ie_invitation = ie;
5091 return 0;
5092}
5093
5094
5095int p2p_set_wfd_ie_prov_disc_req(struct p2p_data *p2p, struct wpabuf *ie)
5096{
5097 wpabuf_free(p2p->wfd_ie_prov_disc_req);
5098 p2p->wfd_ie_prov_disc_req = ie;
5099 return 0;
5100}
5101
5102
5103int p2p_set_wfd_ie_prov_disc_resp(struct p2p_data *p2p, struct wpabuf *ie)
5104{
5105 wpabuf_free(p2p->wfd_ie_prov_disc_resp);
5106 p2p->wfd_ie_prov_disc_resp = ie;
5107 return 0;
5108}
5109
5110
5111int p2p_set_wfd_ie_go_neg(struct p2p_data *p2p, struct wpabuf *ie)
5112{
5113 wpabuf_free(p2p->wfd_ie_go_neg);
5114 p2p->wfd_ie_go_neg = ie;
5115 return 0;
5116}
5117
5118
5119int p2p_set_wfd_dev_info(struct p2p_data *p2p, const struct wpabuf *elem)
5120{
5121 wpabuf_free(p2p->wfd_dev_info);
5122 if (elem) {
5123 p2p->wfd_dev_info = wpabuf_dup(elem);
5124 if (p2p->wfd_dev_info == NULL)
5125 return -1;
5126 } else
5127 p2p->wfd_dev_info = NULL;
5128
5129 return 0;
5130}
5131
5132
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005133int p2p_set_wfd_r2_dev_info(struct p2p_data *p2p, const struct wpabuf *elem)
5134{
5135 wpabuf_free(p2p->wfd_r2_dev_info);
5136 if (elem) {
5137 p2p->wfd_r2_dev_info = wpabuf_dup(elem);
5138 if (p2p->wfd_r2_dev_info == NULL)
5139 return -1;
5140 } else
5141 p2p->wfd_r2_dev_info = NULL;
5142
5143 return 0;
5144}
5145
5146
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005147int p2p_set_wfd_assoc_bssid(struct p2p_data *p2p, const struct wpabuf *elem)
5148{
5149 wpabuf_free(p2p->wfd_assoc_bssid);
5150 if (elem) {
5151 p2p->wfd_assoc_bssid = wpabuf_dup(elem);
5152 if (p2p->wfd_assoc_bssid == NULL)
5153 return -1;
5154 } else
5155 p2p->wfd_assoc_bssid = NULL;
5156
5157 return 0;
5158}
5159
5160
5161int p2p_set_wfd_coupled_sink_info(struct p2p_data *p2p,
5162 const struct wpabuf *elem)
5163{
5164 wpabuf_free(p2p->wfd_coupled_sink_info);
5165 if (elem) {
5166 p2p->wfd_coupled_sink_info = wpabuf_dup(elem);
5167 if (p2p->wfd_coupled_sink_info == NULL)
5168 return -1;
5169 } else
5170 p2p->wfd_coupled_sink_info = NULL;
5171
5172 return 0;
5173}
5174
5175#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005176
5177
5178int p2p_set_disc_int(struct p2p_data *p2p, int min_disc_int, int max_disc_int,
5179 int max_disc_tu)
5180{
5181 if (min_disc_int > max_disc_int || min_disc_int < 0 || max_disc_int < 0)
5182 return -1;
5183
5184 p2p->min_disc_int = min_disc_int;
5185 p2p->max_disc_int = max_disc_int;
5186 p2p->max_disc_tu = max_disc_tu;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005187 p2p_dbg(p2p, "Set discoverable interval: min=%d max=%d max_tu=%d",
5188 min_disc_int, max_disc_int, max_disc_tu);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005189
5190 return 0;
5191}
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005192
5193
5194void p2p_dbg(struct p2p_data *p2p, const char *fmt, ...)
5195{
5196 va_list ap;
5197 char buf[500];
5198
5199 if (!p2p->cfg->debug_print)
5200 return;
5201
5202 va_start(ap, fmt);
5203 vsnprintf(buf, sizeof(buf), fmt, ap);
5204 buf[sizeof(buf) - 1] = '\0';
5205 va_end(ap);
5206 p2p->cfg->debug_print(p2p->cfg->cb_ctx, MSG_DEBUG, buf);
5207}
5208
5209
5210void p2p_info(struct p2p_data *p2p, const char *fmt, ...)
5211{
5212 va_list ap;
5213 char buf[500];
5214
5215 if (!p2p->cfg->debug_print)
5216 return;
5217
5218 va_start(ap, fmt);
5219 vsnprintf(buf, sizeof(buf), fmt, ap);
5220 buf[sizeof(buf) - 1] = '\0';
5221 va_end(ap);
5222 p2p->cfg->debug_print(p2p->cfg->cb_ctx, MSG_INFO, buf);
5223}
5224
5225
5226void p2p_err(struct p2p_data *p2p, const char *fmt, ...)
5227{
5228 va_list ap;
5229 char buf[500];
5230
5231 if (!p2p->cfg->debug_print)
5232 return;
5233
5234 va_start(ap, fmt);
5235 vsnprintf(buf, sizeof(buf), fmt, ap);
5236 buf[sizeof(buf) - 1] = '\0';
5237 va_end(ap);
5238 p2p->cfg->debug_print(p2p->cfg->cb_ctx, MSG_ERROR, buf);
5239}
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005240
5241
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07005242void p2p_loop_on_known_peers(struct p2p_data *p2p,
5243 void (*peer_callback)(struct p2p_peer_info *peer,
5244 void *user_data),
5245 void *user_data)
5246{
5247 struct p2p_device *dev, *n;
5248
5249 dl_list_for_each_safe(dev, n, &p2p->devices, struct p2p_device, list) {
5250 peer_callback(&dev->info, user_data);
5251 }
5252}
5253
5254
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005255#ifdef CONFIG_WPS_NFC
5256
5257static struct wpabuf * p2p_build_nfc_handover(struct p2p_data *p2p,
5258 int client_freq,
5259 const u8 *go_dev_addr,
5260 const u8 *ssid, size_t ssid_len)
5261{
5262 struct wpabuf *buf;
5263 u8 op_class, channel;
5264 enum p2p_role_indication role = P2P_DEVICE_NOT_IN_GROUP;
5265
5266 buf = wpabuf_alloc(1000);
5267 if (buf == NULL)
5268 return NULL;
5269
5270 op_class = p2p->cfg->reg_class;
5271 channel = p2p->cfg->channel;
5272
5273 p2p_buf_add_capability(buf, p2p->dev_capab &
5274 ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY, 0);
5275 p2p_buf_add_device_info(buf, p2p, NULL);
5276
5277 if (p2p->num_groups > 0) {
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07005278 int freq = p2p_group_get_freq(p2p->groups[0]);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005279 role = P2P_GO_IN_A_GROUP;
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07005280 if (p2p_freq_to_channel(freq, &op_class, &channel) < 0) {
5281 p2p_dbg(p2p,
5282 "Unknown GO operating frequency %d MHz for NFC handover",
5283 freq);
5284 wpabuf_free(buf);
5285 return NULL;
5286 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005287 } else if (client_freq > 0) {
5288 role = P2P_CLIENT_IN_A_GROUP;
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07005289 if (p2p_freq_to_channel(client_freq, &op_class, &channel) < 0) {
5290 p2p_dbg(p2p,
5291 "Unknown client operating frequency %d MHz for NFC handover",
5292 client_freq);
5293 wpabuf_free(buf);
5294 return NULL;
5295 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005296 }
5297
5298 p2p_buf_add_oob_go_neg_channel(buf, p2p->cfg->country, op_class,
5299 channel, role);
5300
5301 if (p2p->num_groups > 0) {
5302 /* Limit number of clients to avoid very long message */
5303 p2p_buf_add_group_info(p2p->groups[0], buf, 5);
5304 p2p_group_buf_add_id(p2p->groups[0], buf);
5305 } else if (client_freq > 0 &&
5306 go_dev_addr && !is_zero_ether_addr(go_dev_addr) &&
5307 ssid && ssid_len > 0) {
5308 /*
5309 * Add the optional P2P Group ID to indicate in which group this
5310 * device is a P2P Client.
5311 */
5312 p2p_buf_add_group_id(buf, go_dev_addr, ssid, ssid_len);
5313 }
5314
5315 return buf;
5316}
5317
5318
5319struct wpabuf * p2p_build_nfc_handover_req(struct p2p_data *p2p,
5320 int client_freq,
5321 const u8 *go_dev_addr,
5322 const u8 *ssid, size_t ssid_len)
5323{
5324 return p2p_build_nfc_handover(p2p, client_freq, go_dev_addr, ssid,
5325 ssid_len);
5326}
5327
5328
5329struct wpabuf * p2p_build_nfc_handover_sel(struct p2p_data *p2p,
5330 int client_freq,
5331 const u8 *go_dev_addr,
5332 const u8 *ssid, size_t ssid_len)
5333{
5334 return p2p_build_nfc_handover(p2p, client_freq, go_dev_addr, ssid,
5335 ssid_len);
5336}
5337
5338
5339int p2p_process_nfc_connection_handover(struct p2p_data *p2p,
5340 struct p2p_nfc_params *params)
5341{
5342 struct p2p_message msg;
5343 struct p2p_device *dev;
5344 const u8 *p2p_dev_addr;
5345 int freq;
5346 enum p2p_role_indication role;
5347
5348 params->next_step = NO_ACTION;
5349
5350 if (p2p_parse_ies_separate(params->wsc_attr, params->wsc_len,
5351 params->p2p_attr, params->p2p_len, &msg)) {
5352 p2p_dbg(p2p, "Failed to parse WSC/P2P attributes from NFC");
5353 p2p_parse_free(&msg);
5354 return -1;
5355 }
5356
5357 if (msg.p2p_device_addr)
5358 p2p_dev_addr = msg.p2p_device_addr;
5359 else if (msg.device_id)
5360 p2p_dev_addr = msg.device_id;
5361 else {
5362 p2p_dbg(p2p, "Ignore scan data without P2P Device Info or P2P Device Id");
5363 p2p_parse_free(&msg);
5364 return -1;
5365 }
5366
5367 if (msg.oob_dev_password) {
5368 os_memcpy(params->oob_dev_pw, msg.oob_dev_password,
5369 msg.oob_dev_password_len);
5370 params->oob_dev_pw_len = msg.oob_dev_password_len;
5371 }
5372
5373 dev = p2p_create_device(p2p, p2p_dev_addr);
5374 if (dev == NULL) {
5375 p2p_parse_free(&msg);
5376 return -1;
5377 }
5378
5379 params->peer = &dev->info;
5380
5381 os_get_reltime(&dev->last_seen);
5382 dev->flags &= ~(P2P_DEV_PROBE_REQ_ONLY | P2P_DEV_GROUP_CLIENT_ONLY);
5383 p2p_copy_wps_info(p2p, dev, 0, &msg);
5384
5385 if (!msg.oob_go_neg_channel) {
5386 p2p_dbg(p2p, "OOB GO Negotiation Channel attribute not included");
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07005387 p2p_parse_free(&msg);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005388 return -1;
5389 }
5390
5391 if (msg.oob_go_neg_channel[3] == 0 &&
5392 msg.oob_go_neg_channel[4] == 0)
5393 freq = 0;
5394 else
5395 freq = p2p_channel_to_freq(msg.oob_go_neg_channel[3],
5396 msg.oob_go_neg_channel[4]);
5397 if (freq < 0) {
5398 p2p_dbg(p2p, "Unknown peer OOB GO Neg channel");
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07005399 p2p_parse_free(&msg);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005400 return -1;
5401 }
5402 role = msg.oob_go_neg_channel[5];
5403
5404 if (role == P2P_GO_IN_A_GROUP) {
5405 p2p_dbg(p2p, "Peer OOB GO operating channel: %u MHz", freq);
5406 params->go_freq = freq;
5407 } else if (role == P2P_CLIENT_IN_A_GROUP) {
5408 p2p_dbg(p2p, "Peer (client) OOB GO operating channel: %u MHz",
5409 freq);
5410 params->go_freq = freq;
5411 } else
5412 p2p_dbg(p2p, "Peer OOB GO Neg channel: %u MHz", freq);
5413 dev->oob_go_neg_freq = freq;
5414
5415 if (!params->sel && role != P2P_GO_IN_A_GROUP) {
5416 freq = p2p_channel_to_freq(p2p->cfg->reg_class,
5417 p2p->cfg->channel);
5418 if (freq < 0) {
5419 p2p_dbg(p2p, "Own listen channel not known");
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07005420 p2p_parse_free(&msg);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005421 return -1;
5422 }
5423 p2p_dbg(p2p, "Use own Listen channel as OOB GO Neg channel: %u MHz", freq);
5424 dev->oob_go_neg_freq = freq;
5425 }
5426
5427 if (msg.group_id) {
5428 os_memcpy(params->go_dev_addr, msg.group_id, ETH_ALEN);
5429 params->go_ssid_len = msg.group_id_len - ETH_ALEN;
5430 os_memcpy(params->go_ssid, msg.group_id + ETH_ALEN,
5431 params->go_ssid_len);
5432 }
5433
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005434 if (dev->flags & P2P_DEV_USER_REJECTED) {
5435 p2p_dbg(p2p, "Do not report rejected device");
Dmitry Shmidt71757432014-06-02 13:50:35 -07005436 p2p_parse_free(&msg);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005437 return 0;
5438 }
5439
5440 if (!(dev->flags & P2P_DEV_REPORTED)) {
5441 p2p->cfg->dev_found(p2p->cfg->cb_ctx, p2p_dev_addr, &dev->info,
5442 !(dev->flags & P2P_DEV_REPORTED_ONCE));
5443 dev->flags |= P2P_DEV_REPORTED | P2P_DEV_REPORTED_ONCE;
5444 }
Dmitry Shmidt71757432014-06-02 13:50:35 -07005445 p2p_parse_free(&msg);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005446
5447 if (role == P2P_GO_IN_A_GROUP && p2p->num_groups > 0)
5448 params->next_step = BOTH_GO;
5449 else if (role == P2P_GO_IN_A_GROUP)
5450 params->next_step = JOIN_GROUP;
5451 else if (role == P2P_CLIENT_IN_A_GROUP) {
5452 dev->flags |= P2P_DEV_GROUP_CLIENT_ONLY;
5453 params->next_step = PEER_CLIENT;
5454 } else if (p2p->num_groups > 0)
5455 params->next_step = AUTH_JOIN;
5456 else if (params->sel)
5457 params->next_step = INIT_GO_NEG;
5458 else
5459 params->next_step = RESP_GO_NEG;
5460
5461 return 0;
5462}
5463
5464
5465void p2p_set_authorized_oob_dev_pw_id(struct p2p_data *p2p, u16 dev_pw_id,
5466 int go_intent,
5467 const u8 *own_interface_addr)
5468{
5469
5470 p2p->authorized_oob_dev_pw_id = dev_pw_id;
5471 if (dev_pw_id == 0) {
5472 p2p_dbg(p2p, "NFC OOB Password unauthorized for static handover");
5473 return;
5474 }
5475
5476 p2p_dbg(p2p, "NFC OOB Password (id=%u) authorized for static handover",
5477 dev_pw_id);
5478
5479 p2p->go_intent = go_intent;
5480 os_memcpy(p2p->intended_addr, own_interface_addr, ETH_ALEN);
5481}
5482
5483#endif /* CONFIG_WPS_NFC */
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07005484
5485
5486int p2p_set_passphrase_len(struct p2p_data *p2p, unsigned int len)
5487{
5488 if (len < 8 || len > 63)
5489 return -1;
5490 p2p->cfg->passphrase_len = len;
5491 return 0;
5492}
Dmitry Shmidt2e67f062014-07-16 09:55:28 -07005493
5494
5495void p2p_set_vendor_elems(struct p2p_data *p2p, struct wpabuf **vendor_elem)
5496{
5497 p2p->vendor_elem = vendor_elem;
5498}
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005499
5500
5501void p2p_go_neg_wait_timeout(void *eloop_ctx, void *timeout_ctx)
5502{
5503 struct p2p_data *p2p = eloop_ctx;
5504
5505 p2p_dbg(p2p,
5506 "Timeout on waiting peer to become ready for GO Negotiation");
5507 p2p_go_neg_failed(p2p, -1);
5508}
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005509
5510
5511void p2p_set_own_pref_freq_list(struct p2p_data *p2p,
5512 const unsigned int *pref_freq_list,
5513 unsigned int size)
5514{
5515 unsigned int i;
5516
5517 if (size > P2P_MAX_PREF_CHANNELS)
5518 size = P2P_MAX_PREF_CHANNELS;
5519 p2p->num_pref_freq = size;
5520 for (i = 0; i < size; i++) {
5521 p2p->pref_freq_list[i] = pref_freq_list[i];
5522 p2p_dbg(p2p, "Own preferred frequency list[%u]=%u MHz",
5523 i, p2p->pref_freq_list[i]);
5524 }
5525}
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07005526
5527
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08005528void p2p_set_override_pref_op_chan(struct p2p_data *p2p, u8 op_class,
5529 u8 chan)
5530{
5531 p2p->override_pref_op_class = op_class;
5532 p2p->override_pref_channel = chan;
5533}
5534
5535
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07005536struct wpabuf * p2p_build_probe_resp_template(struct p2p_data *p2p,
5537 unsigned int freq)
5538{
5539 struct wpabuf *ies, *buf;
5540 u8 addr[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
5541 int ret;
5542
5543 ies = p2p_build_probe_resp_ies(p2p, NULL, 0);
5544 if (!ies) {
5545 wpa_printf(MSG_ERROR,
5546 "CTRL: Failed to build Probe Response IEs");
5547 return NULL;
5548 }
5549
5550 buf = wpabuf_alloc(200 + wpabuf_len(ies));
5551 if (!buf) {
5552 wpabuf_free(ies);
5553 return NULL;
5554 }
5555
5556 ret = p2p_build_probe_resp_buf(p2p, buf, ies, addr, freq);
5557 wpabuf_free(ies);
5558 if (ret) {
5559 wpabuf_free(buf);
5560 return NULL;
5561 }
5562
5563 return buf;
5564}