2 * Wi-Fi Direct - P2P module
3 * Copyright (c) 2009-2010, Atheros Communications
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
9 * Alternatively, this software may be distributed under the terms of BSD
12 * See README and COPYING for more details.
19 #include "common/ieee802_11_defs.h"
20 #include "common/ieee802_11_common.h"
21 #include "wps/wps_i.h"
26 static void p2p_state_timeout(void *eloop_ctx
, void *timeout_ctx
);
27 static void p2p_device_free(struct p2p_data
*p2p
, struct p2p_device
*dev
);
28 static void p2p_process_presence_req(struct p2p_data
*p2p
, const u8
*da
,
29 const u8
*sa
, const u8
*data
, size_t len
,
31 static void p2p_process_presence_resp(struct p2p_data
*p2p
, const u8
*da
,
32 const u8
*sa
, const u8
*data
,
34 static void p2p_ext_listen_timeout(void *eloop_ctx
, void *timeout_ctx
);
35 static void p2p_scan_timeout(void *eloop_ctx
, void *timeout_ctx
);
39 * p2p_scan recovery timeout
41 * Many drivers are using 30 second timeout on scan results. Allow a bit larger
42 * timeout for this to avoid hitting P2P timeout unnecessarily.
44 #define P2P_SCAN_TIMEOUT 35
47 * P2P_PEER_EXPIRATION_AGE - Number of seconds after which inactive peer
48 * entries will be removed
50 #define P2P_PEER_EXPIRATION_AGE 300
52 #define P2P_PEER_EXPIRATION_INTERVAL (P2P_PEER_EXPIRATION_AGE / 2)
54 static void p2p_expire_peers(struct p2p_data
*p2p
)
56 struct p2p_device
*dev
, *n
;
60 dl_list_for_each_safe(dev
, n
, &p2p
->devices
, struct p2p_device
, list
) {
61 if (dev
->last_seen
.sec
+ P2P_PEER_EXPIRATION_AGE
>= now
.sec
)
63 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
, "P2P: Expiring old peer "
64 "entry " MACSTR
, MAC2STR(dev
->info
.p2p_device_addr
));
65 dl_list_del(&dev
->list
);
66 p2p_device_free(p2p
, dev
);
71 static void p2p_expiration_timeout(void *eloop_ctx
, void *timeout_ctx
)
73 struct p2p_data
*p2p
= eloop_ctx
;
74 p2p_expire_peers(p2p
);
75 eloop_register_timeout(P2P_PEER_EXPIRATION_INTERVAL
, 0,
76 p2p_expiration_timeout
, p2p
, NULL
);
80 static const char * p2p_state_txt(int state
)
89 case P2P_CONNECT_LISTEN
:
90 return "CONNECT_LISTEN";
95 case P2P_WAIT_PEER_CONNECT
:
96 return "WAIT_PEER_CONNECT";
97 case P2P_WAIT_PEER_IDLE
:
98 return "WAIT_PEER_IDLE";
99 case P2P_SD_DURING_FIND
:
100 return "SD_DURING_FIND";
101 case P2P_PROVISIONING
:
102 return "PROVISIONING";
103 case P2P_PD_DURING_FIND
:
104 return "PD_DURING_FIND";
107 case P2P_INVITE_LISTEN
:
108 return "INVITE_LISTEN";
109 case P2P_SEARCH_WHEN_READY
:
110 return "SEARCH_WHEN_READY";
117 u16
p2p_get_provisioning_info(struct p2p_data
*p2p
, const u8
*addr
)
119 struct p2p_device
*dev
= NULL
;
124 dev
= p2p_get_device(p2p
, addr
);
126 return dev
->wps_prov_info
;
132 void p2p_clear_provisioning_info(struct p2p_data
*p2p
, const u8
*iface_addr
)
134 struct p2p_device
*dev
= NULL
;
136 if (!iface_addr
|| !p2p
)
139 dev
= p2p_get_device_interface(p2p
, iface_addr
);
141 dev
->wps_prov_info
= 0;
145 void p2p_set_state(struct p2p_data
*p2p
, int new_state
)
147 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
, "P2P: State %s -> %s",
148 p2p_state_txt(p2p
->state
), p2p_state_txt(new_state
));
149 p2p
->state
= new_state
;
153 void p2p_set_timeout(struct p2p_data
*p2p
, unsigned int sec
, unsigned int usec
)
155 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
156 "P2P: Set timeout (state=%s): %u.%06u sec",
157 p2p_state_txt(p2p
->state
), sec
, usec
);
158 eloop_cancel_timeout(p2p_state_timeout
, p2p
, NULL
);
159 eloop_register_timeout(sec
, usec
, p2p_state_timeout
, p2p
, NULL
);
163 void p2p_clear_timeout(struct p2p_data
*p2p
)
165 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
, "P2P: Clear timeout (state=%s)",
166 p2p_state_txt(p2p
->state
));
167 eloop_cancel_timeout(p2p_state_timeout
, p2p
, NULL
);
171 void p2p_go_neg_failed(struct p2p_data
*p2p
, struct p2p_device
*peer
,
174 struct p2p_go_neg_results res
;
175 p2p_clear_timeout(p2p
);
176 p2p_set_state(p2p
, P2P_IDLE
);
177 if (p2p
->go_neg_peer
)
178 p2p
->go_neg_peer
->wps_method
= WPS_NOT_READY
;
179 p2p
->go_neg_peer
= NULL
;
181 os_memset(&res
, 0, sizeof(res
));
184 os_memcpy(res
.peer_device_addr
, peer
->info
.p2p_device_addr
,
186 os_memcpy(res
.peer_interface_addr
, peer
->intended_addr
,
189 p2p
->cfg
->go_neg_completed(p2p
->cfg
->cb_ctx
, &res
);
193 static void p2p_listen_in_find(struct p2p_data
*p2p
)
199 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
200 "P2P: Starting short listen state (state=%s)",
201 p2p_state_txt(p2p
->state
));
203 freq
= p2p_channel_to_freq(p2p
->cfg
->country
, p2p
->cfg
->reg_class
,
206 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
207 "P2P: Unknown regulatory class/channel");
211 os_get_random((u8
*) &r
, sizeof(r
));
212 tu
= (r
% ((p2p
->max_disc_int
- p2p
->min_disc_int
) + 1) +
213 p2p
->min_disc_int
) * 100;
215 p2p
->pending_listen_freq
= freq
;
216 p2p
->pending_listen_sec
= 0;
217 p2p
->pending_listen_usec
= 1024 * tu
;
219 ies
= p2p_build_probe_resp_ies(p2p
);
223 if (p2p
->cfg
->start_listen(p2p
->cfg
->cb_ctx
, freq
, 1024 * tu
/ 1000,
225 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
226 "P2P: Failed to start listen mode");
227 p2p
->pending_listen_freq
= 0;
233 int p2p_listen(struct p2p_data
*p2p
, unsigned int timeout
)
238 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
239 "P2P: Going to listen(only) state");
241 freq
= p2p_channel_to_freq(p2p
->cfg
->country
, p2p
->cfg
->reg_class
,
244 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
245 "P2P: Unknown regulatory class/channel");
249 p2p
->pending_listen_freq
= freq
;
250 p2p
->pending_listen_sec
= timeout
/ 1000;
251 p2p
->pending_listen_usec
= (timeout
% 1000) * 1000;
253 if (p2p
->p2p_scan_running
) {
254 if (p2p
->start_after_scan
== P2P_AFTER_SCAN_NOTHING
) {
255 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
256 "P2P: p2p_scan running - connect is already "
257 "pending - skip listen");
260 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
261 "P2P: p2p_scan running - delay start of listen state");
262 p2p
->start_after_scan
= P2P_AFTER_SCAN_LISTEN
;
266 ies
= p2p_build_probe_resp_ies(p2p
);
270 if (p2p
->cfg
->start_listen(p2p
->cfg
->cb_ctx
, freq
, timeout
, ies
) < 0) {
271 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
272 "P2P: Failed to start listen mode");
273 p2p
->pending_listen_freq
= 0;
279 p2p_set_state(p2p
, P2P_LISTEN_ONLY
);
285 static void p2p_device_clear_reported(struct p2p_data
*p2p
)
287 struct p2p_device
*dev
;
288 dl_list_for_each(dev
, &p2p
->devices
, struct p2p_device
, list
)
289 dev
->flags
&= ~P2P_DEV_REPORTED
;
294 * p2p_get_device - Fetch a peer entry
295 * @p2p: P2P module context from p2p_init()
296 * @addr: P2P Device Address of the peer
297 * Returns: Pointer to the device entry or %NULL if not found
299 struct p2p_device
* p2p_get_device(struct p2p_data
*p2p
, const u8
*addr
)
301 struct p2p_device
*dev
;
302 dl_list_for_each(dev
, &p2p
->devices
, struct p2p_device
, list
) {
303 if (os_memcmp(dev
->info
.p2p_device_addr
, addr
, ETH_ALEN
) == 0)
311 * p2p_get_device_interface - Fetch a peer entry based on P2P Interface Address
312 * @p2p: P2P module context from p2p_init()
313 * @addr: P2P Interface Address of the peer
314 * Returns: Pointer to the device entry or %NULL if not found
316 struct p2p_device
* p2p_get_device_interface(struct p2p_data
*p2p
,
319 struct p2p_device
*dev
;
320 dl_list_for_each(dev
, &p2p
->devices
, struct p2p_device
, list
) {
321 if (os_memcmp(dev
->interface_addr
, addr
, ETH_ALEN
) == 0)
329 * p2p_create_device - Create a peer entry
330 * @p2p: P2P module context from p2p_init()
331 * @addr: P2P Device Address of the peer
332 * Returns: Pointer to the device entry or %NULL on failure
334 * If there is already an entry for the peer, it will be returned instead of
335 * creating a new one.
337 static struct p2p_device
* p2p_create_device(struct p2p_data
*p2p
,
340 struct p2p_device
*dev
, *oldest
= NULL
;
343 dev
= p2p_get_device(p2p
, addr
);
347 dl_list_for_each(dev
, &p2p
->devices
, struct p2p_device
, list
) {
349 if (oldest
== NULL
||
350 os_time_before(&dev
->last_seen
, &oldest
->last_seen
))
353 if (count
+ 1 > p2p
->cfg
->max_peers
&& oldest
) {
354 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
355 "P2P: Remove oldest peer entry to make room for a new "
357 dl_list_del(&oldest
->list
);
358 p2p_device_free(p2p
, oldest
);
361 dev
= os_zalloc(sizeof(*dev
));
364 dl_list_add(&p2p
->devices
, &dev
->list
);
365 os_memcpy(dev
->info
.p2p_device_addr
, addr
, ETH_ALEN
);
371 static void p2p_copy_client_info(struct p2p_device
*dev
,
372 struct p2p_client_info
*cli
)
374 os_memcpy(dev
->info
.device_name
, cli
->dev_name
, cli
->dev_name_len
);
375 dev
->info
.device_name
[cli
->dev_name_len
] = '\0';
376 dev
->info
.dev_capab
= cli
->dev_capab
;
377 dev
->info
.config_methods
= cli
->config_methods
;
378 os_memcpy(dev
->info
.pri_dev_type
, cli
->pri_dev_type
, 8);
379 dev
->info
.wps_sec_dev_type_list_len
= 8 * cli
->num_sec_dev_types
;
380 os_memcpy(dev
->info
.wps_sec_dev_type_list
, cli
->sec_dev_types
,
381 dev
->info
.wps_sec_dev_type_list_len
);
385 static int p2p_add_group_clients(struct p2p_data
*p2p
, const u8
*go_dev_addr
,
386 const u8
*go_interface_addr
, int freq
,
387 const u8
*gi
, size_t gi_len
)
389 struct p2p_group_info info
;
391 struct p2p_device
*dev
;
396 if (p2p_group_info_parse(gi
, gi_len
, &info
) < 0)
400 * Clear old data for this group; if the devices are still in the
401 * group, the information will be restored in the loop following this.
403 dl_list_for_each(dev
, &p2p
->devices
, struct p2p_device
, list
) {
404 if (os_memcmp(dev
->member_in_go_iface
, go_interface_addr
,
406 os_memset(dev
->member_in_go_iface
, 0, ETH_ALEN
);
407 os_memset(dev
->member_in_go_dev
, 0, ETH_ALEN
);
411 for (c
= 0; c
< info
.num_clients
; c
++) {
412 struct p2p_client_info
*cli
= &info
.client
[c
];
413 if (os_memcmp(cli
->p2p_device_addr
, p2p
->cfg
->dev_addr
,
415 continue; /* ignore our own entry */
416 dev
= p2p_get_device(p2p
, cli
->p2p_device_addr
);
419 * Update information only if we have not received this
420 * directly from the client.
422 if (dev
->flags
& (P2P_DEV_GROUP_CLIENT_ONLY
|
423 P2P_DEV_PROBE_REQ_ONLY
))
424 p2p_copy_client_info(dev
, cli
);
425 if (dev
->flags
& P2P_DEV_PROBE_REQ_ONLY
) {
426 dev
->flags
&= ~P2P_DEV_PROBE_REQ_ONLY
;
429 dev
= p2p_create_device(p2p
, cli
->p2p_device_addr
);
432 dev
->flags
|= P2P_DEV_GROUP_CLIENT_ONLY
;
433 p2p_copy_client_info(dev
, cli
);
434 dev
->oper_freq
= freq
;
435 p2p
->cfg
->dev_found(p2p
->cfg
->cb_ctx
,
436 dev
->info
.p2p_device_addr
,
438 dev
->flags
|= P2P_DEV_REPORTED
| P2P_DEV_REPORTED_ONCE
;
441 os_memcpy(dev
->interface_addr
, cli
->p2p_interface_addr
,
443 os_get_time(&dev
->last_seen
);
444 os_memcpy(dev
->member_in_go_dev
, go_dev_addr
, ETH_ALEN
);
445 os_memcpy(dev
->member_in_go_iface
, go_interface_addr
,
453 static void p2p_copy_wps_info(struct p2p_device
*dev
, int probe_req
,
454 const struct p2p_message
*msg
)
456 os_memcpy(dev
->info
.device_name
, msg
->device_name
,
457 sizeof(dev
->info
.device_name
));
459 if (msg
->manufacturer
&&
460 msg
->manufacturer_len
< sizeof(dev
->info
.manufacturer
)) {
461 os_memset(dev
->info
.manufacturer
, 0,
462 sizeof(dev
->info
.manufacturer
));
463 os_memcpy(dev
->info
.manufacturer
, msg
->manufacturer
,
464 msg
->manufacturer_len
);
467 if (msg
->model_name
&&
468 msg
->model_name_len
< sizeof(dev
->info
.model_name
)) {
469 os_memset(dev
->info
.model_name
, 0,
470 sizeof(dev
->info
.model_name
));
471 os_memcpy(dev
->info
.model_name
, msg
->model_name
,
472 msg
->model_name_len
);
475 if (msg
->model_number
&&
476 msg
->model_number_len
< sizeof(dev
->info
.model_number
)) {
477 os_memset(dev
->info
.model_number
, 0,
478 sizeof(dev
->info
.model_number
));
479 os_memcpy(dev
->info
.model_number
, msg
->model_number
,
480 msg
->model_number_len
);
483 if (msg
->serial_number
&&
484 msg
->serial_number_len
< sizeof(dev
->info
.serial_number
)) {
485 os_memset(dev
->info
.serial_number
, 0,
486 sizeof(dev
->info
.serial_number
));
487 os_memcpy(dev
->info
.serial_number
, msg
->serial_number
,
488 msg
->serial_number_len
);
491 if (msg
->pri_dev_type
)
492 os_memcpy(dev
->info
.pri_dev_type
, msg
->pri_dev_type
,
493 sizeof(dev
->info
.pri_dev_type
));
494 else if (msg
->wps_pri_dev_type
)
495 os_memcpy(dev
->info
.pri_dev_type
, msg
->wps_pri_dev_type
,
496 sizeof(dev
->info
.pri_dev_type
));
498 if (msg
->wps_sec_dev_type_list
) {
499 os_memcpy(dev
->info
.wps_sec_dev_type_list
,
500 msg
->wps_sec_dev_type_list
,
501 msg
->wps_sec_dev_type_list_len
);
502 dev
->info
.wps_sec_dev_type_list_len
=
503 msg
->wps_sec_dev_type_list_len
;
506 if (msg
->capability
) {
507 dev
->info
.dev_capab
= msg
->capability
[0];
508 dev
->info
.group_capab
= msg
->capability
[1];
511 if (msg
->ext_listen_timing
) {
512 dev
->ext_listen_period
= WPA_GET_LE16(msg
->ext_listen_timing
);
513 dev
->ext_listen_interval
=
514 WPA_GET_LE16(msg
->ext_listen_timing
+ 2);
518 dev
->info
.config_methods
= msg
->config_methods
?
519 msg
->config_methods
: msg
->wps_config_methods
;
525 * p2p_add_device - Add peer entries based on scan results
526 * @p2p: P2P module context from p2p_init()
527 * @addr: Source address of Beacon or Probe Response frame (may be either
528 * P2P Device Address or P2P Interface Address)
529 * @level: Signal level (signal strength of the received frame from the peer)
530 * @freq: Frequency on which the Beacon or Probe Response frame was received
531 * @ies: IEs from the Beacon or Probe Response frame
532 * @ies_len: Length of ies buffer in octets
533 * Returns: 0 on success, -1 on failure
535 * If the scan result is for a GO, the clients in the group will also be added
536 * to the peer table. This function can also be used with some other frames
537 * like Provision Discovery Request that contains P2P Capability and P2P Device
540 int p2p_add_device(struct p2p_data
*p2p
, const u8
*addr
, int freq
, int level
,
541 const u8
*ies
, size_t ies_len
)
543 struct p2p_device
*dev
;
544 struct p2p_message msg
;
545 const u8
*p2p_dev_addr
;
548 os_memset(&msg
, 0, sizeof(msg
));
549 if (p2p_parse_ies(ies
, ies_len
, &msg
)) {
550 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
551 "P2P: Failed to parse P2P IE for a device entry");
552 p2p_parse_free(&msg
);
556 if (msg
.p2p_device_addr
)
557 p2p_dev_addr
= msg
.p2p_device_addr
;
558 else if (msg
.device_id
)
559 p2p_dev_addr
= msg
.device_id
;
561 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
562 "P2P: Ignore scan data without P2P Device Info or "
564 p2p_parse_free(&msg
);
568 if (!is_zero_ether_addr(p2p
->peer_filter
) &&
569 os_memcmp(p2p_dev_addr
, p2p
->peer_filter
, ETH_ALEN
) != 0) {
570 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
, "P2P: Do not add peer "
571 "filter for " MACSTR
" due to peer filter",
572 MAC2STR(p2p_dev_addr
));
576 dev
= p2p_create_device(p2p
, p2p_dev_addr
);
578 p2p_parse_free(&msg
);
581 os_get_time(&dev
->last_seen
);
582 dev
->flags
&= ~(P2P_DEV_PROBE_REQ_ONLY
| P2P_DEV_GROUP_CLIENT_ONLY
);
584 if (os_memcmp(addr
, p2p_dev_addr
, ETH_ALEN
) != 0)
585 os_memcpy(dev
->interface_addr
, addr
, ETH_ALEN
);
587 (msg
.ssid
[1] != P2P_WILDCARD_SSID_LEN
||
588 os_memcmp(msg
.ssid
+ 2, P2P_WILDCARD_SSID
, P2P_WILDCARD_SSID_LEN
)
590 os_memcpy(dev
->oper_ssid
, msg
.ssid
+ 2, msg
.ssid
[1]);
591 dev
->oper_ssid_len
= msg
.ssid
[1];
594 if (freq
>= 2412 && freq
<= 2484 && msg
.ds_params
&&
595 *msg
.ds_params
>= 1 && *msg
.ds_params
<= 14) {
597 if (*msg
.ds_params
== 14)
600 ds_freq
= 2407 + *msg
.ds_params
* 5;
601 if (freq
!= ds_freq
) {
602 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
603 "P2P: Update Listen frequency based on DS "
604 "Parameter Set IE: %d -> %d MHz",
610 if (dev
->listen_freq
&& dev
->listen_freq
!= freq
) {
611 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
612 "P2P: Update Listen frequency based on scan "
613 "results (" MACSTR
" %d -> %d MHz (DS param %d)",
614 MAC2STR(dev
->info
.p2p_device_addr
), dev
->listen_freq
,
615 freq
, msg
.ds_params
? *msg
.ds_params
: -1);
617 dev
->listen_freq
= freq
;
619 dev
->oper_freq
= freq
;
620 dev
->info
.level
= level
;
622 p2p_copy_wps_info(dev
, 0, &msg
);
624 for (i
= 0; i
< P2P_MAX_WPS_VENDOR_EXT
; i
++) {
625 wpabuf_free(dev
->info
.wps_vendor_ext
[i
]);
626 dev
->info
.wps_vendor_ext
[i
] = NULL
;
629 for (i
= 0; i
< P2P_MAX_WPS_VENDOR_EXT
; i
++) {
630 if (msg
.wps_vendor_ext
[i
] == NULL
)
632 dev
->info
.wps_vendor_ext
[i
] = wpabuf_alloc_copy(
633 msg
.wps_vendor_ext
[i
], msg
.wps_vendor_ext_len
[i
]);
634 if (dev
->info
.wps_vendor_ext
[i
] == NULL
)
638 p2p_add_group_clients(p2p
, p2p_dev_addr
, addr
, freq
, msg
.group_info
,
641 p2p_parse_free(&msg
);
643 if (p2p_pending_sd_req(p2p
, dev
))
644 dev
->flags
|= P2P_DEV_SD_SCHEDULE
;
646 if (dev
->flags
& P2P_DEV_REPORTED
)
649 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
650 "P2P: Peer found with Listen frequency %d MHz", freq
);
651 if (dev
->flags
& P2P_DEV_USER_REJECTED
) {
652 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
653 "P2P: Do not report rejected device");
657 p2p
->cfg
->dev_found(p2p
->cfg
->cb_ctx
, addr
, &dev
->info
,
658 !(dev
->flags
& P2P_DEV_REPORTED_ONCE
));
659 dev
->flags
|= P2P_DEV_REPORTED
| P2P_DEV_REPORTED_ONCE
;
665 static void p2p_device_free(struct p2p_data
*p2p
, struct p2p_device
*dev
)
669 if (p2p
->go_neg_peer
== dev
) {
671 * If GO Negotiation is in progress, report that it has failed.
673 p2p_go_neg_failed(p2p
, dev
, -1);
674 p2p
->go_neg_peer
= NULL
;
676 if (p2p
->invite_peer
== dev
)
677 p2p
->invite_peer
= NULL
;
678 if (p2p
->sd_peer
== dev
)
680 if (p2p
->pending_client_disc_go
== dev
)
681 p2p
->pending_client_disc_go
= NULL
;
683 /* dev_lost() device, but only if it was previously dev_found() */
684 if (dev
->flags
& P2P_DEV_REPORTED_ONCE
)
685 p2p
->cfg
->dev_lost(p2p
->cfg
->cb_ctx
,
686 dev
->info
.p2p_device_addr
);
688 for (i
= 0; i
< P2P_MAX_WPS_VENDOR_EXT
; i
++) {
689 wpabuf_free(dev
->info
.wps_vendor_ext
[i
]);
690 dev
->info
.wps_vendor_ext
[i
] = NULL
;
697 static int p2p_get_next_prog_freq(struct p2p_data
*p2p
)
699 struct p2p_channels
*c
;
700 struct p2p_reg_class
*cla
;
707 c
= &p2p
->cfg
->channels
;
708 for (cl
= 0; cl
< c
->reg_classes
; cl
++) {
709 cla
= &c
->reg_class
[cl
];
710 if (cla
->reg_class
!= p2p
->last_prog_scan_class
)
712 for (ch
= 0; ch
< cla
->channels
; ch
++) {
713 if (cla
->channel
[ch
] == p2p
->last_prog_scan_chan
) {
723 /* Start from beginning */
724 reg_class
= c
->reg_class
[0].reg_class
;
725 channel
= c
->reg_class
[0].channel
[0];
727 /* Pick the next channel */
729 if (ch
== cla
->channels
) {
731 if (cl
== c
->reg_classes
)
735 reg_class
= c
->reg_class
[cl
].reg_class
;
736 channel
= c
->reg_class
[cl
].channel
[ch
];
739 freq
= p2p_channel_to_freq(p2p
->cfg
->country
, reg_class
, channel
);
740 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
, "P2P: Next progressive search "
741 "channel: reg_class %u channel %u -> %d MHz",
742 reg_class
, channel
, freq
);
743 p2p
->last_prog_scan_class
= reg_class
;
744 p2p
->last_prog_scan_chan
= channel
;
746 if (freq
== 2412 || freq
== 2437 || freq
== 2462)
747 return 0; /* No need to add social channels */
752 static void p2p_search(struct p2p_data
*p2p
)
755 enum p2p_scan_type type
;
757 if (p2p
->drv_in_listen
) {
758 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
, "P2P: Driver is still "
759 "in Listen state - wait for it to end before "
763 p2p
->cfg
->stop_listen(p2p
->cfg
->cb_ctx
);
765 if (p2p
->go_neg_peer
) {
767 * Only scan the known listen frequency of the peer
768 * during GO Negotiation start.
770 freq
= p2p
->go_neg_peer
->listen_freq
;
772 freq
= p2p
->go_neg_peer
->oper_freq
;
773 type
= P2P_SCAN_SPECIFIC
;
774 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
, "P2P: Starting search "
775 "for freq %u (GO Neg)", freq
);
776 } else if (p2p
->invite_peer
) {
778 * Only scan the known listen frequency of the peer
779 * during Invite start.
781 freq
= p2p
->invite_peer
->listen_freq
;
783 freq
= p2p
->invite_peer
->oper_freq
;
784 type
= P2P_SCAN_SPECIFIC
;
785 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
, "P2P: Starting search "
786 "for freq %u (Invite)", freq
);
787 } else if (p2p
->find_type
== P2P_FIND_PROGRESSIVE
&&
788 (freq
= p2p_get_next_prog_freq(p2p
)) > 0) {
789 type
= P2P_SCAN_SOCIAL_PLUS_ONE
;
790 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
, "P2P: Starting search "
791 "(+ freq %u)", freq
);
793 type
= P2P_SCAN_SOCIAL
;
794 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
, "P2P: Starting search");
797 if (p2p
->cfg
->p2p_scan(p2p
->cfg
->cb_ctx
, type
, freq
,
798 p2p
->num_req_dev_types
, p2p
->req_dev_types
,
799 p2p
->find_dev_id
) < 0) {
800 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
801 "P2P: Scan request failed");
802 p2p_continue_find(p2p
);
804 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
, "P2P: Running p2p_scan");
805 p2p
->p2p_scan_running
= 1;
806 eloop_cancel_timeout(p2p_scan_timeout
, p2p
, NULL
);
807 eloop_register_timeout(P2P_SCAN_TIMEOUT
, 0, p2p_scan_timeout
,
813 static void p2p_find_timeout(void *eloop_ctx
, void *timeout_ctx
)
815 struct p2p_data
*p2p
= eloop_ctx
;
816 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
, "P2P: Find timeout -> stop");
821 static int p2p_run_after_scan(struct p2p_data
*p2p
)
823 struct p2p_device
*dev
;
824 enum p2p_after_scan op
;
826 if (p2p
->after_scan_tx
) {
827 /* TODO: schedule p2p_run_after_scan to be called from TX
828 * status callback(?) */
829 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
, "P2P: Send pending "
830 "Action frame at p2p_scan completion");
831 p2p
->cfg
->send_action(p2p
->cfg
->cb_ctx
,
832 p2p
->after_scan_tx
->freq
,
833 p2p
->after_scan_tx
->dst
,
834 p2p
->after_scan_tx
->src
,
835 p2p
->after_scan_tx
->bssid
,
836 (u8
*) (p2p
->after_scan_tx
+ 1),
837 p2p
->after_scan_tx
->len
,
838 p2p
->after_scan_tx
->wait_time
);
839 os_free(p2p
->after_scan_tx
);
840 p2p
->after_scan_tx
= NULL
;
844 op
= p2p
->start_after_scan
;
845 p2p
->start_after_scan
= P2P_AFTER_SCAN_NOTHING
;
847 case P2P_AFTER_SCAN_NOTHING
:
849 case P2P_AFTER_SCAN_LISTEN
:
850 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
, "P2P: Start previously "
851 "requested Listen state");
852 p2p_listen(p2p
, p2p
->pending_listen_sec
* 1000 +
853 p2p
->pending_listen_usec
/ 1000);
855 case P2P_AFTER_SCAN_CONNECT
:
856 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
, "P2P: Start previously "
857 "requested connect with " MACSTR
,
858 MAC2STR(p2p
->after_scan_peer
));
859 dev
= p2p_get_device(p2p
, p2p
->after_scan_peer
);
861 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
, "P2P: Peer not "
865 p2p_connect_send(p2p
, dev
);
873 static void p2p_scan_timeout(void *eloop_ctx
, void *timeout_ctx
)
875 struct p2p_data
*p2p
= eloop_ctx
;
877 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
, "P2P: p2p_scan timeout "
878 "(running=%d)", p2p
->p2p_scan_running
);
879 running
= p2p
->p2p_scan_running
;
880 /* Make sure we recover from missed scan results callback */
881 p2p
->p2p_scan_running
= 0;
884 p2p_run_after_scan(p2p
);
888 static void p2p_free_req_dev_types(struct p2p_data
*p2p
)
890 p2p
->num_req_dev_types
= 0;
891 os_free(p2p
->req_dev_types
);
892 p2p
->req_dev_types
= NULL
;
896 int p2p_find(struct p2p_data
*p2p
, unsigned int timeout
,
897 enum p2p_discovery_type type
,
898 unsigned int num_req_dev_types
, const u8
*req_dev_types
,
903 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
, "P2P: Starting find (type=%d)",
905 if (p2p
->p2p_scan_running
) {
906 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
, "P2P: p2p_scan is "
910 p2p_free_req_dev_types(p2p
);
911 if (req_dev_types
&& num_req_dev_types
) {
912 p2p
->req_dev_types
= os_malloc(num_req_dev_types
*
914 if (p2p
->req_dev_types
== NULL
)
916 os_memcpy(p2p
->req_dev_types
, req_dev_types
,
917 num_req_dev_types
* WPS_DEV_TYPE_LEN
);
918 p2p
->num_req_dev_types
= num_req_dev_types
;
922 os_memcpy(p2p
->find_dev_id_buf
, dev_id
, ETH_ALEN
);
923 p2p
->find_dev_id
= p2p
->find_dev_id_buf
;
925 p2p
->find_dev_id
= NULL
;
927 p2p
->start_after_scan
= P2P_AFTER_SCAN_NOTHING
;
928 p2p_clear_timeout(p2p
);
929 p2p
->cfg
->stop_listen(p2p
->cfg
->cb_ctx
);
930 p2p
->find_type
= type
;
931 p2p_device_clear_reported(p2p
);
932 p2p_set_state(p2p
, P2P_SEARCH
);
933 eloop_cancel_timeout(p2p_find_timeout
, p2p
, NULL
);
934 p2p
->last_p2p_find_timeout
= timeout
;
936 eloop_register_timeout(timeout
, 0, p2p_find_timeout
,
939 case P2P_FIND_START_WITH_FULL
:
940 case P2P_FIND_PROGRESSIVE
:
941 res
= p2p
->cfg
->p2p_scan(p2p
->cfg
->cb_ctx
, P2P_SCAN_FULL
, 0,
942 p2p
->num_req_dev_types
,
943 p2p
->req_dev_types
, dev_id
);
945 case P2P_FIND_ONLY_SOCIAL
:
946 res
= p2p
->cfg
->p2p_scan(p2p
->cfg
->cb_ctx
, P2P_SCAN_SOCIAL
, 0,
947 p2p
->num_req_dev_types
,
948 p2p
->req_dev_types
, dev_id
);
955 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
, "P2P: Running p2p_scan");
956 p2p
->p2p_scan_running
= 1;
957 eloop_cancel_timeout(p2p_scan_timeout
, p2p
, NULL
);
958 eloop_register_timeout(P2P_SCAN_TIMEOUT
, 0, p2p_scan_timeout
,
960 } else if (res
== 1) {
961 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
, "P2P: Could not start "
962 "p2p_scan at this point - will try again after "
963 "previous scan completes");
965 p2p_set_state(p2p
, P2P_SEARCH_WHEN_READY
);
966 eloop_cancel_timeout(p2p_find_timeout
, p2p
, NULL
);
968 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
, "P2P: Failed to start "
970 p2p_set_state(p2p
, P2P_IDLE
);
971 eloop_cancel_timeout(p2p_find_timeout
, p2p
, NULL
);
978 int p2p_other_scan_completed(struct p2p_data
*p2p
)
980 if (p2p
->state
!= P2P_SEARCH_WHEN_READY
)
982 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
, "P2P: Starting pending P2P find "
983 "now that previous scan was completed");
984 if (p2p_find(p2p
, p2p
->last_p2p_find_timeout
, p2p
->find_type
,
985 p2p
->num_req_dev_types
, p2p
->req_dev_types
,
986 p2p
->find_dev_id
) < 0)
992 void p2p_stop_find_for_freq(struct p2p_data
*p2p
, int freq
)
994 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
, "P2P: Stopping find");
995 eloop_cancel_timeout(p2p_find_timeout
, p2p
, NULL
);
996 p2p_clear_timeout(p2p
);
997 p2p_set_state(p2p
, P2P_IDLE
);
998 p2p_free_req_dev_types(p2p
);
999 p2p
->start_after_scan
= P2P_AFTER_SCAN_NOTHING
;
1000 p2p
->go_neg_peer
= NULL
;
1001 p2p
->sd_peer
= NULL
;
1002 p2p
->invite_peer
= NULL
;
1003 if (freq
> 0 && p2p
->drv_in_listen
== freq
&& p2p
->in_listen
) {
1004 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
, "P2P: Skip stop_listen "
1005 "since we are on correct channel for response");
1008 if (p2p
->drv_in_listen
) {
1010 * The driver may not deliver callback to p2p_listen_end()
1011 * when the operation gets canceled, so clear the internal
1012 * variable that is tracking driver state.
1014 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
, "P2P: Clear "
1015 "drv_in_listen (%d)", p2p
->drv_in_listen
);
1016 p2p
->drv_in_listen
= 0;
1018 p2p
->cfg
->stop_listen(p2p
->cfg
->cb_ctx
);
1022 void p2p_stop_find(struct p2p_data
*p2p
)
1024 p2p_stop_find_for_freq(p2p
, 0);
1028 static int p2p_prepare_channel(struct p2p_data
*p2p
, unsigned int force_freq
)
1031 u8 op_reg_class
, op_channel
;
1032 if (p2p_freq_to_channel(p2p
->cfg
->country
, force_freq
,
1033 &op_reg_class
, &op_channel
) < 0) {
1034 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
1035 "P2P: Unsupported frequency %u MHz",
1039 if (!p2p_channels_includes(&p2p
->cfg
->channels
, op_reg_class
,
1041 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
1042 "P2P: Frequency %u MHz (oper_class %u "
1043 "channel %u) not allowed for P2P",
1044 force_freq
, op_reg_class
, op_channel
);
1047 p2p
->op_reg_class
= op_reg_class
;
1048 p2p
->op_channel
= op_channel
;
1049 p2p
->channels
.reg_classes
= 1;
1050 p2p
->channels
.reg_class
[0].channels
= 1;
1051 p2p
->channels
.reg_class
[0].reg_class
= p2p
->op_reg_class
;
1052 p2p
->channels
.reg_class
[0].channel
[0] = p2p
->op_channel
;
1054 u8 op_reg_class
, op_channel
;
1056 if (!p2p
->cfg
->cfg_op_channel
&& p2p
->best_freq_overall
> 0 &&
1057 p2p_supported_freq(p2p
, p2p
->best_freq_overall
) &&
1058 p2p_freq_to_channel(p2p
->cfg
->country
,
1059 p2p
->best_freq_overall
,
1060 &op_reg_class
, &op_channel
) == 0) {
1061 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
1062 "P2P: Select best overall channel as "
1063 "operating channel preference");
1064 p2p
->op_reg_class
= op_reg_class
;
1065 p2p
->op_channel
= op_channel
;
1066 } else if (!p2p
->cfg
->cfg_op_channel
&& p2p
->best_freq_5
> 0 &&
1067 p2p_supported_freq(p2p
, p2p
->best_freq_5
) &&
1068 p2p_freq_to_channel(p2p
->cfg
->country
,
1070 &op_reg_class
, &op_channel
) ==
1072 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
1073 "P2P: Select best 5 GHz channel as "
1074 "operating channel preference");
1075 p2p
->op_reg_class
= op_reg_class
;
1076 p2p
->op_channel
= op_channel
;
1077 } else if (!p2p
->cfg
->cfg_op_channel
&&
1078 p2p
->best_freq_24
> 0 &&
1079 p2p_supported_freq(p2p
, p2p
->best_freq_24
) &&
1080 p2p_freq_to_channel(p2p
->cfg
->country
,
1082 &op_reg_class
, &op_channel
) ==
1084 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
1085 "P2P: Select best 2.4 GHz channel as "
1086 "operating channel preference");
1087 p2p
->op_reg_class
= op_reg_class
;
1088 p2p
->op_channel
= op_channel
;
1090 p2p
->op_reg_class
= p2p
->cfg
->op_reg_class
;
1091 p2p
->op_channel
= p2p
->cfg
->op_channel
;
1094 os_memcpy(&p2p
->channels
, &p2p
->cfg
->channels
,
1095 sizeof(struct p2p_channels
));
1097 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
1098 "P2P: Own preference for operation channel: "
1099 "Operating Class %u Channel %u%s",
1100 p2p
->op_reg_class
, p2p
->op_channel
,
1101 force_freq
? " (forced)" : "");
1107 static void p2p_set_dev_persistent(struct p2p_device
*dev
,
1108 int persistent_group
)
1110 switch (persistent_group
) {
1112 dev
->flags
&= ~(P2P_DEV_PREFER_PERSISTENT_GROUP
|
1113 P2P_DEV_PREFER_PERSISTENT_RECONN
);
1116 dev
->flags
|= P2P_DEV_PREFER_PERSISTENT_GROUP
;
1117 dev
->flags
&= ~P2P_DEV_PREFER_PERSISTENT_RECONN
;
1120 dev
->flags
|= P2P_DEV_PREFER_PERSISTENT_GROUP
|
1121 P2P_DEV_PREFER_PERSISTENT_RECONN
;
1127 int p2p_connect(struct p2p_data
*p2p
, const u8
*peer_addr
,
1128 enum p2p_wps_method wps_method
,
1129 int go_intent
, const u8
*own_interface_addr
,
1130 unsigned int force_freq
, int persistent_group
)
1132 struct p2p_device
*dev
;
1134 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
1135 "P2P: Request to start group negotiation - peer=" MACSTR
1136 " GO Intent=%d Intended Interface Address=" MACSTR
1137 " wps_method=%d persistent_group=%d",
1138 MAC2STR(peer_addr
), go_intent
, MAC2STR(own_interface_addr
),
1139 wps_method
, persistent_group
);
1141 if (p2p_prepare_channel(p2p
, force_freq
) < 0)
1145 dev
= p2p_get_device(p2p
, peer_addr
);
1146 if (dev
== NULL
|| (dev
->flags
& P2P_DEV_PROBE_REQ_ONLY
)) {
1147 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
1148 "P2P: Cannot connect to unknown P2P Device " MACSTR
,
1149 MAC2STR(peer_addr
));
1153 if (dev
->flags
& P2P_DEV_GROUP_CLIENT_ONLY
) {
1154 if (!(dev
->info
.dev_capab
&
1155 P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY
)) {
1156 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
1157 "P2P: Cannot connect to P2P Device " MACSTR
1158 " that is in a group and is not discoverable",
1159 MAC2STR(peer_addr
));
1162 if (dev
->oper_freq
<= 0) {
1163 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
1164 "P2P: Cannot connect to P2P Device " MACSTR
1165 " with incomplete information",
1166 MAC2STR(peer_addr
));
1171 * First, try to connect directly. If the peer does not
1172 * acknowledge frames, assume it is sleeping and use device
1173 * discoverability via the GO at that point.
1177 dev
->flags
&= ~P2P_DEV_NOT_YET_READY
;
1178 dev
->flags
&= ~P2P_DEV_USER_REJECTED
;
1179 dev
->flags
&= ~P2P_DEV_WAIT_GO_NEG_RESPONSE
;
1180 dev
->flags
&= ~P2P_DEV_WAIT_GO_NEG_CONFIRM
;
1181 dev
->connect_reqs
= 0;
1182 dev
->go_neg_req_sent
= 0;
1183 dev
->go_state
= UNKNOWN_GO
;
1184 p2p_set_dev_persistent(dev
, persistent_group
);
1185 p2p
->go_intent
= go_intent
;
1186 os_memcpy(p2p
->intended_addr
, own_interface_addr
, ETH_ALEN
);
1188 if (p2p
->state
!= P2P_IDLE
)
1191 if (p2p
->after_scan_tx
) {
1193 * We need to drop the pending frame to avoid issues with the
1194 * new GO Negotiation, e.g., when the pending frame was from a
1195 * previous attempt at starting a GO Negotiation.
1197 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
, "P2P: Dropped "
1198 "previous pending Action frame TX that was waiting "
1199 "for p2p_scan completion");
1200 os_free(p2p
->after_scan_tx
);
1201 p2p
->after_scan_tx
= NULL
;
1204 dev
->wps_method
= wps_method
;
1205 dev
->status
= P2P_SC_SUCCESS
;
1208 dev
->flags
|= P2P_DEV_FORCE_FREQ
;
1210 dev
->flags
&= ~P2P_DEV_FORCE_FREQ
;
1212 if (p2p
->p2p_scan_running
) {
1213 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
1214 "P2P: p2p_scan running - delay connect send");
1215 p2p
->start_after_scan
= P2P_AFTER_SCAN_CONNECT
;
1216 os_memcpy(p2p
->after_scan_peer
, peer_addr
, ETH_ALEN
);
1219 p2p
->start_after_scan
= P2P_AFTER_SCAN_NOTHING
;
1221 return p2p_connect_send(p2p
, dev
);
1225 int p2p_authorize(struct p2p_data
*p2p
, const u8
*peer_addr
,
1226 enum p2p_wps_method wps_method
,
1227 int go_intent
, const u8
*own_interface_addr
,
1228 unsigned int force_freq
, int persistent_group
)
1230 struct p2p_device
*dev
;
1232 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
1233 "P2P: Request to authorize group negotiation - peer=" MACSTR
1234 " GO Intent=%d Intended Interface Address=" MACSTR
1235 " wps_method=%d persistent_group=%d",
1236 MAC2STR(peer_addr
), go_intent
, MAC2STR(own_interface_addr
),
1237 wps_method
, persistent_group
);
1239 if (p2p_prepare_channel(p2p
, force_freq
) < 0)
1242 dev
= p2p_get_device(p2p
, peer_addr
);
1244 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
1245 "P2P: Cannot authorize unknown P2P Device " MACSTR
,
1246 MAC2STR(peer_addr
));
1250 dev
->flags
&= ~P2P_DEV_NOT_YET_READY
;
1251 dev
->flags
&= ~P2P_DEV_USER_REJECTED
;
1252 dev
->go_neg_req_sent
= 0;
1253 dev
->go_state
= UNKNOWN_GO
;
1254 p2p_set_dev_persistent(dev
, persistent_group
);
1255 p2p
->go_intent
= go_intent
;
1256 os_memcpy(p2p
->intended_addr
, own_interface_addr
, ETH_ALEN
);
1258 dev
->wps_method
= wps_method
;
1259 dev
->status
= P2P_SC_SUCCESS
;
1262 dev
->flags
|= P2P_DEV_FORCE_FREQ
;
1264 dev
->flags
&= ~P2P_DEV_FORCE_FREQ
;
1270 void p2p_add_dev_info(struct p2p_data
*p2p
, const u8
*addr
,
1271 struct p2p_device
*dev
, struct p2p_message
*msg
)
1273 os_get_time(&dev
->last_seen
);
1275 p2p_copy_wps_info(dev
, 0, msg
);
1277 if (msg
->listen_channel
) {
1279 freq
= p2p_channel_to_freq((char *) msg
->listen_channel
,
1280 msg
->listen_channel
[3],
1281 msg
->listen_channel
[4]);
1283 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
1284 "P2P: Unknown peer Listen channel: "
1285 "country=%c%c(0x%02x) reg_class=%u channel=%u",
1286 msg
->listen_channel
[0],
1287 msg
->listen_channel
[1],
1288 msg
->listen_channel
[2],
1289 msg
->listen_channel
[3],
1290 msg
->listen_channel
[4]);
1292 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
, "P2P: Update "
1293 "peer " MACSTR
" Listen channel: %u -> %u MHz",
1294 MAC2STR(dev
->info
.p2p_device_addr
),
1295 dev
->listen_freq
, freq
);
1296 dev
->listen_freq
= freq
;
1300 if (dev
->flags
& P2P_DEV_PROBE_REQ_ONLY
) {
1301 dev
->flags
&= ~P2P_DEV_PROBE_REQ_ONLY
;
1302 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
1303 "P2P: Completed device entry based on data from "
1304 "GO Negotiation Request");
1306 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
1307 "P2P: Created device entry based on GO Neg Req: "
1308 MACSTR
" dev_capab=0x%x group_capab=0x%x name='%s' "
1310 MAC2STR(dev
->info
.p2p_device_addr
),
1311 dev
->info
.dev_capab
, dev
->info
.group_capab
,
1312 dev
->info
.device_name
, dev
->listen_freq
);
1315 dev
->flags
&= ~P2P_DEV_GROUP_CLIENT_ONLY
;
1317 if (dev
->flags
& P2P_DEV_USER_REJECTED
) {
1318 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
1319 "P2P: Do not report rejected device");
1323 p2p
->cfg
->dev_found(p2p
->cfg
->cb_ctx
, addr
, &dev
->info
,
1324 !(dev
->flags
& P2P_DEV_REPORTED_ONCE
));
1325 dev
->flags
|= P2P_DEV_REPORTED
| P2P_DEV_REPORTED_ONCE
;
1329 void p2p_build_ssid(struct p2p_data
*p2p
, u8
*ssid
, size_t *ssid_len
)
1331 os_memcpy(ssid
, P2P_WILDCARD_SSID
, P2P_WILDCARD_SSID_LEN
);
1332 p2p_random((char *) &ssid
[P2P_WILDCARD_SSID_LEN
], 2);
1333 os_memcpy(&ssid
[P2P_WILDCARD_SSID_LEN
+ 2],
1334 p2p
->cfg
->ssid_postfix
, p2p
->cfg
->ssid_postfix_len
);
1335 *ssid_len
= P2P_WILDCARD_SSID_LEN
+ 2 + p2p
->cfg
->ssid_postfix_len
;
1339 int p2p_go_params(struct p2p_data
*p2p
, struct p2p_go_neg_results
*params
)
1341 p2p_build_ssid(p2p
, params
->ssid
, ¶ms
->ssid_len
);
1342 p2p_random(params
->passphrase
, 8);
1347 void p2p_go_complete(struct p2p_data
*p2p
, struct p2p_device
*peer
)
1349 struct p2p_go_neg_results res
;
1350 int go
= peer
->go_state
== LOCAL_GO
;
1351 struct p2p_channels intersection
;
1355 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
1356 "P2P: GO Negotiation with " MACSTR
" completed (%s will be "
1357 "GO)", MAC2STR(peer
->info
.p2p_device_addr
),
1358 go
? "local end" : "peer");
1360 os_memset(&res
, 0, sizeof(res
));
1362 os_memcpy(res
.peer_device_addr
, peer
->info
.p2p_device_addr
, ETH_ALEN
);
1363 os_memcpy(res
.peer_interface_addr
, peer
->intended_addr
, ETH_ALEN
);
1364 res
.wps_method
= peer
->wps_method
;
1365 if (peer
->flags
& P2P_DEV_PREFER_PERSISTENT_GROUP
) {
1366 if (peer
->flags
& P2P_DEV_PREFER_PERSISTENT_RECONN
)
1367 res
.persistent_group
= 2;
1369 res
.persistent_group
= 1;
1373 /* Setup AP mode for WPS provisioning */
1374 res
.freq
= p2p_channel_to_freq(p2p
->cfg
->country
,
1377 os_memcpy(res
.ssid
, p2p
->ssid
, p2p
->ssid_len
);
1378 res
.ssid_len
= p2p
->ssid_len
;
1379 p2p_random(res
.passphrase
, 8);
1381 res
.freq
= peer
->oper_freq
;
1382 if (p2p
->ssid_len
) {
1383 os_memcpy(res
.ssid
, p2p
->ssid
, p2p
->ssid_len
);
1384 res
.ssid_len
= p2p
->ssid_len
;
1388 p2p_channels_intersect(&p2p
->channels
, &peer
->channels
,
1391 for (i
= 0; i
< intersection
.reg_classes
; i
++) {
1392 struct p2p_reg_class
*c
= &intersection
.reg_class
[i
];
1393 if (freqs
+ 1 == P2P_MAX_CHANNELS
)
1395 for (j
= 0; j
< c
->channels
; j
++) {
1397 if (freqs
+ 1 == P2P_MAX_CHANNELS
)
1399 freq
= p2p_channel_to_freq(peer
->country
, c
->reg_class
,
1403 res
.freq_list
[freqs
++] = freq
;
1407 res
.peer_config_timeout
= go
? peer
->client_timeout
: peer
->go_timeout
;
1409 p2p_clear_timeout(p2p
);
1411 peer
->go_neg_req_sent
= 0;
1412 peer
->wps_method
= WPS_NOT_READY
;
1414 p2p_set_state(p2p
, P2P_PROVISIONING
);
1415 p2p
->cfg
->go_neg_completed(p2p
->cfg
->cb_ctx
, &res
);
1419 static void p2p_rx_p2p_action(struct p2p_data
*p2p
, const u8
*sa
,
1420 const u8
*data
, size_t len
, int rx_freq
)
1422 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
1423 "P2P: RX P2P Public Action from " MACSTR
, MAC2STR(sa
));
1424 wpa_hexdump(MSG_MSGDUMP
, "P2P: P2P Public Action contents", data
, len
);
1430 case P2P_GO_NEG_REQ
:
1431 p2p_process_go_neg_req(p2p
, sa
, data
+ 1, len
- 1, rx_freq
);
1433 case P2P_GO_NEG_RESP
:
1434 p2p_process_go_neg_resp(p2p
, sa
, data
+ 1, len
- 1, rx_freq
);
1436 case P2P_GO_NEG_CONF
:
1437 p2p_process_go_neg_conf(p2p
, sa
, data
+ 1, len
- 1);
1439 case P2P_INVITATION_REQ
:
1440 p2p_process_invitation_req(p2p
, sa
, data
+ 1, len
- 1,
1443 case P2P_INVITATION_RESP
:
1444 p2p_process_invitation_resp(p2p
, sa
, data
+ 1, len
- 1);
1446 case P2P_PROV_DISC_REQ
:
1447 p2p_process_prov_disc_req(p2p
, sa
, data
+ 1, len
- 1, rx_freq
);
1449 case P2P_PROV_DISC_RESP
:
1450 p2p_process_prov_disc_resp(p2p
, sa
, data
+ 1, len
- 1);
1452 case P2P_DEV_DISC_REQ
:
1453 p2p_process_dev_disc_req(p2p
, sa
, data
+ 1, len
- 1, rx_freq
);
1455 case P2P_DEV_DISC_RESP
:
1456 p2p_process_dev_disc_resp(p2p
, sa
, data
+ 1, len
- 1);
1459 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
1460 "P2P: Unsupported P2P Public Action frame type %d",
1467 static void p2p_rx_action_public(struct p2p_data
*p2p
, const u8
*da
,
1468 const u8
*sa
, const u8
*bssid
, const u8
*data
,
1469 size_t len
, int freq
)
1475 case WLAN_PA_VENDOR_SPECIFIC
:
1480 if (WPA_GET_BE24(data
) != OUI_WFA
)
1488 if (*data
!= P2P_OUI_TYPE
)
1491 p2p_rx_p2p_action(p2p
, sa
, data
+ 1, len
- 1, freq
);
1493 case WLAN_PA_GAS_INITIAL_REQ
:
1494 p2p_rx_gas_initial_req(p2p
, sa
, data
+ 1, len
- 1, freq
);
1496 case WLAN_PA_GAS_INITIAL_RESP
:
1497 p2p_rx_gas_initial_resp(p2p
, sa
, data
+ 1, len
- 1, freq
);
1499 case WLAN_PA_GAS_COMEBACK_REQ
:
1500 p2p_rx_gas_comeback_req(p2p
, sa
, data
+ 1, len
- 1, freq
);
1502 case WLAN_PA_GAS_COMEBACK_RESP
:
1503 p2p_rx_gas_comeback_resp(p2p
, sa
, data
+ 1, len
- 1, freq
);
1509 void p2p_rx_action(struct p2p_data
*p2p
, const u8
*da
, const u8
*sa
,
1510 const u8
*bssid
, u8 category
,
1511 const u8
*data
, size_t len
, int freq
)
1513 if (category
== WLAN_ACTION_PUBLIC
) {
1514 p2p_rx_action_public(p2p
, da
, sa
, bssid
, data
, len
, freq
);
1518 if (category
!= WLAN_ACTION_VENDOR_SPECIFIC
)
1524 if (WPA_GET_BE24(data
) != OUI_WFA
)
1529 if (*data
!= P2P_OUI_TYPE
)
1534 /* P2P action frame */
1535 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
1536 "P2P: RX P2P Action from " MACSTR
, MAC2STR(sa
));
1537 wpa_hexdump(MSG_MSGDUMP
, "P2P: P2P Action contents", data
, len
);
1543 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
1544 "P2P: Received P2P Action - Notice of Absence");
1547 case P2P_PRESENCE_REQ
:
1548 p2p_process_presence_req(p2p
, da
, sa
, data
+ 1, len
- 1, freq
);
1550 case P2P_PRESENCE_RESP
:
1551 p2p_process_presence_resp(p2p
, da
, sa
, data
+ 1, len
- 1);
1553 case P2P_GO_DISC_REQ
:
1554 p2p_process_go_disc_req(p2p
, da
, sa
, data
+ 1, len
- 1, freq
);
1557 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
1558 "P2P: Received P2P Action - unknown type %u", data
[0]);
1564 static void p2p_go_neg_start(void *eloop_ctx
, void *timeout_ctx
)
1566 struct p2p_data
*p2p
= eloop_ctx
;
1567 if (p2p
->go_neg_peer
== NULL
)
1569 p2p
->cfg
->stop_listen(p2p
->cfg
->cb_ctx
);
1570 p2p
->go_neg_peer
->status
= P2P_SC_SUCCESS
;
1571 p2p_connect_send(p2p
, p2p
->go_neg_peer
);
1575 static void p2p_invite_start(void *eloop_ctx
, void *timeout_ctx
)
1577 struct p2p_data
*p2p
= eloop_ctx
;
1578 if (p2p
->invite_peer
== NULL
)
1580 p2p
->cfg
->stop_listen(p2p
->cfg
->cb_ctx
);
1581 p2p_invite_send(p2p
, p2p
->invite_peer
, p2p
->invite_go_dev_addr
);
1585 static void p2p_add_dev_from_probe_req(struct p2p_data
*p2p
, const u8
*addr
,
1586 const u8
*ie
, size_t ie_len
)
1588 struct p2p_message msg
;
1589 struct p2p_device
*dev
;
1591 os_memset(&msg
, 0, sizeof(msg
));
1592 if (p2p_parse_ies(ie
, ie_len
, &msg
) < 0 || msg
.p2p_attributes
== NULL
)
1594 p2p_parse_free(&msg
);
1595 return; /* not a P2P probe */
1598 if (msg
.ssid
== NULL
|| msg
.ssid
[1] != P2P_WILDCARD_SSID_LEN
||
1599 os_memcmp(msg
.ssid
+ 2, P2P_WILDCARD_SSID
, P2P_WILDCARD_SSID_LEN
)
1601 /* The Probe Request is not part of P2P Device Discovery. It is
1602 * not known whether the source address of the frame is the P2P
1603 * Device Address or P2P Interface Address. Do not add a new
1604 * peer entry based on this frames.
1606 p2p_parse_free(&msg
);
1610 dev
= p2p_get_device(p2p
, addr
);
1612 if (dev
->country
[0] == 0 && msg
.listen_channel
)
1613 os_memcpy(dev
->country
, msg
.listen_channel
, 3);
1614 os_get_time(&dev
->last_seen
);
1615 p2p_parse_free(&msg
);
1616 return; /* already known */
1619 dev
= p2p_create_device(p2p
, addr
);
1621 p2p_parse_free(&msg
);
1625 os_get_time(&dev
->last_seen
);
1626 dev
->flags
|= P2P_DEV_PROBE_REQ_ONLY
;
1628 if (msg
.listen_channel
) {
1629 os_memcpy(dev
->country
, msg
.listen_channel
, 3);
1630 dev
->listen_freq
= p2p_channel_to_freq(dev
->country
,
1631 msg
.listen_channel
[3],
1632 msg
.listen_channel
[4]);
1635 p2p_copy_wps_info(dev
, 1, &msg
);
1637 p2p_parse_free(&msg
);
1639 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
1640 "P2P: Created device entry based on Probe Req: " MACSTR
1641 " dev_capab=0x%x group_capab=0x%x name='%s' listen_freq=%d",
1642 MAC2STR(dev
->info
.p2p_device_addr
), dev
->info
.dev_capab
,
1643 dev
->info
.group_capab
, dev
->info
.device_name
,
1648 struct p2p_device
* p2p_add_dev_from_go_neg_req(struct p2p_data
*p2p
,
1650 struct p2p_message
*msg
)
1652 struct p2p_device
*dev
;
1654 dev
= p2p_get_device(p2p
, addr
);
1656 os_get_time(&dev
->last_seen
);
1657 return dev
; /* already known */
1660 dev
= p2p_create_device(p2p
, addr
);
1664 p2p_add_dev_info(p2p
, addr
, dev
, msg
);
1670 static int dev_type_match(const u8
*dev_type
, const u8
*req_dev_type
)
1672 if (os_memcmp(dev_type
, req_dev_type
, WPS_DEV_TYPE_LEN
) == 0)
1674 if (os_memcmp(dev_type
, req_dev_type
, 2) == 0 &&
1675 WPA_GET_BE32(&req_dev_type
[2]) == 0 &&
1676 WPA_GET_BE16(&req_dev_type
[6]) == 0)
1677 return 1; /* Category match with wildcard OUI/sub-category */
1682 int dev_type_list_match(const u8
*dev_type
, const u8
*req_dev_type
[],
1683 size_t num_req_dev_type
)
1686 for (i
= 0; i
< num_req_dev_type
; i
++) {
1687 if (dev_type_match(dev_type
, req_dev_type
[i
]))
1695 * p2p_match_dev_type - Match local device type with requested type
1696 * @p2p: P2P module context from p2p_init()
1697 * @wps: WPS TLVs from Probe Request frame (concatenated WPS IEs)
1698 * Returns: 1 on match, 0 on mismatch
1700 * This function can be used to match the Requested Device Type attribute in
1701 * WPS IE with the local device types for deciding whether to reply to a Probe
1704 int p2p_match_dev_type(struct p2p_data
*p2p
, struct wpabuf
*wps
)
1706 struct wps_parse_attr attr
;
1709 if (wps_parse_msg(wps
, &attr
))
1710 return 1; /* assume no Requested Device Type attributes */
1712 if (attr
.num_req_dev_type
== 0)
1713 return 1; /* no Requested Device Type attributes -> match */
1715 if (dev_type_list_match(p2p
->cfg
->pri_dev_type
, attr
.req_dev_type
,
1716 attr
.num_req_dev_type
))
1717 return 1; /* Own Primary Device Type matches */
1719 for (i
= 0; i
< p2p
->cfg
->num_sec_dev_types
; i
++)
1720 if (dev_type_list_match(p2p
->cfg
->sec_dev_type
[i
],
1722 attr
.num_req_dev_type
))
1723 return 1; /* Own Secondary Device Type matches */
1725 /* No matching device type found */
1730 struct wpabuf
* p2p_build_probe_resp_ies(struct p2p_data
*p2p
)
1735 buf
= wpabuf_alloc(1000);
1739 p2p_build_wps_ie(p2p
, buf
, DEV_PW_DEFAULT
, 1);
1742 len
= p2p_buf_add_ie_hdr(buf
);
1743 p2p_buf_add_capability(buf
, p2p
->dev_capab
, 0);
1744 if (p2p
->ext_listen_interval
)
1745 p2p_buf_add_ext_listen_timing(buf
, p2p
->ext_listen_period
,
1746 p2p
->ext_listen_interval
);
1747 p2p_buf_add_device_info(buf
, p2p
, NULL
);
1748 p2p_buf_update_ie_hdr(buf
, len
);
1754 static int is_11b(u8 rate
)
1756 return rate
== 0x02 || rate
== 0x04 || rate
== 0x0b || rate
== 0x16;
1760 static int supp_rates_11b_only(struct ieee802_11_elems
*elems
)
1762 int num_11b
= 0, num_others
= 0;
1765 if (elems
->supp_rates
== NULL
&& elems
->ext_supp_rates
== NULL
)
1768 for (i
= 0; elems
->supp_rates
&& i
< elems
->supp_rates_len
; i
++) {
1769 if (is_11b(elems
->supp_rates
[i
]))
1775 for (i
= 0; elems
->ext_supp_rates
&& i
< elems
->ext_supp_rates_len
;
1777 if (is_11b(elems
->ext_supp_rates
[i
]))
1783 return num_11b
> 0 && num_others
== 0;
1787 static void p2p_reply_probe(struct p2p_data
*p2p
, const u8
*addr
,
1788 const u8
*dst
, const u8
*bssid
, const u8
*ie
,
1791 struct ieee802_11_elems elems
;
1793 struct ieee80211_mgmt
*resp
;
1794 struct p2p_message msg
;
1797 if (!p2p
->in_listen
|| !p2p
->drv_in_listen
) {
1798 /* not in Listen state - ignore Probe Request */
1802 if (ieee802_11_parse_elems((u8
*) ie
, ie_len
, &elems
, 0) ==
1804 /* Ignore invalid Probe Request frames */
1808 if (elems
.p2p
== NULL
) {
1809 /* not a P2P probe - ignore it */
1813 if (dst
&& !is_broadcast_ether_addr(dst
) &&
1814 os_memcmp(dst
, p2p
->cfg
->dev_addr
, ETH_ALEN
) != 0) {
1815 /* Not sent to the broadcast address or our P2P Device Address
1820 if (bssid
&& !is_broadcast_ether_addr(bssid
)) {
1821 /* Not sent to the Wildcard BSSID */
1825 if (elems
.ssid
== NULL
|| elems
.ssid_len
!= P2P_WILDCARD_SSID_LEN
||
1826 os_memcmp(elems
.ssid
, P2P_WILDCARD_SSID
, P2P_WILDCARD_SSID_LEN
) !=
1828 /* not using P2P Wildcard SSID - ignore */
1832 if (supp_rates_11b_only(&elems
)) {
1833 /* Indicates support for 11b rates only */
1837 os_memset(&msg
, 0, sizeof(msg
));
1838 if (p2p_parse_ies(ie
, ie_len
, &msg
) < 0) {
1839 /* Could not parse P2P attributes */
1843 if (msg
.device_id
&&
1844 os_memcmp(msg
.device_id
, p2p
->cfg
->dev_addr
, ETH_ALEN
!= 0)) {
1845 /* Device ID did not match */
1846 p2p_parse_free(&msg
);
1850 /* Check Requested Device Type match */
1851 if (msg
.wps_attributes
&&
1852 !p2p_match_dev_type(p2p
, msg
.wps_attributes
)) {
1853 /* No match with Requested Device Type */
1854 p2p_parse_free(&msg
);
1857 p2p_parse_free(&msg
);
1859 if (!p2p
->cfg
->send_probe_resp
)
1860 return; /* Response generated elsewhere */
1862 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
1863 "P2P: Reply to P2P Probe Request in Listen state");
1866 * We do not really have a specific BSS that this frame is advertising,
1867 * so build a frame that has some information in valid format. This is
1868 * really only used for discovery purposes, not to learn exact BSS
1871 ies
= p2p_build_probe_resp_ies(p2p
);
1875 buf
= wpabuf_alloc(200 + wpabuf_len(ies
));
1882 resp
= wpabuf_put(buf
, resp
->u
.probe_resp
.variable
- (u8
*) resp
);
1884 resp
->frame_control
= host_to_le16((WLAN_FC_TYPE_MGMT
<< 2) |
1885 (WLAN_FC_STYPE_PROBE_RESP
<< 4));
1886 os_memcpy(resp
->da
, addr
, ETH_ALEN
);
1887 os_memcpy(resp
->sa
, p2p
->cfg
->dev_addr
, ETH_ALEN
);
1888 os_memcpy(resp
->bssid
, p2p
->cfg
->dev_addr
, ETH_ALEN
);
1889 resp
->u
.probe_resp
.beacon_int
= host_to_le16(100);
1890 /* hardware or low-level driver will setup seq_ctrl and timestamp */
1891 resp
->u
.probe_resp
.capab_info
=
1892 host_to_le16(WLAN_CAPABILITY_SHORT_PREAMBLE
|
1893 WLAN_CAPABILITY_PRIVACY
|
1894 WLAN_CAPABILITY_SHORT_SLOT_TIME
);
1896 wpabuf_put_u8(buf
, WLAN_EID_SSID
);
1897 wpabuf_put_u8(buf
, P2P_WILDCARD_SSID_LEN
);
1898 wpabuf_put_data(buf
, P2P_WILDCARD_SSID
, P2P_WILDCARD_SSID_LEN
);
1900 wpabuf_put_u8(buf
, WLAN_EID_SUPP_RATES
);
1901 wpabuf_put_u8(buf
, 8);
1902 wpabuf_put_u8(buf
, (60 / 5) | 0x80);
1903 wpabuf_put_u8(buf
, 90 / 5);
1904 wpabuf_put_u8(buf
, (120 / 5) | 0x80);
1905 wpabuf_put_u8(buf
, 180 / 5);
1906 wpabuf_put_u8(buf
, (240 / 5) | 0x80);
1907 wpabuf_put_u8(buf
, 360 / 5);
1908 wpabuf_put_u8(buf
, 480 / 5);
1909 wpabuf_put_u8(buf
, 540 / 5);
1911 wpabuf_put_u8(buf
, WLAN_EID_DS_PARAMS
);
1912 wpabuf_put_u8(buf
, 1);
1913 wpabuf_put_u8(buf
, p2p
->cfg
->channel
);
1915 wpabuf_put_buf(buf
, ies
);
1918 p2p
->cfg
->send_probe_resp(p2p
->cfg
->cb_ctx
, buf
);
1924 int p2p_probe_req_rx(struct p2p_data
*p2p
, const u8
*addr
, const u8
*dst
,
1925 const u8
*bssid
, const u8
*ie
, size_t ie_len
)
1927 p2p_add_dev_from_probe_req(p2p
, addr
, ie
, ie_len
);
1929 p2p_reply_probe(p2p
, addr
, dst
, bssid
, ie
, ie_len
);
1931 if ((p2p
->state
== P2P_CONNECT
|| p2p
->state
== P2P_CONNECT_LISTEN
) &&
1933 os_memcmp(addr
, p2p
->go_neg_peer
->info
.p2p_device_addr
, ETH_ALEN
)
1935 /* Received a Probe Request from GO Negotiation peer */
1936 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
1937 "P2P: Found GO Negotiation peer - try to start GO "
1938 "negotiation from timeout");
1939 eloop_register_timeout(0, 0, p2p_go_neg_start
, p2p
, NULL
);
1943 if ((p2p
->state
== P2P_INVITE
|| p2p
->state
== P2P_INVITE_LISTEN
) &&
1945 os_memcmp(addr
, p2p
->invite_peer
->info
.p2p_device_addr
, ETH_ALEN
)
1947 /* Received a Probe Request from Invite peer */
1948 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
1949 "P2P: Found Invite peer - try to start Invite from "
1951 eloop_register_timeout(0, 0, p2p_invite_start
, p2p
, NULL
);
1959 static int p2p_assoc_req_ie_wlan_ap(struct p2p_data
*p2p
, const u8
*bssid
,
1960 u8
*buf
, size_t len
, struct wpabuf
*p2p_ie
)
1969 return 0; /* WLAN AP is not a P2P manager */
1972 * (Re)Association Request - P2P IE
1973 * P2P Capability attribute (shall be present)
1974 * P2P Interface attribute (present if concurrent device and
1975 * P2P Management is enabled)
1977 tmp
= wpabuf_alloc(200);
1981 lpos
= p2p_buf_add_ie_hdr(tmp
);
1983 if (p2p
->num_groups
> 0) {
1984 group_capab
|= P2P_GROUP_CAPAB_GROUP_OWNER
;
1985 if ((p2p
->dev_capab
& P2P_DEV_CAPAB_CONCURRENT_OPER
) &&
1986 (p2p
->dev_capab
& P2P_DEV_CAPAB_INFRA_MANAGED
) &&
1988 group_capab
|= P2P_GROUP_CAPAB_CROSS_CONN
;
1990 p2p_buf_add_capability(tmp
, p2p
->dev_capab
, group_capab
);
1991 if ((p2p
->dev_capab
& P2P_DEV_CAPAB_CONCURRENT_OPER
) &&
1992 (p2p
->dev_capab
& P2P_DEV_CAPAB_INFRA_MANAGED
))
1993 p2p_buf_add_p2p_interface(tmp
, p2p
);
1994 p2p_buf_update_ie_hdr(tmp
, lpos
);
1996 tmplen
= wpabuf_len(tmp
);
2000 os_memcpy(buf
, wpabuf_head(tmp
), tmplen
);
2009 int p2p_assoc_req_ie(struct p2p_data
*p2p
, const u8
*bssid
, u8
*buf
,
2010 size_t len
, int p2p_group
, struct wpabuf
*p2p_ie
)
2014 struct p2p_device
*peer
;
2019 return p2p_assoc_req_ie_wlan_ap(p2p
, bssid
, buf
, len
, p2p_ie
);
2022 * (Re)Association Request - P2P IE
2023 * P2P Capability attribute (shall be present)
2024 * Extended Listen Timing (may be present)
2025 * P2P Device Info attribute (shall be present)
2027 tmp
= wpabuf_alloc(200);
2031 peer
= bssid
? p2p_get_device(p2p
, bssid
) : NULL
;
2033 lpos
= p2p_buf_add_ie_hdr(tmp
);
2034 p2p_buf_add_capability(tmp
, p2p
->dev_capab
, 0);
2035 if (p2p
->ext_listen_interval
)
2036 p2p_buf_add_ext_listen_timing(tmp
, p2p
->ext_listen_period
,
2037 p2p
->ext_listen_interval
);
2038 p2p_buf_add_device_info(tmp
, p2p
, peer
);
2039 p2p_buf_update_ie_hdr(tmp
, lpos
);
2041 tmplen
= wpabuf_len(tmp
);
2045 os_memcpy(buf
, wpabuf_head(tmp
), tmplen
);
2054 int p2p_scan_result_text(const u8
*ies
, size_t ies_len
, char *buf
, char *end
)
2056 struct wpabuf
*p2p_ie
;
2059 p2p_ie
= ieee802_11_vendor_ie_concat(ies
, ies_len
, P2P_IE_VENDOR_TYPE
);
2063 ret
= p2p_attr_text(p2p_ie
, buf
, end
);
2064 wpabuf_free(p2p_ie
);
2069 static void p2p_clear_go_neg(struct p2p_data
*p2p
)
2071 p2p
->go_neg_peer
= NULL
;
2072 p2p_clear_timeout(p2p
);
2073 p2p_set_state(p2p
, P2P_IDLE
);
2077 void p2p_wps_success_cb(struct p2p_data
*p2p
, const u8
*mac_addr
)
2079 if (p2p
->go_neg_peer
== NULL
) {
2080 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
2081 "P2P: No pending Group Formation - "
2082 "ignore WPS registration success notification");
2083 return; /* No pending Group Formation */
2086 if (os_memcmp(mac_addr
, p2p
->go_neg_peer
->intended_addr
, ETH_ALEN
) !=
2088 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
2089 "P2P: Ignore WPS registration success notification "
2090 "for " MACSTR
" (GO Negotiation peer " MACSTR
")",
2092 MAC2STR(p2p
->go_neg_peer
->intended_addr
));
2093 return; /* Ignore unexpected peer address */
2096 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
2097 "P2P: Group Formation completed successfully with " MACSTR
,
2100 p2p_clear_go_neg(p2p
);
2104 void p2p_group_formation_failed(struct p2p_data
*p2p
)
2106 if (p2p
->go_neg_peer
== NULL
) {
2107 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
2108 "P2P: No pending Group Formation - "
2109 "ignore group formation failure notification");
2110 return; /* No pending Group Formation */
2113 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
2114 "P2P: Group Formation failed with " MACSTR
,
2115 MAC2STR(p2p
->go_neg_peer
->intended_addr
));
2117 p2p_clear_go_neg(p2p
);
2121 struct p2p_data
* p2p_init(const struct p2p_config
*cfg
)
2123 struct p2p_data
*p2p
;
2125 if (cfg
->max_peers
< 1)
2128 p2p
= os_zalloc(sizeof(*p2p
) + sizeof(*cfg
));
2131 p2p
->cfg
= (struct p2p_config
*) (p2p
+ 1);
2132 os_memcpy(p2p
->cfg
, cfg
, sizeof(*cfg
));
2134 p2p
->cfg
->dev_name
= os_strdup(cfg
->dev_name
);
2135 if (cfg
->manufacturer
)
2136 p2p
->cfg
->manufacturer
= os_strdup(cfg
->manufacturer
);
2137 if (cfg
->model_name
)
2138 p2p
->cfg
->model_name
= os_strdup(cfg
->model_name
);
2139 if (cfg
->model_number
)
2140 p2p
->cfg
->model_number
= os_strdup(cfg
->model_number
);
2141 if (cfg
->serial_number
)
2142 p2p
->cfg
->serial_number
= os_strdup(cfg
->serial_number
);
2144 p2p
->min_disc_int
= 1;
2145 p2p
->max_disc_int
= 3;
2147 os_get_random(&p2p
->next_tie_breaker
, 1);
2148 p2p
->next_tie_breaker
&= 0x01;
2149 if (cfg
->sd_request
)
2150 p2p
->dev_capab
|= P2P_DEV_CAPAB_SERVICE_DISCOVERY
;
2151 p2p
->dev_capab
|= P2P_DEV_CAPAB_INVITATION_PROCEDURE
;
2152 if (cfg
->concurrent_operations
)
2153 p2p
->dev_capab
|= P2P_DEV_CAPAB_CONCURRENT_OPER
;
2154 p2p
->dev_capab
|= P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY
;
2156 dl_list_init(&p2p
->devices
);
2158 eloop_register_timeout(P2P_PEER_EXPIRATION_INTERVAL
, 0,
2159 p2p_expiration_timeout
, p2p
, NULL
);
2165 void p2p_deinit(struct p2p_data
*p2p
)
2167 eloop_cancel_timeout(p2p_expiration_timeout
, p2p
, NULL
);
2168 eloop_cancel_timeout(p2p_ext_listen_timeout
, p2p
, NULL
);
2169 eloop_cancel_timeout(p2p_scan_timeout
, p2p
, NULL
);
2171 p2p_free_req_dev_types(p2p
);
2172 os_free(p2p
->cfg
->dev_name
);
2173 os_free(p2p
->cfg
->manufacturer
);
2174 os_free(p2p
->cfg
->model_name
);
2175 os_free(p2p
->cfg
->model_number
);
2176 os_free(p2p
->cfg
->serial_number
);
2177 os_free(p2p
->groups
);
2178 wpabuf_free(p2p
->sd_resp
);
2179 os_free(p2p
->after_scan_tx
);
2180 p2p_remove_wps_vendor_extensions(p2p
);
2185 void p2p_flush(struct p2p_data
*p2p
)
2187 struct p2p_device
*dev
, *prev
;
2188 p2p_clear_timeout(p2p
);
2189 p2p_set_state(p2p
, P2P_IDLE
);
2190 p2p
->start_after_scan
= P2P_AFTER_SCAN_NOTHING
;
2191 p2p
->go_neg_peer
= NULL
;
2192 eloop_cancel_timeout(p2p_find_timeout
, p2p
, NULL
);
2193 dl_list_for_each_safe(dev
, prev
, &p2p
->devices
, struct p2p_device
,
2195 dl_list_del(&dev
->list
);
2196 p2p_device_free(p2p
, dev
);
2198 p2p_free_sd_queries(p2p
);
2199 os_free(p2p
->after_scan_tx
);
2200 p2p
->after_scan_tx
= NULL
;
2204 int p2p_unauthorize(struct p2p_data
*p2p
, const u8
*addr
)
2206 struct p2p_device
*dev
;
2208 dev
= p2p_get_device(p2p
, addr
);
2212 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
, "P2P: Unauthorizing " MACSTR
,
2215 if (p2p
->go_neg_peer
== dev
)
2216 p2p
->go_neg_peer
= NULL
;
2218 dev
->wps_method
= WPS_NOT_READY
;
2219 dev
->flags
&= ~P2P_DEV_WAIT_GO_NEG_RESPONSE
;
2220 dev
->flags
&= ~P2P_DEV_WAIT_GO_NEG_CONFIRM
;
2222 /* Check if after_scan_tx is for this peer. If so free it */
2223 if (p2p
->after_scan_tx
&&
2224 os_memcmp(addr
, p2p
->after_scan_tx
->dst
, ETH_ALEN
) == 0) {
2225 os_free(p2p
->after_scan_tx
);
2226 p2p
->after_scan_tx
= NULL
;
2233 int p2p_set_dev_name(struct p2p_data
*p2p
, const char *dev_name
)
2235 os_free(p2p
->cfg
->dev_name
);
2237 p2p
->cfg
->dev_name
= os_strdup(dev_name
);
2238 if (p2p
->cfg
->dev_name
== NULL
)
2241 p2p
->cfg
->dev_name
= NULL
;
2246 int p2p_set_manufacturer(struct p2p_data
*p2p
, const char *manufacturer
)
2248 os_free(p2p
->cfg
->manufacturer
);
2249 p2p
->cfg
->manufacturer
= NULL
;
2251 p2p
->cfg
->manufacturer
= os_strdup(manufacturer
);
2252 if (p2p
->cfg
->manufacturer
== NULL
)
2260 int p2p_set_model_name(struct p2p_data
*p2p
, const char *model_name
)
2262 os_free(p2p
->cfg
->model_name
);
2263 p2p
->cfg
->model_name
= NULL
;
2265 p2p
->cfg
->model_name
= os_strdup(model_name
);
2266 if (p2p
->cfg
->model_name
== NULL
)
2274 int p2p_set_model_number(struct p2p_data
*p2p
, const char *model_number
)
2276 os_free(p2p
->cfg
->model_number
);
2277 p2p
->cfg
->model_number
= NULL
;
2279 p2p
->cfg
->model_number
= os_strdup(model_number
);
2280 if (p2p
->cfg
->model_number
== NULL
)
2288 int p2p_set_serial_number(struct p2p_data
*p2p
, const char *serial_number
)
2290 os_free(p2p
->cfg
->serial_number
);
2291 p2p
->cfg
->serial_number
= NULL
;
2292 if (serial_number
) {
2293 p2p
->cfg
->serial_number
= os_strdup(serial_number
);
2294 if (p2p
->cfg
->serial_number
== NULL
)
2302 void p2p_set_config_methods(struct p2p_data
*p2p
, u16 config_methods
)
2304 p2p
->cfg
->config_methods
= config_methods
;
2308 void p2p_set_uuid(struct p2p_data
*p2p
, const u8
*uuid
)
2310 os_memcpy(p2p
->cfg
->uuid
, uuid
, 16);
2314 int p2p_set_pri_dev_type(struct p2p_data
*p2p
, const u8
*pri_dev_type
)
2316 os_memcpy(p2p
->cfg
->pri_dev_type
, pri_dev_type
, 8);
2321 int p2p_set_sec_dev_types(struct p2p_data
*p2p
, const u8 dev_types
[][8],
2322 size_t num_dev_types
)
2324 if (num_dev_types
> P2P_SEC_DEVICE_TYPES
)
2325 num_dev_types
= P2P_SEC_DEVICE_TYPES
;
2326 p2p
->cfg
->num_sec_dev_types
= num_dev_types
;
2327 os_memcpy(p2p
->cfg
->sec_dev_type
, dev_types
, num_dev_types
* 8);
2332 void p2p_remove_wps_vendor_extensions(struct p2p_data
*p2p
)
2336 for (i
= 0; i
< P2P_MAX_WPS_VENDOR_EXT
; i
++) {
2337 wpabuf_free(p2p
->wps_vendor_ext
[i
]);
2338 p2p
->wps_vendor_ext
[i
] = NULL
;
2343 int p2p_add_wps_vendor_extension(struct p2p_data
*p2p
,
2344 const struct wpabuf
*vendor_ext
)
2348 if (vendor_ext
== NULL
)
2351 for (i
= 0; i
< P2P_MAX_WPS_VENDOR_EXT
; i
++) {
2352 if (p2p
->wps_vendor_ext
[i
] == NULL
)
2355 if (i
>= P2P_MAX_WPS_VENDOR_EXT
)
2358 p2p
->wps_vendor_ext
[i
] = wpabuf_dup(vendor_ext
);
2359 if (p2p
->wps_vendor_ext
[i
] == NULL
)
2366 int p2p_set_country(struct p2p_data
*p2p
, const char *country
)
2368 os_memcpy(p2p
->cfg
->country
, country
, 3);
2373 void p2p_continue_find(struct p2p_data
*p2p
)
2375 struct p2p_device
*dev
;
2376 p2p_set_state(p2p
, P2P_SEARCH
);
2377 dl_list_for_each(dev
, &p2p
->devices
, struct p2p_device
, list
) {
2378 if (dev
->flags
& P2P_DEV_SD_SCHEDULE
) {
2379 if (p2p_start_sd(p2p
, dev
) == 0)
2383 } else if (dev
->req_config_methods
&&
2384 !(dev
->flags
& P2P_DEV_PD_FOR_JOIN
)) {
2385 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
, "P2P: Send "
2386 "pending Provision Discovery Request to "
2387 MACSTR
" (config methods 0x%x)",
2388 MAC2STR(dev
->info
.p2p_device_addr
),
2389 dev
->req_config_methods
);
2390 if (p2p_send_prov_disc_req(p2p
, dev
, 0, 0) == 0)
2395 p2p_listen_in_find(p2p
);
2399 static void p2p_sd_cb(struct p2p_data
*p2p
, int success
)
2401 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
2402 "P2P: Service Discovery Query TX callback: success=%d",
2404 p2p
->pending_action_state
= P2P_NO_PENDING_ACTION
;
2408 p2p
->sd_peer
->flags
&= ~P2P_DEV_SD_SCHEDULE
;
2409 p2p
->sd_peer
= NULL
;
2411 p2p_continue_find(p2p
);
2415 if (p2p
->sd_peer
== NULL
) {
2416 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
2417 "P2P: No SD peer entry known");
2418 p2p_continue_find(p2p
);
2422 /* Wait for response from the peer */
2423 p2p_set_state(p2p
, P2P_SD_DURING_FIND
);
2424 p2p_set_timeout(p2p
, 0, 200000);
2429 * p2p_retry_pd - Retry any pending provision disc requests in IDLE state
2430 * @p2p: P2P module context from p2p_init()
2432 static void p2p_retry_pd(struct p2p_data
*p2p
)
2434 struct p2p_device
*dev
;
2436 if (p2p
->state
!= P2P_IDLE
)
2440 * Retry the prov disc req attempt only for the peer that the user had
2441 * requested for and provided a join has not been initiated on it
2445 dl_list_for_each(dev
, &p2p
->devices
, struct p2p_device
, list
) {
2446 if (os_memcmp(p2p
->pending_pd_devaddr
,
2447 dev
->info
.p2p_device_addr
, ETH_ALEN
) != 0)
2449 if (!dev
->req_config_methods
)
2451 if (dev
->flags
& P2P_DEV_PD_FOR_JOIN
)
2454 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
, "P2P: Send "
2455 "pending Provision Discovery Request to "
2456 MACSTR
" (config methods 0x%x)",
2457 MAC2STR(dev
->info
.p2p_device_addr
),
2458 dev
->req_config_methods
);
2459 p2p_send_prov_disc_req(p2p
, dev
, 0, 0);
2465 static void p2p_prov_disc_cb(struct p2p_data
*p2p
, int success
)
2467 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
2468 "P2P: Provision Discovery Request TX callback: success=%d",
2472 * Postpone resetting the pending action state till after we actually
2473 * time out. This allows us to take some action like notifying any
2474 * interested parties about no response to the request.
2476 * When the timer (below) goes off we check in IDLE, SEARCH, or
2477 * LISTEN_ONLY state, which are the only allowed states to issue a PD
2478 * requests in, if this was still pending and then raise notification.
2482 p2p
->pending_action_state
= P2P_NO_PENDING_ACTION
;
2484 if (p2p
->state
!= P2P_IDLE
)
2485 p2p_continue_find(p2p
);
2486 else if (p2p
->user_initiated_pd
) {
2487 p2p
->pending_action_state
= P2P_PENDING_PD
;
2488 p2p_set_timeout(p2p
, 0, 300000);
2494 * This postponing, of resetting pending_action_state, needs to be
2495 * done only for user initiated PD requests and not internal ones.
2497 if (p2p
->user_initiated_pd
)
2498 p2p
->pending_action_state
= P2P_PENDING_PD
;
2500 p2p
->pending_action_state
= P2P_NO_PENDING_ACTION
;
2502 /* Wait for response from the peer */
2503 if (p2p
->state
== P2P_SEARCH
)
2504 p2p_set_state(p2p
, P2P_PD_DURING_FIND
);
2505 p2p_set_timeout(p2p
, 0, 200000);
2509 int p2p_scan_res_handler(struct p2p_data
*p2p
, const u8
*bssid
, int freq
,
2510 int level
, const u8
*ies
, size_t ies_len
)
2512 p2p_add_device(p2p
, bssid
, freq
, level
, ies
, ies_len
);
2514 if (p2p
->go_neg_peer
&& p2p
->state
== P2P_SEARCH
&&
2515 os_memcmp(p2p
->go_neg_peer
->info
.p2p_device_addr
, bssid
, ETH_ALEN
)
2517 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
2518 "P2P: Found GO Negotiation peer - try to start GO "
2520 p2p_connect_send(p2p
, p2p
->go_neg_peer
);
2528 void p2p_scan_res_handled(struct p2p_data
*p2p
)
2530 if (!p2p
->p2p_scan_running
) {
2531 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
, "P2P: p2p_scan was not "
2532 "running, but scan results received");
2534 p2p
->p2p_scan_running
= 0;
2535 eloop_cancel_timeout(p2p_scan_timeout
, p2p
, NULL
);
2537 if (p2p_run_after_scan(p2p
))
2539 if (p2p
->state
== P2P_SEARCH
)
2540 p2p_continue_find(p2p
);
2544 void p2p_scan_ie(struct p2p_data
*p2p
, struct wpabuf
*ies
, const u8
*dev_id
)
2546 u8
*len
= p2p_buf_add_ie_hdr(ies
);
2547 p2p_buf_add_capability(ies
, p2p
->dev_capab
, 0);
2549 p2p_buf_add_device_id(ies
, dev_id
);
2550 if (p2p
->cfg
->reg_class
&& p2p
->cfg
->channel
)
2551 p2p_buf_add_listen_channel(ies
, p2p
->cfg
->country
,
2552 p2p
->cfg
->reg_class
,
2554 if (p2p
->ext_listen_interval
)
2555 p2p_buf_add_ext_listen_timing(ies
, p2p
->ext_listen_period
,
2556 p2p
->ext_listen_interval
);
2557 /* TODO: p2p_buf_add_operating_channel() if GO */
2558 p2p_buf_update_ie_hdr(ies
, len
);
2562 size_t p2p_scan_ie_buf_len(struct p2p_data
*p2p
)
2568 int p2p_ie_text(struct wpabuf
*p2p_ie
, char *buf
, char *end
)
2570 return p2p_attr_text(p2p_ie
, buf
, end
);
2574 static void p2p_go_neg_req_cb(struct p2p_data
*p2p
, int success
)
2576 struct p2p_device
*dev
= p2p
->go_neg_peer
;
2578 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
2579 "P2P: GO Negotiation Request TX callback: success=%d",
2583 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
2584 "P2P: No pending GO Negotiation");
2589 dev
->go_neg_req_sent
++;
2590 if (dev
->flags
& P2P_DEV_USER_REJECTED
) {
2591 p2p_set_state(p2p
, P2P_IDLE
);
2597 (dev
->info
.dev_capab
& P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY
) &&
2598 !is_zero_ether_addr(dev
->member_in_go_dev
)) {
2599 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
2600 "P2P: Peer " MACSTR
" did not acknowledge request - "
2601 "try to use device discoverability through its GO",
2602 MAC2STR(dev
->info
.p2p_device_addr
));
2603 p2p
->cfg
->send_action_done(p2p
->cfg
->cb_ctx
);
2604 p2p_send_dev_disc_req(p2p
, dev
);
2609 * Use P2P find, if needed, to find the other device from its listen
2612 p2p_set_state(p2p
, P2P_CONNECT
);
2613 p2p_set_timeout(p2p
, 0, 100000);
2617 static void p2p_go_neg_resp_cb(struct p2p_data
*p2p
, int success
)
2619 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
2620 "P2P: GO Negotiation Response TX callback: success=%d",
2622 if (!p2p
->go_neg_peer
&& p2p
->state
== P2P_PROVISIONING
) {
2623 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
2624 "P2P: Ignore TX callback event - GO Negotiation is "
2625 "not running anymore");
2628 p2p_set_state(p2p
, P2P_CONNECT
);
2629 p2p_set_timeout(p2p
, 0, 100000);
2633 static void p2p_go_neg_resp_failure_cb(struct p2p_data
*p2p
, int success
)
2635 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
2636 "P2P: GO Negotiation Response (failure) TX callback: "
2637 "success=%d", success
);
2638 if (p2p
->go_neg_peer
&& p2p
->go_neg_peer
->status
!= P2P_SC_SUCCESS
) {
2639 p2p_go_neg_failed(p2p
, p2p
->go_neg_peer
,
2640 p2p
->go_neg_peer
->status
);
2645 static void p2p_go_neg_conf_cb(struct p2p_data
*p2p
,
2646 enum p2p_send_action_result result
)
2648 struct p2p_device
*dev
;
2650 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
2651 "P2P: GO Negotiation Confirm TX callback: result=%d",
2653 p2p
->cfg
->send_action_done(p2p
->cfg
->cb_ctx
);
2654 if (result
== P2P_SEND_ACTION_FAILED
) {
2655 p2p_go_neg_failed(p2p
, p2p
->go_neg_peer
, -1);
2658 if (result
== P2P_SEND_ACTION_NO_ACK
) {
2660 * It looks like the TX status for GO Negotiation Confirm is
2661 * often showing failure even when the peer has actually
2662 * received the frame. Since the peer may change channels
2663 * immediately after having received the frame, we may not see
2664 * an Ack for retries, so just dropping a single frame may
2665 * trigger this. To allow the group formation to succeed if the
2666 * peer did indeed receive the frame, continue regardless of
2669 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
2670 "P2P: Assume GO Negotiation Confirm TX was actually "
2671 "received by the peer even though Ack was not "
2675 dev
= p2p
->go_neg_peer
;
2679 p2p_go_complete(p2p
, dev
);
2683 void p2p_send_action_cb(struct p2p_data
*p2p
, unsigned int freq
, const u8
*dst
,
2684 const u8
*src
, const u8
*bssid
,
2685 enum p2p_send_action_result result
)
2687 enum p2p_pending_action_state state
;
2690 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
2691 "P2P: Action frame TX callback (state=%d freq=%u dst=" MACSTR
2692 " src=" MACSTR
" bssid=" MACSTR
" result=%d",
2693 p2p
->pending_action_state
, freq
, MAC2STR(dst
), MAC2STR(src
),
2694 MAC2STR(bssid
), result
);
2695 success
= result
== P2P_SEND_ACTION_SUCCESS
;
2696 state
= p2p
->pending_action_state
;
2697 p2p
->pending_action_state
= P2P_NO_PENDING_ACTION
;
2699 case P2P_NO_PENDING_ACTION
:
2701 case P2P_PENDING_GO_NEG_REQUEST
:
2702 p2p_go_neg_req_cb(p2p
, success
);
2704 case P2P_PENDING_GO_NEG_RESPONSE
:
2705 p2p_go_neg_resp_cb(p2p
, success
);
2707 case P2P_PENDING_GO_NEG_RESPONSE_FAILURE
:
2708 p2p_go_neg_resp_failure_cb(p2p
, success
);
2710 case P2P_PENDING_GO_NEG_CONFIRM
:
2711 p2p_go_neg_conf_cb(p2p
, result
);
2713 case P2P_PENDING_SD
:
2714 p2p_sd_cb(p2p
, success
);
2716 case P2P_PENDING_PD
:
2717 p2p_prov_disc_cb(p2p
, success
);
2719 case P2P_PENDING_INVITATION_REQUEST
:
2720 p2p_invitation_req_cb(p2p
, success
);
2722 case P2P_PENDING_INVITATION_RESPONSE
:
2723 p2p_invitation_resp_cb(p2p
, success
);
2725 case P2P_PENDING_DEV_DISC_REQUEST
:
2726 p2p_dev_disc_req_cb(p2p
, success
);
2728 case P2P_PENDING_DEV_DISC_RESPONSE
:
2729 p2p_dev_disc_resp_cb(p2p
, success
);
2731 case P2P_PENDING_GO_DISC_REQ
:
2732 p2p_go_disc_req_cb(p2p
, success
);
2738 void p2p_listen_cb(struct p2p_data
*p2p
, unsigned int freq
,
2739 unsigned int duration
)
2741 if (freq
== p2p
->pending_client_disc_freq
) {
2742 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
2743 "P2P: Client discoverability remain-awake completed");
2744 p2p
->pending_client_disc_freq
= 0;
2748 if (freq
!= p2p
->pending_listen_freq
) {
2749 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
2750 "P2P: Unexpected listen callback for freq=%u "
2751 "duration=%u (pending_listen_freq=%u)",
2752 freq
, duration
, p2p
->pending_listen_freq
);
2756 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
2757 "P2P: Starting Listen timeout(%u,%u) on freq=%u based on "
2759 p2p
->pending_listen_sec
, p2p
->pending_listen_usec
,
2760 p2p
->pending_listen_freq
);
2762 p2p
->drv_in_listen
= freq
;
2763 if (p2p
->pending_listen_sec
|| p2p
->pending_listen_usec
) {
2765 * Add 20 msec extra wait to avoid race condition with driver
2766 * remain-on-channel end event, i.e., give driver more time to
2767 * complete the operation before our timeout expires.
2769 p2p_set_timeout(p2p
, p2p
->pending_listen_sec
,
2770 p2p
->pending_listen_usec
+ 20000);
2773 p2p
->pending_listen_freq
= 0;
2777 int p2p_listen_end(struct p2p_data
*p2p
, unsigned int freq
)
2779 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
, "P2P: Driver ended Listen "
2780 "state (freq=%u)", freq
);
2781 p2p
->drv_in_listen
= 0;
2783 return 0; /* Internal timeout will trigger the next step */
2785 if (p2p
->state
== P2P_CONNECT_LISTEN
&& p2p
->go_neg_peer
) {
2786 if (p2p
->go_neg_peer
->connect_reqs
>= 120) {
2787 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
2788 "P2P: Timeout on sending GO Negotiation "
2789 "Request without getting response");
2790 p2p_go_neg_failed(p2p
, p2p
->go_neg_peer
, -1);
2794 p2p_set_state(p2p
, P2P_CONNECT
);
2795 p2p_connect_send(p2p
, p2p
->go_neg_peer
);
2797 } else if (p2p
->state
== P2P_SEARCH
) {
2806 static void p2p_timeout_connect(struct p2p_data
*p2p
)
2808 p2p
->cfg
->send_action_done(p2p
->cfg
->cb_ctx
);
2809 p2p_set_state(p2p
, P2P_CONNECT_LISTEN
);
2810 p2p_listen_in_find(p2p
);
2814 static void p2p_timeout_connect_listen(struct p2p_data
*p2p
)
2816 if (p2p
->go_neg_peer
) {
2817 if (p2p
->drv_in_listen
) {
2818 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
, "P2P: Driver is "
2819 "still in Listen state; wait for it to "
2824 if (p2p
->go_neg_peer
->connect_reqs
>= 120) {
2825 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
2826 "P2P: Timeout on sending GO Negotiation "
2827 "Request without getting response");
2828 p2p_go_neg_failed(p2p
, p2p
->go_neg_peer
, -1);
2832 p2p_set_state(p2p
, P2P_CONNECT
);
2833 p2p_connect_send(p2p
, p2p
->go_neg_peer
);
2835 p2p_set_state(p2p
, P2P_IDLE
);
2839 static void p2p_timeout_wait_peer_connect(struct p2p_data
*p2p
)
2842 * TODO: could remain constantly in Listen state for some time if there
2843 * are no other concurrent uses for the radio. For now, go to listen
2844 * state once per second to give other uses a chance to use the radio.
2846 p2p_set_state(p2p
, P2P_WAIT_PEER_IDLE
);
2847 p2p_set_timeout(p2p
, 0, 500000);
2851 static void p2p_timeout_wait_peer_idle(struct p2p_data
*p2p
)
2853 struct p2p_device
*dev
= p2p
->go_neg_peer
;
2856 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
2857 "P2P: Unknown GO Neg peer - stop GO Neg wait");
2862 if (dev
->wait_count
>= 120) {
2863 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
2864 "P2P: Timeout on waiting peer to become ready for GO "
2866 p2p_go_neg_failed(p2p
, dev
, -1);
2870 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
2871 "P2P: Go to Listen state while waiting for the peer to become "
2872 "ready for GO Negotiation");
2873 p2p_set_state(p2p
, P2P_WAIT_PEER_CONNECT
);
2874 p2p_listen_in_find(p2p
);
2878 static void p2p_timeout_sd_during_find(struct p2p_data
*p2p
)
2880 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
2881 "P2P: Service Discovery Query timeout");
2883 p2p
->cfg
->send_action_done(p2p
->cfg
->cb_ctx
);
2884 p2p
->sd_peer
->flags
&= ~P2P_DEV_SD_SCHEDULE
;
2885 p2p
->sd_peer
= NULL
;
2887 p2p_continue_find(p2p
);
2891 static void p2p_timeout_prov_disc_during_find(struct p2p_data
*p2p
)
2893 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
2894 "P2P: Provision Discovery Request timeout");
2895 p2p
->cfg
->send_action_done(p2p
->cfg
->cb_ctx
);
2896 p2p_continue_find(p2p
);
2900 static void p2p_timeout_prov_disc_req(struct p2p_data
*p2p
)
2902 p2p
->pending_action_state
= P2P_NO_PENDING_ACTION
;
2905 * For user initiated PD requests that we have not gotten any responses
2906 * for while in IDLE state, we retry them a couple of times before
2909 if (!p2p
->user_initiated_pd
)
2912 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
2913 "P2P: User initiated Provision Discovery Request timeout");
2915 if (p2p
->pd_retries
) {
2919 if (p2p
->cfg
->prov_disc_fail
)
2920 p2p
->cfg
->prov_disc_fail(p2p
->cfg
->cb_ctx
,
2921 p2p
->pending_pd_devaddr
,
2922 P2P_PROV_DISC_TIMEOUT
);
2923 p2p_reset_pending_pd(p2p
);
2928 static void p2p_timeout_invite(struct p2p_data
*p2p
)
2930 p2p
->cfg
->send_action_done(p2p
->cfg
->cb_ctx
);
2931 p2p_set_state(p2p
, P2P_INVITE_LISTEN
);
2932 if (p2p
->inv_role
== P2P_INVITE_ROLE_ACTIVE_GO
) {
2934 * Better remain on operating channel instead of listen channel
2935 * when running a group.
2937 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
, "P2P: Inviting in "
2938 "active GO role - wait on operating channel");
2939 p2p_set_timeout(p2p
, 0, 100000);
2942 p2p_listen_in_find(p2p
);
2946 static void p2p_timeout_invite_listen(struct p2p_data
*p2p
)
2948 if (p2p
->invite_peer
&& p2p
->invite_peer
->invitation_reqs
< 100) {
2949 p2p_set_state(p2p
, P2P_INVITE
);
2950 p2p_invite_send(p2p
, p2p
->invite_peer
,
2951 p2p
->invite_go_dev_addr
);
2953 if (p2p
->invite_peer
) {
2954 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
2955 "P2P: Invitation Request retry limit reached");
2956 if (p2p
->cfg
->invitation_result
)
2957 p2p
->cfg
->invitation_result(
2958 p2p
->cfg
->cb_ctx
, -1, NULL
);
2960 p2p_set_state(p2p
, P2P_IDLE
);
2965 static void p2p_state_timeout(void *eloop_ctx
, void *timeout_ctx
)
2967 struct p2p_data
*p2p
= eloop_ctx
;
2969 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
, "P2P: Timeout (state=%s)",
2970 p2p_state_txt(p2p
->state
));
2974 switch (p2p
->state
) {
2976 /* Check if we timed out waiting for PD req */
2977 if (p2p
->pending_action_state
== P2P_PENDING_PD
)
2978 p2p_timeout_prov_disc_req(p2p
);
2981 /* Check if we timed out waiting for PD req */
2982 if (p2p
->pending_action_state
== P2P_PENDING_PD
)
2983 p2p_timeout_prov_disc_req(p2p
);
2987 p2p_timeout_connect(p2p
);
2989 case P2P_CONNECT_LISTEN
:
2990 p2p_timeout_connect_listen(p2p
);
2994 case P2P_LISTEN_ONLY
:
2995 /* Check if we timed out waiting for PD req */
2996 if (p2p
->pending_action_state
== P2P_PENDING_PD
)
2997 p2p_timeout_prov_disc_req(p2p
);
2999 if (p2p
->ext_listen_only
) {
3000 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
3001 "P2P: Extended Listen Timing - Listen State "
3003 p2p
->ext_listen_only
= 0;
3004 p2p_set_state(p2p
, P2P_IDLE
);
3007 case P2P_WAIT_PEER_CONNECT
:
3008 p2p_timeout_wait_peer_connect(p2p
);
3010 case P2P_WAIT_PEER_IDLE
:
3011 p2p_timeout_wait_peer_idle(p2p
);
3013 case P2P_SD_DURING_FIND
:
3014 p2p_timeout_sd_during_find(p2p
);
3016 case P2P_PROVISIONING
:
3018 case P2P_PD_DURING_FIND
:
3019 p2p_timeout_prov_disc_during_find(p2p
);
3022 p2p_timeout_invite(p2p
);
3024 case P2P_INVITE_LISTEN
:
3025 p2p_timeout_invite_listen(p2p
);
3027 case P2P_SEARCH_WHEN_READY
:
3033 int p2p_reject(struct p2p_data
*p2p
, const u8
*peer_addr
)
3035 struct p2p_device
*dev
;
3037 dev
= p2p_get_device(p2p
, peer_addr
);
3038 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
, "P2P: Local request to reject "
3039 "connection attempts by peer " MACSTR
, MAC2STR(peer_addr
));
3041 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
, "P2P: Peer " MACSTR
3042 " unknown", MAC2STR(peer_addr
));
3045 dev
->status
= P2P_SC_FAIL_REJECTED_BY_USER
;
3046 dev
->flags
|= P2P_DEV_USER_REJECTED
;
3051 const char * p2p_wps_method_text(enum p2p_wps_method method
)
3056 case WPS_PIN_DISPLAY
:
3058 case WPS_PIN_KEYPAD
:
3068 static const char * p2p_go_state_text(enum p2p_go_state go_state
)
3083 const struct p2p_peer_info
* p2p_get_peer_info(struct p2p_data
*p2p
,
3084 const u8
*addr
, int next
)
3086 struct p2p_device
*dev
;
3089 dev
= p2p_get_device(p2p
, addr
);
3091 dev
= dl_list_first(&p2p
->devices
, struct p2p_device
, list
);
3094 dev
= dl_list_first(&dev
->list
, struct p2p_device
, list
);
3095 if (&dev
->list
== &p2p
->devices
)
3106 int p2p_get_peer_info_txt(const struct p2p_peer_info
*info
,
3107 char *buf
, size_t buflen
)
3109 struct p2p_device
*dev
;
3117 dev
= (struct p2p_device
*) (((u8
*) info
) -
3118 offsetof(struct p2p_device
, info
));
3124 res
= os_snprintf(pos
, end
- pos
,
3128 "interface_addr=" MACSTR
"\n"
3129 "member_in_go_dev=" MACSTR
"\n"
3130 "member_in_go_iface=" MACSTR
"\n"
3131 "go_neg_req_sent=%d\n"
3134 "intended_addr=" MACSTR
"\n"
3137 "req_config_methods=0x%x\n"
3138 "flags=%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n"
3141 "invitation_reqs=%u\n",
3142 (int) (now
.sec
- dev
->last_seen
.sec
),
3144 p2p_wps_method_text(dev
->wps_method
),
3145 MAC2STR(dev
->interface_addr
),
3146 MAC2STR(dev
->member_in_go_dev
),
3147 MAC2STR(dev
->member_in_go_iface
),
3148 dev
->go_neg_req_sent
,
3149 p2p_go_state_text(dev
->go_state
),
3151 MAC2STR(dev
->intended_addr
),
3152 dev
->country
[0] ? dev
->country
[0] : '_',
3153 dev
->country
[1] ? dev
->country
[1] : '_',
3155 dev
->req_config_methods
,
3156 dev
->flags
& P2P_DEV_PROBE_REQ_ONLY
?
3157 "[PROBE_REQ_ONLY]" : "",
3158 dev
->flags
& P2P_DEV_REPORTED
? "[REPORTED]" : "",
3159 dev
->flags
& P2P_DEV_NOT_YET_READY
?
3160 "[NOT_YET_READY]" : "",
3161 dev
->flags
& P2P_DEV_SD_INFO
? "[SD_INFO]" : "",
3162 dev
->flags
& P2P_DEV_SD_SCHEDULE
? "[SD_SCHEDULE]" :
3164 dev
->flags
& P2P_DEV_PD_PEER_DISPLAY
?
3165 "[PD_PEER_DISPLAY]" : "",
3166 dev
->flags
& P2P_DEV_PD_PEER_KEYPAD
?
3167 "[PD_PEER_KEYPAD]" : "",
3168 dev
->flags
& P2P_DEV_USER_REJECTED
?
3169 "[USER_REJECTED]" : "",
3170 dev
->flags
& P2P_DEV_PEER_WAITING_RESPONSE
?
3171 "[PEER_WAITING_RESPONSE]" : "",
3172 dev
->flags
& P2P_DEV_PREFER_PERSISTENT_GROUP
?
3173 "[PREFER_PERSISTENT_GROUP]" : "",
3174 dev
->flags
& P2P_DEV_WAIT_GO_NEG_RESPONSE
?
3175 "[WAIT_GO_NEG_RESPONSE]" : "",
3176 dev
->flags
& P2P_DEV_WAIT_GO_NEG_CONFIRM
?
3177 "[WAIT_GO_NEG_CONFIRM]" : "",
3178 dev
->flags
& P2P_DEV_GROUP_CLIENT_ONLY
?
3179 "[GROUP_CLIENT_ONLY]" : "",
3180 dev
->flags
& P2P_DEV_FORCE_FREQ
?
3181 "[FORCE_FREQ]" : "",
3182 dev
->flags
& P2P_DEV_PD_FOR_JOIN
?
3183 "[PD_FOR_JOIN]" : "",
3186 dev
->invitation_reqs
);
3187 if (res
< 0 || res
>= end
- pos
)
3191 if (dev
->ext_listen_period
) {
3192 res
= os_snprintf(pos
, end
- pos
,
3193 "ext_listen_period=%u\n"
3194 "ext_listen_interval=%u\n",
3195 dev
->ext_listen_period
,
3196 dev
->ext_listen_interval
);
3197 if (res
< 0 || res
>= end
- pos
)
3202 if (dev
->oper_ssid_len
) {
3203 res
= os_snprintf(pos
, end
- pos
,
3205 wpa_ssid_txt(dev
->oper_ssid
,
3206 dev
->oper_ssid_len
));
3207 if (res
< 0 || res
>= end
- pos
)
3216 int p2p_peer_known(struct p2p_data
*p2p
, const u8
*addr
)
3218 return p2p_get_device(p2p
, addr
) != NULL
;
3222 void p2p_set_client_discoverability(struct p2p_data
*p2p
, int enabled
)
3225 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
, "P2P: Client "
3226 "discoverability enabled");
3227 p2p
->dev_capab
|= P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY
;
3229 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
, "P2P: Client "
3230 "discoverability disabled");
3231 p2p
->dev_capab
&= ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY
;
3236 static struct wpabuf
* p2p_build_presence_req(u32 duration1
, u32 interval1
,
3237 u32 duration2
, u32 interval2
)
3240 struct p2p_noa_desc desc1
, desc2
, *ptr1
= NULL
, *ptr2
= NULL
;
3243 req
= wpabuf_alloc(100);
3247 if (duration1
|| interval1
) {
3248 os_memset(&desc1
, 0, sizeof(desc1
));
3249 desc1
.count_type
= 1;
3250 desc1
.duration
= duration1
;
3251 desc1
.interval
= interval1
;
3254 if (duration2
|| interval2
) {
3255 os_memset(&desc2
, 0, sizeof(desc2
));
3256 desc2
.count_type
= 2;
3257 desc2
.duration
= duration2
;
3258 desc2
.interval
= interval2
;
3263 p2p_buf_add_action_hdr(req
, P2P_PRESENCE_REQ
, 1);
3264 len
= p2p_buf_add_ie_hdr(req
);
3265 p2p_buf_add_noa(req
, 0, 0, 0, ptr1
, ptr2
);
3266 p2p_buf_update_ie_hdr(req
, len
);
3272 int p2p_presence_req(struct p2p_data
*p2p
, const u8
*go_interface_addr
,
3273 const u8
*own_interface_addr
, unsigned int freq
,
3274 u32 duration1
, u32 interval1
, u32 duration2
,
3279 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
, "P2P: Send Presence Request to "
3280 "GO " MACSTR
" (own interface " MACSTR
") freq=%u dur1=%u "
3281 "int1=%u dur2=%u int2=%u",
3282 MAC2STR(go_interface_addr
), MAC2STR(own_interface_addr
),
3283 freq
, duration1
, interval1
, duration2
, interval2
);
3285 req
= p2p_build_presence_req(duration1
, interval1
, duration2
,
3290 p2p
->pending_action_state
= P2P_NO_PENDING_ACTION
;
3291 if (p2p_send_action(p2p
, freq
, go_interface_addr
, own_interface_addr
,
3293 wpabuf_head(req
), wpabuf_len(req
), 200) < 0) {
3294 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
3295 "P2P: Failed to send Action frame");
3303 static struct wpabuf
* p2p_build_presence_resp(u8 status
, const u8
*noa
,
3304 size_t noa_len
, u8 dialog_token
)
3306 struct wpabuf
*resp
;
3309 resp
= wpabuf_alloc(100 + noa_len
);
3313 p2p_buf_add_action_hdr(resp
, P2P_PRESENCE_RESP
, dialog_token
);
3314 len
= p2p_buf_add_ie_hdr(resp
);
3315 p2p_buf_add_status(resp
, status
);
3317 wpabuf_put_u8(resp
, P2P_ATTR_NOTICE_OF_ABSENCE
);
3318 wpabuf_put_le16(resp
, noa_len
);
3319 wpabuf_put_data(resp
, noa
, noa_len
);
3321 p2p_buf_add_noa(resp
, 0, 0, 0, NULL
, NULL
);
3322 p2p_buf_update_ie_hdr(resp
, len
);
3328 static void p2p_process_presence_req(struct p2p_data
*p2p
, const u8
*da
,
3329 const u8
*sa
, const u8
*data
, size_t len
,
3332 struct p2p_message msg
;
3334 struct wpabuf
*resp
;
3336 struct p2p_group
*group
= NULL
;
3341 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
3342 "P2P: Received P2P Action - P2P Presence Request");
3344 for (g
= 0; g
< p2p
->num_groups
; g
++) {
3345 if (os_memcmp(da
, p2p_group_get_interface_addr(p2p
->groups
[g
]),
3347 group
= p2p
->groups
[g
];
3351 if (group
== NULL
) {
3352 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
3353 "P2P: Ignore P2P Presence Request for unknown group "
3354 MACSTR
, MAC2STR(da
));
3358 if (p2p_parse(data
, len
, &msg
) < 0) {
3359 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
3360 "P2P: Failed to parse P2P Presence Request");
3361 status
= P2P_SC_FAIL_INVALID_PARAMS
;
3366 if (msg
.noa
== NULL
) {
3367 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
3368 "P2P: No NoA attribute in P2P Presence Request");
3369 status
= P2P_SC_FAIL_INVALID_PARAMS
;
3373 status
= p2p_group_presence_req(group
, sa
, msg
.noa
, msg
.noa_len
);
3376 if (p2p
->cfg
->get_noa
)
3377 noa_len
= p2p
->cfg
->get_noa(p2p
->cfg
->cb_ctx
, da
, noa
,
3381 resp
= p2p_build_presence_resp(status
, noa_len
> 0 ? noa
: NULL
,
3382 noa_len
> 0 ? noa_len
: 0,
3385 p2p_parse_free(&msg
);
3389 p2p
->pending_action_state
= P2P_NO_PENDING_ACTION
;
3390 if (p2p_send_action(p2p
, rx_freq
, sa
, da
, da
,
3391 wpabuf_head(resp
), wpabuf_len(resp
), 200) < 0) {
3392 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
3393 "P2P: Failed to send Action frame");
3399 static void p2p_process_presence_resp(struct p2p_data
*p2p
, const u8
*da
,
3400 const u8
*sa
, const u8
*data
, size_t len
)
3402 struct p2p_message msg
;
3404 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
3405 "P2P: Received P2P Action - P2P Presence Response");
3407 if (p2p_parse(data
, len
, &msg
) < 0) {
3408 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
3409 "P2P: Failed to parse P2P Presence Response");
3413 if (msg
.status
== NULL
|| msg
.noa
== NULL
) {
3414 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
3415 "P2P: No Status or NoA attribute in P2P Presence "
3417 p2p_parse_free(&msg
);
3422 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
3423 "P2P: P2P Presence Request was rejected: status %u",
3425 p2p_parse_free(&msg
);
3429 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
3430 "P2P: P2P Presence Request was accepted");
3431 wpa_hexdump(MSG_DEBUG
, "P2P: P2P Presence Response - NoA",
3432 msg
.noa
, msg
.noa_len
);
3433 /* TODO: process NoA */
3434 p2p_parse_free(&msg
);
3438 static void p2p_ext_listen_timeout(void *eloop_ctx
, void *timeout_ctx
)
3440 struct p2p_data
*p2p
= eloop_ctx
;
3442 if (p2p
->ext_listen_interval
) {
3443 /* Schedule next extended listen timeout */
3444 eloop_register_timeout(p2p
->ext_listen_interval_sec
,
3445 p2p
->ext_listen_interval_usec
,
3446 p2p_ext_listen_timeout
, p2p
, NULL
);
3449 if (p2p
->state
== P2P_LISTEN_ONLY
&& p2p
->ext_listen_only
) {
3451 * This should not really happen, but it looks like the Listen
3452 * command may fail is something else (e.g., a scan) was
3453 * running at an inconvenient time. As a workaround, allow new
3454 * Extended Listen operation to be started.
3456 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
, "P2P: Previous "
3457 "Extended Listen operation had not been completed - "
3459 p2p
->ext_listen_only
= 0;
3460 p2p_set_state(p2p
, P2P_IDLE
);
3463 if (p2p
->state
!= P2P_IDLE
) {
3464 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
, "P2P: Skip Extended "
3465 "Listen timeout in active state (%s)",
3466 p2p_state_txt(p2p
->state
));
3470 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
, "P2P: Extended Listen timeout");
3471 p2p
->ext_listen_only
= 1;
3472 if (p2p_listen(p2p
, p2p
->ext_listen_period
) < 0) {
3473 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
, "P2P: Failed to start "
3474 "Listen state for Extended Listen Timing");
3475 p2p
->ext_listen_only
= 0;
3480 int p2p_ext_listen(struct p2p_data
*p2p
, unsigned int period
,
3481 unsigned int interval
)
3483 if (period
> 65535 || interval
> 65535 || period
> interval
||
3484 (period
== 0 && interval
> 0) || (period
> 0 && interval
== 0)) {
3485 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
3486 "P2P: Invalid Extended Listen Timing request: "
3487 "period=%u interval=%u", period
, interval
);
3491 eloop_cancel_timeout(p2p_ext_listen_timeout
, p2p
, NULL
);
3493 if (interval
== 0) {
3494 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
3495 "P2P: Disabling Extended Listen Timing");
3496 p2p
->ext_listen_period
= 0;
3497 p2p
->ext_listen_interval
= 0;
3501 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
3502 "P2P: Enabling Extended Listen Timing: period %u msec, "
3503 "interval %u msec", period
, interval
);
3504 p2p
->ext_listen_period
= period
;
3505 p2p
->ext_listen_interval
= interval
;
3506 p2p
->ext_listen_interval_sec
= interval
/ 1000;
3507 p2p
->ext_listen_interval_usec
= (interval
% 1000) * 1000;
3509 eloop_register_timeout(p2p
->ext_listen_interval_sec
,
3510 p2p
->ext_listen_interval_usec
,
3511 p2p_ext_listen_timeout
, p2p
, NULL
);
3517 void p2p_deauth_notif(struct p2p_data
*p2p
, const u8
*bssid
, u16 reason_code
,
3518 const u8
*ie
, size_t ie_len
)
3520 struct p2p_message msg
;
3522 if (bssid
== NULL
|| ie
== NULL
)
3525 os_memset(&msg
, 0, sizeof(msg
));
3526 if (p2p_parse_ies(ie
, ie_len
, &msg
))
3528 if (msg
.minor_reason_code
== NULL
)
3531 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_INFO
,
3532 "P2P: Deauthentication notification BSSID " MACSTR
3533 " reason_code=%u minor_reason_code=%u",
3534 MAC2STR(bssid
), reason_code
, *msg
.minor_reason_code
);
3536 p2p_parse_free(&msg
);
3540 void p2p_disassoc_notif(struct p2p_data
*p2p
, const u8
*bssid
, u16 reason_code
,
3541 const u8
*ie
, size_t ie_len
)
3543 struct p2p_message msg
;
3545 if (bssid
== NULL
|| ie
== NULL
)
3548 os_memset(&msg
, 0, sizeof(msg
));
3549 if (p2p_parse_ies(ie
, ie_len
, &msg
))
3551 if (msg
.minor_reason_code
== NULL
)
3554 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_INFO
,
3555 "P2P: Disassociation notification BSSID " MACSTR
3556 " reason_code=%u minor_reason_code=%u",
3557 MAC2STR(bssid
), reason_code
, *msg
.minor_reason_code
);
3559 p2p_parse_free(&msg
);
3563 void p2p_set_managed_oper(struct p2p_data
*p2p
, int enabled
)
3566 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
, "P2P: Managed P2P "
3567 "Device operations enabled");
3568 p2p
->dev_capab
|= P2P_DEV_CAPAB_INFRA_MANAGED
;
3570 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
, "P2P: Managed P2P "
3571 "Device operations disabled");
3572 p2p
->dev_capab
&= ~P2P_DEV_CAPAB_INFRA_MANAGED
;
3577 int p2p_set_listen_channel(struct p2p_data
*p2p
, u8 reg_class
, u8 channel
)
3579 if (p2p_channel_to_freq(p2p
->cfg
->country
, reg_class
, channel
) < 0)
3582 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
, "P2P: Set Listen channel: "
3583 "reg_class %u channel %u", reg_class
, channel
);
3584 p2p
->cfg
->reg_class
= reg_class
;
3585 p2p
->cfg
->channel
= channel
;
3591 int p2p_set_ssid_postfix(struct p2p_data
*p2p
, const u8
*postfix
, size_t len
)
3593 wpa_hexdump_ascii(MSG_DEBUG
, "P2P: New SSID postfix", postfix
, len
);
3594 if (postfix
== NULL
) {
3595 p2p
->cfg
->ssid_postfix_len
= 0;
3598 if (len
> sizeof(p2p
->cfg
->ssid_postfix
))
3600 os_memcpy(p2p
->cfg
->ssid_postfix
, postfix
, len
);
3601 p2p
->cfg
->ssid_postfix_len
= len
;
3606 int p2p_set_oper_channel(struct p2p_data
*p2p
, u8 op_reg_class
, u8 op_channel
,
3609 if (p2p_channel_to_freq(p2p
->cfg
->country
, op_reg_class
, op_channel
)
3613 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_INFO
, "P2P: Set Operating channel: "
3614 "reg_class %u channel %u", op_reg_class
, op_channel
);
3615 p2p
->cfg
->op_reg_class
= op_reg_class
;
3616 p2p
->cfg
->op_channel
= op_channel
;
3617 p2p
->cfg
->cfg_op_channel
= cfg_op_channel
;
3622 int p2p_get_interface_addr(struct p2p_data
*p2p
, const u8
*dev_addr
,
3625 struct p2p_device
*dev
= p2p_get_device(p2p
, dev_addr
);
3626 if (dev
== NULL
|| is_zero_ether_addr(dev
->interface_addr
))
3628 os_memcpy(iface_addr
, dev
->interface_addr
, ETH_ALEN
);
3633 int p2p_get_dev_addr(struct p2p_data
*p2p
, const u8
*iface_addr
,
3636 struct p2p_device
*dev
= p2p_get_device_interface(p2p
, iface_addr
);
3639 os_memcpy(dev_addr
, dev
->info
.p2p_device_addr
, ETH_ALEN
);
3644 void p2p_set_peer_filter(struct p2p_data
*p2p
, const u8
*addr
)
3646 os_memcpy(p2p
->peer_filter
, addr
, ETH_ALEN
);
3647 if (is_zero_ether_addr(p2p
->peer_filter
))
3648 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
, "P2P: Disable peer "
3651 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
, "P2P: Enable peer "
3652 "filter for " MACSTR
, MAC2STR(p2p
->peer_filter
));
3656 void p2p_set_cross_connect(struct p2p_data
*p2p
, int enabled
)
3658 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
, "P2P: Cross connection %s",
3659 enabled
? "enabled" : "disabled");
3660 if (p2p
->cross_connect
== enabled
)
3662 p2p
->cross_connect
= enabled
;
3663 /* TODO: may need to tear down any action group where we are GO(?) */
3667 int p2p_get_oper_freq(struct p2p_data
*p2p
, const u8
*iface_addr
)
3669 struct p2p_device
*dev
= p2p_get_device_interface(p2p
, iface_addr
);
3672 if (dev
->oper_freq
<= 0)
3674 return dev
->oper_freq
;
3678 void p2p_set_intra_bss_dist(struct p2p_data
*p2p
, int enabled
)
3680 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
, "P2P: Intra BSS distribution %s",
3681 enabled
? "enabled" : "disabled");
3682 p2p
->cfg
->p2p_intra_bss
= enabled
;
3686 void p2p_update_channel_list(struct p2p_data
*p2p
, struct p2p_channels
*chan
)
3688 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
, "P2P: Update channel list");
3689 os_memcpy(&p2p
->cfg
->channels
, chan
, sizeof(struct p2p_channels
));
3693 int p2p_send_action(struct p2p_data
*p2p
, unsigned int freq
, const u8
*dst
,
3694 const u8
*src
, const u8
*bssid
, const u8
*buf
,
3695 size_t len
, unsigned int wait_time
)
3697 if (p2p
->p2p_scan_running
) {
3698 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
, "P2P: Delay Action "
3699 "frame TX until p2p_scan completes");
3700 if (p2p
->after_scan_tx
) {
3701 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
, "P2P: Dropped "
3702 "previous pending Action frame TX");
3703 os_free(p2p
->after_scan_tx
);
3705 p2p
->after_scan_tx
= os_malloc(sizeof(*p2p
->after_scan_tx
) +
3707 if (p2p
->after_scan_tx
== NULL
)
3709 p2p
->after_scan_tx
->freq
= freq
;
3710 os_memcpy(p2p
->after_scan_tx
->dst
, dst
, ETH_ALEN
);
3711 os_memcpy(p2p
->after_scan_tx
->src
, src
, ETH_ALEN
);
3712 os_memcpy(p2p
->after_scan_tx
->bssid
, bssid
, ETH_ALEN
);
3713 p2p
->after_scan_tx
->len
= len
;
3714 p2p
->after_scan_tx
->wait_time
= wait_time
;
3715 os_memcpy(p2p
->after_scan_tx
+ 1, buf
, len
);
3719 return p2p
->cfg
->send_action(p2p
->cfg
->cb_ctx
, freq
, dst
, src
, bssid
,
3720 buf
, len
, wait_time
);
3724 void p2p_set_best_channels(struct p2p_data
*p2p
, int freq_24
, int freq_5
,
3727 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
, "P2P: Best channel: 2.4 GHz: %d,"
3728 " 5 GHz: %d, overall: %d", freq_24
, freq_5
, freq_overall
);
3729 p2p
->best_freq_24
= freq_24
;
3730 p2p
->best_freq_5
= freq_5
;
3731 p2p
->best_freq_overall
= freq_overall
;
3735 const u8
* p2p_get_go_neg_peer(struct p2p_data
*p2p
)
3737 if (p2p
== NULL
|| p2p
->go_neg_peer
== NULL
)
3739 return p2p
->go_neg_peer
->info
.p2p_device_addr
;
3743 const struct p2p_peer_info
*
3744 p2p_get_peer_found(struct p2p_data
*p2p
, const u8
*addr
, int next
)
3746 struct p2p_device
*dev
;
3749 dev
= p2p_get_device(p2p
, addr
);
3754 if (dev
->flags
& P2P_DEV_PROBE_REQ_ONLY
)
3760 dev
= dl_list_first(&dev
->list
,
3763 if (&dev
->list
== &p2p
->devices
)
3765 } while (dev
->flags
& P2P_DEV_PROBE_REQ_ONLY
);
3768 dev
= dl_list_first(&p2p
->devices
, struct p2p_device
, list
);
3771 while (dev
->flags
& P2P_DEV_PROBE_REQ_ONLY
) {
3772 dev
= dl_list_first(&dev
->list
,
3775 if (&dev
->list
== &p2p
->devices
)
3784 int p2p_in_progress(struct p2p_data
*p2p
)
3788 return p2p
->state
!= P2P_IDLE
&& p2p
->state
!= P2P_PROVISIONING
;