blob: 9c7388506a6494860b040a39d78cccc91880176a [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;
456 os_memcpy(dev->info.wps_sec_dev_type_list, cli->sec_dev_types,
457 dev->info.wps_sec_dev_type_list_len);
458}
459
460
461static int p2p_add_group_clients(struct p2p_data *p2p, const u8 *go_dev_addr,
462 const u8 *go_interface_addr, int freq,
Dmitry Shmidta3dc3092015-06-23 11:21:28 -0700463 const u8 *gi, size_t gi_len,
464 struct os_reltime *rx_time)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700465{
466 struct p2p_group_info info;
467 size_t c;
468 struct p2p_device *dev;
469
470 if (gi == NULL)
471 return 0;
472
473 if (p2p_group_info_parse(gi, gi_len, &info) < 0)
474 return -1;
475
476 /*
477 * Clear old data for this group; if the devices are still in the
478 * group, the information will be restored in the loop following this.
479 */
480 dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800481 if (os_memcmp(dev->member_in_go_iface, go_interface_addr,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700482 ETH_ALEN) == 0) {
483 os_memset(dev->member_in_go_iface, 0, ETH_ALEN);
484 os_memset(dev->member_in_go_dev, 0, ETH_ALEN);
485 }
486 }
487
488 for (c = 0; c < info.num_clients; c++) {
489 struct p2p_client_info *cli = &info.client[c];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800490 if (os_memcmp(cli->p2p_device_addr, p2p->cfg->dev_addr,
491 ETH_ALEN) == 0)
492 continue; /* ignore our own entry */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700493 dev = p2p_get_device(p2p, cli->p2p_device_addr);
494 if (dev) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700495 if (dev->flags & (P2P_DEV_GROUP_CLIENT_ONLY |
Dmitry Shmidt04949592012-07-19 12:16:46 -0700496 P2P_DEV_PROBE_REQ_ONLY)) {
497 /*
498 * Update information since we have not
499 * received this directly from the client.
500 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700501 p2p_copy_client_info(dev, cli);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700502 } else {
503 /*
504 * Need to update P2P Client Discoverability
505 * flag since it is valid only in P2P Group
506 * Info attribute.
507 */
508 dev->info.dev_capab &=
509 ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
510 dev->info.dev_capab |=
511 cli->dev_capab &
512 P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
513 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700514 if (dev->flags & P2P_DEV_PROBE_REQ_ONLY) {
515 dev->flags &= ~P2P_DEV_PROBE_REQ_ONLY;
516 }
517 } else {
518 dev = p2p_create_device(p2p, cli->p2p_device_addr);
519 if (dev == NULL)
520 continue;
521 dev->flags |= P2P_DEV_GROUP_CLIENT_ONLY;
522 p2p_copy_client_info(dev, cli);
523 dev->oper_freq = freq;
524 p2p->cfg->dev_found(p2p->cfg->cb_ctx,
525 dev->info.p2p_device_addr,
526 &dev->info, 1);
527 dev->flags |= P2P_DEV_REPORTED | P2P_DEV_REPORTED_ONCE;
528 }
529
530 os_memcpy(dev->interface_addr, cli->p2p_interface_addr,
531 ETH_ALEN);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -0700532 os_memcpy(&dev->last_seen, rx_time, sizeof(struct os_reltime));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700533 os_memcpy(dev->member_in_go_dev, go_dev_addr, ETH_ALEN);
534 os_memcpy(dev->member_in_go_iface, go_interface_addr,
535 ETH_ALEN);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -0700536 dev->flags |= P2P_DEV_LAST_SEEN_AS_GROUP_CLIENT;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700537 }
538
539 return 0;
540}
541
542
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700543static void p2p_copy_wps_info(struct p2p_data *p2p, struct p2p_device *dev,
544 int probe_req, const struct p2p_message *msg)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700545{
546 os_memcpy(dev->info.device_name, msg->device_name,
547 sizeof(dev->info.device_name));
548
549 if (msg->manufacturer &&
550 msg->manufacturer_len < sizeof(dev->info.manufacturer)) {
551 os_memset(dev->info.manufacturer, 0,
552 sizeof(dev->info.manufacturer));
553 os_memcpy(dev->info.manufacturer, msg->manufacturer,
554 msg->manufacturer_len);
555 }
556
557 if (msg->model_name &&
558 msg->model_name_len < sizeof(dev->info.model_name)) {
559 os_memset(dev->info.model_name, 0,
560 sizeof(dev->info.model_name));
561 os_memcpy(dev->info.model_name, msg->model_name,
562 msg->model_name_len);
563 }
564
565 if (msg->model_number &&
566 msg->model_number_len < sizeof(dev->info.model_number)) {
567 os_memset(dev->info.model_number, 0,
568 sizeof(dev->info.model_number));
569 os_memcpy(dev->info.model_number, msg->model_number,
570 msg->model_number_len);
571 }
572
573 if (msg->serial_number &&
574 msg->serial_number_len < sizeof(dev->info.serial_number)) {
575 os_memset(dev->info.serial_number, 0,
576 sizeof(dev->info.serial_number));
577 os_memcpy(dev->info.serial_number, msg->serial_number,
578 msg->serial_number_len);
579 }
580
581 if (msg->pri_dev_type)
582 os_memcpy(dev->info.pri_dev_type, msg->pri_dev_type,
583 sizeof(dev->info.pri_dev_type));
584 else if (msg->wps_pri_dev_type)
585 os_memcpy(dev->info.pri_dev_type, msg->wps_pri_dev_type,
586 sizeof(dev->info.pri_dev_type));
587
588 if (msg->wps_sec_dev_type_list) {
589 os_memcpy(dev->info.wps_sec_dev_type_list,
590 msg->wps_sec_dev_type_list,
591 msg->wps_sec_dev_type_list_len);
592 dev->info.wps_sec_dev_type_list_len =
593 msg->wps_sec_dev_type_list_len;
594 }
595
596 if (msg->capability) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700597 /*
598 * P2P Client Discoverability bit is reserved in all frames
599 * that use this function, so do not change its value here.
600 */
601 dev->info.dev_capab &= P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
602 dev->info.dev_capab |= msg->capability[0] &
603 ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700604 dev->info.group_capab = msg->capability[1];
605 }
606
607 if (msg->ext_listen_timing) {
608 dev->ext_listen_period = WPA_GET_LE16(msg->ext_listen_timing);
609 dev->ext_listen_interval =
610 WPA_GET_LE16(msg->ext_listen_timing + 2);
611 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700612
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700613 if (!probe_req) {
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800614 u16 new_config_methods;
615 new_config_methods = msg->config_methods ?
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700616 msg->config_methods : msg->wps_config_methods;
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800617 if (new_config_methods &&
618 dev->info.config_methods != new_config_methods) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700619 p2p_dbg(p2p, "Update peer " MACSTR
620 " config_methods 0x%x -> 0x%x",
621 MAC2STR(dev->info.p2p_device_addr),
622 dev->info.config_methods,
623 new_config_methods);
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800624 dev->info.config_methods = new_config_methods;
625 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700626 }
627}
628
629
Dmitry Shmidt2e67f062014-07-16 09:55:28 -0700630static void p2p_update_peer_vendor_elems(struct p2p_device *dev, const u8 *ies,
631 size_t ies_len)
632{
633 const u8 *pos, *end;
634 u8 id, len;
635
636 wpabuf_free(dev->info.vendor_elems);
637 dev->info.vendor_elems = NULL;
638
639 end = ies + ies_len;
640
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800641 for (pos = ies; end - pos > 1; pos += len) {
Dmitry Shmidt2e67f062014-07-16 09:55:28 -0700642 id = *pos++;
643 len = *pos++;
644
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800645 if (len > end - pos)
Dmitry Shmidt2e67f062014-07-16 09:55:28 -0700646 break;
647
648 if (id != WLAN_EID_VENDOR_SPECIFIC || len < 3)
649 continue;
650
651 if (len >= 4) {
652 u32 type = WPA_GET_BE32(pos);
653
654 if (type == WPA_IE_VENDOR_TYPE ||
655 type == WMM_IE_VENDOR_TYPE ||
656 type == WPS_IE_VENDOR_TYPE ||
657 type == P2P_IE_VENDOR_TYPE ||
658 type == WFD_IE_VENDOR_TYPE)
659 continue;
660 }
661
662 /* Unknown vendor element - make raw IE data available */
663 if (wpabuf_resize(&dev->info.vendor_elems, 2 + len) < 0)
664 break;
665 wpabuf_put_data(dev->info.vendor_elems, pos - 2, 2 + len);
666 }
667}
668
669
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800670static int p2p_compare_wfd_info(struct p2p_device *dev,
671 const struct p2p_message *msg)
672{
673 if (dev->info.wfd_subelems && msg->wfd_subelems) {
674 if (dev->info.wfd_subelems->used != msg->wfd_subelems->used)
675 return 1;
676
677 return os_memcmp(dev->info.wfd_subelems->buf,
678 msg->wfd_subelems->buf,
679 dev->info.wfd_subelems->used);
680 }
681 if (dev->info.wfd_subelems || msg->wfd_subelems)
682 return 1;
683
684 return 0;
685}
686
687
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700688/**
Dmitry Shmidt04949592012-07-19 12:16:46 -0700689 * p2p_add_device - Add peer entries based on scan results or P2P frames
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700690 * @p2p: P2P module context from p2p_init()
691 * @addr: Source address of Beacon or Probe Response frame (may be either
692 * P2P Device Address or P2P Interface Address)
693 * @level: Signal level (signal strength of the received frame from the peer)
694 * @freq: Frequency on which the Beacon or Probe Response frame was received
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800695 * @rx_time: Time when the result was received
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700696 * @ies: IEs from the Beacon or Probe Response frame
697 * @ies_len: Length of ies buffer in octets
Dmitry Shmidt04949592012-07-19 12:16:46 -0700698 * @scan_res: Whether this was based on scan results
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700699 * Returns: 0 on success, -1 on failure
700 *
701 * If the scan result is for a GO, the clients in the group will also be added
702 * to the peer table. This function can also be used with some other frames
703 * like Provision Discovery Request that contains P2P Capability and P2P Device
704 * Info attributes.
705 */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800706int p2p_add_device(struct p2p_data *p2p, const u8 *addr, int freq,
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800707 struct os_reltime *rx_time, int level, const u8 *ies,
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800708 size_t ies_len, int scan_res)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700709{
710 struct p2p_device *dev;
711 struct p2p_message msg;
712 const u8 *p2p_dev_addr;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800713 int wfd_changed;
Dmitry Shmidt29333592017-01-09 12:27:11 -0800714 int dev_name_changed;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700715 int i;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800716 struct os_reltime time_now;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700717
718 os_memset(&msg, 0, sizeof(msg));
719 if (p2p_parse_ies(ies, ies_len, &msg)) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700720 p2p_dbg(p2p, "Failed to parse P2P IE for a device entry");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700721 p2p_parse_free(&msg);
722 return -1;
723 }
724
725 if (msg.p2p_device_addr)
726 p2p_dev_addr = msg.p2p_device_addr;
727 else if (msg.device_id)
728 p2p_dev_addr = msg.device_id;
729 else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700730 p2p_dbg(p2p, "Ignore scan data without P2P Device Info or P2P Device Id");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700731 p2p_parse_free(&msg);
732 return -1;
733 }
734
735 if (!is_zero_ether_addr(p2p->peer_filter) &&
736 os_memcmp(p2p_dev_addr, p2p->peer_filter, ETH_ALEN) != 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700737 p2p_dbg(p2p, "Do not add peer filter for " MACSTR
738 " due to peer filter", MAC2STR(p2p_dev_addr));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800739 p2p_parse_free(&msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700740 return 0;
741 }
742
743 dev = p2p_create_device(p2p, p2p_dev_addr);
744 if (dev == NULL) {
745 p2p_parse_free(&msg);
746 return -1;
747 }
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800748
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800749 if (rx_time == NULL) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800750 os_get_reltime(&time_now);
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800751 rx_time = &time_now;
752 }
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800753
754 /*
755 * Update the device entry only if the new peer
Dmitry Shmidta3dc3092015-06-23 11:21:28 -0700756 * entry is newer than the one previously stored, or if
757 * the device was previously seen as a P2P Client in a group
758 * and the new entry isn't older than a threshold.
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800759 */
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800760 if (dev->last_seen.sec > 0 &&
Dmitry Shmidta3dc3092015-06-23 11:21:28 -0700761 os_reltime_before(rx_time, &dev->last_seen) &&
762 (!(dev->flags & P2P_DEV_LAST_SEEN_AS_GROUP_CLIENT) ||
763 os_reltime_expired(&dev->last_seen, rx_time,
764 P2P_DEV_GROUP_CLIENT_RESP_THRESHOLD))) {
765 p2p_dbg(p2p,
766 "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 -0800767 (unsigned int) rx_time->sec,
768 (unsigned int) rx_time->usec,
769 (unsigned int) dev->last_seen.sec,
Dmitry Shmidta3dc3092015-06-23 11:21:28 -0700770 (unsigned int) dev->last_seen.usec,
771 dev->flags);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800772 p2p_parse_free(&msg);
773 return -1;
774 }
775
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800776 os_memcpy(&dev->last_seen, rx_time, sizeof(struct os_reltime));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800777
Dmitry Shmidta3dc3092015-06-23 11:21:28 -0700778 dev->flags &= ~(P2P_DEV_PROBE_REQ_ONLY | P2P_DEV_GROUP_CLIENT_ONLY |
779 P2P_DEV_LAST_SEEN_AS_GROUP_CLIENT);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700780
781 if (os_memcmp(addr, p2p_dev_addr, ETH_ALEN) != 0)
782 os_memcpy(dev->interface_addr, addr, ETH_ALEN);
783 if (msg.ssid &&
Jouni Malinenfdb708a2015-04-07 11:32:11 +0300784 msg.ssid[1] <= sizeof(dev->oper_ssid) &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700785 (msg.ssid[1] != P2P_WILDCARD_SSID_LEN ||
786 os_memcmp(msg.ssid + 2, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN)
787 != 0)) {
788 os_memcpy(dev->oper_ssid, msg.ssid + 2, msg.ssid[1]);
789 dev->oper_ssid_len = msg.ssid[1];
790 }
791
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -0700792 wpabuf_free(dev->info.p2ps_instance);
793 dev->info.p2ps_instance = NULL;
794 if (msg.adv_service_instance && msg.adv_service_instance_len)
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800795 dev->info.p2ps_instance = wpabuf_alloc_copy(
796 msg.adv_service_instance, msg.adv_service_instance_len);
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800797
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700798 if (freq >= 2412 && freq <= 2484 && msg.ds_params &&
799 *msg.ds_params >= 1 && *msg.ds_params <= 14) {
800 int ds_freq;
801 if (*msg.ds_params == 14)
802 ds_freq = 2484;
803 else
804 ds_freq = 2407 + *msg.ds_params * 5;
805 if (freq != ds_freq) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700806 p2p_dbg(p2p, "Update Listen frequency based on DS Parameter Set IE: %d -> %d MHz",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700807 freq, ds_freq);
808 freq = ds_freq;
809 }
810 }
811
Dmitry Shmidt04949592012-07-19 12:16:46 -0700812 if (dev->listen_freq && dev->listen_freq != freq && scan_res) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700813 p2p_dbg(p2p, "Update Listen frequency based on scan results ("
814 MACSTR " %d -> %d MHz (DS param %d)",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700815 MAC2STR(dev->info.p2p_device_addr), dev->listen_freq,
816 freq, msg.ds_params ? *msg.ds_params : -1);
817 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700818 if (scan_res) {
819 dev->listen_freq = freq;
820 if (msg.group_info)
821 dev->oper_freq = freq;
822 }
Jouni Malinen75ecf522011-06-27 15:19:46 -0700823 dev->info.level = level;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700824
Dmitry Shmidt29333592017-01-09 12:27:11 -0800825 dev_name_changed = os_strncmp(dev->info.device_name, msg.device_name,
826 WPS_DEV_NAME_MAX_LEN) != 0;
827
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700828 p2p_copy_wps_info(p2p, dev, 0, &msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700829
830 for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) {
831 wpabuf_free(dev->info.wps_vendor_ext[i]);
832 dev->info.wps_vendor_ext[i] = NULL;
833 }
834
835 for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) {
836 if (msg.wps_vendor_ext[i] == NULL)
837 break;
838 dev->info.wps_vendor_ext[i] = wpabuf_alloc_copy(
839 msg.wps_vendor_ext[i], msg.wps_vendor_ext_len[i]);
840 if (dev->info.wps_vendor_ext[i] == NULL)
841 break;
842 }
843
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800844 wfd_changed = p2p_compare_wfd_info(dev, &msg);
845
Dmitry Shmidt29333592017-01-09 12:27:11 -0800846 if (wfd_changed) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700847 wpabuf_free(dev->info.wfd_subelems);
Dmitry Shmidt29333592017-01-09 12:27:11 -0800848 if (msg.wfd_subelems)
849 dev->info.wfd_subelems = wpabuf_dup(msg.wfd_subelems);
850 else
851 dev->info.wfd_subelems = NULL;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700852 }
853
Dmitry Shmidt04949592012-07-19 12:16:46 -0700854 if (scan_res) {
855 p2p_add_group_clients(p2p, p2p_dev_addr, addr, freq,
Dmitry Shmidta3dc3092015-06-23 11:21:28 -0700856 msg.group_info, msg.group_info_len,
857 rx_time);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700858 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700859
860 p2p_parse_free(&msg);
861
Dmitry Shmidt2e67f062014-07-16 09:55:28 -0700862 p2p_update_peer_vendor_elems(dev, ies, ies_len);
863
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800864 if (dev->flags & P2P_DEV_REPORTED && !wfd_changed &&
Dmitry Shmidt29333592017-01-09 12:27:11 -0800865 !dev_name_changed &&
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800866 (!msg.adv_service_instance ||
867 (dev->flags & P2P_DEV_P2PS_REPORTED)))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700868 return 0;
869
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700870 p2p_dbg(p2p, "Peer found with Listen frequency %d MHz (rx_time=%u.%06u)",
871 freq, (unsigned int) rx_time->sec,
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800872 (unsigned int) rx_time->usec);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700873 if (dev->flags & P2P_DEV_USER_REJECTED) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700874 p2p_dbg(p2p, "Do not report rejected device");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700875 return 0;
876 }
877
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800878 if (dev->info.config_methods == 0 &&
879 (freq == 2412 || freq == 2437 || freq == 2462)) {
880 /*
881 * If we have only seen a Beacon frame from a GO, we do not yet
882 * know what WPS config methods it supports. Since some
883 * applications use config_methods value from P2P-DEVICE-FOUND
884 * events, postpone reporting this peer until we've fully
885 * discovered its capabilities.
886 *
887 * At least for now, do this only if the peer was detected on
888 * one of the social channels since that peer can be easily be
889 * found again and there are no limitations of having to use
890 * passive scan on this channels, so this can be done through
891 * Probe Response frame that includes the config_methods
892 * information.
893 */
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700894 p2p_dbg(p2p, "Do not report peer " MACSTR
895 " with unknown config methods", MAC2STR(addr));
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800896 return 0;
897 }
898
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700899 p2p->cfg->dev_found(p2p->cfg->cb_ctx, addr, &dev->info,
900 !(dev->flags & P2P_DEV_REPORTED_ONCE));
901 dev->flags |= P2P_DEV_REPORTED | P2P_DEV_REPORTED_ONCE;
902
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800903 if (msg.adv_service_instance)
904 dev->flags |= P2P_DEV_P2PS_REPORTED;
905
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700906 return 0;
907}
908
909
910static void p2p_device_free(struct p2p_data *p2p, struct p2p_device *dev)
911{
912 int i;
913
Dmitry Shmidt497c1d52011-07-21 15:19:46 -0700914 if (p2p->go_neg_peer == dev) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800915 /*
916 * If GO Negotiation is in progress, report that it has failed.
917 */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800918 p2p_go_neg_failed(p2p, -1);
Dmitry Shmidt497c1d52011-07-21 15:19:46 -0700919 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700920 if (p2p->invite_peer == dev)
921 p2p->invite_peer = NULL;
922 if (p2p->sd_peer == dev)
923 p2p->sd_peer = NULL;
924 if (p2p->pending_client_disc_go == dev)
925 p2p->pending_client_disc_go = NULL;
926
Dmitry Shmidtc55524a2011-07-07 11:18:38 -0700927 /* dev_lost() device, but only if it was previously dev_found() */
928 if (dev->flags & P2P_DEV_REPORTED_ONCE)
929 p2p->cfg->dev_lost(p2p->cfg->cb_ctx,
930 dev->info.p2p_device_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700931
932 for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) {
933 wpabuf_free(dev->info.wps_vendor_ext[i]);
934 dev->info.wps_vendor_ext[i] = NULL;
935 }
936
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700937 wpabuf_free(dev->info.wfd_subelems);
Dmitry Shmidt2e67f062014-07-16 09:55:28 -0700938 wpabuf_free(dev->info.vendor_elems);
Dmitry Shmidt413dde72014-04-11 10:23:22 -0700939 wpabuf_free(dev->go_neg_conf);
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800940 wpabuf_free(dev->info.p2ps_instance);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700941
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700942 os_free(dev);
943}
944
945
946static int p2p_get_next_prog_freq(struct p2p_data *p2p)
947{
948 struct p2p_channels *c;
949 struct p2p_reg_class *cla;
950 size_t cl, ch;
951 int found = 0;
952 u8 reg_class;
953 u8 channel;
954 int freq;
955
956 c = &p2p->cfg->channels;
957 for (cl = 0; cl < c->reg_classes; cl++) {
958 cla = &c->reg_class[cl];
959 if (cla->reg_class != p2p->last_prog_scan_class)
960 continue;
961 for (ch = 0; ch < cla->channels; ch++) {
962 if (cla->channel[ch] == p2p->last_prog_scan_chan) {
963 found = 1;
964 break;
965 }
966 }
967 if (found)
968 break;
969 }
970
971 if (!found) {
972 /* Start from beginning */
973 reg_class = c->reg_class[0].reg_class;
974 channel = c->reg_class[0].channel[0];
975 } else {
976 /* Pick the next channel */
977 ch++;
978 if (ch == cla->channels) {
979 cl++;
980 if (cl == c->reg_classes)
981 cl = 0;
982 ch = 0;
983 }
984 reg_class = c->reg_class[cl].reg_class;
985 channel = c->reg_class[cl].channel[ch];
986 }
987
Dmitry Shmidt4b060592013-04-29 16:42:49 -0700988 freq = p2p_channel_to_freq(reg_class, channel);
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700989 p2p_dbg(p2p, "Next progressive search channel: reg_class %u channel %u -> %d MHz",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700990 reg_class, channel, freq);
991 p2p->last_prog_scan_class = reg_class;
992 p2p->last_prog_scan_chan = channel;
993
994 if (freq == 2412 || freq == 2437 || freq == 2462)
995 return 0; /* No need to add social channels */
996 return freq;
997}
998
999
1000static void p2p_search(struct p2p_data *p2p)
1001{
1002 int freq = 0;
1003 enum p2p_scan_type type;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001004 u16 pw_id = DEV_PW_DEFAULT;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001005 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001006
1007 if (p2p->drv_in_listen) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001008 p2p_dbg(p2p, "Driver is still in Listen state - wait for it to end before continuing");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001009 return;
1010 }
1011 p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
1012
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001013 if (p2p->find_pending_full &&
1014 (p2p->find_type == P2P_FIND_PROGRESSIVE ||
1015 p2p->find_type == P2P_FIND_START_WITH_FULL)) {
1016 type = P2P_SCAN_FULL;
1017 p2p_dbg(p2p, "Starting search (pending full scan)");
1018 p2p->find_pending_full = 0;
1019 } else if ((p2p->find_type == P2P_FIND_PROGRESSIVE &&
1020 (freq = p2p_get_next_prog_freq(p2p)) > 0) ||
1021 (p2p->find_type == P2P_FIND_START_WITH_FULL &&
1022 (freq = p2p->find_specified_freq) > 0)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001023 type = P2P_SCAN_SOCIAL_PLUS_ONE;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001024 p2p_dbg(p2p, "Starting search (+ freq %u)", freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001025 } else {
1026 type = P2P_SCAN_SOCIAL;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001027 p2p_dbg(p2p, "Starting search");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001028 }
1029
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001030 res = p2p->cfg->p2p_scan(p2p->cfg->cb_ctx, type, freq,
1031 p2p->num_req_dev_types, p2p->req_dev_types,
1032 p2p->find_dev_id, pw_id);
1033 if (res < 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001034 p2p_dbg(p2p, "Scan request schedule failed");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001035 p2p_continue_find(p2p);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001036 }
1037}
1038
1039
1040static void p2p_find_timeout(void *eloop_ctx, void *timeout_ctx)
1041{
1042 struct p2p_data *p2p = eloop_ctx;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001043 p2p_dbg(p2p, "Find timeout -> stop");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001044 p2p_stop_find(p2p);
1045}
1046
1047
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001048void p2p_notify_scan_trigger_status(struct p2p_data *p2p, int status)
1049{
1050 if (status != 0) {
1051 p2p_dbg(p2p, "Scan request failed");
1052 /* Do continue find even for the first p2p_find_scan */
1053 p2p_continue_find(p2p);
1054 } else {
1055 p2p_dbg(p2p, "Running p2p_scan");
1056 p2p->p2p_scan_running = 1;
1057 eloop_cancel_timeout(p2p_scan_timeout, p2p, NULL);
1058 eloop_register_timeout(P2P_SCAN_TIMEOUT, 0, p2p_scan_timeout,
1059 p2p, NULL);
1060 }
1061}
1062
1063
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001064static int p2p_run_after_scan(struct p2p_data *p2p)
1065{
1066 struct p2p_device *dev;
1067 enum p2p_after_scan op;
1068
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001069 op = p2p->start_after_scan;
1070 p2p->start_after_scan = P2P_AFTER_SCAN_NOTHING;
1071 switch (op) {
1072 case P2P_AFTER_SCAN_NOTHING:
1073 break;
1074 case P2P_AFTER_SCAN_LISTEN:
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001075 p2p_dbg(p2p, "Start previously requested Listen state");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001076 p2p_listen(p2p, p2p->pending_listen_sec * 1000 +
1077 p2p->pending_listen_usec / 1000);
1078 return 1;
1079 case P2P_AFTER_SCAN_CONNECT:
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001080 p2p_dbg(p2p, "Start previously requested connect with " MACSTR,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001081 MAC2STR(p2p->after_scan_peer));
1082 dev = p2p_get_device(p2p, p2p->after_scan_peer);
1083 if (dev == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001084 p2p_dbg(p2p, "Peer not known anymore");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001085 break;
1086 }
1087 p2p_connect_send(p2p, dev);
1088 return 1;
1089 }
1090
1091 return 0;
1092}
1093
1094
1095static void p2p_scan_timeout(void *eloop_ctx, void *timeout_ctx)
1096{
1097 struct p2p_data *p2p = eloop_ctx;
1098 int running;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001099 p2p_dbg(p2p, "p2p_scan timeout (running=%d)", p2p->p2p_scan_running);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001100 running = p2p->p2p_scan_running;
1101 /* Make sure we recover from missed scan results callback */
1102 p2p->p2p_scan_running = 0;
1103
1104 if (running)
1105 p2p_run_after_scan(p2p);
1106}
1107
1108
1109static void p2p_free_req_dev_types(struct p2p_data *p2p)
1110{
1111 p2p->num_req_dev_types = 0;
1112 os_free(p2p->req_dev_types);
1113 p2p->req_dev_types = NULL;
1114}
1115
1116
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001117static int p2ps_gen_hash(struct p2p_data *p2p, const char *str, u8 *hash)
1118{
1119 u8 buf[SHA256_MAC_LEN];
1120 char str_buf[256];
1121 const u8 *adv_array;
1122 size_t i, adv_len;
1123
1124 if (!str || !hash)
1125 return 0;
1126
1127 if (!str[0]) {
1128 os_memcpy(hash, p2p->wild_card_hash, P2PS_HASH_LEN);
1129 return 1;
1130 }
1131
1132 adv_array = (u8 *) str_buf;
1133 adv_len = os_strlen(str);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07001134 if (adv_len >= sizeof(str_buf))
1135 return 0;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001136
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001137 for (i = 0; i < adv_len; i++) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001138 if (str[i] >= 'A' && str[i] <= 'Z')
1139 str_buf[i] = str[i] - 'A' + 'a';
1140 else
1141 str_buf[i] = str[i];
1142 }
1143
1144 if (sha256_vector(1, &adv_array, &adv_len, buf))
1145 return 0;
1146
1147 os_memcpy(hash, buf, P2PS_HASH_LEN);
1148 return 1;
1149}
1150
1151
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001152int p2p_find(struct p2p_data *p2p, unsigned int timeout,
1153 enum p2p_discovery_type type,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001154 unsigned int num_req_dev_types, const u8 *req_dev_types,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001155 const u8 *dev_id, unsigned int search_delay,
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001156 u8 seek_count, const char **seek, int freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001157{
1158 int res;
Hai Shalom74f70d42019-02-11 14:42:39 -08001159 struct os_reltime start;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001160
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001161 p2p_dbg(p2p, "Starting find (type=%d)", type);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001162 if (p2p->p2p_scan_running) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001163 p2p_dbg(p2p, "p2p_scan is already running");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001164 }
1165
1166 p2p_free_req_dev_types(p2p);
1167 if (req_dev_types && num_req_dev_types) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001168 p2p->req_dev_types = os_memdup(req_dev_types,
1169 num_req_dev_types *
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001170 WPS_DEV_TYPE_LEN);
1171 if (p2p->req_dev_types == NULL)
1172 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001173 p2p->num_req_dev_types = num_req_dev_types;
1174 }
1175
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001176 if (dev_id) {
1177 os_memcpy(p2p->find_dev_id_buf, dev_id, ETH_ALEN);
1178 p2p->find_dev_id = p2p->find_dev_id_buf;
1179 } else
1180 p2p->find_dev_id = NULL;
1181
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001182 if (seek_count == 0 || !seek) {
1183 /* Not an ASP search */
1184 p2p->p2ps_seek = 0;
1185 } else if (seek_count == 1 && seek && (!seek[0] || !seek[0][0])) {
1186 /*
1187 * An empty seek string means no hash values, but still an ASP
1188 * search.
1189 */
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07001190 p2p_dbg(p2p, "ASP search");
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001191 p2p->p2ps_seek_count = 0;
1192 p2p->p2ps_seek = 1;
1193 } else if (seek && seek_count <= P2P_MAX_QUERY_HASH) {
1194 u8 buf[P2PS_HASH_LEN];
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07001195 int i, count = 0;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001196
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001197 for (i = 0; i < seek_count; i++) {
1198 if (!p2ps_gen_hash(p2p, seek[i], buf))
1199 continue;
1200
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07001201 p2p_dbg(p2p, "Seek service %s hash " MACSTR,
1202 seek[i], MAC2STR(buf));
1203 os_memcpy(&p2p->p2ps_seek_hash[count * P2PS_HASH_LEN],
1204 buf, P2PS_HASH_LEN);
1205 count++;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001206 }
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07001207
1208 p2p->p2ps_seek_count = count;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001209 p2p->p2ps_seek = 1;
1210 } else {
1211 p2p->p2ps_seek_count = 0;
1212 p2p->p2ps_seek = 1;
1213 }
1214
1215 /* Special case to perform wildcard search */
1216 if (p2p->p2ps_seek_count == 0 && p2p->p2ps_seek) {
1217 p2p->p2ps_seek_count = 1;
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07001218 os_memcpy(&p2p->p2ps_seek_hash, p2p->wild_card_hash,
1219 P2PS_HASH_LEN);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001220 }
1221
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001222 p2p->start_after_scan = P2P_AFTER_SCAN_NOTHING;
1223 p2p_clear_timeout(p2p);
Dmitry Shmidtde47be72016-01-07 12:52:55 -08001224 if (p2p->pending_listen_freq) {
1225 p2p_dbg(p2p, "Clear pending_listen_freq for p2p_find");
1226 p2p->pending_listen_freq = 0;
1227 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001228 p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001229 p2p->find_pending_full = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001230 p2p->find_type = type;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001231 if (freq != 2412 && freq != 2437 && freq != 2462 && freq != 60480)
1232 p2p->find_specified_freq = freq;
1233 else
1234 p2p->find_specified_freq = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001235 p2p_device_clear_reported(p2p);
Dmitry Shmidtde47be72016-01-07 12:52:55 -08001236 os_memset(p2p->sd_query_no_ack, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001237 p2p_set_state(p2p, P2P_SEARCH);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001238 p2p->search_delay = search_delay;
1239 p2p->in_search_delay = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001240 eloop_cancel_timeout(p2p_find_timeout, p2p, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001241 p2p->last_p2p_find_timeout = timeout;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001242 if (timeout)
1243 eloop_register_timeout(timeout, 0, p2p_find_timeout,
1244 p2p, NULL);
Hai Shalom74f70d42019-02-11 14:42:39 -08001245 os_get_reltime(&start);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001246 switch (type) {
1247 case P2P_FIND_START_WITH_FULL:
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001248 if (freq > 0) {
1249 /*
1250 * Start with the specified channel and then move to
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001251 * scans for social channels and this specific channel.
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001252 */
1253 res = p2p->cfg->p2p_scan(p2p->cfg->cb_ctx,
1254 P2P_SCAN_SPECIFIC, freq,
1255 p2p->num_req_dev_types,
1256 p2p->req_dev_types, dev_id,
1257 DEV_PW_DEFAULT);
1258 break;
1259 }
1260 /* fall through */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001261 case P2P_FIND_PROGRESSIVE:
1262 res = p2p->cfg->p2p_scan(p2p->cfg->cb_ctx, P2P_SCAN_FULL, 0,
1263 p2p->num_req_dev_types,
Dmitry Shmidt04949592012-07-19 12:16:46 -07001264 p2p->req_dev_types, dev_id,
1265 DEV_PW_DEFAULT);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001266 break;
1267 case P2P_FIND_ONLY_SOCIAL:
1268 res = p2p->cfg->p2p_scan(p2p->cfg->cb_ctx, P2P_SCAN_SOCIAL, 0,
1269 p2p->num_req_dev_types,
Dmitry Shmidt04949592012-07-19 12:16:46 -07001270 p2p->req_dev_types, dev_id,
1271 DEV_PW_DEFAULT);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001272 break;
1273 default:
1274 return -1;
1275 }
1276
Hai Shalom74f70d42019-02-11 14:42:39 -08001277 if (!res)
1278 p2p->find_start = start;
1279
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001280 if (res != 0 && p2p->p2p_scan_running) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001281 p2p_dbg(p2p, "Failed to start p2p_scan - another p2p_scan was already running");
1282 /* wait for the previous p2p_scan to complete */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001283 if (type == P2P_FIND_PROGRESSIVE ||
1284 (type == P2P_FIND_START_WITH_FULL && freq == 0))
1285 p2p->find_pending_full = 1;
Dmitry Shmidt96be6222014-02-13 10:16:51 -08001286 res = 0; /* do not report failure */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001287 } else if (res != 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001288 p2p_dbg(p2p, "Failed to start p2p_scan");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001289 p2p_set_state(p2p, P2P_IDLE);
1290 eloop_cancel_timeout(p2p_find_timeout, p2p, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001291 }
1292
1293 return res;
1294}
1295
1296
1297void p2p_stop_find_for_freq(struct p2p_data *p2p, int freq)
1298{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001299 p2p_dbg(p2p, "Stopping find");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001300 eloop_cancel_timeout(p2p_find_timeout, p2p, NULL);
1301 p2p_clear_timeout(p2p);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001302 if (p2p->state == P2P_SEARCH || p2p->state == P2P_SD_DURING_FIND)
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001303 p2p->cfg->find_stopped(p2p->cfg->cb_ctx);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001304
1305 p2p->p2ps_seek_count = 0;
1306
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001307 p2p_set_state(p2p, P2P_IDLE);
1308 p2p_free_req_dev_types(p2p);
1309 p2p->start_after_scan = P2P_AFTER_SCAN_NOTHING;
Dmitry Shmidt8c652892013-03-01 10:14:01 -08001310 if (p2p->go_neg_peer)
1311 p2p->go_neg_peer->flags &= ~P2P_DEV_PEER_WAITING_RESPONSE;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001312 p2p->go_neg_peer = NULL;
1313 p2p->sd_peer = NULL;
1314 p2p->invite_peer = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001315 p2p_stop_listen_for_freq(p2p, freq);
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001316 p2p->send_action_in_progress = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001317}
1318
1319
1320void p2p_stop_listen_for_freq(struct p2p_data *p2p, int freq)
1321{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001322 if (freq > 0 && p2p->drv_in_listen == freq && p2p->in_listen) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001323 p2p_dbg(p2p, "Skip stop_listen since we are on correct channel for response");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001324 return;
1325 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001326 if (p2p->in_listen) {
1327 p2p->in_listen = 0;
1328 p2p_clear_timeout(p2p);
1329 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001330 if (p2p->drv_in_listen) {
1331 /*
1332 * The driver may not deliver callback to p2p_listen_end()
1333 * when the operation gets canceled, so clear the internal
1334 * variable that is tracking driver state.
1335 */
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001336 p2p_dbg(p2p, "Clear drv_in_listen (%d)", p2p->drv_in_listen);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001337 p2p->drv_in_listen = 0;
1338 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001339 p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
1340}
1341
1342
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001343void p2p_stop_listen(struct p2p_data *p2p)
1344{
1345 if (p2p->state != P2P_LISTEN_ONLY) {
1346 p2p_dbg(p2p, "Skip stop_listen since not in listen_only state.");
1347 return;
1348 }
1349
1350 p2p_stop_listen_for_freq(p2p, 0);
1351 p2p_set_state(p2p, P2P_IDLE);
1352}
1353
1354
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001355void p2p_stop_find(struct p2p_data *p2p)
1356{
Dmitry Shmidt3c57b3f2014-05-22 15:13:07 -07001357 p2p->pending_listen_freq = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001358 p2p_stop_find_for_freq(p2p, 0);
1359}
1360
1361
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001362static int p2p_prepare_channel_pref(struct p2p_data *p2p,
1363 unsigned int force_freq,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001364 unsigned int pref_freq, int go)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001365{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001366 u8 op_class, op_channel;
1367 unsigned int freq = force_freq ? force_freq : pref_freq;
1368
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001369 p2p_dbg(p2p, "Prepare channel pref - force_freq=%u pref_freq=%u go=%d",
1370 force_freq, pref_freq, go);
Dmitry Shmidt4b060592013-04-29 16:42:49 -07001371 if (p2p_freq_to_channel(freq, &op_class, &op_channel) < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001372 p2p_dbg(p2p, "Unsupported frequency %u MHz", freq);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001373 return -1;
1374 }
1375
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001376 if (!p2p_channels_includes(&p2p->cfg->channels, op_class, op_channel) &&
1377 (go || !p2p_channels_includes(&p2p->cfg->cli_channels, op_class,
1378 op_channel))) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001379 p2p_dbg(p2p, "Frequency %u MHz (oper_class %u channel %u) not allowed for P2P",
1380 freq, op_class, op_channel);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001381 return -1;
1382 }
1383
1384 p2p->op_reg_class = op_class;
1385 p2p->op_channel = op_channel;
1386
1387 if (force_freq) {
1388 p2p->channels.reg_classes = 1;
1389 p2p->channels.reg_class[0].channels = 1;
1390 p2p->channels.reg_class[0].reg_class = p2p->op_reg_class;
1391 p2p->channels.reg_class[0].channel[0] = p2p->op_channel;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001392 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001393 os_memcpy(&p2p->channels, &p2p->cfg->channels,
1394 sizeof(struct p2p_channels));
1395 }
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001396
1397 return 0;
1398}
1399
1400
1401static void p2p_prepare_channel_best(struct p2p_data *p2p)
1402{
1403 u8 op_class, op_channel;
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07001404 const int op_classes_5ghz[] = { 124, 125, 115, 0 };
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08001405 const int op_classes_ht40[] = { 126, 127, 116, 117, 0 };
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001406 const int op_classes_vht[] = { 128, 0 };
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001407 const int op_classes_edmg[] = { 181, 182, 183, 0 };
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001408
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07001409 p2p_dbg(p2p, "Prepare channel best");
1410
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001411 if (!p2p->cfg->cfg_op_channel && p2p->best_freq_overall > 0 &&
1412 p2p_supported_freq(p2p, p2p->best_freq_overall) &&
Dmitry Shmidt4b060592013-04-29 16:42:49 -07001413 p2p_freq_to_channel(p2p->best_freq_overall, &op_class, &op_channel)
1414 == 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001415 p2p_dbg(p2p, "Select best overall channel as operating channel preference");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001416 p2p->op_reg_class = op_class;
1417 p2p->op_channel = op_channel;
1418 } else if (!p2p->cfg->cfg_op_channel && p2p->best_freq_5 > 0 &&
1419 p2p_supported_freq(p2p, p2p->best_freq_5) &&
Dmitry Shmidt4b060592013-04-29 16:42:49 -07001420 p2p_freq_to_channel(p2p->best_freq_5, &op_class, &op_channel)
1421 == 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001422 p2p_dbg(p2p, "Select best 5 GHz channel as operating channel preference");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001423 p2p->op_reg_class = op_class;
1424 p2p->op_channel = op_channel;
1425 } else if (!p2p->cfg->cfg_op_channel && p2p->best_freq_24 > 0 &&
1426 p2p_supported_freq(p2p, p2p->best_freq_24) &&
Dmitry Shmidt4b060592013-04-29 16:42:49 -07001427 p2p_freq_to_channel(p2p->best_freq_24, &op_class,
1428 &op_channel) == 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001429 p2p_dbg(p2p, "Select best 2.4 GHz channel as operating channel preference");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001430 p2p->op_reg_class = op_class;
1431 p2p->op_channel = op_channel;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07001432 } else if (p2p->cfg->num_pref_chan > 0 &&
1433 p2p_channels_includes(&p2p->cfg->channels,
1434 p2p->cfg->pref_chan[0].op_class,
1435 p2p->cfg->pref_chan[0].chan)) {
1436 p2p_dbg(p2p, "Select first pref_chan entry as operating channel preference");
1437 p2p->op_reg_class = p2p->cfg->pref_chan[0].op_class;
1438 p2p->op_channel = p2p->cfg->pref_chan[0].chan;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001439 } else if (p2p_channel_select(&p2p->cfg->channels, op_classes_edmg,
1440 &p2p->op_reg_class, &p2p->op_channel) ==
1441 0) {
1442 p2p_dbg(p2p, "Select possible EDMG channel (op_class %u channel %u) as operating channel preference",
1443 p2p->op_reg_class, p2p->op_channel);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001444 } else if (p2p_channel_select(&p2p->cfg->channels, op_classes_vht,
1445 &p2p->op_reg_class, &p2p->op_channel) ==
1446 0) {
1447 p2p_dbg(p2p, "Select possible VHT channel (op_class %u channel %u) as operating channel preference",
1448 p2p->op_reg_class, p2p->op_channel);
1449 } else if (p2p_channel_select(&p2p->cfg->channels, op_classes_ht40,
1450 &p2p->op_reg_class, &p2p->op_channel) ==
1451 0) {
1452 p2p_dbg(p2p, "Select possible HT40 channel (op_class %u channel %u) as operating channel preference",
1453 p2p->op_reg_class, p2p->op_channel);
1454 } else if (p2p_channel_select(&p2p->cfg->channels, op_classes_5ghz,
1455 &p2p->op_reg_class, &p2p->op_channel) ==
1456 0) {
1457 p2p_dbg(p2p, "Select possible 5 GHz channel (op_class %u channel %u) as operating channel preference",
1458 p2p->op_reg_class, p2p->op_channel);
Dmitry Shmidtd11f0192014-03-24 12:09:47 -07001459 } else if (p2p_channels_includes(&p2p->cfg->channels,
1460 p2p->cfg->op_reg_class,
1461 p2p->cfg->op_channel)) {
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07001462 p2p_dbg(p2p, "Select pre-configured channel as operating channel preference");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001463 p2p->op_reg_class = p2p->cfg->op_reg_class;
1464 p2p->op_channel = p2p->cfg->op_channel;
Dmitry Shmidtd11f0192014-03-24 12:09:47 -07001465 } else if (p2p_channel_random_social(&p2p->cfg->channels,
1466 &p2p->op_reg_class,
Hai Shalom74f70d42019-02-11 14:42:39 -08001467 &p2p->op_channel,
1468 NULL, NULL) == 0) {
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001469 p2p_dbg(p2p, "Select random available social channel (op_class %u channel %u) as operating channel preference",
1470 p2p->op_reg_class, p2p->op_channel);
Dmitry Shmidtd11f0192014-03-24 12:09:47 -07001471 } else {
1472 /* Select any random available channel from the first available
1473 * operating class */
1474 p2p_channel_select(&p2p->cfg->channels, NULL,
1475 &p2p->op_reg_class,
1476 &p2p->op_channel);
1477 p2p_dbg(p2p, "Select random available channel %d from operating class %d as operating channel preference",
1478 p2p->op_channel, p2p->op_reg_class);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001479 }
1480
1481 os_memcpy(&p2p->channels, &p2p->cfg->channels,
1482 sizeof(struct p2p_channels));
1483}
1484
1485
1486/**
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001487 * p2p_prepare_channel - Select operating channel for GO Negotiation or P2PS PD
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001488 * @p2p: P2P module context from p2p_init()
1489 * @dev: Selected peer device
1490 * @force_freq: Forced frequency in MHz or 0 if not forced
1491 * @pref_freq: Preferred frequency in MHz or 0 if no preference
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001492 * @go: Whether the local end will be forced to be GO
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001493 * Returns: 0 on success, -1 on failure (channel not supported for P2P)
1494 *
1495 * This function is used to do initial operating channel selection for GO
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001496 * Negotiation prior to having received peer information or for P2PS PD
1497 * signalling. The selected channel may be further optimized in
1498 * p2p_reselect_channel() once the peer information is available.
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001499 */
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08001500int p2p_prepare_channel(struct p2p_data *p2p, struct p2p_device *dev,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001501 unsigned int force_freq, unsigned int pref_freq, int go)
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001502{
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001503 p2p_dbg(p2p, "Prepare channel - force_freq=%u pref_freq=%u go=%d",
1504 force_freq, pref_freq, go);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001505 if (force_freq || pref_freq) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001506 if (p2p_prepare_channel_pref(p2p, force_freq, pref_freq, go) <
1507 0)
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001508 return -1;
1509 } else {
1510 p2p_prepare_channel_best(p2p);
1511 }
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001512 p2p_channels_dump(p2p, "prepared channels", &p2p->channels);
1513 if (go)
1514 p2p_channels_remove_freqs(&p2p->channels, &p2p->no_go_freq);
1515 else if (!force_freq)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001516 p2p_channels_union_inplace(&p2p->channels,
1517 &p2p->cfg->cli_channels);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001518 p2p_channels_dump(p2p, "after go/cli filter/add", &p2p->channels);
1519
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001520 p2p_dbg(p2p, "Own preference for operation channel: Operating Class %u Channel %u%s",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001521 p2p->op_reg_class, p2p->op_channel,
1522 force_freq ? " (forced)" : "");
1523
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001524 if (force_freq)
1525 dev->flags |= P2P_DEV_FORCE_FREQ;
1526 else
1527 dev->flags &= ~P2P_DEV_FORCE_FREQ;
1528
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001529 return 0;
1530}
1531
1532
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001533static void p2p_set_dev_persistent(struct p2p_device *dev,
1534 int persistent_group)
1535{
1536 switch (persistent_group) {
1537 case 0:
1538 dev->flags &= ~(P2P_DEV_PREFER_PERSISTENT_GROUP |
1539 P2P_DEV_PREFER_PERSISTENT_RECONN);
1540 break;
1541 case 1:
1542 dev->flags |= P2P_DEV_PREFER_PERSISTENT_GROUP;
1543 dev->flags &= ~P2P_DEV_PREFER_PERSISTENT_RECONN;
1544 break;
1545 case 2:
1546 dev->flags |= P2P_DEV_PREFER_PERSISTENT_GROUP |
1547 P2P_DEV_PREFER_PERSISTENT_RECONN;
1548 break;
1549 }
1550}
1551
1552
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001553int p2p_connect(struct p2p_data *p2p, const u8 *peer_addr,
1554 enum p2p_wps_method wps_method,
1555 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07001556 unsigned int force_freq, int persistent_group,
1557 const u8 *force_ssid, size_t force_ssid_len,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001558 int pd_before_go_neg, unsigned int pref_freq, u16 oob_pw_id)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001559{
1560 struct p2p_device *dev;
1561
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001562 p2p_dbg(p2p, "Request to start group negotiation - peer=" MACSTR
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001563 " GO Intent=%d Intended Interface Address=" MACSTR
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001564 " wps_method=%d persistent_group=%d pd_before_go_neg=%d "
1565 "oob_pw_id=%u",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001566 MAC2STR(peer_addr), go_intent, MAC2STR(own_interface_addr),
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001567 wps_method, persistent_group, pd_before_go_neg, oob_pw_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001568
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001569 dev = p2p_get_device(p2p, peer_addr);
1570 if (dev == NULL || (dev->flags & P2P_DEV_PROBE_REQ_ONLY)) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001571 p2p_dbg(p2p, "Cannot connect to unknown P2P Device " MACSTR,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001572 MAC2STR(peer_addr));
1573 return -1;
1574 }
1575
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001576 if (p2p_prepare_channel(p2p, dev, force_freq, pref_freq,
1577 go_intent == 15) < 0)
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001578 return -1;
1579
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001580 if (dev->flags & P2P_DEV_GROUP_CLIENT_ONLY) {
1581 if (!(dev->info.dev_capab &
1582 P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY)) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001583 p2p_dbg(p2p, "Cannot connect to P2P Device " MACSTR
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001584 " that is in a group and is not discoverable",
1585 MAC2STR(peer_addr));
1586 return -1;
1587 }
1588 if (dev->oper_freq <= 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001589 p2p_dbg(p2p, "Cannot connect to P2P Device " MACSTR
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001590 " with incomplete information",
1591 MAC2STR(peer_addr));
1592 return -1;
1593 }
1594
1595 /*
1596 * First, try to connect directly. If the peer does not
1597 * acknowledge frames, assume it is sleeping and use device
1598 * discoverability via the GO at that point.
1599 */
1600 }
1601
Dmitry Shmidt04949592012-07-19 12:16:46 -07001602 p2p->ssid_set = 0;
1603 if (force_ssid) {
1604 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Forced SSID",
1605 force_ssid, force_ssid_len);
1606 os_memcpy(p2p->ssid, force_ssid, force_ssid_len);
1607 p2p->ssid_len = force_ssid_len;
1608 p2p->ssid_set = 1;
1609 }
1610
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001611 dev->flags &= ~P2P_DEV_NOT_YET_READY;
1612 dev->flags &= ~P2P_DEV_USER_REJECTED;
1613 dev->flags &= ~P2P_DEV_WAIT_GO_NEG_RESPONSE;
1614 dev->flags &= ~P2P_DEV_WAIT_GO_NEG_CONFIRM;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001615 if (pd_before_go_neg)
1616 dev->flags |= P2P_DEV_PD_BEFORE_GO_NEG;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001617 else {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001618 dev->flags &= ~P2P_DEV_PD_BEFORE_GO_NEG;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001619 /*
Dmitry Shmidt9cdf1b92013-02-27 12:58:50 -08001620 * Assign dialog token and tie breaker here to use the same
1621 * values in each retry within the same GO Negotiation exchange.
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001622 */
1623 dev->dialog_token++;
1624 if (dev->dialog_token == 0)
1625 dev->dialog_token = 1;
Dmitry Shmidt9cdf1b92013-02-27 12:58:50 -08001626 dev->tie_breaker = p2p->next_tie_breaker;
1627 p2p->next_tie_breaker = !p2p->next_tie_breaker;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001628 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001629 dev->connect_reqs = 0;
1630 dev->go_neg_req_sent = 0;
1631 dev->go_state = UNKNOWN_GO;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001632 p2p_set_dev_persistent(dev, persistent_group);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001633 p2p->go_intent = go_intent;
1634 os_memcpy(p2p->intended_addr, own_interface_addr, ETH_ALEN);
1635
1636 if (p2p->state != P2P_IDLE)
1637 p2p_stop_find(p2p);
1638
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001639 dev->wps_method = wps_method;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001640 dev->oob_pw_id = oob_pw_id;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001641 dev->status = P2P_SC_SUCCESS;
1642
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001643 if (p2p->p2p_scan_running) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001644 p2p_dbg(p2p, "p2p_scan running - delay connect send");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001645 p2p->start_after_scan = P2P_AFTER_SCAN_CONNECT;
1646 os_memcpy(p2p->after_scan_peer, peer_addr, ETH_ALEN);
1647 return 0;
1648 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001649
1650 return p2p_connect_send(p2p, dev);
1651}
1652
1653
1654int p2p_authorize(struct p2p_data *p2p, const u8 *peer_addr,
1655 enum p2p_wps_method wps_method,
1656 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07001657 unsigned int force_freq, int persistent_group,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001658 const u8 *force_ssid, size_t force_ssid_len,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001659 unsigned int pref_freq, u16 oob_pw_id)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001660{
1661 struct p2p_device *dev;
1662
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001663 p2p_dbg(p2p, "Request to authorize group negotiation - peer=" MACSTR
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001664 " GO Intent=%d Intended Interface Address=" MACSTR
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001665 " wps_method=%d persistent_group=%d oob_pw_id=%u",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001666 MAC2STR(peer_addr), go_intent, MAC2STR(own_interface_addr),
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001667 wps_method, persistent_group, oob_pw_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001668
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001669 dev = p2p_get_device(p2p, peer_addr);
1670 if (dev == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001671 p2p_dbg(p2p, "Cannot authorize unknown P2P Device " MACSTR,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001672 MAC2STR(peer_addr));
1673 return -1;
1674 }
1675
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001676 if (p2p_prepare_channel(p2p, dev, force_freq, pref_freq, go_intent ==
1677 15) < 0)
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001678 return -1;
1679
Dmitry Shmidt04949592012-07-19 12:16:46 -07001680 p2p->ssid_set = 0;
1681 if (force_ssid) {
1682 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Forced SSID",
1683 force_ssid, force_ssid_len);
1684 os_memcpy(p2p->ssid, force_ssid, force_ssid_len);
1685 p2p->ssid_len = force_ssid_len;
1686 p2p->ssid_set = 1;
1687 }
1688
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001689 dev->flags &= ~P2P_DEV_NOT_YET_READY;
1690 dev->flags &= ~P2P_DEV_USER_REJECTED;
1691 dev->go_neg_req_sent = 0;
1692 dev->go_state = UNKNOWN_GO;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001693 p2p_set_dev_persistent(dev, persistent_group);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001694 p2p->go_intent = go_intent;
1695 os_memcpy(p2p->intended_addr, own_interface_addr, ETH_ALEN);
1696
1697 dev->wps_method = wps_method;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001698 dev->oob_pw_id = oob_pw_id;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001699 dev->status = P2P_SC_SUCCESS;
1700
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001701 return 0;
1702}
1703
1704
1705void p2p_add_dev_info(struct p2p_data *p2p, const u8 *addr,
1706 struct p2p_device *dev, struct p2p_message *msg)
1707{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001708 os_get_reltime(&dev->last_seen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001709
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001710 p2p_copy_wps_info(p2p, dev, 0, msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001711
1712 if (msg->listen_channel) {
1713 int freq;
Dmitry Shmidt4b060592013-04-29 16:42:49 -07001714 freq = p2p_channel_to_freq(msg->listen_channel[3],
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001715 msg->listen_channel[4]);
1716 if (freq < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001717 p2p_dbg(p2p, "Unknown peer Listen channel: "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001718 "country=%c%c(0x%02x) reg_class=%u channel=%u",
1719 msg->listen_channel[0],
1720 msg->listen_channel[1],
1721 msg->listen_channel[2],
1722 msg->listen_channel[3],
1723 msg->listen_channel[4]);
1724 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001725 p2p_dbg(p2p, "Update peer " MACSTR
1726 " Listen channel: %u -> %u MHz",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001727 MAC2STR(dev->info.p2p_device_addr),
1728 dev->listen_freq, freq);
1729 dev->listen_freq = freq;
1730 }
1731 }
1732
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001733 if (msg->wfd_subelems) {
1734 wpabuf_free(dev->info.wfd_subelems);
1735 dev->info.wfd_subelems = wpabuf_dup(msg->wfd_subelems);
1736 }
1737
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001738 if (dev->flags & P2P_DEV_PROBE_REQ_ONLY) {
1739 dev->flags &= ~P2P_DEV_PROBE_REQ_ONLY;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001740 p2p_dbg(p2p, "Completed device entry based on data from GO Negotiation Request");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001741 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001742 p2p_dbg(p2p, "Created device entry based on GO Neg Req: "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001743 MACSTR " dev_capab=0x%x group_capab=0x%x name='%s' "
1744 "listen_freq=%d",
1745 MAC2STR(dev->info.p2p_device_addr),
1746 dev->info.dev_capab, dev->info.group_capab,
1747 dev->info.device_name, dev->listen_freq);
1748 }
1749
1750 dev->flags &= ~P2P_DEV_GROUP_CLIENT_ONLY;
1751
1752 if (dev->flags & P2P_DEV_USER_REJECTED) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001753 p2p_dbg(p2p, "Do not report rejected device");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001754 return;
1755 }
1756
1757 p2p->cfg->dev_found(p2p->cfg->cb_ctx, addr, &dev->info,
1758 !(dev->flags & P2P_DEV_REPORTED_ONCE));
1759 dev->flags |= P2P_DEV_REPORTED | P2P_DEV_REPORTED_ONCE;
1760}
1761
1762
1763void p2p_build_ssid(struct p2p_data *p2p, u8 *ssid, size_t *ssid_len)
1764{
1765 os_memcpy(ssid, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN);
1766 p2p_random((char *) &ssid[P2P_WILDCARD_SSID_LEN], 2);
1767 os_memcpy(&ssid[P2P_WILDCARD_SSID_LEN + 2],
1768 p2p->cfg->ssid_postfix, p2p->cfg->ssid_postfix_len);
1769 *ssid_len = P2P_WILDCARD_SSID_LEN + 2 + p2p->cfg->ssid_postfix_len;
1770}
1771
1772
1773int p2p_go_params(struct p2p_data *p2p, struct p2p_go_neg_results *params)
1774{
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001775 if (p2p->ssid_set) {
1776 os_memcpy(params->ssid, p2p->ssid, p2p->ssid_len);
1777 params->ssid_len = p2p->ssid_len;
1778 } else {
1779 p2p_build_ssid(p2p, params->ssid, &params->ssid_len);
1780 }
1781 p2p->ssid_set = 0;
1782
Jimmy Chen6d7e3902018-11-20 10:15:16 +08001783 if (p2p->passphrase_set) {
1784 os_memcpy(params->passphrase, p2p->passphrase, os_strlen(p2p->passphrase));
1785 } else {
1786 p2p_random(params->passphrase, p2p->cfg->passphrase_len);
1787 }
1788 p2p->passphrase_set = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001789 return 0;
1790}
1791
1792
1793void p2p_go_complete(struct p2p_data *p2p, struct p2p_device *peer)
1794{
1795 struct p2p_go_neg_results res;
1796 int go = peer->go_state == LOCAL_GO;
1797 struct p2p_channels intersection;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001798
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001799 p2p_dbg(p2p, "GO Negotiation with " MACSTR " completed (%s will be GO)",
1800 MAC2STR(peer->info.p2p_device_addr), go ? "local end" : "peer");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001801
1802 os_memset(&res, 0, sizeof(res));
1803 res.role_go = go;
1804 os_memcpy(res.peer_device_addr, peer->info.p2p_device_addr, ETH_ALEN);
1805 os_memcpy(res.peer_interface_addr, peer->intended_addr, ETH_ALEN);
1806 res.wps_method = peer->wps_method;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001807 if (peer->flags & P2P_DEV_PREFER_PERSISTENT_GROUP) {
1808 if (peer->flags & P2P_DEV_PREFER_PERSISTENT_RECONN)
1809 res.persistent_group = 2;
1810 else
1811 res.persistent_group = 1;
1812 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001813
1814 if (go) {
1815 /* Setup AP mode for WPS provisioning */
Dmitry Shmidt4b060592013-04-29 16:42:49 -07001816 res.freq = p2p_channel_to_freq(p2p->op_reg_class,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001817 p2p->op_channel);
1818 os_memcpy(res.ssid, p2p->ssid, p2p->ssid_len);
1819 res.ssid_len = p2p->ssid_len;
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07001820 p2p_random(res.passphrase, p2p->cfg->passphrase_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001821 } else {
1822 res.freq = peer->oper_freq;
1823 if (p2p->ssid_len) {
1824 os_memcpy(res.ssid, p2p->ssid, p2p->ssid_len);
1825 res.ssid_len = p2p->ssid_len;
1826 }
1827 }
1828
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001829 p2p_channels_dump(p2p, "own channels", &p2p->channels);
1830 p2p_channels_dump(p2p, "peer channels", &peer->channels);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001831 p2p_channels_intersect(&p2p->channels, &peer->channels,
1832 &intersection);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001833 if (go) {
1834 p2p_channels_remove_freqs(&intersection, &p2p->no_go_freq);
1835 p2p_channels_dump(p2p, "intersection after no-GO removal",
1836 &intersection);
1837 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001838
1839 p2p_channels_to_freqs(&intersection, res.freq_list,
1840 P2P_MAX_CHANNELS);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001841
1842 res.peer_config_timeout = go ? peer->client_timeout : peer->go_timeout;
1843
1844 p2p_clear_timeout(p2p);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001845 p2p->ssid_set = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001846 peer->go_neg_req_sent = 0;
Dmitry Shmidt29333592017-01-09 12:27:11 -08001847 peer->flags &= ~P2P_DEV_PEER_WAITING_RESPONSE;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001848 peer->wps_method = WPS_NOT_READY;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001849 peer->oob_pw_id = 0;
Dmitry Shmidt413dde72014-04-11 10:23:22 -07001850 wpabuf_free(peer->go_neg_conf);
1851 peer->go_neg_conf = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001852
1853 p2p_set_state(p2p, P2P_PROVISIONING);
1854 p2p->cfg->go_neg_completed(p2p->cfg->cb_ctx, &res);
1855}
1856
1857
1858static void p2p_rx_p2p_action(struct p2p_data *p2p, const u8 *sa,
1859 const u8 *data, size_t len, int rx_freq)
1860{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001861 p2p_dbg(p2p, "RX P2P Public Action from " MACSTR, MAC2STR(sa));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001862 wpa_hexdump(MSG_MSGDUMP, "P2P: P2P Public Action contents", data, len);
1863
1864 if (len < 1)
1865 return;
1866
1867 switch (data[0]) {
1868 case P2P_GO_NEG_REQ:
1869 p2p_process_go_neg_req(p2p, sa, data + 1, len - 1, rx_freq);
1870 break;
1871 case P2P_GO_NEG_RESP:
1872 p2p_process_go_neg_resp(p2p, sa, data + 1, len - 1, rx_freq);
1873 break;
1874 case P2P_GO_NEG_CONF:
1875 p2p_process_go_neg_conf(p2p, sa, data + 1, len - 1);
1876 break;
1877 case P2P_INVITATION_REQ:
1878 p2p_process_invitation_req(p2p, sa, data + 1, len - 1,
1879 rx_freq);
1880 break;
1881 case P2P_INVITATION_RESP:
1882 p2p_process_invitation_resp(p2p, sa, data + 1, len - 1);
1883 break;
1884 case P2P_PROV_DISC_REQ:
1885 p2p_process_prov_disc_req(p2p, sa, data + 1, len - 1, rx_freq);
1886 break;
1887 case P2P_PROV_DISC_RESP:
1888 p2p_process_prov_disc_resp(p2p, sa, data + 1, len - 1);
1889 break;
1890 case P2P_DEV_DISC_REQ:
1891 p2p_process_dev_disc_req(p2p, sa, data + 1, len - 1, rx_freq);
1892 break;
1893 case P2P_DEV_DISC_RESP:
1894 p2p_process_dev_disc_resp(p2p, sa, data + 1, len - 1);
1895 break;
1896 default:
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001897 p2p_dbg(p2p, "Unsupported P2P Public Action frame type %d",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001898 data[0]);
1899 break;
1900 }
1901}
1902
1903
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001904static void p2p_rx_action_public(struct p2p_data *p2p, const u8 *da,
1905 const u8 *sa, const u8 *bssid, const u8 *data,
1906 size_t len, int freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001907{
1908 if (len < 1)
1909 return;
1910
1911 switch (data[0]) {
1912 case WLAN_PA_VENDOR_SPECIFIC:
1913 data++;
1914 len--;
Dmitry Shmidta38abf92014-03-06 13:38:44 -08001915 if (len < 4)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001916 return;
Dmitry Shmidta38abf92014-03-06 13:38:44 -08001917 if (WPA_GET_BE32(data) != P2P_IE_VENDOR_TYPE)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001918 return;
1919
Dmitry Shmidta38abf92014-03-06 13:38:44 -08001920 data += 4;
1921 len -= 4;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001922
Dmitry Shmidta38abf92014-03-06 13:38:44 -08001923 p2p_rx_p2p_action(p2p, sa, data, len, freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001924 break;
1925 case WLAN_PA_GAS_INITIAL_REQ:
1926 p2p_rx_gas_initial_req(p2p, sa, data + 1, len - 1, freq);
1927 break;
1928 case WLAN_PA_GAS_INITIAL_RESP:
1929 p2p_rx_gas_initial_resp(p2p, sa, data + 1, len - 1, freq);
1930 break;
1931 case WLAN_PA_GAS_COMEBACK_REQ:
1932 p2p_rx_gas_comeback_req(p2p, sa, data + 1, len - 1, freq);
1933 break;
1934 case WLAN_PA_GAS_COMEBACK_RESP:
1935 p2p_rx_gas_comeback_resp(p2p, sa, data + 1, len - 1, freq);
1936 break;
1937 }
1938}
1939
1940
1941void p2p_rx_action(struct p2p_data *p2p, const u8 *da, const u8 *sa,
1942 const u8 *bssid, u8 category,
1943 const u8 *data, size_t len, int freq)
1944{
1945 if (category == WLAN_ACTION_PUBLIC) {
1946 p2p_rx_action_public(p2p, da, sa, bssid, data, len, freq);
1947 return;
1948 }
1949
1950 if (category != WLAN_ACTION_VENDOR_SPECIFIC)
1951 return;
1952
1953 if (len < 4)
1954 return;
1955
Dmitry Shmidta38abf92014-03-06 13:38:44 -08001956 if (WPA_GET_BE32(data) != P2P_IE_VENDOR_TYPE)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001957 return;
Dmitry Shmidta38abf92014-03-06 13:38:44 -08001958 data += 4;
1959 len -= 4;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001960
1961 /* P2P action frame */
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001962 p2p_dbg(p2p, "RX P2P Action from " MACSTR, MAC2STR(sa));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001963 wpa_hexdump(MSG_MSGDUMP, "P2P: P2P Action contents", data, len);
1964
1965 if (len < 1)
1966 return;
1967 switch (data[0]) {
1968 case P2P_NOA:
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001969 p2p_dbg(p2p, "Received P2P Action - Notice of Absence");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001970 /* TODO */
1971 break;
1972 case P2P_PRESENCE_REQ:
1973 p2p_process_presence_req(p2p, da, sa, data + 1, len - 1, freq);
1974 break;
1975 case P2P_PRESENCE_RESP:
1976 p2p_process_presence_resp(p2p, da, sa, data + 1, len - 1);
1977 break;
1978 case P2P_GO_DISC_REQ:
1979 p2p_process_go_disc_req(p2p, da, sa, data + 1, len - 1, freq);
1980 break;
1981 default:
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001982 p2p_dbg(p2p, "Received P2P Action - unknown type %u", data[0]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001983 break;
1984 }
1985}
1986
1987
1988static void p2p_go_neg_start(void *eloop_ctx, void *timeout_ctx)
1989{
1990 struct p2p_data *p2p = eloop_ctx;
1991 if (p2p->go_neg_peer == NULL)
1992 return;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001993 if (p2p->pending_listen_freq) {
1994 p2p_dbg(p2p, "Clear pending_listen_freq for p2p_go_neg_start");
1995 p2p->pending_listen_freq = 0;
1996 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001997 p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
1998 p2p->go_neg_peer->status = P2P_SC_SUCCESS;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001999 /*
2000 * Set new timeout to make sure a previously set one does not expire
2001 * too quickly while waiting for the GO Negotiation to complete.
2002 */
2003 p2p_set_timeout(p2p, 0, 500000);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002004 p2p_connect_send(p2p, p2p->go_neg_peer);
2005}
2006
2007
2008static void p2p_invite_start(void *eloop_ctx, void *timeout_ctx)
2009{
2010 struct p2p_data *p2p = eloop_ctx;
2011 if (p2p->invite_peer == NULL)
2012 return;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07002013 if (p2p->pending_listen_freq) {
2014 p2p_dbg(p2p, "Clear pending_listen_freq for p2p_invite_start");
2015 p2p->pending_listen_freq = 0;
2016 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002017 p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002018 p2p_invite_send(p2p, p2p->invite_peer, p2p->invite_go_dev_addr,
2019 p2p->invite_dev_pw_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002020}
2021
2022
2023static void p2p_add_dev_from_probe_req(struct p2p_data *p2p, const u8 *addr,
2024 const u8 *ie, size_t ie_len)
2025{
2026 struct p2p_message msg;
2027 struct p2p_device *dev;
2028
2029 os_memset(&msg, 0, sizeof(msg));
2030 if (p2p_parse_ies(ie, ie_len, &msg) < 0 || msg.p2p_attributes == NULL)
2031 {
2032 p2p_parse_free(&msg);
2033 return; /* not a P2P probe */
2034 }
2035
2036 if (msg.ssid == NULL || msg.ssid[1] != P2P_WILDCARD_SSID_LEN ||
2037 os_memcmp(msg.ssid + 2, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN)
2038 != 0) {
2039 /* The Probe Request is not part of P2P Device Discovery. It is
2040 * not known whether the source address of the frame is the P2P
2041 * Device Address or P2P Interface Address. Do not add a new
2042 * peer entry based on this frames.
2043 */
2044 p2p_parse_free(&msg);
2045 return;
2046 }
2047
2048 dev = p2p_get_device(p2p, addr);
2049 if (dev) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002050 if (msg.listen_channel) {
2051 int freq;
2052
2053 if (dev->country[0] == 0)
2054 os_memcpy(dev->country, msg.listen_channel, 3);
2055
2056 freq = p2p_channel_to_freq(msg.listen_channel[3],
2057 msg.listen_channel[4]);
2058
2059 if (freq > 0 && dev->listen_freq != freq) {
2060 p2p_dbg(p2p,
2061 "Updated peer " MACSTR " Listen channel (Probe Request): %d -> %d MHz",
2062 MAC2STR(addr), dev->listen_freq, freq);
2063 dev->listen_freq = freq;
2064 }
2065 }
2066
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002067 os_get_reltime(&dev->last_seen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002068 p2p_parse_free(&msg);
2069 return; /* already known */
2070 }
2071
2072 dev = p2p_create_device(p2p, addr);
2073 if (dev == NULL) {
2074 p2p_parse_free(&msg);
2075 return;
2076 }
2077
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002078 os_get_reltime(&dev->last_seen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002079 dev->flags |= P2P_DEV_PROBE_REQ_ONLY;
2080
2081 if (msg.listen_channel) {
2082 os_memcpy(dev->country, msg.listen_channel, 3);
Dmitry Shmidt4b060592013-04-29 16:42:49 -07002083 dev->listen_freq = p2p_channel_to_freq(msg.listen_channel[3],
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002084 msg.listen_channel[4]);
2085 }
2086
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002087 p2p_copy_wps_info(p2p, dev, 1, &msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002088
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002089 if (msg.wfd_subelems) {
2090 wpabuf_free(dev->info.wfd_subelems);
2091 dev->info.wfd_subelems = wpabuf_dup(msg.wfd_subelems);
2092 }
2093
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002094 p2p_parse_free(&msg);
2095
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002096 p2p_dbg(p2p, "Created device entry based on Probe Req: " MACSTR
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002097 " dev_capab=0x%x group_capab=0x%x name='%s' listen_freq=%d",
2098 MAC2STR(dev->info.p2p_device_addr), dev->info.dev_capab,
2099 dev->info.group_capab, dev->info.device_name,
2100 dev->listen_freq);
2101}
2102
2103
2104struct p2p_device * p2p_add_dev_from_go_neg_req(struct p2p_data *p2p,
2105 const u8 *addr,
2106 struct p2p_message *msg)
2107{
2108 struct p2p_device *dev;
2109
2110 dev = p2p_get_device(p2p, addr);
2111 if (dev) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002112 os_get_reltime(&dev->last_seen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002113 return dev; /* already known */
2114 }
2115
2116 dev = p2p_create_device(p2p, addr);
2117 if (dev == NULL)
2118 return NULL;
2119
2120 p2p_add_dev_info(p2p, addr, dev, msg);
2121
2122 return dev;
2123}
2124
2125
2126static int dev_type_match(const u8 *dev_type, const u8 *req_dev_type)
2127{
2128 if (os_memcmp(dev_type, req_dev_type, WPS_DEV_TYPE_LEN) == 0)
2129 return 1;
2130 if (os_memcmp(dev_type, req_dev_type, 2) == 0 &&
2131 WPA_GET_BE32(&req_dev_type[2]) == 0 &&
2132 WPA_GET_BE16(&req_dev_type[6]) == 0)
2133 return 1; /* Category match with wildcard OUI/sub-category */
2134 return 0;
2135}
2136
2137
2138int dev_type_list_match(const u8 *dev_type, const u8 *req_dev_type[],
2139 size_t num_req_dev_type)
2140{
2141 size_t i;
2142 for (i = 0; i < num_req_dev_type; i++) {
2143 if (dev_type_match(dev_type, req_dev_type[i]))
2144 return 1;
2145 }
2146 return 0;
2147}
2148
2149
2150/**
2151 * p2p_match_dev_type - Match local device type with requested type
2152 * @p2p: P2P module context from p2p_init()
2153 * @wps: WPS TLVs from Probe Request frame (concatenated WPS IEs)
2154 * Returns: 1 on match, 0 on mismatch
2155 *
2156 * This function can be used to match the Requested Device Type attribute in
2157 * WPS IE with the local device types for deciding whether to reply to a Probe
2158 * Request frame.
2159 */
2160int p2p_match_dev_type(struct p2p_data *p2p, struct wpabuf *wps)
2161{
2162 struct wps_parse_attr attr;
2163 size_t i;
2164
2165 if (wps_parse_msg(wps, &attr))
2166 return 1; /* assume no Requested Device Type attributes */
2167
2168 if (attr.num_req_dev_type == 0)
2169 return 1; /* no Requested Device Type attributes -> match */
2170
2171 if (dev_type_list_match(p2p->cfg->pri_dev_type, attr.req_dev_type,
2172 attr.num_req_dev_type))
2173 return 1; /* Own Primary Device Type matches */
2174
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002175 for (i = 0; i < p2p->cfg->num_sec_dev_types; i++) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002176 if (dev_type_list_match(p2p->cfg->sec_dev_type[i],
2177 attr.req_dev_type,
2178 attr.num_req_dev_type))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002179 return 1; /* Own Secondary Device Type matches */
2180 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002181
2182 /* No matching device type found */
2183 return 0;
2184}
2185
2186
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002187struct wpabuf * p2p_build_probe_resp_ies(struct p2p_data *p2p,
2188 const u8 *query_hash,
2189 u8 query_count)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002190{
2191 struct wpabuf *buf;
2192 u8 *len;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002193 int pw_id = -1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002194 size_t extra = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002195
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002196#ifdef CONFIG_WIFI_DISPLAY
2197 if (p2p->wfd_ie_probe_resp)
2198 extra = wpabuf_len(p2p->wfd_ie_probe_resp);
2199#endif /* CONFIG_WIFI_DISPLAY */
2200
Dmitry Shmidt2e67f062014-07-16 09:55:28 -07002201 if (p2p->vendor_elem && p2p->vendor_elem[VENDOR_ELEM_PROBE_RESP_P2P])
2202 extra += wpabuf_len(p2p->vendor_elem[VENDOR_ELEM_PROBE_RESP_P2P]);
2203
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002204 if (query_count)
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002205 extra += MAX_SVC_ADV_IE_LEN;
2206
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002207 buf = wpabuf_alloc(1000 + extra);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002208 if (buf == NULL)
2209 return NULL;
2210
Dmitry Shmidt04949592012-07-19 12:16:46 -07002211 if (p2p->go_neg_peer) {
2212 /* Advertise immediate availability of WPS credential */
2213 pw_id = p2p_wps_method_pw_id(p2p->go_neg_peer->wps_method);
2214 }
2215
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002216 if (p2p_build_wps_ie(p2p, buf, pw_id, 1) < 0) {
2217 p2p_dbg(p2p, "Failed to build WPS IE for Probe Response");
2218 wpabuf_free(buf);
2219 return NULL;
2220 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002221
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002222#ifdef CONFIG_WIFI_DISPLAY
2223 if (p2p->wfd_ie_probe_resp)
2224 wpabuf_put_buf(buf, p2p->wfd_ie_probe_resp);
2225#endif /* CONFIG_WIFI_DISPLAY */
2226
Dmitry Shmidt2e67f062014-07-16 09:55:28 -07002227 if (p2p->vendor_elem && p2p->vendor_elem[VENDOR_ELEM_PROBE_RESP_P2P])
2228 wpabuf_put_buf(buf,
2229 p2p->vendor_elem[VENDOR_ELEM_PROBE_RESP_P2P]);
2230
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002231 /* P2P IE */
2232 len = p2p_buf_add_ie_hdr(buf);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002233 p2p_buf_add_capability(buf, p2p->dev_capab &
2234 ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002235 if (p2p->ext_listen_interval)
2236 p2p_buf_add_ext_listen_timing(buf, p2p->ext_listen_period,
2237 p2p->ext_listen_interval);
2238 p2p_buf_add_device_info(buf, p2p, NULL);
2239 p2p_buf_update_ie_hdr(buf, len);
2240
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002241 if (query_count) {
2242 p2p_buf_add_service_instance(buf, p2p, query_count, query_hash,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002243 p2p->p2ps_adv_list);
2244 }
2245
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002246 return buf;
2247}
2248
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07002249static int p2p_build_probe_resp_buf(struct p2p_data *p2p, struct wpabuf *buf,
2250 struct wpabuf *ies,
2251 const u8 *addr, int rx_freq)
2252{
2253 struct ieee80211_mgmt *resp;
2254 u8 channel, op_class;
2255
2256 resp = wpabuf_put(buf, offsetof(struct ieee80211_mgmt,
2257 u.probe_resp.variable));
2258
2259 resp->frame_control = host_to_le16((WLAN_FC_TYPE_MGMT << 2) |
2260 (WLAN_FC_STYPE_PROBE_RESP << 4));
2261 os_memcpy(resp->da, addr, ETH_ALEN);
2262 os_memcpy(resp->sa, p2p->cfg->dev_addr, ETH_ALEN);
2263 os_memcpy(resp->bssid, p2p->cfg->dev_addr, ETH_ALEN);
2264 resp->u.probe_resp.beacon_int = host_to_le16(100);
2265 /* hardware or low-level driver will setup seq_ctrl and timestamp */
2266 resp->u.probe_resp.capab_info =
2267 host_to_le16(WLAN_CAPABILITY_SHORT_PREAMBLE |
2268 WLAN_CAPABILITY_PRIVACY |
2269 WLAN_CAPABILITY_SHORT_SLOT_TIME);
2270
2271 wpabuf_put_u8(buf, WLAN_EID_SSID);
2272 wpabuf_put_u8(buf, P2P_WILDCARD_SSID_LEN);
2273 wpabuf_put_data(buf, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN);
2274
2275 wpabuf_put_u8(buf, WLAN_EID_SUPP_RATES);
2276 wpabuf_put_u8(buf, 8);
2277 wpabuf_put_u8(buf, (60 / 5) | 0x80);
2278 wpabuf_put_u8(buf, 90 / 5);
2279 wpabuf_put_u8(buf, (120 / 5) | 0x80);
2280 wpabuf_put_u8(buf, 180 / 5);
2281 wpabuf_put_u8(buf, (240 / 5) | 0x80);
2282 wpabuf_put_u8(buf, 360 / 5);
2283 wpabuf_put_u8(buf, 480 / 5);
2284 wpabuf_put_u8(buf, 540 / 5);
2285
2286 if (!rx_freq) {
2287 channel = p2p->cfg->channel;
2288 } else if (p2p_freq_to_channel(rx_freq, &op_class, &channel)) {
2289 p2p_err(p2p, "Failed to convert freq to channel");
2290 return -1;
2291 }
2292
2293 wpabuf_put_u8(buf, WLAN_EID_DS_PARAMS);
2294 wpabuf_put_u8(buf, 1);
2295 wpabuf_put_u8(buf, channel);
2296
2297 wpabuf_put_buf(buf, ies);
2298
2299 return 0;
2300}
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002301
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002302static int p2p_service_find_asp(struct p2p_data *p2p, const u8 *hash)
2303{
2304 struct p2ps_advertisement *adv_data;
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002305 int any_wfa;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002306
2307 p2p_dbg(p2p, "ASP find - ASP list: %p", p2p->p2ps_adv_list);
2308
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002309 /* Wildcard org.wi-fi.wfds matches any WFA spec defined service */
2310 any_wfa = os_memcmp(hash, p2p->wild_card_hash, P2PS_HASH_LEN) == 0;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002311
2312 adv_data = p2p->p2ps_adv_list;
2313 while (adv_data) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002314 if (os_memcmp(hash, adv_data->hash, P2PS_HASH_LEN) == 0)
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002315 return 1; /* exact hash match */
2316 if (any_wfa &&
2317 os_strncmp(adv_data->svc_name, P2PS_WILD_HASH_STR,
2318 os_strlen(P2PS_WILD_HASH_STR)) == 0)
2319 return 1; /* WFA service match */
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002320 adv_data = adv_data->next;
2321 }
2322
2323 return 0;
2324}
2325
2326
Dmitry Shmidt04949592012-07-19 12:16:46 -07002327static enum p2p_probe_req_status
2328p2p_reply_probe(struct p2p_data *p2p, const u8 *addr, const u8 *dst,
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002329 const u8 *bssid, const u8 *ie, size_t ie_len,
2330 unsigned int rx_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002331{
2332 struct ieee802_11_elems elems;
2333 struct wpabuf *buf;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002334 struct p2p_message msg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002335 struct wpabuf *ies;
2336
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002337 if (ieee802_11_parse_elems((u8 *) ie, ie_len, &elems, 0) ==
2338 ParseFailed) {
2339 /* Ignore invalid Probe Request frames */
Dmitry Shmidtec58b162014-02-19 12:44:18 -08002340 p2p_dbg(p2p, "Could not parse Probe Request frame - ignore it");
Dmitry Shmidt04949592012-07-19 12:16:46 -07002341 return P2P_PREQ_MALFORMED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002342 }
2343
2344 if (elems.p2p == NULL) {
2345 /* not a P2P probe - ignore it */
Dmitry Shmidtec58b162014-02-19 12:44:18 -08002346 p2p_dbg(p2p, "Not a P2P probe - ignore it");
Dmitry Shmidt04949592012-07-19 12:16:46 -07002347 return P2P_PREQ_NOT_P2P;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002348 }
2349
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002350 if (dst && !is_broadcast_ether_addr(dst) &&
2351 os_memcmp(dst, p2p->cfg->dev_addr, ETH_ALEN) != 0) {
2352 /* Not sent to the broadcast address or our P2P Device Address
2353 */
Dmitry Shmidtec58b162014-02-19 12:44:18 -08002354 p2p_dbg(p2p, "Probe Req DA " MACSTR " not ours - ignore it",
2355 MAC2STR(dst));
Dmitry Shmidt04949592012-07-19 12:16:46 -07002356 return P2P_PREQ_NOT_PROCESSED;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002357 }
2358
2359 if (bssid && !is_broadcast_ether_addr(bssid)) {
2360 /* Not sent to the Wildcard BSSID */
Dmitry Shmidtec58b162014-02-19 12:44:18 -08002361 p2p_dbg(p2p, "Probe Req BSSID " MACSTR " not wildcard - ignore it",
2362 MAC2STR(bssid));
Dmitry Shmidt04949592012-07-19 12:16:46 -07002363 return P2P_PREQ_NOT_PROCESSED;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002364 }
2365
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002366 if (elems.ssid == NULL || elems.ssid_len != P2P_WILDCARD_SSID_LEN ||
2367 os_memcmp(elems.ssid, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN) !=
2368 0) {
2369 /* not using P2P Wildcard SSID - ignore */
Dmitry Shmidtec58b162014-02-19 12:44:18 -08002370 p2p_dbg(p2p, "Probe Req not using P2P Wildcard SSID - ignore it");
Dmitry Shmidt04949592012-07-19 12:16:46 -07002371 return P2P_PREQ_NOT_PROCESSED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002372 }
2373
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002374 if (supp_rates_11b_only(&elems)) {
2375 /* Indicates support for 11b rates only */
Dmitry Shmidtec58b162014-02-19 12:44:18 -08002376 p2p_dbg(p2p, "Probe Req with 11b rates only supported - ignore it");
Dmitry Shmidt04949592012-07-19 12:16:46 -07002377 return P2P_PREQ_NOT_P2P;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002378 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002379
2380 os_memset(&msg, 0, sizeof(msg));
2381 if (p2p_parse_ies(ie, ie_len, &msg) < 0) {
2382 /* Could not parse P2P attributes */
Dmitry Shmidtec58b162014-02-19 12:44:18 -08002383 p2p_dbg(p2p, "Could not parse P2P attributes in Probe Req - ignore it");
Dmitry Shmidt04949592012-07-19 12:16:46 -07002384 return P2P_PREQ_NOT_P2P;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002385 }
2386
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002387 if (msg.service_hash && msg.service_hash_count) {
2388 const u8 *hash = msg.service_hash;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002389 u8 i;
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002390 int p2ps_svc_found = 0;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002391
Dmitry Shmidt41712582015-06-29 11:02:15 -07002392 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",
2393 p2p->in_listen, p2p->drv_in_listen, rx_freq,
2394 p2p->cfg->channel, p2p->pending_listen_freq);
2395
2396 if (!p2p->in_listen && !p2p->drv_in_listen &&
2397 p2p->pending_listen_freq && rx_freq &&
2398 rx_freq != p2p->pending_listen_freq) {
2399 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",
2400 rx_freq, p2p->pending_listen_freq);
2401 p2p_parse_free(&msg);
2402 return P2P_PREQ_NOT_LISTEN;
2403 }
2404
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002405 for (i = 0; i < msg.service_hash_count; i++) {
2406 if (p2p_service_find_asp(p2p, hash)) {
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002407 p2p_dbg(p2p, "Service Hash match found: "
2408 MACSTR, MAC2STR(hash));
2409 p2ps_svc_found = 1;
2410 break;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002411 }
2412 hash += P2PS_HASH_LEN;
2413 }
2414
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002415 /* Probed hash unknown */
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002416 if (!p2ps_svc_found) {
2417 p2p_dbg(p2p, "No Service Hash match found");
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002418 p2p_parse_free(&msg);
2419 return P2P_PREQ_NOT_PROCESSED;
2420 }
2421 } else {
2422 /* This is not a P2PS Probe Request */
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002423 p2p_dbg(p2p, "No P2PS Hash in Probe Request");
2424
2425 if (!p2p->in_listen || !p2p->drv_in_listen) {
2426 /* not in Listen state - ignore Probe Request */
2427 p2p_dbg(p2p, "Not in Listen state (in_listen=%d drv_in_listen=%d) - ignore Probe Request",
2428 p2p->in_listen, p2p->drv_in_listen);
2429 p2p_parse_free(&msg);
2430 return P2P_PREQ_NOT_LISTEN;
2431 }
2432 }
2433
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002434 if (msg.device_id &&
Dmitry Shmidt04949592012-07-19 12:16:46 -07002435 os_memcmp(msg.device_id, p2p->cfg->dev_addr, ETH_ALEN) != 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002436 /* Device ID did not match */
Dmitry Shmidtec58b162014-02-19 12:44:18 -08002437 p2p_dbg(p2p, "Probe Req requested Device ID " MACSTR " did not match - ignore it",
2438 MAC2STR(msg.device_id));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002439 p2p_parse_free(&msg);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002440 return P2P_PREQ_NOT_PROCESSED;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002441 }
2442
2443 /* Check Requested Device Type match */
2444 if (msg.wps_attributes &&
2445 !p2p_match_dev_type(p2p, msg.wps_attributes)) {
2446 /* No match with Requested Device Type */
Hai Shalom021b0b52019-04-10 11:17:58 -07002447 p2p_dbg(p2p, "Probe Req requested Device Type did not match - ignore it");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002448 p2p_parse_free(&msg);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002449 return P2P_PREQ_NOT_PROCESSED;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002450 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002451
Dmitry Shmidt04949592012-07-19 12:16:46 -07002452 if (!p2p->cfg->send_probe_resp) {
2453 /* Response generated elsewhere */
Dmitry Shmidtec58b162014-02-19 12:44:18 -08002454 p2p_dbg(p2p, "Probe Resp generated elsewhere - do not generate additional response");
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002455 p2p_parse_free(&msg);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002456 return P2P_PREQ_NOT_PROCESSED;
2457 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002458
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002459 p2p_dbg(p2p, "Reply to P2P Probe Request in Listen state");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002460
2461 /*
2462 * We do not really have a specific BSS that this frame is advertising,
2463 * so build a frame that has some information in valid format. This is
2464 * really only used for discovery purposes, not to learn exact BSS
2465 * parameters.
2466 */
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002467 ies = p2p_build_probe_resp_ies(p2p, msg.service_hash,
2468 msg.service_hash_count);
2469 p2p_parse_free(&msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002470 if (ies == NULL)
Dmitry Shmidt04949592012-07-19 12:16:46 -07002471 return P2P_PREQ_NOT_PROCESSED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002472
2473 buf = wpabuf_alloc(200 + wpabuf_len(ies));
2474 if (buf == NULL) {
2475 wpabuf_free(ies);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002476 return P2P_PREQ_NOT_PROCESSED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002477 }
2478
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07002479 if (p2p_build_probe_resp_buf(p2p, buf, ies, addr, rx_freq)) {
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002480 wpabuf_free(ies);
2481 wpabuf_free(buf);
2482 return P2P_PREQ_NOT_PROCESSED;
2483 }
2484
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002485 wpabuf_free(ies);
2486
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002487 p2p->cfg->send_probe_resp(p2p->cfg->cb_ctx, buf, rx_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002488
2489 wpabuf_free(buf);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002490
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002491 return P2P_PREQ_PROCESSED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002492}
2493
2494
Dmitry Shmidt04949592012-07-19 12:16:46 -07002495enum p2p_probe_req_status
2496p2p_probe_req_rx(struct p2p_data *p2p, const u8 *addr, const u8 *dst,
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002497 const u8 *bssid, const u8 *ie, size_t ie_len,
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07002498 unsigned int rx_freq, int p2p_lo_started)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002499{
Dmitry Shmidt04949592012-07-19 12:16:46 -07002500 enum p2p_probe_req_status res;
2501
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002502 p2p_add_dev_from_probe_req(p2p, addr, ie, ie_len);
2503
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07002504 if (p2p_lo_started) {
2505 p2p_dbg(p2p,
2506 "Probe Response is offloaded, do not reply Probe Request");
2507 return P2P_PREQ_PROCESSED;
2508 }
2509
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002510 res = p2p_reply_probe(p2p, addr, dst, bssid, ie, ie_len, rx_freq);
2511 if (res != P2P_PREQ_PROCESSED && res != P2P_PREQ_NOT_PROCESSED)
2512 return res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002513
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002514 /*
2515 * Activate a pending GO Negotiation/Invite flow if a received Probe
2516 * Request frame is from an expected peer. Some devices may share the
2517 * same address for P2P and non-P2P STA running simultaneously. The
2518 * P2P_PREQ_PROCESSED and P2P_PREQ_NOT_PROCESSED p2p_reply_probe()
2519 * return values verified above ensure we are handling a Probe Request
2520 * frame from a P2P peer.
2521 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002522 if ((p2p->state == P2P_CONNECT || p2p->state == P2P_CONNECT_LISTEN) &&
2523 p2p->go_neg_peer &&
2524 os_memcmp(addr, p2p->go_neg_peer->info.p2p_device_addr, ETH_ALEN)
Dmitry Shmidt9cdf1b92013-02-27 12:58:50 -08002525 == 0 &&
2526 !(p2p->go_neg_peer->flags & P2P_DEV_WAIT_GO_NEG_CONFIRM)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002527 /* Received a Probe Request from GO Negotiation peer */
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002528 p2p_dbg(p2p, "Found GO Negotiation peer - try to start GO negotiation from timeout");
Dmitry Shmidt9cdf1b92013-02-27 12:58:50 -08002529 eloop_cancel_timeout(p2p_go_neg_start, p2p, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002530 eloop_register_timeout(0, 0, p2p_go_neg_start, p2p, NULL);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002531 return res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002532 }
2533
2534 if ((p2p->state == P2P_INVITE || p2p->state == P2P_INVITE_LISTEN) &&
2535 p2p->invite_peer &&
Dmitry Shmidt3c479372014-02-04 10:50:36 -08002536 (p2p->invite_peer->flags & P2P_DEV_WAIT_INV_REQ_ACK) &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002537 os_memcmp(addr, p2p->invite_peer->info.p2p_device_addr, ETH_ALEN)
2538 == 0) {
2539 /* Received a Probe Request from Invite peer */
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002540 p2p_dbg(p2p, "Found Invite peer - try to start Invite from timeout");
Dmitry Shmidt7f93d6f2014-02-21 11:22:49 -08002541 eloop_cancel_timeout(p2p_invite_start, p2p, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002542 eloop_register_timeout(0, 0, p2p_invite_start, p2p, NULL);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002543 return res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002544 }
2545
Dmitry Shmidt04949592012-07-19 12:16:46 -07002546 return res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002547}
2548
2549
2550static int p2p_assoc_req_ie_wlan_ap(struct p2p_data *p2p, const u8 *bssid,
2551 u8 *buf, size_t len, struct wpabuf *p2p_ie)
2552{
2553 struct wpabuf *tmp;
2554 u8 *lpos;
2555 size_t tmplen;
2556 int res;
2557 u8 group_capab;
Dmitry Shmidt05df46a2015-05-19 11:02:01 -07002558 struct p2p_message msg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002559
2560 if (p2p_ie == NULL)
2561 return 0; /* WLAN AP is not a P2P manager */
2562
Dmitry Shmidt05df46a2015-05-19 11:02:01 -07002563 os_memset(&msg, 0, sizeof(msg));
2564 if (p2p_parse_p2p_ie(p2p_ie, &msg) < 0)
2565 return 0;
2566
2567 p2p_dbg(p2p, "BSS P2P manageability %s",
2568 msg.manageability ? "enabled" : "disabled");
2569
2570 if (!msg.manageability)
2571 return 0;
2572
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002573 /*
2574 * (Re)Association Request - P2P IE
2575 * P2P Capability attribute (shall be present)
2576 * P2P Interface attribute (present if concurrent device and
2577 * P2P Management is enabled)
2578 */
2579 tmp = wpabuf_alloc(200);
2580 if (tmp == NULL)
2581 return -1;
2582
2583 lpos = p2p_buf_add_ie_hdr(tmp);
2584 group_capab = 0;
2585 if (p2p->num_groups > 0) {
2586 group_capab |= P2P_GROUP_CAPAB_GROUP_OWNER;
2587 if ((p2p->dev_capab & P2P_DEV_CAPAB_CONCURRENT_OPER) &&
2588 (p2p->dev_capab & P2P_DEV_CAPAB_INFRA_MANAGED) &&
2589 p2p->cross_connect)
2590 group_capab |= P2P_GROUP_CAPAB_CROSS_CONN;
2591 }
2592 p2p_buf_add_capability(tmp, p2p->dev_capab, group_capab);
2593 if ((p2p->dev_capab & P2P_DEV_CAPAB_CONCURRENT_OPER) &&
2594 (p2p->dev_capab & P2P_DEV_CAPAB_INFRA_MANAGED))
2595 p2p_buf_add_p2p_interface(tmp, p2p);
2596 p2p_buf_update_ie_hdr(tmp, lpos);
2597
2598 tmplen = wpabuf_len(tmp);
2599 if (tmplen > len)
2600 res = -1;
2601 else {
2602 os_memcpy(buf, wpabuf_head(tmp), tmplen);
2603 res = tmplen;
2604 }
2605 wpabuf_free(tmp);
2606
2607 return res;
2608}
2609
2610
2611int p2p_assoc_req_ie(struct p2p_data *p2p, const u8 *bssid, u8 *buf,
2612 size_t len, int p2p_group, struct wpabuf *p2p_ie)
2613{
2614 struct wpabuf *tmp;
2615 u8 *lpos;
2616 struct p2p_device *peer;
2617 size_t tmplen;
2618 int res;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002619 size_t extra = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002620
2621 if (!p2p_group)
2622 return p2p_assoc_req_ie_wlan_ap(p2p, bssid, buf, len, p2p_ie);
2623
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002624#ifdef CONFIG_WIFI_DISPLAY
2625 if (p2p->wfd_ie_assoc_req)
2626 extra = wpabuf_len(p2p->wfd_ie_assoc_req);
2627#endif /* CONFIG_WIFI_DISPLAY */
2628
Dmitry Shmidt2e67f062014-07-16 09:55:28 -07002629 if (p2p->vendor_elem && p2p->vendor_elem[VENDOR_ELEM_P2P_ASSOC_REQ])
2630 extra += wpabuf_len(p2p->vendor_elem[VENDOR_ELEM_P2P_ASSOC_REQ]);
2631
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002632 /*
2633 * (Re)Association Request - P2P IE
2634 * P2P Capability attribute (shall be present)
2635 * Extended Listen Timing (may be present)
2636 * P2P Device Info attribute (shall be present)
2637 */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002638 tmp = wpabuf_alloc(200 + extra);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002639 if (tmp == NULL)
2640 return -1;
2641
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002642#ifdef CONFIG_WIFI_DISPLAY
2643 if (p2p->wfd_ie_assoc_req)
2644 wpabuf_put_buf(tmp, p2p->wfd_ie_assoc_req);
2645#endif /* CONFIG_WIFI_DISPLAY */
2646
Dmitry Shmidt2e67f062014-07-16 09:55:28 -07002647 if (p2p->vendor_elem && p2p->vendor_elem[VENDOR_ELEM_P2P_ASSOC_REQ])
2648 wpabuf_put_buf(tmp,
2649 p2p->vendor_elem[VENDOR_ELEM_P2P_ASSOC_REQ]);
2650
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002651 peer = bssid ? p2p_get_device(p2p, bssid) : NULL;
2652
2653 lpos = p2p_buf_add_ie_hdr(tmp);
2654 p2p_buf_add_capability(tmp, p2p->dev_capab, 0);
2655 if (p2p->ext_listen_interval)
2656 p2p_buf_add_ext_listen_timing(tmp, p2p->ext_listen_period,
2657 p2p->ext_listen_interval);
2658 p2p_buf_add_device_info(tmp, p2p, peer);
2659 p2p_buf_update_ie_hdr(tmp, lpos);
2660
2661 tmplen = wpabuf_len(tmp);
2662 if (tmplen > len)
2663 res = -1;
2664 else {
2665 os_memcpy(buf, wpabuf_head(tmp), tmplen);
2666 res = tmplen;
2667 }
2668 wpabuf_free(tmp);
2669
2670 return res;
2671}
2672
2673
2674int p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf, char *end)
2675{
2676 struct wpabuf *p2p_ie;
2677 int ret;
2678
2679 p2p_ie = ieee802_11_vendor_ie_concat(ies, ies_len, P2P_IE_VENDOR_TYPE);
2680 if (p2p_ie == NULL)
2681 return 0;
2682
2683 ret = p2p_attr_text(p2p_ie, buf, end);
2684 wpabuf_free(p2p_ie);
2685 return ret;
2686}
2687
2688
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002689struct p2ps_advertisement *
2690p2p_service_p2ps_id(struct p2p_data *p2p, u32 adv_id)
2691{
2692 struct p2ps_advertisement *adv_data;
2693
2694 if (!p2p)
2695 return NULL;
2696
2697 adv_data = p2p->p2ps_adv_list;
2698 while (adv_data) {
2699 if (adv_data->id == adv_id)
2700 return adv_data;
2701 adv_data = adv_data->next;
2702 }
2703
2704 return NULL;
2705}
2706
2707
2708int p2p_service_del_asp(struct p2p_data *p2p, u32 adv_id)
2709{
2710 struct p2ps_advertisement *adv_data;
2711 struct p2ps_advertisement **prior;
2712
2713 if (!p2p)
2714 return -1;
2715
2716 adv_data = p2p->p2ps_adv_list;
2717 prior = &p2p->p2ps_adv_list;
2718 while (adv_data) {
2719 if (adv_data->id == adv_id) {
2720 p2p_dbg(p2p, "Delete ASP adv_id=0x%x", adv_id);
2721 *prior = adv_data->next;
2722 os_free(adv_data);
2723 return 0;
2724 }
2725 prior = &adv_data->next;
2726 adv_data = adv_data->next;
2727 }
2728
2729 return -1;
2730}
2731
2732
2733int p2p_service_add_asp(struct p2p_data *p2p, int auto_accept, u32 adv_id,
2734 const char *adv_str, u8 svc_state, u16 config_methods,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002735 const char *svc_info, const u8 *cpt_priority)
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002736{
2737 struct p2ps_advertisement *adv_data, *tmp, **prev;
2738 u8 buf[P2PS_HASH_LEN];
2739 size_t adv_data_len, adv_len, info_len = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002740 int i;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002741
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002742 if (!p2p || !adv_str || !adv_str[0] || !cpt_priority)
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002743 return -1;
2744
2745 if (!(config_methods & p2p->cfg->config_methods)) {
2746 p2p_dbg(p2p, "Config methods not supported svc: 0x%x dev: 0x%x",
2747 config_methods, p2p->cfg->config_methods);
2748 return -1;
2749 }
2750
2751 if (!p2ps_gen_hash(p2p, adv_str, buf))
2752 return -1;
2753
2754 if (svc_info)
2755 info_len = os_strlen(svc_info);
2756 adv_len = os_strlen(adv_str);
2757 adv_data_len = sizeof(struct p2ps_advertisement) + adv_len + 1 +
2758 info_len + 1;
2759
2760 adv_data = os_zalloc(adv_data_len);
2761 if (!adv_data)
2762 return -1;
2763
2764 os_memcpy(adv_data->hash, buf, P2PS_HASH_LEN);
2765 adv_data->id = adv_id;
2766 adv_data->state = svc_state;
2767 adv_data->config_methods = config_methods & p2p->cfg->config_methods;
2768 adv_data->auto_accept = (u8) auto_accept;
2769 os_memcpy(adv_data->svc_name, adv_str, adv_len);
2770
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002771 for (i = 0; cpt_priority[i] && i < P2PS_FEATURE_CAPAB_CPT_MAX; i++) {
2772 adv_data->cpt_priority[i] = cpt_priority[i];
2773 adv_data->cpt_mask |= cpt_priority[i];
2774 }
2775
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002776 if (svc_info && info_len) {
2777 adv_data->svc_info = &adv_data->svc_name[adv_len + 1];
2778 os_memcpy(adv_data->svc_info, svc_info, info_len);
2779 }
2780
2781 /*
2782 * Group Advertisements by service string. They do not need to be
2783 * sorted, but groups allow easier Probe Response instance grouping
2784 */
2785 tmp = p2p->p2ps_adv_list;
2786 prev = &p2p->p2ps_adv_list;
2787 while (tmp) {
2788 if (tmp->id == adv_data->id) {
2789 if (os_strcmp(tmp->svc_name, adv_data->svc_name) != 0) {
2790 os_free(adv_data);
2791 return -1;
2792 }
2793 adv_data->next = tmp->next;
2794 *prev = adv_data;
2795 os_free(tmp);
2796 goto inserted;
2797 } else {
2798 if (os_strcmp(tmp->svc_name, adv_data->svc_name) == 0) {
2799 adv_data->next = tmp->next;
2800 tmp->next = adv_data;
2801 goto inserted;
2802 }
2803 }
2804 prev = &tmp->next;
2805 tmp = tmp->next;
2806 }
2807
2808 /* No svc_name match found */
2809 adv_data->next = p2p->p2ps_adv_list;
2810 p2p->p2ps_adv_list = adv_data;
2811
2812inserted:
2813 p2p_dbg(p2p,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002814 "Added ASP advertisement adv_id=0x%x config_methods=0x%x svc_state=0x%x adv_str='%s' cpt_mask=0x%x",
2815 adv_id, adv_data->config_methods, svc_state, adv_str,
2816 adv_data->cpt_mask);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002817
2818 return 0;
2819}
2820
2821
Dmitry Shmidtdda10c22015-03-24 16:05:01 -07002822void p2p_service_flush_asp(struct p2p_data *p2p)
2823{
2824 struct p2ps_advertisement *adv, *prev;
2825
2826 if (!p2p)
2827 return;
2828
2829 adv = p2p->p2ps_adv_list;
2830 while (adv) {
2831 prev = adv;
2832 adv = adv->next;
2833 os_free(prev);
2834 }
2835
2836 p2p->p2ps_adv_list = NULL;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002837 p2ps_prov_free(p2p);
Dmitry Shmidtdda10c22015-03-24 16:05:01 -07002838 p2p_dbg(p2p, "All ASP advertisements flushed");
2839}
2840
2841
Dmitry Shmidt04949592012-07-19 12:16:46 -07002842int p2p_parse_dev_addr_in_p2p_ie(struct wpabuf *p2p_ie, u8 *dev_addr)
2843{
2844 struct p2p_message msg;
2845
2846 os_memset(&msg, 0, sizeof(msg));
2847 if (p2p_parse_p2p_ie(p2p_ie, &msg))
2848 return -1;
2849
2850 if (msg.p2p_device_addr) {
2851 os_memcpy(dev_addr, msg.p2p_device_addr, ETH_ALEN);
2852 return 0;
2853 } else if (msg.device_id) {
2854 os_memcpy(dev_addr, msg.device_id, ETH_ALEN);
2855 return 0;
2856 }
2857 return -1;
2858}
2859
2860
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002861int p2p_parse_dev_addr(const u8 *ies, size_t ies_len, u8 *dev_addr)
2862{
2863 struct wpabuf *p2p_ie;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002864 int ret;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002865
2866 p2p_ie = ieee802_11_vendor_ie_concat(ies, ies_len,
2867 P2P_IE_VENDOR_TYPE);
2868 if (p2p_ie == NULL)
2869 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002870 ret = p2p_parse_dev_addr_in_p2p_ie(p2p_ie, dev_addr);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002871 wpabuf_free(p2p_ie);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002872 return ret;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002873}
2874
2875
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002876static void p2p_clear_go_neg(struct p2p_data *p2p)
2877{
2878 p2p->go_neg_peer = NULL;
2879 p2p_clear_timeout(p2p);
2880 p2p_set_state(p2p, P2P_IDLE);
2881}
2882
2883
2884void p2p_wps_success_cb(struct p2p_data *p2p, const u8 *mac_addr)
2885{
2886 if (p2p->go_neg_peer == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002887 p2p_dbg(p2p, "No pending Group Formation - ignore WPS registration success notification");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002888 return; /* No pending Group Formation */
2889 }
2890
2891 if (os_memcmp(mac_addr, p2p->go_neg_peer->intended_addr, ETH_ALEN) !=
2892 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002893 p2p_dbg(p2p, "Ignore WPS registration success notification for "
2894 MACSTR " (GO Negotiation peer " MACSTR ")",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002895 MAC2STR(mac_addr),
2896 MAC2STR(p2p->go_neg_peer->intended_addr));
2897 return; /* Ignore unexpected peer address */
2898 }
2899
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002900 p2p_dbg(p2p, "Group Formation completed successfully with " MACSTR,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002901 MAC2STR(mac_addr));
2902
2903 p2p_clear_go_neg(p2p);
2904}
2905
2906
2907void p2p_group_formation_failed(struct p2p_data *p2p)
2908{
2909 if (p2p->go_neg_peer == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002910 p2p_dbg(p2p, "No pending Group Formation - ignore group formation failure notification");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002911 return; /* No pending Group Formation */
2912 }
2913
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002914 p2p_dbg(p2p, "Group Formation failed with " MACSTR,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002915 MAC2STR(p2p->go_neg_peer->intended_addr));
2916
2917 p2p_clear_go_neg(p2p);
2918}
2919
2920
2921struct p2p_data * p2p_init(const struct p2p_config *cfg)
2922{
2923 struct p2p_data *p2p;
2924
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07002925 if (cfg->max_peers < 1 ||
2926 cfg->passphrase_len < 8 || cfg->passphrase_len > 63)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002927 return NULL;
2928
2929 p2p = os_zalloc(sizeof(*p2p) + sizeof(*cfg));
2930 if (p2p == NULL)
2931 return NULL;
2932 p2p->cfg = (struct p2p_config *) (p2p + 1);
2933 os_memcpy(p2p->cfg, cfg, sizeof(*cfg));
2934 if (cfg->dev_name)
2935 p2p->cfg->dev_name = os_strdup(cfg->dev_name);
2936 if (cfg->manufacturer)
2937 p2p->cfg->manufacturer = os_strdup(cfg->manufacturer);
2938 if (cfg->model_name)
2939 p2p->cfg->model_name = os_strdup(cfg->model_name);
2940 if (cfg->model_number)
2941 p2p->cfg->model_number = os_strdup(cfg->model_number);
2942 if (cfg->serial_number)
2943 p2p->cfg->serial_number = os_strdup(cfg->serial_number);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002944 if (cfg->pref_chan) {
2945 p2p->cfg->pref_chan = os_malloc(cfg->num_pref_chan *
2946 sizeof(struct p2p_channel));
2947 if (p2p->cfg->pref_chan) {
2948 os_memcpy(p2p->cfg->pref_chan, cfg->pref_chan,
2949 cfg->num_pref_chan *
2950 sizeof(struct p2p_channel));
2951 } else
2952 p2p->cfg->num_pref_chan = 0;
2953 }
2954
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002955 p2ps_gen_hash(p2p, P2PS_WILD_HASH_STR, p2p->wild_card_hash);
2956
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002957 p2p->min_disc_int = 1;
2958 p2p->max_disc_int = 3;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002959 p2p->max_disc_tu = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002960
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07002961 if (os_get_random(&p2p->next_tie_breaker, 1) < 0)
2962 p2p->next_tie_breaker = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002963 p2p->next_tie_breaker &= 0x01;
2964 if (cfg->sd_request)
2965 p2p->dev_capab |= P2P_DEV_CAPAB_SERVICE_DISCOVERY;
2966 p2p->dev_capab |= P2P_DEV_CAPAB_INVITATION_PROCEDURE;
2967 if (cfg->concurrent_operations)
2968 p2p->dev_capab |= P2P_DEV_CAPAB_CONCURRENT_OPER;
2969 p2p->dev_capab |= P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
2970
2971 dl_list_init(&p2p->devices);
2972
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002973 p2p->go_timeout = 100;
2974 p2p->client_timeout = 20;
Dmitry Shmidt13ca8d82014-02-20 10:18:40 -08002975 p2p->num_p2p_sd_queries = 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002976
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002977 p2p_dbg(p2p, "initialized");
2978 p2p_channels_dump(p2p, "channels", &p2p->cfg->channels);
2979 p2p_channels_dump(p2p, "cli_channels", &p2p->cfg->cli_channels);
2980
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002981 return p2p;
2982}
2983
2984
2985void p2p_deinit(struct p2p_data *p2p)
2986{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002987#ifdef CONFIG_WIFI_DISPLAY
2988 wpabuf_free(p2p->wfd_ie_beacon);
2989 wpabuf_free(p2p->wfd_ie_probe_req);
2990 wpabuf_free(p2p->wfd_ie_probe_resp);
2991 wpabuf_free(p2p->wfd_ie_assoc_req);
2992 wpabuf_free(p2p->wfd_ie_invitation);
2993 wpabuf_free(p2p->wfd_ie_prov_disc_req);
2994 wpabuf_free(p2p->wfd_ie_prov_disc_resp);
2995 wpabuf_free(p2p->wfd_ie_go_neg);
2996 wpabuf_free(p2p->wfd_dev_info);
2997 wpabuf_free(p2p->wfd_assoc_bssid);
2998 wpabuf_free(p2p->wfd_coupled_sink_info);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002999 wpabuf_free(p2p->wfd_r2_dev_info);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003000#endif /* CONFIG_WIFI_DISPLAY */
3001
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003002 eloop_cancel_timeout(p2p_scan_timeout, p2p, NULL);
Dmitry Shmidt9cdf1b92013-02-27 12:58:50 -08003003 eloop_cancel_timeout(p2p_go_neg_start, p2p, NULL);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003004 eloop_cancel_timeout(p2p_go_neg_wait_timeout, p2p, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003005 p2p_flush(p2p);
3006 p2p_free_req_dev_types(p2p);
3007 os_free(p2p->cfg->dev_name);
3008 os_free(p2p->cfg->manufacturer);
3009 os_free(p2p->cfg->model_name);
3010 os_free(p2p->cfg->model_number);
3011 os_free(p2p->cfg->serial_number);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003012 os_free(p2p->cfg->pref_chan);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003013 os_free(p2p->groups);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07003014 p2ps_prov_free(p2p);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003015 wpabuf_free(p2p->sd_resp);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003016 p2p_remove_wps_vendor_extensions(p2p);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003017 os_free(p2p->no_go_freq.range);
Dmitry Shmidtdda10c22015-03-24 16:05:01 -07003018 p2p_service_flush_asp(p2p);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003019
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003020 os_free(p2p);
3021}
3022
3023
3024void p2p_flush(struct p2p_data *p2p)
3025{
3026 struct p2p_device *dev, *prev;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003027
3028 p2p_ext_listen(p2p, 0, 0);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003029 p2p_stop_find(p2p);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003030 dl_list_for_each_safe(dev, prev, &p2p->devices, struct p2p_device,
3031 list) {
3032 dl_list_del(&dev->list);
3033 p2p_device_free(p2p, dev);
3034 }
3035 p2p_free_sd_queries(p2p);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003036 p2p->ssid_set = 0;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003037 p2ps_prov_free(p2p);
3038 p2p_reset_pending_pd(p2p);
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08003039 p2p->override_pref_op_class = 0;
3040 p2p->override_pref_channel = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003041}
3042
3043
3044int p2p_unauthorize(struct p2p_data *p2p, const u8 *addr)
3045{
3046 struct p2p_device *dev;
3047
3048 dev = p2p_get_device(p2p, addr);
3049 if (dev == NULL)
3050 return -1;
3051
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003052 p2p_dbg(p2p, "Unauthorizing " MACSTR, MAC2STR(addr));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003053
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003054 if (p2p->go_neg_peer == dev) {
3055 eloop_cancel_timeout(p2p_go_neg_wait_timeout, p2p, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003056 p2p->go_neg_peer = NULL;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003057 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003058
3059 dev->wps_method = WPS_NOT_READY;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003060 dev->oob_pw_id = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003061 dev->flags &= ~P2P_DEV_WAIT_GO_NEG_RESPONSE;
3062 dev->flags &= ~P2P_DEV_WAIT_GO_NEG_CONFIRM;
3063
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003064 return 0;
3065}
3066
3067
3068int p2p_set_dev_name(struct p2p_data *p2p, const char *dev_name)
3069{
3070 os_free(p2p->cfg->dev_name);
3071 if (dev_name) {
3072 p2p->cfg->dev_name = os_strdup(dev_name);
3073 if (p2p->cfg->dev_name == NULL)
3074 return -1;
3075 } else
3076 p2p->cfg->dev_name = NULL;
3077 return 0;
3078}
3079
3080
3081int p2p_set_manufacturer(struct p2p_data *p2p, const char *manufacturer)
3082{
3083 os_free(p2p->cfg->manufacturer);
3084 p2p->cfg->manufacturer = NULL;
3085 if (manufacturer) {
3086 p2p->cfg->manufacturer = os_strdup(manufacturer);
3087 if (p2p->cfg->manufacturer == NULL)
3088 return -1;
3089 }
3090
3091 return 0;
3092}
3093
3094
3095int p2p_set_model_name(struct p2p_data *p2p, const char *model_name)
3096{
3097 os_free(p2p->cfg->model_name);
3098 p2p->cfg->model_name = NULL;
3099 if (model_name) {
3100 p2p->cfg->model_name = os_strdup(model_name);
3101 if (p2p->cfg->model_name == NULL)
3102 return -1;
3103 }
3104
3105 return 0;
3106}
3107
3108
3109int p2p_set_model_number(struct p2p_data *p2p, const char *model_number)
3110{
3111 os_free(p2p->cfg->model_number);
3112 p2p->cfg->model_number = NULL;
3113 if (model_number) {
3114 p2p->cfg->model_number = os_strdup(model_number);
3115 if (p2p->cfg->model_number == NULL)
3116 return -1;
3117 }
3118
3119 return 0;
3120}
3121
3122
3123int p2p_set_serial_number(struct p2p_data *p2p, const char *serial_number)
3124{
3125 os_free(p2p->cfg->serial_number);
3126 p2p->cfg->serial_number = NULL;
3127 if (serial_number) {
3128 p2p->cfg->serial_number = os_strdup(serial_number);
3129 if (p2p->cfg->serial_number == NULL)
3130 return -1;
3131 }
3132
3133 return 0;
3134}
3135
3136
3137void p2p_set_config_methods(struct p2p_data *p2p, u16 config_methods)
3138{
3139 p2p->cfg->config_methods = config_methods;
3140}
3141
3142
3143void p2p_set_uuid(struct p2p_data *p2p, const u8 *uuid)
3144{
3145 os_memcpy(p2p->cfg->uuid, uuid, 16);
3146}
3147
3148
3149int p2p_set_pri_dev_type(struct p2p_data *p2p, const u8 *pri_dev_type)
3150{
3151 os_memcpy(p2p->cfg->pri_dev_type, pri_dev_type, 8);
3152 return 0;
3153}
3154
3155
3156int p2p_set_sec_dev_types(struct p2p_data *p2p, const u8 dev_types[][8],
3157 size_t num_dev_types)
3158{
3159 if (num_dev_types > P2P_SEC_DEVICE_TYPES)
3160 num_dev_types = P2P_SEC_DEVICE_TYPES;
3161 p2p->cfg->num_sec_dev_types = num_dev_types;
3162 os_memcpy(p2p->cfg->sec_dev_type, dev_types, num_dev_types * 8);
3163 return 0;
3164}
3165
3166
3167void p2p_remove_wps_vendor_extensions(struct p2p_data *p2p)
3168{
3169 int i;
3170
3171 for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) {
3172 wpabuf_free(p2p->wps_vendor_ext[i]);
3173 p2p->wps_vendor_ext[i] = NULL;
3174 }
3175}
3176
3177
3178int p2p_add_wps_vendor_extension(struct p2p_data *p2p,
3179 const struct wpabuf *vendor_ext)
3180{
3181 int i;
3182
3183 if (vendor_ext == NULL)
3184 return -1;
3185
3186 for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) {
3187 if (p2p->wps_vendor_ext[i] == NULL)
3188 break;
3189 }
3190 if (i >= P2P_MAX_WPS_VENDOR_EXT)
3191 return -1;
3192
3193 p2p->wps_vendor_ext[i] = wpabuf_dup(vendor_ext);
3194 if (p2p->wps_vendor_ext[i] == NULL)
3195 return -1;
3196
3197 return 0;
3198}
3199
3200
3201int p2p_set_country(struct p2p_data *p2p, const char *country)
3202{
3203 os_memcpy(p2p->cfg->country, country, 3);
3204 return 0;
3205}
3206
3207
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003208static int p2p_pre_find_operation(struct p2p_data *p2p, struct p2p_device *dev)
3209{
Dmitry Shmidtde47be72016-01-07 12:52:55 -08003210 int res;
3211
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003212 if (dev->sd_pending_bcast_queries == 0) {
3213 /* Initialize with total number of registered broadcast
3214 * SD queries. */
3215 dev->sd_pending_bcast_queries = p2p->num_p2p_sd_queries;
3216 }
3217
Dmitry Shmidtde47be72016-01-07 12:52:55 -08003218 res = p2p_start_sd(p2p, dev);
3219 if (res == -2)
3220 return -2;
3221 if (res == 0)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003222 return 1;
3223
3224 if (dev->req_config_methods &&
3225 !(dev->flags & P2P_DEV_PD_FOR_JOIN)) {
3226 p2p_dbg(p2p, "Send pending Provision Discovery Request to "
3227 MACSTR " (config methods 0x%x)",
3228 MAC2STR(dev->info.p2p_device_addr),
3229 dev->req_config_methods);
3230 if (p2p_send_prov_disc_req(p2p, dev, 0, 0) == 0)
3231 return 1;
3232 }
3233
3234 return 0;
3235}
3236
3237
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003238void p2p_continue_find(struct p2p_data *p2p)
3239{
3240 struct p2p_device *dev;
Dmitry Shmidtde47be72016-01-07 12:52:55 -08003241 int found, res;
Dmitry Shmidt13ca8d82014-02-20 10:18:40 -08003242
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003243 p2p_set_state(p2p, P2P_SEARCH);
3244
3245 /* Continue from the device following the last iteration */
3246 found = 0;
3247 dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
3248 if (dev == p2p->last_p2p_find_oper) {
3249 found = 1;
3250 continue;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003251 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003252 if (!found)
3253 continue;
Dmitry Shmidtde47be72016-01-07 12:52:55 -08003254 res = p2p_pre_find_operation(p2p, dev);
3255 if (res > 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003256 p2p->last_p2p_find_oper = dev;
3257 return;
3258 }
Dmitry Shmidtde47be72016-01-07 12:52:55 -08003259 if (res == -2)
3260 goto skip_sd;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003261 }
3262
3263 /*
3264 * Wrap around to the beginning of the list and continue until the last
3265 * iteration device.
3266 */
3267 dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
Dmitry Shmidtde47be72016-01-07 12:52:55 -08003268 res = p2p_pre_find_operation(p2p, dev);
3269 if (res > 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003270 p2p->last_p2p_find_oper = dev;
3271 return;
3272 }
Dmitry Shmidtde47be72016-01-07 12:52:55 -08003273 if (res == -2)
3274 goto skip_sd;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003275 if (dev == p2p->last_p2p_find_oper)
3276 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003277 }
3278
Dmitry Shmidtde47be72016-01-07 12:52:55 -08003279skip_sd:
3280 os_memset(p2p->sd_query_no_ack, 0, ETH_ALEN);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003281 p2p_listen_in_find(p2p, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003282}
3283
3284
3285static void p2p_sd_cb(struct p2p_data *p2p, int success)
3286{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003287 p2p_dbg(p2p, "Service Discovery Query TX callback: success=%d",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003288 success);
3289 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
3290
3291 if (!success) {
Dmitry Shmidtde47be72016-01-07 12:52:55 -08003292 if (p2p->sd_peer) {
3293 if (is_zero_ether_addr(p2p->sd_query_no_ack)) {
3294 os_memcpy(p2p->sd_query_no_ack,
3295 p2p->sd_peer->info.p2p_device_addr,
3296 ETH_ALEN);
3297 p2p_dbg(p2p,
3298 "First SD Query no-ACK in this search iteration: "
3299 MACSTR, MAC2STR(p2p->sd_query_no_ack));
3300 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003301 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
Dmitry Shmidtde47be72016-01-07 12:52:55 -08003302 }
Dmitry Shmidt13ca8d82014-02-20 10:18:40 -08003303 p2p->sd_peer = NULL;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003304 if (p2p->state != P2P_IDLE)
3305 p2p_continue_find(p2p);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003306 return;
3307 }
3308
3309 if (p2p->sd_peer == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003310 p2p_dbg(p2p, "No SD peer entry known");
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003311 if (p2p->state != P2P_IDLE)
3312 p2p_continue_find(p2p);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003313 return;
3314 }
3315
Dmitry Shmidt807291d2015-01-27 13:40:23 -08003316 if (p2p->sd_query && p2p->sd_query->for_all_peers) {
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07003317 /* Update the pending broadcast SD query count for this device
3318 */
3319 p2p->sd_peer->sd_pending_bcast_queries--;
3320
3321 /*
3322 * If there are no pending broadcast queries for this device,
3323 * mark it as done (-1).
3324 */
3325 if (p2p->sd_peer->sd_pending_bcast_queries == 0)
3326 p2p->sd_peer->sd_pending_bcast_queries = -1;
3327 }
3328
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003329 /* Wait for response from the peer */
3330 p2p_set_state(p2p, P2P_SD_DURING_FIND);
3331 p2p_set_timeout(p2p, 0, 200000);
3332}
3333
Jouni Malinen75ecf522011-06-27 15:19:46 -07003334
3335/**
3336 * p2p_retry_pd - Retry any pending provision disc requests in IDLE state
3337 * @p2p: P2P module context from p2p_init()
3338 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003339static void p2p_retry_pd(struct p2p_data *p2p)
Jouni Malinen75ecf522011-06-27 15:19:46 -07003340{
3341 struct p2p_device *dev;
3342
Jouni Malinen75ecf522011-06-27 15:19:46 -07003343 /*
3344 * Retry the prov disc req attempt only for the peer that the user had
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003345 * requested.
Jouni Malinen75ecf522011-06-27 15:19:46 -07003346 */
3347
3348 dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
3349 if (os_memcmp(p2p->pending_pd_devaddr,
3350 dev->info.p2p_device_addr, ETH_ALEN) != 0)
3351 continue;
3352 if (!dev->req_config_methods)
3353 continue;
Jouni Malinen75ecf522011-06-27 15:19:46 -07003354
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003355 p2p_dbg(p2p, "Send pending Provision Discovery Request to "
Jouni Malinen75ecf522011-06-27 15:19:46 -07003356 MACSTR " (config methods 0x%x)",
3357 MAC2STR(dev->info.p2p_device_addr),
3358 dev->req_config_methods);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003359 p2p_send_prov_disc_req(p2p, dev,
Dmitry Shmidt051af732013-10-22 13:52:46 -07003360 dev->flags & P2P_DEV_PD_FOR_JOIN,
3361 p2p->pd_force_freq);
Jouni Malinen75ecf522011-06-27 15:19:46 -07003362 return;
3363 }
3364}
3365
3366
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003367static void p2p_prov_disc_cb(struct p2p_data *p2p, int success)
3368{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003369 p2p_dbg(p2p, "Provision Discovery Request TX callback: success=%d",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003370 success);
Jouni Malinen75ecf522011-06-27 15:19:46 -07003371
3372 /*
3373 * Postpone resetting the pending action state till after we actually
3374 * time out. This allows us to take some action like notifying any
3375 * interested parties about no response to the request.
3376 *
3377 * When the timer (below) goes off we check in IDLE, SEARCH, or
3378 * LISTEN_ONLY state, which are the only allowed states to issue a PD
3379 * requests in, if this was still pending and then raise notification.
3380 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003381
3382 if (!success) {
Jouni Malinen75ecf522011-06-27 15:19:46 -07003383 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
3384
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003385 if (p2p->user_initiated_pd &&
3386 (p2p->state == P2P_SEARCH || p2p->state == P2P_LISTEN_ONLY))
3387 {
3388 /* Retry request from timeout to avoid busy loops */
3389 p2p->pending_action_state = P2P_PENDING_PD;
3390 p2p_set_timeout(p2p, 0, 50000);
3391 } else if (p2p->state != P2P_IDLE)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003392 p2p_continue_find(p2p);
Jouni Malinen75ecf522011-06-27 15:19:46 -07003393 else if (p2p->user_initiated_pd) {
3394 p2p->pending_action_state = P2P_PENDING_PD;
3395 p2p_set_timeout(p2p, 0, 300000);
3396 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003397 return;
3398 }
3399
Jouni Malinen75ecf522011-06-27 15:19:46 -07003400 /*
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003401 * If after PD Request the peer doesn't expect to receive PD Response
3402 * the PD Request ACK indicates a completion of the current PD. This
3403 * happens only on the advertiser side sending the follow-on PD Request
3404 * with the status different than 12 (Success: accepted by user).
3405 */
3406 if (p2p->p2ps_prov && !p2p->p2ps_prov->pd_seeker &&
3407 p2p->p2ps_prov->status != P2P_SC_SUCCESS_DEFERRED) {
3408 p2p_dbg(p2p, "P2PS PD completion on Follow-on PD Request ACK");
3409
3410 if (p2p->send_action_in_progress) {
3411 p2p->send_action_in_progress = 0;
3412 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
3413 }
3414
3415 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
3416
3417 if (p2p->cfg->p2ps_prov_complete) {
3418 p2p->cfg->p2ps_prov_complete(
3419 p2p->cfg->cb_ctx,
3420 p2p->p2ps_prov->status,
3421 p2p->p2ps_prov->adv_mac,
3422 p2p->p2ps_prov->adv_mac,
3423 p2p->p2ps_prov->session_mac,
3424 NULL, p2p->p2ps_prov->adv_id,
3425 p2p->p2ps_prov->session_id,
3426 0, 0, NULL, 0, 0, 0,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08003427 NULL, NULL, 0, 0, NULL, 0);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003428 }
3429
3430 if (p2p->user_initiated_pd)
3431 p2p_reset_pending_pd(p2p);
3432
3433 p2ps_prov_free(p2p);
3434 return;
3435 }
3436
3437 /*
Jouni Malinen75ecf522011-06-27 15:19:46 -07003438 * This postponing, of resetting pending_action_state, needs to be
3439 * done only for user initiated PD requests and not internal ones.
3440 */
3441 if (p2p->user_initiated_pd)
3442 p2p->pending_action_state = P2P_PENDING_PD;
3443 else
3444 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
3445
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003446 /* Wait for response from the peer */
3447 if (p2p->state == P2P_SEARCH)
3448 p2p_set_state(p2p, P2P_PD_DURING_FIND);
3449 p2p_set_timeout(p2p, 0, 200000);
3450}
3451
3452
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003453static void p2p_prov_disc_resp_cb(struct p2p_data *p2p, int success)
3454{
3455 p2p_dbg(p2p, "Provision Discovery Response TX callback: success=%d",
3456 success);
3457
3458 if (p2p->send_action_in_progress) {
3459 p2p->send_action_in_progress = 0;
3460 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
3461 }
3462
3463 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
3464
Jimmy Chen5ef7aad2019-10-15 15:45:26 +08003465 if (!success) {
3466 if (p2p->state == P2P_SEARCH)
3467 p2p_continue_find(p2p);
Hai Shalom81f62d82019-07-22 12:10:00 -07003468 return;
Jimmy Chen5ef7aad2019-10-15 15:45:26 +08003469 }
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003470
3471 if (!p2p->cfg->prov_disc_resp_cb ||
Jimmy Chen5ef7aad2019-10-15 15:45:26 +08003472 p2p->cfg->prov_disc_resp_cb(p2p->cfg->cb_ctx) < 1) {
3473 if (p2p->state == P2P_SEARCH)
3474 p2p_continue_find(p2p);
Hai Shalom81f62d82019-07-22 12:10:00 -07003475 return;
Jimmy Chen5ef7aad2019-10-15 15:45:26 +08003476 }
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003477
3478 p2p_dbg(p2p,
3479 "Post-Provision Discovery operations started - do not try to continue other P2P operations");
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003480}
3481
3482
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003483int p2p_scan_res_handler(struct p2p_data *p2p, const u8 *bssid, int freq,
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003484 struct os_reltime *rx_time, int level, const u8 *ies,
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003485 size_t ies_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003486{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003487 if (os_reltime_before(rx_time, &p2p->find_start)) {
Dmitry Shmidtf8623282013-02-20 14:34:59 -08003488 /*
3489 * The driver may have cached (e.g., in cfg80211 BSS table) the
3490 * scan results for relatively long time. To avoid reporting
3491 * stale information, update P2P peers only based on results
3492 * that have based on frames received after the last p2p_find
3493 * operation was started.
3494 */
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003495 p2p_dbg(p2p, "Ignore old scan result for " MACSTR
Dmitry Shmidtde47be72016-01-07 12:52:55 -08003496 " (rx_time=%u.%06u find_start=%u.%06u)",
Dmitry Shmidtf8623282013-02-20 14:34:59 -08003497 MAC2STR(bssid), (unsigned int) rx_time->sec,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08003498 (unsigned int) rx_time->usec,
3499 (unsigned int) p2p->find_start.sec,
3500 (unsigned int) p2p->find_start.usec);
Dmitry Shmidtf8623282013-02-20 14:34:59 -08003501 return 0;
3502 }
3503
3504 p2p_add_device(p2p, bssid, freq, rx_time, level, ies, ies_len, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003505
3506 return 0;
3507}
3508
3509
3510void p2p_scan_res_handled(struct p2p_data *p2p)
3511{
3512 if (!p2p->p2p_scan_running) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003513 p2p_dbg(p2p, "p2p_scan was not running, but scan results received");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003514 }
3515 p2p->p2p_scan_running = 0;
3516 eloop_cancel_timeout(p2p_scan_timeout, p2p, NULL);
3517
3518 if (p2p_run_after_scan(p2p))
3519 return;
3520 if (p2p->state == P2P_SEARCH)
3521 p2p_continue_find(p2p);
3522}
3523
3524
Dmitry Shmidt9c175262016-03-03 10:20:07 -08003525void p2p_scan_ie(struct p2p_data *p2p, struct wpabuf *ies, const u8 *dev_id,
3526 unsigned int bands)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003527{
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003528 u8 dev_capab;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003529 u8 *len;
3530
3531#ifdef CONFIG_WIFI_DISPLAY
3532 if (p2p->wfd_ie_probe_req)
3533 wpabuf_put_buf(ies, p2p->wfd_ie_probe_req);
3534#endif /* CONFIG_WIFI_DISPLAY */
3535
Dmitry Shmidt2e67f062014-07-16 09:55:28 -07003536 if (p2p->vendor_elem && p2p->vendor_elem[VENDOR_ELEM_PROBE_REQ_P2P])
3537 wpabuf_put_buf(ies,
3538 p2p->vendor_elem[VENDOR_ELEM_PROBE_REQ_P2P]);
3539
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003540 len = p2p_buf_add_ie_hdr(ies);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003541
3542 dev_capab = p2p->dev_capab & ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
3543
3544 /* P2PS requires Probe Request frames to include SD bit */
3545 if (p2p->p2ps_seek && p2p->p2ps_seek_count)
3546 dev_capab |= P2P_DEV_CAPAB_SERVICE_DISCOVERY;
3547
3548 p2p_buf_add_capability(ies, dev_capab, 0);
3549
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003550 if (dev_id)
3551 p2p_buf_add_device_id(ies, dev_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003552 if (p2p->cfg->reg_class && p2p->cfg->channel)
3553 p2p_buf_add_listen_channel(ies, p2p->cfg->country,
3554 p2p->cfg->reg_class,
3555 p2p->cfg->channel);
3556 if (p2p->ext_listen_interval)
3557 p2p_buf_add_ext_listen_timing(ies, p2p->ext_listen_period,
3558 p2p->ext_listen_interval);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003559
Dmitry Shmidt9c175262016-03-03 10:20:07 -08003560 if (bands & BAND_60_GHZ)
3561 p2p_buf_add_device_info(ies, p2p, NULL);
3562
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003563 if (p2p->p2ps_seek && p2p->p2ps_seek_count)
3564 p2p_buf_add_service_hash(ies, p2p);
3565
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003566 /* TODO: p2p_buf_add_operating_channel() if GO */
3567 p2p_buf_update_ie_hdr(ies, len);
3568}
3569
3570
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003571size_t p2p_scan_ie_buf_len(struct p2p_data *p2p)
3572{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003573 size_t len = 100;
3574
3575#ifdef CONFIG_WIFI_DISPLAY
3576 if (p2p && p2p->wfd_ie_probe_req)
3577 len += wpabuf_len(p2p->wfd_ie_probe_req);
3578#endif /* CONFIG_WIFI_DISPLAY */
3579
Dmitry Shmidt2e67f062014-07-16 09:55:28 -07003580 if (p2p && p2p->vendor_elem &&
3581 p2p->vendor_elem[VENDOR_ELEM_PROBE_REQ_P2P])
3582 len += wpabuf_len(p2p->vendor_elem[VENDOR_ELEM_PROBE_REQ_P2P]);
3583
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003584 return len;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003585}
3586
3587
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003588int p2p_ie_text(struct wpabuf *p2p_ie, char *buf, char *end)
3589{
3590 return p2p_attr_text(p2p_ie, buf, end);
3591}
3592
3593
3594static void p2p_go_neg_req_cb(struct p2p_data *p2p, int success)
3595{
3596 struct p2p_device *dev = p2p->go_neg_peer;
Dmitry Shmidt8c652892013-03-01 10:14:01 -08003597 int timeout;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003598
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003599 p2p_dbg(p2p, "GO Negotiation Request TX callback: success=%d", success);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003600
3601 if (dev == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003602 p2p_dbg(p2p, "No pending GO Negotiation");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003603 return;
3604 }
3605
3606 if (success) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003607 if (dev->flags & P2P_DEV_USER_REJECTED) {
3608 p2p_set_state(p2p, P2P_IDLE);
3609 return;
3610 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07003611 } else if (dev->go_neg_req_sent) {
3612 /* Cancel the increment from p2p_connect_send() on failure */
Dmitry Shmidt98f9e762012-05-30 11:18:46 -07003613 dev->go_neg_req_sent--;
3614 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003615
3616 if (!success &&
3617 (dev->info.dev_capab & P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY) &&
3618 !is_zero_ether_addr(dev->member_in_go_dev)) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003619 p2p_dbg(p2p, "Peer " MACSTR " did not acknowledge request - try to use device discoverability through its GO",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003620 MAC2STR(dev->info.p2p_device_addr));
3621 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
3622 p2p_send_dev_disc_req(p2p, dev);
3623 return;
3624 }
3625
3626 /*
3627 * Use P2P find, if needed, to find the other device from its listen
3628 * channel.
3629 */
3630 p2p_set_state(p2p, P2P_CONNECT);
Dmitry Shmidt8c652892013-03-01 10:14:01 -08003631 timeout = success ? 500000 : 100000;
3632 if (!success && p2p->go_neg_peer &&
3633 (p2p->go_neg_peer->flags & P2P_DEV_PEER_WAITING_RESPONSE)) {
3634 unsigned int r;
3635 /*
3636 * Peer is expected to wait our response and we will skip the
3637 * listen phase. Add some randomness to the wait time here to
3638 * make it less likely to hit cases where we could end up in
3639 * sync with peer not listening.
3640 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07003641 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
3642 r = 0;
Dmitry Shmidt8c652892013-03-01 10:14:01 -08003643 timeout += r % 100000;
3644 }
3645 p2p_set_timeout(p2p, 0, timeout);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003646}
3647
3648
3649static void p2p_go_neg_resp_cb(struct p2p_data *p2p, int success)
3650{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003651 p2p_dbg(p2p, "GO Negotiation Response TX callback: success=%d",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003652 success);
3653 if (!p2p->go_neg_peer && p2p->state == P2P_PROVISIONING) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003654 p2p_dbg(p2p, "Ignore TX callback event - GO Negotiation is not running anymore");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003655 return;
3656 }
3657 p2p_set_state(p2p, P2P_CONNECT);
Dmitry Shmidt8c652892013-03-01 10:14:01 -08003658 p2p_set_timeout(p2p, 0, 500000);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003659}
3660
3661
Dmitry Shmidt4b060592013-04-29 16:42:49 -07003662static void p2p_go_neg_resp_failure_cb(struct p2p_data *p2p, int success,
3663 const u8 *addr)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003664{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003665 p2p_dbg(p2p, "GO Negotiation Response (failure) TX callback: success=%d", success);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003666 if (p2p->go_neg_peer && p2p->go_neg_peer->status != P2P_SC_SUCCESS) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003667 p2p_go_neg_failed(p2p, p2p->go_neg_peer->status);
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08003668 return;
3669 }
3670
3671 if (success) {
Dmitry Shmidt4b060592013-04-29 16:42:49 -07003672 struct p2p_device *dev;
3673 dev = p2p_get_device(p2p, addr);
3674 if (dev &&
Dmitry Shmidt34c12022015-03-05 14:11:20 -08003675 dev->status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE)
Dmitry Shmidt4b060592013-04-29 16:42:49 -07003676 dev->flags |= P2P_DEV_PEER_WAITING_RESPONSE;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003677 }
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08003678
3679 if (p2p->state == P2P_SEARCH || p2p->state == P2P_SD_DURING_FIND)
3680 p2p_continue_find(p2p);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003681}
3682
3683
3684static void p2p_go_neg_conf_cb(struct p2p_data *p2p,
3685 enum p2p_send_action_result result)
3686{
3687 struct p2p_device *dev;
3688
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003689 p2p_dbg(p2p, "GO Negotiation Confirm TX callback: result=%d", result);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003690 if (result == P2P_SEND_ACTION_FAILED) {
Dmitry Shmidt413dde72014-04-11 10:23:22 -07003691 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003692 p2p_go_neg_failed(p2p, -1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003693 return;
3694 }
Dmitry Shmidt413dde72014-04-11 10:23:22 -07003695
3696 dev = p2p->go_neg_peer;
3697
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003698 if (result == P2P_SEND_ACTION_NO_ACK) {
3699 /*
Dmitry Shmidt413dde72014-04-11 10:23:22 -07003700 * Retry GO Negotiation Confirmation
3701 * P2P_GO_NEG_CNF_MAX_RETRY_COUNT times if we did not receive
3702 * ACK for confirmation.
3703 */
3704 if (dev && dev->go_neg_conf &&
3705 dev->go_neg_conf_sent <= P2P_GO_NEG_CNF_MAX_RETRY_COUNT) {
3706 p2p_dbg(p2p, "GO Negotiation Confirm retry %d",
3707 dev->go_neg_conf_sent);
3708 p2p->pending_action_state = P2P_PENDING_GO_NEG_CONFIRM;
3709 if (p2p_send_action(p2p, dev->go_neg_conf_freq,
3710 dev->info.p2p_device_addr,
3711 p2p->cfg->dev_addr,
3712 dev->info.p2p_device_addr,
3713 wpabuf_head(dev->go_neg_conf),
3714 wpabuf_len(dev->go_neg_conf), 0) >=
3715 0) {
3716 dev->go_neg_conf_sent++;
3717 return;
3718 }
3719 p2p_dbg(p2p, "Failed to re-send Action frame");
3720
3721 /*
3722 * Continue with the assumption that the first attempt
3723 * went through and just the ACK frame was lost.
3724 */
3725 }
3726
3727 /*
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003728 * It looks like the TX status for GO Negotiation Confirm is
3729 * often showing failure even when the peer has actually
3730 * received the frame. Since the peer may change channels
3731 * immediately after having received the frame, we may not see
3732 * an Ack for retries, so just dropping a single frame may
3733 * trigger this. To allow the group formation to succeed if the
3734 * peer did indeed receive the frame, continue regardless of
3735 * the TX status.
3736 */
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003737 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 -07003738 }
3739
Dmitry Shmidt413dde72014-04-11 10:23:22 -07003740 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
3741
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003742 if (dev == NULL)
3743 return;
3744
3745 p2p_go_complete(p2p, dev);
3746}
3747
3748
3749void p2p_send_action_cb(struct p2p_data *p2p, unsigned int freq, const u8 *dst,
3750 const u8 *src, const u8 *bssid,
3751 enum p2p_send_action_result result)
3752{
3753 enum p2p_pending_action_state state;
3754 int success;
3755
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003756 p2p_dbg(p2p, "Action frame TX callback (state=%d freq=%u dst=" MACSTR
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003757 " src=" MACSTR " bssid=" MACSTR " result=%d p2p_state=%s)",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003758 p2p->pending_action_state, freq, MAC2STR(dst), MAC2STR(src),
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003759 MAC2STR(bssid), result, p2p_state_txt(p2p->state));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003760 success = result == P2P_SEND_ACTION_SUCCESS;
3761 state = p2p->pending_action_state;
3762 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
3763 switch (state) {
3764 case P2P_NO_PENDING_ACTION:
Dmitry Shmidt4582d2a2014-02-28 11:14:23 -08003765 if (p2p->send_action_in_progress) {
3766 p2p->send_action_in_progress = 0;
3767 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
3768 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003769 break;
3770 case P2P_PENDING_GO_NEG_REQUEST:
3771 p2p_go_neg_req_cb(p2p, success);
3772 break;
3773 case P2P_PENDING_GO_NEG_RESPONSE:
3774 p2p_go_neg_resp_cb(p2p, success);
3775 break;
3776 case P2P_PENDING_GO_NEG_RESPONSE_FAILURE:
Dmitry Shmidt4b060592013-04-29 16:42:49 -07003777 p2p_go_neg_resp_failure_cb(p2p, success, dst);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003778 break;
3779 case P2P_PENDING_GO_NEG_CONFIRM:
3780 p2p_go_neg_conf_cb(p2p, result);
3781 break;
3782 case P2P_PENDING_SD:
3783 p2p_sd_cb(p2p, success);
3784 break;
3785 case P2P_PENDING_PD:
3786 p2p_prov_disc_cb(p2p, success);
3787 break;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003788 case P2P_PENDING_PD_RESPONSE:
3789 p2p_prov_disc_resp_cb(p2p, success);
3790 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003791 case P2P_PENDING_INVITATION_REQUEST:
3792 p2p_invitation_req_cb(p2p, success);
3793 break;
3794 case P2P_PENDING_INVITATION_RESPONSE:
3795 p2p_invitation_resp_cb(p2p, success);
3796 break;
3797 case P2P_PENDING_DEV_DISC_REQUEST:
3798 p2p_dev_disc_req_cb(p2p, success);
3799 break;
3800 case P2P_PENDING_DEV_DISC_RESPONSE:
3801 p2p_dev_disc_resp_cb(p2p, success);
3802 break;
3803 case P2P_PENDING_GO_DISC_REQ:
3804 p2p_go_disc_req_cb(p2p, success);
3805 break;
3806 }
3807}
3808
3809
3810void p2p_listen_cb(struct p2p_data *p2p, unsigned int freq,
3811 unsigned int duration)
3812{
3813 if (freq == p2p->pending_client_disc_freq) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003814 p2p_dbg(p2p, "Client discoverability remain-awake completed");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003815 p2p->pending_client_disc_freq = 0;
3816 return;
3817 }
3818
3819 if (freq != p2p->pending_listen_freq) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003820 p2p_dbg(p2p, "Unexpected listen callback for freq=%u duration=%u (pending_listen_freq=%u)",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003821 freq, duration, p2p->pending_listen_freq);
3822 return;
3823 }
3824
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003825 p2p_dbg(p2p, "Starting Listen timeout(%u,%u) on freq=%u based on callback",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003826 p2p->pending_listen_sec, p2p->pending_listen_usec,
3827 p2p->pending_listen_freq);
3828 p2p->in_listen = 1;
3829 p2p->drv_in_listen = freq;
3830 if (p2p->pending_listen_sec || p2p->pending_listen_usec) {
3831 /*
3832 * Add 20 msec extra wait to avoid race condition with driver
3833 * remain-on-channel end event, i.e., give driver more time to
3834 * complete the operation before our timeout expires.
3835 */
3836 p2p_set_timeout(p2p, p2p->pending_listen_sec,
3837 p2p->pending_listen_usec + 20000);
3838 }
3839
3840 p2p->pending_listen_freq = 0;
3841}
3842
3843
3844int p2p_listen_end(struct p2p_data *p2p, unsigned int freq)
3845{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003846 p2p_dbg(p2p, "Driver ended Listen state (freq=%u)", freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003847 p2p->drv_in_listen = 0;
3848 if (p2p->in_listen)
3849 return 0; /* Internal timeout will trigger the next step */
3850
Roshan Pius3a1667e2018-07-03 15:17:14 -07003851 if (p2p->state == P2P_WAIT_PEER_CONNECT && p2p->go_neg_peer &&
3852 p2p->pending_listen_freq) {
3853 /*
3854 * Better wait a bit if the driver is unable to start
3855 * offchannel operation for some reason to continue with
3856 * P2P_WAIT_PEER_(IDLE/CONNECT) state transitions.
3857 */
3858 p2p_dbg(p2p,
3859 "Listen operation did not seem to start - delay idle phase to avoid busy loop");
3860 p2p_set_timeout(p2p, 0, 100000);
3861 return 1;
3862 }
3863
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003864 if (p2p->state == P2P_CONNECT_LISTEN && p2p->go_neg_peer) {
3865 if (p2p->go_neg_peer->connect_reqs >= 120) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003866 p2p_dbg(p2p, "Timeout on sending GO Negotiation Request without getting response");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003867 p2p_go_neg_failed(p2p, -1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003868 return 0;
3869 }
3870
3871 p2p_set_state(p2p, P2P_CONNECT);
3872 p2p_connect_send(p2p, p2p->go_neg_peer);
3873 return 1;
3874 } else if (p2p->state == P2P_SEARCH) {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003875 if (p2p->p2p_scan_running) {
3876 /*
3877 * Search is already in progress. This can happen if
3878 * an Action frame RX is reported immediately after
3879 * the end of a remain-on-channel operation and the
3880 * response frame to that is sent using an offchannel
3881 * operation while in p2p_find. Avoid an attempt to
3882 * restart a scan here.
3883 */
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003884 p2p_dbg(p2p, "p2p_scan already in progress - do not try to start a new one");
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003885 return 1;
3886 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07003887 if (p2p->pending_listen_freq) {
3888 /*
3889 * Better wait a bit if the driver is unable to start
3890 * offchannel operation for some reason. p2p_search()
3891 * will be started from internal timeout.
3892 */
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003893 p2p_dbg(p2p, "Listen operation did not seem to start - delay search phase to avoid busy loop");
Dmitry Shmidt04949592012-07-19 12:16:46 -07003894 p2p_set_timeout(p2p, 0, 100000);
3895 return 1;
3896 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003897 if (p2p->search_delay) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003898 p2p_dbg(p2p, "Delay search operation by %u ms",
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003899 p2p->search_delay);
3900 p2p_set_timeout(p2p, p2p->search_delay / 1000,
3901 (p2p->search_delay % 1000) * 1000);
3902 return 1;
3903 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003904 p2p_search(p2p);
3905 return 1;
3906 }
3907
3908 return 0;
3909}
3910
3911
3912static void p2p_timeout_connect(struct p2p_data *p2p)
3913{
3914 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003915 if (p2p->go_neg_peer &&
3916 (p2p->go_neg_peer->flags & P2P_DEV_WAIT_GO_NEG_CONFIRM)) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003917 p2p_dbg(p2p, "Wait for GO Negotiation Confirm timed out - assume GO Negotiation failed");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003918 p2p_go_neg_failed(p2p, -1);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003919 return;
3920 }
Dmitry Shmidt8c652892013-03-01 10:14:01 -08003921 if (p2p->go_neg_peer &&
3922 (p2p->go_neg_peer->flags & P2P_DEV_PEER_WAITING_RESPONSE) &&
3923 p2p->go_neg_peer->connect_reqs < 120) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003924 p2p_dbg(p2p, "Peer expected to wait our response - skip listen");
Dmitry Shmidt8c652892013-03-01 10:14:01 -08003925 p2p_connect_send(p2p, p2p->go_neg_peer);
3926 return;
3927 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003928 if (p2p->go_neg_peer && p2p->go_neg_peer->oob_go_neg_freq > 0) {
3929 p2p_dbg(p2p, "Skip connect-listen since GO Neg channel known (OOB)");
3930 p2p_set_state(p2p, P2P_CONNECT_LISTEN);
3931 p2p_set_timeout(p2p, 0, 30000);
3932 return;
3933 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003934 p2p_set_state(p2p, P2P_CONNECT_LISTEN);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003935 p2p_listen_in_find(p2p, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003936}
3937
3938
3939static void p2p_timeout_connect_listen(struct p2p_data *p2p)
3940{
3941 if (p2p->go_neg_peer) {
3942 if (p2p->drv_in_listen) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003943 p2p_dbg(p2p, "Driver is still in Listen state; wait for it to complete");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003944 return;
3945 }
3946
3947 if (p2p->go_neg_peer->connect_reqs >= 120) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003948 p2p_dbg(p2p, "Timeout on sending GO Negotiation Request without getting response");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003949 p2p_go_neg_failed(p2p, -1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003950 return;
3951 }
3952
3953 p2p_set_state(p2p, P2P_CONNECT);
3954 p2p_connect_send(p2p, p2p->go_neg_peer);
3955 } else
3956 p2p_set_state(p2p, P2P_IDLE);
3957}
3958
3959
3960static void p2p_timeout_wait_peer_connect(struct p2p_data *p2p)
3961{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003962 p2p_set_state(p2p, P2P_WAIT_PEER_IDLE);
Dmitry Shmidt18463232014-01-24 12:29:41 -08003963
3964 if (p2p->cfg->is_concurrent_session_active &&
3965 p2p->cfg->is_concurrent_session_active(p2p->cfg->cb_ctx))
3966 p2p_set_timeout(p2p, 0, 500000);
3967 else
3968 p2p_set_timeout(p2p, 0, 200000);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003969}
3970
3971
3972static void p2p_timeout_wait_peer_idle(struct p2p_data *p2p)
3973{
3974 struct p2p_device *dev = p2p->go_neg_peer;
3975
3976 if (dev == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003977 p2p_dbg(p2p, "Unknown GO Neg peer - stop GO Neg wait");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003978 return;
3979 }
3980
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003981 p2p_dbg(p2p, "Go to Listen state while waiting for the peer to become ready for GO Negotiation");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003982 p2p_set_state(p2p, P2P_WAIT_PEER_CONNECT);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003983 p2p_listen_in_find(p2p, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003984}
3985
3986
3987static void p2p_timeout_sd_during_find(struct p2p_data *p2p)
3988{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003989 p2p_dbg(p2p, "Service Discovery Query timeout");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003990 if (p2p->sd_peer) {
3991 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003992 p2p->sd_peer = NULL;
3993 }
3994 p2p_continue_find(p2p);
3995}
3996
3997
3998static void p2p_timeout_prov_disc_during_find(struct p2p_data *p2p)
3999{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004000 p2p_dbg(p2p, "Provision Discovery Request timeout");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004001 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
4002 p2p_continue_find(p2p);
4003}
4004
4005
Jouni Malinen75ecf522011-06-27 15:19:46 -07004006static void p2p_timeout_prov_disc_req(struct p2p_data *p2p)
4007{
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004008 u32 adv_id = 0;
4009 u8 *adv_mac = NULL;
4010
Jouni Malinen75ecf522011-06-27 15:19:46 -07004011 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
4012
4013 /*
4014 * For user initiated PD requests that we have not gotten any responses
4015 * for while in IDLE state, we retry them a couple of times before
4016 * giving up.
4017 */
4018 if (!p2p->user_initiated_pd)
4019 return;
4020
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004021 p2p_dbg(p2p, "User initiated Provision Discovery Request timeout");
Jouni Malinen75ecf522011-06-27 15:19:46 -07004022
4023 if (p2p->pd_retries) {
4024 p2p->pd_retries--;
4025 p2p_retry_pd(p2p);
4026 } else {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004027 struct p2p_device *dev;
4028 int for_join = 0;
4029
4030 dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
4031 if (os_memcmp(p2p->pending_pd_devaddr,
4032 dev->info.p2p_device_addr, ETH_ALEN) != 0)
4033 continue;
4034 if (dev->req_config_methods &&
4035 (dev->flags & P2P_DEV_PD_FOR_JOIN))
4036 for_join = 1;
4037 }
4038
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004039 if (p2p->p2ps_prov) {
4040 adv_id = p2p->p2ps_prov->adv_id;
4041 adv_mac = p2p->p2ps_prov->adv_mac;
4042 }
4043
Jouni Malinen75ecf522011-06-27 15:19:46 -07004044 if (p2p->cfg->prov_disc_fail)
4045 p2p->cfg->prov_disc_fail(p2p->cfg->cb_ctx,
4046 p2p->pending_pd_devaddr,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004047 for_join ?
4048 P2P_PROV_DISC_TIMEOUT_JOIN :
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004049 P2P_PROV_DISC_TIMEOUT,
4050 adv_id, adv_mac, NULL);
Jouni Malinen75ecf522011-06-27 15:19:46 -07004051 p2p_reset_pending_pd(p2p);
4052 }
4053}
4054
4055
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004056static void p2p_timeout_invite(struct p2p_data *p2p)
4057{
4058 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
4059 p2p_set_state(p2p, P2P_INVITE_LISTEN);
4060 if (p2p->inv_role == P2P_INVITE_ROLE_ACTIVE_GO) {
4061 /*
4062 * Better remain on operating channel instead of listen channel
4063 * when running a group.
4064 */
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004065 p2p_dbg(p2p, "Inviting in active GO role - wait on operating channel");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004066 p2p_set_timeout(p2p, 0, 100000);
4067 return;
4068 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004069 p2p_listen_in_find(p2p, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004070}
4071
4072
4073static void p2p_timeout_invite_listen(struct p2p_data *p2p)
4074{
4075 if (p2p->invite_peer && p2p->invite_peer->invitation_reqs < 100) {
4076 p2p_set_state(p2p, P2P_INVITE);
4077 p2p_invite_send(p2p, p2p->invite_peer,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004078 p2p->invite_go_dev_addr, p2p->invite_dev_pw_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004079 } else {
4080 if (p2p->invite_peer) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004081 p2p_dbg(p2p, "Invitation Request retry limit reached");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004082 if (p2p->cfg->invitation_result)
4083 p2p->cfg->invitation_result(
Dmitry Shmidt700a1372013-03-15 14:14:44 -07004084 p2p->cfg->cb_ctx, -1, NULL, NULL,
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004085 p2p->invite_peer->info.p2p_device_addr,
Dmitry Shmidt15907092014-03-25 10:42:57 -07004086 0, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004087 }
4088 p2p_set_state(p2p, P2P_IDLE);
4089 }
4090}
4091
4092
4093static void p2p_state_timeout(void *eloop_ctx, void *timeout_ctx)
4094{
4095 struct p2p_data *p2p = eloop_ctx;
4096
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004097 p2p_dbg(p2p, "Timeout (state=%s)", p2p_state_txt(p2p->state));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004098
4099 p2p->in_listen = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004100 if (p2p->drv_in_listen) {
4101 p2p_dbg(p2p, "Driver is still in listen state - stop it");
4102 p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
4103 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004104
4105 switch (p2p->state) {
4106 case P2P_IDLE:
Jouni Malinen75ecf522011-06-27 15:19:46 -07004107 /* Check if we timed out waiting for PD req */
4108 if (p2p->pending_action_state == P2P_PENDING_PD)
4109 p2p_timeout_prov_disc_req(p2p);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004110 break;
4111 case P2P_SEARCH:
Jouni Malinen75ecf522011-06-27 15:19:46 -07004112 /* Check if we timed out waiting for PD req */
4113 if (p2p->pending_action_state == P2P_PENDING_PD)
4114 p2p_timeout_prov_disc_req(p2p);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004115 if (p2p->search_delay && !p2p->in_search_delay) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004116 p2p_dbg(p2p, "Delay search operation by %u ms",
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004117 p2p->search_delay);
4118 p2p->in_search_delay = 1;
4119 p2p_set_timeout(p2p, p2p->search_delay / 1000,
4120 (p2p->search_delay % 1000) * 1000);
4121 break;
4122 }
4123 p2p->in_search_delay = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004124 p2p_search(p2p);
4125 break;
4126 case P2P_CONNECT:
4127 p2p_timeout_connect(p2p);
4128 break;
4129 case P2P_CONNECT_LISTEN:
4130 p2p_timeout_connect_listen(p2p);
4131 break;
4132 case P2P_GO_NEG:
4133 break;
4134 case P2P_LISTEN_ONLY:
Jouni Malinen75ecf522011-06-27 15:19:46 -07004135 /* Check if we timed out waiting for PD req */
4136 if (p2p->pending_action_state == P2P_PENDING_PD)
4137 p2p_timeout_prov_disc_req(p2p);
4138
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004139 if (p2p->ext_listen_only) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004140 p2p_dbg(p2p, "Extended Listen Timing - Listen State completed");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004141 p2p->ext_listen_only = 0;
4142 p2p_set_state(p2p, P2P_IDLE);
4143 }
4144 break;
4145 case P2P_WAIT_PEER_CONNECT:
4146 p2p_timeout_wait_peer_connect(p2p);
4147 break;
4148 case P2P_WAIT_PEER_IDLE:
4149 p2p_timeout_wait_peer_idle(p2p);
4150 break;
4151 case P2P_SD_DURING_FIND:
4152 p2p_timeout_sd_during_find(p2p);
4153 break;
4154 case P2P_PROVISIONING:
4155 break;
4156 case P2P_PD_DURING_FIND:
4157 p2p_timeout_prov_disc_during_find(p2p);
4158 break;
4159 case P2P_INVITE:
4160 p2p_timeout_invite(p2p);
4161 break;
4162 case P2P_INVITE_LISTEN:
4163 p2p_timeout_invite_listen(p2p);
4164 break;
4165 }
4166}
4167
4168
4169int p2p_reject(struct p2p_data *p2p, const u8 *peer_addr)
4170{
4171 struct p2p_device *dev;
4172
4173 dev = p2p_get_device(p2p, peer_addr);
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004174 p2p_dbg(p2p, "Local request to reject connection attempts by peer "
4175 MACSTR, MAC2STR(peer_addr));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004176 if (dev == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004177 p2p_dbg(p2p, "Peer " MACSTR " unknown", MAC2STR(peer_addr));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004178 return -1;
4179 }
4180 dev->status = P2P_SC_FAIL_REJECTED_BY_USER;
4181 dev->flags |= P2P_DEV_USER_REJECTED;
4182 return 0;
4183}
4184
4185
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004186const char * p2p_wps_method_text(enum p2p_wps_method method)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004187{
4188 switch (method) {
4189 case WPS_NOT_READY:
4190 return "not-ready";
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004191 case WPS_PIN_DISPLAY:
4192 return "Display";
4193 case WPS_PIN_KEYPAD:
4194 return "Keypad";
4195 case WPS_PBC:
4196 return "PBC";
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004197 case WPS_NFC:
4198 return "NFC";
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004199 case WPS_P2PS:
4200 return "P2PS";
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004201 }
4202
4203 return "??";
4204}
4205
4206
4207static const char * p2p_go_state_text(enum p2p_go_state go_state)
4208{
4209 switch (go_state) {
4210 case UNKNOWN_GO:
4211 return "unknown";
4212 case LOCAL_GO:
4213 return "local";
4214 case REMOTE_GO:
4215 return "remote";
4216 }
4217
4218 return "??";
4219}
4220
4221
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004222const struct p2p_peer_info * p2p_get_peer_info(struct p2p_data *p2p,
4223 const u8 *addr, int next)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004224{
4225 struct p2p_device *dev;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004226
4227 if (addr)
4228 dev = p2p_get_device(p2p, addr);
4229 else
4230 dev = dl_list_first(&p2p->devices, struct p2p_device, list);
4231
4232 if (dev && next) {
4233 dev = dl_list_first(&dev->list, struct p2p_device, list);
4234 if (&dev->list == &p2p->devices)
4235 dev = NULL;
4236 }
4237
4238 if (dev == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004239 return NULL;
4240
4241 return &dev->info;
4242}
4243
4244
4245int p2p_get_peer_info_txt(const struct p2p_peer_info *info,
4246 char *buf, size_t buflen)
4247{
4248 struct p2p_device *dev;
4249 int res;
4250 char *pos, *end;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004251 struct os_reltime now;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004252
4253 if (info == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004254 return -1;
4255
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004256 dev = (struct p2p_device *) (((u8 *) info) -
4257 offsetof(struct p2p_device, info));
4258
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004259 pos = buf;
4260 end = buf + buflen;
4261
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004262 os_get_reltime(&now);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004263 res = os_snprintf(pos, end - pos,
4264 "age=%d\n"
4265 "listen_freq=%d\n"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004266 "wps_method=%s\n"
4267 "interface_addr=" MACSTR "\n"
4268 "member_in_go_dev=" MACSTR "\n"
4269 "member_in_go_iface=" MACSTR "\n"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004270 "go_neg_req_sent=%d\n"
4271 "go_state=%s\n"
4272 "dialog_token=%u\n"
4273 "intended_addr=" MACSTR "\n"
4274 "country=%c%c\n"
4275 "oper_freq=%d\n"
4276 "req_config_methods=0x%x\n"
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07004277 "flags=%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004278 "status=%d\n"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004279 "invitation_reqs=%u\n",
4280 (int) (now.sec - dev->last_seen.sec),
4281 dev->listen_freq,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004282 p2p_wps_method_text(dev->wps_method),
4283 MAC2STR(dev->interface_addr),
4284 MAC2STR(dev->member_in_go_dev),
4285 MAC2STR(dev->member_in_go_iface),
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004286 dev->go_neg_req_sent,
4287 p2p_go_state_text(dev->go_state),
4288 dev->dialog_token,
4289 MAC2STR(dev->intended_addr),
4290 dev->country[0] ? dev->country[0] : '_',
4291 dev->country[1] ? dev->country[1] : '_',
4292 dev->oper_freq,
4293 dev->req_config_methods,
4294 dev->flags & P2P_DEV_PROBE_REQ_ONLY ?
4295 "[PROBE_REQ_ONLY]" : "",
4296 dev->flags & P2P_DEV_REPORTED ? "[REPORTED]" : "",
4297 dev->flags & P2P_DEV_NOT_YET_READY ?
4298 "[NOT_YET_READY]" : "",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004299 dev->flags & P2P_DEV_PD_PEER_DISPLAY ?
4300 "[PD_PEER_DISPLAY]" : "",
4301 dev->flags & P2P_DEV_PD_PEER_KEYPAD ?
4302 "[PD_PEER_KEYPAD]" : "",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004303 dev->flags & P2P_DEV_PD_PEER_P2PS ?
4304 "[PD_PEER_P2PS]" : "",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004305 dev->flags & P2P_DEV_USER_REJECTED ?
4306 "[USER_REJECTED]" : "",
4307 dev->flags & P2P_DEV_PEER_WAITING_RESPONSE ?
4308 "[PEER_WAITING_RESPONSE]" : "",
4309 dev->flags & P2P_DEV_PREFER_PERSISTENT_GROUP ?
4310 "[PREFER_PERSISTENT_GROUP]" : "",
4311 dev->flags & P2P_DEV_WAIT_GO_NEG_RESPONSE ?
4312 "[WAIT_GO_NEG_RESPONSE]" : "",
4313 dev->flags & P2P_DEV_WAIT_GO_NEG_CONFIRM ?
4314 "[WAIT_GO_NEG_CONFIRM]" : "",
4315 dev->flags & P2P_DEV_GROUP_CLIENT_ONLY ?
4316 "[GROUP_CLIENT_ONLY]" : "",
4317 dev->flags & P2P_DEV_FORCE_FREQ ?
4318 "[FORCE_FREQ]" : "",
4319 dev->flags & P2P_DEV_PD_FOR_JOIN ?
4320 "[PD_FOR_JOIN]" : "",
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07004321 dev->flags & P2P_DEV_LAST_SEEN_AS_GROUP_CLIENT ?
4322 "[LAST_SEEN_AS_GROUP_CLIENT]" : "",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004323 dev->status,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004324 dev->invitation_reqs);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004325 if (os_snprintf_error(end - pos, res))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004326 return pos - buf;
4327 pos += res;
4328
4329 if (dev->ext_listen_period) {
4330 res = os_snprintf(pos, end - pos,
4331 "ext_listen_period=%u\n"
4332 "ext_listen_interval=%u\n",
4333 dev->ext_listen_period,
4334 dev->ext_listen_interval);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004335 if (os_snprintf_error(end - pos, res))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004336 return pos - buf;
4337 pos += res;
4338 }
4339
4340 if (dev->oper_ssid_len) {
4341 res = os_snprintf(pos, end - pos,
4342 "oper_ssid=%s\n",
4343 wpa_ssid_txt(dev->oper_ssid,
4344 dev->oper_ssid_len));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004345 if (os_snprintf_error(end - pos, res))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004346 return pos - buf;
4347 pos += res;
4348 }
4349
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004350#ifdef CONFIG_WIFI_DISPLAY
4351 if (dev->info.wfd_subelems) {
4352 res = os_snprintf(pos, end - pos, "wfd_subelems=");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004353 if (os_snprintf_error(end - pos, res))
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004354 return pos - buf;
4355 pos += res;
4356
4357 pos += wpa_snprintf_hex(pos, end - pos,
4358 wpabuf_head(dev->info.wfd_subelems),
4359 wpabuf_len(dev->info.wfd_subelems));
4360
4361 res = os_snprintf(pos, end - pos, "\n");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004362 if (os_snprintf_error(end - pos, res))
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004363 return pos - buf;
4364 pos += res;
4365 }
4366#endif /* CONFIG_WIFI_DISPLAY */
4367
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004368 return pos - buf;
4369}
4370
4371
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004372int p2p_peer_known(struct p2p_data *p2p, const u8 *addr)
4373{
4374 return p2p_get_device(p2p, addr) != NULL;
4375}
4376
4377
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004378void p2p_set_client_discoverability(struct p2p_data *p2p, int enabled)
4379{
4380 if (enabled) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004381 p2p_dbg(p2p, "Client discoverability enabled");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004382 p2p->dev_capab |= P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
4383 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004384 p2p_dbg(p2p, "Client discoverability disabled");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004385 p2p->dev_capab &= ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
4386 }
4387}
4388
4389
4390static struct wpabuf * p2p_build_presence_req(u32 duration1, u32 interval1,
4391 u32 duration2, u32 interval2)
4392{
4393 struct wpabuf *req;
4394 struct p2p_noa_desc desc1, desc2, *ptr1 = NULL, *ptr2 = NULL;
4395 u8 *len;
4396
4397 req = wpabuf_alloc(100);
4398 if (req == NULL)
4399 return NULL;
4400
4401 if (duration1 || interval1) {
4402 os_memset(&desc1, 0, sizeof(desc1));
4403 desc1.count_type = 1;
4404 desc1.duration = duration1;
4405 desc1.interval = interval1;
4406 ptr1 = &desc1;
4407
4408 if (duration2 || interval2) {
4409 os_memset(&desc2, 0, sizeof(desc2));
4410 desc2.count_type = 2;
4411 desc2.duration = duration2;
4412 desc2.interval = interval2;
4413 ptr2 = &desc2;
4414 }
4415 }
4416
4417 p2p_buf_add_action_hdr(req, P2P_PRESENCE_REQ, 1);
4418 len = p2p_buf_add_ie_hdr(req);
4419 p2p_buf_add_noa(req, 0, 0, 0, ptr1, ptr2);
4420 p2p_buf_update_ie_hdr(req, len);
4421
4422 return req;
4423}
4424
4425
4426int p2p_presence_req(struct p2p_data *p2p, const u8 *go_interface_addr,
4427 const u8 *own_interface_addr, unsigned int freq,
4428 u32 duration1, u32 interval1, u32 duration2,
4429 u32 interval2)
4430{
4431 struct wpabuf *req;
4432
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004433 p2p_dbg(p2p, "Send Presence Request to GO " MACSTR
4434 " (own interface " MACSTR ") freq=%u dur1=%u int1=%u "
4435 "dur2=%u int2=%u",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004436 MAC2STR(go_interface_addr), MAC2STR(own_interface_addr),
4437 freq, duration1, interval1, duration2, interval2);
4438
4439 req = p2p_build_presence_req(duration1, interval1, duration2,
4440 interval2);
4441 if (req == NULL)
4442 return -1;
4443
4444 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
4445 if (p2p_send_action(p2p, freq, go_interface_addr, own_interface_addr,
4446 go_interface_addr,
4447 wpabuf_head(req), wpabuf_len(req), 200) < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004448 p2p_dbg(p2p, "Failed to send Action frame");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004449 }
4450 wpabuf_free(req);
4451
4452 return 0;
4453}
4454
4455
4456static struct wpabuf * p2p_build_presence_resp(u8 status, const u8 *noa,
4457 size_t noa_len, u8 dialog_token)
4458{
4459 struct wpabuf *resp;
4460 u8 *len;
4461
4462 resp = wpabuf_alloc(100 + noa_len);
4463 if (resp == NULL)
4464 return NULL;
4465
4466 p2p_buf_add_action_hdr(resp, P2P_PRESENCE_RESP, dialog_token);
4467 len = p2p_buf_add_ie_hdr(resp);
4468 p2p_buf_add_status(resp, status);
4469 if (noa) {
4470 wpabuf_put_u8(resp, P2P_ATTR_NOTICE_OF_ABSENCE);
4471 wpabuf_put_le16(resp, noa_len);
4472 wpabuf_put_data(resp, noa, noa_len);
4473 } else
4474 p2p_buf_add_noa(resp, 0, 0, 0, NULL, NULL);
4475 p2p_buf_update_ie_hdr(resp, len);
4476
4477 return resp;
4478}
4479
4480
4481static void p2p_process_presence_req(struct p2p_data *p2p, const u8 *da,
4482 const u8 *sa, const u8 *data, size_t len,
4483 int rx_freq)
4484{
4485 struct p2p_message msg;
4486 u8 status;
4487 struct wpabuf *resp;
4488 size_t g;
4489 struct p2p_group *group = NULL;
4490 int parsed = 0;
4491 u8 noa[50];
4492 int noa_len;
4493
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004494 p2p_dbg(p2p, "Received P2P Action - P2P Presence Request");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004495
4496 for (g = 0; g < p2p->num_groups; g++) {
4497 if (os_memcmp(da, p2p_group_get_interface_addr(p2p->groups[g]),
4498 ETH_ALEN) == 0) {
4499 group = p2p->groups[g];
4500 break;
4501 }
4502 }
4503 if (group == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004504 p2p_dbg(p2p, "Ignore P2P Presence Request for unknown group "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004505 MACSTR, MAC2STR(da));
4506 return;
4507 }
4508
4509 if (p2p_parse(data, len, &msg) < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004510 p2p_dbg(p2p, "Failed to parse P2P Presence Request");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004511 status = P2P_SC_FAIL_INVALID_PARAMS;
4512 goto fail;
4513 }
4514 parsed = 1;
4515
4516 if (msg.noa == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004517 p2p_dbg(p2p, "No NoA attribute in P2P Presence Request");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004518 status = P2P_SC_FAIL_INVALID_PARAMS;
4519 goto fail;
4520 }
4521
4522 status = p2p_group_presence_req(group, sa, msg.noa, msg.noa_len);
4523
4524fail:
4525 if (p2p->cfg->get_noa)
4526 noa_len = p2p->cfg->get_noa(p2p->cfg->cb_ctx, da, noa,
4527 sizeof(noa));
4528 else
4529 noa_len = -1;
4530 resp = p2p_build_presence_resp(status, noa_len > 0 ? noa : NULL,
4531 noa_len > 0 ? noa_len : 0,
4532 msg.dialog_token);
4533 if (parsed)
4534 p2p_parse_free(&msg);
4535 if (resp == NULL)
4536 return;
4537
4538 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
4539 if (p2p_send_action(p2p, rx_freq, sa, da, da,
4540 wpabuf_head(resp), wpabuf_len(resp), 200) < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004541 p2p_dbg(p2p, "Failed to send Action frame");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004542 }
4543 wpabuf_free(resp);
4544}
4545
4546
4547static void p2p_process_presence_resp(struct p2p_data *p2p, const u8 *da,
4548 const u8 *sa, const u8 *data, size_t len)
4549{
4550 struct p2p_message msg;
4551
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004552 p2p_dbg(p2p, "Received P2P Action - P2P Presence Response");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004553
4554 if (p2p_parse(data, len, &msg) < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004555 p2p_dbg(p2p, "Failed to parse P2P Presence Response");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004556 return;
4557 }
4558
4559 if (msg.status == NULL || msg.noa == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004560 p2p_dbg(p2p, "No Status or NoA attribute in P2P Presence Response");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004561 p2p_parse_free(&msg);
4562 return;
4563 }
4564
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004565 if (p2p->cfg->presence_resp) {
4566 p2p->cfg->presence_resp(p2p->cfg->cb_ctx, sa, *msg.status,
4567 msg.noa, msg.noa_len);
4568 }
4569
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004570 if (*msg.status) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004571 p2p_dbg(p2p, "P2P Presence Request was rejected: status %u",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004572 *msg.status);
4573 p2p_parse_free(&msg);
4574 return;
4575 }
4576
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004577 p2p_dbg(p2p, "P2P Presence Request was accepted");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004578 wpa_hexdump(MSG_DEBUG, "P2P: P2P Presence Response - NoA",
4579 msg.noa, msg.noa_len);
4580 /* TODO: process NoA */
4581 p2p_parse_free(&msg);
4582}
4583
4584
4585static void p2p_ext_listen_timeout(void *eloop_ctx, void *timeout_ctx)
4586{
4587 struct p2p_data *p2p = eloop_ctx;
4588
4589 if (p2p->ext_listen_interval) {
4590 /* Schedule next extended listen timeout */
4591 eloop_register_timeout(p2p->ext_listen_interval_sec,
4592 p2p->ext_listen_interval_usec,
4593 p2p_ext_listen_timeout, p2p, NULL);
4594 }
4595
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -07004596 if ((p2p->cfg->is_p2p_in_progress &&
4597 p2p->cfg->is_p2p_in_progress(p2p->cfg->cb_ctx)) ||
4598 (p2p->pending_action_state == P2P_PENDING_PD &&
4599 p2p->pd_retries > 0)) {
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -07004600 p2p_dbg(p2p, "Operation in progress - skip Extended Listen timeout (%s)",
4601 p2p_state_txt(p2p->state));
4602 return;
4603 }
4604
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004605 if (p2p->state == P2P_LISTEN_ONLY && p2p->ext_listen_only) {
4606 /*
4607 * This should not really happen, but it looks like the Listen
4608 * command may fail is something else (e.g., a scan) was
4609 * running at an inconvenient time. As a workaround, allow new
4610 * Extended Listen operation to be started.
4611 */
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004612 p2p_dbg(p2p, "Previous Extended Listen operation had not been completed - try again");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004613 p2p->ext_listen_only = 0;
4614 p2p_set_state(p2p, P2P_IDLE);
4615 }
4616
4617 if (p2p->state != P2P_IDLE) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004618 p2p_dbg(p2p, "Skip Extended Listen timeout in active state (%s)", p2p_state_txt(p2p->state));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004619 return;
4620 }
4621
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004622 p2p_dbg(p2p, "Extended Listen timeout");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004623 p2p->ext_listen_only = 1;
4624 if (p2p_listen(p2p, p2p->ext_listen_period) < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004625 p2p_dbg(p2p, "Failed to start Listen state for Extended Listen Timing");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004626 p2p->ext_listen_only = 0;
4627 }
4628}
4629
4630
4631int p2p_ext_listen(struct p2p_data *p2p, unsigned int period,
4632 unsigned int interval)
4633{
4634 if (period > 65535 || interval > 65535 || period > interval ||
4635 (period == 0 && interval > 0) || (period > 0 && interval == 0)) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004636 p2p_dbg(p2p, "Invalid Extended Listen Timing request: period=%u interval=%u",
4637 period, interval);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004638 return -1;
4639 }
4640
4641 eloop_cancel_timeout(p2p_ext_listen_timeout, p2p, NULL);
4642
4643 if (interval == 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004644 p2p_dbg(p2p, "Disabling Extended Listen Timing");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004645 p2p->ext_listen_period = 0;
4646 p2p->ext_listen_interval = 0;
4647 return 0;
4648 }
4649
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004650 p2p_dbg(p2p, "Enabling Extended Listen Timing: period %u msec, interval %u msec",
4651 period, interval);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004652 p2p->ext_listen_period = period;
4653 p2p->ext_listen_interval = interval;
4654 p2p->ext_listen_interval_sec = interval / 1000;
4655 p2p->ext_listen_interval_usec = (interval % 1000) * 1000;
4656
4657 eloop_register_timeout(p2p->ext_listen_interval_sec,
4658 p2p->ext_listen_interval_usec,
4659 p2p_ext_listen_timeout, p2p, NULL);
4660
4661 return 0;
4662}
4663
4664
4665void p2p_deauth_notif(struct p2p_data *p2p, const u8 *bssid, u16 reason_code,
4666 const u8 *ie, size_t ie_len)
4667{
4668 struct p2p_message msg;
4669
4670 if (bssid == NULL || ie == NULL)
4671 return;
4672
4673 os_memset(&msg, 0, sizeof(msg));
4674 if (p2p_parse_ies(ie, ie_len, &msg))
4675 return;
Dmitry Shmidt97672262014-02-03 13:02:54 -08004676 if (msg.minor_reason_code == NULL) {
4677 p2p_parse_free(&msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004678 return;
Dmitry Shmidt97672262014-02-03 13:02:54 -08004679 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004680
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004681 p2p_dbg(p2p, "Deauthentication notification BSSID " MACSTR
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004682 " reason_code=%u minor_reason_code=%u",
4683 MAC2STR(bssid), reason_code, *msg.minor_reason_code);
4684
4685 p2p_parse_free(&msg);
4686}
4687
4688
4689void p2p_disassoc_notif(struct p2p_data *p2p, const u8 *bssid, u16 reason_code,
4690 const u8 *ie, size_t ie_len)
4691{
4692 struct p2p_message msg;
4693
4694 if (bssid == NULL || ie == NULL)
4695 return;
4696
4697 os_memset(&msg, 0, sizeof(msg));
4698 if (p2p_parse_ies(ie, ie_len, &msg))
4699 return;
Dmitry Shmidt97672262014-02-03 13:02:54 -08004700 if (msg.minor_reason_code == NULL) {
4701 p2p_parse_free(&msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004702 return;
Dmitry Shmidt97672262014-02-03 13:02:54 -08004703 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004704
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004705 p2p_dbg(p2p, "Disassociation notification BSSID " MACSTR
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004706 " reason_code=%u minor_reason_code=%u",
4707 MAC2STR(bssid), reason_code, *msg.minor_reason_code);
4708
4709 p2p_parse_free(&msg);
4710}
4711
4712
4713void p2p_set_managed_oper(struct p2p_data *p2p, int enabled)
4714{
4715 if (enabled) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004716 p2p_dbg(p2p, "Managed P2P Device operations enabled");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004717 p2p->dev_capab |= P2P_DEV_CAPAB_INFRA_MANAGED;
4718 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004719 p2p_dbg(p2p, "Managed P2P Device operations disabled");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004720 p2p->dev_capab &= ~P2P_DEV_CAPAB_INFRA_MANAGED;
4721 }
4722}
4723
4724
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004725int p2p_config_get_random_social(struct p2p_config *p2p, u8 *op_class,
Hai Shalom74f70d42019-02-11 14:42:39 -08004726 u8 *op_channel,
4727 struct wpa_freq_range_list *avoid_list,
4728 struct wpa_freq_range_list *disallow_list)
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004729{
Hai Shalom74f70d42019-02-11 14:42:39 -08004730 return p2p_channel_random_social(&p2p->channels, op_class, op_channel,
4731 avoid_list, disallow_list);
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004732}
4733
4734
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004735int p2p_set_listen_channel(struct p2p_data *p2p, u8 reg_class, u8 channel,
4736 u8 forced)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004737{
Dmitry Shmidt4b060592013-04-29 16:42:49 -07004738 if (p2p_channel_to_freq(reg_class, channel) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004739 return -1;
4740
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004741 /*
4742 * Listen channel was set in configuration or set by control interface;
4743 * cannot override it.
4744 */
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004745 if (p2p->cfg->channel_forced && forced == 0) {
4746 p2p_dbg(p2p,
4747 "Listen channel was previously configured - do not override based on optimization");
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004748 return -1;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004749 }
4750
4751 p2p_dbg(p2p, "Set Listen channel: reg_class %u channel %u",
4752 reg_class, channel);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004753
4754 if (p2p->state == P2P_IDLE) {
4755 p2p->cfg->reg_class = reg_class;
4756 p2p->cfg->channel = channel;
4757 p2p->cfg->channel_forced = forced;
4758 } else {
4759 p2p_dbg(p2p, "Defer setting listen channel");
4760 p2p->pending_reg_class = reg_class;
4761 p2p->pending_channel = channel;
4762 p2p->pending_channel_forced = forced;
4763 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004764
4765 return 0;
4766}
4767
4768
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004769u8 p2p_get_listen_channel(struct p2p_data *p2p)
4770{
4771 return p2p->cfg->channel;
4772}
4773
4774
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004775int p2p_set_ssid_postfix(struct p2p_data *p2p, const u8 *postfix, size_t len)
4776{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004777 p2p_dbg(p2p, "New SSID postfix: %s", wpa_ssid_txt(postfix, len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004778 if (postfix == NULL) {
4779 p2p->cfg->ssid_postfix_len = 0;
4780 return 0;
4781 }
4782 if (len > sizeof(p2p->cfg->ssid_postfix))
4783 return -1;
4784 os_memcpy(p2p->cfg->ssid_postfix, postfix, len);
4785 p2p->cfg->ssid_postfix_len = len;
4786 return 0;
4787}
4788
4789
Jouni Malinen75ecf522011-06-27 15:19:46 -07004790int p2p_set_oper_channel(struct p2p_data *p2p, u8 op_reg_class, u8 op_channel,
4791 int cfg_op_channel)
4792{
Dmitry Shmidt4b060592013-04-29 16:42:49 -07004793 if (p2p_channel_to_freq(op_reg_class, op_channel) < 0)
Jouni Malinen75ecf522011-06-27 15:19:46 -07004794 return -1;
4795
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004796 p2p_dbg(p2p, "Set Operating channel: reg_class %u channel %u",
4797 op_reg_class, op_channel);
Jouni Malinen75ecf522011-06-27 15:19:46 -07004798 p2p->cfg->op_reg_class = op_reg_class;
4799 p2p->cfg->op_channel = op_channel;
4800 p2p->cfg->cfg_op_channel = cfg_op_channel;
4801 return 0;
4802}
4803
4804
Dmitry Shmidt04949592012-07-19 12:16:46 -07004805int p2p_set_pref_chan(struct p2p_data *p2p, unsigned int num_pref_chan,
4806 const struct p2p_channel *pref_chan)
4807{
4808 struct p2p_channel *n;
4809
4810 if (pref_chan) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004811 n = os_memdup(pref_chan,
4812 num_pref_chan * sizeof(struct p2p_channel));
Dmitry Shmidt04949592012-07-19 12:16:46 -07004813 if (n == NULL)
4814 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004815 } else
4816 n = NULL;
4817
4818 os_free(p2p->cfg->pref_chan);
4819 p2p->cfg->pref_chan = n;
4820 p2p->cfg->num_pref_chan = num_pref_chan;
4821
4822 return 0;
4823}
4824
4825
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004826int p2p_set_no_go_freq(struct p2p_data *p2p,
4827 const struct wpa_freq_range_list *list)
4828{
4829 struct wpa_freq_range *tmp;
4830
4831 if (list == NULL || list->num == 0) {
4832 os_free(p2p->no_go_freq.range);
4833 p2p->no_go_freq.range = NULL;
4834 p2p->no_go_freq.num = 0;
4835 return 0;
4836 }
4837
4838 tmp = os_calloc(list->num, sizeof(struct wpa_freq_range));
4839 if (tmp == NULL)
4840 return -1;
4841 os_memcpy(tmp, list->range, list->num * sizeof(struct wpa_freq_range));
4842 os_free(p2p->no_go_freq.range);
4843 p2p->no_go_freq.range = tmp;
4844 p2p->no_go_freq.num = list->num;
4845 p2p_dbg(p2p, "Updated no GO chan list");
4846
4847 return 0;
4848}
4849
4850
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004851int p2p_get_interface_addr(struct p2p_data *p2p, const u8 *dev_addr,
4852 u8 *iface_addr)
4853{
4854 struct p2p_device *dev = p2p_get_device(p2p, dev_addr);
4855 if (dev == NULL || is_zero_ether_addr(dev->interface_addr))
4856 return -1;
4857 os_memcpy(iface_addr, dev->interface_addr, ETH_ALEN);
4858 return 0;
4859}
4860
4861
4862int p2p_get_dev_addr(struct p2p_data *p2p, const u8 *iface_addr,
4863 u8 *dev_addr)
4864{
4865 struct p2p_device *dev = p2p_get_device_interface(p2p, iface_addr);
4866 if (dev == NULL)
4867 return -1;
4868 os_memcpy(dev_addr, dev->info.p2p_device_addr, ETH_ALEN);
4869 return 0;
4870}
4871
4872
4873void p2p_set_peer_filter(struct p2p_data *p2p, const u8 *addr)
4874{
4875 os_memcpy(p2p->peer_filter, addr, ETH_ALEN);
4876 if (is_zero_ether_addr(p2p->peer_filter))
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004877 p2p_dbg(p2p, "Disable peer filter");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004878 else
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004879 p2p_dbg(p2p, "Enable peer filter for " MACSTR,
4880 MAC2STR(p2p->peer_filter));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004881}
4882
4883
4884void p2p_set_cross_connect(struct p2p_data *p2p, int enabled)
4885{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004886 p2p_dbg(p2p, "Cross connection %s", enabled ? "enabled" : "disabled");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004887 if (p2p->cross_connect == enabled)
4888 return;
4889 p2p->cross_connect = enabled;
4890 /* TODO: may need to tear down any action group where we are GO(?) */
4891}
4892
4893
4894int p2p_get_oper_freq(struct p2p_data *p2p, const u8 *iface_addr)
4895{
4896 struct p2p_device *dev = p2p_get_device_interface(p2p, iface_addr);
4897 if (dev == NULL)
4898 return -1;
4899 if (dev->oper_freq <= 0)
4900 return -1;
4901 return dev->oper_freq;
4902}
4903
4904
4905void p2p_set_intra_bss_dist(struct p2p_data *p2p, int enabled)
4906{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004907 p2p_dbg(p2p, "Intra BSS distribution %s",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004908 enabled ? "enabled" : "disabled");
4909 p2p->cfg->p2p_intra_bss = enabled;
4910}
4911
4912
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004913void p2p_update_channel_list(struct p2p_data *p2p,
4914 const struct p2p_channels *chan,
4915 const struct p2p_channels *cli_chan)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004916{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004917 p2p_dbg(p2p, "Update channel list");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004918 os_memcpy(&p2p->cfg->channels, chan, sizeof(struct p2p_channels));
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004919 p2p_channels_dump(p2p, "channels", &p2p->cfg->channels);
4920 os_memcpy(&p2p->cfg->cli_channels, cli_chan,
4921 sizeof(struct p2p_channels));
4922 p2p_channels_dump(p2p, "cli_channels", &p2p->cfg->cli_channels);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004923}
4924
4925
4926int p2p_send_action(struct p2p_data *p2p, unsigned int freq, const u8 *dst,
4927 const u8 *src, const u8 *bssid, const u8 *buf,
4928 size_t len, unsigned int wait_time)
4929{
Hai Shalom021b0b52019-04-10 11:17:58 -07004930 int res, scheduled;
4931
Hai Shalom021b0b52019-04-10 11:17:58 -07004932 res = p2p->cfg->send_action(p2p->cfg->cb_ctx, freq, dst, src, bssid,
4933 buf, len, wait_time, &scheduled);
4934 if (res == 0 && scheduled && p2p->in_listen && freq > 0 &&
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08004935 p2p->drv_in_listen > 0 &&
Hai Shalom021b0b52019-04-10 11:17:58 -07004936 (unsigned int) p2p->drv_in_listen != freq) {
4937 p2p_dbg(p2p,
4938 "Stop listen on %d MHz to allow a frame to be sent immediately on %d MHz",
4939 p2p->drv_in_listen, freq);
4940 p2p_stop_listen_for_freq(p2p, freq);
4941 }
4942 return res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004943}
4944
4945
4946void p2p_set_best_channels(struct p2p_data *p2p, int freq_24, int freq_5,
4947 int freq_overall)
4948{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004949 p2p_dbg(p2p, "Best channel: 2.4 GHz: %d, 5 GHz: %d, overall: %d",
4950 freq_24, freq_5, freq_overall);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004951 p2p->best_freq_24 = freq_24;
4952 p2p->best_freq_5 = freq_5;
4953 p2p->best_freq_overall = freq_overall;
4954}
4955
4956
Dmitry Shmidt700a1372013-03-15 14:14:44 -07004957void p2p_set_own_freq_preference(struct p2p_data *p2p, int freq)
4958{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004959 p2p_dbg(p2p, "Own frequency preference: %d MHz", freq);
Dmitry Shmidt700a1372013-03-15 14:14:44 -07004960 p2p->own_freq_preference = freq;
4961}
4962
4963
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004964const u8 * p2p_get_go_neg_peer(struct p2p_data *p2p)
4965{
4966 if (p2p == NULL || p2p->go_neg_peer == NULL)
4967 return NULL;
4968 return p2p->go_neg_peer->info.p2p_device_addr;
4969}
4970
4971
4972const struct p2p_peer_info *
4973p2p_get_peer_found(struct p2p_data *p2p, const u8 *addr, int next)
4974{
4975 struct p2p_device *dev;
4976
4977 if (addr) {
4978 dev = p2p_get_device(p2p, addr);
4979 if (!dev)
4980 return NULL;
4981
4982 if (!next) {
4983 if (dev->flags & P2P_DEV_PROBE_REQ_ONLY)
4984 return NULL;
4985
4986 return &dev->info;
4987 } else {
4988 do {
4989 dev = dl_list_first(&dev->list,
4990 struct p2p_device,
4991 list);
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07004992 if (!dev || &dev->list == &p2p->devices)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004993 return NULL;
4994 } while (dev->flags & P2P_DEV_PROBE_REQ_ONLY);
4995 }
4996 } else {
4997 dev = dl_list_first(&p2p->devices, struct p2p_device, list);
4998 if (!dev)
4999 return NULL;
5000 while (dev->flags & P2P_DEV_PROBE_REQ_ONLY) {
5001 dev = dl_list_first(&dev->list,
5002 struct p2p_device,
5003 list);
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07005004 if (!dev || &dev->list == &p2p->devices)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005005 return NULL;
5006 }
5007 }
5008
5009 return &dev->info;
5010}
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005011
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005012
5013int p2p_in_progress(struct p2p_data *p2p)
5014{
5015 if (p2p == NULL)
5016 return 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005017 if (p2p->state == P2P_SEARCH)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005018 return 2;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005019 return p2p->state != P2P_IDLE && p2p->state != P2P_PROVISIONING;
5020}
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005021
5022
5023void p2p_set_config_timeout(struct p2p_data *p2p, u8 go_timeout,
5024 u8 client_timeout)
5025{
5026 if (p2p) {
5027 p2p->go_timeout = go_timeout;
5028 p2p->client_timeout = client_timeout;
5029 }
5030}
5031
5032
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005033#ifdef CONFIG_WIFI_DISPLAY
5034
5035static void p2p_update_wfd_ie_groups(struct p2p_data *p2p)
5036{
5037 size_t g;
5038 struct p2p_group *group;
5039
5040 for (g = 0; g < p2p->num_groups; g++) {
5041 group = p2p->groups[g];
Dmitry Shmidtb96dad42013-11-05 10:07:29 -08005042 p2p_group_force_beacon_update_ies(group);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005043 }
5044}
5045
5046
5047int p2p_set_wfd_ie_beacon(struct p2p_data *p2p, struct wpabuf *ie)
5048{
5049 wpabuf_free(p2p->wfd_ie_beacon);
5050 p2p->wfd_ie_beacon = ie;
5051 p2p_update_wfd_ie_groups(p2p);
5052 return 0;
5053}
5054
5055
5056int p2p_set_wfd_ie_probe_req(struct p2p_data *p2p, struct wpabuf *ie)
5057{
5058 wpabuf_free(p2p->wfd_ie_probe_req);
5059 p2p->wfd_ie_probe_req = ie;
5060 return 0;
5061}
5062
5063
5064int p2p_set_wfd_ie_probe_resp(struct p2p_data *p2p, struct wpabuf *ie)
5065{
5066 wpabuf_free(p2p->wfd_ie_probe_resp);
5067 p2p->wfd_ie_probe_resp = ie;
5068 p2p_update_wfd_ie_groups(p2p);
5069 return 0;
5070}
5071
5072
5073int p2p_set_wfd_ie_assoc_req(struct p2p_data *p2p, struct wpabuf *ie)
5074{
5075 wpabuf_free(p2p->wfd_ie_assoc_req);
5076 p2p->wfd_ie_assoc_req = ie;
5077 return 0;
5078}
5079
5080
5081int p2p_set_wfd_ie_invitation(struct p2p_data *p2p, struct wpabuf *ie)
5082{
5083 wpabuf_free(p2p->wfd_ie_invitation);
5084 p2p->wfd_ie_invitation = ie;
5085 return 0;
5086}
5087
5088
5089int p2p_set_wfd_ie_prov_disc_req(struct p2p_data *p2p, struct wpabuf *ie)
5090{
5091 wpabuf_free(p2p->wfd_ie_prov_disc_req);
5092 p2p->wfd_ie_prov_disc_req = ie;
5093 return 0;
5094}
5095
5096
5097int p2p_set_wfd_ie_prov_disc_resp(struct p2p_data *p2p, struct wpabuf *ie)
5098{
5099 wpabuf_free(p2p->wfd_ie_prov_disc_resp);
5100 p2p->wfd_ie_prov_disc_resp = ie;
5101 return 0;
5102}
5103
5104
5105int p2p_set_wfd_ie_go_neg(struct p2p_data *p2p, struct wpabuf *ie)
5106{
5107 wpabuf_free(p2p->wfd_ie_go_neg);
5108 p2p->wfd_ie_go_neg = ie;
5109 return 0;
5110}
5111
5112
5113int p2p_set_wfd_dev_info(struct p2p_data *p2p, const struct wpabuf *elem)
5114{
5115 wpabuf_free(p2p->wfd_dev_info);
5116 if (elem) {
5117 p2p->wfd_dev_info = wpabuf_dup(elem);
5118 if (p2p->wfd_dev_info == NULL)
5119 return -1;
5120 } else
5121 p2p->wfd_dev_info = NULL;
5122
5123 return 0;
5124}
5125
5126
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005127int p2p_set_wfd_r2_dev_info(struct p2p_data *p2p, const struct wpabuf *elem)
5128{
5129 wpabuf_free(p2p->wfd_r2_dev_info);
5130 if (elem) {
5131 p2p->wfd_r2_dev_info = wpabuf_dup(elem);
5132 if (p2p->wfd_r2_dev_info == NULL)
5133 return -1;
5134 } else
5135 p2p->wfd_r2_dev_info = NULL;
5136
5137 return 0;
5138}
5139
5140
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005141int p2p_set_wfd_assoc_bssid(struct p2p_data *p2p, const struct wpabuf *elem)
5142{
5143 wpabuf_free(p2p->wfd_assoc_bssid);
5144 if (elem) {
5145 p2p->wfd_assoc_bssid = wpabuf_dup(elem);
5146 if (p2p->wfd_assoc_bssid == NULL)
5147 return -1;
5148 } else
5149 p2p->wfd_assoc_bssid = NULL;
5150
5151 return 0;
5152}
5153
5154
5155int p2p_set_wfd_coupled_sink_info(struct p2p_data *p2p,
5156 const struct wpabuf *elem)
5157{
5158 wpabuf_free(p2p->wfd_coupled_sink_info);
5159 if (elem) {
5160 p2p->wfd_coupled_sink_info = wpabuf_dup(elem);
5161 if (p2p->wfd_coupled_sink_info == NULL)
5162 return -1;
5163 } else
5164 p2p->wfd_coupled_sink_info = NULL;
5165
5166 return 0;
5167}
5168
5169#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005170
5171
5172int p2p_set_disc_int(struct p2p_data *p2p, int min_disc_int, int max_disc_int,
5173 int max_disc_tu)
5174{
5175 if (min_disc_int > max_disc_int || min_disc_int < 0 || max_disc_int < 0)
5176 return -1;
5177
5178 p2p->min_disc_int = min_disc_int;
5179 p2p->max_disc_int = max_disc_int;
5180 p2p->max_disc_tu = max_disc_tu;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005181 p2p_dbg(p2p, "Set discoverable interval: min=%d max=%d max_tu=%d",
5182 min_disc_int, max_disc_int, max_disc_tu);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005183
5184 return 0;
5185}
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005186
5187
5188void p2p_dbg(struct p2p_data *p2p, const char *fmt, ...)
5189{
5190 va_list ap;
5191 char buf[500];
5192
5193 if (!p2p->cfg->debug_print)
5194 return;
5195
5196 va_start(ap, fmt);
5197 vsnprintf(buf, sizeof(buf), fmt, ap);
5198 buf[sizeof(buf) - 1] = '\0';
5199 va_end(ap);
5200 p2p->cfg->debug_print(p2p->cfg->cb_ctx, MSG_DEBUG, buf);
5201}
5202
5203
5204void p2p_info(struct p2p_data *p2p, const char *fmt, ...)
5205{
5206 va_list ap;
5207 char buf[500];
5208
5209 if (!p2p->cfg->debug_print)
5210 return;
5211
5212 va_start(ap, fmt);
5213 vsnprintf(buf, sizeof(buf), fmt, ap);
5214 buf[sizeof(buf) - 1] = '\0';
5215 va_end(ap);
5216 p2p->cfg->debug_print(p2p->cfg->cb_ctx, MSG_INFO, buf);
5217}
5218
5219
5220void p2p_err(struct p2p_data *p2p, const char *fmt, ...)
5221{
5222 va_list ap;
5223 char buf[500];
5224
5225 if (!p2p->cfg->debug_print)
5226 return;
5227
5228 va_start(ap, fmt);
5229 vsnprintf(buf, sizeof(buf), fmt, ap);
5230 buf[sizeof(buf) - 1] = '\0';
5231 va_end(ap);
5232 p2p->cfg->debug_print(p2p->cfg->cb_ctx, MSG_ERROR, buf);
5233}
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005234
5235
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07005236void p2p_loop_on_known_peers(struct p2p_data *p2p,
5237 void (*peer_callback)(struct p2p_peer_info *peer,
5238 void *user_data),
5239 void *user_data)
5240{
5241 struct p2p_device *dev, *n;
5242
5243 dl_list_for_each_safe(dev, n, &p2p->devices, struct p2p_device, list) {
5244 peer_callback(&dev->info, user_data);
5245 }
5246}
5247
5248
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005249#ifdef CONFIG_WPS_NFC
5250
5251static struct wpabuf * p2p_build_nfc_handover(struct p2p_data *p2p,
5252 int client_freq,
5253 const u8 *go_dev_addr,
5254 const u8 *ssid, size_t ssid_len)
5255{
5256 struct wpabuf *buf;
5257 u8 op_class, channel;
5258 enum p2p_role_indication role = P2P_DEVICE_NOT_IN_GROUP;
5259
5260 buf = wpabuf_alloc(1000);
5261 if (buf == NULL)
5262 return NULL;
5263
5264 op_class = p2p->cfg->reg_class;
5265 channel = p2p->cfg->channel;
5266
5267 p2p_buf_add_capability(buf, p2p->dev_capab &
5268 ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY, 0);
5269 p2p_buf_add_device_info(buf, p2p, NULL);
5270
5271 if (p2p->num_groups > 0) {
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07005272 int freq = p2p_group_get_freq(p2p->groups[0]);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005273 role = P2P_GO_IN_A_GROUP;
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07005274 if (p2p_freq_to_channel(freq, &op_class, &channel) < 0) {
5275 p2p_dbg(p2p,
5276 "Unknown GO operating frequency %d MHz for NFC handover",
5277 freq);
5278 wpabuf_free(buf);
5279 return NULL;
5280 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005281 } else if (client_freq > 0) {
5282 role = P2P_CLIENT_IN_A_GROUP;
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07005283 if (p2p_freq_to_channel(client_freq, &op_class, &channel) < 0) {
5284 p2p_dbg(p2p,
5285 "Unknown client operating frequency %d MHz for NFC handover",
5286 client_freq);
5287 wpabuf_free(buf);
5288 return NULL;
5289 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005290 }
5291
5292 p2p_buf_add_oob_go_neg_channel(buf, p2p->cfg->country, op_class,
5293 channel, role);
5294
5295 if (p2p->num_groups > 0) {
5296 /* Limit number of clients to avoid very long message */
5297 p2p_buf_add_group_info(p2p->groups[0], buf, 5);
5298 p2p_group_buf_add_id(p2p->groups[0], buf);
5299 } else if (client_freq > 0 &&
5300 go_dev_addr && !is_zero_ether_addr(go_dev_addr) &&
5301 ssid && ssid_len > 0) {
5302 /*
5303 * Add the optional P2P Group ID to indicate in which group this
5304 * device is a P2P Client.
5305 */
5306 p2p_buf_add_group_id(buf, go_dev_addr, ssid, ssid_len);
5307 }
5308
5309 return buf;
5310}
5311
5312
5313struct wpabuf * p2p_build_nfc_handover_req(struct p2p_data *p2p,
5314 int client_freq,
5315 const u8 *go_dev_addr,
5316 const u8 *ssid, size_t ssid_len)
5317{
5318 return p2p_build_nfc_handover(p2p, client_freq, go_dev_addr, ssid,
5319 ssid_len);
5320}
5321
5322
5323struct wpabuf * p2p_build_nfc_handover_sel(struct p2p_data *p2p,
5324 int client_freq,
5325 const u8 *go_dev_addr,
5326 const u8 *ssid, size_t ssid_len)
5327{
5328 return p2p_build_nfc_handover(p2p, client_freq, go_dev_addr, ssid,
5329 ssid_len);
5330}
5331
5332
5333int p2p_process_nfc_connection_handover(struct p2p_data *p2p,
5334 struct p2p_nfc_params *params)
5335{
5336 struct p2p_message msg;
5337 struct p2p_device *dev;
5338 const u8 *p2p_dev_addr;
5339 int freq;
5340 enum p2p_role_indication role;
5341
5342 params->next_step = NO_ACTION;
5343
5344 if (p2p_parse_ies_separate(params->wsc_attr, params->wsc_len,
5345 params->p2p_attr, params->p2p_len, &msg)) {
5346 p2p_dbg(p2p, "Failed to parse WSC/P2P attributes from NFC");
5347 p2p_parse_free(&msg);
5348 return -1;
5349 }
5350
5351 if (msg.p2p_device_addr)
5352 p2p_dev_addr = msg.p2p_device_addr;
5353 else if (msg.device_id)
5354 p2p_dev_addr = msg.device_id;
5355 else {
5356 p2p_dbg(p2p, "Ignore scan data without P2P Device Info or P2P Device Id");
5357 p2p_parse_free(&msg);
5358 return -1;
5359 }
5360
5361 if (msg.oob_dev_password) {
5362 os_memcpy(params->oob_dev_pw, msg.oob_dev_password,
5363 msg.oob_dev_password_len);
5364 params->oob_dev_pw_len = msg.oob_dev_password_len;
5365 }
5366
5367 dev = p2p_create_device(p2p, p2p_dev_addr);
5368 if (dev == NULL) {
5369 p2p_parse_free(&msg);
5370 return -1;
5371 }
5372
5373 params->peer = &dev->info;
5374
5375 os_get_reltime(&dev->last_seen);
5376 dev->flags &= ~(P2P_DEV_PROBE_REQ_ONLY | P2P_DEV_GROUP_CLIENT_ONLY);
5377 p2p_copy_wps_info(p2p, dev, 0, &msg);
5378
5379 if (!msg.oob_go_neg_channel) {
5380 p2p_dbg(p2p, "OOB GO Negotiation Channel attribute not included");
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07005381 p2p_parse_free(&msg);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005382 return -1;
5383 }
5384
5385 if (msg.oob_go_neg_channel[3] == 0 &&
5386 msg.oob_go_neg_channel[4] == 0)
5387 freq = 0;
5388 else
5389 freq = p2p_channel_to_freq(msg.oob_go_neg_channel[3],
5390 msg.oob_go_neg_channel[4]);
5391 if (freq < 0) {
5392 p2p_dbg(p2p, "Unknown peer OOB GO Neg channel");
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07005393 p2p_parse_free(&msg);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005394 return -1;
5395 }
5396 role = msg.oob_go_neg_channel[5];
5397
5398 if (role == P2P_GO_IN_A_GROUP) {
5399 p2p_dbg(p2p, "Peer OOB GO operating channel: %u MHz", freq);
5400 params->go_freq = freq;
5401 } else if (role == P2P_CLIENT_IN_A_GROUP) {
5402 p2p_dbg(p2p, "Peer (client) OOB GO operating channel: %u MHz",
5403 freq);
5404 params->go_freq = freq;
5405 } else
5406 p2p_dbg(p2p, "Peer OOB GO Neg channel: %u MHz", freq);
5407 dev->oob_go_neg_freq = freq;
5408
5409 if (!params->sel && role != P2P_GO_IN_A_GROUP) {
5410 freq = p2p_channel_to_freq(p2p->cfg->reg_class,
5411 p2p->cfg->channel);
5412 if (freq < 0) {
5413 p2p_dbg(p2p, "Own listen channel not known");
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07005414 p2p_parse_free(&msg);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005415 return -1;
5416 }
5417 p2p_dbg(p2p, "Use own Listen channel as OOB GO Neg channel: %u MHz", freq);
5418 dev->oob_go_neg_freq = freq;
5419 }
5420
5421 if (msg.group_id) {
5422 os_memcpy(params->go_dev_addr, msg.group_id, ETH_ALEN);
5423 params->go_ssid_len = msg.group_id_len - ETH_ALEN;
5424 os_memcpy(params->go_ssid, msg.group_id + ETH_ALEN,
5425 params->go_ssid_len);
5426 }
5427
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005428 if (dev->flags & P2P_DEV_USER_REJECTED) {
5429 p2p_dbg(p2p, "Do not report rejected device");
Dmitry Shmidt71757432014-06-02 13:50:35 -07005430 p2p_parse_free(&msg);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005431 return 0;
5432 }
5433
5434 if (!(dev->flags & P2P_DEV_REPORTED)) {
5435 p2p->cfg->dev_found(p2p->cfg->cb_ctx, p2p_dev_addr, &dev->info,
5436 !(dev->flags & P2P_DEV_REPORTED_ONCE));
5437 dev->flags |= P2P_DEV_REPORTED | P2P_DEV_REPORTED_ONCE;
5438 }
Dmitry Shmidt71757432014-06-02 13:50:35 -07005439 p2p_parse_free(&msg);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005440
5441 if (role == P2P_GO_IN_A_GROUP && p2p->num_groups > 0)
5442 params->next_step = BOTH_GO;
5443 else if (role == P2P_GO_IN_A_GROUP)
5444 params->next_step = JOIN_GROUP;
5445 else if (role == P2P_CLIENT_IN_A_GROUP) {
5446 dev->flags |= P2P_DEV_GROUP_CLIENT_ONLY;
5447 params->next_step = PEER_CLIENT;
5448 } else if (p2p->num_groups > 0)
5449 params->next_step = AUTH_JOIN;
5450 else if (params->sel)
5451 params->next_step = INIT_GO_NEG;
5452 else
5453 params->next_step = RESP_GO_NEG;
5454
5455 return 0;
5456}
5457
5458
5459void p2p_set_authorized_oob_dev_pw_id(struct p2p_data *p2p, u16 dev_pw_id,
5460 int go_intent,
5461 const u8 *own_interface_addr)
5462{
5463
5464 p2p->authorized_oob_dev_pw_id = dev_pw_id;
5465 if (dev_pw_id == 0) {
5466 p2p_dbg(p2p, "NFC OOB Password unauthorized for static handover");
5467 return;
5468 }
5469
5470 p2p_dbg(p2p, "NFC OOB Password (id=%u) authorized for static handover",
5471 dev_pw_id);
5472
5473 p2p->go_intent = go_intent;
5474 os_memcpy(p2p->intended_addr, own_interface_addr, ETH_ALEN);
5475}
5476
5477#endif /* CONFIG_WPS_NFC */
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07005478
5479
5480int p2p_set_passphrase_len(struct p2p_data *p2p, unsigned int len)
5481{
5482 if (len < 8 || len > 63)
5483 return -1;
5484 p2p->cfg->passphrase_len = len;
5485 return 0;
5486}
Dmitry Shmidt2e67f062014-07-16 09:55:28 -07005487
5488
5489void p2p_set_vendor_elems(struct p2p_data *p2p, struct wpabuf **vendor_elem)
5490{
5491 p2p->vendor_elem = vendor_elem;
5492}
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005493
5494
5495void p2p_go_neg_wait_timeout(void *eloop_ctx, void *timeout_ctx)
5496{
5497 struct p2p_data *p2p = eloop_ctx;
5498
5499 p2p_dbg(p2p,
5500 "Timeout on waiting peer to become ready for GO Negotiation");
5501 p2p_go_neg_failed(p2p, -1);
5502}
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005503
5504
5505void p2p_set_own_pref_freq_list(struct p2p_data *p2p,
5506 const unsigned int *pref_freq_list,
5507 unsigned int size)
5508{
5509 unsigned int i;
5510
5511 if (size > P2P_MAX_PREF_CHANNELS)
5512 size = P2P_MAX_PREF_CHANNELS;
5513 p2p->num_pref_freq = size;
5514 for (i = 0; i < size; i++) {
5515 p2p->pref_freq_list[i] = pref_freq_list[i];
5516 p2p_dbg(p2p, "Own preferred frequency list[%u]=%u MHz",
5517 i, p2p->pref_freq_list[i]);
5518 }
5519}
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07005520
5521
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08005522void p2p_set_override_pref_op_chan(struct p2p_data *p2p, u8 op_class,
5523 u8 chan)
5524{
5525 p2p->override_pref_op_class = op_class;
5526 p2p->override_pref_channel = chan;
5527}
5528
5529
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07005530struct wpabuf * p2p_build_probe_resp_template(struct p2p_data *p2p,
5531 unsigned int freq)
5532{
5533 struct wpabuf *ies, *buf;
5534 u8 addr[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
5535 int ret;
5536
5537 ies = p2p_build_probe_resp_ies(p2p, NULL, 0);
5538 if (!ies) {
5539 wpa_printf(MSG_ERROR,
5540 "CTRL: Failed to build Probe Response IEs");
5541 return NULL;
5542 }
5543
5544 buf = wpabuf_alloc(200 + wpabuf_len(ies));
5545 if (!buf) {
5546 wpabuf_free(ies);
5547 return NULL;
5548 }
5549
5550 ret = p2p_build_probe_resp_buf(p2p, buf, ies, addr, freq);
5551 wpabuf_free(ies);
5552 if (ret) {
5553 wpabuf_free(buf);
5554 return NULL;
5555 }
5556
5557 return buf;
5558}