blob: 65dd1a32d210be8c4f575e12c14b0b0bf593363d [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 Shmidt04949592012-07-19 12:16:46 -07001013 if (p2p->find_type == P2P_FIND_PROGRESSIVE &&
1014 (freq = p2p_get_next_prog_freq(p2p)) > 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001015 type = P2P_SCAN_SOCIAL_PLUS_ONE;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001016 p2p_dbg(p2p, "Starting search (+ freq %u)", freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001017 } else {
1018 type = P2P_SCAN_SOCIAL;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001019 p2p_dbg(p2p, "Starting search");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001020 }
1021
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001022 res = p2p->cfg->p2p_scan(p2p->cfg->cb_ctx, type, freq,
1023 p2p->num_req_dev_types, p2p->req_dev_types,
1024 p2p->find_dev_id, pw_id);
1025 if (res < 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001026 p2p_dbg(p2p, "Scan request schedule failed");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001027 p2p_continue_find(p2p);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001028 }
1029}
1030
1031
1032static void p2p_find_timeout(void *eloop_ctx, void *timeout_ctx)
1033{
1034 struct p2p_data *p2p = eloop_ctx;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001035 p2p_dbg(p2p, "Find timeout -> stop");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001036 p2p_stop_find(p2p);
1037}
1038
1039
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001040void p2p_notify_scan_trigger_status(struct p2p_data *p2p, int status)
1041{
1042 if (status != 0) {
1043 p2p_dbg(p2p, "Scan request failed");
1044 /* Do continue find even for the first p2p_find_scan */
1045 p2p_continue_find(p2p);
1046 } else {
1047 p2p_dbg(p2p, "Running p2p_scan");
1048 p2p->p2p_scan_running = 1;
1049 eloop_cancel_timeout(p2p_scan_timeout, p2p, NULL);
1050 eloop_register_timeout(P2P_SCAN_TIMEOUT, 0, p2p_scan_timeout,
1051 p2p, NULL);
1052 }
1053}
1054
1055
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001056static int p2p_run_after_scan(struct p2p_data *p2p)
1057{
1058 struct p2p_device *dev;
1059 enum p2p_after_scan op;
1060
1061 if (p2p->after_scan_tx) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07001062 p2p->after_scan_tx_in_progress = 1;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001063 p2p_dbg(p2p, "Send pending Action frame at p2p_scan completion");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001064 p2p->cfg->send_action(p2p->cfg->cb_ctx,
1065 p2p->after_scan_tx->freq,
1066 p2p->after_scan_tx->dst,
1067 p2p->after_scan_tx->src,
1068 p2p->after_scan_tx->bssid,
1069 (u8 *) (p2p->after_scan_tx + 1),
1070 p2p->after_scan_tx->len,
1071 p2p->after_scan_tx->wait_time);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001072 os_free(p2p->after_scan_tx);
1073 p2p->after_scan_tx = NULL;
1074 return 1;
1075 }
1076
1077 op = p2p->start_after_scan;
1078 p2p->start_after_scan = P2P_AFTER_SCAN_NOTHING;
1079 switch (op) {
1080 case P2P_AFTER_SCAN_NOTHING:
1081 break;
1082 case P2P_AFTER_SCAN_LISTEN:
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001083 p2p_dbg(p2p, "Start previously requested Listen state");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001084 p2p_listen(p2p, p2p->pending_listen_sec * 1000 +
1085 p2p->pending_listen_usec / 1000);
1086 return 1;
1087 case P2P_AFTER_SCAN_CONNECT:
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001088 p2p_dbg(p2p, "Start previously requested connect with " MACSTR,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001089 MAC2STR(p2p->after_scan_peer));
1090 dev = p2p_get_device(p2p, p2p->after_scan_peer);
1091 if (dev == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001092 p2p_dbg(p2p, "Peer not known anymore");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001093 break;
1094 }
1095 p2p_connect_send(p2p, dev);
1096 return 1;
1097 }
1098
1099 return 0;
1100}
1101
1102
1103static void p2p_scan_timeout(void *eloop_ctx, void *timeout_ctx)
1104{
1105 struct p2p_data *p2p = eloop_ctx;
1106 int running;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001107 p2p_dbg(p2p, "p2p_scan timeout (running=%d)", p2p->p2p_scan_running);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001108 running = p2p->p2p_scan_running;
1109 /* Make sure we recover from missed scan results callback */
1110 p2p->p2p_scan_running = 0;
1111
1112 if (running)
1113 p2p_run_after_scan(p2p);
1114}
1115
1116
1117static void p2p_free_req_dev_types(struct p2p_data *p2p)
1118{
1119 p2p->num_req_dev_types = 0;
1120 os_free(p2p->req_dev_types);
1121 p2p->req_dev_types = NULL;
1122}
1123
1124
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001125static int p2ps_gen_hash(struct p2p_data *p2p, const char *str, u8 *hash)
1126{
1127 u8 buf[SHA256_MAC_LEN];
1128 char str_buf[256];
1129 const u8 *adv_array;
1130 size_t i, adv_len;
1131
1132 if (!str || !hash)
1133 return 0;
1134
1135 if (!str[0]) {
1136 os_memcpy(hash, p2p->wild_card_hash, P2PS_HASH_LEN);
1137 return 1;
1138 }
1139
1140 adv_array = (u8 *) str_buf;
1141 adv_len = os_strlen(str);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07001142 if (adv_len >= sizeof(str_buf))
1143 return 0;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001144
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001145 for (i = 0; i < adv_len; i++) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001146 if (str[i] >= 'A' && str[i] <= 'Z')
1147 str_buf[i] = str[i] - 'A' + 'a';
1148 else
1149 str_buf[i] = str[i];
1150 }
1151
1152 if (sha256_vector(1, &adv_array, &adv_len, buf))
1153 return 0;
1154
1155 os_memcpy(hash, buf, P2PS_HASH_LEN);
1156 return 1;
1157}
1158
1159
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001160int p2p_find(struct p2p_data *p2p, unsigned int timeout,
1161 enum p2p_discovery_type type,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001162 unsigned int num_req_dev_types, const u8 *req_dev_types,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001163 const u8 *dev_id, unsigned int search_delay,
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001164 u8 seek_count, const char **seek, int freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001165{
1166 int res;
1167
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001168 p2p_dbg(p2p, "Starting find (type=%d)", type);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001169 os_get_reltime(&p2p->find_start);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001170 if (p2p->p2p_scan_running) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001171 p2p_dbg(p2p, "p2p_scan is already running");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001172 }
1173
1174 p2p_free_req_dev_types(p2p);
1175 if (req_dev_types && num_req_dev_types) {
1176 p2p->req_dev_types = os_malloc(num_req_dev_types *
1177 WPS_DEV_TYPE_LEN);
1178 if (p2p->req_dev_types == NULL)
1179 return -1;
1180 os_memcpy(p2p->req_dev_types, req_dev_types,
1181 num_req_dev_types * WPS_DEV_TYPE_LEN);
1182 p2p->num_req_dev_types = num_req_dev_types;
1183 }
1184
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001185 if (dev_id) {
1186 os_memcpy(p2p->find_dev_id_buf, dev_id, ETH_ALEN);
1187 p2p->find_dev_id = p2p->find_dev_id_buf;
1188 } else
1189 p2p->find_dev_id = NULL;
1190
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001191 if (seek_count == 0 || !seek) {
1192 /* Not an ASP search */
1193 p2p->p2ps_seek = 0;
1194 } else if (seek_count == 1 && seek && (!seek[0] || !seek[0][0])) {
1195 /*
1196 * An empty seek string means no hash values, but still an ASP
1197 * search.
1198 */
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07001199 p2p_dbg(p2p, "ASP search");
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001200 p2p->p2ps_seek_count = 0;
1201 p2p->p2ps_seek = 1;
1202 } else if (seek && seek_count <= P2P_MAX_QUERY_HASH) {
1203 u8 buf[P2PS_HASH_LEN];
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07001204 int i, count = 0;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001205
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001206 for (i = 0; i < seek_count; i++) {
1207 if (!p2ps_gen_hash(p2p, seek[i], buf))
1208 continue;
1209
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07001210 p2p_dbg(p2p, "Seek service %s hash " MACSTR,
1211 seek[i], MAC2STR(buf));
1212 os_memcpy(&p2p->p2ps_seek_hash[count * P2PS_HASH_LEN],
1213 buf, P2PS_HASH_LEN);
1214 count++;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001215 }
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07001216
1217 p2p->p2ps_seek_count = count;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001218 p2p->p2ps_seek = 1;
1219 } else {
1220 p2p->p2ps_seek_count = 0;
1221 p2p->p2ps_seek = 1;
1222 }
1223
1224 /* Special case to perform wildcard search */
1225 if (p2p->p2ps_seek_count == 0 && p2p->p2ps_seek) {
1226 p2p->p2ps_seek_count = 1;
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07001227 os_memcpy(&p2p->p2ps_seek_hash, p2p->wild_card_hash,
1228 P2PS_HASH_LEN);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001229 }
1230
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001231 p2p->start_after_scan = P2P_AFTER_SCAN_NOTHING;
1232 p2p_clear_timeout(p2p);
Dmitry Shmidtde47be72016-01-07 12:52:55 -08001233 if (p2p->pending_listen_freq) {
1234 p2p_dbg(p2p, "Clear pending_listen_freq for p2p_find");
1235 p2p->pending_listen_freq = 0;
1236 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001237 p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
1238 p2p->find_type = type;
1239 p2p_device_clear_reported(p2p);
Dmitry Shmidtde47be72016-01-07 12:52:55 -08001240 os_memset(p2p->sd_query_no_ack, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001241 p2p_set_state(p2p, P2P_SEARCH);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001242 p2p->search_delay = search_delay;
1243 p2p->in_search_delay = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001244 eloop_cancel_timeout(p2p_find_timeout, p2p, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001245 p2p->last_p2p_find_timeout = timeout;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001246 if (timeout)
1247 eloop_register_timeout(timeout, 0, p2p_find_timeout,
1248 p2p, NULL);
1249 switch (type) {
1250 case P2P_FIND_START_WITH_FULL:
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001251 if (freq > 0) {
1252 /*
1253 * Start with the specified channel and then move to
1254 * social channels only scans.
1255 */
1256 res = p2p->cfg->p2p_scan(p2p->cfg->cb_ctx,
1257 P2P_SCAN_SPECIFIC, freq,
1258 p2p->num_req_dev_types,
1259 p2p->req_dev_types, dev_id,
1260 DEV_PW_DEFAULT);
1261 break;
1262 }
1263 /* fall through */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001264 case P2P_FIND_PROGRESSIVE:
1265 res = p2p->cfg->p2p_scan(p2p->cfg->cb_ctx, P2P_SCAN_FULL, 0,
1266 p2p->num_req_dev_types,
Dmitry Shmidt04949592012-07-19 12:16:46 -07001267 p2p->req_dev_types, dev_id,
1268 DEV_PW_DEFAULT);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001269 break;
1270 case P2P_FIND_ONLY_SOCIAL:
1271 res = p2p->cfg->p2p_scan(p2p->cfg->cb_ctx, P2P_SCAN_SOCIAL, 0,
1272 p2p->num_req_dev_types,
Dmitry Shmidt04949592012-07-19 12:16:46 -07001273 p2p->req_dev_types, dev_id,
1274 DEV_PW_DEFAULT);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001275 break;
1276 default:
1277 return -1;
1278 }
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 Shmidt96be6222014-02-13 10:16:51 -08001283 res = 0; /* do not report failure */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001284 } else if (res != 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001285 p2p_dbg(p2p, "Failed to start p2p_scan");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001286 p2p_set_state(p2p, P2P_IDLE);
1287 eloop_cancel_timeout(p2p_find_timeout, p2p, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001288 }
1289
1290 return res;
1291}
1292
1293
1294void p2p_stop_find_for_freq(struct p2p_data *p2p, int freq)
1295{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001296 p2p_dbg(p2p, "Stopping find");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001297 eloop_cancel_timeout(p2p_find_timeout, p2p, NULL);
1298 p2p_clear_timeout(p2p);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001299 if (p2p->state == P2P_SEARCH || p2p->state == P2P_SD_DURING_FIND)
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001300 p2p->cfg->find_stopped(p2p->cfg->cb_ctx);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001301
1302 p2p->p2ps_seek_count = 0;
1303
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001304 p2p_set_state(p2p, P2P_IDLE);
1305 p2p_free_req_dev_types(p2p);
1306 p2p->start_after_scan = P2P_AFTER_SCAN_NOTHING;
Dmitry Shmidt8c652892013-03-01 10:14:01 -08001307 if (p2p->go_neg_peer)
1308 p2p->go_neg_peer->flags &= ~P2P_DEV_PEER_WAITING_RESPONSE;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001309 p2p->go_neg_peer = NULL;
1310 p2p->sd_peer = NULL;
1311 p2p->invite_peer = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001312 p2p_stop_listen_for_freq(p2p, freq);
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001313 p2p->send_action_in_progress = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001314}
1315
1316
1317void p2p_stop_listen_for_freq(struct p2p_data *p2p, int freq)
1318{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001319 if (freq > 0 && p2p->drv_in_listen == freq && p2p->in_listen) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001320 p2p_dbg(p2p, "Skip stop_listen since we are on correct channel for response");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001321 return;
1322 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001323 if (p2p->in_listen) {
1324 p2p->in_listen = 0;
1325 p2p_clear_timeout(p2p);
1326 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001327 if (p2p->drv_in_listen) {
1328 /*
1329 * The driver may not deliver callback to p2p_listen_end()
1330 * when the operation gets canceled, so clear the internal
1331 * variable that is tracking driver state.
1332 */
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001333 p2p_dbg(p2p, "Clear drv_in_listen (%d)", p2p->drv_in_listen);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001334 p2p->drv_in_listen = 0;
1335 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001336 p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
1337}
1338
1339
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001340void p2p_stop_listen(struct p2p_data *p2p)
1341{
1342 if (p2p->state != P2P_LISTEN_ONLY) {
1343 p2p_dbg(p2p, "Skip stop_listen since not in listen_only state.");
1344 return;
1345 }
1346
1347 p2p_stop_listen_for_freq(p2p, 0);
1348 p2p_set_state(p2p, P2P_IDLE);
1349}
1350
1351
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001352void p2p_stop_find(struct p2p_data *p2p)
1353{
Dmitry Shmidt3c57b3f2014-05-22 15:13:07 -07001354 p2p->pending_listen_freq = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001355 p2p_stop_find_for_freq(p2p, 0);
1356}
1357
1358
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001359static int p2p_prepare_channel_pref(struct p2p_data *p2p,
1360 unsigned int force_freq,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001361 unsigned int pref_freq, int go)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001362{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001363 u8 op_class, op_channel;
1364 unsigned int freq = force_freq ? force_freq : pref_freq;
1365
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001366 p2p_dbg(p2p, "Prepare channel pref - force_freq=%u pref_freq=%u go=%d",
1367 force_freq, pref_freq, go);
Dmitry Shmidt4b060592013-04-29 16:42:49 -07001368 if (p2p_freq_to_channel(freq, &op_class, &op_channel) < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001369 p2p_dbg(p2p, "Unsupported frequency %u MHz", freq);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001370 return -1;
1371 }
1372
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001373 if (!p2p_channels_includes(&p2p->cfg->channels, op_class, op_channel) &&
1374 (go || !p2p_channels_includes(&p2p->cfg->cli_channels, op_class,
1375 op_channel))) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001376 p2p_dbg(p2p, "Frequency %u MHz (oper_class %u channel %u) not allowed for P2P",
1377 freq, op_class, op_channel);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001378 return -1;
1379 }
1380
1381 p2p->op_reg_class = op_class;
1382 p2p->op_channel = op_channel;
1383
1384 if (force_freq) {
1385 p2p->channels.reg_classes = 1;
1386 p2p->channels.reg_class[0].channels = 1;
1387 p2p->channels.reg_class[0].reg_class = p2p->op_reg_class;
1388 p2p->channels.reg_class[0].channel[0] = p2p->op_channel;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001389 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001390 os_memcpy(&p2p->channels, &p2p->cfg->channels,
1391 sizeof(struct p2p_channels));
1392 }
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001393
1394 return 0;
1395}
1396
1397
1398static void p2p_prepare_channel_best(struct p2p_data *p2p)
1399{
1400 u8 op_class, op_channel;
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07001401 const int op_classes_5ghz[] = { 124, 125, 115, 0 };
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08001402 const int op_classes_ht40[] = { 126, 127, 116, 117, 0 };
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001403 const int op_classes_vht[] = { 128, 0 };
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001404
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07001405 p2p_dbg(p2p, "Prepare channel best");
1406
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001407 if (!p2p->cfg->cfg_op_channel && p2p->best_freq_overall > 0 &&
1408 p2p_supported_freq(p2p, p2p->best_freq_overall) &&
Dmitry Shmidt4b060592013-04-29 16:42:49 -07001409 p2p_freq_to_channel(p2p->best_freq_overall, &op_class, &op_channel)
1410 == 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001411 p2p_dbg(p2p, "Select best overall channel as operating channel preference");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001412 p2p->op_reg_class = op_class;
1413 p2p->op_channel = op_channel;
1414 } else if (!p2p->cfg->cfg_op_channel && p2p->best_freq_5 > 0 &&
1415 p2p_supported_freq(p2p, p2p->best_freq_5) &&
Dmitry Shmidt4b060592013-04-29 16:42:49 -07001416 p2p_freq_to_channel(p2p->best_freq_5, &op_class, &op_channel)
1417 == 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001418 p2p_dbg(p2p, "Select best 5 GHz channel as operating channel preference");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001419 p2p->op_reg_class = op_class;
1420 p2p->op_channel = op_channel;
1421 } else if (!p2p->cfg->cfg_op_channel && p2p->best_freq_24 > 0 &&
1422 p2p_supported_freq(p2p, p2p->best_freq_24) &&
Dmitry Shmidt4b060592013-04-29 16:42:49 -07001423 p2p_freq_to_channel(p2p->best_freq_24, &op_class,
1424 &op_channel) == 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001425 p2p_dbg(p2p, "Select best 2.4 GHz channel as operating channel preference");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001426 p2p->op_reg_class = op_class;
1427 p2p->op_channel = op_channel;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07001428 } else if (p2p->cfg->num_pref_chan > 0 &&
1429 p2p_channels_includes(&p2p->cfg->channels,
1430 p2p->cfg->pref_chan[0].op_class,
1431 p2p->cfg->pref_chan[0].chan)) {
1432 p2p_dbg(p2p, "Select first pref_chan entry as operating channel preference");
1433 p2p->op_reg_class = p2p->cfg->pref_chan[0].op_class;
1434 p2p->op_channel = p2p->cfg->pref_chan[0].chan;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001435 } else if (p2p_channel_select(&p2p->cfg->channels, op_classes_vht,
1436 &p2p->op_reg_class, &p2p->op_channel) ==
1437 0) {
1438 p2p_dbg(p2p, "Select possible VHT channel (op_class %u channel %u) as operating channel preference",
1439 p2p->op_reg_class, p2p->op_channel);
1440 } else if (p2p_channel_select(&p2p->cfg->channels, op_classes_ht40,
1441 &p2p->op_reg_class, &p2p->op_channel) ==
1442 0) {
1443 p2p_dbg(p2p, "Select possible HT40 channel (op_class %u channel %u) as operating channel preference",
1444 p2p->op_reg_class, p2p->op_channel);
1445 } else if (p2p_channel_select(&p2p->cfg->channels, op_classes_5ghz,
1446 &p2p->op_reg_class, &p2p->op_channel) ==
1447 0) {
1448 p2p_dbg(p2p, "Select possible 5 GHz channel (op_class %u channel %u) as operating channel preference",
1449 p2p->op_reg_class, p2p->op_channel);
Dmitry Shmidtd11f0192014-03-24 12:09:47 -07001450 } else if (p2p_channels_includes(&p2p->cfg->channels,
1451 p2p->cfg->op_reg_class,
1452 p2p->cfg->op_channel)) {
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07001453 p2p_dbg(p2p, "Select pre-configured channel as operating channel preference");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001454 p2p->op_reg_class = p2p->cfg->op_reg_class;
1455 p2p->op_channel = p2p->cfg->op_channel;
Dmitry Shmidtd11f0192014-03-24 12:09:47 -07001456 } else if (p2p_channel_random_social(&p2p->cfg->channels,
1457 &p2p->op_reg_class,
1458 &p2p->op_channel) == 0) {
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001459 p2p_dbg(p2p, "Select random available social channel (op_class %u channel %u) as operating channel preference",
1460 p2p->op_reg_class, p2p->op_channel);
Dmitry Shmidtd11f0192014-03-24 12:09:47 -07001461 } else {
1462 /* Select any random available channel from the first available
1463 * operating class */
1464 p2p_channel_select(&p2p->cfg->channels, NULL,
1465 &p2p->op_reg_class,
1466 &p2p->op_channel);
1467 p2p_dbg(p2p, "Select random available channel %d from operating class %d as operating channel preference",
1468 p2p->op_channel, p2p->op_reg_class);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001469 }
1470
1471 os_memcpy(&p2p->channels, &p2p->cfg->channels,
1472 sizeof(struct p2p_channels));
1473}
1474
1475
1476/**
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001477 * p2p_prepare_channel - Select operating channel for GO Negotiation or P2PS PD
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001478 * @p2p: P2P module context from p2p_init()
1479 * @dev: Selected peer device
1480 * @force_freq: Forced frequency in MHz or 0 if not forced
1481 * @pref_freq: Preferred frequency in MHz or 0 if no preference
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001482 * @go: Whether the local end will be forced to be GO
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001483 * Returns: 0 on success, -1 on failure (channel not supported for P2P)
1484 *
1485 * This function is used to do initial operating channel selection for GO
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001486 * Negotiation prior to having received peer information or for P2PS PD
1487 * signalling. The selected channel may be further optimized in
1488 * p2p_reselect_channel() once the peer information is available.
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001489 */
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08001490int p2p_prepare_channel(struct p2p_data *p2p, struct p2p_device *dev,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001491 unsigned int force_freq, unsigned int pref_freq, int go)
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001492{
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001493 p2p_dbg(p2p, "Prepare channel - force_freq=%u pref_freq=%u go=%d",
1494 force_freq, pref_freq, go);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001495 if (force_freq || pref_freq) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001496 if (p2p_prepare_channel_pref(p2p, force_freq, pref_freq, go) <
1497 0)
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001498 return -1;
1499 } else {
1500 p2p_prepare_channel_best(p2p);
1501 }
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001502 p2p_channels_dump(p2p, "prepared channels", &p2p->channels);
1503 if (go)
1504 p2p_channels_remove_freqs(&p2p->channels, &p2p->no_go_freq);
1505 else if (!force_freq)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001506 p2p_channels_union_inplace(&p2p->channels,
1507 &p2p->cfg->cli_channels);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001508 p2p_channels_dump(p2p, "after go/cli filter/add", &p2p->channels);
1509
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001510 p2p_dbg(p2p, "Own preference for operation channel: Operating Class %u Channel %u%s",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001511 p2p->op_reg_class, p2p->op_channel,
1512 force_freq ? " (forced)" : "");
1513
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001514 if (force_freq)
1515 dev->flags |= P2P_DEV_FORCE_FREQ;
1516 else
1517 dev->flags &= ~P2P_DEV_FORCE_FREQ;
1518
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001519 return 0;
1520}
1521
1522
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001523static void p2p_set_dev_persistent(struct p2p_device *dev,
1524 int persistent_group)
1525{
1526 switch (persistent_group) {
1527 case 0:
1528 dev->flags &= ~(P2P_DEV_PREFER_PERSISTENT_GROUP |
1529 P2P_DEV_PREFER_PERSISTENT_RECONN);
1530 break;
1531 case 1:
1532 dev->flags |= P2P_DEV_PREFER_PERSISTENT_GROUP;
1533 dev->flags &= ~P2P_DEV_PREFER_PERSISTENT_RECONN;
1534 break;
1535 case 2:
1536 dev->flags |= P2P_DEV_PREFER_PERSISTENT_GROUP |
1537 P2P_DEV_PREFER_PERSISTENT_RECONN;
1538 break;
1539 }
1540}
1541
1542
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001543int p2p_connect(struct p2p_data *p2p, const u8 *peer_addr,
1544 enum p2p_wps_method wps_method,
1545 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07001546 unsigned int force_freq, int persistent_group,
1547 const u8 *force_ssid, size_t force_ssid_len,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001548 int pd_before_go_neg, unsigned int pref_freq, u16 oob_pw_id)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001549{
1550 struct p2p_device *dev;
1551
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001552 p2p_dbg(p2p, "Request to start group negotiation - peer=" MACSTR
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001553 " GO Intent=%d Intended Interface Address=" MACSTR
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001554 " wps_method=%d persistent_group=%d pd_before_go_neg=%d "
1555 "oob_pw_id=%u",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001556 MAC2STR(peer_addr), go_intent, MAC2STR(own_interface_addr),
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001557 wps_method, persistent_group, pd_before_go_neg, oob_pw_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001558
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001559 dev = p2p_get_device(p2p, peer_addr);
1560 if (dev == NULL || (dev->flags & P2P_DEV_PROBE_REQ_ONLY)) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001561 p2p_dbg(p2p, "Cannot connect to unknown P2P Device " MACSTR,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001562 MAC2STR(peer_addr));
1563 return -1;
1564 }
1565
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001566 if (p2p_prepare_channel(p2p, dev, force_freq, pref_freq,
1567 go_intent == 15) < 0)
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001568 return -1;
1569
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001570 if (dev->flags & P2P_DEV_GROUP_CLIENT_ONLY) {
1571 if (!(dev->info.dev_capab &
1572 P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY)) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001573 p2p_dbg(p2p, "Cannot connect to P2P Device " MACSTR
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001574 " that is in a group and is not discoverable",
1575 MAC2STR(peer_addr));
1576 return -1;
1577 }
1578 if (dev->oper_freq <= 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001579 p2p_dbg(p2p, "Cannot connect to P2P Device " MACSTR
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001580 " with incomplete information",
1581 MAC2STR(peer_addr));
1582 return -1;
1583 }
1584
1585 /*
1586 * First, try to connect directly. If the peer does not
1587 * acknowledge frames, assume it is sleeping and use device
1588 * discoverability via the GO at that point.
1589 */
1590 }
1591
Dmitry Shmidt04949592012-07-19 12:16:46 -07001592 p2p->ssid_set = 0;
1593 if (force_ssid) {
1594 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Forced SSID",
1595 force_ssid, force_ssid_len);
1596 os_memcpy(p2p->ssid, force_ssid, force_ssid_len);
1597 p2p->ssid_len = force_ssid_len;
1598 p2p->ssid_set = 1;
1599 }
1600
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001601 dev->flags &= ~P2P_DEV_NOT_YET_READY;
1602 dev->flags &= ~P2P_DEV_USER_REJECTED;
1603 dev->flags &= ~P2P_DEV_WAIT_GO_NEG_RESPONSE;
1604 dev->flags &= ~P2P_DEV_WAIT_GO_NEG_CONFIRM;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001605 if (pd_before_go_neg)
1606 dev->flags |= P2P_DEV_PD_BEFORE_GO_NEG;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001607 else {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001608 dev->flags &= ~P2P_DEV_PD_BEFORE_GO_NEG;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001609 /*
Dmitry Shmidt9cdf1b92013-02-27 12:58:50 -08001610 * Assign dialog token and tie breaker here to use the same
1611 * values in each retry within the same GO Negotiation exchange.
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001612 */
1613 dev->dialog_token++;
1614 if (dev->dialog_token == 0)
1615 dev->dialog_token = 1;
Dmitry Shmidt9cdf1b92013-02-27 12:58:50 -08001616 dev->tie_breaker = p2p->next_tie_breaker;
1617 p2p->next_tie_breaker = !p2p->next_tie_breaker;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001618 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001619 dev->connect_reqs = 0;
1620 dev->go_neg_req_sent = 0;
1621 dev->go_state = UNKNOWN_GO;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001622 p2p_set_dev_persistent(dev, persistent_group);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001623 p2p->go_intent = go_intent;
1624 os_memcpy(p2p->intended_addr, own_interface_addr, ETH_ALEN);
1625
1626 if (p2p->state != P2P_IDLE)
1627 p2p_stop_find(p2p);
1628
1629 if (p2p->after_scan_tx) {
1630 /*
1631 * We need to drop the pending frame to avoid issues with the
1632 * new GO Negotiation, e.g., when the pending frame was from a
1633 * previous attempt at starting a GO Negotiation.
1634 */
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001635 p2p_dbg(p2p, "Dropped previous pending Action frame TX that was waiting for p2p_scan completion");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001636 os_free(p2p->after_scan_tx);
1637 p2p->after_scan_tx = NULL;
1638 }
1639
1640 dev->wps_method = wps_method;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001641 dev->oob_pw_id = oob_pw_id;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001642 dev->status = P2P_SC_SUCCESS;
1643
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001644 if (p2p->p2p_scan_running) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001645 p2p_dbg(p2p, "p2p_scan running - delay connect send");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001646 p2p->start_after_scan = P2P_AFTER_SCAN_CONNECT;
1647 os_memcpy(p2p->after_scan_peer, peer_addr, ETH_ALEN);
1648 return 0;
1649 }
1650 p2p->start_after_scan = P2P_AFTER_SCAN_NOTHING;
1651
1652 return p2p_connect_send(p2p, dev);
1653}
1654
1655
1656int p2p_authorize(struct p2p_data *p2p, const u8 *peer_addr,
1657 enum p2p_wps_method wps_method,
1658 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07001659 unsigned int force_freq, int persistent_group,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001660 const u8 *force_ssid, size_t force_ssid_len,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001661 unsigned int pref_freq, u16 oob_pw_id)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001662{
1663 struct p2p_device *dev;
1664
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001665 p2p_dbg(p2p, "Request to authorize group negotiation - peer=" MACSTR
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001666 " GO Intent=%d Intended Interface Address=" MACSTR
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001667 " wps_method=%d persistent_group=%d oob_pw_id=%u",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001668 MAC2STR(peer_addr), go_intent, MAC2STR(own_interface_addr),
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001669 wps_method, persistent_group, oob_pw_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001670
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001671 dev = p2p_get_device(p2p, peer_addr);
1672 if (dev == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001673 p2p_dbg(p2p, "Cannot authorize unknown P2P Device " MACSTR,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001674 MAC2STR(peer_addr));
1675 return -1;
1676 }
1677
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001678 if (p2p_prepare_channel(p2p, dev, force_freq, pref_freq, go_intent ==
1679 15) < 0)
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001680 return -1;
1681
Dmitry Shmidt04949592012-07-19 12:16:46 -07001682 p2p->ssid_set = 0;
1683 if (force_ssid) {
1684 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Forced SSID",
1685 force_ssid, force_ssid_len);
1686 os_memcpy(p2p->ssid, force_ssid, force_ssid_len);
1687 p2p->ssid_len = force_ssid_len;
1688 p2p->ssid_set = 1;
1689 }
1690
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001691 dev->flags &= ~P2P_DEV_NOT_YET_READY;
1692 dev->flags &= ~P2P_DEV_USER_REJECTED;
1693 dev->go_neg_req_sent = 0;
1694 dev->go_state = UNKNOWN_GO;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001695 p2p_set_dev_persistent(dev, persistent_group);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001696 p2p->go_intent = go_intent;
1697 os_memcpy(p2p->intended_addr, own_interface_addr, ETH_ALEN);
1698
1699 dev->wps_method = wps_method;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001700 dev->oob_pw_id = oob_pw_id;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001701 dev->status = P2P_SC_SUCCESS;
1702
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001703 return 0;
1704}
1705
1706
1707void p2p_add_dev_info(struct p2p_data *p2p, const u8 *addr,
1708 struct p2p_device *dev, struct p2p_message *msg)
1709{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001710 os_get_reltime(&dev->last_seen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001711
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001712 p2p_copy_wps_info(p2p, dev, 0, msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001713
1714 if (msg->listen_channel) {
1715 int freq;
Dmitry Shmidt4b060592013-04-29 16:42:49 -07001716 freq = p2p_channel_to_freq(msg->listen_channel[3],
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001717 msg->listen_channel[4]);
1718 if (freq < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001719 p2p_dbg(p2p, "Unknown peer Listen channel: "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001720 "country=%c%c(0x%02x) reg_class=%u channel=%u",
1721 msg->listen_channel[0],
1722 msg->listen_channel[1],
1723 msg->listen_channel[2],
1724 msg->listen_channel[3],
1725 msg->listen_channel[4]);
1726 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001727 p2p_dbg(p2p, "Update peer " MACSTR
1728 " Listen channel: %u -> %u MHz",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001729 MAC2STR(dev->info.p2p_device_addr),
1730 dev->listen_freq, freq);
1731 dev->listen_freq = freq;
1732 }
1733 }
1734
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001735 if (msg->wfd_subelems) {
1736 wpabuf_free(dev->info.wfd_subelems);
1737 dev->info.wfd_subelems = wpabuf_dup(msg->wfd_subelems);
1738 }
1739
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001740 if (dev->flags & P2P_DEV_PROBE_REQ_ONLY) {
1741 dev->flags &= ~P2P_DEV_PROBE_REQ_ONLY;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001742 p2p_dbg(p2p, "Completed device entry based on data from GO Negotiation Request");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001743 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001744 p2p_dbg(p2p, "Created device entry based on GO Neg Req: "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001745 MACSTR " dev_capab=0x%x group_capab=0x%x name='%s' "
1746 "listen_freq=%d",
1747 MAC2STR(dev->info.p2p_device_addr),
1748 dev->info.dev_capab, dev->info.group_capab,
1749 dev->info.device_name, dev->listen_freq);
1750 }
1751
1752 dev->flags &= ~P2P_DEV_GROUP_CLIENT_ONLY;
1753
1754 if (dev->flags & P2P_DEV_USER_REJECTED) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001755 p2p_dbg(p2p, "Do not report rejected device");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001756 return;
1757 }
1758
1759 p2p->cfg->dev_found(p2p->cfg->cb_ctx, addr, &dev->info,
1760 !(dev->flags & P2P_DEV_REPORTED_ONCE));
1761 dev->flags |= P2P_DEV_REPORTED | P2P_DEV_REPORTED_ONCE;
1762}
1763
1764
1765void p2p_build_ssid(struct p2p_data *p2p, u8 *ssid, size_t *ssid_len)
1766{
1767 os_memcpy(ssid, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN);
1768 p2p_random((char *) &ssid[P2P_WILDCARD_SSID_LEN], 2);
1769 os_memcpy(&ssid[P2P_WILDCARD_SSID_LEN + 2],
1770 p2p->cfg->ssid_postfix, p2p->cfg->ssid_postfix_len);
1771 *ssid_len = P2P_WILDCARD_SSID_LEN + 2 + p2p->cfg->ssid_postfix_len;
1772}
1773
1774
1775int p2p_go_params(struct p2p_data *p2p, struct p2p_go_neg_results *params)
1776{
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001777 if (p2p->ssid_set) {
1778 os_memcpy(params->ssid, p2p->ssid, p2p->ssid_len);
1779 params->ssid_len = p2p->ssid_len;
1780 } else {
1781 p2p_build_ssid(p2p, params->ssid, &params->ssid_len);
1782 }
1783 p2p->ssid_set = 0;
1784
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07001785 p2p_random(params->passphrase, p2p->cfg->passphrase_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001786 return 0;
1787}
1788
1789
1790void p2p_go_complete(struct p2p_data *p2p, struct p2p_device *peer)
1791{
1792 struct p2p_go_neg_results res;
1793 int go = peer->go_state == LOCAL_GO;
1794 struct p2p_channels intersection;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001795
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001796 p2p_dbg(p2p, "GO Negotiation with " MACSTR " completed (%s will be GO)",
1797 MAC2STR(peer->info.p2p_device_addr), go ? "local end" : "peer");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001798
1799 os_memset(&res, 0, sizeof(res));
1800 res.role_go = go;
1801 os_memcpy(res.peer_device_addr, peer->info.p2p_device_addr, ETH_ALEN);
1802 os_memcpy(res.peer_interface_addr, peer->intended_addr, ETH_ALEN);
1803 res.wps_method = peer->wps_method;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001804 if (peer->flags & P2P_DEV_PREFER_PERSISTENT_GROUP) {
1805 if (peer->flags & P2P_DEV_PREFER_PERSISTENT_RECONN)
1806 res.persistent_group = 2;
1807 else
1808 res.persistent_group = 1;
1809 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001810
1811 if (go) {
1812 /* Setup AP mode for WPS provisioning */
Dmitry Shmidt4b060592013-04-29 16:42:49 -07001813 res.freq = p2p_channel_to_freq(p2p->op_reg_class,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001814 p2p->op_channel);
1815 os_memcpy(res.ssid, p2p->ssid, p2p->ssid_len);
1816 res.ssid_len = p2p->ssid_len;
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07001817 p2p_random(res.passphrase, p2p->cfg->passphrase_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001818 } else {
1819 res.freq = peer->oper_freq;
1820 if (p2p->ssid_len) {
1821 os_memcpy(res.ssid, p2p->ssid, p2p->ssid_len);
1822 res.ssid_len = p2p->ssid_len;
1823 }
1824 }
1825
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001826 p2p_channels_dump(p2p, "own channels", &p2p->channels);
1827 p2p_channels_dump(p2p, "peer channels", &peer->channels);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001828 p2p_channels_intersect(&p2p->channels, &peer->channels,
1829 &intersection);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001830 if (go) {
1831 p2p_channels_remove_freqs(&intersection, &p2p->no_go_freq);
1832 p2p_channels_dump(p2p, "intersection after no-GO removal",
1833 &intersection);
1834 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001835
1836 p2p_channels_to_freqs(&intersection, res.freq_list,
1837 P2P_MAX_CHANNELS);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001838
1839 res.peer_config_timeout = go ? peer->client_timeout : peer->go_timeout;
1840
1841 p2p_clear_timeout(p2p);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001842 p2p->ssid_set = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001843 peer->go_neg_req_sent = 0;
Dmitry Shmidt29333592017-01-09 12:27:11 -08001844 peer->flags &= ~P2P_DEV_PEER_WAITING_RESPONSE;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001845 peer->wps_method = WPS_NOT_READY;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001846 peer->oob_pw_id = 0;
Dmitry Shmidt413dde72014-04-11 10:23:22 -07001847 wpabuf_free(peer->go_neg_conf);
1848 peer->go_neg_conf = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001849
1850 p2p_set_state(p2p, P2P_PROVISIONING);
1851 p2p->cfg->go_neg_completed(p2p->cfg->cb_ctx, &res);
1852}
1853
1854
1855static void p2p_rx_p2p_action(struct p2p_data *p2p, const u8 *sa,
1856 const u8 *data, size_t len, int rx_freq)
1857{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001858 p2p_dbg(p2p, "RX P2P Public Action from " MACSTR, MAC2STR(sa));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001859 wpa_hexdump(MSG_MSGDUMP, "P2P: P2P Public Action contents", data, len);
1860
1861 if (len < 1)
1862 return;
1863
1864 switch (data[0]) {
1865 case P2P_GO_NEG_REQ:
1866 p2p_process_go_neg_req(p2p, sa, data + 1, len - 1, rx_freq);
1867 break;
1868 case P2P_GO_NEG_RESP:
1869 p2p_process_go_neg_resp(p2p, sa, data + 1, len - 1, rx_freq);
1870 break;
1871 case P2P_GO_NEG_CONF:
1872 p2p_process_go_neg_conf(p2p, sa, data + 1, len - 1);
1873 break;
1874 case P2P_INVITATION_REQ:
1875 p2p_process_invitation_req(p2p, sa, data + 1, len - 1,
1876 rx_freq);
1877 break;
1878 case P2P_INVITATION_RESP:
1879 p2p_process_invitation_resp(p2p, sa, data + 1, len - 1);
1880 break;
1881 case P2P_PROV_DISC_REQ:
1882 p2p_process_prov_disc_req(p2p, sa, data + 1, len - 1, rx_freq);
1883 break;
1884 case P2P_PROV_DISC_RESP:
1885 p2p_process_prov_disc_resp(p2p, sa, data + 1, len - 1);
1886 break;
1887 case P2P_DEV_DISC_REQ:
1888 p2p_process_dev_disc_req(p2p, sa, data + 1, len - 1, rx_freq);
1889 break;
1890 case P2P_DEV_DISC_RESP:
1891 p2p_process_dev_disc_resp(p2p, sa, data + 1, len - 1);
1892 break;
1893 default:
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001894 p2p_dbg(p2p, "Unsupported P2P Public Action frame type %d",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001895 data[0]);
1896 break;
1897 }
1898}
1899
1900
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001901static void p2p_rx_action_public(struct p2p_data *p2p, const u8 *da,
1902 const u8 *sa, const u8 *bssid, const u8 *data,
1903 size_t len, int freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001904{
1905 if (len < 1)
1906 return;
1907
1908 switch (data[0]) {
1909 case WLAN_PA_VENDOR_SPECIFIC:
1910 data++;
1911 len--;
Dmitry Shmidta38abf92014-03-06 13:38:44 -08001912 if (len < 4)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001913 return;
Dmitry Shmidta38abf92014-03-06 13:38:44 -08001914 if (WPA_GET_BE32(data) != P2P_IE_VENDOR_TYPE)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001915 return;
1916
Dmitry Shmidta38abf92014-03-06 13:38:44 -08001917 data += 4;
1918 len -= 4;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001919
Dmitry Shmidta38abf92014-03-06 13:38:44 -08001920 p2p_rx_p2p_action(p2p, sa, data, len, freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001921 break;
1922 case WLAN_PA_GAS_INITIAL_REQ:
1923 p2p_rx_gas_initial_req(p2p, sa, data + 1, len - 1, freq);
1924 break;
1925 case WLAN_PA_GAS_INITIAL_RESP:
1926 p2p_rx_gas_initial_resp(p2p, sa, data + 1, len - 1, freq);
1927 break;
1928 case WLAN_PA_GAS_COMEBACK_REQ:
1929 p2p_rx_gas_comeback_req(p2p, sa, data + 1, len - 1, freq);
1930 break;
1931 case WLAN_PA_GAS_COMEBACK_RESP:
1932 p2p_rx_gas_comeback_resp(p2p, sa, data + 1, len - 1, freq);
1933 break;
1934 }
1935}
1936
1937
1938void p2p_rx_action(struct p2p_data *p2p, const u8 *da, const u8 *sa,
1939 const u8 *bssid, u8 category,
1940 const u8 *data, size_t len, int freq)
1941{
1942 if (category == WLAN_ACTION_PUBLIC) {
1943 p2p_rx_action_public(p2p, da, sa, bssid, data, len, freq);
1944 return;
1945 }
1946
1947 if (category != WLAN_ACTION_VENDOR_SPECIFIC)
1948 return;
1949
1950 if (len < 4)
1951 return;
1952
Dmitry Shmidta38abf92014-03-06 13:38:44 -08001953 if (WPA_GET_BE32(data) != P2P_IE_VENDOR_TYPE)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001954 return;
Dmitry Shmidta38abf92014-03-06 13:38:44 -08001955 data += 4;
1956 len -= 4;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001957
1958 /* P2P action frame */
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001959 p2p_dbg(p2p, "RX P2P Action from " MACSTR, MAC2STR(sa));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001960 wpa_hexdump(MSG_MSGDUMP, "P2P: P2P Action contents", data, len);
1961
1962 if (len < 1)
1963 return;
1964 switch (data[0]) {
1965 case P2P_NOA:
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001966 p2p_dbg(p2p, "Received P2P Action - Notice of Absence");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001967 /* TODO */
1968 break;
1969 case P2P_PRESENCE_REQ:
1970 p2p_process_presence_req(p2p, da, sa, data + 1, len - 1, freq);
1971 break;
1972 case P2P_PRESENCE_RESP:
1973 p2p_process_presence_resp(p2p, da, sa, data + 1, len - 1);
1974 break;
1975 case P2P_GO_DISC_REQ:
1976 p2p_process_go_disc_req(p2p, da, sa, data + 1, len - 1, freq);
1977 break;
1978 default:
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001979 p2p_dbg(p2p, "Received P2P Action - unknown type %u", data[0]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001980 break;
1981 }
1982}
1983
1984
1985static void p2p_go_neg_start(void *eloop_ctx, void *timeout_ctx)
1986{
1987 struct p2p_data *p2p = eloop_ctx;
1988 if (p2p->go_neg_peer == NULL)
1989 return;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001990 if (p2p->pending_listen_freq) {
1991 p2p_dbg(p2p, "Clear pending_listen_freq for p2p_go_neg_start");
1992 p2p->pending_listen_freq = 0;
1993 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001994 p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
1995 p2p->go_neg_peer->status = P2P_SC_SUCCESS;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001996 /*
1997 * Set new timeout to make sure a previously set one does not expire
1998 * too quickly while waiting for the GO Negotiation to complete.
1999 */
2000 p2p_set_timeout(p2p, 0, 500000);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002001 p2p_connect_send(p2p, p2p->go_neg_peer);
2002}
2003
2004
2005static void p2p_invite_start(void *eloop_ctx, void *timeout_ctx)
2006{
2007 struct p2p_data *p2p = eloop_ctx;
2008 if (p2p->invite_peer == NULL)
2009 return;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07002010 if (p2p->pending_listen_freq) {
2011 p2p_dbg(p2p, "Clear pending_listen_freq for p2p_invite_start");
2012 p2p->pending_listen_freq = 0;
2013 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002014 p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002015 p2p_invite_send(p2p, p2p->invite_peer, p2p->invite_go_dev_addr,
2016 p2p->invite_dev_pw_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002017}
2018
2019
2020static void p2p_add_dev_from_probe_req(struct p2p_data *p2p, const u8 *addr,
2021 const u8 *ie, size_t ie_len)
2022{
2023 struct p2p_message msg;
2024 struct p2p_device *dev;
2025
2026 os_memset(&msg, 0, sizeof(msg));
2027 if (p2p_parse_ies(ie, ie_len, &msg) < 0 || msg.p2p_attributes == NULL)
2028 {
2029 p2p_parse_free(&msg);
2030 return; /* not a P2P probe */
2031 }
2032
2033 if (msg.ssid == NULL || msg.ssid[1] != P2P_WILDCARD_SSID_LEN ||
2034 os_memcmp(msg.ssid + 2, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN)
2035 != 0) {
2036 /* The Probe Request is not part of P2P Device Discovery. It is
2037 * not known whether the source address of the frame is the P2P
2038 * Device Address or P2P Interface Address. Do not add a new
2039 * peer entry based on this frames.
2040 */
2041 p2p_parse_free(&msg);
2042 return;
2043 }
2044
2045 dev = p2p_get_device(p2p, addr);
2046 if (dev) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002047 if (msg.listen_channel) {
2048 int freq;
2049
2050 if (dev->country[0] == 0)
2051 os_memcpy(dev->country, msg.listen_channel, 3);
2052
2053 freq = p2p_channel_to_freq(msg.listen_channel[3],
2054 msg.listen_channel[4]);
2055
2056 if (freq > 0 && dev->listen_freq != freq) {
2057 p2p_dbg(p2p,
2058 "Updated peer " MACSTR " Listen channel (Probe Request): %d -> %d MHz",
2059 MAC2STR(addr), dev->listen_freq, freq);
2060 dev->listen_freq = freq;
2061 }
2062 }
2063
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002064 os_get_reltime(&dev->last_seen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002065 p2p_parse_free(&msg);
2066 return; /* already known */
2067 }
2068
2069 dev = p2p_create_device(p2p, addr);
2070 if (dev == NULL) {
2071 p2p_parse_free(&msg);
2072 return;
2073 }
2074
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002075 os_get_reltime(&dev->last_seen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002076 dev->flags |= P2P_DEV_PROBE_REQ_ONLY;
2077
2078 if (msg.listen_channel) {
2079 os_memcpy(dev->country, msg.listen_channel, 3);
Dmitry Shmidt4b060592013-04-29 16:42:49 -07002080 dev->listen_freq = p2p_channel_to_freq(msg.listen_channel[3],
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002081 msg.listen_channel[4]);
2082 }
2083
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002084 p2p_copy_wps_info(p2p, dev, 1, &msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002085
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002086 if (msg.wfd_subelems) {
2087 wpabuf_free(dev->info.wfd_subelems);
2088 dev->info.wfd_subelems = wpabuf_dup(msg.wfd_subelems);
2089 }
2090
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002091 p2p_parse_free(&msg);
2092
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002093 p2p_dbg(p2p, "Created device entry based on Probe Req: " MACSTR
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002094 " dev_capab=0x%x group_capab=0x%x name='%s' listen_freq=%d",
2095 MAC2STR(dev->info.p2p_device_addr), dev->info.dev_capab,
2096 dev->info.group_capab, dev->info.device_name,
2097 dev->listen_freq);
2098}
2099
2100
2101struct p2p_device * p2p_add_dev_from_go_neg_req(struct p2p_data *p2p,
2102 const u8 *addr,
2103 struct p2p_message *msg)
2104{
2105 struct p2p_device *dev;
2106
2107 dev = p2p_get_device(p2p, addr);
2108 if (dev) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002109 os_get_reltime(&dev->last_seen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002110 return dev; /* already known */
2111 }
2112
2113 dev = p2p_create_device(p2p, addr);
2114 if (dev == NULL)
2115 return NULL;
2116
2117 p2p_add_dev_info(p2p, addr, dev, msg);
2118
2119 return dev;
2120}
2121
2122
2123static int dev_type_match(const u8 *dev_type, const u8 *req_dev_type)
2124{
2125 if (os_memcmp(dev_type, req_dev_type, WPS_DEV_TYPE_LEN) == 0)
2126 return 1;
2127 if (os_memcmp(dev_type, req_dev_type, 2) == 0 &&
2128 WPA_GET_BE32(&req_dev_type[2]) == 0 &&
2129 WPA_GET_BE16(&req_dev_type[6]) == 0)
2130 return 1; /* Category match with wildcard OUI/sub-category */
2131 return 0;
2132}
2133
2134
2135int dev_type_list_match(const u8 *dev_type, const u8 *req_dev_type[],
2136 size_t num_req_dev_type)
2137{
2138 size_t i;
2139 for (i = 0; i < num_req_dev_type; i++) {
2140 if (dev_type_match(dev_type, req_dev_type[i]))
2141 return 1;
2142 }
2143 return 0;
2144}
2145
2146
2147/**
2148 * p2p_match_dev_type - Match local device type with requested type
2149 * @p2p: P2P module context from p2p_init()
2150 * @wps: WPS TLVs from Probe Request frame (concatenated WPS IEs)
2151 * Returns: 1 on match, 0 on mismatch
2152 *
2153 * This function can be used to match the Requested Device Type attribute in
2154 * WPS IE with the local device types for deciding whether to reply to a Probe
2155 * Request frame.
2156 */
2157int p2p_match_dev_type(struct p2p_data *p2p, struct wpabuf *wps)
2158{
2159 struct wps_parse_attr attr;
2160 size_t i;
2161
2162 if (wps_parse_msg(wps, &attr))
2163 return 1; /* assume no Requested Device Type attributes */
2164
2165 if (attr.num_req_dev_type == 0)
2166 return 1; /* no Requested Device Type attributes -> match */
2167
2168 if (dev_type_list_match(p2p->cfg->pri_dev_type, attr.req_dev_type,
2169 attr.num_req_dev_type))
2170 return 1; /* Own Primary Device Type matches */
2171
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002172 for (i = 0; i < p2p->cfg->num_sec_dev_types; i++) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002173 if (dev_type_list_match(p2p->cfg->sec_dev_type[i],
2174 attr.req_dev_type,
2175 attr.num_req_dev_type))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002176 return 1; /* Own Secondary Device Type matches */
2177 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002178
2179 /* No matching device type found */
2180 return 0;
2181}
2182
2183
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002184struct wpabuf * p2p_build_probe_resp_ies(struct p2p_data *p2p,
2185 const u8 *query_hash,
2186 u8 query_count)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002187{
2188 struct wpabuf *buf;
2189 u8 *len;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002190 int pw_id = -1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002191 size_t extra = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002192
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002193#ifdef CONFIG_WIFI_DISPLAY
2194 if (p2p->wfd_ie_probe_resp)
2195 extra = wpabuf_len(p2p->wfd_ie_probe_resp);
2196#endif /* CONFIG_WIFI_DISPLAY */
2197
Dmitry Shmidt2e67f062014-07-16 09:55:28 -07002198 if (p2p->vendor_elem && p2p->vendor_elem[VENDOR_ELEM_PROBE_RESP_P2P])
2199 extra += wpabuf_len(p2p->vendor_elem[VENDOR_ELEM_PROBE_RESP_P2P]);
2200
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002201 if (query_count)
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002202 extra += MAX_SVC_ADV_IE_LEN;
2203
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002204 buf = wpabuf_alloc(1000 + extra);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002205 if (buf == NULL)
2206 return NULL;
2207
Dmitry Shmidt04949592012-07-19 12:16:46 -07002208 if (p2p->go_neg_peer) {
2209 /* Advertise immediate availability of WPS credential */
2210 pw_id = p2p_wps_method_pw_id(p2p->go_neg_peer->wps_method);
2211 }
2212
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002213 if (p2p_build_wps_ie(p2p, buf, pw_id, 1) < 0) {
2214 p2p_dbg(p2p, "Failed to build WPS IE for Probe Response");
2215 wpabuf_free(buf);
2216 return NULL;
2217 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002218
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002219#ifdef CONFIG_WIFI_DISPLAY
2220 if (p2p->wfd_ie_probe_resp)
2221 wpabuf_put_buf(buf, p2p->wfd_ie_probe_resp);
2222#endif /* CONFIG_WIFI_DISPLAY */
2223
Dmitry Shmidt2e67f062014-07-16 09:55:28 -07002224 if (p2p->vendor_elem && p2p->vendor_elem[VENDOR_ELEM_PROBE_RESP_P2P])
2225 wpabuf_put_buf(buf,
2226 p2p->vendor_elem[VENDOR_ELEM_PROBE_RESP_P2P]);
2227
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002228 /* P2P IE */
2229 len = p2p_buf_add_ie_hdr(buf);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002230 p2p_buf_add_capability(buf, p2p->dev_capab &
2231 ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002232 if (p2p->ext_listen_interval)
2233 p2p_buf_add_ext_listen_timing(buf, p2p->ext_listen_period,
2234 p2p->ext_listen_interval);
2235 p2p_buf_add_device_info(buf, p2p, NULL);
2236 p2p_buf_update_ie_hdr(buf, len);
2237
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002238 if (query_count) {
2239 p2p_buf_add_service_instance(buf, p2p, query_count, query_hash,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002240 p2p->p2ps_adv_list);
2241 }
2242
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002243 return buf;
2244}
2245
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07002246static int p2p_build_probe_resp_buf(struct p2p_data *p2p, struct wpabuf *buf,
2247 struct wpabuf *ies,
2248 const u8 *addr, int rx_freq)
2249{
2250 struct ieee80211_mgmt *resp;
2251 u8 channel, op_class;
2252
2253 resp = wpabuf_put(buf, offsetof(struct ieee80211_mgmt,
2254 u.probe_resp.variable));
2255
2256 resp->frame_control = host_to_le16((WLAN_FC_TYPE_MGMT << 2) |
2257 (WLAN_FC_STYPE_PROBE_RESP << 4));
2258 os_memcpy(resp->da, addr, ETH_ALEN);
2259 os_memcpy(resp->sa, p2p->cfg->dev_addr, ETH_ALEN);
2260 os_memcpy(resp->bssid, p2p->cfg->dev_addr, ETH_ALEN);
2261 resp->u.probe_resp.beacon_int = host_to_le16(100);
2262 /* hardware or low-level driver will setup seq_ctrl and timestamp */
2263 resp->u.probe_resp.capab_info =
2264 host_to_le16(WLAN_CAPABILITY_SHORT_PREAMBLE |
2265 WLAN_CAPABILITY_PRIVACY |
2266 WLAN_CAPABILITY_SHORT_SLOT_TIME);
2267
2268 wpabuf_put_u8(buf, WLAN_EID_SSID);
2269 wpabuf_put_u8(buf, P2P_WILDCARD_SSID_LEN);
2270 wpabuf_put_data(buf, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN);
2271
2272 wpabuf_put_u8(buf, WLAN_EID_SUPP_RATES);
2273 wpabuf_put_u8(buf, 8);
2274 wpabuf_put_u8(buf, (60 / 5) | 0x80);
2275 wpabuf_put_u8(buf, 90 / 5);
2276 wpabuf_put_u8(buf, (120 / 5) | 0x80);
2277 wpabuf_put_u8(buf, 180 / 5);
2278 wpabuf_put_u8(buf, (240 / 5) | 0x80);
2279 wpabuf_put_u8(buf, 360 / 5);
2280 wpabuf_put_u8(buf, 480 / 5);
2281 wpabuf_put_u8(buf, 540 / 5);
2282
2283 if (!rx_freq) {
2284 channel = p2p->cfg->channel;
2285 } else if (p2p_freq_to_channel(rx_freq, &op_class, &channel)) {
2286 p2p_err(p2p, "Failed to convert freq to channel");
2287 return -1;
2288 }
2289
2290 wpabuf_put_u8(buf, WLAN_EID_DS_PARAMS);
2291 wpabuf_put_u8(buf, 1);
2292 wpabuf_put_u8(buf, channel);
2293
2294 wpabuf_put_buf(buf, ies);
2295
2296 return 0;
2297}
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002298
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002299static int p2p_service_find_asp(struct p2p_data *p2p, const u8 *hash)
2300{
2301 struct p2ps_advertisement *adv_data;
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002302 int any_wfa;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002303
2304 p2p_dbg(p2p, "ASP find - ASP list: %p", p2p->p2ps_adv_list);
2305
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002306 /* Wildcard org.wi-fi.wfds matches any WFA spec defined service */
2307 any_wfa = os_memcmp(hash, p2p->wild_card_hash, P2PS_HASH_LEN) == 0;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002308
2309 adv_data = p2p->p2ps_adv_list;
2310 while (adv_data) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002311 if (os_memcmp(hash, adv_data->hash, P2PS_HASH_LEN) == 0)
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002312 return 1; /* exact hash match */
2313 if (any_wfa &&
2314 os_strncmp(adv_data->svc_name, P2PS_WILD_HASH_STR,
2315 os_strlen(P2PS_WILD_HASH_STR)) == 0)
2316 return 1; /* WFA service match */
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002317 adv_data = adv_data->next;
2318 }
2319
2320 return 0;
2321}
2322
2323
Dmitry Shmidt04949592012-07-19 12:16:46 -07002324static enum p2p_probe_req_status
2325p2p_reply_probe(struct p2p_data *p2p, const u8 *addr, const u8 *dst,
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002326 const u8 *bssid, const u8 *ie, size_t ie_len,
2327 unsigned int rx_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002328{
2329 struct ieee802_11_elems elems;
2330 struct wpabuf *buf;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002331 struct p2p_message msg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002332 struct wpabuf *ies;
2333
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002334 if (ieee802_11_parse_elems((u8 *) ie, ie_len, &elems, 0) ==
2335 ParseFailed) {
2336 /* Ignore invalid Probe Request frames */
Dmitry Shmidtec58b162014-02-19 12:44:18 -08002337 p2p_dbg(p2p, "Could not parse Probe Request frame - ignore it");
Dmitry Shmidt04949592012-07-19 12:16:46 -07002338 return P2P_PREQ_MALFORMED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002339 }
2340
2341 if (elems.p2p == NULL) {
2342 /* not a P2P probe - ignore it */
Dmitry Shmidtec58b162014-02-19 12:44:18 -08002343 p2p_dbg(p2p, "Not a P2P probe - ignore it");
Dmitry Shmidt04949592012-07-19 12:16:46 -07002344 return P2P_PREQ_NOT_P2P;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002345 }
2346
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002347 if (dst && !is_broadcast_ether_addr(dst) &&
2348 os_memcmp(dst, p2p->cfg->dev_addr, ETH_ALEN) != 0) {
2349 /* Not sent to the broadcast address or our P2P Device Address
2350 */
Dmitry Shmidtec58b162014-02-19 12:44:18 -08002351 p2p_dbg(p2p, "Probe Req DA " MACSTR " not ours - ignore it",
2352 MAC2STR(dst));
Dmitry Shmidt04949592012-07-19 12:16:46 -07002353 return P2P_PREQ_NOT_PROCESSED;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002354 }
2355
2356 if (bssid && !is_broadcast_ether_addr(bssid)) {
2357 /* Not sent to the Wildcard BSSID */
Dmitry Shmidtec58b162014-02-19 12:44:18 -08002358 p2p_dbg(p2p, "Probe Req BSSID " MACSTR " not wildcard - ignore it",
2359 MAC2STR(bssid));
Dmitry Shmidt04949592012-07-19 12:16:46 -07002360 return P2P_PREQ_NOT_PROCESSED;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002361 }
2362
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002363 if (elems.ssid == NULL || elems.ssid_len != P2P_WILDCARD_SSID_LEN ||
2364 os_memcmp(elems.ssid, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN) !=
2365 0) {
2366 /* not using P2P Wildcard SSID - ignore */
Dmitry Shmidtec58b162014-02-19 12:44:18 -08002367 p2p_dbg(p2p, "Probe Req not using P2P Wildcard SSID - ignore it");
Dmitry Shmidt04949592012-07-19 12:16:46 -07002368 return P2P_PREQ_NOT_PROCESSED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002369 }
2370
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002371 if (supp_rates_11b_only(&elems)) {
2372 /* Indicates support for 11b rates only */
Dmitry Shmidtec58b162014-02-19 12:44:18 -08002373 p2p_dbg(p2p, "Probe Req with 11b rates only supported - ignore it");
Dmitry Shmidt04949592012-07-19 12:16:46 -07002374 return P2P_PREQ_NOT_P2P;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002375 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002376
2377 os_memset(&msg, 0, sizeof(msg));
2378 if (p2p_parse_ies(ie, ie_len, &msg) < 0) {
2379 /* Could not parse P2P attributes */
Dmitry Shmidtec58b162014-02-19 12:44:18 -08002380 p2p_dbg(p2p, "Could not parse P2P attributes in Probe Req - ignore it");
Dmitry Shmidt04949592012-07-19 12:16:46 -07002381 return P2P_PREQ_NOT_P2P;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002382 }
2383
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002384 if (msg.service_hash && msg.service_hash_count) {
2385 const u8 *hash = msg.service_hash;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002386 u8 i;
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002387 int p2ps_svc_found = 0;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002388
Dmitry Shmidt41712582015-06-29 11:02:15 -07002389 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",
2390 p2p->in_listen, p2p->drv_in_listen, rx_freq,
2391 p2p->cfg->channel, p2p->pending_listen_freq);
2392
2393 if (!p2p->in_listen && !p2p->drv_in_listen &&
2394 p2p->pending_listen_freq && rx_freq &&
2395 rx_freq != p2p->pending_listen_freq) {
2396 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",
2397 rx_freq, p2p->pending_listen_freq);
2398 p2p_parse_free(&msg);
2399 return P2P_PREQ_NOT_LISTEN;
2400 }
2401
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002402 for (i = 0; i < msg.service_hash_count; i++) {
2403 if (p2p_service_find_asp(p2p, hash)) {
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002404 p2p_dbg(p2p, "Service Hash match found: "
2405 MACSTR, MAC2STR(hash));
2406 p2ps_svc_found = 1;
2407 break;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002408 }
2409 hash += P2PS_HASH_LEN;
2410 }
2411
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002412 /* Probed hash unknown */
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002413 if (!p2ps_svc_found) {
2414 p2p_dbg(p2p, "No Service Hash match found");
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002415 p2p_parse_free(&msg);
2416 return P2P_PREQ_NOT_PROCESSED;
2417 }
2418 } else {
2419 /* This is not a P2PS Probe Request */
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002420 p2p_dbg(p2p, "No P2PS Hash in Probe Request");
2421
2422 if (!p2p->in_listen || !p2p->drv_in_listen) {
2423 /* not in Listen state - ignore Probe Request */
2424 p2p_dbg(p2p, "Not in Listen state (in_listen=%d drv_in_listen=%d) - ignore Probe Request",
2425 p2p->in_listen, p2p->drv_in_listen);
2426 p2p_parse_free(&msg);
2427 return P2P_PREQ_NOT_LISTEN;
2428 }
2429 }
2430
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002431 if (msg.device_id &&
Dmitry Shmidt04949592012-07-19 12:16:46 -07002432 os_memcmp(msg.device_id, p2p->cfg->dev_addr, ETH_ALEN) != 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002433 /* Device ID did not match */
Dmitry Shmidtec58b162014-02-19 12:44:18 -08002434 p2p_dbg(p2p, "Probe Req requested Device ID " MACSTR " did not match - ignore it",
2435 MAC2STR(msg.device_id));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002436 p2p_parse_free(&msg);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002437 return P2P_PREQ_NOT_PROCESSED;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002438 }
2439
2440 /* Check Requested Device Type match */
2441 if (msg.wps_attributes &&
2442 !p2p_match_dev_type(p2p, msg.wps_attributes)) {
2443 /* No match with Requested Device Type */
Dmitry Shmidtec58b162014-02-19 12:44:18 -08002444 p2p_dbg(p2p, "Probe Req requestred Device Type did not match - ignore it");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002445 p2p_parse_free(&msg);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002446 return P2P_PREQ_NOT_PROCESSED;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002447 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002448
Dmitry Shmidt04949592012-07-19 12:16:46 -07002449 if (!p2p->cfg->send_probe_resp) {
2450 /* Response generated elsewhere */
Dmitry Shmidtec58b162014-02-19 12:44:18 -08002451 p2p_dbg(p2p, "Probe Resp generated elsewhere - do not generate additional response");
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002452 p2p_parse_free(&msg);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002453 return P2P_PREQ_NOT_PROCESSED;
2454 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002455
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002456 p2p_dbg(p2p, "Reply to P2P Probe Request in Listen state");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002457
2458 /*
2459 * We do not really have a specific BSS that this frame is advertising,
2460 * so build a frame that has some information in valid format. This is
2461 * really only used for discovery purposes, not to learn exact BSS
2462 * parameters.
2463 */
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002464 ies = p2p_build_probe_resp_ies(p2p, msg.service_hash,
2465 msg.service_hash_count);
2466 p2p_parse_free(&msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002467 if (ies == NULL)
Dmitry Shmidt04949592012-07-19 12:16:46 -07002468 return P2P_PREQ_NOT_PROCESSED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002469
2470 buf = wpabuf_alloc(200 + wpabuf_len(ies));
2471 if (buf == NULL) {
2472 wpabuf_free(ies);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002473 return P2P_PREQ_NOT_PROCESSED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002474 }
2475
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07002476 if (p2p_build_probe_resp_buf(p2p, buf, ies, addr, rx_freq)) {
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002477 wpabuf_free(ies);
2478 wpabuf_free(buf);
2479 return P2P_PREQ_NOT_PROCESSED;
2480 }
2481
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002482 wpabuf_free(ies);
2483
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002484 p2p->cfg->send_probe_resp(p2p->cfg->cb_ctx, buf, rx_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002485
2486 wpabuf_free(buf);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002487
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002488 return P2P_PREQ_PROCESSED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002489}
2490
2491
Dmitry Shmidt04949592012-07-19 12:16:46 -07002492enum p2p_probe_req_status
2493p2p_probe_req_rx(struct p2p_data *p2p, const u8 *addr, const u8 *dst,
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002494 const u8 *bssid, const u8 *ie, size_t ie_len,
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07002495 unsigned int rx_freq, int p2p_lo_started)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002496{
Dmitry Shmidt04949592012-07-19 12:16:46 -07002497 enum p2p_probe_req_status res;
2498
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002499 p2p_add_dev_from_probe_req(p2p, addr, ie, ie_len);
2500
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07002501 if (p2p_lo_started) {
2502 p2p_dbg(p2p,
2503 "Probe Response is offloaded, do not reply Probe Request");
2504 return P2P_PREQ_PROCESSED;
2505 }
2506
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002507 res = p2p_reply_probe(p2p, addr, dst, bssid, ie, ie_len, rx_freq);
2508 if (res != P2P_PREQ_PROCESSED && res != P2P_PREQ_NOT_PROCESSED)
2509 return res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002510
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002511 /*
2512 * Activate a pending GO Negotiation/Invite flow if a received Probe
2513 * Request frame is from an expected peer. Some devices may share the
2514 * same address for P2P and non-P2P STA running simultaneously. The
2515 * P2P_PREQ_PROCESSED and P2P_PREQ_NOT_PROCESSED p2p_reply_probe()
2516 * return values verified above ensure we are handling a Probe Request
2517 * frame from a P2P peer.
2518 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002519 if ((p2p->state == P2P_CONNECT || p2p->state == P2P_CONNECT_LISTEN) &&
2520 p2p->go_neg_peer &&
2521 os_memcmp(addr, p2p->go_neg_peer->info.p2p_device_addr, ETH_ALEN)
Dmitry Shmidt9cdf1b92013-02-27 12:58:50 -08002522 == 0 &&
2523 !(p2p->go_neg_peer->flags & P2P_DEV_WAIT_GO_NEG_CONFIRM)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002524 /* Received a Probe Request from GO Negotiation peer */
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002525 p2p_dbg(p2p, "Found GO Negotiation peer - try to start GO negotiation from timeout");
Dmitry Shmidt9cdf1b92013-02-27 12:58:50 -08002526 eloop_cancel_timeout(p2p_go_neg_start, p2p, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002527 eloop_register_timeout(0, 0, p2p_go_neg_start, p2p, NULL);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002528 return res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002529 }
2530
2531 if ((p2p->state == P2P_INVITE || p2p->state == P2P_INVITE_LISTEN) &&
2532 p2p->invite_peer &&
Dmitry Shmidt3c479372014-02-04 10:50:36 -08002533 (p2p->invite_peer->flags & P2P_DEV_WAIT_INV_REQ_ACK) &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002534 os_memcmp(addr, p2p->invite_peer->info.p2p_device_addr, ETH_ALEN)
2535 == 0) {
2536 /* Received a Probe Request from Invite peer */
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002537 p2p_dbg(p2p, "Found Invite peer - try to start Invite from timeout");
Dmitry Shmidt7f93d6f2014-02-21 11:22:49 -08002538 eloop_cancel_timeout(p2p_invite_start, p2p, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002539 eloop_register_timeout(0, 0, p2p_invite_start, p2p, NULL);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002540 return res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002541 }
2542
Dmitry Shmidt04949592012-07-19 12:16:46 -07002543 return res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002544}
2545
2546
2547static int p2p_assoc_req_ie_wlan_ap(struct p2p_data *p2p, const u8 *bssid,
2548 u8 *buf, size_t len, struct wpabuf *p2p_ie)
2549{
2550 struct wpabuf *tmp;
2551 u8 *lpos;
2552 size_t tmplen;
2553 int res;
2554 u8 group_capab;
Dmitry Shmidt05df46a2015-05-19 11:02:01 -07002555 struct p2p_message msg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002556
2557 if (p2p_ie == NULL)
2558 return 0; /* WLAN AP is not a P2P manager */
2559
Dmitry Shmidt05df46a2015-05-19 11:02:01 -07002560 os_memset(&msg, 0, sizeof(msg));
2561 if (p2p_parse_p2p_ie(p2p_ie, &msg) < 0)
2562 return 0;
2563
2564 p2p_dbg(p2p, "BSS P2P manageability %s",
2565 msg.manageability ? "enabled" : "disabled");
2566
2567 if (!msg.manageability)
2568 return 0;
2569
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002570 /*
2571 * (Re)Association Request - P2P IE
2572 * P2P Capability attribute (shall be present)
2573 * P2P Interface attribute (present if concurrent device and
2574 * P2P Management is enabled)
2575 */
2576 tmp = wpabuf_alloc(200);
2577 if (tmp == NULL)
2578 return -1;
2579
2580 lpos = p2p_buf_add_ie_hdr(tmp);
2581 group_capab = 0;
2582 if (p2p->num_groups > 0) {
2583 group_capab |= P2P_GROUP_CAPAB_GROUP_OWNER;
2584 if ((p2p->dev_capab & P2P_DEV_CAPAB_CONCURRENT_OPER) &&
2585 (p2p->dev_capab & P2P_DEV_CAPAB_INFRA_MANAGED) &&
2586 p2p->cross_connect)
2587 group_capab |= P2P_GROUP_CAPAB_CROSS_CONN;
2588 }
2589 p2p_buf_add_capability(tmp, p2p->dev_capab, group_capab);
2590 if ((p2p->dev_capab & P2P_DEV_CAPAB_CONCURRENT_OPER) &&
2591 (p2p->dev_capab & P2P_DEV_CAPAB_INFRA_MANAGED))
2592 p2p_buf_add_p2p_interface(tmp, p2p);
2593 p2p_buf_update_ie_hdr(tmp, lpos);
2594
2595 tmplen = wpabuf_len(tmp);
2596 if (tmplen > len)
2597 res = -1;
2598 else {
2599 os_memcpy(buf, wpabuf_head(tmp), tmplen);
2600 res = tmplen;
2601 }
2602 wpabuf_free(tmp);
2603
2604 return res;
2605}
2606
2607
2608int p2p_assoc_req_ie(struct p2p_data *p2p, const u8 *bssid, u8 *buf,
2609 size_t len, int p2p_group, struct wpabuf *p2p_ie)
2610{
2611 struct wpabuf *tmp;
2612 u8 *lpos;
2613 struct p2p_device *peer;
2614 size_t tmplen;
2615 int res;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002616 size_t extra = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002617
2618 if (!p2p_group)
2619 return p2p_assoc_req_ie_wlan_ap(p2p, bssid, buf, len, p2p_ie);
2620
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002621#ifdef CONFIG_WIFI_DISPLAY
2622 if (p2p->wfd_ie_assoc_req)
2623 extra = wpabuf_len(p2p->wfd_ie_assoc_req);
2624#endif /* CONFIG_WIFI_DISPLAY */
2625
Dmitry Shmidt2e67f062014-07-16 09:55:28 -07002626 if (p2p->vendor_elem && p2p->vendor_elem[VENDOR_ELEM_P2P_ASSOC_REQ])
2627 extra += wpabuf_len(p2p->vendor_elem[VENDOR_ELEM_P2P_ASSOC_REQ]);
2628
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002629 /*
2630 * (Re)Association Request - P2P IE
2631 * P2P Capability attribute (shall be present)
2632 * Extended Listen Timing (may be present)
2633 * P2P Device Info attribute (shall be present)
2634 */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002635 tmp = wpabuf_alloc(200 + extra);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002636 if (tmp == NULL)
2637 return -1;
2638
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002639#ifdef CONFIG_WIFI_DISPLAY
2640 if (p2p->wfd_ie_assoc_req)
2641 wpabuf_put_buf(tmp, p2p->wfd_ie_assoc_req);
2642#endif /* CONFIG_WIFI_DISPLAY */
2643
Dmitry Shmidt2e67f062014-07-16 09:55:28 -07002644 if (p2p->vendor_elem && p2p->vendor_elem[VENDOR_ELEM_P2P_ASSOC_REQ])
2645 wpabuf_put_buf(tmp,
2646 p2p->vendor_elem[VENDOR_ELEM_P2P_ASSOC_REQ]);
2647
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002648 peer = bssid ? p2p_get_device(p2p, bssid) : NULL;
2649
2650 lpos = p2p_buf_add_ie_hdr(tmp);
2651 p2p_buf_add_capability(tmp, p2p->dev_capab, 0);
2652 if (p2p->ext_listen_interval)
2653 p2p_buf_add_ext_listen_timing(tmp, p2p->ext_listen_period,
2654 p2p->ext_listen_interval);
2655 p2p_buf_add_device_info(tmp, p2p, peer);
2656 p2p_buf_update_ie_hdr(tmp, lpos);
2657
2658 tmplen = wpabuf_len(tmp);
2659 if (tmplen > len)
2660 res = -1;
2661 else {
2662 os_memcpy(buf, wpabuf_head(tmp), tmplen);
2663 res = tmplen;
2664 }
2665 wpabuf_free(tmp);
2666
2667 return res;
2668}
2669
2670
2671int p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf, char *end)
2672{
2673 struct wpabuf *p2p_ie;
2674 int ret;
2675
2676 p2p_ie = ieee802_11_vendor_ie_concat(ies, ies_len, P2P_IE_VENDOR_TYPE);
2677 if (p2p_ie == NULL)
2678 return 0;
2679
2680 ret = p2p_attr_text(p2p_ie, buf, end);
2681 wpabuf_free(p2p_ie);
2682 return ret;
2683}
2684
2685
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002686struct p2ps_advertisement *
2687p2p_service_p2ps_id(struct p2p_data *p2p, u32 adv_id)
2688{
2689 struct p2ps_advertisement *adv_data;
2690
2691 if (!p2p)
2692 return NULL;
2693
2694 adv_data = p2p->p2ps_adv_list;
2695 while (adv_data) {
2696 if (adv_data->id == adv_id)
2697 return adv_data;
2698 adv_data = adv_data->next;
2699 }
2700
2701 return NULL;
2702}
2703
2704
2705int p2p_service_del_asp(struct p2p_data *p2p, u32 adv_id)
2706{
2707 struct p2ps_advertisement *adv_data;
2708 struct p2ps_advertisement **prior;
2709
2710 if (!p2p)
2711 return -1;
2712
2713 adv_data = p2p->p2ps_adv_list;
2714 prior = &p2p->p2ps_adv_list;
2715 while (adv_data) {
2716 if (adv_data->id == adv_id) {
2717 p2p_dbg(p2p, "Delete ASP adv_id=0x%x", adv_id);
2718 *prior = adv_data->next;
2719 os_free(adv_data);
2720 return 0;
2721 }
2722 prior = &adv_data->next;
2723 adv_data = adv_data->next;
2724 }
2725
2726 return -1;
2727}
2728
2729
2730int p2p_service_add_asp(struct p2p_data *p2p, int auto_accept, u32 adv_id,
2731 const char *adv_str, u8 svc_state, u16 config_methods,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002732 const char *svc_info, const u8 *cpt_priority)
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002733{
2734 struct p2ps_advertisement *adv_data, *tmp, **prev;
2735 u8 buf[P2PS_HASH_LEN];
2736 size_t adv_data_len, adv_len, info_len = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002737 int i;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002738
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002739 if (!p2p || !adv_str || !adv_str[0] || !cpt_priority)
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002740 return -1;
2741
2742 if (!(config_methods & p2p->cfg->config_methods)) {
2743 p2p_dbg(p2p, "Config methods not supported svc: 0x%x dev: 0x%x",
2744 config_methods, p2p->cfg->config_methods);
2745 return -1;
2746 }
2747
2748 if (!p2ps_gen_hash(p2p, adv_str, buf))
2749 return -1;
2750
2751 if (svc_info)
2752 info_len = os_strlen(svc_info);
2753 adv_len = os_strlen(adv_str);
2754 adv_data_len = sizeof(struct p2ps_advertisement) + adv_len + 1 +
2755 info_len + 1;
2756
2757 adv_data = os_zalloc(adv_data_len);
2758 if (!adv_data)
2759 return -1;
2760
2761 os_memcpy(adv_data->hash, buf, P2PS_HASH_LEN);
2762 adv_data->id = adv_id;
2763 adv_data->state = svc_state;
2764 adv_data->config_methods = config_methods & p2p->cfg->config_methods;
2765 adv_data->auto_accept = (u8) auto_accept;
2766 os_memcpy(adv_data->svc_name, adv_str, adv_len);
2767
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002768 for (i = 0; cpt_priority[i] && i < P2PS_FEATURE_CAPAB_CPT_MAX; i++) {
2769 adv_data->cpt_priority[i] = cpt_priority[i];
2770 adv_data->cpt_mask |= cpt_priority[i];
2771 }
2772
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002773 if (svc_info && info_len) {
2774 adv_data->svc_info = &adv_data->svc_name[adv_len + 1];
2775 os_memcpy(adv_data->svc_info, svc_info, info_len);
2776 }
2777
2778 /*
2779 * Group Advertisements by service string. They do not need to be
2780 * sorted, but groups allow easier Probe Response instance grouping
2781 */
2782 tmp = p2p->p2ps_adv_list;
2783 prev = &p2p->p2ps_adv_list;
2784 while (tmp) {
2785 if (tmp->id == adv_data->id) {
2786 if (os_strcmp(tmp->svc_name, adv_data->svc_name) != 0) {
2787 os_free(adv_data);
2788 return -1;
2789 }
2790 adv_data->next = tmp->next;
2791 *prev = adv_data;
2792 os_free(tmp);
2793 goto inserted;
2794 } else {
2795 if (os_strcmp(tmp->svc_name, adv_data->svc_name) == 0) {
2796 adv_data->next = tmp->next;
2797 tmp->next = adv_data;
2798 goto inserted;
2799 }
2800 }
2801 prev = &tmp->next;
2802 tmp = tmp->next;
2803 }
2804
2805 /* No svc_name match found */
2806 adv_data->next = p2p->p2ps_adv_list;
2807 p2p->p2ps_adv_list = adv_data;
2808
2809inserted:
2810 p2p_dbg(p2p,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002811 "Added ASP advertisement adv_id=0x%x config_methods=0x%x svc_state=0x%x adv_str='%s' cpt_mask=0x%x",
2812 adv_id, adv_data->config_methods, svc_state, adv_str,
2813 adv_data->cpt_mask);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002814
2815 return 0;
2816}
2817
2818
Dmitry Shmidtdda10c22015-03-24 16:05:01 -07002819void p2p_service_flush_asp(struct p2p_data *p2p)
2820{
2821 struct p2ps_advertisement *adv, *prev;
2822
2823 if (!p2p)
2824 return;
2825
2826 adv = p2p->p2ps_adv_list;
2827 while (adv) {
2828 prev = adv;
2829 adv = adv->next;
2830 os_free(prev);
2831 }
2832
2833 p2p->p2ps_adv_list = NULL;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002834 p2ps_prov_free(p2p);
Dmitry Shmidtdda10c22015-03-24 16:05:01 -07002835 p2p_dbg(p2p, "All ASP advertisements flushed");
2836}
2837
2838
Dmitry Shmidt04949592012-07-19 12:16:46 -07002839int p2p_parse_dev_addr_in_p2p_ie(struct wpabuf *p2p_ie, u8 *dev_addr)
2840{
2841 struct p2p_message msg;
2842
2843 os_memset(&msg, 0, sizeof(msg));
2844 if (p2p_parse_p2p_ie(p2p_ie, &msg))
2845 return -1;
2846
2847 if (msg.p2p_device_addr) {
2848 os_memcpy(dev_addr, msg.p2p_device_addr, ETH_ALEN);
2849 return 0;
2850 } else if (msg.device_id) {
2851 os_memcpy(dev_addr, msg.device_id, ETH_ALEN);
2852 return 0;
2853 }
2854 return -1;
2855}
2856
2857
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002858int p2p_parse_dev_addr(const u8 *ies, size_t ies_len, u8 *dev_addr)
2859{
2860 struct wpabuf *p2p_ie;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002861 int ret;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002862
2863 p2p_ie = ieee802_11_vendor_ie_concat(ies, ies_len,
2864 P2P_IE_VENDOR_TYPE);
2865 if (p2p_ie == NULL)
2866 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002867 ret = p2p_parse_dev_addr_in_p2p_ie(p2p_ie, dev_addr);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002868 wpabuf_free(p2p_ie);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002869 return ret;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002870}
2871
2872
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002873static void p2p_clear_go_neg(struct p2p_data *p2p)
2874{
2875 p2p->go_neg_peer = NULL;
2876 p2p_clear_timeout(p2p);
2877 p2p_set_state(p2p, P2P_IDLE);
2878}
2879
2880
2881void p2p_wps_success_cb(struct p2p_data *p2p, const u8 *mac_addr)
2882{
2883 if (p2p->go_neg_peer == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002884 p2p_dbg(p2p, "No pending Group Formation - ignore WPS registration success notification");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002885 return; /* No pending Group Formation */
2886 }
2887
2888 if (os_memcmp(mac_addr, p2p->go_neg_peer->intended_addr, ETH_ALEN) !=
2889 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002890 p2p_dbg(p2p, "Ignore WPS registration success notification for "
2891 MACSTR " (GO Negotiation peer " MACSTR ")",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002892 MAC2STR(mac_addr),
2893 MAC2STR(p2p->go_neg_peer->intended_addr));
2894 return; /* Ignore unexpected peer address */
2895 }
2896
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002897 p2p_dbg(p2p, "Group Formation completed successfully with " MACSTR,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002898 MAC2STR(mac_addr));
2899
2900 p2p_clear_go_neg(p2p);
2901}
2902
2903
2904void p2p_group_formation_failed(struct p2p_data *p2p)
2905{
2906 if (p2p->go_neg_peer == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002907 p2p_dbg(p2p, "No pending Group Formation - ignore group formation failure notification");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002908 return; /* No pending Group Formation */
2909 }
2910
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002911 p2p_dbg(p2p, "Group Formation failed with " MACSTR,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002912 MAC2STR(p2p->go_neg_peer->intended_addr));
2913
2914 p2p_clear_go_neg(p2p);
2915}
2916
2917
2918struct p2p_data * p2p_init(const struct p2p_config *cfg)
2919{
2920 struct p2p_data *p2p;
2921
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07002922 if (cfg->max_peers < 1 ||
2923 cfg->passphrase_len < 8 || cfg->passphrase_len > 63)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002924 return NULL;
2925
2926 p2p = os_zalloc(sizeof(*p2p) + sizeof(*cfg));
2927 if (p2p == NULL)
2928 return NULL;
2929 p2p->cfg = (struct p2p_config *) (p2p + 1);
2930 os_memcpy(p2p->cfg, cfg, sizeof(*cfg));
2931 if (cfg->dev_name)
2932 p2p->cfg->dev_name = os_strdup(cfg->dev_name);
2933 if (cfg->manufacturer)
2934 p2p->cfg->manufacturer = os_strdup(cfg->manufacturer);
2935 if (cfg->model_name)
2936 p2p->cfg->model_name = os_strdup(cfg->model_name);
2937 if (cfg->model_number)
2938 p2p->cfg->model_number = os_strdup(cfg->model_number);
2939 if (cfg->serial_number)
2940 p2p->cfg->serial_number = os_strdup(cfg->serial_number);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002941 if (cfg->pref_chan) {
2942 p2p->cfg->pref_chan = os_malloc(cfg->num_pref_chan *
2943 sizeof(struct p2p_channel));
2944 if (p2p->cfg->pref_chan) {
2945 os_memcpy(p2p->cfg->pref_chan, cfg->pref_chan,
2946 cfg->num_pref_chan *
2947 sizeof(struct p2p_channel));
2948 } else
2949 p2p->cfg->num_pref_chan = 0;
2950 }
2951
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002952 p2ps_gen_hash(p2p, P2PS_WILD_HASH_STR, p2p->wild_card_hash);
2953
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002954 p2p->min_disc_int = 1;
2955 p2p->max_disc_int = 3;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002956 p2p->max_disc_tu = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002957
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07002958 if (os_get_random(&p2p->next_tie_breaker, 1) < 0)
2959 p2p->next_tie_breaker = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002960 p2p->next_tie_breaker &= 0x01;
2961 if (cfg->sd_request)
2962 p2p->dev_capab |= P2P_DEV_CAPAB_SERVICE_DISCOVERY;
2963 p2p->dev_capab |= P2P_DEV_CAPAB_INVITATION_PROCEDURE;
2964 if (cfg->concurrent_operations)
2965 p2p->dev_capab |= P2P_DEV_CAPAB_CONCURRENT_OPER;
2966 p2p->dev_capab |= P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
2967
2968 dl_list_init(&p2p->devices);
2969
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002970 p2p->go_timeout = 100;
2971 p2p->client_timeout = 20;
Dmitry Shmidt13ca8d82014-02-20 10:18:40 -08002972 p2p->num_p2p_sd_queries = 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002973
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002974 p2p_dbg(p2p, "initialized");
2975 p2p_channels_dump(p2p, "channels", &p2p->cfg->channels);
2976 p2p_channels_dump(p2p, "cli_channels", &p2p->cfg->cli_channels);
2977
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002978 return p2p;
2979}
2980
2981
2982void p2p_deinit(struct p2p_data *p2p)
2983{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002984#ifdef CONFIG_WIFI_DISPLAY
2985 wpabuf_free(p2p->wfd_ie_beacon);
2986 wpabuf_free(p2p->wfd_ie_probe_req);
2987 wpabuf_free(p2p->wfd_ie_probe_resp);
2988 wpabuf_free(p2p->wfd_ie_assoc_req);
2989 wpabuf_free(p2p->wfd_ie_invitation);
2990 wpabuf_free(p2p->wfd_ie_prov_disc_req);
2991 wpabuf_free(p2p->wfd_ie_prov_disc_resp);
2992 wpabuf_free(p2p->wfd_ie_go_neg);
2993 wpabuf_free(p2p->wfd_dev_info);
2994 wpabuf_free(p2p->wfd_assoc_bssid);
2995 wpabuf_free(p2p->wfd_coupled_sink_info);
2996#endif /* CONFIG_WIFI_DISPLAY */
2997
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002998 eloop_cancel_timeout(p2p_scan_timeout, p2p, NULL);
Dmitry Shmidt9cdf1b92013-02-27 12:58:50 -08002999 eloop_cancel_timeout(p2p_go_neg_start, p2p, NULL);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003000 eloop_cancel_timeout(p2p_go_neg_wait_timeout, p2p, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003001 p2p_flush(p2p);
3002 p2p_free_req_dev_types(p2p);
3003 os_free(p2p->cfg->dev_name);
3004 os_free(p2p->cfg->manufacturer);
3005 os_free(p2p->cfg->model_name);
3006 os_free(p2p->cfg->model_number);
3007 os_free(p2p->cfg->serial_number);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003008 os_free(p2p->cfg->pref_chan);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003009 os_free(p2p->groups);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07003010 p2ps_prov_free(p2p);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003011 wpabuf_free(p2p->sd_resp);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003012 p2p_remove_wps_vendor_extensions(p2p);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003013 os_free(p2p->no_go_freq.range);
Dmitry Shmidtdda10c22015-03-24 16:05:01 -07003014 p2p_service_flush_asp(p2p);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003015
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003016 os_free(p2p);
3017}
3018
3019
3020void p2p_flush(struct p2p_data *p2p)
3021{
3022 struct p2p_device *dev, *prev;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003023
3024 p2p_ext_listen(p2p, 0, 0);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003025 p2p_stop_find(p2p);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003026 dl_list_for_each_safe(dev, prev, &p2p->devices, struct p2p_device,
3027 list) {
3028 dl_list_del(&dev->list);
3029 p2p_device_free(p2p, dev);
3030 }
3031 p2p_free_sd_queries(p2p);
3032 os_free(p2p->after_scan_tx);
3033 p2p->after_scan_tx = NULL;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003034 p2p->ssid_set = 0;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003035 p2ps_prov_free(p2p);
3036 p2p_reset_pending_pd(p2p);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003037}
3038
3039
3040int p2p_unauthorize(struct p2p_data *p2p, const u8 *addr)
3041{
3042 struct p2p_device *dev;
3043
3044 dev = p2p_get_device(p2p, addr);
3045 if (dev == NULL)
3046 return -1;
3047
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003048 p2p_dbg(p2p, "Unauthorizing " MACSTR, MAC2STR(addr));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003049
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003050 if (p2p->go_neg_peer == dev) {
3051 eloop_cancel_timeout(p2p_go_neg_wait_timeout, p2p, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003052 p2p->go_neg_peer = NULL;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003053 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003054
3055 dev->wps_method = WPS_NOT_READY;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003056 dev->oob_pw_id = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003057 dev->flags &= ~P2P_DEV_WAIT_GO_NEG_RESPONSE;
3058 dev->flags &= ~P2P_DEV_WAIT_GO_NEG_CONFIRM;
3059
3060 /* Check if after_scan_tx is for this peer. If so free it */
3061 if (p2p->after_scan_tx &&
3062 os_memcmp(addr, p2p->after_scan_tx->dst, ETH_ALEN) == 0) {
3063 os_free(p2p->after_scan_tx);
3064 p2p->after_scan_tx = NULL;
3065 }
3066
3067 return 0;
3068}
3069
3070
3071int p2p_set_dev_name(struct p2p_data *p2p, const char *dev_name)
3072{
3073 os_free(p2p->cfg->dev_name);
3074 if (dev_name) {
3075 p2p->cfg->dev_name = os_strdup(dev_name);
3076 if (p2p->cfg->dev_name == NULL)
3077 return -1;
3078 } else
3079 p2p->cfg->dev_name = NULL;
3080 return 0;
3081}
3082
3083
3084int p2p_set_manufacturer(struct p2p_data *p2p, const char *manufacturer)
3085{
3086 os_free(p2p->cfg->manufacturer);
3087 p2p->cfg->manufacturer = NULL;
3088 if (manufacturer) {
3089 p2p->cfg->manufacturer = os_strdup(manufacturer);
3090 if (p2p->cfg->manufacturer == NULL)
3091 return -1;
3092 }
3093
3094 return 0;
3095}
3096
3097
3098int p2p_set_model_name(struct p2p_data *p2p, const char *model_name)
3099{
3100 os_free(p2p->cfg->model_name);
3101 p2p->cfg->model_name = NULL;
3102 if (model_name) {
3103 p2p->cfg->model_name = os_strdup(model_name);
3104 if (p2p->cfg->model_name == NULL)
3105 return -1;
3106 }
3107
3108 return 0;
3109}
3110
3111
3112int p2p_set_model_number(struct p2p_data *p2p, const char *model_number)
3113{
3114 os_free(p2p->cfg->model_number);
3115 p2p->cfg->model_number = NULL;
3116 if (model_number) {
3117 p2p->cfg->model_number = os_strdup(model_number);
3118 if (p2p->cfg->model_number == NULL)
3119 return -1;
3120 }
3121
3122 return 0;
3123}
3124
3125
3126int p2p_set_serial_number(struct p2p_data *p2p, const char *serial_number)
3127{
3128 os_free(p2p->cfg->serial_number);
3129 p2p->cfg->serial_number = NULL;
3130 if (serial_number) {
3131 p2p->cfg->serial_number = os_strdup(serial_number);
3132 if (p2p->cfg->serial_number == NULL)
3133 return -1;
3134 }
3135
3136 return 0;
3137}
3138
3139
3140void p2p_set_config_methods(struct p2p_data *p2p, u16 config_methods)
3141{
3142 p2p->cfg->config_methods = config_methods;
3143}
3144
3145
3146void p2p_set_uuid(struct p2p_data *p2p, const u8 *uuid)
3147{
3148 os_memcpy(p2p->cfg->uuid, uuid, 16);
3149}
3150
3151
3152int p2p_set_pri_dev_type(struct p2p_data *p2p, const u8 *pri_dev_type)
3153{
3154 os_memcpy(p2p->cfg->pri_dev_type, pri_dev_type, 8);
3155 return 0;
3156}
3157
3158
3159int p2p_set_sec_dev_types(struct p2p_data *p2p, const u8 dev_types[][8],
3160 size_t num_dev_types)
3161{
3162 if (num_dev_types > P2P_SEC_DEVICE_TYPES)
3163 num_dev_types = P2P_SEC_DEVICE_TYPES;
3164 p2p->cfg->num_sec_dev_types = num_dev_types;
3165 os_memcpy(p2p->cfg->sec_dev_type, dev_types, num_dev_types * 8);
3166 return 0;
3167}
3168
3169
3170void p2p_remove_wps_vendor_extensions(struct p2p_data *p2p)
3171{
3172 int i;
3173
3174 for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) {
3175 wpabuf_free(p2p->wps_vendor_ext[i]);
3176 p2p->wps_vendor_ext[i] = NULL;
3177 }
3178}
3179
3180
3181int p2p_add_wps_vendor_extension(struct p2p_data *p2p,
3182 const struct wpabuf *vendor_ext)
3183{
3184 int i;
3185
3186 if (vendor_ext == NULL)
3187 return -1;
3188
3189 for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) {
3190 if (p2p->wps_vendor_ext[i] == NULL)
3191 break;
3192 }
3193 if (i >= P2P_MAX_WPS_VENDOR_EXT)
3194 return -1;
3195
3196 p2p->wps_vendor_ext[i] = wpabuf_dup(vendor_ext);
3197 if (p2p->wps_vendor_ext[i] == NULL)
3198 return -1;
3199
3200 return 0;
3201}
3202
3203
3204int p2p_set_country(struct p2p_data *p2p, const char *country)
3205{
3206 os_memcpy(p2p->cfg->country, country, 3);
3207 return 0;
3208}
3209
3210
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003211static int p2p_pre_find_operation(struct p2p_data *p2p, struct p2p_device *dev)
3212{
Dmitry Shmidtde47be72016-01-07 12:52:55 -08003213 int res;
3214
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003215 if (dev->sd_pending_bcast_queries == 0) {
3216 /* Initialize with total number of registered broadcast
3217 * SD queries. */
3218 dev->sd_pending_bcast_queries = p2p->num_p2p_sd_queries;
3219 }
3220
Dmitry Shmidtde47be72016-01-07 12:52:55 -08003221 res = p2p_start_sd(p2p, dev);
3222 if (res == -2)
3223 return -2;
3224 if (res == 0)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003225 return 1;
3226
3227 if (dev->req_config_methods &&
3228 !(dev->flags & P2P_DEV_PD_FOR_JOIN)) {
3229 p2p_dbg(p2p, "Send pending Provision Discovery Request to "
3230 MACSTR " (config methods 0x%x)",
3231 MAC2STR(dev->info.p2p_device_addr),
3232 dev->req_config_methods);
3233 if (p2p_send_prov_disc_req(p2p, dev, 0, 0) == 0)
3234 return 1;
3235 }
3236
3237 return 0;
3238}
3239
3240
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003241void p2p_continue_find(struct p2p_data *p2p)
3242{
3243 struct p2p_device *dev;
Dmitry Shmidtde47be72016-01-07 12:52:55 -08003244 int found, res;
Dmitry Shmidt13ca8d82014-02-20 10:18:40 -08003245
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003246 p2p_set_state(p2p, P2P_SEARCH);
3247
3248 /* Continue from the device following the last iteration */
3249 found = 0;
3250 dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
3251 if (dev == p2p->last_p2p_find_oper) {
3252 found = 1;
3253 continue;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003254 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003255 if (!found)
3256 continue;
Dmitry Shmidtde47be72016-01-07 12:52:55 -08003257 res = p2p_pre_find_operation(p2p, dev);
3258 if (res > 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003259 p2p->last_p2p_find_oper = dev;
3260 return;
3261 }
Dmitry Shmidtde47be72016-01-07 12:52:55 -08003262 if (res == -2)
3263 goto skip_sd;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003264 }
3265
3266 /*
3267 * Wrap around to the beginning of the list and continue until the last
3268 * iteration device.
3269 */
3270 dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
Dmitry Shmidtde47be72016-01-07 12:52:55 -08003271 res = p2p_pre_find_operation(p2p, dev);
3272 if (res > 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003273 p2p->last_p2p_find_oper = dev;
3274 return;
3275 }
Dmitry Shmidtde47be72016-01-07 12:52:55 -08003276 if (res == -2)
3277 goto skip_sd;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003278 if (dev == p2p->last_p2p_find_oper)
3279 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003280 }
3281
Dmitry Shmidtde47be72016-01-07 12:52:55 -08003282skip_sd:
3283 os_memset(p2p->sd_query_no_ack, 0, ETH_ALEN);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003284 p2p_listen_in_find(p2p, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003285}
3286
3287
3288static void p2p_sd_cb(struct p2p_data *p2p, int success)
3289{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003290 p2p_dbg(p2p, "Service Discovery Query TX callback: success=%d",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003291 success);
3292 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
3293
3294 if (!success) {
Dmitry Shmidtde47be72016-01-07 12:52:55 -08003295 if (p2p->sd_peer) {
3296 if (is_zero_ether_addr(p2p->sd_query_no_ack)) {
3297 os_memcpy(p2p->sd_query_no_ack,
3298 p2p->sd_peer->info.p2p_device_addr,
3299 ETH_ALEN);
3300 p2p_dbg(p2p,
3301 "First SD Query no-ACK in this search iteration: "
3302 MACSTR, MAC2STR(p2p->sd_query_no_ack));
3303 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003304 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
Dmitry Shmidtde47be72016-01-07 12:52:55 -08003305 }
Dmitry Shmidt13ca8d82014-02-20 10:18:40 -08003306 p2p->sd_peer = NULL;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003307 if (p2p->state != P2P_IDLE)
3308 p2p_continue_find(p2p);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003309 return;
3310 }
3311
3312 if (p2p->sd_peer == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003313 p2p_dbg(p2p, "No SD peer entry known");
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003314 if (p2p->state != P2P_IDLE)
3315 p2p_continue_find(p2p);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003316 return;
3317 }
3318
Dmitry Shmidt807291d2015-01-27 13:40:23 -08003319 if (p2p->sd_query && p2p->sd_query->for_all_peers) {
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07003320 /* Update the pending broadcast SD query count for this device
3321 */
3322 p2p->sd_peer->sd_pending_bcast_queries--;
3323
3324 /*
3325 * If there are no pending broadcast queries for this device,
3326 * mark it as done (-1).
3327 */
3328 if (p2p->sd_peer->sd_pending_bcast_queries == 0)
3329 p2p->sd_peer->sd_pending_bcast_queries = -1;
3330 }
3331
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003332 /* Wait for response from the peer */
3333 p2p_set_state(p2p, P2P_SD_DURING_FIND);
3334 p2p_set_timeout(p2p, 0, 200000);
3335}
3336
Jouni Malinen75ecf522011-06-27 15:19:46 -07003337
3338/**
3339 * p2p_retry_pd - Retry any pending provision disc requests in IDLE state
3340 * @p2p: P2P module context from p2p_init()
3341 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003342static void p2p_retry_pd(struct p2p_data *p2p)
Jouni Malinen75ecf522011-06-27 15:19:46 -07003343{
3344 struct p2p_device *dev;
3345
Jouni Malinen75ecf522011-06-27 15:19:46 -07003346 /*
3347 * Retry the prov disc req attempt only for the peer that the user had
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003348 * requested.
Jouni Malinen75ecf522011-06-27 15:19:46 -07003349 */
3350
3351 dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
3352 if (os_memcmp(p2p->pending_pd_devaddr,
3353 dev->info.p2p_device_addr, ETH_ALEN) != 0)
3354 continue;
3355 if (!dev->req_config_methods)
3356 continue;
Jouni Malinen75ecf522011-06-27 15:19:46 -07003357
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003358 p2p_dbg(p2p, "Send pending Provision Discovery Request to "
Jouni Malinen75ecf522011-06-27 15:19:46 -07003359 MACSTR " (config methods 0x%x)",
3360 MAC2STR(dev->info.p2p_device_addr),
3361 dev->req_config_methods);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003362 p2p_send_prov_disc_req(p2p, dev,
Dmitry Shmidt051af732013-10-22 13:52:46 -07003363 dev->flags & P2P_DEV_PD_FOR_JOIN,
3364 p2p->pd_force_freq);
Jouni Malinen75ecf522011-06-27 15:19:46 -07003365 return;
3366 }
3367}
3368
3369
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003370static void p2p_prov_disc_cb(struct p2p_data *p2p, int success)
3371{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003372 p2p_dbg(p2p, "Provision Discovery Request TX callback: success=%d",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003373 success);
Jouni Malinen75ecf522011-06-27 15:19:46 -07003374
3375 /*
3376 * Postpone resetting the pending action state till after we actually
3377 * time out. This allows us to take some action like notifying any
3378 * interested parties about no response to the request.
3379 *
3380 * When the timer (below) goes off we check in IDLE, SEARCH, or
3381 * LISTEN_ONLY state, which are the only allowed states to issue a PD
3382 * requests in, if this was still pending and then raise notification.
3383 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003384
3385 if (!success) {
Jouni Malinen75ecf522011-06-27 15:19:46 -07003386 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
3387
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003388 if (p2p->user_initiated_pd &&
3389 (p2p->state == P2P_SEARCH || p2p->state == P2P_LISTEN_ONLY))
3390 {
3391 /* Retry request from timeout to avoid busy loops */
3392 p2p->pending_action_state = P2P_PENDING_PD;
3393 p2p_set_timeout(p2p, 0, 50000);
3394 } else if (p2p->state != P2P_IDLE)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003395 p2p_continue_find(p2p);
Jouni Malinen75ecf522011-06-27 15:19:46 -07003396 else if (p2p->user_initiated_pd) {
3397 p2p->pending_action_state = P2P_PENDING_PD;
3398 p2p_set_timeout(p2p, 0, 300000);
3399 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003400 return;
3401 }
3402
Jouni Malinen75ecf522011-06-27 15:19:46 -07003403 /*
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003404 * If after PD Request the peer doesn't expect to receive PD Response
3405 * the PD Request ACK indicates a completion of the current PD. This
3406 * happens only on the advertiser side sending the follow-on PD Request
3407 * with the status different than 12 (Success: accepted by user).
3408 */
3409 if (p2p->p2ps_prov && !p2p->p2ps_prov->pd_seeker &&
3410 p2p->p2ps_prov->status != P2P_SC_SUCCESS_DEFERRED) {
3411 p2p_dbg(p2p, "P2PS PD completion on Follow-on PD Request ACK");
3412
3413 if (p2p->send_action_in_progress) {
3414 p2p->send_action_in_progress = 0;
3415 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
3416 }
3417
3418 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
3419
3420 if (p2p->cfg->p2ps_prov_complete) {
3421 p2p->cfg->p2ps_prov_complete(
3422 p2p->cfg->cb_ctx,
3423 p2p->p2ps_prov->status,
3424 p2p->p2ps_prov->adv_mac,
3425 p2p->p2ps_prov->adv_mac,
3426 p2p->p2ps_prov->session_mac,
3427 NULL, p2p->p2ps_prov->adv_id,
3428 p2p->p2ps_prov->session_id,
3429 0, 0, NULL, 0, 0, 0,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08003430 NULL, NULL, 0, 0, NULL, 0);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003431 }
3432
3433 if (p2p->user_initiated_pd)
3434 p2p_reset_pending_pd(p2p);
3435
3436 p2ps_prov_free(p2p);
3437 return;
3438 }
3439
3440 /*
Jouni Malinen75ecf522011-06-27 15:19:46 -07003441 * This postponing, of resetting pending_action_state, needs to be
3442 * done only for user initiated PD requests and not internal ones.
3443 */
3444 if (p2p->user_initiated_pd)
3445 p2p->pending_action_state = P2P_PENDING_PD;
3446 else
3447 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
3448
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003449 /* Wait for response from the peer */
3450 if (p2p->state == P2P_SEARCH)
3451 p2p_set_state(p2p, P2P_PD_DURING_FIND);
3452 p2p_set_timeout(p2p, 0, 200000);
3453}
3454
3455
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003456static int p2p_check_after_scan_tx_continuation(struct p2p_data *p2p)
3457{
3458 if (p2p->after_scan_tx_in_progress) {
3459 p2p->after_scan_tx_in_progress = 0;
3460 if (p2p->start_after_scan != P2P_AFTER_SCAN_NOTHING &&
3461 p2p_run_after_scan(p2p))
3462 return 1;
3463 if (p2p->state == P2P_SEARCH) {
3464 p2p_dbg(p2p, "Continue find after after_scan_tx completion");
3465 p2p_continue_find(p2p);
3466 }
3467 }
3468
3469 return 0;
3470}
3471
3472
3473static void p2p_prov_disc_resp_cb(struct p2p_data *p2p, int success)
3474{
3475 p2p_dbg(p2p, "Provision Discovery Response TX callback: success=%d",
3476 success);
3477
3478 if (p2p->send_action_in_progress) {
3479 p2p->send_action_in_progress = 0;
3480 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
3481 }
3482
3483 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
3484
3485 if (!success)
3486 goto continue_search;
3487
3488 if (!p2p->cfg->prov_disc_resp_cb ||
3489 p2p->cfg->prov_disc_resp_cb(p2p->cfg->cb_ctx) < 1)
3490 goto continue_search;
3491
3492 p2p_dbg(p2p,
3493 "Post-Provision Discovery operations started - do not try to continue other P2P operations");
3494 return;
3495
3496continue_search:
3497 p2p_check_after_scan_tx_continuation(p2p);
3498}
3499
3500
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003501int p2p_scan_res_handler(struct p2p_data *p2p, const u8 *bssid, int freq,
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003502 struct os_reltime *rx_time, int level, const u8 *ies,
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003503 size_t ies_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003504{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003505 if (os_reltime_before(rx_time, &p2p->find_start)) {
Dmitry Shmidtf8623282013-02-20 14:34:59 -08003506 /*
3507 * The driver may have cached (e.g., in cfg80211 BSS table) the
3508 * scan results for relatively long time. To avoid reporting
3509 * stale information, update P2P peers only based on results
3510 * that have based on frames received after the last p2p_find
3511 * operation was started.
3512 */
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003513 p2p_dbg(p2p, "Ignore old scan result for " MACSTR
Dmitry Shmidtde47be72016-01-07 12:52:55 -08003514 " (rx_time=%u.%06u find_start=%u.%06u)",
Dmitry Shmidtf8623282013-02-20 14:34:59 -08003515 MAC2STR(bssid), (unsigned int) rx_time->sec,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08003516 (unsigned int) rx_time->usec,
3517 (unsigned int) p2p->find_start.sec,
3518 (unsigned int) p2p->find_start.usec);
Dmitry Shmidtf8623282013-02-20 14:34:59 -08003519 return 0;
3520 }
3521
3522 p2p_add_device(p2p, bssid, freq, rx_time, level, ies, ies_len, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003523
3524 return 0;
3525}
3526
3527
3528void p2p_scan_res_handled(struct p2p_data *p2p)
3529{
3530 if (!p2p->p2p_scan_running) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003531 p2p_dbg(p2p, "p2p_scan was not running, but scan results received");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003532 }
3533 p2p->p2p_scan_running = 0;
3534 eloop_cancel_timeout(p2p_scan_timeout, p2p, NULL);
3535
3536 if (p2p_run_after_scan(p2p))
3537 return;
3538 if (p2p->state == P2P_SEARCH)
3539 p2p_continue_find(p2p);
3540}
3541
3542
Dmitry Shmidt9c175262016-03-03 10:20:07 -08003543void p2p_scan_ie(struct p2p_data *p2p, struct wpabuf *ies, const u8 *dev_id,
3544 unsigned int bands)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003545{
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003546 u8 dev_capab;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003547 u8 *len;
3548
3549#ifdef CONFIG_WIFI_DISPLAY
3550 if (p2p->wfd_ie_probe_req)
3551 wpabuf_put_buf(ies, p2p->wfd_ie_probe_req);
3552#endif /* CONFIG_WIFI_DISPLAY */
3553
Dmitry Shmidt2e67f062014-07-16 09:55:28 -07003554 if (p2p->vendor_elem && p2p->vendor_elem[VENDOR_ELEM_PROBE_REQ_P2P])
3555 wpabuf_put_buf(ies,
3556 p2p->vendor_elem[VENDOR_ELEM_PROBE_REQ_P2P]);
3557
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003558 len = p2p_buf_add_ie_hdr(ies);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003559
3560 dev_capab = p2p->dev_capab & ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
3561
3562 /* P2PS requires Probe Request frames to include SD bit */
3563 if (p2p->p2ps_seek && p2p->p2ps_seek_count)
3564 dev_capab |= P2P_DEV_CAPAB_SERVICE_DISCOVERY;
3565
3566 p2p_buf_add_capability(ies, dev_capab, 0);
3567
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003568 if (dev_id)
3569 p2p_buf_add_device_id(ies, dev_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003570 if (p2p->cfg->reg_class && p2p->cfg->channel)
3571 p2p_buf_add_listen_channel(ies, p2p->cfg->country,
3572 p2p->cfg->reg_class,
3573 p2p->cfg->channel);
3574 if (p2p->ext_listen_interval)
3575 p2p_buf_add_ext_listen_timing(ies, p2p->ext_listen_period,
3576 p2p->ext_listen_interval);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003577
Dmitry Shmidt9c175262016-03-03 10:20:07 -08003578 if (bands & BAND_60_GHZ)
3579 p2p_buf_add_device_info(ies, p2p, NULL);
3580
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003581 if (p2p->p2ps_seek && p2p->p2ps_seek_count)
3582 p2p_buf_add_service_hash(ies, p2p);
3583
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003584 /* TODO: p2p_buf_add_operating_channel() if GO */
3585 p2p_buf_update_ie_hdr(ies, len);
3586}
3587
3588
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003589size_t p2p_scan_ie_buf_len(struct p2p_data *p2p)
3590{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003591 size_t len = 100;
3592
3593#ifdef CONFIG_WIFI_DISPLAY
3594 if (p2p && p2p->wfd_ie_probe_req)
3595 len += wpabuf_len(p2p->wfd_ie_probe_req);
3596#endif /* CONFIG_WIFI_DISPLAY */
3597
Dmitry Shmidt2e67f062014-07-16 09:55:28 -07003598 if (p2p && p2p->vendor_elem &&
3599 p2p->vendor_elem[VENDOR_ELEM_PROBE_REQ_P2P])
3600 len += wpabuf_len(p2p->vendor_elem[VENDOR_ELEM_PROBE_REQ_P2P]);
3601
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003602 return len;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003603}
3604
3605
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003606int p2p_ie_text(struct wpabuf *p2p_ie, char *buf, char *end)
3607{
3608 return p2p_attr_text(p2p_ie, buf, end);
3609}
3610
3611
3612static void p2p_go_neg_req_cb(struct p2p_data *p2p, int success)
3613{
3614 struct p2p_device *dev = p2p->go_neg_peer;
Dmitry Shmidt8c652892013-03-01 10:14:01 -08003615 int timeout;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003616
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003617 p2p_dbg(p2p, "GO Negotiation Request TX callback: success=%d", success);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003618
3619 if (dev == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003620 p2p_dbg(p2p, "No pending GO Negotiation");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003621 return;
3622 }
3623
3624 if (success) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003625 if (dev->flags & P2P_DEV_USER_REJECTED) {
3626 p2p_set_state(p2p, P2P_IDLE);
3627 return;
3628 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07003629 } else if (dev->go_neg_req_sent) {
3630 /* Cancel the increment from p2p_connect_send() on failure */
Dmitry Shmidt98f9e762012-05-30 11:18:46 -07003631 dev->go_neg_req_sent--;
3632 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003633
3634 if (!success &&
3635 (dev->info.dev_capab & P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY) &&
3636 !is_zero_ether_addr(dev->member_in_go_dev)) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003637 p2p_dbg(p2p, "Peer " MACSTR " did not acknowledge request - try to use device discoverability through its GO",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003638 MAC2STR(dev->info.p2p_device_addr));
3639 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
3640 p2p_send_dev_disc_req(p2p, dev);
3641 return;
3642 }
3643
3644 /*
3645 * Use P2P find, if needed, to find the other device from its listen
3646 * channel.
3647 */
3648 p2p_set_state(p2p, P2P_CONNECT);
Dmitry Shmidt8c652892013-03-01 10:14:01 -08003649 timeout = success ? 500000 : 100000;
3650 if (!success && p2p->go_neg_peer &&
3651 (p2p->go_neg_peer->flags & P2P_DEV_PEER_WAITING_RESPONSE)) {
3652 unsigned int r;
3653 /*
3654 * Peer is expected to wait our response and we will skip the
3655 * listen phase. Add some randomness to the wait time here to
3656 * make it less likely to hit cases where we could end up in
3657 * sync with peer not listening.
3658 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07003659 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
3660 r = 0;
Dmitry Shmidt8c652892013-03-01 10:14:01 -08003661 timeout += r % 100000;
3662 }
3663 p2p_set_timeout(p2p, 0, timeout);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003664}
3665
3666
3667static void p2p_go_neg_resp_cb(struct p2p_data *p2p, int success)
3668{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003669 p2p_dbg(p2p, "GO Negotiation Response TX callback: success=%d",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003670 success);
3671 if (!p2p->go_neg_peer && p2p->state == P2P_PROVISIONING) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003672 p2p_dbg(p2p, "Ignore TX callback event - GO Negotiation is not running anymore");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003673 return;
3674 }
3675 p2p_set_state(p2p, P2P_CONNECT);
Dmitry Shmidt8c652892013-03-01 10:14:01 -08003676 p2p_set_timeout(p2p, 0, 500000);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003677}
3678
3679
Dmitry Shmidt4b060592013-04-29 16:42:49 -07003680static void p2p_go_neg_resp_failure_cb(struct p2p_data *p2p, int success,
3681 const u8 *addr)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003682{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003683 p2p_dbg(p2p, "GO Negotiation Response (failure) TX callback: success=%d", success);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003684 if (p2p->go_neg_peer && p2p->go_neg_peer->status != P2P_SC_SUCCESS) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003685 p2p_go_neg_failed(p2p, p2p->go_neg_peer->status);
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08003686 return;
3687 }
3688
3689 if (success) {
Dmitry Shmidt4b060592013-04-29 16:42:49 -07003690 struct p2p_device *dev;
3691 dev = p2p_get_device(p2p, addr);
3692 if (dev &&
Dmitry Shmidt34c12022015-03-05 14:11:20 -08003693 dev->status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE)
Dmitry Shmidt4b060592013-04-29 16:42:49 -07003694 dev->flags |= P2P_DEV_PEER_WAITING_RESPONSE;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003695 }
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08003696
3697 if (p2p->state == P2P_SEARCH || p2p->state == P2P_SD_DURING_FIND)
3698 p2p_continue_find(p2p);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003699}
3700
3701
3702static void p2p_go_neg_conf_cb(struct p2p_data *p2p,
3703 enum p2p_send_action_result result)
3704{
3705 struct p2p_device *dev;
3706
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003707 p2p_dbg(p2p, "GO Negotiation Confirm TX callback: result=%d", result);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003708 if (result == P2P_SEND_ACTION_FAILED) {
Dmitry Shmidt413dde72014-04-11 10:23:22 -07003709 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003710 p2p_go_neg_failed(p2p, -1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003711 return;
3712 }
Dmitry Shmidt413dde72014-04-11 10:23:22 -07003713
3714 dev = p2p->go_neg_peer;
3715
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003716 if (result == P2P_SEND_ACTION_NO_ACK) {
3717 /*
Dmitry Shmidt413dde72014-04-11 10:23:22 -07003718 * Retry GO Negotiation Confirmation
3719 * P2P_GO_NEG_CNF_MAX_RETRY_COUNT times if we did not receive
3720 * ACK for confirmation.
3721 */
3722 if (dev && dev->go_neg_conf &&
3723 dev->go_neg_conf_sent <= P2P_GO_NEG_CNF_MAX_RETRY_COUNT) {
3724 p2p_dbg(p2p, "GO Negotiation Confirm retry %d",
3725 dev->go_neg_conf_sent);
3726 p2p->pending_action_state = P2P_PENDING_GO_NEG_CONFIRM;
3727 if (p2p_send_action(p2p, dev->go_neg_conf_freq,
3728 dev->info.p2p_device_addr,
3729 p2p->cfg->dev_addr,
3730 dev->info.p2p_device_addr,
3731 wpabuf_head(dev->go_neg_conf),
3732 wpabuf_len(dev->go_neg_conf), 0) >=
3733 0) {
3734 dev->go_neg_conf_sent++;
3735 return;
3736 }
3737 p2p_dbg(p2p, "Failed to re-send Action frame");
3738
3739 /*
3740 * Continue with the assumption that the first attempt
3741 * went through and just the ACK frame was lost.
3742 */
3743 }
3744
3745 /*
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003746 * It looks like the TX status for GO Negotiation Confirm is
3747 * often showing failure even when the peer has actually
3748 * received the frame. Since the peer may change channels
3749 * immediately after having received the frame, we may not see
3750 * an Ack for retries, so just dropping a single frame may
3751 * trigger this. To allow the group formation to succeed if the
3752 * peer did indeed receive the frame, continue regardless of
3753 * the TX status.
3754 */
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003755 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 -07003756 }
3757
Dmitry Shmidt413dde72014-04-11 10:23:22 -07003758 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
3759
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003760 if (dev == NULL)
3761 return;
3762
3763 p2p_go_complete(p2p, dev);
3764}
3765
3766
3767void p2p_send_action_cb(struct p2p_data *p2p, unsigned int freq, const u8 *dst,
3768 const u8 *src, const u8 *bssid,
3769 enum p2p_send_action_result result)
3770{
3771 enum p2p_pending_action_state state;
3772 int success;
3773
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003774 p2p_dbg(p2p, "Action frame TX callback (state=%d freq=%u dst=" MACSTR
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003775 " src=" MACSTR " bssid=" MACSTR " result=%d p2p_state=%s)",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003776 p2p->pending_action_state, freq, MAC2STR(dst), MAC2STR(src),
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003777 MAC2STR(bssid), result, p2p_state_txt(p2p->state));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003778 success = result == P2P_SEND_ACTION_SUCCESS;
3779 state = p2p->pending_action_state;
3780 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
3781 switch (state) {
3782 case P2P_NO_PENDING_ACTION:
Dmitry Shmidt4582d2a2014-02-28 11:14:23 -08003783 if (p2p->send_action_in_progress) {
3784 p2p->send_action_in_progress = 0;
3785 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
3786 }
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003787 p2p_check_after_scan_tx_continuation(p2p);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003788 break;
3789 case P2P_PENDING_GO_NEG_REQUEST:
3790 p2p_go_neg_req_cb(p2p, success);
3791 break;
3792 case P2P_PENDING_GO_NEG_RESPONSE:
3793 p2p_go_neg_resp_cb(p2p, success);
3794 break;
3795 case P2P_PENDING_GO_NEG_RESPONSE_FAILURE:
Dmitry Shmidt4b060592013-04-29 16:42:49 -07003796 p2p_go_neg_resp_failure_cb(p2p, success, dst);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003797 break;
3798 case P2P_PENDING_GO_NEG_CONFIRM:
3799 p2p_go_neg_conf_cb(p2p, result);
3800 break;
3801 case P2P_PENDING_SD:
3802 p2p_sd_cb(p2p, success);
3803 break;
3804 case P2P_PENDING_PD:
3805 p2p_prov_disc_cb(p2p, success);
3806 break;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003807 case P2P_PENDING_PD_RESPONSE:
3808 p2p_prov_disc_resp_cb(p2p, success);
3809 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003810 case P2P_PENDING_INVITATION_REQUEST:
3811 p2p_invitation_req_cb(p2p, success);
3812 break;
3813 case P2P_PENDING_INVITATION_RESPONSE:
3814 p2p_invitation_resp_cb(p2p, success);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003815 if (p2p->inv_status != P2P_SC_SUCCESS)
3816 p2p_check_after_scan_tx_continuation(p2p);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003817 break;
3818 case P2P_PENDING_DEV_DISC_REQUEST:
3819 p2p_dev_disc_req_cb(p2p, success);
3820 break;
3821 case P2P_PENDING_DEV_DISC_RESPONSE:
3822 p2p_dev_disc_resp_cb(p2p, success);
3823 break;
3824 case P2P_PENDING_GO_DISC_REQ:
3825 p2p_go_disc_req_cb(p2p, success);
3826 break;
3827 }
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07003828
3829 p2p->after_scan_tx_in_progress = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003830}
3831
3832
3833void p2p_listen_cb(struct p2p_data *p2p, unsigned int freq,
3834 unsigned int duration)
3835{
3836 if (freq == p2p->pending_client_disc_freq) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003837 p2p_dbg(p2p, "Client discoverability remain-awake completed");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003838 p2p->pending_client_disc_freq = 0;
3839 return;
3840 }
3841
3842 if (freq != p2p->pending_listen_freq) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003843 p2p_dbg(p2p, "Unexpected listen callback for freq=%u duration=%u (pending_listen_freq=%u)",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003844 freq, duration, p2p->pending_listen_freq);
3845 return;
3846 }
3847
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003848 p2p_dbg(p2p, "Starting Listen timeout(%u,%u) on freq=%u based on callback",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003849 p2p->pending_listen_sec, p2p->pending_listen_usec,
3850 p2p->pending_listen_freq);
3851 p2p->in_listen = 1;
3852 p2p->drv_in_listen = freq;
3853 if (p2p->pending_listen_sec || p2p->pending_listen_usec) {
3854 /*
3855 * Add 20 msec extra wait to avoid race condition with driver
3856 * remain-on-channel end event, i.e., give driver more time to
3857 * complete the operation before our timeout expires.
3858 */
3859 p2p_set_timeout(p2p, p2p->pending_listen_sec,
3860 p2p->pending_listen_usec + 20000);
3861 }
3862
3863 p2p->pending_listen_freq = 0;
3864}
3865
3866
3867int p2p_listen_end(struct p2p_data *p2p, unsigned int freq)
3868{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003869 p2p_dbg(p2p, "Driver ended Listen state (freq=%u)", freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003870 p2p->drv_in_listen = 0;
3871 if (p2p->in_listen)
3872 return 0; /* Internal timeout will trigger the next step */
3873
3874 if (p2p->state == P2P_CONNECT_LISTEN && p2p->go_neg_peer) {
3875 if (p2p->go_neg_peer->connect_reqs >= 120) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003876 p2p_dbg(p2p, "Timeout on sending GO Negotiation Request without getting response");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003877 p2p_go_neg_failed(p2p, -1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003878 return 0;
3879 }
3880
3881 p2p_set_state(p2p, P2P_CONNECT);
3882 p2p_connect_send(p2p, p2p->go_neg_peer);
3883 return 1;
3884 } else if (p2p->state == P2P_SEARCH) {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003885 if (p2p->p2p_scan_running) {
3886 /*
3887 * Search is already in progress. This can happen if
3888 * an Action frame RX is reported immediately after
3889 * the end of a remain-on-channel operation and the
3890 * response frame to that is sent using an offchannel
3891 * operation while in p2p_find. Avoid an attempt to
3892 * restart a scan here.
3893 */
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003894 p2p_dbg(p2p, "p2p_scan already in progress - do not try to start a new one");
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003895 return 1;
3896 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07003897 if (p2p->pending_listen_freq) {
3898 /*
3899 * Better wait a bit if the driver is unable to start
3900 * offchannel operation for some reason. p2p_search()
3901 * will be started from internal timeout.
3902 */
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003903 p2p_dbg(p2p, "Listen operation did not seem to start - delay search phase to avoid busy loop");
Dmitry Shmidt04949592012-07-19 12:16:46 -07003904 p2p_set_timeout(p2p, 0, 100000);
3905 return 1;
3906 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003907 if (p2p->search_delay) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003908 p2p_dbg(p2p, "Delay search operation by %u ms",
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003909 p2p->search_delay);
3910 p2p_set_timeout(p2p, p2p->search_delay / 1000,
3911 (p2p->search_delay % 1000) * 1000);
3912 return 1;
3913 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003914 p2p_search(p2p);
3915 return 1;
3916 }
3917
3918 return 0;
3919}
3920
3921
3922static void p2p_timeout_connect(struct p2p_data *p2p)
3923{
3924 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003925 if (p2p->go_neg_peer &&
3926 (p2p->go_neg_peer->flags & P2P_DEV_WAIT_GO_NEG_CONFIRM)) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003927 p2p_dbg(p2p, "Wait for GO Negotiation Confirm timed out - assume GO Negotiation failed");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003928 p2p_go_neg_failed(p2p, -1);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003929 return;
3930 }
Dmitry Shmidt8c652892013-03-01 10:14:01 -08003931 if (p2p->go_neg_peer &&
3932 (p2p->go_neg_peer->flags & P2P_DEV_PEER_WAITING_RESPONSE) &&
3933 p2p->go_neg_peer->connect_reqs < 120) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003934 p2p_dbg(p2p, "Peer expected to wait our response - skip listen");
Dmitry Shmidt8c652892013-03-01 10:14:01 -08003935 p2p_connect_send(p2p, p2p->go_neg_peer);
3936 return;
3937 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003938 if (p2p->go_neg_peer && p2p->go_neg_peer->oob_go_neg_freq > 0) {
3939 p2p_dbg(p2p, "Skip connect-listen since GO Neg channel known (OOB)");
3940 p2p_set_state(p2p, P2P_CONNECT_LISTEN);
3941 p2p_set_timeout(p2p, 0, 30000);
3942 return;
3943 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003944 p2p_set_state(p2p, P2P_CONNECT_LISTEN);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003945 p2p_listen_in_find(p2p, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003946}
3947
3948
3949static void p2p_timeout_connect_listen(struct p2p_data *p2p)
3950{
3951 if (p2p->go_neg_peer) {
3952 if (p2p->drv_in_listen) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003953 p2p_dbg(p2p, "Driver is still in Listen state; wait for it to complete");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003954 return;
3955 }
3956
3957 if (p2p->go_neg_peer->connect_reqs >= 120) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003958 p2p_dbg(p2p, "Timeout on sending GO Negotiation Request without getting response");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003959 p2p_go_neg_failed(p2p, -1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003960 return;
3961 }
3962
3963 p2p_set_state(p2p, P2P_CONNECT);
3964 p2p_connect_send(p2p, p2p->go_neg_peer);
3965 } else
3966 p2p_set_state(p2p, P2P_IDLE);
3967}
3968
3969
3970static void p2p_timeout_wait_peer_connect(struct p2p_data *p2p)
3971{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003972 p2p_set_state(p2p, P2P_WAIT_PEER_IDLE);
Dmitry Shmidt18463232014-01-24 12:29:41 -08003973
3974 if (p2p->cfg->is_concurrent_session_active &&
3975 p2p->cfg->is_concurrent_session_active(p2p->cfg->cb_ctx))
3976 p2p_set_timeout(p2p, 0, 500000);
3977 else
3978 p2p_set_timeout(p2p, 0, 200000);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003979}
3980
3981
3982static void p2p_timeout_wait_peer_idle(struct p2p_data *p2p)
3983{
3984 struct p2p_device *dev = p2p->go_neg_peer;
3985
3986 if (dev == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003987 p2p_dbg(p2p, "Unknown GO Neg peer - stop GO Neg wait");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003988 return;
3989 }
3990
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003991 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 -07003992 p2p_set_state(p2p, P2P_WAIT_PEER_CONNECT);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003993 p2p_listen_in_find(p2p, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003994}
3995
3996
3997static void p2p_timeout_sd_during_find(struct p2p_data *p2p)
3998{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003999 p2p_dbg(p2p, "Service Discovery Query timeout");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004000 if (p2p->sd_peer) {
4001 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004002 p2p->sd_peer = NULL;
4003 }
4004 p2p_continue_find(p2p);
4005}
4006
4007
4008static void p2p_timeout_prov_disc_during_find(struct p2p_data *p2p)
4009{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004010 p2p_dbg(p2p, "Provision Discovery Request timeout");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004011 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
4012 p2p_continue_find(p2p);
4013}
4014
4015
Jouni Malinen75ecf522011-06-27 15:19:46 -07004016static void p2p_timeout_prov_disc_req(struct p2p_data *p2p)
4017{
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004018 u32 adv_id = 0;
4019 u8 *adv_mac = NULL;
4020
Jouni Malinen75ecf522011-06-27 15:19:46 -07004021 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
4022
4023 /*
4024 * For user initiated PD requests that we have not gotten any responses
4025 * for while in IDLE state, we retry them a couple of times before
4026 * giving up.
4027 */
4028 if (!p2p->user_initiated_pd)
4029 return;
4030
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004031 p2p_dbg(p2p, "User initiated Provision Discovery Request timeout");
Jouni Malinen75ecf522011-06-27 15:19:46 -07004032
4033 if (p2p->pd_retries) {
4034 p2p->pd_retries--;
4035 p2p_retry_pd(p2p);
4036 } else {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004037 struct p2p_device *dev;
4038 int for_join = 0;
4039
4040 dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
4041 if (os_memcmp(p2p->pending_pd_devaddr,
4042 dev->info.p2p_device_addr, ETH_ALEN) != 0)
4043 continue;
4044 if (dev->req_config_methods &&
4045 (dev->flags & P2P_DEV_PD_FOR_JOIN))
4046 for_join = 1;
4047 }
4048
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004049 if (p2p->p2ps_prov) {
4050 adv_id = p2p->p2ps_prov->adv_id;
4051 adv_mac = p2p->p2ps_prov->adv_mac;
4052 }
4053
Jouni Malinen75ecf522011-06-27 15:19:46 -07004054 if (p2p->cfg->prov_disc_fail)
4055 p2p->cfg->prov_disc_fail(p2p->cfg->cb_ctx,
4056 p2p->pending_pd_devaddr,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004057 for_join ?
4058 P2P_PROV_DISC_TIMEOUT_JOIN :
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004059 P2P_PROV_DISC_TIMEOUT,
4060 adv_id, adv_mac, NULL);
Jouni Malinen75ecf522011-06-27 15:19:46 -07004061 p2p_reset_pending_pd(p2p);
4062 }
4063}
4064
4065
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004066static void p2p_timeout_invite(struct p2p_data *p2p)
4067{
4068 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
4069 p2p_set_state(p2p, P2P_INVITE_LISTEN);
4070 if (p2p->inv_role == P2P_INVITE_ROLE_ACTIVE_GO) {
4071 /*
4072 * Better remain on operating channel instead of listen channel
4073 * when running a group.
4074 */
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004075 p2p_dbg(p2p, "Inviting in active GO role - wait on operating channel");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004076 p2p_set_timeout(p2p, 0, 100000);
4077 return;
4078 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004079 p2p_listen_in_find(p2p, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004080}
4081
4082
4083static void p2p_timeout_invite_listen(struct p2p_data *p2p)
4084{
4085 if (p2p->invite_peer && p2p->invite_peer->invitation_reqs < 100) {
4086 p2p_set_state(p2p, P2P_INVITE);
4087 p2p_invite_send(p2p, p2p->invite_peer,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004088 p2p->invite_go_dev_addr, p2p->invite_dev_pw_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004089 } else {
4090 if (p2p->invite_peer) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004091 p2p_dbg(p2p, "Invitation Request retry limit reached");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004092 if (p2p->cfg->invitation_result)
4093 p2p->cfg->invitation_result(
Dmitry Shmidt700a1372013-03-15 14:14:44 -07004094 p2p->cfg->cb_ctx, -1, NULL, NULL,
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004095 p2p->invite_peer->info.p2p_device_addr,
Dmitry Shmidt15907092014-03-25 10:42:57 -07004096 0, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004097 }
4098 p2p_set_state(p2p, P2P_IDLE);
4099 }
4100}
4101
4102
4103static void p2p_state_timeout(void *eloop_ctx, void *timeout_ctx)
4104{
4105 struct p2p_data *p2p = eloop_ctx;
4106
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004107 p2p_dbg(p2p, "Timeout (state=%s)", p2p_state_txt(p2p->state));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004108
4109 p2p->in_listen = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004110 if (p2p->drv_in_listen) {
4111 p2p_dbg(p2p, "Driver is still in listen state - stop it");
4112 p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
4113 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004114
4115 switch (p2p->state) {
4116 case P2P_IDLE:
Jouni Malinen75ecf522011-06-27 15:19:46 -07004117 /* Check if we timed out waiting for PD req */
4118 if (p2p->pending_action_state == P2P_PENDING_PD)
4119 p2p_timeout_prov_disc_req(p2p);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004120 break;
4121 case P2P_SEARCH:
Jouni Malinen75ecf522011-06-27 15:19:46 -07004122 /* Check if we timed out waiting for PD req */
4123 if (p2p->pending_action_state == P2P_PENDING_PD)
4124 p2p_timeout_prov_disc_req(p2p);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004125 if (p2p->search_delay && !p2p->in_search_delay) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004126 p2p_dbg(p2p, "Delay search operation by %u ms",
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004127 p2p->search_delay);
4128 p2p->in_search_delay = 1;
4129 p2p_set_timeout(p2p, p2p->search_delay / 1000,
4130 (p2p->search_delay % 1000) * 1000);
4131 break;
4132 }
4133 p2p->in_search_delay = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004134 p2p_search(p2p);
4135 break;
4136 case P2P_CONNECT:
4137 p2p_timeout_connect(p2p);
4138 break;
4139 case P2P_CONNECT_LISTEN:
4140 p2p_timeout_connect_listen(p2p);
4141 break;
4142 case P2P_GO_NEG:
4143 break;
4144 case P2P_LISTEN_ONLY:
Jouni Malinen75ecf522011-06-27 15:19:46 -07004145 /* Check if we timed out waiting for PD req */
4146 if (p2p->pending_action_state == P2P_PENDING_PD)
4147 p2p_timeout_prov_disc_req(p2p);
4148
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004149 if (p2p->ext_listen_only) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004150 p2p_dbg(p2p, "Extended Listen Timing - Listen State completed");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004151 p2p->ext_listen_only = 0;
4152 p2p_set_state(p2p, P2P_IDLE);
4153 }
4154 break;
4155 case P2P_WAIT_PEER_CONNECT:
4156 p2p_timeout_wait_peer_connect(p2p);
4157 break;
4158 case P2P_WAIT_PEER_IDLE:
4159 p2p_timeout_wait_peer_idle(p2p);
4160 break;
4161 case P2P_SD_DURING_FIND:
4162 p2p_timeout_sd_during_find(p2p);
4163 break;
4164 case P2P_PROVISIONING:
4165 break;
4166 case P2P_PD_DURING_FIND:
4167 p2p_timeout_prov_disc_during_find(p2p);
4168 break;
4169 case P2P_INVITE:
4170 p2p_timeout_invite(p2p);
4171 break;
4172 case P2P_INVITE_LISTEN:
4173 p2p_timeout_invite_listen(p2p);
4174 break;
4175 }
4176}
4177
4178
4179int p2p_reject(struct p2p_data *p2p, const u8 *peer_addr)
4180{
4181 struct p2p_device *dev;
4182
4183 dev = p2p_get_device(p2p, peer_addr);
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004184 p2p_dbg(p2p, "Local request to reject connection attempts by peer "
4185 MACSTR, MAC2STR(peer_addr));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004186 if (dev == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004187 p2p_dbg(p2p, "Peer " MACSTR " unknown", MAC2STR(peer_addr));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004188 return -1;
4189 }
4190 dev->status = P2P_SC_FAIL_REJECTED_BY_USER;
4191 dev->flags |= P2P_DEV_USER_REJECTED;
4192 return 0;
4193}
4194
4195
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004196const char * p2p_wps_method_text(enum p2p_wps_method method)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004197{
4198 switch (method) {
4199 case WPS_NOT_READY:
4200 return "not-ready";
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004201 case WPS_PIN_DISPLAY:
4202 return "Display";
4203 case WPS_PIN_KEYPAD:
4204 return "Keypad";
4205 case WPS_PBC:
4206 return "PBC";
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004207 case WPS_NFC:
4208 return "NFC";
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004209 case WPS_P2PS:
4210 return "P2PS";
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004211 }
4212
4213 return "??";
4214}
4215
4216
4217static const char * p2p_go_state_text(enum p2p_go_state go_state)
4218{
4219 switch (go_state) {
4220 case UNKNOWN_GO:
4221 return "unknown";
4222 case LOCAL_GO:
4223 return "local";
4224 case REMOTE_GO:
4225 return "remote";
4226 }
4227
4228 return "??";
4229}
4230
4231
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004232const struct p2p_peer_info * p2p_get_peer_info(struct p2p_data *p2p,
4233 const u8 *addr, int next)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004234{
4235 struct p2p_device *dev;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004236
4237 if (addr)
4238 dev = p2p_get_device(p2p, addr);
4239 else
4240 dev = dl_list_first(&p2p->devices, struct p2p_device, list);
4241
4242 if (dev && next) {
4243 dev = dl_list_first(&dev->list, struct p2p_device, list);
4244 if (&dev->list == &p2p->devices)
4245 dev = NULL;
4246 }
4247
4248 if (dev == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004249 return NULL;
4250
4251 return &dev->info;
4252}
4253
4254
4255int p2p_get_peer_info_txt(const struct p2p_peer_info *info,
4256 char *buf, size_t buflen)
4257{
4258 struct p2p_device *dev;
4259 int res;
4260 char *pos, *end;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004261 struct os_reltime now;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004262
4263 if (info == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004264 return -1;
4265
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004266 dev = (struct p2p_device *) (((u8 *) info) -
4267 offsetof(struct p2p_device, info));
4268
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004269 pos = buf;
4270 end = buf + buflen;
4271
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004272 os_get_reltime(&now);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004273 res = os_snprintf(pos, end - pos,
4274 "age=%d\n"
4275 "listen_freq=%d\n"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004276 "wps_method=%s\n"
4277 "interface_addr=" MACSTR "\n"
4278 "member_in_go_dev=" MACSTR "\n"
4279 "member_in_go_iface=" MACSTR "\n"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004280 "go_neg_req_sent=%d\n"
4281 "go_state=%s\n"
4282 "dialog_token=%u\n"
4283 "intended_addr=" MACSTR "\n"
4284 "country=%c%c\n"
4285 "oper_freq=%d\n"
4286 "req_config_methods=0x%x\n"
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07004287 "flags=%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004288 "status=%d\n"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004289 "invitation_reqs=%u\n",
4290 (int) (now.sec - dev->last_seen.sec),
4291 dev->listen_freq,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004292 p2p_wps_method_text(dev->wps_method),
4293 MAC2STR(dev->interface_addr),
4294 MAC2STR(dev->member_in_go_dev),
4295 MAC2STR(dev->member_in_go_iface),
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004296 dev->go_neg_req_sent,
4297 p2p_go_state_text(dev->go_state),
4298 dev->dialog_token,
4299 MAC2STR(dev->intended_addr),
4300 dev->country[0] ? dev->country[0] : '_',
4301 dev->country[1] ? dev->country[1] : '_',
4302 dev->oper_freq,
4303 dev->req_config_methods,
4304 dev->flags & P2P_DEV_PROBE_REQ_ONLY ?
4305 "[PROBE_REQ_ONLY]" : "",
4306 dev->flags & P2P_DEV_REPORTED ? "[REPORTED]" : "",
4307 dev->flags & P2P_DEV_NOT_YET_READY ?
4308 "[NOT_YET_READY]" : "",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004309 dev->flags & P2P_DEV_PD_PEER_DISPLAY ?
4310 "[PD_PEER_DISPLAY]" : "",
4311 dev->flags & P2P_DEV_PD_PEER_KEYPAD ?
4312 "[PD_PEER_KEYPAD]" : "",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004313 dev->flags & P2P_DEV_PD_PEER_P2PS ?
4314 "[PD_PEER_P2PS]" : "",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004315 dev->flags & P2P_DEV_USER_REJECTED ?
4316 "[USER_REJECTED]" : "",
4317 dev->flags & P2P_DEV_PEER_WAITING_RESPONSE ?
4318 "[PEER_WAITING_RESPONSE]" : "",
4319 dev->flags & P2P_DEV_PREFER_PERSISTENT_GROUP ?
4320 "[PREFER_PERSISTENT_GROUP]" : "",
4321 dev->flags & P2P_DEV_WAIT_GO_NEG_RESPONSE ?
4322 "[WAIT_GO_NEG_RESPONSE]" : "",
4323 dev->flags & P2P_DEV_WAIT_GO_NEG_CONFIRM ?
4324 "[WAIT_GO_NEG_CONFIRM]" : "",
4325 dev->flags & P2P_DEV_GROUP_CLIENT_ONLY ?
4326 "[GROUP_CLIENT_ONLY]" : "",
4327 dev->flags & P2P_DEV_FORCE_FREQ ?
4328 "[FORCE_FREQ]" : "",
4329 dev->flags & P2P_DEV_PD_FOR_JOIN ?
4330 "[PD_FOR_JOIN]" : "",
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07004331 dev->flags & P2P_DEV_LAST_SEEN_AS_GROUP_CLIENT ?
4332 "[LAST_SEEN_AS_GROUP_CLIENT]" : "",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004333 dev->status,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004334 dev->invitation_reqs);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004335 if (os_snprintf_error(end - pos, res))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004336 return pos - buf;
4337 pos += res;
4338
4339 if (dev->ext_listen_period) {
4340 res = os_snprintf(pos, end - pos,
4341 "ext_listen_period=%u\n"
4342 "ext_listen_interval=%u\n",
4343 dev->ext_listen_period,
4344 dev->ext_listen_interval);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004345 if (os_snprintf_error(end - pos, res))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004346 return pos - buf;
4347 pos += res;
4348 }
4349
4350 if (dev->oper_ssid_len) {
4351 res = os_snprintf(pos, end - pos,
4352 "oper_ssid=%s\n",
4353 wpa_ssid_txt(dev->oper_ssid,
4354 dev->oper_ssid_len));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004355 if (os_snprintf_error(end - pos, res))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004356 return pos - buf;
4357 pos += res;
4358 }
4359
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004360#ifdef CONFIG_WIFI_DISPLAY
4361 if (dev->info.wfd_subelems) {
4362 res = os_snprintf(pos, end - pos, "wfd_subelems=");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004363 if (os_snprintf_error(end - pos, res))
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004364 return pos - buf;
4365 pos += res;
4366
4367 pos += wpa_snprintf_hex(pos, end - pos,
4368 wpabuf_head(dev->info.wfd_subelems),
4369 wpabuf_len(dev->info.wfd_subelems));
4370
4371 res = os_snprintf(pos, end - pos, "\n");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004372 if (os_snprintf_error(end - pos, res))
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004373 return pos - buf;
4374 pos += res;
4375 }
4376#endif /* CONFIG_WIFI_DISPLAY */
4377
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004378 return pos - buf;
4379}
4380
4381
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004382int p2p_peer_known(struct p2p_data *p2p, const u8 *addr)
4383{
4384 return p2p_get_device(p2p, addr) != NULL;
4385}
4386
4387
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004388void p2p_set_client_discoverability(struct p2p_data *p2p, int enabled)
4389{
4390 if (enabled) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004391 p2p_dbg(p2p, "Client discoverability enabled");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004392 p2p->dev_capab |= P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
4393 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004394 p2p_dbg(p2p, "Client discoverability disabled");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004395 p2p->dev_capab &= ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
4396 }
4397}
4398
4399
4400static struct wpabuf * p2p_build_presence_req(u32 duration1, u32 interval1,
4401 u32 duration2, u32 interval2)
4402{
4403 struct wpabuf *req;
4404 struct p2p_noa_desc desc1, desc2, *ptr1 = NULL, *ptr2 = NULL;
4405 u8 *len;
4406
4407 req = wpabuf_alloc(100);
4408 if (req == NULL)
4409 return NULL;
4410
4411 if (duration1 || interval1) {
4412 os_memset(&desc1, 0, sizeof(desc1));
4413 desc1.count_type = 1;
4414 desc1.duration = duration1;
4415 desc1.interval = interval1;
4416 ptr1 = &desc1;
4417
4418 if (duration2 || interval2) {
4419 os_memset(&desc2, 0, sizeof(desc2));
4420 desc2.count_type = 2;
4421 desc2.duration = duration2;
4422 desc2.interval = interval2;
4423 ptr2 = &desc2;
4424 }
4425 }
4426
4427 p2p_buf_add_action_hdr(req, P2P_PRESENCE_REQ, 1);
4428 len = p2p_buf_add_ie_hdr(req);
4429 p2p_buf_add_noa(req, 0, 0, 0, ptr1, ptr2);
4430 p2p_buf_update_ie_hdr(req, len);
4431
4432 return req;
4433}
4434
4435
4436int p2p_presence_req(struct p2p_data *p2p, const u8 *go_interface_addr,
4437 const u8 *own_interface_addr, unsigned int freq,
4438 u32 duration1, u32 interval1, u32 duration2,
4439 u32 interval2)
4440{
4441 struct wpabuf *req;
4442
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004443 p2p_dbg(p2p, "Send Presence Request to GO " MACSTR
4444 " (own interface " MACSTR ") freq=%u dur1=%u int1=%u "
4445 "dur2=%u int2=%u",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004446 MAC2STR(go_interface_addr), MAC2STR(own_interface_addr),
4447 freq, duration1, interval1, duration2, interval2);
4448
4449 req = p2p_build_presence_req(duration1, interval1, duration2,
4450 interval2);
4451 if (req == NULL)
4452 return -1;
4453
4454 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
4455 if (p2p_send_action(p2p, freq, go_interface_addr, own_interface_addr,
4456 go_interface_addr,
4457 wpabuf_head(req), wpabuf_len(req), 200) < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004458 p2p_dbg(p2p, "Failed to send Action frame");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004459 }
4460 wpabuf_free(req);
4461
4462 return 0;
4463}
4464
4465
4466static struct wpabuf * p2p_build_presence_resp(u8 status, const u8 *noa,
4467 size_t noa_len, u8 dialog_token)
4468{
4469 struct wpabuf *resp;
4470 u8 *len;
4471
4472 resp = wpabuf_alloc(100 + noa_len);
4473 if (resp == NULL)
4474 return NULL;
4475
4476 p2p_buf_add_action_hdr(resp, P2P_PRESENCE_RESP, dialog_token);
4477 len = p2p_buf_add_ie_hdr(resp);
4478 p2p_buf_add_status(resp, status);
4479 if (noa) {
4480 wpabuf_put_u8(resp, P2P_ATTR_NOTICE_OF_ABSENCE);
4481 wpabuf_put_le16(resp, noa_len);
4482 wpabuf_put_data(resp, noa, noa_len);
4483 } else
4484 p2p_buf_add_noa(resp, 0, 0, 0, NULL, NULL);
4485 p2p_buf_update_ie_hdr(resp, len);
4486
4487 return resp;
4488}
4489
4490
4491static void p2p_process_presence_req(struct p2p_data *p2p, const u8 *da,
4492 const u8 *sa, const u8 *data, size_t len,
4493 int rx_freq)
4494{
4495 struct p2p_message msg;
4496 u8 status;
4497 struct wpabuf *resp;
4498 size_t g;
4499 struct p2p_group *group = NULL;
4500 int parsed = 0;
4501 u8 noa[50];
4502 int noa_len;
4503
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004504 p2p_dbg(p2p, "Received P2P Action - P2P Presence Request");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004505
4506 for (g = 0; g < p2p->num_groups; g++) {
4507 if (os_memcmp(da, p2p_group_get_interface_addr(p2p->groups[g]),
4508 ETH_ALEN) == 0) {
4509 group = p2p->groups[g];
4510 break;
4511 }
4512 }
4513 if (group == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004514 p2p_dbg(p2p, "Ignore P2P Presence Request for unknown group "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004515 MACSTR, MAC2STR(da));
4516 return;
4517 }
4518
4519 if (p2p_parse(data, len, &msg) < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004520 p2p_dbg(p2p, "Failed to parse P2P Presence Request");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004521 status = P2P_SC_FAIL_INVALID_PARAMS;
4522 goto fail;
4523 }
4524 parsed = 1;
4525
4526 if (msg.noa == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004527 p2p_dbg(p2p, "No NoA attribute in P2P Presence Request");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004528 status = P2P_SC_FAIL_INVALID_PARAMS;
4529 goto fail;
4530 }
4531
4532 status = p2p_group_presence_req(group, sa, msg.noa, msg.noa_len);
4533
4534fail:
4535 if (p2p->cfg->get_noa)
4536 noa_len = p2p->cfg->get_noa(p2p->cfg->cb_ctx, da, noa,
4537 sizeof(noa));
4538 else
4539 noa_len = -1;
4540 resp = p2p_build_presence_resp(status, noa_len > 0 ? noa : NULL,
4541 noa_len > 0 ? noa_len : 0,
4542 msg.dialog_token);
4543 if (parsed)
4544 p2p_parse_free(&msg);
4545 if (resp == NULL)
4546 return;
4547
4548 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
4549 if (p2p_send_action(p2p, rx_freq, sa, da, da,
4550 wpabuf_head(resp), wpabuf_len(resp), 200) < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004551 p2p_dbg(p2p, "Failed to send Action frame");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004552 }
4553 wpabuf_free(resp);
4554}
4555
4556
4557static void p2p_process_presence_resp(struct p2p_data *p2p, const u8 *da,
4558 const u8 *sa, const u8 *data, size_t len)
4559{
4560 struct p2p_message msg;
4561
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004562 p2p_dbg(p2p, "Received P2P Action - P2P Presence Response");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004563
4564 if (p2p_parse(data, len, &msg) < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004565 p2p_dbg(p2p, "Failed to parse P2P Presence Response");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004566 return;
4567 }
4568
4569 if (msg.status == NULL || msg.noa == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004570 p2p_dbg(p2p, "No Status or NoA attribute in P2P Presence Response");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004571 p2p_parse_free(&msg);
4572 return;
4573 }
4574
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004575 if (p2p->cfg->presence_resp) {
4576 p2p->cfg->presence_resp(p2p->cfg->cb_ctx, sa, *msg.status,
4577 msg.noa, msg.noa_len);
4578 }
4579
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004580 if (*msg.status) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004581 p2p_dbg(p2p, "P2P Presence Request was rejected: status %u",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004582 *msg.status);
4583 p2p_parse_free(&msg);
4584 return;
4585 }
4586
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004587 p2p_dbg(p2p, "P2P Presence Request was accepted");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004588 wpa_hexdump(MSG_DEBUG, "P2P: P2P Presence Response - NoA",
4589 msg.noa, msg.noa_len);
4590 /* TODO: process NoA */
4591 p2p_parse_free(&msg);
4592}
4593
4594
4595static void p2p_ext_listen_timeout(void *eloop_ctx, void *timeout_ctx)
4596{
4597 struct p2p_data *p2p = eloop_ctx;
4598
4599 if (p2p->ext_listen_interval) {
4600 /* Schedule next extended listen timeout */
4601 eloop_register_timeout(p2p->ext_listen_interval_sec,
4602 p2p->ext_listen_interval_usec,
4603 p2p_ext_listen_timeout, p2p, NULL);
4604 }
4605
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -07004606 if ((p2p->cfg->is_p2p_in_progress &&
4607 p2p->cfg->is_p2p_in_progress(p2p->cfg->cb_ctx)) ||
4608 (p2p->pending_action_state == P2P_PENDING_PD &&
4609 p2p->pd_retries > 0)) {
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -07004610 p2p_dbg(p2p, "Operation in progress - skip Extended Listen timeout (%s)",
4611 p2p_state_txt(p2p->state));
4612 return;
4613 }
4614
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004615 if (p2p->state == P2P_LISTEN_ONLY && p2p->ext_listen_only) {
4616 /*
4617 * This should not really happen, but it looks like the Listen
4618 * command may fail is something else (e.g., a scan) was
4619 * running at an inconvenient time. As a workaround, allow new
4620 * Extended Listen operation to be started.
4621 */
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004622 p2p_dbg(p2p, "Previous Extended Listen operation had not been completed - try again");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004623 p2p->ext_listen_only = 0;
4624 p2p_set_state(p2p, P2P_IDLE);
4625 }
4626
4627 if (p2p->state != P2P_IDLE) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004628 p2p_dbg(p2p, "Skip Extended Listen timeout in active state (%s)", p2p_state_txt(p2p->state));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004629 return;
4630 }
4631
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004632 p2p_dbg(p2p, "Extended Listen timeout");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004633 p2p->ext_listen_only = 1;
4634 if (p2p_listen(p2p, p2p->ext_listen_period) < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004635 p2p_dbg(p2p, "Failed to start Listen state for Extended Listen Timing");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004636 p2p->ext_listen_only = 0;
4637 }
4638}
4639
4640
4641int p2p_ext_listen(struct p2p_data *p2p, unsigned int period,
4642 unsigned int interval)
4643{
4644 if (period > 65535 || interval > 65535 || period > interval ||
4645 (period == 0 && interval > 0) || (period > 0 && interval == 0)) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004646 p2p_dbg(p2p, "Invalid Extended Listen Timing request: period=%u interval=%u",
4647 period, interval);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004648 return -1;
4649 }
4650
4651 eloop_cancel_timeout(p2p_ext_listen_timeout, p2p, NULL);
4652
4653 if (interval == 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004654 p2p_dbg(p2p, "Disabling Extended Listen Timing");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004655 p2p->ext_listen_period = 0;
4656 p2p->ext_listen_interval = 0;
4657 return 0;
4658 }
4659
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004660 p2p_dbg(p2p, "Enabling Extended Listen Timing: period %u msec, interval %u msec",
4661 period, interval);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004662 p2p->ext_listen_period = period;
4663 p2p->ext_listen_interval = interval;
4664 p2p->ext_listen_interval_sec = interval / 1000;
4665 p2p->ext_listen_interval_usec = (interval % 1000) * 1000;
4666
4667 eloop_register_timeout(p2p->ext_listen_interval_sec,
4668 p2p->ext_listen_interval_usec,
4669 p2p_ext_listen_timeout, p2p, NULL);
4670
4671 return 0;
4672}
4673
4674
4675void p2p_deauth_notif(struct p2p_data *p2p, const u8 *bssid, u16 reason_code,
4676 const u8 *ie, size_t ie_len)
4677{
4678 struct p2p_message msg;
4679
4680 if (bssid == NULL || ie == NULL)
4681 return;
4682
4683 os_memset(&msg, 0, sizeof(msg));
4684 if (p2p_parse_ies(ie, ie_len, &msg))
4685 return;
Dmitry Shmidt97672262014-02-03 13:02:54 -08004686 if (msg.minor_reason_code == NULL) {
4687 p2p_parse_free(&msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004688 return;
Dmitry Shmidt97672262014-02-03 13:02:54 -08004689 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004690
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004691 p2p_dbg(p2p, "Deauthentication notification BSSID " MACSTR
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004692 " reason_code=%u minor_reason_code=%u",
4693 MAC2STR(bssid), reason_code, *msg.minor_reason_code);
4694
4695 p2p_parse_free(&msg);
4696}
4697
4698
4699void p2p_disassoc_notif(struct p2p_data *p2p, const u8 *bssid, u16 reason_code,
4700 const u8 *ie, size_t ie_len)
4701{
4702 struct p2p_message msg;
4703
4704 if (bssid == NULL || ie == NULL)
4705 return;
4706
4707 os_memset(&msg, 0, sizeof(msg));
4708 if (p2p_parse_ies(ie, ie_len, &msg))
4709 return;
Dmitry Shmidt97672262014-02-03 13:02:54 -08004710 if (msg.minor_reason_code == NULL) {
4711 p2p_parse_free(&msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004712 return;
Dmitry Shmidt97672262014-02-03 13:02:54 -08004713 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004714
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004715 p2p_dbg(p2p, "Disassociation notification BSSID " MACSTR
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004716 " reason_code=%u minor_reason_code=%u",
4717 MAC2STR(bssid), reason_code, *msg.minor_reason_code);
4718
4719 p2p_parse_free(&msg);
4720}
4721
4722
4723void p2p_set_managed_oper(struct p2p_data *p2p, int enabled)
4724{
4725 if (enabled) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004726 p2p_dbg(p2p, "Managed P2P Device operations enabled");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004727 p2p->dev_capab |= P2P_DEV_CAPAB_INFRA_MANAGED;
4728 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004729 p2p_dbg(p2p, "Managed P2P Device operations disabled");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004730 p2p->dev_capab &= ~P2P_DEV_CAPAB_INFRA_MANAGED;
4731 }
4732}
4733
4734
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004735int p2p_config_get_random_social(struct p2p_config *p2p, u8 *op_class,
4736 u8 *op_channel)
4737{
4738 return p2p_channel_random_social(&p2p->channels, op_class, op_channel);
4739}
4740
4741
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004742int p2p_set_listen_channel(struct p2p_data *p2p, u8 reg_class, u8 channel,
4743 u8 forced)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004744{
Dmitry Shmidt4b060592013-04-29 16:42:49 -07004745 if (p2p_channel_to_freq(reg_class, channel) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004746 return -1;
4747
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004748 /*
4749 * Listen channel was set in configuration or set by control interface;
4750 * cannot override it.
4751 */
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004752 if (p2p->cfg->channel_forced && forced == 0) {
4753 p2p_dbg(p2p,
4754 "Listen channel was previously configured - do not override based on optimization");
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004755 return -1;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004756 }
4757
4758 p2p_dbg(p2p, "Set Listen channel: reg_class %u channel %u",
4759 reg_class, channel);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004760
4761 if (p2p->state == P2P_IDLE) {
4762 p2p->cfg->reg_class = reg_class;
4763 p2p->cfg->channel = channel;
4764 p2p->cfg->channel_forced = forced;
4765 } else {
4766 p2p_dbg(p2p, "Defer setting listen channel");
4767 p2p->pending_reg_class = reg_class;
4768 p2p->pending_channel = channel;
4769 p2p->pending_channel_forced = forced;
4770 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004771
4772 return 0;
4773}
4774
4775
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004776u8 p2p_get_listen_channel(struct p2p_data *p2p)
4777{
4778 return p2p->cfg->channel;
4779}
4780
4781
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004782int p2p_set_ssid_postfix(struct p2p_data *p2p, const u8 *postfix, size_t len)
4783{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004784 p2p_dbg(p2p, "New SSID postfix: %s", wpa_ssid_txt(postfix, len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004785 if (postfix == NULL) {
4786 p2p->cfg->ssid_postfix_len = 0;
4787 return 0;
4788 }
4789 if (len > sizeof(p2p->cfg->ssid_postfix))
4790 return -1;
4791 os_memcpy(p2p->cfg->ssid_postfix, postfix, len);
4792 p2p->cfg->ssid_postfix_len = len;
4793 return 0;
4794}
4795
4796
Jouni Malinen75ecf522011-06-27 15:19:46 -07004797int p2p_set_oper_channel(struct p2p_data *p2p, u8 op_reg_class, u8 op_channel,
4798 int cfg_op_channel)
4799{
Dmitry Shmidt4b060592013-04-29 16:42:49 -07004800 if (p2p_channel_to_freq(op_reg_class, op_channel) < 0)
Jouni Malinen75ecf522011-06-27 15:19:46 -07004801 return -1;
4802
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004803 p2p_dbg(p2p, "Set Operating channel: reg_class %u channel %u",
4804 op_reg_class, op_channel);
Jouni Malinen75ecf522011-06-27 15:19:46 -07004805 p2p->cfg->op_reg_class = op_reg_class;
4806 p2p->cfg->op_channel = op_channel;
4807 p2p->cfg->cfg_op_channel = cfg_op_channel;
4808 return 0;
4809}
4810
4811
Dmitry Shmidt04949592012-07-19 12:16:46 -07004812int p2p_set_pref_chan(struct p2p_data *p2p, unsigned int num_pref_chan,
4813 const struct p2p_channel *pref_chan)
4814{
4815 struct p2p_channel *n;
4816
4817 if (pref_chan) {
4818 n = os_malloc(num_pref_chan * sizeof(struct p2p_channel));
4819 if (n == NULL)
4820 return -1;
4821 os_memcpy(n, pref_chan,
4822 num_pref_chan * sizeof(struct p2p_channel));
4823 } else
4824 n = NULL;
4825
4826 os_free(p2p->cfg->pref_chan);
4827 p2p->cfg->pref_chan = n;
4828 p2p->cfg->num_pref_chan = num_pref_chan;
4829
4830 return 0;
4831}
4832
4833
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004834int p2p_set_no_go_freq(struct p2p_data *p2p,
4835 const struct wpa_freq_range_list *list)
4836{
4837 struct wpa_freq_range *tmp;
4838
4839 if (list == NULL || list->num == 0) {
4840 os_free(p2p->no_go_freq.range);
4841 p2p->no_go_freq.range = NULL;
4842 p2p->no_go_freq.num = 0;
4843 return 0;
4844 }
4845
4846 tmp = os_calloc(list->num, sizeof(struct wpa_freq_range));
4847 if (tmp == NULL)
4848 return -1;
4849 os_memcpy(tmp, list->range, list->num * sizeof(struct wpa_freq_range));
4850 os_free(p2p->no_go_freq.range);
4851 p2p->no_go_freq.range = tmp;
4852 p2p->no_go_freq.num = list->num;
4853 p2p_dbg(p2p, "Updated no GO chan list");
4854
4855 return 0;
4856}
4857
4858
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004859int p2p_get_interface_addr(struct p2p_data *p2p, const u8 *dev_addr,
4860 u8 *iface_addr)
4861{
4862 struct p2p_device *dev = p2p_get_device(p2p, dev_addr);
4863 if (dev == NULL || is_zero_ether_addr(dev->interface_addr))
4864 return -1;
4865 os_memcpy(iface_addr, dev->interface_addr, ETH_ALEN);
4866 return 0;
4867}
4868
4869
4870int p2p_get_dev_addr(struct p2p_data *p2p, const u8 *iface_addr,
4871 u8 *dev_addr)
4872{
4873 struct p2p_device *dev = p2p_get_device_interface(p2p, iface_addr);
4874 if (dev == NULL)
4875 return -1;
4876 os_memcpy(dev_addr, dev->info.p2p_device_addr, ETH_ALEN);
4877 return 0;
4878}
4879
4880
4881void p2p_set_peer_filter(struct p2p_data *p2p, const u8 *addr)
4882{
4883 os_memcpy(p2p->peer_filter, addr, ETH_ALEN);
4884 if (is_zero_ether_addr(p2p->peer_filter))
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004885 p2p_dbg(p2p, "Disable peer filter");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004886 else
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004887 p2p_dbg(p2p, "Enable peer filter for " MACSTR,
4888 MAC2STR(p2p->peer_filter));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004889}
4890
4891
4892void p2p_set_cross_connect(struct p2p_data *p2p, int enabled)
4893{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004894 p2p_dbg(p2p, "Cross connection %s", enabled ? "enabled" : "disabled");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004895 if (p2p->cross_connect == enabled)
4896 return;
4897 p2p->cross_connect = enabled;
4898 /* TODO: may need to tear down any action group where we are GO(?) */
4899}
4900
4901
4902int p2p_get_oper_freq(struct p2p_data *p2p, const u8 *iface_addr)
4903{
4904 struct p2p_device *dev = p2p_get_device_interface(p2p, iface_addr);
4905 if (dev == NULL)
4906 return -1;
4907 if (dev->oper_freq <= 0)
4908 return -1;
4909 return dev->oper_freq;
4910}
4911
4912
4913void p2p_set_intra_bss_dist(struct p2p_data *p2p, int enabled)
4914{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004915 p2p_dbg(p2p, "Intra BSS distribution %s",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004916 enabled ? "enabled" : "disabled");
4917 p2p->cfg->p2p_intra_bss = enabled;
4918}
4919
4920
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004921void p2p_update_channel_list(struct p2p_data *p2p,
4922 const struct p2p_channels *chan,
4923 const struct p2p_channels *cli_chan)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004924{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004925 p2p_dbg(p2p, "Update channel list");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004926 os_memcpy(&p2p->cfg->channels, chan, sizeof(struct p2p_channels));
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004927 p2p_channels_dump(p2p, "channels", &p2p->cfg->channels);
4928 os_memcpy(&p2p->cfg->cli_channels, cli_chan,
4929 sizeof(struct p2p_channels));
4930 p2p_channels_dump(p2p, "cli_channels", &p2p->cfg->cli_channels);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004931}
4932
4933
4934int p2p_send_action(struct p2p_data *p2p, unsigned int freq, const u8 *dst,
4935 const u8 *src, const u8 *bssid, const u8 *buf,
4936 size_t len, unsigned int wait_time)
4937{
4938 if (p2p->p2p_scan_running) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004939 p2p_dbg(p2p, "Delay Action frame TX until p2p_scan completes");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004940 if (p2p->after_scan_tx) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004941 p2p_dbg(p2p, "Dropped previous pending Action frame TX");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004942 os_free(p2p->after_scan_tx);
4943 }
4944 p2p->after_scan_tx = os_malloc(sizeof(*p2p->after_scan_tx) +
4945 len);
4946 if (p2p->after_scan_tx == NULL)
4947 return -1;
4948 p2p->after_scan_tx->freq = freq;
4949 os_memcpy(p2p->after_scan_tx->dst, dst, ETH_ALEN);
4950 os_memcpy(p2p->after_scan_tx->src, src, ETH_ALEN);
4951 os_memcpy(p2p->after_scan_tx->bssid, bssid, ETH_ALEN);
4952 p2p->after_scan_tx->len = len;
4953 p2p->after_scan_tx->wait_time = wait_time;
4954 os_memcpy(p2p->after_scan_tx + 1, buf, len);
4955 return 0;
4956 }
4957
4958 return p2p->cfg->send_action(p2p->cfg->cb_ctx, freq, dst, src, bssid,
4959 buf, len, wait_time);
4960}
4961
4962
4963void p2p_set_best_channels(struct p2p_data *p2p, int freq_24, int freq_5,
4964 int freq_overall)
4965{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004966 p2p_dbg(p2p, "Best channel: 2.4 GHz: %d, 5 GHz: %d, overall: %d",
4967 freq_24, freq_5, freq_overall);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004968 p2p->best_freq_24 = freq_24;
4969 p2p->best_freq_5 = freq_5;
4970 p2p->best_freq_overall = freq_overall;
4971}
4972
4973
Dmitry Shmidt700a1372013-03-15 14:14:44 -07004974void p2p_set_own_freq_preference(struct p2p_data *p2p, int freq)
4975{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004976 p2p_dbg(p2p, "Own frequency preference: %d MHz", freq);
Dmitry Shmidt700a1372013-03-15 14:14:44 -07004977 p2p->own_freq_preference = freq;
4978}
4979
4980
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004981const u8 * p2p_get_go_neg_peer(struct p2p_data *p2p)
4982{
4983 if (p2p == NULL || p2p->go_neg_peer == NULL)
4984 return NULL;
4985 return p2p->go_neg_peer->info.p2p_device_addr;
4986}
4987
4988
4989const struct p2p_peer_info *
4990p2p_get_peer_found(struct p2p_data *p2p, const u8 *addr, int next)
4991{
4992 struct p2p_device *dev;
4993
4994 if (addr) {
4995 dev = p2p_get_device(p2p, addr);
4996 if (!dev)
4997 return NULL;
4998
4999 if (!next) {
5000 if (dev->flags & P2P_DEV_PROBE_REQ_ONLY)
5001 return NULL;
5002
5003 return &dev->info;
5004 } else {
5005 do {
5006 dev = dl_list_first(&dev->list,
5007 struct p2p_device,
5008 list);
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07005009 if (!dev || &dev->list == &p2p->devices)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005010 return NULL;
5011 } while (dev->flags & P2P_DEV_PROBE_REQ_ONLY);
5012 }
5013 } else {
5014 dev = dl_list_first(&p2p->devices, struct p2p_device, list);
5015 if (!dev)
5016 return NULL;
5017 while (dev->flags & P2P_DEV_PROBE_REQ_ONLY) {
5018 dev = dl_list_first(&dev->list,
5019 struct p2p_device,
5020 list);
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07005021 if (!dev || &dev->list == &p2p->devices)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005022 return NULL;
5023 }
5024 }
5025
5026 return &dev->info;
5027}
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005028
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005029
5030int p2p_in_progress(struct p2p_data *p2p)
5031{
5032 if (p2p == NULL)
5033 return 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005034 if (p2p->state == P2P_SEARCH)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005035 return 2;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005036 return p2p->state != P2P_IDLE && p2p->state != P2P_PROVISIONING;
5037}
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005038
5039
5040void p2p_set_config_timeout(struct p2p_data *p2p, u8 go_timeout,
5041 u8 client_timeout)
5042{
5043 if (p2p) {
5044 p2p->go_timeout = go_timeout;
5045 p2p->client_timeout = client_timeout;
5046 }
5047}
5048
5049
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005050#ifdef CONFIG_WIFI_DISPLAY
5051
5052static void p2p_update_wfd_ie_groups(struct p2p_data *p2p)
5053{
5054 size_t g;
5055 struct p2p_group *group;
5056
5057 for (g = 0; g < p2p->num_groups; g++) {
5058 group = p2p->groups[g];
Dmitry Shmidtb96dad42013-11-05 10:07:29 -08005059 p2p_group_force_beacon_update_ies(group);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005060 }
5061}
5062
5063
5064int p2p_set_wfd_ie_beacon(struct p2p_data *p2p, struct wpabuf *ie)
5065{
5066 wpabuf_free(p2p->wfd_ie_beacon);
5067 p2p->wfd_ie_beacon = ie;
5068 p2p_update_wfd_ie_groups(p2p);
5069 return 0;
5070}
5071
5072
5073int p2p_set_wfd_ie_probe_req(struct p2p_data *p2p, struct wpabuf *ie)
5074{
5075 wpabuf_free(p2p->wfd_ie_probe_req);
5076 p2p->wfd_ie_probe_req = ie;
5077 return 0;
5078}
5079
5080
5081int p2p_set_wfd_ie_probe_resp(struct p2p_data *p2p, struct wpabuf *ie)
5082{
5083 wpabuf_free(p2p->wfd_ie_probe_resp);
5084 p2p->wfd_ie_probe_resp = ie;
5085 p2p_update_wfd_ie_groups(p2p);
5086 return 0;
5087}
5088
5089
5090int p2p_set_wfd_ie_assoc_req(struct p2p_data *p2p, struct wpabuf *ie)
5091{
5092 wpabuf_free(p2p->wfd_ie_assoc_req);
5093 p2p->wfd_ie_assoc_req = ie;
5094 return 0;
5095}
5096
5097
5098int p2p_set_wfd_ie_invitation(struct p2p_data *p2p, struct wpabuf *ie)
5099{
5100 wpabuf_free(p2p->wfd_ie_invitation);
5101 p2p->wfd_ie_invitation = ie;
5102 return 0;
5103}
5104
5105
5106int p2p_set_wfd_ie_prov_disc_req(struct p2p_data *p2p, struct wpabuf *ie)
5107{
5108 wpabuf_free(p2p->wfd_ie_prov_disc_req);
5109 p2p->wfd_ie_prov_disc_req = ie;
5110 return 0;
5111}
5112
5113
5114int p2p_set_wfd_ie_prov_disc_resp(struct p2p_data *p2p, struct wpabuf *ie)
5115{
5116 wpabuf_free(p2p->wfd_ie_prov_disc_resp);
5117 p2p->wfd_ie_prov_disc_resp = ie;
5118 return 0;
5119}
5120
5121
5122int p2p_set_wfd_ie_go_neg(struct p2p_data *p2p, struct wpabuf *ie)
5123{
5124 wpabuf_free(p2p->wfd_ie_go_neg);
5125 p2p->wfd_ie_go_neg = ie;
5126 return 0;
5127}
5128
5129
5130int p2p_set_wfd_dev_info(struct p2p_data *p2p, const struct wpabuf *elem)
5131{
5132 wpabuf_free(p2p->wfd_dev_info);
5133 if (elem) {
5134 p2p->wfd_dev_info = wpabuf_dup(elem);
5135 if (p2p->wfd_dev_info == NULL)
5136 return -1;
5137 } else
5138 p2p->wfd_dev_info = NULL;
5139
5140 return 0;
5141}
5142
5143
5144int p2p_set_wfd_assoc_bssid(struct p2p_data *p2p, const struct wpabuf *elem)
5145{
5146 wpabuf_free(p2p->wfd_assoc_bssid);
5147 if (elem) {
5148 p2p->wfd_assoc_bssid = wpabuf_dup(elem);
5149 if (p2p->wfd_assoc_bssid == NULL)
5150 return -1;
5151 } else
5152 p2p->wfd_assoc_bssid = NULL;
5153
5154 return 0;
5155}
5156
5157
5158int p2p_set_wfd_coupled_sink_info(struct p2p_data *p2p,
5159 const struct wpabuf *elem)
5160{
5161 wpabuf_free(p2p->wfd_coupled_sink_info);
5162 if (elem) {
5163 p2p->wfd_coupled_sink_info = wpabuf_dup(elem);
5164 if (p2p->wfd_coupled_sink_info == NULL)
5165 return -1;
5166 } else
5167 p2p->wfd_coupled_sink_info = NULL;
5168
5169 return 0;
5170}
5171
5172#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005173
5174
5175int p2p_set_disc_int(struct p2p_data *p2p, int min_disc_int, int max_disc_int,
5176 int max_disc_tu)
5177{
5178 if (min_disc_int > max_disc_int || min_disc_int < 0 || max_disc_int < 0)
5179 return -1;
5180
5181 p2p->min_disc_int = min_disc_int;
5182 p2p->max_disc_int = max_disc_int;
5183 p2p->max_disc_tu = max_disc_tu;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005184 p2p_dbg(p2p, "Set discoverable interval: min=%d max=%d max_tu=%d",
5185 min_disc_int, max_disc_int, max_disc_tu);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005186
5187 return 0;
5188}
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005189
5190
5191void p2p_dbg(struct p2p_data *p2p, const char *fmt, ...)
5192{
5193 va_list ap;
5194 char buf[500];
5195
5196 if (!p2p->cfg->debug_print)
5197 return;
5198
5199 va_start(ap, fmt);
5200 vsnprintf(buf, sizeof(buf), fmt, ap);
5201 buf[sizeof(buf) - 1] = '\0';
5202 va_end(ap);
5203 p2p->cfg->debug_print(p2p->cfg->cb_ctx, MSG_DEBUG, buf);
5204}
5205
5206
5207void p2p_info(struct p2p_data *p2p, const char *fmt, ...)
5208{
5209 va_list ap;
5210 char buf[500];
5211
5212 if (!p2p->cfg->debug_print)
5213 return;
5214
5215 va_start(ap, fmt);
5216 vsnprintf(buf, sizeof(buf), fmt, ap);
5217 buf[sizeof(buf) - 1] = '\0';
5218 va_end(ap);
5219 p2p->cfg->debug_print(p2p->cfg->cb_ctx, MSG_INFO, buf);
5220}
5221
5222
5223void p2p_err(struct p2p_data *p2p, const char *fmt, ...)
5224{
5225 va_list ap;
5226 char buf[500];
5227
5228 if (!p2p->cfg->debug_print)
5229 return;
5230
5231 va_start(ap, fmt);
5232 vsnprintf(buf, sizeof(buf), fmt, ap);
5233 buf[sizeof(buf) - 1] = '\0';
5234 va_end(ap);
5235 p2p->cfg->debug_print(p2p->cfg->cb_ctx, MSG_ERROR, buf);
5236}
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005237
5238
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07005239void p2p_loop_on_known_peers(struct p2p_data *p2p,
5240 void (*peer_callback)(struct p2p_peer_info *peer,
5241 void *user_data),
5242 void *user_data)
5243{
5244 struct p2p_device *dev, *n;
5245
5246 dl_list_for_each_safe(dev, n, &p2p->devices, struct p2p_device, list) {
5247 peer_callback(&dev->info, user_data);
5248 }
5249}
5250
5251
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005252#ifdef CONFIG_WPS_NFC
5253
5254static struct wpabuf * p2p_build_nfc_handover(struct p2p_data *p2p,
5255 int client_freq,
5256 const u8 *go_dev_addr,
5257 const u8 *ssid, size_t ssid_len)
5258{
5259 struct wpabuf *buf;
5260 u8 op_class, channel;
5261 enum p2p_role_indication role = P2P_DEVICE_NOT_IN_GROUP;
5262
5263 buf = wpabuf_alloc(1000);
5264 if (buf == NULL)
5265 return NULL;
5266
5267 op_class = p2p->cfg->reg_class;
5268 channel = p2p->cfg->channel;
5269
5270 p2p_buf_add_capability(buf, p2p->dev_capab &
5271 ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY, 0);
5272 p2p_buf_add_device_info(buf, p2p, NULL);
5273
5274 if (p2p->num_groups > 0) {
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07005275 int freq = p2p_group_get_freq(p2p->groups[0]);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005276 role = P2P_GO_IN_A_GROUP;
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07005277 if (p2p_freq_to_channel(freq, &op_class, &channel) < 0) {
5278 p2p_dbg(p2p,
5279 "Unknown GO operating frequency %d MHz for NFC handover",
5280 freq);
5281 wpabuf_free(buf);
5282 return NULL;
5283 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005284 } else if (client_freq > 0) {
5285 role = P2P_CLIENT_IN_A_GROUP;
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07005286 if (p2p_freq_to_channel(client_freq, &op_class, &channel) < 0) {
5287 p2p_dbg(p2p,
5288 "Unknown client operating frequency %d MHz for NFC handover",
5289 client_freq);
5290 wpabuf_free(buf);
5291 return NULL;
5292 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005293 }
5294
5295 p2p_buf_add_oob_go_neg_channel(buf, p2p->cfg->country, op_class,
5296 channel, role);
5297
5298 if (p2p->num_groups > 0) {
5299 /* Limit number of clients to avoid very long message */
5300 p2p_buf_add_group_info(p2p->groups[0], buf, 5);
5301 p2p_group_buf_add_id(p2p->groups[0], buf);
5302 } else if (client_freq > 0 &&
5303 go_dev_addr && !is_zero_ether_addr(go_dev_addr) &&
5304 ssid && ssid_len > 0) {
5305 /*
5306 * Add the optional P2P Group ID to indicate in which group this
5307 * device is a P2P Client.
5308 */
5309 p2p_buf_add_group_id(buf, go_dev_addr, ssid, ssid_len);
5310 }
5311
5312 return buf;
5313}
5314
5315
5316struct wpabuf * p2p_build_nfc_handover_req(struct p2p_data *p2p,
5317 int client_freq,
5318 const u8 *go_dev_addr,
5319 const u8 *ssid, size_t ssid_len)
5320{
5321 return p2p_build_nfc_handover(p2p, client_freq, go_dev_addr, ssid,
5322 ssid_len);
5323}
5324
5325
5326struct wpabuf * p2p_build_nfc_handover_sel(struct p2p_data *p2p,
5327 int client_freq,
5328 const u8 *go_dev_addr,
5329 const u8 *ssid, size_t ssid_len)
5330{
5331 return p2p_build_nfc_handover(p2p, client_freq, go_dev_addr, ssid,
5332 ssid_len);
5333}
5334
5335
5336int p2p_process_nfc_connection_handover(struct p2p_data *p2p,
5337 struct p2p_nfc_params *params)
5338{
5339 struct p2p_message msg;
5340 struct p2p_device *dev;
5341 const u8 *p2p_dev_addr;
5342 int freq;
5343 enum p2p_role_indication role;
5344
5345 params->next_step = NO_ACTION;
5346
5347 if (p2p_parse_ies_separate(params->wsc_attr, params->wsc_len,
5348 params->p2p_attr, params->p2p_len, &msg)) {
5349 p2p_dbg(p2p, "Failed to parse WSC/P2P attributes from NFC");
5350 p2p_parse_free(&msg);
5351 return -1;
5352 }
5353
5354 if (msg.p2p_device_addr)
5355 p2p_dev_addr = msg.p2p_device_addr;
5356 else if (msg.device_id)
5357 p2p_dev_addr = msg.device_id;
5358 else {
5359 p2p_dbg(p2p, "Ignore scan data without P2P Device Info or P2P Device Id");
5360 p2p_parse_free(&msg);
5361 return -1;
5362 }
5363
5364 if (msg.oob_dev_password) {
5365 os_memcpy(params->oob_dev_pw, msg.oob_dev_password,
5366 msg.oob_dev_password_len);
5367 params->oob_dev_pw_len = msg.oob_dev_password_len;
5368 }
5369
5370 dev = p2p_create_device(p2p, p2p_dev_addr);
5371 if (dev == NULL) {
5372 p2p_parse_free(&msg);
5373 return -1;
5374 }
5375
5376 params->peer = &dev->info;
5377
5378 os_get_reltime(&dev->last_seen);
5379 dev->flags &= ~(P2P_DEV_PROBE_REQ_ONLY | P2P_DEV_GROUP_CLIENT_ONLY);
5380 p2p_copy_wps_info(p2p, dev, 0, &msg);
5381
5382 if (!msg.oob_go_neg_channel) {
5383 p2p_dbg(p2p, "OOB GO Negotiation Channel attribute not included");
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07005384 p2p_parse_free(&msg);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005385 return -1;
5386 }
5387
5388 if (msg.oob_go_neg_channel[3] == 0 &&
5389 msg.oob_go_neg_channel[4] == 0)
5390 freq = 0;
5391 else
5392 freq = p2p_channel_to_freq(msg.oob_go_neg_channel[3],
5393 msg.oob_go_neg_channel[4]);
5394 if (freq < 0) {
5395 p2p_dbg(p2p, "Unknown peer OOB GO Neg channel");
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07005396 p2p_parse_free(&msg);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005397 return -1;
5398 }
5399 role = msg.oob_go_neg_channel[5];
5400
5401 if (role == P2P_GO_IN_A_GROUP) {
5402 p2p_dbg(p2p, "Peer OOB GO operating channel: %u MHz", freq);
5403 params->go_freq = freq;
5404 } else if (role == P2P_CLIENT_IN_A_GROUP) {
5405 p2p_dbg(p2p, "Peer (client) OOB GO operating channel: %u MHz",
5406 freq);
5407 params->go_freq = freq;
5408 } else
5409 p2p_dbg(p2p, "Peer OOB GO Neg channel: %u MHz", freq);
5410 dev->oob_go_neg_freq = freq;
5411
5412 if (!params->sel && role != P2P_GO_IN_A_GROUP) {
5413 freq = p2p_channel_to_freq(p2p->cfg->reg_class,
5414 p2p->cfg->channel);
5415 if (freq < 0) {
5416 p2p_dbg(p2p, "Own listen channel not known");
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07005417 p2p_parse_free(&msg);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005418 return -1;
5419 }
5420 p2p_dbg(p2p, "Use own Listen channel as OOB GO Neg channel: %u MHz", freq);
5421 dev->oob_go_neg_freq = freq;
5422 }
5423
5424 if (msg.group_id) {
5425 os_memcpy(params->go_dev_addr, msg.group_id, ETH_ALEN);
5426 params->go_ssid_len = msg.group_id_len - ETH_ALEN;
5427 os_memcpy(params->go_ssid, msg.group_id + ETH_ALEN,
5428 params->go_ssid_len);
5429 }
5430
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005431 if (dev->flags & P2P_DEV_USER_REJECTED) {
5432 p2p_dbg(p2p, "Do not report rejected device");
Dmitry Shmidt71757432014-06-02 13:50:35 -07005433 p2p_parse_free(&msg);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005434 return 0;
5435 }
5436
5437 if (!(dev->flags & P2P_DEV_REPORTED)) {
5438 p2p->cfg->dev_found(p2p->cfg->cb_ctx, p2p_dev_addr, &dev->info,
5439 !(dev->flags & P2P_DEV_REPORTED_ONCE));
5440 dev->flags |= P2P_DEV_REPORTED | P2P_DEV_REPORTED_ONCE;
5441 }
Dmitry Shmidt71757432014-06-02 13:50:35 -07005442 p2p_parse_free(&msg);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005443
5444 if (role == P2P_GO_IN_A_GROUP && p2p->num_groups > 0)
5445 params->next_step = BOTH_GO;
5446 else if (role == P2P_GO_IN_A_GROUP)
5447 params->next_step = JOIN_GROUP;
5448 else if (role == P2P_CLIENT_IN_A_GROUP) {
5449 dev->flags |= P2P_DEV_GROUP_CLIENT_ONLY;
5450 params->next_step = PEER_CLIENT;
5451 } else if (p2p->num_groups > 0)
5452 params->next_step = AUTH_JOIN;
5453 else if (params->sel)
5454 params->next_step = INIT_GO_NEG;
5455 else
5456 params->next_step = RESP_GO_NEG;
5457
5458 return 0;
5459}
5460
5461
5462void p2p_set_authorized_oob_dev_pw_id(struct p2p_data *p2p, u16 dev_pw_id,
5463 int go_intent,
5464 const u8 *own_interface_addr)
5465{
5466
5467 p2p->authorized_oob_dev_pw_id = dev_pw_id;
5468 if (dev_pw_id == 0) {
5469 p2p_dbg(p2p, "NFC OOB Password unauthorized for static handover");
5470 return;
5471 }
5472
5473 p2p_dbg(p2p, "NFC OOB Password (id=%u) authorized for static handover",
5474 dev_pw_id);
5475
5476 p2p->go_intent = go_intent;
5477 os_memcpy(p2p->intended_addr, own_interface_addr, ETH_ALEN);
5478}
5479
5480#endif /* CONFIG_WPS_NFC */
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07005481
5482
5483int p2p_set_passphrase_len(struct p2p_data *p2p, unsigned int len)
5484{
5485 if (len < 8 || len > 63)
5486 return -1;
5487 p2p->cfg->passphrase_len = len;
5488 return 0;
5489}
Dmitry Shmidt2e67f062014-07-16 09:55:28 -07005490
5491
5492void p2p_set_vendor_elems(struct p2p_data *p2p, struct wpabuf **vendor_elem)
5493{
5494 p2p->vendor_elem = vendor_elem;
5495}
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005496
5497
5498void p2p_go_neg_wait_timeout(void *eloop_ctx, void *timeout_ctx)
5499{
5500 struct p2p_data *p2p = eloop_ctx;
5501
5502 p2p_dbg(p2p,
5503 "Timeout on waiting peer to become ready for GO Negotiation");
5504 p2p_go_neg_failed(p2p, -1);
5505}
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005506
5507
5508void p2p_set_own_pref_freq_list(struct p2p_data *p2p,
5509 const unsigned int *pref_freq_list,
5510 unsigned int size)
5511{
5512 unsigned int i;
5513
5514 if (size > P2P_MAX_PREF_CHANNELS)
5515 size = P2P_MAX_PREF_CHANNELS;
5516 p2p->num_pref_freq = size;
5517 for (i = 0; i < size; i++) {
5518 p2p->pref_freq_list[i] = pref_freq_list[i];
5519 p2p_dbg(p2p, "Own preferred frequency list[%u]=%u MHz",
5520 i, p2p->pref_freq_list[i]);
5521 }
5522}
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07005523
5524
5525struct wpabuf * p2p_build_probe_resp_template(struct p2p_data *p2p,
5526 unsigned int freq)
5527{
5528 struct wpabuf *ies, *buf;
5529 u8 addr[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
5530 int ret;
5531
5532 ies = p2p_build_probe_resp_ies(p2p, NULL, 0);
5533 if (!ies) {
5534 wpa_printf(MSG_ERROR,
5535 "CTRL: Failed to build Probe Response IEs");
5536 return NULL;
5537 }
5538
5539 buf = wpabuf_alloc(200 + wpabuf_len(ies));
5540 if (!buf) {
5541 wpabuf_free(ies);
5542 return NULL;
5543 }
5544
5545 ret = p2p_build_probe_resp_buf(p2p, buf, ies, addr, freq);
5546 wpabuf_free(ies);
5547 if (ret) {
5548 wpabuf_free(buf);
5549 return NULL;
5550 }
5551
5552 return buf;
5553}