2 * Wi-Fi Direct - P2P module
3 * Copyright (c) 2009-2010, Atheros Communications
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
13 #include "common/ieee802_11_defs.h"
14 #include "common/ieee802_11_common.h"
15 #include "common/wpa_ctrl.h"
16 #include "wps/wps_i.h"
21 static void p2p_state_timeout(void *eloop_ctx
, void *timeout_ctx
);
22 static void p2p_device_free(struct p2p_data
*p2p
, struct p2p_device
*dev
);
23 static void p2p_process_presence_req(struct p2p_data
*p2p
, const u8
*da
,
24 const u8
*sa
, const u8
*data
, size_t len
,
26 static void p2p_process_presence_resp(struct p2p_data
*p2p
, const u8
*da
,
27 const u8
*sa
, const u8
*data
,
29 static void p2p_ext_listen_timeout(void *eloop_ctx
, void *timeout_ctx
);
30 static void p2p_scan_timeout(void *eloop_ctx
, void *timeout_ctx
);
34 * p2p_scan recovery timeout
36 * Many drivers are using 30 second timeout on scan results. Allow a bit larger
37 * timeout for this to avoid hitting P2P timeout unnecessarily.
39 #define P2P_SCAN_TIMEOUT 35
42 * P2P_PEER_EXPIRATION_AGE - Number of seconds after which inactive peer
43 * entries will be removed
45 #define P2P_PEER_EXPIRATION_AGE 300
47 #define P2P_PEER_EXPIRATION_INTERVAL (P2P_PEER_EXPIRATION_AGE / 2)
49 static void p2p_expire_peers(struct p2p_data
*p2p
)
51 struct p2p_device
*dev
, *n
;
56 dl_list_for_each_safe(dev
, n
, &p2p
->devices
, struct p2p_device
, list
) {
57 if (dev
->last_seen
.sec
+ P2P_PEER_EXPIRATION_AGE
>= now
.sec
)
60 if (p2p
->cfg
->go_connected
&&
61 p2p
->cfg
->go_connected(p2p
->cfg
->cb_ctx
,
62 dev
->info
.p2p_device_addr
)) {
64 * We are connected as a client to a group in which the
65 * peer is the GO, so do not expire the peer entry.
67 os_get_time(&dev
->last_seen
);
71 for (i
= 0; i
< p2p
->num_groups
; i
++) {
72 if (p2p_group_is_client_connected(
73 p2p
->groups
[i
], dev
->info
.p2p_device_addr
))
76 if (i
< p2p
->num_groups
) {
78 * The peer is connected as a client in a group where
79 * we are the GO, so do not expire the peer entry.
81 os_get_time(&dev
->last_seen
);
85 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
, "P2P: Expiring old peer "
86 "entry " MACSTR
, MAC2STR(dev
->info
.p2p_device_addr
));
87 dl_list_del(&dev
->list
);
88 p2p_device_free(p2p
, dev
);
93 static void p2p_expiration_timeout(void *eloop_ctx
, void *timeout_ctx
)
95 struct p2p_data
*p2p
= eloop_ctx
;
96 p2p_expire_peers(p2p
);
97 eloop_register_timeout(P2P_PEER_EXPIRATION_INTERVAL
, 0,
98 p2p_expiration_timeout
, p2p
, NULL
);
102 static const char * p2p_state_txt(int state
)
111 case P2P_CONNECT_LISTEN
:
112 return "CONNECT_LISTEN";
115 case P2P_LISTEN_ONLY
:
116 return "LISTEN_ONLY";
117 case P2P_WAIT_PEER_CONNECT
:
118 return "WAIT_PEER_CONNECT";
119 case P2P_WAIT_PEER_IDLE
:
120 return "WAIT_PEER_IDLE";
121 case P2P_SD_DURING_FIND
:
122 return "SD_DURING_FIND";
123 case P2P_PROVISIONING
:
124 return "PROVISIONING";
125 case P2P_PD_DURING_FIND
:
126 return "PD_DURING_FIND";
129 case P2P_INVITE_LISTEN
:
130 return "INVITE_LISTEN";
131 case P2P_SEARCH_WHEN_READY
:
132 return "SEARCH_WHEN_READY";
133 case P2P_CONTINUE_SEARCH_WHEN_READY
:
134 return "CONTINUE_SEARCH_WHEN_READY";
141 u16
p2p_get_provisioning_info(struct p2p_data
*p2p
, const u8
*addr
)
143 struct p2p_device
*dev
= NULL
;
148 dev
= p2p_get_device(p2p
, addr
);
150 return dev
->wps_prov_info
;
156 void p2p_clear_provisioning_info(struct p2p_data
*p2p
, const u8
*addr
)
158 struct p2p_device
*dev
= NULL
;
163 dev
= p2p_get_device(p2p
, addr
);
165 dev
->wps_prov_info
= 0;
169 void p2p_set_state(struct p2p_data
*p2p
, int new_state
)
171 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
, "P2P: State %s -> %s",
172 p2p_state_txt(p2p
->state
), p2p_state_txt(new_state
));
173 p2p
->state
= new_state
;
177 void p2p_set_timeout(struct p2p_data
*p2p
, unsigned int sec
, unsigned int usec
)
179 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
180 "P2P: Set timeout (state=%s): %u.%06u sec",
181 p2p_state_txt(p2p
->state
), sec
, usec
);
182 eloop_cancel_timeout(p2p_state_timeout
, p2p
, NULL
);
183 eloop_register_timeout(sec
, usec
, p2p_state_timeout
, p2p
, NULL
);
187 void p2p_clear_timeout(struct p2p_data
*p2p
)
189 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
, "P2P: Clear timeout (state=%s)",
190 p2p_state_txt(p2p
->state
));
191 eloop_cancel_timeout(p2p_state_timeout
, p2p
, NULL
);
195 void p2p_go_neg_failed(struct p2p_data
*p2p
, struct p2p_device
*peer
,
198 struct p2p_go_neg_results res
;
199 p2p_clear_timeout(p2p
);
200 p2p_set_state(p2p
, P2P_IDLE
);
201 if (p2p
->go_neg_peer
)
202 p2p
->go_neg_peer
->wps_method
= WPS_NOT_READY
;
203 p2p
->go_neg_peer
= NULL
;
205 os_memset(&res
, 0, sizeof(res
));
208 os_memcpy(res
.peer_device_addr
, peer
->info
.p2p_device_addr
,
210 os_memcpy(res
.peer_interface_addr
, peer
->intended_addr
,
213 p2p
->cfg
->go_neg_completed(p2p
->cfg
->cb_ctx
, &res
);
217 static void p2p_listen_in_find(struct p2p_data
*p2p
, int dev_disc
)
223 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
224 "P2P: Starting short listen state (state=%s)",
225 p2p_state_txt(p2p
->state
));
227 freq
= p2p_channel_to_freq(p2p
->cfg
->country
, p2p
->cfg
->reg_class
,
230 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
231 "P2P: Unknown regulatory class/channel");
235 os_get_random((u8
*) &r
, sizeof(r
));
236 tu
= (r
% ((p2p
->max_disc_int
- p2p
->min_disc_int
) + 1) +
237 p2p
->min_disc_int
) * 100;
238 if (p2p
->max_disc_tu
>= 0 && tu
> (unsigned int) p2p
->max_disc_tu
)
239 tu
= p2p
->max_disc_tu
;
240 if (!dev_disc
&& tu
< 100)
241 tu
= 100; /* Need to wait in non-device discovery use cases */
242 if (p2p
->cfg
->max_listen
&& 1024 * tu
/ 1000 > p2p
->cfg
->max_listen
)
243 tu
= p2p
->cfg
->max_listen
* 1000 / 1024;
246 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
, "P2P: Skip listen state "
247 "since duration was 0 TU");
248 p2p_set_timeout(p2p
, 0, 0);
252 p2p
->pending_listen_freq
= freq
;
253 p2p
->pending_listen_sec
= 0;
254 p2p
->pending_listen_usec
= 1024 * tu
;
256 ies
= p2p_build_probe_resp_ies(p2p
);
260 if (p2p
->cfg
->start_listen(p2p
->cfg
->cb_ctx
, freq
, 1024 * tu
/ 1000,
262 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
263 "P2P: Failed to start listen mode");
264 p2p
->pending_listen_freq
= 0;
270 int p2p_listen(struct p2p_data
*p2p
, unsigned int timeout
)
275 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
276 "P2P: Going to listen(only) state");
278 freq
= p2p_channel_to_freq(p2p
->cfg
->country
, p2p
->cfg
->reg_class
,
281 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
282 "P2P: Unknown regulatory class/channel");
286 p2p
->pending_listen_freq
= freq
;
287 p2p
->pending_listen_sec
= timeout
/ 1000;
288 p2p
->pending_listen_usec
= (timeout
% 1000) * 1000;
290 if (p2p
->p2p_scan_running
) {
291 if (p2p
->start_after_scan
== P2P_AFTER_SCAN_CONNECT
) {
292 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
293 "P2P: p2p_scan running - connect is already "
294 "pending - skip listen");
297 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
298 "P2P: p2p_scan running - delay start of listen state");
299 p2p
->start_after_scan
= P2P_AFTER_SCAN_LISTEN
;
303 ies
= p2p_build_probe_resp_ies(p2p
);
307 if (p2p
->cfg
->start_listen(p2p
->cfg
->cb_ctx
, freq
, timeout
, ies
) < 0) {
308 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
309 "P2P: Failed to start listen mode");
310 p2p
->pending_listen_freq
= 0;
316 p2p_set_state(p2p
, P2P_LISTEN_ONLY
);
322 static void p2p_device_clear_reported(struct p2p_data
*p2p
)
324 struct p2p_device
*dev
;
325 dl_list_for_each(dev
, &p2p
->devices
, struct p2p_device
, list
)
326 dev
->flags
&= ~P2P_DEV_REPORTED
;
331 * p2p_get_device - Fetch a peer entry
332 * @p2p: P2P module context from p2p_init()
333 * @addr: P2P Device Address of the peer
334 * Returns: Pointer to the device entry or %NULL if not found
336 struct p2p_device
* p2p_get_device(struct p2p_data
*p2p
, const u8
*addr
)
338 struct p2p_device
*dev
;
339 dl_list_for_each(dev
, &p2p
->devices
, struct p2p_device
, list
) {
340 if (os_memcmp(dev
->info
.p2p_device_addr
, addr
, ETH_ALEN
) == 0)
348 * p2p_get_device_interface - Fetch a peer entry based on P2P Interface Address
349 * @p2p: P2P module context from p2p_init()
350 * @addr: P2P Interface Address of the peer
351 * Returns: Pointer to the device entry or %NULL if not found
353 struct p2p_device
* p2p_get_device_interface(struct p2p_data
*p2p
,
356 struct p2p_device
*dev
;
357 dl_list_for_each(dev
, &p2p
->devices
, struct p2p_device
, list
) {
358 if (os_memcmp(dev
->interface_addr
, addr
, ETH_ALEN
) == 0)
366 * p2p_create_device - Create a peer entry
367 * @p2p: P2P module context from p2p_init()
368 * @addr: P2P Device Address of the peer
369 * Returns: Pointer to the device entry or %NULL on failure
371 * If there is already an entry for the peer, it will be returned instead of
372 * creating a new one.
374 static struct p2p_device
* p2p_create_device(struct p2p_data
*p2p
,
377 struct p2p_device
*dev
, *oldest
= NULL
;
380 dev
= p2p_get_device(p2p
, addr
);
384 dl_list_for_each(dev
, &p2p
->devices
, struct p2p_device
, list
) {
386 if (oldest
== NULL
||
387 os_time_before(&dev
->last_seen
, &oldest
->last_seen
))
390 if (count
+ 1 > p2p
->cfg
->max_peers
&& oldest
) {
391 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
392 "P2P: Remove oldest peer entry to make room for a new "
394 dl_list_del(&oldest
->list
);
395 p2p_device_free(p2p
, oldest
);
398 dev
= os_zalloc(sizeof(*dev
));
401 dl_list_add(&p2p
->devices
, &dev
->list
);
402 os_memcpy(dev
->info
.p2p_device_addr
, addr
, ETH_ALEN
);
408 static void p2p_copy_client_info(struct p2p_device
*dev
,
409 struct p2p_client_info
*cli
)
411 os_memcpy(dev
->info
.device_name
, cli
->dev_name
, cli
->dev_name_len
);
412 dev
->info
.device_name
[cli
->dev_name_len
] = '\0';
413 dev
->info
.dev_capab
= cli
->dev_capab
;
414 dev
->info
.config_methods
= cli
->config_methods
;
415 os_memcpy(dev
->info
.pri_dev_type
, cli
->pri_dev_type
, 8);
416 dev
->info
.wps_sec_dev_type_list_len
= 8 * cli
->num_sec_dev_types
;
417 os_memcpy(dev
->info
.wps_sec_dev_type_list
, cli
->sec_dev_types
,
418 dev
->info
.wps_sec_dev_type_list_len
);
422 static int p2p_add_group_clients(struct p2p_data
*p2p
, const u8
*go_dev_addr
,
423 const u8
*go_interface_addr
, int freq
,
424 const u8
*gi
, size_t gi_len
)
426 struct p2p_group_info info
;
428 struct p2p_device
*dev
;
433 if (p2p_group_info_parse(gi
, gi_len
, &info
) < 0)
437 * Clear old data for this group; if the devices are still in the
438 * group, the information will be restored in the loop following this.
440 dl_list_for_each(dev
, &p2p
->devices
, struct p2p_device
, list
) {
441 if (os_memcmp(dev
->member_in_go_iface
, go_interface_addr
,
443 os_memset(dev
->member_in_go_iface
, 0, ETH_ALEN
);
444 os_memset(dev
->member_in_go_dev
, 0, ETH_ALEN
);
448 for (c
= 0; c
< info
.num_clients
; c
++) {
449 struct p2p_client_info
*cli
= &info
.client
[c
];
450 if (os_memcmp(cli
->p2p_device_addr
, p2p
->cfg
->dev_addr
,
452 continue; /* ignore our own entry */
453 dev
= p2p_get_device(p2p
, cli
->p2p_device_addr
);
455 if (dev
->flags
& (P2P_DEV_GROUP_CLIENT_ONLY
|
456 P2P_DEV_PROBE_REQ_ONLY
)) {
458 * Update information since we have not
459 * received this directly from the client.
461 p2p_copy_client_info(dev
, cli
);
464 * Need to update P2P Client Discoverability
465 * flag since it is valid only in P2P Group
468 dev
->info
.dev_capab
&=
469 ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY
;
470 dev
->info
.dev_capab
|=
472 P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY
;
474 if (dev
->flags
& P2P_DEV_PROBE_REQ_ONLY
) {
475 dev
->flags
&= ~P2P_DEV_PROBE_REQ_ONLY
;
478 dev
= p2p_create_device(p2p
, cli
->p2p_device_addr
);
481 dev
->flags
|= P2P_DEV_GROUP_CLIENT_ONLY
;
482 p2p_copy_client_info(dev
, cli
);
483 dev
->oper_freq
= freq
;
484 p2p
->cfg
->dev_found(p2p
->cfg
->cb_ctx
,
485 dev
->info
.p2p_device_addr
,
487 dev
->flags
|= P2P_DEV_REPORTED
| P2P_DEV_REPORTED_ONCE
;
490 os_memcpy(dev
->interface_addr
, cli
->p2p_interface_addr
,
492 os_get_time(&dev
->last_seen
);
493 os_memcpy(dev
->member_in_go_dev
, go_dev_addr
, ETH_ALEN
);
494 os_memcpy(dev
->member_in_go_iface
, go_interface_addr
,
502 static void p2p_copy_wps_info(struct p2p_device
*dev
, int probe_req
,
503 const struct p2p_message
*msg
)
505 os_memcpy(dev
->info
.device_name
, msg
->device_name
,
506 sizeof(dev
->info
.device_name
));
508 if (msg
->manufacturer
&&
509 msg
->manufacturer_len
< sizeof(dev
->info
.manufacturer
)) {
510 os_memset(dev
->info
.manufacturer
, 0,
511 sizeof(dev
->info
.manufacturer
));
512 os_memcpy(dev
->info
.manufacturer
, msg
->manufacturer
,
513 msg
->manufacturer_len
);
516 if (msg
->model_name
&&
517 msg
->model_name_len
< sizeof(dev
->info
.model_name
)) {
518 os_memset(dev
->info
.model_name
, 0,
519 sizeof(dev
->info
.model_name
));
520 os_memcpy(dev
->info
.model_name
, msg
->model_name
,
521 msg
->model_name_len
);
524 if (msg
->model_number
&&
525 msg
->model_number_len
< sizeof(dev
->info
.model_number
)) {
526 os_memset(dev
->info
.model_number
, 0,
527 sizeof(dev
->info
.model_number
));
528 os_memcpy(dev
->info
.model_number
, msg
->model_number
,
529 msg
->model_number_len
);
532 if (msg
->serial_number
&&
533 msg
->serial_number_len
< sizeof(dev
->info
.serial_number
)) {
534 os_memset(dev
->info
.serial_number
, 0,
535 sizeof(dev
->info
.serial_number
));
536 os_memcpy(dev
->info
.serial_number
, msg
->serial_number
,
537 msg
->serial_number_len
);
540 if (msg
->pri_dev_type
)
541 os_memcpy(dev
->info
.pri_dev_type
, msg
->pri_dev_type
,
542 sizeof(dev
->info
.pri_dev_type
));
543 else if (msg
->wps_pri_dev_type
)
544 os_memcpy(dev
->info
.pri_dev_type
, msg
->wps_pri_dev_type
,
545 sizeof(dev
->info
.pri_dev_type
));
547 if (msg
->wps_sec_dev_type_list
) {
548 os_memcpy(dev
->info
.wps_sec_dev_type_list
,
549 msg
->wps_sec_dev_type_list
,
550 msg
->wps_sec_dev_type_list_len
);
551 dev
->info
.wps_sec_dev_type_list_len
=
552 msg
->wps_sec_dev_type_list_len
;
555 if (msg
->capability
) {
557 * P2P Client Discoverability bit is reserved in all frames
558 * that use this function, so do not change its value here.
560 dev
->info
.dev_capab
&= P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY
;
561 dev
->info
.dev_capab
|= msg
->capability
[0] &
562 ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY
;
563 dev
->info
.group_capab
= msg
->capability
[1];
566 if (msg
->ext_listen_timing
) {
567 dev
->ext_listen_period
= WPA_GET_LE16(msg
->ext_listen_timing
);
568 dev
->ext_listen_interval
=
569 WPA_GET_LE16(msg
->ext_listen_timing
+ 2);
573 dev
->info
.config_methods
= msg
->config_methods
?
574 msg
->config_methods
: msg
->wps_config_methods
;
580 * p2p_add_device - Add peer entries based on scan results or P2P frames
581 * @p2p: P2P module context from p2p_init()
582 * @addr: Source address of Beacon or Probe Response frame (may be either
583 * P2P Device Address or P2P Interface Address)
584 * @level: Signal level (signal strength of the received frame from the peer)
585 * @freq: Frequency on which the Beacon or Probe Response frame was received
586 * @age_ms: Age of the information in milliseconds
587 * @ies: IEs from the Beacon or Probe Response frame
588 * @ies_len: Length of ies buffer in octets
589 * @scan_res: Whether this was based on scan results
590 * Returns: 0 on success, -1 on failure
592 * If the scan result is for a GO, the clients in the group will also be added
593 * to the peer table. This function can also be used with some other frames
594 * like Provision Discovery Request that contains P2P Capability and P2P Device
597 int p2p_add_device(struct p2p_data
*p2p
, const u8
*addr
, int freq
,
598 unsigned int age_ms
, int level
, const u8
*ies
,
599 size_t ies_len
, int scan_res
)
601 struct p2p_device
*dev
;
602 struct p2p_message msg
;
603 const u8
*p2p_dev_addr
;
605 struct os_time time_now
, time_tmp_age
, entry_ts
;
607 os_memset(&msg
, 0, sizeof(msg
));
608 if (p2p_parse_ies(ies
, ies_len
, &msg
)) {
609 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
610 "P2P: Failed to parse P2P IE for a device entry");
611 p2p_parse_free(&msg
);
615 if (msg
.p2p_device_addr
)
616 p2p_dev_addr
= msg
.p2p_device_addr
;
617 else if (msg
.device_id
)
618 p2p_dev_addr
= msg
.device_id
;
620 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
621 "P2P: Ignore scan data without P2P Device Info or "
623 p2p_parse_free(&msg
);
627 if (!is_zero_ether_addr(p2p
->peer_filter
) &&
628 os_memcmp(p2p_dev_addr
, p2p
->peer_filter
, ETH_ALEN
) != 0) {
629 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
, "P2P: Do not add peer "
630 "filter for " MACSTR
" due to peer filter",
631 MAC2STR(p2p_dev_addr
));
635 dev
= p2p_create_device(p2p
, p2p_dev_addr
);
637 p2p_parse_free(&msg
);
641 os_get_time(&time_now
);
642 time_tmp_age
.sec
= age_ms
/ 1000;
643 time_tmp_age
.usec
= (age_ms
% 1000) * 1000;
644 os_time_sub(&time_now
, &time_tmp_age
, &entry_ts
);
647 * Update the device entry only if the new peer
648 * entry is newer than the one previously stored.
650 if (dev
->last_seen
.usec
> 0 &&
651 os_time_before(&entry_ts
, &dev
->last_seen
))
654 os_memcpy(&dev
->last_seen
, &entry_ts
, sizeof(struct os_time
));
656 dev
->flags
&= ~(P2P_DEV_PROBE_REQ_ONLY
| P2P_DEV_GROUP_CLIENT_ONLY
);
658 if (os_memcmp(addr
, p2p_dev_addr
, ETH_ALEN
) != 0)
659 os_memcpy(dev
->interface_addr
, addr
, ETH_ALEN
);
661 (msg
.ssid
[1] != P2P_WILDCARD_SSID_LEN
||
662 os_memcmp(msg
.ssid
+ 2, P2P_WILDCARD_SSID
, P2P_WILDCARD_SSID_LEN
)
664 os_memcpy(dev
->oper_ssid
, msg
.ssid
+ 2, msg
.ssid
[1]);
665 dev
->oper_ssid_len
= msg
.ssid
[1];
668 if (freq
>= 2412 && freq
<= 2484 && msg
.ds_params
&&
669 *msg
.ds_params
>= 1 && *msg
.ds_params
<= 14) {
671 if (*msg
.ds_params
== 14)
674 ds_freq
= 2407 + *msg
.ds_params
* 5;
675 if (freq
!= ds_freq
) {
676 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
677 "P2P: Update Listen frequency based on DS "
678 "Parameter Set IE: %d -> %d MHz",
684 if (dev
->listen_freq
&& dev
->listen_freq
!= freq
&& scan_res
) {
685 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
686 "P2P: Update Listen frequency based on scan "
687 "results (" MACSTR
" %d -> %d MHz (DS param %d)",
688 MAC2STR(dev
->info
.p2p_device_addr
), dev
->listen_freq
,
689 freq
, msg
.ds_params
? *msg
.ds_params
: -1);
692 dev
->listen_freq
= freq
;
694 dev
->oper_freq
= freq
;
696 dev
->info
.level
= level
;
698 p2p_copy_wps_info(dev
, 0, &msg
);
700 for (i
= 0; i
< P2P_MAX_WPS_VENDOR_EXT
; i
++) {
701 wpabuf_free(dev
->info
.wps_vendor_ext
[i
]);
702 dev
->info
.wps_vendor_ext
[i
] = NULL
;
705 for (i
= 0; i
< P2P_MAX_WPS_VENDOR_EXT
; i
++) {
706 if (msg
.wps_vendor_ext
[i
] == NULL
)
708 dev
->info
.wps_vendor_ext
[i
] = wpabuf_alloc_copy(
709 msg
.wps_vendor_ext
[i
], msg
.wps_vendor_ext_len
[i
]);
710 if (dev
->info
.wps_vendor_ext
[i
] == NULL
)
714 if (msg
.wfd_subelems
) {
715 wpabuf_free(dev
->info
.wfd_subelems
);
716 dev
->info
.wfd_subelems
= wpabuf_dup(msg
.wfd_subelems
);
720 p2p_add_group_clients(p2p
, p2p_dev_addr
, addr
, freq
,
721 msg
.group_info
, msg
.group_info_len
);
724 p2p_parse_free(&msg
);
726 if (p2p_pending_sd_req(p2p
, dev
))
727 dev
->flags
|= P2P_DEV_SD_SCHEDULE
;
729 if (dev
->flags
& P2P_DEV_REPORTED
)
732 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
733 "P2P: Peer found with Listen frequency %d MHz", freq
);
734 if (dev
->flags
& P2P_DEV_USER_REJECTED
) {
735 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
736 "P2P: Do not report rejected device");
740 p2p
->cfg
->dev_found(p2p
->cfg
->cb_ctx
, addr
, &dev
->info
,
741 !(dev
->flags
& P2P_DEV_REPORTED_ONCE
));
742 dev
->flags
|= P2P_DEV_REPORTED
| P2P_DEV_REPORTED_ONCE
;
748 static void p2p_device_free(struct p2p_data
*p2p
, struct p2p_device
*dev
)
752 if (p2p
->go_neg_peer
== dev
) {
754 * If GO Negotiation is in progress, report that it has failed.
756 p2p_go_neg_failed(p2p
, dev
, -1);
757 p2p
->go_neg_peer
= NULL
;
759 if (p2p
->invite_peer
== dev
)
760 p2p
->invite_peer
= NULL
;
761 if (p2p
->sd_peer
== dev
)
763 if (p2p
->pending_client_disc_go
== dev
)
764 p2p
->pending_client_disc_go
= NULL
;
766 /* dev_lost() device, but only if it was previously dev_found() */
767 if (dev
->flags
& P2P_DEV_REPORTED_ONCE
)
768 p2p
->cfg
->dev_lost(p2p
->cfg
->cb_ctx
,
769 dev
->info
.p2p_device_addr
);
771 for (i
= 0; i
< P2P_MAX_WPS_VENDOR_EXT
; i
++) {
772 wpabuf_free(dev
->info
.wps_vendor_ext
[i
]);
773 dev
->info
.wps_vendor_ext
[i
] = NULL
;
776 wpabuf_free(dev
->info
.wfd_subelems
);
782 static int p2p_get_next_prog_freq(struct p2p_data
*p2p
)
784 struct p2p_channels
*c
;
785 struct p2p_reg_class
*cla
;
792 c
= &p2p
->cfg
->channels
;
793 for (cl
= 0; cl
< c
->reg_classes
; cl
++) {
794 cla
= &c
->reg_class
[cl
];
795 if (cla
->reg_class
!= p2p
->last_prog_scan_class
)
797 for (ch
= 0; ch
< cla
->channels
; ch
++) {
798 if (cla
->channel
[ch
] == p2p
->last_prog_scan_chan
) {
808 /* Start from beginning */
809 reg_class
= c
->reg_class
[0].reg_class
;
810 channel
= c
->reg_class
[0].channel
[0];
812 /* Pick the next channel */
814 if (ch
== cla
->channels
) {
816 if (cl
== c
->reg_classes
)
820 reg_class
= c
->reg_class
[cl
].reg_class
;
821 channel
= c
->reg_class
[cl
].channel
[ch
];
824 freq
= p2p_channel_to_freq(p2p
->cfg
->country
, reg_class
, channel
);
825 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
, "P2P: Next progressive search "
826 "channel: reg_class %u channel %u -> %d MHz",
827 reg_class
, channel
, freq
);
828 p2p
->last_prog_scan_class
= reg_class
;
829 p2p
->last_prog_scan_chan
= channel
;
831 if (freq
== 2412 || freq
== 2437 || freq
== 2462)
832 return 0; /* No need to add social channels */
837 static void p2p_search(struct p2p_data
*p2p
)
840 enum p2p_scan_type type
;
841 u16 pw_id
= DEV_PW_DEFAULT
;
844 if (p2p
->drv_in_listen
) {
845 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
, "P2P: Driver is still "
846 "in Listen state - wait for it to end before "
850 p2p
->cfg
->stop_listen(p2p
->cfg
->cb_ctx
);
852 if (p2p
->find_type
== P2P_FIND_PROGRESSIVE
&&
853 (freq
= p2p_get_next_prog_freq(p2p
)) > 0) {
854 type
= P2P_SCAN_SOCIAL_PLUS_ONE
;
855 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
, "P2P: Starting search "
856 "(+ freq %u)", freq
);
858 type
= P2P_SCAN_SOCIAL
;
859 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
, "P2P: Starting search");
862 res
= p2p
->cfg
->p2p_scan(p2p
->cfg
->cb_ctx
, type
, freq
,
863 p2p
->num_req_dev_types
, p2p
->req_dev_types
,
864 p2p
->find_dev_id
, pw_id
);
866 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
867 "P2P: Scan request failed");
868 p2p_continue_find(p2p
);
869 } else if (res
== 1) {
870 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
, "P2P: Could not start "
871 "p2p_scan at this point - will try again after "
872 "previous scan completes");
873 p2p_set_state(p2p
, P2P_CONTINUE_SEARCH_WHEN_READY
);
875 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
, "P2P: Running p2p_scan");
876 p2p
->p2p_scan_running
= 1;
877 eloop_cancel_timeout(p2p_scan_timeout
, p2p
, NULL
);
878 eloop_register_timeout(P2P_SCAN_TIMEOUT
, 0, p2p_scan_timeout
,
884 static void p2p_find_timeout(void *eloop_ctx
, void *timeout_ctx
)
886 struct p2p_data
*p2p
= eloop_ctx
;
887 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
, "P2P: Find timeout -> stop");
892 static int p2p_run_after_scan(struct p2p_data
*p2p
)
894 struct p2p_device
*dev
;
895 enum p2p_after_scan op
;
897 if (p2p
->after_scan_tx
) {
898 /* TODO: schedule p2p_run_after_scan to be called from TX
899 * status callback(?) */
900 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
, "P2P: Send pending "
901 "Action frame at p2p_scan completion");
902 p2p
->cfg
->send_action(p2p
->cfg
->cb_ctx
,
903 p2p
->after_scan_tx
->freq
,
904 p2p
->after_scan_tx
->dst
,
905 p2p
->after_scan_tx
->src
,
906 p2p
->after_scan_tx
->bssid
,
907 (u8
*) (p2p
->after_scan_tx
+ 1),
908 p2p
->after_scan_tx
->len
,
909 p2p
->after_scan_tx
->wait_time
);
910 os_free(p2p
->after_scan_tx
);
911 p2p
->after_scan_tx
= NULL
;
915 op
= p2p
->start_after_scan
;
916 p2p
->start_after_scan
= P2P_AFTER_SCAN_NOTHING
;
918 case P2P_AFTER_SCAN_NOTHING
:
920 case P2P_AFTER_SCAN_LISTEN
:
921 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
, "P2P: Start previously "
922 "requested Listen state");
923 p2p_listen(p2p
, p2p
->pending_listen_sec
* 1000 +
924 p2p
->pending_listen_usec
/ 1000);
926 case P2P_AFTER_SCAN_CONNECT
:
927 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
, "P2P: Start previously "
928 "requested connect with " MACSTR
,
929 MAC2STR(p2p
->after_scan_peer
));
930 dev
= p2p_get_device(p2p
, p2p
->after_scan_peer
);
932 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
, "P2P: Peer not "
936 p2p_connect_send(p2p
, dev
);
944 static void p2p_scan_timeout(void *eloop_ctx
, void *timeout_ctx
)
946 struct p2p_data
*p2p
= eloop_ctx
;
948 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
, "P2P: p2p_scan timeout "
949 "(running=%d)", p2p
->p2p_scan_running
);
950 running
= p2p
->p2p_scan_running
;
951 /* Make sure we recover from missed scan results callback */
952 p2p
->p2p_scan_running
= 0;
955 p2p_run_after_scan(p2p
);
959 static void p2p_free_req_dev_types(struct p2p_data
*p2p
)
961 p2p
->num_req_dev_types
= 0;
962 os_free(p2p
->req_dev_types
);
963 p2p
->req_dev_types
= NULL
;
967 int p2p_find(struct p2p_data
*p2p
, unsigned int timeout
,
968 enum p2p_discovery_type type
,
969 unsigned int num_req_dev_types
, const u8
*req_dev_types
,
970 const u8
*dev_id
, unsigned int search_delay
)
974 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
, "P2P: Starting find (type=%d)",
976 if (p2p
->p2p_scan_running
) {
977 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
, "P2P: p2p_scan is "
981 p2p_free_req_dev_types(p2p
);
982 if (req_dev_types
&& num_req_dev_types
) {
983 p2p
->req_dev_types
= os_malloc(num_req_dev_types
*
985 if (p2p
->req_dev_types
== NULL
)
987 os_memcpy(p2p
->req_dev_types
, req_dev_types
,
988 num_req_dev_types
* WPS_DEV_TYPE_LEN
);
989 p2p
->num_req_dev_types
= num_req_dev_types
;
993 os_memcpy(p2p
->find_dev_id_buf
, dev_id
, ETH_ALEN
);
994 p2p
->find_dev_id
= p2p
->find_dev_id_buf
;
996 p2p
->find_dev_id
= NULL
;
998 p2p
->start_after_scan
= P2P_AFTER_SCAN_NOTHING
;
999 p2p_clear_timeout(p2p
);
1000 p2p
->cfg
->stop_listen(p2p
->cfg
->cb_ctx
);
1001 p2p
->find_type
= type
;
1002 p2p_device_clear_reported(p2p
);
1003 p2p_set_state(p2p
, P2P_SEARCH
);
1004 p2p
->search_delay
= search_delay
;
1005 p2p
->in_search_delay
= 0;
1006 eloop_cancel_timeout(p2p_find_timeout
, p2p
, NULL
);
1007 p2p
->last_p2p_find_timeout
= timeout
;
1009 eloop_register_timeout(timeout
, 0, p2p_find_timeout
,
1012 case P2P_FIND_START_WITH_FULL
:
1013 case P2P_FIND_PROGRESSIVE
:
1014 res
= p2p
->cfg
->p2p_scan(p2p
->cfg
->cb_ctx
, P2P_SCAN_FULL
, 0,
1015 p2p
->num_req_dev_types
,
1016 p2p
->req_dev_types
, dev_id
,
1019 case P2P_FIND_ONLY_SOCIAL
:
1020 res
= p2p
->cfg
->p2p_scan(p2p
->cfg
->cb_ctx
, P2P_SCAN_SOCIAL
, 0,
1021 p2p
->num_req_dev_types
,
1022 p2p
->req_dev_types
, dev_id
,
1030 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
, "P2P: Running p2p_scan");
1031 p2p
->p2p_scan_running
= 1;
1032 eloop_cancel_timeout(p2p_scan_timeout
, p2p
, NULL
);
1033 eloop_register_timeout(P2P_SCAN_TIMEOUT
, 0, p2p_scan_timeout
,
1035 } else if (res
== 1) {
1036 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
, "P2P: Could not start "
1037 "p2p_scan at this point - will try again after "
1038 "previous scan completes");
1040 p2p_set_state(p2p
, P2P_SEARCH_WHEN_READY
);
1041 eloop_cancel_timeout(p2p_find_timeout
, p2p
, NULL
);
1043 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
, "P2P: Failed to start "
1045 p2p_set_state(p2p
, P2P_IDLE
);
1046 eloop_cancel_timeout(p2p_find_timeout
, p2p
, NULL
);
1053 int p2p_other_scan_completed(struct p2p_data
*p2p
)
1055 if (p2p
->state
== P2P_CONTINUE_SEARCH_WHEN_READY
) {
1056 p2p_set_state(p2p
, P2P_SEARCH
);
1060 if (p2p
->state
!= P2P_SEARCH_WHEN_READY
)
1062 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
, "P2P: Starting pending P2P find "
1063 "now that previous scan was completed");
1064 if (p2p_find(p2p
, p2p
->last_p2p_find_timeout
, p2p
->find_type
,
1065 p2p
->num_req_dev_types
, p2p
->req_dev_types
,
1066 p2p
->find_dev_id
, p2p
->search_delay
) < 0)
1072 void p2p_stop_find_for_freq(struct p2p_data
*p2p
, int freq
)
1074 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
, "P2P: Stopping find");
1075 eloop_cancel_timeout(p2p_find_timeout
, p2p
, NULL
);
1076 p2p_clear_timeout(p2p
);
1077 if (p2p
->state
== P2P_SEARCH
)
1078 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_INFO
, P2P_EVENT_FIND_STOPPED
);
1079 p2p_set_state(p2p
, P2P_IDLE
);
1080 p2p_free_req_dev_types(p2p
);
1081 p2p
->start_after_scan
= P2P_AFTER_SCAN_NOTHING
;
1082 p2p
->go_neg_peer
= NULL
;
1083 p2p
->sd_peer
= NULL
;
1084 p2p
->invite_peer
= NULL
;
1085 p2p_stop_listen_for_freq(p2p
, freq
);
1089 void p2p_stop_listen_for_freq(struct p2p_data
*p2p
, int freq
)
1091 if (freq
> 0 && p2p
->drv_in_listen
== freq
&& p2p
->in_listen
) {
1092 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
, "P2P: Skip stop_listen "
1093 "since we are on correct channel for response");
1096 if (p2p
->in_listen
) {
1098 p2p_clear_timeout(p2p
);
1100 if (p2p
->drv_in_listen
) {
1102 * The driver may not deliver callback to p2p_listen_end()
1103 * when the operation gets canceled, so clear the internal
1104 * variable that is tracking driver state.
1106 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
, "P2P: Clear "
1107 "drv_in_listen (%d)", p2p
->drv_in_listen
);
1108 p2p
->drv_in_listen
= 0;
1110 p2p
->cfg
->stop_listen(p2p
->cfg
->cb_ctx
);
1114 void p2p_stop_find(struct p2p_data
*p2p
)
1116 p2p_stop_find_for_freq(p2p
, 0);
1120 static int p2p_prepare_channel(struct p2p_data
*p2p
, struct p2p_device
*dev
,
1121 unsigned int force_freq
, unsigned int pref_freq
)
1123 if (force_freq
|| pref_freq
) {
1124 u8 op_reg_class
, op_channel
;
1125 unsigned int freq
= force_freq
? force_freq
: pref_freq
;
1126 if (p2p_freq_to_channel(p2p
->cfg
->country
, freq
,
1127 &op_reg_class
, &op_channel
) < 0) {
1128 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
1129 "P2P: Unsupported frequency %u MHz",
1133 if (!p2p_channels_includes(&p2p
->cfg
->channels
, op_reg_class
,
1135 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
1136 "P2P: Frequency %u MHz (oper_class %u "
1137 "channel %u) not allowed for P2P",
1138 freq
, op_reg_class
, op_channel
);
1141 p2p
->op_reg_class
= op_reg_class
;
1142 p2p
->op_channel
= op_channel
;
1144 p2p
->channels
.reg_classes
= 1;
1145 p2p
->channels
.reg_class
[0].channels
= 1;
1146 p2p
->channels
.reg_class
[0].reg_class
=
1148 p2p
->channels
.reg_class
[0].channel
[0] = p2p
->op_channel
;
1150 os_memcpy(&p2p
->channels
, &p2p
->cfg
->channels
,
1151 sizeof(struct p2p_channels
));
1154 u8 op_reg_class
, op_channel
;
1156 if (!p2p
->cfg
->cfg_op_channel
&& p2p
->best_freq_overall
> 0 &&
1157 p2p_supported_freq(p2p
, p2p
->best_freq_overall
) &&
1158 p2p_freq_to_channel(p2p
->cfg
->country
,
1159 p2p
->best_freq_overall
,
1160 &op_reg_class
, &op_channel
) == 0) {
1161 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
1162 "P2P: Select best overall channel as "
1163 "operating channel preference");
1164 p2p
->op_reg_class
= op_reg_class
;
1165 p2p
->op_channel
= op_channel
;
1166 } else if (!p2p
->cfg
->cfg_op_channel
&& p2p
->best_freq_5
> 0 &&
1167 p2p_supported_freq(p2p
, p2p
->best_freq_5
) &&
1168 p2p_freq_to_channel(p2p
->cfg
->country
,
1170 &op_reg_class
, &op_channel
) ==
1172 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
1173 "P2P: Select best 5 GHz channel as "
1174 "operating channel preference");
1175 p2p
->op_reg_class
= op_reg_class
;
1176 p2p
->op_channel
= op_channel
;
1177 } else if (!p2p
->cfg
->cfg_op_channel
&&
1178 p2p
->best_freq_24
> 0 &&
1179 p2p_supported_freq(p2p
, p2p
->best_freq_24
) &&
1180 p2p_freq_to_channel(p2p
->cfg
->country
,
1182 &op_reg_class
, &op_channel
) ==
1184 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
1185 "P2P: Select best 2.4 GHz channel as "
1186 "operating channel preference");
1187 p2p
->op_reg_class
= op_reg_class
;
1188 p2p
->op_channel
= op_channel
;
1190 p2p
->op_reg_class
= p2p
->cfg
->op_reg_class
;
1191 p2p
->op_channel
= p2p
->cfg
->op_channel
;
1194 os_memcpy(&p2p
->channels
, &p2p
->cfg
->channels
,
1195 sizeof(struct p2p_channels
));
1197 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
1198 "P2P: Own preference for operation channel: "
1199 "Operating Class %u Channel %u%s",
1200 p2p
->op_reg_class
, p2p
->op_channel
,
1201 force_freq
? " (forced)" : "");
1204 dev
->flags
|= P2P_DEV_FORCE_FREQ
;
1206 dev
->flags
&= ~P2P_DEV_FORCE_FREQ
;
1212 static void p2p_set_dev_persistent(struct p2p_device
*dev
,
1213 int persistent_group
)
1215 switch (persistent_group
) {
1217 dev
->flags
&= ~(P2P_DEV_PREFER_PERSISTENT_GROUP
|
1218 P2P_DEV_PREFER_PERSISTENT_RECONN
);
1221 dev
->flags
|= P2P_DEV_PREFER_PERSISTENT_GROUP
;
1222 dev
->flags
&= ~P2P_DEV_PREFER_PERSISTENT_RECONN
;
1225 dev
->flags
|= P2P_DEV_PREFER_PERSISTENT_GROUP
|
1226 P2P_DEV_PREFER_PERSISTENT_RECONN
;
1232 int p2p_connect(struct p2p_data
*p2p
, const u8
*peer_addr
,
1233 enum p2p_wps_method wps_method
,
1234 int go_intent
, const u8
*own_interface_addr
,
1235 unsigned int force_freq
, int persistent_group
,
1236 const u8
*force_ssid
, size_t force_ssid_len
,
1237 int pd_before_go_neg
, unsigned int pref_freq
)
1239 struct p2p_device
*dev
;
1241 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
1242 "P2P: Request to start group negotiation - peer=" MACSTR
1243 " GO Intent=%d Intended Interface Address=" MACSTR
1244 " wps_method=%d persistent_group=%d pd_before_go_neg=%d",
1245 MAC2STR(peer_addr
), go_intent
, MAC2STR(own_interface_addr
),
1246 wps_method
, persistent_group
, pd_before_go_neg
);
1248 dev
= p2p_get_device(p2p
, peer_addr
);
1249 if (dev
== NULL
|| (dev
->flags
& P2P_DEV_PROBE_REQ_ONLY
)) {
1250 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
1251 "P2P: Cannot connect to unknown P2P Device " MACSTR
,
1252 MAC2STR(peer_addr
));
1256 if (p2p_prepare_channel(p2p
, dev
, force_freq
, pref_freq
) < 0)
1259 if (dev
->flags
& P2P_DEV_GROUP_CLIENT_ONLY
) {
1260 if (!(dev
->info
.dev_capab
&
1261 P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY
)) {
1262 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
1263 "P2P: Cannot connect to P2P Device " MACSTR
1264 " that is in a group and is not discoverable",
1265 MAC2STR(peer_addr
));
1268 if (dev
->oper_freq
<= 0) {
1269 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
1270 "P2P: Cannot connect to P2P Device " MACSTR
1271 " with incomplete information",
1272 MAC2STR(peer_addr
));
1277 * First, try to connect directly. If the peer does not
1278 * acknowledge frames, assume it is sleeping and use device
1279 * discoverability via the GO at that point.
1285 wpa_hexdump_ascii(MSG_DEBUG
, "P2P: Forced SSID",
1286 force_ssid
, force_ssid_len
);
1287 os_memcpy(p2p
->ssid
, force_ssid
, force_ssid_len
);
1288 p2p
->ssid_len
= force_ssid_len
;
1292 dev
->flags
&= ~P2P_DEV_NOT_YET_READY
;
1293 dev
->flags
&= ~P2P_DEV_USER_REJECTED
;
1294 dev
->flags
&= ~P2P_DEV_WAIT_GO_NEG_RESPONSE
;
1295 dev
->flags
&= ~P2P_DEV_WAIT_GO_NEG_CONFIRM
;
1296 if (pd_before_go_neg
)
1297 dev
->flags
|= P2P_DEV_PD_BEFORE_GO_NEG
;
1299 dev
->flags
&= ~P2P_DEV_PD_BEFORE_GO_NEG
;
1300 dev
->connect_reqs
= 0;
1301 dev
->go_neg_req_sent
= 0;
1302 dev
->go_state
= UNKNOWN_GO
;
1303 p2p_set_dev_persistent(dev
, persistent_group
);
1304 p2p
->go_intent
= go_intent
;
1305 os_memcpy(p2p
->intended_addr
, own_interface_addr
, ETH_ALEN
);
1307 if (p2p
->state
!= P2P_IDLE
)
1310 if (p2p
->after_scan_tx
) {
1312 * We need to drop the pending frame to avoid issues with the
1313 * new GO Negotiation, e.g., when the pending frame was from a
1314 * previous attempt at starting a GO Negotiation.
1316 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
, "P2P: Dropped "
1317 "previous pending Action frame TX that was waiting "
1318 "for p2p_scan completion");
1319 os_free(p2p
->after_scan_tx
);
1320 p2p
->after_scan_tx
= NULL
;
1323 dev
->wps_method
= wps_method
;
1324 dev
->status
= P2P_SC_SUCCESS
;
1326 if (p2p
->p2p_scan_running
) {
1327 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
1328 "P2P: p2p_scan running - delay connect send");
1329 p2p
->start_after_scan
= P2P_AFTER_SCAN_CONNECT
;
1330 os_memcpy(p2p
->after_scan_peer
, peer_addr
, ETH_ALEN
);
1333 p2p
->start_after_scan
= P2P_AFTER_SCAN_NOTHING
;
1335 return p2p_connect_send(p2p
, dev
);
1339 int p2p_authorize(struct p2p_data
*p2p
, const u8
*peer_addr
,
1340 enum p2p_wps_method wps_method
,
1341 int go_intent
, const u8
*own_interface_addr
,
1342 unsigned int force_freq
, int persistent_group
,
1343 const u8
*force_ssid
, size_t force_ssid_len
,
1344 unsigned int pref_freq
)
1346 struct p2p_device
*dev
;
1348 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
1349 "P2P: Request to authorize group negotiation - peer=" MACSTR
1350 " GO Intent=%d Intended Interface Address=" MACSTR
1351 " wps_method=%d persistent_group=%d",
1352 MAC2STR(peer_addr
), go_intent
, MAC2STR(own_interface_addr
),
1353 wps_method
, persistent_group
);
1355 dev
= p2p_get_device(p2p
, peer_addr
);
1357 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
1358 "P2P: Cannot authorize unknown P2P Device " MACSTR
,
1359 MAC2STR(peer_addr
));
1363 if (p2p_prepare_channel(p2p
, dev
, force_freq
, pref_freq
) < 0)
1368 wpa_hexdump_ascii(MSG_DEBUG
, "P2P: Forced SSID",
1369 force_ssid
, force_ssid_len
);
1370 os_memcpy(p2p
->ssid
, force_ssid
, force_ssid_len
);
1371 p2p
->ssid_len
= force_ssid_len
;
1375 dev
->flags
&= ~P2P_DEV_NOT_YET_READY
;
1376 dev
->flags
&= ~P2P_DEV_USER_REJECTED
;
1377 dev
->go_neg_req_sent
= 0;
1378 dev
->go_state
= UNKNOWN_GO
;
1379 p2p_set_dev_persistent(dev
, persistent_group
);
1380 p2p
->go_intent
= go_intent
;
1381 os_memcpy(p2p
->intended_addr
, own_interface_addr
, ETH_ALEN
);
1383 dev
->wps_method
= wps_method
;
1384 dev
->status
= P2P_SC_SUCCESS
;
1390 void p2p_add_dev_info(struct p2p_data
*p2p
, const u8
*addr
,
1391 struct p2p_device
*dev
, struct p2p_message
*msg
)
1393 os_get_time(&dev
->last_seen
);
1395 p2p_copy_wps_info(dev
, 0, msg
);
1397 if (msg
->listen_channel
) {
1399 freq
= p2p_channel_to_freq((char *) msg
->listen_channel
,
1400 msg
->listen_channel
[3],
1401 msg
->listen_channel
[4]);
1403 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
1404 "P2P: Unknown peer Listen channel: "
1405 "country=%c%c(0x%02x) reg_class=%u channel=%u",
1406 msg
->listen_channel
[0],
1407 msg
->listen_channel
[1],
1408 msg
->listen_channel
[2],
1409 msg
->listen_channel
[3],
1410 msg
->listen_channel
[4]);
1412 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
, "P2P: Update "
1413 "peer " MACSTR
" Listen channel: %u -> %u MHz",
1414 MAC2STR(dev
->info
.p2p_device_addr
),
1415 dev
->listen_freq
, freq
);
1416 dev
->listen_freq
= freq
;
1420 if (msg
->wfd_subelems
) {
1421 wpabuf_free(dev
->info
.wfd_subelems
);
1422 dev
->info
.wfd_subelems
= wpabuf_dup(msg
->wfd_subelems
);
1425 if (dev
->flags
& P2P_DEV_PROBE_REQ_ONLY
) {
1426 dev
->flags
&= ~P2P_DEV_PROBE_REQ_ONLY
;
1427 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
1428 "P2P: Completed device entry based on data from "
1429 "GO Negotiation Request");
1431 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
1432 "P2P: Created device entry based on GO Neg Req: "
1433 MACSTR
" dev_capab=0x%x group_capab=0x%x name='%s' "
1435 MAC2STR(dev
->info
.p2p_device_addr
),
1436 dev
->info
.dev_capab
, dev
->info
.group_capab
,
1437 dev
->info
.device_name
, dev
->listen_freq
);
1440 dev
->flags
&= ~P2P_DEV_GROUP_CLIENT_ONLY
;
1442 if (dev
->flags
& P2P_DEV_USER_REJECTED
) {
1443 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
1444 "P2P: Do not report rejected device");
1448 p2p
->cfg
->dev_found(p2p
->cfg
->cb_ctx
, addr
, &dev
->info
,
1449 !(dev
->flags
& P2P_DEV_REPORTED_ONCE
));
1450 dev
->flags
|= P2P_DEV_REPORTED
| P2P_DEV_REPORTED_ONCE
;
1454 void p2p_build_ssid(struct p2p_data
*p2p
, u8
*ssid
, size_t *ssid_len
)
1456 os_memcpy(ssid
, P2P_WILDCARD_SSID
, P2P_WILDCARD_SSID_LEN
);
1457 p2p_random((char *) &ssid
[P2P_WILDCARD_SSID_LEN
], 2);
1458 os_memcpy(&ssid
[P2P_WILDCARD_SSID_LEN
+ 2],
1459 p2p
->cfg
->ssid_postfix
, p2p
->cfg
->ssid_postfix_len
);
1460 *ssid_len
= P2P_WILDCARD_SSID_LEN
+ 2 + p2p
->cfg
->ssid_postfix_len
;
1464 int p2p_go_params(struct p2p_data
*p2p
, struct p2p_go_neg_results
*params
)
1466 p2p_build_ssid(p2p
, params
->ssid
, ¶ms
->ssid_len
);
1467 p2p_random(params
->passphrase
, 8);
1472 void p2p_go_complete(struct p2p_data
*p2p
, struct p2p_device
*peer
)
1474 struct p2p_go_neg_results res
;
1475 int go
= peer
->go_state
== LOCAL_GO
;
1476 struct p2p_channels intersection
;
1480 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
1481 "P2P: GO Negotiation with " MACSTR
" completed (%s will be "
1482 "GO)", MAC2STR(peer
->info
.p2p_device_addr
),
1483 go
? "local end" : "peer");
1485 os_memset(&res
, 0, sizeof(res
));
1487 os_memcpy(res
.peer_device_addr
, peer
->info
.p2p_device_addr
, ETH_ALEN
);
1488 os_memcpy(res
.peer_interface_addr
, peer
->intended_addr
, ETH_ALEN
);
1489 res
.wps_method
= peer
->wps_method
;
1490 if (peer
->flags
& P2P_DEV_PREFER_PERSISTENT_GROUP
) {
1491 if (peer
->flags
& P2P_DEV_PREFER_PERSISTENT_RECONN
)
1492 res
.persistent_group
= 2;
1494 res
.persistent_group
= 1;
1498 /* Setup AP mode for WPS provisioning */
1499 res
.freq
= p2p_channel_to_freq(p2p
->cfg
->country
,
1502 os_memcpy(res
.ssid
, p2p
->ssid
, p2p
->ssid_len
);
1503 res
.ssid_len
= p2p
->ssid_len
;
1504 p2p_random(res
.passphrase
, 8);
1506 res
.freq
= peer
->oper_freq
;
1507 if (p2p
->ssid_len
) {
1508 os_memcpy(res
.ssid
, p2p
->ssid
, p2p
->ssid_len
);
1509 res
.ssid_len
= p2p
->ssid_len
;
1513 p2p_channels_intersect(&p2p
->channels
, &peer
->channels
,
1516 for (i
= 0; i
< intersection
.reg_classes
; i
++) {
1517 struct p2p_reg_class
*c
= &intersection
.reg_class
[i
];
1518 if (freqs
+ 1 == P2P_MAX_CHANNELS
)
1520 for (j
= 0; j
< c
->channels
; j
++) {
1522 if (freqs
+ 1 == P2P_MAX_CHANNELS
)
1524 freq
= p2p_channel_to_freq(peer
->country
, c
->reg_class
,
1528 res
.freq_list
[freqs
++] = freq
;
1532 res
.peer_config_timeout
= go
? peer
->client_timeout
: peer
->go_timeout
;
1534 p2p_clear_timeout(p2p
);
1536 peer
->go_neg_req_sent
= 0;
1537 peer
->wps_method
= WPS_NOT_READY
;
1539 p2p_set_state(p2p
, P2P_PROVISIONING
);
1540 p2p
->cfg
->go_neg_completed(p2p
->cfg
->cb_ctx
, &res
);
1544 static void p2p_rx_p2p_action(struct p2p_data
*p2p
, const u8
*sa
,
1545 const u8
*data
, size_t len
, int rx_freq
)
1547 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
1548 "P2P: RX P2P Public Action from " MACSTR
, MAC2STR(sa
));
1549 wpa_hexdump(MSG_MSGDUMP
, "P2P: P2P Public Action contents", data
, len
);
1555 case P2P_GO_NEG_REQ
:
1556 p2p_process_go_neg_req(p2p
, sa
, data
+ 1, len
- 1, rx_freq
);
1558 case P2P_GO_NEG_RESP
:
1559 p2p_process_go_neg_resp(p2p
, sa
, data
+ 1, len
- 1, rx_freq
);
1561 case P2P_GO_NEG_CONF
:
1562 p2p_process_go_neg_conf(p2p
, sa
, data
+ 1, len
- 1);
1564 case P2P_INVITATION_REQ
:
1565 p2p_process_invitation_req(p2p
, sa
, data
+ 1, len
- 1,
1568 case P2P_INVITATION_RESP
:
1569 p2p_process_invitation_resp(p2p
, sa
, data
+ 1, len
- 1);
1571 case P2P_PROV_DISC_REQ
:
1572 p2p_process_prov_disc_req(p2p
, sa
, data
+ 1, len
- 1, rx_freq
);
1574 case P2P_PROV_DISC_RESP
:
1575 p2p_process_prov_disc_resp(p2p
, sa
, data
+ 1, len
- 1);
1577 case P2P_DEV_DISC_REQ
:
1578 p2p_process_dev_disc_req(p2p
, sa
, data
+ 1, len
- 1, rx_freq
);
1580 case P2P_DEV_DISC_RESP
:
1581 p2p_process_dev_disc_resp(p2p
, sa
, data
+ 1, len
- 1);
1584 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
1585 "P2P: Unsupported P2P Public Action frame type %d",
1592 static void p2p_rx_action_public(struct p2p_data
*p2p
, const u8
*da
,
1593 const u8
*sa
, const u8
*bssid
, const u8
*data
,
1594 size_t len
, int freq
)
1600 case WLAN_PA_VENDOR_SPECIFIC
:
1605 if (WPA_GET_BE24(data
) != OUI_WFA
)
1613 if (*data
!= P2P_OUI_TYPE
)
1616 p2p_rx_p2p_action(p2p
, sa
, data
+ 1, len
- 1, freq
);
1618 case WLAN_PA_GAS_INITIAL_REQ
:
1619 p2p_rx_gas_initial_req(p2p
, sa
, data
+ 1, len
- 1, freq
);
1621 case WLAN_PA_GAS_INITIAL_RESP
:
1622 p2p_rx_gas_initial_resp(p2p
, sa
, data
+ 1, len
- 1, freq
);
1624 case WLAN_PA_GAS_COMEBACK_REQ
:
1625 p2p_rx_gas_comeback_req(p2p
, sa
, data
+ 1, len
- 1, freq
);
1627 case WLAN_PA_GAS_COMEBACK_RESP
:
1628 p2p_rx_gas_comeback_resp(p2p
, sa
, data
+ 1, len
- 1, freq
);
1634 void p2p_rx_action(struct p2p_data
*p2p
, const u8
*da
, const u8
*sa
,
1635 const u8
*bssid
, u8 category
,
1636 const u8
*data
, size_t len
, int freq
)
1638 if (category
== WLAN_ACTION_PUBLIC
) {
1639 p2p_rx_action_public(p2p
, da
, sa
, bssid
, data
, len
, freq
);
1643 if (category
!= WLAN_ACTION_VENDOR_SPECIFIC
)
1649 if (WPA_GET_BE24(data
) != OUI_WFA
)
1654 if (*data
!= P2P_OUI_TYPE
)
1659 /* P2P action frame */
1660 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
1661 "P2P: RX P2P Action from " MACSTR
, MAC2STR(sa
));
1662 wpa_hexdump(MSG_MSGDUMP
, "P2P: P2P Action contents", data
, len
);
1668 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
1669 "P2P: Received P2P Action - Notice of Absence");
1672 case P2P_PRESENCE_REQ
:
1673 p2p_process_presence_req(p2p
, da
, sa
, data
+ 1, len
- 1, freq
);
1675 case P2P_PRESENCE_RESP
:
1676 p2p_process_presence_resp(p2p
, da
, sa
, data
+ 1, len
- 1);
1678 case P2P_GO_DISC_REQ
:
1679 p2p_process_go_disc_req(p2p
, da
, sa
, data
+ 1, len
- 1, freq
);
1682 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
1683 "P2P: Received P2P Action - unknown type %u", data
[0]);
1689 static void p2p_go_neg_start(void *eloop_ctx
, void *timeout_ctx
)
1691 struct p2p_data
*p2p
= eloop_ctx
;
1692 if (p2p
->go_neg_peer
== NULL
)
1694 p2p
->cfg
->stop_listen(p2p
->cfg
->cb_ctx
);
1695 p2p
->go_neg_peer
->status
= P2P_SC_SUCCESS
;
1696 p2p_connect_send(p2p
, p2p
->go_neg_peer
);
1700 static void p2p_invite_start(void *eloop_ctx
, void *timeout_ctx
)
1702 struct p2p_data
*p2p
= eloop_ctx
;
1703 if (p2p
->invite_peer
== NULL
)
1705 p2p
->cfg
->stop_listen(p2p
->cfg
->cb_ctx
);
1706 p2p_invite_send(p2p
, p2p
->invite_peer
, p2p
->invite_go_dev_addr
);
1710 static void p2p_add_dev_from_probe_req(struct p2p_data
*p2p
, const u8
*addr
,
1711 const u8
*ie
, size_t ie_len
)
1713 struct p2p_message msg
;
1714 struct p2p_device
*dev
;
1716 os_memset(&msg
, 0, sizeof(msg
));
1717 if (p2p_parse_ies(ie
, ie_len
, &msg
) < 0 || msg
.p2p_attributes
== NULL
)
1719 p2p_parse_free(&msg
);
1720 return; /* not a P2P probe */
1723 if (msg
.ssid
== NULL
|| msg
.ssid
[1] != P2P_WILDCARD_SSID_LEN
||
1724 os_memcmp(msg
.ssid
+ 2, P2P_WILDCARD_SSID
, P2P_WILDCARD_SSID_LEN
)
1726 /* The Probe Request is not part of P2P Device Discovery. It is
1727 * not known whether the source address of the frame is the P2P
1728 * Device Address or P2P Interface Address. Do not add a new
1729 * peer entry based on this frames.
1731 p2p_parse_free(&msg
);
1735 dev
= p2p_get_device(p2p
, addr
);
1737 if (dev
->country
[0] == 0 && msg
.listen_channel
)
1738 os_memcpy(dev
->country
, msg
.listen_channel
, 3);
1739 os_get_time(&dev
->last_seen
);
1740 p2p_parse_free(&msg
);
1741 return; /* already known */
1744 dev
= p2p_create_device(p2p
, addr
);
1746 p2p_parse_free(&msg
);
1750 os_get_time(&dev
->last_seen
);
1751 dev
->flags
|= P2P_DEV_PROBE_REQ_ONLY
;
1753 if (msg
.listen_channel
) {
1754 os_memcpy(dev
->country
, msg
.listen_channel
, 3);
1755 dev
->listen_freq
= p2p_channel_to_freq(dev
->country
,
1756 msg
.listen_channel
[3],
1757 msg
.listen_channel
[4]);
1760 p2p_copy_wps_info(dev
, 1, &msg
);
1762 if (msg
.wfd_subelems
) {
1763 wpabuf_free(dev
->info
.wfd_subelems
);
1764 dev
->info
.wfd_subelems
= wpabuf_dup(msg
.wfd_subelems
);
1767 p2p_parse_free(&msg
);
1769 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
1770 "P2P: Created device entry based on Probe Req: " MACSTR
1771 " dev_capab=0x%x group_capab=0x%x name='%s' listen_freq=%d",
1772 MAC2STR(dev
->info
.p2p_device_addr
), dev
->info
.dev_capab
,
1773 dev
->info
.group_capab
, dev
->info
.device_name
,
1778 struct p2p_device
* p2p_add_dev_from_go_neg_req(struct p2p_data
*p2p
,
1780 struct p2p_message
*msg
)
1782 struct p2p_device
*dev
;
1784 dev
= p2p_get_device(p2p
, addr
);
1786 os_get_time(&dev
->last_seen
);
1787 return dev
; /* already known */
1790 dev
= p2p_create_device(p2p
, addr
);
1794 p2p_add_dev_info(p2p
, addr
, dev
, msg
);
1800 static int dev_type_match(const u8
*dev_type
, const u8
*req_dev_type
)
1802 if (os_memcmp(dev_type
, req_dev_type
, WPS_DEV_TYPE_LEN
) == 0)
1804 if (os_memcmp(dev_type
, req_dev_type
, 2) == 0 &&
1805 WPA_GET_BE32(&req_dev_type
[2]) == 0 &&
1806 WPA_GET_BE16(&req_dev_type
[6]) == 0)
1807 return 1; /* Category match with wildcard OUI/sub-category */
1812 int dev_type_list_match(const u8
*dev_type
, const u8
*req_dev_type
[],
1813 size_t num_req_dev_type
)
1816 for (i
= 0; i
< num_req_dev_type
; i
++) {
1817 if (dev_type_match(dev_type
, req_dev_type
[i
]))
1825 * p2p_match_dev_type - Match local device type with requested type
1826 * @p2p: P2P module context from p2p_init()
1827 * @wps: WPS TLVs from Probe Request frame (concatenated WPS IEs)
1828 * Returns: 1 on match, 0 on mismatch
1830 * This function can be used to match the Requested Device Type attribute in
1831 * WPS IE with the local device types for deciding whether to reply to a Probe
1834 int p2p_match_dev_type(struct p2p_data
*p2p
, struct wpabuf
*wps
)
1836 struct wps_parse_attr attr
;
1839 if (wps_parse_msg(wps
, &attr
))
1840 return 1; /* assume no Requested Device Type attributes */
1842 if (attr
.num_req_dev_type
== 0)
1843 return 1; /* no Requested Device Type attributes -> match */
1845 if (dev_type_list_match(p2p
->cfg
->pri_dev_type
, attr
.req_dev_type
,
1846 attr
.num_req_dev_type
))
1847 return 1; /* Own Primary Device Type matches */
1849 for (i
= 0; i
< p2p
->cfg
->num_sec_dev_types
; i
++)
1850 if (dev_type_list_match(p2p
->cfg
->sec_dev_type
[i
],
1852 attr
.num_req_dev_type
))
1853 return 1; /* Own Secondary Device Type matches */
1855 /* No matching device type found */
1860 struct wpabuf
* p2p_build_probe_resp_ies(struct p2p_data
*p2p
)
1867 #ifdef CONFIG_WIFI_DISPLAY
1868 if (p2p
->wfd_ie_probe_resp
)
1869 extra
= wpabuf_len(p2p
->wfd_ie_probe_resp
);
1870 #endif /* CONFIG_WIFI_DISPLAY */
1872 buf
= wpabuf_alloc(1000 + extra
);
1876 if (p2p
->go_neg_peer
) {
1877 /* Advertise immediate availability of WPS credential */
1878 pw_id
= p2p_wps_method_pw_id(p2p
->go_neg_peer
->wps_method
);
1881 p2p_build_wps_ie(p2p
, buf
, pw_id
, 1);
1883 #ifdef CONFIG_WIFI_DISPLAY
1884 if (p2p
->wfd_ie_probe_resp
)
1885 wpabuf_put_buf(buf
, p2p
->wfd_ie_probe_resp
);
1886 #endif /* CONFIG_WIFI_DISPLAY */
1889 len
= p2p_buf_add_ie_hdr(buf
);
1890 p2p_buf_add_capability(buf
, p2p
->dev_capab
&
1891 ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY
, 0);
1892 if (p2p
->ext_listen_interval
)
1893 p2p_buf_add_ext_listen_timing(buf
, p2p
->ext_listen_period
,
1894 p2p
->ext_listen_interval
);
1895 p2p_buf_add_device_info(buf
, p2p
, NULL
);
1896 p2p_buf_update_ie_hdr(buf
, len
);
1902 static int is_11b(u8 rate
)
1904 return rate
== 0x02 || rate
== 0x04 || rate
== 0x0b || rate
== 0x16;
1908 static int supp_rates_11b_only(struct ieee802_11_elems
*elems
)
1910 int num_11b
= 0, num_others
= 0;
1913 if (elems
->supp_rates
== NULL
&& elems
->ext_supp_rates
== NULL
)
1916 for (i
= 0; elems
->supp_rates
&& i
< elems
->supp_rates_len
; i
++) {
1917 if (is_11b(elems
->supp_rates
[i
]))
1923 for (i
= 0; elems
->ext_supp_rates
&& i
< elems
->ext_supp_rates_len
;
1925 if (is_11b(elems
->ext_supp_rates
[i
]))
1931 return num_11b
> 0 && num_others
== 0;
1935 static enum p2p_probe_req_status
1936 p2p_reply_probe(struct p2p_data
*p2p
, const u8
*addr
, const u8
*dst
,
1937 const u8
*bssid
, const u8
*ie
, size_t ie_len
)
1939 struct ieee802_11_elems elems
;
1941 struct ieee80211_mgmt
*resp
;
1942 struct p2p_message msg
;
1945 if (!p2p
->in_listen
|| !p2p
->drv_in_listen
) {
1946 /* not in Listen state - ignore Probe Request */
1947 return P2P_PREQ_NOT_LISTEN
;
1950 if (ieee802_11_parse_elems((u8
*) ie
, ie_len
, &elems
, 0) ==
1952 /* Ignore invalid Probe Request frames */
1953 return P2P_PREQ_MALFORMED
;
1956 if (elems
.p2p
== NULL
) {
1957 /* not a P2P probe - ignore it */
1958 return P2P_PREQ_NOT_P2P
;
1961 if (dst
&& !is_broadcast_ether_addr(dst
) &&
1962 os_memcmp(dst
, p2p
->cfg
->dev_addr
, ETH_ALEN
) != 0) {
1963 /* Not sent to the broadcast address or our P2P Device Address
1965 return P2P_PREQ_NOT_PROCESSED
;
1968 if (bssid
&& !is_broadcast_ether_addr(bssid
)) {
1969 /* Not sent to the Wildcard BSSID */
1970 return P2P_PREQ_NOT_PROCESSED
;
1973 if (elems
.ssid
== NULL
|| elems
.ssid_len
!= P2P_WILDCARD_SSID_LEN
||
1974 os_memcmp(elems
.ssid
, P2P_WILDCARD_SSID
, P2P_WILDCARD_SSID_LEN
) !=
1976 /* not using P2P Wildcard SSID - ignore */
1977 return P2P_PREQ_NOT_PROCESSED
;
1980 if (supp_rates_11b_only(&elems
)) {
1981 /* Indicates support for 11b rates only */
1982 return P2P_PREQ_NOT_P2P
;
1985 os_memset(&msg
, 0, sizeof(msg
));
1986 if (p2p_parse_ies(ie
, ie_len
, &msg
) < 0) {
1987 /* Could not parse P2P attributes */
1988 return P2P_PREQ_NOT_P2P
;
1991 if (msg
.device_id
&&
1992 os_memcmp(msg
.device_id
, p2p
->cfg
->dev_addr
, ETH_ALEN
) != 0) {
1993 /* Device ID did not match */
1994 p2p_parse_free(&msg
);
1995 return P2P_PREQ_NOT_PROCESSED
;
1998 /* Check Requested Device Type match */
1999 if (msg
.wps_attributes
&&
2000 !p2p_match_dev_type(p2p
, msg
.wps_attributes
)) {
2001 /* No match with Requested Device Type */
2002 p2p_parse_free(&msg
);
2003 return P2P_PREQ_NOT_PROCESSED
;
2005 p2p_parse_free(&msg
);
2007 if (!p2p
->cfg
->send_probe_resp
) {
2008 /* Response generated elsewhere */
2009 return P2P_PREQ_NOT_PROCESSED
;
2012 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
2013 "P2P: Reply to P2P Probe Request in Listen state");
2016 * We do not really have a specific BSS that this frame is advertising,
2017 * so build a frame that has some information in valid format. This is
2018 * really only used for discovery purposes, not to learn exact BSS
2021 ies
= p2p_build_probe_resp_ies(p2p
);
2023 return P2P_PREQ_NOT_PROCESSED
;
2025 buf
= wpabuf_alloc(200 + wpabuf_len(ies
));
2028 return P2P_PREQ_NOT_PROCESSED
;
2032 resp
= wpabuf_put(buf
, resp
->u
.probe_resp
.variable
- (u8
*) resp
);
2034 resp
->frame_control
= host_to_le16((WLAN_FC_TYPE_MGMT
<< 2) |
2035 (WLAN_FC_STYPE_PROBE_RESP
<< 4));
2036 os_memcpy(resp
->da
, addr
, ETH_ALEN
);
2037 os_memcpy(resp
->sa
, p2p
->cfg
->dev_addr
, ETH_ALEN
);
2038 os_memcpy(resp
->bssid
, p2p
->cfg
->dev_addr
, ETH_ALEN
);
2039 resp
->u
.probe_resp
.beacon_int
= host_to_le16(100);
2040 /* hardware or low-level driver will setup seq_ctrl and timestamp */
2041 resp
->u
.probe_resp
.capab_info
=
2042 host_to_le16(WLAN_CAPABILITY_SHORT_PREAMBLE
|
2043 WLAN_CAPABILITY_PRIVACY
|
2044 WLAN_CAPABILITY_SHORT_SLOT_TIME
);
2046 wpabuf_put_u8(buf
, WLAN_EID_SSID
);
2047 wpabuf_put_u8(buf
, P2P_WILDCARD_SSID_LEN
);
2048 wpabuf_put_data(buf
, P2P_WILDCARD_SSID
, P2P_WILDCARD_SSID_LEN
);
2050 wpabuf_put_u8(buf
, WLAN_EID_SUPP_RATES
);
2051 wpabuf_put_u8(buf
, 8);
2052 wpabuf_put_u8(buf
, (60 / 5) | 0x80);
2053 wpabuf_put_u8(buf
, 90 / 5);
2054 wpabuf_put_u8(buf
, (120 / 5) | 0x80);
2055 wpabuf_put_u8(buf
, 180 / 5);
2056 wpabuf_put_u8(buf
, (240 / 5) | 0x80);
2057 wpabuf_put_u8(buf
, 360 / 5);
2058 wpabuf_put_u8(buf
, 480 / 5);
2059 wpabuf_put_u8(buf
, 540 / 5);
2061 wpabuf_put_u8(buf
, WLAN_EID_DS_PARAMS
);
2062 wpabuf_put_u8(buf
, 1);
2063 wpabuf_put_u8(buf
, p2p
->cfg
->channel
);
2065 wpabuf_put_buf(buf
, ies
);
2068 p2p
->cfg
->send_probe_resp(p2p
->cfg
->cb_ctx
, buf
);
2072 return P2P_PREQ_NOT_PROCESSED
;
2076 enum p2p_probe_req_status
2077 p2p_probe_req_rx(struct p2p_data
*p2p
, const u8
*addr
, const u8
*dst
,
2078 const u8
*bssid
, const u8
*ie
, size_t ie_len
)
2080 enum p2p_probe_req_status res
;
2082 p2p_add_dev_from_probe_req(p2p
, addr
, ie
, ie_len
);
2084 res
= p2p_reply_probe(p2p
, addr
, dst
, bssid
, ie
, ie_len
);
2086 if ((p2p
->state
== P2P_CONNECT
|| p2p
->state
== P2P_CONNECT_LISTEN
) &&
2088 os_memcmp(addr
, p2p
->go_neg_peer
->info
.p2p_device_addr
, ETH_ALEN
)
2090 /* Received a Probe Request from GO Negotiation peer */
2091 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
2092 "P2P: Found GO Negotiation peer - try to start GO "
2093 "negotiation from timeout");
2094 eloop_register_timeout(0, 0, p2p_go_neg_start
, p2p
, NULL
);
2095 return P2P_PREQ_PROCESSED
;
2098 if ((p2p
->state
== P2P_INVITE
|| p2p
->state
== P2P_INVITE_LISTEN
) &&
2100 os_memcmp(addr
, p2p
->invite_peer
->info
.p2p_device_addr
, ETH_ALEN
)
2102 /* Received a Probe Request from Invite peer */
2103 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
2104 "P2P: Found Invite peer - try to start Invite from "
2106 eloop_register_timeout(0, 0, p2p_invite_start
, p2p
, NULL
);
2107 return P2P_PREQ_PROCESSED
;
2114 static int p2p_assoc_req_ie_wlan_ap(struct p2p_data
*p2p
, const u8
*bssid
,
2115 u8
*buf
, size_t len
, struct wpabuf
*p2p_ie
)
2124 return 0; /* WLAN AP is not a P2P manager */
2127 * (Re)Association Request - P2P IE
2128 * P2P Capability attribute (shall be present)
2129 * P2P Interface attribute (present if concurrent device and
2130 * P2P Management is enabled)
2132 tmp
= wpabuf_alloc(200);
2136 lpos
= p2p_buf_add_ie_hdr(tmp
);
2138 if (p2p
->num_groups
> 0) {
2139 group_capab
|= P2P_GROUP_CAPAB_GROUP_OWNER
;
2140 if ((p2p
->dev_capab
& P2P_DEV_CAPAB_CONCURRENT_OPER
) &&
2141 (p2p
->dev_capab
& P2P_DEV_CAPAB_INFRA_MANAGED
) &&
2143 group_capab
|= P2P_GROUP_CAPAB_CROSS_CONN
;
2145 p2p_buf_add_capability(tmp
, p2p
->dev_capab
, group_capab
);
2146 if ((p2p
->dev_capab
& P2P_DEV_CAPAB_CONCURRENT_OPER
) &&
2147 (p2p
->dev_capab
& P2P_DEV_CAPAB_INFRA_MANAGED
))
2148 p2p_buf_add_p2p_interface(tmp
, p2p
);
2149 p2p_buf_update_ie_hdr(tmp
, lpos
);
2151 tmplen
= wpabuf_len(tmp
);
2155 os_memcpy(buf
, wpabuf_head(tmp
), tmplen
);
2164 int p2p_assoc_req_ie(struct p2p_data
*p2p
, const u8
*bssid
, u8
*buf
,
2165 size_t len
, int p2p_group
, struct wpabuf
*p2p_ie
)
2169 struct p2p_device
*peer
;
2175 return p2p_assoc_req_ie_wlan_ap(p2p
, bssid
, buf
, len
, p2p_ie
);
2177 #ifdef CONFIG_WIFI_DISPLAY
2178 if (p2p
->wfd_ie_assoc_req
)
2179 extra
= wpabuf_len(p2p
->wfd_ie_assoc_req
);
2180 #endif /* CONFIG_WIFI_DISPLAY */
2183 * (Re)Association Request - P2P IE
2184 * P2P Capability attribute (shall be present)
2185 * Extended Listen Timing (may be present)
2186 * P2P Device Info attribute (shall be present)
2188 tmp
= wpabuf_alloc(200 + extra
);
2192 #ifdef CONFIG_WIFI_DISPLAY
2193 if (p2p
->wfd_ie_assoc_req
)
2194 wpabuf_put_buf(tmp
, p2p
->wfd_ie_assoc_req
);
2195 #endif /* CONFIG_WIFI_DISPLAY */
2197 peer
= bssid
? p2p_get_device(p2p
, bssid
) : NULL
;
2199 lpos
= p2p_buf_add_ie_hdr(tmp
);
2200 p2p_buf_add_capability(tmp
, p2p
->dev_capab
, 0);
2201 if (p2p
->ext_listen_interval
)
2202 p2p_buf_add_ext_listen_timing(tmp
, p2p
->ext_listen_period
,
2203 p2p
->ext_listen_interval
);
2204 p2p_buf_add_device_info(tmp
, p2p
, peer
);
2205 p2p_buf_update_ie_hdr(tmp
, lpos
);
2207 tmplen
= wpabuf_len(tmp
);
2211 os_memcpy(buf
, wpabuf_head(tmp
), tmplen
);
2220 int p2p_scan_result_text(const u8
*ies
, size_t ies_len
, char *buf
, char *end
)
2222 struct wpabuf
*p2p_ie
;
2225 p2p_ie
= ieee802_11_vendor_ie_concat(ies
, ies_len
, P2P_IE_VENDOR_TYPE
);
2229 ret
= p2p_attr_text(p2p_ie
, buf
, end
);
2230 wpabuf_free(p2p_ie
);
2235 int p2p_parse_dev_addr_in_p2p_ie(struct wpabuf
*p2p_ie
, u8
*dev_addr
)
2237 struct p2p_message msg
;
2239 os_memset(&msg
, 0, sizeof(msg
));
2240 if (p2p_parse_p2p_ie(p2p_ie
, &msg
))
2243 if (msg
.p2p_device_addr
) {
2244 os_memcpy(dev_addr
, msg
.p2p_device_addr
, ETH_ALEN
);
2246 } else if (msg
.device_id
) {
2247 os_memcpy(dev_addr
, msg
.device_id
, ETH_ALEN
);
2254 int p2p_parse_dev_addr(const u8
*ies
, size_t ies_len
, u8
*dev_addr
)
2256 struct wpabuf
*p2p_ie
;
2259 p2p_ie
= ieee802_11_vendor_ie_concat(ies
, ies_len
,
2260 P2P_IE_VENDOR_TYPE
);
2263 ret
= p2p_parse_dev_addr_in_p2p_ie(p2p_ie
, dev_addr
);
2264 wpabuf_free(p2p_ie
);
2269 static void p2p_clear_go_neg(struct p2p_data
*p2p
)
2271 p2p
->go_neg_peer
= NULL
;
2272 p2p_clear_timeout(p2p
);
2273 p2p_set_state(p2p
, P2P_IDLE
);
2277 void p2p_wps_success_cb(struct p2p_data
*p2p
, const u8
*mac_addr
)
2279 if (p2p
->go_neg_peer
== NULL
) {
2280 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
2281 "P2P: No pending Group Formation - "
2282 "ignore WPS registration success notification");
2283 return; /* No pending Group Formation */
2286 if (os_memcmp(mac_addr
, p2p
->go_neg_peer
->intended_addr
, ETH_ALEN
) !=
2288 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
2289 "P2P: Ignore WPS registration success notification "
2290 "for " MACSTR
" (GO Negotiation peer " MACSTR
")",
2292 MAC2STR(p2p
->go_neg_peer
->intended_addr
));
2293 return; /* Ignore unexpected peer address */
2296 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
2297 "P2P: Group Formation completed successfully with " MACSTR
,
2300 p2p_clear_go_neg(p2p
);
2304 void p2p_group_formation_failed(struct p2p_data
*p2p
)
2306 if (p2p
->go_neg_peer
== NULL
) {
2307 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
2308 "P2P: No pending Group Formation - "
2309 "ignore group formation failure notification");
2310 return; /* No pending Group Formation */
2313 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
2314 "P2P: Group Formation failed with " MACSTR
,
2315 MAC2STR(p2p
->go_neg_peer
->intended_addr
));
2317 p2p_clear_go_neg(p2p
);
2321 struct p2p_data
* p2p_init(const struct p2p_config
*cfg
)
2323 struct p2p_data
*p2p
;
2325 if (cfg
->max_peers
< 1)
2328 p2p
= os_zalloc(sizeof(*p2p
) + sizeof(*cfg
));
2331 p2p
->cfg
= (struct p2p_config
*) (p2p
+ 1);
2332 os_memcpy(p2p
->cfg
, cfg
, sizeof(*cfg
));
2334 p2p
->cfg
->dev_name
= os_strdup(cfg
->dev_name
);
2335 if (cfg
->manufacturer
)
2336 p2p
->cfg
->manufacturer
= os_strdup(cfg
->manufacturer
);
2337 if (cfg
->model_name
)
2338 p2p
->cfg
->model_name
= os_strdup(cfg
->model_name
);
2339 if (cfg
->model_number
)
2340 p2p
->cfg
->model_number
= os_strdup(cfg
->model_number
);
2341 if (cfg
->serial_number
)
2342 p2p
->cfg
->serial_number
= os_strdup(cfg
->serial_number
);
2343 if (cfg
->pref_chan
) {
2344 p2p
->cfg
->pref_chan
= os_malloc(cfg
->num_pref_chan
*
2345 sizeof(struct p2p_channel
));
2346 if (p2p
->cfg
->pref_chan
) {
2347 os_memcpy(p2p
->cfg
->pref_chan
, cfg
->pref_chan
,
2348 cfg
->num_pref_chan
*
2349 sizeof(struct p2p_channel
));
2351 p2p
->cfg
->num_pref_chan
= 0;
2354 p2p
->min_disc_int
= 1;
2355 p2p
->max_disc_int
= 3;
2356 p2p
->max_disc_tu
= -1;
2358 os_get_random(&p2p
->next_tie_breaker
, 1);
2359 p2p
->next_tie_breaker
&= 0x01;
2360 if (cfg
->sd_request
)
2361 p2p
->dev_capab
|= P2P_DEV_CAPAB_SERVICE_DISCOVERY
;
2362 p2p
->dev_capab
|= P2P_DEV_CAPAB_INVITATION_PROCEDURE
;
2363 if (cfg
->concurrent_operations
)
2364 p2p
->dev_capab
|= P2P_DEV_CAPAB_CONCURRENT_OPER
;
2365 p2p
->dev_capab
|= P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY
;
2367 dl_list_init(&p2p
->devices
);
2369 eloop_register_timeout(P2P_PEER_EXPIRATION_INTERVAL
, 0,
2370 p2p_expiration_timeout
, p2p
, NULL
);
2372 p2p
->go_timeout
= 100;
2373 p2p
->client_timeout
= 20;
2379 void p2p_deinit(struct p2p_data
*p2p
)
2381 #ifdef CONFIG_WIFI_DISPLAY
2382 wpabuf_free(p2p
->wfd_ie_beacon
);
2383 wpabuf_free(p2p
->wfd_ie_probe_req
);
2384 wpabuf_free(p2p
->wfd_ie_probe_resp
);
2385 wpabuf_free(p2p
->wfd_ie_assoc_req
);
2386 wpabuf_free(p2p
->wfd_ie_invitation
);
2387 wpabuf_free(p2p
->wfd_ie_prov_disc_req
);
2388 wpabuf_free(p2p
->wfd_ie_prov_disc_resp
);
2389 wpabuf_free(p2p
->wfd_ie_go_neg
);
2390 wpabuf_free(p2p
->wfd_dev_info
);
2391 wpabuf_free(p2p
->wfd_assoc_bssid
);
2392 wpabuf_free(p2p
->wfd_coupled_sink_info
);
2393 #endif /* CONFIG_WIFI_DISPLAY */
2395 eloop_cancel_timeout(p2p_expiration_timeout
, p2p
, NULL
);
2396 eloop_cancel_timeout(p2p_ext_listen_timeout
, p2p
, NULL
);
2397 eloop_cancel_timeout(p2p_scan_timeout
, p2p
, NULL
);
2399 p2p_free_req_dev_types(p2p
);
2400 os_free(p2p
->cfg
->dev_name
);
2401 os_free(p2p
->cfg
->manufacturer
);
2402 os_free(p2p
->cfg
->model_name
);
2403 os_free(p2p
->cfg
->model_number
);
2404 os_free(p2p
->cfg
->serial_number
);
2405 os_free(p2p
->cfg
->pref_chan
);
2406 os_free(p2p
->groups
);
2407 wpabuf_free(p2p
->sd_resp
);
2408 os_free(p2p
->after_scan_tx
);
2409 p2p_remove_wps_vendor_extensions(p2p
);
2414 void p2p_flush(struct p2p_data
*p2p
)
2416 struct p2p_device
*dev
, *prev
;
2418 dl_list_for_each_safe(dev
, prev
, &p2p
->devices
, struct p2p_device
,
2420 dl_list_del(&dev
->list
);
2421 p2p_device_free(p2p
, dev
);
2423 p2p_free_sd_queries(p2p
);
2424 os_free(p2p
->after_scan_tx
);
2425 p2p
->after_scan_tx
= NULL
;
2429 int p2p_unauthorize(struct p2p_data
*p2p
, const u8
*addr
)
2431 struct p2p_device
*dev
;
2433 dev
= p2p_get_device(p2p
, addr
);
2437 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
, "P2P: Unauthorizing " MACSTR
,
2440 if (p2p
->go_neg_peer
== dev
)
2441 p2p
->go_neg_peer
= NULL
;
2443 dev
->wps_method
= WPS_NOT_READY
;
2444 dev
->flags
&= ~P2P_DEV_WAIT_GO_NEG_RESPONSE
;
2445 dev
->flags
&= ~P2P_DEV_WAIT_GO_NEG_CONFIRM
;
2447 /* Check if after_scan_tx is for this peer. If so free it */
2448 if (p2p
->after_scan_tx
&&
2449 os_memcmp(addr
, p2p
->after_scan_tx
->dst
, ETH_ALEN
) == 0) {
2450 os_free(p2p
->after_scan_tx
);
2451 p2p
->after_scan_tx
= NULL
;
2458 int p2p_set_dev_name(struct p2p_data
*p2p
, const char *dev_name
)
2460 os_free(p2p
->cfg
->dev_name
);
2462 p2p
->cfg
->dev_name
= os_strdup(dev_name
);
2463 if (p2p
->cfg
->dev_name
== NULL
)
2466 p2p
->cfg
->dev_name
= NULL
;
2471 int p2p_set_manufacturer(struct p2p_data
*p2p
, const char *manufacturer
)
2473 os_free(p2p
->cfg
->manufacturer
);
2474 p2p
->cfg
->manufacturer
= NULL
;
2476 p2p
->cfg
->manufacturer
= os_strdup(manufacturer
);
2477 if (p2p
->cfg
->manufacturer
== NULL
)
2485 int p2p_set_model_name(struct p2p_data
*p2p
, const char *model_name
)
2487 os_free(p2p
->cfg
->model_name
);
2488 p2p
->cfg
->model_name
= NULL
;
2490 p2p
->cfg
->model_name
= os_strdup(model_name
);
2491 if (p2p
->cfg
->model_name
== NULL
)
2499 int p2p_set_model_number(struct p2p_data
*p2p
, const char *model_number
)
2501 os_free(p2p
->cfg
->model_number
);
2502 p2p
->cfg
->model_number
= NULL
;
2504 p2p
->cfg
->model_number
= os_strdup(model_number
);
2505 if (p2p
->cfg
->model_number
== NULL
)
2513 int p2p_set_serial_number(struct p2p_data
*p2p
, const char *serial_number
)
2515 os_free(p2p
->cfg
->serial_number
);
2516 p2p
->cfg
->serial_number
= NULL
;
2517 if (serial_number
) {
2518 p2p
->cfg
->serial_number
= os_strdup(serial_number
);
2519 if (p2p
->cfg
->serial_number
== NULL
)
2527 void p2p_set_config_methods(struct p2p_data
*p2p
, u16 config_methods
)
2529 p2p
->cfg
->config_methods
= config_methods
;
2533 void p2p_set_uuid(struct p2p_data
*p2p
, const u8
*uuid
)
2535 os_memcpy(p2p
->cfg
->uuid
, uuid
, 16);
2539 int p2p_set_pri_dev_type(struct p2p_data
*p2p
, const u8
*pri_dev_type
)
2541 os_memcpy(p2p
->cfg
->pri_dev_type
, pri_dev_type
, 8);
2546 int p2p_set_sec_dev_types(struct p2p_data
*p2p
, const u8 dev_types
[][8],
2547 size_t num_dev_types
)
2549 if (num_dev_types
> P2P_SEC_DEVICE_TYPES
)
2550 num_dev_types
= P2P_SEC_DEVICE_TYPES
;
2551 p2p
->cfg
->num_sec_dev_types
= num_dev_types
;
2552 os_memcpy(p2p
->cfg
->sec_dev_type
, dev_types
, num_dev_types
* 8);
2557 void p2p_remove_wps_vendor_extensions(struct p2p_data
*p2p
)
2561 for (i
= 0; i
< P2P_MAX_WPS_VENDOR_EXT
; i
++) {
2562 wpabuf_free(p2p
->wps_vendor_ext
[i
]);
2563 p2p
->wps_vendor_ext
[i
] = NULL
;
2568 int p2p_add_wps_vendor_extension(struct p2p_data
*p2p
,
2569 const struct wpabuf
*vendor_ext
)
2573 if (vendor_ext
== NULL
)
2576 for (i
= 0; i
< P2P_MAX_WPS_VENDOR_EXT
; i
++) {
2577 if (p2p
->wps_vendor_ext
[i
] == NULL
)
2580 if (i
>= P2P_MAX_WPS_VENDOR_EXT
)
2583 p2p
->wps_vendor_ext
[i
] = wpabuf_dup(vendor_ext
);
2584 if (p2p
->wps_vendor_ext
[i
] == NULL
)
2591 int p2p_set_country(struct p2p_data
*p2p
, const char *country
)
2593 os_memcpy(p2p
->cfg
->country
, country
, 3);
2598 void p2p_continue_find(struct p2p_data
*p2p
)
2600 struct p2p_device
*dev
;
2601 p2p_set_state(p2p
, P2P_SEARCH
);
2602 dl_list_for_each(dev
, &p2p
->devices
, struct p2p_device
, list
) {
2603 if (dev
->flags
& P2P_DEV_SD_SCHEDULE
) {
2604 if (p2p_start_sd(p2p
, dev
) == 0)
2608 } else if (dev
->req_config_methods
&&
2609 !(dev
->flags
& P2P_DEV_PD_FOR_JOIN
)) {
2610 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
, "P2P: Send "
2611 "pending Provision Discovery Request to "
2612 MACSTR
" (config methods 0x%x)",
2613 MAC2STR(dev
->info
.p2p_device_addr
),
2614 dev
->req_config_methods
);
2615 if (p2p_send_prov_disc_req(p2p
, dev
, 0, 0) == 0)
2620 p2p_listen_in_find(p2p
, 1);
2624 static void p2p_sd_cb(struct p2p_data
*p2p
, int success
)
2626 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
2627 "P2P: Service Discovery Query TX callback: success=%d",
2629 p2p
->pending_action_state
= P2P_NO_PENDING_ACTION
;
2633 p2p
->sd_peer
->flags
&= ~P2P_DEV_SD_SCHEDULE
;
2634 p2p
->sd_peer
= NULL
;
2636 p2p_continue_find(p2p
);
2640 if (p2p
->sd_peer
== NULL
) {
2641 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
2642 "P2P: No SD peer entry known");
2643 p2p_continue_find(p2p
);
2647 /* Wait for response from the peer */
2648 p2p_set_state(p2p
, P2P_SD_DURING_FIND
);
2649 p2p_set_timeout(p2p
, 0, 200000);
2654 * p2p_retry_pd - Retry any pending provision disc requests in IDLE state
2655 * @p2p: P2P module context from p2p_init()
2657 static void p2p_retry_pd(struct p2p_data
*p2p
)
2659 struct p2p_device
*dev
;
2661 if (p2p
->state
!= P2P_IDLE
)
2665 * Retry the prov disc req attempt only for the peer that the user had
2669 dl_list_for_each(dev
, &p2p
->devices
, struct p2p_device
, list
) {
2670 if (os_memcmp(p2p
->pending_pd_devaddr
,
2671 dev
->info
.p2p_device_addr
, ETH_ALEN
) != 0)
2673 if (!dev
->req_config_methods
)
2676 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
, "P2P: Send "
2677 "pending Provision Discovery Request to "
2678 MACSTR
" (config methods 0x%x)",
2679 MAC2STR(dev
->info
.p2p_device_addr
),
2680 dev
->req_config_methods
);
2681 p2p_send_prov_disc_req(p2p
, dev
,
2682 dev
->flags
& P2P_DEV_PD_FOR_JOIN
, 0);
2688 static void p2p_prov_disc_cb(struct p2p_data
*p2p
, int success
)
2690 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
2691 "P2P: Provision Discovery Request TX callback: success=%d",
2695 * Postpone resetting the pending action state till after we actually
2696 * time out. This allows us to take some action like notifying any
2697 * interested parties about no response to the request.
2699 * When the timer (below) goes off we check in IDLE, SEARCH, or
2700 * LISTEN_ONLY state, which are the only allowed states to issue a PD
2701 * requests in, if this was still pending and then raise notification.
2705 p2p
->pending_action_state
= P2P_NO_PENDING_ACTION
;
2707 if (p2p
->user_initiated_pd
&&
2708 (p2p
->state
== P2P_SEARCH
|| p2p
->state
== P2P_LISTEN_ONLY
))
2710 /* Retry request from timeout to avoid busy loops */
2711 p2p
->pending_action_state
= P2P_PENDING_PD
;
2712 p2p_set_timeout(p2p
, 0, 50000);
2713 } else if (p2p
->state
!= P2P_IDLE
)
2714 p2p_continue_find(p2p
);
2715 else if (p2p
->user_initiated_pd
) {
2716 p2p
->pending_action_state
= P2P_PENDING_PD
;
2717 p2p_set_timeout(p2p
, 0, 300000);
2723 * This postponing, of resetting pending_action_state, needs to be
2724 * done only for user initiated PD requests and not internal ones.
2726 if (p2p
->user_initiated_pd
)
2727 p2p
->pending_action_state
= P2P_PENDING_PD
;
2729 p2p
->pending_action_state
= P2P_NO_PENDING_ACTION
;
2731 /* Wait for response from the peer */
2732 if (p2p
->state
== P2P_SEARCH
)
2733 p2p_set_state(p2p
, P2P_PD_DURING_FIND
);
2734 p2p_set_timeout(p2p
, 0, 200000);
2738 int p2p_scan_res_handler(struct p2p_data
*p2p
, const u8
*bssid
, int freq
,
2739 unsigned int age
, int level
, const u8
*ies
,
2742 p2p_add_device(p2p
, bssid
, freq
, age
, level
, ies
, ies_len
, 1);
2748 void p2p_scan_res_handled(struct p2p_data
*p2p
)
2750 if (!p2p
->p2p_scan_running
) {
2751 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
, "P2P: p2p_scan was not "
2752 "running, but scan results received");
2754 p2p
->p2p_scan_running
= 0;
2755 eloop_cancel_timeout(p2p_scan_timeout
, p2p
, NULL
);
2757 if (p2p_run_after_scan(p2p
))
2759 if (p2p
->state
== P2P_SEARCH
)
2760 p2p_continue_find(p2p
);
2764 void p2p_scan_ie(struct p2p_data
*p2p
, struct wpabuf
*ies
, const u8
*dev_id
)
2768 #ifdef CONFIG_WIFI_DISPLAY
2769 if (p2p
->wfd_ie_probe_req
)
2770 wpabuf_put_buf(ies
, p2p
->wfd_ie_probe_req
);
2771 #endif /* CONFIG_WIFI_DISPLAY */
2773 len
= p2p_buf_add_ie_hdr(ies
);
2774 p2p_buf_add_capability(ies
, p2p
->dev_capab
&
2775 ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY
, 0);
2777 p2p_buf_add_device_id(ies
, dev_id
);
2778 if (p2p
->cfg
->reg_class
&& p2p
->cfg
->channel
)
2779 p2p_buf_add_listen_channel(ies
, p2p
->cfg
->country
,
2780 p2p
->cfg
->reg_class
,
2782 if (p2p
->ext_listen_interval
)
2783 p2p_buf_add_ext_listen_timing(ies
, p2p
->ext_listen_period
,
2784 p2p
->ext_listen_interval
);
2785 /* TODO: p2p_buf_add_operating_channel() if GO */
2786 p2p_buf_update_ie_hdr(ies
, len
);
2790 size_t p2p_scan_ie_buf_len(struct p2p_data
*p2p
)
2794 #ifdef CONFIG_WIFI_DISPLAY
2795 if (p2p
&& p2p
->wfd_ie_probe_req
)
2796 len
+= wpabuf_len(p2p
->wfd_ie_probe_req
);
2797 #endif /* CONFIG_WIFI_DISPLAY */
2803 int p2p_ie_text(struct wpabuf
*p2p_ie
, char *buf
, char *end
)
2805 return p2p_attr_text(p2p_ie
, buf
, end
);
2809 static void p2p_go_neg_req_cb(struct p2p_data
*p2p
, int success
)
2811 struct p2p_device
*dev
= p2p
->go_neg_peer
;
2813 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
2814 "P2P: GO Negotiation Request TX callback: success=%d",
2818 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
2819 "P2P: No pending GO Negotiation");
2824 if (dev
->flags
& P2P_DEV_USER_REJECTED
) {
2825 p2p_set_state(p2p
, P2P_IDLE
);
2828 } else if (dev
->go_neg_req_sent
) {
2829 /* Cancel the increment from p2p_connect_send() on failure */
2830 dev
->go_neg_req_sent
--;
2834 (dev
->info
.dev_capab
& P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY
) &&
2835 !is_zero_ether_addr(dev
->member_in_go_dev
)) {
2836 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
2837 "P2P: Peer " MACSTR
" did not acknowledge request - "
2838 "try to use device discoverability through its GO",
2839 MAC2STR(dev
->info
.p2p_device_addr
));
2840 p2p
->cfg
->send_action_done(p2p
->cfg
->cb_ctx
);
2841 p2p_send_dev_disc_req(p2p
, dev
);
2846 * Use P2P find, if needed, to find the other device from its listen
2849 p2p_set_state(p2p
, P2P_CONNECT
);
2850 p2p_set_timeout(p2p
, 0, success
? 200000 : 100000);
2854 static void p2p_go_neg_resp_cb(struct p2p_data
*p2p
, int success
)
2856 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
2857 "P2P: GO Negotiation Response TX callback: success=%d",
2859 if (!p2p
->go_neg_peer
&& p2p
->state
== P2P_PROVISIONING
) {
2860 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
2861 "P2P: Ignore TX callback event - GO Negotiation is "
2862 "not running anymore");
2865 p2p_set_state(p2p
, P2P_CONNECT
);
2866 p2p_set_timeout(p2p
, 0, 250000);
2870 static void p2p_go_neg_resp_failure_cb(struct p2p_data
*p2p
, int success
)
2872 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
2873 "P2P: GO Negotiation Response (failure) TX callback: "
2874 "success=%d", success
);
2875 if (p2p
->go_neg_peer
&& p2p
->go_neg_peer
->status
!= P2P_SC_SUCCESS
) {
2876 p2p_go_neg_failed(p2p
, p2p
->go_neg_peer
,
2877 p2p
->go_neg_peer
->status
);
2882 static void p2p_go_neg_conf_cb(struct p2p_data
*p2p
,
2883 enum p2p_send_action_result result
)
2885 struct p2p_device
*dev
;
2887 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
2888 "P2P: GO Negotiation Confirm TX callback: result=%d",
2890 p2p
->cfg
->send_action_done(p2p
->cfg
->cb_ctx
);
2891 if (result
== P2P_SEND_ACTION_FAILED
) {
2892 p2p_go_neg_failed(p2p
, p2p
->go_neg_peer
, -1);
2895 if (result
== P2P_SEND_ACTION_NO_ACK
) {
2897 * It looks like the TX status for GO Negotiation Confirm is
2898 * often showing failure even when the peer has actually
2899 * received the frame. Since the peer may change channels
2900 * immediately after having received the frame, we may not see
2901 * an Ack for retries, so just dropping a single frame may
2902 * trigger this. To allow the group formation to succeed if the
2903 * peer did indeed receive the frame, continue regardless of
2906 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
2907 "P2P: Assume GO Negotiation Confirm TX was actually "
2908 "received by the peer even though Ack was not "
2912 dev
= p2p
->go_neg_peer
;
2916 p2p_go_complete(p2p
, dev
);
2920 void p2p_send_action_cb(struct p2p_data
*p2p
, unsigned int freq
, const u8
*dst
,
2921 const u8
*src
, const u8
*bssid
,
2922 enum p2p_send_action_result result
)
2924 enum p2p_pending_action_state state
;
2927 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
2928 "P2P: Action frame TX callback (state=%d freq=%u dst=" MACSTR
2929 " src=" MACSTR
" bssid=" MACSTR
" result=%d",
2930 p2p
->pending_action_state
, freq
, MAC2STR(dst
), MAC2STR(src
),
2931 MAC2STR(bssid
), result
);
2932 success
= result
== P2P_SEND_ACTION_SUCCESS
;
2933 state
= p2p
->pending_action_state
;
2934 p2p
->pending_action_state
= P2P_NO_PENDING_ACTION
;
2936 case P2P_NO_PENDING_ACTION
:
2938 case P2P_PENDING_GO_NEG_REQUEST
:
2939 p2p_go_neg_req_cb(p2p
, success
);
2941 case P2P_PENDING_GO_NEG_RESPONSE
:
2942 p2p_go_neg_resp_cb(p2p
, success
);
2944 case P2P_PENDING_GO_NEG_RESPONSE_FAILURE
:
2945 p2p_go_neg_resp_failure_cb(p2p
, success
);
2947 case P2P_PENDING_GO_NEG_CONFIRM
:
2948 p2p_go_neg_conf_cb(p2p
, result
);
2950 case P2P_PENDING_SD
:
2951 p2p_sd_cb(p2p
, success
);
2953 case P2P_PENDING_PD
:
2954 p2p_prov_disc_cb(p2p
, success
);
2956 case P2P_PENDING_INVITATION_REQUEST
:
2957 p2p_invitation_req_cb(p2p
, success
);
2959 case P2P_PENDING_INVITATION_RESPONSE
:
2960 p2p_invitation_resp_cb(p2p
, success
);
2962 case P2P_PENDING_DEV_DISC_REQUEST
:
2963 p2p_dev_disc_req_cb(p2p
, success
);
2965 case P2P_PENDING_DEV_DISC_RESPONSE
:
2966 p2p_dev_disc_resp_cb(p2p
, success
);
2968 case P2P_PENDING_GO_DISC_REQ
:
2969 p2p_go_disc_req_cb(p2p
, success
);
2975 void p2p_listen_cb(struct p2p_data
*p2p
, unsigned int freq
,
2976 unsigned int duration
)
2978 if (freq
== p2p
->pending_client_disc_freq
) {
2979 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
2980 "P2P: Client discoverability remain-awake completed");
2981 p2p
->pending_client_disc_freq
= 0;
2985 if (freq
!= p2p
->pending_listen_freq
) {
2986 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
2987 "P2P: Unexpected listen callback for freq=%u "
2988 "duration=%u (pending_listen_freq=%u)",
2989 freq
, duration
, p2p
->pending_listen_freq
);
2993 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
2994 "P2P: Starting Listen timeout(%u,%u) on freq=%u based on "
2996 p2p
->pending_listen_sec
, p2p
->pending_listen_usec
,
2997 p2p
->pending_listen_freq
);
2999 p2p
->drv_in_listen
= freq
;
3000 if (p2p
->pending_listen_sec
|| p2p
->pending_listen_usec
) {
3002 * Add 20 msec extra wait to avoid race condition with driver
3003 * remain-on-channel end event, i.e., give driver more time to
3004 * complete the operation before our timeout expires.
3006 p2p_set_timeout(p2p
, p2p
->pending_listen_sec
,
3007 p2p
->pending_listen_usec
+ 20000);
3010 p2p
->pending_listen_freq
= 0;
3014 int p2p_listen_end(struct p2p_data
*p2p
, unsigned int freq
)
3016 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
, "P2P: Driver ended Listen "
3017 "state (freq=%u)", freq
);
3018 p2p
->drv_in_listen
= 0;
3020 return 0; /* Internal timeout will trigger the next step */
3022 if (p2p
->state
== P2P_CONNECT_LISTEN
&& p2p
->go_neg_peer
) {
3023 if (p2p
->go_neg_peer
->connect_reqs
>= 120) {
3024 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
3025 "P2P: Timeout on sending GO Negotiation "
3026 "Request without getting response");
3027 p2p_go_neg_failed(p2p
, p2p
->go_neg_peer
, -1);
3031 p2p_set_state(p2p
, P2P_CONNECT
);
3032 p2p_connect_send(p2p
, p2p
->go_neg_peer
);
3034 } else if (p2p
->state
== P2P_SEARCH
) {
3035 if (p2p
->p2p_scan_running
) {
3037 * Search is already in progress. This can happen if
3038 * an Action frame RX is reported immediately after
3039 * the end of a remain-on-channel operation and the
3040 * response frame to that is sent using an offchannel
3041 * operation while in p2p_find. Avoid an attempt to
3042 * restart a scan here.
3044 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
, "P2P: p2p_scan "
3045 "already in progress - do not try to start a "
3049 if (p2p
->pending_listen_freq
) {
3051 * Better wait a bit if the driver is unable to start
3052 * offchannel operation for some reason. p2p_search()
3053 * will be started from internal timeout.
3055 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
, "P2P: Listen "
3056 "operation did not seem to start - delay "
3057 "search phase to avoid busy loop");
3058 p2p_set_timeout(p2p
, 0, 100000);
3061 if (p2p
->search_delay
) {
3062 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
, "P2P: Delay "
3063 "search operation by %u ms",
3065 p2p_set_timeout(p2p
, p2p
->search_delay
/ 1000,
3066 (p2p
->search_delay
% 1000) * 1000);
3077 static void p2p_timeout_connect(struct p2p_data
*p2p
)
3079 p2p
->cfg
->send_action_done(p2p
->cfg
->cb_ctx
);
3080 if (p2p
->go_neg_peer
&&
3081 (p2p
->go_neg_peer
->flags
& P2P_DEV_WAIT_GO_NEG_CONFIRM
)) {
3082 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
, "P2P: Wait for GO "
3083 "Negotiation Confirm timed out - assume GO "
3084 "Negotiation failed");
3085 p2p_go_neg_failed(p2p
, p2p
->go_neg_peer
, -1);
3088 p2p_set_state(p2p
, P2P_CONNECT_LISTEN
);
3089 p2p_listen_in_find(p2p
, 0);
3093 static void p2p_timeout_connect_listen(struct p2p_data
*p2p
)
3095 if (p2p
->go_neg_peer
) {
3096 if (p2p
->drv_in_listen
) {
3097 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
, "P2P: Driver is "
3098 "still in Listen state; wait for it to "
3103 if (p2p
->go_neg_peer
->connect_reqs
>= 120) {
3104 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
3105 "P2P: Timeout on sending GO Negotiation "
3106 "Request without getting response");
3107 p2p_go_neg_failed(p2p
, p2p
->go_neg_peer
, -1);
3111 p2p_set_state(p2p
, P2P_CONNECT
);
3112 p2p_connect_send(p2p
, p2p
->go_neg_peer
);
3114 p2p_set_state(p2p
, P2P_IDLE
);
3118 static void p2p_timeout_wait_peer_connect(struct p2p_data
*p2p
)
3121 * TODO: could remain constantly in Listen state for some time if there
3122 * are no other concurrent uses for the radio. For now, go to listen
3123 * state once per second to give other uses a chance to use the radio.
3125 p2p_set_state(p2p
, P2P_WAIT_PEER_IDLE
);
3126 p2p_set_timeout(p2p
, 0, 500000);
3130 static void p2p_timeout_wait_peer_idle(struct p2p_data
*p2p
)
3132 struct p2p_device
*dev
= p2p
->go_neg_peer
;
3135 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
3136 "P2P: Unknown GO Neg peer - stop GO Neg wait");
3141 if (dev
->wait_count
>= 120) {
3142 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
3143 "P2P: Timeout on waiting peer to become ready for GO "
3145 p2p_go_neg_failed(p2p
, dev
, -1);
3149 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
3150 "P2P: Go to Listen state while waiting for the peer to become "
3151 "ready for GO Negotiation");
3152 p2p_set_state(p2p
, P2P_WAIT_PEER_CONNECT
);
3153 p2p_listen_in_find(p2p
, 0);
3157 static void p2p_timeout_sd_during_find(struct p2p_data
*p2p
)
3159 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
3160 "P2P: Service Discovery Query timeout");
3162 p2p
->cfg
->send_action_done(p2p
->cfg
->cb_ctx
);
3163 p2p
->sd_peer
->flags
&= ~P2P_DEV_SD_SCHEDULE
;
3164 p2p
->sd_peer
= NULL
;
3166 p2p_continue_find(p2p
);
3170 static void p2p_timeout_prov_disc_during_find(struct p2p_data
*p2p
)
3172 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
3173 "P2P: Provision Discovery Request timeout");
3174 p2p
->cfg
->send_action_done(p2p
->cfg
->cb_ctx
);
3175 p2p_continue_find(p2p
);
3179 static void p2p_timeout_prov_disc_req(struct p2p_data
*p2p
)
3181 p2p
->pending_action_state
= P2P_NO_PENDING_ACTION
;
3184 * For user initiated PD requests that we have not gotten any responses
3185 * for while in IDLE state, we retry them a couple of times before
3188 if (!p2p
->user_initiated_pd
)
3191 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
3192 "P2P: User initiated Provision Discovery Request timeout");
3194 if (p2p
->pd_retries
) {
3198 struct p2p_device
*dev
;
3201 dl_list_for_each(dev
, &p2p
->devices
, struct p2p_device
, list
) {
3202 if (os_memcmp(p2p
->pending_pd_devaddr
,
3203 dev
->info
.p2p_device_addr
, ETH_ALEN
) != 0)
3205 if (dev
->req_config_methods
&&
3206 (dev
->flags
& P2P_DEV_PD_FOR_JOIN
))
3210 if (p2p
->cfg
->prov_disc_fail
)
3211 p2p
->cfg
->prov_disc_fail(p2p
->cfg
->cb_ctx
,
3212 p2p
->pending_pd_devaddr
,
3214 P2P_PROV_DISC_TIMEOUT_JOIN
:
3215 P2P_PROV_DISC_TIMEOUT
);
3216 p2p_reset_pending_pd(p2p
);
3221 static void p2p_timeout_invite(struct p2p_data
*p2p
)
3223 p2p
->cfg
->send_action_done(p2p
->cfg
->cb_ctx
);
3224 p2p_set_state(p2p
, P2P_INVITE_LISTEN
);
3225 if (p2p
->inv_role
== P2P_INVITE_ROLE_ACTIVE_GO
) {
3227 * Better remain on operating channel instead of listen channel
3228 * when running a group.
3230 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
, "P2P: Inviting in "
3231 "active GO role - wait on operating channel");
3232 p2p_set_timeout(p2p
, 0, 100000);
3235 p2p_listen_in_find(p2p
, 0);
3239 static void p2p_timeout_invite_listen(struct p2p_data
*p2p
)
3241 if (p2p
->invite_peer
&& p2p
->invite_peer
->invitation_reqs
< 100) {
3242 p2p_set_state(p2p
, P2P_INVITE
);
3243 p2p_invite_send(p2p
, p2p
->invite_peer
,
3244 p2p
->invite_go_dev_addr
);
3246 if (p2p
->invite_peer
) {
3247 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
3248 "P2P: Invitation Request retry limit reached");
3249 if (p2p
->cfg
->invitation_result
)
3250 p2p
->cfg
->invitation_result(
3251 p2p
->cfg
->cb_ctx
, -1, NULL
);
3253 p2p_set_state(p2p
, P2P_IDLE
);
3258 static void p2p_state_timeout(void *eloop_ctx
, void *timeout_ctx
)
3260 struct p2p_data
*p2p
= eloop_ctx
;
3262 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
, "P2P: Timeout (state=%s)",
3263 p2p_state_txt(p2p
->state
));
3267 switch (p2p
->state
) {
3269 /* Check if we timed out waiting for PD req */
3270 if (p2p
->pending_action_state
== P2P_PENDING_PD
)
3271 p2p_timeout_prov_disc_req(p2p
);
3274 /* Check if we timed out waiting for PD req */
3275 if (p2p
->pending_action_state
== P2P_PENDING_PD
)
3276 p2p_timeout_prov_disc_req(p2p
);
3277 if (p2p
->search_delay
&& !p2p
->in_search_delay
) {
3278 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
, "P2P: Delay "
3279 "search operation by %u ms",
3281 p2p
->in_search_delay
= 1;
3282 p2p_set_timeout(p2p
, p2p
->search_delay
/ 1000,
3283 (p2p
->search_delay
% 1000) * 1000);
3286 p2p
->in_search_delay
= 0;
3290 p2p_timeout_connect(p2p
);
3292 case P2P_CONNECT_LISTEN
:
3293 p2p_timeout_connect_listen(p2p
);
3297 case P2P_LISTEN_ONLY
:
3298 /* Check if we timed out waiting for PD req */
3299 if (p2p
->pending_action_state
== P2P_PENDING_PD
)
3300 p2p_timeout_prov_disc_req(p2p
);
3302 if (p2p
->ext_listen_only
) {
3303 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
3304 "P2P: Extended Listen Timing - Listen State "
3306 p2p
->ext_listen_only
= 0;
3307 p2p_set_state(p2p
, P2P_IDLE
);
3310 case P2P_WAIT_PEER_CONNECT
:
3311 p2p_timeout_wait_peer_connect(p2p
);
3313 case P2P_WAIT_PEER_IDLE
:
3314 p2p_timeout_wait_peer_idle(p2p
);
3316 case P2P_SD_DURING_FIND
:
3317 p2p_timeout_sd_during_find(p2p
);
3319 case P2P_PROVISIONING
:
3321 case P2P_PD_DURING_FIND
:
3322 p2p_timeout_prov_disc_during_find(p2p
);
3325 p2p_timeout_invite(p2p
);
3327 case P2P_INVITE_LISTEN
:
3328 p2p_timeout_invite_listen(p2p
);
3330 case P2P_SEARCH_WHEN_READY
:
3332 case P2P_CONTINUE_SEARCH_WHEN_READY
:
3338 int p2p_reject(struct p2p_data
*p2p
, const u8
*peer_addr
)
3340 struct p2p_device
*dev
;
3342 dev
= p2p_get_device(p2p
, peer_addr
);
3343 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
, "P2P: Local request to reject "
3344 "connection attempts by peer " MACSTR
, MAC2STR(peer_addr
));
3346 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
, "P2P: Peer " MACSTR
3347 " unknown", MAC2STR(peer_addr
));
3350 dev
->status
= P2P_SC_FAIL_REJECTED_BY_USER
;
3351 dev
->flags
|= P2P_DEV_USER_REJECTED
;
3356 const char * p2p_wps_method_text(enum p2p_wps_method method
)
3361 case WPS_PIN_DISPLAY
:
3363 case WPS_PIN_KEYPAD
:
3373 static const char * p2p_go_state_text(enum p2p_go_state go_state
)
3388 const struct p2p_peer_info
* p2p_get_peer_info(struct p2p_data
*p2p
,
3389 const u8
*addr
, int next
)
3391 struct p2p_device
*dev
;
3394 dev
= p2p_get_device(p2p
, addr
);
3396 dev
= dl_list_first(&p2p
->devices
, struct p2p_device
, list
);
3399 dev
= dl_list_first(&dev
->list
, struct p2p_device
, list
);
3400 if (&dev
->list
== &p2p
->devices
)
3411 int p2p_get_peer_info_txt(const struct p2p_peer_info
*info
,
3412 char *buf
, size_t buflen
)
3414 struct p2p_device
*dev
;
3422 dev
= (struct p2p_device
*) (((u8
*) info
) -
3423 offsetof(struct p2p_device
, info
));
3429 res
= os_snprintf(pos
, end
- pos
,
3433 "interface_addr=" MACSTR
"\n"
3434 "member_in_go_dev=" MACSTR
"\n"
3435 "member_in_go_iface=" MACSTR
"\n"
3436 "go_neg_req_sent=%d\n"
3439 "intended_addr=" MACSTR
"\n"
3442 "req_config_methods=0x%x\n"
3443 "flags=%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n"
3446 "invitation_reqs=%u\n",
3447 (int) (now
.sec
- dev
->last_seen
.sec
),
3449 p2p_wps_method_text(dev
->wps_method
),
3450 MAC2STR(dev
->interface_addr
),
3451 MAC2STR(dev
->member_in_go_dev
),
3452 MAC2STR(dev
->member_in_go_iface
),
3453 dev
->go_neg_req_sent
,
3454 p2p_go_state_text(dev
->go_state
),
3456 MAC2STR(dev
->intended_addr
),
3457 dev
->country
[0] ? dev
->country
[0] : '_',
3458 dev
->country
[1] ? dev
->country
[1] : '_',
3460 dev
->req_config_methods
,
3461 dev
->flags
& P2P_DEV_PROBE_REQ_ONLY
?
3462 "[PROBE_REQ_ONLY]" : "",
3463 dev
->flags
& P2P_DEV_REPORTED
? "[REPORTED]" : "",
3464 dev
->flags
& P2P_DEV_NOT_YET_READY
?
3465 "[NOT_YET_READY]" : "",
3466 dev
->flags
& P2P_DEV_SD_INFO
? "[SD_INFO]" : "",
3467 dev
->flags
& P2P_DEV_SD_SCHEDULE
? "[SD_SCHEDULE]" :
3469 dev
->flags
& P2P_DEV_PD_PEER_DISPLAY
?
3470 "[PD_PEER_DISPLAY]" : "",
3471 dev
->flags
& P2P_DEV_PD_PEER_KEYPAD
?
3472 "[PD_PEER_KEYPAD]" : "",
3473 dev
->flags
& P2P_DEV_USER_REJECTED
?
3474 "[USER_REJECTED]" : "",
3475 dev
->flags
& P2P_DEV_PEER_WAITING_RESPONSE
?
3476 "[PEER_WAITING_RESPONSE]" : "",
3477 dev
->flags
& P2P_DEV_PREFER_PERSISTENT_GROUP
?
3478 "[PREFER_PERSISTENT_GROUP]" : "",
3479 dev
->flags
& P2P_DEV_WAIT_GO_NEG_RESPONSE
?
3480 "[WAIT_GO_NEG_RESPONSE]" : "",
3481 dev
->flags
& P2P_DEV_WAIT_GO_NEG_CONFIRM
?
3482 "[WAIT_GO_NEG_CONFIRM]" : "",
3483 dev
->flags
& P2P_DEV_GROUP_CLIENT_ONLY
?
3484 "[GROUP_CLIENT_ONLY]" : "",
3485 dev
->flags
& P2P_DEV_FORCE_FREQ
?
3486 "[FORCE_FREQ]" : "",
3487 dev
->flags
& P2P_DEV_PD_FOR_JOIN
?
3488 "[PD_FOR_JOIN]" : "",
3491 dev
->invitation_reqs
);
3492 if (res
< 0 || res
>= end
- pos
)
3496 if (dev
->ext_listen_period
) {
3497 res
= os_snprintf(pos
, end
- pos
,
3498 "ext_listen_period=%u\n"
3499 "ext_listen_interval=%u\n",
3500 dev
->ext_listen_period
,
3501 dev
->ext_listen_interval
);
3502 if (res
< 0 || res
>= end
- pos
)
3507 if (dev
->oper_ssid_len
) {
3508 res
= os_snprintf(pos
, end
- pos
,
3510 wpa_ssid_txt(dev
->oper_ssid
,
3511 dev
->oper_ssid_len
));
3512 if (res
< 0 || res
>= end
- pos
)
3517 #ifdef CONFIG_WIFI_DISPLAY
3518 if (dev
->info
.wfd_subelems
) {
3519 res
= os_snprintf(pos
, end
- pos
, "wfd_subelems=");
3520 if (res
< 0 || res
>= end
- pos
)
3524 pos
+= wpa_snprintf_hex(pos
, end
- pos
,
3525 wpabuf_head(dev
->info
.wfd_subelems
),
3526 wpabuf_len(dev
->info
.wfd_subelems
));
3528 res
= os_snprintf(pos
, end
- pos
, "\n");
3529 if (res
< 0 || res
>= end
- pos
)
3533 #endif /* CONFIG_WIFI_DISPLAY */
3539 int p2p_peer_known(struct p2p_data
*p2p
, const u8
*addr
)
3541 return p2p_get_device(p2p
, addr
) != NULL
;
3545 void p2p_set_client_discoverability(struct p2p_data
*p2p
, int enabled
)
3548 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
, "P2P: Client "
3549 "discoverability enabled");
3550 p2p
->dev_capab
|= P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY
;
3552 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
, "P2P: Client "
3553 "discoverability disabled");
3554 p2p
->dev_capab
&= ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY
;
3559 static struct wpabuf
* p2p_build_presence_req(u32 duration1
, u32 interval1
,
3560 u32 duration2
, u32 interval2
)
3563 struct p2p_noa_desc desc1
, desc2
, *ptr1
= NULL
, *ptr2
= NULL
;
3566 req
= wpabuf_alloc(100);
3570 if (duration1
|| interval1
) {
3571 os_memset(&desc1
, 0, sizeof(desc1
));
3572 desc1
.count_type
= 1;
3573 desc1
.duration
= duration1
;
3574 desc1
.interval
= interval1
;
3577 if (duration2
|| interval2
) {
3578 os_memset(&desc2
, 0, sizeof(desc2
));
3579 desc2
.count_type
= 2;
3580 desc2
.duration
= duration2
;
3581 desc2
.interval
= interval2
;
3586 p2p_buf_add_action_hdr(req
, P2P_PRESENCE_REQ
, 1);
3587 len
= p2p_buf_add_ie_hdr(req
);
3588 p2p_buf_add_noa(req
, 0, 0, 0, ptr1
, ptr2
);
3589 p2p_buf_update_ie_hdr(req
, len
);
3595 int p2p_presence_req(struct p2p_data
*p2p
, const u8
*go_interface_addr
,
3596 const u8
*own_interface_addr
, unsigned int freq
,
3597 u32 duration1
, u32 interval1
, u32 duration2
,
3602 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
, "P2P: Send Presence Request to "
3603 "GO " MACSTR
" (own interface " MACSTR
") freq=%u dur1=%u "
3604 "int1=%u dur2=%u int2=%u",
3605 MAC2STR(go_interface_addr
), MAC2STR(own_interface_addr
),
3606 freq
, duration1
, interval1
, duration2
, interval2
);
3608 req
= p2p_build_presence_req(duration1
, interval1
, duration2
,
3613 p2p
->pending_action_state
= P2P_NO_PENDING_ACTION
;
3614 if (p2p_send_action(p2p
, freq
, go_interface_addr
, own_interface_addr
,
3616 wpabuf_head(req
), wpabuf_len(req
), 200) < 0) {
3617 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
3618 "P2P: Failed to send Action frame");
3626 static struct wpabuf
* p2p_build_presence_resp(u8 status
, const u8
*noa
,
3627 size_t noa_len
, u8 dialog_token
)
3629 struct wpabuf
*resp
;
3632 resp
= wpabuf_alloc(100 + noa_len
);
3636 p2p_buf_add_action_hdr(resp
, P2P_PRESENCE_RESP
, dialog_token
);
3637 len
= p2p_buf_add_ie_hdr(resp
);
3638 p2p_buf_add_status(resp
, status
);
3640 wpabuf_put_u8(resp
, P2P_ATTR_NOTICE_OF_ABSENCE
);
3641 wpabuf_put_le16(resp
, noa_len
);
3642 wpabuf_put_data(resp
, noa
, noa_len
);
3644 p2p_buf_add_noa(resp
, 0, 0, 0, NULL
, NULL
);
3645 p2p_buf_update_ie_hdr(resp
, len
);
3651 static void p2p_process_presence_req(struct p2p_data
*p2p
, const u8
*da
,
3652 const u8
*sa
, const u8
*data
, size_t len
,
3655 struct p2p_message msg
;
3657 struct wpabuf
*resp
;
3659 struct p2p_group
*group
= NULL
;
3664 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
3665 "P2P: Received P2P Action - P2P Presence Request");
3667 for (g
= 0; g
< p2p
->num_groups
; g
++) {
3668 if (os_memcmp(da
, p2p_group_get_interface_addr(p2p
->groups
[g
]),
3670 group
= p2p
->groups
[g
];
3674 if (group
== NULL
) {
3675 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
3676 "P2P: Ignore P2P Presence Request for unknown group "
3677 MACSTR
, MAC2STR(da
));
3681 if (p2p_parse(data
, len
, &msg
) < 0) {
3682 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
3683 "P2P: Failed to parse P2P Presence Request");
3684 status
= P2P_SC_FAIL_INVALID_PARAMS
;
3689 if (msg
.noa
== NULL
) {
3690 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
3691 "P2P: No NoA attribute in P2P Presence Request");
3692 status
= P2P_SC_FAIL_INVALID_PARAMS
;
3696 status
= p2p_group_presence_req(group
, sa
, msg
.noa
, msg
.noa_len
);
3699 if (p2p
->cfg
->get_noa
)
3700 noa_len
= p2p
->cfg
->get_noa(p2p
->cfg
->cb_ctx
, da
, noa
,
3704 resp
= p2p_build_presence_resp(status
, noa_len
> 0 ? noa
: NULL
,
3705 noa_len
> 0 ? noa_len
: 0,
3708 p2p_parse_free(&msg
);
3712 p2p
->pending_action_state
= P2P_NO_PENDING_ACTION
;
3713 if (p2p_send_action(p2p
, rx_freq
, sa
, da
, da
,
3714 wpabuf_head(resp
), wpabuf_len(resp
), 200) < 0) {
3715 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
3716 "P2P: Failed to send Action frame");
3722 static void p2p_process_presence_resp(struct p2p_data
*p2p
, const u8
*da
,
3723 const u8
*sa
, const u8
*data
, size_t len
)
3725 struct p2p_message msg
;
3727 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
3728 "P2P: Received P2P Action - P2P Presence Response");
3730 if (p2p_parse(data
, len
, &msg
) < 0) {
3731 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
3732 "P2P: Failed to parse P2P Presence Response");
3736 if (msg
.status
== NULL
|| msg
.noa
== NULL
) {
3737 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
3738 "P2P: No Status or NoA attribute in P2P Presence "
3740 p2p_parse_free(&msg
);
3745 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
3746 "P2P: P2P Presence Request was rejected: status %u",
3748 p2p_parse_free(&msg
);
3752 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
3753 "P2P: P2P Presence Request was accepted");
3754 wpa_hexdump(MSG_DEBUG
, "P2P: P2P Presence Response - NoA",
3755 msg
.noa
, msg
.noa_len
);
3756 /* TODO: process NoA */
3757 p2p_parse_free(&msg
);
3761 static void p2p_ext_listen_timeout(void *eloop_ctx
, void *timeout_ctx
)
3763 struct p2p_data
*p2p
= eloop_ctx
;
3765 if (p2p
->ext_listen_interval
) {
3766 /* Schedule next extended listen timeout */
3767 eloop_register_timeout(p2p
->ext_listen_interval_sec
,
3768 p2p
->ext_listen_interval_usec
,
3769 p2p_ext_listen_timeout
, p2p
, NULL
);
3772 if (p2p
->state
== P2P_LISTEN_ONLY
&& p2p
->ext_listen_only
) {
3774 * This should not really happen, but it looks like the Listen
3775 * command may fail is something else (e.g., a scan) was
3776 * running at an inconvenient time. As a workaround, allow new
3777 * Extended Listen operation to be started.
3779 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
, "P2P: Previous "
3780 "Extended Listen operation had not been completed - "
3782 p2p
->ext_listen_only
= 0;
3783 p2p_set_state(p2p
, P2P_IDLE
);
3786 if (p2p
->state
!= P2P_IDLE
) {
3787 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
, "P2P: Skip Extended "
3788 "Listen timeout in active state (%s)",
3789 p2p_state_txt(p2p
->state
));
3793 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
, "P2P: Extended Listen timeout");
3794 p2p
->ext_listen_only
= 1;
3795 if (p2p_listen(p2p
, p2p
->ext_listen_period
) < 0) {
3796 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
, "P2P: Failed to start "
3797 "Listen state for Extended Listen Timing");
3798 p2p
->ext_listen_only
= 0;
3803 int p2p_ext_listen(struct p2p_data
*p2p
, unsigned int period
,
3804 unsigned int interval
)
3806 if (period
> 65535 || interval
> 65535 || period
> interval
||
3807 (period
== 0 && interval
> 0) || (period
> 0 && interval
== 0)) {
3808 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
3809 "P2P: Invalid Extended Listen Timing request: "
3810 "period=%u interval=%u", period
, interval
);
3814 eloop_cancel_timeout(p2p_ext_listen_timeout
, p2p
, NULL
);
3816 if (interval
== 0) {
3817 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
3818 "P2P: Disabling Extended Listen Timing");
3819 p2p
->ext_listen_period
= 0;
3820 p2p
->ext_listen_interval
= 0;
3824 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
,
3825 "P2P: Enabling Extended Listen Timing: period %u msec, "
3826 "interval %u msec", period
, interval
);
3827 p2p
->ext_listen_period
= period
;
3828 p2p
->ext_listen_interval
= interval
;
3829 p2p
->ext_listen_interval_sec
= interval
/ 1000;
3830 p2p
->ext_listen_interval_usec
= (interval
% 1000) * 1000;
3832 eloop_register_timeout(p2p
->ext_listen_interval_sec
,
3833 p2p
->ext_listen_interval_usec
,
3834 p2p_ext_listen_timeout
, p2p
, NULL
);
3840 void p2p_deauth_notif(struct p2p_data
*p2p
, const u8
*bssid
, u16 reason_code
,
3841 const u8
*ie
, size_t ie_len
)
3843 struct p2p_message msg
;
3845 if (bssid
== NULL
|| ie
== NULL
)
3848 os_memset(&msg
, 0, sizeof(msg
));
3849 if (p2p_parse_ies(ie
, ie_len
, &msg
))
3851 if (msg
.minor_reason_code
== NULL
)
3854 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_INFO
,
3855 "P2P: Deauthentication notification BSSID " MACSTR
3856 " reason_code=%u minor_reason_code=%u",
3857 MAC2STR(bssid
), reason_code
, *msg
.minor_reason_code
);
3859 p2p_parse_free(&msg
);
3863 void p2p_disassoc_notif(struct p2p_data
*p2p
, const u8
*bssid
, u16 reason_code
,
3864 const u8
*ie
, size_t ie_len
)
3866 struct p2p_message msg
;
3868 if (bssid
== NULL
|| ie
== NULL
)
3871 os_memset(&msg
, 0, sizeof(msg
));
3872 if (p2p_parse_ies(ie
, ie_len
, &msg
))
3874 if (msg
.minor_reason_code
== NULL
)
3877 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_INFO
,
3878 "P2P: Disassociation notification BSSID " MACSTR
3879 " reason_code=%u minor_reason_code=%u",
3880 MAC2STR(bssid
), reason_code
, *msg
.minor_reason_code
);
3882 p2p_parse_free(&msg
);
3886 void p2p_set_managed_oper(struct p2p_data
*p2p
, int enabled
)
3889 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
, "P2P: Managed P2P "
3890 "Device operations enabled");
3891 p2p
->dev_capab
|= P2P_DEV_CAPAB_INFRA_MANAGED
;
3893 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
, "P2P: Managed P2P "
3894 "Device operations disabled");
3895 p2p
->dev_capab
&= ~P2P_DEV_CAPAB_INFRA_MANAGED
;
3900 int p2p_set_listen_channel(struct p2p_data
*p2p
, u8 reg_class
, u8 channel
)
3902 if (p2p_channel_to_freq(p2p
->cfg
->country
, reg_class
, channel
) < 0)
3905 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
, "P2P: Set Listen channel: "
3906 "reg_class %u channel %u", reg_class
, channel
);
3907 p2p
->cfg
->reg_class
= reg_class
;
3908 p2p
->cfg
->channel
= channel
;
3914 int p2p_set_ssid_postfix(struct p2p_data
*p2p
, const u8
*postfix
, size_t len
)
3916 wpa_hexdump_ascii(MSG_DEBUG
, "P2P: New SSID postfix", postfix
, len
);
3917 if (postfix
== NULL
) {
3918 p2p
->cfg
->ssid_postfix_len
= 0;
3921 if (len
> sizeof(p2p
->cfg
->ssid_postfix
))
3923 os_memcpy(p2p
->cfg
->ssid_postfix
, postfix
, len
);
3924 p2p
->cfg
->ssid_postfix_len
= len
;
3929 int p2p_set_oper_channel(struct p2p_data
*p2p
, u8 op_reg_class
, u8 op_channel
,
3932 if (p2p_channel_to_freq(p2p
->cfg
->country
, op_reg_class
, op_channel
)
3936 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_INFO
, "P2P: Set Operating channel: "
3937 "reg_class %u channel %u", op_reg_class
, op_channel
);
3938 p2p
->cfg
->op_reg_class
= op_reg_class
;
3939 p2p
->cfg
->op_channel
= op_channel
;
3940 p2p
->cfg
->cfg_op_channel
= cfg_op_channel
;
3945 int p2p_set_pref_chan(struct p2p_data
*p2p
, unsigned int num_pref_chan
,
3946 const struct p2p_channel
*pref_chan
)
3948 struct p2p_channel
*n
;
3951 n
= os_malloc(num_pref_chan
* sizeof(struct p2p_channel
));
3954 os_memcpy(n
, pref_chan
,
3955 num_pref_chan
* sizeof(struct p2p_channel
));
3959 os_free(p2p
->cfg
->pref_chan
);
3960 p2p
->cfg
->pref_chan
= n
;
3961 p2p
->cfg
->num_pref_chan
= num_pref_chan
;
3967 int p2p_get_interface_addr(struct p2p_data
*p2p
, const u8
*dev_addr
,
3970 struct p2p_device
*dev
= p2p_get_device(p2p
, dev_addr
);
3971 if (dev
== NULL
|| is_zero_ether_addr(dev
->interface_addr
))
3973 os_memcpy(iface_addr
, dev
->interface_addr
, ETH_ALEN
);
3978 int p2p_get_dev_addr(struct p2p_data
*p2p
, const u8
*iface_addr
,
3981 struct p2p_device
*dev
= p2p_get_device_interface(p2p
, iface_addr
);
3984 os_memcpy(dev_addr
, dev
->info
.p2p_device_addr
, ETH_ALEN
);
3989 void p2p_set_peer_filter(struct p2p_data
*p2p
, const u8
*addr
)
3991 os_memcpy(p2p
->peer_filter
, addr
, ETH_ALEN
);
3992 if (is_zero_ether_addr(p2p
->peer_filter
))
3993 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
, "P2P: Disable peer "
3996 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
, "P2P: Enable peer "
3997 "filter for " MACSTR
, MAC2STR(p2p
->peer_filter
));
4001 void p2p_set_cross_connect(struct p2p_data
*p2p
, int enabled
)
4003 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
, "P2P: Cross connection %s",
4004 enabled
? "enabled" : "disabled");
4005 if (p2p
->cross_connect
== enabled
)
4007 p2p
->cross_connect
= enabled
;
4008 /* TODO: may need to tear down any action group where we are GO(?) */
4012 int p2p_get_oper_freq(struct p2p_data
*p2p
, const u8
*iface_addr
)
4014 struct p2p_device
*dev
= p2p_get_device_interface(p2p
, iface_addr
);
4017 if (dev
->oper_freq
<= 0)
4019 return dev
->oper_freq
;
4023 void p2p_set_intra_bss_dist(struct p2p_data
*p2p
, int enabled
)
4025 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
, "P2P: Intra BSS distribution %s",
4026 enabled
? "enabled" : "disabled");
4027 p2p
->cfg
->p2p_intra_bss
= enabled
;
4031 void p2p_update_channel_list(struct p2p_data
*p2p
, struct p2p_channels
*chan
)
4033 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
, "P2P: Update channel list");
4034 os_memcpy(&p2p
->cfg
->channels
, chan
, sizeof(struct p2p_channels
));
4038 int p2p_send_action(struct p2p_data
*p2p
, unsigned int freq
, const u8
*dst
,
4039 const u8
*src
, const u8
*bssid
, const u8
*buf
,
4040 size_t len
, unsigned int wait_time
)
4042 if (p2p
->p2p_scan_running
) {
4043 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
, "P2P: Delay Action "
4044 "frame TX until p2p_scan completes");
4045 if (p2p
->after_scan_tx
) {
4046 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
, "P2P: Dropped "
4047 "previous pending Action frame TX");
4048 os_free(p2p
->after_scan_tx
);
4050 p2p
->after_scan_tx
= os_malloc(sizeof(*p2p
->after_scan_tx
) +
4052 if (p2p
->after_scan_tx
== NULL
)
4054 p2p
->after_scan_tx
->freq
= freq
;
4055 os_memcpy(p2p
->after_scan_tx
->dst
, dst
, ETH_ALEN
);
4056 os_memcpy(p2p
->after_scan_tx
->src
, src
, ETH_ALEN
);
4057 os_memcpy(p2p
->after_scan_tx
->bssid
, bssid
, ETH_ALEN
);
4058 p2p
->after_scan_tx
->len
= len
;
4059 p2p
->after_scan_tx
->wait_time
= wait_time
;
4060 os_memcpy(p2p
->after_scan_tx
+ 1, buf
, len
);
4064 return p2p
->cfg
->send_action(p2p
->cfg
->cb_ctx
, freq
, dst
, src
, bssid
,
4065 buf
, len
, wait_time
);
4069 void p2p_set_best_channels(struct p2p_data
*p2p
, int freq_24
, int freq_5
,
4072 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
, "P2P: Best channel: 2.4 GHz: %d,"
4073 " 5 GHz: %d, overall: %d", freq_24
, freq_5
, freq_overall
);
4074 p2p
->best_freq_24
= freq_24
;
4075 p2p
->best_freq_5
= freq_5
;
4076 p2p
->best_freq_overall
= freq_overall
;
4080 const u8
* p2p_get_go_neg_peer(struct p2p_data
*p2p
)
4082 if (p2p
== NULL
|| p2p
->go_neg_peer
== NULL
)
4084 return p2p
->go_neg_peer
->info
.p2p_device_addr
;
4088 const struct p2p_peer_info
*
4089 p2p_get_peer_found(struct p2p_data
*p2p
, const u8
*addr
, int next
)
4091 struct p2p_device
*dev
;
4094 dev
= p2p_get_device(p2p
, addr
);
4099 if (dev
->flags
& P2P_DEV_PROBE_REQ_ONLY
)
4105 dev
= dl_list_first(&dev
->list
,
4108 if (&dev
->list
== &p2p
->devices
)
4110 } while (dev
->flags
& P2P_DEV_PROBE_REQ_ONLY
);
4113 dev
= dl_list_first(&p2p
->devices
, struct p2p_device
, list
);
4116 while (dev
->flags
& P2P_DEV_PROBE_REQ_ONLY
) {
4117 dev
= dl_list_first(&dev
->list
,
4120 if (&dev
->list
== &p2p
->devices
)
4129 int p2p_in_progress(struct p2p_data
*p2p
)
4133 if (p2p
->state
== P2P_SEARCH
|| p2p
->state
== P2P_SEARCH_WHEN_READY
||
4134 p2p
->state
== P2P_CONTINUE_SEARCH_WHEN_READY
)
4136 return p2p
->state
!= P2P_IDLE
&& p2p
->state
!= P2P_PROVISIONING
;
4140 void p2p_set_config_timeout(struct p2p_data
*p2p
, u8 go_timeout
,
4144 p2p
->go_timeout
= go_timeout
;
4145 p2p
->client_timeout
= client_timeout
;
4150 void p2p_increase_search_delay(struct p2p_data
*p2p
, unsigned int delay
)
4152 if (p2p
&& p2p
->search_delay
< delay
)
4153 p2p
->search_delay
= delay
;
4157 #ifdef CONFIG_WIFI_DISPLAY
4159 static void p2p_update_wfd_ie_groups(struct p2p_data
*p2p
)
4162 struct p2p_group
*group
;
4164 for (g
= 0; g
< p2p
->num_groups
; g
++) {
4165 group
= p2p
->groups
[g
];
4166 p2p_group_update_ies(group
);
4171 int p2p_set_wfd_ie_beacon(struct p2p_data
*p2p
, struct wpabuf
*ie
)
4173 wpabuf_free(p2p
->wfd_ie_beacon
);
4174 p2p
->wfd_ie_beacon
= ie
;
4175 p2p_update_wfd_ie_groups(p2p
);
4180 int p2p_set_wfd_ie_probe_req(struct p2p_data
*p2p
, struct wpabuf
*ie
)
4182 wpabuf_free(p2p
->wfd_ie_probe_req
);
4183 p2p
->wfd_ie_probe_req
= ie
;
4188 int p2p_set_wfd_ie_probe_resp(struct p2p_data
*p2p
, struct wpabuf
*ie
)
4190 wpabuf_free(p2p
->wfd_ie_probe_resp
);
4191 p2p
->wfd_ie_probe_resp
= ie
;
4192 p2p_update_wfd_ie_groups(p2p
);
4197 int p2p_set_wfd_ie_assoc_req(struct p2p_data
*p2p
, struct wpabuf
*ie
)
4199 wpabuf_free(p2p
->wfd_ie_assoc_req
);
4200 p2p
->wfd_ie_assoc_req
= ie
;
4205 int p2p_set_wfd_ie_invitation(struct p2p_data
*p2p
, struct wpabuf
*ie
)
4207 wpabuf_free(p2p
->wfd_ie_invitation
);
4208 p2p
->wfd_ie_invitation
= ie
;
4213 int p2p_set_wfd_ie_prov_disc_req(struct p2p_data
*p2p
, struct wpabuf
*ie
)
4215 wpabuf_free(p2p
->wfd_ie_prov_disc_req
);
4216 p2p
->wfd_ie_prov_disc_req
= ie
;
4221 int p2p_set_wfd_ie_prov_disc_resp(struct p2p_data
*p2p
, struct wpabuf
*ie
)
4223 wpabuf_free(p2p
->wfd_ie_prov_disc_resp
);
4224 p2p
->wfd_ie_prov_disc_resp
= ie
;
4229 int p2p_set_wfd_ie_go_neg(struct p2p_data
*p2p
, struct wpabuf
*ie
)
4231 wpabuf_free(p2p
->wfd_ie_go_neg
);
4232 p2p
->wfd_ie_go_neg
= ie
;
4237 int p2p_set_wfd_dev_info(struct p2p_data
*p2p
, const struct wpabuf
*elem
)
4239 wpabuf_free(p2p
->wfd_dev_info
);
4241 p2p
->wfd_dev_info
= wpabuf_dup(elem
);
4242 if (p2p
->wfd_dev_info
== NULL
)
4245 p2p
->wfd_dev_info
= NULL
;
4251 int p2p_set_wfd_assoc_bssid(struct p2p_data
*p2p
, const struct wpabuf
*elem
)
4253 wpabuf_free(p2p
->wfd_assoc_bssid
);
4255 p2p
->wfd_assoc_bssid
= wpabuf_dup(elem
);
4256 if (p2p
->wfd_assoc_bssid
== NULL
)
4259 p2p
->wfd_assoc_bssid
= NULL
;
4265 int p2p_set_wfd_coupled_sink_info(struct p2p_data
*p2p
,
4266 const struct wpabuf
*elem
)
4268 wpabuf_free(p2p
->wfd_coupled_sink_info
);
4270 p2p
->wfd_coupled_sink_info
= wpabuf_dup(elem
);
4271 if (p2p
->wfd_coupled_sink_info
== NULL
)
4274 p2p
->wfd_coupled_sink_info
= NULL
;
4279 #endif /* CONFIG_WIFI_DISPLAY */
4282 int p2p_set_disc_int(struct p2p_data
*p2p
, int min_disc_int
, int max_disc_int
,
4285 if (min_disc_int
> max_disc_int
|| min_disc_int
< 0 || max_disc_int
< 0)
4288 p2p
->min_disc_int
= min_disc_int
;
4289 p2p
->max_disc_int
= max_disc_int
;
4290 p2p
->max_disc_tu
= max_disc_tu
;
4291 wpa_msg(p2p
->cfg
->msg_ctx
, MSG_DEBUG
, "P2P: Set discoverable interval: "
4292 "min=%d max=%d max_tu=%d", min_disc_int
, max_disc_int
,