]> git.ipfire.org Git - thirdparty/hostap.git/blame - src/p2p/p2p.c
P2P: Prefer 5 GHz channels for initial pick
[thirdparty/hostap.git] / src / p2p / p2p.c
CommitLineData
b22128ef
JM
1/*
2 * Wi-Fi Direct - P2P module
3 * Copyright (c) 2009-2010, Atheros Communications
4 *
e22d4d95
JM
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
b22128ef
JM
7 */
8
9#include "includes.h"
10
11#include "common.h"
12#include "eloop.h"
13#include "common/ieee802_11_defs.h"
14#include "common/ieee802_11_common.h"
15#include "wps/wps_i.h"
16#include "p2p_i.h"
17#include "p2p.h"
18
19
20static void p2p_state_timeout(void *eloop_ctx, void *timeout_ctx);
21static void p2p_device_free(struct p2p_data *p2p, struct p2p_device *dev);
22static void p2p_process_presence_req(struct p2p_data *p2p, const u8 *da,
23 const u8 *sa, const u8 *data, size_t len,
24 int rx_freq);
25static void p2p_process_presence_resp(struct p2p_data *p2p, const u8 *da,
26 const u8 *sa, const u8 *data,
27 size_t len);
28static void p2p_ext_listen_timeout(void *eloop_ctx, void *timeout_ctx);
40c03fd4 29static void p2p_scan_timeout(void *eloop_ctx, void *timeout_ctx);
b22128ef
JM
30
31
40c03fd4
JM
32/*
33 * p2p_scan recovery timeout
34 *
35 * Many drivers are using 30 second timeout on scan results. Allow a bit larger
36 * timeout for this to avoid hitting P2P timeout unnecessarily.
37 */
38#define P2P_SCAN_TIMEOUT 35
39
b22128ef
JM
40/**
41 * P2P_PEER_EXPIRATION_AGE - Number of seconds after which inactive peer
42 * entries will be removed
43 */
44#define P2P_PEER_EXPIRATION_AGE 300
45
46#define P2P_PEER_EXPIRATION_INTERVAL (P2P_PEER_EXPIRATION_AGE / 2)
47
48static void p2p_expire_peers(struct p2p_data *p2p)
49{
50 struct p2p_device *dev, *n;
51 struct os_time now;
1d277f02 52 size_t i;
b22128ef
JM
53
54 os_get_time(&now);
55 dl_list_for_each_safe(dev, n, &p2p->devices, struct p2p_device, list) {
56 if (dev->last_seen.sec + P2P_PEER_EXPIRATION_AGE >= now.sec)
57 continue;
b1aebbc4
JM
58
59 if (p2p->cfg->go_connected &&
60 p2p->cfg->go_connected(p2p->cfg->cb_ctx,
61 dev->info.p2p_device_addr)) {
62 /*
63 * We are connected as a client to a group in which the
64 * peer is the GO, so do not expire the peer entry.
65 */
66 os_get_time(&dev->last_seen);
67 continue;
68 }
69
1d277f02
JM
70 for (i = 0; i < p2p->num_groups; i++) {
71 if (p2p_group_is_client_connected(
72 p2p->groups[i], dev->info.p2p_device_addr))
73 break;
74 }
75 if (i < p2p->num_groups) {
76 /*
77 * The peer is connected as a client in a group where
78 * we are the GO, so do not expire the peer entry.
79 */
80 os_get_time(&dev->last_seen);
81 continue;
82 }
83
ed496f13
JM
84 p2p_dbg(p2p, "Expiring old peer entry " MACSTR,
85 MAC2STR(dev->info.p2p_device_addr));
b22128ef
JM
86 dl_list_del(&dev->list);
87 p2p_device_free(p2p, dev);
88 }
89}
90
91
92static void p2p_expiration_timeout(void *eloop_ctx, void *timeout_ctx)
93{
94 struct p2p_data *p2p = eloop_ctx;
95 p2p_expire_peers(p2p);
96 eloop_register_timeout(P2P_PEER_EXPIRATION_INTERVAL, 0,
97 p2p_expiration_timeout, p2p, NULL);
98}
99
100
101static const char * p2p_state_txt(int state)
102{
103 switch (state) {
104 case P2P_IDLE:
105 return "IDLE";
106 case P2P_SEARCH:
107 return "SEARCH";
108 case P2P_CONNECT:
109 return "CONNECT";
110 case P2P_CONNECT_LISTEN:
111 return "CONNECT_LISTEN";
112 case P2P_GO_NEG:
113 return "GO_NEG";
114 case P2P_LISTEN_ONLY:
115 return "LISTEN_ONLY";
116 case P2P_WAIT_PEER_CONNECT:
117 return "WAIT_PEER_CONNECT";
118 case P2P_WAIT_PEER_IDLE:
119 return "WAIT_PEER_IDLE";
120 case P2P_SD_DURING_FIND:
121 return "SD_DURING_FIND";
122 case P2P_PROVISIONING:
123 return "PROVISIONING";
124 case P2P_PD_DURING_FIND:
125 return "PD_DURING_FIND";
126 case P2P_INVITE:
127 return "INVITE";
128 case P2P_INVITE_LISTEN:
129 return "INVITE_LISTEN";
39185dfa
JM
130 case P2P_SEARCH_WHEN_READY:
131 return "SEARCH_WHEN_READY";
99fcd404
JM
132 case P2P_CONTINUE_SEARCH_WHEN_READY:
133 return "CONTINUE_SEARCH_WHEN_READY";
b22128ef
JM
134 default:
135 return "?";
136 }
137}
138
139
4c559019
JM
140const char * p2p_get_state_txt(struct p2p_data *p2p)
141{
142 return p2p_state_txt(p2p->state);
143}
144
145
ec437d9e
JJ
146u16 p2p_get_provisioning_info(struct p2p_data *p2p, const u8 *addr)
147{
148 struct p2p_device *dev = NULL;
149
150 if (!addr || !p2p)
151 return 0;
152
153 dev = p2p_get_device(p2p, addr);
154 if (dev)
155 return dev->wps_prov_info;
156 else
157 return 0;
158}
159
160
10531d21 161void p2p_clear_provisioning_info(struct p2p_data *p2p, const u8 *addr)
ec437d9e
JJ
162{
163 struct p2p_device *dev = NULL;
164
10531d21 165 if (!addr || !p2p)
ec437d9e
JJ
166 return;
167
10531d21 168 dev = p2p_get_device(p2p, addr);
ec437d9e
JJ
169 if (dev)
170 dev->wps_prov_info = 0;
171}
172
173
b22128ef
JM
174void p2p_set_state(struct p2p_data *p2p, int new_state)
175{
ed496f13 176 p2p_dbg(p2p, "State %s -> %s",
b22128ef
JM
177 p2p_state_txt(p2p->state), p2p_state_txt(new_state));
178 p2p->state = new_state;
179}
180
181
182void p2p_set_timeout(struct p2p_data *p2p, unsigned int sec, unsigned int usec)
183{
ed496f13 184 p2p_dbg(p2p, "Set timeout (state=%s): %u.%06u sec",
b22128ef
JM
185 p2p_state_txt(p2p->state), sec, usec);
186 eloop_cancel_timeout(p2p_state_timeout, p2p, NULL);
187 eloop_register_timeout(sec, usec, p2p_state_timeout, p2p, NULL);
188}
189
190
191void p2p_clear_timeout(struct p2p_data *p2p)
192{
ed496f13 193 p2p_dbg(p2p, "Clear timeout (state=%s)", p2p_state_txt(p2p->state));
b22128ef
JM
194 eloop_cancel_timeout(p2p_state_timeout, p2p, NULL);
195}
196
197
198void p2p_go_neg_failed(struct p2p_data *p2p, struct p2p_device *peer,
199 int status)
200{
201 struct p2p_go_neg_results res;
202 p2p_clear_timeout(p2p);
203 p2p_set_state(p2p, P2P_IDLE);
fb8984fd
JM
204 if (p2p->go_neg_peer) {
205 p2p->go_neg_peer->flags &= ~P2P_DEV_PEER_WAITING_RESPONSE;
eb916eb8 206 p2p->go_neg_peer->wps_method = WPS_NOT_READY;
fb8984fd 207 }
b22128ef
JM
208 p2p->go_neg_peer = NULL;
209
210 os_memset(&res, 0, sizeof(res));
211 res.status = status;
212 if (peer) {
c5db8e51 213 os_memcpy(res.peer_device_addr, peer->info.p2p_device_addr,
b22128ef
JM
214 ETH_ALEN);
215 os_memcpy(res.peer_interface_addr, peer->intended_addr,
216 ETH_ALEN);
217 }
218 p2p->cfg->go_neg_completed(p2p->cfg->cb_ctx, &res);
219}
220
221
96beff11 222static void p2p_listen_in_find(struct p2p_data *p2p, int dev_disc)
b22128ef
JM
223{
224 unsigned int r, tu;
225 int freq;
226 struct wpabuf *ies;
227
ed496f13 228 p2p_dbg(p2p, "Starting short listen state (state=%s)",
b22128ef
JM
229 p2p_state_txt(p2p->state));
230
9ccd9165 231 freq = p2p_channel_to_freq(p2p->cfg->reg_class, p2p->cfg->channel);
b22128ef 232 if (freq < 0) {
ed496f13 233 p2p_dbg(p2p, "Unknown regulatory class/channel");
b22128ef
JM
234 return;
235 }
236
237 os_get_random((u8 *) &r, sizeof(r));
238 tu = (r % ((p2p->max_disc_int - p2p->min_disc_int) + 1) +
239 p2p->min_disc_int) * 100;
96beff11
JM
240 if (p2p->max_disc_tu >= 0 && tu > (unsigned int) p2p->max_disc_tu)
241 tu = p2p->max_disc_tu;
242 if (!dev_disc && tu < 100)
243 tu = 100; /* Need to wait in non-device discovery use cases */
244 if (p2p->cfg->max_listen && 1024 * tu / 1000 > p2p->cfg->max_listen)
245 tu = p2p->cfg->max_listen * 1000 / 1024;
246
247 if (tu == 0) {
ed496f13 248 p2p_dbg(p2p, "Skip listen state since duration was 0 TU");
96beff11
JM
249 p2p_set_timeout(p2p, 0, 0);
250 return;
251 }
b22128ef
JM
252
253 p2p->pending_listen_freq = freq;
254 p2p->pending_listen_sec = 0;
255 p2p->pending_listen_usec = 1024 * tu;
256
257 ies = p2p_build_probe_resp_ies(p2p);
258 if (ies == NULL)
259 return;
260
261 if (p2p->cfg->start_listen(p2p->cfg->cb_ctx, freq, 1024 * tu / 1000,
262 ies) < 0) {
ed496f13 263 p2p_dbg(p2p, "Failed to start listen mode");
b22128ef
JM
264 p2p->pending_listen_freq = 0;
265 }
266 wpabuf_free(ies);
267}
268
269
270int p2p_listen(struct p2p_data *p2p, unsigned int timeout)
271{
272 int freq;
273 struct wpabuf *ies;
274
ed496f13 275 p2p_dbg(p2p, "Going to listen(only) state");
b22128ef 276
9ccd9165 277 freq = p2p_channel_to_freq(p2p->cfg->reg_class, p2p->cfg->channel);
b22128ef 278 if (freq < 0) {
ed496f13 279 p2p_dbg(p2p, "Unknown regulatory class/channel");
b22128ef
JM
280 return -1;
281 }
282
283 p2p->pending_listen_freq = freq;
284 p2p->pending_listen_sec = timeout / 1000;
285 p2p->pending_listen_usec = (timeout % 1000) * 1000;
286
287 if (p2p->p2p_scan_running) {
5b9cecaf 288 if (p2p->start_after_scan == P2P_AFTER_SCAN_CONNECT) {
ed496f13 289 p2p_dbg(p2p, "p2p_scan running - connect is already pending - skip listen");
d9bdba9f
JM
290 return 0;
291 }
ed496f13 292 p2p_dbg(p2p, "p2p_scan running - delay start of listen state");
b22128ef
JM
293 p2p->start_after_scan = P2P_AFTER_SCAN_LISTEN;
294 return 0;
295 }
296
297 ies = p2p_build_probe_resp_ies(p2p);
298 if (ies == NULL)
299 return -1;
300
301 if (p2p->cfg->start_listen(p2p->cfg->cb_ctx, freq, timeout, ies) < 0) {
ed496f13 302 p2p_dbg(p2p, "Failed to start listen mode");
b22128ef
JM
303 p2p->pending_listen_freq = 0;
304 wpabuf_free(ies);
305 return -1;
306 }
307 wpabuf_free(ies);
308
309 p2p_set_state(p2p, P2P_LISTEN_ONLY);
310
311 return 0;
312}
313
314
315static void p2p_device_clear_reported(struct p2p_data *p2p)
316{
317 struct p2p_device *dev;
318 dl_list_for_each(dev, &p2p->devices, struct p2p_device, list)
319 dev->flags &= ~P2P_DEV_REPORTED;
320}
321
322
323/**
324 * p2p_get_device - Fetch a peer entry
325 * @p2p: P2P module context from p2p_init()
326 * @addr: P2P Device Address of the peer
327 * Returns: Pointer to the device entry or %NULL if not found
328 */
329struct p2p_device * p2p_get_device(struct p2p_data *p2p, const u8 *addr)
330{
331 struct p2p_device *dev;
332 dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
c5db8e51 333 if (os_memcmp(dev->info.p2p_device_addr, addr, ETH_ALEN) == 0)
b22128ef
JM
334 return dev;
335 }
336 return NULL;
337}
338
339
340/**
341 * p2p_get_device_interface - Fetch a peer entry based on P2P Interface Address
342 * @p2p: P2P module context from p2p_init()
343 * @addr: P2P Interface Address of the peer
344 * Returns: Pointer to the device entry or %NULL if not found
345 */
346struct p2p_device * p2p_get_device_interface(struct p2p_data *p2p,
347 const u8 *addr)
348{
349 struct p2p_device *dev;
350 dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
351 if (os_memcmp(dev->interface_addr, addr, ETH_ALEN) == 0)
352 return dev;
353 }
354 return NULL;
355}
356
357
358/**
359 * p2p_create_device - Create a peer entry
360 * @p2p: P2P module context from p2p_init()
361 * @addr: P2P Device Address of the peer
362 * Returns: Pointer to the device entry or %NULL on failure
363 *
364 * If there is already an entry for the peer, it will be returned instead of
365 * creating a new one.
366 */
367static struct p2p_device * p2p_create_device(struct p2p_data *p2p,
368 const u8 *addr)
369{
370 struct p2p_device *dev, *oldest = NULL;
371 size_t count = 0;
372
373 dev = p2p_get_device(p2p, addr);
374 if (dev)
375 return dev;
376
377 dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
378 count++;
379 if (oldest == NULL ||
380 os_time_before(&dev->last_seen, &oldest->last_seen))
381 oldest = dev;
382 }
383 if (count + 1 > p2p->cfg->max_peers && oldest) {
ed496f13 384 p2p_dbg(p2p, "Remove oldest peer entry to make room for a new peer");
b22128ef
JM
385 dl_list_del(&oldest->list);
386 p2p_device_free(p2p, oldest);
387 }
388
389 dev = os_zalloc(sizeof(*dev));
390 if (dev == NULL)
391 return NULL;
392 dl_list_add(&p2p->devices, &dev->list);
c5db8e51 393 os_memcpy(dev->info.p2p_device_addr, addr, ETH_ALEN);
b22128ef
JM
394
395 return dev;
396}
397
398
399static void p2p_copy_client_info(struct p2p_device *dev,
400 struct p2p_client_info *cli)
401{
c5db8e51
KRK
402 os_memcpy(dev->info.device_name, cli->dev_name, cli->dev_name_len);
403 dev->info.device_name[cli->dev_name_len] = '\0';
404 dev->info.dev_capab = cli->dev_capab;
405 dev->info.config_methods = cli->config_methods;
406 os_memcpy(dev->info.pri_dev_type, cli->pri_dev_type, 8);
e57ae6e1
JMB
407 dev->info.wps_sec_dev_type_list_len = 8 * cli->num_sec_dev_types;
408 os_memcpy(dev->info.wps_sec_dev_type_list, cli->sec_dev_types,
409 dev->info.wps_sec_dev_type_list_len);
b22128ef
JM
410}
411
412
413static int p2p_add_group_clients(struct p2p_data *p2p, const u8 *go_dev_addr,
414 const u8 *go_interface_addr, int freq,
415 const u8 *gi, size_t gi_len)
416{
417 struct p2p_group_info info;
418 size_t c;
419 struct p2p_device *dev;
420
421 if (gi == NULL)
422 return 0;
423
424 if (p2p_group_info_parse(gi, gi_len, &info) < 0)
425 return -1;
426
427 /*
428 * Clear old data for this group; if the devices are still in the
429 * group, the information will be restored in the loop following this.
430 */
431 dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
b5472a45 432 if (os_memcmp(dev->member_in_go_iface, go_interface_addr,
b22128ef
JM
433 ETH_ALEN) == 0) {
434 os_memset(dev->member_in_go_iface, 0, ETH_ALEN);
435 os_memset(dev->member_in_go_dev, 0, ETH_ALEN);
436 }
437 }
438
439 for (c = 0; c < info.num_clients; c++) {
440 struct p2p_client_info *cli = &info.client[c];
2f0c8936
MK
441 if (os_memcmp(cli->p2p_device_addr, p2p->cfg->dev_addr,
442 ETH_ALEN) == 0)
443 continue; /* ignore our own entry */
b22128ef
JM
444 dev = p2p_get_device(p2p, cli->p2p_device_addr);
445 if (dev) {
b22128ef 446 if (dev->flags & (P2P_DEV_GROUP_CLIENT_ONLY |
f33bc035
JM
447 P2P_DEV_PROBE_REQ_ONLY)) {
448 /*
449 * Update information since we have not
450 * received this directly from the client.
451 */
b22128ef 452 p2p_copy_client_info(dev, cli);
f33bc035
JM
453 } else {
454 /*
455 * Need to update P2P Client Discoverability
456 * flag since it is valid only in P2P Group
457 * Info attribute.
458 */
459 dev->info.dev_capab &=
460 ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
461 dev->info.dev_capab |=
462 cli->dev_capab &
463 P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
464 }
b22128ef
JM
465 if (dev->flags & P2P_DEV_PROBE_REQ_ONLY) {
466 dev->flags &= ~P2P_DEV_PROBE_REQ_ONLY;
467 }
468 } else {
469 dev = p2p_create_device(p2p, cli->p2p_device_addr);
470 if (dev == NULL)
471 continue;
472 dev->flags |= P2P_DEV_GROUP_CLIENT_ONLY;
473 p2p_copy_client_info(dev, cli);
474 dev->oper_freq = freq;
c5db8e51
KRK
475 p2p->cfg->dev_found(p2p->cfg->cb_ctx,
476 dev->info.p2p_device_addr,
8fd7dc1b
JB
477 &dev->info, 1);
478 dev->flags |= P2P_DEV_REPORTED | P2P_DEV_REPORTED_ONCE;
b22128ef
JM
479 }
480
481 os_memcpy(dev->interface_addr, cli->p2p_interface_addr,
482 ETH_ALEN);
483 os_get_time(&dev->last_seen);
484 os_memcpy(dev->member_in_go_dev, go_dev_addr, ETH_ALEN);
485 os_memcpy(dev->member_in_go_iface, go_interface_addr,
486 ETH_ALEN);
487 }
488
489 return 0;
490}
491
492
ed496f13
JM
493static void p2p_copy_wps_info(struct p2p_data *p2p, struct p2p_device *dev,
494 int probe_req, const struct p2p_message *msg)
b67d0d9e
JM
495{
496 os_memcpy(dev->info.device_name, msg->device_name,
497 sizeof(dev->info.device_name));
498
499 if (msg->manufacturer &&
500 msg->manufacturer_len < sizeof(dev->info.manufacturer)) {
501 os_memset(dev->info.manufacturer, 0,
502 sizeof(dev->info.manufacturer));
503 os_memcpy(dev->info.manufacturer, msg->manufacturer,
504 msg->manufacturer_len);
505 }
506
507 if (msg->model_name &&
508 msg->model_name_len < sizeof(dev->info.model_name)) {
509 os_memset(dev->info.model_name, 0,
510 sizeof(dev->info.model_name));
511 os_memcpy(dev->info.model_name, msg->model_name,
512 msg->model_name_len);
513 }
514
515 if (msg->model_number &&
516 msg->model_number_len < sizeof(dev->info.model_number)) {
517 os_memset(dev->info.model_number, 0,
518 sizeof(dev->info.model_number));
519 os_memcpy(dev->info.model_number, msg->model_number,
520 msg->model_number_len);
521 }
522
523 if (msg->serial_number &&
524 msg->serial_number_len < sizeof(dev->info.serial_number)) {
525 os_memset(dev->info.serial_number, 0,
526 sizeof(dev->info.serial_number));
527 os_memcpy(dev->info.serial_number, msg->serial_number,
528 msg->serial_number_len);
529 }
530
531 if (msg->pri_dev_type)
532 os_memcpy(dev->info.pri_dev_type, msg->pri_dev_type,
533 sizeof(dev->info.pri_dev_type));
534 else if (msg->wps_pri_dev_type)
535 os_memcpy(dev->info.pri_dev_type, msg->wps_pri_dev_type,
536 sizeof(dev->info.pri_dev_type));
537
538 if (msg->wps_sec_dev_type_list) {
539 os_memcpy(dev->info.wps_sec_dev_type_list,
540 msg->wps_sec_dev_type_list,
541 msg->wps_sec_dev_type_list_len);
542 dev->info.wps_sec_dev_type_list_len =
543 msg->wps_sec_dev_type_list_len;
544 }
545
546 if (msg->capability) {
f33bc035
JM
547 /*
548 * P2P Client Discoverability bit is reserved in all frames
549 * that use this function, so do not change its value here.
550 */
551 dev->info.dev_capab &= P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
552 dev->info.dev_capab |= msg->capability[0] &
553 ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
b67d0d9e
JM
554 dev->info.group_capab = msg->capability[1];
555 }
556
557 if (msg->ext_listen_timing) {
558 dev->ext_listen_period = WPA_GET_LE16(msg->ext_listen_timing);
559 dev->ext_listen_interval =
560 WPA_GET_LE16(msg->ext_listen_timing + 2);
561 }
562
563 if (!probe_req) {
954ee628
JM
564 u16 new_config_methods;
565 new_config_methods = msg->config_methods ?
b67d0d9e 566 msg->config_methods : msg->wps_config_methods;
954ee628
JM
567 if (new_config_methods &&
568 dev->info.config_methods != new_config_methods) {
ed496f13
JM
569 p2p_dbg(p2p, "Update peer " MACSTR
570 " config_methods 0x%x -> 0x%x",
571 MAC2STR(dev->info.p2p_device_addr),
572 dev->info.config_methods,
573 new_config_methods);
954ee628
JM
574 dev->info.config_methods = new_config_methods;
575 }
b67d0d9e
JM
576 }
577}
578
579
b22128ef 580/**
c98b83f2 581 * p2p_add_device - Add peer entries based on scan results or P2P frames
b22128ef
JM
582 * @p2p: P2P module context from p2p_init()
583 * @addr: Source address of Beacon or Probe Response frame (may be either
584 * P2P Device Address or P2P Interface Address)
585 * @level: Signal level (signal strength of the received frame from the peer)
586 * @freq: Frequency on which the Beacon or Probe Response frame was received
c5f10e80 587 * @rx_time: Time when the result was received
b22128ef
JM
588 * @ies: IEs from the Beacon or Probe Response frame
589 * @ies_len: Length of ies buffer in octets
c98b83f2 590 * @scan_res: Whether this was based on scan results
b22128ef
JM
591 * Returns: 0 on success, -1 on failure
592 *
593 * If the scan result is for a GO, the clients in the group will also be added
17bef1e9
AC
594 * to the peer table. This function can also be used with some other frames
595 * like Provision Discovery Request that contains P2P Capability and P2P Device
596 * Info attributes.
b22128ef 597 */
3dfd0484 598int p2p_add_device(struct p2p_data *p2p, const u8 *addr, int freq,
c5f10e80 599 struct os_time *rx_time, int level, const u8 *ies,
3dfd0484 600 size_t ies_len, int scan_res)
b22128ef
JM
601{
602 struct p2p_device *dev;
603 struct p2p_message msg;
604 const u8 *p2p_dev_addr;
6f2c0607 605 int i;
c5f10e80 606 struct os_time time_now;
b22128ef
JM
607
608 os_memset(&msg, 0, sizeof(msg));
609 if (p2p_parse_ies(ies, ies_len, &msg)) {
ed496f13 610 p2p_dbg(p2p, "Failed to parse P2P IE for a device entry");
b22128ef
JM
611 p2p_parse_free(&msg);
612 return -1;
613 }
614
615 if (msg.p2p_device_addr)
616 p2p_dev_addr = msg.p2p_device_addr;
617 else if (msg.device_id)
618 p2p_dev_addr = msg.device_id;
619 else {
ed496f13 620 p2p_dbg(p2p, "Ignore scan data without P2P Device Info or P2P Device Id");
b22128ef
JM
621 p2p_parse_free(&msg);
622 return -1;
623 }
624
80c9582a
JM
625 if (!is_zero_ether_addr(p2p->peer_filter) &&
626 os_memcmp(p2p_dev_addr, p2p->peer_filter, ETH_ALEN) != 0) {
ed496f13
JM
627 p2p_dbg(p2p, "Do not add peer filter for " MACSTR
628 " due to peer filter", MAC2STR(p2p_dev_addr));
f96c1d76 629 p2p_parse_free(&msg);
80c9582a
JM
630 return 0;
631 }
632
b22128ef
JM
633 dev = p2p_create_device(p2p, p2p_dev_addr);
634 if (dev == NULL) {
635 p2p_parse_free(&msg);
636 return -1;
637 }
3dfd0484 638
c5f10e80
JM
639 if (rx_time == NULL) {
640 os_get_time(&time_now);
641 rx_time = &time_now;
642 }
3dfd0484
YD
643
644 /*
645 * Update the device entry only if the new peer
646 * entry is newer than the one previously stored.
647 */
c5f10e80
JM
648 if (dev->last_seen.sec > 0 &&
649 os_time_before(rx_time, &dev->last_seen)) {
ed496f13 650 p2p_dbg(p2p, "Do not update peer entry based on old frame (rx_time=%u.%06u last_seen=%u.%06u)",
c5f10e80
JM
651 (unsigned int) rx_time->sec,
652 (unsigned int) rx_time->usec,
653 (unsigned int) dev->last_seen.sec,
654 (unsigned int) dev->last_seen.usec);
f96c1d76 655 p2p_parse_free(&msg);
3dfd0484 656 return -1;
f96c1d76 657 }
3dfd0484 658
c5f10e80 659 os_memcpy(&dev->last_seen, rx_time, sizeof(struct os_time));
3dfd0484 660
b22128ef
JM
661 dev->flags &= ~(P2P_DEV_PROBE_REQ_ONLY | P2P_DEV_GROUP_CLIENT_ONLY);
662
663 if (os_memcmp(addr, p2p_dev_addr, ETH_ALEN) != 0)
664 os_memcpy(dev->interface_addr, addr, ETH_ALEN);
665 if (msg.ssid &&
666 (msg.ssid[1] != P2P_WILDCARD_SSID_LEN ||
667 os_memcmp(msg.ssid + 2, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN)
668 != 0)) {
669 os_memcpy(dev->oper_ssid, msg.ssid + 2, msg.ssid[1]);
670 dev->oper_ssid_len = msg.ssid[1];
671 }
672
673 if (freq >= 2412 && freq <= 2484 && msg.ds_params &&
674 *msg.ds_params >= 1 && *msg.ds_params <= 14) {
675 int ds_freq;
676 if (*msg.ds_params == 14)
677 ds_freq = 2484;
678 else
679 ds_freq = 2407 + *msg.ds_params * 5;
680 if (freq != ds_freq) {
ed496f13 681 p2p_dbg(p2p, "Update Listen frequency based on DS Parameter Set IE: %d -> %d MHz",
b22128ef
JM
682 freq, ds_freq);
683 freq = ds_freq;
684 }
685 }
686
c98b83f2 687 if (dev->listen_freq && dev->listen_freq != freq && scan_res) {
ed496f13
JM
688 p2p_dbg(p2p, "Update Listen frequency based on scan results ("
689 MACSTR " %d -> %d MHz (DS param %d)",
c5db8e51
KRK
690 MAC2STR(dev->info.p2p_device_addr), dev->listen_freq,
691 freq, msg.ds_params ? *msg.ds_params : -1);
b22128ef 692 }
c98b83f2
JM
693 if (scan_res) {
694 dev->listen_freq = freq;
695 if (msg.group_info)
696 dev->oper_freq = freq;
697 }
6402fc43 698 dev->info.level = level;
b22128ef 699
ed496f13 700 p2p_copy_wps_info(p2p, dev, 0, &msg);
e57ae6e1 701
10c5d2a5 702 for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) {
6f2c0607
JMB
703 wpabuf_free(dev->info.wps_vendor_ext[i]);
704 dev->info.wps_vendor_ext[i] = NULL;
705 }
706
10c5d2a5 707 for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) {
6f2c0607
JMB
708 if (msg.wps_vendor_ext[i] == NULL)
709 break;
710 dev->info.wps_vendor_ext[i] = wpabuf_alloc_copy(
711 msg.wps_vendor_ext[i], msg.wps_vendor_ext_len[i]);
712 if (dev->info.wps_vendor_ext[i] == NULL)
713 break;
714 }
715
9675ce35
JM
716 if (msg.wfd_subelems) {
717 wpabuf_free(dev->info.wfd_subelems);
718 dev->info.wfd_subelems = wpabuf_dup(msg.wfd_subelems);
719 }
720
c98b83f2
JM
721 if (scan_res) {
722 p2p_add_group_clients(p2p, p2p_dev_addr, addr, freq,
723 msg.group_info, msg.group_info_len);
724 }
b22128ef
JM
725
726 p2p_parse_free(&msg);
727
728 if (p2p_pending_sd_req(p2p, dev))
729 dev->flags |= P2P_DEV_SD_SCHEDULE;
730
731 if (dev->flags & P2P_DEV_REPORTED)
732 return 0;
733
ed496f13
JM
734 p2p_dbg(p2p, "Peer found with Listen frequency %d MHz (rx_time=%u.%06u)",
735 freq, (unsigned int) rx_time->sec,
c5f10e80 736 (unsigned int) rx_time->usec);
b22128ef 737 if (dev->flags & P2P_DEV_USER_REJECTED) {
ed496f13 738 p2p_dbg(p2p, "Do not report rejected device");
b22128ef
JM
739 return 0;
740 }
8fd7dc1b 741
8b2b2a70
JM
742 if (dev->info.config_methods == 0 &&
743 (freq == 2412 || freq == 2437 || freq == 2462)) {
744 /*
745 * If we have only seen a Beacon frame from a GO, we do not yet
746 * know what WPS config methods it supports. Since some
747 * applications use config_methods value from P2P-DEVICE-FOUND
748 * events, postpone reporting this peer until we've fully
749 * discovered its capabilities.
750 *
751 * At least for now, do this only if the peer was detected on
752 * one of the social channels since that peer can be easily be
753 * found again and there are no limitations of having to use
754 * passive scan on this channels, so this can be done through
755 * Probe Response frame that includes the config_methods
756 * information.
757 */
ed496f13
JM
758 p2p_dbg(p2p, "Do not report peer " MACSTR
759 " with unknown config methods", MAC2STR(addr));
8b2b2a70
JM
760 return 0;
761 }
762
8fd7dc1b
JB
763 p2p->cfg->dev_found(p2p->cfg->cb_ctx, addr, &dev->info,
764 !(dev->flags & P2P_DEV_REPORTED_ONCE));
765 dev->flags |= P2P_DEV_REPORTED | P2P_DEV_REPORTED_ONCE;
b22128ef
JM
766
767 return 0;
768}
769
770
771static void p2p_device_free(struct p2p_data *p2p, struct p2p_device *dev)
772{
6f2c0607
JMB
773 int i;
774
5cfda25e
JJ
775 if (p2p->go_neg_peer == dev) {
776 /*
777 * If GO Negotiation is in progress, report that it has failed.
778 */
779 p2p_go_neg_failed(p2p, dev, -1);
b22128ef 780 p2p->go_neg_peer = NULL;
5cfda25e 781 }
b22128ef
JM
782 if (p2p->invite_peer == dev)
783 p2p->invite_peer = NULL;
784 if (p2p->sd_peer == dev)
785 p2p->sd_peer = NULL;
786 if (p2p->pending_client_disc_go == dev)
787 p2p->pending_client_disc_go = NULL;
788
f5fc6032
AC
789 /* dev_lost() device, but only if it was previously dev_found() */
790 if (dev->flags & P2P_DEV_REPORTED_ONCE)
791 p2p->cfg->dev_lost(p2p->cfg->cb_ctx,
792 dev->info.p2p_device_addr);
56eeb8f2 793
10c5d2a5 794 for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) {
6f2c0607
JMB
795 wpabuf_free(dev->info.wps_vendor_ext[i]);
796 dev->info.wps_vendor_ext[i] = NULL;
797 }
798
9675ce35
JM
799 wpabuf_free(dev->info.wfd_subelems);
800
b22128ef
JM
801 os_free(dev);
802}
803
804
805static int p2p_get_next_prog_freq(struct p2p_data *p2p)
806{
807 struct p2p_channels *c;
808 struct p2p_reg_class *cla;
809 size_t cl, ch;
810 int found = 0;
811 u8 reg_class;
812 u8 channel;
813 int freq;
814
815 c = &p2p->cfg->channels;
816 for (cl = 0; cl < c->reg_classes; cl++) {
817 cla = &c->reg_class[cl];
818 if (cla->reg_class != p2p->last_prog_scan_class)
819 continue;
820 for (ch = 0; ch < cla->channels; ch++) {
821 if (cla->channel[ch] == p2p->last_prog_scan_chan) {
822 found = 1;
823 break;
824 }
825 }
826 if (found)
827 break;
828 }
829
830 if (!found) {
831 /* Start from beginning */
832 reg_class = c->reg_class[0].reg_class;
833 channel = c->reg_class[0].channel[0];
834 } else {
835 /* Pick the next channel */
836 ch++;
837 if (ch == cla->channels) {
838 cl++;
839 if (cl == c->reg_classes)
840 cl = 0;
841 ch = 0;
842 }
843 reg_class = c->reg_class[cl].reg_class;
844 channel = c->reg_class[cl].channel[ch];
845 }
846
9ccd9165 847 freq = p2p_channel_to_freq(reg_class, channel);
ed496f13 848 p2p_dbg(p2p, "Next progressive search channel: reg_class %u channel %u -> %d MHz",
b22128ef
JM
849 reg_class, channel, freq);
850 p2p->last_prog_scan_class = reg_class;
851 p2p->last_prog_scan_chan = channel;
852
853 if (freq == 2412 || freq == 2437 || freq == 2462)
854 return 0; /* No need to add social channels */
855 return freq;
856}
857
858
859static void p2p_search(struct p2p_data *p2p)
860{
861 int freq = 0;
862 enum p2p_scan_type type;
360182ed 863 u16 pw_id = DEV_PW_DEFAULT;
99fcd404 864 int res;
b22128ef
JM
865
866 if (p2p->drv_in_listen) {
ed496f13 867 p2p_dbg(p2p, "Driver is still in Listen state - wait for it to end before continuing");
b22128ef
JM
868 return;
869 }
870 p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
871
e6ecfc4f
JB
872 if (p2p->find_type == P2P_FIND_PROGRESSIVE &&
873 (freq = p2p_get_next_prog_freq(p2p)) > 0) {
b22128ef 874 type = P2P_SCAN_SOCIAL_PLUS_ONE;
ed496f13 875 p2p_dbg(p2p, "Starting search (+ freq %u)", freq);
b22128ef
JM
876 } else {
877 type = P2P_SCAN_SOCIAL;
ed496f13 878 p2p_dbg(p2p, "Starting search");
b22128ef
JM
879 }
880
99fcd404
JM
881 res = p2p->cfg->p2p_scan(p2p->cfg->cb_ctx, type, freq,
882 p2p->num_req_dev_types, p2p->req_dev_types,
883 p2p->find_dev_id, pw_id);
884 if (res < 0) {
ed496f13 885 p2p_dbg(p2p, "Scan request failed");
b22128ef 886 p2p_continue_find(p2p);
99fcd404 887 } else if (res == 1) {
ed496f13 888 p2p_dbg(p2p, "Could not start p2p_scan at this point - will try again after previous scan completes");
99fcd404 889 p2p_set_state(p2p, P2P_CONTINUE_SEARCH_WHEN_READY);
40c03fd4 890 } else {
ed496f13 891 p2p_dbg(p2p, "Running p2p_scan");
40c03fd4
JM
892 p2p->p2p_scan_running = 1;
893 eloop_cancel_timeout(p2p_scan_timeout, p2p, NULL);
894 eloop_register_timeout(P2P_SCAN_TIMEOUT, 0, p2p_scan_timeout,
895 p2p, NULL);
b22128ef
JM
896 }
897}
898
899
900static void p2p_find_timeout(void *eloop_ctx, void *timeout_ctx)
901{
902 struct p2p_data *p2p = eloop_ctx;
ed496f13 903 p2p_dbg(p2p, "Find timeout -> stop");
b22128ef
JM
904 p2p_stop_find(p2p);
905}
906
907
908static int p2p_run_after_scan(struct p2p_data *p2p)
909{
910 struct p2p_device *dev;
911 enum p2p_after_scan op;
912
3f9285ff 913 if (p2p->after_scan_tx) {
63a965c3 914 p2p->after_scan_tx_in_progress = 1;
ed496f13 915 p2p_dbg(p2p, "Send pending Action frame at p2p_scan completion");
46eeedac
JM
916 p2p->cfg->send_action(p2p->cfg->cb_ctx,
917 p2p->after_scan_tx->freq,
918 p2p->after_scan_tx->dst,
919 p2p->after_scan_tx->src,
920 p2p->after_scan_tx->bssid,
921 (u8 *) (p2p->after_scan_tx + 1),
922 p2p->after_scan_tx->len,
923 p2p->after_scan_tx->wait_time);
3f9285ff
JM
924 os_free(p2p->after_scan_tx);
925 p2p->after_scan_tx = NULL;
926 return 1;
927 }
928
b22128ef
JM
929 op = p2p->start_after_scan;
930 p2p->start_after_scan = P2P_AFTER_SCAN_NOTHING;
931 switch (op) {
932 case P2P_AFTER_SCAN_NOTHING:
933 break;
934 case P2P_AFTER_SCAN_LISTEN:
ed496f13 935 p2p_dbg(p2p, "Start previously requested Listen state");
b22128ef
JM
936 p2p_listen(p2p, p2p->pending_listen_sec * 1000 +
937 p2p->pending_listen_usec / 1000);
938 return 1;
939 case P2P_AFTER_SCAN_CONNECT:
ed496f13 940 p2p_dbg(p2p, "Start previously requested connect with " MACSTR,
b22128ef
JM
941 MAC2STR(p2p->after_scan_peer));
942 dev = p2p_get_device(p2p, p2p->after_scan_peer);
943 if (dev == NULL) {
ed496f13 944 p2p_dbg(p2p, "Peer not known anymore");
b22128ef
JM
945 break;
946 }
947 p2p_connect_send(p2p, dev);
948 return 1;
949 }
950
951 return 0;
952}
953
954
b22128ef
JM
955static void p2p_scan_timeout(void *eloop_ctx, void *timeout_ctx)
956{
957 struct p2p_data *p2p = eloop_ctx;
958 int running;
ed496f13 959 p2p_dbg(p2p, "p2p_scan timeout (running=%d)", p2p->p2p_scan_running);
b22128ef
JM
960 running = p2p->p2p_scan_running;
961 /* Make sure we recover from missed scan results callback */
962 p2p->p2p_scan_running = 0;
963
964 if (running)
965 p2p_run_after_scan(p2p);
966}
967
968
046ef4aa
JMB
969static void p2p_free_req_dev_types(struct p2p_data *p2p)
970{
971 p2p->num_req_dev_types = 0;
972 os_free(p2p->req_dev_types);
973 p2p->req_dev_types = NULL;
974}
975
976
b22128ef 977int p2p_find(struct p2p_data *p2p, unsigned int timeout,
046ef4aa 978 enum p2p_discovery_type type,
6d92fa6e 979 unsigned int num_req_dev_types, const u8 *req_dev_types,
37448ede 980 const u8 *dev_id, unsigned int search_delay)
b22128ef
JM
981{
982 int res;
983
ed496f13 984 p2p_dbg(p2p, "Starting find (type=%d)", type);
a5b5e830 985 os_get_time(&p2p->find_start);
b22128ef 986 if (p2p->p2p_scan_running) {
ed496f13 987 p2p_dbg(p2p, "p2p_scan is already running");
b22128ef 988 }
046ef4aa
JMB
989
990 p2p_free_req_dev_types(p2p);
991 if (req_dev_types && num_req_dev_types) {
992 p2p->req_dev_types = os_malloc(num_req_dev_types *
993 WPS_DEV_TYPE_LEN);
994 if (p2p->req_dev_types == NULL)
995 return -1;
996 os_memcpy(p2p->req_dev_types, req_dev_types,
997 num_req_dev_types * WPS_DEV_TYPE_LEN);
998 p2p->num_req_dev_types = num_req_dev_types;
999 }
1000
6d92fa6e
JM
1001 if (dev_id) {
1002 os_memcpy(p2p->find_dev_id_buf, dev_id, ETH_ALEN);
1003 p2p->find_dev_id = p2p->find_dev_id_buf;
1004 } else
1005 p2p->find_dev_id = NULL;
1006
b22128ef
JM
1007 p2p->start_after_scan = P2P_AFTER_SCAN_NOTHING;
1008 p2p_clear_timeout(p2p);
1009 p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
1010 p2p->find_type = type;
1011 p2p_device_clear_reported(p2p);
1012 p2p_set_state(p2p, P2P_SEARCH);
37448ede
JM
1013 p2p->search_delay = search_delay;
1014 p2p->in_search_delay = 0;
b22128ef 1015 eloop_cancel_timeout(p2p_find_timeout, p2p, NULL);
39185dfa 1016 p2p->last_p2p_find_timeout = timeout;
b22128ef
JM
1017 if (timeout)
1018 eloop_register_timeout(timeout, 0, p2p_find_timeout,
1019 p2p, NULL);
1020 switch (type) {
1021 case P2P_FIND_START_WITH_FULL:
1022 case P2P_FIND_PROGRESSIVE:
046ef4aa
JMB
1023 res = p2p->cfg->p2p_scan(p2p->cfg->cb_ctx, P2P_SCAN_FULL, 0,
1024 p2p->num_req_dev_types,
360182ed
JM
1025 p2p->req_dev_types, dev_id,
1026 DEV_PW_DEFAULT);
b22128ef
JM
1027 break;
1028 case P2P_FIND_ONLY_SOCIAL:
046ef4aa
JMB
1029 res = p2p->cfg->p2p_scan(p2p->cfg->cb_ctx, P2P_SCAN_SOCIAL, 0,
1030 p2p->num_req_dev_types,
360182ed
JM
1031 p2p->req_dev_types, dev_id,
1032 DEV_PW_DEFAULT);
b22128ef
JM
1033 break;
1034 default:
1035 return -1;
1036 }
1037
1038 if (res == 0) {
ed496f13 1039 p2p_dbg(p2p, "Running p2p_scan");
b22128ef
JM
1040 p2p->p2p_scan_running = 1;
1041 eloop_cancel_timeout(p2p_scan_timeout, p2p, NULL);
1042 eloop_register_timeout(P2P_SCAN_TIMEOUT, 0, p2p_scan_timeout,
1043 p2p, NULL);
39185dfa 1044 } else if (res == 1) {
ed496f13 1045 p2p_dbg(p2p, "Could not start p2p_scan at this point - will try again after previous scan completes");
39185dfa
JM
1046 res = 0;
1047 p2p_set_state(p2p, P2P_SEARCH_WHEN_READY);
1048 eloop_cancel_timeout(p2p_find_timeout, p2p, NULL);
b22128ef 1049 } else {
ed496f13 1050 p2p_dbg(p2p, "Failed to start p2p_scan");
0c96fd6d
JM
1051 p2p_set_state(p2p, P2P_IDLE);
1052 eloop_cancel_timeout(p2p_find_timeout, p2p, NULL);
b22128ef
JM
1053 }
1054
1055 return res;
1056}
1057
1058
39185dfa
JM
1059int p2p_other_scan_completed(struct p2p_data *p2p)
1060{
99fcd404
JM
1061 if (p2p->state == P2P_CONTINUE_SEARCH_WHEN_READY) {
1062 p2p_set_state(p2p, P2P_SEARCH);
1063 p2p_search(p2p);
1064 return 1;
1065 }
39185dfa
JM
1066 if (p2p->state != P2P_SEARCH_WHEN_READY)
1067 return 0;
ed496f13 1068 p2p_dbg(p2p, "Starting pending P2P find now that previous scan was completed");
39185dfa 1069 if (p2p_find(p2p, p2p->last_p2p_find_timeout, p2p->find_type,
6d92fa6e 1070 p2p->num_req_dev_types, p2p->req_dev_types,
c002f640 1071 p2p->find_dev_id, p2p->search_delay) < 0) {
710ae9ac 1072 p2p->cfg->find_stopped(p2p->cfg->cb_ctx);
39185dfa 1073 return 0;
c002f640 1074 }
39185dfa
JM
1075 return 1;
1076}
1077
1078
0b8889d8 1079void p2p_stop_find_for_freq(struct p2p_data *p2p, int freq)
b22128ef 1080{
ed496f13 1081 p2p_dbg(p2p, "Stopping find");
b22128ef
JM
1082 eloop_cancel_timeout(p2p_find_timeout, p2p, NULL);
1083 p2p_clear_timeout(p2p);
a16ed53a
PK
1084 if (p2p->state == P2P_SEARCH ||
1085 p2p->state == P2P_CONTINUE_SEARCH_WHEN_READY ||
1086 p2p->state == P2P_SEARCH_WHEN_READY)
710ae9ac 1087 p2p->cfg->find_stopped(p2p->cfg->cb_ctx);
b22128ef 1088 p2p_set_state(p2p, P2P_IDLE);
046ef4aa 1089 p2p_free_req_dev_types(p2p);
b22128ef 1090 p2p->start_after_scan = P2P_AFTER_SCAN_NOTHING;
3a2a7c3d 1091 if (p2p->go_neg_peer)
fb8984fd 1092 p2p->go_neg_peer->flags &= ~P2P_DEV_PEER_WAITING_RESPONSE;
b22128ef
JM
1093 p2p->go_neg_peer = NULL;
1094 p2p->sd_peer = NULL;
1095 p2p->invite_peer = NULL;
1a9c618d
JM
1096 p2p_stop_listen_for_freq(p2p, freq);
1097}
1098
1099
1100void p2p_stop_listen_for_freq(struct p2p_data *p2p, int freq)
1101{
0b8889d8 1102 if (freq > 0 && p2p->drv_in_listen == freq && p2p->in_listen) {
ed496f13 1103 p2p_dbg(p2p, "Skip stop_listen since we are on correct channel for response");
0b8889d8
JM
1104 return;
1105 }
1a9c618d
JM
1106 if (p2p->in_listen) {
1107 p2p->in_listen = 0;
1108 p2p_clear_timeout(p2p);
1109 }
54b8f994
JM
1110 if (p2p->drv_in_listen) {
1111 /*
1112 * The driver may not deliver callback to p2p_listen_end()
1113 * when the operation gets canceled, so clear the internal
1114 * variable that is tracking driver state.
1115 */
ed496f13 1116 p2p_dbg(p2p, "Clear drv_in_listen (%d)", p2p->drv_in_listen);
54b8f994
JM
1117 p2p->drv_in_listen = 0;
1118 }
b22128ef
JM
1119 p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
1120}
1121
1122
0b5fb86a
SADR
1123void p2p_stop_listen(struct p2p_data *p2p)
1124{
1125 if (p2p->state != P2P_LISTEN_ONLY) {
1126 p2p_dbg(p2p, "Skip stop_listen since not in listen_only state.");
1127 return;
1128 }
1129
1130 p2p_stop_listen_for_freq(p2p, 0);
1131 p2p_set_state(p2p, P2P_IDLE);
1132}
1133
1134
0b8889d8
JM
1135void p2p_stop_find(struct p2p_data *p2p)
1136{
1137 p2p_stop_find_for_freq(p2p, 0);
1138}
1139
1140
e28c226d
JM
1141static int p2p_prepare_channel_pref(struct p2p_data *p2p,
1142 unsigned int force_freq,
51e9f228 1143 unsigned int pref_freq, int go)
e28c226d
JM
1144{
1145 u8 op_class, op_channel;
1146 unsigned int freq = force_freq ? force_freq : pref_freq;
1147
51e9f228
JM
1148 p2p_dbg(p2p, "Prepare channel pref - force_freq=%u pref_freq=%u go=%d",
1149 force_freq, pref_freq, go);
9ccd9165 1150 if (p2p_freq_to_channel(freq, &op_class, &op_channel) < 0) {
ed496f13 1151 p2p_dbg(p2p, "Unsupported frequency %u MHz", freq);
e28c226d
JM
1152 return -1;
1153 }
1154
51e9f228
JM
1155 if (!p2p_channels_includes(&p2p->cfg->channels, op_class, op_channel) &&
1156 (go || !p2p_channels_includes(&p2p->cfg->cli_channels, op_class,
1157 op_channel))) {
ed496f13
JM
1158 p2p_dbg(p2p, "Frequency %u MHz (oper_class %u channel %u) not allowed for P2P",
1159 freq, op_class, op_channel);
e28c226d
JM
1160 return -1;
1161 }
1162
1163 p2p->op_reg_class = op_class;
1164 p2p->op_channel = op_channel;
1165
1166 if (force_freq) {
1167 p2p->channels.reg_classes = 1;
1168 p2p->channels.reg_class[0].channels = 1;
1169 p2p->channels.reg_class[0].reg_class = p2p->op_reg_class;
1170 p2p->channels.reg_class[0].channel[0] = p2p->op_channel;
1171 } else {
1172 os_memcpy(&p2p->channels, &p2p->cfg->channels,
1173 sizeof(struct p2p_channels));
1174 }
1175
1176 return 0;
1177}
1178
1179
1180static void p2p_prepare_channel_best(struct p2p_data *p2p)
1181{
1182 u8 op_class, op_channel;
f2407107 1183 const int op_classes_5ghz[] = { 115, 124, 0 };
e28c226d 1184
99d7c762
JM
1185 p2p_dbg(p2p, "Prepare channel best");
1186
e28c226d
JM
1187 if (!p2p->cfg->cfg_op_channel && p2p->best_freq_overall > 0 &&
1188 p2p_supported_freq(p2p, p2p->best_freq_overall) &&
9ccd9165
JM
1189 p2p_freq_to_channel(p2p->best_freq_overall, &op_class, &op_channel)
1190 == 0) {
ed496f13 1191 p2p_dbg(p2p, "Select best overall channel as operating channel preference");
e28c226d
JM
1192 p2p->op_reg_class = op_class;
1193 p2p->op_channel = op_channel;
1194 } else if (!p2p->cfg->cfg_op_channel && p2p->best_freq_5 > 0 &&
1195 p2p_supported_freq(p2p, p2p->best_freq_5) &&
9ccd9165
JM
1196 p2p_freq_to_channel(p2p->best_freq_5, &op_class, &op_channel)
1197 == 0) {
ed496f13 1198 p2p_dbg(p2p, "Select best 5 GHz channel as operating channel preference");
e28c226d
JM
1199 p2p->op_reg_class = op_class;
1200 p2p->op_channel = op_channel;
1201 } else if (!p2p->cfg->cfg_op_channel && p2p->best_freq_24 > 0 &&
1202 p2p_supported_freq(p2p, p2p->best_freq_24) &&
9ccd9165
JM
1203 p2p_freq_to_channel(p2p->best_freq_24, &op_class,
1204 &op_channel) == 0) {
ed496f13 1205 p2p_dbg(p2p, "Select best 2.4 GHz channel as operating channel preference");
e28c226d
JM
1206 p2p->op_reg_class = op_class;
1207 p2p->op_channel = op_channel;
6701fdc3
JM
1208 } else if (p2p->cfg->num_pref_chan > 0 &&
1209 p2p_channels_includes(&p2p->cfg->channels,
1210 p2p->cfg->pref_chan[0].op_class,
1211 p2p->cfg->pref_chan[0].chan)) {
1212 p2p_dbg(p2p, "Select first pref_chan entry as operating channel preference");
1213 p2p->op_reg_class = p2p->cfg->pref_chan[0].op_class;
1214 p2p->op_channel = p2p->cfg->pref_chan[0].chan;
f2407107
JM
1215 } else if (p2p_channel_select(&p2p->cfg->channels, op_classes_5ghz,
1216 &p2p->op_reg_class, &p2p->op_channel) ==
1217 0) {
1218 p2p_dbg(p2p, "Select possible 5 GHz channel (op_class %u channel %u) as operating channel preference",
1219 p2p->op_reg_class, p2p->op_channel);
e28c226d 1220 } else {
99d7c762 1221 p2p_dbg(p2p, "Select pre-configured channel as operating channel preference");
e28c226d
JM
1222 p2p->op_reg_class = p2p->cfg->op_reg_class;
1223 p2p->op_channel = p2p->cfg->op_channel;
1224 }
1225
1226 os_memcpy(&p2p->channels, &p2p->cfg->channels,
1227 sizeof(struct p2p_channels));
1228}
1229
1230
a5830ede
JM
1231/**
1232 * p2p_prepare_channel - Select operating channel for GO Negotiation
1233 * @p2p: P2P module context from p2p_init()
1234 * @dev: Selected peer device
1235 * @force_freq: Forced frequency in MHz or 0 if not forced
1236 * @pref_freq: Preferred frequency in MHz or 0 if no preference
51e9f228 1237 * @go: Whether the local end will be forced to be GO
a5830ede
JM
1238 * Returns: 0 on success, -1 on failure (channel not supported for P2P)
1239 *
1240 * This function is used to do initial operating channel selection for GO
1241 * Negotiation prior to having received peer information. The selected channel
1242 * may be further optimized in p2p_reselect_channel() once the peer information
1243 * is available.
1244 */
79879f4a 1245int p2p_prepare_channel(struct p2p_data *p2p, struct p2p_device *dev,
51e9f228 1246 unsigned int force_freq, unsigned int pref_freq, int go)
b22128ef 1247{
51e9f228
JM
1248 p2p_dbg(p2p, "Prepare channel - force_freq=%u pref_freq=%u go=%d",
1249 force_freq, pref_freq, go);
04a3e69d 1250 if (force_freq || pref_freq) {
51e9f228
JM
1251 if (p2p_prepare_channel_pref(p2p, force_freq, pref_freq, go) <
1252 0)
1e19f734 1253 return -1;
b22128ef 1254 } else {
e28c226d 1255 p2p_prepare_channel_best(p2p);
b22128ef 1256 }
51e9f228
JM
1257 p2p_channels_dump(p2p, "prepared channels", &p2p->channels);
1258 if (go)
1259 p2p_channels_remove_freqs(&p2p->channels, &p2p->no_go_freq);
1260 else if (!force_freq)
1261 p2p_channels_union(&p2p->channels, &p2p->cfg->cli_channels,
1262 &p2p->channels);
1263 p2p_channels_dump(p2p, "after go/cli filter/add", &p2p->channels);
1264
ed496f13 1265 p2p_dbg(p2p, "Own preference for operation channel: Operating Class %u Channel %u%s",
b22128ef
JM
1266 p2p->op_reg_class, p2p->op_channel,
1267 force_freq ? " (forced)" : "");
1268
92ac756c
JM
1269 if (force_freq)
1270 dev->flags |= P2P_DEV_FORCE_FREQ;
1271 else
1272 dev->flags &= ~P2P_DEV_FORCE_FREQ;
1273
7861cb08
JM
1274 return 0;
1275}
1276
1277
acc247b2
JM
1278static void p2p_set_dev_persistent(struct p2p_device *dev,
1279 int persistent_group)
1280{
1281 switch (persistent_group) {
1282 case 0:
1283 dev->flags &= ~(P2P_DEV_PREFER_PERSISTENT_GROUP |
1284 P2P_DEV_PREFER_PERSISTENT_RECONN);
1285 break;
1286 case 1:
1287 dev->flags |= P2P_DEV_PREFER_PERSISTENT_GROUP;
1288 dev->flags &= ~P2P_DEV_PREFER_PERSISTENT_RECONN;
1289 break;
1290 case 2:
1291 dev->flags |= P2P_DEV_PREFER_PERSISTENT_GROUP |
1292 P2P_DEV_PREFER_PERSISTENT_RECONN;
1293 break;
1294 }
1295}
1296
1297
7861cb08
JM
1298int p2p_connect(struct p2p_data *p2p, const u8 *peer_addr,
1299 enum p2p_wps_method wps_method,
1300 int go_intent, const u8 *own_interface_addr,
23c84252 1301 unsigned int force_freq, int persistent_group,
3bc462cb 1302 const u8 *force_ssid, size_t force_ssid_len,
04a3e69d 1303 int pd_before_go_neg, unsigned int pref_freq)
7861cb08
JM
1304{
1305 struct p2p_device *dev;
1306
ed496f13 1307 p2p_dbg(p2p, "Request to start group negotiation - peer=" MACSTR
7861cb08 1308 " GO Intent=%d Intended Interface Address=" MACSTR
3bc462cb 1309 " wps_method=%d persistent_group=%d pd_before_go_neg=%d",
7861cb08 1310 MAC2STR(peer_addr), go_intent, MAC2STR(own_interface_addr),
3bc462cb 1311 wps_method, persistent_group, pd_before_go_neg);
7861cb08 1312
b22128ef
JM
1313 dev = p2p_get_device(p2p, peer_addr);
1314 if (dev == NULL || (dev->flags & P2P_DEV_PROBE_REQ_ONLY)) {
ed496f13 1315 p2p_dbg(p2p, "Cannot connect to unknown P2P Device " MACSTR,
b22128ef
JM
1316 MAC2STR(peer_addr));
1317 return -1;
1318 }
1319
51e9f228
JM
1320 if (p2p_prepare_channel(p2p, dev, force_freq, pref_freq,
1321 go_intent == 15) < 0)
92ac756c
JM
1322 return -1;
1323
b22128ef 1324 if (dev->flags & P2P_DEV_GROUP_CLIENT_ONLY) {
c5db8e51
KRK
1325 if (!(dev->info.dev_capab &
1326 P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY)) {
ed496f13 1327 p2p_dbg(p2p, "Cannot connect to P2P Device " MACSTR
b22128ef
JM
1328 " that is in a group and is not discoverable",
1329 MAC2STR(peer_addr));
1330 return -1;
1331 }
1332 if (dev->oper_freq <= 0) {
ed496f13 1333 p2p_dbg(p2p, "Cannot connect to P2P Device " MACSTR
b22128ef
JM
1334 " with incomplete information",
1335 MAC2STR(peer_addr));
1336 return -1;
1337 }
1338
1339 /*
1340 * First, try to connect directly. If the peer does not
1341 * acknowledge frames, assume it is sleeping and use device
1342 * discoverability via the GO at that point.
1343 */
1344 }
1345
23c84252
JM
1346 p2p->ssid_set = 0;
1347 if (force_ssid) {
1348 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Forced SSID",
1349 force_ssid, force_ssid_len);
1350 os_memcpy(p2p->ssid, force_ssid, force_ssid_len);
1351 p2p->ssid_len = force_ssid_len;
1352 p2p->ssid_set = 1;
1353 }
1354
b22128ef
JM
1355 dev->flags &= ~P2P_DEV_NOT_YET_READY;
1356 dev->flags &= ~P2P_DEV_USER_REJECTED;
1357 dev->flags &= ~P2P_DEV_WAIT_GO_NEG_RESPONSE;
1358 dev->flags &= ~P2P_DEV_WAIT_GO_NEG_CONFIRM;
3bc462cb
JM
1359 if (pd_before_go_neg)
1360 dev->flags |= P2P_DEV_PD_BEFORE_GO_NEG;
624b4d5a 1361 else {
3bc462cb 1362 dev->flags &= ~P2P_DEV_PD_BEFORE_GO_NEG;
624b4d5a 1363 /*
003c4580
JM
1364 * Assign dialog token and tie breaker here to use the same
1365 * values in each retry within the same GO Negotiation exchange.
624b4d5a
SD
1366 */
1367 dev->dialog_token++;
1368 if (dev->dialog_token == 0)
1369 dev->dialog_token = 1;
003c4580
JM
1370 dev->tie_breaker = p2p->next_tie_breaker;
1371 p2p->next_tie_breaker = !p2p->next_tie_breaker;
624b4d5a 1372 }
9dac8c3e 1373 dev->connect_reqs = 0;
b22128ef
JM
1374 dev->go_neg_req_sent = 0;
1375 dev->go_state = UNKNOWN_GO;
acc247b2 1376 p2p_set_dev_persistent(dev, persistent_group);
b22128ef
JM
1377 p2p->go_intent = go_intent;
1378 os_memcpy(p2p->intended_addr, own_interface_addr, ETH_ALEN);
1379
1380 if (p2p->state != P2P_IDLE)
1381 p2p_stop_find(p2p);
1382
f44ae207
JM
1383 if (p2p->after_scan_tx) {
1384 /*
1385 * We need to drop the pending frame to avoid issues with the
1386 * new GO Negotiation, e.g., when the pending frame was from a
1387 * previous attempt at starting a GO Negotiation.
1388 */
ed496f13 1389 p2p_dbg(p2p, "Dropped previous pending Action frame TX that was waiting for p2p_scan completion");
f44ae207
JM
1390 os_free(p2p->after_scan_tx);
1391 p2p->after_scan_tx = NULL;
1392 }
1393
b22128ef
JM
1394 dev->wps_method = wps_method;
1395 dev->status = P2P_SC_SUCCESS;
d5b20a73 1396
b22128ef 1397 if (p2p->p2p_scan_running) {
ed496f13 1398 p2p_dbg(p2p, "p2p_scan running - delay connect send");
b22128ef
JM
1399 p2p->start_after_scan = P2P_AFTER_SCAN_CONNECT;
1400 os_memcpy(p2p->after_scan_peer, peer_addr, ETH_ALEN);
1401 return 0;
1402 }
1403 p2p->start_after_scan = P2P_AFTER_SCAN_NOTHING;
1404
1405 return p2p_connect_send(p2p, dev);
1406}
1407
1408
1409int p2p_authorize(struct p2p_data *p2p, const u8 *peer_addr,
1410 enum p2p_wps_method wps_method,
1411 int go_intent, const u8 *own_interface_addr,
23c84252 1412 unsigned int force_freq, int persistent_group,
04a3e69d
JM
1413 const u8 *force_ssid, size_t force_ssid_len,
1414 unsigned int pref_freq)
b22128ef
JM
1415{
1416 struct p2p_device *dev;
1417
ed496f13 1418 p2p_dbg(p2p, "Request to authorize group negotiation - peer=" MACSTR
b22128ef
JM
1419 " GO Intent=%d Intended Interface Address=" MACSTR
1420 " wps_method=%d persistent_group=%d",
1421 MAC2STR(peer_addr), go_intent, MAC2STR(own_interface_addr),
1422 wps_method, persistent_group);
1423
b22128ef
JM
1424 dev = p2p_get_device(p2p, peer_addr);
1425 if (dev == NULL) {
ed496f13 1426 p2p_dbg(p2p, "Cannot authorize unknown P2P Device " MACSTR,
b22128ef
JM
1427 MAC2STR(peer_addr));
1428 return -1;
1429 }
1430
51e9f228
JM
1431 if (p2p_prepare_channel(p2p, dev, force_freq, pref_freq, go_intent ==
1432 15) < 0)
92ac756c
JM
1433 return -1;
1434
23c84252
JM
1435 p2p->ssid_set = 0;
1436 if (force_ssid) {
1437 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Forced SSID",
1438 force_ssid, force_ssid_len);
1439 os_memcpy(p2p->ssid, force_ssid, force_ssid_len);
1440 p2p->ssid_len = force_ssid_len;
1441 p2p->ssid_set = 1;
1442 }
1443
b22128ef
JM
1444 dev->flags &= ~P2P_DEV_NOT_YET_READY;
1445 dev->flags &= ~P2P_DEV_USER_REJECTED;
1446 dev->go_neg_req_sent = 0;
1447 dev->go_state = UNKNOWN_GO;
acc247b2 1448 p2p_set_dev_persistent(dev, persistent_group);
b22128ef
JM
1449 p2p->go_intent = go_intent;
1450 os_memcpy(p2p->intended_addr, own_interface_addr, ETH_ALEN);
1451
1452 dev->wps_method = wps_method;
1453 dev->status = P2P_SC_SUCCESS;
1454
1455 return 0;
1456}
1457
1458
1459void p2p_add_dev_info(struct p2p_data *p2p, const u8 *addr,
1460 struct p2p_device *dev, struct p2p_message *msg)
1461{
1462 os_get_time(&dev->last_seen);
1463
ed496f13 1464 p2p_copy_wps_info(p2p, dev, 0, msg);
e57ae6e1 1465
b22128ef
JM
1466 if (msg->listen_channel) {
1467 int freq;
9ccd9165 1468 freq = p2p_channel_to_freq(msg->listen_channel[3],
b22128ef
JM
1469 msg->listen_channel[4]);
1470 if (freq < 0) {
ed496f13 1471 p2p_dbg(p2p, "Unknown peer Listen channel: "
b22128ef
JM
1472 "country=%c%c(0x%02x) reg_class=%u channel=%u",
1473 msg->listen_channel[0],
1474 msg->listen_channel[1],
1475 msg->listen_channel[2],
1476 msg->listen_channel[3],
1477 msg->listen_channel[4]);
1478 } else {
ed496f13
JM
1479 p2p_dbg(p2p, "Update peer " MACSTR
1480 " Listen channel: %u -> %u MHz",
c5db8e51 1481 MAC2STR(dev->info.p2p_device_addr),
b22128ef
JM
1482 dev->listen_freq, freq);
1483 dev->listen_freq = freq;
1484 }
1485 }
b22128ef 1486
9675ce35
JM
1487 if (msg->wfd_subelems) {
1488 wpabuf_free(dev->info.wfd_subelems);
1489 dev->info.wfd_subelems = wpabuf_dup(msg->wfd_subelems);
1490 }
1491
b22128ef
JM
1492 if (dev->flags & P2P_DEV_PROBE_REQ_ONLY) {
1493 dev->flags &= ~P2P_DEV_PROBE_REQ_ONLY;
ed496f13 1494 p2p_dbg(p2p, "Completed device entry based on data from GO Negotiation Request");
b22128ef 1495 } else {
ed496f13 1496 p2p_dbg(p2p, "Created device entry based on GO Neg Req: "
b22128ef
JM
1497 MACSTR " dev_capab=0x%x group_capab=0x%x name='%s' "
1498 "listen_freq=%d",
c5db8e51
KRK
1499 MAC2STR(dev->info.p2p_device_addr),
1500 dev->info.dev_capab, dev->info.group_capab,
1501 dev->info.device_name, dev->listen_freq);
b22128ef
JM
1502 }
1503
1504 dev->flags &= ~P2P_DEV_GROUP_CLIENT_ONLY;
1505
1506 if (dev->flags & P2P_DEV_USER_REJECTED) {
ed496f13 1507 p2p_dbg(p2p, "Do not report rejected device");
b22128ef
JM
1508 return;
1509 }
1510
8fd7dc1b
JB
1511 p2p->cfg->dev_found(p2p->cfg->cb_ctx, addr, &dev->info,
1512 !(dev->flags & P2P_DEV_REPORTED_ONCE));
1513 dev->flags |= P2P_DEV_REPORTED | P2P_DEV_REPORTED_ONCE;
b22128ef
JM
1514}
1515
1516
1517void p2p_build_ssid(struct p2p_data *p2p, u8 *ssid, size_t *ssid_len)
1518{
1519 os_memcpy(ssid, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN);
1520 p2p_random((char *) &ssid[P2P_WILDCARD_SSID_LEN], 2);
1521 os_memcpy(&ssid[P2P_WILDCARD_SSID_LEN + 2],
1522 p2p->cfg->ssid_postfix, p2p->cfg->ssid_postfix_len);
1523 *ssid_len = P2P_WILDCARD_SSID_LEN + 2 + p2p->cfg->ssid_postfix_len;
1524}
1525
1526
1527int p2p_go_params(struct p2p_data *p2p, struct p2p_go_neg_results *params)
1528{
1529 p2p_build_ssid(p2p, params->ssid, &params->ssid_len);
1530 p2p_random(params->passphrase, 8);
1531 return 0;
1532}
1533
1534
1535void p2p_go_complete(struct p2p_data *p2p, struct p2p_device *peer)
1536{
1537 struct p2p_go_neg_results res;
1538 int go = peer->go_state == LOCAL_GO;
1539 struct p2p_channels intersection;
1540 int freqs;
1541 size_t i, j;
1542
ed496f13
JM
1543 p2p_dbg(p2p, "GO Negotiation with " MACSTR " completed (%s will be GO)",
1544 MAC2STR(peer->info.p2p_device_addr), go ? "local end" : "peer");
b22128ef
JM
1545
1546 os_memset(&res, 0, sizeof(res));
1547 res.role_go = go;
c5db8e51 1548 os_memcpy(res.peer_device_addr, peer->info.p2p_device_addr, ETH_ALEN);
b22128ef
JM
1549 os_memcpy(res.peer_interface_addr, peer->intended_addr, ETH_ALEN);
1550 res.wps_method = peer->wps_method;
acc247b2
JM
1551 if (peer->flags & P2P_DEV_PREFER_PERSISTENT_GROUP) {
1552 if (peer->flags & P2P_DEV_PREFER_PERSISTENT_RECONN)
1553 res.persistent_group = 2;
1554 else
1555 res.persistent_group = 1;
1556 }
b22128ef
JM
1557
1558 if (go) {
1559 /* Setup AP mode for WPS provisioning */
9ccd9165 1560 res.freq = p2p_channel_to_freq(p2p->op_reg_class,
b22128ef
JM
1561 p2p->op_channel);
1562 os_memcpy(res.ssid, p2p->ssid, p2p->ssid_len);
1563 res.ssid_len = p2p->ssid_len;
1564 p2p_random(res.passphrase, 8);
e9a7ae41 1565 } else {
b22128ef 1566 res.freq = peer->oper_freq;
e9a7ae41
JM
1567 if (p2p->ssid_len) {
1568 os_memcpy(res.ssid, p2p->ssid, p2p->ssid_len);
1569 res.ssid_len = p2p->ssid_len;
1570 }
1571 }
b22128ef 1572
556b30da
JM
1573 p2p_channels_dump(p2p, "own channels", &p2p->channels);
1574 p2p_channels_dump(p2p, "peer channels", &peer->channels);
b22128ef
JM
1575 p2p_channels_intersect(&p2p->channels, &peer->channels,
1576 &intersection);
556b30da
JM
1577 if (go) {
1578 p2p_channels_remove_freqs(&intersection, &p2p->no_go_freq);
1579 p2p_channels_dump(p2p, "intersection after no-GO removal",
1580 &intersection);
1581 }
b22128ef
JM
1582 freqs = 0;
1583 for (i = 0; i < intersection.reg_classes; i++) {
1584 struct p2p_reg_class *c = &intersection.reg_class[i];
1585 if (freqs + 1 == P2P_MAX_CHANNELS)
1586 break;
1587 for (j = 0; j < c->channels; j++) {
1588 int freq;
1589 if (freqs + 1 == P2P_MAX_CHANNELS)
1590 break;
9ccd9165 1591 freq = p2p_channel_to_freq(c->reg_class, c->channel[j]);
b22128ef
JM
1592 if (freq < 0)
1593 continue;
1594 res.freq_list[freqs++] = freq;
1595 }
1596 }
1597
ae3e3421
JM
1598 res.peer_config_timeout = go ? peer->client_timeout : peer->go_timeout;
1599
b22128ef 1600 p2p_clear_timeout(p2p);
4458d915 1601 p2p->ssid_set = 0;
b22128ef
JM
1602 peer->go_neg_req_sent = 0;
1603 peer->wps_method = WPS_NOT_READY;
1604
1605 p2p_set_state(p2p, P2P_PROVISIONING);
1606 p2p->cfg->go_neg_completed(p2p->cfg->cb_ctx, &res);
1607}
1608
1609
1610static void p2p_rx_p2p_action(struct p2p_data *p2p, const u8 *sa,
1611 const u8 *data, size_t len, int rx_freq)
1612{
ed496f13 1613 p2p_dbg(p2p, "RX P2P Public Action from " MACSTR, MAC2STR(sa));
b22128ef
JM
1614 wpa_hexdump(MSG_MSGDUMP, "P2P: P2P Public Action contents", data, len);
1615
1616 if (len < 1)
1617 return;
1618
1619 switch (data[0]) {
1620 case P2P_GO_NEG_REQ:
1621 p2p_process_go_neg_req(p2p, sa, data + 1, len - 1, rx_freq);
1622 break;
1623 case P2P_GO_NEG_RESP:
1624 p2p_process_go_neg_resp(p2p, sa, data + 1, len - 1, rx_freq);
1625 break;
1626 case P2P_GO_NEG_CONF:
1627 p2p_process_go_neg_conf(p2p, sa, data + 1, len - 1);
1628 break;
1629 case P2P_INVITATION_REQ:
1630 p2p_process_invitation_req(p2p, sa, data + 1, len - 1,
1631 rx_freq);
1632 break;
1633 case P2P_INVITATION_RESP:
dc46fd66 1634 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
b22128ef
JM
1635 p2p_process_invitation_resp(p2p, sa, data + 1, len - 1);
1636 break;
1637 case P2P_PROV_DISC_REQ:
1638 p2p_process_prov_disc_req(p2p, sa, data + 1, len - 1, rx_freq);
1639 break;
1640 case P2P_PROV_DISC_RESP:
1641 p2p_process_prov_disc_resp(p2p, sa, data + 1, len - 1);
1642 break;
1643 case P2P_DEV_DISC_REQ:
1644 p2p_process_dev_disc_req(p2p, sa, data + 1, len - 1, rx_freq);
1645 break;
1646 case P2P_DEV_DISC_RESP:
1647 p2p_process_dev_disc_resp(p2p, sa, data + 1, len - 1);
1648 break;
1649 default:
ed496f13 1650 p2p_dbg(p2p, "Unsupported P2P Public Action frame type %d",
b22128ef
JM
1651 data[0]);
1652 break;
1653 }
1654}
1655
1656
19df9b07
JM
1657static void p2p_rx_action_public(struct p2p_data *p2p, const u8 *da,
1658 const u8 *sa, const u8 *bssid, const u8 *data,
1659 size_t len, int freq)
b22128ef
JM
1660{
1661 if (len < 1)
1662 return;
1663
1664 switch (data[0]) {
1665 case WLAN_PA_VENDOR_SPECIFIC:
1666 data++;
1667 len--;
1668 if (len < 3)
1669 return;
1670 if (WPA_GET_BE24(data) != OUI_WFA)
1671 return;
1672
1673 data += 3;
1674 len -= 3;
1675 if (len < 1)
1676 return;
1677
1678 if (*data != P2P_OUI_TYPE)
1679 return;
1680
1681 p2p_rx_p2p_action(p2p, sa, data + 1, len - 1, freq);
1682 break;
1683 case WLAN_PA_GAS_INITIAL_REQ:
1684 p2p_rx_gas_initial_req(p2p, sa, data + 1, len - 1, freq);
1685 break;
1686 case WLAN_PA_GAS_INITIAL_RESP:
18708aad
JM
1687 p2p_rx_gas_initial_resp(p2p, sa, data + 1, len - 1, freq);
1688 break;
1689 case WLAN_PA_GAS_COMEBACK_REQ:
1690 p2p_rx_gas_comeback_req(p2p, sa, data + 1, len - 1, freq);
1691 break;
1692 case WLAN_PA_GAS_COMEBACK_RESP:
1693 p2p_rx_gas_comeback_resp(p2p, sa, data + 1, len - 1, freq);
b22128ef
JM
1694 break;
1695 }
1696}
1697
1698
1699void p2p_rx_action(struct p2p_data *p2p, const u8 *da, const u8 *sa,
1700 const u8 *bssid, u8 category,
1701 const u8 *data, size_t len, int freq)
1702{
1703 if (category == WLAN_ACTION_PUBLIC) {
1704 p2p_rx_action_public(p2p, da, sa, bssid, data, len, freq);
1705 return;
1706 }
1707
1708 if (category != WLAN_ACTION_VENDOR_SPECIFIC)
1709 return;
1710
1711 if (len < 4)
1712 return;
1713
1714 if (WPA_GET_BE24(data) != OUI_WFA)
1715 return;
1716 data += 3;
1717 len -= 3;
1718
1719 if (*data != P2P_OUI_TYPE)
1720 return;
1721 data++;
1722 len--;
1723
1724 /* P2P action frame */
ed496f13 1725 p2p_dbg(p2p, "RX P2P Action from " MACSTR, MAC2STR(sa));
b22128ef
JM
1726 wpa_hexdump(MSG_MSGDUMP, "P2P: P2P Action contents", data, len);
1727
1728 if (len < 1)
1729 return;
1730 switch (data[0]) {
1731 case P2P_NOA:
ed496f13 1732 p2p_dbg(p2p, "Received P2P Action - Notice of Absence");
b22128ef
JM
1733 /* TODO */
1734 break;
1735 case P2P_PRESENCE_REQ:
1736 p2p_process_presence_req(p2p, da, sa, data + 1, len - 1, freq);
1737 break;
1738 case P2P_PRESENCE_RESP:
1739 p2p_process_presence_resp(p2p, da, sa, data + 1, len - 1);
1740 break;
1741 case P2P_GO_DISC_REQ:
1742 p2p_process_go_disc_req(p2p, da, sa, data + 1, len - 1, freq);
1743 break;
1744 default:
ed496f13 1745 p2p_dbg(p2p, "Received P2P Action - unknown type %u", data[0]);
b22128ef
JM
1746 break;
1747 }
1748}
1749
1750
1751static void p2p_go_neg_start(void *eloop_ctx, void *timeout_ctx)
1752{
1753 struct p2p_data *p2p = eloop_ctx;
1754 if (p2p->go_neg_peer == NULL)
1755 return;
1756 p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
1757 p2p->go_neg_peer->status = P2P_SC_SUCCESS;
1758 p2p_connect_send(p2p, p2p->go_neg_peer);
1759}
1760
1761
1762static void p2p_invite_start(void *eloop_ctx, void *timeout_ctx)
1763{
1764 struct p2p_data *p2p = eloop_ctx;
1765 if (p2p->invite_peer == NULL)
1766 return;
1767 p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
1768 p2p_invite_send(p2p, p2p->invite_peer, p2p->invite_go_dev_addr);
1769}
1770
1771
1772static void p2p_add_dev_from_probe_req(struct p2p_data *p2p, const u8 *addr,
1773 const u8 *ie, size_t ie_len)
1774{
1775 struct p2p_message msg;
1776 struct p2p_device *dev;
1777
1778 os_memset(&msg, 0, sizeof(msg));
1779 if (p2p_parse_ies(ie, ie_len, &msg) < 0 || msg.p2p_attributes == NULL)
1780 {
1781 p2p_parse_free(&msg);
1782 return; /* not a P2P probe */
1783 }
1784
1785 if (msg.ssid == NULL || msg.ssid[1] != P2P_WILDCARD_SSID_LEN ||
1786 os_memcmp(msg.ssid + 2, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN)
1787 != 0) {
1788 /* The Probe Request is not part of P2P Device Discovery. It is
1789 * not known whether the source address of the frame is the P2P
1790 * Device Address or P2P Interface Address. Do not add a new
1791 * peer entry based on this frames.
1792 */
1793 p2p_parse_free(&msg);
1794 return;
1795 }
1796
1797 dev = p2p_get_device(p2p, addr);
1798 if (dev) {
1799 if (dev->country[0] == 0 && msg.listen_channel)
1800 os_memcpy(dev->country, msg.listen_channel, 3);
ed908a55 1801 os_get_time(&dev->last_seen);
b22128ef
JM
1802 p2p_parse_free(&msg);
1803 return; /* already known */
1804 }
1805
1806 dev = p2p_create_device(p2p, addr);
1807 if (dev == NULL) {
1808 p2p_parse_free(&msg);
1809 return;
1810 }
1811
1812 os_get_time(&dev->last_seen);
1813 dev->flags |= P2P_DEV_PROBE_REQ_ONLY;
1814
b22128ef
JM
1815 if (msg.listen_channel) {
1816 os_memcpy(dev->country, msg.listen_channel, 3);
9ccd9165 1817 dev->listen_freq = p2p_channel_to_freq(msg.listen_channel[3],
b22128ef
JM
1818 msg.listen_channel[4]);
1819 }
1820
ed496f13 1821 p2p_copy_wps_info(p2p, dev, 1, &msg);
e57ae6e1 1822
9675ce35
JM
1823 if (msg.wfd_subelems) {
1824 wpabuf_free(dev->info.wfd_subelems);
1825 dev->info.wfd_subelems = wpabuf_dup(msg.wfd_subelems);
1826 }
1827
b22128ef
JM
1828 p2p_parse_free(&msg);
1829
ed496f13 1830 p2p_dbg(p2p, "Created device entry based on Probe Req: " MACSTR
b22128ef 1831 " dev_capab=0x%x group_capab=0x%x name='%s' listen_freq=%d",
c5db8e51
KRK
1832 MAC2STR(dev->info.p2p_device_addr), dev->info.dev_capab,
1833 dev->info.group_capab, dev->info.device_name,
1834 dev->listen_freq);
b22128ef
JM
1835}
1836
1837
1838struct p2p_device * p2p_add_dev_from_go_neg_req(struct p2p_data *p2p,
1839 const u8 *addr,
1840 struct p2p_message *msg)
1841{
1842 struct p2p_device *dev;
1843
1844 dev = p2p_get_device(p2p, addr);
1845 if (dev) {
1846 os_get_time(&dev->last_seen);
1847 return dev; /* already known */
1848 }
1849
1850 dev = p2p_create_device(p2p, addr);
1851 if (dev == NULL)
1852 return NULL;
1853
1854 p2p_add_dev_info(p2p, addr, dev, msg);
1855
1856 return dev;
1857}
1858
1859
1860static int dev_type_match(const u8 *dev_type, const u8 *req_dev_type)
1861{
1862 if (os_memcmp(dev_type, req_dev_type, WPS_DEV_TYPE_LEN) == 0)
1863 return 1;
1864 if (os_memcmp(dev_type, req_dev_type, 2) == 0 &&
1865 WPA_GET_BE32(&req_dev_type[2]) == 0 &&
1866 WPA_GET_BE16(&req_dev_type[6]) == 0)
1867 return 1; /* Category match with wildcard OUI/sub-category */
1868 return 0;
1869}
1870
1871
1872int dev_type_list_match(const u8 *dev_type, const u8 *req_dev_type[],
1873 size_t num_req_dev_type)
1874{
1875 size_t i;
1876 for (i = 0; i < num_req_dev_type; i++) {
1877 if (dev_type_match(dev_type, req_dev_type[i]))
1878 return 1;
1879 }
1880 return 0;
1881}
1882
1883
1884/**
1885 * p2p_match_dev_type - Match local device type with requested type
1886 * @p2p: P2P module context from p2p_init()
1887 * @wps: WPS TLVs from Probe Request frame (concatenated WPS IEs)
1888 * Returns: 1 on match, 0 on mismatch
1889 *
1890 * This function can be used to match the Requested Device Type attribute in
1891 * WPS IE with the local device types for deciding whether to reply to a Probe
1892 * Request frame.
1893 */
1894int p2p_match_dev_type(struct p2p_data *p2p, struct wpabuf *wps)
1895{
1896 struct wps_parse_attr attr;
1897 size_t i;
1898
1899 if (wps_parse_msg(wps, &attr))
1900 return 1; /* assume no Requested Device Type attributes */
1901
1902 if (attr.num_req_dev_type == 0)
1903 return 1; /* no Requested Device Type attributes -> match */
1904
1905 if (dev_type_list_match(p2p->cfg->pri_dev_type, attr.req_dev_type,
1906 attr.num_req_dev_type))
1907 return 1; /* Own Primary Device Type matches */
1908
1909 for (i = 0; i < p2p->cfg->num_sec_dev_types; i++)
1910 if (dev_type_list_match(p2p->cfg->sec_dev_type[i],
1911 attr.req_dev_type,
1912 attr.num_req_dev_type))
1913 return 1; /* Own Secondary Device Type matches */
1914
1915 /* No matching device type found */
1916 return 0;
1917}
1918
1919
1920struct wpabuf * p2p_build_probe_resp_ies(struct p2p_data *p2p)
1921{
1922 struct wpabuf *buf;
1923 u8 *len;
360182ed 1924 int pw_id = -1;
9675ce35 1925 size_t extra = 0;
b22128ef 1926
9675ce35
JM
1927#ifdef CONFIG_WIFI_DISPLAY
1928 if (p2p->wfd_ie_probe_resp)
1929 extra = wpabuf_len(p2p->wfd_ie_probe_resp);
1930#endif /* CONFIG_WIFI_DISPLAY */
1931
1932 buf = wpabuf_alloc(1000 + extra);
b22128ef
JM
1933 if (buf == NULL)
1934 return NULL;
1935
360182ed
JM
1936 if (p2p->go_neg_peer) {
1937 /* Advertise immediate availability of WPS credential */
1938 pw_id = p2p_wps_method_pw_id(p2p->go_neg_peer->wps_method);
1939 }
1940
33183761
JM
1941 if (p2p_build_wps_ie(p2p, buf, pw_id, 1) < 0) {
1942 p2p_dbg(p2p, "Failed to build WPS IE for Probe Response");
1943 wpabuf_free(buf);
1944 return NULL;
1945 }
b22128ef 1946
9675ce35
JM
1947#ifdef CONFIG_WIFI_DISPLAY
1948 if (p2p->wfd_ie_probe_resp)
1949 wpabuf_put_buf(buf, p2p->wfd_ie_probe_resp);
1950#endif /* CONFIG_WIFI_DISPLAY */
1951
b22128ef
JM
1952 /* P2P IE */
1953 len = p2p_buf_add_ie_hdr(buf);
18485b54
MH
1954 p2p_buf_add_capability(buf, p2p->dev_capab &
1955 ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY, 0);
b22128ef
JM
1956 if (p2p->ext_listen_interval)
1957 p2p_buf_add_ext_listen_timing(buf, p2p->ext_listen_period,
1958 p2p->ext_listen_interval);
1959 p2p_buf_add_device_info(buf, p2p, NULL);
1960 p2p_buf_update_ie_hdr(buf, len);
1961
1962 return buf;
1963}
1964
1965
2d43d37f
JB
1966static enum p2p_probe_req_status
1967p2p_reply_probe(struct p2p_data *p2p, const u8 *addr, const u8 *dst,
1968 const u8 *bssid, const u8 *ie, size_t ie_len)
b22128ef
JM
1969{
1970 struct ieee802_11_elems elems;
1971 struct wpabuf *buf;
1972 struct ieee80211_mgmt *resp;
97c5b3c4 1973 struct p2p_message msg;
b22128ef
JM
1974 struct wpabuf *ies;
1975
1976 if (!p2p->in_listen || !p2p->drv_in_listen) {
1977 /* not in Listen state - ignore Probe Request */
2d43d37f 1978 return P2P_PREQ_NOT_LISTEN;
b22128ef
JM
1979 }
1980
1981 if (ieee802_11_parse_elems((u8 *) ie, ie_len, &elems, 0) ==
1982 ParseFailed) {
1983 /* Ignore invalid Probe Request frames */
2d43d37f 1984 return P2P_PREQ_MALFORMED;
b22128ef
JM
1985 }
1986
1987 if (elems.p2p == NULL) {
1988 /* not a P2P probe - ignore it */
2d43d37f 1989 return P2P_PREQ_NOT_P2P;
b22128ef
JM
1990 }
1991
04a85e44
JM
1992 if (dst && !is_broadcast_ether_addr(dst) &&
1993 os_memcmp(dst, p2p->cfg->dev_addr, ETH_ALEN) != 0) {
1994 /* Not sent to the broadcast address or our P2P Device Address
1995 */
2d43d37f 1996 return P2P_PREQ_NOT_PROCESSED;
04a85e44
JM
1997 }
1998
1999 if (bssid && !is_broadcast_ether_addr(bssid)) {
2000 /* Not sent to the Wildcard BSSID */
2d43d37f 2001 return P2P_PREQ_NOT_PROCESSED;
04a85e44
JM
2002 }
2003
b22128ef
JM
2004 if (elems.ssid == NULL || elems.ssid_len != P2P_WILDCARD_SSID_LEN ||
2005 os_memcmp(elems.ssid, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN) !=
2006 0) {
2007 /* not using P2P Wildcard SSID - ignore */
2d43d37f 2008 return P2P_PREQ_NOT_PROCESSED;
b22128ef
JM
2009 }
2010
e1d52629
JM
2011 if (supp_rates_11b_only(&elems)) {
2012 /* Indicates support for 11b rates only */
2d43d37f 2013 return P2P_PREQ_NOT_P2P;
e1d52629
JM
2014 }
2015
97c5b3c4
JM
2016 os_memset(&msg, 0, sizeof(msg));
2017 if (p2p_parse_ies(ie, ie_len, &msg) < 0) {
2018 /* Could not parse P2P attributes */
2d43d37f 2019 return P2P_PREQ_NOT_P2P;
97c5b3c4
JM
2020 }
2021
2022 if (msg.device_id &&
1c7447d0 2023 os_memcmp(msg.device_id, p2p->cfg->dev_addr, ETH_ALEN) != 0) {
97c5b3c4
JM
2024 /* Device ID did not match */
2025 p2p_parse_free(&msg);
2d43d37f 2026 return P2P_PREQ_NOT_PROCESSED;
97c5b3c4
JM
2027 }
2028
b22128ef 2029 /* Check Requested Device Type match */
97c5b3c4
JM
2030 if (msg.wps_attributes &&
2031 !p2p_match_dev_type(p2p, msg.wps_attributes)) {
b22128ef 2032 /* No match with Requested Device Type */
97c5b3c4 2033 p2p_parse_free(&msg);
2d43d37f 2034 return P2P_PREQ_NOT_PROCESSED;
b22128ef 2035 }
97c5b3c4 2036 p2p_parse_free(&msg);
b22128ef 2037
2d43d37f
JB
2038 if (!p2p->cfg->send_probe_resp) {
2039 /* Response generated elsewhere */
2040 return P2P_PREQ_NOT_PROCESSED;
2041 }
b22128ef 2042
ed496f13 2043 p2p_dbg(p2p, "Reply to P2P Probe Request in Listen state");
b22128ef
JM
2044
2045 /*
2046 * We do not really have a specific BSS that this frame is advertising,
2047 * so build a frame that has some information in valid format. This is
2048 * really only used for discovery purposes, not to learn exact BSS
2049 * parameters.
2050 */
2051 ies = p2p_build_probe_resp_ies(p2p);
2052 if (ies == NULL)
2d43d37f 2053 return P2P_PREQ_NOT_PROCESSED;
b22128ef
JM
2054
2055 buf = wpabuf_alloc(200 + wpabuf_len(ies));
2056 if (buf == NULL) {
2057 wpabuf_free(ies);
2d43d37f 2058 return P2P_PREQ_NOT_PROCESSED;
b22128ef
JM
2059 }
2060
2061 resp = NULL;
2062 resp = wpabuf_put(buf, resp->u.probe_resp.variable - (u8 *) resp);
2063
2064 resp->frame_control = host_to_le16((WLAN_FC_TYPE_MGMT << 2) |
2065 (WLAN_FC_STYPE_PROBE_RESP << 4));
2066 os_memcpy(resp->da, addr, ETH_ALEN);
2067 os_memcpy(resp->sa, p2p->cfg->dev_addr, ETH_ALEN);
2068 os_memcpy(resp->bssid, p2p->cfg->dev_addr, ETH_ALEN);
2069 resp->u.probe_resp.beacon_int = host_to_le16(100);
2070 /* hardware or low-level driver will setup seq_ctrl and timestamp */
2071 resp->u.probe_resp.capab_info =
2072 host_to_le16(WLAN_CAPABILITY_SHORT_PREAMBLE |
2073 WLAN_CAPABILITY_PRIVACY |
2074 WLAN_CAPABILITY_SHORT_SLOT_TIME);
2075
2076 wpabuf_put_u8(buf, WLAN_EID_SSID);
2077 wpabuf_put_u8(buf, P2P_WILDCARD_SSID_LEN);
2078 wpabuf_put_data(buf, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN);
2079
2080 wpabuf_put_u8(buf, WLAN_EID_SUPP_RATES);
2081 wpabuf_put_u8(buf, 8);
2082 wpabuf_put_u8(buf, (60 / 5) | 0x80);
2083 wpabuf_put_u8(buf, 90 / 5);
2084 wpabuf_put_u8(buf, (120 / 5) | 0x80);
2085 wpabuf_put_u8(buf, 180 / 5);
2086 wpabuf_put_u8(buf, (240 / 5) | 0x80);
2087 wpabuf_put_u8(buf, 360 / 5);
2088 wpabuf_put_u8(buf, 480 / 5);
2089 wpabuf_put_u8(buf, 540 / 5);
2090
2091 wpabuf_put_u8(buf, WLAN_EID_DS_PARAMS);
2092 wpabuf_put_u8(buf, 1);
2093 wpabuf_put_u8(buf, p2p->cfg->channel);
2094
2095 wpabuf_put_buf(buf, ies);
2096 wpabuf_free(ies);
2097
2098 p2p->cfg->send_probe_resp(p2p->cfg->cb_ctx, buf);
2099
2100 wpabuf_free(buf);
2d43d37f
JB
2101
2102 return P2P_PREQ_NOT_PROCESSED;
b22128ef
JM
2103}
2104
2105
2d43d37f
JB
2106enum p2p_probe_req_status
2107p2p_probe_req_rx(struct p2p_data *p2p, const u8 *addr, const u8 *dst,
2108 const u8 *bssid, const u8 *ie, size_t ie_len)
b22128ef 2109{
2d43d37f
JB
2110 enum p2p_probe_req_status res;
2111
b22128ef
JM
2112 p2p_add_dev_from_probe_req(p2p, addr, ie, ie_len);
2113
2d43d37f 2114 res = p2p_reply_probe(p2p, addr, dst, bssid, ie, ie_len);
b22128ef
JM
2115
2116 if ((p2p->state == P2P_CONNECT || p2p->state == P2P_CONNECT_LISTEN) &&
2117 p2p->go_neg_peer &&
c5db8e51 2118 os_memcmp(addr, p2p->go_neg_peer->info.p2p_device_addr, ETH_ALEN)
c03e2113
JM
2119 == 0 &&
2120 !(p2p->go_neg_peer->flags & P2P_DEV_WAIT_GO_NEG_CONFIRM)) {
b22128ef 2121 /* Received a Probe Request from GO Negotiation peer */
ed496f13 2122 p2p_dbg(p2p, "Found GO Negotiation peer - try to start GO negotiation from timeout");
8cee87ab 2123 eloop_cancel_timeout(p2p_go_neg_start, p2p, NULL);
b22128ef 2124 eloop_register_timeout(0, 0, p2p_go_neg_start, p2p, NULL);
2d43d37f 2125 return P2P_PREQ_PROCESSED;
b22128ef
JM
2126 }
2127
2128 if ((p2p->state == P2P_INVITE || p2p->state == P2P_INVITE_LISTEN) &&
2129 p2p->invite_peer &&
c5db8e51
KRK
2130 os_memcmp(addr, p2p->invite_peer->info.p2p_device_addr, ETH_ALEN)
2131 == 0) {
b22128ef 2132 /* Received a Probe Request from Invite peer */
ed496f13 2133 p2p_dbg(p2p, "Found Invite peer - try to start Invite from timeout");
b22128ef 2134 eloop_register_timeout(0, 0, p2p_invite_start, p2p, NULL);
2d43d37f 2135 return P2P_PREQ_PROCESSED;
b22128ef
JM
2136 }
2137
2d43d37f 2138 return res;
b22128ef
JM
2139}
2140
2141
2142static int p2p_assoc_req_ie_wlan_ap(struct p2p_data *p2p, const u8 *bssid,
4c08c0bd 2143 u8 *buf, size_t len, struct wpabuf *p2p_ie)
b22128ef
JM
2144{
2145 struct wpabuf *tmp;
2146 u8 *lpos;
2147 size_t tmplen;
2148 int res;
72044390 2149 u8 group_capab;
b22128ef 2150
4c08c0bd
JM
2151 if (p2p_ie == NULL)
2152 return 0; /* WLAN AP is not a P2P manager */
b22128ef
JM
2153
2154 /*
2155 * (Re)Association Request - P2P IE
2156 * P2P Capability attribute (shall be present)
4c08c0bd
JM
2157 * P2P Interface attribute (present if concurrent device and
2158 * P2P Management is enabled)
b22128ef
JM
2159 */
2160 tmp = wpabuf_alloc(200);
2161 if (tmp == NULL)
2162 return -1;
2163
2164 lpos = p2p_buf_add_ie_hdr(tmp);
72044390
JM
2165 group_capab = 0;
2166 if (p2p->num_groups > 0) {
2167 group_capab |= P2P_GROUP_CAPAB_GROUP_OWNER;
2168 if ((p2p->dev_capab & P2P_DEV_CAPAB_CONCURRENT_OPER) &&
2169 (p2p->dev_capab & P2P_DEV_CAPAB_INFRA_MANAGED) &&
2170 p2p->cross_connect)
2171 group_capab |= P2P_GROUP_CAPAB_CROSS_CONN;
2172 }
2173 p2p_buf_add_capability(tmp, p2p->dev_capab, group_capab);
4c08c0bd
JM
2174 if ((p2p->dev_capab & P2P_DEV_CAPAB_CONCURRENT_OPER) &&
2175 (p2p->dev_capab & P2P_DEV_CAPAB_INFRA_MANAGED))
b22128ef
JM
2176 p2p_buf_add_p2p_interface(tmp, p2p);
2177 p2p_buf_update_ie_hdr(tmp, lpos);
2178
2179 tmplen = wpabuf_len(tmp);
2180 if (tmplen > len)
2181 res = -1;
2182 else {
2183 os_memcpy(buf, wpabuf_head(tmp), tmplen);
2184 res = tmplen;
2185 }
2186 wpabuf_free(tmp);
2187
2188 return res;
2189}
2190
2191
2192int p2p_assoc_req_ie(struct p2p_data *p2p, const u8 *bssid, u8 *buf,
4c08c0bd 2193 size_t len, int p2p_group, struct wpabuf *p2p_ie)
b22128ef
JM
2194{
2195 struct wpabuf *tmp;
2196 u8 *lpos;
2197 struct p2p_device *peer;
2198 size_t tmplen;
2199 int res;
9675ce35 2200 size_t extra = 0;
b22128ef
JM
2201
2202 if (!p2p_group)
4c08c0bd 2203 return p2p_assoc_req_ie_wlan_ap(p2p, bssid, buf, len, p2p_ie);
b22128ef 2204
9675ce35
JM
2205#ifdef CONFIG_WIFI_DISPLAY
2206 if (p2p->wfd_ie_assoc_req)
2207 extra = wpabuf_len(p2p->wfd_ie_assoc_req);
2208#endif /* CONFIG_WIFI_DISPLAY */
2209
b22128ef
JM
2210 /*
2211 * (Re)Association Request - P2P IE
2212 * P2P Capability attribute (shall be present)
2213 * Extended Listen Timing (may be present)
2214 * P2P Device Info attribute (shall be present)
2215 */
9675ce35 2216 tmp = wpabuf_alloc(200 + extra);
b22128ef
JM
2217 if (tmp == NULL)
2218 return -1;
2219
9675ce35
JM
2220#ifdef CONFIG_WIFI_DISPLAY
2221 if (p2p->wfd_ie_assoc_req)
2222 wpabuf_put_buf(tmp, p2p->wfd_ie_assoc_req);
2223#endif /* CONFIG_WIFI_DISPLAY */
2224
b22128ef
JM
2225 peer = bssid ? p2p_get_device(p2p, bssid) : NULL;
2226
2227 lpos = p2p_buf_add_ie_hdr(tmp);
2228 p2p_buf_add_capability(tmp, p2p->dev_capab, 0);
5be5305b
JM
2229 if (p2p->ext_listen_interval)
2230 p2p_buf_add_ext_listen_timing(tmp, p2p->ext_listen_period,
2231 p2p->ext_listen_interval);
b22128ef
JM
2232 p2p_buf_add_device_info(tmp, p2p, peer);
2233 p2p_buf_update_ie_hdr(tmp, lpos);
2234
2235 tmplen = wpabuf_len(tmp);
2236 if (tmplen > len)
2237 res = -1;
2238 else {
2239 os_memcpy(buf, wpabuf_head(tmp), tmplen);
2240 res = tmplen;
2241 }
2242 wpabuf_free(tmp);
2243
2244 return res;
2245}
2246
2247
2248int p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf, char *end)
2249{
2250 struct wpabuf *p2p_ie;
2251 int ret;
2252
2253 p2p_ie = ieee802_11_vendor_ie_concat(ies, ies_len, P2P_IE_VENDOR_TYPE);
2254 if (p2p_ie == NULL)
2255 return 0;
2256
2257 ret = p2p_attr_text(p2p_ie, buf, end);
2258 wpabuf_free(p2p_ie);
2259 return ret;
2260}
2261
2262
c2d76aa6 2263int p2p_parse_dev_addr_in_p2p_ie(struct wpabuf *p2p_ie, u8 *dev_addr)
0a70f34f 2264{
0a70f34f
JM
2265 struct p2p_message msg;
2266
0a70f34f 2267 os_memset(&msg, 0, sizeof(msg));
c2d76aa6 2268 if (p2p_parse_p2p_ie(p2p_ie, &msg))
0a70f34f 2269 return -1;
0a70f34f 2270
526ec4ae
JM
2271 if (msg.p2p_device_addr) {
2272 os_memcpy(dev_addr, msg.p2p_device_addr, ETH_ALEN);
c2d76aa6 2273 return 0;
526ec4ae
JM
2274 } else if (msg.device_id) {
2275 os_memcpy(dev_addr, msg.device_id, ETH_ALEN);
c2d76aa6 2276 return 0;
0a70f34f 2277 }
c2d76aa6
MH
2278 return -1;
2279}
0a70f34f 2280
c2d76aa6
MH
2281
2282int p2p_parse_dev_addr(const u8 *ies, size_t ies_len, u8 *dev_addr)
2283{
2284 struct wpabuf *p2p_ie;
2285 int ret;
2286
2287 p2p_ie = ieee802_11_vendor_ie_concat(ies, ies_len,
2288 P2P_IE_VENDOR_TYPE);
2289 if (p2p_ie == NULL)
2290 return -1;
2291 ret = p2p_parse_dev_addr_in_p2p_ie(p2p_ie, dev_addr);
0a70f34f 2292 wpabuf_free(p2p_ie);
526ec4ae 2293 return ret;
0a70f34f
JM
2294}
2295
2296
b22128ef
JM
2297static void p2p_clear_go_neg(struct p2p_data *p2p)
2298{
2299 p2p->go_neg_peer = NULL;
2300 p2p_clear_timeout(p2p);
2301 p2p_set_state(p2p, P2P_IDLE);
2302}
2303
2304
2305void p2p_wps_success_cb(struct p2p_data *p2p, const u8 *mac_addr)
2306{
2307 if (p2p->go_neg_peer == NULL) {
ed496f13 2308 p2p_dbg(p2p, "No pending Group Formation - ignore WPS registration success notification");
b22128ef
JM
2309 return; /* No pending Group Formation */
2310 }
2311
2312 if (os_memcmp(mac_addr, p2p->go_neg_peer->intended_addr, ETH_ALEN) !=
2313 0) {
ed496f13
JM
2314 p2p_dbg(p2p, "Ignore WPS registration success notification for "
2315 MACSTR " (GO Negotiation peer " MACSTR ")",
b22128ef
JM
2316 MAC2STR(mac_addr),
2317 MAC2STR(p2p->go_neg_peer->intended_addr));
2318 return; /* Ignore unexpected peer address */
2319 }
2320
ed496f13 2321 p2p_dbg(p2p, "Group Formation completed successfully with " MACSTR,
b22128ef
JM
2322 MAC2STR(mac_addr));
2323
2324 p2p_clear_go_neg(p2p);
2325}
2326
2327
2328void p2p_group_formation_failed(struct p2p_data *p2p)
2329{
2330 if (p2p->go_neg_peer == NULL) {
ed496f13 2331 p2p_dbg(p2p, "No pending Group Formation - ignore group formation failure notification");
b22128ef
JM
2332 return; /* No pending Group Formation */
2333 }
2334
ed496f13 2335 p2p_dbg(p2p, "Group Formation failed with " MACSTR,
b22128ef
JM
2336 MAC2STR(p2p->go_neg_peer->intended_addr));
2337
2338 p2p_clear_go_neg(p2p);
2339}
2340
2341
2342struct p2p_data * p2p_init(const struct p2p_config *cfg)
2343{
2344 struct p2p_data *p2p;
2345
2346 if (cfg->max_peers < 1)
2347 return NULL;
2348
2349 p2p = os_zalloc(sizeof(*p2p) + sizeof(*cfg));
2350 if (p2p == NULL)
2351 return NULL;
2352 p2p->cfg = (struct p2p_config *) (p2p + 1);
2353 os_memcpy(p2p->cfg, cfg, sizeof(*cfg));
2354 if (cfg->dev_name)
2355 p2p->cfg->dev_name = os_strdup(cfg->dev_name);
b6e01800
JM
2356 if (cfg->manufacturer)
2357 p2p->cfg->manufacturer = os_strdup(cfg->manufacturer);
2358 if (cfg->model_name)
2359 p2p->cfg->model_name = os_strdup(cfg->model_name);
2360 if (cfg->model_number)
2361 p2p->cfg->model_number = os_strdup(cfg->model_number);
2362 if (cfg->serial_number)
2363 p2p->cfg->serial_number = os_strdup(cfg->serial_number);
21d996f7
JM
2364 if (cfg->pref_chan) {
2365 p2p->cfg->pref_chan = os_malloc(cfg->num_pref_chan *
2366 sizeof(struct p2p_channel));
2367 if (p2p->cfg->pref_chan) {
2368 os_memcpy(p2p->cfg->pref_chan, cfg->pref_chan,
2369 cfg->num_pref_chan *
2370 sizeof(struct p2p_channel));
2371 } else
2372 p2p->cfg->num_pref_chan = 0;
2373 }
b22128ef
JM
2374
2375 p2p->min_disc_int = 1;
2376 p2p->max_disc_int = 3;
96beff11 2377 p2p->max_disc_tu = -1;
b22128ef
JM
2378
2379 os_get_random(&p2p->next_tie_breaker, 1);
2380 p2p->next_tie_breaker &= 0x01;
2381 if (cfg->sd_request)
2382 p2p->dev_capab |= P2P_DEV_CAPAB_SERVICE_DISCOVERY;
2383 p2p->dev_capab |= P2P_DEV_CAPAB_INVITATION_PROCEDURE;
2384 if (cfg->concurrent_operations)
2385 p2p->dev_capab |= P2P_DEV_CAPAB_CONCURRENT_OPER;
2386 p2p->dev_capab |= P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
2387
2388 dl_list_init(&p2p->devices);
2389
2390 eloop_register_timeout(P2P_PEER_EXPIRATION_INTERVAL, 0,
2391 p2p_expiration_timeout, p2p, NULL);
2392
4f219667
JM
2393 p2p->go_timeout = 100;
2394 p2p->client_timeout = 20;
2395
51e9f228
JM
2396 p2p_dbg(p2p, "initialized");
2397 p2p_channels_dump(p2p, "channels", &p2p->cfg->channels);
2398 p2p_channels_dump(p2p, "cli_channels", &p2p->cfg->cli_channels);
2399
b22128ef
JM
2400 return p2p;
2401}
2402
2403
2404void p2p_deinit(struct p2p_data *p2p)
2405{
9675ce35
JM
2406#ifdef CONFIG_WIFI_DISPLAY
2407 wpabuf_free(p2p->wfd_ie_beacon);
2408 wpabuf_free(p2p->wfd_ie_probe_req);
2409 wpabuf_free(p2p->wfd_ie_probe_resp);
2410 wpabuf_free(p2p->wfd_ie_assoc_req);
2411 wpabuf_free(p2p->wfd_ie_invitation);
2412 wpabuf_free(p2p->wfd_ie_prov_disc_req);
2413 wpabuf_free(p2p->wfd_ie_prov_disc_resp);
2414 wpabuf_free(p2p->wfd_ie_go_neg);
2415 wpabuf_free(p2p->wfd_dev_info);
2416 wpabuf_free(p2p->wfd_assoc_bssid);
2417 wpabuf_free(p2p->wfd_coupled_sink_info);
2418#endif /* CONFIG_WIFI_DISPLAY */
2419
b22128ef
JM
2420 eloop_cancel_timeout(p2p_expiration_timeout, p2p, NULL);
2421 eloop_cancel_timeout(p2p_ext_listen_timeout, p2p, NULL);
2422 eloop_cancel_timeout(p2p_scan_timeout, p2p, NULL);
8cee87ab 2423 eloop_cancel_timeout(p2p_go_neg_start, p2p, NULL);
b22128ef 2424 p2p_flush(p2p);
046ef4aa 2425 p2p_free_req_dev_types(p2p);
b22128ef 2426 os_free(p2p->cfg->dev_name);
b6e01800
JM
2427 os_free(p2p->cfg->manufacturer);
2428 os_free(p2p->cfg->model_name);
2429 os_free(p2p->cfg->model_number);
2430 os_free(p2p->cfg->serial_number);
21d996f7 2431 os_free(p2p->cfg->pref_chan);
b22128ef 2432 os_free(p2p->groups);
18708aad 2433 wpabuf_free(p2p->sd_resp);
3f9285ff 2434 os_free(p2p->after_scan_tx);
f95cac27 2435 p2p_remove_wps_vendor_extensions(p2p);
556b30da 2436 os_free(p2p->no_go_freq.range);
b22128ef
JM
2437 os_free(p2p);
2438}
2439
2440
2441void p2p_flush(struct p2p_data *p2p)
2442{
2443 struct p2p_device *dev, *prev;
78db55b8 2444 p2p_stop_find(p2p);
b22128ef
JM
2445 dl_list_for_each_safe(dev, prev, &p2p->devices, struct p2p_device,
2446 list) {
2447 dl_list_del(&dev->list);
2448 p2p_device_free(p2p, dev);
2449 }
2450 p2p_free_sd_queries(p2p);
f44ae207
JM
2451 os_free(p2p->after_scan_tx);
2452 p2p->after_scan_tx = NULL;
b22128ef
JM
2453}
2454
2455
9d562b79
SS
2456int p2p_unauthorize(struct p2p_data *p2p, const u8 *addr)
2457{
2458 struct p2p_device *dev;
2459
2460 dev = p2p_get_device(p2p, addr);
2461 if (dev == NULL)
2462 return -1;
2463
ed496f13 2464 p2p_dbg(p2p, "Unauthorizing " MACSTR, MAC2STR(addr));
9d562b79
SS
2465
2466 if (p2p->go_neg_peer == dev)
2467 p2p->go_neg_peer = NULL;
2468
2469 dev->wps_method = WPS_NOT_READY;
2470 dev->flags &= ~P2P_DEV_WAIT_GO_NEG_RESPONSE;
2471 dev->flags &= ~P2P_DEV_WAIT_GO_NEG_CONFIRM;
2472
2473 /* Check if after_scan_tx is for this peer. If so free it */
2474 if (p2p->after_scan_tx &&
2475 os_memcmp(addr, p2p->after_scan_tx->dst, ETH_ALEN) == 0) {
2476 os_free(p2p->after_scan_tx);
2477 p2p->after_scan_tx = NULL;
2478 }
2479
2480 return 0;
2481}
2482
2483
b22128ef
JM
2484int p2p_set_dev_name(struct p2p_data *p2p, const char *dev_name)
2485{
2486 os_free(p2p->cfg->dev_name);
2487 if (dev_name) {
2488 p2p->cfg->dev_name = os_strdup(dev_name);
2489 if (p2p->cfg->dev_name == NULL)
2490 return -1;
2491 } else
2492 p2p->cfg->dev_name = NULL;
2493 return 0;
2494}
2495
2496
b6e01800
JM
2497int p2p_set_manufacturer(struct p2p_data *p2p, const char *manufacturer)
2498{
2499 os_free(p2p->cfg->manufacturer);
2500 p2p->cfg->manufacturer = NULL;
2501 if (manufacturer) {
2502 p2p->cfg->manufacturer = os_strdup(manufacturer);
2503 if (p2p->cfg->manufacturer == NULL)
2504 return -1;
2505 }
2506
2507 return 0;
2508}
2509
2510
2511int p2p_set_model_name(struct p2p_data *p2p, const char *model_name)
2512{
2513 os_free(p2p->cfg->model_name);
2514 p2p->cfg->model_name = NULL;
2515 if (model_name) {
2516 p2p->cfg->model_name = os_strdup(model_name);
2517 if (p2p->cfg->model_name == NULL)
2518 return -1;
2519 }
2520
2521 return 0;
2522}
2523
2524
2525int p2p_set_model_number(struct p2p_data *p2p, const char *model_number)
2526{
2527 os_free(p2p->cfg->model_number);
2528 p2p->cfg->model_number = NULL;
2529 if (model_number) {
2530 p2p->cfg->model_number = os_strdup(model_number);
2531 if (p2p->cfg->model_number == NULL)
2532 return -1;
2533 }
2534
2535 return 0;
2536}
2537
2538
2539int p2p_set_serial_number(struct p2p_data *p2p, const char *serial_number)
2540{
2541 os_free(p2p->cfg->serial_number);
2542 p2p->cfg->serial_number = NULL;
2543 if (serial_number) {
2544 p2p->cfg->serial_number = os_strdup(serial_number);
2545 if (p2p->cfg->serial_number == NULL)
2546 return -1;
2547 }
2548
2549 return 0;
2550}
2551
2552
2553void p2p_set_config_methods(struct p2p_data *p2p, u16 config_methods)
2554{
2555 p2p->cfg->config_methods = config_methods;
2556}
2557
2558
2559void p2p_set_uuid(struct p2p_data *p2p, const u8 *uuid)
2560{
2561 os_memcpy(p2p->cfg->uuid, uuid, 16);
2562}
2563
2564
b22128ef
JM
2565int p2p_set_pri_dev_type(struct p2p_data *p2p, const u8 *pri_dev_type)
2566{
2567 os_memcpy(p2p->cfg->pri_dev_type, pri_dev_type, 8);
2568 return 0;
2569}
2570
2571
2572int p2p_set_sec_dev_types(struct p2p_data *p2p, const u8 dev_types[][8],
2573 size_t num_dev_types)
2574{
2575 if (num_dev_types > P2P_SEC_DEVICE_TYPES)
2576 num_dev_types = P2P_SEC_DEVICE_TYPES;
2577 p2p->cfg->num_sec_dev_types = num_dev_types;
2578 os_memcpy(p2p->cfg->sec_dev_type, dev_types, num_dev_types * 8);
2579 return 0;
2580}
2581
2582
f95cac27
JMB
2583void p2p_remove_wps_vendor_extensions(struct p2p_data *p2p)
2584{
2585 int i;
2586
10c5d2a5 2587 for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) {
f95cac27
JMB
2588 wpabuf_free(p2p->wps_vendor_ext[i]);
2589 p2p->wps_vendor_ext[i] = NULL;
2590 }
2591}
2592
2593
2594int p2p_add_wps_vendor_extension(struct p2p_data *p2p,
2595 const struct wpabuf *vendor_ext)
2596{
2597 int i;
2598
2599 if (vendor_ext == NULL)
2600 return -1;
2601
10c5d2a5 2602 for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) {
f95cac27
JMB
2603 if (p2p->wps_vendor_ext[i] == NULL)
2604 break;
2605 }
10c5d2a5 2606 if (i >= P2P_MAX_WPS_VENDOR_EXT)
f95cac27
JMB
2607 return -1;
2608
2609 p2p->wps_vendor_ext[i] = wpabuf_dup(vendor_ext);
2610 if (p2p->wps_vendor_ext[i] == NULL)
2611 return -1;
2612
2613 return 0;
2614}
2615
2616
b22128ef
JM
2617int p2p_set_country(struct p2p_data *p2p, const char *country)
2618{
2619 os_memcpy(p2p->cfg->country, country, 3);
2620 return 0;
2621}
2622
2623
2624void p2p_continue_find(struct p2p_data *p2p)
2625{
2626 struct p2p_device *dev;
2627 p2p_set_state(p2p, P2P_SEARCH);
2628 dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
2629 if (dev->flags & P2P_DEV_SD_SCHEDULE) {
2630 if (p2p_start_sd(p2p, dev) == 0)
2631 return;
2632 else
2633 break;
10c4edde
JM
2634 } else if (dev->req_config_methods &&
2635 !(dev->flags & P2P_DEV_PD_FOR_JOIN)) {
ed496f13 2636 p2p_dbg(p2p, "Send pending Provision Discovery Request to "
10c4edde 2637 MACSTR " (config methods 0x%x)",
c5db8e51 2638 MAC2STR(dev->info.p2p_device_addr),
10c4edde 2639 dev->req_config_methods);
1ef2f7ff 2640 if (p2p_send_prov_disc_req(p2p, dev, 0, 0) == 0)
b22128ef
JM
2641 return;
2642 }
2643 }
2644
96beff11 2645 p2p_listen_in_find(p2p, 1);
b22128ef
JM
2646}
2647
2648
2649static void p2p_sd_cb(struct p2p_data *p2p, int success)
2650{
ed496f13 2651 p2p_dbg(p2p, "Service Discovery Query TX callback: success=%d",
b22128ef
JM
2652 success);
2653 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
2654
2655 if (!success) {
2656 if (p2p->sd_peer) {
2657 p2p->sd_peer->flags &= ~P2P_DEV_SD_SCHEDULE;
2658 p2p->sd_peer = NULL;
2659 }
2660 p2p_continue_find(p2p);
2661 return;
2662 }
2663
2664 if (p2p->sd_peer == NULL) {
ed496f13 2665 p2p_dbg(p2p, "No SD peer entry known");
b22128ef
JM
2666 p2p_continue_find(p2p);
2667 return;
2668 }
2669
2670 /* Wait for response from the peer */
2671 p2p_set_state(p2p, P2P_SD_DURING_FIND);
2672 p2p_set_timeout(p2p, 0, 200000);
2673}
2674
6b56cc2d
JS
2675
2676/**
2677 * p2p_retry_pd - Retry any pending provision disc requests in IDLE state
2678 * @p2p: P2P module context from p2p_init()
2679 */
19df9b07 2680static void p2p_retry_pd(struct p2p_data *p2p)
6b56cc2d
JS
2681{
2682 struct p2p_device *dev;
2683
2684 if (p2p->state != P2P_IDLE)
2685 return;
2686
2687 /*
2688 * Retry the prov disc req attempt only for the peer that the user had
175171ac 2689 * requested.
6b56cc2d
JS
2690 */
2691
2692 dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
2693 if (os_memcmp(p2p->pending_pd_devaddr,
2694 dev->info.p2p_device_addr, ETH_ALEN) != 0)
2695 continue;
2696 if (!dev->req_config_methods)
2697 continue;
6b56cc2d 2698
ed496f13 2699 p2p_dbg(p2p, "Send pending Provision Discovery Request to "
6b56cc2d
JS
2700 MACSTR " (config methods 0x%x)",
2701 MAC2STR(dev->info.p2p_device_addr),
2702 dev->req_config_methods);
175171ac 2703 p2p_send_prov_disc_req(p2p, dev,
8d82c210
JM
2704 dev->flags & P2P_DEV_PD_FOR_JOIN,
2705 p2p->pd_force_freq);
6b56cc2d
JS
2706 return;
2707 }
2708}
2709
2710
b22128ef
JM
2711static void p2p_prov_disc_cb(struct p2p_data *p2p, int success)
2712{
ed496f13 2713 p2p_dbg(p2p, "Provision Discovery Request TX callback: success=%d",
b22128ef 2714 success);
6b56cc2d
JS
2715
2716 /*
2717 * Postpone resetting the pending action state till after we actually
2718 * time out. This allows us to take some action like notifying any
2719 * interested parties about no response to the request.
2720 *
2721 * When the timer (below) goes off we check in IDLE, SEARCH, or
2722 * LISTEN_ONLY state, which are the only allowed states to issue a PD
2723 * requests in, if this was still pending and then raise notification.
2724 */
b22128ef
JM
2725
2726 if (!success) {
6b56cc2d
JS
2727 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
2728
488f4a71
JM
2729 if (p2p->user_initiated_pd &&
2730 (p2p->state == P2P_SEARCH || p2p->state == P2P_LISTEN_ONLY))
2731 {
2732 /* Retry request from timeout to avoid busy loops */
2733 p2p->pending_action_state = P2P_PENDING_PD;
2734 p2p_set_timeout(p2p, 0, 50000);
2735 } else if (p2p->state != P2P_IDLE)
b22128ef 2736 p2p_continue_find(p2p);
6b56cc2d
JS
2737 else if (p2p->user_initiated_pd) {
2738 p2p->pending_action_state = P2P_PENDING_PD;
2739 p2p_set_timeout(p2p, 0, 300000);
2740 }
b22128ef
JM
2741 return;
2742 }
2743
6b56cc2d
JS
2744 /*
2745 * This postponing, of resetting pending_action_state, needs to be
2746 * done only for user initiated PD requests and not internal ones.
2747 */
2748 if (p2p->user_initiated_pd)
2749 p2p->pending_action_state = P2P_PENDING_PD;
2750 else
2751 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
2752
b22128ef
JM
2753 /* Wait for response from the peer */
2754 if (p2p->state == P2P_SEARCH)
2755 p2p_set_state(p2p, P2P_PD_DURING_FIND);
2756 p2p_set_timeout(p2p, 0, 200000);
2757}
2758
2759
2760int p2p_scan_res_handler(struct p2p_data *p2p, const u8 *bssid, int freq,
c5f10e80 2761 struct os_time *rx_time, int level, const u8 *ies,
3dfd0484 2762 size_t ies_len)
b22128ef 2763{
a5b5e830
JM
2764 if (os_time_before(rx_time, &p2p->find_start)) {
2765 /*
2766 * The driver may have cached (e.g., in cfg80211 BSS table) the
2767 * scan results for relatively long time. To avoid reporting
2768 * stale information, update P2P peers only based on results
2769 * that have based on frames received after the last p2p_find
2770 * operation was started.
2771 */
ed496f13
JM
2772 p2p_dbg(p2p, "Ignore old scan result for " MACSTR
2773 " (rx_time=%u.%06u)",
a5b5e830
JM
2774 MAC2STR(bssid), (unsigned int) rx_time->sec,
2775 (unsigned int) rx_time->usec);
2776 return 0;
2777 }
2778
c5f10e80 2779 p2p_add_device(p2p, bssid, freq, rx_time, level, ies, ies_len, 1);
b22128ef 2780
b22128ef
JM
2781 return 0;
2782}
2783
2784
2785void p2p_scan_res_handled(struct p2p_data *p2p)
2786{
2787 if (!p2p->p2p_scan_running) {
ed496f13 2788 p2p_dbg(p2p, "p2p_scan was not running, but scan results received");
b22128ef
JM
2789 }
2790 p2p->p2p_scan_running = 0;
2791 eloop_cancel_timeout(p2p_scan_timeout, p2p, NULL);
2792
2793 if (p2p_run_after_scan(p2p))
2794 return;
2795 if (p2p->state == P2P_SEARCH)
2796 p2p_continue_find(p2p);
2797}
2798
2799
6d92fa6e 2800void p2p_scan_ie(struct p2p_data *p2p, struct wpabuf *ies, const u8 *dev_id)
b22128ef 2801{
9675ce35
JM
2802 u8 *len;
2803
2804#ifdef CONFIG_WIFI_DISPLAY
2805 if (p2p->wfd_ie_probe_req)
2806 wpabuf_put_buf(ies, p2p->wfd_ie_probe_req);
2807#endif /* CONFIG_WIFI_DISPLAY */
2808
2809 len = p2p_buf_add_ie_hdr(ies);
18485b54
MH
2810 p2p_buf_add_capability(ies, p2p->dev_capab &
2811 ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY, 0);
6d92fa6e
JM
2812 if (dev_id)
2813 p2p_buf_add_device_id(ies, dev_id);
b22128ef
JM
2814 if (p2p->cfg->reg_class && p2p->cfg->channel)
2815 p2p_buf_add_listen_channel(ies, p2p->cfg->country,
2816 p2p->cfg->reg_class,
2817 p2p->cfg->channel);
2818 if (p2p->ext_listen_interval)
2819 p2p_buf_add_ext_listen_timing(ies, p2p->ext_listen_period,
2820 p2p->ext_listen_interval);
2821 /* TODO: p2p_buf_add_operating_channel() if GO */
2822 p2p_buf_update_ie_hdr(ies, len);
2823}
2824
2825
206e1f42
JM
2826size_t p2p_scan_ie_buf_len(struct p2p_data *p2p)
2827{
9675ce35
JM
2828 size_t len = 100;
2829
2830#ifdef CONFIG_WIFI_DISPLAY
2831 if (p2p && p2p->wfd_ie_probe_req)
2832 len += wpabuf_len(p2p->wfd_ie_probe_req);
2833#endif /* CONFIG_WIFI_DISPLAY */
2834
2835 return len;
206e1f42
JM
2836}
2837
2838
b22128ef
JM
2839int p2p_ie_text(struct wpabuf *p2p_ie, char *buf, char *end)
2840{
2841 return p2p_attr_text(p2p_ie, buf, end);
2842}
2843
2844
2845static void p2p_go_neg_req_cb(struct p2p_data *p2p, int success)
2846{
2847 struct p2p_device *dev = p2p->go_neg_peer;
fb8984fd 2848 int timeout;
b22128ef 2849
ed496f13 2850 p2p_dbg(p2p, "GO Negotiation Request TX callback: success=%d", success);
b22128ef
JM
2851
2852 if (dev == NULL) {
ed496f13 2853 p2p_dbg(p2p, "No pending GO Negotiation");
b22128ef
JM
2854 return;
2855 }
2856
2857 if (success) {
b22128ef
JM
2858 if (dev->flags & P2P_DEV_USER_REJECTED) {
2859 p2p_set_state(p2p, P2P_IDLE);
2860 return;
2861 }
a1d2ab32
NKG
2862 } else if (dev->go_neg_req_sent) {
2863 /* Cancel the increment from p2p_connect_send() on failure */
2864 dev->go_neg_req_sent--;
b22128ef
JM
2865 }
2866
2867 if (!success &&
c5db8e51 2868 (dev->info.dev_capab & P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY) &&
b22128ef 2869 !is_zero_ether_addr(dev->member_in_go_dev)) {
ed496f13 2870 p2p_dbg(p2p, "Peer " MACSTR " did not acknowledge request - try to use device discoverability through its GO",
c5db8e51 2871 MAC2STR(dev->info.p2p_device_addr));
b22128ef
JM
2872 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
2873 p2p_send_dev_disc_req(p2p, dev);
2874 return;
2875 }
2876
2877 /*
2878 * Use P2P find, if needed, to find the other device from its listen
2879 * channel.
2880 */
2881 p2p_set_state(p2p, P2P_CONNECT);
fb8984fd
JM
2882 timeout = success ? 500000 : 100000;
2883 if (!success && p2p->go_neg_peer &&
2884 (p2p->go_neg_peer->flags & P2P_DEV_PEER_WAITING_RESPONSE)) {
2885 unsigned int r;
2886 /*
2887 * Peer is expected to wait our response and we will skip the
2888 * listen phase. Add some randomness to the wait time here to
2889 * make it less likely to hit cases where we could end up in
2890 * sync with peer not listening.
2891 */
2892 os_get_random((u8 *) &r, sizeof(r));
2893 timeout += r % 100000;
2894 }
2895 p2p_set_timeout(p2p, 0, timeout);
b22128ef
JM
2896}
2897
2898
2899static void p2p_go_neg_resp_cb(struct p2p_data *p2p, int success)
2900{
ed496f13 2901 p2p_dbg(p2p, "GO Negotiation Response TX callback: success=%d",
b22128ef
JM
2902 success);
2903 if (!p2p->go_neg_peer && p2p->state == P2P_PROVISIONING) {
ed496f13 2904 p2p_dbg(p2p, "Ignore TX callback event - GO Negotiation is not running anymore");
b22128ef
JM
2905 return;
2906 }
2907 p2p_set_state(p2p, P2P_CONNECT);
8e4839ce 2908 p2p_set_timeout(p2p, 0, 500000);
b22128ef
JM
2909}
2910
2911
7800d45c
JM
2912static void p2p_go_neg_resp_failure_cb(struct p2p_data *p2p, int success,
2913 const u8 *addr)
b22128ef 2914{
ed496f13 2915 p2p_dbg(p2p, "GO Negotiation Response (failure) TX callback: success=%d", success);
fbe70272
JM
2916 if (p2p->go_neg_peer && p2p->go_neg_peer->status != P2P_SC_SUCCESS) {
2917 p2p_go_neg_failed(p2p, p2p->go_neg_peer,
2918 p2p->go_neg_peer->status);
7800d45c
JM
2919 } else if (success) {
2920 struct p2p_device *dev;
2921 dev = p2p_get_device(p2p, addr);
2922 if (dev &&
2923 dev->status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE)
2924 dev->flags |= P2P_DEV_PEER_WAITING_RESPONSE;
fbe70272 2925 }
b22128ef
JM
2926}
2927
2928
93b7ddd0
JM
2929static void p2p_go_neg_conf_cb(struct p2p_data *p2p,
2930 enum p2p_send_action_result result)
b22128ef
JM
2931{
2932 struct p2p_device *dev;
2933
ed496f13 2934 p2p_dbg(p2p, "GO Negotiation Confirm TX callback: result=%d", result);
b22128ef 2935 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
93b7ddd0
JM
2936 if (result == P2P_SEND_ACTION_FAILED) {
2937 p2p_go_neg_failed(p2p, p2p->go_neg_peer, -1);
2938 return;
2939 }
2940 if (result == P2P_SEND_ACTION_NO_ACK) {
b22128ef
JM
2941 /*
2942 * It looks like the TX status for GO Negotiation Confirm is
2943 * often showing failure even when the peer has actually
2944 * received the frame. Since the peer may change channels
2945 * immediately after having received the frame, we may not see
2946 * an Ack for retries, so just dropping a single frame may
2947 * trigger this. To allow the group formation to succeed if the
2948 * peer did indeed receive the frame, continue regardless of
2949 * the TX status.
2950 */
ed496f13 2951 p2p_dbg(p2p, "Assume GO Negotiation Confirm TX was actually received by the peer even though Ack was not reported");
b22128ef
JM
2952 }
2953
2954 dev = p2p->go_neg_peer;
2955 if (dev == NULL)
2956 return;
2957
2958 p2p_go_complete(p2p, dev);
2959}
2960
2961
2962void p2p_send_action_cb(struct p2p_data *p2p, unsigned int freq, const u8 *dst,
93b7ddd0
JM
2963 const u8 *src, const u8 *bssid,
2964 enum p2p_send_action_result result)
b22128ef
JM
2965{
2966 enum p2p_pending_action_state state;
93b7ddd0 2967 int success;
b22128ef 2968
ed496f13 2969 p2p_dbg(p2p, "Action frame TX callback (state=%d freq=%u dst=" MACSTR
93b7ddd0 2970 " src=" MACSTR " bssid=" MACSTR " result=%d",
b22128ef 2971 p2p->pending_action_state, freq, MAC2STR(dst), MAC2STR(src),
93b7ddd0
JM
2972 MAC2STR(bssid), result);
2973 success = result == P2P_SEND_ACTION_SUCCESS;
b22128ef
JM
2974 state = p2p->pending_action_state;
2975 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
2976 switch (state) {
2977 case P2P_NO_PENDING_ACTION:
63a965c3
JM
2978 if (p2p->after_scan_tx_in_progress) {
2979 p2p->after_scan_tx_in_progress = 0;
2980 if (p2p->start_after_scan != P2P_AFTER_SCAN_NOTHING &&
2981 p2p_run_after_scan(p2p))
2982 break;
2983 if (p2p->state == P2P_SEARCH) {
ed496f13 2984 p2p_dbg(p2p, "Continue find after after_scan_tx completion");
63a965c3
JM
2985 p2p_continue_find(p2p);
2986 }
2987 }
b22128ef
JM
2988 break;
2989 case P2P_PENDING_GO_NEG_REQUEST:
2990 p2p_go_neg_req_cb(p2p, success);
2991 break;
2992 case P2P_PENDING_GO_NEG_RESPONSE:
2993 p2p_go_neg_resp_cb(p2p, success);
2994 break;
2995 case P2P_PENDING_GO_NEG_RESPONSE_FAILURE:
7800d45c 2996 p2p_go_neg_resp_failure_cb(p2p, success, dst);
b22128ef
JM
2997 break;
2998 case P2P_PENDING_GO_NEG_CONFIRM:
93b7ddd0 2999 p2p_go_neg_conf_cb(p2p, result);
b22128ef
JM
3000 break;
3001 case P2P_PENDING_SD:
3002 p2p_sd_cb(p2p, success);
3003 break;
3004 case P2P_PENDING_PD:
3005 p2p_prov_disc_cb(p2p, success);
3006 break;
3007 case P2P_PENDING_INVITATION_REQUEST:
3008 p2p_invitation_req_cb(p2p, success);
3009 break;
3010 case P2P_PENDING_INVITATION_RESPONSE:
3011 p2p_invitation_resp_cb(p2p, success);
3012 break;
3013 case P2P_PENDING_DEV_DISC_REQUEST:
3014 p2p_dev_disc_req_cb(p2p, success);
3015 break;
3016 case P2P_PENDING_DEV_DISC_RESPONSE:
3017 p2p_dev_disc_resp_cb(p2p, success);
3018 break;
3019 case P2P_PENDING_GO_DISC_REQ:
3020 p2p_go_disc_req_cb(p2p, success);
3021 break;
3022 }
63a965c3
JM
3023
3024 p2p->after_scan_tx_in_progress = 0;
b22128ef
JM
3025}
3026
3027
3028void p2p_listen_cb(struct p2p_data *p2p, unsigned int freq,
3029 unsigned int duration)
3030{
3031 if (freq == p2p->pending_client_disc_freq) {
ed496f13 3032 p2p_dbg(p2p, "Client discoverability remain-awake completed");
b22128ef
JM
3033 p2p->pending_client_disc_freq = 0;
3034 return;
3035 }
3036
3037 if (freq != p2p->pending_listen_freq) {
ed496f13 3038 p2p_dbg(p2p, "Unexpected listen callback for freq=%u duration=%u (pending_listen_freq=%u)",
b22128ef
JM
3039 freq, duration, p2p->pending_listen_freq);
3040 return;
3041 }
3042
ed496f13 3043 p2p_dbg(p2p, "Starting Listen timeout(%u,%u) on freq=%u based on callback",
b22128ef
JM
3044 p2p->pending_listen_sec, p2p->pending_listen_usec,
3045 p2p->pending_listen_freq);
3046 p2p->in_listen = 1;
0b8889d8 3047 p2p->drv_in_listen = freq;
b22128ef
JM
3048 if (p2p->pending_listen_sec || p2p->pending_listen_usec) {
3049 /*
3050 * Add 20 msec extra wait to avoid race condition with driver
3051 * remain-on-channel end event, i.e., give driver more time to
3052 * complete the operation before our timeout expires.
3053 */
3054 p2p_set_timeout(p2p, p2p->pending_listen_sec,
3055 p2p->pending_listen_usec + 20000);
3056 }
3057
3058 p2p->pending_listen_freq = 0;
3059}
3060
3061
3062int p2p_listen_end(struct p2p_data *p2p, unsigned int freq)
3063{
ed496f13 3064 p2p_dbg(p2p, "Driver ended Listen state (freq=%u)", freq);
b22128ef
JM
3065 p2p->drv_in_listen = 0;
3066 if (p2p->in_listen)
3067 return 0; /* Internal timeout will trigger the next step */
3068
3069 if (p2p->state == P2P_CONNECT_LISTEN && p2p->go_neg_peer) {
e24cf97c 3070 if (p2p->go_neg_peer->connect_reqs >= 120) {
ed496f13 3071 p2p_dbg(p2p, "Timeout on sending GO Negotiation Request without getting response");
e24cf97c
JM
3072 p2p_go_neg_failed(p2p, p2p->go_neg_peer, -1);
3073 return 0;
3074 }
3075
b22128ef
JM
3076 p2p_set_state(p2p, P2P_CONNECT);
3077 p2p_connect_send(p2p, p2p->go_neg_peer);
3078 return 1;
3079 } else if (p2p->state == P2P_SEARCH) {
59acfe87
JM
3080 if (p2p->p2p_scan_running) {
3081 /*
3082 * Search is already in progress. This can happen if
3083 * an Action frame RX is reported immediately after
3084 * the end of a remain-on-channel operation and the
3085 * response frame to that is sent using an offchannel
3086 * operation while in p2p_find. Avoid an attempt to
3087 * restart a scan here.
3088 */
ed496f13 3089 p2p_dbg(p2p, "p2p_scan already in progress - do not try to start a new one");
59acfe87
JM
3090 return 1;
3091 }
3fe8b68d
JM
3092 if (p2p->pending_listen_freq) {
3093 /*
3094 * Better wait a bit if the driver is unable to start
3095 * offchannel operation for some reason. p2p_search()
3096 * will be started from internal timeout.
3097 */
ed496f13 3098 p2p_dbg(p2p, "Listen operation did not seem to start - delay search phase to avoid busy loop");
3fe8b68d
JM
3099 p2p_set_timeout(p2p, 0, 100000);
3100 return 1;
3101 }
37448ede 3102 if (p2p->search_delay) {
ed496f13 3103 p2p_dbg(p2p, "Delay search operation by %u ms",
37448ede
JM
3104 p2p->search_delay);
3105 p2p_set_timeout(p2p, p2p->search_delay / 1000,
3106 (p2p->search_delay % 1000) * 1000);
3107 return 1;
3108 }
b22128ef
JM
3109 p2p_search(p2p);
3110 return 1;
3111 }
3112
3113 return 0;
3114}
3115
3116
3117static void p2p_timeout_connect(struct p2p_data *p2p)
3118{
3119 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
579a8098
JM
3120 if (p2p->go_neg_peer &&
3121 (p2p->go_neg_peer->flags & P2P_DEV_WAIT_GO_NEG_CONFIRM)) {
ed496f13 3122 p2p_dbg(p2p, "Wait for GO Negotiation Confirm timed out - assume GO Negotiation failed");
579a8098
JM
3123 p2p_go_neg_failed(p2p, p2p->go_neg_peer, -1);
3124 return;
3125 }
fb8984fd
JM
3126 if (p2p->go_neg_peer &&
3127 (p2p->go_neg_peer->flags & P2P_DEV_PEER_WAITING_RESPONSE) &&
3128 p2p->go_neg_peer->connect_reqs < 120) {
ed496f13 3129 p2p_dbg(p2p, "Peer expected to wait our response - skip listen");
fb8984fd
JM
3130 p2p_connect_send(p2p, p2p->go_neg_peer);
3131 return;
3132 }
3133
b22128ef 3134 p2p_set_state(p2p, P2P_CONNECT_LISTEN);
96beff11 3135 p2p_listen_in_find(p2p, 0);
b22128ef
JM
3136}
3137
3138
3139static void p2p_timeout_connect_listen(struct p2p_data *p2p)
3140{
3141 if (p2p->go_neg_peer) {
3142 if (p2p->drv_in_listen) {
ed496f13 3143 p2p_dbg(p2p, "Driver is still in Listen state; wait for it to complete");
b22128ef
JM
3144 return;
3145 }
9dac8c3e
FM
3146
3147 if (p2p->go_neg_peer->connect_reqs >= 120) {
ed496f13 3148 p2p_dbg(p2p, "Timeout on sending GO Negotiation Request without getting response");
9dac8c3e
FM
3149 p2p_go_neg_failed(p2p, p2p->go_neg_peer, -1);
3150 return;
3151 }
3152
b22128ef
JM
3153 p2p_set_state(p2p, P2P_CONNECT);
3154 p2p_connect_send(p2p, p2p->go_neg_peer);
3155 } else
3156 p2p_set_state(p2p, P2P_IDLE);
3157}
3158
3159
3160static void p2p_timeout_wait_peer_connect(struct p2p_data *p2p)
3161{
3162 /*
3163 * TODO: could remain constantly in Listen state for some time if there
3164 * are no other concurrent uses for the radio. For now, go to listen
3165 * state once per second to give other uses a chance to use the radio.
3166 */
3167 p2p_set_state(p2p, P2P_WAIT_PEER_IDLE);
d58ed4e3 3168 p2p_set_timeout(p2p, 0, 500000);
b22128ef
JM
3169}
3170
3171
3172static void p2p_timeout_wait_peer_idle(struct p2p_data *p2p)
3173{
3174 struct p2p_device *dev = p2p->go_neg_peer;
3175
3176 if (dev == NULL) {
ed496f13 3177 p2p_dbg(p2p, "Unknown GO Neg peer - stop GO Neg wait");
b22128ef
JM
3178 return;
3179 }
3180
3181 dev->wait_count++;
3182 if (dev->wait_count >= 120) {
ed496f13 3183 p2p_dbg(p2p, "Timeout on waiting peer to become ready for GO Negotiation");
b22128ef
JM
3184 p2p_go_neg_failed(p2p, dev, -1);
3185 return;
3186 }
3187
ed496f13 3188 p2p_dbg(p2p, "Go to Listen state while waiting for the peer to become ready for GO Negotiation");
b22128ef 3189 p2p_set_state(p2p, P2P_WAIT_PEER_CONNECT);
96beff11 3190 p2p_listen_in_find(p2p, 0);
b22128ef
JM
3191}
3192
3193
3194static void p2p_timeout_sd_during_find(struct p2p_data *p2p)
3195{
ed496f13 3196 p2p_dbg(p2p, "Service Discovery Query timeout");
b22128ef
JM
3197 if (p2p->sd_peer) {
3198 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
3199 p2p->sd_peer->flags &= ~P2P_DEV_SD_SCHEDULE;
3200 p2p->sd_peer = NULL;
3201 }
3202 p2p_continue_find(p2p);
3203}
3204
3205
3206static void p2p_timeout_prov_disc_during_find(struct p2p_data *p2p)
3207{
ed496f13 3208 p2p_dbg(p2p, "Provision Discovery Request timeout");
b22128ef
JM
3209 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
3210 p2p_continue_find(p2p);
3211}
3212
3213
6b56cc2d
JS
3214static void p2p_timeout_prov_disc_req(struct p2p_data *p2p)
3215{
3216 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
3217
3218 /*
3219 * For user initiated PD requests that we have not gotten any responses
3220 * for while in IDLE state, we retry them a couple of times before
3221 * giving up.
3222 */
3223 if (!p2p->user_initiated_pd)
3224 return;
3225
ed496f13 3226 p2p_dbg(p2p, "User initiated Provision Discovery Request timeout");
6b56cc2d
JS
3227
3228 if (p2p->pd_retries) {
3229 p2p->pd_retries--;
3230 p2p_retry_pd(p2p);
3231 } else {
175171ac
JM
3232 struct p2p_device *dev;
3233 int for_join = 0;
3234
3235 dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
3236 if (os_memcmp(p2p->pending_pd_devaddr,
3237 dev->info.p2p_device_addr, ETH_ALEN) != 0)
3238 continue;
3239 if (dev->req_config_methods &&
3240 (dev->flags & P2P_DEV_PD_FOR_JOIN))
3241 for_join = 1;
3242 }
3243
349b213c
JS
3244 if (p2p->cfg->prov_disc_fail)
3245 p2p->cfg->prov_disc_fail(p2p->cfg->cb_ctx,
3246 p2p->pending_pd_devaddr,
175171ac
JM
3247 for_join ?
3248 P2P_PROV_DISC_TIMEOUT_JOIN :
349b213c 3249 P2P_PROV_DISC_TIMEOUT);
6b56cc2d
JS
3250 p2p_reset_pending_pd(p2p);
3251 }
3252}
3253
3254
b22128ef
JM
3255static void p2p_timeout_invite(struct p2p_data *p2p)
3256{
3257 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
3258 p2p_set_state(p2p, P2P_INVITE_LISTEN);
04d8dad5
JM
3259 if (p2p->inv_role == P2P_INVITE_ROLE_ACTIVE_GO) {
3260 /*
3261 * Better remain on operating channel instead of listen channel
3262 * when running a group.
3263 */
ed496f13 3264 p2p_dbg(p2p, "Inviting in active GO role - wait on operating channel");
04d8dad5
JM
3265 p2p_set_timeout(p2p, 0, 100000);
3266 return;
3267 }
96beff11 3268 p2p_listen_in_find(p2p, 0);
b22128ef
JM
3269}
3270
3271
3272static void p2p_timeout_invite_listen(struct p2p_data *p2p)
3273{
3274 if (p2p->invite_peer && p2p->invite_peer->invitation_reqs < 100) {
3275 p2p_set_state(p2p, P2P_INVITE);
3276 p2p_invite_send(p2p, p2p->invite_peer,
3277 p2p->invite_go_dev_addr);
3278 } else {
3279 if (p2p->invite_peer) {
ed496f13 3280 p2p_dbg(p2p, "Invitation Request retry limit reached");
b22128ef
JM
3281 if (p2p->cfg->invitation_result)
3282 p2p->cfg->invitation_result(
dbca75f8 3283 p2p->cfg->cb_ctx, -1, NULL, NULL,
dcd25d4c
VKE
3284 p2p->invite_peer->info.p2p_device_addr,
3285 0);
b22128ef
JM
3286 }
3287 p2p_set_state(p2p, P2P_IDLE);
3288 }
3289}
3290
3291
3292static void p2p_state_timeout(void *eloop_ctx, void *timeout_ctx)
3293{
3294 struct p2p_data *p2p = eloop_ctx;
3295
ed496f13 3296 p2p_dbg(p2p, "Timeout (state=%s)", p2p_state_txt(p2p->state));
b22128ef
JM
3297
3298 p2p->in_listen = 0;
3299
3300 switch (p2p->state) {
3301 case P2P_IDLE:
6b56cc2d
JS
3302 /* Check if we timed out waiting for PD req */
3303 if (p2p->pending_action_state == P2P_PENDING_PD)
3304 p2p_timeout_prov_disc_req(p2p);
b22128ef
JM
3305 break;
3306 case P2P_SEARCH:
6b56cc2d
JS
3307 /* Check if we timed out waiting for PD req */
3308 if (p2p->pending_action_state == P2P_PENDING_PD)
3309 p2p_timeout_prov_disc_req(p2p);
37448ede 3310 if (p2p->search_delay && !p2p->in_search_delay) {
ed496f13 3311 p2p_dbg(p2p, "Delay search operation by %u ms",
37448ede
JM
3312 p2p->search_delay);
3313 p2p->in_search_delay = 1;
3314 p2p_set_timeout(p2p, p2p->search_delay / 1000,
3315 (p2p->search_delay % 1000) * 1000);
3316 break;
3317 }
3318 p2p->in_search_delay = 0;
b22128ef
JM
3319 p2p_search(p2p);
3320 break;
3321 case P2P_CONNECT:
3322 p2p_timeout_connect(p2p);
3323 break;
3324 case P2P_CONNECT_LISTEN:
3325 p2p_timeout_connect_listen(p2p);
3326 break;
3327 case P2P_GO_NEG:
3328 break;
3329 case P2P_LISTEN_ONLY:
6b56cc2d
JS
3330 /* Check if we timed out waiting for PD req */
3331 if (p2p->pending_action_state == P2P_PENDING_PD)
3332 p2p_timeout_prov_disc_req(p2p);
3333
b22128ef 3334 if (p2p->ext_listen_only) {
ed496f13 3335 p2p_dbg(p2p, "Extended Listen Timing - Listen State completed");
b22128ef
JM
3336 p2p->ext_listen_only = 0;
3337 p2p_set_state(p2p, P2P_IDLE);
3338 }
3339 break;
3340 case P2P_WAIT_PEER_CONNECT:
3341 p2p_timeout_wait_peer_connect(p2p);
3342 break;
3343 case P2P_WAIT_PEER_IDLE:
3344 p2p_timeout_wait_peer_idle(p2p);
3345 break;
3346 case P2P_SD_DURING_FIND:
3347 p2p_timeout_sd_during_find(p2p);
3348 break;
3349 case P2P_PROVISIONING:
3350 break;
3351 case P2P_PD_DURING_FIND:
3352 p2p_timeout_prov_disc_during_find(p2p);
3353 break;
3354 case P2P_INVITE:
3355 p2p_timeout_invite(p2p);
3356 break;
3357 case P2P_INVITE_LISTEN:
3358 p2p_timeout_invite_listen(p2p);
3359 break;
39185dfa
JM
3360 case P2P_SEARCH_WHEN_READY:
3361 break;
99fcd404
JM
3362 case P2P_CONTINUE_SEARCH_WHEN_READY:
3363 break;
b22128ef
JM
3364 }
3365}
3366
3367
3368int p2p_reject(struct p2p_data *p2p, const u8 *peer_addr)
3369{
3370 struct p2p_device *dev;
3371
3372 dev = p2p_get_device(p2p, peer_addr);
ed496f13
JM
3373 p2p_dbg(p2p, "Local request to reject connection attempts by peer "
3374 MACSTR, MAC2STR(peer_addr));
b22128ef 3375 if (dev == NULL) {
ed496f13 3376 p2p_dbg(p2p, "Peer " MACSTR " unknown", MAC2STR(peer_addr));
b22128ef
JM
3377 return -1;
3378 }
3379 dev->status = P2P_SC_FAIL_REJECTED_BY_USER;
3380 dev->flags |= P2P_DEV_USER_REJECTED;
3381 return 0;
3382}
3383
3384
e5a359cf 3385const char * p2p_wps_method_text(enum p2p_wps_method method)
b22128ef
JM
3386{
3387 switch (method) {
3388 case WPS_NOT_READY:
3389 return "not-ready";
b22128ef
JM
3390 case WPS_PIN_DISPLAY:
3391 return "Display";
3392 case WPS_PIN_KEYPAD:
3393 return "Keypad";
3394 case WPS_PBC:
3395 return "PBC";
3396 }
3397
3398 return "??";
3399}
3400
3401
3402static const char * p2p_go_state_text(enum p2p_go_state go_state)
3403{
3404 switch (go_state) {
3405 case UNKNOWN_GO:
3406 return "unknown";
3407 case LOCAL_GO:
3408 return "local";
3409 case REMOTE_GO:
3410 return "remote";
3411 }
3412
3413 return "??";
3414}
3415
3416
b3ffc80b
JM
3417const struct p2p_peer_info * p2p_get_peer_info(struct p2p_data *p2p,
3418 const u8 *addr, int next)
b22128ef
JM
3419{
3420 struct p2p_device *dev;
b22128ef
JM
3421
3422 if (addr)
3423 dev = p2p_get_device(p2p, addr);
3424 else
3425 dev = dl_list_first(&p2p->devices, struct p2p_device, list);
3426
3427 if (dev && next) {
3428 dev = dl_list_first(&dev->list, struct p2p_device, list);
3429 if (&dev->list == &p2p->devices)
3430 dev = NULL;
3431 }
3432
3433 if (dev == NULL)
b3ffc80b
JM
3434 return NULL;
3435
3436 return &dev->info;
3437}
3438
3439
3440int p2p_get_peer_info_txt(const struct p2p_peer_info *info,
3441 char *buf, size_t buflen)
3442{
3443 struct p2p_device *dev;
3444 int res;
3445 char *pos, *end;
3446 struct os_time now;
3447
3448 if (info == NULL)
b22128ef
JM
3449 return -1;
3450
b3ffc80b
JM
3451 dev = (struct p2p_device *) (((u8 *) info) -
3452 offsetof(struct p2p_device, info));
3453
b22128ef
JM
3454 pos = buf;
3455 end = buf + buflen;
3456
b22128ef
JM
3457 os_get_time(&now);
3458 res = os_snprintf(pos, end - pos,
3459 "age=%d\n"
3460 "listen_freq=%d\n"
b22128ef
JM
3461 "wps_method=%s\n"
3462 "interface_addr=" MACSTR "\n"
3463 "member_in_go_dev=" MACSTR "\n"
3464 "member_in_go_iface=" MACSTR "\n"
b22128ef
JM
3465 "go_neg_req_sent=%d\n"
3466 "go_state=%s\n"
3467 "dialog_token=%u\n"
3468 "intended_addr=" MACSTR "\n"
3469 "country=%c%c\n"
3470 "oper_freq=%d\n"
3471 "req_config_methods=0x%x\n"
10c4edde 3472 "flags=%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n"
b22128ef
JM
3473 "status=%d\n"
3474 "wait_count=%u\n"
3475 "invitation_reqs=%u\n",
3476 (int) (now.sec - dev->last_seen.sec),
3477 dev->listen_freq,
b22128ef
JM
3478 p2p_wps_method_text(dev->wps_method),
3479 MAC2STR(dev->interface_addr),
3480 MAC2STR(dev->member_in_go_dev),
3481 MAC2STR(dev->member_in_go_iface),
b22128ef
JM
3482 dev->go_neg_req_sent,
3483 p2p_go_state_text(dev->go_state),
3484 dev->dialog_token,
3485 MAC2STR(dev->intended_addr),
3486 dev->country[0] ? dev->country[0] : '_',
3487 dev->country[1] ? dev->country[1] : '_',
3488 dev->oper_freq,
3489 dev->req_config_methods,
3490 dev->flags & P2P_DEV_PROBE_REQ_ONLY ?
3491 "[PROBE_REQ_ONLY]" : "",
3492 dev->flags & P2P_DEV_REPORTED ? "[REPORTED]" : "",
3493 dev->flags & P2P_DEV_NOT_YET_READY ?
3494 "[NOT_YET_READY]" : "",
3495 dev->flags & P2P_DEV_SD_INFO ? "[SD_INFO]" : "",
3496 dev->flags & P2P_DEV_SD_SCHEDULE ? "[SD_SCHEDULE]" :
3497 "",
3498 dev->flags & P2P_DEV_PD_PEER_DISPLAY ?
3499 "[PD_PEER_DISPLAY]" : "",
3500 dev->flags & P2P_DEV_PD_PEER_KEYPAD ?
3501 "[PD_PEER_KEYPAD]" : "",
3502 dev->flags & P2P_DEV_USER_REJECTED ?
3503 "[USER_REJECTED]" : "",
3504 dev->flags & P2P_DEV_PEER_WAITING_RESPONSE ?
3505 "[PEER_WAITING_RESPONSE]" : "",
3506 dev->flags & P2P_DEV_PREFER_PERSISTENT_GROUP ?
3507 "[PREFER_PERSISTENT_GROUP]" : "",
3508 dev->flags & P2P_DEV_WAIT_GO_NEG_RESPONSE ?
3509 "[WAIT_GO_NEG_RESPONSE]" : "",
3510 dev->flags & P2P_DEV_WAIT_GO_NEG_CONFIRM ?
3511 "[WAIT_GO_NEG_CONFIRM]" : "",
3512 dev->flags & P2P_DEV_GROUP_CLIENT_ONLY ?
3513 "[GROUP_CLIENT_ONLY]" : "",
d5b20a73 3514 dev->flags & P2P_DEV_FORCE_FREQ ?
10c4edde
JM
3515 "[FORCE_FREQ]" : "",
3516 dev->flags & P2P_DEV_PD_FOR_JOIN ?
3517 "[PD_FOR_JOIN]" : "",
b22128ef
JM
3518 dev->status,
3519 dev->wait_count,
3520 dev->invitation_reqs);
3521 if (res < 0 || res >= end - pos)
3522 return pos - buf;
3523 pos += res;
3524
3525 if (dev->ext_listen_period) {
3526 res = os_snprintf(pos, end - pos,
3527 "ext_listen_period=%u\n"
3528 "ext_listen_interval=%u\n",
3529 dev->ext_listen_period,
3530 dev->ext_listen_interval);
3531 if (res < 0 || res >= end - pos)
3532 return pos - buf;
3533 pos += res;
3534 }
3535
3536 if (dev->oper_ssid_len) {
3537 res = os_snprintf(pos, end - pos,
3538 "oper_ssid=%s\n",
3539 wpa_ssid_txt(dev->oper_ssid,
3540 dev->oper_ssid_len));
3541 if (res < 0 || res >= end - pos)
3542 return pos - buf;
3543 pos += res;
3544 }
3545
9675ce35
JM
3546#ifdef CONFIG_WIFI_DISPLAY
3547 if (dev->info.wfd_subelems) {
3548 res = os_snprintf(pos, end - pos, "wfd_subelems=");
3549 if (res < 0 || res >= end - pos)
3550 return pos - buf;
3551 pos += res;
3552
3553 pos += wpa_snprintf_hex(pos, end - pos,
3554 wpabuf_head(dev->info.wfd_subelems),
3555 wpabuf_len(dev->info.wfd_subelems));
3556
3557 res = os_snprintf(pos, end - pos, "\n");
3558 if (res < 0 || res >= end - pos)
3559 return pos - buf;
3560 pos += res;
3561 }
3562#endif /* CONFIG_WIFI_DISPLAY */
3563
b22128ef
JM
3564 return pos - buf;
3565}
3566
3567
b3bcc0f5
JM
3568int p2p_peer_known(struct p2p_data *p2p, const u8 *addr)
3569{
3570 return p2p_get_device(p2p, addr) != NULL;
3571}
3572
3573
b22128ef
JM
3574void p2p_set_client_discoverability(struct p2p_data *p2p, int enabled)
3575{
3576 if (enabled) {
ed496f13 3577 p2p_dbg(p2p, "Client discoverability enabled");
b22128ef
JM
3578 p2p->dev_capab |= P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
3579 } else {
ed496f13 3580 p2p_dbg(p2p, "Client discoverability disabled");
b22128ef
JM
3581 p2p->dev_capab &= ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
3582 }
3583}
3584
3585
3586static struct wpabuf * p2p_build_presence_req(u32 duration1, u32 interval1,
3587 u32 duration2, u32 interval2)
3588{
3589 struct wpabuf *req;
3590 struct p2p_noa_desc desc1, desc2, *ptr1 = NULL, *ptr2 = NULL;
3591 u8 *len;
3592
3593 req = wpabuf_alloc(100);
3594 if (req == NULL)
3595 return NULL;
3596
3597 if (duration1 || interval1) {
3598 os_memset(&desc1, 0, sizeof(desc1));
3599 desc1.count_type = 1;
3600 desc1.duration = duration1;
3601 desc1.interval = interval1;
3602 ptr1 = &desc1;
3603
3604 if (duration2 || interval2) {
3605 os_memset(&desc2, 0, sizeof(desc2));
3606 desc2.count_type = 2;
3607 desc2.duration = duration2;
3608 desc2.interval = interval2;
3609 ptr2 = &desc2;
3610 }
3611 }
3612
3613 p2p_buf_add_action_hdr(req, P2P_PRESENCE_REQ, 1);
3614 len = p2p_buf_add_ie_hdr(req);
3615 p2p_buf_add_noa(req, 0, 0, 0, ptr1, ptr2);
3616 p2p_buf_update_ie_hdr(req, len);
3617
3618 return req;
3619}
3620
3621
3622int p2p_presence_req(struct p2p_data *p2p, const u8 *go_interface_addr,
3623 const u8 *own_interface_addr, unsigned int freq,
3624 u32 duration1, u32 interval1, u32 duration2,
3625 u32 interval2)
3626{
3627 struct wpabuf *req;
3628
ed496f13
JM
3629 p2p_dbg(p2p, "Send Presence Request to GO " MACSTR
3630 " (own interface " MACSTR ") freq=%u dur1=%u int1=%u "
3631 "dur2=%u int2=%u",
b22128ef
JM
3632 MAC2STR(go_interface_addr), MAC2STR(own_interface_addr),
3633 freq, duration1, interval1, duration2, interval2);
3634
3635 req = p2p_build_presence_req(duration1, interval1, duration2,
3636 interval2);
3637 if (req == NULL)
3638 return -1;
3639
3640 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
3f9285ff
JM
3641 if (p2p_send_action(p2p, freq, go_interface_addr, own_interface_addr,
3642 go_interface_addr,
3643 wpabuf_head(req), wpabuf_len(req), 200) < 0) {
ed496f13 3644 p2p_dbg(p2p, "Failed to send Action frame");
b22128ef
JM
3645 }
3646 wpabuf_free(req);
3647
3648 return 0;
3649}
3650
3651
3652static struct wpabuf * p2p_build_presence_resp(u8 status, const u8 *noa,
3653 size_t noa_len, u8 dialog_token)
3654{
3655 struct wpabuf *resp;
3656 u8 *len;
3657
3658 resp = wpabuf_alloc(100 + noa_len);
3659 if (resp == NULL)
3660 return NULL;
3661
3662 p2p_buf_add_action_hdr(resp, P2P_PRESENCE_RESP, dialog_token);
3663 len = p2p_buf_add_ie_hdr(resp);
3664 p2p_buf_add_status(resp, status);
3665 if (noa) {
3666 wpabuf_put_u8(resp, P2P_ATTR_NOTICE_OF_ABSENCE);
3667 wpabuf_put_le16(resp, noa_len);
3668 wpabuf_put_data(resp, noa, noa_len);
3669 } else
3670 p2p_buf_add_noa(resp, 0, 0, 0, NULL, NULL);
3671 p2p_buf_update_ie_hdr(resp, len);
3672
3673 return resp;
3674}
3675
3676
3677static void p2p_process_presence_req(struct p2p_data *p2p, const u8 *da,
3678 const u8 *sa, const u8 *data, size_t len,
3679 int rx_freq)
3680{
3681 struct p2p_message msg;
3682 u8 status;
3683 struct wpabuf *resp;
3684 size_t g;
3685 struct p2p_group *group = NULL;
3686 int parsed = 0;
3687 u8 noa[50];
3688 int noa_len;
3689
ed496f13 3690 p2p_dbg(p2p, "Received P2P Action - P2P Presence Request");
b22128ef
JM
3691
3692 for (g = 0; g < p2p->num_groups; g++) {
3693 if (os_memcmp(da, p2p_group_get_interface_addr(p2p->groups[g]),
3694 ETH_ALEN) == 0) {
3695 group = p2p->groups[g];
3696 break;
3697 }
3698 }
3699 if (group == NULL) {
ed496f13 3700 p2p_dbg(p2p, "Ignore P2P Presence Request for unknown group "
b22128ef
JM
3701 MACSTR, MAC2STR(da));
3702 return;
3703 }
3704
3705 if (p2p_parse(data, len, &msg) < 0) {
ed496f13 3706 p2p_dbg(p2p, "Failed to parse P2P Presence Request");
b22128ef
JM
3707 status = P2P_SC_FAIL_INVALID_PARAMS;
3708 goto fail;
3709 }
3710 parsed = 1;
3711
3712 if (msg.noa == NULL) {
ed496f13 3713 p2p_dbg(p2p, "No NoA attribute in P2P Presence Request");
b22128ef
JM
3714 status = P2P_SC_FAIL_INVALID_PARAMS;
3715 goto fail;
3716 }
3717
3718 status = p2p_group_presence_req(group, sa, msg.noa, msg.noa_len);
3719
3720fail:
3721 if (p2p->cfg->get_noa)
3722 noa_len = p2p->cfg->get_noa(p2p->cfg->cb_ctx, da, noa,
3723 sizeof(noa));
3724 else
3725 noa_len = -1;
3726 resp = p2p_build_presence_resp(status, noa_len > 0 ? noa : NULL,
3727 noa_len > 0 ? noa_len : 0,
3728 msg.dialog_token);
3729 if (parsed)
3730 p2p_parse_free(&msg);
3731 if (resp == NULL)
3732 return;
3733
3734 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
3f9285ff
JM
3735 if (p2p_send_action(p2p, rx_freq, sa, da, da,
3736 wpabuf_head(resp), wpabuf_len(resp), 200) < 0) {
ed496f13 3737 p2p_dbg(p2p, "Failed to send Action frame");
b22128ef
JM
3738 }
3739 wpabuf_free(resp);
3740}
3741
3742
3743static void p2p_process_presence_resp(struct p2p_data *p2p, const u8 *da,
3744 const u8 *sa, const u8 *data, size_t len)
3745{
3746 struct p2p_message msg;
3747
ed496f13 3748 p2p_dbg(p2p, "Received P2P Action - P2P Presence Response");
b22128ef
JM
3749
3750 if (p2p_parse(data, len, &msg) < 0) {
ed496f13 3751 p2p_dbg(p2p, "Failed to parse P2P Presence Response");
b22128ef
JM
3752 return;
3753 }
3754
3755 if (msg.status == NULL || msg.noa == NULL) {
ed496f13 3756 p2p_dbg(p2p, "No Status or NoA attribute in P2P Presence Response");
b22128ef
JM
3757 p2p_parse_free(&msg);
3758 return;
3759 }
3760
3761 if (*msg.status) {
ed496f13 3762 p2p_dbg(p2p, "P2P Presence Request was rejected: status %u",
b22128ef
JM
3763 *msg.status);
3764 p2p_parse_free(&msg);
3765 return;
3766 }
3767
ed496f13 3768 p2p_dbg(p2p, "P2P Presence Request was accepted");
b22128ef
JM
3769 wpa_hexdump(MSG_DEBUG, "P2P: P2P Presence Response - NoA",
3770 msg.noa, msg.noa_len);
3771 /* TODO: process NoA */
3772 p2p_parse_free(&msg);
3773}
3774
3775
3776static void p2p_ext_listen_timeout(void *eloop_ctx, void *timeout_ctx)
3777{
3778 struct p2p_data *p2p = eloop_ctx;
3779
3780 if (p2p->ext_listen_interval) {
3781 /* Schedule next extended listen timeout */
3782 eloop_register_timeout(p2p->ext_listen_interval_sec,
3783 p2p->ext_listen_interval_usec,
3784 p2p_ext_listen_timeout, p2p, NULL);
3785 }
3786
f7a69057
JM
3787 if (p2p->state == P2P_LISTEN_ONLY && p2p->ext_listen_only) {
3788 /*
3789 * This should not really happen, but it looks like the Listen
3790 * command may fail is something else (e.g., a scan) was
3791 * running at an inconvenient time. As a workaround, allow new
3792 * Extended Listen operation to be started.
3793 */
ed496f13 3794 p2p_dbg(p2p, "Previous Extended Listen operation had not been completed - try again");
f7a69057
JM
3795 p2p->ext_listen_only = 0;
3796 p2p_set_state(p2p, P2P_IDLE);
3797 }
3798
b22128ef 3799 if (p2p->state != P2P_IDLE) {
ed496f13 3800 p2p_dbg(p2p, "Skip Extended Listen timeout in active state (%s)", p2p_state_txt(p2p->state));
b22128ef
JM
3801 return;
3802 }
3803
ed496f13 3804 p2p_dbg(p2p, "Extended Listen timeout");
b22128ef
JM
3805 p2p->ext_listen_only = 1;
3806 if (p2p_listen(p2p, p2p->ext_listen_period) < 0) {
ed496f13 3807 p2p_dbg(p2p, "Failed to start Listen state for Extended Listen Timing");
b22128ef
JM
3808 p2p->ext_listen_only = 0;
3809 }
3810}
3811
3812
3813int p2p_ext_listen(struct p2p_data *p2p, unsigned int period,
3814 unsigned int interval)
3815{
3816 if (period > 65535 || interval > 65535 || period > interval ||
3817 (period == 0 && interval > 0) || (period > 0 && interval == 0)) {
ed496f13
JM
3818 p2p_dbg(p2p, "Invalid Extended Listen Timing request: period=%u interval=%u",
3819 period, interval);
b22128ef
JM
3820 return -1;
3821 }
3822
3823 eloop_cancel_timeout(p2p_ext_listen_timeout, p2p, NULL);
3824
3825 if (interval == 0) {
ed496f13 3826 p2p_dbg(p2p, "Disabling Extended Listen Timing");
b22128ef
JM
3827 p2p->ext_listen_period = 0;
3828 p2p->ext_listen_interval = 0;
3829 return 0;
3830 }
3831
ed496f13
JM
3832 p2p_dbg(p2p, "Enabling Extended Listen Timing: period %u msec, interval %u msec",
3833 period, interval);
b22128ef
JM
3834 p2p->ext_listen_period = period;
3835 p2p->ext_listen_interval = interval;
3836 p2p->ext_listen_interval_sec = interval / 1000;
3837 p2p->ext_listen_interval_usec = (interval % 1000) * 1000;
3838
3839 eloop_register_timeout(p2p->ext_listen_interval_sec,
3840 p2p->ext_listen_interval_usec,
3841 p2p_ext_listen_timeout, p2p, NULL);
3842
3843 return 0;
3844}
3845
3846
3847void p2p_deauth_notif(struct p2p_data *p2p, const u8 *bssid, u16 reason_code,
3848 const u8 *ie, size_t ie_len)
3849{
3850 struct p2p_message msg;
3851
3852 if (bssid == NULL || ie == NULL)
3853 return;
3854
3855 os_memset(&msg, 0, sizeof(msg));
3856 if (p2p_parse_ies(ie, ie_len, &msg))
3857 return;
3858 if (msg.minor_reason_code == NULL)
3859 return;
3860
ed496f13 3861 p2p_dbg(p2p, "Deauthentication notification BSSID " MACSTR
b22128ef
JM
3862 " reason_code=%u minor_reason_code=%u",
3863 MAC2STR(bssid), reason_code, *msg.minor_reason_code);
3864
3865 p2p_parse_free(&msg);
3866}
3867
3868
3869void p2p_disassoc_notif(struct p2p_data *p2p, const u8 *bssid, u16 reason_code,
3870 const u8 *ie, size_t ie_len)
3871{
3872 struct p2p_message msg;
3873
3874 if (bssid == NULL || ie == NULL)
3875 return;
3876
3877 os_memset(&msg, 0, sizeof(msg));
3878 if (p2p_parse_ies(ie, ie_len, &msg))
3879 return;
3880 if (msg.minor_reason_code == NULL)
3881 return;
3882
ed496f13 3883 p2p_dbg(p2p, "Disassociation notification BSSID " MACSTR
b22128ef
JM
3884 " reason_code=%u minor_reason_code=%u",
3885 MAC2STR(bssid), reason_code, *msg.minor_reason_code);
3886
3887 p2p_parse_free(&msg);
3888}
3889
3890
3891void p2p_set_managed_oper(struct p2p_data *p2p, int enabled)
3892{
3893 if (enabled) {
ed496f13 3894 p2p_dbg(p2p, "Managed P2P Device operations enabled");
b22128ef
JM
3895 p2p->dev_capab |= P2P_DEV_CAPAB_INFRA_MANAGED;
3896 } else {
ed496f13 3897 p2p_dbg(p2p, "Managed P2P Device operations disabled");
b22128ef
JM
3898 p2p->dev_capab &= ~P2P_DEV_CAPAB_INFRA_MANAGED;
3899 }
3900}
3901
3902
3903int p2p_set_listen_channel(struct p2p_data *p2p, u8 reg_class, u8 channel)
3904{
9ccd9165 3905 if (p2p_channel_to_freq(reg_class, channel) < 0)
b22128ef
JM
3906 return -1;
3907
ed496f13
JM
3908 p2p_dbg(p2p, "Set Listen channel: reg_class %u channel %u",
3909 reg_class, channel);
b22128ef
JM
3910 p2p->cfg->reg_class = reg_class;
3911 p2p->cfg->channel = channel;
3912
3913 return 0;
3914}
3915
3916
3917int p2p_set_ssid_postfix(struct p2p_data *p2p, const u8 *postfix, size_t len)
3918{
ed496f13 3919 p2p_dbg(p2p, "New SSID postfix: %s", wpa_ssid_txt(postfix, len));
b22128ef
JM
3920 if (postfix == NULL) {
3921 p2p->cfg->ssid_postfix_len = 0;
3922 return 0;
3923 }
3924 if (len > sizeof(p2p->cfg->ssid_postfix))
3925 return -1;
3926 os_memcpy(p2p->cfg->ssid_postfix, postfix, len);
3927 p2p->cfg->ssid_postfix_len = len;
3928 return 0;
3929}
3930
3931
2463ba70
JS
3932int p2p_set_oper_channel(struct p2p_data *p2p, u8 op_reg_class, u8 op_channel,
3933 int cfg_op_channel)
3934{
9ccd9165 3935 if (p2p_channel_to_freq(op_reg_class, op_channel) < 0)
2463ba70
JS
3936 return -1;
3937
ed496f13
JM
3938 p2p_dbg(p2p, "Set Operating channel: reg_class %u channel %u",
3939 op_reg_class, op_channel);
2463ba70
JS
3940 p2p->cfg->op_reg_class = op_reg_class;
3941 p2p->cfg->op_channel = op_channel;
3942 p2p->cfg->cfg_op_channel = cfg_op_channel;
3943 return 0;
3944}
3945
3946
21d996f7
JM
3947int p2p_set_pref_chan(struct p2p_data *p2p, unsigned int num_pref_chan,
3948 const struct p2p_channel *pref_chan)
3949{
3950 struct p2p_channel *n;
3951
3952 if (pref_chan) {
3953 n = os_malloc(num_pref_chan * sizeof(struct p2p_channel));
3954 if (n == NULL)
3955 return -1;
3956 os_memcpy(n, pref_chan,
3957 num_pref_chan * sizeof(struct p2p_channel));
3958 } else
3959 n = NULL;
3960
3961 os_free(p2p->cfg->pref_chan);
3962 p2p->cfg->pref_chan = n;
3963 p2p->cfg->num_pref_chan = num_pref_chan;
3964
3965 return 0;
3966}
3967
3968
556b30da
JM
3969int p2p_set_no_go_freq(struct p2p_data *p2p,
3970 const struct wpa_freq_range_list *list)
3971{
3972 struct wpa_freq_range *tmp;
3973
3974 if (list == NULL || list->num == 0) {
3975 os_free(p2p->no_go_freq.range);
3976 p2p->no_go_freq.range = NULL;
3977 p2p->no_go_freq.num = 0;
3978 return 0;
3979 }
3980
3981 tmp = os_calloc(list->num, sizeof(struct wpa_freq_range));
3982 if (tmp == NULL)
3983 return -1;
3984 os_memcpy(tmp, list->range, list->num * sizeof(struct wpa_freq_range));
3985 os_free(p2p->no_go_freq.range);
3986 p2p->no_go_freq.range = tmp;
3987 p2p->no_go_freq.num = list->num;
3988 p2p_dbg(p2p, "Updated no GO chan list");
3989
3990 return 0;
3991}
3992
3993
b22128ef
JM
3994int p2p_get_interface_addr(struct p2p_data *p2p, const u8 *dev_addr,
3995 u8 *iface_addr)
3996{
3997 struct p2p_device *dev = p2p_get_device(p2p, dev_addr);
3998 if (dev == NULL || is_zero_ether_addr(dev->interface_addr))
3999 return -1;
4000 os_memcpy(iface_addr, dev->interface_addr, ETH_ALEN);
4001 return 0;
4002}
80c9582a
JM
4003
4004
4147a2cc
JM
4005int p2p_get_dev_addr(struct p2p_data *p2p, const u8 *iface_addr,
4006 u8 *dev_addr)
4007{
4008 struct p2p_device *dev = p2p_get_device_interface(p2p, iface_addr);
4009 if (dev == NULL)
4010 return -1;
c5db8e51 4011 os_memcpy(dev_addr, dev->info.p2p_device_addr, ETH_ALEN);
4147a2cc
JM
4012 return 0;
4013}
4014
4015
80c9582a
JM
4016void p2p_set_peer_filter(struct p2p_data *p2p, const u8 *addr)
4017{
4018 os_memcpy(p2p->peer_filter, addr, ETH_ALEN);
4019 if (is_zero_ether_addr(p2p->peer_filter))
ed496f13 4020 p2p_dbg(p2p, "Disable peer filter");
80c9582a 4021 else
ed496f13
JM
4022 p2p_dbg(p2p, "Enable peer filter for " MACSTR,
4023 MAC2STR(p2p->peer_filter));
80c9582a 4024}
72044390
JM
4025
4026
4027void p2p_set_cross_connect(struct p2p_data *p2p, int enabled)
4028{
ed496f13 4029 p2p_dbg(p2p, "Cross connection %s", enabled ? "enabled" : "disabled");
72044390
JM
4030 if (p2p->cross_connect == enabled)
4031 return;
4032 p2p->cross_connect = enabled;
4033 /* TODO: may need to tear down any action group where we are GO(?) */
4034}
f8d0131a
JM
4035
4036
4037int p2p_get_oper_freq(struct p2p_data *p2p, const u8 *iface_addr)
4038{
4039 struct p2p_device *dev = p2p_get_device_interface(p2p, iface_addr);
4040 if (dev == NULL)
4041 return -1;
4042 if (dev->oper_freq <= 0)
4043 return -1;
4044 return dev->oper_freq;
4045}
0f66abd2
SS
4046
4047
4048void p2p_set_intra_bss_dist(struct p2p_data *p2p, int enabled)
4049{
ed496f13 4050 p2p_dbg(p2p, "Intra BSS distribution %s",
0f66abd2
SS
4051 enabled ? "enabled" : "disabled");
4052 p2p->cfg->p2p_intra_bss = enabled;
4053}
b5c9da8d
JM
4054
4055
51e9f228
JM
4056void p2p_update_channel_list(struct p2p_data *p2p,
4057 const struct p2p_channels *chan,
4058 const struct p2p_channels *cli_chan)
b5c9da8d 4059{
ed496f13 4060 p2p_dbg(p2p, "Update channel list");
b5c9da8d 4061 os_memcpy(&p2p->cfg->channels, chan, sizeof(struct p2p_channels));
51e9f228
JM
4062 p2p_channels_dump(p2p, "channels", &p2p->cfg->channels);
4063 os_memcpy(&p2p->cfg->cli_channels, cli_chan,
4064 sizeof(struct p2p_channels));
4065 p2p_channels_dump(p2p, "cli_channels", &p2p->cfg->cli_channels);
b5c9da8d 4066}
3f9285ff
JM
4067
4068
4069int p2p_send_action(struct p2p_data *p2p, unsigned int freq, const u8 *dst,
4070 const u8 *src, const u8 *bssid, const u8 *buf,
4071 size_t len, unsigned int wait_time)
4072{
4073 if (p2p->p2p_scan_running) {
ed496f13 4074 p2p_dbg(p2p, "Delay Action frame TX until p2p_scan completes");
3f9285ff 4075 if (p2p->after_scan_tx) {
ed496f13 4076 p2p_dbg(p2p, "Dropped previous pending Action frame TX");
3f9285ff
JM
4077 os_free(p2p->after_scan_tx);
4078 }
4079 p2p->after_scan_tx = os_malloc(sizeof(*p2p->after_scan_tx) +
4080 len);
4081 if (p2p->after_scan_tx == NULL)
4082 return -1;
4083 p2p->after_scan_tx->freq = freq;
4084 os_memcpy(p2p->after_scan_tx->dst, dst, ETH_ALEN);
4085 os_memcpy(p2p->after_scan_tx->src, src, ETH_ALEN);
4086 os_memcpy(p2p->after_scan_tx->bssid, bssid, ETH_ALEN);
4087 p2p->after_scan_tx->len = len;
4088 p2p->after_scan_tx->wait_time = wait_time;
4089 os_memcpy(p2p->after_scan_tx + 1, buf, len);
4090 return 0;
4091 }
4092
4093 return p2p->cfg->send_action(p2p->cfg->cb_ctx, freq, dst, src, bssid,
4094 buf, len, wait_time);
4095}
7cfc4ac3
AGS
4096
4097
4098void p2p_set_best_channels(struct p2p_data *p2p, int freq_24, int freq_5,
4099 int freq_overall)
4100{
ed496f13
JM
4101 p2p_dbg(p2p, "Best channel: 2.4 GHz: %d, 5 GHz: %d, overall: %d",
4102 freq_24, freq_5, freq_overall);
7cfc4ac3
AGS
4103 p2p->best_freq_24 = freq_24;
4104 p2p->best_freq_5 = freq_5;
4105 p2p->best_freq_overall = freq_overall;
4106}
231bbd03
SS
4107
4108
6cb27aa8
JM
4109void p2p_set_own_freq_preference(struct p2p_data *p2p, int freq)
4110{
ed496f13 4111 p2p_dbg(p2p, "Own frequency preference: %d MHz", freq);
6cb27aa8
JM
4112 p2p->own_freq_preference = freq;
4113}
4114
4115
231bbd03
SS
4116const u8 * p2p_get_go_neg_peer(struct p2p_data *p2p)
4117{
4118 if (p2p == NULL || p2p->go_neg_peer == NULL)
4119 return NULL;
c5db8e51 4120 return p2p->go_neg_peer->info.p2p_device_addr;
231bbd03 4121}
c165d81e
JB
4122
4123
4124const struct p2p_peer_info *
4125p2p_get_peer_found(struct p2p_data *p2p, const u8 *addr, int next)
4126{
4127 struct p2p_device *dev;
4128
4129 if (addr) {
4130 dev = p2p_get_device(p2p, addr);
4131 if (!dev)
4132 return NULL;
4133
4134 if (!next) {
4135 if (dev->flags & P2P_DEV_PROBE_REQ_ONLY)
4136 return NULL;
4137
4138 return &dev->info;
4139 } else {
4140 do {
4141 dev = dl_list_first(&dev->list,
4142 struct p2p_device,
4143 list);
4144 if (&dev->list == &p2p->devices)
4145 return NULL;
4146 } while (dev->flags & P2P_DEV_PROBE_REQ_ONLY);
4147 }
4148 } else {
4149 dev = dl_list_first(&p2p->devices, struct p2p_device, list);
4150 if (!dev)
4151 return NULL;
4152 while (dev->flags & P2P_DEV_PROBE_REQ_ONLY) {
4153 dev = dl_list_first(&dev->list,
4154 struct p2p_device,
4155 list);
4156 if (&dev->list == &p2p->devices)
4157 return NULL;
4158 }
4159 }
4160
4161 return &dev->info;
4162}
303f60d3
JM
4163
4164
4165int p2p_in_progress(struct p2p_data *p2p)
4166{
4167 if (p2p == NULL)
4168 return 0;
99fcd404
JM
4169 if (p2p->state == P2P_SEARCH || p2p->state == P2P_SEARCH_WHEN_READY ||
4170 p2p->state == P2P_CONTINUE_SEARCH_WHEN_READY)
4171 return 2;
fc6997b3 4172 return p2p->state != P2P_IDLE && p2p->state != P2P_PROVISIONING;
303f60d3 4173}
4f219667
JM
4174
4175
4176void p2p_set_config_timeout(struct p2p_data *p2p, u8 go_timeout,
4177 u8 client_timeout)
4178{
4179 if (p2p) {
4180 p2p->go_timeout = go_timeout;
4181 p2p->client_timeout = client_timeout;
4182 }
4183}
99fcd404
JM
4184
4185
4186void p2p_increase_search_delay(struct p2p_data *p2p, unsigned int delay)
4187{
4188 if (p2p && p2p->search_delay < delay)
4189 p2p->search_delay = delay;
4190}
9675ce35
JM
4191
4192
4193#ifdef CONFIG_WIFI_DISPLAY
4194
4195static void p2p_update_wfd_ie_groups(struct p2p_data *p2p)
4196{
4197 size_t g;
4198 struct p2p_group *group;
4199
4200 for (g = 0; g < p2p->num_groups; g++) {
4201 group = p2p->groups[g];
3342c263 4202 p2p_group_force_beacon_update_ies(group);
9675ce35
JM
4203 }
4204}
4205
4206
4207int p2p_set_wfd_ie_beacon(struct p2p_data *p2p, struct wpabuf *ie)
4208{
4209 wpabuf_free(p2p->wfd_ie_beacon);
4210 p2p->wfd_ie_beacon = ie;
4211 p2p_update_wfd_ie_groups(p2p);
4212 return 0;
4213}
4214
4215
4216int p2p_set_wfd_ie_probe_req(struct p2p_data *p2p, struct wpabuf *ie)
4217{
4218 wpabuf_free(p2p->wfd_ie_probe_req);
4219 p2p->wfd_ie_probe_req = ie;
4220 return 0;
4221}
4222
4223
4224int p2p_set_wfd_ie_probe_resp(struct p2p_data *p2p, struct wpabuf *ie)
4225{
4226 wpabuf_free(p2p->wfd_ie_probe_resp);
4227 p2p->wfd_ie_probe_resp = ie;
4228 p2p_update_wfd_ie_groups(p2p);
4229 return 0;
4230}
4231
4232
4233int p2p_set_wfd_ie_assoc_req(struct p2p_data *p2p, struct wpabuf *ie)
4234{
4235 wpabuf_free(p2p->wfd_ie_assoc_req);
4236 p2p->wfd_ie_assoc_req = ie;
4237 return 0;
4238}
4239
4240
4241int p2p_set_wfd_ie_invitation(struct p2p_data *p2p, struct wpabuf *ie)
4242{
4243 wpabuf_free(p2p->wfd_ie_invitation);
4244 p2p->wfd_ie_invitation = ie;
4245 return 0;
4246}
4247
4248
4249int p2p_set_wfd_ie_prov_disc_req(struct p2p_data *p2p, struct wpabuf *ie)
4250{
4251 wpabuf_free(p2p->wfd_ie_prov_disc_req);
4252 p2p->wfd_ie_prov_disc_req = ie;
4253 return 0;
4254}
4255
4256
4257int p2p_set_wfd_ie_prov_disc_resp(struct p2p_data *p2p, struct wpabuf *ie)
4258{
4259 wpabuf_free(p2p->wfd_ie_prov_disc_resp);
4260 p2p->wfd_ie_prov_disc_resp = ie;
4261 return 0;
4262}
4263
4264
4265int p2p_set_wfd_ie_go_neg(struct p2p_data *p2p, struct wpabuf *ie)
4266{
4267 wpabuf_free(p2p->wfd_ie_go_neg);
4268 p2p->wfd_ie_go_neg = ie;
4269 return 0;
4270}
4271
4272
4273int p2p_set_wfd_dev_info(struct p2p_data *p2p, const struct wpabuf *elem)
4274{
4275 wpabuf_free(p2p->wfd_dev_info);
4276 if (elem) {
4277 p2p->wfd_dev_info = wpabuf_dup(elem);
4278 if (p2p->wfd_dev_info == NULL)
4279 return -1;
4280 } else
4281 p2p->wfd_dev_info = NULL;
4282
4283 return 0;
4284}
4285
4286
4287int p2p_set_wfd_assoc_bssid(struct p2p_data *p2p, const struct wpabuf *elem)
4288{
4289 wpabuf_free(p2p->wfd_assoc_bssid);
4290 if (elem) {
4291 p2p->wfd_assoc_bssid = wpabuf_dup(elem);
4292 if (p2p->wfd_assoc_bssid == NULL)
4293 return -1;
4294 } else
4295 p2p->wfd_assoc_bssid = NULL;
4296
4297 return 0;
4298}
4299
4300
4301int p2p_set_wfd_coupled_sink_info(struct p2p_data *p2p,
4302 const struct wpabuf *elem)
4303{
4304 wpabuf_free(p2p->wfd_coupled_sink_info);
4305 if (elem) {
4306 p2p->wfd_coupled_sink_info = wpabuf_dup(elem);
4307 if (p2p->wfd_coupled_sink_info == NULL)
4308 return -1;
4309 } else
4310 p2p->wfd_coupled_sink_info = NULL;
4311
4312 return 0;
4313}
4314
4315#endif /* CONFIG_WIFI_DISPLAY */
96beff11
JM
4316
4317
4318int p2p_set_disc_int(struct p2p_data *p2p, int min_disc_int, int max_disc_int,
4319 int max_disc_tu)
4320{
4321 if (min_disc_int > max_disc_int || min_disc_int < 0 || max_disc_int < 0)
4322 return -1;
4323
4324 p2p->min_disc_int = min_disc_int;
4325 p2p->max_disc_int = max_disc_int;
4326 p2p->max_disc_tu = max_disc_tu;
ed496f13
JM
4327 p2p_dbg(p2p, "Set discoverable interval: min=%d max=%d max_tu=%d",
4328 min_disc_int, max_disc_int, max_disc_tu);
96beff11
JM
4329
4330 return 0;
4331}
ed496f13
JM
4332
4333
4334void p2p_dbg(struct p2p_data *p2p, const char *fmt, ...)
4335{
4336 va_list ap;
4337 char buf[500];
4338
4339 if (!p2p->cfg->debug_print)
4340 return;
4341
4342 va_start(ap, fmt);
4343 vsnprintf(buf, sizeof(buf), fmt, ap);
4344 buf[sizeof(buf) - 1] = '\0';
4345 va_end(ap);
4346 p2p->cfg->debug_print(p2p->cfg->cb_ctx, MSG_DEBUG, buf);
4347}
4348
4349
4350void p2p_info(struct p2p_data *p2p, const char *fmt, ...)
4351{
4352 va_list ap;
4353 char buf[500];
4354
4355 if (!p2p->cfg->debug_print)
4356 return;
4357
4358 va_start(ap, fmt);
4359 vsnprintf(buf, sizeof(buf), fmt, ap);
4360 buf[sizeof(buf) - 1] = '\0';
4361 va_end(ap);
4362 p2p->cfg->debug_print(p2p->cfg->cb_ctx, MSG_INFO, buf);
4363}
4364
4365
4366void p2p_err(struct p2p_data *p2p, const char *fmt, ...)
4367{
4368 va_list ap;
4369 char buf[500];
4370
4371 if (!p2p->cfg->debug_print)
4372 return;
4373
4374 va_start(ap, fmt);
4375 vsnprintf(buf, sizeof(buf), fmt, ap);
4376 buf[sizeof(buf) - 1] = '\0';
4377 va_end(ap);
4378 p2p->cfg->debug_print(p2p->cfg->cb_ctx, MSG_ERROR, buf);
4379}