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_common.h"
14 #include "common/ieee802_11_defs.h"
15 #include "common/wpa_ctrl.h"
16 #include "wps/wps_i.h"
18 #include "ap/hostapd.h"
19 #include "ap/ap_config.h"
20 #include "ap/sta_info.h"
21 #include "ap/ap_drv_ops.h"
22 #include "ap/p2p_hostapd.h"
23 #include "eapol_supp/eapol_supp_sm.h"
24 #include "rsn_supp/wpa.h"
25 #include "wpa_supplicant_i.h"
28 #include "config_ssid.h"
33 #include "offchannel.h"
34 #include "wps_supplicant.h"
35 #include "p2p_supplicant.h"
39 * How many times to try to scan to find the GO before giving up on join
42 #define P2P_MAX_JOIN_SCAN_ATTEMPTS 10
44 #define P2P_AUTO_PD_SCAN_ATTEMPTS 5
46 #ifndef P2P_MAX_CLIENT_IDLE
48 * How many seconds to try to reconnect to the GO when connection in P2P client
51 #define P2P_MAX_CLIENT_IDLE 10
52 #endif /* P2P_MAX_CLIENT_IDLE */
54 #ifndef P2P_MAX_INITIAL_CONN_WAIT
56 * How many seconds to wait for initial 4-way handshake to get completed after
57 * WPS provisioning step.
59 #define P2P_MAX_INITIAL_CONN_WAIT 10
60 #endif /* P2P_MAX_INITIAL_CONN_WAIT */
62 #ifndef P2P_MAX_INITIAL_CONN_WAIT_GO
64 * How many seconds to wait for initial 4-way handshake to get completed after
65 * WPS provisioning step on the GO. This controls the extra time the P2P
66 * operation is considered to be in progress (e.g., to delay other scans) after
67 * WPS provisioning has been completed on the GO during group formation.
69 #define P2P_MAX_INITIAL_CONN_WAIT_GO 10
70 #endif /* P2P_MAX_INITIAL_CONN_WAIT_GO */
72 #ifndef P2P_CONCURRENT_SEARCH_DELAY
73 #define P2P_CONCURRENT_SEARCH_DELAY 500
74 #endif /* P2P_CONCURRENT_SEARCH_DELAY */
76 #define P2P_MGMT_DEVICE_PREFIX "p2p-dev-"
78 enum p2p_group_removal_reason
{
79 P2P_GROUP_REMOVAL_UNKNOWN
,
80 P2P_GROUP_REMOVAL_SILENT
,
81 P2P_GROUP_REMOVAL_FORMATION_FAILED
,
82 P2P_GROUP_REMOVAL_REQUESTED
,
83 P2P_GROUP_REMOVAL_IDLE_TIMEOUT
,
84 P2P_GROUP_REMOVAL_UNAVAILABLE
,
85 P2P_GROUP_REMOVAL_GO_ENDING_SESSION
,
86 P2P_GROUP_REMOVAL_PSK_FAILURE
90 static void wpas_p2p_long_listen_timeout(void *eloop_ctx
, void *timeout_ctx
);
91 static struct wpa_supplicant
*
92 wpas_p2p_get_group_iface(struct wpa_supplicant
*wpa_s
, int addr_allocated
,
94 static int wpas_p2p_join_start(struct wpa_supplicant
*wpa_s
);
95 static void wpas_p2p_join_scan_req(struct wpa_supplicant
*wpa_s
, int freq
);
96 static void wpas_p2p_join_scan(void *eloop_ctx
, void *timeout_ctx
);
97 static int wpas_p2p_join(struct wpa_supplicant
*wpa_s
, const u8
*iface_addr
,
98 const u8
*dev_addr
, enum p2p_wps_method wps_method
,
100 static int wpas_p2p_create_iface(struct wpa_supplicant
*wpa_s
);
101 static void wpas_p2p_cross_connect_setup(struct wpa_supplicant
*wpa_s
);
102 static void wpas_p2p_group_idle_timeout(void *eloop_ctx
, void *timeout_ctx
);
103 static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant
*wpa_s
);
104 static void wpas_p2p_group_formation_timeout(void *eloop_ctx
,
106 static void wpas_p2p_fallback_to_go_neg(struct wpa_supplicant
*wpa_s
,
108 static int wpas_p2p_stop_find_oper(struct wpa_supplicant
*wpa_s
);
112 * Get the number of concurrent channels that the HW can operate, but that are
113 * currently not in use by any of the wpa_supplicant interfaces.
115 static int wpas_p2p_num_unused_channels(struct wpa_supplicant
*wpa_s
)
120 freqs
= os_calloc(wpa_s
->num_multichan_concurrent
, sizeof(int));
124 num
= get_shared_radio_freqs(wpa_s
, freqs
,
125 wpa_s
->num_multichan_concurrent
);
128 return wpa_s
->num_multichan_concurrent
- num
;
133 * Get the frequencies that are currently in use by one or more of the virtual
134 * interfaces, and that are also valid for P2P operation.
136 static int wpas_p2p_valid_oper_freqs(struct wpa_supplicant
*wpa_s
,
137 int *p2p_freqs
, unsigned int len
)
140 unsigned int num
, i
, j
;
142 freqs
= os_calloc(wpa_s
->num_multichan_concurrent
, sizeof(int));
146 num
= get_shared_radio_freqs(wpa_s
, freqs
,
147 wpa_s
->num_multichan_concurrent
);
149 os_memset(p2p_freqs
, 0, sizeof(int) * len
);
151 for (i
= 0, j
= 0; i
< num
&& j
< len
; i
++) {
152 if (p2p_supported_freq(wpa_s
->global
->p2p
, freqs
[i
]))
153 p2p_freqs
[j
++] = freqs
[i
];
162 static void wpas_p2p_set_own_freq_preference(struct wpa_supplicant
*wpa_s
,
165 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
167 if (freq
> 0 && wpa_s
->num_multichan_concurrent
> 1 &&
168 wpas_p2p_num_unused_channels(wpa_s
) > 0 &&
169 wpa_s
->parent
->conf
->p2p_ignore_shared_freq
)
171 p2p_set_own_freq_preference(wpa_s
->global
->p2p
, freq
);
175 static void wpas_p2p_scan_res_handler(struct wpa_supplicant
*wpa_s
,
176 struct wpa_scan_results
*scan_res
)
180 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
183 wpa_printf(MSG_DEBUG
, "P2P: Scan results received (%d BSS)",
184 (int) scan_res
->num
);
186 for (i
= 0; i
< scan_res
->num
; i
++) {
187 struct wpa_scan_res
*bss
= scan_res
->res
[i
];
188 struct os_time time_tmp_age
, entry_ts
;
189 time_tmp_age
.sec
= bss
->age
/ 1000;
190 time_tmp_age
.usec
= (bss
->age
% 1000) * 1000;
191 os_time_sub(&scan_res
->fetch_time
, &time_tmp_age
, &entry_ts
);
192 if (p2p_scan_res_handler(wpa_s
->global
->p2p
, bss
->bssid
,
193 bss
->freq
, &entry_ts
, bss
->level
,
194 (const u8
*) (bss
+ 1),
199 p2p_scan_res_handled(wpa_s
->global
->p2p
);
203 static int wpas_p2p_scan(void *ctx
, enum p2p_scan_type type
, int freq
,
204 unsigned int num_req_dev_types
,
205 const u8
*req_dev_types
, const u8
*dev_id
, u16 pw_id
)
207 struct wpa_supplicant
*wpa_s
= ctx
;
208 struct wpa_supplicant
*ifs
;
209 struct wpa_driver_scan_params params
;
211 struct wpabuf
*wps_ie
, *ies
;
212 int social_channels
[] = { 2412, 2437, 2462, 0, 0 };
215 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
218 for (ifs
= wpa_s
->global
->ifaces
; ifs
; ifs
= ifs
->next
) {
219 if (ifs
->sta_scan_pending
&&
220 (wpas_scan_scheduled(ifs
) || ifs
->scanning
) &&
221 wpas_p2p_in_progress(wpa_s
) == 2) {
222 wpa_printf(MSG_DEBUG
, "Delaying P2P scan to allow "
223 "pending station mode scan to be "
224 "completed on interface %s", ifs
->ifname
);
225 wpa_s
->global
->p2p_cb_on_scan_complete
= 1;
226 wpa_supplicant_req_scan(ifs
, 0, 0);
231 os_memset(¶ms
, 0, sizeof(params
));
233 /* P2P Wildcard SSID */
234 params
.num_ssids
= 1;
235 params
.ssids
[0].ssid
= (u8
*) P2P_WILDCARD_SSID
;
236 params
.ssids
[0].ssid_len
= P2P_WILDCARD_SSID_LEN
;
238 wpa_s
->wps
->dev
.p2p
= 1;
239 wps_ie
= wps_build_probe_req_ie(pw_id
, &wpa_s
->wps
->dev
,
240 wpa_s
->wps
->uuid
, WPS_REQ_ENROLLEE
,
241 num_req_dev_types
, req_dev_types
);
245 ielen
= p2p_scan_ie_buf_len(wpa_s
->global
->p2p
);
246 ies
= wpabuf_alloc(wpabuf_len(wps_ie
) + ielen
);
251 wpabuf_put_buf(ies
, wps_ie
);
254 p2p_scan_ie(wpa_s
->global
->p2p
, ies
, dev_id
);
256 params
.p2p_probe
= 1;
257 params
.extra_ies
= wpabuf_head(ies
);
258 params
.extra_ies_len
= wpabuf_len(ies
);
261 case P2P_SCAN_SOCIAL
:
262 params
.freqs
= social_channels
;
266 case P2P_SCAN_SOCIAL_PLUS_ONE
:
267 social_channels
[3] = freq
;
268 params
.freqs
= social_channels
;
272 ret
= wpa_drv_scan(wpa_s
, ¶ms
);
277 for (ifs
= wpa_s
->global
->ifaces
; ifs
; ifs
= ifs
->next
) {
279 ifs
->scan_res_handler
== wpas_p2p_scan_res_handler
) {
280 wpa_s
->global
->p2p_cb_on_scan_complete
= 1;
286 os_get_time(&wpa_s
->scan_trigger_time
);
287 wpa_s
->scan_res_handler
= wpas_p2p_scan_res_handler
;
294 static enum wpa_driver_if_type
wpas_p2p_if_type(int p2p_group_interface
)
296 switch (p2p_group_interface
) {
297 case P2P_GROUP_INTERFACE_PENDING
:
298 return WPA_IF_P2P_GROUP
;
299 case P2P_GROUP_INTERFACE_GO
:
300 return WPA_IF_P2P_GO
;
301 case P2P_GROUP_INTERFACE_CLIENT
:
302 return WPA_IF_P2P_CLIENT
;
305 return WPA_IF_P2P_GROUP
;
309 static struct wpa_supplicant
* wpas_get_p2p_group(struct wpa_supplicant
*wpa_s
,
311 size_t ssid_len
, int *go
)
315 for (wpa_s
= wpa_s
->global
->ifaces
; wpa_s
; wpa_s
= wpa_s
->next
) {
316 for (s
= wpa_s
->conf
->ssid
; s
; s
= s
->next
) {
317 if (s
->disabled
!= 0 || !s
->p2p_group
||
318 s
->ssid_len
!= ssid_len
||
319 os_memcmp(ssid
, s
->ssid
, ssid_len
) != 0)
321 if (s
->mode
== WPAS_MODE_P2P_GO
&&
322 s
!= wpa_s
->current_ssid
)
325 *go
= s
->mode
== WPAS_MODE_P2P_GO
;
334 static int wpas_p2p_group_delete(struct wpa_supplicant
*wpa_s
,
335 enum p2p_group_removal_reason removal_reason
)
337 struct wpa_ssid
*ssid
;
341 ssid
= wpa_s
->current_ssid
;
344 * The current SSID was not known, but there may still be a
345 * pending P2P group interface waiting for provisioning or a
346 * P2P group that is trying to reconnect.
348 ssid
= wpa_s
->conf
->ssid
;
350 if (ssid
->p2p_group
&& ssid
->disabled
!= 2)
355 wpa_s
->p2p_group_interface
== NOT_P2P_GROUP_INTERFACE
)
357 wpa_printf(MSG_ERROR
, "P2P: P2P group interface "
362 if (wpa_s
->p2p_group_interface
== P2P_GROUP_INTERFACE_GO
)
364 else if (wpa_s
->p2p_group_interface
== P2P_GROUP_INTERFACE_CLIENT
||
365 (ssid
&& ssid
->mode
== WPAS_MODE_INFRA
)) {
366 wpa_s
->reassociate
= 0;
367 wpa_s
->disconnected
= 1;
368 wpa_supplicant_deauthenticate(wpa_s
,
369 WLAN_REASON_DEAUTH_LEAVING
);
373 if (wpa_s
->cross_connect_in_use
) {
374 wpa_s
->cross_connect_in_use
= 0;
375 wpa_msg_global(wpa_s
->parent
, MSG_INFO
,
376 P2P_EVENT_CROSS_CONNECT_DISABLE
"%s %s",
377 wpa_s
->ifname
, wpa_s
->cross_connect_uplink
);
379 switch (removal_reason
) {
380 case P2P_GROUP_REMOVAL_REQUESTED
:
381 reason
= " reason=REQUESTED";
383 case P2P_GROUP_REMOVAL_FORMATION_FAILED
:
384 reason
= " reason=FORMATION_FAILED";
386 case P2P_GROUP_REMOVAL_IDLE_TIMEOUT
:
387 reason
= " reason=IDLE";
389 case P2P_GROUP_REMOVAL_UNAVAILABLE
:
390 reason
= " reason=UNAVAILABLE";
392 case P2P_GROUP_REMOVAL_GO_ENDING_SESSION
:
393 reason
= " reason=GO_ENDING_SESSION";
395 case P2P_GROUP_REMOVAL_PSK_FAILURE
:
396 reason
= " reason=PSK_FAILURE";
402 if (removal_reason
!= P2P_GROUP_REMOVAL_SILENT
) {
403 wpa_msg_global(wpa_s
->parent
, MSG_INFO
,
404 P2P_EVENT_GROUP_REMOVED
"%s %s%s",
405 wpa_s
->ifname
, gtype
, reason
);
408 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout
, wpa_s
, NULL
) > 0)
409 wpa_printf(MSG_DEBUG
, "P2P: Cancelled P2P group idle timeout");
410 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout
,
411 wpa_s
->parent
, NULL
) > 0) {
412 wpa_printf(MSG_DEBUG
, "P2P: Cancelled P2P group formation "
414 wpa_s
->p2p_in_provisioning
= 0;
417 if (removal_reason
!= P2P_GROUP_REMOVAL_SILENT
&& ssid
)
418 wpas_notify_p2p_group_removed(wpa_s
, ssid
, gtype
);
420 if (wpa_s
->p2p_group_interface
!= NOT_P2P_GROUP_INTERFACE
) {
421 struct wpa_global
*global
;
423 enum wpa_driver_if_type type
;
424 wpa_printf(MSG_DEBUG
, "P2P: Remove group interface %s",
426 global
= wpa_s
->global
;
427 ifname
= os_strdup(wpa_s
->ifname
);
428 type
= wpas_p2p_if_type(wpa_s
->p2p_group_interface
);
429 wpa_supplicant_remove_iface(wpa_s
->global
, wpa_s
, 0);
430 wpa_s
= global
->ifaces
;
432 wpa_drv_if_remove(wpa_s
, type
, ifname
);
437 wpa_printf(MSG_DEBUG
, "P2P: Remove temporary group network");
438 if (ssid
&& (ssid
->p2p_group
||
439 ssid
->mode
== WPAS_MODE_P2P_GROUP_FORMATION
||
440 (ssid
->key_mgmt
& WPA_KEY_MGMT_WPS
))) {
442 if (ssid
== wpa_s
->current_ssid
) {
443 wpa_sm_set_config(wpa_s
->wpa
, NULL
);
444 eapol_sm_notify_config(wpa_s
->eapol
, NULL
, NULL
);
445 wpa_s
->current_ssid
= NULL
;
448 * Networks objects created during any P2P activities are not
449 * exposed out as they might/will confuse certain non-P2P aware
450 * applications since these network objects won't behave like
453 * Likewise, we don't send out network removed signals for such
456 wpa_config_remove_network(wpa_s
->conf
, id
);
457 wpa_supplicant_clear_status(wpa_s
);
458 wpa_supplicant_cancel_sched_scan(wpa_s
);
459 wpa_s
->sta_scan_pending
= 0;
461 wpa_printf(MSG_DEBUG
, "P2P: Temporary group network not "
465 wpa_supplicant_ap_deinit(wpa_s
);
467 wpa_drv_deinit_p2p_cli(wpa_s
);
473 static int wpas_p2p_persistent_group(struct wpa_supplicant
*wpa_s
,
475 const u8
*ssid
, size_t ssid_len
)
483 if (wpa_s
->go_params
)
484 bssid
= wpa_s
->go_params
->peer_interface_addr
;
486 bssid
= wpa_s
->bssid
;
488 bss
= wpa_bss_get(wpa_s
, bssid
, ssid
, ssid_len
);
490 u8 iface_addr
[ETH_ALEN
];
491 if (p2p_get_interface_addr(wpa_s
->global
->p2p
, bssid
,
493 bss
= wpa_bss_get(wpa_s
, iface_addr
, ssid
, ssid_len
);
496 wpa_printf(MSG_DEBUG
, "P2P: Could not figure out whether "
497 "group is persistent - BSS " MACSTR
" not found",
502 p2p
= wpa_bss_get_vendor_ie_multi(bss
, P2P_IE_VENDOR_TYPE
);
504 wpa_printf(MSG_DEBUG
, "P2P: Could not figure out whether "
505 "group is persistent - BSS " MACSTR
506 " did not include P2P IE", MAC2STR(bssid
));
507 wpa_hexdump(MSG_DEBUG
, "P2P: Probe Response IEs",
508 (u8
*) (bss
+ 1), bss
->ie_len
);
509 wpa_hexdump(MSG_DEBUG
, "P2P: Beacon IEs",
510 ((u8
*) bss
+ 1) + bss
->ie_len
,
515 group_capab
= p2p_get_group_capab(p2p
);
516 addr
= p2p_get_go_dev_addr(p2p
);
517 wpa_printf(MSG_DEBUG
, "P2P: Checking whether group is persistent: "
518 "group_capab=0x%x", group_capab
);
520 os_memcpy(go_dev_addr
, addr
, ETH_ALEN
);
521 wpa_printf(MSG_DEBUG
, "P2P: GO Device Address " MACSTR
,
524 os_memset(go_dev_addr
, 0, ETH_ALEN
);
527 wpa_printf(MSG_DEBUG
, "P2P: BSS " MACSTR
" group_capab=0x%x "
528 "go_dev_addr=" MACSTR
,
529 MAC2STR(bssid
), group_capab
, MAC2STR(go_dev_addr
));
531 return group_capab
& P2P_GROUP_CAPAB_PERSISTENT_GROUP
;
535 static int wpas_p2p_store_persistent_group(struct wpa_supplicant
*wpa_s
,
536 struct wpa_ssid
*ssid
,
537 const u8
*go_dev_addr
)
542 wpa_printf(MSG_DEBUG
, "P2P: Storing credentials for a persistent "
543 "group (GO Dev Addr " MACSTR
")", MAC2STR(go_dev_addr
));
544 for (s
= wpa_s
->conf
->ssid
; s
; s
= s
->next
) {
545 if (s
->disabled
== 2 &&
546 os_memcmp(go_dev_addr
, s
->bssid
, ETH_ALEN
) == 0 &&
547 s
->ssid_len
== ssid
->ssid_len
&&
548 os_memcmp(ssid
->ssid
, s
->ssid
, ssid
->ssid_len
) == 0)
553 wpa_printf(MSG_DEBUG
, "P2P: Update existing persistent group "
555 if (ssid
->passphrase
&& !s
->passphrase
)
557 else if (ssid
->passphrase
&& s
->passphrase
&&
558 os_strcmp(ssid
->passphrase
, s
->passphrase
) != 0)
561 wpa_printf(MSG_DEBUG
, "P2P: Create a new persistent group "
564 s
= wpa_config_add_network(wpa_s
->conf
);
569 * Instead of network_added we emit persistent_group_added
570 * notification. Also to keep the defense checks in
571 * persistent_group obj registration method, we set the
572 * relevant flags in s to designate it as a persistent group.
575 s
->p2p_persistent_group
= 1;
576 wpas_notify_persistent_group_added(wpa_s
, s
);
577 wpa_config_set_network_defaults(s
);
581 s
->p2p_persistent_group
= 1;
584 os_memcpy(s
->bssid
, go_dev_addr
, ETH_ALEN
);
585 s
->mode
= ssid
->mode
;
586 s
->auth_alg
= WPA_AUTH_ALG_OPEN
;
587 s
->key_mgmt
= WPA_KEY_MGMT_PSK
;
588 s
->proto
= WPA_PROTO_RSN
;
589 s
->pairwise_cipher
= WPA_CIPHER_CCMP
;
591 if (ssid
->passphrase
) {
592 os_free(s
->passphrase
);
593 s
->passphrase
= os_strdup(ssid
->passphrase
);
597 os_memcpy(s
->psk
, ssid
->psk
, 32);
599 if (s
->passphrase
&& !s
->psk_set
)
600 wpa_config_update_psk(s
);
601 if (s
->ssid
== NULL
|| s
->ssid_len
< ssid
->ssid_len
) {
603 s
->ssid
= os_malloc(ssid
->ssid_len
);
606 s
->ssid_len
= ssid
->ssid_len
;
607 os_memcpy(s
->ssid
, ssid
->ssid
, s
->ssid_len
);
609 if (ssid
->mode
== WPAS_MODE_P2P_GO
&& wpa_s
->global
->add_psk
) {
610 dl_list_add(&s
->psk_list
, &wpa_s
->global
->add_psk
->list
);
611 wpa_s
->global
->add_psk
= NULL
;
615 #ifndef CONFIG_NO_CONFIG_WRITE
616 if (changed
&& wpa_s
->conf
->update_config
&&
617 wpa_config_write(wpa_s
->confname
, wpa_s
->conf
)) {
618 wpa_printf(MSG_DEBUG
, "P2P: Failed to update configuration");
620 #endif /* CONFIG_NO_CONFIG_WRITE */
626 static void wpas_p2p_add_persistent_group_client(struct wpa_supplicant
*wpa_s
,
629 struct wpa_ssid
*ssid
, *s
;
634 ssid
= wpa_s
->current_ssid
;
635 if (ssid
== NULL
|| ssid
->mode
!= WPAS_MODE_P2P_GO
||
636 !ssid
->p2p_persistent_group
)
639 for (s
= wpa_s
->parent
->conf
->ssid
; s
; s
= s
->next
) {
640 if (s
->disabled
!= 2 || s
->mode
!= WPAS_MODE_P2P_GO
)
643 if (s
->ssid_len
== ssid
->ssid_len
&&
644 os_memcmp(s
->ssid
, ssid
->ssid
, s
->ssid_len
) == 0)
651 for (i
= 0; s
->p2p_client_list
&& i
< s
->num_p2p_clients
; i
++) {
652 if (os_memcmp(s
->p2p_client_list
+ i
* ETH_ALEN
, addr
,
656 if (i
== s
->num_p2p_clients
- 1)
657 return; /* already the most recent entry */
659 /* move the entry to mark it most recent */
660 os_memmove(s
->p2p_client_list
+ i
* ETH_ALEN
,
661 s
->p2p_client_list
+ (i
+ 1) * ETH_ALEN
,
662 (s
->num_p2p_clients
- i
- 1) * ETH_ALEN
);
663 os_memcpy(s
->p2p_client_list
+
664 (s
->num_p2p_clients
- 1) * ETH_ALEN
, addr
, ETH_ALEN
);
669 if (!found
&& s
->num_p2p_clients
< P2P_MAX_STORED_CLIENTS
) {
670 n
= os_realloc_array(s
->p2p_client_list
,
671 s
->num_p2p_clients
+ 1, ETH_ALEN
);
674 os_memcpy(n
+ s
->num_p2p_clients
* ETH_ALEN
, addr
, ETH_ALEN
);
675 s
->p2p_client_list
= n
;
676 s
->num_p2p_clients
++;
678 /* Not enough room for an additional entry - drop the oldest
680 os_memmove(s
->p2p_client_list
,
681 s
->p2p_client_list
+ ETH_ALEN
,
682 (s
->num_p2p_clients
- 1) * ETH_ALEN
);
683 os_memcpy(s
->p2p_client_list
+
684 (s
->num_p2p_clients
- 1) * ETH_ALEN
,
688 #ifndef CONFIG_NO_CONFIG_WRITE
689 if (wpa_s
->parent
->conf
->update_config
&&
690 wpa_config_write(wpa_s
->parent
->confname
, wpa_s
->parent
->conf
))
691 wpa_printf(MSG_DEBUG
, "P2P: Failed to update configuration");
692 #endif /* CONFIG_NO_CONFIG_WRITE */
696 static void wpas_group_formation_completed(struct wpa_supplicant
*wpa_s
,
699 struct wpa_ssid
*ssid
;
700 const char *ssid_txt
;
703 u8 go_dev_addr
[ETH_ALEN
];
707 * This callback is likely called for the main interface. Update wpa_s
708 * to use the group interface if a new interface was created for the
711 if (wpa_s
->global
->p2p_group_formation
)
712 wpa_s
= wpa_s
->global
->p2p_group_formation
;
713 wpa_s
->global
->p2p_group_formation
= NULL
;
714 wpa_s
->p2p_in_provisioning
= 0;
717 wpa_msg_global(wpa_s
->parent
, MSG_INFO
,
718 P2P_EVENT_GROUP_FORMATION_FAILURE
);
719 wpas_p2p_group_delete(wpa_s
,
720 P2P_GROUP_REMOVAL_FORMATION_FAILED
);
724 wpa_msg_global(wpa_s
->parent
, MSG_INFO
,
725 P2P_EVENT_GROUP_FORMATION_SUCCESS
);
727 ssid
= wpa_s
->current_ssid
;
728 if (ssid
&& ssid
->mode
== WPAS_MODE_P2P_GROUP_FORMATION
) {
729 ssid
->mode
= WPAS_MODE_P2P_GO
;
730 p2p_group_notif_formation_done(wpa_s
->p2p_group
);
731 wpa_supplicant_ap_mac_addr_filter(wpa_s
, NULL
);
736 ssid_txt
= wpa_ssid_txt(ssid
->ssid
, ssid
->ssid_len
);
737 client
= ssid
->mode
== WPAS_MODE_INFRA
;
738 if (ssid
->mode
== WPAS_MODE_P2P_GO
) {
739 persistent
= ssid
->p2p_persistent_group
;
740 os_memcpy(go_dev_addr
, wpa_s
->global
->p2p_dev_addr
,
743 persistent
= wpas_p2p_persistent_group(wpa_s
,
749 client
= wpa_s
->p2p_group_interface
==
750 P2P_GROUP_INTERFACE_CLIENT
;
751 os_memset(go_dev_addr
, 0, ETH_ALEN
);
754 wpa_s
->show_group_started
= 0;
757 * Indicate event only after successfully completed 4-way
758 * handshake, i.e., when the interface is ready for data
761 wpa_s
->show_group_started
= 1;
762 } else if (ssid
&& ssid
->passphrase
== NULL
&& ssid
->psk_set
) {
764 wpa_snprintf_hex(psk
, sizeof(psk
), ssid
->psk
, 32);
765 wpa_msg_global(wpa_s
->parent
, MSG_INFO
, P2P_EVENT_GROUP_STARTED
766 "%s GO ssid=\"%s\" freq=%d psk=%s go_dev_addr="
768 wpa_s
->ifname
, ssid_txt
, ssid
->frequency
, psk
,
769 MAC2STR(go_dev_addr
),
770 persistent
? " [PERSISTENT]" : "");
771 wpas_p2p_cross_connect_setup(wpa_s
);
772 wpas_p2p_set_group_idle_timeout(wpa_s
);
774 wpa_msg_global(wpa_s
->parent
, MSG_INFO
, P2P_EVENT_GROUP_STARTED
775 "%s GO ssid=\"%s\" freq=%d passphrase=\"%s\" "
776 "go_dev_addr=" MACSTR
"%s",
777 wpa_s
->ifname
, ssid_txt
,
778 ssid
? ssid
->frequency
: 0,
779 ssid
&& ssid
->passphrase
? ssid
->passphrase
: "",
780 MAC2STR(go_dev_addr
),
781 persistent
? " [PERSISTENT]" : "");
782 wpas_p2p_cross_connect_setup(wpa_s
);
783 wpas_p2p_set_group_idle_timeout(wpa_s
);
787 network_id
= wpas_p2p_store_persistent_group(wpa_s
->parent
,
790 os_free(wpa_s
->global
->add_psk
);
791 wpa_s
->global
->add_psk
= NULL
;
793 if (network_id
< 0 && ssid
)
794 network_id
= ssid
->id
;
796 wpas_notify_p2p_group_started(wpa_s
, ssid
, network_id
, 0);
797 os_get_time(&wpa_s
->global
->p2p_go_wait_client
);
802 static void wpas_p2p_send_action_tx_status(struct wpa_supplicant
*wpa_s
,
804 const u8
*dst
, const u8
*src
,
806 const u8
*data
, size_t data_len
,
807 enum offchannel_send_action_result
810 enum p2p_send_action_result res
= P2P_SEND_ACTION_SUCCESS
;
812 if (wpa_s
->global
->p2p
== NULL
|| wpa_s
->global
->p2p_disabled
)
814 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
818 case OFFCHANNEL_SEND_ACTION_SUCCESS
:
819 res
= P2P_SEND_ACTION_SUCCESS
;
821 case OFFCHANNEL_SEND_ACTION_NO_ACK
:
822 res
= P2P_SEND_ACTION_NO_ACK
;
824 case OFFCHANNEL_SEND_ACTION_FAILED
:
825 res
= P2P_SEND_ACTION_FAILED
;
829 p2p_send_action_cb(wpa_s
->global
->p2p
, freq
, dst
, src
, bssid
, res
);
831 if (result
!= OFFCHANNEL_SEND_ACTION_SUCCESS
&&
832 wpa_s
->pending_pd_before_join
&&
833 (os_memcmp(dst
, wpa_s
->pending_join_dev_addr
, ETH_ALEN
) == 0 ||
834 os_memcmp(dst
, wpa_s
->pending_join_iface_addr
, ETH_ALEN
) == 0) &&
835 wpa_s
->p2p_fallback_to_go_neg
) {
836 wpa_s
->pending_pd_before_join
= 0;
837 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: No ACK for PD Req "
838 "during p2p_connect-auto");
839 wpas_p2p_fallback_to_go_neg(wpa_s
, 0);
845 static int wpas_send_action(void *ctx
, unsigned int freq
, const u8
*dst
,
846 const u8
*src
, const u8
*bssid
, const u8
*buf
,
847 size_t len
, unsigned int wait_time
)
849 struct wpa_supplicant
*wpa_s
= ctx
;
850 return offchannel_send_action(wpa_s
, freq
, dst
, src
, bssid
, buf
, len
,
852 wpas_p2p_send_action_tx_status
, 1);
856 static void wpas_send_action_done(void *ctx
)
858 struct wpa_supplicant
*wpa_s
= ctx
;
859 offchannel_send_action_done(wpa_s
);
863 static int wpas_copy_go_neg_results(struct wpa_supplicant
*wpa_s
,
864 struct p2p_go_neg_results
*params
)
866 if (wpa_s
->go_params
== NULL
) {
867 wpa_s
->go_params
= os_malloc(sizeof(*params
));
868 if (wpa_s
->go_params
== NULL
)
871 os_memcpy(wpa_s
->go_params
, params
, sizeof(*params
));
876 static void wpas_start_wps_enrollee(struct wpa_supplicant
*wpa_s
,
877 struct p2p_go_neg_results
*res
)
879 wpa_printf(MSG_DEBUG
, "P2P: Start WPS Enrollee for peer " MACSTR
,
880 MAC2STR(res
->peer_interface_addr
));
881 wpa_hexdump_ascii(MSG_DEBUG
, "P2P: Start WPS Enrollee for SSID",
882 res
->ssid
, res
->ssid_len
);
883 wpa_supplicant_ap_deinit(wpa_s
);
884 wpas_copy_go_neg_results(wpa_s
, res
);
885 if (res
->wps_method
== WPS_PBC
)
886 wpas_wps_start_pbc(wpa_s
, res
->peer_interface_addr
, 1);
888 u16 dev_pw_id
= DEV_PW_DEFAULT
;
889 if (wpa_s
->p2p_wps_method
== WPS_PIN_KEYPAD
)
890 dev_pw_id
= DEV_PW_REGISTRAR_SPECIFIED
;
891 wpas_wps_start_pin(wpa_s
, res
->peer_interface_addr
,
892 wpa_s
->p2p_pin
, 1, dev_pw_id
);
897 static void wpas_p2p_add_psk_list(struct wpa_supplicant
*wpa_s
,
898 struct wpa_ssid
*ssid
)
900 struct wpa_ssid
*persistent
;
901 struct psk_list_entry
*psk
;
902 struct hostapd_data
*hapd
;
904 if (!wpa_s
->ap_iface
)
907 persistent
= wpas_p2p_get_persistent(wpa_s
->parent
, NULL
, ssid
->ssid
,
909 if (persistent
== NULL
)
912 hapd
= wpa_s
->ap_iface
->bss
[0];
914 dl_list_for_each(psk
, &persistent
->psk_list
, struct psk_list_entry
,
916 struct hostapd_wpa_psk
*hpsk
;
918 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: Add persistent group PSK entry for "
920 MAC2STR(psk
->addr
), psk
->p2p
);
921 hpsk
= os_zalloc(sizeof(*hpsk
));
924 os_memcpy(hpsk
->psk
, psk
->psk
, PMK_LEN
);
926 os_memcpy(hpsk
->p2p_dev_addr
, psk
->addr
, ETH_ALEN
);
928 os_memcpy(hpsk
->addr
, psk
->addr
, ETH_ALEN
);
929 hpsk
->next
= hapd
->conf
->ssid
.wpa_psk
;
930 hapd
->conf
->ssid
.wpa_psk
= hpsk
;
935 static void p2p_go_configured(void *ctx
, void *data
)
937 struct wpa_supplicant
*wpa_s
= ctx
;
938 struct p2p_go_neg_results
*params
= data
;
939 struct wpa_ssid
*ssid
;
942 ssid
= wpa_s
->current_ssid
;
943 if (ssid
&& ssid
->mode
== WPAS_MODE_P2P_GO
) {
944 wpa_printf(MSG_DEBUG
, "P2P: Group setup without provisioning");
945 if (wpa_s
->global
->p2p_group_formation
== wpa_s
)
946 wpa_s
->global
->p2p_group_formation
= NULL
;
947 if (os_strlen(params
->passphrase
) > 0) {
948 wpa_msg_global(wpa_s
->parent
, MSG_INFO
,
949 P2P_EVENT_GROUP_STARTED
950 "%s GO ssid=\"%s\" freq=%d "
951 "passphrase=\"%s\" go_dev_addr=" MACSTR
953 wpa_ssid_txt(ssid
->ssid
, ssid
->ssid_len
),
954 ssid
->frequency
, params
->passphrase
,
955 MAC2STR(wpa_s
->global
->p2p_dev_addr
),
956 params
->persistent_group
?
957 " [PERSISTENT]" : "");
960 wpa_snprintf_hex(psk
, sizeof(psk
), params
->psk
,
961 sizeof(params
->psk
));
962 wpa_msg_global(wpa_s
->parent
, MSG_INFO
,
963 P2P_EVENT_GROUP_STARTED
964 "%s GO ssid=\"%s\" freq=%d psk=%s "
965 "go_dev_addr=" MACSTR
"%s",
967 wpa_ssid_txt(ssid
->ssid
, ssid
->ssid_len
),
968 ssid
->frequency
, psk
,
969 MAC2STR(wpa_s
->global
->p2p_dev_addr
),
970 params
->persistent_group
?
971 " [PERSISTENT]" : "");
974 if (params
->persistent_group
) {
975 network_id
= wpas_p2p_store_persistent_group(
977 wpa_s
->global
->p2p_dev_addr
);
978 wpas_p2p_add_psk_list(wpa_s
, ssid
);
981 network_id
= ssid
->id
;
982 wpas_notify_p2p_group_started(wpa_s
, ssid
, network_id
, 0);
983 wpas_p2p_cross_connect_setup(wpa_s
);
984 wpas_p2p_set_group_idle_timeout(wpa_s
);
988 wpa_printf(MSG_DEBUG
, "P2P: Setting up WPS for GO provisioning");
989 if (wpa_supplicant_ap_mac_addr_filter(wpa_s
,
990 params
->peer_interface_addr
)) {
991 wpa_printf(MSG_DEBUG
, "P2P: Failed to setup MAC address "
995 if (params
->wps_method
== WPS_PBC
)
996 wpa_supplicant_ap_wps_pbc(wpa_s
, params
->peer_interface_addr
,
997 params
->peer_device_addr
);
998 else if (wpa_s
->p2p_pin
[0])
999 wpa_supplicant_ap_wps_pin(wpa_s
, params
->peer_interface_addr
,
1000 wpa_s
->p2p_pin
, NULL
, 0, 0);
1001 os_free(wpa_s
->go_params
);
1002 wpa_s
->go_params
= NULL
;
1006 static void wpas_start_wps_go(struct wpa_supplicant
*wpa_s
,
1007 struct p2p_go_neg_results
*params
,
1008 int group_formation
)
1010 struct wpa_ssid
*ssid
;
1012 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: Starting GO");
1013 if (wpas_copy_go_neg_results(wpa_s
, params
) < 0) {
1014 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: Could not copy GO Negotiation "
1019 ssid
= wpa_config_add_network(wpa_s
->conf
);
1021 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: Could not add network for GO");
1025 wpa_s
->show_group_started
= 0;
1027 wpa_config_set_network_defaults(ssid
);
1028 ssid
->temporary
= 1;
1029 ssid
->p2p_group
= 1;
1030 ssid
->p2p_persistent_group
= params
->persistent_group
;
1031 ssid
->mode
= group_formation
? WPAS_MODE_P2P_GROUP_FORMATION
:
1033 ssid
->frequency
= params
->freq
;
1034 ssid
->ht40
= params
->ht40
;
1035 ssid
->ssid
= os_zalloc(params
->ssid_len
+ 1);
1037 os_memcpy(ssid
->ssid
, params
->ssid
, params
->ssid_len
);
1038 ssid
->ssid_len
= params
->ssid_len
;
1040 ssid
->auth_alg
= WPA_AUTH_ALG_OPEN
;
1041 ssid
->key_mgmt
= WPA_KEY_MGMT_PSK
;
1042 ssid
->proto
= WPA_PROTO_RSN
;
1043 ssid
->pairwise_cipher
= WPA_CIPHER_CCMP
;
1044 if (os_strlen(params
->passphrase
) > 0) {
1045 ssid
->passphrase
= os_strdup(params
->passphrase
);
1046 if (ssid
->passphrase
== NULL
) {
1047 wpa_msg_global(wpa_s
, MSG_ERROR
,
1048 "P2P: Failed to copy passphrase for GO");
1049 wpa_config_remove_network(wpa_s
->conf
, ssid
->id
);
1053 ssid
->passphrase
= NULL
;
1054 ssid
->psk_set
= params
->psk_set
;
1056 os_memcpy(ssid
->psk
, params
->psk
, sizeof(ssid
->psk
));
1057 else if (ssid
->passphrase
)
1058 wpa_config_update_psk(ssid
);
1059 ssid
->ap_max_inactivity
= wpa_s
->parent
->conf
->p2p_go_max_inactivity
;
1061 wpa_s
->ap_configured_cb
= p2p_go_configured
;
1062 wpa_s
->ap_configured_cb_ctx
= wpa_s
;
1063 wpa_s
->ap_configured_cb_data
= wpa_s
->go_params
;
1064 wpa_s
->connect_without_scan
= ssid
;
1065 wpa_s
->reassociate
= 1;
1066 wpa_s
->disconnected
= 0;
1067 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: Request scan (that will be skipped) to "
1069 wpa_supplicant_req_scan(wpa_s
, 0, 0);
1073 static void wpas_p2p_clone_config(struct wpa_supplicant
*dst
,
1074 const struct wpa_supplicant
*src
)
1076 struct wpa_config
*d
;
1077 const struct wpa_config
*s
;
1082 #define C(n) if (s->n) d->n = os_strdup(s->n)
1091 os_memcpy(d
->device_type
, s
->device_type
, WPS_DEV_TYPE_LEN
);
1092 os_memcpy(d
->sec_device_type
, s
->sec_device_type
,
1093 sizeof(d
->sec_device_type
));
1094 d
->num_sec_device_types
= s
->num_sec_device_types
;
1096 d
->p2p_group_idle
= s
->p2p_group_idle
;
1097 d
->p2p_intra_bss
= s
->p2p_intra_bss
;
1098 d
->persistent_reconnect
= s
->persistent_reconnect
;
1099 d
->max_num_sta
= s
->max_num_sta
;
1100 d
->pbc_in_m1
= s
->pbc_in_m1
;
1101 d
->ignore_old_scan_res
= s
->ignore_old_scan_res
;
1102 d
->beacon_int
= s
->beacon_int
;
1103 d
->disassoc_low_ack
= s
->disassoc_low_ack
;
1107 static void wpas_p2p_get_group_ifname(struct wpa_supplicant
*wpa_s
,
1108 char *ifname
, size_t len
)
1110 char *ifname_ptr
= wpa_s
->ifname
;
1112 if (os_strncmp(wpa_s
->ifname
, P2P_MGMT_DEVICE_PREFIX
,
1113 os_strlen(P2P_MGMT_DEVICE_PREFIX
)) == 0) {
1114 ifname_ptr
= os_strrchr(wpa_s
->ifname
, '-') + 1;
1117 os_snprintf(ifname
, len
, "p2p-%s-%d", ifname_ptr
, wpa_s
->p2p_group_idx
);
1118 if (os_strlen(ifname
) >= IFNAMSIZ
&&
1119 os_strlen(wpa_s
->ifname
) < IFNAMSIZ
) {
1120 /* Try to avoid going over the IFNAMSIZ length limit */
1121 os_snprintf(ifname
, sizeof(ifname
), "p2p-%d",
1122 wpa_s
->p2p_group_idx
);
1127 static int wpas_p2p_add_group_interface(struct wpa_supplicant
*wpa_s
,
1128 enum wpa_driver_if_type type
)
1130 char ifname
[120], force_ifname
[120];
1132 if (wpa_s
->pending_interface_name
[0]) {
1133 wpa_printf(MSG_DEBUG
, "P2P: Pending virtual interface exists "
1134 "- skip creation of a new one");
1135 if (is_zero_ether_addr(wpa_s
->pending_interface_addr
)) {
1136 wpa_printf(MSG_DEBUG
, "P2P: Pending virtual address "
1137 "unknown?! ifname='%s'",
1138 wpa_s
->pending_interface_name
);
1144 wpas_p2p_get_group_ifname(wpa_s
, ifname
, sizeof(ifname
));
1145 force_ifname
[0] = '\0';
1147 wpa_printf(MSG_DEBUG
, "P2P: Create a new interface %s for the group",
1149 wpa_s
->p2p_group_idx
++;
1151 wpa_s
->pending_interface_type
= type
;
1152 if (wpa_drv_if_add(wpa_s
, type
, ifname
, NULL
, NULL
, force_ifname
,
1153 wpa_s
->pending_interface_addr
, NULL
) < 0) {
1154 wpa_printf(MSG_ERROR
, "P2P: Failed to create new group "
1159 if (force_ifname
[0]) {
1160 wpa_printf(MSG_DEBUG
, "P2P: Driver forced interface name %s",
1162 os_strlcpy(wpa_s
->pending_interface_name
, force_ifname
,
1163 sizeof(wpa_s
->pending_interface_name
));
1165 os_strlcpy(wpa_s
->pending_interface_name
, ifname
,
1166 sizeof(wpa_s
->pending_interface_name
));
1167 wpa_printf(MSG_DEBUG
, "P2P: Created pending virtual interface %s addr "
1168 MACSTR
, wpa_s
->pending_interface_name
,
1169 MAC2STR(wpa_s
->pending_interface_addr
));
1175 static void wpas_p2p_remove_pending_group_interface(
1176 struct wpa_supplicant
*wpa_s
)
1178 if (!wpa_s
->pending_interface_name
[0] ||
1179 is_zero_ether_addr(wpa_s
->pending_interface_addr
))
1180 return; /* No pending virtual interface */
1182 wpa_printf(MSG_DEBUG
, "P2P: Removing pending group interface %s",
1183 wpa_s
->pending_interface_name
);
1184 wpa_drv_if_remove(wpa_s
, wpa_s
->pending_interface_type
,
1185 wpa_s
->pending_interface_name
);
1186 os_memset(wpa_s
->pending_interface_addr
, 0, ETH_ALEN
);
1187 wpa_s
->pending_interface_name
[0] = '\0';
1191 static struct wpa_supplicant
*
1192 wpas_p2p_init_group_interface(struct wpa_supplicant
*wpa_s
, int go
)
1194 struct wpa_interface iface
;
1195 struct wpa_supplicant
*group_wpa_s
;
1197 if (!wpa_s
->pending_interface_name
[0]) {
1198 wpa_printf(MSG_ERROR
, "P2P: No pending group interface");
1199 if (!wpas_p2p_create_iface(wpa_s
))
1202 * Something has forced us to remove the pending interface; try
1203 * to create a new one and hope for the best that we will get
1204 * the same local address.
1206 if (wpas_p2p_add_group_interface(wpa_s
, go
? WPA_IF_P2P_GO
:
1207 WPA_IF_P2P_CLIENT
) < 0)
1211 os_memset(&iface
, 0, sizeof(iface
));
1212 iface
.ifname
= wpa_s
->pending_interface_name
;
1213 iface
.driver
= wpa_s
->driver
->name
;
1214 if (wpa_s
->conf
->ctrl_interface
== NULL
&&
1215 wpa_s
->parent
!= wpa_s
&&
1217 (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE
))
1218 iface
.ctrl_interface
= wpa_s
->parent
->conf
->ctrl_interface
;
1220 iface
.ctrl_interface
= wpa_s
->conf
->ctrl_interface
;
1221 iface
.driver_param
= wpa_s
->conf
->driver_param
;
1222 group_wpa_s
= wpa_supplicant_add_iface(wpa_s
->global
, &iface
);
1223 if (group_wpa_s
== NULL
) {
1224 wpa_printf(MSG_ERROR
, "P2P: Failed to create new "
1225 "wpa_supplicant interface");
1228 wpa_s
->pending_interface_name
[0] = '\0';
1229 group_wpa_s
->parent
= wpa_s
;
1230 group_wpa_s
->p2p_group_interface
= go
? P2P_GROUP_INTERFACE_GO
:
1231 P2P_GROUP_INTERFACE_CLIENT
;
1232 wpa_s
->global
->p2p_group_formation
= group_wpa_s
;
1234 wpas_p2p_clone_config(group_wpa_s
, wpa_s
);
1240 static void wpas_p2p_group_formation_timeout(void *eloop_ctx
,
1243 struct wpa_supplicant
*wpa_s
= eloop_ctx
;
1244 wpa_printf(MSG_DEBUG
, "P2P: Group Formation timed out");
1245 wpas_p2p_group_formation_failed(wpa_s
);
1249 void wpas_p2p_group_formation_failed(struct wpa_supplicant
*wpa_s
)
1251 eloop_cancel_timeout(wpas_p2p_group_formation_timeout
,
1252 wpa_s
->parent
, NULL
);
1253 if (wpa_s
->global
->p2p
)
1254 p2p_group_formation_failed(wpa_s
->global
->p2p
);
1255 else if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
1256 wpa_drv_p2p_group_formation_failed(wpa_s
);
1257 wpas_group_formation_completed(wpa_s
, 0);
1261 void wpas_p2p_ap_setup_failed(struct wpa_supplicant
*wpa_s
)
1263 if (wpa_s
->global
->p2p_group_formation
!= wpa_s
)
1265 /* Speed up group formation timeout since this cannot succeed */
1266 eloop_cancel_timeout(wpas_p2p_group_formation_timeout
,
1267 wpa_s
->parent
, NULL
);
1268 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout
,
1269 wpa_s
->parent
, NULL
);
1273 void wpas_go_neg_completed(void *ctx
, struct p2p_go_neg_results
*res
)
1275 struct wpa_supplicant
*wpa_s
= ctx
;
1277 if (wpa_s
->off_channel_freq
|| wpa_s
->roc_waiting_drv_freq
) {
1278 wpa_drv_cancel_remain_on_channel(wpa_s
);
1279 wpa_s
->off_channel_freq
= 0;
1280 wpa_s
->roc_waiting_drv_freq
= 0;
1284 wpa_msg_global(wpa_s
, MSG_INFO
,
1285 P2P_EVENT_GO_NEG_FAILURE
"status=%d",
1287 wpas_notify_p2p_go_neg_completed(wpa_s
, res
);
1288 wpas_p2p_remove_pending_group_interface(wpa_s
);
1292 if (wpa_s
->p2p_go_ht40
)
1295 wpa_msg_global(wpa_s
, MSG_INFO
, P2P_EVENT_GO_NEG_SUCCESS
"role=%s "
1296 "freq=%d ht40=%d peer_dev=" MACSTR
" peer_iface=" MACSTR
1298 res
->role_go
? "GO" : "client", res
->freq
, res
->ht40
,
1299 MAC2STR(res
->peer_device_addr
),
1300 MAC2STR(res
->peer_interface_addr
),
1301 p2p_wps_method_text(res
->wps_method
));
1302 wpas_notify_p2p_go_neg_completed(wpa_s
, res
);
1304 if (res
->role_go
&& wpa_s
->p2p_persistent_id
>= 0) {
1305 struct wpa_ssid
*ssid
;
1306 ssid
= wpa_config_get_network(wpa_s
->conf
,
1307 wpa_s
->p2p_persistent_id
);
1308 if (ssid
&& ssid
->disabled
== 2 &&
1309 ssid
->mode
== WPAS_MODE_P2P_GO
&& ssid
->passphrase
) {
1310 size_t len
= os_strlen(ssid
->passphrase
);
1311 wpa_printf(MSG_DEBUG
, "P2P: Override passphrase based "
1312 "on requested persistent group");
1313 os_memcpy(res
->passphrase
, ssid
->passphrase
, len
);
1314 res
->passphrase
[len
] = '\0';
1318 if (wpa_s
->create_p2p_iface
) {
1319 struct wpa_supplicant
*group_wpa_s
=
1320 wpas_p2p_init_group_interface(wpa_s
, res
->role_go
);
1321 if (group_wpa_s
== NULL
) {
1322 wpas_p2p_remove_pending_group_interface(wpa_s
);
1325 if (group_wpa_s
!= wpa_s
) {
1326 os_memcpy(group_wpa_s
->p2p_pin
, wpa_s
->p2p_pin
,
1327 sizeof(group_wpa_s
->p2p_pin
));
1328 group_wpa_s
->p2p_wps_method
= wpa_s
->p2p_wps_method
;
1330 os_memset(wpa_s
->pending_interface_addr
, 0, ETH_ALEN
);
1331 wpa_s
->pending_interface_name
[0] = '\0';
1332 group_wpa_s
->p2p_in_provisioning
= 1;
1335 wpas_start_wps_go(group_wpa_s
, res
, 1);
1337 wpas_start_wps_enrollee(group_wpa_s
, res
);
1339 wpa_s
->p2p_in_provisioning
= 1;
1340 wpa_s
->global
->p2p_group_formation
= wpa_s
;
1343 wpas_start_wps_go(wpa_s
, res
, 1);
1345 wpas_start_wps_enrollee(ctx
, res
);
1348 wpa_s
->p2p_long_listen
= 0;
1349 eloop_cancel_timeout(wpas_p2p_long_listen_timeout
, wpa_s
, NULL
);
1351 eloop_cancel_timeout(wpas_p2p_group_formation_timeout
, wpa_s
, NULL
);
1352 eloop_register_timeout(15 + res
->peer_config_timeout
/ 100,
1353 (res
->peer_config_timeout
% 100) * 10000,
1354 wpas_p2p_group_formation_timeout
, wpa_s
, NULL
);
1358 void wpas_go_neg_req_rx(void *ctx
, const u8
*src
, u16 dev_passwd_id
)
1360 struct wpa_supplicant
*wpa_s
= ctx
;
1361 wpa_msg_global(wpa_s
, MSG_INFO
, P2P_EVENT_GO_NEG_REQUEST MACSTR
1362 " dev_passwd_id=%u", MAC2STR(src
), dev_passwd_id
);
1364 wpas_notify_p2p_go_neg_req(wpa_s
, src
, dev_passwd_id
);
1368 void wpas_dev_found(void *ctx
, const u8
*addr
,
1369 const struct p2p_peer_info
*info
,
1372 #ifndef CONFIG_NO_STDOUT_DEBUG
1373 struct wpa_supplicant
*wpa_s
= ctx
;
1374 char devtype
[WPS_DEV_TYPE_BUFSIZE
];
1376 wpa_msg_global(wpa_s
, MSG_INFO
, P2P_EVENT_DEVICE_FOUND MACSTR
1377 " p2p_dev_addr=" MACSTR
1378 " pri_dev_type=%s name='%s' config_methods=0x%x "
1379 "dev_capab=0x%x group_capab=0x%x",
1380 MAC2STR(addr
), MAC2STR(info
->p2p_device_addr
),
1381 wps_dev_type_bin2str(info
->pri_dev_type
, devtype
,
1383 info
->device_name
, info
->config_methods
,
1384 info
->dev_capab
, info
->group_capab
);
1385 #endif /* CONFIG_NO_STDOUT_DEBUG */
1387 wpas_notify_p2p_device_found(ctx
, info
->p2p_device_addr
, new_device
);
1391 static void wpas_dev_lost(void *ctx
, const u8
*dev_addr
)
1393 struct wpa_supplicant
*wpa_s
= ctx
;
1395 wpa_msg_global(wpa_s
, MSG_INFO
, P2P_EVENT_DEVICE_LOST
1396 "p2p_dev_addr=" MACSTR
, MAC2STR(dev_addr
));
1398 wpas_notify_p2p_device_lost(wpa_s
, dev_addr
);
1402 static void wpas_find_stopped(void *ctx
)
1404 struct wpa_supplicant
*wpa_s
= ctx
;
1405 wpa_msg_global(wpa_s
, MSG_INFO
, P2P_EVENT_FIND_STOPPED
);
1409 static int wpas_start_listen(void *ctx
, unsigned int freq
,
1410 unsigned int duration
,
1411 const struct wpabuf
*probe_resp_ie
)
1413 struct wpa_supplicant
*wpa_s
= ctx
;
1415 wpa_drv_set_ap_wps_ie(wpa_s
, NULL
, probe_resp_ie
, NULL
);
1417 if (wpa_drv_probe_req_report(wpa_s
, 1) < 0) {
1418 wpa_printf(MSG_DEBUG
, "P2P: Failed to request the driver to "
1419 "report received Probe Request frames");
1423 wpa_s
->pending_listen_freq
= freq
;
1424 wpa_s
->pending_listen_duration
= duration
;
1426 if (wpa_drv_remain_on_channel(wpa_s
, freq
, duration
) < 0) {
1427 wpa_printf(MSG_DEBUG
, "P2P: Failed to request the driver "
1428 "to remain on channel (%u MHz) for Listen "
1430 wpa_s
->pending_listen_freq
= 0;
1433 wpa_s
->off_channel_freq
= 0;
1434 wpa_s
->roc_waiting_drv_freq
= freq
;
1440 static void wpas_stop_listen(void *ctx
)
1442 struct wpa_supplicant
*wpa_s
= ctx
;
1443 if (wpa_s
->off_channel_freq
|| wpa_s
->roc_waiting_drv_freq
) {
1444 wpa_drv_cancel_remain_on_channel(wpa_s
);
1445 wpa_s
->off_channel_freq
= 0;
1446 wpa_s
->roc_waiting_drv_freq
= 0;
1448 wpa_drv_set_ap_wps_ie(wpa_s
, NULL
, NULL
, NULL
);
1449 wpa_drv_probe_req_report(wpa_s
, 0);
1453 static int wpas_send_probe_resp(void *ctx
, const struct wpabuf
*buf
)
1455 struct wpa_supplicant
*wpa_s
= ctx
;
1456 return wpa_drv_send_mlme(wpa_s
, wpabuf_head(buf
), wpabuf_len(buf
), 1);
1461 * DNS Header section is used only to calculate compression pointers, so the
1462 * contents of this data does not matter, but the length needs to be reserved
1463 * in the virtual packet.
1465 #define DNS_HEADER_LEN 12
1468 * 27-octet in-memory packet from P2P specification containing two implied
1469 * queries for _tcp.lcoal. PTR IN and _udp.local. PTR IN
1471 #define P2P_SD_IN_MEMORY_LEN 27
1473 static int p2p_sd_dns_uncompress_label(char **upos
, char *uend
, u8
*start
,
1474 u8
**spos
, const u8
*end
)
1476 while (*spos
< end
) {
1477 u8 val
= ((*spos
)[0] & 0xc0) >> 6;
1480 if (val
== 1 || val
== 2) {
1481 /* These are reserved values in RFC 1035 */
1482 wpa_printf(MSG_DEBUG
, "P2P: Invalid domain name "
1483 "sequence starting with 0x%x", val
);
1492 if (*spos
+ 2 > end
) {
1493 wpa_printf(MSG_DEBUG
, "P2P: No room for full "
1494 "DNS offset field");
1498 offset
= (((*spos
)[0] & 0x3f) << 8) | (*spos
)[1];
1499 if (offset
>= *spos
- start
) {
1500 wpa_printf(MSG_DEBUG
, "P2P: Invalid DNS "
1501 "pointer offset %u", offset
);
1506 spos_tmp
= start
+ offset
;
1507 return p2p_sd_dns_uncompress_label(upos
, uend
, start
,
1513 len
= (*spos
)[0] & 0x3f;
1518 if (*spos
+ len
> end
) {
1519 wpa_printf(MSG_DEBUG
, "P2P: Invalid domain name "
1520 "sequence - no room for label with length "
1525 if (*upos
+ len
+ 2 > uend
)
1528 os_memcpy(*upos
, *spos
, len
);
1540 /* Uncompress domain names per RFC 1035 using the P2P SD in-memory packet.
1541 * Returns -1 on parsing error (invalid input sequence), -2 if output buffer is
1542 * not large enough */
1543 static int p2p_sd_dns_uncompress(char *buf
, size_t buf_len
, const u8
*msg
,
1544 size_t msg_len
, size_t offset
)
1546 /* 27-octet in-memory packet from P2P specification */
1547 const char *prefix
= "\x04_tcp\x05local\x00\x00\x0C\x00\x01"
1548 "\x04_udp\xC0\x11\x00\x0C\x00\x01";
1549 u8
*tmp
, *end
, *spos
;
1555 if (offset
> msg_len
)
1558 tmp
= os_malloc(DNS_HEADER_LEN
+ P2P_SD_IN_MEMORY_LEN
+ msg_len
);
1561 spos
= tmp
+ DNS_HEADER_LEN
+ P2P_SD_IN_MEMORY_LEN
;
1562 end
= spos
+ msg_len
;
1565 os_memset(tmp
, 0, DNS_HEADER_LEN
);
1566 os_memcpy(tmp
+ DNS_HEADER_LEN
, prefix
, P2P_SD_IN_MEMORY_LEN
);
1567 os_memcpy(tmp
+ DNS_HEADER_LEN
+ P2P_SD_IN_MEMORY_LEN
, msg
, msg_len
);
1570 uend
= buf
+ buf_len
;
1572 ret
= p2p_sd_dns_uncompress_label(&upos
, uend
, tmp
, &spos
, end
);
1581 } else if (upos
[-1] == '.')
1589 static struct p2p_srv_bonjour
*
1590 wpas_p2p_service_get_bonjour(struct wpa_supplicant
*wpa_s
,
1591 const struct wpabuf
*query
)
1593 struct p2p_srv_bonjour
*bsrv
;
1596 len
= wpabuf_len(query
);
1597 dl_list_for_each(bsrv
, &wpa_s
->global
->p2p_srv_bonjour
,
1598 struct p2p_srv_bonjour
, list
) {
1599 if (len
== wpabuf_len(bsrv
->query
) &&
1600 os_memcmp(wpabuf_head(query
), wpabuf_head(bsrv
->query
),
1608 static struct p2p_srv_upnp
*
1609 wpas_p2p_service_get_upnp(struct wpa_supplicant
*wpa_s
, u8 version
,
1610 const char *service
)
1612 struct p2p_srv_upnp
*usrv
;
1614 dl_list_for_each(usrv
, &wpa_s
->global
->p2p_srv_upnp
,
1615 struct p2p_srv_upnp
, list
) {
1616 if (version
== usrv
->version
&&
1617 os_strcmp(service
, usrv
->service
) == 0)
1624 static void wpas_sd_add_proto_not_avail(struct wpabuf
*resp
, u8 srv_proto
,
1629 if (wpabuf_tailroom(resp
) < 5)
1632 /* Length (to be filled) */
1633 len_pos
= wpabuf_put(resp
, 2);
1634 wpabuf_put_u8(resp
, srv_proto
);
1635 wpabuf_put_u8(resp
, srv_trans_id
);
1637 wpabuf_put_u8(resp
, P2P_SD_PROTO_NOT_AVAILABLE
);
1638 /* Response Data: empty */
1639 WPA_PUT_LE16(len_pos
, (u8
*) wpabuf_put(resp
, 0) - len_pos
- 2);
1643 static void wpas_sd_all_bonjour(struct wpa_supplicant
*wpa_s
,
1644 struct wpabuf
*resp
, u8 srv_trans_id
)
1646 struct p2p_srv_bonjour
*bsrv
;
1649 wpa_printf(MSG_DEBUG
, "P2P: SD Request for all Bonjour services");
1651 if (dl_list_empty(&wpa_s
->global
->p2p_srv_bonjour
)) {
1652 wpa_printf(MSG_DEBUG
, "P2P: Bonjour protocol not available");
1656 dl_list_for_each(bsrv
, &wpa_s
->global
->p2p_srv_bonjour
,
1657 struct p2p_srv_bonjour
, list
) {
1658 if (wpabuf_tailroom(resp
) <
1659 5 + wpabuf_len(bsrv
->query
) + wpabuf_len(bsrv
->resp
))
1661 /* Length (to be filled) */
1662 len_pos
= wpabuf_put(resp
, 2);
1663 wpabuf_put_u8(resp
, P2P_SERV_BONJOUR
);
1664 wpabuf_put_u8(resp
, srv_trans_id
);
1666 wpabuf_put_u8(resp
, P2P_SD_SUCCESS
);
1667 wpa_hexdump_ascii(MSG_DEBUG
, "P2P: Matching Bonjour service",
1668 wpabuf_head(bsrv
->resp
),
1669 wpabuf_len(bsrv
->resp
));
1671 wpabuf_put_buf(resp
, bsrv
->query
); /* Key */
1672 wpabuf_put_buf(resp
, bsrv
->resp
); /* Value */
1673 WPA_PUT_LE16(len_pos
, (u8
*) wpabuf_put(resp
, 0) - len_pos
-
1679 static int match_bonjour_query(struct p2p_srv_bonjour
*bsrv
, const u8
*query
,
1682 char str_rx
[256], str_srv
[256];
1684 if (query_len
< 3 || wpabuf_len(bsrv
->query
) < 3)
1685 return 0; /* Too short to include DNS Type and Version */
1686 if (os_memcmp(query
+ query_len
- 3,
1687 wpabuf_head_u8(bsrv
->query
) + wpabuf_len(bsrv
->query
) - 3,
1689 return 0; /* Mismatch in DNS Type or Version */
1690 if (query_len
== wpabuf_len(bsrv
->query
) &&
1691 os_memcmp(query
, wpabuf_head(bsrv
->query
), query_len
- 3) == 0)
1692 return 1; /* Binary match */
1694 if (p2p_sd_dns_uncompress(str_rx
, sizeof(str_rx
), query
, query_len
- 3,
1696 return 0; /* Failed to uncompress query */
1697 if (p2p_sd_dns_uncompress(str_srv
, sizeof(str_srv
),
1698 wpabuf_head(bsrv
->query
),
1699 wpabuf_len(bsrv
->query
) - 3, 0))
1700 return 0; /* Failed to uncompress service */
1702 return os_strcmp(str_rx
, str_srv
) == 0;
1706 static void wpas_sd_req_bonjour(struct wpa_supplicant
*wpa_s
,
1707 struct wpabuf
*resp
, u8 srv_trans_id
,
1708 const u8
*query
, size_t query_len
)
1710 struct p2p_srv_bonjour
*bsrv
;
1714 wpa_hexdump_ascii(MSG_DEBUG
, "P2P: SD Request for Bonjour",
1716 if (dl_list_empty(&wpa_s
->global
->p2p_srv_bonjour
)) {
1717 wpa_printf(MSG_DEBUG
, "P2P: Bonjour protocol not available");
1718 wpas_sd_add_proto_not_avail(resp
, P2P_SERV_BONJOUR
,
1723 if (query_len
== 0) {
1724 wpas_sd_all_bonjour(wpa_s
, resp
, srv_trans_id
);
1728 dl_list_for_each(bsrv
, &wpa_s
->global
->p2p_srv_bonjour
,
1729 struct p2p_srv_bonjour
, list
) {
1730 if (!match_bonjour_query(bsrv
, query
, query_len
))
1733 if (wpabuf_tailroom(resp
) <
1734 5 + query_len
+ wpabuf_len(bsrv
->resp
))
1739 /* Length (to be filled) */
1740 len_pos
= wpabuf_put(resp
, 2);
1741 wpabuf_put_u8(resp
, P2P_SERV_BONJOUR
);
1742 wpabuf_put_u8(resp
, srv_trans_id
);
1745 wpabuf_put_u8(resp
, P2P_SD_SUCCESS
);
1746 wpa_hexdump_ascii(MSG_DEBUG
, "P2P: Matching Bonjour service",
1747 wpabuf_head(bsrv
->resp
),
1748 wpabuf_len(bsrv
->resp
));
1751 wpabuf_put_data(resp
, query
, query_len
); /* Key */
1752 wpabuf_put_buf(resp
, bsrv
->resp
); /* Value */
1754 WPA_PUT_LE16(len_pos
, (u8
*) wpabuf_put(resp
, 0) - len_pos
- 2);
1758 wpa_printf(MSG_DEBUG
, "P2P: Requested Bonjour service not "
1760 if (wpabuf_tailroom(resp
) < 5)
1763 /* Length (to be filled) */
1764 len_pos
= wpabuf_put(resp
, 2);
1765 wpabuf_put_u8(resp
, P2P_SERV_BONJOUR
);
1766 wpabuf_put_u8(resp
, srv_trans_id
);
1769 wpabuf_put_u8(resp
, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE
);
1770 /* Response Data: empty */
1771 WPA_PUT_LE16(len_pos
, (u8
*) wpabuf_put(resp
, 0) - len_pos
-
1777 static void wpas_sd_all_upnp(struct wpa_supplicant
*wpa_s
,
1778 struct wpabuf
*resp
, u8 srv_trans_id
)
1780 struct p2p_srv_upnp
*usrv
;
1783 wpa_printf(MSG_DEBUG
, "P2P: SD Request for all UPnP services");
1785 if (dl_list_empty(&wpa_s
->global
->p2p_srv_upnp
)) {
1786 wpa_printf(MSG_DEBUG
, "P2P: UPnP protocol not available");
1790 dl_list_for_each(usrv
, &wpa_s
->global
->p2p_srv_upnp
,
1791 struct p2p_srv_upnp
, list
) {
1792 if (wpabuf_tailroom(resp
) < 5 + 1 + os_strlen(usrv
->service
))
1795 /* Length (to be filled) */
1796 len_pos
= wpabuf_put(resp
, 2);
1797 wpabuf_put_u8(resp
, P2P_SERV_UPNP
);
1798 wpabuf_put_u8(resp
, srv_trans_id
);
1801 wpabuf_put_u8(resp
, P2P_SD_SUCCESS
);
1803 wpabuf_put_u8(resp
, usrv
->version
);
1804 wpa_printf(MSG_DEBUG
, "P2P: Matching UPnP Service: %s",
1806 wpabuf_put_str(resp
, usrv
->service
);
1807 WPA_PUT_LE16(len_pos
, (u8
*) wpabuf_put(resp
, 0) - len_pos
-
1813 static void wpas_sd_req_upnp(struct wpa_supplicant
*wpa_s
,
1814 struct wpabuf
*resp
, u8 srv_trans_id
,
1815 const u8
*query
, size_t query_len
)
1817 struct p2p_srv_upnp
*usrv
;
1823 wpa_hexdump_ascii(MSG_DEBUG
, "P2P: SD Request for UPnP",
1826 if (dl_list_empty(&wpa_s
->global
->p2p_srv_upnp
)) {
1827 wpa_printf(MSG_DEBUG
, "P2P: UPnP protocol not available");
1828 wpas_sd_add_proto_not_avail(resp
, P2P_SERV_UPNP
,
1833 if (query_len
== 0) {
1834 wpas_sd_all_upnp(wpa_s
, resp
, srv_trans_id
);
1838 if (wpabuf_tailroom(resp
) < 5)
1841 /* Length (to be filled) */
1842 len_pos
= wpabuf_put(resp
, 2);
1843 wpabuf_put_u8(resp
, P2P_SERV_UPNP
);
1844 wpabuf_put_u8(resp
, srv_trans_id
);
1847 str
= os_malloc(query_len
);
1850 os_memcpy(str
, query
+ 1, query_len
- 1);
1851 str
[query_len
- 1] = '\0';
1853 dl_list_for_each(usrv
, &wpa_s
->global
->p2p_srv_upnp
,
1854 struct p2p_srv_upnp
, list
) {
1855 if (version
!= usrv
->version
)
1858 if (os_strcmp(str
, "ssdp:all") != 0 &&
1859 os_strstr(usrv
->service
, str
) == NULL
)
1862 if (wpabuf_tailroom(resp
) < 2)
1866 wpabuf_put_u8(resp
, P2P_SD_SUCCESS
);
1868 wpabuf_put_u8(resp
, version
);
1870 wpabuf_put_u8(resp
, ',');
1874 wpa_printf(MSG_DEBUG
, "P2P: Matching UPnP Service: %s",
1876 if (wpabuf_tailroom(resp
) < os_strlen(usrv
->service
))
1878 wpabuf_put_str(resp
, usrv
->service
);
1883 wpa_printf(MSG_DEBUG
, "P2P: Requested UPnP service not "
1886 wpabuf_put_u8(resp
, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE
);
1887 /* Response Data: empty */
1890 WPA_PUT_LE16(len_pos
, (u8
*) wpabuf_put(resp
, 0) - len_pos
- 2);
1894 #ifdef CONFIG_WIFI_DISPLAY
1895 static void wpas_sd_req_wfd(struct wpa_supplicant
*wpa_s
,
1896 struct wpabuf
*resp
, u8 srv_trans_id
,
1897 const u8
*query
, size_t query_len
)
1903 wpa_hexdump(MSG_DEBUG
, "P2P: SD Request for WFD", query
, query_len
);
1905 if (!wpa_s
->global
->wifi_display
) {
1906 wpa_printf(MSG_DEBUG
, "P2P: WFD protocol not available");
1907 wpas_sd_add_proto_not_avail(resp
, P2P_SERV_WIFI_DISPLAY
,
1912 if (query_len
< 1) {
1913 wpa_printf(MSG_DEBUG
, "P2P: Missing WFD Requested Device "
1918 if (wpabuf_tailroom(resp
) < 5)
1923 wpa_printf(MSG_DEBUG
, "P2P: WSD for device role 0x%x", role
);
1925 /* TODO: role specific handling */
1927 /* Length (to be filled) */
1928 len_pos
= wpabuf_put(resp
, 2);
1929 wpabuf_put_u8(resp
, P2P_SERV_WIFI_DISPLAY
);
1930 wpabuf_put_u8(resp
, srv_trans_id
);
1931 wpabuf_put_u8(resp
, P2P_SD_SUCCESS
); /* Status Code */
1933 while (pos
< query
+ query_len
) {
1934 if (*pos
< MAX_WFD_SUBELEMS
&&
1935 wpa_s
->global
->wfd_subelem
[*pos
] &&
1936 wpabuf_tailroom(resp
) >=
1937 wpabuf_len(wpa_s
->global
->wfd_subelem
[*pos
])) {
1938 wpa_printf(MSG_DEBUG
, "P2P: Add WSD response "
1939 "subelement %u", *pos
);
1940 wpabuf_put_buf(resp
, wpa_s
->global
->wfd_subelem
[*pos
]);
1945 WPA_PUT_LE16(len_pos
, (u8
*) wpabuf_put(resp
, 0) - len_pos
- 2);
1947 #endif /* CONFIG_WIFI_DISPLAY */
1950 void wpas_sd_request(void *ctx
, int freq
, const u8
*sa
, u8 dialog_token
,
1951 u16 update_indic
, const u8
*tlvs
, size_t tlvs_len
)
1953 struct wpa_supplicant
*wpa_s
= ctx
;
1954 const u8
*pos
= tlvs
;
1955 const u8
*end
= tlvs
+ tlvs_len
;
1958 struct wpabuf
*resp
;
1959 u8 srv_proto
, srv_trans_id
;
1963 wpa_hexdump(MSG_MSGDUMP
, "P2P: Service Discovery Request TLVs",
1965 buf_len
= 2 * tlvs_len
+ 1;
1966 buf
= os_malloc(buf_len
);
1968 wpa_snprintf_hex(buf
, buf_len
, tlvs
, tlvs_len
);
1969 wpa_msg_ctrl(wpa_s
, MSG_INFO
, P2P_EVENT_SERV_DISC_REQ
"%d "
1971 freq
, MAC2STR(sa
), dialog_token
, update_indic
,
1976 if (wpa_s
->p2p_sd_over_ctrl_iface
) {
1977 wpas_notify_p2p_sd_request(wpa_s
, freq
, sa
, dialog_token
,
1978 update_indic
, tlvs
, tlvs_len
);
1979 return; /* to be processed by an external program */
1982 resp
= wpabuf_alloc(10000);
1986 while (pos
+ 1 < end
) {
1987 wpa_printf(MSG_DEBUG
, "P2P: Service Request TLV");
1988 slen
= WPA_GET_LE16(pos
);
1990 if (pos
+ slen
> end
|| slen
< 2) {
1991 wpa_printf(MSG_DEBUG
, "P2P: Unexpected Query Data "
1996 tlv_end
= pos
+ slen
;
1999 wpa_printf(MSG_DEBUG
, "P2P: Service Protocol Type %u",
2001 srv_trans_id
= *pos
++;
2002 wpa_printf(MSG_DEBUG
, "P2P: Service Transaction ID %u",
2005 wpa_hexdump(MSG_MSGDUMP
, "P2P: Query Data",
2006 pos
, tlv_end
- pos
);
2009 if (wpa_s
->force_long_sd
) {
2010 wpa_printf(MSG_DEBUG
, "P2P: SD test - force long "
2012 wpas_sd_all_bonjour(wpa_s
, resp
, srv_trans_id
);
2013 wpas_sd_all_upnp(wpa_s
, resp
, srv_trans_id
);
2017 switch (srv_proto
) {
2018 case P2P_SERV_ALL_SERVICES
:
2019 wpa_printf(MSG_DEBUG
, "P2P: Service Discovery Request "
2020 "for all services");
2021 if (dl_list_empty(&wpa_s
->global
->p2p_srv_upnp
) &&
2022 dl_list_empty(&wpa_s
->global
->p2p_srv_bonjour
)) {
2023 wpa_printf(MSG_DEBUG
, "P2P: No service "
2024 "discovery protocols available");
2025 wpas_sd_add_proto_not_avail(
2026 resp
, P2P_SERV_ALL_SERVICES
,
2030 wpas_sd_all_bonjour(wpa_s
, resp
, srv_trans_id
);
2031 wpas_sd_all_upnp(wpa_s
, resp
, srv_trans_id
);
2033 case P2P_SERV_BONJOUR
:
2034 wpas_sd_req_bonjour(wpa_s
, resp
, srv_trans_id
,
2035 pos
, tlv_end
- pos
);
2038 wpas_sd_req_upnp(wpa_s
, resp
, srv_trans_id
,
2039 pos
, tlv_end
- pos
);
2041 #ifdef CONFIG_WIFI_DISPLAY
2042 case P2P_SERV_WIFI_DISPLAY
:
2043 wpas_sd_req_wfd(wpa_s
, resp
, srv_trans_id
,
2044 pos
, tlv_end
- pos
);
2046 #endif /* CONFIG_WIFI_DISPLAY */
2048 wpa_printf(MSG_DEBUG
, "P2P: Unavailable service "
2049 "protocol %u", srv_proto
);
2050 wpas_sd_add_proto_not_avail(resp
, srv_proto
,
2059 wpas_notify_p2p_sd_request(wpa_s
, freq
, sa
, dialog_token
,
2060 update_indic
, tlvs
, tlvs_len
);
2062 wpas_p2p_sd_response(wpa_s
, freq
, sa
, dialog_token
, resp
);
2068 void wpas_sd_response(void *ctx
, const u8
*sa
, u16 update_indic
,
2069 const u8
*tlvs
, size_t tlvs_len
)
2071 struct wpa_supplicant
*wpa_s
= ctx
;
2072 const u8
*pos
= tlvs
;
2073 const u8
*end
= tlvs
+ tlvs_len
;
2079 wpa_hexdump(MSG_MSGDUMP
, "P2P: Service Discovery Response TLVs",
2081 if (tlvs_len
> 1500) {
2082 /* TODO: better way for handling this */
2083 wpa_msg_ctrl(wpa_s
, MSG_INFO
,
2084 P2P_EVENT_SERV_DISC_RESP MACSTR
2085 " %u <long response: %u bytes>",
2086 MAC2STR(sa
), update_indic
,
2087 (unsigned int) tlvs_len
);
2089 buf_len
= 2 * tlvs_len
+ 1;
2090 buf
= os_malloc(buf_len
);
2092 wpa_snprintf_hex(buf
, buf_len
, tlvs
, tlvs_len
);
2093 wpa_msg_ctrl(wpa_s
, MSG_INFO
,
2094 P2P_EVENT_SERV_DISC_RESP MACSTR
" %u %s",
2095 MAC2STR(sa
), update_indic
, buf
);
2101 u8 srv_proto
, srv_trans_id
, status
;
2103 wpa_printf(MSG_DEBUG
, "P2P: Service Response TLV");
2104 slen
= WPA_GET_LE16(pos
);
2106 if (pos
+ slen
> end
|| slen
< 3) {
2107 wpa_printf(MSG_DEBUG
, "P2P: Unexpected Response Data "
2111 tlv_end
= pos
+ slen
;
2114 wpa_printf(MSG_DEBUG
, "P2P: Service Protocol Type %u",
2116 srv_trans_id
= *pos
++;
2117 wpa_printf(MSG_DEBUG
, "P2P: Service Transaction ID %u",
2120 wpa_printf(MSG_DEBUG
, "P2P: Status Code ID %u",
2123 wpa_hexdump(MSG_MSGDUMP
, "P2P: Response Data",
2124 pos
, tlv_end
- pos
);
2129 wpas_notify_p2p_sd_response(wpa_s
, sa
, update_indic
, tlvs
, tlvs_len
);
2133 u64
wpas_p2p_sd_request(struct wpa_supplicant
*wpa_s
, const u8
*dst
,
2134 const struct wpabuf
*tlvs
)
2136 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
2137 return wpa_drv_p2p_sd_request(wpa_s
, dst
, tlvs
);
2138 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
2140 return (uintptr_t) p2p_sd_request(wpa_s
->global
->p2p
, dst
, tlvs
);
2144 u64
wpas_p2p_sd_request_upnp(struct wpa_supplicant
*wpa_s
, const u8
*dst
,
2145 u8 version
, const char *query
)
2147 struct wpabuf
*tlvs
;
2150 tlvs
= wpabuf_alloc(2 + 1 + 1 + 1 + os_strlen(query
));
2153 wpabuf_put_le16(tlvs
, 1 + 1 + 1 + os_strlen(query
));
2154 wpabuf_put_u8(tlvs
, P2P_SERV_UPNP
); /* Service Protocol Type */
2155 wpabuf_put_u8(tlvs
, 1); /* Service Transaction ID */
2156 wpabuf_put_u8(tlvs
, version
);
2157 wpabuf_put_str(tlvs
, query
);
2158 ret
= wpas_p2p_sd_request(wpa_s
, dst
, tlvs
);
2164 #ifdef CONFIG_WIFI_DISPLAY
2166 static u64
wpas_p2p_sd_request_wfd(struct wpa_supplicant
*wpa_s
, const u8
*dst
,
2167 const struct wpabuf
*tlvs
)
2169 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
2171 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
2173 return (uintptr_t) p2p_sd_request_wfd(wpa_s
->global
->p2p
, dst
, tlvs
);
2177 #define MAX_WFD_SD_SUBELEMS 20
2179 static void wfd_add_sd_req_role(struct wpabuf
*tlvs
, u8 id
, u8 role
,
2180 const char *subelems
)
2187 len
= wpabuf_put(tlvs
, 2);
2188 wpabuf_put_u8(tlvs
, P2P_SERV_WIFI_DISPLAY
); /* Service Protocol Type */
2189 wpabuf_put_u8(tlvs
, id
); /* Service Transaction ID */
2191 wpabuf_put_u8(tlvs
, role
);
2196 if (val
>= 0 && val
< 256) {
2197 wpabuf_put_u8(tlvs
, val
);
2199 if (count
== MAX_WFD_SD_SUBELEMS
)
2202 pos
= os_strchr(pos
+ 1, ',');
2208 WPA_PUT_LE16(len
, (u8
*) wpabuf_put(tlvs
, 0) - len
- 2);
2212 u64
wpas_p2p_sd_request_wifi_display(struct wpa_supplicant
*wpa_s
,
2213 const u8
*dst
, const char *role
)
2215 struct wpabuf
*tlvs
;
2217 const char *subelems
;
2220 subelems
= os_strchr(role
, ' ');
2221 if (subelems
== NULL
)
2225 tlvs
= wpabuf_alloc(4 * (2 + 1 + 1 + 1 + MAX_WFD_SD_SUBELEMS
));
2229 if (os_strstr(role
, "[source]"))
2230 wfd_add_sd_req_role(tlvs
, id
++, 0x00, subelems
);
2231 if (os_strstr(role
, "[pri-sink]"))
2232 wfd_add_sd_req_role(tlvs
, id
++, 0x01, subelems
);
2233 if (os_strstr(role
, "[sec-sink]"))
2234 wfd_add_sd_req_role(tlvs
, id
++, 0x02, subelems
);
2235 if (os_strstr(role
, "[source+sink]"))
2236 wfd_add_sd_req_role(tlvs
, id
++, 0x03, subelems
);
2238 ret
= wpas_p2p_sd_request_wfd(wpa_s
, dst
, tlvs
);
2243 #endif /* CONFIG_WIFI_DISPLAY */
2246 int wpas_p2p_sd_cancel_request(struct wpa_supplicant
*wpa_s
, u64 req
)
2248 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
2249 return wpa_drv_p2p_sd_cancel_request(wpa_s
, req
);
2250 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
2252 return p2p_sd_cancel_request(wpa_s
->global
->p2p
,
2253 (void *) (uintptr_t) req
);
2257 void wpas_p2p_sd_response(struct wpa_supplicant
*wpa_s
, int freq
,
2258 const u8
*dst
, u8 dialog_token
,
2259 const struct wpabuf
*resp_tlvs
)
2261 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
) {
2262 wpa_drv_p2p_sd_response(wpa_s
, freq
, dst
, dialog_token
,
2266 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
2268 p2p_sd_response(wpa_s
->global
->p2p
, freq
, dst
, dialog_token
,
2273 void wpas_p2p_sd_service_update(struct wpa_supplicant
*wpa_s
)
2275 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
) {
2276 wpa_drv_p2p_service_update(wpa_s
);
2279 if (wpa_s
->global
->p2p
)
2280 p2p_sd_service_update(wpa_s
->global
->p2p
);
2284 static void wpas_p2p_srv_bonjour_free(struct p2p_srv_bonjour
*bsrv
)
2286 dl_list_del(&bsrv
->list
);
2287 wpabuf_free(bsrv
->query
);
2288 wpabuf_free(bsrv
->resp
);
2293 static void wpas_p2p_srv_upnp_free(struct p2p_srv_upnp
*usrv
)
2295 dl_list_del(&usrv
->list
);
2296 os_free(usrv
->service
);
2301 void wpas_p2p_service_flush(struct wpa_supplicant
*wpa_s
)
2303 struct p2p_srv_bonjour
*bsrv
, *bn
;
2304 struct p2p_srv_upnp
*usrv
, *un
;
2306 dl_list_for_each_safe(bsrv
, bn
, &wpa_s
->global
->p2p_srv_bonjour
,
2307 struct p2p_srv_bonjour
, list
)
2308 wpas_p2p_srv_bonjour_free(bsrv
);
2310 dl_list_for_each_safe(usrv
, un
, &wpa_s
->global
->p2p_srv_upnp
,
2311 struct p2p_srv_upnp
, list
)
2312 wpas_p2p_srv_upnp_free(usrv
);
2314 wpas_p2p_sd_service_update(wpa_s
);
2318 int wpas_p2p_service_add_bonjour(struct wpa_supplicant
*wpa_s
,
2319 struct wpabuf
*query
, struct wpabuf
*resp
)
2321 struct p2p_srv_bonjour
*bsrv
;
2323 bsrv
= os_zalloc(sizeof(*bsrv
));
2326 bsrv
->query
= query
;
2328 dl_list_add(&wpa_s
->global
->p2p_srv_bonjour
, &bsrv
->list
);
2330 wpas_p2p_sd_service_update(wpa_s
);
2335 int wpas_p2p_service_del_bonjour(struct wpa_supplicant
*wpa_s
,
2336 const struct wpabuf
*query
)
2338 struct p2p_srv_bonjour
*bsrv
;
2340 bsrv
= wpas_p2p_service_get_bonjour(wpa_s
, query
);
2343 wpas_p2p_srv_bonjour_free(bsrv
);
2344 wpas_p2p_sd_service_update(wpa_s
);
2349 int wpas_p2p_service_add_upnp(struct wpa_supplicant
*wpa_s
, u8 version
,
2350 const char *service
)
2352 struct p2p_srv_upnp
*usrv
;
2354 if (wpas_p2p_service_get_upnp(wpa_s
, version
, service
))
2355 return 0; /* Already listed */
2356 usrv
= os_zalloc(sizeof(*usrv
));
2359 usrv
->version
= version
;
2360 usrv
->service
= os_strdup(service
);
2361 if (usrv
->service
== NULL
) {
2365 dl_list_add(&wpa_s
->global
->p2p_srv_upnp
, &usrv
->list
);
2367 wpas_p2p_sd_service_update(wpa_s
);
2372 int wpas_p2p_service_del_upnp(struct wpa_supplicant
*wpa_s
, u8 version
,
2373 const char *service
)
2375 struct p2p_srv_upnp
*usrv
;
2377 usrv
= wpas_p2p_service_get_upnp(wpa_s
, version
, service
);
2380 wpas_p2p_srv_upnp_free(usrv
);
2381 wpas_p2p_sd_service_update(wpa_s
);
2386 static void wpas_prov_disc_local_display(struct wpa_supplicant
*wpa_s
,
2387 const u8
*peer
, const char *params
,
2388 unsigned int generated_pin
)
2390 wpa_msg_global(wpa_s
, MSG_INFO
, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR
2391 " %08d%s", MAC2STR(peer
), generated_pin
, params
);
2395 static void wpas_prov_disc_local_keypad(struct wpa_supplicant
*wpa_s
,
2396 const u8
*peer
, const char *params
)
2398 wpa_msg_global(wpa_s
, MSG_INFO
, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR
2399 "%s", MAC2STR(peer
), params
);
2403 void wpas_prov_disc_req(void *ctx
, const u8
*peer
, u16 config_methods
,
2404 const u8
*dev_addr
, const u8
*pri_dev_type
,
2405 const char *dev_name
, u16 supp_config_methods
,
2406 u8 dev_capab
, u8 group_capab
, const u8
*group_id
,
2407 size_t group_id_len
)
2409 struct wpa_supplicant
*wpa_s
= ctx
;
2410 char devtype
[WPS_DEV_TYPE_BUFSIZE
];
2412 u8 empty_dev_type
[8];
2413 unsigned int generated_pin
= 0;
2414 struct wpa_supplicant
*group
= NULL
;
2417 for (group
= wpa_s
->global
->ifaces
; group
; group
= group
->next
)
2419 struct wpa_ssid
*s
= group
->current_ssid
;
2421 s
->mode
== WPAS_MODE_P2P_GO
&&
2422 group_id_len
- ETH_ALEN
== s
->ssid_len
&&
2423 os_memcmp(group_id
+ ETH_ALEN
, s
->ssid
,
2429 if (pri_dev_type
== NULL
) {
2430 os_memset(empty_dev_type
, 0, sizeof(empty_dev_type
));
2431 pri_dev_type
= empty_dev_type
;
2433 os_snprintf(params
, sizeof(params
), " p2p_dev_addr=" MACSTR
2434 " pri_dev_type=%s name='%s' config_methods=0x%x "
2435 "dev_capab=0x%x group_capab=0x%x%s%s",
2437 wps_dev_type_bin2str(pri_dev_type
, devtype
,
2439 dev_name
, supp_config_methods
, dev_capab
, group_capab
,
2440 group
? " group=" : "",
2441 group
? group
->ifname
: "");
2442 params
[sizeof(params
) - 1] = '\0';
2444 if (config_methods
& WPS_CONFIG_DISPLAY
) {
2445 generated_pin
= wps_generate_pin();
2446 wpas_prov_disc_local_display(wpa_s
, peer
, params
,
2448 } else if (config_methods
& WPS_CONFIG_KEYPAD
)
2449 wpas_prov_disc_local_keypad(wpa_s
, peer
, params
);
2450 else if (config_methods
& WPS_CONFIG_PUSHBUTTON
)
2451 wpa_msg_global(wpa_s
, MSG_INFO
, P2P_EVENT_PROV_DISC_PBC_REQ
2452 MACSTR
"%s", MAC2STR(peer
), params
);
2454 wpas_notify_p2p_provision_discovery(wpa_s
, peer
, 1 /* request */,
2455 P2P_PROV_DISC_SUCCESS
,
2456 config_methods
, generated_pin
);
2460 void wpas_prov_disc_resp(void *ctx
, const u8
*peer
, u16 config_methods
)
2462 struct wpa_supplicant
*wpa_s
= ctx
;
2463 unsigned int generated_pin
= 0;
2466 if (wpa_s
->pending_pd_before_join
&&
2467 (os_memcmp(peer
, wpa_s
->pending_join_dev_addr
, ETH_ALEN
) == 0 ||
2468 os_memcmp(peer
, wpa_s
->pending_join_iface_addr
, ETH_ALEN
) == 0)) {
2469 wpa_s
->pending_pd_before_join
= 0;
2470 wpa_printf(MSG_DEBUG
, "P2P: Starting pending "
2471 "join-existing-group operation");
2472 wpas_p2p_join_start(wpa_s
);
2476 if (wpa_s
->pending_pd_use
== AUTO_PD_JOIN
||
2477 wpa_s
->pending_pd_use
== AUTO_PD_GO_NEG
)
2478 os_snprintf(params
, sizeof(params
), " peer_go=%d",
2479 wpa_s
->pending_pd_use
== AUTO_PD_JOIN
);
2483 if (config_methods
& WPS_CONFIG_DISPLAY
)
2484 wpas_prov_disc_local_keypad(wpa_s
, peer
, params
);
2485 else if (config_methods
& WPS_CONFIG_KEYPAD
) {
2486 generated_pin
= wps_generate_pin();
2487 wpas_prov_disc_local_display(wpa_s
, peer
, params
,
2489 } else if (config_methods
& WPS_CONFIG_PUSHBUTTON
)
2490 wpa_msg_global(wpa_s
, MSG_INFO
, P2P_EVENT_PROV_DISC_PBC_RESP
2491 MACSTR
"%s", MAC2STR(peer
), params
);
2493 wpas_notify_p2p_provision_discovery(wpa_s
, peer
, 0 /* response */,
2494 P2P_PROV_DISC_SUCCESS
,
2495 config_methods
, generated_pin
);
2499 static void wpas_prov_disc_fail(void *ctx
, const u8
*peer
,
2500 enum p2p_prov_disc_status status
)
2502 struct wpa_supplicant
*wpa_s
= ctx
;
2504 if (wpa_s
->p2p_fallback_to_go_neg
) {
2505 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: PD for p2p_connect-auto "
2506 "failed - fall back to GO Negotiation");
2507 wpas_p2p_fallback_to_go_neg(wpa_s
, 0);
2511 if (status
== P2P_PROV_DISC_TIMEOUT_JOIN
) {
2512 wpa_s
->pending_pd_before_join
= 0;
2513 wpa_printf(MSG_DEBUG
, "P2P: Starting pending "
2514 "join-existing-group operation (no ACK for PD "
2516 wpas_p2p_join_start(wpa_s
);
2520 wpa_msg_global(wpa_s
, MSG_INFO
, P2P_EVENT_PROV_DISC_FAILURE
2521 " p2p_dev_addr=" MACSTR
" status=%d",
2522 MAC2STR(peer
), status
);
2524 wpas_notify_p2p_provision_discovery(wpa_s
, peer
, 0 /* response */,
2529 static int freq_included(const struct p2p_channels
*channels
, unsigned int freq
)
2531 if (channels
== NULL
)
2532 return 1; /* Assume no restrictions */
2533 return p2p_channels_includes_freq(channels
, freq
);
2538 static u8
wpas_invitation_process(void *ctx
, const u8
*sa
, const u8
*bssid
,
2539 const u8
*go_dev_addr
, const u8
*ssid
,
2540 size_t ssid_len
, int *go
, u8
*group_bssid
,
2541 int *force_freq
, int persistent_group
,
2542 const struct p2p_channels
*channels
)
2544 struct wpa_supplicant
*wpa_s
= ctx
;
2547 struct wpa_supplicant
*grp
;
2549 if (!persistent_group
) {
2550 wpa_printf(MSG_DEBUG
, "P2P: Invitation from " MACSTR
2551 " to join an active group", MAC2STR(sa
));
2552 if (!is_zero_ether_addr(wpa_s
->p2p_auth_invite
) &&
2553 (os_memcmp(go_dev_addr
, wpa_s
->p2p_auth_invite
, ETH_ALEN
)
2555 os_memcmp(sa
, wpa_s
->p2p_auth_invite
, ETH_ALEN
) == 0)) {
2556 wpa_printf(MSG_DEBUG
, "P2P: Accept previously "
2557 "authorized invitation");
2561 * Do not accept the invitation automatically; notify user and
2564 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE
;
2567 grp
= wpas_get_p2p_group(wpa_s
, ssid
, ssid_len
, go
);
2569 wpa_printf(MSG_DEBUG
, "P2P: Accept invitation to already "
2570 "running persistent group");
2572 os_memcpy(group_bssid
, grp
->own_addr
, ETH_ALEN
);
2576 if (!is_zero_ether_addr(wpa_s
->p2p_auth_invite
) &&
2577 os_memcmp(sa
, wpa_s
->p2p_auth_invite
, ETH_ALEN
) == 0) {
2578 wpa_printf(MSG_DEBUG
, "P2P: Accept previously initiated "
2579 "invitation to re-invoke a persistent group");
2580 } else if (!wpa_s
->conf
->persistent_reconnect
)
2581 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE
;
2583 for (s
= wpa_s
->conf
->ssid
; s
; s
= s
->next
) {
2584 if (s
->disabled
== 2 &&
2585 os_memcmp(s
->bssid
, go_dev_addr
, ETH_ALEN
) == 0 &&
2586 s
->ssid_len
== ssid_len
&&
2587 os_memcmp(ssid
, s
->ssid
, ssid_len
) == 0)
2592 wpa_printf(MSG_DEBUG
, "P2P: Invitation from " MACSTR
2593 " requested reinvocation of an unknown group",
2595 return P2P_SC_FAIL_UNKNOWN_GROUP
;
2598 if (s
->mode
== WPAS_MODE_P2P_GO
&& !wpas_p2p_create_iface(wpa_s
)) {
2600 if (wpa_s
->wpa_state
>= WPA_AUTHENTICATING
) {
2601 wpa_printf(MSG_DEBUG
, "P2P: The only available "
2602 "interface is already in use - reject "
2604 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE
;
2606 os_memcpy(group_bssid
, wpa_s
->own_addr
, ETH_ALEN
);
2607 } else if (s
->mode
== WPAS_MODE_P2P_GO
) {
2609 if (wpas_p2p_add_group_interface(wpa_s
, WPA_IF_P2P_GO
) < 0)
2611 wpa_printf(MSG_ERROR
, "P2P: Failed to allocate a new "
2612 "interface address for the group");
2613 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE
;
2615 os_memcpy(group_bssid
, wpa_s
->pending_interface_addr
,
2620 wpas_p2p_set_own_freq_preference(wpa_s
, 0);
2622 /* Get one of the frequencies currently in use */
2623 if (wpas_p2p_valid_oper_freqs(wpa_s
, &res
, 1) > 0) {
2624 wpa_printf(MSG_DEBUG
, "P2P: Trying to force channel to match a channel already used by one of the interfaces");
2626 wpas_p2p_set_own_freq_preference(wpa_s
, res
);
2629 if (*force_freq
> 0 && wpa_s
->num_multichan_concurrent
> 1 &&
2630 wpas_p2p_num_unused_channels(wpa_s
) > 0) {
2632 /* We are the client */
2633 wpa_printf(MSG_DEBUG
, "P2P: Peer was found to be "
2634 "running a GO but we are capable of MCC, "
2635 "figure out the best channel to use");
2637 } else if (!freq_included(channels
, *force_freq
)) {
2638 /* We are the GO, and *force_freq is not in the
2640 wpa_printf(MSG_DEBUG
, "P2P: Forced GO freq %d MHz not "
2641 "in intersection but we are capable of MCC, "
2642 "figure out the best channel to use",
2648 return P2P_SC_SUCCESS
;
2652 static void wpas_invitation_received(void *ctx
, const u8
*sa
, const u8
*bssid
,
2653 const u8
*ssid
, size_t ssid_len
,
2654 const u8
*go_dev_addr
, u8 status
,
2657 struct wpa_supplicant
*wpa_s
= ctx
;
2660 for (s
= wpa_s
->conf
->ssid
; s
; s
= s
->next
) {
2661 if (s
->disabled
== 2 &&
2662 s
->ssid_len
== ssid_len
&&
2663 os_memcmp(ssid
, s
->ssid
, ssid_len
) == 0)
2667 if (status
== P2P_SC_SUCCESS
) {
2668 wpa_printf(MSG_DEBUG
, "P2P: Invitation from peer " MACSTR
2669 " was accepted; op_freq=%d MHz",
2670 MAC2STR(sa
), op_freq
);
2672 int go
= s
->mode
== WPAS_MODE_P2P_GO
;
2673 wpas_p2p_group_add_persistent(
2674 wpa_s
, s
, go
, go
? op_freq
: 0, 0, NULL
);
2676 wpa_s
->user_initiated_pd
= 0;
2677 wpas_p2p_join(wpa_s
, bssid
, go_dev_addr
,
2678 wpa_s
->p2p_wps_method
, 0);
2683 if (status
!= P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE
) {
2684 wpa_printf(MSG_DEBUG
, "P2P: Invitation from peer " MACSTR
2685 " was rejected (status %u)", MAC2STR(sa
), status
);
2691 wpa_msg_global(wpa_s
, MSG_INFO
,
2692 P2P_EVENT_INVITATION_RECEIVED
2693 "sa=" MACSTR
" go_dev_addr=" MACSTR
2694 " bssid=" MACSTR
" unknown-network",
2695 MAC2STR(sa
), MAC2STR(go_dev_addr
),
2698 wpa_msg_global(wpa_s
, MSG_INFO
,
2699 P2P_EVENT_INVITATION_RECEIVED
2700 "sa=" MACSTR
" go_dev_addr=" MACSTR
2702 MAC2STR(sa
), MAC2STR(go_dev_addr
));
2707 if (s
->mode
== WPAS_MODE_P2P_GO
&& op_freq
) {
2708 wpa_msg_global(wpa_s
, MSG_INFO
, P2P_EVENT_INVITATION_RECEIVED
2709 "sa=" MACSTR
" persistent=%d freq=%d",
2710 MAC2STR(sa
), s
->id
, op_freq
);
2712 wpa_msg_global(wpa_s
, MSG_INFO
, P2P_EVENT_INVITATION_RECEIVED
2713 "sa=" MACSTR
" persistent=%d",
2714 MAC2STR(sa
), s
->id
);
2719 static void wpas_remove_persistent_peer(struct wpa_supplicant
*wpa_s
,
2720 struct wpa_ssid
*ssid
,
2721 const u8
*peer
, int inv
)
2728 for (i
= 0; ssid
->p2p_client_list
&& i
< ssid
->num_p2p_clients
; i
++) {
2729 if (os_memcmp(ssid
->p2p_client_list
+ i
* ETH_ALEN
, peer
,
2733 if (i
>= ssid
->num_p2p_clients
) {
2734 if (ssid
->mode
!= WPAS_MODE_P2P_GO
&&
2735 os_memcmp(ssid
->bssid
, peer
, ETH_ALEN
) == 0) {
2736 wpa_printf(MSG_DEBUG
, "P2P: Remove persistent group %d "
2737 "due to invitation result", ssid
->id
);
2738 wpas_notify_network_removed(wpa_s
, ssid
);
2739 wpa_config_remove_network(wpa_s
->conf
, ssid
->id
);
2742 return; /* Peer not found in client list */
2745 wpa_printf(MSG_DEBUG
, "P2P: Remove peer " MACSTR
" from persistent "
2746 "group %d client list%s",
2747 MAC2STR(peer
), ssid
->id
,
2748 inv
? " due to invitation result" : "");
2749 os_memmove(ssid
->p2p_client_list
+ i
* ETH_ALEN
,
2750 ssid
->p2p_client_list
+ (i
+ 1) * ETH_ALEN
,
2751 (ssid
->num_p2p_clients
- i
- 1) * ETH_ALEN
);
2752 ssid
->num_p2p_clients
--;
2753 #ifndef CONFIG_NO_CONFIG_WRITE
2754 if (wpa_s
->parent
->conf
->update_config
&&
2755 wpa_config_write(wpa_s
->parent
->confname
, wpa_s
->parent
->conf
))
2756 wpa_printf(MSG_DEBUG
, "P2P: Failed to update configuration");
2757 #endif /* CONFIG_NO_CONFIG_WRITE */
2761 static void wpas_remove_persistent_client(struct wpa_supplicant
*wpa_s
,
2764 struct wpa_ssid
*ssid
;
2766 wpa_s
= wpa_s
->global
->p2p_invite_group
;
2768 return; /* No known invitation group */
2769 ssid
= wpa_s
->current_ssid
;
2770 if (ssid
== NULL
|| ssid
->mode
!= WPAS_MODE_P2P_GO
||
2771 !ssid
->p2p_persistent_group
)
2772 return; /* Not operating as a GO in persistent group */
2773 ssid
= wpas_p2p_get_persistent(wpa_s
->parent
, peer
,
2774 ssid
->ssid
, ssid
->ssid_len
);
2775 wpas_remove_persistent_peer(wpa_s
, ssid
, peer
, 1);
2779 static void wpas_invitation_result(void *ctx
, int status
, const u8
*bssid
,
2780 const struct p2p_channels
*channels
,
2783 struct wpa_supplicant
*wpa_s
= ctx
;
2784 struct wpa_ssid
*ssid
;
2787 wpa_msg_global(wpa_s
, MSG_INFO
, P2P_EVENT_INVITATION_RESULT
2788 "status=%d " MACSTR
,
2789 status
, MAC2STR(bssid
));
2791 wpa_msg_global(wpa_s
, MSG_INFO
, P2P_EVENT_INVITATION_RESULT
2792 "status=%d ", status
);
2794 wpas_notify_p2p_invitation_result(wpa_s
, status
, bssid
);
2796 wpa_printf(MSG_DEBUG
, "P2P: Invitation result - status=%d peer=" MACSTR
,
2797 status
, MAC2STR(peer
));
2798 if (wpa_s
->pending_invite_ssid_id
== -1) {
2799 if (status
== P2P_SC_FAIL_UNKNOWN_GROUP
)
2800 wpas_remove_persistent_client(wpa_s
, peer
);
2801 return; /* Invitation to active group */
2804 if (status
== P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE
) {
2805 wpa_printf(MSG_DEBUG
, "P2P: Waiting for peer to start another "
2806 "invitation exchange to indicate readiness for "
2810 if (status
!= P2P_SC_SUCCESS
) {
2811 if (status
== P2P_SC_FAIL_UNKNOWN_GROUP
) {
2812 ssid
= wpa_config_get_network(
2813 wpa_s
->conf
, wpa_s
->pending_invite_ssid_id
);
2814 wpas_remove_persistent_peer(wpa_s
, ssid
, peer
, 1);
2816 wpas_p2p_remove_pending_group_interface(wpa_s
);
2820 ssid
= wpa_config_get_network(wpa_s
->conf
,
2821 wpa_s
->pending_invite_ssid_id
);
2823 wpa_printf(MSG_ERROR
, "P2P: Could not find persistent group "
2824 "data matching with invitation");
2829 * The peer could have missed our ctrl::ack frame for Invitation
2830 * Response and continue retransmitting the frame. To reduce the
2831 * likelihood of the peer not getting successful TX status for the
2832 * Invitation Response frame, wait a short time here before starting
2833 * the persistent group so that we will remain on the current channel to
2834 * acknowledge any possible retransmission from the peer.
2836 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: 50 ms wait on current channel before "
2837 "starting persistent group");
2840 wpas_p2p_group_add_persistent(wpa_s
, ssid
,
2841 ssid
->mode
== WPAS_MODE_P2P_GO
,
2842 wpa_s
->p2p_persistent_go_freq
,
2843 wpa_s
->p2p_go_ht40
, channels
);
2847 static int wpas_p2p_disallowed_freq(struct wpa_global
*global
,
2852 if (global
->p2p_disallow_freq
== NULL
)
2855 for (i
= 0; i
< global
->num_p2p_disallow_freq
; i
++) {
2856 if (freq
>= global
->p2p_disallow_freq
[i
].min
&&
2857 freq
<= global
->p2p_disallow_freq
[i
].max
)
2865 static void wpas_p2p_add_chan(struct p2p_reg_class
*reg
, u8 chan
)
2867 reg
->channel
[reg
->channels
] = chan
;
2872 static int wpas_p2p_default_channels(struct wpa_supplicant
*wpa_s
,
2873 struct p2p_channels
*chan
)
2877 wpa_printf(MSG_DEBUG
, "P2P: Enable operating classes for 2.4 GHz "
2880 /* Operating class 81 - 2.4 GHz band channels 1..13 */
2881 chan
->reg_class
[cla
].reg_class
= 81;
2882 chan
->reg_class
[cla
].channels
= 0;
2883 for (i
= 0; i
< 11; i
++) {
2884 if (!wpas_p2p_disallowed_freq(wpa_s
->global
, 2412 + i
* 5))
2885 wpas_p2p_add_chan(&chan
->reg_class
[cla
], i
+ 1);
2887 if (chan
->reg_class
[cla
].channels
)
2890 wpa_printf(MSG_DEBUG
, "P2P: Enable operating classes for lower 5 GHz "
2893 /* Operating class 115 - 5 GHz, channels 36-48 */
2894 chan
->reg_class
[cla
].reg_class
= 115;
2895 chan
->reg_class
[cla
].channels
= 0;
2896 if (!wpas_p2p_disallowed_freq(wpa_s
->global
, 5000 + 36 * 5))
2897 wpas_p2p_add_chan(&chan
->reg_class
[cla
], 36);
2898 if (!wpas_p2p_disallowed_freq(wpa_s
->global
, 5000 + 40 * 5))
2899 wpas_p2p_add_chan(&chan
->reg_class
[cla
], 40);
2900 if (!wpas_p2p_disallowed_freq(wpa_s
->global
, 5000 + 44 * 5))
2901 wpas_p2p_add_chan(&chan
->reg_class
[cla
], 44);
2902 if (!wpas_p2p_disallowed_freq(wpa_s
->global
, 5000 + 48 * 5))
2903 wpas_p2p_add_chan(&chan
->reg_class
[cla
], 48);
2904 if (chan
->reg_class
[cla
].channels
)
2907 wpa_printf(MSG_DEBUG
, "P2P: Enable operating classes for higher 5 GHz "
2910 /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
2911 chan
->reg_class
[cla
].reg_class
= 124;
2912 chan
->reg_class
[cla
].channels
= 0;
2913 if (!wpas_p2p_disallowed_freq(wpa_s
->global
, 5000 + 149 * 5))
2914 wpas_p2p_add_chan(&chan
->reg_class
[cla
], 149);
2915 if (!wpas_p2p_disallowed_freq(wpa_s
->global
, 5000 + 153 * 5))
2916 wpas_p2p_add_chan(&chan
->reg_class
[cla
], 153);
2917 if (!wpas_p2p_disallowed_freq(wpa_s
->global
, 5000 + 156 * 5))
2918 wpas_p2p_add_chan(&chan
->reg_class
[cla
], 157);
2919 if (!wpas_p2p_disallowed_freq(wpa_s
->global
, 5000 + 161 * 5))
2920 wpas_p2p_add_chan(&chan
->reg_class
[cla
], 161);
2921 if (chan
->reg_class
[cla
].channels
)
2924 chan
->reg_classes
= cla
;
2929 static struct hostapd_hw_modes
* get_mode(struct hostapd_hw_modes
*modes
,
2931 enum hostapd_hw_mode mode
)
2935 for (i
= 0; i
< num_modes
; i
++) {
2936 if (modes
[i
].mode
== mode
)
2944 static int has_channel(struct wpa_global
*global
,
2945 struct hostapd_hw_modes
*mode
, u8 chan
, int *flags
)
2950 freq
= (mode
->mode
== HOSTAPD_MODE_IEEE80211A
? 5000 : 2407) +
2952 if (wpas_p2p_disallowed_freq(global
, freq
))
2955 for (i
= 0; i
< mode
->num_channels
; i
++) {
2956 if (mode
->channels
[i
].chan
== chan
) {
2958 *flags
= mode
->channels
[i
].flag
;
2959 return !(mode
->channels
[i
].flag
&
2960 (HOSTAPD_CHAN_DISABLED
|
2961 HOSTAPD_CHAN_PASSIVE_SCAN
|
2962 HOSTAPD_CHAN_NO_IBSS
|
2963 HOSTAPD_CHAN_RADAR
));
2971 struct p2p_oper_class_map
{
2972 enum hostapd_hw_mode mode
;
2977 enum { BW20
, BW40PLUS
, BW40MINUS
} bw
;
2980 static struct p2p_oper_class_map op_class
[] = {
2981 { HOSTAPD_MODE_IEEE80211G
, 81, 1, 13, 1, BW20
},
2982 #if 0 /* Do not enable HT40 on 2 GHz for now */
2983 { HOSTAPD_MODE_IEEE80211G
, 83, 1, 9, 1, BW40PLUS
},
2984 { HOSTAPD_MODE_IEEE80211G
, 84, 5, 13, 1, BW40MINUS
},
2986 { HOSTAPD_MODE_IEEE80211A
, 115, 36, 48, 4, BW20
},
2987 { HOSTAPD_MODE_IEEE80211A
, 124, 149, 161, 4, BW20
},
2988 { HOSTAPD_MODE_IEEE80211A
, 116, 36, 44, 8, BW40PLUS
},
2989 { HOSTAPD_MODE_IEEE80211A
, 117, 40, 48, 8, BW40MINUS
},
2990 { HOSTAPD_MODE_IEEE80211A
, 126, 149, 157, 8, BW40PLUS
},
2991 { HOSTAPD_MODE_IEEE80211A
, 127, 153, 161, 8, BW40MINUS
},
2992 { -1, 0, 0, 0, 0, BW20
}
2996 static int wpas_p2p_verify_channel(struct wpa_supplicant
*wpa_s
,
2997 struct hostapd_hw_modes
*mode
,
3002 if (!has_channel(wpa_s
->global
, mode
, channel
, &flag
))
3004 if (bw
== BW40MINUS
&&
3005 (!(flag
& HOSTAPD_CHAN_HT40MINUS
) ||
3006 !has_channel(wpa_s
->global
, mode
, channel
- 4, NULL
)))
3008 if (bw
== BW40PLUS
&&
3009 (!(flag
& HOSTAPD_CHAN_HT40PLUS
) ||
3010 !has_channel(wpa_s
->global
, mode
, channel
+ 4, NULL
)))
3016 static int wpas_p2p_setup_channels(struct wpa_supplicant
*wpa_s
,
3017 struct p2p_channels
*chan
)
3019 struct hostapd_hw_modes
*mode
;
3022 if (wpa_s
->hw
.modes
== NULL
) {
3023 wpa_printf(MSG_DEBUG
, "P2P: Driver did not support fetching "
3024 "of all supported channels; assume dualband "
3026 return wpas_p2p_default_channels(wpa_s
, chan
);
3031 for (op
= 0; op_class
[op
].op_class
; op
++) {
3032 struct p2p_oper_class_map
*o
= &op_class
[op
];
3034 struct p2p_reg_class
*reg
= NULL
;
3036 mode
= get_mode(wpa_s
->hw
.modes
, wpa_s
->hw
.num_modes
, o
->mode
);
3039 for (ch
= o
->min_chan
; ch
<= o
->max_chan
; ch
+= o
->inc
) {
3040 if (wpas_p2p_verify_channel(wpa_s
, mode
, ch
, o
->bw
) < 1)
3043 wpa_printf(MSG_DEBUG
, "P2P: Add operating "
3044 "class %u", o
->op_class
);
3045 reg
= &chan
->reg_class
[cla
];
3047 reg
->reg_class
= o
->op_class
;
3049 reg
->channel
[reg
->channels
] = ch
;
3053 wpa_hexdump(MSG_DEBUG
, "P2P: Channels",
3054 reg
->channel
, reg
->channels
);
3058 chan
->reg_classes
= cla
;
3064 int wpas_p2p_get_ht40_mode(struct wpa_supplicant
*wpa_s
,
3065 struct hostapd_hw_modes
*mode
, u8 channel
)
3069 for (op
= 0; op_class
[op
].op_class
; op
++) {
3070 struct p2p_oper_class_map
*o
= &op_class
[op
];
3073 for (ch
= o
->min_chan
; ch
<= o
->max_chan
; ch
+= o
->inc
) {
3074 if (o
->mode
!= HOSTAPD_MODE_IEEE80211A
||
3075 o
->bw
== BW20
|| ch
!= channel
)
3077 ret
= wpas_p2p_verify_channel(wpa_s
, mode
, ch
, o
->bw
);
3081 return (o
->bw
== BW40MINUS
) ? -1 : 1;
3090 static int wpas_get_noa(void *ctx
, const u8
*interface_addr
, u8
*buf
,
3093 struct wpa_supplicant
*wpa_s
= ctx
;
3095 for (wpa_s
= wpa_s
->global
->ifaces
; wpa_s
; wpa_s
= wpa_s
->next
) {
3096 if (os_memcmp(wpa_s
->own_addr
, interface_addr
, ETH_ALEN
) == 0)
3102 return wpa_drv_get_noa(wpa_s
, buf
, buf_len
);
3106 static int wpas_go_connected(void *ctx
, const u8
*dev_addr
)
3108 struct wpa_supplicant
*wpa_s
= ctx
;
3110 for (wpa_s
= wpa_s
->global
->ifaces
; wpa_s
; wpa_s
= wpa_s
->next
) {
3111 struct wpa_ssid
*ssid
= wpa_s
->current_ssid
;
3114 if (ssid
->mode
!= WPAS_MODE_INFRA
)
3116 if (wpa_s
->wpa_state
!= WPA_COMPLETED
&&
3117 wpa_s
->wpa_state
!= WPA_GROUP_HANDSHAKE
)
3119 if (os_memcmp(wpa_s
->go_dev_addr
, dev_addr
, ETH_ALEN
) == 0)
3127 static void wpas_p2p_debug_print(void *ctx
, int level
, const char *msg
)
3129 struct wpa_supplicant
*wpa_s
= ctx
;
3130 wpa_msg_global(wpa_s
, level
, "P2P: %s", msg
);
3134 int wpas_p2p_add_p2pdev_interface(struct wpa_supplicant
*wpa_s
)
3136 struct wpa_interface iface
;
3137 struct wpa_supplicant
*p2pdev_wpa_s
;
3139 char force_name
[100];
3142 os_snprintf(ifname
, sizeof(ifname
), P2P_MGMT_DEVICE_PREFIX
"%s",
3144 force_name
[0] = '\0';
3145 wpa_s
->pending_interface_type
= WPA_IF_P2P_DEVICE
;
3146 ret
= wpa_drv_if_add(wpa_s
, WPA_IF_P2P_DEVICE
, ifname
, NULL
, NULL
,
3147 force_name
, wpa_s
->pending_interface_addr
, NULL
);
3149 wpa_printf(MSG_DEBUG
, "P2P: Failed to create P2P Device interface");
3152 os_strlcpy(wpa_s
->pending_interface_name
, ifname
,
3153 sizeof(wpa_s
->pending_interface_name
));
3155 os_memset(&iface
, 0, sizeof(iface
));
3157 iface
.ifname
= wpa_s
->pending_interface_name
;
3158 iface
.driver
= wpa_s
->driver
->name
;
3159 iface
.driver_param
= wpa_s
->conf
->driver_param
;
3160 iface
.confname
= wpa_s
->confname
;
3161 p2pdev_wpa_s
= wpa_supplicant_add_iface(wpa_s
->global
, &iface
);
3162 if (!p2pdev_wpa_s
) {
3163 wpa_printf(MSG_DEBUG
, "P2P: Failed to add P2P Device interface");
3166 p2pdev_wpa_s
->parent
= wpa_s
;
3168 wpa_s
->pending_interface_name
[0] = '\0';
3174 * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
3175 * @global: Pointer to global data from wpa_supplicant_init()
3176 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3177 * Returns: 0 on success, -1 on failure
3179 int wpas_p2p_init(struct wpa_global
*global
, struct wpa_supplicant
*wpa_s
)
3181 struct p2p_config p2p
;
3185 if (wpa_s
->conf
->p2p_disabled
)
3188 if (!(wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_CAPABLE
))
3194 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
) {
3195 struct p2p_params params
;
3197 wpa_printf(MSG_DEBUG
, "P2P: Use driver-based P2P management");
3198 os_memset(¶ms
, 0, sizeof(params
));
3199 params
.dev_name
= wpa_s
->conf
->device_name
;
3200 os_memcpy(params
.pri_dev_type
, wpa_s
->conf
->device_type
,
3202 params
.num_sec_dev_types
= wpa_s
->conf
->num_sec_device_types
;
3203 os_memcpy(params
.sec_dev_type
,
3204 wpa_s
->conf
->sec_device_type
,
3205 params
.num_sec_dev_types
* WPS_DEV_TYPE_LEN
);
3207 if (wpa_drv_p2p_set_params(wpa_s
, ¶ms
) < 0)
3213 os_memset(&p2p
, 0, sizeof(p2p
));
3215 p2p
.debug_print
= wpas_p2p_debug_print
;
3216 p2p
.p2p_scan
= wpas_p2p_scan
;
3217 p2p
.send_action
= wpas_send_action
;
3218 p2p
.send_action_done
= wpas_send_action_done
;
3219 p2p
.go_neg_completed
= wpas_go_neg_completed
;
3220 p2p
.go_neg_req_rx
= wpas_go_neg_req_rx
;
3221 p2p
.dev_found
= wpas_dev_found
;
3222 p2p
.dev_lost
= wpas_dev_lost
;
3223 p2p
.find_stopped
= wpas_find_stopped
;
3224 p2p
.start_listen
= wpas_start_listen
;
3225 p2p
.stop_listen
= wpas_stop_listen
;
3226 p2p
.send_probe_resp
= wpas_send_probe_resp
;
3227 p2p
.sd_request
= wpas_sd_request
;
3228 p2p
.sd_response
= wpas_sd_response
;
3229 p2p
.prov_disc_req
= wpas_prov_disc_req
;
3230 p2p
.prov_disc_resp
= wpas_prov_disc_resp
;
3231 p2p
.prov_disc_fail
= wpas_prov_disc_fail
;
3232 p2p
.invitation_process
= wpas_invitation_process
;
3233 p2p
.invitation_received
= wpas_invitation_received
;
3234 p2p
.invitation_result
= wpas_invitation_result
;
3235 p2p
.get_noa
= wpas_get_noa
;
3236 p2p
.go_connected
= wpas_go_connected
;
3238 os_memcpy(wpa_s
->global
->p2p_dev_addr
, wpa_s
->own_addr
, ETH_ALEN
);
3239 os_memcpy(p2p
.dev_addr
, wpa_s
->global
->p2p_dev_addr
, ETH_ALEN
);
3240 p2p
.dev_name
= wpa_s
->conf
->device_name
;
3241 p2p
.manufacturer
= wpa_s
->conf
->manufacturer
;
3242 p2p
.model_name
= wpa_s
->conf
->model_name
;
3243 p2p
.model_number
= wpa_s
->conf
->model_number
;
3244 p2p
.serial_number
= wpa_s
->conf
->serial_number
;
3246 os_memcpy(p2p
.uuid
, wpa_s
->wps
->uuid
, 16);
3247 p2p
.config_methods
= wpa_s
->wps
->config_methods
;
3250 if (wpa_s
->conf
->p2p_listen_reg_class
&&
3251 wpa_s
->conf
->p2p_listen_channel
) {
3252 p2p
.reg_class
= wpa_s
->conf
->p2p_listen_reg_class
;
3253 p2p
.channel
= wpa_s
->conf
->p2p_listen_channel
;
3257 * Pick one of the social channels randomly as the listen
3260 os_get_random((u8
*) &r
, sizeof(r
));
3261 p2p
.channel
= 1 + (r
% 3) * 5;
3263 wpa_printf(MSG_DEBUG
, "P2P: Own listen channel: %d", p2p
.channel
);
3265 if (wpa_s
->conf
->p2p_oper_reg_class
&&
3266 wpa_s
->conf
->p2p_oper_channel
) {
3267 p2p
.op_reg_class
= wpa_s
->conf
->p2p_oper_reg_class
;
3268 p2p
.op_channel
= wpa_s
->conf
->p2p_oper_channel
;
3269 p2p
.cfg_op_channel
= 1;
3270 wpa_printf(MSG_DEBUG
, "P2P: Configured operating channel: "
3271 "%d:%d", p2p
.op_reg_class
, p2p
.op_channel
);
3274 p2p
.op_reg_class
= 81;
3276 * Use random operation channel from (1, 6, 11) if no other
3277 * preference is indicated.
3279 os_get_random((u8
*) &r
, sizeof(r
));
3280 p2p
.op_channel
= 1 + (r
% 3) * 5;
3281 p2p
.cfg_op_channel
= 0;
3282 wpa_printf(MSG_DEBUG
, "P2P: Random operating channel: "
3283 "%d:%d", p2p
.op_reg_class
, p2p
.op_channel
);
3286 if (wpa_s
->conf
->p2p_pref_chan
&& wpa_s
->conf
->num_p2p_pref_chan
) {
3287 p2p
.pref_chan
= wpa_s
->conf
->p2p_pref_chan
;
3288 p2p
.num_pref_chan
= wpa_s
->conf
->num_p2p_pref_chan
;
3291 if (wpa_s
->conf
->country
[0] && wpa_s
->conf
->country
[1]) {
3292 os_memcpy(p2p
.country
, wpa_s
->conf
->country
, 2);
3293 p2p
.country
[2] = 0x04;
3295 os_memcpy(p2p
.country
, "XX\x04", 3);
3297 if (wpas_p2p_setup_channels(wpa_s
, &p2p
.channels
)) {
3298 wpa_printf(MSG_ERROR
, "P2P: Failed to configure supported "
3303 os_memcpy(p2p
.pri_dev_type
, wpa_s
->conf
->device_type
,
3306 p2p
.num_sec_dev_types
= wpa_s
->conf
->num_sec_device_types
;
3307 os_memcpy(p2p
.sec_dev_type
, wpa_s
->conf
->sec_device_type
,
3308 p2p
.num_sec_dev_types
* WPS_DEV_TYPE_LEN
);
3310 p2p
.concurrent_operations
= !!(wpa_s
->drv_flags
&
3311 WPA_DRIVER_FLAGS_P2P_CONCURRENT
);
3313 p2p
.max_peers
= 100;
3315 if (wpa_s
->conf
->p2p_ssid_postfix
) {
3316 p2p
.ssid_postfix_len
=
3317 os_strlen(wpa_s
->conf
->p2p_ssid_postfix
);
3318 if (p2p
.ssid_postfix_len
> sizeof(p2p
.ssid_postfix
))
3319 p2p
.ssid_postfix_len
= sizeof(p2p
.ssid_postfix
);
3320 os_memcpy(p2p
.ssid_postfix
, wpa_s
->conf
->p2p_ssid_postfix
,
3321 p2p
.ssid_postfix_len
);
3324 p2p
.p2p_intra_bss
= wpa_s
->conf
->p2p_intra_bss
;
3326 p2p
.max_listen
= wpa_s
->max_remain_on_chan
;
3328 global
->p2p
= p2p_init(&p2p
);
3329 if (global
->p2p
== NULL
)
3331 global
->p2p_init_wpa_s
= wpa_s
;
3333 for (i
= 0; i
< MAX_WPS_VENDOR_EXT
; i
++) {
3334 if (wpa_s
->conf
->wps_vendor_ext
[i
] == NULL
)
3336 p2p_add_wps_vendor_extension(
3337 global
->p2p
, wpa_s
->conf
->wps_vendor_ext
[i
]);
3345 * wpas_p2p_deinit - Deinitialize per-interface P2P data
3346 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3348 * This function deinitialize per-interface P2P data.
3350 void wpas_p2p_deinit(struct wpa_supplicant
*wpa_s
)
3352 if (wpa_s
->driver
&& wpa_s
->drv_priv
)
3353 wpa_drv_probe_req_report(wpa_s
, 0);
3355 if (wpa_s
->go_params
) {
3356 /* Clear any stored provisioning info */
3357 p2p_clear_provisioning_info(
3359 wpa_s
->go_params
->peer_device_addr
);
3362 os_free(wpa_s
->go_params
);
3363 wpa_s
->go_params
= NULL
;
3364 eloop_cancel_timeout(wpas_p2p_group_formation_timeout
, wpa_s
, NULL
);
3365 eloop_cancel_timeout(wpas_p2p_join_scan
, wpa_s
, NULL
);
3366 wpa_s
->p2p_long_listen
= 0;
3367 eloop_cancel_timeout(wpas_p2p_long_listen_timeout
, wpa_s
, NULL
);
3368 eloop_cancel_timeout(wpas_p2p_group_idle_timeout
, wpa_s
, NULL
);
3369 wpas_p2p_remove_pending_group_interface(wpa_s
);
3371 /* TODO: remove group interface from the driver if this wpa_s instance
3372 * is on top of a P2P group interface */
3377 * wpas_p2p_deinit_global - Deinitialize global P2P module
3378 * @global: Pointer to global data from wpa_supplicant_init()
3380 * This function deinitializes the global (per device) P2P module.
3382 void wpas_p2p_deinit_global(struct wpa_global
*global
)
3384 struct wpa_supplicant
*wpa_s
, *tmp
;
3386 wpa_s
= global
->ifaces
;
3388 wpas_p2p_service_flush(wpa_s
);
3390 if (global
->p2p
== NULL
)
3393 /* Remove remaining P2P group interfaces */
3394 while (wpa_s
&& wpa_s
->p2p_group_interface
!= NOT_P2P_GROUP_INTERFACE
)
3395 wpa_s
= wpa_s
->next
;
3397 tmp
= global
->ifaces
;
3400 tmp
->p2p_group_interface
== NOT_P2P_GROUP_INTERFACE
)) {
3405 /* Disconnect from the P2P group and deinit the interface */
3406 wpas_p2p_disconnect(tmp
);
3410 * Deinit GO data on any possibly remaining interface (if main
3411 * interface is used as GO).
3413 for (wpa_s
= global
->ifaces
; wpa_s
; wpa_s
= wpa_s
->next
) {
3414 if (wpa_s
->ap_iface
)
3415 wpas_p2p_group_deinit(wpa_s
);
3418 p2p_deinit(global
->p2p
);
3420 global
->p2p_init_wpa_s
= NULL
;
3424 static int wpas_p2p_create_iface(struct wpa_supplicant
*wpa_s
)
3426 if (!(wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE
) &&
3427 wpa_s
->conf
->p2p_no_group_iface
)
3428 return 0; /* separate interface disabled per configuration */
3429 if (wpa_s
->drv_flags
&
3430 (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE
|
3431 WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P
))
3432 return 1; /* P2P group requires a new interface in every case
3434 if (!(wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_CONCURRENT
))
3435 return 0; /* driver does not support concurrent operations */
3436 if (wpa_s
->global
->ifaces
->next
)
3437 return 1; /* more that one interface already in use */
3438 if (wpa_s
->wpa_state
>= WPA_AUTHENTICATING
)
3439 return 1; /* this interface is already in use */
3444 static int wpas_p2p_start_go_neg(struct wpa_supplicant
*wpa_s
,
3445 const u8
*peer_addr
,
3446 enum p2p_wps_method wps_method
,
3447 int go_intent
, const u8
*own_interface_addr
,
3448 unsigned int force_freq
, int persistent_group
,
3449 struct wpa_ssid
*ssid
, unsigned int pref_freq
)
3451 if (persistent_group
&& wpa_s
->conf
->persistent_reconnect
)
3452 persistent_group
= 2;
3454 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
) {
3455 return wpa_drv_p2p_connect(wpa_s
, peer_addr
, wps_method
,
3456 go_intent
, own_interface_addr
,
3457 force_freq
, persistent_group
);
3461 * Increase GO config timeout if HT40 is used since it takes some time
3462 * to scan channels for coex purposes before the BSS can be started.
3464 p2p_set_config_timeout(wpa_s
->global
->p2p
,
3465 wpa_s
->p2p_go_ht40
? 255 : 100, 20);
3467 return p2p_connect(wpa_s
->global
->p2p
, peer_addr
, wps_method
,
3468 go_intent
, own_interface_addr
, force_freq
,
3469 persistent_group
, ssid
? ssid
->ssid
: NULL
,
3470 ssid
? ssid
->ssid_len
: 0,
3471 wpa_s
->p2p_pd_before_go_neg
, pref_freq
);
3475 static int wpas_p2p_auth_go_neg(struct wpa_supplicant
*wpa_s
,
3476 const u8
*peer_addr
,
3477 enum p2p_wps_method wps_method
,
3478 int go_intent
, const u8
*own_interface_addr
,
3479 unsigned int force_freq
, int persistent_group
,
3480 struct wpa_ssid
*ssid
, unsigned int pref_freq
)
3482 if (persistent_group
&& wpa_s
->conf
->persistent_reconnect
)
3483 persistent_group
= 2;
3485 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
3488 return p2p_authorize(wpa_s
->global
->p2p
, peer_addr
, wps_method
,
3489 go_intent
, own_interface_addr
, force_freq
,
3490 persistent_group
, ssid
? ssid
->ssid
: NULL
,
3491 ssid
? ssid
->ssid_len
: 0, pref_freq
);
3495 static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant
*wpa_s
)
3497 wpa_s
->p2p_join_scan_count
++;
3498 wpa_printf(MSG_DEBUG
, "P2P: Join scan attempt %d",
3499 wpa_s
->p2p_join_scan_count
);
3500 if (wpa_s
->p2p_join_scan_count
> P2P_MAX_JOIN_SCAN_ATTEMPTS
) {
3501 wpa_printf(MSG_DEBUG
, "P2P: Failed to find GO " MACSTR
3502 " for join operationg - stop join attempt",
3503 MAC2STR(wpa_s
->pending_join_iface_addr
));
3504 eloop_cancel_timeout(wpas_p2p_join_scan
, wpa_s
, NULL
);
3505 if (wpa_s
->p2p_auto_pd
) {
3506 wpa_s
->p2p_auto_pd
= 0;
3507 wpa_msg_global(wpa_s
, MSG_INFO
,
3508 P2P_EVENT_PROV_DISC_FAILURE
3509 " p2p_dev_addr=" MACSTR
" status=N/A",
3510 MAC2STR(wpa_s
->pending_join_dev_addr
));
3513 wpa_msg_global(wpa_s
->parent
, MSG_INFO
,
3514 P2P_EVENT_GROUP_FORMATION_FAILURE
);
3519 static int wpas_check_freq_conflict(struct wpa_supplicant
*wpa_s
, int freq
)
3521 int *freqs
, res
, num
, i
;
3523 if (wpas_p2p_num_unused_channels(wpa_s
) > 0) {
3524 /* Multiple channels are supported and not all are in use */
3528 freqs
= os_calloc(wpa_s
->num_multichan_concurrent
, sizeof(int));
3532 num
= wpas_p2p_valid_oper_freqs(wpa_s
, freqs
,
3533 wpa_s
->num_multichan_concurrent
);
3539 for (i
= 0; i
< num
; i
++) {
3540 if (freqs
[i
] == freq
) {
3541 wpa_printf(MSG_DEBUG
, "P2P: Frequency %d MHz in use by another virtual interface and can be used",
3556 static int wpas_p2p_peer_go(struct wpa_supplicant
*wpa_s
,
3557 const u8
*peer_dev_addr
)
3559 struct wpa_bss
*bss
;
3562 bss
= wpa_bss_get_p2p_dev_addr(wpa_s
, peer_dev_addr
);
3565 if (bss
->last_update_idx
< wpa_s
->bss_update_idx
) {
3566 wpa_printf(MSG_DEBUG
, "P2P: Peer BSS entry not updated in the "
3571 updated
= os_time_before(&wpa_s
->p2p_auto_started
, &bss
->last_update
);
3572 wpa_printf(MSG_DEBUG
, "P2P: Current BSS entry for peer updated at "
3573 "%ld.%06ld (%supdated in last scan)",
3574 bss
->last_update
.sec
, bss
->last_update
.usec
,
3575 updated
? "": "not ");
3581 static void wpas_p2p_scan_res_join(struct wpa_supplicant
*wpa_s
,
3582 struct wpa_scan_results
*scan_res
)
3584 struct wpa_bss
*bss
;
3586 u8 iface_addr
[ETH_ALEN
];
3588 eloop_cancel_timeout(wpas_p2p_join_scan
, wpa_s
, NULL
);
3590 if (wpa_s
->global
->p2p_disabled
)
3593 wpa_printf(MSG_DEBUG
, "P2P: Scan results received (%d BSS) for %sjoin",
3594 scan_res
? (int) scan_res
->num
: -1,
3595 wpa_s
->p2p_auto_join
? "auto_" : "");
3598 wpas_p2p_scan_res_handler(wpa_s
, scan_res
);
3600 if (wpa_s
->p2p_auto_pd
) {
3601 int join
= wpas_p2p_peer_go(wpa_s
,
3602 wpa_s
->pending_join_dev_addr
);
3604 wpa_s
->auto_pd_scan_retry
< P2P_AUTO_PD_SCAN_ATTEMPTS
) {
3605 wpa_s
->auto_pd_scan_retry
++;
3606 bss
= wpa_bss_get_bssid_latest(
3607 wpa_s
, wpa_s
->pending_join_dev_addr
);
3610 wpa_printf(MSG_DEBUG
, "P2P: Scan retry %d for "
3611 "the peer " MACSTR
" at %d MHz",
3612 wpa_s
->auto_pd_scan_retry
,
3614 pending_join_dev_addr
),
3616 wpas_p2p_join_scan_req(wpa_s
, freq
);
3624 wpa_s
->p2p_auto_pd
= 0;
3625 wpa_s
->pending_pd_use
= join
? AUTO_PD_JOIN
: AUTO_PD_GO_NEG
;
3626 wpa_printf(MSG_DEBUG
, "P2P: Auto PD with " MACSTR
" join=%d",
3627 MAC2STR(wpa_s
->pending_join_dev_addr
), join
);
3628 if (p2p_prov_disc_req(wpa_s
->global
->p2p
,
3629 wpa_s
->pending_join_dev_addr
,
3630 wpa_s
->pending_pd_config_methods
, join
,
3631 0, wpa_s
->user_initiated_pd
) < 0) {
3632 wpa_s
->p2p_auto_pd
= 0;
3633 wpa_msg_global(wpa_s
, MSG_INFO
,
3634 P2P_EVENT_PROV_DISC_FAILURE
3635 " p2p_dev_addr=" MACSTR
" status=N/A",
3636 MAC2STR(wpa_s
->pending_join_dev_addr
));
3641 if (wpa_s
->p2p_auto_join
) {
3642 int join
= wpas_p2p_peer_go(wpa_s
,
3643 wpa_s
->pending_join_dev_addr
);
3645 wpa_printf(MSG_DEBUG
, "P2P: Peer was not found to be "
3646 "running a GO -> use GO Negotiation");
3647 wpas_p2p_connect(wpa_s
, wpa_s
->pending_join_dev_addr
,
3648 wpa_s
->p2p_pin
, wpa_s
->p2p_wps_method
,
3649 wpa_s
->p2p_persistent_group
, 0, 0, 0,
3650 wpa_s
->p2p_go_intent
,
3651 wpa_s
->p2p_connect_freq
,
3652 wpa_s
->p2p_persistent_id
,
3653 wpa_s
->p2p_pd_before_go_neg
,
3654 wpa_s
->p2p_go_ht40
);
3658 wpa_printf(MSG_DEBUG
, "P2P: Peer was found running GO%s -> "
3659 "try to join the group", join
? "" :
3662 wpa_s
->p2p_fallback_to_go_neg
= 1;
3665 freq
= p2p_get_oper_freq(wpa_s
->global
->p2p
,
3666 wpa_s
->pending_join_iface_addr
);
3668 p2p_get_interface_addr(wpa_s
->global
->p2p
,
3669 wpa_s
->pending_join_dev_addr
,
3671 os_memcmp(iface_addr
, wpa_s
->pending_join_dev_addr
, ETH_ALEN
) != 0)
3673 wpa_printf(MSG_DEBUG
, "P2P: Overwrite pending interface "
3674 "address for join from " MACSTR
" to " MACSTR
3675 " based on newly discovered P2P peer entry",
3676 MAC2STR(wpa_s
->pending_join_iface_addr
),
3677 MAC2STR(iface_addr
));
3678 os_memcpy(wpa_s
->pending_join_iface_addr
, iface_addr
,
3681 freq
= p2p_get_oper_freq(wpa_s
->global
->p2p
,
3682 wpa_s
->pending_join_iface_addr
);
3685 wpa_printf(MSG_DEBUG
, "P2P: Target GO operating frequency "
3686 "from P2P peer table: %d MHz", freq
);
3688 bss
= wpa_bss_get_bssid_latest(wpa_s
, wpa_s
->pending_join_iface_addr
);
3691 wpa_printf(MSG_DEBUG
, "P2P: Target GO operating frequency "
3692 "from BSS table: %d MHz", freq
);
3697 if (wpas_check_freq_conflict(wpa_s
, freq
) > 0) {
3698 wpa_msg_global(wpa_s
->parent
, MSG_INFO
,
3699 P2P_EVENT_GROUP_FORMATION_FAILURE
3700 "reason=FREQ_CONFLICT");
3704 wpa_printf(MSG_DEBUG
, "P2P: Send Provision Discovery Request "
3705 "prior to joining an existing group (GO " MACSTR
3707 MAC2STR(wpa_s
->pending_join_dev_addr
), freq
);
3708 wpa_s
->pending_pd_before_join
= 1;
3710 switch (wpa_s
->pending_join_wps_method
) {
3711 case WPS_PIN_DISPLAY
:
3712 method
= WPS_CONFIG_KEYPAD
;
3714 case WPS_PIN_KEYPAD
:
3715 method
= WPS_CONFIG_DISPLAY
;
3718 method
= WPS_CONFIG_PUSHBUTTON
;
3725 if ((p2p_get_provisioning_info(wpa_s
->global
->p2p
,
3726 wpa_s
->pending_join_dev_addr
) ==
3729 * We have already performed provision discovery for
3730 * joining the group. Proceed directly to join
3731 * operation without duplicated provision discovery. */
3732 wpa_printf(MSG_DEBUG
, "P2P: Provision discovery "
3733 "with " MACSTR
" already done - proceed to "
3735 MAC2STR(wpa_s
->pending_join_dev_addr
));
3736 wpa_s
->pending_pd_before_join
= 0;
3740 if (p2p_prov_disc_req(wpa_s
->global
->p2p
,
3741 wpa_s
->pending_join_dev_addr
, method
, 1,
3742 freq
, wpa_s
->user_initiated_pd
) < 0) {
3743 wpa_printf(MSG_DEBUG
, "P2P: Failed to send Provision "
3744 "Discovery Request before joining an "
3746 wpa_s
->pending_pd_before_join
= 0;
3752 wpa_printf(MSG_DEBUG
, "P2P: Failed to find BSS/GO - try again later");
3753 eloop_cancel_timeout(wpas_p2p_join_scan
, wpa_s
, NULL
);
3754 eloop_register_timeout(1, 0, wpas_p2p_join_scan
, wpa_s
, NULL
);
3755 wpas_p2p_check_join_scan_limit(wpa_s
);
3759 /* Start join operation immediately */
3760 wpas_p2p_join_start(wpa_s
);
3764 static void wpas_p2p_join_scan_req(struct wpa_supplicant
*wpa_s
, int freq
)
3767 struct wpa_driver_scan_params params
;
3768 struct wpabuf
*wps_ie
, *ies
;
3770 int freqs
[2] = { 0, 0 };
3772 os_memset(¶ms
, 0, sizeof(params
));
3774 /* P2P Wildcard SSID */
3775 params
.num_ssids
= 1;
3776 params
.ssids
[0].ssid
= (u8
*) P2P_WILDCARD_SSID
;
3777 params
.ssids
[0].ssid_len
= P2P_WILDCARD_SSID_LEN
;
3779 wpa_s
->wps
->dev
.p2p
= 1;
3780 wps_ie
= wps_build_probe_req_ie(DEV_PW_DEFAULT
, &wpa_s
->wps
->dev
,
3781 wpa_s
->wps
->uuid
, WPS_REQ_ENROLLEE
, 0,
3783 if (wps_ie
== NULL
) {
3784 wpas_p2p_scan_res_join(wpa_s
, NULL
);
3788 ielen
= p2p_scan_ie_buf_len(wpa_s
->global
->p2p
);
3789 ies
= wpabuf_alloc(wpabuf_len(wps_ie
) + ielen
);
3791 wpabuf_free(wps_ie
);
3792 wpas_p2p_scan_res_join(wpa_s
, NULL
);
3795 wpabuf_put_buf(ies
, wps_ie
);
3796 wpabuf_free(wps_ie
);
3798 p2p_scan_ie(wpa_s
->global
->p2p
, ies
, NULL
);
3800 params
.p2p_probe
= 1;
3801 params
.extra_ies
= wpabuf_head(ies
);
3802 params
.extra_ies_len
= wpabuf_len(ies
);
3805 params
.freqs
= freqs
;
3809 * Run a scan to update BSS table and start Provision Discovery once
3810 * the new scan results become available.
3812 ret
= wpa_drv_scan(wpa_s
, ¶ms
);
3814 os_get_time(&wpa_s
->scan_trigger_time
);
3815 wpa_s
->scan_res_handler
= wpas_p2p_scan_res_join
;
3821 wpa_printf(MSG_DEBUG
, "P2P: Failed to start scan for join - "
3823 eloop_cancel_timeout(wpas_p2p_join_scan
, wpa_s
, NULL
);
3824 eloop_register_timeout(1, 0, wpas_p2p_join_scan
, wpa_s
, NULL
);
3825 wpas_p2p_check_join_scan_limit(wpa_s
);
3830 static void wpas_p2p_join_scan(void *eloop_ctx
, void *timeout_ctx
)
3832 struct wpa_supplicant
*wpa_s
= eloop_ctx
;
3833 wpas_p2p_join_scan_req(wpa_s
, 0);
3837 static int wpas_p2p_join(struct wpa_supplicant
*wpa_s
, const u8
*iface_addr
,
3838 const u8
*dev_addr
, enum p2p_wps_method wps_method
,
3841 wpa_printf(MSG_DEBUG
, "P2P: Request to join existing group (iface "
3842 MACSTR
" dev " MACSTR
")%s",
3843 MAC2STR(iface_addr
), MAC2STR(dev_addr
),
3844 auto_join
? " (auto_join)" : "");
3846 wpa_s
->p2p_auto_pd
= 0;
3847 wpa_s
->p2p_auto_join
= !!auto_join
;
3848 os_memcpy(wpa_s
->pending_join_iface_addr
, iface_addr
, ETH_ALEN
);
3849 os_memcpy(wpa_s
->pending_join_dev_addr
, dev_addr
, ETH_ALEN
);
3850 wpa_s
->pending_join_wps_method
= wps_method
;
3852 /* Make sure we are not running find during connection establishment */
3853 wpas_p2p_stop_find(wpa_s
);
3855 wpa_s
->p2p_join_scan_count
= 0;
3856 wpas_p2p_join_scan(wpa_s
, NULL
);
3861 static int wpas_p2p_join_start(struct wpa_supplicant
*wpa_s
)
3863 struct wpa_supplicant
*group
;
3864 struct p2p_go_neg_results res
;
3865 struct wpa_bss
*bss
;
3867 group
= wpas_p2p_get_group_iface(wpa_s
, 0, 0);
3870 if (group
!= wpa_s
) {
3871 os_memcpy(group
->p2p_pin
, wpa_s
->p2p_pin
,
3872 sizeof(group
->p2p_pin
));
3873 group
->p2p_wps_method
= wpa_s
->p2p_wps_method
;
3876 * Need to mark the current interface for p2p_group_formation
3877 * when a separate group interface is not used. This is needed
3878 * to allow p2p_cancel stop a pending p2p_connect-join.
3879 * wpas_p2p_init_group_interface() addresses this for the case
3880 * where a separate group interface is used.
3882 wpa_s
->global
->p2p_group_formation
= wpa_s
;
3885 group
->p2p_in_provisioning
= 1;
3886 group
->p2p_fallback_to_go_neg
= wpa_s
->p2p_fallback_to_go_neg
;
3888 os_memset(&res
, 0, sizeof(res
));
3889 os_memcpy(res
.peer_interface_addr
, wpa_s
->pending_join_iface_addr
,
3891 res
.wps_method
= wpa_s
->pending_join_wps_method
;
3892 bss
= wpa_bss_get_bssid_latest(wpa_s
, wpa_s
->pending_join_iface_addr
);
3894 res
.freq
= bss
->freq
;
3895 res
.ssid_len
= bss
->ssid_len
;
3896 os_memcpy(res
.ssid
, bss
->ssid
, bss
->ssid_len
);
3899 if (wpa_s
->off_channel_freq
|| wpa_s
->roc_waiting_drv_freq
) {
3900 wpa_printf(MSG_DEBUG
, "P2P: Cancel remain-on-channel prior to "
3902 wpa_drv_cancel_remain_on_channel(wpa_s
);
3903 wpa_s
->off_channel_freq
= 0;
3904 wpa_s
->roc_waiting_drv_freq
= 0;
3906 wpas_start_wps_enrollee(group
, &res
);
3909 * Allow a longer timeout for join-a-running-group than normal 15
3910 * second group formation timeout since the GO may not have authorized
3911 * our connection yet.
3913 eloop_cancel_timeout(wpas_p2p_group_formation_timeout
, wpa_s
, NULL
);
3914 eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout
,
3921 static int wpas_p2p_setup_freqs(struct wpa_supplicant
*wpa_s
, int freq
,
3922 int *force_freq
, int *pref_freq
)
3925 unsigned int freq_in_use
= 0, num
, i
;
3927 freqs
= os_calloc(wpa_s
->num_multichan_concurrent
, sizeof(int));
3931 num
= get_shared_radio_freqs(wpa_s
, freqs
,
3932 wpa_s
->num_multichan_concurrent
);
3933 wpa_printf(MSG_DEBUG
,
3934 "P2P: Setup freqs: freq=%d num_MCC=%d shared_freqs=%u",
3935 freq
, wpa_s
->num_multichan_concurrent
, num
);
3938 if (!p2p_supported_freq(wpa_s
->global
->p2p
, freq
)) {
3939 wpa_printf(MSG_DEBUG
, "P2P: The forced channel "
3940 "(%u MHz) is not supported for P2P uses",
3946 for (i
= 0; i
< num
; i
++) {
3947 if (freqs
[i
] == freq
)
3951 if (num
== wpa_s
->num_multichan_concurrent
&& !freq_in_use
) {
3952 wpa_printf(MSG_DEBUG
, "P2P: Cannot start P2P group on %u MHz as there are no available channels",
3957 wpa_printf(MSG_DEBUG
, "P2P: Trying to force us to use the "
3958 "requested channel (%u MHz)", freq
);
3963 for (i
= 0; i
< num
; i
++) {
3964 if (!p2p_supported_freq(wpa_s
->global
->p2p
, freqs
[i
]))
3967 wpa_printf(MSG_DEBUG
, "P2P: Try to force us to use frequency (%u MHz) which is already in use",
3969 *force_freq
= freqs
[i
];
3971 if (*pref_freq
== 0 && num
< wpa_s
->num_multichan_concurrent
) {
3972 wpa_printf(MSG_DEBUG
, "P2P: Try to prefer a frequency we are already using");
3973 *pref_freq
= *force_freq
;
3979 if (num
< wpa_s
->num_multichan_concurrent
) {
3980 wpa_printf(MSG_DEBUG
, "P2P: Current operating channels are not available for P2P. Try to use another channel");
3983 wpa_printf(MSG_DEBUG
, "P2P: All channels are in use and none of them are P2P enabled. Cannot start P2P group");
3998 * wpas_p2p_connect - Request P2P Group Formation to be started
3999 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4000 * @peer_addr: Address of the peer P2P Device
4001 * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
4002 * @persistent_group: Whether to create a persistent group
4003 * @auto_join: Whether to select join vs. GO Negotiation automatically
4004 * @join: Whether to join an existing group (as a client) instead of starting
4005 * Group Owner negotiation; @peer_addr is BSSID in that case
4006 * @auth: Whether to only authorize the connection instead of doing that and
4007 * initiating Group Owner negotiation
4008 * @go_intent: GO Intent or -1 to use default
4009 * @freq: Frequency for the group or 0 for auto-selection
4010 * @persistent_id: Persistent group credentials to use for forcing GO
4011 * parameters or -1 to generate new values (SSID/passphrase)
4012 * @pd: Whether to send Provision Discovery prior to GO Negotiation as an
4013 * interoperability workaround when initiating group formation
4014 * @ht40: Start GO with 40 MHz channel width
4015 * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
4016 * failure, -2 on failure due to channel not currently available,
4017 * -3 if forced channel is not supported
4019 int wpas_p2p_connect(struct wpa_supplicant
*wpa_s
, const u8
*peer_addr
,
4020 const char *pin
, enum p2p_wps_method wps_method
,
4021 int persistent_group
, int auto_join
, int join
, int auth
,
4022 int go_intent
, int freq
, int persistent_id
, int pd
,
4025 int force_freq
= 0, pref_freq
= 0;
4027 enum wpa_driver_if_type iftype
;
4029 struct wpa_ssid
*ssid
= NULL
;
4031 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
4034 if (persistent_id
>= 0) {
4035 ssid
= wpa_config_get_network(wpa_s
->conf
, persistent_id
);
4036 if (ssid
== NULL
|| ssid
->disabled
!= 2 ||
4037 ssid
->mode
!= WPAS_MODE_P2P_GO
)
4041 os_free(wpa_s
->global
->add_psk
);
4042 wpa_s
->global
->add_psk
= NULL
;
4045 go_intent
= wpa_s
->conf
->p2p_go_intent
;
4048 wpa_s
->p2p_long_listen
= 0;
4050 wpa_s
->p2p_wps_method
= wps_method
;
4051 wpa_s
->p2p_persistent_group
= !!persistent_group
;
4052 wpa_s
->p2p_persistent_id
= persistent_id
;
4053 wpa_s
->p2p_go_intent
= go_intent
;
4054 wpa_s
->p2p_connect_freq
= freq
;
4055 wpa_s
->p2p_fallback_to_go_neg
= 0;
4056 wpa_s
->p2p_pd_before_go_neg
= !!pd
;
4057 wpa_s
->p2p_go_ht40
= !!ht40
;
4060 os_strlcpy(wpa_s
->p2p_pin
, pin
, sizeof(wpa_s
->p2p_pin
));
4061 else if (wps_method
== WPS_PIN_DISPLAY
) {
4062 ret
= wps_generate_pin();
4063 os_snprintf(wpa_s
->p2p_pin
, sizeof(wpa_s
->p2p_pin
), "%08d",
4065 wpa_printf(MSG_DEBUG
, "P2P: Randomly generated PIN: %s",
4068 wpa_s
->p2p_pin
[0] = '\0';
4070 if (join
|| auto_join
) {
4071 u8 iface_addr
[ETH_ALEN
], dev_addr
[ETH_ALEN
];
4073 wpa_printf(MSG_DEBUG
, "P2P: Authorize invitation to "
4074 "connect a running group from " MACSTR
,
4075 MAC2STR(peer_addr
));
4076 os_memcpy(wpa_s
->p2p_auth_invite
, peer_addr
, ETH_ALEN
);
4079 os_memcpy(dev_addr
, peer_addr
, ETH_ALEN
);
4080 if (p2p_get_interface_addr(wpa_s
->global
->p2p
, peer_addr
,
4082 os_memcpy(iface_addr
, peer_addr
, ETH_ALEN
);
4083 p2p_get_dev_addr(wpa_s
->global
->p2p
, peer_addr
,
4087 os_get_time(&wpa_s
->p2p_auto_started
);
4088 wpa_printf(MSG_DEBUG
, "P2P: Auto join started at "
4090 wpa_s
->p2p_auto_started
.sec
,
4091 wpa_s
->p2p_auto_started
.usec
);
4093 wpa_s
->user_initiated_pd
= 1;
4094 if (wpas_p2p_join(wpa_s
, iface_addr
, dev_addr
, wps_method
,
4100 res
= wpas_p2p_setup_freqs(wpa_s
, freq
, &force_freq
, &pref_freq
);
4103 wpas_p2p_set_own_freq_preference(wpa_s
, force_freq
);
4105 wpa_s
->create_p2p_iface
= wpas_p2p_create_iface(wpa_s
);
4107 if (wpa_s
->create_p2p_iface
) {
4108 /* Prepare to add a new interface for the group */
4109 iftype
= WPA_IF_P2P_GROUP
;
4110 if (go_intent
== 15)
4111 iftype
= WPA_IF_P2P_GO
;
4112 if (wpas_p2p_add_group_interface(wpa_s
, iftype
) < 0) {
4113 wpa_printf(MSG_ERROR
, "P2P: Failed to allocate a new "
4114 "interface for the group");
4118 if_addr
= wpa_s
->pending_interface_addr
;
4120 if_addr
= wpa_s
->own_addr
;
4123 if (wpas_p2p_auth_go_neg(wpa_s
, peer_addr
, wps_method
,
4125 force_freq
, persistent_group
, ssid
,
4131 if (wpas_p2p_start_go_neg(wpa_s
, peer_addr
, wps_method
,
4132 go_intent
, if_addr
, force_freq
,
4133 persistent_group
, ssid
, pref_freq
) < 0) {
4134 if (wpa_s
->create_p2p_iface
)
4135 wpas_p2p_remove_pending_group_interface(wpa_s
);
4143 * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
4144 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4145 * @freq: Frequency of the channel in MHz
4146 * @duration: Duration of the stay on the channel in milliseconds
4148 * This callback is called when the driver indicates that it has started the
4149 * requested remain-on-channel duration.
4151 void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant
*wpa_s
,
4152 unsigned int freq
, unsigned int duration
)
4154 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
4156 if (wpa_s
->off_channel_freq
== wpa_s
->pending_listen_freq
) {
4157 p2p_listen_cb(wpa_s
->global
->p2p
, wpa_s
->pending_listen_freq
,
4158 wpa_s
->pending_listen_duration
);
4159 wpa_s
->pending_listen_freq
= 0;
4164 static int wpas_p2p_listen_start(struct wpa_supplicant
*wpa_s
,
4165 unsigned int timeout
)
4167 /* Limit maximum Listen state time based on driver limitation. */
4168 if (timeout
> wpa_s
->max_remain_on_chan
)
4169 timeout
= wpa_s
->max_remain_on_chan
;
4171 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
4172 return wpa_drv_p2p_listen(wpa_s
, timeout
);
4174 return p2p_listen(wpa_s
->global
->p2p
, timeout
);
4179 * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
4180 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4181 * @freq: Frequency of the channel in MHz
4183 * This callback is called when the driver indicates that a remain-on-channel
4184 * operation has been completed, i.e., the duration on the requested channel
4187 void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant
*wpa_s
,
4190 wpa_printf(MSG_DEBUG
, "P2P: Cancel remain-on-channel callback "
4191 "(p2p_long_listen=%d ms pending_action_tx=%p)",
4192 wpa_s
->p2p_long_listen
, offchannel_pending_action_tx(wpa_s
));
4193 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
4195 if (p2p_listen_end(wpa_s
->global
->p2p
, freq
) > 0)
4196 return; /* P2P module started a new operation */
4197 if (offchannel_pending_action_tx(wpa_s
))
4199 if (wpa_s
->p2p_long_listen
> 0)
4200 wpa_s
->p2p_long_listen
-= wpa_s
->max_remain_on_chan
;
4201 if (wpa_s
->p2p_long_listen
> 0) {
4202 wpa_printf(MSG_DEBUG
, "P2P: Continuing long Listen state");
4203 wpas_p2p_listen_start(wpa_s
, wpa_s
->p2p_long_listen
);
4206 * When listen duration is over, stop listen & update p2p_state
4209 p2p_stop_listen(wpa_s
->global
->p2p
);
4215 * wpas_p2p_group_remove - Remove a P2P group
4216 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4217 * @ifname: Network interface name of the group interface or "*" to remove all
4219 * Returns: 0 on success, -1 on failure
4221 * This function is used to remove a P2P group. This can be used to disconnect
4222 * from a group in which the local end is a P2P Client or to end a P2P Group in
4223 * case the local end is the Group Owner. If a virtual network interface was
4224 * created for this group, that interface will be removed. Otherwise, only the
4225 * configured P2P group network will be removed from the interface.
4227 int wpas_p2p_group_remove(struct wpa_supplicant
*wpa_s
, const char *ifname
)
4229 struct wpa_global
*global
= wpa_s
->global
;
4231 if (os_strcmp(ifname
, "*") == 0) {
4232 struct wpa_supplicant
*prev
;
4233 wpa_s
= global
->ifaces
;
4236 wpa_s
= wpa_s
->next
;
4237 if (prev
->p2p_group_interface
!=
4238 NOT_P2P_GROUP_INTERFACE
||
4239 (prev
->current_ssid
&&
4240 prev
->current_ssid
->p2p_group
))
4241 wpas_p2p_disconnect(prev
);
4246 for (wpa_s
= global
->ifaces
; wpa_s
; wpa_s
= wpa_s
->next
) {
4247 if (os_strcmp(wpa_s
->ifname
, ifname
) == 0)
4251 return wpas_p2p_disconnect(wpa_s
);
4255 static int wpas_p2p_select_go_freq(struct wpa_supplicant
*wpa_s
, int freq
)
4260 wpa_printf(MSG_DEBUG
, "P2P: Request to start GO on 2.4 GHz "
4262 if (wpa_s
->best_24_freq
> 0 &&
4263 p2p_supported_freq(wpa_s
->global
->p2p
,
4264 wpa_s
->best_24_freq
)) {
4265 freq
= wpa_s
->best_24_freq
;
4266 wpa_printf(MSG_DEBUG
, "P2P: Use best 2.4 GHz band "
4267 "channel: %d MHz", freq
);
4269 os_get_random((u8
*) &r
, sizeof(r
));
4270 freq
= 2412 + (r
% 3) * 25;
4271 wpa_printf(MSG_DEBUG
, "P2P: Use random 2.4 GHz band "
4272 "channel: %d MHz", freq
);
4277 wpa_printf(MSG_DEBUG
, "P2P: Request to start GO on 5 GHz "
4279 if (wpa_s
->best_5_freq
> 0 &&
4280 p2p_supported_freq(wpa_s
->global
->p2p
,
4281 wpa_s
->best_5_freq
)) {
4282 freq
= wpa_s
->best_5_freq
;
4283 wpa_printf(MSG_DEBUG
, "P2P: Use best 5 GHz band "
4284 "channel: %d MHz", freq
);
4286 os_get_random((u8
*) &r
, sizeof(r
));
4287 freq
= 5180 + (r
% 4) * 20;
4288 if (!p2p_supported_freq(wpa_s
->global
->p2p
, freq
)) {
4289 wpa_printf(MSG_DEBUG
, "P2P: Could not select "
4290 "5 GHz channel for P2P group");
4293 wpa_printf(MSG_DEBUG
, "P2P: Use random 5 GHz band "
4294 "channel: %d MHz", freq
);
4298 if (freq
> 0 && !p2p_supported_freq(wpa_s
->global
->p2p
, freq
)) {
4299 wpa_printf(MSG_DEBUG
, "P2P: The forced channel for GO "
4300 "(%u MHz) is not supported for P2P uses",
4309 static int wpas_p2p_init_go_params(struct wpa_supplicant
*wpa_s
,
4310 struct p2p_go_neg_results
*params
,
4312 const struct p2p_channels
*channels
)
4315 unsigned int pref_freq
;
4316 unsigned int num
, i
;
4318 os_memset(params
, 0, sizeof(*params
));
4319 params
->role_go
= 1;
4320 params
->ht40
= ht40
;
4322 if (!freq_included(channels
, freq
)) {
4323 wpa_printf(MSG_DEBUG
, "P2P: Forced GO freq %d MHz not "
4327 wpa_printf(MSG_DEBUG
, "P2P: Set GO freq based on forced "
4328 "frequency %d MHz", freq
);
4329 params
->freq
= freq
;
4330 } else if (wpa_s
->conf
->p2p_oper_reg_class
== 81 &&
4331 wpa_s
->conf
->p2p_oper_channel
>= 1 &&
4332 wpa_s
->conf
->p2p_oper_channel
<= 11 &&
4333 freq_included(channels
,
4334 2407 + 5 * wpa_s
->conf
->p2p_oper_channel
)) {
4335 params
->freq
= 2407 + 5 * wpa_s
->conf
->p2p_oper_channel
;
4336 wpa_printf(MSG_DEBUG
, "P2P: Set GO freq based on configured "
4337 "frequency %d MHz", params
->freq
);
4338 } else if ((wpa_s
->conf
->p2p_oper_reg_class
== 115 ||
4339 wpa_s
->conf
->p2p_oper_reg_class
== 116 ||
4340 wpa_s
->conf
->p2p_oper_reg_class
== 117 ||
4341 wpa_s
->conf
->p2p_oper_reg_class
== 124 ||
4342 wpa_s
->conf
->p2p_oper_reg_class
== 126 ||
4343 wpa_s
->conf
->p2p_oper_reg_class
== 127) &&
4344 freq_included(channels
,
4345 5000 + 5 * wpa_s
->conf
->p2p_oper_channel
)) {
4346 params
->freq
= 5000 + 5 * wpa_s
->conf
->p2p_oper_channel
;
4347 wpa_printf(MSG_DEBUG
, "P2P: Set GO freq based on configured "
4348 "frequency %d MHz", params
->freq
);
4349 } else if (wpa_s
->conf
->p2p_oper_channel
== 0 &&
4350 wpa_s
->best_overall_freq
> 0 &&
4351 p2p_supported_freq(wpa_s
->global
->p2p
,
4352 wpa_s
->best_overall_freq
) &&
4353 freq_included(channels
, wpa_s
->best_overall_freq
)) {
4354 params
->freq
= wpa_s
->best_overall_freq
;
4355 wpa_printf(MSG_DEBUG
, "P2P: Set GO freq based on best overall "
4356 "channel %d MHz", params
->freq
);
4357 } else if (wpa_s
->conf
->p2p_oper_channel
== 0 &&
4358 wpa_s
->best_24_freq
> 0 &&
4359 p2p_supported_freq(wpa_s
->global
->p2p
,
4360 wpa_s
->best_24_freq
) &&
4361 freq_included(channels
, wpa_s
->best_24_freq
)) {
4362 params
->freq
= wpa_s
->best_24_freq
;
4363 wpa_printf(MSG_DEBUG
, "P2P: Set GO freq based on best 2.4 GHz "
4364 "channel %d MHz", params
->freq
);
4365 } else if (wpa_s
->conf
->p2p_oper_channel
== 0 &&
4366 wpa_s
->best_5_freq
> 0 &&
4367 p2p_supported_freq(wpa_s
->global
->p2p
,
4368 wpa_s
->best_5_freq
) &&
4369 freq_included(channels
, wpa_s
->best_5_freq
)) {
4370 params
->freq
= wpa_s
->best_5_freq
;
4371 wpa_printf(MSG_DEBUG
, "P2P: Set GO freq based on best 5 GHz "
4372 "channel %d MHz", params
->freq
);
4373 } else if ((pref_freq
= p2p_get_pref_freq(wpa_s
->global
->p2p
,
4375 params
->freq
= pref_freq
;
4376 wpa_printf(MSG_DEBUG
, "P2P: Set GO freq %d MHz from preferred "
4377 "channels", params
->freq
);
4380 for (chan
= 0; chan
< 11; chan
++) {
4381 params
->freq
= 2412 + chan
* 5;
4382 if (!wpas_p2p_disallowed_freq(wpa_s
->global
,
4384 freq_included(channels
, params
->freq
))
4388 wpa_printf(MSG_DEBUG
, "P2P: No 2.4 GHz channel "
4392 wpa_printf(MSG_DEBUG
, "P2P: Set GO freq %d MHz (no preference "
4393 "known)", params
->freq
);
4396 freqs
= os_calloc(wpa_s
->num_multichan_concurrent
, sizeof(int));
4400 res
= wpas_p2p_valid_oper_freqs(wpa_s
, freqs
,
4401 wpa_s
->num_multichan_concurrent
);
4409 for (i
= 0; i
< num
; i
++) {
4410 if (freq_included(channels
, freqs
[i
])) {
4411 wpa_printf(MSG_DEBUG
, "P2P: Force GO on a channel we are already using (%u MHz)",
4413 params
->freq
= freqs
[i
];
4419 if (num
== wpa_s
->num_multichan_concurrent
) {
4420 wpa_printf(MSG_DEBUG
, "P2P: Cannot force GO on any of the channels we are already using");
4424 wpa_printf(MSG_DEBUG
, "P2P: Cannot force GO on any of the channels we are already using. Use one of the free channels");
4428 for (i
= 0; i
< num
; i
++) {
4429 if (freqs
[i
] == freq
)
4434 if (num
== wpa_s
->num_multichan_concurrent
) {
4435 wpa_printf(MSG_DEBUG
, "P2P: Cannot force GO on freq (%u MHz) as all the channels are in use", freq
);
4439 wpa_printf(MSG_DEBUG
, "P2P: Cannot force GO on any of the channels we are already using. Use one of the free channels");
4448 static struct wpa_supplicant
*
4449 wpas_p2p_get_group_iface(struct wpa_supplicant
*wpa_s
, int addr_allocated
,
4452 struct wpa_supplicant
*group_wpa_s
;
4454 if (!wpas_p2p_create_iface(wpa_s
)) {
4455 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: Use same interface for group "
4460 if (wpas_p2p_add_group_interface(wpa_s
, go
? WPA_IF_P2P_GO
:
4461 WPA_IF_P2P_CLIENT
) < 0) {
4462 wpa_msg_global(wpa_s
, MSG_ERROR
,
4463 "P2P: Failed to add group interface");
4466 group_wpa_s
= wpas_p2p_init_group_interface(wpa_s
, go
);
4467 if (group_wpa_s
== NULL
) {
4468 wpa_msg_global(wpa_s
, MSG_ERROR
,
4469 "P2P: Failed to initialize group interface");
4470 wpas_p2p_remove_pending_group_interface(wpa_s
);
4474 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: Use separate group interface %s",
4475 group_wpa_s
->ifname
);
4481 * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
4482 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4483 * @persistent_group: Whether to create a persistent group
4484 * @freq: Frequency for the group or 0 to indicate no hardcoding
4485 * Returns: 0 on success, -1 on failure
4487 * This function creates a new P2P group with the local end as the Group Owner,
4488 * i.e., without using Group Owner Negotiation.
4490 int wpas_p2p_group_add(struct wpa_supplicant
*wpa_s
, int persistent_group
,
4493 struct p2p_go_neg_results params
;
4495 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
4498 os_free(wpa_s
->global
->add_psk
);
4499 wpa_s
->global
->add_psk
= NULL
;
4501 /* Make sure we are not running find during connection establishment */
4502 wpa_printf(MSG_DEBUG
, "P2P: Stop any on-going P2P FIND");
4503 wpas_p2p_stop_find_oper(wpa_s
);
4505 freq
= wpas_p2p_select_go_freq(wpa_s
, freq
);
4509 if (wpas_p2p_init_go_params(wpa_s
, ¶ms
, freq
, ht40
, NULL
))
4512 !p2p_supported_freq(wpa_s
->global
->p2p
, params
.freq
)) {
4513 wpa_printf(MSG_DEBUG
, "P2P: The selected channel for GO "
4514 "(%u MHz) is not supported for P2P uses",
4518 p2p_go_params(wpa_s
->global
->p2p
, ¶ms
);
4519 params
.persistent_group
= persistent_group
;
4521 wpa_s
= wpas_p2p_get_group_iface(wpa_s
, 0, 1);
4524 wpas_start_wps_go(wpa_s
, ¶ms
, 0);
4530 static int wpas_start_p2p_client(struct wpa_supplicant
*wpa_s
,
4531 struct wpa_ssid
*params
, int addr_allocated
)
4533 struct wpa_ssid
*ssid
;
4535 wpa_s
= wpas_p2p_get_group_iface(wpa_s
, addr_allocated
, 0);
4538 wpa_s
->p2p_last_4way_hs_fail
= NULL
;
4540 wpa_supplicant_ap_deinit(wpa_s
);
4542 ssid
= wpa_config_add_network(wpa_s
->conf
);
4545 wpa_config_set_network_defaults(ssid
);
4546 ssid
->temporary
= 1;
4547 ssid
->proto
= WPA_PROTO_RSN
;
4548 ssid
->pairwise_cipher
= WPA_CIPHER_CCMP
;
4549 ssid
->group_cipher
= WPA_CIPHER_CCMP
;
4550 ssid
->key_mgmt
= WPA_KEY_MGMT_PSK
;
4551 ssid
->ssid
= os_malloc(params
->ssid_len
);
4552 if (ssid
->ssid
== NULL
) {
4553 wpa_config_remove_network(wpa_s
->conf
, ssid
->id
);
4556 os_memcpy(ssid
->ssid
, params
->ssid
, params
->ssid_len
);
4557 ssid
->ssid_len
= params
->ssid_len
;
4558 ssid
->p2p_group
= 1;
4559 ssid
->export_keys
= 1;
4560 if (params
->psk_set
) {
4561 os_memcpy(ssid
->psk
, params
->psk
, 32);
4564 if (params
->passphrase
)
4565 ssid
->passphrase
= os_strdup(params
->passphrase
);
4567 wpa_supplicant_select_network(wpa_s
, ssid
);
4569 wpa_s
->show_group_started
= 1;
4575 int wpas_p2p_group_add_persistent(struct wpa_supplicant
*wpa_s
,
4576 struct wpa_ssid
*ssid
, int addr_allocated
,
4578 const struct p2p_channels
*channels
)
4580 struct p2p_go_neg_results params
;
4583 if (ssid
->disabled
!= 2 || ssid
->ssid
== NULL
)
4586 if (wpas_get_p2p_group(wpa_s
, ssid
->ssid
, ssid
->ssid_len
, &go
) &&
4587 go
== (ssid
->mode
== WPAS_MODE_P2P_GO
)) {
4588 wpa_printf(MSG_DEBUG
, "P2P: Requested persistent group is "
4593 os_free(wpa_s
->global
->add_psk
);
4594 wpa_s
->global
->add_psk
= NULL
;
4596 /* Make sure we are not running find during connection establishment */
4597 wpas_p2p_stop_find_oper(wpa_s
);
4599 wpa_s
->p2p_fallback_to_go_neg
= 0;
4601 if (ssid
->mode
== WPAS_MODE_INFRA
)
4602 return wpas_start_p2p_client(wpa_s
, ssid
, addr_allocated
);
4604 if (ssid
->mode
!= WPAS_MODE_P2P_GO
)
4607 freq
= wpas_p2p_select_go_freq(wpa_s
, freq
);
4611 if (wpas_p2p_init_go_params(wpa_s
, ¶ms
, freq
, ht40
, channels
))
4615 params
.psk_set
= ssid
->psk_set
;
4617 os_memcpy(params
.psk
, ssid
->psk
, sizeof(params
.psk
));
4618 if (ssid
->passphrase
) {
4619 if (os_strlen(ssid
->passphrase
) >= sizeof(params
.passphrase
)) {
4620 wpa_printf(MSG_ERROR
, "P2P: Invalid passphrase in "
4621 "persistent group");
4624 os_strlcpy(params
.passphrase
, ssid
->passphrase
,
4625 sizeof(params
.passphrase
));
4627 os_memcpy(params
.ssid
, ssid
->ssid
, ssid
->ssid_len
);
4628 params
.ssid_len
= ssid
->ssid_len
;
4629 params
.persistent_group
= 1;
4631 wpa_s
= wpas_p2p_get_group_iface(wpa_s
, addr_allocated
, 1);
4635 wpas_start_wps_go(wpa_s
, ¶ms
, 0);
4641 static void wpas_p2p_ie_update(void *ctx
, struct wpabuf
*beacon_ies
,
4642 struct wpabuf
*proberesp_ies
)
4644 struct wpa_supplicant
*wpa_s
= ctx
;
4645 if (wpa_s
->ap_iface
) {
4646 struct hostapd_data
*hapd
= wpa_s
->ap_iface
->bss
[0];
4647 if (!(hapd
->conf
->p2p
& P2P_GROUP_OWNER
)) {
4648 wpabuf_free(beacon_ies
);
4649 wpabuf_free(proberesp_ies
);
4653 wpabuf_free(hapd
->p2p_beacon_ie
);
4654 hapd
->p2p_beacon_ie
= beacon_ies
;
4656 wpabuf_free(hapd
->p2p_probe_resp_ie
);
4657 hapd
->p2p_probe_resp_ie
= proberesp_ies
;
4659 wpabuf_free(beacon_ies
);
4660 wpabuf_free(proberesp_ies
);
4662 wpa_supplicant_ap_update_beacon(wpa_s
);
4666 static void wpas_p2p_idle_update(void *ctx
, int idle
)
4668 struct wpa_supplicant
*wpa_s
= ctx
;
4669 if (!wpa_s
->ap_iface
)
4671 wpa_printf(MSG_DEBUG
, "P2P: GO - group %sidle", idle
? "" : "not ");
4673 wpas_p2p_set_group_idle_timeout(wpa_s
);
4675 eloop_cancel_timeout(wpas_p2p_group_idle_timeout
, wpa_s
, NULL
);
4679 struct p2p_group
* wpas_p2p_group_init(struct wpa_supplicant
*wpa_s
,
4680 struct wpa_ssid
*ssid
)
4682 struct p2p_group
*group
;
4683 struct p2p_group_config
*cfg
;
4685 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
4687 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
4690 cfg
= os_zalloc(sizeof(*cfg
));
4694 if (ssid
->p2p_persistent_group
&& wpa_s
->conf
->persistent_reconnect
)
4695 cfg
->persistent_group
= 2;
4696 else if (ssid
->p2p_persistent_group
)
4697 cfg
->persistent_group
= 1;
4698 os_memcpy(cfg
->interface_addr
, wpa_s
->own_addr
, ETH_ALEN
);
4699 if (wpa_s
->max_stations
&&
4700 wpa_s
->max_stations
< wpa_s
->conf
->max_num_sta
)
4701 cfg
->max_clients
= wpa_s
->max_stations
;
4703 cfg
->max_clients
= wpa_s
->conf
->max_num_sta
;
4704 os_memcpy(cfg
->ssid
, ssid
->ssid
, ssid
->ssid_len
);
4705 cfg
->ssid_len
= ssid
->ssid_len
;
4706 cfg
->cb_ctx
= wpa_s
;
4707 cfg
->ie_update
= wpas_p2p_ie_update
;
4708 cfg
->idle_update
= wpas_p2p_idle_update
;
4710 group
= p2p_group_init(wpa_s
->global
->p2p
, cfg
);
4713 if (ssid
->mode
!= WPAS_MODE_P2P_GROUP_FORMATION
)
4714 p2p_group_notif_formation_done(group
);
4715 wpa_s
->p2p_group
= group
;
4720 void wpas_p2p_wps_success(struct wpa_supplicant
*wpa_s
, const u8
*peer_addr
,
4723 struct wpa_ssid
*ssid
= wpa_s
->current_ssid
;
4725 if (!wpa_s
->p2p_in_provisioning
) {
4726 wpa_printf(MSG_DEBUG
, "P2P: Ignore WPS success event - P2P "
4727 "provisioning not in progress");
4731 if (ssid
&& ssid
->mode
== WPAS_MODE_INFRA
) {
4732 u8 go_dev_addr
[ETH_ALEN
];
4733 os_memcpy(go_dev_addr
, wpa_s
->bssid
, ETH_ALEN
);
4734 wpas_p2p_persistent_group(wpa_s
, go_dev_addr
, ssid
->ssid
,
4736 /* Clear any stored provisioning info */
4737 p2p_clear_provisioning_info(wpa_s
->global
->p2p
, go_dev_addr
);
4740 eloop_cancel_timeout(wpas_p2p_group_formation_timeout
, wpa_s
->parent
,
4742 if (ssid
&& ssid
->mode
== WPAS_MODE_INFRA
) {
4744 * Use a separate timeout for initial data connection to
4745 * complete to allow the group to be removed automatically if
4746 * something goes wrong in this step before the P2P group idle
4747 * timeout mechanism is taken into use.
4749 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT
, 0,
4750 wpas_p2p_group_formation_timeout
,
4751 wpa_s
->parent
, NULL
);
4753 if (wpa_s
->global
->p2p
)
4754 p2p_wps_success_cb(wpa_s
->global
->p2p
, peer_addr
);
4755 else if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
4756 wpa_drv_wps_success_cb(wpa_s
, peer_addr
);
4757 wpas_group_formation_completed(wpa_s
, 1);
4761 void wpas_p2p_wps_failed(struct wpa_supplicant
*wpa_s
,
4762 struct wps_event_fail
*fail
)
4764 if (!wpa_s
->p2p_in_provisioning
) {
4765 wpa_printf(MSG_DEBUG
, "P2P: Ignore WPS fail event - P2P "
4766 "provisioning not in progress");
4770 if (wpa_s
->go_params
) {
4771 p2p_clear_provisioning_info(
4773 wpa_s
->go_params
->peer_device_addr
);
4776 wpas_notify_p2p_wps_failed(wpa_s
, fail
);
4780 int wpas_p2p_prov_disc(struct wpa_supplicant
*wpa_s
, const u8
*peer_addr
,
4781 const char *config_method
,
4782 enum wpas_p2p_prov_disc_use use
)
4786 wpa_s
->p2p_fallback_to_go_neg
= 0;
4787 wpa_s
->pending_pd_use
= NORMAL_PD
;
4788 if (os_strncmp(config_method
, "display", 7) == 0)
4789 config_methods
= WPS_CONFIG_DISPLAY
;
4790 else if (os_strncmp(config_method
, "keypad", 6) == 0)
4791 config_methods
= WPS_CONFIG_KEYPAD
;
4792 else if (os_strncmp(config_method
, "pbc", 3) == 0 ||
4793 os_strncmp(config_method
, "pushbutton", 10) == 0)
4794 config_methods
= WPS_CONFIG_PUSHBUTTON
;
4796 wpa_printf(MSG_DEBUG
, "P2P: Unknown config method");
4800 if (use
== WPAS_P2P_PD_AUTO
) {
4801 os_memcpy(wpa_s
->pending_join_dev_addr
, peer_addr
, ETH_ALEN
);
4802 wpa_s
->pending_pd_config_methods
= config_methods
;
4803 wpa_s
->p2p_auto_pd
= 1;
4804 wpa_s
->p2p_auto_join
= 0;
4805 wpa_s
->pending_pd_before_join
= 0;
4806 wpa_s
->auto_pd_scan_retry
= 0;
4807 wpas_p2p_stop_find(wpa_s
);
4808 wpa_s
->p2p_join_scan_count
= 0;
4809 os_get_time(&wpa_s
->p2p_auto_started
);
4810 wpa_printf(MSG_DEBUG
, "P2P: Auto PD started at %ld.%06ld",
4811 wpa_s
->p2p_auto_started
.sec
,
4812 wpa_s
->p2p_auto_started
.usec
);
4813 wpas_p2p_join_scan(wpa_s
, NULL
);
4817 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
) {
4818 return wpa_drv_p2p_prov_disc_req(wpa_s
, peer_addr
,
4820 use
== WPAS_P2P_PD_FOR_JOIN
);
4823 if (wpa_s
->global
->p2p
== NULL
|| wpa_s
->global
->p2p_disabled
)
4826 return p2p_prov_disc_req(wpa_s
->global
->p2p
, peer_addr
,
4827 config_methods
, use
== WPAS_P2P_PD_FOR_JOIN
,
4832 int wpas_p2p_scan_result_text(const u8
*ies
, size_t ies_len
, char *buf
,
4835 return p2p_scan_result_text(ies
, ies_len
, buf
, end
);
4839 static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant
*wpa_s
)
4841 if (!offchannel_pending_action_tx(wpa_s
))
4844 wpa_printf(MSG_DEBUG
, "P2P: Drop pending Action TX due to new "
4845 "operation request");
4846 offchannel_clear_pending_action_tx(wpa_s
);
4850 int wpas_p2p_find(struct wpa_supplicant
*wpa_s
, unsigned int timeout
,
4851 enum p2p_discovery_type type
,
4852 unsigned int num_req_dev_types
, const u8
*req_dev_types
,
4853 const u8
*dev_id
, unsigned int search_delay
)
4855 wpas_p2p_clear_pending_action_tx(wpa_s
);
4856 wpa_s
->p2p_long_listen
= 0;
4858 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
4859 return wpa_drv_p2p_find(wpa_s
, timeout
, type
);
4861 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
||
4862 wpa_s
->p2p_in_provisioning
)
4865 wpa_supplicant_cancel_sched_scan(wpa_s
);
4867 return p2p_find(wpa_s
->global
->p2p
, timeout
, type
,
4868 num_req_dev_types
, req_dev_types
, dev_id
,
4873 static int wpas_p2p_stop_find_oper(struct wpa_supplicant
*wpa_s
)
4875 wpas_p2p_clear_pending_action_tx(wpa_s
);
4876 wpa_s
->p2p_long_listen
= 0;
4877 eloop_cancel_timeout(wpas_p2p_long_listen_timeout
, wpa_s
, NULL
);
4878 eloop_cancel_timeout(wpas_p2p_join_scan
, wpa_s
, NULL
);
4879 wpa_s
->global
->p2p_cb_on_scan_complete
= 0;
4881 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
) {
4882 wpa_drv_p2p_stop_find(wpa_s
);
4886 if (wpa_s
->global
->p2p
)
4887 p2p_stop_find(wpa_s
->global
->p2p
);
4893 void wpas_p2p_stop_find(struct wpa_supplicant
*wpa_s
)
4895 if (wpas_p2p_stop_find_oper(wpa_s
) > 0)
4897 wpas_p2p_remove_pending_group_interface(wpa_s
);
4901 static void wpas_p2p_long_listen_timeout(void *eloop_ctx
, void *timeout_ctx
)
4903 struct wpa_supplicant
*wpa_s
= eloop_ctx
;
4904 wpa_s
->p2p_long_listen
= 0;
4908 int wpas_p2p_listen(struct wpa_supplicant
*wpa_s
, unsigned int timeout
)
4912 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
4915 wpa_supplicant_cancel_sched_scan(wpa_s
);
4916 wpas_p2p_clear_pending_action_tx(wpa_s
);
4920 * This is a request for unlimited Listen state. However, at
4921 * least for now, this is mapped to a Listen state for one
4926 eloop_cancel_timeout(wpas_p2p_long_listen_timeout
, wpa_s
, NULL
);
4927 wpa_s
->p2p_long_listen
= 0;
4930 * Stop previous find/listen operation to avoid trying to request a new
4931 * remain-on-channel operation while the driver is still running the
4934 if (wpa_s
->global
->p2p
)
4935 p2p_stop_find(wpa_s
->global
->p2p
);
4937 res
= wpas_p2p_listen_start(wpa_s
, timeout
* 1000);
4938 if (res
== 0 && timeout
* 1000 > wpa_s
->max_remain_on_chan
) {
4939 wpa_s
->p2p_long_listen
= timeout
* 1000;
4940 eloop_register_timeout(timeout
, 0,
4941 wpas_p2p_long_listen_timeout
,
4949 int wpas_p2p_assoc_req_ie(struct wpa_supplicant
*wpa_s
, struct wpa_bss
*bss
,
4950 u8
*buf
, size_t len
, int p2p_group
)
4952 struct wpabuf
*p2p_ie
;
4955 if (wpa_s
->global
->p2p_disabled
)
4957 if (wpa_s
->global
->p2p
== NULL
)
4962 p2p_ie
= wpa_bss_get_vendor_ie_multi(bss
, P2P_IE_VENDOR_TYPE
);
4963 ret
= p2p_assoc_req_ie(wpa_s
->global
->p2p
, bss
->bssid
, buf
, len
,
4965 wpabuf_free(p2p_ie
);
4971 int wpas_p2p_probe_req_rx(struct wpa_supplicant
*wpa_s
, const u8
*addr
,
4972 const u8
*dst
, const u8
*bssid
,
4973 const u8
*ie
, size_t ie_len
, int ssi_signal
)
4975 if (wpa_s
->global
->p2p_disabled
)
4977 if (wpa_s
->global
->p2p
== NULL
)
4980 switch (p2p_probe_req_rx(wpa_s
->global
->p2p
, addr
, dst
, bssid
,
4982 case P2P_PREQ_NOT_P2P
:
4983 wpas_notify_preq(wpa_s
, addr
, dst
, bssid
, ie
, ie_len
,
4986 case P2P_PREQ_MALFORMED
:
4987 case P2P_PREQ_NOT_LISTEN
:
4988 case P2P_PREQ_NOT_PROCESSED
:
4989 default: /* make gcc happy */
4991 case P2P_PREQ_PROCESSED
:
4997 void wpas_p2p_rx_action(struct wpa_supplicant
*wpa_s
, const u8
*da
,
4998 const u8
*sa
, const u8
*bssid
,
4999 u8 category
, const u8
*data
, size_t len
, int freq
)
5001 if (wpa_s
->global
->p2p_disabled
)
5003 if (wpa_s
->global
->p2p
== NULL
)
5006 p2p_rx_action(wpa_s
->global
->p2p
, da
, sa
, bssid
, category
, data
, len
,
5011 void wpas_p2p_scan_ie(struct wpa_supplicant
*wpa_s
, struct wpabuf
*ies
)
5013 if (wpa_s
->global
->p2p_disabled
)
5015 if (wpa_s
->global
->p2p
== NULL
)
5018 p2p_scan_ie(wpa_s
->global
->p2p
, ies
, NULL
);
5022 void wpas_p2p_group_deinit(struct wpa_supplicant
*wpa_s
)
5024 p2p_group_deinit(wpa_s
->p2p_group
);
5025 wpa_s
->p2p_group
= NULL
;
5027 wpa_s
->ap_configured_cb
= NULL
;
5028 wpa_s
->ap_configured_cb_ctx
= NULL
;
5029 wpa_s
->ap_configured_cb_data
= NULL
;
5030 wpa_s
->connect_without_scan
= NULL
;
5034 int wpas_p2p_reject(struct wpa_supplicant
*wpa_s
, const u8
*addr
)
5036 wpa_s
->p2p_long_listen
= 0;
5038 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
5039 return wpa_drv_p2p_reject(wpa_s
, addr
);
5041 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
5044 return p2p_reject(wpa_s
->global
->p2p
, addr
);
5048 /* Invite to reinvoke a persistent group */
5049 int wpas_p2p_invite(struct wpa_supplicant
*wpa_s
, const u8
*peer_addr
,
5050 struct wpa_ssid
*ssid
, const u8
*go_dev_addr
, int freq
,
5051 int ht40
, int pref_freq
)
5053 enum p2p_invite_role role
;
5058 wpa_s
->global
->p2p_invite_group
= NULL
;
5060 os_memcpy(wpa_s
->p2p_auth_invite
, peer_addr
, ETH_ALEN
);
5062 os_memset(wpa_s
->p2p_auth_invite
, 0, ETH_ALEN
);
5064 wpa_s
->p2p_persistent_go_freq
= freq
;
5065 wpa_s
->p2p_go_ht40
= !!ht40
;
5066 if (ssid
->mode
== WPAS_MODE_P2P_GO
) {
5067 role
= P2P_INVITE_ROLE_GO
;
5068 if (peer_addr
== NULL
) {
5069 wpa_printf(MSG_DEBUG
, "P2P: Missing peer "
5070 "address in invitation command");
5073 if (wpas_p2p_create_iface(wpa_s
)) {
5074 if (wpas_p2p_add_group_interface(wpa_s
,
5075 WPA_IF_P2P_GO
) < 0) {
5076 wpa_printf(MSG_ERROR
, "P2P: Failed to "
5077 "allocate a new interface for the "
5081 bssid
= wpa_s
->pending_interface_addr
;
5083 bssid
= wpa_s
->own_addr
;
5085 role
= P2P_INVITE_ROLE_CLIENT
;
5086 peer_addr
= ssid
->bssid
;
5088 wpa_s
->pending_invite_ssid_id
= ssid
->id
;
5090 res
= wpas_p2p_setup_freqs(wpa_s
, freq
, &force_freq
, &pref_freq
);
5094 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
5095 return wpa_drv_p2p_invite(wpa_s
, peer_addr
, role
, bssid
,
5096 ssid
->ssid
, ssid
->ssid_len
,
5099 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
5102 return p2p_invite(wpa_s
->global
->p2p
, peer_addr
, role
, bssid
,
5103 ssid
->ssid
, ssid
->ssid_len
, force_freq
, go_dev_addr
,
5108 /* Invite to join an active group */
5109 int wpas_p2p_invite_group(struct wpa_supplicant
*wpa_s
, const char *ifname
,
5110 const u8
*peer_addr
, const u8
*go_dev_addr
)
5112 struct wpa_global
*global
= wpa_s
->global
;
5113 enum p2p_invite_role role
;
5115 struct wpa_ssid
*ssid
;
5117 int force_freq
= 0, pref_freq
= 0;
5120 wpa_s
->p2p_persistent_go_freq
= 0;
5121 wpa_s
->p2p_go_ht40
= 0;
5123 for (wpa_s
= global
->ifaces
; wpa_s
; wpa_s
= wpa_s
->next
) {
5124 if (os_strcmp(wpa_s
->ifname
, ifname
) == 0)
5127 if (wpa_s
== NULL
) {
5128 wpa_printf(MSG_DEBUG
, "P2P: Interface '%s' not found", ifname
);
5132 ssid
= wpa_s
->current_ssid
;
5134 wpa_printf(MSG_DEBUG
, "P2P: No current SSID to use for "
5139 wpa_s
->global
->p2p_invite_group
= wpa_s
;
5140 persistent
= ssid
->p2p_persistent_group
&&
5141 wpas_p2p_get_persistent(wpa_s
->parent
, peer_addr
,
5142 ssid
->ssid
, ssid
->ssid_len
);
5144 if (ssid
->mode
== WPAS_MODE_P2P_GO
) {
5145 role
= P2P_INVITE_ROLE_ACTIVE_GO
;
5146 bssid
= wpa_s
->own_addr
;
5147 if (go_dev_addr
== NULL
)
5148 go_dev_addr
= wpa_s
->global
->p2p_dev_addr
;
5150 role
= P2P_INVITE_ROLE_CLIENT
;
5151 if (wpa_s
->wpa_state
< WPA_ASSOCIATED
) {
5152 wpa_printf(MSG_DEBUG
, "P2P: Not associated - cannot "
5153 "invite to current group");
5156 bssid
= wpa_s
->bssid
;
5157 if (go_dev_addr
== NULL
&&
5158 !is_zero_ether_addr(wpa_s
->go_dev_addr
))
5159 go_dev_addr
= wpa_s
->go_dev_addr
;
5161 wpa_s
->parent
->pending_invite_ssid_id
= -1;
5163 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
5164 return wpa_drv_p2p_invite(wpa_s
, peer_addr
, role
, bssid
,
5165 ssid
->ssid
, ssid
->ssid_len
,
5166 go_dev_addr
, persistent
);
5168 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
5171 res
= wpas_p2p_setup_freqs(wpa_s
, 0, &force_freq
, &pref_freq
);
5174 wpas_p2p_set_own_freq_preference(wpa_s
, force_freq
);
5176 return p2p_invite(wpa_s
->global
->p2p
, peer_addr
, role
, bssid
,
5177 ssid
->ssid
, ssid
->ssid_len
, force_freq
,
5178 go_dev_addr
, persistent
, pref_freq
);
5182 void wpas_p2p_completed(struct wpa_supplicant
*wpa_s
)
5184 struct wpa_ssid
*ssid
= wpa_s
->current_ssid
;
5185 const char *ssid_txt
;
5186 u8 go_dev_addr
[ETH_ALEN
];
5187 int network_id
= -1;
5191 if (ssid
== NULL
|| ssid
->mode
!= WPAS_MODE_P2P_GROUP_FORMATION
) {
5192 eloop_cancel_timeout(wpas_p2p_group_formation_timeout
,
5193 wpa_s
->parent
, NULL
);
5196 if (!wpa_s
->show_group_started
|| !ssid
)
5199 wpa_s
->show_group_started
= 0;
5201 ssid_txt
= wpa_ssid_txt(ssid
->ssid
, ssid
->ssid_len
);
5202 os_memset(go_dev_addr
, 0, ETH_ALEN
);
5203 if (ssid
->bssid_set
)
5204 os_memcpy(go_dev_addr
, ssid
->bssid
, ETH_ALEN
);
5205 persistent
= wpas_p2p_persistent_group(wpa_s
, go_dev_addr
, ssid
->ssid
,
5207 os_memcpy(wpa_s
->go_dev_addr
, go_dev_addr
, ETH_ALEN
);
5209 if (wpa_s
->global
->p2p_group_formation
== wpa_s
)
5210 wpa_s
->global
->p2p_group_formation
= NULL
;
5212 freq
= wpa_s
->current_bss
? wpa_s
->current_bss
->freq
:
5213 (int) wpa_s
->assoc_freq
;
5214 if (ssid
->passphrase
== NULL
&& ssid
->psk_set
) {
5216 wpa_snprintf_hex(psk
, sizeof(psk
), ssid
->psk
, 32);
5217 wpa_msg_global(wpa_s
->parent
, MSG_INFO
, P2P_EVENT_GROUP_STARTED
5218 "%s client ssid=\"%s\" freq=%d psk=%s "
5219 "go_dev_addr=" MACSTR
"%s",
5220 wpa_s
->ifname
, ssid_txt
, freq
, psk
,
5221 MAC2STR(go_dev_addr
),
5222 persistent
? " [PERSISTENT]" : "");
5224 wpa_msg_global(wpa_s
->parent
, MSG_INFO
, P2P_EVENT_GROUP_STARTED
5225 "%s client ssid=\"%s\" freq=%d "
5226 "passphrase=\"%s\" go_dev_addr=" MACSTR
"%s",
5227 wpa_s
->ifname
, ssid_txt
, freq
,
5228 ssid
->passphrase
? ssid
->passphrase
: "",
5229 MAC2STR(go_dev_addr
),
5230 persistent
? " [PERSISTENT]" : "");
5234 network_id
= wpas_p2p_store_persistent_group(wpa_s
->parent
,
5237 network_id
= ssid
->id
;
5238 wpas_notify_p2p_group_started(wpa_s
, ssid
, network_id
, 1);
5241 wpas_p2p_continue_after_scan(wpa_s
);
5245 int wpas_p2p_presence_req(struct wpa_supplicant
*wpa_s
, u32 duration1
,
5246 u32 interval1
, u32 duration2
, u32 interval2
)
5248 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
5250 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
5253 if (wpa_s
->wpa_state
< WPA_ASSOCIATED
||
5254 wpa_s
->current_ssid
== NULL
||
5255 wpa_s
->current_ssid
->mode
!= WPAS_MODE_INFRA
)
5258 return p2p_presence_req(wpa_s
->global
->p2p
, wpa_s
->bssid
,
5259 wpa_s
->own_addr
, wpa_s
->assoc_freq
,
5260 duration1
, interval1
, duration2
, interval2
);
5264 int wpas_p2p_ext_listen(struct wpa_supplicant
*wpa_s
, unsigned int period
,
5265 unsigned int interval
)
5267 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
5270 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
5273 return p2p_ext_listen(wpa_s
->global
->p2p
, period
, interval
);
5277 static int wpas_p2p_is_client(struct wpa_supplicant
*wpa_s
)
5279 if (wpa_s
->current_ssid
== NULL
) {
5281 * current_ssid can be cleared when P2P client interface gets
5282 * disconnected, so assume this interface was used as P2P
5287 return wpa_s
->current_ssid
->p2p_group
&&
5288 wpa_s
->current_ssid
->mode
== WPAS_MODE_INFRA
;
5292 static void wpas_p2p_group_idle_timeout(void *eloop_ctx
, void *timeout_ctx
)
5294 struct wpa_supplicant
*wpa_s
= eloop_ctx
;
5296 if (wpa_s
->conf
->p2p_group_idle
== 0 && !wpas_p2p_is_client(wpa_s
)) {
5297 wpa_printf(MSG_DEBUG
, "P2P: Ignore group idle timeout - "
5302 wpa_printf(MSG_DEBUG
, "P2P: Group idle timeout reached - terminate "
5304 wpas_p2p_group_delete(wpa_s
, P2P_GROUP_REMOVAL_IDLE_TIMEOUT
);
5308 static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant
*wpa_s
)
5312 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout
, wpa_s
, NULL
) > 0)
5313 wpa_printf(MSG_DEBUG
, "P2P: Cancelled P2P group idle timeout");
5315 if (wpa_s
->current_ssid
== NULL
|| !wpa_s
->current_ssid
->p2p_group
)
5318 timeout
= wpa_s
->conf
->p2p_group_idle
;
5319 if (wpa_s
->current_ssid
->mode
== WPAS_MODE_INFRA
&&
5320 (timeout
== 0 || timeout
> P2P_MAX_CLIENT_IDLE
))
5321 timeout
= P2P_MAX_CLIENT_IDLE
;
5327 if (wpa_s
->current_ssid
->mode
== WPAS_MODE_INFRA
)
5328 timeout
= 0; /* special client mode no-timeout */
5333 if (wpa_s
->p2p_in_provisioning
) {
5335 * Use the normal group formation timeout during the
5336 * provisioning phase to avoid terminating this process too
5337 * early due to group idle timeout.
5339 wpa_printf(MSG_DEBUG
, "P2P: Do not use P2P group idle timeout "
5340 "during provisioning");
5344 if (wpa_s
->show_group_started
) {
5346 * Use the normal group formation timeout between the end of
5347 * the provisioning phase and completion of 4-way handshake to
5348 * avoid terminating this process too early due to group idle
5351 wpa_printf(MSG_DEBUG
, "P2P: Do not use P2P group idle timeout "
5352 "while waiting for initial 4-way handshake to "
5357 wpa_printf(MSG_DEBUG
, "P2P: Set P2P group idle timeout to %u seconds",
5359 eloop_register_timeout(timeout
, 0, wpas_p2p_group_idle_timeout
,
5364 /* Returns 1 if the interface was removed */
5365 int wpas_p2p_deauth_notif(struct wpa_supplicant
*wpa_s
, const u8
*bssid
,
5366 u16 reason_code
, const u8
*ie
, size_t ie_len
,
5367 int locally_generated
)
5369 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
5371 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
5374 if (!locally_generated
)
5375 p2p_deauth_notif(wpa_s
->global
->p2p
, bssid
, reason_code
, ie
,
5378 if (reason_code
== WLAN_REASON_DEAUTH_LEAVING
&& !locally_generated
&&
5379 wpa_s
->current_ssid
&&
5380 wpa_s
->current_ssid
->p2p_group
&&
5381 wpa_s
->current_ssid
->mode
== WPAS_MODE_INFRA
) {
5382 wpa_printf(MSG_DEBUG
, "P2P: GO indicated that the P2P Group "
5383 "session is ending");
5384 if (wpas_p2p_group_delete(wpa_s
,
5385 P2P_GROUP_REMOVAL_GO_ENDING_SESSION
)
5394 void wpas_p2p_disassoc_notif(struct wpa_supplicant
*wpa_s
, const u8
*bssid
,
5395 u16 reason_code
, const u8
*ie
, size_t ie_len
,
5396 int locally_generated
)
5398 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
5400 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
5403 if (!locally_generated
)
5404 p2p_disassoc_notif(wpa_s
->global
->p2p
, bssid
, reason_code
, ie
,
5409 void wpas_p2p_update_config(struct wpa_supplicant
*wpa_s
)
5411 struct p2p_data
*p2p
= wpa_s
->global
->p2p
;
5416 if (!(wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_CAPABLE
))
5419 if (wpa_s
->conf
->changed_parameters
& CFG_CHANGED_DEVICE_NAME
)
5420 p2p_set_dev_name(p2p
, wpa_s
->conf
->device_name
);
5422 if (wpa_s
->conf
->changed_parameters
& CFG_CHANGED_DEVICE_TYPE
)
5423 p2p_set_pri_dev_type(p2p
, wpa_s
->conf
->device_type
);
5426 (wpa_s
->conf
->changed_parameters
& CFG_CHANGED_CONFIG_METHODS
))
5427 p2p_set_config_methods(p2p
, wpa_s
->wps
->config_methods
);
5429 if (wpa_s
->wps
&& (wpa_s
->conf
->changed_parameters
& CFG_CHANGED_UUID
))
5430 p2p_set_uuid(p2p
, wpa_s
->wps
->uuid
);
5432 if (wpa_s
->conf
->changed_parameters
& CFG_CHANGED_WPS_STRING
) {
5433 p2p_set_manufacturer(p2p
, wpa_s
->conf
->manufacturer
);
5434 p2p_set_model_name(p2p
, wpa_s
->conf
->model_name
);
5435 p2p_set_model_number(p2p
, wpa_s
->conf
->model_number
);
5436 p2p_set_serial_number(p2p
, wpa_s
->conf
->serial_number
);
5439 if (wpa_s
->conf
->changed_parameters
& CFG_CHANGED_SEC_DEVICE_TYPE
)
5440 p2p_set_sec_dev_types(p2p
,
5441 (void *) wpa_s
->conf
->sec_device_type
,
5442 wpa_s
->conf
->num_sec_device_types
);
5444 if (wpa_s
->conf
->changed_parameters
& CFG_CHANGED_VENDOR_EXTENSION
) {
5446 p2p_remove_wps_vendor_extensions(p2p
);
5447 for (i
= 0; i
< MAX_WPS_VENDOR_EXT
; i
++) {
5448 if (wpa_s
->conf
->wps_vendor_ext
[i
] == NULL
)
5450 p2p_add_wps_vendor_extension(
5451 p2p
, wpa_s
->conf
->wps_vendor_ext
[i
]);
5455 if ((wpa_s
->conf
->changed_parameters
& CFG_CHANGED_COUNTRY
) &&
5456 wpa_s
->conf
->country
[0] && wpa_s
->conf
->country
[1]) {
5458 country
[0] = wpa_s
->conf
->country
[0];
5459 country
[1] = wpa_s
->conf
->country
[1];
5461 p2p_set_country(p2p
, country
);
5464 if (wpa_s
->conf
->changed_parameters
& CFG_CHANGED_P2P_SSID_POSTFIX
) {
5465 p2p_set_ssid_postfix(p2p
, (u8
*) wpa_s
->conf
->p2p_ssid_postfix
,
5466 wpa_s
->conf
->p2p_ssid_postfix
?
5467 os_strlen(wpa_s
->conf
->p2p_ssid_postfix
) :
5471 if (wpa_s
->conf
->changed_parameters
& CFG_CHANGED_P2P_INTRA_BSS
)
5472 p2p_set_intra_bss_dist(p2p
, wpa_s
->conf
->p2p_intra_bss
);
5474 if (wpa_s
->conf
->changed_parameters
& CFG_CHANGED_P2P_LISTEN_CHANNEL
) {
5475 u8 reg_class
, channel
;
5478 if (wpa_s
->conf
->p2p_listen_reg_class
&&
5479 wpa_s
->conf
->p2p_listen_channel
) {
5480 reg_class
= wpa_s
->conf
->p2p_listen_reg_class
;
5481 channel
= wpa_s
->conf
->p2p_listen_channel
;
5485 * Pick one of the social channels randomly as the
5488 os_get_random((u8
*) &r
, sizeof(r
));
5489 channel
= 1 + (r
% 3) * 5;
5491 ret
= p2p_set_listen_channel(p2p
, reg_class
, channel
);
5493 wpa_printf(MSG_ERROR
, "P2P: Own listen channel update "
5496 if (wpa_s
->conf
->changed_parameters
& CFG_CHANGED_P2P_OPER_CHANNEL
) {
5497 u8 op_reg_class
, op_channel
, cfg_op_channel
;
5500 if (wpa_s
->conf
->p2p_oper_reg_class
&&
5501 wpa_s
->conf
->p2p_oper_channel
) {
5502 op_reg_class
= wpa_s
->conf
->p2p_oper_reg_class
;
5503 op_channel
= wpa_s
->conf
->p2p_oper_channel
;
5508 * Use random operation channel from (1, 6, 11)
5509 *if no other preference is indicated.
5511 os_get_random((u8
*) &r
, sizeof(r
));
5512 op_channel
= 1 + (r
% 3) * 5;
5515 ret
= p2p_set_oper_channel(p2p
, op_reg_class
, op_channel
,
5518 wpa_printf(MSG_ERROR
, "P2P: Own oper channel update "
5522 if (wpa_s
->conf
->changed_parameters
& CFG_CHANGED_P2P_PREF_CHAN
) {
5523 if (p2p_set_pref_chan(p2p
, wpa_s
->conf
->num_p2p_pref_chan
,
5524 wpa_s
->conf
->p2p_pref_chan
) < 0) {
5525 wpa_printf(MSG_ERROR
, "P2P: Preferred channel list "
5532 int wpas_p2p_set_noa(struct wpa_supplicant
*wpa_s
, u8 count
, int start
,
5535 if (!wpa_s
->ap_iface
)
5537 return hostapd_p2p_set_noa(wpa_s
->ap_iface
->bss
[0], count
, start
,
5542 int wpas_p2p_set_cross_connect(struct wpa_supplicant
*wpa_s
, int enabled
)
5544 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
5546 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
5549 wpa_s
->global
->cross_connection
= enabled
;
5550 p2p_set_cross_connect(wpa_s
->global
->p2p
, enabled
);
5553 struct wpa_supplicant
*iface
;
5555 for (iface
= wpa_s
->global
->ifaces
; iface
; iface
= iface
->next
)
5557 if (iface
->cross_connect_enabled
== 0)
5560 iface
->cross_connect_enabled
= 0;
5561 iface
->cross_connect_in_use
= 0;
5562 wpa_msg_global(iface
->parent
, MSG_INFO
,
5563 P2P_EVENT_CROSS_CONNECT_DISABLE
"%s %s",
5565 iface
->cross_connect_uplink
);
5573 static void wpas_p2p_enable_cross_connect(struct wpa_supplicant
*uplink
)
5575 struct wpa_supplicant
*iface
;
5577 if (!uplink
->global
->cross_connection
)
5580 for (iface
= uplink
->global
->ifaces
; iface
; iface
= iface
->next
) {
5581 if (!iface
->cross_connect_enabled
)
5583 if (os_strcmp(uplink
->ifname
, iface
->cross_connect_uplink
) !=
5586 if (iface
->ap_iface
== NULL
)
5588 if (iface
->cross_connect_in_use
)
5591 iface
->cross_connect_in_use
= 1;
5592 wpa_msg_global(iface
->parent
, MSG_INFO
,
5593 P2P_EVENT_CROSS_CONNECT_ENABLE
"%s %s",
5594 iface
->ifname
, iface
->cross_connect_uplink
);
5599 static void wpas_p2p_disable_cross_connect(struct wpa_supplicant
*uplink
)
5601 struct wpa_supplicant
*iface
;
5603 for (iface
= uplink
->global
->ifaces
; iface
; iface
= iface
->next
) {
5604 if (!iface
->cross_connect_enabled
)
5606 if (os_strcmp(uplink
->ifname
, iface
->cross_connect_uplink
) !=
5609 if (!iface
->cross_connect_in_use
)
5612 wpa_msg_global(iface
->parent
, MSG_INFO
,
5613 P2P_EVENT_CROSS_CONNECT_DISABLE
"%s %s",
5614 iface
->ifname
, iface
->cross_connect_uplink
);
5615 iface
->cross_connect_in_use
= 0;
5620 void wpas_p2p_notif_connected(struct wpa_supplicant
*wpa_s
)
5622 if (wpa_s
->ap_iface
|| wpa_s
->current_ssid
== NULL
||
5623 wpa_s
->current_ssid
->mode
!= WPAS_MODE_INFRA
||
5624 wpa_s
->cross_connect_disallowed
)
5625 wpas_p2p_disable_cross_connect(wpa_s
);
5627 wpas_p2p_enable_cross_connect(wpa_s
);
5628 if (!wpa_s
->ap_iface
&&
5629 eloop_cancel_timeout(wpas_p2p_group_idle_timeout
, wpa_s
, NULL
) > 0)
5630 wpa_printf(MSG_DEBUG
, "P2P: Cancelled P2P group idle timeout");
5634 void wpas_p2p_notif_disconnected(struct wpa_supplicant
*wpa_s
)
5636 wpas_p2p_disable_cross_connect(wpa_s
);
5637 if (!wpa_s
->ap_iface
&&
5638 !eloop_is_timeout_registered(wpas_p2p_group_idle_timeout
,
5640 wpas_p2p_set_group_idle_timeout(wpa_s
);
5644 static void wpas_p2p_cross_connect_setup(struct wpa_supplicant
*wpa_s
)
5646 struct wpa_supplicant
*iface
;
5648 if (!wpa_s
->global
->cross_connection
)
5651 for (iface
= wpa_s
->global
->ifaces
; iface
; iface
= iface
->next
) {
5654 if (iface
->drv_flags
&
5655 WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE
)
5657 if (iface
->drv_flags
& WPA_DRIVER_FLAGS_P2P_CAPABLE
)
5660 wpa_s
->cross_connect_enabled
= 1;
5661 os_strlcpy(wpa_s
->cross_connect_uplink
, iface
->ifname
,
5662 sizeof(wpa_s
->cross_connect_uplink
));
5663 wpa_printf(MSG_DEBUG
, "P2P: Enable cross connection from "
5664 "%s to %s whenever uplink is available",
5665 wpa_s
->ifname
, wpa_s
->cross_connect_uplink
);
5667 if (iface
->ap_iface
|| iface
->current_ssid
== NULL
||
5668 iface
->current_ssid
->mode
!= WPAS_MODE_INFRA
||
5669 iface
->cross_connect_disallowed
||
5670 iface
->wpa_state
!= WPA_COMPLETED
)
5673 wpa_s
->cross_connect_in_use
= 1;
5674 wpa_msg_global(wpa_s
->parent
, MSG_INFO
,
5675 P2P_EVENT_CROSS_CONNECT_ENABLE
"%s %s",
5676 wpa_s
->ifname
, wpa_s
->cross_connect_uplink
);
5682 int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant
*wpa_s
)
5684 if (wpa_s
->p2p_group_interface
!= P2P_GROUP_INTERFACE_CLIENT
&&
5685 !wpa_s
->p2p_in_provisioning
)
5686 return 0; /* not P2P client operation */
5688 wpa_printf(MSG_DEBUG
, "P2P: Terminate connection due to WPS PBC "
5690 if (wpa_s
!= wpa_s
->parent
)
5691 wpa_msg_ctrl(wpa_s
->parent
, MSG_INFO
, WPS_EVENT_OVERLAP
);
5692 wpas_p2p_group_formation_failed(wpa_s
);
5697 void wpas_p2p_update_channel_list(struct wpa_supplicant
*wpa_s
)
5699 struct p2p_channels chan
;
5701 if (wpa_s
->global
== NULL
|| wpa_s
->global
->p2p
== NULL
)
5704 os_memset(&chan
, 0, sizeof(chan
));
5705 if (wpas_p2p_setup_channels(wpa_s
, &chan
)) {
5706 wpa_printf(MSG_ERROR
, "P2P: Failed to update supported "
5711 p2p_update_channel_list(wpa_s
->global
->p2p
, &chan
);
5715 static void wpas_p2p_scan_res_ignore(struct wpa_supplicant
*wpa_s
,
5716 struct wpa_scan_results
*scan_res
)
5718 wpa_printf(MSG_DEBUG
, "P2P: Ignore scan results");
5722 int wpas_p2p_cancel(struct wpa_supplicant
*wpa_s
)
5724 struct wpa_global
*global
= wpa_s
->global
;
5728 if (global
->p2p
== NULL
)
5731 wpa_printf(MSG_DEBUG
, "P2P: Request to cancel group formation");
5733 if (wpa_s
->pending_interface_name
[0] &&
5734 !is_zero_ether_addr(wpa_s
->pending_interface_addr
))
5737 peer
= p2p_get_go_neg_peer(global
->p2p
);
5739 wpa_printf(MSG_DEBUG
, "P2P: Unauthorize pending GO Neg peer "
5740 MACSTR
, MAC2STR(peer
));
5741 p2p_unauthorize(global
->p2p
, peer
);
5745 if (wpa_s
->scan_res_handler
== wpas_p2p_scan_res_join
) {
5746 wpa_printf(MSG_DEBUG
, "P2P: Stop pending scan for join");
5747 wpa_s
->scan_res_handler
= wpas_p2p_scan_res_ignore
;
5751 if (wpa_s
->pending_pd_before_join
) {
5752 wpa_printf(MSG_DEBUG
, "P2P: Stop pending PD before join");
5753 wpa_s
->pending_pd_before_join
= 0;
5757 wpas_p2p_stop_find(wpa_s
);
5759 for (wpa_s
= global
->ifaces
; wpa_s
; wpa_s
= wpa_s
->next
) {
5760 if (wpa_s
== global
->p2p_group_formation
&&
5761 (wpa_s
->p2p_in_provisioning
||
5762 wpa_s
->parent
->pending_interface_type
==
5763 WPA_IF_P2P_CLIENT
)) {
5764 wpa_printf(MSG_DEBUG
, "P2P: Interface %s in group "
5765 "formation found - cancelling",
5768 eloop_cancel_timeout(wpas_p2p_group_formation_timeout
,
5769 wpa_s
->parent
, NULL
);
5770 if (wpa_s
->p2p_in_provisioning
) {
5771 wpas_group_formation_completed(wpa_s
, 0);
5774 wpas_p2p_group_delete(wpa_s
,
5775 P2P_GROUP_REMOVAL_REQUESTED
);
5781 wpa_printf(MSG_DEBUG
, "P2P: No ongoing group formation found");
5789 void wpas_p2p_interface_unavailable(struct wpa_supplicant
*wpa_s
)
5791 if (wpa_s
->current_ssid
== NULL
|| !wpa_s
->current_ssid
->p2p_group
)
5794 wpa_printf(MSG_DEBUG
, "P2P: Remove group due to driver resource not "
5795 "being available anymore");
5796 wpas_p2p_group_delete(wpa_s
, P2P_GROUP_REMOVAL_UNAVAILABLE
);
5800 void wpas_p2p_update_best_channels(struct wpa_supplicant
*wpa_s
,
5801 int freq_24
, int freq_5
, int freq_overall
)
5803 struct p2p_data
*p2p
= wpa_s
->global
->p2p
;
5804 if (p2p
== NULL
|| (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
))
5806 p2p_set_best_channels(p2p
, freq_24
, freq_5
, freq_overall
);
5810 int wpas_p2p_unauthorize(struct wpa_supplicant
*wpa_s
, const char *addr
)
5813 struct p2p_data
*p2p
= wpa_s
->global
->p2p
;
5815 if (p2p
== NULL
|| (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
))
5818 if (hwaddr_aton(addr
, peer
))
5821 return p2p_unauthorize(p2p
, peer
);
5826 * wpas_p2p_disconnect - Disconnect from a P2P Group
5827 * @wpa_s: Pointer to wpa_supplicant data
5828 * Returns: 0 on success, -1 on failure
5830 * This can be used to disconnect from a group in which the local end is a P2P
5831 * Client or to end a P2P Group in case the local end is the Group Owner. If a
5832 * virtual network interface was created for this group, that interface will be
5833 * removed. Otherwise, only the configured P2P group network will be removed
5834 * from the interface.
5836 int wpas_p2p_disconnect(struct wpa_supplicant
*wpa_s
)
5842 return wpas_p2p_group_delete(wpa_s
, P2P_GROUP_REMOVAL_REQUESTED
) < 0 ?
5847 int wpas_p2p_in_progress(struct wpa_supplicant
*wpa_s
)
5851 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
5854 ret
= p2p_in_progress(wpa_s
->global
->p2p
);
5857 * Check whether there is an ongoing WPS provisioning step (or
5858 * other parts of group formation) on another interface since
5859 * p2p_in_progress() does not report this to avoid issues for
5860 * scans during such provisioning step.
5862 if (wpa_s
->global
->p2p_group_formation
&&
5863 wpa_s
->global
->p2p_group_formation
!= wpa_s
) {
5864 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: Another interface (%s) "
5865 "in group formation",
5866 wpa_s
->global
->p2p_group_formation
->ifname
);
5871 if (!ret
&& wpa_s
->global
->p2p_go_wait_client
.sec
) {
5874 if (now
.sec
> wpa_s
->global
->p2p_go_wait_client
.sec
+
5875 P2P_MAX_INITIAL_CONN_WAIT_GO
) {
5876 /* Wait for the first client has expired */
5877 wpa_s
->global
->p2p_go_wait_client
.sec
= 0;
5879 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: Waiting for initial client connection during group formation");
5888 void wpas_p2p_network_removed(struct wpa_supplicant
*wpa_s
,
5889 struct wpa_ssid
*ssid
)
5891 if (wpa_s
->p2p_in_provisioning
&& ssid
->p2p_group
&&
5892 eloop_cancel_timeout(wpas_p2p_group_formation_timeout
,
5893 wpa_s
->parent
, NULL
) > 0) {
5895 * Remove the network by scheduling the group formation
5896 * timeout to happen immediately. The teardown code
5897 * needs to be scheduled to run asynch later so that we
5898 * don't delete data from under ourselves unexpectedly.
5899 * Calling wpas_p2p_group_formation_timeout directly
5900 * causes a series of crashes in WPS failure scenarios.
5902 wpa_printf(MSG_DEBUG
, "P2P: Canceled group formation due to "
5903 "P2P group network getting removed");
5904 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout
,
5905 wpa_s
->parent
, NULL
);
5910 struct wpa_ssid
* wpas_p2p_get_persistent(struct wpa_supplicant
*wpa_s
,
5911 const u8
*addr
, const u8
*ssid
,
5917 for (s
= wpa_s
->conf
->ssid
; s
; s
= s
->next
) {
5918 if (s
->disabled
!= 2)
5921 (ssid_len
!= s
->ssid_len
||
5922 os_memcmp(ssid
, s
->ssid
, ssid_len
) != 0))
5925 if (s
->mode
== WPAS_MODE_P2P_GO
)
5929 if (os_memcmp(s
->bssid
, addr
, ETH_ALEN
) == 0)
5930 return s
; /* peer is GO in the persistent group */
5931 if (s
->mode
!= WPAS_MODE_P2P_GO
|| s
->p2p_client_list
== NULL
)
5933 for (i
= 0; i
< s
->num_p2p_clients
; i
++) {
5934 if (os_memcmp(s
->p2p_client_list
+ i
* ETH_ALEN
,
5935 addr
, ETH_ALEN
) == 0)
5936 return s
; /* peer is P2P client in persistent
5945 void wpas_p2p_notify_ap_sta_authorized(struct wpa_supplicant
*wpa_s
,
5948 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout
,
5949 wpa_s
->parent
, NULL
) > 0) {
5951 * This can happen if WPS provisioning step is not terminated
5952 * cleanly (e.g., P2P Client does not send WSC_Done). Since the
5953 * peer was able to connect, there is no need to time out group
5954 * formation after this, though.
5956 wpa_printf(MSG_DEBUG
, "P2P: Workaround - cancelled P2P group formation timeout on data connection");
5958 wpa_s
->global
->p2p_go_wait_client
.sec
= 0;
5961 wpas_p2p_add_persistent_group_client(wpa_s
, addr
);
5965 static void wpas_p2p_fallback_to_go_neg(struct wpa_supplicant
*wpa_s
,
5968 struct wpa_supplicant
*group
= wpa_s
;
5969 if (wpa_s
->global
->p2p_group_formation
)
5970 group
= wpa_s
->global
->p2p_group_formation
;
5971 wpa_s
= wpa_s
->parent
;
5972 offchannel_send_action_done(wpa_s
);
5974 wpas_p2p_group_delete(group
, P2P_GROUP_REMOVAL_SILENT
);
5975 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: Fall back to GO Negotiation");
5976 wpas_p2p_connect(wpa_s
, wpa_s
->pending_join_dev_addr
, wpa_s
->p2p_pin
,
5977 wpa_s
->p2p_wps_method
, wpa_s
->p2p_persistent_group
, 0,
5978 0, 0, wpa_s
->p2p_go_intent
, wpa_s
->p2p_connect_freq
,
5979 wpa_s
->p2p_persistent_id
,
5980 wpa_s
->p2p_pd_before_go_neg
,
5981 wpa_s
->p2p_go_ht40
);
5985 int wpas_p2p_scan_no_go_seen(struct wpa_supplicant
*wpa_s
)
5987 if (!wpa_s
->p2p_fallback_to_go_neg
||
5988 wpa_s
->p2p_in_provisioning
<= 5)
5991 if (wpas_p2p_peer_go(wpa_s
, wpa_s
->pending_join_dev_addr
) > 0)
5992 return 0; /* peer operating as a GO */
5994 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: GO not found for p2p_connect-auto - "
5995 "fallback to GO Negotiation");
5996 wpas_p2p_fallback_to_go_neg(wpa_s
, 1);
6002 unsigned int wpas_p2p_search_delay(struct wpa_supplicant
*wpa_s
)
6004 const char *rn
, *rn2
;
6005 struct wpa_supplicant
*ifs
;
6007 if (wpa_s
->wpa_state
> WPA_SCANNING
) {
6008 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: Use %u ms search delay due to "
6009 "concurrent operation",
6010 P2P_CONCURRENT_SEARCH_DELAY
);
6011 return P2P_CONCURRENT_SEARCH_DELAY
;
6014 if (!wpa_s
->driver
->get_radio_name
)
6016 rn
= wpa_s
->driver
->get_radio_name(wpa_s
->drv_priv
);
6017 if (rn
== NULL
|| rn
[0] == '\0')
6020 for (ifs
= wpa_s
->global
->ifaces
; ifs
; ifs
= ifs
->next
) {
6021 if (ifs
== wpa_s
|| !ifs
->driver
->get_radio_name
)
6024 rn2
= ifs
->driver
->get_radio_name(ifs
->drv_priv
);
6025 if (!rn2
|| os_strcmp(rn
, rn2
) != 0)
6027 if (ifs
->wpa_state
> WPA_SCANNING
) {
6028 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: Use %u ms search "
6029 "delay due to concurrent operation on "
6031 P2P_CONCURRENT_SEARCH_DELAY
, ifs
->ifname
);
6032 return P2P_CONCURRENT_SEARCH_DELAY
;
6040 void wpas_p2p_continue_after_scan(struct wpa_supplicant
*wpa_s
)
6042 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: Station mode scan operation not "
6043 "pending anymore (sta_scan_pending=%d "
6044 "p2p_cb_on_scan_complete=%d)", wpa_s
->sta_scan_pending
,
6045 wpa_s
->global
->p2p_cb_on_scan_complete
);
6046 wpa_s
->sta_scan_pending
= 0;
6048 if (!wpa_s
->global
->p2p_cb_on_scan_complete
)
6050 wpa_s
->global
->p2p_cb_on_scan_complete
= 0;
6052 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
6055 if (p2p_other_scan_completed(wpa_s
->global
->p2p
) == 1) {
6056 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: Pending P2P operation "
6057 "continued after successful connection");
6058 p2p_increase_search_delay(wpa_s
->global
->p2p
,
6059 wpas_p2p_search_delay(wpa_s
));
6064 static int wpas_p2p_remove_psk_entry(struct wpa_supplicant
*wpa_s
,
6065 struct wpa_ssid
*s
, const u8
*addr
,
6068 struct psk_list_entry
*psk
, *tmp
;
6071 dl_list_for_each_safe(psk
, tmp
, &s
->psk_list
, struct psk_list_entry
,
6073 if ((iface_addr
&& !psk
->p2p
&&
6074 os_memcmp(addr
, psk
->addr
, ETH_ALEN
) == 0) ||
6075 (!iface_addr
&& psk
->p2p
&&
6076 os_memcmp(addr
, psk
->addr
, ETH_ALEN
) == 0)) {
6077 wpa_dbg(wpa_s
, MSG_DEBUG
,
6078 "P2P: Remove persistent group PSK list entry for "
6080 MAC2STR(psk
->addr
), psk
->p2p
);
6081 dl_list_del(&psk
->list
);
6091 void wpas_p2p_new_psk_cb(struct wpa_supplicant
*wpa_s
, const u8
*mac_addr
,
6092 const u8
*p2p_dev_addr
,
6093 const u8
*psk
, size_t psk_len
)
6095 struct wpa_ssid
*ssid
= wpa_s
->current_ssid
;
6096 struct wpa_ssid
*persistent
;
6097 struct psk_list_entry
*p
;
6099 if (psk_len
!= sizeof(p
->psk
))
6103 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: New PSK for addr=" MACSTR
6104 " p2p_dev_addr=" MACSTR
,
6105 MAC2STR(mac_addr
), MAC2STR(p2p_dev_addr
));
6106 if (is_zero_ether_addr(p2p_dev_addr
))
6107 p2p_dev_addr
= NULL
;
6109 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: New PSK for addr=" MACSTR
,
6113 if (ssid
->mode
== WPAS_MODE_P2P_GROUP_FORMATION
) {
6114 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: new_psk_cb during group formation");
6115 /* To be added to persistent group once created */
6116 if (wpa_s
->global
->add_psk
== NULL
) {
6117 wpa_s
->global
->add_psk
= os_zalloc(sizeof(*p
));
6118 if (wpa_s
->global
->add_psk
== NULL
)
6121 p
= wpa_s
->global
->add_psk
;
6124 os_memcpy(p
->addr
, p2p_dev_addr
, ETH_ALEN
);
6127 os_memcpy(p
->addr
, mac_addr
, ETH_ALEN
);
6129 os_memcpy(p
->psk
, psk
, psk_len
);
6133 if (ssid
->mode
!= WPAS_MODE_P2P_GO
|| !ssid
->p2p_persistent_group
) {
6134 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: Ignore new_psk_cb on not-persistent GO");
6138 persistent
= wpas_p2p_get_persistent(wpa_s
->parent
, NULL
, ssid
->ssid
,
6141 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: Could not find persistent group information to store the new PSK");
6145 p
= os_zalloc(sizeof(*p
));
6150 os_memcpy(p
->addr
, p2p_dev_addr
, ETH_ALEN
);
6153 os_memcpy(p
->addr
, mac_addr
, ETH_ALEN
);
6155 os_memcpy(p
->psk
, psk
, psk_len
);
6157 if (dl_list_len(&persistent
->psk_list
) > P2P_MAX_STORED_CLIENTS
) {
6158 struct psk_list_entry
*last
;
6159 last
= dl_list_last(&persistent
->psk_list
,
6160 struct psk_list_entry
, list
);
6161 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: Remove oldest PSK entry for "
6162 MACSTR
" (p2p=%u) to make room for a new one",
6163 MAC2STR(last
->addr
), last
->p2p
);
6164 dl_list_del(&last
->list
);
6168 wpas_p2p_remove_psk_entry(wpa_s
->parent
, persistent
,
6169 p2p_dev_addr
? p2p_dev_addr
: mac_addr
,
6170 p2p_dev_addr
== NULL
);
6172 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: Add new PSK for p2p_dev_addr="
6173 MACSTR
, MAC2STR(p2p_dev_addr
));
6175 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: Add new PSK for addr=" MACSTR
,
6178 dl_list_add(&persistent
->psk_list
, &p
->list
);
6180 #ifndef CONFIG_NO_CONFIG_WRITE
6181 if (wpa_s
->parent
->conf
->update_config
&&
6182 wpa_config_write(wpa_s
->parent
->confname
, wpa_s
->parent
->conf
))
6183 wpa_printf(MSG_DEBUG
, "P2P: Failed to update configuration");
6184 #endif /* CONFIG_NO_CONFIG_WRITE */
6188 static void wpas_p2p_remove_psk(struct wpa_supplicant
*wpa_s
,
6189 struct wpa_ssid
*s
, const u8
*addr
,
6194 res
= wpas_p2p_remove_psk_entry(wpa_s
, s
, addr
, iface_addr
);
6196 #ifndef CONFIG_NO_CONFIG_WRITE
6197 if (wpa_s
->conf
->update_config
&&
6198 wpa_config_write(wpa_s
->confname
, wpa_s
->conf
))
6199 wpa_dbg(wpa_s
, MSG_DEBUG
,
6200 "P2P: Failed to update configuration");
6201 #endif /* CONFIG_NO_CONFIG_WRITE */
6206 static void wpas_p2p_remove_client_go(struct wpa_supplicant
*wpa_s
,
6207 const u8
*peer
, int iface_addr
)
6209 struct hostapd_data
*hapd
;
6210 struct hostapd_wpa_psk
*psk
, *prev
, *rem
;
6211 struct sta_info
*sta
;
6213 if (wpa_s
->ap_iface
== NULL
|| wpa_s
->current_ssid
== NULL
||
6214 wpa_s
->current_ssid
->mode
!= WPAS_MODE_P2P_GO
)
6217 /* Remove per-station PSK entry */
6218 hapd
= wpa_s
->ap_iface
->bss
[0];
6220 psk
= hapd
->conf
->ssid
.wpa_psk
;
6222 if ((iface_addr
&& os_memcmp(peer
, psk
->addr
, ETH_ALEN
) == 0) ||
6224 os_memcmp(peer
, psk
->p2p_dev_addr
, ETH_ALEN
) == 0)) {
6225 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: Remove operating group PSK entry for "
6226 MACSTR
" iface_addr=%d",
6227 MAC2STR(peer
), iface_addr
);
6229 prev
->next
= psk
->next
;
6231 hapd
->conf
->ssid
.wpa_psk
= psk
->next
;
6241 /* Disconnect from group */
6243 sta
= ap_get_sta(hapd
, peer
);
6245 sta
= ap_get_sta_p2p(hapd
, peer
);
6247 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: Disconnect peer " MACSTR
6248 " (iface_addr=%d) from group",
6249 MAC2STR(peer
), iface_addr
);
6250 hostapd_drv_sta_deauth(hapd
, sta
->addr
,
6251 WLAN_REASON_DEAUTH_LEAVING
);
6252 ap_sta_deauthenticate(hapd
, sta
, WLAN_REASON_DEAUTH_LEAVING
);
6257 void wpas_p2p_remove_client(struct wpa_supplicant
*wpa_s
, const u8
*peer
,
6261 struct wpa_supplicant
*w
;
6263 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: Remove client " MACSTR
, MAC2STR(peer
));
6265 /* Remove from any persistent group */
6266 for (s
= wpa_s
->parent
->conf
->ssid
; s
; s
= s
->next
) {
6267 if (s
->disabled
!= 2 || s
->mode
!= WPAS_MODE_P2P_GO
)
6270 wpas_remove_persistent_peer(wpa_s
, s
, peer
, 0);
6271 wpas_p2p_remove_psk(wpa_s
->parent
, s
, peer
, iface_addr
);
6274 /* Remove from any operating group */
6275 for (w
= wpa_s
->global
->ifaces
; w
; w
= w
->next
)
6276 wpas_p2p_remove_client_go(w
, peer
, iface_addr
);
6280 static void wpas_p2p_psk_failure_removal(void *eloop_ctx
, void *timeout_ctx
)
6282 struct wpa_supplicant
*wpa_s
= eloop_ctx
;
6283 wpas_p2p_group_delete(wpa_s
, P2P_GROUP_REMOVAL_PSK_FAILURE
);
6287 int wpas_p2p_4way_hs_failed(struct wpa_supplicant
*wpa_s
)
6289 struct wpa_ssid
*ssid
= wpa_s
->current_ssid
;
6291 if (ssid
== NULL
|| !ssid
->p2p_group
)
6294 if (wpa_s
->p2p_last_4way_hs_fail
&&
6295 wpa_s
->p2p_last_4way_hs_fail
== ssid
) {
6296 u8 go_dev_addr
[ETH_ALEN
];
6297 struct wpa_ssid
*persistent
;
6299 if (wpas_p2p_persistent_group(wpa_s
, go_dev_addr
,
6301 ssid
->ssid_len
) <= 0) {
6302 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: Could not determine whether 4-way handshake failures were for a persistent group");
6306 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: Two 4-way handshake failures for a P2P group - go_dev_addr="
6307 MACSTR
, MAC2STR(go_dev_addr
));
6308 persistent
= wpas_p2p_get_persistent(wpa_s
->parent
, go_dev_addr
,
6311 if (persistent
== NULL
|| persistent
->mode
!= WPAS_MODE_INFRA
) {
6312 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: No matching persistent group stored");
6315 wpa_msg_global(wpa_s
->parent
, MSG_INFO
,
6316 P2P_EVENT_PERSISTENT_PSK_FAIL
"%d",
6319 wpa_s
->p2p_last_4way_hs_fail
= NULL
;
6321 * Remove the group from a timeout to avoid issues with caller
6322 * continuing to use the interface if this is on a P2P group
6325 eloop_register_timeout(0, 0, wpas_p2p_psk_failure_removal
,
6330 wpa_s
->p2p_last_4way_hs_fail
= ssid
;