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/p2p_hostapd.h"
21 #include "eapol_supp/eapol_supp_sm.h"
22 #include "rsn_supp/wpa.h"
23 #include "wpa_supplicant_i.h"
26 #include "config_ssid.h"
31 #include "offchannel.h"
32 #include "wps_supplicant.h"
33 #include "p2p_supplicant.h"
37 * How many times to try to scan to find the GO before giving up on join
40 #define P2P_MAX_JOIN_SCAN_ATTEMPTS 10
42 #define P2P_AUTO_PD_SCAN_ATTEMPTS 5
44 #ifndef P2P_MAX_CLIENT_IDLE
46 * How many seconds to try to reconnect to the GO when connection in P2P client
49 #define P2P_MAX_CLIENT_IDLE 10
50 #endif /* P2P_MAX_CLIENT_IDLE */
52 #ifndef P2P_MAX_INITIAL_CONN_WAIT
54 * How many seconds to wait for initial 4-way handshake to get completed after
55 * WPS provisioning step.
57 #define P2P_MAX_INITIAL_CONN_WAIT 10
58 #endif /* P2P_MAX_INITIAL_CONN_WAIT */
60 #ifndef P2P_CONCURRENT_SEARCH_DELAY
61 #define P2P_CONCURRENT_SEARCH_DELAY 500
62 #endif /* P2P_CONCURRENT_SEARCH_DELAY */
64 enum p2p_group_removal_reason
{
65 P2P_GROUP_REMOVAL_UNKNOWN
,
66 P2P_GROUP_REMOVAL_SILENT
,
67 P2P_GROUP_REMOVAL_FORMATION_FAILED
,
68 P2P_GROUP_REMOVAL_REQUESTED
,
69 P2P_GROUP_REMOVAL_IDLE_TIMEOUT
,
70 P2P_GROUP_REMOVAL_UNAVAILABLE
,
71 P2P_GROUP_REMOVAL_GO_ENDING_SESSION
75 static void wpas_p2p_long_listen_timeout(void *eloop_ctx
, void *timeout_ctx
);
76 static struct wpa_supplicant
*
77 wpas_p2p_get_group_iface(struct wpa_supplicant
*wpa_s
, int addr_allocated
,
79 static int wpas_p2p_join_start(struct wpa_supplicant
*wpa_s
);
80 static void wpas_p2p_join_scan_req(struct wpa_supplicant
*wpa_s
, int freq
);
81 static void wpas_p2p_join_scan(void *eloop_ctx
, void *timeout_ctx
);
82 static int wpas_p2p_join(struct wpa_supplicant
*wpa_s
, const u8
*iface_addr
,
83 const u8
*dev_addr
, enum p2p_wps_method wps_method
,
85 static int wpas_p2p_create_iface(struct wpa_supplicant
*wpa_s
);
86 static void wpas_p2p_cross_connect_setup(struct wpa_supplicant
*wpa_s
);
87 static void wpas_p2p_group_idle_timeout(void *eloop_ctx
, void *timeout_ctx
);
88 static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant
*wpa_s
);
89 static void wpas_p2p_group_formation_timeout(void *eloop_ctx
,
91 static void wpas_p2p_fallback_to_go_neg(struct wpa_supplicant
*wpa_s
,
93 static int wpas_p2p_stop_find_oper(struct wpa_supplicant
*wpa_s
);
96 static void wpas_p2p_scan_res_handler(struct wpa_supplicant
*wpa_s
,
97 struct wpa_scan_results
*scan_res
)
101 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
104 wpa_printf(MSG_DEBUG
, "P2P: Scan results received (%d BSS)",
105 (int) scan_res
->num
);
107 for (i
= 0; i
< scan_res
->num
; i
++) {
108 struct wpa_scan_res
*bss
= scan_res
->res
[i
];
109 struct os_time time_tmp_age
, entry_ts
;
110 time_tmp_age
.sec
= bss
->age
/ 1000;
111 time_tmp_age
.usec
= (bss
->age
% 1000) * 1000;
112 os_time_sub(&scan_res
->fetch_time
, &time_tmp_age
, &entry_ts
);
113 if (p2p_scan_res_handler(wpa_s
->global
->p2p
, bss
->bssid
,
114 bss
->freq
, &entry_ts
, bss
->level
,
115 (const u8
*) (bss
+ 1),
120 p2p_scan_res_handled(wpa_s
->global
->p2p
);
124 static int wpas_p2p_scan(void *ctx
, enum p2p_scan_type type
, int freq
,
125 unsigned int num_req_dev_types
,
126 const u8
*req_dev_types
, const u8
*dev_id
, u16 pw_id
)
128 struct wpa_supplicant
*wpa_s
= ctx
;
129 struct wpa_supplicant
*ifs
;
130 struct wpa_driver_scan_params params
;
132 struct wpabuf
*wps_ie
, *ies
;
133 int social_channels
[] = { 2412, 2437, 2462, 0, 0 };
136 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
139 for (ifs
= wpa_s
->global
->ifaces
; ifs
; ifs
= ifs
->next
) {
140 if (ifs
->sta_scan_pending
&&
141 wpas_p2p_in_progress(wpa_s
) == 2) {
142 wpa_printf(MSG_DEBUG
, "Delaying P2P scan to allow "
143 "pending station mode scan to be "
144 "completed on interface %s", ifs
->ifname
);
145 wpa_s
->global
->p2p_cb_on_scan_complete
= 1;
146 wpa_supplicant_req_scan(ifs
, 0, 0);
151 os_memset(¶ms
, 0, sizeof(params
));
153 /* P2P Wildcard SSID */
154 params
.num_ssids
= 1;
155 params
.ssids
[0].ssid
= (u8
*) P2P_WILDCARD_SSID
;
156 params
.ssids
[0].ssid_len
= P2P_WILDCARD_SSID_LEN
;
158 wpa_s
->wps
->dev
.p2p
= 1;
159 wps_ie
= wps_build_probe_req_ie(pw_id
, &wpa_s
->wps
->dev
,
160 wpa_s
->wps
->uuid
, WPS_REQ_ENROLLEE
,
161 num_req_dev_types
, req_dev_types
);
165 ielen
= p2p_scan_ie_buf_len(wpa_s
->global
->p2p
);
166 ies
= wpabuf_alloc(wpabuf_len(wps_ie
) + ielen
);
171 wpabuf_put_buf(ies
, wps_ie
);
174 p2p_scan_ie(wpa_s
->global
->p2p
, ies
, dev_id
);
176 params
.p2p_probe
= 1;
177 params
.extra_ies
= wpabuf_head(ies
);
178 params
.extra_ies_len
= wpabuf_len(ies
);
181 case P2P_SCAN_SOCIAL
:
182 params
.freqs
= social_channels
;
186 case P2P_SCAN_SOCIAL_PLUS_ONE
:
187 social_channels
[3] = freq
;
188 params
.freqs
= social_channels
;
192 ret
= wpa_drv_scan(wpa_s
, ¶ms
);
197 for (ifs
= wpa_s
->global
->ifaces
; ifs
; ifs
= ifs
->next
) {
199 ifs
->scan_res_handler
== wpas_p2p_scan_res_handler
) {
200 wpa_s
->global
->p2p_cb_on_scan_complete
= 1;
206 wpa_s
->scan_res_handler
= wpas_p2p_scan_res_handler
;
212 static enum wpa_driver_if_type
wpas_p2p_if_type(int p2p_group_interface
)
214 switch (p2p_group_interface
) {
215 case P2P_GROUP_INTERFACE_PENDING
:
216 return WPA_IF_P2P_GROUP
;
217 case P2P_GROUP_INTERFACE_GO
:
218 return WPA_IF_P2P_GO
;
219 case P2P_GROUP_INTERFACE_CLIENT
:
220 return WPA_IF_P2P_CLIENT
;
223 return WPA_IF_P2P_GROUP
;
227 static struct wpa_supplicant
* wpas_get_p2p_group(struct wpa_supplicant
*wpa_s
,
229 size_t ssid_len
, int *go
)
233 for (wpa_s
= wpa_s
->global
->ifaces
; wpa_s
; wpa_s
= wpa_s
->next
) {
234 for (s
= wpa_s
->conf
->ssid
; s
; s
= s
->next
) {
235 if (s
->disabled
!= 0 || !s
->p2p_group
||
236 s
->ssid_len
!= ssid_len
||
237 os_memcmp(ssid
, s
->ssid
, ssid_len
) != 0)
239 if (s
->mode
== WPAS_MODE_P2P_GO
&&
240 s
!= wpa_s
->current_ssid
)
243 *go
= s
->mode
== WPAS_MODE_P2P_GO
;
252 static int wpas_p2p_group_delete(struct wpa_supplicant
*wpa_s
,
253 enum p2p_group_removal_reason removal_reason
)
255 struct wpa_ssid
*ssid
;
259 ssid
= wpa_s
->current_ssid
;
262 * The current SSID was not known, but there may still be a
263 * pending P2P group interface waiting for provisioning or a
264 * P2P group that is trying to reconnect.
266 ssid
= wpa_s
->conf
->ssid
;
268 if (ssid
->p2p_group
&& ssid
->disabled
!= 2)
273 wpa_s
->p2p_group_interface
== NOT_P2P_GROUP_INTERFACE
)
275 wpa_printf(MSG_ERROR
, "P2P: P2P group interface "
280 if (wpa_s
->p2p_group_interface
== P2P_GROUP_INTERFACE_GO
)
282 else if (wpa_s
->p2p_group_interface
== P2P_GROUP_INTERFACE_CLIENT
||
283 (ssid
&& ssid
->mode
== WPAS_MODE_INFRA
)) {
284 wpa_s
->reassociate
= 0;
285 wpa_s
->disconnected
= 1;
286 wpa_supplicant_deauthenticate(wpa_s
,
287 WLAN_REASON_DEAUTH_LEAVING
);
291 if (wpa_s
->cross_connect_in_use
) {
292 wpa_s
->cross_connect_in_use
= 0;
293 wpa_msg(wpa_s
->parent
, MSG_INFO
,
294 P2P_EVENT_CROSS_CONNECT_DISABLE
"%s %s",
295 wpa_s
->ifname
, wpa_s
->cross_connect_uplink
);
297 switch (removal_reason
) {
298 case P2P_GROUP_REMOVAL_REQUESTED
:
299 reason
= " reason=REQUESTED";
301 case P2P_GROUP_REMOVAL_FORMATION_FAILED
:
302 reason
= " reason=FORMATION_FAILED";
304 case P2P_GROUP_REMOVAL_IDLE_TIMEOUT
:
305 reason
= " reason=IDLE";
307 case P2P_GROUP_REMOVAL_UNAVAILABLE
:
308 reason
= " reason=UNAVAILABLE";
310 case P2P_GROUP_REMOVAL_GO_ENDING_SESSION
:
311 reason
= " reason=GO_ENDING_SESSION";
317 if (removal_reason
!= P2P_GROUP_REMOVAL_SILENT
) {
318 wpa_msg(wpa_s
->parent
, MSG_INFO
,
319 P2P_EVENT_GROUP_REMOVED
"%s %s%s",
320 wpa_s
->ifname
, gtype
, reason
);
323 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout
, wpa_s
, NULL
) > 0)
324 wpa_printf(MSG_DEBUG
, "P2P: Cancelled P2P group idle timeout");
325 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout
,
326 wpa_s
->parent
, NULL
) > 0) {
327 wpa_printf(MSG_DEBUG
, "P2P: Cancelled P2P group formation "
329 wpa_s
->p2p_in_provisioning
= 0;
332 if (removal_reason
!= P2P_GROUP_REMOVAL_SILENT
&& ssid
)
333 wpas_notify_p2p_group_removed(wpa_s
, ssid
, gtype
);
335 if (wpa_s
->p2p_group_interface
!= NOT_P2P_GROUP_INTERFACE
) {
336 struct wpa_global
*global
;
338 enum wpa_driver_if_type type
;
339 wpa_printf(MSG_DEBUG
, "P2P: Remove group interface %s",
341 global
= wpa_s
->global
;
342 ifname
= os_strdup(wpa_s
->ifname
);
343 type
= wpas_p2p_if_type(wpa_s
->p2p_group_interface
);
344 wpa_supplicant_remove_iface(wpa_s
->global
, wpa_s
, 0);
345 wpa_s
= global
->ifaces
;
347 wpa_drv_if_remove(wpa_s
, type
, ifname
);
352 wpa_printf(MSG_DEBUG
, "P2P: Remove temporary group network");
353 if (ssid
&& (ssid
->p2p_group
||
354 ssid
->mode
== WPAS_MODE_P2P_GROUP_FORMATION
||
355 (ssid
->key_mgmt
& WPA_KEY_MGMT_WPS
))) {
357 if (ssid
== wpa_s
->current_ssid
) {
358 wpa_sm_set_config(wpa_s
->wpa
, NULL
);
359 eapol_sm_notify_config(wpa_s
->eapol
, NULL
, NULL
);
360 wpa_s
->current_ssid
= NULL
;
363 * Networks objects created during any P2P activities are not
364 * exposed out as they might/will confuse certain non-P2P aware
365 * applications since these network objects won't behave like
368 * Likewise, we don't send out network removed signals for such
371 wpa_config_remove_network(wpa_s
->conf
, id
);
372 wpa_supplicant_clear_status(wpa_s
);
373 wpa_supplicant_cancel_sched_scan(wpa_s
);
374 wpa_s
->sta_scan_pending
= 0;
376 wpa_printf(MSG_DEBUG
, "P2P: Temporary group network not "
380 wpa_supplicant_ap_deinit(wpa_s
);
382 wpa_drv_deinit_p2p_cli(wpa_s
);
388 static int wpas_p2p_persistent_group(struct wpa_supplicant
*wpa_s
,
390 const u8
*ssid
, size_t ssid_len
)
398 if (wpa_s
->go_params
)
399 bssid
= wpa_s
->go_params
->peer_interface_addr
;
401 bssid
= wpa_s
->bssid
;
403 bss
= wpa_bss_get(wpa_s
, bssid
, ssid
, ssid_len
);
405 u8 iface_addr
[ETH_ALEN
];
406 if (p2p_get_interface_addr(wpa_s
->global
->p2p
, bssid
,
408 bss
= wpa_bss_get(wpa_s
, iface_addr
, ssid
, ssid_len
);
411 wpa_printf(MSG_DEBUG
, "P2P: Could not figure out whether "
412 "group is persistent - BSS " MACSTR
" not found",
417 p2p
= wpa_bss_get_vendor_ie_multi(bss
, P2P_IE_VENDOR_TYPE
);
419 wpa_printf(MSG_DEBUG
, "P2P: Could not figure out whether "
420 "group is persistent - BSS " MACSTR
421 " did not include P2P IE", MAC2STR(bssid
));
422 wpa_hexdump(MSG_DEBUG
, "P2P: Probe Response IEs",
423 (u8
*) (bss
+ 1), bss
->ie_len
);
424 wpa_hexdump(MSG_DEBUG
, "P2P: Beacon IEs",
425 ((u8
*) bss
+ 1) + bss
->ie_len
,
430 group_capab
= p2p_get_group_capab(p2p
);
431 addr
= p2p_get_go_dev_addr(p2p
);
432 wpa_printf(MSG_DEBUG
, "P2P: Checking whether group is persistent: "
433 "group_capab=0x%x", group_capab
);
435 os_memcpy(go_dev_addr
, addr
, ETH_ALEN
);
436 wpa_printf(MSG_DEBUG
, "P2P: GO Device Address " MACSTR
,
439 os_memset(go_dev_addr
, 0, ETH_ALEN
);
442 wpa_printf(MSG_DEBUG
, "P2P: BSS " MACSTR
" group_capab=0x%x "
443 "go_dev_addr=" MACSTR
,
444 MAC2STR(bssid
), group_capab
, MAC2STR(go_dev_addr
));
446 return group_capab
& P2P_GROUP_CAPAB_PERSISTENT_GROUP
;
450 static int wpas_p2p_store_persistent_group(struct wpa_supplicant
*wpa_s
,
451 struct wpa_ssid
*ssid
,
452 const u8
*go_dev_addr
)
457 wpa_printf(MSG_DEBUG
, "P2P: Storing credentials for a persistent "
458 "group (GO Dev Addr " MACSTR
")", MAC2STR(go_dev_addr
));
459 for (s
= wpa_s
->conf
->ssid
; s
; s
= s
->next
) {
460 if (s
->disabled
== 2 &&
461 os_memcmp(go_dev_addr
, s
->bssid
, ETH_ALEN
) == 0 &&
462 s
->ssid_len
== ssid
->ssid_len
&&
463 os_memcmp(ssid
->ssid
, s
->ssid
, ssid
->ssid_len
) == 0)
468 wpa_printf(MSG_DEBUG
, "P2P: Update existing persistent group "
470 if (ssid
->passphrase
&& !s
->passphrase
)
472 else if (ssid
->passphrase
&& s
->passphrase
&&
473 os_strcmp(ssid
->passphrase
, s
->passphrase
) != 0)
476 wpa_printf(MSG_DEBUG
, "P2P: Create a new persistent group "
479 s
= wpa_config_add_network(wpa_s
->conf
);
484 * Instead of network_added we emit persistent_group_added
485 * notification. Also to keep the defense checks in
486 * persistent_group obj registration method, we set the
487 * relevant flags in s to designate it as a persistent group.
490 s
->p2p_persistent_group
= 1;
491 wpas_notify_persistent_group_added(wpa_s
, s
);
492 wpa_config_set_network_defaults(s
);
496 s
->p2p_persistent_group
= 1;
499 os_memcpy(s
->bssid
, go_dev_addr
, ETH_ALEN
);
500 s
->mode
= ssid
->mode
;
501 s
->auth_alg
= WPA_AUTH_ALG_OPEN
;
502 s
->key_mgmt
= WPA_KEY_MGMT_PSK
;
503 s
->proto
= WPA_PROTO_RSN
;
504 s
->pairwise_cipher
= WPA_CIPHER_CCMP
;
506 if (ssid
->passphrase
) {
507 os_free(s
->passphrase
);
508 s
->passphrase
= os_strdup(ssid
->passphrase
);
512 os_memcpy(s
->psk
, ssid
->psk
, 32);
514 if (s
->passphrase
&& !s
->psk_set
)
515 wpa_config_update_psk(s
);
516 if (s
->ssid
== NULL
|| s
->ssid_len
< ssid
->ssid_len
) {
518 s
->ssid
= os_malloc(ssid
->ssid_len
);
521 s
->ssid_len
= ssid
->ssid_len
;
522 os_memcpy(s
->ssid
, ssid
->ssid
, s
->ssid_len
);
525 #ifndef CONFIG_NO_CONFIG_WRITE
526 if (changed
&& wpa_s
->conf
->update_config
&&
527 wpa_config_write(wpa_s
->confname
, wpa_s
->conf
)) {
528 wpa_printf(MSG_DEBUG
, "P2P: Failed to update configuration");
530 #endif /* CONFIG_NO_CONFIG_WRITE */
536 static void wpas_p2p_add_persistent_group_client(struct wpa_supplicant
*wpa_s
,
539 struct wpa_ssid
*ssid
, *s
;
544 ssid
= wpa_s
->current_ssid
;
545 if (ssid
== NULL
|| ssid
->mode
!= WPAS_MODE_P2P_GO
||
546 !ssid
->p2p_persistent_group
)
549 for (s
= wpa_s
->parent
->conf
->ssid
; s
; s
= s
->next
) {
550 if (s
->disabled
!= 2 || s
->mode
!= WPAS_MODE_P2P_GO
)
553 if (s
->ssid_len
== ssid
->ssid_len
&&
554 os_memcmp(s
->ssid
, ssid
->ssid
, s
->ssid_len
) == 0)
561 for (i
= 0; s
->p2p_client_list
&& i
< s
->num_p2p_clients
; i
++) {
562 if (os_memcmp(s
->p2p_client_list
+ i
* ETH_ALEN
, addr
,
566 if (i
== s
->num_p2p_clients
- 1)
567 return; /* already the most recent entry */
569 /* move the entry to mark it most recent */
570 os_memmove(s
->p2p_client_list
+ i
* ETH_ALEN
,
571 s
->p2p_client_list
+ (i
+ 1) * ETH_ALEN
,
572 (s
->num_p2p_clients
- i
- 1) * ETH_ALEN
);
573 os_memcpy(s
->p2p_client_list
+
574 (s
->num_p2p_clients
- 1) * ETH_ALEN
, addr
, ETH_ALEN
);
579 if (!found
&& s
->num_p2p_clients
< P2P_MAX_STORED_CLIENTS
) {
580 n
= os_realloc_array(s
->p2p_client_list
,
581 s
->num_p2p_clients
+ 1, ETH_ALEN
);
584 os_memcpy(n
+ s
->num_p2p_clients
* ETH_ALEN
, addr
, ETH_ALEN
);
585 s
->p2p_client_list
= n
;
586 s
->num_p2p_clients
++;
588 /* Not enough room for an additional entry - drop the oldest
590 os_memmove(s
->p2p_client_list
,
591 s
->p2p_client_list
+ ETH_ALEN
,
592 (s
->num_p2p_clients
- 1) * ETH_ALEN
);
593 os_memcpy(s
->p2p_client_list
+
594 (s
->num_p2p_clients
- 1) * ETH_ALEN
,
598 #ifndef CONFIG_NO_CONFIG_WRITE
599 if (wpa_s
->parent
->conf
->update_config
&&
600 wpa_config_write(wpa_s
->parent
->confname
, wpa_s
->parent
->conf
))
601 wpa_printf(MSG_DEBUG
, "P2P: Failed to update configuration");
602 #endif /* CONFIG_NO_CONFIG_WRITE */
606 static void wpas_group_formation_completed(struct wpa_supplicant
*wpa_s
,
609 struct wpa_ssid
*ssid
;
610 const char *ssid_txt
;
613 u8 go_dev_addr
[ETH_ALEN
];
617 * This callback is likely called for the main interface. Update wpa_s
618 * to use the group interface if a new interface was created for the
621 if (wpa_s
->global
->p2p_group_formation
)
622 wpa_s
= wpa_s
->global
->p2p_group_formation
;
623 wpa_s
->global
->p2p_group_formation
= NULL
;
624 wpa_s
->p2p_in_provisioning
= 0;
627 wpa_msg(wpa_s
->parent
, MSG_INFO
,
628 P2P_EVENT_GROUP_FORMATION_FAILURE
);
629 wpas_p2p_group_delete(wpa_s
,
630 P2P_GROUP_REMOVAL_FORMATION_FAILED
);
634 wpa_msg(wpa_s
->parent
, MSG_INFO
, P2P_EVENT_GROUP_FORMATION_SUCCESS
);
636 ssid
= wpa_s
->current_ssid
;
637 if (ssid
&& ssid
->mode
== WPAS_MODE_P2P_GROUP_FORMATION
) {
638 ssid
->mode
= WPAS_MODE_P2P_GO
;
639 p2p_group_notif_formation_done(wpa_s
->p2p_group
);
640 wpa_supplicant_ap_mac_addr_filter(wpa_s
, NULL
);
645 ssid_txt
= wpa_ssid_txt(ssid
->ssid
, ssid
->ssid_len
);
646 client
= ssid
->mode
== WPAS_MODE_INFRA
;
647 if (ssid
->mode
== WPAS_MODE_P2P_GO
) {
648 persistent
= ssid
->p2p_persistent_group
;
649 os_memcpy(go_dev_addr
, wpa_s
->global
->p2p_dev_addr
,
652 persistent
= wpas_p2p_persistent_group(wpa_s
,
658 client
= wpa_s
->p2p_group_interface
==
659 P2P_GROUP_INTERFACE_CLIENT
;
660 os_memset(go_dev_addr
, 0, ETH_ALEN
);
663 wpa_s
->show_group_started
= 0;
666 * Indicate event only after successfully completed 4-way
667 * handshake, i.e., when the interface is ready for data
670 wpa_s
->show_group_started
= 1;
671 } else if (ssid
&& ssid
->passphrase
== NULL
&& ssid
->psk_set
) {
673 wpa_snprintf_hex(psk
, sizeof(psk
), ssid
->psk
, 32);
674 wpa_msg(wpa_s
->parent
, MSG_INFO
, P2P_EVENT_GROUP_STARTED
675 "%s GO ssid=\"%s\" freq=%d psk=%s go_dev_addr=" MACSTR
677 wpa_s
->ifname
, ssid_txt
, ssid
->frequency
, psk
,
678 MAC2STR(go_dev_addr
),
679 persistent
? " [PERSISTENT]" : "");
680 wpas_p2p_cross_connect_setup(wpa_s
);
681 wpas_p2p_set_group_idle_timeout(wpa_s
);
683 wpa_msg(wpa_s
->parent
, MSG_INFO
, P2P_EVENT_GROUP_STARTED
684 "%s GO ssid=\"%s\" freq=%d passphrase=\"%s\" "
685 "go_dev_addr=" MACSTR
"%s",
686 wpa_s
->ifname
, ssid_txt
, ssid
? ssid
->frequency
: 0,
687 ssid
&& ssid
->passphrase
? ssid
->passphrase
: "",
688 MAC2STR(go_dev_addr
),
689 persistent
? " [PERSISTENT]" : "");
690 wpas_p2p_cross_connect_setup(wpa_s
);
691 wpas_p2p_set_group_idle_timeout(wpa_s
);
695 network_id
= wpas_p2p_store_persistent_group(wpa_s
->parent
,
697 if (network_id
< 0 && ssid
)
698 network_id
= ssid
->id
;
700 wpas_notify_p2p_group_started(wpa_s
, ssid
, network_id
, 0);
704 static void wpas_p2p_send_action_tx_status(struct wpa_supplicant
*wpa_s
,
706 const u8
*dst
, const u8
*src
,
708 const u8
*data
, size_t data_len
,
709 enum offchannel_send_action_result
712 enum p2p_send_action_result res
= P2P_SEND_ACTION_SUCCESS
;
714 if (wpa_s
->global
->p2p
== NULL
|| wpa_s
->global
->p2p_disabled
)
716 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
720 case OFFCHANNEL_SEND_ACTION_SUCCESS
:
721 res
= P2P_SEND_ACTION_SUCCESS
;
723 case OFFCHANNEL_SEND_ACTION_NO_ACK
:
724 res
= P2P_SEND_ACTION_NO_ACK
;
726 case OFFCHANNEL_SEND_ACTION_FAILED
:
727 res
= P2P_SEND_ACTION_FAILED
;
731 p2p_send_action_cb(wpa_s
->global
->p2p
, freq
, dst
, src
, bssid
, res
);
733 if (result
!= OFFCHANNEL_SEND_ACTION_SUCCESS
&&
734 wpa_s
->pending_pd_before_join
&&
735 (os_memcmp(dst
, wpa_s
->pending_join_dev_addr
, ETH_ALEN
) == 0 ||
736 os_memcmp(dst
, wpa_s
->pending_join_iface_addr
, ETH_ALEN
) == 0) &&
737 wpa_s
->p2p_fallback_to_go_neg
) {
738 wpa_s
->pending_pd_before_join
= 0;
739 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: No ACK for PD Req "
740 "during p2p_connect-auto");
741 wpas_p2p_fallback_to_go_neg(wpa_s
, 0);
747 static int wpas_send_action(void *ctx
, unsigned int freq
, const u8
*dst
,
748 const u8
*src
, const u8
*bssid
, const u8
*buf
,
749 size_t len
, unsigned int wait_time
)
751 struct wpa_supplicant
*wpa_s
= ctx
;
752 return offchannel_send_action(wpa_s
, freq
, dst
, src
, bssid
, buf
, len
,
754 wpas_p2p_send_action_tx_status
, 1);
758 static void wpas_send_action_done(void *ctx
)
760 struct wpa_supplicant
*wpa_s
= ctx
;
761 offchannel_send_action_done(wpa_s
);
765 static int wpas_copy_go_neg_results(struct wpa_supplicant
*wpa_s
,
766 struct p2p_go_neg_results
*params
)
768 if (wpa_s
->go_params
== NULL
) {
769 wpa_s
->go_params
= os_malloc(sizeof(*params
));
770 if (wpa_s
->go_params
== NULL
)
773 os_memcpy(wpa_s
->go_params
, params
, sizeof(*params
));
778 static void wpas_start_wps_enrollee(struct wpa_supplicant
*wpa_s
,
779 struct p2p_go_neg_results
*res
)
781 wpa_printf(MSG_DEBUG
, "P2P: Start WPS Enrollee for peer " MACSTR
,
782 MAC2STR(res
->peer_interface_addr
));
783 wpa_hexdump_ascii(MSG_DEBUG
, "P2P: Start WPS Enrollee for SSID",
784 res
->ssid
, res
->ssid_len
);
785 wpa_supplicant_ap_deinit(wpa_s
);
786 wpas_copy_go_neg_results(wpa_s
, res
);
787 if (res
->wps_method
== WPS_PBC
)
788 wpas_wps_start_pbc(wpa_s
, res
->peer_interface_addr
, 1);
790 u16 dev_pw_id
= DEV_PW_DEFAULT
;
791 if (wpa_s
->p2p_wps_method
== WPS_PIN_KEYPAD
)
792 dev_pw_id
= DEV_PW_REGISTRAR_SPECIFIED
;
793 wpas_wps_start_pin(wpa_s
, res
->peer_interface_addr
,
794 wpa_s
->p2p_pin
, 1, dev_pw_id
);
799 static void p2p_go_configured(void *ctx
, void *data
)
801 struct wpa_supplicant
*wpa_s
= ctx
;
802 struct p2p_go_neg_results
*params
= data
;
803 struct wpa_ssid
*ssid
;
806 ssid
= wpa_s
->current_ssid
;
807 if (ssid
&& ssid
->mode
== WPAS_MODE_P2P_GO
) {
808 wpa_printf(MSG_DEBUG
, "P2P: Group setup without provisioning");
809 if (wpa_s
->global
->p2p_group_formation
== wpa_s
)
810 wpa_s
->global
->p2p_group_formation
= NULL
;
811 if (os_strlen(params
->passphrase
) > 0) {
812 wpa_msg(wpa_s
->parent
, MSG_INFO
, P2P_EVENT_GROUP_STARTED
813 "%s GO ssid=\"%s\" freq=%d passphrase=\"%s\" "
814 "go_dev_addr=" MACSTR
"%s", wpa_s
->ifname
,
815 wpa_ssid_txt(ssid
->ssid
, ssid
->ssid_len
),
816 ssid
->frequency
, params
->passphrase
,
817 MAC2STR(wpa_s
->global
->p2p_dev_addr
),
818 params
->persistent_group
? " [PERSISTENT]" :
822 wpa_snprintf_hex(psk
, sizeof(psk
), params
->psk
,
823 sizeof(params
->psk
));
824 wpa_msg(wpa_s
->parent
, MSG_INFO
, P2P_EVENT_GROUP_STARTED
825 "%s GO ssid=\"%s\" freq=%d psk=%s "
826 "go_dev_addr=" MACSTR
"%s", wpa_s
->ifname
,
827 wpa_ssid_txt(ssid
->ssid
, ssid
->ssid_len
),
828 ssid
->frequency
, psk
,
829 MAC2STR(wpa_s
->global
->p2p_dev_addr
),
830 params
->persistent_group
? " [PERSISTENT]" :
834 if (params
->persistent_group
)
835 network_id
= wpas_p2p_store_persistent_group(
837 wpa_s
->global
->p2p_dev_addr
);
839 network_id
= ssid
->id
;
840 wpas_notify_p2p_group_started(wpa_s
, ssid
, network_id
, 0);
841 wpas_p2p_cross_connect_setup(wpa_s
);
842 wpas_p2p_set_group_idle_timeout(wpa_s
);
846 wpa_printf(MSG_DEBUG
, "P2P: Setting up WPS for GO provisioning");
847 if (wpa_supplicant_ap_mac_addr_filter(wpa_s
,
848 params
->peer_interface_addr
)) {
849 wpa_printf(MSG_DEBUG
, "P2P: Failed to setup MAC address "
853 if (params
->wps_method
== WPS_PBC
)
854 wpa_supplicant_ap_wps_pbc(wpa_s
, params
->peer_interface_addr
,
855 params
->peer_device_addr
);
856 else if (wpa_s
->p2p_pin
[0])
857 wpa_supplicant_ap_wps_pin(wpa_s
, params
->peer_interface_addr
,
858 wpa_s
->p2p_pin
, NULL
, 0, 0);
859 os_free(wpa_s
->go_params
);
860 wpa_s
->go_params
= NULL
;
864 static void wpas_start_wps_go(struct wpa_supplicant
*wpa_s
,
865 struct p2p_go_neg_results
*params
,
868 struct wpa_ssid
*ssid
;
870 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: Starting GO");
871 if (wpas_copy_go_neg_results(wpa_s
, params
) < 0) {
872 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: Could not copy GO Negotiation "
877 ssid
= wpa_config_add_network(wpa_s
->conf
);
879 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: Could not add network for GO");
883 wpa_s
->show_group_started
= 0;
885 wpa_config_set_network_defaults(ssid
);
888 ssid
->p2p_persistent_group
= params
->persistent_group
;
889 ssid
->mode
= group_formation
? WPAS_MODE_P2P_GROUP_FORMATION
:
891 ssid
->frequency
= params
->freq
;
892 ssid
->ht40
= params
->ht40
;
893 ssid
->ssid
= os_zalloc(params
->ssid_len
+ 1);
895 os_memcpy(ssid
->ssid
, params
->ssid
, params
->ssid_len
);
896 ssid
->ssid_len
= params
->ssid_len
;
898 ssid
->auth_alg
= WPA_AUTH_ALG_OPEN
;
899 ssid
->key_mgmt
= WPA_KEY_MGMT_PSK
;
900 ssid
->proto
= WPA_PROTO_RSN
;
901 ssid
->pairwise_cipher
= WPA_CIPHER_CCMP
;
902 if (os_strlen(params
->passphrase
) > 0) {
903 ssid
->passphrase
= os_strdup(params
->passphrase
);
904 if (ssid
->passphrase
== NULL
) {
905 wpa_msg(wpa_s
, MSG_ERROR
, "P2P: Failed to copy "
906 "passphrase for GO");
907 wpa_config_remove_network(wpa_s
->conf
, ssid
->id
);
911 ssid
->passphrase
= NULL
;
912 ssid
->psk_set
= params
->psk_set
;
914 os_memcpy(ssid
->psk
, params
->psk
, sizeof(ssid
->psk
));
915 else if (ssid
->passphrase
)
916 wpa_config_update_psk(ssid
);
917 ssid
->ap_max_inactivity
= wpa_s
->parent
->conf
->p2p_go_max_inactivity
;
919 wpa_s
->ap_configured_cb
= p2p_go_configured
;
920 wpa_s
->ap_configured_cb_ctx
= wpa_s
;
921 wpa_s
->ap_configured_cb_data
= wpa_s
->go_params
;
922 wpa_s
->connect_without_scan
= ssid
;
923 wpa_s
->reassociate
= 1;
924 wpa_s
->disconnected
= 0;
925 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: Request scan (that will be skipped) to "
927 wpa_supplicant_req_scan(wpa_s
, 0, 0);
931 static void wpas_p2p_clone_config(struct wpa_supplicant
*dst
,
932 const struct wpa_supplicant
*src
)
934 struct wpa_config
*d
;
935 const struct wpa_config
*s
;
940 #define C(n) if (s->n) d->n = os_strdup(s->n)
949 os_memcpy(d
->device_type
, s
->device_type
, WPS_DEV_TYPE_LEN
);
950 os_memcpy(d
->sec_device_type
, s
->sec_device_type
,
951 sizeof(d
->sec_device_type
));
952 d
->num_sec_device_types
= s
->num_sec_device_types
;
954 d
->p2p_group_idle
= s
->p2p_group_idle
;
955 d
->p2p_intra_bss
= s
->p2p_intra_bss
;
956 d
->persistent_reconnect
= s
->persistent_reconnect
;
957 d
->max_num_sta
= s
->max_num_sta
;
958 d
->pbc_in_m1
= s
->pbc_in_m1
;
962 static int wpas_p2p_add_group_interface(struct wpa_supplicant
*wpa_s
,
963 enum wpa_driver_if_type type
)
965 char ifname
[120], force_ifname
[120];
967 if (wpa_s
->pending_interface_name
[0]) {
968 wpa_printf(MSG_DEBUG
, "P2P: Pending virtual interface exists "
969 "- skip creation of a new one");
970 if (is_zero_ether_addr(wpa_s
->pending_interface_addr
)) {
971 wpa_printf(MSG_DEBUG
, "P2P: Pending virtual address "
972 "unknown?! ifname='%s'",
973 wpa_s
->pending_interface_name
);
979 os_snprintf(ifname
, sizeof(ifname
), "p2p-%s-%d", wpa_s
->ifname
,
980 wpa_s
->p2p_group_idx
);
981 if (os_strlen(ifname
) >= IFNAMSIZ
&&
982 os_strlen(wpa_s
->ifname
) < IFNAMSIZ
) {
983 /* Try to avoid going over the IFNAMSIZ length limit */
984 os_snprintf(ifname
, sizeof(ifname
), "p2p-%d",
985 wpa_s
->p2p_group_idx
);
987 force_ifname
[0] = '\0';
989 wpa_printf(MSG_DEBUG
, "P2P: Create a new interface %s for the group",
991 wpa_s
->p2p_group_idx
++;
993 wpa_s
->pending_interface_type
= type
;
994 if (wpa_drv_if_add(wpa_s
, type
, ifname
, NULL
, NULL
, force_ifname
,
995 wpa_s
->pending_interface_addr
, NULL
) < 0) {
996 wpa_printf(MSG_ERROR
, "P2P: Failed to create new group "
1001 if (force_ifname
[0]) {
1002 wpa_printf(MSG_DEBUG
, "P2P: Driver forced interface name %s",
1004 os_strlcpy(wpa_s
->pending_interface_name
, force_ifname
,
1005 sizeof(wpa_s
->pending_interface_name
));
1007 os_strlcpy(wpa_s
->pending_interface_name
, ifname
,
1008 sizeof(wpa_s
->pending_interface_name
));
1009 wpa_printf(MSG_DEBUG
, "P2P: Created pending virtual interface %s addr "
1010 MACSTR
, wpa_s
->pending_interface_name
,
1011 MAC2STR(wpa_s
->pending_interface_addr
));
1017 static void wpas_p2p_remove_pending_group_interface(
1018 struct wpa_supplicant
*wpa_s
)
1020 if (!wpa_s
->pending_interface_name
[0] ||
1021 is_zero_ether_addr(wpa_s
->pending_interface_addr
))
1022 return; /* No pending virtual interface */
1024 wpa_printf(MSG_DEBUG
, "P2P: Removing pending group interface %s",
1025 wpa_s
->pending_interface_name
);
1026 wpa_drv_if_remove(wpa_s
, wpa_s
->pending_interface_type
,
1027 wpa_s
->pending_interface_name
);
1028 os_memset(wpa_s
->pending_interface_addr
, 0, ETH_ALEN
);
1029 wpa_s
->pending_interface_name
[0] = '\0';
1033 static struct wpa_supplicant
*
1034 wpas_p2p_init_group_interface(struct wpa_supplicant
*wpa_s
, int go
)
1036 struct wpa_interface iface
;
1037 struct wpa_supplicant
*group_wpa_s
;
1039 if (!wpa_s
->pending_interface_name
[0]) {
1040 wpa_printf(MSG_ERROR
, "P2P: No pending group interface");
1041 if (!wpas_p2p_create_iface(wpa_s
))
1044 * Something has forced us to remove the pending interface; try
1045 * to create a new one and hope for the best that we will get
1046 * the same local address.
1048 if (wpas_p2p_add_group_interface(wpa_s
, go
? WPA_IF_P2P_GO
:
1049 WPA_IF_P2P_CLIENT
) < 0)
1053 os_memset(&iface
, 0, sizeof(iface
));
1054 iface
.ifname
= wpa_s
->pending_interface_name
;
1055 iface
.driver
= wpa_s
->driver
->name
;
1056 iface
.ctrl_interface
= wpa_s
->conf
->ctrl_interface
;
1057 iface
.driver_param
= wpa_s
->conf
->driver_param
;
1058 group_wpa_s
= wpa_supplicant_add_iface(wpa_s
->global
, &iface
);
1059 if (group_wpa_s
== NULL
) {
1060 wpa_printf(MSG_ERROR
, "P2P: Failed to create new "
1061 "wpa_supplicant interface");
1064 wpa_s
->pending_interface_name
[0] = '\0';
1065 group_wpa_s
->parent
= wpa_s
;
1066 group_wpa_s
->p2p_group_interface
= go
? P2P_GROUP_INTERFACE_GO
:
1067 P2P_GROUP_INTERFACE_CLIENT
;
1068 wpa_s
->global
->p2p_group_formation
= group_wpa_s
;
1070 wpas_p2p_clone_config(group_wpa_s
, wpa_s
);
1076 static void wpas_p2p_group_formation_timeout(void *eloop_ctx
,
1079 struct wpa_supplicant
*wpa_s
= eloop_ctx
;
1080 wpa_printf(MSG_DEBUG
, "P2P: Group Formation timed out");
1081 if (wpa_s
->global
->p2p
)
1082 p2p_group_formation_failed(wpa_s
->global
->p2p
);
1083 else if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
1084 wpa_drv_p2p_group_formation_failed(wpa_s
);
1085 wpas_group_formation_completed(wpa_s
, 0);
1089 void wpas_go_neg_completed(void *ctx
, struct p2p_go_neg_results
*res
)
1091 struct wpa_supplicant
*wpa_s
= ctx
;
1093 if (wpa_s
->off_channel_freq
|| wpa_s
->roc_waiting_drv_freq
) {
1094 wpa_drv_cancel_remain_on_channel(wpa_s
);
1095 wpa_s
->off_channel_freq
= 0;
1096 wpa_s
->roc_waiting_drv_freq
= 0;
1100 wpa_msg(wpa_s
, MSG_INFO
, P2P_EVENT_GO_NEG_FAILURE
"status=%d",
1102 wpas_notify_p2p_go_neg_completed(wpa_s
, res
);
1103 wpas_p2p_remove_pending_group_interface(wpa_s
);
1107 if (wpa_s
->p2p_go_ht40
)
1110 wpa_msg(wpa_s
, MSG_INFO
, P2P_EVENT_GO_NEG_SUCCESS
);
1111 wpas_notify_p2p_go_neg_completed(wpa_s
, res
);
1113 if (res
->role_go
&& wpa_s
->p2p_persistent_id
>= 0) {
1114 struct wpa_ssid
*ssid
;
1115 ssid
= wpa_config_get_network(wpa_s
->conf
,
1116 wpa_s
->p2p_persistent_id
);
1117 if (ssid
&& ssid
->disabled
== 2 &&
1118 ssid
->mode
== WPAS_MODE_P2P_GO
&& ssid
->passphrase
) {
1119 size_t len
= os_strlen(ssid
->passphrase
);
1120 wpa_printf(MSG_DEBUG
, "P2P: Override passphrase based "
1121 "on requested persistent group");
1122 os_memcpy(res
->passphrase
, ssid
->passphrase
, len
);
1123 res
->passphrase
[len
] = '\0';
1127 if (wpa_s
->create_p2p_iface
) {
1128 struct wpa_supplicant
*group_wpa_s
=
1129 wpas_p2p_init_group_interface(wpa_s
, res
->role_go
);
1130 if (group_wpa_s
== NULL
) {
1131 wpas_p2p_remove_pending_group_interface(wpa_s
);
1134 if (group_wpa_s
!= wpa_s
) {
1135 os_memcpy(group_wpa_s
->p2p_pin
, wpa_s
->p2p_pin
,
1136 sizeof(group_wpa_s
->p2p_pin
));
1137 group_wpa_s
->p2p_wps_method
= wpa_s
->p2p_wps_method
;
1139 os_memset(wpa_s
->pending_interface_addr
, 0, ETH_ALEN
);
1140 wpa_s
->pending_interface_name
[0] = '\0';
1141 group_wpa_s
->p2p_in_provisioning
= 1;
1144 wpas_start_wps_go(group_wpa_s
, res
, 1);
1146 wpas_start_wps_enrollee(group_wpa_s
, res
);
1148 wpa_s
->p2p_in_provisioning
= 1;
1149 wpa_s
->global
->p2p_group_formation
= wpa_s
;
1152 wpas_start_wps_go(wpa_s
, res
, 1);
1154 wpas_start_wps_enrollee(ctx
, res
);
1157 wpa_s
->p2p_long_listen
= 0;
1158 eloop_cancel_timeout(wpas_p2p_long_listen_timeout
, wpa_s
, NULL
);
1160 eloop_cancel_timeout(wpas_p2p_group_formation_timeout
, wpa_s
, NULL
);
1161 eloop_register_timeout(15 + res
->peer_config_timeout
/ 100,
1162 (res
->peer_config_timeout
% 100) * 10000,
1163 wpas_p2p_group_formation_timeout
, wpa_s
, NULL
);
1167 void wpas_go_neg_req_rx(void *ctx
, const u8
*src
, u16 dev_passwd_id
)
1169 struct wpa_supplicant
*wpa_s
= ctx
;
1170 wpa_msg(wpa_s
, MSG_INFO
, P2P_EVENT_GO_NEG_REQUEST MACSTR
1171 " dev_passwd_id=%u", MAC2STR(src
), dev_passwd_id
);
1173 wpas_notify_p2p_go_neg_req(wpa_s
, src
, dev_passwd_id
);
1177 void wpas_dev_found(void *ctx
, const u8
*addr
,
1178 const struct p2p_peer_info
*info
,
1181 #ifndef CONFIG_NO_STDOUT_DEBUG
1182 struct wpa_supplicant
*wpa_s
= ctx
;
1183 char devtype
[WPS_DEV_TYPE_BUFSIZE
];
1185 wpa_msg(wpa_s
, MSG_INFO
, P2P_EVENT_DEVICE_FOUND MACSTR
1186 " p2p_dev_addr=" MACSTR
1187 " pri_dev_type=%s name='%s' config_methods=0x%x "
1188 "dev_capab=0x%x group_capab=0x%x",
1189 MAC2STR(addr
), MAC2STR(info
->p2p_device_addr
),
1190 wps_dev_type_bin2str(info
->pri_dev_type
, devtype
,
1192 info
->device_name
, info
->config_methods
,
1193 info
->dev_capab
, info
->group_capab
);
1194 #endif /* CONFIG_NO_STDOUT_DEBUG */
1196 wpas_notify_p2p_device_found(ctx
, info
->p2p_device_addr
, new_device
);
1200 static void wpas_dev_lost(void *ctx
, const u8
*dev_addr
)
1202 struct wpa_supplicant
*wpa_s
= ctx
;
1204 wpa_msg(wpa_s
, MSG_INFO
, P2P_EVENT_DEVICE_LOST
1205 "p2p_dev_addr=" MACSTR
, MAC2STR(dev_addr
));
1207 wpas_notify_p2p_device_lost(wpa_s
, dev_addr
);
1211 static int wpas_start_listen(void *ctx
, unsigned int freq
,
1212 unsigned int duration
,
1213 const struct wpabuf
*probe_resp_ie
)
1215 struct wpa_supplicant
*wpa_s
= ctx
;
1217 wpa_drv_set_ap_wps_ie(wpa_s
, NULL
, probe_resp_ie
, NULL
);
1219 if (wpa_drv_probe_req_report(wpa_s
, 1) < 0) {
1220 wpa_printf(MSG_DEBUG
, "P2P: Failed to request the driver to "
1221 "report received Probe Request frames");
1225 wpa_s
->pending_listen_freq
= freq
;
1226 wpa_s
->pending_listen_duration
= duration
;
1228 if (wpa_drv_remain_on_channel(wpa_s
, freq
, duration
) < 0) {
1229 wpa_printf(MSG_DEBUG
, "P2P: Failed to request the driver "
1230 "to remain on channel (%u MHz) for Listen "
1232 wpa_s
->pending_listen_freq
= 0;
1235 wpa_s
->off_channel_freq
= 0;
1236 wpa_s
->roc_waiting_drv_freq
= freq
;
1242 static void wpas_stop_listen(void *ctx
)
1244 struct wpa_supplicant
*wpa_s
= ctx
;
1245 if (wpa_s
->off_channel_freq
|| wpa_s
->roc_waiting_drv_freq
) {
1246 wpa_drv_cancel_remain_on_channel(wpa_s
);
1247 wpa_s
->off_channel_freq
= 0;
1248 wpa_s
->roc_waiting_drv_freq
= 0;
1250 wpa_drv_set_ap_wps_ie(wpa_s
, NULL
, NULL
, NULL
);
1251 wpa_drv_probe_req_report(wpa_s
, 0);
1255 static int wpas_send_probe_resp(void *ctx
, const struct wpabuf
*buf
)
1257 struct wpa_supplicant
*wpa_s
= ctx
;
1258 return wpa_drv_send_mlme(wpa_s
, wpabuf_head(buf
), wpabuf_len(buf
), 1);
1263 * DNS Header section is used only to calculate compression pointers, so the
1264 * contents of this data does not matter, but the length needs to be reserved
1265 * in the virtual packet.
1267 #define DNS_HEADER_LEN 12
1270 * 27-octet in-memory packet from P2P specification containing two implied
1271 * queries for _tcp.lcoal. PTR IN and _udp.local. PTR IN
1273 #define P2P_SD_IN_MEMORY_LEN 27
1275 static int p2p_sd_dns_uncompress_label(char **upos
, char *uend
, u8
*start
,
1276 u8
**spos
, const u8
*end
)
1278 while (*spos
< end
) {
1279 u8 val
= ((*spos
)[0] & 0xc0) >> 6;
1282 if (val
== 1 || val
== 2) {
1283 /* These are reserved values in RFC 1035 */
1284 wpa_printf(MSG_DEBUG
, "P2P: Invalid domain name "
1285 "sequence starting with 0x%x", val
);
1294 if (*spos
+ 2 > end
) {
1295 wpa_printf(MSG_DEBUG
, "P2P: No room for full "
1296 "DNS offset field");
1300 offset
= (((*spos
)[0] & 0x3f) << 8) | (*spos
)[1];
1301 if (offset
>= *spos
- start
) {
1302 wpa_printf(MSG_DEBUG
, "P2P: Invalid DNS "
1303 "pointer offset %u", offset
);
1308 spos_tmp
= start
+ offset
;
1309 return p2p_sd_dns_uncompress_label(upos
, uend
, start
,
1315 len
= (*spos
)[0] & 0x3f;
1320 if (*spos
+ len
> end
) {
1321 wpa_printf(MSG_DEBUG
, "P2P: Invalid domain name "
1322 "sequence - no room for label with length "
1327 if (*upos
+ len
+ 2 > uend
)
1330 os_memcpy(*upos
, *spos
, len
);
1342 /* Uncompress domain names per RFC 1035 using the P2P SD in-memory packet.
1343 * Returns -1 on parsing error (invalid input sequence), -2 if output buffer is
1344 * not large enough */
1345 static int p2p_sd_dns_uncompress(char *buf
, size_t buf_len
, const u8
*msg
,
1346 size_t msg_len
, size_t offset
)
1348 /* 27-octet in-memory packet from P2P specification */
1349 const char *prefix
= "\x04_tcp\x05local\x00\x00\x0C\x00\x01"
1350 "\x04_udp\xC0\x11\x00\x0C\x00\x01";
1351 u8
*tmp
, *end
, *spos
;
1357 if (offset
> msg_len
)
1360 tmp
= os_malloc(DNS_HEADER_LEN
+ P2P_SD_IN_MEMORY_LEN
+ msg_len
);
1363 spos
= tmp
+ DNS_HEADER_LEN
+ P2P_SD_IN_MEMORY_LEN
;
1364 end
= spos
+ msg_len
;
1367 os_memset(tmp
, 0, DNS_HEADER_LEN
);
1368 os_memcpy(tmp
+ DNS_HEADER_LEN
, prefix
, P2P_SD_IN_MEMORY_LEN
);
1369 os_memcpy(tmp
+ DNS_HEADER_LEN
+ P2P_SD_IN_MEMORY_LEN
, msg
, msg_len
);
1372 uend
= buf
+ buf_len
;
1374 ret
= p2p_sd_dns_uncompress_label(&upos
, uend
, tmp
, &spos
, end
);
1383 } else if (upos
[-1] == '.')
1391 static struct p2p_srv_bonjour
*
1392 wpas_p2p_service_get_bonjour(struct wpa_supplicant
*wpa_s
,
1393 const struct wpabuf
*query
)
1395 struct p2p_srv_bonjour
*bsrv
;
1398 len
= wpabuf_len(query
);
1399 dl_list_for_each(bsrv
, &wpa_s
->global
->p2p_srv_bonjour
,
1400 struct p2p_srv_bonjour
, list
) {
1401 if (len
== wpabuf_len(bsrv
->query
) &&
1402 os_memcmp(wpabuf_head(query
), wpabuf_head(bsrv
->query
),
1410 static struct p2p_srv_upnp
*
1411 wpas_p2p_service_get_upnp(struct wpa_supplicant
*wpa_s
, u8 version
,
1412 const char *service
)
1414 struct p2p_srv_upnp
*usrv
;
1416 dl_list_for_each(usrv
, &wpa_s
->global
->p2p_srv_upnp
,
1417 struct p2p_srv_upnp
, list
) {
1418 if (version
== usrv
->version
&&
1419 os_strcmp(service
, usrv
->service
) == 0)
1426 static void wpas_sd_add_proto_not_avail(struct wpabuf
*resp
, u8 srv_proto
,
1431 if (wpabuf_tailroom(resp
) < 5)
1434 /* Length (to be filled) */
1435 len_pos
= wpabuf_put(resp
, 2);
1436 wpabuf_put_u8(resp
, srv_proto
);
1437 wpabuf_put_u8(resp
, srv_trans_id
);
1439 wpabuf_put_u8(resp
, P2P_SD_PROTO_NOT_AVAILABLE
);
1440 /* Response Data: empty */
1441 WPA_PUT_LE16(len_pos
, (u8
*) wpabuf_put(resp
, 0) - len_pos
- 2);
1445 static void wpas_sd_all_bonjour(struct wpa_supplicant
*wpa_s
,
1446 struct wpabuf
*resp
, u8 srv_trans_id
)
1448 struct p2p_srv_bonjour
*bsrv
;
1451 wpa_printf(MSG_DEBUG
, "P2P: SD Request for all Bonjour services");
1453 if (dl_list_empty(&wpa_s
->global
->p2p_srv_bonjour
)) {
1454 wpa_printf(MSG_DEBUG
, "P2P: Bonjour protocol not available");
1458 dl_list_for_each(bsrv
, &wpa_s
->global
->p2p_srv_bonjour
,
1459 struct p2p_srv_bonjour
, list
) {
1460 if (wpabuf_tailroom(resp
) <
1461 5 + wpabuf_len(bsrv
->query
) + wpabuf_len(bsrv
->resp
))
1463 /* Length (to be filled) */
1464 len_pos
= wpabuf_put(resp
, 2);
1465 wpabuf_put_u8(resp
, P2P_SERV_BONJOUR
);
1466 wpabuf_put_u8(resp
, srv_trans_id
);
1468 wpabuf_put_u8(resp
, P2P_SD_SUCCESS
);
1469 wpa_hexdump_ascii(MSG_DEBUG
, "P2P: Matching Bonjour service",
1470 wpabuf_head(bsrv
->resp
),
1471 wpabuf_len(bsrv
->resp
));
1473 wpabuf_put_buf(resp
, bsrv
->query
); /* Key */
1474 wpabuf_put_buf(resp
, bsrv
->resp
); /* Value */
1475 WPA_PUT_LE16(len_pos
, (u8
*) wpabuf_put(resp
, 0) - len_pos
-
1481 static int match_bonjour_query(struct p2p_srv_bonjour
*bsrv
, const u8
*query
,
1484 char str_rx
[256], str_srv
[256];
1486 if (query_len
< 3 || wpabuf_len(bsrv
->query
) < 3)
1487 return 0; /* Too short to include DNS Type and Version */
1488 if (os_memcmp(query
+ query_len
- 3,
1489 wpabuf_head_u8(bsrv
->query
) + wpabuf_len(bsrv
->query
) - 3,
1491 return 0; /* Mismatch in DNS Type or Version */
1492 if (query_len
== wpabuf_len(bsrv
->query
) &&
1493 os_memcmp(query
, wpabuf_head(bsrv
->query
), query_len
- 3) == 0)
1494 return 1; /* Binary match */
1496 if (p2p_sd_dns_uncompress(str_rx
, sizeof(str_rx
), query
, query_len
- 3,
1498 return 0; /* Failed to uncompress query */
1499 if (p2p_sd_dns_uncompress(str_srv
, sizeof(str_srv
),
1500 wpabuf_head(bsrv
->query
),
1501 wpabuf_len(bsrv
->query
) - 3, 0))
1502 return 0; /* Failed to uncompress service */
1504 return os_strcmp(str_rx
, str_srv
) == 0;
1508 static void wpas_sd_req_bonjour(struct wpa_supplicant
*wpa_s
,
1509 struct wpabuf
*resp
, u8 srv_trans_id
,
1510 const u8
*query
, size_t query_len
)
1512 struct p2p_srv_bonjour
*bsrv
;
1516 wpa_hexdump_ascii(MSG_DEBUG
, "P2P: SD Request for Bonjour",
1518 if (dl_list_empty(&wpa_s
->global
->p2p_srv_bonjour
)) {
1519 wpa_printf(MSG_DEBUG
, "P2P: Bonjour protocol not available");
1520 wpas_sd_add_proto_not_avail(resp
, P2P_SERV_BONJOUR
,
1525 if (query_len
== 0) {
1526 wpas_sd_all_bonjour(wpa_s
, resp
, srv_trans_id
);
1530 dl_list_for_each(bsrv
, &wpa_s
->global
->p2p_srv_bonjour
,
1531 struct p2p_srv_bonjour
, list
) {
1532 if (!match_bonjour_query(bsrv
, query
, query_len
))
1535 if (wpabuf_tailroom(resp
) <
1536 5 + query_len
+ wpabuf_len(bsrv
->resp
))
1541 /* Length (to be filled) */
1542 len_pos
= wpabuf_put(resp
, 2);
1543 wpabuf_put_u8(resp
, P2P_SERV_BONJOUR
);
1544 wpabuf_put_u8(resp
, srv_trans_id
);
1547 wpabuf_put_u8(resp
, P2P_SD_SUCCESS
);
1548 wpa_hexdump_ascii(MSG_DEBUG
, "P2P: Matching Bonjour service",
1549 wpabuf_head(bsrv
->resp
),
1550 wpabuf_len(bsrv
->resp
));
1553 wpabuf_put_data(resp
, query
, query_len
); /* Key */
1554 wpabuf_put_buf(resp
, bsrv
->resp
); /* Value */
1556 WPA_PUT_LE16(len_pos
, (u8
*) wpabuf_put(resp
, 0) - len_pos
- 2);
1560 wpa_printf(MSG_DEBUG
, "P2P: Requested Bonjour service not "
1562 if (wpabuf_tailroom(resp
) < 5)
1565 /* Length (to be filled) */
1566 len_pos
= wpabuf_put(resp
, 2);
1567 wpabuf_put_u8(resp
, P2P_SERV_BONJOUR
);
1568 wpabuf_put_u8(resp
, srv_trans_id
);
1571 wpabuf_put_u8(resp
, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE
);
1572 /* Response Data: empty */
1573 WPA_PUT_LE16(len_pos
, (u8
*) wpabuf_put(resp
, 0) - len_pos
-
1579 static void wpas_sd_all_upnp(struct wpa_supplicant
*wpa_s
,
1580 struct wpabuf
*resp
, u8 srv_trans_id
)
1582 struct p2p_srv_upnp
*usrv
;
1585 wpa_printf(MSG_DEBUG
, "P2P: SD Request for all UPnP services");
1587 if (dl_list_empty(&wpa_s
->global
->p2p_srv_upnp
)) {
1588 wpa_printf(MSG_DEBUG
, "P2P: UPnP protocol not available");
1592 dl_list_for_each(usrv
, &wpa_s
->global
->p2p_srv_upnp
,
1593 struct p2p_srv_upnp
, list
) {
1594 if (wpabuf_tailroom(resp
) < 5 + 1 + os_strlen(usrv
->service
))
1597 /* Length (to be filled) */
1598 len_pos
= wpabuf_put(resp
, 2);
1599 wpabuf_put_u8(resp
, P2P_SERV_UPNP
);
1600 wpabuf_put_u8(resp
, srv_trans_id
);
1603 wpabuf_put_u8(resp
, P2P_SD_SUCCESS
);
1605 wpabuf_put_u8(resp
, usrv
->version
);
1606 wpa_printf(MSG_DEBUG
, "P2P: Matching UPnP Service: %s",
1608 wpabuf_put_str(resp
, usrv
->service
);
1609 WPA_PUT_LE16(len_pos
, (u8
*) wpabuf_put(resp
, 0) - len_pos
-
1615 static void wpas_sd_req_upnp(struct wpa_supplicant
*wpa_s
,
1616 struct wpabuf
*resp
, u8 srv_trans_id
,
1617 const u8
*query
, size_t query_len
)
1619 struct p2p_srv_upnp
*usrv
;
1625 wpa_hexdump_ascii(MSG_DEBUG
, "P2P: SD Request for UPnP",
1628 if (dl_list_empty(&wpa_s
->global
->p2p_srv_upnp
)) {
1629 wpa_printf(MSG_DEBUG
, "P2P: UPnP protocol not available");
1630 wpas_sd_add_proto_not_avail(resp
, P2P_SERV_UPNP
,
1635 if (query_len
== 0) {
1636 wpas_sd_all_upnp(wpa_s
, resp
, srv_trans_id
);
1640 if (wpabuf_tailroom(resp
) < 5)
1643 /* Length (to be filled) */
1644 len_pos
= wpabuf_put(resp
, 2);
1645 wpabuf_put_u8(resp
, P2P_SERV_UPNP
);
1646 wpabuf_put_u8(resp
, srv_trans_id
);
1649 str
= os_malloc(query_len
);
1652 os_memcpy(str
, query
+ 1, query_len
- 1);
1653 str
[query_len
- 1] = '\0';
1655 dl_list_for_each(usrv
, &wpa_s
->global
->p2p_srv_upnp
,
1656 struct p2p_srv_upnp
, list
) {
1657 if (version
!= usrv
->version
)
1660 if (os_strcmp(str
, "ssdp:all") != 0 &&
1661 os_strstr(usrv
->service
, str
) == NULL
)
1664 if (wpabuf_tailroom(resp
) < 2)
1668 wpabuf_put_u8(resp
, P2P_SD_SUCCESS
);
1670 wpabuf_put_u8(resp
, version
);
1672 wpabuf_put_u8(resp
, ',');
1676 wpa_printf(MSG_DEBUG
, "P2P: Matching UPnP Service: %s",
1678 if (wpabuf_tailroom(resp
) < os_strlen(usrv
->service
))
1680 wpabuf_put_str(resp
, usrv
->service
);
1685 wpa_printf(MSG_DEBUG
, "P2P: Requested UPnP service not "
1688 wpabuf_put_u8(resp
, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE
);
1689 /* Response Data: empty */
1692 WPA_PUT_LE16(len_pos
, (u8
*) wpabuf_put(resp
, 0) - len_pos
- 2);
1696 #ifdef CONFIG_WIFI_DISPLAY
1697 static void wpas_sd_req_wfd(struct wpa_supplicant
*wpa_s
,
1698 struct wpabuf
*resp
, u8 srv_trans_id
,
1699 const u8
*query
, size_t query_len
)
1705 wpa_hexdump(MSG_DEBUG
, "P2P: SD Request for WFD", query
, query_len
);
1707 if (!wpa_s
->global
->wifi_display
) {
1708 wpa_printf(MSG_DEBUG
, "P2P: WFD protocol not available");
1709 wpas_sd_add_proto_not_avail(resp
, P2P_SERV_WIFI_DISPLAY
,
1714 if (query_len
< 1) {
1715 wpa_printf(MSG_DEBUG
, "P2P: Missing WFD Requested Device "
1720 if (wpabuf_tailroom(resp
) < 5)
1725 wpa_printf(MSG_DEBUG
, "P2P: WSD for device role 0x%x", role
);
1727 /* TODO: role specific handling */
1729 /* Length (to be filled) */
1730 len_pos
= wpabuf_put(resp
, 2);
1731 wpabuf_put_u8(resp
, P2P_SERV_WIFI_DISPLAY
);
1732 wpabuf_put_u8(resp
, srv_trans_id
);
1733 wpabuf_put_u8(resp
, P2P_SD_SUCCESS
); /* Status Code */
1735 while (pos
< query
+ query_len
) {
1736 if (*pos
< MAX_WFD_SUBELEMS
&&
1737 wpa_s
->global
->wfd_subelem
[*pos
] &&
1738 wpabuf_tailroom(resp
) >=
1739 wpabuf_len(wpa_s
->global
->wfd_subelem
[*pos
])) {
1740 wpa_printf(MSG_DEBUG
, "P2P: Add WSD response "
1741 "subelement %u", *pos
);
1742 wpabuf_put_buf(resp
, wpa_s
->global
->wfd_subelem
[*pos
]);
1747 WPA_PUT_LE16(len_pos
, (u8
*) wpabuf_put(resp
, 0) - len_pos
- 2);
1749 #endif /* CONFIG_WIFI_DISPLAY */
1752 void wpas_sd_request(void *ctx
, int freq
, const u8
*sa
, u8 dialog_token
,
1753 u16 update_indic
, const u8
*tlvs
, size_t tlvs_len
)
1755 struct wpa_supplicant
*wpa_s
= ctx
;
1756 const u8
*pos
= tlvs
;
1757 const u8
*end
= tlvs
+ tlvs_len
;
1760 struct wpabuf
*resp
;
1761 u8 srv_proto
, srv_trans_id
;
1765 wpa_hexdump(MSG_MSGDUMP
, "P2P: Service Discovery Request TLVs",
1767 buf_len
= 2 * tlvs_len
+ 1;
1768 buf
= os_malloc(buf_len
);
1770 wpa_snprintf_hex(buf
, buf_len
, tlvs
, tlvs_len
);
1771 wpa_msg_ctrl(wpa_s
, MSG_INFO
, P2P_EVENT_SERV_DISC_REQ
"%d "
1773 freq
, MAC2STR(sa
), dialog_token
, update_indic
,
1778 if (wpa_s
->p2p_sd_over_ctrl_iface
) {
1779 wpas_notify_p2p_sd_request(wpa_s
, freq
, sa
, dialog_token
,
1780 update_indic
, tlvs
, tlvs_len
);
1781 return; /* to be processed by an external program */
1784 resp
= wpabuf_alloc(10000);
1788 while (pos
+ 1 < end
) {
1789 wpa_printf(MSG_DEBUG
, "P2P: Service Request TLV");
1790 slen
= WPA_GET_LE16(pos
);
1792 if (pos
+ slen
> end
|| slen
< 2) {
1793 wpa_printf(MSG_DEBUG
, "P2P: Unexpected Query Data "
1798 tlv_end
= pos
+ slen
;
1801 wpa_printf(MSG_DEBUG
, "P2P: Service Protocol Type %u",
1803 srv_trans_id
= *pos
++;
1804 wpa_printf(MSG_DEBUG
, "P2P: Service Transaction ID %u",
1807 wpa_hexdump(MSG_MSGDUMP
, "P2P: Query Data",
1808 pos
, tlv_end
- pos
);
1811 if (wpa_s
->force_long_sd
) {
1812 wpa_printf(MSG_DEBUG
, "P2P: SD test - force long "
1814 wpas_sd_all_bonjour(wpa_s
, resp
, srv_trans_id
);
1815 wpas_sd_all_upnp(wpa_s
, resp
, srv_trans_id
);
1819 switch (srv_proto
) {
1820 case P2P_SERV_ALL_SERVICES
:
1821 wpa_printf(MSG_DEBUG
, "P2P: Service Discovery Request "
1822 "for all services");
1823 if (dl_list_empty(&wpa_s
->global
->p2p_srv_upnp
) &&
1824 dl_list_empty(&wpa_s
->global
->p2p_srv_bonjour
)) {
1825 wpa_printf(MSG_DEBUG
, "P2P: No service "
1826 "discovery protocols available");
1827 wpas_sd_add_proto_not_avail(
1828 resp
, P2P_SERV_ALL_SERVICES
,
1832 wpas_sd_all_bonjour(wpa_s
, resp
, srv_trans_id
);
1833 wpas_sd_all_upnp(wpa_s
, resp
, srv_trans_id
);
1835 case P2P_SERV_BONJOUR
:
1836 wpas_sd_req_bonjour(wpa_s
, resp
, srv_trans_id
,
1837 pos
, tlv_end
- pos
);
1840 wpas_sd_req_upnp(wpa_s
, resp
, srv_trans_id
,
1841 pos
, tlv_end
- pos
);
1843 #ifdef CONFIG_WIFI_DISPLAY
1844 case P2P_SERV_WIFI_DISPLAY
:
1845 wpas_sd_req_wfd(wpa_s
, resp
, srv_trans_id
,
1846 pos
, tlv_end
- pos
);
1848 #endif /* CONFIG_WIFI_DISPLAY */
1850 wpa_printf(MSG_DEBUG
, "P2P: Unavailable service "
1851 "protocol %u", srv_proto
);
1852 wpas_sd_add_proto_not_avail(resp
, srv_proto
,
1861 wpas_notify_p2p_sd_request(wpa_s
, freq
, sa
, dialog_token
,
1862 update_indic
, tlvs
, tlvs_len
);
1864 wpas_p2p_sd_response(wpa_s
, freq
, sa
, dialog_token
, resp
);
1870 void wpas_sd_response(void *ctx
, const u8
*sa
, u16 update_indic
,
1871 const u8
*tlvs
, size_t tlvs_len
)
1873 struct wpa_supplicant
*wpa_s
= ctx
;
1874 const u8
*pos
= tlvs
;
1875 const u8
*end
= tlvs
+ tlvs_len
;
1881 wpa_hexdump(MSG_MSGDUMP
, "P2P: Service Discovery Response TLVs",
1883 if (tlvs_len
> 1500) {
1884 /* TODO: better way for handling this */
1885 wpa_msg_ctrl(wpa_s
, MSG_INFO
,
1886 P2P_EVENT_SERV_DISC_RESP MACSTR
1887 " %u <long response: %u bytes>",
1888 MAC2STR(sa
), update_indic
,
1889 (unsigned int) tlvs_len
);
1891 buf_len
= 2 * tlvs_len
+ 1;
1892 buf
= os_malloc(buf_len
);
1894 wpa_snprintf_hex(buf
, buf_len
, tlvs
, tlvs_len
);
1895 wpa_msg_ctrl(wpa_s
, MSG_INFO
,
1896 P2P_EVENT_SERV_DISC_RESP MACSTR
" %u %s",
1897 MAC2STR(sa
), update_indic
, buf
);
1903 u8 srv_proto
, srv_trans_id
, status
;
1905 wpa_printf(MSG_DEBUG
, "P2P: Service Response TLV");
1906 slen
= WPA_GET_LE16(pos
);
1908 if (pos
+ slen
> end
|| slen
< 3) {
1909 wpa_printf(MSG_DEBUG
, "P2P: Unexpected Response Data "
1913 tlv_end
= pos
+ slen
;
1916 wpa_printf(MSG_DEBUG
, "P2P: Service Protocol Type %u",
1918 srv_trans_id
= *pos
++;
1919 wpa_printf(MSG_DEBUG
, "P2P: Service Transaction ID %u",
1922 wpa_printf(MSG_DEBUG
, "P2P: Status Code ID %u",
1925 wpa_hexdump(MSG_MSGDUMP
, "P2P: Response Data",
1926 pos
, tlv_end
- pos
);
1931 wpas_notify_p2p_sd_response(wpa_s
, sa
, update_indic
, tlvs
, tlvs_len
);
1935 u64
wpas_p2p_sd_request(struct wpa_supplicant
*wpa_s
, const u8
*dst
,
1936 const struct wpabuf
*tlvs
)
1938 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
1939 return wpa_drv_p2p_sd_request(wpa_s
, dst
, tlvs
);
1940 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
1942 return (uintptr_t) p2p_sd_request(wpa_s
->global
->p2p
, dst
, tlvs
);
1946 u64
wpas_p2p_sd_request_upnp(struct wpa_supplicant
*wpa_s
, const u8
*dst
,
1947 u8 version
, const char *query
)
1949 struct wpabuf
*tlvs
;
1952 tlvs
= wpabuf_alloc(2 + 1 + 1 + 1 + os_strlen(query
));
1955 wpabuf_put_le16(tlvs
, 1 + 1 + 1 + os_strlen(query
));
1956 wpabuf_put_u8(tlvs
, P2P_SERV_UPNP
); /* Service Protocol Type */
1957 wpabuf_put_u8(tlvs
, 1); /* Service Transaction ID */
1958 wpabuf_put_u8(tlvs
, version
);
1959 wpabuf_put_str(tlvs
, query
);
1960 ret
= wpas_p2p_sd_request(wpa_s
, dst
, tlvs
);
1966 #ifdef CONFIG_WIFI_DISPLAY
1968 static u64
wpas_p2p_sd_request_wfd(struct wpa_supplicant
*wpa_s
, const u8
*dst
,
1969 const struct wpabuf
*tlvs
)
1971 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
1973 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
1975 return (uintptr_t) p2p_sd_request_wfd(wpa_s
->global
->p2p
, dst
, tlvs
);
1979 #define MAX_WFD_SD_SUBELEMS 20
1981 static void wfd_add_sd_req_role(struct wpabuf
*tlvs
, u8 id
, u8 role
,
1982 const char *subelems
)
1989 len
= wpabuf_put(tlvs
, 2);
1990 wpabuf_put_u8(tlvs
, P2P_SERV_WIFI_DISPLAY
); /* Service Protocol Type */
1991 wpabuf_put_u8(tlvs
, id
); /* Service Transaction ID */
1993 wpabuf_put_u8(tlvs
, role
);
1998 if (val
>= 0 && val
< 256) {
1999 wpabuf_put_u8(tlvs
, val
);
2001 if (count
== MAX_WFD_SD_SUBELEMS
)
2004 pos
= os_strchr(pos
+ 1, ',');
2010 WPA_PUT_LE16(len
, (u8
*) wpabuf_put(tlvs
, 0) - len
- 2);
2014 u64
wpas_p2p_sd_request_wifi_display(struct wpa_supplicant
*wpa_s
,
2015 const u8
*dst
, const char *role
)
2017 struct wpabuf
*tlvs
;
2019 const char *subelems
;
2022 subelems
= os_strchr(role
, ' ');
2023 if (subelems
== NULL
)
2027 tlvs
= wpabuf_alloc(4 * (2 + 1 + 1 + 1 + MAX_WFD_SD_SUBELEMS
));
2031 if (os_strstr(role
, "[source]"))
2032 wfd_add_sd_req_role(tlvs
, id
++, 0x00, subelems
);
2033 if (os_strstr(role
, "[pri-sink]"))
2034 wfd_add_sd_req_role(tlvs
, id
++, 0x01, subelems
);
2035 if (os_strstr(role
, "[sec-sink]"))
2036 wfd_add_sd_req_role(tlvs
, id
++, 0x02, subelems
);
2037 if (os_strstr(role
, "[source+sink]"))
2038 wfd_add_sd_req_role(tlvs
, id
++, 0x03, subelems
);
2040 ret
= wpas_p2p_sd_request_wfd(wpa_s
, dst
, tlvs
);
2045 #endif /* CONFIG_WIFI_DISPLAY */
2048 int wpas_p2p_sd_cancel_request(struct wpa_supplicant
*wpa_s
, u64 req
)
2050 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
2051 return wpa_drv_p2p_sd_cancel_request(wpa_s
, req
);
2052 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
2054 return p2p_sd_cancel_request(wpa_s
->global
->p2p
,
2055 (void *) (uintptr_t) req
);
2059 void wpas_p2p_sd_response(struct wpa_supplicant
*wpa_s
, int freq
,
2060 const u8
*dst
, u8 dialog_token
,
2061 const struct wpabuf
*resp_tlvs
)
2063 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
) {
2064 wpa_drv_p2p_sd_response(wpa_s
, freq
, dst
, dialog_token
,
2068 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
2070 p2p_sd_response(wpa_s
->global
->p2p
, freq
, dst
, dialog_token
,
2075 void wpas_p2p_sd_service_update(struct wpa_supplicant
*wpa_s
)
2077 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
) {
2078 wpa_drv_p2p_service_update(wpa_s
);
2081 if (wpa_s
->global
->p2p
)
2082 p2p_sd_service_update(wpa_s
->global
->p2p
);
2086 static void wpas_p2p_srv_bonjour_free(struct p2p_srv_bonjour
*bsrv
)
2088 dl_list_del(&bsrv
->list
);
2089 wpabuf_free(bsrv
->query
);
2090 wpabuf_free(bsrv
->resp
);
2095 static void wpas_p2p_srv_upnp_free(struct p2p_srv_upnp
*usrv
)
2097 dl_list_del(&usrv
->list
);
2098 os_free(usrv
->service
);
2103 void wpas_p2p_service_flush(struct wpa_supplicant
*wpa_s
)
2105 struct p2p_srv_bonjour
*bsrv
, *bn
;
2106 struct p2p_srv_upnp
*usrv
, *un
;
2108 dl_list_for_each_safe(bsrv
, bn
, &wpa_s
->global
->p2p_srv_bonjour
,
2109 struct p2p_srv_bonjour
, list
)
2110 wpas_p2p_srv_bonjour_free(bsrv
);
2112 dl_list_for_each_safe(usrv
, un
, &wpa_s
->global
->p2p_srv_upnp
,
2113 struct p2p_srv_upnp
, list
)
2114 wpas_p2p_srv_upnp_free(usrv
);
2116 wpas_p2p_sd_service_update(wpa_s
);
2120 int wpas_p2p_service_add_bonjour(struct wpa_supplicant
*wpa_s
,
2121 struct wpabuf
*query
, struct wpabuf
*resp
)
2123 struct p2p_srv_bonjour
*bsrv
;
2125 bsrv
= os_zalloc(sizeof(*bsrv
));
2128 bsrv
->query
= query
;
2130 dl_list_add(&wpa_s
->global
->p2p_srv_bonjour
, &bsrv
->list
);
2132 wpas_p2p_sd_service_update(wpa_s
);
2137 int wpas_p2p_service_del_bonjour(struct wpa_supplicant
*wpa_s
,
2138 const struct wpabuf
*query
)
2140 struct p2p_srv_bonjour
*bsrv
;
2142 bsrv
= wpas_p2p_service_get_bonjour(wpa_s
, query
);
2145 wpas_p2p_srv_bonjour_free(bsrv
);
2146 wpas_p2p_sd_service_update(wpa_s
);
2151 int wpas_p2p_service_add_upnp(struct wpa_supplicant
*wpa_s
, u8 version
,
2152 const char *service
)
2154 struct p2p_srv_upnp
*usrv
;
2156 if (wpas_p2p_service_get_upnp(wpa_s
, version
, service
))
2157 return 0; /* Already listed */
2158 usrv
= os_zalloc(sizeof(*usrv
));
2161 usrv
->version
= version
;
2162 usrv
->service
= os_strdup(service
);
2163 if (usrv
->service
== NULL
) {
2167 dl_list_add(&wpa_s
->global
->p2p_srv_upnp
, &usrv
->list
);
2169 wpas_p2p_sd_service_update(wpa_s
);
2174 int wpas_p2p_service_del_upnp(struct wpa_supplicant
*wpa_s
, u8 version
,
2175 const char *service
)
2177 struct p2p_srv_upnp
*usrv
;
2179 usrv
= wpas_p2p_service_get_upnp(wpa_s
, version
, service
);
2182 wpas_p2p_srv_upnp_free(usrv
);
2183 wpas_p2p_sd_service_update(wpa_s
);
2188 static void wpas_prov_disc_local_display(struct wpa_supplicant
*wpa_s
,
2189 const u8
*peer
, const char *params
,
2190 unsigned int generated_pin
)
2192 wpa_msg(wpa_s
, MSG_INFO
, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR
" %08d%s",
2193 MAC2STR(peer
), generated_pin
, params
);
2197 static void wpas_prov_disc_local_keypad(struct wpa_supplicant
*wpa_s
,
2198 const u8
*peer
, const char *params
)
2200 wpa_msg(wpa_s
, MSG_INFO
, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR
"%s",
2201 MAC2STR(peer
), params
);
2205 void wpas_prov_disc_req(void *ctx
, const u8
*peer
, u16 config_methods
,
2206 const u8
*dev_addr
, const u8
*pri_dev_type
,
2207 const char *dev_name
, u16 supp_config_methods
,
2208 u8 dev_capab
, u8 group_capab
, const u8
*group_id
,
2209 size_t group_id_len
)
2211 struct wpa_supplicant
*wpa_s
= ctx
;
2212 char devtype
[WPS_DEV_TYPE_BUFSIZE
];
2214 u8 empty_dev_type
[8];
2215 unsigned int generated_pin
= 0;
2216 struct wpa_supplicant
*group
= NULL
;
2219 for (group
= wpa_s
->global
->ifaces
; group
; group
= group
->next
)
2221 struct wpa_ssid
*s
= group
->current_ssid
;
2223 s
->mode
== WPAS_MODE_P2P_GO
&&
2224 group_id_len
- ETH_ALEN
== s
->ssid_len
&&
2225 os_memcmp(group_id
+ ETH_ALEN
, s
->ssid
,
2231 if (pri_dev_type
== NULL
) {
2232 os_memset(empty_dev_type
, 0, sizeof(empty_dev_type
));
2233 pri_dev_type
= empty_dev_type
;
2235 os_snprintf(params
, sizeof(params
), " p2p_dev_addr=" MACSTR
2236 " pri_dev_type=%s name='%s' config_methods=0x%x "
2237 "dev_capab=0x%x group_capab=0x%x%s%s",
2239 wps_dev_type_bin2str(pri_dev_type
, devtype
,
2241 dev_name
, supp_config_methods
, dev_capab
, group_capab
,
2242 group
? " group=" : "",
2243 group
? group
->ifname
: "");
2244 params
[sizeof(params
) - 1] = '\0';
2246 if (config_methods
& WPS_CONFIG_DISPLAY
) {
2247 generated_pin
= wps_generate_pin();
2248 wpas_prov_disc_local_display(wpa_s
, peer
, params
,
2250 } else if (config_methods
& WPS_CONFIG_KEYPAD
)
2251 wpas_prov_disc_local_keypad(wpa_s
, peer
, params
);
2252 else if (config_methods
& WPS_CONFIG_PUSHBUTTON
)
2253 wpa_msg(wpa_s
, MSG_INFO
, P2P_EVENT_PROV_DISC_PBC_REQ MACSTR
2254 "%s", MAC2STR(peer
), params
);
2256 wpas_notify_p2p_provision_discovery(wpa_s
, peer
, 1 /* request */,
2257 P2P_PROV_DISC_SUCCESS
,
2258 config_methods
, generated_pin
);
2262 void wpas_prov_disc_resp(void *ctx
, const u8
*peer
, u16 config_methods
)
2264 struct wpa_supplicant
*wpa_s
= ctx
;
2265 unsigned int generated_pin
= 0;
2268 if (wpa_s
->pending_pd_before_join
&&
2269 (os_memcmp(peer
, wpa_s
->pending_join_dev_addr
, ETH_ALEN
) == 0 ||
2270 os_memcmp(peer
, wpa_s
->pending_join_iface_addr
, ETH_ALEN
) == 0)) {
2271 wpa_s
->pending_pd_before_join
= 0;
2272 wpa_printf(MSG_DEBUG
, "P2P: Starting pending "
2273 "join-existing-group operation");
2274 wpas_p2p_join_start(wpa_s
);
2278 if (wpa_s
->pending_pd_use
== AUTO_PD_JOIN
||
2279 wpa_s
->pending_pd_use
== AUTO_PD_GO_NEG
)
2280 os_snprintf(params
, sizeof(params
), " peer_go=%d",
2281 wpa_s
->pending_pd_use
== AUTO_PD_JOIN
);
2285 if (config_methods
& WPS_CONFIG_DISPLAY
)
2286 wpas_prov_disc_local_keypad(wpa_s
, peer
, params
);
2287 else if (config_methods
& WPS_CONFIG_KEYPAD
) {
2288 generated_pin
= wps_generate_pin();
2289 wpas_prov_disc_local_display(wpa_s
, peer
, params
,
2291 } else if (config_methods
& WPS_CONFIG_PUSHBUTTON
)
2292 wpa_msg(wpa_s
, MSG_INFO
, P2P_EVENT_PROV_DISC_PBC_RESP MACSTR
2293 "%s", MAC2STR(peer
), params
);
2295 wpas_notify_p2p_provision_discovery(wpa_s
, peer
, 0 /* response */,
2296 P2P_PROV_DISC_SUCCESS
,
2297 config_methods
, generated_pin
);
2301 static void wpas_prov_disc_fail(void *ctx
, const u8
*peer
,
2302 enum p2p_prov_disc_status status
)
2304 struct wpa_supplicant
*wpa_s
= ctx
;
2306 if (wpa_s
->p2p_fallback_to_go_neg
) {
2307 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: PD for p2p_connect-auto "
2308 "failed - fall back to GO Negotiation");
2309 wpas_p2p_fallback_to_go_neg(wpa_s
, 0);
2313 if (status
== P2P_PROV_DISC_TIMEOUT_JOIN
) {
2314 wpa_s
->pending_pd_before_join
= 0;
2315 wpa_printf(MSG_DEBUG
, "P2P: Starting pending "
2316 "join-existing-group operation (no ACK for PD "
2318 wpas_p2p_join_start(wpa_s
);
2322 wpa_msg(wpa_s
, MSG_INFO
, P2P_EVENT_PROV_DISC_FAILURE
2323 " p2p_dev_addr=" MACSTR
" status=%d",
2324 MAC2STR(peer
), status
);
2326 wpas_notify_p2p_provision_discovery(wpa_s
, peer
, 0 /* response */,
2331 static u8
wpas_invitation_process(void *ctx
, const u8
*sa
, const u8
*bssid
,
2332 const u8
*go_dev_addr
, const u8
*ssid
,
2333 size_t ssid_len
, int *go
, u8
*group_bssid
,
2334 int *force_freq
, int persistent_group
)
2336 struct wpa_supplicant
*wpa_s
= ctx
;
2338 u8 cur_bssid
[ETH_ALEN
];
2340 struct wpa_supplicant
*grp
;
2342 if (!persistent_group
) {
2343 wpa_printf(MSG_DEBUG
, "P2P: Invitation from " MACSTR
2344 " to join an active group", MAC2STR(sa
));
2345 if (!is_zero_ether_addr(wpa_s
->p2p_auth_invite
) &&
2346 (os_memcmp(go_dev_addr
, wpa_s
->p2p_auth_invite
, ETH_ALEN
)
2348 os_memcmp(sa
, wpa_s
->p2p_auth_invite
, ETH_ALEN
) == 0)) {
2349 wpa_printf(MSG_DEBUG
, "P2P: Accept previously "
2350 "authorized invitation");
2354 * Do not accept the invitation automatically; notify user and
2357 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE
;
2360 grp
= wpas_get_p2p_group(wpa_s
, ssid
, ssid_len
, go
);
2362 wpa_printf(MSG_DEBUG
, "P2P: Accept invitation to already "
2363 "running persistent group");
2365 os_memcpy(group_bssid
, grp
->own_addr
, ETH_ALEN
);
2369 if (!is_zero_ether_addr(wpa_s
->p2p_auth_invite
) &&
2370 os_memcmp(sa
, wpa_s
->p2p_auth_invite
, ETH_ALEN
) == 0) {
2371 wpa_printf(MSG_DEBUG
, "P2P: Accept previously initiated "
2372 "invitation to re-invoke a persistent group");
2373 } else if (!wpa_s
->conf
->persistent_reconnect
)
2374 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE
;
2376 for (s
= wpa_s
->conf
->ssid
; s
; s
= s
->next
) {
2377 if (s
->disabled
== 2 &&
2378 os_memcmp(s
->bssid
, go_dev_addr
, ETH_ALEN
) == 0 &&
2379 s
->ssid_len
== ssid_len
&&
2380 os_memcmp(ssid
, s
->ssid
, ssid_len
) == 0)
2385 wpa_printf(MSG_DEBUG
, "P2P: Invitation from " MACSTR
2386 " requested reinvocation of an unknown group",
2388 return P2P_SC_FAIL_UNKNOWN_GROUP
;
2391 if (s
->mode
== WPAS_MODE_P2P_GO
&& !wpas_p2p_create_iface(wpa_s
)) {
2393 if (wpa_s
->wpa_state
>= WPA_AUTHENTICATING
) {
2394 wpa_printf(MSG_DEBUG
, "P2P: The only available "
2395 "interface is already in use - reject "
2397 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE
;
2399 os_memcpy(group_bssid
, wpa_s
->own_addr
, ETH_ALEN
);
2400 } else if (s
->mode
== WPAS_MODE_P2P_GO
) {
2402 if (wpas_p2p_add_group_interface(wpa_s
, WPA_IF_P2P_GO
) < 0)
2404 wpa_printf(MSG_ERROR
, "P2P: Failed to allocate a new "
2405 "interface address for the group");
2406 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE
;
2408 os_memcpy(group_bssid
, wpa_s
->pending_interface_addr
,
2413 if (wpa_s
->current_ssid
&& wpa_drv_get_bssid(wpa_s
, cur_bssid
) == 0 &&
2414 wpa_s
->assoc_freq
) {
2415 wpa_printf(MSG_DEBUG
, "P2P: Trying to force channel to match "
2416 "the channel we are already using");
2417 *force_freq
= wpa_s
->assoc_freq
;
2420 res
= wpa_drv_shared_freq(wpa_s
);
2422 wpa_printf(MSG_DEBUG
, "P2P: Trying to force channel to match "
2423 "with the channel we are already using on a "
2424 "shared interface");
2428 return P2P_SC_SUCCESS
;
2432 static void wpas_invitation_received(void *ctx
, const u8
*sa
, const u8
*bssid
,
2433 const u8
*ssid
, size_t ssid_len
,
2434 const u8
*go_dev_addr
, u8 status
,
2437 struct wpa_supplicant
*wpa_s
= ctx
;
2440 for (s
= wpa_s
->conf
->ssid
; s
; s
= s
->next
) {
2441 if (s
->disabled
== 2 &&
2442 s
->ssid_len
== ssid_len
&&
2443 os_memcmp(ssid
, s
->ssid
, ssid_len
) == 0)
2447 if (status
== P2P_SC_SUCCESS
) {
2448 wpa_printf(MSG_DEBUG
, "P2P: Invitation from peer " MACSTR
2449 " was accepted; op_freq=%d MHz",
2450 MAC2STR(sa
), op_freq
);
2452 int go
= s
->mode
== WPAS_MODE_P2P_GO
;
2453 wpas_p2p_group_add_persistent(
2454 wpa_s
, s
, go
, go
? op_freq
: 0, 0, NULL
);
2456 wpa_s
->user_initiated_pd
= 0;
2457 wpas_p2p_join(wpa_s
, bssid
, go_dev_addr
,
2458 wpa_s
->p2p_wps_method
, 0);
2463 if (status
!= P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE
) {
2464 wpa_printf(MSG_DEBUG
, "P2P: Invitation from peer " MACSTR
2465 " was rejected (status %u)", MAC2STR(sa
), status
);
2471 wpa_msg(wpa_s
, MSG_INFO
, P2P_EVENT_INVITATION_RECEIVED
2472 "sa=" MACSTR
" go_dev_addr=" MACSTR
2473 " bssid=" MACSTR
" unknown-network",
2474 MAC2STR(sa
), MAC2STR(go_dev_addr
),
2477 wpa_msg(wpa_s
, MSG_INFO
, P2P_EVENT_INVITATION_RECEIVED
2478 "sa=" MACSTR
" go_dev_addr=" MACSTR
2480 MAC2STR(sa
), MAC2STR(go_dev_addr
));
2485 if (s
->mode
== WPAS_MODE_P2P_GO
&& op_freq
) {
2486 wpa_msg(wpa_s
, MSG_INFO
, P2P_EVENT_INVITATION_RECEIVED
"sa="
2487 MACSTR
" persistent=%d freq=%d",
2488 MAC2STR(sa
), s
->id
, op_freq
);
2490 wpa_msg(wpa_s
, MSG_INFO
, P2P_EVENT_INVITATION_RECEIVED
"sa="
2491 MACSTR
" persistent=%d", MAC2STR(sa
), s
->id
);
2496 static void wpas_invitation_result(void *ctx
, int status
, const u8
*bssid
,
2497 const struct p2p_channels
*channels
)
2499 struct wpa_supplicant
*wpa_s
= ctx
;
2500 struct wpa_ssid
*ssid
;
2503 wpa_msg(wpa_s
, MSG_INFO
, P2P_EVENT_INVITATION_RESULT
2504 "status=%d " MACSTR
,
2505 status
, MAC2STR(bssid
));
2507 wpa_msg(wpa_s
, MSG_INFO
, P2P_EVENT_INVITATION_RESULT
2508 "status=%d ", status
);
2510 wpas_notify_p2p_invitation_result(wpa_s
, status
, bssid
);
2512 if (wpa_s
->pending_invite_ssid_id
== -1)
2513 return; /* Invitation to active group */
2515 if (status
== P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE
) {
2516 wpa_printf(MSG_DEBUG
, "P2P: Waiting for peer to start another "
2517 "invitation exchange to indicate readiness for "
2521 if (status
!= P2P_SC_SUCCESS
) {
2522 wpas_p2p_remove_pending_group_interface(wpa_s
);
2526 ssid
= wpa_config_get_network(wpa_s
->conf
,
2527 wpa_s
->pending_invite_ssid_id
);
2529 wpa_printf(MSG_ERROR
, "P2P: Could not find persistent group "
2530 "data matching with invitation");
2535 * The peer could have missed our ctrl::ack frame for Invitation
2536 * Response and continue retransmitting the frame. To reduce the
2537 * likelihood of the peer not getting successful TX status for the
2538 * Invitation Response frame, wait a short time here before starting
2539 * the persistent group so that we will remain on the current channel to
2540 * acknowledge any possible retransmission from the peer.
2542 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: 50 ms wait on current channel before "
2543 "starting persistent group");
2546 wpas_p2p_group_add_persistent(wpa_s
, ssid
,
2547 ssid
->mode
== WPAS_MODE_P2P_GO
,
2548 wpa_s
->p2p_persistent_go_freq
,
2549 wpa_s
->p2p_go_ht40
, channels
);
2553 static int wpas_p2p_disallowed_freq(struct wpa_global
*global
,
2558 if (global
->p2p_disallow_freq
== NULL
)
2561 for (i
= 0; i
< global
->num_p2p_disallow_freq
; i
++) {
2562 if (freq
>= global
->p2p_disallow_freq
[i
].min
&&
2563 freq
<= global
->p2p_disallow_freq
[i
].max
)
2571 static void wpas_p2p_add_chan(struct p2p_reg_class
*reg
, u8 chan
)
2573 reg
->channel
[reg
->channels
] = chan
;
2578 static int wpas_p2p_default_channels(struct wpa_supplicant
*wpa_s
,
2579 struct p2p_channels
*chan
)
2583 wpa_printf(MSG_DEBUG
, "P2P: Enable operating classes for 2.4 GHz "
2586 /* Operating class 81 - 2.4 GHz band channels 1..13 */
2587 chan
->reg_class
[cla
].reg_class
= 81;
2588 chan
->reg_class
[cla
].channels
= 0;
2589 for (i
= 0; i
< 11; i
++) {
2590 if (!wpas_p2p_disallowed_freq(wpa_s
->global
, 2412 + i
* 5))
2591 wpas_p2p_add_chan(&chan
->reg_class
[cla
], i
+ 1);
2593 if (chan
->reg_class
[cla
].channels
)
2596 wpa_printf(MSG_DEBUG
, "P2P: Enable operating classes for lower 5 GHz "
2599 /* Operating class 115 - 5 GHz, channels 36-48 */
2600 chan
->reg_class
[cla
].reg_class
= 115;
2601 chan
->reg_class
[cla
].channels
= 0;
2602 if (!wpas_p2p_disallowed_freq(wpa_s
->global
, 5000 + 36 * 5))
2603 wpas_p2p_add_chan(&chan
->reg_class
[cla
], 36);
2604 if (!wpas_p2p_disallowed_freq(wpa_s
->global
, 5000 + 40 * 5))
2605 wpas_p2p_add_chan(&chan
->reg_class
[cla
], 40);
2606 if (!wpas_p2p_disallowed_freq(wpa_s
->global
, 5000 + 44 * 5))
2607 wpas_p2p_add_chan(&chan
->reg_class
[cla
], 44);
2608 if (!wpas_p2p_disallowed_freq(wpa_s
->global
, 5000 + 48 * 5))
2609 wpas_p2p_add_chan(&chan
->reg_class
[cla
], 48);
2610 if (chan
->reg_class
[cla
].channels
)
2613 wpa_printf(MSG_DEBUG
, "P2P: Enable operating classes for higher 5 GHz "
2616 /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
2617 chan
->reg_class
[cla
].reg_class
= 124;
2618 chan
->reg_class
[cla
].channels
= 0;
2619 if (!wpas_p2p_disallowed_freq(wpa_s
->global
, 5000 + 149 * 5))
2620 wpas_p2p_add_chan(&chan
->reg_class
[cla
], 149);
2621 if (!wpas_p2p_disallowed_freq(wpa_s
->global
, 5000 + 153 * 5))
2622 wpas_p2p_add_chan(&chan
->reg_class
[cla
], 153);
2623 if (!wpas_p2p_disallowed_freq(wpa_s
->global
, 5000 + 156 * 5))
2624 wpas_p2p_add_chan(&chan
->reg_class
[cla
], 157);
2625 if (!wpas_p2p_disallowed_freq(wpa_s
->global
, 5000 + 161 * 5))
2626 wpas_p2p_add_chan(&chan
->reg_class
[cla
], 161);
2627 if (chan
->reg_class
[cla
].channels
)
2630 chan
->reg_classes
= cla
;
2635 static struct hostapd_hw_modes
* get_mode(struct hostapd_hw_modes
*modes
,
2637 enum hostapd_hw_mode mode
)
2641 for (i
= 0; i
< num_modes
; i
++) {
2642 if (modes
[i
].mode
== mode
)
2650 static int has_channel(struct wpa_global
*global
,
2651 struct hostapd_hw_modes
*mode
, u8 chan
, int *flags
)
2656 freq
= (mode
->mode
== HOSTAPD_MODE_IEEE80211A
? 5000 : 2407) +
2658 if (wpas_p2p_disallowed_freq(global
, freq
))
2661 for (i
= 0; i
< mode
->num_channels
; i
++) {
2662 if (mode
->channels
[i
].chan
== chan
) {
2664 *flags
= mode
->channels
[i
].flag
;
2665 return !(mode
->channels
[i
].flag
&
2666 (HOSTAPD_CHAN_DISABLED
|
2667 HOSTAPD_CHAN_PASSIVE_SCAN
|
2668 HOSTAPD_CHAN_NO_IBSS
|
2669 HOSTAPD_CHAN_RADAR
));
2677 struct p2p_oper_class_map
{
2678 enum hostapd_hw_mode mode
;
2683 enum { BW20
, BW40PLUS
, BW40MINUS
} bw
;
2686 static struct p2p_oper_class_map op_class
[] = {
2687 { HOSTAPD_MODE_IEEE80211G
, 81, 1, 13, 1, BW20
},
2688 #if 0 /* Do not enable HT40 on 2 GHz for now */
2689 { HOSTAPD_MODE_IEEE80211G
, 83, 1, 9, 1, BW40PLUS
},
2690 { HOSTAPD_MODE_IEEE80211G
, 84, 5, 13, 1, BW40MINUS
},
2692 { HOSTAPD_MODE_IEEE80211A
, 115, 36, 48, 4, BW20
},
2693 { HOSTAPD_MODE_IEEE80211A
, 124, 149, 161, 4, BW20
},
2694 { HOSTAPD_MODE_IEEE80211A
, 116, 36, 44, 8, BW40PLUS
},
2695 { HOSTAPD_MODE_IEEE80211A
, 117, 40, 48, 8, BW40MINUS
},
2696 { HOSTAPD_MODE_IEEE80211A
, 126, 149, 157, 8, BW40PLUS
},
2697 { HOSTAPD_MODE_IEEE80211A
, 127, 153, 161, 8, BW40MINUS
},
2698 { -1, 0, 0, 0, 0, BW20
}
2702 static int wpas_p2p_verify_channel(struct wpa_supplicant
*wpa_s
,
2703 struct hostapd_hw_modes
*mode
,
2708 if (!has_channel(wpa_s
->global
, mode
, channel
, &flag
))
2710 if (bw
== BW40MINUS
&&
2711 (!(flag
& HOSTAPD_CHAN_HT40MINUS
) ||
2712 !has_channel(wpa_s
->global
, mode
, channel
- 4, NULL
)))
2714 if (bw
== BW40PLUS
&&
2715 (!(flag
& HOSTAPD_CHAN_HT40PLUS
) ||
2716 !has_channel(wpa_s
->global
, mode
, channel
+ 4, NULL
)))
2722 static int wpas_p2p_setup_channels(struct wpa_supplicant
*wpa_s
,
2723 struct p2p_channels
*chan
)
2725 struct hostapd_hw_modes
*mode
;
2728 if (wpa_s
->hw
.modes
== NULL
) {
2729 wpa_printf(MSG_DEBUG
, "P2P: Driver did not support fetching "
2730 "of all supported channels; assume dualband "
2732 return wpas_p2p_default_channels(wpa_s
, chan
);
2737 for (op
= 0; op_class
[op
].op_class
; op
++) {
2738 struct p2p_oper_class_map
*o
= &op_class
[op
];
2740 struct p2p_reg_class
*reg
= NULL
;
2742 mode
= get_mode(wpa_s
->hw
.modes
, wpa_s
->hw
.num_modes
, o
->mode
);
2745 for (ch
= o
->min_chan
; ch
<= o
->max_chan
; ch
+= o
->inc
) {
2746 if (wpas_p2p_verify_channel(wpa_s
, mode
, ch
, o
->bw
) < 1)
2749 wpa_printf(MSG_DEBUG
, "P2P: Add operating "
2750 "class %u", o
->op_class
);
2751 reg
= &chan
->reg_class
[cla
];
2753 reg
->reg_class
= o
->op_class
;
2755 reg
->channel
[reg
->channels
] = ch
;
2759 wpa_hexdump(MSG_DEBUG
, "P2P: Channels",
2760 reg
->channel
, reg
->channels
);
2764 chan
->reg_classes
= cla
;
2770 int wpas_p2p_get_ht40_mode(struct wpa_supplicant
*wpa_s
,
2771 struct hostapd_hw_modes
*mode
, u8 channel
)
2775 for (op
= 0; op_class
[op
].op_class
; op
++) {
2776 struct p2p_oper_class_map
*o
= &op_class
[op
];
2779 for (ch
= o
->min_chan
; ch
<= o
->max_chan
; ch
+= o
->inc
) {
2780 if (o
->mode
!= HOSTAPD_MODE_IEEE80211A
||
2781 o
->bw
== BW20
|| ch
!= channel
)
2783 ret
= wpas_p2p_verify_channel(wpa_s
, mode
, ch
, o
->bw
);
2787 return (o
->bw
== BW40MINUS
) ? -1 : 1;
2796 static int wpas_get_noa(void *ctx
, const u8
*interface_addr
, u8
*buf
,
2799 struct wpa_supplicant
*wpa_s
= ctx
;
2801 for (wpa_s
= wpa_s
->global
->ifaces
; wpa_s
; wpa_s
= wpa_s
->next
) {
2802 if (os_memcmp(wpa_s
->own_addr
, interface_addr
, ETH_ALEN
) == 0)
2808 return wpa_drv_get_noa(wpa_s
, buf
, buf_len
);
2812 static int wpas_go_connected(void *ctx
, const u8
*dev_addr
)
2814 struct wpa_supplicant
*wpa_s
= ctx
;
2816 for (wpa_s
= wpa_s
->global
->ifaces
; wpa_s
; wpa_s
= wpa_s
->next
) {
2817 struct wpa_ssid
*ssid
= wpa_s
->current_ssid
;
2820 if (ssid
->mode
!= WPAS_MODE_INFRA
)
2822 if (wpa_s
->wpa_state
!= WPA_COMPLETED
&&
2823 wpa_s
->wpa_state
!= WPA_GROUP_HANDSHAKE
)
2825 if (os_memcmp(wpa_s
->go_dev_addr
, dev_addr
, ETH_ALEN
) == 0)
2834 * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
2835 * @global: Pointer to global data from wpa_supplicant_init()
2836 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
2837 * Returns: 0 on success, -1 on failure
2839 int wpas_p2p_init(struct wpa_global
*global
, struct wpa_supplicant
*wpa_s
)
2841 struct p2p_config p2p
;
2845 if (!(wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_CAPABLE
))
2851 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
) {
2852 struct p2p_params params
;
2854 wpa_printf(MSG_DEBUG
, "P2P: Use driver-based P2P management");
2855 os_memset(¶ms
, 0, sizeof(params
));
2856 params
.dev_name
= wpa_s
->conf
->device_name
;
2857 os_memcpy(params
.pri_dev_type
, wpa_s
->conf
->device_type
,
2859 params
.num_sec_dev_types
= wpa_s
->conf
->num_sec_device_types
;
2860 os_memcpy(params
.sec_dev_type
,
2861 wpa_s
->conf
->sec_device_type
,
2862 params
.num_sec_dev_types
* WPS_DEV_TYPE_LEN
);
2864 if (wpa_drv_p2p_set_params(wpa_s
, ¶ms
) < 0)
2870 os_memset(&p2p
, 0, sizeof(p2p
));
2871 p2p
.msg_ctx
= wpa_s
;
2873 p2p
.p2p_scan
= wpas_p2p_scan
;
2874 p2p
.send_action
= wpas_send_action
;
2875 p2p
.send_action_done
= wpas_send_action_done
;
2876 p2p
.go_neg_completed
= wpas_go_neg_completed
;
2877 p2p
.go_neg_req_rx
= wpas_go_neg_req_rx
;
2878 p2p
.dev_found
= wpas_dev_found
;
2879 p2p
.dev_lost
= wpas_dev_lost
;
2880 p2p
.start_listen
= wpas_start_listen
;
2881 p2p
.stop_listen
= wpas_stop_listen
;
2882 p2p
.send_probe_resp
= wpas_send_probe_resp
;
2883 p2p
.sd_request
= wpas_sd_request
;
2884 p2p
.sd_response
= wpas_sd_response
;
2885 p2p
.prov_disc_req
= wpas_prov_disc_req
;
2886 p2p
.prov_disc_resp
= wpas_prov_disc_resp
;
2887 p2p
.prov_disc_fail
= wpas_prov_disc_fail
;
2888 p2p
.invitation_process
= wpas_invitation_process
;
2889 p2p
.invitation_received
= wpas_invitation_received
;
2890 p2p
.invitation_result
= wpas_invitation_result
;
2891 p2p
.get_noa
= wpas_get_noa
;
2892 p2p
.go_connected
= wpas_go_connected
;
2894 os_memcpy(wpa_s
->global
->p2p_dev_addr
, wpa_s
->own_addr
, ETH_ALEN
);
2895 os_memcpy(p2p
.dev_addr
, wpa_s
->global
->p2p_dev_addr
, ETH_ALEN
);
2896 p2p
.dev_name
= wpa_s
->conf
->device_name
;
2897 p2p
.manufacturer
= wpa_s
->conf
->manufacturer
;
2898 p2p
.model_name
= wpa_s
->conf
->model_name
;
2899 p2p
.model_number
= wpa_s
->conf
->model_number
;
2900 p2p
.serial_number
= wpa_s
->conf
->serial_number
;
2902 os_memcpy(p2p
.uuid
, wpa_s
->wps
->uuid
, 16);
2903 p2p
.config_methods
= wpa_s
->wps
->config_methods
;
2906 if (wpa_s
->conf
->p2p_listen_reg_class
&&
2907 wpa_s
->conf
->p2p_listen_channel
) {
2908 p2p
.reg_class
= wpa_s
->conf
->p2p_listen_reg_class
;
2909 p2p
.channel
= wpa_s
->conf
->p2p_listen_channel
;
2913 * Pick one of the social channels randomly as the listen
2916 os_get_random((u8
*) &r
, sizeof(r
));
2917 p2p
.channel
= 1 + (r
% 3) * 5;
2919 wpa_printf(MSG_DEBUG
, "P2P: Own listen channel: %d", p2p
.channel
);
2921 if (wpa_s
->conf
->p2p_oper_reg_class
&&
2922 wpa_s
->conf
->p2p_oper_channel
) {
2923 p2p
.op_reg_class
= wpa_s
->conf
->p2p_oper_reg_class
;
2924 p2p
.op_channel
= wpa_s
->conf
->p2p_oper_channel
;
2925 p2p
.cfg_op_channel
= 1;
2926 wpa_printf(MSG_DEBUG
, "P2P: Configured operating channel: "
2927 "%d:%d", p2p
.op_reg_class
, p2p
.op_channel
);
2930 p2p
.op_reg_class
= 81;
2932 * Use random operation channel from (1, 6, 11) if no other
2933 * preference is indicated.
2935 os_get_random((u8
*) &r
, sizeof(r
));
2936 p2p
.op_channel
= 1 + (r
% 3) * 5;
2937 p2p
.cfg_op_channel
= 0;
2938 wpa_printf(MSG_DEBUG
, "P2P: Random operating channel: "
2939 "%d:%d", p2p
.op_reg_class
, p2p
.op_channel
);
2941 if (wpa_s
->conf
->country
[0] && wpa_s
->conf
->country
[1]) {
2942 os_memcpy(p2p
.country
, wpa_s
->conf
->country
, 2);
2943 p2p
.country
[2] = 0x04;
2945 os_memcpy(p2p
.country
, "XX\x04", 3);
2947 if (wpas_p2p_setup_channels(wpa_s
, &p2p
.channels
)) {
2948 wpa_printf(MSG_ERROR
, "P2P: Failed to configure supported "
2953 os_memcpy(p2p
.pri_dev_type
, wpa_s
->conf
->device_type
,
2956 p2p
.num_sec_dev_types
= wpa_s
->conf
->num_sec_device_types
;
2957 os_memcpy(p2p
.sec_dev_type
, wpa_s
->conf
->sec_device_type
,
2958 p2p
.num_sec_dev_types
* WPS_DEV_TYPE_LEN
);
2960 p2p
.concurrent_operations
= !!(wpa_s
->drv_flags
&
2961 WPA_DRIVER_FLAGS_P2P_CONCURRENT
);
2963 p2p
.max_peers
= 100;
2965 if (wpa_s
->conf
->p2p_ssid_postfix
) {
2966 p2p
.ssid_postfix_len
=
2967 os_strlen(wpa_s
->conf
->p2p_ssid_postfix
);
2968 if (p2p
.ssid_postfix_len
> sizeof(p2p
.ssid_postfix
))
2969 p2p
.ssid_postfix_len
= sizeof(p2p
.ssid_postfix
);
2970 os_memcpy(p2p
.ssid_postfix
, wpa_s
->conf
->p2p_ssid_postfix
,
2971 p2p
.ssid_postfix_len
);
2974 p2p
.p2p_intra_bss
= wpa_s
->conf
->p2p_intra_bss
;
2976 p2p
.max_listen
= wpa_s
->max_remain_on_chan
;
2978 global
->p2p
= p2p_init(&p2p
);
2979 if (global
->p2p
== NULL
)
2981 global
->p2p_init_wpa_s
= wpa_s
;
2983 for (i
= 0; i
< MAX_WPS_VENDOR_EXT
; i
++) {
2984 if (wpa_s
->conf
->wps_vendor_ext
[i
] == NULL
)
2986 p2p_add_wps_vendor_extension(
2987 global
->p2p
, wpa_s
->conf
->wps_vendor_ext
[i
]);
2995 * wpas_p2p_deinit - Deinitialize per-interface P2P data
2996 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
2998 * This function deinitialize per-interface P2P data.
3000 void wpas_p2p_deinit(struct wpa_supplicant
*wpa_s
)
3002 if (wpa_s
->driver
&& wpa_s
->drv_priv
)
3003 wpa_drv_probe_req_report(wpa_s
, 0);
3005 if (wpa_s
->go_params
) {
3006 /* Clear any stored provisioning info */
3007 p2p_clear_provisioning_info(
3009 wpa_s
->go_params
->peer_device_addr
);
3012 os_free(wpa_s
->go_params
);
3013 wpa_s
->go_params
= NULL
;
3014 eloop_cancel_timeout(wpas_p2p_group_formation_timeout
, wpa_s
, NULL
);
3015 eloop_cancel_timeout(wpas_p2p_join_scan
, wpa_s
, NULL
);
3016 wpa_s
->p2p_long_listen
= 0;
3017 eloop_cancel_timeout(wpas_p2p_long_listen_timeout
, wpa_s
, NULL
);
3018 eloop_cancel_timeout(wpas_p2p_group_idle_timeout
, wpa_s
, NULL
);
3019 wpas_p2p_remove_pending_group_interface(wpa_s
);
3021 /* TODO: remove group interface from the driver if this wpa_s instance
3022 * is on top of a P2P group interface */
3027 * wpas_p2p_deinit_global - Deinitialize global P2P module
3028 * @global: Pointer to global data from wpa_supplicant_init()
3030 * This function deinitializes the global (per device) P2P module.
3032 void wpas_p2p_deinit_global(struct wpa_global
*global
)
3034 struct wpa_supplicant
*wpa_s
, *tmp
;
3036 wpa_s
= global
->ifaces
;
3038 wpas_p2p_service_flush(wpa_s
);
3040 if (global
->p2p
== NULL
)
3043 /* Remove remaining P2P group interfaces */
3044 while (wpa_s
&& wpa_s
->p2p_group_interface
!= NOT_P2P_GROUP_INTERFACE
)
3045 wpa_s
= wpa_s
->next
;
3047 tmp
= global
->ifaces
;
3050 tmp
->p2p_group_interface
== NOT_P2P_GROUP_INTERFACE
)) {
3055 /* Disconnect from the P2P group and deinit the interface */
3056 wpas_p2p_disconnect(tmp
);
3060 * Deinit GO data on any possibly remaining interface (if main
3061 * interface is used as GO).
3063 for (wpa_s
= global
->ifaces
; wpa_s
; wpa_s
= wpa_s
->next
) {
3064 if (wpa_s
->ap_iface
)
3065 wpas_p2p_group_deinit(wpa_s
);
3068 p2p_deinit(global
->p2p
);
3070 global
->p2p_init_wpa_s
= NULL
;
3074 static int wpas_p2p_create_iface(struct wpa_supplicant
*wpa_s
)
3076 if (wpa_s
->conf
->p2p_no_group_iface
)
3077 return 0; /* separate interface disabled per configuration */
3078 if (wpa_s
->drv_flags
&
3079 (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE
|
3080 WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P
))
3081 return 1; /* P2P group requires a new interface in every case
3083 if (!(wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_CONCURRENT
))
3084 return 0; /* driver does not support concurrent operations */
3085 if (wpa_s
->global
->ifaces
->next
)
3086 return 1; /* more that one interface already in use */
3087 if (wpa_s
->wpa_state
>= WPA_AUTHENTICATING
)
3088 return 1; /* this interface is already in use */
3093 static int wpas_p2p_start_go_neg(struct wpa_supplicant
*wpa_s
,
3094 const u8
*peer_addr
,
3095 enum p2p_wps_method wps_method
,
3096 int go_intent
, const u8
*own_interface_addr
,
3097 unsigned int force_freq
, int persistent_group
,
3098 struct wpa_ssid
*ssid
, unsigned int pref_freq
)
3100 if (persistent_group
&& wpa_s
->conf
->persistent_reconnect
)
3101 persistent_group
= 2;
3103 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
) {
3104 return wpa_drv_p2p_connect(wpa_s
, peer_addr
, wps_method
,
3105 go_intent
, own_interface_addr
,
3106 force_freq
, persistent_group
);
3110 * Increase GO config timeout if HT40 is used since it takes some time
3111 * to scan channels for coex purposes before the BSS can be started.
3113 p2p_set_config_timeout(wpa_s
->global
->p2p
,
3114 wpa_s
->p2p_go_ht40
? 255 : 100, 20);
3116 return p2p_connect(wpa_s
->global
->p2p
, peer_addr
, wps_method
,
3117 go_intent
, own_interface_addr
, force_freq
,
3118 persistent_group
, ssid
? ssid
->ssid
: NULL
,
3119 ssid
? ssid
->ssid_len
: 0,
3120 wpa_s
->p2p_pd_before_go_neg
, pref_freq
);
3124 static int wpas_p2p_auth_go_neg(struct wpa_supplicant
*wpa_s
,
3125 const u8
*peer_addr
,
3126 enum p2p_wps_method wps_method
,
3127 int go_intent
, const u8
*own_interface_addr
,
3128 unsigned int force_freq
, int persistent_group
,
3129 struct wpa_ssid
*ssid
, unsigned int pref_freq
)
3131 if (persistent_group
&& wpa_s
->conf
->persistent_reconnect
)
3132 persistent_group
= 2;
3134 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
3137 return p2p_authorize(wpa_s
->global
->p2p
, peer_addr
, wps_method
,
3138 go_intent
, own_interface_addr
, force_freq
,
3139 persistent_group
, ssid
? ssid
->ssid
: NULL
,
3140 ssid
? ssid
->ssid_len
: 0, pref_freq
);
3144 static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant
*wpa_s
)
3146 wpa_s
->p2p_join_scan_count
++;
3147 wpa_printf(MSG_DEBUG
, "P2P: Join scan attempt %d",
3148 wpa_s
->p2p_join_scan_count
);
3149 if (wpa_s
->p2p_join_scan_count
> P2P_MAX_JOIN_SCAN_ATTEMPTS
) {
3150 wpa_printf(MSG_DEBUG
, "P2P: Failed to find GO " MACSTR
3151 " for join operationg - stop join attempt",
3152 MAC2STR(wpa_s
->pending_join_iface_addr
));
3153 eloop_cancel_timeout(wpas_p2p_join_scan
, wpa_s
, NULL
);
3154 if (wpa_s
->p2p_auto_pd
) {
3155 wpa_s
->p2p_auto_pd
= 0;
3156 wpa_msg(wpa_s
, MSG_INFO
, P2P_EVENT_PROV_DISC_FAILURE
3157 " p2p_dev_addr=" MACSTR
" status=N/A",
3158 MAC2STR(wpa_s
->pending_join_dev_addr
));
3161 wpa_msg(wpa_s
->parent
, MSG_INFO
,
3162 P2P_EVENT_GROUP_FORMATION_FAILURE
);
3167 static int wpas_check_freq_conflict(struct wpa_supplicant
*wpa_s
, int freq
)
3169 struct wpa_supplicant
*iface
;
3173 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT
)
3176 for (iface
= wpa_s
->global
->ifaces
; iface
; iface
= iface
->next
) {
3177 if (!wpas_p2p_create_iface(wpa_s
) && iface
== wpa_s
)
3179 if (iface
->current_ssid
== NULL
|| iface
->assoc_freq
== 0)
3181 if (iface
->current_ssid
->mode
== WPAS_MODE_AP
||
3182 iface
->current_ssid
->mode
== WPAS_MODE_P2P_GO
)
3183 shared_freq
= iface
->current_ssid
->frequency
;
3184 else if (wpa_drv_get_bssid(iface
, bssid
) == 0)
3185 shared_freq
= iface
->assoc_freq
;
3189 if (shared_freq
&& freq
!= shared_freq
) {
3190 wpa_printf(MSG_DEBUG
, "P2P: Frequency conflict - %s "
3191 "connected on %d MHz - new connection on "
3192 "%d MHz", iface
->ifname
, shared_freq
, freq
);
3197 shared_freq
= wpa_drv_shared_freq(wpa_s
);
3198 if (shared_freq
> 0 && shared_freq
!= freq
) {
3199 wpa_printf(MSG_DEBUG
, "P2P: Frequency conflict - shared "
3200 "virtual interface connected on %d MHz - new "
3201 "connection on %d MHz", shared_freq
, freq
);
3209 static int wpas_p2p_peer_go(struct wpa_supplicant
*wpa_s
,
3210 const u8
*peer_dev_addr
)
3212 struct wpa_bss
*bss
;
3215 bss
= wpa_bss_get_p2p_dev_addr(wpa_s
, peer_dev_addr
);
3218 if (bss
->last_update_idx
< wpa_s
->bss_update_idx
) {
3219 wpa_printf(MSG_DEBUG
, "P2P: Peer BSS entry not updated in the "
3224 updated
= os_time_before(&wpa_s
->p2p_auto_started
, &bss
->last_update
);
3225 wpa_printf(MSG_DEBUG
, "P2P: Current BSS entry for peer updated at "
3226 "%ld.%06ld (%supdated in last scan)",
3227 bss
->last_update
.sec
, bss
->last_update
.usec
,
3228 updated
? "": "not ");
3234 static void wpas_p2p_scan_res_join(struct wpa_supplicant
*wpa_s
,
3235 struct wpa_scan_results
*scan_res
)
3237 struct wpa_bss
*bss
;
3239 u8 iface_addr
[ETH_ALEN
];
3241 eloop_cancel_timeout(wpas_p2p_join_scan
, wpa_s
, NULL
);
3243 if (wpa_s
->global
->p2p_disabled
)
3246 wpa_printf(MSG_DEBUG
, "P2P: Scan results received (%d BSS) for %sjoin",
3247 scan_res
? (int) scan_res
->num
: -1,
3248 wpa_s
->p2p_auto_join
? "auto_" : "");
3251 wpas_p2p_scan_res_handler(wpa_s
, scan_res
);
3253 if (wpa_s
->p2p_auto_pd
) {
3254 int join
= wpas_p2p_peer_go(wpa_s
,
3255 wpa_s
->pending_join_dev_addr
);
3257 wpa_s
->auto_pd_scan_retry
< P2P_AUTO_PD_SCAN_ATTEMPTS
) {
3258 wpa_s
->auto_pd_scan_retry
++;
3259 bss
= wpa_bss_get_bssid(wpa_s
,
3260 wpa_s
->pending_join_dev_addr
);
3263 wpa_printf(MSG_DEBUG
, "P2P: Scan retry %d for "
3264 "the peer " MACSTR
" at %d MHz",
3265 wpa_s
->auto_pd_scan_retry
,
3267 pending_join_dev_addr
),
3269 wpas_p2p_join_scan_req(wpa_s
, freq
);
3277 wpa_s
->p2p_auto_pd
= 0;
3278 wpa_s
->pending_pd_use
= join
? AUTO_PD_JOIN
: AUTO_PD_GO_NEG
;
3279 wpa_printf(MSG_DEBUG
, "P2P: Auto PD with " MACSTR
" join=%d",
3280 MAC2STR(wpa_s
->pending_join_dev_addr
), join
);
3281 if (p2p_prov_disc_req(wpa_s
->global
->p2p
,
3282 wpa_s
->pending_join_dev_addr
,
3283 wpa_s
->pending_pd_config_methods
, join
,
3284 0, wpa_s
->user_initiated_pd
) < 0) {
3285 wpa_s
->p2p_auto_pd
= 0;
3286 wpa_msg(wpa_s
, MSG_INFO
, P2P_EVENT_PROV_DISC_FAILURE
3287 " p2p_dev_addr=" MACSTR
" status=N/A",
3288 MAC2STR(wpa_s
->pending_join_dev_addr
));
3293 if (wpa_s
->p2p_auto_join
) {
3294 int join
= wpas_p2p_peer_go(wpa_s
,
3295 wpa_s
->pending_join_dev_addr
);
3297 wpa_printf(MSG_DEBUG
, "P2P: Peer was not found to be "
3298 "running a GO -> use GO Negotiation");
3299 wpas_p2p_connect(wpa_s
, wpa_s
->pending_join_dev_addr
,
3300 wpa_s
->p2p_pin
, wpa_s
->p2p_wps_method
,
3301 wpa_s
->p2p_persistent_group
, 0, 0, 0,
3302 wpa_s
->p2p_go_intent
,
3303 wpa_s
->p2p_connect_freq
,
3304 wpa_s
->p2p_persistent_id
,
3305 wpa_s
->p2p_pd_before_go_neg
,
3306 wpa_s
->p2p_go_ht40
);
3310 wpa_printf(MSG_DEBUG
, "P2P: Peer was found running GO%s -> "
3311 "try to join the group", join
? "" :
3314 wpa_s
->p2p_fallback_to_go_neg
= 1;
3317 freq
= p2p_get_oper_freq(wpa_s
->global
->p2p
,
3318 wpa_s
->pending_join_iface_addr
);
3320 p2p_get_interface_addr(wpa_s
->global
->p2p
,
3321 wpa_s
->pending_join_dev_addr
,
3323 os_memcmp(iface_addr
, wpa_s
->pending_join_dev_addr
, ETH_ALEN
) != 0)
3325 wpa_printf(MSG_DEBUG
, "P2P: Overwrite pending interface "
3326 "address for join from " MACSTR
" to " MACSTR
3327 " based on newly discovered P2P peer entry",
3328 MAC2STR(wpa_s
->pending_join_iface_addr
),
3329 MAC2STR(iface_addr
));
3330 os_memcpy(wpa_s
->pending_join_iface_addr
, iface_addr
,
3333 freq
= p2p_get_oper_freq(wpa_s
->global
->p2p
,
3334 wpa_s
->pending_join_iface_addr
);
3337 wpa_printf(MSG_DEBUG
, "P2P: Target GO operating frequency "
3338 "from P2P peer table: %d MHz", freq
);
3340 bss
= wpa_bss_get_bssid(wpa_s
, wpa_s
->pending_join_iface_addr
);
3343 wpa_printf(MSG_DEBUG
, "P2P: Target GO operating frequency "
3344 "from BSS table: %d MHz", freq
);
3349 if (wpas_check_freq_conflict(wpa_s
, freq
) > 0) {
3350 wpa_msg(wpa_s
->parent
, MSG_INFO
,
3351 P2P_EVENT_GROUP_FORMATION_FAILURE
3352 "reason=FREQ_CONFLICT");
3356 wpa_printf(MSG_DEBUG
, "P2P: Send Provision Discovery Request "
3357 "prior to joining an existing group (GO " MACSTR
3359 MAC2STR(wpa_s
->pending_join_dev_addr
), freq
);
3360 wpa_s
->pending_pd_before_join
= 1;
3362 switch (wpa_s
->pending_join_wps_method
) {
3363 case WPS_PIN_DISPLAY
:
3364 method
= WPS_CONFIG_KEYPAD
;
3366 case WPS_PIN_KEYPAD
:
3367 method
= WPS_CONFIG_DISPLAY
;
3370 method
= WPS_CONFIG_PUSHBUTTON
;
3377 if ((p2p_get_provisioning_info(wpa_s
->global
->p2p
,
3378 wpa_s
->pending_join_dev_addr
) ==
3381 * We have already performed provision discovery for
3382 * joining the group. Proceed directly to join
3383 * operation without duplicated provision discovery. */
3384 wpa_printf(MSG_DEBUG
, "P2P: Provision discovery "
3385 "with " MACSTR
" already done - proceed to "
3387 MAC2STR(wpa_s
->pending_join_dev_addr
));
3388 wpa_s
->pending_pd_before_join
= 0;
3392 if (p2p_prov_disc_req(wpa_s
->global
->p2p
,
3393 wpa_s
->pending_join_dev_addr
, method
, 1,
3394 freq
, wpa_s
->user_initiated_pd
) < 0) {
3395 wpa_printf(MSG_DEBUG
, "P2P: Failed to send Provision "
3396 "Discovery Request before joining an "
3398 wpa_s
->pending_pd_before_join
= 0;
3404 wpa_printf(MSG_DEBUG
, "P2P: Failed to find BSS/GO - try again later");
3405 eloop_cancel_timeout(wpas_p2p_join_scan
, wpa_s
, NULL
);
3406 eloop_register_timeout(1, 0, wpas_p2p_join_scan
, wpa_s
, NULL
);
3407 wpas_p2p_check_join_scan_limit(wpa_s
);
3411 /* Start join operation immediately */
3412 wpas_p2p_join_start(wpa_s
);
3416 static void wpas_p2p_join_scan_req(struct wpa_supplicant
*wpa_s
, int freq
)
3419 struct wpa_driver_scan_params params
;
3420 struct wpabuf
*wps_ie
, *ies
;
3422 int freqs
[2] = { 0, 0 };
3424 os_memset(¶ms
, 0, sizeof(params
));
3426 /* P2P Wildcard SSID */
3427 params
.num_ssids
= 1;
3428 params
.ssids
[0].ssid
= (u8
*) P2P_WILDCARD_SSID
;
3429 params
.ssids
[0].ssid_len
= P2P_WILDCARD_SSID_LEN
;
3431 wpa_s
->wps
->dev
.p2p
= 1;
3432 wps_ie
= wps_build_probe_req_ie(DEV_PW_DEFAULT
, &wpa_s
->wps
->dev
,
3433 wpa_s
->wps
->uuid
, WPS_REQ_ENROLLEE
, 0,
3435 if (wps_ie
== NULL
) {
3436 wpas_p2p_scan_res_join(wpa_s
, NULL
);
3440 ielen
= p2p_scan_ie_buf_len(wpa_s
->global
->p2p
);
3441 ies
= wpabuf_alloc(wpabuf_len(wps_ie
) + ielen
);
3443 wpabuf_free(wps_ie
);
3444 wpas_p2p_scan_res_join(wpa_s
, NULL
);
3447 wpabuf_put_buf(ies
, wps_ie
);
3448 wpabuf_free(wps_ie
);
3450 p2p_scan_ie(wpa_s
->global
->p2p
, ies
, NULL
);
3452 params
.p2p_probe
= 1;
3453 params
.extra_ies
= wpabuf_head(ies
);
3454 params
.extra_ies_len
= wpabuf_len(ies
);
3457 params
.freqs
= freqs
;
3461 * Run a scan to update BSS table and start Provision Discovery once
3462 * the new scan results become available.
3464 ret
= wpa_drv_scan(wpa_s
, ¶ms
);
3466 wpa_s
->scan_res_handler
= wpas_p2p_scan_res_join
;
3471 wpa_printf(MSG_DEBUG
, "P2P: Failed to start scan for join - "
3473 eloop_cancel_timeout(wpas_p2p_join_scan
, wpa_s
, NULL
);
3474 eloop_register_timeout(1, 0, wpas_p2p_join_scan
, wpa_s
, NULL
);
3475 wpas_p2p_check_join_scan_limit(wpa_s
);
3480 static void wpas_p2p_join_scan(void *eloop_ctx
, void *timeout_ctx
)
3482 struct wpa_supplicant
*wpa_s
= eloop_ctx
;
3483 wpas_p2p_join_scan_req(wpa_s
, 0);
3487 static int wpas_p2p_join(struct wpa_supplicant
*wpa_s
, const u8
*iface_addr
,
3488 const u8
*dev_addr
, enum p2p_wps_method wps_method
,
3491 wpa_printf(MSG_DEBUG
, "P2P: Request to join existing group (iface "
3492 MACSTR
" dev " MACSTR
")%s",
3493 MAC2STR(iface_addr
), MAC2STR(dev_addr
),
3494 auto_join
? " (auto_join)" : "");
3496 wpa_s
->p2p_auto_pd
= 0;
3497 wpa_s
->p2p_auto_join
= !!auto_join
;
3498 os_memcpy(wpa_s
->pending_join_iface_addr
, iface_addr
, ETH_ALEN
);
3499 os_memcpy(wpa_s
->pending_join_dev_addr
, dev_addr
, ETH_ALEN
);
3500 wpa_s
->pending_join_wps_method
= wps_method
;
3502 /* Make sure we are not running find during connection establishment */
3503 wpas_p2p_stop_find(wpa_s
);
3505 wpa_s
->p2p_join_scan_count
= 0;
3506 wpas_p2p_join_scan(wpa_s
, NULL
);
3511 static int wpas_p2p_join_start(struct wpa_supplicant
*wpa_s
)
3513 struct wpa_supplicant
*group
;
3514 struct p2p_go_neg_results res
;
3515 struct wpa_bss
*bss
;
3517 group
= wpas_p2p_get_group_iface(wpa_s
, 0, 0);
3520 if (group
!= wpa_s
) {
3521 os_memcpy(group
->p2p_pin
, wpa_s
->p2p_pin
,
3522 sizeof(group
->p2p_pin
));
3523 group
->p2p_wps_method
= wpa_s
->p2p_wps_method
;
3526 * Need to mark the current interface for p2p_group_formation
3527 * when a separate group interface is not used. This is needed
3528 * to allow p2p_cancel stop a pending p2p_connect-join.
3529 * wpas_p2p_init_group_interface() addresses this for the case
3530 * where a separate group interface is used.
3532 wpa_s
->global
->p2p_group_formation
= wpa_s
;
3535 group
->p2p_in_provisioning
= 1;
3536 group
->p2p_fallback_to_go_neg
= wpa_s
->p2p_fallback_to_go_neg
;
3538 os_memset(&res
, 0, sizeof(res
));
3539 os_memcpy(res
.peer_interface_addr
, wpa_s
->pending_join_iface_addr
,
3541 res
.wps_method
= wpa_s
->pending_join_wps_method
;
3542 bss
= wpa_bss_get_bssid(wpa_s
, wpa_s
->pending_join_iface_addr
);
3544 res
.freq
= bss
->freq
;
3545 res
.ssid_len
= bss
->ssid_len
;
3546 os_memcpy(res
.ssid
, bss
->ssid
, bss
->ssid_len
);
3549 if (wpa_s
->off_channel_freq
|| wpa_s
->roc_waiting_drv_freq
) {
3550 wpa_printf(MSG_DEBUG
, "P2P: Cancel remain-on-channel prior to "
3552 wpa_drv_cancel_remain_on_channel(wpa_s
);
3553 wpa_s
->off_channel_freq
= 0;
3554 wpa_s
->roc_waiting_drv_freq
= 0;
3556 wpas_start_wps_enrollee(group
, &res
);
3559 * Allow a longer timeout for join-a-running-group than normal 15
3560 * second group formation timeout since the GO may not have authorized
3561 * our connection yet.
3563 eloop_cancel_timeout(wpas_p2p_group_formation_timeout
, wpa_s
, NULL
);
3564 eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout
,
3571 static int wpas_p2p_setup_freqs(struct wpa_supplicant
*wpa_s
, int freq
,
3572 int *force_freq
, int *pref_freq
,
3576 if (!p2p_supported_freq(wpa_s
->global
->p2p
, freq
)) {
3577 wpa_printf(MSG_DEBUG
, "P2P: The forced channel "
3578 "(%u MHz) is not supported for P2P uses",
3583 if (*oper_freq
> 0 && freq
!= *oper_freq
&&
3584 !(wpa_s
->drv_flags
&
3585 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT
)) {
3586 wpa_printf(MSG_DEBUG
, "P2P: Cannot start P2P group "
3587 "on %u MHz while connected on another "
3588 "channel (%u MHz)", freq
, *oper_freq
);
3591 wpa_printf(MSG_DEBUG
, "P2P: Trying to force us to use the "
3592 "requested channel (%u MHz)", freq
);
3594 } else if (*oper_freq
> 0 &&
3595 !p2p_supported_freq(wpa_s
->global
->p2p
, *oper_freq
)) {
3596 if (!(wpa_s
->drv_flags
&
3597 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT
)) {
3598 wpa_printf(MSG_DEBUG
, "P2P: Cannot start P2P group "
3599 "while connected on non-P2P supported "
3600 "channel (%u MHz)", *oper_freq
);
3603 wpa_printf(MSG_DEBUG
, "P2P: Current operating channel "
3604 "(%u MHz) not available for P2P - try to use "
3605 "another channel", *oper_freq
);
3607 } else if (*oper_freq
> 0 && *pref_freq
== 0 &&
3609 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT
)) {
3610 wpa_printf(MSG_DEBUG
, "P2P: Trying to prefer the channel we "
3611 "are already using (%u MHz) on another interface",
3613 *pref_freq
= *oper_freq
;
3614 } else if (*oper_freq
> 0) {
3615 wpa_printf(MSG_DEBUG
, "P2P: Trying to force us to use the "
3616 "channel we are already using (%u MHz) on another "
3617 "interface", *oper_freq
);
3618 *force_freq
= *oper_freq
;
3626 * wpas_p2p_connect - Request P2P Group Formation to be started
3627 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3628 * @peer_addr: Address of the peer P2P Device
3629 * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
3630 * @persistent_group: Whether to create a persistent group
3631 * @auto_join: Whether to select join vs. GO Negotiation automatically
3632 * @join: Whether to join an existing group (as a client) instead of starting
3633 * Group Owner negotiation; @peer_addr is BSSID in that case
3634 * @auth: Whether to only authorize the connection instead of doing that and
3635 * initiating Group Owner negotiation
3636 * @go_intent: GO Intent or -1 to use default
3637 * @freq: Frequency for the group or 0 for auto-selection
3638 * @persistent_id: Persistent group credentials to use for forcing GO
3639 * parameters or -1 to generate new values (SSID/passphrase)
3640 * @pd: Whether to send Provision Discovery prior to GO Negotiation as an
3641 * interoperability workaround when initiating group formation
3642 * @ht40: Start GO with 40 MHz channel width
3643 * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
3644 * failure, -2 on failure due to channel not currently available,
3645 * -3 if forced channel is not supported
3647 int wpas_p2p_connect(struct wpa_supplicant
*wpa_s
, const u8
*peer_addr
,
3648 const char *pin
, enum p2p_wps_method wps_method
,
3649 int persistent_group
, int auto_join
, int join
, int auth
,
3650 int go_intent
, int freq
, int persistent_id
, int pd
,
3653 int force_freq
= 0, pref_freq
= 0, oper_freq
= 0;
3656 enum wpa_driver_if_type iftype
;
3658 struct wpa_ssid
*ssid
= NULL
;
3660 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
3663 if (persistent_id
>= 0) {
3664 ssid
= wpa_config_get_network(wpa_s
->conf
, persistent_id
);
3665 if (ssid
== NULL
|| ssid
->disabled
!= 2 ||
3666 ssid
->mode
!= WPAS_MODE_P2P_GO
)
3671 go_intent
= wpa_s
->conf
->p2p_go_intent
;
3674 wpa_s
->p2p_long_listen
= 0;
3676 wpa_s
->p2p_wps_method
= wps_method
;
3677 wpa_s
->p2p_persistent_group
= !!persistent_group
;
3678 wpa_s
->p2p_persistent_id
= persistent_id
;
3679 wpa_s
->p2p_go_intent
= go_intent
;
3680 wpa_s
->p2p_connect_freq
= freq
;
3681 wpa_s
->p2p_fallback_to_go_neg
= 0;
3682 wpa_s
->p2p_pd_before_go_neg
= !!pd
;
3683 wpa_s
->p2p_go_ht40
= !!ht40
;
3686 os_strlcpy(wpa_s
->p2p_pin
, pin
, sizeof(wpa_s
->p2p_pin
));
3687 else if (wps_method
== WPS_PIN_DISPLAY
) {
3688 ret
= wps_generate_pin();
3689 os_snprintf(wpa_s
->p2p_pin
, sizeof(wpa_s
->p2p_pin
), "%08d",
3691 wpa_printf(MSG_DEBUG
, "P2P: Randomly generated PIN: %s",
3694 wpa_s
->p2p_pin
[0] = '\0';
3696 if (join
|| auto_join
) {
3697 u8 iface_addr
[ETH_ALEN
], dev_addr
[ETH_ALEN
];
3699 wpa_printf(MSG_DEBUG
, "P2P: Authorize invitation to "
3700 "connect a running group from " MACSTR
,
3701 MAC2STR(peer_addr
));
3702 os_memcpy(wpa_s
->p2p_auth_invite
, peer_addr
, ETH_ALEN
);
3705 os_memcpy(dev_addr
, peer_addr
, ETH_ALEN
);
3706 if (p2p_get_interface_addr(wpa_s
->global
->p2p
, peer_addr
,
3708 os_memcpy(iface_addr
, peer_addr
, ETH_ALEN
);
3709 p2p_get_dev_addr(wpa_s
->global
->p2p
, peer_addr
,
3713 os_get_time(&wpa_s
->p2p_auto_started
);
3714 wpa_printf(MSG_DEBUG
, "P2P: Auto join started at "
3716 wpa_s
->p2p_auto_started
.sec
,
3717 wpa_s
->p2p_auto_started
.usec
);
3719 wpa_s
->user_initiated_pd
= 1;
3720 if (wpas_p2p_join(wpa_s
, iface_addr
, dev_addr
, wps_method
,
3726 if (wpa_s
->current_ssid
&& wpa_drv_get_bssid(wpa_s
, bssid
) == 0 &&
3727 wpa_s
->assoc_freq
) {
3728 oper_freq
= wpa_s
->assoc_freq
;
3730 oper_freq
= wpa_drv_shared_freq(wpa_s
);
3735 res
= wpas_p2p_setup_freqs(wpa_s
, freq
, &force_freq
, &pref_freq
,
3740 wpa_s
->create_p2p_iface
= wpas_p2p_create_iface(wpa_s
);
3742 if (wpa_s
->create_p2p_iface
) {
3743 /* Prepare to add a new interface for the group */
3744 iftype
= WPA_IF_P2P_GROUP
;
3745 if (go_intent
== 15)
3746 iftype
= WPA_IF_P2P_GO
;
3747 if (wpas_p2p_add_group_interface(wpa_s
, iftype
) < 0) {
3748 wpa_printf(MSG_ERROR
, "P2P: Failed to allocate a new "
3749 "interface for the group");
3753 if_addr
= wpa_s
->pending_interface_addr
;
3755 if_addr
= wpa_s
->own_addr
;
3758 if (wpas_p2p_auth_go_neg(wpa_s
, peer_addr
, wps_method
,
3760 force_freq
, persistent_group
, ssid
,
3766 if (wpas_p2p_start_go_neg(wpa_s
, peer_addr
, wps_method
,
3767 go_intent
, if_addr
, force_freq
,
3768 persistent_group
, ssid
, pref_freq
) < 0) {
3769 if (wpa_s
->create_p2p_iface
)
3770 wpas_p2p_remove_pending_group_interface(wpa_s
);
3778 * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
3779 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3780 * @freq: Frequency of the channel in MHz
3781 * @duration: Duration of the stay on the channel in milliseconds
3783 * This callback is called when the driver indicates that it has started the
3784 * requested remain-on-channel duration.
3786 void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant
*wpa_s
,
3787 unsigned int freq
, unsigned int duration
)
3789 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
3791 if (wpa_s
->off_channel_freq
== wpa_s
->pending_listen_freq
) {
3792 p2p_listen_cb(wpa_s
->global
->p2p
, wpa_s
->pending_listen_freq
,
3793 wpa_s
->pending_listen_duration
);
3794 wpa_s
->pending_listen_freq
= 0;
3799 static int wpas_p2p_listen_start(struct wpa_supplicant
*wpa_s
,
3800 unsigned int timeout
)
3802 /* Limit maximum Listen state time based on driver limitation. */
3803 if (timeout
> wpa_s
->max_remain_on_chan
)
3804 timeout
= wpa_s
->max_remain_on_chan
;
3806 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
3807 return wpa_drv_p2p_listen(wpa_s
, timeout
);
3809 return p2p_listen(wpa_s
->global
->p2p
, timeout
);
3814 * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
3815 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3816 * @freq: Frequency of the channel in MHz
3818 * This callback is called when the driver indicates that a remain-on-channel
3819 * operation has been completed, i.e., the duration on the requested channel
3822 void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant
*wpa_s
,
3825 wpa_printf(MSG_DEBUG
, "P2P: Cancel remain-on-channel callback "
3826 "(p2p_long_listen=%d ms pending_action_tx=%p)",
3827 wpa_s
->p2p_long_listen
, offchannel_pending_action_tx(wpa_s
));
3828 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
3830 if (p2p_listen_end(wpa_s
->global
->p2p
, freq
) > 0)
3831 return; /* P2P module started a new operation */
3832 if (offchannel_pending_action_tx(wpa_s
))
3834 if (wpa_s
->p2p_long_listen
> 0)
3835 wpa_s
->p2p_long_listen
-= wpa_s
->max_remain_on_chan
;
3836 if (wpa_s
->p2p_long_listen
> 0) {
3837 wpa_printf(MSG_DEBUG
, "P2P: Continuing long Listen state");
3838 wpas_p2p_listen_start(wpa_s
, wpa_s
->p2p_long_listen
);
3844 * wpas_p2p_group_remove - Remove a P2P group
3845 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3846 * @ifname: Network interface name of the group interface or "*" to remove all
3848 * Returns: 0 on success, -1 on failure
3850 * This function is used to remove a P2P group. This can be used to disconnect
3851 * from a group in which the local end is a P2P Client or to end a P2P Group in
3852 * case the local end is the Group Owner. If a virtual network interface was
3853 * created for this group, that interface will be removed. Otherwise, only the
3854 * configured P2P group network will be removed from the interface.
3856 int wpas_p2p_group_remove(struct wpa_supplicant
*wpa_s
, const char *ifname
)
3858 struct wpa_global
*global
= wpa_s
->global
;
3860 if (os_strcmp(ifname
, "*") == 0) {
3861 struct wpa_supplicant
*prev
;
3862 wpa_s
= global
->ifaces
;
3865 wpa_s
= wpa_s
->next
;
3866 wpas_p2p_disconnect(prev
);
3871 for (wpa_s
= global
->ifaces
; wpa_s
; wpa_s
= wpa_s
->next
) {
3872 if (os_strcmp(wpa_s
->ifname
, ifname
) == 0)
3876 return wpas_p2p_disconnect(wpa_s
);
3880 static int freq_included(const struct p2p_channels
*channels
, unsigned int freq
)
3882 if (channels
== NULL
)
3883 return 1; /* Assume no restrictions */
3884 return p2p_channels_includes_freq(channels
, freq
);
3888 static int wpas_p2p_init_go_params(struct wpa_supplicant
*wpa_s
,
3889 struct p2p_go_neg_results
*params
,
3891 const struct p2p_channels
*channels
)
3896 os_memset(params
, 0, sizeof(*params
));
3897 params
->role_go
= 1;
3898 params
->ht40
= ht40
;
3900 if (!freq_included(channels
, freq
)) {
3901 wpa_printf(MSG_DEBUG
, "P2P: Forced GO freq %d MHz not "
3905 wpa_printf(MSG_DEBUG
, "P2P: Set GO freq based on forced "
3906 "frequency %d MHz", freq
);
3907 params
->freq
= freq
;
3908 } else if (wpa_s
->conf
->p2p_oper_reg_class
== 81 &&
3909 wpa_s
->conf
->p2p_oper_channel
>= 1 &&
3910 wpa_s
->conf
->p2p_oper_channel
<= 11 &&
3911 freq_included(channels
,
3912 2407 + 5 * wpa_s
->conf
->p2p_oper_channel
)) {
3913 params
->freq
= 2407 + 5 * wpa_s
->conf
->p2p_oper_channel
;
3914 wpa_printf(MSG_DEBUG
, "P2P: Set GO freq based on configured "
3915 "frequency %d MHz", params
->freq
);
3916 } else if ((wpa_s
->conf
->p2p_oper_reg_class
== 115 ||
3917 wpa_s
->conf
->p2p_oper_reg_class
== 116 ||
3918 wpa_s
->conf
->p2p_oper_reg_class
== 117 ||
3919 wpa_s
->conf
->p2p_oper_reg_class
== 124 ||
3920 wpa_s
->conf
->p2p_oper_reg_class
== 126 ||
3921 wpa_s
->conf
->p2p_oper_reg_class
== 127) &&
3922 freq_included(channels
,
3923 5000 + 5 * wpa_s
->conf
->p2p_oper_channel
)) {
3924 params
->freq
= 5000 + 5 * wpa_s
->conf
->p2p_oper_channel
;
3925 wpa_printf(MSG_DEBUG
, "P2P: Set GO freq based on configured "
3926 "frequency %d MHz", params
->freq
);
3927 } else if (wpa_s
->conf
->p2p_oper_channel
== 0 &&
3928 wpa_s
->best_overall_freq
> 0 &&
3929 p2p_supported_freq(wpa_s
->global
->p2p
,
3930 wpa_s
->best_overall_freq
) &&
3931 freq_included(channels
, wpa_s
->best_overall_freq
)) {
3932 params
->freq
= wpa_s
->best_overall_freq
;
3933 wpa_printf(MSG_DEBUG
, "P2P: Set GO freq based on best overall "
3934 "channel %d MHz", params
->freq
);
3935 } else if (wpa_s
->conf
->p2p_oper_channel
== 0 &&
3936 wpa_s
->best_24_freq
> 0 &&
3937 p2p_supported_freq(wpa_s
->global
->p2p
,
3938 wpa_s
->best_24_freq
) &&
3939 freq_included(channels
, wpa_s
->best_24_freq
)) {
3940 params
->freq
= wpa_s
->best_24_freq
;
3941 wpa_printf(MSG_DEBUG
, "P2P: Set GO freq based on best 2.4 GHz "
3942 "channel %d MHz", params
->freq
);
3943 } else if (wpa_s
->conf
->p2p_oper_channel
== 0 &&
3944 wpa_s
->best_5_freq
> 0 &&
3945 p2p_supported_freq(wpa_s
->global
->p2p
,
3946 wpa_s
->best_5_freq
) &&
3947 freq_included(channels
, wpa_s
->best_5_freq
)) {
3948 params
->freq
= wpa_s
->best_5_freq
;
3949 wpa_printf(MSG_DEBUG
, "P2P: Set GO freq based on best 5 GHz "
3950 "channel %d MHz", params
->freq
);
3953 for (chan
= 0; chan
< 11; chan
++) {
3954 params
->freq
= 2412 + chan
* 5;
3955 if (!wpas_p2p_disallowed_freq(wpa_s
->global
,
3957 freq_included(channels
, params
->freq
))
3961 wpa_printf(MSG_DEBUG
, "P2P: No 2.4 GHz channel "
3965 wpa_printf(MSG_DEBUG
, "P2P: Set GO freq %d MHz (no preference "
3966 "known)", params
->freq
);
3969 if (wpa_s
->current_ssid
&& wpa_drv_get_bssid(wpa_s
, bssid
) == 0 &&
3970 wpa_s
->assoc_freq
&& !freq
) {
3971 if (!p2p_supported_freq(wpa_s
->global
->p2p
, wpa_s
->assoc_freq
)
3972 || !freq_included(channels
, wpa_s
->assoc_freq
)) {
3973 if (wpa_s
->drv_flags
&
3974 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT
) {
3975 wpa_printf(MSG_DEBUG
, "P2P: Cannot force GO on "
3976 "the channel we are already using "
3977 "(%u MHz) - allow multi-channel "
3978 "concurrency", wpa_s
->assoc_freq
);
3980 wpa_printf(MSG_DEBUG
, "P2P: Cannot force GO on "
3981 "the channel we are already using "
3982 "(%u MHz)", wpa_s
->assoc_freq
);
3986 wpa_printf(MSG_DEBUG
, "P2P: Force GO on the channel we "
3987 "are already using (%u MHz)",
3989 params
->freq
= wpa_s
->assoc_freq
;
3993 res
= wpa_drv_shared_freq(wpa_s
);
3994 if (res
> 0 && !freq
&&
3995 (!p2p_supported_freq(wpa_s
->global
->p2p
, res
) ||
3996 !freq_included(channels
, res
))) {
3997 if (wpa_s
->drv_flags
&
3998 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT
) {
3999 wpa_printf(MSG_DEBUG
, "P2P: Cannot force GO on the "
4000 "channel we are already using on a shared "
4001 "interface (%u MHz) - allow multi-channel "
4002 "concurrency", res
);
4004 wpa_printf(MSG_DEBUG
, "P2P: Cannot force GO on the "
4005 "channel we are already using on a shared "
4006 "interface (%u MHz)", res
);
4009 } else if (res
> 0 && !freq
) {
4010 wpa_printf(MSG_DEBUG
, "P2P: Force GO on the channel we are "
4011 "already using on a shared interface");
4013 if (!freq_included(channels
, params
->freq
)) {
4014 wpa_printf(MSG_DEBUG
, "P2P: Forced GO freq %d MHz not "
4015 "accepted", params
->freq
);
4018 } else if (res
> 0 && freq
!= res
&&
4019 !(wpa_s
->drv_flags
&
4020 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT
)) {
4021 wpa_printf(MSG_DEBUG
, "P2P: Cannot start P2P group on %u MHz "
4022 "while connected on another channel (%u MHz)",
4031 static struct wpa_supplicant
*
4032 wpas_p2p_get_group_iface(struct wpa_supplicant
*wpa_s
, int addr_allocated
,
4035 struct wpa_supplicant
*group_wpa_s
;
4037 if (!wpas_p2p_create_iface(wpa_s
)) {
4038 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: Use same interface for group "
4043 if (wpas_p2p_add_group_interface(wpa_s
, go
? WPA_IF_P2P_GO
:
4044 WPA_IF_P2P_CLIENT
) < 0) {
4045 wpa_msg(wpa_s
, MSG_ERROR
, "P2P: Failed to add group interface");
4048 group_wpa_s
= wpas_p2p_init_group_interface(wpa_s
, go
);
4049 if (group_wpa_s
== NULL
) {
4050 wpa_msg(wpa_s
, MSG_ERROR
, "P2P: Failed to initialize group "
4052 wpas_p2p_remove_pending_group_interface(wpa_s
);
4056 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: Use separate group interface %s",
4057 group_wpa_s
->ifname
);
4063 * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
4064 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4065 * @persistent_group: Whether to create a persistent group
4066 * @freq: Frequency for the group or 0 to indicate no hardcoding
4067 * Returns: 0 on success, -1 on failure
4069 * This function creates a new P2P group with the local end as the Group Owner,
4070 * i.e., without using Group Owner Negotiation.
4072 int wpas_p2p_group_add(struct wpa_supplicant
*wpa_s
, int persistent_group
,
4075 struct p2p_go_neg_results params
;
4078 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
4081 /* Make sure we are not running find during connection establishment */
4082 wpa_printf(MSG_DEBUG
, "P2P: Stop any on-going P2P FIND");
4083 wpas_p2p_stop_find_oper(wpa_s
);
4086 wpa_printf(MSG_DEBUG
, "P2P: Request to start GO on 2.4 GHz "
4088 if (wpa_s
->best_24_freq
> 0 &&
4089 p2p_supported_freq(wpa_s
->global
->p2p
,
4090 wpa_s
->best_24_freq
)) {
4091 freq
= wpa_s
->best_24_freq
;
4092 wpa_printf(MSG_DEBUG
, "P2P: Use best 2.4 GHz band "
4093 "channel: %d MHz", freq
);
4095 os_get_random((u8
*) &r
, sizeof(r
));
4096 freq
= 2412 + (r
% 3) * 25;
4097 wpa_printf(MSG_DEBUG
, "P2P: Use random 2.4 GHz band "
4098 "channel: %d MHz", freq
);
4103 wpa_printf(MSG_DEBUG
, "P2P: Request to start GO on 5 GHz "
4105 if (wpa_s
->best_5_freq
> 0 &&
4106 p2p_supported_freq(wpa_s
->global
->p2p
,
4107 wpa_s
->best_5_freq
)) {
4108 freq
= wpa_s
->best_5_freq
;
4109 wpa_printf(MSG_DEBUG
, "P2P: Use best 5 GHz band "
4110 "channel: %d MHz", freq
);
4112 os_get_random((u8
*) &r
, sizeof(r
));
4113 freq
= 5180 + (r
% 4) * 20;
4114 if (!p2p_supported_freq(wpa_s
->global
->p2p
, freq
)) {
4115 wpa_printf(MSG_DEBUG
, "P2P: Could not select "
4116 "5 GHz channel for P2P group");
4119 wpa_printf(MSG_DEBUG
, "P2P: Use random 5 GHz band "
4120 "channel: %d MHz", freq
);
4124 if (freq
> 0 && !p2p_supported_freq(wpa_s
->global
->p2p
, freq
)) {
4125 wpa_printf(MSG_DEBUG
, "P2P: The forced channel for GO "
4126 "(%u MHz) is not supported for P2P uses",
4131 if (wpas_p2p_init_go_params(wpa_s
, ¶ms
, freq
, ht40
, NULL
))
4134 !p2p_supported_freq(wpa_s
->global
->p2p
, params
.freq
)) {
4135 wpa_printf(MSG_DEBUG
, "P2P: The selected channel for GO "
4136 "(%u MHz) is not supported for P2P uses",
4140 p2p_go_params(wpa_s
->global
->p2p
, ¶ms
);
4141 params
.persistent_group
= persistent_group
;
4143 wpa_s
= wpas_p2p_get_group_iface(wpa_s
, 0, 1);
4146 wpas_start_wps_go(wpa_s
, ¶ms
, 0);
4152 static int wpas_start_p2p_client(struct wpa_supplicant
*wpa_s
,
4153 struct wpa_ssid
*params
, int addr_allocated
)
4155 struct wpa_ssid
*ssid
;
4157 wpa_s
= wpas_p2p_get_group_iface(wpa_s
, addr_allocated
, 0);
4161 wpa_supplicant_ap_deinit(wpa_s
);
4163 ssid
= wpa_config_add_network(wpa_s
->conf
);
4166 wpa_config_set_network_defaults(ssid
);
4167 ssid
->temporary
= 1;
4168 ssid
->proto
= WPA_PROTO_RSN
;
4169 ssid
->pairwise_cipher
= WPA_CIPHER_CCMP
;
4170 ssid
->group_cipher
= WPA_CIPHER_CCMP
;
4171 ssid
->key_mgmt
= WPA_KEY_MGMT_PSK
;
4172 ssid
->ssid
= os_malloc(params
->ssid_len
);
4173 if (ssid
->ssid
== NULL
) {
4174 wpa_config_remove_network(wpa_s
->conf
, ssid
->id
);
4177 os_memcpy(ssid
->ssid
, params
->ssid
, params
->ssid_len
);
4178 ssid
->ssid_len
= params
->ssid_len
;
4179 ssid
->p2p_group
= 1;
4180 ssid
->export_keys
= 1;
4181 if (params
->psk_set
) {
4182 os_memcpy(ssid
->psk
, params
->psk
, 32);
4185 if (params
->passphrase
)
4186 ssid
->passphrase
= os_strdup(params
->passphrase
);
4188 wpa_supplicant_select_network(wpa_s
, ssid
);
4190 wpa_s
->show_group_started
= 1;
4196 int wpas_p2p_group_add_persistent(struct wpa_supplicant
*wpa_s
,
4197 struct wpa_ssid
*ssid
, int addr_allocated
,
4199 const struct p2p_channels
*channels
)
4201 struct p2p_go_neg_results params
;
4204 if (ssid
->disabled
!= 2 || ssid
->ssid
== NULL
)
4207 if (wpas_get_p2p_group(wpa_s
, ssid
->ssid
, ssid
->ssid_len
, &go
) &&
4208 go
== (ssid
->mode
== WPAS_MODE_P2P_GO
)) {
4209 wpa_printf(MSG_DEBUG
, "P2P: Requested persistent group is "
4214 /* Make sure we are not running find during connection establishment */
4215 wpas_p2p_stop_find_oper(wpa_s
);
4217 wpa_s
->p2p_fallback_to_go_neg
= 0;
4219 if (ssid
->mode
== WPAS_MODE_INFRA
)
4220 return wpas_start_p2p_client(wpa_s
, ssid
, addr_allocated
);
4222 if (ssid
->mode
!= WPAS_MODE_P2P_GO
)
4225 if (wpas_p2p_init_go_params(wpa_s
, ¶ms
, freq
, ht40
, channels
))
4229 params
.psk_set
= ssid
->psk_set
;
4231 os_memcpy(params
.psk
, ssid
->psk
, sizeof(params
.psk
));
4232 if (ssid
->passphrase
) {
4233 if (os_strlen(ssid
->passphrase
) >= sizeof(params
.passphrase
)) {
4234 wpa_printf(MSG_ERROR
, "P2P: Invalid passphrase in "
4235 "persistent group");
4238 os_strlcpy(params
.passphrase
, ssid
->passphrase
,
4239 sizeof(params
.passphrase
));
4241 os_memcpy(params
.ssid
, ssid
->ssid
, ssid
->ssid_len
);
4242 params
.ssid_len
= ssid
->ssid_len
;
4243 params
.persistent_group
= 1;
4245 wpa_s
= wpas_p2p_get_group_iface(wpa_s
, addr_allocated
, 1);
4249 wpas_start_wps_go(wpa_s
, ¶ms
, 0);
4255 static void wpas_p2p_ie_update(void *ctx
, struct wpabuf
*beacon_ies
,
4256 struct wpabuf
*proberesp_ies
)
4258 struct wpa_supplicant
*wpa_s
= ctx
;
4259 if (wpa_s
->ap_iface
) {
4260 struct hostapd_data
*hapd
= wpa_s
->ap_iface
->bss
[0];
4261 if (!(hapd
->conf
->p2p
& P2P_GROUP_OWNER
)) {
4262 wpabuf_free(beacon_ies
);
4263 wpabuf_free(proberesp_ies
);
4267 wpabuf_free(hapd
->p2p_beacon_ie
);
4268 hapd
->p2p_beacon_ie
= beacon_ies
;
4270 wpabuf_free(hapd
->p2p_probe_resp_ie
);
4271 hapd
->p2p_probe_resp_ie
= proberesp_ies
;
4273 wpabuf_free(beacon_ies
);
4274 wpabuf_free(proberesp_ies
);
4276 wpa_supplicant_ap_update_beacon(wpa_s
);
4280 static void wpas_p2p_idle_update(void *ctx
, int idle
)
4282 struct wpa_supplicant
*wpa_s
= ctx
;
4283 if (!wpa_s
->ap_iface
)
4285 wpa_printf(MSG_DEBUG
, "P2P: GO - group %sidle", idle
? "" : "not ");
4287 wpas_p2p_set_group_idle_timeout(wpa_s
);
4289 eloop_cancel_timeout(wpas_p2p_group_idle_timeout
, wpa_s
, NULL
);
4293 struct p2p_group
* wpas_p2p_group_init(struct wpa_supplicant
*wpa_s
,
4294 struct wpa_ssid
*ssid
)
4296 struct p2p_group
*group
;
4297 struct p2p_group_config
*cfg
;
4299 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
4301 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
4304 cfg
= os_zalloc(sizeof(*cfg
));
4308 if (ssid
->p2p_persistent_group
&& wpa_s
->conf
->persistent_reconnect
)
4309 cfg
->persistent_group
= 2;
4310 else if (ssid
->p2p_persistent_group
)
4311 cfg
->persistent_group
= 1;
4312 os_memcpy(cfg
->interface_addr
, wpa_s
->own_addr
, ETH_ALEN
);
4313 if (wpa_s
->max_stations
&&
4314 wpa_s
->max_stations
< wpa_s
->conf
->max_num_sta
)
4315 cfg
->max_clients
= wpa_s
->max_stations
;
4317 cfg
->max_clients
= wpa_s
->conf
->max_num_sta
;
4318 os_memcpy(cfg
->ssid
, ssid
->ssid
, ssid
->ssid_len
);
4319 cfg
->ssid_len
= ssid
->ssid_len
;
4320 cfg
->cb_ctx
= wpa_s
;
4321 cfg
->ie_update
= wpas_p2p_ie_update
;
4322 cfg
->idle_update
= wpas_p2p_idle_update
;
4324 group
= p2p_group_init(wpa_s
->global
->p2p
, cfg
);
4327 if (ssid
->mode
!= WPAS_MODE_P2P_GROUP_FORMATION
)
4328 p2p_group_notif_formation_done(group
);
4329 wpa_s
->p2p_group
= group
;
4334 void wpas_p2p_wps_success(struct wpa_supplicant
*wpa_s
, const u8
*peer_addr
,
4337 struct wpa_ssid
*ssid
= wpa_s
->current_ssid
;
4339 if (!wpa_s
->p2p_in_provisioning
) {
4340 wpa_printf(MSG_DEBUG
, "P2P: Ignore WPS success event - P2P "
4341 "provisioning not in progress");
4345 if (ssid
&& ssid
->mode
== WPAS_MODE_INFRA
) {
4346 u8 go_dev_addr
[ETH_ALEN
];
4347 os_memcpy(go_dev_addr
, wpa_s
->bssid
, ETH_ALEN
);
4348 wpas_p2p_persistent_group(wpa_s
, go_dev_addr
, ssid
->ssid
,
4350 /* Clear any stored provisioning info */
4351 p2p_clear_provisioning_info(wpa_s
->global
->p2p
, go_dev_addr
);
4354 eloop_cancel_timeout(wpas_p2p_group_formation_timeout
, wpa_s
->parent
,
4356 if (ssid
&& ssid
->mode
== WPAS_MODE_INFRA
) {
4358 * Use a separate timeout for initial data connection to
4359 * complete to allow the group to be removed automatically if
4360 * something goes wrong in this step before the P2P group idle
4361 * timeout mechanism is taken into use.
4363 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT
, 0,
4364 wpas_p2p_group_formation_timeout
,
4365 wpa_s
->parent
, NULL
);
4367 if (wpa_s
->global
->p2p
)
4368 p2p_wps_success_cb(wpa_s
->global
->p2p
, peer_addr
);
4369 else if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
4370 wpa_drv_wps_success_cb(wpa_s
, peer_addr
);
4371 wpas_group_formation_completed(wpa_s
, 1);
4375 void wpas_p2p_wps_failed(struct wpa_supplicant
*wpa_s
,
4376 struct wps_event_fail
*fail
)
4378 if (!wpa_s
->p2p_in_provisioning
) {
4379 wpa_printf(MSG_DEBUG
, "P2P: Ignore WPS fail event - P2P "
4380 "provisioning not in progress");
4384 if (wpa_s
->go_params
) {
4385 p2p_clear_provisioning_info(
4387 wpa_s
->go_params
->peer_device_addr
);
4390 wpas_notify_p2p_wps_failed(wpa_s
, fail
);
4394 int wpas_p2p_prov_disc(struct wpa_supplicant
*wpa_s
, const u8
*peer_addr
,
4395 const char *config_method
,
4396 enum wpas_p2p_prov_disc_use use
)
4400 wpa_s
->p2p_fallback_to_go_neg
= 0;
4401 wpa_s
->pending_pd_use
= NORMAL_PD
;
4402 if (os_strncmp(config_method
, "display", 7) == 0)
4403 config_methods
= WPS_CONFIG_DISPLAY
;
4404 else if (os_strncmp(config_method
, "keypad", 6) == 0)
4405 config_methods
= WPS_CONFIG_KEYPAD
;
4406 else if (os_strncmp(config_method
, "pbc", 3) == 0 ||
4407 os_strncmp(config_method
, "pushbutton", 10) == 0)
4408 config_methods
= WPS_CONFIG_PUSHBUTTON
;
4410 wpa_printf(MSG_DEBUG
, "P2P: Unknown config method");
4414 if (use
== WPAS_P2P_PD_AUTO
) {
4415 os_memcpy(wpa_s
->pending_join_dev_addr
, peer_addr
, ETH_ALEN
);
4416 wpa_s
->pending_pd_config_methods
= config_methods
;
4417 wpa_s
->p2p_auto_pd
= 1;
4418 wpa_s
->p2p_auto_join
= 0;
4419 wpa_s
->pending_pd_before_join
= 0;
4420 wpa_s
->auto_pd_scan_retry
= 0;
4421 wpas_p2p_stop_find(wpa_s
);
4422 wpa_s
->p2p_join_scan_count
= 0;
4423 os_get_time(&wpa_s
->p2p_auto_started
);
4424 wpa_printf(MSG_DEBUG
, "P2P: Auto PD started at %ld.%06ld",
4425 wpa_s
->p2p_auto_started
.sec
,
4426 wpa_s
->p2p_auto_started
.usec
);
4427 wpas_p2p_join_scan(wpa_s
, NULL
);
4431 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
) {
4432 return wpa_drv_p2p_prov_disc_req(wpa_s
, peer_addr
,
4434 use
== WPAS_P2P_PD_FOR_JOIN
);
4437 if (wpa_s
->global
->p2p
== NULL
|| wpa_s
->global
->p2p_disabled
)
4440 return p2p_prov_disc_req(wpa_s
->global
->p2p
, peer_addr
,
4441 config_methods
, use
== WPAS_P2P_PD_FOR_JOIN
,
4446 int wpas_p2p_scan_result_text(const u8
*ies
, size_t ies_len
, char *buf
,
4449 return p2p_scan_result_text(ies
, ies_len
, buf
, end
);
4453 static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant
*wpa_s
)
4455 if (!offchannel_pending_action_tx(wpa_s
))
4458 wpa_printf(MSG_DEBUG
, "P2P: Drop pending Action TX due to new "
4459 "operation request");
4460 offchannel_clear_pending_action_tx(wpa_s
);
4464 int wpas_p2p_find(struct wpa_supplicant
*wpa_s
, unsigned int timeout
,
4465 enum p2p_discovery_type type
,
4466 unsigned int num_req_dev_types
, const u8
*req_dev_types
,
4467 const u8
*dev_id
, unsigned int search_delay
)
4469 wpas_p2p_clear_pending_action_tx(wpa_s
);
4470 wpa_s
->p2p_long_listen
= 0;
4472 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
4473 return wpa_drv_p2p_find(wpa_s
, timeout
, type
);
4475 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
||
4476 wpa_s
->p2p_in_provisioning
)
4479 wpa_supplicant_cancel_sched_scan(wpa_s
);
4481 return p2p_find(wpa_s
->global
->p2p
, timeout
, type
,
4482 num_req_dev_types
, req_dev_types
, dev_id
,
4487 static int wpas_p2p_stop_find_oper(struct wpa_supplicant
*wpa_s
)
4489 wpas_p2p_clear_pending_action_tx(wpa_s
);
4490 wpa_s
->p2p_long_listen
= 0;
4491 eloop_cancel_timeout(wpas_p2p_long_listen_timeout
, wpa_s
, NULL
);
4492 eloop_cancel_timeout(wpas_p2p_join_scan
, wpa_s
, NULL
);
4493 wpa_s
->global
->p2p_cb_on_scan_complete
= 0;
4495 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
) {
4496 wpa_drv_p2p_stop_find(wpa_s
);
4500 if (wpa_s
->global
->p2p
)
4501 p2p_stop_find(wpa_s
->global
->p2p
);
4507 void wpas_p2p_stop_find(struct wpa_supplicant
*wpa_s
)
4509 if (wpas_p2p_stop_find_oper(wpa_s
) > 0)
4511 wpas_p2p_remove_pending_group_interface(wpa_s
);
4515 static void wpas_p2p_long_listen_timeout(void *eloop_ctx
, void *timeout_ctx
)
4517 struct wpa_supplicant
*wpa_s
= eloop_ctx
;
4518 wpa_s
->p2p_long_listen
= 0;
4522 int wpas_p2p_listen(struct wpa_supplicant
*wpa_s
, unsigned int timeout
)
4526 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
4529 wpa_supplicant_cancel_sched_scan(wpa_s
);
4530 wpas_p2p_clear_pending_action_tx(wpa_s
);
4534 * This is a request for unlimited Listen state. However, at
4535 * least for now, this is mapped to a Listen state for one
4540 eloop_cancel_timeout(wpas_p2p_long_listen_timeout
, wpa_s
, NULL
);
4541 wpa_s
->p2p_long_listen
= 0;
4544 * Stop previous find/listen operation to avoid trying to request a new
4545 * remain-on-channel operation while the driver is still running the
4548 if (wpa_s
->global
->p2p
)
4549 p2p_stop_find(wpa_s
->global
->p2p
);
4551 res
= wpas_p2p_listen_start(wpa_s
, timeout
* 1000);
4552 if (res
== 0 && timeout
* 1000 > wpa_s
->max_remain_on_chan
) {
4553 wpa_s
->p2p_long_listen
= timeout
* 1000;
4554 eloop_register_timeout(timeout
, 0,
4555 wpas_p2p_long_listen_timeout
,
4563 int wpas_p2p_assoc_req_ie(struct wpa_supplicant
*wpa_s
, struct wpa_bss
*bss
,
4564 u8
*buf
, size_t len
, int p2p_group
)
4566 struct wpabuf
*p2p_ie
;
4569 if (wpa_s
->global
->p2p_disabled
)
4571 if (wpa_s
->global
->p2p
== NULL
)
4576 p2p_ie
= wpa_bss_get_vendor_ie_multi(bss
, P2P_IE_VENDOR_TYPE
);
4577 ret
= p2p_assoc_req_ie(wpa_s
->global
->p2p
, bss
->bssid
, buf
, len
,
4579 wpabuf_free(p2p_ie
);
4585 int wpas_p2p_probe_req_rx(struct wpa_supplicant
*wpa_s
, const u8
*addr
,
4586 const u8
*dst
, const u8
*bssid
,
4587 const u8
*ie
, size_t ie_len
, int ssi_signal
)
4589 if (wpa_s
->global
->p2p_disabled
)
4591 if (wpa_s
->global
->p2p
== NULL
)
4594 switch (p2p_probe_req_rx(wpa_s
->global
->p2p
, addr
, dst
, bssid
,
4596 case P2P_PREQ_NOT_P2P
:
4597 wpas_notify_preq(wpa_s
, addr
, dst
, bssid
, ie
, ie_len
,
4600 case P2P_PREQ_MALFORMED
:
4601 case P2P_PREQ_NOT_LISTEN
:
4602 case P2P_PREQ_NOT_PROCESSED
:
4603 default: /* make gcc happy */
4605 case P2P_PREQ_PROCESSED
:
4611 void wpas_p2p_rx_action(struct wpa_supplicant
*wpa_s
, const u8
*da
,
4612 const u8
*sa
, const u8
*bssid
,
4613 u8 category
, const u8
*data
, size_t len
, int freq
)
4615 if (wpa_s
->global
->p2p_disabled
)
4617 if (wpa_s
->global
->p2p
== NULL
)
4620 p2p_rx_action(wpa_s
->global
->p2p
, da
, sa
, bssid
, category
, data
, len
,
4625 void wpas_p2p_scan_ie(struct wpa_supplicant
*wpa_s
, struct wpabuf
*ies
)
4627 if (wpa_s
->global
->p2p_disabled
)
4629 if (wpa_s
->global
->p2p
== NULL
)
4632 p2p_scan_ie(wpa_s
->global
->p2p
, ies
, NULL
);
4636 void wpas_p2p_group_deinit(struct wpa_supplicant
*wpa_s
)
4638 p2p_group_deinit(wpa_s
->p2p_group
);
4639 wpa_s
->p2p_group
= NULL
;
4641 wpa_s
->ap_configured_cb
= NULL
;
4642 wpa_s
->ap_configured_cb_ctx
= NULL
;
4643 wpa_s
->ap_configured_cb_data
= NULL
;
4644 wpa_s
->connect_without_scan
= NULL
;
4648 int wpas_p2p_reject(struct wpa_supplicant
*wpa_s
, const u8
*addr
)
4650 wpa_s
->p2p_long_listen
= 0;
4652 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
4653 return wpa_drv_p2p_reject(wpa_s
, addr
);
4655 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
4658 return p2p_reject(wpa_s
->global
->p2p
, addr
);
4662 /* Invite to reinvoke a persistent group */
4663 int wpas_p2p_invite(struct wpa_supplicant
*wpa_s
, const u8
*peer_addr
,
4664 struct wpa_ssid
*ssid
, const u8
*go_dev_addr
, int freq
,
4665 int ht40
, int pref_freq
)
4667 enum p2p_invite_role role
;
4668 u8
*bssid
= NULL
, bssid_buf
[ETH_ALEN
];
4669 int force_freq
= 0, oper_freq
= 0;
4673 os_memcpy(wpa_s
->p2p_auth_invite
, peer_addr
, ETH_ALEN
);
4675 os_memset(wpa_s
->p2p_auth_invite
, 0, ETH_ALEN
);
4677 wpa_s
->p2p_persistent_go_freq
= freq
;
4678 wpa_s
->p2p_go_ht40
= !!ht40
;
4679 if (ssid
->mode
== WPAS_MODE_P2P_GO
) {
4680 role
= P2P_INVITE_ROLE_GO
;
4681 if (peer_addr
== NULL
) {
4682 wpa_printf(MSG_DEBUG
, "P2P: Missing peer "
4683 "address in invitation command");
4686 if (wpas_p2p_create_iface(wpa_s
)) {
4687 if (wpas_p2p_add_group_interface(wpa_s
,
4688 WPA_IF_P2P_GO
) < 0) {
4689 wpa_printf(MSG_ERROR
, "P2P: Failed to "
4690 "allocate a new interface for the "
4694 bssid
= wpa_s
->pending_interface_addr
;
4696 bssid
= wpa_s
->own_addr
;
4698 role
= P2P_INVITE_ROLE_CLIENT
;
4699 peer_addr
= ssid
->bssid
;
4701 wpa_s
->pending_invite_ssid_id
= ssid
->id
;
4703 if (wpa_s
->current_ssid
&& wpa_drv_get_bssid(wpa_s
, bssid_buf
) == 0 &&
4704 wpa_s
->assoc_freq
) {
4705 oper_freq
= wpa_s
->assoc_freq
;
4709 oper_freq
= wpa_drv_shared_freq(wpa_s
);
4714 res
= wpas_p2p_setup_freqs(wpa_s
, freq
, &force_freq
, &pref_freq
,
4719 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
4720 return wpa_drv_p2p_invite(wpa_s
, peer_addr
, role
, bssid
,
4721 ssid
->ssid
, ssid
->ssid_len
,
4724 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
4727 return p2p_invite(wpa_s
->global
->p2p
, peer_addr
, role
, bssid
,
4728 ssid
->ssid
, ssid
->ssid_len
, force_freq
, go_dev_addr
,
4733 /* Invite to join an active group */
4734 int wpas_p2p_invite_group(struct wpa_supplicant
*wpa_s
, const char *ifname
,
4735 const u8
*peer_addr
, const u8
*go_dev_addr
)
4737 struct wpa_global
*global
= wpa_s
->global
;
4738 enum p2p_invite_role role
;
4739 u8
*bssid
= NULL
, bssid_buf
[ETH_ALEN
];
4740 struct wpa_ssid
*ssid
;
4742 int force_freq
= 0, oper_freq
= 0, pref_freq
= 0;
4745 wpa_s
->p2p_persistent_go_freq
= 0;
4746 wpa_s
->p2p_go_ht40
= 0;
4748 for (wpa_s
= global
->ifaces
; wpa_s
; wpa_s
= wpa_s
->next
) {
4749 if (os_strcmp(wpa_s
->ifname
, ifname
) == 0)
4752 if (wpa_s
== NULL
) {
4753 wpa_printf(MSG_DEBUG
, "P2P: Interface '%s' not found", ifname
);
4757 ssid
= wpa_s
->current_ssid
;
4759 wpa_printf(MSG_DEBUG
, "P2P: No current SSID to use for "
4764 persistent
= ssid
->p2p_persistent_group
&&
4765 wpas_p2p_get_persistent(wpa_s
->parent
, peer_addr
,
4766 ssid
->ssid
, ssid
->ssid_len
);
4768 if (ssid
->mode
== WPAS_MODE_P2P_GO
) {
4769 role
= P2P_INVITE_ROLE_ACTIVE_GO
;
4770 bssid
= wpa_s
->own_addr
;
4771 if (go_dev_addr
== NULL
)
4772 go_dev_addr
= wpa_s
->global
->p2p_dev_addr
;
4774 role
= P2P_INVITE_ROLE_CLIENT
;
4775 if (wpa_s
->wpa_state
< WPA_ASSOCIATED
) {
4776 wpa_printf(MSG_DEBUG
, "P2P: Not associated - cannot "
4777 "invite to current group");
4780 bssid
= wpa_s
->bssid
;
4781 if (go_dev_addr
== NULL
&&
4782 !is_zero_ether_addr(wpa_s
->go_dev_addr
))
4783 go_dev_addr
= wpa_s
->go_dev_addr
;
4785 wpa_s
->parent
->pending_invite_ssid_id
= -1;
4787 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
4788 return wpa_drv_p2p_invite(wpa_s
, peer_addr
, role
, bssid
,
4789 ssid
->ssid
, ssid
->ssid_len
,
4790 go_dev_addr
, persistent
);
4792 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
4795 if (wpa_s
->current_ssid
&& wpa_drv_get_bssid(wpa_s
, bssid_buf
) == 0 &&
4796 wpa_s
->assoc_freq
) {
4797 oper_freq
= wpa_s
->assoc_freq
;
4801 oper_freq
= wpa_drv_shared_freq(wpa_s
);
4806 res
= wpas_p2p_setup_freqs(wpa_s
, 0, &force_freq
, &pref_freq
,
4811 return p2p_invite(wpa_s
->global
->p2p
, peer_addr
, role
, bssid
,
4812 ssid
->ssid
, ssid
->ssid_len
, force_freq
,
4813 go_dev_addr
, persistent
, pref_freq
);
4817 void wpas_p2p_completed(struct wpa_supplicant
*wpa_s
)
4819 struct wpa_ssid
*ssid
= wpa_s
->current_ssid
;
4820 const char *ssid_txt
;
4821 u8 go_dev_addr
[ETH_ALEN
];
4822 int network_id
= -1;
4826 if (ssid
== NULL
|| ssid
->mode
!= WPAS_MODE_P2P_GROUP_FORMATION
) {
4827 eloop_cancel_timeout(wpas_p2p_group_formation_timeout
,
4828 wpa_s
->parent
, NULL
);
4831 if (!wpa_s
->show_group_started
|| !ssid
)
4834 wpa_s
->show_group_started
= 0;
4836 ssid_txt
= wpa_ssid_txt(ssid
->ssid
, ssid
->ssid_len
);
4837 os_memset(go_dev_addr
, 0, ETH_ALEN
);
4838 if (ssid
->bssid_set
)
4839 os_memcpy(go_dev_addr
, ssid
->bssid
, ETH_ALEN
);
4840 persistent
= wpas_p2p_persistent_group(wpa_s
, go_dev_addr
, ssid
->ssid
,
4842 os_memcpy(wpa_s
->go_dev_addr
, go_dev_addr
, ETH_ALEN
);
4844 if (wpa_s
->global
->p2p_group_formation
== wpa_s
)
4845 wpa_s
->global
->p2p_group_formation
= NULL
;
4847 freq
= wpa_s
->current_bss
? wpa_s
->current_bss
->freq
:
4848 (int) wpa_s
->assoc_freq
;
4849 if (ssid
->passphrase
== NULL
&& ssid
->psk_set
) {
4851 wpa_snprintf_hex(psk
, sizeof(psk
), ssid
->psk
, 32);
4852 wpa_msg(wpa_s
->parent
, MSG_INFO
, P2P_EVENT_GROUP_STARTED
4853 "%s client ssid=\"%s\" freq=%d psk=%s go_dev_addr="
4855 wpa_s
->ifname
, ssid_txt
, freq
, psk
,
4856 MAC2STR(go_dev_addr
),
4857 persistent
? " [PERSISTENT]" : "");
4859 wpa_msg(wpa_s
->parent
, MSG_INFO
, P2P_EVENT_GROUP_STARTED
4860 "%s client ssid=\"%s\" freq=%d passphrase=\"%s\" "
4861 "go_dev_addr=" MACSTR
"%s",
4862 wpa_s
->ifname
, ssid_txt
, freq
,
4863 ssid
->passphrase
? ssid
->passphrase
: "",
4864 MAC2STR(go_dev_addr
),
4865 persistent
? " [PERSISTENT]" : "");
4869 network_id
= wpas_p2p_store_persistent_group(wpa_s
->parent
,
4872 network_id
= ssid
->id
;
4873 wpas_notify_p2p_group_started(wpa_s
, ssid
, network_id
, 1);
4876 if (wpa_s
->global
->p2p_cb_on_scan_complete
&& !wpa_s
->global
->p2p_disabled
&&
4877 wpa_s
->global
->p2p
!= NULL
) {
4878 wpa_s
->global
->p2p_cb_on_scan_complete
= 0;
4879 if (p2p_other_scan_completed(wpa_s
->global
->p2p
) == 1) {
4880 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: Pending P2P operation "
4881 "continued after successful connection");
4882 p2p_increase_search_delay(
4884 wpas_p2p_search_delay(wpa_s
));
4890 int wpas_p2p_presence_req(struct wpa_supplicant
*wpa_s
, u32 duration1
,
4891 u32 interval1
, u32 duration2
, u32 interval2
)
4893 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
4895 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
4898 if (wpa_s
->wpa_state
< WPA_ASSOCIATED
||
4899 wpa_s
->current_ssid
== NULL
||
4900 wpa_s
->current_ssid
->mode
!= WPAS_MODE_INFRA
)
4903 return p2p_presence_req(wpa_s
->global
->p2p
, wpa_s
->bssid
,
4904 wpa_s
->own_addr
, wpa_s
->assoc_freq
,
4905 duration1
, interval1
, duration2
, interval2
);
4909 int wpas_p2p_ext_listen(struct wpa_supplicant
*wpa_s
, unsigned int period
,
4910 unsigned int interval
)
4912 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
4915 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
4918 return p2p_ext_listen(wpa_s
->global
->p2p
, period
, interval
);
4922 static int wpas_p2p_is_client(struct wpa_supplicant
*wpa_s
)
4924 if (wpa_s
->current_ssid
== NULL
) {
4926 * current_ssid can be cleared when P2P client interface gets
4927 * disconnected, so assume this interface was used as P2P
4932 return wpa_s
->current_ssid
->p2p_group
&&
4933 wpa_s
->current_ssid
->mode
== WPAS_MODE_INFRA
;
4937 static void wpas_p2p_group_idle_timeout(void *eloop_ctx
, void *timeout_ctx
)
4939 struct wpa_supplicant
*wpa_s
= eloop_ctx
;
4941 if (wpa_s
->conf
->p2p_group_idle
== 0 && !wpas_p2p_is_client(wpa_s
)) {
4942 wpa_printf(MSG_DEBUG
, "P2P: Ignore group idle timeout - "
4947 wpa_printf(MSG_DEBUG
, "P2P: Group idle timeout reached - terminate "
4949 wpas_p2p_group_delete(wpa_s
, P2P_GROUP_REMOVAL_IDLE_TIMEOUT
);
4953 static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant
*wpa_s
)
4957 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout
, wpa_s
, NULL
) > 0)
4958 wpa_printf(MSG_DEBUG
, "P2P: Cancelled P2P group idle timeout");
4960 if (wpa_s
->current_ssid
== NULL
|| !wpa_s
->current_ssid
->p2p_group
)
4963 timeout
= wpa_s
->conf
->p2p_group_idle
;
4964 if (wpa_s
->current_ssid
->mode
== WPAS_MODE_INFRA
&&
4965 (timeout
== 0 || timeout
> P2P_MAX_CLIENT_IDLE
))
4966 timeout
= P2P_MAX_CLIENT_IDLE
;
4972 if (wpa_s
->current_ssid
->mode
== WPAS_MODE_INFRA
)
4973 timeout
= 0; /* special client mode no-timeout */
4978 if (wpa_s
->p2p_in_provisioning
) {
4980 * Use the normal group formation timeout during the
4981 * provisioning phase to avoid terminating this process too
4982 * early due to group idle timeout.
4984 wpa_printf(MSG_DEBUG
, "P2P: Do not use P2P group idle timeout "
4985 "during provisioning");
4989 if (wpa_s
->show_group_started
) {
4991 * Use the normal group formation timeout between the end of
4992 * the provisioning phase and completion of 4-way handshake to
4993 * avoid terminating this process too early due to group idle
4996 wpa_printf(MSG_DEBUG
, "P2P: Do not use P2P group idle timeout "
4997 "while waiting for initial 4-way handshake to "
5002 wpa_printf(MSG_DEBUG
, "P2P: Set P2P group idle timeout to %u seconds",
5004 eloop_register_timeout(timeout
, 0, wpas_p2p_group_idle_timeout
,
5009 /* Returns 1 if the interface was removed */
5010 int wpas_p2p_deauth_notif(struct wpa_supplicant
*wpa_s
, const u8
*bssid
,
5011 u16 reason_code
, const u8
*ie
, size_t ie_len
,
5012 int locally_generated
)
5014 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
5016 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
5019 if (!locally_generated
)
5020 p2p_deauth_notif(wpa_s
->global
->p2p
, bssid
, reason_code
, ie
,
5023 if (reason_code
== WLAN_REASON_DEAUTH_LEAVING
&& !locally_generated
&&
5024 wpa_s
->current_ssid
&&
5025 wpa_s
->current_ssid
->p2p_group
&&
5026 wpa_s
->current_ssid
->mode
== WPAS_MODE_INFRA
) {
5027 wpa_printf(MSG_DEBUG
, "P2P: GO indicated that the P2P Group "
5028 "session is ending");
5029 if (wpas_p2p_group_delete(wpa_s
,
5030 P2P_GROUP_REMOVAL_GO_ENDING_SESSION
)
5039 void wpas_p2p_disassoc_notif(struct wpa_supplicant
*wpa_s
, const u8
*bssid
,
5040 u16 reason_code
, const u8
*ie
, size_t ie_len
,
5041 int locally_generated
)
5043 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
5045 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
5048 if (!locally_generated
)
5049 p2p_disassoc_notif(wpa_s
->global
->p2p
, bssid
, reason_code
, ie
,
5054 void wpas_p2p_update_config(struct wpa_supplicant
*wpa_s
)
5056 struct p2p_data
*p2p
= wpa_s
->global
->p2p
;
5061 if (!(wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_CAPABLE
))
5064 if (wpa_s
->conf
->changed_parameters
& CFG_CHANGED_DEVICE_NAME
)
5065 p2p_set_dev_name(p2p
, wpa_s
->conf
->device_name
);
5067 if (wpa_s
->conf
->changed_parameters
& CFG_CHANGED_DEVICE_TYPE
)
5068 p2p_set_pri_dev_type(p2p
, wpa_s
->conf
->device_type
);
5071 (wpa_s
->conf
->changed_parameters
& CFG_CHANGED_CONFIG_METHODS
))
5072 p2p_set_config_methods(p2p
, wpa_s
->wps
->config_methods
);
5074 if (wpa_s
->wps
&& (wpa_s
->conf
->changed_parameters
& CFG_CHANGED_UUID
))
5075 p2p_set_uuid(p2p
, wpa_s
->wps
->uuid
);
5077 if (wpa_s
->conf
->changed_parameters
& CFG_CHANGED_WPS_STRING
) {
5078 p2p_set_manufacturer(p2p
, wpa_s
->conf
->manufacturer
);
5079 p2p_set_model_name(p2p
, wpa_s
->conf
->model_name
);
5080 p2p_set_model_number(p2p
, wpa_s
->conf
->model_number
);
5081 p2p_set_serial_number(p2p
, wpa_s
->conf
->serial_number
);
5084 if (wpa_s
->conf
->changed_parameters
& CFG_CHANGED_SEC_DEVICE_TYPE
)
5085 p2p_set_sec_dev_types(p2p
,
5086 (void *) wpa_s
->conf
->sec_device_type
,
5087 wpa_s
->conf
->num_sec_device_types
);
5089 if (wpa_s
->conf
->changed_parameters
& CFG_CHANGED_VENDOR_EXTENSION
) {
5091 p2p_remove_wps_vendor_extensions(p2p
);
5092 for (i
= 0; i
< MAX_WPS_VENDOR_EXT
; i
++) {
5093 if (wpa_s
->conf
->wps_vendor_ext
[i
] == NULL
)
5095 p2p_add_wps_vendor_extension(
5096 p2p
, wpa_s
->conf
->wps_vendor_ext
[i
]);
5100 if ((wpa_s
->conf
->changed_parameters
& CFG_CHANGED_COUNTRY
) &&
5101 wpa_s
->conf
->country
[0] && wpa_s
->conf
->country
[1]) {
5103 country
[0] = wpa_s
->conf
->country
[0];
5104 country
[1] = wpa_s
->conf
->country
[1];
5106 p2p_set_country(p2p
, country
);
5109 if (wpa_s
->conf
->changed_parameters
& CFG_CHANGED_P2P_SSID_POSTFIX
) {
5110 p2p_set_ssid_postfix(p2p
, (u8
*) wpa_s
->conf
->p2p_ssid_postfix
,
5111 wpa_s
->conf
->p2p_ssid_postfix
?
5112 os_strlen(wpa_s
->conf
->p2p_ssid_postfix
) :
5116 if (wpa_s
->conf
->changed_parameters
& CFG_CHANGED_P2P_INTRA_BSS
)
5117 p2p_set_intra_bss_dist(p2p
, wpa_s
->conf
->p2p_intra_bss
);
5119 if (wpa_s
->conf
->changed_parameters
& CFG_CHANGED_P2P_LISTEN_CHANNEL
) {
5120 u8 reg_class
, channel
;
5123 if (wpa_s
->conf
->p2p_listen_reg_class
&&
5124 wpa_s
->conf
->p2p_listen_channel
) {
5125 reg_class
= wpa_s
->conf
->p2p_listen_reg_class
;
5126 channel
= wpa_s
->conf
->p2p_listen_channel
;
5130 * Pick one of the social channels randomly as the
5133 os_get_random((u8
*) &r
, sizeof(r
));
5134 channel
= 1 + (r
% 3) * 5;
5136 ret
= p2p_set_listen_channel(p2p
, reg_class
, channel
);
5138 wpa_printf(MSG_ERROR
, "P2P: Own listen channel update "
5141 if (wpa_s
->conf
->changed_parameters
& CFG_CHANGED_P2P_OPER_CHANNEL
) {
5142 u8 op_reg_class
, op_channel
, cfg_op_channel
;
5145 if (wpa_s
->conf
->p2p_oper_reg_class
&&
5146 wpa_s
->conf
->p2p_oper_channel
) {
5147 op_reg_class
= wpa_s
->conf
->p2p_oper_reg_class
;
5148 op_channel
= wpa_s
->conf
->p2p_oper_channel
;
5153 * Use random operation channel from (1, 6, 11)
5154 *if no other preference is indicated.
5156 os_get_random((u8
*) &r
, sizeof(r
));
5157 op_channel
= 1 + (r
% 3) * 5;
5160 ret
= p2p_set_oper_channel(p2p
, op_reg_class
, op_channel
,
5163 wpa_printf(MSG_ERROR
, "P2P: Own oper channel update "
5167 if (wpa_s
->conf
->changed_parameters
& CFG_CHANGED_P2P_PREF_CHAN
) {
5168 if (p2p_set_pref_chan(p2p
, wpa_s
->conf
->num_p2p_pref_chan
,
5169 wpa_s
->conf
->p2p_pref_chan
) < 0) {
5170 wpa_printf(MSG_ERROR
, "P2P: Preferred channel list "
5177 int wpas_p2p_set_noa(struct wpa_supplicant
*wpa_s
, u8 count
, int start
,
5180 if (!wpa_s
->ap_iface
)
5182 return hostapd_p2p_set_noa(wpa_s
->ap_iface
->bss
[0], count
, start
,
5187 int wpas_p2p_set_cross_connect(struct wpa_supplicant
*wpa_s
, int enabled
)
5189 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
5191 if (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
)
5194 wpa_s
->global
->cross_connection
= enabled
;
5195 p2p_set_cross_connect(wpa_s
->global
->p2p
, enabled
);
5198 struct wpa_supplicant
*iface
;
5200 for (iface
= wpa_s
->global
->ifaces
; iface
; iface
= iface
->next
)
5202 if (iface
->cross_connect_enabled
== 0)
5205 iface
->cross_connect_enabled
= 0;
5206 iface
->cross_connect_in_use
= 0;
5207 wpa_msg(iface
->parent
, MSG_INFO
,
5208 P2P_EVENT_CROSS_CONNECT_DISABLE
"%s %s",
5209 iface
->ifname
, iface
->cross_connect_uplink
);
5217 static void wpas_p2p_enable_cross_connect(struct wpa_supplicant
*uplink
)
5219 struct wpa_supplicant
*iface
;
5221 if (!uplink
->global
->cross_connection
)
5224 for (iface
= uplink
->global
->ifaces
; iface
; iface
= iface
->next
) {
5225 if (!iface
->cross_connect_enabled
)
5227 if (os_strcmp(uplink
->ifname
, iface
->cross_connect_uplink
) !=
5230 if (iface
->ap_iface
== NULL
)
5232 if (iface
->cross_connect_in_use
)
5235 iface
->cross_connect_in_use
= 1;
5236 wpa_msg(iface
->parent
, MSG_INFO
,
5237 P2P_EVENT_CROSS_CONNECT_ENABLE
"%s %s",
5238 iface
->ifname
, iface
->cross_connect_uplink
);
5243 static void wpas_p2p_disable_cross_connect(struct wpa_supplicant
*uplink
)
5245 struct wpa_supplicant
*iface
;
5247 for (iface
= uplink
->global
->ifaces
; iface
; iface
= iface
->next
) {
5248 if (!iface
->cross_connect_enabled
)
5250 if (os_strcmp(uplink
->ifname
, iface
->cross_connect_uplink
) !=
5253 if (!iface
->cross_connect_in_use
)
5256 wpa_msg(iface
->parent
, MSG_INFO
,
5257 P2P_EVENT_CROSS_CONNECT_DISABLE
"%s %s",
5258 iface
->ifname
, iface
->cross_connect_uplink
);
5259 iface
->cross_connect_in_use
= 0;
5264 void wpas_p2p_notif_connected(struct wpa_supplicant
*wpa_s
)
5266 if (wpa_s
->ap_iface
|| wpa_s
->current_ssid
== NULL
||
5267 wpa_s
->current_ssid
->mode
!= WPAS_MODE_INFRA
||
5268 wpa_s
->cross_connect_disallowed
)
5269 wpas_p2p_disable_cross_connect(wpa_s
);
5271 wpas_p2p_enable_cross_connect(wpa_s
);
5272 if (!wpa_s
->ap_iface
&&
5273 eloop_cancel_timeout(wpas_p2p_group_idle_timeout
, wpa_s
, NULL
) > 0)
5274 wpa_printf(MSG_DEBUG
, "P2P: Cancelled P2P group idle timeout");
5278 void wpas_p2p_notif_disconnected(struct wpa_supplicant
*wpa_s
)
5280 wpas_p2p_disable_cross_connect(wpa_s
);
5281 if (!wpa_s
->ap_iface
&&
5282 !eloop_is_timeout_registered(wpas_p2p_group_idle_timeout
,
5284 wpas_p2p_set_group_idle_timeout(wpa_s
);
5288 static void wpas_p2p_cross_connect_setup(struct wpa_supplicant
*wpa_s
)
5290 struct wpa_supplicant
*iface
;
5292 if (!wpa_s
->global
->cross_connection
)
5295 for (iface
= wpa_s
->global
->ifaces
; iface
; iface
= iface
->next
) {
5298 if (iface
->drv_flags
&
5299 WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE
)
5301 if (iface
->drv_flags
& WPA_DRIVER_FLAGS_P2P_CAPABLE
)
5304 wpa_s
->cross_connect_enabled
= 1;
5305 os_strlcpy(wpa_s
->cross_connect_uplink
, iface
->ifname
,
5306 sizeof(wpa_s
->cross_connect_uplink
));
5307 wpa_printf(MSG_DEBUG
, "P2P: Enable cross connection from "
5308 "%s to %s whenever uplink is available",
5309 wpa_s
->ifname
, wpa_s
->cross_connect_uplink
);
5311 if (iface
->ap_iface
|| iface
->current_ssid
== NULL
||
5312 iface
->current_ssid
->mode
!= WPAS_MODE_INFRA
||
5313 iface
->cross_connect_disallowed
||
5314 iface
->wpa_state
!= WPA_COMPLETED
)
5317 wpa_s
->cross_connect_in_use
= 1;
5318 wpa_msg(wpa_s
->parent
, MSG_INFO
,
5319 P2P_EVENT_CROSS_CONNECT_ENABLE
"%s %s",
5320 wpa_s
->ifname
, wpa_s
->cross_connect_uplink
);
5326 int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant
*wpa_s
)
5328 if (wpa_s
->p2p_group_interface
!= P2P_GROUP_INTERFACE_CLIENT
&&
5329 !wpa_s
->p2p_in_provisioning
)
5330 return 0; /* not P2P client operation */
5332 wpa_printf(MSG_DEBUG
, "P2P: Terminate connection due to WPS PBC "
5334 if (wpa_s
!= wpa_s
->parent
)
5335 wpa_msg_ctrl(wpa_s
->parent
, MSG_INFO
, WPS_EVENT_OVERLAP
);
5337 if (wpa_s
->global
->p2p
)
5338 p2p_group_formation_failed(wpa_s
->global
->p2p
);
5340 eloop_cancel_timeout(wpas_p2p_group_formation_timeout
,
5341 wpa_s
->parent
, NULL
);
5343 wpas_group_formation_completed(wpa_s
, 0);
5348 void wpas_p2p_update_channel_list(struct wpa_supplicant
*wpa_s
)
5350 struct p2p_channels chan
;
5352 if (wpa_s
->global
== NULL
|| wpa_s
->global
->p2p
== NULL
)
5355 os_memset(&chan
, 0, sizeof(chan
));
5356 if (wpas_p2p_setup_channels(wpa_s
, &chan
)) {
5357 wpa_printf(MSG_ERROR
, "P2P: Failed to update supported "
5362 p2p_update_channel_list(wpa_s
->global
->p2p
, &chan
);
5366 static void wpas_p2p_scan_res_ignore(struct wpa_supplicant
*wpa_s
,
5367 struct wpa_scan_results
*scan_res
)
5369 wpa_printf(MSG_DEBUG
, "P2P: Ignore scan results");
5373 int wpas_p2p_cancel(struct wpa_supplicant
*wpa_s
)
5375 struct wpa_global
*global
= wpa_s
->global
;
5379 if (global
->p2p
== NULL
)
5382 wpa_printf(MSG_DEBUG
, "P2P: Request to cancel group formation");
5384 if (wpa_s
->pending_interface_name
[0] &&
5385 !is_zero_ether_addr(wpa_s
->pending_interface_addr
))
5388 peer
= p2p_get_go_neg_peer(global
->p2p
);
5390 wpa_printf(MSG_DEBUG
, "P2P: Unauthorize pending GO Neg peer "
5391 MACSTR
, MAC2STR(peer
));
5392 p2p_unauthorize(global
->p2p
, peer
);
5396 if (wpa_s
->scan_res_handler
== wpas_p2p_scan_res_join
) {
5397 wpa_printf(MSG_DEBUG
, "P2P: Stop pending scan for join");
5398 wpa_s
->scan_res_handler
= wpas_p2p_scan_res_ignore
;
5402 if (wpa_s
->pending_pd_before_join
) {
5403 wpa_printf(MSG_DEBUG
, "P2P: Stop pending PD before join");
5404 wpa_s
->pending_pd_before_join
= 0;
5408 wpas_p2p_stop_find(wpa_s
);
5410 for (wpa_s
= global
->ifaces
; wpa_s
; wpa_s
= wpa_s
->next
) {
5411 if (wpa_s
== global
->p2p_group_formation
&&
5412 (wpa_s
->p2p_in_provisioning
||
5413 wpa_s
->parent
->pending_interface_type
==
5414 WPA_IF_P2P_CLIENT
)) {
5415 wpa_printf(MSG_DEBUG
, "P2P: Interface %s in group "
5416 "formation found - cancelling",
5419 eloop_cancel_timeout(wpas_p2p_group_formation_timeout
,
5420 wpa_s
->parent
, NULL
);
5421 if (wpa_s
->p2p_in_provisioning
) {
5422 wpas_group_formation_completed(wpa_s
, 0);
5425 wpas_p2p_group_delete(wpa_s
,
5426 P2P_GROUP_REMOVAL_REQUESTED
);
5432 wpa_printf(MSG_DEBUG
, "P2P: No ongoing group formation found");
5440 void wpas_p2p_interface_unavailable(struct wpa_supplicant
*wpa_s
)
5442 if (wpa_s
->current_ssid
== NULL
|| !wpa_s
->current_ssid
->p2p_group
)
5445 wpa_printf(MSG_DEBUG
, "P2P: Remove group due to driver resource not "
5446 "being available anymore");
5447 wpas_p2p_group_delete(wpa_s
, P2P_GROUP_REMOVAL_UNAVAILABLE
);
5451 void wpas_p2p_update_best_channels(struct wpa_supplicant
*wpa_s
,
5452 int freq_24
, int freq_5
, int freq_overall
)
5454 struct p2p_data
*p2p
= wpa_s
->global
->p2p
;
5455 if (p2p
== NULL
|| (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
))
5457 p2p_set_best_channels(p2p
, freq_24
, freq_5
, freq_overall
);
5461 int wpas_p2p_unauthorize(struct wpa_supplicant
*wpa_s
, const char *addr
)
5464 struct p2p_data
*p2p
= wpa_s
->global
->p2p
;
5466 if (p2p
== NULL
|| (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_P2P_MGMT
))
5469 if (hwaddr_aton(addr
, peer
))
5472 return p2p_unauthorize(p2p
, peer
);
5477 * wpas_p2p_disconnect - Disconnect from a P2P Group
5478 * @wpa_s: Pointer to wpa_supplicant data
5479 * Returns: 0 on success, -1 on failure
5481 * This can be used to disconnect from a group in which the local end is a P2P
5482 * Client or to end a P2P Group in case the local end is the Group Owner. If a
5483 * virtual network interface was created for this group, that interface will be
5484 * removed. Otherwise, only the configured P2P group network will be removed
5485 * from the interface.
5487 int wpas_p2p_disconnect(struct wpa_supplicant
*wpa_s
)
5493 return wpas_p2p_group_delete(wpa_s
, P2P_GROUP_REMOVAL_REQUESTED
) < 0 ?
5498 int wpas_p2p_in_progress(struct wpa_supplicant
*wpa_s
)
5502 if (wpa_s
->global
->p2p_disabled
|| wpa_s
->global
->p2p
== NULL
)
5505 ret
= p2p_in_progress(wpa_s
->global
->p2p
);
5508 * Check whether there is an ongoing WPS provisioning step (or
5509 * other parts of group formation) on another interface since
5510 * p2p_in_progress() does not report this to avoid issues for
5511 * scans during such provisioning step.
5513 if (wpa_s
->global
->p2p_group_formation
&&
5514 wpa_s
->global
->p2p_group_formation
!= wpa_s
) {
5515 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: Another interface (%s) "
5516 "in group formation",
5517 wpa_s
->global
->p2p_group_formation
->ifname
);
5526 void wpas_p2p_network_removed(struct wpa_supplicant
*wpa_s
,
5527 struct wpa_ssid
*ssid
)
5529 if (wpa_s
->p2p_in_provisioning
&& ssid
->p2p_group
&&
5530 eloop_cancel_timeout(wpas_p2p_group_formation_timeout
,
5531 wpa_s
->parent
, NULL
) > 0) {
5533 * Remove the network by scheduling the group formation
5534 * timeout to happen immediately. The teardown code
5535 * needs to be scheduled to run asynch later so that we
5536 * don't delete data from under ourselves unexpectedly.
5537 * Calling wpas_p2p_group_formation_timeout directly
5538 * causes a series of crashes in WPS failure scenarios.
5540 wpa_printf(MSG_DEBUG
, "P2P: Canceled group formation due to "
5541 "P2P group network getting removed");
5542 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout
,
5543 wpa_s
->parent
, NULL
);
5548 struct wpa_ssid
* wpas_p2p_get_persistent(struct wpa_supplicant
*wpa_s
,
5549 const u8
*addr
, const u8
*ssid
,
5555 for (s
= wpa_s
->conf
->ssid
; s
; s
= s
->next
) {
5556 if (s
->disabled
!= 2)
5559 (ssid_len
!= s
->ssid_len
||
5560 os_memcmp(ssid
, s
->ssid
, ssid_len
) != 0))
5562 if (os_memcmp(s
->bssid
, addr
, ETH_ALEN
) == 0)
5563 return s
; /* peer is GO in the persistent group */
5564 if (s
->mode
!= WPAS_MODE_P2P_GO
|| s
->p2p_client_list
== NULL
)
5566 for (i
= 0; i
< s
->num_p2p_clients
; i
++) {
5567 if (os_memcmp(s
->p2p_client_list
+ i
* ETH_ALEN
,
5568 addr
, ETH_ALEN
) == 0)
5569 return s
; /* peer is P2P client in persistent
5578 void wpas_p2p_notify_ap_sta_authorized(struct wpa_supplicant
*wpa_s
,
5583 wpas_p2p_add_persistent_group_client(wpa_s
, addr
);
5587 static void wpas_p2p_fallback_to_go_neg(struct wpa_supplicant
*wpa_s
,
5590 struct wpa_supplicant
*group
= wpa_s
;
5591 if (wpa_s
->global
->p2p_group_formation
)
5592 group
= wpa_s
->global
->p2p_group_formation
;
5593 wpa_s
= wpa_s
->parent
;
5594 offchannel_send_action_done(wpa_s
);
5596 wpas_p2p_group_delete(group
, P2P_GROUP_REMOVAL_SILENT
);
5597 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: Fall back to GO Negotiation");
5598 wpas_p2p_connect(wpa_s
, wpa_s
->pending_join_dev_addr
, wpa_s
->p2p_pin
,
5599 wpa_s
->p2p_wps_method
, wpa_s
->p2p_persistent_group
, 0,
5600 0, 0, wpa_s
->p2p_go_intent
, wpa_s
->p2p_connect_freq
,
5601 wpa_s
->p2p_persistent_id
,
5602 wpa_s
->p2p_pd_before_go_neg
,
5603 wpa_s
->p2p_go_ht40
);
5607 int wpas_p2p_scan_no_go_seen(struct wpa_supplicant
*wpa_s
)
5609 if (!wpa_s
->p2p_fallback_to_go_neg
||
5610 wpa_s
->p2p_in_provisioning
<= 5)
5613 if (wpas_p2p_peer_go(wpa_s
, wpa_s
->pending_join_dev_addr
) > 0)
5614 return 0; /* peer operating as a GO */
5616 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: GO not found for p2p_connect-auto - "
5617 "fallback to GO Negotiation");
5618 wpas_p2p_fallback_to_go_neg(wpa_s
, 1);
5624 unsigned int wpas_p2p_search_delay(struct wpa_supplicant
*wpa_s
)
5626 const char *rn
, *rn2
;
5627 struct wpa_supplicant
*ifs
;
5629 if (wpa_s
->wpa_state
> WPA_SCANNING
) {
5630 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: Use %u ms search delay due to "
5631 "concurrent operation",
5632 P2P_CONCURRENT_SEARCH_DELAY
);
5633 return P2P_CONCURRENT_SEARCH_DELAY
;
5636 if (!wpa_s
->driver
->get_radio_name
)
5638 rn
= wpa_s
->driver
->get_radio_name(wpa_s
->drv_priv
);
5639 if (rn
== NULL
|| rn
[0] == '\0')
5642 for (ifs
= wpa_s
->global
->ifaces
; ifs
; ifs
= ifs
->next
) {
5643 if (ifs
== wpa_s
|| !ifs
->driver
->get_radio_name
)
5646 rn2
= ifs
->driver
->get_radio_name(ifs
->drv_priv
);
5647 if (!rn2
|| os_strcmp(rn
, rn2
) != 0)
5649 if (ifs
->wpa_state
> WPA_SCANNING
) {
5650 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: Use %u ms search "
5651 "delay due to concurrent operation on "
5653 P2P_CONCURRENT_SEARCH_DELAY
, ifs
->ifname
);
5654 return P2P_CONCURRENT_SEARCH_DELAY
;