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