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