blob: c67ecb68c022c68e1f79106c5aa3e1ec95ff578e [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
11#include "common.h"
12#include "eloop.h"
Dmitry Shmidt9c175262016-03-03 10:20:07 -080013#include "common/defs.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070014#include "common/ieee802_11_defs.h"
15#include "common/ieee802_11_common.h"
Dmitry Shmidt2e67f062014-07-16 09:55:28 -070016#include "common/wpa_ctrl.h"
Dmitry Shmidt216983b2015-02-06 10:50:36 -080017#include "crypto/sha256.h"
18#include "crypto/crypto.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070019#include "wps/wps_i.h"
20#include "p2p_i.h"
21#include "p2p.h"
22
23
24static void p2p_state_timeout(void *eloop_ctx, void *timeout_ctx);
25static void p2p_device_free(struct p2p_data *p2p, struct p2p_device *dev);
26static void p2p_process_presence_req(struct p2p_data *p2p, const u8 *da,
27 const u8 *sa, const u8 *data, size_t len,
28 int rx_freq);
29static void p2p_process_presence_resp(struct p2p_data *p2p, const u8 *da,
30 const u8 *sa, const u8 *data,
31 size_t len);
32static void p2p_ext_listen_timeout(void *eloop_ctx, void *timeout_ctx);
33static void p2p_scan_timeout(void *eloop_ctx, void *timeout_ctx);
34
35
36/*
37 * p2p_scan recovery timeout
38 *
39 * Many drivers are using 30 second timeout on scan results. Allow a bit larger
40 * timeout for this to avoid hitting P2P timeout unnecessarily.
41 */
42#define P2P_SCAN_TIMEOUT 35
43
44/**
45 * P2P_PEER_EXPIRATION_AGE - Number of seconds after which inactive peer
46 * entries will be removed
47 */
Dmitry Shmidt2093d062014-01-17 10:58:50 -080048#ifndef P2P_PEER_EXPIRATION_AGE
49#define P2P_PEER_EXPIRATION_AGE 60
Dmitry Shmidt18463232014-01-24 12:29:41 -080050#endif /* P2P_PEER_EXPIRATION_AGE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070051
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070052
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080053void p2p_expire_peers(struct p2p_data *p2p)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070054{
55 struct p2p_device *dev, *n;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -080056 struct os_reltime now;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -080057 size_t i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070058
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -080059 os_get_reltime(&now);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070060 dl_list_for_each_safe(dev, n, &p2p->devices, struct p2p_device, list) {
61 if (dev->last_seen.sec + P2P_PEER_EXPIRATION_AGE >= now.sec)
62 continue;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -080063
Dmitry Shmidt9e3f8ee2014-01-17 10:52:01 -080064 if (dev == p2p->go_neg_peer) {
65 /*
66 * GO Negotiation is in progress with the peer, so
67 * don't expire the peer entry until GO Negotiation
68 * fails or times out.
69 */
70 continue;
71 }
72
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -080073 if (p2p->cfg->go_connected &&
74 p2p->cfg->go_connected(p2p->cfg->cb_ctx,
75 dev->info.p2p_device_addr)) {
76 /*
77 * We are connected as a client to a group in which the
78 * peer is the GO, so do not expire the peer entry.
79 */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -080080 os_get_reltime(&dev->last_seen);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -080081 continue;
82 }
83
84 for (i = 0; i < p2p->num_groups; i++) {
85 if (p2p_group_is_client_connected(
86 p2p->groups[i], dev->info.p2p_device_addr))
87 break;
88 }
89 if (i < p2p->num_groups) {
90 /*
91 * The peer is connected as a client in a group where
92 * we are the GO, so do not expire the peer entry.
93 */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -080094 os_get_reltime(&dev->last_seen);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -080095 continue;
96 }
97
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -070098 p2p_dbg(p2p, "Expiring old peer entry " MACSTR,
99 MAC2STR(dev->info.p2p_device_addr));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700100 dl_list_del(&dev->list);
101 p2p_device_free(p2p, dev);
102 }
103}
104
105
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700106static const char * p2p_state_txt(int state)
107{
108 switch (state) {
109 case P2P_IDLE:
110 return "IDLE";
111 case P2P_SEARCH:
112 return "SEARCH";
113 case P2P_CONNECT:
114 return "CONNECT";
115 case P2P_CONNECT_LISTEN:
116 return "CONNECT_LISTEN";
117 case P2P_GO_NEG:
118 return "GO_NEG";
119 case P2P_LISTEN_ONLY:
120 return "LISTEN_ONLY";
121 case P2P_WAIT_PEER_CONNECT:
122 return "WAIT_PEER_CONNECT";
123 case P2P_WAIT_PEER_IDLE:
124 return "WAIT_PEER_IDLE";
125 case P2P_SD_DURING_FIND:
126 return "SD_DURING_FIND";
127 case P2P_PROVISIONING:
128 return "PROVISIONING";
129 case P2P_PD_DURING_FIND:
130 return "PD_DURING_FIND";
131 case P2P_INVITE:
132 return "INVITE";
133 case P2P_INVITE_LISTEN:
134 return "INVITE_LISTEN";
135 default:
136 return "?";
137 }
138}
139
140
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700141const char * p2p_get_state_txt(struct p2p_data *p2p)
142{
143 return p2p_state_txt(p2p->state);
144}
145
146
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800147struct p2ps_advertisement * p2p_get_p2ps_adv_list(struct p2p_data *p2p)
148{
149 return p2p ? p2p->p2ps_adv_list : NULL;
150}
151
152
153void p2p_set_intended_addr(struct p2p_data *p2p, const u8 *intended_addr)
154{
155 if (p2p && intended_addr)
156 os_memcpy(p2p->intended_addr, intended_addr, ETH_ALEN);
157}
158
159
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800160u16 p2p_get_provisioning_info(struct p2p_data *p2p, const u8 *addr)
161{
162 struct p2p_device *dev = NULL;
163
164 if (!addr || !p2p)
165 return 0;
166
167 dev = p2p_get_device(p2p, addr);
168 if (dev)
169 return dev->wps_prov_info;
170 else
171 return 0;
172}
173
174
Dmitry Shmidt04949592012-07-19 12:16:46 -0700175void p2p_clear_provisioning_info(struct p2p_data *p2p, const u8 *addr)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800176{
177 struct p2p_device *dev = NULL;
178
Dmitry Shmidt04949592012-07-19 12:16:46 -0700179 if (!addr || !p2p)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800180 return;
181
Dmitry Shmidt04949592012-07-19 12:16:46 -0700182 dev = p2p_get_device(p2p, addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800183 if (dev)
184 dev->wps_prov_info = 0;
185}
186
187
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700188void p2p_set_state(struct p2p_data *p2p, int new_state)
189{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700190 p2p_dbg(p2p, "State %s -> %s",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700191 p2p_state_txt(p2p->state), p2p_state_txt(new_state));
192 p2p->state = new_state;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700193
194 if (new_state == P2P_IDLE && p2p->pending_channel) {
195 p2p_dbg(p2p, "Apply change in listen channel");
196 p2p->cfg->reg_class = p2p->pending_reg_class;
197 p2p->cfg->channel = p2p->pending_channel;
198 p2p->pending_reg_class = 0;
199 p2p->pending_channel = 0;
200 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700201}
202
203
204void p2p_set_timeout(struct p2p_data *p2p, unsigned int sec, unsigned int usec)
205{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700206 p2p_dbg(p2p, "Set timeout (state=%s): %u.%06u sec",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700207 p2p_state_txt(p2p->state), sec, usec);
208 eloop_cancel_timeout(p2p_state_timeout, p2p, NULL);
209 eloop_register_timeout(sec, usec, p2p_state_timeout, p2p, NULL);
210}
211
212
213void p2p_clear_timeout(struct p2p_data *p2p)
214{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700215 p2p_dbg(p2p, "Clear timeout (state=%s)", p2p_state_txt(p2p->state));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700216 eloop_cancel_timeout(p2p_state_timeout, p2p, NULL);
217}
218
219
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800220void p2p_go_neg_failed(struct p2p_data *p2p, int status)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700221{
222 struct p2p_go_neg_results res;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800223 struct p2p_device *peer = p2p->go_neg_peer;
224
225 if (!peer)
226 return;
227
228 eloop_cancel_timeout(p2p_go_neg_wait_timeout, p2p, NULL);
229 if (p2p->state != P2P_SEARCH) {
230 /*
231 * Clear timeouts related to GO Negotiation if no new p2p_find
232 * has been started.
233 */
234 p2p_clear_timeout(p2p);
235 p2p_set_state(p2p, P2P_IDLE);
Dmitry Shmidt8c652892013-03-01 10:14:01 -0800236 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800237
238 peer->flags &= ~P2P_DEV_PEER_WAITING_RESPONSE;
239 peer->wps_method = WPS_NOT_READY;
240 peer->oob_pw_id = 0;
241 wpabuf_free(peer->go_neg_conf);
242 peer->go_neg_conf = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700243 p2p->go_neg_peer = NULL;
244
245 os_memset(&res, 0, sizeof(res));
246 res.status = status;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800247 os_memcpy(res.peer_device_addr, peer->info.p2p_device_addr, ETH_ALEN);
248 os_memcpy(res.peer_interface_addr, peer->intended_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700249 p2p->cfg->go_neg_completed(p2p->cfg->cb_ctx, &res);
250}
251
252
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800253static void p2p_listen_in_find(struct p2p_data *p2p, int dev_disc)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700254{
255 unsigned int r, tu;
256 int freq;
257 struct wpabuf *ies;
258
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700259 p2p_dbg(p2p, "Starting short listen state (state=%s)",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700260 p2p_state_txt(p2p->state));
261
Dmitry Shmidt3c57b3f2014-05-22 15:13:07 -0700262 if (p2p->pending_listen_freq) {
263 /* We have a pending p2p_listen request */
264 p2p_dbg(p2p, "p2p_listen command pending already");
265 return;
266 }
267
Dmitry Shmidt4b060592013-04-29 16:42:49 -0700268 freq = p2p_channel_to_freq(p2p->cfg->reg_class, p2p->cfg->channel);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700269 if (freq < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700270 p2p_dbg(p2p, "Unknown regulatory class/channel");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700271 return;
272 }
273
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700274 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
275 r = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700276 tu = (r % ((p2p->max_disc_int - p2p->min_disc_int) + 1) +
277 p2p->min_disc_int) * 100;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800278 if (p2p->max_disc_tu >= 0 && tu > (unsigned int) p2p->max_disc_tu)
279 tu = p2p->max_disc_tu;
280 if (!dev_disc && tu < 100)
281 tu = 100; /* Need to wait in non-device discovery use cases */
282 if (p2p->cfg->max_listen && 1024 * tu / 1000 > p2p->cfg->max_listen)
283 tu = p2p->cfg->max_listen * 1000 / 1024;
284
285 if (tu == 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700286 p2p_dbg(p2p, "Skip listen state since duration was 0 TU");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800287 p2p_set_timeout(p2p, 0, 0);
288 return;
289 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700290
Dmitry Shmidta3dc3092015-06-23 11:21:28 -0700291 ies = p2p_build_probe_resp_ies(p2p, NULL, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700292 if (ies == NULL)
293 return;
294
Dmitry Shmidt3c57b3f2014-05-22 15:13:07 -0700295 p2p->pending_listen_freq = freq;
296 p2p->pending_listen_sec = 0;
297 p2p->pending_listen_usec = 1024 * tu;
298
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700299 if (p2p->cfg->start_listen(p2p->cfg->cb_ctx, freq, 1024 * tu / 1000,
300 ies) < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700301 p2p_dbg(p2p, "Failed to start listen mode");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700302 p2p->pending_listen_freq = 0;
303 }
304 wpabuf_free(ies);
305}
306
307
308int p2p_listen(struct p2p_data *p2p, unsigned int timeout)
309{
310 int freq;
311 struct wpabuf *ies;
312
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700313 p2p_dbg(p2p, "Going to listen(only) state");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700314
Dmitry Shmidt3c57b3f2014-05-22 15:13:07 -0700315 if (p2p->pending_listen_freq) {
316 /* We have a pending p2p_listen request */
317 p2p_dbg(p2p, "p2p_listen command pending already");
318 return -1;
319 }
320
Dmitry Shmidt4b060592013-04-29 16:42:49 -0700321 freq = p2p_channel_to_freq(p2p->cfg->reg_class, p2p->cfg->channel);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700322 if (freq < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700323 p2p_dbg(p2p, "Unknown regulatory class/channel");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700324 return -1;
325 }
326
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700327 p2p->pending_listen_sec = timeout / 1000;
328 p2p->pending_listen_usec = (timeout % 1000) * 1000;
329
330 if (p2p->p2p_scan_running) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700331 if (p2p->start_after_scan == P2P_AFTER_SCAN_CONNECT) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700332 p2p_dbg(p2p, "p2p_scan running - connect is already pending - skip listen");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800333 return 0;
334 }
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700335 p2p_dbg(p2p, "p2p_scan running - delay start of listen state");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700336 p2p->start_after_scan = P2P_AFTER_SCAN_LISTEN;
337 return 0;
338 }
339
Dmitry Shmidta3dc3092015-06-23 11:21:28 -0700340 ies = p2p_build_probe_resp_ies(p2p, NULL, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700341 if (ies == NULL)
342 return -1;
343
Dmitry Shmidt3c57b3f2014-05-22 15:13:07 -0700344 p2p->pending_listen_freq = freq;
345
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700346 if (p2p->cfg->start_listen(p2p->cfg->cb_ctx, freq, timeout, ies) < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700347 p2p_dbg(p2p, "Failed to start listen mode");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700348 p2p->pending_listen_freq = 0;
349 wpabuf_free(ies);
350 return -1;
351 }
352 wpabuf_free(ies);
353
354 p2p_set_state(p2p, P2P_LISTEN_ONLY);
355
356 return 0;
357}
358
359
360static void p2p_device_clear_reported(struct p2p_data *p2p)
361{
362 struct p2p_device *dev;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800363 dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700364 dev->flags &= ~P2P_DEV_REPORTED;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800365 dev->sd_reqs = 0;
366 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700367}
368
369
370/**
371 * p2p_get_device - Fetch a peer entry
372 * @p2p: P2P module context from p2p_init()
373 * @addr: P2P Device Address of the peer
374 * Returns: Pointer to the device entry or %NULL if not found
375 */
376struct p2p_device * p2p_get_device(struct p2p_data *p2p, const u8 *addr)
377{
378 struct p2p_device *dev;
379 dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
380 if (os_memcmp(dev->info.p2p_device_addr, addr, ETH_ALEN) == 0)
381 return dev;
382 }
383 return NULL;
384}
385
386
387/**
388 * p2p_get_device_interface - Fetch a peer entry based on P2P Interface Address
389 * @p2p: P2P module context from p2p_init()
390 * @addr: P2P Interface Address of the peer
391 * Returns: Pointer to the device entry or %NULL if not found
392 */
393struct p2p_device * p2p_get_device_interface(struct p2p_data *p2p,
394 const u8 *addr)
395{
396 struct p2p_device *dev;
397 dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
398 if (os_memcmp(dev->interface_addr, addr, ETH_ALEN) == 0)
399 return dev;
400 }
401 return NULL;
402}
403
404
405/**
406 * p2p_create_device - Create a peer entry
407 * @p2p: P2P module context from p2p_init()
408 * @addr: P2P Device Address of the peer
409 * Returns: Pointer to the device entry or %NULL on failure
410 *
411 * If there is already an entry for the peer, it will be returned instead of
412 * creating a new one.
413 */
414static struct p2p_device * p2p_create_device(struct p2p_data *p2p,
415 const u8 *addr)
416{
417 struct p2p_device *dev, *oldest = NULL;
418 size_t count = 0;
419
420 dev = p2p_get_device(p2p, addr);
421 if (dev)
422 return dev;
423
424 dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
425 count++;
426 if (oldest == NULL ||
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800427 os_reltime_before(&dev->last_seen, &oldest->last_seen))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700428 oldest = dev;
429 }
430 if (count + 1 > p2p->cfg->max_peers && oldest) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700431 p2p_dbg(p2p, "Remove oldest peer entry to make room for a new peer");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700432 dl_list_del(&oldest->list);
433 p2p_device_free(p2p, oldest);
434 }
435
436 dev = os_zalloc(sizeof(*dev));
437 if (dev == NULL)
438 return NULL;
439 dl_list_add(&p2p->devices, &dev->list);
440 os_memcpy(dev->info.p2p_device_addr, addr, ETH_ALEN);
441
442 return dev;
443}
444
445
446static void p2p_copy_client_info(struct p2p_device *dev,
447 struct p2p_client_info *cli)
448{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800449 p2p_copy_filter_devname(dev->info.device_name,
450 sizeof(dev->info.device_name),
451 cli->dev_name, cli->dev_name_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700452 dev->info.dev_capab = cli->dev_capab;
453 dev->info.config_methods = cli->config_methods;
454 os_memcpy(dev->info.pri_dev_type, cli->pri_dev_type, 8);
455 dev->info.wps_sec_dev_type_list_len = 8 * cli->num_sec_dev_types;
Jimmy Chene12b6972020-11-09 11:43:12 +0200456 if (dev->info.wps_sec_dev_type_list_len > WPS_SEC_DEV_TYPE_MAX_LEN)
457 dev->info.wps_sec_dev_type_list_len = WPS_SEC_DEV_TYPE_MAX_LEN;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700458 os_memcpy(dev->info.wps_sec_dev_type_list, cli->sec_dev_types,
459 dev->info.wps_sec_dev_type_list_len);
460}
461
462
463static int p2p_add_group_clients(struct p2p_data *p2p, const u8 *go_dev_addr,
464 const u8 *go_interface_addr, int freq,
Dmitry Shmidta3dc3092015-06-23 11:21:28 -0700465 const u8 *gi, size_t gi_len,
466 struct os_reltime *rx_time)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700467{
468 struct p2p_group_info info;
469 size_t c;
470 struct p2p_device *dev;
471
472 if (gi == NULL)
473 return 0;
474
475 if (p2p_group_info_parse(gi, gi_len, &info) < 0)
476 return -1;
477
478 /*
479 * Clear old data for this group; if the devices are still in the
480 * group, the information will be restored in the loop following this.
481 */
482 dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800483 if (os_memcmp(dev->member_in_go_iface, go_interface_addr,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700484 ETH_ALEN) == 0) {
485 os_memset(dev->member_in_go_iface, 0, ETH_ALEN);
486 os_memset(dev->member_in_go_dev, 0, ETH_ALEN);
487 }
488 }
489
490 for (c = 0; c < info.num_clients; c++) {
491 struct p2p_client_info *cli = &info.client[c];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800492 if (os_memcmp(cli->p2p_device_addr, p2p->cfg->dev_addr,
493 ETH_ALEN) == 0)
494 continue; /* ignore our own entry */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700495 dev = p2p_get_device(p2p, cli->p2p_device_addr);
496 if (dev) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700497 if (dev->flags & (P2P_DEV_GROUP_CLIENT_ONLY |
Dmitry Shmidt04949592012-07-19 12:16:46 -0700498 P2P_DEV_PROBE_REQ_ONLY)) {
499 /*
500 * Update information since we have not
501 * received this directly from the client.
502 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700503 p2p_copy_client_info(dev, cli);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700504 } else {
505 /*
506 * Need to update P2P Client Discoverability
507 * flag since it is valid only in P2P Group
508 * Info attribute.
509 */
510 dev->info.dev_capab &=
511 ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
512 dev->info.dev_capab |=
513 cli->dev_capab &
514 P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
515 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700516 if (dev->flags & P2P_DEV_PROBE_REQ_ONLY) {
517 dev->flags &= ~P2P_DEV_PROBE_REQ_ONLY;
518 }
519 } else {
520 dev = p2p_create_device(p2p, cli->p2p_device_addr);
521 if (dev == NULL)
522 continue;
523 dev->flags |= P2P_DEV_GROUP_CLIENT_ONLY;
524 p2p_copy_client_info(dev, cli);
525 dev->oper_freq = freq;
526 p2p->cfg->dev_found(p2p->cfg->cb_ctx,
527 dev->info.p2p_device_addr,
528 &dev->info, 1);
529 dev->flags |= P2P_DEV_REPORTED | P2P_DEV_REPORTED_ONCE;
530 }
531
532 os_memcpy(dev->interface_addr, cli->p2p_interface_addr,
533 ETH_ALEN);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -0700534 os_memcpy(&dev->last_seen, rx_time, sizeof(struct os_reltime));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700535 os_memcpy(dev->member_in_go_dev, go_dev_addr, ETH_ALEN);
536 os_memcpy(dev->member_in_go_iface, go_interface_addr,
537 ETH_ALEN);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -0700538 dev->flags |= P2P_DEV_LAST_SEEN_AS_GROUP_CLIENT;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700539 }
540
541 return 0;
542}
543
544
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700545static void p2p_copy_wps_info(struct p2p_data *p2p, struct p2p_device *dev,
546 int probe_req, const struct p2p_message *msg)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700547{
548 os_memcpy(dev->info.device_name, msg->device_name,
549 sizeof(dev->info.device_name));
550
551 if (msg->manufacturer &&
552 msg->manufacturer_len < sizeof(dev->info.manufacturer)) {
553 os_memset(dev->info.manufacturer, 0,
554 sizeof(dev->info.manufacturer));
555 os_memcpy(dev->info.manufacturer, msg->manufacturer,
556 msg->manufacturer_len);
557 }
558
559 if (msg->model_name &&
560 msg->model_name_len < sizeof(dev->info.model_name)) {
561 os_memset(dev->info.model_name, 0,
562 sizeof(dev->info.model_name));
563 os_memcpy(dev->info.model_name, msg->model_name,
564 msg->model_name_len);
565 }
566
567 if (msg->model_number &&
568 msg->model_number_len < sizeof(dev->info.model_number)) {
569 os_memset(dev->info.model_number, 0,
570 sizeof(dev->info.model_number));
571 os_memcpy(dev->info.model_number, msg->model_number,
572 msg->model_number_len);
573 }
574
575 if (msg->serial_number &&
576 msg->serial_number_len < sizeof(dev->info.serial_number)) {
577 os_memset(dev->info.serial_number, 0,
578 sizeof(dev->info.serial_number));
579 os_memcpy(dev->info.serial_number, msg->serial_number,
580 msg->serial_number_len);
581 }
582
583 if (msg->pri_dev_type)
584 os_memcpy(dev->info.pri_dev_type, msg->pri_dev_type,
585 sizeof(dev->info.pri_dev_type));
586 else if (msg->wps_pri_dev_type)
587 os_memcpy(dev->info.pri_dev_type, msg->wps_pri_dev_type,
588 sizeof(dev->info.pri_dev_type));
589
590 if (msg->wps_sec_dev_type_list) {
591 os_memcpy(dev->info.wps_sec_dev_type_list,
592 msg->wps_sec_dev_type_list,
593 msg->wps_sec_dev_type_list_len);
594 dev->info.wps_sec_dev_type_list_len =
595 msg->wps_sec_dev_type_list_len;
596 }
597
598 if (msg->capability) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700599 /*
600 * P2P Client Discoverability bit is reserved in all frames
601 * that use this function, so do not change its value here.
602 */
603 dev->info.dev_capab &= P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
604 dev->info.dev_capab |= msg->capability[0] &
605 ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700606 dev->info.group_capab = msg->capability[1];
607 }
608
609 if (msg->ext_listen_timing) {
610 dev->ext_listen_period = WPA_GET_LE16(msg->ext_listen_timing);
611 dev->ext_listen_interval =
612 WPA_GET_LE16(msg->ext_listen_timing + 2);
613 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700614
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700615 if (!probe_req) {
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800616 u16 new_config_methods;
617 new_config_methods = msg->config_methods ?
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700618 msg->config_methods : msg->wps_config_methods;
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800619 if (new_config_methods &&
620 dev->info.config_methods != new_config_methods) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700621 p2p_dbg(p2p, "Update peer " MACSTR
622 " config_methods 0x%x -> 0x%x",
623 MAC2STR(dev->info.p2p_device_addr),
624 dev->info.config_methods,
625 new_config_methods);
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800626 dev->info.config_methods = new_config_methods;
627 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700628 }
629}
630
631
Dmitry Shmidt2e67f062014-07-16 09:55:28 -0700632static void p2p_update_peer_vendor_elems(struct p2p_device *dev, const u8 *ies,
633 size_t ies_len)
634{
635 const u8 *pos, *end;
636 u8 id, len;
637
638 wpabuf_free(dev->info.vendor_elems);
639 dev->info.vendor_elems = NULL;
640
641 end = ies + ies_len;
642
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800643 for (pos = ies; end - pos > 1; pos += len) {
Dmitry Shmidt2e67f062014-07-16 09:55:28 -0700644 id = *pos++;
645 len = *pos++;
646
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800647 if (len > end - pos)
Dmitry Shmidt2e67f062014-07-16 09:55:28 -0700648 break;
649
650 if (id != WLAN_EID_VENDOR_SPECIFIC || len < 3)
651 continue;
652
653 if (len >= 4) {
654 u32 type = WPA_GET_BE32(pos);
655
656 if (type == WPA_IE_VENDOR_TYPE ||
657 type == WMM_IE_VENDOR_TYPE ||
658 type == WPS_IE_VENDOR_TYPE ||
659 type == P2P_IE_VENDOR_TYPE ||
660 type == WFD_IE_VENDOR_TYPE)
661 continue;
662 }
663
664 /* Unknown vendor element - make raw IE data available */
665 if (wpabuf_resize(&dev->info.vendor_elems, 2 + len) < 0)
666 break;
667 wpabuf_put_data(dev->info.vendor_elems, pos - 2, 2 + len);
668 }
669}
670
671
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800672static int p2p_compare_wfd_info(struct p2p_device *dev,
673 const struct p2p_message *msg)
674{
675 if (dev->info.wfd_subelems && msg->wfd_subelems) {
676 if (dev->info.wfd_subelems->used != msg->wfd_subelems->used)
677 return 1;
678
679 return os_memcmp(dev->info.wfd_subelems->buf,
680 msg->wfd_subelems->buf,
681 dev->info.wfd_subelems->used);
682 }
683 if (dev->info.wfd_subelems || msg->wfd_subelems)
684 return 1;
685
686 return 0;
687}
688
689
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700690/**
Dmitry Shmidt04949592012-07-19 12:16:46 -0700691 * p2p_add_device - Add peer entries based on scan results or P2P frames
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700692 * @p2p: P2P module context from p2p_init()
693 * @addr: Source address of Beacon or Probe Response frame (may be either
694 * P2P Device Address or P2P Interface Address)
695 * @level: Signal level (signal strength of the received frame from the peer)
696 * @freq: Frequency on which the Beacon or Probe Response frame was received
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800697 * @rx_time: Time when the result was received
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700698 * @ies: IEs from the Beacon or Probe Response frame
699 * @ies_len: Length of ies buffer in octets
Dmitry Shmidt04949592012-07-19 12:16:46 -0700700 * @scan_res: Whether this was based on scan results
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700701 * Returns: 0 on success, -1 on failure
702 *
703 * If the scan result is for a GO, the clients in the group will also be added
704 * to the peer table. This function can also be used with some other frames
705 * like Provision Discovery Request that contains P2P Capability and P2P Device
706 * Info attributes.
707 */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800708int p2p_add_device(struct p2p_data *p2p, const u8 *addr, int freq,
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800709 struct os_reltime *rx_time, int level, const u8 *ies,
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800710 size_t ies_len, int scan_res)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700711{
712 struct p2p_device *dev;
713 struct p2p_message msg;
714 const u8 *p2p_dev_addr;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800715 int wfd_changed;
Dmitry Shmidt29333592017-01-09 12:27:11 -0800716 int dev_name_changed;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700717 int i;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800718 struct os_reltime time_now;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700719
720 os_memset(&msg, 0, sizeof(msg));
721 if (p2p_parse_ies(ies, ies_len, &msg)) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700722 p2p_dbg(p2p, "Failed to parse P2P IE for a device entry");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700723 p2p_parse_free(&msg);
724 return -1;
725 }
726
727 if (msg.p2p_device_addr)
728 p2p_dev_addr = msg.p2p_device_addr;
729 else if (msg.device_id)
730 p2p_dev_addr = msg.device_id;
731 else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700732 p2p_dbg(p2p, "Ignore scan data without P2P Device Info or P2P Device Id");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700733 p2p_parse_free(&msg);
734 return -1;
735 }
736
737 if (!is_zero_ether_addr(p2p->peer_filter) &&
738 os_memcmp(p2p_dev_addr, p2p->peer_filter, ETH_ALEN) != 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700739 p2p_dbg(p2p, "Do not add peer filter for " MACSTR
740 " due to peer filter", MAC2STR(p2p_dev_addr));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800741 p2p_parse_free(&msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700742 return 0;
743 }
744
745 dev = p2p_create_device(p2p, p2p_dev_addr);
746 if (dev == NULL) {
747 p2p_parse_free(&msg);
748 return -1;
749 }
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800750
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800751 if (rx_time == NULL) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800752 os_get_reltime(&time_now);
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800753 rx_time = &time_now;
754 }
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800755
756 /*
757 * Update the device entry only if the new peer
Dmitry Shmidta3dc3092015-06-23 11:21:28 -0700758 * entry is newer than the one previously stored, or if
759 * the device was previously seen as a P2P Client in a group
760 * and the new entry isn't older than a threshold.
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800761 */
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800762 if (dev->last_seen.sec > 0 &&
Dmitry Shmidta3dc3092015-06-23 11:21:28 -0700763 os_reltime_before(rx_time, &dev->last_seen) &&
764 (!(dev->flags & P2P_DEV_LAST_SEEN_AS_GROUP_CLIENT) ||
765 os_reltime_expired(&dev->last_seen, rx_time,
766 P2P_DEV_GROUP_CLIENT_RESP_THRESHOLD))) {
767 p2p_dbg(p2p,
768 "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 -0800769 (unsigned int) rx_time->sec,
770 (unsigned int) rx_time->usec,
771 (unsigned int) dev->last_seen.sec,
Dmitry Shmidta3dc3092015-06-23 11:21:28 -0700772 (unsigned int) dev->last_seen.usec,
773 dev->flags);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800774 p2p_parse_free(&msg);
775 return -1;
776 }
777
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800778 os_memcpy(&dev->last_seen, rx_time, sizeof(struct os_reltime));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800779
Dmitry Shmidta3dc3092015-06-23 11:21:28 -0700780 dev->flags &= ~(P2P_DEV_PROBE_REQ_ONLY | P2P_DEV_GROUP_CLIENT_ONLY |
781 P2P_DEV_LAST_SEEN_AS_GROUP_CLIENT);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700782
783 if (os_memcmp(addr, p2p_dev_addr, ETH_ALEN) != 0)
784 os_memcpy(dev->interface_addr, addr, ETH_ALEN);
785 if (msg.ssid &&
Jouni Malinenfdb708a2015-04-07 11:32:11 +0300786 msg.ssid[1] <= sizeof(dev->oper_ssid) &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700787 (msg.ssid[1] != P2P_WILDCARD_SSID_LEN ||
788 os_memcmp(msg.ssid + 2, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN)
789 != 0)) {
790 os_memcpy(dev->oper_ssid, msg.ssid + 2, msg.ssid[1]);
791 dev->oper_ssid_len = msg.ssid[1];
792 }
793
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -0700794 wpabuf_free(dev->info.p2ps_instance);
795 dev->info.p2ps_instance = NULL;
796 if (msg.adv_service_instance && msg.adv_service_instance_len)
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800797 dev->info.p2ps_instance = wpabuf_alloc_copy(
798 msg.adv_service_instance, msg.adv_service_instance_len);
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800799
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700800 if (freq >= 2412 && freq <= 2484 && msg.ds_params &&
801 *msg.ds_params >= 1 && *msg.ds_params <= 14) {
802 int ds_freq;
803 if (*msg.ds_params == 14)
804 ds_freq = 2484;
805 else
806 ds_freq = 2407 + *msg.ds_params * 5;
807 if (freq != ds_freq) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700808 p2p_dbg(p2p, "Update Listen frequency based on DS Parameter Set IE: %d -> %d MHz",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700809 freq, ds_freq);
810 freq = ds_freq;
811 }
812 }
813
Dmitry Shmidt04949592012-07-19 12:16:46 -0700814 if (dev->listen_freq && dev->listen_freq != freq && scan_res) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700815 p2p_dbg(p2p, "Update Listen frequency based on scan results ("
816 MACSTR " %d -> %d MHz (DS param %d)",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700817 MAC2STR(dev->info.p2p_device_addr), dev->listen_freq,
818 freq, msg.ds_params ? *msg.ds_params : -1);
819 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700820 if (scan_res) {
821 dev->listen_freq = freq;
822 if (msg.group_info)
823 dev->oper_freq = freq;
824 }
Jouni Malinen75ecf522011-06-27 15:19:46 -0700825 dev->info.level = level;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700826
Dmitry Shmidt29333592017-01-09 12:27:11 -0800827 dev_name_changed = os_strncmp(dev->info.device_name, msg.device_name,
828 WPS_DEV_NAME_MAX_LEN) != 0;
829
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700830 p2p_copy_wps_info(p2p, dev, 0, &msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700831
832 for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) {
833 wpabuf_free(dev->info.wps_vendor_ext[i]);
834 dev->info.wps_vendor_ext[i] = NULL;
835 }
836
837 for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) {
838 if (msg.wps_vendor_ext[i] == NULL)
839 break;
840 dev->info.wps_vendor_ext[i] = wpabuf_alloc_copy(
841 msg.wps_vendor_ext[i], msg.wps_vendor_ext_len[i]);
842 if (dev->info.wps_vendor_ext[i] == NULL)
843 break;
844 }
845
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800846 wfd_changed = p2p_compare_wfd_info(dev, &msg);
847
Dmitry Shmidt29333592017-01-09 12:27:11 -0800848 if (wfd_changed) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700849 wpabuf_free(dev->info.wfd_subelems);
Dmitry Shmidt29333592017-01-09 12:27:11 -0800850 if (msg.wfd_subelems)
851 dev->info.wfd_subelems = wpabuf_dup(msg.wfd_subelems);
852 else
853 dev->info.wfd_subelems = NULL;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700854 }
855
Dmitry Shmidt04949592012-07-19 12:16:46 -0700856 if (scan_res) {
857 p2p_add_group_clients(p2p, p2p_dev_addr, addr, freq,
Dmitry Shmidta3dc3092015-06-23 11:21:28 -0700858 msg.group_info, msg.group_info_len,
859 rx_time);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700860 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700861
862 p2p_parse_free(&msg);
863
Dmitry Shmidt2e67f062014-07-16 09:55:28 -0700864 p2p_update_peer_vendor_elems(dev, ies, ies_len);
865
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800866 if (dev->flags & P2P_DEV_REPORTED && !wfd_changed &&
Dmitry Shmidt29333592017-01-09 12:27:11 -0800867 !dev_name_changed &&
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800868 (!msg.adv_service_instance ||
869 (dev->flags & P2P_DEV_P2PS_REPORTED)))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700870 return 0;
871
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700872 p2p_dbg(p2p, "Peer found with Listen frequency %d MHz (rx_time=%u.%06u)",
873 freq, (unsigned int) rx_time->sec,
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800874 (unsigned int) rx_time->usec);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700875 if (dev->flags & P2P_DEV_USER_REJECTED) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700876 p2p_dbg(p2p, "Do not report rejected device");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700877 return 0;
878 }
879
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800880 if (dev->info.config_methods == 0 &&
881 (freq == 2412 || freq == 2437 || freq == 2462)) {
882 /*
883 * If we have only seen a Beacon frame from a GO, we do not yet
884 * know what WPS config methods it supports. Since some
885 * applications use config_methods value from P2P-DEVICE-FOUND
886 * events, postpone reporting this peer until we've fully
887 * discovered its capabilities.
888 *
889 * At least for now, do this only if the peer was detected on
890 * one of the social channels since that peer can be easily be
891 * found again and there are no limitations of having to use
892 * passive scan on this channels, so this can be done through
893 * Probe Response frame that includes the config_methods
894 * information.
895 */
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700896 p2p_dbg(p2p, "Do not report peer " MACSTR
897 " with unknown config methods", MAC2STR(addr));
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800898 return 0;
899 }
900
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700901 p2p->cfg->dev_found(p2p->cfg->cb_ctx, addr, &dev->info,
902 !(dev->flags & P2P_DEV_REPORTED_ONCE));
903 dev->flags |= P2P_DEV_REPORTED | P2P_DEV_REPORTED_ONCE;
904
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800905 if (msg.adv_service_instance)
906 dev->flags |= P2P_DEV_P2PS_REPORTED;
907
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700908 return 0;
909}
910
911
912static void p2p_device_free(struct p2p_data *p2p, struct p2p_device *dev)
913{
914 int i;
915
Dmitry Shmidt497c1d52011-07-21 15:19:46 -0700916 if (p2p->go_neg_peer == dev) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800917 /*
918 * If GO Negotiation is in progress, report that it has failed.
919 */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800920 p2p_go_neg_failed(p2p, -1);
Dmitry Shmidt497c1d52011-07-21 15:19:46 -0700921 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700922 if (p2p->invite_peer == dev)
923 p2p->invite_peer = NULL;
924 if (p2p->sd_peer == dev)
925 p2p->sd_peer = NULL;
926 if (p2p->pending_client_disc_go == dev)
927 p2p->pending_client_disc_go = NULL;
928
Dmitry Shmidtc55524a2011-07-07 11:18:38 -0700929 /* dev_lost() device, but only if it was previously dev_found() */
930 if (dev->flags & P2P_DEV_REPORTED_ONCE)
931 p2p->cfg->dev_lost(p2p->cfg->cb_ctx,
932 dev->info.p2p_device_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700933
934 for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) {
935 wpabuf_free(dev->info.wps_vendor_ext[i]);
936 dev->info.wps_vendor_ext[i] = NULL;
937 }
938
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700939 wpabuf_free(dev->info.wfd_subelems);
Dmitry Shmidt2e67f062014-07-16 09:55:28 -0700940 wpabuf_free(dev->info.vendor_elems);
Dmitry Shmidt413dde72014-04-11 10:23:22 -0700941 wpabuf_free(dev->go_neg_conf);
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800942 wpabuf_free(dev->info.p2ps_instance);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700943
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700944 os_free(dev);
945}
946
947
948static int p2p_get_next_prog_freq(struct p2p_data *p2p)
949{
950 struct p2p_channels *c;
951 struct p2p_reg_class *cla;
952 size_t cl, ch;
953 int found = 0;
954 u8 reg_class;
955 u8 channel;
956 int freq;
957
958 c = &p2p->cfg->channels;
959 for (cl = 0; cl < c->reg_classes; cl++) {
960 cla = &c->reg_class[cl];
961 if (cla->reg_class != p2p->last_prog_scan_class)
962 continue;
963 for (ch = 0; ch < cla->channels; ch++) {
964 if (cla->channel[ch] == p2p->last_prog_scan_chan) {
965 found = 1;
966 break;
967 }
968 }
969 if (found)
970 break;
971 }
972
973 if (!found) {
974 /* Start from beginning */
975 reg_class = c->reg_class[0].reg_class;
976 channel = c->reg_class[0].channel[0];
977 } else {
978 /* Pick the next channel */
979 ch++;
980 if (ch == cla->channels) {
981 cl++;
982 if (cl == c->reg_classes)
983 cl = 0;
984 ch = 0;
985 }
986 reg_class = c->reg_class[cl].reg_class;
987 channel = c->reg_class[cl].channel[ch];
988 }
989
Dmitry Shmidt4b060592013-04-29 16:42:49 -0700990 freq = p2p_channel_to_freq(reg_class, channel);
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700991 p2p_dbg(p2p, "Next progressive search channel: reg_class %u channel %u -> %d MHz",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700992 reg_class, channel, freq);
993 p2p->last_prog_scan_class = reg_class;
994 p2p->last_prog_scan_chan = channel;
995
996 if (freq == 2412 || freq == 2437 || freq == 2462)
997 return 0; /* No need to add social channels */
998 return freq;
999}
1000
1001
1002static void p2p_search(struct p2p_data *p2p)
1003{
1004 int freq = 0;
1005 enum p2p_scan_type type;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001006 u16 pw_id = DEV_PW_DEFAULT;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001007 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001008
1009 if (p2p->drv_in_listen) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001010 p2p_dbg(p2p, "Driver is still in Listen state - wait for it to end before continuing");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001011 return;
1012 }
1013 p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
1014
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001015 if (p2p->find_pending_full &&
1016 (p2p->find_type == P2P_FIND_PROGRESSIVE ||
1017 p2p->find_type == P2P_FIND_START_WITH_FULL)) {
1018 type = P2P_SCAN_FULL;
1019 p2p_dbg(p2p, "Starting search (pending full scan)");
1020 p2p->find_pending_full = 0;
1021 } else if ((p2p->find_type == P2P_FIND_PROGRESSIVE &&
1022 (freq = p2p_get_next_prog_freq(p2p)) > 0) ||
1023 (p2p->find_type == P2P_FIND_START_WITH_FULL &&
1024 (freq = p2p->find_specified_freq) > 0)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001025 type = P2P_SCAN_SOCIAL_PLUS_ONE;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001026 p2p_dbg(p2p, "Starting search (+ freq %u)", freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001027 } else {
1028 type = P2P_SCAN_SOCIAL;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001029 p2p_dbg(p2p, "Starting search");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001030 }
1031
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001032 res = p2p->cfg->p2p_scan(p2p->cfg->cb_ctx, type, freq,
1033 p2p->num_req_dev_types, p2p->req_dev_types,
1034 p2p->find_dev_id, pw_id);
1035 if (res < 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001036 p2p_dbg(p2p, "Scan request schedule failed");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001037 p2p_continue_find(p2p);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001038 }
1039}
1040
1041
1042static void p2p_find_timeout(void *eloop_ctx, void *timeout_ctx)
1043{
1044 struct p2p_data *p2p = eloop_ctx;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001045 p2p_dbg(p2p, "Find timeout -> stop");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001046 p2p_stop_find(p2p);
1047}
1048
1049
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001050void p2p_notify_scan_trigger_status(struct p2p_data *p2p, int status)
1051{
1052 if (status != 0) {
1053 p2p_dbg(p2p, "Scan request failed");
1054 /* Do continue find even for the first p2p_find_scan */
1055 p2p_continue_find(p2p);
1056 } else {
1057 p2p_dbg(p2p, "Running p2p_scan");
1058 p2p->p2p_scan_running = 1;
1059 eloop_cancel_timeout(p2p_scan_timeout, p2p, NULL);
1060 eloop_register_timeout(P2P_SCAN_TIMEOUT, 0, p2p_scan_timeout,
1061 p2p, NULL);
1062 }
1063}
1064
1065
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001066static int p2p_run_after_scan(struct p2p_data *p2p)
1067{
1068 struct p2p_device *dev;
1069 enum p2p_after_scan op;
1070
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001071 op = p2p->start_after_scan;
1072 p2p->start_after_scan = P2P_AFTER_SCAN_NOTHING;
1073 switch (op) {
1074 case P2P_AFTER_SCAN_NOTHING:
1075 break;
1076 case P2P_AFTER_SCAN_LISTEN:
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001077 p2p_dbg(p2p, "Start previously requested Listen state");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001078 p2p_listen(p2p, p2p->pending_listen_sec * 1000 +
1079 p2p->pending_listen_usec / 1000);
1080 return 1;
1081 case P2P_AFTER_SCAN_CONNECT:
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001082 p2p_dbg(p2p, "Start previously requested connect with " MACSTR,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001083 MAC2STR(p2p->after_scan_peer));
1084 dev = p2p_get_device(p2p, p2p->after_scan_peer);
1085 if (dev == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001086 p2p_dbg(p2p, "Peer not known anymore");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001087 break;
1088 }
1089 p2p_connect_send(p2p, dev);
1090 return 1;
1091 }
1092
1093 return 0;
1094}
1095
1096
1097static void p2p_scan_timeout(void *eloop_ctx, void *timeout_ctx)
1098{
1099 struct p2p_data *p2p = eloop_ctx;
1100 int running;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001101 p2p_dbg(p2p, "p2p_scan timeout (running=%d)", p2p->p2p_scan_running);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001102 running = p2p->p2p_scan_running;
1103 /* Make sure we recover from missed scan results callback */
1104 p2p->p2p_scan_running = 0;
1105
1106 if (running)
1107 p2p_run_after_scan(p2p);
1108}
1109
1110
1111static void p2p_free_req_dev_types(struct p2p_data *p2p)
1112{
1113 p2p->num_req_dev_types = 0;
1114 os_free(p2p->req_dev_types);
1115 p2p->req_dev_types = NULL;
1116}
1117
1118
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001119static int p2ps_gen_hash(struct p2p_data *p2p, const char *str, u8 *hash)
1120{
1121 u8 buf[SHA256_MAC_LEN];
1122 char str_buf[256];
1123 const u8 *adv_array;
1124 size_t i, adv_len;
1125
1126 if (!str || !hash)
1127 return 0;
1128
1129 if (!str[0]) {
1130 os_memcpy(hash, p2p->wild_card_hash, P2PS_HASH_LEN);
1131 return 1;
1132 }
1133
1134 adv_array = (u8 *) str_buf;
1135 adv_len = os_strlen(str);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07001136 if (adv_len >= sizeof(str_buf))
1137 return 0;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001138
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001139 for (i = 0; i < adv_len; i++) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001140 if (str[i] >= 'A' && str[i] <= 'Z')
1141 str_buf[i] = str[i] - 'A' + 'a';
1142 else
1143 str_buf[i] = str[i];
1144 }
1145
1146 if (sha256_vector(1, &adv_array, &adv_len, buf))
1147 return 0;
1148
1149 os_memcpy(hash, buf, P2PS_HASH_LEN);
1150 return 1;
1151}
1152
1153
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001154int p2p_find(struct p2p_data *p2p, unsigned int timeout,
1155 enum p2p_discovery_type type,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001156 unsigned int num_req_dev_types, const u8 *req_dev_types,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001157 const u8 *dev_id, unsigned int search_delay,
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001158 u8 seek_count, const char **seek, int freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001159{
1160 int res;
Hai Shalom74f70d42019-02-11 14:42:39 -08001161 struct os_reltime start;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001162
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001163 p2p_dbg(p2p, "Starting find (type=%d)", type);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001164 if (p2p->p2p_scan_running) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001165 p2p_dbg(p2p, "p2p_scan is already running");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001166 }
1167
1168 p2p_free_req_dev_types(p2p);
1169 if (req_dev_types && num_req_dev_types) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001170 p2p->req_dev_types = os_memdup(req_dev_types,
1171 num_req_dev_types *
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001172 WPS_DEV_TYPE_LEN);
1173 if (p2p->req_dev_types == NULL)
1174 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001175 p2p->num_req_dev_types = num_req_dev_types;
1176 }
1177
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001178 if (dev_id) {
1179 os_memcpy(p2p->find_dev_id_buf, dev_id, ETH_ALEN);
1180 p2p->find_dev_id = p2p->find_dev_id_buf;
1181 } else
1182 p2p->find_dev_id = NULL;
1183
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001184 if (seek_count == 0 || !seek) {
1185 /* Not an ASP search */
1186 p2p->p2ps_seek = 0;
1187 } else if (seek_count == 1 && seek && (!seek[0] || !seek[0][0])) {
1188 /*
1189 * An empty seek string means no hash values, but still an ASP
1190 * search.
1191 */
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07001192 p2p_dbg(p2p, "ASP search");
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001193 p2p->p2ps_seek_count = 0;
1194 p2p->p2ps_seek = 1;
1195 } else if (seek && seek_count <= P2P_MAX_QUERY_HASH) {
1196 u8 buf[P2PS_HASH_LEN];
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07001197 int i, count = 0;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001198
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001199 for (i = 0; i < seek_count; i++) {
1200 if (!p2ps_gen_hash(p2p, seek[i], buf))
1201 continue;
1202
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07001203 p2p_dbg(p2p, "Seek service %s hash " MACSTR,
1204 seek[i], MAC2STR(buf));
1205 os_memcpy(&p2p->p2ps_seek_hash[count * P2PS_HASH_LEN],
1206 buf, P2PS_HASH_LEN);
1207 count++;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001208 }
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07001209
1210 p2p->p2ps_seek_count = count;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001211 p2p->p2ps_seek = 1;
1212 } else {
1213 p2p->p2ps_seek_count = 0;
1214 p2p->p2ps_seek = 1;
1215 }
1216
1217 /* Special case to perform wildcard search */
1218 if (p2p->p2ps_seek_count == 0 && p2p->p2ps_seek) {
1219 p2p->p2ps_seek_count = 1;
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07001220 os_memcpy(&p2p->p2ps_seek_hash, p2p->wild_card_hash,
1221 P2PS_HASH_LEN);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001222 }
1223
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001224 p2p->start_after_scan = P2P_AFTER_SCAN_NOTHING;
1225 p2p_clear_timeout(p2p);
Dmitry Shmidtde47be72016-01-07 12:52:55 -08001226 if (p2p->pending_listen_freq) {
1227 p2p_dbg(p2p, "Clear pending_listen_freq for p2p_find");
1228 p2p->pending_listen_freq = 0;
1229 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001230 p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001231 p2p->find_pending_full = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001232 p2p->find_type = type;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001233 if (freq != 2412 && freq != 2437 && freq != 2462 && freq != 60480)
1234 p2p->find_specified_freq = freq;
1235 else
1236 p2p->find_specified_freq = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001237 p2p_device_clear_reported(p2p);
Dmitry Shmidtde47be72016-01-07 12:52:55 -08001238 os_memset(p2p->sd_query_no_ack, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001239 p2p_set_state(p2p, P2P_SEARCH);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001240 p2p->search_delay = search_delay;
1241 p2p->in_search_delay = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001242 eloop_cancel_timeout(p2p_find_timeout, p2p, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001243 p2p->last_p2p_find_timeout = timeout;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001244 if (timeout)
1245 eloop_register_timeout(timeout, 0, p2p_find_timeout,
1246 p2p, NULL);
Hai Shalom74f70d42019-02-11 14:42:39 -08001247 os_get_reltime(&start);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001248 switch (type) {
1249 case P2P_FIND_START_WITH_FULL:
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001250 if (freq > 0) {
1251 /*
1252 * Start with the specified channel and then move to
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001253 * scans for social channels and this specific channel.
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001254 */
1255 res = p2p->cfg->p2p_scan(p2p->cfg->cb_ctx,
1256 P2P_SCAN_SPECIFIC, freq,
1257 p2p->num_req_dev_types,
1258 p2p->req_dev_types, dev_id,
1259 DEV_PW_DEFAULT);
1260 break;
1261 }
1262 /* fall through */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001263 case P2P_FIND_PROGRESSIVE:
1264 res = p2p->cfg->p2p_scan(p2p->cfg->cb_ctx, P2P_SCAN_FULL, 0,
1265 p2p->num_req_dev_types,
Dmitry Shmidt04949592012-07-19 12:16:46 -07001266 p2p->req_dev_types, dev_id,
1267 DEV_PW_DEFAULT);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001268 break;
1269 case P2P_FIND_ONLY_SOCIAL:
1270 res = p2p->cfg->p2p_scan(p2p->cfg->cb_ctx, P2P_SCAN_SOCIAL, 0,
1271 p2p->num_req_dev_types,
Dmitry Shmidt04949592012-07-19 12:16:46 -07001272 p2p->req_dev_types, dev_id,
1273 DEV_PW_DEFAULT);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001274 break;
1275 default:
1276 return -1;
1277 }
1278
Hai Shalom74f70d42019-02-11 14:42:39 -08001279 if (!res)
1280 p2p->find_start = start;
1281
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001282 if (res != 0 && p2p->p2p_scan_running) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001283 p2p_dbg(p2p, "Failed to start p2p_scan - another p2p_scan was already running");
1284 /* wait for the previous p2p_scan to complete */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001285 if (type == P2P_FIND_PROGRESSIVE ||
1286 (type == P2P_FIND_START_WITH_FULL && freq == 0))
1287 p2p->find_pending_full = 1;
Dmitry Shmidt96be6222014-02-13 10:16:51 -08001288 res = 0; /* do not report failure */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001289 } else if (res != 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001290 p2p_dbg(p2p, "Failed to start p2p_scan");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001291 p2p_set_state(p2p, P2P_IDLE);
1292 eloop_cancel_timeout(p2p_find_timeout, p2p, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001293 }
1294
1295 return res;
1296}
1297
1298
1299void p2p_stop_find_for_freq(struct p2p_data *p2p, int freq)
1300{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001301 p2p_dbg(p2p, "Stopping find");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001302 eloop_cancel_timeout(p2p_find_timeout, p2p, NULL);
1303 p2p_clear_timeout(p2p);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001304 if (p2p->state == P2P_SEARCH || p2p->state == P2P_SD_DURING_FIND)
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001305 p2p->cfg->find_stopped(p2p->cfg->cb_ctx);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001306
1307 p2p->p2ps_seek_count = 0;
1308
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001309 p2p_set_state(p2p, P2P_IDLE);
1310 p2p_free_req_dev_types(p2p);
1311 p2p->start_after_scan = P2P_AFTER_SCAN_NOTHING;
Dmitry Shmidt8c652892013-03-01 10:14:01 -08001312 if (p2p->go_neg_peer)
1313 p2p->go_neg_peer->flags &= ~P2P_DEV_PEER_WAITING_RESPONSE;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001314 p2p->go_neg_peer = NULL;
1315 p2p->sd_peer = NULL;
1316 p2p->invite_peer = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001317 p2p_stop_listen_for_freq(p2p, freq);
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001318 p2p->send_action_in_progress = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001319}
1320
1321
1322void p2p_stop_listen_for_freq(struct p2p_data *p2p, int freq)
1323{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001324 if (freq > 0 && p2p->drv_in_listen == freq && p2p->in_listen) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001325 p2p_dbg(p2p, "Skip stop_listen since we are on correct channel for response");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001326 return;
1327 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001328 if (p2p->in_listen) {
1329 p2p->in_listen = 0;
1330 p2p_clear_timeout(p2p);
1331 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001332 if (p2p->drv_in_listen) {
1333 /*
1334 * The driver may not deliver callback to p2p_listen_end()
1335 * when the operation gets canceled, so clear the internal
1336 * variable that is tracking driver state.
1337 */
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001338 p2p_dbg(p2p, "Clear drv_in_listen (%d)", p2p->drv_in_listen);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001339 p2p->drv_in_listen = 0;
1340 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001341 p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
1342}
1343
1344
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001345void p2p_stop_listen(struct p2p_data *p2p)
1346{
1347 if (p2p->state != P2P_LISTEN_ONLY) {
1348 p2p_dbg(p2p, "Skip stop_listen since not in listen_only state.");
1349 return;
1350 }
1351
1352 p2p_stop_listen_for_freq(p2p, 0);
1353 p2p_set_state(p2p, P2P_IDLE);
1354}
1355
1356
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001357void p2p_stop_find(struct p2p_data *p2p)
1358{
Dmitry Shmidt3c57b3f2014-05-22 15:13:07 -07001359 p2p->pending_listen_freq = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001360 p2p_stop_find_for_freq(p2p, 0);
1361}
1362
1363
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001364static int p2p_prepare_channel_pref(struct p2p_data *p2p,
1365 unsigned int force_freq,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001366 unsigned int pref_freq, int go)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001367{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001368 u8 op_class, op_channel;
1369 unsigned int freq = force_freq ? force_freq : pref_freq;
1370
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001371 p2p_dbg(p2p, "Prepare channel pref - force_freq=%u pref_freq=%u go=%d",
1372 force_freq, pref_freq, go);
Dmitry Shmidt4b060592013-04-29 16:42:49 -07001373 if (p2p_freq_to_channel(freq, &op_class, &op_channel) < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001374 p2p_dbg(p2p, "Unsupported frequency %u MHz", freq);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001375 return -1;
1376 }
1377
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001378 if (!p2p_channels_includes(&p2p->cfg->channels, op_class, op_channel) &&
1379 (go || !p2p_channels_includes(&p2p->cfg->cli_channels, op_class,
1380 op_channel))) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001381 p2p_dbg(p2p, "Frequency %u MHz (oper_class %u channel %u) not allowed for P2P",
1382 freq, op_class, op_channel);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001383 return -1;
1384 }
1385
1386 p2p->op_reg_class = op_class;
1387 p2p->op_channel = op_channel;
1388
1389 if (force_freq) {
1390 p2p->channels.reg_classes = 1;
1391 p2p->channels.reg_class[0].channels = 1;
1392 p2p->channels.reg_class[0].reg_class = p2p->op_reg_class;
1393 p2p->channels.reg_class[0].channel[0] = p2p->op_channel;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001394 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001395 os_memcpy(&p2p->channels, &p2p->cfg->channels,
1396 sizeof(struct p2p_channels));
1397 }
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001398
1399 return 0;
1400}
1401
1402
1403static void p2p_prepare_channel_best(struct p2p_data *p2p)
1404{
1405 u8 op_class, op_channel;
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07001406 const int op_classes_5ghz[] = { 124, 125, 115, 0 };
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08001407 const int op_classes_ht40[] = { 126, 127, 116, 117, 0 };
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001408 const int op_classes_vht[] = { 128, 0 };
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001409 const int op_classes_edmg[] = { 181, 182, 183, 0 };
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001410
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07001411 p2p_dbg(p2p, "Prepare channel best");
1412
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001413 if (!p2p->cfg->cfg_op_channel && p2p->best_freq_overall > 0 &&
1414 p2p_supported_freq(p2p, p2p->best_freq_overall) &&
Dmitry Shmidt4b060592013-04-29 16:42:49 -07001415 p2p_freq_to_channel(p2p->best_freq_overall, &op_class, &op_channel)
1416 == 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001417 p2p_dbg(p2p, "Select best overall channel as operating channel preference");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001418 p2p->op_reg_class = op_class;
1419 p2p->op_channel = op_channel;
1420 } else if (!p2p->cfg->cfg_op_channel && p2p->best_freq_5 > 0 &&
1421 p2p_supported_freq(p2p, p2p->best_freq_5) &&
Dmitry Shmidt4b060592013-04-29 16:42:49 -07001422 p2p_freq_to_channel(p2p->best_freq_5, &op_class, &op_channel)
1423 == 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001424 p2p_dbg(p2p, "Select best 5 GHz channel as operating channel preference");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001425 p2p->op_reg_class = op_class;
1426 p2p->op_channel = op_channel;
1427 } else if (!p2p->cfg->cfg_op_channel && p2p->best_freq_24 > 0 &&
1428 p2p_supported_freq(p2p, p2p->best_freq_24) &&
Dmitry Shmidt4b060592013-04-29 16:42:49 -07001429 p2p_freq_to_channel(p2p->best_freq_24, &op_class,
1430 &op_channel) == 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001431 p2p_dbg(p2p, "Select best 2.4 GHz channel as operating channel preference");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001432 p2p->op_reg_class = op_class;
1433 p2p->op_channel = op_channel;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07001434 } else if (p2p->cfg->num_pref_chan > 0 &&
1435 p2p_channels_includes(&p2p->cfg->channels,
1436 p2p->cfg->pref_chan[0].op_class,
1437 p2p->cfg->pref_chan[0].chan)) {
1438 p2p_dbg(p2p, "Select first pref_chan entry as operating channel preference");
1439 p2p->op_reg_class = p2p->cfg->pref_chan[0].op_class;
1440 p2p->op_channel = p2p->cfg->pref_chan[0].chan;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001441 } else if (p2p_channel_select(&p2p->cfg->channels, op_classes_edmg,
1442 &p2p->op_reg_class, &p2p->op_channel) ==
1443 0) {
1444 p2p_dbg(p2p, "Select possible EDMG channel (op_class %u channel %u) as operating channel preference",
1445 p2p->op_reg_class, p2p->op_channel);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001446 } else if (p2p_channel_select(&p2p->cfg->channels, op_classes_vht,
1447 &p2p->op_reg_class, &p2p->op_channel) ==
1448 0) {
1449 p2p_dbg(p2p, "Select possible VHT channel (op_class %u channel %u) as operating channel preference",
1450 p2p->op_reg_class, p2p->op_channel);
1451 } else if (p2p_channel_select(&p2p->cfg->channels, op_classes_ht40,
1452 &p2p->op_reg_class, &p2p->op_channel) ==
1453 0) {
1454 p2p_dbg(p2p, "Select possible HT40 channel (op_class %u channel %u) as operating channel preference",
1455 p2p->op_reg_class, p2p->op_channel);
1456 } else if (p2p_channel_select(&p2p->cfg->channels, op_classes_5ghz,
1457 &p2p->op_reg_class, &p2p->op_channel) ==
1458 0) {
1459 p2p_dbg(p2p, "Select possible 5 GHz channel (op_class %u channel %u) as operating channel preference",
1460 p2p->op_reg_class, p2p->op_channel);
Dmitry Shmidtd11f0192014-03-24 12:09:47 -07001461 } else if (p2p_channels_includes(&p2p->cfg->channels,
1462 p2p->cfg->op_reg_class,
1463 p2p->cfg->op_channel)) {
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07001464 p2p_dbg(p2p, "Select pre-configured channel as operating channel preference");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001465 p2p->op_reg_class = p2p->cfg->op_reg_class;
1466 p2p->op_channel = p2p->cfg->op_channel;
Dmitry Shmidtd11f0192014-03-24 12:09:47 -07001467 } else if (p2p_channel_random_social(&p2p->cfg->channels,
1468 &p2p->op_reg_class,
Hai Shalom74f70d42019-02-11 14:42:39 -08001469 &p2p->op_channel,
1470 NULL, NULL) == 0) {
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001471 p2p_dbg(p2p, "Select random available social channel (op_class %u channel %u) as operating channel preference",
1472 p2p->op_reg_class, p2p->op_channel);
Dmitry Shmidtd11f0192014-03-24 12:09:47 -07001473 } else {
1474 /* Select any random available channel from the first available
1475 * operating class */
1476 p2p_channel_select(&p2p->cfg->channels, NULL,
1477 &p2p->op_reg_class,
1478 &p2p->op_channel);
1479 p2p_dbg(p2p, "Select random available channel %d from operating class %d as operating channel preference",
1480 p2p->op_channel, p2p->op_reg_class);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001481 }
1482
1483 os_memcpy(&p2p->channels, &p2p->cfg->channels,
1484 sizeof(struct p2p_channels));
1485}
1486
1487
1488/**
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001489 * p2p_prepare_channel - Select operating channel for GO Negotiation or P2PS PD
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001490 * @p2p: P2P module context from p2p_init()
1491 * @dev: Selected peer device
1492 * @force_freq: Forced frequency in MHz or 0 if not forced
1493 * @pref_freq: Preferred frequency in MHz or 0 if no preference
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001494 * @go: Whether the local end will be forced to be GO
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001495 * Returns: 0 on success, -1 on failure (channel not supported for P2P)
1496 *
1497 * This function is used to do initial operating channel selection for GO
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001498 * Negotiation prior to having received peer information or for P2PS PD
1499 * signalling. The selected channel may be further optimized in
1500 * p2p_reselect_channel() once the peer information is available.
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001501 */
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08001502int p2p_prepare_channel(struct p2p_data *p2p, struct p2p_device *dev,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001503 unsigned int force_freq, unsigned int pref_freq, int go)
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001504{
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001505 p2p_dbg(p2p, "Prepare channel - force_freq=%u pref_freq=%u go=%d",
1506 force_freq, pref_freq, go);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001507 if (force_freq || pref_freq) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001508 if (p2p_prepare_channel_pref(p2p, force_freq, pref_freq, go) <
1509 0)
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001510 return -1;
1511 } else {
1512 p2p_prepare_channel_best(p2p);
1513 }
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001514 p2p_channels_dump(p2p, "prepared channels", &p2p->channels);
1515 if (go)
1516 p2p_channels_remove_freqs(&p2p->channels, &p2p->no_go_freq);
1517 else if (!force_freq)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001518 p2p_channels_union_inplace(&p2p->channels,
1519 &p2p->cfg->cli_channels);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001520 p2p_channels_dump(p2p, "after go/cli filter/add", &p2p->channels);
1521
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001522 p2p_dbg(p2p, "Own preference for operation channel: Operating Class %u Channel %u%s",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001523 p2p->op_reg_class, p2p->op_channel,
1524 force_freq ? " (forced)" : "");
1525
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001526 if (force_freq)
1527 dev->flags |= P2P_DEV_FORCE_FREQ;
1528 else
1529 dev->flags &= ~P2P_DEV_FORCE_FREQ;
1530
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001531 return 0;
1532}
1533
1534
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001535static void p2p_set_dev_persistent(struct p2p_device *dev,
1536 int persistent_group)
1537{
1538 switch (persistent_group) {
1539 case 0:
1540 dev->flags &= ~(P2P_DEV_PREFER_PERSISTENT_GROUP |
1541 P2P_DEV_PREFER_PERSISTENT_RECONN);
1542 break;
1543 case 1:
1544 dev->flags |= P2P_DEV_PREFER_PERSISTENT_GROUP;
1545 dev->flags &= ~P2P_DEV_PREFER_PERSISTENT_RECONN;
1546 break;
1547 case 2:
1548 dev->flags |= P2P_DEV_PREFER_PERSISTENT_GROUP |
1549 P2P_DEV_PREFER_PERSISTENT_RECONN;
1550 break;
1551 }
1552}
1553
1554
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001555int p2p_connect(struct p2p_data *p2p, const u8 *peer_addr,
1556 enum p2p_wps_method wps_method,
1557 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07001558 unsigned int force_freq, int persistent_group,
1559 const u8 *force_ssid, size_t force_ssid_len,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001560 int pd_before_go_neg, unsigned int pref_freq, u16 oob_pw_id)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001561{
1562 struct p2p_device *dev;
1563
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001564 p2p_dbg(p2p, "Request to start group negotiation - peer=" MACSTR
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001565 " GO Intent=%d Intended Interface Address=" MACSTR
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001566 " wps_method=%d persistent_group=%d pd_before_go_neg=%d "
1567 "oob_pw_id=%u",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001568 MAC2STR(peer_addr), go_intent, MAC2STR(own_interface_addr),
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001569 wps_method, persistent_group, pd_before_go_neg, oob_pw_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001570
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001571 dev = p2p_get_device(p2p, peer_addr);
1572 if (dev == NULL || (dev->flags & P2P_DEV_PROBE_REQ_ONLY)) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001573 p2p_dbg(p2p, "Cannot connect to unknown P2P Device " MACSTR,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001574 MAC2STR(peer_addr));
1575 return -1;
1576 }
1577
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001578 if (p2p_prepare_channel(p2p, dev, force_freq, pref_freq,
1579 go_intent == 15) < 0)
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001580 return -1;
1581
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001582 if (dev->flags & P2P_DEV_GROUP_CLIENT_ONLY) {
1583 if (!(dev->info.dev_capab &
1584 P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY)) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001585 p2p_dbg(p2p, "Cannot connect to P2P Device " MACSTR
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001586 " that is in a group and is not discoverable",
1587 MAC2STR(peer_addr));
1588 return -1;
1589 }
1590 if (dev->oper_freq <= 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001591 p2p_dbg(p2p, "Cannot connect to P2P Device " MACSTR
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001592 " with incomplete information",
1593 MAC2STR(peer_addr));
1594 return -1;
1595 }
1596
1597 /*
1598 * First, try to connect directly. If the peer does not
1599 * acknowledge frames, assume it is sleeping and use device
1600 * discoverability via the GO at that point.
1601 */
1602 }
1603
Dmitry Shmidt04949592012-07-19 12:16:46 -07001604 p2p->ssid_set = 0;
1605 if (force_ssid) {
1606 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Forced SSID",
1607 force_ssid, force_ssid_len);
1608 os_memcpy(p2p->ssid, force_ssid, force_ssid_len);
1609 p2p->ssid_len = force_ssid_len;
1610 p2p->ssid_set = 1;
1611 }
1612
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001613 dev->flags &= ~P2P_DEV_NOT_YET_READY;
1614 dev->flags &= ~P2P_DEV_USER_REJECTED;
1615 dev->flags &= ~P2P_DEV_WAIT_GO_NEG_RESPONSE;
1616 dev->flags &= ~P2P_DEV_WAIT_GO_NEG_CONFIRM;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001617 if (pd_before_go_neg)
1618 dev->flags |= P2P_DEV_PD_BEFORE_GO_NEG;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001619 else {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001620 dev->flags &= ~P2P_DEV_PD_BEFORE_GO_NEG;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001621 /*
Dmitry Shmidt9cdf1b92013-02-27 12:58:50 -08001622 * Assign dialog token and tie breaker here to use the same
1623 * values in each retry within the same GO Negotiation exchange.
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001624 */
1625 dev->dialog_token++;
1626 if (dev->dialog_token == 0)
1627 dev->dialog_token = 1;
Dmitry Shmidt9cdf1b92013-02-27 12:58:50 -08001628 dev->tie_breaker = p2p->next_tie_breaker;
1629 p2p->next_tie_breaker = !p2p->next_tie_breaker;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001630 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001631 dev->connect_reqs = 0;
1632 dev->go_neg_req_sent = 0;
1633 dev->go_state = UNKNOWN_GO;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001634 p2p_set_dev_persistent(dev, persistent_group);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001635 p2p->go_intent = go_intent;
1636 os_memcpy(p2p->intended_addr, own_interface_addr, ETH_ALEN);
1637
1638 if (p2p->state != P2P_IDLE)
1639 p2p_stop_find(p2p);
1640
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001641 dev->wps_method = wps_method;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001642 dev->oob_pw_id = oob_pw_id;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001643 dev->status = P2P_SC_SUCCESS;
1644
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001645 if (p2p->p2p_scan_running) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001646 p2p_dbg(p2p, "p2p_scan running - delay connect send");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001647 p2p->start_after_scan = P2P_AFTER_SCAN_CONNECT;
1648 os_memcpy(p2p->after_scan_peer, peer_addr, ETH_ALEN);
1649 return 0;
1650 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001651
1652 return p2p_connect_send(p2p, dev);
1653}
1654
1655
1656int p2p_authorize(struct p2p_data *p2p, const u8 *peer_addr,
1657 enum p2p_wps_method wps_method,
1658 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07001659 unsigned int force_freq, int persistent_group,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001660 const u8 *force_ssid, size_t force_ssid_len,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001661 unsigned int pref_freq, u16 oob_pw_id)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001662{
1663 struct p2p_device *dev;
1664
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001665 p2p_dbg(p2p, "Request to authorize group negotiation - peer=" MACSTR
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001666 " GO Intent=%d Intended Interface Address=" MACSTR
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001667 " wps_method=%d persistent_group=%d oob_pw_id=%u",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001668 MAC2STR(peer_addr), go_intent, MAC2STR(own_interface_addr),
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001669 wps_method, persistent_group, oob_pw_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001670
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001671 dev = p2p_get_device(p2p, peer_addr);
1672 if (dev == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001673 p2p_dbg(p2p, "Cannot authorize unknown P2P Device " MACSTR,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001674 MAC2STR(peer_addr));
1675 return -1;
1676 }
1677
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001678 if (p2p_prepare_channel(p2p, dev, force_freq, pref_freq, go_intent ==
1679 15) < 0)
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001680 return -1;
1681
Dmitry Shmidt04949592012-07-19 12:16:46 -07001682 p2p->ssid_set = 0;
1683 if (force_ssid) {
1684 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Forced SSID",
1685 force_ssid, force_ssid_len);
1686 os_memcpy(p2p->ssid, force_ssid, force_ssid_len);
1687 p2p->ssid_len = force_ssid_len;
1688 p2p->ssid_set = 1;
1689 }
1690
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001691 dev->flags &= ~P2P_DEV_NOT_YET_READY;
1692 dev->flags &= ~P2P_DEV_USER_REJECTED;
1693 dev->go_neg_req_sent = 0;
1694 dev->go_state = UNKNOWN_GO;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001695 p2p_set_dev_persistent(dev, persistent_group);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001696 p2p->go_intent = go_intent;
1697 os_memcpy(p2p->intended_addr, own_interface_addr, ETH_ALEN);
1698
1699 dev->wps_method = wps_method;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001700 dev->oob_pw_id = oob_pw_id;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001701 dev->status = P2P_SC_SUCCESS;
1702
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001703 return 0;
1704}
1705
1706
1707void p2p_add_dev_info(struct p2p_data *p2p, const u8 *addr,
1708 struct p2p_device *dev, struct p2p_message *msg)
1709{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001710 os_get_reltime(&dev->last_seen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001711
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001712 p2p_copy_wps_info(p2p, dev, 0, msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001713
1714 if (msg->listen_channel) {
1715 int freq;
Dmitry Shmidt4b060592013-04-29 16:42:49 -07001716 freq = p2p_channel_to_freq(msg->listen_channel[3],
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001717 msg->listen_channel[4]);
1718 if (freq < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001719 p2p_dbg(p2p, "Unknown peer Listen channel: "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001720 "country=%c%c(0x%02x) reg_class=%u channel=%u",
1721 msg->listen_channel[0],
1722 msg->listen_channel[1],
1723 msg->listen_channel[2],
1724 msg->listen_channel[3],
1725 msg->listen_channel[4]);
1726 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001727 p2p_dbg(p2p, "Update peer " MACSTR
1728 " Listen channel: %u -> %u MHz",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001729 MAC2STR(dev->info.p2p_device_addr),
1730 dev->listen_freq, freq);
1731 dev->listen_freq = freq;
1732 }
1733 }
1734
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001735 if (msg->wfd_subelems) {
1736 wpabuf_free(dev->info.wfd_subelems);
1737 dev->info.wfd_subelems = wpabuf_dup(msg->wfd_subelems);
1738 }
1739
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001740 if (dev->flags & P2P_DEV_PROBE_REQ_ONLY) {
1741 dev->flags &= ~P2P_DEV_PROBE_REQ_ONLY;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001742 p2p_dbg(p2p, "Completed device entry based on data from GO Negotiation Request");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001743 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001744 p2p_dbg(p2p, "Created device entry based on GO Neg Req: "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001745 MACSTR " dev_capab=0x%x group_capab=0x%x name='%s' "
1746 "listen_freq=%d",
1747 MAC2STR(dev->info.p2p_device_addr),
1748 dev->info.dev_capab, dev->info.group_capab,
1749 dev->info.device_name, dev->listen_freq);
1750 }
1751
1752 dev->flags &= ~P2P_DEV_GROUP_CLIENT_ONLY;
1753
1754 if (dev->flags & P2P_DEV_USER_REJECTED) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001755 p2p_dbg(p2p, "Do not report rejected device");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001756 return;
1757 }
1758
1759 p2p->cfg->dev_found(p2p->cfg->cb_ctx, addr, &dev->info,
1760 !(dev->flags & P2P_DEV_REPORTED_ONCE));
1761 dev->flags |= P2P_DEV_REPORTED | P2P_DEV_REPORTED_ONCE;
1762}
1763
1764
1765void p2p_build_ssid(struct p2p_data *p2p, u8 *ssid, size_t *ssid_len)
1766{
1767 os_memcpy(ssid, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN);
1768 p2p_random((char *) &ssid[P2P_WILDCARD_SSID_LEN], 2);
1769 os_memcpy(&ssid[P2P_WILDCARD_SSID_LEN + 2],
1770 p2p->cfg->ssid_postfix, p2p->cfg->ssid_postfix_len);
1771 *ssid_len = P2P_WILDCARD_SSID_LEN + 2 + p2p->cfg->ssid_postfix_len;
1772}
1773
1774
1775int p2p_go_params(struct p2p_data *p2p, struct p2p_go_neg_results *params)
1776{
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001777 if (p2p->ssid_set) {
1778 os_memcpy(params->ssid, p2p->ssid, p2p->ssid_len);
1779 params->ssid_len = p2p->ssid_len;
1780 } else {
1781 p2p_build_ssid(p2p, params->ssid, &params->ssid_len);
1782 }
1783 p2p->ssid_set = 0;
1784
Jimmy Chen6d7e3902018-11-20 10:15:16 +08001785 if (p2p->passphrase_set) {
1786 os_memcpy(params->passphrase, p2p->passphrase, os_strlen(p2p->passphrase));
1787 } else {
1788 p2p_random(params->passphrase, p2p->cfg->passphrase_len);
1789 }
1790 p2p->passphrase_set = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001791 return 0;
1792}
1793
1794
1795void p2p_go_complete(struct p2p_data *p2p, struct p2p_device *peer)
1796{
1797 struct p2p_go_neg_results res;
1798 int go = peer->go_state == LOCAL_GO;
1799 struct p2p_channels intersection;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001800
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001801 p2p_dbg(p2p, "GO Negotiation with " MACSTR " completed (%s will be GO)",
1802 MAC2STR(peer->info.p2p_device_addr), go ? "local end" : "peer");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001803
1804 os_memset(&res, 0, sizeof(res));
1805 res.role_go = go;
1806 os_memcpy(res.peer_device_addr, peer->info.p2p_device_addr, ETH_ALEN);
1807 os_memcpy(res.peer_interface_addr, peer->intended_addr, ETH_ALEN);
1808 res.wps_method = peer->wps_method;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001809 if (peer->flags & P2P_DEV_PREFER_PERSISTENT_GROUP) {
1810 if (peer->flags & P2P_DEV_PREFER_PERSISTENT_RECONN)
1811 res.persistent_group = 2;
1812 else
1813 res.persistent_group = 1;
1814 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001815
1816 if (go) {
1817 /* Setup AP mode for WPS provisioning */
Dmitry Shmidt4b060592013-04-29 16:42:49 -07001818 res.freq = p2p_channel_to_freq(p2p->op_reg_class,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001819 p2p->op_channel);
1820 os_memcpy(res.ssid, p2p->ssid, p2p->ssid_len);
1821 res.ssid_len = p2p->ssid_len;
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07001822 p2p_random(res.passphrase, p2p->cfg->passphrase_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001823 } else {
1824 res.freq = peer->oper_freq;
1825 if (p2p->ssid_len) {
1826 os_memcpy(res.ssid, p2p->ssid, p2p->ssid_len);
1827 res.ssid_len = p2p->ssid_len;
1828 }
1829 }
1830
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001831 p2p_channels_dump(p2p, "own channels", &p2p->channels);
1832 p2p_channels_dump(p2p, "peer channels", &peer->channels);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001833 p2p_channels_intersect(&p2p->channels, &peer->channels,
1834 &intersection);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001835 if (go) {
1836 p2p_channels_remove_freqs(&intersection, &p2p->no_go_freq);
1837 p2p_channels_dump(p2p, "intersection after no-GO removal",
1838 &intersection);
1839 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001840
1841 p2p_channels_to_freqs(&intersection, res.freq_list,
1842 P2P_MAX_CHANNELS);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001843
1844 res.peer_config_timeout = go ? peer->client_timeout : peer->go_timeout;
1845
1846 p2p_clear_timeout(p2p);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001847 p2p->ssid_set = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001848 peer->go_neg_req_sent = 0;
Dmitry Shmidt29333592017-01-09 12:27:11 -08001849 peer->flags &= ~P2P_DEV_PEER_WAITING_RESPONSE;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001850 peer->wps_method = WPS_NOT_READY;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001851 peer->oob_pw_id = 0;
Dmitry Shmidt413dde72014-04-11 10:23:22 -07001852 wpabuf_free(peer->go_neg_conf);
1853 peer->go_neg_conf = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001854
1855 p2p_set_state(p2p, P2P_PROVISIONING);
1856 p2p->cfg->go_neg_completed(p2p->cfg->cb_ctx, &res);
1857}
1858
1859
1860static void p2p_rx_p2p_action(struct p2p_data *p2p, const u8 *sa,
1861 const u8 *data, size_t len, int rx_freq)
1862{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001863 p2p_dbg(p2p, "RX P2P Public Action from " MACSTR, MAC2STR(sa));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001864 wpa_hexdump(MSG_MSGDUMP, "P2P: P2P Public Action contents", data, len);
1865
1866 if (len < 1)
1867 return;
1868
1869 switch (data[0]) {
1870 case P2P_GO_NEG_REQ:
1871 p2p_process_go_neg_req(p2p, sa, data + 1, len - 1, rx_freq);
1872 break;
1873 case P2P_GO_NEG_RESP:
1874 p2p_process_go_neg_resp(p2p, sa, data + 1, len - 1, rx_freq);
1875 break;
1876 case P2P_GO_NEG_CONF:
1877 p2p_process_go_neg_conf(p2p, sa, data + 1, len - 1);
1878 break;
1879 case P2P_INVITATION_REQ:
1880 p2p_process_invitation_req(p2p, sa, data + 1, len - 1,
1881 rx_freq);
1882 break;
1883 case P2P_INVITATION_RESP:
1884 p2p_process_invitation_resp(p2p, sa, data + 1, len - 1);
1885 break;
1886 case P2P_PROV_DISC_REQ:
1887 p2p_process_prov_disc_req(p2p, sa, data + 1, len - 1, rx_freq);
1888 break;
1889 case P2P_PROV_DISC_RESP:
1890 p2p_process_prov_disc_resp(p2p, sa, data + 1, len - 1);
1891 break;
1892 case P2P_DEV_DISC_REQ:
1893 p2p_process_dev_disc_req(p2p, sa, data + 1, len - 1, rx_freq);
1894 break;
1895 case P2P_DEV_DISC_RESP:
1896 p2p_process_dev_disc_resp(p2p, sa, data + 1, len - 1);
1897 break;
1898 default:
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001899 p2p_dbg(p2p, "Unsupported P2P Public Action frame type %d",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001900 data[0]);
1901 break;
1902 }
1903}
1904
1905
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001906static void p2p_rx_action_public(struct p2p_data *p2p, const u8 *da,
1907 const u8 *sa, const u8 *bssid, const u8 *data,
1908 size_t len, int freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001909{
1910 if (len < 1)
1911 return;
1912
1913 switch (data[0]) {
1914 case WLAN_PA_VENDOR_SPECIFIC:
1915 data++;
1916 len--;
Dmitry Shmidta38abf92014-03-06 13:38:44 -08001917 if (len < 4)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001918 return;
Dmitry Shmidta38abf92014-03-06 13:38:44 -08001919 if (WPA_GET_BE32(data) != P2P_IE_VENDOR_TYPE)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001920 return;
1921
Dmitry Shmidta38abf92014-03-06 13:38:44 -08001922 data += 4;
1923 len -= 4;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001924
Dmitry Shmidta38abf92014-03-06 13:38:44 -08001925 p2p_rx_p2p_action(p2p, sa, data, len, freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001926 break;
1927 case WLAN_PA_GAS_INITIAL_REQ:
1928 p2p_rx_gas_initial_req(p2p, sa, data + 1, len - 1, freq);
1929 break;
1930 case WLAN_PA_GAS_INITIAL_RESP:
1931 p2p_rx_gas_initial_resp(p2p, sa, data + 1, len - 1, freq);
1932 break;
1933 case WLAN_PA_GAS_COMEBACK_REQ:
1934 p2p_rx_gas_comeback_req(p2p, sa, data + 1, len - 1, freq);
1935 break;
1936 case WLAN_PA_GAS_COMEBACK_RESP:
1937 p2p_rx_gas_comeback_resp(p2p, sa, data + 1, len - 1, freq);
1938 break;
1939 }
1940}
1941
1942
1943void p2p_rx_action(struct p2p_data *p2p, const u8 *da, const u8 *sa,
1944 const u8 *bssid, u8 category,
1945 const u8 *data, size_t len, int freq)
1946{
1947 if (category == WLAN_ACTION_PUBLIC) {
1948 p2p_rx_action_public(p2p, da, sa, bssid, data, len, freq);
1949 return;
1950 }
1951
1952 if (category != WLAN_ACTION_VENDOR_SPECIFIC)
1953 return;
1954
1955 if (len < 4)
1956 return;
1957
Dmitry Shmidta38abf92014-03-06 13:38:44 -08001958 if (WPA_GET_BE32(data) != P2P_IE_VENDOR_TYPE)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001959 return;
Dmitry Shmidta38abf92014-03-06 13:38:44 -08001960 data += 4;
1961 len -= 4;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001962
1963 /* P2P action frame */
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001964 p2p_dbg(p2p, "RX P2P Action from " MACSTR, MAC2STR(sa));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001965 wpa_hexdump(MSG_MSGDUMP, "P2P: P2P Action contents", data, len);
1966
1967 if (len < 1)
1968 return;
1969 switch (data[0]) {
1970 case P2P_NOA:
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001971 p2p_dbg(p2p, "Received P2P Action - Notice of Absence");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001972 /* TODO */
1973 break;
1974 case P2P_PRESENCE_REQ:
1975 p2p_process_presence_req(p2p, da, sa, data + 1, len - 1, freq);
1976 break;
1977 case P2P_PRESENCE_RESP:
1978 p2p_process_presence_resp(p2p, da, sa, data + 1, len - 1);
1979 break;
1980 case P2P_GO_DISC_REQ:
1981 p2p_process_go_disc_req(p2p, da, sa, data + 1, len - 1, freq);
1982 break;
1983 default:
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001984 p2p_dbg(p2p, "Received P2P Action - unknown type %u", data[0]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001985 break;
1986 }
1987}
1988
1989
1990static void p2p_go_neg_start(void *eloop_ctx, void *timeout_ctx)
1991{
1992 struct p2p_data *p2p = eloop_ctx;
1993 if (p2p->go_neg_peer == NULL)
1994 return;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001995 if (p2p->pending_listen_freq) {
1996 p2p_dbg(p2p, "Clear pending_listen_freq for p2p_go_neg_start");
1997 p2p->pending_listen_freq = 0;
1998 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001999 p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
2000 p2p->go_neg_peer->status = P2P_SC_SUCCESS;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07002001 /*
2002 * Set new timeout to make sure a previously set one does not expire
2003 * too quickly while waiting for the GO Negotiation to complete.
2004 */
2005 p2p_set_timeout(p2p, 0, 500000);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002006 p2p_connect_send(p2p, p2p->go_neg_peer);
2007}
2008
2009
2010static void p2p_invite_start(void *eloop_ctx, void *timeout_ctx)
2011{
2012 struct p2p_data *p2p = eloop_ctx;
2013 if (p2p->invite_peer == NULL)
2014 return;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07002015 if (p2p->pending_listen_freq) {
2016 p2p_dbg(p2p, "Clear pending_listen_freq for p2p_invite_start");
2017 p2p->pending_listen_freq = 0;
2018 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002019 p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002020 p2p_invite_send(p2p, p2p->invite_peer, p2p->invite_go_dev_addr,
2021 p2p->invite_dev_pw_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002022}
2023
2024
2025static void p2p_add_dev_from_probe_req(struct p2p_data *p2p, const u8 *addr,
2026 const u8 *ie, size_t ie_len)
2027{
2028 struct p2p_message msg;
2029 struct p2p_device *dev;
2030
2031 os_memset(&msg, 0, sizeof(msg));
2032 if (p2p_parse_ies(ie, ie_len, &msg) < 0 || msg.p2p_attributes == NULL)
2033 {
2034 p2p_parse_free(&msg);
2035 return; /* not a P2P probe */
2036 }
2037
2038 if (msg.ssid == NULL || msg.ssid[1] != P2P_WILDCARD_SSID_LEN ||
2039 os_memcmp(msg.ssid + 2, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN)
2040 != 0) {
2041 /* The Probe Request is not part of P2P Device Discovery. It is
2042 * not known whether the source address of the frame is the P2P
2043 * Device Address or P2P Interface Address. Do not add a new
2044 * peer entry based on this frames.
2045 */
2046 p2p_parse_free(&msg);
2047 return;
2048 }
2049
2050 dev = p2p_get_device(p2p, addr);
2051 if (dev) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002052 if (msg.listen_channel) {
2053 int freq;
2054
2055 if (dev->country[0] == 0)
2056 os_memcpy(dev->country, msg.listen_channel, 3);
2057
2058 freq = p2p_channel_to_freq(msg.listen_channel[3],
2059 msg.listen_channel[4]);
2060
2061 if (freq > 0 && dev->listen_freq != freq) {
2062 p2p_dbg(p2p,
2063 "Updated peer " MACSTR " Listen channel (Probe Request): %d -> %d MHz",
2064 MAC2STR(addr), dev->listen_freq, freq);
2065 dev->listen_freq = freq;
2066 }
2067 }
2068
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002069 os_get_reltime(&dev->last_seen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002070 p2p_parse_free(&msg);
2071 return; /* already known */
2072 }
2073
2074 dev = p2p_create_device(p2p, addr);
2075 if (dev == NULL) {
2076 p2p_parse_free(&msg);
2077 return;
2078 }
2079
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002080 os_get_reltime(&dev->last_seen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002081 dev->flags |= P2P_DEV_PROBE_REQ_ONLY;
2082
2083 if (msg.listen_channel) {
2084 os_memcpy(dev->country, msg.listen_channel, 3);
Dmitry Shmidt4b060592013-04-29 16:42:49 -07002085 dev->listen_freq = p2p_channel_to_freq(msg.listen_channel[3],
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002086 msg.listen_channel[4]);
2087 }
2088
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002089 p2p_copy_wps_info(p2p, dev, 1, &msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002090
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002091 if (msg.wfd_subelems) {
2092 wpabuf_free(dev->info.wfd_subelems);
2093 dev->info.wfd_subelems = wpabuf_dup(msg.wfd_subelems);
2094 }
2095
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002096 p2p_parse_free(&msg);
2097
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002098 p2p_dbg(p2p, "Created device entry based on Probe Req: " MACSTR
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002099 " dev_capab=0x%x group_capab=0x%x name='%s' listen_freq=%d",
2100 MAC2STR(dev->info.p2p_device_addr), dev->info.dev_capab,
2101 dev->info.group_capab, dev->info.device_name,
2102 dev->listen_freq);
2103}
2104
2105
2106struct p2p_device * p2p_add_dev_from_go_neg_req(struct p2p_data *p2p,
2107 const u8 *addr,
2108 struct p2p_message *msg)
2109{
2110 struct p2p_device *dev;
2111
2112 dev = p2p_get_device(p2p, addr);
2113 if (dev) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002114 os_get_reltime(&dev->last_seen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002115 return dev; /* already known */
2116 }
2117
2118 dev = p2p_create_device(p2p, addr);
2119 if (dev == NULL)
2120 return NULL;
2121
2122 p2p_add_dev_info(p2p, addr, dev, msg);
2123
2124 return dev;
2125}
2126
2127
2128static int dev_type_match(const u8 *dev_type, const u8 *req_dev_type)
2129{
2130 if (os_memcmp(dev_type, req_dev_type, WPS_DEV_TYPE_LEN) == 0)
2131 return 1;
2132 if (os_memcmp(dev_type, req_dev_type, 2) == 0 &&
2133 WPA_GET_BE32(&req_dev_type[2]) == 0 &&
2134 WPA_GET_BE16(&req_dev_type[6]) == 0)
2135 return 1; /* Category match with wildcard OUI/sub-category */
2136 return 0;
2137}
2138
2139
2140int dev_type_list_match(const u8 *dev_type, const u8 *req_dev_type[],
2141 size_t num_req_dev_type)
2142{
2143 size_t i;
2144 for (i = 0; i < num_req_dev_type; i++) {
2145 if (dev_type_match(dev_type, req_dev_type[i]))
2146 return 1;
2147 }
2148 return 0;
2149}
2150
2151
2152/**
2153 * p2p_match_dev_type - Match local device type with requested type
2154 * @p2p: P2P module context from p2p_init()
2155 * @wps: WPS TLVs from Probe Request frame (concatenated WPS IEs)
2156 * Returns: 1 on match, 0 on mismatch
2157 *
2158 * This function can be used to match the Requested Device Type attribute in
2159 * WPS IE with the local device types for deciding whether to reply to a Probe
2160 * Request frame.
2161 */
2162int p2p_match_dev_type(struct p2p_data *p2p, struct wpabuf *wps)
2163{
2164 struct wps_parse_attr attr;
2165 size_t i;
2166
2167 if (wps_parse_msg(wps, &attr))
2168 return 1; /* assume no Requested Device Type attributes */
2169
2170 if (attr.num_req_dev_type == 0)
2171 return 1; /* no Requested Device Type attributes -> match */
2172
2173 if (dev_type_list_match(p2p->cfg->pri_dev_type, attr.req_dev_type,
2174 attr.num_req_dev_type))
2175 return 1; /* Own Primary Device Type matches */
2176
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002177 for (i = 0; i < p2p->cfg->num_sec_dev_types; i++) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002178 if (dev_type_list_match(p2p->cfg->sec_dev_type[i],
2179 attr.req_dev_type,
2180 attr.num_req_dev_type))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002181 return 1; /* Own Secondary Device Type matches */
2182 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002183
2184 /* No matching device type found */
2185 return 0;
2186}
2187
2188
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002189struct wpabuf * p2p_build_probe_resp_ies(struct p2p_data *p2p,
2190 const u8 *query_hash,
2191 u8 query_count)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002192{
2193 struct wpabuf *buf;
2194 u8 *len;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002195 int pw_id = -1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002196 size_t extra = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002197
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002198#ifdef CONFIG_WIFI_DISPLAY
2199 if (p2p->wfd_ie_probe_resp)
2200 extra = wpabuf_len(p2p->wfd_ie_probe_resp);
2201#endif /* CONFIG_WIFI_DISPLAY */
2202
Dmitry Shmidt2e67f062014-07-16 09:55:28 -07002203 if (p2p->vendor_elem && p2p->vendor_elem[VENDOR_ELEM_PROBE_RESP_P2P])
2204 extra += wpabuf_len(p2p->vendor_elem[VENDOR_ELEM_PROBE_RESP_P2P]);
2205
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002206 if (query_count)
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002207 extra += MAX_SVC_ADV_IE_LEN;
2208
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002209 buf = wpabuf_alloc(1000 + extra);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002210 if (buf == NULL)
2211 return NULL;
2212
Dmitry Shmidt04949592012-07-19 12:16:46 -07002213 if (p2p->go_neg_peer) {
2214 /* Advertise immediate availability of WPS credential */
2215 pw_id = p2p_wps_method_pw_id(p2p->go_neg_peer->wps_method);
2216 }
2217
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002218 if (p2p_build_wps_ie(p2p, buf, pw_id, 1) < 0) {
2219 p2p_dbg(p2p, "Failed to build WPS IE for Probe Response");
2220 wpabuf_free(buf);
2221 return NULL;
2222 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002223
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002224#ifdef CONFIG_WIFI_DISPLAY
2225 if (p2p->wfd_ie_probe_resp)
2226 wpabuf_put_buf(buf, p2p->wfd_ie_probe_resp);
2227#endif /* CONFIG_WIFI_DISPLAY */
2228
Dmitry Shmidt2e67f062014-07-16 09:55:28 -07002229 if (p2p->vendor_elem && p2p->vendor_elem[VENDOR_ELEM_PROBE_RESP_P2P])
2230 wpabuf_put_buf(buf,
2231 p2p->vendor_elem[VENDOR_ELEM_PROBE_RESP_P2P]);
2232
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002233 /* P2P IE */
2234 len = p2p_buf_add_ie_hdr(buf);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002235 p2p_buf_add_capability(buf, p2p->dev_capab &
2236 ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002237 if (p2p->ext_listen_interval)
2238 p2p_buf_add_ext_listen_timing(buf, p2p->ext_listen_period,
2239 p2p->ext_listen_interval);
2240 p2p_buf_add_device_info(buf, p2p, NULL);
2241 p2p_buf_update_ie_hdr(buf, len);
2242
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002243 if (query_count) {
2244 p2p_buf_add_service_instance(buf, p2p, query_count, query_hash,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002245 p2p->p2ps_adv_list);
2246 }
2247
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002248 return buf;
2249}
2250
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07002251static int p2p_build_probe_resp_buf(struct p2p_data *p2p, struct wpabuf *buf,
2252 struct wpabuf *ies,
2253 const u8 *addr, int rx_freq)
2254{
2255 struct ieee80211_mgmt *resp;
2256 u8 channel, op_class;
2257
2258 resp = wpabuf_put(buf, offsetof(struct ieee80211_mgmt,
2259 u.probe_resp.variable));
2260
2261 resp->frame_control = host_to_le16((WLAN_FC_TYPE_MGMT << 2) |
2262 (WLAN_FC_STYPE_PROBE_RESP << 4));
2263 os_memcpy(resp->da, addr, ETH_ALEN);
2264 os_memcpy(resp->sa, p2p->cfg->dev_addr, ETH_ALEN);
2265 os_memcpy(resp->bssid, p2p->cfg->dev_addr, ETH_ALEN);
2266 resp->u.probe_resp.beacon_int = host_to_le16(100);
2267 /* hardware or low-level driver will setup seq_ctrl and timestamp */
2268 resp->u.probe_resp.capab_info =
2269 host_to_le16(WLAN_CAPABILITY_SHORT_PREAMBLE |
2270 WLAN_CAPABILITY_PRIVACY |
2271 WLAN_CAPABILITY_SHORT_SLOT_TIME);
2272
2273 wpabuf_put_u8(buf, WLAN_EID_SSID);
2274 wpabuf_put_u8(buf, P2P_WILDCARD_SSID_LEN);
2275 wpabuf_put_data(buf, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN);
2276
2277 wpabuf_put_u8(buf, WLAN_EID_SUPP_RATES);
2278 wpabuf_put_u8(buf, 8);
2279 wpabuf_put_u8(buf, (60 / 5) | 0x80);
2280 wpabuf_put_u8(buf, 90 / 5);
2281 wpabuf_put_u8(buf, (120 / 5) | 0x80);
2282 wpabuf_put_u8(buf, 180 / 5);
2283 wpabuf_put_u8(buf, (240 / 5) | 0x80);
2284 wpabuf_put_u8(buf, 360 / 5);
2285 wpabuf_put_u8(buf, 480 / 5);
2286 wpabuf_put_u8(buf, 540 / 5);
2287
2288 if (!rx_freq) {
2289 channel = p2p->cfg->channel;
2290 } else if (p2p_freq_to_channel(rx_freq, &op_class, &channel)) {
2291 p2p_err(p2p, "Failed to convert freq to channel");
2292 return -1;
2293 }
2294
2295 wpabuf_put_u8(buf, WLAN_EID_DS_PARAMS);
2296 wpabuf_put_u8(buf, 1);
2297 wpabuf_put_u8(buf, channel);
2298
2299 wpabuf_put_buf(buf, ies);
2300
2301 return 0;
2302}
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002303
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002304static int p2p_service_find_asp(struct p2p_data *p2p, const u8 *hash)
2305{
2306 struct p2ps_advertisement *adv_data;
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002307 int any_wfa;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002308
2309 p2p_dbg(p2p, "ASP find - ASP list: %p", p2p->p2ps_adv_list);
2310
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002311 /* Wildcard org.wi-fi.wfds matches any WFA spec defined service */
2312 any_wfa = os_memcmp(hash, p2p->wild_card_hash, P2PS_HASH_LEN) == 0;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002313
2314 adv_data = p2p->p2ps_adv_list;
2315 while (adv_data) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002316 if (os_memcmp(hash, adv_data->hash, P2PS_HASH_LEN) == 0)
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002317 return 1; /* exact hash match */
2318 if (any_wfa &&
2319 os_strncmp(adv_data->svc_name, P2PS_WILD_HASH_STR,
2320 os_strlen(P2PS_WILD_HASH_STR)) == 0)
2321 return 1; /* WFA service match */
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002322 adv_data = adv_data->next;
2323 }
2324
2325 return 0;
2326}
2327
2328
Dmitry Shmidt04949592012-07-19 12:16:46 -07002329static enum p2p_probe_req_status
2330p2p_reply_probe(struct p2p_data *p2p, const u8 *addr, const u8 *dst,
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002331 const u8 *bssid, const u8 *ie, size_t ie_len,
2332 unsigned int rx_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002333{
2334 struct ieee802_11_elems elems;
2335 struct wpabuf *buf;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002336 struct p2p_message msg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002337 struct wpabuf *ies;
2338
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002339 if (ieee802_11_parse_elems((u8 *) ie, ie_len, &elems, 0) ==
2340 ParseFailed) {
2341 /* Ignore invalid Probe Request frames */
Dmitry Shmidtec58b162014-02-19 12:44:18 -08002342 p2p_dbg(p2p, "Could not parse Probe Request frame - ignore it");
Dmitry Shmidt04949592012-07-19 12:16:46 -07002343 return P2P_PREQ_MALFORMED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002344 }
2345
2346 if (elems.p2p == NULL) {
2347 /* not a P2P probe - ignore it */
Dmitry Shmidtec58b162014-02-19 12:44:18 -08002348 p2p_dbg(p2p, "Not a P2P probe - ignore it");
Dmitry Shmidt04949592012-07-19 12:16:46 -07002349 return P2P_PREQ_NOT_P2P;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002350 }
2351
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002352 if (dst && !is_broadcast_ether_addr(dst) &&
2353 os_memcmp(dst, p2p->cfg->dev_addr, ETH_ALEN) != 0) {
2354 /* Not sent to the broadcast address or our P2P Device Address
2355 */
Dmitry Shmidtec58b162014-02-19 12:44:18 -08002356 p2p_dbg(p2p, "Probe Req DA " MACSTR " not ours - ignore it",
2357 MAC2STR(dst));
Dmitry Shmidt04949592012-07-19 12:16:46 -07002358 return P2P_PREQ_NOT_PROCESSED;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002359 }
2360
2361 if (bssid && !is_broadcast_ether_addr(bssid)) {
2362 /* Not sent to the Wildcard BSSID */
Dmitry Shmidtec58b162014-02-19 12:44:18 -08002363 p2p_dbg(p2p, "Probe Req BSSID " MACSTR " not wildcard - ignore it",
2364 MAC2STR(bssid));
Dmitry Shmidt04949592012-07-19 12:16:46 -07002365 return P2P_PREQ_NOT_PROCESSED;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002366 }
2367
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002368 if (elems.ssid == NULL || elems.ssid_len != P2P_WILDCARD_SSID_LEN ||
2369 os_memcmp(elems.ssid, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN) !=
2370 0) {
2371 /* not using P2P Wildcard SSID - ignore */
Dmitry Shmidtec58b162014-02-19 12:44:18 -08002372 p2p_dbg(p2p, "Probe Req not using P2P Wildcard SSID - ignore it");
Dmitry Shmidt04949592012-07-19 12:16:46 -07002373 return P2P_PREQ_NOT_PROCESSED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002374 }
2375
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002376 if (supp_rates_11b_only(&elems)) {
2377 /* Indicates support for 11b rates only */
Dmitry Shmidtec58b162014-02-19 12:44:18 -08002378 p2p_dbg(p2p, "Probe Req with 11b rates only supported - ignore it");
Dmitry Shmidt04949592012-07-19 12:16:46 -07002379 return P2P_PREQ_NOT_P2P;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002380 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002381
2382 os_memset(&msg, 0, sizeof(msg));
2383 if (p2p_parse_ies(ie, ie_len, &msg) < 0) {
2384 /* Could not parse P2P attributes */
Dmitry Shmidtec58b162014-02-19 12:44:18 -08002385 p2p_dbg(p2p, "Could not parse P2P attributes in Probe Req - ignore it");
Dmitry Shmidt04949592012-07-19 12:16:46 -07002386 return P2P_PREQ_NOT_P2P;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002387 }
2388
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002389 if (msg.service_hash && msg.service_hash_count) {
2390 const u8 *hash = msg.service_hash;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002391 u8 i;
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002392 int p2ps_svc_found = 0;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002393
Dmitry Shmidt41712582015-06-29 11:02:15 -07002394 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",
2395 p2p->in_listen, p2p->drv_in_listen, rx_freq,
2396 p2p->cfg->channel, p2p->pending_listen_freq);
2397
2398 if (!p2p->in_listen && !p2p->drv_in_listen &&
2399 p2p->pending_listen_freq && rx_freq &&
2400 rx_freq != p2p->pending_listen_freq) {
2401 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",
2402 rx_freq, p2p->pending_listen_freq);
2403 p2p_parse_free(&msg);
2404 return P2P_PREQ_NOT_LISTEN;
2405 }
2406
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002407 for (i = 0; i < msg.service_hash_count; i++) {
2408 if (p2p_service_find_asp(p2p, hash)) {
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002409 p2p_dbg(p2p, "Service Hash match found: "
2410 MACSTR, MAC2STR(hash));
2411 p2ps_svc_found = 1;
2412 break;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002413 }
2414 hash += P2PS_HASH_LEN;
2415 }
2416
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002417 /* Probed hash unknown */
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002418 if (!p2ps_svc_found) {
2419 p2p_dbg(p2p, "No Service Hash match found");
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002420 p2p_parse_free(&msg);
2421 return P2P_PREQ_NOT_PROCESSED;
2422 }
2423 } else {
2424 /* This is not a P2PS Probe Request */
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002425 p2p_dbg(p2p, "No P2PS Hash in Probe Request");
2426
2427 if (!p2p->in_listen || !p2p->drv_in_listen) {
2428 /* not in Listen state - ignore Probe Request */
2429 p2p_dbg(p2p, "Not in Listen state (in_listen=%d drv_in_listen=%d) - ignore Probe Request",
2430 p2p->in_listen, p2p->drv_in_listen);
2431 p2p_parse_free(&msg);
2432 return P2P_PREQ_NOT_LISTEN;
2433 }
2434 }
2435
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002436 if (msg.device_id &&
Dmitry Shmidt04949592012-07-19 12:16:46 -07002437 os_memcmp(msg.device_id, p2p->cfg->dev_addr, ETH_ALEN) != 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002438 /* Device ID did not match */
Dmitry Shmidtec58b162014-02-19 12:44:18 -08002439 p2p_dbg(p2p, "Probe Req requested Device ID " MACSTR " did not match - ignore it",
2440 MAC2STR(msg.device_id));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002441 p2p_parse_free(&msg);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002442 return P2P_PREQ_NOT_PROCESSED;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002443 }
2444
2445 /* Check Requested Device Type match */
2446 if (msg.wps_attributes &&
2447 !p2p_match_dev_type(p2p, msg.wps_attributes)) {
2448 /* No match with Requested Device Type */
Hai Shalom021b0b52019-04-10 11:17:58 -07002449 p2p_dbg(p2p, "Probe Req requested Device Type did not match - ignore it");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002450 p2p_parse_free(&msg);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002451 return P2P_PREQ_NOT_PROCESSED;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002452 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002453
Dmitry Shmidt04949592012-07-19 12:16:46 -07002454 if (!p2p->cfg->send_probe_resp) {
2455 /* Response generated elsewhere */
Dmitry Shmidtec58b162014-02-19 12:44:18 -08002456 p2p_dbg(p2p, "Probe Resp generated elsewhere - do not generate additional response");
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002457 p2p_parse_free(&msg);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002458 return P2P_PREQ_NOT_PROCESSED;
2459 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002460
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002461 p2p_dbg(p2p, "Reply to P2P Probe Request in Listen state");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002462
2463 /*
2464 * We do not really have a specific BSS that this frame is advertising,
2465 * so build a frame that has some information in valid format. This is
2466 * really only used for discovery purposes, not to learn exact BSS
2467 * parameters.
2468 */
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002469 ies = p2p_build_probe_resp_ies(p2p, msg.service_hash,
2470 msg.service_hash_count);
2471 p2p_parse_free(&msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002472 if (ies == NULL)
Dmitry Shmidt04949592012-07-19 12:16:46 -07002473 return P2P_PREQ_NOT_PROCESSED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002474
2475 buf = wpabuf_alloc(200 + wpabuf_len(ies));
2476 if (buf == NULL) {
2477 wpabuf_free(ies);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002478 return P2P_PREQ_NOT_PROCESSED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002479 }
2480
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07002481 if (p2p_build_probe_resp_buf(p2p, buf, ies, addr, rx_freq)) {
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002482 wpabuf_free(ies);
2483 wpabuf_free(buf);
2484 return P2P_PREQ_NOT_PROCESSED;
2485 }
2486
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002487 wpabuf_free(ies);
2488
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002489 p2p->cfg->send_probe_resp(p2p->cfg->cb_ctx, buf, rx_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002490
2491 wpabuf_free(buf);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002492
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002493 return P2P_PREQ_PROCESSED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002494}
2495
2496
Dmitry Shmidt04949592012-07-19 12:16:46 -07002497enum p2p_probe_req_status
2498p2p_probe_req_rx(struct p2p_data *p2p, const u8 *addr, const u8 *dst,
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002499 const u8 *bssid, const u8 *ie, size_t ie_len,
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07002500 unsigned int rx_freq, int p2p_lo_started)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002501{
Dmitry Shmidt04949592012-07-19 12:16:46 -07002502 enum p2p_probe_req_status res;
2503
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002504 p2p_add_dev_from_probe_req(p2p, addr, ie, ie_len);
2505
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07002506 if (p2p_lo_started) {
2507 p2p_dbg(p2p,
2508 "Probe Response is offloaded, do not reply Probe Request");
2509 return P2P_PREQ_PROCESSED;
2510 }
2511
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002512 res = p2p_reply_probe(p2p, addr, dst, bssid, ie, ie_len, rx_freq);
2513 if (res != P2P_PREQ_PROCESSED && res != P2P_PREQ_NOT_PROCESSED)
2514 return res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002515
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002516 /*
2517 * Activate a pending GO Negotiation/Invite flow if a received Probe
2518 * Request frame is from an expected peer. Some devices may share the
2519 * same address for P2P and non-P2P STA running simultaneously. The
2520 * P2P_PREQ_PROCESSED and P2P_PREQ_NOT_PROCESSED p2p_reply_probe()
2521 * return values verified above ensure we are handling a Probe Request
2522 * frame from a P2P peer.
2523 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002524 if ((p2p->state == P2P_CONNECT || p2p->state == P2P_CONNECT_LISTEN) &&
2525 p2p->go_neg_peer &&
2526 os_memcmp(addr, p2p->go_neg_peer->info.p2p_device_addr, ETH_ALEN)
Dmitry Shmidt9cdf1b92013-02-27 12:58:50 -08002527 == 0 &&
2528 !(p2p->go_neg_peer->flags & P2P_DEV_WAIT_GO_NEG_CONFIRM)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002529 /* Received a Probe Request from GO Negotiation peer */
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002530 p2p_dbg(p2p, "Found GO Negotiation peer - try to start GO negotiation from timeout");
Dmitry Shmidt9cdf1b92013-02-27 12:58:50 -08002531 eloop_cancel_timeout(p2p_go_neg_start, p2p, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002532 eloop_register_timeout(0, 0, p2p_go_neg_start, p2p, NULL);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002533 return res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002534 }
2535
2536 if ((p2p->state == P2P_INVITE || p2p->state == P2P_INVITE_LISTEN) &&
2537 p2p->invite_peer &&
Dmitry Shmidt3c479372014-02-04 10:50:36 -08002538 (p2p->invite_peer->flags & P2P_DEV_WAIT_INV_REQ_ACK) &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002539 os_memcmp(addr, p2p->invite_peer->info.p2p_device_addr, ETH_ALEN)
2540 == 0) {
2541 /* Received a Probe Request from Invite peer */
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002542 p2p_dbg(p2p, "Found Invite peer - try to start Invite from timeout");
Dmitry Shmidt7f93d6f2014-02-21 11:22:49 -08002543 eloop_cancel_timeout(p2p_invite_start, p2p, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002544 eloop_register_timeout(0, 0, p2p_invite_start, p2p, NULL);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002545 return res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002546 }
2547
Dmitry Shmidt04949592012-07-19 12:16:46 -07002548 return res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002549}
2550
2551
2552static int p2p_assoc_req_ie_wlan_ap(struct p2p_data *p2p, const u8 *bssid,
2553 u8 *buf, size_t len, struct wpabuf *p2p_ie)
2554{
2555 struct wpabuf *tmp;
2556 u8 *lpos;
2557 size_t tmplen;
2558 int res;
2559 u8 group_capab;
Dmitry Shmidt05df46a2015-05-19 11:02:01 -07002560 struct p2p_message msg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002561
2562 if (p2p_ie == NULL)
2563 return 0; /* WLAN AP is not a P2P manager */
2564
Dmitry Shmidt05df46a2015-05-19 11:02:01 -07002565 os_memset(&msg, 0, sizeof(msg));
2566 if (p2p_parse_p2p_ie(p2p_ie, &msg) < 0)
2567 return 0;
2568
2569 p2p_dbg(p2p, "BSS P2P manageability %s",
2570 msg.manageability ? "enabled" : "disabled");
2571
2572 if (!msg.manageability)
2573 return 0;
2574
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002575 /*
2576 * (Re)Association Request - P2P IE
2577 * P2P Capability attribute (shall be present)
2578 * P2P Interface attribute (present if concurrent device and
2579 * P2P Management is enabled)
2580 */
2581 tmp = wpabuf_alloc(200);
2582 if (tmp == NULL)
2583 return -1;
2584
2585 lpos = p2p_buf_add_ie_hdr(tmp);
2586 group_capab = 0;
2587 if (p2p->num_groups > 0) {
2588 group_capab |= P2P_GROUP_CAPAB_GROUP_OWNER;
2589 if ((p2p->dev_capab & P2P_DEV_CAPAB_CONCURRENT_OPER) &&
2590 (p2p->dev_capab & P2P_DEV_CAPAB_INFRA_MANAGED) &&
2591 p2p->cross_connect)
2592 group_capab |= P2P_GROUP_CAPAB_CROSS_CONN;
2593 }
2594 p2p_buf_add_capability(tmp, p2p->dev_capab, group_capab);
2595 if ((p2p->dev_capab & P2P_DEV_CAPAB_CONCURRENT_OPER) &&
2596 (p2p->dev_capab & P2P_DEV_CAPAB_INFRA_MANAGED))
2597 p2p_buf_add_p2p_interface(tmp, p2p);
2598 p2p_buf_update_ie_hdr(tmp, lpos);
2599
2600 tmplen = wpabuf_len(tmp);
2601 if (tmplen > len)
2602 res = -1;
2603 else {
2604 os_memcpy(buf, wpabuf_head(tmp), tmplen);
2605 res = tmplen;
2606 }
2607 wpabuf_free(tmp);
2608
2609 return res;
2610}
2611
2612
2613int p2p_assoc_req_ie(struct p2p_data *p2p, const u8 *bssid, u8 *buf,
2614 size_t len, int p2p_group, struct wpabuf *p2p_ie)
2615{
2616 struct wpabuf *tmp;
2617 u8 *lpos;
2618 struct p2p_device *peer;
2619 size_t tmplen;
2620 int res;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002621 size_t extra = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002622
2623 if (!p2p_group)
2624 return p2p_assoc_req_ie_wlan_ap(p2p, bssid, buf, len, p2p_ie);
2625
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002626#ifdef CONFIG_WIFI_DISPLAY
2627 if (p2p->wfd_ie_assoc_req)
2628 extra = wpabuf_len(p2p->wfd_ie_assoc_req);
2629#endif /* CONFIG_WIFI_DISPLAY */
2630
Dmitry Shmidt2e67f062014-07-16 09:55:28 -07002631 if (p2p->vendor_elem && p2p->vendor_elem[VENDOR_ELEM_P2P_ASSOC_REQ])
2632 extra += wpabuf_len(p2p->vendor_elem[VENDOR_ELEM_P2P_ASSOC_REQ]);
2633
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002634 /*
2635 * (Re)Association Request - P2P IE
2636 * P2P Capability attribute (shall be present)
2637 * Extended Listen Timing (may be present)
2638 * P2P Device Info attribute (shall be present)
2639 */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002640 tmp = wpabuf_alloc(200 + extra);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002641 if (tmp == NULL)
2642 return -1;
2643
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002644#ifdef CONFIG_WIFI_DISPLAY
2645 if (p2p->wfd_ie_assoc_req)
2646 wpabuf_put_buf(tmp, p2p->wfd_ie_assoc_req);
2647#endif /* CONFIG_WIFI_DISPLAY */
2648
Dmitry Shmidt2e67f062014-07-16 09:55:28 -07002649 if (p2p->vendor_elem && p2p->vendor_elem[VENDOR_ELEM_P2P_ASSOC_REQ])
2650 wpabuf_put_buf(tmp,
2651 p2p->vendor_elem[VENDOR_ELEM_P2P_ASSOC_REQ]);
2652
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002653 peer = bssid ? p2p_get_device(p2p, bssid) : NULL;
2654
2655 lpos = p2p_buf_add_ie_hdr(tmp);
2656 p2p_buf_add_capability(tmp, p2p->dev_capab, 0);
2657 if (p2p->ext_listen_interval)
2658 p2p_buf_add_ext_listen_timing(tmp, p2p->ext_listen_period,
2659 p2p->ext_listen_interval);
2660 p2p_buf_add_device_info(tmp, p2p, peer);
2661 p2p_buf_update_ie_hdr(tmp, lpos);
2662
2663 tmplen = wpabuf_len(tmp);
2664 if (tmplen > len)
2665 res = -1;
2666 else {
2667 os_memcpy(buf, wpabuf_head(tmp), tmplen);
2668 res = tmplen;
2669 }
2670 wpabuf_free(tmp);
2671
2672 return res;
2673}
2674
2675
2676int p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf, char *end)
2677{
2678 struct wpabuf *p2p_ie;
2679 int ret;
2680
2681 p2p_ie = ieee802_11_vendor_ie_concat(ies, ies_len, P2P_IE_VENDOR_TYPE);
2682 if (p2p_ie == NULL)
2683 return 0;
2684
2685 ret = p2p_attr_text(p2p_ie, buf, end);
2686 wpabuf_free(p2p_ie);
2687 return ret;
2688}
2689
2690
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002691struct p2ps_advertisement *
2692p2p_service_p2ps_id(struct p2p_data *p2p, u32 adv_id)
2693{
2694 struct p2ps_advertisement *adv_data;
2695
2696 if (!p2p)
2697 return NULL;
2698
2699 adv_data = p2p->p2ps_adv_list;
2700 while (adv_data) {
2701 if (adv_data->id == adv_id)
2702 return adv_data;
2703 adv_data = adv_data->next;
2704 }
2705
2706 return NULL;
2707}
2708
2709
2710int p2p_service_del_asp(struct p2p_data *p2p, u32 adv_id)
2711{
2712 struct p2ps_advertisement *adv_data;
2713 struct p2ps_advertisement **prior;
2714
2715 if (!p2p)
2716 return -1;
2717
2718 adv_data = p2p->p2ps_adv_list;
2719 prior = &p2p->p2ps_adv_list;
2720 while (adv_data) {
2721 if (adv_data->id == adv_id) {
2722 p2p_dbg(p2p, "Delete ASP adv_id=0x%x", adv_id);
2723 *prior = adv_data->next;
2724 os_free(adv_data);
2725 return 0;
2726 }
2727 prior = &adv_data->next;
2728 adv_data = adv_data->next;
2729 }
2730
2731 return -1;
2732}
2733
2734
2735int p2p_service_add_asp(struct p2p_data *p2p, int auto_accept, u32 adv_id,
2736 const char *adv_str, u8 svc_state, u16 config_methods,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002737 const char *svc_info, const u8 *cpt_priority)
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002738{
2739 struct p2ps_advertisement *adv_data, *tmp, **prev;
2740 u8 buf[P2PS_HASH_LEN];
2741 size_t adv_data_len, adv_len, info_len = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002742 int i;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002743
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002744 if (!p2p || !adv_str || !adv_str[0] || !cpt_priority)
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002745 return -1;
2746
2747 if (!(config_methods & p2p->cfg->config_methods)) {
2748 p2p_dbg(p2p, "Config methods not supported svc: 0x%x dev: 0x%x",
2749 config_methods, p2p->cfg->config_methods);
2750 return -1;
2751 }
2752
2753 if (!p2ps_gen_hash(p2p, adv_str, buf))
2754 return -1;
2755
2756 if (svc_info)
2757 info_len = os_strlen(svc_info);
2758 adv_len = os_strlen(adv_str);
2759 adv_data_len = sizeof(struct p2ps_advertisement) + adv_len + 1 +
2760 info_len + 1;
2761
2762 adv_data = os_zalloc(adv_data_len);
2763 if (!adv_data)
2764 return -1;
2765
2766 os_memcpy(adv_data->hash, buf, P2PS_HASH_LEN);
2767 adv_data->id = adv_id;
2768 adv_data->state = svc_state;
2769 adv_data->config_methods = config_methods & p2p->cfg->config_methods;
2770 adv_data->auto_accept = (u8) auto_accept;
2771 os_memcpy(adv_data->svc_name, adv_str, adv_len);
2772
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002773 for (i = 0; cpt_priority[i] && i < P2PS_FEATURE_CAPAB_CPT_MAX; i++) {
2774 adv_data->cpt_priority[i] = cpt_priority[i];
2775 adv_data->cpt_mask |= cpt_priority[i];
2776 }
2777
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002778 if (svc_info && info_len) {
2779 adv_data->svc_info = &adv_data->svc_name[adv_len + 1];
2780 os_memcpy(adv_data->svc_info, svc_info, info_len);
2781 }
2782
2783 /*
2784 * Group Advertisements by service string. They do not need to be
2785 * sorted, but groups allow easier Probe Response instance grouping
2786 */
2787 tmp = p2p->p2ps_adv_list;
2788 prev = &p2p->p2ps_adv_list;
2789 while (tmp) {
2790 if (tmp->id == adv_data->id) {
2791 if (os_strcmp(tmp->svc_name, adv_data->svc_name) != 0) {
2792 os_free(adv_data);
2793 return -1;
2794 }
2795 adv_data->next = tmp->next;
2796 *prev = adv_data;
2797 os_free(tmp);
2798 goto inserted;
2799 } else {
2800 if (os_strcmp(tmp->svc_name, adv_data->svc_name) == 0) {
2801 adv_data->next = tmp->next;
2802 tmp->next = adv_data;
2803 goto inserted;
2804 }
2805 }
2806 prev = &tmp->next;
2807 tmp = tmp->next;
2808 }
2809
2810 /* No svc_name match found */
2811 adv_data->next = p2p->p2ps_adv_list;
2812 p2p->p2ps_adv_list = adv_data;
2813
2814inserted:
2815 p2p_dbg(p2p,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002816 "Added ASP advertisement adv_id=0x%x config_methods=0x%x svc_state=0x%x adv_str='%s' cpt_mask=0x%x",
2817 adv_id, adv_data->config_methods, svc_state, adv_str,
2818 adv_data->cpt_mask);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002819
2820 return 0;
2821}
2822
2823
Dmitry Shmidtdda10c22015-03-24 16:05:01 -07002824void p2p_service_flush_asp(struct p2p_data *p2p)
2825{
2826 struct p2ps_advertisement *adv, *prev;
2827
2828 if (!p2p)
2829 return;
2830
2831 adv = p2p->p2ps_adv_list;
2832 while (adv) {
2833 prev = adv;
2834 adv = adv->next;
2835 os_free(prev);
2836 }
2837
2838 p2p->p2ps_adv_list = NULL;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002839 p2ps_prov_free(p2p);
Dmitry Shmidtdda10c22015-03-24 16:05:01 -07002840 p2p_dbg(p2p, "All ASP advertisements flushed");
2841}
2842
2843
Dmitry Shmidt04949592012-07-19 12:16:46 -07002844int p2p_parse_dev_addr_in_p2p_ie(struct wpabuf *p2p_ie, u8 *dev_addr)
2845{
2846 struct p2p_message msg;
2847
2848 os_memset(&msg, 0, sizeof(msg));
2849 if (p2p_parse_p2p_ie(p2p_ie, &msg))
2850 return -1;
2851
2852 if (msg.p2p_device_addr) {
2853 os_memcpy(dev_addr, msg.p2p_device_addr, ETH_ALEN);
2854 return 0;
2855 } else if (msg.device_id) {
2856 os_memcpy(dev_addr, msg.device_id, ETH_ALEN);
2857 return 0;
2858 }
2859 return -1;
2860}
2861
2862
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002863int p2p_parse_dev_addr(const u8 *ies, size_t ies_len, u8 *dev_addr)
2864{
2865 struct wpabuf *p2p_ie;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002866 int ret;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002867
2868 p2p_ie = ieee802_11_vendor_ie_concat(ies, ies_len,
2869 P2P_IE_VENDOR_TYPE);
2870 if (p2p_ie == NULL)
2871 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002872 ret = p2p_parse_dev_addr_in_p2p_ie(p2p_ie, dev_addr);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002873 wpabuf_free(p2p_ie);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002874 return ret;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002875}
2876
2877
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002878static void p2p_clear_go_neg(struct p2p_data *p2p)
2879{
2880 p2p->go_neg_peer = NULL;
2881 p2p_clear_timeout(p2p);
2882 p2p_set_state(p2p, P2P_IDLE);
2883}
2884
2885
2886void p2p_wps_success_cb(struct p2p_data *p2p, const u8 *mac_addr)
2887{
2888 if (p2p->go_neg_peer == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002889 p2p_dbg(p2p, "No pending Group Formation - ignore WPS registration success notification");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002890 return; /* No pending Group Formation */
2891 }
2892
2893 if (os_memcmp(mac_addr, p2p->go_neg_peer->intended_addr, ETH_ALEN) !=
2894 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002895 p2p_dbg(p2p, "Ignore WPS registration success notification for "
2896 MACSTR " (GO Negotiation peer " MACSTR ")",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002897 MAC2STR(mac_addr),
2898 MAC2STR(p2p->go_neg_peer->intended_addr));
2899 return; /* Ignore unexpected peer address */
2900 }
2901
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002902 p2p_dbg(p2p, "Group Formation completed successfully with " MACSTR,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002903 MAC2STR(mac_addr));
2904
2905 p2p_clear_go_neg(p2p);
2906}
2907
2908
2909void p2p_group_formation_failed(struct p2p_data *p2p)
2910{
2911 if (p2p->go_neg_peer == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002912 p2p_dbg(p2p, "No pending Group Formation - ignore group formation failure notification");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002913 return; /* No pending Group Formation */
2914 }
2915
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002916 p2p_dbg(p2p, "Group Formation failed with " MACSTR,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002917 MAC2STR(p2p->go_neg_peer->intended_addr));
2918
2919 p2p_clear_go_neg(p2p);
2920}
2921
2922
2923struct p2p_data * p2p_init(const struct p2p_config *cfg)
2924{
2925 struct p2p_data *p2p;
2926
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07002927 if (cfg->max_peers < 1 ||
2928 cfg->passphrase_len < 8 || cfg->passphrase_len > 63)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002929 return NULL;
2930
2931 p2p = os_zalloc(sizeof(*p2p) + sizeof(*cfg));
2932 if (p2p == NULL)
2933 return NULL;
2934 p2p->cfg = (struct p2p_config *) (p2p + 1);
2935 os_memcpy(p2p->cfg, cfg, sizeof(*cfg));
2936 if (cfg->dev_name)
2937 p2p->cfg->dev_name = os_strdup(cfg->dev_name);
2938 if (cfg->manufacturer)
2939 p2p->cfg->manufacturer = os_strdup(cfg->manufacturer);
2940 if (cfg->model_name)
2941 p2p->cfg->model_name = os_strdup(cfg->model_name);
2942 if (cfg->model_number)
2943 p2p->cfg->model_number = os_strdup(cfg->model_number);
2944 if (cfg->serial_number)
2945 p2p->cfg->serial_number = os_strdup(cfg->serial_number);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002946 if (cfg->pref_chan) {
2947 p2p->cfg->pref_chan = os_malloc(cfg->num_pref_chan *
2948 sizeof(struct p2p_channel));
2949 if (p2p->cfg->pref_chan) {
2950 os_memcpy(p2p->cfg->pref_chan, cfg->pref_chan,
2951 cfg->num_pref_chan *
2952 sizeof(struct p2p_channel));
2953 } else
2954 p2p->cfg->num_pref_chan = 0;
2955 }
2956
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002957 p2ps_gen_hash(p2p, P2PS_WILD_HASH_STR, p2p->wild_card_hash);
2958
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002959 p2p->min_disc_int = 1;
2960 p2p->max_disc_int = 3;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002961 p2p->max_disc_tu = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002962
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07002963 if (os_get_random(&p2p->next_tie_breaker, 1) < 0)
2964 p2p->next_tie_breaker = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002965 p2p->next_tie_breaker &= 0x01;
2966 if (cfg->sd_request)
2967 p2p->dev_capab |= P2P_DEV_CAPAB_SERVICE_DISCOVERY;
2968 p2p->dev_capab |= P2P_DEV_CAPAB_INVITATION_PROCEDURE;
2969 if (cfg->concurrent_operations)
2970 p2p->dev_capab |= P2P_DEV_CAPAB_CONCURRENT_OPER;
2971 p2p->dev_capab |= P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
2972
2973 dl_list_init(&p2p->devices);
2974
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002975 p2p->go_timeout = 100;
2976 p2p->client_timeout = 20;
Dmitry Shmidt13ca8d82014-02-20 10:18:40 -08002977 p2p->num_p2p_sd_queries = 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002978
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002979 p2p_dbg(p2p, "initialized");
2980 p2p_channels_dump(p2p, "channels", &p2p->cfg->channels);
2981 p2p_channels_dump(p2p, "cli_channels", &p2p->cfg->cli_channels);
2982
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002983 return p2p;
2984}
2985
2986
2987void p2p_deinit(struct p2p_data *p2p)
2988{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002989#ifdef CONFIG_WIFI_DISPLAY
2990 wpabuf_free(p2p->wfd_ie_beacon);
2991 wpabuf_free(p2p->wfd_ie_probe_req);
2992 wpabuf_free(p2p->wfd_ie_probe_resp);
2993 wpabuf_free(p2p->wfd_ie_assoc_req);
2994 wpabuf_free(p2p->wfd_ie_invitation);
2995 wpabuf_free(p2p->wfd_ie_prov_disc_req);
2996 wpabuf_free(p2p->wfd_ie_prov_disc_resp);
2997 wpabuf_free(p2p->wfd_ie_go_neg);
2998 wpabuf_free(p2p->wfd_dev_info);
2999 wpabuf_free(p2p->wfd_assoc_bssid);
3000 wpabuf_free(p2p->wfd_coupled_sink_info);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003001 wpabuf_free(p2p->wfd_r2_dev_info);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003002#endif /* CONFIG_WIFI_DISPLAY */
3003
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003004 eloop_cancel_timeout(p2p_scan_timeout, p2p, NULL);
Dmitry Shmidt9cdf1b92013-02-27 12:58:50 -08003005 eloop_cancel_timeout(p2p_go_neg_start, p2p, NULL);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003006 eloop_cancel_timeout(p2p_go_neg_wait_timeout, p2p, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003007 p2p_flush(p2p);
3008 p2p_free_req_dev_types(p2p);
3009 os_free(p2p->cfg->dev_name);
3010 os_free(p2p->cfg->manufacturer);
3011 os_free(p2p->cfg->model_name);
3012 os_free(p2p->cfg->model_number);
3013 os_free(p2p->cfg->serial_number);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003014 os_free(p2p->cfg->pref_chan);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003015 os_free(p2p->groups);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07003016 p2ps_prov_free(p2p);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003017 wpabuf_free(p2p->sd_resp);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003018 p2p_remove_wps_vendor_extensions(p2p);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003019 os_free(p2p->no_go_freq.range);
Dmitry Shmidtdda10c22015-03-24 16:05:01 -07003020 p2p_service_flush_asp(p2p);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003021
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003022 os_free(p2p);
3023}
3024
3025
3026void p2p_flush(struct p2p_data *p2p)
3027{
3028 struct p2p_device *dev, *prev;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003029
3030 p2p_ext_listen(p2p, 0, 0);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003031 p2p_stop_find(p2p);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003032 dl_list_for_each_safe(dev, prev, &p2p->devices, struct p2p_device,
3033 list) {
3034 dl_list_del(&dev->list);
3035 p2p_device_free(p2p, dev);
3036 }
3037 p2p_free_sd_queries(p2p);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003038 p2p->ssid_set = 0;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003039 p2ps_prov_free(p2p);
3040 p2p_reset_pending_pd(p2p);
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08003041 p2p->override_pref_op_class = 0;
3042 p2p->override_pref_channel = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003043}
3044
3045
3046int p2p_unauthorize(struct p2p_data *p2p, const u8 *addr)
3047{
3048 struct p2p_device *dev;
3049
3050 dev = p2p_get_device(p2p, addr);
3051 if (dev == NULL)
3052 return -1;
3053
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003054 p2p_dbg(p2p, "Unauthorizing " MACSTR, MAC2STR(addr));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003055
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003056 if (p2p->go_neg_peer == dev) {
3057 eloop_cancel_timeout(p2p_go_neg_wait_timeout, p2p, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003058 p2p->go_neg_peer = NULL;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003059 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003060
3061 dev->wps_method = WPS_NOT_READY;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003062 dev->oob_pw_id = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003063 dev->flags &= ~P2P_DEV_WAIT_GO_NEG_RESPONSE;
3064 dev->flags &= ~P2P_DEV_WAIT_GO_NEG_CONFIRM;
3065
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003066 return 0;
3067}
3068
3069
3070int p2p_set_dev_name(struct p2p_data *p2p, const char *dev_name)
3071{
3072 os_free(p2p->cfg->dev_name);
3073 if (dev_name) {
3074 p2p->cfg->dev_name = os_strdup(dev_name);
3075 if (p2p->cfg->dev_name == NULL)
3076 return -1;
3077 } else
3078 p2p->cfg->dev_name = NULL;
3079 return 0;
3080}
3081
3082
3083int p2p_set_manufacturer(struct p2p_data *p2p, const char *manufacturer)
3084{
3085 os_free(p2p->cfg->manufacturer);
3086 p2p->cfg->manufacturer = NULL;
3087 if (manufacturer) {
3088 p2p->cfg->manufacturer = os_strdup(manufacturer);
3089 if (p2p->cfg->manufacturer == NULL)
3090 return -1;
3091 }
3092
3093 return 0;
3094}
3095
3096
3097int p2p_set_model_name(struct p2p_data *p2p, const char *model_name)
3098{
3099 os_free(p2p->cfg->model_name);
3100 p2p->cfg->model_name = NULL;
3101 if (model_name) {
3102 p2p->cfg->model_name = os_strdup(model_name);
3103 if (p2p->cfg->model_name == NULL)
3104 return -1;
3105 }
3106
3107 return 0;
3108}
3109
3110
3111int p2p_set_model_number(struct p2p_data *p2p, const char *model_number)
3112{
3113 os_free(p2p->cfg->model_number);
3114 p2p->cfg->model_number = NULL;
3115 if (model_number) {
3116 p2p->cfg->model_number = os_strdup(model_number);
3117 if (p2p->cfg->model_number == NULL)
3118 return -1;
3119 }
3120
3121 return 0;
3122}
3123
3124
3125int p2p_set_serial_number(struct p2p_data *p2p, const char *serial_number)
3126{
3127 os_free(p2p->cfg->serial_number);
3128 p2p->cfg->serial_number = NULL;
3129 if (serial_number) {
3130 p2p->cfg->serial_number = os_strdup(serial_number);
3131 if (p2p->cfg->serial_number == NULL)
3132 return -1;
3133 }
3134
3135 return 0;
3136}
3137
3138
3139void p2p_set_config_methods(struct p2p_data *p2p, u16 config_methods)
3140{
3141 p2p->cfg->config_methods = config_methods;
3142}
3143
3144
3145void p2p_set_uuid(struct p2p_data *p2p, const u8 *uuid)
3146{
3147 os_memcpy(p2p->cfg->uuid, uuid, 16);
3148}
3149
3150
3151int p2p_set_pri_dev_type(struct p2p_data *p2p, const u8 *pri_dev_type)
3152{
3153 os_memcpy(p2p->cfg->pri_dev_type, pri_dev_type, 8);
3154 return 0;
3155}
3156
3157
3158int p2p_set_sec_dev_types(struct p2p_data *p2p, const u8 dev_types[][8],
3159 size_t num_dev_types)
3160{
3161 if (num_dev_types > P2P_SEC_DEVICE_TYPES)
3162 num_dev_types = P2P_SEC_DEVICE_TYPES;
3163 p2p->cfg->num_sec_dev_types = num_dev_types;
3164 os_memcpy(p2p->cfg->sec_dev_type, dev_types, num_dev_types * 8);
3165 return 0;
3166}
3167
3168
3169void p2p_remove_wps_vendor_extensions(struct p2p_data *p2p)
3170{
3171 int i;
3172
3173 for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) {
3174 wpabuf_free(p2p->wps_vendor_ext[i]);
3175 p2p->wps_vendor_ext[i] = NULL;
3176 }
3177}
3178
3179
3180int p2p_add_wps_vendor_extension(struct p2p_data *p2p,
3181 const struct wpabuf *vendor_ext)
3182{
3183 int i;
3184
3185 if (vendor_ext == NULL)
3186 return -1;
3187
3188 for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) {
3189 if (p2p->wps_vendor_ext[i] == NULL)
3190 break;
3191 }
3192 if (i >= P2P_MAX_WPS_VENDOR_EXT)
3193 return -1;
3194
3195 p2p->wps_vendor_ext[i] = wpabuf_dup(vendor_ext);
3196 if (p2p->wps_vendor_ext[i] == NULL)
3197 return -1;
3198
3199 return 0;
3200}
3201
3202
3203int p2p_set_country(struct p2p_data *p2p, const char *country)
3204{
3205 os_memcpy(p2p->cfg->country, country, 3);
3206 return 0;
3207}
3208
3209
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003210static int p2p_pre_find_operation(struct p2p_data *p2p, struct p2p_device *dev)
3211{
Dmitry Shmidtde47be72016-01-07 12:52:55 -08003212 int res;
3213
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003214 if (dev->sd_pending_bcast_queries == 0) {
3215 /* Initialize with total number of registered broadcast
3216 * SD queries. */
3217 dev->sd_pending_bcast_queries = p2p->num_p2p_sd_queries;
3218 }
3219
Dmitry Shmidtde47be72016-01-07 12:52:55 -08003220 res = p2p_start_sd(p2p, dev);
3221 if (res == -2)
3222 return -2;
3223 if (res == 0)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003224 return 1;
3225
3226 if (dev->req_config_methods &&
3227 !(dev->flags & P2P_DEV_PD_FOR_JOIN)) {
3228 p2p_dbg(p2p, "Send pending Provision Discovery Request to "
3229 MACSTR " (config methods 0x%x)",
3230 MAC2STR(dev->info.p2p_device_addr),
3231 dev->req_config_methods);
3232 if (p2p_send_prov_disc_req(p2p, dev, 0, 0) == 0)
3233 return 1;
3234 }
3235
3236 return 0;
3237}
3238
3239
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003240void p2p_continue_find(struct p2p_data *p2p)
3241{
3242 struct p2p_device *dev;
Dmitry Shmidtde47be72016-01-07 12:52:55 -08003243 int found, res;
Dmitry Shmidt13ca8d82014-02-20 10:18:40 -08003244
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003245 p2p_set_state(p2p, P2P_SEARCH);
3246
3247 /* Continue from the device following the last iteration */
3248 found = 0;
3249 dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
3250 if (dev == p2p->last_p2p_find_oper) {
3251 found = 1;
3252 continue;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003253 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003254 if (!found)
3255 continue;
Dmitry Shmidtde47be72016-01-07 12:52:55 -08003256 res = p2p_pre_find_operation(p2p, dev);
3257 if (res > 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003258 p2p->last_p2p_find_oper = dev;
3259 return;
3260 }
Dmitry Shmidtde47be72016-01-07 12:52:55 -08003261 if (res == -2)
3262 goto skip_sd;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003263 }
3264
3265 /*
3266 * Wrap around to the beginning of the list and continue until the last
3267 * iteration device.
3268 */
3269 dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
Dmitry Shmidtde47be72016-01-07 12:52:55 -08003270 res = p2p_pre_find_operation(p2p, dev);
3271 if (res > 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003272 p2p->last_p2p_find_oper = dev;
3273 return;
3274 }
Dmitry Shmidtde47be72016-01-07 12:52:55 -08003275 if (res == -2)
3276 goto skip_sd;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003277 if (dev == p2p->last_p2p_find_oper)
3278 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003279 }
3280
Dmitry Shmidtde47be72016-01-07 12:52:55 -08003281skip_sd:
3282 os_memset(p2p->sd_query_no_ack, 0, ETH_ALEN);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003283 p2p_listen_in_find(p2p, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003284}
3285
3286
3287static void p2p_sd_cb(struct p2p_data *p2p, int success)
3288{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003289 p2p_dbg(p2p, "Service Discovery Query TX callback: success=%d",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003290 success);
3291 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
3292
3293 if (!success) {
Dmitry Shmidtde47be72016-01-07 12:52:55 -08003294 if (p2p->sd_peer) {
3295 if (is_zero_ether_addr(p2p->sd_query_no_ack)) {
3296 os_memcpy(p2p->sd_query_no_ack,
3297 p2p->sd_peer->info.p2p_device_addr,
3298 ETH_ALEN);
3299 p2p_dbg(p2p,
3300 "First SD Query no-ACK in this search iteration: "
3301 MACSTR, MAC2STR(p2p->sd_query_no_ack));
3302 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003303 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
Dmitry Shmidtde47be72016-01-07 12:52:55 -08003304 }
Dmitry Shmidt13ca8d82014-02-20 10:18:40 -08003305 p2p->sd_peer = NULL;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003306 if (p2p->state != P2P_IDLE)
3307 p2p_continue_find(p2p);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003308 return;
3309 }
3310
3311 if (p2p->sd_peer == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003312 p2p_dbg(p2p, "No SD peer entry known");
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003313 if (p2p->state != P2P_IDLE)
3314 p2p_continue_find(p2p);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003315 return;
3316 }
3317
Dmitry Shmidt807291d2015-01-27 13:40:23 -08003318 if (p2p->sd_query && p2p->sd_query->for_all_peers) {
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07003319 /* Update the pending broadcast SD query count for this device
3320 */
3321 p2p->sd_peer->sd_pending_bcast_queries--;
3322
3323 /*
3324 * If there are no pending broadcast queries for this device,
3325 * mark it as done (-1).
3326 */
3327 if (p2p->sd_peer->sd_pending_bcast_queries == 0)
3328 p2p->sd_peer->sd_pending_bcast_queries = -1;
3329 }
3330
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003331 /* Wait for response from the peer */
3332 p2p_set_state(p2p, P2P_SD_DURING_FIND);
3333 p2p_set_timeout(p2p, 0, 200000);
3334}
3335
Jouni Malinen75ecf522011-06-27 15:19:46 -07003336
3337/**
3338 * p2p_retry_pd - Retry any pending provision disc requests in IDLE state
3339 * @p2p: P2P module context from p2p_init()
3340 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003341static void p2p_retry_pd(struct p2p_data *p2p)
Jouni Malinen75ecf522011-06-27 15:19:46 -07003342{
3343 struct p2p_device *dev;
3344
Jouni Malinen75ecf522011-06-27 15:19:46 -07003345 /*
3346 * Retry the prov disc req attempt only for the peer that the user had
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003347 * requested.
Jouni Malinen75ecf522011-06-27 15:19:46 -07003348 */
3349
3350 dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
3351 if (os_memcmp(p2p->pending_pd_devaddr,
3352 dev->info.p2p_device_addr, ETH_ALEN) != 0)
3353 continue;
3354 if (!dev->req_config_methods)
3355 continue;
Jouni Malinen75ecf522011-06-27 15:19:46 -07003356
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003357 p2p_dbg(p2p, "Send pending Provision Discovery Request to "
Jouni Malinen75ecf522011-06-27 15:19:46 -07003358 MACSTR " (config methods 0x%x)",
3359 MAC2STR(dev->info.p2p_device_addr),
3360 dev->req_config_methods);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003361 p2p_send_prov_disc_req(p2p, dev,
Dmitry Shmidt051af732013-10-22 13:52:46 -07003362 dev->flags & P2P_DEV_PD_FOR_JOIN,
3363 p2p->pd_force_freq);
Jouni Malinen75ecf522011-06-27 15:19:46 -07003364 return;
3365 }
3366}
3367
3368
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003369static void p2p_prov_disc_cb(struct p2p_data *p2p, int success)
3370{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003371 p2p_dbg(p2p, "Provision Discovery Request TX callback: success=%d",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003372 success);
Jouni Malinen75ecf522011-06-27 15:19:46 -07003373
3374 /*
3375 * Postpone resetting the pending action state till after we actually
3376 * time out. This allows us to take some action like notifying any
3377 * interested parties about no response to the request.
3378 *
3379 * When the timer (below) goes off we check in IDLE, SEARCH, or
3380 * LISTEN_ONLY state, which are the only allowed states to issue a PD
3381 * requests in, if this was still pending and then raise notification.
3382 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003383
3384 if (!success) {
Jouni Malinen75ecf522011-06-27 15:19:46 -07003385 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
3386
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003387 if (p2p->user_initiated_pd &&
3388 (p2p->state == P2P_SEARCH || p2p->state == P2P_LISTEN_ONLY))
3389 {
3390 /* Retry request from timeout to avoid busy loops */
3391 p2p->pending_action_state = P2P_PENDING_PD;
3392 p2p_set_timeout(p2p, 0, 50000);
3393 } else if (p2p->state != P2P_IDLE)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003394 p2p_continue_find(p2p);
Jouni Malinen75ecf522011-06-27 15:19:46 -07003395 else if (p2p->user_initiated_pd) {
3396 p2p->pending_action_state = P2P_PENDING_PD;
3397 p2p_set_timeout(p2p, 0, 300000);
3398 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003399 return;
3400 }
3401
Jouni Malinen75ecf522011-06-27 15:19:46 -07003402 /*
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003403 * If after PD Request the peer doesn't expect to receive PD Response
3404 * the PD Request ACK indicates a completion of the current PD. This
3405 * happens only on the advertiser side sending the follow-on PD Request
3406 * with the status different than 12 (Success: accepted by user).
3407 */
3408 if (p2p->p2ps_prov && !p2p->p2ps_prov->pd_seeker &&
3409 p2p->p2ps_prov->status != P2P_SC_SUCCESS_DEFERRED) {
3410 p2p_dbg(p2p, "P2PS PD completion on Follow-on PD Request ACK");
3411
3412 if (p2p->send_action_in_progress) {
3413 p2p->send_action_in_progress = 0;
3414 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
3415 }
3416
3417 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
3418
3419 if (p2p->cfg->p2ps_prov_complete) {
3420 p2p->cfg->p2ps_prov_complete(
3421 p2p->cfg->cb_ctx,
3422 p2p->p2ps_prov->status,
3423 p2p->p2ps_prov->adv_mac,
3424 p2p->p2ps_prov->adv_mac,
3425 p2p->p2ps_prov->session_mac,
3426 NULL, p2p->p2ps_prov->adv_id,
3427 p2p->p2ps_prov->session_id,
3428 0, 0, NULL, 0, 0, 0,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08003429 NULL, NULL, 0, 0, NULL, 0);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003430 }
3431
3432 if (p2p->user_initiated_pd)
3433 p2p_reset_pending_pd(p2p);
3434
3435 p2ps_prov_free(p2p);
3436 return;
3437 }
3438
3439 /*
Jouni Malinen75ecf522011-06-27 15:19:46 -07003440 * This postponing, of resetting pending_action_state, needs to be
3441 * done only for user initiated PD requests and not internal ones.
3442 */
3443 if (p2p->user_initiated_pd)
3444 p2p->pending_action_state = P2P_PENDING_PD;
3445 else
3446 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
3447
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003448 /* Wait for response from the peer */
3449 if (p2p->state == P2P_SEARCH)
3450 p2p_set_state(p2p, P2P_PD_DURING_FIND);
3451 p2p_set_timeout(p2p, 0, 200000);
3452}
3453
3454
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003455static void p2p_prov_disc_resp_cb(struct p2p_data *p2p, int success)
3456{
3457 p2p_dbg(p2p, "Provision Discovery Response TX callback: success=%d",
3458 success);
3459
3460 if (p2p->send_action_in_progress) {
3461 p2p->send_action_in_progress = 0;
3462 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
3463 }
3464
3465 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
3466
Jimmy Chen5ef7aad2019-10-15 15:45:26 +08003467 if (!success) {
3468 if (p2p->state == P2P_SEARCH)
3469 p2p_continue_find(p2p);
Hai Shalom81f62d82019-07-22 12:10:00 -07003470 return;
Jimmy Chen5ef7aad2019-10-15 15:45:26 +08003471 }
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003472
3473 if (!p2p->cfg->prov_disc_resp_cb ||
Jimmy Chen5ef7aad2019-10-15 15:45:26 +08003474 p2p->cfg->prov_disc_resp_cb(p2p->cfg->cb_ctx) < 1) {
3475 if (p2p->state == P2P_SEARCH)
3476 p2p_continue_find(p2p);
Hai Shalom81f62d82019-07-22 12:10:00 -07003477 return;
Jimmy Chen5ef7aad2019-10-15 15:45:26 +08003478 }
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003479
3480 p2p_dbg(p2p,
3481 "Post-Provision Discovery operations started - do not try to continue other P2P operations");
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003482}
3483
3484
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003485int p2p_scan_res_handler(struct p2p_data *p2p, const u8 *bssid, int freq,
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003486 struct os_reltime *rx_time, int level, const u8 *ies,
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003487 size_t ies_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003488{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003489 if (os_reltime_before(rx_time, &p2p->find_start)) {
Dmitry Shmidtf8623282013-02-20 14:34:59 -08003490 /*
3491 * The driver may have cached (e.g., in cfg80211 BSS table) the
3492 * scan results for relatively long time. To avoid reporting
3493 * stale information, update P2P peers only based on results
3494 * that have based on frames received after the last p2p_find
3495 * operation was started.
3496 */
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003497 p2p_dbg(p2p, "Ignore old scan result for " MACSTR
Dmitry Shmidtde47be72016-01-07 12:52:55 -08003498 " (rx_time=%u.%06u find_start=%u.%06u)",
Dmitry Shmidtf8623282013-02-20 14:34:59 -08003499 MAC2STR(bssid), (unsigned int) rx_time->sec,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08003500 (unsigned int) rx_time->usec,
3501 (unsigned int) p2p->find_start.sec,
3502 (unsigned int) p2p->find_start.usec);
Dmitry Shmidtf8623282013-02-20 14:34:59 -08003503 return 0;
3504 }
3505
3506 p2p_add_device(p2p, bssid, freq, rx_time, level, ies, ies_len, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003507
3508 return 0;
3509}
3510
3511
3512void p2p_scan_res_handled(struct p2p_data *p2p)
3513{
3514 if (!p2p->p2p_scan_running) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003515 p2p_dbg(p2p, "p2p_scan was not running, but scan results received");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003516 }
3517 p2p->p2p_scan_running = 0;
3518 eloop_cancel_timeout(p2p_scan_timeout, p2p, NULL);
3519
3520 if (p2p_run_after_scan(p2p))
3521 return;
3522 if (p2p->state == P2P_SEARCH)
3523 p2p_continue_find(p2p);
3524}
3525
3526
Dmitry Shmidt9c175262016-03-03 10:20:07 -08003527void p2p_scan_ie(struct p2p_data *p2p, struct wpabuf *ies, const u8 *dev_id,
3528 unsigned int bands)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003529{
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003530 u8 dev_capab;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003531 u8 *len;
3532
3533#ifdef CONFIG_WIFI_DISPLAY
3534 if (p2p->wfd_ie_probe_req)
3535 wpabuf_put_buf(ies, p2p->wfd_ie_probe_req);
3536#endif /* CONFIG_WIFI_DISPLAY */
3537
Dmitry Shmidt2e67f062014-07-16 09:55:28 -07003538 if (p2p->vendor_elem && p2p->vendor_elem[VENDOR_ELEM_PROBE_REQ_P2P])
3539 wpabuf_put_buf(ies,
3540 p2p->vendor_elem[VENDOR_ELEM_PROBE_REQ_P2P]);
3541
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003542 len = p2p_buf_add_ie_hdr(ies);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003543
3544 dev_capab = p2p->dev_capab & ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
3545
3546 /* P2PS requires Probe Request frames to include SD bit */
3547 if (p2p->p2ps_seek && p2p->p2ps_seek_count)
3548 dev_capab |= P2P_DEV_CAPAB_SERVICE_DISCOVERY;
3549
3550 p2p_buf_add_capability(ies, dev_capab, 0);
3551
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003552 if (dev_id)
3553 p2p_buf_add_device_id(ies, dev_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003554 if (p2p->cfg->reg_class && p2p->cfg->channel)
3555 p2p_buf_add_listen_channel(ies, p2p->cfg->country,
3556 p2p->cfg->reg_class,
3557 p2p->cfg->channel);
3558 if (p2p->ext_listen_interval)
3559 p2p_buf_add_ext_listen_timing(ies, p2p->ext_listen_period,
3560 p2p->ext_listen_interval);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003561
Dmitry Shmidt9c175262016-03-03 10:20:07 -08003562 if (bands & BAND_60_GHZ)
3563 p2p_buf_add_device_info(ies, p2p, NULL);
3564
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003565 if (p2p->p2ps_seek && p2p->p2ps_seek_count)
3566 p2p_buf_add_service_hash(ies, p2p);
3567
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003568 /* TODO: p2p_buf_add_operating_channel() if GO */
3569 p2p_buf_update_ie_hdr(ies, len);
3570}
3571
3572
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003573size_t p2p_scan_ie_buf_len(struct p2p_data *p2p)
3574{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003575 size_t len = 100;
3576
3577#ifdef CONFIG_WIFI_DISPLAY
3578 if (p2p && p2p->wfd_ie_probe_req)
3579 len += wpabuf_len(p2p->wfd_ie_probe_req);
3580#endif /* CONFIG_WIFI_DISPLAY */
3581
Dmitry Shmidt2e67f062014-07-16 09:55:28 -07003582 if (p2p && p2p->vendor_elem &&
3583 p2p->vendor_elem[VENDOR_ELEM_PROBE_REQ_P2P])
3584 len += wpabuf_len(p2p->vendor_elem[VENDOR_ELEM_PROBE_REQ_P2P]);
3585
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003586 return len;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003587}
3588
3589
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003590int p2p_ie_text(struct wpabuf *p2p_ie, char *buf, char *end)
3591{
3592 return p2p_attr_text(p2p_ie, buf, end);
3593}
3594
3595
3596static void p2p_go_neg_req_cb(struct p2p_data *p2p, int success)
3597{
3598 struct p2p_device *dev = p2p->go_neg_peer;
Dmitry Shmidt8c652892013-03-01 10:14:01 -08003599 int timeout;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003600
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003601 p2p_dbg(p2p, "GO Negotiation Request TX callback: success=%d", success);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003602
3603 if (dev == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003604 p2p_dbg(p2p, "No pending GO Negotiation");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003605 return;
3606 }
3607
3608 if (success) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003609 if (dev->flags & P2P_DEV_USER_REJECTED) {
3610 p2p_set_state(p2p, P2P_IDLE);
3611 return;
3612 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07003613 } else if (dev->go_neg_req_sent) {
3614 /* Cancel the increment from p2p_connect_send() on failure */
Dmitry Shmidt98f9e762012-05-30 11:18:46 -07003615 dev->go_neg_req_sent--;
3616 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003617
3618 if (!success &&
3619 (dev->info.dev_capab & P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY) &&
3620 !is_zero_ether_addr(dev->member_in_go_dev)) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003621 p2p_dbg(p2p, "Peer " MACSTR " did not acknowledge request - try to use device discoverability through its GO",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003622 MAC2STR(dev->info.p2p_device_addr));
3623 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
3624 p2p_send_dev_disc_req(p2p, dev);
3625 return;
3626 }
3627
3628 /*
3629 * Use P2P find, if needed, to find the other device from its listen
3630 * channel.
3631 */
3632 p2p_set_state(p2p, P2P_CONNECT);
Dmitry Shmidt8c652892013-03-01 10:14:01 -08003633 timeout = success ? 500000 : 100000;
3634 if (!success && p2p->go_neg_peer &&
3635 (p2p->go_neg_peer->flags & P2P_DEV_PEER_WAITING_RESPONSE)) {
3636 unsigned int r;
3637 /*
3638 * Peer is expected to wait our response and we will skip the
3639 * listen phase. Add some randomness to the wait time here to
3640 * make it less likely to hit cases where we could end up in
3641 * sync with peer not listening.
3642 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07003643 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
3644 r = 0;
Dmitry Shmidt8c652892013-03-01 10:14:01 -08003645 timeout += r % 100000;
3646 }
3647 p2p_set_timeout(p2p, 0, timeout);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003648}
3649
3650
3651static void p2p_go_neg_resp_cb(struct p2p_data *p2p, int success)
3652{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003653 p2p_dbg(p2p, "GO Negotiation Response TX callback: success=%d",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003654 success);
3655 if (!p2p->go_neg_peer && p2p->state == P2P_PROVISIONING) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003656 p2p_dbg(p2p, "Ignore TX callback event - GO Negotiation is not running anymore");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003657 return;
3658 }
3659 p2p_set_state(p2p, P2P_CONNECT);
Dmitry Shmidt8c652892013-03-01 10:14:01 -08003660 p2p_set_timeout(p2p, 0, 500000);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003661}
3662
3663
Dmitry Shmidt4b060592013-04-29 16:42:49 -07003664static void p2p_go_neg_resp_failure_cb(struct p2p_data *p2p, int success,
3665 const u8 *addr)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003666{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003667 p2p_dbg(p2p, "GO Negotiation Response (failure) TX callback: success=%d", success);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003668 if (p2p->go_neg_peer && p2p->go_neg_peer->status != P2P_SC_SUCCESS) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003669 p2p_go_neg_failed(p2p, p2p->go_neg_peer->status);
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08003670 return;
3671 }
3672
3673 if (success) {
Dmitry Shmidt4b060592013-04-29 16:42:49 -07003674 struct p2p_device *dev;
3675 dev = p2p_get_device(p2p, addr);
3676 if (dev &&
Dmitry Shmidt34c12022015-03-05 14:11:20 -08003677 dev->status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE)
Dmitry Shmidt4b060592013-04-29 16:42:49 -07003678 dev->flags |= P2P_DEV_PEER_WAITING_RESPONSE;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003679 }
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08003680
3681 if (p2p->state == P2P_SEARCH || p2p->state == P2P_SD_DURING_FIND)
3682 p2p_continue_find(p2p);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003683}
3684
3685
3686static void p2p_go_neg_conf_cb(struct p2p_data *p2p,
3687 enum p2p_send_action_result result)
3688{
3689 struct p2p_device *dev;
3690
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003691 p2p_dbg(p2p, "GO Negotiation Confirm TX callback: result=%d", result);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003692 if (result == P2P_SEND_ACTION_FAILED) {
Dmitry Shmidt413dde72014-04-11 10:23:22 -07003693 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003694 p2p_go_neg_failed(p2p, -1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003695 return;
3696 }
Dmitry Shmidt413dde72014-04-11 10:23:22 -07003697
3698 dev = p2p->go_neg_peer;
3699
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003700 if (result == P2P_SEND_ACTION_NO_ACK) {
3701 /*
Dmitry Shmidt413dde72014-04-11 10:23:22 -07003702 * Retry GO Negotiation Confirmation
3703 * P2P_GO_NEG_CNF_MAX_RETRY_COUNT times if we did not receive
3704 * ACK for confirmation.
3705 */
3706 if (dev && dev->go_neg_conf &&
3707 dev->go_neg_conf_sent <= P2P_GO_NEG_CNF_MAX_RETRY_COUNT) {
3708 p2p_dbg(p2p, "GO Negotiation Confirm retry %d",
3709 dev->go_neg_conf_sent);
3710 p2p->pending_action_state = P2P_PENDING_GO_NEG_CONFIRM;
3711 if (p2p_send_action(p2p, dev->go_neg_conf_freq,
3712 dev->info.p2p_device_addr,
3713 p2p->cfg->dev_addr,
3714 dev->info.p2p_device_addr,
3715 wpabuf_head(dev->go_neg_conf),
3716 wpabuf_len(dev->go_neg_conf), 0) >=
3717 0) {
3718 dev->go_neg_conf_sent++;
3719 return;
3720 }
3721 p2p_dbg(p2p, "Failed to re-send Action frame");
3722
3723 /*
3724 * Continue with the assumption that the first attempt
3725 * went through and just the ACK frame was lost.
3726 */
3727 }
3728
3729 /*
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003730 * It looks like the TX status for GO Negotiation Confirm is
3731 * often showing failure even when the peer has actually
3732 * received the frame. Since the peer may change channels
3733 * immediately after having received the frame, we may not see
3734 * an Ack for retries, so just dropping a single frame may
3735 * trigger this. To allow the group formation to succeed if the
3736 * peer did indeed receive the frame, continue regardless of
3737 * the TX status.
3738 */
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003739 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 -07003740 }
3741
Dmitry Shmidt413dde72014-04-11 10:23:22 -07003742 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
3743
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003744 if (dev == NULL)
3745 return;
3746
3747 p2p_go_complete(p2p, dev);
3748}
3749
3750
3751void p2p_send_action_cb(struct p2p_data *p2p, unsigned int freq, const u8 *dst,
3752 const u8 *src, const u8 *bssid,
3753 enum p2p_send_action_result result)
3754{
3755 enum p2p_pending_action_state state;
3756 int success;
3757
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003758 p2p_dbg(p2p, "Action frame TX callback (state=%d freq=%u dst=" MACSTR
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003759 " src=" MACSTR " bssid=" MACSTR " result=%d p2p_state=%s)",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003760 p2p->pending_action_state, freq, MAC2STR(dst), MAC2STR(src),
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003761 MAC2STR(bssid), result, p2p_state_txt(p2p->state));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003762 success = result == P2P_SEND_ACTION_SUCCESS;
3763 state = p2p->pending_action_state;
3764 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
3765 switch (state) {
3766 case P2P_NO_PENDING_ACTION:
Dmitry Shmidt4582d2a2014-02-28 11:14:23 -08003767 if (p2p->send_action_in_progress) {
3768 p2p->send_action_in_progress = 0;
3769 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
3770 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003771 break;
3772 case P2P_PENDING_GO_NEG_REQUEST:
3773 p2p_go_neg_req_cb(p2p, success);
3774 break;
3775 case P2P_PENDING_GO_NEG_RESPONSE:
3776 p2p_go_neg_resp_cb(p2p, success);
3777 break;
3778 case P2P_PENDING_GO_NEG_RESPONSE_FAILURE:
Dmitry Shmidt4b060592013-04-29 16:42:49 -07003779 p2p_go_neg_resp_failure_cb(p2p, success, dst);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003780 break;
3781 case P2P_PENDING_GO_NEG_CONFIRM:
3782 p2p_go_neg_conf_cb(p2p, result);
3783 break;
3784 case P2P_PENDING_SD:
3785 p2p_sd_cb(p2p, success);
3786 break;
3787 case P2P_PENDING_PD:
3788 p2p_prov_disc_cb(p2p, success);
3789 break;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003790 case P2P_PENDING_PD_RESPONSE:
3791 p2p_prov_disc_resp_cb(p2p, success);
3792 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003793 case P2P_PENDING_INVITATION_REQUEST:
3794 p2p_invitation_req_cb(p2p, success);
3795 break;
3796 case P2P_PENDING_INVITATION_RESPONSE:
3797 p2p_invitation_resp_cb(p2p, success);
3798 break;
3799 case P2P_PENDING_DEV_DISC_REQUEST:
3800 p2p_dev_disc_req_cb(p2p, success);
3801 break;
3802 case P2P_PENDING_DEV_DISC_RESPONSE:
3803 p2p_dev_disc_resp_cb(p2p, success);
3804 break;
3805 case P2P_PENDING_GO_DISC_REQ:
3806 p2p_go_disc_req_cb(p2p, success);
3807 break;
3808 }
3809}
3810
3811
3812void p2p_listen_cb(struct p2p_data *p2p, unsigned int freq,
3813 unsigned int duration)
3814{
3815 if (freq == p2p->pending_client_disc_freq) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003816 p2p_dbg(p2p, "Client discoverability remain-awake completed");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003817 p2p->pending_client_disc_freq = 0;
3818 return;
3819 }
3820
3821 if (freq != p2p->pending_listen_freq) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003822 p2p_dbg(p2p, "Unexpected listen callback for freq=%u duration=%u (pending_listen_freq=%u)",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003823 freq, duration, p2p->pending_listen_freq);
3824 return;
3825 }
3826
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003827 p2p_dbg(p2p, "Starting Listen timeout(%u,%u) on freq=%u based on callback",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003828 p2p->pending_listen_sec, p2p->pending_listen_usec,
3829 p2p->pending_listen_freq);
3830 p2p->in_listen = 1;
3831 p2p->drv_in_listen = freq;
3832 if (p2p->pending_listen_sec || p2p->pending_listen_usec) {
3833 /*
3834 * Add 20 msec extra wait to avoid race condition with driver
3835 * remain-on-channel end event, i.e., give driver more time to
3836 * complete the operation before our timeout expires.
3837 */
3838 p2p_set_timeout(p2p, p2p->pending_listen_sec,
3839 p2p->pending_listen_usec + 20000);
3840 }
3841
3842 p2p->pending_listen_freq = 0;
3843}
3844
3845
3846int p2p_listen_end(struct p2p_data *p2p, unsigned int freq)
3847{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003848 p2p_dbg(p2p, "Driver ended Listen state (freq=%u)", freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003849 p2p->drv_in_listen = 0;
3850 if (p2p->in_listen)
3851 return 0; /* Internal timeout will trigger the next step */
3852
Roshan Pius3a1667e2018-07-03 15:17:14 -07003853 if (p2p->state == P2P_WAIT_PEER_CONNECT && p2p->go_neg_peer &&
3854 p2p->pending_listen_freq) {
3855 /*
3856 * Better wait a bit if the driver is unable to start
3857 * offchannel operation for some reason to continue with
3858 * P2P_WAIT_PEER_(IDLE/CONNECT) state transitions.
3859 */
3860 p2p_dbg(p2p,
3861 "Listen operation did not seem to start - delay idle phase to avoid busy loop");
3862 p2p_set_timeout(p2p, 0, 100000);
3863 return 1;
3864 }
3865
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003866 if (p2p->state == P2P_CONNECT_LISTEN && p2p->go_neg_peer) {
3867 if (p2p->go_neg_peer->connect_reqs >= 120) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003868 p2p_dbg(p2p, "Timeout on sending GO Negotiation Request without getting response");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003869 p2p_go_neg_failed(p2p, -1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003870 return 0;
3871 }
3872
3873 p2p_set_state(p2p, P2P_CONNECT);
3874 p2p_connect_send(p2p, p2p->go_neg_peer);
3875 return 1;
3876 } else if (p2p->state == P2P_SEARCH) {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003877 if (p2p->p2p_scan_running) {
3878 /*
3879 * Search is already in progress. This can happen if
3880 * an Action frame RX is reported immediately after
3881 * the end of a remain-on-channel operation and the
3882 * response frame to that is sent using an offchannel
3883 * operation while in p2p_find. Avoid an attempt to
3884 * restart a scan here.
3885 */
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003886 p2p_dbg(p2p, "p2p_scan already in progress - do not try to start a new one");
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003887 return 1;
3888 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07003889 if (p2p->pending_listen_freq) {
3890 /*
3891 * Better wait a bit if the driver is unable to start
3892 * offchannel operation for some reason. p2p_search()
3893 * will be started from internal timeout.
3894 */
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003895 p2p_dbg(p2p, "Listen operation did not seem to start - delay search phase to avoid busy loop");
Dmitry Shmidt04949592012-07-19 12:16:46 -07003896 p2p_set_timeout(p2p, 0, 100000);
3897 return 1;
3898 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003899 if (p2p->search_delay) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003900 p2p_dbg(p2p, "Delay search operation by %u ms",
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003901 p2p->search_delay);
3902 p2p_set_timeout(p2p, p2p->search_delay / 1000,
3903 (p2p->search_delay % 1000) * 1000);
3904 return 1;
3905 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003906 p2p_search(p2p);
3907 return 1;
3908 }
3909
3910 return 0;
3911}
3912
3913
3914static void p2p_timeout_connect(struct p2p_data *p2p)
3915{
3916 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003917 if (p2p->go_neg_peer &&
3918 (p2p->go_neg_peer->flags & P2P_DEV_WAIT_GO_NEG_CONFIRM)) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003919 p2p_dbg(p2p, "Wait for GO Negotiation Confirm timed out - assume GO Negotiation failed");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003920 p2p_go_neg_failed(p2p, -1);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003921 return;
3922 }
Dmitry Shmidt8c652892013-03-01 10:14:01 -08003923 if (p2p->go_neg_peer &&
3924 (p2p->go_neg_peer->flags & P2P_DEV_PEER_WAITING_RESPONSE) &&
3925 p2p->go_neg_peer->connect_reqs < 120) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003926 p2p_dbg(p2p, "Peer expected to wait our response - skip listen");
Dmitry Shmidt8c652892013-03-01 10:14:01 -08003927 p2p_connect_send(p2p, p2p->go_neg_peer);
3928 return;
3929 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003930 if (p2p->go_neg_peer && p2p->go_neg_peer->oob_go_neg_freq > 0) {
3931 p2p_dbg(p2p, "Skip connect-listen since GO Neg channel known (OOB)");
3932 p2p_set_state(p2p, P2P_CONNECT_LISTEN);
3933 p2p_set_timeout(p2p, 0, 30000);
3934 return;
3935 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003936 p2p_set_state(p2p, P2P_CONNECT_LISTEN);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003937 p2p_listen_in_find(p2p, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003938}
3939
3940
3941static void p2p_timeout_connect_listen(struct p2p_data *p2p)
3942{
3943 if (p2p->go_neg_peer) {
3944 if (p2p->drv_in_listen) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003945 p2p_dbg(p2p, "Driver is still in Listen state; wait for it to complete");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003946 return;
3947 }
3948
3949 if (p2p->go_neg_peer->connect_reqs >= 120) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003950 p2p_dbg(p2p, "Timeout on sending GO Negotiation Request without getting response");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003951 p2p_go_neg_failed(p2p, -1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003952 return;
3953 }
3954
3955 p2p_set_state(p2p, P2P_CONNECT);
3956 p2p_connect_send(p2p, p2p->go_neg_peer);
3957 } else
3958 p2p_set_state(p2p, P2P_IDLE);
3959}
3960
3961
3962static void p2p_timeout_wait_peer_connect(struct p2p_data *p2p)
3963{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003964 p2p_set_state(p2p, P2P_WAIT_PEER_IDLE);
Dmitry Shmidt18463232014-01-24 12:29:41 -08003965
3966 if (p2p->cfg->is_concurrent_session_active &&
3967 p2p->cfg->is_concurrent_session_active(p2p->cfg->cb_ctx))
3968 p2p_set_timeout(p2p, 0, 500000);
3969 else
3970 p2p_set_timeout(p2p, 0, 200000);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003971}
3972
3973
3974static void p2p_timeout_wait_peer_idle(struct p2p_data *p2p)
3975{
3976 struct p2p_device *dev = p2p->go_neg_peer;
3977
3978 if (dev == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003979 p2p_dbg(p2p, "Unknown GO Neg peer - stop GO Neg wait");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003980 return;
3981 }
3982
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003983 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 -07003984 p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003985 p2p_set_state(p2p, P2P_WAIT_PEER_CONNECT);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003986 p2p_listen_in_find(p2p, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003987}
3988
3989
3990static void p2p_timeout_sd_during_find(struct p2p_data *p2p)
3991{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003992 p2p_dbg(p2p, "Service Discovery Query timeout");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003993 if (p2p->sd_peer) {
3994 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003995 p2p->sd_peer = NULL;
3996 }
3997 p2p_continue_find(p2p);
3998}
3999
4000
4001static void p2p_timeout_prov_disc_during_find(struct p2p_data *p2p)
4002{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004003 p2p_dbg(p2p, "Provision Discovery Request timeout");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004004 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
4005 p2p_continue_find(p2p);
4006}
4007
4008
Jouni Malinen75ecf522011-06-27 15:19:46 -07004009static void p2p_timeout_prov_disc_req(struct p2p_data *p2p)
4010{
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004011 u32 adv_id = 0;
4012 u8 *adv_mac = NULL;
4013
Jouni Malinen75ecf522011-06-27 15:19:46 -07004014 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
4015
4016 /*
4017 * For user initiated PD requests that we have not gotten any responses
4018 * for while in IDLE state, we retry them a couple of times before
4019 * giving up.
4020 */
4021 if (!p2p->user_initiated_pd)
4022 return;
4023
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004024 p2p_dbg(p2p, "User initiated Provision Discovery Request timeout");
Jouni Malinen75ecf522011-06-27 15:19:46 -07004025
4026 if (p2p->pd_retries) {
4027 p2p->pd_retries--;
4028 p2p_retry_pd(p2p);
4029 } else {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004030 struct p2p_device *dev;
4031 int for_join = 0;
4032
4033 dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
4034 if (os_memcmp(p2p->pending_pd_devaddr,
4035 dev->info.p2p_device_addr, ETH_ALEN) != 0)
4036 continue;
4037 if (dev->req_config_methods &&
4038 (dev->flags & P2P_DEV_PD_FOR_JOIN))
4039 for_join = 1;
4040 }
4041
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004042 if (p2p->p2ps_prov) {
4043 adv_id = p2p->p2ps_prov->adv_id;
4044 adv_mac = p2p->p2ps_prov->adv_mac;
4045 }
4046
Jouni Malinen75ecf522011-06-27 15:19:46 -07004047 if (p2p->cfg->prov_disc_fail)
4048 p2p->cfg->prov_disc_fail(p2p->cfg->cb_ctx,
4049 p2p->pending_pd_devaddr,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004050 for_join ?
4051 P2P_PROV_DISC_TIMEOUT_JOIN :
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004052 P2P_PROV_DISC_TIMEOUT,
4053 adv_id, adv_mac, NULL);
Jouni Malinen75ecf522011-06-27 15:19:46 -07004054 p2p_reset_pending_pd(p2p);
4055 }
4056}
4057
4058
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004059static void p2p_timeout_invite(struct p2p_data *p2p)
4060{
4061 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
4062 p2p_set_state(p2p, P2P_INVITE_LISTEN);
4063 if (p2p->inv_role == P2P_INVITE_ROLE_ACTIVE_GO) {
4064 /*
4065 * Better remain on operating channel instead of listen channel
4066 * when running a group.
4067 */
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004068 p2p_dbg(p2p, "Inviting in active GO role - wait on operating channel");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004069 p2p_set_timeout(p2p, 0, 100000);
4070 return;
4071 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004072 p2p_listen_in_find(p2p, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004073}
4074
4075
4076static void p2p_timeout_invite_listen(struct p2p_data *p2p)
4077{
4078 if (p2p->invite_peer && p2p->invite_peer->invitation_reqs < 100) {
4079 p2p_set_state(p2p, P2P_INVITE);
4080 p2p_invite_send(p2p, p2p->invite_peer,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004081 p2p->invite_go_dev_addr, p2p->invite_dev_pw_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004082 } else {
4083 if (p2p->invite_peer) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004084 p2p_dbg(p2p, "Invitation Request retry limit reached");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004085 if (p2p->cfg->invitation_result)
4086 p2p->cfg->invitation_result(
Dmitry Shmidt700a1372013-03-15 14:14:44 -07004087 p2p->cfg->cb_ctx, -1, NULL, NULL,
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004088 p2p->invite_peer->info.p2p_device_addr,
Dmitry Shmidt15907092014-03-25 10:42:57 -07004089 0, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004090 }
4091 p2p_set_state(p2p, P2P_IDLE);
4092 }
4093}
4094
4095
4096static void p2p_state_timeout(void *eloop_ctx, void *timeout_ctx)
4097{
4098 struct p2p_data *p2p = eloop_ctx;
4099
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004100 p2p_dbg(p2p, "Timeout (state=%s)", p2p_state_txt(p2p->state));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004101
4102 p2p->in_listen = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004103 if (p2p->drv_in_listen) {
4104 p2p_dbg(p2p, "Driver is still in listen state - stop it");
4105 p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
4106 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004107
4108 switch (p2p->state) {
4109 case P2P_IDLE:
Jouni Malinen75ecf522011-06-27 15:19:46 -07004110 /* Check if we timed out waiting for PD req */
4111 if (p2p->pending_action_state == P2P_PENDING_PD)
4112 p2p_timeout_prov_disc_req(p2p);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004113 break;
4114 case P2P_SEARCH:
Jouni Malinen75ecf522011-06-27 15:19:46 -07004115 /* Check if we timed out waiting for PD req */
4116 if (p2p->pending_action_state == P2P_PENDING_PD)
4117 p2p_timeout_prov_disc_req(p2p);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004118 if (p2p->search_delay && !p2p->in_search_delay) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004119 p2p_dbg(p2p, "Delay search operation by %u ms",
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004120 p2p->search_delay);
4121 p2p->in_search_delay = 1;
4122 p2p_set_timeout(p2p, p2p->search_delay / 1000,
4123 (p2p->search_delay % 1000) * 1000);
4124 break;
4125 }
4126 p2p->in_search_delay = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004127 p2p_search(p2p);
4128 break;
4129 case P2P_CONNECT:
4130 p2p_timeout_connect(p2p);
4131 break;
4132 case P2P_CONNECT_LISTEN:
4133 p2p_timeout_connect_listen(p2p);
4134 break;
4135 case P2P_GO_NEG:
4136 break;
4137 case P2P_LISTEN_ONLY:
Jouni Malinen75ecf522011-06-27 15:19:46 -07004138 /* Check if we timed out waiting for PD req */
4139 if (p2p->pending_action_state == P2P_PENDING_PD)
4140 p2p_timeout_prov_disc_req(p2p);
4141
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004142 if (p2p->ext_listen_only) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004143 p2p_dbg(p2p, "Extended Listen Timing - Listen State completed");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004144 p2p->ext_listen_only = 0;
4145 p2p_set_state(p2p, P2P_IDLE);
4146 }
4147 break;
4148 case P2P_WAIT_PEER_CONNECT:
4149 p2p_timeout_wait_peer_connect(p2p);
4150 break;
4151 case P2P_WAIT_PEER_IDLE:
4152 p2p_timeout_wait_peer_idle(p2p);
4153 break;
4154 case P2P_SD_DURING_FIND:
4155 p2p_timeout_sd_during_find(p2p);
4156 break;
4157 case P2P_PROVISIONING:
4158 break;
4159 case P2P_PD_DURING_FIND:
4160 p2p_timeout_prov_disc_during_find(p2p);
4161 break;
4162 case P2P_INVITE:
4163 p2p_timeout_invite(p2p);
4164 break;
4165 case P2P_INVITE_LISTEN:
4166 p2p_timeout_invite_listen(p2p);
4167 break;
4168 }
4169}
4170
4171
4172int p2p_reject(struct p2p_data *p2p, const u8 *peer_addr)
4173{
4174 struct p2p_device *dev;
4175
4176 dev = p2p_get_device(p2p, peer_addr);
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004177 p2p_dbg(p2p, "Local request to reject connection attempts by peer "
4178 MACSTR, MAC2STR(peer_addr));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004179 if (dev == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004180 p2p_dbg(p2p, "Peer " MACSTR " unknown", MAC2STR(peer_addr));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004181 return -1;
4182 }
4183 dev->status = P2P_SC_FAIL_REJECTED_BY_USER;
4184 dev->flags |= P2P_DEV_USER_REJECTED;
4185 return 0;
4186}
4187
4188
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004189const char * p2p_wps_method_text(enum p2p_wps_method method)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004190{
4191 switch (method) {
4192 case WPS_NOT_READY:
4193 return "not-ready";
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004194 case WPS_PIN_DISPLAY:
4195 return "Display";
4196 case WPS_PIN_KEYPAD:
4197 return "Keypad";
4198 case WPS_PBC:
4199 return "PBC";
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004200 case WPS_NFC:
4201 return "NFC";
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004202 case WPS_P2PS:
4203 return "P2PS";
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004204 }
4205
4206 return "??";
4207}
4208
4209
4210static const char * p2p_go_state_text(enum p2p_go_state go_state)
4211{
4212 switch (go_state) {
4213 case UNKNOWN_GO:
4214 return "unknown";
4215 case LOCAL_GO:
4216 return "local";
4217 case REMOTE_GO:
4218 return "remote";
4219 }
4220
4221 return "??";
4222}
4223
4224
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004225const struct p2p_peer_info * p2p_get_peer_info(struct p2p_data *p2p,
4226 const u8 *addr, int next)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004227{
4228 struct p2p_device *dev;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004229
4230 if (addr)
4231 dev = p2p_get_device(p2p, addr);
4232 else
4233 dev = dl_list_first(&p2p->devices, struct p2p_device, list);
4234
4235 if (dev && next) {
4236 dev = dl_list_first(&dev->list, struct p2p_device, list);
4237 if (&dev->list == &p2p->devices)
4238 dev = NULL;
4239 }
4240
4241 if (dev == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004242 return NULL;
4243
4244 return &dev->info;
4245}
4246
4247
4248int p2p_get_peer_info_txt(const struct p2p_peer_info *info,
4249 char *buf, size_t buflen)
4250{
4251 struct p2p_device *dev;
4252 int res;
4253 char *pos, *end;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004254 struct os_reltime now;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004255
4256 if (info == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004257 return -1;
4258
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004259 dev = (struct p2p_device *) (((u8 *) info) -
4260 offsetof(struct p2p_device, info));
4261
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004262 pos = buf;
4263 end = buf + buflen;
4264
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004265 os_get_reltime(&now);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004266 res = os_snprintf(pos, end - pos,
4267 "age=%d\n"
4268 "listen_freq=%d\n"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004269 "wps_method=%s\n"
4270 "interface_addr=" MACSTR "\n"
4271 "member_in_go_dev=" MACSTR "\n"
4272 "member_in_go_iface=" MACSTR "\n"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004273 "go_neg_req_sent=%d\n"
4274 "go_state=%s\n"
4275 "dialog_token=%u\n"
4276 "intended_addr=" MACSTR "\n"
4277 "country=%c%c\n"
4278 "oper_freq=%d\n"
4279 "req_config_methods=0x%x\n"
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07004280 "flags=%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004281 "status=%d\n"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004282 "invitation_reqs=%u\n",
4283 (int) (now.sec - dev->last_seen.sec),
4284 dev->listen_freq,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004285 p2p_wps_method_text(dev->wps_method),
4286 MAC2STR(dev->interface_addr),
4287 MAC2STR(dev->member_in_go_dev),
4288 MAC2STR(dev->member_in_go_iface),
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004289 dev->go_neg_req_sent,
4290 p2p_go_state_text(dev->go_state),
4291 dev->dialog_token,
4292 MAC2STR(dev->intended_addr),
4293 dev->country[0] ? dev->country[0] : '_',
4294 dev->country[1] ? dev->country[1] : '_',
4295 dev->oper_freq,
4296 dev->req_config_methods,
4297 dev->flags & P2P_DEV_PROBE_REQ_ONLY ?
4298 "[PROBE_REQ_ONLY]" : "",
4299 dev->flags & P2P_DEV_REPORTED ? "[REPORTED]" : "",
4300 dev->flags & P2P_DEV_NOT_YET_READY ?
4301 "[NOT_YET_READY]" : "",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004302 dev->flags & P2P_DEV_PD_PEER_DISPLAY ?
4303 "[PD_PEER_DISPLAY]" : "",
4304 dev->flags & P2P_DEV_PD_PEER_KEYPAD ?
4305 "[PD_PEER_KEYPAD]" : "",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004306 dev->flags & P2P_DEV_PD_PEER_P2PS ?
4307 "[PD_PEER_P2PS]" : "",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004308 dev->flags & P2P_DEV_USER_REJECTED ?
4309 "[USER_REJECTED]" : "",
4310 dev->flags & P2P_DEV_PEER_WAITING_RESPONSE ?
4311 "[PEER_WAITING_RESPONSE]" : "",
4312 dev->flags & P2P_DEV_PREFER_PERSISTENT_GROUP ?
4313 "[PREFER_PERSISTENT_GROUP]" : "",
4314 dev->flags & P2P_DEV_WAIT_GO_NEG_RESPONSE ?
4315 "[WAIT_GO_NEG_RESPONSE]" : "",
4316 dev->flags & P2P_DEV_WAIT_GO_NEG_CONFIRM ?
4317 "[WAIT_GO_NEG_CONFIRM]" : "",
4318 dev->flags & P2P_DEV_GROUP_CLIENT_ONLY ?
4319 "[GROUP_CLIENT_ONLY]" : "",
4320 dev->flags & P2P_DEV_FORCE_FREQ ?
4321 "[FORCE_FREQ]" : "",
4322 dev->flags & P2P_DEV_PD_FOR_JOIN ?
4323 "[PD_FOR_JOIN]" : "",
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07004324 dev->flags & P2P_DEV_LAST_SEEN_AS_GROUP_CLIENT ?
4325 "[LAST_SEEN_AS_GROUP_CLIENT]" : "",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004326 dev->status,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004327 dev->invitation_reqs);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004328 if (os_snprintf_error(end - pos, res))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004329 return pos - buf;
4330 pos += res;
4331
4332 if (dev->ext_listen_period) {
4333 res = os_snprintf(pos, end - pos,
4334 "ext_listen_period=%u\n"
4335 "ext_listen_interval=%u\n",
4336 dev->ext_listen_period,
4337 dev->ext_listen_interval);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004338 if (os_snprintf_error(end - pos, res))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004339 return pos - buf;
4340 pos += res;
4341 }
4342
4343 if (dev->oper_ssid_len) {
4344 res = os_snprintf(pos, end - pos,
4345 "oper_ssid=%s\n",
4346 wpa_ssid_txt(dev->oper_ssid,
4347 dev->oper_ssid_len));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004348 if (os_snprintf_error(end - pos, res))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004349 return pos - buf;
4350 pos += res;
4351 }
4352
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004353#ifdef CONFIG_WIFI_DISPLAY
4354 if (dev->info.wfd_subelems) {
4355 res = os_snprintf(pos, end - pos, "wfd_subelems=");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004356 if (os_snprintf_error(end - pos, res))
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004357 return pos - buf;
4358 pos += res;
4359
4360 pos += wpa_snprintf_hex(pos, end - pos,
4361 wpabuf_head(dev->info.wfd_subelems),
4362 wpabuf_len(dev->info.wfd_subelems));
4363
4364 res = os_snprintf(pos, end - pos, "\n");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004365 if (os_snprintf_error(end - pos, res))
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004366 return pos - buf;
4367 pos += res;
4368 }
4369#endif /* CONFIG_WIFI_DISPLAY */
4370
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004371 return pos - buf;
4372}
4373
4374
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004375int p2p_peer_known(struct p2p_data *p2p, const u8 *addr)
4376{
4377 return p2p_get_device(p2p, addr) != NULL;
4378}
4379
4380
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004381void p2p_set_client_discoverability(struct p2p_data *p2p, int enabled)
4382{
4383 if (enabled) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004384 p2p_dbg(p2p, "Client discoverability enabled");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004385 p2p->dev_capab |= P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
4386 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004387 p2p_dbg(p2p, "Client discoverability disabled");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004388 p2p->dev_capab &= ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
4389 }
4390}
4391
4392
4393static struct wpabuf * p2p_build_presence_req(u32 duration1, u32 interval1,
4394 u32 duration2, u32 interval2)
4395{
4396 struct wpabuf *req;
4397 struct p2p_noa_desc desc1, desc2, *ptr1 = NULL, *ptr2 = NULL;
4398 u8 *len;
4399
4400 req = wpabuf_alloc(100);
4401 if (req == NULL)
4402 return NULL;
4403
4404 if (duration1 || interval1) {
4405 os_memset(&desc1, 0, sizeof(desc1));
4406 desc1.count_type = 1;
4407 desc1.duration = duration1;
4408 desc1.interval = interval1;
4409 ptr1 = &desc1;
4410
4411 if (duration2 || interval2) {
4412 os_memset(&desc2, 0, sizeof(desc2));
4413 desc2.count_type = 2;
4414 desc2.duration = duration2;
4415 desc2.interval = interval2;
4416 ptr2 = &desc2;
4417 }
4418 }
4419
4420 p2p_buf_add_action_hdr(req, P2P_PRESENCE_REQ, 1);
4421 len = p2p_buf_add_ie_hdr(req);
4422 p2p_buf_add_noa(req, 0, 0, 0, ptr1, ptr2);
4423 p2p_buf_update_ie_hdr(req, len);
4424
4425 return req;
4426}
4427
4428
4429int p2p_presence_req(struct p2p_data *p2p, const u8 *go_interface_addr,
4430 const u8 *own_interface_addr, unsigned int freq,
4431 u32 duration1, u32 interval1, u32 duration2,
4432 u32 interval2)
4433{
4434 struct wpabuf *req;
4435
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004436 p2p_dbg(p2p, "Send Presence Request to GO " MACSTR
4437 " (own interface " MACSTR ") freq=%u dur1=%u int1=%u "
4438 "dur2=%u int2=%u",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004439 MAC2STR(go_interface_addr), MAC2STR(own_interface_addr),
4440 freq, duration1, interval1, duration2, interval2);
4441
4442 req = p2p_build_presence_req(duration1, interval1, duration2,
4443 interval2);
4444 if (req == NULL)
4445 return -1;
4446
4447 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
4448 if (p2p_send_action(p2p, freq, go_interface_addr, own_interface_addr,
4449 go_interface_addr,
4450 wpabuf_head(req), wpabuf_len(req), 200) < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004451 p2p_dbg(p2p, "Failed to send Action frame");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004452 }
4453 wpabuf_free(req);
4454
4455 return 0;
4456}
4457
4458
4459static struct wpabuf * p2p_build_presence_resp(u8 status, const u8 *noa,
4460 size_t noa_len, u8 dialog_token)
4461{
4462 struct wpabuf *resp;
4463 u8 *len;
4464
4465 resp = wpabuf_alloc(100 + noa_len);
4466 if (resp == NULL)
4467 return NULL;
4468
4469 p2p_buf_add_action_hdr(resp, P2P_PRESENCE_RESP, dialog_token);
4470 len = p2p_buf_add_ie_hdr(resp);
4471 p2p_buf_add_status(resp, status);
4472 if (noa) {
4473 wpabuf_put_u8(resp, P2P_ATTR_NOTICE_OF_ABSENCE);
4474 wpabuf_put_le16(resp, noa_len);
4475 wpabuf_put_data(resp, noa, noa_len);
4476 } else
4477 p2p_buf_add_noa(resp, 0, 0, 0, NULL, NULL);
4478 p2p_buf_update_ie_hdr(resp, len);
4479
4480 return resp;
4481}
4482
4483
4484static void p2p_process_presence_req(struct p2p_data *p2p, const u8 *da,
4485 const u8 *sa, const u8 *data, size_t len,
4486 int rx_freq)
4487{
4488 struct p2p_message msg;
4489 u8 status;
4490 struct wpabuf *resp;
4491 size_t g;
4492 struct p2p_group *group = NULL;
4493 int parsed = 0;
4494 u8 noa[50];
4495 int noa_len;
4496
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004497 p2p_dbg(p2p, "Received P2P Action - P2P Presence Request");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004498
4499 for (g = 0; g < p2p->num_groups; g++) {
4500 if (os_memcmp(da, p2p_group_get_interface_addr(p2p->groups[g]),
4501 ETH_ALEN) == 0) {
4502 group = p2p->groups[g];
4503 break;
4504 }
4505 }
4506 if (group == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004507 p2p_dbg(p2p, "Ignore P2P Presence Request for unknown group "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004508 MACSTR, MAC2STR(da));
4509 return;
4510 }
4511
4512 if (p2p_parse(data, len, &msg) < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004513 p2p_dbg(p2p, "Failed to parse P2P Presence Request");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004514 status = P2P_SC_FAIL_INVALID_PARAMS;
4515 goto fail;
4516 }
4517 parsed = 1;
4518
4519 if (msg.noa == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004520 p2p_dbg(p2p, "No NoA attribute in P2P Presence Request");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004521 status = P2P_SC_FAIL_INVALID_PARAMS;
4522 goto fail;
4523 }
4524
4525 status = p2p_group_presence_req(group, sa, msg.noa, msg.noa_len);
4526
4527fail:
4528 if (p2p->cfg->get_noa)
4529 noa_len = p2p->cfg->get_noa(p2p->cfg->cb_ctx, da, noa,
4530 sizeof(noa));
4531 else
4532 noa_len = -1;
4533 resp = p2p_build_presence_resp(status, noa_len > 0 ? noa : NULL,
4534 noa_len > 0 ? noa_len : 0,
4535 msg.dialog_token);
4536 if (parsed)
4537 p2p_parse_free(&msg);
4538 if (resp == NULL)
4539 return;
4540
4541 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
4542 if (p2p_send_action(p2p, rx_freq, sa, da, da,
4543 wpabuf_head(resp), wpabuf_len(resp), 200) < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004544 p2p_dbg(p2p, "Failed to send Action frame");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004545 }
4546 wpabuf_free(resp);
4547}
4548
4549
4550static void p2p_process_presence_resp(struct p2p_data *p2p, const u8 *da,
4551 const u8 *sa, const u8 *data, size_t len)
4552{
4553 struct p2p_message msg;
4554
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004555 p2p_dbg(p2p, "Received P2P Action - P2P Presence Response");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004556
4557 if (p2p_parse(data, len, &msg) < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004558 p2p_dbg(p2p, "Failed to parse P2P Presence Response");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004559 return;
4560 }
4561
4562 if (msg.status == NULL || msg.noa == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004563 p2p_dbg(p2p, "No Status or NoA attribute in P2P Presence Response");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004564 p2p_parse_free(&msg);
4565 return;
4566 }
4567
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004568 if (p2p->cfg->presence_resp) {
4569 p2p->cfg->presence_resp(p2p->cfg->cb_ctx, sa, *msg.status,
4570 msg.noa, msg.noa_len);
4571 }
4572
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004573 if (*msg.status) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004574 p2p_dbg(p2p, "P2P Presence Request was rejected: status %u",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004575 *msg.status);
4576 p2p_parse_free(&msg);
4577 return;
4578 }
4579
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004580 p2p_dbg(p2p, "P2P Presence Request was accepted");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004581 wpa_hexdump(MSG_DEBUG, "P2P: P2P Presence Response - NoA",
4582 msg.noa, msg.noa_len);
4583 /* TODO: process NoA */
4584 p2p_parse_free(&msg);
4585}
4586
4587
4588static void p2p_ext_listen_timeout(void *eloop_ctx, void *timeout_ctx)
4589{
4590 struct p2p_data *p2p = eloop_ctx;
4591
4592 if (p2p->ext_listen_interval) {
4593 /* Schedule next extended listen timeout */
4594 eloop_register_timeout(p2p->ext_listen_interval_sec,
4595 p2p->ext_listen_interval_usec,
4596 p2p_ext_listen_timeout, p2p, NULL);
4597 }
4598
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -07004599 if ((p2p->cfg->is_p2p_in_progress &&
4600 p2p->cfg->is_p2p_in_progress(p2p->cfg->cb_ctx)) ||
4601 (p2p->pending_action_state == P2P_PENDING_PD &&
4602 p2p->pd_retries > 0)) {
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -07004603 p2p_dbg(p2p, "Operation in progress - skip Extended Listen timeout (%s)",
4604 p2p_state_txt(p2p->state));
4605 return;
4606 }
4607
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004608 if (p2p->state == P2P_LISTEN_ONLY && p2p->ext_listen_only) {
4609 /*
4610 * This should not really happen, but it looks like the Listen
4611 * command may fail is something else (e.g., a scan) was
4612 * running at an inconvenient time. As a workaround, allow new
4613 * Extended Listen operation to be started.
4614 */
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004615 p2p_dbg(p2p, "Previous Extended Listen operation had not been completed - try again");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004616 p2p->ext_listen_only = 0;
4617 p2p_set_state(p2p, P2P_IDLE);
4618 }
4619
4620 if (p2p->state != P2P_IDLE) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004621 p2p_dbg(p2p, "Skip Extended Listen timeout in active state (%s)", p2p_state_txt(p2p->state));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004622 return;
4623 }
4624
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004625 p2p_dbg(p2p, "Extended Listen timeout");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004626 p2p->ext_listen_only = 1;
4627 if (p2p_listen(p2p, p2p->ext_listen_period) < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004628 p2p_dbg(p2p, "Failed to start Listen state for Extended Listen Timing");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004629 p2p->ext_listen_only = 0;
4630 }
4631}
4632
4633
4634int p2p_ext_listen(struct p2p_data *p2p, unsigned int period,
4635 unsigned int interval)
4636{
4637 if (period > 65535 || interval > 65535 || period > interval ||
4638 (period == 0 && interval > 0) || (period > 0 && interval == 0)) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004639 p2p_dbg(p2p, "Invalid Extended Listen Timing request: period=%u interval=%u",
4640 period, interval);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004641 return -1;
4642 }
4643
4644 eloop_cancel_timeout(p2p_ext_listen_timeout, p2p, NULL);
4645
4646 if (interval == 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004647 p2p_dbg(p2p, "Disabling Extended Listen Timing");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004648 p2p->ext_listen_period = 0;
4649 p2p->ext_listen_interval = 0;
4650 return 0;
4651 }
4652
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004653 p2p_dbg(p2p, "Enabling Extended Listen Timing: period %u msec, interval %u msec",
4654 period, interval);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004655 p2p->ext_listen_period = period;
4656 p2p->ext_listen_interval = interval;
4657 p2p->ext_listen_interval_sec = interval / 1000;
4658 p2p->ext_listen_interval_usec = (interval % 1000) * 1000;
4659
4660 eloop_register_timeout(p2p->ext_listen_interval_sec,
4661 p2p->ext_listen_interval_usec,
4662 p2p_ext_listen_timeout, p2p, NULL);
4663
4664 return 0;
4665}
4666
4667
4668void p2p_deauth_notif(struct p2p_data *p2p, const u8 *bssid, u16 reason_code,
4669 const u8 *ie, size_t ie_len)
4670{
4671 struct p2p_message msg;
4672
4673 if (bssid == NULL || ie == NULL)
4674 return;
4675
4676 os_memset(&msg, 0, sizeof(msg));
4677 if (p2p_parse_ies(ie, ie_len, &msg))
4678 return;
Dmitry Shmidt97672262014-02-03 13:02:54 -08004679 if (msg.minor_reason_code == NULL) {
4680 p2p_parse_free(&msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004681 return;
Dmitry Shmidt97672262014-02-03 13:02:54 -08004682 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004683
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004684 p2p_dbg(p2p, "Deauthentication notification BSSID " MACSTR
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004685 " reason_code=%u minor_reason_code=%u",
4686 MAC2STR(bssid), reason_code, *msg.minor_reason_code);
4687
4688 p2p_parse_free(&msg);
4689}
4690
4691
4692void p2p_disassoc_notif(struct p2p_data *p2p, const u8 *bssid, u16 reason_code,
4693 const u8 *ie, size_t ie_len)
4694{
4695 struct p2p_message msg;
4696
4697 if (bssid == NULL || ie == NULL)
4698 return;
4699
4700 os_memset(&msg, 0, sizeof(msg));
4701 if (p2p_parse_ies(ie, ie_len, &msg))
4702 return;
Dmitry Shmidt97672262014-02-03 13:02:54 -08004703 if (msg.minor_reason_code == NULL) {
4704 p2p_parse_free(&msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004705 return;
Dmitry Shmidt97672262014-02-03 13:02:54 -08004706 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004707
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004708 p2p_dbg(p2p, "Disassociation notification BSSID " MACSTR
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004709 " reason_code=%u minor_reason_code=%u",
4710 MAC2STR(bssid), reason_code, *msg.minor_reason_code);
4711
4712 p2p_parse_free(&msg);
4713}
4714
4715
4716void p2p_set_managed_oper(struct p2p_data *p2p, int enabled)
4717{
4718 if (enabled) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004719 p2p_dbg(p2p, "Managed P2P Device operations enabled");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004720 p2p->dev_capab |= P2P_DEV_CAPAB_INFRA_MANAGED;
4721 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004722 p2p_dbg(p2p, "Managed P2P Device operations disabled");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004723 p2p->dev_capab &= ~P2P_DEV_CAPAB_INFRA_MANAGED;
4724 }
4725}
4726
4727
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004728int p2p_config_get_random_social(struct p2p_config *p2p, u8 *op_class,
Hai Shalom74f70d42019-02-11 14:42:39 -08004729 u8 *op_channel,
4730 struct wpa_freq_range_list *avoid_list,
4731 struct wpa_freq_range_list *disallow_list)
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004732{
Hai Shalom74f70d42019-02-11 14:42:39 -08004733 return p2p_channel_random_social(&p2p->channels, op_class, op_channel,
4734 avoid_list, disallow_list);
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004735}
4736
4737
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004738int p2p_set_listen_channel(struct p2p_data *p2p, u8 reg_class, u8 channel,
4739 u8 forced)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004740{
Dmitry Shmidt4b060592013-04-29 16:42:49 -07004741 if (p2p_channel_to_freq(reg_class, channel) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004742 return -1;
4743
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004744 /*
4745 * Listen channel was set in configuration or set by control interface;
4746 * cannot override it.
4747 */
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004748 if (p2p->cfg->channel_forced && forced == 0) {
4749 p2p_dbg(p2p,
4750 "Listen channel was previously configured - do not override based on optimization");
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004751 return -1;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004752 }
4753
4754 p2p_dbg(p2p, "Set Listen channel: reg_class %u channel %u",
4755 reg_class, channel);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004756
4757 if (p2p->state == P2P_IDLE) {
4758 p2p->cfg->reg_class = reg_class;
4759 p2p->cfg->channel = channel;
4760 p2p->cfg->channel_forced = forced;
4761 } else {
4762 p2p_dbg(p2p, "Defer setting listen channel");
4763 p2p->pending_reg_class = reg_class;
4764 p2p->pending_channel = channel;
4765 p2p->pending_channel_forced = forced;
4766 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004767
4768 return 0;
4769}
4770
4771
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004772u8 p2p_get_listen_channel(struct p2p_data *p2p)
4773{
4774 return p2p->cfg->channel;
4775}
4776
4777
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004778int p2p_set_ssid_postfix(struct p2p_data *p2p, const u8 *postfix, size_t len)
4779{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004780 p2p_dbg(p2p, "New SSID postfix: %s", wpa_ssid_txt(postfix, len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004781 if (postfix == NULL) {
4782 p2p->cfg->ssid_postfix_len = 0;
4783 return 0;
4784 }
4785 if (len > sizeof(p2p->cfg->ssid_postfix))
4786 return -1;
4787 os_memcpy(p2p->cfg->ssid_postfix, postfix, len);
4788 p2p->cfg->ssid_postfix_len = len;
4789 return 0;
4790}
4791
4792
Jouni Malinen75ecf522011-06-27 15:19:46 -07004793int p2p_set_oper_channel(struct p2p_data *p2p, u8 op_reg_class, u8 op_channel,
4794 int cfg_op_channel)
4795{
Dmitry Shmidt4b060592013-04-29 16:42:49 -07004796 if (p2p_channel_to_freq(op_reg_class, op_channel) < 0)
Jouni Malinen75ecf522011-06-27 15:19:46 -07004797 return -1;
4798
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004799 p2p_dbg(p2p, "Set Operating channel: reg_class %u channel %u",
4800 op_reg_class, op_channel);
Jouni Malinen75ecf522011-06-27 15:19:46 -07004801 p2p->cfg->op_reg_class = op_reg_class;
4802 p2p->cfg->op_channel = op_channel;
4803 p2p->cfg->cfg_op_channel = cfg_op_channel;
4804 return 0;
4805}
4806
4807
Dmitry Shmidt04949592012-07-19 12:16:46 -07004808int p2p_set_pref_chan(struct p2p_data *p2p, unsigned int num_pref_chan,
4809 const struct p2p_channel *pref_chan)
4810{
4811 struct p2p_channel *n;
4812
4813 if (pref_chan) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004814 n = os_memdup(pref_chan,
4815 num_pref_chan * sizeof(struct p2p_channel));
Dmitry Shmidt04949592012-07-19 12:16:46 -07004816 if (n == NULL)
4817 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004818 } else
4819 n = NULL;
4820
4821 os_free(p2p->cfg->pref_chan);
4822 p2p->cfg->pref_chan = n;
4823 p2p->cfg->num_pref_chan = num_pref_chan;
4824
4825 return 0;
4826}
4827
4828
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004829int p2p_set_no_go_freq(struct p2p_data *p2p,
4830 const struct wpa_freq_range_list *list)
4831{
4832 struct wpa_freq_range *tmp;
4833
4834 if (list == NULL || list->num == 0) {
4835 os_free(p2p->no_go_freq.range);
4836 p2p->no_go_freq.range = NULL;
4837 p2p->no_go_freq.num = 0;
4838 return 0;
4839 }
4840
4841 tmp = os_calloc(list->num, sizeof(struct wpa_freq_range));
4842 if (tmp == NULL)
4843 return -1;
4844 os_memcpy(tmp, list->range, list->num * sizeof(struct wpa_freq_range));
4845 os_free(p2p->no_go_freq.range);
4846 p2p->no_go_freq.range = tmp;
4847 p2p->no_go_freq.num = list->num;
4848 p2p_dbg(p2p, "Updated no GO chan list");
4849
4850 return 0;
4851}
4852
4853
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004854int p2p_get_interface_addr(struct p2p_data *p2p, const u8 *dev_addr,
4855 u8 *iface_addr)
4856{
4857 struct p2p_device *dev = p2p_get_device(p2p, dev_addr);
4858 if (dev == NULL || is_zero_ether_addr(dev->interface_addr))
4859 return -1;
4860 os_memcpy(iface_addr, dev->interface_addr, ETH_ALEN);
4861 return 0;
4862}
4863
4864
4865int p2p_get_dev_addr(struct p2p_data *p2p, const u8 *iface_addr,
4866 u8 *dev_addr)
4867{
4868 struct p2p_device *dev = p2p_get_device_interface(p2p, iface_addr);
4869 if (dev == NULL)
4870 return -1;
4871 os_memcpy(dev_addr, dev->info.p2p_device_addr, ETH_ALEN);
4872 return 0;
4873}
4874
4875
4876void p2p_set_peer_filter(struct p2p_data *p2p, const u8 *addr)
4877{
4878 os_memcpy(p2p->peer_filter, addr, ETH_ALEN);
4879 if (is_zero_ether_addr(p2p->peer_filter))
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004880 p2p_dbg(p2p, "Disable peer filter");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004881 else
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004882 p2p_dbg(p2p, "Enable peer filter for " MACSTR,
4883 MAC2STR(p2p->peer_filter));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004884}
4885
4886
4887void p2p_set_cross_connect(struct p2p_data *p2p, int enabled)
4888{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004889 p2p_dbg(p2p, "Cross connection %s", enabled ? "enabled" : "disabled");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004890 if (p2p->cross_connect == enabled)
4891 return;
4892 p2p->cross_connect = enabled;
4893 /* TODO: may need to tear down any action group where we are GO(?) */
4894}
4895
4896
4897int p2p_get_oper_freq(struct p2p_data *p2p, const u8 *iface_addr)
4898{
4899 struct p2p_device *dev = p2p_get_device_interface(p2p, iface_addr);
4900 if (dev == NULL)
4901 return -1;
4902 if (dev->oper_freq <= 0)
4903 return -1;
4904 return dev->oper_freq;
4905}
4906
4907
4908void p2p_set_intra_bss_dist(struct p2p_data *p2p, int enabled)
4909{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004910 p2p_dbg(p2p, "Intra BSS distribution %s",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004911 enabled ? "enabled" : "disabled");
4912 p2p->cfg->p2p_intra_bss = enabled;
4913}
4914
4915
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004916void p2p_update_channel_list(struct p2p_data *p2p,
4917 const struct p2p_channels *chan,
4918 const struct p2p_channels *cli_chan)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004919{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004920 p2p_dbg(p2p, "Update channel list");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004921 os_memcpy(&p2p->cfg->channels, chan, sizeof(struct p2p_channels));
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004922 p2p_channels_dump(p2p, "channels", &p2p->cfg->channels);
4923 os_memcpy(&p2p->cfg->cli_channels, cli_chan,
4924 sizeof(struct p2p_channels));
4925 p2p_channels_dump(p2p, "cli_channels", &p2p->cfg->cli_channels);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004926}
4927
4928
4929int p2p_send_action(struct p2p_data *p2p, unsigned int freq, const u8 *dst,
4930 const u8 *src, const u8 *bssid, const u8 *buf,
4931 size_t len, unsigned int wait_time)
4932{
Hai Shalom021b0b52019-04-10 11:17:58 -07004933 int res, scheduled;
4934
Hai Shalom021b0b52019-04-10 11:17:58 -07004935 res = p2p->cfg->send_action(p2p->cfg->cb_ctx, freq, dst, src, bssid,
4936 buf, len, wait_time, &scheduled);
4937 if (res == 0 && scheduled && p2p->in_listen && freq > 0 &&
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08004938 p2p->drv_in_listen > 0 &&
Hai Shalom021b0b52019-04-10 11:17:58 -07004939 (unsigned int) p2p->drv_in_listen != freq) {
4940 p2p_dbg(p2p,
4941 "Stop listen on %d MHz to allow a frame to be sent immediately on %d MHz",
4942 p2p->drv_in_listen, freq);
4943 p2p_stop_listen_for_freq(p2p, freq);
4944 }
4945 return res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004946}
4947
4948
4949void p2p_set_best_channels(struct p2p_data *p2p, int freq_24, int freq_5,
4950 int freq_overall)
4951{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004952 p2p_dbg(p2p, "Best channel: 2.4 GHz: %d, 5 GHz: %d, overall: %d",
4953 freq_24, freq_5, freq_overall);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004954 p2p->best_freq_24 = freq_24;
4955 p2p->best_freq_5 = freq_5;
4956 p2p->best_freq_overall = freq_overall;
4957}
4958
4959
Dmitry Shmidt700a1372013-03-15 14:14:44 -07004960void p2p_set_own_freq_preference(struct p2p_data *p2p, int freq)
4961{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004962 p2p_dbg(p2p, "Own frequency preference: %d MHz", freq);
Dmitry Shmidt700a1372013-03-15 14:14:44 -07004963 p2p->own_freq_preference = freq;
4964}
4965
4966
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004967const u8 * p2p_get_go_neg_peer(struct p2p_data *p2p)
4968{
4969 if (p2p == NULL || p2p->go_neg_peer == NULL)
4970 return NULL;
4971 return p2p->go_neg_peer->info.p2p_device_addr;
4972}
4973
4974
4975const struct p2p_peer_info *
4976p2p_get_peer_found(struct p2p_data *p2p, const u8 *addr, int next)
4977{
4978 struct p2p_device *dev;
4979
4980 if (addr) {
4981 dev = p2p_get_device(p2p, addr);
4982 if (!dev)
4983 return NULL;
4984
4985 if (!next) {
4986 if (dev->flags & P2P_DEV_PROBE_REQ_ONLY)
4987 return NULL;
4988
4989 return &dev->info;
4990 } else {
4991 do {
4992 dev = dl_list_first(&dev->list,
4993 struct p2p_device,
4994 list);
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07004995 if (!dev || &dev->list == &p2p->devices)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004996 return NULL;
4997 } while (dev->flags & P2P_DEV_PROBE_REQ_ONLY);
4998 }
4999 } else {
5000 dev = dl_list_first(&p2p->devices, struct p2p_device, list);
5001 if (!dev)
5002 return NULL;
5003 while (dev->flags & P2P_DEV_PROBE_REQ_ONLY) {
5004 dev = dl_list_first(&dev->list,
5005 struct p2p_device,
5006 list);
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07005007 if (!dev || &dev->list == &p2p->devices)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005008 return NULL;
5009 }
5010 }
5011
5012 return &dev->info;
5013}
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005014
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005015
5016int p2p_in_progress(struct p2p_data *p2p)
5017{
5018 if (p2p == NULL)
5019 return 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005020 if (p2p->state == P2P_SEARCH)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005021 return 2;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005022 return p2p->state != P2P_IDLE && p2p->state != P2P_PROVISIONING;
5023}
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005024
5025
5026void p2p_set_config_timeout(struct p2p_data *p2p, u8 go_timeout,
5027 u8 client_timeout)
5028{
5029 if (p2p) {
5030 p2p->go_timeout = go_timeout;
5031 p2p->client_timeout = client_timeout;
5032 }
5033}
5034
5035
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005036#ifdef CONFIG_WIFI_DISPLAY
5037
5038static void p2p_update_wfd_ie_groups(struct p2p_data *p2p)
5039{
5040 size_t g;
5041 struct p2p_group *group;
5042
5043 for (g = 0; g < p2p->num_groups; g++) {
5044 group = p2p->groups[g];
Dmitry Shmidtb96dad42013-11-05 10:07:29 -08005045 p2p_group_force_beacon_update_ies(group);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005046 }
5047}
5048
5049
5050int p2p_set_wfd_ie_beacon(struct p2p_data *p2p, struct wpabuf *ie)
5051{
5052 wpabuf_free(p2p->wfd_ie_beacon);
5053 p2p->wfd_ie_beacon = ie;
5054 p2p_update_wfd_ie_groups(p2p);
5055 return 0;
5056}
5057
5058
5059int p2p_set_wfd_ie_probe_req(struct p2p_data *p2p, struct wpabuf *ie)
5060{
5061 wpabuf_free(p2p->wfd_ie_probe_req);
5062 p2p->wfd_ie_probe_req = ie;
5063 return 0;
5064}
5065
5066
5067int p2p_set_wfd_ie_probe_resp(struct p2p_data *p2p, struct wpabuf *ie)
5068{
5069 wpabuf_free(p2p->wfd_ie_probe_resp);
5070 p2p->wfd_ie_probe_resp = ie;
5071 p2p_update_wfd_ie_groups(p2p);
5072 return 0;
5073}
5074
5075
5076int p2p_set_wfd_ie_assoc_req(struct p2p_data *p2p, struct wpabuf *ie)
5077{
5078 wpabuf_free(p2p->wfd_ie_assoc_req);
5079 p2p->wfd_ie_assoc_req = ie;
5080 return 0;
5081}
5082
5083
5084int p2p_set_wfd_ie_invitation(struct p2p_data *p2p, struct wpabuf *ie)
5085{
5086 wpabuf_free(p2p->wfd_ie_invitation);
5087 p2p->wfd_ie_invitation = ie;
5088 return 0;
5089}
5090
5091
5092int p2p_set_wfd_ie_prov_disc_req(struct p2p_data *p2p, struct wpabuf *ie)
5093{
5094 wpabuf_free(p2p->wfd_ie_prov_disc_req);
5095 p2p->wfd_ie_prov_disc_req = ie;
5096 return 0;
5097}
5098
5099
5100int p2p_set_wfd_ie_prov_disc_resp(struct p2p_data *p2p, struct wpabuf *ie)
5101{
5102 wpabuf_free(p2p->wfd_ie_prov_disc_resp);
5103 p2p->wfd_ie_prov_disc_resp = ie;
5104 return 0;
5105}
5106
5107
5108int p2p_set_wfd_ie_go_neg(struct p2p_data *p2p, struct wpabuf *ie)
5109{
5110 wpabuf_free(p2p->wfd_ie_go_neg);
5111 p2p->wfd_ie_go_neg = ie;
5112 return 0;
5113}
5114
5115
5116int p2p_set_wfd_dev_info(struct p2p_data *p2p, const struct wpabuf *elem)
5117{
5118 wpabuf_free(p2p->wfd_dev_info);
5119 if (elem) {
5120 p2p->wfd_dev_info = wpabuf_dup(elem);
5121 if (p2p->wfd_dev_info == NULL)
5122 return -1;
5123 } else
5124 p2p->wfd_dev_info = NULL;
5125
5126 return 0;
5127}
5128
5129
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005130int p2p_set_wfd_r2_dev_info(struct p2p_data *p2p, const struct wpabuf *elem)
5131{
5132 wpabuf_free(p2p->wfd_r2_dev_info);
5133 if (elem) {
5134 p2p->wfd_r2_dev_info = wpabuf_dup(elem);
5135 if (p2p->wfd_r2_dev_info == NULL)
5136 return -1;
5137 } else
5138 p2p->wfd_r2_dev_info = NULL;
5139
5140 return 0;
5141}
5142
5143
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005144int p2p_set_wfd_assoc_bssid(struct p2p_data *p2p, const struct wpabuf *elem)
5145{
5146 wpabuf_free(p2p->wfd_assoc_bssid);
5147 if (elem) {
5148 p2p->wfd_assoc_bssid = wpabuf_dup(elem);
5149 if (p2p->wfd_assoc_bssid == NULL)
5150 return -1;
5151 } else
5152 p2p->wfd_assoc_bssid = NULL;
5153
5154 return 0;
5155}
5156
5157
5158int p2p_set_wfd_coupled_sink_info(struct p2p_data *p2p,
5159 const struct wpabuf *elem)
5160{
5161 wpabuf_free(p2p->wfd_coupled_sink_info);
5162 if (elem) {
5163 p2p->wfd_coupled_sink_info = wpabuf_dup(elem);
5164 if (p2p->wfd_coupled_sink_info == NULL)
5165 return -1;
5166 } else
5167 p2p->wfd_coupled_sink_info = NULL;
5168
5169 return 0;
5170}
5171
5172#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005173
5174
5175int p2p_set_disc_int(struct p2p_data *p2p, int min_disc_int, int max_disc_int,
5176 int max_disc_tu)
5177{
5178 if (min_disc_int > max_disc_int || min_disc_int < 0 || max_disc_int < 0)
5179 return -1;
5180
5181 p2p->min_disc_int = min_disc_int;
5182 p2p->max_disc_int = max_disc_int;
5183 p2p->max_disc_tu = max_disc_tu;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005184 p2p_dbg(p2p, "Set discoverable interval: min=%d max=%d max_tu=%d",
5185 min_disc_int, max_disc_int, max_disc_tu);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005186
5187 return 0;
5188}
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005189
5190
5191void p2p_dbg(struct p2p_data *p2p, const char *fmt, ...)
5192{
5193 va_list ap;
5194 char buf[500];
5195
5196 if (!p2p->cfg->debug_print)
5197 return;
5198
5199 va_start(ap, fmt);
5200 vsnprintf(buf, sizeof(buf), fmt, ap);
5201 buf[sizeof(buf) - 1] = '\0';
5202 va_end(ap);
5203 p2p->cfg->debug_print(p2p->cfg->cb_ctx, MSG_DEBUG, buf);
5204}
5205
5206
5207void p2p_info(struct p2p_data *p2p, const char *fmt, ...)
5208{
5209 va_list ap;
5210 char buf[500];
5211
5212 if (!p2p->cfg->debug_print)
5213 return;
5214
5215 va_start(ap, fmt);
5216 vsnprintf(buf, sizeof(buf), fmt, ap);
5217 buf[sizeof(buf) - 1] = '\0';
5218 va_end(ap);
5219 p2p->cfg->debug_print(p2p->cfg->cb_ctx, MSG_INFO, buf);
5220}
5221
5222
5223void p2p_err(struct p2p_data *p2p, const char *fmt, ...)
5224{
5225 va_list ap;
5226 char buf[500];
5227
5228 if (!p2p->cfg->debug_print)
5229 return;
5230
5231 va_start(ap, fmt);
5232 vsnprintf(buf, sizeof(buf), fmt, ap);
5233 buf[sizeof(buf) - 1] = '\0';
5234 va_end(ap);
5235 p2p->cfg->debug_print(p2p->cfg->cb_ctx, MSG_ERROR, buf);
5236}
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005237
5238
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07005239void p2p_loop_on_known_peers(struct p2p_data *p2p,
5240 void (*peer_callback)(struct p2p_peer_info *peer,
5241 void *user_data),
5242 void *user_data)
5243{
5244 struct p2p_device *dev, *n;
5245
5246 dl_list_for_each_safe(dev, n, &p2p->devices, struct p2p_device, list) {
5247 peer_callback(&dev->info, user_data);
5248 }
5249}
5250
5251
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005252#ifdef CONFIG_WPS_NFC
5253
5254static struct wpabuf * p2p_build_nfc_handover(struct p2p_data *p2p,
5255 int client_freq,
5256 const u8 *go_dev_addr,
5257 const u8 *ssid, size_t ssid_len)
5258{
5259 struct wpabuf *buf;
5260 u8 op_class, channel;
5261 enum p2p_role_indication role = P2P_DEVICE_NOT_IN_GROUP;
5262
5263 buf = wpabuf_alloc(1000);
5264 if (buf == NULL)
5265 return NULL;
5266
5267 op_class = p2p->cfg->reg_class;
5268 channel = p2p->cfg->channel;
5269
5270 p2p_buf_add_capability(buf, p2p->dev_capab &
5271 ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY, 0);
5272 p2p_buf_add_device_info(buf, p2p, NULL);
5273
5274 if (p2p->num_groups > 0) {
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07005275 int freq = p2p_group_get_freq(p2p->groups[0]);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005276 role = P2P_GO_IN_A_GROUP;
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07005277 if (p2p_freq_to_channel(freq, &op_class, &channel) < 0) {
5278 p2p_dbg(p2p,
5279 "Unknown GO operating frequency %d MHz for NFC handover",
5280 freq);
5281 wpabuf_free(buf);
5282 return NULL;
5283 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005284 } else if (client_freq > 0) {
5285 role = P2P_CLIENT_IN_A_GROUP;
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07005286 if (p2p_freq_to_channel(client_freq, &op_class, &channel) < 0) {
5287 p2p_dbg(p2p,
5288 "Unknown client operating frequency %d MHz for NFC handover",
5289 client_freq);
5290 wpabuf_free(buf);
5291 return NULL;
5292 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005293 }
5294
5295 p2p_buf_add_oob_go_neg_channel(buf, p2p->cfg->country, op_class,
5296 channel, role);
5297
5298 if (p2p->num_groups > 0) {
5299 /* Limit number of clients to avoid very long message */
5300 p2p_buf_add_group_info(p2p->groups[0], buf, 5);
5301 p2p_group_buf_add_id(p2p->groups[0], buf);
5302 } else if (client_freq > 0 &&
5303 go_dev_addr && !is_zero_ether_addr(go_dev_addr) &&
5304 ssid && ssid_len > 0) {
5305 /*
5306 * Add the optional P2P Group ID to indicate in which group this
5307 * device is a P2P Client.
5308 */
5309 p2p_buf_add_group_id(buf, go_dev_addr, ssid, ssid_len);
5310 }
5311
5312 return buf;
5313}
5314
5315
5316struct wpabuf * p2p_build_nfc_handover_req(struct p2p_data *p2p,
5317 int client_freq,
5318 const u8 *go_dev_addr,
5319 const u8 *ssid, size_t ssid_len)
5320{
5321 return p2p_build_nfc_handover(p2p, client_freq, go_dev_addr, ssid,
5322 ssid_len);
5323}
5324
5325
5326struct wpabuf * p2p_build_nfc_handover_sel(struct p2p_data *p2p,
5327 int client_freq,
5328 const u8 *go_dev_addr,
5329 const u8 *ssid, size_t ssid_len)
5330{
5331 return p2p_build_nfc_handover(p2p, client_freq, go_dev_addr, ssid,
5332 ssid_len);
5333}
5334
5335
5336int p2p_process_nfc_connection_handover(struct p2p_data *p2p,
5337 struct p2p_nfc_params *params)
5338{
5339 struct p2p_message msg;
5340 struct p2p_device *dev;
5341 const u8 *p2p_dev_addr;
5342 int freq;
5343 enum p2p_role_indication role;
5344
5345 params->next_step = NO_ACTION;
5346
5347 if (p2p_parse_ies_separate(params->wsc_attr, params->wsc_len,
5348 params->p2p_attr, params->p2p_len, &msg)) {
5349 p2p_dbg(p2p, "Failed to parse WSC/P2P attributes from NFC");
5350 p2p_parse_free(&msg);
5351 return -1;
5352 }
5353
5354 if (msg.p2p_device_addr)
5355 p2p_dev_addr = msg.p2p_device_addr;
5356 else if (msg.device_id)
5357 p2p_dev_addr = msg.device_id;
5358 else {
5359 p2p_dbg(p2p, "Ignore scan data without P2P Device Info or P2P Device Id");
5360 p2p_parse_free(&msg);
5361 return -1;
5362 }
5363
5364 if (msg.oob_dev_password) {
5365 os_memcpy(params->oob_dev_pw, msg.oob_dev_password,
5366 msg.oob_dev_password_len);
5367 params->oob_dev_pw_len = msg.oob_dev_password_len;
5368 }
5369
5370 dev = p2p_create_device(p2p, p2p_dev_addr);
5371 if (dev == NULL) {
5372 p2p_parse_free(&msg);
5373 return -1;
5374 }
5375
5376 params->peer = &dev->info;
5377
5378 os_get_reltime(&dev->last_seen);
5379 dev->flags &= ~(P2P_DEV_PROBE_REQ_ONLY | P2P_DEV_GROUP_CLIENT_ONLY);
5380 p2p_copy_wps_info(p2p, dev, 0, &msg);
5381
5382 if (!msg.oob_go_neg_channel) {
5383 p2p_dbg(p2p, "OOB GO Negotiation Channel attribute not included");
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07005384 p2p_parse_free(&msg);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005385 return -1;
5386 }
5387
5388 if (msg.oob_go_neg_channel[3] == 0 &&
5389 msg.oob_go_neg_channel[4] == 0)
5390 freq = 0;
5391 else
5392 freq = p2p_channel_to_freq(msg.oob_go_neg_channel[3],
5393 msg.oob_go_neg_channel[4]);
5394 if (freq < 0) {
5395 p2p_dbg(p2p, "Unknown peer OOB GO Neg channel");
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07005396 p2p_parse_free(&msg);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005397 return -1;
5398 }
5399 role = msg.oob_go_neg_channel[5];
5400
5401 if (role == P2P_GO_IN_A_GROUP) {
5402 p2p_dbg(p2p, "Peer OOB GO operating channel: %u MHz", freq);
5403 params->go_freq = freq;
5404 } else if (role == P2P_CLIENT_IN_A_GROUP) {
5405 p2p_dbg(p2p, "Peer (client) OOB GO operating channel: %u MHz",
5406 freq);
5407 params->go_freq = freq;
5408 } else
5409 p2p_dbg(p2p, "Peer OOB GO Neg channel: %u MHz", freq);
5410 dev->oob_go_neg_freq = freq;
5411
5412 if (!params->sel && role != P2P_GO_IN_A_GROUP) {
5413 freq = p2p_channel_to_freq(p2p->cfg->reg_class,
5414 p2p->cfg->channel);
5415 if (freq < 0) {
5416 p2p_dbg(p2p, "Own listen channel not known");
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07005417 p2p_parse_free(&msg);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005418 return -1;
5419 }
5420 p2p_dbg(p2p, "Use own Listen channel as OOB GO Neg channel: %u MHz", freq);
5421 dev->oob_go_neg_freq = freq;
5422 }
5423
5424 if (msg.group_id) {
5425 os_memcpy(params->go_dev_addr, msg.group_id, ETH_ALEN);
5426 params->go_ssid_len = msg.group_id_len - ETH_ALEN;
5427 os_memcpy(params->go_ssid, msg.group_id + ETH_ALEN,
5428 params->go_ssid_len);
5429 }
5430
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005431 if (dev->flags & P2P_DEV_USER_REJECTED) {
5432 p2p_dbg(p2p, "Do not report rejected device");
Dmitry Shmidt71757432014-06-02 13:50:35 -07005433 p2p_parse_free(&msg);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005434 return 0;
5435 }
5436
5437 if (!(dev->flags & P2P_DEV_REPORTED)) {
5438 p2p->cfg->dev_found(p2p->cfg->cb_ctx, p2p_dev_addr, &dev->info,
5439 !(dev->flags & P2P_DEV_REPORTED_ONCE));
5440 dev->flags |= P2P_DEV_REPORTED | P2P_DEV_REPORTED_ONCE;
5441 }
Dmitry Shmidt71757432014-06-02 13:50:35 -07005442 p2p_parse_free(&msg);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005443
5444 if (role == P2P_GO_IN_A_GROUP && p2p->num_groups > 0)
5445 params->next_step = BOTH_GO;
5446 else if (role == P2P_GO_IN_A_GROUP)
5447 params->next_step = JOIN_GROUP;
5448 else if (role == P2P_CLIENT_IN_A_GROUP) {
5449 dev->flags |= P2P_DEV_GROUP_CLIENT_ONLY;
5450 params->next_step = PEER_CLIENT;
5451 } else if (p2p->num_groups > 0)
5452 params->next_step = AUTH_JOIN;
5453 else if (params->sel)
5454 params->next_step = INIT_GO_NEG;
5455 else
5456 params->next_step = RESP_GO_NEG;
5457
5458 return 0;
5459}
5460
5461
5462void p2p_set_authorized_oob_dev_pw_id(struct p2p_data *p2p, u16 dev_pw_id,
5463 int go_intent,
5464 const u8 *own_interface_addr)
5465{
5466
5467 p2p->authorized_oob_dev_pw_id = dev_pw_id;
5468 if (dev_pw_id == 0) {
5469 p2p_dbg(p2p, "NFC OOB Password unauthorized for static handover");
5470 return;
5471 }
5472
5473 p2p_dbg(p2p, "NFC OOB Password (id=%u) authorized for static handover",
5474 dev_pw_id);
5475
5476 p2p->go_intent = go_intent;
5477 os_memcpy(p2p->intended_addr, own_interface_addr, ETH_ALEN);
5478}
5479
5480#endif /* CONFIG_WPS_NFC */
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07005481
5482
5483int p2p_set_passphrase_len(struct p2p_data *p2p, unsigned int len)
5484{
5485 if (len < 8 || len > 63)
5486 return -1;
5487 p2p->cfg->passphrase_len = len;
5488 return 0;
5489}
Dmitry Shmidt2e67f062014-07-16 09:55:28 -07005490
5491
5492void p2p_set_vendor_elems(struct p2p_data *p2p, struct wpabuf **vendor_elem)
5493{
5494 p2p->vendor_elem = vendor_elem;
5495}
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005496
5497
5498void p2p_go_neg_wait_timeout(void *eloop_ctx, void *timeout_ctx)
5499{
5500 struct p2p_data *p2p = eloop_ctx;
5501
5502 p2p_dbg(p2p,
5503 "Timeout on waiting peer to become ready for GO Negotiation");
5504 p2p_go_neg_failed(p2p, -1);
5505}
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005506
5507
5508void p2p_set_own_pref_freq_list(struct p2p_data *p2p,
5509 const unsigned int *pref_freq_list,
5510 unsigned int size)
5511{
5512 unsigned int i;
5513
5514 if (size > P2P_MAX_PREF_CHANNELS)
5515 size = P2P_MAX_PREF_CHANNELS;
5516 p2p->num_pref_freq = size;
5517 for (i = 0; i < size; i++) {
5518 p2p->pref_freq_list[i] = pref_freq_list[i];
5519 p2p_dbg(p2p, "Own preferred frequency list[%u]=%u MHz",
5520 i, p2p->pref_freq_list[i]);
5521 }
5522}
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07005523
5524
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08005525void p2p_set_override_pref_op_chan(struct p2p_data *p2p, u8 op_class,
5526 u8 chan)
5527{
5528 p2p->override_pref_op_class = op_class;
5529 p2p->override_pref_channel = chan;
5530}
5531
5532
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07005533struct wpabuf * p2p_build_probe_resp_template(struct p2p_data *p2p,
5534 unsigned int freq)
5535{
5536 struct wpabuf *ies, *buf;
5537 u8 addr[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
5538 int ret;
5539
5540 ies = p2p_build_probe_resp_ies(p2p, NULL, 0);
5541 if (!ies) {
5542 wpa_printf(MSG_ERROR,
5543 "CTRL: Failed to build Probe Response IEs");
5544 return NULL;
5545 }
5546
5547 buf = wpabuf_alloc(200 + wpabuf_len(ies));
5548 if (!buf) {
5549 wpabuf_free(ies);
5550 return NULL;
5551 }
5552
5553 ret = p2p_build_probe_resp_buf(p2p, buf, ies, addr, freq);
5554 wpabuf_free(ies);
5555 if (ret) {
5556 wpabuf_free(buf);
5557 return NULL;
5558 }
5559
5560 return buf;
5561}