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