blob: 03963123a7fd08b0b3f339dbb625b27dabe797a5 [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 };
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001407
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07001408 p2p_dbg(p2p, "Prepare channel best");
1409
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001410 if (!p2p->cfg->cfg_op_channel && p2p->best_freq_overall > 0 &&
1411 p2p_supported_freq(p2p, p2p->best_freq_overall) &&
Dmitry Shmidt4b060592013-04-29 16:42:49 -07001412 p2p_freq_to_channel(p2p->best_freq_overall, &op_class, &op_channel)
1413 == 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001414 p2p_dbg(p2p, "Select best overall channel as operating channel preference");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001415 p2p->op_reg_class = op_class;
1416 p2p->op_channel = op_channel;
1417 } else if (!p2p->cfg->cfg_op_channel && p2p->best_freq_5 > 0 &&
1418 p2p_supported_freq(p2p, p2p->best_freq_5) &&
Dmitry Shmidt4b060592013-04-29 16:42:49 -07001419 p2p_freq_to_channel(p2p->best_freq_5, &op_class, &op_channel)
1420 == 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001421 p2p_dbg(p2p, "Select best 5 GHz channel as operating channel preference");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001422 p2p->op_reg_class = op_class;
1423 p2p->op_channel = op_channel;
1424 } else if (!p2p->cfg->cfg_op_channel && p2p->best_freq_24 > 0 &&
1425 p2p_supported_freq(p2p, p2p->best_freq_24) &&
Dmitry Shmidt4b060592013-04-29 16:42:49 -07001426 p2p_freq_to_channel(p2p->best_freq_24, &op_class,
1427 &op_channel) == 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001428 p2p_dbg(p2p, "Select best 2.4 GHz channel as operating channel preference");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001429 p2p->op_reg_class = op_class;
1430 p2p->op_channel = op_channel;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07001431 } else if (p2p->cfg->num_pref_chan > 0 &&
1432 p2p_channels_includes(&p2p->cfg->channels,
1433 p2p->cfg->pref_chan[0].op_class,
1434 p2p->cfg->pref_chan[0].chan)) {
1435 p2p_dbg(p2p, "Select first pref_chan entry as operating channel preference");
1436 p2p->op_reg_class = p2p->cfg->pref_chan[0].op_class;
1437 p2p->op_channel = p2p->cfg->pref_chan[0].chan;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001438 } else if (p2p_channel_select(&p2p->cfg->channels, op_classes_vht,
1439 &p2p->op_reg_class, &p2p->op_channel) ==
1440 0) {
1441 p2p_dbg(p2p, "Select possible VHT channel (op_class %u channel %u) as operating channel preference",
1442 p2p->op_reg_class, p2p->op_channel);
1443 } else if (p2p_channel_select(&p2p->cfg->channels, op_classes_ht40,
1444 &p2p->op_reg_class, &p2p->op_channel) ==
1445 0) {
1446 p2p_dbg(p2p, "Select possible HT40 channel (op_class %u channel %u) as operating channel preference",
1447 p2p->op_reg_class, p2p->op_channel);
1448 } else if (p2p_channel_select(&p2p->cfg->channels, op_classes_5ghz,
1449 &p2p->op_reg_class, &p2p->op_channel) ==
1450 0) {
1451 p2p_dbg(p2p, "Select possible 5 GHz channel (op_class %u channel %u) as operating channel preference",
1452 p2p->op_reg_class, p2p->op_channel);
Dmitry Shmidtd11f0192014-03-24 12:09:47 -07001453 } else if (p2p_channels_includes(&p2p->cfg->channels,
1454 p2p->cfg->op_reg_class,
1455 p2p->cfg->op_channel)) {
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07001456 p2p_dbg(p2p, "Select pre-configured channel as operating channel preference");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001457 p2p->op_reg_class = p2p->cfg->op_reg_class;
1458 p2p->op_channel = p2p->cfg->op_channel;
Dmitry Shmidtd11f0192014-03-24 12:09:47 -07001459 } else if (p2p_channel_random_social(&p2p->cfg->channels,
1460 &p2p->op_reg_class,
Hai Shalom74f70d42019-02-11 14:42:39 -08001461 &p2p->op_channel,
1462 NULL, NULL) == 0) {
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001463 p2p_dbg(p2p, "Select random available social channel (op_class %u channel %u) as operating channel preference",
1464 p2p->op_reg_class, p2p->op_channel);
Dmitry Shmidtd11f0192014-03-24 12:09:47 -07001465 } else {
1466 /* Select any random available channel from the first available
1467 * operating class */
1468 p2p_channel_select(&p2p->cfg->channels, NULL,
1469 &p2p->op_reg_class,
1470 &p2p->op_channel);
1471 p2p_dbg(p2p, "Select random available channel %d from operating class %d as operating channel preference",
1472 p2p->op_channel, p2p->op_reg_class);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001473 }
1474
1475 os_memcpy(&p2p->channels, &p2p->cfg->channels,
1476 sizeof(struct p2p_channels));
1477}
1478
1479
1480/**
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001481 * p2p_prepare_channel - Select operating channel for GO Negotiation or P2PS PD
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001482 * @p2p: P2P module context from p2p_init()
1483 * @dev: Selected peer device
1484 * @force_freq: Forced frequency in MHz or 0 if not forced
1485 * @pref_freq: Preferred frequency in MHz or 0 if no preference
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001486 * @go: Whether the local end will be forced to be GO
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001487 * Returns: 0 on success, -1 on failure (channel not supported for P2P)
1488 *
1489 * This function is used to do initial operating channel selection for GO
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001490 * Negotiation prior to having received peer information or for P2PS PD
1491 * signalling. The selected channel may be further optimized in
1492 * p2p_reselect_channel() once the peer information is available.
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001493 */
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08001494int p2p_prepare_channel(struct p2p_data *p2p, struct p2p_device *dev,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001495 unsigned int force_freq, unsigned int pref_freq, int go)
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001496{
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001497 p2p_dbg(p2p, "Prepare channel - force_freq=%u pref_freq=%u go=%d",
1498 force_freq, pref_freq, go);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001499 if (force_freq || pref_freq) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001500 if (p2p_prepare_channel_pref(p2p, force_freq, pref_freq, go) <
1501 0)
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001502 return -1;
1503 } else {
1504 p2p_prepare_channel_best(p2p);
1505 }
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001506 p2p_channels_dump(p2p, "prepared channels", &p2p->channels);
1507 if (go)
1508 p2p_channels_remove_freqs(&p2p->channels, &p2p->no_go_freq);
1509 else if (!force_freq)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001510 p2p_channels_union_inplace(&p2p->channels,
1511 &p2p->cfg->cli_channels);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001512 p2p_channels_dump(p2p, "after go/cli filter/add", &p2p->channels);
1513
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001514 p2p_dbg(p2p, "Own preference for operation channel: Operating Class %u Channel %u%s",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001515 p2p->op_reg_class, p2p->op_channel,
1516 force_freq ? " (forced)" : "");
1517
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001518 if (force_freq)
1519 dev->flags |= P2P_DEV_FORCE_FREQ;
1520 else
1521 dev->flags &= ~P2P_DEV_FORCE_FREQ;
1522
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001523 return 0;
1524}
1525
1526
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001527static void p2p_set_dev_persistent(struct p2p_device *dev,
1528 int persistent_group)
1529{
1530 switch (persistent_group) {
1531 case 0:
1532 dev->flags &= ~(P2P_DEV_PREFER_PERSISTENT_GROUP |
1533 P2P_DEV_PREFER_PERSISTENT_RECONN);
1534 break;
1535 case 1:
1536 dev->flags |= P2P_DEV_PREFER_PERSISTENT_GROUP;
1537 dev->flags &= ~P2P_DEV_PREFER_PERSISTENT_RECONN;
1538 break;
1539 case 2:
1540 dev->flags |= P2P_DEV_PREFER_PERSISTENT_GROUP |
1541 P2P_DEV_PREFER_PERSISTENT_RECONN;
1542 break;
1543 }
1544}
1545
1546
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001547int p2p_connect(struct p2p_data *p2p, const u8 *peer_addr,
1548 enum p2p_wps_method wps_method,
1549 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07001550 unsigned int force_freq, int persistent_group,
1551 const u8 *force_ssid, size_t force_ssid_len,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001552 int pd_before_go_neg, unsigned int pref_freq, u16 oob_pw_id)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001553{
1554 struct p2p_device *dev;
1555
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001556 p2p_dbg(p2p, "Request to start group negotiation - peer=" MACSTR
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001557 " GO Intent=%d Intended Interface Address=" MACSTR
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001558 " wps_method=%d persistent_group=%d pd_before_go_neg=%d "
1559 "oob_pw_id=%u",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001560 MAC2STR(peer_addr), go_intent, MAC2STR(own_interface_addr),
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001561 wps_method, persistent_group, pd_before_go_neg, oob_pw_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001562
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001563 dev = p2p_get_device(p2p, peer_addr);
1564 if (dev == NULL || (dev->flags & P2P_DEV_PROBE_REQ_ONLY)) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001565 p2p_dbg(p2p, "Cannot connect to unknown P2P Device " MACSTR,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001566 MAC2STR(peer_addr));
1567 return -1;
1568 }
1569
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001570 if (p2p_prepare_channel(p2p, dev, force_freq, pref_freq,
1571 go_intent == 15) < 0)
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001572 return -1;
1573
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001574 if (dev->flags & P2P_DEV_GROUP_CLIENT_ONLY) {
1575 if (!(dev->info.dev_capab &
1576 P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY)) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001577 p2p_dbg(p2p, "Cannot connect to P2P Device " MACSTR
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001578 " that is in a group and is not discoverable",
1579 MAC2STR(peer_addr));
1580 return -1;
1581 }
1582 if (dev->oper_freq <= 0) {
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 " with incomplete information",
1585 MAC2STR(peer_addr));
1586 return -1;
1587 }
1588
1589 /*
1590 * First, try to connect directly. If the peer does not
1591 * acknowledge frames, assume it is sleeping and use device
1592 * discoverability via the GO at that point.
1593 */
1594 }
1595
Dmitry Shmidt04949592012-07-19 12:16:46 -07001596 p2p->ssid_set = 0;
1597 if (force_ssid) {
1598 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Forced SSID",
1599 force_ssid, force_ssid_len);
1600 os_memcpy(p2p->ssid, force_ssid, force_ssid_len);
1601 p2p->ssid_len = force_ssid_len;
1602 p2p->ssid_set = 1;
1603 }
1604
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001605 dev->flags &= ~P2P_DEV_NOT_YET_READY;
1606 dev->flags &= ~P2P_DEV_USER_REJECTED;
1607 dev->flags &= ~P2P_DEV_WAIT_GO_NEG_RESPONSE;
1608 dev->flags &= ~P2P_DEV_WAIT_GO_NEG_CONFIRM;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001609 if (pd_before_go_neg)
1610 dev->flags |= P2P_DEV_PD_BEFORE_GO_NEG;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001611 else {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001612 dev->flags &= ~P2P_DEV_PD_BEFORE_GO_NEG;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001613 /*
Dmitry Shmidt9cdf1b92013-02-27 12:58:50 -08001614 * Assign dialog token and tie breaker here to use the same
1615 * values in each retry within the same GO Negotiation exchange.
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001616 */
1617 dev->dialog_token++;
1618 if (dev->dialog_token == 0)
1619 dev->dialog_token = 1;
Dmitry Shmidt9cdf1b92013-02-27 12:58:50 -08001620 dev->tie_breaker = p2p->next_tie_breaker;
1621 p2p->next_tie_breaker = !p2p->next_tie_breaker;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001622 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001623 dev->connect_reqs = 0;
1624 dev->go_neg_req_sent = 0;
1625 dev->go_state = UNKNOWN_GO;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001626 p2p_set_dev_persistent(dev, persistent_group);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001627 p2p->go_intent = go_intent;
1628 os_memcpy(p2p->intended_addr, own_interface_addr, ETH_ALEN);
1629
1630 if (p2p->state != P2P_IDLE)
1631 p2p_stop_find(p2p);
1632
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001633 dev->wps_method = wps_method;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001634 dev->oob_pw_id = oob_pw_id;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001635 dev->status = P2P_SC_SUCCESS;
1636
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001637 if (p2p->p2p_scan_running) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001638 p2p_dbg(p2p, "p2p_scan running - delay connect send");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001639 p2p->start_after_scan = P2P_AFTER_SCAN_CONNECT;
1640 os_memcpy(p2p->after_scan_peer, peer_addr, ETH_ALEN);
1641 return 0;
1642 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001643
1644 return p2p_connect_send(p2p, dev);
1645}
1646
1647
1648int p2p_authorize(struct p2p_data *p2p, const u8 *peer_addr,
1649 enum p2p_wps_method wps_method,
1650 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07001651 unsigned int force_freq, int persistent_group,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001652 const u8 *force_ssid, size_t force_ssid_len,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001653 unsigned int pref_freq, u16 oob_pw_id)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001654{
1655 struct p2p_device *dev;
1656
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001657 p2p_dbg(p2p, "Request to authorize group negotiation - peer=" MACSTR
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001658 " GO Intent=%d Intended Interface Address=" MACSTR
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001659 " wps_method=%d persistent_group=%d oob_pw_id=%u",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001660 MAC2STR(peer_addr), go_intent, MAC2STR(own_interface_addr),
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001661 wps_method, persistent_group, oob_pw_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001662
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001663 dev = p2p_get_device(p2p, peer_addr);
1664 if (dev == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001665 p2p_dbg(p2p, "Cannot authorize unknown P2P Device " MACSTR,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001666 MAC2STR(peer_addr));
1667 return -1;
1668 }
1669
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001670 if (p2p_prepare_channel(p2p, dev, force_freq, pref_freq, go_intent ==
1671 15) < 0)
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001672 return -1;
1673
Dmitry Shmidt04949592012-07-19 12:16:46 -07001674 p2p->ssid_set = 0;
1675 if (force_ssid) {
1676 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Forced SSID",
1677 force_ssid, force_ssid_len);
1678 os_memcpy(p2p->ssid, force_ssid, force_ssid_len);
1679 p2p->ssid_len = force_ssid_len;
1680 p2p->ssid_set = 1;
1681 }
1682
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001683 dev->flags &= ~P2P_DEV_NOT_YET_READY;
1684 dev->flags &= ~P2P_DEV_USER_REJECTED;
1685 dev->go_neg_req_sent = 0;
1686 dev->go_state = UNKNOWN_GO;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001687 p2p_set_dev_persistent(dev, persistent_group);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001688 p2p->go_intent = go_intent;
1689 os_memcpy(p2p->intended_addr, own_interface_addr, ETH_ALEN);
1690
1691 dev->wps_method = wps_method;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001692 dev->oob_pw_id = oob_pw_id;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001693 dev->status = P2P_SC_SUCCESS;
1694
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001695 return 0;
1696}
1697
1698
1699void p2p_add_dev_info(struct p2p_data *p2p, const u8 *addr,
1700 struct p2p_device *dev, struct p2p_message *msg)
1701{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001702 os_get_reltime(&dev->last_seen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001703
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001704 p2p_copy_wps_info(p2p, dev, 0, msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001705
1706 if (msg->listen_channel) {
1707 int freq;
Dmitry Shmidt4b060592013-04-29 16:42:49 -07001708 freq = p2p_channel_to_freq(msg->listen_channel[3],
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001709 msg->listen_channel[4]);
1710 if (freq < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001711 p2p_dbg(p2p, "Unknown peer Listen channel: "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001712 "country=%c%c(0x%02x) reg_class=%u channel=%u",
1713 msg->listen_channel[0],
1714 msg->listen_channel[1],
1715 msg->listen_channel[2],
1716 msg->listen_channel[3],
1717 msg->listen_channel[4]);
1718 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001719 p2p_dbg(p2p, "Update peer " MACSTR
1720 " Listen channel: %u -> %u MHz",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001721 MAC2STR(dev->info.p2p_device_addr),
1722 dev->listen_freq, freq);
1723 dev->listen_freq = freq;
1724 }
1725 }
1726
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001727 if (msg->wfd_subelems) {
1728 wpabuf_free(dev->info.wfd_subelems);
1729 dev->info.wfd_subelems = wpabuf_dup(msg->wfd_subelems);
1730 }
1731
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001732 if (dev->flags & P2P_DEV_PROBE_REQ_ONLY) {
1733 dev->flags &= ~P2P_DEV_PROBE_REQ_ONLY;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001734 p2p_dbg(p2p, "Completed device entry based on data from GO Negotiation Request");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001735 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001736 p2p_dbg(p2p, "Created device entry based on GO Neg Req: "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001737 MACSTR " dev_capab=0x%x group_capab=0x%x name='%s' "
1738 "listen_freq=%d",
1739 MAC2STR(dev->info.p2p_device_addr),
1740 dev->info.dev_capab, dev->info.group_capab,
1741 dev->info.device_name, dev->listen_freq);
1742 }
1743
1744 dev->flags &= ~P2P_DEV_GROUP_CLIENT_ONLY;
1745
1746 if (dev->flags & P2P_DEV_USER_REJECTED) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001747 p2p_dbg(p2p, "Do not report rejected device");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001748 return;
1749 }
1750
1751 p2p->cfg->dev_found(p2p->cfg->cb_ctx, addr, &dev->info,
1752 !(dev->flags & P2P_DEV_REPORTED_ONCE));
1753 dev->flags |= P2P_DEV_REPORTED | P2P_DEV_REPORTED_ONCE;
1754}
1755
1756
1757void p2p_build_ssid(struct p2p_data *p2p, u8 *ssid, size_t *ssid_len)
1758{
1759 os_memcpy(ssid, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN);
1760 p2p_random((char *) &ssid[P2P_WILDCARD_SSID_LEN], 2);
1761 os_memcpy(&ssid[P2P_WILDCARD_SSID_LEN + 2],
1762 p2p->cfg->ssid_postfix, p2p->cfg->ssid_postfix_len);
1763 *ssid_len = P2P_WILDCARD_SSID_LEN + 2 + p2p->cfg->ssid_postfix_len;
1764}
1765
1766
1767int p2p_go_params(struct p2p_data *p2p, struct p2p_go_neg_results *params)
1768{
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001769 if (p2p->ssid_set) {
1770 os_memcpy(params->ssid, p2p->ssid, p2p->ssid_len);
1771 params->ssid_len = p2p->ssid_len;
1772 } else {
1773 p2p_build_ssid(p2p, params->ssid, &params->ssid_len);
1774 }
1775 p2p->ssid_set = 0;
1776
Jimmy Chen6d7e3902018-11-20 10:15:16 +08001777 if (p2p->passphrase_set) {
1778 os_memcpy(params->passphrase, p2p->passphrase, os_strlen(p2p->passphrase));
1779 } else {
1780 p2p_random(params->passphrase, p2p->cfg->passphrase_len);
1781 }
1782 p2p->passphrase_set = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001783 return 0;
1784}
1785
1786
1787void p2p_go_complete(struct p2p_data *p2p, struct p2p_device *peer)
1788{
1789 struct p2p_go_neg_results res;
1790 int go = peer->go_state == LOCAL_GO;
1791 struct p2p_channels intersection;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001792
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001793 p2p_dbg(p2p, "GO Negotiation with " MACSTR " completed (%s will be GO)",
1794 MAC2STR(peer->info.p2p_device_addr), go ? "local end" : "peer");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001795
1796 os_memset(&res, 0, sizeof(res));
1797 res.role_go = go;
1798 os_memcpy(res.peer_device_addr, peer->info.p2p_device_addr, ETH_ALEN);
1799 os_memcpy(res.peer_interface_addr, peer->intended_addr, ETH_ALEN);
1800 res.wps_method = peer->wps_method;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001801 if (peer->flags & P2P_DEV_PREFER_PERSISTENT_GROUP) {
1802 if (peer->flags & P2P_DEV_PREFER_PERSISTENT_RECONN)
1803 res.persistent_group = 2;
1804 else
1805 res.persistent_group = 1;
1806 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001807
1808 if (go) {
1809 /* Setup AP mode for WPS provisioning */
Dmitry Shmidt4b060592013-04-29 16:42:49 -07001810 res.freq = p2p_channel_to_freq(p2p->op_reg_class,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001811 p2p->op_channel);
1812 os_memcpy(res.ssid, p2p->ssid, p2p->ssid_len);
1813 res.ssid_len = p2p->ssid_len;
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07001814 p2p_random(res.passphrase, p2p->cfg->passphrase_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001815 } else {
1816 res.freq = peer->oper_freq;
1817 if (p2p->ssid_len) {
1818 os_memcpy(res.ssid, p2p->ssid, p2p->ssid_len);
1819 res.ssid_len = p2p->ssid_len;
1820 }
1821 }
1822
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001823 p2p_channels_dump(p2p, "own channels", &p2p->channels);
1824 p2p_channels_dump(p2p, "peer channels", &peer->channels);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001825 p2p_channels_intersect(&p2p->channels, &peer->channels,
1826 &intersection);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001827 if (go) {
1828 p2p_channels_remove_freqs(&intersection, &p2p->no_go_freq);
1829 p2p_channels_dump(p2p, "intersection after no-GO removal",
1830 &intersection);
1831 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001832
1833 p2p_channels_to_freqs(&intersection, res.freq_list,
1834 P2P_MAX_CHANNELS);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001835
1836 res.peer_config_timeout = go ? peer->client_timeout : peer->go_timeout;
1837
1838 p2p_clear_timeout(p2p);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001839 p2p->ssid_set = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001840 peer->go_neg_req_sent = 0;
Dmitry Shmidt29333592017-01-09 12:27:11 -08001841 peer->flags &= ~P2P_DEV_PEER_WAITING_RESPONSE;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001842 peer->wps_method = WPS_NOT_READY;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001843 peer->oob_pw_id = 0;
Dmitry Shmidt413dde72014-04-11 10:23:22 -07001844 wpabuf_free(peer->go_neg_conf);
1845 peer->go_neg_conf = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001846
1847 p2p_set_state(p2p, P2P_PROVISIONING);
1848 p2p->cfg->go_neg_completed(p2p->cfg->cb_ctx, &res);
1849}
1850
1851
1852static void p2p_rx_p2p_action(struct p2p_data *p2p, const u8 *sa,
1853 const u8 *data, size_t len, int rx_freq)
1854{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001855 p2p_dbg(p2p, "RX P2P Public Action from " MACSTR, MAC2STR(sa));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001856 wpa_hexdump(MSG_MSGDUMP, "P2P: P2P Public Action contents", data, len);
1857
1858 if (len < 1)
1859 return;
1860
1861 switch (data[0]) {
1862 case P2P_GO_NEG_REQ:
1863 p2p_process_go_neg_req(p2p, sa, data + 1, len - 1, rx_freq);
1864 break;
1865 case P2P_GO_NEG_RESP:
1866 p2p_process_go_neg_resp(p2p, sa, data + 1, len - 1, rx_freq);
1867 break;
1868 case P2P_GO_NEG_CONF:
1869 p2p_process_go_neg_conf(p2p, sa, data + 1, len - 1);
1870 break;
1871 case P2P_INVITATION_REQ:
1872 p2p_process_invitation_req(p2p, sa, data + 1, len - 1,
1873 rx_freq);
1874 break;
1875 case P2P_INVITATION_RESP:
1876 p2p_process_invitation_resp(p2p, sa, data + 1, len - 1);
1877 break;
1878 case P2P_PROV_DISC_REQ:
1879 p2p_process_prov_disc_req(p2p, sa, data + 1, len - 1, rx_freq);
1880 break;
1881 case P2P_PROV_DISC_RESP:
1882 p2p_process_prov_disc_resp(p2p, sa, data + 1, len - 1);
1883 break;
1884 case P2P_DEV_DISC_REQ:
1885 p2p_process_dev_disc_req(p2p, sa, data + 1, len - 1, rx_freq);
1886 break;
1887 case P2P_DEV_DISC_RESP:
1888 p2p_process_dev_disc_resp(p2p, sa, data + 1, len - 1);
1889 break;
1890 default:
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001891 p2p_dbg(p2p, "Unsupported P2P Public Action frame type %d",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001892 data[0]);
1893 break;
1894 }
1895}
1896
1897
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001898static void p2p_rx_action_public(struct p2p_data *p2p, const u8 *da,
1899 const u8 *sa, const u8 *bssid, const u8 *data,
1900 size_t len, int freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001901{
1902 if (len < 1)
1903 return;
1904
1905 switch (data[0]) {
1906 case WLAN_PA_VENDOR_SPECIFIC:
1907 data++;
1908 len--;
Dmitry Shmidta38abf92014-03-06 13:38:44 -08001909 if (len < 4)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001910 return;
Dmitry Shmidta38abf92014-03-06 13:38:44 -08001911 if (WPA_GET_BE32(data) != P2P_IE_VENDOR_TYPE)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001912 return;
1913
Dmitry Shmidta38abf92014-03-06 13:38:44 -08001914 data += 4;
1915 len -= 4;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001916
Dmitry Shmidta38abf92014-03-06 13:38:44 -08001917 p2p_rx_p2p_action(p2p, sa, data, len, freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001918 break;
1919 case WLAN_PA_GAS_INITIAL_REQ:
1920 p2p_rx_gas_initial_req(p2p, sa, data + 1, len - 1, freq);
1921 break;
1922 case WLAN_PA_GAS_INITIAL_RESP:
1923 p2p_rx_gas_initial_resp(p2p, sa, data + 1, len - 1, freq);
1924 break;
1925 case WLAN_PA_GAS_COMEBACK_REQ:
1926 p2p_rx_gas_comeback_req(p2p, sa, data + 1, len - 1, freq);
1927 break;
1928 case WLAN_PA_GAS_COMEBACK_RESP:
1929 p2p_rx_gas_comeback_resp(p2p, sa, data + 1, len - 1, freq);
1930 break;
1931 }
1932}
1933
1934
1935void p2p_rx_action(struct p2p_data *p2p, const u8 *da, const u8 *sa,
1936 const u8 *bssid, u8 category,
1937 const u8 *data, size_t len, int freq)
1938{
1939 if (category == WLAN_ACTION_PUBLIC) {
1940 p2p_rx_action_public(p2p, da, sa, bssid, data, len, freq);
1941 return;
1942 }
1943
1944 if (category != WLAN_ACTION_VENDOR_SPECIFIC)
1945 return;
1946
1947 if (len < 4)
1948 return;
1949
Dmitry Shmidta38abf92014-03-06 13:38:44 -08001950 if (WPA_GET_BE32(data) != P2P_IE_VENDOR_TYPE)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001951 return;
Dmitry Shmidta38abf92014-03-06 13:38:44 -08001952 data += 4;
1953 len -= 4;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001954
1955 /* P2P action frame */
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001956 p2p_dbg(p2p, "RX P2P Action from " MACSTR, MAC2STR(sa));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001957 wpa_hexdump(MSG_MSGDUMP, "P2P: P2P Action contents", data, len);
1958
1959 if (len < 1)
1960 return;
1961 switch (data[0]) {
1962 case P2P_NOA:
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001963 p2p_dbg(p2p, "Received P2P Action - Notice of Absence");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001964 /* TODO */
1965 break;
1966 case P2P_PRESENCE_REQ:
1967 p2p_process_presence_req(p2p, da, sa, data + 1, len - 1, freq);
1968 break;
1969 case P2P_PRESENCE_RESP:
1970 p2p_process_presence_resp(p2p, da, sa, data + 1, len - 1);
1971 break;
1972 case P2P_GO_DISC_REQ:
1973 p2p_process_go_disc_req(p2p, da, sa, data + 1, len - 1, freq);
1974 break;
1975 default:
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001976 p2p_dbg(p2p, "Received P2P Action - unknown type %u", data[0]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001977 break;
1978 }
1979}
1980
1981
1982static void p2p_go_neg_start(void *eloop_ctx, void *timeout_ctx)
1983{
1984 struct p2p_data *p2p = eloop_ctx;
1985 if (p2p->go_neg_peer == NULL)
1986 return;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001987 if (p2p->pending_listen_freq) {
1988 p2p_dbg(p2p, "Clear pending_listen_freq for p2p_go_neg_start");
1989 p2p->pending_listen_freq = 0;
1990 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001991 p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
1992 p2p->go_neg_peer->status = P2P_SC_SUCCESS;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001993 /*
1994 * Set new timeout to make sure a previously set one does not expire
1995 * too quickly while waiting for the GO Negotiation to complete.
1996 */
1997 p2p_set_timeout(p2p, 0, 500000);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001998 p2p_connect_send(p2p, p2p->go_neg_peer);
1999}
2000
2001
2002static void p2p_invite_start(void *eloop_ctx, void *timeout_ctx)
2003{
2004 struct p2p_data *p2p = eloop_ctx;
2005 if (p2p->invite_peer == NULL)
2006 return;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07002007 if (p2p->pending_listen_freq) {
2008 p2p_dbg(p2p, "Clear pending_listen_freq for p2p_invite_start");
2009 p2p->pending_listen_freq = 0;
2010 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002011 p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002012 p2p_invite_send(p2p, p2p->invite_peer, p2p->invite_go_dev_addr,
2013 p2p->invite_dev_pw_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002014}
2015
2016
2017static void p2p_add_dev_from_probe_req(struct p2p_data *p2p, const u8 *addr,
2018 const u8 *ie, size_t ie_len)
2019{
2020 struct p2p_message msg;
2021 struct p2p_device *dev;
2022
2023 os_memset(&msg, 0, sizeof(msg));
2024 if (p2p_parse_ies(ie, ie_len, &msg) < 0 || msg.p2p_attributes == NULL)
2025 {
2026 p2p_parse_free(&msg);
2027 return; /* not a P2P probe */
2028 }
2029
2030 if (msg.ssid == NULL || msg.ssid[1] != P2P_WILDCARD_SSID_LEN ||
2031 os_memcmp(msg.ssid + 2, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN)
2032 != 0) {
2033 /* The Probe Request is not part of P2P Device Discovery. It is
2034 * not known whether the source address of the frame is the P2P
2035 * Device Address or P2P Interface Address. Do not add a new
2036 * peer entry based on this frames.
2037 */
2038 p2p_parse_free(&msg);
2039 return;
2040 }
2041
2042 dev = p2p_get_device(p2p, addr);
2043 if (dev) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002044 if (msg.listen_channel) {
2045 int freq;
2046
2047 if (dev->country[0] == 0)
2048 os_memcpy(dev->country, msg.listen_channel, 3);
2049
2050 freq = p2p_channel_to_freq(msg.listen_channel[3],
2051 msg.listen_channel[4]);
2052
2053 if (freq > 0 && dev->listen_freq != freq) {
2054 p2p_dbg(p2p,
2055 "Updated peer " MACSTR " Listen channel (Probe Request): %d -> %d MHz",
2056 MAC2STR(addr), dev->listen_freq, freq);
2057 dev->listen_freq = freq;
2058 }
2059 }
2060
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002061 os_get_reltime(&dev->last_seen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002062 p2p_parse_free(&msg);
2063 return; /* already known */
2064 }
2065
2066 dev = p2p_create_device(p2p, addr);
2067 if (dev == NULL) {
2068 p2p_parse_free(&msg);
2069 return;
2070 }
2071
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002072 os_get_reltime(&dev->last_seen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002073 dev->flags |= P2P_DEV_PROBE_REQ_ONLY;
2074
2075 if (msg.listen_channel) {
2076 os_memcpy(dev->country, msg.listen_channel, 3);
Dmitry Shmidt4b060592013-04-29 16:42:49 -07002077 dev->listen_freq = p2p_channel_to_freq(msg.listen_channel[3],
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002078 msg.listen_channel[4]);
2079 }
2080
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002081 p2p_copy_wps_info(p2p, dev, 1, &msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002082
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002083 if (msg.wfd_subelems) {
2084 wpabuf_free(dev->info.wfd_subelems);
2085 dev->info.wfd_subelems = wpabuf_dup(msg.wfd_subelems);
2086 }
2087
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002088 p2p_parse_free(&msg);
2089
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002090 p2p_dbg(p2p, "Created device entry based on Probe Req: " MACSTR
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002091 " dev_capab=0x%x group_capab=0x%x name='%s' listen_freq=%d",
2092 MAC2STR(dev->info.p2p_device_addr), dev->info.dev_capab,
2093 dev->info.group_capab, dev->info.device_name,
2094 dev->listen_freq);
2095}
2096
2097
2098struct p2p_device * p2p_add_dev_from_go_neg_req(struct p2p_data *p2p,
2099 const u8 *addr,
2100 struct p2p_message *msg)
2101{
2102 struct p2p_device *dev;
2103
2104 dev = p2p_get_device(p2p, addr);
2105 if (dev) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002106 os_get_reltime(&dev->last_seen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002107 return dev; /* already known */
2108 }
2109
2110 dev = p2p_create_device(p2p, addr);
2111 if (dev == NULL)
2112 return NULL;
2113
2114 p2p_add_dev_info(p2p, addr, dev, msg);
2115
2116 return dev;
2117}
2118
2119
2120static int dev_type_match(const u8 *dev_type, const u8 *req_dev_type)
2121{
2122 if (os_memcmp(dev_type, req_dev_type, WPS_DEV_TYPE_LEN) == 0)
2123 return 1;
2124 if (os_memcmp(dev_type, req_dev_type, 2) == 0 &&
2125 WPA_GET_BE32(&req_dev_type[2]) == 0 &&
2126 WPA_GET_BE16(&req_dev_type[6]) == 0)
2127 return 1; /* Category match with wildcard OUI/sub-category */
2128 return 0;
2129}
2130
2131
2132int dev_type_list_match(const u8 *dev_type, const u8 *req_dev_type[],
2133 size_t num_req_dev_type)
2134{
2135 size_t i;
2136 for (i = 0; i < num_req_dev_type; i++) {
2137 if (dev_type_match(dev_type, req_dev_type[i]))
2138 return 1;
2139 }
2140 return 0;
2141}
2142
2143
2144/**
2145 * p2p_match_dev_type - Match local device type with requested type
2146 * @p2p: P2P module context from p2p_init()
2147 * @wps: WPS TLVs from Probe Request frame (concatenated WPS IEs)
2148 * Returns: 1 on match, 0 on mismatch
2149 *
2150 * This function can be used to match the Requested Device Type attribute in
2151 * WPS IE with the local device types for deciding whether to reply to a Probe
2152 * Request frame.
2153 */
2154int p2p_match_dev_type(struct p2p_data *p2p, struct wpabuf *wps)
2155{
2156 struct wps_parse_attr attr;
2157 size_t i;
2158
2159 if (wps_parse_msg(wps, &attr))
2160 return 1; /* assume no Requested Device Type attributes */
2161
2162 if (attr.num_req_dev_type == 0)
2163 return 1; /* no Requested Device Type attributes -> match */
2164
2165 if (dev_type_list_match(p2p->cfg->pri_dev_type, attr.req_dev_type,
2166 attr.num_req_dev_type))
2167 return 1; /* Own Primary Device Type matches */
2168
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002169 for (i = 0; i < p2p->cfg->num_sec_dev_types; i++) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002170 if (dev_type_list_match(p2p->cfg->sec_dev_type[i],
2171 attr.req_dev_type,
2172 attr.num_req_dev_type))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002173 return 1; /* Own Secondary Device Type matches */
2174 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002175
2176 /* No matching device type found */
2177 return 0;
2178}
2179
2180
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002181struct wpabuf * p2p_build_probe_resp_ies(struct p2p_data *p2p,
2182 const u8 *query_hash,
2183 u8 query_count)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002184{
2185 struct wpabuf *buf;
2186 u8 *len;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002187 int pw_id = -1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002188 size_t extra = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002189
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002190#ifdef CONFIG_WIFI_DISPLAY
2191 if (p2p->wfd_ie_probe_resp)
2192 extra = wpabuf_len(p2p->wfd_ie_probe_resp);
2193#endif /* CONFIG_WIFI_DISPLAY */
2194
Dmitry Shmidt2e67f062014-07-16 09:55:28 -07002195 if (p2p->vendor_elem && p2p->vendor_elem[VENDOR_ELEM_PROBE_RESP_P2P])
2196 extra += wpabuf_len(p2p->vendor_elem[VENDOR_ELEM_PROBE_RESP_P2P]);
2197
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002198 if (query_count)
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002199 extra += MAX_SVC_ADV_IE_LEN;
2200
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002201 buf = wpabuf_alloc(1000 + extra);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002202 if (buf == NULL)
2203 return NULL;
2204
Dmitry Shmidt04949592012-07-19 12:16:46 -07002205 if (p2p->go_neg_peer) {
2206 /* Advertise immediate availability of WPS credential */
2207 pw_id = p2p_wps_method_pw_id(p2p->go_neg_peer->wps_method);
2208 }
2209
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002210 if (p2p_build_wps_ie(p2p, buf, pw_id, 1) < 0) {
2211 p2p_dbg(p2p, "Failed to build WPS IE for Probe Response");
2212 wpabuf_free(buf);
2213 return NULL;
2214 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002215
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002216#ifdef CONFIG_WIFI_DISPLAY
2217 if (p2p->wfd_ie_probe_resp)
2218 wpabuf_put_buf(buf, p2p->wfd_ie_probe_resp);
2219#endif /* CONFIG_WIFI_DISPLAY */
2220
Dmitry Shmidt2e67f062014-07-16 09:55:28 -07002221 if (p2p->vendor_elem && p2p->vendor_elem[VENDOR_ELEM_PROBE_RESP_P2P])
2222 wpabuf_put_buf(buf,
2223 p2p->vendor_elem[VENDOR_ELEM_PROBE_RESP_P2P]);
2224
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002225 /* P2P IE */
2226 len = p2p_buf_add_ie_hdr(buf);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002227 p2p_buf_add_capability(buf, p2p->dev_capab &
2228 ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002229 if (p2p->ext_listen_interval)
2230 p2p_buf_add_ext_listen_timing(buf, p2p->ext_listen_period,
2231 p2p->ext_listen_interval);
2232 p2p_buf_add_device_info(buf, p2p, NULL);
2233 p2p_buf_update_ie_hdr(buf, len);
2234
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002235 if (query_count) {
2236 p2p_buf_add_service_instance(buf, p2p, query_count, query_hash,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002237 p2p->p2ps_adv_list);
2238 }
2239
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002240 return buf;
2241}
2242
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07002243static int p2p_build_probe_resp_buf(struct p2p_data *p2p, struct wpabuf *buf,
2244 struct wpabuf *ies,
2245 const u8 *addr, int rx_freq)
2246{
2247 struct ieee80211_mgmt *resp;
2248 u8 channel, op_class;
2249
2250 resp = wpabuf_put(buf, offsetof(struct ieee80211_mgmt,
2251 u.probe_resp.variable));
2252
2253 resp->frame_control = host_to_le16((WLAN_FC_TYPE_MGMT << 2) |
2254 (WLAN_FC_STYPE_PROBE_RESP << 4));
2255 os_memcpy(resp->da, addr, ETH_ALEN);
2256 os_memcpy(resp->sa, p2p->cfg->dev_addr, ETH_ALEN);
2257 os_memcpy(resp->bssid, p2p->cfg->dev_addr, ETH_ALEN);
2258 resp->u.probe_resp.beacon_int = host_to_le16(100);
2259 /* hardware or low-level driver will setup seq_ctrl and timestamp */
2260 resp->u.probe_resp.capab_info =
2261 host_to_le16(WLAN_CAPABILITY_SHORT_PREAMBLE |
2262 WLAN_CAPABILITY_PRIVACY |
2263 WLAN_CAPABILITY_SHORT_SLOT_TIME);
2264
2265 wpabuf_put_u8(buf, WLAN_EID_SSID);
2266 wpabuf_put_u8(buf, P2P_WILDCARD_SSID_LEN);
2267 wpabuf_put_data(buf, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN);
2268
2269 wpabuf_put_u8(buf, WLAN_EID_SUPP_RATES);
2270 wpabuf_put_u8(buf, 8);
2271 wpabuf_put_u8(buf, (60 / 5) | 0x80);
2272 wpabuf_put_u8(buf, 90 / 5);
2273 wpabuf_put_u8(buf, (120 / 5) | 0x80);
2274 wpabuf_put_u8(buf, 180 / 5);
2275 wpabuf_put_u8(buf, (240 / 5) | 0x80);
2276 wpabuf_put_u8(buf, 360 / 5);
2277 wpabuf_put_u8(buf, 480 / 5);
2278 wpabuf_put_u8(buf, 540 / 5);
2279
2280 if (!rx_freq) {
2281 channel = p2p->cfg->channel;
2282 } else if (p2p_freq_to_channel(rx_freq, &op_class, &channel)) {
2283 p2p_err(p2p, "Failed to convert freq to channel");
2284 return -1;
2285 }
2286
2287 wpabuf_put_u8(buf, WLAN_EID_DS_PARAMS);
2288 wpabuf_put_u8(buf, 1);
2289 wpabuf_put_u8(buf, channel);
2290
2291 wpabuf_put_buf(buf, ies);
2292
2293 return 0;
2294}
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002295
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002296static int p2p_service_find_asp(struct p2p_data *p2p, const u8 *hash)
2297{
2298 struct p2ps_advertisement *adv_data;
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002299 int any_wfa;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002300
2301 p2p_dbg(p2p, "ASP find - ASP list: %p", p2p->p2ps_adv_list);
2302
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002303 /* Wildcard org.wi-fi.wfds matches any WFA spec defined service */
2304 any_wfa = os_memcmp(hash, p2p->wild_card_hash, P2PS_HASH_LEN) == 0;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002305
2306 adv_data = p2p->p2ps_adv_list;
2307 while (adv_data) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002308 if (os_memcmp(hash, adv_data->hash, P2PS_HASH_LEN) == 0)
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002309 return 1; /* exact hash match */
2310 if (any_wfa &&
2311 os_strncmp(adv_data->svc_name, P2PS_WILD_HASH_STR,
2312 os_strlen(P2PS_WILD_HASH_STR)) == 0)
2313 return 1; /* WFA service match */
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002314 adv_data = adv_data->next;
2315 }
2316
2317 return 0;
2318}
2319
2320
Dmitry Shmidt04949592012-07-19 12:16:46 -07002321static enum p2p_probe_req_status
2322p2p_reply_probe(struct p2p_data *p2p, const u8 *addr, const u8 *dst,
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002323 const u8 *bssid, const u8 *ie, size_t ie_len,
2324 unsigned int rx_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002325{
2326 struct ieee802_11_elems elems;
2327 struct wpabuf *buf;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002328 struct p2p_message msg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002329 struct wpabuf *ies;
2330
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002331 if (ieee802_11_parse_elems((u8 *) ie, ie_len, &elems, 0) ==
2332 ParseFailed) {
2333 /* Ignore invalid Probe Request frames */
Dmitry Shmidtec58b162014-02-19 12:44:18 -08002334 p2p_dbg(p2p, "Could not parse Probe Request frame - ignore it");
Dmitry Shmidt04949592012-07-19 12:16:46 -07002335 return P2P_PREQ_MALFORMED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002336 }
2337
2338 if (elems.p2p == NULL) {
2339 /* not a P2P probe - ignore it */
Dmitry Shmidtec58b162014-02-19 12:44:18 -08002340 p2p_dbg(p2p, "Not a P2P probe - ignore it");
Dmitry Shmidt04949592012-07-19 12:16:46 -07002341 return P2P_PREQ_NOT_P2P;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002342 }
2343
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002344 if (dst && !is_broadcast_ether_addr(dst) &&
2345 os_memcmp(dst, p2p->cfg->dev_addr, ETH_ALEN) != 0) {
2346 /* Not sent to the broadcast address or our P2P Device Address
2347 */
Dmitry Shmidtec58b162014-02-19 12:44:18 -08002348 p2p_dbg(p2p, "Probe Req DA " MACSTR " not ours - ignore it",
2349 MAC2STR(dst));
Dmitry Shmidt04949592012-07-19 12:16:46 -07002350 return P2P_PREQ_NOT_PROCESSED;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002351 }
2352
2353 if (bssid && !is_broadcast_ether_addr(bssid)) {
2354 /* Not sent to the Wildcard BSSID */
Dmitry Shmidtec58b162014-02-19 12:44:18 -08002355 p2p_dbg(p2p, "Probe Req BSSID " MACSTR " not wildcard - ignore it",
2356 MAC2STR(bssid));
Dmitry Shmidt04949592012-07-19 12:16:46 -07002357 return P2P_PREQ_NOT_PROCESSED;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002358 }
2359
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002360 if (elems.ssid == NULL || elems.ssid_len != P2P_WILDCARD_SSID_LEN ||
2361 os_memcmp(elems.ssid, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN) !=
2362 0) {
2363 /* not using P2P Wildcard SSID - ignore */
Dmitry Shmidtec58b162014-02-19 12:44:18 -08002364 p2p_dbg(p2p, "Probe Req not using P2P Wildcard SSID - ignore it");
Dmitry Shmidt04949592012-07-19 12:16:46 -07002365 return P2P_PREQ_NOT_PROCESSED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002366 }
2367
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002368 if (supp_rates_11b_only(&elems)) {
2369 /* Indicates support for 11b rates only */
Dmitry Shmidtec58b162014-02-19 12:44:18 -08002370 p2p_dbg(p2p, "Probe Req with 11b rates only supported - ignore it");
Dmitry Shmidt04949592012-07-19 12:16:46 -07002371 return P2P_PREQ_NOT_P2P;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002372 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002373
2374 os_memset(&msg, 0, sizeof(msg));
2375 if (p2p_parse_ies(ie, ie_len, &msg) < 0) {
2376 /* Could not parse P2P attributes */
Dmitry Shmidtec58b162014-02-19 12:44:18 -08002377 p2p_dbg(p2p, "Could not parse P2P attributes in Probe Req - ignore it");
Dmitry Shmidt04949592012-07-19 12:16:46 -07002378 return P2P_PREQ_NOT_P2P;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002379 }
2380
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002381 if (msg.service_hash && msg.service_hash_count) {
2382 const u8 *hash = msg.service_hash;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002383 u8 i;
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002384 int p2ps_svc_found = 0;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002385
Dmitry Shmidt41712582015-06-29 11:02:15 -07002386 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",
2387 p2p->in_listen, p2p->drv_in_listen, rx_freq,
2388 p2p->cfg->channel, p2p->pending_listen_freq);
2389
2390 if (!p2p->in_listen && !p2p->drv_in_listen &&
2391 p2p->pending_listen_freq && rx_freq &&
2392 rx_freq != p2p->pending_listen_freq) {
2393 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",
2394 rx_freq, p2p->pending_listen_freq);
2395 p2p_parse_free(&msg);
2396 return P2P_PREQ_NOT_LISTEN;
2397 }
2398
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002399 for (i = 0; i < msg.service_hash_count; i++) {
2400 if (p2p_service_find_asp(p2p, hash)) {
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002401 p2p_dbg(p2p, "Service Hash match found: "
2402 MACSTR, MAC2STR(hash));
2403 p2ps_svc_found = 1;
2404 break;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002405 }
2406 hash += P2PS_HASH_LEN;
2407 }
2408
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002409 /* Probed hash unknown */
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002410 if (!p2ps_svc_found) {
2411 p2p_dbg(p2p, "No Service Hash match found");
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002412 p2p_parse_free(&msg);
2413 return P2P_PREQ_NOT_PROCESSED;
2414 }
2415 } else {
2416 /* This is not a P2PS Probe Request */
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002417 p2p_dbg(p2p, "No P2PS Hash in Probe Request");
2418
2419 if (!p2p->in_listen || !p2p->drv_in_listen) {
2420 /* not in Listen state - ignore Probe Request */
2421 p2p_dbg(p2p, "Not in Listen state (in_listen=%d drv_in_listen=%d) - ignore Probe Request",
2422 p2p->in_listen, p2p->drv_in_listen);
2423 p2p_parse_free(&msg);
2424 return P2P_PREQ_NOT_LISTEN;
2425 }
2426 }
2427
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002428 if (msg.device_id &&
Dmitry Shmidt04949592012-07-19 12:16:46 -07002429 os_memcmp(msg.device_id, p2p->cfg->dev_addr, ETH_ALEN) != 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002430 /* Device ID did not match */
Dmitry Shmidtec58b162014-02-19 12:44:18 -08002431 p2p_dbg(p2p, "Probe Req requested Device ID " MACSTR " did not match - ignore it",
2432 MAC2STR(msg.device_id));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002433 p2p_parse_free(&msg);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002434 return P2P_PREQ_NOT_PROCESSED;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002435 }
2436
2437 /* Check Requested Device Type match */
2438 if (msg.wps_attributes &&
2439 !p2p_match_dev_type(p2p, msg.wps_attributes)) {
2440 /* No match with Requested Device Type */
Hai Shalom021b0b52019-04-10 11:17:58 -07002441 p2p_dbg(p2p, "Probe Req requested Device Type did not match - ignore it");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002442 p2p_parse_free(&msg);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002443 return P2P_PREQ_NOT_PROCESSED;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002444 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002445
Dmitry Shmidt04949592012-07-19 12:16:46 -07002446 if (!p2p->cfg->send_probe_resp) {
2447 /* Response generated elsewhere */
Dmitry Shmidtec58b162014-02-19 12:44:18 -08002448 p2p_dbg(p2p, "Probe Resp generated elsewhere - do not generate additional response");
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002449 p2p_parse_free(&msg);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002450 return P2P_PREQ_NOT_PROCESSED;
2451 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002452
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002453 p2p_dbg(p2p, "Reply to P2P Probe Request in Listen state");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002454
2455 /*
2456 * We do not really have a specific BSS that this frame is advertising,
2457 * so build a frame that has some information in valid format. This is
2458 * really only used for discovery purposes, not to learn exact BSS
2459 * parameters.
2460 */
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002461 ies = p2p_build_probe_resp_ies(p2p, msg.service_hash,
2462 msg.service_hash_count);
2463 p2p_parse_free(&msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002464 if (ies == NULL)
Dmitry Shmidt04949592012-07-19 12:16:46 -07002465 return P2P_PREQ_NOT_PROCESSED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002466
2467 buf = wpabuf_alloc(200 + wpabuf_len(ies));
2468 if (buf == NULL) {
2469 wpabuf_free(ies);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002470 return P2P_PREQ_NOT_PROCESSED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002471 }
2472
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07002473 if (p2p_build_probe_resp_buf(p2p, buf, ies, addr, rx_freq)) {
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002474 wpabuf_free(ies);
2475 wpabuf_free(buf);
2476 return P2P_PREQ_NOT_PROCESSED;
2477 }
2478
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002479 wpabuf_free(ies);
2480
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002481 p2p->cfg->send_probe_resp(p2p->cfg->cb_ctx, buf, rx_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002482
2483 wpabuf_free(buf);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002484
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002485 return P2P_PREQ_PROCESSED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002486}
2487
2488
Dmitry Shmidt04949592012-07-19 12:16:46 -07002489enum p2p_probe_req_status
2490p2p_probe_req_rx(struct p2p_data *p2p, const u8 *addr, const u8 *dst,
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002491 const u8 *bssid, const u8 *ie, size_t ie_len,
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07002492 unsigned int rx_freq, int p2p_lo_started)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002493{
Dmitry Shmidt04949592012-07-19 12:16:46 -07002494 enum p2p_probe_req_status res;
2495
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002496 p2p_add_dev_from_probe_req(p2p, addr, ie, ie_len);
2497
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07002498 if (p2p_lo_started) {
2499 p2p_dbg(p2p,
2500 "Probe Response is offloaded, do not reply Probe Request");
2501 return P2P_PREQ_PROCESSED;
2502 }
2503
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002504 res = p2p_reply_probe(p2p, addr, dst, bssid, ie, ie_len, rx_freq);
2505 if (res != P2P_PREQ_PROCESSED && res != P2P_PREQ_NOT_PROCESSED)
2506 return res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002507
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002508 /*
2509 * Activate a pending GO Negotiation/Invite flow if a received Probe
2510 * Request frame is from an expected peer. Some devices may share the
2511 * same address for P2P and non-P2P STA running simultaneously. The
2512 * P2P_PREQ_PROCESSED and P2P_PREQ_NOT_PROCESSED p2p_reply_probe()
2513 * return values verified above ensure we are handling a Probe Request
2514 * frame from a P2P peer.
2515 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002516 if ((p2p->state == P2P_CONNECT || p2p->state == P2P_CONNECT_LISTEN) &&
2517 p2p->go_neg_peer &&
2518 os_memcmp(addr, p2p->go_neg_peer->info.p2p_device_addr, ETH_ALEN)
Dmitry Shmidt9cdf1b92013-02-27 12:58:50 -08002519 == 0 &&
2520 !(p2p->go_neg_peer->flags & P2P_DEV_WAIT_GO_NEG_CONFIRM)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002521 /* Received a Probe Request from GO Negotiation peer */
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002522 p2p_dbg(p2p, "Found GO Negotiation peer - try to start GO negotiation from timeout");
Dmitry Shmidt9cdf1b92013-02-27 12:58:50 -08002523 eloop_cancel_timeout(p2p_go_neg_start, p2p, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002524 eloop_register_timeout(0, 0, p2p_go_neg_start, p2p, NULL);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002525 return res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002526 }
2527
2528 if ((p2p->state == P2P_INVITE || p2p->state == P2P_INVITE_LISTEN) &&
2529 p2p->invite_peer &&
Dmitry Shmidt3c479372014-02-04 10:50:36 -08002530 (p2p->invite_peer->flags & P2P_DEV_WAIT_INV_REQ_ACK) &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002531 os_memcmp(addr, p2p->invite_peer->info.p2p_device_addr, ETH_ALEN)
2532 == 0) {
2533 /* Received a Probe Request from Invite peer */
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002534 p2p_dbg(p2p, "Found Invite peer - try to start Invite from timeout");
Dmitry Shmidt7f93d6f2014-02-21 11:22:49 -08002535 eloop_cancel_timeout(p2p_invite_start, p2p, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002536 eloop_register_timeout(0, 0, p2p_invite_start, p2p, NULL);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002537 return res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002538 }
2539
Dmitry Shmidt04949592012-07-19 12:16:46 -07002540 return res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002541}
2542
2543
2544static int p2p_assoc_req_ie_wlan_ap(struct p2p_data *p2p, const u8 *bssid,
2545 u8 *buf, size_t len, struct wpabuf *p2p_ie)
2546{
2547 struct wpabuf *tmp;
2548 u8 *lpos;
2549 size_t tmplen;
2550 int res;
2551 u8 group_capab;
Dmitry Shmidt05df46a2015-05-19 11:02:01 -07002552 struct p2p_message msg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002553
2554 if (p2p_ie == NULL)
2555 return 0; /* WLAN AP is not a P2P manager */
2556
Dmitry Shmidt05df46a2015-05-19 11:02:01 -07002557 os_memset(&msg, 0, sizeof(msg));
2558 if (p2p_parse_p2p_ie(p2p_ie, &msg) < 0)
2559 return 0;
2560
2561 p2p_dbg(p2p, "BSS P2P manageability %s",
2562 msg.manageability ? "enabled" : "disabled");
2563
2564 if (!msg.manageability)
2565 return 0;
2566
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002567 /*
2568 * (Re)Association Request - P2P IE
2569 * P2P Capability attribute (shall be present)
2570 * P2P Interface attribute (present if concurrent device and
2571 * P2P Management is enabled)
2572 */
2573 tmp = wpabuf_alloc(200);
2574 if (tmp == NULL)
2575 return -1;
2576
2577 lpos = p2p_buf_add_ie_hdr(tmp);
2578 group_capab = 0;
2579 if (p2p->num_groups > 0) {
2580 group_capab |= P2P_GROUP_CAPAB_GROUP_OWNER;
2581 if ((p2p->dev_capab & P2P_DEV_CAPAB_CONCURRENT_OPER) &&
2582 (p2p->dev_capab & P2P_DEV_CAPAB_INFRA_MANAGED) &&
2583 p2p->cross_connect)
2584 group_capab |= P2P_GROUP_CAPAB_CROSS_CONN;
2585 }
2586 p2p_buf_add_capability(tmp, p2p->dev_capab, group_capab);
2587 if ((p2p->dev_capab & P2P_DEV_CAPAB_CONCURRENT_OPER) &&
2588 (p2p->dev_capab & P2P_DEV_CAPAB_INFRA_MANAGED))
2589 p2p_buf_add_p2p_interface(tmp, p2p);
2590 p2p_buf_update_ie_hdr(tmp, lpos);
2591
2592 tmplen = wpabuf_len(tmp);
2593 if (tmplen > len)
2594 res = -1;
2595 else {
2596 os_memcpy(buf, wpabuf_head(tmp), tmplen);
2597 res = tmplen;
2598 }
2599 wpabuf_free(tmp);
2600
2601 return res;
2602}
2603
2604
2605int p2p_assoc_req_ie(struct p2p_data *p2p, const u8 *bssid, u8 *buf,
2606 size_t len, int p2p_group, struct wpabuf *p2p_ie)
2607{
2608 struct wpabuf *tmp;
2609 u8 *lpos;
2610 struct p2p_device *peer;
2611 size_t tmplen;
2612 int res;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002613 size_t extra = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002614
2615 if (!p2p_group)
2616 return p2p_assoc_req_ie_wlan_ap(p2p, bssid, buf, len, p2p_ie);
2617
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002618#ifdef CONFIG_WIFI_DISPLAY
2619 if (p2p->wfd_ie_assoc_req)
2620 extra = wpabuf_len(p2p->wfd_ie_assoc_req);
2621#endif /* CONFIG_WIFI_DISPLAY */
2622
Dmitry Shmidt2e67f062014-07-16 09:55:28 -07002623 if (p2p->vendor_elem && p2p->vendor_elem[VENDOR_ELEM_P2P_ASSOC_REQ])
2624 extra += wpabuf_len(p2p->vendor_elem[VENDOR_ELEM_P2P_ASSOC_REQ]);
2625
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002626 /*
2627 * (Re)Association Request - P2P IE
2628 * P2P Capability attribute (shall be present)
2629 * Extended Listen Timing (may be present)
2630 * P2P Device Info attribute (shall be present)
2631 */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002632 tmp = wpabuf_alloc(200 + extra);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002633 if (tmp == NULL)
2634 return -1;
2635
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002636#ifdef CONFIG_WIFI_DISPLAY
2637 if (p2p->wfd_ie_assoc_req)
2638 wpabuf_put_buf(tmp, p2p->wfd_ie_assoc_req);
2639#endif /* CONFIG_WIFI_DISPLAY */
2640
Dmitry Shmidt2e67f062014-07-16 09:55:28 -07002641 if (p2p->vendor_elem && p2p->vendor_elem[VENDOR_ELEM_P2P_ASSOC_REQ])
2642 wpabuf_put_buf(tmp,
2643 p2p->vendor_elem[VENDOR_ELEM_P2P_ASSOC_REQ]);
2644
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002645 peer = bssid ? p2p_get_device(p2p, bssid) : NULL;
2646
2647 lpos = p2p_buf_add_ie_hdr(tmp);
2648 p2p_buf_add_capability(tmp, p2p->dev_capab, 0);
2649 if (p2p->ext_listen_interval)
2650 p2p_buf_add_ext_listen_timing(tmp, p2p->ext_listen_period,
2651 p2p->ext_listen_interval);
2652 p2p_buf_add_device_info(tmp, p2p, peer);
2653 p2p_buf_update_ie_hdr(tmp, lpos);
2654
2655 tmplen = wpabuf_len(tmp);
2656 if (tmplen > len)
2657 res = -1;
2658 else {
2659 os_memcpy(buf, wpabuf_head(tmp), tmplen);
2660 res = tmplen;
2661 }
2662 wpabuf_free(tmp);
2663
2664 return res;
2665}
2666
2667
2668int p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf, char *end)
2669{
2670 struct wpabuf *p2p_ie;
2671 int ret;
2672
2673 p2p_ie = ieee802_11_vendor_ie_concat(ies, ies_len, P2P_IE_VENDOR_TYPE);
2674 if (p2p_ie == NULL)
2675 return 0;
2676
2677 ret = p2p_attr_text(p2p_ie, buf, end);
2678 wpabuf_free(p2p_ie);
2679 return ret;
2680}
2681
2682
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002683struct p2ps_advertisement *
2684p2p_service_p2ps_id(struct p2p_data *p2p, u32 adv_id)
2685{
2686 struct p2ps_advertisement *adv_data;
2687
2688 if (!p2p)
2689 return NULL;
2690
2691 adv_data = p2p->p2ps_adv_list;
2692 while (adv_data) {
2693 if (adv_data->id == adv_id)
2694 return adv_data;
2695 adv_data = adv_data->next;
2696 }
2697
2698 return NULL;
2699}
2700
2701
2702int p2p_service_del_asp(struct p2p_data *p2p, u32 adv_id)
2703{
2704 struct p2ps_advertisement *adv_data;
2705 struct p2ps_advertisement **prior;
2706
2707 if (!p2p)
2708 return -1;
2709
2710 adv_data = p2p->p2ps_adv_list;
2711 prior = &p2p->p2ps_adv_list;
2712 while (adv_data) {
2713 if (adv_data->id == adv_id) {
2714 p2p_dbg(p2p, "Delete ASP adv_id=0x%x", adv_id);
2715 *prior = adv_data->next;
2716 os_free(adv_data);
2717 return 0;
2718 }
2719 prior = &adv_data->next;
2720 adv_data = adv_data->next;
2721 }
2722
2723 return -1;
2724}
2725
2726
2727int p2p_service_add_asp(struct p2p_data *p2p, int auto_accept, u32 adv_id,
2728 const char *adv_str, u8 svc_state, u16 config_methods,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002729 const char *svc_info, const u8 *cpt_priority)
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002730{
2731 struct p2ps_advertisement *adv_data, *tmp, **prev;
2732 u8 buf[P2PS_HASH_LEN];
2733 size_t adv_data_len, adv_len, info_len = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002734 int i;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002735
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002736 if (!p2p || !adv_str || !adv_str[0] || !cpt_priority)
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002737 return -1;
2738
2739 if (!(config_methods & p2p->cfg->config_methods)) {
2740 p2p_dbg(p2p, "Config methods not supported svc: 0x%x dev: 0x%x",
2741 config_methods, p2p->cfg->config_methods);
2742 return -1;
2743 }
2744
2745 if (!p2ps_gen_hash(p2p, adv_str, buf))
2746 return -1;
2747
2748 if (svc_info)
2749 info_len = os_strlen(svc_info);
2750 adv_len = os_strlen(adv_str);
2751 adv_data_len = sizeof(struct p2ps_advertisement) + adv_len + 1 +
2752 info_len + 1;
2753
2754 adv_data = os_zalloc(adv_data_len);
2755 if (!adv_data)
2756 return -1;
2757
2758 os_memcpy(adv_data->hash, buf, P2PS_HASH_LEN);
2759 adv_data->id = adv_id;
2760 adv_data->state = svc_state;
2761 adv_data->config_methods = config_methods & p2p->cfg->config_methods;
2762 adv_data->auto_accept = (u8) auto_accept;
2763 os_memcpy(adv_data->svc_name, adv_str, adv_len);
2764
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002765 for (i = 0; cpt_priority[i] && i < P2PS_FEATURE_CAPAB_CPT_MAX; i++) {
2766 adv_data->cpt_priority[i] = cpt_priority[i];
2767 adv_data->cpt_mask |= cpt_priority[i];
2768 }
2769
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002770 if (svc_info && info_len) {
2771 adv_data->svc_info = &adv_data->svc_name[adv_len + 1];
2772 os_memcpy(adv_data->svc_info, svc_info, info_len);
2773 }
2774
2775 /*
2776 * Group Advertisements by service string. They do not need to be
2777 * sorted, but groups allow easier Probe Response instance grouping
2778 */
2779 tmp = p2p->p2ps_adv_list;
2780 prev = &p2p->p2ps_adv_list;
2781 while (tmp) {
2782 if (tmp->id == adv_data->id) {
2783 if (os_strcmp(tmp->svc_name, adv_data->svc_name) != 0) {
2784 os_free(adv_data);
2785 return -1;
2786 }
2787 adv_data->next = tmp->next;
2788 *prev = adv_data;
2789 os_free(tmp);
2790 goto inserted;
2791 } else {
2792 if (os_strcmp(tmp->svc_name, adv_data->svc_name) == 0) {
2793 adv_data->next = tmp->next;
2794 tmp->next = adv_data;
2795 goto inserted;
2796 }
2797 }
2798 prev = &tmp->next;
2799 tmp = tmp->next;
2800 }
2801
2802 /* No svc_name match found */
2803 adv_data->next = p2p->p2ps_adv_list;
2804 p2p->p2ps_adv_list = adv_data;
2805
2806inserted:
2807 p2p_dbg(p2p,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002808 "Added ASP advertisement adv_id=0x%x config_methods=0x%x svc_state=0x%x adv_str='%s' cpt_mask=0x%x",
2809 adv_id, adv_data->config_methods, svc_state, adv_str,
2810 adv_data->cpt_mask);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002811
2812 return 0;
2813}
2814
2815
Dmitry Shmidtdda10c22015-03-24 16:05:01 -07002816void p2p_service_flush_asp(struct p2p_data *p2p)
2817{
2818 struct p2ps_advertisement *adv, *prev;
2819
2820 if (!p2p)
2821 return;
2822
2823 adv = p2p->p2ps_adv_list;
2824 while (adv) {
2825 prev = adv;
2826 adv = adv->next;
2827 os_free(prev);
2828 }
2829
2830 p2p->p2ps_adv_list = NULL;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002831 p2ps_prov_free(p2p);
Dmitry Shmidtdda10c22015-03-24 16:05:01 -07002832 p2p_dbg(p2p, "All ASP advertisements flushed");
2833}
2834
2835
Dmitry Shmidt04949592012-07-19 12:16:46 -07002836int p2p_parse_dev_addr_in_p2p_ie(struct wpabuf *p2p_ie, u8 *dev_addr)
2837{
2838 struct p2p_message msg;
2839
2840 os_memset(&msg, 0, sizeof(msg));
2841 if (p2p_parse_p2p_ie(p2p_ie, &msg))
2842 return -1;
2843
2844 if (msg.p2p_device_addr) {
2845 os_memcpy(dev_addr, msg.p2p_device_addr, ETH_ALEN);
2846 return 0;
2847 } else if (msg.device_id) {
2848 os_memcpy(dev_addr, msg.device_id, ETH_ALEN);
2849 return 0;
2850 }
2851 return -1;
2852}
2853
2854
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002855int p2p_parse_dev_addr(const u8 *ies, size_t ies_len, u8 *dev_addr)
2856{
2857 struct wpabuf *p2p_ie;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002858 int ret;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002859
2860 p2p_ie = ieee802_11_vendor_ie_concat(ies, ies_len,
2861 P2P_IE_VENDOR_TYPE);
2862 if (p2p_ie == NULL)
2863 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002864 ret = p2p_parse_dev_addr_in_p2p_ie(p2p_ie, dev_addr);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002865 wpabuf_free(p2p_ie);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002866 return ret;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002867}
2868
2869
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002870static void p2p_clear_go_neg(struct p2p_data *p2p)
2871{
2872 p2p->go_neg_peer = NULL;
2873 p2p_clear_timeout(p2p);
2874 p2p_set_state(p2p, P2P_IDLE);
2875}
2876
2877
2878void p2p_wps_success_cb(struct p2p_data *p2p, const u8 *mac_addr)
2879{
2880 if (p2p->go_neg_peer == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002881 p2p_dbg(p2p, "No pending Group Formation - ignore WPS registration success notification");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002882 return; /* No pending Group Formation */
2883 }
2884
2885 if (os_memcmp(mac_addr, p2p->go_neg_peer->intended_addr, ETH_ALEN) !=
2886 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002887 p2p_dbg(p2p, "Ignore WPS registration success notification for "
2888 MACSTR " (GO Negotiation peer " MACSTR ")",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002889 MAC2STR(mac_addr),
2890 MAC2STR(p2p->go_neg_peer->intended_addr));
2891 return; /* Ignore unexpected peer address */
2892 }
2893
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002894 p2p_dbg(p2p, "Group Formation completed successfully with " MACSTR,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002895 MAC2STR(mac_addr));
2896
2897 p2p_clear_go_neg(p2p);
2898}
2899
2900
2901void p2p_group_formation_failed(struct p2p_data *p2p)
2902{
2903 if (p2p->go_neg_peer == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002904 p2p_dbg(p2p, "No pending Group Formation - ignore group formation failure notification");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002905 return; /* No pending Group Formation */
2906 }
2907
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002908 p2p_dbg(p2p, "Group Formation failed with " MACSTR,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002909 MAC2STR(p2p->go_neg_peer->intended_addr));
2910
2911 p2p_clear_go_neg(p2p);
2912}
2913
2914
2915struct p2p_data * p2p_init(const struct p2p_config *cfg)
2916{
2917 struct p2p_data *p2p;
2918
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07002919 if (cfg->max_peers < 1 ||
2920 cfg->passphrase_len < 8 || cfg->passphrase_len > 63)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002921 return NULL;
2922
2923 p2p = os_zalloc(sizeof(*p2p) + sizeof(*cfg));
2924 if (p2p == NULL)
2925 return NULL;
2926 p2p->cfg = (struct p2p_config *) (p2p + 1);
2927 os_memcpy(p2p->cfg, cfg, sizeof(*cfg));
2928 if (cfg->dev_name)
2929 p2p->cfg->dev_name = os_strdup(cfg->dev_name);
2930 if (cfg->manufacturer)
2931 p2p->cfg->manufacturer = os_strdup(cfg->manufacturer);
2932 if (cfg->model_name)
2933 p2p->cfg->model_name = os_strdup(cfg->model_name);
2934 if (cfg->model_number)
2935 p2p->cfg->model_number = os_strdup(cfg->model_number);
2936 if (cfg->serial_number)
2937 p2p->cfg->serial_number = os_strdup(cfg->serial_number);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002938 if (cfg->pref_chan) {
2939 p2p->cfg->pref_chan = os_malloc(cfg->num_pref_chan *
2940 sizeof(struct p2p_channel));
2941 if (p2p->cfg->pref_chan) {
2942 os_memcpy(p2p->cfg->pref_chan, cfg->pref_chan,
2943 cfg->num_pref_chan *
2944 sizeof(struct p2p_channel));
2945 } else
2946 p2p->cfg->num_pref_chan = 0;
2947 }
2948
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002949 p2ps_gen_hash(p2p, P2PS_WILD_HASH_STR, p2p->wild_card_hash);
2950
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002951 p2p->min_disc_int = 1;
2952 p2p->max_disc_int = 3;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002953 p2p->max_disc_tu = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002954
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07002955 if (os_get_random(&p2p->next_tie_breaker, 1) < 0)
2956 p2p->next_tie_breaker = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002957 p2p->next_tie_breaker &= 0x01;
2958 if (cfg->sd_request)
2959 p2p->dev_capab |= P2P_DEV_CAPAB_SERVICE_DISCOVERY;
2960 p2p->dev_capab |= P2P_DEV_CAPAB_INVITATION_PROCEDURE;
2961 if (cfg->concurrent_operations)
2962 p2p->dev_capab |= P2P_DEV_CAPAB_CONCURRENT_OPER;
2963 p2p->dev_capab |= P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
2964
2965 dl_list_init(&p2p->devices);
2966
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002967 p2p->go_timeout = 100;
2968 p2p->client_timeout = 20;
Dmitry Shmidt13ca8d82014-02-20 10:18:40 -08002969 p2p->num_p2p_sd_queries = 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002970
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002971 p2p_dbg(p2p, "initialized");
2972 p2p_channels_dump(p2p, "channels", &p2p->cfg->channels);
2973 p2p_channels_dump(p2p, "cli_channels", &p2p->cfg->cli_channels);
2974
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002975 return p2p;
2976}
2977
2978
2979void p2p_deinit(struct p2p_data *p2p)
2980{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002981#ifdef CONFIG_WIFI_DISPLAY
2982 wpabuf_free(p2p->wfd_ie_beacon);
2983 wpabuf_free(p2p->wfd_ie_probe_req);
2984 wpabuf_free(p2p->wfd_ie_probe_resp);
2985 wpabuf_free(p2p->wfd_ie_assoc_req);
2986 wpabuf_free(p2p->wfd_ie_invitation);
2987 wpabuf_free(p2p->wfd_ie_prov_disc_req);
2988 wpabuf_free(p2p->wfd_ie_prov_disc_resp);
2989 wpabuf_free(p2p->wfd_ie_go_neg);
2990 wpabuf_free(p2p->wfd_dev_info);
2991 wpabuf_free(p2p->wfd_assoc_bssid);
2992 wpabuf_free(p2p->wfd_coupled_sink_info);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002993 wpabuf_free(p2p->wfd_r2_dev_info);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002994#endif /* CONFIG_WIFI_DISPLAY */
2995
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002996 eloop_cancel_timeout(p2p_scan_timeout, p2p, NULL);
Dmitry Shmidt9cdf1b92013-02-27 12:58:50 -08002997 eloop_cancel_timeout(p2p_go_neg_start, p2p, NULL);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002998 eloop_cancel_timeout(p2p_go_neg_wait_timeout, p2p, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002999 p2p_flush(p2p);
3000 p2p_free_req_dev_types(p2p);
3001 os_free(p2p->cfg->dev_name);
3002 os_free(p2p->cfg->manufacturer);
3003 os_free(p2p->cfg->model_name);
3004 os_free(p2p->cfg->model_number);
3005 os_free(p2p->cfg->serial_number);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003006 os_free(p2p->cfg->pref_chan);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003007 os_free(p2p->groups);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07003008 p2ps_prov_free(p2p);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003009 wpabuf_free(p2p->sd_resp);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003010 p2p_remove_wps_vendor_extensions(p2p);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003011 os_free(p2p->no_go_freq.range);
Dmitry Shmidtdda10c22015-03-24 16:05:01 -07003012 p2p_service_flush_asp(p2p);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003013
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003014 os_free(p2p);
3015}
3016
3017
3018void p2p_flush(struct p2p_data *p2p)
3019{
3020 struct p2p_device *dev, *prev;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003021
3022 p2p_ext_listen(p2p, 0, 0);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003023 p2p_stop_find(p2p);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003024 dl_list_for_each_safe(dev, prev, &p2p->devices, struct p2p_device,
3025 list) {
3026 dl_list_del(&dev->list);
3027 p2p_device_free(p2p, dev);
3028 }
3029 p2p_free_sd_queries(p2p);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003030 p2p->ssid_set = 0;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003031 p2ps_prov_free(p2p);
3032 p2p_reset_pending_pd(p2p);
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08003033 p2p->override_pref_op_class = 0;
3034 p2p->override_pref_channel = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003035}
3036
3037
3038int p2p_unauthorize(struct p2p_data *p2p, const u8 *addr)
3039{
3040 struct p2p_device *dev;
3041
3042 dev = p2p_get_device(p2p, addr);
3043 if (dev == NULL)
3044 return -1;
3045
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003046 p2p_dbg(p2p, "Unauthorizing " MACSTR, MAC2STR(addr));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003047
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003048 if (p2p->go_neg_peer == dev) {
3049 eloop_cancel_timeout(p2p_go_neg_wait_timeout, p2p, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003050 p2p->go_neg_peer = NULL;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003051 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003052
3053 dev->wps_method = WPS_NOT_READY;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003054 dev->oob_pw_id = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003055 dev->flags &= ~P2P_DEV_WAIT_GO_NEG_RESPONSE;
3056 dev->flags &= ~P2P_DEV_WAIT_GO_NEG_CONFIRM;
3057
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003058 return 0;
3059}
3060
3061
3062int p2p_set_dev_name(struct p2p_data *p2p, const char *dev_name)
3063{
3064 os_free(p2p->cfg->dev_name);
3065 if (dev_name) {
3066 p2p->cfg->dev_name = os_strdup(dev_name);
3067 if (p2p->cfg->dev_name == NULL)
3068 return -1;
3069 } else
3070 p2p->cfg->dev_name = NULL;
3071 return 0;
3072}
3073
3074
3075int p2p_set_manufacturer(struct p2p_data *p2p, const char *manufacturer)
3076{
3077 os_free(p2p->cfg->manufacturer);
3078 p2p->cfg->manufacturer = NULL;
3079 if (manufacturer) {
3080 p2p->cfg->manufacturer = os_strdup(manufacturer);
3081 if (p2p->cfg->manufacturer == NULL)
3082 return -1;
3083 }
3084
3085 return 0;
3086}
3087
3088
3089int p2p_set_model_name(struct p2p_data *p2p, const char *model_name)
3090{
3091 os_free(p2p->cfg->model_name);
3092 p2p->cfg->model_name = NULL;
3093 if (model_name) {
3094 p2p->cfg->model_name = os_strdup(model_name);
3095 if (p2p->cfg->model_name == NULL)
3096 return -1;
3097 }
3098
3099 return 0;
3100}
3101
3102
3103int p2p_set_model_number(struct p2p_data *p2p, const char *model_number)
3104{
3105 os_free(p2p->cfg->model_number);
3106 p2p->cfg->model_number = NULL;
3107 if (model_number) {
3108 p2p->cfg->model_number = os_strdup(model_number);
3109 if (p2p->cfg->model_number == NULL)
3110 return -1;
3111 }
3112
3113 return 0;
3114}
3115
3116
3117int p2p_set_serial_number(struct p2p_data *p2p, const char *serial_number)
3118{
3119 os_free(p2p->cfg->serial_number);
3120 p2p->cfg->serial_number = NULL;
3121 if (serial_number) {
3122 p2p->cfg->serial_number = os_strdup(serial_number);
3123 if (p2p->cfg->serial_number == NULL)
3124 return -1;
3125 }
3126
3127 return 0;
3128}
3129
3130
3131void p2p_set_config_methods(struct p2p_data *p2p, u16 config_methods)
3132{
3133 p2p->cfg->config_methods = config_methods;
3134}
3135
3136
3137void p2p_set_uuid(struct p2p_data *p2p, const u8 *uuid)
3138{
3139 os_memcpy(p2p->cfg->uuid, uuid, 16);
3140}
3141
3142
3143int p2p_set_pri_dev_type(struct p2p_data *p2p, const u8 *pri_dev_type)
3144{
3145 os_memcpy(p2p->cfg->pri_dev_type, pri_dev_type, 8);
3146 return 0;
3147}
3148
3149
3150int p2p_set_sec_dev_types(struct p2p_data *p2p, const u8 dev_types[][8],
3151 size_t num_dev_types)
3152{
3153 if (num_dev_types > P2P_SEC_DEVICE_TYPES)
3154 num_dev_types = P2P_SEC_DEVICE_TYPES;
3155 p2p->cfg->num_sec_dev_types = num_dev_types;
3156 os_memcpy(p2p->cfg->sec_dev_type, dev_types, num_dev_types * 8);
3157 return 0;
3158}
3159
3160
3161void p2p_remove_wps_vendor_extensions(struct p2p_data *p2p)
3162{
3163 int i;
3164
3165 for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) {
3166 wpabuf_free(p2p->wps_vendor_ext[i]);
3167 p2p->wps_vendor_ext[i] = NULL;
3168 }
3169}
3170
3171
3172int p2p_add_wps_vendor_extension(struct p2p_data *p2p,
3173 const struct wpabuf *vendor_ext)
3174{
3175 int i;
3176
3177 if (vendor_ext == NULL)
3178 return -1;
3179
3180 for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) {
3181 if (p2p->wps_vendor_ext[i] == NULL)
3182 break;
3183 }
3184 if (i >= P2P_MAX_WPS_VENDOR_EXT)
3185 return -1;
3186
3187 p2p->wps_vendor_ext[i] = wpabuf_dup(vendor_ext);
3188 if (p2p->wps_vendor_ext[i] == NULL)
3189 return -1;
3190
3191 return 0;
3192}
3193
3194
3195int p2p_set_country(struct p2p_data *p2p, const char *country)
3196{
3197 os_memcpy(p2p->cfg->country, country, 3);
3198 return 0;
3199}
3200
3201
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003202static int p2p_pre_find_operation(struct p2p_data *p2p, struct p2p_device *dev)
3203{
Dmitry Shmidtde47be72016-01-07 12:52:55 -08003204 int res;
3205
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003206 if (dev->sd_pending_bcast_queries == 0) {
3207 /* Initialize with total number of registered broadcast
3208 * SD queries. */
3209 dev->sd_pending_bcast_queries = p2p->num_p2p_sd_queries;
3210 }
3211
Dmitry Shmidtde47be72016-01-07 12:52:55 -08003212 res = p2p_start_sd(p2p, dev);
3213 if (res == -2)
3214 return -2;
3215 if (res == 0)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003216 return 1;
3217
3218 if (dev->req_config_methods &&
3219 !(dev->flags & P2P_DEV_PD_FOR_JOIN)) {
3220 p2p_dbg(p2p, "Send pending Provision Discovery Request to "
3221 MACSTR " (config methods 0x%x)",
3222 MAC2STR(dev->info.p2p_device_addr),
3223 dev->req_config_methods);
3224 if (p2p_send_prov_disc_req(p2p, dev, 0, 0) == 0)
3225 return 1;
3226 }
3227
3228 return 0;
3229}
3230
3231
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003232void p2p_continue_find(struct p2p_data *p2p)
3233{
3234 struct p2p_device *dev;
Dmitry Shmidtde47be72016-01-07 12:52:55 -08003235 int found, res;
Dmitry Shmidt13ca8d82014-02-20 10:18:40 -08003236
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003237 p2p_set_state(p2p, P2P_SEARCH);
3238
3239 /* Continue from the device following the last iteration */
3240 found = 0;
3241 dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
3242 if (dev == p2p->last_p2p_find_oper) {
3243 found = 1;
3244 continue;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003245 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003246 if (!found)
3247 continue;
Dmitry Shmidtde47be72016-01-07 12:52:55 -08003248 res = p2p_pre_find_operation(p2p, dev);
3249 if (res > 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003250 p2p->last_p2p_find_oper = dev;
3251 return;
3252 }
Dmitry Shmidtde47be72016-01-07 12:52:55 -08003253 if (res == -2)
3254 goto skip_sd;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003255 }
3256
3257 /*
3258 * Wrap around to the beginning of the list and continue until the last
3259 * iteration device.
3260 */
3261 dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
Dmitry Shmidtde47be72016-01-07 12:52:55 -08003262 res = p2p_pre_find_operation(p2p, dev);
3263 if (res > 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003264 p2p->last_p2p_find_oper = dev;
3265 return;
3266 }
Dmitry Shmidtde47be72016-01-07 12:52:55 -08003267 if (res == -2)
3268 goto skip_sd;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003269 if (dev == p2p->last_p2p_find_oper)
3270 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003271 }
3272
Dmitry Shmidtde47be72016-01-07 12:52:55 -08003273skip_sd:
3274 os_memset(p2p->sd_query_no_ack, 0, ETH_ALEN);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003275 p2p_listen_in_find(p2p, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003276}
3277
3278
3279static void p2p_sd_cb(struct p2p_data *p2p, int success)
3280{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003281 p2p_dbg(p2p, "Service Discovery Query TX callback: success=%d",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003282 success);
3283 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
3284
3285 if (!success) {
Dmitry Shmidtde47be72016-01-07 12:52:55 -08003286 if (p2p->sd_peer) {
3287 if (is_zero_ether_addr(p2p->sd_query_no_ack)) {
3288 os_memcpy(p2p->sd_query_no_ack,
3289 p2p->sd_peer->info.p2p_device_addr,
3290 ETH_ALEN);
3291 p2p_dbg(p2p,
3292 "First SD Query no-ACK in this search iteration: "
3293 MACSTR, MAC2STR(p2p->sd_query_no_ack));
3294 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003295 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
Dmitry Shmidtde47be72016-01-07 12:52:55 -08003296 }
Dmitry Shmidt13ca8d82014-02-20 10:18:40 -08003297 p2p->sd_peer = NULL;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003298 if (p2p->state != P2P_IDLE)
3299 p2p_continue_find(p2p);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003300 return;
3301 }
3302
3303 if (p2p->sd_peer == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003304 p2p_dbg(p2p, "No SD peer entry known");
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003305 if (p2p->state != P2P_IDLE)
3306 p2p_continue_find(p2p);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003307 return;
3308 }
3309
Dmitry Shmidt807291d2015-01-27 13:40:23 -08003310 if (p2p->sd_query && p2p->sd_query->for_all_peers) {
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07003311 /* Update the pending broadcast SD query count for this device
3312 */
3313 p2p->sd_peer->sd_pending_bcast_queries--;
3314
3315 /*
3316 * If there are no pending broadcast queries for this device,
3317 * mark it as done (-1).
3318 */
3319 if (p2p->sd_peer->sd_pending_bcast_queries == 0)
3320 p2p->sd_peer->sd_pending_bcast_queries = -1;
3321 }
3322
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003323 /* Wait for response from the peer */
3324 p2p_set_state(p2p, P2P_SD_DURING_FIND);
3325 p2p_set_timeout(p2p, 0, 200000);
3326}
3327
Jouni Malinen75ecf522011-06-27 15:19:46 -07003328
3329/**
3330 * p2p_retry_pd - Retry any pending provision disc requests in IDLE state
3331 * @p2p: P2P module context from p2p_init()
3332 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003333static void p2p_retry_pd(struct p2p_data *p2p)
Jouni Malinen75ecf522011-06-27 15:19:46 -07003334{
3335 struct p2p_device *dev;
3336
Jouni Malinen75ecf522011-06-27 15:19:46 -07003337 /*
3338 * Retry the prov disc req attempt only for the peer that the user had
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003339 * requested.
Jouni Malinen75ecf522011-06-27 15:19:46 -07003340 */
3341
3342 dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
3343 if (os_memcmp(p2p->pending_pd_devaddr,
3344 dev->info.p2p_device_addr, ETH_ALEN) != 0)
3345 continue;
3346 if (!dev->req_config_methods)
3347 continue;
Jouni Malinen75ecf522011-06-27 15:19:46 -07003348
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003349 p2p_dbg(p2p, "Send pending Provision Discovery Request to "
Jouni Malinen75ecf522011-06-27 15:19:46 -07003350 MACSTR " (config methods 0x%x)",
3351 MAC2STR(dev->info.p2p_device_addr),
3352 dev->req_config_methods);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003353 p2p_send_prov_disc_req(p2p, dev,
Dmitry Shmidt051af732013-10-22 13:52:46 -07003354 dev->flags & P2P_DEV_PD_FOR_JOIN,
3355 p2p->pd_force_freq);
Jouni Malinen75ecf522011-06-27 15:19:46 -07003356 return;
3357 }
3358}
3359
3360
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003361static void p2p_prov_disc_cb(struct p2p_data *p2p, int success)
3362{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003363 p2p_dbg(p2p, "Provision Discovery Request TX callback: success=%d",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003364 success);
Jouni Malinen75ecf522011-06-27 15:19:46 -07003365
3366 /*
3367 * Postpone resetting the pending action state till after we actually
3368 * time out. This allows us to take some action like notifying any
3369 * interested parties about no response to the request.
3370 *
3371 * When the timer (below) goes off we check in IDLE, SEARCH, or
3372 * LISTEN_ONLY state, which are the only allowed states to issue a PD
3373 * requests in, if this was still pending and then raise notification.
3374 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003375
3376 if (!success) {
Jouni Malinen75ecf522011-06-27 15:19:46 -07003377 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
3378
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003379 if (p2p->user_initiated_pd &&
3380 (p2p->state == P2P_SEARCH || p2p->state == P2P_LISTEN_ONLY))
3381 {
3382 /* Retry request from timeout to avoid busy loops */
3383 p2p->pending_action_state = P2P_PENDING_PD;
3384 p2p_set_timeout(p2p, 0, 50000);
3385 } else if (p2p->state != P2P_IDLE)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003386 p2p_continue_find(p2p);
Jouni Malinen75ecf522011-06-27 15:19:46 -07003387 else if (p2p->user_initiated_pd) {
3388 p2p->pending_action_state = P2P_PENDING_PD;
3389 p2p_set_timeout(p2p, 0, 300000);
3390 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003391 return;
3392 }
3393
Jouni Malinen75ecf522011-06-27 15:19:46 -07003394 /*
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003395 * If after PD Request the peer doesn't expect to receive PD Response
3396 * the PD Request ACK indicates a completion of the current PD. This
3397 * happens only on the advertiser side sending the follow-on PD Request
3398 * with the status different than 12 (Success: accepted by user).
3399 */
3400 if (p2p->p2ps_prov && !p2p->p2ps_prov->pd_seeker &&
3401 p2p->p2ps_prov->status != P2P_SC_SUCCESS_DEFERRED) {
3402 p2p_dbg(p2p, "P2PS PD completion on Follow-on PD Request ACK");
3403
3404 if (p2p->send_action_in_progress) {
3405 p2p->send_action_in_progress = 0;
3406 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
3407 }
3408
3409 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
3410
3411 if (p2p->cfg->p2ps_prov_complete) {
3412 p2p->cfg->p2ps_prov_complete(
3413 p2p->cfg->cb_ctx,
3414 p2p->p2ps_prov->status,
3415 p2p->p2ps_prov->adv_mac,
3416 p2p->p2ps_prov->adv_mac,
3417 p2p->p2ps_prov->session_mac,
3418 NULL, p2p->p2ps_prov->adv_id,
3419 p2p->p2ps_prov->session_id,
3420 0, 0, NULL, 0, 0, 0,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08003421 NULL, NULL, 0, 0, NULL, 0);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003422 }
3423
3424 if (p2p->user_initiated_pd)
3425 p2p_reset_pending_pd(p2p);
3426
3427 p2ps_prov_free(p2p);
3428 return;
3429 }
3430
3431 /*
Jouni Malinen75ecf522011-06-27 15:19:46 -07003432 * This postponing, of resetting pending_action_state, needs to be
3433 * done only for user initiated PD requests and not internal ones.
3434 */
3435 if (p2p->user_initiated_pd)
3436 p2p->pending_action_state = P2P_PENDING_PD;
3437 else
3438 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
3439
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003440 /* Wait for response from the peer */
3441 if (p2p->state == P2P_SEARCH)
3442 p2p_set_state(p2p, P2P_PD_DURING_FIND);
3443 p2p_set_timeout(p2p, 0, 200000);
3444}
3445
3446
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003447static void p2p_prov_disc_resp_cb(struct p2p_data *p2p, int success)
3448{
3449 p2p_dbg(p2p, "Provision Discovery Response TX callback: success=%d",
3450 success);
3451
3452 if (p2p->send_action_in_progress) {
3453 p2p->send_action_in_progress = 0;
3454 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
3455 }
3456
3457 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
3458
Jimmy Chen5ef7aad2019-10-15 15:45:26 +08003459 if (!success) {
3460 if (p2p->state == P2P_SEARCH)
3461 p2p_continue_find(p2p);
Hai Shalom81f62d82019-07-22 12:10:00 -07003462 return;
Jimmy Chen5ef7aad2019-10-15 15:45:26 +08003463 }
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003464
3465 if (!p2p->cfg->prov_disc_resp_cb ||
Jimmy Chen5ef7aad2019-10-15 15:45:26 +08003466 p2p->cfg->prov_disc_resp_cb(p2p->cfg->cb_ctx) < 1) {
3467 if (p2p->state == P2P_SEARCH)
3468 p2p_continue_find(p2p);
Hai Shalom81f62d82019-07-22 12:10:00 -07003469 return;
Jimmy Chen5ef7aad2019-10-15 15:45:26 +08003470 }
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003471
3472 p2p_dbg(p2p,
3473 "Post-Provision Discovery operations started - do not try to continue other P2P operations");
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003474}
3475
3476
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003477int p2p_scan_res_handler(struct p2p_data *p2p, const u8 *bssid, int freq,
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003478 struct os_reltime *rx_time, int level, const u8 *ies,
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003479 size_t ies_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003480{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003481 if (os_reltime_before(rx_time, &p2p->find_start)) {
Dmitry Shmidtf8623282013-02-20 14:34:59 -08003482 /*
3483 * The driver may have cached (e.g., in cfg80211 BSS table) the
3484 * scan results for relatively long time. To avoid reporting
3485 * stale information, update P2P peers only based on results
3486 * that have based on frames received after the last p2p_find
3487 * operation was started.
3488 */
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003489 p2p_dbg(p2p, "Ignore old scan result for " MACSTR
Dmitry Shmidtde47be72016-01-07 12:52:55 -08003490 " (rx_time=%u.%06u find_start=%u.%06u)",
Dmitry Shmidtf8623282013-02-20 14:34:59 -08003491 MAC2STR(bssid), (unsigned int) rx_time->sec,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08003492 (unsigned int) rx_time->usec,
3493 (unsigned int) p2p->find_start.sec,
3494 (unsigned int) p2p->find_start.usec);
Dmitry Shmidtf8623282013-02-20 14:34:59 -08003495 return 0;
3496 }
3497
3498 p2p_add_device(p2p, bssid, freq, rx_time, level, ies, ies_len, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003499
3500 return 0;
3501}
3502
3503
3504void p2p_scan_res_handled(struct p2p_data *p2p)
3505{
3506 if (!p2p->p2p_scan_running) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003507 p2p_dbg(p2p, "p2p_scan was not running, but scan results received");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003508 }
3509 p2p->p2p_scan_running = 0;
3510 eloop_cancel_timeout(p2p_scan_timeout, p2p, NULL);
3511
3512 if (p2p_run_after_scan(p2p))
3513 return;
3514 if (p2p->state == P2P_SEARCH)
3515 p2p_continue_find(p2p);
3516}
3517
3518
Dmitry Shmidt9c175262016-03-03 10:20:07 -08003519void p2p_scan_ie(struct p2p_data *p2p, struct wpabuf *ies, const u8 *dev_id,
3520 unsigned int bands)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003521{
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003522 u8 dev_capab;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003523 u8 *len;
3524
3525#ifdef CONFIG_WIFI_DISPLAY
3526 if (p2p->wfd_ie_probe_req)
3527 wpabuf_put_buf(ies, p2p->wfd_ie_probe_req);
3528#endif /* CONFIG_WIFI_DISPLAY */
3529
Dmitry Shmidt2e67f062014-07-16 09:55:28 -07003530 if (p2p->vendor_elem && p2p->vendor_elem[VENDOR_ELEM_PROBE_REQ_P2P])
3531 wpabuf_put_buf(ies,
3532 p2p->vendor_elem[VENDOR_ELEM_PROBE_REQ_P2P]);
3533
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003534 len = p2p_buf_add_ie_hdr(ies);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003535
3536 dev_capab = p2p->dev_capab & ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
3537
3538 /* P2PS requires Probe Request frames to include SD bit */
3539 if (p2p->p2ps_seek && p2p->p2ps_seek_count)
3540 dev_capab |= P2P_DEV_CAPAB_SERVICE_DISCOVERY;
3541
3542 p2p_buf_add_capability(ies, dev_capab, 0);
3543
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003544 if (dev_id)
3545 p2p_buf_add_device_id(ies, dev_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003546 if (p2p->cfg->reg_class && p2p->cfg->channel)
3547 p2p_buf_add_listen_channel(ies, p2p->cfg->country,
3548 p2p->cfg->reg_class,
3549 p2p->cfg->channel);
3550 if (p2p->ext_listen_interval)
3551 p2p_buf_add_ext_listen_timing(ies, p2p->ext_listen_period,
3552 p2p->ext_listen_interval);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003553
Dmitry Shmidt9c175262016-03-03 10:20:07 -08003554 if (bands & BAND_60_GHZ)
3555 p2p_buf_add_device_info(ies, p2p, NULL);
3556
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003557 if (p2p->p2ps_seek && p2p->p2ps_seek_count)
3558 p2p_buf_add_service_hash(ies, p2p);
3559
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003560 /* TODO: p2p_buf_add_operating_channel() if GO */
3561 p2p_buf_update_ie_hdr(ies, len);
3562}
3563
3564
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003565size_t p2p_scan_ie_buf_len(struct p2p_data *p2p)
3566{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003567 size_t len = 100;
3568
3569#ifdef CONFIG_WIFI_DISPLAY
3570 if (p2p && p2p->wfd_ie_probe_req)
3571 len += wpabuf_len(p2p->wfd_ie_probe_req);
3572#endif /* CONFIG_WIFI_DISPLAY */
3573
Dmitry Shmidt2e67f062014-07-16 09:55:28 -07003574 if (p2p && p2p->vendor_elem &&
3575 p2p->vendor_elem[VENDOR_ELEM_PROBE_REQ_P2P])
3576 len += wpabuf_len(p2p->vendor_elem[VENDOR_ELEM_PROBE_REQ_P2P]);
3577
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003578 return len;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003579}
3580
3581
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003582int p2p_ie_text(struct wpabuf *p2p_ie, char *buf, char *end)
3583{
3584 return p2p_attr_text(p2p_ie, buf, end);
3585}
3586
3587
3588static void p2p_go_neg_req_cb(struct p2p_data *p2p, int success)
3589{
3590 struct p2p_device *dev = p2p->go_neg_peer;
Dmitry Shmidt8c652892013-03-01 10:14:01 -08003591 int timeout;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003592
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003593 p2p_dbg(p2p, "GO Negotiation Request TX callback: success=%d", success);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003594
3595 if (dev == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003596 p2p_dbg(p2p, "No pending GO Negotiation");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003597 return;
3598 }
3599
3600 if (success) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003601 if (dev->flags & P2P_DEV_USER_REJECTED) {
3602 p2p_set_state(p2p, P2P_IDLE);
3603 return;
3604 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07003605 } else if (dev->go_neg_req_sent) {
3606 /* Cancel the increment from p2p_connect_send() on failure */
Dmitry Shmidt98f9e762012-05-30 11:18:46 -07003607 dev->go_neg_req_sent--;
3608 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003609
3610 if (!success &&
3611 (dev->info.dev_capab & P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY) &&
3612 !is_zero_ether_addr(dev->member_in_go_dev)) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003613 p2p_dbg(p2p, "Peer " MACSTR " did not acknowledge request - try to use device discoverability through its GO",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003614 MAC2STR(dev->info.p2p_device_addr));
3615 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
3616 p2p_send_dev_disc_req(p2p, dev);
3617 return;
3618 }
3619
3620 /*
3621 * Use P2P find, if needed, to find the other device from its listen
3622 * channel.
3623 */
3624 p2p_set_state(p2p, P2P_CONNECT);
Dmitry Shmidt8c652892013-03-01 10:14:01 -08003625 timeout = success ? 500000 : 100000;
3626 if (!success && p2p->go_neg_peer &&
3627 (p2p->go_neg_peer->flags & P2P_DEV_PEER_WAITING_RESPONSE)) {
3628 unsigned int r;
3629 /*
3630 * Peer is expected to wait our response and we will skip the
3631 * listen phase. Add some randomness to the wait time here to
3632 * make it less likely to hit cases where we could end up in
3633 * sync with peer not listening.
3634 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07003635 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
3636 r = 0;
Dmitry Shmidt8c652892013-03-01 10:14:01 -08003637 timeout += r % 100000;
3638 }
3639 p2p_set_timeout(p2p, 0, timeout);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003640}
3641
3642
3643static void p2p_go_neg_resp_cb(struct p2p_data *p2p, int success)
3644{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003645 p2p_dbg(p2p, "GO Negotiation Response TX callback: success=%d",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003646 success);
3647 if (!p2p->go_neg_peer && p2p->state == P2P_PROVISIONING) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003648 p2p_dbg(p2p, "Ignore TX callback event - GO Negotiation is not running anymore");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003649 return;
3650 }
3651 p2p_set_state(p2p, P2P_CONNECT);
Dmitry Shmidt8c652892013-03-01 10:14:01 -08003652 p2p_set_timeout(p2p, 0, 500000);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003653}
3654
3655
Dmitry Shmidt4b060592013-04-29 16:42:49 -07003656static void p2p_go_neg_resp_failure_cb(struct p2p_data *p2p, int success,
3657 const u8 *addr)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003658{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003659 p2p_dbg(p2p, "GO Negotiation Response (failure) TX callback: success=%d", success);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003660 if (p2p->go_neg_peer && p2p->go_neg_peer->status != P2P_SC_SUCCESS) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003661 p2p_go_neg_failed(p2p, p2p->go_neg_peer->status);
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08003662 return;
3663 }
3664
3665 if (success) {
Dmitry Shmidt4b060592013-04-29 16:42:49 -07003666 struct p2p_device *dev;
3667 dev = p2p_get_device(p2p, addr);
3668 if (dev &&
Dmitry Shmidt34c12022015-03-05 14:11:20 -08003669 dev->status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE)
Dmitry Shmidt4b060592013-04-29 16:42:49 -07003670 dev->flags |= P2P_DEV_PEER_WAITING_RESPONSE;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003671 }
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08003672
3673 if (p2p->state == P2P_SEARCH || p2p->state == P2P_SD_DURING_FIND)
3674 p2p_continue_find(p2p);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003675}
3676
3677
3678static void p2p_go_neg_conf_cb(struct p2p_data *p2p,
3679 enum p2p_send_action_result result)
3680{
3681 struct p2p_device *dev;
3682
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003683 p2p_dbg(p2p, "GO Negotiation Confirm TX callback: result=%d", result);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003684 if (result == P2P_SEND_ACTION_FAILED) {
Dmitry Shmidt413dde72014-04-11 10:23:22 -07003685 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003686 p2p_go_neg_failed(p2p, -1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003687 return;
3688 }
Dmitry Shmidt413dde72014-04-11 10:23:22 -07003689
3690 dev = p2p->go_neg_peer;
3691
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003692 if (result == P2P_SEND_ACTION_NO_ACK) {
3693 /*
Dmitry Shmidt413dde72014-04-11 10:23:22 -07003694 * Retry GO Negotiation Confirmation
3695 * P2P_GO_NEG_CNF_MAX_RETRY_COUNT times if we did not receive
3696 * ACK for confirmation.
3697 */
3698 if (dev && dev->go_neg_conf &&
3699 dev->go_neg_conf_sent <= P2P_GO_NEG_CNF_MAX_RETRY_COUNT) {
3700 p2p_dbg(p2p, "GO Negotiation Confirm retry %d",
3701 dev->go_neg_conf_sent);
3702 p2p->pending_action_state = P2P_PENDING_GO_NEG_CONFIRM;
3703 if (p2p_send_action(p2p, dev->go_neg_conf_freq,
3704 dev->info.p2p_device_addr,
3705 p2p->cfg->dev_addr,
3706 dev->info.p2p_device_addr,
3707 wpabuf_head(dev->go_neg_conf),
3708 wpabuf_len(dev->go_neg_conf), 0) >=
3709 0) {
3710 dev->go_neg_conf_sent++;
3711 return;
3712 }
3713 p2p_dbg(p2p, "Failed to re-send Action frame");
3714
3715 /*
3716 * Continue with the assumption that the first attempt
3717 * went through and just the ACK frame was lost.
3718 */
3719 }
3720
3721 /*
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003722 * It looks like the TX status for GO Negotiation Confirm is
3723 * often showing failure even when the peer has actually
3724 * received the frame. Since the peer may change channels
3725 * immediately after having received the frame, we may not see
3726 * an Ack for retries, so just dropping a single frame may
3727 * trigger this. To allow the group formation to succeed if the
3728 * peer did indeed receive the frame, continue regardless of
3729 * the TX status.
3730 */
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003731 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 -07003732 }
3733
Dmitry Shmidt413dde72014-04-11 10:23:22 -07003734 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
3735
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003736 if (dev == NULL)
3737 return;
3738
3739 p2p_go_complete(p2p, dev);
3740}
3741
3742
3743void p2p_send_action_cb(struct p2p_data *p2p, unsigned int freq, const u8 *dst,
3744 const u8 *src, const u8 *bssid,
3745 enum p2p_send_action_result result)
3746{
3747 enum p2p_pending_action_state state;
3748 int success;
3749
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003750 p2p_dbg(p2p, "Action frame TX callback (state=%d freq=%u dst=" MACSTR
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003751 " src=" MACSTR " bssid=" MACSTR " result=%d p2p_state=%s)",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003752 p2p->pending_action_state, freq, MAC2STR(dst), MAC2STR(src),
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003753 MAC2STR(bssid), result, p2p_state_txt(p2p->state));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003754 success = result == P2P_SEND_ACTION_SUCCESS;
3755 state = p2p->pending_action_state;
3756 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
3757 switch (state) {
3758 case P2P_NO_PENDING_ACTION:
Dmitry Shmidt4582d2a2014-02-28 11:14:23 -08003759 if (p2p->send_action_in_progress) {
3760 p2p->send_action_in_progress = 0;
3761 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
3762 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003763 break;
3764 case P2P_PENDING_GO_NEG_REQUEST:
3765 p2p_go_neg_req_cb(p2p, success);
3766 break;
3767 case P2P_PENDING_GO_NEG_RESPONSE:
3768 p2p_go_neg_resp_cb(p2p, success);
3769 break;
3770 case P2P_PENDING_GO_NEG_RESPONSE_FAILURE:
Dmitry Shmidt4b060592013-04-29 16:42:49 -07003771 p2p_go_neg_resp_failure_cb(p2p, success, dst);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003772 break;
3773 case P2P_PENDING_GO_NEG_CONFIRM:
3774 p2p_go_neg_conf_cb(p2p, result);
3775 break;
3776 case P2P_PENDING_SD:
3777 p2p_sd_cb(p2p, success);
3778 break;
3779 case P2P_PENDING_PD:
3780 p2p_prov_disc_cb(p2p, success);
3781 break;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003782 case P2P_PENDING_PD_RESPONSE:
3783 p2p_prov_disc_resp_cb(p2p, success);
3784 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003785 case P2P_PENDING_INVITATION_REQUEST:
3786 p2p_invitation_req_cb(p2p, success);
3787 break;
3788 case P2P_PENDING_INVITATION_RESPONSE:
3789 p2p_invitation_resp_cb(p2p, success);
3790 break;
3791 case P2P_PENDING_DEV_DISC_REQUEST:
3792 p2p_dev_disc_req_cb(p2p, success);
3793 break;
3794 case P2P_PENDING_DEV_DISC_RESPONSE:
3795 p2p_dev_disc_resp_cb(p2p, success);
3796 break;
3797 case P2P_PENDING_GO_DISC_REQ:
3798 p2p_go_disc_req_cb(p2p, success);
3799 break;
3800 }
3801}
3802
3803
3804void p2p_listen_cb(struct p2p_data *p2p, unsigned int freq,
3805 unsigned int duration)
3806{
3807 if (freq == p2p->pending_client_disc_freq) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003808 p2p_dbg(p2p, "Client discoverability remain-awake completed");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003809 p2p->pending_client_disc_freq = 0;
3810 return;
3811 }
3812
3813 if (freq != p2p->pending_listen_freq) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003814 p2p_dbg(p2p, "Unexpected listen callback for freq=%u duration=%u (pending_listen_freq=%u)",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003815 freq, duration, p2p->pending_listen_freq);
3816 return;
3817 }
3818
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003819 p2p_dbg(p2p, "Starting Listen timeout(%u,%u) on freq=%u based on callback",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003820 p2p->pending_listen_sec, p2p->pending_listen_usec,
3821 p2p->pending_listen_freq);
3822 p2p->in_listen = 1;
3823 p2p->drv_in_listen = freq;
3824 if (p2p->pending_listen_sec || p2p->pending_listen_usec) {
3825 /*
3826 * Add 20 msec extra wait to avoid race condition with driver
3827 * remain-on-channel end event, i.e., give driver more time to
3828 * complete the operation before our timeout expires.
3829 */
3830 p2p_set_timeout(p2p, p2p->pending_listen_sec,
3831 p2p->pending_listen_usec + 20000);
3832 }
3833
3834 p2p->pending_listen_freq = 0;
3835}
3836
3837
3838int p2p_listen_end(struct p2p_data *p2p, unsigned int freq)
3839{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003840 p2p_dbg(p2p, "Driver ended Listen state (freq=%u)", freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003841 p2p->drv_in_listen = 0;
3842 if (p2p->in_listen)
3843 return 0; /* Internal timeout will trigger the next step */
3844
Roshan Pius3a1667e2018-07-03 15:17:14 -07003845 if (p2p->state == P2P_WAIT_PEER_CONNECT && p2p->go_neg_peer &&
3846 p2p->pending_listen_freq) {
3847 /*
3848 * Better wait a bit if the driver is unable to start
3849 * offchannel operation for some reason to continue with
3850 * P2P_WAIT_PEER_(IDLE/CONNECT) state transitions.
3851 */
3852 p2p_dbg(p2p,
3853 "Listen operation did not seem to start - delay idle phase to avoid busy loop");
3854 p2p_set_timeout(p2p, 0, 100000);
3855 return 1;
3856 }
3857
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003858 if (p2p->state == P2P_CONNECT_LISTEN && p2p->go_neg_peer) {
3859 if (p2p->go_neg_peer->connect_reqs >= 120) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003860 p2p_dbg(p2p, "Timeout on sending GO Negotiation Request without getting response");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003861 p2p_go_neg_failed(p2p, -1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003862 return 0;
3863 }
3864
3865 p2p_set_state(p2p, P2P_CONNECT);
3866 p2p_connect_send(p2p, p2p->go_neg_peer);
3867 return 1;
3868 } else if (p2p->state == P2P_SEARCH) {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003869 if (p2p->p2p_scan_running) {
3870 /*
3871 * Search is already in progress. This can happen if
3872 * an Action frame RX is reported immediately after
3873 * the end of a remain-on-channel operation and the
3874 * response frame to that is sent using an offchannel
3875 * operation while in p2p_find. Avoid an attempt to
3876 * restart a scan here.
3877 */
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003878 p2p_dbg(p2p, "p2p_scan already in progress - do not try to start a new one");
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003879 return 1;
3880 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07003881 if (p2p->pending_listen_freq) {
3882 /*
3883 * Better wait a bit if the driver is unable to start
3884 * offchannel operation for some reason. p2p_search()
3885 * will be started from internal timeout.
3886 */
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003887 p2p_dbg(p2p, "Listen operation did not seem to start - delay search phase to avoid busy loop");
Dmitry Shmidt04949592012-07-19 12:16:46 -07003888 p2p_set_timeout(p2p, 0, 100000);
3889 return 1;
3890 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003891 if (p2p->search_delay) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003892 p2p_dbg(p2p, "Delay search operation by %u ms",
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003893 p2p->search_delay);
3894 p2p_set_timeout(p2p, p2p->search_delay / 1000,
3895 (p2p->search_delay % 1000) * 1000);
3896 return 1;
3897 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003898 p2p_search(p2p);
3899 return 1;
3900 }
3901
3902 return 0;
3903}
3904
3905
3906static void p2p_timeout_connect(struct p2p_data *p2p)
3907{
3908 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003909 if (p2p->go_neg_peer &&
3910 (p2p->go_neg_peer->flags & P2P_DEV_WAIT_GO_NEG_CONFIRM)) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003911 p2p_dbg(p2p, "Wait for GO Negotiation Confirm timed out - assume GO Negotiation failed");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003912 p2p_go_neg_failed(p2p, -1);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003913 return;
3914 }
Dmitry Shmidt8c652892013-03-01 10:14:01 -08003915 if (p2p->go_neg_peer &&
3916 (p2p->go_neg_peer->flags & P2P_DEV_PEER_WAITING_RESPONSE) &&
3917 p2p->go_neg_peer->connect_reqs < 120) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003918 p2p_dbg(p2p, "Peer expected to wait our response - skip listen");
Dmitry Shmidt8c652892013-03-01 10:14:01 -08003919 p2p_connect_send(p2p, p2p->go_neg_peer);
3920 return;
3921 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003922 if (p2p->go_neg_peer && p2p->go_neg_peer->oob_go_neg_freq > 0) {
3923 p2p_dbg(p2p, "Skip connect-listen since GO Neg channel known (OOB)");
3924 p2p_set_state(p2p, P2P_CONNECT_LISTEN);
3925 p2p_set_timeout(p2p, 0, 30000);
3926 return;
3927 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003928 p2p_set_state(p2p, P2P_CONNECT_LISTEN);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003929 p2p_listen_in_find(p2p, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003930}
3931
3932
3933static void p2p_timeout_connect_listen(struct p2p_data *p2p)
3934{
3935 if (p2p->go_neg_peer) {
3936 if (p2p->drv_in_listen) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003937 p2p_dbg(p2p, "Driver is still in Listen state; wait for it to complete");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003938 return;
3939 }
3940
3941 if (p2p->go_neg_peer->connect_reqs >= 120) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003942 p2p_dbg(p2p, "Timeout on sending GO Negotiation Request without getting response");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003943 p2p_go_neg_failed(p2p, -1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003944 return;
3945 }
3946
3947 p2p_set_state(p2p, P2P_CONNECT);
3948 p2p_connect_send(p2p, p2p->go_neg_peer);
3949 } else
3950 p2p_set_state(p2p, P2P_IDLE);
3951}
3952
3953
3954static void p2p_timeout_wait_peer_connect(struct p2p_data *p2p)
3955{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003956 p2p_set_state(p2p, P2P_WAIT_PEER_IDLE);
Dmitry Shmidt18463232014-01-24 12:29:41 -08003957
3958 if (p2p->cfg->is_concurrent_session_active &&
3959 p2p->cfg->is_concurrent_session_active(p2p->cfg->cb_ctx))
3960 p2p_set_timeout(p2p, 0, 500000);
3961 else
3962 p2p_set_timeout(p2p, 0, 200000);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003963}
3964
3965
3966static void p2p_timeout_wait_peer_idle(struct p2p_data *p2p)
3967{
3968 struct p2p_device *dev = p2p->go_neg_peer;
3969
3970 if (dev == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003971 p2p_dbg(p2p, "Unknown GO Neg peer - stop GO Neg wait");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003972 return;
3973 }
3974
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003975 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 -07003976 p2p_set_state(p2p, P2P_WAIT_PEER_CONNECT);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003977 p2p_listen_in_find(p2p, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003978}
3979
3980
3981static void p2p_timeout_sd_during_find(struct p2p_data *p2p)
3982{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003983 p2p_dbg(p2p, "Service Discovery Query timeout");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003984 if (p2p->sd_peer) {
3985 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003986 p2p->sd_peer = NULL;
3987 }
3988 p2p_continue_find(p2p);
3989}
3990
3991
3992static void p2p_timeout_prov_disc_during_find(struct p2p_data *p2p)
3993{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003994 p2p_dbg(p2p, "Provision Discovery Request timeout");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003995 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
3996 p2p_continue_find(p2p);
3997}
3998
3999
Jouni Malinen75ecf522011-06-27 15:19:46 -07004000static void p2p_timeout_prov_disc_req(struct p2p_data *p2p)
4001{
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004002 u32 adv_id = 0;
4003 u8 *adv_mac = NULL;
4004
Jouni Malinen75ecf522011-06-27 15:19:46 -07004005 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
4006
4007 /*
4008 * For user initiated PD requests that we have not gotten any responses
4009 * for while in IDLE state, we retry them a couple of times before
4010 * giving up.
4011 */
4012 if (!p2p->user_initiated_pd)
4013 return;
4014
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004015 p2p_dbg(p2p, "User initiated Provision Discovery Request timeout");
Jouni Malinen75ecf522011-06-27 15:19:46 -07004016
4017 if (p2p->pd_retries) {
4018 p2p->pd_retries--;
4019 p2p_retry_pd(p2p);
4020 } else {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004021 struct p2p_device *dev;
4022 int for_join = 0;
4023
4024 dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
4025 if (os_memcmp(p2p->pending_pd_devaddr,
4026 dev->info.p2p_device_addr, ETH_ALEN) != 0)
4027 continue;
4028 if (dev->req_config_methods &&
4029 (dev->flags & P2P_DEV_PD_FOR_JOIN))
4030 for_join = 1;
4031 }
4032
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004033 if (p2p->p2ps_prov) {
4034 adv_id = p2p->p2ps_prov->adv_id;
4035 adv_mac = p2p->p2ps_prov->adv_mac;
4036 }
4037
Jouni Malinen75ecf522011-06-27 15:19:46 -07004038 if (p2p->cfg->prov_disc_fail)
4039 p2p->cfg->prov_disc_fail(p2p->cfg->cb_ctx,
4040 p2p->pending_pd_devaddr,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004041 for_join ?
4042 P2P_PROV_DISC_TIMEOUT_JOIN :
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004043 P2P_PROV_DISC_TIMEOUT,
4044 adv_id, adv_mac, NULL);
Jouni Malinen75ecf522011-06-27 15:19:46 -07004045 p2p_reset_pending_pd(p2p);
4046 }
4047}
4048
4049
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004050static void p2p_timeout_invite(struct p2p_data *p2p)
4051{
4052 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
4053 p2p_set_state(p2p, P2P_INVITE_LISTEN);
4054 if (p2p->inv_role == P2P_INVITE_ROLE_ACTIVE_GO) {
4055 /*
4056 * Better remain on operating channel instead of listen channel
4057 * when running a group.
4058 */
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004059 p2p_dbg(p2p, "Inviting in active GO role - wait on operating channel");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004060 p2p_set_timeout(p2p, 0, 100000);
4061 return;
4062 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004063 p2p_listen_in_find(p2p, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004064}
4065
4066
4067static void p2p_timeout_invite_listen(struct p2p_data *p2p)
4068{
4069 if (p2p->invite_peer && p2p->invite_peer->invitation_reqs < 100) {
4070 p2p_set_state(p2p, P2P_INVITE);
4071 p2p_invite_send(p2p, p2p->invite_peer,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004072 p2p->invite_go_dev_addr, p2p->invite_dev_pw_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004073 } else {
4074 if (p2p->invite_peer) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004075 p2p_dbg(p2p, "Invitation Request retry limit reached");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004076 if (p2p->cfg->invitation_result)
4077 p2p->cfg->invitation_result(
Dmitry Shmidt700a1372013-03-15 14:14:44 -07004078 p2p->cfg->cb_ctx, -1, NULL, NULL,
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004079 p2p->invite_peer->info.p2p_device_addr,
Dmitry Shmidt15907092014-03-25 10:42:57 -07004080 0, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004081 }
4082 p2p_set_state(p2p, P2P_IDLE);
4083 }
4084}
4085
4086
4087static void p2p_state_timeout(void *eloop_ctx, void *timeout_ctx)
4088{
4089 struct p2p_data *p2p = eloop_ctx;
4090
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004091 p2p_dbg(p2p, "Timeout (state=%s)", p2p_state_txt(p2p->state));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004092
4093 p2p->in_listen = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004094 if (p2p->drv_in_listen) {
4095 p2p_dbg(p2p, "Driver is still in listen state - stop it");
4096 p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
4097 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004098
4099 switch (p2p->state) {
4100 case P2P_IDLE:
Jouni Malinen75ecf522011-06-27 15:19:46 -07004101 /* Check if we timed out waiting for PD req */
4102 if (p2p->pending_action_state == P2P_PENDING_PD)
4103 p2p_timeout_prov_disc_req(p2p);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004104 break;
4105 case P2P_SEARCH:
Jouni Malinen75ecf522011-06-27 15:19:46 -07004106 /* Check if we timed out waiting for PD req */
4107 if (p2p->pending_action_state == P2P_PENDING_PD)
4108 p2p_timeout_prov_disc_req(p2p);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004109 if (p2p->search_delay && !p2p->in_search_delay) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004110 p2p_dbg(p2p, "Delay search operation by %u ms",
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004111 p2p->search_delay);
4112 p2p->in_search_delay = 1;
4113 p2p_set_timeout(p2p, p2p->search_delay / 1000,
4114 (p2p->search_delay % 1000) * 1000);
4115 break;
4116 }
4117 p2p->in_search_delay = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004118 p2p_search(p2p);
4119 break;
4120 case P2P_CONNECT:
4121 p2p_timeout_connect(p2p);
4122 break;
4123 case P2P_CONNECT_LISTEN:
4124 p2p_timeout_connect_listen(p2p);
4125 break;
4126 case P2P_GO_NEG:
4127 break;
4128 case P2P_LISTEN_ONLY:
Jouni Malinen75ecf522011-06-27 15:19:46 -07004129 /* Check if we timed out waiting for PD req */
4130 if (p2p->pending_action_state == P2P_PENDING_PD)
4131 p2p_timeout_prov_disc_req(p2p);
4132
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004133 if (p2p->ext_listen_only) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004134 p2p_dbg(p2p, "Extended Listen Timing - Listen State completed");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004135 p2p->ext_listen_only = 0;
4136 p2p_set_state(p2p, P2P_IDLE);
4137 }
4138 break;
4139 case P2P_WAIT_PEER_CONNECT:
4140 p2p_timeout_wait_peer_connect(p2p);
4141 break;
4142 case P2P_WAIT_PEER_IDLE:
4143 p2p_timeout_wait_peer_idle(p2p);
4144 break;
4145 case P2P_SD_DURING_FIND:
4146 p2p_timeout_sd_during_find(p2p);
4147 break;
4148 case P2P_PROVISIONING:
4149 break;
4150 case P2P_PD_DURING_FIND:
4151 p2p_timeout_prov_disc_during_find(p2p);
4152 break;
4153 case P2P_INVITE:
4154 p2p_timeout_invite(p2p);
4155 break;
4156 case P2P_INVITE_LISTEN:
4157 p2p_timeout_invite_listen(p2p);
4158 break;
4159 }
4160}
4161
4162
4163int p2p_reject(struct p2p_data *p2p, const u8 *peer_addr)
4164{
4165 struct p2p_device *dev;
4166
4167 dev = p2p_get_device(p2p, peer_addr);
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004168 p2p_dbg(p2p, "Local request to reject connection attempts by peer "
4169 MACSTR, MAC2STR(peer_addr));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004170 if (dev == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004171 p2p_dbg(p2p, "Peer " MACSTR " unknown", MAC2STR(peer_addr));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004172 return -1;
4173 }
4174 dev->status = P2P_SC_FAIL_REJECTED_BY_USER;
4175 dev->flags |= P2P_DEV_USER_REJECTED;
4176 return 0;
4177}
4178
4179
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004180const char * p2p_wps_method_text(enum p2p_wps_method method)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004181{
4182 switch (method) {
4183 case WPS_NOT_READY:
4184 return "not-ready";
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004185 case WPS_PIN_DISPLAY:
4186 return "Display";
4187 case WPS_PIN_KEYPAD:
4188 return "Keypad";
4189 case WPS_PBC:
4190 return "PBC";
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004191 case WPS_NFC:
4192 return "NFC";
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004193 case WPS_P2PS:
4194 return "P2PS";
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004195 }
4196
4197 return "??";
4198}
4199
4200
4201static const char * p2p_go_state_text(enum p2p_go_state go_state)
4202{
4203 switch (go_state) {
4204 case UNKNOWN_GO:
4205 return "unknown";
4206 case LOCAL_GO:
4207 return "local";
4208 case REMOTE_GO:
4209 return "remote";
4210 }
4211
4212 return "??";
4213}
4214
4215
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004216const struct p2p_peer_info * p2p_get_peer_info(struct p2p_data *p2p,
4217 const u8 *addr, int next)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004218{
4219 struct p2p_device *dev;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004220
4221 if (addr)
4222 dev = p2p_get_device(p2p, addr);
4223 else
4224 dev = dl_list_first(&p2p->devices, struct p2p_device, list);
4225
4226 if (dev && next) {
4227 dev = dl_list_first(&dev->list, struct p2p_device, list);
4228 if (&dev->list == &p2p->devices)
4229 dev = NULL;
4230 }
4231
4232 if (dev == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004233 return NULL;
4234
4235 return &dev->info;
4236}
4237
4238
4239int p2p_get_peer_info_txt(const struct p2p_peer_info *info,
4240 char *buf, size_t buflen)
4241{
4242 struct p2p_device *dev;
4243 int res;
4244 char *pos, *end;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004245 struct os_reltime now;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004246
4247 if (info == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004248 return -1;
4249
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004250 dev = (struct p2p_device *) (((u8 *) info) -
4251 offsetof(struct p2p_device, info));
4252
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004253 pos = buf;
4254 end = buf + buflen;
4255
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004256 os_get_reltime(&now);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004257 res = os_snprintf(pos, end - pos,
4258 "age=%d\n"
4259 "listen_freq=%d\n"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004260 "wps_method=%s\n"
4261 "interface_addr=" MACSTR "\n"
4262 "member_in_go_dev=" MACSTR "\n"
4263 "member_in_go_iface=" MACSTR "\n"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004264 "go_neg_req_sent=%d\n"
4265 "go_state=%s\n"
4266 "dialog_token=%u\n"
4267 "intended_addr=" MACSTR "\n"
4268 "country=%c%c\n"
4269 "oper_freq=%d\n"
4270 "req_config_methods=0x%x\n"
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07004271 "flags=%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004272 "status=%d\n"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004273 "invitation_reqs=%u\n",
4274 (int) (now.sec - dev->last_seen.sec),
4275 dev->listen_freq,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004276 p2p_wps_method_text(dev->wps_method),
4277 MAC2STR(dev->interface_addr),
4278 MAC2STR(dev->member_in_go_dev),
4279 MAC2STR(dev->member_in_go_iface),
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004280 dev->go_neg_req_sent,
4281 p2p_go_state_text(dev->go_state),
4282 dev->dialog_token,
4283 MAC2STR(dev->intended_addr),
4284 dev->country[0] ? dev->country[0] : '_',
4285 dev->country[1] ? dev->country[1] : '_',
4286 dev->oper_freq,
4287 dev->req_config_methods,
4288 dev->flags & P2P_DEV_PROBE_REQ_ONLY ?
4289 "[PROBE_REQ_ONLY]" : "",
4290 dev->flags & P2P_DEV_REPORTED ? "[REPORTED]" : "",
4291 dev->flags & P2P_DEV_NOT_YET_READY ?
4292 "[NOT_YET_READY]" : "",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004293 dev->flags & P2P_DEV_PD_PEER_DISPLAY ?
4294 "[PD_PEER_DISPLAY]" : "",
4295 dev->flags & P2P_DEV_PD_PEER_KEYPAD ?
4296 "[PD_PEER_KEYPAD]" : "",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004297 dev->flags & P2P_DEV_PD_PEER_P2PS ?
4298 "[PD_PEER_P2PS]" : "",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004299 dev->flags & P2P_DEV_USER_REJECTED ?
4300 "[USER_REJECTED]" : "",
4301 dev->flags & P2P_DEV_PEER_WAITING_RESPONSE ?
4302 "[PEER_WAITING_RESPONSE]" : "",
4303 dev->flags & P2P_DEV_PREFER_PERSISTENT_GROUP ?
4304 "[PREFER_PERSISTENT_GROUP]" : "",
4305 dev->flags & P2P_DEV_WAIT_GO_NEG_RESPONSE ?
4306 "[WAIT_GO_NEG_RESPONSE]" : "",
4307 dev->flags & P2P_DEV_WAIT_GO_NEG_CONFIRM ?
4308 "[WAIT_GO_NEG_CONFIRM]" : "",
4309 dev->flags & P2P_DEV_GROUP_CLIENT_ONLY ?
4310 "[GROUP_CLIENT_ONLY]" : "",
4311 dev->flags & P2P_DEV_FORCE_FREQ ?
4312 "[FORCE_FREQ]" : "",
4313 dev->flags & P2P_DEV_PD_FOR_JOIN ?
4314 "[PD_FOR_JOIN]" : "",
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07004315 dev->flags & P2P_DEV_LAST_SEEN_AS_GROUP_CLIENT ?
4316 "[LAST_SEEN_AS_GROUP_CLIENT]" : "",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004317 dev->status,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004318 dev->invitation_reqs);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004319 if (os_snprintf_error(end - pos, res))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004320 return pos - buf;
4321 pos += res;
4322
4323 if (dev->ext_listen_period) {
4324 res = os_snprintf(pos, end - pos,
4325 "ext_listen_period=%u\n"
4326 "ext_listen_interval=%u\n",
4327 dev->ext_listen_period,
4328 dev->ext_listen_interval);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004329 if (os_snprintf_error(end - pos, res))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004330 return pos - buf;
4331 pos += res;
4332 }
4333
4334 if (dev->oper_ssid_len) {
4335 res = os_snprintf(pos, end - pos,
4336 "oper_ssid=%s\n",
4337 wpa_ssid_txt(dev->oper_ssid,
4338 dev->oper_ssid_len));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004339 if (os_snprintf_error(end - pos, res))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004340 return pos - buf;
4341 pos += res;
4342 }
4343
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004344#ifdef CONFIG_WIFI_DISPLAY
4345 if (dev->info.wfd_subelems) {
4346 res = os_snprintf(pos, end - pos, "wfd_subelems=");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004347 if (os_snprintf_error(end - pos, res))
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004348 return pos - buf;
4349 pos += res;
4350
4351 pos += wpa_snprintf_hex(pos, end - pos,
4352 wpabuf_head(dev->info.wfd_subelems),
4353 wpabuf_len(dev->info.wfd_subelems));
4354
4355 res = os_snprintf(pos, end - pos, "\n");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004356 if (os_snprintf_error(end - pos, res))
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004357 return pos - buf;
4358 pos += res;
4359 }
4360#endif /* CONFIG_WIFI_DISPLAY */
4361
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004362 return pos - buf;
4363}
4364
4365
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004366int p2p_peer_known(struct p2p_data *p2p, const u8 *addr)
4367{
4368 return p2p_get_device(p2p, addr) != NULL;
4369}
4370
4371
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004372void p2p_set_client_discoverability(struct p2p_data *p2p, int enabled)
4373{
4374 if (enabled) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004375 p2p_dbg(p2p, "Client discoverability enabled");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004376 p2p->dev_capab |= P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
4377 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004378 p2p_dbg(p2p, "Client discoverability disabled");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004379 p2p->dev_capab &= ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
4380 }
4381}
4382
4383
4384static struct wpabuf * p2p_build_presence_req(u32 duration1, u32 interval1,
4385 u32 duration2, u32 interval2)
4386{
4387 struct wpabuf *req;
4388 struct p2p_noa_desc desc1, desc2, *ptr1 = NULL, *ptr2 = NULL;
4389 u8 *len;
4390
4391 req = wpabuf_alloc(100);
4392 if (req == NULL)
4393 return NULL;
4394
4395 if (duration1 || interval1) {
4396 os_memset(&desc1, 0, sizeof(desc1));
4397 desc1.count_type = 1;
4398 desc1.duration = duration1;
4399 desc1.interval = interval1;
4400 ptr1 = &desc1;
4401
4402 if (duration2 || interval2) {
4403 os_memset(&desc2, 0, sizeof(desc2));
4404 desc2.count_type = 2;
4405 desc2.duration = duration2;
4406 desc2.interval = interval2;
4407 ptr2 = &desc2;
4408 }
4409 }
4410
4411 p2p_buf_add_action_hdr(req, P2P_PRESENCE_REQ, 1);
4412 len = p2p_buf_add_ie_hdr(req);
4413 p2p_buf_add_noa(req, 0, 0, 0, ptr1, ptr2);
4414 p2p_buf_update_ie_hdr(req, len);
4415
4416 return req;
4417}
4418
4419
4420int p2p_presence_req(struct p2p_data *p2p, const u8 *go_interface_addr,
4421 const u8 *own_interface_addr, unsigned int freq,
4422 u32 duration1, u32 interval1, u32 duration2,
4423 u32 interval2)
4424{
4425 struct wpabuf *req;
4426
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004427 p2p_dbg(p2p, "Send Presence Request to GO " MACSTR
4428 " (own interface " MACSTR ") freq=%u dur1=%u int1=%u "
4429 "dur2=%u int2=%u",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004430 MAC2STR(go_interface_addr), MAC2STR(own_interface_addr),
4431 freq, duration1, interval1, duration2, interval2);
4432
4433 req = p2p_build_presence_req(duration1, interval1, duration2,
4434 interval2);
4435 if (req == NULL)
4436 return -1;
4437
4438 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
4439 if (p2p_send_action(p2p, freq, go_interface_addr, own_interface_addr,
4440 go_interface_addr,
4441 wpabuf_head(req), wpabuf_len(req), 200) < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004442 p2p_dbg(p2p, "Failed to send Action frame");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004443 }
4444 wpabuf_free(req);
4445
4446 return 0;
4447}
4448
4449
4450static struct wpabuf * p2p_build_presence_resp(u8 status, const u8 *noa,
4451 size_t noa_len, u8 dialog_token)
4452{
4453 struct wpabuf *resp;
4454 u8 *len;
4455
4456 resp = wpabuf_alloc(100 + noa_len);
4457 if (resp == NULL)
4458 return NULL;
4459
4460 p2p_buf_add_action_hdr(resp, P2P_PRESENCE_RESP, dialog_token);
4461 len = p2p_buf_add_ie_hdr(resp);
4462 p2p_buf_add_status(resp, status);
4463 if (noa) {
4464 wpabuf_put_u8(resp, P2P_ATTR_NOTICE_OF_ABSENCE);
4465 wpabuf_put_le16(resp, noa_len);
4466 wpabuf_put_data(resp, noa, noa_len);
4467 } else
4468 p2p_buf_add_noa(resp, 0, 0, 0, NULL, NULL);
4469 p2p_buf_update_ie_hdr(resp, len);
4470
4471 return resp;
4472}
4473
4474
4475static void p2p_process_presence_req(struct p2p_data *p2p, const u8 *da,
4476 const u8 *sa, const u8 *data, size_t len,
4477 int rx_freq)
4478{
4479 struct p2p_message msg;
4480 u8 status;
4481 struct wpabuf *resp;
4482 size_t g;
4483 struct p2p_group *group = NULL;
4484 int parsed = 0;
4485 u8 noa[50];
4486 int noa_len;
4487
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004488 p2p_dbg(p2p, "Received P2P Action - P2P Presence Request");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004489
4490 for (g = 0; g < p2p->num_groups; g++) {
4491 if (os_memcmp(da, p2p_group_get_interface_addr(p2p->groups[g]),
4492 ETH_ALEN) == 0) {
4493 group = p2p->groups[g];
4494 break;
4495 }
4496 }
4497 if (group == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004498 p2p_dbg(p2p, "Ignore P2P Presence Request for unknown group "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004499 MACSTR, MAC2STR(da));
4500 return;
4501 }
4502
4503 if (p2p_parse(data, len, &msg) < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004504 p2p_dbg(p2p, "Failed to parse P2P Presence Request");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004505 status = P2P_SC_FAIL_INVALID_PARAMS;
4506 goto fail;
4507 }
4508 parsed = 1;
4509
4510 if (msg.noa == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004511 p2p_dbg(p2p, "No NoA attribute in P2P Presence Request");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004512 status = P2P_SC_FAIL_INVALID_PARAMS;
4513 goto fail;
4514 }
4515
4516 status = p2p_group_presence_req(group, sa, msg.noa, msg.noa_len);
4517
4518fail:
4519 if (p2p->cfg->get_noa)
4520 noa_len = p2p->cfg->get_noa(p2p->cfg->cb_ctx, da, noa,
4521 sizeof(noa));
4522 else
4523 noa_len = -1;
4524 resp = p2p_build_presence_resp(status, noa_len > 0 ? noa : NULL,
4525 noa_len > 0 ? noa_len : 0,
4526 msg.dialog_token);
4527 if (parsed)
4528 p2p_parse_free(&msg);
4529 if (resp == NULL)
4530 return;
4531
4532 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
4533 if (p2p_send_action(p2p, rx_freq, sa, da, da,
4534 wpabuf_head(resp), wpabuf_len(resp), 200) < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004535 p2p_dbg(p2p, "Failed to send Action frame");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004536 }
4537 wpabuf_free(resp);
4538}
4539
4540
4541static void p2p_process_presence_resp(struct p2p_data *p2p, const u8 *da,
4542 const u8 *sa, const u8 *data, size_t len)
4543{
4544 struct p2p_message msg;
4545
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004546 p2p_dbg(p2p, "Received P2P Action - P2P Presence Response");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004547
4548 if (p2p_parse(data, len, &msg) < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004549 p2p_dbg(p2p, "Failed to parse P2P Presence Response");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004550 return;
4551 }
4552
4553 if (msg.status == NULL || msg.noa == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004554 p2p_dbg(p2p, "No Status or NoA attribute in P2P Presence Response");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004555 p2p_parse_free(&msg);
4556 return;
4557 }
4558
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004559 if (p2p->cfg->presence_resp) {
4560 p2p->cfg->presence_resp(p2p->cfg->cb_ctx, sa, *msg.status,
4561 msg.noa, msg.noa_len);
4562 }
4563
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004564 if (*msg.status) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004565 p2p_dbg(p2p, "P2P Presence Request was rejected: status %u",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004566 *msg.status);
4567 p2p_parse_free(&msg);
4568 return;
4569 }
4570
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004571 p2p_dbg(p2p, "P2P Presence Request was accepted");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004572 wpa_hexdump(MSG_DEBUG, "P2P: P2P Presence Response - NoA",
4573 msg.noa, msg.noa_len);
4574 /* TODO: process NoA */
4575 p2p_parse_free(&msg);
4576}
4577
4578
4579static void p2p_ext_listen_timeout(void *eloop_ctx, void *timeout_ctx)
4580{
4581 struct p2p_data *p2p = eloop_ctx;
4582
4583 if (p2p->ext_listen_interval) {
4584 /* Schedule next extended listen timeout */
4585 eloop_register_timeout(p2p->ext_listen_interval_sec,
4586 p2p->ext_listen_interval_usec,
4587 p2p_ext_listen_timeout, p2p, NULL);
4588 }
4589
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -07004590 if ((p2p->cfg->is_p2p_in_progress &&
4591 p2p->cfg->is_p2p_in_progress(p2p->cfg->cb_ctx)) ||
4592 (p2p->pending_action_state == P2P_PENDING_PD &&
4593 p2p->pd_retries > 0)) {
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -07004594 p2p_dbg(p2p, "Operation in progress - skip Extended Listen timeout (%s)",
4595 p2p_state_txt(p2p->state));
4596 return;
4597 }
4598
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004599 if (p2p->state == P2P_LISTEN_ONLY && p2p->ext_listen_only) {
4600 /*
4601 * This should not really happen, but it looks like the Listen
4602 * command may fail is something else (e.g., a scan) was
4603 * running at an inconvenient time. As a workaround, allow new
4604 * Extended Listen operation to be started.
4605 */
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004606 p2p_dbg(p2p, "Previous Extended Listen operation had not been completed - try again");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004607 p2p->ext_listen_only = 0;
4608 p2p_set_state(p2p, P2P_IDLE);
4609 }
4610
4611 if (p2p->state != P2P_IDLE) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004612 p2p_dbg(p2p, "Skip Extended Listen timeout in active state (%s)", p2p_state_txt(p2p->state));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004613 return;
4614 }
4615
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004616 p2p_dbg(p2p, "Extended Listen timeout");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004617 p2p->ext_listen_only = 1;
4618 if (p2p_listen(p2p, p2p->ext_listen_period) < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004619 p2p_dbg(p2p, "Failed to start Listen state for Extended Listen Timing");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004620 p2p->ext_listen_only = 0;
4621 }
4622}
4623
4624
4625int p2p_ext_listen(struct p2p_data *p2p, unsigned int period,
4626 unsigned int interval)
4627{
4628 if (period > 65535 || interval > 65535 || period > interval ||
4629 (period == 0 && interval > 0) || (period > 0 && interval == 0)) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004630 p2p_dbg(p2p, "Invalid Extended Listen Timing request: period=%u interval=%u",
4631 period, interval);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004632 return -1;
4633 }
4634
4635 eloop_cancel_timeout(p2p_ext_listen_timeout, p2p, NULL);
4636
4637 if (interval == 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004638 p2p_dbg(p2p, "Disabling Extended Listen Timing");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004639 p2p->ext_listen_period = 0;
4640 p2p->ext_listen_interval = 0;
4641 return 0;
4642 }
4643
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004644 p2p_dbg(p2p, "Enabling Extended Listen Timing: period %u msec, interval %u msec",
4645 period, interval);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004646 p2p->ext_listen_period = period;
4647 p2p->ext_listen_interval = interval;
4648 p2p->ext_listen_interval_sec = interval / 1000;
4649 p2p->ext_listen_interval_usec = (interval % 1000) * 1000;
4650
4651 eloop_register_timeout(p2p->ext_listen_interval_sec,
4652 p2p->ext_listen_interval_usec,
4653 p2p_ext_listen_timeout, p2p, NULL);
4654
4655 return 0;
4656}
4657
4658
4659void p2p_deauth_notif(struct p2p_data *p2p, const u8 *bssid, u16 reason_code,
4660 const u8 *ie, size_t ie_len)
4661{
4662 struct p2p_message msg;
4663
4664 if (bssid == NULL || ie == NULL)
4665 return;
4666
4667 os_memset(&msg, 0, sizeof(msg));
4668 if (p2p_parse_ies(ie, ie_len, &msg))
4669 return;
Dmitry Shmidt97672262014-02-03 13:02:54 -08004670 if (msg.minor_reason_code == NULL) {
4671 p2p_parse_free(&msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004672 return;
Dmitry Shmidt97672262014-02-03 13:02:54 -08004673 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004674
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004675 p2p_dbg(p2p, "Deauthentication notification BSSID " MACSTR
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004676 " reason_code=%u minor_reason_code=%u",
4677 MAC2STR(bssid), reason_code, *msg.minor_reason_code);
4678
4679 p2p_parse_free(&msg);
4680}
4681
4682
4683void p2p_disassoc_notif(struct p2p_data *p2p, const u8 *bssid, u16 reason_code,
4684 const u8 *ie, size_t ie_len)
4685{
4686 struct p2p_message msg;
4687
4688 if (bssid == NULL || ie == NULL)
4689 return;
4690
4691 os_memset(&msg, 0, sizeof(msg));
4692 if (p2p_parse_ies(ie, ie_len, &msg))
4693 return;
Dmitry Shmidt97672262014-02-03 13:02:54 -08004694 if (msg.minor_reason_code == NULL) {
4695 p2p_parse_free(&msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004696 return;
Dmitry Shmidt97672262014-02-03 13:02:54 -08004697 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004698
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004699 p2p_dbg(p2p, "Disassociation notification BSSID " MACSTR
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004700 " reason_code=%u minor_reason_code=%u",
4701 MAC2STR(bssid), reason_code, *msg.minor_reason_code);
4702
4703 p2p_parse_free(&msg);
4704}
4705
4706
4707void p2p_set_managed_oper(struct p2p_data *p2p, int enabled)
4708{
4709 if (enabled) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004710 p2p_dbg(p2p, "Managed P2P Device operations enabled");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004711 p2p->dev_capab |= P2P_DEV_CAPAB_INFRA_MANAGED;
4712 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004713 p2p_dbg(p2p, "Managed P2P Device operations disabled");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004714 p2p->dev_capab &= ~P2P_DEV_CAPAB_INFRA_MANAGED;
4715 }
4716}
4717
4718
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004719int p2p_config_get_random_social(struct p2p_config *p2p, u8 *op_class,
Hai Shalom74f70d42019-02-11 14:42:39 -08004720 u8 *op_channel,
4721 struct wpa_freq_range_list *avoid_list,
4722 struct wpa_freq_range_list *disallow_list)
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004723{
Hai Shalom74f70d42019-02-11 14:42:39 -08004724 return p2p_channel_random_social(&p2p->channels, op_class, op_channel,
4725 avoid_list, disallow_list);
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004726}
4727
4728
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004729int p2p_set_listen_channel(struct p2p_data *p2p, u8 reg_class, u8 channel,
4730 u8 forced)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004731{
Dmitry Shmidt4b060592013-04-29 16:42:49 -07004732 if (p2p_channel_to_freq(reg_class, channel) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004733 return -1;
4734
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004735 /*
4736 * Listen channel was set in configuration or set by control interface;
4737 * cannot override it.
4738 */
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004739 if (p2p->cfg->channel_forced && forced == 0) {
4740 p2p_dbg(p2p,
4741 "Listen channel was previously configured - do not override based on optimization");
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004742 return -1;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004743 }
4744
4745 p2p_dbg(p2p, "Set Listen channel: reg_class %u channel %u",
4746 reg_class, channel);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004747
4748 if (p2p->state == P2P_IDLE) {
4749 p2p->cfg->reg_class = reg_class;
4750 p2p->cfg->channel = channel;
4751 p2p->cfg->channel_forced = forced;
4752 } else {
4753 p2p_dbg(p2p, "Defer setting listen channel");
4754 p2p->pending_reg_class = reg_class;
4755 p2p->pending_channel = channel;
4756 p2p->pending_channel_forced = forced;
4757 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004758
4759 return 0;
4760}
4761
4762
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004763u8 p2p_get_listen_channel(struct p2p_data *p2p)
4764{
4765 return p2p->cfg->channel;
4766}
4767
4768
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004769int p2p_set_ssid_postfix(struct p2p_data *p2p, const u8 *postfix, size_t len)
4770{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004771 p2p_dbg(p2p, "New SSID postfix: %s", wpa_ssid_txt(postfix, len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004772 if (postfix == NULL) {
4773 p2p->cfg->ssid_postfix_len = 0;
4774 return 0;
4775 }
4776 if (len > sizeof(p2p->cfg->ssid_postfix))
4777 return -1;
4778 os_memcpy(p2p->cfg->ssid_postfix, postfix, len);
4779 p2p->cfg->ssid_postfix_len = len;
4780 return 0;
4781}
4782
4783
Jouni Malinen75ecf522011-06-27 15:19:46 -07004784int p2p_set_oper_channel(struct p2p_data *p2p, u8 op_reg_class, u8 op_channel,
4785 int cfg_op_channel)
4786{
Dmitry Shmidt4b060592013-04-29 16:42:49 -07004787 if (p2p_channel_to_freq(op_reg_class, op_channel) < 0)
Jouni Malinen75ecf522011-06-27 15:19:46 -07004788 return -1;
4789
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004790 p2p_dbg(p2p, "Set Operating channel: reg_class %u channel %u",
4791 op_reg_class, op_channel);
Jouni Malinen75ecf522011-06-27 15:19:46 -07004792 p2p->cfg->op_reg_class = op_reg_class;
4793 p2p->cfg->op_channel = op_channel;
4794 p2p->cfg->cfg_op_channel = cfg_op_channel;
4795 return 0;
4796}
4797
4798
Dmitry Shmidt04949592012-07-19 12:16:46 -07004799int p2p_set_pref_chan(struct p2p_data *p2p, unsigned int num_pref_chan,
4800 const struct p2p_channel *pref_chan)
4801{
4802 struct p2p_channel *n;
4803
4804 if (pref_chan) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004805 n = os_memdup(pref_chan,
4806 num_pref_chan * sizeof(struct p2p_channel));
Dmitry Shmidt04949592012-07-19 12:16:46 -07004807 if (n == NULL)
4808 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004809 } else
4810 n = NULL;
4811
4812 os_free(p2p->cfg->pref_chan);
4813 p2p->cfg->pref_chan = n;
4814 p2p->cfg->num_pref_chan = num_pref_chan;
4815
4816 return 0;
4817}
4818
4819
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004820int p2p_set_no_go_freq(struct p2p_data *p2p,
4821 const struct wpa_freq_range_list *list)
4822{
4823 struct wpa_freq_range *tmp;
4824
4825 if (list == NULL || list->num == 0) {
4826 os_free(p2p->no_go_freq.range);
4827 p2p->no_go_freq.range = NULL;
4828 p2p->no_go_freq.num = 0;
4829 return 0;
4830 }
4831
4832 tmp = os_calloc(list->num, sizeof(struct wpa_freq_range));
4833 if (tmp == NULL)
4834 return -1;
4835 os_memcpy(tmp, list->range, list->num * sizeof(struct wpa_freq_range));
4836 os_free(p2p->no_go_freq.range);
4837 p2p->no_go_freq.range = tmp;
4838 p2p->no_go_freq.num = list->num;
4839 p2p_dbg(p2p, "Updated no GO chan list");
4840
4841 return 0;
4842}
4843
4844
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004845int p2p_get_interface_addr(struct p2p_data *p2p, const u8 *dev_addr,
4846 u8 *iface_addr)
4847{
4848 struct p2p_device *dev = p2p_get_device(p2p, dev_addr);
4849 if (dev == NULL || is_zero_ether_addr(dev->interface_addr))
4850 return -1;
4851 os_memcpy(iface_addr, dev->interface_addr, ETH_ALEN);
4852 return 0;
4853}
4854
4855
4856int p2p_get_dev_addr(struct p2p_data *p2p, const u8 *iface_addr,
4857 u8 *dev_addr)
4858{
4859 struct p2p_device *dev = p2p_get_device_interface(p2p, iface_addr);
4860 if (dev == NULL)
4861 return -1;
4862 os_memcpy(dev_addr, dev->info.p2p_device_addr, ETH_ALEN);
4863 return 0;
4864}
4865
4866
4867void p2p_set_peer_filter(struct p2p_data *p2p, const u8 *addr)
4868{
4869 os_memcpy(p2p->peer_filter, addr, ETH_ALEN);
4870 if (is_zero_ether_addr(p2p->peer_filter))
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004871 p2p_dbg(p2p, "Disable peer filter");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004872 else
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004873 p2p_dbg(p2p, "Enable peer filter for " MACSTR,
4874 MAC2STR(p2p->peer_filter));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004875}
4876
4877
4878void p2p_set_cross_connect(struct p2p_data *p2p, int enabled)
4879{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004880 p2p_dbg(p2p, "Cross connection %s", enabled ? "enabled" : "disabled");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004881 if (p2p->cross_connect == enabled)
4882 return;
4883 p2p->cross_connect = enabled;
4884 /* TODO: may need to tear down any action group where we are GO(?) */
4885}
4886
4887
4888int p2p_get_oper_freq(struct p2p_data *p2p, const u8 *iface_addr)
4889{
4890 struct p2p_device *dev = p2p_get_device_interface(p2p, iface_addr);
4891 if (dev == NULL)
4892 return -1;
4893 if (dev->oper_freq <= 0)
4894 return -1;
4895 return dev->oper_freq;
4896}
4897
4898
4899void p2p_set_intra_bss_dist(struct p2p_data *p2p, int enabled)
4900{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004901 p2p_dbg(p2p, "Intra BSS distribution %s",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004902 enabled ? "enabled" : "disabled");
4903 p2p->cfg->p2p_intra_bss = enabled;
4904}
4905
4906
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004907void p2p_update_channel_list(struct p2p_data *p2p,
4908 const struct p2p_channels *chan,
4909 const struct p2p_channels *cli_chan)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004910{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004911 p2p_dbg(p2p, "Update channel list");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004912 os_memcpy(&p2p->cfg->channels, chan, sizeof(struct p2p_channels));
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004913 p2p_channels_dump(p2p, "channels", &p2p->cfg->channels);
4914 os_memcpy(&p2p->cfg->cli_channels, cli_chan,
4915 sizeof(struct p2p_channels));
4916 p2p_channels_dump(p2p, "cli_channels", &p2p->cfg->cli_channels);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004917}
4918
4919
4920int p2p_send_action(struct p2p_data *p2p, unsigned int freq, const u8 *dst,
4921 const u8 *src, const u8 *bssid, const u8 *buf,
4922 size_t len, unsigned int wait_time)
4923{
Hai Shalom021b0b52019-04-10 11:17:58 -07004924 int res, scheduled;
4925
Hai Shalom021b0b52019-04-10 11:17:58 -07004926 res = p2p->cfg->send_action(p2p->cfg->cb_ctx, freq, dst, src, bssid,
4927 buf, len, wait_time, &scheduled);
4928 if (res == 0 && scheduled && p2p->in_listen && freq > 0 &&
4929 (unsigned int) p2p->drv_in_listen != freq) {
4930 p2p_dbg(p2p,
4931 "Stop listen on %d MHz to allow a frame to be sent immediately on %d MHz",
4932 p2p->drv_in_listen, freq);
4933 p2p_stop_listen_for_freq(p2p, freq);
4934 }
4935 return res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004936}
4937
4938
4939void p2p_set_best_channels(struct p2p_data *p2p, int freq_24, int freq_5,
4940 int freq_overall)
4941{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004942 p2p_dbg(p2p, "Best channel: 2.4 GHz: %d, 5 GHz: %d, overall: %d",
4943 freq_24, freq_5, freq_overall);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004944 p2p->best_freq_24 = freq_24;
4945 p2p->best_freq_5 = freq_5;
4946 p2p->best_freq_overall = freq_overall;
4947}
4948
4949
Dmitry Shmidt700a1372013-03-15 14:14:44 -07004950void p2p_set_own_freq_preference(struct p2p_data *p2p, int freq)
4951{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004952 p2p_dbg(p2p, "Own frequency preference: %d MHz", freq);
Dmitry Shmidt700a1372013-03-15 14:14:44 -07004953 p2p->own_freq_preference = freq;
4954}
4955
4956
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004957const u8 * p2p_get_go_neg_peer(struct p2p_data *p2p)
4958{
4959 if (p2p == NULL || p2p->go_neg_peer == NULL)
4960 return NULL;
4961 return p2p->go_neg_peer->info.p2p_device_addr;
4962}
4963
4964
4965const struct p2p_peer_info *
4966p2p_get_peer_found(struct p2p_data *p2p, const u8 *addr, int next)
4967{
4968 struct p2p_device *dev;
4969
4970 if (addr) {
4971 dev = p2p_get_device(p2p, addr);
4972 if (!dev)
4973 return NULL;
4974
4975 if (!next) {
4976 if (dev->flags & P2P_DEV_PROBE_REQ_ONLY)
4977 return NULL;
4978
4979 return &dev->info;
4980 } else {
4981 do {
4982 dev = dl_list_first(&dev->list,
4983 struct p2p_device,
4984 list);
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07004985 if (!dev || &dev->list == &p2p->devices)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004986 return NULL;
4987 } while (dev->flags & P2P_DEV_PROBE_REQ_ONLY);
4988 }
4989 } else {
4990 dev = dl_list_first(&p2p->devices, struct p2p_device, list);
4991 if (!dev)
4992 return NULL;
4993 while (dev->flags & P2P_DEV_PROBE_REQ_ONLY) {
4994 dev = dl_list_first(&dev->list,
4995 struct p2p_device,
4996 list);
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07004997 if (!dev || &dev->list == &p2p->devices)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004998 return NULL;
4999 }
5000 }
5001
5002 return &dev->info;
5003}
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005004
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005005
5006int p2p_in_progress(struct p2p_data *p2p)
5007{
5008 if (p2p == NULL)
5009 return 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005010 if (p2p->state == P2P_SEARCH)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005011 return 2;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005012 return p2p->state != P2P_IDLE && p2p->state != P2P_PROVISIONING;
5013}
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005014
5015
5016void p2p_set_config_timeout(struct p2p_data *p2p, u8 go_timeout,
5017 u8 client_timeout)
5018{
5019 if (p2p) {
5020 p2p->go_timeout = go_timeout;
5021 p2p->client_timeout = client_timeout;
5022 }
5023}
5024
5025
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005026#ifdef CONFIG_WIFI_DISPLAY
5027
5028static void p2p_update_wfd_ie_groups(struct p2p_data *p2p)
5029{
5030 size_t g;
5031 struct p2p_group *group;
5032
5033 for (g = 0; g < p2p->num_groups; g++) {
5034 group = p2p->groups[g];
Dmitry Shmidtb96dad42013-11-05 10:07:29 -08005035 p2p_group_force_beacon_update_ies(group);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005036 }
5037}
5038
5039
5040int p2p_set_wfd_ie_beacon(struct p2p_data *p2p, struct wpabuf *ie)
5041{
5042 wpabuf_free(p2p->wfd_ie_beacon);
5043 p2p->wfd_ie_beacon = ie;
5044 p2p_update_wfd_ie_groups(p2p);
5045 return 0;
5046}
5047
5048
5049int p2p_set_wfd_ie_probe_req(struct p2p_data *p2p, struct wpabuf *ie)
5050{
5051 wpabuf_free(p2p->wfd_ie_probe_req);
5052 p2p->wfd_ie_probe_req = ie;
5053 return 0;
5054}
5055
5056
5057int p2p_set_wfd_ie_probe_resp(struct p2p_data *p2p, struct wpabuf *ie)
5058{
5059 wpabuf_free(p2p->wfd_ie_probe_resp);
5060 p2p->wfd_ie_probe_resp = ie;
5061 p2p_update_wfd_ie_groups(p2p);
5062 return 0;
5063}
5064
5065
5066int p2p_set_wfd_ie_assoc_req(struct p2p_data *p2p, struct wpabuf *ie)
5067{
5068 wpabuf_free(p2p->wfd_ie_assoc_req);
5069 p2p->wfd_ie_assoc_req = ie;
5070 return 0;
5071}
5072
5073
5074int p2p_set_wfd_ie_invitation(struct p2p_data *p2p, struct wpabuf *ie)
5075{
5076 wpabuf_free(p2p->wfd_ie_invitation);
5077 p2p->wfd_ie_invitation = ie;
5078 return 0;
5079}
5080
5081
5082int p2p_set_wfd_ie_prov_disc_req(struct p2p_data *p2p, struct wpabuf *ie)
5083{
5084 wpabuf_free(p2p->wfd_ie_prov_disc_req);
5085 p2p->wfd_ie_prov_disc_req = ie;
5086 return 0;
5087}
5088
5089
5090int p2p_set_wfd_ie_prov_disc_resp(struct p2p_data *p2p, struct wpabuf *ie)
5091{
5092 wpabuf_free(p2p->wfd_ie_prov_disc_resp);
5093 p2p->wfd_ie_prov_disc_resp = ie;
5094 return 0;
5095}
5096
5097
5098int p2p_set_wfd_ie_go_neg(struct p2p_data *p2p, struct wpabuf *ie)
5099{
5100 wpabuf_free(p2p->wfd_ie_go_neg);
5101 p2p->wfd_ie_go_neg = ie;
5102 return 0;
5103}
5104
5105
5106int p2p_set_wfd_dev_info(struct p2p_data *p2p, const struct wpabuf *elem)
5107{
5108 wpabuf_free(p2p->wfd_dev_info);
5109 if (elem) {
5110 p2p->wfd_dev_info = wpabuf_dup(elem);
5111 if (p2p->wfd_dev_info == NULL)
5112 return -1;
5113 } else
5114 p2p->wfd_dev_info = NULL;
5115
5116 return 0;
5117}
5118
5119
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005120int p2p_set_wfd_r2_dev_info(struct p2p_data *p2p, const struct wpabuf *elem)
5121{
5122 wpabuf_free(p2p->wfd_r2_dev_info);
5123 if (elem) {
5124 p2p->wfd_r2_dev_info = wpabuf_dup(elem);
5125 if (p2p->wfd_r2_dev_info == NULL)
5126 return -1;
5127 } else
5128 p2p->wfd_r2_dev_info = NULL;
5129
5130 return 0;
5131}
5132
5133
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005134int p2p_set_wfd_assoc_bssid(struct p2p_data *p2p, const struct wpabuf *elem)
5135{
5136 wpabuf_free(p2p->wfd_assoc_bssid);
5137 if (elem) {
5138 p2p->wfd_assoc_bssid = wpabuf_dup(elem);
5139 if (p2p->wfd_assoc_bssid == NULL)
5140 return -1;
5141 } else
5142 p2p->wfd_assoc_bssid = NULL;
5143
5144 return 0;
5145}
5146
5147
5148int p2p_set_wfd_coupled_sink_info(struct p2p_data *p2p,
5149 const struct wpabuf *elem)
5150{
5151 wpabuf_free(p2p->wfd_coupled_sink_info);
5152 if (elem) {
5153 p2p->wfd_coupled_sink_info = wpabuf_dup(elem);
5154 if (p2p->wfd_coupled_sink_info == NULL)
5155 return -1;
5156 } else
5157 p2p->wfd_coupled_sink_info = NULL;
5158
5159 return 0;
5160}
5161
5162#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005163
5164
5165int p2p_set_disc_int(struct p2p_data *p2p, int min_disc_int, int max_disc_int,
5166 int max_disc_tu)
5167{
5168 if (min_disc_int > max_disc_int || min_disc_int < 0 || max_disc_int < 0)
5169 return -1;
5170
5171 p2p->min_disc_int = min_disc_int;
5172 p2p->max_disc_int = max_disc_int;
5173 p2p->max_disc_tu = max_disc_tu;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005174 p2p_dbg(p2p, "Set discoverable interval: min=%d max=%d max_tu=%d",
5175 min_disc_int, max_disc_int, max_disc_tu);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005176
5177 return 0;
5178}
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005179
5180
5181void p2p_dbg(struct p2p_data *p2p, const char *fmt, ...)
5182{
5183 va_list ap;
5184 char buf[500];
5185
5186 if (!p2p->cfg->debug_print)
5187 return;
5188
5189 va_start(ap, fmt);
5190 vsnprintf(buf, sizeof(buf), fmt, ap);
5191 buf[sizeof(buf) - 1] = '\0';
5192 va_end(ap);
5193 p2p->cfg->debug_print(p2p->cfg->cb_ctx, MSG_DEBUG, buf);
5194}
5195
5196
5197void p2p_info(struct p2p_data *p2p, const char *fmt, ...)
5198{
5199 va_list ap;
5200 char buf[500];
5201
5202 if (!p2p->cfg->debug_print)
5203 return;
5204
5205 va_start(ap, fmt);
5206 vsnprintf(buf, sizeof(buf), fmt, ap);
5207 buf[sizeof(buf) - 1] = '\0';
5208 va_end(ap);
5209 p2p->cfg->debug_print(p2p->cfg->cb_ctx, MSG_INFO, buf);
5210}
5211
5212
5213void p2p_err(struct p2p_data *p2p, const char *fmt, ...)
5214{
5215 va_list ap;
5216 char buf[500];
5217
5218 if (!p2p->cfg->debug_print)
5219 return;
5220
5221 va_start(ap, fmt);
5222 vsnprintf(buf, sizeof(buf), fmt, ap);
5223 buf[sizeof(buf) - 1] = '\0';
5224 va_end(ap);
5225 p2p->cfg->debug_print(p2p->cfg->cb_ctx, MSG_ERROR, buf);
5226}
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005227
5228
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07005229void p2p_loop_on_known_peers(struct p2p_data *p2p,
5230 void (*peer_callback)(struct p2p_peer_info *peer,
5231 void *user_data),
5232 void *user_data)
5233{
5234 struct p2p_device *dev, *n;
5235
5236 dl_list_for_each_safe(dev, n, &p2p->devices, struct p2p_device, list) {
5237 peer_callback(&dev->info, user_data);
5238 }
5239}
5240
5241
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005242#ifdef CONFIG_WPS_NFC
5243
5244static struct wpabuf * p2p_build_nfc_handover(struct p2p_data *p2p,
5245 int client_freq,
5246 const u8 *go_dev_addr,
5247 const u8 *ssid, size_t ssid_len)
5248{
5249 struct wpabuf *buf;
5250 u8 op_class, channel;
5251 enum p2p_role_indication role = P2P_DEVICE_NOT_IN_GROUP;
5252
5253 buf = wpabuf_alloc(1000);
5254 if (buf == NULL)
5255 return NULL;
5256
5257 op_class = p2p->cfg->reg_class;
5258 channel = p2p->cfg->channel;
5259
5260 p2p_buf_add_capability(buf, p2p->dev_capab &
5261 ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY, 0);
5262 p2p_buf_add_device_info(buf, p2p, NULL);
5263
5264 if (p2p->num_groups > 0) {
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07005265 int freq = p2p_group_get_freq(p2p->groups[0]);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005266 role = P2P_GO_IN_A_GROUP;
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07005267 if (p2p_freq_to_channel(freq, &op_class, &channel) < 0) {
5268 p2p_dbg(p2p,
5269 "Unknown GO operating frequency %d MHz for NFC handover",
5270 freq);
5271 wpabuf_free(buf);
5272 return NULL;
5273 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005274 } else if (client_freq > 0) {
5275 role = P2P_CLIENT_IN_A_GROUP;
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07005276 if (p2p_freq_to_channel(client_freq, &op_class, &channel) < 0) {
5277 p2p_dbg(p2p,
5278 "Unknown client operating frequency %d MHz for NFC handover",
5279 client_freq);
5280 wpabuf_free(buf);
5281 return NULL;
5282 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005283 }
5284
5285 p2p_buf_add_oob_go_neg_channel(buf, p2p->cfg->country, op_class,
5286 channel, role);
5287
5288 if (p2p->num_groups > 0) {
5289 /* Limit number of clients to avoid very long message */
5290 p2p_buf_add_group_info(p2p->groups[0], buf, 5);
5291 p2p_group_buf_add_id(p2p->groups[0], buf);
5292 } else if (client_freq > 0 &&
5293 go_dev_addr && !is_zero_ether_addr(go_dev_addr) &&
5294 ssid && ssid_len > 0) {
5295 /*
5296 * Add the optional P2P Group ID to indicate in which group this
5297 * device is a P2P Client.
5298 */
5299 p2p_buf_add_group_id(buf, go_dev_addr, ssid, ssid_len);
5300 }
5301
5302 return buf;
5303}
5304
5305
5306struct wpabuf * p2p_build_nfc_handover_req(struct p2p_data *p2p,
5307 int client_freq,
5308 const u8 *go_dev_addr,
5309 const u8 *ssid, size_t ssid_len)
5310{
5311 return p2p_build_nfc_handover(p2p, client_freq, go_dev_addr, ssid,
5312 ssid_len);
5313}
5314
5315
5316struct wpabuf * p2p_build_nfc_handover_sel(struct p2p_data *p2p,
5317 int client_freq,
5318 const u8 *go_dev_addr,
5319 const u8 *ssid, size_t ssid_len)
5320{
5321 return p2p_build_nfc_handover(p2p, client_freq, go_dev_addr, ssid,
5322 ssid_len);
5323}
5324
5325
5326int p2p_process_nfc_connection_handover(struct p2p_data *p2p,
5327 struct p2p_nfc_params *params)
5328{
5329 struct p2p_message msg;
5330 struct p2p_device *dev;
5331 const u8 *p2p_dev_addr;
5332 int freq;
5333 enum p2p_role_indication role;
5334
5335 params->next_step = NO_ACTION;
5336
5337 if (p2p_parse_ies_separate(params->wsc_attr, params->wsc_len,
5338 params->p2p_attr, params->p2p_len, &msg)) {
5339 p2p_dbg(p2p, "Failed to parse WSC/P2P attributes from NFC");
5340 p2p_parse_free(&msg);
5341 return -1;
5342 }
5343
5344 if (msg.p2p_device_addr)
5345 p2p_dev_addr = msg.p2p_device_addr;
5346 else if (msg.device_id)
5347 p2p_dev_addr = msg.device_id;
5348 else {
5349 p2p_dbg(p2p, "Ignore scan data without P2P Device Info or P2P Device Id");
5350 p2p_parse_free(&msg);
5351 return -1;
5352 }
5353
5354 if (msg.oob_dev_password) {
5355 os_memcpy(params->oob_dev_pw, msg.oob_dev_password,
5356 msg.oob_dev_password_len);
5357 params->oob_dev_pw_len = msg.oob_dev_password_len;
5358 }
5359
5360 dev = p2p_create_device(p2p, p2p_dev_addr);
5361 if (dev == NULL) {
5362 p2p_parse_free(&msg);
5363 return -1;
5364 }
5365
5366 params->peer = &dev->info;
5367
5368 os_get_reltime(&dev->last_seen);
5369 dev->flags &= ~(P2P_DEV_PROBE_REQ_ONLY | P2P_DEV_GROUP_CLIENT_ONLY);
5370 p2p_copy_wps_info(p2p, dev, 0, &msg);
5371
5372 if (!msg.oob_go_neg_channel) {
5373 p2p_dbg(p2p, "OOB GO Negotiation Channel attribute not included");
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07005374 p2p_parse_free(&msg);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005375 return -1;
5376 }
5377
5378 if (msg.oob_go_neg_channel[3] == 0 &&
5379 msg.oob_go_neg_channel[4] == 0)
5380 freq = 0;
5381 else
5382 freq = p2p_channel_to_freq(msg.oob_go_neg_channel[3],
5383 msg.oob_go_neg_channel[4]);
5384 if (freq < 0) {
5385 p2p_dbg(p2p, "Unknown peer OOB GO Neg channel");
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07005386 p2p_parse_free(&msg);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005387 return -1;
5388 }
5389 role = msg.oob_go_neg_channel[5];
5390
5391 if (role == P2P_GO_IN_A_GROUP) {
5392 p2p_dbg(p2p, "Peer OOB GO operating channel: %u MHz", freq);
5393 params->go_freq = freq;
5394 } else if (role == P2P_CLIENT_IN_A_GROUP) {
5395 p2p_dbg(p2p, "Peer (client) OOB GO operating channel: %u MHz",
5396 freq);
5397 params->go_freq = freq;
5398 } else
5399 p2p_dbg(p2p, "Peer OOB GO Neg channel: %u MHz", freq);
5400 dev->oob_go_neg_freq = freq;
5401
5402 if (!params->sel && role != P2P_GO_IN_A_GROUP) {
5403 freq = p2p_channel_to_freq(p2p->cfg->reg_class,
5404 p2p->cfg->channel);
5405 if (freq < 0) {
5406 p2p_dbg(p2p, "Own listen channel not known");
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07005407 p2p_parse_free(&msg);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005408 return -1;
5409 }
5410 p2p_dbg(p2p, "Use own Listen channel as OOB GO Neg channel: %u MHz", freq);
5411 dev->oob_go_neg_freq = freq;
5412 }
5413
5414 if (msg.group_id) {
5415 os_memcpy(params->go_dev_addr, msg.group_id, ETH_ALEN);
5416 params->go_ssid_len = msg.group_id_len - ETH_ALEN;
5417 os_memcpy(params->go_ssid, msg.group_id + ETH_ALEN,
5418 params->go_ssid_len);
5419 }
5420
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005421 if (dev->flags & P2P_DEV_USER_REJECTED) {
5422 p2p_dbg(p2p, "Do not report rejected device");
Dmitry Shmidt71757432014-06-02 13:50:35 -07005423 p2p_parse_free(&msg);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005424 return 0;
5425 }
5426
5427 if (!(dev->flags & P2P_DEV_REPORTED)) {
5428 p2p->cfg->dev_found(p2p->cfg->cb_ctx, p2p_dev_addr, &dev->info,
5429 !(dev->flags & P2P_DEV_REPORTED_ONCE));
5430 dev->flags |= P2P_DEV_REPORTED | P2P_DEV_REPORTED_ONCE;
5431 }
Dmitry Shmidt71757432014-06-02 13:50:35 -07005432 p2p_parse_free(&msg);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005433
5434 if (role == P2P_GO_IN_A_GROUP && p2p->num_groups > 0)
5435 params->next_step = BOTH_GO;
5436 else if (role == P2P_GO_IN_A_GROUP)
5437 params->next_step = JOIN_GROUP;
5438 else if (role == P2P_CLIENT_IN_A_GROUP) {
5439 dev->flags |= P2P_DEV_GROUP_CLIENT_ONLY;
5440 params->next_step = PEER_CLIENT;
5441 } else if (p2p->num_groups > 0)
5442 params->next_step = AUTH_JOIN;
5443 else if (params->sel)
5444 params->next_step = INIT_GO_NEG;
5445 else
5446 params->next_step = RESP_GO_NEG;
5447
5448 return 0;
5449}
5450
5451
5452void p2p_set_authorized_oob_dev_pw_id(struct p2p_data *p2p, u16 dev_pw_id,
5453 int go_intent,
5454 const u8 *own_interface_addr)
5455{
5456
5457 p2p->authorized_oob_dev_pw_id = dev_pw_id;
5458 if (dev_pw_id == 0) {
5459 p2p_dbg(p2p, "NFC OOB Password unauthorized for static handover");
5460 return;
5461 }
5462
5463 p2p_dbg(p2p, "NFC OOB Password (id=%u) authorized for static handover",
5464 dev_pw_id);
5465
5466 p2p->go_intent = go_intent;
5467 os_memcpy(p2p->intended_addr, own_interface_addr, ETH_ALEN);
5468}
5469
5470#endif /* CONFIG_WPS_NFC */
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07005471
5472
5473int p2p_set_passphrase_len(struct p2p_data *p2p, unsigned int len)
5474{
5475 if (len < 8 || len > 63)
5476 return -1;
5477 p2p->cfg->passphrase_len = len;
5478 return 0;
5479}
Dmitry Shmidt2e67f062014-07-16 09:55:28 -07005480
5481
5482void p2p_set_vendor_elems(struct p2p_data *p2p, struct wpabuf **vendor_elem)
5483{
5484 p2p->vendor_elem = vendor_elem;
5485}
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005486
5487
5488void p2p_go_neg_wait_timeout(void *eloop_ctx, void *timeout_ctx)
5489{
5490 struct p2p_data *p2p = eloop_ctx;
5491
5492 p2p_dbg(p2p,
5493 "Timeout on waiting peer to become ready for GO Negotiation");
5494 p2p_go_neg_failed(p2p, -1);
5495}
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005496
5497
5498void p2p_set_own_pref_freq_list(struct p2p_data *p2p,
5499 const unsigned int *pref_freq_list,
5500 unsigned int size)
5501{
5502 unsigned int i;
5503
5504 if (size > P2P_MAX_PREF_CHANNELS)
5505 size = P2P_MAX_PREF_CHANNELS;
5506 p2p->num_pref_freq = size;
5507 for (i = 0; i < size; i++) {
5508 p2p->pref_freq_list[i] = pref_freq_list[i];
5509 p2p_dbg(p2p, "Own preferred frequency list[%u]=%u MHz",
5510 i, p2p->pref_freq_list[i]);
5511 }
5512}
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07005513
5514
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08005515void p2p_set_override_pref_op_chan(struct p2p_data *p2p, u8 op_class,
5516 u8 chan)
5517{
5518 p2p->override_pref_op_class = op_class;
5519 p2p->override_pref_channel = chan;
5520}
5521
5522
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07005523struct wpabuf * p2p_build_probe_resp_template(struct p2p_data *p2p,
5524 unsigned int freq)
5525{
5526 struct wpabuf *ies, *buf;
5527 u8 addr[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
5528 int ret;
5529
5530 ies = p2p_build_probe_resp_ies(p2p, NULL, 0);
5531 if (!ies) {
5532 wpa_printf(MSG_ERROR,
5533 "CTRL: Failed to build Probe Response IEs");
5534 return NULL;
5535 }
5536
5537 buf = wpabuf_alloc(200 + wpabuf_len(ies));
5538 if (!buf) {
5539 wpabuf_free(ies);
5540 return NULL;
5541 }
5542
5543 ret = p2p_build_probe_resp_buf(p2p, buf, ies, addr, freq);
5544 wpabuf_free(ies);
5545 if (ret) {
5546 wpabuf_free(buf);
5547 return NULL;
5548 }
5549
5550 return buf;
5551}